2 * windows.c: Windows front end for my puzzle collection.
9 #endif /* NO_HTMLHELP */
21 #define IDM_NEW 0x0010
22 #define IDM_RESTART 0x0020
23 #define IDM_UNDO 0x0030
24 #define IDM_REDO 0x0040
25 #define IDM_COPY 0x0050
26 #define IDM_SOLVE 0x0060
27 #define IDM_QUIT 0x0070
28 #define IDM_CONFIG 0x0080
29 #define IDM_DESC 0x0090
30 #define IDM_SEED 0x00A0
31 #define IDM_HELPC 0x00B0
32 #define IDM_GAMEHELP 0x00C0
33 #define IDM_ABOUT 0x00D0
34 #define IDM_SAVE 0x00E0
35 #define IDM_LOAD 0x00F0
36 #define IDM_PRINT 0x0100
37 #define IDM_PRESETS 0x0110
39 #define HELP_FILE_NAME "puzzles.hlp"
40 #define HELP_CNT_NAME "puzzles.cnt"
42 #define CHM_FILE_NAME "puzzles.chm"
43 #endif /* NO_HTMLHELP */
46 typedef HWND (CALLBACK *htmlhelp_t)(HWND, LPCSTR, UINT, DWORD);
47 static htmlhelp_t htmlhelp;
48 static HINSTANCE hh_dll;
49 #endif /* NO_HTMLHELP */
50 enum { NONE, HLP, CHM } help_type;
52 const char *help_topic;
53 int help_has_contents;
56 static FILE *debug_fp = NULL;
57 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
58 static int debug_got_console = 0;
64 if (!debug_got_console) {
66 debug_got_console = 1;
67 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
71 debug_fp = fopen("debug.log", "w");
74 if (debug_hdl != INVALID_HANDLE_VALUE) {
75 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
81 void debug_printf(char *fmt, ...)
87 vsprintf(buf, fmt, ap);
93 #define WINFLAGS (WS_OVERLAPPEDWINDOW &~ \
94 (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED))
96 static void new_game_size(frontend *fe);
114 enum { CFG_PRINT = CFG_FRONTEND_SPECIFIC };
118 HWND hwnd, statusbar, cfgbox;
120 HBITMAP bitmap, prevbm;
127 DWORD timer_last_tickcount;
129 game_params **presets;
131 int nfonts, fontsize;
133 struct cfg_aux *cfgaux;
134 int cfg_which, dlg_done;
139 enum { DRAWING, PRINTING, NOTHING } drawstatus;
141 int printcount, printw, printh, printsolns, printcurr, printcolour;
143 int printoffsetx, printoffsety;
144 float printpixelscale;
150 void fatal(char *fmt, ...)
156 vsprintf(buf, fmt, ap);
159 MessageBox(NULL, buf, "Fatal error", MB_ICONEXCLAMATION | MB_OK);
164 char *geterrstr(void)
167 DWORD dw = GetLastError();
171 FORMAT_MESSAGE_ALLOCATE_BUFFER |
172 FORMAT_MESSAGE_FROM_SYSTEM,
175 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
179 ret = dupstr(lpMsgBuf);
186 void get_random_seed(void **randseed, int *randseedsize)
188 time_t *tp = snew(time_t);
190 *randseed = (void *)tp;
191 *randseedsize = sizeof(time_t);
194 static void win_status_bar(void *handle, char *text)
196 frontend *fe = (frontend *)handle;
198 SetWindowText(fe->statusbar, text);
201 static blitter *win_blitter_new(void *handle, int w, int h)
203 blitter *bl = snew(blitter);
205 memset(bl, 0, sizeof(blitter));
213 static void win_blitter_free(void *handle, blitter *bl)
215 if (bl->bitmap) DeleteObject(bl->bitmap);
219 static void blitter_mkbitmap(frontend *fe, blitter *bl)
221 HDC hdc = GetDC(fe->hwnd);
222 bl->bitmap = CreateCompatibleBitmap(hdc, bl->w, bl->h);
223 ReleaseDC(fe->hwnd, hdc);
226 /* BitBlt(dstDC, dstX, dstY, dstW, dstH, srcDC, srcX, srcY, dType) */
228 static void win_blitter_save(void *handle, blitter *bl, int x, int y)
230 frontend *fe = (frontend *)handle;
231 HDC hdc_win, hdc_blit;
234 assert(fe->drawstatus == DRAWING);
236 if (!bl->bitmap) blitter_mkbitmap(fe, bl);
238 bl->x = x; bl->y = y;
240 hdc_win = GetDC(fe->hwnd);
241 hdc_blit = CreateCompatibleDC(hdc_win);
242 if (!hdc_blit) fatal("hdc_blit failed: 0x%x", GetLastError());
244 prev_blit = SelectObject(hdc_blit, bl->bitmap);
245 if (prev_blit == NULL || prev_blit == HGDI_ERROR)
246 fatal("SelectObject for hdc_main failed: 0x%x", GetLastError());
248 if (!BitBlt(hdc_blit, 0, 0, bl->w, bl->h,
249 fe->hdc, x, y, SRCCOPY))
250 fatal("BitBlt failed: 0x%x", GetLastError());
252 SelectObject(hdc_blit, prev_blit);
254 ReleaseDC(fe->hwnd, hdc_win);
257 static void win_blitter_load(void *handle, blitter *bl, int x, int y)
259 frontend *fe = (frontend *)handle;
260 HDC hdc_win, hdc_blit;
263 assert(fe->drawstatus == DRAWING);
265 assert(bl->bitmap); /* we should always have saved before loading */
267 if (x == BLITTER_FROMSAVED) x = bl->x;
268 if (y == BLITTER_FROMSAVED) y = bl->y;
270 hdc_win = GetDC(fe->hwnd);
271 hdc_blit = CreateCompatibleDC(hdc_win);
273 prev_blit = SelectObject(hdc_blit, bl->bitmap);
275 BitBlt(fe->hdc, x, y, bl->w, bl->h,
276 hdc_blit, 0, 0, SRCCOPY);
278 SelectObject(hdc_blit, prev_blit);
280 ReleaseDC(fe->hwnd, hdc_win);
283 void frontend_default_colour(frontend *fe, float *output)
285 DWORD c = GetSysColor(COLOR_MENU); /* ick */
287 output[0] = (float)(GetRValue(c) / 255.0);
288 output[1] = (float)(GetGValue(c) / 255.0);
289 output[2] = (float)(GetBValue(c) / 255.0);
292 static POINT win_transform_point(frontend *fe, int x, int y)
296 assert(fe->drawstatus != NOTHING);
298 if (fe->drawstatus == PRINTING) {
299 ret.x = (int)(fe->printoffsetx + fe->printpixelscale * x);
300 ret.y = (int)(fe->printoffsety + fe->printpixelscale * y);
309 static void win_text_colour(frontend *fe, int colour)
311 assert(fe->drawstatus != NOTHING);
313 if (fe->drawstatus == PRINTING) {
316 print_get_colour(fe->dr, colour, &hatch, &r, &g, &b);
318 SetTextColor(fe->hdc, RGB(r * 255, g * 255, b * 255));
320 SetTextColor(fe->hdc,
321 hatch == HATCH_CLEAR ? RGB(255,255,255) : RGB(0,0,0));
323 SetTextColor(fe->hdc, fe->colours[colour]);
327 static void win_set_brush(frontend *fe, int colour)
330 assert(fe->drawstatus != NOTHING);
332 if (fe->drawstatus == PRINTING) {
335 print_get_colour(fe->dr, colour, &hatch, &r, &g, &b);
338 br = CreateSolidBrush(RGB(r * 255, g * 255, b * 255));
339 else if (hatch == HATCH_SOLID)
340 br = CreateSolidBrush(RGB(0,0,0));
341 else if (hatch == HATCH_CLEAR)
342 br = CreateSolidBrush(RGB(255,255,255));
344 br = CreateHatchBrush(hatch == HATCH_BACKSLASH ? HS_FDIAGONAL :
345 hatch == HATCH_SLASH ? HS_BDIAGONAL :
346 hatch == HATCH_HORIZ ? HS_HORIZONTAL :
347 hatch == HATCH_VERT ? HS_VERTICAL :
348 hatch == HATCH_PLUS ? HS_CROSS :
349 /* hatch == HATCH_X ? */ HS_DIAGCROSS,
352 br = fe->brushes[colour];
354 fe->oldbr = SelectObject(fe->hdc, br);
357 static void win_reset_brush(frontend *fe)
361 assert(fe->drawstatus != NOTHING);
363 br = SelectObject(fe->hdc, fe->oldbr);
364 if (fe->drawstatus == PRINTING)
368 static void win_set_pen(frontend *fe, int colour, int thin)
371 assert(fe->drawstatus != NOTHING);
373 if (fe->drawstatus == PRINTING) {
376 int width = thin ? 0 : fe->linewidth;
378 print_get_colour(fe->dr, colour, &hatch, &r, &g, &b);
380 pen = CreatePen(PS_SOLID, width,
381 RGB(r * 255, g * 255, b * 255));
382 else if (hatch == HATCH_SOLID)
383 pen = CreatePen(PS_SOLID, width, RGB(0, 0, 0));
384 else if (hatch == HATCH_CLEAR)
385 pen = CreatePen(PS_SOLID, width, RGB(255,255,255));
387 assert(!"This shouldn't happen");
388 pen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
391 pen = fe->pens[colour];
393 fe->oldpen = SelectObject(fe->hdc, pen);
396 static void win_reset_pen(frontend *fe)
400 assert(fe->drawstatus != NOTHING);
402 pen = SelectObject(fe->hdc, fe->oldpen);
403 if (fe->drawstatus == PRINTING)
407 static void win_clip(void *handle, int x, int y, int w, int h)
409 frontend *fe = (frontend *)handle;
412 if (fe->drawstatus == NOTHING)
415 p = win_transform_point(fe, x, y);
416 q = win_transform_point(fe, x+w, y+h);
417 IntersectClipRect(fe->hdc, p.x, p.y, q.x, q.y);
420 static void win_unclip(void *handle)
422 frontend *fe = (frontend *)handle;
424 if (fe->drawstatus == NOTHING)
427 SelectClipRgn(fe->hdc, NULL);
430 static void win_draw_text(void *handle, int x, int y, int fonttype,
431 int fontsize, int align, int colour, char *text)
433 frontend *fe = (frontend *)handle;
437 if (fe->drawstatus == NOTHING)
440 if (fe->drawstatus == PRINTING)
441 fontsize = (int)(fontsize * fe->printpixelscale);
443 xy = win_transform_point(fe, x, y);
446 * Find or create the font.
448 for (i = fe->fontstart; i < fe->nfonts; i++)
449 if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
452 if (i == fe->nfonts) {
453 if (fe->fontsize <= fe->nfonts) {
454 fe->fontsize = fe->nfonts + 10;
455 fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
460 fe->fonts[i].type = fonttype;
461 fe->fonts[i].size = fontsize;
463 fe->fonts[i].font = CreateFont(-fontsize, 0, 0, 0,
464 fe->drawstatus == PRINTING ? 0 : FW_BOLD,
465 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
466 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
468 (fonttype == FONT_FIXED ?
469 FIXED_PITCH | FF_DONTCARE :
470 VARIABLE_PITCH | FF_SWISS),
475 * Position and draw the text.
482 oldfont = SelectObject(fe->hdc, fe->fonts[i].font);
483 if (GetTextMetrics(fe->hdc, &tm)) {
484 if (align & ALIGN_VCENTRE)
485 xy.y -= (tm.tmAscent+tm.tmDescent)/2;
489 if (GetTextExtentPoint32(fe->hdc, text, strlen(text), &size)) {
490 if (align & ALIGN_HCENTRE)
492 else if (align & ALIGN_HRIGHT)
495 SetBkMode(fe->hdc, TRANSPARENT);
496 win_text_colour(fe, colour);
497 TextOut(fe->hdc, xy.x, xy.y, text, strlen(text));
498 SelectObject(fe->hdc, oldfont);
502 static void win_draw_rect(void *handle, int x, int y, int w, int h, int colour)
504 frontend *fe = (frontend *)handle;
507 if (fe->drawstatus == NOTHING)
510 if (fe->drawstatus == DRAWING && w == 1 && h == 1) {
512 * Rectangle() appears to get uppity if asked to draw a 1x1
513 * rectangle, presumably on the grounds that that's beneath
514 * its dignity and you ought to be using SetPixel instead.
517 SetPixel(fe->hdc, x, y, fe->colours[colour]);
519 win_set_brush(fe, colour);
520 win_set_pen(fe, colour, TRUE);
521 p = win_transform_point(fe, x, y);
522 q = win_transform_point(fe, x+w, y+h);
523 Rectangle(fe->hdc, p.x, p.y, q.x, q.y);
529 static void win_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour)
531 frontend *fe = (frontend *)handle;
534 if (fe->drawstatus == NOTHING)
537 win_set_pen(fe, colour, FALSE);
538 p = win_transform_point(fe, x1, y1);
539 q = win_transform_point(fe, x2, y2);
540 MoveToEx(fe->hdc, p.x, p.y, NULL);
541 LineTo(fe->hdc, q.x, q.y);
542 if (fe->drawstatus == DRAWING)
543 SetPixel(fe->hdc, q.x, q.y, fe->colours[colour]);
547 static void win_draw_circle(void *handle, int cx, int cy, int radius,
548 int fillcolour, int outlinecolour)
550 frontend *fe = (frontend *)handle;
553 assert(outlinecolour >= 0);
555 if (fe->drawstatus == NOTHING)
558 if (fillcolour >= 0) {
559 win_set_brush(fe, fillcolour);
560 win_set_pen(fe, outlinecolour, FALSE);
561 p = win_transform_point(fe, cx - radius, cy - radius);
562 q = win_transform_point(fe, cx + radius, cy + radius);
563 Ellipse(fe->hdc, p.x, p.y, q.x+1, q.y+1);
567 win_set_pen(fe, outlinecolour, FALSE);
568 p = win_transform_point(fe, cx - radius, cy - radius);
569 q = win_transform_point(fe, cx + radius, cy + radius);
570 r = win_transform_point(fe, cx - radius, cy);
571 Arc(fe->hdc, p.x, p.y, q.x+1, q.y+1, r.x, r.y, r.x, r.y);
576 static void win_draw_polygon(void *handle, int *coords, int npoints,
577 int fillcolour, int outlinecolour)
579 frontend *fe = (frontend *)handle;
583 if (fe->drawstatus == NOTHING)
586 pts = snewn(npoints+1, POINT);
588 for (i = 0; i <= npoints; i++) {
589 int j = (i < npoints ? i : 0);
590 pts[i] = win_transform_point(fe, coords[j*2], coords[j*2+1]);
593 assert(outlinecolour >= 0);
595 if (fillcolour >= 0) {
596 win_set_brush(fe, fillcolour);
597 win_set_pen(fe, outlinecolour, FALSE);
598 Polygon(fe->hdc, pts, npoints);
602 win_set_pen(fe, outlinecolour, FALSE);
603 Polyline(fe->hdc, pts, npoints+1);
610 static void win_start_draw(void *handle)
612 frontend *fe = (frontend *)handle;
615 assert(fe->drawstatus == NOTHING);
617 hdc_win = GetDC(fe->hwnd);
618 fe->hdc = CreateCompatibleDC(hdc_win);
619 fe->prevbm = SelectObject(fe->hdc, fe->bitmap);
620 ReleaseDC(fe->hwnd, hdc_win);
622 SetMapMode(fe->hdc, MM_TEXT);
623 fe->drawstatus = DRAWING;
626 static void win_draw_update(void *handle, int x, int y, int w, int h)
628 frontend *fe = (frontend *)handle;
631 if (fe->drawstatus != DRAWING)
639 InvalidateRect(fe->hwnd, &r, FALSE);
642 static void win_end_draw(void *handle)
644 frontend *fe = (frontend *)handle;
645 assert(fe->drawstatus == DRAWING);
646 SelectObject(fe->hdc, fe->prevbm);
649 DeleteObject(fe->clip);
652 fe->drawstatus = NOTHING;
655 static void win_line_width(void *handle, float width)
657 frontend *fe = (frontend *)handle;
659 assert(fe->drawstatus != DRAWING);
660 if (fe->drawstatus == NOTHING)
663 fe->linewidth = (int)(width * fe->printpixelscale);
666 static void win_begin_doc(void *handle, int pages)
668 frontend *fe = (frontend *)handle;
670 assert(fe->drawstatus != DRAWING);
671 if (fe->drawstatus == NOTHING)
674 if (StartDoc(fe->hdc, &fe->di) <= 0) {
675 char *e = geterrstr();
676 MessageBox(fe->hwnd, e, "Error starting to print",
677 MB_ICONERROR | MB_OK);
679 fe->drawstatus = NOTHING;
683 * Push a marker on the font stack so that we won't use the
684 * same fonts for printing and drawing. (This is because
685 * drawing seems to look generally better in bold, but printing
686 * is better not in bold.)
688 fe->fontstart = fe->nfonts;
691 static void win_begin_page(void *handle, int number)
693 frontend *fe = (frontend *)handle;
695 assert(fe->drawstatus != DRAWING);
696 if (fe->drawstatus == NOTHING)
699 if (StartPage(fe->hdc) <= 0) {
700 char *e = geterrstr();
701 MessageBox(fe->hwnd, e, "Error starting a page",
702 MB_ICONERROR | MB_OK);
704 fe->drawstatus = NOTHING;
708 static void win_begin_puzzle(void *handle, float xm, float xc,
709 float ym, float yc, int pw, int ph, float wmm)
711 frontend *fe = (frontend *)handle;
712 int ppw, pph, pox, poy;
713 float mmpw, mmph, mmox, mmoy;
716 assert(fe->drawstatus != DRAWING);
717 if (fe->drawstatus == NOTHING)
720 ppw = GetDeviceCaps(fe->hdc, HORZRES);
721 pph = GetDeviceCaps(fe->hdc, VERTRES);
722 mmpw = (float)GetDeviceCaps(fe->hdc, HORZSIZE);
723 mmph = (float)GetDeviceCaps(fe->hdc, VERTSIZE);
726 * Compute the puzzle's position on the logical page.
728 mmox = xm * mmpw + xc;
729 mmoy = ym * mmph + yc;
732 * Work out what that comes to in pixels.
734 pox = (int)(mmox * (float)ppw / mmpw);
735 poy = (int)(mmoy * (float)ppw / mmpw);
738 * And determine the scale.
740 * I need a scale such that the maximum puzzle-coordinate
741 * extent of the rectangle (pw * scale) is equal to the pixel
742 * equivalent of the puzzle's millimetre width (wmm * ppw /
745 scale = (wmm * ppw) / (mmpw * pw);
748 * Now store pox, poy and scale for use in the main drawing
751 fe->printoffsetx = pox;
752 fe->printoffsety = poy;
753 fe->printpixelscale = scale;
758 static void win_end_puzzle(void *handle)
760 /* Nothing needs to be done here. */
763 static void win_end_page(void *handle, int number)
765 frontend *fe = (frontend *)handle;
767 assert(fe->drawstatus != DRAWING);
769 if (fe->drawstatus == NOTHING)
772 if (EndPage(fe->hdc) <= 0) {
773 char *e = geterrstr();
774 MessageBox(fe->hwnd, e, "Error finishing a page",
775 MB_ICONERROR | MB_OK);
777 fe->drawstatus = NOTHING;
781 static void win_end_doc(void *handle)
783 frontend *fe = (frontend *)handle;
785 assert(fe->drawstatus != DRAWING);
788 * Free all the fonts created since we began printing.
790 while (fe->nfonts > fe->fontstart) {
792 DeleteObject(fe->fonts[fe->nfonts].font);
797 * The MSDN web site sample code doesn't bother to call EndDoc
798 * if an error occurs half way through printing. I expect doing
799 * so would cause the erroneous document to actually be
800 * printed, or something equally undesirable.
802 if (fe->drawstatus == NOTHING)
805 if (EndDoc(fe->hdc) <= 0) {
806 char *e = geterrstr();
807 MessageBox(fe->hwnd, e, "Error finishing printing",
808 MB_ICONERROR | MB_OK);
810 fe->drawstatus = NOTHING;
814 const struct drawing_api win_drawing = {
839 void print(frontend *fe)
844 midend *nme = NULL; /* non-interactive midend for bulk puzzle generation */
849 * Create our document structure and fill it up with puzzles.
851 doc = document_new(fe->printw, fe->printh, fe->printscale / 100.0F);
852 for (i = 0; i < fe->printcount; i++) {
853 if (i == 0 && fe->printcurr) {
854 err = midend_print_puzzle(fe->me, doc, fe->printsolns);
859 nme = midend_new(NULL, &thegame, NULL, NULL);
862 * Set the non-interactive mid-end to have the same
863 * parameters as the standard one.
865 params = midend_get_params(fe->me);
866 midend_set_params(nme, params);
867 thegame.free_params(params);
870 midend_new_game(nme);
871 err = midend_print_puzzle(nme, doc, fe->printsolns);
880 MessageBox(fe->hwnd, err, "Error preparing puzzles for printing",
881 MB_ICONERROR | MB_OK);
886 memset(&pd, 0, sizeof(pd));
887 pd.lStructSize = sizeof(pd);
888 pd.hwndOwner = fe->hwnd;
891 pd.Flags = PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC |
892 PD_NOPAGENUMS | PD_NOSELECTION;
894 pd.nFromPage = pd.nToPage = 0xFFFF;
895 pd.nMinPage = pd.nMaxPage = 1;
897 if (!PrintDlg(&pd)) {
903 * Now pd.hDC is a device context for the printer.
907 * FIXME: IWBNI we put up an Abort box here.
910 memset(&fe->di, 0, sizeof(fe->di));
911 fe->di.cbSize = sizeof(fe->di);
912 sprintf(doctitle, "Printed puzzles from %s (from Simon Tatham's"
913 " Portable Puzzle Collection)", thegame.name);
914 fe->di.lpszDocName = doctitle;
915 fe->di.lpszOutput = NULL;
916 fe->di.lpszDatatype = NULL;
919 fe->drawstatus = PRINTING;
922 fe->dr = drawing_new(&win_drawing, NULL, fe);
923 document_print(doc, fe->dr);
924 drawing_free(fe->dr);
927 fe->drawstatus = NOTHING;
933 void deactivate_timer(frontend *fe)
936 return; /* for non-interactive midend */
937 if (fe->hwnd) KillTimer(fe->hwnd, fe->timer);
941 void activate_timer(frontend *fe)
944 return; /* for non-interactive midend */
946 fe->timer = SetTimer(fe->hwnd, fe->timer, 20, NULL);
947 fe->timer_last_tickcount = GetTickCount();
951 void write_clip(HWND hwnd, char *data)
959 * Windows expects CRLF in the clipboard, so we must convert
960 * any \n that has come out of the puzzle backend.
963 for (i = 0; data[i]; i++) {
968 data2 = snewn(len+1, char);
970 for (i = 0; data[i]; i++) {
973 data2[j++] = data[i];
978 clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
981 lock = GlobalLock(clipdata);
984 memcpy(lock, data2, len);
985 ((unsigned char *) lock)[len] = 0;
986 GlobalUnlock(clipdata);
988 if (OpenClipboard(hwnd)) {
990 SetClipboardData(CF_TEXT, clipdata);
993 GlobalFree(clipdata);
999 * Set up Help and see if we can find a help file.
1001 static void init_help(void)
1003 char b[2048], *p, *q, *r;
1007 * Find the executable file path, so we can look alongside
1008 * it for help files. Trim the filename off the end.
1010 GetModuleFileName(NULL, b, sizeof(b) - 1);
1012 p = strrchr(b, '\\');
1013 if (p && p >= r) r = p+1;
1014 q = strrchr(b, ':');
1015 if (q && q >= r) r = q+1;
1019 * Try HTML Help first.
1021 strcpy(r, CHM_FILE_NAME);
1022 if ( (fp = fopen(b, "r")) != NULL) {
1026 * We have a .CHM. See if we can use it.
1028 hh_dll = LoadLibrary("hhctrl.ocx");
1030 htmlhelp = (htmlhelp_t)GetProcAddress(hh_dll, "HtmlHelpA");
1032 FreeLibrary(hh_dll);
1035 help_path = dupstr(b);
1037 help_topic = thegame.htmlhelp_topic;
1041 #endif /* NO_HTMLHELP */
1044 * Now try old-style .HLP.
1046 strcpy(r, HELP_FILE_NAME);
1047 if ( (fp = fopen(b, "r")) != NULL) {
1050 help_path = dupstr(b);
1053 help_topic = thegame.winhelp_topic;
1056 * See if there's a .CNT file alongside it.
1058 strcpy(r, HELP_CNT_NAME);
1059 if ( (fp = fopen(b, "r")) != NULL) {
1061 help_has_contents = TRUE;
1063 help_has_contents = FALSE;
1068 help_type = NONE; /* didn't find any */
1074 static void start_help(frontend *fe, const char *topic)
1079 switch (help_type) {
1083 str = snewn(10+strlen(topic), char);
1084 sprintf(str, "JI(`',`%s')", topic);
1086 } else if (help_has_contents) {
1089 cmd = HELP_CONTENTS;
1091 WinHelp(fe->hwnd, help_path, cmd, (DWORD)str);
1092 fe->help_running = TRUE;
1099 str = snewn(20 + strlen(topic) + strlen(help_path), char);
1100 sprintf(str, "%s::/%s.html>main", help_path, topic);
1102 str = dupstr(help_path);
1104 htmlhelp(fe->hwnd, str, HH_DISPLAY_TOPIC, 0);
1105 fe->help_running = TRUE;
1107 #endif /* NO_HTMLHELP */
1109 assert(!"This shouldn't happen");
1117 * Stop Help on window cleanup.
1119 static void stop_help(frontend *fe)
1121 if (fe->help_running) {
1122 switch (help_type) {
1124 WinHelp(fe->hwnd, help_path, HELP_QUIT, 0);
1129 htmlhelp(NULL, NULL, HH_CLOSE_ALL, 0);
1131 #endif /* NO_HTMLHELP */
1133 assert(!"This shouldn't happen");
1136 fe->help_running = FALSE;
1141 * Terminate Help on process exit.
1143 static void cleanup_help(void)
1145 /* Nothing to do currently.
1146 * (If we were running HTML Help single-threaded, this is where we'd
1147 * call HH_UNINITIALIZE.) */
1150 static void check_window_size(frontend *fe, int *px, int *py)
1155 if (fe->statusbar) {
1157 GetWindowRect(fe->statusbar, &sr);
1158 sy = sr.bottom - sr.top;
1164 * See if we actually got the window size we wanted, and adjust
1165 * the puzzle size if not.
1167 GetClientRect(fe->hwnd, &r);
1168 x = r.right - r.left;
1169 y = r.bottom - r.top - sy;
1170 midend_size(fe->me, &x, &y, FALSE);
1171 if (x != r.right - r.left || y != r.bottom - r.top) {
1173 * Resize the window, now we know what size we _really_
1179 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1180 SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left, r.bottom - r.top,
1181 SWP_NOMOVE | SWP_NOZORDER);
1184 if (fe->statusbar) {
1185 GetClientRect(fe->hwnd, &r);
1186 SetWindowPos(fe->statusbar, NULL, 0, r.bottom-r.top-sy, r.right-r.left,
1194 static void get_max_puzzle_size(frontend *fe, int *x, int *y)
1198 if (SystemParametersInfo(SPI_GETWORKAREA, 0, &sr, FALSE)) {
1199 *x = sr.right - sr.left;
1200 *y = sr.bottom - sr.top;
1205 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1206 *x -= r.right - r.left - 100;
1207 *y -= r.bottom - r.top - 100;
1212 if (fe->statusbar != NULL) {
1213 GetWindowRect(fe->statusbar, &sr);
1214 *y -= sr.bottom - sr.top;
1218 static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
1224 fe = snew(frontend);
1226 fe->me = midend_new(fe, &thegame, &win_drawing, fe);
1229 *error = midend_game_id(fe->me, game_id);
1231 midend_free(fe->me);
1242 fe->help_running = FALSE;
1244 fe->drawstatus = NOTHING;
1248 midend_new_game(fe->me);
1251 fe->nfonts = fe->fontsize = 0;
1257 colours = midend_colours(fe->me, &ncolours);
1259 fe->colours = snewn(ncolours, COLORREF);
1260 fe->brushes = snewn(ncolours, HBRUSH);
1261 fe->pens = snewn(ncolours, HPEN);
1263 for (i = 0; i < ncolours; i++) {
1264 fe->colours[i] = RGB(255 * colours[i*3+0],
1265 255 * colours[i*3+1],
1266 255 * colours[i*3+2]);
1267 fe->brushes[i] = CreateSolidBrush(fe->colours[i]);
1268 fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]);
1273 if (midend_wants_statusbar(fe->me)) {
1274 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh",
1275 WS_CHILD | WS_VISIBLE,
1276 0, 0, 0, 0, /* status bar does these */
1277 NULL, NULL, inst, NULL);
1279 fe->statusbar = NULL;
1281 get_max_puzzle_size(fe, &x, &y);
1282 midend_size(fe->me, &x, &y, FALSE);
1287 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1289 fe->hwnd = CreateWindowEx(0, thegame.name, thegame.name,
1290 WS_OVERLAPPEDWINDOW &~
1291 (WS_THICKFRAME | WS_MAXIMIZEBOX),
1292 CW_USEDEFAULT, CW_USEDEFAULT,
1293 r.right - r.left, r.bottom - r.top,
1294 NULL, NULL, inst, NULL);
1296 if (midend_wants_statusbar(fe->me)) {
1298 DestroyWindow(fe->statusbar);
1299 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh",
1300 WS_CHILD | WS_VISIBLE,
1301 0, 0, 0, 0, /* status bar does these */
1302 fe->hwnd, NULL, inst, NULL);
1304 * Now resize the window to take account of the status bar.
1306 GetWindowRect(fe->statusbar, &sr);
1307 GetWindowRect(fe->hwnd, &r);
1308 SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left,
1309 r.bottom - r.top + sr.bottom - sr.top,
1310 SWP_NOMOVE | SWP_NOZORDER);
1312 fe->statusbar = NULL;
1316 HMENU bar = CreateMenu();
1317 HMENU menu = CreateMenu();
1319 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Game");
1320 AppendMenu(menu, MF_ENABLED, IDM_NEW, "New");
1321 AppendMenu(menu, MF_ENABLED, IDM_RESTART, "Restart");
1322 AppendMenu(menu, MF_ENABLED, IDM_DESC, "Specific...");
1323 AppendMenu(menu, MF_ENABLED, IDM_SEED, "Random Seed...");
1325 if ((fe->npresets = midend_num_presets(fe->me)) > 0 ||
1326 thegame.can_configure) {
1327 HMENU sub = CreateMenu();
1330 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "Type");
1332 fe->presets = snewn(fe->npresets, game_params *);
1334 for (i = 0; i < fe->npresets; i++) {
1337 midend_fetch_preset(fe->me, i, &name, &fe->presets[i]);
1340 * FIXME: we ought to go through and do something
1341 * with ampersands here.
1344 AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name);
1347 if (thegame.can_configure) {
1348 AppendMenu(sub, MF_ENABLED, IDM_CONFIG, "Custom...");
1352 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1353 AppendMenu(menu, MF_ENABLED, IDM_LOAD, "Load...");
1354 AppendMenu(menu, MF_ENABLED, IDM_SAVE, "Save...");
1355 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1356 if (thegame.can_print) {
1357 AppendMenu(menu, MF_ENABLED, IDM_PRINT, "Print...");
1358 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1360 AppendMenu(menu, MF_ENABLED, IDM_UNDO, "Undo");
1361 AppendMenu(menu, MF_ENABLED, IDM_REDO, "Redo");
1362 if (thegame.can_format_as_text) {
1363 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1364 AppendMenu(menu, MF_ENABLED, IDM_COPY, "Copy");
1366 if (thegame.can_solve) {
1367 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1368 AppendMenu(menu, MF_ENABLED, IDM_SOLVE, "Solve");
1370 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1371 AppendMenu(menu, MF_ENABLED, IDM_QUIT, "Exit");
1372 menu = CreateMenu();
1373 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Help");
1374 AppendMenu(menu, MF_ENABLED, IDM_ABOUT, "About");
1375 if (help_type != NONE) {
1376 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1377 AppendMenu(menu, MF_ENABLED, IDM_HELPC, "Contents");
1380 assert(thegame.name);
1381 item = snewn(9+strlen(thegame.name), char); /*ick*/
1382 sprintf(item, "Help on %s", thegame.name);
1383 AppendMenu(menu, MF_ENABLED, IDM_GAMEHELP, item);
1387 SetMenu(fe->hwnd, bar);
1391 new_game_size(fe); /* initialises fe->bitmap */
1392 check_window_size(fe, &x, &y);
1394 SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe);
1396 ShowWindow(fe->hwnd, SW_NORMAL);
1397 SetForegroundWindow(fe->hwnd);
1399 midend_redraw(fe->me);
1404 static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg,
1405 WPARAM wParam, LPARAM lParam)
1407 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1414 if ((HIWORD(wParam) == BN_CLICKED ||
1415 HIWORD(wParam) == BN_DOUBLECLICKED) &&
1416 LOWORD(wParam) == IDOK)
1429 * Wrappers on midend_{get,set}_config, which extend the CFG_*
1430 * enumeration to add CFG_PRINT.
1432 static config_item *frontend_get_config(frontend *fe, int which,
1435 if (which < CFG_FRONTEND_SPECIFIC) {
1436 return midend_get_config(fe->me, which, wintitle);
1437 } else if (which == CFG_PRINT) {
1441 *wintitle = snewn(40 + strlen(thegame.name), char);
1442 sprintf(*wintitle, "%s print setup", thegame.name);
1444 ret = snewn(8, config_item);
1448 ret[i].name = "Number of puzzles to print";
1449 ret[i].type = C_STRING;
1450 ret[i].sval = dupstr("1");
1454 ret[i].name = "Number of puzzles across the page";
1455 ret[i].type = C_STRING;
1456 ret[i].sval = dupstr("1");
1460 ret[i].name = "Number of puzzles down the page";
1461 ret[i].type = C_STRING;
1462 ret[i].sval = dupstr("1");
1466 ret[i].name = "Percentage of standard size";
1467 ret[i].type = C_STRING;
1468 ret[i].sval = dupstr("100.0");
1472 ret[i].name = "Include currently shown puzzle";
1473 ret[i].type = C_BOOLEAN;
1478 ret[i].name = "Print solutions";
1479 ret[i].type = C_BOOLEAN;
1481 ret[i].ival = FALSE;
1484 if (thegame.can_print_in_colour) {
1485 ret[i].name = "Print in colour";
1486 ret[i].type = C_BOOLEAN;
1488 ret[i].ival = FALSE;
1493 ret[i].type = C_END;
1500 assert(!"We should never get here");
1505 static char *frontend_set_config(frontend *fe, int which, config_item *cfg)
1507 if (which < CFG_FRONTEND_SPECIFIC) {
1508 return midend_set_config(fe->me, which, cfg);
1509 } else if (which == CFG_PRINT) {
1510 if ((fe->printcount = atoi(cfg[0].sval)) <= 0)
1511 return "Number of puzzles to print should be at least one";
1512 if ((fe->printw = atoi(cfg[1].sval)) <= 0)
1513 return "Number of puzzles across the page should be at least one";
1514 if ((fe->printh = atoi(cfg[2].sval)) <= 0)
1515 return "Number of puzzles down the page should be at least one";
1516 if ((fe->printscale = (float)atof(cfg[3].sval)) <= 0)
1517 return "Print size should be positive";
1518 fe->printcurr = cfg[4].ival;
1519 fe->printsolns = cfg[5].ival;
1520 fe->printcolour = thegame.can_print_in_colour && cfg[6].ival;
1523 assert(!"We should never get here");
1524 return "Internal error";
1528 static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg,
1529 WPARAM wParam, LPARAM lParam)
1531 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1541 * OK and Cancel are special cases.
1543 if ((HIWORD(wParam) == BN_CLICKED ||
1544 HIWORD(wParam) == BN_DOUBLECLICKED) &&
1545 (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) {
1546 if (LOWORD(wParam) == IDOK) {
1547 char *err = frontend_set_config(fe, fe->cfg_which, fe->cfg);
1550 MessageBox(hwnd, err, "Validation error",
1551 MB_ICONERROR | MB_OK);
1562 * First find the control whose id this is.
1564 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
1565 if (j->ctlid == LOWORD(wParam))
1568 if (i->type == C_END)
1569 return 0; /* not our problem */
1571 if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) {
1573 GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer));
1574 buffer[lenof(buffer)-1] = '\0';
1576 i->sval = dupstr(buffer);
1577 } else if (i->type == C_BOOLEAN &&
1578 (HIWORD(wParam) == BN_CLICKED ||
1579 HIWORD(wParam) == BN_DOUBLECLICKED)) {
1580 i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid);
1581 } else if (i->type == C_CHOICES &&
1582 HIWORD(wParam) == CBN_SELCHANGE) {
1583 i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid,
1584 CB_GETCURSEL, 0, 0);
1597 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
1598 char *wclass, int wstyle,
1599 int exstyle, const char *wtext, int wid)
1602 ret = CreateWindowEx(exstyle, wclass, wtext,
1603 wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1,
1604 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
1605 SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0));
1609 static void about(frontend *fe)
1619 int winwidth, winheight, y;
1620 int height, width, maxwid;
1621 const char *strings[16];
1626 sprintf(titlebuf, "About %.250s", thegame.name);
1628 strings[nstrings++] = thegame.name;
1629 strings[nstrings++] = "from Simon Tatham's Portable Puzzle Collection";
1630 strings[nstrings++] = ver;
1632 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
1633 wc.lpfnWndProc = DefDlgProc;
1635 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
1636 wc.hInstance = fe->inst;
1638 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
1639 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
1640 wc.lpszMenuName = NULL;
1641 wc.lpszClassName = "GameAboutBox";
1644 hdc = GetDC(fe->hwnd);
1645 SetMapMode(hdc, MM_TEXT);
1647 fe->dlg_done = FALSE;
1649 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
1651 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
1652 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
1657 oldfont = SelectObject(hdc, fe->cfgfont);
1658 if (GetTextMetrics(hdc, &tm)) {
1659 height = tm.tmAscent + tm.tmDescent;
1660 width = tm.tmAveCharWidth;
1662 height = width = 30;
1666 * Figure out the layout of the About box by measuring the
1667 * length of each piece of text.
1670 winheight = height/2;
1672 for (i = 0; i < nstrings; i++) {
1673 if (GetTextExtentPoint32(hdc, strings[i], strlen(strings[i]), &size))
1674 lengths[i] = size.cx;
1676 lengths[i] = 0; /* *shrug* */
1677 if (maxwid < lengths[i])
1678 maxwid = lengths[i];
1679 winheight += height * 3 / 2 + (height / 2);
1682 winheight += height + height * 7 / 4; /* OK button */
1683 winwidth = maxwid + 4*width;
1685 SelectObject(hdc, oldfont);
1686 ReleaseDC(fe->hwnd, hdc);
1689 * Create the dialog, now that we know its size.
1696 r.bottom = winheight;
1698 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
1699 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1700 WS_CAPTION | WS_SYSMENU*/) &~
1701 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
1705 * Centre the dialog on its parent window.
1709 GetWindowRect(fe->hwnd, &r2);
1710 r.left = (r2.left + r2.right - r.right) / 2;
1711 r.top = (r2.top + r2.bottom - r.bottom) / 2;
1715 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, titlebuf,
1716 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1717 WS_CAPTION | WS_SYSMENU,
1719 r.right-r.left, r.bottom-r.top,
1720 fe->hwnd, NULL, fe->inst, NULL);
1723 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
1725 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
1726 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)AboutDlgProc);
1730 for (i = 0; i < nstrings; i++) {
1731 int border = width*2 + (maxwid - lengths[i]) / 2;
1732 mkctrl(fe, border, border+lengths[i], y+height*1/8, y+height*9/8,
1733 "Static", 0, 0, strings[i], id++);
1736 assert(y < winheight);
1740 y += height/2; /* extra space before OK */
1741 mkctrl(fe, width*2, maxwid+width*2, y, y+height*7/4, "BUTTON",
1742 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
1745 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
1747 EnableWindow(fe->hwnd, FALSE);
1748 ShowWindow(fe->cfgbox, SW_NORMAL);
1749 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
1750 if (!IsDialogMessage(fe->cfgbox, &msg))
1751 DispatchMessage(&msg);
1755 EnableWindow(fe->hwnd, TRUE);
1756 SetForegroundWindow(fe->hwnd);
1757 DestroyWindow(fe->cfgbox);
1758 DeleteObject(fe->cfgfont);
1761 static int get_config(frontend *fe, int which)
1774 int winwidth, winheight, col1l, col1r, col2l, col2r, y;
1775 int height, width, maxlabel, maxcheckbox;
1777 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
1778 wc.lpfnWndProc = DefDlgProc;
1780 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
1781 wc.hInstance = fe->inst;
1783 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
1784 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
1785 wc.lpszMenuName = NULL;
1786 wc.lpszClassName = "GameConfigBox";
1789 hdc = GetDC(fe->hwnd);
1790 SetMapMode(hdc, MM_TEXT);
1792 fe->dlg_done = FALSE;
1794 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
1796 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
1797 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
1802 oldfont = SelectObject(hdc, fe->cfgfont);
1803 if (GetTextMetrics(hdc, &tm)) {
1804 height = tm.tmAscent + tm.tmDescent;
1805 width = tm.tmAveCharWidth;
1807 height = width = 30;
1810 fe->cfg = frontend_get_config(fe, which, &title);
1811 fe->cfg_which = which;
1814 * Figure out the layout of the config box by measuring the
1815 * length of each piece of text.
1817 maxlabel = maxcheckbox = 0;
1818 winheight = height/2;
1820 for (i = fe->cfg; i->type != C_END; i++) {
1825 * Both these control types have a label filling only
1826 * the left-hand column of the box.
1828 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
1831 winheight += height * 3 / 2 + (height / 2);
1836 * Checkboxes take up the whole of the box width.
1838 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
1839 maxcheckbox < size.cx)
1840 maxcheckbox = size.cx;
1841 winheight += height + (height / 2);
1846 winheight += height + height * 7 / 4; /* OK / Cancel buttons */
1849 col1r = col1l + maxlabel;
1850 col2l = col1r + 2*width;
1851 col2r = col2l + 30*width;
1852 if (col2r < col1l+2*height+maxcheckbox)
1853 col2r = col1l+2*height+maxcheckbox;
1854 winwidth = col2r + 2*width;
1856 SelectObject(hdc, oldfont);
1857 ReleaseDC(fe->hwnd, hdc);
1860 * Create the dialog, now that we know its size.
1867 r.bottom = winheight;
1869 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
1870 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1871 WS_CAPTION | WS_SYSMENU*/) &~
1872 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
1876 * Centre the dialog on its parent window.
1880 GetWindowRect(fe->hwnd, &r2);
1881 r.left = (r2.left + r2.right - r.right) / 2;
1882 r.top = (r2.top + r2.bottom - r.bottom) / 2;
1886 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title,
1887 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1888 WS_CAPTION | WS_SYSMENU,
1890 r.right-r.left, r.bottom-r.top,
1891 fe->hwnd, NULL, fe->inst, NULL);
1895 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
1897 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
1898 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc);
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);
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+height*1/8, y+height*9/8,
1916 "Static", 0, 0, i->name, id++);
1917 ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2,
1918 "EDIT", WS_TABSTOP | ES_AUTOHSCROLL,
1919 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
1920 SetWindowText(ctl, i->sval);
1928 mkctrl(fe, col1l, col2r, y, y+height, "BUTTON",
1929 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
1930 0, i->name, (j->ctlid = id++));
1931 CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0));
1937 * Drop-down list with a label beside it.
1939 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
1940 "STATIC", 0, 0, i->name, id++);
1941 ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2,
1942 "COMBOBOX", WS_TABSTOP |
1943 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
1944 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
1946 char c, *p, *q, *str;
1948 SendMessage(ctl, CB_RESETCONTENT, 0, 0);
1953 while (*q && *q != c) q++;
1954 str = snewn(q-p+1, char);
1955 strncpy(str, p, q-p);
1957 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str);
1964 SendMessage(ctl, CB_SETCURSEL, i->ival, 0);
1970 assert(y < winheight);
1974 y += height/2; /* extra space before OK and Cancel */
1975 mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON",
1976 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
1978 mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON",
1979 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL);
1981 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
1983 EnableWindow(fe->hwnd, FALSE);
1984 ShowWindow(fe->cfgbox, SW_NORMAL);
1985 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
1986 if (!IsDialogMessage(fe->cfgbox, &msg))
1987 DispatchMessage(&msg);
1991 EnableWindow(fe->hwnd, TRUE);
1992 SetForegroundWindow(fe->hwnd);
1993 DestroyWindow(fe->cfgbox);
1994 DeleteObject(fe->cfgfont);
1999 return (fe->dlg_done == 2);
2002 static void new_game_size(frontend *fe)
2008 get_max_puzzle_size(fe, &x, &y);
2009 midend_size(fe->me, &x, &y, FALSE);
2014 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
2016 if (fe->statusbar != NULL) {
2017 GetWindowRect(fe->statusbar, &sr);
2019 sr.left = sr.right = sr.top = sr.bottom = 0;
2021 SetWindowPos(fe->hwnd, NULL, 0, 0,
2023 r.bottom - r.top + sr.bottom - sr.top,
2024 SWP_NOMOVE | SWP_NOZORDER);
2026 check_window_size(fe, &x, &y);
2028 if (fe->statusbar != NULL)
2029 SetWindowPos(fe->statusbar, NULL, 0, y, x,
2030 sr.bottom - sr.top, SWP_NOZORDER);
2032 if (fe->bitmap) DeleteObject(fe->bitmap);
2034 hdc = GetDC(fe->hwnd);
2035 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
2036 ReleaseDC(fe->hwnd, hdc);
2038 midend_redraw(fe->me);
2041 static void new_game_type(frontend *fe)
2043 midend_new_game(fe->me);
2047 static int is_alt_pressed(void)
2050 int r = GetKeyboardState(keystate);
2053 if (keystate[VK_MENU] & 0x80)
2055 if (keystate[VK_RMENU] & 0x80)
2060 static void savefile_write(void *wctx, void *buf, int len)
2062 FILE *fp = (FILE *)wctx;
2063 fwrite(buf, 1, len, fp);
2066 static int savefile_read(void *wctx, void *buf, int len)
2068 FILE *fp = (FILE *)wctx;
2071 ret = fread(buf, 1, len, fp);
2072 return (ret == len);
2075 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
2076 WPARAM wParam, LPARAM lParam)
2078 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
2083 DestroyWindow(hwnd);
2086 cmd = wParam & ~0xF; /* low 4 bits reserved to Windows */
2089 if (!midend_process_key(fe->me, 0, 0, 'n'))
2093 midend_restart_game(fe->me);
2096 if (!midend_process_key(fe->me, 0, 0, 'u'))
2100 if (!midend_process_key(fe->me, 0, 0, '\x12'))
2105 char *text = midend_text_format(fe->me);
2107 write_clip(hwnd, text);
2109 MessageBeep(MB_ICONWARNING);
2115 char *msg = midend_solve(fe->me);
2117 MessageBox(hwnd, msg, "Unable to solve",
2118 MB_ICONERROR | MB_OK);
2122 if (!midend_process_key(fe->me, 0, 0, 'q'))
2126 if (get_config(fe, CFG_SETTINGS))
2130 if (get_config(fe, CFG_SEED))
2134 if (get_config(fe, CFG_DESC))
2138 if (get_config(fe, CFG_PRINT))
2148 char filename[FILENAME_MAX];
2151 memset(&of, 0, sizeof(of));
2152 of.hwndOwner = hwnd;
2153 of.lpstrFilter = "All Files (*.*)\0*\0\0\0";
2154 of.lpstrCustomFilter = NULL;
2155 of.nFilterIndex = 1;
2156 of.lpstrFile = filename;
2158 of.nMaxFile = lenof(filename);
2159 of.lpstrFileTitle = NULL;
2160 of.lpstrTitle = (cmd == IDM_SAVE ?
2161 "Enter name of game file to save" :
2162 "Enter name of saved game file to load");
2164 #ifdef OPENFILENAME_SIZE_VERSION_400
2165 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
2167 of.lStructSize = sizeof(of);
2169 of.lpstrInitialDir = NULL;
2171 if (cmd == IDM_SAVE)
2172 ret = GetSaveFileName(&of);
2174 ret = GetOpenFileName(&of);
2177 if (cmd == IDM_SAVE) {
2180 if ((fp = fopen(filename, "r")) != NULL) {
2181 char buf[256 + FILENAME_MAX];
2185 sprintf(buf, "Are you sure you want to overwrite"
2186 " the file \"%.*s\"?",
2187 FILENAME_MAX, filename);
2188 if (MessageBox(hwnd, buf, "Question",
2189 MB_YESNO | MB_ICONQUESTION)
2194 fp = fopen(filename, "w");
2197 MessageBox(hwnd, "Unable to open save file",
2198 "Error", MB_ICONERROR | MB_OK);
2202 midend_serialise(fe->me, savefile_write, fp);
2206 FILE *fp = fopen(filename, "r");
2210 MessageBox(hwnd, "Unable to open saved game file",
2211 "Error", MB_ICONERROR | MB_OK);
2215 err = midend_deserialise(fe->me, savefile_read, fp);
2220 MessageBox(hwnd, err, "Error", MB_ICONERROR|MB_OK);
2231 start_help(fe, NULL);
2234 start_help(fe, help_topic);
2238 int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10;
2240 if (p >= 0 && p < fe->npresets) {
2241 midend_set_params(fe->me, fe->presets[p]);
2258 hdc = BeginPaint(hwnd, &p);
2259 hdc2 = CreateCompatibleDC(hdc);
2260 prevbm = SelectObject(hdc2, fe->bitmap);
2262 p.rcPaint.left, p.rcPaint.top,
2263 p.rcPaint.right - p.rcPaint.left,
2264 p.rcPaint.bottom - p.rcPaint.top,
2266 p.rcPaint.left, p.rcPaint.top,
2268 SelectObject(hdc2, prevbm);
2277 int r = GetKeyboardState(keystate);
2278 int shift = (r && (keystate[VK_SHIFT] & 0x80)) ? MOD_SHFT : 0;
2279 int ctrl = (r && (keystate[VK_CONTROL] & 0x80)) ? MOD_CTRL : 0;
2283 if (!(lParam & 0x01000000))
2284 key = MOD_NUM_KEYPAD | '4';
2286 key = shift | ctrl | CURSOR_LEFT;
2289 if (!(lParam & 0x01000000))
2290 key = MOD_NUM_KEYPAD | '6';
2292 key = shift | ctrl | CURSOR_RIGHT;
2295 if (!(lParam & 0x01000000))
2296 key = MOD_NUM_KEYPAD | '8';
2298 key = shift | ctrl | CURSOR_UP;
2301 if (!(lParam & 0x01000000))
2302 key = MOD_NUM_KEYPAD | '2';
2304 key = shift | ctrl | CURSOR_DOWN;
2307 * Diagonal keys on the numeric keypad.
2310 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '9';
2313 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '3';
2316 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '7';
2319 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '1';
2322 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '0';
2325 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '5';
2328 * Numeric keypad keys with Num Lock on.
2330 case VK_NUMPAD4: key = MOD_NUM_KEYPAD | '4'; break;
2331 case VK_NUMPAD6: key = MOD_NUM_KEYPAD | '6'; break;
2332 case VK_NUMPAD8: key = MOD_NUM_KEYPAD | '8'; break;
2333 case VK_NUMPAD2: key = MOD_NUM_KEYPAD | '2'; break;
2334 case VK_NUMPAD5: key = MOD_NUM_KEYPAD | '5'; break;
2335 case VK_NUMPAD9: key = MOD_NUM_KEYPAD | '9'; break;
2336 case VK_NUMPAD3: key = MOD_NUM_KEYPAD | '3'; break;
2337 case VK_NUMPAD7: key = MOD_NUM_KEYPAD | '7'; break;
2338 case VK_NUMPAD1: key = MOD_NUM_KEYPAD | '1'; break;
2339 case VK_NUMPAD0: key = MOD_NUM_KEYPAD | '0'; break;
2343 if (!midend_process_key(fe->me, 0, 0, key))
2348 m.message = WM_KEYDOWN;
2350 m.lParam = lParam & 0xdfff;
2351 TranslateMessage(&m);
2355 case WM_LBUTTONDOWN:
2356 case WM_RBUTTONDOWN:
2357 case WM_MBUTTONDOWN:
2362 * Shift-clicks count as middle-clicks, since otherwise
2363 * two-button Windows users won't have any kind of
2364 * middle click to use.
2366 if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT))
2367 button = MIDDLE_BUTTON;
2368 else if (message == WM_RBUTTONDOWN || is_alt_pressed())
2369 button = RIGHT_BUTTON;
2371 button = LEFT_BUTTON;
2373 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
2374 (signed short)HIWORD(lParam), button))
2387 * Shift-clicks count as middle-clicks, since otherwise
2388 * two-button Windows users won't have any kind of
2389 * middle click to use.
2391 if (message == WM_MBUTTONUP || (wParam & MK_SHIFT))
2392 button = MIDDLE_RELEASE;
2393 else if (message == WM_RBUTTONUP || is_alt_pressed())
2394 button = RIGHT_RELEASE;
2396 button = LEFT_RELEASE;
2398 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
2399 (signed short)HIWORD(lParam), button))
2409 if (wParam & (MK_MBUTTON | MK_SHIFT))
2410 button = MIDDLE_DRAG;
2411 else if (wParam & MK_RBUTTON || is_alt_pressed())
2412 button = RIGHT_DRAG;
2416 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
2417 (signed short)HIWORD(lParam), button))
2422 if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam))
2427 DWORD now = GetTickCount();
2428 float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F;
2429 midend_timer(fe->me, elapsed);
2430 fe->timer_last_tickcount = now;
2435 return DefWindowProc(hwnd, message, wParam, lParam);
2438 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
2443 InitCommonControls();
2449 wndclass.lpfnWndProc = WndProc;
2450 wndclass.cbClsExtra = 0;
2451 wndclass.cbWndExtra = 0;
2452 wndclass.hInstance = inst;
2453 wndclass.hIcon = LoadIcon(inst, MAKEINTRESOURCE(200));
2454 if (!wndclass.hIcon) /* in case resource file is absent */
2455 wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION);
2456 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
2457 wndclass.hbrBackground = NULL;
2458 wndclass.lpszMenuName = NULL;
2459 wndclass.lpszClassName = thegame.name;
2461 RegisterClass(&wndclass);
2464 while (*cmdline && isspace((unsigned char)*cmdline))
2469 if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) {
2471 sprintf(buf, "%.100s Error", thegame.name);
2472 MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
2476 while (GetMessage(&msg, NULL, 0, 0)) {
2477 DispatchMessage(&msg);