2 * windows.c: Windows front end for my puzzle collection.
19 #define IDM_NEW 0x0010
20 #define IDM_RESTART 0x0020
21 #define IDM_UNDO 0x0030
22 #define IDM_REDO 0x0040
23 #define IDM_COPY 0x0050
24 #define IDM_SOLVE 0x0060
25 #define IDM_QUIT 0x0070
26 #define IDM_CONFIG 0x0080
27 #define IDM_DESC 0x0090
28 #define IDM_SEED 0x00A0
29 #define IDM_HELPC 0x00B0
30 #define IDM_GAMEHELP 0x00C0
31 #define IDM_ABOUT 0x00D0
32 #define IDM_SAVE 0x00E0
33 #define IDM_LOAD 0x00F0
34 #define IDM_PRINT 0x0100
35 #define IDM_PRESETS 0x0110
37 #define HELP_FILE_NAME "puzzles.hlp"
38 #define HELP_CNT_NAME "puzzles.cnt"
39 #define CHM_FILE_NAME "puzzles.chm"
41 typedef HWND (CALLBACK *htmlhelp_t)(HWND, LPCSTR, UINT, DWORD);
42 static DWORD html_help_cookie;
43 static htmlhelp_t htmlhelp;
44 static HINSTANCE hh_dll;
45 enum { NONE, HLP, CHM } help_type;
47 const char *help_topic;
48 int help_has_contents;
51 static FILE *debug_fp = NULL;
52 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
53 static int debug_got_console = 0;
59 if (!debug_got_console) {
61 debug_got_console = 1;
62 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
66 debug_fp = fopen("debug.log", "w");
69 if (debug_hdl != INVALID_HANDLE_VALUE) {
70 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
76 void debug_printf(char *fmt, ...)
82 vsprintf(buf, fmt, ap);
88 #define WINFLAGS (WS_OVERLAPPEDWINDOW &~ \
89 (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED))
91 static void new_game_size(frontend *fe);
109 enum { CFG_PRINT = CFG_FRONTEND_SPECIFIC };
113 HWND hwnd, statusbar, cfgbox;
115 HBITMAP bitmap, prevbm;
122 DWORD timer_last_tickcount;
124 game_params **presets;
126 int nfonts, fontsize;
128 struct cfg_aux *cfgaux;
129 int cfg_which, dlg_done;
134 enum { DRAWING, PRINTING, NOTHING } drawstatus;
136 int printcount, printw, printh, printsolns, printcurr, printcolour;
138 int printoffsetx, printoffsety;
139 float printpixelscale;
145 void fatal(char *fmt, ...)
151 vsprintf(buf, fmt, ap);
154 MessageBox(NULL, buf, "Fatal error", MB_ICONEXCLAMATION | MB_OK);
159 char *geterrstr(void)
162 DWORD dw = GetLastError();
166 FORMAT_MESSAGE_ALLOCATE_BUFFER |
167 FORMAT_MESSAGE_FROM_SYSTEM,
170 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
174 ret = dupstr(lpMsgBuf);
181 void get_random_seed(void **randseed, int *randseedsize)
183 time_t *tp = snew(time_t);
185 *randseed = (void *)tp;
186 *randseedsize = sizeof(time_t);
189 static void win_status_bar(void *handle, char *text)
191 frontend *fe = (frontend *)handle;
193 SetWindowText(fe->statusbar, text);
196 static blitter *win_blitter_new(void *handle, int w, int h)
198 blitter *bl = snew(blitter);
200 memset(bl, 0, sizeof(blitter));
208 static void win_blitter_free(void *handle, blitter *bl)
210 if (bl->bitmap) DeleteObject(bl->bitmap);
214 static void blitter_mkbitmap(frontend *fe, blitter *bl)
216 HDC hdc = GetDC(fe->hwnd);
217 bl->bitmap = CreateCompatibleBitmap(hdc, bl->w, bl->h);
218 ReleaseDC(fe->hwnd, hdc);
221 /* BitBlt(dstDC, dstX, dstY, dstW, dstH, srcDC, srcX, srcY, dType) */
223 static void win_blitter_save(void *handle, blitter *bl, int x, int y)
225 frontend *fe = (frontend *)handle;
226 HDC hdc_win, hdc_blit;
229 assert(fe->drawstatus == DRAWING);
231 if (!bl->bitmap) blitter_mkbitmap(fe, bl);
233 bl->x = x; bl->y = y;
235 hdc_win = GetDC(fe->hwnd);
236 hdc_blit = CreateCompatibleDC(hdc_win);
237 if (!hdc_blit) fatal("hdc_blit failed: 0x%x", GetLastError());
239 prev_blit = SelectObject(hdc_blit, bl->bitmap);
240 if (prev_blit == NULL || prev_blit == HGDI_ERROR)
241 fatal("SelectObject for hdc_main failed: 0x%x", GetLastError());
243 if (!BitBlt(hdc_blit, 0, 0, bl->w, bl->h,
244 fe->hdc, x, y, SRCCOPY))
245 fatal("BitBlt failed: 0x%x", GetLastError());
247 SelectObject(hdc_blit, prev_blit);
249 ReleaseDC(fe->hwnd, hdc_win);
252 static void win_blitter_load(void *handle, blitter *bl, int x, int y)
254 frontend *fe = (frontend *)handle;
255 HDC hdc_win, hdc_blit;
258 assert(fe->drawstatus == DRAWING);
260 assert(bl->bitmap); /* we should always have saved before loading */
262 if (x == BLITTER_FROMSAVED) x = bl->x;
263 if (y == BLITTER_FROMSAVED) y = bl->y;
265 hdc_win = GetDC(fe->hwnd);
266 hdc_blit = CreateCompatibleDC(hdc_win);
268 prev_blit = SelectObject(hdc_blit, bl->bitmap);
270 BitBlt(fe->hdc, x, y, bl->w, bl->h,
271 hdc_blit, 0, 0, SRCCOPY);
273 SelectObject(hdc_blit, prev_blit);
275 ReleaseDC(fe->hwnd, hdc_win);
278 void frontend_default_colour(frontend *fe, float *output)
280 DWORD c = GetSysColor(COLOR_MENU); /* ick */
282 output[0] = (float)(GetRValue(c) / 255.0);
283 output[1] = (float)(GetGValue(c) / 255.0);
284 output[2] = (float)(GetBValue(c) / 255.0);
287 static POINT win_transform_point(frontend *fe, int x, int y)
291 assert(fe->drawstatus != NOTHING);
293 if (fe->drawstatus == PRINTING) {
294 ret.x = (int)(fe->printoffsetx + fe->printpixelscale * x);
295 ret.y = (int)(fe->printoffsety + fe->printpixelscale * y);
304 static void win_text_colour(frontend *fe, int colour)
306 assert(fe->drawstatus != NOTHING);
308 if (fe->drawstatus == PRINTING) {
311 print_get_colour(fe->dr, colour, &hatch, &r, &g, &b);
313 SetTextColor(fe->hdc, RGB(r * 255, g * 255, b * 255));
315 SetTextColor(fe->hdc,
316 hatch == HATCH_CLEAR ? RGB(255,255,255) : RGB(0,0,0));
318 SetTextColor(fe->hdc, fe->colours[colour]);
322 static void win_set_brush(frontend *fe, int colour)
325 assert(fe->drawstatus != NOTHING);
327 if (fe->drawstatus == PRINTING) {
330 print_get_colour(fe->dr, colour, &hatch, &r, &g, &b);
333 br = CreateSolidBrush(RGB(r * 255, g * 255, b * 255));
334 else if (hatch == HATCH_SOLID)
335 br = CreateSolidBrush(RGB(0,0,0));
336 else if (hatch == HATCH_CLEAR)
337 br = CreateSolidBrush(RGB(255,255,255));
339 br = CreateHatchBrush(hatch == HATCH_BACKSLASH ? HS_FDIAGONAL :
340 hatch == HATCH_SLASH ? HS_BDIAGONAL :
341 hatch == HATCH_HORIZ ? HS_HORIZONTAL :
342 hatch == HATCH_VERT ? HS_VERTICAL :
343 hatch == HATCH_PLUS ? HS_CROSS :
344 /* hatch == HATCH_X ? */ HS_DIAGCROSS,
347 br = fe->brushes[colour];
349 fe->oldbr = SelectObject(fe->hdc, br);
352 static void win_reset_brush(frontend *fe)
356 assert(fe->drawstatus != NOTHING);
358 br = SelectObject(fe->hdc, fe->oldbr);
359 if (fe->drawstatus == PRINTING)
363 static void win_set_pen(frontend *fe, int colour, int thin)
366 assert(fe->drawstatus != NOTHING);
368 if (fe->drawstatus == PRINTING) {
371 int width = thin ? 0 : fe->linewidth;
373 print_get_colour(fe->dr, colour, &hatch, &r, &g, &b);
375 pen = CreatePen(PS_SOLID, width,
376 RGB(r * 255, g * 255, b * 255));
377 else if (hatch == HATCH_SOLID)
378 pen = CreatePen(PS_SOLID, width, RGB(0, 0, 0));
379 else if (hatch == HATCH_CLEAR)
380 pen = CreatePen(PS_SOLID, width, RGB(255,255,255));
382 assert(!"This shouldn't happen");
383 pen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
386 pen = fe->pens[colour];
388 fe->oldpen = SelectObject(fe->hdc, pen);
391 static void win_reset_pen(frontend *fe)
395 assert(fe->drawstatus != NOTHING);
397 pen = SelectObject(fe->hdc, fe->oldpen);
398 if (fe->drawstatus == PRINTING)
402 static void win_clip(void *handle, int x, int y, int w, int h)
404 frontend *fe = (frontend *)handle;
407 if (fe->drawstatus == NOTHING)
410 p = win_transform_point(fe, x, y);
411 q = win_transform_point(fe, x+w, y+h);
412 IntersectClipRect(fe->hdc, p.x, p.y, q.x, q.y);
415 static void win_unclip(void *handle)
417 frontend *fe = (frontend *)handle;
419 if (fe->drawstatus == NOTHING)
422 SelectClipRgn(fe->hdc, NULL);
425 static void win_draw_text(void *handle, int x, int y, int fonttype,
426 int fontsize, int align, int colour, char *text)
428 frontend *fe = (frontend *)handle;
432 if (fe->drawstatus == NOTHING)
435 if (fe->drawstatus == PRINTING)
436 fontsize = (int)(fontsize * fe->printpixelscale);
438 xy = win_transform_point(fe, x, y);
441 * Find or create the font.
443 for (i = fe->fontstart; i < fe->nfonts; i++)
444 if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
447 if (i == fe->nfonts) {
448 if (fe->fontsize <= fe->nfonts) {
449 fe->fontsize = fe->nfonts + 10;
450 fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
455 fe->fonts[i].type = fonttype;
456 fe->fonts[i].size = fontsize;
458 fe->fonts[i].font = CreateFont(-fontsize, 0, 0, 0,
459 fe->drawstatus == PRINTING ? 0 : FW_BOLD,
460 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
461 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
463 (fonttype == FONT_FIXED ?
464 FIXED_PITCH | FF_DONTCARE :
465 VARIABLE_PITCH | FF_SWISS),
470 * Position and draw the text.
477 oldfont = SelectObject(fe->hdc, fe->fonts[i].font);
478 if (GetTextMetrics(fe->hdc, &tm)) {
479 if (align & ALIGN_VCENTRE)
480 xy.y -= (tm.tmAscent+tm.tmDescent)/2;
484 if (GetTextExtentPoint32(fe->hdc, text, strlen(text), &size)) {
485 if (align & ALIGN_HCENTRE)
487 else if (align & ALIGN_HRIGHT)
490 SetBkMode(fe->hdc, TRANSPARENT);
491 win_text_colour(fe, colour);
492 TextOut(fe->hdc, xy.x, xy.y, text, strlen(text));
493 SelectObject(fe->hdc, oldfont);
497 static void win_draw_rect(void *handle, int x, int y, int w, int h, int colour)
499 frontend *fe = (frontend *)handle;
502 if (fe->drawstatus == NOTHING)
505 if (fe->drawstatus == DRAWING && w == 1 && h == 1) {
507 * Rectangle() appears to get uppity if asked to draw a 1x1
508 * rectangle, presumably on the grounds that that's beneath
509 * its dignity and you ought to be using SetPixel instead.
512 SetPixel(fe->hdc, x, y, fe->colours[colour]);
514 win_set_brush(fe, colour);
515 win_set_pen(fe, colour, TRUE);
516 p = win_transform_point(fe, x, y);
517 q = win_transform_point(fe, x+w, y+h);
518 Rectangle(fe->hdc, p.x, p.y, q.x, q.y);
524 static void win_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour)
526 frontend *fe = (frontend *)handle;
529 if (fe->drawstatus == NOTHING)
532 win_set_pen(fe, colour, FALSE);
533 p = win_transform_point(fe, x1, y1);
534 q = win_transform_point(fe, x2, y2);
535 MoveToEx(fe->hdc, p.x, p.y, NULL);
536 LineTo(fe->hdc, q.x, q.y);
537 if (fe->drawstatus == DRAWING)
538 SetPixel(fe->hdc, q.x, q.y, fe->colours[colour]);
542 static void win_draw_circle(void *handle, int cx, int cy, int radius,
543 int fillcolour, int outlinecolour)
545 frontend *fe = (frontend *)handle;
548 assert(outlinecolour >= 0);
550 if (fe->drawstatus == NOTHING)
553 if (fillcolour >= 0) {
554 win_set_brush(fe, fillcolour);
555 win_set_pen(fe, outlinecolour, FALSE);
556 p = win_transform_point(fe, cx - radius, cy - radius);
557 q = win_transform_point(fe, cx + radius, cy + radius);
558 Ellipse(fe->hdc, p.x, p.y, q.x+1, q.y+1);
562 win_set_pen(fe, outlinecolour, FALSE);
563 p = win_transform_point(fe, cx - radius, cy - radius);
564 q = win_transform_point(fe, cx + radius, cy + radius);
565 r = win_transform_point(fe, cx - radius, cy);
566 Arc(fe->hdc, p.x, p.y, q.x+1, q.y+1, r.x, r.y, r.x, r.y);
571 static void win_draw_polygon(void *handle, int *coords, int npoints,
572 int fillcolour, int outlinecolour)
574 frontend *fe = (frontend *)handle;
578 if (fe->drawstatus == NOTHING)
581 pts = snewn(npoints+1, POINT);
583 for (i = 0; i <= npoints; i++) {
584 int j = (i < npoints ? i : 0);
585 pts[i] = win_transform_point(fe, coords[j*2], coords[j*2+1]);
588 assert(outlinecolour >= 0);
590 if (fillcolour >= 0) {
591 win_set_brush(fe, fillcolour);
592 win_set_pen(fe, outlinecolour, FALSE);
593 Polygon(fe->hdc, pts, npoints);
597 win_set_pen(fe, outlinecolour, FALSE);
598 Polyline(fe->hdc, pts, npoints+1);
605 static void win_start_draw(void *handle)
607 frontend *fe = (frontend *)handle;
610 assert(fe->drawstatus == NOTHING);
612 hdc_win = GetDC(fe->hwnd);
613 fe->hdc = CreateCompatibleDC(hdc_win);
614 fe->prevbm = SelectObject(fe->hdc, fe->bitmap);
615 ReleaseDC(fe->hwnd, hdc_win);
617 SetMapMode(fe->hdc, MM_TEXT);
618 fe->drawstatus = DRAWING;
621 static void win_draw_update(void *handle, int x, int y, int w, int h)
623 frontend *fe = (frontend *)handle;
626 if (fe->drawstatus != DRAWING)
634 InvalidateRect(fe->hwnd, &r, FALSE);
637 static void win_end_draw(void *handle)
639 frontend *fe = (frontend *)handle;
640 assert(fe->drawstatus == DRAWING);
641 SelectObject(fe->hdc, fe->prevbm);
644 DeleteObject(fe->clip);
647 fe->drawstatus = NOTHING;
650 static void win_line_width(void *handle, float width)
652 frontend *fe = (frontend *)handle;
654 assert(fe->drawstatus != DRAWING);
655 if (fe->drawstatus == NOTHING)
658 fe->linewidth = (int)(width * fe->printpixelscale);
661 static void win_begin_doc(void *handle, int pages)
663 frontend *fe = (frontend *)handle;
665 assert(fe->drawstatus != DRAWING);
666 if (fe->drawstatus == NOTHING)
669 if (StartDoc(fe->hdc, &fe->di) <= 0) {
670 char *e = geterrstr();
671 MessageBox(fe->hwnd, e, "Error starting to print",
672 MB_ICONERROR | MB_OK);
674 fe->drawstatus = NOTHING;
678 * Push a marker on the font stack so that we won't use the
679 * same fonts for printing and drawing. (This is because
680 * drawing seems to look generally better in bold, but printing
681 * is better not in bold.)
683 fe->fontstart = fe->nfonts;
686 static void win_begin_page(void *handle, int number)
688 frontend *fe = (frontend *)handle;
690 assert(fe->drawstatus != DRAWING);
691 if (fe->drawstatus == NOTHING)
694 if (StartPage(fe->hdc) <= 0) {
695 char *e = geterrstr();
696 MessageBox(fe->hwnd, e, "Error starting a page",
697 MB_ICONERROR | MB_OK);
699 fe->drawstatus = NOTHING;
703 static void win_begin_puzzle(void *handle, float xm, float xc,
704 float ym, float yc, int pw, int ph, float wmm)
706 frontend *fe = (frontend *)handle;
707 int ppw, pph, pox, poy;
708 float mmpw, mmph, mmox, mmoy;
711 assert(fe->drawstatus != DRAWING);
712 if (fe->drawstatus == NOTHING)
715 ppw = GetDeviceCaps(fe->hdc, HORZRES);
716 pph = GetDeviceCaps(fe->hdc, VERTRES);
717 mmpw = (float)GetDeviceCaps(fe->hdc, HORZSIZE);
718 mmph = (float)GetDeviceCaps(fe->hdc, VERTSIZE);
721 * Compute the puzzle's position on the logical page.
723 mmox = xm * mmpw + xc;
724 mmoy = ym * mmph + yc;
727 * Work out what that comes to in pixels.
729 pox = (int)(mmox * (float)ppw / mmpw);
730 poy = (int)(mmoy * (float)ppw / mmpw);
733 * And determine the scale.
735 * I need a scale such that the maximum puzzle-coordinate
736 * extent of the rectangle (pw * scale) is equal to the pixel
737 * equivalent of the puzzle's millimetre width (wmm * ppw /
740 scale = (wmm * ppw) / (mmpw * pw);
743 * Now store pox, poy and scale for use in the main drawing
746 fe->printoffsetx = pox;
747 fe->printoffsety = poy;
748 fe->printpixelscale = scale;
753 static void win_end_puzzle(void *handle)
755 /* Nothing needs to be done here. */
758 static void win_end_page(void *handle, int number)
760 frontend *fe = (frontend *)handle;
762 assert(fe->drawstatus != DRAWING);
764 if (fe->drawstatus == NOTHING)
767 if (EndPage(fe->hdc) <= 0) {
768 char *e = geterrstr();
769 MessageBox(fe->hwnd, e, "Error finishing a page",
770 MB_ICONERROR | MB_OK);
772 fe->drawstatus = NOTHING;
776 static void win_end_doc(void *handle)
778 frontend *fe = (frontend *)handle;
780 assert(fe->drawstatus != DRAWING);
783 * Free all the fonts created since we began printing.
785 while (fe->nfonts > fe->fontstart) {
787 DeleteObject(fe->fonts[fe->nfonts].font);
792 * The MSDN web site sample code doesn't bother to call EndDoc
793 * if an error occurs half way through printing. I expect doing
794 * so would cause the erroneous document to actually be
795 * printed, or something equally undesirable.
797 if (fe->drawstatus == NOTHING)
800 if (EndDoc(fe->hdc) <= 0) {
801 char *e = geterrstr();
802 MessageBox(fe->hwnd, e, "Error finishing printing",
803 MB_ICONERROR | MB_OK);
805 fe->drawstatus = NOTHING;
809 const struct drawing_api win_drawing = {
834 void print(frontend *fe)
839 midend *nme = NULL; /* non-interactive midend for bulk puzzle generation */
844 * Create our document structure and fill it up with puzzles.
846 doc = document_new(fe->printw, fe->printh, fe->printscale / 100.0F);
847 for (i = 0; i < fe->printcount; i++) {
848 if (i == 0 && fe->printcurr) {
849 err = midend_print_puzzle(fe->me, doc, fe->printsolns);
854 nme = midend_new(NULL, &thegame, NULL, NULL);
857 * Set the non-interactive mid-end to have the same
858 * parameters as the standard one.
860 params = midend_get_params(fe->me);
861 midend_set_params(nme, params);
862 thegame.free_params(params);
865 midend_new_game(nme);
866 err = midend_print_puzzle(nme, doc, fe->printsolns);
875 MessageBox(fe->hwnd, err, "Error preparing puzzles for printing",
876 MB_ICONERROR | MB_OK);
881 memset(&pd, 0, sizeof(pd));
882 pd.lStructSize = sizeof(pd);
883 pd.hwndOwner = fe->hwnd;
886 pd.Flags = PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC |
887 PD_NOPAGENUMS | PD_NOSELECTION;
889 pd.nFromPage = pd.nToPage = 0xFFFF;
890 pd.nMinPage = pd.nMaxPage = 1;
892 if (!PrintDlg(&pd)) {
898 * Now pd.hDC is a device context for the printer.
902 * FIXME: IWBNI we put up an Abort box here.
905 memset(&fe->di, 0, sizeof(fe->di));
906 fe->di.cbSize = sizeof(fe->di);
907 sprintf(doctitle, "Printed puzzles from %s (from Simon Tatham's"
908 " Portable Puzzle Collection)", thegame.name);
909 fe->di.lpszDocName = doctitle;
910 fe->di.lpszOutput = NULL;
911 fe->di.lpszDatatype = NULL;
914 fe->drawstatus = PRINTING;
917 fe->dr = drawing_new(&win_drawing, NULL, fe);
918 document_print(doc, fe->dr);
919 drawing_free(fe->dr);
922 fe->drawstatus = NOTHING;
928 void deactivate_timer(frontend *fe)
931 return; /* for non-interactive midend */
932 if (fe->hwnd) KillTimer(fe->hwnd, fe->timer);
936 void activate_timer(frontend *fe)
939 return; /* for non-interactive midend */
941 fe->timer = SetTimer(fe->hwnd, fe->timer, 20, NULL);
942 fe->timer_last_tickcount = GetTickCount();
946 void write_clip(HWND hwnd, char *data)
954 * Windows expects CRLF in the clipboard, so we must convert
955 * any \n that has come out of the puzzle backend.
958 for (i = 0; data[i]; i++) {
963 data2 = snewn(len+1, char);
965 for (i = 0; data[i]; i++) {
968 data2[j++] = data[i];
973 clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
976 lock = GlobalLock(clipdata);
979 memcpy(lock, data2, len);
980 ((unsigned char *) lock)[len] = 0;
981 GlobalUnlock(clipdata);
983 if (OpenClipboard(hwnd)) {
985 SetClipboardData(CF_TEXT, clipdata);
988 GlobalFree(clipdata);
994 * Set up Help and see if we can find a help file.
996 static void init_help(void)
998 char b[2048], *p, *q, *r;
1002 * Find the executable file path, so we can look alongside
1003 * it for help files. Trim the filename off the end.
1005 GetModuleFileName(NULL, b, sizeof(b) - 1);
1007 p = strrchr(b, '\\');
1008 if (p && p >= r) r = p+1;
1009 q = strrchr(b, ':');
1010 if (q && q >= r) r = q+1;
1013 * Try HTML Help first.
1015 strcpy(r, CHM_FILE_NAME);
1016 if ( (fp = fopen(b, "r")) != NULL) {
1020 * We have a .CHM. See if we can use it.
1022 hh_dll = LoadLibrary("hhctrl.ocx");
1024 htmlhelp = (htmlhelp_t)GetProcAddress(hh_dll, "HtmlHelpA");
1026 FreeLibrary(hh_dll);
1029 htmlhelp(NULL, NULL, HH_INITIALIZE, (DWORD)&html_help_cookie);
1030 help_path = dupstr(b);
1032 help_topic = thegame.htmlhelp_topic;
1038 * Now try old-style .HLP.
1040 strcpy(r, HELP_FILE_NAME);
1041 if ( (fp = fopen(b, "r")) != NULL) {
1044 help_path = dupstr(b);
1047 help_topic = thegame.winhelp_topic;
1050 * See if there's a .CNT file alongside it.
1052 strcpy(r, HELP_CNT_NAME);
1053 if ( (fp = fopen(b, "r")) != NULL) {
1055 help_has_contents = TRUE;
1057 help_has_contents = FALSE;
1062 help_type = NONE; /* didn't find any */
1068 static void start_help(frontend *fe, const char *topic)
1073 switch (help_type) {
1077 str = snewn(10+strlen(topic), char);
1078 sprintf(str, "JI(`',`%s')", topic);
1080 } else if (help_has_contents) {
1083 cmd = HELP_CONTENTS;
1085 WinHelp(fe->hwnd, help_path, cmd, (DWORD)str);
1086 fe->help_running = TRUE;
1092 str = snewn(20 + strlen(topic) + strlen(help_path), char);
1093 sprintf(str, "%s::/%s.html>main", help_path, topic);
1095 str = dupstr(help_path);
1097 htmlhelp(fe->hwnd, str, HH_DISPLAY_TOPIC, 0);
1098 fe->help_running = TRUE;
1101 assert(!"This shouldn't happen");
1109 * Stop Help on window cleanup.
1111 static void stop_help(frontend *fe)
1113 if (fe->help_running) {
1114 switch (help_type) {
1116 WinHelp(fe->hwnd, help_path, HELP_QUIT, 0);
1120 htmlhelp(NULL, NULL, HH_CLOSE_ALL, 0);
1123 assert(!"This shouldn't happen");
1126 fe->help_running = FALSE;
1131 * Terminate Help on process exit.
1133 static void cleanup_help(void)
1135 if (help_type == CHM) {
1137 htmlhelp(NULL, NULL, HH_UNINITIALIZE, html_help_cookie);
1141 static void check_window_size(frontend *fe, int *px, int *py)
1146 if (fe->statusbar) {
1148 GetWindowRect(fe->statusbar, &sr);
1149 sy = sr.bottom - sr.top;
1155 * See if we actually got the window size we wanted, and adjust
1156 * the puzzle size if not.
1158 GetClientRect(fe->hwnd, &r);
1159 x = r.right - r.left;
1160 y = r.bottom - r.top - sy;
1161 midend_size(fe->me, &x, &y, FALSE);
1162 if (x != r.right - r.left || y != r.bottom - r.top) {
1164 * Resize the window, now we know what size we _really_
1170 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1171 SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left, r.bottom - r.top,
1172 SWP_NOMOVE | SWP_NOZORDER);
1175 if (fe->statusbar) {
1176 GetClientRect(fe->hwnd, &r);
1177 SetWindowPos(fe->statusbar, NULL, 0, r.bottom-r.top-sy, r.right-r.left,
1185 static void get_max_puzzle_size(frontend *fe, int *x, int *y)
1189 if (SystemParametersInfo(SPI_GETWORKAREA, 0, &sr, FALSE)) {
1190 *x = sr.right - sr.left;
1191 *y = sr.bottom - sr.top;
1196 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1197 *x -= r.right - r.left - 100;
1198 *y -= r.bottom - r.top - 100;
1203 if (fe->statusbar != NULL) {
1204 GetWindowRect(fe->statusbar, &sr);
1205 *y -= sr.bottom - sr.top;
1209 static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
1215 fe = snew(frontend);
1217 fe->me = midend_new(fe, &thegame, &win_drawing, fe);
1220 *error = midend_game_id(fe->me, game_id);
1222 midend_free(fe->me);
1233 fe->help_running = FALSE;
1235 fe->drawstatus = NOTHING;
1239 midend_new_game(fe->me);
1242 fe->nfonts = fe->fontsize = 0;
1248 colours = midend_colours(fe->me, &ncolours);
1250 fe->colours = snewn(ncolours, COLORREF);
1251 fe->brushes = snewn(ncolours, HBRUSH);
1252 fe->pens = snewn(ncolours, HPEN);
1254 for (i = 0; i < ncolours; i++) {
1255 fe->colours[i] = RGB(255 * colours[i*3+0],
1256 255 * colours[i*3+1],
1257 255 * colours[i*3+2]);
1258 fe->brushes[i] = CreateSolidBrush(fe->colours[i]);
1259 fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]);
1264 if (midend_wants_statusbar(fe->me)) {
1265 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh",
1266 WS_CHILD | WS_VISIBLE,
1267 0, 0, 0, 0, /* status bar does these */
1268 NULL, NULL, inst, NULL);
1270 fe->statusbar = NULL;
1272 get_max_puzzle_size(fe, &x, &y);
1273 midend_size(fe->me, &x, &y, FALSE);
1278 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1280 fe->hwnd = CreateWindowEx(0, thegame.name, thegame.name,
1281 WS_OVERLAPPEDWINDOW &~
1282 (WS_THICKFRAME | WS_MAXIMIZEBOX),
1283 CW_USEDEFAULT, CW_USEDEFAULT,
1284 r.right - r.left, r.bottom - r.top,
1285 NULL, NULL, inst, NULL);
1287 if (midend_wants_statusbar(fe->me)) {
1289 DestroyWindow(fe->statusbar);
1290 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh",
1291 WS_CHILD | WS_VISIBLE,
1292 0, 0, 0, 0, /* status bar does these */
1293 fe->hwnd, NULL, inst, NULL);
1295 * Now resize the window to take account of the status bar.
1297 GetWindowRect(fe->statusbar, &sr);
1298 GetWindowRect(fe->hwnd, &r);
1299 SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left,
1300 r.bottom - r.top + sr.bottom - sr.top,
1301 SWP_NOMOVE | SWP_NOZORDER);
1303 fe->statusbar = NULL;
1307 HMENU bar = CreateMenu();
1308 HMENU menu = CreateMenu();
1310 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Game");
1311 AppendMenu(menu, MF_ENABLED, IDM_NEW, "New");
1312 AppendMenu(menu, MF_ENABLED, IDM_RESTART, "Restart");
1313 AppendMenu(menu, MF_ENABLED, IDM_DESC, "Specific...");
1314 AppendMenu(menu, MF_ENABLED, IDM_SEED, "Random Seed...");
1316 if ((fe->npresets = midend_num_presets(fe->me)) > 0 ||
1317 thegame.can_configure) {
1318 HMENU sub = CreateMenu();
1321 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "Type");
1323 fe->presets = snewn(fe->npresets, game_params *);
1325 for (i = 0; i < fe->npresets; i++) {
1328 midend_fetch_preset(fe->me, i, &name, &fe->presets[i]);
1331 * FIXME: we ought to go through and do something
1332 * with ampersands here.
1335 AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name);
1338 if (thegame.can_configure) {
1339 AppendMenu(sub, MF_ENABLED, IDM_CONFIG, "Custom...");
1343 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1344 AppendMenu(menu, MF_ENABLED, IDM_LOAD, "Load");
1345 AppendMenu(menu, MF_ENABLED, IDM_SAVE, "Save");
1346 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1347 if (thegame.can_print) {
1348 AppendMenu(menu, MF_ENABLED, IDM_PRINT, "Print");
1349 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1351 AppendMenu(menu, MF_ENABLED, IDM_UNDO, "Undo");
1352 AppendMenu(menu, MF_ENABLED, IDM_REDO, "Redo");
1353 if (thegame.can_format_as_text) {
1354 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1355 AppendMenu(menu, MF_ENABLED, IDM_COPY, "Copy");
1357 if (thegame.can_solve) {
1358 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1359 AppendMenu(menu, MF_ENABLED, IDM_SOLVE, "Solve");
1361 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1362 AppendMenu(menu, MF_ENABLED, IDM_QUIT, "Exit");
1363 menu = CreateMenu();
1364 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Help");
1365 AppendMenu(menu, MF_ENABLED, IDM_ABOUT, "About");
1366 if (help_type != NONE) {
1367 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1368 AppendMenu(menu, MF_ENABLED, IDM_HELPC, "Contents");
1371 assert(thegame.name);
1372 item = snewn(9+strlen(thegame.name), char); /*ick*/
1373 sprintf(item, "Help on %s", thegame.name);
1374 AppendMenu(menu, MF_ENABLED, IDM_GAMEHELP, item);
1378 SetMenu(fe->hwnd, bar);
1382 new_game_size(fe); /* initialises fe->bitmap */
1383 check_window_size(fe, &x, &y);
1385 SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe);
1387 ShowWindow(fe->hwnd, SW_NORMAL);
1388 SetForegroundWindow(fe->hwnd);
1390 midend_redraw(fe->me);
1395 static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg,
1396 WPARAM wParam, LPARAM lParam)
1398 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1405 if ((HIWORD(wParam) == BN_CLICKED ||
1406 HIWORD(wParam) == BN_DOUBLECLICKED) &&
1407 LOWORD(wParam) == IDOK)
1420 * Wrappers on midend_{get,set}_config, which extend the CFG_*
1421 * enumeration to add CFG_PRINT.
1423 static config_item *frontend_get_config(frontend *fe, int which,
1426 if (which < CFG_FRONTEND_SPECIFIC) {
1427 return midend_get_config(fe->me, which, wintitle);
1428 } else if (which == CFG_PRINT) {
1432 *wintitle = snewn(40 + strlen(thegame.name), char);
1433 sprintf(*wintitle, "%s print setup", thegame.name);
1435 ret = snewn(8, config_item);
1439 ret[i].name = "Number of puzzles to print";
1440 ret[i].type = C_STRING;
1441 ret[i].sval = dupstr("1");
1445 ret[i].name = "Number of puzzles across the page";
1446 ret[i].type = C_STRING;
1447 ret[i].sval = dupstr("1");
1451 ret[i].name = "Number of puzzles down the page";
1452 ret[i].type = C_STRING;
1453 ret[i].sval = dupstr("1");
1457 ret[i].name = "Percentage of standard size";
1458 ret[i].type = C_STRING;
1459 ret[i].sval = dupstr("100.0");
1463 ret[i].name = "Include currently shown puzzle";
1464 ret[i].type = C_BOOLEAN;
1469 ret[i].name = "Print solutions";
1470 ret[i].type = C_BOOLEAN;
1472 ret[i].ival = FALSE;
1475 if (thegame.can_print_in_colour) {
1476 ret[i].name = "Print in colour";
1477 ret[i].type = C_BOOLEAN;
1479 ret[i].ival = FALSE;
1484 ret[i].type = C_END;
1491 assert(!"We should never get here");
1496 static char *frontend_set_config(frontend *fe, int which, config_item *cfg)
1498 if (which < CFG_FRONTEND_SPECIFIC) {
1499 return midend_set_config(fe->me, which, cfg);
1500 } else if (which == CFG_PRINT) {
1501 if ((fe->printcount = atoi(cfg[0].sval)) <= 0)
1502 return "Number of puzzles to print should be at least one";
1503 if ((fe->printw = atoi(cfg[1].sval)) <= 0)
1504 return "Number of puzzles across the page should be at least one";
1505 if ((fe->printh = atoi(cfg[2].sval)) <= 0)
1506 return "Number of puzzles down the page should be at least one";
1507 if ((fe->printscale = (float)atof(cfg[3].sval)) <= 0)
1508 return "Print size should be positive";
1509 fe->printcurr = cfg[4].ival;
1510 fe->printsolns = cfg[5].ival;
1511 fe->printcolour = thegame.can_print_in_colour && cfg[6].ival;
1514 assert(!"We should never get here");
1515 return "Internal error";
1519 static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg,
1520 WPARAM wParam, LPARAM lParam)
1522 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1532 * OK and Cancel are special cases.
1534 if ((HIWORD(wParam) == BN_CLICKED ||
1535 HIWORD(wParam) == BN_DOUBLECLICKED) &&
1536 (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) {
1537 if (LOWORD(wParam) == IDOK) {
1538 char *err = frontend_set_config(fe, fe->cfg_which, fe->cfg);
1541 MessageBox(hwnd, err, "Validation error",
1542 MB_ICONERROR | MB_OK);
1553 * First find the control whose id this is.
1555 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
1556 if (j->ctlid == LOWORD(wParam))
1559 if (i->type == C_END)
1560 return 0; /* not our problem */
1562 if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) {
1564 GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer));
1565 buffer[lenof(buffer)-1] = '\0';
1567 i->sval = dupstr(buffer);
1568 } else if (i->type == C_BOOLEAN &&
1569 (HIWORD(wParam) == BN_CLICKED ||
1570 HIWORD(wParam) == BN_DOUBLECLICKED)) {
1571 i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid);
1572 } else if (i->type == C_CHOICES &&
1573 HIWORD(wParam) == CBN_SELCHANGE) {
1574 i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid,
1575 CB_GETCURSEL, 0, 0);
1588 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
1589 char *wclass, int wstyle,
1590 int exstyle, const char *wtext, int wid)
1593 ret = CreateWindowEx(exstyle, wclass, wtext,
1594 wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1,
1595 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
1596 SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0));
1600 static void about(frontend *fe)
1610 int winwidth, winheight, y;
1611 int height, width, maxwid;
1612 const char *strings[16];
1617 sprintf(titlebuf, "About %.250s", thegame.name);
1619 strings[nstrings++] = thegame.name;
1620 strings[nstrings++] = "from Simon Tatham's Portable Puzzle Collection";
1621 strings[nstrings++] = ver;
1623 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
1624 wc.lpfnWndProc = DefDlgProc;
1626 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
1627 wc.hInstance = fe->inst;
1629 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
1630 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
1631 wc.lpszMenuName = NULL;
1632 wc.lpszClassName = "GameAboutBox";
1635 hdc = GetDC(fe->hwnd);
1636 SetMapMode(hdc, MM_TEXT);
1638 fe->dlg_done = FALSE;
1640 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
1642 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
1643 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
1648 oldfont = SelectObject(hdc, fe->cfgfont);
1649 if (GetTextMetrics(hdc, &tm)) {
1650 height = tm.tmAscent + tm.tmDescent;
1651 width = tm.tmAveCharWidth;
1653 height = width = 30;
1657 * Figure out the layout of the About box by measuring the
1658 * length of each piece of text.
1661 winheight = height/2;
1663 for (i = 0; i < nstrings; i++) {
1664 if (GetTextExtentPoint32(hdc, strings[i], strlen(strings[i]), &size))
1665 lengths[i] = size.cx;
1667 lengths[i] = 0; /* *shrug* */
1668 if (maxwid < lengths[i])
1669 maxwid = lengths[i];
1670 winheight += height * 3 / 2 + (height / 2);
1673 winheight += height + height * 7 / 4; /* OK button */
1674 winwidth = maxwid + 4*width;
1676 SelectObject(hdc, oldfont);
1677 ReleaseDC(fe->hwnd, hdc);
1680 * Create the dialog, now that we know its size.
1687 r.bottom = winheight;
1689 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
1690 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1691 WS_CAPTION | WS_SYSMENU*/) &~
1692 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
1696 * Centre the dialog on its parent window.
1700 GetWindowRect(fe->hwnd, &r2);
1701 r.left = (r2.left + r2.right - r.right) / 2;
1702 r.top = (r2.top + r2.bottom - r.bottom) / 2;
1706 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, titlebuf,
1707 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1708 WS_CAPTION | WS_SYSMENU,
1710 r.right-r.left, r.bottom-r.top,
1711 fe->hwnd, NULL, fe->inst, NULL);
1714 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
1716 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
1717 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)AboutDlgProc);
1721 for (i = 0; i < nstrings; i++) {
1722 int border = width*2 + (maxwid - lengths[i]) / 2;
1723 mkctrl(fe, border, border+lengths[i], y+height*1/8, y+height*9/8,
1724 "Static", 0, 0, strings[i], id++);
1727 assert(y < winheight);
1731 y += height/2; /* extra space before OK */
1732 mkctrl(fe, width*2, maxwid+width*2, y, y+height*7/4, "BUTTON",
1733 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
1736 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
1738 EnableWindow(fe->hwnd, FALSE);
1739 ShowWindow(fe->cfgbox, SW_NORMAL);
1740 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
1741 if (!IsDialogMessage(fe->cfgbox, &msg))
1742 DispatchMessage(&msg);
1746 EnableWindow(fe->hwnd, TRUE);
1747 SetForegroundWindow(fe->hwnd);
1748 DestroyWindow(fe->cfgbox);
1749 DeleteObject(fe->cfgfont);
1752 static int get_config(frontend *fe, int which)
1765 int winwidth, winheight, col1l, col1r, col2l, col2r, y;
1766 int height, width, maxlabel, maxcheckbox;
1768 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
1769 wc.lpfnWndProc = DefDlgProc;
1771 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
1772 wc.hInstance = fe->inst;
1774 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
1775 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
1776 wc.lpszMenuName = NULL;
1777 wc.lpszClassName = "GameConfigBox";
1780 hdc = GetDC(fe->hwnd);
1781 SetMapMode(hdc, MM_TEXT);
1783 fe->dlg_done = FALSE;
1785 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
1787 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
1788 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
1793 oldfont = SelectObject(hdc, fe->cfgfont);
1794 if (GetTextMetrics(hdc, &tm)) {
1795 height = tm.tmAscent + tm.tmDescent;
1796 width = tm.tmAveCharWidth;
1798 height = width = 30;
1801 fe->cfg = frontend_get_config(fe, which, &title);
1802 fe->cfg_which = which;
1805 * Figure out the layout of the config box by measuring the
1806 * length of each piece of text.
1808 maxlabel = maxcheckbox = 0;
1809 winheight = height/2;
1811 for (i = fe->cfg; i->type != C_END; i++) {
1816 * Both these control types have a label filling only
1817 * the left-hand column of the box.
1819 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
1822 winheight += height * 3 / 2 + (height / 2);
1827 * Checkboxes take up the whole of the box width.
1829 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
1830 maxcheckbox < size.cx)
1831 maxcheckbox = size.cx;
1832 winheight += height + (height / 2);
1837 winheight += height + height * 7 / 4; /* OK / Cancel buttons */
1840 col1r = col1l + maxlabel;
1841 col2l = col1r + 2*width;
1842 col2r = col2l + 30*width;
1843 if (col2r < col1l+2*height+maxcheckbox)
1844 col2r = col1l+2*height+maxcheckbox;
1845 winwidth = col2r + 2*width;
1847 SelectObject(hdc, oldfont);
1848 ReleaseDC(fe->hwnd, hdc);
1851 * Create the dialog, now that we know its size.
1858 r.bottom = winheight;
1860 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
1861 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1862 WS_CAPTION | WS_SYSMENU*/) &~
1863 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
1867 * Centre the dialog on its parent window.
1871 GetWindowRect(fe->hwnd, &r2);
1872 r.left = (r2.left + r2.right - r.right) / 2;
1873 r.top = (r2.top + r2.bottom - r.bottom) / 2;
1877 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title,
1878 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1879 WS_CAPTION | WS_SYSMENU,
1881 r.right-r.left, r.bottom-r.top,
1882 fe->hwnd, NULL, fe->inst, NULL);
1886 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
1888 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
1889 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc);
1892 * Count the controls so we can allocate cfgaux.
1894 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
1896 fe->cfgaux = snewn(nctrls, struct cfg_aux);
1900 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
1904 * Edit box with a label beside it.
1906 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
1907 "Static", 0, 0, i->name, id++);
1908 ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2,
1909 "EDIT", WS_TABSTOP | ES_AUTOHSCROLL,
1910 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
1911 SetWindowText(ctl, i->sval);
1919 mkctrl(fe, col1l, col2r, y, y+height, "BUTTON",
1920 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
1921 0, i->name, (j->ctlid = id++));
1922 CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0));
1928 * Drop-down list with a label beside it.
1930 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
1931 "STATIC", 0, 0, i->name, id++);
1932 ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2,
1933 "COMBOBOX", WS_TABSTOP |
1934 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
1935 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
1937 char c, *p, *q, *str;
1939 SendMessage(ctl, CB_RESETCONTENT, 0, 0);
1944 while (*q && *q != c) q++;
1945 str = snewn(q-p+1, char);
1946 strncpy(str, p, q-p);
1948 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str);
1955 SendMessage(ctl, CB_SETCURSEL, i->ival, 0);
1961 assert(y < winheight);
1965 y += height/2; /* extra space before OK and Cancel */
1966 mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON",
1967 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
1969 mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON",
1970 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL);
1972 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
1974 EnableWindow(fe->hwnd, FALSE);
1975 ShowWindow(fe->cfgbox, SW_NORMAL);
1976 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
1977 if (!IsDialogMessage(fe->cfgbox, &msg))
1978 DispatchMessage(&msg);
1982 EnableWindow(fe->hwnd, TRUE);
1983 SetForegroundWindow(fe->hwnd);
1984 DestroyWindow(fe->cfgbox);
1985 DeleteObject(fe->cfgfont);
1990 return (fe->dlg_done == 2);
1993 static void new_game_size(frontend *fe)
1999 get_max_puzzle_size(fe, &x, &y);
2000 midend_size(fe->me, &x, &y, FALSE);
2005 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
2007 if (fe->statusbar != NULL) {
2008 GetWindowRect(fe->statusbar, &sr);
2010 sr.left = sr.right = sr.top = sr.bottom = 0;
2012 SetWindowPos(fe->hwnd, NULL, 0, 0,
2014 r.bottom - r.top + sr.bottom - sr.top,
2015 SWP_NOMOVE | SWP_NOZORDER);
2017 check_window_size(fe, &x, &y);
2019 if (fe->statusbar != NULL)
2020 SetWindowPos(fe->statusbar, NULL, 0, y, x,
2021 sr.bottom - sr.top, SWP_NOZORDER);
2023 if (fe->bitmap) DeleteObject(fe->bitmap);
2025 hdc = GetDC(fe->hwnd);
2026 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
2027 ReleaseDC(fe->hwnd, hdc);
2029 midend_redraw(fe->me);
2032 static void new_game_type(frontend *fe)
2034 midend_new_game(fe->me);
2038 static int is_alt_pressed(void)
2041 int r = GetKeyboardState(keystate);
2044 if (keystate[VK_MENU] & 0x80)
2046 if (keystate[VK_RMENU] & 0x80)
2051 static void savefile_write(void *wctx, void *buf, int len)
2053 FILE *fp = (FILE *)wctx;
2054 fwrite(buf, 1, len, fp);
2057 static int savefile_read(void *wctx, void *buf, int len)
2059 FILE *fp = (FILE *)wctx;
2062 ret = fread(buf, 1, len, fp);
2063 return (ret == len);
2066 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
2067 WPARAM wParam, LPARAM lParam)
2069 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
2074 DestroyWindow(hwnd);
2077 cmd = wParam & ~0xF; /* low 4 bits reserved to Windows */
2080 if (!midend_process_key(fe->me, 0, 0, 'n'))
2084 midend_restart_game(fe->me);
2087 if (!midend_process_key(fe->me, 0, 0, 'u'))
2091 if (!midend_process_key(fe->me, 0, 0, '\x12'))
2096 char *text = midend_text_format(fe->me);
2098 write_clip(hwnd, text);
2100 MessageBeep(MB_ICONWARNING);
2106 char *msg = midend_solve(fe->me);
2108 MessageBox(hwnd, msg, "Unable to solve",
2109 MB_ICONERROR | MB_OK);
2113 if (!midend_process_key(fe->me, 0, 0, 'q'))
2117 if (get_config(fe, CFG_SETTINGS))
2121 if (get_config(fe, CFG_SEED))
2125 if (get_config(fe, CFG_DESC))
2129 if (get_config(fe, CFG_PRINT))
2139 char filename[FILENAME_MAX];
2142 memset(&of, 0, sizeof(of));
2143 of.hwndOwner = hwnd;
2144 of.lpstrFilter = "All Files (*.*)\0*\0\0\0";
2145 of.lpstrCustomFilter = NULL;
2146 of.nFilterIndex = 1;
2147 of.lpstrFile = filename;
2149 of.nMaxFile = lenof(filename);
2150 of.lpstrFileTitle = NULL;
2151 of.lpstrTitle = (cmd == IDM_SAVE ?
2152 "Enter name of game file to save" :
2153 "Enter name of saved game file to load");
2155 #ifdef OPENFILENAME_SIZE_VERSION_400
2156 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
2158 of.lStructSize = sizeof(of);
2160 of.lpstrInitialDir = NULL;
2162 if (cmd == IDM_SAVE)
2163 ret = GetSaveFileName(&of);
2165 ret = GetOpenFileName(&of);
2168 if (cmd == IDM_SAVE) {
2171 if ((fp = fopen(filename, "r")) != NULL) {
2172 char buf[256 + FILENAME_MAX];
2176 sprintf(buf, "Are you sure you want to overwrite"
2177 " the file \"%.*s\"?",
2178 FILENAME_MAX, filename);
2179 if (MessageBox(hwnd, buf, "Question",
2180 MB_YESNO | MB_ICONQUESTION)
2185 fp = fopen(filename, "w");
2188 MessageBox(hwnd, "Unable to open save file",
2189 "Error", MB_ICONERROR | MB_OK);
2193 midend_serialise(fe->me, savefile_write, fp);
2197 FILE *fp = fopen(filename, "r");
2201 MessageBox(hwnd, "Unable to open saved game file",
2202 "Error", MB_ICONERROR | MB_OK);
2206 err = midend_deserialise(fe->me, savefile_read, fp);
2211 MessageBox(hwnd, err, "Error", MB_ICONERROR|MB_OK);
2222 start_help(fe, NULL);
2225 start_help(fe, help_topic);
2229 int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10;
2231 if (p >= 0 && p < fe->npresets) {
2232 midend_set_params(fe->me, fe->presets[p]);
2249 hdc = BeginPaint(hwnd, &p);
2250 hdc2 = CreateCompatibleDC(hdc);
2251 prevbm = SelectObject(hdc2, fe->bitmap);
2253 p.rcPaint.left, p.rcPaint.top,
2254 p.rcPaint.right - p.rcPaint.left,
2255 p.rcPaint.bottom - p.rcPaint.top,
2257 p.rcPaint.left, p.rcPaint.top,
2259 SelectObject(hdc2, prevbm);
2268 int r = GetKeyboardState(keystate);
2269 int shift = (r && (keystate[VK_SHIFT] & 0x80)) ? MOD_SHFT : 0;
2270 int ctrl = (r && (keystate[VK_CONTROL] & 0x80)) ? MOD_CTRL : 0;
2274 if (!(lParam & 0x01000000))
2275 key = MOD_NUM_KEYPAD | '4';
2277 key = shift | ctrl | CURSOR_LEFT;
2280 if (!(lParam & 0x01000000))
2281 key = MOD_NUM_KEYPAD | '6';
2283 key = shift | ctrl | CURSOR_RIGHT;
2286 if (!(lParam & 0x01000000))
2287 key = MOD_NUM_KEYPAD | '8';
2289 key = shift | ctrl | CURSOR_UP;
2292 if (!(lParam & 0x01000000))
2293 key = MOD_NUM_KEYPAD | '2';
2295 key = shift | ctrl | CURSOR_DOWN;
2298 * Diagonal keys on the numeric keypad.
2301 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '9';
2304 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '3';
2307 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '7';
2310 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '1';
2313 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '0';
2316 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '5';
2319 * Numeric keypad keys with Num Lock on.
2321 case VK_NUMPAD4: key = MOD_NUM_KEYPAD | '4'; break;
2322 case VK_NUMPAD6: key = MOD_NUM_KEYPAD | '6'; break;
2323 case VK_NUMPAD8: key = MOD_NUM_KEYPAD | '8'; break;
2324 case VK_NUMPAD2: key = MOD_NUM_KEYPAD | '2'; break;
2325 case VK_NUMPAD5: key = MOD_NUM_KEYPAD | '5'; break;
2326 case VK_NUMPAD9: key = MOD_NUM_KEYPAD | '9'; break;
2327 case VK_NUMPAD3: key = MOD_NUM_KEYPAD | '3'; break;
2328 case VK_NUMPAD7: key = MOD_NUM_KEYPAD | '7'; break;
2329 case VK_NUMPAD1: key = MOD_NUM_KEYPAD | '1'; break;
2330 case VK_NUMPAD0: key = MOD_NUM_KEYPAD | '0'; break;
2334 if (!midend_process_key(fe->me, 0, 0, key))
2339 m.message = WM_KEYDOWN;
2341 m.lParam = lParam & 0xdfff;
2342 TranslateMessage(&m);
2346 case WM_LBUTTONDOWN:
2347 case WM_RBUTTONDOWN:
2348 case WM_MBUTTONDOWN:
2353 * Shift-clicks count as middle-clicks, since otherwise
2354 * two-button Windows users won't have any kind of
2355 * middle click to use.
2357 if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT))
2358 button = MIDDLE_BUTTON;
2359 else if (message == WM_RBUTTONDOWN || is_alt_pressed())
2360 button = RIGHT_BUTTON;
2362 button = LEFT_BUTTON;
2364 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
2365 (signed short)HIWORD(lParam), button))
2378 * Shift-clicks count as middle-clicks, since otherwise
2379 * two-button Windows users won't have any kind of
2380 * middle click to use.
2382 if (message == WM_MBUTTONUP || (wParam & MK_SHIFT))
2383 button = MIDDLE_RELEASE;
2384 else if (message == WM_RBUTTONUP || is_alt_pressed())
2385 button = RIGHT_RELEASE;
2387 button = LEFT_RELEASE;
2389 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
2390 (signed short)HIWORD(lParam), button))
2400 if (wParam & (MK_MBUTTON | MK_SHIFT))
2401 button = MIDDLE_DRAG;
2402 else if (wParam & MK_RBUTTON || is_alt_pressed())
2403 button = RIGHT_DRAG;
2407 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
2408 (signed short)HIWORD(lParam), button))
2413 if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam))
2418 DWORD now = GetTickCount();
2419 float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F;
2420 midend_timer(fe->me, elapsed);
2421 fe->timer_last_tickcount = now;
2426 return DefWindowProc(hwnd, message, wParam, lParam);
2429 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
2434 InitCommonControls();
2440 wndclass.lpfnWndProc = WndProc;
2441 wndclass.cbClsExtra = 0;
2442 wndclass.cbWndExtra = 0;
2443 wndclass.hInstance = inst;
2444 wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION);
2445 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
2446 wndclass.hbrBackground = NULL;
2447 wndclass.lpszMenuName = NULL;
2448 wndclass.lpszClassName = thegame.name;
2450 RegisterClass(&wndclass);
2453 while (*cmdline && isspace((unsigned char)*cmdline))
2458 if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) {
2460 sprintf(buf, "%.100s Error", thegame.name);
2461 MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
2465 while (GetMessage(&msg, NULL, 0, 0)) {
2466 DispatchMessage(&msg);