2 * windows.c: Windows front end for my puzzle collection.
18 #define IDM_NEW 0x0010
19 #define IDM_RESTART 0x0020
20 #define IDM_UNDO 0x0030
21 #define IDM_REDO 0x0040
22 #define IDM_COPY 0x0050
23 #define IDM_SOLVE 0x0060
24 #define IDM_QUIT 0x0070
25 #define IDM_CONFIG 0x0080
26 #define IDM_DESC 0x0090
27 #define IDM_SEED 0x00A0
28 #define IDM_HELPC 0x00B0
29 #define IDM_GAMEHELP 0x00C0
30 #define IDM_ABOUT 0x00D0
31 #define IDM_SAVE 0x00E0
32 #define IDM_LOAD 0x00F0
33 #define IDM_PRINT 0x0100
34 #define IDM_PRESETS 0x0110
36 #define HELP_FILE_NAME "puzzles.hlp"
37 #define HELP_CNT_NAME "puzzles.cnt"
40 static FILE *debug_fp = NULL;
41 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
42 static int debug_got_console = 0;
48 if (!debug_got_console) {
50 debug_got_console = 1;
51 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
55 debug_fp = fopen("debug.log", "w");
58 if (debug_hdl != INVALID_HANDLE_VALUE) {
59 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
65 void debug_printf(char *fmt, ...)
71 vsprintf(buf, fmt, ap);
77 #define WINFLAGS (WS_OVERLAPPEDWINDOW &~ \
78 (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED))
80 static void new_game_size(frontend *fe);
98 enum { CFG_PRINT = CFG_FRONTEND_SPECIFIC };
102 HWND hwnd, statusbar, cfgbox;
104 HBITMAP bitmap, prevbm;
111 DWORD timer_last_tickcount;
113 game_params **presets;
115 int nfonts, fontsize;
117 struct cfg_aux *cfgaux;
118 int cfg_which, dlg_done;
123 int help_has_contents;
124 enum { DRAWING, PRINTING, NOTHING } drawstatus;
126 int printcount, printw, printh, printsolns, printcurr, printcolour;
128 int printoffsetx, printoffsety;
129 float printpixelscale;
135 void fatal(char *fmt, ...)
141 vsprintf(buf, fmt, ap);
144 MessageBox(NULL, buf, "Fatal error", MB_ICONEXCLAMATION | MB_OK);
149 char *geterrstr(void)
152 DWORD dw = GetLastError();
156 FORMAT_MESSAGE_ALLOCATE_BUFFER |
157 FORMAT_MESSAGE_FROM_SYSTEM,
160 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
164 ret = dupstr(lpMsgBuf);
171 void get_random_seed(void **randseed, int *randseedsize)
173 time_t *tp = snew(time_t);
175 *randseed = (void *)tp;
176 *randseedsize = sizeof(time_t);
179 static void win_status_bar(void *handle, char *text)
181 frontend *fe = (frontend *)handle;
183 SetWindowText(fe->statusbar, text);
186 static blitter *win_blitter_new(void *handle, int w, int h)
188 blitter *bl = snew(blitter);
190 memset(bl, 0, sizeof(blitter));
198 static void win_blitter_free(void *handle, blitter *bl)
200 if (bl->bitmap) DeleteObject(bl->bitmap);
204 static void blitter_mkbitmap(frontend *fe, blitter *bl)
206 HDC hdc = GetDC(fe->hwnd);
207 bl->bitmap = CreateCompatibleBitmap(hdc, bl->w, bl->h);
208 ReleaseDC(fe->hwnd, hdc);
211 /* BitBlt(dstDC, dstX, dstY, dstW, dstH, srcDC, srcX, srcY, dType) */
213 static void win_blitter_save(void *handle, blitter *bl, int x, int y)
215 frontend *fe = (frontend *)handle;
216 HDC hdc_win, hdc_blit;
219 assert(fe->drawstatus == DRAWING);
221 if (!bl->bitmap) blitter_mkbitmap(fe, bl);
223 bl->x = x; bl->y = y;
225 hdc_win = GetDC(fe->hwnd);
226 hdc_blit = CreateCompatibleDC(hdc_win);
227 if (!hdc_blit) fatal("hdc_blit failed: 0x%x", GetLastError());
229 prev_blit = SelectObject(hdc_blit, bl->bitmap);
230 if (prev_blit == NULL || prev_blit == HGDI_ERROR)
231 fatal("SelectObject for hdc_main failed: 0x%x", GetLastError());
233 if (!BitBlt(hdc_blit, 0, 0, bl->w, bl->h,
234 fe->hdc, x, y, SRCCOPY))
235 fatal("BitBlt failed: 0x%x", GetLastError());
237 SelectObject(hdc_blit, prev_blit);
239 ReleaseDC(fe->hwnd, hdc_win);
242 static void win_blitter_load(void *handle, blitter *bl, int x, int y)
244 frontend *fe = (frontend *)handle;
245 HDC hdc_win, hdc_blit;
248 assert(fe->drawstatus == DRAWING);
250 assert(bl->bitmap); /* we should always have saved before loading */
252 if (x == BLITTER_FROMSAVED) x = bl->x;
253 if (y == BLITTER_FROMSAVED) y = bl->y;
255 hdc_win = GetDC(fe->hwnd);
256 hdc_blit = CreateCompatibleDC(hdc_win);
258 prev_blit = SelectObject(hdc_blit, bl->bitmap);
260 BitBlt(fe->hdc, x, y, bl->w, bl->h,
261 hdc_blit, 0, 0, SRCCOPY);
263 SelectObject(hdc_blit, prev_blit);
265 ReleaseDC(fe->hwnd, hdc_win);
268 void frontend_default_colour(frontend *fe, float *output)
270 DWORD c = GetSysColor(COLOR_MENU); /* ick */
272 output[0] = (float)(GetRValue(c) / 255.0);
273 output[1] = (float)(GetGValue(c) / 255.0);
274 output[2] = (float)(GetBValue(c) / 255.0);
277 static POINT win_transform_point(frontend *fe, int x, int y)
281 assert(fe->drawstatus != NOTHING);
283 if (fe->drawstatus == PRINTING) {
284 ret.x = (int)(fe->printoffsetx + fe->printpixelscale * x);
285 ret.y = (int)(fe->printoffsety + fe->printpixelscale * y);
294 static void win_text_colour(frontend *fe, int colour)
296 assert(fe->drawstatus != NOTHING);
298 if (fe->drawstatus == PRINTING) {
301 print_get_colour(fe->dr, colour, &hatch, &r, &g, &b);
303 SetTextColor(fe->hdc, RGB(r * 255, g * 255, b * 255));
305 SetTextColor(fe->hdc,
306 hatch == HATCH_CLEAR ? RGB(255,255,255) : RGB(0,0,0));
308 SetTextColor(fe->hdc, fe->colours[colour]);
312 static void win_set_brush(frontend *fe, int colour)
315 assert(fe->drawstatus != NOTHING);
317 if (fe->drawstatus == PRINTING) {
320 print_get_colour(fe->dr, colour, &hatch, &r, &g, &b);
323 br = CreateSolidBrush(RGB(r * 255, g * 255, b * 255));
324 else if (hatch == HATCH_SOLID)
325 br = CreateSolidBrush(RGB(0,0,0));
326 else if (hatch == HATCH_CLEAR)
327 br = CreateSolidBrush(RGB(255,255,255));
329 br = CreateHatchBrush(hatch == HATCH_BACKSLASH ? HS_FDIAGONAL :
330 hatch == HATCH_SLASH ? HS_BDIAGONAL :
331 hatch == HATCH_HORIZ ? HS_HORIZONTAL :
332 hatch == HATCH_VERT ? HS_VERTICAL :
333 hatch == HATCH_PLUS ? HS_CROSS :
334 /* hatch == HATCH_X ? */ HS_DIAGCROSS,
337 br = fe->brushes[colour];
339 fe->oldbr = SelectObject(fe->hdc, br);
342 static void win_reset_brush(frontend *fe)
346 assert(fe->drawstatus != NOTHING);
348 br = SelectObject(fe->hdc, fe->oldbr);
349 if (fe->drawstatus == PRINTING)
353 static void win_set_pen(frontend *fe, int colour, int thin)
356 assert(fe->drawstatus != NOTHING);
358 if (fe->drawstatus == PRINTING) {
361 int width = thin ? 0 : fe->linewidth;
363 print_get_colour(fe->dr, colour, &hatch, &r, &g, &b);
365 pen = CreatePen(PS_SOLID, width,
366 RGB(r * 255, g * 255, b * 255));
367 else if (hatch == HATCH_SOLID)
368 pen = CreatePen(PS_SOLID, width, RGB(0, 0, 0));
369 else if (hatch == HATCH_CLEAR)
370 pen = CreatePen(PS_SOLID, width, RGB(255,255,255));
372 assert(!"This shouldn't happen");
373 pen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
376 pen = fe->pens[colour];
378 fe->oldpen = SelectObject(fe->hdc, pen);
381 static void win_reset_pen(frontend *fe)
385 assert(fe->drawstatus != NOTHING);
387 pen = SelectObject(fe->hdc, fe->oldpen);
388 if (fe->drawstatus == PRINTING)
392 static void win_clip(void *handle, int x, int y, int w, int h)
394 frontend *fe = (frontend *)handle;
397 if (fe->drawstatus == NOTHING)
400 p = win_transform_point(fe, x, y);
401 q = win_transform_point(fe, x+w, y+h);
402 IntersectClipRect(fe->hdc, p.x, p.y, q.x, q.y);
405 static void win_unclip(void *handle)
407 frontend *fe = (frontend *)handle;
409 if (fe->drawstatus == NOTHING)
412 SelectClipRgn(fe->hdc, NULL);
415 static void win_draw_text(void *handle, int x, int y, int fonttype,
416 int fontsize, int align, int colour, char *text)
418 frontend *fe = (frontend *)handle;
422 if (fe->drawstatus == NOTHING)
425 if (fe->drawstatus == PRINTING)
426 fontsize = (int)(fontsize * fe->printpixelscale);
428 xy = win_transform_point(fe, x, y);
431 * Find or create the font.
433 for (i = fe->fontstart; i < fe->nfonts; i++)
434 if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
437 if (i == fe->nfonts) {
438 if (fe->fontsize <= fe->nfonts) {
439 fe->fontsize = fe->nfonts + 10;
440 fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
445 fe->fonts[i].type = fonttype;
446 fe->fonts[i].size = fontsize;
448 fe->fonts[i].font = CreateFont(-fontsize, 0, 0, 0,
449 fe->drawstatus == PRINTING ? 0 : FW_BOLD,
450 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
451 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
453 (fonttype == FONT_FIXED ?
454 FIXED_PITCH | FF_DONTCARE :
455 VARIABLE_PITCH | FF_SWISS),
460 * Position and draw the text.
467 oldfont = SelectObject(fe->hdc, fe->fonts[i].font);
468 if (GetTextMetrics(fe->hdc, &tm)) {
469 if (align & ALIGN_VCENTRE)
470 xy.y -= (tm.tmAscent+tm.tmDescent)/2;
474 if (GetTextExtentPoint32(fe->hdc, text, strlen(text), &size)) {
475 if (align & ALIGN_HCENTRE)
477 else if (align & ALIGN_HRIGHT)
480 SetBkMode(fe->hdc, TRANSPARENT);
481 win_text_colour(fe, colour);
482 TextOut(fe->hdc, xy.x, xy.y, text, strlen(text));
483 SelectObject(fe->hdc, oldfont);
487 static void win_draw_rect(void *handle, int x, int y, int w, int h, int colour)
489 frontend *fe = (frontend *)handle;
492 if (fe->drawstatus == NOTHING)
495 if (fe->drawstatus == DRAWING && w == 1 && h == 1) {
497 * Rectangle() appears to get uppity if asked to draw a 1x1
498 * rectangle, presumably on the grounds that that's beneath
499 * its dignity and you ought to be using SetPixel instead.
502 SetPixel(fe->hdc, x, y, fe->colours[colour]);
504 win_set_brush(fe, colour);
505 win_set_pen(fe, colour, TRUE);
506 p = win_transform_point(fe, x, y);
507 q = win_transform_point(fe, x+w, y+h);
508 Rectangle(fe->hdc, p.x, p.y, q.x, q.y);
514 static void win_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour)
516 frontend *fe = (frontend *)handle;
519 if (fe->drawstatus == NOTHING)
522 win_set_pen(fe, colour, FALSE);
523 p = win_transform_point(fe, x1, y1);
524 q = win_transform_point(fe, x2, y2);
525 MoveToEx(fe->hdc, p.x, p.y, NULL);
526 LineTo(fe->hdc, q.x, q.y);
527 if (fe->drawstatus == DRAWING)
528 SetPixel(fe->hdc, q.x, q.y, fe->colours[colour]);
532 static void win_draw_circle(void *handle, int cx, int cy, int radius,
533 int fillcolour, int outlinecolour)
535 frontend *fe = (frontend *)handle;
538 assert(outlinecolour >= 0);
540 if (fe->drawstatus == NOTHING)
543 if (fillcolour >= 0) {
544 win_set_brush(fe, fillcolour);
545 win_set_pen(fe, outlinecolour, FALSE);
546 p = win_transform_point(fe, cx - radius, cy - radius);
547 q = win_transform_point(fe, cx + radius, cy + radius);
548 Ellipse(fe->hdc, p.x, p.y, q.x+1, q.y+1);
552 win_set_pen(fe, outlinecolour, FALSE);
553 p = win_transform_point(fe, cx - radius, cy - radius);
554 q = win_transform_point(fe, cx + radius, cy + radius);
555 r = win_transform_point(fe, cx - radius, cy);
556 Arc(fe->hdc, p.x, p.y, q.x+1, q.y+1, r.x, r.y, r.x, r.y);
561 static void win_draw_polygon(void *handle, int *coords, int npoints,
562 int fillcolour, int outlinecolour)
564 frontend *fe = (frontend *)handle;
568 if (fe->drawstatus == NOTHING)
571 pts = snewn(npoints+1, POINT);
573 for (i = 0; i <= npoints; i++) {
574 int j = (i < npoints ? i : 0);
575 pts[i] = win_transform_point(fe, coords[j*2], coords[j*2+1]);
578 assert(outlinecolour >= 0);
580 if (fillcolour >= 0) {
581 win_set_brush(fe, fillcolour);
582 win_set_pen(fe, outlinecolour, FALSE);
583 Polygon(fe->hdc, pts, npoints);
587 win_set_pen(fe, outlinecolour, FALSE);
588 Polyline(fe->hdc, pts, npoints+1);
595 static void win_start_draw(void *handle)
597 frontend *fe = (frontend *)handle;
600 assert(fe->drawstatus == NOTHING);
602 hdc_win = GetDC(fe->hwnd);
603 fe->hdc = CreateCompatibleDC(hdc_win);
604 fe->prevbm = SelectObject(fe->hdc, fe->bitmap);
605 ReleaseDC(fe->hwnd, hdc_win);
607 SetMapMode(fe->hdc, MM_TEXT);
608 fe->drawstatus = DRAWING;
611 static void win_draw_update(void *handle, int x, int y, int w, int h)
613 frontend *fe = (frontend *)handle;
616 if (fe->drawstatus != DRAWING)
624 InvalidateRect(fe->hwnd, &r, FALSE);
627 static void win_end_draw(void *handle)
629 frontend *fe = (frontend *)handle;
630 assert(fe->drawstatus == DRAWING);
631 SelectObject(fe->hdc, fe->prevbm);
634 DeleteObject(fe->clip);
637 fe->drawstatus = NOTHING;
640 static void win_line_width(void *handle, float width)
642 frontend *fe = (frontend *)handle;
644 assert(fe->drawstatus != DRAWING);
645 if (fe->drawstatus == NOTHING)
648 fe->linewidth = (int)(width * fe->printpixelscale);
651 static void win_begin_doc(void *handle, int pages)
653 frontend *fe = (frontend *)handle;
655 assert(fe->drawstatus != DRAWING);
656 if (fe->drawstatus == NOTHING)
659 if (StartDoc(fe->hdc, &fe->di) <= 0) {
660 char *e = geterrstr();
661 MessageBox(fe->hwnd, e, "Error starting to print",
662 MB_ICONERROR | MB_OK);
664 fe->drawstatus = NOTHING;
668 * Push a marker on the font stack so that we won't use the
669 * same fonts for printing and drawing. (This is because
670 * drawing seems to look generally better in bold, but printing
671 * is better not in bold.)
673 fe->fontstart = fe->nfonts;
676 static void win_begin_page(void *handle, int number)
678 frontend *fe = (frontend *)handle;
680 assert(fe->drawstatus != DRAWING);
681 if (fe->drawstatus == NOTHING)
684 if (StartPage(fe->hdc) <= 0) {
685 char *e = geterrstr();
686 MessageBox(fe->hwnd, e, "Error starting a page",
687 MB_ICONERROR | MB_OK);
689 fe->drawstatus = NOTHING;
693 static void win_begin_puzzle(void *handle, float xm, float xc,
694 float ym, float yc, int pw, int ph, float wmm)
696 frontend *fe = (frontend *)handle;
697 int ppw, pph, pox, poy;
698 float mmpw, mmph, mmox, mmoy;
701 assert(fe->drawstatus != DRAWING);
702 if (fe->drawstatus == NOTHING)
705 ppw = GetDeviceCaps(fe->hdc, HORZRES);
706 pph = GetDeviceCaps(fe->hdc, VERTRES);
707 mmpw = (float)GetDeviceCaps(fe->hdc, HORZSIZE);
708 mmph = (float)GetDeviceCaps(fe->hdc, VERTSIZE);
711 * Compute the puzzle's position on the logical page.
713 mmox = xm * mmpw + xc;
714 mmoy = ym * mmph + yc;
717 * Work out what that comes to in pixels.
719 pox = (int)(mmox * (float)ppw / mmpw);
720 poy = (int)(mmoy * (float)ppw / mmpw);
723 * And determine the scale.
725 * I need a scale such that the maximum puzzle-coordinate
726 * extent of the rectangle (pw * scale) is equal to the pixel
727 * equivalent of the puzzle's millimetre width (wmm * ppw /
730 scale = (wmm * ppw) / (mmpw * pw);
733 * Now store pox, poy and scale for use in the main drawing
736 fe->printoffsetx = pox;
737 fe->printoffsety = poy;
738 fe->printpixelscale = scale;
743 static void win_end_puzzle(void *handle)
745 /* Nothing needs to be done here. */
748 static void win_end_page(void *handle, int number)
750 frontend *fe = (frontend *)handle;
752 assert(fe->drawstatus != DRAWING);
754 if (fe->drawstatus == NOTHING)
757 if (EndPage(fe->hdc) <= 0) {
758 char *e = geterrstr();
759 MessageBox(fe->hwnd, e, "Error finishing a page",
760 MB_ICONERROR | MB_OK);
762 fe->drawstatus = NOTHING;
766 static void win_end_doc(void *handle)
768 frontend *fe = (frontend *)handle;
770 assert(fe->drawstatus != DRAWING);
773 * Free all the fonts created since we began printing.
775 while (fe->nfonts > fe->fontstart) {
777 DeleteObject(fe->fonts[fe->nfonts].font);
782 * The MSDN web site sample code doesn't bother to call EndDoc
783 * if an error occurs half way through printing. I expect doing
784 * so would cause the erroneous document to actually be
785 * printed, or something equally undesirable.
787 if (fe->drawstatus == NOTHING)
790 if (EndDoc(fe->hdc) <= 0) {
791 char *e = geterrstr();
792 MessageBox(fe->hwnd, e, "Error finishing printing",
793 MB_ICONERROR | MB_OK);
795 fe->drawstatus = NOTHING;
799 const struct drawing_api win_drawing = {
824 void print(frontend *fe)
829 midend *nme = NULL; /* non-interactive midend for bulk puzzle generation */
834 * Create our document structure and fill it up with puzzles.
836 doc = document_new(fe->printw, fe->printh, fe->printscale / 100.0F);
837 for (i = 0; i < fe->printcount; i++) {
838 if (i == 0 && fe->printcurr) {
839 err = midend_print_puzzle(fe->me, doc, fe->printsolns);
844 nme = midend_new(NULL, &thegame, NULL, NULL);
847 * Set the non-interactive mid-end to have the same
848 * parameters as the standard one.
850 params = midend_get_params(fe->me);
851 midend_set_params(nme, params);
852 thegame.free_params(params);
855 midend_new_game(nme);
856 err = midend_print_puzzle(nme, doc, fe->printsolns);
865 MessageBox(fe->hwnd, err, "Error preparing puzzles for printing",
866 MB_ICONERROR | MB_OK);
871 memset(&pd, 0, sizeof(pd));
872 pd.lStructSize = sizeof(pd);
873 pd.hwndOwner = fe->hwnd;
876 pd.Flags = PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC |
877 PD_NOPAGENUMS | PD_NOSELECTION;
879 pd.nFromPage = pd.nToPage = 0xFFFF;
880 pd.nMinPage = pd.nMaxPage = 1;
882 if (!PrintDlg(&pd)) {
888 * Now pd.hDC is a device context for the printer.
892 * FIXME: IWBNI we put up an Abort box here.
895 memset(&fe->di, 0, sizeof(fe->di));
896 fe->di.cbSize = sizeof(fe->di);
897 sprintf(doctitle, "Printed puzzles from %s (from Simon Tatham's"
898 " Portable Puzzle Collection)", thegame.name);
899 fe->di.lpszDocName = doctitle;
900 fe->di.lpszOutput = NULL;
901 fe->di.lpszDatatype = NULL;
904 fe->drawstatus = PRINTING;
907 fe->dr = drawing_new(&win_drawing, NULL, fe);
908 document_print(doc, fe->dr);
909 drawing_free(fe->dr);
912 fe->drawstatus = NOTHING;
918 void deactivate_timer(frontend *fe)
921 return; /* for non-interactive midend */
922 if (fe->hwnd) KillTimer(fe->hwnd, fe->timer);
926 void activate_timer(frontend *fe)
929 return; /* for non-interactive midend */
931 fe->timer = SetTimer(fe->hwnd, fe->timer, 20, NULL);
932 fe->timer_last_tickcount = GetTickCount();
936 void write_clip(HWND hwnd, char *data)
944 * Windows expects CRLF in the clipboard, so we must convert
945 * any \n that has come out of the puzzle backend.
948 for (i = 0; data[i]; i++) {
953 data2 = snewn(len+1, char);
955 for (i = 0; data[i]; i++) {
958 data2[j++] = data[i];
963 clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
966 lock = GlobalLock(clipdata);
969 memcpy(lock, data2, len);
970 ((unsigned char *) lock)[len] = 0;
971 GlobalUnlock(clipdata);
973 if (OpenClipboard(hwnd)) {
975 SetClipboardData(CF_TEXT, clipdata);
978 GlobalFree(clipdata);
984 * See if we can find a help file.
986 static void find_help_file(frontend *fe)
988 char b[2048], *p, *q, *r;
990 if (!fe->help_path) {
991 GetModuleFileName(NULL, b, sizeof(b) - 1);
993 p = strrchr(b, '\\');
994 if (p && p >= r) r = p+1;
996 if (q && q >= r) r = q+1;
997 strcpy(r, HELP_FILE_NAME);
998 if ( (fp = fopen(b, "r")) != NULL) {
999 fe->help_path = dupstr(b);
1002 fe->help_path = NULL;
1003 strcpy(r, HELP_CNT_NAME);
1004 if ( (fp = fopen(b, "r")) != NULL) {
1005 fe->help_has_contents = TRUE;
1008 fe->help_has_contents = FALSE;
1012 static void check_window_size(frontend *fe, int *px, int *py)
1017 if (fe->statusbar) {
1019 GetWindowRect(fe->statusbar, &sr);
1020 sy = sr.bottom - sr.top;
1026 * See if we actually got the window size we wanted, and adjust
1027 * the puzzle size if not.
1029 GetClientRect(fe->hwnd, &r);
1030 x = r.right - r.left;
1031 y = r.bottom - r.top - sy;
1032 midend_size(fe->me, &x, &y, FALSE);
1033 if (x != r.right - r.left || y != r.bottom - r.top) {
1035 * Resize the window, now we know what size we _really_
1041 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1042 SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left, r.bottom - r.top,
1043 SWP_NOMOVE | SWP_NOZORDER);
1046 if (fe->statusbar) {
1047 GetClientRect(fe->hwnd, &r);
1048 SetWindowPos(fe->statusbar, NULL, 0, r.bottom-r.top-sy, r.right-r.left,
1056 static void get_max_puzzle_size(frontend *fe, int *x, int *y)
1060 if (SystemParametersInfo(SPI_GETWORKAREA, 0, &sr, FALSE)) {
1061 *x = sr.right - sr.left;
1062 *y = sr.bottom - sr.top;
1067 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1068 *x -= r.right - r.left - 100;
1069 *y -= r.bottom - r.top - 100;
1074 if (fe->statusbar != NULL) {
1075 GetWindowRect(fe->statusbar, &sr);
1076 *y -= sr.bottom - sr.top;
1080 static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
1086 fe = snew(frontend);
1088 fe->me = midend_new(fe, &thegame, &win_drawing, fe);
1091 *error = midend_game_id(fe->me, game_id);
1093 midend_free(fe->me);
1099 fe->help_path = NULL;
1107 fe->drawstatus = NOTHING;
1111 midend_new_game(fe->me);
1114 fe->nfonts = fe->fontsize = 0;
1120 colours = midend_colours(fe->me, &ncolours);
1122 fe->colours = snewn(ncolours, COLORREF);
1123 fe->brushes = snewn(ncolours, HBRUSH);
1124 fe->pens = snewn(ncolours, HPEN);
1126 for (i = 0; i < ncolours; i++) {
1127 fe->colours[i] = RGB(255 * colours[i*3+0],
1128 255 * colours[i*3+1],
1129 255 * colours[i*3+2]);
1130 fe->brushes[i] = CreateSolidBrush(fe->colours[i]);
1131 fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]);
1136 if (midend_wants_statusbar(fe->me)) {
1137 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh",
1138 WS_CHILD | WS_VISIBLE,
1139 0, 0, 0, 0, /* status bar does these */
1140 NULL, NULL, inst, NULL);
1142 fe->statusbar = NULL;
1144 get_max_puzzle_size(fe, &x, &y);
1145 midend_size(fe->me, &x, &y, FALSE);
1150 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1152 fe->hwnd = CreateWindowEx(0, thegame.name, thegame.name,
1153 WS_OVERLAPPEDWINDOW &~
1154 (WS_THICKFRAME | WS_MAXIMIZEBOX),
1155 CW_USEDEFAULT, CW_USEDEFAULT,
1156 r.right - r.left, r.bottom - r.top,
1157 NULL, NULL, inst, NULL);
1159 if (midend_wants_statusbar(fe->me)) {
1161 DestroyWindow(fe->statusbar);
1162 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh",
1163 WS_CHILD | WS_VISIBLE,
1164 0, 0, 0, 0, /* status bar does these */
1165 fe->hwnd, NULL, inst, NULL);
1167 * Now resize the window to take account of the status bar.
1169 GetWindowRect(fe->statusbar, &sr);
1170 GetWindowRect(fe->hwnd, &r);
1171 SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left,
1172 r.bottom - r.top + sr.bottom - sr.top,
1173 SWP_NOMOVE | SWP_NOZORDER);
1175 fe->statusbar = NULL;
1179 HMENU bar = CreateMenu();
1180 HMENU menu = CreateMenu();
1182 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Game");
1183 AppendMenu(menu, MF_ENABLED, IDM_NEW, "New");
1184 AppendMenu(menu, MF_ENABLED, IDM_RESTART, "Restart");
1185 AppendMenu(menu, MF_ENABLED, IDM_DESC, "Specific...");
1186 AppendMenu(menu, MF_ENABLED, IDM_SEED, "Random Seed...");
1188 if ((fe->npresets = midend_num_presets(fe->me)) > 0 ||
1189 thegame.can_configure) {
1190 HMENU sub = CreateMenu();
1193 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "Type");
1195 fe->presets = snewn(fe->npresets, game_params *);
1197 for (i = 0; i < fe->npresets; i++) {
1200 midend_fetch_preset(fe->me, i, &name, &fe->presets[i]);
1203 * FIXME: we ought to go through and do something
1204 * with ampersands here.
1207 AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name);
1210 if (thegame.can_configure) {
1211 AppendMenu(sub, MF_ENABLED, IDM_CONFIG, "Custom...");
1215 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1216 AppendMenu(menu, MF_ENABLED, IDM_LOAD, "Load");
1217 AppendMenu(menu, MF_ENABLED, IDM_SAVE, "Save");
1218 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1219 if (thegame.can_print) {
1220 AppendMenu(menu, MF_ENABLED, IDM_PRINT, "Print");
1221 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1223 AppendMenu(menu, MF_ENABLED, IDM_UNDO, "Undo");
1224 AppendMenu(menu, MF_ENABLED, IDM_REDO, "Redo");
1225 if (thegame.can_format_as_text) {
1226 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1227 AppendMenu(menu, MF_ENABLED, IDM_COPY, "Copy");
1229 if (thegame.can_solve) {
1230 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1231 AppendMenu(menu, MF_ENABLED, IDM_SOLVE, "Solve");
1233 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1234 AppendMenu(menu, MF_ENABLED, IDM_QUIT, "Exit");
1235 menu = CreateMenu();
1236 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Help");
1237 AppendMenu(menu, MF_ENABLED, IDM_ABOUT, "About");
1238 if (fe->help_path) {
1239 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1240 AppendMenu(menu, MF_ENABLED, IDM_HELPC, "Contents");
1241 if (thegame.winhelp_topic) {
1243 assert(thegame.name);
1244 item = snewn(9+strlen(thegame.name), char); /*ick*/
1245 sprintf(item, "Help on %s", thegame.name);
1246 AppendMenu(menu, MF_ENABLED, IDM_GAMEHELP, item);
1250 SetMenu(fe->hwnd, bar);
1254 new_game_size(fe); /* initialises fe->bitmap */
1255 check_window_size(fe, &x, &y);
1257 SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe);
1259 ShowWindow(fe->hwnd, SW_NORMAL);
1260 SetForegroundWindow(fe->hwnd);
1262 midend_redraw(fe->me);
1267 static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg,
1268 WPARAM wParam, LPARAM lParam)
1270 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1277 if ((HIWORD(wParam) == BN_CLICKED ||
1278 HIWORD(wParam) == BN_DOUBLECLICKED) &&
1279 LOWORD(wParam) == IDOK)
1292 * Wrappers on midend_{get,set}_config, which extend the CFG_*
1293 * enumeration to add CFG_PRINT.
1295 static config_item *frontend_get_config(frontend *fe, int which,
1298 if (which < CFG_FRONTEND_SPECIFIC) {
1299 return midend_get_config(fe->me, which, wintitle);
1300 } else if (which == CFG_PRINT) {
1304 *wintitle = snewn(40 + strlen(thegame.name), char);
1305 sprintf(*wintitle, "%s print setup", thegame.name);
1307 ret = snewn(8, config_item);
1311 ret[i].name = "Number of puzzles to print";
1312 ret[i].type = C_STRING;
1313 ret[i].sval = dupstr("1");
1317 ret[i].name = "Number of puzzles across the page";
1318 ret[i].type = C_STRING;
1319 ret[i].sval = dupstr("1");
1323 ret[i].name = "Number of puzzles down the page";
1324 ret[i].type = C_STRING;
1325 ret[i].sval = dupstr("1");
1329 ret[i].name = "Percentage of standard size";
1330 ret[i].type = C_STRING;
1331 ret[i].sval = dupstr("100.0");
1335 ret[i].name = "Include currently shown puzzle";
1336 ret[i].type = C_BOOLEAN;
1341 ret[i].name = "Print solutions";
1342 ret[i].type = C_BOOLEAN;
1344 ret[i].ival = FALSE;
1347 if (thegame.can_print_in_colour) {
1348 ret[i].name = "Print in colour";
1349 ret[i].type = C_BOOLEAN;
1351 ret[i].ival = FALSE;
1356 ret[i].type = C_END;
1363 assert(!"We should never get here");
1368 static char *frontend_set_config(frontend *fe, int which, config_item *cfg)
1370 if (which < CFG_FRONTEND_SPECIFIC) {
1371 return midend_set_config(fe->me, which, cfg);
1372 } else if (which == CFG_PRINT) {
1373 if ((fe->printcount = atoi(cfg[0].sval)) <= 0)
1374 return "Number of puzzles to print should be at least one";
1375 if ((fe->printw = atoi(cfg[1].sval)) <= 0)
1376 return "Number of puzzles across the page should be at least one";
1377 if ((fe->printh = atoi(cfg[2].sval)) <= 0)
1378 return "Number of puzzles down the page should be at least one";
1379 if ((fe->printscale = (float)atof(cfg[3].sval)) <= 0)
1380 return "Print size should be positive";
1381 fe->printcurr = cfg[4].ival;
1382 fe->printsolns = cfg[5].ival;
1383 fe->printcolour = thegame.can_print_in_colour && cfg[6].ival;
1386 assert(!"We should never get here");
1387 return "Internal error";
1391 static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg,
1392 WPARAM wParam, LPARAM lParam)
1394 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1404 * OK and Cancel are special cases.
1406 if ((HIWORD(wParam) == BN_CLICKED ||
1407 HIWORD(wParam) == BN_DOUBLECLICKED) &&
1408 (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) {
1409 if (LOWORD(wParam) == IDOK) {
1410 char *err = frontend_set_config(fe, fe->cfg_which, fe->cfg);
1413 MessageBox(hwnd, err, "Validation error",
1414 MB_ICONERROR | MB_OK);
1425 * First find the control whose id this is.
1427 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
1428 if (j->ctlid == LOWORD(wParam))
1431 if (i->type == C_END)
1432 return 0; /* not our problem */
1434 if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) {
1436 GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer));
1437 buffer[lenof(buffer)-1] = '\0';
1439 i->sval = dupstr(buffer);
1440 } else if (i->type == C_BOOLEAN &&
1441 (HIWORD(wParam) == BN_CLICKED ||
1442 HIWORD(wParam) == BN_DOUBLECLICKED)) {
1443 i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid);
1444 } else if (i->type == C_CHOICES &&
1445 HIWORD(wParam) == CBN_SELCHANGE) {
1446 i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid,
1447 CB_GETCURSEL, 0, 0);
1460 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
1461 char *wclass, int wstyle,
1462 int exstyle, const char *wtext, int wid)
1465 ret = CreateWindowEx(exstyle, wclass, wtext,
1466 wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1,
1467 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
1468 SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0));
1472 static void about(frontend *fe)
1482 int winwidth, winheight, y;
1483 int height, width, maxwid;
1484 const char *strings[16];
1489 sprintf(titlebuf, "About %.250s", thegame.name);
1491 strings[nstrings++] = thegame.name;
1492 strings[nstrings++] = "from Simon Tatham's Portable Puzzle Collection";
1493 strings[nstrings++] = ver;
1495 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
1496 wc.lpfnWndProc = DefDlgProc;
1498 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
1499 wc.hInstance = fe->inst;
1501 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
1502 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
1503 wc.lpszMenuName = NULL;
1504 wc.lpszClassName = "GameAboutBox";
1507 hdc = GetDC(fe->hwnd);
1508 SetMapMode(hdc, MM_TEXT);
1510 fe->dlg_done = FALSE;
1512 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
1514 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
1515 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
1520 oldfont = SelectObject(hdc, fe->cfgfont);
1521 if (GetTextMetrics(hdc, &tm)) {
1522 height = tm.tmAscent + tm.tmDescent;
1523 width = tm.tmAveCharWidth;
1525 height = width = 30;
1529 * Figure out the layout of the About box by measuring the
1530 * length of each piece of text.
1533 winheight = height/2;
1535 for (i = 0; i < nstrings; i++) {
1536 if (GetTextExtentPoint32(hdc, strings[i], strlen(strings[i]), &size))
1537 lengths[i] = size.cx;
1539 lengths[i] = 0; /* *shrug* */
1540 if (maxwid < lengths[i])
1541 maxwid = lengths[i];
1542 winheight += height * 3 / 2 + (height / 2);
1545 winheight += height + height * 7 / 4; /* OK button */
1546 winwidth = maxwid + 4*width;
1548 SelectObject(hdc, oldfont);
1549 ReleaseDC(fe->hwnd, hdc);
1552 * Create the dialog, now that we know its size.
1559 r.bottom = winheight;
1561 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
1562 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1563 WS_CAPTION | WS_SYSMENU*/) &~
1564 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
1568 * Centre the dialog on its parent window.
1572 GetWindowRect(fe->hwnd, &r2);
1573 r.left = (r2.left + r2.right - r.right) / 2;
1574 r.top = (r2.top + r2.bottom - r.bottom) / 2;
1578 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, titlebuf,
1579 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1580 WS_CAPTION | WS_SYSMENU,
1582 r.right-r.left, r.bottom-r.top,
1583 fe->hwnd, NULL, fe->inst, NULL);
1586 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
1588 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
1589 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)AboutDlgProc);
1593 for (i = 0; i < nstrings; i++) {
1594 int border = width*2 + (maxwid - lengths[i]) / 2;
1595 mkctrl(fe, border, border+lengths[i], y+height*1/8, y+height*9/8,
1596 "Static", 0, 0, strings[i], id++);
1599 assert(y < winheight);
1603 y += height/2; /* extra space before OK */
1604 mkctrl(fe, width*2, maxwid+width*2, y, y+height*7/4, "BUTTON",
1605 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
1608 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
1610 EnableWindow(fe->hwnd, FALSE);
1611 ShowWindow(fe->cfgbox, SW_NORMAL);
1612 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
1613 if (!IsDialogMessage(fe->cfgbox, &msg))
1614 DispatchMessage(&msg);
1618 EnableWindow(fe->hwnd, TRUE);
1619 SetForegroundWindow(fe->hwnd);
1620 DestroyWindow(fe->cfgbox);
1621 DeleteObject(fe->cfgfont);
1624 static int get_config(frontend *fe, int which)
1637 int winwidth, winheight, col1l, col1r, col2l, col2r, y;
1638 int height, width, maxlabel, maxcheckbox;
1640 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
1641 wc.lpfnWndProc = DefDlgProc;
1643 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
1644 wc.hInstance = fe->inst;
1646 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
1647 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
1648 wc.lpszMenuName = NULL;
1649 wc.lpszClassName = "GameConfigBox";
1652 hdc = GetDC(fe->hwnd);
1653 SetMapMode(hdc, MM_TEXT);
1655 fe->dlg_done = FALSE;
1657 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
1659 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
1660 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
1665 oldfont = SelectObject(hdc, fe->cfgfont);
1666 if (GetTextMetrics(hdc, &tm)) {
1667 height = tm.tmAscent + tm.tmDescent;
1668 width = tm.tmAveCharWidth;
1670 height = width = 30;
1673 fe->cfg = frontend_get_config(fe, which, &title);
1674 fe->cfg_which = which;
1677 * Figure out the layout of the config box by measuring the
1678 * length of each piece of text.
1680 maxlabel = maxcheckbox = 0;
1681 winheight = height/2;
1683 for (i = fe->cfg; i->type != C_END; i++) {
1688 * Both these control types have a label filling only
1689 * the left-hand column of the box.
1691 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
1694 winheight += height * 3 / 2 + (height / 2);
1699 * Checkboxes take up the whole of the box width.
1701 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
1702 maxcheckbox < size.cx)
1703 maxcheckbox = size.cx;
1704 winheight += height + (height / 2);
1709 winheight += height + height * 7 / 4; /* OK / Cancel buttons */
1712 col1r = col1l + maxlabel;
1713 col2l = col1r + 2*width;
1714 col2r = col2l + 30*width;
1715 if (col2r < col1l+2*height+maxcheckbox)
1716 col2r = col1l+2*height+maxcheckbox;
1717 winwidth = col2r + 2*width;
1719 SelectObject(hdc, oldfont);
1720 ReleaseDC(fe->hwnd, hdc);
1723 * Create the dialog, now that we know its size.
1730 r.bottom = winheight;
1732 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
1733 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1734 WS_CAPTION | WS_SYSMENU*/) &~
1735 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
1739 * Centre the dialog on its parent window.
1743 GetWindowRect(fe->hwnd, &r2);
1744 r.left = (r2.left + r2.right - r.right) / 2;
1745 r.top = (r2.top + r2.bottom - r.bottom) / 2;
1749 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title,
1750 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1751 WS_CAPTION | WS_SYSMENU,
1753 r.right-r.left, r.bottom-r.top,
1754 fe->hwnd, NULL, fe->inst, NULL);
1758 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
1760 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
1761 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc);
1764 * Count the controls so we can allocate cfgaux.
1766 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
1768 fe->cfgaux = snewn(nctrls, struct cfg_aux);
1772 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
1776 * Edit box with a label beside it.
1778 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
1779 "Static", 0, 0, i->name, id++);
1780 ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2,
1781 "EDIT", WS_TABSTOP | ES_AUTOHSCROLL,
1782 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
1783 SetWindowText(ctl, i->sval);
1791 mkctrl(fe, col1l, col2r, y, y+height, "BUTTON",
1792 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
1793 0, i->name, (j->ctlid = id++));
1794 CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0));
1800 * Drop-down list with a label beside it.
1802 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
1803 "STATIC", 0, 0, i->name, id++);
1804 ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2,
1805 "COMBOBOX", WS_TABSTOP |
1806 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
1807 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
1809 char c, *p, *q, *str;
1811 SendMessage(ctl, CB_RESETCONTENT, 0, 0);
1816 while (*q && *q != c) q++;
1817 str = snewn(q-p+1, char);
1818 strncpy(str, p, q-p);
1820 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str);
1827 SendMessage(ctl, CB_SETCURSEL, i->ival, 0);
1833 assert(y < winheight);
1837 y += height/2; /* extra space before OK and Cancel */
1838 mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON",
1839 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
1841 mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON",
1842 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL);
1844 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
1846 EnableWindow(fe->hwnd, FALSE);
1847 ShowWindow(fe->cfgbox, SW_NORMAL);
1848 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
1849 if (!IsDialogMessage(fe->cfgbox, &msg))
1850 DispatchMessage(&msg);
1854 EnableWindow(fe->hwnd, TRUE);
1855 SetForegroundWindow(fe->hwnd);
1856 DestroyWindow(fe->cfgbox);
1857 DeleteObject(fe->cfgfont);
1862 return (fe->dlg_done == 2);
1865 static void new_game_size(frontend *fe)
1871 get_max_puzzle_size(fe, &x, &y);
1872 midend_size(fe->me, &x, &y, FALSE);
1877 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1879 if (fe->statusbar != NULL) {
1880 GetWindowRect(fe->statusbar, &sr);
1882 sr.left = sr.right = sr.top = sr.bottom = 0;
1884 SetWindowPos(fe->hwnd, NULL, 0, 0,
1886 r.bottom - r.top + sr.bottom - sr.top,
1887 SWP_NOMOVE | SWP_NOZORDER);
1889 check_window_size(fe, &x, &y);
1891 if (fe->statusbar != NULL)
1892 SetWindowPos(fe->statusbar, NULL, 0, y, x,
1893 sr.bottom - sr.top, SWP_NOZORDER);
1895 if (fe->bitmap) DeleteObject(fe->bitmap);
1897 hdc = GetDC(fe->hwnd);
1898 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
1899 ReleaseDC(fe->hwnd, hdc);
1901 midend_redraw(fe->me);
1904 static void new_game_type(frontend *fe)
1906 midend_new_game(fe->me);
1910 static int is_alt_pressed(void)
1913 int r = GetKeyboardState(keystate);
1916 if (keystate[VK_MENU] & 0x80)
1918 if (keystate[VK_RMENU] & 0x80)
1923 static void savefile_write(void *wctx, void *buf, int len)
1925 FILE *fp = (FILE *)wctx;
1926 fwrite(buf, 1, len, fp);
1929 static int savefile_read(void *wctx, void *buf, int len)
1931 FILE *fp = (FILE *)wctx;
1934 ret = fread(buf, 1, len, fp);
1935 return (ret == len);
1938 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
1939 WPARAM wParam, LPARAM lParam)
1941 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1946 DestroyWindow(hwnd);
1949 cmd = wParam & ~0xF; /* low 4 bits reserved to Windows */
1952 if (!midend_process_key(fe->me, 0, 0, 'n'))
1956 midend_restart_game(fe->me);
1959 if (!midend_process_key(fe->me, 0, 0, 'u'))
1963 if (!midend_process_key(fe->me, 0, 0, '\x12'))
1968 char *text = midend_text_format(fe->me);
1970 write_clip(hwnd, text);
1972 MessageBeep(MB_ICONWARNING);
1978 char *msg = midend_solve(fe->me);
1980 MessageBox(hwnd, msg, "Unable to solve",
1981 MB_ICONERROR | MB_OK);
1985 if (!midend_process_key(fe->me, 0, 0, 'q'))
1989 if (get_config(fe, CFG_SETTINGS))
1993 if (get_config(fe, CFG_SEED))
1997 if (get_config(fe, CFG_DESC))
2001 if (get_config(fe, CFG_PRINT))
2011 char filename[FILENAME_MAX];
2014 memset(&of, 0, sizeof(of));
2015 of.hwndOwner = hwnd;
2016 of.lpstrFilter = "All Files (*.*)\0*\0\0\0";
2017 of.lpstrCustomFilter = NULL;
2018 of.nFilterIndex = 1;
2019 of.lpstrFile = filename;
2021 of.nMaxFile = lenof(filename);
2022 of.lpstrFileTitle = NULL;
2023 of.lpstrTitle = (cmd == IDM_SAVE ?
2024 "Enter name of game file to save" :
2025 "Enter name of saved game file to load");
2027 #ifdef OPENFILENAME_SIZE_VERSION_400
2028 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
2030 of.lStructSize = sizeof(of);
2032 of.lpstrInitialDir = NULL;
2034 if (cmd == IDM_SAVE)
2035 ret = GetSaveFileName(&of);
2037 ret = GetOpenFileName(&of);
2040 if (cmd == IDM_SAVE) {
2043 if ((fp = fopen(filename, "r")) != NULL) {
2044 char buf[256 + FILENAME_MAX];
2048 sprintf(buf, "Are you sure you want to overwrite"
2049 " the file \"%.*s\"?",
2050 FILENAME_MAX, filename);
2051 if (MessageBox(hwnd, buf, "Question",
2052 MB_YESNO | MB_ICONQUESTION)
2057 fp = fopen(filename, "w");
2060 MessageBox(hwnd, "Unable to open save file",
2061 "Error", MB_ICONERROR | MB_OK);
2065 midend_serialise(fe->me, savefile_write, fp);
2069 FILE *fp = fopen(filename, "r");
2073 MessageBox(hwnd, "Unable to open saved game file",
2074 "Error", MB_ICONERROR | MB_OK);
2078 err = midend_deserialise(fe->me, savefile_read, fp);
2083 MessageBox(hwnd, err, "Error", MB_ICONERROR|MB_OK);
2094 assert(fe->help_path);
2095 WinHelp(hwnd, fe->help_path,
2096 fe->help_has_contents ? HELP_FINDER : HELP_CONTENTS, 0);
2099 assert(fe->help_path);
2100 assert(thegame.winhelp_topic);
2102 char *cmd = snewn(10+strlen(thegame.winhelp_topic), char);
2103 sprintf(cmd, "JI(`',`%s')", thegame.winhelp_topic);
2104 WinHelp(hwnd, fe->help_path, HELP_COMMAND, (DWORD)cmd);
2110 int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10;
2112 if (p >= 0 && p < fe->npresets) {
2113 midend_set_params(fe->me, fe->presets[p]);
2129 hdc = BeginPaint(hwnd, &p);
2130 hdc2 = CreateCompatibleDC(hdc);
2131 prevbm = SelectObject(hdc2, fe->bitmap);
2133 p.rcPaint.left, p.rcPaint.top,
2134 p.rcPaint.right - p.rcPaint.left,
2135 p.rcPaint.bottom - p.rcPaint.top,
2137 p.rcPaint.left, p.rcPaint.top,
2139 SelectObject(hdc2, prevbm);
2148 int r = GetKeyboardState(keystate);
2149 int shift = (r && (keystate[VK_SHIFT] & 0x80)) ? MOD_SHFT : 0;
2150 int ctrl = (r && (keystate[VK_CONTROL] & 0x80)) ? MOD_CTRL : 0;
2154 if (!(lParam & 0x01000000))
2155 key = MOD_NUM_KEYPAD | '4';
2157 key = shift | ctrl | CURSOR_LEFT;
2160 if (!(lParam & 0x01000000))
2161 key = MOD_NUM_KEYPAD | '6';
2163 key = shift | ctrl | CURSOR_RIGHT;
2166 if (!(lParam & 0x01000000))
2167 key = MOD_NUM_KEYPAD | '8';
2169 key = shift | ctrl | CURSOR_UP;
2172 if (!(lParam & 0x01000000))
2173 key = MOD_NUM_KEYPAD | '2';
2175 key = shift | ctrl | CURSOR_DOWN;
2178 * Diagonal keys on the numeric keypad.
2181 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '9';
2184 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '3';
2187 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '7';
2190 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '1';
2193 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '0';
2196 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '5';
2199 * Numeric keypad keys with Num Lock on.
2201 case VK_NUMPAD4: key = MOD_NUM_KEYPAD | '4'; break;
2202 case VK_NUMPAD6: key = MOD_NUM_KEYPAD | '6'; break;
2203 case VK_NUMPAD8: key = MOD_NUM_KEYPAD | '8'; break;
2204 case VK_NUMPAD2: key = MOD_NUM_KEYPAD | '2'; break;
2205 case VK_NUMPAD5: key = MOD_NUM_KEYPAD | '5'; break;
2206 case VK_NUMPAD9: key = MOD_NUM_KEYPAD | '9'; break;
2207 case VK_NUMPAD3: key = MOD_NUM_KEYPAD | '3'; break;
2208 case VK_NUMPAD7: key = MOD_NUM_KEYPAD | '7'; break;
2209 case VK_NUMPAD1: key = MOD_NUM_KEYPAD | '1'; break;
2210 case VK_NUMPAD0: key = MOD_NUM_KEYPAD | '0'; break;
2214 if (!midend_process_key(fe->me, 0, 0, key))
2219 m.message = WM_KEYDOWN;
2221 m.lParam = lParam & 0xdfff;
2222 TranslateMessage(&m);
2226 case WM_LBUTTONDOWN:
2227 case WM_RBUTTONDOWN:
2228 case WM_MBUTTONDOWN:
2233 * Shift-clicks count as middle-clicks, since otherwise
2234 * two-button Windows users won't have any kind of
2235 * middle click to use.
2237 if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT))
2238 button = MIDDLE_BUTTON;
2239 else if (message == WM_RBUTTONDOWN || is_alt_pressed())
2240 button = RIGHT_BUTTON;
2242 button = LEFT_BUTTON;
2244 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
2245 (signed short)HIWORD(lParam), button))
2258 * Shift-clicks count as middle-clicks, since otherwise
2259 * two-button Windows users won't have any kind of
2260 * middle click to use.
2262 if (message == WM_MBUTTONUP || (wParam & MK_SHIFT))
2263 button = MIDDLE_RELEASE;
2264 else if (message == WM_RBUTTONUP || is_alt_pressed())
2265 button = RIGHT_RELEASE;
2267 button = LEFT_RELEASE;
2269 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
2270 (signed short)HIWORD(lParam), button))
2280 if (wParam & (MK_MBUTTON | MK_SHIFT))
2281 button = MIDDLE_DRAG;
2282 else if (wParam & MK_RBUTTON || is_alt_pressed())
2283 button = RIGHT_DRAG;
2287 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
2288 (signed short)HIWORD(lParam), button))
2293 if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam))
2298 DWORD now = GetTickCount();
2299 float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F;
2300 midend_timer(fe->me, elapsed);
2301 fe->timer_last_tickcount = now;
2306 return DefWindowProc(hwnd, message, wParam, lParam);
2309 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
2314 InitCommonControls();
2320 wndclass.lpfnWndProc = WndProc;
2321 wndclass.cbClsExtra = 0;
2322 wndclass.cbWndExtra = 0;
2323 wndclass.hInstance = inst;
2324 wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION);
2325 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
2326 wndclass.hbrBackground = NULL;
2327 wndclass.lpszMenuName = NULL;
2328 wndclass.lpszClassName = thegame.name;
2330 RegisterClass(&wndclass);
2333 while (*cmdline && isspace((unsigned char)*cmdline))
2336 if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) {
2338 sprintf(buf, "%.100s Error", thegame.name);
2339 MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
2343 while (GetMessage(&msg, NULL, 0, 0)) {
2344 DispatchMessage(&msg);