-
Notifications
You must be signed in to change notification settings - Fork 0
/
combobox.go
764 lines (609 loc) · 17.4 KB
/
combobox.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
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
// 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.
// +build windows
package walk
import (
"fmt"
"math/big"
"reflect"
"strconv"
"syscall"
"time"
"unsafe"
"github.com/lxn/win"
)
type ComboBox struct {
WidgetBase
bindingValueProvider BindingValueProvider
model ListModel
providedModel interface{}
bindingMember string
displayMember string
format string
precision int
itemsResetHandlerHandle int
itemChangedHandlerHandle int
itemsInsertedHandlerHandle int
itemsRemovedHandlerHandle int
maxItemTextWidth int // in native pixels
currentValue interface{}
prevCurIndex int
selChangeIndex int
maxLength int
currentIndexChangedPublisher EventPublisher
textChangedPublisher EventPublisher
editingFinishedPublisher EventPublisher
editOrigWndProcPtr uintptr
editing bool
persistent bool
}
var comboBoxEditWndProcPtr uintptr
func init() {
AppendToWalkInit(func() {
comboBoxEditWndProcPtr = syscall.NewCallback(comboBoxEditWndProc)
})
}
func comboBoxEditWndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
cb := (*ComboBox)(unsafe.Pointer(win.GetWindowLongPtr(hwnd, win.GWLP_USERDATA)))
switch msg {
case win.WM_GETDLGCODE:
if !cb.editing {
if form := ancestor(cb); form != nil {
if dlg, ok := form.(dialogish); ok {
if dlg.DefaultButton() != nil {
// If the ComboBox lives in a Dialog that has a
// DefaultButton, we won't swallow the return key.
break
}
}
}
}
if wParam == win.VK_RETURN {
return win.DLGC_WANTALLKEYS
}
case win.WM_KEYDOWN:
if wParam != win.VK_RETURN || 0 == cb.SendMessage(win.CB_GETDROPPEDSTATE, 0, 0) {
cb.handleKeyDown(wParam, lParam)
}
if cb.editing && wParam == win.VK_RETURN {
cb.editing = false
cb.editingFinishedPublisher.Publish()
}
case win.WM_KEYUP:
if wParam != win.VK_RETURN || 0 == cb.SendMessage(win.CB_GETDROPPEDSTATE, 0, 0) {
cb.handleKeyUp(wParam, lParam)
}
case win.WM_SETFOCUS, win.WM_KILLFOCUS:
cb.invalidateBorderInParent()
if cb.editing && msg == win.WM_KILLFOCUS {
cb.editing = false
cb.editingFinishedPublisher.Publish()
}
}
return win.CallWindowProc(cb.editOrigWndProcPtr, hwnd, msg, wParam, lParam)
}
func NewComboBox(parent Container) (*ComboBox, error) {
cb, err := newComboBoxWithStyle(parent, win.CBS_AUTOHSCROLL|win.CBS_DROPDOWN)
if err != nil {
return nil, err
}
editHwnd := win.GetWindow(cb.hWnd, win.GW_CHILD)
win.SetWindowLongPtr(editHwnd, win.GWLP_USERDATA, uintptr(unsafe.Pointer(cb)))
cb.editOrigWndProcPtr = win.SetWindowLongPtr(editHwnd, win.GWLP_WNDPROC, comboBoxEditWndProcPtr)
return cb, nil
}
func NewDropDownBox(parent Container) (*ComboBox, error) {
return newComboBoxWithStyle(parent, win.CBS_DROPDOWNLIST)
}
func newComboBoxWithStyle(parent Container, style uint32) (*ComboBox, error) {
cb := &ComboBox{prevCurIndex: -1, selChangeIndex: -1, precision: 2}
if err := InitWidget(
cb,
parent,
"COMBOBOX",
win.WS_TABSTOP|win.WS_VISIBLE|win.WS_VSCROLL|style,
0); err != nil {
return nil, err
}
succeeded := false
defer func() {
if !succeeded {
cb.Dispose()
}
}()
var event *Event
if style&win.CBS_DROPDOWNLIST == win.CBS_DROPDOWNLIST {
event = cb.CurrentIndexChanged()
} else {
event = cb.TextChanged()
}
cb.GraphicsEffects().Add(InteractionEffect)
cb.GraphicsEffects().Add(FocusEffect)
cb.MustRegisterProperty("CurrentIndex", NewProperty(
func() interface{} {
return cb.CurrentIndex()
},
func(v interface{}) error {
return cb.SetCurrentIndex(assertIntOr(v, -1))
},
cb.CurrentIndexChanged()))
cb.MustRegisterProperty("Text", NewProperty(
func() interface{} {
return cb.Text()
},
func(v interface{}) error {
return cb.SetText(assertStringOr(v, ""))
},
event))
cb.MustRegisterProperty("CurrentItem", NewReadOnlyProperty(
func() interface{} {
if rlm, ok := cb.providedModel.(ReflectListModel); ok {
if i := cb.CurrentIndex(); i > -1 {
return reflect.ValueOf(rlm.Items()).Index(i).Interface()
}
}
return nil
},
cb.CurrentIndexChanged()))
cb.MustRegisterProperty("HasCurrentItem", NewReadOnlyBoolProperty(
func() bool {
return cb.CurrentIndex() != -1
},
cb.CurrentIndexChanged()))
cb.MustRegisterProperty("TextNotEmpty", NewReadOnlyBoolProperty(
func() bool {
return cb.Text() != ""
},
cb.CurrentIndexChanged()))
cb.MustRegisterProperty("Value", NewProperty(
func() interface{} {
if cb.Editable() {
return cb.Text()
}
index := cb.CurrentIndex()
if cb.bindingValueProvider == nil || index == -1 {
return nil
}
return cb.bindingValueProvider.BindingValue(index)
},
func(v interface{}) error {
if cb.Editable() {
return cb.SetText(assertStringOr(v, ""))
}
if cb.bindingValueProvider == nil {
if cb.model == nil {
return nil
} else {
return newError("Data binding is only supported using a model that implements BindingValueProvider.")
}
}
index := -1
count := cb.model.ItemCount()
for i := 0; i < count; i++ {
if cb.bindingValueProvider.BindingValue(i) == v {
index = i
break
}
}
return cb.SetCurrentIndex(index)
},
event))
succeeded = true
return cb, nil
}
func (cb *ComboBox) applyFont(font *Font) {
cb.WidgetBase.applyFont(font)
if cb.model != nil {
cb.maxItemTextWidth = cb.calculateMaxItemTextWidth()
cb.RequestLayout()
}
}
func (cb *ComboBox) Editable() bool {
return !cb.hasStyleBits(win.CBS_DROPDOWNLIST)
}
func (cb *ComboBox) itemString(index int) string {
switch val := cb.model.Value(index).(type) {
case string:
return val
case time.Time:
return val.Format(cb.format)
case *big.Rat:
return val.FloatString(cb.precision)
default:
return fmt.Sprintf(cb.format, val)
}
panic("unreachable")
}
func (cb *ComboBox) insertItemAt(index int) error {
str := cb.itemString(index)
lp := uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(str)))
if win.CB_ERR == cb.SendMessage(win.CB_INSERTSTRING, uintptr(index), lp) {
return newError("SendMessage(CB_INSERTSTRING)")
}
return nil
}
func (cb *ComboBox) removeItem(index int) error {
if win.CB_ERR == cb.SendMessage(win.CB_DELETESTRING, uintptr(index), 0) {
return newError("SendMessage(CB_DELETESTRING")
}
return nil
}
func (cb *ComboBox) resetItems() error {
cb.SetSuspended(true)
defer cb.SetSuspended(false)
cb.selChangeIndex = -1
if win.FALSE == cb.SendMessage(win.CB_RESETCONTENT, 0, 0) {
return newError("SendMessage(CB_RESETCONTENT)")
}
cb.maxItemTextWidth = 0
oldValue := cb.currentValue
if cb.model == nil {
cb.SetCurrentIndex(-1)
return nil
}
count := cb.model.ItemCount()
for i := 0; i < count; i++ {
if err := cb.insertItemAt(i); err != nil {
return err
}
}
if oldValue != nil {
cb.Property("Value").Set(oldValue)
} else {
cb.SetCurrentIndex(-1)
}
cb.RequestLayout()
return nil
}
func (cb *ComboBox) attachModel() {
itemsResetHandler := func() {
cb.resetItems()
}
cb.itemsResetHandlerHandle = cb.model.ItemsReset().Attach(itemsResetHandler)
itemChangedHandler := func(index int) {
if win.CB_ERR == cb.SendMessage(win.CB_DELETESTRING, uintptr(index), 0) {
newError("SendMessage(CB_DELETESTRING)")
}
cb.insertItemAt(index)
cb.SetCurrentIndex(cb.prevCurIndex)
}
cb.itemChangedHandlerHandle = cb.model.ItemChanged().Attach(itemChangedHandler)
cb.itemsInsertedHandlerHandle = cb.model.ItemsInserted().Attach(func(from, to int) {
for i := from; i <= to; i++ {
cb.insertItemAt(i)
}
})
cb.itemsRemovedHandlerHandle = cb.model.ItemsRemoved().Attach(func(from, to int) {
for i := to; i >= from; i-- {
cb.removeItem(i)
}
})
}
func (cb *ComboBox) detachModel() {
cb.model.ItemsReset().Detach(cb.itemsResetHandlerHandle)
cb.model.ItemChanged().Detach(cb.itemChangedHandlerHandle)
cb.model.ItemsInserted().Detach(cb.itemsInsertedHandlerHandle)
cb.model.ItemsRemoved().Detach(cb.itemsRemovedHandlerHandle)
}
// Model returns the model of the ComboBox.
func (cb *ComboBox) Model() interface{} {
return cb.providedModel
}
// SetModel sets the model of the ComboBox.
//
// It is required that mdl either implements walk.ListModel or
// walk.ReflectListModel or be a slice of pointers to struct or a []string.
func (cb *ComboBox) SetModel(mdl interface{}) error {
model, ok := mdl.(ListModel)
if !ok && mdl != nil {
var err error
if model, err = newReflectListModel(mdl); err != nil {
return err
}
if _, ok := mdl.([]string); !ok {
if badms, ok := model.(bindingAndDisplayMemberSetter); ok {
var bindingMember string
if cb.Editable() {
bindingMember = cb.displayMember
} else {
bindingMember = cb.bindingMember
}
badms.setBindingMember(bindingMember)
badms.setDisplayMember(cb.displayMember)
}
}
}
cb.providedModel = mdl
if cb.model != nil {
cb.detachModel()
}
cb.model = model
cb.bindingValueProvider, _ = model.(BindingValueProvider)
if model != nil {
cb.attachModel()
}
if err := cb.resetItems(); err != nil {
return err
}
if !cb.Editable() && model != nil && model.ItemCount() == 1 {
cb.SetCurrentIndex(0)
}
return cb.Invalidate()
}
// BindingMember returns the member from the model of the ComboBox that is bound
// to a field of the data source managed by an associated DataBinder.
//
// This is only applicable to walk.ReflectListModel models and simple slices of
// pointers to struct.
func (cb *ComboBox) BindingMember() string {
return cb.bindingMember
}
// SetBindingMember sets the member from the model of the ComboBox that is bound
// to a field of the data source managed by an associated DataBinder.
//
// This is only applicable to walk.ReflectListModel models and simple slices of
// pointers to struct.
//
// For a model consisting of items of type S, data source field of type T and
// bindingMember "Foo", this can be one of the following:
//
// A field Foo T
// A method func (s S) Foo() T
// A method func (s S) Foo() (T, error)
//
// If bindingMember is not a simple member name like "Foo", but a path to a
// member like "A.B.Foo", members "A" and "B" both must be one of the options
// mentioned above, but with T having type pointer to struct.
func (cb *ComboBox) SetBindingMember(bindingMember string) error {
if bindingMember != "" {
if _, ok := cb.providedModel.([]string); ok {
return newError("invalid for []string model")
}
}
cb.bindingMember = bindingMember
if badms, ok := cb.model.(bindingAndDisplayMemberSetter); ok {
badms.setBindingMember(bindingMember)
}
return nil
}
// DisplayMember returns the member from the model of the ComboBox that is
// displayed in the ComboBox.
//
// This is only applicable to walk.ReflectListModel models and simple slices of
// pointers to struct.
func (cb *ComboBox) DisplayMember() string {
return cb.displayMember
}
// SetDisplayMember sets the member from the model of the ComboBox that is
// displayed in the ComboBox.
//
// This is only applicable to walk.ReflectListModel models and simple slices of
// pointers to struct.
//
// For a model consisting of items of type S, the type of the specified member T
// and displayMember "Foo", this can be one of the following:
//
// A field Foo T
// A method func (s S) Foo() T
// A method func (s S) Foo() (T, error)
//
// If displayMember is not a simple member name like "Foo", but a path to a
// member like "A.B.Foo", members "A" and "B" both must be one of the options
// mentioned above, but with T having type pointer to struct.
func (cb *ComboBox) SetDisplayMember(displayMember string) error {
if displayMember != "" {
if _, ok := cb.providedModel.([]string); ok {
return newError("invalid for []string model")
}
}
cb.displayMember = displayMember
if badms, ok := cb.model.(bindingAndDisplayMemberSetter); ok {
badms.setDisplayMember(displayMember)
}
return nil
}
func (cb *ComboBox) Format() string {
return cb.format
}
func (cb *ComboBox) SetFormat(value string) {
cb.format = value
}
func (cb *ComboBox) Precision() int {
return cb.precision
}
func (cb *ComboBox) SetPrecision(value int) {
cb.precision = value
}
func (cb *ComboBox) MaxLength() int {
return cb.maxLength
}
func (cb *ComboBox) SetMaxLength(value int) {
cb.SendMessage(win.CB_LIMITTEXT, uintptr(value), 0)
cb.maxLength = value
}
// calculateMaxItemTextWidth returns maximum item text width in native pixels.
func (cb *ComboBox) calculateMaxItemTextWidth() int {
hdc := win.GetDC(cb.hWnd)
if hdc == 0 {
newError("GetDC failed")
return -1
}
defer win.ReleaseDC(cb.hWnd, hdc)
hFontOld := win.SelectObject(hdc, win.HGDIOBJ(cb.Font().handleForDPI(cb.DPI())))
defer win.SelectObject(hdc, hFontOld)
var maxWidth int
count := cb.model.ItemCount()
for i := 0; i < count; i++ {
var s win.SIZE
str := syscall.StringToUTF16(cb.itemString(i))
if !win.GetTextExtentPoint32(hdc, &str[0], int32(len(str)-1), &s) {
newError("GetTextExtentPoint32 failed")
return -1
}
maxWidth = maxi(maxWidth, int(s.CX))
}
return maxWidth
}
func (cb *ComboBox) CurrentIndex() int {
return int(int32(cb.SendMessage(win.CB_GETCURSEL, 0, 0)))
}
func (cb *ComboBox) SetCurrentIndex(value int) error {
index := int(int32(cb.SendMessage(win.CB_SETCURSEL, uintptr(value), 0)))
if index != value {
return newError("invalid index")
}
if value != cb.prevCurIndex {
if value == -1 {
cb.currentValue = nil
} else {
cb.currentValue = cb.Property("Value").Get()
}
cb.prevCurIndex = value
cb.currentIndexChangedPublisher.Publish()
}
return nil
}
func (cb *ComboBox) CurrentIndexChanged() *Event {
return cb.currentIndexChangedPublisher.Event()
}
func (cb *ComboBox) Text() string {
return cb.text()
}
func (cb *ComboBox) SetText(value string) error {
var oldText string
oldText, _ = cb.currentValue.(string)
if err := cb.setText(value); err != nil {
return err
}
if value == oldText {
return nil
}
if cb.Editable() {
cb.currentValue = value
}
cb.textChangedPublisher.Publish()
return nil
}
func (cb *ComboBox) TextSelection() (start, end int) {
cb.SendMessage(win.CB_GETEDITSEL, uintptr(unsafe.Pointer(&start)), uintptr(unsafe.Pointer(&end)))
return
}
func (cb *ComboBox) SetTextSelection(start, end int) {
cb.SendMessage(win.CB_SETEDITSEL, 0, uintptr(win.MAKELONG(uint16(start), uint16(end))))
}
func (cb *ComboBox) TextChanged() *Event {
return cb.textChangedPublisher.Event()
}
func (cb *ComboBox) EditingFinished() *Event {
return cb.editingFinishedPublisher.Event()
}
func (cb *ComboBox) Persistent() bool {
return cb.persistent
}
func (cb *ComboBox) SetPersistent(value bool) {
cb.persistent = value
}
func (cb *ComboBox) SaveState() error {
cb.WriteState(strconv.Itoa(cb.CurrentIndex()))
return nil
}
func (cb *ComboBox) RestoreState() error {
state, err := cb.ReadState()
if err != nil {
return err
}
if state == "" {
return nil
}
if i, err := strconv.Atoi(state); err == nil {
cb.SetCurrentIndex(i)
}
return nil
}
func (cb *ComboBox) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case win.WM_COMMAND:
code := win.HIWORD(uint32(wParam))
selIndex := cb.CurrentIndex()
switch code {
case win.CBN_EDITCHANGE:
cb.editing = true
cb.selChangeIndex = -1
cb.textChangedPublisher.Publish()
case win.CBN_SELCHANGE:
cb.selChangeIndex = selIndex
case win.CBN_SELENDCANCEL:
if cb.selChangeIndex != -1 {
if cb.selChangeIndex < cb.model.ItemCount() {
cb.SetCurrentIndex(cb.selChangeIndex)
}
cb.selChangeIndex = -1
}
case win.CBN_SELENDOK:
if editable := cb.Editable(); editable || selIndex != cb.prevCurIndex {
valueProp := cb.Property("Value")
if editable && selIndex > -1 {
valueProp.Set(cb.model.Value(selIndex))
} else {
cb.currentValue = valueProp.Get()
}
cb.currentIndexChangedPublisher.Publish()
cb.prevCurIndex = selIndex
return 0
}
cb.selChangeIndex = -1
}
case win.WM_MOUSEWHEEL:
if !cb.Enabled() {
return 0
}
case win.WM_WINDOWPOSCHANGED:
wp := (*win.WINDOWPOS)(unsafe.Pointer(lParam))
if wp.Flags&win.SWP_NOSIZE != 0 {
break
}
if cb.Editable() {
result := cb.WidgetBase.WndProc(hwnd, msg, wParam, lParam)
cb.SetTextSelection(0, 0)
return result
}
}
return cb.WidgetBase.WndProc(hwnd, msg, wParam, lParam)
}
func (*ComboBox) NeedsWmSize() bool {
return true
}
func (cb *ComboBox) CreateLayoutItem(ctx *LayoutContext) LayoutItem {
var layoutFlags LayoutFlags
if cb.Editable() {
layoutFlags = GrowableHorz | GreedyHorz
} else {
layoutFlags = GrowableHorz
}
defaultSize := cb.dialogBaseUnitsToPixels(Size{30, 12})
if cb.model != nil && cb.maxItemTextWidth <= 0 {
cb.maxItemTextWidth = cb.calculateMaxItemTextWidth()
}
// FIXME: Use GetThemePartSize instead of guessing
w := maxi(defaultSize.Width, cb.maxItemTextWidth+int(win.GetSystemMetricsForDpi(win.SM_CXVSCROLL, uint32(ctx.dpi)))+8)
h := defaultSize.Height + 1
return &comboBoxLayoutItem{
layoutFlags: layoutFlags,
idealSize: Size{w, h},
}
}
type comboBoxLayoutItem struct {
LayoutItemBase
layoutFlags LayoutFlags
idealSize Size // in native pixels
}
func (li *comboBoxLayoutItem) LayoutFlags() LayoutFlags {
return li.layoutFlags
}
func (li *comboBoxLayoutItem) IdealSize() Size {
return li.idealSize
}
func (li *comboBoxLayoutItem) MinSize() Size {
return li.idealSize
}