Skip to content

Commit

Permalink
NumberEdit: Add opt-in support for spin buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
lxn committed Sep 3, 2020
1 parent 5ebea42 commit f7621ae
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 11 deletions.
27 changes: 16 additions & 11 deletions declarative/numberedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
53 changes: 53 additions & 0 deletions numberedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func init() {
type NumberEdit struct {
WidgetBase
edit *numberLineEdit
hWndUpDown win.HWND
maxValueChangedPublisher EventPublisher
minValueChangedPublisher EventPublisher
prefixChangedPublisher EventPublisher
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit f7621ae

Please sign in to comment.