summaryrefslogtreecommitdiffstatshomepage
path: root/window.go
blob: b15b79a181a432ece67360ea5a2a4acb2d9aaf04 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
// 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"
	"strings"
	"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

	// 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

	// 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

	// ContextMenu returns the context menu of the Window.
	//
	// By default this is nil.
	ContextMenu() *Menu

	// CreateCanvas creates and returns a *Canvas that can be used to draw
	// inside the ClientBounds 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

	// 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

	// Handle returns the window handle of the Window.
	Handle() win.HWND

	// Height returns the outer height of the Window, including decorations.
	Height() 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

	// 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

	// 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

	// 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

	// SetClientSize sets the Size of the inner bounding box of the Window,
	// excluding decorations.
	SetClientSize(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

	// 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

	// 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

	// 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

	// 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

	// 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

	// Size returns the outer Size of the Window, including decorations.
	Size() 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

	// 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

	// 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
}

type calcTextSizeInfo struct {
	width int
	font  fontInfo
	text  string
	size  Size
	dpi   int
}

// WindowBase implements many operations common to all Windows.
type WindowBase struct {
	window                  Window
	hWnd                    win.HWND
	origWndProcPtr          uintptr
	name                    string
	font                    *Font
	contextMenu             *Menu
	disposables             []Disposable
	disposingPublisher      EventPublisher
	dropFilesPublisher      DropFilesEventPublisher
	keyDownPublisher        KeyEventPublisher
	keyPressPublisher       KeyEventPublisher
	keyUpPublisher          KeyEventPublisher
	mouseDownPublisher      MouseEventPublisher
	mouseUpPublisher        MouseEventPublisher
	mouseMovePublisher      MouseEventPublisher
	mouseWheelPublisher     MouseEventPublisher
	boundsChangedPublisher  EventPublisher
	sizeChangedPublisher    EventPublisher
	maxSize                 Size
	minSize                 Size
	background              Brush
	cursor                  Cursor
	suspended               bool
	visible                 bool
	enabled                 bool
	name2Property           map[string]Property
	enabledProperty         Property
	enabledChangedPublisher EventPublisher
	visibleProperty         Property
	visibleChangedPublisher EventPublisher
	focusedProperty         Property
	focusedChangedPublisher EventPublisher
	calcTextSizeInfoPrev    *calcTextSizeInfo
}

var (
	registeredWindowClasses = make(map[string]bool)
	defaultWndProcPtr       = syscall.NewCallback(defaultWndProc)
	hwnd2WindowBase         = make(map[win.HWND]*WindowBase)
)

// 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
}

// InitWindow initializes a window.
//
// Widgets should be initialized using InitWidget instead.
func InitWindow(window, parent Window, className string, style, exStyle uint32) error {
	wb := window.AsWindowBase()
	wb.window = window
	wb.enabled = true
	wb.visible = true

	wb.name2Property = make(map[string]Property)

	var hwndParent win.HWND
	if parent != nil {
		hwndParent = parent.Handle()

		if widget, ok := window.(Widget); ok {
			if container, ok := parent.(Container); ok {
				widget.AsWidgetBase().parent = container
			}
		}
	}

	var windowName *uint16
	if len(wb.name) != 0 {
		windowName = syscall.StringToUTF16Ptr(wb.name)
	}

	if hwnd := window.Handle(); hwnd == 0 {
		wb.hWnd = win.CreateWindowEx(
			exStyle,
			syscall.StringToUTF16Ptr(className),
			windowName,
			style|win.WS_CLIPSIBLINGS,
			win.CW_USEDEFAULT,
			win.CW_USEDEFAULT,
			win.CW_USEDEFAULT,
			win.CW_USEDEFAULT,
			hwndParent,
			0,
			0,
			nil)
		if wb.hWnd == 0 {
			return lastError("CreateWindowEx")
		}
	} else {
		wb.hWnd = hwnd
	}

	succeeded := false
	defer func() {
		if !succeeded {
			wb.Dispose()
		}
	}()

	hwnd2WindowBase[wb.hWnd] = wb

	if !registeredWindowClasses[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 := window.(Form); ok {
		if fb := form.AsFormBase(); fb != nil {
			if err := fb.init(form); err != nil {
				return err
			}
		}
	}

	if widget, ok := 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 window.AsWindowBase().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)
}

// 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()

		switch w := wb.window.(type) {
		case *ToolTip:
		case Widget:
			globalToolTip.RemoveTool(w)
		}

		wb.hWnd = 0
		if _, ok := hwnd2WindowBase[hWnd]; ok {
			win.DestroyWindow(hWnd)
		}
	}

	if cm := wb.contextMenu; cm != nil {
		cm.actions.Clear()
		cm.Dispose()
	}

	for _, p := range wb.name2Property {
		p.SetSource(nil)
	}
}

// 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
}

// 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
}

// SetDoubleBuffering enables or disables double buffering of the
// drawing, which may help reduce flicker.
func (wb *WindowBase) SetDoubleBuffering(enabled bool) error {
	return wb.ensureExtendedStyleBits(win.WS_EX_COMPOSITED, enabled)
}

// DPI returns the current DPI value of the WindowBase.
func (wb *WindowBase) DPI() int {
	return int(win.GetDpiForWindow(wb.hWnd))
}

func (wb *WindowBase) intFrom96DPI(value int) int {
	return wb.scaleInt(value, float64(wb.DPI()) / 96.0)
}

