-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialog.go
217 lines (168 loc) · 4.27 KB
/
dialog.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
// 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 (
"syscall"
"unsafe"
)
import . "github.com/lxn/go-winapi"
const (
DlgCmdOK = IDOK
DlgCmdCancel = IDCANCEL
DlgCmdAbort = IDABORT
DlgCmdRetry = IDRETRY
DlgCmdIgnore = IDIGNORE
DlgCmdYes = IDYES
DlgCmdNo = IDNO
DlgCmdClose = IDCLOSE
DlgCmdHelp = IDHELP
DlgCmdTryAgain = IDTRYAGAIN
DlgCmdContinue = IDCONTINUE
DlgCmdTimeout = IDTIMEOUT
)
const dialogWindowClass = `\o/ Walk_Dialog_Class \o/`
func init() {
mustRegisterWindowClass(dialogWindowClass)
}
type dialogish interface {
DefaultButton() *PushButton
CancelButton() *PushButton
}
type Dialog struct {
TopLevelWindow
result int
defaultButton *PushButton
cancelButton *PushButton
centerInOwnerWhenRun bool
}
func NewDialog(owner RootWidget) (*Dialog, error) {
dlg := &Dialog{
TopLevelWindow: TopLevelWindow{
owner: owner,
},
}
if err := initWidget(
dlg,
owner,
dialogWindowClass,
WS_CAPTION|WS_SYSMENU|WS_THICKFRAME,
WS_EX_DLGMODALFRAME); err != nil {
return nil, err
}
dlg.centerInOwnerWhenRun = owner != nil
dlg.children = newWidgetList(dlg)
// This forces display of focus rectangles, as soon as the user starts to type.
SendMessage(dlg.hWnd, WM_CHANGEUISTATE, UIS_INITIALIZE, 0)
dlg.result = DlgCmdClose
return dlg, nil
}
func (dlg *Dialog) DefaultButton() *PushButton {
return dlg.defaultButton
}
func (dlg *Dialog) SetDefaultButton(button *PushButton) error {
if button != nil && !IsChild(dlg.hWnd, button.hWnd) {
return newError("not a descendant of the dialog")
}
succeeded := false
if dlg.defaultButton != nil {
if err := dlg.defaultButton.setAndClearStyleBits(BS_PUSHBUTTON, BS_DEFPUSHBUTTON); err != nil {
return err
}
defer func() {
if !succeeded {
dlg.defaultButton.setAndClearStyleBits(BS_DEFPUSHBUTTON, BS_PUSHBUTTON)
}
}()
}
if button != nil {
if err := button.setAndClearStyleBits(BS_DEFPUSHBUTTON, BS_PUSHBUTTON); err != nil {
return err
}
}
dlg.defaultButton = button
succeeded = true
return nil
}
func (dlg *Dialog) CancelButton() *PushButton {
return dlg.cancelButton
}
func (dlg *Dialog) SetCancelButton(button *PushButton) error {
if button != nil && !IsChild(dlg.hWnd, button.hWnd) {
return newError("not a descendant of the dialog")
}
dlg.cancelButton = button
return nil
}
func (dlg *Dialog) Accept() {
dlg.Close(DlgCmdOK)
}
func (dlg *Dialog) Cancel() {
dlg.Close(DlgCmdCancel)
}
func (dlg *Dialog) Close(result int) {
dlg.result = result
dlg.TopLevelWindow.Close()
}
func firstFocusableDescendantCallback(hwnd HWND, lParam uintptr) uintptr {
widget := widgetFromHWND(hwnd)
if widget == nil || !widget.Visible() || !widget.Enabled() {
return 1
}
style := uint(GetWindowLong(hwnd, GWL_STYLE))
// FIXME: Ugly workaround for NumberEdit
_, isTextSelectable := widget.(textSelectable)
if style&WS_TABSTOP > 0 || isTextSelectable {
hwndPtr := (*HWND)(unsafe.Pointer(lParam))
*hwndPtr = hwnd
return 0
}
return 1
}
var firstFocusableDescendantCallbackPtr = syscall.NewCallback(firstFocusableDescendantCallback)
func firstFocusableDescendant(container Container) Widget {
var hwnd HWND
EnumChildWindows(container.BaseWidget().hWnd, firstFocusableDescendantCallbackPtr, uintptr(unsafe.Pointer(&hwnd)))
return widgetFromHWND(hwnd)
}
type textSelectable interface {
SetTextSelection(start, end int)
}
func (dlg *Dialog) focusFirstCandidateDescendant() {
widget := firstFocusableDescendant(dlg)
if widget == nil {
return
}
if err := widget.SetFocus(); err != nil {
return
}
if textSel, ok := widget.(textSelectable); ok {
textSel.SetTextSelection(0, -1)
}
}
func (dlg *Dialog) Show() {
if dlg.owner != nil {
ob := dlg.owner.Bounds()
b := dlg.Bounds()
if dlg.centerInOwnerWhenRun {
dlg.SetBounds(Rectangle{
ob.X + (ob.Width-b.Width)/2,
ob.Y + (ob.Height-b.Height)/2,
b.Width,
b.Height,
})
}
} else {
dlg.SetBounds(dlg.Bounds())
}
dlg.TopLevelWindow.Show()
dlg.focusFirstCandidateDescendant()
}
func (dlg *Dialog) Run() int {
dlg.Show()
if dlg.owner != nil {
dlg.owner.SetEnabled(false)
}
dlg.TopLevelWindow.Run()
return dlg.result
}