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