Add comments suggesting some solver upgrades to Light Up (perhaps for
[sgt/puzzles] / windows.c
index 3c2c2f1..7bdb5e2 100644 (file)
--- a/windows.c
+++ b/windows.c
@@ -44,6 +44,7 @@
 #define IDM_LOAD      0x00F0
 #define IDM_PRINT     0x0100
 #define IDM_PRESETS   0x0110
+#define IDM_GAMES     0x0300
 
 #define IDM_KEYEMUL   0x0400
 
@@ -60,7 +61,6 @@ static HINSTANCE hh_dll;
 #endif /* NO_HTMLHELP */
 enum { NONE, HLP, CHM } help_type;
 char *help_path;
-const char *help_topic;
 int help_has_contents;
 
 #ifndef FILENAME_MAX
@@ -71,6 +71,12 @@ int help_has_contents;
 #define HGDI_ERROR ((HANDLE)GDI_ERROR)
 #endif
 
+#ifdef COMBINED
+#define CLASSNAME "Puzzles"
+#else
+#define CLASSNAME thegame.name
+#endif
+
 #ifdef _WIN32_WCE
 
 /*
@@ -111,7 +117,7 @@ BOOL GetKeyboardState(PBYTE pb)
   return FALSE;
 }
 
-static TCHAR wGameName[256];
+static TCHAR wClassName[256], wGameName[256];
 
 #endif
 
@@ -122,7 +128,7 @@ static int debug_got_console = 0;
 
 void dputs(char *buf)
 {
-    DWORD dw;
+    /*DWORD dw;
 
     if (!debug_got_console) {
        if (AllocConsole()) {
@@ -137,8 +143,10 @@ void dputs(char *buf)
     if (debug_hdl != INVALID_HANDLE_VALUE) {
        WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
     }
-    fputs(buf, debug_fp);
-    fflush(debug_fp);
+    if (debug_fp) {
+      fputs(buf, debug_fp);
+      fflush(debug_fp);
+    }*/
     OutputDebugString(buf);
 }
 
