chiark / gitweb /
Fix completion checking in Killer Solo.
[sgt-puzzles.git] / windows.c
index 3c2c2f1009fb773299b1900ac8ac068aeb46c2ad..9cc66e2bd9eacc905fae6fc470cbc79b60dc1304 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
+       WCHAR wText[256];
+       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,206 @@ 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;
+}
+
+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);
+}
+
+/*
+ * Create an appropriate midend structure to go in a puzzle window,
+ * given a game type and/or a command-line argument.
+ *
+ * 'arg' can be either a game ID string (descriptive, random, or a
+ * plain set of parameters) or the filename of a save file. The two
+ * boolean flag arguments indicate which possibilities are
+ * permissible.
+ */
+static midend *midend_for_new_game(frontend *fe, const game *cgame,
+                                   char *arg, int maybe_game_id,
+                                   int maybe_save_file, char **error)
+{
+    midend *me = NULL;
+
+    if (!arg) {
+        if (me) midend_free(me);
+        me = midend_new(fe, cgame, &win_drawing, fe);
+        midend_new_game(me);
+    } else {
+        FILE *fp;
+        char *err_param, *err_load;
+
+        /*
+         * See if arg is a valid filename of a save game file.
+         */
+        err_load = NULL;
+        if (maybe_save_file && (fp = fopen(arg, "r")) != NULL) {
+            const game *loadgame;
+
+#ifdef COMBINED
+            /*
+             * Find out what kind of game is stored in the save
+             * file; if we're going to end up loading that, it
+             * will have to override our caller's judgment as to
+             * what game to initialise our midend with.
+             */
+            char *id_name;
+            err_load = identify_game(&id_name, savefile_read, fp);
+            if (!err_load) {
+                int i;
+                for (i = 0; i < gamecount; i++)
+                    if (!strcmp(id_name, gamelist[i]->name))
+                        break;
+                if (i == gamecount) {
+                    err_load = "Save file is for a game not supported by"
+                        " this program";
+                } else {
+                    loadgame = gamelist[i];
+                    rewind(fp); /* go back to the start for actual load */
+                }
+            }
+#else
+            loadgame = cgame;
+#endif
+            if (!err_load) {
+                if (me) midend_free(me);
+                me = midend_new(fe, loadgame, &win_drawing, fe);
+                err_load = midend_deserialise(me, savefile_read, fp);
+            }
+        } else {
+            err_load = "Unable to open file";
+        }
+
+        if (maybe_game_id && (!maybe_save_file || err_load)) {
+            /*
+             * See if arg is a game description.
+             */
+            if (me) midend_free(me);
+            me = midend_new(fe, cgame, &win_drawing, fe);
+            err_param = midend_game_id(me, arg);
+            if (!err_param) {
+                midend_new_game(me);
+            } else {
+                if (maybe_save_file) {
+                    *error = snewn(256 + strlen(arg) + strlen(err_param) +
+                                   strlen(err_load), char);
+                    sprintf(*error, "Supplied argument \"%s\" is neither a"
+                            " game ID (%s) nor a save file (%s)",
+                            arg, err_param, err_load);
+                } else {
+                    *error = dupstr(err_param);
+                }
+                midend_free(me);
+                sfree(fe);
+                return NULL;
+            }
+        } else if (err_load) {
+            *error = dupstr(err_load);
+            midend_free(me);
+            sfree(fe);
+            return NULL;
+        }
+    }
+
+    return me;
+}
+
+/*
+ * Populate a frontend structure with a new midend structure, and
+ * create any window furniture that it needs.
+ *
+ * Previously-allocated memory and window furniture will be freed by
+ * this function.
+ *
+ */
+static int fe_set_midend(frontend *fe, midend *me)
+{
+    int x, y;
+    RECT r;
+
+    if (fe->me) midend_free(fe->me);
+    fe->me = me;
+    fe->game = midend_which_game(fe->me);
+
     {
        int i, ncolours;
         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 +1699,14 @@ 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"),
+       fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME,
+                                      TEXT(DEFAULT_STATUSBAR_TEXT),
                                       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 +1719,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 +1778,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 +1790,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 +1799,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 +1834,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 +1877,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 +1895,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 +1972,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 +2018,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 +2061,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 +2097,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 +2350,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 +2787,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 +2870,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 +2886,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)
@@ -2655,21 +2934,6 @@ static int is_alt_pressed(void)
     return FALSE;
 }
 
