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