-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathTVDAD.ahk
211 lines (179 loc) · 6.67 KB
/
TVDAD.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
OnMessage( 0x4E, "Treeview_BeginDrag" ) ; WM_NOTIFY
OnMessage( 0x200, "Treeview_Dragging" ) ; WM_MOUSEMOVE
OnMessage( 0x202, "Treeview_EndDrag" ) ; WM_LBUTTONUP
/*
; Add the Treeview
Gui, Add, TreeView, x0 y0 w200 h300 HWNDhTreeview
P1 := TV_Add("Apple", 0, "Expand" )
P1C1 := TV_Add("Apple juice", P1, "Expand" )
P2 := TV_Add("Orange", 0, "Expand" )
P2C1 := TV_Add("Orange juice", P2, "Expand" )
P2C2 := TV_Add("Orange soda", P2, "Expand" )
P2C2C1 := TV_Add("Tang", P2C2, "Expand" )
otherfruits = pear,kiwi,lime,grape,peach
Loop, parse, otherfruits, `,
TV_ADD( A_LoopField, 0 )
; Show the GUI
Gui, Show, w200 h300
return
; ----- End Auto-Execute section -----
guiclose:
exitapp
/*
Treeview_BeginDrag( wParam, lParam )
{
global TV_is_dragging, hTreeview, hDragitem
static TVM_SELECTITEM := 0x110B, TVGN_CARET := 0x9
nmhdr_code := NumGet( lParam + 0, 8, "uint" )
; TVN_BEGINDRAGA = 0xFFFFFE69, TVN_BEGINDRAGW = 0xFFFFFE38
If ( nmhdr_code = 0xFFFFFE69 ) or ( nmhdr_code = 0xFFFFFE38 )
{
; Get the drag item out of the NMTREEVIEW structure
hDragitem := NumGet( lParam + 0, 60, "uint" )
; Select the item before dragging so it's clear what you're dragging
SendMessage( hTreeview, TVM_SELECTITEM, TVGN_CARET, hDragitem )
; If you were so inclined, you could initialize drag image stuff here:
; 1. get a drag image (either by using TVM_SETIMAGE, which requires
; that the Treeview control have been created with an associated
; imagelist) OR just use TVM_GETITEMRECT and fancy gdi-style stuff
; and make one yourself
; 2. add the image to an imagelist and use the imagelist drag functions
; (read up on it at msdn)
; 3. add the appropriate imagelist dragging code in the Treeview_Dragging()
; and Treeview_EndDrag() functions
;
; I initially had some code here that was mostly working but (a) it was
; creating some strange visual artifacts and (b) I kind of prefer just using
; insertion marks and highlighting the drop target.
; Set the dragging flag
TV_is_dragging := 1
}
}
Treeview_Dragging( wParam, lParam )
{
global TV_is_dragging, hTreeview, height
static TVM_HITTEST := 0x1111, TVM_SETINSERTMARK := 0x111A, TVM_GETITEMRECT := 0x1104
static TVM_SELECTITEM := 0x110B, TVGN_DROPHILITE := 0x8, TVM_GETITEMHEIGHT := 0x111C
If TV_is_dragging
{
if not height
height := SendMessage( hTreeview, TVM_GETITEMHEIGHT, 0, 0 )
; Get the mouse location out of lParam
x := lParam & 0xFFFF
y := lParam >> 16
; Create the TVHITTESTINFO struct...
VarSetCapacity( tvht, 16, 0 )
NumPut( x, tvht, 0, "int" ), NumPut( y, tvht, 4, "int" )
; ... to determine whether the pointer is over an item. If it is...
If hitTarget := SendMessage( hTreeview, TVM_HITTEST, 0, &tvht )
{
; ... highlight the item as a drop target, and / or ...
SendMessage( hTreeview, TVM_SELECTITEM, TVGN_DROPHILITE, hitTarget )
; ... if the pointer is in the top or bottom quarter of the item,
; show an insertion mark before or after, respectively.
; This way you can decide whether to make the dragged item
; a child or sibling of the drop target item.
;
; If you really wanted, you could check here to see what kind of
; item hitTarget was and display the insertion mark accordingly.
VarSetCapacity( rcitem, 16, 0 ), NumPut( hitTarget, rcitem )
SendMessage( hTreeview, TVM_GETITEMRECT, 1, &rcitem )
rcitem_top := NumGet( rcitem, 4, "int" )
rcitem_bottom := NumGet( rcitem, 12, "int" )
fAfter := 99 ; just a default that's not 0 or 1
fAfter := ( y - rcitem_top ) < ( height/4 ) ? 0 : ( rcitem_bottom - y) < ( height/4 ) ? 1 : fAfter
If ( fAfter = 99 )
SendMessage( hTreeview, TVM_SETINSERTMARK, 0, 0 ) ; hide insertionmark
Else
SendMessage( hTreeview, TVM_SETINSERTMARK, fAfter, hitTarget ) ; show insertion mark
}
}
}
Treeview_EndDrag( wParam, lParam )
{
global TV_is_dragging, hTreeview, hDragitem
static TVM_SETINSERTMARK := 0x111A, TVM_SELECTITEM := 0x110B, TVGN_DROPHILITE := 0x8
static TVM_HITTEST := 0x1111
If TV_is_dragging
{
; Remove the drop-target highlighting and insertion mark
SendMessage( hTreeview, TVM_SELECTITEM, TVGN_DROPHILITE, 0 )
SendMessage( hTreeview, TVM_SETINSERTMARK, 1, 0 )
; Add code here to handle the moving of the dragged node
; - hDragitem is the handle to the item currently being dragged
; - you can use the code from the WM_MOUSEMOVE to determine
; where the pointer is and where/how the item should be inserted
;
; For the sake of simplicity, this script will always move the
; dragitem to be a child of the drop target
; Get the mouse location out of lParam
x := lParam & 0xFFFF
y := lParam >> 16
; Create the TVHITTESTINFO struct...
VarSetCapacity( tvht, 16, 0 )
NumPut( x, tvht, 0, "int" ), NumPut( y, tvht, 4, "int" )
; ... to determine whether the pointer is over an item.
If hDroptarget := SendMessage( hTreeview, TVM_HITTEST, 0, &tvht )
{
; Only do stuff if the droptarget is different from the drag item
If ( hDragitem != hDroptarget )
{
; To prevent infinite loops, first make sure "parent" isn't actually a
; descendant of node (it's like going back in time and becoming your own
; great-grandfather: no good can come of it)
If not IsParentADescendant( hDragitem, hDroptarget )
{
AddNodeToParent( hDragitem, hDroptarget )
TV_Modify( hDropTarget, "Expand" )
TV_Delete( hDragitem )
}
}
}
; Set the dragging flag and dragitem handle to false
TV_is_dragging := 0, hDragitem := 0
}
}
IsParentADescendant( node, parent )
{
dlist := GetDescendantsList( node )
Loop, parse, dlist, `,
If ( A_LoopField = parent )
return 1
return 0
}
; Wheeeee! Recursion is fun!
GetDescendantsList( node )
{
If ( kid := TV_GetChild( node ) )
{
kids .= kid . "," . GetDescendantsList( kid )
While ( kid := TV_GetNext( kid ) )
kids .= kid . "," . GetDescendantsList( kid )
}
return kids
}
; I recurse! You recurse! We all recurse for Ira Curs! (or something )
AddNodeToParent( node, parent )
{
TV_GetText( t, node)
node_id := TV_Add( t, parent, "Expand" )
If ( kid := TV_GetChild( node ) )
{
AddNodeToParent( kid, node_id )
While ( kid := TV_GetNext( kid ) )
AddNodeToParent( kid, node_id )
}
return node_id
}
SendMessage( hWnd, Msg, wParam, lParam )
{
static SendMessageA
If not SendMessageA
SendMessageA := LoadDllFunction( "user32.dll", "SendMessageA" )
return DllCall( SendMessageA, uint, hWnd, uint, Msg, uint , wParam, uint, lParam )
}
LoadDllFunction( file, function ) {
if !hModule := DllCall( "GetModuleHandle", uint, &file, uint )
hModule := DllCall( "LoadLibrary", uint, &file, uint )
return DllCall("GetProcAddress", uint, hModule, uint, &function, uint)
}