Debian Codegraph @

phoenix/windows/window.cpp

raw
1 namespace phoenix {
2
3 vector<pWindow*> pWindow::modal;
4
5 //EnableWindow(hwnd, false) sends WM_KILLFOCUS; deactivating said window
6 //EnableWindow(hwnd, true) does not restore lost focus
7 //when a modal loop finishes, and the dialog is dismissed, the application loses focus entirely
8 //due to anti-focus-stealing code in Windows, SetForegroundWindow() cannot restore lost focus
9 //further, GetActiveWindow() returns nothing when all windows have lost focus
10 //thus, we must use a focus-stealing hack to reclaim the focus we never intended to dismiss;
11 //and we must replicate GetActiveWindow() by scanning the Z-order of windows for this process
12
13 void pWindow::updateModality() {
14 //bind thread input to process that currently has input focus
15 auto threadId = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
16 AttachThreadInput(threadId, GetCurrentThreadId(), TRUE);
17
18 pWindow* topMost = nullptr;
19 for(auto& object : pObject::objects) {
20 if(dynamic_cast<pWindow*>(object) == nullptr) continue;
21 pWindow* p = (pWindow*)object;
22 bool enable = modal.size() == 0 || modal.find(p);
23 if(IsWindowEnabled(p->hwnd) != enable) EnableWindow(p->hwnd, enable);
24 if(enable && p->window.visible()) {
25 if(topMost == nullptr) topMost = p;
26 else if(GetWindowZOrder(p->hwnd) < GetWindowZOrder(topMost->hwnd)) topMost = p;
27 }
28 }
29
30 //set input focus on top-most window
31 if(topMost) {
32 SetForegroundWindow(topMost->hwnd);
33 SetActiveWindow(topMost->hwnd);
34 }
35
36 //unbind thread input hook
37 AttachThreadInput(threadId, GetCurrentThreadId(), FALSE);
38 }
39
40 static const unsigned FixedStyle = WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX | WS_BORDER;
41 static const unsigned ResizableStyle = WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_THICKFRAME;
42
43 Window& pWindow::none() {
44 static Window* window = nullptr;
45 if(window == nullptr) window = new Window;
46 return *window;
47 }
48
49 void pWindow::append(Layout& layout) {
50 Geometry geom = window.state.geometry;
51 geom.x = geom.y = 0;
52 layout.setGeometry(geom);
53 }
54
55 void pWindow::append(Menu& menu) {
56 menu.p.parentWindow = &window;
57 updateMenu();
58 }
59
60 void pWindow::append(Widget& widget) {
61 if(GetParentWidget(&widget)) {
62 widget.p.parentHwnd = GetParentWidget(&widget)->p.hwnd;
63 } else {
64 widget.p.parentHwnd = window.p.hwnd;
65 }
66 widget.p.orphan();
67
68 if(widget.font().empty() && !window.state.widgetFont.empty()) {
69 widget.setFont(window.state.widgetFont);
70 }
71 }
72
73 bool pWindow::focused() {
74 return (GetForegroundWindow() == hwnd);
75 }
76
77 Geometry pWindow::frameMargin() {
78 unsigned style = window.state.resizable ? ResizableStyle : FixedStyle;
79 if(window.state.fullScreen) style = 0;
80 RECT rc = {0, 0, 640, 480};
81 AdjustWindowRect(&rc, style, window.state.menuVisible);
82 unsigned statusHeight = 0;
83 if(window.state.statusVisible) {
84 RECT src;
85 GetClientRect(hstatus, &src);
86 statusHeight = src.bottom - src.top;
87 }
88 return {abs(rc.left), abs(rc.top), (rc.right - rc.left) - 640, (rc.bottom - rc.top) + statusHeight - 480};
89 }
90
91 Geometry pWindow::geometry() {
92 Geometry margin = frameMargin();
93
94 RECT rc;
95 if(IsIconic(hwnd)) {
96 //GetWindowRect returns -32000(x),-32000(y) when window is minimized
97 WINDOWPLACEMENT wp;
98 GetWindowPlacement(hwnd, &wp);
99 rc = wp.rcNormalPosition;
100 } else {
101 GetWindowRect(hwnd, &rc);
102 }
103
104 signed x = rc.left + margin.x;
105 signed y = rc.top + margin.y;
106 unsigned width = (rc.right - rc.left) - margin.width;
107 unsigned height = (rc.bottom - rc.top) - margin.height;
108
109 return {x, y, width, height};
110 }
111
112 void pWindow::remove(Layout& layout) {
113 }
114
115 void pWindow::remove(Menu& menu) {
116 updateMenu();
117 }
118
119 void pWindow::remove(Widget& widget) {
120 widget.p.orphan();
121 }
122
123 void pWindow::setBackgroundColor(Color color) {
124 if(brush) DeleteObject(brush);
125 brushColor = RGB(color.red, color.green, color.blue);
126 brush = CreateSolidBrush(brushColor);
127 }
128
129 void pWindow::setDroppable(bool droppable) {
130 DragAcceptFiles(hwnd, droppable);
131 }
132
133 void pWindow::setFocused() {
134 if(window.state.visible == false) setVisible(true);
135 SetFocus(hwnd);
136 }
137
138 void pWindow::setFullScreen(bool fullScreen) {
139 locked = true;
140 if(fullScreen == false) {
141 SetWindowLongPtr(hwnd, GWL_STYLE, WS_VISIBLE | (window.state.resizable ? ResizableStyle : FixedStyle));
142 setGeometry(window.state.geometry);
143 } else {
144 HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
145 MONITORINFOEX info;
146 memset(&info, 0, sizeof(MONITORINFOEX));
147 info.cbSize = sizeof(MONITORINFOEX);
148 GetMonitorInfo(monitor, &info);
149 RECT rc = info.rcMonitor;
150 Geometry geometry = {rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top};
151 SetWindowLongPtr(hwnd, GWL_STYLE, WS_VISIBLE | WS_POPUP);
152 Geometry margin = frameMargin();
153 setGeometry({
154 geometry.x + margin.x, geometry.y + margin.y,
155 geometry.width - margin.width, geometry.height - margin.height
156 });
157 }
158 locked = false;
159 }
160
161 void pWindow::setGeometry(Geometry geometry) {
162 locked = true;
163 Geometry margin = frameMargin();
164 SetWindowPos(
165 hwnd, NULL,
166 geometry.x - margin.x, geometry.y - margin.y,
167 geometry.width + margin.width, geometry.height + margin.height,
168 SWP_NOZORDER | SWP_FRAMECHANGED
169 );
170 SetWindowPos(hstatus, NULL, 0, 0, 0, 0, SWP_NOZORDER | SWP_FRAMECHANGED);
171 for(auto& layout : window.state.layout) {
172 Geometry geom = this->geometry();
173 geom.x = geom.y = 0;
174 layout.setGeometry(geom);
175 }
176 locked = false;
177 }
178
179 void pWindow::setMenuFont(string font) {
180 }
181
182 void pWindow::setMenuVisible(bool visible) {
183 locked = true;
184 SetMenu(hwnd, visible ? hmenu : 0);
185 setGeometry(window.state.geometry);
186 locked = false;
187 }
188
189 void pWindow::setModal(bool modality) {
190 if(modality == true) {
191 modal.appendOnce(this);
192 updateModality();
193 while(window.state.modal) {
194 Application::processEvents();
195 usleep(20 * 1000);
196 }
197 if(auto position = modal.find(this)) modal.remove(position());
198 updateModality();
199 }
200 }
201
202 void pWindow::setResizable(bool resizable) {
203 SetWindowLongPtr(hwnd, GWL_STYLE, window.state.resizable ? ResizableStyle : FixedStyle);
204 setGeometry(window.state.geometry);
205 }
206
207 void pWindow::setStatusFont(string font) {
208 if(hstatusfont) DeleteObject(hstatusfont);
209 hstatusfont = pFont::create(font);
210 SendMessage(hstatus, WM_SETFONT, (WPARAM)hstatusfont, 0);
211 }
212
213 void pWindow::setStatusText(string text) {
214 SendMessage(hstatus, SB_SETTEXT, 0, (LPARAM)(wchar_t*)utf16_t(text));
215 }
216
217 void pWindow::setStatusVisible(bool visible) {
218 locked = true;
219 ShowWindow(hstatus, visible ? SW_SHOWNORMAL : SW_HIDE);
220 setGeometry(window.state.geometry);
221 locked = false;
222 }
223
224 void pWindow::setTitle(string text) {
225 SetWindowText(hwnd, utf16_t(text));
226 }
227
228 void pWindow::setVisible(bool visible) {
229 ShowWindow(hwnd, visible ? SW_SHOWNORMAL : SW_HIDE);
230 if(visible == false) setModal(false);
231 }
232
233 void pWindow::setWidgetFont(string font) {
234 }
235
236 void pWindow::constructor() {
237 brush = 0;
238
239 hwnd = CreateWindow(L"phoenix_window", L"", ResizableStyle, 128, 128, 256, 256, 0, 0, GetModuleHandle(0), 0);
240 hmenu = CreateMenu();
241 hstatus = CreateWindow(STATUSCLASSNAME, L"", WS_CHILD, 0, 0, 0, 0, hwnd, 0, GetModuleHandle(0), 0);
242 hstatusfont = 0;
243 setStatusFont(Font::sans(8));
244
245 //status bar will be capable of receiving tab focus if it is not disabled
246 SetWindowLongPtr(hstatus, GWL_STYLE, GetWindowLong(hstatus, GWL_STYLE) | WS_DISABLED);
247
248 SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)&window);
249 setDroppable(window.state.droppable);
250 setGeometry({128, 128, 256, 256});
251
252 DWORD color = GetSysColor(COLOR_3DFACE);
253 window.state.backgroundColor = Color((uint8_t)(color >> 16), (uint8_t)(color >> 8), (uint8_t)(color >> 0), 255u);
254 }
255
256 void pWindow::destructor() {
257 DeleteObject(hstatusfont);
258 DestroyWindow(hstatus);
259 DestroyMenu(hmenu);
260 DestroyWindow(hwnd);
261 }
262
263 void pWindow::updateMenu() {
264 if(hmenu) DestroyMenu(hmenu);
265 hmenu = CreateMenu();
266
267 for(auto& menu : window.state.menu) {
268 menu.p.update(window);
269 if(menu.visible()) {
270 AppendMenu(hmenu, MF_STRING | MF_POPUP, (UINT_PTR)menu.p.hmenu, utf16_t(menu.state.text));
271 }
272 }
273
274 SetMenu(hwnd, window.state.menuVisible ? hmenu : 0);
275 }
276
277 void pWindow::onClose() {
278 if(window.onClose) window.onClose();
279 else window.setVisible(false);
280 if(window.state.modal && !window.state.visible) window.setModal(false);
281 }
282
283 void pWindow::onDrop(WPARAM wparam) {
284 lstring paths = DropPaths(wparam);
285 if(paths.empty()) return;
286 if(window.onDrop) window.onDrop(paths);
287 }
288
289 bool pWindow::onEraseBackground() {
290 if(brush == 0) return false;
291 RECT rc;
292 GetClientRect(hwnd, &rc);
293 PAINTSTRUCT ps;
294 BeginPaint(hwnd, &ps);
295 FillRect(ps.hdc, &rc, brush);
296 EndPaint(hwnd, &ps);
297 return true;
298 }
299
300 void pWindow::onModalBegin() {
301 if(Application::Windows::onModalBegin) Application::Windows::onModalBegin();
302 }
303
304 void pWindow::onModalEnd() {
305 if(Application::Windows::onModalEnd) Application::Windows::onModalEnd();
306 }
307
308 void pWindow::onMove() {
309 if(locked) return;
310
311 Geometry windowGeometry = geometry();
312 window.state.geometry.x = windowGeometry.x;
313 window.state.geometry.y = windowGeometry.y;
314
315 if(window.onMove) window.onMove();
316 }
317
318 void pWindow::onSize() {
319 if(locked) return;
320 SetWindowPos(hstatus, NULL, 0, 0, 0, 0, SWP_NOZORDER | SWP_FRAMECHANGED);
321
322 Geometry windowGeometry = geometry();
323 window.state.geometry.width = windowGeometry.width;
324 window.state.geometry.height = windowGeometry.height;
325
326 for(auto& layout : window.state.layout) {
327 Geometry geom = geometry();
328 geom.x = geom.y = 0;
329 layout.setGeometry(geom);
330 }
331
332 if(window.onSize) window.onSize();
333 }
334
335 }