func (wb *WindowBase) intTo96DPI(value int) int {
	return wb.scaleInt(value, 96.0 / float64(wb.DPI()))
}

func (wb *WindowBase) scaleInt(value int, scale float64) int {
	return int(float64(value) * scale)
}

func (wb *WindowBase) marginsFrom96DPI(value Margins) Margins {
	return wb.scaleMargins(value, float64(wb.DPI()) / 96.0)
}

func (wb *WindowBase) marginsTo96DPI(value Margins) Margins {
	return wb.scaleMargins(value, 96.0 / float64(wb.DPI()))
}

func (wb *WindowBase) scaleMargins(value Margins, scale float64) Margins {
	return Margins{
		HNear: int(float64(value.HNear) * scale),
		VNear: int(float64(value.VNear) * scale),
		HFar: int(float64(value.HFar) * scale),
		VFar: int(float64(value.VFar) * scale),
	}
}

func (wb *WindowBase) pointFrom96DPI(value Point) Point {
	return wb.scalePoint(value, float64(wb.DPI()) / 96.0)
}

func (wb *WindowBase) pointTo96DPI(value Point) Point {
	return wb.scalePoint(value, 96.0 / float64(wb.DPI()))
}

func (wb *WindowBase) scalePoint(value Point, scale float64) Point {
	return Point{
		X: int(float64(value.X) * scale),
		Y: int(float64(value.Y) * scale),
	}
}

func (wb *WindowBase) rectangleFrom96DPI(value Rectangle) Rectangle {
	return wb.scaleRectangle(value, float64(wb.DPI()) / 96.0)
}

func (wb *WindowBase) rectangleTo96DPI(value Rectangle) Rectangle {
	return wb.scaleRectangle(value, 96.0 / float64(wb.DPI()))
}

func (wb *WindowBase) scaleRectangle(value Rectangle, scale float64) Rectangle {
	return Rectangle{
		X: int(float64(value.X) * scale),
		Y: int(float64(value.Y) * scale),
		Width: int(float64(value.Width) * scale),
		Height: int(float64(value.Height) * scale),
	}
}

func (wb *WindowBase) sizeFrom96DPI(value Size) Size {
	return wb.scaleSize(value, float64(wb.DPI()) / 96.0)
}

func (wb *WindowBase) sizeTo96DPI(value Size) Size {
	return wb.scaleSize(value, 96.0 / float64(wb.DPI()))
}

func (wb *WindowBase) scaleSize(value Size, scale float64) Size {
	return Size{
		Width: int(float64(value.Width) * scale),
		Height: int(float64(value.Height) * scale),
	}
}

// Enabled returns if the *WindowBase is enabled for user interaction.
func (wb *WindowBase) Enabled() bool {
	return wb.enabled
}

// SetEnabled sets if the *WindowBase is enabled for user interaction.
func (wb *WindowBase) SetEnabled(enabled bool) {
	wb.enabled = enabled

	wb.window.(applyEnableder).applyEnabled(wb.window.Enabled())

	if widget, ok := wb.window.(Widget); ok {
		widget.AsWidgetBase().invalidateBorderInParent()
	}

	wb.enabledChangedPublisher.Publish()
}

type applyEnableder interface {
	applyEnabled(enabled bool)
}

func (wb *WindowBase) applyEnabled(enabled bool) {
	setWindowEnabled(wb.hWnd, enabled)
}

func setWindowEnabled(hwnd win.HWND, enabled bool) {
	win.EnableWindow(hwnd, enabled)

	win.UpdateWindow(hwnd)
}

// Font returns the *Font of the *WindowBase.
//
// By default this is a MS Shell Dlg 2, 8 point font.
func (wb *WindowBase) Font() *Font {
	if wb.font != nil {
		return wb.font
	}

	return defaultFont
}

// SetFont sets the *Font of the *WindowBase.
func (wb *WindowBase) SetFont(font *Font) {
	if font != wb.font {
		wb.font = font

		wb.window.(applyFonter).applyFont(font)
	}
}

type applyFonter interface {
	applyFont(font *Font)
}

type ApplyFonter interface {
	ApplyFont(font *Font)
}

func (wb *WindowBase) applyFont(font *Font) {
	setWindowFont(wb.hWnd, font)

	if af, ok := wb.window.(ApplyFonter); ok {
		af.ApplyFont(font)
	}
}

func SetWindowFont(hwnd win.HWND, font *Font) {
	setWindowFont(hwnd, font)
}

func setWindowFont(hwnd win.HWND, font *Font) {
	dpi := int(win.GetDpiForWindow(hwnd))

	win.SendMessage(hwnd, win.WM_SETFONT, uintptr(font.handleForDPI(dpi)), 1)

	if window := windowFromHandle(hwnd); window != nil {
		if widget, ok := window.(Widget); ok {
			widget.AsWidgetBase().updateParentLayoutWithReset(false)
		}
	}
}

// Suspended returns if the *WindowBase is suspended for layout and repainting
// purposes.
func (wb *WindowBase) Suspended() bool {
	return wb.suspended
}

// SetSuspended sets if the *WindowBase 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 *WindowBase
// accordingly.
func (wb *WindowBase) SetSuspended(suspend bool) {
	if suspend == wb.suspended {
		return
	}

	var wParam int
	if suspend {
		wParam = 0
	} else {
		wParam = 1
	}

	wb.SendMessage(win.WM_SETREDRAW, uintptr(wParam), 0)

	wb.suspended = suspend

	if !suspend {
		wb.Invalidate()
	}
}

