-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialog.go
228 lines (179 loc) · 4.22 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
218
219
220
221
222
223
224
225
226
227
228
// 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 (
"unsafe"
)
import (
"github.com/lxn/win"
)
const (
DlgCmdNone = 0
DlgCmdOK = win.IDOK
DlgCmdCancel = win.IDCANCEL
DlgCmdAbort = win.IDABORT
DlgCmdRetry = win.IDRETRY
DlgCmdIgnore = win.IDIGNORE
DlgCmdYes = win.IDYES
DlgCmdNo = win.IDNO
DlgCmdClose = win.IDCLOSE
DlgCmdHelp = win.IDHELP
DlgCmdTryAgain = win.IDTRYAGAIN
DlgCmdContinue = win.IDCONTINUE
DlgCmdTimeout = win.IDTIMEOUT
)
const dialogWindowClass = `\o/ Walk_Dialog_Class \o/`
func init() {
MustRegisterWindowClass(dialogWindowClass)
}
type dialogish interface {
DefaultButton() *PushButton
CancelButton() *PushButton
}
type Dialog struct {
FormBase
result int
defaultButton *PushButton
cancelButton *PushButton
centerInOwnerWhenRun bool
}
func NewDialog(owner Form) (*Dialog, error) {
return newDialogWithStyle(owner, win.WS_THICKFRAME)
}
func NewDialogWithFixedSize(owner Form) (*Dialog, error) {
return newDialogWithStyle(owner, 0)
}
func newDialogWithStyle(owner Form, style uint32) (*Dialog, error) {
dlg := &Dialog{
FormBase: FormBase{
owner: owner,
},
}
if err := InitWindow(
dlg,
owner,
dialogWindowClass,
win.WS_CAPTION|win.WS_SYSMENU|style,
0); err != nil {
return nil, err
}
succeeded := false
defer func() {
if !succeeded {
dlg.Dispose()
}
}()
dlg.centerInOwnerWhenRun = owner != nil
dlg.result = DlgCmdNone
succeeded = true
return dlg, nil
}
func (dlg *Dialog) DefaultButton() *PushButton {
return dlg.defaultButton
}
func (dlg *Dialog) SetDefaultButton(button *PushButton) error {
if button != nil && !win.IsChild(dlg.hWnd, button.hWnd) {
return newError("not a descendant of the dialog")
}
succeeded := false
if dlg.defaultButton != nil {
if err := dlg.defaultButton.setAndClearStyleBits(win.BS_PUSHBUTTON, win.BS_DEFPUSHBUTTON); err != nil {
return err
}
defer func() {
if !succeeded {
dlg.defaultButton.setAndClearStyleBits(win.BS_DEFPUSHBUTTON, win.BS_PUSHBUTTON)
}
}()
}
if button != nil {
if err := button.setAndClearStyleBits(win.BS_DEFPUSHBUTTON, win.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 && !win.IsChild(dlg.hWnd, button.hWnd) {
return newError("not a descendant of the dialog")
}
dlg.cancelButton = button
return nil
}
func (dlg *Dialog) Result() int {
return dlg.result
}
func (dlg *Dialog) Accept() {
dlg.Close(DlgCmdOK)
}
func (dlg *Dialog) Cancel() {
dlg.Close(DlgCmdCancel)
}
func (dlg *Dialog) Close(result int) {
dlg.result = result
dlg.FormBase.Close()
}
func (dlg *Dialog) Show() {
if dlg.owner != nil {
var size Size
if layout := dlg.Layout(); layout != nil {
size = layout.MinSize()
min := dlg.MinSize()
size.Width = maxi(size.Width, min.Width)
size.Height = maxi(size.Height, min.Height)
} else {
size = dlg.Size()
}
ob := dlg.owner.Bounds()
if dlg.centerInOwnerWhenRun {
dlg.SetBounds(fitRectToScreen(dlg.hWnd, Rectangle{
ob.X + (ob.Width-size.Width)/2,
ob.Y + (ob.Height-size.Height)/2,
size.Width,
size.Height,
}))
}
} else {
dlg.SetBounds(dlg.Bounds())
}
dlg.FormBase.Show()
}
func fitRectToScreen(hWnd win.HWND, r Rectangle) Rectangle {
var mi win.MONITORINFO
mi.CbSize = uint32(unsafe.Sizeof(mi))
if !win.GetMonitorInfo(win.MonitorFromWindow(
hWnd, win.MONITOR_DEFAULTTOPRIMARY), &mi) {
return r
}
mon := rectangleFromRECT(mi.RcWork)
mon.Height -= int(win.GetSystemMetrics(win.SM_CYCAPTION))
if r.Width <= mon.Width {
switch {
case r.X < mon.X:
r.X = mon.X
case r.X+r.Width > mon.X+mon.Width:
r.X = mon.X + mon.Width - r.Width
}
}
if r.Height <= mon.Height {
switch {
case r.Y < mon.Y:
r.Y = mon.Y
case r.Y+r.Height > mon.Y+mon.Height:
r.Y = mon.Y + mon.Height - r.Height
}
}
return r
}
func (dlg *Dialog) Run() int {
dlg.Show()
dlg.FormBase.Run()
return dlg.result
}