rootwindow.cpp revision 1.23 1 1.23 kamil /* -*-C++-*- $NetBSD: rootwindow.cpp,v 1.23 2015/07/11 10:32:45 kamil Exp $ */
2 1.1 uch
3 1.1 uch /*-
4 1.20 uch * Copyright (c) 2001, 2004 The NetBSD Foundation, Inc.
5 1.1 uch * All rights reserved.
6 1.1 uch *
7 1.1 uch * This code is derived from software contributed to The NetBSD Foundation
8 1.1 uch * by UCHIYAMA Yasushi.
9 1.1 uch *
10 1.1 uch * Redistribution and use in source and binary forms, with or without
11 1.1 uch * modification, are permitted provided that the following conditions
12 1.1 uch * are met:
13 1.1 uch * 1. Redistributions of source code must retain the above copyright
14 1.1 uch * notice, this list of conditions and the following disclaimer.
15 1.1 uch * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 uch * notice, this list of conditions and the following disclaimer in the
17 1.1 uch * documentation and/or other materials provided with the distribution.
18 1.1 uch *
19 1.1 uch * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 uch * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 uch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 uch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 uch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 uch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 uch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 uch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 uch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 uch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 uch * POSSIBILITY OF SUCH DAMAGE.
30 1.1 uch */
31 1.1 uch
32 1.1 uch #include <hpcmenu.h>
33 1.1 uch #include <menu/window.h>
34 1.1 uch #include <menu/tabwindow.h>
35 1.1 uch #include <menu/rootwindow.h>
36 1.1 uch #include <res/resource.h>
37 1.4 uch #include "../binary/build_number.h"
38 1.18 uch #include <console.h>
39 1.1 uch
40 1.1 uch //
41 1.1 uch // root window
42 1.1 uch //
43 1.1 uch RootWindow::RootWindow(HpcBootApp &app)
44 1.1 uch : Window(app)
45 1.1 uch {
46 1.1 uch _boot_button = 0;
47 1.1 uch _base = 0;
48 1.1 uch _main = 0;
49 1.1 uch _option = 0;
50 1.1 uch _console = 0;
51 1.1 uch }
52 1.1 uch
53 1.1 uch RootWindow::~RootWindow()
54 1.1 uch {
55 1.1 uch if (_boot_button)
56 1.1 uch delete _boot_button;
57 1.1 uch if (_cancel_button)
58 1.1 uch delete _cancel_button;
59 1.1 uch if (_progress_bar)
60 1.1 uch delete _progress_bar;
61 1.1 uch if (_main)
62 1.1 uch delete _main;
63 1.1 uch if (_option)
64 1.1 uch delete _option;
65 1.1 uch if (_console)
66 1.1 uch delete _console;
67 1.1 uch if (_base)
68 1.1 uch delete _base;
69 1.1 uch }
70 1.1 uch
71 1.1 uch BOOL
72 1.1 uch RootWindow::create(LPCREATESTRUCT aux)
73 1.1 uch {
74 1.4 uch TCHAR app_name[32];
75 1.1 uch // Root window's create don't called by Window Procedure.
76 1.1 uch // so aux is NULL
77 1.1 uch HINSTANCE inst = _app._instance;
78 1.1 uch TCHAR *wc_name = reinterpret_cast <TCHAR *>
79 1.3 uch (LoadString(inst, IDS_HPCMENU, 0, 0));
80 1.4 uch wsprintf(app_name, TEXT("%s Build %d"), wc_name, HPCBOOT_BUILD_NUMBER);
81 1.10 uwe
82 1.4 uch _window = CreateWindow(wc_name, app_name, WS_VISIBLE,
83 1.3 uch CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
84 1.3 uch 0, 0, inst, this);
85 1.1 uch if (!_window)
86 1.1 uch return FALSE;
87 1.1 uch
88 1.1 uch HpcMenuInterface &menu = HpcMenuInterface::Instance();
89 1.1 uch if (menu._pref.auto_boot > 0)
90 1.1 uch SetTimer(_window, IDD_TIMER, menu._pref.auto_boot * 1000, 0);
91 1.1 uch
92 1.1 uch ShowWindow(_window, SW_SHOW);
93 1.1 uch UpdateWindow(_window);
94 1.1 uch
95 1.1 uch return TRUE;
96 1.1 uch }
97 1.1 uch
98 1.1 uch BOOL
99 1.1 uch RootWindow::proc(HWND w, UINT msg, WPARAM wparam, LPARAM lparam)
100 1.1 uch {
101 1.1 uch LPCREATESTRUCT aux = reinterpret_cast <LPCREATESTRUCT>(lparam);
102 1.1 uch HpcMenuInterface &menu = HpcMenuInterface::Instance();
103 1.1 uch
104 1.1 uch switch(msg) {
105 1.1 uch default: // message can't handle.
106 1.1 uch return FALSE;
107 1.1 uch case WM_CREATE:
108 1.1 uch WMCreate(w, aux);
109 1.1 uch break;
110 1.1 uch case WM_PAINT:
111 1.1 uch WMPaint(w, aux);
112 1.1 uch break;
113 1.14 uwe case WM_ENTERMENULOOP:
114 1.14 uwe SaveFocus();
115 1.14 uwe break;
116 1.14 uwe case WM_EXITMENULOOP:
117 1.14 uwe RestoreFocus();
118 1.14 uwe break;
119 1.14 uwe case WM_ACTIVATE:
120 1.14 uwe if ((UINT)LOWORD(wparam) == WA_INACTIVE)
121 1.14 uwe SaveFocus();
122 1.14 uwe else
123 1.14 uwe RestoreFocus();
124 1.14 uwe break;
125 1.1 uch case WM_NOTIFY:
126 1.1 uch {
127 1.1 uch NMHDR *notify = reinterpret_cast <NMHDR *>(lparam);
128 1.1 uch // get current selected tab id
129 1.1 uch int tab_id = TabCtrl_GetCurSel(_base->_window);
130 1.1 uch // get context
131 1.1 uch TC_ITEM tc_item;
132 1.1 uch tc_item.mask = TCIF_PARAM;
133 1.1 uch TabCtrl_GetItem(_base->_window, tab_id, &tc_item);
134 1.1 uch TabWindow *tab = reinterpret_cast <TabWindow *>
135 1.3 uch (tc_item.lParam);
136 1.1 uch switch(notify->code) {
137 1.1 uch case TCN_SELCHANGING:
138 1.1 uch tab->hide();
139 1.1 uch break;
140 1.1 uch case TCN_SELCHANGE:
141 1.1 uch tab->show();
142 1.1 uch break;
143 1.13 uwe case TCN_KEYDOWN: {
144 1.13 uwe NMTCKEYDOWN *key = reinterpret_cast
145 1.13 uwe <NMTCKEYDOWN *>(lparam);
146 1.13 uwe return _base->focusManagerHook(key->wVKey, key->flags,
147 1.13 uwe _cancel_button->_window);
148 1.13 uwe }
149 1.1 uch }
150 1.1 uch }
151 1.1 uch break;
152 1.1 uch case WM_TIMER:
153 1.1 uch disableTimer();
154 1.1 uch goto boot;
155 1.1 uch case WM_COMMAND:
156 1.1 uch switch(wparam)
157 1.1 uch {
158 1.1 uch case IDC_BOOTBUTTON:
159 1.1 uch // inquire current options.
160 1.1 uch menu.get_options();
161 1.1 uch if (menu._pref.safety_message) {
162 1.12 uwe UINT mb_icon = menu._pref.pause_before_boot ?
163 1.12 uwe MB_ICONQUESTION : MB_ICONWARNING;
164 1.3 uch if (MessageBox(_window,
165 1.5 uwe TEXT("Data in memory will be lost.\nAre you sure?"),
166 1.5 uwe TEXT("WARNING"),
167 1.12 uwe mb_icon | MB_YESNO) != IDYES)
168 1.1 uch break;
169 1.13 uwe UpdateWindow(_window);
170 1.1 uch }
171 1.1 uch boot:
172 1.2 uch SendMessage(_progress_bar->_window, PBM_SETPOS, 0, 0);
173 1.1 uch menu.print(TEXT("BOOT START\n"));
174 1.1 uch // inquire current options.
175 1.1 uch menu.get_options();
176 1.1 uch // save options to `hpcboot.cnf'
177 1.1 uch menu.save();
178 1.5 uwe // start boot sequence.
179 1.1 uch menu.boot();
180 1.1 uch // NOTREACHED
181 1.1 uch break;
182 1.1 uch case IDC_PROGRESSBAR:
183 1.1 uch break;
184 1.1 uch case IDC_CANCELBUTTON:
185 1.1 uch PostQuitMessage(0);
186 1.1 uch break;
187 1.1 uch }
188 1.1 uch break;
189 1.1 uch case WM_DESTROY:
190 1.1 uch PostQuitMessage(0);
191 1.1 uch break;
192 1.1 uch }
193 1.1 uch return TRUE;
194 1.1 uch }
195 1.1 uch
196 1.1 uch void
197 1.14 uwe RootWindow::SaveFocus() {
198 1.14 uwe _saved_focus = GetFocus();
199 1.14 uwe }
200 1.14 uwe
201 1.14 uwe void
202 1.14 uwe RootWindow::RestoreFocus() {
203 1.14 uwe SetFocus(IsWindowEnabled(_saved_focus) ?
204 1.14 uwe _saved_focus : _boot_button->_window);
205 1.14 uwe }
206 1.14 uwe
207 1.14 uwe void
208 1.1 uch RootWindow::WMPaint(HWND w, LPCREATESTRUCT aux)
209 1.10 uwe {
210 1.1 uch PAINTSTRUCT ps;
211 1.1 uch BeginPaint(w, &ps);
212 1.1 uch EndPaint(w, &ps);
213 1.1 uch }
214 1.1 uch
215 1.1 uch void
216 1.1 uch RootWindow::WMCreate(HWND w, LPCREATESTRUCT aux)
217 1.1 uch {
218 1.1 uch int cmdbar_height;
219 1.1 uch
220 1.1 uch _window = w;
221 1.1 uch // Command bar.
222 1.1 uch _app._cmdbar = CommandBar_Create(aux->hInstance, w, IDC_CMDBAR);
223 1.1 uch CommandBar_AddAdornments(_app._cmdbar, 0, 0);
224 1.1 uch cmdbar_height = CommandBar_Height(_app._cmdbar);
225 1.15 uwe
226 1.1 uch _button_height = cmdbar_height;
227 1.15 uwe _button_width = BOOT_BUTTON_WIDTH;
228 1.16 uwe
229 1.16 uwe HDC hdc = GetDC(0);
230 1.16 uwe if (GetDeviceCaps(hdc, HORZRES) > 320)
231 1.15 uwe _button_width += _button_width/2;
232 1.16 uwe ReleaseDC(0, hdc);
233 1.10 uwe
234 1.1 uch RECT rect;
235 1.1 uch GetClientRect(w, &rect);
236 1.1 uch rect.top += cmdbar_height;
237 1.1 uch
238 1.1 uch // BOOT button.
239 1.1 uch _boot_button = new BootButton(_app, *this, rect);
240 1.1 uch _boot_button->create(aux);
241 1.1 uch // CANCEL button.
242 1.1 uch _cancel_button = new CancelButton(_app, *this, rect);
243 1.1 uch _cancel_button->create(aux);
244 1.1 uch // Progress bar
245 1.1 uch _progress_bar = new ProgressBar(_app, *this, rect);
246 1.1 uch _progress_bar->create(aux);
247 1.1 uch
248 1.23 kamil // register myself to menu
249 1.1 uch HpcMenuInterface::Instance()._root = this;
250 1.1 uch
251 1.9 uwe rect.top += _button_height;
252 1.1 uch // Tab control.
253 1.1 uch _base = new TabWindowBase(_app, w, rect, IDC_BASE);
254 1.1 uch _base->create(aux);
255 1.1 uch // main/option/console dialog.(register to Menu)
256 1.1 uch _main = _base->boot(IDC_BASE_MAIN);
257 1.1 uch _option = _base->boot(IDC_BASE_OPTION);
258 1.1 uch _console = _base->boot(IDC_BASE_CONSOLE);
259 1.10 uwe
260 1.1 uch _main->show();
261 1.14 uwe SetFocus(_boot_button->_window);
262 1.1 uch
263 1.1 uch return;
264 1.1 uch }
265 1.1 uch
266 1.1 uch void
267 1.1 uch RootWindow::disableTimer()
268 1.10 uwe {
269 1.1 uch KillTimer(_window, IDD_TIMER);
270 1.1 uch }
271 1.1 uch
272 1.1 uch BOOL
273 1.1 uch RootWindow::isDialogMessage(MSG &msg)
274 1.1 uch {
275 1.13 uwe HWND tab_window;
276 1.13 uwe
277 1.1 uch if (_main && IsWindowVisible(_main->_window))
278 1.13 uwe tab_window = _main->_window;
279 1.13 uwe else if (_option && IsWindowVisible(_option->_window))
280 1.13 uwe tab_window = _option->_window;
281 1.13 uwe else if (_console && IsWindowVisible(_console->_window))
282 1.13 uwe tab_window = _console->_window;
283 1.13 uwe
284 1.13 uwe if (focusManagerHook(msg, tab_window))
285 1.13 uwe return TRUE;
286 1.13 uwe
287 1.13 uwe return IsDialogMessage(tab_window, &msg);
288 1.13 uwe }
289 1.13 uwe
290 1.13 uwe //
291 1.17 uwe // XXX !!! XXX !!! XXX !!! XXX !!!
292 1.17 uwe //
293 1.13 uwe // WinCE 2.11 doesn't support keyboard focus traversal for nested
294 1.13 uwe // dialogs, so implement poor man focus manager for our root window.
295 1.13 uwe // This function handles focus transition from boot/cancel buttons.
296 1.13 uwe // Transition from the tab-control is done on WM_NOTIFY/TCN_KEYDOWN
297 1.13 uwe // above.
298 1.13 uwe //
299 1.13 uwe // XXX: This is a very smplistic implementation that doesn't handle
300 1.17 uwe // <TAB> auto-repeat count in LOWORD(msg.lParam), WS_GROUP, etc...
301 1.13 uwe //
302 1.13 uwe BOOL
303 1.13 uwe RootWindow::focusManagerHook(MSG &msg, HWND tab_window)
304 1.13 uwe {
305 1.13 uwe HWND next, prev;
306 1.13 uwe HWND dst = 0;
307 1.17 uwe LRESULT dlgcode = 0;
308 1.13 uwe
309 1.13 uwe if (msg.message != WM_KEYDOWN)
310 1.13 uwe return FALSE;
311 1.13 uwe
312 1.13 uwe if (msg.hwnd == _boot_button->_window) {
313 1.13 uwe next = _cancel_button->_window;
314 1.13 uwe prev = _base->_window;
315 1.13 uwe } else if (msg.hwnd == _cancel_button->_window) {
316 1.13 uwe next = _base->_window;
317 1.13 uwe prev = _boot_button->_window;
318 1.13 uwe } else if (tab_window == 0) {
319 1.13 uwe return FALSE;
320 1.13 uwe } else {
321 1.13 uwe // last focusable control in the tab_window (XXX: WS_GROUP?)
322 1.13 uwe HWND last = GetNextDlgTabItem(tab_window, NULL, TRUE);
323 1.17 uwe if (last == NULL ||
324 1.17 uwe !(last == msg.hwnd || IsChild(last, msg.hwnd)))
325 1.13 uwe return FALSE;
326 1.17 uwe dlgcode = SendMessage(last, WM_GETDLGCODE, NULL, (LPARAM)&msg);
327 1.13 uwe next = _base->_window; // out of the tab window
328 1.13 uwe prev = 0; // let IsDialogMessage handle it
329 1.13 uwe }
330 1.19 uch
331 1.17 uwe #if 0 // XXX: breaks tabbing out of the console window
332 1.17 uwe if (dlgcode & DLGC_WANTALLKEYS)
333 1.17 uwe return FALSE;
334 1.17 uwe #endif
335 1.13 uwe switch (msg.wParam) {
336 1.13 uwe case VK_RIGHT:
337 1.13 uwe case VK_DOWN:
338 1.17 uwe if (dlgcode & DLGC_WANTARROWS)
339 1.17 uwe return FALSE;
340 1.13 uwe dst = next;
341 1.13 uwe break;
342 1.13 uwe
343 1.13 uwe case VK_LEFT:
344 1.13 uwe case VK_UP:
345 1.17 uwe if (dlgcode & DLGC_WANTARROWS)
346 1.17 uwe return FALSE;
347 1.13 uwe dst = prev;
348 1.13 uwe break;
349 1.13 uwe
350 1.13 uwe case VK_TAB:
351 1.17 uwe if (dlgcode & DLGC_WANTTAB)
352 1.17 uwe return FALSE;
353 1.13 uwe if (GetKeyState(VK_SHIFT) & 0x8000) // Shift-Tab
354 1.13 uwe dst = prev;
355 1.13 uwe else
356 1.13 uwe dst = next;
357 1.13 uwe break;
358 1.13 uwe }
359 1.13 uwe
360 1.13 uwe if (dst == 0)
361 1.13 uwe return FALSE;
362 1.13 uwe
363 1.13 uwe SetFocus(dst);
364 1.13 uwe return TRUE;
365 1.1 uch }
366 1.1 uch
367 1.6 uwe void
368 1.18 uch RootWindow::progress(const char *msg)
369 1.6 uwe {
370 1.18 uch
371 1.18 uch if (msg)
372 1.20 uch Console::Instance()->print(TEXT("[progress] %S\n"), msg);
373 1.18 uch
374 1.7 uwe SendMessage(_progress_bar->_window, PBM_STEPIT, 0, 0);
375 1.6 uwe }
376 1.6 uwe
377 1.6 uwe void
378 1.6 uwe RootWindow::unprogress()
379 1.6 uwe {
380 1.7 uwe SendMessage(_progress_bar->_window, PBM_SETPOS, 0, 0);
381 1.6 uwe }
382 1.6 uwe
383 1.1 uch //
384 1.1 uch // BOOT button
385 1.1 uch //
386 1.1 uch BOOL
387 1.1 uch BootButton::create(LPCREATESTRUCT aux)
388 1.1 uch {
389 1.15 uwe int cx = _root._button_width;
390 1.1 uch int cy = _root._button_height;
391 1.1 uch
392 1.11 uwe _window = CreateWindow(TEXT("BUTTON"), TEXT("Boot"),
393 1.3 uch BS_PUSHBUTTON | BS_NOTIFY |
394 1.3 uch WS_VISIBLE | WS_CHILD | WS_TABSTOP,
395 1.3 uch _rect.left, _rect.top, cx, cy, _parent_window,
396 1.3 uch reinterpret_cast <HMENU>(IDC_BOOTBUTTON),
397 1.3 uch aux->hInstance,
398 1.3 uch NULL);
399 1.1 uch
400 1.1 uch return IsWindow(_window) ? TRUE : FALSE;
401 1.1 uch }
402 1.1 uch
403 1.1 uch //
404 1.1 uch // CANCEL button
405 1.1 uch //
406 1.1 uch BOOL
407 1.1 uch CancelButton::create(LPCREATESTRUCT aux)
408 1.1 uch {
409 1.15 uwe int cx = _root._button_width;
410 1.1 uch int cy = _root._button_height;
411 1.15 uwe int x = _rect.right - _root._button_width;
412 1.1 uch
413 1.11 uwe _window = CreateWindow(TEXT("BUTTON"), TEXT("Cancel"),
414 1.3 uch BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP |
415 1.3 uch WS_VISIBLE | WS_CHILD,
416 1.3 uch x, _rect.top, cx, cy, _parent_window,
417 1.3 uch reinterpret_cast <HMENU>(IDC_CANCELBUTTON),
418 1.3 uch aux->hInstance,
419 1.3 uch NULL);
420 1.13 uwe
421 1.1 uch return IsWindow(_window) ? TRUE : FALSE;
422 1.1 uch }
423 1.1 uch
424 1.1 uch //
425 1.1 uch // PROGRESS BAR
426 1.1 uch //
427 1.1 uch BOOL
428 1.1 uch ProgressBar::create(LPCREATESTRUCT aux)
429 1.1 uch {
430 1.15 uwe int cx = _rect.right - _rect.left - _root._button_width * 2;
431 1.1 uch int cy = _root._button_height;
432 1.15 uwe int x = _rect.left + _root._button_width;
433 1.11 uwe _window = CreateWindowEx(WS_EX_CLIENTEDGE,
434 1.11 uwe PROGRESS_CLASS, TEXT(""),
435 1.11 uwe PBS_SMOOTH | WS_VISIBLE | WS_CHILD,
436 1.3 uch x, _rect.top, cx, cy, _parent_window,
437 1.3 uch reinterpret_cast <HMENU>(IDC_PROGRESSBAR),
438 1.3 uch aux->hInstance, NULL);
439 1.9 uwe SendMessage(_window, PBM_SETRANGE, 0, MAKELPARAM(0, 11));
440 1.11 uwe SendMessage(_window, PBM_SETSTEP, 1, 0);
441 1.11 uwe SendMessage(_window, PBM_SETPOS, 0, 0);
442 1.1 uch
443 1.1 uch return IsWindow(_window) ? TRUE : FALSE;
444 1.1 uch }
445