2 * windows.c: Windows front end for my puzzle collection.
9 #endif /* NO_HTMLHELP */
30 #define IDM_NEW 0x0010
31 #define IDM_RESTART 0x0020
32 #define IDM_UNDO 0x0030
33 #define IDM_REDO 0x0040
34 #define IDM_COPY 0x0050
35 #define IDM_SOLVE 0x0060
36 #define IDM_QUIT 0x0070
37 #define IDM_CONFIG 0x0080
38 #define IDM_DESC 0x0090
39 #define IDM_SEED 0x00A0
40 #define IDM_HELPC 0x00B0
41 #define IDM_GAMEHELP 0x00C0
42 #define IDM_ABOUT 0x00D0
43 #define IDM_SAVE 0x00E0
44 #define IDM_LOAD 0x00F0
45 #define IDM_PRINT 0x0100
46 #define IDM_PRESETS 0x0110
47 #define IDM_GAMES 0x0300
49 #define IDM_KEYEMUL 0x0400
51 #define HELP_FILE_NAME "puzzles.hlp"
52 #define HELP_CNT_NAME "puzzles.cnt"
54 #define CHM_FILE_NAME "puzzles.chm"
55 #endif /* NO_HTMLHELP */
58 typedef HWND (CALLBACK *htmlhelp_t)(HWND, LPCSTR, UINT, DWORD);
59 static htmlhelp_t htmlhelp;
60 static HINSTANCE hh_dll;
61 #endif /* NO_HTMLHELP */
62 enum { NONE, HLP, CHM } help_type;
64 int help_has_contents;
67 #define FILENAME_MAX (260)
71 #define HGDI_ERROR ((HANDLE)GDI_ERROR)
75 #define CLASSNAME "Puzzles"
77 #define CLASSNAME thegame.name
83 * Wrapper implementations of functions not supplied by the
87 #define SHGetSubMenu(hWndMB,ID_MENU) (HMENU)SendMessage((hWndMB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)ID_MENU)
91 int MessageBox(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType)
96 MultiByteToWideChar (CP_ACP, 0, lpText, -1, wText, 2048);
97 MultiByteToWideChar (CP_ACP, 0, lpCaption, -1, wCaption, 2048);
99 return MessageBoxW (hWnd, wText, wCaption, uType);
102 BOOL SetDlgItemTextA(HWND hDlg, int nIDDlgItem, LPCSTR lpString)
106 MultiByteToWideChar (CP_ACP, 0, lpString, -1, wText, 256);
107 return SetDlgItemTextW(hDlg, nIDDlgItem, wText);
110 LPCSTR getenv(LPCSTR buf)
115 BOOL GetKeyboardState(PBYTE pb)
120 static TCHAR wClassName[256], wGameName[256];
125 static FILE *debug_fp = NULL;
126 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
127 static int debug_got_console = 0;
129 void dputs(char *buf)
133 if (!debug_got_console) {
134 if (AllocConsole()) {
135 debug_got_console = 1;
136 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
140 debug_fp = fopen("debug.log", "w");
143 if (debug_hdl != INVALID_HANDLE_VALUE) {
144 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
147 fputs(buf, debug_fp);
150 OutputDebugString(buf);
153 void debug_printf(char *fmt, ...)
157 static int debugging = -1;
160 debugging = getenv("DEBUG_PUZZLES") ? 1 : 0;
164 _vsnprintf(buf, 4095, fmt, ap);
172 #define WINFLAGS (WS_OVERLAPPEDWINDOW &~ \
173 (WS_MAXIMIZEBOX | WS_OVERLAPPED))
175 #define WINFLAGS (WS_CAPTION | WS_SYSMENU)
178 static void new_game_size(frontend *fe, float scale);
196 enum { CFG_PRINT = CFG_FRONTEND_SPECIFIC };
198 struct preset_menuitemref {
206 HWND hwnd, statusbar, cfgbox;
208 HWND numpad; /* window handle for the numeric pad */
211 HBITMAP bitmap, prevbm;
212 RECT bitmapPosition; /* game bitmap position within game window */
218 HMENU gamemenu, typemenu;
220 DWORD timer_last_tickcount;
221 struct preset_menu *preset_menu;
222 struct preset_menuitemref *preset_menuitems;
223 int n_preset_menuitems;
225 int nfonts, fontsize;
227 struct cfg_aux *cfgaux;
228 int cfg_which, dlg_done;
233 enum { DRAWING, PRINTING, NOTHING } drawstatus;
235 int printcount, printw, printh, printsolns, printcurr, printcolour;
237 int printoffsetx, printoffsety;
238 float printpixelscale;
240 int linewidth, linedotted;
246 void frontend_free(frontend *fe)
258 static void update_type_menu_tick(frontend *fe);
259 static void update_copy_menu_greying(frontend *fe);
261 void fatal(char *fmt, ...)
267 vsprintf(buf, fmt, ap);
270 MessageBox(NULL, buf, "Fatal error", MB_ICONEXCLAMATION | MB_OK);
275 char *geterrstr(void)
278 DWORD dw = GetLastError();
282 FORMAT_MESSAGE_ALLOCATE_BUFFER |
283 FORMAT_MESSAGE_FROM_SYSTEM,
286 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
290 ret = dupstr(lpMsgBuf);
297 void get_random_seed(void **randseed, int *randseedsize)
299 SYSTEMTIME *st = snew(SYSTEMTIME);
303 *randseed = (void *)st;
304 *randseedsize = sizeof(SYSTEMTIME);
307 static void win_status_bar(void *handle, char *text)
312 frontend *fe = (frontend *)handle;
315 MultiByteToWideChar (CP_ACP, 0, text, -1, wText, 255);
316 SendMessage(fe->statusbar, SB_SETTEXT,
317 (WPARAM) 255 | SBT_NOBORDERS,
320 SetWindowText(fe->statusbar, text);
324 static blitter *win_blitter_new(void *handle, int w, int h)
326 blitter *bl = snew(blitter);
328 memset(bl, 0, sizeof(blitter));
336 static void win_blitter_free(void *handle, blitter *bl)
338 if (bl->bitmap) DeleteObject(bl->bitmap);
342 static void blitter_mkbitmap(frontend *fe, blitter *bl)
344 HDC hdc = GetDC(fe->hwnd);
345 bl->bitmap = CreateCompatibleBitmap(hdc, bl->w, bl->h);
346 ReleaseDC(fe->hwnd, hdc);
349 /* BitBlt(dstDC, dstX, dstY, dstW, dstH, srcDC, srcX, srcY, dType) */
351 static void win_blitter_save(void *handle, blitter *bl, int x, int y)
353 frontend *fe = (frontend *)handle;
354 HDC hdc_win, hdc_blit;
357 assert(fe->drawstatus == DRAWING);
359 if (!bl->bitmap) blitter_mkbitmap(fe, bl);
361 bl->x = x; bl->y = y;
363 hdc_win = GetDC(fe->hwnd);
364 hdc_blit = CreateCompatibleDC(hdc_win);
365 if (!hdc_blit) fatal("hdc_blit failed: 0x%x", GetLastError());
367 prev_blit = SelectObject(hdc_blit, bl->bitmap);
368 if (prev_blit == NULL || prev_blit == HGDI_ERROR)
369 fatal("SelectObject for hdc_main failed: 0x%x", GetLastError());
371 if (!BitBlt(hdc_blit, 0, 0, bl->w, bl->h,
372 fe->hdc, x, y, SRCCOPY))
373 fatal("BitBlt failed: 0x%x", GetLastError());
375 SelectObject(hdc_blit, prev_blit);
377 ReleaseDC(fe->hwnd, hdc_win);
380 static void win_blitter_load(void *handle, blitter *bl, int x, int y)
382 frontend *fe = (frontend *)handle;
383 HDC hdc_win, hdc_blit;
386 assert(fe->drawstatus == DRAWING);
388 assert(bl->bitmap); /* we should always have saved before loading */
390 if (x == BLITTER_FROMSAVED) x = bl->x;
391 if (y == BLITTER_FROMSAVED) y = bl->y;
393 hdc_win = GetDC(fe->hwnd);
394 hdc_blit = CreateCompatibleDC(hdc_win);
396 prev_blit = SelectObject(hdc_blit, bl->bitmap);
398 BitBlt(fe->hdc, x, y, bl->w, bl->h,
399 hdc_blit, 0, 0, SRCCOPY);
401 SelectObject(hdc_blit, prev_blit);
403 ReleaseDC(fe->hwnd, hdc_win);
406 void frontend_default_colour(frontend *fe, float *output)
408 DWORD c = GetSysColor(COLOR_MENU); /* ick */
410 output[0] = (float)(GetRValue(c) / 255.0);
411 output[1] = (float)(GetGValue(c) / 255.0);
412 output[2] = (float)(GetBValue(c) / 255.0);
415 static POINT win_transform_point(frontend *fe, int x, int y)
419 assert(fe->drawstatus != NOTHING);
421 if (fe->drawstatus == PRINTING) {
422 ret.x = (int)(fe->printoffsetx + fe->printpixelscale * x);
423 ret.y = (int)(fe->printoffsety + fe->printpixelscale * y);
432 static void win_text_colour(frontend *fe, int colour)
434 assert(fe->drawstatus != NOTHING);
436 if (fe->drawstatus == PRINTING) {
439 print_get_colour(fe->dr, colour, fe->printcolour, &hatch, &r, &g, &b);
442 * Displaying text in hatched colours is not permitted.
446 SetTextColor(fe->hdc, RGB(r * 255, g * 255, b * 255));
448 SetTextColor(fe->hdc, fe->colours[colour]);
452 static void win_set_brush(frontend *fe, int colour)
455 assert(fe->drawstatus != NOTHING);
457 if (fe->drawstatus == PRINTING) {
460 print_get_colour(fe->dr, colour, fe->printcolour, &hatch, &r, &g, &b);
463 br = CreateSolidBrush(RGB(r * 255, g * 255, b * 255));
467 * This is only ever required during printing, and the
468 * PocketPC port doesn't support printing.
470 fatal("CreateHatchBrush not supported");
472 br = CreateHatchBrush(hatch == HATCH_BACKSLASH ? HS_FDIAGONAL :
473 hatch == HATCH_SLASH ? HS_BDIAGONAL :
474 hatch == HATCH_HORIZ ? HS_HORIZONTAL :
475 hatch == HATCH_VERT ? HS_VERTICAL :
476 hatch == HATCH_PLUS ? HS_CROSS :
477 /* hatch == HATCH_X ? */ HS_DIAGCROSS,
482 br = fe->brushes[colour];
484 fe->oldbr = SelectObject(fe->hdc, br);
487 static void win_reset_brush(frontend *fe)
491 assert(fe->drawstatus != NOTHING);
493 br = SelectObject(fe->hdc, fe->oldbr);
494 if (fe->drawstatus == PRINTING)
498 static void win_set_pen(frontend *fe, int colour, int thin)
501 assert(fe->drawstatus != NOTHING);
503 if (fe->drawstatus == PRINTING) {
506 int width = thin ? 0 : fe->linewidth;
511 print_get_colour(fe->dr, colour, fe->printcolour, &hatch, &r, &g, &b);
513 * Stroking in hatched colours is not permitted.
516 pen = CreatePen(fe->linedotted ? PS_DOT : PS_SOLID,
517 width, RGB(r * 255, g * 255, b * 255));
519 pen = fe->pens[colour];
521 fe->oldpen = SelectObject(fe->hdc, pen);
524 static void win_reset_pen(frontend *fe)
528 assert(fe->drawstatus != NOTHING);
530 pen = SelectObject(fe->hdc, fe->oldpen);
531 if (fe->drawstatus == PRINTING)
535 static void win_clip(void *handle, int x, int y, int w, int h)
537 frontend *fe = (frontend *)handle;
540 if (fe->drawstatus == NOTHING)
543 p = win_transform_point(fe, x, y);
544 q = win_transform_point(fe, x+w, y+h);
545 IntersectClipRect(fe->hdc, p.x, p.y, q.x, q.y);
548 static void win_unclip(void *handle)
550 frontend *fe = (frontend *)handle;
552 if (fe->drawstatus == NOTHING)
555 SelectClipRgn(fe->hdc, NULL);
558 static void win_draw_text(void *handle, int x, int y, int fonttype,
559 int fontsize, int align, int colour, char *text)
561 frontend *fe = (frontend *)handle;
566 if (fe->drawstatus == NOTHING)
569 if (fe->drawstatus == PRINTING)
570 fontsize = (int)(fontsize * fe->printpixelscale);
572 xy = win_transform_point(fe, x, y);
575 * Find or create the font.
577 for (i = fe->fontstart; i < fe->nfonts; i++)
578 if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
581 if (i == fe->nfonts) {
582 if (fe->fontsize <= fe->nfonts) {
583 fe->fontsize = fe->nfonts + 10;
584 fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
589 fe->fonts[i].type = fonttype;
590 fe->fonts[i].size = fontsize;
592 memset (&lf, 0, sizeof(LOGFONT));
593 lf.lfHeight = -fontsize;
594 lf.lfWeight = (fe->drawstatus == PRINTING ? 0 : FW_BOLD);
595 lf.lfCharSet = DEFAULT_CHARSET;
596 lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
597 lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
598 lf.lfQuality = DEFAULT_QUALITY;
599 lf.lfPitchAndFamily = (fonttype == FONT_FIXED ?
600 FIXED_PITCH | FF_DONTCARE :
601 VARIABLE_PITCH | FF_SWISS);
603 wcscpy(lf.lfFaceName, TEXT("Tahoma"));
606 fe->fonts[i].font = CreateFontIndirect(&lf);
610 * Position and draw the text.
617 MultiByteToWideChar (CP_UTF8, 0, text, -1, wText, 256);
619 oldfont = SelectObject(fe->hdc, fe->fonts[i].font);
620 if (GetTextMetrics(fe->hdc, &tm)) {
621 if (align & ALIGN_VCENTRE)
622 xy.y -= (tm.tmAscent+tm.tmDescent)/2;
626 if (GetTextExtentPoint32W(fe->hdc, wText, wcslen(wText), &size))
628 if (align & ALIGN_HCENTRE)
630 else if (align & ALIGN_HRIGHT)
633 SetBkMode(fe->hdc, TRANSPARENT);
634 win_text_colour(fe, colour);
635 ExtTextOutW(fe->hdc, xy.x, xy.y, 0, NULL, wText, wcslen(wText), NULL);
636 SelectObject(fe->hdc, oldfont);
640 static void win_draw_rect(void *handle, int x, int y, int w, int h, int colour)
642 frontend *fe = (frontend *)handle;
645 if (fe->drawstatus == NOTHING)
648 if (fe->drawstatus == DRAWING && w == 1 && h == 1) {
650 * Rectangle() appears to get uppity if asked to draw a 1x1
651 * rectangle, presumably on the grounds that that's beneath
652 * its dignity and you ought to be using SetPixel instead.
655 SetPixel(fe->hdc, x, y, fe->colours[colour]);
657 win_set_brush(fe, colour);
658 win_set_pen(fe, colour, TRUE);
659 p = win_transform_point(fe, x, y);
660 q = win_transform_point(fe, x+w, y+h);
661 Rectangle(fe->hdc, p.x, p.y, q.x, q.y);
667 static void win_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour)
669 frontend *fe = (frontend *)handle;
672 if (fe->drawstatus == NOTHING)
675 win_set_pen(fe, colour, FALSE);
676 pp[0] = win_transform_point(fe, x1, y1);
677 pp[1] = win_transform_point(fe, x2, y2);
678 Polyline(fe->hdc, pp, 2);
679 if (fe->drawstatus == DRAWING)
680 SetPixel(fe->hdc, pp[1].x, pp[1].y, fe->colours[colour]);
684 static void win_draw_circle(void *handle, int cx, int cy, int radius,
685 int fillcolour, int outlinecolour)
687 frontend *fe = (frontend *)handle;
690 assert(outlinecolour >= 0);
692 if (fe->drawstatus == NOTHING)
696 win_set_brush(fe, fillcolour);
698 fe->oldbr = SelectObject(fe->hdc, GetStockObject(NULL_BRUSH));
700 win_set_pen(fe, outlinecolour, FALSE);
701 p = win_transform_point(fe, cx - radius, cy - radius);
702 q = win_transform_point(fe, cx + radius, cy + radius);
703 Ellipse(fe->hdc, p.x, p.y, q.x+1, q.y+1);
708 static void win_draw_polygon(void *handle, int *coords, int npoints,
709 int fillcolour, int outlinecolour)
711 frontend *fe = (frontend *)handle;
715 if (fe->drawstatus == NOTHING)
718 pts = snewn(npoints+1, POINT);
720 for (i = 0; i <= npoints; i++) {
721 int j = (i < npoints ? i : 0);
722 pts[i] = win_transform_point(fe, coords[j*2], coords[j*2+1]);
725 assert(outlinecolour >= 0);
727 if (fillcolour >= 0) {
728 win_set_brush(fe, fillcolour);
729 win_set_pen(fe, outlinecolour, FALSE);
730 Polygon(fe->hdc, pts, npoints);
734 win_set_pen(fe, outlinecolour, FALSE);
735 Polyline(fe->hdc, pts, npoints+1);
742 static void win_start_draw(void *handle)
744 frontend *fe = (frontend *)handle;
747 assert(fe->drawstatus == NOTHING);
749 hdc_win = GetDC(fe->hwnd);
750 fe->hdc = CreateCompatibleDC(hdc_win);
751 fe->prevbm = SelectObject(fe->hdc, fe->bitmap);
752 ReleaseDC(fe->hwnd, hdc_win);
755 SetMapMode(fe->hdc, MM_TEXT);
757 fe->drawstatus = DRAWING;
760 static void win_draw_update(void *handle, int x, int y, int w, int h)
762 frontend *fe = (frontend *)handle;
765 if (fe->drawstatus != DRAWING)
773 OffsetRect(&r, fe->bitmapPosition.left, fe->bitmapPosition.top);
774 InvalidateRect(fe->hwnd, &r, FALSE);
777 static void win_end_draw(void *handle)
779 frontend *fe = (frontend *)handle;
780 assert(fe->drawstatus == DRAWING);
781 SelectObject(fe->hdc, fe->prevbm);
784 DeleteObject(fe->clip);
787 fe->drawstatus = NOTHING;
790 static void win_line_width(void *handle, float width)
792 frontend *fe = (frontend *)handle;
794 assert(fe->drawstatus != DRAWING);
795 if (fe->drawstatus == NOTHING)
798 fe->linewidth = (int)(width * fe->printpixelscale);
801 static void win_line_dotted(void *handle, int dotted)
803 frontend *fe = (frontend *)handle;
805 assert(fe->drawstatus != DRAWING);
806 if (fe->drawstatus == NOTHING)
809 fe->linedotted = dotted;
812 static void win_begin_doc(void *handle, int pages)
814 frontend *fe = (frontend *)handle;
816 assert(fe->drawstatus != DRAWING);
817 if (fe->drawstatus == NOTHING)
820 if (StartDoc(fe->hdc, &fe->di) <= 0) {
821 char *e = geterrstr();
822 MessageBox(fe->hwnd, e, "Error starting to print",
823 MB_ICONERROR | MB_OK);
825 fe->drawstatus = NOTHING;
829 * Push a marker on the font stack so that we won't use the
830 * same fonts for printing and drawing. (This is because
831 * drawing seems to look generally better in bold, but printing
832 * is better not in bold.)
834 fe->fontstart = fe->nfonts;
837 static void win_begin_page(void *handle, int number)
839 frontend *fe = (frontend *)handle;
841 assert(fe->drawstatus != DRAWING);
842 if (fe->drawstatus == NOTHING)
845 if (StartPage(fe->hdc) <= 0) {
846 char *e = geterrstr();
847 MessageBox(fe->hwnd, e, "Error starting a page",
848 MB_ICONERROR | MB_OK);
850 fe->drawstatus = NOTHING;
854 static void win_begin_puzzle(void *handle, float xm, float xc,
855 float ym, float yc, int pw, int ph, float wmm)
857 frontend *fe = (frontend *)handle;
858 int ppw, pph, pox, poy;
859 float mmpw, mmph, mmox, mmoy;
862 assert(fe->drawstatus != DRAWING);
863 if (fe->drawstatus == NOTHING)
866 ppw = GetDeviceCaps(fe->hdc, HORZRES);
867 pph = GetDeviceCaps(fe->hdc, VERTRES);
868 mmpw = (float)GetDeviceCaps(fe->hdc, HORZSIZE);
869 mmph = (float)GetDeviceCaps(fe->hdc, VERTSIZE);
872 * Compute the puzzle's position on the logical page.
874 mmox = xm * mmpw + xc;
875 mmoy = ym * mmph + yc;
878 * Work out what that comes to in pixels.
880 pox = (int)(mmox * (float)ppw / mmpw);
881 poy = (int)(mmoy * (float)pph / mmph);
884 * And determine the scale.
886 * I need a scale such that the maximum puzzle-coordinate
887 * extent of the rectangle (pw * scale) is equal to the pixel
888 * equivalent of the puzzle's millimetre width (wmm * ppw /
891 scale = (wmm * ppw) / (mmpw * pw);
894 * Now store pox, poy and scale for use in the main drawing
897 fe->printoffsetx = pox;
898 fe->printoffsety = poy;
899 fe->printpixelscale = scale;
902 fe->linedotted = FALSE;
905 static void win_end_puzzle(void *handle)
907 /* Nothing needs to be done here. */
910 static void win_end_page(void *handle, int number)
912 frontend *fe = (frontend *)handle;
914 assert(fe->drawstatus != DRAWING);
916 if (fe->drawstatus == NOTHING)
919 if (EndPage(fe->hdc) <= 0) {
920 char *e = geterrstr();
921 MessageBox(fe->hwnd, e, "Error finishing a page",
922 MB_ICONERROR | MB_OK);
924 fe->drawstatus = NOTHING;
928 static void win_end_doc(void *handle)
930 frontend *fe = (frontend *)handle;
932 assert(fe->drawstatus != DRAWING);
935 * Free all the fonts created since we began printing.
937 while (fe->nfonts > fe->fontstart) {
939 DeleteObject(fe->fonts[fe->nfonts].font);
944 * The MSDN web site sample code doesn't bother to call EndDoc
945 * if an error occurs half way through printing. I expect doing
946 * so would cause the erroneous document to actually be
947 * printed, or something equally undesirable.
949 if (fe->drawstatus == NOTHING)
952 if (EndDoc(fe->hdc) <= 0) {
953 char *e = geterrstr();
954 MessageBox(fe->hwnd, e, "Error finishing printing",
955 MB_ICONERROR | MB_OK);
957 fe->drawstatus = NOTHING;
961 char *win_text_fallback(void *handle, const char *const *strings, int nstrings)
964 * We assume Windows can cope with any UTF-8 likely to be
965 * emitted by a puzzle.
967 return dupstr(strings[0]);
970 const struct drawing_api win_drawing = {
997 void print(frontend *fe)
1003 midend *nme = NULL; /* non-interactive midend for bulk puzzle generation */
1008 * Create our document structure and fill it up with puzzles.
1010 doc = document_new(fe->printw, fe->printh, fe->printscale / 100.0F);
1011 for (i = 0; i < fe->printcount; i++) {
1012 if (i == 0 && fe->printcurr) {
1013 err = midend_print_puzzle(fe->me, doc, fe->printsolns);
1016 game_params *params;
1018 nme = midend_new(NULL, fe->game, NULL, NULL);
1021 * Set the non-interactive mid-end to have the same
1022 * parameters as the standard one.
1024 params = midend_get_params(fe->me);
1025 midend_set_params(nme, params);
1026 fe->game->free_params(params);
1029 midend_new_game(nme);
1030 err = midend_print_puzzle(nme, doc, fe->printsolns);
1039 MessageBox(fe->hwnd, err, "Error preparing puzzles for printing",
1040 MB_ICONERROR | MB_OK);
1045 memset(&pd, 0, sizeof(pd));
1046 pd.lStructSize = sizeof(pd);
1047 pd.hwndOwner = fe->hwnd;
1049 pd.hDevNames = NULL;
1050 pd.Flags = PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC |
1051 PD_NOPAGENUMS | PD_NOSELECTION;
1053 pd.nFromPage = pd.nToPage = 0xFFFF;
1054 pd.nMinPage = pd.nMaxPage = 1;
1056 if (!PrintDlg(&pd)) {
1062 * Now pd.hDC is a device context for the printer.
1066 * FIXME: IWBNI we put up an Abort box here.
1069 memset(&fe->di, 0, sizeof(fe->di));
1070 fe->di.cbSize = sizeof(fe->di);
1071 sprintf(doctitle, "Printed puzzles from %s (from Simon Tatham's"
1072 " Portable Puzzle Collection)", fe->game->name);
1073 fe->di.lpszDocName = doctitle;
1074 fe->di.lpszOutput = NULL;
1075 fe->di.lpszDatatype = NULL;
1078 fe->drawstatus = PRINTING;
1081 fe->dr = drawing_new(&win_drawing, NULL, fe);
1082 document_print(doc, fe->dr);
1083 drawing_free(fe->dr);
1086 fe->drawstatus = NOTHING;
1093 void deactivate_timer(frontend *fe)
1096 return; /* for non-interactive midend */
1097 if (fe->hwnd) KillTimer(fe->hwnd, fe->timer);
1101 void activate_timer(frontend *fe)
1104 return; /* for non-interactive midend */
1106 fe->timer = SetTimer(fe->hwnd, 1, 20, NULL);
1107 fe->timer_last_tickcount = GetTickCount();
1111 void write_clip(HWND hwnd, char *data)
1119 * Windows expects CRLF in the clipboard, so we must convert
1120 * any \n that has come out of the puzzle backend.
1123 for (i = 0; data[i]; i++) {
1124 if (data[i] == '\n')
1128 data2 = snewn(len+1, char);
1130 for (i = 0; data[i]; i++) {
1131 if (data[i] == '\n')
1133 data2[j++] = data[i];
1138 clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
1143 lock = GlobalLock(clipdata);
1145 GlobalFree(clipdata);
1149 memcpy(lock, data2, len);
1150 ((unsigned char *) lock)[len] = 0;
1151 GlobalUnlock(clipdata);
1153 if (OpenClipboard(hwnd)) {
1155 SetClipboardData(CF_TEXT, clipdata);
1158 GlobalFree(clipdata);
1164 * Set up Help and see if we can find a help file.
1166 static void init_help(void)
1169 char b[2048], *p, *q, *r;
1173 * Find the executable file path, so we can look alongside
1174 * it for help files. Trim the filename off the end.
1176 GetModuleFileName(NULL, b, sizeof(b) - 1);
1178 p = strrchr(b, '\\');
1179 if (p && p >= r) r = p+1;
1180 q = strrchr(b, ':');
1181 if (q && q >= r) r = q+1;
1185 * Try HTML Help first.
1187 strcpy(r, CHM_FILE_NAME);
1188 if ( (fp = fopen(b, "r")) != NULL) {
1192 * We have a .CHM. See if we can use it.
1194 hh_dll = LoadLibrary("hhctrl.ocx");
1196 htmlhelp = (htmlhelp_t)GetProcAddress(hh_dll, "HtmlHelpA");
1198 FreeLibrary(hh_dll);
1201 help_path = dupstr(b);
1206 #endif /* NO_HTMLHELP */
1209 * Now try old-style .HLP.
1211 strcpy(r, HELP_FILE_NAME);
1212 if ( (fp = fopen(b, "r")) != NULL) {
1215 help_path = dupstr(b);
1219 * See if there's a .CNT file alongside it.
1221 strcpy(r, HELP_CNT_NAME);
1222 if ( (fp = fopen(b, "r")) != NULL) {
1224 help_has_contents = TRUE;
1226 help_has_contents = FALSE;
1231 help_type = NONE; /* didn't find any */
1240 static void start_help(frontend *fe, const char *topic)
1245 switch (help_type) {
1249 str = snewn(10+strlen(topic), char);
1250 sprintf(str, "JI(`',`%s')", topic);
1252 } else if (help_has_contents) {
1255 cmd = HELP_CONTENTS;
1257 WinHelp(fe->hwnd, help_path, cmd, (DWORD)str);
1258 fe->help_running = TRUE;
1265 str = snewn(20 + strlen(topic) + strlen(help_path), char);
1266 sprintf(str, "%s::/%s.html>main", help_path, topic);
1268 str = dupstr(help_path);
1270 htmlhelp(fe->hwnd, str, HH_DISPLAY_TOPIC, 0);
1271 fe->help_running = TRUE;
1273 #endif /* NO_HTMLHELP */
1275 assert(!"This shouldn't happen");
1283 * Stop Help on window cleanup.
1285 static void stop_help(frontend *fe)
1287 if (fe->help_running) {
1288 switch (help_type) {
1290 WinHelp(fe->hwnd, help_path, HELP_QUIT, 0);
1295 htmlhelp(NULL, NULL, HH_CLOSE_ALL, 0);
1297 #endif /* NO_HTMLHELP */
1299 assert(!"This shouldn't happen");
1302 fe->help_running = FALSE;
1309 * Terminate Help on process exit.
1311 static void cleanup_help(void)
1313 /* Nothing to do currently.
1314 * (If we were running HTML Help single-threaded, this is where we'd
1315 * call HH_UNINITIALIZE.) */
1318 static int get_statusbar_height(frontend *fe)
1321 if (fe->statusbar) {
1323 GetWindowRect(fe->statusbar, &sr);
1324 sy = sr.bottom - sr.top;
1331 static void adjust_statusbar(frontend *fe, RECT *r)
1335 if (!fe->statusbar) return;
1337 sy = get_statusbar_height(fe);
1339 SetWindowPos(fe->statusbar, NULL, 0, r->bottom-r->top-sy, r->right-r->left,
1344 static void get_menu_size(HWND wh, RECT *r)
1346 HMENU bar = GetMenu(wh);
1350 SetRect(r, 0, 0, 0, 0);
1351 for (i = 0; i < GetMenuItemCount(bar); i++) {
1352 GetMenuItemRect(wh, bar, i, &rect);
1353 UnionRect(r, r, &rect);
1358 * Given a proposed new puzzle size (cx,cy), work out the actual
1359 * puzzle size that would be (px,py) and the window size including
1360 * furniture (wx,wy).
1363 static int check_window_resize(frontend *fe, int cx, int cy,
1368 int x, y, sy = get_statusbar_height(fe), changed = 0;
1370 /* disallow making window thinner than menu bar */
1371 x = max(cx, fe->xmin);
1372 y = max(cy - sy, fe->ymin);
1375 * See if we actually got the window size we wanted, and adjust
1376 * the puzzle size if not.
1378 midend_size(fe->me, &x, &y, TRUE);
1379 if (x != cx || y != cy) {
1381 * Resize the window, now we know what size we _really_
1387 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1388 *wx = r.right - r.left;
1389 *wy = r.bottom - r.top;
1397 (float)midend_tilesize(fe->me) / (float)fe->game->preferred_tilesize;
1403 * Given the current window size, make sure it's sane for the
1404 * current puzzle and resize if necessary.
1407 static void check_window_size(frontend *fe, int *px, int *py)
1412 GetClientRect(fe->hwnd, &r);
1413 cx = r.right - r.left;
1414 cy = r.bottom - r.top;
1416 if (check_window_resize(fe, cx, cy, px, py, &wx, &wy)) {
1418 SetWindowPos(fe->hwnd, NULL, 0, 0, wx, wy,
1419 SWP_NOMOVE | SWP_NOZORDER);
1424 GetClientRect(fe->hwnd, &r);
1425 adjust_statusbar(fe, &r);
1428 static void get_max_puzzle_size(frontend *fe, int *x, int *y)
1432 if (SystemParametersInfo(SPI_GETWORKAREA, 0, &sr, FALSE)) {
1433 *x = sr.right - sr.left;
1434 *y = sr.bottom - sr.top;
1439 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1440 *x -= r.right - r.left - 100;
1441 *y -= r.bottom - r.top - 100;
1446 if (fe->statusbar != NULL) {
1447 GetWindowRect(fe->statusbar, &sr);
1448 *y -= sr.bottom - sr.top;
1453 /* Toolbar buttons on the numeric pad */
1454 static TBBUTTON tbNumpadButtons[] =
1456 {0, IDM_KEYEMUL + '1', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1457 {1, IDM_KEYEMUL + '2', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1458 {2, IDM_KEYEMUL + '3', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1459 {3, IDM_KEYEMUL + '4', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1460 {4, IDM_KEYEMUL + '5', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1461 {5, IDM_KEYEMUL + '6', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1462 {6, IDM_KEYEMUL + '7', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1463 {7, IDM_KEYEMUL + '8', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1464 {8, IDM_KEYEMUL + '9', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1465 {9, IDM_KEYEMUL + ' ', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1}
1470 * Allocate a new frontend structure and create its main window.
1472 static frontend *frontend_new(HINSTANCE inst)
1475 const char *nogame = "Puzzles (no game selected)";
1477 fe = snew(frontend);
1487 fe->help_running = FALSE;
1489 fe->drawstatus = NOTHING;
1494 fe->nfonts = fe->fontsize = 0;
1500 fe->puzz_scale = 1.0;
1503 MultiByteToWideChar (CP_ACP, 0, nogame, -1, wGameName, 256);
1504 fe->hwnd = CreateWindowEx(0, wClassName, wGameName,
1506 CW_USEDEFAULT, CW_USEDEFAULT,
1507 CW_USEDEFAULT, CW_USEDEFAULT,
1508 NULL, NULL, inst, NULL);
1512 RECT rc, rcBar, rcTB, rcClient;
1514 memset (&mbi, 0, sizeof(SHMENUBARINFO));
1515 mbi.cbSize = sizeof(SHMENUBARINFO);
1516 mbi.hwndParent = fe->hwnd;
1517 mbi.nToolBarId = IDR_MENUBAR1;
1518 mbi.hInstRes = inst;
1520 SHCreateMenuBar(&mbi);
1522 GetWindowRect(fe->hwnd, &rc);
1523 GetWindowRect(mbi.hwndMB, &rcBar);
1524 rc.bottom -= rcBar.bottom - rcBar.top;
1525 MoveWindow(fe->hwnd, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, FALSE);
1530 fe->hwnd = CreateWindowEx(0, CLASSNAME, nogame,
1531 WS_OVERLAPPEDWINDOW &~
1533 CW_USEDEFAULT, CW_USEDEFAULT,
1534 CW_USEDEFAULT, CW_USEDEFAULT,
1535 NULL, NULL, inst, NULL);
1537 DWORD lerr = GetLastError();
1538 printf("no window: 0x%x\n", (unsigned)lerr);
1542 fe->gamemenu = NULL;
1543 fe->preset_menu = NULL;
1545 fe->statusbar = NULL;
1548 SetWindowLongPtr(fe->hwnd, GWLP_USERDATA, (LONG_PTR)fe);
1553 static void savefile_write(void *wctx, void *buf, int len)
1555 FILE *fp = (FILE *)wctx;
1556 fwrite(buf, 1, len, fp);
1559 static int savefile_read(void *wctx, void *buf, int len)
1561 FILE *fp = (FILE *)wctx;
1564 ret = fread(buf, 1, len, fp);
1565 return (ret == len);
1569 * Create an appropriate midend structure to go in a puzzle window,
1570 * given a game type and/or a command-line argument.
1572 * 'arg' can be either a game ID string (descriptive, random, or a
1573 * plain set of parameters) or the filename of a save file. The two
1574 * boolean flag arguments indicate which possibilities are
1577 static midend *midend_for_new_game(frontend *fe, const game *cgame,
1578 char *arg, int maybe_game_id,
1579 int maybe_save_file, char **error)
1584 if (me) midend_free(me);
1585 me = midend_new(fe, cgame, &win_drawing, fe);
1586 midend_new_game(me);
1589 char *err_param, *err_load;
1592 * See if arg is a valid filename of a save game file.
1595 if (maybe_save_file && (fp = fopen(arg, "r")) != NULL) {
1596 const game *loadgame;
1600 * Find out what kind of game is stored in the save
1601 * file; if we're going to end up loading that, it
1602 * will have to override our caller's judgment as to
1603 * what game to initialise our midend with.
1606 err_load = identify_game(&id_name, savefile_read, fp);
1609 for (i = 0; i < gamecount; i++)
1610 if (!strcmp(id_name, gamelist[i]->name))
1612 if (i == gamecount) {
1613 err_load = "Save file is for a game not supported by"
1616 loadgame = gamelist[i];
1617 rewind(fp); /* go back to the start for actual load */
1624 if (me) midend_free(me);
1625 me = midend_new(fe, loadgame, &win_drawing, fe);
1626 err_load = midend_deserialise(me, savefile_read, fp);
1629 err_load = "Unable to open file";
1632 if (maybe_game_id && (!maybe_save_file || err_load)) {
1634 * See if arg is a game description.
1636 if (me) midend_free(me);
1637 me = midend_new(fe, cgame, &win_drawing, fe);
1638 err_param = midend_game_id(me, arg);
1640 midend_new_game(me);
1642 if (maybe_save_file) {
1643 *error = snewn(256 + strlen(arg) + strlen(err_param) +
1644 strlen(err_load), char);
1645 sprintf(*error, "Supplied argument \"%s\" is neither a"
1646 " game ID (%s) nor a save file (%s)",
1647 arg, err_param, err_load);
1649 *error = dupstr(err_param);
1655 } else if (err_load) {
1656 *error = dupstr(err_load);
1666 static void populate_preset_menu(frontend *fe,
1667 struct preset_menu *menu, HMENU winmenu)
1670 for (i = 0; i < menu->n_entries; i++) {
1671 struct preset_menu_entry *entry = &menu->entries[i];
1673 UINT flags = MF_ENABLED;
1675 if (entry->params) {
1676 id_or_sub = (UINT_PTR)(IDM_PRESETS + 0x10 * entry->id);
1678 fe->preset_menuitems[entry->id].which_menu = winmenu;
1679 fe->preset_menuitems[entry->id].item_index =
1680 GetMenuItemCount(winmenu);
1682 HMENU winsubmenu = CreateMenu();
1683 id_or_sub = (UINT_PTR)winsubmenu;
1686 populate_preset_menu(fe, entry->submenu, winsubmenu);
1690 * FIXME: we ought to go through and do something with ampersands
1695 AppendMenu(winmenu, flags, id_or_sub, entry->title);
1699 MultiByteToWideChar(CP_ACP, 0, entry->title, -1, wName, 255);
1700 AppendMenu(winmenu, flags, id_or_sub, wName);
1707 * Populate a frontend structure with a new midend structure, and
1708 * create any window furniture that it needs.
1710 * Previously-allocated memory and window furniture will be freed by
1714 static int fe_set_midend(frontend *fe, midend *me)
1719 if (fe->me) midend_free(fe->me);
1721 fe->game = midend_which_game(fe->me);
1727 colours = midend_colours(fe->me, &ncolours);
1729 if (fe->colours) sfree(fe->colours);
1730 if (fe->brushes) sfree(fe->brushes);
1731 if (fe->pens) sfree(fe->pens);
1733 fe->colours = snewn(ncolours, COLORREF);
1734 fe->brushes = snewn(ncolours, HBRUSH);
1735 fe->pens = snewn(ncolours, HPEN);
1737 for (i = 0; i < ncolours; i++) {
1738 fe->colours[i] = RGB(255 * colours[i*3+0],
1739 255 * colours[i*3+1],
1740 255 * colours[i*3+2]);
1741 fe->brushes[i] = CreateSolidBrush(fe->colours[i]);
1742 fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]);
1748 DestroyWindow(fe->statusbar);
1749 if (midend_wants_statusbar(fe->me)) {
1750 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME,
1751 TEXT(DEFAULT_STATUSBAR_TEXT),
1752 WS_CHILD | WS_VISIBLE,
1753 0, 0, 0, 0, /* status bar does these */
1754 NULL, NULL, fe->inst, NULL);
1756 fe->statusbar = NULL;
1758 get_max_puzzle_size(fe, &x, &y);
1759 midend_size(fe->me, &x, &y, FALSE);
1764 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1768 DestroyWindow(fe->numpad);
1769 if (fe->game->flags & REQUIRE_NUMPAD)
1771 fe->numpad = CreateToolbarEx (fe->hwnd,
1772 WS_VISIBLE | WS_CHILD | CCS_NOPARENTALIGN | TBSTYLE_FLAT,
1773 0, 10, fe->inst, IDR_PADTOOLBAR,
1774 tbNumpadButtons, sizeof (tbNumpadButtons) / sizeof (TBBUTTON),
1775 0, 0, 14, 15, sizeof (TBBUTTON));
1776 GetWindowRect(fe->numpad, &rcTB);
1777 GetClientRect(fe->hwnd, &rcClient);
1778 MoveWindow(fe->numpad,
1780 rcClient.bottom - (rcTB.bottom - rcTB.top) - 1,
1782 rcTB.bottom - rcTB.top,
1784 SendMessage(fe->numpad, TB_SETINDENT, (rcClient.right - (10 * 21)) / 2, 0);
1789 MultiByteToWideChar (CP_ACP, 0, fe->game->name, -1, wGameName, 256);
1790 SetWindowText(fe->hwnd, wGameName);
1792 SetWindowText(fe->hwnd, fe->game->name);
1796 DestroyWindow(fe->statusbar);
1797 if (midend_wants_statusbar(fe->me)) {
1799 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, TEXT("ooh"),
1800 WS_CHILD | WS_VISIBLE,
1801 0, 0, 0, 0, /* status bar does these */
1802 fe->hwnd, NULL, fe->inst, NULL);
1804 /* Flat status bar looks better on the Pocket PC */
1805 SendMessage(fe->statusbar, SB_SIMPLE, (WPARAM) TRUE, 0);
1806 SendMessage(fe->statusbar, SB_SETTEXT,
1807 (WPARAM) 255 | SBT_NOBORDERS,
1812 * Now resize the window to take account of the status bar.
1814 GetWindowRect(fe->statusbar, &sr);
1815 GetWindowRect(fe->hwnd, &r);
1817 SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left,
1818 r.bottom - r.top + sr.bottom - sr.top,
1819 SWP_NOMOVE | SWP_NOZORDER);
1822 fe->statusbar = NULL;
1826 HMENU oldmenu = GetMenu(fe->hwnd);
1829 HMENU bar = CreateMenu();
1830 HMENU menu = CreateMenu();
1833 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "&Game");
1835 HMENU menu = SHGetSubMenu(SHFindMenuBar(fe->hwnd), ID_GAME);
1836 DeleteMenu(menu, 0, MF_BYPOSITION);
1838 fe->gamemenu = menu;
1839 AppendMenu(menu, MF_ENABLED, IDM_NEW, TEXT("&New"));
1840 AppendMenu(menu, MF_ENABLED, IDM_RESTART, TEXT("&Restart"));
1842 /* ...here I run out of sensible accelerator characters. */
1843 AppendMenu(menu, MF_ENABLED, IDM_DESC, TEXT("Speci&fic..."));
1844 AppendMenu(menu, MF_ENABLED, IDM_SEED, TEXT("Rando&m Seed..."));
1847 if (!fe->preset_menu) {
1849 fe->preset_menu = midend_get_presets(
1850 fe->me, &fe->n_preset_menuitems);
1851 fe->preset_menuitems = snewn(fe->n_preset_menuitems,
1852 struct preset_menuitemref);
1853 for (i = 0; i < fe->n_preset_menuitems; i++)
1854 fe->preset_menuitems[i].which_menu = NULL;
1856 if (fe->preset_menu->n_entries > 0 || fe->game->can_configure) {
1858 HMENU sub = CreateMenu();
1860 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "&Type");
1862 HMENU sub = SHGetSubMenu(SHFindMenuBar(fe->hwnd), ID_TYPE);
1863 DeleteMenu(sub, 0, MF_BYPOSITION);
1866 populate_preset_menu(fe, fe->preset_menu, sub);
1868 if (fe->game->can_configure) {
1869 AppendMenu(sub, MF_ENABLED, IDM_CONFIG, TEXT("&Custom..."));
1874 fe->typemenu = INVALID_HANDLE_VALUE;
1879 #error Windows CE does not support COMBINED build.
1882 HMENU games = CreateMenu();
1885 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1886 AppendMenu(menu, MF_ENABLED|MF_POPUP, (UINT)games, "&Other");
1887 for (i = 0; i < gamecount; i++) {
1888 if (strcmp(gamelist[i]->name, fe->game->name) != 0) {
1889 /* only include those games that aren't the same as the
1890 * game we're currently playing. */
1891 AppendMenu(games, MF_ENABLED, IDM_GAMES + i, gamelist[i]->name);
1897 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1899 AppendMenu(menu, MF_ENABLED, IDM_LOAD, TEXT("&Load..."));
1900 AppendMenu(menu, MF_ENABLED, IDM_SAVE, TEXT("&Save..."));
1901 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1902 if (fe->game->can_print) {
1903 AppendMenu(menu, MF_ENABLED, IDM_PRINT, TEXT("&Print..."));
1904 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1907 AppendMenu(menu, MF_ENABLED, IDM_UNDO, TEXT("Undo"));
1908 AppendMenu(menu, MF_ENABLED, IDM_REDO, TEXT("Redo"));
1910 if (fe->game->can_format_as_text_ever) {
1911 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1912 AppendMenu(menu, MF_ENABLED, IDM_COPY, TEXT("&Copy"));
1915 if (fe->game->can_solve) {
1916 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1917 AppendMenu(menu, MF_ENABLED, IDM_SOLVE, TEXT("Sol&ve"));
1919 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1921 AppendMenu(menu, MF_ENABLED, IDM_QUIT, TEXT("E&xit"));
1922 menu = CreateMenu();
1923 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, TEXT("&Help"));
1925 AppendMenu(menu, MF_ENABLED, IDM_ABOUT, TEXT("&About"));
1927 if (help_type != NONE) {
1929 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1930 AppendMenu(menu, MF_ENABLED, IDM_HELPC, TEXT("&Contents"));
1931 assert(fe->game->name);
1932 item = snewn(10+strlen(fe->game->name), char); /*ick*/
1933 sprintf(item, "&Help on %s", fe->game->name);
1934 AppendMenu(menu, MF_ENABLED, IDM_GAMEHELP, item);
1937 DestroyMenu(oldmenu);
1938 SetMenu(fe->hwnd, bar);
1939 get_menu_size(fe->hwnd, &menusize);
1940 fe->xmin = (menusize.right - menusize.left) + 25;
1944 if (fe->bitmap) DeleteObject(fe->bitmap);
1946 new_game_size(fe, fe->puzz_scale); /* initialises fe->bitmap */
1951 static void show_window(frontend *fe)
1953 ShowWindow(fe->hwnd, SW_SHOWNORMAL);
1954 SetForegroundWindow(fe->hwnd);
1956 update_type_menu_tick(fe);
1957 update_copy_menu_greying(fe);
1959 midend_redraw(fe->me);
1963 static HFONT dialog_title_font()
1965 static HFONT hf = NULL;
1971 memset (&lf, 0, sizeof(LOGFONT));
1972 lf.lfHeight = -11; /* - ((8 * GetDeviceCaps(hdc, LOGPIXELSY)) / 72) */
1973 lf.lfWeight = FW_BOLD;
1974 wcscpy(lf.lfFaceName, TEXT("Tahoma"));
1976 return hf = CreateFontIndirect(&lf);
1979 static void make_dialog_full_screen(HWND hwnd)
1981 SHINITDLGINFO shidi;
1983 /* Make dialog full screen */
1984 shidi.dwMask = SHIDIM_FLAGS;
1985 shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIZEDLGFULLSCREEN |
1988 SHInitDialog(&shidi);
1992 static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg,
1993 WPARAM wParam, LPARAM lParam)
1995 frontend *fe = (frontend *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
2003 make_dialog_full_screen(hwnd);
2005 sprintf(title, "About %.250s", fe->game->name);
2006 SetDlgItemTextA(hwnd, IDC_ABOUT_CAPTION, title);
2008 SendDlgItemMessage(hwnd, IDC_ABOUT_CAPTION, WM_SETFONT,
2009 (WPARAM) dialog_title_font(), 0);
2011 SetDlgItemTextA(hwnd, IDC_ABOUT_GAME, fe->game->name);
2012 SetDlgItemTextA(hwnd, IDC_ABOUT_VERSION, ver);
2018 if (LOWORD(wParam) == IDOK)
2039 * Wrappers on midend_{get,set}_config, which extend the CFG_*
2040 * enumeration to add CFG_PRINT.
2042 static config_item *frontend_get_config(frontend *fe, int which,
2045 if (which < CFG_FRONTEND_SPECIFIC) {
2046 return midend_get_config(fe->me, which, wintitle);
2047 } else if (which == CFG_PRINT) {
2051 *wintitle = snewn(40 + strlen(fe->game->name), char);
2052 sprintf(*wintitle, "%s print setup", fe->game->name);
2054 ret = snewn(8, config_item);
2058 ret[i].name = "Number of puzzles to print";
2059 ret[i].type = C_STRING;
2060 ret[i].u.string.sval = dupstr("1");
2063 ret[i].name = "Number of puzzles across the page";
2064 ret[i].type = C_STRING;
2065 ret[i].u.string.sval = dupstr("1");
2068 ret[i].name = "Number of puzzles down the page";
2069 ret[i].type = C_STRING;
2070 ret[i].u.string.sval = dupstr("1");
2073 ret[i].name = "Percentage of standard size";
2074 ret[i].type = C_STRING;
2075 ret[i].u.string.sval = dupstr("100.0");
2078 ret[i].name = "Include currently shown puzzle";
2079 ret[i].type = C_BOOLEAN;
2080 ret[i].u.boolean.bval = TRUE;
2083 ret[i].name = "Print solutions";
2084 ret[i].type = C_BOOLEAN;
2085 ret[i].u.boolean.bval = FALSE;
2088 if (fe->game->can_print_in_colour) {
2089 ret[i].name = "Print in colour";
2090 ret[i].type = C_BOOLEAN;
2091 ret[i].u.boolean.bval = FALSE;
2096 ret[i].type = C_END;
2101 assert(!"We should never get here");
2106 static char *frontend_set_config(frontend *fe, int which, config_item *cfg)
2108 if (which < CFG_FRONTEND_SPECIFIC) {
2109 return midend_set_config(fe->me, which, cfg);
2110 } else if (which == CFG_PRINT) {
2111 if ((fe->printcount = atoi(cfg[0].u.string.sval)) <= 0)
2112 return "Number of puzzles to print should be at least one";
2113 if ((fe->printw = atoi(cfg[1].u.string.sval)) <= 0)
2114 return "Number of puzzles across the page should be at least one";
2115 if ((fe->printh = atoi(cfg[2].u.string.sval)) <= 0)
2116 return "Number of puzzles down the page should be at least one";
2117 if ((fe->printscale = (float)atof(cfg[3].u.string.sval)) <= 0)
2118 return "Print size should be positive";
2119 fe->printcurr = cfg[4].u.boolean.bval;
2120 fe->printsolns = cfg[5].u.boolean.bval;
2121 fe->printcolour = fe->game->can_print_in_colour &&
2122 cfg[6].u.boolean.bval;
2125 assert(!"We should never get here");
2126 return "Internal error";
2131 /* Separate version of mkctrl function for the Pocket PC. */
2132 /* Control coordinates should be specified in dialog units. */
2133 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
2134 LPCTSTR wclass, int wstyle,
2135 int exstyle, const char *wtext, INT_PTR wid)
2140 /* Convert dialog units into pixels */
2141 rc.left = x1; rc.right = x2;
2142 rc.top = y1; rc.bottom = y2;
2143 MapDialogRect(fe->cfgbox, &rc);
2145 MultiByteToWideChar (CP_ACP, 0, wtext, -1, wwtext, 256);
2147 return CreateWindowEx(exstyle, wclass, wwtext,
2148 wstyle | WS_CHILD | WS_VISIBLE,
2150 rc.right - rc.left, rc.bottom - rc.top,
2151 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
2154 static void create_config_controls(frontend * fe)
2157 int col1l, col1r, col2l, col2r, y;
2162 /* Control placement done in dialog units */
2163 col1l = 4; col1r = 96; /* Label column */
2164 col2l = 100; col2r = 154; /* Input column (edit boxes and combo boxes) */
2167 * Count the controls so we can allocate cfgaux.
2169 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
2171 fe->cfgaux = snewn(nctrls, struct cfg_aux);
2174 y = 22; /* Leave some room for the dialog title */
2175 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
2179 * Edit box with a label beside it.
2181 mkctrl(fe, col1l, col1r, y + 1, y + 11,
2182 TEXT("Static"), SS_LEFTNOWORDWRAP, 0, i->name, id++);
2183 mkctrl(fe, col2l, col2r, y, y + 12,
2184 TEXT("EDIT"), WS_BORDER | WS_TABSTOP | ES_AUTOHSCROLL,
2185 0, "", (j->ctlid = id++));
2186 SetDlgItemTextA(fe->cfgbox, j->ctlid, i->u.string.sval);
2193 mkctrl(fe, col1l, col2r, y + 1, y + 11, TEXT("BUTTON"),
2194 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
2195 0, i->name, (j->ctlid = id++));
2196 CheckDlgButton(fe->cfgbox, j->ctlid, (i->u.boolean.bval != 0));
2201 * Drop-down list with a label beside it.
2203 mkctrl(fe, col1l, col1r, y + 1, y + 11,
2204 TEXT("STATIC"), SS_LEFTNOWORDWRAP, 0, i->name, id++);
2205 ctl = mkctrl(fe, col2l, col2r, y, y + 48,
2206 TEXT("COMBOBOX"), WS_BORDER | WS_TABSTOP |
2207 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
2208 0, "", (j->ctlid = id++));
2214 p = i->u.choices.choicenames;
2218 while (*q && *q != c) q++;
2219 str = snewn(q-p+1, char);
2220 strncpy(str, p, q-p);
2224 MultiByteToWideChar (CP_ACP, 0, str, -1, ws, 50);
2225 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)ws);
2233 SendMessage(ctl, CB_SETCURSEL, i->u.choices.selected, 0);
2243 static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg,
2244 WPARAM wParam, LPARAM lParam)
2246 frontend *fe = (frontend *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
2256 fe = (frontend *) lParam;
2257 SetWindowLongPtr(hwnd, GWLP_USERDATA, lParam);
2260 fe->cfg = frontend_get_config(fe, fe->cfg_which, &title);
2262 make_dialog_full_screen(hwnd);
2264 SetDlgItemTextA(hwnd, IDC_CONFIG_CAPTION, title);
2265 SendDlgItemMessage(hwnd, IDC_CONFIG_CAPTION, WM_SETFONT,
2266 (WPARAM) dialog_title_font(), 0);
2268 create_config_controls(fe);
2275 * OK and Cancel are special cases.
2277 if ((LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) {
2278 if (LOWORD(wParam) == IDOK) {
2279 char *err = frontend_set_config(fe, fe->cfg_which, fe->cfg);
2282 MessageBox(hwnd, err, "Validation error",
2283 MB_ICONERROR | MB_OK);
2302 * First find the control whose id this is.
2304 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
2305 if (j->ctlid == LOWORD(wParam))
2308 if (i->type == C_END)
2309 return 0; /* not our problem */
2311 if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) {
2314 TCHAR wBuffer[4096];
2315 GetDlgItemText(fe->cfgbox, j->ctlid, wBuffer, 4096);
2316 WideCharToMultiByte(CP_ACP, 0, wBuffer, -1, buffer, 4096, NULL, NULL);
2318 GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer));
2320 buffer[lenof(buffer)-1] = '\0';
2321 sfree(i->u.string.sval);
2322 i->u.string.sval = dupstr(buffer);
2323 } else if (i->type == C_BOOLEAN &&
2324 (HIWORD(wParam) == BN_CLICKED ||
2325 HIWORD(wParam) == BN_DBLCLK)) {
2326 i->u.boolean.bval = IsDlgButtonChecked(fe->cfgbox, j->ctlid);
2327 } else if (i->type == C_CHOICES &&
2328 HIWORD(wParam) == CBN_SELCHANGE) {
2329 i->u.choices.selected = SendDlgItemMessage(fe->cfgbox, j->ctlid,
2330 CB_GETCURSEL, 0, 0);
2344 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
2345 char *wclass, int wstyle,
2346 int exstyle, const char *wtext, INT_PTR wid)
2349 ret = CreateWindowEx(exstyle, wclass, wtext,
2350 wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1,
2351 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
2352 SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0));
2357 static void about(frontend *fe)
2360 DialogBox(fe->inst, MAKEINTRESOURCE(IDD_ABOUT), fe->hwnd, AboutDlgProc);
2370 int winwidth, winheight, y;
2371 int height, width, maxwid;
2372 const char *strings[16];
2377 sprintf(titlebuf, "About %.250s", fe->game->name);
2379 strings[nstrings++] = fe->game->name;
2380 strings[nstrings++] = "from Simon Tatham's Portable Puzzle Collection";
2381 strings[nstrings++] = ver;
2383 wc.style = CS_DBLCLKS | CS_SAVEBITS;
2384 wc.lpfnWndProc = DefDlgProc;
2386 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
2387 wc.hInstance = fe->inst;
2389 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
2390 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
2391 wc.lpszMenuName = NULL;
2392 wc.lpszClassName = "GameAboutBox";
2395 hdc = GetDC(fe->hwnd);
2396 SetMapMode(hdc, MM_TEXT);
2398 fe->dlg_done = FALSE;
2400 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
2402 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2403 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2408 oldfont = SelectObject(hdc, fe->cfgfont);
2409 if (GetTextMetrics(hdc, &tm)) {
2410 height = tm.tmAscent + tm.tmDescent;
2411 width = tm.tmAveCharWidth;
2413 height = width = 30;
2417 * Figure out the layout of the About box by measuring the
2418 * length of each piece of text.
2421 winheight = height/2;
2423 for (i = 0; i < nstrings; i++) {
2424 if (GetTextExtentPoint32(hdc, strings[i], strlen(strings[i]), &size))
2425 lengths[i] = size.cx;
2427 lengths[i] = 0; /* *shrug* */
2428 if (maxwid < lengths[i])
2429 maxwid = lengths[i];
2430 winheight += height * 3 / 2 + (height / 2);
2433 winheight += height + height * 7 / 4; /* OK button */
2434 winwidth = maxwid + 4*width;
2436 SelectObject(hdc, oldfont);
2437 ReleaseDC(fe->hwnd, hdc);
2440 * Create the dialog, now that we know its size.
2447 r.bottom = winheight;
2449 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
2450 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
2451 WS_CAPTION | WS_SYSMENU*/) &~
2452 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
2456 * Centre the dialog on its parent window.
2460 GetWindowRect(fe->hwnd, &r2);
2461 r.left = (r2.left + r2.right - r.right) / 2;
2462 r.top = (r2.top + r2.bottom - r.bottom) / 2;
2466 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, titlebuf,
2467 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
2468 WS_CAPTION | WS_SYSMENU,
2470 r.right-r.left, r.bottom-r.top,
2471 fe->hwnd, NULL, fe->inst, NULL);
2474 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
2476 SetWindowLongPtr(fe->cfgbox, GWLP_USERDATA, (LONG_PTR)fe);
2477 SetWindowLongPtr(fe->cfgbox, DWLP_DLGPROC, (LONG_PTR)AboutDlgProc);
2481 for (i = 0; i < nstrings; i++) {
2482 int border = width*2 + (maxwid - lengths[i]) / 2;
2483 mkctrl(fe, border, border+lengths[i], y+height*1/8, y+height*9/8,
2484 "Static", 0, 0, strings[i], id++);
2487 assert(y < winheight);
2491 y += height/2; /* extra space before OK */
2492 mkctrl(fe, width*2, maxwid+width*2, y, y+height*7/4, "BUTTON",
2493 BS_PUSHBUTTON | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
2496 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
2498 EnableWindow(fe->hwnd, FALSE);
2499 ShowWindow(fe->cfgbox, SW_SHOWNORMAL);
2500 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
2501 if (!IsDialogMessage(fe->cfgbox, &msg))
2502 DispatchMessage(&msg);
2506 EnableWindow(fe->hwnd, TRUE);
2507 SetForegroundWindow(fe->hwnd);
2508 DestroyWindow(fe->cfgbox);
2509 DeleteObject(fe->cfgfont);
2513 static int get_config(frontend *fe, int which)
2516 fe->cfg_which = which;
2518 return DialogBoxParam(fe->inst,
2519 MAKEINTRESOURCE(IDD_CONFIG),
2520 fe->hwnd, ConfigDlgProc,
2534 int winwidth, winheight, col1l, col1r, col2l, col2r, y;
2535 int height, width, maxlabel, maxcheckbox;
2537 wc.style = CS_DBLCLKS | CS_SAVEBITS;
2538 wc.lpfnWndProc = DefDlgProc;
2540 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
2541 wc.hInstance = fe->inst;
2543 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
2544 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
2545 wc.lpszMenuName = NULL;
2546 wc.lpszClassName = "GameConfigBox";
2549 hdc = GetDC(fe->hwnd);
2550 SetMapMode(hdc, MM_TEXT);
2552 fe->dlg_done = FALSE;
2554 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
2556 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2557 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2562 oldfont = SelectObject(hdc, fe->cfgfont);
2563 if (GetTextMetrics(hdc, &tm)) {
2564 height = tm.tmAscent + tm.tmDescent;
2565 width = tm.tmAveCharWidth;
2567 height = width = 30;
2570 fe->cfg = frontend_get_config(fe, which, &title);
2571 fe->cfg_which = which;
2574 * Figure out the layout of the config box by measuring the
2575 * length of each piece of text.
2577 maxlabel = maxcheckbox = 0;
2578 winheight = height/2;
2580 for (i = fe->cfg; i->type != C_END; i++) {
2585 * Both these control types have a label filling only
2586 * the left-hand column of the box.
2588 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
2591 winheight += height * 3 / 2 + (height / 2);
2596 * Checkboxes take up the whole of the box width.
2598 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
2599 maxcheckbox < size.cx)
2600 maxcheckbox = size.cx;
2601 winheight += height + (height / 2);
2606 winheight += height + height * 7 / 4; /* OK / Cancel buttons */
2609 col1r = col1l + maxlabel;
2610 col2l = col1r + 2*width;
2611 col2r = col2l + 30*width;
2612 if (col2r < col1l+2*height+maxcheckbox)
2613 col2r = col1l+2*height+maxcheckbox;
2614 winwidth = col2r + 2*width;
2616 SelectObject(hdc, oldfont);
2617 ReleaseDC(fe->hwnd, hdc);
2620 * Create the dialog, now that we know its size.
2627 r.bottom = winheight;
2629 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
2630 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
2631 WS_CAPTION | WS_SYSMENU*/) &~
2632 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
2636 * Centre the dialog on its parent window.
2640 GetWindowRect(fe->hwnd, &r2);
2641 r.left = (r2.left + r2.right - r.right) / 2;
2642 r.top = (r2.top + r2.bottom - r.bottom) / 2;
2646 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title,
2647 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
2648 WS_CAPTION | WS_SYSMENU,
2650 r.right-r.left, r.bottom-r.top,
2651 fe->hwnd, NULL, fe->inst, NULL);
2655 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
2657 SetWindowLongPtr(fe->cfgbox, GWLP_USERDATA, (LONG_PTR)fe);
2658 SetWindowLongPtr(fe->cfgbox, DWLP_DLGPROC, (LONG_PTR)ConfigDlgProc);
2661 * Count the controls so we can allocate cfgaux.
2663 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
2665 fe->cfgaux = snewn(nctrls, struct cfg_aux);
2669 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
2673 * Edit box with a label beside it.
2675 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
2676 "Static", 0, 0, i->name, id++);
2677 ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2,
2678 "EDIT", WS_TABSTOP | ES_AUTOHSCROLL,
2679 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
2680 SetWindowText(ctl, i->u.string.sval);
2688 mkctrl(fe, col1l, col2r, y, y+height, "BUTTON",
2689 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
2690 0, i->name, (j->ctlid = id++));
2691 CheckDlgButton(fe->cfgbox, j->ctlid, (i->u.boolean.bval != 0));
2697 * Drop-down list with a label beside it.
2699 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
2700 "STATIC", 0, 0, i->name, id++);
2701 ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2,
2702 "COMBOBOX", WS_TABSTOP |
2703 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
2704 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
2710 SendMessage(ctl, CB_RESETCONTENT, 0, 0);
2711 p = i->u.choices.choicenames;
2715 while (*q && *q != c) q++;
2716 str = snewn(q-p+1, char);
2717 strncpy(str, p, q-p);
2719 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str);
2726 SendMessage(ctl, CB_SETCURSEL, i->u.choices.selected, 0);
2732 assert(y < winheight);
2736 y += height/2; /* extra space before OK and Cancel */
2737 mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON",
2738 BS_PUSHBUTTON | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
2740 mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON",
2741 BS_PUSHBUTTON | WS_TABSTOP, 0, "Cancel", IDCANCEL);
2743 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
2745 EnableWindow(fe->hwnd, FALSE);
2746 ShowWindow(fe->cfgbox, SW_SHOWNORMAL);
2747 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
2748 if (!IsDialogMessage(fe->cfgbox, &msg))
2749 DispatchMessage(&msg);
2753 EnableWindow(fe->hwnd, TRUE);
2754 SetForegroundWindow(fe->hwnd);
2755 DestroyWindow(fe->cfgbox);
2756 DeleteObject(fe->cfgfont);
2761 return (fe->dlg_done == 2);
2766 static void calculate_bitmap_position(frontend *fe, int x, int y)
2768 /* Pocket PC - center the game in the full screen window */
2772 GetClientRect(fe->hwnd, &rcClient);
2773 fe->bitmapPosition.left = (rcClient.right - x) / 2;
2774 yMargin = rcClient.bottom - y;
2776 if (fe->numpad != NULL) {
2778 GetWindowRect(fe->numpad, &rcPad);
2779 yMargin -= rcPad.bottom - rcPad.top;
2782 if (fe->statusbar != NULL) {
2784 GetWindowRect(fe->statusbar, &rcStatus);
2785 yMargin -= rcStatus.bottom - rcStatus.top;
2788 fe->bitmapPosition.top = yMargin / 2;
2790 fe->bitmapPosition.right = fe->bitmapPosition.left + x;
2791 fe->bitmapPosition.bottom = fe->bitmapPosition.top + y;
2794 static void calculate_bitmap_position(frontend *fe, int x, int y)
2796 /* Plain Windows - position the game in the upper-left corner */
2797 fe->bitmapPosition.left = 0;
2798 fe->bitmapPosition.top = 0;
2799 fe->bitmapPosition.right = fe->bitmapPosition.left + x;
2800 fe->bitmapPosition.bottom = fe->bitmapPosition.top + y;
2804 static void new_bitmap(frontend *fe, int x, int y)
2808 if (fe->bitmap) DeleteObject(fe->bitmap);
2810 hdc = GetDC(fe->hwnd);
2811 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
2812 calculate_bitmap_position(fe, x, y);
2813 ReleaseDC(fe->hwnd, hdc);
2816 static void new_game_size(frontend *fe, float scale)
2821 get_max_puzzle_size(fe, &x, &y);
2822 midend_size(fe->me, &x, &y, FALSE);
2825 x = (int)((float)x * fe->puzz_scale);
2826 y = (int)((float)y * fe->puzz_scale);
2827 midend_size(fe->me, &x, &y, TRUE);
2829 fe->ymin = (fe->xmin * y) / x;
2834 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
2836 if (fe->statusbar != NULL) {
2837 GetWindowRect(fe->statusbar, &sr);
2839 sr.left = sr.right = sr.top = sr.bottom = 0;
2842 SetWindowPos(fe->hwnd, NULL, 0, 0,
2844 r.bottom - r.top + sr.bottom - sr.top,
2845 SWP_NOMOVE | SWP_NOZORDER);
2848 check_window_size(fe, &x, &y);
2851 if (fe->statusbar != NULL)
2852 SetWindowPos(fe->statusbar, NULL, 0, y, x,
2853 sr.bottom - sr.top, SWP_NOZORDER);
2856 new_bitmap(fe, x, y);
2859 InvalidateRect(fe->hwnd, NULL, TRUE);
2861 midend_redraw(fe->me);
2865 * Given a proposed new window rect, work out the resulting
2866 * difference in client size (from current), and use to try
2867 * and resize the puzzle, returning (wx,wy) as the actual
2871 static void adjust_game_size(frontend *fe, RECT *proposed, int isedge,
2872 int *wx_r, int *wy_r)
2875 int nx, ny, xdiff, ydiff, wx, wy;
2877 /* Work out the current window sizing, and thus the
2878 * difference in size we're asking for. */
2879 GetClientRect(fe->hwnd, &cr);
2881 AdjustWindowRectEx(&wr, WINFLAGS, TRUE, 0);
2883 xdiff = (proposed->right - proposed->left) - (wr.right - wr.left);
2884 ydiff = (proposed->bottom - proposed->top) - (wr.bottom - wr.top);
2887 /* These next four lines work around the fact that midend_size
2888 * is happy to shrink _but not grow_ if you change one dimension
2889 * but not the other. */
2890 if (xdiff > 0 && ydiff == 0)
2891 ydiff = (xdiff * (wr.right - wr.left)) / (wr.bottom - wr.top);
2892 if (xdiff == 0 && ydiff > 0)
2893 xdiff = (ydiff * (wr.bottom - wr.top)) / (wr.right - wr.left);
2896 if (check_window_resize(fe,
2897 (cr.right - cr.left) + xdiff,
2898 (cr.bottom - cr.top) + ydiff,
2899 &nx, &ny, &wx, &wy)) {
2900 new_bitmap(fe, nx, ny);
2901 midend_force_redraw(fe->me);
2903 /* reset size to current window size */
2904 wx = wr.right - wr.left;
2905 wy = wr.bottom - wr.top;
2907 /* Re-fetch rectangle; size limits mean we might not have
2908 * taken it quite to the mouse drag positions. */
2909 GetClientRect(fe->hwnd, &cr);
2910 adjust_statusbar(fe, &cr);
2912 *wx_r = wx; *wy_r = wy;
2915 static void update_type_menu_tick(frontend *fe)
2919 if (fe->typemenu == INVALID_HANDLE_VALUE)
2922 n = midend_which_preset(fe->me);
2924 for (i = 0; i < fe->n_preset_menuitems; i++) {
2925 if (fe->preset_menuitems[i].which_menu) {
2926 int flag = (i == n ? MF_CHECKED : MF_UNCHECKED);
2927 CheckMenuItem(fe->preset_menuitems[i].which_menu,
2928 fe->preset_menuitems[i].item_index,
2929 MF_BYPOSITION | flag);
2933 if (fe->game->can_configure) {
2934 int flag = (n < 0 ? MF_CHECKED : MF_UNCHECKED);
2935 /* "Custom" menu item is at the bottom of the top-level Type menu */
2936 total = GetMenuItemCount(fe->typemenu);
2937 CheckMenuItem(fe->typemenu, total - 1, MF_BYPOSITION | flag);
2940 DrawMenuBar(fe->hwnd);
2943 static void update_copy_menu_greying(frontend *fe)
2945 UINT enable = (midend_can_format_as_text_now(fe->me) ?
2946 MF_ENABLED : MF_GRAYED);
2947 EnableMenuItem(fe->gamemenu, IDM_COPY, MF_BYCOMMAND | enable);
2950 static void new_game_type(frontend *fe)
2952 midend_new_game(fe->me);
2953 new_game_size(fe, 1.0);
2954 update_type_menu_tick(fe);
2955 update_copy_menu_greying(fe);
2958 static int is_alt_pressed(void)
2961 int r = GetKeyboardState(keystate);
2964 if (keystate[VK_MENU] & 0x80)
2966 if (keystate[VK_RMENU] & 0x80)
2971 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
2972 WPARAM wParam, LPARAM lParam)
2974 frontend *fe = (frontend *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
2979 DestroyWindow(hwnd);
2983 /* Numeric pad sends WM_COMMAND messages */
2984 if ((wParam >= IDM_KEYEMUL) && (wParam < IDM_KEYEMUL + 256))
2986 midend_process_key(fe->me, 0, 0, wParam - IDM_KEYEMUL);
2989 cmd = wParam & ~0xF; /* low 4 bits reserved to Windows */
2992 if (!midend_process_key(fe->me, 0, 0, UI_NEWGAME))
2996 midend_restart_game(fe->me);
2999 if (!midend_process_key(fe->me, 0, 0, UI_UNDO))
3003 if (!midend_process_key(fe->me, 0, 0, UI_REDO))
3008 char *text = midend_text_format(fe->me);
3010 write_clip(hwnd, text);
3012 MessageBeep(MB_ICONWARNING);
3018 char *msg = midend_solve(fe->me);
3020 MessageBox(hwnd, msg, "Unable to solve",
3021 MB_ICONERROR | MB_OK);
3025 if (!midend_process_key(fe->me, 0, 0, UI_QUIT))
3029 if (get_config(fe, CFG_SETTINGS))
3033 if (get_config(fe, CFG_SEED))
3037 if (get_config(fe, CFG_DESC))
3041 if (get_config(fe, CFG_PRINT))
3051 char filename[FILENAME_MAX];
3054 memset(&of, 0, sizeof(of));
3055 of.hwndOwner = hwnd;
3056 of.lpstrFilter = "All Files (*.*)\0*\0\0\0";
3057 of.lpstrCustomFilter = NULL;
3058 of.nFilterIndex = 1;
3059 of.lpstrFile = filename;
3061 of.nMaxFile = lenof(filename);
3062 of.lpstrFileTitle = NULL;
3063 of.lpstrTitle = (cmd == IDM_SAVE ?
3064 "Enter name of game file to save" :
3065 "Enter name of saved game file to load");
3067 #ifdef OPENFILENAME_SIZE_VERSION_400
3068 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
3070 of.lStructSize = sizeof(of);
3072 of.lpstrInitialDir = NULL;
3074 if (cmd == IDM_SAVE)
3075 ret = GetSaveFileName(&of);
3077 ret = GetOpenFileName(&of);
3080 if (cmd == IDM_SAVE) {
3083 if ((fp = fopen(filename, "r")) != NULL) {
3084 char buf[256 + FILENAME_MAX];
3088 sprintf(buf, "Are you sure you want to overwrite"
3089 " the file \"%.*s\"?",
3090 FILENAME_MAX, filename);
3091 if (MessageBox(hwnd, buf, "Question",
3092 MB_YESNO | MB_ICONQUESTION)
3097 fp = fopen(filename, "w");
3100 MessageBox(hwnd, "Unable to open save file",
3101 "Error", MB_ICONERROR | MB_OK);
3105 midend_serialise(fe->me, savefile_write, fp);
3109 FILE *fp = fopen(filename, "r");
3111 midend *me = fe->me;
3117 MessageBox(hwnd, "Unable to open saved game file",
3118 "Error", MB_ICONERROR | MB_OK);
3124 * This save file might be from a different
3127 err = identify_game(&id_name, savefile_read, fp);
3130 for (i = 0; i < gamecount; i++)
3131 if (!strcmp(id_name, gamelist[i]->name))
3133 if (i == gamecount) {
3134 err = "Save file is for a game not "
3135 "supported by this program";
3137 me = midend_for_new_game(fe, gamelist[i], NULL,
3138 FALSE, FALSE, &err);
3139 rewind(fp); /* for the actual load */
3145 err = midend_deserialise(me, savefile_read, fp);
3150 MessageBox(hwnd, err, "Error", MB_ICONERROR|MB_OK);
3155 fe_set_midend(fe, me);
3156 new_game_size(fe, 1.0);
3164 start_help(fe, NULL);
3167 assert(help_type != NONE);
3168 start_help(fe, help_type == CHM ?
3169 fe->game->htmlhelp_topic : fe->game->winhelp_topic);
3174 if (wParam >= IDM_GAMES && wParam < (IDM_GAMES + (WPARAM)gamecount)) {
3175 int p = wParam - IDM_GAMES;
3177 fe_set_midend(fe, midend_for_new_game(fe, gamelist[p], NULL,
3178 FALSE, FALSE, &error));
3183 game_params *preset = preset_menu_lookup_by_id(
3185 ((wParam &~ 0xF) - IDM_PRESETS) / 0x10);
3188 midend_set_params(fe->me, preset);
3209 hdc = BeginPaint(hwnd, &p);
3210 hdc2 = CreateCompatibleDC(hdc);
3211 prevbm = SelectObject(hdc2, fe->bitmap);
3213 FillRect(hdc, &(p.rcPaint), (HBRUSH) GetStockObject(WHITE_BRUSH));
3215 IntersectRect(&rcDest, &(fe->bitmapPosition), &(p.rcPaint));
3217 rcDest.left, rcDest.top,
3218 rcDest.right - rcDest.left,
3219 rcDest.bottom - rcDest.top,
3221 rcDest.left - fe->bitmapPosition.left,
3222 rcDest.top - fe->bitmapPosition.top,
3224 SelectObject(hdc2, prevbm);
3233 int r = GetKeyboardState(keystate);
3234 int shift = (r && (keystate[VK_SHIFT] & 0x80)) ? MOD_SHFT : 0;
3235 int ctrl = (r && (keystate[VK_CONTROL] & 0x80)) ? MOD_CTRL : 0;
3239 if (!(lParam & 0x01000000))
3240 key = MOD_NUM_KEYPAD | '4';
3242 key = shift | ctrl | CURSOR_LEFT;
3245 if (!(lParam & 0x01000000))
3246 key = MOD_NUM_KEYPAD | '6';
3248 key = shift | ctrl | CURSOR_RIGHT;
3251 if (!(lParam & 0x01000000))
3252 key = MOD_NUM_KEYPAD | '8';
3254 key = shift | ctrl | CURSOR_UP;
3257 if (!(lParam & 0x01000000))
3258 key = MOD_NUM_KEYPAD | '2';
3260 key = shift | ctrl | CURSOR_DOWN;
3263 * Diagonal keys on the numeric keypad.
3266 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '9';
3269 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '3';
3272 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '7';
3275 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '1';
3278 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '0';
3281 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '5';
3284 * Numeric keypad keys with Num Lock on.
3286 case VK_NUMPAD4: key = MOD_NUM_KEYPAD | '4'; break;
3287 case VK_NUMPAD6: key = MOD_NUM_KEYPAD | '6'; break;
3288 case VK_NUMPAD8: key = MOD_NUM_KEYPAD | '8'; break;
3289 case VK_NUMPAD2: key = MOD_NUM_KEYPAD | '2'; break;
3290 case VK_NUMPAD5: key = MOD_NUM_KEYPAD | '5'; break;
3291 case VK_NUMPAD9: key = MOD_NUM_KEYPAD | '9'; break;
3292 case VK_NUMPAD3: key = MOD_NUM_KEYPAD | '3'; break;
3293 case VK_NUMPAD7: key = MOD_NUM_KEYPAD | '7'; break;
3294 case VK_NUMPAD1: key = MOD_NUM_KEYPAD | '1'; break;
3295 case VK_NUMPAD0: key = MOD_NUM_KEYPAD | '0'; break;
3299 if (!midend_process_key(fe->me, 0, 0, key))
3304 m.message = WM_KEYDOWN;
3306 m.lParam = lParam & 0xdfff;
3307 TranslateMessage(&m);
3311 case WM_LBUTTONDOWN:
3312 case WM_RBUTTONDOWN:
3313 case WM_MBUTTONDOWN:
3318 * Shift-clicks count as middle-clicks, since otherwise
3319 * two-button Windows users won't have any kind of
3320 * middle click to use.
3322 if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT))
3323 button = MIDDLE_BUTTON;
3324 else if (message == WM_RBUTTONDOWN || is_alt_pressed())
3325 button = RIGHT_BUTTON;
3328 button = LEFT_BUTTON;
3330 if ((fe->game->flags & REQUIRE_RBUTTON) == 0)
3331 button = LEFT_BUTTON;
3336 shrgi.cbSize = sizeof(SHRGINFO);
3337 shrgi.hwndClient = hwnd;
3338 shrgi.ptDown.x = (signed short)LOWORD(lParam);
3339 shrgi.ptDown.y = (signed short)HIWORD(lParam);
3340 shrgi.dwFlags = SHRG_RETURNCMD;
3342 if (GN_CONTEXTMENU == SHRecognizeGesture(&shrgi))
3343 button = RIGHT_BUTTON;
3345 button = LEFT_BUTTON;
3349 if (!midend_process_key(fe->me,
3350 (signed short)LOWORD(lParam) - fe->bitmapPosition.left,
3351 (signed short)HIWORD(lParam) - fe->bitmapPosition.top,
3365 * Shift-clicks count as middle-clicks, since otherwise
3366 * two-button Windows users won't have any kind of
3367 * middle click to use.
3369 if (message == WM_MBUTTONUP || (wParam & MK_SHIFT))
3370 button = MIDDLE_RELEASE;
3371 else if (message == WM_RBUTTONUP || is_alt_pressed())
3372 button = RIGHT_RELEASE;
3374 button = LEFT_RELEASE;
3376 if (!midend_process_key(fe->me,
3377 (signed short)LOWORD(lParam) - fe->bitmapPosition.left,
3378 (signed short)HIWORD(lParam) - fe->bitmapPosition.top,
3389 if (wParam & (MK_MBUTTON | MK_SHIFT))
3390 button = MIDDLE_DRAG;
3391 else if (wParam & MK_RBUTTON || is_alt_pressed())
3392 button = RIGHT_DRAG;
3396 if (!midend_process_key(fe->me,
3397 (signed short)LOWORD(lParam) - fe->bitmapPosition.left,
3398 (signed short)HIWORD(lParam) - fe->bitmapPosition.top,
3405 int key = (unsigned char)wParam;
3406 if (key == '\x1A') {
3408 if (GetKeyboardState(keystate) &&
3409 (keystate[VK_SHIFT] & 0x80) &&
3410 (keystate[VK_CONTROL] & 0x80))
3413 if (!midend_process_key(fe->me, 0, 0, key))
3419 DWORD now = GetTickCount();
3420 float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F;
3421 midend_timer(fe->me, elapsed);
3422 fe->timer_last_tickcount = now;
3428 RECT *sr = (RECT *)lParam;
3429 int wx, wy, isedge = 0;
3431 if (wParam == WMSZ_TOP ||
3432 wParam == WMSZ_RIGHT ||
3433 wParam == WMSZ_BOTTOM ||
3434 wParam == WMSZ_LEFT) isedge = 1;
3435 adjust_game_size(fe, sr, isedge, &wx, &wy);
3437 /* Given the window size the puzzles constrain
3438 * us to, work out which edge we should be moving. */
3439 if (wParam == WMSZ_TOP ||
3440 wParam == WMSZ_TOPLEFT ||
3441 wParam == WMSZ_TOPRIGHT) {
3442 sr->top = sr->bottom - wy;
3444 sr->bottom = sr->top + wy;
3446 if (wParam == WMSZ_LEFT ||
3447 wParam == WMSZ_TOPLEFT ||
3448 wParam == WMSZ_BOTTOMLEFT) {
3449 sr->left = sr->right - wx;
3451 sr->right = sr->left + wx;
3459 return DefWindowProc(hwnd, message, wParam, lParam);
3463 static int FindPreviousInstance()
3465 /* Check if application is running. If it's running then focus on the window */
3466 HWND hOtherWnd = NULL;
3468 hOtherWnd = FindWindow (wGameName, wGameName);
3471 SetForegroundWindow (hOtherWnd);
3480 * Split a complete command line into argc/argv, attempting to do it
3481 * exactly the same way the Visual Studio C library would do it (so
3482 * that our console utilities, which receive argc and argv already
3483 * broken apart by the C library, will have their command lines
3484 * processed in the same way as the GUI utilities which get a whole
3485 * command line and must call this function).
3487 * Does not modify the input command line.
3489 * The final parameter (argstart) is used to return a second array
3490 * of char * pointers, the same length as argv, each one pointing
3491 * at the start of the corresponding element of argv in the
3492 * original command line. So if you get half way through processing
3493 * your command line in argc/argv form and then decide you want to
3494 * treat the rest as a raw string, you can. If you don't want to,
3495 * `argstart' can be safely left NULL.
3497 void split_into_argv(char *cmdline, int *argc, char ***argv,
3501 char *outputline, *q;
3502 char **outputargv, **outputargstart;
3506 * These argument-breaking rules apply to Visual Studio 7, which
3507 * is currently the compiler expected to be used for the Windows
3508 * port of my puzzles. Visual Studio 10 has different rules,
3509 * lacking the curious mod 3 behaviour of consecutive quotes
3510 * described below; I presume they fixed a bug. As and when we
3511 * migrate to a newer compiler, we'll have to adjust this to
3512 * match; however, for the moment we faithfully imitate in our GUI
3513 * utilities what our CLI utilities can't be prevented from doing.
3515 * When I investigated this, at first glance the rules appeared to
3518 * - Single quotes are not special characters.
3520 * - Double quotes are removed, but within them spaces cease
3523 * - Backslashes are _only_ special when a sequence of them
3524 * appear just before a double quote. In this situation,
3525 * they are treated like C backslashes: so \" just gives a
3526 * literal quote, \\" gives a literal backslash and then
3527 * opens or closes a double-quoted segment, \\\" gives a
3528 * literal backslash and then a literal quote, \\\\" gives
3529 * two literal backslashes and then opens/closes a
3530 * double-quoted segment, and so forth. Note that this
3531 * behaviour is identical inside and outside double quotes.
3533 * - Two successive double quotes become one literal double
3534 * quote, but only _inside_ a double-quoted segment.
3535 * Outside, they just form an empty double-quoted segment
3536 * (which may cause an empty argument word).
3538 * - That only leaves the interesting question of what happens
3539 * when one or more backslashes precedes two or more double
3540 * quotes, starting inside a double-quoted string. And the
3541 * answer to that appears somewhat bizarre. Here I tabulate
3542 * number of backslashes (across the top) against number of
3543 * quotes (down the left), and indicate how many backslashes
3544 * are output, how many quotes are output, and whether a
3545 * quoted segment is open at the end of the sequence:
3551 * 0 0,0,y | 1,0,y 2,0,y 3,0,y 4,0,y
3552 * --------+-----------------------------
3553 * 1 0,0,n | 0,1,y 1,0,n 1,1,y 2,0,n
3554 * q 2 0,1,n | 0,1,n 1,1,n 1,1,n 2,1,n
3555 * u 3 0,1,y | 0,2,n 1,1,y 1,2,n 2,1,y
3556 * o 4 0,1,n | 0,2,y 1,1,n 1,2,y 2,1,n
3557 * t 5 0,2,n | 0,2,n 1,2,n 1,2,n 2,2,n
3558 * e 6 0,2,y | 0,3,n 1,2,y 1,3,n 2,2,y
3559 * s 7 0,2,n | 0,3,y 1,2,n 1,3,y 2,2,n
3560 * 8 0,3,n | 0,3,n 1,3,n 1,3,n 2,3,n
3561 * 9 0,3,y | 0,4,n 1,3,y 1,4,n 2,3,y
3562 * 10 0,3,n | 0,4,y 1,3,n 1,4,y 2,3,n
3563 * 11 0,4,n | 0,4,n 1,4,n 1,4,n 2,4,n
3566 * [Test fragment was of the form "a\\\"""b c" d.]
3568 * There is very weird mod-3 behaviour going on here in the
3569 * number of quotes, and it even applies when there aren't any
3570 * backslashes! How ghastly.
3572 * With a bit of thought, this extremely odd diagram suddenly
3573 * coalesced itself into a coherent, if still ghastly, model of
3576 * - As before, backslashes are only special when one or more
3577 * of them appear contiguously before at least one double
3578 * quote. In this situation the backslashes do exactly what
3579 * you'd expect: each one quotes the next thing in front of
3580 * it, so you end up with n/2 literal backslashes (if n is
3581 * even) or (n-1)/2 literal backslashes and a literal quote
3582 * (if n is odd). In the latter case the double quote
3583 * character right after the backslashes is used up.
3585 * - After that, any remaining double quotes are processed. A
3586 * string of contiguous unescaped double quotes has a mod-3
3589 * * inside a quoted segment, a quote ends the segment.
3590 * * _immediately_ after ending a quoted segment, a quote
3591 * simply produces a literal quote.
3592 * * otherwise, outside a quoted segment, a quote begins a
3595 * So, for example, if we started inside a quoted segment
3596 * then two contiguous quotes would close the segment and
3597 * produce a literal quote; three would close the segment,
3598 * produce a literal quote, and open a new segment. If we
3599 * started outside a quoted segment, then two contiguous
3600 * quotes would open and then close a segment, producing no
3601 * output (but potentially creating a zero-length argument);
3602 * but three quotes would open and close a segment and then
3603 * produce a literal quote.
3607 * First deal with the simplest of all special cases: if there
3608 * aren't any arguments, return 0,NULL,NULL.
3610 while (*cmdline && isspace(*cmdline)) cmdline++;
3612 if (argc) *argc = 0;
3613 if (argv) *argv = NULL;
3614 if (argstart) *argstart = NULL;
3619 * This will guaranteeably be big enough; we can realloc it
3622 outputline = snewn(1+strlen(cmdline), char);
3623 outputargv = snewn(strlen(cmdline)+1 / 2, char *);
3624 outputargstart = snewn(strlen(cmdline)+1 / 2, char *);
3626 p = cmdline; q = outputline; outputargc = 0;
3631 /* Skip whitespace searching for start of argument. */
3632 while (*p && isspace(*p)) p++;
3635 /* We have an argument; start it. */
3636 outputargv[outputargc] = q;
3637 outputargstart[outputargc] = p;
3641 /* Copy data into the argument until it's finished. */
3643 if (!quote && isspace(*p))
3644 break; /* argument is finished */
3646 if (*p == '"' || *p == '\\') {
3648 * We have a sequence of zero or more backslashes
3649 * followed by a sequence of zero or more quotes.
3650 * Count up how many of each, and then deal with
3651 * them as appropriate.
3653 int i, slashes = 0, quotes = 0;
3654 while (*p == '\\') slashes++, p++;
3655 while (*p == '"') quotes++, p++;
3659 * Special case: if there are no quotes,
3660 * slashes are not special at all, so just copy
3661 * n slashes to the output string.
3663 while (slashes--) *q++ = '\\';
3665 /* Slashes annihilate in pairs. */
3666 while (slashes >= 2) slashes -= 2, *q++ = '\\';
3668 /* One remaining slash takes out the first quote. */
3669 if (slashes) quotes--, *q++ = '"';
3672 /* Outside a quote segment, a quote starts one. */
3673 if (!quote) quotes--, quote = 1;
3675 /* Now we produce (n+1)/3 literal quotes... */
3676 for (i = 3; i <= quotes+1; i += 3) *q++ = '"';
3678 /* ... and end in a quote segment iff 3 divides n. */
3679 quote = (quotes % 3 == 0);
3687 /* At the end of an argument, just append a trailing NUL. */
3691 outputargv = sresize(outputargv, outputargc, char *);
3692 outputargstart = sresize(outputargstart, outputargc, char *);
3694 if (argc) *argc = outputargc;
3695 if (argv) *argv = outputargv; else sfree(outputargv);
3696 if (argstart) *argstart = outputargstart; else sfree(outputargstart);
3699 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
3709 split_into_argv(cmdline, &argc, &argv, NULL);
3712 MultiByteToWideChar (CP_ACP, 0, CLASSNAME, -1, wClassName, 256);
3713 if (FindPreviousInstance ())
3717 InitCommonControls();
3723 wndclass.lpfnWndProc = WndProc;
3724 wndclass.cbClsExtra = 0;
3725 wndclass.cbWndExtra = 0;
3726 wndclass.hInstance = inst;
3727 wndclass.hIcon = LoadIcon(inst, MAKEINTRESOURCE(200));
3729 if (!wndclass.hIcon) /* in case resource file is absent */
3730 wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION);
3732 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
3733 wndclass.hbrBackground = NULL;
3734 wndclass.lpszMenuName = NULL;
3736 wndclass.lpszClassName = wClassName;
3738 wndclass.lpszClassName = CLASSNAME;
3741 RegisterClass(&wndclass);
3744 while (*cmdline && isspace((unsigned char)*cmdline))
3753 for (i = 0; i < gamecount; i++) {
3754 const char *p = gamelist[i]->name;
3757 if (isspace((unsigned char)*p)) {
3758 while (*q && isspace((unsigned char)*q))
3761 if (tolower((unsigned char)*p) !=
3762 tolower((unsigned char)*q))
3780 fe = frontend_new(inst);
3781 me = midend_for_new_game(fe, gg, argc > 0 ? argv[0] : NULL,
3782 TRUE, TRUE, &error);
3786 sprintf(buf, "Puzzles Error");
3788 sprintf(buf, "%.100s Error", gg->name);
3790 MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
3794 fe_set_midend(fe, me);
3797 while (GetMessage(&msg, NULL, 0, 0)) {
3798 DispatchMessage(&msg);
3801 DestroyWindow(fe->hwnd);
3806 /* vim: set shiftwidth=4 tabstop=8: */