-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpushbutton.go
162 lines (124 loc) · 2.98 KB
/
pushbutton.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
// 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 (
"unsafe"
)
import (
"github.com/lxn/win"
)
type PushButton struct {
Button
image interface{}
}
func NewPushButton(parent Container) (*PushButton, error) {
pb := new(PushButton)
if err := InitWidget(
pb,
parent,
"BUTTON",
win.WS_TABSTOP|win.WS_VISIBLE|win.BS_PUSHBUTTON,
0); err != nil {
return nil, err
}
pb.Button.init()
return pb, nil
}
func (*PushButton) LayoutFlags() LayoutFlags {
return GrowableHorz
}
func (pb *PushButton) MinSizeHint() Size {
var s win.SIZE
pb.SendMessage(win.BCM_GETIDEALSIZE, 0, uintptr(unsafe.Pointer(&s)))
return maxSize(Size{int(s.CX), int(s.CY)}, pb.dialogBaseUnitsToPixels(Size{50, 14}))
}
func (pb *PushButton) SizeHint() Size {
return pb.MinSizeHint()
}
func (pb *PushButton) Image() interface{} {
return pb.image
}
func (pb *PushButton) SetImage(image interface{}) error {
var typ uintptr
var handle uintptr
switch img := image.(type) {
case nil:
// zeroes are good
case *Bitmap:
typ = win.IMAGE_BITMAP
handle = uintptr(img.hBmp)
case *Icon:
typ = win.IMAGE_ICON
handle = uintptr(img.hIcon)
default:
return newError("image must be either *walk.Bitmap or *walk.Icon")
}
pb.SendMessage(win.BM_SETIMAGE, typ, handle)
pb.image = image
return pb.updateParentLayout()
}
func (pb *PushButton) ImageAboveText() bool {
return pb.hasStyleBits(win.BS_TOP)
}
func (pb *PushButton) SetImageAboveText(value bool) error {
if err := pb.ensureStyleBits(win.BS_TOP, value); err != nil {
return err
}
// We need to set the image again, or Windows will fail to calculate the
// button control size correctly.
return pb.SetImage(pb.image)
}
func (pb *PushButton) ensureProperDialogDefaultButton(hwndFocus win.HWND) {
widget := windowFromHandle(hwndFocus)
if widget == nil {
return
}
if _, ok := widget.(*PushButton); ok {
return
}
form := ancestor(pb)
if form == nil {
return
}
dlg, ok := form.(dialogish)
if !ok {
return
}
defBtn := dlg.DefaultButton()
if defBtn == nil {
return
}
if err := defBtn.setAndClearStyleBits(win.BS_DEFPUSHBUTTON, win.BS_PUSHBUTTON); err != nil {
return
}
if err := defBtn.Invalidate(); err != nil {
return
}
}
func (pb *PushButton) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case win.WM_GETDLGCODE:
hwndFocus := win.GetFocus()
if hwndFocus == pb.hWnd {
form := ancestor(pb)
if form == nil {
break
}
dlg, ok := form.(dialogish)
if !ok {
break
}
defBtn := dlg.DefaultButton()
if defBtn == pb {
pb.setAndClearStyleBits(win.BS_DEFPUSHBUTTON, win.BS_PUSHBUTTON)
return win.DLGC_BUTTON | win.DLGC_DEFPUSHBUTTON
}
break
}
pb.ensureProperDialogDefaultButton(hwndFocus)
case win.WM_KILLFOCUS:
pb.ensureProperDialogDefaultButton(win.HWND(wParam))
}
return pb.Button.WndProc(hwnd, msg, wParam, lParam)
}