1 /* NetHack 3.6 mhmain.c $NHDT-Date: 1432512811 2015/05/25 00:13:31 $ $NHDT-Branch: master $:$NHDT-Revision: 1.62 $ */
2 /* Copyright (C) 2001 by Alex Kompel */
3 /* NetHack may be freely redistributed. See license for details. */
5 /* JNetHack Copyright */
6 /* (c) Issei Numata, Naoki Hamada, Shigehiro Miyashita, 1994-2000 */
7 /* For 3.4-, Copyright (c) SHIRAKATA Kentaro, 2002-2016 */
8 /* JNetHack may be freely redistributed. See license for details. */
13 #include "patchlevel.h"
23 typedef struct mswin_nethack_main_window {
25 } NHMainWindow, *PNHMainWindow;
27 extern winid WIN_STATUS;
29 static TCHAR szMainWindowClass[] = TEXT("MSNHMainWndClass");
30 static TCHAR szTitle[MAX_LOADSTRING];
31 extern void mswin_display_splash_window(BOOL);
33 LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
34 LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
35 static LRESULT onWMCommand(HWND hWnd, WPARAM wParam, LPARAM lParam);
36 static void onMSNHCommand(HWND hWnd, WPARAM wParam, LPARAM lParam);
37 static void register_main_window_class(void);
38 static int menuid2mapmode(int menuid);
39 static int mapmode2menuid(int map_mode);
40 static void nhlock_windows(BOOL lock);
41 static char *nh_compose_ascii_screenshot();
42 static void mswin_apply_window_style_all();
43 // returns strdup() created pointer - callee assumes the ownership
46 mswin_init_main_window()
48 static int run_once = 0;
52 /* register window class */
54 LoadString(GetNHApp()->hApp, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
55 register_main_window_class();
59 /* create the main window */
61 CreateWindow(szMainWindowClass, /* registered class name */
62 szTitle, /* window name */
63 WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, /* window style */
64 CW_USEDEFAULT, /* horizontal position of window */
65 CW_USEDEFAULT, /* vertical position of window */
66 CW_USEDEFAULT, /* window width */
67 CW_USEDEFAULT, /* window height */
68 NULL, /* handle to parent or owner window */
69 NULL, /* menu handle or child identifier */
70 GetNHApp()->hApp, /* handle to application instance */
71 NULL /* window-creation data */
75 panic("Cannot create main window");
77 if (GetNHApp()->regMainMinX != CW_USEDEFAULT) {
78 wp.length = sizeof(wp);
79 wp.showCmd = GetNHApp()->regMainShowState;
81 wp.ptMinPosition.x = GetNHApp()->regMainMinX;
82 wp.ptMinPosition.y = GetNHApp()->regMainMinY;
84 wp.ptMaxPosition.x = GetNHApp()->regMainMaxX;
85 wp.ptMaxPosition.y = GetNHApp()->regMainMaxY;
87 wp.rcNormalPosition.left = GetNHApp()->regMainLeft;
88 wp.rcNormalPosition.top = GetNHApp()->regMainTop;
89 wp.rcNormalPosition.right = GetNHApp()->regMainRight;
90 wp.rcNormalPosition.bottom = GetNHApp()->regMainBottom;
91 SetWindowPlacement(ret, &wp);
93 ShowWindow(ret, SW_SHOWDEFAULT);
100 register_main_window_class()
104 ZeroMemory(&wcex, sizeof(wcex));
105 wcex.style = CS_HREDRAW | CS_VREDRAW;
106 wcex.lpfnWndProc = (WNDPROC) MainWndProc;
109 wcex.hInstance = GetNHApp()->hApp;
110 wcex.hIcon = LoadIcon(GetNHApp()->hApp, (LPCTSTR) IDI_NETHACKW);
111 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
112 wcex.hbrBackground = CreateSolidBrush(RGB(0, 0, 0));
113 wcex.lpszMenuName = (TCHAR *) IDC_NETHACKW;
114 wcex.lpszClassName = szMainWindowClass;
116 RegisterClass(&wcex);
120 * Keypad keys are translated to the normal values below.
121 * Shifted keypad keys are translated to the
122 * shift values below.
142 static const unsigned char
143 /* normal, shift, control */
144 keypad[KEY_LAST][3] =
146 { 'y', 'Y', C('y') }, /* 7 */
147 { 'k', 'K', C('k') }, /* 8 */
148 { 'u', 'U', C('u') }, /* 9 */
149 { 'm', C('p'), C('p') }, /* - */
150 { 'h', 'H', C('h') }, /* 4 */
151 { 'g', 'G', 'g' }, /* 5 */
152 { 'l', 'L', C('l') }, /* 6 */
153 { '+', 'P', C('p') }, /* + */
154 { 'b', 'B', C('b') }, /* 1 */
155 { 'j', 'J', C('j') }, /* 2 */
156 { 'n', 'N', C('n') }, /* 3 */
157 { 'i', 'I', C('i') }, /* Ins */
158 { '.', ':', ':' } /* Del */
160 numpad[KEY_LAST][3] = {
161 { '7', M('7'), '7' }, /* 7 */
162 { '8', M('8'), '8' }, /* 8 */
163 { '9', M('9'), '9' }, /* 9 */
164 { 'm', C('p'), C('p') }, /* - */
165 { '4', M('4'), '4' }, /* 4 */
166 { '5', M('5'), '5' }, /* 5 */
167 { '6', M('6'), '6' }, /* 6 */
168 { '+', 'P', C('p') }, /* + */
169 { '1', M('1'), '1' }, /* 1 */
170 { '2', M('2'), '2' }, /* 2 */
171 { '3', M('3'), '3' }, /* 3 */
172 { '0', M('0'), '0' }, /* Ins */
173 { '.', ':', ':' } /* Del */
176 #define STATEON(x) ((GetKeyState(x) & 0xFFFE) != 0)
177 #define KEYTABLE_REGULAR(x) ((iflags.num_pad ? numpad : keypad)[x][0])
178 #define KEYTABLE_SHIFT(x) ((iflags.num_pad ? numpad : keypad)[x][1])
179 #define KEYTABLE(x) \
180 (STATEON(VK_SHIFT) ? KEYTABLE_SHIFT(x) : KEYTABLE_REGULAR(x))
182 static const char *extendedlist = "acdefijlmnopqrstuvw?2";
185 static const char scanmap[] = {
187 '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 0, 0, 0, 0, 'q', 'w',
188 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', 0, 'a', 's', 'd',
189 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 0, '\\', 'z', 'x', 'c', 'v',
190 'b', 'n', 'm', ',', '.', '?' /* ... */
194 // FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
196 // PURPOSE: Processes messages for the main window.
199 MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
203 static int doublebyte = 0;
208 /* set window data */
209 data = (PNHMainWindow) malloc(sizeof(NHMainWindow));
211 panic("out of memory");
212 ZeroMemory(data, sizeof(NHMainWindow));
213 data->mapAcsiiModeSave = MAP_MODE_ASCII12x16;
214 SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) data);
216 /* update menu items */
218 GetMenu(hWnd), IDM_SETTING_LOCKWINDOWS,
220 | (GetNHApp()->bWindowsLocked ? MF_CHECKED : MF_UNCHECKED));
222 CheckMenuItem(GetMenu(hWnd), IDM_SETTING_AUTOLAYOUT,
223 GetNHApp()->bAutoLayout ? MF_CHECKED : MF_UNCHECKED);
225 /* store handle to the mane menu in the application record */
226 GetNHApp()->hMainWnd = hWnd;
229 case WM_MSNH_COMMAND:
230 onMSNHCommand(hWnd, wParam, lParam);
234 data = (PNHMainWindow) GetWindowLongPtr(hWnd, GWLP_USERDATA);
236 /* translate arrow keys into nethack commands */
239 if (STATEON(VK_CONTROL)) {
240 /* scroll map window one line left */
241 SendMessage(mswin_hwnd_from_winid(WIN_MAP), WM_HSCROLL,
242 MAKEWPARAM(SB_LINEUP, 0), (LPARAM) NULL);
244 NHEVENT_KBD(KEYTABLE(KEY_W));
249 if (STATEON(VK_CONTROL)) {
250 /* scroll map window one line right */
251 SendMessage(mswin_hwnd_from_winid(WIN_MAP), WM_HSCROLL,
252 MAKEWPARAM(SB_LINEDOWN, 0), (LPARAM) NULL);
254 NHEVENT_KBD(KEYTABLE(KEY_E));
259 if (STATEON(VK_CONTROL)) {
260 /* scroll map window one line up */
261 SendMessage(mswin_hwnd_from_winid(WIN_MAP), WM_VSCROLL,
262 MAKEWPARAM(SB_LINEUP, 0), (LPARAM) NULL);
264 NHEVENT_KBD(KEYTABLE(KEY_N));
269 if (STATEON(VK_CONTROL)) {
270 /* scroll map window one line down */
271 SendMessage(mswin_hwnd_from_winid(WIN_MAP), WM_VSCROLL,
272 MAKEWPARAM(SB_LINEDOWN, 0), (LPARAM) NULL);
274 NHEVENT_KBD(KEYTABLE(KEY_S));
279 if (STATEON(VK_CONTROL)) {
280 /* scroll map window to upper left corner */
281 SendMessage(mswin_hwnd_from_winid(WIN_MAP), WM_VSCROLL,
282 MAKEWPARAM(SB_THUMBTRACK, 0), (LPARAM) NULL);
284 SendMessage(mswin_hwnd_from_winid(WIN_MAP), WM_HSCROLL,
285 MAKEWPARAM(SB_THUMBTRACK, 0), (LPARAM) NULL);
287 NHEVENT_KBD(KEYTABLE(KEY_NW));
292 if (STATEON(VK_CONTROL)) {
293 /* scroll map window to lower right corner */
294 SendMessage(mswin_hwnd_from_winid(WIN_MAP), WM_VSCROLL,
295 MAKEWPARAM(SB_THUMBTRACK, ROWNO), (LPARAM) NULL);
297 SendMessage(mswin_hwnd_from_winid(WIN_MAP), WM_HSCROLL,
298 MAKEWPARAM(SB_THUMBTRACK, COLNO), (LPARAM) NULL);
300 NHEVENT_KBD(KEYTABLE(KEY_SW));
305 if (STATEON(VK_CONTROL)) {
306 /* scroll map window one page up */
307 SendMessage(mswin_hwnd_from_winid(WIN_MAP), WM_VSCROLL,
308 MAKEWPARAM(SB_PAGEUP, 0), (LPARAM) NULL);
310 NHEVENT_KBD(KEYTABLE(KEY_NE));
315 if (STATEON(VK_CONTROL)) {
316 /* scroll map window one page down */
317 SendMessage(mswin_hwnd_from_winid(WIN_MAP), WM_VSCROLL,
318 MAKEWPARAM(SB_PAGEDOWN, 0), (LPARAM) NULL);
320 NHEVENT_KBD(KEYTABLE(KEY_SE));
326 NHEVENT_KBD(KEYTABLE(KEY_WAITLOOK));
330 NHEVENT_KBD(KEYTABLE(KEY_INV));
334 NHEVENT_KBD(KEYTABLE(KEY_MINUS));
338 NHEVENT_KBD(KEYTABLE(KEY_PLUS));
341 case VK_CLEAR: /* This is the '5' key */
342 NHEVENT_KBD(KEYTABLE(KEY_GOINTERESTING));
346 if (IS_MAP_FIT_TO_SCREEN(iflags.wc_map_mode)) {
347 mswin_select_map_mode(IS_MAP_ASCII(iflags.wc_map_mode)
348 ? data->mapAcsiiModeSave
351 mswin_select_map_mode(IS_MAP_ASCII(iflags.wc_map_mode)
352 ? MAP_MODE_ASCII_FIT_TO_SCREEN
353 : MAP_MODE_TILES_FIT_TO_SCREEN);
358 if (IS_MAP_ASCII(iflags.wc_map_mode)) {
359 if (IS_MAP_FIT_TO_SCREEN(iflags.wc_map_mode)) {
360 mswin_select_map_mode(MAP_MODE_TILES_FIT_TO_SCREEN);
362 mswin_select_map_mode(MAP_MODE_TILES);
365 if (IS_MAP_FIT_TO_SCREEN(iflags.wc_map_mode)) {
366 mswin_select_map_mode(MAP_MODE_ASCII_FIT_TO_SCREEN);
368 mswin_select_map_mode(data->mapAcsiiModeSave);
378 ZeroMemory(kbd_state, sizeof(kbd_state));
379 GetKeyboardState(kbd_state);
381 if (ToAscii((UINT) wParam, (lParam >> 16) & 0xFF, kbd_state, &c, 0)) {
382 NHEVENT_KBD(c & 0xFF);
392 #if 1 /*JP*//*
\91S
\8ap
\95¶
\8e\9a\91ÃŽ
\89\9e*/
395 if (doublebyte == 1) {
396 NHEVENT_KBD(wParam & 0xFF);
399 } else if (is_kanji(wParam)) {
400 NHEVENT_KBD(wParam & 0xFF);
407 case WM_SYSCHAR: /* Alt-char pressed */
410 If not nethackmode, don't handle Alt-keys here.
411 If no Alt-key pressed it can never be an extended command
413 if (GetNHApp()->regNetHackMode && ((lParam & 1 << 29) != 0)) {
414 unsigned char c = (unsigned char) (wParam & 0xFF);
415 unsigned char scancode = (lParam >> 16) & 0xFF;
416 if (index(extendedlist, tolower(c)) != 0) {
417 NHEVENT_KBD(M(tolower(c)));
418 } else if (scancode == (SCANLO + SIZE(scanmap)) - 1) {
423 return DefWindowProc(hWnd, message, wParam, lParam);
427 /* process commands - menu commands mostly */
428 if (onWMCommand(hWnd, wParam, lParam))
429 return DefWindowProc(hWnd, message, wParam, lParam);
437 mswin_layout_main_window(NULL);
439 wp.length = sizeof(wp);
440 if (GetWindowPlacement(hWnd, &wp)) {
441 GetNHApp()->regMainShowState =
442 (wp.showCmd == SW_SHOWMAXIMIZED ? SW_SHOWMAXIMIZED
445 GetNHApp()->regMainMinX = wp.ptMinPosition.x;
446 GetNHApp()->regMainMinY = wp.ptMinPosition.y;
448 GetNHApp()->regMainMaxX = wp.ptMaxPosition.x;
449 GetNHApp()->regMainMaxY = wp.ptMaxPosition.y;
451 GetNHApp()->regMainLeft = wp.rcNormalPosition.left;
452 GetNHApp()->regMainTop = wp.rcNormalPosition.top;
453 GetNHApp()->regMainRight = wp.rcNormalPosition.right;
454 GetNHApp()->regMainBottom = wp.rcNormalPosition.bottom;
459 /* if there is a menu window out there -
460 transfer input focus to it */
461 if (IsWindow(GetNHApp()->hPopupWnd)) {
462 SetFocus(GetNHApp()->hPopupWnd);
467 /* exit gracefully */
468 if (program_state.gameover) {
469 /* assume the user really meant this, as the game is already
471 /* to make sure we still save bones, just set stop printing flag
473 program_state.stopprint++;
475 '\033'); /* and send keyboard input as if user pressed ESC */
476 /* additional code for this is done in menu and rip windows */
477 } else if (!program_state.something_worth_saving) {
478 /* User exited before the game started, e.g. during splash display
483 /* prompt user for action */
485 switch (NHMessageBox(hWnd, TEXT("Save?"),
487 switch (NHMessageBox(hWnd, TEXT("
\95Û
\91¶
\82µ
\82Ä
\8fI
\97¹
\82µ
\82Ü
\82·
\82©
\81H"),
488 MB_YESNOCANCEL | MB_ICONQUESTION)) {
491 /* destroy popup window - it has its own loop and we need to
492 return control to NetHack core at this point */
493 if (IsWindow(GetNHApp()->hPopupWnd))
494 SendMessage(GetNHApp()->hPopupWnd, WM_COMMAND, IDCANCEL,
497 /* tell NetHack core that "hangup" is requested */
516 /* apparently we never get here
517 TODO: work on exit routines - need to send
521 free((PNHMainWindow) GetWindowLongPtr(hWnd, GWLP_USERDATA));
522 SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0);
524 // PostQuitMessage(0);
529 return DefWindowProc(hWnd, message, wParam, lParam);
535 onMSNHCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
537 UNREFERENCED_PARAMETER(hWnd);
538 UNREFERENCED_PARAMETER(wParam);
539 UNREFERENCED_PARAMETER(lParam);
542 /* new window was just added */
543 case MSNH_MSG_ADDWND: {
544 PMSNHMsgAddWnd msg_param = (PMSNHMsgAddWnd) lParam;
547 if (GetNHApp()->windowlist[msg_param->wid].type == NHW_MAP)
548 mswin_select_map_mode(iflags.wc_map_mode);
550 child = GetNHApp()->windowlist[msg_param->wid].win;
555 /* adjust windows to fit main window layout
556 ---------------------------
558 +-------------------------+
564 +-------------------------+
566 ---------------------------
569 mswin_layout_main_window(HWND changed_child)
572 RECT client_rt, wnd_rect;
580 HWND wnd_status, wnd_msg;
583 if (GetNHApp()->bAutoLayout) {
584 GetClientRect(GetNHApp()->hMainWnd, &client_rt);
585 data = (PNHMainWindow) GetWindowLongPtr(GetNHApp()->hMainWnd,
588 /* get sizes of child windows */
589 wnd_status = mswin_hwnd_from_winid(WIN_STATUS);
590 if (IsWindow(wnd_status)) {
591 mswin_status_window_size(wnd_status, &status_size);
593 status_size.cx = status_size.cy = 0;
596 wnd_msg = mswin_hwnd_from_winid(WIN_MESSAGE);
597 if (IsWindow(wnd_msg)) {
598 mswin_message_window_size(wnd_msg, &msg_size);
600 msg_size.cx = msg_size.cy = 0;
603 /* find all menu windows and calculate the size */
604 menu_size.cx = menu_size.cy = 0;
605 for (i = 0; i < MAXWINDOWS; i++) {
607 if (GetNHApp()->windowlist[i].win
608 && !GetNHApp()->windowlist[i].dead
609 && GetNHApp()->windowlist[i].type == NHW_MENU) {
610 mswin_menu_window_size(GetNHApp()->windowlist[i].win,
612 menu_size.cx = max(menu_size.cx, tmp_size.cx);
613 menu_size.cy = max(menu_size.cy, tmp_size.cy);
617 /* set window positions */
618 SetRect(&wnd_rect, client_rt.left, client_rt.top, client_rt.right,
620 switch (iflags.wc_align_status) {
622 status_size.cx = (wnd_rect.right - wnd_rect.left) / 4;
624 (wnd_rect.bottom - wnd_rect.top); // that won't look good
625 status_org.x = wnd_rect.left;
626 status_org.y = wnd_rect.top;
627 wnd_rect.left += status_size.cx;
631 status_size.cx = (wnd_rect.right - wnd_rect.left) / 4;
633 (wnd_rect.bottom - wnd_rect.top); // that won't look good
634 status_org.x = wnd_rect.right - status_size.cx;
635 status_org.y = wnd_rect.top;
636 wnd_rect.right -= status_size.cx;
640 status_size.cx = (wnd_rect.right - wnd_rect.left);
641 status_org.x = wnd_rect.left;
642 status_org.y = wnd_rect.top;
643 wnd_rect.top += status_size.cy;
648 status_size.cx = (wnd_rect.right - wnd_rect.left);
649 status_org.x = wnd_rect.left;
650 status_org.y = wnd_rect.bottom - status_size.cy;
651 wnd_rect.bottom -= status_size.cy;
655 switch (iflags.wc_align_message) {
657 msg_size.cx = (wnd_rect.right - wnd_rect.left) / 4;
658 msg_size.cy = (wnd_rect.bottom - wnd_rect.top);
659 msg_org.x = wnd_rect.left;
660 msg_org.y = wnd_rect.top;
661 wnd_rect.left += msg_size.cx;
665 msg_size.cx = (wnd_rect.right - wnd_rect.left) / 4;
666 msg_size.cy = (wnd_rect.bottom - wnd_rect.top);
667 msg_org.x = wnd_rect.right - msg_size.cx;
668 msg_org.y = wnd_rect.top;
669 wnd_rect.right -= msg_size.cx;
673 msg_size.cx = (wnd_rect.right - wnd_rect.left);
674 msg_org.x = wnd_rect.left;
675 msg_org.y = wnd_rect.top;
676 wnd_rect.top += msg_size.cy;
681 msg_size.cx = (wnd_rect.right - wnd_rect.left);
682 msg_org.x = wnd_rect.left;
683 msg_org.y = wnd_rect.bottom - msg_size.cy;
684 wnd_rect.bottom -= msg_size.cy;
689 map_org.x = wnd_rect.left;
690 map_org.y = wnd_rect.top;
691 map_size.cx = wnd_rect.right - wnd_rect.left;
692 map_size.cy = wnd_rect.bottom - wnd_rect.top;
694 GetNHApp()->rtStatusWindow.left = status_org.x;
695 GetNHApp()->rtStatusWindow.top = status_org.y;
696 GetNHApp()->rtStatusWindow.right = status_org.x + status_size.cx;
697 GetNHApp()->rtStatusWindow.bottom = status_org.y + status_size.cy;
699 GetNHApp()->rtTextWindow.left = map_org.x;
700 GetNHApp()->rtTextWindow.top = map_org.y;
701 GetNHApp()->rtTextWindow.right =
702 map_org.x + (wnd_rect.right - wnd_rect.left);
703 GetNHApp()->rtTextWindow.bottom = map_org.y + map_size.cy;
705 GetNHApp()->rtMapWindow.left = map_org.x;
706 GetNHApp()->rtMapWindow.top = map_org.y;
707 GetNHApp()->rtMapWindow.right = map_org.x + map_size.cx;
708 GetNHApp()->rtMapWindow.bottom = map_org.y + map_size.cy;
710 GetNHApp()->rtMsgWindow.left = msg_org.x;
711 GetNHApp()->rtMsgWindow.top = msg_org.y;
712 GetNHApp()->rtMsgWindow.right = msg_org.x + msg_size.cx;
713 GetNHApp()->rtMsgWindow.bottom = msg_org.y + msg_size.cy;
715 /* map_width/4 < menu_width < map_width*2/3 */
716 GetNHApp()->rtMenuWindow.left =
717 GetNHApp()->rtMapWindow.right
718 - min(map_size.cx * 2 / 3, max(map_size.cx / 4, menu_size.cx));
719 GetNHApp()->rtMenuWindow.top = GetNHApp()->rtMapWindow.top;
720 GetNHApp()->rtMenuWindow.right = GetNHApp()->rtMapWindow.right;
721 GetNHApp()->rtMenuWindow.bottom = GetNHApp()->rtMapWindow.bottom;
723 GetNHApp()->rtInvenWindow.left = GetNHApp()->rtMenuWindow.left;
724 GetNHApp()->rtInvenWindow.top = GetNHApp()->rtMenuWindow.top;
725 GetNHApp()->rtInvenWindow.right = GetNHApp()->rtMenuWindow.right;
726 GetNHApp()->rtInvenWindow.bottom = GetNHApp()->rtMenuWindow.bottom;
728 /* adjust map window size only if perm_invent is set */
729 if (flags.perm_invent)
730 GetNHApp()->rtMapWindow.right = GetNHApp()->rtMenuWindow.left;
733 /* go through the windows list and adjust sizes */
734 for (i = 0; i < MAXWINDOWS; i++) {
735 if (GetNHApp()->windowlist[i].win
736 && !GetNHApp()->windowlist[i].dead) {
738 /* kludge - inventory window should have its own type (same as
740 as a matter of fact) */
741 if (flags.perm_invent && i == WIN_INVEN) {
742 mswin_get_window_placement(NHW_INVEN, &rt);
744 mswin_get_window_placement(GetNHApp()->windowlist[i].type,
748 MoveWindow(GetNHApp()->windowlist[i].win, rt.left, rt.top,
749 rt.right - rt.left, rt.bottom - rt.top, TRUE);
752 if (IsWindow(changed_child))
753 SetForegroundWindow(changed_child);
757 onWMCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
762 UNREFERENCED_PARAMETER(lParam);
764 data = (PNHMainWindow) GetWindowLongPtr(hWnd, GWLP_USERDATA);
765 wmId = LOWORD(wParam);
766 wmEvent = HIWORD(wParam);
768 // Parse the menu selections:
771 mswin_display_splash_window(TRUE);
779 if (!program_state.gameover && !program_state.done_hup)
786 case IDM_MAP_ASCII4X6:
787 case IDM_MAP_ASCII6X8:
788 case IDM_MAP_ASCII8X8:
789 case IDM_MAP_ASCII16X8:
790 case IDM_MAP_ASCII7X12:
791 case IDM_MAP_ASCII8X12:
792 case IDM_MAP_ASCII12X16:
793 case IDM_MAP_ASCII16X12:
794 case IDM_MAP_ASCII10X18:
795 mswin_select_map_mode(menuid2mapmode(wmId));
798 case IDM_MAP_FIT_TO_SCREEN:
799 if (IS_MAP_FIT_TO_SCREEN(iflags.wc_map_mode)) {
800 mswin_select_map_mode(IS_MAP_ASCII(iflags.wc_map_mode)
801 ? data->mapAcsiiModeSave
804 mswin_select_map_mode(IS_MAP_ASCII(iflags.wc_map_mode)
805 ? MAP_MODE_ASCII_FIT_TO_SCREEN
806 : MAP_MODE_TILES_FIT_TO_SCREEN);
810 case IDM_SETTING_SCREEN_TO_CLIPBOARD: {
816 p = nh_compose_ascii_screenshot();
821 if (!OpenClipboard(hWnd)) {
822 NHMessageBox(hWnd, TEXT("Cannot open clipboard"),
823 MB_OK | MB_ICONERROR);
829 hglbCopy = GlobalAlloc(GMEM_MOVEABLE, (len + 1) * sizeof(char));
830 if (hglbCopy == NULL) {
835 p_copy = (char *) GlobalLock(hglbCopy);
836 strncpy(p_copy, p, len);
837 p_copy[len] = 0; // null character
838 GlobalUnlock(hglbCopy);
840 SetClipboardData(SYMHANDLING(H_IBM) ? CF_OEMTEXT : CF_TEXT, hglbCopy);
847 case IDM_SETTING_SCREEN_TO_FILE: {
849 TCHAR filename[1024];
850 TCHAR whackdir[MAX_PATH];
856 ZeroMemory(filename, sizeof(filename));
857 ZeroMemory(&ofn, sizeof(ofn));
858 ofn.lStructSize = sizeof(OPENFILENAME);
859 ofn.hwndOwner = hWnd;
860 ofn.hInstance = GetNHApp()->hApp;
861 ofn.lpstrFilter = TEXT("Text Files (*.txt)\x0*.txt\x0")
862 TEXT("All Files (*.*)\x0*.*\x0") TEXT("\x0\x0");
863 ofn.lpstrCustomFilter = NULL;
864 ofn.nMaxCustFilter = 0;
865 ofn.nFilterIndex = 1;
866 ofn.lpstrFile = filename;
867 ofn.nMaxFile = SIZE(filename);
868 ofn.lpstrFileTitle = NULL;
869 ofn.nMaxFileTitle = 0;
870 ofn.lpstrInitialDir = NH_A2W(hackdir, whackdir, MAX_PATH);
871 ofn.lpstrTitle = NULL;
872 ofn.Flags = OFN_LONGNAMES | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
874 ofn.nFileExtension = 0;
875 ofn.lpstrDefExt = TEXT("txt");
878 ofn.lpTemplateName = 0;
880 if (!GetSaveFileName(&ofn))
883 text = nh_compose_ascii_screenshot();
887 pFile = _tfopen(filename, TEXT("wt+,ccs=UTF-8"));
890 _stprintf(buf, TEXT("Cannot open %s for writing!"), filename);
891 NHMessageBox(hWnd, buf, MB_OK | MB_ICONERROR);
897 wtext = (wchar_t *) malloc(tlen * sizeof(wchar_t));
899 panic("out of memory");
900 MultiByteToWideChar(NH_CODEPAGE, 0, text, -1, wtext, tlen);
901 fwrite(wtext, tlen * sizeof(wchar_t), 1, pFile);
908 GetNHApp()->regNetHackMode = GetNHApp()->regNetHackMode ? 0 : 1;
909 mswin_menu_check_intf_mode();
910 mswin_apply_window_style_all();
913 case IDM_CLEARSETTINGS: {
915 /* Notify the user that windows settings will not be saved this time.
917 NHMessageBox(GetNHApp()->hMainWnd,
918 TEXT("Your Windows Settings will not be stored when you "
920 MB_OK | MB_ICONINFORMATION);
924 case IDM_SETTING_AUTOLAYOUT:
925 GetNHApp()->bAutoLayout = !GetNHApp()->bAutoLayout;
926 mswin_layout_main_window(NULL);
928 /* Update menu item check-mark */
929 CheckMenuItem(GetMenu(GetNHApp()->hMainWnd), IDM_SETTING_AUTOLAYOUT,
930 GetNHApp()->bAutoLayout ? MF_CHECKED : MF_UNCHECKED);
933 case IDM_SETTING_LOCKWINDOWS:
934 nhlock_windows(!GetNHApp()->bWindowsLocked);
938 display_file(HELP, TRUE);
941 case IDM_HELP_COMMANDS:
942 display_file(SHELP, TRUE);
945 case IDM_HELP_HISTORY:
949 case IDM_HELP_INFO_CHAR:
953 case IDM_HELP_INFO_KEY:
957 case IDM_HELP_OPTIONS:
961 case IDM_HELP_OPTIONS_LONG:
962 display_file(OPTIONFILE, TRUE);
965 case IDM_HELP_EXTCMD:
969 case IDM_HELP_LICENSE:
970 display_file(LICENSE, TRUE);
973 case IDM_HELP_PORTHELP:
974 display_file(PORT_HELP, TRUE);
983 // Mesage handler for about box.
985 About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
989 RECT main_rt, dlg_rt;
992 UNREFERENCED_PARAMETER(lParam);
996 getversionstring(buf);
997 SetDlgItemText(hDlg, IDC_ABOUT_VERSION,
998 NH_A2W(buf, wbuf, sizeof(wbuf)));
1000 SetDlgItemText(hDlg, IDC_ABOUT_COPYRIGHT,
1001 NH_A2W(COPYRIGHT_BANNER_A "\n" COPYRIGHT_BANNER_B
1002 "\n" COPYRIGHT_BANNER_C
1003 "\n" COPYRIGHT_BANNER_D,
1006 /* center dialog in the main window */
1007 GetWindowRect(GetNHApp()->hMainWnd, &main_rt);
1008 GetWindowRect(hDlg, &dlg_rt);
1009 dlg_sz.cx = dlg_rt.right - dlg_rt.left;
1010 dlg_sz.cy = dlg_rt.bottom - dlg_rt.top;
1012 dlg_rt.left = (main_rt.left + main_rt.right - dlg_sz.cx) / 2;
1013 dlg_rt.right = dlg_rt.left + dlg_sz.cx;
1014 dlg_rt.top = (main_rt.top + main_rt.bottom - dlg_sz.cy) / 2;
1015 dlg_rt.bottom = dlg_rt.top + dlg_sz.cy;
1016 MoveWindow(hDlg, (main_rt.left + main_rt.right - dlg_sz.cx) / 2,
1017 (main_rt.top + main_rt.bottom - dlg_sz.cy) / 2, dlg_sz.cx,
1023 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
1024 EndDialog(hDlg, LOWORD(wParam));
1033 mswin_menu_check_intf_mode()
1035 HMENU hMenu = GetMenu(GetNHApp()->hMainWnd);
1037 if (GetNHApp()->regNetHackMode)
1038 CheckMenuItem(hMenu, IDM_NHMODE, MF_CHECKED);
1040 CheckMenuItem(hMenu, IDM_NHMODE, MF_UNCHECKED);
1044 mswin_select_map_mode(int mode)
1051 (PNHMainWindow) GetWindowLongPtr(GetNHApp()->hMainWnd, GWLP_USERDATA);
1053 /* override for Rogue level */
1054 if (Is_rogue_level(&u.uz) && !IS_MAP_ASCII(mode))
1057 /* set map mode menu mark */
1058 if (IS_MAP_ASCII(mode)) {
1060 GetMenu(GetNHApp()->hMainWnd), IDM_MAP_TILES, IDM_MAP_ASCII10X18,
1061 mapmode2menuid(IS_MAP_FIT_TO_SCREEN(mode) ? data->mapAcsiiModeSave
1065 CheckMenuRadioItem(GetMenu(GetNHApp()->hMainWnd), IDM_MAP_TILES,
1066 IDM_MAP_ASCII10X18, mapmode2menuid(MAP_MODE_TILES),
1070 /* set fit-to-screen mode mark */
1071 CheckMenuItem(GetMenu(GetNHApp()->hMainWnd), IDM_MAP_FIT_TO_SCREEN,
1072 MF_BYCOMMAND | (IS_MAP_FIT_TO_SCREEN(mode) ? MF_CHECKED
1075 if (IS_MAP_ASCII(iflags.wc_map_mode)
1076 && !IS_MAP_FIT_TO_SCREEN(iflags.wc_map_mode)) {
1077 data->mapAcsiiModeSave = iflags.wc_map_mode;
1080 iflags.wc_map_mode = mode;
1083 ** first, check if WIN_MAP has been inialized.
1084 ** If not - attempt to retrieve it by type, then check it again
1086 if (map_id == WIN_ERR)
1087 map_id = mswin_winid_from_type(NHW_MAP);
1088 if (map_id != WIN_ERR)
1089 mswin_map_mode(mswin_hwnd_from_winid(map_id), mode);
1092 static struct t_menu2mapmode {
1095 } _menu2mapmode[] = { { IDM_MAP_TILES, MAP_MODE_TILES },
1096 { IDM_MAP_ASCII4X6, MAP_MODE_ASCII4x6 },
1097 { IDM_MAP_ASCII6X8, MAP_MODE_ASCII6x8 },
1098 { IDM_MAP_ASCII8X8, MAP_MODE_ASCII8x8 },
1099 { IDM_MAP_ASCII16X8, MAP_MODE_ASCII16x8 },
1100 { IDM_MAP_ASCII7X12, MAP_MODE_ASCII7x12 },
1101 { IDM_MAP_ASCII8X12, MAP_MODE_ASCII8x12 },
1102 { IDM_MAP_ASCII12X16, MAP_MODE_ASCII12x16 },
1103 { IDM_MAP_ASCII16X12, MAP_MODE_ASCII16x12 },
1104 { IDM_MAP_ASCII10X18, MAP_MODE_ASCII10x18 },
1105 { IDM_MAP_FIT_TO_SCREEN, MAP_MODE_ASCII_FIT_TO_SCREEN },
1109 menuid2mapmode(int menuid)
1111 struct t_menu2mapmode *p;
1112 for (p = _menu2mapmode; p->mapMode != -1; p++)
1113 if (p->menuID == menuid)
1119 mapmode2menuid(int map_mode)
1121 struct t_menu2mapmode *p;
1122 for (p = _menu2mapmode; p->mapMode != -1; p++)
1123 if (p->mapMode == map_mode)
1129 nhlock_windows(BOOL lock)
1132 GetNHApp()->bWindowsLocked = lock;
1133 CheckMenuItem(GetMenu(GetNHApp()->hMainWnd), IDM_SETTING_LOCKWINDOWS,
1134 MF_BYCOMMAND | (lock ? MF_CHECKED : MF_UNCHECKED));
1136 /* restyle windows */
1137 mswin_apply_window_style_all();
1141 mswin_apply_window_style(HWND hwnd) {
1142 DWORD style = 0, exstyle = 0;
1144 style = GetWindowLong(hwnd, GWL_STYLE);
1145 exstyle = GetWindowLong(hwnd, GWL_EXSTYLE);
1147 if( !GetNHApp()->bWindowsLocked ) {
1148 style = WS_CHILD|WS_CLIPSIBLINGS|WS_CAPTION|WS_SIZEBOX|(style & (WS_VISIBLE|WS_VSCROLL|WS_HSCROLL));
1149 exstyle = WS_EX_WINDOWEDGE;
1150 } else if (GetNHApp()->regNetHackMode) {
1151 /* do away borders */
1152 style = WS_CHILD|WS_CLIPSIBLINGS|(style & (WS_VISIBLE|WS_VSCROLL|WS_HSCROLL));
1155 style = WS_CHILD|WS_CLIPSIBLINGS|WS_THICKFRAME|(style & (WS_VISIBLE|WS_VSCROLL|WS_HSCROLL));
1156 exstyle = WS_EX_WINDOWEDGE;
1159 SetWindowLong(hwnd, GWL_STYLE, style);
1160 SetWindowLong(hwnd, GWL_EXSTYLE, exstyle);
1161 SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
1162 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED | SWP_NOCOPYBITS);
1166 mswin_apply_window_style_all() {
1168 for (i = 0; i < MAXWINDOWS; i++) {
1169 if (IsWindow(GetNHApp()->windowlist[i].win)
1170 && !GetNHApp()->windowlist[i].dead) {
1171 mswin_apply_window_style(GetNHApp()->windowlist[i].win);
1174 mswin_layout_main_window(NULL);
1177 // returns strdup() created pointer - callee assumes the ownership
1178 #define TEXT_BUFFER_SIZE 4096
1180 nh_compose_ascii_screenshot()
1183 PMSNHMsgGetText text;
1185 retval = (char *) malloc(3 * TEXT_BUFFER_SIZE);
1188 (PMSNHMsgGetText) malloc(sizeof(MSNHMsgGetText) + TEXT_BUFFER_SIZE);
1191 - 1; /* make sure we always have 0 at the end of the buffer */
1193 ZeroMemory(text->buffer, TEXT_BUFFER_SIZE);
1194 SendMessage(mswin_hwnd_from_winid(WIN_MESSAGE), WM_MSNH_COMMAND,
1195 (WPARAM) MSNH_MSG_GETTEXT, (LPARAM) text);
1196 strcpy(retval, text->buffer);
1198 ZeroMemory(text->buffer, TEXT_BUFFER_SIZE);
1199 SendMessage(mswin_hwnd_from_winid(WIN_MAP), WM_MSNH_COMMAND,
1200 (WPARAM) MSNH_MSG_GETTEXT, (LPARAM) text);
1201 strcat(retval, text->buffer);
1203 ZeroMemory(text->buffer, TEXT_BUFFER_SIZE);
1204 SendMessage(mswin_hwnd_from_winid(WIN_STATUS), WM_MSNH_COMMAND,
1205 (WPARAM) MSNH_MSG_GETTEXT, (LPARAM) text);
1206 strcat(retval, text->buffer);