#include <hpcmenu.h>
#include <menu/window.h>
#include <menu/tabwindow.h>
#include <menu/rootwindow.h>
#include <res/resource.h>
#include "../binary/build_number.h"
#include <console.h>
RootWindow::RootWindow(HpcBootApp &app)
: Window(app)
{
_boot_button = 0;
_base = 0;
_main = 0;
_option = 0;
_console = 0;
}
RootWindow::~RootWindow()
{
if (_boot_button)
delete _boot_button;
if (_cancel_button)
delete _cancel_button;
if (_progress_bar)
delete _progress_bar;
if (_main)
delete _main;
if (_option)
delete _option;
if (_console)
delete _console;
if (_base)
delete _base;
}
BOOL
RootWindow::create(LPCREATESTRUCT aux)
{
TCHAR app_name[32];
HINSTANCE inst = _app._instance;
TCHAR *wc_name = reinterpret_cast <TCHAR *>
(LoadString(inst, IDS_HPCMENU, 0, 0));
wsprintf(app_name, TEXT("%s Build %d"), wc_name, HPCBOOT_BUILD_NUMBER);
_window = CreateWindow(wc_name, app_name, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
0, 0, inst, this);
if (!_window)
return FALSE;
HpcMenuInterface &menu = HpcMenuInterface::Instance();
if (menu._pref.auto_boot > 0)
SetTimer(_window, IDD_TIMER, menu._pref.auto_boot * 1000, 0);
ShowWindow(_window, SW_SHOW);
UpdateWindow(_window);
return TRUE;
}
BOOL
RootWindow::proc(HWND w, UINT msg, WPARAM wparam, LPARAM lparam)
{
LPCREATESTRUCT aux = reinterpret_cast <LPCREATESTRUCT>(lparam);
HpcMenuInterface &menu = HpcMenuInterface::Instance();
switch(msg) {
default:
return FALSE;
case WM_CREATE:
WMCreate(w, aux);
break;
case WM_PAINT:
WMPaint(w, aux);
break;
case WM_ENTERMENULOOP:
SaveFocus();
break;
case WM_EXITMENULOOP:
RestoreFocus();
break;
case WM_ACTIVATE:
if ((UINT)LOWORD(wparam) == WA_INACTIVE)
SaveFocus();
else
RestoreFocus();
break;
case WM_NOTIFY:
{
NMHDR *notify = reinterpret_cast <NMHDR *>(lparam);
int tab_id = TabCtrl_GetCurSel(_base->_window);
TC_ITEM tc_item;
tc_item.mask = TCIF_PARAM;
TabCtrl_GetItem(_base->_window, tab_id, &tc_item);
TabWindow *tab = reinterpret_cast <TabWindow *>
(tc_item.lParam);
switch(notify->code) {
case TCN_SELCHANGING:
tab->hide();
break;
case TCN_SELCHANGE:
tab->show();
break;
case TCN_KEYDOWN: {
NMTCKEYDOWN *key = reinterpret_cast
<NMTCKEYDOWN *>(lparam);
return _base->focusManagerHook(key->wVKey, key->flags,
_cancel_button->_window);
}
}
}
break;
case WM_TIMER:
disableTimer();
goto boot;
case WM_COMMAND:
switch(wparam)
{
case IDC_BOOTBUTTON:
menu.get_options();
if (menu._pref.safety_message) {
UINT mb_icon = menu._pref.pause_before_boot ?
MB_ICONQUESTION : MB_ICONWARNING;
if (MessageBox(_window,
TEXT("Data in memory will be lost.\nAre you sure?"),
TEXT("WARNING"),
mb_icon | MB_YESNO) != IDYES)
break;
UpdateWindow(_window);
}
boot:
SendMessage(_progress_bar->_window, PBM_SETPOS, 0, 0);
menu.print(TEXT("BOOT START\n"));
menu.get_options();
menu.save();
menu.boot();
break;
case IDC_PROGRESSBAR:
break;
case IDC_CANCELBUTTON:
PostQuitMessage(0);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return TRUE;
}
void
RootWindow::SaveFocus() {
_saved_focus = GetFocus();
}
void
RootWindow::RestoreFocus() {
SetFocus(IsWindowEnabled(_saved_focus) ?
_saved_focus : _boot_button->_window);
}
void
RootWindow::WMPaint(HWND w, LPCREATESTRUCT aux)
{
PAINTSTRUCT ps;
BeginPaint(w, &ps);
EndPaint(w, &ps);
}
void
RootWindow::WMCreate(HWND w, LPCREATESTRUCT aux)
{
int cmdbar_height;
_window = w;
_app._cmdbar = CommandBar_Create(aux->hInstance, w, IDC_CMDBAR);
CommandBar_AddAdornments(_app._cmdbar, 0, 0);
cmdbar_height = CommandBar_Height(_app._cmdbar);
_button_height = cmdbar_height;
_button_width = BOOT_BUTTON_WIDTH;
HDC hdc = GetDC(0);
if (GetDeviceCaps(hdc, HORZRES) > 320)
_button_width += _button_width/2;
ReleaseDC(0, hdc);
RECT rect;
GetClientRect(w, &rect);
rect.top += cmdbar_height;
_boot_button = new BootButton(_app, *this, rect);
_boot_button->create(aux);
_cancel_button = new CancelButton(_app, *this, rect);
_cancel_button->create(aux);
_progress_bar = new ProgressBar(_app, *this, rect);
_progress_bar->create(aux);
HpcMenuInterface::Instance()._root = this;
rect.top += _button_height;
_base = new TabWindowBase(_app, w, rect, IDC_BASE);
_base->create(aux);
_main = _base->boot(IDC_BASE_MAIN);
_option = _base->boot(IDC_BASE_OPTION);
_console = _base->boot(IDC_BASE_CONSOLE);
_main->show();
SetFocus(_boot_button->_window);
return;
}
void
RootWindow::disableTimer()
{
KillTimer(_window, IDD_TIMER);
}
BOOL
RootWindow::isDialogMessage(MSG &msg)
{
HWND tab_window;
if (_main && IsWindowVisible(_main->_window))
tab_window = _main->_window;
else if (_option && IsWindowVisible(_option->_window))
tab_window = _option->_window;
else if (_console && IsWindowVisible(_console->_window))
tab_window = _console->_window;
if (focusManagerHook(msg, tab_window))
return TRUE;
return IsDialogMessage(tab_window, &msg);
}
BOOL
RootWindow::focusManagerHook(MSG &msg, HWND tab_window)
{
HWND next, prev;
HWND dst = 0;
LRESULT dlgcode = 0;
if (msg.message != WM_KEYDOWN)
return FALSE;
if (msg.hwnd == _boot_button->_window) {
next = _cancel_button->_window;
prev = _base->_window;
} else if (msg.hwnd == _cancel_button->_window) {
next = _base->_window;
prev = _boot_button->_window;
} else if (tab_window == 0) {
return FALSE;
} else {
HWND last = GetNextDlgTabItem(tab_window, NULL, TRUE);
if (last == NULL ||
!(last == msg.hwnd || IsChild(last, msg.hwnd)))
return FALSE;
dlgcode = SendMessage(last, WM_GETDLGCODE, NULL, (LPARAM)&msg);
next = _base->_window;
prev = 0;
}
#if 0
if (dlgcode & DLGC_WANTALLKEYS)
return FALSE;
#endif
switch (msg.wParam) {
case VK_RIGHT:
case VK_DOWN:
if (dlgcode & DLGC_WANTARROWS)
return FALSE;
dst = next;
break;
case VK_LEFT:
case VK_UP:
if (dlgcode & DLGC_WANTARROWS)
return FALSE;
dst = prev;
break;
case VK_TAB:
if (dlgcode & DLGC_WANTTAB)
return FALSE;
if (GetKeyState(VK_SHIFT) & 0x8000)
dst = prev;
else
dst = next;
break;
}
if (dst == 0)
return FALSE;
SetFocus(dst);
return TRUE;
}
void
RootWindow::progress(const char *msg)
{
if (msg)
Console::Instance()->print(TEXT("[progress] %S\n"), msg);
SendMessage(_progress_bar->_window, PBM_STEPIT, 0, 0);
}
void
RootWindow::unprogress()
{
SendMessage(_progress_bar->_window, PBM_SETPOS, 0, 0);
}
BOOL
BootButton::create(LPCREATESTRUCT aux)
{
int cx = _root._button_width;
int cy = _root._button_height;
_window = CreateWindow(TEXT("BUTTON"), TEXT("Boot"),
BS_PUSHBUTTON | BS_NOTIFY |
WS_VISIBLE | WS_CHILD | WS_TABSTOP,
_rect.left, _rect.top, cx, cy, _parent_window,
reinterpret_cast <HMENU>(IDC_BOOTBUTTON),
aux->hInstance,
NULL);
return IsWindow(_window) ? TRUE : FALSE;
}
BOOL
CancelButton::create(LPCREATESTRUCT aux)
{
int cx = _root._button_width;
int cy = _root._button_height;
int x = _rect.right - _root._button_width;
_window = CreateWindow(TEXT("BUTTON"), TEXT("Cancel"),
BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP |
WS_VISIBLE | WS_CHILD,
x, _rect.top, cx, cy, _parent_window,
reinterpret_cast <HMENU>(IDC_CANCELBUTTON),
aux->hInstance,
NULL);
return IsWindow(_window) ? TRUE : FALSE;
}
BOOL
ProgressBar::create(LPCREATESTRUCT aux)
{
int cx = _rect.right - _rect.left - _root._button_width * 2;
int cy = _root._button_height;
int x = _rect.left + _root._button_width;
_window = CreateWindowEx(WS_EX_CLIENTEDGE,
PROGRESS_CLASS, TEXT(""),
PBS_SMOOTH | WS_VISIBLE | WS_CHILD,
x, _rect.top, cx, cy, _parent_window,
reinterpret_cast <HMENU>(IDC_PROGRESSBAR),
aux->hInstance, NULL);
SendMessage(_window, PBM_SETRANGE, 0, MAKELPARAM(0, 11));
SendMessage(_window, PBM_SETSTEP, 1, 0);
SendMessage(_window, PBM_SETPOS, 0, 0);
return IsWindow(_window) ? TRUE : FALSE;
}