forked from ChrisS85/CGUI
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCTreeViewControl.ahk
636 lines (576 loc) · 17.9 KB
/
CTreeViewControl.ahk
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*
Class: CTreeViewControl
A TreeView control.
This control extends <CControl>. All basic properties and functions are implemented and documented in this class.
*/
Class CTreeViewControl Extends CControl
{
static registration := CGUI.RegisterControl("TreeView", CTreeViewControl)
EditingStart := new EventHandler()
DoubleClick := new EventHandler()
EditingEnd := new EventHandler()
ItemSelected := new EventHandler()
Click := new EventHandler()
RightClick := new EventHandler()
ItemExpanded := new EventHandler()
ItemCollapsed := new EventHandler()
FocusReceived := new EventHandler()
FocusLost := new EventHandler()
KeyPress := new EventHandler()
__New(Name, ByRef Options, Text, GUINum)
{
if(!InStr(Options, "AltSubmit")) ;Automagically add AltSubmit
Options .= " AltSubmit"
base.__New(Name, Options, Text, GUINum)
this._.Insert("ControlStyles", {Checked : 0x100, ReadOnly : -0x8, FullRowSelect : 0x1000, Buttons : 0x1, Lines : 0x2, HScroll : -0x8000, AlwaysShowSelection : 0x20, SingleExpand : 0x400, HotTrack : 0x200})
this._.Insert("Events", ["DoubleClick", "EditingEnd", "ItemSelected", "Click", "RightClick", "EditingStart", "KeyPress", "ItemExpanded", "ItemCollapsed", "FocusReceived", "FocusLost"])
; this._.Insert("Messages", {0x004E : "Notify"}) ;This control uses WM_NOTIFY with NM_SETFOCUS and NM_KILLFOCUS
this.Type := "TreeView"
}
PostCreate()
{
Base.PostCreate()
this._.ImageListManager := new this.CImageListManager(this.GUINum, this.hwnd)
this._.Items := new this.CItem(0, this.GUINum, this.hwnd)
this._.PreviouslySelectedItem := this._.Items
}
/*
Function: FindItem
Finds an item by its ID.
Parameters:
ID - The ID of the item.
*/
FindItem(ID, Root = "")
{
if(!ID) ;Root node
return this.Items
if(!IsObject(Root))
Root := this.Items
if(ID = Root.ID)
return Root
Loop % Root.MaxIndex()
if(result := this.FindItem(ID, Root[A_Index]))
return result
return 0
}
/*
Function: FindItemWithText
Finds an item by its text.
Parameters:
Text - The text of the item.
*/
FindItemWithText(Text, Root = "")
{
if(!Text) ;Root node
return this.Items
if(!IsObject(Root))
Root := this.Items
if(Text = Root.Text)
return Root
Loop % Root.MaxIndex()
if(result := this.FindItemWithText(Text, Root[A_Index]))
return result
return 0
}
/*
Property: Items
Contains the nodes of the tree. Each level can be iterated and indexed. A node is of type <CTreeViewControl.CItem>
Property: SelectedItem
Contains the node of type <CItem> that is currently selected.
Property: PreviouslySelectedItem
Contains the node of type <CItem> that was previously selected.
*/
__Get(Name, Params*)
{
if(Name = "Items")
Value := this._.Items
else if(Name = "SelectedItem")
{
GUI := CGUI.GUIList[this.GUINum]
if(GUI.IsDestroyed)
return
Gui, % this.GUINum ":Default"
Gui, TreeView, % this.ClassNN
Value := this.FindItem(TV_GetSelection())
}
else if Name is Number ;Prevent accidentally trying to access the tree items through the control class directly
throw Exception("Tree Items can't be accessed through the control directly! Use CTreeViewControl.Items[index] instead!", -1)
Loop % Params.MaxIndex()
if(IsObject(Value)) ;Fix unlucky multi parameter __GET
Value := Value[Params[A_Index]]
if(Value)
return Value
}
__Set(Name, Params*)
{
if(!CGUI.GUIList[this.GUINum].IsDestroyed)
{
;Fix completely weird __Set behavior. If one tries to assign a value to a sub item, it doesn't call __Get for each sub item but __Set with the subitems as parameters.
Value := Params.Remove()
if(Params.MaxIndex())
{
Params.Insert(1, Name)
Name := Params.Remove()
return (this[Params*])[Name] := Value
}
DetectHidden := A_DetectHiddenWindows
DetectHiddenWindows, On
Handled := true
if(Name = "SelectedItem")
{
GUI := CGUI.GUIList[this.GUINum]
Gui, % this.GUINum ":Default"
Gui, TreeView, % this.ClassNN
TV_Modify(Value._.ID)
this.ProcessSubControlState(this._.PreviouslySelectedItem, this.SelectedItem)
this._.PreviouslySelectedItem := this.SelectedItem
}
else
Handled := false
if(!DetectHidden)
DetectHiddenWindows, Off
if(Handled)
return Value
}
}
/*
Event: Introduction
There are currently 3 methods to handle control events:
1) Use an event handler. Simply use control.EventName.Handler := "HandlingFunction"
Instead of "HandlingFunction" it is also possible to pass a function reference or a Delegate: control.EventName.Handler := new Delegate(Object, "HandlingFunction")
If this method is used, the first parameter will contain the control object that sent this event.
2) Create a function with this naming scheme in your window class: ControlName_EventName(params)
3) Instead of using ControlName_EventName() you may also call <CControl.RegisterEvent> on a control instance to register a different event function name.
This method is deprecated since event handlers are more flexible.
The parameters depend on the event and there may not be params at all in some cases.
Event: Click(Item)
Invoked when the user clicked on the control.
Event: DoubleClick(Item)
Invoked when the user double-clicked on the control.
Event: RightClick(Item)
Invoked when the user right-clicked on the control.
Event: EditingStart(Item)
Invoked when the user started editing a node.
Event: EditingEnd(Item)
Invoked when the user finished editing a node.
Event: ItemSelected(Item)
Invoked when the user selected a node.
Event: ItemExpanded(Item)
Invoked when the user expanded a node.
Event: ItemCollapsed(Item)
Invoked when the user collapsed a node.
Event: KeyPress(KeyCode)
Invoked when the user pressed a key while the control was focused.
*/
HandleEvent(Event)
{
if(CGUI.GUIList[this.GUINum].IsDestroyed)
return
;Handle visibility of controls associated with tree nodees
if(Event.GUIEvent = "S")
this.ProcessSubControlState(this.PreviouslySelectedItem, SelectedItem := this.Items.ItemByID(Event.EventInfo))
if(Event.GUIEvent == "E")
this.CallEvent("EditingStart", this.Items.ItemByID(Event.EventInfo))
else if(EventName := {DoubleClick : "DoubleClick", e : "EditingEnd", S : "ItemSelected", Normal : "Click", RightClick : "RightClick", "+" : "ItemExpanded", "-" : "ItemCollapsed"}[Event.GUIEvent])
this.CallEvent(EventName, this.Items.ItemByID(Event.EventInfo))
else if(EventName = "K")
this.CallEvent("KeyPress", Event.EventInfo)
else if(Event.GUIEvent == "F")
this.CallEvent("FocusReceived")
else if(Event.GUIEvent == "f")
this.CallEvent("FocusLost")
if(Event.GUIEvent = "S")
this.PreviouslySelectedItem := SelectedItem
}
/*
Class: CTreeViewControl.CItem
A tree node.
*/
Class CItem
{
__New(ID, GUINum, hwnd)
{
this.Insert("_", {})
this._.Insert("GUINum", GUINum)
this._.Insert("hwnd", hwnd)
this._.Insert("ID", ID)
this._.Insert("Controls", {})
}
/*
Function: Add
Adds a new item to the TreeView.
Parameters:
Text - The text of the item.
Options - Various options, see Autohotkey TreeView documentation
Returns:
An object of type CItem representing the newly added item.
*/
Add(Text, Options = "")
{
GUI := CGUI.GUIList[this._.GUINum]
if(GUI.IsDestroyed)
return
Control := GUI.Controls[this._.hwnd]
Gui, % this._.GUINum ":Default"
Gui, TreeView, % Control.ClassNN
ID := TV_Add(Text, this.ID, Options)
Item := new CTreeViewControl.CItem(ID, this._.GUINum, this._.hwnd)
;~ Item.Icon := ""
this.Insert(Item)
return Item
}
/*
Function: AddControl
Adds a control to this tree node that will be visible only when this node is selected. The parameters correspond to the Add() function of CGUI.
Parameters:
Type - The type of the control.
Name - The name of the control.
Options - Options used for creating the control.
Text - The text of the control.
UseEnabledState - If true, the control will be enabled/disabled instead of visible/hidden.
*/
AddControl(type, Name, Options, Text, UseEnabledState = 0)
{
GUI := CGUI.GUIList[this._.GUINum]
if(!this.Selected)
Options .= UseEnabledState ? " Disabled" : " Hidden"
Control := GUI.AddControl(type, Name, Options, Text, this._.Controls, this)
Control._.UseEnabledState := UseEnabledState
Control.hParentControl := this._.hwnd
return Control
}
/*
Function: Delete
Deletes an item.
Parameters:
ObjectOrIndex - The item object or the index of the child item of this.
*/
Delete(ObjectOrIndex)
{
GUI := CGUI.GUIList[this._.GUINum]
if(GUI.IsDestroyed)
return
Control := GUI.Controls[this._.hwnd]
Gui, % this._.GUINum ":Default"
Gui, TreeView, % Control.ClassNN
if(!IsObject(ObjectOrIndex)) ;If index, get object and then handle
ObjectOrIndex := this[ObjectOrIndex]
if(ObjectOrIndex.ID = 0) ;Don't delete root node
return
if(ObjectOrIndex.Selected)
WasSelected := true
p := ObjectOrIndex.Parent
for Index, Item in ObjectOrIndex.Parent
if(Item = ObjectOrIndex)
{
ObjRemove(ObjectOrIndex.Parent, A_Index)
break
}
TV_Delete(ObjectOrIndex.ID)
if(WasSelected)
{
Control.ProcessSubControlState(ObjectOrIndex, this.SelectedItem)
Control.PreviouslySelectedItem := ObjectOrIndex ;The node is accessible here even though it does not exist anymore because the user might have stored data in it that might need to be used in _ItemSelected handler.
}
if(TV_GetCount() = 0) ;If all TreeView items are deleted, fire a selection changed event
if(IsFunc(GUI[Control.Name "_ItemSelected"]))
{
ErrorLevel := ErrLevel
GUI[Control.Name "_ItemSelected"](Control.Items)
if(!Critical)
Critical, Off
return
}
}
/*
Function: Move
Moves an Item to another position.
Parameters:
Position - The new (one-based) - position in the child items of Parent.
Parent - The item will be inserted as child of the Parent item. Leave empty to use its current parent.
*/
Move(Position=1, Parent = "")
{
GUI := CGUI.GUIList[this._.GUINum]
if(GUI.IsDestroyed)
return
Control := GUI.Controls[this._.hwnd]
Gui, % this._.GUINum ":Default"
Gui, TreeView, % Control.ClassNN
;Backup properties which are stored in the TreeList itself
Text := this.Text
Bold := this.bold
Expanded := this.Expanded
Checked := this.Checked
Selected := this.Selected
OldID := this.ID
;If no parent is specified, the item will be moved on the current level
if(!Parent)
Parent := this.Parent
OldParent := this.Parent
;Add new node. At this point there are two nodes.
NewID := TV_Add(Text, Parent.ID, (Position = 1 ? "First" : Parent[Position-1].ID) " " (Bold ? "+Bold" : "") (Expanded ? "Expand" : "") (Checked ? "Check" : "") (Selected ? "Select" : ""))
;Collect all child items
Childs := []
for index, Item in this
Childs.Insert(Item)
this._.ID := NewID
;Remove old parent node link and set the new one
if(OldParent != Parent)
{
for Index, Item in OldParent
if(Item = this)
{
OldParent.Remove(A_Index)
break
}
Parent.Insert(Position, this)
}
if(this.Icon)
Control._.ImageListManager.SetIcon(this._.ID, this.Icon, this.IconNumber)
;Move child items
for index, Item in Childs
Item.Move(index, this)
;Delete old tree node
TV_Delete(OldID)
}
/*
Function: SetIcon
Sets the icon of a tree node
Parameters:
Filename - The filename of the file containing the icon.
IconNumberOrTransparencyColor - The icon number or the transparency color if the used file has no transparency support.
*/
SetIcon(Filename, IconNumberOrTransparencyColor = 1)
{
GUI := CGUI.GUIList[this._.GUINum]
if(GUI.IsDestroyed)
return
Control := GUI.Controls[this._.hwnd]
Control._.ImageListManager.SetIcon(this._.ID, Filename, IconNumberOrTransparencyColor)
this._.Icon := Filename
this._.IconNumber := IconNumberOrTransparencyColor
}
/*
Function: MaxIndex
Returns the number of child nodes.
*/
MaxIndex()
{
GUI := CGUI.GUIList[this._.GUINum]
if(GUI.IsDestroyed)
return
Control := GUI.Controls[this._.hwnd]
Gui, % this._.GUINum ":Default"
Gui, TreeView, % Control.ClassNN
current := this._.ID ? TV_GetChild(this._.ID) : TV_GetNext() ;Get first child or first top node
if(!current)
return 0 ;No children
count := 0
while(current && current := TV_GetNext(current))
count++
return count + 1
}
/*
Function: ItemByID
Access a child item by its ID.
Parameters:
ID - The ID of the child item
*/
;Access a child item by its ID
ItemByID(ID)
{
Loop % this.MaxIndex()
{
if(this[A_Index]._.ID = ID)
return this[A_Index]
else if(Item := this[A_Index].ItemByID(ID))
return Item
}
}
/*
Function: FindItemWithText
Finds a child item by its Text.
Parameters:
Text - The text of the child item
*/
FindItemWithText(Text)
{
GUI := CGUI.GUIList[this._.GUINum]
Control := GUI.Controls[this._.hwnd]
return Control.FindItemWithText(Text, this)
}
_NewEnum()
{
return new CEnumerator(this)
}
/*
Property: 1,2,3,4,...
The child nodes of a tree node may be accessed by their index, e.g. this.TreeView1.Items[1][2][3].Text := "AHK"
Property: CheckedItems
An array containing all checked child nodes of type <CTreeViewControl.CItem>.
Property: CheckedIndices
An array containing all checked child indices.
Property: Parent
The parent node of this node.
Property: ID
The ID used internally in the TreeView control.
Property: Icon
The path of an icon assigned to this node.
Property: IconNumber
The icon number used when an icon file contains more than one icon.
Property: Count
The number of child nodes.
Property: HasChildren
True if there is at least one child node.
Property: Text
The text of this tree node.
Property: Checked
True if the tree node is checked.
Property: Selected
True if the tree node is selected.
Property: Expanded
True if the tree node is expanded.
Property: Bold
If true, the text of this node is bold.
*/
__Get(Name, Params*)
{
if(Name != "_")
{
GUI := CGUI.GUIList[this._.GUINum]
if(!GUI.IsDestroyed)
{
;~ if Name is Integer ;get a child node
;~ {
;~ if(Name <= this.MaxIndex())
;~ {
;~ Control := GUI.Controls[this._.hwnd]
;~ Gui, % this._.GUINum ":Default"
;~ Gui, TreeView, % Control.ClassNN
;~ child := TV_GetChild(this._.ID) ;Find child node id
;~ Loop % Name - 1
;~ child := TV_GetNext(child)
;~ Value := new CTreeViewControl.CItem(child, this._.GUINum, this._.hwnd)
;~ }
;~ }
if(Name = "CheckedItems")
{
Value := []
for index, Item in this
if(Item.Checked)
Value.Insert(Item)
}
else if(Name = "CheckedIndices")
{
Value := []
for index, Item in this
if(Item.Checked)
Value.Insert(index)
}
else if(Name = "Parent")
{
Control := GUI.Controls[this._.hwnd]
Gui, % this._.GUINum ":Default"
Gui, TreeView, % Control.ClassNN
VaLue := Control.FindItem(TV_GetParent(this._.ID))
}
else if(Name = "ID" || Name = "Icon" || Name = "IconNumber")
Value := this._[Name]
else if(Name = "Count")
Value := this.MaxIndex()
else if(Name = "HasChildren")
{
Control := GUI.Controls[this._.hwnd]
Gui, % this._.GUINum ":Default"
Gui, TreeView, % Control.ClassNN
Value := TV_GetChild(this._.ID) > 0
}
else if(Name = "Text")
{
Control := GUI.Controls[this._.hwnd]
Gui, % this._.GUINum ":Default"
Gui, TreeView, % Control.ClassNN
TV_GetText(Value, this._.ID)
}
else if(Name = "Checked" || Name = "Expanded" || Name = "Bold")
{
Control := GUI.Controls[this._.hwnd]
Gui, % this._.GUINum ":Default"
Gui, TreeView, % Control.ClassNN
Value := TV_Get(this._.ID, Name) > 0
}
else if(Name = "Selected")
{
Control := GUI.Controls[this._.hwnd]
Gui, % this._.GUINum ":Default"
Gui, TreeView, % Control.ClassNN
Value := TV_GetSelection() = this._.ID
}
else if(Name = "Controls")
Value := this._.Controls
Loop % Params.MaxIndex()
if(IsObject(Value)) ;Fix unlucky multi parameter __GET
Value := Value[Params[A_Index]]
if(Value)
return Value
}
}
}
__Set(Name, Params*)
{
;Fix completely weird __Set behavior. If one tries to assign a value to a sub item, it doesn't call __Get for each sub item but __Set with the subitems as parameters.
Value := Params.Remove()
if(Params.MaxIndex())
{
Params.Insert(1, Name)
Name := Params.Remove()
return (this[Params*])[Name] := Value
}
GUI := CGUI.GUIList[this._.GUINum]
if(!GUI.IsDestroyed)
{
if(Name = "Text")
{
Control := GUI.Controls[this._.hwnd]
Gui, % this._.GUINum ":Default"
Gui, TreeView, % Control.ClassNN
TV_Modify(this._.ID, "", Value)
return Value
}
else if(Name = "Selected") ;Deselecting is not possible it seems
{
if(Value = 1)
{
Control := GUI.Controls[this._.hwnd]
Gui, % this._.GUINum ":Default"
Gui, TreeView, % Control.ClassNN
TV_Modify(this._.ID)
Control.ProcessSubControlState(Control._.PreviouslySelectedItem, Control.SelectedItem)
Control._.PreviouslySelectedItem := Control.SelectedItem
}
return Value
}
else if(Option := {Checked : "Check", Expanded : "Expand", Bold : "Bold"}[Name]) ;Wee, check and remapping in one step
{
Control := GUI.Controls[this._.hwnd]
Gui, % this._.GUINum ":Default"
Gui, TreeView, % Control.ClassNN
TV_Modify(this._.ID, (Value = 1 ? "+" : "-") Option)
}
else if(Name = "Icon")
{
this.SetIcon(Value, this._.HasKey("IconNumber") ? this._.IconNumber : 1)
return Value
}
else if(Name = "IconNumber")
{
this._.IconNumber := Value
if(this._.Icon)
this.SetIcon(this.Icon, Value)
return Value
}
}
}
}
}