9
9
10
10
namespace Krypton . Toolkit ;
11
11
12
- public class KryptonTaskDialogElementContent : KryptonTaskDialogElementBase ,
12
+ public partial class KryptonTaskDialogElementContent : KryptonTaskDialogElementBase ,
13
13
IKryptonTaskDialogElementContent ,
14
14
IKryptonTaskDialogElementForeColor
15
15
{
16
16
#region Fields
17
17
// default text format flags
18
18
private const TextFormatFlags textFormatFlags = TextFormatFlags . WordBreak | TextFormatFlags . NoPadding | TextFormatFlags . ExpandTabs ;
19
19
20
- // Content text
21
- KryptonWrapLabel _textControl ;
22
- // Ttextbox width
23
- int _textBoxWidth ;
20
+ private KryptonWrapLabel _textBox ;
21
+ private TableLayoutPanel _tlp ;
22
+ private KryptonPictureBox _pictureBox ;
23
+ private bool _disposed ;
24
24
#endregion
25
25
26
26
#region Identity
27
27
public KryptonTaskDialogElementContent ( KryptonTaskDialogDefaults taskDialogDefaults )
28
28
: base ( taskDialogDefaults )
29
29
{
30
+ _disposed = false ;
31
+
30
32
Panel . Height = 120 ;
31
- _textBoxWidth = Defaults . ClientWidth - Defaults . PanelLeft - Defaults . PanelRight ;
33
+ Panel . Padding = Defaults . PanelPadding1 ;
34
+
35
+ ContentImage = new ContentImageStorage ( ) ;
36
+ ContentImage . PositionedLeft = true ;
37
+ ContentImage . PropertyChanged += OnContentImagePropertyChanged ;
38
+
39
+ _pictureBox = new ( ) ;
40
+ _tlp = new ( ) ;
41
+ _textBox = new ( ) ;
42
+
43
+ SetupPanel ( ) ;
44
+
45
+ LayoutDirty = true ;
46
+ OnSizeChanged ( ) ;
47
+ }
48
+ #endregion
49
+
50
+ #region Private
51
+ private void SetupPictureBox ( )
52
+ {
53
+ _pictureBox . Visible = false ;
54
+ _pictureBox . Margin = new Padding ( 0 , 0 , Defaults . ComponentSpace , 0 ) ;
55
+ _pictureBox . Padding = Defaults . NullPadding ;
56
+ _pictureBox . BorderStyle = BorderStyle . None ;
57
+ _pictureBox . SizeMode = PictureBoxSizeMode . CenterImage ;
58
+ _pictureBox . Anchor = AnchorStyles . Top | AnchorStyles . Left ;
59
+ }
60
+
61
+ private void SetupTableLayoutPanel ( )
62
+ {
63
+ _tlp . Left = Defaults . PanelLeft ;
64
+ _tlp . Top = Defaults . PanelTop ;
65
+ _tlp . AutoSize = true ;
66
+ _tlp . Margin = Defaults . PanelPadding1 ;
67
+ _tlp . AutoSizeMode = AutoSizeMode . GrowAndShrink ;
68
+ _tlp . MaximumSize = Defaults . TLP . StdMaxSize ;
69
+ _tlp . MinimumSize = Defaults . TLP . StdMinSize ;
70
+
71
+ // Partition the tlp.
72
+ _tlp . RowStyles . Clear ( ) ;
73
+ _tlp . ColumnStyles . Clear ( ) ;
74
+
75
+ _tlp . RowCount = 1 ;
76
+ _tlp . RowStyles . Add ( new RowStyle ( SizeType . AutoSize ) ) ;
77
+
78
+ _tlp . ColumnCount = 3 ;
79
+ _tlp . ColumnStyles . Add ( new ColumnStyle ( SizeType . AutoSize ) ) ;
80
+ _tlp . ColumnStyles . Add ( new ColumnStyle ( SizeType . AutoSize ) ) ;
81
+ _tlp . ColumnStyles . Add ( new ColumnStyle ( SizeType . AutoSize ) ) ;
82
+ }
83
+
84
+ private void SetupControls ( )
85
+ {
86
+ _textBox . AutoSize = false ;
87
+ _textBox . Height = 100 ;
88
+ _textBox . Padding = new Padding ( 0 ) ;
89
+ _textBox . Margin = new Padding ( 0 , 0 , 0 , 0 ) ;
90
+ _textBox . Location = new Point ( 10 , 10 ) ;
91
+ _textBox . Anchor = AnchorStyles . Top | AnchorStyles . Left | AnchorStyles . Right ;
92
+ }
93
+
94
+ private void SetupPanel ( )
95
+ {
96
+ SetupPictureBox ( ) ;
97
+ SetupControls ( ) ;
98
+ SetupTableLayoutPanel ( ) ;
99
+
100
+ // Put it together
101
+ _tlp . Controls . Add ( _textBox , 1 , 0 ) ;
102
+ _tlp . Controls . Add ( _pictureBox , 0 , 0 ) ;
103
+ Panel . Controls . Add ( _tlp ) ;
104
+ }
105
+
106
+ private void OnContentImagePropertyChanged ( ContentImageStorageProperties property )
107
+ {
108
+ if ( property is ContentImageStorageProperties . Image or ContentImageStorageProperties . Size )
109
+ {
110
+ if ( ContentImage . Image is not null )
111
+ {
112
+ // If the user has set width or height to zero the raw image size is used, whatever that may be.
113
+ _pictureBox . Size = ( ContentImage . Size . Width == 0 || ContentImage . Size . Height == 0 )
114
+ ? ContentImage . Image . Size
115
+ : ContentImage . Size ;
32
116
33
- _textControl = new ( )
117
+ _pictureBox . Image = new Bitmap ( ContentImage . Image , _pictureBox . Size ) ;
118
+ }
119
+ else
120
+ {
121
+ ContentImage . Visible = false ;
122
+ }
123
+ }
124
+ else if ( property == ContentImageStorageProperties . Visible )
125
+ {
126
+ _pictureBox . Visible = ContentImage . Image is not null && ContentImage . Visible ;
127
+ }
128
+ else if ( property == ContentImageStorageProperties . Position )
129
+ {
130
+ int columnIndex = ContentImage . PositionedLeft ? 0 : 2 ;
131
+
132
+ _tlp . Controls . Remove ( _pictureBox ) ;
133
+ _tlp . Controls . Add ( _pictureBox , columnIndex , 0 ) ;
134
+ }
135
+ else
34
136
{
35
- AutoSize = true ,
36
- Width = _textBoxWidth ,
37
- Height = 0 ,
38
- Padding = new Padding ( 0 ) ,
39
- Margin = new Padding ( 3 , 0 , 0 , 0 ) ,
40
- Location = new Point ( 10 , 10 ) ,
41
- MaximumSize = new Size ( _textBoxWidth , 0 ) ,
42
- MinimumSize = new Size ( _textBoxWidth , 0 ) ,
43
- Anchor = AnchorStyles . Top | AnchorStyles . Left | AnchorStyles . Right ,
44
- } ;
45
-
46
- Panel . Controls . Add ( _textControl ) ;
137
+ throw new ArgumentOutOfRangeException ( $ "Unknown ContentImageStorageProperties member: { property } ") ;
138
+ }
139
+
140
+ LayoutDirty = true ;
141
+ OnSizeChanged ( ) ;
47
142
}
48
143
#endregion
49
144
@@ -54,16 +149,17 @@ protected override void OnSizeChanged(bool performLayout = false)
54
149
// Updates / changes are deferred if the element is not visible or until PerformLayout is called
55
150
if ( LayoutDirty && ( Visible || performLayout ) )
56
151
{
57
- Font font = _textControl . StateCommon . Font
152
+ Font font = _textBox . StateCommon . Font
58
153
?? Palette . GetContentShortTextFont ( PaletteContentStyle . LabelNormalControl , PaletteState . Normal )
59
154
?? KryptonManager . CurrentGlobalPalette . BaseFont ;
60
155
61
- int height = TextRenderer . MeasureText ( _textControl . Text , font , new SizeF ( _textBoxWidth , float . MaxValue ) . ToSize ( ) , textFormatFlags ) . Height ;
156
+ _textBox . Width = _pictureBox . Visible
157
+ ? _tlp . MaximumSize . Width - _pictureBox . Width - _pictureBox . Margin . Horizontal
158
+ : _tlp . MaximumSize . Width ;
62
159
63
- // Controls seem to need a little help here to stay within the correct bounds
64
- Panel . Height = height + Defaults . PanelTop + Defaults . PanelBottom ;
65
- _textControl . Width = _textBoxWidth - Defaults . PanelLeft - Defaults . PanelRight ;
66
- _textControl . Height = height ;
160
+ _textBox . Height = TextRenderer . MeasureText ( _textBox . Text , font , new SizeF ( _textBox . Width , float . MaxValue ) . ToSize ( ) , textFormatFlags ) . Height ;
161
+
162
+ Panel . Height = _tlp . Height + Defaults . PanelTop + Defaults . PanelBottom ;
67
163
68
164
// Tell everybody about it when visible.
69
165
base . OnSizeChanged ( performLayout ) ;
@@ -104,19 +200,37 @@ public override bool Visible
104
200
OnSizeChanged ( ) ;
105
201
}
106
202
}
203
+
204
+ protected override void Dispose ( bool disposing )
205
+ {
206
+ if ( ! _disposed && disposing )
207
+ {
208
+ ContentImage . PropertyChanged -= OnContentImagePropertyChanged ;
209
+
210
+ _disposed = true ;
211
+ }
212
+
213
+ base . Dispose ( disposing ) ;
214
+ }
107
215
#endregion
108
216
109
217
#region Public
218
+ /// <summary>
219
+ /// Image to display together with the content.
220
+ /// </summary>
221
+ [ TypeConverter ( typeof ( ExpandableObjectConverter ) ) ]
222
+ public KryptonTaskDialogElementContent . ContentImageStorage ContentImage { get ; }
223
+
110
224
/// <inheritdoc/>
111
225
public string Text
112
226
{
113
- get => _textControl . Text ;
227
+ get => _textBox . Text ;
114
228
115
229
set
116
230
{
117
- if ( _textControl . Text != value )
231
+ if ( _textBox . Text != value )
118
232
{
119
- _textControl . Text = CommonHelper . NormalizeLineBreaks ( value ) + Environment . NewLine ;
233
+ _textBox . Text = CommonHelper . NormalizeLineBreaks ( value ) + Environment . NewLine ;
120
234
LayoutDirty = true ;
121
235
OnSizeChanged ( ) ;
122
236
}
@@ -125,8 +239,8 @@ public string Text
125
239
126
240
public Color ForeColor
127
241
{
128
- get => _textControl . StateCommon . TextColor ;
129
- set => _textControl . StateCommon . TextColor = value ;
242
+ get => _textBox . StateCommon . TextColor ;
243
+ set => _textBox . StateCommon . TextColor = value ;
130
244
}
131
245
#endregion
132
246
}
0 commit comments