xref: /netbsd-src/sys/arch/hpc/stand/hpcboot/menu/rootwindow.cpp (revision 21e37cc72a480a47828990a439cde7ac9ffaf0c6)
1 /* -*-C++-*-	$NetBSD: rootwindow.cpp,v 1.17 2004/04/27 00:04:38 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_ENTERMENULOOP:
120 		SaveFocus();
121 		break;
122 	case WM_EXITMENULOOP:
123 		RestoreFocus();
124 		break;
125 	case WM_ACTIVATE:
126 		if ((UINT)LOWORD(wparam) == WA_INACTIVE)
127 			SaveFocus();
128 		else
129 			RestoreFocus();
130 		break;
131 	case WM_NOTIFY:
132 	{
133 		NMHDR *notify = reinterpret_cast <NMHDR *>(lparam);
134 		// get current selected tab id
135 		int tab_id = TabCtrl_GetCurSel(_base->_window);
136 		// get context
137 		TC_ITEM tc_item;
138 		tc_item.mask = TCIF_PARAM;
139 		TabCtrl_GetItem(_base->_window, tab_id, &tc_item);
140 		TabWindow *tab = reinterpret_cast <TabWindow *>
141 		    (tc_item.lParam);
142 		switch(notify->code) {
143 		case TCN_SELCHANGING:
144 			tab->hide();
145 			break;
146 		case TCN_SELCHANGE:
147 			tab->show();
148 			break;
149 		case TCN_KEYDOWN: {
150 			NMTCKEYDOWN *key = reinterpret_cast
151 				<NMTCKEYDOWN *>(lparam);
152 			return _base->focusManagerHook(key->wVKey, key->flags,
153 						_cancel_button->_window);
154 		    }
155 		}
156 	}
157 	break;
158 	case WM_TIMER:
159 		disableTimer();
160 		goto boot;
161 	case WM_COMMAND:
162 		switch(wparam)
163 		{
164 		case IDC_BOOTBUTTON:
165 			// inquire current options.
166 			menu.get_options();
167 			if (menu._pref.safety_message) {
168 				UINT mb_icon = menu._pref.pause_before_boot ?
169 					MB_ICONQUESTION : MB_ICONWARNING;
170 				if (MessageBox(_window,
171 				    TEXT("Data in memory will be lost.\nAre you sure?"),
172 				    TEXT("WARNING"),
173 				    mb_icon | MB_YESNO) != IDYES)
174 					break;
175 				UpdateWindow(_window);
176 			}
177 		boot:
178 			SendMessage(_progress_bar->_window, PBM_SETPOS, 0, 0);
179 			menu.print(TEXT("BOOT START\n"));
180 			// inquire current options.
181 			menu.get_options();
182 			// save options to `hpcboot.cnf'
183 			menu.save();
184 			// start boot sequence.
185 			menu.boot();
186 			// NOTREACHED
187 			break;
188 		case IDC_PROGRESSBAR:
189 			break;
190 		case IDC_CANCELBUTTON:
191 			PostQuitMessage(0);
192 			break;
193 		}
194 		break;
195 	case WM_DESTROY:
196 		PostQuitMessage(0);
197 		break;
198 	}
199 	return TRUE;
200 }
201 
202 void
203 RootWindow::SaveFocus() {
204 	_saved_focus = GetFocus();
205 }
206 
207 void
208 RootWindow::RestoreFocus() {
209 	SetFocus(IsWindowEnabled(_saved_focus) ?
210 		 _saved_focus : _boot_button->_window);
211 }
212 
213 void
214 RootWindow::WMPaint(HWND w, LPCREATESTRUCT aux)
215 {
216 	PAINTSTRUCT ps;
217 	BeginPaint(w, &ps);
218 	EndPaint(w, &ps);
219 }
220 
221 void
222 RootWindow::WMCreate(HWND w, LPCREATESTRUCT aux)
223 {
224 	int cmdbar_height;
225 
226 	_window = w;
227 	// Command bar.
228 	_app._cmdbar = CommandBar_Create(aux->hInstance, w, IDC_CMDBAR);
229 	CommandBar_AddAdornments(_app._cmdbar, 0, 0);
230 	cmdbar_height = CommandBar_Height(_app._cmdbar);
231 
232 	_button_height = cmdbar_height;
233 	_button_width = BOOT_BUTTON_WIDTH;
234 
235 	HDC hdc = GetDC(0);
236 	if (GetDeviceCaps(hdc, HORZRES) > 320)
237 	    _button_width += _button_width/2;
238 	ReleaseDC(0, hdc);
239 
240 	RECT rect;
241 	GetClientRect(w, &rect);
242 	rect.top += cmdbar_height;
243 
244 	// BOOT button.
245 	_boot_button = new BootButton(_app, *this, rect);
246 	_boot_button->create(aux);
247 	// CANCEL button.
248 	_cancel_button = new CancelButton(_app, *this, rect);
249 	_cancel_button->create(aux);
250 	// Progress bar
251 	_progress_bar = new ProgressBar(_app, *this, rect);
252 	_progress_bar->create(aux);
253 
254  	// regsiter myself to menu
255 	HpcMenuInterface::Instance()._root = this;
256 
257 	rect.top += _button_height;
258 	// Tab control.
259 	_base =  new TabWindowBase(_app, w, rect, IDC_BASE);
260 	_base->create(aux);
261 	// main/option/console dialog.(register to Menu)
262 	_main = _base->boot(IDC_BASE_MAIN);
263 	_option = _base->boot(IDC_BASE_OPTION);
264 	_console = _base->boot(IDC_BASE_CONSOLE);
265 
266 	_main->show();
267 	SetFocus(_boot_button->_window);
268 
269 	return;
270 }
271 
272 void
273 RootWindow::disableTimer()
274 {
275 	KillTimer(_window, IDD_TIMER);
276 }
277 
278 BOOL
279 RootWindow::isDialogMessage(MSG &msg)
280 {
281 	HWND tab_window;
282 
283 	if (_main && IsWindowVisible(_main->_window))
284 		tab_window = _main->_window;
285 	else if (_option && IsWindowVisible(_option->_window))
286 		tab_window = _option->_window;
287 	else if (_console && IsWindowVisible(_console->_window))
288 		tab_window = _console->_window;
289 
290 	if (focusManagerHook(msg, tab_window))
291 		return TRUE;
292 
293 	return IsDialogMessage(tab_window, &msg);
294 }
295 
296 //
297 // XXX !!! XXX !!! XXX !!! XXX !!!
298 //
299 // WinCE 2.11 doesn't support keyboard focus traversal for nested
300 // dialogs, so implement poor man focus manager for our root window.
301 // This function handles focus transition from boot/cancel buttons.
302 // Transition from the tab-control is done on WM_NOTIFY/TCN_KEYDOWN
303 // above.
304 //
305 // XXX: This is a very smplistic implementation that doesn't handle
306 // <TAB> auto-repeat count in LOWORD(msg.lParam), WS_GROUP, etc...
307 //
308 BOOL
309 RootWindow::focusManagerHook(MSG &msg, HWND tab_window)
310 {
311 	HWND next, prev;
312 	HWND dst = 0;
313 	LRESULT dlgcode = 0;
314 
315 	if (msg.message != WM_KEYDOWN)
316 		return FALSE;
317 
318 	if (msg.hwnd == _boot_button->_window) {
319 		next = _cancel_button->_window;
320 		prev = _base->_window;
321 	} else if (msg.hwnd == _cancel_button->_window) {
322 		next = _base->_window;
323 		prev = _boot_button->_window;
324 	} else if (tab_window == 0) {
325 		return FALSE;
326 	} else {
327 		// last focusable control in the tab_window (XXX: WS_GROUP?)
328 		HWND last = GetNextDlgTabItem(tab_window, NULL, TRUE);
329 		if (last == NULL ||
330 		    !(last == msg.hwnd || IsChild(last, msg.hwnd)))
331 			return FALSE;
332 		dlgcode = SendMessage(last, WM_GETDLGCODE, NULL, (LPARAM)&msg);
333 		next = _base->_window; // out of the tab window
334 		prev = 0;	// let IsDialogMessage handle it
335 	}
336 
337 #if 0 // XXX: breaks tabbing out of the console window
338 	if (dlgcode & DLGC_WANTALLKEYS)
339 		return FALSE;
340 #endif
341 	switch (msg.wParam) {
342 	case VK_RIGHT:
343 	case VK_DOWN:
344 		if (dlgcode & DLGC_WANTARROWS)
345 			return FALSE;
346 		dst = next;
347 		break;
348 
349 	case VK_LEFT:
350 	case VK_UP:
351 		if (dlgcode & DLGC_WANTARROWS)
352 			return FALSE;
353 		dst = prev;
354 		break;
355 
356 	case VK_TAB:
357 		if (dlgcode & DLGC_WANTTAB)
358 			return FALSE;
359 		if (GetKeyState(VK_SHIFT) & 0x8000) // Shift-Tab
360 			dst = prev;
361 		else
362 			dst = next;
363 		break;
364 	}
365 
366 	if (dst == 0)
367 		return FALSE;
368 
369 	SetFocus(dst);
370 	return TRUE;
371 }
372 
373 void
374 RootWindow::progress()
375 {
376 	SendMessage(_progress_bar->_window, PBM_STEPIT, 0, 0);
377 }
378 
379 void
380 RootWindow::unprogress()
381 {
382 	SendMessage(_progress_bar->_window, PBM_SETPOS, 0, 0);
383 }
384 
385 //
386 // BOOT button
387 //
388 BOOL
389 BootButton::create(LPCREATESTRUCT aux)
390 {
391 	int cx = _root._button_width;
392 	int cy = _root._button_height;
393 
394 	_window = CreateWindow(TEXT("BUTTON"), TEXT("Boot"),
395 	    BS_PUSHBUTTON | BS_NOTIFY |
396 	    WS_VISIBLE | WS_CHILD | WS_TABSTOP,
397 	    _rect.left, _rect.top, cx, cy, _parent_window,
398 	    reinterpret_cast <HMENU>(IDC_BOOTBUTTON),
399 	    aux->hInstance,
400 	    NULL);
401 
402 	return IsWindow(_window) ? TRUE : FALSE;
403 }
404 
405 //
406 // CANCEL button
407 //
408 BOOL
409 CancelButton::create(LPCREATESTRUCT aux)
410 {
411 	int cx = _root._button_width;
412 	int cy = _root._button_height;
413 	int x = _rect.right - _root._button_width;
414 
415 	_window = CreateWindow(TEXT("BUTTON"), TEXT("Cancel"),
416 	    BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP |
417 	    WS_VISIBLE | WS_CHILD,
418 	    x, _rect.top, cx, cy, _parent_window,
419 	    reinterpret_cast <HMENU>(IDC_CANCELBUTTON),
420 	    aux->hInstance,
421 	    NULL);
422 
423 	return IsWindow(_window) ? TRUE : FALSE;
424 }
425 
426 //
427 // PROGRESS BAR
428 //
429 BOOL
430 ProgressBar::create(LPCREATESTRUCT aux)
431 {
432 	int cx = _rect.right - _rect.left - _root._button_width * 2;
433 	int cy = _root._button_height;
434 	int x = _rect.left + _root._button_width;
435 	_window = CreateWindowEx(WS_EX_CLIENTEDGE,
436 	    PROGRESS_CLASS, TEXT(""),
437 	    PBS_SMOOTH | WS_VISIBLE | WS_CHILD,
438 	    x, _rect.top, cx, cy, _parent_window,
439 	    reinterpret_cast <HMENU>(IDC_PROGRESSBAR),
440 	    aux->hInstance, NULL);
441 	SendMessage(_window, PBM_SETRANGE, 0, MAKELPARAM(0, 11));
442 	SendMessage(_window, PBM_SETSTEP, 1, 0);
443 	SendMessage(_window, PBM_SETPOS, 0, 0);
444 
445 	return IsWindow(_window) ? TRUE : FALSE;
446 }
447