forked from ChineduOpara/KCS.Common.Windows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollapsiblePanel.cs
More file actions
491 lines (434 loc) · 15.6 KB
/
CollapsiblePanel.cs
File metadata and controls
491 lines (434 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using OVT.CustomControls.Properties;
namespace OVT.CustomControls
{
[Designer(typeof(CollapsiblePanelDesigner))]
[ToolboxBitmap(typeof(CollapsiblePanel), "OVT.CustomControls.CollapsiblePanel.bmp")]
[DefaultProperty("HeaderText")]
public partial class CollapsiblePanel : Panel
{
#region "Private members"
private bool collapse = false;
private int originalHight = 0;
private bool useAnimation;
private bool showHeaderSeparator = true;
private bool roundedCorners;
private int headerCornersRadius = 10;
private bool headerTextAutoEllipsis;
private string headerText;
private Color headerTextColor;
private Image headerImage;
private Font headerFont;
private RectangleF toolTipRectangle = new RectangleF();
private bool useToolTip = false;
#endregion
#region "Public Properties"
[Browsable(false)]
public new Color BackColor
{
get
{
return Color.Transparent;
}
set
{
base.BackColor = Color.Transparent;
}
}
[DefaultValue(false)]
[Description("Collapses the control when set to true")]
[Category("CollapsiblePanel")]
public bool Collapse
{
get { return collapse; }
set
{
// If using animation make sure to ignore requests for collapse or expand while a previous
// operation is in progress.
if (useAnimation)
{
// An operation is already in progress.
if (timerAnimation.Enabled)
{
return;
}
}
collapse = value;
CollapseOrExpand();
Refresh();
}
}
[DefaultValue(50)]
[Category("CollapsiblePanel")]
[Description("Specifies the speed (in ms) of Expand/Collapse operation when using animation. UseAnimation property must be set to true.")]
public int AnimationInterval
{
get
{
return timerAnimation.Interval ;
}
set
{
// Update animation interval only during idle times.
if(!timerAnimation.Enabled )
timerAnimation.Interval = value;
}
}
[DefaultValue(false)]
[Category("CollapsiblePanel")]
[Description("Indicate if the panel uses amination during Expand/Collapse operation")]
public bool UseAnimation
{
get { return useAnimation; }
set { useAnimation = value; }
}
[DefaultValue(true)]
[Category("CollapsiblePanel")]
[Description("When set to true draws panel borders, and shows a line separating the panel's header from the rest of the control")]
public bool ShowHeaderSeparator
{
get { return showHeaderSeparator; }
set
{
showHeaderSeparator = value;
Refresh();
}
}
[DefaultValue(false)]
[Category("CollapsiblePanel")]
[Description("When set to true, draws a panel with rounded top corners, the radius can bet set through HeaderCornersRadius property")]
public bool RoundedCorners
{
get
{
return roundedCorners;
}
set
{
roundedCorners = value;
Refresh();
}
}
[DefaultValue(10)]
[Category("CollapsiblePanel")]
[Description("Top corners radius, it should be in [1, 15] range")]
public int HeaderCornersRadius
{
get
{
return headerCornersRadius;
}
set
{
if (value < 1 || value > 15)
throw new ArgumentOutOfRangeException("HeaderCornersRadius", value, "Value should be in range [1, 90]");
else
{
headerCornersRadius = value;
Refresh();
}
}
}
[DefaultValue(false)]
[Category("CollapsiblePanel")]
[Description("Enables the automatic handling of text that extends beyond the width of the label control.")]
public bool HeaderTextAutoEllipsis
{
get { return headerTextAutoEllipsis; }
set
{
headerTextAutoEllipsis = value;
Refresh();
}
}
[Category("CollapsiblePanel")]
[Description("Text to show in panel's header")]
public string HeaderText
{
get { return headerText; }
set
{
headerText = value;
Refresh();
}
}
[Category("CollapsiblePanel")]
[Description("Color of text header, and panel's borders when ShowHeaderSeparator is set to true")]
public Color HeaderTextColor
{
get { return headerTextColor; }
set
{
headerTextColor = value;
Refresh();
}
}
[Category("CollapsiblePanel")]
[Description("Image that will be displayed in the top left corner of the panel")]
public Image HeaderImage
{
get { return headerImage; }
set
{
headerImage = value;
Refresh();
}
}
[Category("CollapsiblePanel")]
[Description("The font used to display text in the panel's header.")]
public Font HeaderFont
{
get { return headerFont; }
set
{
headerFont = value;
Refresh();
}
}
#endregion
public CollapsiblePanel()
{
InitializeComponent();
this.pnlHeader.Width = this.Width -1;
headerFont = new Font(Font, FontStyle.Bold);
headerTextColor = Color.Black;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
DrawHeaderPanel(e);
}
public void DrawHeaderCorners(Graphics g, Brush brush, float x, float y, float width, float height, float radius)
{
GraphicsPath gp = new GraphicsPath();
gp.AddLine(x + radius, y, x + width - (radius * 2), y); // Line
gp.AddArc(x + width - (radius * 2), y, radius * 2, radius * 2, 270, 90); // Corner
gp.AddLine(x + width, y + radius, x + width, y + height ); // Line
gp.AddLine(x + width , y + height, x , y + height); // Line
gp.AddLine(x, y + height , x, y + radius); // Line
gp.AddArc(x, y, radius * 2, radius * 2, 180, 90); // Corner
gp.CloseFigure();
g.FillPath(brush, gp);
if (showHeaderSeparator)
{
g.DrawPath(new Pen(headerTextColor), gp);
}
gp.Dispose();
}
private void DrawHeaderPanel(PaintEventArgs e)
{
Rectangle headerRect = pnlHeader.ClientRectangle;
LinearGradientBrush headerBrush = new LinearGradientBrush(
headerRect, Color.Snow, Color.LightBlue, LinearGradientMode.Horizontal);
if (!roundedCorners)
{
e.Graphics.FillRectangle(headerBrush, headerRect);
if (showHeaderSeparator)
{
e.Graphics.DrawRectangle(new Pen(headerTextColor), headerRect);
}
}
else
DrawHeaderCorners(e.Graphics, headerBrush, headerRect.X, headerRect.Y,
headerRect.Width, headerRect.Height, headerCornersRadius);
// Draw header separator
if (showHeaderSeparator)
{
Point start = new Point(pnlHeader.Location.X, pnlHeader.Location.Y+ pnlHeader.Height);
Point end = new Point(pnlHeader.Location.X+ pnlHeader.Width, pnlHeader.Location.Y+ pnlHeader.Height);
e.Graphics.DrawLine(new Pen(headerTextColor, 2), start, end);
// Draw rectangle lines for the rest of the control.
Rectangle bodyRect = this.ClientRectangle;
bodyRect.Y += this.pnlHeader.Height;
bodyRect.Height -= (this.pnlHeader.Height+1);
bodyRect.Width -= 1;
e.Graphics.DrawRectangle(new Pen(headerTextColor), bodyRect);
}
int headerRectHeight = pnlHeader.Height;
// Draw header image.
if (headerImage != null)
{
pictureBoxImage.Image = headerImage;
pictureBoxImage.Visible = true;
}
else
{
pictureBoxImage.Image = null;
pictureBoxImage.Visible = false;
}
// Calculate header string position.
if (!String.IsNullOrEmpty(headerText))
{
useToolTip = false;
int delta = pictureBoxExpandCollapse.Width+5;
int offset = 0;
if (headerImage != null)
{
offset = headerRectHeight;
}
PointF headerTextPosition = new PointF();
Size headerTextSize = TextRenderer.MeasureText(headerText, headerFont);
if (headerTextAutoEllipsis)
{
if (headerTextSize.Width >= headerRect.Width - (delta+offset))
{
RectangleF rectLayout =
new RectangleF((float)headerRect.X + offset,
(float)(headerRect.Height - headerTextSize.Height) / 2,
(float)headerRect.Width - delta,
(float)headerTextSize.Height);
StringFormat format = new StringFormat();
format.Trimming = StringTrimming.EllipsisWord;
e.Graphics.DrawString(headerText, headerFont, new SolidBrush(headerTextColor),
rectLayout, format);
toolTipRectangle = rectLayout;
useToolTip = true;
}
else
{
headerTextPosition.X = (offset + headerRect.Width - headerTextSize.Width) / 2;
headerTextPosition.Y = (headerRect.Height - headerTextSize.Height) / 2;
e.Graphics.DrawString(headerText, headerFont, new SolidBrush(headerTextColor),
headerTextPosition);
}
}
else
{
headerTextPosition.X = (offset + headerRect.Width - headerTextSize.Width) / 2;
headerTextPosition.Y = (headerRect.Height - headerTextSize.Height) / 2;
e.Graphics.DrawString(headerText, headerFont, new SolidBrush(headerTextColor),
headerTextPosition);
}
}
}
private void pictureBoxExpandCollapse_Click(object sender, EventArgs e)
{
Collapse = !Collapse;
}
private void CollapseOrExpand()
{
if (!useAnimation)
{
if (collapse)
{
originalHight = this.Height;
this.Height = pnlHeader.Height + 3;
pictureBoxExpandCollapse.Image = Resources.expand;
}
else
{
this.Height = originalHight;
pictureBoxExpandCollapse.Image = Resources.collapse;
}
}
else
{
// Keep original height only in case of a collapse operation.
if(collapse)
originalHight = this.Height;
timerAnimation.Enabled = true;
timerAnimation.Start();
}
}
private void pictureBoxExpandCollapse_MouseMove(object sender, MouseEventArgs e)
{
if (!timerAnimation.Enabled)
{
if (!collapse)
{
pictureBoxExpandCollapse.Image = Resources.collapse_hightlight;
}
else
pictureBoxExpandCollapse.Image = Resources.expand_highlight;
}
}
private void pictureBoxExpandCollapse_MouseLeave(object sender, EventArgs e)
{
if (!timerAnimation.Enabled)
{
if (!collapse)
{
pictureBoxExpandCollapse.Image = Resources.collapse;
}
else
pictureBoxExpandCollapse.Image = Resources.expand;
}
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
this.pnlHeader.Width = this.Width -1;
Refresh();
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
this.pnlHeader.Width = this.Width -1;
Refresh();
}
private void timerAnimation_Tick(object sender, EventArgs e)
{
if (collapse)
{
if (this.Height <= pnlHeader.Height + 3)
{
timerAnimation.Stop();
timerAnimation.Enabled = false;
pictureBoxExpandCollapse.Image = Resources.expand;
}
else
{
int newHight = this.Height - 20;
if (newHight <= pnlHeader.Height + 3)
newHight = pnlHeader.Height + 3;
this.Height = newHight;
}
}
else
{
if (this.Height >= originalHight)
{
timerAnimation.Stop();
timerAnimation.Enabled = false;
pictureBoxExpandCollapse.Image = Resources.collapse;
}
else
{
int newHeight = this.Height + 20;
if (newHeight >= originalHight)
newHeight = originalHight;
this.Height = newHeight;
}
}
}
private void pnlHeader_MouseHover(object sender, EventArgs e)
{
if (useToolTip)
{
Point p = this.PointToClient(Control.MousePosition);
if (toolTipRectangle.Contains(p))
{
toolTip.Show(headerText, pnlHeader, p);
}
}
}
private void pnlHeader_MouseLeave(object sender, EventArgs e)
{
if (useToolTip)
{
Point p = this.PointToClient(Control.MousePosition);
if (!toolTipRectangle.Contains(p))
{
toolTip.Hide(pnlHeader);
}
}
}
}
}