-
Notifications
You must be signed in to change notification settings - Fork 886
/
numberedit.go
980 lines (783 loc) · 21.3 KB
/
numberedit.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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
// Copyright 2011 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 (
"bytes"
"fmt"
"math"
"strconv"
"strings"
"syscall"
"unsafe"
"github.com/lxn/win"
)
const numberEditWindowClass = `\o/ Walk_NumberEdit_Class \o/`
func init() {
AppendToWalkInit(func() {
MustRegisterWindowClass(numberEditWindowClass)
})
}
// NumberEdit is a widget that is suited to edit numeric values.
type NumberEdit struct {
WidgetBase
edit *numberLineEdit
hWndUpDown win.HWND
maxValueChangedPublisher EventPublisher
minValueChangedPublisher EventPublisher
prefixChangedPublisher EventPublisher
suffixChangedPublisher EventPublisher
}
// NewNumberEdit returns a new NumberEdit widget as child of parent.
func NewNumberEdit(parent Container) (*NumberEdit, error) {
ne := new(NumberEdit)
if err := InitWidget(
ne,
parent,
numberEditWindowClass,
win.WS_VISIBLE,
win.WS_EX_CONTROLPARENT); err != nil {
return nil, err
}
var succeeded bool
defer func() {
if !succeeded {
ne.Dispose()
}
}()
var err error
if ne.edit, err = newNumberLineEdit(ne); err != nil {
return nil, err
}
ne.edit.applyFont(ne.Font())
ne.SetRange(-math.MaxFloat64, math.MaxFloat64)
if err = ne.SetValue(0); err != nil {
return nil, err
}
ne.GraphicsEffects().Add(InteractionEffect)
ne.GraphicsEffects().Add(FocusEffect)
ne.MustRegisterProperty("MaxValue", NewProperty(
func() interface{} {
return ne.MaxValue()
},
func(v interface{}) error {
return ne.SetRange(ne.MinValue(), assertFloat64Or(v, 0.0))
},
ne.minValueChangedPublisher.Event()))
ne.MustRegisterProperty("MinValue", NewProperty(
func() interface{} {
return ne.MinValue()
},
func(v interface{}) error {
return ne.SetRange(assertFloat64Or(v, 0.0), ne.MaxValue())
},
ne.maxValueChangedPublisher.Event()))
ne.MustRegisterProperty("Prefix", NewProperty(
func() interface{} {
return ne.Prefix()
},
func(v interface{}) error {
return ne.SetPrefix(assertStringOr(v, ""))
},
ne.prefixChangedPublisher.Event()))
ne.MustRegisterProperty("ReadOnly", NewProperty(
func() interface{} {
return ne.ReadOnly()
},
func(v interface{}) error {
return ne.SetReadOnly(v.(bool))
},
ne.edit.readOnlyChangedPublisher.Event()))
ne.MustRegisterProperty("Suffix", NewProperty(
func() interface{} {
return ne.Suffix()
},
func(v interface{}) error {
return ne.SetSuffix(assertStringOr(v, ""))
},
ne.suffixChangedPublisher.Event()))
ne.MustRegisterProperty("Value", NewProperty(
func() interface{} {
return ne.Value()
},
func(v interface{}) error {
return ne.SetValue(assertFloat64Or(v, 0.0))
},
ne.edit.valueChangedPublisher.Event()))
succeeded = true
return ne, nil
}
func (ne *NumberEdit) applyEnabled(enabled bool) {
ne.WidgetBase.applyEnabled(enabled)
if ne.edit == nil {
return
}
ne.edit.applyEnabled(enabled)
}
func (ne *NumberEdit) applyFont(font *Font) {
ne.WidgetBase.applyFont(font)
if ne.edit == nil {
return
}
ne.edit.applyFont(font)
}
// Decimals returns the number of decimal places in the NumberEdit.
func (ne *NumberEdit) Decimals() int {
return ne.edit.decimals
}
// SetDecimals sets the number of decimal places in the NumberEdit.
func (ne *NumberEdit) SetDecimals(decimals int) error {
if decimals < 0 || decimals > 8 {
return newError("decimals must >= 0 && <= 8")
}
ne.edit.decimals = decimals
return ne.SetValue(ne.edit.value)
}
// Prefix returns the text that appears in the NumberEdit before the number.
func (ne *NumberEdit) Prefix() string {
return syscall.UTF16ToString(ne.edit.prefix)
}
// SetPrefix sets the text that appears in the NumberEdit before the number.
func (ne *NumberEdit) SetPrefix(prefix string) error {
if prefix == ne.Prefix() {
return nil
}
p, err := syscall.UTF16FromString(prefix)
if err != nil {
return err
}
old := ne.edit.prefix
ne.edit.prefix = p[:len(p)-1]
if err := ne.edit.setTextFromValue(ne.edit.value); err != nil {
ne.edit.prefix = old
return err
}
ne.prefixChangedPublisher.Publish()
return nil
}
// PrefixChanged returns the event that is published when the prefix changed.
func (ne *NumberEdit) PrefixChanged() *Event {
return ne.prefixChangedPublisher.Event()
}
// Suffix returns the text that appears in the NumberEdit after the number.
func (ne *NumberEdit) Suffix() string {
return syscall.UTF16ToString(ne.edit.suffix)
}
// SetSuffix sets the text that appears in the NumberEdit after the number.
func (ne *NumberEdit) SetSuffix(suffix string) error {
if suffix == ne.Suffix() {
return nil
}
s, err := syscall.UTF16FromString(suffix)
if err != nil {
return err
}
old := ne.edit.suffix
ne.edit.suffix = s[:len(s)-1]
if err := ne.edit.setTextFromValue(ne.edit.value); err != nil {
ne.edit.suffix = old
return err
}
ne.suffixChangedPublisher.Publish()
return nil
}
// SuffixChanged returns the event that is published when the suffix changed.
func (ne *NumberEdit) SuffixChanged() *Event {
return ne.suffixChangedPublisher.Event()
}
// Increment returns the amount by which the NumberEdit increments or decrements
// its value, when the user presses the KeyDown or KeyUp keys, or when the mouse
// wheel is rotated.
func (ne *NumberEdit) Increment() float64 {
return ne.edit.increment
}
// SetIncrement sets the amount by which the NumberEdit increments or decrements
// its value, when the user presses the KeyDown or KeyUp keys, or when the mouse
// wheel is rotated.
func (ne *NumberEdit) SetIncrement(increment float64) error {
ne.edit.increment = increment
return nil
}
// MinValue returns the minimum value the NumberEdit will accept.
func (ne *NumberEdit) MinValue() float64 {
return ne.edit.minValue
}
// MinValue returns the maximum value the NumberEdit will accept.
func (ne *NumberEdit) MaxValue() float64 {
return ne.edit.maxValue
}
// SetRange sets the minimum and maximum values the NumberEdit will accept.
//
// If the current value is out of this range, it will be adjusted.
func (ne *NumberEdit) SetRange(min, max float64) error {
if min > max {
return newError(fmt.Sprintf("invalid range - min: %f, max: %f", min, max))
}
minChanged := min != ne.edit.minValue
maxChanged := max != ne.edit.maxValue
ne.edit.minValue = min
ne.edit.maxValue = max
if min != max {
if ne.edit.value < min {
if err := ne.edit.setValue(min, true); err != nil {
return err
}
} else if ne.edit.value > max {
if err := ne.edit.setValue(max, true); err != nil {
return err
}
}
}
if minChanged {
ne.minValueChangedPublisher.Publish()
}
if maxChanged {
ne.maxValueChangedPublisher.Publish()
}
return nil
}
// Value returns the value of the NumberEdit.
func (ne *NumberEdit) Value() float64 {
return ne.edit.value
}
// SetValue sets the value of the NumberEdit.
func (ne *NumberEdit) SetValue(value float64) error {
if ne.edit.minValue != ne.edit.maxValue &&
(value < ne.edit.minValue || value > ne.edit.maxValue) {
return newError("value out of range")
}
return ne.edit.setValue(value, true)
}
// ValueChanged returns an Event that can be used to track changes to Value.
func (ne *NumberEdit) ValueChanged() *Event {
return ne.edit.valueChangedPublisher.Event()
}
// SetFocus sets the keyboard input focus to the NumberEdit.
func (ne *NumberEdit) SetFocus() error {
if win.SetFocus(ne.edit.hWnd) == 0 {
return lastError("SetFocus")
}
return nil
}
// TextSelection returns the range of the current text selection of the
// NumberEdit.
func (ne *NumberEdit) TextSelection() (start, end int) {
return ne.edit.TextSelection()
}
// SetTextSelection sets the range of the current text selection of the
// NumberEdit.
func (ne *NumberEdit) SetTextSelection(start, end int) {
ne.edit.SetTextSelection(start, end)
}
// ReadOnly returns whether the NumberEdit is in read-only mode.
func (ne *NumberEdit) ReadOnly() bool {
return ne.edit.ReadOnly()
}
// SetReadOnly sets whether the NumberEdit is in read-only mode.
func (ne *NumberEdit) SetReadOnly(readOnly bool) error {
if readOnly != ne.ReadOnly() {
ne.invalidateBorderInParent()
}
return ne.edit.SetReadOnly(readOnly)
}
// SpinButtonsVisible returns whether the NumberEdit appears with spin buttons.
func (ne *NumberEdit) SpinButtonsVisible() bool {
return ne.hWndUpDown != 0
}
// SetSpinButtonsVisible sets whether the NumberEdit appears with spin buttons.
func (ne *NumberEdit) SetSpinButtonsVisible(visible bool) error {
if visible == ne.SpinButtonsVisible() {
return nil
}
if visible {
ne.hWndUpDown = win.CreateWindowEx(
0,
syscall.StringToUTF16Ptr("msctls_updown32"),
nil,
win.WS_CHILD|win.WS_VISIBLE|win.UDS_ALIGNRIGHT|win.UDS_ARROWKEYS|win.UDS_HOTTRACK,
0,
0,
16,
20,
ne.hWnd,
0,
0,
nil)
if ne.hWndUpDown == 0 {
return lastError("CreateWindowEx")
}
win.SendMessage(ne.hWndUpDown, win.UDM_SETBUDDY, uintptr(ne.edit.hWnd), 0)
} else {
if !win.DestroyWindow(ne.hWndUpDown) {
return lastError("DestroyWindow")
}
ne.hWndUpDown = 0
}
return nil
}
// Background returns the background Brush of the NumberEdit.
//
// By default this is nil.
func (ne *NumberEdit) Background() Brush {
return ne.edit.Background()
}
// SetBackground sets the background Brush of the NumberEdit.
func (ne *NumberEdit) SetBackground(bg Brush) {
ne.edit.SetBackground(bg)
}
// TextColor returns the Color used to draw the text of the NumberEdit.
func (ne *NumberEdit) TextColor() Color {
return ne.edit.TextColor()
}
// TextColor sets the Color used to draw the text of the NumberEdit.
func (ne *NumberEdit) SetTextColor(c Color) {
ne.edit.SetTextColor(c)
}
func (*NumberEdit) NeedsWmSize() bool {
return true
}
// WndProc is the window procedure of the NumberEdit.
//
// When implementing your own WndProc to add or modify behavior, call the
// WndProc of the embedded NumberEdit for messages you don't handle yourself.
func (ne *NumberEdit) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case win.WM_NOTIFY:
switch ((*win.NMHDR)(unsafe.Pointer(lParam))).Code {
case win.UDN_DELTAPOS:
nmud := (*win.NMUPDOWN)(unsafe.Pointer(lParam))
ne.edit.incrementValue(-float64(nmud.IDelta) * ne.edit.increment)
}
case win.WM_CTLCOLOREDIT, win.WM_CTLCOLORSTATIC:
if hBrush := ne.handleWMCTLCOLOR(wParam, lParam); hBrush != 0 {
return hBrush
}
case win.WM_WINDOWPOSCHANGED:
wp := (*win.WINDOWPOS)(unsafe.Pointer(lParam))
if wp.Flags&win.SWP_NOSIZE != 0 {
break
}
if ne.edit == nil {
break
}
cb := ne.ClientBoundsPixels()
if err := ne.edit.SetBoundsPixels(cb); err != nil {
break
}
if ne.hWndUpDown != 0 {
win.SendMessage(ne.hWndUpDown, win.UDM_SETBUDDY, uintptr(ne.edit.hWnd), 0)
}
}
return ne.WidgetBase.WndProc(hwnd, msg, wParam, lParam)
}
func (ne *NumberEdit) CreateLayoutItem(ctx *LayoutContext) LayoutItem {
return &numberEditLayoutItem{
idealSize: ne.dialogBaseUnitsToPixels(Size{50, 12}),
minSize: ne.dialogBaseUnitsToPixels(Size{20, 12}),
}
}
type numberEditLayoutItem struct {
LayoutItemBase
idealSize Size // in native pixels
minSize Size // in native pixels
}
func (*numberEditLayoutItem) LayoutFlags() LayoutFlags {
return ShrinkableHorz | GrowableHorz
}
func (li *numberEditLayoutItem) IdealSize() Size {
return li.idealSize
}
func (li *numberEditLayoutItem) MinSize() Size {
return li.minSize
}
type numberLineEdit struct {
*LineEdit
buf *bytes.Buffer
prefix []uint16
suffix []uint16
value float64
minValue float64
maxValue float64
increment float64
decimals int
valueChangedPublisher EventPublisher
inEditMode bool
}
func newNumberLineEdit(parent Widget) (*numberLineEdit, error) {
nle := &numberLineEdit{
buf: new(bytes.Buffer),
increment: 1,
}
var err error
if nle.LineEdit, err = newLineEdit(parent); err != nil {
return nil, err
}
succeeded := false
defer func() {
if !succeeded {
nle.Dispose()
}
}()
if err := nle.LineEdit.setAndClearStyleBits(win.ES_RIGHT, win.ES_LEFT|win.ES_CENTER); err != nil {
return nil, err
}
if err := InitWrapperWindow(nle); err != nil {
return nil, err
}
succeeded = true
return nle, nil
}
func (nle *numberLineEdit) TextColor() Color {
return nle.LineEdit.TextColor()
}
func (nle *numberLineEdit) SetTextColor(c Color) {
nle.LineEdit.SetTextColor(c)
}
func (nle *numberLineEdit) setValue(value float64, setText bool) error {
if setText {
if err := nle.setTextFromValue(value); err != nil {
return err
}
}
if value == nle.value {
return nil
}
nle.value = value
nle.valueChangedPublisher.Publish()
return nil
}
func (nle *numberLineEdit) setTextFromValue(value float64) error {
nle.buf.Reset()
nle.buf.WriteString(syscall.UTF16ToString(nle.prefix))
if nle.decimals > 0 {
nle.buf.WriteString(FormatFloatGrouped(value, nle.decimals))
} else {
nle.buf.WriteString(FormatFloat(value, nle.decimals))
}
nle.buf.WriteString(syscall.UTF16ToString(nle.suffix))
return nle.SetText(nle.buf.String())
}
func (nle *numberLineEdit) endEdit() error {
if err := nle.setTextFromValue(nle.value); err != nil {
return err
}
nle.inEditMode = false
return nil
}
func (nle *numberLineEdit) processChar(text []uint16, start, end int, key Key, char uint16) {
hadSelection := start != end
if !nle.inEditMode {
var groupSepsBeforeStart int
if nle.decimals > 0 {
groupSepsBeforeStart = uint16CountUint16(text[:start], groupSepUint16)
}
if hadSelection {
text = append(text[:start], text[end:]...)
}
if nle.decimals > 0 {
text = uint16RemoveUint16(text, groupSepUint16)
start -= groupSepsBeforeStart
}
nle.inEditMode = true
} else {
if hadSelection {
text = append(text[:start], text[end:]...)
}
}
end = start
switch key {
case KeyBack:
if !hadSelection && start > 0 {
start -= 1
text = append(text[:start], text[start+1:]...)
}
case KeyDelete:
if !hadSelection && start < len(text) {
text = append(text[:start], text[start+1:]...)
}
default:
t := make([]uint16, len(text[:start]), len(text)+1)
copy(t, text[:start])
t = append(t, char)
text = append(t, text[start:]...)
start += 1
}
nle.buf.Reset()
str := syscall.UTF16ToString(text)
nle.buf.WriteString(syscall.UTF16ToString(nle.prefix))
nle.buf.WriteString(str)
nle.buf.WriteString(syscall.UTF16ToString(nle.suffix))
nle.SetText(nle.buf.String())
start += len(nle.prefix)
nle.SetTextSelection(start, start)
nle.tryUpdateValue(false)
}
func (nle *numberLineEdit) tryUpdateValue(setText bool) bool {
t := nle.textUTF16()
t = t[len(nle.prefix) : len(t)-len(nle.suffix)]
text := strings.Replace(syscall.UTF16ToString(t), decimalSepS, ".", 1)
switch text {
case "", ".":
text = "0"
}
if value, err := strconv.ParseFloat(text, 64); err == nil {
if nle.minValue == nle.maxValue || value >= nle.minValue && value <= nle.maxValue {
return nle.setValue(value, setText) == nil
}
}
return false
}
func (nle *numberLineEdit) selectNumber() {
nle.SetTextSelection(len(nle.prefix), len(nle.textUTF16())-len(nle.suffix))
}
func (nle *numberLineEdit) textUTF16() []uint16 {
textLength := nle.SendMessage(win.WM_GETTEXTLENGTH, 0, 0)
buf := make([]uint16, textLength+1)
nle.SendMessage(win.WM_GETTEXT, uintptr(textLength+1), uintptr(unsafe.Pointer(&buf[0])))
return buf[:len(buf)-1]
}
func (nle *numberLineEdit) incrementValue(delta float64) {
value := nle.value + delta
if nle.minValue != nle.maxValue {
if value < nle.minValue {
value = nle.minValue
} else if value > nle.maxValue {
value = nle.maxValue
}
}
nle.setValue(value, true)
nle.selectNumber()
}
func (nle *numberLineEdit) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case win.WM_CHAR:
if nle.ReadOnly() {
break
}
if AltDown() {
return 0
}
if ControlDown() {
if wParam == 1 {
// Ctrl+A
return 0
}
break
}
char := uint16(wParam)
text := nle.textUTF16()
text = text[len(nle.prefix) : len(text)-len(nle.suffix)]
start, end := nle.TextSelection()
start -= len(nle.prefix)
end -= len(nle.prefix)
if Key(wParam) == KeyBack {
nle.processChar(text, start, end, KeyBack, 0)
return 0
}
switch char {
case uint16('0'), uint16('1'), uint16('2'), uint16('3'), uint16('4'), uint16('5'), uint16('6'), uint16('7'), uint16('8'), uint16('9'):
if start == end && nle.decimals > 0 {
if i := uint16IndexUint16(text, decimalSepUint16); i > -1 && i < len(text)-nle.decimals && start > i {
return 0
}
}
nle.processChar(text, start, end, 0, char)
return 0
case uint16('-'):
if nle.minValue != nle.maxValue && nle.minValue >= 0 {
return 0
}
if start > 0 || uint16ContainsUint16(text, uint16('-')) && end == 0 {
return 0
}
nle.processChar(text, start, end, 0, char)
return 0
case decimalSepUint16:
if nle.decimals == 0 {
return 0
}
if start == 0 && end == 0 && len(text) > 0 && text[0] == '-' {
return 0
}
if end < len(text)-nle.decimals {
return 0
}
if i := uint16IndexUint16(text, decimalSepUint16); i > -1 && i <= start || i > end {
return 0
}
nle.processChar(text, start, end, 0, char)
return 0
default:
return 0
}
case win.WM_KEYDOWN:
switch Key(wParam) {
case KeyA:
if ControlDown() {
nle.selectNumber()
return 0
}
case KeyDelete:
if nle.ReadOnly() {
break
}
text := nle.textUTF16()
text = text[len(nle.prefix) : len(text)-len(nle.suffix)]
start, end := nle.TextSelection()
start -= len(nle.prefix)
end -= len(nle.prefix)
nle.processChar(text, start, end, KeyDelete, 0)
return 0
case KeyDown:
if nle.ReadOnly() || nle.increment <= 0 {
return 0
}
nle.incrementValue(-nle.increment)
return 0
case KeyEnd:
start, end := nle.TextSelection()
end = len(nle.textUTF16()) - len(nle.suffix)
if !ShiftDown() {
start = end
}
nle.SetTextSelection(start, end)
return 0
case KeyHome:
start, end := nle.TextSelection()
start = len(nle.prefix)
if !ShiftDown() {
end = start
}
nle.SetTextSelection(start, end)
return 0
case KeyLeft:
var pos win.POINT
win.GetCaretPos(&pos)
lParam := uintptr(win.MAKELONG(uint16(pos.X), uint16(pos.Y)))
i := int(win.LOWORD(uint32(nle.SendMessage(win.EM_CHARFROMPOS, 0, lParam))))
if min := len(nle.prefix); i <= min {
if !ShiftDown() {
nle.SetTextSelection(min, min)
}
return 0
}
case KeyReturn:
if nle.ReadOnly() {
break
}
if nle.inEditMode {
nle.endEdit()
nle.selectNumber()
return 0
}
case KeyRight:
var pos win.POINT
win.GetCaretPos(&pos)
lParam := uintptr(win.MAKELONG(uint16(pos.X), uint16(pos.Y)))
i := int(win.LOWORD(uint32(nle.SendMessage(win.EM_CHARFROMPOS, 0, lParam))))
if max := len(nle.textUTF16()) - len(nle.suffix); i >= max {
if !ShiftDown() {
nle.SetTextSelection(max, max)
}
return 0
}
case KeyUp:
if nle.ReadOnly() || nle.increment <= 0 {
return 0
}
nle.incrementValue(nle.increment)
return 0
}
case win.WM_GETDLGCODE:
if !nle.inEditMode {
if form := ancestor(nle); form != nil {
if dlg, ok := form.(dialogish); ok {
if dlg.DefaultButton() != nil {
// If the NumberEdit 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_KILLFOCUS:
nle.onFocusChanged()
nle.endEdit()
case win.WM_LBUTTONDOWN:
i := int(win.LOWORD(uint32(nle.SendMessage(win.EM_CHARFROMPOS, 0, lParam))))
if min := len(nle.prefix); i < min {
nle.SetFocus()
nle.SetTextSelection(min, min)
return 0
}
if max := len(nle.textUTF16()) - len(nle.suffix); i > max {
nle.SetFocus()
nle.SetTextSelection(max, max)
return 0
}
case win.WM_LBUTTONDBLCLK:
nle.selectNumber()
return 0
case win.WM_MOUSEMOVE:
i := int(win.LOWORD(uint32(nle.SendMessage(win.EM_CHARFROMPOS, 0, lParam))))
if min := len(nle.prefix); i < min {
return 0
}
if max := len(nle.textUTF16()) - len(nle.suffix); i > max {
return 0
}
case win.WM_MOUSEWHEEL:
if nle.ReadOnly() || nle.increment <= 0 {
break
}
delta := float64(int16(win.HIWORD(uint32(wParam))))
nle.incrementValue(delta / 120 * nle.increment)
return 0
case win.WM_PASTE:
if nle.ReadOnly() {
break
}
ret := nle.LineEdit.WndProc(hwnd, msg, wParam, lParam)
if !nle.tryUpdateValue(true) {
nle.setTextFromValue(nle.value)
}
nle.selectNumber()
return ret
case win.WM_SETFOCUS:
nle.onFocusChanged()
nle.selectNumber()
case win.EM_SETSEL:
start := int(wParam)
end := int(lParam)
adjusted := false
if min := len(nle.prefix); start < min {
start = min
adjusted = true
}
if max := len(nle.textUTF16()) - len(nle.suffix); end < 0 || end > max {
end = max
adjusted = true
}
if adjusted {
nle.SetTextSelection(start, end)
return 0
}
}
return nle.LineEdit.WndProc(hwnd, msg, wParam, lParam)
}
func (nle *numberLineEdit) onFocusChanged() {
if ne := windowFromHandle(win.GetParent(nle.hWnd)); ne != nil {
if wnd := windowFromHandle(win.GetParent(ne.Handle())); wnd != nil {
if _, ok := wnd.(Container); ok {
ne.(Widget).AsWidgetBase().invalidateBorderInParent()
}
}
}
}
func (ne *NumberEdit) SetToolTipText(s string) error {
return ne.edit.SetToolTipText(s)
}