summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorAlexander Neumann <alexander.neumann@picos-software.com>2020-09-03 11:19:59 +0200
committerAlexander Neumann <alexander.neumann@picos-software.com>2020-09-03 11:19:59 +0200
commitf7621aeb8225ff560a1b58a5f461735b291f05f8 (patch)
tree35a7d0400436e0a583515df2ce5d2ad16d33189a
parentBitmap: Reduce fake array size for 32 bit (diff)
downloadwireguard-windows-f7621aeb8225ff560a1b58a5f461735b291f05f8.tar.xz
wireguard-windows-f7621aeb8225ff560a1b58a5f461735b291f05f8.zip
NumberEdit: Add opt-in support for spin buttons
-rw-r--r--declarative/numberedit.go27
-rw-r--r--numberedit.go53
2 files changed, 69 insertions, 11 deletions
diff --git a/declarative/numberedit.go b/declarative/numberedit.go
index ab92460a..fe0f569f 100644
--- a/declarative/numberedit.go
+++ b/declarative/numberedit.go
@@ -48,17 +48,18 @@ type NumberEdit struct {
// NumberEdit
- AssignTo **walk.NumberEdit
- Decimals int
- Increment float64
- MaxValue float64
- MinValue float64
- Prefix Property
- OnValueChanged walk.EventHandler
- ReadOnly Property
- Suffix Property
- TextColor walk.Color
- Value Property
+ AssignTo **walk.NumberEdit
+ Decimals int
+ Increment float64
+ MaxValue float64
+ MinValue float64
+ Prefix Property
+ OnValueChanged walk.EventHandler
+ ReadOnly Property
+ SpinButtonsVisible bool
+ Suffix Property
+ TextColor walk.Color
+ Value Property
}
func (ne NumberEdit) Create(builder *Builder) error {
@@ -93,6 +94,10 @@ func (ne NumberEdit) Create(builder *Builder) error {
}
}
+ if err := w.SetSpinButtonsVisible(ne.SpinButtonsVisible); err != nil {
+ return err
+ }
+
if ne.OnValueChanged != nil {
w.ValueChanged().Attach(ne.OnValueChanged)
}
diff --git a/numberedit.go b/numberedit.go
index d755e30f..a5dfb700 100644
--- a/numberedit.go
+++ b/numberedit.go
@@ -30,6 +30,7 @@ func init() {
type NumberEdit struct {
WidgetBase
edit *numberLineEdit
+ hWndUpDown win.HWND
maxValueChangedPublisher EventPublisher
minValueChangedPublisher EventPublisher
prefixChangedPublisher EventPublisher
@@ -352,6 +353,47 @@ func (ne *NumberEdit) SetReadOnly(readOnly bool) error {
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.
@@ -384,6 +426,13 @@ func (*NumberEdit) NeedsWmSize() bool {
// 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
@@ -404,6 +453,10 @@ func (ne *NumberEdit) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr)
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)