forked from Torabi/GHCustomControls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTabPanel.cs
426 lines (369 loc) · 16.7 KB
/
TabPanel.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
using GH_IO.Types;
using Grasshopper.GUI;
using Grasshopper.GUI.Canvas;
using Grasshopper.Kernel;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*
Original work Copyright (c) 2021 Ali Torabi ([email protected])
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.
*/
namespace GHCustomControls
{
public class TabPanel : GHParameter, IGHPanel
{
/// <summary>
/// value correspond to each tab
/// </summary>
List<int> _values;
public TabPanel(string name, string description, int activeTab, bool showTitle, bool showHeaderText=true, Bitmap toolTipDiagram = null) : base(name,description, activeTab,toolTipDiagram)
{
_tabs = new Dictionary<string, GHControl[]>();
_icons = new Dictionary<string, Image>();
_values = new List<int>();
_showTitle = showTitle;
_showHeaderText = showHeaderText;
_titleHeight = GH_FontServer.MeasureString(Name, SmallFont).Height + Offset;
}
public void Add(string name,int value, params GHControl[] controls)
{
foreach (var control in controls)
control.attributes = attributes;
_tabs.Add(name, controls);
_values.Add(value);
}
public void Add(string name,int value,Image image16x16, params GHControl[] controls)
{
foreach (var control in controls)
control.attributes = attributes;
_tabs.Add(name, controls);
_values.Add(value);
_icons.Add(name, image16x16);
}
public void Add(string name, params GHControl[] controls)
{
foreach (var control in controls)
control.attributes = attributes;
_tabs.Add(name, controls);
_values.Add(_values.Count);
}
public void Add(string name, Image image16x16, params GHControl[] controls)
{
foreach (var control in controls)
control.attributes = attributes;
_tabs.Add(name, controls);
_values.Add(_values.Count);
_icons.Add(name, image16x16);
}
/// <summary>
/// the tab name - imahe dictionary
/// </summary>
Dictionary<string, Image> _icons;
/// <summary>
/// the tab name - control array dictionary
/// </summary>
Dictionary<string, GHControl[]> _tabs;
/// <summary>
/// the rectangles of the headers
/// </summary>
List<RectangleF> _headers = new List<RectangleF>();
/// <summary>
/// the area of the tab contents
/// </summary>
RectangleF _tabRec;
/// <summary>
/// brush used to create the highlight
/// </summary>
LinearGradientBrush gradientBrush;
/// <summary>
/// if true then it shows the title of the panel group
/// </summary>
bool _showTitle;
/// <summary>
/// set to false if the icon is provided .
/// </summary>
bool _showHeaderText;
public override GH_Types DataType => GH_Types.gh_int32;
public override GH_ParamAccess Access => GH_ParamAccess.item;
RectangleF _bounds;
public override RectangleF Bounds
{
get => _bounds;
set
{
_bounds = value;
_headers = new List<RectangleF>();
float shift = (_bounds.Width - 4) / _tabs.Count;
float down = _showTitle ? _titleHeight : 0;
// updates the headers layout
var rec = new RectangleF(_bounds.Left + 2, _bounds.Top + down + Offset, shift, _tabHeight);
for (int i = 0; i < _tabs.Count; i++)
{
_headers.Add(rec);
rec.Offset(shift, 0);
}
gradientBrush = new LinearGradientBrush(new PointF(rec.Left, rec.Top), new PointF(rec.Left, rec.Bottom), Color.AntiqueWhite, Color.Transparent);
_tabRec = new RectangleF(_bounds.Left + 2, _bounds.Top + down +Offset + _tabHeight, _bounds.Width - 4, _bounds.Height - down - 2 * Offset - _tabHeight - 4);
this.SetChildrenBounds(0,_tabHeight+ (_showTitle?_titleHeight:0)+2);
}
}
public override int Offset => 4;
int _titleHeight;
public GHControl[] Items
{
get
{
int selectedTab = _values.IndexOf((int)CurrentValue);
if (selectedTab > -1)
return _tabs.Values.ToArray()[selectedTab];
else
return new GHControl[0];
}
set
{
}
}
#region layout
Orientation _orientation = Orientation.Vecrtical;
public Orientation Orientation
{
get => _orientation;
set { _orientation = value; }
}
/// <summary>
/// the height of the area where tab names are located
/// </summary>
int _tabHeight = 18;
internal override int GetHeight()
{
int titleHeight = (_showTitle) ? _titleHeight : 0;
return _tabHeight + titleHeight +this.GetChildrenHeight(Items) + 2 * Offset;
}
internal override int GetWidth()
{
// find the total tab width by summing up all the tab names
int w1 = _tabs.Keys.Aggregate(
Offset, (int w, string key) =>
w
+ (_showHeaderText?GH_FontServer.StringWidth(key, SmallFont):0)
+ (_icons.ContainsKey(key)&&_icons[key]!=null? 18:0) + Offset
);
// now we find the tab maximum tab size
int w2 = this.GetChildrenWidth(Items);
return Math.Max(w1, w2);
}
#endregion
#region Mouse events
internal override void MouseLeftClick(GH_Canvas sender, GHCustomComponent customComponent, GH_CanvasMouseEvent e, ref GHMouseEventResult result)
{
// firts check if one of the headers has been clicked
for (int i = 0; i < _headers.Count; i++)
{
RectangleF rec = _headers[i];
if (rec.Contains(e.CanvasLocation))
{
// now check if this is selected header?
if ((int)CurrentValue == i)
return; // nothing happens
// another tab is selected , we switch the tab
CurrentValue = _values[i];
// change the items with items inside the tab
//Items = _tabs.Values.ToArray()[i];
// update the mouse event
result = result | GHMouseEventResult.Handled | GHMouseEventResult.UpdateSolution;
attributes?.Redraw();
break;
}
}
// click was out side of the header area, pass the click to the children
this.MouseClickChildren(sender, customComponent, e, ref result);
}
internal override void MouseLeave(GH_Canvas sender, GHCustomComponent customComponent, GH_CanvasMouseEvent e, ref GHMouseEventResult result)
{
// the cursor is already outside this control we must remove the heighlight from the header
if (HighlightedHeader > -1)
{
HighlightedHeader = -1;
result = result | GHMouseEventResult.Invalidated;
}
// when cursor leaves the panel , it leaves all the childs
this.MouseOverChildren(sender, customComponent, e, ref result);
}
int HighlightedHeader = -1;
internal override void MouseOver(GH_Canvas sender, GHCustomComponent customComponent, GH_CanvasMouseEvent e, ref GHMouseEventResult result)
{
//when cursor is hovering above the control we check if it highlights any of header
for (int i = 0; i < _headers.Count; i++)
{
if (_headers[i].Contains(e.CanvasLocation))
{
if (i != HighlightedHeader)
{
// highlight chnages
HighlightedHeader = i;
result = result | GHMouseEventResult.Invalidated;
}
return; // no need to check the children!
}
}
//the cursor was not above any header so we check the child
this.MouseOverChildren(sender, customComponent, e, ref result);
}
internal override void SetupToolTip(PointF canvasPoint, GH_TooltipDisplayEventArgs e)
{
if (this.SetChildrenToolTip(canvasPoint, e))
return;
int i = _headers.FindIndex(rec => rec.Contains(canvasPoint));
if (i == -1)
{
base.SetupToolTip(canvasPoint,e);
}
else
{
e.Title = _tabs.ToArray()[i].Key;
//e.Text = Description;
}
}
internal override void MouseRightClick(GH_Canvas sender, GHCustomComponent customComponent, GH_CanvasMouseEvent e, ref GHMouseEventResult result)
{
// click was out side of the header area, pass the click to the children
this.MouseClickChildren( sender, customComponent, e, ref result);
}
#endregion
private PointF[] headerShape(RectangleF rec)
{
return new PointF[]
{
new PointF(rec.Left,rec.Bottom),
new PointF(rec.Left+2,rec.Top),
new PointF(rec.Right-2,rec.Top),
new PointF(rec.Right,rec.Bottom),
new PointF(rec.Left,rec.Bottom)
};
}
internal override void Render(Graphics graphics,PointF cursorCanvasPosition, bool selected, bool locked, bool hidden)
{
// render the frame
Helpers.DrawFrame(Bounds, graphics, (_showTitle)?Name:"", selected, !Enabled, hidden);
// render the headers,
int selectedTab = _values.IndexOf((int)CurrentValue);
for (int i = 0; i < _headers.Count; i++)
{
//first draw only the unselected ones
if (i != selectedTab)
{
//GH_Capsule capsule = GH_Capsule.CreateCapsule(_headers[i], GH_Palette.Transparent, 2, 0);
//capsule.Render(graphics, new GH_PaletteStyle((Highlighted == i) ? Color.LightGoldenrodYellow : Color.Transparent, Color.Black));
//capsule.Dispose();
PointF[] poly = headerShape(_headers[i]);
if (_headers[i].Contains(cursorCanvasPosition) && Enabled)
{
graphics.FillPolygon(gradientBrush, poly);
}
using (SolidBrush b = new SolidBrush(Color.FromArgb(50, Color.Gray)))
{
graphics.FillPolygon(b, poly);
}
using (Pen p = new Pen(Brushes.Black))
{
graphics.DrawPolygon(p, poly);
}
string tabName = _tabs.Keys.ToArray()[i];
Image icon = _icons.ContainsKey(tabName) ? _icons[tabName] : null;
if (icon != null)
{
graphics.DrawImage(icon, _headers[i].Left + 1, _headers[i].Top + 1);
}
// draw the text
if (_showHeaderText)
{
if (icon == null)
{
using (StringFormat s = new StringFormat() { Alignment = StringAlignment.Center })
{
graphics.DrawString(
tabName, SmallFont, this.ActiveBrush(), _headers[i], s
);
}
}
else
{
using (StringFormat s = new StringFormat() { Alignment = StringAlignment.Near })
{
graphics.DrawString(
tabName, SmallFont, this.ActiveBrush(), _headers[i].Left+1+icon.Width,_headers[i].Top, s
);
}
}
}
}
}
if (_headers.Count == 0)
return;
// now draw the border
//int selectedTab = (int)CurrentValue;
//RectangleF rect = new RectangleF(Bounds.Left + 2, Bounds.Top + _fontSize + _tabHeight, Bounds.Width - 4, Bounds.Height - _fontSize - _tabHeight - 2);
RectangleF header = _headers[selectedTab];
PointF[] tabPoly = new PointF[]
{
new PointF(_tabRec.Left,_tabRec.Top),
new PointF(header.Left,header.Bottom),
new PointF(header.Left+2,header.Top),
new PointF(header.Right-2,header.Top),
new PointF(header.Right,header.Bottom),
new PointF(_tabRec.Right,_tabRec.Top),
new PointF(_tabRec.Right,_tabRec.Bottom+2),
new PointF(_tabRec.Left,_tabRec.Bottom+2),
new PointF(_tabRec.Left,_tabRec.Top)
};
graphics.FillPolygon(new SolidBrush(Color.FromArgb(50, Color.Gray)), tabPoly);
graphics.DrawPolygon(new Pen(Brushes.Black, (Enabled)?2:1), tabPoly);
//// draw the active tab
//int selectedTab = (int)CurrentValue;
//GH_Capsule capsule2 = GH_Capsule.CreateCapsule(_headers[selectedTab], GH_Palette.Transparent, 2, 0);
//capsule2.Render(graphics, new GH_PaletteStyle((Highlighted == selectedTab) ? Color.LightGoldenrodYellow : Color.Transparent, Color.Black));
//capsule2.Dispose();
// draw the text
string selectedTabName = _tabs.Keys.ToArray()[selectedTab];
Image slectedicon = _icons.ContainsKey(selectedTabName) ? _icons[selectedTabName] : null;
if (slectedicon != null)
{
graphics.DrawImage(slectedicon, _headers[selectedTab].Left + 1, _headers[selectedTab].Top + 1);
}
if (_showHeaderText)
{
using (Font f = new Font(SmallFont, FontStyle.Bold))
{
if (slectedicon == null)
{
using (StringFormat s = new StringFormat() { Alignment = StringAlignment.Center })
graphics.DrawString(
selectedTabName, f, this.ActiveBrush(), _headers[selectedTab], s);
}
else
{
using (StringFormat s = new StringFormat() { Alignment = StringAlignment.Near })
graphics.DrawString(
selectedTabName, f, this.ActiveBrush(), _headers[selectedTab].Left+1+ slectedicon.Width, _headers[selectedTab].Top, s);
}
}
}
// draw the items
foreach (GHControl control in Items)
if (control.IsVisible)
control.Render(graphics, cursorCanvasPosition, selected, !Enabled, hidden);
}
}
}