// Invalidate schedules a full repaint of the *WindowBase.
func (wb *WindowBase) Invalidate() error {
	if !win.InvalidateRect(wb.hWnd, nil, true) {
		return newError("InvalidateRect failed")
	}

	return nil
}

func (wb *WindowBase) text() string {
	return windowText(wb.hWnd)
}

func (wb *WindowBase) setText(text string) error {
	if err := setWindowText(wb.hWnd, text); err != nil {
		return err
	}

	if wb.calcTextSizeInfoPrev != nil {
		wb.calcTextSizeInfoPrev.font.family = ""
		wb.calcTextSizeInfoPrev.text = text
	}

	return nil
}

func windowText(hwnd win.HWND) string {
	textLength := win.SendMessage(hwnd, win.WM_GETTEXTLENGTH, 0, 0)
	buf := make([]uint16, textLength+1)
	win.SendMessage(hwnd, win.WM_GETTEXT, uintptr(textLength+1), uintptr(unsafe.Pointer(&buf[0])))
	return syscall.UTF16ToString(buf)
}

func setWindowText(hwnd win.HWND, text string) error {
	if win.TRUE != win.SendMessage(hwnd, win.WM_SETTEXT, 0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text)))) {
		return newError("WM_SETTEXT failed")
	}

	return nil
}

func (wb *WindowBase) RestoreState() (err error) {
	wb.ForEachDescendant(func(widget Widget) bool {
		if persistable, ok := widget.(Persistable); ok && persistable.Persistent() {
			if err = persistable.RestoreState(); err != nil {
				return false
			}
		}

		if _, ok := widget.(Container); ok {
			return false
		}

		return true
	})

	return
}

func (wb *WindowBase) SaveState() (err error) {
	wb.ForEachDescendant(func(widget Widget) bool {
		if persistable, ok := widget.(Persistable); ok && persistable.Persistent() {
			if err = persistable.SaveState(); err != nil {
				return false
			}
		}

		if _, ok := widget.(Container); ok {
			return false
		}

		return true
	})

	return
}

func forEachDescendant(hwnd win.HWND, lParam uintptr) uintptr {
	if window := windowFromHandle(hwnd); window != nil && forEachDescendantCallback(window.(Widget)) {
		return 1
	}

	return 0
}

var (
	forEachDescendantCallbackPtr = syscall.NewCallback(forEachDescendant)
	forEachDescendantCallback    func(widget Widget) bool
)

func (wb *WindowBase) ForEachDescendant(f func(widget Widget) bool) {
	forEachDescendantCallback = f
	defer func() {
		forEachDescendantCallback = nil
	}()

	win.EnumChildWindows(wb.hWnd, forEachDescendantCallbackPtr, 0)
}

// Visible returns if the *WindowBase is visible.
func (wb *WindowBase) Visible() bool {
	return win.IsWindowVisible(wb.hWnd)
}

// SetVisible sets if the *WindowBase is visible.
func (wb *WindowBase) SetVisible(visible bool) {
	old := wb.Visible()

	setWindowVisible(wb.hWnd, visible)

	wb.visible = visible

	walkDescendants(wb.window, func(w Window) bool {
		w.AsWindowBase().visibleChangedPublisher.Publish()

		return true
	})

	if visible == old {
		return
	}

	if widget, ok := wb.window.(Widget); ok {
		wb := widget.AsWidgetBase()
		wb.invalidateBorderInParent()
		wb.updateParentLayoutWithReset(true)
	}

	wb.visibleChangedPublisher.Publish()
}

// VisibleChanged returns an Event that you can attach to for handling
// visible changed events for the Window.
func (wb *WindowBase) VisibleChanged() *Event {
	return wb.visibleChangedPublisher.Event()
}

func setWindowVisible(hwnd win.HWND, visible bool) {
	var cmd int32
	if visible {
		cmd = win.SW_SHOWNA
	} else {
		cmd = win.SW_HIDE
	}
	win.ShowWindow(hwnd, cmd)
}

// BringToTop moves the *WindowBase to the top of the keyboard focus order.
func (wb *WindowBase) BringToTop() error {
	if !win.SetWindowPos(wb.hWnd, win.HWND_TOP, 0, 0, 0, 0, win.SWP_NOACTIVATE|win.SWP_NOMOVE|win.SWP_NOSIZE) {
		return lastError("SetWindowPos")
	}

	return nil
}

// Bounds returns the outer bounding box Rectangle of the *WindowBase, including
// decorations.
//
// The coordinates are relative to the screen.
func (wb *WindowBase) Bounds() Rectangle {
	var r win.RECT

	if !win.GetWindowRect(wb.hWnd, &r) {
		lastError("GetWindowRect")
		return Rectangle{}
	}

	return Rectangle{
		int(r.Left),
		int(r.Top),
		int(r.Right - r.Left),
		int(r.Bottom - r.Top),
	}
}

// SetBounds sets the outer bounding box Rectangle of the *WindowBase,
// 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.
func (wb *WindowBase) SetBounds(bounds Rectangle) error {
	if !win.MoveWindow(
		wb.hWnd,
		int32(bounds.X),
		int32(bounds.Y),
		int32(bounds.Width),
		int32(bounds.Height),
		true) {

		return lastError("MoveWindow")
	}

	return nil
}

