X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=windows.c;h=0a43c5d2785c93caa23cc6808251d34d446f049e;hb=3276376d1be74b66970b88c3e941dcedf8d22474;hp=6249d55f209abffdc22ecf352ab791d292b9de45;hpb=407f29c46f35910ce3d7ddd41f13e94213c2597e;p=sgt-puzzles.git diff --git a/windows.c b/windows.c index 6249d55..0a43c5d 100644 --- a/windows.c +++ b/windows.c @@ -150,15 +150,21 @@ void dputs(char *buf) OutputDebugString(buf); } -void debug_printf(char *fmt, ...) +void debug_printf(const char *fmt, ...) { char buf[4096]; va_list ap; + static int debugging = -1; - va_start(ap, fmt); - _vsnprintf(buf, 4095, 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 @@ -189,6 +195,11 @@ struct blitter { enum { CFG_PRINT = CFG_FRONTEND_SPECIFIC }; +struct preset_menuitemref { + HMENU which_menu; + int item_index; +}; + struct frontend { const game *game; midend *me; @@ -207,8 +218,9 @@ struct frontend { HMENU gamemenu, typemenu; UINT timer; DWORD timer_last_tickcount; - int npresets; - game_params **presets; + struct preset_menu *preset_menu; + struct preset_menuitemref *preset_menuitems; + int n_preset_menuitems; struct font *fonts; int nfonts, fontsize; config_item *cfg; @@ -238,7 +250,7 @@ void frontend_free(frontend *fe) sfree(fe->colours); sfree(fe->brushes); sfree(fe->pens); - sfree(fe->presets); + sfree(fe->fonts); sfree(fe); } @@ -246,7 +258,7 @@ void frontend_free(frontend *fe) static void update_type_menu_tick(frontend *fe); static void update_copy_menu_greying(frontend *fe); -void fatal(char *fmt, ...) +void fatal(const char *fmt, ...) { char buf[2048]; va_list ap; @@ -292,7 +304,7 @@ void get_random_seed(void **randseed, int *randseedsize) *randseedsize = sizeof(SYSTEMTIME); } -static void win_status_bar(void *handle, char *text) +static void win_status_bar(void *handle, const char *text) { #ifdef _WIN32_WCE TCHAR wText[255]; @@ -544,7 +556,8 @@ static void win_unclip(void *handle) } static void win_draw_text(void *handle, int x, int y, int fonttype, - int fontsize, int align, int colour, char *text) + int fontsize, int align, int colour, + const char *text) { frontend *fe = (frontend *)handle; POINT xy; @@ -601,10 +614,8 @@ static void win_draw_text(void *handle, int x, int y, int fonttype, HFONT oldfont; TEXTMETRIC tm; SIZE size; -#ifdef _WIN32_WCE - TCHAR wText[256]; - MultiByteToWideChar (CP_ACP, 0, text, -1, wText, 256); -#endif + WCHAR wText[256]; + MultiByteToWideChar (CP_UTF8, 0, text, -1, wText, 256); oldfont = SelectObject(fe->hdc, fe->fonts[i].font); if (GetTextMetrics(fe->hdc, &tm)) { @@ -613,11 +624,7 @@ static void win_draw_text(void *handle, int x, int y, int fonttype, else xy.y -= tm.tmAscent; } -#ifndef _WIN32_WCE - if (GetTextExtentPoint32(fe->hdc, text, strlen(text), &size)) -#else - if (GetTextExtentPoint32(fe->hdc, wText, wcslen(wText), &size)) -#endif + if (GetTextExtentPoint32W(fe->hdc, wText, wcslen(wText), &size)) { if (align & ALIGN_HCENTRE) xy.x -= size.cx / 2; @@ -626,11 +633,7 @@ static void win_draw_text(void *handle, int x, int y, int fonttype, } SetBkMode(fe->hdc, TRANSPARENT); win_text_colour(fe, colour); -#ifndef _WIN32_WCE - TextOut(fe->hdc, xy.x, xy.y, text, strlen(text)); -#else - ExtTextOut(fe->hdc, xy.x, xy.y, 0, NULL, wText, wcslen(wText), NULL); -#endif + ExtTextOutW(fe->hdc, xy.x, xy.y, 0, NULL, wText, wcslen(wText), NULL); SelectObject(fe->hdc, oldfont); } } @@ -956,6 +959,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, @@ -980,6 +992,7 @@ const struct drawing_api win_drawing = { win_end_doc, win_line_width, win_line_dotted, + win_text_fallback, }; void print(frontend *fe) @@ -990,7 +1003,7 @@ void print(frontend *fe) document *doc; midend *nme = NULL; /* non-interactive midend for bulk puzzle generation */ int i; - char *err = NULL; + const char *err = NULL; /* * Create our document structure and fill it up with puzzles. @@ -1523,47 +1536,190 @@ static frontend *frontend_new(HINSTANCE inst) NULL, NULL, inst, NULL); if (!fe->hwnd) { DWORD lerr = GetLastError(); - printf("no window: 0x%x\n", lerr); + printf("no window: 0x%x\n", (unsigned)lerr); } #endif fe->gamemenu = NULL; - fe->presets = NULL; + fe->preset_menu = NULL; fe->statusbar = NULL; fe->bitmap = NULL; - SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe); + SetWindowLongPtr(fe->hwnd, GWLP_USERDATA, (LONG_PTR)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); +} + /* - * Populate a frontend structure with a (new) game and midend structure, and - * create any window furniture that it needs. + * Create an appropriate midend structure to go in a puzzle window, + * given a game type and/or a command-line argument. * - * Previously-allocated memory and window furniture will be freed by this function. + * '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 int new_game(frontend *fe, const game *game, char *game_id, char **error) +static midend *midend_for_new_game(frontend *fe, const game *cgame, + char *arg, int maybe_game_id, + int maybe_save_file, char **error) { - int x, y; - RECT r; + midend *me = NULL; - fe->game = game; + if (!arg) { + if (me) midend_free(me); + me = midend_new(fe, cgame, &win_drawing, fe); + midend_new_game(me); + } else { + FILE *fp; + const char *err_param, *err_load; - if (fe->me) midend_free(fe->me); - fe->me = midend_new(fe, fe->game, &win_drawing, fe); + /* + * 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; - if (game_id) { - *error = midend_game_id(fe->me, game_id); - if (*error) { - midend_free(fe->me); +#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 -1; + return NULL; } } - midend_new_game(fe->me); + return me; +} + +static void populate_preset_menu(frontend *fe, + struct preset_menu *menu, HMENU winmenu) +{ + int i; + for (i = 0; i < menu->n_entries; i++) { + struct preset_menu_entry *entry = &menu->entries[i]; + UINT_PTR id_or_sub; + UINT flags = MF_ENABLED; + + if (entry->params) { + id_or_sub = (UINT_PTR)(IDM_PRESETS + 0x10 * entry->id); + + fe->preset_menuitems[entry->id].which_menu = winmenu; + fe->preset_menuitems[entry->id].item_index = + GetMenuItemCount(winmenu); + } else { + HMENU winsubmenu = CreateMenu(); + id_or_sub = (UINT_PTR)winsubmenu; + flags |= MF_POPUP; + + populate_preset_menu(fe, entry->submenu, winsubmenu); + } + + /* + * FIXME: we ought to go through and do something with ampersands + * here. + */ + +#ifndef _WIN32_WCE + AppendMenu(winmenu, flags, id_or_sub, entry->title); +#else + { + TCHAR wName[255]; + MultiByteToWideChar(CP_ACP, 0, entry->title, -1, wName, 255); + AppendMenu(winmenu, flags, id_or_sub, wName); + } +#endif + } +} + +/* + * 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; @@ -1592,7 +1748,8 @@ static int new_game(frontend *fe, const game *game, char *game_id, char **error) if (fe->statusbar) DestroyWindow(fe->statusbar); if (midend_wants_statusbar(fe->me)) { - fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, TEXT("ooh"), + fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, + TEXT(DEFAULT_STATUSBAR_TEXT), WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, /* status bar does these */ NULL, NULL, fe->inst, NULL); @@ -1688,11 +1845,16 @@ static int new_game(frontend *fe, const game *game, char *game_id, char **error) 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 || - fe->game->can_configure) { - int i; + if (!fe->preset_menu) { + int i; + fe->preset_menu = midend_get_presets( + fe->me, &fe->n_preset_menuitems); + fe->preset_menuitems = snewn(fe->n_preset_menuitems, + struct preset_menuitemref); + for (i = 0; i < fe->n_preset_menuitems; i++) + fe->preset_menuitems[i].which_menu = NULL; + } + if (fe->preset_menu->n_entries > 0 || fe->game->can_configure) { #ifndef _WIN32_WCE HMENU sub = CreateMenu(); @@ -1701,28 +1863,9 @@ static int new_game(frontend *fe, const game *game, char *game_id, char **error) 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]); - /* - * FIXME: we ought to go through and do something - * with ampersands here. - */ + populate_preset_menu(fe, fe->preset_menu, sub); -#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 (fe->game->can_configure) { AppendMenu(sub, MF_ENABLED, IDM_CONFIG, TEXT("&Custom...")); } @@ -1730,7 +1873,6 @@ static int new_game(frontend *fe, const game *game, char *game_id, char **error) fe->typemenu = sub; } else { fe->typemenu = INVALID_HANDLE_VALUE; - fe->presets = NULL; } #ifdef COMBINED @@ -1851,7 +1993,7 @@ static void make_dialog_full_screen(HWND hwnd) static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { - frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA); + frontend *fe = (frontend *)GetWindowLongPtr(hwnd, GWLP_USERDATA); switch (msg) { case WM_INITDIALOG: @@ -1916,52 +2058,43 @@ static config_item *frontend_get_config(frontend *fe, int which, ret[i].name = "Number of puzzles to print"; ret[i].type = C_STRING; - ret[i].sval = dupstr("1"); - ret[i].ival = 0; + ret[i].u.string.sval = dupstr("1"); i++; ret[i].name = "Number of puzzles across the page"; ret[i].type = C_STRING; - ret[i].sval = dupstr("1"); - ret[i].ival = 0; + ret[i].u.string.sval = dupstr("1"); i++; ret[i].name = "Number of puzzles down the page"; ret[i].type = C_STRING; - ret[i].sval = dupstr("1"); - ret[i].ival = 0; + ret[i].u.string.sval = dupstr("1"); i++; ret[i].name = "Percentage of standard size"; ret[i].type = C_STRING; - ret[i].sval = dupstr("100.0"); - ret[i].ival = 0; + ret[i].u.string.sval = dupstr("100.0"); i++; ret[i].name = "Include currently shown puzzle"; ret[i].type = C_BOOLEAN; - ret[i].sval = NULL; - ret[i].ival = TRUE; + ret[i].u.boolean.bval = TRUE; i++; ret[i].name = "Print solutions"; ret[i].type = C_BOOLEAN; - ret[i].sval = NULL; - ret[i].ival = FALSE; + ret[i].u.boolean.bval = FALSE; i++; if (fe->game->can_print_in_colour) { ret[i].name = "Print in colour"; ret[i].type = C_BOOLEAN; - ret[i].sval = NULL; - ret[i].ival = FALSE; + ret[i].u.boolean.bval = FALSE; i++; } ret[i].name = NULL; ret[i].type = C_END; - ret[i].sval = NULL; - ret[i].ival = 0; i++; return ret; @@ -1971,22 +2104,24 @@ static config_item *frontend_get_config(frontend *fe, int which, } } -static char *frontend_set_config(frontend *fe, int which, config_item *cfg) +static const char *frontend_set_config( + frontend *fe, int which, config_item *cfg) { if (which < CFG_FRONTEND_SPECIFIC) { return midend_set_config(fe->me, which, cfg); } else if (which == CFG_PRINT) { - if ((fe->printcount = atoi(cfg[0].sval)) <= 0) + if ((fe->printcount = atoi(cfg[0].u.string.sval)) <= 0) return "Number of puzzles to print should be at least one"; - if ((fe->printw = atoi(cfg[1].sval)) <= 0) + if ((fe->printw = atoi(cfg[1].u.string.sval)) <= 0) return "Number of puzzles across the page should be at least one"; - if ((fe->printh = atoi(cfg[2].sval)) <= 0) + if ((fe->printh = atoi(cfg[2].u.string.sval)) <= 0) return "Number of puzzles down the page should be at least one"; - if ((fe->printscale = (float)atof(cfg[3].sval)) <= 0) + if ((fe->printscale = (float)atof(cfg[3].u.string.sval)) <= 0) return "Print size should be positive"; - fe->printcurr = cfg[4].ival; - fe->printsolns = cfg[5].ival; - fe->printcolour = fe->game->can_print_in_colour && cfg[6].ival; + fe->printcurr = cfg[4].u.boolean.bval; + fe->printsolns = cfg[5].u.boolean.bval; + fe->printcolour = fe->game->can_print_in_colour && + cfg[6].u.boolean.bval; return NULL; } else { assert(!"We should never get here"); @@ -1999,7 +2134,7 @@ static char *frontend_set_config(frontend *fe, int which, config_item *cfg) /* 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) + int exstyle, const char *wtext, INT_PTR wid) { RECT rc; TCHAR wwtext[256]; @@ -2050,7 +2185,7 @@ static void create_config_controls(frontend * fe) 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); + SetDlgItemTextA(fe->cfgbox, j->ctlid, i->u.string.sval); break; case C_BOOLEAN: @@ -2060,7 +2195,7 @@ static void create_config_controls(frontend * fe) 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)); + CheckDlgButton(fe->cfgbox, j->ctlid, (i->u.boolean.bval != 0)); break; case C_CHOICES: @@ -2074,9 +2209,11 @@ static void create_config_controls(frontend * fe) CBS_DROPDOWNLIST | CBS_HASSTRINGS, 0, "", (j->ctlid = id++)); { - char c, *p, *q, *str; + char c; + const char *p, *q; + char *str; - p = i->sval; + p = i->u.choices.choicenames; c = *p++; while (*p) { q = p; @@ -2095,7 +2232,7 @@ static void create_config_controls(frontend * fe) p = q; } } - SendMessage(ctl, CB_SETCURSEL, i->ival, 0); + SendMessage(ctl, CB_SETCURSEL, i->u.choices.selected, 0); break; } @@ -2108,7 +2245,7 @@ static void create_config_controls(frontend * fe) static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { - frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA); + frontend *fe = (frontend *)GetWindowLongPtr(hwnd, GWLP_USERDATA); config_item *i; struct cfg_aux *j; @@ -2119,7 +2256,7 @@ static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg, char *title; fe = (frontend *) lParam; - SetWindowLong(hwnd, GWL_USERDATA, lParam); + SetWindowLongPtr(hwnd, GWLP_USERDATA, lParam); fe->cfgbox = hwnd; fe->cfg = frontend_get_config(fe, fe->cfg_which, &title); @@ -2141,7 +2278,8 @@ static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg, */ if ((LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) { if (LOWORD(wParam) == IDOK) { - char *err = frontend_set_config(fe, fe->cfg_which, fe->cfg); + const char *err = frontend_set_config( + fe, fe->cfg_which, fe->cfg); if (err) { MessageBox(hwnd, err, "Validation error", @@ -2183,16 +2321,16 @@ static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg, GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer)); #endif buffer[lenof(buffer)-1] = '\0'; - sfree(i->sval); - i->sval = dupstr(buffer); + sfree(i->u.string.sval); + i->u.string.sval = dupstr(buffer); } else if (i->type == C_BOOLEAN && (HIWORD(wParam) == BN_CLICKED || HIWORD(wParam) == BN_DBLCLK)) { - i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid); + i->u.boolean.bval = IsDlgButtonChecked(fe->cfgbox, j->ctlid); } else if (i->type == C_CHOICES && HIWORD(wParam) == CBN_SELCHANGE) { - i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid, - CB_GETCURSEL, 0, 0); + i->u.choices.selected = SendDlgItemMessage(fe->cfgbox, j->ctlid, + CB_GETCURSEL, 0, 0); } return 0; @@ -2208,7 +2346,7 @@ static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg, #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) + int exstyle, const char *wtext, INT_PTR wid) { HWND ret; ret = CreateWindowEx(exstyle, wclass, wtext, @@ -2338,8 +2476,8 @@ static void about(frontend *fe) SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE); - SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe); - SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)AboutDlgProc); + SetWindowLongPtr(fe->cfgbox, GWLP_USERDATA, (LONG_PTR)fe); + SetWindowLongPtr(fe->cfgbox, DWLP_DLGPROC, (LONG_PTR)AboutDlgProc); id = 1000; y = height/2; @@ -2519,8 +2657,8 @@ static int get_config(frontend *fe, int which) SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE); - SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe); - SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc); + SetWindowLongPtr(fe->cfgbox, GWLP_USERDATA, (LONG_PTR)fe); + SetWindowLongPtr(fe->cfgbox, DWLP_DLGPROC, (LONG_PTR)ConfigDlgProc); /* * Count the controls so we can allocate cfgaux. @@ -2542,7 +2680,7 @@ static int get_config(frontend *fe, int which) ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2, "EDIT", WS_TABSTOP | ES_AUTOHSCROLL, WS_EX_CLIENTEDGE, "", (j->ctlid = id++)); - SetWindowText(ctl, i->sval); + SetWindowText(ctl, i->u.string.sval); y += height*3/2; break; @@ -2553,7 +2691,7 @@ static int get_config(frontend *fe, int which) mkctrl(fe, col1l, col2r, y, y+height, "BUTTON", BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP, 0, i->name, (j->ctlid = id++)); - CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0)); + CheckDlgButton(fe->cfgbox, j->ctlid, (i->u.boolean.bval != 0)); y += height; break; @@ -2568,10 +2706,12 @@ static int get_config(frontend *fe, int which) CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", (j->ctlid = id++)); { - char c, *p, *q, *str; + char c; + const char *p, *q; + char *str; SendMessage(ctl, CB_RESETCONTENT, 0, 0); - p = i->sval; + p = i->u.choices.choicenames; c = *p++; while (*p) { q = p; @@ -2586,7 +2726,7 @@ static int get_config(frontend *fe, int which) } } - SendMessage(ctl, CB_SETCURSEL, i->ival, 0); + SendMessage(ctl, CB_SETCURSEL, i->u.choices.selected, 0); y += height*3/2; break; @@ -2782,14 +2922,22 @@ static void update_type_menu_tick(frontend *fe) 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); + for (i = 0; i < fe->n_preset_menuitems; i++) { + if (fe->preset_menuitems[i].which_menu) { + int flag = (i == n ? MF_CHECKED : MF_UNCHECKED); + CheckMenuItem(fe->preset_menuitems[i].which_menu, + fe->preset_menuitems[i].item_index, + MF_BYPOSITION | flag); + } + } + + if (fe->game->can_configure) { + int flag = (n < 0 ? MF_CHECKED : MF_UNCHECKED); + /* "Custom" menu item is at the bottom of the top-level Type menu */ + total = GetMenuItemCount(fe->typemenu); + CheckMenuItem(fe->typemenu, total - 1, MF_BYPOSITION | flag); } DrawMenuBar(fe->hwnd); @@ -2823,25 +2971,10 @@ 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) { - frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA); + frontend *fe = (frontend *)GetWindowLongPtr(hwnd, GWLP_USERDATA); int cmd; switch (message) { @@ -2859,18 +2992,18 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, cmd = wParam & ~0xF; /* low 4 bits reserved to Windows */ switch (cmd) { case IDM_NEW: - if (!midend_process_key(fe->me, 0, 0, 'n')) + if (!midend_process_key(fe->me, 0, 0, UI_NEWGAME)) PostQuitMessage(0); break; case IDM_RESTART: midend_restart_game(fe->me); break; case IDM_UNDO: - if (!midend_process_key(fe->me, 0, 0, 'u')) + if (!midend_process_key(fe->me, 0, 0, UI_UNDO)) PostQuitMessage(0); break; case IDM_REDO: - if (!midend_process_key(fe->me, 0, 0, '\x12')) + if (!midend_process_key(fe->me, 0, 0, UI_REDO)) PostQuitMessage(0); break; case IDM_COPY: @@ -2885,14 +3018,14 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, break; case IDM_SOLVE: { - char *msg = midend_solve(fe->me); + const char *msg = midend_solve(fe->me); if (msg) MessageBox(hwnd, msg, "Unable to solve", MB_ICONERROR | MB_OK); } break; case IDM_QUIT: - if (!midend_process_key(fe->me, 0, 0, 'q')) + if (!midend_process_key(fe->me, 0, 0, UI_QUIT)) PostQuitMessage(0); break; case IDM_CONFIG: @@ -2977,7 +3110,12 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, fclose(fp); } else { FILE *fp = fopen(filename, "r"); - char *err; + const char *err = NULL; + char *err_w = NULL; + midend *me = fe->me; +#ifdef COMBINED + char *id_name; +#endif if (!fp) { MessageBox(hwnd, "Unable to open saved game file", @@ -2985,15 +3123,42 @@ 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_w); + err = err_w; + rewind(fp); /* for the actual load */ + } + sfree(id_name); + } +#endif + if (!err) + err = midend_deserialise(me, savefile_read, fp); fclose(fp); if (err) { MessageBox(hwnd, err, "Error", MB_ICONERROR|MB_OK); + sfree(err_w); break; } + if (fe->me != me) + fe_set_midend(fe, me); new_game_size(fe, 1.0); } } @@ -3014,16 +3179,19 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, #ifdef COMBINED if (wParam >= IDM_GAMES && wParam < (IDM_GAMES + (WPARAM)gamecount)) { int p = wParam - IDM_GAMES; - char *error; - - new_game(fe, gamelist[p], NULL, &error); + 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; + game_params *preset = preset_menu_lookup_by_id( + fe->preset_menu, + ((wParam &~ 0xF) - IDM_PRESETS) / 0x10); - if (p >= 0 && p < fe->npresets) { - midend_set_params(fe->me, fe->presets[p]); + if (preset) { + midend_set_params(fe->me, preset); new_game_type(fe); } } @@ -3239,8 +3407,18 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, } break; case WM_CHAR: - if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam)) - PostQuitMessage(0); + { + int key = (unsigned char)wParam; + if (key == '\x1A') { + BYTE keystate[256]; + if (GetKeyboardState(keystate) && + (keystate[VK_SHIFT] & 0x80) && + (keystate[VK_CONTROL] & 0x80)) + key = UI_REDO; + } + if (!midend_process_key(fe->me, 0, 0, key)) + PostQuitMessage(0); + } return 0; case WM_TIMER: if (fe->timer) { @@ -3304,12 +3482,237 @@ static int FindPreviousInstance() } #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; + char *error = NULL; 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); @@ -3351,13 +3754,11 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) #ifdef COMBINED gg = gamelist[0]; - { + if (argc > 0) { int i; for (i = 0; i < gamecount; i++) { const char *p = gamelist[i]->name; - char *q = cmdline; - while (*q && isspace((unsigned char)*q)) - q++; + char *q = argv[0]; while (*p && *q) { if (isspace((unsigned char)*p)) { while (*q && isspace((unsigned char)*q)) @@ -3372,11 +3773,9 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) } if (!*p) { gg = gamelist[i]; - cmdline = q; - if (*cmdline) { - while (*cmdline && isspace((unsigned char)*cmdline)) - cmdline++; - } + --argc; + ++argv; + break; } } } @@ -3385,18 +3784,27 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) #endif fe = frontend_new(inst); - if (new_game(fe, gg, *cmdline ? cmdline : NULL, &error)) { + me = midend_for_new_game(fe, gg, argc > 0 ? argv[0] : NULL, + TRUE, TRUE, &error); + if (!me) { char buf[128]; +#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;