@@ -146,11 +154,17 @@ void debug_printf(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
 
@@ -161,7 +175,7 @@ void debug_printf(char *fmt, ...)
 #define WINFLAGS (WS_CAPTION | WS_SYSMENU)
 #endif
 
-static void new_game_size(frontend *fe);
+static void new_game_size(frontend *fe, float scale);
 
 struct font {
     HFONT font;
@@ -182,6 +196,7 @@ struct blitter {
 enum { CFG_PRINT = CFG_FRONTEND_SPECIFIC };
 
 struct frontend {
+    const game *game;
     midend *me;
     HWND hwnd, statusbar, cfgbox;
 #ifdef _WIN32_WCE
@@ -195,6 +210,7 @@ struct frontend {
     HBRUSH *brushes;
     HPEN *pens;
     HRGN clip;
+    HMENU gamemenu, typemenu;
     UINT timer;
     DWORD timer_last_tickcount;
     int npresets;
@@ -215,11 +231,28 @@ struct frontend {
     int printoffsetx, printoffsety;
     float printpixelscale;
     int fontstart;
-    int linewidth;
+    int linewidth, linedotted;
     drawing *dr;
     int xmin, ymin;
+    float puzz_scale;
 };
 
+void frontend_free(frontend *fe)
+{
+    midend_free(fe->me);
+
+    sfree(fe->colours);
+    sfree(fe->brushes);
+    sfree(fe->pens);
+    sfree(fe->presets);
+    sfree(fe->fonts);
+
+    sfree(fe);
+}
+
+static void update_type_menu_tick(frontend *fe);
+static void update_copy_menu_greying(frontend *fe);
+
 void fatal(char *fmt, ...)
 {
     char buf[2048];
@@ -398,12 +431,14 @@ static void win_text_colour(frontend *fe, int colour)
     if (fe->drawstatus == PRINTING) {
        int hatch;
        float r, g, b;
-       print_get_colour(fe->dr, colour, &hatch, &r, &g, &b);
-       if (fe->printcolour)
-           SetTextColor(fe->hdc, RGB(r * 255, g * 255, b * 255));
-       else
-           SetTextColor(fe->hdc,
-                        hatch == HATCH_CLEAR ? RGB(255,255,255) : RGB(0,0,0));
+       print_get_colour(fe->dr, colour, fe->printcolour, &hatch, &r, &g, &b);
+
+       /*
+        * Displaying text in hatched colours is not permitted.
+        */
+       assert(hatch < 0);
+
+       SetTextColor(fe->hdc, RGB(r * 255, g * 255, b * 255));
     } else {
        SetTextColor(fe->hdc, fe->colours[colour]);
     }
@@ -417,14 +452,10 @@ static void win_set_brush(frontend *fe, int colour)
     if (fe->drawstatus == PRINTING) {
        int hatch;
        float r, g, b;
-       print_get_colour(fe->dr, colour, &hatch, &r, &g, &b);
+       print_get_colour(fe->dr, colour, fe->printcolour, &hatch, &r, &g, &b);
 
-       if (fe->printcolour) {
+       if (hatch < 0) {
            br = CreateSolidBrush(RGB(r * 255, g * 255, b * 255));
-       } else if (hatch == HATCH_SOLID) {
-           br = CreateSolidBrush(RGB(0,0,0));
-       } else if (hatch == HATCH_CLEAR) {
-           br = CreateSolidBrush(RGB(255,255,255));
        } else {
 #ifdef _WIN32_WCE
            /*
@@ -469,18 +500,16 @@ static void win_set_pen(frontend *fe, int colour, int thin)
        float r, g, b;
        int width = thin ? 0 : fe->linewidth;
 
-       print_get_colour(fe->dr, colour, &hatch, &r, &g, &b);
-       if (fe->printcolour)
-           pen = CreatePen(PS_SOLID, width,
-                           RGB(r * 255, g * 255, b * 255));
-       else if (hatch == HATCH_SOLID)
-           pen = CreatePen(PS_SOLID, width, RGB(0, 0, 0));
-       else if (hatch == HATCH_CLEAR)
-           pen = CreatePen(PS_SOLID, width, RGB(255,255,255));
-       else {
-           assert(!"This shouldn't happen");
-           pen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
-       }
+       if (fe->linedotted)
+           width = 0;
+
+       print_get_colour(fe->dr, colour, fe->printcolour, &hatch, &r, &g, &b);
+       /*
+        * Stroking in hatched colours is not permitted.
+        */
+       assert(hatch < 0);
+       pen = CreatePen(fe->linedotted ? PS_DOT : PS_SOLID,
+                       width, RGB(r * 255, g * 255, b * 255));
     } else {
        pen = fe->pens[colour];
     }
@@ -579,10 +608,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
+       MultiByteToWideChar (CP_UTF8, 0, text, -1, wText, 256);
 
        oldfont = SelectObject(fe->hdc, fe->fonts[i].font);
        if (GetTextMetrics(fe->hdc, &tm)) {
@@ -591,11 +618,8 @@ 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;
            else if (align & ALIGN_HRIGHT)
@@ -603,11 +627,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);
     }
 }
@@ -773,6 +793,17 @@ static void win_line_width(void *handle, float width)
     fe->linewidth = (int)(width * fe->printpixelscale);
 }
 
+static void win_line_dotted(void *handle, int dotted)
+{
+    frontend *fe = (frontend *)handle;
+
+    assert(fe->drawstatus != DRAWING);
+    if (fe->drawstatus == NOTHING)
+       return;
+
+    fe->linedotted = dotted;
+}
+
 static void win_begin_doc(void *handle, int pages)
 {
     frontend *fe = (frontend *)handle;
@@ -842,7 +873,7 @@ static void win_begin_puzzle(void *handle, float xm, float xc,
      * Work out what that comes to in pixels.
      */
     pox = (int)(mmox * (float)ppw / mmpw);
-    poy = (int)(mmoy * (float)ppw / mmpw);
+    poy = (int)(mmoy * (float)pph / mmph);
 
     /*
      * And determine the scale.
@@ -863,6 +894,7 @@ static void win_begin_puzzle(void *handle, float xm, float xc,
     fe->printpixelscale = scale;
 
     fe->linewidth = 1;
+    fe->linedotted = FALSE;
 }
 
 static void win_end_puzzle(void *handle)
@@ -921,6 +953,15 @@ static void win_end_doc(void *handle)
     }
 }
 
+char *win_text_fallback(void *handle, const char *const *strings, int nstrings)
+{
+    /*
+     * We assume Windows can cope with any UTF-8 likely to be
+     * emitted by a puzzle.
+     */
+    return dupstr(strings[0]);
+}
+
 const struct drawing_api win_drawing = {
     win_draw_text,
     win_draw_rect,
@@ -944,6 +985,8 @@ const struct drawing_api win_drawing = {
     win_end_page,
     win_end_doc,
     win_line_width,
+    win_line_dotted,
+    win_text_fallback,
 };
 
 void print(frontend *fe)
@@ -967,7 +1010,7 @@ void print(frontend *fe)
            if (!nme) {
                game_params *params;
 
-               nme = midend_new(NULL, &thegame, NULL, NULL);
+               nme = midend_new(NULL, fe->game, NULL, NULL);
 
                /*
                 * Set the non-interactive mid-end to have the same
@@ -975,7 +1018,7 @@ void print(frontend *fe)
                 */
                params = midend_get_params(fe->me);
                midend_set_params(nme, params);
-               thegame.free_params(params);
+               fe->game->free_params(params);
            }
 
            midend_new_game(nme);
@@ -1021,7 +1064,7 @@ void print(frontend *fe)
     memset(&fe->di, 0, sizeof(fe->di));
     fe->di.cbSize = sizeof(fe->di);
     sprintf(doctitle, "Printed puzzles from %s (from Simon Tatham's"
-           " Portable Puzzle Collection)", thegame.name);
+           " Portable Puzzle Collection)", fe->game->name);
     fe->di.lpszDocName = doctitle;
     fe->di.lpszOutput = NULL;
     fe->di.lpszDatatype = NULL;
@@ -1088,11 +1131,16 @@ void write_clip(HWND hwnd, char *data)
     data2[j] = '\0';
 
     clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
-    if (!clipdata)
+    if (!clipdata) {
+        sfree(data2);
        return;
+    }
     lock = GlobalLock(clipdata);
-    if (!lock)
+    if (!lock) {
+        GlobalFree(clipdata);
+        sfree(data2);
        return;
+    }
     memcpy(lock, data2, len);
     ((unsigned char *) lock)[len] = 0;
     GlobalUnlock(clipdata);
@@ -1147,7 +1195,6 @@ static void init_help(void)
        if (htmlhelp) {
            help_path = dupstr(b);
            help_type = CHM;
-           help_topic = thegame.htmlhelp_topic;
            return;
        }
     }
@@ -1163,8 +1210,6 @@ static void init_help(void)
        help_path = dupstr(b);
        help_type = HLP;
 
-       help_topic = thegame.winhelp_topic;
-
        /*
         * See if there's a .CNT file alongside it.
         */
@@ -1312,7 +1357,7 @@ static void get_menu_size(HWND wh, RECT *r)
 
 static int check_window_resize(frontend *fe, int cx, int cy,
                                int *px, int *py,
-                               int *wx, int *wy, int resize)
+                               int *wx, int *wy)
 {
     RECT r;
     int x, y, sy = get_statusbar_height(fe), changed = 0;
@@ -1325,7 +1370,7 @@ static int check_window_resize(frontend *fe, int cx, int cy,
      * See if we actually got the window size we wanted, and adjust
      * the puzzle size if not.
      */
-    midend_size(fe->me, &x, &y, resize);
+    midend_size(fe->me, &x, &y, TRUE);
     if (x != cx || y != cy) {
         /*
          * Resize the window, now we know what size we _really_
@@ -1343,6 +1388,9 @@ static int check_window_resize(frontend *fe, int cx, int cy,
     *px = x;
     *py = y;
 
+    fe->puzz_scale =
+      (float)midend_tilesize(fe->me) / (float)fe->game->preferred_tilesize;
+
     return changed;
 }
 
@@ -1360,7 +1408,7 @@ static void check_window_size(frontend *fe, int *px, int *py)
     cx = r.right - r.left;
     cy = r.bottom - r.top;
 
-    if (check_window_resize(fe, cx, cy, px, py, &wx, &wy, FALSE)) {
+    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);
@@ -1413,27 +1461,21 @@ static TBBUTTON tbNumpadButtons[] =
 };
 #endif
 
-static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
+/*
+ * Allocate a new frontend structure and create its main window.
+ */
+static frontend *frontend_new(HINSTANCE inst)
 {
     frontend *fe;
-    int x, y;
-    RECT r;
+    const char *nogame = "Puzzles (no game selected)";
 
     fe = snew(frontend);
 
-    fe->me = midend_new(fe, &thegame, &win_drawing, fe);
-
-    if (game_id) {
-        *error = midend_game_id(fe->me, game_id);
-        if (*error) {
-            midend_free(fe->me);
-            sfree(fe);
-            return NULL;
-        }
-    }
-
     fe->inst = inst;
 
+    fe->game = NULL;
+    fe->me = NULL;
+
     fe->timer = 0;
     fe->hwnd = NULL;
 
@@ -1443,17 +1485,103 @@ static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
     fe->dr = NULL;
     fe->fontstart = 0;
 
-    midend_new_game(fe->me);
-
     fe->fonts = NULL;
     fe->nfonts = fe->fontsize = 0;
 
+    fe->colours = NULL;
+    fe->brushes = NULL;
+    fe->pens = NULL;
+
+    fe->puzz_scale = 1.0;
+
+    #ifdef _WIN32_WCE
+    MultiByteToWideChar (CP_ACP, 0, nogame, -1, wGameName, 256);
+    fe->hwnd = CreateWindowEx(0, wClassName, wGameName,
+                             WS_VISIBLE,
+                             CW_USEDEFAULT, CW_USEDEFAULT,
+                             CW_USEDEFAULT, CW_USEDEFAULT,
+                             NULL, NULL, inst, NULL);
+
+    {
+       SHMENUBARINFO mbi;
+       RECT rc, rcBar, rcTB, rcClient;
+
+       memset (&mbi, 0, sizeof(SHMENUBARINFO));
+       mbi.cbSize     = sizeof(SHMENUBARINFO);
+       mbi.hwndParent = fe->hwnd;
+       mbi.nToolBarId = IDR_MENUBAR1;
+       mbi.hInstRes   = inst;
+
+       SHCreateMenuBar(&mbi);
+
+       GetWindowRect(fe->hwnd, &rc);
+       GetWindowRect(mbi.hwndMB, &rcBar);
+       rc.bottom -= rcBar.bottom - rcBar.top;
+       MoveWindow(fe->hwnd, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, FALSE);
+
+        fe->numpad = NULL;
+    }
+#else
+    fe->hwnd = CreateWindowEx(0, CLASSNAME, nogame,
+                             WS_OVERLAPPEDWINDOW &~
+                             (WS_MAXIMIZEBOX),
+                             CW_USEDEFAULT, CW_USEDEFAULT,
+                             CW_USEDEFAULT, CW_USEDEFAULT,
+                             NULL, NULL, inst, NULL);
+    if (!fe->hwnd) {
+        DWORD lerr = GetLastError();
+        printf("no window: 0x%x\n", lerr);
+    }
+#endif
+
+    fe->gamemenu = NULL;
+    fe->presets = NULL;
+
+    fe->statusbar = NULL;
+    fe->bitmap = NULL;
+
+    SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe);
+
+    return fe;
+}
+
+/*
+ * 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);
@@ -1468,11 +1596,13 @@ static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
         sfree(colours);
     }
 
+    if (fe->statusbar)
+        DestroyWindow(fe->statusbar);
     if (midend_wants_statusbar(fe->me)) {
        fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, TEXT("ooh"),
                                       WS_CHILD | WS_VISIBLE,
                                       0, 0, 0, 0, /* status bar does these */
-                                      NULL, NULL, inst, NULL);
+                                      NULL, NULL, fe->inst, NULL);
     } else
         fe->statusbar = NULL;
 
@@ -1485,65 +1615,42 @@ static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
     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);
-
+    if (fe->numpad)
+        DestroyWindow(fe->numpad);
+    if (fe->game->flags & REQUIRE_NUMPAD)
     {
-       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;
-    }
+        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
-    fe->hwnd = CreateWindowEx(0, thegame.name, thegame.name,
-                             WS_OVERLAPPEDWINDOW &~
-                             (WS_MAXIMIZEBOX),
-                             CW_USEDEFAULT, CW_USEDEFAULT,
-                             r.right - r.left, r.bottom - r.top,
-                             NULL, NULL, inst, NULL);
+    SetWindowText(fe->hwnd, fe->game->name);
 #endif
 
+    if (fe->statusbar)
+        DestroyWindow(fe->statusbar);
     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);
+                                      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);
@@ -1567,6 +1674,8 @@ static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
     }
 
     {
+        HMENU oldmenu = GetMenu(fe->hwnd);
+
 #ifndef _WIN32_WCE
        HMENU bar = CreateMenu();
        HMENU menu = CreateMenu();
@@ -1577,6 +1686,7 @@ static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
        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
@@ -1585,8 +1695,10 @@ static frontend *new_window(HINSTANCE inst, 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 ||
-           thegame.can_configure) {
+           fe->game->can_configure) {
            int i;
 #ifndef _WIN32_WCE
            HMENU sub = CreateMenu();
@@ -1618,17 +1730,42 @@ static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
                AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, wName);
 #endif
            }