// MinSize returns the minimum allowed outer Size for the *WindowBase, including
// decorations.
//
// For child windows, this is only relevant when the parent of the *WindowBase
// has a Layout. RootWidgets, like *MainWindow and *Dialog, also honor this.
func (wb *WindowBase) MinSize() Size {
	return wb.minSize
}

// MaxSize returns the maximum allowed outer Size for the *WindowBase, including
// decorations.
//
// For child windows, this is only relevant when the parent of the *WindowBase
// has a Layout. RootWidgets, like *MainWindow and *Dialog, also honor this.
func (wb *WindowBase) MaxSize() Size {
	return wb.maxSize
}

// SetMinMaxSize sets the minimum and maximum outer Size of the *WindowBase,
// including decorations.
//
// Use walk.Size{} to make the respective limit be ignored.
func (wb *WindowBase) SetMinMaxSize(min, max Size) error {
	if min.Width < 0 || min.Height < 0 {
		return newError("min must be positive")
	}
	if max.Width > 0 && max.Width < min.Width ||
		max.Height > 0 && max.Height < min.Height {
		return newError("max must be greater as or equal to min")
	}

	wb.minSize = min
	wb.maxSize = max

	return nil
}

type fontInfoAndDPI struct {
	fontInfo
	dpi int
}

var (
	dialogBaseUnitsUTF16StringPtr = syscall.StringToUTF16Ptr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
	fontInfoAndDPI2DialogBaseUnits      = make(map[fontInfoAndDPI]Size)
)

func (wb *WindowBase) dialogBaseUnits() Size {
	// The window may use a font different from that in WindowBase,
	// like e.g. NumberEdit does, so we try to use the right one.
	font := wb.window.Font()
	fi := fontInfoAndDPI{
		fontInfo: fontInfo{
			family: font.Family(),
			pointSize: font.PointSize(), 
			style: font.Style(), 
		},
		dpi: wb.DPI()}
	if s, ok := fontInfoAndDPI2DialogBaseUnits[fi]; ok {
		return s
	}

	hdc := win.GetDC(wb.hWnd)
	defer win.ReleaseDC(wb.hWnd, hdc)

	hFont := font.handleForDPI(wb.DPI())
	hFontOld := win.SelectObject(hdc, win.HGDIOBJ(hFont))
	defer win.SelectObject(hdc, win.HGDIOBJ(hFontOld))

	var tm win.TEXTMETRIC
	if !win.GetTextMetrics(hdc, &tm) {
		newError("GetTextMetrics failed")
	}

	var size win.SIZE
	if !win.GetTextExtentPoint32(
		hdc,
		dialogBaseUnitsUTF16StringPtr,
		52,
		&size) {
		newError("GetTextExtentPoint32 failed")
	}

	s := Size{int((size.CX/26 + 1) / 2), int(tm.TmHeight)}

	fontInfoAndDPI2DialogBaseUnits[fi] = s

	return s
}

func (wb *WindowBase) dialogBaseUnitsToPixels(dlus Size) (pixels Size) {
	base := wb.dialogBaseUnits()

	return Size{
		int(win.MulDiv(int32(dlus.Width), int32(base.Width), 4)),
		int(win.MulDiv(int32(dlus.Height), int32(base.Height), 8)),
	}
}

func (wb *WindowBase) calculateTextSizeImpl(text string) Size {
	return wb.calculateTextSizeImplForWidth(text, 0)
}

func (wb *WindowBase) calculateTextSizeImplForWidth(text string, width int) Size {
	font := wb.window.Font()

	dpi := wb.DPI()

	if wb.calcTextSizeInfoPrev != nil &&
		width == wb.calcTextSizeInfoPrev.width &&
		font.family == wb.calcTextSizeInfoPrev.font.family &&
		font.pointSize == wb.calcTextSizeInfoPrev.font.pointSize &&
		font.style == wb.calcTextSizeInfoPrev.font.style &&
		text == wb.calcTextSizeInfoPrev.text &&
		dpi == wb.calcTextSizeInfoPrev.dpi {
		return wb.calcTextSizeInfoPrev.size
	}

	if wb.calcTextSizeInfoPrev != nil && dpi != wb.calcTextSizeInfoPrev.dpi {
		width = int(float64(width) * float64(dpi) / float64(wb.calcTextSizeInfoPrev.dpi))
	}

	var size Size
	if width > 0 {
		canvas, err := wb.CreateCanvas()
		if err != nil {
			return size
		}
		defer canvas.Dispose()

		bounds, _, err := canvas.MeasureText(text, font, Rectangle{Width: width, Height: 9999999}, 0)
		if err != nil {
			return size
		}

		size = bounds.Size()
	} else {
		hdc := win.GetDC(wb.hWnd)
		if hdc == 0 {
			newError("GetDC failed")
			return Size{}
		}
		defer win.ReleaseDC(wb.hWnd, hdc)

		hFontOld := win.SelectObject(hdc, win.HGDIOBJ(font.handleForDPI(dpi)))
		defer win.SelectObject(hdc, hFontOld)

		lines := strings.Split(text, "\n")

		for _, line := range lines {
			var s win.SIZE
			str := syscall.StringToUTF16(strings.TrimRight(line, "\r "))

			if !win.GetTextExtentPoint32(hdc, &str[0], int32(len(str)-1), &s) {
				newError("GetTextExtentPoint32 failed")
				return Size{}
			}

			size.Width = maxi(size.Width, int(s.CX))
			size.Height += int(s.CY)
		}
	}

	if wb.calcTextSizeInfoPrev == nil {
		wb.calcTextSizeInfoPrev = new(calcTextSizeInfo)
	}

	wb.calcTextSizeInfoPrev.width = width
	wb.calcTextSizeInfoPrev.font.family = font.family
	wb.calcTextSizeInfoPrev.font.pointSize = font.pointSize
	wb.calcTextSizeInfoPrev.font.style = font.style
	wb.calcTextSizeInfoPrev.text = text
	wb.calcTextSizeInfoPrev.size = size
	wb.calcTextSizeInfoPrev.dpi = dpi

	return size
}

