-
Notifications
You must be signed in to change notification settings - Fork 24
/
control.go
513 lines (439 loc) · 11.2 KB
/
control.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
//+build windows
package wui
import (
"syscall"
"unicode"
"unicode/utf8"
"unsafe"
"github.com/gonutz/w32/v2"
)
// Anchor defines how a child control is resized when its parent changes size.
// Children can be anchored to min (left or top), center and max (right or
// bottom) of their parents.
type Anchor int
const (
// AnchorMin makes a control stick to the left/top side of its parent. The
// control's size is not changed.
AnchorMin Anchor = iota
// AnchorMax makes a control stick to the right/bottom side of its parent.
// The control's size is not changed.
AnchorMax
// AnchorCenter keeps a control fixed at the horizontal/vertical center of
// its parent, e.g. if it is placed 10 pixels left of the center, it will
// stay 10 pixels left of its parent's center. Its size is not changed.
AnchorCenter
// AnchorMinAndMax makes a control's borders stick to its parent's borders.
// The size changes propertionally to its parent's size, keeping the
// original distances to its parents borders.
AnchorMinAndMax
// AnchorMinAndCenter makes the left/top side of a control stick to its
// parent's left/top side while the right/bottom side sticks to the parent's
// center.
AnchorMinAndCenter
// AnchorMaxAndCenter makes the right/bottom side of a control stick to its
// parent's right/bottom side while the left/top side sticks to the parent's
// center.
AnchorMaxAndCenter
)
func (a Anchor) String() string {
// NOTE that these strings are used in the designer to get their
// representations as Go code so they must always correspond to their
// constant names and be prefixed with the package name.
switch a {
case AnchorMin:
return "wui.AnchorMin"
case AnchorMax:
return "wui.AnchorMax"
case AnchorCenter:
return "wui.AnchorCenter"
case AnchorMinAndMax:
return "wui.AnchorMinAndMax"
case AnchorMinAndCenter:
return "wui.AnchorMinAndCenter"
case AnchorMaxAndCenter:
return "wui.AnchorMaxAndCenter"
default:
return "unknown Anchor"
}
}
type control struct {
handle w32.HWND
x int
y int
width int
height int
hAnchor Anchor
vAnchor Anchor
parent Container
disabled bool
hidden bool
onResize func()
onTabFocus func()
}
// closing defaults to nothing, the base control has no properties that are
// user-changeable through the GUI.
func (c *control) closing() {}
func (c *control) destroy() {
if c.handle != 0 {
w32.DestroyWindow(c.handle)
c.handle = 0
}
}
func (c *control) Handle() uintptr {
return uintptr(c.handle)
}
func (c *control) Parent() Container {
return c.parent
}
func (c *control) setParent(parent Container) {
c.parent = parent
}
func (c *control) create(id int, exStyle uint, className string, style uint) {
var visible uint
if !c.hidden {
visible = w32.WS_VISIBLE
}
c.handle = w32.CreateWindowExStr(
exStyle,
className,
"",
visible|w32.WS_CHILD|style,
c.x, c.y, c.width, c.height,
c.parent.getHandle(), w32.HMENU(id), c.parent.getInstance(), nil,
)
if c.disabled {
w32.EnableWindow(c.handle, false)
}
}
func (c *control) parentFontChanged() {}
func (c *control) SetOnResize(f func()) {
c.onResize = f
}
func (c *control) OnResize() func() {
return c.onResize
}
func (c *control) X() int {
return c.x
}
func (c *control) SetX(x int) {
c.SetBounds(x, c.y, c.width, c.height)
}
func (c *control) Y() int {
return c.y
}
func (c *control) SetY(y int) {
c.SetBounds(c.x, y, c.width, c.height)
}
func (c *control) Position() (x, y int) {
return c.x, c.y
}
func (c *control) SetPosition(x, y int) {
c.SetBounds(x, y, c.width, c.height)
}
func (c *control) Width() int {
return c.width
}
func (c *control) SetWidth(width int) {
c.SetBounds(c.x, c.y, width, c.height)
}
func (c *control) Height() int {
return c.height
}
func (c *control) SetHeight(height int) {
c.SetBounds(c.x, c.y, c.width, height)
}
func (c *control) Size() (width, height int) {
return c.width, c.height
}
func (c *control) SetSize(width, height int) {
c.SetBounds(c.x, c.y, width, height)
}
func (c *control) Bounds() (x, y, width, height int) {
return c.x, c.y, c.width, c.height
}
func (c *control) SetBounds(x, y, width, height int) {
resize := false
if c.width != width || c.height != height {
resize = true
}
c.x, c.y, c.width, c.height = x, y, width, height
if c.handle != 0 {
w32.SetWindowPos(
c.handle, 0,
c.x, c.y, c.width, c.height,
w32.SWP_NOOWNERZORDER|w32.SWP_NOZORDER,
)
}
if resize && c.onResize != nil {
c.onResize()
}
}
func (c *control) SetAnchors(horizontal, vertical Anchor) {
c.hAnchor = horizontal
c.vAnchor = vertical
}
func (c *control) SetHorizontalAnchor(a Anchor) {
c.hAnchor = a
}
func (c *control) SetVerticalAnchor(a Anchor) {
c.vAnchor = a
}
func (c *control) Anchors() (horizontal, vertical Anchor) {
return c.hAnchor, c.vAnchor
}
func (c *control) HorizontalAnchor() Anchor {
return c.hAnchor
}
func (c *control) VerticalAnchor() Anchor {
return c.vAnchor
}
func (c *control) Enabled() bool {
return !c.disabled
}
func (c *control) SetEnabled(e bool) {
c.disabled = !e
if c.handle != 0 {
w32.EnableWindow(c.handle, e)
}
}
func (c *control) Visible() bool {
return !c.hidden
}
func (c *control) SetVisible(v bool) {
c.hidden = !v
if c.handle != 0 {
if v {
w32.ShowWindow(c.handle, w32.SW_SHOW)
} else {
w32.ShowWindow(c.handle, w32.SW_HIDE)
}
w32.InvalidateRect(c.handle, nil, true)
}
}
func (c *control) handleNotification(cmd uintptr) {}
func (c *control) wasFocussedWithTab() {
if c.onTabFocus != nil {
c.onTabFocus()
}
}
type textControl struct {
control
text string
font *Font
}
func (c *textControl) create(id int, exStyle uint, className string, style uint) {
c.control.create(id, exStyle, className, style)
w32.SetWindowText(c.handle, c.text)
c.SetFont(c.font)
}
func deleteWordBeforeCursor(text []rune, cursor int) (newText string, newCursor int) {
prefix := text[:cursor]
n := len(prefix)
if n >= 2 && prefix[n-2] == '\r' && prefix[n-1] == '\n' {
prefix = prefix[:n-2]
} else if n <= 1 {
prefix = nil
} else {
if unicode.IsSpace(prefix[n-1]) {
for n > 0 && unicode.IsSpace(prefix[n-1]) {
prefix = prefix[:n-1]
n--
}
}
for n > 0 && !unicode.IsSpace(prefix[n-1]) {
prefix = prefix[:n-1]
n--
}
}
newText = string(append(prefix, text[cursor:]...))
newCursor = len(prefix)
return
}
func (c *textControl) Text() string {
if c.handle != 0 {
c.text = w32.GetWindowText(c.handle)
}
return c.text
}
func (c *textControl) SetText(text string) {
c.text = text
if c.handle != 0 {
w32.SetWindowText(c.handle, text)
}
}
func (c *textControl) Font() *Font {
return c.font
}
func (c *textControl) parentFontChanged() {
c.SetFont(c.font)
}
func (c *textControl) SetFont(font *Font) {
c.font = font
if c.handle != 0 {
w32.SendMessage(c.handle, w32.WM_SETFONT, uintptr(c.fontHandle()), 1)
}
}
func (c *textControl) fontHandle() w32.HFONT {
if c.font != nil {
return c.font.handle
}
if c.parent != nil {
font := c.parent.Font()
if font != nil {
return font.handle
}
}
return 0
}
func (c *textControl) Focus() {
// TODO Allow this before showing a window.
if c.handle != 0 {
w32.SetFocus(c.handle)
}
}
func (c *textControl) HasFocus() bool {
return c.handle != 0 && w32.GetFocus() == c.handle
}
type textEditControl struct {
textControl
cursorStart int
cursorEnd int
}
func (c *textEditControl) create(id int, exStyle uint, className string, style uint) {
c.textControl.create(id, exStyle, className, style)
if c.cursorStart != 0 || c.cursorEnd != 0 {
c.setCursor(c.cursorStart, c.cursorEnd)
}
w32.SetWindowSubclass(c.handle, textEditSubclassProc, 0, uintptr(unsafe.Pointer(c)))
}
var textEditSubclassProc = syscall.NewCallback(func(
window w32.HWND,
msg uint32,
wParam, lParam uintptr,
subclassID uintptr,
refData uintptr,
) uintptr {
c := (*textEditControl)(unsafe.Pointer(refData))
switch msg {
case w32.WM_CHAR:
shift := w32.GetKeyState(w32.VK_SHIFT)&0x8000 != 0
if wParam == 1 {
// Ctrl+A was pressed - select all text.
c.SelectAll()
return 0
}
if wParam == 26 && !shift {
// TODO Ctrl+Z was pressed - undo the last action.
//return 0
}
if wParam == 25 || wParam == 26 && shift {
// TODO Ctrl+Y of Ctrl+Shift+Z was pressed - redo the last action.
//return 0
}
if wParam == 127 {
// Ctrl+Backspace was pressed, if there is currently a selection
// active, delete it. If there is just the cursor, delete the
// last word before the cursor.
text := []rune(c.Text())
start, end := c.CursorPosition()
if start != end {
// There is a selection, delete it.
c.SetText(string(append(text[:start], text[end:]...)))
c.SetCursorPosition(start)
} else {
// No selection, delete the last word before the cursor.
newText, newCursor := deleteWordBeforeCursor(text, start)
c.SetText(newText)
c.SetCursorPosition(newCursor)
}
// Since we will return 0 from this message, no EN_CHANGE will be
// sent for us as this is usually done by DefSubclassProc. We have
// to send it ourselves.
if c.parent != nil {
id := w32.GetDlgCtrlID(c.handle)
w32.SendMessage(
c.parent.getHandle(),
w32.WM_COMMAND,
uintptr(id)&0xFFFF|(w32.EN_CHANGE<<16),
uintptr(c.handle),
)
}
return 0
}
return w32.DefSubclassProc(window, msg, wParam, lParam)
default:
return w32.DefSubclassProc(window, msg, wParam, lParam)
}
})
// CursorPosition returns the current cursor position, respectively the current
// selection.
//
// If no selection is active, the returned start and end values are the same.
// They then indicate the index of the UTF-8 character in this EditLine's Text()
// before which the caret is currently set.
//
// If a selection is active, start is the index of the first selected UTF-8
// character in Text() and end is the position one character after the end of
// the selection. The selected text is thus
//
// c.Text()[start:end]
func (c *textEditControl) CursorPosition() (start, end int) {
if c.handle != 0 {
var start, end uint32
w32.SendMessage(
c.handle,
w32.EM_GETSEL,
uintptr(unsafe.Pointer(&start)),
uintptr(unsafe.Pointer(&end)),
)
c.cursorStart, c.cursorEnd = int(start), int(end)
}
return c.cursorStart, c.cursorEnd
}
func (c *textEditControl) SetCursorPosition(pos int) {
c.setCursor(pos, pos)
}
func (c *textEditControl) SetSelection(start, end int) {
c.setCursor(start, end)
}
func (c *textEditControl) SelectAll() {
c.setCursor(0, -1)
}
func (c *textEditControl) setCursor(start, end int) {
c.cursorStart = start
c.cursorEnd = end
if c.handle != 0 {
w32.SendMessage(
c.handle,
w32.EM_SETSEL,
uintptr(uint32(c.cursorStart)),
uintptr(uint32(c.cursorEnd)),
)
w32.SendMessage(c.handle, w32.EM_SCROLLCARET, 0, 0)
} else {
c.clampCursorToText()
}
}
func (c *textEditControl) clampCursorToText() {
// If called before we have a window, we have to handle clamping of the
// positions ourselves.
n := utf8.RuneCountInString(c.Text())
if c.cursorEnd == -1 {
c.cursorEnd = n
}
if c.cursorStart < 0 {
c.cursorStart = 0
}
if c.cursorStart > n {
c.cursorStart = n
}
if c.cursorEnd < 0 {
c.cursorEnd = 0
}
if c.cursorEnd > n {
c.cursorEnd = n
}
if c.cursorEnd < c.cursorStart {
c.cursorStart, c.cursorEnd = c.cursorEnd, c.cursorStart
}
}