-
Notifications
You must be signed in to change notification settings - Fork 80
/
MetroSetTabControl.cs
483 lines (399 loc) · 15.9 KB
/
MetroSetTabControl.cs
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
/**
* MetroSet UI - MetroSet UI Framewrok
*
* The MIT License (MIT)
* Copyright (c) 2011 Narwin, https://github.com/N-a-r-w-i-n
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in the
* Software without restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using MetroSet_UI.Child;
using MetroSet_UI.Design;
using MetroSet_UI.Enums;
using MetroSet_UI.Extensions;
using MetroSet_UI.Interfaces;
using MetroSet_UI.Property;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace MetroSet_UI.Controls
{
[ToolboxItem(true)]
[ToolboxBitmap(typeof(MetroSetTabControl), "Bitmaps.TabControl.bmp")]
[Designer(typeof(MetroSetTabControlDesigner))]
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDispatch)]
public class MetroSetTabControl : TabControl, iControl
{
#region Interfaces
/// <summary>
/// Gets or sets the style associated with the control.
/// </summary>
[Category("MetroSet Framework"), Description("Gets or sets the style associated with the control.")]
public Style Style
{
get
{
return StyleManager?.Style ?? style;
}
set
{
style = value;
switch (value)
{
case Style.Light:
ApplyTheme();
break;
case Style.Dark:
ApplyTheme(Style.Dark);
break;
case Style.Custom:
ApplyTheme(Style.Custom);
break;
default:
ApplyTheme();
break;
}
Invalidate();
}
}
/// <summary>
/// Gets or sets the Style Manager associated with the control.
/// </summary>
[Category("MetroSet Framework"), Description("Gets or sets the Style Manager associated with the control.")]
public StyleManager StyleManager
{
get { return _StyleManager; }
set { _StyleManager = value; Invalidate(); }
}
/// <summary>
/// Gets or sets the The Author name associated with the theme.
/// </summary>
[Category("MetroSet Framework"), Description("Gets or sets the The Author name associated with the theme.")]
public string ThemeAuthor { get; set; }
/// <summary>
/// Gets or sets the The Theme name associated with the theme.
/// </summary>
[Category("MetroSet Framework"), Description("Gets or sets the The Theme name associated with the theme.")]
public string ThemeName { get; set; }
#endregion Interfaces
#region Global Vars
private static TabControlProperties prop;
private Methods mth;
private Utilites utl;
#endregion Global Vars
#region Internal Vars
private Style style;
private StyleManager _StyleManager;
#endregion Internal Vars
#region Constructors
public MetroSetTabControl()
{
SetStyle(
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.SupportsTransparentBackColor, true);
DoubleBuffered = true;
UpdateStyles();
ItemSize = new Size(100, 38);
DrawMode = TabDrawMode.Normal;
SizeMode = TabSizeMode.Fixed;
Font = MetroSetFonts.UIRegular(8);
prop = new TabControlProperties();
mth = new Methods();
utl = new Utilites();
style = Style.Light;
ApplyTheme();
}
#endregion Constructors
#region ApplyTheme
/// <summary>
/// Gets or sets the style provided by the user.
/// </summary>
/// <param name="style">The Style.</param>
internal void ApplyTheme(Style style = Style.Light)
{
switch (style)
{
case Style.Light:
prop.ForeColor = Color.FromArgb(65, 177, 225);
prop.BackColor = Color.White;
prop.UnselectedTextColor = Color.Gray;
prop.SelectedTextColor = Color.White;
ThemeAuthor = "Narwin";
ThemeName = "MetroLite";
SetProperties();
break;
case Style.Dark:
prop.ForeColor = Color.FromArgb(65, 177, 225);
prop.BackColor = Color.FromArgb(30, 30, 30);
prop.UnselectedTextColor = Color.Gray;
prop.SelectedTextColor = Color.White;
ThemeAuthor = "Narwin";
ThemeName = "MetroDark";
SetProperties();
break;
case Style.Custom:
if (StyleManager != null)
foreach (var varkey in StyleManager.TabControlDictionary)
{
switch (varkey.Key)
{
case "ForeColor":
prop.ForeColor = utl.HexColor((string)varkey.Value);
break;
case "BackColor":
prop.BackColor = utl.HexColor((string)varkey.Value);
break;
case "UnselectedTextColor":
prop.UnselectedTextColor = utl.HexColor((string)varkey.Value);
break;
case "SelectedTextColor":
prop.SelectedTextColor = utl.HexColor((string)varkey.Value);
break;
default:
return;
}
}
SetProperties();
break;
}
}
public void SetProperties()
{
try
{
BaseColor = prop.BackColor;
ForeroundColor = prop.ForeColor;
UnselectedTextColor = prop.UnselectedTextColor;
SelectedTextColor = prop.SelectedTextColor;
InvalidateTabPage(BaseColor);
Invalidate();
}
catch (Exception ex)
{
throw new Exception(ex.StackTrace);
}
}
#endregion ApplyTheme
#region Properties
[Category("MetroSet Framework")]
[Editor(typeof(MetroSetTabPageCollectionEditor), typeof(UITypeEditor))]
public new TabPageCollection TabPages
{
get { return base.TabPages; }
}
/// <summary>
/// Gets or sets wether the tabcontrol use animation or not.
/// </summary>
[Category("MetroSet Framework"), Description("Gets or sets wether the tabcontrol use animation or not.")]
public bool UseAnimation { get; set; } = true;
[Category("MetroSet Framework")]
public new Size ItemSize
{
get { return base.ItemSize; }
set
{
base.ItemSize = value;
Invalidate();
}
}
/// <summary>
/// Gets or sets the speed of transition.
/// </summary>
[Category("MetroSet Framework"), Description("Gets or sets the speed of transition.")]
public int Speed { get; set; } = 20;
[Category("MetroSet Framework")]
public override DockStyle Dock { get => base.Dock; set => base.Dock = value; }
[Category("MetroSet Framework")]
[Browsable(false)]
public new TabSizeMode SizeMode { get; set; } = TabSizeMode.Fixed;
[Category("MetroSet Framework")]
[Browsable(false)]
public new TabDrawMode DrawMode { get; set; } = TabDrawMode.Normal;
[Category("MetroSet Framework")]
[Browsable(false)]
private Color BaseColor { get; set; }
[Category("MetroSet Framework")]
[Browsable(false)]
private Color ForeroundColor { get; set; }
[Category("MetroSet Framework")]
[Browsable(false)]
private Color UnselectedTextColor { get; set; }
[Category("MetroSet Framework")]
[Browsable(false)]
private Color SelectedTextColor { get; set; }
/// <summary>
/// Gets or sets the tancontrol apperance style
/// </summary>
[Category("MetroSet Framework"), Description("Gets or sets the tancontrol apperance style.")]
public TabStyle TabStyle { get; set; } = TabStyle.Style1;
#endregion Properties
#region Draw Control
protected override void OnPaint(PaintEventArgs e)
{
var G = e.Graphics;
G.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
G.Clear(BaseColor);
var h = ItemSize.Height + 2;
switch (TabStyle)
{
case TabStyle.Style1:
using (Pen SB = new Pen(ForeroundColor, 2))
{
G.DrawLine(SB, 2, h, Width - 3, h);
}
for (int i = 0; i <= TabCount - 1; i++)
{
var r = GetTabRect(i);
if (i == SelectedIndex)
{
using (SolidBrush SB = new SolidBrush(ForeroundColor))
{
G.FillRectangle(SB, r);
}
}
using (SolidBrush TB = new SolidBrush(i == SelectedIndex ? SelectedTextColor : UnselectedTextColor))
{
G.DrawString(TabPages[i].Text, Font, TB, r, mth.SetPosition());
}
}
break;
case TabStyle.Style2:
for (int i = 0; i <= TabCount - 1; i++)
{
var r = GetTabRect(i);
if (i == SelectedIndex)
{
using (Pen SB = new Pen(ForeroundColor, 2))
{
G.DrawLine(SB, r.X, r.Height, r.X + r.Width, r.Height);
}
}
using (SolidBrush TB = new SolidBrush(i == SelectedIndex ? SelectedTextColor : UnselectedTextColor))
{
G.DrawString(TabPages[i].Text, Font, TB, r, mth.SetPosition());
}
}
break;
}
}
#endregion Draw Control
#region Events
#region Animation
// Credits : Mavamaarten
private int OldIndex;
public void DoAnimationScrollLeft(Control Control1, Control Control2)
{
Graphics G = Control1.CreateGraphics();
Bitmap P1 = new Bitmap(Control1.Width, Control1.Height);
Bitmap P2 = new Bitmap(Control2.Width, Control2.Height);
Control1.DrawToBitmap(P1, new Rectangle(0, 0, Control1.Width, Control1.Height));
Control2.DrawToBitmap(P2, new Rectangle(0, 0, Control2.Width, Control2.Height));
foreach (Control c in Control1.Controls)
{
c.Hide();
}
int Slide = Control1.Width - (Control1.Width % Speed);
int a = 0;
for (a = 0; a <= Slide; a += Speed)
{
G.DrawImage(P1, new Rectangle(a, 0, Control1.Width, Control1.Height));
G.DrawImage(P2, new Rectangle(a - Control2.Width, 0, Control2.Width, Control2.Height));
}
a = Control1.Width;
G.DrawImage(P1, new Rectangle(a, 0, Control1.Width, Control1.Height));
G.DrawImage(P2, new Rectangle(a - Control2.Width, 0, Control2.Width, Control2.Height));
SelectedTab = (TabPage)Control2;
foreach (Control c in Control2.Controls)
{
c.Show();
}
foreach (Control c in Control1.Controls)
{
c.Show();
}
}
protected override void OnSelecting(TabControlCancelEventArgs e)
{
if (UseAnimation)
{
if (OldIndex < e.TabPageIndex)
{
DoAnimationScrollRight(TabPages[OldIndex], TabPages[e.TabPageIndex]);
}
else
{
DoAnimationScrollLeft(TabPages[OldIndex], TabPages[e.TabPageIndex]);
}
}
}
protected override void OnDeselecting(TabControlCancelEventArgs e)
{
OldIndex = e.TabPageIndex;
}
public void DoAnimationScrollRight(Control Control1, Control Control2)
{
Graphics G = Control1.CreateGraphics();
Bitmap P1 = new Bitmap(Control1.Width, Control1.Height);
Bitmap P2 = new Bitmap(Control2.Width, Control2.Height);
Control1.DrawToBitmap(P1, new Rectangle(0, 0, Control1.Width, Control1.Height));
Control2.DrawToBitmap(P2, new Rectangle(0, 0, Control2.Width, Control2.Height));
foreach (Control c in Control1.Controls)
{
c.Hide();
}
int Slide = Control1.Width - (Control1.Width % Speed);
int a = 0;
for (a = 0; a >= -Slide; a += -Speed)
{
G.DrawImage(P1, new Rectangle(a, 0, Control1.Width, Control1.Height));
G.DrawImage(P2, new Rectangle(a + Control2.Width, 0, Control2.Width, Control2.Height));
}
a = Control1.Width;
G.DrawImage(P1, new Rectangle(a, 0, Control1.Width, Control1.Height));
G.DrawImage(P2, new Rectangle(a + Control2.Width, 0, Control2.Width, Control2.Height));
SelectedTab = (TabPage)Control2;
foreach (Control c in Control2.Controls)
{
c.Show();
}
foreach (Control c in Control1.Controls)
{
c.Show();
}
}
#endregion Animation
#endregion Events
#region Methods
private void InvalidateTabPage(Color C)
{
foreach (MetroSetTabPage T in TabPages)
{
T.Style = Style;
T.BaseColor = BaseColor;
T.Invalidate();
}
}
#endregion
}
}