forked from jaysonragasa/MultiRDPClient.NET
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTreeListColumn.cs
More file actions
612 lines (577 loc) · 14.7 KB
/
TreeListColumn.cs
File metadata and controls
612 lines (577 loc) · 14.7 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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Diagnostics;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Reflection;
using System.Windows.Forms;
namespace CommonTools
{
public class HitInfo
{
public enum eHitType
{
kColumnHeader = 0x0001,
kColumnHeaderResize = 0x0002,
}
public eHitType HitType = 0;
public TreeListColumn Column = null;
}
/// <summary>
/// DesignTimeVisible(false) prevents the columns from showing in the component tray (bottom of screen)
/// If the class implement IComponent it must also implement default (void) constructor and when overriding
/// the collection editors CreateInstance the return object must be used (if implementing IComponent), the reason
/// is that ISite is needed, and ISite is set when base.CreateInstance is called.
/// If no default constructor then the object will not be added to the collection in the initialize.
/// In addition if implementing IComponent then name and generatemember is shown in the property grid
/// Columns should just be added to the collection, no need for member, so no need to implement IComponent
/// </summary>
[DesignTimeVisible(false)]
[TypeConverter(typeof(ColumnConverter))]
public class TreeListColumn
{
TreeList.TextFormatting m_headerFormat = new TreeList.TextFormatting();
TreeList.TextFormatting m_cellFormat = new TreeList.TextFormatting();
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public TreeList.TextFormatting HeaderFormat
{
get { return m_headerFormat; }
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public TreeList.TextFormatting CellFormat
{
get { return m_cellFormat; }
}
TreeListColumnCollection m_owner = null;
Rectangle m_calculatedRect;
int m_visibleIndex = -1;
int m_colIndex = -1;
int m_width = 50;
string m_fieldName = string.Empty;
string m_caption = string.Empty;
internal TreeListColumnCollection Owner
{
get { return m_owner; }
set { m_owner = value; }
}
internal Rectangle internalCalculatedRect
{
get { return m_calculatedRect; }
set { m_calculatedRect = value; }
}
internal int internalVisibleIndex
{
get { return m_visibleIndex; }
set { m_visibleIndex = value; }
}
internal int internalIndex
{
get { return m_colIndex; }
set { m_colIndex = value; }
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Rectangle CalculatedRect
{
get { return internalCalculatedRect; }
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public TreeListView TreeList
{
get
{
if (Owner == null)
return null;
return Owner.Owner;
}
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Font Font
{
get { return m_owner.Font; }
}
public int Width
{
get { return m_width; }
set
{
if (m_width == value)
return;
m_width = value;
if (m_owner != null && m_owner.DesignMode)
m_owner.RecalcVisibleColumsRect();
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string Caption
{
get { return m_caption; }
set
{
m_caption = value;
if (m_owner != null)
m_owner.Invalidate();
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string Fieldname
{
get { return m_fieldName; }
set
{
if (m_owner == null || m_owner.DesignMode == false)
throw new Exception("Fieldname can only be set at design time, Use Constructor to set programatically");
if (value.Length == 0)
throw new Exception("empty Fieldname not value");
if (m_owner[value] != null)
throw new Exception("fieldname already exist in collection");
m_fieldName = value;
}
}
public TreeListColumn(string fieldName)
{
m_fieldName = fieldName;
}
public TreeListColumn(string fieldName, string caption)
{
m_fieldName = fieldName;
m_caption = caption;
}
public TreeListColumn(string fieldName, string caption, int width)
{
m_fieldName = fieldName;
m_caption = caption;
m_width = width;
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public int VisibleIndex
{
get { return internalVisibleIndex; }
set
{
if (m_owner != null)
m_owner.SetVisibleIndex(this, value);
}
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public int Index
{
get { return internalIndex; }
}
public bool ishot = false;
public virtual void Draw(Graphics dc, ColumnHeaderPainter painter, Rectangle r)
{
painter.DrawHeader(dc, r, this, this.HeaderFormat, ishot);
}
bool m_autoSize = false;
float m_autoSizeRatio = 100;
int m_autoSizeMinSize;
[DefaultValue(false)]
public bool AutoSize
{
get { return m_autoSize; }
set { m_autoSize = value; }
}
[DefaultValue(100f)]
public float AutoSizeRatio
{
get { return m_autoSizeRatio; }
set { m_autoSizeRatio = value; }
}
public int AutoSizeMinSize
{
get { return m_autoSizeMinSize; }
set { m_autoSizeMinSize = value; }
}
int m_calculatedAutoSize;
internal int CalculatedAutoSize
{
get { return m_calculatedAutoSize; }
set { m_calculatedAutoSize = value; }
}
}
[Description("This is the columns collection")]
//[TypeConverterAttribute(typeof(ColumnsTypeConverter))]
[Editor(typeof(ColumnCollectionEditor),typeof(System.Drawing.Design.UITypeEditor))]
public class TreeListColumnCollection : IList<TreeListColumn>, IList
{
ColumnHeaderPainter m_painter = new ColumnHeaderPainter();
TreeList.CollumnSetting m_options;
TreeListView m_owner;
List<TreeListColumn> m_columns = new List<TreeListColumn>();
List<TreeListColumn> m_visibleCols = new List<TreeListColumn>();
Dictionary<string, TreeListColumn> m_columnMap = new Dictionary<string,TreeListColumn>();
[Browsable(false)]
public TreeList.CollumnSetting Options
{
get { return m_options; }
}
[Browsable(false)]
public ColumnHeaderPainter Painter
{
get { return m_painter; }
set { m_painter = value; }
}
[Browsable(false)]
public TreeListView Owner
{
get { return m_owner; }
}
[Browsable(false)]
public Font Font
{
get { return m_owner.Font; }
}
[Browsable(false)]
public TreeListColumn[] VisibleColumns
{
get { return m_visibleCols.ToArray(); }
}
[Browsable(false)]
public int ColumnsWidth
{
get
{
int width = 0;
foreach (TreeListColumn col in m_visibleCols)
{
if (col.AutoSize)
width += col.CalculatedAutoSize;
else
width += col.Width;
}
return width;
}
}
public TreeListColumnCollection(TreeListView owner)
{
m_owner = owner;
m_options = new TreeList.CollumnSetting(owner);
}
public TreeListColumn this[int index]
{
get
{
return m_columns[index];
}
set
{
m_columns[index] = value;
}
}
public TreeListColumn this[string fieldname]
{
get
{
TreeListColumn col;
m_columnMap.TryGetValue(fieldname, out col);
return col;
}
}
public void SetVisibleIndex(TreeListColumn col, int index)
{
m_visibleCols.Remove(col);
if (index >= 0)
{
if (index < m_visibleCols.Count)
m_visibleCols.Insert(index, col);
else
m_visibleCols.Add(col);
}
RecalcVisibleColumsRect();
}
public HitInfo CalcHitInfo(Point point, int horzOffset)
{
HitInfo info = new HitInfo();
info.Column = CalcHitColumn(point, horzOffset);
if ((info.Column != null) && (point.Y < Options.HeaderHeight))
{
info.HitType |= HitInfo.eHitType.kColumnHeader;
int right = info.Column.CalculatedRect.Right - horzOffset;
if (info.Column.AutoSize == false)
{
if (point.X >= right - 4 && point.X <= right)
info.HitType |= HitInfo.eHitType.kColumnHeaderResize;
}
}
return info;
}
public TreeListColumn CalcHitColumn(Point point, int horzOffset)
{
if (point.X < Options.LeftMargin)
return null;
foreach (TreeListColumn col in m_visibleCols)
{
int left = col.CalculatedRect.Left - horzOffset;
int right = col.CalculatedRect.Right - horzOffset;
if (point.X >= left && point.X <= right)
return col;
}
return null;
}
public void RecalcVisibleColumsRect()
{
RecalcVisibleColumsRect(false);
}
public void RecalcVisibleColumsRect(bool isDoingColumnResizing)
{
if (IsInitializing)
return;
int x = 0;//m_leftMargin;
if (m_owner.RowOptions.ShowHeader)
x = m_owner.RowOptions.HeaderWidth;
int y = 0;
int h = Options.HeaderHeight;
int index = 0;
foreach(TreeListColumn col in m_columns)
{
col.internalVisibleIndex = -1;
col.internalIndex = index++;
}
// calculate size requierd by fix columns and auto adjusted columns
// at the same time calculate total ratio value
int widthFixedColumns = 0;
int widthAutoSizeColumns = 0;
float totalRatio = 0;
foreach (TreeListColumn col in m_visibleCols)
{
if (col.AutoSize)
{
widthAutoSizeColumns += col.AutoSizeMinSize;
totalRatio += col.AutoSizeRatio;
}
else
widthFixedColumns += col.Width;
}
/*
CommonTools.Tracing.WriteLine(0, "\nRecalcVisibleColumsRect");
CommonTools.Tracing.WriteLine(0, "WidthRequired, auto {0}, fixed {1}, total {2}",
widthAutoSizeColumns,
widthFixedColumns,
widthAutoSizeColumns + widthFixedColumns);
*/
int clientWidth = m_owner.ClientRectangle.Width - m_owner.RowHeaderWidth();
// find ratio 'unit' value
float remainingWidth = clientWidth - (widthFixedColumns + widthAutoSizeColumns);
float ratioUnit = 0;
if (totalRatio > 0 && remainingWidth > 0)
ratioUnit = remainingWidth / totalRatio;
/*
CommonTools.Tracing.WriteLine(0, "ClientWidth {0}", clientWidth);
CommonTools.Tracing.WriteLine(0, "RemainngWidth {0}, TotalRatio {1}, RatioUnits {2}",
remainingWidth,
totalRatio,
ratioUnit);
*/
for (index = 0; index < m_visibleCols.Count; index++)
{
TreeListColumn col = m_visibleCols[index];
int width = col.Width;
if (col.AutoSize)
{
// if doing column resizing then keep adjustable columns fixed at last width
if (isDoingColumnResizing)
width = col.CalculatedAutoSize;
else
width = col.AutoSizeMinSize + (int)(ratioUnit * col.AutoSizeRatio) - 1;
col.CalculatedAutoSize = width;
}
col.internalCalculatedRect = new Rectangle(x, y, width, h);
col.internalVisibleIndex = index;
x += width;
}
Invalidate();
}
public void Draw(Graphics dc, Rectangle rect, int horzOffset)
{
foreach (TreeListColumn col in m_visibleCols)
{
Rectangle r = col.CalculatedRect;
r.X -= horzOffset;
if (r.Left > rect.Right)
break;
col.Draw(dc, m_painter, r);
}
// drwa row header filler
if (m_owner.RowOptions.ShowHeader)
{
Rectangle r = new Rectangle(0, 0, m_owner.RowOptions.HeaderWidth, Options.HeaderHeight);
m_painter.DrawHeaderFiller(dc, r);
}
}
public void AddRange(IEnumerable<TreeListColumn> columns)
{
foreach (TreeListColumn col in columns)
Add(col);
}
/// <summary>
/// AddRange(Item[]) is required for the designer.
/// </summary>
/// <param name="columns"></param>
public void AddRange(TreeListColumn[] columns)
{
foreach (TreeListColumn col in columns)
Add(col);
}
public void Add(TreeListColumn item)
{
bool designmode = Owner.DesignMode;
if (!designmode)
{
//Debug.Assert(m_columnMap.ContainsKey(item.Fieldname) == false);
//Debug.Assert(item.Owner == null, "column.Owner == null");
}
else
{
m_columns.Remove(item);
m_visibleCols.Remove(item);
}
item.Owner = this;
m_columns.Add(item);
m_visibleCols.Add(item);
m_columnMap[item.Fieldname] = item;
RecalcVisibleColumsRect();
//return item;
}
public void Clear()
{
m_columnMap.Clear();
m_columns.Clear();
m_visibleCols.Clear();
}
public bool Contains(TreeListColumn item)
{
return m_columns.Contains(item);
}
[Browsable(false)]
public int Count
{
get { return m_columns.Count; }
}
[Browsable(false)]
public bool IsReadOnly
{
get { return false; }
}
#region IList<TreeListColumn> Members
public int IndexOf(TreeListColumn item)
{
return m_columns.IndexOf(item);
}
public void Insert(int index, TreeListColumn item)
{
m_columns.Insert(index, item);
}
public void RemoveAt(int index)
{
m_columns.RemoveAt(index);
}
#endregion
#region ICollection<TreeListColumn> Members
public void CopyTo(TreeListColumn[] array, int arrayIndex)
{
m_columns.CopyTo(array, arrayIndex);
}
public bool Remove(TreeListColumn item)
{
return m_columns.Remove(item);
}
#endregion
#region IEnumerable<TreeListColumn> Members
public IEnumerator<TreeListColumn> GetEnumerator()
{
return m_columns.GetEnumerator();
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
#region IList Members
int IList.Add(object value)
{
Add((TreeListColumn)value);
return Count - 1;
}
bool IList.Contains(object value)
{
return Contains((TreeListColumn)value);
}
int IList.IndexOf(object value)
{
return IndexOf((TreeListColumn)value);
}
void IList.Insert(int index, object value)
{
Insert(index, (TreeListColumn)value);
}
bool IList.IsFixedSize
{
get { return false; }
}
void IList.Remove(object value)
{
Remove((TreeListColumn)value);
}
object IList.this[int index]
{
get
{
throw new Exception("The method or operation is not implemented.");
}
set
{
throw new Exception("The method or operation is not implemented.");
}
}
#endregion
#region ICollection Members
public void CopyTo(Array array, int index)
{
throw new Exception("The method or operation is not implemented.");
}
public bool IsSynchronized
{
get { throw new Exception("The method or operation is not implemented."); }
}
public object SyncRoot
{
get { throw new Exception("The method or operation is not implemented."); }
}
#endregion
internal bool DesignMode
{
get
{
if (m_owner != null)
return m_owner.DesignMode;
return false;
}
}
internal void Invalidate()
{
if (m_owner != null)
m_owner.Invalidate();
}
bool IsInitializing = false;
internal void BeginInit()
{
IsInitializing = true;
}
internal void EndInit()
{
IsInitializing = false;
RecalcVisibleColumsRect();
}
}
}