func (wb *WindowBase) calculateTextSize() Size {
	return wb.calculateTextSizeForWidth(0)
}

func (wb *WindowBase) calculateTextSizeForWidth(width int) Size {
	var text string
	if wb.calcTextSizeInfoPrev != nil {
		// setText copied the new text here for us.
		text = wb.calcTextSizeInfoPrev.text
	}

	if text == "" {
		text = wb.text()
	}

	return wb.calculateTextSizeImplForWidth(text, width)
}

// Size returns the outer Size of the *WindowBase, including decorations.
func (wb *WindowBase) Size() Size {
	return wb.window.Bounds().Size()
}

// SetSize sets the outer Size of the *WindowBase, including decorations.
func (wb *WindowBase) SetSize(size Size) error {
	bounds := wb.window.Bounds()

	return wb.SetBounds(bounds.SetSize(size))
}

// X returns the x coordinate of the *WindowBase, relative to the screen for
// RootWidgets like *MainWindow or *Dialog and relative to the parent for
// child Windows.
func (wb *WindowBase) X() int {
	return wb.window.Bounds().X
}

// SetX sets the x coordinate of the *WindowBase, relative to the screen for
// RootWidgets like *MainWindow or *Dialog and relative to the parent for
// child Windows.
func (wb *WindowBase) SetX(value int) error {
	bounds := wb.window.Bounds()
	bounds.X = value

	return wb.SetBounds(bounds)
}

// Y returns the y coordinate of the *WindowBase, relative to the screen for
// RootWidgets like *MainWindow or *Dialog and relative to the parent for
// child Windows.
func (wb *WindowBase) Y() int {
	return wb.window.Bounds().Y
}

// SetY sets the y coordinate of the *WindowBase, relative to the screen for
// RootWidgets like *MainWindow or *Dialog and relative to the parent for
// child Windows.
func (wb *WindowBase) SetY(value int) error {
	bounds := wb.window.Bounds()
	bounds.Y = value

	return wb.SetBounds(bounds)
}

// Width returns the outer width of the *WindowBase, including decorations.
func (wb *WindowBase) Width() int {
	return wb.window.Bounds().Width
}

// SetWidth sets the outer width of the *WindowBase, including decorations.
func (wb *WindowBase) SetWidth(value int) error {
	bounds := wb.window.Bounds()
	bounds.Width = value

	return wb.SetBounds(bounds)
}

// Height returns the outer height of the *WindowBase, including decorations.
func (wb *WindowBase) Height() int {
	return wb.window.Bounds().Height
}

// SetHeight sets the outer height of the *WindowBase, including decorations.
func (wb *WindowBase) SetHeight(value int) error {
	bounds := wb.window.Bounds()
	bounds.Height = value

	return wb.SetBounds(bounds)
}

func windowClientBounds(hwnd win.HWND) Rectangle {
	var r win.RECT

	if !win.GetClientRect(hwnd, &r) {
		lastError("GetClientRect")
		return Rectangle{}
	}

	return Rectangle{
		int(r.Left),
		int(r.Top),
		int(r.Right - r.Left),
		int(r.Bottom - r.Top),
	}
}

// ClientBounds returns the inner bounding box Rectangle of the *WindowBase,
// excluding decorations.
func (wb *WindowBase) ClientBounds() Rectangle {
	return windowClientBounds(wb.hWnd)
}

func (wb *WindowBase) sizeFromClientSize(clientSize Size) Size {
	window := wb.window
	s := window.Size()
	cs := window.ClientBounds().Size()
	ncs := Size{s.Width - cs.Width, s.Height - cs.Height}

	return Size{clientSize.Width + ncs.Width, clientSize.Height + ncs.Height}
}

func (wb *WindowBase) clientSizeFromSize(size Size) Size {
	window := wb.window
	s := window.Size()
	cs := window.ClientBounds().Size()
	ncs := Size{s.Width - cs.Width, s.Height - cs.Height}

	return Size{size.Width - ncs.Width, size.Height - ncs.Height}
}

// SetClientSize sets the Size of the inner bounding box of the *WindowBase,
// excluding decorations.
func (wb *WindowBase) SetClientSize(value Size) error {
	return wb.SetSize(wb.sizeFromClientSize(value))
}

// RightToLeftReading returns whether the reading order of the Window
// is from right to left.
func (wb *WindowBase) RightToLeftReading() bool {
	return wb.hasExtendedStyleBits(win.WS_EX_RTLREADING)
}

// SetRightToLeftReading sets whether the reading order of the Window
// is from right to left.
func (wb *WindowBase) SetRightToLeftReading(rtl bool) error {
	return wb.ensureExtendedStyleBits(win.WS_EX_RTLREADING, rtl)
}

