-
Notifications
You must be signed in to change notification settings - Fork 2
/
widget.go
327 lines (258 loc) · 7.52 KB
/
widget.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
// 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 (
"github.com/lxn/win"
)
// LayoutFlags specify how a Widget wants to be treated when used with a Layout.
//
// These flags are interpreted in respect to Widget.SizeHint.
type LayoutFlags byte
const (
// ShrinkableHorz allows a Widget to be shrunk horizontally.
ShrinkableHorz LayoutFlags = 1 << iota
// ShrinkableVert allows a Widget to be shrunk vertically.
ShrinkableVert
// GrowableHorz allows a Widget to be enlarged horizontally.
GrowableHorz
// GrowableVert allows a Widget to be enlarged vertically.
GrowableVert
// GreedyHorz specifies that the widget prefers to take up as much space as
// possible, horizontally.
GreedyHorz
// GreedyVert specifies that the widget prefers to take up as much space as
// possible, vertically.
GreedyVert
)
type Widget interface {
Window
// AsWidgetBase returns a *WidgetBase that implements Widget.
AsWidgetBase() *WidgetBase
// Form returns the root ancestor Form of the Widget.
Form() Form
// LayoutFlags returns a combination of LayoutFlags that specify how the
// Widget wants to be treated by Layout implementations.
LayoutFlags() LayoutFlags
// MinSizeHint returns the minimum outer Size, including decorations, that
// makes sense for the respective type of Widget.
MinSizeHint() Size
// Parent returns the Container of the Widget.
Parent() Container
// SetParent sets the parent of the Widget and adds the Widget to the
// Children list of the Container.
SetParent(value Container) error
// SetToolTipText sets the tool tip text of the Widget.
SetToolTipText(s string) error
// SizeHint returns the preferred Size for the respective type of Widget.
SizeHint() Size
// ToolTipText returns the tool tip text of the Widget.
ToolTipText() string
}
type WidgetBase struct {
WindowBase
parent Container
toolTipTextProperty Property
toolTipTextChangedPublisher EventPublisher
}
// InitWidget initializes a Widget.
func InitWidget(widget Widget, parent Window, className string, style, exStyle uint32) error {
if parent == nil {
return newError("parent cannot be nil")
}
if err := InitWindow(widget, parent, className, style|win.WS_CHILD, exStyle); err != nil {
return err
}
if container, ok := parent.(Container); ok {
if container.Children() == nil {
// Required by parents like MainWindow and GroupBox.
if win.SetParent(widget.Handle(), container.Handle()) == 0 {
return lastError("SetParent")
}
} else {
if err := container.Children().Add(widget); err != nil {
return err
}
}
}
return nil
}
func (wb *WidgetBase) init(widget Widget) error {
if err := globalToolTip.AddTool(wb); err != nil {
return err
}
wb.toolTipTextProperty = NewProperty(
func() interface{} {
return wb.window.(Widget).ToolTipText()
},
func(v interface{}) error {
wb.window.(Widget).SetToolTipText(v.(string))
return nil
},
wb.toolTipTextChangedPublisher.Event())
wb.MustRegisterProperty("ToolTipText", wb.toolTipTextProperty)
return nil
}
// AsWidgetBase just returns the receiver.
func (wb *WidgetBase) AsWidgetBase() *WidgetBase {
return wb
}
// Bounds returns the outer bounding box Rectangle of the WidgetBase, including
// decorations.
//
// The coordinates are relative to the parent of the Widget.
func (wb *WidgetBase) Bounds() Rectangle {
b := wb.WindowBase.Bounds()
if wb.parent != nil {
p := win.POINT{int32(b.X), int32(b.Y)}
if !win.ScreenToClient(wb.parent.Handle(), &p) {
newError("ScreenToClient failed")
return Rectangle{}
}
b.X = int(p.X)
b.Y = int(p.Y)
}
return b
}
// BringToTop moves the WidgetBase to the top of the keyboard focus order.
func (wb *WidgetBase) BringToTop() error {
if wb.parent != nil {
if err := wb.parent.BringToTop(); err != nil {
return err
}
}
return wb.WindowBase.BringToTop()
}
// Enabled returns if the WidgetBase is enabled for user interaction.
func (wb *WidgetBase) Enabled() bool {
if wb.parent != nil {
return wb.enabled && wb.parent.Enabled()
}
return wb.enabled
}
// Font returns the Font of the WidgetBase.
//
// By default this is a MS Shell Dlg 2, 8 point font.
func (wb *WidgetBase) Font() *Font {
if wb.font != nil {
return wb.font
} else if wb.parent != nil {
return wb.parent.Font()
}
return defaultFont
}
// Form returns the root ancestor Form of the Widget.
func (wb *WidgetBase) Form() Form {
return ancestor(wb)
}
// LayoutFlags returns a combination of LayoutFlags that specify how the
// WidgetBase wants to be treated by Layout implementations.
func (wb *WidgetBase) LayoutFlags() LayoutFlags {
return 0
}
// MinSizeHint returns the minimum outer Size, including decorations, that
// makes sense for the respective type of Widget.
func (wb *WidgetBase) MinSizeHint() Size {
return Size{10, 10}
}
// Parent returns the Container of the WidgetBase.
func (wb *WidgetBase) Parent() Container {
return wb.parent
}
// SetParent sets the parent of the WidgetBase and adds the WidgetBase to the
// Children list of the Container.
func (wb *WidgetBase) SetParent(parent Container) (err error) {
if parent == wb.parent {
return nil
}
style := uint32(win.GetWindowLong(wb.hWnd, win.GWL_STYLE))
if style == 0 {
return lastError("GetWindowLong")
}
if parent == nil {
style &^= win.WS_CHILD
style |= win.WS_POPUP
if win.SetParent(wb.hWnd, 0) == 0 {
return lastError("SetParent")
}
win.SetLastError(0)
if win.SetWindowLong(wb.hWnd, win.GWL_STYLE, int32(style)) == 0 {
return lastError("SetWindowLong")
}
} else {
style |= win.WS_CHILD
style &^= win.WS_POPUP
win.SetLastError(0)
if win.SetWindowLong(wb.hWnd, win.GWL_STYLE, int32(style)) == 0 {
return lastError("SetWindowLong")
}
if win.SetParent(wb.hWnd, parent.Handle()) == 0 {
return lastError("SetParent")
}
}
b := wb.Bounds()
if !win.SetWindowPos(
wb.hWnd,
win.HWND_BOTTOM,
int32(b.X),
int32(b.Y),
int32(b.Width),
int32(b.Height),
win.SWP_FRAMECHANGED) {
return lastError("SetWindowPos")
}
oldParent := wb.parent
wb.parent = parent
var oldChildren, newChildren *WidgetList
if oldParent != nil {
oldChildren = oldParent.Children()
}
if parent != nil {
newChildren = parent.Children()
}
if newChildren == oldChildren {
return nil
}
widget := wb.window.(Widget)
if oldChildren != nil {
oldChildren.Remove(widget)
}
if newChildren != nil && !newChildren.containsHandle(wb.hWnd) {
newChildren.Add(widget)
}
return nil
}
// SizeHint returns a default Size that should be "overidden" by a concrete
// Widget type.
func (wb *WidgetBase) SizeHint() Size {
return wb.window.(Widget).MinSizeHint()
}
// ToolTipText returns the tool tip text of the WidgetBase.
func (wb *WidgetBase) ToolTipText() string {
return globalToolTip.Text(wb.window.(Widget))
}
// SetToolTipText sets the tool tip text of the WidgetBase.
func (wb *WidgetBase) SetToolTipText(s string) error {
if err := globalToolTip.SetText(wb.window.(Widget), s); err != nil {
return err
}
wb.toolTipTextChangedPublisher.Publish()
return nil
}
func (wb *WidgetBase) updateParentLayout() error {
if wb.parent == nil || wb.parent.Layout() == nil || wb.parent.Suspended() {
return nil
}
return wb.parent.Layout().Update(false)
}
func ancestor(w Widget) Form {
if w == nil {
return nil
}
hWndRoot := win.GetAncestor(w.Handle(), win.GA_ROOT)
rw, _ := windowFromHandle(hWndRoot).(Form)
return rw
}
func minSizeEffective(w Widget) Size {
return maxSize(w.MinSize(), w.MinSizeHint())
}