-
Notifications
You must be signed in to change notification settings - Fork 892
/
pushbutton.go
132 lines (103 loc) · 2.47 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
// 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 (
"github.com/lxn/win"
)
type PushButton struct {
Button
}
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()
pb.GraphicsEffects().Add(InteractionEffect)
pb.GraphicsEffects().Add(FocusEffect)
return pb, nil
}
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)
}
func (pb *PushButton) CreateLayoutItem(ctx *LayoutContext) LayoutItem {
return &pushButtonLayoutItem{
buttonLayoutItem: buttonLayoutItem{
idealSize: pb.idealSize(),
},
}
}
type pushButtonLayoutItem struct {
buttonLayoutItem
}
func (*pushButtonLayoutItem) LayoutFlags() LayoutFlags {
return GrowableHorz
}