// Screenshot returns an image of the window.
func (wb *WindowBase) Screenshot() (*image.RGBA, error) {
	bmp, err := NewBitmapFromWindow(wb)
	if err != nil {
		return nil, err
	}
	defer bmp.Dispose()

	return bmp.ToImage()
}

// FocusedWindow returns the Window that has the keyboard input focus.
func FocusedWindow() Window {
	return windowFromHandle(win.GetFocus())
}

// Focused returns whether the Window has the keyboard input focus.
func (wb *WindowBase) Focused() bool {
	return wb.hWnd == win.GetFocus()
}

// SetFocus sets the keyboard input focus to the *WindowBase.
func (wb *WindowBase) SetFocus() error {
	if win.SetFocus(wb.hWnd) == 0 {
		return lastError("SetFocus")
	}

	return nil
}

// FocusedChanged returns an Event that you can attach to for handling focus
// change events for the WindowBase.
func (wb *WindowBase) FocusedChanged() *Event {
	return wb.focusedChangedPublisher.Event()
}

// CreateCanvas creates and returns a *Canvas that can be used to draw
// inside the ClientBounds of the *WindowBase.
//
// Remember to call the Dispose method on the canvas to release resources,
// when you no longer need it.
func (wb *WindowBase) CreateCanvas() (*Canvas, error) {
	return newCanvasFromHWND(wb.hWnd)
}

func (wb *WindowBase) setTheme(appName string) error {
	if hr := win.SetWindowTheme(wb.hWnd, syscall.StringToUTF16Ptr(appName), nil); win.FAILED(hr) {
		return errorFromHRESULT("SetWindowTheme", hr)
	}

	return nil
}

// KeyDown returns a *KeyEvent that you can attach to for handling key down
// events for the *WindowBase.
func (wb *WindowBase) KeyDown() *KeyEvent {
	return wb.keyDownPublisher.Event()
}

// KeyPress returns a *KeyEvent that you can attach to for handling key press
// events for the *WindowBase.
func (wb *WindowBase) KeyPress() *KeyEvent {
	return wb.keyPressPublisher.Event()
}

// KeyUp returns a *KeyEvent that you can attach to for handling key up
// events for the *WindowBase.
func (wb *WindowBase) KeyUp() *KeyEvent {
	return wb.keyUpPublisher.Event()
}

// DropFiles returns a *DropFilesEvent that you can attach to for handling
// drop file events for the *WindowBase.
func (wb *WindowBase) DropFiles() *DropFilesEvent {
	return wb.dropFilesPublisher.Event(wb.hWnd)
}

// MouseDown returns a *MouseEvent that you can attach to for handling
// mouse down events for the *WindowBase.
func (wb *WindowBase) MouseDown() *MouseEvent {
	return wb.mouseDownPublisher.Event()
}

// MouseMove returns a *MouseEvent that you can attach to for handling
// mouse move events for the *WindowBase.
func (wb *WindowBase) MouseMove() *MouseEvent {
	return wb.mouseMovePublisher.Event()
}

// MouseUp returns a *MouseEvent that you can attach to for handling
// mouse up events for the *WindowBase.
func (wb *WindowBase) MouseUp() *MouseEvent {
	return wb.mouseUpPublisher.Event()
}

func (wb *WindowBase) MouseWheel() *MouseEvent {
	return wb.mouseWheelPublisher.Event()
}

func (wb *WindowBase) publishMouseEvent(publisher *MouseEventPublisher, msg uint32, wParam, lParam uintptr) {
	x := int(win.GET_X_LPARAM(lParam))
	y := int(win.GET_Y_LPARAM(lParam))

	var button MouseButton
	switch msg {
	case win.WM_LBUTTONUP:
		button = LeftButton

	case win.WM_RBUTTONUP:
		button = RightButton

	case win.WM_MBUTTONUP:
		button = MiddleButton

	default:
		button = MouseButton(wParam&win.MK_LBUTTON | wParam&win.MK_RBUTTON | wParam&win.MK_MBUTTON)
	}

	publisher.Publish(x, y, button)
}

func (wb *WindowBase) publishMouseWheelEvent(publisher *MouseEventPublisher, wParam, lParam uintptr) {
	x := int(win.GET_X_LPARAM(lParam))
	y := int(win.GET_Y_LPARAM(lParam))
	button := MouseButton(uint32(wParam))

	publisher.Publish(x, y, button)
}

// SizeChanged returns an *Event that you can attach to for handling size
// changed events for the *WindowBase.
func (wb *WindowBase) SizeChanged() *Event {
	return wb.sizeChangedPublisher.Event()
}

// BoundsChanged returns an *Event that you can attach to for handling bounds
// changed events for the *WindowBase.
func (wb *WindowBase) BoundsChanged() *Event {
	return wb.boundsChangedPublisher.Event()
}

// Synchronize enqueues func f to be called some time later by the main
// goroutine from inside a message loop.
func (wb *WindowBase) Synchronize(f func()) {
	synchronize(f)

	win.PostMessage(wb.hWnd, syncMsgId, 0, 0)
}

func (wb *WindowBase) ReadState() (string, error) {
	settings := appSingleton.settings
	if settings == nil {
		return "", newError("App().Settings() must not be nil")
	}

	state, _ := settings.Get(wb.path())
	return state, nil
}