-static void savefile_write(void *wctx, void *buf, int len)
-{
-    FILE *fp = (FILE *)wctx;
-    fwrite(buf, 1, len, fp);
-}
-
-static int savefile_read(void *wctx, void *buf, int len)
-{
-    FILE *fp = (FILE *)wctx;
-    int ret;
-
-    ret = fread(buf, 1, len, fp);
-    return (ret == len);
-}
-
 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
                                WPARAM wParam, LPARAM lParam)
 {
@@ -2809,7 +3073,11 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
                        fclose(fp);
                    } else {
                        FILE *fp = fopen(filename, "r");
-                       char *err;
+                       char *err = NULL;
+                        midend *me = fe->me;
+#ifdef COMBINED
+                        char *id_name;
+#endif
 
                        if (!fp) {
                            MessageBox(hwnd, "Unable to open saved game file",
@@ -2817,7 +3085,30 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
                            break;
                        }
 
-                       err = midend_deserialise(fe->me, savefile_read, fp);
+#ifdef COMBINED
+                        /*
+                         * This save file might be from a different
+                         * game.
+                         */
+                        err = identify_game(&id_name, savefile_read, fp);
+                        if (!err) {
+                            int i;
+                            for (i = 0; i < gamecount; i++)
+                                if (!strcmp(id_name, gamelist[i]->name))
+                                    break;
+                            if (i == gamecount) {
+                                err = "Save file is for a game not "
+                                    "supported by this program";
+                            } else {
+                                me = midend_for_new_game(fe, gamelist[i], NULL,
+                                                         FALSE, FALSE, &err);
+                                rewind(fp); /* for the actual load */
+                            }
+                            sfree(id_name);
+                        }
+#endif
+                        if (!err)
+                            err = midend_deserialise(me, savefile_read, fp);
 
                        fclose(fp);
 
@@ -2826,7 +3117,9 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
                            break;
                        }
 
-                       new_game_size(fe);
+                        if (fe->me != me)
+                            fe_set_midend(fe, me);
+                       new_game_size(fe, 1.0);
                    }
                }
            }
