Skip to content

Commit bb5be62

Browse files
authored
1376-V100-Extending-KryptonTaskDialog (#2526)
* 1376-V100-Extending-KryptonTaskDialog - Refactoring of property sub classes and made those event based leaving the logic in the parent class. - Fixed Header element alignment and label positioning - Added an optional image to the Content and Expander elements. - Some housekeeping * 1376-V100-Extending-KryptonTaskDialog - Refactoring of property sub classes and made those event based leaving the logic in the parent class. - Fixed Header element alignment and label positioning - Added an optional image to the Content and Expander elements. - Some housekeeping
1 parent ed2ac72 commit bb5be62

11 files changed

+549
-133
lines changed

Source/Krypton Components/Krypton.Toolkit/Controls Toolkit/KryptonTaskDialog/Elements/KryptonTaskDialogElementContent.cs

Lines changed: 144 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,136 @@
99

1010
namespace Krypton.Toolkit;
1111

12-
public class KryptonTaskDialogElementContent : KryptonTaskDialogElementBase,
12+
public partial class KryptonTaskDialogElementContent : KryptonTaskDialogElementBase,
1313
IKryptonTaskDialogElementContent,
1414
IKryptonTaskDialogElementForeColor
1515
{
1616
#region Fields
1717
// default text format flags
1818
private const TextFormatFlags textFormatFlags = TextFormatFlags.WordBreak | TextFormatFlags.NoPadding | TextFormatFlags.ExpandTabs;
1919

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;
2424
#endregion
2525

2626
#region Identity
2727
public KryptonTaskDialogElementContent(KryptonTaskDialogDefaults taskDialogDefaults)
2828
: base(taskDialogDefaults)
2929
{
30+
_disposed = false;
31+
3032
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;
32116

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
34136
{
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();
47142
}
48143
#endregion
49144

@@ -54,16 +149,17 @@ protected override void OnSizeChanged(bool performLayout = false)
54149
// Updates / changes are deferred if the element is not visible or until PerformLayout is called
55150
if (LayoutDirty && (Visible || performLayout))
56151
{
57-
Font font = _textControl.StateCommon.Font
152+
Font font = _textBox.StateCommon.Font
58153
?? Palette.GetContentShortTextFont(PaletteContentStyle.LabelNormalControl, PaletteState.Normal)
59154
?? KryptonManager.CurrentGlobalPalette.BaseFont;
60155

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;
62159

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;
67163

68164
// Tell everybody about it when visible.
69165
base.OnSizeChanged(performLayout);
@@ -104,19 +200,37 @@ public override bool Visible
104200
OnSizeChanged();
105201
}
106202
}
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+
}
107215
#endregion
108216

109217
#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+
110224
/// <inheritdoc/>
111225
public string Text
112226
{
113-
get => _textControl.Text;
227+
get => _textBox.Text;
114228

115229
set
116230
{
117-
if (_textControl.Text != value)
231+
if (_textBox.Text != value)
118232
{
119-
_textControl.Text = CommonHelper.NormalizeLineBreaks(value) + Environment.NewLine;
233+
_textBox.Text = CommonHelper.NormalizeLineBreaks(value) + Environment.NewLine;
120234
LayoutDirty = true;
121235
OnSizeChanged();
122236
}
@@ -125,8 +239,8 @@ public string Text
125239

126240
public Color ForeColor
127241
{
128-
get => _textControl.StateCommon.TextColor;
129-
set => _textControl.StateCommon.TextColor = value;
242+
get => _textBox.StateCommon.TextColor;
243+
set => _textBox.StateCommon.TextColor = value;
130244
}
131245
#endregion
132246
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#region BSD License
2+
/*
3+
*
4+
* New BSD 3-Clause License (https://github.com/Krypton-Suite/Standard-Toolkit/blob/master/LICENSE)
5+
* Modifications by Peter Wagner (aka Wagnerp), Simon Coghlan (aka Smurf-IV), Giduac, Ahmed Abdelhameed, tobitege et al. 2025 - 2025. All rights reserved.
6+
*
7+
*/
8+
#endregion
9+
10+
namespace Krypton.Toolkit;
11+
12+
public partial class KryptonTaskDialogElementContent
13+
{
14+
/// <summary>
15+
/// Used internally by KryptonTaskDialogElementContent
16+
/// </summary>
17+
public class ContentImageStorage :
18+
IKryptonTaskDialogElementContentImage,
19+
IKryptonTaskDialogElementPropertyChanged<ContentImageStorageProperties>
20+
{
21+
#region Events
22+
/// <summary>
23+
/// Subscribe to be notified when one of the properties changes.
24+
/// </summary>
25+
public event Action<ContentImageStorageProperties>? PropertyChanged;
26+
#endregion
27+
28+
#region Identity
29+
public ContentImageStorage()
30+
{
31+
Visible = false;
32+
Size = new Size(0, 0);
33+
Image = null;
34+
}
35+
#endregion
36+
37+
#region Private
38+
private void OnPropertyChanged(ContentImageStorageProperties property)
39+
{
40+
PropertyChanged?.Invoke(property);
41+
}
42+
#endregion
43+
44+
#region Public
45+
/// <summary>
46+
/// When true the image display left of the text otherwise on the right side.
47+
/// </summary>
48+
public bool PositionedLeft
49+
{
50+
get => field;
51+
52+
set
53+
{
54+
if (field != value)
55+
{
56+
field = value;
57+
OnPropertyChanged(ContentImageStorageProperties.Position);
58+
}
59+
}
60+
}
61+
62+
/// <summary>
63+
/// Desired size of the image.<br/>
64+
/// If the size is left to zero the image will be display using it's own size.
65+
/// </summary>
66+
public Size Size
67+
{
68+
get => field;
69+
70+
set
71+
{
72+
if (field != value)
73+
{
74+
field = value;
75+
OnPropertyChanged(ContentImageStorageProperties.Size);
76+
}
77+
}
78+
}
79+
80+
/// <inheritdoc/>
81+
public bool Visible
82+
{
83+
get => field;
84+
85+
set
86+
{
87+
if (field != value)
88+
{
89+
field = value;
90+
OnPropertyChanged(ContentImageStorageProperties.Visible);
91+
}
92+
}
93+
}
94+
95+
/// <inheritdoc/>
96+
[DefaultValue(null)]
97+
public Image? Image
98+
{
99+
get => field;
100+
101+
set
102+
{
103+
if (field != value)
104+
{
105+
field = value;
106+
OnPropertyChanged(ContentImageStorageProperties.Image);
107+
}
108+
}
109+
}
110+
public void ResetImage() => Image = null;
111+
112+
/// <summary>
113+
/// Not implemented
114+
/// </summary>
115+
/// <returns>String.Empty</returns>
116+
public sealed override string ToString()
117+
{
118+
return string.Empty;
119+
}
120+
#endregion
121+
}
122+
}
123+

Source/Krypton Components/Krypton.Toolkit/Controls Toolkit/KryptonTaskDialog/Elements/KryptonTaskDialogElementContentTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public KryptonTaskDialogElementContentTest(KryptonTaskDialogDefaults taskDialogD
7272

7373
Panel.Controls.Add(_tlp);
7474
//Panel.Controls.Add(_description);
75-
//Panel.Controls.Add(_textControl);
75+
//Panel.Controls.Add(_textBox);
7676

7777
_tlp.Controls.Add(_description, 0, 0);
7878
_tlp.Controls.Add(_textControl, 0, 1);

0 commit comments

Comments
 (0)