-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtv.ahk
428 lines (340 loc) · 11.4 KB
/
tv.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
;tv.ahk
;Tuesday, June 10, 2014
;a class and wrapper library for Autohotkey's treeview control
;http://ahkscript.org/boards/viewtopic.php?f=6&t=3716
;by Brother Gabriel-Marie
;contributors: JoeDF and Just Me from the ahkscript.org forum
;NOTES:
;the class does not need to be initiated
;--------SPECIAL-----------------------------------------------
; selection()
; count()
;-------FETCH ID's-------------------------------------------------
; first()
; next(whatid, ifchecked=false)
; previous(whatid)
; sibling(whatid, ifchecked=false)
; parent(whatid)
; child(whatid)
; lastchild(whatid)
; firstchild(whatid)
;-----RELATIONSHIPS--------------------------------------
; isparent(whatid)
; ischild(whatid)
; isroot(whatid)
; islastchild(whatid)
; isfirstchild(whatid)
;-----ATTRIBUTES------------------------------------------
; ischecked(whatid)
; isexpanded(whatid)
; isbold(whatid)
; hasattribute(whatid, whatattribute="checked")
; haschild(whatid)
; hasparent(whatid)
; allChildrenHaveAttribute(whatid, whatattribute := "checked", recurse := true, checkparents := true)
;------METHODS-----------------------------------------------
; text(whatid, whattext="") ;get or set the text of an item
; parenttext(whatid, whattext="")
; delete(whatid, deleteall=false)
; add(whatname, whatparentid=0, whatoptions="")
; check(whatid, value=1)
; expand(whatid, value=1)
; bold(whatid, value=1)
; checkparent(whatid, value=true)
; boldparent(whatid, value=true)
; expandparent(whatid, value=true)
; toggleTree(modify="check", setparent=true, setsubfamilies=true, setsubparents=true)
; toggleFamily(whatid=0, modify="check", setparent=true, setsubfamilies=true, setsubparents=true)
;--------------------------------------------------------------------------------------------------------------------------------------
class tv
{
;--------SPECIAL-----------------------------------------------
;returns the id of the current selection
selection(){
return tv_getselection()
}
;returns a count of all items in the tree
count(){
return tv_getcount()
}
;-------GET ID's-------------------------------------------------
;get the very first element in the tree
first(){
return tv_getnext()
}
;returns the next item in the tree regardless of structure
;set ifchecked to true to locate the next checked item
next(whatid, ifchecked=false){
if(ifchecked)
return tv_getnext(whatid, "checked |full")
else
return tv_getnext(whatid, "full")
}
;returns the node previous to this one
previous(whatid){
return tv_getprev(whatid)
}
;returns the id of the next sibling; returns 0 if none
;set ifchecked to true to locate the next checked item
sibling(whatid, ifchecked=false){
if(ifchecked)
return tv_getnext(whatid, "checked")
else
return tv_getnext(whatid)
}
;return the parent id of the item
parent(whatid){
return tv_getparent(whatid)
}
;return the id of the first child of an item
;if there are no children or this is a parent item, it will return 0
child(whatid){
return tv_getchild(whatid)
}
;returns the id of the last child of the node
;if this function is called on a node that is root, it will return the last root node
lastchild(whatid){
thisid := whatid
loop{
if(!thisid)
break
nextnode := this.sibling(thisid)
if(!nextnode)
return thisid
thisid := this.lastchild(nextnode)
}
}
;returns the first child of a family (does not recurse subfamilies)
;if the node has no children, it returns 0
firstchild(whatid){
if(!this.isparent(whatid) ){
thisid := this.parent(whatid)
if(thisid)
return this.child(thisid)
}else{
return this.child(whatid)
}
}
;-----RELATIONSHIPS--------------------------------------
;return true if it is a parent node
isparent(whatid){
return tv_getchild(whatid)
}
;return true if it is a child node
ischild(whatid){
return tv_getparent(whatid)
}
isroot(whatid){
if(!this.isparent(whatid) && !this.ischild(whatid) )
return true
}
;returns true if the node is the last child in a family
islastchild(whatid){
if(whatid = this.lastchild(whatid) )
return true
}
;returns true if the node is the first child in a family
isfirstchild(whatid){
if(whatid = this.firstchild(whatid) )
return true
}
;-----ATTRIBUTES------------------------------------------
;return true if the item is checked
ischecked(whatid){
if(tv_get(whatid, "checked") )
return true
}
;return true if the item is expanded
isexpanded(whatid){
if(tv_get(whatid, "expanded") )
return true
}
;return true if the item is bolded
isbold(whatid){
if(tv_get(whatid, "bold") )
return true
}
hasattribute(whatid, whatattribute="checked"){
if(tv_get(whatid, whatattribute) )
return true
}
haschild(whatid){
return tv_getchild(whatid)
}
hasparent(whatid){
return tv_getparent(whatid)
}
;determine whether all members of a node are set to a certain attribute
;whatid --> is the id of a member of the node you want to check
;whatattribute --> should be one of either "checked" "bold" or "expanded"
;recurse --> whether or not to confirm attributes in subtrees
;checkparents --> whether or not to confirm subparents (nodes that have children)
;you can only check for one attribute at a time
;RETURNS:
;if the attribute is found in every member, it will return true
;if the attribute is missing from any member, it will return false
;USAGE:
;tv.allChildrenHaveAttribute(thiselement, "checked", true, true) --> should report true if all nodes are checked
;tv.allChildrenHaveAttribute(thiselement, "checked", true, false) --> should report true if all child elements (that are not parents and have no subtrees of their own) are checked (subparents are skipped)
;tv.allChildrenHaveAttribute(thiselement, "checked", false, true) --> should report true if all children of the parent are checked, not checking items in subtrees (subparents are treated as children only)
;Much thanks to JoeDF and JustMe for their diligence with this function
allChildrenHaveAttribute(whatid, whatattribute := "checked", recurse := true, checkparents := true){
; invalid item ID
if(whatid < 0)
return false
;store the parent of whatid in the 'parents' array for non-recursive checks
parents := [this.parent(whatid)]
;walk through the node, as long as the 'parents' array contains parent nodes ...
while(parents.maxindex() <> ""){
currentid := this.firstchild(parents[1])
;as long as child items are found
while(currentid){
;if it is a parent ...
if(this.isparent(currentid)){
;if checkparents is true, check the attribute.
if(checkparents) && !(this.hasattribute(currentid, whatattribute))
return false
;if recurse is true, add the parent to the 'parents' array.
if(recurse)
parents.insert(currentid)
}else{
if !(this.hasattribute(currentid, whatattribute))
return false
}
;get the next sibling, if any.
currentid := this.sibling(currentid)
}
;remove the current parent node from the 'parents' array
parents.remove(1)
}
;done, all items have been checked
return true
}
;------METHODS-----------------------------------------------
;gets or sets an item's text
;to set the value, specifiy the second parameter - function will return 1 for success and 0 for failure
;to get the text, do not use the second parameter; returns false if there is no text or there is a failure
text(whatid, whattext=""){
if(whattext){
return tv_modify(whatid, whattext)
}else{
tv_gettext(thistext, whatid)
return thistext
}
}
;gets or sets the text of the item's parent.
;if there is no parent, it returns false
parenttext(whatid, whattext=""){
thisid := this.parent(whatid)
return this.text(thisid, whattext)
}
;deletes a particular item in the tree
;specify the second parameter as true to delete the entire tree
;returns 1 upon success
delete(whatid, deleteall=false){
if(deleteall)
return tv_delete(whatid)
else if(whatid)
return tv_delete(whatid)
}
;adds a new item to the tree
;returns the new item's id
;if parentid is not specified, the item will be added at root
add(whatname, whatparentid=0, whatoptions=""){
if(!whatname)
return 0
return tv_add(whatname, whatparentid, whatoptions)
}
;check or uncheck the item
check(whatid, value=true){
if(value)
tv_modify(whatid, "check")
else
tv_modify(whatid, "-check")
}
;check or uncheck the item
expand(whatid, value=true){
if(value)
tv_modify(whatid, "expand")
else
tv_modify(whatid, "-expand")
}
;check or uncheck the item
bold(whatid, value=true){
if(value)
tv_modify(whatid, "bold")
else
tv_modify(whatid, "-bold")
}
checkparent(whatid, value=true){
thisid := this.parent(whatid)
this.check(thisid, value)
}
boldparent(whatid, value=true){
thisid := this.parent(whatid)
this.bold(thisid, value)
}
expandparent(whatid, value=true){
thisid := this.parent(whatid)
this.expand(thisid, value)
}
;This will allow you to operate on the entire tree
;modify is the value to add/subtract to the tree items
;checkparent is whether to check the main item if it is a parent (don't choose this if you attach this to an event launched when the user clicks the checkbox)
;checksubfamilies is whether to check any families within the parent's element
;checksubparents is whether to check any other parents in the family under the main parent
;note that if you set all the settings to false, it will only operate on root nodes without any subtrees
;the last parameter should not be used - it's only for the function to be able to recurse
toggleTree(modify="check", setparent=true, setsubfamilies=true, setsubparents=true, nextid=0){
;if the firstid is recorded already, don't start over
if(!nextid){
thisid := this.first()
}else{
thisid := nextid
}
if(thisid){
;got to add this in order to check a root item that has no children
if(this.isroot(thisid) )
tv_modify(thisid, modify)
this.toggleFamily(thisid, modify, setparent, setsubfamilies, setsubparents)
nextid := this.sibling(thisid)
if(nextid){
this.toggleTree(modify, setparent, setsubfamilies, setsubparents, nextid)
}
}
} ;end toggleTree
;toggleFamily - allows you to recurse a tree and set attributes for all the sub-parents, a family, or just the elements in a family
;whatid is which item to work upon
;modify is the value to add/subtract to the tree items
;checkparent is whether to check the main item if it is a parent (don't choose this if you attach this to an event launched when the user clicks the checkbox)
;checksubfamilies is whether to check any families within the parent's element
;checksubparents is whether to check any other parents in the family under the main parent
;usage:
; tv.toggleFamily(myitemid, "check expand", true, false, true)
toggleFamily(whatid=0, modify="check", setparent=true, setsubfamilies=true, setsubparents=true){
;if whatid is not specified, then operate on the entire tree
if(!whatid){
whatid := this.first()
}
;check this item if it is a parent item, depending on the parameters
if(setparent && this.isparent(whatid)){
tv_modify(whatid, modify)
}
thisid := this.child(whatid)
if(thisid){
loop{
if(!thisid)
break
if(setsubfamilies){
;check this particular item if it isn't a parent item
if(!this.isparent(thisid) )
tv_modify(thisid, modify)
}
;set up for the next item and recurse
thisid := this.sibling(thisid)
if(thisid)
this.toggleFamily(thisid, modify, setsubparents, setsubfamilies, setsubparents)
} ;end loop
} ;end if
} ;end togglefamily
} ;end tv class