| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include "gui.h" |
| 19 |
#include "systest.h" |
| 20 |
#include <windows.h> |
| 21 |
|
| 22 |
|
| 23 |
#include "gui.h" |
| 24 |
#include <pjlib.h> |
| 25 |
#include <windows.h> |
| 26 |
#include <winuserm.h> |
| 27 |
#include <aygshell.h> |
| 28 |
|
| 29 |
#define MAINWINDOWCLASS TEXT("SysTestDlg") |
| 30 |
#define MAINWINDOWTITLE TEXT("PJSYSTEST") |
| 31 |
|
| 32 |
typedef struct menu_handler_t { |
| 33 |
UINT id; |
| 34 |
gui_menu_handler handler; |
| 35 |
} menu_handler_t; |
| 36 |
|
| 37 |
static HINSTANCE g_hInst; |
| 38 |
static HWND g_hWndMenuBar; |
| 39 |
static HWND g_hWndMain; |
| 40 |
static HWND g_hWndLog; |
| 41 |
static pj_thread_t *g_log_thread; |
| 42 |
static gui_menu *g_menu; |
| 43 |
static unsigned g_menu_handler_cnt; |
| 44 |
static menu_handler_t g_menu_handlers[64]; |
| 45 |
|
| 46 |
static pj_log_func *g_log_writer_orig; |
| 47 |
|
| 48 |
static pj_status_t gui_update_menu(gui_menu *menu); |
| 49 |
|
| 50 |
static void log_writer(int level, const char *buffer, int len) |
| 51 |
{ |
| 52 |
wchar_t buf[512]; |
| 53 |
int cur_len; |
| 54 |
|
| 55 |
PJ_UNUSED_ARG(level); |
| 56 |
|
| 57 |
pj_ansi_to_unicode(buffer, len, buf, 512); |
| 58 |
|
| 59 |
if (!g_hWndLog) |
| 60 |
return; |
| 61 |
|
| 62 |
|
| 63 |
if (g_log_thread == pj_thread_this()) { |
| 64 |
cur_len = (int)SendMessage(g_hWndLog, WM_GETTEXTLENGTH, 0, 0); |
| 65 |
SendMessage(g_hWndLog, EM_SETSEL, (WPARAM)cur_len, (LPARAM)cur_len); |
| 66 |
SendMessage(g_hWndLog, EM_REPLACESEL, (WPARAM)0, (LPARAM)buf); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
if (g_log_writer_orig) |
| 71 |
(*g_log_writer_orig)(level, buffer, len); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
static BOOL handle_menu(UINT id) |
| 78 |
{ |
| 79 |
unsigned i; |
| 80 |
|
| 81 |
for (i = 0; i < g_menu_handler_cnt; ++i) { |
| 82 |
if (g_menu_handlers[i].id == id) { |
| 83 |
|
| 84 |
(*g_menu_handlers[i].handler)(); |
| 85 |
return TRUE; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return FALSE; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
static UINT generate_submenu(HMENU parent, UINT id_start, gui_menu *menu) |
| 94 |
{ |
| 95 |
unsigned i; |
| 96 |
UINT id = id_start; |
| 97 |
|
| 98 |
if (!menu) |
| 99 |
return id; |
| 100 |
|
| 101 |
|
| 102 |
for (i = 0; i < menu->submenu_cnt; ++i) { |
| 103 |
|
| 104 |
if (menu->submenus[i] == NULL) { |
| 105 |
|
| 106 |
|
| 107 |
AppendMenu(parent, MF_SEPARATOR, 0, 0); |
| 108 |
|
| 109 |
} else if (menu->submenus[i]->submenu_cnt != 0) { |
| 110 |
|
| 111 |
|
| 112 |
HMENU hMenu; |
| 113 |
wchar_t buf[64]; |
| 114 |
|
| 115 |
pj_ansi_to_unicode(menu->submenus[i]->title, |
| 116 |
pj_ansi_strlen(menu->submenus[i]->title), |
| 117 |
buf, 64); |
| 118 |
|
| 119 |
hMenu = CreatePopupMenu(); |
| 120 |
AppendMenu(parent, MF_STRING|MF_ENABLED|MF_POPUP, (UINT)hMenu, buf); |
| 121 |
id = generate_submenu(hMenu, id, menu->submenus[i]); |
| 122 |
|
| 123 |
} else { |
| 124 |
|
| 125 |
|
| 126 |
wchar_t buf[64]; |
| 127 |
|
| 128 |
pj_ansi_to_unicode(menu->submenus[i]->title, |
| 129 |
pj_ansi_strlen(menu->submenus[i]->title), |
| 130 |
buf, 64); |
| 131 |
|
| 132 |
AppendMenu(parent, MF_STRING, id, buf); |
| 133 |
|
| 134 |
if (menu->submenus[i]->handler) { |
| 135 |
g_menu_handlers[g_menu_handler_cnt].id = id; |
| 136 |
g_menu_handlers[g_menu_handler_cnt].handler = |
| 137 |
menu->submenus[i]->handler; |
| 138 |
++g_menu_handler_cnt; |
| 139 |
} |
| 140 |
|
| 141 |
++id; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
return id; |
| 146 |
} |
| 147 |
|
| 148 |
BOOL InitDialog() |
| 149 |
{ |
| 150 |
|
| 151 |
if (gui_update_menu(g_menu) != PJ_SUCCESS) |
| 152 |
return FALSE; |
| 153 |
|
| 154 |
return TRUE; |
| 155 |
} |
| 156 |
|
| 157 |
LRESULT CALLBACK DialogProc(const HWND hWnd, |
| 158 |
const UINT Msg, |
| 159 |
const WPARAM wParam, |
| 160 |
const LPARAM lParam) |
| 161 |
{ |
| 162 |
LRESULT res = 0; |
| 163 |
|
| 164 |
switch (Msg) { |
| 165 |
case WM_CREATE: |
| 166 |
g_hWndMain = hWnd; |
| 167 |
if (FALSE == InitDialog()){ |
| 168 |
DestroyWindow(g_hWndMain); |
| 169 |
} |
| 170 |
break; |
| 171 |
|
| 172 |
case WM_CLOSE: |
| 173 |
DestroyWindow(g_hWndMain); |
| 174 |
break; |
| 175 |
|
| 176 |
case WM_DESTROY: |
| 177 |
if (g_hWndMenuBar) |
| 178 |
DestroyWindow(g_hWndMenuBar); |
| 179 |
g_hWndMenuBar = NULL; |
| 180 |
g_hWndMain = NULL; |
| 181 |
PostQuitMessage(0); |
| 182 |
break; |
| 183 |
|
| 184 |
case WM_HOTKEY: |
| 185 |
|
| 186 |
if (VK_TBACK == HIWORD(lParam) && (0 != (MOD_KEYUP & LOWORD(lParam)))) { |
| 187 |
DestroyWindow(g_hWndMain); |
| 188 |
} else { |
| 189 |
return DefWindowProc(hWnd, Msg, wParam, lParam); |
| 190 |
} |
| 191 |
break; |
| 192 |
|
| 193 |
case WM_COMMAND: |
| 194 |
res = handle_menu(LOWORD(wParam)); |
| 195 |
break; |
| 196 |
|
| 197 |
default: |
| 198 |
return DefWindowProc(hWnd, Msg, wParam, lParam); |
| 199 |
} |
| 200 |
|
| 201 |
return res; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
pj_status_t gui_init(gui_menu *menu) |
| 208 |
{ |
| 209 |
WNDCLASS wc; |
| 210 |
HWND hWnd = NULL; |
| 211 |
RECT r; |
| 212 |
DWORD dwStyle; |
| 213 |
|
| 214 |
pj_status_t status = PJ_SUCCESS; |
| 215 |
|
| 216 |
|
| 217 |
hWnd = FindWindow(MAINWINDOWCLASS, MAINWINDOWTITLE); |
| 218 |
|
| 219 |
if (NULL != hWnd) { |
| 220 |
SetForegroundWindow(hWnd); |
| 221 |
return status; |
| 222 |
} |
| 223 |
|
| 224 |
g_menu = menu; |
| 225 |
|
| 226 |
wc.style = CS_HREDRAW | CS_VREDRAW; |
| 227 |
wc.lpfnWndProc = (WNDPROC)DialogProc; |
| 228 |
wc.cbClsExtra = 0; |
| 229 |
wc.cbWndExtra = 0; |
| 230 |
wc.hInstance = g_hInst; |
| 231 |
wc.hIcon = 0; |
| 232 |
wc.hCursor = 0; |
| 233 |
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); |
| 234 |
wc.lpszMenuName = 0; |
| 235 |
wc.lpszClassName = MAINWINDOWCLASS; |
| 236 |
|
| 237 |
if (!RegisterClass(&wc) != 0) { |
| 238 |
DWORD err = GetLastError(); |
| 239 |
return PJ_RETURN_OS_ERROR(err); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
g_hWndMain = CreateWindow(MAINWINDOWCLASS, MAINWINDOWTITLE, |
| 244 |
WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, |
| 245 |
CW_USEDEFAULT, CW_USEDEFAULT, |
| 246 |
(HWND)NULL, NULL, g_hInst, (LPSTR)NULL); |
| 247 |
|
| 248 |
|
| 249 |
GetClientRect(g_hWndMain, &r); |
| 250 |
dwStyle = WS_CHILD | WS_VISIBLE | WS_VSCROLL | |
| 251 |
ES_MULTILINE | ES_READONLY | ES_AUTOVSCROLL | ES_LEFT; |
| 252 |
g_hWndLog = CreateWindow( |
| 253 |
TEXT("EDIT"), |
| 254 |
NULL, |
| 255 |
dwStyle, |
| 256 |
0, |
| 257 |
0, |
| 258 |
r.right-r.left, |
| 259 |
|
| 260 |
r.bottom-r.top, |
| 261 |
|
| 262 |
g_hWndMain, |
| 263 |
(HMENU) 0, |
| 264 |
g_hInst, |
| 265 |
NULL); |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
if (g_hWndMenuBar) { |
| 270 |
RECT r_menu = {0}; |
| 271 |
|
| 272 |
GetWindowRect(g_hWndLog, &r); |
| 273 |
GetWindowRect(g_hWndMenuBar, &r_menu); |
| 274 |
if (r.bottom > r_menu.top) { |
| 275 |
MoveWindow(g_hWndLog, 0, 0, r.right-r.left, |
| 276 |
(r.bottom-r.top)-(r_menu.bottom-r_menu.top), TRUE); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
SetFocus(g_hWndLog); |
| 282 |
|
| 283 |
|
| 284 |
g_log_thread = pj_thread_this(); |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
g_log_writer_orig = pj_log_get_log_func(); |
| 293 |
pj_log_set_log_func(&log_writer); |
| 294 |
|
| 295 |
return status; |
| 296 |
} |
| 297 |
|
| 298 |
static pj_status_t gui_update_menu(gui_menu *menu) |
| 299 |
{ |
| 300 |
enum { MENU_ID_START = 50000 }; |
| 301 |
UINT id_start = MENU_ID_START; |
| 302 |
HMENU hRootMenu; |
| 303 |
SHMENUBARINFO mbi; |
| 304 |
|
| 305 |
|
| 306 |
if (g_hWndMenuBar) { |
| 307 |
DestroyWindow(g_hWndMenuBar); |
| 308 |
g_hWndMenuBar = NULL; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
g_menu_handler_cnt = 0; |
| 313 |
|
| 314 |
|
| 315 |
pj_assert(menu->submenu_cnt <= 2); |
| 316 |
|
| 317 |
|
| 318 |
hRootMenu = CreateMenu(); |
| 319 |
id_start = generate_submenu(hRootMenu, id_start, menu); |
| 320 |
|
| 321 |
|
| 322 |
ZeroMemory(&mbi, sizeof(SHMENUBARINFO)); |
| 323 |
mbi.cbSize = sizeof(SHMENUBARINFO); |
| 324 |
mbi.hwndParent = g_hWndMain; |
| 325 |
mbi.dwFlags = SHCMBF_HIDESIPBUTTON|SHCMBF_HMENU; |
| 326 |
mbi.nToolBarId = (UINT)hRootMenu; |
| 327 |
mbi.hInstRes = g_hInst; |
| 328 |
|
| 329 |
if (FALSE == SHCreateMenuBar(&mbi)) { |
| 330 |
DWORD err = GetLastError(); |
| 331 |
return PJ_RETURN_OS_ERROR(err); |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
g_hWndMenuBar = mbi.hwndMB; |
| 336 |
|
| 337 |
|
| 338 |
g_menu = menu; |
| 339 |
|
| 340 |
|
| 341 |
DrawMenuBar(g_hWndMain); |
| 342 |
ShowWindow(g_hWndMenuBar, SW_SHOW); |
| 343 |
|
| 344 |
|
| 345 |
SendMessage(g_hWndMenuBar, SHCMBM_OVERRIDEKEY, VK_TBACK, |
| 346 |
MAKELPARAM(SHMBOF_NODEFAULT | SHMBOF_NOTIFY, |
| 347 |
SHMBOF_NODEFAULT | SHMBOF_NOTIFY)); |
| 348 |
|
| 349 |
|
| 350 |
return PJ_SUCCESS; |
| 351 |
} |
| 352 |
|
| 353 |
enum gui_key gui_msgbox(const char *title, const char *message, enum gui_flag flag) |
| 354 |
{ |
| 355 |
wchar_t buf_title[64]; |
| 356 |
wchar_t buf_msg[512]; |
| 357 |
UINT wflag = 0; |
| 358 |
int retcode; |
| 359 |
|
| 360 |
pj_ansi_to_unicode(title, pj_ansi_strlen(title), buf_title, 64); |
| 361 |
pj_ansi_to_unicode(message, pj_ansi_strlen(message), buf_msg, 512); |
| 362 |
|
| 363 |
switch (flag) { |
| 364 |
case WITH_OK: |
| 365 |
wflag = MB_OK; |
| 366 |
break; |
| 367 |
case WITH_YESNO: |
| 368 |
wflag = MB_YESNO; |
| 369 |
break; |
| 370 |
case WITH_OKCANCEL: |
| 371 |
wflag = MB_OKCANCEL; |
| 372 |
break; |
| 373 |
} |
| 374 |
|
| 375 |
retcode = MessageBox(g_hWndMain, buf_msg, buf_title, wflag); |
| 376 |
|
| 377 |
switch (retcode) { |
| 378 |
case IDOK: |
| 379 |
return KEY_OK; |
| 380 |
case IDYES: |
| 381 |
return KEY_YES; |
| 382 |
case IDNO: |
| 383 |
return KEY_NO; |
| 384 |
default: |
| 385 |
return KEY_CANCEL; |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
void gui_sleep(unsigned sec) |
| 390 |
{ |
| 391 |
pj_thread_sleep(sec * 1000); |
| 392 |
} |
| 393 |
|
| 394 |
pj_status_t gui_start(gui_menu *menu) |
| 395 |
{ |
| 396 |
MSG msg; |
| 397 |
|
| 398 |
PJ_UNUSED_ARG(menu); |
| 399 |
|
| 400 |
while (GetMessage(&msg, NULL, 0, 0)) { |
| 401 |
TranslateMessage(&msg); |
| 402 |
DispatchMessage(&msg); |
| 403 |
} |
| 404 |
|
| 405 |
return (msg.wParam); |
| 406 |
} |
| 407 |
|
| 408 |
void gui_destroy(void) |
| 409 |
{ |
| 410 |
if (g_hWndMain) { |
| 411 |
DestroyWindow(g_hWndMain); |
| 412 |
g_hWndMain = NULL; |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
int WINAPI WinMain( |
| 418 |
HINSTANCE hInstance, |
| 419 |
HINSTANCE hPrevInstance, |
| 420 |
LPWSTR lpCmdLine, |
| 421 |
int nShowCmd |
| 422 |
) |
| 423 |
{ |
| 424 |
int status; |
| 425 |
|
| 426 |
PJ_UNUSED_ARG(hPrevInstance); |
| 427 |
PJ_UNUSED_ARG(lpCmdLine); |
| 428 |
PJ_UNUSED_ARG(nShowCmd); |
| 429 |
|
| 430 |
|
| 431 |
g_hInst = hInstance; |
| 432 |
|
| 433 |
status = systest_init(); |
| 434 |
if (status != 0) |
| 435 |
goto on_return; |
| 436 |
|
| 437 |
status = systest_run(); |
| 438 |
|
| 439 |
on_return: |
| 440 |
systest_deinit(); |
| 441 |
|
| 442 |
return status; |
| 443 |
} |