/
window.go
2516 lines (2028 loc) · 66.3 KB
/
window.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 (
"bytes"
"fmt"
"image"
"runtime"
"strings"
"sync/atomic"
"syscall"
"unsafe"
"github.com/lxn/win"
)
// App-specific message ids for internal use in Walk.
// TODO: Document reserved range somewhere (when we have an idea how many we need).
const (
notifyIconMessageId = win.WM_APP + iota
)
// Window is an interface that provides operations common to all windows.
type Window interface {
// AddDisposable adds a Disposable resource that should be disposed of
// together with this Window.
AddDisposable(d Disposable)
// AsWindowBase returns a *WindowBase, a pointer to an instance of the
// struct that implements most operations common to all windows.
AsWindowBase() *WindowBase
// Accessibility returns the accessibility object used to set Dynamic Annotation properties of the
// window.
Accessibility() *Accessibility
// Background returns the background Brush of the Window.
//
// By default this is nil.
Background() Brush
// Bounds returns the outer bounding box rectangle of the Window, including
// decorations.
//
// For a Form, like *MainWindow or *Dialog, the rectangle is in screen
// coordinates, for a child Window the coordinates are relative to its
// parent.
Bounds() Rectangle
// BoundsPixels returns the outer bounding box rectangle of the Window, including
// decorations.
//
// For a Form, like *MainWindow or *Dialog, the rectangle is in screen
// coordinates, for a child Window the coordinates are relative to its
// parent.
BoundsPixels() Rectangle
// BoundsChanged returns an *Event that you can attach to for handling bounds
// changed events for the Window.
BoundsChanged() *Event
// BringToTop moves the Window to the top of the keyboard focus order.
BringToTop() error
// ClientBounds returns the inner bounding box rectangle of the Window,
// excluding decorations.
ClientBounds() Rectangle
// ClientBoundsPixels returns the inner bounding box rectangle of the Window,
// excluding decorations.
ClientBoundsPixels() Rectangle
// ContextMenu returns the context menu of the Window.
//
// By default this is nil.
ContextMenu() *Menu
// ContextMenuLocation returns the context menu suggested location in screen coordinates in
// native pixels. This method is called when context menu is invoked using keyboard and mouse
// coordinates are not available.
ContextMenuLocation() Point
// CreateCanvas creates and returns a *Canvas that can be used to draw
// inside the ClientBoundsPixels of the Window.
//
// Remember to call the Dispose method on the canvas to release resources,
// when you no longer need it.
CreateCanvas() (*Canvas, error)
// Cursor returns the Cursor of the Window.
//
// By default this is nil.
Cursor() Cursor
// Dispose releases the operating system resources, associated with the
// Window.
//
// If a user closes a *MainWindow or *Dialog, it is automatically released.
// Also, if a Container is disposed of, all its descendants will be released
// as well.
Dispose()
// Disposing returns an Event that is published when the Window is disposed
// of.
Disposing() *Event
// DoubleBuffering returns whether double buffering of the
// drawing is enabled, which may help reduce flicker.
DoubleBuffering() bool
// DPI returns the current DPI value of the Window.
DPI() int
// Enabled returns if the Window is enabled for user interaction.
Enabled() bool
// Focused returns whether the Window has the keyboard input focus.
Focused() bool
// FocusedChanged returns an Event that you can attach to for handling focus
// changed events for the Window.
FocusedChanged() *Event
// Font returns the *Font of the Window.
//
// By default this is a MS Shell Dlg 2, 8 point font.
Font() *Font
// Form returns the Form of the Window.
Form() Form
// Handle returns the window handle of the Window.
Handle() win.HWND
// Height returns the outer height of the Window, including decorations.
Height() int
// HeightPixels returns the outer height of the Window, including decorations.
HeightPixels() int
// Invalidate schedules a full repaint of the Window.
Invalidate() error
// IsDisposed returns if the Window has been disposed of.
IsDisposed() bool
// KeyDown returns a *KeyEvent that you can attach to for handling key down
// events for the Window.
KeyDown() *KeyEvent
// KeyPress returns a *KeyEvent that you can attach to for handling key
// press events for the Window.
KeyPress() *KeyEvent
// KeyUp returns a *KeyEvent that you can attach to for handling key up
// events for the Window.
KeyUp() *KeyEvent
// MaxSize returns the maximum allowed outer size for the Window, including
// decorations.
//
// For child windows, this is only relevant when the parent of the Window
// has a Layout. RootWidgets, like *MainWindow and *Dialog, also honor this.
MaxSize() Size
// MaxSizePixels returns the maximum allowed outer size for the Window, including
// decorations.
//
// For child windows, this is only relevant when the parent of the Window
// has a Layout. RootWidgets, like *MainWindow and *Dialog, also honor this.
MaxSizePixels() Size
// MinSize returns the minimum allowed outer size for the Window, including
// decorations.
//
// For child windows, this is only relevant when the parent of the Window
// has a Layout. RootWidgets, like *MainWindow and *Dialog, also honor this.
MinSize() Size
// MinSizePixels returns the minimum allowed outer size for the Window, including
// decorations.
//
// For child windows, this is only relevant when the parent of the Window
// has a Layout. RootWidgets, like *MainWindow and *Dialog, also honor this.
MinSizePixels() Size
// MouseDown returns a *MouseEvent that you can attach to for handling
// mouse down events for the Window.
MouseDown() *MouseEvent
// MouseMove returns a *MouseEvent that you can attach to for handling
// mouse move events for the Window.
MouseMove() *MouseEvent
// MouseUp returns a *MouseEvent that you can attach to for handling
// mouse up events for the Window.
MouseUp() *MouseEvent
// Name returns the name of the Window.
Name() string
// RequestLayout either schedules or immediately starts performing layout.
RequestLayout()
// RightToLeftReading returns whether the reading order of the Window
// is from right to left.
RightToLeftReading() bool
// Screenshot returns an image of the window.
Screenshot() (*image.RGBA, error)
// SendMessage sends a message to the window and returns the result.
SendMessage(msg uint32, wParam, lParam uintptr) uintptr
// SetBackground sets the background Brush of the Window.
SetBackground(value Brush)
// SetBounds sets the outer bounding box rectangle of the Window, including
// decorations.
//
// For a Form, like *MainWindow or *Dialog, the rectangle is in screen
// coordinates, for a child Window the coordinates are relative to its
// parent.
SetBounds(value Rectangle) error
// SetBoundsPixels sets the outer bounding box rectangle of the Window, including
// decorations.
//
// For a Form, like *MainWindow or *Dialog, the rectangle is in screen
// coordinates, for a child Window the coordinates are relative to its
// parent.
SetBoundsPixels(value Rectangle) error
// SetClientSize sets the size of the inner bounding box of the Window,
// excluding decorations.
SetClientSize(value Size) error
// SetClientSizePixels sets the size of the inner bounding box of the Window,
// excluding decorations.
SetClientSizePixels(value Size) error
// SetContextMenu sets the context menu of the Window.
SetContextMenu(value *Menu)
// SetCursor sets the Cursor of the Window.
SetCursor(value Cursor)
// SetDoubleBuffering enables or disables double buffering of the
// drawing, which may help reduce flicker.
SetDoubleBuffering(value bool) error
// SetEnabled sets if the Window is enabled for user interaction.
SetEnabled(value bool)
// SetFocus sets the keyboard input focus to the Window.
SetFocus() error
// SetFont sets the *Font of the Window.
SetFont(value *Font)
// SetHeight sets the outer height of the Window, including decorations.
SetHeight(value int) error
// SetHeightPixels sets the outer height of the Window, including decorations.
SetHeightPixels(value int) error
// SetMinMaxSize sets the minimum and maximum outer size of the Window,
// including decorations.
//
// Use walk.Size{} to make the respective limit be ignored.
SetMinMaxSize(min, max Size) error
// SetMinMaxSizePixels sets the minimum and maximum outer size of the Window,
// including decorations.
//
// Use walk.Size{} to make the respective limit be ignored.
SetMinMaxSizePixels(min, max Size) error
// SetName sets the name of the Window.
//
// This is important if you want to make use of the built-in UI persistence.
// Some windows support automatic state persistence. See Settings for
// details.
SetName(name string)
// SetRightToLeftReading sets whether the reading order of the Window
// is from right to left.
SetRightToLeftReading(rtl bool) error
// SetSize sets the outer size of the Window, including decorations.
SetSize(value Size) error
// SetSizePixels sets the outer size of the Window, including decorations.
SetSizePixels(value Size) error
// SetSuspended sets if the Window is suspended for layout and repainting
// purposes.
//
// You should call SetSuspended(true), before doing a batch of modifications
// that would cause multiple layout or drawing updates. Remember to call
// SetSuspended(false) afterwards, which will update the Window accordingly.
SetSuspended(suspend bool)
// SetVisible sets if the Window is visible.
SetVisible(value bool)
// SetWidth sets the outer width of the Window, including decorations.
SetWidth(value int) error
// SetWidthPixels sets the outer width of the Window, including decorations.
SetWidthPixels(value int) error
// SetX sets the x coordinate of the Window, relative to the screen for
// RootWidgets like *MainWindow or *Dialog and relative to the parent for
// child Windows.
SetX(value int) error
// SetXPixels sets the x coordinate of the Window, relative to the screen for
// RootWidgets like *MainWindow or *Dialog and relative to the parent for
// child Windows.
SetXPixels(value int) error
// SetY sets the y coordinate of the Window, relative to the screen for
// RootWidgets like *MainWindow or *Dialog and relative to the parent for
// child Windows.
SetY(value int) error
// SetYPixels sets the y coordinate of the Window, relative to the screen for
// RootWidgets like *MainWindow or *Dialog and relative to the parent for
// child Windows.
SetYPixels(value int) error
// Size returns the outer size of the Window, including decorations.
Size() Size
// SizePixels returns the outer size of the Window, including decorations.
SizePixels() Size
// SizeChanged returns an *Event that you can attach to for handling size
// changed events for the Window.
SizeChanged() *Event
// Suspended returns if the Window is suspended for layout and repainting
// purposes.
Suspended() bool
// Synchronize enqueues func f to be called some time later by the main
// goroutine from inside a message loop.
Synchronize(f func())
// Visible returns if the Window is visible.
Visible() bool
// VisibleChanged returns an Event that you can attach to for handling
// visible changed events for the Window.
VisibleChanged() *Event
// Width returns the outer width of the Window, including decorations.
Width() int
// WidthPixels returns the outer width of the Window, including decorations.
WidthPixels() int
// WndProc is the window procedure of the window.
//
// When implementing your own WndProc to add or modify behavior, call the
// WndProc of the embedded window for messages you don't handle yourself.
WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr
// X returns the x coordinate of the Window, relative to the screen for
// RootWidgets like *MainWindow or *Dialog and relative to the parent for
// child Windows.
X() int
// XPixels returns the x coordinate of the Window, relative to the screen for
// RootWidgets like *MainWindow or *Dialog and relative to the parent for
// child Windows.
XPixels() int
// Y returns the y coordinate of the Window, relative to the screen for
// RootWidgets like *MainWindow or *Dialog and relative to the parent for
// child Windows.
Y() int
// YPixels returns the y coordinate of the Window, relative to the screen for
// RootWidgets like *MainWindow or *Dialog and relative to the parent for
// child Windows.
YPixels() int
}
type calcTextSizeInfo struct {
width int // in native pixels
font fontInfo
text string
dpi int
}
// WindowBase implements many operations common to all Windows.
type WindowBase struct {
nopActionListObserver
group *WindowGroup
window Window
form Form
hWnd win.HWND
origWndProcPtr uintptr
name string
font *Font
hFont win.HFONT
contextMenu *Menu
shortcutActions *ActionList
disposables []Disposable
disposingPublisher EventPublisher
dropFilesPublisher DropFilesEventPublisher
keyDownPublisher KeyEventPublisher
keyPressPublisher KeyEventPublisher
keyUpPublisher KeyEventPublisher
mouseDownPublisher MouseEventPublisher
mouseUpPublisher MouseEventPublisher
mouseMovePublisher MouseEventPublisher
mouseWheelPublisher MouseEventPublisher
boundsChangedPublisher EventPublisher
sizeChangedPublisher EventPublisher
maxSize96dpi Size
minSize96dpi Size
background Brush
cursor Cursor
name2Property map[string]Property
enabledProperty Property
enabledChangedPublisher EventPublisher
visibleProperty Property
visibleChangedPublisher EventPublisher
focusedProperty Property
focusedChangedPublisher EventPublisher
calcTextSizeInfo2TextSize map[calcTextSizeInfo]Size // in native pixels
suspended bool
visible bool
enabled bool
acc *Accessibility
}
var (
registeredWindowClasses = make(map[string]bool)
defaultWndProcPtr uintptr
hwnd2WindowBase = make(map[win.HWND]*WindowBase)
)
func init() {
AppendToWalkInit(func() {
forEachDescendantCallbackPtr = syscall.NewCallback(forEachDescendant)
forEachDescendantRawCallbackPtr = syscall.NewCallback(forEachDescendantRaw)
dialogBaseUnitsUTF16StringPtr = syscall.StringToUTF16Ptr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
})
}
// MustRegisterWindowClass registers the specified window class.
//
// MustRegisterWindowClass must be called once for every window type that is not
// based on any system provided control, before calling InitChildWidget or
// InitWidget. Calling MustRegisterWindowClass twice with the same className
// results in a panic.
func MustRegisterWindowClass(className string) {
MustRegisterWindowClassWithWndProcPtr(className, defaultWndProcPtr)
}
func MustRegisterWindowClassWithStyle(className string, style uint32) {
MustRegisterWindowClassWithWndProcPtrAndStyle(className, defaultWndProcPtr, style)
}
func MustRegisterWindowClassWithWndProcPtr(className string, wndProcPtr uintptr) {
MustRegisterWindowClassWithWndProcPtrAndStyle(className, wndProcPtr, 0)
}
func MustRegisterWindowClassWithWndProcPtrAndStyle(className string, wndProcPtr uintptr, style uint32) {
if registeredWindowClasses[className] {
panic("window class already registered")
}
hInst := win.GetModuleHandle(nil)
if hInst == 0 {
panic("GetModuleHandle")
}
hIcon := win.LoadIcon(hInst, win.MAKEINTRESOURCE(7)) // rsrc uses 7 for app icon
if hIcon == 0 {
hIcon = win.LoadIcon(0, win.MAKEINTRESOURCE(win.IDI_APPLICATION))
}
if hIcon == 0 {
panic("LoadIcon")
}
hCursor := win.LoadCursor(0, win.MAKEINTRESOURCE(win.IDC_ARROW))
if hCursor == 0 {
panic("LoadCursor")
}
var wc win.WNDCLASSEX
wc.CbSize = uint32(unsafe.Sizeof(wc))
wc.LpfnWndProc = wndProcPtr
wc.HInstance = hInst
wc.HIcon = hIcon
wc.HCursor = hCursor
wc.HbrBackground = win.COLOR_BTNFACE + 1
wc.LpszClassName = syscall.StringToUTF16Ptr(className)
wc.Style = style
if atom := win.RegisterClassEx(&wc); atom == 0 {
panic("RegisterClassEx")
}
registeredWindowClasses[className] = true
}
var initedWalk uint32
var walkInit []func()
func AppendToWalkInit(fn func()) {
walkInit = append(walkInit, fn)
}
type windowCfg struct {
Window Window
Parent Window
ClassName string
Style uint32
ExStyle uint32
Bounds Rectangle
}
// InitWindow initializes a window.
//
// Widgets should be initialized using InitWidget instead.
func InitWindow(window, parent Window, className string, style, exStyle uint32) error {
return initWindowWithCfg(&windowCfg{
Window: window,
Parent: parent,
ClassName: className,
Style: style,
ExStyle: exStyle,
})
}
func initWindowWithCfg(cfg *windowCfg) error {
// We can't use sync.Once, because tooltip.go's init also calls InitWindow, so we deadlock.
if atomic.CompareAndSwapUint32(&initedWalk, 0, 1) {
runtime.LockOSThread()
var initCtrls win.INITCOMMONCONTROLSEX
initCtrls.DwSize = uint32(unsafe.Sizeof(initCtrls))
initCtrls.DwICC = win.ICC_LINK_CLASS | win.ICC_LISTVIEW_CLASSES | win.ICC_PROGRESS_CLASS | win.ICC_TAB_CLASSES | win.ICC_TREEVIEW_CLASSES
win.InitCommonControlsEx(&initCtrls)
defaultWndProcPtr = syscall.NewCallback(defaultWndProc)
for _, fn := range walkInit {
fn()
}
}
wb := cfg.Window.AsWindowBase()
wb.window = cfg.Window
wb.enabled = true
wb.visible = cfg.Style&win.WS_VISIBLE != 0
wb.calcTextSizeInfo2TextSize = make(map[calcTextSizeInfo]Size)
wb.name2Property = make(map[string]Property)
var hwndParent win.HWND
var hMenu win.HMENU
if cfg.Parent != nil {
hwndParent = cfg.Parent.Handle()
if widget, ok := cfg.Window.(Widget); ok {
if container, ok := cfg.Parent.(Container); ok {
if cb := container.AsContainerBase(); cb != nil {
hMenu = win.HMENU(cb.NextChildID())
}
widget.AsWidgetBase().parent = container
}
}
}
var windowName *uint16
if len(wb.name) != 0 {
windowName = syscall.StringToUTF16Ptr(wb.name)
}
if hwnd := cfg.Window.Handle(); hwnd == 0 {
var x, y, w, h int32
if cfg.Bounds.IsZero() {
x = win.CW_USEDEFAULT
y = win.CW_USEDEFAULT
w = win.CW_USEDEFAULT
h = win.CW_USEDEFAULT
} else {
x = int32(cfg.Bounds.X)
y = int32(cfg.Bounds.Y)
w = int32(cfg.Bounds.Width)
h = int32(cfg.Bounds.Height)
}
wb.hWnd = win.CreateWindowEx(
cfg.ExStyle,
syscall.StringToUTF16Ptr(cfg.ClassName),
windowName,
cfg.Style|win.WS_CLIPSIBLINGS,
x,
y,
w,
h,
hwndParent,
hMenu,
0,
nil)
if wb.hWnd == 0 {
return lastError("CreateWindowEx")
}
} else {
wb.hWnd = hwnd
}
// Handles returned by CreateWindowEx can only be used by the calling
// thread. As a result, InitWindow *must* be called from a goroutine that
// has been locked to an OS thread via runtime.LockOSThread().
//
// This means we can ask the OS for the ID of the current thread and we
// don't have to worry about the scheduler moving us onto another thread
// later.
tid := win.GetCurrentThreadId()
// Use the thread ID to look up our window group, which stores data that
// is common to all windows on a common thread. A group will be created
// if one doesn't already exist for the thread ID.
//
// CreateGroup automatically increments the reference counter for the
// group. The counter will be decremented later in WindowBase.Dispose.
wb.group = wgm.CreateGroup(tid)
succeeded := false
defer func() {
if !succeeded {
wb.Dispose()
}
}()
hwnd2WindowBase[wb.hWnd] = wb
if !registeredWindowClasses[cfg.ClassName] {
// We subclass all windows of system classes.
wb.origWndProcPtr = win.SetWindowLongPtr(wb.hWnd, win.GWLP_WNDPROC, defaultWndProcPtr)
if wb.origWndProcPtr == 0 {
return lastError("SetWindowLongPtr")
}
}
SetWindowFont(wb.hWnd, defaultFont)
if form, ok := cfg.Window.(Form); ok {
if fb := form.AsFormBase(); fb != nil {
if err := fb.init(form); err != nil {
return err
}
}
}
if widget, ok := cfg.Window.(Widget); ok {
if wb := widget.AsWidgetBase(); wb != nil {
if err := wb.init(widget); err != nil {
return err
}
}
}
wb.enabledProperty = NewBoolProperty(
func() bool {
return wb.window.Enabled()
},
func(b bool) error {
wb.window.SetEnabled(b)
return nil
},
wb.enabledChangedPublisher.Event())
wb.visibleProperty = NewBoolProperty(
func() bool {
return wb.visible
},
func(b bool) error {
wb.window.SetVisible(b)
return nil
},
wb.visibleChangedPublisher.Event())
wb.focusedProperty = NewReadOnlyBoolProperty(
func() bool {
return wb.window.Focused()
},
wb.focusedChangedPublisher.Event())
wb.MustRegisterProperty("Enabled", wb.enabledProperty)
wb.MustRegisterProperty("Visible", wb.visibleProperty)
wb.MustRegisterProperty("Focused", wb.focusedProperty)
succeeded = true
return nil
}
// InitWrapperWindow initializes a window that wraps (embeds) another window.
//
// Calling this method is necessary, if you want to be able to override the
// WndProc method of the embedded window. The embedded window should only be
// used as inseparable part of the wrapper window to avoid undefined behavior.
func InitWrapperWindow(window Window) error {
wb := window.AsWindowBase()
wb.window = window
if container, ok := window.(Container); ok {
children := container.Children()
if wlo, ok := window.(widgetListObserver); ok {
children.observer = wlo
}
for _, child := range children.items {
child.parent = container
}
}
return nil
}
func (wb *WindowBase) MustRegisterProperty(name string, property Property) {
if property == nil {
panic("property must not be nil")
}
if wb.name2Property[name] != nil {
panic("property already registered")
}
wb.name2Property[name] = property
}
func (wb *WindowBase) Property(name string) Property {
return wb.name2Property[name]
}
func (wb *WindowBase) hasStyleBits(bits uint32) bool {
return hasWindowLongBits(wb.hWnd, win.GWL_STYLE, bits)
}
func (wb *WindowBase) hasExtendedStyleBits(bits uint32) bool {
return hasWindowLongBits(wb.hWnd, win.GWL_EXSTYLE, bits)
}
func hasWindowLongBits(hwnd win.HWND, index int32, bits uint32) bool {
value := uint32(win.GetWindowLong(hwnd, index))
return value&bits == bits
}
func (wb *WindowBase) setAndClearStyleBits(set, clear uint32) error {
return setAndClearWindowLongBits(wb.hWnd, win.GWL_STYLE, set, clear)
}
func (wb *WindowBase) setAndClearExtendedStyleBits(set, clear uint32) error {
return setAndClearWindowLongBits(wb.hWnd, win.GWL_EXSTYLE, set, clear)
}
func setAndClearWindowLongBits(hwnd win.HWND, index int32, set, clear uint32) error {
value := uint32(win.GetWindowLong(hwnd, index))
if value == 0 {
return lastError("GetWindowLong")
}
if newValue := value&^clear | set; newValue != value {
win.SetLastError(0)
if win.SetWindowLong(hwnd, index, int32(newValue)) == 0 {
return lastError("SetWindowLong")
}
}
return nil
}
func (wb *WindowBase) ensureStyleBits(bits uint32, set bool) error {
return ensureWindowLongBits(wb.hWnd, win.GWL_STYLE, bits, set)
}
func (wb *WindowBase) ensureExtendedStyleBits(bits uint32, set bool) error {
return ensureWindowLongBits(wb.hWnd, win.GWL_EXSTYLE, bits, set)
}
func ensureWindowLongBits(hwnd win.HWND, index int32, bits uint32, set bool) error {
var setBits uint32
var clearBits uint32
if set {
setBits = bits
} else {
clearBits = bits
}
return setAndClearWindowLongBits(hwnd, index, setBits, clearBits)
}
// Accessibility returns the accessibility object used to set Dynamic Annotation properties of the
// window.
func (wb *WindowBase) Accessibility() *Accessibility {
if wb.acc == nil {
wb.acc = &Accessibility{wb: wb}
}
return wb.acc
}
// Handle returns the window handle of the Window.
func (wb *WindowBase) Handle() win.HWND {
return wb.hWnd
}
// SendMessage sends a message to the window and returns the result.
func (wb *WindowBase) SendMessage(msg uint32, wParam, lParam uintptr) uintptr {
return win.SendMessage(wb.hWnd, msg, wParam, lParam)
}
// Name returns the name of the *WindowBase.
func (wb *WindowBase) Name() string {
return wb.name
}
// SetName sets the name of the *WindowBase.
func (wb *WindowBase) SetName(name string) {
wb.name = name
}
func (wb *WindowBase) writePath(buf *bytes.Buffer) {
hWndParent := win.GetAncestor(wb.hWnd, win.GA_PARENT)
if pwi := windowFromHandle(hWndParent); pwi != nil {
if sv, ok := pwi.(*ScrollView); ok {
pwi = sv.Parent()
}
pwi.AsWindowBase().writePath(buf)
buf.WriteByte('/')
}
buf.WriteString(wb.name)
}
func (wb *WindowBase) path() string {
buf := bytes.NewBuffer(nil)
wb.writePath(buf)
return buf.String()
}
// WindowBase simply returns the receiver.
func (wb *WindowBase) AsWindowBase() *WindowBase {
return wb
}
// AddDisposable adds a Disposable resource that should be disposed of
// together with this Window.
func (wb *WindowBase) AddDisposable(d Disposable) {
wb.disposables = append(wb.disposables, d)
}
// Dispose releases the operating system resources, associated with the
// *WindowBase.
//
// If a user closes a *MainWindow or *Dialog, it is automatically released.
// Also, if a Container is disposed of, all its descendants will be released
// as well.
func (wb *WindowBase) Dispose() {
for _, d := range wb.disposables {
d.Dispose()
}
if wb.background != nil {
wb.background.detachWindow(wb)
}
hWnd := wb.hWnd
if hWnd != 0 {
wb.disposingPublisher.Publish()
wb.hWnd = 0
if _, ok := hwnd2WindowBase[hWnd]; ok {
win.DestroyWindow(hWnd)
}
}
if cm := wb.contextMenu; cm != nil {
cm.actions.Clear()
cm.Dispose()
}
if wb.shortcutActions != nil {
wb.shortcutActions.Clear()
}
for _, p := range wb.name2Property {
p.SetSource(nil)
}
if hWnd != 0 {
wb.group.accClearHwndProps(wb.hWnd)
wb.group.Done()
}
}
// Disposing returns an Event that is published when the Window is disposed
// of.
func (wb *WindowBase) Disposing() *Event {
return wb.disposingPublisher.Event()
}
// IsDisposed returns if the *WindowBase has been disposed of.
func (wb *WindowBase) IsDisposed() bool {
return wb.hWnd == 0
}
// ContextMenu returns the context menu of the *WindowBase.
//
// By default this is nil.
func (wb *WindowBase) ContextMenu() *Menu {
return wb.contextMenu
}
// SetContextMenu sets the context menu of the *WindowBase.
func (wb *WindowBase) SetContextMenu(value *Menu) {
wb.contextMenu = value
}
// ContextMenuLocation returns the the *WindowBase center in screen coordinates in native pixels.
func (wb *WindowBase) ContextMenuLocation() Point {
var rc win.RECT
if !win.GetWindowRect(wb.hWnd, &rc) {
return Point{}
}
return Point{int(rc.Left+rc.Right) / 2, int(rc.Top+rc.Bottom) / 2}
}
// ShortcutActions returns the list of actions that will be triggered if their
// shortcut is pressed when this window or one of its descendants has the
// keyboard focus.
func (wb *WindowBase) ShortcutActions() *ActionList {
if wb.shortcutActions == nil {
wb.shortcutActions = newActionList(wb)
}
return wb.shortcutActions
}
// Background returns the background Brush of the *WindowBase.
//
// By default this is nil.
func (wb *WindowBase) Background() Brush {
return wb.background
}
// SetBackground sets the background Brush of the *WindowBase.
func (wb *WindowBase) SetBackground(background Brush) {
if wb.background != nil {
wb.background.detachWindow(wb)
}
wb.background = background
if background != nil {
background.attachWindow(wb)
}
wb.Invalidate()
// Sliders need some extra encouragement...
walkDescendants(wb, func(w Window) bool {
if s, ok := w.(*Slider); ok {
s.SetRange(s.MinValue(), s.MaxValue()+1)
s.SetRange(s.MinValue(), s.MaxValue()-1)
}
return true
})
}
// Cursor returns the Cursor of the *WindowBase.
//
// By default this is nil.
func (wb *WindowBase) Cursor() Cursor {
return wb.cursor
}
// SetCursor sets the Cursor of the *WindowBase.
func (wb *WindowBase) SetCursor(value Cursor) {
wb.cursor = value
}
// DoubleBuffering returns whether double buffering of the