-
Notifications
You must be signed in to change notification settings - Fork 1
/
groupbox.go
440 lines (342 loc) · 9.4 KB
/
groupbox.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// 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.
//go:build windows
// +build windows
package winapi
import (
"syscall"
"unsafe"
"github.com/Gipcomp/win32/handle"
"github.com/Gipcomp/win32/user32"
"github.com/Gipcomp/win32/win"
"github.com/Gipcomp/winapi/errs"
)
const groupBoxWindowClass = `\o/ Walk_GroupBox_Class \o/`
func init() {
AppendToWalkInit(func() {
MustRegisterWindowClass(groupBoxWindowClass)
})
}
type GroupBox struct {
WidgetBase
hWndGroupBox handle.HWND
checkBox *CheckBox
composite *Composite
headerHeight int // in native pixels
titleChangedPublisher EventPublisher
}
func NewGroupBox(parent Container) (*GroupBox, error) {
gb := new(GroupBox)
if err := InitWidget(
gb,
parent,
groupBoxWindowClass,
user32.WS_VISIBLE,
user32.WS_EX_CONTROLPARENT); err != nil {
return nil, err
}
succeeded := false
defer func() {
if !succeeded {
gb.Dispose()
}
}()
strPtr, err2 := syscall.UTF16PtrFromString("BUTTON")
if err2 != nil {
errs.NewError(err2.Error())
}
gb.hWndGroupBox = user32.CreateWindowEx(
0, strPtr, nil,
user32.WS_CHILD|user32.WS_VISIBLE|user32.BS_GROUPBOX,
0, 0, 80, 24, gb.hWnd, 0, 0, nil)
if gb.hWndGroupBox == 0 {
return nil, errs.LastError("CreateWindowEx(BUTTON)")
}
user32.SetWindowLong(gb.hWndGroupBox, user32.GWL_ID, 1)
gb.applyFont(gb.Font())
gb.updateHeaderHeight()
var err error
gb.checkBox, err = NewCheckBox(gb)
if err != nil {
return nil, err
}
user32.SetWindowLong(gb.checkBox.hWnd, user32.GWL_ID, 2)
gb.SetCheckable(false)
gb.checkBox.SetChecked(true)
gb.checkBox.CheckedChanged().Attach(func() {
gb.applyEnabledFromCheckBox(gb.checkBox.Checked())
})
setWindowVisible(gb.checkBox.hWnd, false)
gb.composite, err = NewComposite(gb)
if err != nil {
return nil, err
}
user32.SetWindowLong(gb.composite.hWnd, user32.GWL_ID, 3)
gb.composite.name = "composite"
user32.SetWindowPos(gb.checkBox.hWnd, user32.HWND_TOP, 0, 0, 0, 0, user32.SWP_NOMOVE|user32.SWP_NOSIZE)
gb.SetBackground(NullBrush())
gb.MustRegisterProperty("Title", NewProperty(
func() interface{} {
return gb.Title()
},
func(v interface{}) error {
return gb.SetTitle(assertStringOr(v, ""))
},
gb.titleChangedPublisher.Event()))
gb.MustRegisterProperty("Checked", NewBoolProperty(
func() bool {
return gb.Checked()
},
func(v bool) error {
gb.SetChecked(v)
return nil
},
gb.CheckedChanged()))
succeeded = true
return gb, nil
}
func (gb *GroupBox) AsContainerBase() *ContainerBase {
if gb.composite == nil {
return nil
}
return gb.composite.AsContainerBase()
}
func (gb *GroupBox) ClientBoundsPixels() Rectangle {
cb := windowClientBounds(gb.hWndGroupBox)
if gb.Layout() == nil {
return cb
}
if gb.Checkable() {
s := createLayoutItemForWidget(gb.checkBox).(MinSizer).MinSize()
cb.Y += s.Height
cb.Height -= s.Height
}
padding := gb.IntFrom96DPI(1)
return Rectangle{cb.X + padding, cb.Y + gb.headerHeight, cb.Width - 2*padding, cb.Height - gb.headerHeight - 2*padding}
}
func (gb *GroupBox) updateHeaderHeight() {
gb.headerHeight = gb.calculateTextSizeImpl("gM").Height
}
func (gb *GroupBox) Persistent() bool {
return gb.composite.Persistent()
}
func (gb *GroupBox) SetPersistent(value bool) {
gb.composite.SetPersistent(value)
}
func (gb *GroupBox) SaveState() error {
return gb.composite.SaveState()
}
func (gb *GroupBox) RestoreState() error {
return gb.composite.RestoreState()
}
func (gb *GroupBox) applyEnabled(enabled bool) {
gb.WidgetBase.applyEnabled(enabled)
if gb.hWndGroupBox != 0 {
setWindowEnabled(gb.hWndGroupBox, enabled)
}
if gb.checkBox != nil {
gb.checkBox.applyEnabled(enabled)
}
if gb.composite != nil {
gb.composite.applyEnabled(enabled)
}
}
func (gb *GroupBox) applyEnabledFromCheckBox(enabled bool) {
if gb.hWndGroupBox != 0 {
setWindowEnabled(gb.hWndGroupBox, enabled)
}
if gb.composite != nil {
gb.composite.applyEnabled(enabled)
}
}
func (gb *GroupBox) applyFont(font *Font) {
gb.WidgetBase.applyFont(font)
if gb.checkBox != nil {
gb.checkBox.applyFont(font)
}
if gb.hWndGroupBox != 0 {
SetWindowFont(gb.hWndGroupBox, font)
}
if gb.composite != nil {
gb.composite.applyFont(font)
}
gb.updateHeaderHeight()
}
func (gb *GroupBox) SetSuspended(suspend bool) {
gb.composite.SetSuspended(suspend)
gb.WidgetBase.SetSuspended(suspend)
gb.Invalidate()
}
func (gb *GroupBox) DataBinder() *DataBinder {
return gb.composite.dataBinder
}
func (gb *GroupBox) SetDataBinder(dataBinder *DataBinder) {
gb.composite.SetDataBinder(dataBinder)
}
func (gb *GroupBox) Title() string {
if gb.Checkable() {
return gb.checkBox.Text()
}
return windowText(gb.hWndGroupBox)
}
func (gb *GroupBox) SetTitle(title string) error {
if gb.Checkable() {
if err := user32.SetWindowText(gb.hWndGroupBox, ""); err != nil {
return err
}
return gb.checkBox.SetText(title)
}
return user32.SetWindowText(gb.hWndGroupBox, title)
}
func (gb *GroupBox) Checkable() bool {
return gb.checkBox.visible
}
func (gb *GroupBox) SetCheckable(checkable bool) {
title := gb.Title()
gb.checkBox.SetVisible(checkable)
gb.SetTitle(title)
gb.RequestLayout()
}
func (gb *GroupBox) Checked() bool {
return gb.checkBox.Checked()
}
func (gb *GroupBox) SetChecked(checked bool) {
gb.checkBox.SetChecked(checked)
}
func (gb *GroupBox) CheckedChanged() *Event {
return gb.checkBox.CheckedChanged()
}
func (gb *GroupBox) ApplyDPI(dpi int) {
gb.WidgetBase.ApplyDPI(dpi)
if gb.checkBox != nil {
gb.checkBox.ApplyDPI(dpi)
}
if gb.composite != nil {
gb.composite.ApplyDPI(dpi)
}
}
func (gb *GroupBox) Children() *WidgetList {
if gb.composite == nil {
// Without this we would get into trouble in NewComposite.
return nil
}
return gb.composite.Children()
}
func (gb *GroupBox) Layout() Layout {
if gb.composite == nil {
// Without this we would get into trouble through the call to
// SetCheckable in NewGroupBox.
return nil
}
return gb.composite.Layout()
}
func (gb *GroupBox) SetLayout(value Layout) error {
return gb.composite.SetLayout(value)
}
func (gb *GroupBox) MouseDown() *MouseEvent {
return gb.composite.MouseDown()
}
func (gb *GroupBox) MouseMove() *MouseEvent {
return gb.composite.MouseMove()
}
func (gb *GroupBox) MouseUp() *MouseEvent {
return gb.composite.MouseUp()
}
func (gb *GroupBox) WndProc(hwnd handle.HWND, msg uint32, wParam, lParam uintptr) uintptr {
if gb.composite != nil {
switch msg {
case user32.WM_CTLCOLORSTATIC:
if hBrush := gb.handleWMCTLCOLOR(wParam, lParam); hBrush != 0 {
return hBrush
}
case user32.WM_COMMAND:
hwndSrc := user32.GetDlgItem(gb.hWnd, int32(win.LOWORD(uint32(wParam))))
if window := windowFromHandle(hwndSrc); window != nil {
window.WndProc(hwnd, msg, wParam, lParam)
}
case user32.WM_NOTIFY:
gb.composite.WndProc(hwnd, msg, wParam, lParam)
case user32.WM_SETTEXT:
gb.titleChangedPublisher.Publish()
case user32.WM_PAINT:
user32.UpdateWindow(gb.checkBox.hWnd)
case user32.WM_WINDOWPOSCHANGED:
wp := (*user32.WINDOWPOS)(unsafe.Pointer(lParam))
if wp.Flags&user32.SWP_NOSIZE != 0 {
break
}
wbcb := gb.WidgetBase.ClientBoundsPixels()
if !user32.MoveWindow(
gb.hWndGroupBox,
int32(wbcb.X),
int32(wbcb.Y),
int32(wbcb.Width),
int32(wbcb.Height),
true) {
errs.LastError("MoveWindow")
break
}
if gb.Checkable() {
s := createLayoutItemForWidget(gb.checkBox).(MinSizer).MinSize()
var x int
if l := gb.Layout(); l != nil {
x = gb.IntFrom96DPI(l.Margins().HNear)
} else {
x = gb.headerHeight * 2 / 3
}
gb.checkBox.SetBoundsPixels(Rectangle{x, gb.headerHeight, s.Width, s.Height})
}
}
}
return gb.WidgetBase.WndProc(hwnd, msg, wParam, lParam)
}
func (gb *GroupBox) CreateLayoutItem(ctx *LayoutContext) LayoutItem {
compositePos := Point{gb.IntFrom96DPI(1), gb.headerHeight}
if gb.Checkable() {
idealSize := gb.checkBox.idealSize()
compositePos.Y += idealSize.Height
}
li := &groupBoxLayoutItem{
compositePos: compositePos,
}
gbli := CreateLayoutItemsForContainerWithContext(gb.composite, ctx)
gbli.AsLayoutItemBase().parent = li
li.children = append(li.children, gbli)
return li
}
type groupBoxLayoutItem struct {
ContainerLayoutItemBase
compositePos Point // in native pixels
}
func (li *groupBoxLayoutItem) LayoutFlags() LayoutFlags {
return li.children[0].LayoutFlags()
}
func (li *groupBoxLayoutItem) MinSize() Size {
min := li.children[0].(MinSizer).MinSize()
min.Width += li.compositePos.X * 2
min.Height += li.compositePos.Y + 2
return min
}
func (li *groupBoxLayoutItem) MinSizeForSize(size Size) Size {
return li.MinSize()
}
func (li *groupBoxLayoutItem) HasHeightForWidth() bool {
return li.children[0].(HeightForWidther).HasHeightForWidth()
}
func (li *groupBoxLayoutItem) HeightForWidth(width int) int {
return li.children[0].(HeightForWidther).HeightForWidth(width-li.compositePos.X*2) + li.compositePos.Y
}
func (li *groupBoxLayoutItem) IdealSize() Size {
size := li.children[0].(IdealSizer).IdealSize()
size.Height += li.compositePos.Y
return size
}
func (li *groupBoxLayoutItem) PerformLayout() []LayoutResultItem {
return []LayoutResultItem{
{
Item: li.children[0],
Bounds: Rectangle{X: li.compositePos.X, Y: li.compositePos.Y, Width: li.geometry.Size.Width - li.compositePos.X*2, Height: li.geometry.Size.Height - li.compositePos.Y - 4},
},
}
}