X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/fa2cb13d4940a616f7d35151d0c9c341a52e5e98..HEAD:/windows.c diff --git a/windows.c b/windows.c index d282d72..fde1e6b 100644 --- a/windows.c +++ b/windows.c @@ -4,7 +4,14 @@ #include #include +#ifndef NO_HTMLHELP #include +#endif /* NO_HTMLHELP */ + +#ifdef _WIN32_WCE +#include +#include +#endif #include #include @@ -16,6 +23,10 @@ #include "puzzles.h" +#ifdef _WIN32_WCE +#include "resource.h" +#endif + #define IDM_NEW 0x0010 #define IDM_RESTART 0x0020 #define IDM_UNDO 0x0030 @@ -33,20 +44,83 @@ #define IDM_LOAD 0x00F0 #define IDM_PRINT 0x0100 #define IDM_PRESETS 0x0110 +#define IDM_GAMES 0x0300 + +#define IDM_KEYEMUL 0x0400 #define HELP_FILE_NAME "puzzles.hlp" #define HELP_CNT_NAME "puzzles.cnt" +#ifndef NO_HTMLHELP #define CHM_FILE_NAME "puzzles.chm" +#endif /* NO_HTMLHELP */ +#ifndef NO_HTMLHELP typedef HWND (CALLBACK *htmlhelp_t)(HWND, LPCSTR, UINT, DWORD); -static DWORD html_help_cookie; static htmlhelp_t htmlhelp; static HINSTANCE hh_dll; +#endif /* NO_HTMLHELP */ enum { NONE, HLP, CHM } help_type; char *help_path; -const char *help_topic; int help_has_contents; +#ifndef FILENAME_MAX +#define FILENAME_MAX (260) +#endif + +#ifndef HGDI_ERROR +#define HGDI_ERROR ((HANDLE)GDI_ERROR) +#endif + +#ifdef COMBINED +#define CLASSNAME "Puzzles" +#else +#define CLASSNAME thegame.name +#endif + +#ifdef _WIN32_WCE + +/* + * Wrapper implementations of functions not supplied by the + * PocketPC API. + */ + +#define SHGetSubMenu(hWndMB,ID_MENU) (HMENU)SendMessage((hWndMB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)ID_MENU) + +#undef MessageBox + +int MessageBox(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType) +{ + TCHAR wText[2048]; + TCHAR wCaption[2048]; + + MultiByteToWideChar (CP_ACP, 0, lpText, -1, wText, 2048); + MultiByteToWideChar (CP_ACP, 0, lpCaption, -1, wCaption, 2048); + + return MessageBoxW (hWnd, wText, wCaption, uType); +} + +BOOL SetDlgItemTextA(HWND hDlg, int nIDDlgItem, LPCSTR lpString) +{ + TCHAR wText[256]; + + MultiByteToWideChar (CP_ACP, 0, lpString, -1, wText, 256); + return SetDlgItemTextW(hDlg, nIDDlgItem, wText); +} + +LPCSTR getenv(LPCSTR buf) +{ + return NULL; +} + +BOOL GetKeyboardState(PBYTE pb) +{ + return FALSE; +} + +static TCHAR wClassName[256], wGameName[256]; + +#endif + #ifdef DEBUGGING static FILE *debug_fp = NULL; static HANDLE debug_hdl = INVALID_HANDLE_VALUE; @@ -54,7 +128,7 @@ static int debug_got_console = 0; void dputs(char *buf) { - DWORD dw; + /*DWORD dw; if (!debug_got_console) { if (AllocConsole()) { @@ -69,26 +143,39 @@ void dputs(char *buf) if (debug_hdl != INVALID_HANDLE_VALUE) { WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL); } - fputs(buf, debug_fp); - fflush(debug_fp); + if (debug_fp) { + fputs(buf, debug_fp); + fflush(debug_fp); + }*/ + OutputDebugString(buf); } void debug_printf(char *fmt, ...) { char buf[4096]; va_list ap; + static int debugging = -1; - va_start(ap, fmt); - vsprintf(buf, fmt, ap); - dputs(buf); - va_end(ap); + if (debugging == -1) + debugging = getenv("DEBUG_PUZZLES") ? 1 : 0; + + if (debugging) { + va_start(ap, fmt); + _vsnprintf(buf, 4095, fmt, ap); + dputs(buf); + va_end(ap); + } } #endif +#ifndef _WIN32_WCE #define WINFLAGS (WS_OVERLAPPEDWINDOW &~ \ - (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED)) + (WS_MAXIMIZEBOX | WS_OVERLAPPED)) +#else +#define WINFLAGS (WS_CAPTION | WS_SYSMENU) +#endif -static void new_game_size(frontend *fe); +static void new_game_size(frontend *fe, float scale); struct font { HFONT font; @@ -109,15 +196,21 @@ struct blitter { enum { CFG_PRINT = CFG_FRONTEND_SPECIFIC }; struct frontend { + const game *game; midend *me; HWND hwnd, statusbar, cfgbox; +#ifdef _WIN32_WCE + HWND numpad; /* window handle for the numeric pad */ +#endif HINSTANCE inst; HBITMAP bitmap, prevbm; + RECT bitmapPosition; /* game bitmap position within game window */ HDC hdc; COLORREF *colours; HBRUSH *brushes; HPEN *pens; HRGN clip; + HMENU gamemenu, typemenu; UINT timer; DWORD timer_last_tickcount; int npresets; @@ -138,10 +231,28 @@ struct frontend { int printoffsetx, printoffsety; float printpixelscale; int fontstart; - int linewidth; + int linewidth, linedotted; drawing *dr; + int xmin, ymin; + float puzz_scale; }; +void frontend_free(frontend *fe) +{ + midend_free(fe->me); + + sfree(fe->colours); + sfree(fe->brushes); + sfree(fe->pens); + sfree(fe->presets); + sfree(fe->fonts); + + sfree(fe); +} + +static void update_type_menu_tick(frontend *fe); +static void update_copy_menu_greying(frontend *fe); + void fatal(char *fmt, ...) { char buf[2048]; @@ -180,17 +291,29 @@ char *geterrstr(void) void get_random_seed(void **randseed, int *randseedsize) { - time_t *tp = snew(time_t); - time(tp); - *randseed = (void *)tp; - *randseedsize = sizeof(time_t); + SYSTEMTIME *st = snew(SYSTEMTIME); + + GetLocalTime(st); + + *randseed = (void *)st; + *randseedsize = sizeof(SYSTEMTIME); } static void win_status_bar(void *handle, char *text) { +#ifdef _WIN32_WCE + TCHAR wText[255]; +#endif frontend *fe = (frontend *)handle; +#ifdef _WIN32_WCE + MultiByteToWideChar (CP_ACP, 0, text, -1, wText, 255); + SendMessage(fe->statusbar, SB_SETTEXT, + (WPARAM) 255 | SBT_NOBORDERS, + (LPARAM) wText); +#else SetWindowText(fe->statusbar, text); +#endif } static blitter *win_blitter_new(void *handle, int w, int h) @@ -308,12 +431,14 @@ static void win_text_colour(frontend *fe, int colour) if (fe->drawstatus == PRINTING) { int hatch; float r, g, b; - print_get_colour(fe->dr, colour, &hatch, &r, &g, &b); - if (fe->printcolour) - SetTextColor(fe->hdc, RGB(r * 255, g * 255, b * 255)); - else - SetTextColor(fe->hdc, - hatch == HATCH_CLEAR ? RGB(255,255,255) : RGB(0,0,0)); + print_get_colour(fe->dr, colour, fe->printcolour, &hatch, &r, &g, &b); + + /* + * Displaying text in hatched colours is not permitted. + */ + assert(hatch < 0); + + SetTextColor(fe->hdc, RGB(r * 255, g * 255, b * 255)); } else { SetTextColor(fe->hdc, fe->colours[colour]); } @@ -327,15 +452,18 @@ static void win_set_brush(frontend *fe, int colour) if (fe->drawstatus == PRINTING) { int hatch; float r, g, b; - print_get_colour(fe->dr, colour, &hatch, &r, &g, &b); + print_get_colour(fe->dr, colour, fe->printcolour, &hatch, &r, &g, &b); - if (fe->printcolour) + if (hatch < 0) { br = CreateSolidBrush(RGB(r * 255, g * 255, b * 255)); - else if (hatch == HATCH_SOLID) - br = CreateSolidBrush(RGB(0,0,0)); - else if (hatch == HATCH_CLEAR) - br = CreateSolidBrush(RGB(255,255,255)); - else + } else { +#ifdef _WIN32_WCE + /* + * This is only ever required during printing, and the + * PocketPC port doesn't support printing. + */ + fatal("CreateHatchBrush not supported"); +#else br = CreateHatchBrush(hatch == HATCH_BACKSLASH ? HS_FDIAGONAL : hatch == HATCH_SLASH ? HS_BDIAGONAL : hatch == HATCH_HORIZ ? HS_HORIZONTAL : @@ -343,6 +471,8 @@ static void win_set_brush(frontend *fe, int colour) hatch == HATCH_PLUS ? HS_CROSS : /* hatch == HATCH_X ? */ HS_DIAGCROSS, RGB(0,0,0)); +#endif + } } else { br = fe->brushes[colour]; } @@ -370,18 +500,16 @@ static void win_set_pen(frontend *fe, int colour, int thin) float r, g, b; int width = thin ? 0 : fe->linewidth; - print_get_colour(fe->dr, colour, &hatch, &r, &g, &b); - if (fe->printcolour) - pen = CreatePen(PS_SOLID, width, - RGB(r * 255, g * 255, b * 255)); - else if (hatch == HATCH_SOLID) - pen = CreatePen(PS_SOLID, width, RGB(0, 0, 0)); - else if (hatch == HATCH_CLEAR) - pen = CreatePen(PS_SOLID, width, RGB(255,255,255)); - else { - assert(!"This shouldn't happen"); - pen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0)); - } + if (fe->linedotted) + width = 0; + + print_get_colour(fe->dr, colour, fe->printcolour, &hatch, &r, &g, &b); + /* + * Stroking in hatched colours is not permitted. + */ + assert(hatch < 0); + pen = CreatePen(fe->linedotted ? PS_DOT : PS_SOLID, + width, RGB(r * 255, g * 255, b * 255)); } else { pen = fe->pens[colour]; } @@ -428,6 +556,7 @@ static void win_draw_text(void *handle, int x, int y, int fonttype, frontend *fe = (frontend *)handle; POINT xy; int i; + LOGFONT lf; if (fe->drawstatus == NOTHING) return; @@ -455,15 +584,21 @@ static void win_draw_text(void *handle, int x, int y, int fonttype, fe->fonts[i].type = fonttype; fe->fonts[i].size = fontsize; - fe->fonts[i].font = CreateFont(-fontsize, 0, 0, 0, - fe->drawstatus == PRINTING ? 0 : FW_BOLD, - FALSE, FALSE, FALSE, DEFAULT_CHARSET, - OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, - DEFAULT_QUALITY, - (fonttype == FONT_FIXED ? - FIXED_PITCH | FF_DONTCARE : - VARIABLE_PITCH | FF_SWISS), - NULL); + memset (&lf, 0, sizeof(LOGFONT)); + lf.lfHeight = -fontsize; + lf.lfWeight = (fe->drawstatus == PRINTING ? 0 : FW_BOLD); + lf.lfCharSet = DEFAULT_CHARSET; + lf.lfOutPrecision = OUT_DEFAULT_PRECIS; + lf.lfClipPrecision = CLIP_DEFAULT_PRECIS; + lf.lfQuality = DEFAULT_QUALITY; + lf.lfPitchAndFamily = (fonttype == FONT_FIXED ? + FIXED_PITCH | FF_DONTCARE : + VARIABLE_PITCH | FF_SWISS); +#ifdef _WIN32_WCE + wcscpy(lf.lfFaceName, TEXT("Tahoma")); +#endif + + fe->fonts[i].font = CreateFontIndirect(&lf); } /* @@ -473,6 +608,8 @@ static void win_draw_text(void *handle, int x, int y, int fonttype, HFONT oldfont; TEXTMETRIC tm; SIZE size; + TCHAR wText[256]; + MultiByteToWideChar (CP_UTF8, 0, text, -1, wText, 256); oldfont = SelectObject(fe->hdc, fe->fonts[i].font); if (GetTextMetrics(fe->hdc, &tm)) { @@ -481,7 +618,8 @@ static void win_draw_text(void *handle, int x, int y, int fonttype, else xy.y -= tm.tmAscent; } - if (GetTextExtentPoint32(fe->hdc, text, strlen(text), &size)) { + if (GetTextExtentPoint32W(fe->hdc, wText, wcslen(wText), &size)) + { if (align & ALIGN_HCENTRE) xy.x -= size.cx / 2; else if (align & ALIGN_HRIGHT) @@ -489,7 +627,7 @@ static void win_draw_text(void *handle, int x, int y, int fonttype, } SetBkMode(fe->hdc, TRANSPARENT); win_text_colour(fe, colour); - TextOut(fe->hdc, xy.x, xy.y, text, strlen(text)); + ExtTextOutW(fe->hdc, xy.x, xy.y, 0, NULL, wText, wcslen(wText), NULL); SelectObject(fe->hdc, oldfont); } } @@ -524,18 +662,17 @@ static void win_draw_rect(void *handle, int x, int y, int w, int h, int colour) static void win_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour) { frontend *fe = (frontend *)handle; - POINT p, q; + POINT pp[2]; if (fe->drawstatus == NOTHING) return; win_set_pen(fe, colour, FALSE); - p = win_transform_point(fe, x1, y1); - q = win_transform_point(fe, x2, y2); - MoveToEx(fe->hdc, p.x, p.y, NULL); - LineTo(fe->hdc, q.x, q.y); + pp[0] = win_transform_point(fe, x1, y1); + pp[1] = win_transform_point(fe, x2, y2); + Polyline(fe->hdc, pp, 2); if (fe->drawstatus == DRAWING) - SetPixel(fe->hdc, q.x, q.y, fe->colours[colour]); + SetPixel(fe->hdc, pp[1].x, pp[1].y, fe->colours[colour]); win_reset_pen(fe); } @@ -543,29 +680,24 @@ static void win_draw_circle(void *handle, int cx, int cy, int radius, int fillcolour, int outlinecolour) { frontend *fe = (frontend *)handle; - POINT p, q, r; + POINT p, q; assert(outlinecolour >= 0); if (fe->drawstatus == NOTHING) return; - if (fillcolour >= 0) { + if (fillcolour >= 0) win_set_brush(fe, fillcolour); - win_set_pen(fe, outlinecolour, FALSE); - p = win_transform_point(fe, cx - radius, cy - radius); - q = win_transform_point(fe, cx + radius, cy + radius); - Ellipse(fe->hdc, p.x, p.y, q.x+1, q.y+1); - win_reset_brush(fe); - win_reset_pen(fe); - } else { - win_set_pen(fe, outlinecolour, FALSE); - p = win_transform_point(fe, cx - radius, cy - radius); - q = win_transform_point(fe, cx + radius, cy + radius); - r = win_transform_point(fe, cx - radius, cy); - Arc(fe->hdc, p.x, p.y, q.x+1, q.y+1, r.x, r.y, r.x, r.y); - win_reset_pen(fe); - } + else + fe->oldbr = SelectObject(fe->hdc, GetStockObject(NULL_BRUSH)); + + win_set_pen(fe, outlinecolour, FALSE); + p = win_transform_point(fe, cx - radius, cy - radius); + q = win_transform_point(fe, cx + radius, cy + radius); + Ellipse(fe->hdc, p.x, p.y, q.x+1, q.y+1); + win_reset_brush(fe); + win_reset_pen(fe); } static void win_draw_polygon(void *handle, int *coords, int npoints, @@ -614,7 +746,9 @@ static void win_start_draw(void *handle) fe->prevbm = SelectObject(fe->hdc, fe->bitmap); ReleaseDC(fe->hwnd, hdc_win); fe->clip = NULL; +#ifndef _WIN32_WCE SetMapMode(fe->hdc, MM_TEXT); +#endif fe->drawstatus = DRAWING; } @@ -631,6 +765,7 @@ static void win_draw_update(void *handle, int x, int y, int w, int h) r.right = x + w; r.bottom = y + h; + OffsetRect(&r, fe->bitmapPosition.left, fe->bitmapPosition.top); InvalidateRect(fe->hwnd, &r, FALSE); } @@ -658,6 +793,17 @@ static void win_line_width(void *handle, float width) fe->linewidth = (int)(width * fe->printpixelscale); } +static void win_line_dotted(void *handle, int dotted) +{ + frontend *fe = (frontend *)handle; + + assert(fe->drawstatus != DRAWING); + if (fe->drawstatus == NOTHING) + return; + + fe->linedotted = dotted; +} + static void win_begin_doc(void *handle, int pages) { frontend *fe = (frontend *)handle; @@ -727,7 +873,7 @@ static void win_begin_puzzle(void *handle, float xm, float xc, * Work out what that comes to in pixels. */ pox = (int)(mmox * (float)ppw / mmpw); - poy = (int)(mmoy * (float)ppw / mmpw); + poy = (int)(mmoy * (float)pph / mmph); /* * And determine the scale. @@ -748,6 +894,7 @@ static void win_begin_puzzle(void *handle, float xm, float xc, fe->printpixelscale = scale; fe->linewidth = 1; + fe->linedotted = FALSE; } static void win_end_puzzle(void *handle) @@ -806,6 +953,15 @@ static void win_end_doc(void *handle) } } +char *win_text_fallback(void *handle, const char *const *strings, int nstrings) +{ + /* + * We assume Windows can cope with any UTF-8 likely to be + * emitted by a puzzle. + */ + return dupstr(strings[0]); +} + const struct drawing_api win_drawing = { win_draw_text, win_draw_rect, @@ -829,10 +985,13 @@ const struct drawing_api win_drawing = { win_end_page, win_end_doc, win_line_width, + win_line_dotted, + win_text_fallback, }; void print(frontend *fe) { +#ifndef _WIN32_WCE PRINTDLG pd; char doctitle[256]; document *doc; @@ -851,7 +1010,7 @@ void print(frontend *fe) if (!nme) { game_params *params; - nme = midend_new(NULL, &thegame, NULL, NULL); + nme = midend_new(NULL, fe->game, NULL, NULL); /* * Set the non-interactive mid-end to have the same @@ -859,7 +1018,7 @@ void print(frontend *fe) */ params = midend_get_params(fe->me); midend_set_params(nme, params); - thegame.free_params(params); + fe->game->free_params(params); } midend_new_game(nme); @@ -905,7 +1064,7 @@ void print(frontend *fe) memset(&fe->di, 0, sizeof(fe->di)); fe->di.cbSize = sizeof(fe->di); sprintf(doctitle, "Printed puzzles from %s (from Simon Tatham's" - " Portable Puzzle Collection)", thegame.name); + " Portable Puzzle Collection)", fe->game->name); fe->di.lpszDocName = doctitle; fe->di.lpszOutput = NULL; fe->di.lpszDatatype = NULL; @@ -923,6 +1082,7 @@ void print(frontend *fe) DeleteDC(pd.hDC); document_free(doc); +#endif } void deactivate_timer(frontend *fe) @@ -938,7 +1098,7 @@ void activate_timer(frontend *fe) if (!fe) return; /* for non-interactive midend */ if (!fe->timer) { - fe->timer = SetTimer(fe->hwnd, fe->timer, 20, NULL); + fe->timer = SetTimer(fe->hwnd, 1, 20, NULL); fe->timer_last_tickcount = GetTickCount(); } } @@ -971,11 +1131,16 @@ void write_clip(HWND hwnd, char *data) data2[j] = '\0'; clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1); - if (!clipdata) + if (!clipdata) { + sfree(data2); return; + } lock = GlobalLock(clipdata); - if (!lock) + if (!lock) { + GlobalFree(clipdata); + sfree(data2); return; + } memcpy(lock, data2, len); ((unsigned char *) lock)[len] = 0; GlobalUnlock(clipdata); @@ -995,6 +1160,7 @@ void write_clip(HWND hwnd, char *data) */ static void init_help(void) { +#ifndef _WIN32_WCE char b[2048], *p, *q, *r; FILE *fp; @@ -1009,6 +1175,7 @@ static void init_help(void) q = strrchr(b, ':'); if (q && q >= r) r = q+1; +#ifndef NO_HTMLHELP /* * Try HTML Help first. */ @@ -1026,13 +1193,12 @@ static void init_help(void) FreeLibrary(hh_dll); } if (htmlhelp) { - htmlhelp(NULL, NULL, HH_INITIALIZE, (DWORD)&html_help_cookie); help_path = dupstr(b); help_type = CHM; - help_topic = thegame.htmlhelp_topic; return; } } +#endif /* NO_HTMLHELP */ /* * Now try old-style .HLP. @@ -1044,8 +1210,6 @@ static void init_help(void) help_path = dupstr(b); help_type = HLP; - help_topic = thegame.winhelp_topic; - /* * See if there's a .CNT file alongside it. */ @@ -1060,8 +1224,11 @@ static void init_help(void) } help_type = NONE; /* didn't find any */ +#endif } +#ifndef _WIN32_WCE + /* * Start Help. */ @@ -1086,6 +1253,7 @@ static void start_help(frontend *fe, const char *topic) fe->help_running = TRUE; break; case CHM: +#ifndef NO_HTMLHELP assert(help_path); assert(htmlhelp); if (topic) { @@ -1097,6 +1265,7 @@ static void start_help(frontend *fe, const char *topic) htmlhelp(fe->hwnd, str, HH_DISPLAY_TOPIC, 0); fe->help_running = TRUE; break; +#endif /* NO_HTMLHELP */ case NONE: assert(!"This shouldn't happen"); break; @@ -1116,9 +1285,11 @@ static void stop_help(frontend *fe) WinHelp(fe->hwnd, help_path, HELP_QUIT, 0); break; case CHM: +#ifndef NO_HTMLHELP assert(htmlhelp); htmlhelp(NULL, NULL, HH_CLOSE_ALL, 0); break; +#endif /* NO_HTMLHELP */ case NONE: assert(!"This shouldn't happen"); break; @@ -1127,22 +1298,21 @@ static void stop_help(frontend *fe) } } +#endif + /* * Terminate Help on process exit. */ static void cleanup_help(void) { - if (help_type == CHM) { - assert(htmlhelp); - htmlhelp(NULL, NULL, HH_UNINITIALIZE, html_help_cookie); - } + /* Nothing to do currently. + * (If we were running HTML Help single-threaded, this is where we'd + * call HH_UNINITIALIZE.) */ } -static void check_window_size(frontend *fe, int *px, int *py) +static int get_statusbar_height(frontend *fe) { - RECT r; - int x, y, sy; - + int sy; if (fe->statusbar) { RECT sr; GetWindowRect(fe->statusbar, &sr); @@ -1150,36 +1320,104 @@ static void check_window_size(frontend *fe, int *px, int *py) } else { sy = 0; } + return sy; +} + +static void adjust_statusbar(frontend *fe, RECT *r) +{ + int sy; + + if (!fe->statusbar) return; + + sy = get_statusbar_height(fe); +#ifndef _WIN32_WCE + SetWindowPos(fe->statusbar, NULL, 0, r->bottom-r->top-sy, r->right-r->left, + sy, SWP_NOZORDER); +#endif +} + +static void get_menu_size(HWND wh, RECT *r) +{ + HMENU bar = GetMenu(wh); + RECT rect; + int i; + + SetRect(r, 0, 0, 0, 0); + for (i = 0; i < GetMenuItemCount(bar); i++) { + GetMenuItemRect(wh, bar, i, &rect); + UnionRect(r, r, &rect); + } +} + +/* + * Given a proposed new puzzle size (cx,cy), work out the actual + * puzzle size that would be (px,py) and the window size including + * furniture (wx,wy). + */ + +static int check_window_resize(frontend *fe, int cx, int cy, + int *px, int *py, + int *wx, int *wy) +{ + RECT r; + int x, y, sy = get_statusbar_height(fe), changed = 0; + + /* disallow making window thinner than menu bar */ + x = max(cx, fe->xmin); + y = max(cy - sy, fe->ymin); /* * 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, WINFLAGS, 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); + midend_size(fe->me, &x, &y, TRUE); + if (x != cx || y != cy) { + /* + * 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, WINFLAGS, TRUE, 0); + *wx = r.right - r.left; + *wy = r.bottom - r.top; + changed = 1; } *px = x; *py = y; + + fe->puzz_scale = + (float)midend_tilesize(fe->me) / (float)fe->game->preferred_tilesize; + + return changed; +} + +/* + * Given the current window size, make sure it's sane for the + * current puzzle and resize if necessary. + */ + +static void check_window_size(frontend *fe, int *px, int *py) +{ + RECT r; + int wx, wy, cx, cy; + + GetClientRect(fe->hwnd, &r); + cx = r.right - r.left; + cy = r.bottom - r.top; + + if (check_window_resize(fe, cx, cy, px, py, &wx, &wy)) { +#ifdef _WIN32_WCE + SetWindowPos(fe->hwnd, NULL, 0, 0, wx, wy, + SWP_NOMOVE | SWP_NOZORDER); +#endif + ; + } + + GetClientRect(fe->hwnd, &r); + adjust_statusbar(fe, &r); } static void get_max_puzzle_size(frontend *fe, int *x, int *y) @@ -1206,27 +1444,38 @@ static void get_max_puzzle_size(frontend *fe, int *x, int *y) } } -static frontend *new_window(HINSTANCE inst, char *game_id, char **error) +#ifdef _WIN32_WCE +/* Toolbar buttons on the numeric pad */ +static TBBUTTON tbNumpadButtons[] = +{ + {0, IDM_KEYEMUL + '1', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1}, + {1, IDM_KEYEMUL + '2', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1}, + {2, IDM_KEYEMUL + '3', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1}, + {3, IDM_KEYEMUL + '4', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1}, + {4, IDM_KEYEMUL + '5', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1}, + {5, IDM_KEYEMUL + '6', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1}, + {6, IDM_KEYEMUL + '7', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1}, + {7, IDM_KEYEMUL + '8', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1}, + {8, IDM_KEYEMUL + '9', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1}, + {9, IDM_KEYEMUL + ' ', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1} +}; +#endif + +/* + * Allocate a new frontend structure and create its main window. + */ +static frontend *frontend_new(HINSTANCE inst) { frontend *fe; - int x, y; - RECT r; + const char *nogame = "Puzzles (no game selected)"; fe = snew(frontend); - fe->me = midend_new(fe, &thegame, &win_drawing, fe); - - if (game_id) { - *error = midend_game_id(fe->me, game_id); - if (*error) { - midend_free(fe->me); - sfree(fe); - return NULL; - } - } - fe->inst = inst; + fe->game = NULL; + fe->me = NULL; + fe->timer = 0; fe->hwnd = NULL; @@ -1236,17 +1485,206 @@ static frontend *new_window(HINSTANCE inst, char *game_id, char **error) fe->dr = NULL; fe->fontstart = 0; - midend_new_game(fe->me); - fe->fonts = NULL; fe->nfonts = fe->fontsize = 0; + fe->colours = NULL; + fe->brushes = NULL; + fe->pens = NULL; + + fe->puzz_scale = 1.0; + + #ifdef _WIN32_WCE + MultiByteToWideChar (CP_ACP, 0, nogame, -1, wGameName, 256); + fe->hwnd = CreateWindowEx(0, wClassName, wGameName, + WS_VISIBLE, + CW_USEDEFAULT, CW_USEDEFAULT, + CW_USEDEFAULT, CW_USEDEFAULT, + NULL, NULL, inst, NULL); + + { + SHMENUBARINFO mbi; + RECT rc, rcBar, rcTB, rcClient; + + memset (&mbi, 0, sizeof(SHMENUBARINFO)); + mbi.cbSize = sizeof(SHMENUBARINFO); + mbi.hwndParent = fe->hwnd; + mbi.nToolBarId = IDR_MENUBAR1; + mbi.hInstRes = inst; + + SHCreateMenuBar(&mbi); + + GetWindowRect(fe->hwnd, &rc); + GetWindowRect(mbi.hwndMB, &rcBar); + rc.bottom -= rcBar.bottom - rcBar.top; + MoveWindow(fe->hwnd, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, FALSE); + + fe->numpad = NULL; + } +#else + fe->hwnd = CreateWindowEx(0, CLASSNAME, nogame, + WS_OVERLAPPEDWINDOW &~ + (WS_MAXIMIZEBOX), + CW_USEDEFAULT, CW_USEDEFAULT, + CW_USEDEFAULT, CW_USEDEFAULT, + NULL, NULL, inst, NULL); + if (!fe->hwnd) { + DWORD lerr = GetLastError(); + printf("no window: 0x%x\n", lerr); + } +#endif + + fe->gamemenu = NULL; + fe->presets = NULL; + + fe->statusbar = NULL; + fe->bitmap = NULL; + + SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe); + + return fe; +} + +static void savefile_write(void *wctx, void *buf, int len) +{ + FILE *fp = (FILE *)wctx; + fwrite(buf, 1, len, fp); +} + +static int savefile_read(void *wctx, void *buf, int len) +{ + FILE *fp = (FILE *)wctx; + int ret; + + ret = fread(buf, 1, len, fp); + return (ret == len); +} + +/* + * Create an appropriate midend structure to go in a puzzle window, + * given a game type and/or a command-line argument. + * + * 'arg' can be either a game ID string (descriptive, random, or a + * plain set of parameters) or the filename of a save file. The two + * boolean flag arguments indicate which possibilities are + * permissible. + */ +static midend *midend_for_new_game(frontend *fe, const game *cgame, + char *arg, int maybe_game_id, + int maybe_save_file, char **error) +{ + midend *me = NULL; + + if (!arg) { + if (me) midend_free(me); + me = midend_new(fe, cgame, &win_drawing, fe); + midend_new_game(me); + } else { + FILE *fp; + char *err_param, *err_load; + + /* + * See if arg is a valid filename of a save game file. + */ + err_load = NULL; + if (maybe_save_file && (fp = fopen(arg, "r")) != NULL) { + const game *loadgame; + +#ifdef COMBINED + /* + * Find out what kind of game is stored in the save + * file; if we're going to end up loading that, it + * will have to override our caller's judgment as to + * what game to initialise our midend with. + */ + char *id_name; + err_load = identify_game(&id_name, savefile_read, fp); + if (!err_load) { + int i; + for (i = 0; i < gamecount; i++) + if (!strcmp(id_name, gamelist[i]->name)) + break; + if (i == gamecount) { + err_load = "Save file is for a game not supported by" + " this program"; + } else { + loadgame = gamelist[i]; + rewind(fp); /* go back to the start for actual load */ + } + } +#else + loadgame = cgame; +#endif + if (!err_load) { + if (me) midend_free(me); + me = midend_new(fe, loadgame, &win_drawing, fe); + err_load = midend_deserialise(me, savefile_read, fp); + } + } else { + err_load = "Unable to open file"; + } + + if (maybe_game_id && (!maybe_save_file || err_load)) { + /* + * See if arg is a game description. + */ + if (me) midend_free(me); + me = midend_new(fe, cgame, &win_drawing, fe); + err_param = midend_game_id(me, arg); + if (!err_param) { + midend_new_game(me); + } else { + if (maybe_save_file) { + *error = snewn(256 + strlen(arg) + strlen(err_param) + + strlen(err_load), char); + sprintf(*error, "Supplied argument \"%s\" is neither a" + " game ID (%s) nor a save file (%s)", + arg, err_param, err_load); + } else { + *error = dupstr(err_param); + } + midend_free(me); + sfree(fe); + return NULL; + } + } else if (err_load) { + *error = dupstr(err_load); + midend_free(me); + sfree(fe); + return NULL; + } + } + + return me; +} + +/* + * Populate a frontend structure with a new midend structure, and + * create any window furniture that it needs. + * + * Previously-allocated memory and window furniture will be freed by + * this function. + * + */ +static int fe_set_midend(frontend *fe, midend *me) +{ + int x, y; + RECT r; + + if (fe->me) midend_free(fe->me); + fe->me = me; + fe->game = midend_which_game(fe->me); + { int i, ncolours; float *colours; colours = midend_colours(fe->me, &ncolours); + if (fe->colours) sfree(fe->colours); + if (fe->brushes) sfree(fe->brushes); + if (fe->pens) sfree(fe->pens); + fe->colours = snewn(ncolours, COLORREF); fe->brushes = snewn(ncolours, HBRUSH); fe->pens = snewn(ncolours, HPEN); @@ -1261,11 +1699,13 @@ static frontend *new_window(HINSTANCE inst, char *game_id, char **error) sfree(colours); } + if (fe->statusbar) + DestroyWindow(fe->statusbar); if (midend_wants_statusbar(fe->me)) { - fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh", + fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, TEXT("ooh"), WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, /* status bar does these */ - NULL, NULL, inst, NULL); + NULL, NULL, fe->inst, NULL); } else fe->statusbar = NULL; @@ -1277,53 +1717,107 @@ static frontend *new_window(HINSTANCE inst, char *game_id, char **error) r.bottom = y; AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0); - fe->hwnd = CreateWindowEx(0, thegame.name, thegame.name, - WS_OVERLAPPEDWINDOW &~ - (WS_THICKFRAME | WS_MAXIMIZEBOX), - CW_USEDEFAULT, CW_USEDEFAULT, - r.right - r.left, r.bottom - r.top, - NULL, NULL, inst, NULL); - - if (midend_wants_statusbar(fe->me)) { - RECT sr; - DestroyWindow(fe->statusbar); - fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh", - WS_CHILD | WS_VISIBLE, +#ifdef _WIN32_WCE + if (fe->numpad) + DestroyWindow(fe->numpad); + if (fe->game->flags & REQUIRE_NUMPAD) + { + fe->numpad = CreateToolbarEx (fe->hwnd, + WS_VISIBLE | WS_CHILD | CCS_NOPARENTALIGN | TBSTYLE_FLAT, + 0, 10, fe->inst, IDR_PADTOOLBAR, + tbNumpadButtons, sizeof (tbNumpadButtons) / sizeof (TBBUTTON), + 0, 0, 14, 15, sizeof (TBBUTTON)); + GetWindowRect(fe->numpad, &rcTB); + GetClientRect(fe->hwnd, &rcClient); + MoveWindow(fe->numpad, + 0, + rcClient.bottom - (rcTB.bottom - rcTB.top) - 1, + rcClient.right, + rcTB.bottom - rcTB.top, + FALSE); + SendMessage(fe->numpad, TB_SETINDENT, (rcClient.right - (10 * 21)) / 2, 0); + } + else { + fe->numpad = NULL; + } + MultiByteToWideChar (CP_ACP, 0, fe->game->name, -1, wGameName, 256); + SetWindowText(fe->hwnd, wGameName); +#else + SetWindowText(fe->hwnd, fe->game->name); +#endif + + if (fe->statusbar) + DestroyWindow(fe->statusbar); + if (midend_wants_statusbar(fe->me)) { + RECT sr; + fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, TEXT("ooh"), + WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, /* status bar does these */ - fe->hwnd, NULL, inst, NULL); + fe->hwnd, NULL, fe->inst, NULL); +#ifdef _WIN32_WCE + /* Flat status bar looks better on the Pocket PC */ + SendMessage(fe->statusbar, SB_SIMPLE, (WPARAM) TRUE, 0); + SendMessage(fe->statusbar, SB_SETTEXT, + (WPARAM) 255 | SBT_NOBORDERS, + (LPARAM) L""); +#endif + /* * Now resize the window to take account of the status bar. */ GetWindowRect(fe->statusbar, &sr); GetWindowRect(fe->hwnd, &r); +#ifndef _WIN32_WCE SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left, r.bottom - r.top + sr.bottom - sr.top, SWP_NOMOVE | SWP_NOZORDER); +#endif } else { fe->statusbar = NULL; } { + HMENU oldmenu = GetMenu(fe->hwnd); + +#ifndef _WIN32_WCE HMENU bar = CreateMenu(); HMENU menu = CreateMenu(); + RECT menusize; - 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_DESC, "Specific..."); - AppendMenu(menu, MF_ENABLED, IDM_SEED, "Random Seed..."); + AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "&Game"); +#else + HMENU menu = SHGetSubMenu(SHFindMenuBar(fe->hwnd), ID_GAME); + DeleteMenu(menu, 0, MF_BYPOSITION); +#endif + fe->gamemenu = menu; + AppendMenu(menu, MF_ENABLED, IDM_NEW, TEXT("&New")); + AppendMenu(menu, MF_ENABLED, IDM_RESTART, TEXT("&Restart")); +#ifndef _WIN32_WCE + /* ...here I run out of sensible accelerator characters. */ + AppendMenu(menu, MF_ENABLED, IDM_DESC, TEXT("Speci&fic...")); + AppendMenu(menu, MF_ENABLED, IDM_SEED, TEXT("Rando&m Seed...")); +#endif + if (fe->presets) + sfree(fe->presets); if ((fe->npresets = midend_num_presets(fe->me)) > 0 || - thegame.can_configure) { - HMENU sub = CreateMenu(); + fe->game->can_configure) { int i; +#ifndef _WIN32_WCE + HMENU sub = CreateMenu(); - AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "Type"); - + AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "&Type"); +#else + HMENU sub = SHGetSubMenu(SHFindMenuBar(fe->hwnd), ID_TYPE); + DeleteMenu(sub, 0, MF_BYPOSITION); +#endif fe->presets = snewn(fe->npresets, game_params *); for (i = 0; i < fe->npresets; i++) { char *name; +#ifdef _WIN32_WCE + TCHAR wName[255]; +#endif midend_fetch_preset(fe->me, i, &name, &fe->presets[i]); @@ -1332,66 +1826,138 @@ static frontend *new_window(HINSTANCE inst, char *game_id, char **error) * with ampersands here. */ +#ifndef _WIN32_WCE AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name); +#else + MultiByteToWideChar (CP_ACP, 0, name, -1, wName, 255); + AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, wName); +#endif } - - if (thegame.can_configure) { - AppendMenu(sub, MF_ENABLED, IDM_CONFIG, "Custom..."); + if (fe->game->can_configure) { + AppendMenu(sub, MF_ENABLED, IDM_CONFIG, TEXT("&Custom...")); } - } + + fe->typemenu = sub; + } else { + fe->typemenu = INVALID_HANDLE_VALUE; + fe->presets = NULL; + } + +#ifdef COMBINED +#ifdef _WIN32_WCE +#error Windows CE does not support COMBINED build. +#endif + { + HMENU games = CreateMenu(); + int i; + + AppendMenu(menu, MF_SEPARATOR, 0, 0); + AppendMenu(menu, MF_ENABLED|MF_POPUP, (UINT)games, "&Other"); + for (i = 0; i < gamecount; i++) { + if (strcmp(gamelist[i]->name, fe->game->name) != 0) { + /* only include those games that aren't the same as the + * game we're currently playing. */ + AppendMenu(games, MF_ENABLED, IDM_GAMES + i, gamelist[i]->name); + } + } + } +#endif AppendMenu(menu, MF_SEPARATOR, 0, 0); - AppendMenu(menu, MF_ENABLED, IDM_LOAD, "Load"); - AppendMenu(menu, MF_ENABLED, IDM_SAVE, "Save"); +#ifndef _WIN32_WCE + AppendMenu(menu, MF_ENABLED, IDM_LOAD, TEXT("&Load...")); + AppendMenu(menu, MF_ENABLED, IDM_SAVE, TEXT("&Save...")); AppendMenu(menu, MF_SEPARATOR, 0, 0); - if (thegame.can_print) { - AppendMenu(menu, MF_ENABLED, IDM_PRINT, "Print"); + if (fe->game->can_print) { + AppendMenu(menu, MF_ENABLED, IDM_PRINT, TEXT("&Print...")); AppendMenu(menu, MF_SEPARATOR, 0, 0); } - AppendMenu(menu, MF_ENABLED, IDM_UNDO, "Undo"); - AppendMenu(menu, MF_ENABLED, IDM_REDO, "Redo"); - if (thegame.can_format_as_text) { +#endif + AppendMenu(menu, MF_ENABLED, IDM_UNDO, TEXT("Undo")); + AppendMenu(menu, MF_ENABLED, IDM_REDO, TEXT("Redo")); +#ifndef _WIN32_WCE + if (fe->game->can_format_as_text_ever) { AppendMenu(menu, MF_SEPARATOR, 0, 0); - AppendMenu(menu, MF_ENABLED, IDM_COPY, "Copy"); + AppendMenu(menu, MF_ENABLED, IDM_COPY, TEXT("&Copy")); } - if (thegame.can_solve) { +#endif + if (fe->game->can_solve) { AppendMenu(menu, MF_SEPARATOR, 0, 0); - AppendMenu(menu, MF_ENABLED, IDM_SOLVE, "Solve"); + AppendMenu(menu, MF_ENABLED, IDM_SOLVE, TEXT("Sol&ve")); } AppendMenu(menu, MF_SEPARATOR, 0, 0); - AppendMenu(menu, MF_ENABLED, IDM_QUIT, "Exit"); +#ifndef _WIN32_WCE + AppendMenu(menu, MF_ENABLED, IDM_QUIT, TEXT("E&xit")); menu = CreateMenu(); - AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Help"); - AppendMenu(menu, MF_ENABLED, IDM_ABOUT, "About"); + AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, TEXT("&Help")); +#endif + AppendMenu(menu, MF_ENABLED, IDM_ABOUT, TEXT("&About")); +#ifndef _WIN32_WCE if (help_type != NONE) { - AppendMenu(menu, MF_SEPARATOR, 0, 0); - AppendMenu(menu, MF_ENABLED, IDM_HELPC, "Contents"); - if (help_topic) { - char *item; - assert(thegame.name); - item = snewn(9+strlen(thegame.name), char); /*ick*/ - sprintf(item, "Help on %s", thegame.name); - AppendMenu(menu, MF_ENABLED, IDM_GAMEHELP, item); - sfree(item); - } + char *item; + AppendMenu(menu, MF_SEPARATOR, 0, 0); + AppendMenu(menu, MF_ENABLED, IDM_HELPC, TEXT("&Contents")); + assert(fe->game->name); + item = snewn(10+strlen(fe->game->name), char); /*ick*/ + sprintf(item, "&Help on %s", fe->game->name); + AppendMenu(menu, MF_ENABLED, IDM_GAMEHELP, item); + sfree(item); } + DestroyMenu(oldmenu); SetMenu(fe->hwnd, bar); + get_menu_size(fe->hwnd, &menusize); + fe->xmin = (menusize.right - menusize.left) + 25; +#endif } + if (fe->bitmap) DeleteObject(fe->bitmap); fe->bitmap = NULL; - new_game_size(fe); /* initialises fe->bitmap */ - check_window_size(fe, &x, &y); + new_game_size(fe, fe->puzz_scale); /* initialises fe->bitmap */ - SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe); + return 0; +} - ShowWindow(fe->hwnd, SW_NORMAL); +static void show_window(frontend *fe) +{ + ShowWindow(fe->hwnd, SW_SHOWNORMAL); SetForegroundWindow(fe->hwnd); + update_type_menu_tick(fe); + update_copy_menu_greying(fe); + midend_redraw(fe->me); +} - return fe; +#ifdef _WIN32_WCE +static HFONT dialog_title_font() +{ + static HFONT hf = NULL; + LOGFONT lf; + + if (hf) + return hf; + + memset (&lf, 0, sizeof(LOGFONT)); + lf.lfHeight = -11; /* - ((8 * GetDeviceCaps(hdc, LOGPIXELSY)) / 72) */ + lf.lfWeight = FW_BOLD; + wcscpy(lf.lfFaceName, TEXT("Tahoma")); + + return hf = CreateFontIndirect(&lf); } +static void make_dialog_full_screen(HWND hwnd) +{ + SHINITDLGINFO shidi; + + /* Make dialog full screen */ + shidi.dwMask = SHIDIM_FLAGS; + shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIZEDLGFULLSCREEN | + SHIDIF_EMPTYMENU; + shidi.hDlg = hwnd; + SHInitDialog(&shidi); +} +#endif + static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { @@ -1399,17 +1965,39 @@ static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg, switch (msg) { case WM_INITDIALOG: - return 0; +#ifdef _WIN32_WCE + { + char title[256]; + + make_dialog_full_screen(hwnd); + + sprintf(title, "About %.250s", fe->game->name); + SetDlgItemTextA(hwnd, IDC_ABOUT_CAPTION, title); + + SendDlgItemMessage(hwnd, IDC_ABOUT_CAPTION, WM_SETFONT, + (WPARAM) dialog_title_font(), 0); + + SetDlgItemTextA(hwnd, IDC_ABOUT_GAME, fe->game->name); + SetDlgItemTextA(hwnd, IDC_ABOUT_VERSION, ver); + } +#endif + return TRUE; case WM_COMMAND: - if ((HIWORD(wParam) == BN_CLICKED || - HIWORD(wParam) == BN_DOUBLECLICKED) && - LOWORD(wParam) == IDOK) + if (LOWORD(wParam) == IDOK) +#ifdef _WIN32_WCE + EndDialog(hwnd, 1); +#else fe->dlg_done = 1; +#endif return 0; case WM_CLOSE: +#ifdef _WIN32_WCE + EndDialog(hwnd, 1); +#else fe->dlg_done = 1; +#endif return 0; } @@ -1429,8 +2017,8 @@ static config_item *frontend_get_config(frontend *fe, int which, config_item *ret; int i; - *wintitle = snewn(40 + strlen(thegame.name), char); - sprintf(*wintitle, "%s print setup", thegame.name); + *wintitle = snewn(40 + strlen(fe->game->name), char); + sprintf(*wintitle, "%s print setup", fe->game->name); ret = snewn(8, config_item); @@ -1472,7 +2060,7 @@ static config_item *frontend_get_config(frontend *fe, int which, ret[i].ival = FALSE; i++; - if (thegame.can_print_in_colour) { + if (fe->game->can_print_in_colour) { ret[i].name = "Print in colour"; ret[i].type = C_BOOLEAN; ret[i].sval = NULL; @@ -1508,7 +2096,7 @@ static char *frontend_set_config(frontend *fe, int which, config_item *cfg) return "Print size should be positive"; fe->printcurr = cfg[4].ival; fe->printsolns = cfg[5].ival; - fe->printcolour = thegame.can_print_in_colour && cfg[6].ival; + fe->printcolour = fe->game->can_print_in_colour && cfg[6].ival; return NULL; } else { assert(!"We should never get here"); @@ -1516,6 +2104,117 @@ static char *frontend_set_config(frontend *fe, int which, config_item *cfg) } } +#ifdef _WIN32_WCE +/* Separate version of mkctrl function for the Pocket PC. */ +/* Control coordinates should be specified in dialog units. */ +HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2, + LPCTSTR wclass, int wstyle, + int exstyle, const char *wtext, int wid) +{ + RECT rc; + TCHAR wwtext[256]; + + /* Convert dialog units into pixels */ + rc.left = x1; rc.right = x2; + rc.top = y1; rc.bottom = y2; + MapDialogRect(fe->cfgbox, &rc); + + MultiByteToWideChar (CP_ACP, 0, wtext, -1, wwtext, 256); + + return CreateWindowEx(exstyle, wclass, wwtext, + wstyle | WS_CHILD | WS_VISIBLE, + rc.left, rc.top, + rc.right - rc.left, rc.bottom - rc.top, + fe->cfgbox, (HMENU) wid, fe->inst, NULL); +} + +static void create_config_controls(frontend * fe) +{ + int id, nctrls; + int col1l, col1r, col2l, col2r, y; + config_item *i; + struct cfg_aux *j; + HWND ctl; + + /* Control placement done in dialog units */ + col1l = 4; col1r = 96; /* Label column */ + col2l = 100; col2r = 154; /* Input column (edit boxes and combo boxes) */ + + /* + * Count the controls so we can allocate cfgaux. + */ + for (nctrls = 0, i = fe->cfg; i->type != C_END; i++) + nctrls++; + fe->cfgaux = snewn(nctrls, struct cfg_aux); + + id = 1000; + y = 22; /* Leave some room for the dialog title */ + for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) { + switch (i->type) { + case C_STRING: + /* + * Edit box with a label beside it. + */ + mkctrl(fe, col1l, col1r, y + 1, y + 11, + TEXT("Static"), SS_LEFTNOWORDWRAP, 0, i->name, id++); + mkctrl(fe, col2l, col2r, y, y + 12, + TEXT("EDIT"), WS_BORDER | WS_TABSTOP | ES_AUTOHSCROLL, + 0, "", (j->ctlid = id++)); + SetDlgItemTextA(fe->cfgbox, j->ctlid, i->sval); + break; + + case C_BOOLEAN: + /* + * Simple checkbox. + */ + mkctrl(fe, col1l, col2r, y + 1, y + 11, TEXT("BUTTON"), + BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP, + 0, i->name, (j->ctlid = id++)); + CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0)); + break; + + case C_CHOICES: + /* + * Drop-down list with a label beside it. + */ + mkctrl(fe, col1l, col1r, y + 1, y + 11, + TEXT("STATIC"), SS_LEFTNOWORDWRAP, 0, i->name, id++); + ctl = mkctrl(fe, col2l, col2r, y, y + 48, + TEXT("COMBOBOX"), WS_BORDER | WS_TABSTOP | + CBS_DROPDOWNLIST | CBS_HASSTRINGS, + 0, "", (j->ctlid = id++)); + { + char c, *p, *q, *str; + + p = i->sval; + c = *p++; + while (*p) { + q = p; + while (*q && *q != c) q++; + str = snewn(q-p+1, char); + strncpy(str, p, q-p); + str[q-p] = '\0'; + { + TCHAR ws[50]; + MultiByteToWideChar (CP_ACP, 0, str, -1, ws, 50); + SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)ws); + } + + sfree(str); + if (*q) q++; + p = q; + } + } + SendMessage(ctl, CB_SETCURSEL, i->ival, 0); + break; + } + + y += 15; + } + +} +#endif + static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { @@ -1525,15 +2224,32 @@ static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg, switch (msg) { case WM_INITDIALOG: - return 0; +#ifdef _WIN32_WCE + { + char *title; + + fe = (frontend *) lParam; + SetWindowLong(hwnd, GWL_USERDATA, lParam); + fe->cfgbox = hwnd; + + fe->cfg = frontend_get_config(fe, fe->cfg_which, &title); + + make_dialog_full_screen(hwnd); + + SetDlgItemTextA(hwnd, IDC_CONFIG_CAPTION, title); + SendDlgItemMessage(hwnd, IDC_CONFIG_CAPTION, WM_SETFONT, + (WPARAM) dialog_title_font(), 0); + + create_config_controls(fe); + } +#endif + return TRUE; case WM_COMMAND: /* * OK and Cancel are special cases. */ - if ((HIWORD(wParam) == BN_CLICKED || - HIWORD(wParam) == BN_DOUBLECLICKED) && - (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) { + if ((LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) { if (LOWORD(wParam) == IDOK) { char *err = frontend_set_config(fe, fe->cfg_which, fe->cfg); @@ -1541,10 +2257,18 @@ static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg, MessageBox(hwnd, err, "Validation error", MB_ICONERROR | MB_OK); } else { +#ifdef _WIN32_WCE + EndDialog(hwnd, 2); +#else fe->dlg_done = 2; +#endif } } else { +#ifdef _WIN32_WCE + EndDialog(hwnd, 1); +#else fe->dlg_done = 1; +#endif } return 0; } @@ -1561,13 +2285,19 @@ static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg, if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) { char buffer[4096]; +#ifdef _WIN32_WCE + TCHAR wBuffer[4096]; + GetDlgItemText(fe->cfgbox, j->ctlid, wBuffer, 4096); + WideCharToMultiByte(CP_ACP, 0, wBuffer, -1, buffer, 4096, NULL, NULL); +#else GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer)); +#endif buffer[lenof(buffer)-1] = '\0'; sfree(i->sval); i->sval = dupstr(buffer); } else if (i->type == C_BOOLEAN && (HIWORD(wParam) == BN_CLICKED || - HIWORD(wParam) == BN_DOUBLECLICKED)) { + HIWORD(wParam) == BN_DBLCLK)) { i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid); } else if (i->type == C_CHOICES && HIWORD(wParam) == CBN_SELCHANGE) { @@ -1585,6 +2315,7 @@ static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg, return 0; } +#ifndef _WIN32_WCE HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2, char *wclass, int wstyle, int exstyle, const char *wtext, int wid) @@ -1596,9 +2327,13 @@ HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2, SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0)); return ret; } +#endif static void about(frontend *fe) { +#ifdef _WIN32_WCE + DialogBox(fe->inst, MAKEINTRESOURCE(IDD_ABOUT), fe->hwnd, AboutDlgProc); +#else int i; WNDCLASS wc; MSG msg; @@ -1614,13 +2349,13 @@ static void about(frontend *fe) int nstrings = 0; char titlebuf[512]; - sprintf(titlebuf, "About %.250s", thegame.name); + sprintf(titlebuf, "About %.250s", fe->game->name); - strings[nstrings++] = thegame.name; + strings[nstrings++] = fe->game->name; strings[nstrings++] = "from Simon Tatham's Portable Puzzle Collection"; strings[nstrings++] = ver; - wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW; + wc.style = CS_DBLCLKS | CS_SAVEBITS; wc.lpfnWndProc = DefDlgProc; wc.cbClsExtra = 0; wc.cbWndExtra = DLGWINDOWEXTRA + 8; @@ -1730,13 +2465,13 @@ static void about(frontend *fe) y += height/2; /* extra space before OK */ mkctrl(fe, width*2, maxwid+width*2, y, y+height*7/4, "BUTTON", - BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0, + BS_PUSHBUTTON | WS_TABSTOP | BS_DEFPUSHBUTTON, 0, "OK", IDOK); SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0); EnableWindow(fe->hwnd, FALSE); - ShowWindow(fe->cfgbox, SW_NORMAL); + ShowWindow(fe->cfgbox, SW_SHOWNORMAL); while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) { if (!IsDialogMessage(fe->cfgbox, &msg)) DispatchMessage(&msg); @@ -1747,10 +2482,19 @@ static void about(frontend *fe) SetForegroundWindow(fe->hwnd); DestroyWindow(fe->cfgbox); DeleteObject(fe->cfgfont); +#endif } static int get_config(frontend *fe, int which) { +#ifdef _WIN32_WCE + fe->cfg_which = which; + + return DialogBoxParam(fe->inst, + MAKEINTRESOURCE(IDD_CONFIG), + fe->hwnd, ConfigDlgProc, + (LPARAM) fe) == 2; +#else config_item *i; struct cfg_aux *j; char *title; @@ -1765,7 +2509,7 @@ static int get_config(frontend *fe, int which) int winwidth, winheight, col1l, col1r, col2l, col2r, y; int height, width, maxlabel, maxcheckbox; - wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW; + wc.style = CS_DBLCLKS | CS_SAVEBITS; wc.lpfnWndProc = DefDlgProc; wc.cbClsExtra = 0; wc.cbWndExtra = DLGWINDOWEXTRA + 8; @@ -1964,15 +2708,15 @@ static int get_config(frontend *fe, int which) y += height/2; /* extra space before OK and Cancel */ mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON", - BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0, + BS_PUSHBUTTON | WS_TABSTOP | BS_DEFPUSHBUTTON, 0, "OK", IDOK); mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON", - BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL); + BS_PUSHBUTTON | WS_TABSTOP, 0, "Cancel", IDCANCEL); SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0); EnableWindow(fe->hwnd, FALSE); - ShowWindow(fe->cfgbox, SW_NORMAL); + ShowWindow(fe->cfgbox, SW_SHOWNORMAL); while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) { if (!IsDialogMessage(fe->cfgbox, &msg)) DispatchMessage(&msg); @@ -1988,17 +2732,75 @@ static int get_config(frontend *fe, int which) sfree(fe->cfgaux); return (fe->dlg_done == 2); +#endif } -static void new_game_size(frontend *fe) +#ifdef _WIN32_WCE +static void calculate_bitmap_position(frontend *fe, int x, int y) +{ + /* Pocket PC - center the game in the full screen window */ + int yMargin; + RECT rcClient; + + GetClientRect(fe->hwnd, &rcClient); + fe->bitmapPosition.left = (rcClient.right - x) / 2; + yMargin = rcClient.bottom - y; + + if (fe->numpad != NULL) { + RECT rcPad; + GetWindowRect(fe->numpad, &rcPad); + yMargin -= rcPad.bottom - rcPad.top; + } + + if (fe->statusbar != NULL) { + RECT rcStatus; + GetWindowRect(fe->statusbar, &rcStatus); + yMargin -= rcStatus.bottom - rcStatus.top; + } + + fe->bitmapPosition.top = yMargin / 2; + + fe->bitmapPosition.right = fe->bitmapPosition.left + x; + fe->bitmapPosition.bottom = fe->bitmapPosition.top + y; +} +#else +static void calculate_bitmap_position(frontend *fe, int x, int y) +{ + /* Plain Windows - position the game in the upper-left corner */ + fe->bitmapPosition.left = 0; + fe->bitmapPosition.top = 0; + fe->bitmapPosition.right = fe->bitmapPosition.left + x; + fe->bitmapPosition.bottom = fe->bitmapPosition.top + y; +} +#endif + +static void new_bitmap(frontend *fe, int x, int y) { - RECT r, sr; HDC hdc; + + if (fe->bitmap) DeleteObject(fe->bitmap); + + hdc = GetDC(fe->hwnd); + fe->bitmap = CreateCompatibleBitmap(hdc, x, y); + calculate_bitmap_position(fe, x, y); + ReleaseDC(fe->hwnd, hdc); +} + +static void new_game_size(frontend *fe, float scale) +{ + RECT r, sr; int x, y; get_max_puzzle_size(fe, &x, &y); midend_size(fe->me, &x, &y, FALSE); + if (scale != 1.0) { + x = (int)((float)x * fe->puzz_scale); + y = (int)((float)y * fe->puzz_scale); + midend_size(fe->me, &x, &y, TRUE); + } + fe->ymin = (fe->xmin * y) / x; + r.left = r.top = 0; r.right = x; r.bottom = y; @@ -2009,30 +2811,113 @@ static void new_game_size(frontend *fe) } else { sr.left = sr.right = sr.top = sr.bottom = 0; } +#ifndef _WIN32_WCE SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left, r.bottom - r.top + sr.bottom - sr.top, SWP_NOMOVE | SWP_NOZORDER); +#endif check_window_size(fe, &x, &y); +#ifndef _WIN32_WCE if (fe->statusbar != NULL) SetWindowPos(fe->statusbar, NULL, 0, y, x, sr.bottom - sr.top, SWP_NOZORDER); +#endif - if (fe->bitmap) DeleteObject(fe->bitmap); - - hdc = GetDC(fe->hwnd); - fe->bitmap = CreateCompatibleBitmap(hdc, x, y); - ReleaseDC(fe->hwnd, hdc); + new_bitmap(fe, x, y); +#ifdef _WIN32_WCE + InvalidateRect(fe->hwnd, NULL, TRUE); +#endif midend_redraw(fe->me); } +/* + * Given a proposed new window rect, work out the resulting + * difference in client size (from current), and use to try + * and resize the puzzle, returning (wx,wy) as the actual + * new window size. + */ + +static void adjust_game_size(frontend *fe, RECT *proposed, int isedge, + int *wx_r, int *wy_r) +{ + RECT cr, wr; + int nx, ny, xdiff, ydiff, wx, wy; + + /* Work out the current window sizing, and thus the + * difference in size we're asking for. */ + GetClientRect(fe->hwnd, &cr); + wr = cr; + AdjustWindowRectEx(&wr, WINFLAGS, TRUE, 0); + + xdiff = (proposed->right - proposed->left) - (wr.right - wr.left); + ydiff = (proposed->bottom - proposed->top) - (wr.bottom - wr.top); + + if (isedge) { + /* These next four lines work around the fact that midend_size + * is happy to shrink _but not grow_ if you change one dimension + * but not the other. */ + if (xdiff > 0 && ydiff == 0) + ydiff = (xdiff * (wr.right - wr.left)) / (wr.bottom - wr.top); + if (xdiff == 0 && ydiff > 0) + xdiff = (ydiff * (wr.bottom - wr.top)) / (wr.right - wr.left); + } + + if (check_window_resize(fe, + (cr.right - cr.left) + xdiff, + (cr.bottom - cr.top) + ydiff, + &nx, &ny, &wx, &wy)) { + new_bitmap(fe, nx, ny); + midend_force_redraw(fe->me); + } else { + /* reset size to current window size */ + wx = wr.right - wr.left; + wy = wr.bottom - wr.top; + } + /* Re-fetch rectangle; size limits mean we might not have + * taken it quite to the mouse drag positions. */ + GetClientRect(fe->hwnd, &cr); + adjust_statusbar(fe, &cr); + + *wx_r = wx; *wy_r = wy; +} + +static void update_type_menu_tick(frontend *fe) +{ + int total, n, i; + + if (fe->typemenu == INVALID_HANDLE_VALUE) + return; + + total = GetMenuItemCount(fe->typemenu); + n = midend_which_preset(fe->me); + if (n < 0) + n = total - 1; /* "Custom" item */ + + for (i = 0; i < total; i++) { + int flag = (i == n ? MF_CHECKED : MF_UNCHECKED); + CheckMenuItem(fe->typemenu, i, MF_BYPOSITION | flag); + } + + DrawMenuBar(fe->hwnd); +} + +static void update_copy_menu_greying(frontend *fe) +{ + UINT enable = (midend_can_format_as_text_now(fe->me) ? + MF_ENABLED : MF_GRAYED); + EnableMenuItem(fe->gamemenu, IDM_COPY, MF_BYCOMMAND | enable); +} + static void new_game_type(frontend *fe) { midend_new_game(fe->me); - new_game_size(fe); + new_game_size(fe, 1.0); + update_type_menu_tick(fe); + update_copy_menu_greying(fe); } static int is_alt_pressed(void) @@ -2048,21 +2933,6 @@ static int is_alt_pressed(void) return FALSE; } -static void savefile_write(void *wctx, void *buf, int len) -{ - FILE *fp = (FILE *)wctx; - fwrite(buf, 1, len, fp); -} - -static int savefile_read(void *wctx, void *buf, int len) -{ - FILE *fp = (FILE *)wctx; - int ret; - - ret = fread(buf, 1, len, fp); - return (ret == len); -} - static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { @@ -2074,6 +2944,13 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, DestroyWindow(hwnd); return 0; case WM_COMMAND: +#ifdef _WIN32_WCE + /* Numeric pad sends WM_COMMAND messages */ + if ((wParam >= IDM_KEYEMUL) && (wParam < IDM_KEYEMUL + 256)) + { + midend_process_key(fe->me, 0, 0, wParam - IDM_KEYEMUL); + } +#endif cmd = wParam & ~0xF; /* low 4 bits reserved to Windows */ switch (cmd) { case IDM_NEW: @@ -2195,7 +3072,11 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, fclose(fp); } else { FILE *fp = fopen(filename, "r"); - char *err; + char *err = NULL; + midend *me = fe->me; +#ifdef COMBINED + char *id_name; +#endif if (!fp) { MessageBox(hwnd, "Unable to open saved game file", @@ -2203,7 +3084,30 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, break; } - err = midend_deserialise(fe->me, savefile_read, fp); +#ifdef COMBINED + /* + * This save file might be from a different + * game. + */ + err = identify_game(&id_name, savefile_read, fp); + if (!err) { + int i; + for (i = 0; i < gamecount; i++) + if (!strcmp(id_name, gamelist[i]->name)) + break; + if (i == gamecount) { + err = "Save file is for a game not " + "supported by this program"; + } else { + me = midend_for_new_game(fe, gamelist[i], NULL, + FALSE, FALSE, &err); + rewind(fp); /* for the actual load */ + } + sfree(id_name); + } +#endif + if (!err) + err = midend_deserialise(me, savefile_read, fp); fclose(fp); @@ -2212,19 +3116,34 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, break; } - new_game_size(fe); + if (fe->me != me) + fe_set_midend(fe, me); + new_game_size(fe, 1.0); } } } break; +#ifndef _WIN32_WCE case IDM_HELPC: start_help(fe, NULL); break; case IDM_GAMEHELP: - start_help(fe, help_topic); + assert(help_type != NONE); + start_help(fe, help_type == CHM ? + fe->game->htmlhelp_topic : fe->game->winhelp_topic); break; +#endif default: +#ifdef COMBINED + if (wParam >= IDM_GAMES && wParam < (IDM_GAMES + (WPARAM)gamecount)) { + int p = wParam - IDM_GAMES; + char *error = NULL; + fe_set_midend(fe, midend_for_new_game(fe, gamelist[p], NULL, + FALSE, FALSE, &error)); + sfree(error); + } else +#endif { int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10; @@ -2237,24 +3156,33 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, } break; case WM_DESTROY: +#ifndef _WIN32_WCE stop_help(fe); - PostQuitMessage(0); +#endif + frontend_free(fe); + PostQuitMessage(0); return 0; case WM_PAINT: { PAINTSTRUCT p; HDC hdc, hdc2; HBITMAP prevbm; + RECT rcDest; hdc = BeginPaint(hwnd, &p); hdc2 = CreateCompatibleDC(hdc); prevbm = SelectObject(hdc2, fe->bitmap); +#ifdef _WIN32_WCE + FillRect(hdc, &(p.rcPaint), (HBRUSH) GetStockObject(WHITE_BRUSH)); +#endif + IntersectRect(&rcDest, &(fe->bitmapPosition), &(p.rcPaint)); BitBlt(hdc, - p.rcPaint.left, p.rcPaint.top, - p.rcPaint.right - p.rcPaint.left, - p.rcPaint.bottom - p.rcPaint.top, + rcDest.left, rcDest.top, + rcDest.right - rcDest.left, + rcDest.bottom - rcDest.top, hdc2, - p.rcPaint.left, p.rcPaint.top, + rcDest.left - fe->bitmapPosition.left, + rcDest.top - fe->bitmapPosition.top, SRCCOPY); SelectObject(hdc2, prevbm); DeleteDC(hdc2); @@ -2359,10 +3287,32 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, else if (message == WM_RBUTTONDOWN || is_alt_pressed()) button = RIGHT_BUTTON; else +#ifndef _WIN32_WCE button = LEFT_BUTTON; +#else + if ((fe->game->flags & REQUIRE_RBUTTON) == 0) + button = LEFT_BUTTON; + else + { + SHRGINFO shrgi; + + shrgi.cbSize = sizeof(SHRGINFO); + shrgi.hwndClient = hwnd; + shrgi.ptDown.x = (signed short)LOWORD(lParam); + shrgi.ptDown.y = (signed short)HIWORD(lParam); + shrgi.dwFlags = SHRG_RETURNCMD; + + if (GN_CONTEXTMENU == SHRecognizeGesture(&shrgi)) + button = RIGHT_BUTTON; + else + button = LEFT_BUTTON; + } +#endif - if (!midend_process_key(fe->me, (signed short)LOWORD(lParam), - (signed short)HIWORD(lParam), button)) + if (!midend_process_key(fe->me, + (signed short)LOWORD(lParam) - fe->bitmapPosition.left, + (signed short)HIWORD(lParam) - fe->bitmapPosition.top, + button)) PostQuitMessage(0); SetCapture(hwnd); @@ -2386,8 +3336,10 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, else button = LEFT_RELEASE; - if (!midend_process_key(fe->me, (signed short)LOWORD(lParam), - (signed short)HIWORD(lParam), button)) + if (!midend_process_key(fe->me, + (signed short)LOWORD(lParam) - fe->bitmapPosition.left, + (signed short)HIWORD(lParam) - fe->bitmapPosition.top, + button)) PostQuitMessage(0); ReleaseCapture(); @@ -2404,8 +3356,10 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, else button = LEFT_DRAG; - if (!midend_process_key(fe->me, (signed short)LOWORD(lParam), - (signed short)HIWORD(lParam), button)) + if (!midend_process_key(fe->me, + (signed short)LOWORD(lParam) - fe->bitmapPosition.left, + (signed short)HIWORD(lParam) - fe->bitmapPosition.top, + button)) PostQuitMessage(0); } break; @@ -2421,15 +3375,297 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, fe->timer_last_tickcount = now; } return 0; +#ifndef _WIN32_WCE + case WM_SIZING: + { + RECT *sr = (RECT *)lParam; + int wx, wy, isedge = 0; + + if (wParam == WMSZ_TOP || + wParam == WMSZ_RIGHT || + wParam == WMSZ_BOTTOM || + wParam == WMSZ_LEFT) isedge = 1; + adjust_game_size(fe, sr, isedge, &wx, &wy); + + /* Given the window size the puzzles constrain + * us to, work out which edge we should be moving. */ + if (wParam == WMSZ_TOP || + wParam == WMSZ_TOPLEFT || + wParam == WMSZ_TOPRIGHT) { + sr->top = sr->bottom - wy; + } else { + sr->bottom = sr->top + wy; + } + if (wParam == WMSZ_LEFT || + wParam == WMSZ_TOPLEFT || + wParam == WMSZ_BOTTOMLEFT) { + sr->left = sr->right - wx; + } else { + sr->right = sr->left + wx; + } + return TRUE; + } + break; +#endif } return DefWindowProc(hwnd, message, wParam, lParam); } +#ifdef _WIN32_WCE +static int FindPreviousInstance() +{ + /* Check if application is running. If it's running then focus on the window */ + HWND hOtherWnd = NULL; + + hOtherWnd = FindWindow (wGameName, wGameName); + if (hOtherWnd) + { + SetForegroundWindow (hOtherWnd); + return TRUE; + } + + return FALSE; +} +#endif + +/* + * Split a complete command line into argc/argv, attempting to do it + * exactly the same way the Visual Studio C library would do it (so + * that our console utilities, which receive argc and argv already + * broken apart by the C library, will have their command lines + * processed in the same way as the GUI utilities which get a whole + * command line and must call this function). + * + * Does not modify the input command line. + * + * The final parameter (argstart) is used to return a second array + * of char * pointers, the same length as argv, each one pointing + * at the start of the corresponding element of argv in the + * original command line. So if you get half way through processing + * your command line in argc/argv form and then decide you want to + * treat the rest as a raw string, you can. If you don't want to, + * `argstart' can be safely left NULL. + */ +void split_into_argv(char *cmdline, int *argc, char ***argv, + char ***argstart) +{ + char *p; + char *outputline, *q; + char **outputargv, **outputargstart; + int outputargc; + + /* + * These argument-breaking rules apply to Visual Studio 7, which + * is currently the compiler expected to be used for the Windows + * port of my puzzles. Visual Studio 10 has different rules, + * lacking the curious mod 3 behaviour of consecutive quotes + * described below; I presume they fixed a bug. As and when we + * migrate to a newer compiler, we'll have to adjust this to + * match; however, for the moment we faithfully imitate in our GUI + * utilities what our CLI utilities can't be prevented from doing. + * + * When I investigated this, at first glance the rules appeared to + * be: + * + * - Single quotes are not special characters. + * + * - Double quotes are removed, but within them spaces cease + * to be special. + * + * - Backslashes are _only_ special when a sequence of them + * appear just before a double quote. In this situation, + * they are treated like C backslashes: so \" just gives a + * literal quote, \\" gives a literal backslash and then + * opens or closes a double-quoted segment, \\\" gives a + * literal backslash and then a literal quote, \\\\" gives + * two literal backslashes and then opens/closes a + * double-quoted segment, and so forth. Note that this + * behaviour is identical inside and outside double quotes. + * + * - Two successive double quotes become one literal double + * quote, but only _inside_ a double-quoted segment. + * Outside, they just form an empty double-quoted segment + * (which may cause an empty argument word). + * + * - That only leaves the interesting question of what happens + * when one or more backslashes precedes two or more double + * quotes, starting inside a double-quoted string. And the + * answer to that appears somewhat bizarre. Here I tabulate + * number of backslashes (across the top) against number of + * quotes (down the left), and indicate how many backslashes + * are output, how many quotes are output, and whether a + * quoted segment is open at the end of the sequence: + * + * backslashes + * + * 0 1 2 3 4 + * + * 0 0,0,y | 1,0,y 2,0,y 3,0,y 4,0,y + * --------+----------------------------- + * 1 0,0,n | 0,1,y 1,0,n 1,1,y 2,0,n + * q 2 0,1,n | 0,1,n 1,1,n 1,1,n 2,1,n + * u 3 0,1,y | 0,2,n 1,1,y 1,2,n 2,1,y + * o 4 0,1,n | 0,2,y 1,1,n 1,2,y 2,1,n + * t 5 0,2,n | 0,2,n 1,2,n 1,2,n 2,2,n + * e 6 0,2,y | 0,3,n 1,2,y 1,3,n 2,2,y + * s 7 0,2,n | 0,3,y 1,2,n 1,3,y 2,2,n + * 8 0,3,n | 0,3,n 1,3,n 1,3,n 2,3,n + * 9 0,3,y | 0,4,n 1,3,y 1,4,n 2,3,y + * 10 0,3,n | 0,4,y 1,3,n 1,4,y 2,3,n + * 11 0,4,n | 0,4,n 1,4,n 1,4,n 2,4,n + * + * + * [Test fragment was of the form "a\\\"""b c" d.] + * + * There is very weird mod-3 behaviour going on here in the + * number of quotes, and it even applies when there aren't any + * backslashes! How ghastly. + * + * With a bit of thought, this extremely odd diagram suddenly + * coalesced itself into a coherent, if still ghastly, model of + * how things work: + * + * - As before, backslashes are only special when one or more + * of them appear contiguously before at least one double + * quote. In this situation the backslashes do exactly what + * you'd expect: each one quotes the next thing in front of + * it, so you end up with n/2 literal backslashes (if n is + * even) or (n-1)/2 literal backslashes and a literal quote + * (if n is odd). In the latter case the double quote + * character right after the backslashes is used up. + * + * - After that, any remaining double quotes are processed. A + * string of contiguous unescaped double quotes has a mod-3 + * behaviour: + * + * * inside a quoted segment, a quote ends the segment. + * * _immediately_ after ending a quoted segment, a quote + * simply produces a literal quote. + * * otherwise, outside a quoted segment, a quote begins a + * quoted segment. + * + * So, for example, if we started inside a quoted segment + * then two contiguous quotes would close the segment and + * produce a literal quote; three would close the segment, + * produce a literal quote, and open a new segment. If we + * started outside a quoted segment, then two contiguous + * quotes would open and then close a segment, producing no + * output (but potentially creating a zero-length argument); + * but three quotes would open and close a segment and then + * produce a literal quote. + */ + + /* + * First deal with the simplest of all special cases: if there + * aren't any arguments, return 0,NULL,NULL. + */ + while (*cmdline && isspace(*cmdline)) cmdline++; + if (!*cmdline) { + if (argc) *argc = 0; + if (argv) *argv = NULL; + if (argstart) *argstart = NULL; + return; + } + + /* + * This will guaranteeably be big enough; we can realloc it + * down later. + */ + outputline = snewn(1+strlen(cmdline), char); + outputargv = snewn(strlen(cmdline)+1 / 2, char *); + outputargstart = snewn(strlen(cmdline)+1 / 2, char *); + + p = cmdline; q = outputline; outputargc = 0; + + while (*p) { + int quote; + + /* Skip whitespace searching for start of argument. */ + while (*p && isspace(*p)) p++; + if (!*p) break; + + /* We have an argument; start it. */ + outputargv[outputargc] = q; + outputargstart[outputargc] = p; + outputargc++; + quote = 0; + + /* Copy data into the argument until it's finished. */ + while (*p) { + if (!quote && isspace(*p)) + break; /* argument is finished */ + + if (*p == '"' || *p == '\\') { + /* + * We have a sequence of zero or more backslashes + * followed by a sequence of zero or more quotes. + * Count up how many of each, and then deal with + * them as appropriate. + */ + int i, slashes = 0, quotes = 0; + while (*p == '\\') slashes++, p++; + while (*p == '"') quotes++, p++; + + if (!quotes) { + /* + * Special case: if there are no quotes, + * slashes are not special at all, so just copy + * n slashes to the output string. + */ + while (slashes--) *q++ = '\\'; + } else { + /* Slashes annihilate in pairs. */ + while (slashes >= 2) slashes -= 2, *q++ = '\\'; + + /* One remaining slash takes out the first quote. */ + if (slashes) quotes--, *q++ = '"'; + + if (quotes > 0) { + /* Outside a quote segment, a quote starts one. */ + if (!quote) quotes--, quote = 1; + + /* Now we produce (n+1)/3 literal quotes... */ + for (i = 3; i <= quotes+1; i += 3) *q++ = '"'; + + /* ... and end in a quote segment iff 3 divides n. */ + quote = (quotes % 3 == 0); + } + } + } else { + *q++ = *p++; + } + } + + /* At the end of an argument, just append a trailing NUL. */ + *q++ = '\0'; + } + + outputargv = sresize(outputargv, outputargc, char *); + outputargstart = sresize(outputargstart, outputargc, char *); + + if (argc) *argc = outputargc; + if (argv) *argv = outputargv; else sfree(outputargv); + if (argstart) *argstart = outputargstart; else sfree(outputargstart); +} + int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) { MSG msg; char *error; + const game *gg; + frontend *fe; + midend *me; + int argc; + char **argv; + + split_into_argv(cmdline, &argc, &argv, NULL); + +#ifdef _WIN32_WCE + MultiByteToWideChar (CP_ACP, 0, CLASSNAME, -1, wClassName, 256); + if (FindPreviousInstance ()) + return 0; +#endif InitCommonControls(); @@ -2442,12 +3678,18 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) wndclass.cbWndExtra = 0; wndclass.hInstance = inst; wndclass.hIcon = LoadIcon(inst, MAKEINTRESOURCE(200)); +#ifndef _WIN32_WCE if (!wndclass.hIcon) /* in case resource file is absent */ wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION); +#endif wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = NULL; wndclass.lpszMenuName = NULL; - wndclass.lpszClassName = thegame.name; +#ifdef _WIN32_WCE + wndclass.lpszClassName = wClassName; +#else + wndclass.lpszClassName = CLASSNAME; +#endif RegisterClass(&wndclass); } @@ -2457,18 +3699,61 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) init_help(); - if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) { +#ifdef COMBINED + gg = gamelist[0]; + if (argc > 0) { + int i; + for (i = 0; i < gamecount; i++) { + const char *p = gamelist[i]->name; + char *q = argv[0]; + while (*p && *q) { + if (isspace((unsigned char)*p)) { + while (*q && isspace((unsigned char)*q)) + q++; + } else { + if (tolower((unsigned char)*p) != + tolower((unsigned char)*q)) + break; + q++; + } + p++; + } + if (!*p) { + gg = gamelist[i]; + --argc; + ++argv; + break; + } + } + } +#else + gg = &thegame; +#endif + + fe = frontend_new(inst); + me = midend_for_new_game(fe, gg, argc > 0 ? argv[0] : NULL, + TRUE, TRUE, &error); + if (!me) { char buf[128]; - sprintf(buf, "%.100s Error", thegame.name); +#ifdef COMBINED + sprintf(buf, "Puzzles Error"); +#else + sprintf(buf, "%.100s Error", gg->name); +#endif MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR); + sfree(error); return 1; } + fe_set_midend(fe, me); + show_window(fe); while (GetMessage(&msg, NULL, 0, 0)) { DispatchMessage(&msg); } + DestroyWindow(fe->hwnd); cleanup_help(); return msg.wParam; } +/* vim: set shiftwidth=4 tabstop=8: */