+}
+
+const struct drawing_api win_drawing = {
+ win_draw_text,
+ win_draw_rect,
+ win_draw_line,
+ win_draw_polygon,
+ win_draw_circle,
+ win_draw_update,
+ win_clip,
+ win_unclip,
+ win_start_draw,
+ win_end_draw,
+ win_status_bar,
+ win_blitter_new,
+ win_blitter_free,
+ win_blitter_save,
+ win_blitter_load,
+ win_begin_doc,
+ win_begin_page,
+ win_begin_puzzle,
+ win_end_puzzle,
+ win_end_page,
+ win_end_doc,
+ win_line_width,
+};
+
+void print(frontend *fe)
+{
+#ifndef _WIN32_WCE
+ PRINTDLG pd;
+ char doctitle[256];
+ document *doc;
+ midend *nme = NULL; /* non-interactive midend for bulk puzzle generation */
+ int i;
+ char *err = NULL;
+
+ /*
+ * Create our document structure and fill it up with puzzles.
+ */
+ doc = document_new(fe->printw, fe->printh, fe->printscale / 100.0F);
+ for (i = 0; i < fe->printcount; i++) {
+ if (i == 0 && fe->printcurr) {
+ err = midend_print_puzzle(fe->me, doc, fe->printsolns);
+ } else {
+ if (!nme) {
+ game_params *params;
+
+ nme = midend_new(NULL, &thegame, NULL, NULL);
+
+ /*
+ * Set the non-interactive mid-end to have the same
+ * parameters as the standard one.
+ */
+ params = midend_get_params(fe->me);
+ midend_set_params(nme, params);
+ thegame.free_params(params);
+ }
+
+ midend_new_game(nme);
+ err = midend_print_puzzle(nme, doc, fe->printsolns);
+ }
+ if (err)
+ break;
+ }
+ if (nme)
+ midend_free(nme);
+
+ if (err) {
+ MessageBox(fe->hwnd, err, "Error preparing puzzles for printing",
+ MB_ICONERROR | MB_OK);
+ document_free(doc);
+ return;
+ }
+
+ memset(&pd, 0, sizeof(pd));
+ pd.lStructSize = sizeof(pd);
+ pd.hwndOwner = fe->hwnd;
+ pd.hDevMode = NULL;
+ pd.hDevNames = NULL;
+ pd.Flags = PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC |
+ PD_NOPAGENUMS | PD_NOSELECTION;
+ pd.nCopies = 1;
+ pd.nFromPage = pd.nToPage = 0xFFFF;
+ pd.nMinPage = pd.nMaxPage = 1;
+
+ if (!PrintDlg(&pd)) {
+ document_free(doc);
+ return;
+ }
+
+ /*
+ * Now pd.hDC is a device context for the printer.
+ */
+
+ /*
+ * FIXME: IWBNI we put up an Abort box here.
+ */
+
+ 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);
+ fe->di.lpszDocName = doctitle;
+ fe->di.lpszOutput = NULL;
+ fe->di.lpszDatatype = NULL;
+ fe->di.fwType = 0;
+
+ fe->drawstatus = PRINTING;
+ fe->hdc = pd.hDC;
+
+ fe->dr = drawing_new(&win_drawing, NULL, fe);
+ document_print(doc, fe->dr);
+ drawing_free(fe->dr);
+ fe->dr = NULL;
+
+ fe->drawstatus = NOTHING;
+
+ DeleteDC(pd.hDC);
+ document_free(doc);
+#endif
+}
+
+void deactivate_timer(frontend *fe)
+{
+ if (!fe)
+ return; /* for non-interactive midend */
+ if (fe->hwnd) KillTimer(fe->hwnd, fe->timer);
+ fe->timer = 0;
+}
+
+void activate_timer(frontend *fe)
+{
+ if (!fe)
+ return; /* for non-interactive midend */
+ if (!fe->timer) {
+ fe->timer = SetTimer(fe->hwnd, 1, 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)
+{
+#ifndef _WIN32_WCE
+ 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) {
+ 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 */
+#endif
+}
+
+#ifndef _WIN32_WCE
+
+/*
+ * 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;
+ }
+}
+
+#endif
+
+/*
+ * Terminate Help on process exit.
+ */
+static void cleanup_help(void)
+{
+ /* 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)
+{
+ 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);
+#ifndef _WIN32_WCE
+ SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left, r.bottom - r.top,
+ SWP_NOMOVE | SWP_NOZORDER);
+#endif
+ }
+
+ if (fe->statusbar) {
+ GetClientRect(fe->hwnd, &r);
+#ifndef _WIN32_WCE
+ SetWindowPos(fe->statusbar, NULL, 0, r.bottom-r.top-sy, r.right-r.left,
+ sy, SWP_NOZORDER);
+#endif
+ }
+
+ *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;
+ }
+}
+
+#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
+
+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, TEXT("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);
+
+#ifdef _WIN32_WCE
+ fe->hwnd = CreateWindowEx(0, wGameName, 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);
+
+ if (thegame.flags & REQUIRE_NUMPAD)
+ {
+ fe->numpad = CreateToolbarEx (fe->hwnd,
+ WS_VISIBLE | WS_CHILD | CCS_NOPARENTALIGN | TBSTYLE_FLAT,
+ 0, 10, 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;
+ }
+#else
+ 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);
+#endif
+
+ if (midend_wants_statusbar(fe->me)) {
+ RECT sr;
+ DestroyWindow(fe->statusbar);
+ fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, TEXT("ooh"),
+ WS_CHILD | WS_VISIBLE,
+ 0, 0, 0, 0, /* status bar does these */
+ fe->hwnd, NULL, 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;
+ }
+
+ {
+#ifndef _WIN32_WCE
+ HMENU bar = CreateMenu();
+ HMENU menu = CreateMenu();
+
+ AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Game");
+#else
+ HMENU menu = SHGetSubMenu(SHFindMenuBar(fe->hwnd), ID_GAME);
+ DeleteMenu(menu, 0, MF_BYPOSITION);
+#endif
+ AppendMenu(menu, MF_ENABLED, IDM_NEW, TEXT("New"));
+ AppendMenu(menu, MF_ENABLED, IDM_RESTART, TEXT("Restart"));
+#ifndef _WIN32_WCE
+ AppendMenu(menu, MF_ENABLED, IDM_DESC, TEXT("Specific..."));
+ AppendMenu(menu, MF_ENABLED, IDM_SEED, TEXT("Random Seed..."));
+#endif
+
+ if ((fe->npresets = midend_num_presets(fe->me)) > 0 ||
+ thegame.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 (thegame.can_configure) {
+ AppendMenu(sub, MF_ENABLED, IDM_CONFIG, TEXT("Custom..."));
+ }
+ }
+
+ 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 (thegame.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 (thegame.can_format_as_text) {
+ AppendMenu(menu, MF_SEPARATOR, 0, 0);
+ AppendMenu(menu, MF_ENABLED, IDM_COPY, TEXT("Copy"));
+ }
+#endif
+ if (thegame.can_solve) {
+ AppendMenu(menu, MF_SEPARATOR, 0, 0);
+ AppendMenu(menu, MF_ENABLED, IDM_SOLVE, TEXT("Solve"));
+ }
+ AppendMenu(menu, MF_SEPARATOR, 0, 0);
+#ifndef _WIN32_WCE
+ AppendMenu(menu, MF_ENABLED, IDM_QUIT, TEXT("Exit"));
+ 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) {
+ AppendMenu(menu, MF_SEPARATOR, 0, 0);
+ AppendMenu(menu, MF_ENABLED, IDM_HELPC, TEXT("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);
+#endif
+ }
+
+ 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_SHOWNORMAL);
+ SetForegroundWindow(fe->hwnd);
+
+ 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)
+{
+ 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", thegame.name);
+ SetDlgItemTextA(hwnd, IDC_ABOUT_CAPTION, title);
+
+ SendDlgItemMessage(hwnd, IDC_ABOUT_CAPTION, WM_SETFONT,
+ (WPARAM) dialog_title_font(), 0);
+
+ SetDlgItemTextA(hwnd, IDC_ABOUT_GAME, thegame.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(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";
+ }
+}
+
+#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", thegame.name);
+
+ strings[nstrings++] = thegame.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);