X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/9709875736518ec7ae58ea474ac5130ae320158a..3fee7a57faa025c2785c79625075761e91e98dba:/windows.c diff --git a/windows.c b/windows.c index feee3cf..b93847d 100644 --- a/windows.c +++ b/windows.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include "puzzles.h" @@ -31,11 +32,12 @@ #define IDM_SOLVE 0x0060 #define IDM_QUIT 0x0070 #define IDM_CONFIG 0x0080 -#define IDM_SEED 0x0090 -#define IDM_HELPC 0x00A0 -#define IDM_GAMEHELP 0x00B0 +#define IDM_DESC 0x0090 +#define IDM_SEED 0x00A0 +#define IDM_HELPC 0x00B0 +#define IDM_GAMEHELP 0x00C0 +#define IDM_ABOUT 0x00D0 #define IDM_PRESETS 0x0100 -#define IDM_ABOUT 0x0110 #define HELP_FILE_NAME "puzzles.hlp" #define HELP_CNT_NAME "puzzles.cnt" @@ -76,13 +78,6 @@ void debug_printf(char *fmt, ...) dputs(buf); va_end(ap); } - -#define debug(x) (debug_printf x) - -#else - -#define debug(x) - #endif struct font { @@ -117,6 +112,7 @@ struct frontend { HFONT cfgfont; char *help_path; int help_has_contents; + char *laststatus; }; void fatal(char *fmt, ...) @@ -143,7 +139,14 @@ void get_random_seed(void **randseed, int *randseedsize) void status_bar(frontend *fe, char *text) { - SetWindowText(fe->statusbar, text); + char *rewritten = midend_rewrite_statusbar(fe->me, text); + if (!fe->laststatus || strcmp(rewritten, fe->laststatus)) { + SetWindowText(fe->statusbar, rewritten); + sfree(fe->laststatus); + fe->laststatus = rewritten; + } else { + sfree(rewritten); + } } void frontend_default_colour(frontend *fe, float *output) @@ -404,11 +407,57 @@ static void find_help_file(frontend *fe) } } +static void check_window_size(frontend *fe, int *px, int *py) +{ + RECT r; + int x, y, sy; + + if (fe->statusbar) { + RECT sr; + GetWindowRect(fe->statusbar, &sr); + sy = sr.bottom - sr.top; + } else { + sy = 0; + } + + /* + * See if we actually got the window size we wanted, and adjust + * the puzzle size if not. + */ + GetClientRect(fe->hwnd, &r); + x = r.right - r.left; + y = r.bottom - r.top - sy; + midend_size(fe->me, &x, &y, FALSE); + if (x != r.right - r.left || y != r.bottom - r.top) { + /* + * Resize the window, now we know what size we _really_ + * want it to be. + */ + r.left = r.top = 0; + r.right = x; + r.bottom = y + sy; + AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~ + (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED), + TRUE, 0); + SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left, r.bottom - r.top, + SWP_NOMOVE | SWP_NOZORDER); + } + + if (fe->statusbar) { + GetClientRect(fe->hwnd, &r); + SetWindowPos(fe->statusbar, NULL, 0, r.bottom-r.top-sy, r.right-r.left, + sy, SWP_NOZORDER); + } + + *px = x; + *py = y; +} + static frontend *new_window(HINSTANCE inst, char *game_id, char **error) { frontend *fe; int x, y; - RECT r, sr; + RECT r; HDC hdc; fe = snew(frontend); @@ -416,7 +465,7 @@ static frontend *new_window(HINSTANCE inst, char *game_id, char **error) fe->me = midend_new(fe, &thegame); if (game_id) { - *error = midend_game_id(fe->me, game_id, FALSE); + *error = midend_game_id(fe->me, game_id); if (*error) { midend_free(fe->me); sfree(fe); @@ -429,13 +478,14 @@ static frontend *new_window(HINSTANCE inst, char *game_id, char **error) fe->inst = inst; midend_new_game(fe->me); - midend_size(fe->me, &x, &y); fe->timer = 0; fe->fonts = NULL; fe->nfonts = fe->fontsize = 0; + fe->laststatus = NULL; + { int i, ncolours; float *colours; @@ -455,6 +505,9 @@ static frontend *new_window(HINSTANCE inst, char *game_id, char **error) } } + x = y = INT_MAX; /* find puzzle's preferred size */ + midend_size(fe->me, &x, &y, FALSE); + r.left = r.top = 0; r.right = x; r.bottom = y; @@ -469,6 +522,24 @@ static frontend *new_window(HINSTANCE inst, char *game_id, char **error) r.right - r.left, r.bottom - r.top, NULL, NULL, inst, NULL); + if (midend_wants_statusbar(fe->me)) { + RECT sr; + fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh", + WS_CHILD | WS_VISIBLE, + 0, 0, 0, 0, /* status bar does these */ + fe->hwnd, NULL, inst, NULL); + /* + * Now resize the window to take account of the status bar. + */ + GetWindowRect(fe->statusbar, &sr); + GetWindowRect(fe->hwnd, &r); + SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left, + r.bottom - r.top + sr.bottom - sr.top, + SWP_NOMOVE | SWP_NOZORDER); + } else { + fe->statusbar = NULL; + } + { HMENU bar = CreateMenu(); HMENU menu = CreateMenu(); @@ -476,7 +547,8 @@ static frontend *new_window(HINSTANCE inst, char *game_id, char **error) AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Game"); AppendMenu(menu, MF_ENABLED, IDM_NEW, "New"); AppendMenu(menu, MF_ENABLED, IDM_RESTART, "Restart"); - AppendMenu(menu, MF_ENABLED, IDM_SEED, "Specific..."); + AppendMenu(menu, MF_ENABLED, IDM_DESC, "Specific..."); + AppendMenu(menu, MF_ENABLED, IDM_SEED, "Random Seed..."); if ((fe->npresets = midend_num_presets(fe->me)) > 0 || thegame.can_configure) { @@ -536,20 +608,7 @@ static frontend *new_window(HINSTANCE inst, char *game_id, char **error) SetMenu(fe->hwnd, bar); } - if (midend_wants_statusbar(fe->me)) { - fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh", - WS_CHILD | WS_VISIBLE, - 0, 0, 0, 0, /* status bar does these */ - fe->hwnd, NULL, inst, NULL); - GetWindowRect(fe->statusbar, &sr); - SetWindowPos(fe->hwnd, NULL, 0, 0, - r.right - r.left, r.bottom - r.top + sr.bottom - sr.top, - SWP_NOMOVE | SWP_NOZORDER); - SetWindowPos(fe->statusbar, NULL, 0, y, x, sr.bottom - sr.top, - SWP_NOZORDER); - } else { - fe->statusbar = NULL; - } + check_window_size(fe, &x, &y); hdc = GetDC(fe->hwnd); fe->bitmap = CreateCompatibleBitmap(hdc, x, y); @@ -1070,7 +1129,8 @@ static void new_game_type(frontend *fe) int x, y; midend_new_game(fe->me); - midend_size(fe->me, &x, &y); + x = y = INT_MAX; + midend_size(fe->me, &x, &y, FALSE); r.left = r.top = 0; r.right = x; @@ -1089,6 +1149,9 @@ static void new_game_type(frontend *fe) r.right - r.left, r.bottom - r.top + sr.bottom - sr.top, SWP_NOMOVE | SWP_NOZORDER); + + check_window_size(fe, &x, &y); + if (fe->statusbar != NULL) SetWindowPos(fe->statusbar, NULL, 0, y, x, sr.bottom - sr.top, SWP_NOZORDER); @@ -1102,6 +1165,19 @@ static void new_game_type(frontend *fe) midend_redraw(fe->me); } +static int is_alt_pressed(void) +{ + BYTE keystate[256]; + int r = GetKeyboardState(keystate); + if (!r) + return FALSE; + if (keystate[VK_MENU] & 0x80) + return TRUE; + if (keystate[VK_RMENU] & 0x80) + return TRUE; + return FALSE; +} + static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { @@ -1118,8 +1194,7 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, PostQuitMessage(0); break; case IDM_RESTART: - if (!midend_process_key(fe->me, 0, 0, 'r')) - PostQuitMessage(0); + midend_restart_game(fe->me); break; case IDM_UNDO: if (!midend_process_key(fe->me, 0, 0, 'u')) @@ -1159,6 +1234,10 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, if (get_config(fe, CFG_SEED)) new_game_type(fe); break; + case IDM_DESC: + if (get_config(fe, CFG_DESC)) + new_game_type(fe); + break; case IDM_ABOUT: about(fe); break; @@ -1216,31 +1295,35 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, case WM_KEYDOWN: { int key = -1; + BYTE keystate[256]; + int r = GetKeyboardState(keystate); + int shift = (r && (keystate[VK_SHIFT] & 0x80)) ? MOD_SHFT : 0; + int ctrl = (r && (keystate[VK_CONTROL] & 0x80)) ? MOD_CTRL : 0; switch (wParam) { case VK_LEFT: if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '4'; - else - key = CURSOR_LEFT; + else + key = shift | ctrl | CURSOR_LEFT; break; case VK_RIGHT: if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '6'; - else - key = CURSOR_RIGHT; + else + key = shift | ctrl | CURSOR_RIGHT; break; case VK_UP: if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '8'; - else - key = CURSOR_UP; + else + key = shift | ctrl | CURSOR_UP; break; case VK_DOWN: if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '2'; - else - key = CURSOR_DOWN; + else + key = shift | ctrl | CURSOR_DOWN; break; /* * Diagonal keys on the numeric keypad. @@ -1304,10 +1387,10 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, */ if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT)) button = MIDDLE_BUTTON; - else if (message == WM_LBUTTONDOWN) - button = LEFT_BUTTON; - else + else if (message == WM_RBUTTONDOWN || is_alt_pressed()) button = RIGHT_BUTTON; + else + button = LEFT_BUTTON; if (!midend_process_key(fe->me, (signed short)LOWORD(lParam), (signed short)HIWORD(lParam), button)) @@ -1329,10 +1412,10 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, */ if (message == WM_MBUTTONUP || (wParam & MK_SHIFT)) button = MIDDLE_RELEASE; - else if (message == WM_LBUTTONUP) - button = LEFT_RELEASE; - else + else if (message == WM_RBUTTONUP || is_alt_pressed()) button = RIGHT_RELEASE; + else + button = LEFT_RELEASE; if (!midend_process_key(fe->me, (signed short)LOWORD(lParam), (signed short)HIWORD(lParam), button)) @@ -1347,10 +1430,10 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, if (wParam & (MK_MBUTTON | MK_SHIFT)) button = MIDDLE_DRAG; - else if (wParam & MK_LBUTTON) - button = LEFT_DRAG; - else + else if (wParam & MK_RBUTTON || is_alt_pressed()) button = RIGHT_DRAG; + else + button = LEFT_DRAG; if (!midend_process_key(fe->me, (signed short)LOWORD(lParam), (signed short)HIWORD(lParam), button))