-           if (thegame.can_configure) {
+           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 (thegame.can_print) {
+       if (fe->game->can_print) {
            AppendMenu(menu, MF_ENABLED, IDM_PRINT, TEXT("&Print..."));
            AppendMenu(menu, MF_SEPARATOR, 0, 0);
        }
@@ -1636,12 +1773,12 @@ static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
        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) {
+       if (fe->game->can_format_as_text_ever) {
            AppendMenu(menu, MF_SEPARATOR, 0, 0);
            AppendMenu(menu, MF_ENABLED, IDM_COPY, TEXT("&Copy"));
        }
 #endif
-       if (thegame.can_solve) {
+       if (fe->game->can_solve) {
            AppendMenu(menu, MF_SEPARATOR, 0, 0);
            AppendMenu(menu, MF_ENABLED, IDM_SOLVE, TEXT("Sol&ve"));
        }
@@ -1654,35 +1791,38 @@ static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
        AppendMenu(menu, MF_ENABLED, IDM_ABOUT, TEXT("&About"));
 #ifndef _WIN32_WCE
         if (help_type != NONE) {
-           AppendMenu(menu, MF_SEPARATOR, 0, 0);
+            char *item;
+            AppendMenu(menu, MF_SEPARATOR, 0, 0);
             AppendMenu(menu, MF_ENABLED, IDM_HELPC, TEXT("&Contents"));
-            if (help_topic) {
-                char *item;
-                assert(thegame.name);
-                item = snewn(10+strlen(thegame.name), char); /*ick*/
-                sprintf(item, "&Help on %s", thegame.name);
-                AppendMenu(menu, MF_ENABLED, IDM_GAMEHELP, item);
-                sfree(item);
-            }
+            assert(fe->game->name);
+            item = snewn(10+strlen(fe->game->name), char); /*ick*/
+            sprintf(item, "&Help on %s", fe->game->name);
+            AppendMenu(menu, MF_ENABLED, IDM_GAMEHELP, item);
+            sfree(item);
         }
+        DestroyMenu(oldmenu);
        SetMenu(fe->hwnd, bar);
         get_menu_size(fe->hwnd, &menusize);
         fe->xmin = (menusize.right - menusize.left) + 25;
 #endif
     }
 
+    if (fe->bitmap) DeleteObject(fe->bitmap);
     fe->bitmap = NULL;
-    new_game_size(fe); /* initialises fe->bitmap */
-    check_window_size(fe, &x, &y);
+    new_game_size(fe, fe->puzz_scale); /* initialises fe->bitmap */
 
-    SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe);
+    return 0;
+}
 
