-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathTVX.ahk
439 lines (372 loc) · 9.33 KB
/
TVX.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
;Title: TreeViewX
; TreeViewX extends standard TreeView control to support moving, deleting & inserting.
TVX( pTree, pSub, pOptions="", pUserData="" ) {
;----------------------------------------------------------------------------------------
; Function: TVX
; Initialisation function. Mandatory to call before you show the TreeView.
;
; Parameters:
; pTree - AHK name of the TreeView control
; pSub - Subroutine for TreeViewX, the same rules as in g.
; pOptions - String containing space delimited options for setting up TreeViewX
; pUserData - Base name of the array holding user data.
; This array is indexed using tree view item handles.
;
; Options:
; HasRoot - TreeViewX has root item - the one containing all other items.
; Root item can't be moved, edited or delited, and items can not
; be moved or created outside of it. This option need to be set
; after root is already added to the menu, as TreeViewX need to
; know the root menu handle.
;
; CollapseOnMove - When moving item out of of its container, this option makes container collapse
; EditOnInsert - Automaticaly enters edit mode upon insertion of new item
;
; Example:
;>
;> TVX("MyTree", "Handler", "HasRoot CollapseOnMove")
;>
global
if InStr(pOptions, "HasRoot") {
TVX_HasRoot := 1
TVX_root := TV_GetNext()
}
if InStr(pOptions, "CollapseOnMove")
TVX_CollapseOnMove := 1
if InStr(pOptions, "EditOnInsert")
TVX_EditOnInsert := 1
TVX_userData := pUserData
TVX_sub := pSub
GuiControl, +AltSubmit -ReadOnly +gTVX_OnEvent, %pTree%
}
TVX_Walk(root, label, ByRef event_type, ByRef event_param){
;----------------------------------------------------------------------------------------
; Function: Walk
; Walk the menu and rise events
;
; Parameters:
; root - menu to iterate, can be simple item also
; label - event handler
; event_type - event argument 1 - Event type
; event_param - event argument 2 - Item upon which event is rised
;
;
; Type Param
;
; + - Iteration start, root handle
; M - Menu item, menu handle
; I - Item, item handle
; E - End of menu menu handle (pseudo item)
; - - Iteration end root handle (pseudo item)
;
local n, t, p, c, pref, bSetEnd, lastParent, rootsParent, tmp
; start event for menus
event_type := "+"
event_param := root
GoSub %label%
if !TV_GetChild(root)
return
; this will be exit condition. If we come to roots parent, stop walking.
rootsParent := TV_GetParent(root)
lastParent := root
c := root
loop {
c := TV_GetNext(c, "Full")
TV_GetText(tmp, c)
; Check if this item is submenu. If so, set the lastParent
if ( TV_GetChild(c) ){
lastParent := c
event_type := "M"
}
else event_type := "I" ; not a submenu, it is normal item
event_param := c
GoSub %label%
; Check if c is the last item in the current submenu
; Do so by taking the next item and checking its parent.
; If the parent is different then "lastParent" current item is
; at the end of the its submenu.
n := TV_GetNext(c, "FULL")
if (n)
{
p := TV_GetParent(n)
if ( p != lastParent){
t := lastParent
lastParent := p
}
else continue
; It is the last child
Loop { ; rise "E" (end of menu) event
event_type := "E"
event_param := t
GoSub %label%
t := TV_GetParent(t)
if (t = rootsParent) { ; rise "-" (end of walk) event
event_type := "-"
event_param := t
GoSub %label%
return
}
if (p = t)
break
}
} else
Loop {
;this is the end of the complite menu, so close all open submenus, if any
if (lastParent = root) {
event_type := "-"
event_param := root
GoSub %label%
return
}
event_type := "E"
event_param := lastParent
GoSub %label%
lastParent := TV_GetParent(lastParent)
}
}
}
TVX_Move(item, direction){
;----------------------------------------------------------------------------------------------
; Function: Move
; Moves tree view item up or down
;
; Parameters:
; item - Handle of the item to move
; direction - "u" or "d" (Up & Down)
;
; Returns:
; Handle of the item
;
; Remarks:
; Item to be moved is copied to the new place then source item is deleted. This
; creates new handle for the moved item. New handle will be returned by the function.
;
local newc, newp, t, p, n
p := TV_GetPrev(item)
n := TV_GetNext(item)
if (TVX_HasRoot)
{
if TV_GetNext()=item
return
if (direction="u")
{
if (p = 0 && TV_GetParent(item)=TV_GetNext()) ;don't let item go above root
{
TV_Modify(item)
TVX_sel:=item
return
}
}
}
; Do so by coping an item bellow calculated item and deleting the old one.
; Return handle of new item
; newc - calculated child after which "item" should be created.
; newp - ... and its parent
; if moving down
if (direction = "d")
{
; handle end of submenu
if !n
{
newc := TV_GetParent(item)
; check the end of the entire list
if (TVX_HasRoot && newc = TVX_root)
return
if TVX_CollapseOnMove
TV_Modify(newc, "-Expand")
newp := TV_GetParent(newc)
}
; somewhere in the middle
else
{
; if submenu, go into it
t := TV_Get(n, "E")
if (t = n)
{
newp := n
newc := "First"
}
; not a submenu
else
{
newc := n
newp := TV_GetParent(n)
}
}
}
; if moving up
if (direction = "u")
{
;going up - handle start of the submenu
if !p
{
t := TV_GetParent(item)
if TVX_CollapseOnMove
TV_Modify(t, "-Expand")
newc := TV_GetPrev(t)
; handle start of the menu again
if !newc
{
newp := TV_GetParent(t)
newc := "First"
}
else
newp := TV_GetParent(newc)
}
; somewhere in the middle
else
{
; if submenu is expanded, go into it
t := TV_Get(p, "E")
if (t = p)
{
newc := "First"
newp := t
}
else
{
t := TV_GetPrev(p)
;check the top of the list
if !t
{
newc := "First"
newp := TV_GetParent(p)
}
else
{
newc := t
newp := TV_GetParent(newc)
}
}
}
}
newc := TVX_CopyItem(newc, newp, item)
TV_Delete(item)
return newc
}
TVX_CopyProc(iType, item) {
;---------------------------------------------------------------------------------------------
; TVX_Walk event handler wrapped in the function
;
; Function that copies menu item to the destination item.
; Handle of destination item is specified in the global variable TVX_copyDest.
;
local c, txt
static lastParent
TV_GetText(txt, item)
if iType in +
{
lastParent := TVX_copyDest
TV_Modify(TVX_copyDest, "", txt)
if TVX_userData
{
%TVX_userData%%TVX_copyDest% := %TVX_userData%%item%
%TVX_userData%%item% := ""
}
}
if iType in I,M
{
c := TV_Add(txt, lastParent)
if iType = M
lastParent := c
if TVX_userData
{
%TVX_userData%%c% := %TVX_userData%%item%
%TVX_userData%%item% := ""
}
}
if iType = E
lastParent := TV_GetParent(lastParent)
}
TVX_CopyItem(destc, destp, source){
;-----------------------------------------------------------------------------------------------
; Create new item after the child "destc" with parent "destp" and copy the "source" item into it
;
global
;create the holder and call the copy function
TVX_copyDest := TV_Add("", destp , destc )
TVX_Walk(source, "_TVX_CopyProc", TVX_itemType, TVX_param)
return TVX_copyDest
}
TVX_OnItemSelect(pItemId){
;----------------------------------------------------------------------------------------------
; Used to control moving
;
global
if (TVX_bSelfSelect) {
TVX_bSelfSelect := false
return true
}
TVX_prevSel := TVX_sel
TVX_sel := pItemId
if GetKeyState("Shift") && (TVX_lastKey=38 || TVX_lastKey=40)
if (pItemId != TVX_root)
{
TVX_sel := TVX_Move( TVX_prevSel, TVX_lastKey=40 ? "d" : "u")
TVX_prevSel := pItemId
TVX_bSelfSelect := true
TV_Modify(TVX_sel, "Select Bold")
return true
}
return false
}
TVX_OnKeyPress(pKey){
local tp, sel
TVX_lastKey := pKey
if (TVX_bSelfPress) {
TVX_bSelfPress := false
return true
}
;delete
if pKey = 46
{
; use GetSelection instead Editor_sel since if key is pressed and hold
; TVX_OnSelect handler may not be called before delete to set the TVX_sel
sel := TV_GetSelection()
if (TVX_HasRoot && sel = TVX_root)
return false
; is shift delete is pressed return - some problems with this combination
if (GetKeyState("Shift"))
return false
TV_Delete(sel)
return true
}
;insert
if pKey = 45
{
tp := TV_GetParent(TVX_sel)
if (TVX_sel = TVX_root)
tp := TVX_root
tp := TV_Add("__ new item __", tp, "Bold " . TVX_sel)
if GetKeyState("Shift")
{
TV_Add("__ new item __", tp, "Bold First ")
TV_Modify(tp,"Expand", "__ new group __")
}
if (TVX_EditOnInsert)
{
TVX_bSelfPress := TVX_bSelfSelect := true
TV_Modify(tp, "Select")
Send, {F2}
}
return true
}
return false
}
;----------------------------------------------
_TVX_CopyProc:
TVX_CopyProc(TVX_itemType, TVX_param)
return
;----------------------------------------------------------------------------------------------
; g soubroutine for Tree View
;
TVX_OnEvent:
if (A_GuiEvent="S")
if TVX_OnItemSelect(A_EventInfo)
return
if (A_GuiEvent="K")
if TVX_OnKeyPress(A_EventInfo)
return
;if not the Xtended property send event to the caller
gosub %TVX_sub%
return