-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.go
236 lines (180 loc) · 4.69 KB
/
mainwindow.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
// 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 (
"unsafe"
)
import (
"github.com/lxn/win"
)
const mainWindowWindowClass = `\o/ Walk_MainWindow_Class \o/`
func init() {
AppendToWalkInit(func() {
MustRegisterWindowClass(mainWindowWindowClass)
})
}
type MainWindow struct {
FormBase
windowPlacement *win.WINDOWPLACEMENT
menu *Menu
toolBar *ToolBar
statusBar *StatusBar
}
func NewMainWindow() (*MainWindow, error) {
return NewMainWindowWithName("")
}
func NewMainWindowWithName(name string) (*MainWindow, error) {
mw := new(MainWindow)
mw.SetName(name)
if err := InitWindow(
mw,
nil,
mainWindowWindowClass,
win.WS_OVERLAPPEDWINDOW,
win.WS_EX_CONTROLPARENT); err != nil {
return nil, err
}
succeeded := false
defer func() {
if !succeeded {
mw.Dispose()
}
}()
mw.SetPersistent(true)
var err error
if mw.menu, err = newMenuBar(mw); err != nil {
return nil, err
}
if !win.SetMenu(mw.hWnd, mw.menu.hMenu) {
return nil, lastError("SetMenu")
}
tb, err := NewToolBar(mw)
if err != nil {
return nil, err
}
mw.SetToolBar(tb)
if mw.statusBar, err = NewStatusBar(mw); err != nil {
return nil, err
}
mw.statusBar.parent = nil
mw.Children().Remove(mw.statusBar)
mw.statusBar.parent = mw
win.SetParent(mw.statusBar.hWnd, mw.hWnd)
mw.statusBar.visibleChangedPublisher.event.Attach(func() {
mw.SetBoundsPixels(mw.BoundsPixels())
})
succeeded = true
return mw, nil
}
func (mw *MainWindow) Menu() *Menu {
return mw.menu
}
func (mw *MainWindow) ToolBar() *ToolBar {
return mw.toolBar
}
func (mw *MainWindow) SetToolBar(tb *ToolBar) {
if mw.toolBar != nil {
win.SetParent(mw.toolBar.hWnd, 0)
}
if tb != nil {
parent := tb.parent
tb.parent = nil
parent.Children().Remove(tb)
tb.parent = mw
win.SetParent(tb.hWnd, mw.hWnd)
}
mw.toolBar = tb
}
func (mw *MainWindow) StatusBar() *StatusBar {
return mw.statusBar
}
func (mw *MainWindow) ClientBoundsPixels() Rectangle {
bounds := mw.FormBase.ClientBoundsPixels()
if mw.toolBar != nil && mw.toolBar.Actions().Len() > 0 {
tlbBounds := mw.toolBar.BoundsPixels()
bounds.Y += tlbBounds.Height
bounds.Height -= tlbBounds.Height
}
if mw.statusBar.Visible() {
bounds.Height -= mw.statusBar.HeightPixels()
}
return bounds
}
func (mw *MainWindow) SetVisible(visible bool) {
if visible {
win.DrawMenuBar(mw.hWnd)
mw.clientComposite.RequestLayout()
}
mw.FormBase.SetVisible(visible)
}
func (mw *MainWindow) applyFont(font *Font) {
mw.FormBase.applyFont(font)
if mw.toolBar != nil {
mw.toolBar.applyFont(font)
}
if mw.statusBar != nil {
mw.statusBar.applyFont(font)
}
}
func (mw *MainWindow) Fullscreen() bool {
return win.GetWindowLong(mw.hWnd, win.GWL_STYLE)&win.WS_OVERLAPPEDWINDOW == 0
}
func (mw *MainWindow) SetFullscreen(fullscreen bool) error {
if fullscreen == mw.Fullscreen() {
return nil
}
if fullscreen {
var mi win.MONITORINFO
mi.CbSize = uint32(unsafe.Sizeof(mi))
if mw.windowPlacement == nil {
mw.windowPlacement = new(win.WINDOWPLACEMENT)
}
if !win.GetWindowPlacement(mw.hWnd, mw.windowPlacement) {
return lastError("GetWindowPlacement")
}
if !win.GetMonitorInfo(win.MonitorFromWindow(
mw.hWnd, win.MONITOR_DEFAULTTOPRIMARY), &mi) {
return newError("GetMonitorInfo")
}
if err := mw.ensureStyleBits(win.WS_OVERLAPPEDWINDOW, false); err != nil {
return err
}
if r := mi.RcMonitor; !win.SetWindowPos(
mw.hWnd, win.HWND_TOP,
r.Left, r.Top, r.Right-r.Left, r.Bottom-r.Top,
win.SWP_FRAMECHANGED|win.SWP_NOOWNERZORDER) {
return lastError("SetWindowPos")
}
} else {
if err := mw.ensureStyleBits(win.WS_OVERLAPPEDWINDOW, true); err != nil {
return err
}
if !win.SetWindowPlacement(mw.hWnd, mw.windowPlacement) {
return lastError("SetWindowPlacement")
}
if !win.SetWindowPos(mw.hWnd, 0, 0, 0, 0, 0, win.SWP_FRAMECHANGED|win.SWP_NOMOVE|
win.SWP_NOOWNERZORDER|win.SWP_NOSIZE|win.SWP_NOZORDER) {
return lastError("SetWindowPos")
}
}
return nil
}
func (mw *MainWindow) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case win.WM_WINDOWPOSCHANGED:
wp := (*win.WINDOWPOS)(unsafe.Pointer(lParam))
if wp.Flags&win.SWP_NOSIZE != 0 {
break
}
cb := mw.ClientBoundsPixels()
if mw.toolBar != nil {
mw.toolBar.SetBoundsPixels(Rectangle{0, 0, cb.Width, mw.toolBar.HeightPixels()})
}
mw.statusBar.SetBoundsPixels(Rectangle{0, cb.Y + cb.Height, cb.Width, mw.statusBar.HeightPixels()})
case win.WM_INITMENUPOPUP:
mw.menu.updateItemsWithImageForWindow(mw)
}
return mw.FormBase.WndProc(hwnd, msg, wParam, lParam)
}