forked from lxn/walk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
treeview.go
462 lines (359 loc) · 10.2 KB
/
treeview.go
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
// Copyright 2010 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package walk
import (
"syscall"
"unsafe"
)
import (
"github.com/lxn/win"
)
type treeViewItemInfo struct {
handle win.HTREEITEM
child2Handle map[TreeItem]win.HTREEITEM
}
type TreeView struct {
WidgetBase
model TreeModel
lazyPopulation bool
itemsResetEventHandlerHandle int
itemChangedEventHandlerHandle int
item2Info map[TreeItem]*treeViewItemInfo
handle2Item map[win.HTREEITEM]TreeItem
currItem TreeItem
hIml win.HIMAGELIST
usingSysIml bool
imageUintptr2Index map[uintptr]int32
filePath2IconIndex map[string]int32
expandedChangedPublisher TreeItemEventPublisher
currentItemChangedPublisher EventPublisher
}
func NewTreeView(parent Container) (*TreeView, error) {
tv := new(TreeView)
if err := InitWidget(
tv,
parent,
"SysTreeView32",
win.WS_TABSTOP|win.WS_VISIBLE|win.TVS_HASBUTTONS|win.TVS_LINESATROOT|win.TVS_SHOWSELALWAYS|win.TVS_TRACKSELECT,
win.WS_EX_CLIENTEDGE); err != nil {
return nil, err
}
succeeded := false
defer func() {
if !succeeded {
tv.Dispose()
}
}()
if err := tv.setTheme("Explorer"); err != nil {
return nil, err
}
succeeded = true
return tv, nil
}
func (*TreeView) LayoutFlags() LayoutFlags {
return ShrinkableHorz | ShrinkableVert | GrowableHorz | GrowableVert | GreedyHorz | GreedyVert
}
func (tv *TreeView) SizeHint() Size {
return tv.dialogBaseUnitsToPixels(Size{100, 100})
}
func (tv *TreeView) Dispose() {
tv.WidgetBase.Dispose()
tv.disposeImageListAndCaches()
}
func (tv *TreeView) Model() TreeModel {
return tv.model
}
func (tv *TreeView) SetModel(model TreeModel) error {
if tv.model != nil {
tv.model.ItemsReset().Detach(tv.itemsResetEventHandlerHandle)
tv.model.ItemChanged().Detach(tv.itemChangedEventHandlerHandle)
tv.disposeImageListAndCaches()
}
tv.model = model
if model != nil {
tv.lazyPopulation = model.LazyPopulation()
tv.itemsResetEventHandlerHandle = model.ItemsReset().Attach(func(parent TreeItem) {
if parent == nil {
tv.resetItems()
} else if tv.item2Info[parent] != nil {
tv.SetSuspended(true)
defer tv.SetSuspended(false)
if err := tv.removeDescendants(parent); err != nil {
return
}
if err := tv.insertChildren(parent); err != nil {
return
}
}
})
tv.itemChangedEventHandlerHandle = model.ItemChanged().Attach(func(item TreeItem) {
if item == nil || tv.item2Info[item] == nil {
return
}
if err := tv.updateItem(item); err != nil {
return
}
})
}
return tv.resetItems()
}
func (tv *TreeView) CurrentItem() TreeItem {
return tv.currItem
}
func (tv *TreeView) SetCurrentItem(item TreeItem) error {
if item == tv.currItem {
return nil
}
var handle win.HTREEITEM
if item != nil {
if info := tv.item2Info[item]; info == nil {
return newError("invalid item")
} else {
handle = info.handle
}
}
if 0 == tv.SendMessage(win.TVM_SELECTITEM, win.TVGN_CARET, uintptr(handle)) {
return newError("SendMessage(TVM_SELECTITEM) failed")
}
tv.currItem = item
return nil
}
func (tv *TreeView) ItemAt(x, y int) TreeItem {
hti := win.TVHITTESTINFO{Pt: win.POINT{int32(x), int32(y)}}
tv.SendMessage(win.TVM_HITTEST, 0, uintptr(unsafe.Pointer(&hti)))
if item, ok := tv.handle2Item[hti.HItem]; ok {
return item
}
return nil
}
func (tv *TreeView) resetItems() error {
tv.SetSuspended(true)
defer tv.SetSuspended(false)
if err := tv.clearItems(); err != nil {
return err
}
if tv.model == nil {
return nil
}
if err := tv.insertRoots(); err != nil {
return err
}
return nil
}
func (tv *TreeView) clearItems() error {
if 0 == tv.SendMessage(win.TVM_DELETEITEM, 0, 0) {
return newError("SendMessage(TVM_DELETEITEM) failed")
}
tv.item2Info = make(map[TreeItem]*treeViewItemInfo)
tv.handle2Item = make(map[win.HTREEITEM]TreeItem)
return nil
}
func (tv *TreeView) insertRoots() error {
count := tv.model.RootCount()
for i := 0; i < count; i++ {
if _, err := tv.insertItem(i, tv.model.RootAt(i)); err != nil {
return err
}
}
return nil
}
func (tv *TreeView) applyImageListForImage(image interface{}) {
tv.hIml, tv.usingSysIml, _ = imageListForImage(image)
tv.SendMessage(win.TVM_SETIMAGELIST, 0, uintptr(tv.hIml))
tv.imageUintptr2Index = make(map[uintptr]int32)
tv.filePath2IconIndex = make(map[string]int32)
}
func (tv *TreeView) disposeImageListAndCaches() {
if tv.hIml != 0 && !tv.usingSysIml {
win.ImageList_Destroy(tv.hIml)
}
tv.hIml = 0
tv.imageUintptr2Index = nil
tv.filePath2IconIndex = nil
}
func (tv *TreeView) setTVITEMImageInfo(tvi *win.TVITEM, item TreeItem) {
if imager, ok := item.(Imager); ok {
if tv.hIml == 0 {
tv.applyImageListForImage(imager.Image())
}
// FIXME: If not setting TVIF_SELECTEDIMAGE and tvi.ISelectedImage,
// some default icon will show up, even though we have not asked for it.
tvi.Mask |= win.TVIF_IMAGE | win.TVIF_SELECTEDIMAGE
tvi.IImage = imageIndexMaybeAdd(
imager.Image(),
tv.hIml,
tv.usingSysIml,
tv.imageUintptr2Index,
tv.filePath2IconIndex)
tvi.ISelectedImage = tvi.IImage
}
}
func (tv *TreeView) insertItem(index int, item TreeItem) (win.HTREEITEM, error) {
var tvins win.TVINSERTSTRUCT
tvi := &tvins.Item
tvi.Mask = win.TVIF_CHILDREN | win.TVIF_TEXT
tvi.PszText = win.LPSTR_TEXTCALLBACK
tvi.CChildren = win.I_CHILDRENCALLBACK
tv.setTVITEMImageInfo(tvi, item)
parent := item.Parent()
if parent == nil {
tvins.HParent = win.TVI_ROOT
} else {
info := tv.item2Info[parent]
if info == nil {
return 0, newError("invalid parent")
}
tvins.HParent = info.handle
}
if index == 0 {
tvins.HInsertAfter = win.TVI_LAST
} else {
var prevItem TreeItem
if parent == nil {
prevItem = tv.model.RootAt(index - 1)
} else {
prevItem = parent.ChildAt(index - 1)
}
info := tv.item2Info[prevItem]
if info == nil {
return 0, newError("invalid prev item")
}
tvins.HInsertAfter = info.handle
}
hItem := win.HTREEITEM(tv.SendMessage(win.TVM_INSERTITEM, 0, uintptr(unsafe.Pointer(&tvins))))
if hItem == 0 {
return 0, newError("TVM_INSERTITEM failed")
}
tv.item2Info[item] = &treeViewItemInfo{hItem, make(map[TreeItem]win.HTREEITEM)}
tv.handle2Item[hItem] = item
if !tv.lazyPopulation {
if err := tv.insertChildren(item); err != nil {
return 0, err
}
}
return hItem, nil
}
func (tv *TreeView) insertChildren(parent TreeItem) error {
info := tv.item2Info[parent]
count := parent.ChildCount()
for i := 0; i < count; i++ {
child := parent.ChildAt(i)
if handle, err := tv.insertItem(i, child); err != nil {
return err
} else {
info.child2Handle[child] = handle
}
}
return nil
}
func (tv *TreeView) updateItem(item TreeItem) error {
tvi := &win.TVITEM{
Mask: win.TVIF_TEXT,
HItem: tv.item2Info[item].handle,
PszText: win.LPSTR_TEXTCALLBACK,
}
tv.setTVITEMImageInfo(tvi, item)
if 0 == tv.SendMessage(win.TVM_SETITEM, 0, uintptr(unsafe.Pointer(tvi))) {
return newError("SendMessage(TVM_SETITEM) failed")
}
return nil
}
func (tv *TreeView) removeItem(item TreeItem) error {
if err := tv.removeDescendants(item); err != nil {
return err
}
info := tv.item2Info[item]
if info == nil {
return newError("invalid item")
}
if 0 == tv.SendMessage(win.TVM_DELETEITEM, 0, uintptr(info.handle)) {
return newError("SendMessage(TVM_DELETEITEM) failed")
}
if parentInfo := tv.item2Info[item.Parent()]; parentInfo != nil {
delete(parentInfo.child2Handle, item)
}
delete(tv.item2Info, item)
delete(tv.handle2Item, info.handle)
return nil
}
func (tv *TreeView) removeDescendants(parent TreeItem) error {
for item, _ := range tv.item2Info[parent].child2Handle {
if err := tv.removeItem(item); err != nil {
return err
}
}
return nil
}
func (tv *TreeView) Expanded(item TreeItem) bool {
tvi := &win.TVITEM{
HItem: tv.item2Info[item].handle,
Mask: win.TVIF_STATE,
StateMask: win.TVIS_EXPANDED,
}
if 0 == tv.SendMessage(win.TVM_GETITEM, 0, uintptr(unsafe.Pointer(tvi))) {
newError("SendMessage(TVM_GETITEM) failed")
}
return tvi.State&win.TVIS_EXPANDED != 0
}
func (tv *TreeView) SetExpanded(item TreeItem, expanded bool) error {
var action uintptr
if expanded {
action = win.TVE_EXPAND
} else {
action = win.TVE_COLLAPSE
}
if 0 == tv.SendMessage(win.TVM_EXPAND, action, uintptr(tv.item2Info[item].handle)) {
return newError("SendMessage(TVM_EXPAND) failed")
}
return nil
}
func (tv *TreeView) ExpandedChanged() *TreeItemEvent {
return tv.expandedChangedPublisher.Event()
}
func (tv *TreeView) CurrentItemChanged() *Event {
return tv.currentItemChangedPublisher.Event()
}
func (tv *TreeView) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case win.WM_NOTIFY:
nmhdr := (*win.NMHDR)(unsafe.Pointer(lParam))
switch nmhdr.Code {
case win.TVN_GETDISPINFO:
nmtvdi := (*win.NMTVDISPINFO)(unsafe.Pointer(lParam))
item := tv.handle2Item[nmtvdi.Item.HItem]
if nmtvdi.Item.Mask&win.TVIF_TEXT != 0 {
nmtvdi.Item.PszText = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(item.Text())))
}
if nmtvdi.Item.Mask&win.TVIF_CHILDREN != 0 {
nmtvdi.Item.CChildren = int32(item.ChildCount())
}
case win.TVN_ITEMEXPANDING:
nmtv := (*win.NMTREEVIEW)(unsafe.Pointer(lParam))
item := tv.handle2Item[nmtv.ItemNew.HItem]
if nmtv.Action == win.TVE_EXPAND && tv.lazyPopulation {
info := tv.item2Info[item]
if len(info.child2Handle) == 0 {
tv.insertChildren(item)
}
}
case win.TVN_ITEMEXPANDED:
nmtv := (*win.NMTREEVIEW)(unsafe.Pointer(lParam))
item := tv.handle2Item[nmtv.ItemNew.HItem]
switch nmtv.Action {
case win.TVE_COLLAPSE:
tv.expandedChangedPublisher.Publish(item)
case win.TVE_COLLAPSERESET:
case win.TVE_EXPAND:
tv.expandedChangedPublisher.Publish(item)
case win.TVE_EXPANDPARTIAL:
case win.TVE_TOGGLE:
}
case win.TVN_SELCHANGED:
nmtv := (*win.NMTREEVIEW)(unsafe.Pointer(lParam))
tv.currItem = tv.handle2Item[nmtv.ItemNew.HItem]
tv.currentItemChangedPublisher.Publish()
}
}
return tv.WidgetBase.WndProc(hwnd, msg, wParam, lParam)
}