@@ -2837,10 +3130,21 @@ 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 = NULL;
+                fe_set_midend(fe, midend_for_new_game(fe, gamelist[p], NULL,
+                                                      FALSE, FALSE, &error));
+                sfree(error);
+            } else
+#endif
            {
                int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10;
 
@@ -2856,7 +3160,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 +3291,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
                {
@@ -3125,13 +3430,240 @@ static int FindPreviousInstance()
 }
 #endif
 
+/*
+ * Split a complete command line into argc/argv, attempting to do it
+ * exactly the same way the Visual Studio C library would do it (so
+ * that our console utilities, which receive argc and argv already
+ * broken apart by the C library, will have their command lines
+ * processed in the same way as the GUI utilities which get a whole
+ * command line and must call this function).
+ * 
+ * Does not modify the input command line.
+ * 
+ * The final parameter (argstart) is used to return a second array
+ * of char * pointers, the same length as argv, each one pointing
+ * at the start of the corresponding element of argv in the
+ * original command line. So if you get half way through processing
+ * your command line in argc/argv form and then decide you want to
+ * treat the rest as a raw string, you can. If you don't want to,
+ * `argstart' can be safely left NULL.
+ */
+void split_into_argv(char *cmdline, int *argc, char ***argv,
+                    char ***argstart)
+{
+    char *p;
+    char *outputline, *q;
+    char **outputargv, **outputargstart;
+    int outputargc;
+
+    /*
+     * These argument-breaking rules apply to Visual Studio 7, which
+     * is currently the compiler expected to be used for the Windows
+     * port of my puzzles. Visual Studio 10 has different rules,
+     * lacking the curious mod 3 behaviour of consecutive quotes
+     * described below; I presume they fixed a bug. As and when we
+     * migrate to a newer compiler, we'll have to adjust this to
+     * match; however, for the moment we faithfully imitate in our GUI
+     * utilities what our CLI utilities can't be prevented from doing.
+     *
+     * When I investigated this, at first glance the rules appeared to
+     * be:
+     *
+     *  - Single quotes are not special characters.
+     *
+     *  - Double quotes are removed, but within them spaces cease
+     *    to be special.
+     *
+     *  - Backslashes are _only_ special when a sequence of them
+     *    appear just before a double quote. In this situation,
+     *    they are treated like C backslashes: so \" just gives a
+     *    literal quote, \\" gives a literal backslash and then
+     *    opens or closes a double-quoted segment, \\\" gives a
+     *    literal backslash and then a literal quote, \\\\" gives
+     *    two literal backslashes and then opens/closes a
+     *    double-quoted segment, and so forth. Note that this
+     *    behaviour is identical inside and outside double quotes.
+     *
+     *  - Two successive double quotes become one literal double
+     *    quote, but only _inside_ a double-quoted segment.
+     *    Outside, they just form an empty double-quoted segment
+     *    (which may cause an empty argument word).
+     *
+     *  - That only leaves the interesting question of what happens
+     *    when one or more backslashes precedes two or more double
+     *    quotes, starting inside a double-quoted string. And the
+     *    answer to that appears somewhat bizarre. Here I tabulate
+     *    number of backslashes (across the top) against number of
+     *    quotes (down the left), and indicate how many backslashes
+     *    are output, how many quotes are output, and whether a
+     *    quoted segment is open at the end of the sequence:
+     * 
+     *                      backslashes
+     * 
+     *               0         1      2      3      4
+     * 
+     *         0   0,0,y  |  1,0,y  2,0,y  3,0,y  4,0,y
+     *            --------+-----------------------------
+     *         1   0,0,n  |  0,1,y  1,0,n  1,1,y  2,0,n
+     *    q    2   0,1,n  |  0,1,n  1,1,n  1,1,n  2,1,n
+     *    u    3   0,1,y  |  0,2,n  1,1,y  1,2,n  2,1,y
+     *    o    4   0,1,n  |  0,2,y  1,1,n  1,2,y  2,1,n
+     *    t    5   0,2,n  |  0,2,n  1,2,n  1,2,n  2,2,n
+     *    e    6   0,2,y  |  0,3,n  1,2,y  1,3,n  2,2,y
+     *    s    7   0,2,n  |  0,3,y  1,2,n  1,3,y  2,2,n
+     *         8   0,3,n  |  0,3,n  1,3,n  1,3,n  2,3,n
+     *         9   0,3,y  |  0,4,n  1,3,y  1,4,n  2,3,y
+     *        10   0,3,n  |  0,4,y  1,3,n  1,4,y  2,3,n
+     *        11   0,4,n  |  0,4,n  1,4,n  1,4,n  2,4,n
+     * 
+     * 
+     *      [Test fragment was of the form "a\\\"""b c" d.]
+     * 
+     * There is very weird mod-3 behaviour going on here in the
+     * number of quotes, and it even applies when there aren't any
+     * backslashes! How ghastly.
+     * 
+     * With a bit of thought, this extremely odd diagram suddenly
+     * coalesced itself into a coherent, if still ghastly, model of
+     * how things work:
+     * 
+     *  - As before, backslashes are only special when one or more
+     *    of them appear contiguously before at least one double
+     *    quote. In this situation the backslashes do exactly what
+     *    you'd expect: each one quotes the next thing in front of
+     *    it, so you end up with n/2 literal backslashes (if n is
+     *    even) or (n-1)/2 literal backslashes and a literal quote
+     *    (if n is odd). In the latter case the double quote
+     *    character right after the backslashes is used up.
+     * 
+     *  - After that, any remaining double quotes are processed. A
+     *    string of contiguous unescaped double quotes has a mod-3
+     *    behaviour:
+     * 
+     *     * inside a quoted segment, a quote ends the segment.
+     *     * _immediately_ after ending a quoted segment, a quote
+     *       simply produces a literal quote.
+     *     * otherwise, outside a quoted segment, a quote begins a
+     *       quoted segment.
+     * 
+     *    So, for example, if we started inside a quoted segment
+     *    then two contiguous quotes would close the segment and
+     *    produce a literal quote; three would close the segment,
+     *    produce a literal quote, and open a new segment. If we
+     *    started outside a quoted segment, then two contiguous
+     *    quotes would open and then close a segment, producing no
+     *    output (but potentially creating a zero-length argument);
+     *    but three quotes would open and close a segment and then
+     *    produce a literal quote.
+     */
+
+    /*
+     * First deal with the simplest of all special cases: if there
+     * aren't any arguments, return 0,NULL,NULL.
+     */
+    while (*cmdline && isspace(*cmdline)) cmdline++;
+    if (!*cmdline) {
+       if (argc) *argc = 0;
+       if (argv) *argv = NULL;
+       if (argstart) *argstart = NULL;
+       return;
+    }
+
+    /*
+     * This will guaranteeably be big enough; we can realloc it
+     * down later.
+     */
+    outputline = snewn(1+strlen(cmdline), char);
+    outputargv = snewn(strlen(cmdline)+1 / 2, char *);
+    outputargstart = snewn(strlen(cmdline)+1 / 2, char *);
+
+    p = cmdline; q = outputline; outputargc = 0;
+
+    while (*p) {
+       int quote;
+
+       /* Skip whitespace searching for start of argument. */
+       while (*p && isspace(*p)) p++;
+       if (!*p) break;
+
+       /* We have an argument; start it. */
+       outputargv[outputargc] = q;
+       outputargstart[outputargc] = p;
+       outputargc++;
+       quote = 0;
+
+       /* Copy data into the argument until it's finished. */
+       while (*p) {
+           if (!quote && isspace(*p))
+               break;                 /* argument is finished */
+
+           if (*p == '"' || *p == '\\') {
+               /*
+                * We have a sequence of zero or more backslashes
+                * followed by a sequence of zero or more quotes.
+                * Count up how many of each, and then deal with
+                * them as appropriate.
+                */
+               int i, slashes = 0, quotes = 0;
+               while (*p == '\\') slashes++, p++;
+               while (*p == '"') quotes++, p++;
+
+               if (!quotes) {
+                   /*
+                    * Special case: if there are no quotes,
+                    * slashes are not special at all, so just copy
+                    * n slashes to the output string.
+                    */
+                   while (slashes--) *q++ = '\\';
+               } else {
+                   /* Slashes annihilate in pairs. */
+                   while (slashes >= 2) slashes -= 2, *q++ = '\\';
+
+                   /* One remaining slash takes out the first quote. */
+                   if (slashes) quotes--, *q++ = '"';
+
+                   if (quotes > 0) {
+                       /* Outside a quote segment, a quote starts one. */
+                       if (!quote) quotes--, quote = 1;
+
+                       /* Now we produce (n+1)/3 literal quotes... */
+                       for (i = 3; i <= quotes+1; i += 3) *q++ = '"';
+
+                       /* ... and end in a quote segment iff 3 divides n. */
+                       quote = (quotes % 3 == 0);
+                   }
+               }
+           } else {
+               *q++ = *p++;
+           }
+       }
+
+       /* At the end of an argument, just append a trailing NUL. */
+       *q++ = '\0';
+    }
+
+    outputargv = sresize(outputargv, outputargc, char *);
+    outputargstart = sresize(outputargstart, outputargc, char *);
+
+    if (argc) *argc = outputargc;
+    if (argv) *argv = outputargv; else sfree(outputargv);
+    if (argstart) *argstart = outputargstart; else sfree(outputargstart);
+}
+
 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
 {
     MSG msg;
     char *error;
+    const game *gg;
+    frontend *fe;
+    midend *me;
+    int argc;
+    char **argv;
+
+    split_into_argv(cmdline, &argc, &argv, NULL);
 
 #ifdef _WIN32_WCE
-    MultiByteToWideChar (CP_ACP, 0, thegame.name, -1, wGameName, 256);
+    MultiByteToWideChar (CP_ACP, 0, CLASSNAME, -1, wClassName, 256);
     if (FindPreviousInstance ())
         return 0;
 #endif
@@ -3155,9 +3687,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 +3700,61 @@ 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];
+    if (argc > 0) {
+        int i;
+        for (i = 0; i < gamecount; i++) {
+           const char *p = gamelist[i]->name;
+           char *q = argv[0];
+           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];
+                --argc;
+                ++argv;
+                break;
+            }
+        }
+    }
+#else
+    gg = &thegame;
+#endif
+
+    fe = frontend_new(inst);
+    me = midend_for_new_game(fe, gg, argc > 0 ? argv[0] : NULL,
+                             TRUE, TRUE, &error);
+    if (!me) {
        char buf[128];
-       sprintf(buf, "%.100s Error", thegame.name);
+#ifdef COMBINED
+       sprintf(buf, "Puzzles Error");
+#else
+       sprintf(buf, "%.100s Error", gg->name);
+#endif
        MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
+        sfree(error);
        return 1;
     }
+    fe_set_midend(fe, me);
+    show_window(fe);
 
     while (GetMessage(&msg, NULL, 0, 0)) {
        DispatchMessage(&msg);
     }
 
+    DestroyWindow(fe->hwnd);
     cleanup_help();
 
     return msg.wParam;
 }
+/* vim: set shiftwidth=4 tabstop=8: */