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
48 #define IDM_KEYEMUL 0x0400
50 #define HELP_FILE_NAME "puzzles.hlp"
51 #define HELP_CNT_NAME "puzzles.cnt"
53 #define CHM_FILE_NAME "puzzles.chm"
54 #endif /* NO_HTMLHELP */
57 typedef HWND (CALLBACK *htmlhelp_t)(HWND, LPCSTR, UINT, DWORD);
58 static htmlhelp_t htmlhelp;
59 static HINSTANCE hh_dll;
60 #endif /* NO_HTMLHELP */
61 enum { NONE, HLP, CHM } help_type;
63 const char *help_topic;
64 int help_has_contents;
67 #define FILENAME_MAX (260)
71 #define HGDI_ERROR ((HANDLE)GDI_ERROR)
77 * Wrapper implementations of functions not supplied by the
81 #define SHGetSubMenu(hWndMB,ID_MENU) (HMENU)SendMessage((hWndMB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)ID_MENU)
85 int MessageBox(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType)
90 MultiByteToWideChar (CP_ACP, 0, lpText, -1, wText, 2048);
91 MultiByteToWideChar (CP_ACP, 0, lpCaption, -1, wCaption, 2048);
93 return MessageBoxW (hWnd, wText, wCaption, uType);
96 BOOL SetDlgItemTextA(HWND hDlg, int nIDDlgItem, LPCSTR lpString)
100 MultiByteToWideChar (CP_ACP, 0, lpString, -1, wText, 256);
101 return SetDlgItemTextW(hDlg, nIDDlgItem, wText);
104 LPCSTR getenv(LPCSTR buf)
109 BOOL GetKeyboardState(PBYTE pb)
114 static TCHAR wGameName[256];
119 static FILE *debug_fp = NULL;
120 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
121 static int debug_got_console = 0;
123 void dputs(char *buf)
127 if (!debug_got_console) {
128 if (AllocConsole()) {
129 debug_got_console = 1;
130 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
134 debug_fp = fopen("debug.log", "w");
137 if (debug_hdl != INVALID_HANDLE_VALUE) {
138 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
140 fputs(buf, debug_fp);
142 OutputDebugString(buf);
145 void debug_printf(char *fmt, ...)
151 _vsnprintf(buf, 4095, fmt, ap);
158 #define WINFLAGS (WS_OVERLAPPEDWINDOW &~ \
159 (WS_MAXIMIZEBOX | WS_OVERLAPPED))
161 #define WINFLAGS (WS_CAPTION | WS_SYSMENU)
164 static void new_game_size(frontend *fe);
182 enum { CFG_PRINT = CFG_FRONTEND_SPECIFIC };
186 HWND hwnd, statusbar, cfgbox;
188 HWND numpad; /* window handle for the numeric pad */
191 HBITMAP bitmap, prevbm;
192 RECT bitmapPosition; /* game bitmap position within game window */
199 DWORD timer_last_tickcount;
201 game_params **presets;
203 int nfonts, fontsize;
205 struct cfg_aux *cfgaux;
206 int cfg_which, dlg_done;
211 enum { DRAWING, PRINTING, NOTHING } drawstatus;
213 int printcount, printw, printh, printsolns, printcurr, printcolour;
215 int printoffsetx, printoffsety;
216 float printpixelscale;
223 void fatal(char *fmt, ...)
229 vsprintf(buf, fmt, ap);
232 MessageBox(NULL, buf, "Fatal error", MB_ICONEXCLAMATION | MB_OK);
237 char *geterrstr(void)
240 DWORD dw = GetLastError();
244 FORMAT_MESSAGE_ALLOCATE_BUFFER |
245 FORMAT_MESSAGE_FROM_SYSTEM,
248 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
252 ret = dupstr(lpMsgBuf);
259 void get_random_seed(void **randseed, int *randseedsize)
261 SYSTEMTIME *st = snew(SYSTEMTIME);
265 *randseed = (void *)st;
266 *randseedsize = sizeof(SYSTEMTIME);
269 static void win_status_bar(void *handle, char *text)
274 frontend *fe = (frontend *)handle;
277 MultiByteToWideChar (CP_ACP, 0, text, -1, wText, 255);
278 SendMessage(fe->statusbar, SB_SETTEXT,
279 (WPARAM) 255 | SBT_NOBORDERS,
282 SetWindowText(fe->statusbar, text);
286 static blitter *win_blitter_new(void *handle, int w, int h)
288 blitter *bl = snew(blitter);
290 memset(bl, 0, sizeof(blitter));
298 static void win_blitter_free(void *handle, blitter *bl)
300 if (bl->bitmap) DeleteObject(bl->bitmap);
304 static void blitter_mkbitmap(frontend *fe, blitter *bl)
306 HDC hdc = GetDC(fe->hwnd);
307 bl->bitmap = CreateCompatibleBitmap(hdc, bl->w, bl->h);
308 ReleaseDC(fe->hwnd, hdc);
311 /* BitBlt(dstDC, dstX, dstY, dstW, dstH, srcDC, srcX, srcY, dType) */
313 static void win_blitter_save(void *handle, blitter *bl, int x, int y)
315 frontend *fe = (frontend *)handle;
316 HDC hdc_win, hdc_blit;
319 assert(fe->drawstatus == DRAWING);
321 if (!bl->bitmap) blitter_mkbitmap(fe, bl);
323 bl->x = x; bl->y = y;
325 hdc_win = GetDC(fe->hwnd);
326 hdc_blit = CreateCompatibleDC(hdc_win);
327 if (!hdc_blit) fatal("hdc_blit failed: 0x%x", GetLastError());
329 prev_blit = SelectObject(hdc_blit, bl->bitmap);
330 if (prev_blit == NULL || prev_blit == HGDI_ERROR)
331 fatal("SelectObject for hdc_main failed: 0x%x", GetLastError());
333 if (!BitBlt(hdc_blit, 0, 0, bl->w, bl->h,
334 fe->hdc, x, y, SRCCOPY))
335 fatal("BitBlt failed: 0x%x", GetLastError());
337 SelectObject(hdc_blit, prev_blit);
339 ReleaseDC(fe->hwnd, hdc_win);
342 static void win_blitter_load(void *handle, blitter *bl, int x, int y)
344 frontend *fe = (frontend *)handle;
345 HDC hdc_win, hdc_blit;
348 assert(fe->drawstatus == DRAWING);
350 assert(bl->bitmap); /* we should always have saved before loading */
352 if (x == BLITTER_FROMSAVED) x = bl->x;
353 if (y == BLITTER_FROMSAVED) y = bl->y;
355 hdc_win = GetDC(fe->hwnd);
356 hdc_blit = CreateCompatibleDC(hdc_win);
358 prev_blit = SelectObject(hdc_blit, bl->bitmap);
360 BitBlt(fe->hdc, x, y, bl->w, bl->h,
361 hdc_blit, 0, 0, SRCCOPY);
363 SelectObject(hdc_blit, prev_blit);
365 ReleaseDC(fe->hwnd, hdc_win);
368 void frontend_default_colour(frontend *fe, float *output)
370 DWORD c = GetSysColor(COLOR_MENU); /* ick */
372 output[0] = (float)(GetRValue(c) / 255.0);
373 output[1] = (float)(GetGValue(c) / 255.0);
374 output[2] = (float)(GetBValue(c) / 255.0);
377 static POINT win_transform_point(frontend *fe, int x, int y)
381 assert(fe->drawstatus != NOTHING);
383 if (fe->drawstatus == PRINTING) {
384 ret.x = (int)(fe->printoffsetx + fe->printpixelscale * x);
385 ret.y = (int)(fe->printoffsety + fe->printpixelscale * y);
394 static void win_text_colour(frontend *fe, int colour)
396 assert(fe->drawstatus != NOTHING);
398 if (fe->drawstatus == PRINTING) {
401 print_get_colour(fe->dr, colour, &hatch, &r, &g, &b);
403 SetTextColor(fe->hdc, RGB(r * 255, g * 255, b * 255));
405 SetTextColor(fe->hdc,
406 hatch == HATCH_CLEAR ? RGB(255,255,255) : RGB(0,0,0));
408 SetTextColor(fe->hdc, fe->colours[colour]);
412 static void win_set_brush(frontend *fe, int colour)
415 assert(fe->drawstatus != NOTHING);
417 if (fe->drawstatus == PRINTING) {
420 print_get_colour(fe->dr, colour, &hatch, &r, &g, &b);
422 if (fe->printcolour) {
423 br = CreateSolidBrush(RGB(r * 255, g * 255, b * 255));
424 } else if (hatch == HATCH_SOLID) {
425 br = CreateSolidBrush(RGB(0,0,0));
426 } else if (hatch == HATCH_CLEAR) {
427 br = CreateSolidBrush(RGB(255,255,255));
431 * This is only ever required during printing, and the
432 * PocketPC port doesn't support printing.
434 fatal("CreateHatchBrush not supported");
436 br = CreateHatchBrush(hatch == HATCH_BACKSLASH ? HS_FDIAGONAL :
437 hatch == HATCH_SLASH ? HS_BDIAGONAL :
438 hatch == HATCH_HORIZ ? HS_HORIZONTAL :
439 hatch == HATCH_VERT ? HS_VERTICAL :
440 hatch == HATCH_PLUS ? HS_CROSS :
441 /* hatch == HATCH_X ? */ HS_DIAGCROSS,
446 br = fe->brushes[colour];
448 fe->oldbr = SelectObject(fe->hdc, br);
451 static void win_reset_brush(frontend *fe)
455 assert(fe->drawstatus != NOTHING);
457 br = SelectObject(fe->hdc, fe->oldbr);
458 if (fe->drawstatus == PRINTING)
462 static void win_set_pen(frontend *fe, int colour, int thin)
465 assert(fe->drawstatus != NOTHING);
467 if (fe->drawstatus == PRINTING) {
470 int width = thin ? 0 : fe->linewidth;
472 print_get_colour(fe->dr, colour, &hatch, &r, &g, &b);
474 pen = CreatePen(PS_SOLID, width,
475 RGB(r * 255, g * 255, b * 255));
476 else if (hatch == HATCH_SOLID)
477 pen = CreatePen(PS_SOLID, width, RGB(0, 0, 0));
478 else if (hatch == HATCH_CLEAR)
479 pen = CreatePen(PS_SOLID, width, RGB(255,255,255));
481 assert(!"This shouldn't happen");
482 pen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
485 pen = fe->pens[colour];
487 fe->oldpen = SelectObject(fe->hdc, pen);
490 static void win_reset_pen(frontend *fe)
494 assert(fe->drawstatus != NOTHING);
496 pen = SelectObject(fe->hdc, fe->oldpen);
497 if (fe->drawstatus == PRINTING)
501 static void win_clip(void *handle, int x, int y, int w, int h)
503 frontend *fe = (frontend *)handle;
506 if (fe->drawstatus == NOTHING)
509 p = win_transform_point(fe, x, y);
510 q = win_transform_point(fe, x+w, y+h);
511 IntersectClipRect(fe->hdc, p.x, p.y, q.x, q.y);
514 static void win_unclip(void *handle)
516 frontend *fe = (frontend *)handle;
518 if (fe->drawstatus == NOTHING)
521 SelectClipRgn(fe->hdc, NULL);
524 static void win_draw_text(void *handle, int x, int y, int fonttype,
525 int fontsize, int align, int colour, char *text)
527 frontend *fe = (frontend *)handle;
532 if (fe->drawstatus == NOTHING)
535 if (fe->drawstatus == PRINTING)
536 fontsize = (int)(fontsize * fe->printpixelscale);
538 xy = win_transform_point(fe, x, y);
541 * Find or create the font.
543 for (i = fe->fontstart; i < fe->nfonts; i++)
544 if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
547 if (i == fe->nfonts) {
548 if (fe->fontsize <= fe->nfonts) {
549 fe->fontsize = fe->nfonts + 10;
550 fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
555 fe->fonts[i].type = fonttype;
556 fe->fonts[i].size = fontsize;
558 memset (&lf, 0, sizeof(LOGFONT));
559 lf.lfHeight = -fontsize;
560 lf.lfWeight = (fe->drawstatus == PRINTING ? 0 : FW_BOLD);
561 lf.lfCharSet = DEFAULT_CHARSET;
562 lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
563 lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
564 lf.lfQuality = DEFAULT_QUALITY;
565 lf.lfPitchAndFamily = (fonttype == FONT_FIXED ?
566 FIXED_PITCH | FF_DONTCARE :
567 VARIABLE_PITCH | FF_SWISS);
569 wcscpy(lf.lfFaceName, TEXT("Tahoma"));
572 fe->fonts[i].font = CreateFontIndirect(&lf);
576 * Position and draw the text.
584 MultiByteToWideChar (CP_ACP, 0, text, -1, wText, 256);
587 oldfont = SelectObject(fe->hdc, fe->fonts[i].font);
588 if (GetTextMetrics(fe->hdc, &tm)) {
589 if (align & ALIGN_VCENTRE)
590 xy.y -= (tm.tmAscent+tm.tmDescent)/2;
595 if (GetTextExtentPoint32(fe->hdc, text, strlen(text), &size)) {
597 if (GetTextExtentPoint32(fe->hdc, wText, wcslen(wText), &size)) {
599 if (align & ALIGN_HCENTRE)
601 else if (align & ALIGN_HRIGHT)
604 SetBkMode(fe->hdc, TRANSPARENT);
605 win_text_colour(fe, colour);
607 TextOut(fe->hdc, xy.x, xy.y, text, strlen(text));
609 ExtTextOut(fe->hdc, xy.x, xy.y, 0, NULL, wText, wcslen(wText), NULL);
611 SelectObject(fe->hdc, oldfont);
615 static void win_draw_rect(void *handle, int x, int y, int w, int h, int colour)
617 frontend *fe = (frontend *)handle;
620 if (fe->drawstatus == NOTHING)
623 if (fe->drawstatus == DRAWING && w == 1 && h == 1) {
625 * Rectangle() appears to get uppity if asked to draw a 1x1
626 * rectangle, presumably on the grounds that that's beneath
627 * its dignity and you ought to be using SetPixel instead.
630 SetPixel(fe->hdc, x, y, fe->colours[colour]);
632 win_set_brush(fe, colour);
633 win_set_pen(fe, colour, TRUE);
634 p = win_transform_point(fe, x, y);
635 q = win_transform_point(fe, x+w, y+h);
636 Rectangle(fe->hdc, p.x, p.y, q.x, q.y);
642 static void win_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour)
644 frontend *fe = (frontend *)handle;
647 if (fe->drawstatus == NOTHING)
650 win_set_pen(fe, colour, FALSE);
651 pp[0] = win_transform_point(fe, x1, y1);
652 pp[1] = win_transform_point(fe, x2, y2);
653 Polyline(fe->hdc, pp, 2);
654 if (fe->drawstatus == DRAWING)
655 SetPixel(fe->hdc, pp[1].x, pp[1].y, fe->colours[colour]);
659 static void win_draw_circle(void *handle, int cx, int cy, int radius,
660 int fillcolour, int outlinecolour)
662 frontend *fe = (frontend *)handle;
665 assert(outlinecolour >= 0);
667 if (fe->drawstatus == NOTHING)
671 win_set_brush(fe, fillcolour);
673 fe->oldbr = SelectObject(fe->hdc, GetStockObject(NULL_BRUSH));
675 win_set_pen(fe, outlinecolour, FALSE);
676 p = win_transform_point(fe, cx - radius, cy - radius);
677 q = win_transform_point(fe, cx + radius, cy + radius);
678 Ellipse(fe->hdc, p.x, p.y, q.x+1, q.y+1);
683 static void win_draw_polygon(void *handle, int *coords, int npoints,
684 int fillcolour, int outlinecolour)
686 frontend *fe = (frontend *)handle;
690 if (fe->drawstatus == NOTHING)
693 pts = snewn(npoints+1, POINT);
695 for (i = 0; i <= npoints; i++) {
696 int j = (i < npoints ? i : 0);
697 pts[i] = win_transform_point(fe, coords[j*2], coords[j*2+1]);
700 assert(outlinecolour >= 0);
702 if (fillcolour >= 0) {
703 win_set_brush(fe, fillcolour);
704 win_set_pen(fe, outlinecolour, FALSE);
705 Polygon(fe->hdc, pts, npoints);
709 win_set_pen(fe, outlinecolour, FALSE);
710 Polyline(fe->hdc, pts, npoints+1);
717 static void win_start_draw(void *handle)
719 frontend *fe = (frontend *)handle;
722 assert(fe->drawstatus == NOTHING);
724 hdc_win = GetDC(fe->hwnd);
725 fe->hdc = CreateCompatibleDC(hdc_win);
726 fe->prevbm = SelectObject(fe->hdc, fe->bitmap);
727 ReleaseDC(fe->hwnd, hdc_win);
730 SetMapMode(fe->hdc, MM_TEXT);
732 fe->drawstatus = DRAWING;
735 static void win_draw_update(void *handle, int x, int y, int w, int h)
737 frontend *fe = (frontend *)handle;
740 if (fe->drawstatus != DRAWING)
748 OffsetRect(&r, fe->bitmapPosition.left, fe->bitmapPosition.top);
749 InvalidateRect(fe->hwnd, &r, FALSE);
752 static void win_end_draw(void *handle)
754 frontend *fe = (frontend *)handle;
755 assert(fe->drawstatus == DRAWING);
756 SelectObject(fe->hdc, fe->prevbm);
759 DeleteObject(fe->clip);
762 fe->drawstatus = NOTHING;
765 static void win_line_width(void *handle, float width)
767 frontend *fe = (frontend *)handle;
769 assert(fe->drawstatus != DRAWING);
770 if (fe->drawstatus == NOTHING)
773 fe->linewidth = (int)(width * fe->printpixelscale);
776 static void win_begin_doc(void *handle, int pages)
778 frontend *fe = (frontend *)handle;
780 assert(fe->drawstatus != DRAWING);
781 if (fe->drawstatus == NOTHING)
784 if (StartDoc(fe->hdc, &fe->di) <= 0) {
785 char *e = geterrstr();
786 MessageBox(fe->hwnd, e, "Error starting to print",
787 MB_ICONERROR | MB_OK);
789 fe->drawstatus = NOTHING;
793 * Push a marker on the font stack so that we won't use the
794 * same fonts for printing and drawing. (This is because
795 * drawing seems to look generally better in bold, but printing
796 * is better not in bold.)
798 fe->fontstart = fe->nfonts;
801 static void win_begin_page(void *handle, int number)
803 frontend *fe = (frontend *)handle;
805 assert(fe->drawstatus != DRAWING);
806 if (fe->drawstatus == NOTHING)
809 if (StartPage(fe->hdc) <= 0) {
810 char *e = geterrstr();
811 MessageBox(fe->hwnd, e, "Error starting a page",
812 MB_ICONERROR | MB_OK);
814 fe->drawstatus = NOTHING;
818 static void win_begin_puzzle(void *handle, float xm, float xc,
819 float ym, float yc, int pw, int ph, float wmm)
821 frontend *fe = (frontend *)handle;
822 int ppw, pph, pox, poy;
823 float mmpw, mmph, mmox, mmoy;
826 assert(fe->drawstatus != DRAWING);
827 if (fe->drawstatus == NOTHING)
830 ppw = GetDeviceCaps(fe->hdc, HORZRES);
831 pph = GetDeviceCaps(fe->hdc, VERTRES);
832 mmpw = (float)GetDeviceCaps(fe->hdc, HORZSIZE);
833 mmph = (float)GetDeviceCaps(fe->hdc, VERTSIZE);
836 * Compute the puzzle's position on the logical page.
838 mmox = xm * mmpw + xc;
839 mmoy = ym * mmph + yc;
842 * Work out what that comes to in pixels.
844 pox = (int)(mmox * (float)ppw / mmpw);
845 poy = (int)(mmoy * (float)ppw / mmpw);
848 * And determine the scale.
850 * I need a scale such that the maximum puzzle-coordinate
851 * extent of the rectangle (pw * scale) is equal to the pixel
852 * equivalent of the puzzle's millimetre width (wmm * ppw /
855 scale = (wmm * ppw) / (mmpw * pw);
858 * Now store pox, poy and scale for use in the main drawing
861 fe->printoffsetx = pox;
862 fe->printoffsety = poy;
863 fe->printpixelscale = scale;
868 static void win_end_puzzle(void *handle)
870 /* Nothing needs to be done here. */
873 static void win_end_page(void *handle, int number)
875 frontend *fe = (frontend *)handle;
877 assert(fe->drawstatus != DRAWING);
879 if (fe->drawstatus == NOTHING)
882 if (EndPage(fe->hdc) <= 0) {
883 char *e = geterrstr();
884 MessageBox(fe->hwnd, e, "Error finishing a page",
885 MB_ICONERROR | MB_OK);
887 fe->drawstatus = NOTHING;
891 static void win_end_doc(void *handle)
893 frontend *fe = (frontend *)handle;
895 assert(fe->drawstatus != DRAWING);
898 * Free all the fonts created since we began printing.
900 while (fe->nfonts > fe->fontstart) {
902 DeleteObject(fe->fonts[fe->nfonts].font);
907 * The MSDN web site sample code doesn't bother to call EndDoc
908 * if an error occurs half way through printing. I expect doing
909 * so would cause the erroneous document to actually be
910 * printed, or something equally undesirable.
912 if (fe->drawstatus == NOTHING)
915 if (EndDoc(fe->hdc) <= 0) {
916 char *e = geterrstr();
917 MessageBox(fe->hwnd, e, "Error finishing printing",
918 MB_ICONERROR | MB_OK);
920 fe->drawstatus = NOTHING;
924 const struct drawing_api win_drawing = {
949 void print(frontend *fe)
955 midend *nme = NULL; /* non-interactive midend for bulk puzzle generation */
960 * Create our document structure and fill it up with puzzles.
962 doc = document_new(fe->printw, fe->printh, fe->printscale / 100.0F);
963 for (i = 0; i < fe->printcount; i++) {
964 if (i == 0 && fe->printcurr) {
965 err = midend_print_puzzle(fe->me, doc, fe->printsolns);
970 nme = midend_new(NULL, &thegame, NULL, NULL);
973 * Set the non-interactive mid-end to have the same
974 * parameters as the standard one.
976 params = midend_get_params(fe->me);
977 midend_set_params(nme, params);
978 thegame.free_params(params);
981 midend_new_game(nme);
982 err = midend_print_puzzle(nme, doc, fe->printsolns);
991 MessageBox(fe->hwnd, err, "Error preparing puzzles for printing",
992 MB_ICONERROR | MB_OK);
997 memset(&pd, 0, sizeof(pd));
998 pd.lStructSize = sizeof(pd);
999 pd.hwndOwner = fe->hwnd;
1001 pd.hDevNames = NULL;
1002 pd.Flags = PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC |
1003 PD_NOPAGENUMS | PD_NOSELECTION;
1005 pd.nFromPage = pd.nToPage = 0xFFFF;
1006 pd.nMinPage = pd.nMaxPage = 1;
1008 if (!PrintDlg(&pd)) {
1014 * Now pd.hDC is a device context for the printer.
1018 * FIXME: IWBNI we put up an Abort box here.
1021 memset(&fe->di, 0, sizeof(fe->di));
1022 fe->di.cbSize = sizeof(fe->di);
1023 sprintf(doctitle, "Printed puzzles from %s (from Simon Tatham's"
1024 " Portable Puzzle Collection)", thegame.name);
1025 fe->di.lpszDocName = doctitle;
1026 fe->di.lpszOutput = NULL;
1027 fe->di.lpszDatatype = NULL;
1030 fe->drawstatus = PRINTING;
1033 fe->dr = drawing_new(&win_drawing, NULL, fe);
1034 document_print(doc, fe->dr);
1035 drawing_free(fe->dr);
1038 fe->drawstatus = NOTHING;
1045 void deactivate_timer(frontend *fe)
1048 return; /* for non-interactive midend */
1049 if (fe->hwnd) KillTimer(fe->hwnd, fe->timer);
1053 void activate_timer(frontend *fe)
1056 return; /* for non-interactive midend */
1058 fe->timer = SetTimer(fe->hwnd, 1, 20, NULL);
1059 fe->timer_last_tickcount = GetTickCount();
1063 void write_clip(HWND hwnd, char *data)
1071 * Windows expects CRLF in the clipboard, so we must convert
1072 * any \n that has come out of the puzzle backend.
1075 for (i = 0; data[i]; i++) {
1076 if (data[i] == '\n')
1080 data2 = snewn(len+1, char);
1082 for (i = 0; data[i]; i++) {
1083 if (data[i] == '\n')
1085 data2[j++] = data[i];
1090 clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
1093 lock = GlobalLock(clipdata);
1096 memcpy(lock, data2, len);
1097 ((unsigned char *) lock)[len] = 0;
1098 GlobalUnlock(clipdata);
1100 if (OpenClipboard(hwnd)) {
1102 SetClipboardData(CF_TEXT, clipdata);
1105 GlobalFree(clipdata);
1111 * Set up Help and see if we can find a help file.
1113 static void init_help(void)
1116 char b[2048], *p, *q, *r;
1120 * Find the executable file path, so we can look alongside
1121 * it for help files. Trim the filename off the end.
1123 GetModuleFileName(NULL, b, sizeof(b) - 1);
1125 p = strrchr(b, '\\');
1126 if (p && p >= r) r = p+1;
1127 q = strrchr(b, ':');
1128 if (q && q >= r) r = q+1;
1132 * Try HTML Help first.
1134 strcpy(r, CHM_FILE_NAME);
1135 if ( (fp = fopen(b, "r")) != NULL) {
1139 * We have a .CHM. See if we can use it.
1141 hh_dll = LoadLibrary("hhctrl.ocx");
1143 htmlhelp = (htmlhelp_t)GetProcAddress(hh_dll, "HtmlHelpA");
1145 FreeLibrary(hh_dll);
1148 help_path = dupstr(b);
1150 help_topic = thegame.htmlhelp_topic;
1154 #endif /* NO_HTMLHELP */
1157 * Now try old-style .HLP.
1159 strcpy(r, HELP_FILE_NAME);
1160 if ( (fp = fopen(b, "r")) != NULL) {
1163 help_path = dupstr(b);
1166 help_topic = thegame.winhelp_topic;
1169 * See if there's a .CNT file alongside it.
1171 strcpy(r, HELP_CNT_NAME);
1172 if ( (fp = fopen(b, "r")) != NULL) {
1174 help_has_contents = TRUE;
1176 help_has_contents = FALSE;
1181 help_type = NONE; /* didn't find any */
1190 static void start_help(frontend *fe, const char *topic)
1195 switch (help_type) {
1199 str = snewn(10+strlen(topic), char);
1200 sprintf(str, "JI(`',`%s')", topic);
1202 } else if (help_has_contents) {
1205 cmd = HELP_CONTENTS;
1207 WinHelp(fe->hwnd, help_path, cmd, (DWORD)str);
1208 fe->help_running = TRUE;
1215 str = snewn(20 + strlen(topic) + strlen(help_path), char);
1216 sprintf(str, "%s::/%s.html>main", help_path, topic);
1218 str = dupstr(help_path);
1220 htmlhelp(fe->hwnd, str, HH_DISPLAY_TOPIC, 0);
1221 fe->help_running = TRUE;
1223 #endif /* NO_HTMLHELP */
1225 assert(!"This shouldn't happen");
1233 * Stop Help on window cleanup.
1235 static void stop_help(frontend *fe)
1237 if (fe->help_running) {
1238 switch (help_type) {
1240 WinHelp(fe->hwnd, help_path, HELP_QUIT, 0);
1245 htmlhelp(NULL, NULL, HH_CLOSE_ALL, 0);
1247 #endif /* NO_HTMLHELP */
1249 assert(!"This shouldn't happen");
1252 fe->help_running = FALSE;
1259 * Terminate Help on process exit.
1261 static void cleanup_help(void)
1263 /* Nothing to do currently.
1264 * (If we were running HTML Help single-threaded, this is where we'd
1265 * call HH_UNINITIALIZE.) */
1268 static int get_statusbar_height(frontend *fe)
1271 if (fe->statusbar) {
1273 GetWindowRect(fe->statusbar, &sr);
1274 sy = sr.bottom - sr.top;
1281 static void adjust_statusbar(frontend *fe, RECT *r)
1285 if (!fe->statusbar) return;
1287 sy = get_statusbar_height(fe);
1289 SetWindowPos(fe->statusbar, NULL, 0, r->bottom-r->top-sy, r->right-r->left,
1294 static void get_menu_size(HWND wh, RECT *r)
1296 HMENU bar = GetMenu(wh);
1300 SetRect(r, 0, 0, 0, 0);
1301 for (i = 0; i < GetMenuItemCount(bar); i++) {
1302 GetMenuItemRect(wh, bar, i, &rect);
1303 UnionRect(r, r, &rect);
1308 * Given a proposed new puzzle size (cx,cy), work out the actual
1309 * puzzle size that would be (px,py) and the window size including
1310 * furniture (wx,wy).
1313 static int check_window_resize(frontend *fe, int cx, int cy,
1315 int *wx, int *wy, int resize)
1318 int x, y, sy = get_statusbar_height(fe), changed = 0;
1320 /* disallow making window thinner than menu bar */
1321 x = max(cx, fe->xmin);
1322 y = max(cy - sy, fe->ymin);
1325 * See if we actually got the window size we wanted, and adjust
1326 * the puzzle size if not.
1328 midend_size(fe->me, &x, &y, resize);
1329 if (x != cx || y != cy) {
1331 * Resize the window, now we know what size we _really_
1337 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1338 *wx = r.right - r.left;
1339 *wy = r.bottom - r.top;
1350 * Given the current window size, make sure it's sane for the
1351 * current puzzle and resize if necessary.
1354 static void check_window_size(frontend *fe, int *px, int *py)
1359 GetClientRect(fe->hwnd, &r);
1360 cx = r.right - r.left;
1361 cy = r.bottom - r.top;
1363 if (check_window_resize(fe, cx, cy, px, py, &wx, &wy, FALSE)) {
1365 SetWindowPos(fe->hwnd, NULL, 0, 0, wx, wy,
1366 SWP_NOMOVE | SWP_NOZORDER);
1371 GetClientRect(fe->hwnd, &r);
1372 adjust_statusbar(fe, &r);
1375 static void get_max_puzzle_size(frontend *fe, int *x, int *y)
1379 if (SystemParametersInfo(SPI_GETWORKAREA, 0, &sr, FALSE)) {
1380 *x = sr.right - sr.left;
1381 *y = sr.bottom - sr.top;
1386 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1387 *x -= r.right - r.left - 100;
1388 *y -= r.bottom - r.top - 100;
1393 if (fe->statusbar != NULL) {
1394 GetWindowRect(fe->statusbar, &sr);
1395 *y -= sr.bottom - sr.top;
1400 /* Toolbar buttons on the numeric pad */
1401 static TBBUTTON tbNumpadButtons[] =
1403 {0, IDM_KEYEMUL + '1', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1404 {1, IDM_KEYEMUL + '2', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1405 {2, IDM_KEYEMUL + '3', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1406 {3, IDM_KEYEMUL + '4', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1407 {4, IDM_KEYEMUL + '5', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1408 {5, IDM_KEYEMUL + '6', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1409 {6, IDM_KEYEMUL + '7', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1410 {7, IDM_KEYEMUL + '8', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1411 {8, IDM_KEYEMUL + '9', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1412 {9, IDM_KEYEMUL + ' ', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1}
1416 static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
1422 fe = snew(frontend);
1424 fe->me = midend_new(fe, &thegame, &win_drawing, fe);
1427 *error = midend_game_id(fe->me, game_id);
1429 midend_free(fe->me);
1440 fe->help_running = FALSE;
1442 fe->drawstatus = NOTHING;
1446 midend_new_game(fe->me);
1449 fe->nfonts = fe->fontsize = 0;
1455 colours = midend_colours(fe->me, &ncolours);
1457 fe->colours = snewn(ncolours, COLORREF);
1458 fe->brushes = snewn(ncolours, HBRUSH);
1459 fe->pens = snewn(ncolours, HPEN);
1461 for (i = 0; i < ncolours; i++) {
1462 fe->colours[i] = RGB(255 * colours[i*3+0],
1463 255 * colours[i*3+1],
1464 255 * colours[i*3+2]);
1465 fe->brushes[i] = CreateSolidBrush(fe->colours[i]);
1466 fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]);
1471 if (midend_wants_statusbar(fe->me)) {
1472 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, TEXT("ooh"),
1473 WS_CHILD | WS_VISIBLE,
1474 0, 0, 0, 0, /* status bar does these */
1475 NULL, NULL, inst, NULL);
1477 fe->statusbar = NULL;
1479 get_max_puzzle_size(fe, &x, &y);
1480 midend_size(fe->me, &x, &y, FALSE);
1485 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1488 fe->hwnd = CreateWindowEx(0, wGameName, wGameName,
1490 CW_USEDEFAULT, CW_USEDEFAULT,
1491 CW_USEDEFAULT, CW_USEDEFAULT,
1492 NULL, NULL, inst, NULL);
1496 RECT rc, rcBar, rcTB, rcClient;
1498 memset (&mbi, 0, sizeof(SHMENUBARINFO));
1499 mbi.cbSize = sizeof(SHMENUBARINFO);
1500 mbi.hwndParent = fe->hwnd;
1501 mbi.nToolBarId = IDR_MENUBAR1;
1502 mbi.hInstRes = inst;
1504 SHCreateMenuBar(&mbi);
1506 GetWindowRect(fe->hwnd, &rc);
1507 GetWindowRect(mbi.hwndMB, &rcBar);
1508 rc.bottom -= rcBar.bottom - rcBar.top;
1509 MoveWindow(fe->hwnd, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, FALSE);
1511 if (thegame.flags & REQUIRE_NUMPAD)
1513 fe->numpad = CreateToolbarEx (fe->hwnd,
1514 WS_VISIBLE | WS_CHILD | CCS_NOPARENTALIGN | TBSTYLE_FLAT,
1515 0, 10, inst, IDR_PADTOOLBAR,
1516 tbNumpadButtons, sizeof (tbNumpadButtons) / sizeof (TBBUTTON),
1517 0, 0, 14, 15, sizeof (TBBUTTON));
1518 GetWindowRect(fe->numpad, &rcTB);
1519 GetClientRect(fe->hwnd, &rcClient);
1520 MoveWindow(fe->numpad,
1522 rcClient.bottom - (rcTB.bottom - rcTB.top) - 1,
1524 rcTB.bottom - rcTB.top,
1526 SendMessage(fe->numpad, TB_SETINDENT, (rcClient.right - (10 * 21)) / 2, 0);
1532 fe->hwnd = CreateWindowEx(0, thegame.name, thegame.name,
1533 WS_OVERLAPPEDWINDOW &~
1535 CW_USEDEFAULT, CW_USEDEFAULT,
1536 r.right - r.left, r.bottom - r.top,
1537 NULL, NULL, inst, NULL);
1540 if (midend_wants_statusbar(fe->me)) {
1542 DestroyWindow(fe->statusbar);
1543 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, TEXT("ooh"),
1544 WS_CHILD | WS_VISIBLE,
1545 0, 0, 0, 0, /* status bar does these */
1546 fe->hwnd, NULL, inst, NULL);
1548 /* Flat status bar looks better on the Pocket PC */
1549 SendMessage(fe->statusbar, SB_SIMPLE, (WPARAM) TRUE, 0);
1550 SendMessage(fe->statusbar, SB_SETTEXT,
1551 (WPARAM) 255 | SBT_NOBORDERS,
1556 * Now resize the window to take account of the status bar.
1558 GetWindowRect(fe->statusbar, &sr);
1559 GetWindowRect(fe->hwnd, &r);
1561 SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left,
1562 r.bottom - r.top + sr.bottom - sr.top,
1563 SWP_NOMOVE | SWP_NOZORDER);
1566 fe->statusbar = NULL;
1571 HMENU bar = CreateMenu();
1572 HMENU menu = CreateMenu();
1575 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "&Game");
1577 HMENU menu = SHGetSubMenu(SHFindMenuBar(fe->hwnd), ID_GAME);
1578 DeleteMenu(menu, 0, MF_BYPOSITION);
1580 AppendMenu(menu, MF_ENABLED, IDM_NEW, TEXT("&New"));
1581 AppendMenu(menu, MF_ENABLED, IDM_RESTART, TEXT("&Restart"));
1583 /* ...here I run out of sensible accelerator characters. */
1584 AppendMenu(menu, MF_ENABLED, IDM_DESC, TEXT("Speci&fic..."));
1585 AppendMenu(menu, MF_ENABLED, IDM_SEED, TEXT("Rando&m Seed..."));
1588 if ((fe->npresets = midend_num_presets(fe->me)) > 0 ||
1589 thegame.can_configure) {
1592 HMENU sub = CreateMenu();
1594 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "&Type");
1596 HMENU sub = SHGetSubMenu(SHFindMenuBar(fe->hwnd), ID_TYPE);
1597 DeleteMenu(sub, 0, MF_BYPOSITION);
1599 fe->presets = snewn(fe->npresets, game_params *);
1601 for (i = 0; i < fe->npresets; i++) {
1607 midend_fetch_preset(fe->me, i, &name, &fe->presets[i]);
1610 * FIXME: we ought to go through and do something
1611 * with ampersands here.
1615 AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name);
1617 MultiByteToWideChar (CP_ACP, 0, name, -1, wName, 255);
1618 AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, wName);
1621 if (thegame.can_configure) {
1622 AppendMenu(sub, MF_ENABLED, IDM_CONFIG, TEXT("&Custom..."));
1626 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1628 AppendMenu(menu, MF_ENABLED, IDM_LOAD, TEXT("&Load..."));
1629 AppendMenu(menu, MF_ENABLED, IDM_SAVE, TEXT("&Save..."));
1630 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1631 if (thegame.can_print) {
1632 AppendMenu(menu, MF_ENABLED, IDM_PRINT, TEXT("&Print..."));
1633 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1636 AppendMenu(menu, MF_ENABLED, IDM_UNDO, TEXT("Undo"));
1637 AppendMenu(menu, MF_ENABLED, IDM_REDO, TEXT("Redo"));
1639 if (thegame.can_format_as_text) {
1640 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1641 AppendMenu(menu, MF_ENABLED, IDM_COPY, TEXT("&Copy"));
1644 if (thegame.can_solve) {
1645 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1646 AppendMenu(menu, MF_ENABLED, IDM_SOLVE, TEXT("Sol&ve"));
1648 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1650 AppendMenu(menu, MF_ENABLED, IDM_QUIT, TEXT("E&xit"));
1651 menu = CreateMenu();
1652 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, TEXT("&Help"));
1654 AppendMenu(menu, MF_ENABLED, IDM_ABOUT, TEXT("&About"));
1656 if (help_type != NONE) {
1657 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1658 AppendMenu(menu, MF_ENABLED, IDM_HELPC, TEXT("&Contents"));
1661 assert(thegame.name);
1662 item = snewn(10+strlen(thegame.name), char); /*ick*/
1663 sprintf(item, "&Help on %s", thegame.name);
1664 AppendMenu(menu, MF_ENABLED, IDM_GAMEHELP, item);
1668 SetMenu(fe->hwnd, bar);
1669 get_menu_size(fe->hwnd, &menusize);
1670 fe->xmin = (menusize.right - menusize.left) + 25;
1675 new_game_size(fe); /* initialises fe->bitmap */
1676 check_window_size(fe, &x, &y);
1678 SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe);
1680 ShowWindow(fe->hwnd, SW_SHOWNORMAL);
1681 SetForegroundWindow(fe->hwnd);
1683 midend_redraw(fe->me);
1689 static HFONT dialog_title_font()
1691 static HFONT hf = NULL;
1697 memset (&lf, 0, sizeof(LOGFONT));
1698 lf.lfHeight = -11; /* - ((8 * GetDeviceCaps(hdc, LOGPIXELSY)) / 72) */
1699 lf.lfWeight = FW_BOLD;
1700 wcscpy(lf.lfFaceName, TEXT("Tahoma"));
1702 return hf = CreateFontIndirect(&lf);
1705 static void make_dialog_full_screen(HWND hwnd)
1707 SHINITDLGINFO shidi;
1709 /* Make dialog full screen */
1710 shidi.dwMask = SHIDIM_FLAGS;
1711 shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIZEDLGFULLSCREEN |
1714 SHInitDialog(&shidi);
1718 static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg,
1719 WPARAM wParam, LPARAM lParam)
1721 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1729 make_dialog_full_screen(hwnd);
1731 sprintf(title, "About %.250s", thegame.name);
1732 SetDlgItemTextA(hwnd, IDC_ABOUT_CAPTION, title);
1734 SendDlgItemMessage(hwnd, IDC_ABOUT_CAPTION, WM_SETFONT,
1735 (WPARAM) dialog_title_font(), 0);
1737 SetDlgItemTextA(hwnd, IDC_ABOUT_GAME, thegame.name);
1738 SetDlgItemTextA(hwnd, IDC_ABOUT_VERSION, ver);
1744 if (LOWORD(wParam) == IDOK)
1765 * Wrappers on midend_{get,set}_config, which extend the CFG_*
1766 * enumeration to add CFG_PRINT.
1768 static config_item *frontend_get_config(frontend *fe, int which,
1771 if (which < CFG_FRONTEND_SPECIFIC) {
1772 return midend_get_config(fe->me, which, wintitle);
1773 } else if (which == CFG_PRINT) {
1777 *wintitle = snewn(40 + strlen(thegame.name), char);
1778 sprintf(*wintitle, "%s print setup", thegame.name);
1780 ret = snewn(8, config_item);
1784 ret[i].name = "Number of puzzles to print";
1785 ret[i].type = C_STRING;
1786 ret[i].sval = dupstr("1");
1790 ret[i].name = "Number of puzzles across the page";
1791 ret[i].type = C_STRING;
1792 ret[i].sval = dupstr("1");
1796 ret[i].name = "Number of puzzles down the page";
1797 ret[i].type = C_STRING;
1798 ret[i].sval = dupstr("1");
1802 ret[i].name = "Percentage of standard size";
1803 ret[i].type = C_STRING;
1804 ret[i].sval = dupstr("100.0");
1808 ret[i].name = "Include currently shown puzzle";
1809 ret[i].type = C_BOOLEAN;
1814 ret[i].name = "Print solutions";
1815 ret[i].type = C_BOOLEAN;
1817 ret[i].ival = FALSE;
1820 if (thegame.can_print_in_colour) {
1821 ret[i].name = "Print in colour";
1822 ret[i].type = C_BOOLEAN;
1824 ret[i].ival = FALSE;
1829 ret[i].type = C_END;
1836 assert(!"We should never get here");
1841 static char *frontend_set_config(frontend *fe, int which, config_item *cfg)
1843 if (which < CFG_FRONTEND_SPECIFIC) {
1844 return midend_set_config(fe->me, which, cfg);
1845 } else if (which == CFG_PRINT) {
1846 if ((fe->printcount = atoi(cfg[0].sval)) <= 0)
1847 return "Number of puzzles to print should be at least one";
1848 if ((fe->printw = atoi(cfg[1].sval)) <= 0)
1849 return "Number of puzzles across the page should be at least one";
1850 if ((fe->printh = atoi(cfg[2].sval)) <= 0)
1851 return "Number of puzzles down the page should be at least one";
1852 if ((fe->printscale = (float)atof(cfg[3].sval)) <= 0)
1853 return "Print size should be positive";
1854 fe->printcurr = cfg[4].ival;
1855 fe->printsolns = cfg[5].ival;
1856 fe->printcolour = thegame.can_print_in_colour && cfg[6].ival;
1859 assert(!"We should never get here");
1860 return "Internal error";
1865 /* Separate version of mkctrl function for the Pocket PC. */
1866 /* Control coordinates should be specified in dialog units. */
1867 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
1868 LPCTSTR wclass, int wstyle,
1869 int exstyle, const char *wtext, int wid)
1874 /* Convert dialog units into pixels */
1875 rc.left = x1; rc.right = x2;
1876 rc.top = y1; rc.bottom = y2;
1877 MapDialogRect(fe->cfgbox, &rc);
1879 MultiByteToWideChar (CP_ACP, 0, wtext, -1, wwtext, 256);
1881 return CreateWindowEx(exstyle, wclass, wwtext,
1882 wstyle | WS_CHILD | WS_VISIBLE,
1884 rc.right - rc.left, rc.bottom - rc.top,
1885 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
1888 static void create_config_controls(frontend * fe)
1891 int col1l, col1r, col2l, col2r, y;
1896 /* Control placement done in dialog units */
1897 col1l = 4; col1r = 96; /* Label column */
1898 col2l = 100; col2r = 154; /* Input column (edit boxes and combo boxes) */
1901 * Count the controls so we can allocate cfgaux.
1903 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
1905 fe->cfgaux = snewn(nctrls, struct cfg_aux);
1908 y = 22; /* Leave some room for the dialog title */
1909 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
1913 * Edit box with a label beside it.
1915 mkctrl(fe, col1l, col1r, y + 1, y + 11,
1916 TEXT("Static"), SS_LEFTNOWORDWRAP, 0, i->name, id++);
1917 mkctrl(fe, col2l, col2r, y, y + 12,
1918 TEXT("EDIT"), WS_BORDER | WS_TABSTOP | ES_AUTOHSCROLL,
1919 0, "", (j->ctlid = id++));
1920 SetDlgItemTextA(fe->cfgbox, j->ctlid, i->sval);
1927 mkctrl(fe, col1l, col2r, y + 1, y + 11, TEXT("BUTTON"),
1928 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
1929 0, i->name, (j->ctlid = id++));
1930 CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0));
1935 * Drop-down list with a label beside it.
1937 mkctrl(fe, col1l, col1r, y + 1, y + 11,
1938 TEXT("STATIC"), SS_LEFTNOWORDWRAP, 0, i->name, id++);
1939 ctl = mkctrl(fe, col2l, col2r, y, y + 48,
1940 TEXT("COMBOBOX"), WS_BORDER | WS_TABSTOP |
1941 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
1942 0, "", (j->ctlid = id++));
1944 char c, *p, *q, *str;
1950 while (*q && *q != c) q++;
1951 str = snewn(q-p+1, char);
1952 strncpy(str, p, q-p);
1956 MultiByteToWideChar (CP_ACP, 0, str, -1, ws, 50);
1957 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)ws);
1965 SendMessage(ctl, CB_SETCURSEL, i->ival, 0);
1975 static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg,
1976 WPARAM wParam, LPARAM lParam)
1978 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1988 fe = (frontend *) lParam;
1989 SetWindowLong(hwnd, GWL_USERDATA, lParam);
1992 fe->cfg = frontend_get_config(fe, fe->cfg_which, &title);
1994 make_dialog_full_screen(hwnd);
1996 SetDlgItemTextA(hwnd, IDC_CONFIG_CAPTION, title);
1997 SendDlgItemMessage(hwnd, IDC_CONFIG_CAPTION, WM_SETFONT,
1998 (WPARAM) dialog_title_font(), 0);
2000 create_config_controls(fe);
2007 * OK and Cancel are special cases.
2009 if ((LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) {
2010 if (LOWORD(wParam) == IDOK) {
2011 char *err = frontend_set_config(fe, fe->cfg_which, fe->cfg);
2014 MessageBox(hwnd, err, "Validation error",
2015 MB_ICONERROR | MB_OK);
2034 * First find the control whose id this is.
2036 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
2037 if (j->ctlid == LOWORD(wParam))
2040 if (i->type == C_END)
2041 return 0; /* not our problem */
2043 if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) {
2046 TCHAR wBuffer[4096];
2047 GetDlgItemText(fe->cfgbox, j->ctlid, wBuffer, 4096);
2048 WideCharToMultiByte(CP_ACP, 0, wBuffer, -1, buffer, 4096, NULL, NULL);
2050 GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer));
2052 buffer[lenof(buffer)-1] = '\0';
2054 i->sval = dupstr(buffer);
2055 } else if (i->type == C_BOOLEAN &&
2056 (HIWORD(wParam) == BN_CLICKED ||
2057 HIWORD(wParam) == BN_DBLCLK)) {
2058 i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid);
2059 } else if (i->type == C_CHOICES &&
2060 HIWORD(wParam) == CBN_SELCHANGE) {
2061 i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid,
2062 CB_GETCURSEL, 0, 0);
2076 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
2077 char *wclass, int wstyle,
2078 int exstyle, const char *wtext, int wid)
2081 ret = CreateWindowEx(exstyle, wclass, wtext,
2082 wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1,
2083 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
2084 SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0));
2089 static void about(frontend *fe)
2092 DialogBox(fe->inst, MAKEINTRESOURCE(IDD_ABOUT), fe->hwnd, AboutDlgProc);
2102 int winwidth, winheight, y;
2103 int height, width, maxwid;
2104 const char *strings[16];
2109 sprintf(titlebuf, "About %.250s", thegame.name);
2111 strings[nstrings++] = thegame.name;
2112 strings[nstrings++] = "from Simon Tatham's Portable Puzzle Collection";
2113 strings[nstrings++] = ver;
2115 wc.style = CS_DBLCLKS | CS_SAVEBITS;
2116 wc.lpfnWndProc = DefDlgProc;
2118 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
2119 wc.hInstance = fe->inst;
2121 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
2122 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
2123 wc.lpszMenuName = NULL;
2124 wc.lpszClassName = "GameAboutBox";
2127 hdc = GetDC(fe->hwnd);
2128 SetMapMode(hdc, MM_TEXT);
2130 fe->dlg_done = FALSE;
2132 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
2134 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2135 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2140 oldfont = SelectObject(hdc, fe->cfgfont);
2141 if (GetTextMetrics(hdc, &tm)) {
2142 height = tm.tmAscent + tm.tmDescent;
2143 width = tm.tmAveCharWidth;
2145 height = width = 30;
2149 * Figure out the layout of the About box by measuring the
2150 * length of each piece of text.
2153 winheight = height/2;
2155 for (i = 0; i < nstrings; i++) {
2156 if (GetTextExtentPoint32(hdc, strings[i], strlen(strings[i]), &size))
2157 lengths[i] = size.cx;
2159 lengths[i] = 0; /* *shrug* */
2160 if (maxwid < lengths[i])
2161 maxwid = lengths[i];
2162 winheight += height * 3 / 2 + (height / 2);
2165 winheight += height + height * 7 / 4; /* OK button */
2166 winwidth = maxwid + 4*width;
2168 SelectObject(hdc, oldfont);
2169 ReleaseDC(fe->hwnd, hdc);
2172 * Create the dialog, now that we know its size.
2179 r.bottom = winheight;
2181 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
2182 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
2183 WS_CAPTION | WS_SYSMENU*/) &~
2184 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
2188 * Centre the dialog on its parent window.
2192 GetWindowRect(fe->hwnd, &r2);
2193 r.left = (r2.left + r2.right - r.right) / 2;
2194 r.top = (r2.top + r2.bottom - r.bottom) / 2;
2198 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, titlebuf,
2199 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
2200 WS_CAPTION | WS_SYSMENU,
2202 r.right-r.left, r.bottom-r.top,
2203 fe->hwnd, NULL, fe->inst, NULL);
2206 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
2208 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
2209 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)AboutDlgProc);
2213 for (i = 0; i < nstrings; i++) {
2214 int border = width*2 + (maxwid - lengths[i]) / 2;
2215 mkctrl(fe, border, border+lengths[i], y+height*1/8, y+height*9/8,
2216 "Static", 0, 0, strings[i], id++);
2219 assert(y < winheight);
2223 y += height/2; /* extra space before OK */
2224 mkctrl(fe, width*2, maxwid+width*2, y, y+height*7/4, "BUTTON",
2225 BS_PUSHBUTTON | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
2228 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
2230 EnableWindow(fe->hwnd, FALSE);
2231 ShowWindow(fe->cfgbox, SW_SHOWNORMAL);
2232 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
2233 if (!IsDialogMessage(fe->cfgbox, &msg))
2234 DispatchMessage(&msg);
2238 EnableWindow(fe->hwnd, TRUE);
2239 SetForegroundWindow(fe->hwnd);
2240 DestroyWindow(fe->cfgbox);
2241 DeleteObject(fe->cfgfont);
2245 static int get_config(frontend *fe, int which)
2248 fe->cfg_which = which;
2250 return DialogBoxParam(fe->inst,
2251 MAKEINTRESOURCE(IDD_CONFIG),
2252 fe->hwnd, ConfigDlgProc,
2266 int winwidth, winheight, col1l, col1r, col2l, col2r, y;
2267 int height, width, maxlabel, maxcheckbox;
2269 wc.style = CS_DBLCLKS | CS_SAVEBITS;
2270 wc.lpfnWndProc = DefDlgProc;
2272 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
2273 wc.hInstance = fe->inst;
2275 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
2276 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
2277 wc.lpszMenuName = NULL;
2278 wc.lpszClassName = "GameConfigBox";
2281 hdc = GetDC(fe->hwnd);
2282 SetMapMode(hdc, MM_TEXT);
2284 fe->dlg_done = FALSE;
2286 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
2288 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2289 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2294 oldfont = SelectObject(hdc, fe->cfgfont);
2295 if (GetTextMetrics(hdc, &tm)) {
2296 height = tm.tmAscent + tm.tmDescent;
2297 width = tm.tmAveCharWidth;
2299 height = width = 30;
2302 fe->cfg = frontend_get_config(fe, which, &title);
2303 fe->cfg_which = which;
2306 * Figure out the layout of the config box by measuring the
2307 * length of each piece of text.
2309 maxlabel = maxcheckbox = 0;
2310 winheight = height/2;
2312 for (i = fe->cfg; i->type != C_END; i++) {
2317 * Both these control types have a label filling only
2318 * the left-hand column of the box.
2320 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
2323 winheight += height * 3 / 2 + (height / 2);
2328 * Checkboxes take up the whole of the box width.
2330 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
2331 maxcheckbox < size.cx)
2332 maxcheckbox = size.cx;
2333 winheight += height + (height / 2);
2338 winheight += height + height * 7 / 4; /* OK / Cancel buttons */
2341 col1r = col1l + maxlabel;
2342 col2l = col1r + 2*width;
2343 col2r = col2l + 30*width;
2344 if (col2r < col1l+2*height+maxcheckbox)
2345 col2r = col1l+2*height+maxcheckbox;
2346 winwidth = col2r + 2*width;
2348 SelectObject(hdc, oldfont);
2349 ReleaseDC(fe->hwnd, hdc);
2352 * Create the dialog, now that we know its size.
2359 r.bottom = winheight;
2361 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
2362 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
2363 WS_CAPTION | WS_SYSMENU*/) &~
2364 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
2368 * Centre the dialog on its parent window.
2372 GetWindowRect(fe->hwnd, &r2);
2373 r.left = (r2.left + r2.right - r.right) / 2;
2374 r.top = (r2.top + r2.bottom - r.bottom) / 2;
2378 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title,
2379 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
2380 WS_CAPTION | WS_SYSMENU,
2382 r.right-r.left, r.bottom-r.top,
2383 fe->hwnd, NULL, fe->inst, NULL);
2387 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
2389 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
2390 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc);
2393 * Count the controls so we can allocate cfgaux.
2395 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
2397 fe->cfgaux = snewn(nctrls, struct cfg_aux);
2401 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
2405 * Edit box with a label beside it.
2407 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
2408 "Static", 0, 0, i->name, id++);
2409 ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2,
2410 "EDIT", WS_TABSTOP | ES_AUTOHSCROLL,
2411 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
2412 SetWindowText(ctl, i->sval);
2420 mkctrl(fe, col1l, col2r, y, y+height, "BUTTON",
2421 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
2422 0, i->name, (j->ctlid = id++));
2423 CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0));
2429 * Drop-down list with a label beside it.
2431 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
2432 "STATIC", 0, 0, i->name, id++);
2433 ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2,
2434 "COMBOBOX", WS_TABSTOP |
2435 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
2436 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
2438 char c, *p, *q, *str;
2440 SendMessage(ctl, CB_RESETCONTENT, 0, 0);
2445 while (*q && *q != c) q++;
2446 str = snewn(q-p+1, char);
2447 strncpy(str, p, q-p);
2449 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str);
2456 SendMessage(ctl, CB_SETCURSEL, i->ival, 0);
2462 assert(y < winheight);
2466 y += height/2; /* extra space before OK and Cancel */
2467 mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON",
2468 BS_PUSHBUTTON | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
2470 mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON",
2471 BS_PUSHBUTTON | WS_TABSTOP, 0, "Cancel", IDCANCEL);
2473 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
2475 EnableWindow(fe->hwnd, FALSE);
2476 ShowWindow(fe->cfgbox, SW_SHOWNORMAL);
2477 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
2478 if (!IsDialogMessage(fe->cfgbox, &msg))
2479 DispatchMessage(&msg);
2483 EnableWindow(fe->hwnd, TRUE);
2484 SetForegroundWindow(fe->hwnd);
2485 DestroyWindow(fe->cfgbox);
2486 DeleteObject(fe->cfgfont);
2491 return (fe->dlg_done == 2);
2496 static void calculate_bitmap_position(frontend *fe, int x, int y)
2498 /* Pocket PC - center the game in the full screen window */
2502 GetClientRect(fe->hwnd, &rcClient);
2503 fe->bitmapPosition.left = (rcClient.right - x) / 2;
2504 yMargin = rcClient.bottom - y;
2506 if (fe->numpad != NULL) {
2508 GetWindowRect(fe->numpad, &rcPad);
2509 yMargin -= rcPad.bottom - rcPad.top;
2512 if (fe->statusbar != NULL) {
2514 GetWindowRect(fe->statusbar, &rcStatus);
2515 yMargin -= rcStatus.bottom - rcStatus.top;
2518 fe->bitmapPosition.top = yMargin / 2;
2520 fe->bitmapPosition.right = fe->bitmapPosition.left + x;
2521 fe->bitmapPosition.bottom = fe->bitmapPosition.top + y;
2524 static void calculate_bitmap_position(frontend *fe, int x, int y)
2526 /* Plain Windows - position the game in the upper-left corner */
2527 fe->bitmapPosition.left = 0;
2528 fe->bitmapPosition.top = 0;
2529 fe->bitmapPosition.right = fe->bitmapPosition.left + x;
2530 fe->bitmapPosition.bottom = fe->bitmapPosition.top + y;
2534 static void new_bitmap(frontend *fe, int x, int y)
2538 if (fe->bitmap) DeleteObject(fe->bitmap);
2540 hdc = GetDC(fe->hwnd);
2541 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
2542 calculate_bitmap_position(fe, x, y);
2543 ReleaseDC(fe->hwnd, hdc);
2546 static void new_game_size(frontend *fe)
2551 get_max_puzzle_size(fe, &x, &y);
2552 midend_size(fe->me, &x, &y, FALSE);
2553 fe->ymin = (fe->xmin * y) / x;
2558 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
2560 if (fe->statusbar != NULL) {
2561 GetWindowRect(fe->statusbar, &sr);
2563 sr.left = sr.right = sr.top = sr.bottom = 0;
2566 SetWindowPos(fe->hwnd, NULL, 0, 0,
2568 r.bottom - r.top + sr.bottom - sr.top,
2569 SWP_NOMOVE | SWP_NOZORDER);
2572 check_window_size(fe, &x, &y);
2575 if (fe->statusbar != NULL)
2576 SetWindowPos(fe->statusbar, NULL, 0, y, x,
2577 sr.bottom - sr.top, SWP_NOZORDER);
2580 new_bitmap(fe, x, y);
2583 InvalidateRect(fe->hwnd, NULL, TRUE);
2585 midend_redraw(fe->me);
2589 * Given a proposed new window rect, work out the resulting
2590 * difference in client size (from current), and use to try
2591 * and resize the puzzle, returning (wx,wy) as the actual
2595 static void adjust_game_size(frontend *fe, RECT *proposed, int isedge,
2596 int *wx_r, int *wy_r)
2599 int nx, ny, xdiff, ydiff, wx, wy;
2601 /* Work out the current window sizing, and thus the
2602 * difference in size we're asking for. */
2603 GetClientRect(fe->hwnd, &cr);
2605 AdjustWindowRectEx(&wr, WINFLAGS, TRUE, 0);
2607 xdiff = (proposed->right - proposed->left) - (wr.right - wr.left);
2608 ydiff = (proposed->bottom - proposed->top) - (wr.bottom - wr.top);
2611 /* These next four lines work around the fact that midend_size
2612 * is happy to shrink _but not grow_ if you change one dimension
2613 * but not the other. */
2614 if (xdiff > 0 && ydiff == 0)
2615 ydiff = (xdiff * (wr.right - wr.left)) / (wr.bottom - wr.top);
2616 if (xdiff == 0 && ydiff > 0)
2617 xdiff = (ydiff * (wr.bottom - wr.top)) / (wr.right - wr.left);
2620 if (check_window_resize(fe,
2621 (cr.right - cr.left) + xdiff,
2622 (cr.bottom - cr.top) + ydiff,
2623 &nx, &ny, &wx, &wy, TRUE)) {
2624 new_bitmap(fe, nx, ny);
2625 midend_force_redraw(fe->me);
2627 /* reset size to current window size */
2628 wx = wr.right - wr.left;
2629 wy = wr.bottom - wr.top;
2631 /* Re-fetch rectangle; size limits mean we might not have
2632 * taken it quite to the mouse drag positions. */
2633 GetClientRect(fe->hwnd, &cr);
2634 adjust_statusbar(fe, &cr);
2636 *wx_r = wx; *wy_r = wy;
2639 static void new_game_type(frontend *fe)
2641 midend_new_game(fe->me);
2645 static int is_alt_pressed(void)
2648 int r = GetKeyboardState(keystate);
2651 if (keystate[VK_MENU] & 0x80)
2653 if (keystate[VK_RMENU] & 0x80)
2658 static void savefile_write(void *wctx, void *buf, int len)
2660 FILE *fp = (FILE *)wctx;
2661 fwrite(buf, 1, len, fp);
2664 static int savefile_read(void *wctx, void *buf, int len)
2666 FILE *fp = (FILE *)wctx;
2669 ret = fread(buf, 1, len, fp);
2670 return (ret == len);
2673 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
2674 WPARAM wParam, LPARAM lParam)
2676 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
2681 DestroyWindow(hwnd);
2685 /* Numeric pad sends WM_COMMAND messages */
2686 if ((wParam >= IDM_KEYEMUL) && (wParam < IDM_KEYEMUL + 256))
2688 midend_process_key(fe->me, 0, 0, wParam - IDM_KEYEMUL);
2691 cmd = wParam & ~0xF; /* low 4 bits reserved to Windows */
2694 if (!midend_process_key(fe->me, 0, 0, 'n'))
2698 midend_restart_game(fe->me);
2701 if (!midend_process_key(fe->me, 0, 0, 'u'))
2705 if (!midend_process_key(fe->me, 0, 0, '\x12'))
2710 char *text = midend_text_format(fe->me);
2712 write_clip(hwnd, text);
2714 MessageBeep(MB_ICONWARNING);
2720 char *msg = midend_solve(fe->me);
2722 MessageBox(hwnd, msg, "Unable to solve",
2723 MB_ICONERROR | MB_OK);
2727 if (!midend_process_key(fe->me, 0, 0, 'q'))
2731 if (get_config(fe, CFG_SETTINGS))
2735 if (get_config(fe, CFG_SEED))
2739 if (get_config(fe, CFG_DESC))
2743 if (get_config(fe, CFG_PRINT))
2753 char filename[FILENAME_MAX];
2756 memset(&of, 0, sizeof(of));
2757 of.hwndOwner = hwnd;
2758 of.lpstrFilter = "All Files (*.*)\0*\0\0\0";
2759 of.lpstrCustomFilter = NULL;
2760 of.nFilterIndex = 1;
2761 of.lpstrFile = filename;
2763 of.nMaxFile = lenof(filename);
2764 of.lpstrFileTitle = NULL;
2765 of.lpstrTitle = (cmd == IDM_SAVE ?
2766 "Enter name of game file to save" :
2767 "Enter name of saved game file to load");
2769 #ifdef OPENFILENAME_SIZE_VERSION_400
2770 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
2772 of.lStructSize = sizeof(of);
2774 of.lpstrInitialDir = NULL;
2776 if (cmd == IDM_SAVE)
2777 ret = GetSaveFileName(&of);
2779 ret = GetOpenFileName(&of);
2782 if (cmd == IDM_SAVE) {
2785 if ((fp = fopen(filename, "r")) != NULL) {
2786 char buf[256 + FILENAME_MAX];
2790 sprintf(buf, "Are you sure you want to overwrite"
2791 " the file \"%.*s\"?",
2792 FILENAME_MAX, filename);
2793 if (MessageBox(hwnd, buf, "Question",
2794 MB_YESNO | MB_ICONQUESTION)
2799 fp = fopen(filename, "w");
2802 MessageBox(hwnd, "Unable to open save file",
2803 "Error", MB_ICONERROR | MB_OK);
2807 midend_serialise(fe->me, savefile_write, fp);
2811 FILE *fp = fopen(filename, "r");
2815 MessageBox(hwnd, "Unable to open saved game file",
2816 "Error", MB_ICONERROR | MB_OK);
2820 err = midend_deserialise(fe->me, savefile_read, fp);
2825 MessageBox(hwnd, err, "Error", MB_ICONERROR|MB_OK);
2837 start_help(fe, NULL);
2840 start_help(fe, help_topic);
2845 int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10;
2847 if (p >= 0 && p < fe->npresets) {
2848 midend_set_params(fe->me, fe->presets[p]);
2868 hdc = BeginPaint(hwnd, &p);
2869 hdc2 = CreateCompatibleDC(hdc);
2870 prevbm = SelectObject(hdc2, fe->bitmap);
2872 FillRect(hdc, &(p.rcPaint), (HBRUSH) GetStockObject(WHITE_BRUSH));
2874 IntersectRect(&rcDest, &(fe->bitmapPosition), &(p.rcPaint));
2876 rcDest.left, rcDest.top,
2877 rcDest.right - rcDest.left,
2878 rcDest.bottom - rcDest.top,
2880 rcDest.left - fe->bitmapPosition.left,
2881 rcDest.top - fe->bitmapPosition.top,
2883 SelectObject(hdc2, prevbm);
2892 int r = GetKeyboardState(keystate);
2893 int shift = (r && (keystate[VK_SHIFT] & 0x80)) ? MOD_SHFT : 0;
2894 int ctrl = (r && (keystate[VK_CONTROL] & 0x80)) ? MOD_CTRL : 0;
2898 if (!(lParam & 0x01000000))
2899 key = MOD_NUM_KEYPAD | '4';
2901 key = shift | ctrl | CURSOR_LEFT;
2904 if (!(lParam & 0x01000000))
2905 key = MOD_NUM_KEYPAD | '6';
2907 key = shift | ctrl | CURSOR_RIGHT;
2910 if (!(lParam & 0x01000000))
2911 key = MOD_NUM_KEYPAD | '8';
2913 key = shift | ctrl | CURSOR_UP;
2916 if (!(lParam & 0x01000000))
2917 key = MOD_NUM_KEYPAD | '2';
2919 key = shift | ctrl | CURSOR_DOWN;
2922 * Diagonal keys on the numeric keypad.
2925 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '9';
2928 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '3';
2931 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '7';
2934 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '1';
2937 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '0';
2940 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '5';
2943 * Numeric keypad keys with Num Lock on.
2945 case VK_NUMPAD4: key = MOD_NUM_KEYPAD | '4'; break;
2946 case VK_NUMPAD6: key = MOD_NUM_KEYPAD | '6'; break;
2947 case VK_NUMPAD8: key = MOD_NUM_KEYPAD | '8'; break;
2948 case VK_NUMPAD2: key = MOD_NUM_KEYPAD | '2'; break;
2949 case VK_NUMPAD5: key = MOD_NUM_KEYPAD | '5'; break;
2950 case VK_NUMPAD9: key = MOD_NUM_KEYPAD | '9'; break;
2951 case VK_NUMPAD3: key = MOD_NUM_KEYPAD | '3'; break;
2952 case VK_NUMPAD7: key = MOD_NUM_KEYPAD | '7'; break;
2953 case VK_NUMPAD1: key = MOD_NUM_KEYPAD | '1'; break;
2954 case VK_NUMPAD0: key = MOD_NUM_KEYPAD | '0'; break;
2958 if (!midend_process_key(fe->me, 0, 0, key))
2963 m.message = WM_KEYDOWN;
2965 m.lParam = lParam & 0xdfff;
2966 TranslateMessage(&m);
2970 case WM_LBUTTONDOWN:
2971 case WM_RBUTTONDOWN:
2972 case WM_MBUTTONDOWN:
2977 * Shift-clicks count as middle-clicks, since otherwise
2978 * two-button Windows users won't have any kind of
2979 * middle click to use.
2981 if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT))
2982 button = MIDDLE_BUTTON;
2983 else if (message == WM_RBUTTONDOWN || is_alt_pressed())
2984 button = RIGHT_BUTTON;
2987 button = LEFT_BUTTON;
2989 if ((thegame.flags & REQUIRE_RBUTTON) == 0)
2990 button = LEFT_BUTTON;
2995 shrgi.cbSize = sizeof(SHRGINFO);
2996 shrgi.hwndClient = hwnd;
2997 shrgi.ptDown.x = (signed short)LOWORD(lParam);
2998 shrgi.ptDown.y = (signed short)HIWORD(lParam);
2999 shrgi.dwFlags = SHRG_RETURNCMD;
3001 if (GN_CONTEXTMENU == SHRecognizeGesture(&shrgi))
3002 button = RIGHT_BUTTON;
3004 button = LEFT_BUTTON;
3008 if (!midend_process_key(fe->me,
3009 (signed short)LOWORD(lParam) - fe->bitmapPosition.left,
3010 (signed short)HIWORD(lParam) - fe->bitmapPosition.top,
3024 * Shift-clicks count as middle-clicks, since otherwise
3025 * two-button Windows users won't have any kind of
3026 * middle click to use.
3028 if (message == WM_MBUTTONUP || (wParam & MK_SHIFT))
3029 button = MIDDLE_RELEASE;
3030 else if (message == WM_RBUTTONUP || is_alt_pressed())
3031 button = RIGHT_RELEASE;
3033 button = LEFT_RELEASE;
3035 if (!midend_process_key(fe->me,
3036 (signed short)LOWORD(lParam) - fe->bitmapPosition.left,
3037 (signed short)HIWORD(lParam) - fe->bitmapPosition.top,
3048 if (wParam & (MK_MBUTTON | MK_SHIFT))
3049 button = MIDDLE_DRAG;
3050 else if (wParam & MK_RBUTTON || is_alt_pressed())
3051 button = RIGHT_DRAG;
3055 if (!midend_process_key(fe->me,
3056 (signed short)LOWORD(lParam) - fe->bitmapPosition.left,
3057 (signed short)HIWORD(lParam) - fe->bitmapPosition.top,
3063 if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam))
3068 DWORD now = GetTickCount();
3069 float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F;
3070 midend_timer(fe->me, elapsed);
3071 fe->timer_last_tickcount = now;
3077 RECT *sr = (RECT *)lParam;
3078 int wx, wy, isedge = 0;
3080 if (wParam == WMSZ_TOP ||
3081 wParam == WMSZ_RIGHT ||
3082 wParam == WMSZ_BOTTOM ||
3083 wParam == WMSZ_LEFT) isedge = 1;
3084 adjust_game_size(fe, sr, isedge, &wx, &wy);
3086 /* Given the window size the puzzles constrain
3087 * us to, work out which edge we should be moving. */
3088 if (wParam == WMSZ_TOP ||
3089 wParam == WMSZ_TOPLEFT ||
3090 wParam == WMSZ_TOPRIGHT) {
3091 sr->top = sr->bottom - wy;
3093 sr->bottom = sr->top + wy;
3095 if (wParam == WMSZ_LEFT ||
3096 wParam == WMSZ_TOPLEFT ||
3097 wParam == WMSZ_BOTTOMLEFT) {
3098 sr->left = sr->right - wx;
3100 sr->right = sr->left + wx;
3108 return DefWindowProc(hwnd, message, wParam, lParam);
3112 static int FindPreviousInstance()
3114 /* Check if application is running. If it's running then focus on the window */
3115 HWND hOtherWnd = NULL;
3117 hOtherWnd = FindWindow (wGameName, wGameName);
3120 SetForegroundWindow (hOtherWnd);
3128 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
3134 MultiByteToWideChar (CP_ACP, 0, thegame.name, -1, wGameName, 256);
3135 if (FindPreviousInstance ())
3139 InitCommonControls();
3145 wndclass.lpfnWndProc = WndProc;
3146 wndclass.cbClsExtra = 0;
3147 wndclass.cbWndExtra = 0;
3148 wndclass.hInstance = inst;
3149 wndclass.hIcon = LoadIcon(inst, MAKEINTRESOURCE(200));
3151 if (!wndclass.hIcon) /* in case resource file is absent */
3152 wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION);
3154 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
3155 wndclass.hbrBackground = NULL;
3156 wndclass.lpszMenuName = NULL;
3158 wndclass.lpszClassName = wGameName;
3160 wndclass.lpszClassName = thegame.name;
3163 RegisterClass(&wndclass);
3166 while (*cmdline && isspace((unsigned char)*cmdline))
3171 if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) {
3173 sprintf(buf, "%.100s Error", thegame.name);
3174 MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
3178 while (GetMessage(&msg, NULL, 0, 0)) {
3179 DispatchMessage(&msg);