+static void show_window(frontend *fe)
+{
     ShowWindow(fe->hwnd, SW_SHOWNORMAL);
     SetForegroundWindow(fe->hwnd);
 
-    midend_redraw(fe->me);
+    update_type_menu_tick(fe);
+    update_copy_menu_greying(fe);
 
-    return fe;
+    midend_redraw(fe->me);
 }
 
 #ifdef _WIN32_WCE
@@ -1728,13 +1868,13 @@ static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg,
 
            make_dialog_full_screen(hwnd);
 
-           sprintf(title, "About %.250s", thegame.name);
+           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, thegame.name);
+           SetDlgItemTextA(hwnd, IDC_ABOUT_GAME, fe->game->name);
            SetDlgItemTextA(hwnd, IDC_ABOUT_VERSION, ver);
        }
 #endif
@@ -1774,8 +1914,8 @@ static config_item *frontend_get_config(frontend *fe, int which,
        config_item *ret;
        int i;
 
-       *wintitle = snewn(40 + strlen(thegame.name), char);
-       sprintf(*wintitle, "%s print setup", thegame.name);
+       *wintitle = snewn(40 + strlen(fe->game->name), char);
+       sprintf(*wintitle, "%s print setup", fe->game->name);
 
        ret = snewn(8, config_item);
 
@@ -1817,7 +1957,7 @@ static config_item *frontend_get_config(frontend *fe, int which,
        ret[i].ival = FALSE;
        i++;
 
-       if (thegame.can_print_in_colour) {
+       if (fe->game->can_print_in_colour) {
            ret[i].name = "Print in colour";
            ret[i].type = C_BOOLEAN;
            ret[i].sval = NULL;
@@ -1853,7 +1993,7 @@ static char *frontend_set_config(frontend *fe, int which, config_item *cfg)
            return "Print size should be positive";
        fe->printcurr = cfg[4].ival;
        fe->printsolns = cfg[5].ival;
-       fe->printcolour = thegame.can_print_in_colour && cfg[6].ival;
+       fe->printcolour = fe->game->can_print_in_colour && cfg[6].ival;
        return NULL;
     } else {
        assert(!"We should never get here");
@@ -2106,9 +2246,9 @@ static void about(frontend *fe)
     int nstrings = 0;
     char titlebuf[512];
 
-    sprintf(titlebuf, "About %.250s", thegame.name);
+    sprintf(titlebuf, "About %.250s", fe->game->name);
 
-    strings[nstrings++] = thegame.name;
+    strings[nstrings++] = fe->game->name;
     strings[nstrings++] = "from Simon Tatham's Portable Puzzle Collection";
     strings[nstrings++] = ver;
 
@@ -2543,13 +2683,19 @@ static void new_bitmap(frontend *fe, int x, int y)
     ReleaseDC(fe->hwnd, hdc);
 }
 
-static void new_game_size(frontend *fe)
+static void new_game_size(frontend *fe, float scale)
 {
     RECT r, sr;
     int x, y;
 
     get_max_puzzle_size(fe, &x, &y);
     midend_size(fe->me, &x, &y, FALSE);
+
+    if (scale != 1.0) {
+      x = (int)((float)x * fe->puzz_scale);
+      y = (int)((float)y * fe->puzz_scale);
+      midend_size(fe->me, &x, &y, TRUE);
+    }
     fe->ymin = (fe->xmin * y) / x;
 
     r.left = r.top = 0;
@@ -2620,7 +2766,7 @@ static void adjust_game_size(frontend *fe, RECT *proposed, int isedge,
     if (check_window_resize(fe,
                             (cr.right - cr.left) + xdiff,
                             (cr.bottom - cr.top) + ydiff,
-                            &nx, &ny, &wx, &wy, TRUE)) {
+                            &nx, &ny, &wx, &wy)) {
         new_bitmap(fe, nx, ny);
         midend_force_redraw(fe->me);
     } else {
@@ -2636,10 +2782,39 @@ static void adjust_game_size(frontend *fe, RECT *proposed, int isedge,
     *wx_r = wx; *wy_r = wy;
 }
 
+static void update_type_menu_tick(frontend *fe)
+{
+    int total, n, i;
+
+    if (fe->typemenu == INVALID_HANDLE_VALUE)
+       return;
+
+    total = GetMenuItemCount(fe->typemenu);
+    n = midend_which_preset(fe->me);
+    if (n < 0)
+       n = total - 1;                 /* "Custom" item */
+
+    for (i = 0; i < total; i++) {
+       int flag = (i == n ? MF_CHECKED : MF_UNCHECKED);
+       CheckMenuItem(fe->typemenu, i, MF_BYPOSITION | flag);
+    }
+
+    DrawMenuBar(fe->hwnd);
+}
+
+static void update_copy_menu_greying(frontend *fe)
+{
+    UINT enable = (midend_can_format_as_text_now(fe->me) ?
+                  MF_ENABLED : MF_GRAYED);
+    EnableMenuItem(fe->gamemenu, IDM_COPY, MF_BYCOMMAND | enable);
+}
+
 static void new_game_type(frontend *fe)
 {
     midend_new_game(fe->me);
-    new_game_size(fe);
+    new_game_size(fe, 1.0);
+    update_type_menu_tick(fe);
+    update_copy_menu_greying(fe);
 }
 
 static int is_alt_pressed(void)
@@ -2826,7 +3001,7 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
                            break;
                        }
 
-                       new_game_size(fe);
+                       new_game_size(fe, 1.0);
                    }
                }
            }
@@ -2837,10 +3012,20 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
            start_help(fe, NULL);
            break;
           case IDM_GAMEHELP:
-           start_help(fe, help_topic);
+            assert(help_type != NONE);
+           start_help(fe, help_type == CHM ?
+                       fe->game->htmlhelp_topic : fe->game->winhelp_topic);
             break;
 #endif
          default:
+#ifdef COMBINED
+            if (wParam >= IDM_GAMES && wParam < (IDM_GAMES + (WPARAM)gamecount)) {
+                int p = wParam - IDM_GAMES;
+                char *error;
+
+                new_game(fe, gamelist[p], NULL, &error);
+            } else
+#endif
            {
                int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10;
 
@@ -2856,7 +3041,8 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
 #ifndef _WIN32_WCE
        stop_help(fe);
 #endif
-       PostQuitMessage(0);
+        frontend_free(fe);
+        PostQuitMessage(0);
        return 0;
       case WM_PAINT:
        {
@@ -2986,7 +3172,7 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
 #ifndef _WIN32_WCE
                button = LEFT_BUTTON;
 #else
-               if ((thegame.flags & REQUIRE_RBUTTON) == 0)
+               if ((fe->game->flags & REQUIRE_RBUTTON) == 0)
                    button = LEFT_BUTTON;
                else
                {
@@ -3129,9 +3315,11 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
 {
     MSG msg;
     char *error;
+    const game *gg;
+    frontend *fe;
 
 #ifdef _WIN32_WCE
-    MultiByteToWideChar (CP_ACP, 0, thegame.name, -1, wGameName, 256);
+    MultiByteToWideChar (CP_ACP, 0, CLASSNAME, -1, wClassName, 256);
     if (FindPreviousInstance ())
         return 0;
 #endif
@@ -3155,9 +3343,9 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
        wndclass.hbrBackground = NULL;
        wndclass.lpszMenuName = NULL;
 #ifdef _WIN32_WCE
-       wndclass.lpszClassName = wGameName;
+       wndclass.lpszClassName = wClassName;
 #else
-       wndclass.lpszClassName = thegame.name;
+       wndclass.lpszClassName = CLASSNAME;
 #endif
 
        RegisterClass(&wndclass);
@@ -3168,18 +3356,57 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
 
     init_help();
 
-    if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) {
+#ifdef COMBINED
+    gg = gamelist[0];
+    {
+        int i;
+        for (i = 0; i < gamecount; i++) {
+           const char *p = gamelist[i]->name;
+           char *q = cmdline;
+           while (*q && isspace((unsigned char)*q))
+               q++;
+           while (*p && *q) {
+               if (isspace((unsigned char)*p)) {
+                   while (*q && isspace((unsigned char)*q))
+                       q++;
+               } else {
+                   if (tolower((unsigned char)*p) !=
+                       tolower((unsigned char)*q))
+                       break;
+                   q++;
+               }
+               p++;
+           }
+           if (!*p) {
+                gg = gamelist[i];
+                cmdline = q;
+                if (*cmdline) {
+                   while (*cmdline && isspace((unsigned char)*cmdline))
+                       cmdline++;
+                }
+            }
+        }
+    }
+#else
+    gg = &thegame;
+#endif
+
+    fe = frontend_new(inst);
+    if (new_game(fe, gg, *cmdline ? cmdline : NULL, &error)) {
        char buf[128];
-       sprintf(buf, "%.100s Error", thegame.name);
+       sprintf(buf, "%.100s Error", gg->name);
        MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
        return 1;
     }
+    show_window(fe);
 
     while (GetMessage(&msg, NULL, 0, 0)) {
        DispatchMessage(&msg);
     }
 
+    DestroyWindow(fe->hwnd);
     cleanup_help();
 
     return msg.wParam;
 }
+/* vim: set shiftwidth=4 tabstop=8: */