+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_last_tickcount = GetTickCount();
+ }
+}
+
+void write_clip(HWND hwnd, char *data)
+{
+ HGLOBAL clipdata;
+ int len, i, j;
+ char *data2;
+ void *lock;
+
+ /*
+ * Windows expects CRLF in the clipboard, so we must convert
+ * any \n that has come out of the puzzle backend.
+ */
+ len = 0;
+ for (i = 0; data[i]; i++) {
+ if (data[i] == '\n')
+ len++;
+ len++;
+ }
+ data2 = snewn(len+1, char);
+ j = 0;
+ for (i = 0; data[i]; i++) {
+ if (data[i] == '\n')
+ data2[j++] = '\r';
+ data2[j++] = data[i];
+ }
+ assert(j == len);
+ data2[j] = '\0';
+
+ clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
+ if (!clipdata)
+ return;
+ lock = GlobalLock(clipdata);
+ if (!lock)
+ return;
+ memcpy(lock, data2, len);
+ ((unsigned char *) lock)[len] = 0;
+ GlobalUnlock(clipdata);
+
+ if (OpenClipboard(hwnd)) {
+ EmptyClipboard();
+ SetClipboardData(CF_TEXT, clipdata);
+ CloseClipboard();
+ } else
+ GlobalFree(clipdata);
+
+ sfree(data2);
+}
+
+/*
+ * Set up Help and see if we can find a help file.
+ */
+static void init_help(void)
+{
+ char b[2048], *p, *q, *r;
+ FILE *fp;
+
+ /*
+ * Find the executable file path, so we can look alongside
+ * it for help files. Trim the filename off the end.
+ */
+ GetModuleFileName(NULL, b, sizeof(b) - 1);
+ r = b;
+ p = strrchr(b, '\\');
+ if (p && p >= r) r = p+1;
+ q = strrchr(b, ':');
+ if (q && q >= r) r = q+1;
+
+#ifndef NO_HTMLHELP
+ /*
+ * Try HTML Help first.
+ */
+ strcpy(r, CHM_FILE_NAME);
+ if ( (fp = fopen(b, "r")) != NULL) {
+ fclose(fp);
+
+ /*
+ * We have a .CHM. See if we can use it.
+ */
+ hh_dll = LoadLibrary("hhctrl.ocx");
+ if (hh_dll) {
+ htmlhelp = (htmlhelp_t)GetProcAddress(hh_dll, "HtmlHelpA");
+ if (!htmlhelp)
+ 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.
+ */
+ strcpy(r, HELP_FILE_NAME);
+ if ( (fp = fopen(b, "r")) != NULL) {
+ fclose(fp);
+
+ help_path = dupstr(b);
+ help_type = HLP;
+
+ help_topic = thegame.winhelp_topic;
+
+ /*
+ * See if there's a .CNT file alongside it.
+ */
+ strcpy(r, HELP_CNT_NAME);
+ if ( (fp = fopen(b, "r")) != NULL) {
+ fclose(fp);
+ help_has_contents = TRUE;
+ } else
+ help_has_contents = FALSE;
+
+ return;
+ }
+
+ help_type = NONE; /* didn't find any */
+}
+
+/*
+ * Start Help.
+ */
+static void start_help(frontend *fe, const char *topic)
+{
+ char *str = NULL;
+ int cmd;
+
+ switch (help_type) {
+ case HLP:
+ assert(help_path);
+ if (topic) {
+ str = snewn(10+strlen(topic), char);
+ sprintf(str, "JI(`',`%s')", topic);
+ cmd = HELP_COMMAND;
+ } else if (help_has_contents) {
+ cmd = HELP_FINDER;
+ } else {
+ cmd = HELP_CONTENTS;
+ }
+ WinHelp(fe->hwnd, help_path, cmd, (DWORD)str);
+ fe->help_running = TRUE;
+ break;
+ case CHM:
+#ifndef NO_HTMLHELP
+ assert(help_path);
+ assert(htmlhelp);
+ if (topic) {
+ str = snewn(20 + strlen(topic) + strlen(help_path), char);
+ sprintf(str, "%s::/%s.html>main", help_path, topic);
+ } else {
+ str = dupstr(help_path);
+ }
+ htmlhelp(fe->hwnd, str, HH_DISPLAY_TOPIC, 0);
+ fe->help_running = TRUE;
+ break;
+#endif /* NO_HTMLHELP */
+ case NONE:
+ assert(!"This shouldn't happen");
+ break;
+ }
+
+ sfree(str);
+}
+
+/*
+ * Stop Help on window cleanup.
+ */
+static void stop_help(frontend *fe)
+{
+ if (fe->help_running) {
+ switch (help_type) {
+ case HLP:
+ 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;
+ }
+ fe->help_running = FALSE;
+ }
+}
+
+/*
+ * Terminate Help on process exit.
+ */
+static void cleanup_help(void)
+{
+#ifndef NO_HTMLHELP
+ if (help_type == CHM) {
+ assert(htmlhelp);
+ htmlhelp(NULL, NULL, HH_UNINITIALIZE, html_help_cookie);
+ }
+#endif /* NO_HTMLHELP */
+}
+
+static void check_window_size(frontend *fe, int *px, int *py)
+{
+ RECT r;
+ int x, y, sy;
+
+ if (fe->statusbar) {
+ RECT sr;
+ GetWindowRect(fe->statusbar, &sr);
+ sy = sr.bottom - sr.top;
+ } else {
+ sy = 0;
+ }
+
+ /*
+ * See if we actually got the window size we wanted, and adjust
+ * the puzzle size if not.
+ */
+ GetClientRect(fe->hwnd, &r);
+ x = r.right - r.left;
+ y = r.bottom - r.top - sy;
+ midend_size(fe->me, &x, &y, FALSE);
+ if (x != r.right - r.left || y != r.bottom - r.top) {
+ /*
+ * Resize the window, now we know what size we _really_
+ * want it to be.
+ */
+ r.left = r.top = 0;
+ r.right = x;
+ r.bottom = y + sy;
+ AdjustWindowRectEx(&r, 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);
+ }
+
+ *px = x;
+ *py = y;
+}
+
+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;
+ }
+}
+
+static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
+{
+ frontend *fe;
+ int x, y;
+ RECT r;
+
+ 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->timer = 0;
+ fe->hwnd = NULL;
+
+ fe->help_running = FALSE;
+
+ fe->drawstatus = NOTHING;
+ fe->dr = NULL;
+ fe->fontstart = 0;
+
+ midend_new_game(fe->me);
+
+ fe->fonts = NULL;
+ fe->nfonts = fe->fontsize = 0;
+
+ {
+ int i, ncolours;
+ float *colours;
+
+ colours = midend_colours(fe->me, &ncolours);
+
+ 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 (midend_wants_statusbar(fe->me)) {
+ fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh",
+ WS_CHILD | WS_VISIBLE,
+ 0, 0, 0, 0, /* status bar does these */
+ NULL, NULL, 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);
+
+ 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,
+ 0, 0, 0, 0, /* status bar does these */
+ fe->hwnd, NULL, inst, NULL);
+ /*
+ * Now resize the window to take account of the status bar.
+ */
+ GetWindowRect(fe->statusbar, &sr);
+ GetWindowRect(fe->hwnd, &r);
+ SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left,
+ r.bottom - r.top + sr.bottom - sr.top,
+ SWP_NOMOVE | SWP_NOZORDER);
+ } else {
+ fe->statusbar = NULL;
+ }
+
+ {
+ HMENU bar = CreateMenu();
+ HMENU menu = CreateMenu();
+
+ 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...");
+
+ if ((fe->npresets = midend_num_presets(fe->me)) > 0 ||
+ thegame.can_configure) {
+ HMENU sub = CreateMenu();
+ int i;
+
+ AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "Type");
+
+ fe->presets = snewn(fe->npresets, game_params *);
+
+ for (i = 0; i < fe->npresets; i++) {
+ char *name;
+
+ midend_fetch_preset(fe->me, i, &name, &fe->presets[i]);
+
+ /*
+ * FIXME: we ought to go through and do something
+ * with ampersands here.
+ */
+
+ AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name);
+ }
+
+ if (thegame.can_configure) {
+ AppendMenu(sub, MF_ENABLED, IDM_CONFIG, "Custom...");
+ }
+ }
+
+ AppendMenu(menu, MF_SEPARATOR, 0, 0);
+ AppendMenu(menu, MF_ENABLED, IDM_LOAD, "Load");
+ AppendMenu(menu, MF_ENABLED, IDM_SAVE, "Save");
+ AppendMenu(menu, MF_SEPARATOR, 0, 0);
+ if (thegame.can_print) {
+ AppendMenu(menu, MF_ENABLED, IDM_PRINT, "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) {
+ AppendMenu(menu, MF_SEPARATOR, 0, 0);
+ AppendMenu(menu, MF_ENABLED, IDM_COPY, "Copy");
+ }
+ if (thegame.can_solve) {
+ AppendMenu(menu, MF_SEPARATOR, 0, 0);
+ AppendMenu(menu, MF_ENABLED, IDM_SOLVE, "Solve");
+ }
+ AppendMenu(menu, MF_SEPARATOR, 0, 0);
+ AppendMenu(menu, MF_ENABLED, IDM_QUIT, "Exit");
+ menu = CreateMenu();
+ AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Help");
+ AppendMenu(menu, MF_ENABLED, IDM_ABOUT, "About");
+ 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);
+ }
+ }
+ SetMenu(fe->hwnd, bar);
+ }
+
+ fe->bitmap = NULL;
+ new_game_size(fe); /* initialises fe->bitmap */
+ check_window_size(fe, &x, &y);
+
+ SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe);
+
+ ShowWindow(fe->hwnd, SW_NORMAL);
+ SetForegroundWindow(fe->hwnd);
+
+ midend_redraw(fe->me);
+
+ return fe;
+}
+
+static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg,
+ WPARAM wParam, LPARAM lParam)
+{
+ frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
+
+ switch (msg) {
+ case WM_INITDIALOG:
+ return 0;
+
+ case WM_COMMAND:
+ if ((HIWORD(wParam) == BN_CLICKED ||
+ HIWORD(wParam) == BN_DOUBLECLICKED) &&
+ LOWORD(wParam) == IDOK)
+ fe->dlg_done = 1;
+ return 0;
+
+ case WM_CLOSE:
+ fe->dlg_done = 1;
+ 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(thegame.name), char);
+ sprintf(*wintitle, "%s print setup", thegame.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 (thegame.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 = thegame.can_print_in_colour && cfg[6].ival;
+ return NULL;
+ } else {
+ assert(!"We should never get here");
+ return "Internal error";
+ }
+}
+
+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:
+ return 0;
+
+ 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) {
+ char *err = frontend_set_config(fe, fe->cfg_which, fe->cfg);
+
+ if (err) {
+ MessageBox(hwnd, err, "Validation error",
+ MB_ICONERROR | MB_OK);
+ } else {
+ fe->dlg_done = 2;
+ }
+ } else {
+ fe->dlg_done = 1;
+ }
+ 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];
+ GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer));
+ 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)) {
+ 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;
+}
+
+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;
+}
+
+static void about(frontend *fe)
+{
+ 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", thegame.name);
+
+ strings[nstrings++] = thegame.name;
+ strings[nstrings++] = "from Simon Tatham's Portable Puzzle Collection";
+ strings[nstrings++] = ver;
+
+ wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
+ 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;
+
+ AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
+ DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
+ WS_CAPTION | WS_SYSMENU*/) &~
+ (WS_MAXIMIZEBOX | WS_OVERLAPPED),
+ FALSE, 0);
+
+ /*
+ * Centre the dialog on its parent window.
+ */
+ r.right -= r.left;
+ r.bottom -= r.top;
+ GetWindowRect(fe->hwnd, &r2);
+ r.left = (r2.left + r2.right - r.right) / 2;
+ r.top = (r2.top + r2.bottom - r.bottom) / 2;
+ r.right += r.left;
+ r.bottom += r.top;
+
+ fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, titlebuf,
+ DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
+ WS_CAPTION | WS_SYSMENU,
+ r.left, r.top,
+ r.right-r.left, r.bottom-r.top,
+ fe->hwnd, NULL, fe->inst, NULL);
+ }
+
+ SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
+
+ SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
+ SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)AboutDlgProc);
+
+ id = 1000;
+ y = height/2;
+ for (i = 0; i < nstrings; i++) {
+ int border = width*2 + (maxwid - lengths[i]) / 2;
+ mkctrl(fe, border, border+lengths[i], y+height*1/8, y+height*9/8,
+ "Static", 0, 0, strings[i], id++);
+ y += height*3/2;
+
+ assert(y < winheight);
+ y += height/2;
+ }
+
+ 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,
+ "OK", IDOK);
+
+ SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
+
+ EnableWindow(fe->hwnd, FALSE);
+ ShowWindow(fe->cfgbox, SW_NORMAL);
+ while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
+ if (!IsDialogMessage(fe->cfgbox, &msg))
+ DispatchMessage(&msg);
+ if (fe->dlg_done)
+ break;
+ }
+ EnableWindow(fe->hwnd, TRUE);
+ SetForegroundWindow(fe->hwnd);
+ DestroyWindow(fe->cfgbox);
+ DeleteObject(fe->cfgfont);
+}
+
+static int get_config(frontend *fe, int which)
+{
+ config_item *i;
+ struct cfg_aux *j;
+ char *title;
+ WNDCLASS wc;
+ MSG msg;
+ TEXTMETRIC tm;
+ HDC hdc;
+ HFONT oldfont;
+ SIZE size;
+ HWND ctl;
+ int gm, id, nctrls;
+ int winwidth, winheight, col1l, col1r, col2l, col2r, y;
+ int height, width, maxlabel, maxcheckbox;
+
+ wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
+ 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 = "GameConfigBox";
+ 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;
+ }
+
+ fe->cfg = frontend_get_config(fe, which, &title);
+ fe->cfg_which = which;
+
+ /*
+ * Figure out the layout of the config box by measuring the
+ * length of each piece of text.
+ */
+ maxlabel = maxcheckbox = 0;
+ winheight = height/2;
+
+ for (i = fe->cfg; i->type != C_END; i++) {
+ switch (i->type) {
+ case C_STRING:
+ case C_CHOICES:
+ /*
+ * Both these control types have a label filling only
+ * the left-hand column of the box.
+ */
+ if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
+ maxlabel < size.cx)
+ maxlabel = size.cx;
+ winheight += height * 3 / 2 + (height / 2);
+ break;
+
+ case C_BOOLEAN:
+ /*
+ * Checkboxes take up the whole of the box width.
+ */
+ if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
+ maxcheckbox < size.cx)
+ maxcheckbox = size.cx;
+ winheight += height + (height / 2);
+ break;
+ }
+ }
+
+ winheight += height + height * 7 / 4; /* OK / Cancel buttons */
+
+ col1l = 2*width;
+ col1r = col1l + maxlabel;
+ col2l = col1r + 2*width;
+ col2r = col2l + 30*width;
+ if (col2r < col1l+2*height+maxcheckbox)
+ col2r = col1l+2*height+maxcheckbox;
+ winwidth = col2r + 2*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;
+
+ AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
+ DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
+ WS_CAPTION | WS_SYSMENU*/) &~
+ (WS_MAXIMIZEBOX | WS_OVERLAPPED),
+ FALSE, 0);
+
+ /*
+ * Centre the dialog on its parent window.
+ */
+ r.right -= r.left;
+ r.bottom -= r.top;
+ GetWindowRect(fe->hwnd, &r2);
+ r.left = (r2.left + r2.right - r.right) / 2;
+ r.top = (r2.top + r2.bottom - r.bottom) / 2;
+ r.right += r.left;
+ r.bottom += r.top;
+
+ fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title,
+ DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
+ WS_CAPTION | WS_SYSMENU,
+ r.left, r.top,
+ r.right-r.left, r.bottom-r.top,
+ fe->hwnd, NULL, fe->inst, NULL);
+ sfree(title);
+ }
+
+ SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
+
+ SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
+ SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc);
+
+ /*
+ * 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 = height/2;
+ 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+height*1/8, y+height*9/8,
+ "Static", 0, 0, i->name, id++);
+ 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);
+ y += height*3/2;
+ break;
+
+ case C_BOOLEAN:
+ /*
+ * Simple checkbox.
+ */
+ 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));
+ y += height;
+ break;
+
+ case C_CHOICES:
+ /*
+ * Drop-down list with a label beside it.
+ */
+ mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
+ "STATIC", 0, 0, i->name, id++);
+ ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2,
+ "COMBOBOX", WS_TABSTOP |
+ CBS_DROPDOWNLIST | CBS_HASSTRINGS,
+ WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
+ {
+ char c, *p, *q, *str;
+
+ SendMessage(ctl, CB_RESETCONTENT, 0, 0);
+ 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';
+ SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str);
+ sfree(str);
+ if (*q) q++;
+ p = q;
+ }
+ }
+
+ SendMessage(ctl, CB_SETCURSEL, i->ival, 0);
+
+ y += height*3/2;
+ break;
+ }
+
+ assert(y < winheight);
+ y += height/2;
+ }
+
+ 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,
+ "OK", IDOK);
+ mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON",
+ BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL);
+
+ SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
+
+ EnableWindow(fe->hwnd, FALSE);
+ ShowWindow(fe->cfgbox, SW_NORMAL);
+ while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
+ if (!IsDialogMessage(fe->cfgbox, &msg))
+ DispatchMessage(&msg);
+ if (fe->dlg_done)
+ break;
+ }
+ EnableWindow(fe->hwnd, TRUE);
+ SetForegroundWindow(fe->hwnd);
+ DestroyWindow(fe->cfgbox);
+ DeleteObject(fe->cfgfont);
+
+ free_cfg(fe->cfg);
+ sfree(fe->cfgaux);
+
+ return (fe->dlg_done == 2);
+}
+
+static void new_game_size(frontend *fe)
+{
+ RECT r, sr;
+ HDC hdc;
+ int x, y;
+
+ 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);
+
+ if (fe->statusbar != NULL) {
+ GetWindowRect(fe->statusbar, &sr);
+ } else {
+ sr.left = sr.right = sr.top = sr.bottom = 0;
+ }
+ SetWindowPos(fe->hwnd, NULL, 0, 0,
+ r.right - r.left,
+ r.bottom - r.top + sr.bottom - sr.top,
+ SWP_NOMOVE | SWP_NOZORDER);
+
+ check_window_size(fe, &x, &y);
+
+ if (fe->statusbar != NULL)
+ SetWindowPos(fe->statusbar, NULL, 0, y, x,
+ sr.bottom - sr.top, SWP_NOZORDER);
+
+ if (fe->bitmap) DeleteObject(fe->bitmap);
+
+ hdc = GetDC(fe->hwnd);
+ fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
+ ReleaseDC(fe->hwnd, hdc);
+
+ midend_redraw(fe->me);
+}
+
+static void new_game_type(frontend *fe)
+{
+ midend_new_game(fe->me);
+ new_game_size(fe);
+}
+
+static int is_alt_pressed(void)
+{
+ BYTE keystate[256];
+ int r = GetKeyboardState(keystate);
+ if (!r)
+ return FALSE;
+ if (keystate[VK_MENU] & 0x80)
+ return TRUE;
+ if (keystate[VK_RMENU] & 0x80)
+ return TRUE;
+ return FALSE;
+}
+
+static 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);
+ int cmd;
+
+ switch (message) {
+ case WM_CLOSE:
+ DestroyWindow(hwnd);
+ return 0;
+ case WM_COMMAND:
+ cmd = wParam & ~0xF; /* low 4 bits reserved to Windows */
+ switch (cmd) {
+ case IDM_NEW:
+ if (!midend_process_key(fe->me, 0, 0, 'n'))
+ PostQuitMessage(0);
+ break;
+ case IDM_RESTART:
+ midend_restart_game(fe->me);
+ break;
+ case IDM_UNDO:
+ if (!midend_process_key(fe->me, 0, 0, 'u'))