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(const 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(const 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, const 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,
562 frontend *fe = (frontend *)handle;
567 if (fe->drawstatus == NOTHING)
570 if (fe->drawstatus == PRINTING)
571 fontsize = (int)(fontsize * fe->printpixelscale);
573 xy = win_transform_point(fe, x, y);
576 * Find or create the font.
578 for (i = fe->fontstart; i < fe->nfonts; i++)
579 if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
582 if (i == fe->nfonts) {
583 if (fe->fontsize <= fe->nfonts) {
584 fe->fontsize = fe->nfonts + 10;
585 fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
590 fe->fonts[i].type = fonttype;
591 fe->fonts[i].size = fontsize;
593 memset (&lf, 0, sizeof(LOGFONT));
594 lf.lfHeight = -fontsize;
595 lf.lfWeight = (fe->drawstatus == PRINTING ? 0 : FW_BOLD);
596 lf.lfCharSet = DEFAULT_CHARSET;
597 lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
598 lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
599 lf.lfQuality = DEFAULT_QUALITY;
600 lf.lfPitchAndFamily = (fonttype == FONT_FIXED ?
601 FIXED_PITCH | FF_DONTCARE :
602 VARIABLE_PITCH | FF_SWISS);
604 wcscpy(lf.lfFaceName, TEXT("Tahoma"));
607 fe->fonts[i].font = CreateFontIndirect(&lf);
611 * Position and draw the text.
618 MultiByteToWideChar (CP_UTF8, 0, text, -1, wText, 256);
620 oldfont = SelectObject(fe->hdc, fe->fonts[i].font);
621 if (GetTextMetrics(fe->hdc, &tm)) {
622 if (align & ALIGN_VCENTRE)
623 xy.y -= (tm.tmAscent+tm.tmDescent)/2;
627 if (GetTextExtentPoint32W(fe->hdc, wText, wcslen(wText), &size))
629 if (align & ALIGN_HCENTRE)
631 else if (align & ALIGN_HRIGHT)
634 SetBkMode(fe->hdc, TRANSPARENT);
635 win_text_colour(fe, colour);
636 ExtTextOutW(fe->hdc, xy.x, xy.y, 0, NULL, wText, wcslen(wText), NULL);
637 SelectObject(fe->hdc, oldfont);
641 static void win_draw_rect(void *handle, int x, int y, int w, int h, int colour)
643 frontend *fe = (frontend *)handle;
646 if (fe->drawstatus == NOTHING)
649 if (fe->drawstatus == DRAWING && w == 1 && h == 1) {
651 * Rectangle() appears to get uppity if asked to draw a 1x1
652 * rectangle, presumably on the grounds that that's beneath
653 * its dignity and you ought to be using SetPixel instead.
656 SetPixel(fe->hdc, x, y, fe->colours[colour]);
658 win_set_brush(fe, colour);
659 win_set_pen(fe, colour, TRUE);
660 p = win_transform_point(fe, x, y);
661 q = win_transform_point(fe, x+w, y+h);
662 Rectangle(fe->hdc, p.x, p.y, q.x, q.y);
668 static void win_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour)
670 frontend *fe = (frontend *)handle;
673 if (fe->drawstatus == NOTHING)
676 win_set_pen(fe, colour, FALSE);
677 pp[0] = win_transform_point(fe, x1, y1);
678 pp[1] = win_transform_point(fe, x2, y2);
679 Polyline(fe->hdc, pp, 2);
680 if (fe->drawstatus == DRAWING)
681 SetPixel(fe->hdc, pp[1].x, pp[1].y, fe->colours[colour]);
685 static void win_draw_circle(void *handle, int cx, int cy, int radius,
686 int fillcolour, int outlinecolour)
688 frontend *fe = (frontend *)handle;
691 assert(outlinecolour >= 0);
693 if (fe->drawstatus == NOTHING)
697 win_set_brush(fe, fillcolour);
699 fe->oldbr = SelectObject(fe->hdc, GetStockObject(NULL_BRUSH));
701 win_set_pen(fe, outlinecolour, FALSE);
702 p = win_transform_point(fe, cx - radius, cy - radius);
703 q = win_transform_point(fe, cx + radius, cy + radius);
704 Ellipse(fe->hdc, p.x, p.y, q.x+1, q.y+1);
709 static void win_draw_polygon(void *handle, int *coords, int npoints,
710 int fillcolour, int outlinecolour)
712 frontend *fe = (frontend *)handle;
716 if (fe->drawstatus == NOTHING)
719 pts = snewn(npoints+1, POINT);
721 for (i = 0; i <= npoints; i++) {
722 int j = (i < npoints ? i : 0);
723 pts[i] = win_transform_point(fe, coords[j*2], coords[j*2+1]);
726 assert(outlinecolour >= 0);
728 if (fillcolour >= 0) {
729 win_set_brush(fe, fillcolour);
730 win_set_pen(fe, outlinecolour, FALSE);
731 Polygon(fe->hdc, pts, npoints);
735 win_set_pen(fe, outlinecolour, FALSE);
736 Polyline(fe->hdc, pts, npoints+1);
743 static void win_start_draw(void *handle)
745 frontend *fe = (frontend *)handle;
748 assert(fe->drawstatus == NOTHING);
750 hdc_win = GetDC(fe->hwnd);
751 fe->hdc = CreateCompatibleDC(hdc_win);
752 fe->prevbm = SelectObject(fe->hdc, fe->bitmap);
753 ReleaseDC(fe->hwnd, hdc_win);
756 SetMapMode(fe->hdc, MM_TEXT);
758 fe->drawstatus = DRAWING;
761 static void win_draw_update(void *handle, int x, int y, int w, int h)
763 frontend *fe = (frontend *)handle;
766 if (fe->drawstatus != DRAWING)
774 OffsetRect(&r, fe->bitmapPosition.left, fe->bitmapPosition.top);
775 InvalidateRect(fe->hwnd, &r, FALSE);
778 static void win_end_draw(void *handle)
780 frontend *fe = (frontend *)handle;
781 assert(fe->drawstatus == DRAWING);
782 SelectObject(fe->hdc, fe->prevbm);
785 DeleteObject(fe->clip);
788 fe->drawstatus = NOTHING;
791 static void win_line_width(void *handle, float width)
793 frontend *fe = (frontend *)handle;
795 assert(fe->drawstatus != DRAWING);
796 if (fe->drawstatus == NOTHING)
799 fe->linewidth = (int)(width * fe->printpixelscale);
802 static void win_line_dotted(void *handle, int dotted)
804 frontend *fe = (frontend *)handle;
806 assert(fe->drawstatus != DRAWING);
807 if (fe->drawstatus == NOTHING)
810 fe->linedotted = dotted;
813 static void win_begin_doc(void *handle, int pages)
815 frontend *fe = (frontend *)handle;
817 assert(fe->drawstatus != DRAWING);
818 if (fe->drawstatus == NOTHING)
821 if (StartDoc(fe->hdc, &fe->di) <= 0) {
822 char *e = geterrstr();
823 MessageBox(fe->hwnd, e, "Error starting to print",
824 MB_ICONERROR | MB_OK);
826 fe->drawstatus = NOTHING;
830 * Push a marker on the font stack so that we won't use the
831 * same fonts for printing and drawing. (This is because
832 * drawing seems to look generally better in bold, but printing
833 * is better not in bold.)
835 fe->fontstart = fe->nfonts;
838 static void win_begin_page(void *handle, int number)
840 frontend *fe = (frontend *)handle;
842 assert(fe->drawstatus != DRAWING);
843 if (fe->drawstatus == NOTHING)
846 if (StartPage(fe->hdc) <= 0) {
847 char *e = geterrstr();
848 MessageBox(fe->hwnd, e, "Error starting a page",
849 MB_ICONERROR | MB_OK);
851 fe->drawstatus = NOTHING;
855 static void win_begin_puzzle(void *handle, float xm, float xc,
856 float ym, float yc, int pw, int ph, float wmm)
858 frontend *fe = (frontend *)handle;
859 int ppw, pph, pox, poy;
860 float mmpw, mmph, mmox, mmoy;
863 assert(fe->drawstatus != DRAWING);
864 if (fe->drawstatus == NOTHING)
867 ppw = GetDeviceCaps(fe->hdc, HORZRES);
868 pph = GetDeviceCaps(fe->hdc, VERTRES);
869 mmpw = (float)GetDeviceCaps(fe->hdc, HORZSIZE);
870 mmph = (float)GetDeviceCaps(fe->hdc, VERTSIZE);
873 * Compute the puzzle's position on the logical page.
875 mmox = xm * mmpw + xc;
876 mmoy = ym * mmph + yc;
879 * Work out what that comes to in pixels.
881 pox = (int)(mmox * (float)ppw / mmpw);
882 poy = (int)(mmoy * (float)pph / mmph);
885 * And determine the scale.
887 * I need a scale such that the maximum puzzle-coordinate
888 * extent of the rectangle (pw * scale) is equal to the pixel
889 * equivalent of the puzzle's millimetre width (wmm * ppw /
892 scale = (wmm * ppw) / (mmpw * pw);
895 * Now store pox, poy and scale for use in the main drawing
898 fe->printoffsetx = pox;
899 fe->printoffsety = poy;
900 fe->printpixelscale = scale;
903 fe->linedotted = FALSE;
906 static void win_end_puzzle(void *handle)
908 /* Nothing needs to be done here. */
911 static void win_end_page(void *handle, int number)
913 frontend *fe = (frontend *)handle;
915 assert(fe->drawstatus != DRAWING);
917 if (fe->drawstatus == NOTHING)
920 if (EndPage(fe->hdc) <= 0) {
921 char *e = geterrstr();
922 MessageBox(fe->hwnd, e, "Error finishing a page",
923 MB_ICONERROR | MB_OK);
925 fe->drawstatus = NOTHING;
929 static void win_end_doc(void *handle)
931 frontend *fe = (frontend *)handle;
933 assert(fe->drawstatus != DRAWING);
936 * Free all the fonts created since we began printing.
938 while (fe->nfonts > fe->fontstart) {
940 DeleteObject(fe->fonts[fe->nfonts].font);
945 * The MSDN web site sample code doesn't bother to call EndDoc
946 * if an error occurs half way through printing. I expect doing
947 * so would cause the erroneous document to actually be
948 * printed, or something equally undesirable.
950 if (fe->drawstatus == NOTHING)
953 if (EndDoc(fe->hdc) <= 0) {
954 char *e = geterrstr();
955 MessageBox(fe->hwnd, e, "Error finishing printing",
956 MB_ICONERROR | MB_OK);
958 fe->drawstatus = NOTHING;
962 char *win_text_fallback(void *handle, const char *const *strings, int nstrings)
965 * We assume Windows can cope with any UTF-8 likely to be
966 * emitted by a puzzle.
968 return dupstr(strings[0]);
971 const struct drawing_api win_drawing = {
998 void print(frontend *fe)
1004 midend *nme = NULL; /* non-interactive midend for bulk puzzle generation */
1006 const char *err = NULL;
1009 * Create our document structure and fill it up with puzzles.
1011 doc = document_new(fe->printw, fe->printh, fe->printscale / 100.0F);
1012 for (i = 0; i < fe->printcount; i++) {
1013 if (i == 0 && fe->printcurr) {
1014 err = midend_print_puzzle(fe->me, doc, fe->printsolns);
1017 game_params *params;
1019 nme = midend_new(NULL, fe->game, NULL, NULL);
1022 * Set the non-interactive mid-end to have the same
1023 * parameters as the standard one.
1025 params = midend_get_params(fe->me);
1026 midend_set_params(nme, params);
1027 fe->game->free_params(params);
1030 midend_new_game(nme);
1031 err = midend_print_puzzle(nme, doc, fe->printsolns);
1040 MessageBox(fe->hwnd, err, "Error preparing puzzles for printing",
1041 MB_ICONERROR | MB_OK);
1046 memset(&pd, 0, sizeof(pd));
1047 pd.lStructSize = sizeof(pd);
1048 pd.hwndOwner = fe->hwnd;
1050 pd.hDevNames = NULL;
1051 pd.Flags = PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC |
1052 PD_NOPAGENUMS | PD_NOSELECTION;
1054 pd.nFromPage = pd.nToPage = 0xFFFF;
1055 pd.nMinPage = pd.nMaxPage = 1;
1057 if (!PrintDlg(&pd)) {
1063 * Now pd.hDC is a device context for the printer.
1067 * FIXME: IWBNI we put up an Abort box here.
1070 memset(&fe->di, 0, sizeof(fe->di));
1071 fe->di.cbSize = sizeof(fe->di);
1072 sprintf(doctitle, "Printed puzzles from %s (from Simon Tatham's"
1073 " Portable Puzzle Collection)", fe->game->name);
1074 fe->di.lpszDocName = doctitle;
1075 fe->di.lpszOutput = NULL;
1076 fe->di.lpszDatatype = NULL;
1079 fe->drawstatus = PRINTING;
1082 fe->dr = drawing_new(&win_drawing, NULL, fe);
1083 document_print(doc, fe->dr);
1084 drawing_free(fe->dr);
1087 fe->drawstatus = NOTHING;
1094 void deactivate_timer(frontend *fe)
1097 return; /* for non-interactive midend */
1098 if (fe->hwnd) KillTimer(fe->hwnd, fe->timer);
1102 void activate_timer(frontend *fe)
1105 return; /* for non-interactive midend */
1107 fe->timer = SetTimer(fe->hwnd, 1, 20, NULL);
1108 fe->timer_last_tickcount = GetTickCount();
1112 void write_clip(HWND hwnd, char *data)
1120 * Windows expects CRLF in the clipboard, so we must convert
1121 * any \n that has come out of the puzzle backend.
1124 for (i = 0; data[i]; i++) {
1125 if (data[i] == '\n')
1129 data2 = snewn(len+1, char);
1131 for (i = 0; data[i]; i++) {
1132 if (data[i] == '\n')
1134 data2[j++] = data[i];
1139 clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
1144 lock = GlobalLock(clipdata);
1146 GlobalFree(clipdata);
1150 memcpy(lock, data2, len);
1151 ((unsigned char *) lock)[len] = 0;
1152 GlobalUnlock(clipdata);
1154 if (OpenClipboard(hwnd)) {
1156 SetClipboardData(CF_TEXT, clipdata);
1159 GlobalFree(clipdata);
1165 * Set up Help and see if we can find a help file.
1167 static void init_help(void)
1170 char b[2048], *p, *q, *r;
1174 * Find the executable file path, so we can look alongside
1175 * it for help files. Trim the filename off the end.
1177 GetModuleFileName(NULL, b, sizeof(b) - 1);
1179 p = strrchr(b, '\\');
1180 if (p && p >= r) r = p+1;
1181 q = strrchr(b, ':');
1182 if (q && q >= r) r = q+1;
1186 * Try HTML Help first.
1188 strcpy(r, CHM_FILE_NAME);
1189 if ( (fp = fopen(b, "r")) != NULL) {
1193 * We have a .CHM. See if we can use it.
1195 hh_dll = LoadLibrary("hhctrl.ocx");
1197 htmlhelp = (htmlhelp_t)GetProcAddress(hh_dll, "HtmlHelpA");
1199 FreeLibrary(hh_dll);
1202 help_path = dupstr(b);
1207 #endif /* NO_HTMLHELP */
1210 * Now try old-style .HLP.
1212 strcpy(r, HELP_FILE_NAME);
1213 if ( (fp = fopen(b, "r")) != NULL) {
1216 help_path = dupstr(b);
1220 * See if there's a .CNT file alongside it.
1222 strcpy(r, HELP_CNT_NAME);
1223 if ( (fp = fopen(b, "r")) != NULL) {
1225 help_has_contents = TRUE;
1227 help_has_contents = FALSE;
1232 help_type = NONE; /* didn't find any */
1241 static void start_help(frontend *fe, const char *topic)
1246 switch (help_type) {
1250 str = snewn(10+strlen(topic), char);
1251 sprintf(str, "JI(`',`%s')", topic);
1253 } else if (help_has_contents) {
1256 cmd = HELP_CONTENTS;
1258 WinHelp(fe->hwnd, help_path, cmd, (DWORD)str);
1259 fe->help_running = TRUE;
1266 str = snewn(20 + strlen(topic) + strlen(help_path), char);
1267 sprintf(str, "%s::/%s.html>main", help_path, topic);
1269 str = dupstr(help_path);
1271 htmlhelp(fe->hwnd, str, HH_DISPLAY_TOPIC, 0);
1272 fe->help_running = TRUE;
1274 #endif /* NO_HTMLHELP */
1276 assert(!"This shouldn't happen");
1284 * Stop Help on window cleanup.
1286 static void stop_help(frontend *fe)
1288 if (fe->help_running) {
1289 switch (help_type) {
1291 WinHelp(fe->hwnd, help_path, HELP_QUIT, 0);
1296 htmlhelp(NULL, NULL, HH_CLOSE_ALL, 0);
1298 #endif /* NO_HTMLHELP */
1300 assert(!"This shouldn't happen");
1303 fe->help_running = FALSE;
1310 * Terminate Help on process exit.
1312 static void cleanup_help(void)
1314 /* Nothing to do currently.
1315 * (If we were running HTML Help single-threaded, this is where we'd
1316 * call HH_UNINITIALIZE.) */
1319 static int get_statusbar_height(frontend *fe)
1322 if (fe->statusbar) {
1324 GetWindowRect(fe->statusbar, &sr);
1325 sy = sr.bottom - sr.top;
1332 static void adjust_statusbar(frontend *fe, RECT *r)
1336 if (!fe->statusbar) return;
1338 sy = get_statusbar_height(fe);
1340 SetWindowPos(fe->statusbar, NULL, 0, r->bottom-r->top-sy, r->right-r->left,
1345 static void get_menu_size(HWND wh, RECT *r)
1347 HMENU bar = GetMenu(wh);
1351 SetRect(r, 0, 0, 0, 0);
1352 for (i = 0; i < GetMenuItemCount(bar); i++) {
1353 GetMenuItemRect(wh, bar, i, &rect);
1354 UnionRect(r, r, &rect);
1359 * Given a proposed new puzzle size (cx,cy), work out the actual
1360 * puzzle size that would be (px,py) and the window size including
1361 * furniture (wx,wy).
1364 static int check_window_resize(frontend *fe, int cx, int cy,
1369 int x, y, sy = get_statusbar_height(fe), changed = 0;
1371 /* disallow making window thinner than menu bar */
1372 x = max(cx, fe->xmin);
1373 y = max(cy - sy, fe->ymin);
1376 * See if we actually got the window size we wanted, and adjust
1377 * the puzzle size if not.
1379 midend_size(fe->me, &x, &y, TRUE);
1380 if (x != cx || y != cy) {
1382 * Resize the window, now we know what size we _really_
1388 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1389 *wx = r.right - r.left;
1390 *wy = r.bottom - r.top;
1398 (float)midend_tilesize(fe->me) / (float)fe->game->preferred_tilesize;
1404 * Given the current window size, make sure it's sane for the
1405 * current puzzle and resize if necessary.
1408 static void check_window_size(frontend *fe, int *px, int *py)
1413 GetClientRect(fe->hwnd, &r);
1414 cx = r.right - r.left;
1415 cy = r.bottom - r.top;
1417 if (check_window_resize(fe, cx, cy, px, py, &wx, &wy)) {
1419 SetWindowPos(fe->hwnd, NULL, 0, 0, wx, wy,
1420 SWP_NOMOVE | SWP_NOZORDER);
1425 GetClientRect(fe->hwnd, &r);
1426 adjust_statusbar(fe, &r);
1429 static void get_max_puzzle_size(frontend *fe, int *x, int *y)
1433 if (SystemParametersInfo(SPI_GETWORKAREA, 0, &sr, FALSE)) {
1434 *x = sr.right - sr.left;
1435 *y = sr.bottom - sr.top;
1440 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1441 *x -= r.right - r.left - 100;
1442 *y -= r.bottom - r.top - 100;
1447 if (fe->statusbar != NULL) {
1448 GetWindowRect(fe->statusbar, &sr);
1449 *y -= sr.bottom - sr.top;
1454 /* Toolbar buttons on the numeric pad */
1455 static TBBUTTON tbNumpadButtons[] =
1457 {0, IDM_KEYEMUL + '1', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1458 {1, IDM_KEYEMUL + '2', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1459 {2, IDM_KEYEMUL + '3', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1460 {3, IDM_KEYEMUL + '4', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1461 {4, IDM_KEYEMUL + '5', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1462 {5, IDM_KEYEMUL + '6', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1463 {6, IDM_KEYEMUL + '7', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1464 {7, IDM_KEYEMUL + '8', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1465 {8, IDM_KEYEMUL + '9', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1466 {9, IDM_KEYEMUL + ' ', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1}
1471 * Allocate a new frontend structure and create its main window.
1473 static frontend *frontend_new(HINSTANCE inst)
1476 const char *nogame = "Puzzles (no game selected)";
1478 fe = snew(frontend);
1488 fe->help_running = FALSE;
1490 fe->drawstatus = NOTHING;
1495 fe->nfonts = fe->fontsize = 0;
1501 fe->puzz_scale = 1.0;
1504 MultiByteToWideChar (CP_ACP, 0, nogame, -1, wGameName, 256);
1505 fe->hwnd = CreateWindowEx(0, wClassName, wGameName,
1507 CW_USEDEFAULT, CW_USEDEFAULT,
1508 CW_USEDEFAULT, CW_USEDEFAULT,
1509 NULL, NULL, inst, NULL);
1513 RECT rc, rcBar, rcTB, rcClient;
1515 memset (&mbi, 0, sizeof(SHMENUBARINFO));
1516 mbi.cbSize = sizeof(SHMENUBARINFO);
1517 mbi.hwndParent = fe->hwnd;
1518 mbi.nToolBarId = IDR_MENUBAR1;
1519 mbi.hInstRes = inst;
1521 SHCreateMenuBar(&mbi);
1523 GetWindowRect(fe->hwnd, &rc);
1524 GetWindowRect(mbi.hwndMB, &rcBar);
1525 rc.bottom -= rcBar.bottom - rcBar.top;
1526 MoveWindow(fe->hwnd, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, FALSE);
1531 fe->hwnd = CreateWindowEx(0, CLASSNAME, nogame,
1532 WS_OVERLAPPEDWINDOW &~
1534 CW_USEDEFAULT, CW_USEDEFAULT,
1535 CW_USEDEFAULT, CW_USEDEFAULT,
1536 NULL, NULL, inst, NULL);
1538 DWORD lerr = GetLastError();
1539 printf("no window: 0x%x\n", (unsigned)lerr);
1543 fe->gamemenu = NULL;
1544 fe->preset_menu = NULL;
1546 fe->statusbar = NULL;
1549 SetWindowLongPtr(fe->hwnd, GWLP_USERDATA, (LONG_PTR)fe);
1554 static void savefile_write(void *wctx, void *buf, int len)
1556 FILE *fp = (FILE *)wctx;
1557 fwrite(buf, 1, len, fp);
1560 static int savefile_read(void *wctx, void *buf, int len)
1562 FILE *fp = (FILE *)wctx;
1565 ret = fread(buf, 1, len, fp);
1566 return (ret == len);
1570 * Create an appropriate midend structure to go in a puzzle window,
1571 * given a game type and/or a command-line argument.
1573 * 'arg' can be either a game ID string (descriptive, random, or a
1574 * plain set of parameters) or the filename of a save file. The two
1575 * boolean flag arguments indicate which possibilities are
1578 static midend *midend_for_new_game(frontend *fe, const game *cgame,
1579 char *arg, int maybe_game_id,
1580 int maybe_save_file, char **error)
1585 if (me) midend_free(me);
1586 me = midend_new(fe, cgame, &win_drawing, fe);
1587 midend_new_game(me);
1590 const char *err_param, *err_load;
1593 * See if arg is a valid filename of a save game file.
1596 if (maybe_save_file && (fp = fopen(arg, "r")) != NULL) {
1597 const game *loadgame;
1601 * Find out what kind of game is stored in the save
1602 * file; if we're going to end up loading that, it
1603 * will have to override our caller's judgment as to
1604 * what game to initialise our midend with.
1607 err_load = identify_game(&id_name, savefile_read, fp);
1610 for (i = 0; i < gamecount; i++)
1611 if (!strcmp(id_name, gamelist[i]->name))
1613 if (i == gamecount) {
1614 err_load = "Save file is for a game not supported by"
1617 loadgame = gamelist[i];
1618 rewind(fp); /* go back to the start for actual load */
1625 if (me) midend_free(me);
1626 me = midend_new(fe, loadgame, &win_drawing, fe);
1627 err_load = midend_deserialise(me, savefile_read, fp);
1630 err_load = "Unable to open file";
1633 if (maybe_game_id && (!maybe_save_file || err_load)) {
1635 * See if arg is a game description.
1637 if (me) midend_free(me);
1638 me = midend_new(fe, cgame, &win_drawing, fe);
1639 err_param = midend_game_id(me, arg);
1641 midend_new_game(me);
1643 if (maybe_save_file) {
1644 *error = snewn(256 + strlen(arg) + strlen(err_param) +
1645 strlen(err_load), char);
1646 sprintf(*error, "Supplied argument \"%s\" is neither a"
1647 " game ID (%s) nor a save file (%s)",
1648 arg, err_param, err_load);
1650 *error = dupstr(err_param);
1656 } else if (err_load) {
1657 *error = dupstr(err_load);
1667 static void populate_preset_menu(frontend *fe,
1668 struct preset_menu *menu, HMENU winmenu)
1671 for (i = 0; i < menu->n_entries; i++) {
1672 struct preset_menu_entry *entry = &menu->entries[i];
1674 UINT flags = MF_ENABLED;
1676 if (entry->params) {
1677 id_or_sub = (UINT_PTR)(IDM_PRESETS + 0x10 * entry->id);
1679 fe->preset_menuitems[entry->id].which_menu = winmenu;
1680 fe->preset_menuitems[entry->id].item_index =
1681 GetMenuItemCount(winmenu);
1683 HMENU winsubmenu = CreateMenu();
1684 id_or_sub = (UINT_PTR)winsubmenu;
1687 populate_preset_menu(fe, entry->submenu, winsubmenu);
1691 * FIXME: we ought to go through and do something with ampersands
1696 AppendMenu(winmenu, flags, id_or_sub, entry->title);
1700 MultiByteToWideChar(CP_ACP, 0, entry->title, -1, wName, 255);
1701 AppendMenu(winmenu, flags, id_or_sub, wName);
1708 * Populate a frontend structure with a new midend structure, and
1709 * create any window furniture that it needs.
1711 * Previously-allocated memory and window furniture will be freed by
1715 static int fe_set_midend(frontend *fe, midend *me)
1720 if (fe->me) midend_free(fe->me);
1722 fe->game = midend_which_game(fe->me);
1728 colours = midend_colours(fe->me, &ncolours);
1730 if (fe->colours) sfree(fe->colours);
1731 if (fe->brushes) sfree(fe->brushes);
1732 if (fe->pens) sfree(fe->pens);
1734 fe->colours = snewn(ncolours, COLORREF);
1735 fe->brushes = snewn(ncolours, HBRUSH);
1736 fe->pens = snewn(ncolours, HPEN);
1738 for (i = 0; i < ncolours; i++) {
1739 fe->colours[i] = RGB(255 * colours[i*3+0],
1740 255 * colours[i*3+1],
1741 255 * colours[i*3+2]);
1742 fe->brushes[i] = CreateSolidBrush(fe->colours[i]);
1743 fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]);
1749 DestroyWindow(fe->statusbar);
1750 if (midend_wants_statusbar(fe->me)) {
1751 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME,
1752 TEXT(DEFAULT_STATUSBAR_TEXT),
1753 WS_CHILD | WS_VISIBLE,
1754 0, 0, 0, 0, /* status bar does these */
1755 NULL, NULL, fe->inst, NULL);
1757 fe->statusbar = NULL;
1759 get_max_puzzle_size(fe, &x, &y);
1760 midend_size(fe->me, &x, &y, FALSE);
1765 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1769 DestroyWindow(fe->numpad);
1770 if (fe->game->flags & REQUIRE_NUMPAD)
1772 fe->numpad = CreateToolbarEx (fe->hwnd,
1773 WS_VISIBLE | WS_CHILD | CCS_NOPARENTALIGN | TBSTYLE_FLAT,
1774 0, 10, fe->inst, IDR_PADTOOLBAR,
1775 tbNumpadButtons, sizeof (tbNumpadButtons) / sizeof (TBBUTTON),
1776 0, 0, 14, 15, sizeof (TBBUTTON));
1777 GetWindowRect(fe->numpad, &rcTB);
1778 GetClientRect(fe->hwnd, &rcClient);
1779 MoveWindow(fe->numpad,
1781 rcClient.bottom - (rcTB.bottom - rcTB.top) - 1,
1783 rcTB.bottom - rcTB.top,
1785 SendMessage(fe->numpad, TB_SETINDENT, (rcClient.right - (10 * 21)) / 2, 0);
1790 MultiByteToWideChar (CP_ACP, 0, fe->game->name, -1, wGameName, 256);
1791 SetWindowText(fe->hwnd, wGameName);
1793 SetWindowText(fe->hwnd, fe->game->name);
1797 DestroyWindow(fe->statusbar);
1798 if (midend_wants_statusbar(fe->me)) {
1800 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, TEXT("ooh"),
1801 WS_CHILD | WS_VISIBLE,
1802 0, 0, 0, 0, /* status bar does these */
1803 fe->hwnd, NULL, fe->inst, NULL);
1805 /* Flat status bar looks better on the Pocket PC */
1806 SendMessage(fe->statusbar, SB_SIMPLE, (WPARAM) TRUE, 0);
1807 SendMessage(fe->statusbar, SB_SETTEXT,
1808 (WPARAM) 255 | SBT_NOBORDERS,
1813 * Now resize the window to take account of the status bar.
1815 GetWindowRect(fe->statusbar, &sr);
1816 GetWindowRect(fe->hwnd, &r);
1818 SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left,
1819 r.bottom - r.top + sr.bottom - sr.top,
1820 SWP_NOMOVE | SWP_NOZORDER);
1823 fe->statusbar = NULL;
1827 HMENU oldmenu = GetMenu(fe->hwnd);
1830 HMENU bar = CreateMenu();
1831 HMENU menu = CreateMenu();
1834 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "&Game");
1836 HMENU menu = SHGetSubMenu(SHFindMenuBar(fe->hwnd), ID_GAME);
1837 DeleteMenu(menu, 0, MF_BYPOSITION);
1839 fe->gamemenu = menu;
1840 AppendMenu(menu, MF_ENABLED, IDM_NEW, TEXT("&New"));
1841 AppendMenu(menu, MF_ENABLED, IDM_RESTART, TEXT("&Restart"));
1843 /* ...here I run out of sensible accelerator characters. */
1844 AppendMenu(menu, MF_ENABLED, IDM_DESC, TEXT("Speci&fic..."));
1845 AppendMenu(menu, MF_ENABLED, IDM_SEED, TEXT("Rando&m Seed..."));
1848 if (!fe->preset_menu) {
1850 fe->preset_menu = midend_get_presets(
1851 fe->me, &fe->n_preset_menuitems);
1852 fe->preset_menuitems = snewn(fe->n_preset_menuitems,
1853 struct preset_menuitemref);
1854 for (i = 0; i < fe->n_preset_menuitems; i++)
1855 fe->preset_menuitems[i].which_menu = NULL;
1857 if (fe->preset_menu->n_entries > 0 || fe->game->can_configure) {
1859 HMENU sub = CreateMenu();
1861 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "&Type");
1863 HMENU sub = SHGetSubMenu(SHFindMenuBar(fe->hwnd), ID_TYPE);
1864 DeleteMenu(sub, 0, MF_BYPOSITION);
1867 populate_preset_menu(fe, fe->preset_menu, sub);
1869 if (fe->game->can_configure) {
1870 AppendMenu(sub, MF_ENABLED, IDM_CONFIG, TEXT("&Custom..."));
1875 fe->typemenu = INVALID_HANDLE_VALUE;
1880 #error Windows CE does not support COMBINED build.
1883 HMENU games = CreateMenu();
1886 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1887 AppendMenu(menu, MF_ENABLED|MF_POPUP, (UINT)games, "&Other");
1888 for (i = 0; i < gamecount; i++) {
1889 if (strcmp(gamelist[i]->name, fe->game->name) != 0) {
1890 /* only include those games that aren't the same as the
1891 * game we're currently playing. */
1892 AppendMenu(games, MF_ENABLED, IDM_GAMES + i, gamelist[i]->name);
1898 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1900 AppendMenu(menu, MF_ENABLED, IDM_LOAD, TEXT("&Load..."));
1901 AppendMenu(menu, MF_ENABLED, IDM_SAVE, TEXT("&Save..."));
1902 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1903 if (fe->game->can_print) {
1904 AppendMenu(menu, MF_ENABLED, IDM_PRINT, TEXT("&Print..."));
1905 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1908 AppendMenu(menu, MF_ENABLED, IDM_UNDO, TEXT("Undo"));
1909 AppendMenu(menu, MF_ENABLED, IDM_REDO, TEXT("Redo"));
1911 if (fe->game->can_format_as_text_ever) {
1912 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1913 AppendMenu(menu, MF_ENABLED, IDM_COPY, TEXT("&Copy"));
1916 if (fe->game->can_solve) {
1917 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1918 AppendMenu(menu, MF_ENABLED, IDM_SOLVE, TEXT("Sol&ve"));
1920 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1922 AppendMenu(menu, MF_ENABLED, IDM_QUIT, TEXT("E&xit"));
1923 menu = CreateMenu();
1924 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, TEXT("&Help"));
1926 AppendMenu(menu, MF_ENABLED, IDM_ABOUT, TEXT("&About"));
1928 if (help_type != NONE) {
1930 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1931 AppendMenu(menu, MF_ENABLED, IDM_HELPC, TEXT("&Contents"));
1932 assert(fe->game->name);
1933 item = snewn(10+strlen(fe->game->name), char); /*ick*/
1934 sprintf(item, "&Help on %s", fe->game->name);
1935 AppendMenu(menu, MF_ENABLED, IDM_GAMEHELP, item);
1938 DestroyMenu(oldmenu);
1939 SetMenu(fe->hwnd, bar);
1940 get_menu_size(fe->hwnd, &menusize);
1941 fe->xmin = (menusize.right - menusize.left) + 25;
1945 if (fe->bitmap) DeleteObject(fe->bitmap);
1947 new_game_size(fe, fe->puzz_scale); /* initialises fe->bitmap */
1952 static void show_window(frontend *fe)
1954 ShowWindow(fe->hwnd, SW_SHOWNORMAL);
1955 SetForegroundWindow(fe->hwnd);
1957 update_type_menu_tick(fe);
1958 update_copy_menu_greying(fe);
1960 midend_redraw(fe->me);
1964 static HFONT dialog_title_font()
1966 static HFONT hf = NULL;
1972 memset (&lf, 0, sizeof(LOGFONT));
1973 lf.lfHeight = -11; /* - ((8 * GetDeviceCaps(hdc, LOGPIXELSY)) / 72) */
1974 lf.lfWeight = FW_BOLD;
1975 wcscpy(lf.lfFaceName, TEXT("Tahoma"));
1977 return hf = CreateFontIndirect(&lf);
1980 static void make_dialog_full_screen(HWND hwnd)
1982 SHINITDLGINFO shidi;
1984 /* Make dialog full screen */
1985 shidi.dwMask = SHIDIM_FLAGS;
1986 shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIZEDLGFULLSCREEN |
1989 SHInitDialog(&shidi);
1993 static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg,
1994 WPARAM wParam, LPARAM lParam)
1996 frontend *fe = (frontend *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
2004 make_dialog_full_screen(hwnd);
2006 sprintf(title, "About %.250s", fe->game->name);
2007 SetDlgItemTextA(hwnd, IDC_ABOUT_CAPTION, title);
2009 SendDlgItemMessage(hwnd, IDC_ABOUT_CAPTION, WM_SETFONT,
2010 (WPARAM) dialog_title_font(), 0);
2012 SetDlgItemTextA(hwnd, IDC_ABOUT_GAME, fe->game->name);
2013 SetDlgItemTextA(hwnd, IDC_ABOUT_VERSION, ver);
2019 if (LOWORD(wParam) == IDOK)
2040 * Wrappers on midend_{get,set}_config, which extend the CFG_*
2041 * enumeration to add CFG_PRINT.
2043 static config_item *frontend_get_config(frontend *fe, int which,
2046 if (which < CFG_FRONTEND_SPECIFIC) {
2047 return midend_get_config(fe->me, which, wintitle);
2048 } else if (which == CFG_PRINT) {
2052 *wintitle = snewn(40 + strlen(fe->game->name), char);
2053 sprintf(*wintitle, "%s print setup", fe->game->name);
2055 ret = snewn(8, config_item);
2059 ret[i].name = "Number of puzzles to print";
2060 ret[i].type = C_STRING;
2061 ret[i].u.string.sval = dupstr("1");
2064 ret[i].name = "Number of puzzles across the page";
2065 ret[i].type = C_STRING;
2066 ret[i].u.string.sval = dupstr("1");
2069 ret[i].name = "Number of puzzles down the page";
2070 ret[i].type = C_STRING;
2071 ret[i].u.string.sval = dupstr("1");
2074 ret[i].name = "Percentage of standard size";
2075 ret[i].type = C_STRING;
2076 ret[i].u.string.sval = dupstr("100.0");
2079 ret[i].name = "Include currently shown puzzle";
2080 ret[i].type = C_BOOLEAN;
2081 ret[i].u.boolean.bval = TRUE;
2084 ret[i].name = "Print solutions";
2085 ret[i].type = C_BOOLEAN;
2086 ret[i].u.boolean.bval = FALSE;
2089 if (fe->game->can_print_in_colour) {
2090 ret[i].name = "Print in colour";
2091 ret[i].type = C_BOOLEAN;
2092 ret[i].u.boolean.bval = FALSE;
2097 ret[i].type = C_END;
2102 assert(!"We should never get here");
2107 static const char *frontend_set_config(
2108 frontend *fe, int which, config_item *cfg)
2110 if (which < CFG_FRONTEND_SPECIFIC) {
2111 return midend_set_config(fe->me, which, cfg);
2112 } else if (which == CFG_PRINT) {
2113 if ((fe->printcount = atoi(cfg[0].u.string.sval)) <= 0)
2114 return "Number of puzzles to print should be at least one";
2115 if ((fe->printw = atoi(cfg[1].u.string.sval)) <= 0)
2116 return "Number of puzzles across the page should be at least one";
2117 if ((fe->printh = atoi(cfg[2].u.string.sval)) <= 0)
2118 return "Number of puzzles down the page should be at least one";
2119 if ((fe->printscale = (float)atof(cfg[3].u.string.sval)) <= 0)
2120 return "Print size should be positive";
2121 fe->printcurr = cfg[4].u.boolean.bval;
2122 fe->printsolns = cfg[5].u.boolean.bval;
2123 fe->printcolour = fe->game->can_print_in_colour &&
2124 cfg[6].u.boolean.bval;
2127 assert(!"We should never get here");
2128 return "Internal error";
2133 /* Separate version of mkctrl function for the Pocket PC. */
2134 /* Control coordinates should be specified in dialog units. */
2135 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
2136 LPCTSTR wclass, int wstyle,
2137 int exstyle, const char *wtext, INT_PTR wid)
2142 /* Convert dialog units into pixels */
2143 rc.left = x1; rc.right = x2;
2144 rc.top = y1; rc.bottom = y2;
2145 MapDialogRect(fe->cfgbox, &rc);
2147 MultiByteToWideChar (CP_ACP, 0, wtext, -1, wwtext, 256);
2149 return CreateWindowEx(exstyle, wclass, wwtext,
2150 wstyle | WS_CHILD | WS_VISIBLE,
2152 rc.right - rc.left, rc.bottom - rc.top,
2153 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
2156 static void create_config_controls(frontend * fe)
2159 int col1l, col1r, col2l, col2r, y;
2164 /* Control placement done in dialog units */
2165 col1l = 4; col1r = 96; /* Label column */
2166 col2l = 100; col2r = 154; /* Input column (edit boxes and combo boxes) */
2169 * Count the controls so we can allocate cfgaux.
2171 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
2173 fe->cfgaux = snewn(nctrls, struct cfg_aux);
2176 y = 22; /* Leave some room for the dialog title */
2177 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
2181 * Edit box with a label beside it.
2183 mkctrl(fe, col1l, col1r, y + 1, y + 11,
2184 TEXT("Static"), SS_LEFTNOWORDWRAP, 0, i->name, id++);
2185 mkctrl(fe, col2l, col2r, y, y + 12,
2186 TEXT("EDIT"), WS_BORDER | WS_TABSTOP | ES_AUTOHSCROLL,
2187 0, "", (j->ctlid = id++));
2188 SetDlgItemTextA(fe->cfgbox, j->ctlid, i->u.string.sval);
2195 mkctrl(fe, col1l, col2r, y + 1, y + 11, TEXT("BUTTON"),
2196 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
2197 0, i->name, (j->ctlid = id++));
2198 CheckDlgButton(fe->cfgbox, j->ctlid, (i->u.boolean.bval != 0));
2203 * Drop-down list with a label beside it.
2205 mkctrl(fe, col1l, col1r, y + 1, y + 11,
2206 TEXT("STATIC"), SS_LEFTNOWORDWRAP, 0, i->name, id++);
2207 ctl = mkctrl(fe, col2l, col2r, y, y + 48,
2208 TEXT("COMBOBOX"), WS_BORDER | WS_TABSTOP |
2209 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
2210 0, "", (j->ctlid = id++));
2216 p = i->u.choices.choicenames;
2220 while (*q && *q != c) q++;
2221 str = snewn(q-p+1, char);
2222 strncpy(str, p, q-p);
2226 MultiByteToWideChar (CP_ACP, 0, str, -1, ws, 50);
2227 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)ws);
2235 SendMessage(ctl, CB_SETCURSEL, i->u.choices.selected, 0);
2245 static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg,
2246 WPARAM wParam, LPARAM lParam)
2248 frontend *fe = (frontend *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
2258 fe = (frontend *) lParam;
2259 SetWindowLongPtr(hwnd, GWLP_USERDATA, lParam);
2262 fe->cfg = frontend_get_config(fe, fe->cfg_which, &title);
2264 make_dialog_full_screen(hwnd);
2266 SetDlgItemTextA(hwnd, IDC_CONFIG_CAPTION, title);
2267 SendDlgItemMessage(hwnd, IDC_CONFIG_CAPTION, WM_SETFONT,
2268 (WPARAM) dialog_title_font(), 0);
2270 create_config_controls(fe);
2277 * OK and Cancel are special cases.
2279 if ((LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) {
2280 if (LOWORD(wParam) == IDOK) {
2281 const char *err = frontend_set_config(
2282 fe, fe->cfg_which, fe->cfg);
2285 MessageBox(hwnd, err, "Validation error",
2286 MB_ICONERROR | MB_OK);
2305 * First find the control whose id this is.
2307 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
2308 if (j->ctlid == LOWORD(wParam))
2311 if (i->type == C_END)
2312 return 0; /* not our problem */
2314 if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) {
2317 TCHAR wBuffer[4096];
2318 GetDlgItemText(fe->cfgbox, j->ctlid, wBuffer, 4096);
2319 WideCharToMultiByte(CP_ACP, 0, wBuffer, -1, buffer, 4096, NULL, NULL);
2321 GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer));
2323 buffer[lenof(buffer)-1] = '\0';
2324 sfree(i->u.string.sval);
2325 i->u.string.sval = dupstr(buffer);
2326 } else if (i->type == C_BOOLEAN &&
2327 (HIWORD(wParam) == BN_CLICKED ||
2328 HIWORD(wParam) == BN_DBLCLK)) {
2329 i->u.boolean.bval = IsDlgButtonChecked(fe->cfgbox, j->ctlid);
2330 } else if (i->type == C_CHOICES &&
2331 HIWORD(wParam) == CBN_SELCHANGE) {
2332 i->u.choices.selected = SendDlgItemMessage(fe->cfgbox, j->ctlid,
2333 CB_GETCURSEL, 0, 0);
2347 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
2348 char *wclass, int wstyle,
2349 int exstyle, const char *wtext, INT_PTR wid)
2352 ret = CreateWindowEx(exstyle, wclass, wtext,
2353 wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1,
2354 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
2355 SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0));
2360 static void about(frontend *fe)
2363 DialogBox(fe->inst, MAKEINTRESOURCE(IDD_ABOUT), fe->hwnd, AboutDlgProc);
2373 int winwidth, winheight, y;
2374 int height, width, maxwid;
2375 const char *strings[16];
2380 sprintf(titlebuf, "About %.250s", fe->game->name);
2382 strings[nstrings++] = fe->game->name;
2383 strings[nstrings++] = "from Simon Tatham's Portable Puzzle Collection";
2384 strings[nstrings++] = ver;
2386 wc.style = CS_DBLCLKS | CS_SAVEBITS;
2387 wc.lpfnWndProc = DefDlgProc;
2389 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
2390 wc.hInstance = fe->inst;
2392 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
2393 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
2394 wc.lpszMenuName = NULL;
2395 wc.lpszClassName = "GameAboutBox";
2398 hdc = GetDC(fe->hwnd);
2399 SetMapMode(hdc, MM_TEXT);
2401 fe->dlg_done = FALSE;
2403 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
2405 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2406 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2411 oldfont = SelectObject(hdc, fe->cfgfont);
2412 if (GetTextMetrics(hdc, &tm)) {
2413 height = tm.tmAscent + tm.tmDescent;
2414 width = tm.tmAveCharWidth;
2416 height = width = 30;
2420 * Figure out the layout of the About box by measuring the
2421 * length of each piece of text.
2424 winheight = height/2;
2426 for (i = 0; i < nstrings; i++) {
2427 if (GetTextExtentPoint32(hdc, strings[i], strlen(strings[i]), &size))
2428 lengths[i] = size.cx;
2430 lengths[i] = 0; /* *shrug* */
2431 if (maxwid < lengths[i])
2432 maxwid = lengths[i];
2433 winheight += height * 3 / 2 + (height / 2);
2436 winheight += height + height * 7 / 4; /* OK button */
2437 winwidth = maxwid + 4*width;
2439 SelectObject(hdc, oldfont);
2440 ReleaseDC(fe->hwnd, hdc);
2443 * Create the dialog, now that we know its size.
2450 r.bottom = winheight;
2452 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
2453 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
2454 WS_CAPTION | WS_SYSMENU*/) &~
2455 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
2459 * Centre the dialog on its parent window.
2463 GetWindowRect(fe->hwnd, &r2);
2464 r.left = (r2.left + r2.right - r.right) / 2;
2465 r.top = (r2.top + r2.bottom - r.bottom) / 2;
2469 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, titlebuf,
2470 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
2471 WS_CAPTION | WS_SYSMENU,
2473 r.right-r.left, r.bottom-r.top,
2474 fe->hwnd, NULL, fe->inst, NULL);
2477 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
2479 SetWindowLongPtr(fe->cfgbox, GWLP_USERDATA, (LONG_PTR)fe);
2480 SetWindowLongPtr(fe->cfgbox, DWLP_DLGPROC, (LONG_PTR)AboutDlgProc);
2484 for (i = 0; i < nstrings; i++) {
2485 int border = width*2 + (maxwid - lengths[i]) / 2;
2486 mkctrl(fe, border, border+lengths[i], y+height*1/8, y+height*9/8,
2487 "Static", 0, 0, strings[i], id++);
2490 assert(y < winheight);
2494 y += height/2; /* extra space before OK */
2495 mkctrl(fe, width*2, maxwid+width*2, y, y+height*7/4, "BUTTON",
2496 BS_PUSHBUTTON | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
2499 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
2501 EnableWindow(fe->hwnd, FALSE);
2502 ShowWindow(fe->cfgbox, SW_SHOWNORMAL);
2503 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
2504 if (!IsDialogMessage(fe->cfgbox, &msg))
2505 DispatchMessage(&msg);
2509 EnableWindow(fe->hwnd, TRUE);
2510 SetForegroundWindow(fe->hwnd);
2511 DestroyWindow(fe->cfgbox);
2512 DeleteObject(fe->cfgfont);
2516 static int get_config(frontend *fe, int which)
2519 fe->cfg_which = which;
2521 return DialogBoxParam(fe->inst,
2522 MAKEINTRESOURCE(IDD_CONFIG),
2523 fe->hwnd, ConfigDlgProc,
2537 int winwidth, winheight, col1l, col1r, col2l, col2r, y;
2538 int height, width, maxlabel, maxcheckbox;
2540 wc.style = CS_DBLCLKS | CS_SAVEBITS;
2541 wc.lpfnWndProc = DefDlgProc;
2543 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
2544 wc.hInstance = fe->inst;
2546 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
2547 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
2548 wc.lpszMenuName = NULL;
2549 wc.lpszClassName = "GameConfigBox";
2552 hdc = GetDC(fe->hwnd);
2553 SetMapMode(hdc, MM_TEXT);
2555 fe->dlg_done = FALSE;
2557 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
2559 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2560 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2565 oldfont = SelectObject(hdc, fe->cfgfont);
2566 if (GetTextMetrics(hdc, &tm)) {
2567 height = tm.tmAscent + tm.tmDescent;
2568 width = tm.tmAveCharWidth;
2570 height = width = 30;
2573 fe->cfg = frontend_get_config(fe, which, &title);
2574 fe->cfg_which = which;
2577 * Figure out the layout of the config box by measuring the
2578 * length of each piece of text.
2580 maxlabel = maxcheckbox = 0;
2581 winheight = height/2;
2583 for (i = fe->cfg; i->type != C_END; i++) {
2588 * Both these control types have a label filling only
2589 * the left-hand column of the box.
2591 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
2594 winheight += height * 3 / 2 + (height / 2);
2599 * Checkboxes take up the whole of the box width.
2601 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
2602 maxcheckbox < size.cx)
2603 maxcheckbox = size.cx;
2604 winheight += height + (height / 2);
2609 winheight += height + height * 7 / 4; /* OK / Cancel buttons */
2612 col1r = col1l + maxlabel;
2613 col2l = col1r + 2*width;
2614 col2r = col2l + 30*width;
2615 if (col2r < col1l+2*height+maxcheckbox)
2616 col2r = col1l+2*height+maxcheckbox;
2617 winwidth = col2r + 2*width;
2619 SelectObject(hdc, oldfont);
2620 ReleaseDC(fe->hwnd, hdc);
2623 * Create the dialog, now that we know its size.
2630 r.bottom = winheight;
2632 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
2633 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
2634 WS_CAPTION | WS_SYSMENU*/) &~
2635 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
2639 * Centre the dialog on its parent window.
2643 GetWindowRect(fe->hwnd, &r2);
2644 r.left = (r2.left + r2.right - r.right) / 2;
2645 r.top = (r2.top + r2.bottom - r.bottom) / 2;
2649 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title,
2650 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
2651 WS_CAPTION | WS_SYSMENU,
2653 r.right-r.left, r.bottom-r.top,
2654 fe->hwnd, NULL, fe->inst, NULL);
2658 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
2660 SetWindowLongPtr(fe->cfgbox, GWLP_USERDATA, (LONG_PTR)fe);
2661 SetWindowLongPtr(fe->cfgbox, DWLP_DLGPROC, (LONG_PTR)ConfigDlgProc);
2664 * Count the controls so we can allocate cfgaux.
2666 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
2668 fe->cfgaux = snewn(nctrls, struct cfg_aux);
2672 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
2676 * Edit box with a label beside it.
2678 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
2679 "Static", 0, 0, i->name, id++);
2680 ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2,
2681 "EDIT", WS_TABSTOP | ES_AUTOHSCROLL,
2682 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
2683 SetWindowText(ctl, i->u.string.sval);
2691 mkctrl(fe, col1l, col2r, y, y+height, "BUTTON",
2692 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
2693 0, i->name, (j->ctlid = id++));
2694 CheckDlgButton(fe->cfgbox, j->ctlid, (i->u.boolean.bval != 0));
2700 * Drop-down list with a label beside it.
2702 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
2703 "STATIC", 0, 0, i->name, id++);
2704 ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2,
2705 "COMBOBOX", WS_TABSTOP |
2706 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
2707 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
2713 SendMessage(ctl, CB_RESETCONTENT, 0, 0);
2714 p = i->u.choices.choicenames;
2718 while (*q && *q != c) q++;
2719 str = snewn(q-p+1, char);
2720 strncpy(str, p, q-p);
2722 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str);
2729 SendMessage(ctl, CB_SETCURSEL, i->u.choices.selected, 0);
2735 assert(y < winheight);
2739 y += height/2; /* extra space before OK and Cancel */
2740 mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON",
2741 BS_PUSHBUTTON | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
2743 mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON",
2744 BS_PUSHBUTTON | WS_TABSTOP, 0, "Cancel", IDCANCEL);
2746 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
2748 EnableWindow(fe->hwnd, FALSE);
2749 ShowWindow(fe->cfgbox, SW_SHOWNORMAL);
2750 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
2751 if (!IsDialogMessage(fe->cfgbox, &msg))
2752 DispatchMessage(&msg);
2756 EnableWindow(fe->hwnd, TRUE);
2757 SetForegroundWindow(fe->hwnd);
2758 DestroyWindow(fe->cfgbox);
2759 DeleteObject(fe->cfgfont);
2764 return (fe->dlg_done == 2);
2769 static void calculate_bitmap_position(frontend *fe, int x, int y)
2771 /* Pocket PC - center the game in the full screen window */
2775 GetClientRect(fe->hwnd, &rcClient);
2776 fe->bitmapPosition.left = (rcClient.right - x) / 2;
2777 yMargin = rcClient.bottom - y;
2779 if (fe->numpad != NULL) {
2781 GetWindowRect(fe->numpad, &rcPad);
2782 yMargin -= rcPad.bottom - rcPad.top;
2785 if (fe->statusbar != NULL) {
2787 GetWindowRect(fe->statusbar, &rcStatus);
2788 yMargin -= rcStatus.bottom - rcStatus.top;
2791 fe->bitmapPosition.top = yMargin / 2;
2793 fe->bitmapPosition.right = fe->bitmapPosition.left + x;
2794 fe->bitmapPosition.bottom = fe->bitmapPosition.top + y;
2797 static void calculate_bitmap_position(frontend *fe, int x, int y)
2799 /* Plain Windows - position the game in the upper-left corner */
2800 fe->bitmapPosition.left = 0;
2801 fe->bitmapPosition.top = 0;
2802 fe->bitmapPosition.right = fe->bitmapPosition.left + x;
2803 fe->bitmapPosition.bottom = fe->bitmapPosition.top + y;
2807 static void new_bitmap(frontend *fe, int x, int y)
2811 if (fe->bitmap) DeleteObject(fe->bitmap);
2813 hdc = GetDC(fe->hwnd);
2814 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
2815 calculate_bitmap_position(fe, x, y);
2816 ReleaseDC(fe->hwnd, hdc);
2819 static void new_game_size(frontend *fe, float scale)
2824 get_max_puzzle_size(fe, &x, &y);
2825 midend_size(fe->me, &x, &y, FALSE);
2828 x = (int)((float)x * fe->puzz_scale);
2829 y = (int)((float)y * fe->puzz_scale);
2830 midend_size(fe->me, &x, &y, TRUE);
2832 fe->ymin = (fe->xmin * y) / x;
2837 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
2839 if (fe->statusbar != NULL) {
2840 GetWindowRect(fe->statusbar, &sr);
2842 sr.left = sr.right = sr.top = sr.bottom = 0;
2845 SetWindowPos(fe->hwnd, NULL, 0, 0,
2847 r.bottom - r.top + sr.bottom - sr.top,
2848 SWP_NOMOVE | SWP_NOZORDER);
2851 check_window_size(fe, &x, &y);
2854 if (fe->statusbar != NULL)
2855 SetWindowPos(fe->statusbar, NULL, 0, y, x,
2856 sr.bottom - sr.top, SWP_NOZORDER);
2859 new_bitmap(fe, x, y);
2862 InvalidateRect(fe->hwnd, NULL, TRUE);
2864 midend_redraw(fe->me);
2868 * Given a proposed new window rect, work out the resulting
2869 * difference in client size (from current), and use to try
2870 * and resize the puzzle, returning (wx,wy) as the actual
2874 static void adjust_game_size(frontend *fe, RECT *proposed, int isedge,
2875 int *wx_r, int *wy_r)
2878 int nx, ny, xdiff, ydiff, wx, wy;
2880 /* Work out the current window sizing, and thus the
2881 * difference in size we're asking for. */
2882 GetClientRect(fe->hwnd, &cr);
2884 AdjustWindowRectEx(&wr, WINFLAGS, TRUE, 0);
2886 xdiff = (proposed->right - proposed->left) - (wr.right - wr.left);
2887 ydiff = (proposed->bottom - proposed->top) - (wr.bottom - wr.top);
2890 /* These next four lines work around the fact that midend_size
2891 * is happy to shrink _but not grow_ if you change one dimension
2892 * but not the other. */
2893 if (xdiff > 0 && ydiff == 0)
2894 ydiff = (xdiff * (wr.right - wr.left)) / (wr.bottom - wr.top);
2895 if (xdiff == 0 && ydiff > 0)
2896 xdiff = (ydiff * (wr.bottom - wr.top)) / (wr.right - wr.left);
2899 if (check_window_resize(fe,
2900 (cr.right - cr.left) + xdiff,
2901 (cr.bottom - cr.top) + ydiff,
2902 &nx, &ny, &wx, &wy)) {
2903 new_bitmap(fe, nx, ny);
2904 midend_force_redraw(fe->me);
2906 /* reset size to current window size */
2907 wx = wr.right - wr.left;
2908 wy = wr.bottom - wr.top;
2910 /* Re-fetch rectangle; size limits mean we might not have
2911 * taken it quite to the mouse drag positions. */
2912 GetClientRect(fe->hwnd, &cr);
2913 adjust_statusbar(fe, &cr);
2915 *wx_r = wx; *wy_r = wy;
2918 static void update_type_menu_tick(frontend *fe)
2922 if (fe->typemenu == INVALID_HANDLE_VALUE)
2925 n = midend_which_preset(fe->me);
2927 for (i = 0; i < fe->n_preset_menuitems; i++) {
2928 if (fe->preset_menuitems[i].which_menu) {
2929 int flag = (i == n ? MF_CHECKED : MF_UNCHECKED);
2930 CheckMenuItem(fe->preset_menuitems[i].which_menu,
2931 fe->preset_menuitems[i].item_index,
2932 MF_BYPOSITION | flag);
2936 if (fe->game->can_configure) {
2937 int flag = (n < 0 ? MF_CHECKED : MF_UNCHECKED);
2938 /* "Custom" menu item is at the bottom of the top-level Type menu */
2939 total = GetMenuItemCount(fe->typemenu);
2940 CheckMenuItem(fe->typemenu, total - 1, MF_BYPOSITION | flag);
2943 DrawMenuBar(fe->hwnd);
2946 static void update_copy_menu_greying(frontend *fe)
2948 UINT enable = (midend_can_format_as_text_now(fe->me) ?
2949 MF_ENABLED : MF_GRAYED);
2950 EnableMenuItem(fe->gamemenu, IDM_COPY, MF_BYCOMMAND | enable);
2953 static void new_game_type(frontend *fe)
2955 midend_new_game(fe->me);
2956 new_game_size(fe, 1.0);
2957 update_type_menu_tick(fe);
2958 update_copy_menu_greying(fe);
2961 static int is_alt_pressed(void)
2964 int r = GetKeyboardState(keystate);
2967 if (keystate[VK_MENU] & 0x80)
2969 if (keystate[VK_RMENU] & 0x80)
2974 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
2975 WPARAM wParam, LPARAM lParam)
2977 frontend *fe = (frontend *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
2982 DestroyWindow(hwnd);
2986 /* Numeric pad sends WM_COMMAND messages */
2987 if ((wParam >= IDM_KEYEMUL) && (wParam < IDM_KEYEMUL + 256))
2989 midend_process_key(fe->me, 0, 0, wParam - IDM_KEYEMUL);
2992 cmd = wParam & ~0xF; /* low 4 bits reserved to Windows */
2995 if (!midend_process_key(fe->me, 0, 0, UI_NEWGAME))
2999 midend_restart_game(fe->me);
3002 if (!midend_process_key(fe->me, 0, 0, UI_UNDO))
3006 if (!midend_process_key(fe->me, 0, 0, UI_REDO))
3011 char *text = midend_text_format(fe->me);
3013 write_clip(hwnd, text);
3015 MessageBeep(MB_ICONWARNING);
3021 const char *msg = midend_solve(fe->me);
3023 MessageBox(hwnd, msg, "Unable to solve",
3024 MB_ICONERROR | MB_OK);
3028 if (!midend_process_key(fe->me, 0, 0, UI_QUIT))
3032 if (get_config(fe, CFG_SETTINGS))
3036 if (get_config(fe, CFG_SEED))
3040 if (get_config(fe, CFG_DESC))
3044 if (get_config(fe, CFG_PRINT))
3054 char filename[FILENAME_MAX];
3057 memset(&of, 0, sizeof(of));
3058 of.hwndOwner = hwnd;
3059 of.lpstrFilter = "All Files (*.*)\0*\0\0\0";
3060 of.lpstrCustomFilter = NULL;
3061 of.nFilterIndex = 1;
3062 of.lpstrFile = filename;
3064 of.nMaxFile = lenof(filename);
3065 of.lpstrFileTitle = NULL;
3066 of.lpstrTitle = (cmd == IDM_SAVE ?
3067 "Enter name of game file to save" :
3068 "Enter name of saved game file to load");
3070 #ifdef OPENFILENAME_SIZE_VERSION_400
3071 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
3073 of.lStructSize = sizeof(of);
3075 of.lpstrInitialDir = NULL;
3077 if (cmd == IDM_SAVE)
3078 ret = GetSaveFileName(&of);
3080 ret = GetOpenFileName(&of);
3083 if (cmd == IDM_SAVE) {
3086 if ((fp = fopen(filename, "r")) != NULL) {
3087 char buf[256 + FILENAME_MAX];
3091 sprintf(buf, "Are you sure you want to overwrite"
3092 " the file \"%.*s\"?",
3093 FILENAME_MAX, filename);
3094 if (MessageBox(hwnd, buf, "Question",
3095 MB_YESNO | MB_ICONQUESTION)
3100 fp = fopen(filename, "w");
3103 MessageBox(hwnd, "Unable to open save file",
3104 "Error", MB_ICONERROR | MB_OK);
3108 midend_serialise(fe->me, savefile_write, fp);
3112 FILE *fp = fopen(filename, "r");
3113 const char *err = NULL;
3115 midend *me = fe->me;
3121 MessageBox(hwnd, "Unable to open saved game file",
3122 "Error", MB_ICONERROR | MB_OK);
3128 * This save file might be from a different
3131 err = identify_game(&id_name, savefile_read, fp);
3134 for (i = 0; i < gamecount; i++)
3135 if (!strcmp(id_name, gamelist[i]->name))
3137 if (i == gamecount) {
3138 err = "Save file is for a game not "
3139 "supported by this program";
3141 me = midend_for_new_game(fe, gamelist[i], NULL,
3142 FALSE, FALSE, &err_w);
3144 rewind(fp); /* for the actual load */
3150 err = midend_deserialise(me, savefile_read, fp);
3155 MessageBox(hwnd, err, "Error", MB_ICONERROR|MB_OK);
3161 fe_set_midend(fe, me);
3162 new_game_size(fe, 1.0);
3170 start_help(fe, NULL);
3173 assert(help_type != NONE);
3174 start_help(fe, help_type == CHM ?
3175 fe->game->htmlhelp_topic : fe->game->winhelp_topic);
3180 if (wParam >= IDM_GAMES && wParam < (IDM_GAMES + (WPARAM)gamecount)) {
3181 int p = wParam - IDM_GAMES;
3183 fe_set_midend(fe, midend_for_new_game(fe, gamelist[p], NULL,
3184 FALSE, FALSE, &error));
3189 game_params *preset = preset_menu_lookup_by_id(
3191 ((wParam &~ 0xF) - IDM_PRESETS) / 0x10);
3194 midend_set_params(fe->me, preset);
3215 hdc = BeginPaint(hwnd, &p);
3216 hdc2 = CreateCompatibleDC(hdc);
3217 prevbm = SelectObject(hdc2, fe->bitmap);
3219 FillRect(hdc, &(p.rcPaint), (HBRUSH) GetStockObject(WHITE_BRUSH));
3221 IntersectRect(&rcDest, &(fe->bitmapPosition), &(p.rcPaint));
3223 rcDest.left, rcDest.top,
3224 rcDest.right - rcDest.left,
3225 rcDest.bottom - rcDest.top,
3227 rcDest.left - fe->bitmapPosition.left,
3228 rcDest.top - fe->bitmapPosition.top,
3230 SelectObject(hdc2, prevbm);
3239 int r = GetKeyboardState(keystate);
3240 int shift = (r && (keystate[VK_SHIFT] & 0x80)) ? MOD_SHFT : 0;
3241 int ctrl = (r && (keystate[VK_CONTROL] & 0x80)) ? MOD_CTRL : 0;
3245 if (!(lParam & 0x01000000))
3246 key = MOD_NUM_KEYPAD | '4';
3248 key = shift | ctrl | CURSOR_LEFT;
3251 if (!(lParam & 0x01000000))
3252 key = MOD_NUM_KEYPAD | '6';
3254 key = shift | ctrl | CURSOR_RIGHT;
3257 if (!(lParam & 0x01000000))
3258 key = MOD_NUM_KEYPAD | '8';
3260 key = shift | ctrl | CURSOR_UP;
3263 if (!(lParam & 0x01000000))
3264 key = MOD_NUM_KEYPAD | '2';
3266 key = shift | ctrl | CURSOR_DOWN;
3269 * Diagonal keys on the numeric keypad.
3272 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '9';
3275 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '3';
3278 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '7';
3281 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '1';
3284 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '0';
3287 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '5';
3290 * Numeric keypad keys with Num Lock on.
3292 case VK_NUMPAD4: key = MOD_NUM_KEYPAD | '4'; break;
3293 case VK_NUMPAD6: key = MOD_NUM_KEYPAD | '6'; break;
3294 case VK_NUMPAD8: key = MOD_NUM_KEYPAD | '8'; break;
3295 case VK_NUMPAD2: key = MOD_NUM_KEYPAD | '2'; break;
3296 case VK_NUMPAD5: key = MOD_NUM_KEYPAD | '5'; break;
3297 case VK_NUMPAD9: key = MOD_NUM_KEYPAD | '9'; break;
3298 case VK_NUMPAD3: key = MOD_NUM_KEYPAD | '3'; break;
3299 case VK_NUMPAD7: key = MOD_NUM_KEYPAD | '7'; break;
3300 case VK_NUMPAD1: key = MOD_NUM_KEYPAD | '1'; break;
3301 case VK_NUMPAD0: key = MOD_NUM_KEYPAD | '0'; break;
3305 if (!midend_process_key(fe->me, 0, 0, key))
3310 m.message = WM_KEYDOWN;
3312 m.lParam = lParam & 0xdfff;
3313 TranslateMessage(&m);
3317 case WM_LBUTTONDOWN:
3318 case WM_RBUTTONDOWN:
3319 case WM_MBUTTONDOWN:
3324 * Shift-clicks count as middle-clicks, since otherwise
3325 * two-button Windows users won't have any kind of
3326 * middle click to use.
3328 if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT))
3329 button = MIDDLE_BUTTON;
3330 else if (message == WM_RBUTTONDOWN || is_alt_pressed())
3331 button = RIGHT_BUTTON;
3334 button = LEFT_BUTTON;
3336 if ((fe->game->flags & REQUIRE_RBUTTON) == 0)
3337 button = LEFT_BUTTON;
3342 shrgi.cbSize = sizeof(SHRGINFO);
3343 shrgi.hwndClient = hwnd;
3344 shrgi.ptDown.x = (signed short)LOWORD(lParam);
3345 shrgi.ptDown.y = (signed short)HIWORD(lParam);
3346 shrgi.dwFlags = SHRG_RETURNCMD;
3348 if (GN_CONTEXTMENU == SHRecognizeGesture(&shrgi))
3349 button = RIGHT_BUTTON;
3351 button = LEFT_BUTTON;
3355 if (!midend_process_key(fe->me,
3356 (signed short)LOWORD(lParam) - fe->bitmapPosition.left,
3357 (signed short)HIWORD(lParam) - fe->bitmapPosition.top,
3371 * Shift-clicks count as middle-clicks, since otherwise
3372 * two-button Windows users won't have any kind of
3373 * middle click to use.
3375 if (message == WM_MBUTTONUP || (wParam & MK_SHIFT))
3376 button = MIDDLE_RELEASE;
3377 else if (message == WM_RBUTTONUP || is_alt_pressed())
3378 button = RIGHT_RELEASE;
3380 button = LEFT_RELEASE;
3382 if (!midend_process_key(fe->me,
3383 (signed short)LOWORD(lParam) - fe->bitmapPosition.left,
3384 (signed short)HIWORD(lParam) - fe->bitmapPosition.top,
3395 if (wParam & (MK_MBUTTON | MK_SHIFT))
3396 button = MIDDLE_DRAG;
3397 else if (wParam & MK_RBUTTON || is_alt_pressed())
3398 button = RIGHT_DRAG;
3402 if (!midend_process_key(fe->me,
3403 (signed short)LOWORD(lParam) - fe->bitmapPosition.left,
3404 (signed short)HIWORD(lParam) - fe->bitmapPosition.top,
3411 int key = (unsigned char)wParam;
3412 if (key == '\x1A') {
3414 if (GetKeyboardState(keystate) &&
3415 (keystate[VK_SHIFT] & 0x80) &&
3416 (keystate[VK_CONTROL] & 0x80))
3419 if (!midend_process_key(fe->me, 0, 0, key))
3425 DWORD now = GetTickCount();
3426 float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F;
3427 midend_timer(fe->me, elapsed);
3428 fe->timer_last_tickcount = now;
3434 RECT *sr = (RECT *)lParam;
3435 int wx, wy, isedge = 0;
3437 if (wParam == WMSZ_TOP ||
3438 wParam == WMSZ_RIGHT ||
3439 wParam == WMSZ_BOTTOM ||
3440 wParam == WMSZ_LEFT) isedge = 1;
3441 adjust_game_size(fe, sr, isedge, &wx, &wy);
3443 /* Given the window size the puzzles constrain
3444 * us to, work out which edge we should be moving. */
3445 if (wParam == WMSZ_TOP ||
3446 wParam == WMSZ_TOPLEFT ||
3447 wParam == WMSZ_TOPRIGHT) {
3448 sr->top = sr->bottom - wy;
3450 sr->bottom = sr->top + wy;
3452 if (wParam == WMSZ_LEFT ||
3453 wParam == WMSZ_TOPLEFT ||
3454 wParam == WMSZ_BOTTOMLEFT) {
3455 sr->left = sr->right - wx;
3457 sr->right = sr->left + wx;
3465 return DefWindowProc(hwnd, message, wParam, lParam);
3469 static int FindPreviousInstance()
3471 /* Check if application is running. If it's running then focus on the window */
3472 HWND hOtherWnd = NULL;
3474 hOtherWnd = FindWindow (wGameName, wGameName);
3477 SetForegroundWindow (hOtherWnd);
3486 * Split a complete command line into argc/argv, attempting to do it
3487 * exactly the same way the Visual Studio C library would do it (so
3488 * that our console utilities, which receive argc and argv already
3489 * broken apart by the C library, will have their command lines
3490 * processed in the same way as the GUI utilities which get a whole
3491 * command line and must call this function).
3493 * Does not modify the input command line.
3495 * The final parameter (argstart) is used to return a second array
3496 * of char * pointers, the same length as argv, each one pointing
3497 * at the start of the corresponding element of argv in the
3498 * original command line. So if you get half way through processing
3499 * your command line in argc/argv form and then decide you want to
3500 * treat the rest as a raw string, you can. If you don't want to,
3501 * `argstart' can be safely left NULL.
3503 void split_into_argv(char *cmdline, int *argc, char ***argv,
3507 char *outputline, *q;
3508 char **outputargv, **outputargstart;
3512 * These argument-breaking rules apply to Visual Studio 7, which
3513 * is currently the compiler expected to be used for the Windows
3514 * port of my puzzles. Visual Studio 10 has different rules,
3515 * lacking the curious mod 3 behaviour of consecutive quotes
3516 * described below; I presume they fixed a bug. As and when we
3517 * migrate to a newer compiler, we'll have to adjust this to
3518 * match; however, for the moment we faithfully imitate in our GUI
3519 * utilities what our CLI utilities can't be prevented from doing.
3521 * When I investigated this, at first glance the rules appeared to
3524 * - Single quotes are not special characters.
3526 * - Double quotes are removed, but within them spaces cease
3529 * - Backslashes are _only_ special when a sequence of them
3530 * appear just before a double quote. In this situation,
3531 * they are treated like C backslashes: so \" just gives a
3532 * literal quote, \\" gives a literal backslash and then
3533 * opens or closes a double-quoted segment, \\\" gives a
3534 * literal backslash and then a literal quote, \\\\" gives
3535 * two literal backslashes and then opens/closes a
3536 * double-quoted segment, and so forth. Note that this
3537 * behaviour is identical inside and outside double quotes.
3539 * - Two successive double quotes become one literal double
3540 * quote, but only _inside_ a double-quoted segment.
3541 * Outside, they just form an empty double-quoted segment
3542 * (which may cause an empty argument word).
3544 * - That only leaves the interesting question of what happens
3545 * when one or more backslashes precedes two or more double
3546 * quotes, starting inside a double-quoted string. And the
3547 * answer to that appears somewhat bizarre. Here I tabulate
3548 * number of backslashes (across the top) against number of
3549 * quotes (down the left), and indicate how many backslashes
3550 * are output, how many quotes are output, and whether a
3551 * quoted segment is open at the end of the sequence:
3557 * 0 0,0,y | 1,0,y 2,0,y 3,0,y 4,0,y
3558 * --------+-----------------------------
3559 * 1 0,0,n | 0,1,y 1,0,n 1,1,y 2,0,n
3560 * q 2 0,1,n | 0,1,n 1,1,n 1,1,n 2,1,n
3561 * u 3 0,1,y | 0,2,n 1,1,y 1,2,n 2,1,y
3562 * o 4 0,1,n | 0,2,y 1,1,n 1,2,y 2,1,n
3563 * t 5 0,2,n | 0,2,n 1,2,n 1,2,n 2,2,n
3564 * e 6 0,2,y | 0,3,n 1,2,y 1,3,n 2,2,y
3565 * s 7 0,2,n | 0,3,y 1,2,n 1,3,y 2,2,n
3566 * 8 0,3,n | 0,3,n 1,3,n 1,3,n 2,3,n
3567 * 9 0,3,y | 0,4,n 1,3,y 1,4,n 2,3,y
3568 * 10 0,3,n | 0,4,y 1,3,n 1,4,y 2,3,n
3569 * 11 0,4,n | 0,4,n 1,4,n 1,4,n 2,4,n
3572 * [Test fragment was of the form "a\\\"""b c" d.]
3574 * There is very weird mod-3 behaviour going on here in the
3575 * number of quotes, and it even applies when there aren't any
3576 * backslashes! How ghastly.
3578 * With a bit of thought, this extremely odd diagram suddenly
3579 * coalesced itself into a coherent, if still ghastly, model of
3582 * - As before, backslashes are only special when one or more
3583 * of them appear contiguously before at least one double
3584 * quote. In this situation the backslashes do exactly what
3585 * you'd expect: each one quotes the next thing in front of
3586 * it, so you end up with n/2 literal backslashes (if n is
3587 * even) or (n-1)/2 literal backslashes and a literal quote
3588 * (if n is odd). In the latter case the double quote
3589 * character right after the backslashes is used up.
3591 * - After that, any remaining double quotes are processed. A
3592 * string of contiguous unescaped double quotes has a mod-3
3595 * * inside a quoted segment, a quote ends the segment.
3596 * * _immediately_ after ending a quoted segment, a quote
3597 * simply produces a literal quote.
3598 * * otherwise, outside a quoted segment, a quote begins a
3601 * So, for example, if we started inside a quoted segment
3602 * then two contiguous quotes would close the segment and
3603 * produce a literal quote; three would close the segment,
3604 * produce a literal quote, and open a new segment. If we
3605 * started outside a quoted segment, then two contiguous
3606 * quotes would open and then close a segment, producing no
3607 * output (but potentially creating a zero-length argument);
3608 * but three quotes would open and close a segment and then
3609 * produce a literal quote.
3613 * First deal with the simplest of all special cases: if there
3614 * aren't any arguments, return 0,NULL,NULL.
3616 while (*cmdline && isspace(*cmdline)) cmdline++;
3618 if (argc) *argc = 0;
3619 if (argv) *argv = NULL;
3620 if (argstart) *argstart = NULL;
3625 * This will guaranteeably be big enough; we can realloc it
3628 outputline = snewn(1+strlen(cmdline), char);
3629 outputargv = snewn(strlen(cmdline)+1 / 2, char *);
3630 outputargstart = snewn(strlen(cmdline)+1 / 2, char *);
3632 p = cmdline; q = outputline; outputargc = 0;
3637 /* Skip whitespace searching for start of argument. */
3638 while (*p && isspace(*p)) p++;
3641 /* We have an argument; start it. */
3642 outputargv[outputargc] = q;
3643 outputargstart[outputargc] = p;
3647 /* Copy data into the argument until it's finished. */
3649 if (!quote && isspace(*p))
3650 break; /* argument is finished */
3652 if (*p == '"' || *p == '\\') {
3654 * We have a sequence of zero or more backslashes
3655 * followed by a sequence of zero or more quotes.
3656 * Count up how many of each, and then deal with
3657 * them as appropriate.
3659 int i, slashes = 0, quotes = 0;
3660 while (*p == '\\') slashes++, p++;
3661 while (*p == '"') quotes++, p++;
3665 * Special case: if there are no quotes,
3666 * slashes are not special at all, so just copy
3667 * n slashes to the output string.
3669 while (slashes--) *q++ = '\\';
3671 /* Slashes annihilate in pairs. */
3672 while (slashes >= 2) slashes -= 2, *q++ = '\\';
3674 /* One remaining slash takes out the first quote. */
3675 if (slashes) quotes--, *q++ = '"';
3678 /* Outside a quote segment, a quote starts one. */
3679 if (!quote) quotes--, quote = 1;
3681 /* Now we produce (n+1)/3 literal quotes... */
3682 for (i = 3; i <= quotes+1; i += 3) *q++ = '"';
3684 /* ... and end in a quote segment iff 3 divides n. */
3685 quote = (quotes % 3 == 0);
3693 /* At the end of an argument, just append a trailing NUL. */
3697 outputargv = sresize(outputargv, outputargc, char *);
3698 outputargstart = sresize(outputargstart, outputargc, char *);
3700 if (argc) *argc = outputargc;
3701 if (argv) *argv = outputargv; else sfree(outputargv);
3702 if (argstart) *argstart = outputargstart; else sfree(outputargstart);
3705 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
3715 split_into_argv(cmdline, &argc, &argv, NULL);
3718 MultiByteToWideChar (CP_ACP, 0, CLASSNAME, -1, wClassName, 256);
3719 if (FindPreviousInstance ())
3723 InitCommonControls();
3729 wndclass.lpfnWndProc = WndProc;
3730 wndclass.cbClsExtra = 0;
3731 wndclass.cbWndExtra = 0;
3732 wndclass.hInstance = inst;
3733 wndclass.hIcon = LoadIcon(inst, MAKEINTRESOURCE(200));
3735 if (!wndclass.hIcon) /* in case resource file is absent */
3736 wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION);
3738 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
3739 wndclass.hbrBackground = NULL;
3740 wndclass.lpszMenuName = NULL;
3742 wndclass.lpszClassName = wClassName;
3744 wndclass.lpszClassName = CLASSNAME;
3747 RegisterClass(&wndclass);
3750 while (*cmdline && isspace((unsigned char)*cmdline))
3759 for (i = 0; i < gamecount; i++) {
3760 const char *p = gamelist[i]->name;
3763 if (isspace((unsigned char)*p)) {
3764 while (*q && isspace((unsigned char)*q))
3767 if (tolower((unsigned char)*p) !=
3768 tolower((unsigned char)*q))
3786 fe = frontend_new(inst);
3787 me = midend_for_new_game(fe, gg, argc > 0 ? argv[0] : NULL,
3788 TRUE, TRUE, &error);
3792 sprintf(buf, "Puzzles Error");
3794 sprintf(buf, "%.100s Error", gg->name);
3796 MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
3800 fe_set_midend(fe, me);
3803 while (GetMessage(&msg, NULL, 0, 0)) {
3804 DispatchMessage(&msg);
3807 DestroyWindow(fe->hwnd);
3812 /* vim: set shiftwidth=4 tabstop=8: */