func (wb *WindowBase) WriteState(state string) error {
	settings := appSingleton.settings
	if settings == nil {
		return newError("App().Settings() must not be nil")
	}

	p := wb.path()
	if strings.HasPrefix(p, "/") ||
		strings.HasSuffix(p, "/") ||
		strings.Contains(p, "//") {

		return nil
	}

	return settings.PutExpiring(p, state)
}

func windowFromHandle(hwnd win.HWND) Window {
	if wb := hwnd2WindowBase[hwnd]; wb != nil {
		return wb.window
	}

	return nil
}

func defaultWndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) (result uintptr) {
	defer func() {
		if len(appSingleton.panickingPublisher.event.handlers) > 0 {
			var err error
			if x := recover(); x != nil {
				if e, ok := x.(error); ok {
					err = wrapErrorNoPanic(e)
				} else {
					err = newErrorNoPanic(fmt.Sprint(x))
				}
			}
			if err != nil {
				appSingleton.panickingPublisher.Publish(err)
			}
		}
	}()

	if msg == notifyIconMessageId {
		return notifyIconWndProc(hwnd, msg, wParam, lParam)
	}

	wi := windowFromHandle(hwnd)
	if wi == nil {
		return win.DefWindowProc(hwnd, msg, wParam, lParam)
	}

	result = wi.WndProc(hwnd, msg, wParam, lParam)

	return
}

type menuer interface {
	Menu() *Menu
}

func menuContainsAction(menu *Menu, action *Action) bool {
	if menu.Actions().Contains(action) {
		return true
	}

	for _, a := range menu.actions.actions {
		if a.menu != nil && menuContainsAction(a.menu, action) {
			return true
		}
	}

	return false
}

func (wb *WindowBase) handleKeyDown(wParam, lParam uintptr) {
	key := Key(wParam)

	if uint32(lParam)>>30 == 0 {
		wb.keyDownPublisher.Publish(key)

		// Using TranslateAccelerators refused to work, so we handle them
		// ourselves, at least for now.
		shortcut := Shortcut{ModifiersDown(), key}
		if action, ok := shortcut2Action[shortcut]; ok {
			if action.Visible() && action.Enabled() {
				window := wb.window

				if w, ok := window.(Widget); ok {
					window = ancestor(w)
				}

				if m, ok := window.(menuer); ok && menuContainsAction(m.Menu(), action) {
					action.raiseTriggered()
				}
			}
		}
	}

	switch key {
	case KeyAlt, KeyControl, KeyShift:
		// nop

	default:
		wb.keyPressPublisher.Publish(key)
	}
}

func (wb *WindowBase) handleKeyUp(wParam, lParam uintptr) {
	wb.keyUpPublisher.Publish(Key(wParam))
}

func (wb *WindowBase) backgroundEffective() (Brush, Window) {
	wnd := wb.window
	bg := wnd.Background()

	if widget, ok := wb.window.(Widget); ok {
		for bg == nullBrushSingleton && widget != nil {
			if hwndParent := win.GetParent(widget.Handle()); hwndParent != 0 {
				if parent := windowFromHandle(hwndParent); parent != nil {
					wnd = parent
					bg = parent.Background()

					widget, _ = parent.(Widget)
				} else {
					break
				}
			} else {
				break
			}
		}
	}

	if bg != nil {
		if pwb, ok := bg.(perWindowBrush); ok {
			bg = pwb.delegateForWindow(wnd.AsWindowBase())
		}
	}

	return bg, wnd
}

func (wb *WindowBase) prepareDCForBackground(hdc win.HDC, hwnd win.HWND, brushWnd Window) {
	win.SetBkMode(hdc, win.TRANSPARENT)

	var bgRC win.RECT
	win.GetWindowRect(brushWnd.Handle(), &bgRC)

	var rc win.RECT
	win.GetWindowRect(hwnd, &rc)

	win.SetBrushOrgEx(hdc, bgRC.Left-rc.Left, bgRC.Top-rc.Top, nil)
}

func (wb *WindowBase) handleWMCTLCOLOR(wParam, lParam uintptr) uintptr {
	hwnd := win.HWND(lParam)
	hdc := win.HDC(wParam)

	type TextColorer interface {
		TextColor() Color
	}

	wnd := windowFromHandle(hwnd)
	if wnd == nil {
		switch windowFromHandle(win.GetParent(hwnd)).(type) {
		case *ComboBox:
			// nop
			return 0
		}

		wnd = wb
	} else if tc, ok := wnd.(TextColorer); ok {
		win.SetTextColor(hdc, win.COLORREF(tc.TextColor()))
	}

	if bg, wnd := wnd.AsWindowBase().backgroundEffective(); bg != nil {
		wb.prepareDCForBackground(hdc, hwnd, wnd)

		type Colorer interface {
			Color() Color
		}

		if c, ok := bg.(Colorer); ok {
			win.SetBkColor(hdc, win.COLORREF(c.Color()))
		}

		return uintptr(bg.handle())
	}

	switch wnd.(type) {
	case *LineEdit, *numberLineEdit, *TextEdit:
		type ReadOnlyer interface {
			ReadOnly() bool
		}

		var sysColor int
		if ro, ok := wnd.(ReadOnlyer); ok && ro.ReadOnly() {
			sysColor = win.COLOR_BTNFACE
		} else {
			sysColor = win.COLOR_WINDOW
		}

		win.SetBkColor(hdc, win.COLORREF(win.GetSysColor(sysColor)))

		return uintptr(win.GetSysColorBrush(sysColor))
	}

	return 0
}

