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