+/*
+ * 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)
+{
+ RECT r, sr;
+
+ if (SystemParametersInfo(SPI_GETWORKAREA, 0, &sr, FALSE)) {
+ *x = sr.right - sr.left;
+ *y = sr.bottom - sr.top;
+ r.left = 100;
+ r.right = 200;
+ r.top = 100;
+ r.bottom = 200;
+ AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
+ *x -= r.right - r.left - 100;
+ *y -= r.bottom - r.top - 100;
+ } else {
+ *x = *y = INT_MAX;
+ }
+
+ if (fe->statusbar != NULL) {
+ GetWindowRect(fe->statusbar, &sr);
+ *y -= sr.bottom - sr.top;
+ }
+}
+
+#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;
+ const char *nogame = "Puzzles (no game selected)";
+
+ fe = snew(frontend);
+
+ fe->inst = inst;
+
+ fe->game = NULL;
+ fe->me = NULL;
+
+ fe->timer = 0;
+ fe->hwnd = NULL;
+
+ fe->help_running = FALSE;
+
+ fe->drawstatus = NOTHING;
+ fe->dr = NULL;
+ fe->fontstart = 0;
+
+ 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;
+}
+
+/*
+ * Populate a frontend structure with a (new) game and midend structure, and
+ * create any window furniture that it needs.
+ *
+ * Previously-allocated memory and window furniture will be freed by this function.
+ */
+static int new_game(frontend *fe, const game *game, char *game_id, char **error)
+{
+ int x, y;
+ RECT r;
+
+ fe->game = game;
+
+ if (fe->me) midend_free(fe->me);
+ fe->me = midend_new(fe, fe->game, &win_drawing, fe);
+
+ if (game_id) {
+ *error = midend_game_id(fe->me, game_id);
+ if (*error) {
+ midend_free(fe->me);
+ sfree(fe);
+ return -1;
+ }
+ }
+
+ midend_new_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);
+
+ for (i = 0; i < ncolours; i++) {
+ fe->colours[i] = RGB(255 * colours[i*3+0],
+ 255 * colours[i*3+1],
+ 255 * colours[i*3+2]);
+ fe->brushes[i] = CreateSolidBrush(fe->colours[i]);
+ fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]);
+ }
+ sfree(colours);
+ }
+
+ if (fe->statusbar)
+ DestroyWindow(fe->statusbar);
+ if (midend_wants_statusbar(fe->me)) {
+ fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, TEXT("ooh"),
+ WS_CHILD | WS_VISIBLE,
+ 0, 0, 0, 0, /* status bar does these */
+ NULL, NULL, fe->inst, NULL);
+ } else
+ fe->statusbar = NULL;
+
+ get_max_puzzle_size(fe, &x, &y);
+ midend_size(fe->me, &x, &y, FALSE);
+
+ r.left = r.top = 0;
+ r.right = x;
+ r.bottom = y;
+ AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
+
+#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, 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");
+#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 ||
+ fe->game->can_configure) {
+ int i;
+#ifndef _WIN32_WCE
+ HMENU sub = CreateMenu();
+
+ 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]);
+
+ /*
+ * FIXME: we ought to go through and do something
+ * 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 (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);
+#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 (fe->game->can_print) {
+ AppendMenu(menu, MF_ENABLED, IDM_PRINT, TEXT("&Print..."));
+ AppendMenu(menu, MF_SEPARATOR, 0, 0);
+ }
+#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, TEXT("&Copy"));
+ }
+#endif
+ if (fe->game->can_solve) {
+ AppendMenu(menu, MF_SEPARATOR, 0, 0);
+ AppendMenu(menu, MF_ENABLED, IDM_SOLVE, TEXT("Sol&ve"));
+ }
+ AppendMenu(menu, MF_SEPARATOR, 0, 0);
+#ifndef _WIN32_WCE
+ AppendMenu(menu, MF_ENABLED, IDM_QUIT, TEXT("E&xit"));
+ menu = CreateMenu();
+ 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) {
+ 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, fe->puzz_scale); /* initialises fe->bitmap */
+
+ return 0;
+}
+
+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);
+}
+
+#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)
+{
+ frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
+
+ switch (msg) {
+ case WM_INITDIALOG:
+#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 (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;
+ }
+
+ return 0;
+}
+
+/*
+ * Wrappers on midend_{get,set}_config, which extend the CFG_*
+ * enumeration to add CFG_PRINT.
+ */
+static config_item *frontend_get_config(frontend *fe, int which,
+ char **wintitle)
+{
+ if (which < CFG_FRONTEND_SPECIFIC) {
+ return midend_get_config(fe->me, which, wintitle);
+ } else if (which == CFG_PRINT) {
+ config_item *ret;
+ int i;
+
+ *wintitle = snewn(40 + strlen(fe->game->name), char);
+ sprintf(*wintitle, "%s print setup", fe->game->name);
+
+ ret = snewn(8, config_item);
+
+ i = 0;
+
+ ret[i].name = "Number of puzzles to print";
+ ret[i].type = C_STRING;
+ ret[i].sval = dupstr("1");
+ ret[i].ival = 0;
+ i++;
+
+ ret[i].name = "Number of puzzles across the page";
+ ret[i].type = C_STRING;
+ ret[i].sval = dupstr("1");
+ ret[i].ival = 0;
+ i++;
+
+ ret[i].name = "Number of puzzles down the page";
+ ret[i].type = C_STRING;
+ ret[i].sval = dupstr("1");
+ ret[i].ival = 0;
+ i++;
+
+ ret[i].name = "Percentage of standard size";
+ ret[i].type = C_STRING;
+ ret[i].sval = dupstr("100.0");
+ ret[i].ival = 0;
+ i++;
+
+ ret[i].name = "Include currently shown puzzle";
+ ret[i].type = C_BOOLEAN;
+ ret[i].sval = NULL;
+ ret[i].ival = TRUE;
+ i++;
+
+ ret[i].name = "Print solutions";
+ ret[i].type = C_BOOLEAN;
+ ret[i].sval = NULL;
+ ret[i].ival = 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;
+ i++;
+ }
+
+ ret[i].name = NULL;
+ ret[i].type = C_END;
+ ret[i].sval = NULL;
+ ret[i].ival = 0;
+ i++;
+
+ return ret;
+ } else {
+ assert(!"We should never get here");
+ return NULL;
+ }
+}
+
+static 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)
+ return "Number of puzzles to print should be at least one";
+ if ((fe->printw = atoi(cfg[1].sval)) <= 0)
+ return "Number of puzzles across the page should be at least one";
+ if ((fe->printh = atoi(cfg[2].sval)) <= 0)
+ return "Number of puzzles down the page should be at least one";
+ if ((fe->printscale = (float)atof(cfg[3].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;
+ return NULL;
+ } else {
+ assert(!"We should never get here");
+ return "Internal error";
+ }
+}
+
+#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)
+{
+ frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
+ config_item *i;
+ struct cfg_aux *j;
+
+ switch (msg) {
+ case WM_INITDIALOG:
+#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 ((LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) {
+ if (LOWORD(wParam) == IDOK) {
+ char *err = frontend_set_config(fe, fe->cfg_which, fe->cfg);
+
+ if (err) {
+ 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;
+ }
+
+ /*
+ * First find the control whose id this is.
+ */
+ for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
+ if (j->ctlid == LOWORD(wParam))
+ break;
+ }
+ if (i->type == C_END)
+ return 0; /* not our problem */
+
+ 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_DBLCLK)) {
+ i->ival = 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);
+ }
+
+ return 0;
+
+ case WM_CLOSE:
+ fe->dlg_done = 1;
+ return 0;
+ }
+
+ 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)
+{
+ HWND ret;
+ ret = CreateWindowEx(exstyle, wclass, wtext,
+ wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1,
+ fe->cfgbox, (HMENU) wid, fe->inst, NULL);
+ 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;
+ TEXTMETRIC tm;
+ HDC hdc;
+ HFONT oldfont;
+ SIZE size;
+ int gm, id;
+ int winwidth, winheight, y;
+ int height, width, maxwid;
+ const char *strings[16];
+ int lengths[16];
+ int nstrings = 0;
+ char titlebuf[512];
+
+ sprintf(titlebuf, "About %.250s", fe->game->name);
+
+ strings[nstrings++] = fe->game->name;
+ strings[nstrings++] = "from Simon Tatham's Portable Puzzle Collection";
+ strings[nstrings++] = ver;
+
+ wc.style = CS_DBLCLKS | CS_SAVEBITS;
+ wc.lpfnWndProc = DefDlgProc;
+ wc.cbClsExtra = 0;
+ wc.cbWndExtra = DLGWINDOWEXTRA + 8;
+ wc.hInstance = fe->inst;
+ wc.hIcon = NULL;
+ wc.hCursor = LoadCursor(NULL, IDC_ARROW);
+ wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
+ wc.lpszMenuName = NULL;
+ wc.lpszClassName = "GameAboutBox";
+ RegisterClass(&wc);
+
+ hdc = GetDC(fe->hwnd);
+ SetMapMode(hdc, MM_TEXT);
+
+ fe->dlg_done = FALSE;
+
+ fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
+ 0, 0, 0, 0,
+ FALSE, FALSE, FALSE, DEFAULT_CHARSET,
+ OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
+ DEFAULT_QUALITY,
+ FF_SWISS,
+ "MS Shell Dlg");
+
+ oldfont = SelectObject(hdc, fe->cfgfont);
+ if (GetTextMetrics(hdc, &tm)) {
+ height = tm.tmAscent + tm.tmDescent;
+ width = tm.tmAveCharWidth;
+ } else {
+ height = width = 30;
+ }
+
+ /*
+ * Figure out the layout of the About box by measuring the
+ * length of each piece of text.
+ */
+ maxwid = 0;
+ winheight = height/2;
+
+ for (i = 0; i < nstrings; i++) {
+ if (GetTextExtentPoint32(hdc, strings[i], strlen(strings[i]), &size))
+ lengths[i] = size.cx;
+ else
+ lengths[i] = 0; /* *shrug* */
+ if (maxwid < lengths[i])
+ maxwid = lengths[i];
+ winheight += height * 3 / 2 + (height / 2);
+ }
+
+ winheight += height + height * 7 / 4; /* OK button */
+ winwidth = maxwid + 4*width;
+
+ SelectObject(hdc, oldfont);
+ ReleaseDC(fe->hwnd, hdc);
+
+ /*
+ * Create the dialog, now that we know its size.
+ */
+ {
+ RECT r, r2;
+
+ r.left = r.top = 0;
+ r.right = winwidth;
+ r.bottom = winheight;