// 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.
func (wb *WindowBase) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
	window := windowFromHandle(hwnd)

	switch msg {
	case win.WM_ERASEBKGND:
		if _, ok := window.(Widget); !ok {
			return 0
		}

		bg, wnd := wb.backgroundEffective()
		if bg == nil {
			break
		}

		hdc := win.HDC(wParam)

		canvas, err := newCanvasFromHDC(hdc)
		if err != nil {
			break
		}
		defer canvas.Dispose()

		wb.prepareDCForBackground(hdc, hwnd, wnd)

		if err := canvas.FillRectangle(bg, wb.ClientBounds()); err != nil {
			break
		}

		return 1

	case win.WM_HSCROLL, win.WM_VSCROLL:
		if window := windowFromHandle(win.HWND(lParam)); window != nil {
			// The window that sent the notification shall handle it itself.
			return window.WndProc(hwnd, msg, wParam, lParam)
		}

	case win.WM_LBUTTONDOWN, win.WM_MBUTTONDOWN, win.WM_RBUTTONDOWN:
		if msg == win.WM_LBUTTONDOWN && wb.origWndProcPtr == 0 {
			// Only call SetCapture if this is no subclassed control.
			// (Otherwise e.g. WM_COMMAND(BN_CLICKED) would no longer
			// be generated for PushButton.)
			win.SetCapture(wb.hWnd)
		}
		wb.publishMouseEvent(&wb.mouseDownPublisher, msg, wParam, lParam)

	case win.WM_LBUTTONUP, win.WM_MBUTTONUP, win.WM_RBUTTONUP:
		if msg == win.WM_LBUTTONUP && wb.origWndProcPtr == 0 {
			// See WM_LBUTTONDOWN for why we require origWndProcPtr == 0 here.
			if !win.ReleaseCapture() {
				lastError("ReleaseCapture")
			}
		}
		wb.publishMouseEvent(&wb.mouseUpPublisher, msg, wParam, lParam)

	case win.WM_MOUSEMOVE:
		wb.publishMouseEvent(&wb.mouseMovePublisher, msg, wParam, lParam)

	case win.WM_MOUSEWHEEL:
		wb.publishMouseWheelEvent(&wb.mouseWheelPublisher, wParam, lParam)

	case win.WM_SETFOCUS, win.WM_KILLFOCUS:
		switch wnd := wb.window.(type) {
		case *splitterHandle:
			// nop

		case Widget:
			parent := wnd.Parent()
			if parent == nil {
				hwndParent := win.GetParent(wnd.Handle())
				for parent == nil && hwndParent != 0 {
					hwndParent = win.GetParent(hwndParent)
					if wnd := windowFromHandle(hwndParent); wnd != nil {
						parent, _ = wnd.(Container)
					}
				}
			}
			wnd.AsWidgetBase().invalidateBorderInParent()
		}

		wb.focusedChangedPublisher.Publish()

	case win.WM_SETCURSOR:
		if wb.cursor != nil {
			win.SetCursor(wb.cursor.handle())
			return 0
		}

	case win.WM_CONTEXTMENU:
		sourceWindow := windowFromHandle(win.HWND(wParam))
		if sourceWindow == nil {
			break
		}

		x := win.GET_X_LPARAM(lParam)
		y := win.GET_Y_LPARAM(lParam)

		contextMenu := sourceWindow.ContextMenu()

		var handle win.HWND
		if widget, ok := sourceWindow.(Widget); ok {
			if form := ancestor(widget); form != nil {
				handle = form.Handle()
			}
		}

		if handle == 0 {
			handle = sourceWindow.Handle()
		}

		if contextMenu != nil {
			win.TrackPopupMenuEx(
				contextMenu.hMenu,
				win.TPM_NOANIMATION,
				x,
				y,
				handle,
				nil)
			return 0
		}

	case win.WM_KEYDOWN:
		wb.handleKeyDown(wParam, lParam)

	case win.WM_KEYUP:
		wb.handleKeyUp(wParam, lParam)

	case win.WM_DROPFILES:
		wb.dropFilesPublisher.Publish(win.HDROP(wParam))

	case win.WM_SIZE, win.WM_SIZING:
		if msg == win.WM_SIZE {
			if widget, ok := wb.window.(Widget); ok {
				widget.AsWidgetBase().invalidateBorderInParent()
			}
		}

		wb.sizeChangedPublisher.Publish()

	case win.WM_WINDOWPOSCHANGED:
		wb.boundsChangedPublisher.Publish()

	case win.WM_DESTROY:
		if wb.origWndProcPtr != 0 {
			// As we subclass all windows of system classes, we prevented the
			// clean-up code in the WM_NCDESTROY handlers of some windows from
			// being called. To fix this, we restore the original window
			// procedure here.
			win.SetWindowLongPtr(wb.hWnd, win.GWLP_WNDPROC, wb.origWndProcPtr)
		}

		delete(hwnd2WindowBase, hwnd)

		wb.window.Dispose()
		wb.hWnd = 0
	}

	if window != nil {
		if wndProc := window.AsWindowBase().origWndProcPtr; wndProc != 0 {
			return win.CallWindowProc(wndProc, hwnd, msg, wParam, lParam)
		}
	}

	return win.DefWindowProc(hwnd, msg, wParam, lParam)
}