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;
125 enum { DRAWING, PRINTING, NOTHING } drawstatus;
127 int printcount, printw, printh, printsolns, printcurr, printcolour;
129 int printoffsetx, printoffsety;
130 float printpixelscale;
136 void fatal(char *fmt, ...)
142 vsprintf(buf, fmt, ap);
145 MessageBox(NULL, buf, "Fatal error", MB_ICONEXCLAMATION | MB_OK);
150 char *geterrstr(void)
153 DWORD dw = GetLastError();
157 FORMAT_MESSAGE_ALLOCATE_BUFFER |
158 FORMAT_MESSAGE_FROM_SYSTEM,
161 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
165 ret = dupstr(lpMsgBuf);
172 void get_random_seed(void **randseed, int *randseedsize)
174 time_t *tp = snew(time_t);
176 *randseed = (void *)tp;
177 *randseedsize = sizeof(time_t);
180 static void win_status_bar(void *handle, char *text)
182 frontend *fe = (frontend *)handle;
185 rewritten = midend_rewrite_statusbar(fe->me, text);
186 if (!fe->laststatus || strcmp(rewritten, fe->laststatus)) {
187 SetWindowText(fe->statusbar, rewritten);
188 sfree(fe->laststatus);
189 fe->laststatus = rewritten;
195 static blitter *win_blitter_new(void *handle, int w, int h)
197 blitter *bl = snew(blitter);
199 memset(bl, 0, sizeof(blitter));
207 static void win_blitter_free(void *handle, blitter *bl)
209 if (bl->bitmap) DeleteObject(bl->bitmap);
213 static void blitter_mkbitmap(frontend *fe, blitter *bl)
215 HDC hdc = GetDC(fe->hwnd);
216 bl->bitmap = CreateCompatibleBitmap(hdc, bl->w, bl->h);
217 ReleaseDC(fe->hwnd, hdc);
220 /* BitBlt(dstDC, dstX, dstY, dstW, dstH, srcDC, srcX, srcY, dType) */
222 static void win_blitter_save(void *handle, blitter *bl, int x, int y)
224 frontend *fe = (frontend *)handle;
225 HDC hdc_win, hdc_blit;
228 assert(fe->drawstatus == DRAWING);
230 if (!bl->bitmap) blitter_mkbitmap(fe, bl);
232 bl->x = x; bl->y = y;
234 hdc_win = GetDC(fe->hwnd);
235 hdc_blit = CreateCompatibleDC(hdc_win);
236 if (!hdc_blit) fatal("hdc_blit failed: 0x%x", GetLastError());
238 prev_blit = SelectObject(hdc_blit, bl->bitmap);
239 if (prev_blit == NULL || prev_blit == HGDI_ERROR)
240 fatal("SelectObject for hdc_main failed: 0x%x", GetLastError());
242 if (!BitBlt(hdc_blit, 0, 0, bl->w, bl->h,
243 fe->hdc, x, y, SRCCOPY))
244 fatal("BitBlt failed: 0x%x", GetLastError());
246 SelectObject(hdc_blit, prev_blit);
248 ReleaseDC(fe->hwnd, hdc_win);
251 static void win_blitter_load(void *handle, blitter *bl, int x, int y)
253 frontend *fe = (frontend *)handle;
254 HDC hdc_win, hdc_blit;
257 assert(fe->drawstatus == DRAWING);
259 assert(bl->bitmap); /* we should always have saved before loading */
261 if (x == BLITTER_FROMSAVED) x = bl->x;
262 if (y == BLITTER_FROMSAVED) y = bl->y;
264 hdc_win = GetDC(fe->hwnd);
265 hdc_blit = CreateCompatibleDC(hdc_win);
267 prev_blit = SelectObject(hdc_blit, bl->bitmap);
269 BitBlt(fe->hdc, x, y, bl->w, bl->h,
270 hdc_blit, 0, 0, SRCCOPY);
272 SelectObject(hdc_blit, prev_blit);
274 ReleaseDC(fe->hwnd, hdc_win);
277 void frontend_default_colour(frontend *fe, float *output)
279 DWORD c = GetSysColor(COLOR_MENU); /* ick */
281 output[0] = (float)(GetRValue(c) / 255.0);
282 output[1] = (float)(GetGValue(c) / 255.0);
283 output[2] = (float)(GetBValue(c) / 255.0);
286 static POINT win_transform_point(frontend *fe, int x, int y)
290 assert(fe->drawstatus != NOTHING);
292 if (fe->drawstatus == PRINTING) {
293 ret.x = (int)(fe->printoffsetx + fe->printpixelscale * x);
294 ret.y = (int)(fe->printoffsety + fe->printpixelscale * y);
303 static void win_text_colour(frontend *fe, int colour)
305 assert(fe->drawstatus != NOTHING);
307 if (fe->drawstatus == PRINTING) {
310 print_get_colour(fe->dr, colour, &hatch, &r, &g, &b);
312 SetTextColor(fe->hdc, RGB(r * 255, g * 255, b * 255));
314 SetTextColor(fe->hdc,
315 hatch == HATCH_CLEAR ? RGB(255,255,255) : RGB(0,0,0));
317 SetTextColor(fe->hdc, fe->colours[colour]);
321 static void win_set_brush(frontend *fe, int colour)
324 assert(fe->drawstatus != NOTHING);
326 if (fe->drawstatus == PRINTING) {
329 print_get_colour(fe->dr, colour, &hatch, &r, &g, &b);
332 br = CreateSolidBrush(RGB(r * 255, g * 255, b * 255));
333 else if (hatch == HATCH_SOLID)
334 br = CreateSolidBrush(RGB(0,0,0));
335 else if (hatch == HATCH_CLEAR)
336 br = CreateSolidBrush(RGB(255,255,255));
338 br = CreateHatchBrush(hatch == HATCH_BACKSLASH ? HS_FDIAGONAL :
339 hatch == HATCH_SLASH ? HS_BDIAGONAL :
340 hatch == HATCH_HORIZ ? HS_HORIZONTAL :
341 hatch == HATCH_VERT ? HS_VERTICAL :
342 hatch == HATCH_PLUS ? HS_CROSS :
343 /* hatch == HATCH_X ? */ HS_DIAGCROSS,
346 br = fe->brushes[colour];
348 fe->oldbr = SelectObject(fe->hdc, br);
351 static void win_reset_brush(frontend *fe)
355 assert(fe->drawstatus != NOTHING);
357 br = SelectObject(fe->hdc, fe->oldbr);
358 if (fe->drawstatus == PRINTING)
362 static void win_set_pen(frontend *fe, int colour, int thin)
365 assert(fe->drawstatus != NOTHING);
367 if (fe->drawstatus == PRINTING) {
370 int width = thin ? 0 : fe->linewidth;
372 print_get_colour(fe->dr, colour, &hatch, &r, &g, &b);
374 pen = CreatePen(PS_SOLID, width,
375 RGB(r * 255, g * 255, b * 255));
376 else if (hatch == HATCH_SOLID)
377 pen = CreatePen(PS_SOLID, width, RGB(0, 0, 0));
378 else if (hatch == HATCH_CLEAR)
379 pen = CreatePen(PS_SOLID, width, RGB(255,255,255));
381 assert(!"This shouldn't happen");
382 pen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
385 pen = fe->pens[colour];
387 fe->oldpen = SelectObject(fe->hdc, pen);
390 static void win_reset_pen(frontend *fe)
394 assert(fe->drawstatus != NOTHING);
396 pen = SelectObject(fe->hdc, fe->oldpen);
397 if (fe->drawstatus == PRINTING)
401 static void win_clip(void *handle, int x, int y, int w, int h)
403 frontend *fe = (frontend *)handle;
406 if (fe->drawstatus == NOTHING)
409 p = win_transform_point(fe, x, y);
410 q = win_transform_point(fe, x+w, y+h);
411 IntersectClipRect(fe->hdc, p.x, p.y, q.x, q.y);
414 static void win_unclip(void *handle)
416 frontend *fe = (frontend *)handle;
418 if (fe->drawstatus == NOTHING)
421 SelectClipRgn(fe->hdc, NULL);
424 static void win_draw_text(void *handle, int x, int y, int fonttype,
425 int fontsize, int align, int colour, char *text)
427 frontend *fe = (frontend *)handle;
431 if (fe->drawstatus == NOTHING)
434 if (fe->drawstatus == PRINTING)
435 fontsize = (int)(fontsize * fe->printpixelscale);
437 xy = win_transform_point(fe, x, y);
440 * Find or create the font.
442 for (i = fe->fontstart; i < fe->nfonts; i++)
443 if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
446 if (i == fe->nfonts) {
447 if (fe->fontsize <= fe->nfonts) {
448 fe->fontsize = fe->nfonts + 10;
449 fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
454 fe->fonts[i].type = fonttype;
455 fe->fonts[i].size = fontsize;
457 fe->fonts[i].font = CreateFont(-fontsize, 0, 0, 0,
458 fe->drawstatus == PRINTING ? 0 : FW_BOLD,
459 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
460 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
462 (fonttype == FONT_FIXED ?
463 FIXED_PITCH | FF_DONTCARE :
464 VARIABLE_PITCH | FF_SWISS),
469 * Position and draw the text.
476 oldfont = SelectObject(fe->hdc, fe->fonts[i].font);
477 if (GetTextMetrics(fe->hdc, &tm)) {
478 if (align & ALIGN_VCENTRE)
479 xy.y -= (tm.tmAscent+tm.tmDescent)/2;
483 if (GetTextExtentPoint32(fe->hdc, text, strlen(text), &size)) {
484 if (align & ALIGN_HCENTRE)
486 else if (align & ALIGN_HRIGHT)
489 SetBkMode(fe->hdc, TRANSPARENT);
490 win_text_colour(fe, colour);
491 TextOut(fe->hdc, xy.x, xy.y, text, strlen(text));
492 SelectObject(fe->hdc, oldfont);
496 static void win_draw_rect(void *handle, int x, int y, int w, int h, int colour)
498 frontend *fe = (frontend *)handle;
501 if (fe->drawstatus == NOTHING)
504 if (fe->drawstatus == DRAWING && w == 1 && h == 1) {
506 * Rectangle() appears to get uppity if asked to draw a 1x1
507 * rectangle, presumably on the grounds that that's beneath
508 * its dignity and you ought to be using SetPixel instead.
511 SetPixel(fe->hdc, x, y, fe->colours[colour]);
513 win_set_brush(fe, colour);
514 win_set_pen(fe, colour, TRUE);
515 p = win_transform_point(fe, x, y);
516 q = win_transform_point(fe, x+w, y+h);
517 Rectangle(fe->hdc, p.x, p.y, q.x, q.y);
523 static void win_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour)
525 frontend *fe = (frontend *)handle;
528 if (fe->drawstatus == NOTHING)
531 win_set_pen(fe, colour, FALSE);
532 p = win_transform_point(fe, x1, y1);
533 q = win_transform_point(fe, x2, y2);
534 MoveToEx(fe->hdc, p.x, p.y, NULL);
535 LineTo(fe->hdc, q.x, q.y);
536 if (fe->drawstatus == DRAWING)
537 SetPixel(fe->hdc, q.x, q.y, fe->colours[colour]);
541 static void win_draw_circle(void *handle, int cx, int cy, int radius,
542 int fillcolour, int outlinecolour)
544 frontend *fe = (frontend *)handle;
547 assert(outlinecolour >= 0);
549 if (fe->drawstatus == NOTHING)
552 if (fillcolour >= 0) {
553 win_set_brush(fe, fillcolour);
554 win_set_pen(fe, outlinecolour, FALSE);
555 p = win_transform_point(fe, cx - radius, cy - radius);
556 q = win_transform_point(fe, cx + radius, cy + radius);
557 Ellipse(fe->hdc, p.x, p.y, q.x+1, q.y+1);
561 win_set_pen(fe, outlinecolour, FALSE);
562 p = win_transform_point(fe, cx - radius, cy - radius);
563 q = win_transform_point(fe, cx + radius, cy + radius);
564 r = win_transform_point(fe, cx - radius, cy);
565 Arc(fe->hdc, p.x, p.y, q.x+1, q.y+1, r.x, r.y, r.x, r.y);
570 static void win_draw_polygon(void *handle, int *coords, int npoints,
571 int fillcolour, int outlinecolour)
573 frontend *fe = (frontend *)handle;
577 if (fe->drawstatus == NOTHING)
580 pts = snewn(npoints+1, POINT);
582 for (i = 0; i <= npoints; i++) {
583 int j = (i < npoints ? i : 0);
584 pts[i] = win_transform_point(fe, coords[j*2], coords[j*2+1]);
587 assert(outlinecolour >= 0);
589 if (fillcolour >= 0) {
590 win_set_brush(fe, fillcolour);
591 win_set_pen(fe, outlinecolour, FALSE);
592 Polygon(fe->hdc, pts, npoints);
596 win_set_pen(fe, outlinecolour, FALSE);
597 Polyline(fe->hdc, pts, npoints+1);
604 static void win_start_draw(void *handle)
606 frontend *fe = (frontend *)handle;
609 assert(fe->drawstatus == NOTHING);
611 hdc_win = GetDC(fe->hwnd);
612 fe->hdc = CreateCompatibleDC(hdc_win);
613 fe->prevbm = SelectObject(fe->hdc, fe->bitmap);
614 ReleaseDC(fe->hwnd, hdc_win);
616 SetMapMode(fe->hdc, MM_TEXT);
617 fe->drawstatus = DRAWING;
620 static void win_draw_update(void *handle, int x, int y, int w, int h)
622 frontend *fe = (frontend *)handle;
625 if (fe->drawstatus != DRAWING)
633 InvalidateRect(fe->hwnd, &r, FALSE);
636 static void win_end_draw(void *handle)
638 frontend *fe = (frontend *)handle;
639 assert(fe->drawstatus == DRAWING);
640 SelectObject(fe->hdc, fe->prevbm);
643 DeleteObject(fe->clip);
646 fe->drawstatus = NOTHING;
649 static void win_line_width(void *handle, float width)
651 frontend *fe = (frontend *)handle;
653 assert(fe->drawstatus != DRAWING);
654 if (fe->drawstatus == NOTHING)
657 fe->linewidth = (int)(width * fe->printpixelscale);
660 static void win_begin_doc(void *handle, int pages)
662 frontend *fe = (frontend *)handle;
664 assert(fe->drawstatus != DRAWING);
665 if (fe->drawstatus == NOTHING)
668 if (StartDoc(fe->hdc, &fe->di) <= 0) {
669 char *e = geterrstr();
670 MessageBox(fe->hwnd, e, "Error starting to print",
671 MB_ICONERROR | MB_OK);
673 fe->drawstatus = NOTHING;
677 * Push a marker on the font stack so that we won't use the
678 * same fonts for printing and drawing. (This is because
679 * drawing seems to look generally better in bold, but printing
680 * is better not in bold.)
682 fe->fontstart = fe->nfonts;
685 static void win_begin_page(void *handle, int number)
687 frontend *fe = (frontend *)handle;
689 assert(fe->drawstatus != DRAWING);
690 if (fe->drawstatus == NOTHING)
693 if (StartPage(fe->hdc) <= 0) {
694 char *e = geterrstr();
695 MessageBox(fe->hwnd, e, "Error starting a page",
696 MB_ICONERROR | MB_OK);
698 fe->drawstatus = NOTHING;
702 static void win_begin_puzzle(void *handle, float xm, float xc,
703 float ym, float yc, int pw, int ph, float wmm)
705 frontend *fe = (frontend *)handle;
706 int ppw, pph, pox, poy;
707 float mmpw, mmph, mmox, mmoy;
710 assert(fe->drawstatus != DRAWING);
711 if (fe->drawstatus == NOTHING)
714 ppw = GetDeviceCaps(fe->hdc, HORZRES);
715 pph = GetDeviceCaps(fe->hdc, VERTRES);
716 mmpw = (float)GetDeviceCaps(fe->hdc, HORZSIZE);
717 mmph = (float)GetDeviceCaps(fe->hdc, VERTSIZE);
720 * Compute the puzzle's position on the logical page.
722 mmox = xm * mmpw + xc;
723 mmoy = ym * mmph + yc;
726 * Work out what that comes to in pixels.
728 pox = (int)(mmox * (float)ppw / mmpw);
729 poy = (int)(mmoy * (float)ppw / mmpw);
732 * And determine the scale.
734 * I need a scale such that the maximum puzzle-coordinate
735 * extent of the rectangle (pw * scale) is equal to the pixel
736 * equivalent of the puzzle's millimetre width (wmm * ppw /
739 scale = (wmm * ppw) / (mmpw * pw);
742 * Now store pox, poy and scale for use in the main drawing
745 fe->printoffsetx = pox;
746 fe->printoffsety = poy;
747 fe->printpixelscale = scale;
752 static void win_end_puzzle(void *handle)
754 /* Nothing needs to be done here. */
757 static void win_end_page(void *handle, int number)
759 frontend *fe = (frontend *)handle;
761 assert(fe->drawstatus != DRAWING);
763 if (fe->drawstatus == NOTHING)
766 if (EndPage(fe->hdc) <= 0) {
767 char *e = geterrstr();
768 MessageBox(fe->hwnd, e, "Error finishing a page",
769 MB_ICONERROR | MB_OK);
771 fe->drawstatus = NOTHING;
775 static void win_end_doc(void *handle)
777 frontend *fe = (frontend *)handle;
779 assert(fe->drawstatus != DRAWING);
782 * Free all the fonts created since we began printing.
784 while (fe->nfonts > fe->fontstart) {
786 DeleteObject(fe->fonts[fe->nfonts].font);
791 * The MSDN web site sample code doesn't bother to call EndDoc
792 * if an error occurs half way through printing. I expect doing
793 * so would cause the erroneous document to actually be
794 * printed, or something equally undesirable.
796 if (fe->drawstatus == NOTHING)
799 if (EndDoc(fe->hdc) <= 0) {
800 char *e = geterrstr();
801 MessageBox(fe->hwnd, e, "Error finishing printing",
802 MB_ICONERROR | MB_OK);
804 fe->drawstatus = NOTHING;
808 const struct drawing_api win_drawing = {
833 void print(frontend *fe)
838 midend *nme = NULL; /* non-interactive midend for bulk puzzle generation */
843 * Create our document structure and fill it up with puzzles.
845 doc = document_new(fe->printw, fe->printh, fe->printscale / 100.0F);
846 for (i = 0; i < fe->printcount; i++) {
847 if (i == 0 && fe->printcurr) {
848 err = midend_print_puzzle(fe->me, doc, fe->printsolns);
853 nme = midend_new(NULL, &thegame, NULL, NULL);
856 * Set the non-interactive mid-end to have the same
857 * parameters as the standard one.
859 params = midend_get_params(fe->me);
860 midend_set_params(nme, params);
861 thegame.free_params(params);
864 midend_new_game(nme);
865 err = midend_print_puzzle(nme, doc, fe->printsolns);
874 MessageBox(fe->hwnd, err, "Error preparing puzzles for printing",
875 MB_ICONERROR | MB_OK);
880 memset(&pd, 0, sizeof(pd));
881 pd.lStructSize = sizeof(pd);
882 pd.hwndOwner = fe->hwnd;
885 pd.Flags = PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC |
886 PD_NOPAGENUMS | PD_NOSELECTION;
888 pd.nFromPage = pd.nToPage = 0xFFFF;
889 pd.nMinPage = pd.nMaxPage = 1;
891 if (!PrintDlg(&pd)) {
897 * Now pd.hDC is a device context for the printer.
901 * FIXME: IWBNI we put up an Abort box here.
904 memset(&fe->di, 0, sizeof(fe->di));
905 fe->di.cbSize = sizeof(fe->di);
906 sprintf(doctitle, "Printed puzzles from %s (from Simon Tatham's"
907 " Portable Puzzle Collection)", thegame.name);
908 fe->di.lpszDocName = doctitle;
909 fe->di.lpszOutput = NULL;
910 fe->di.lpszDatatype = NULL;
913 fe->drawstatus = PRINTING;
916 fe->dr = drawing_init(&win_drawing, fe);
917 document_print(doc, fe->dr);
918 drawing_free(fe->dr);
921 fe->drawstatus = NOTHING;
927 void deactivate_timer(frontend *fe)
930 return; /* for non-interactive midend */
931 if (fe->hwnd) KillTimer(fe->hwnd, fe->timer);
935 void activate_timer(frontend *fe)
938 return; /* for non-interactive midend */
940 fe->timer = SetTimer(fe->hwnd, fe->timer, 20, NULL);
941 fe->timer_last_tickcount = GetTickCount();
945 void write_clip(HWND hwnd, char *data)
953 * Windows expects CRLF in the clipboard, so we must convert
954 * any \n that has come out of the puzzle backend.
957 for (i = 0; data[i]; i++) {
962 data2 = snewn(len+1, char);
964 for (i = 0; data[i]; i++) {
967 data2[j++] = data[i];
972 clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
975 lock = GlobalLock(clipdata);
978 memcpy(lock, data2, len);
979 ((unsigned char *) lock)[len] = 0;
980 GlobalUnlock(clipdata);
982 if (OpenClipboard(hwnd)) {
984 SetClipboardData(CF_TEXT, clipdata);
987 GlobalFree(clipdata);
993 * See if we can find a help file.
995 static void find_help_file(frontend *fe)
997 char b[2048], *p, *q, *r;
999 if (!fe->help_path) {
1000 GetModuleFileName(NULL, b, sizeof(b) - 1);
1002 p = strrchr(b, '\\');
1003 if (p && p >= r) r = p+1;
1004 q = strrchr(b, ':');
1005 if (q && q >= r) r = q+1;
1006 strcpy(r, HELP_FILE_NAME);
1007 if ( (fp = fopen(b, "r")) != NULL) {
1008 fe->help_path = dupstr(b);
1011 fe->help_path = NULL;
1012 strcpy(r, HELP_CNT_NAME);
1013 if ( (fp = fopen(b, "r")) != NULL) {
1014 fe->help_has_contents = TRUE;
1017 fe->help_has_contents = FALSE;
1021 static void check_window_size(frontend *fe, int *px, int *py)
1026 if (fe->statusbar) {
1028 GetWindowRect(fe->statusbar, &sr);
1029 sy = sr.bottom - sr.top;
1035 * See if we actually got the window size we wanted, and adjust
1036 * the puzzle size if not.
1038 GetClientRect(fe->hwnd, &r);
1039 x = r.right - r.left;
1040 y = r.bottom - r.top - sy;
1041 midend_size(fe->me, &x, &y, FALSE);
1042 if (x != r.right - r.left || y != r.bottom - r.top) {
1044 * Resize the window, now we know what size we _really_
1050 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1051 SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left, r.bottom - r.top,
1052 SWP_NOMOVE | SWP_NOZORDER);
1055 if (fe->statusbar) {
1056 GetClientRect(fe->hwnd, &r);
1057 SetWindowPos(fe->statusbar, NULL, 0, r.bottom-r.top-sy, r.right-r.left,
1065 static void get_max_puzzle_size(frontend *fe, int *x, int *y)
1069 if (SystemParametersInfo(SPI_GETWORKAREA, 0, &sr, FALSE)) {
1070 *x = sr.right - sr.left;
1071 *y = sr.bottom - sr.top;
1076 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1077 *x -= r.right - r.left - 100;
1078 *y -= r.bottom - r.top - 100;
1083 if (fe->statusbar != NULL) {
1084 GetWindowRect(fe->statusbar, &sr);
1085 *y -= sr.bottom - sr.top;
1089 static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
1096 fe = snew(frontend);
1098 fe->me = midend_new(fe, &thegame, &win_drawing, fe);
1101 *error = midend_game_id(fe->me, game_id);
1103 midend_free(fe->me);
1109 fe->help_path = NULL;
1117 fe->drawstatus = NOTHING;
1121 midend_new_game(fe->me);
1124 fe->nfonts = fe->fontsize = 0;
1126 fe->laststatus = NULL;
1132 colours = midend_colours(fe->me, &ncolours);
1134 fe->colours = snewn(ncolours, COLORREF);
1135 fe->brushes = snewn(ncolours, HBRUSH);
1136 fe->pens = snewn(ncolours, HPEN);
1138 for (i = 0; i < ncolours; i++) {
1139 fe->colours[i] = RGB(255 * colours[i*3+0],
1140 255 * colours[i*3+1],
1141 255 * colours[i*3+2]);
1142 fe->brushes[i] = CreateSolidBrush(fe->colours[i]);
1143 fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]);
1148 if (midend_wants_statusbar(fe->me)) {
1149 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh",
1150 WS_CHILD | WS_VISIBLE,
1151 0, 0, 0, 0, /* status bar does these */
1152 NULL, NULL, inst, NULL);
1155 get_max_puzzle_size(fe, &x, &y);
1156 midend_size(fe->me, &x, &y, FALSE);
1161 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1163 fe->hwnd = CreateWindowEx(0, thegame.name, thegame.name,
1164 WS_OVERLAPPEDWINDOW &~
1165 (WS_THICKFRAME | WS_MAXIMIZEBOX),
1166 CW_USEDEFAULT, CW_USEDEFAULT,
1167 r.right - r.left, r.bottom - r.top,
1168 NULL, NULL, inst, NULL);
1170 if (midend_wants_statusbar(fe->me)) {
1172 DestroyWindow(fe->statusbar);
1173 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh",
1174 WS_CHILD | WS_VISIBLE,
1175 0, 0, 0, 0, /* status bar does these */
1176 fe->hwnd, NULL, inst, NULL);
1178 * Now resize the window to take account of the status bar.
1180 GetWindowRect(fe->statusbar, &sr);
1181 GetWindowRect(fe->hwnd, &r);
1182 SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left,
1183 r.bottom - r.top + sr.bottom - sr.top,
1184 SWP_NOMOVE | SWP_NOZORDER);
1186 fe->statusbar = NULL;
1190 HMENU bar = CreateMenu();
1191 HMENU menu = CreateMenu();
1193 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Game");
1194 AppendMenu(menu, MF_ENABLED, IDM_NEW, "New");
1195 AppendMenu(menu, MF_ENABLED, IDM_RESTART, "Restart");
1196 AppendMenu(menu, MF_ENABLED, IDM_DESC, "Specific...");
1197 AppendMenu(menu, MF_ENABLED, IDM_SEED, "Random Seed...");
1199 if ((fe->npresets = midend_num_presets(fe->me)) > 0 ||
1200 thegame.can_configure) {
1201 HMENU sub = CreateMenu();
1204 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "Type");
1206 fe->presets = snewn(fe->npresets, game_params *);
1208 for (i = 0; i < fe->npresets; i++) {
1211 midend_fetch_preset(fe->me, i, &name, &fe->presets[i]);
1214 * FIXME: we ought to go through and do something
1215 * with ampersands here.
1218 AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name);
1221 if (thegame.can_configure) {
1222 AppendMenu(sub, MF_ENABLED, IDM_CONFIG, "Custom...");
1226 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1227 AppendMenu(menu, MF_ENABLED, IDM_LOAD, "Load");
1228 AppendMenu(menu, MF_ENABLED, IDM_SAVE, "Save");
1229 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1230 if (thegame.can_print) {
1231 AppendMenu(menu, MF_ENABLED, IDM_PRINT, "Print");
1232 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1234 AppendMenu(menu, MF_ENABLED, IDM_UNDO, "Undo");
1235 AppendMenu(menu, MF_ENABLED, IDM_REDO, "Redo");
1236 if (thegame.can_format_as_text) {
1237 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1238 AppendMenu(menu, MF_ENABLED, IDM_COPY, "Copy");
1240 if (thegame.can_solve) {
1241 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1242 AppendMenu(menu, MF_ENABLED, IDM_SOLVE, "Solve");
1244 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1245 AppendMenu(menu, MF_ENABLED, IDM_QUIT, "Exit");
1246 menu = CreateMenu();
1247 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Help");
1248 AppendMenu(menu, MF_ENABLED, IDM_ABOUT, "About");
1249 if (fe->help_path) {
1250 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1251 AppendMenu(menu, MF_ENABLED, IDM_HELPC, "Contents");
1252 if (thegame.winhelp_topic) {
1254 assert(thegame.name);
1255 item = snewn(9+strlen(thegame.name), char); /*ick*/
1256 sprintf(item, "Help on %s", thegame.name);
1257 AppendMenu(menu, MF_ENABLED, IDM_GAMEHELP, item);
1261 SetMenu(fe->hwnd, bar);
1265 check_window_size(fe, &x, &y);
1267 hdc = GetDC(fe->hwnd);
1268 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
1269 ReleaseDC(fe->hwnd, hdc);
1271 SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe);
1273 ShowWindow(fe->hwnd, SW_NORMAL);
1274 SetForegroundWindow(fe->hwnd);
1276 midend_redraw(fe->me);
1281 static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg,
1282 WPARAM wParam, LPARAM lParam)
1284 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1291 if ((HIWORD(wParam) == BN_CLICKED ||
1292 HIWORD(wParam) == BN_DOUBLECLICKED) &&
1293 LOWORD(wParam) == IDOK)
1306 * Wrappers on midend_{get,set}_config, which extend the CFG_*
1307 * enumeration to add CFG_PRINT.
1309 static config_item *frontend_get_config(frontend *fe, int which,
1312 if (which < CFG_FRONTEND_SPECIFIC) {
1313 return midend_get_config(fe->me, which, wintitle);
1314 } else if (which == CFG_PRINT) {
1318 *wintitle = snewn(40 + strlen(thegame.name), char);
1319 sprintf(*wintitle, "%s print setup", thegame.name);
1321 ret = snewn(8, config_item);
1325 ret[i].name = "Number of puzzles to print";
1326 ret[i].type = C_STRING;
1327 ret[i].sval = dupstr("1");
1331 ret[i].name = "Number of puzzles across the page";
1332 ret[i].type = C_STRING;
1333 ret[i].sval = dupstr("1");
1337 ret[i].name = "Number of puzzles down the page";
1338 ret[i].type = C_STRING;
1339 ret[i].sval = dupstr("1");
1343 ret[i].name = "Percentage of standard size";
1344 ret[i].type = C_STRING;
1345 ret[i].sval = dupstr("100.0");
1349 ret[i].name = "Include currently shown puzzle";
1350 ret[i].type = C_BOOLEAN;
1355 ret[i].name = "Print solutions";
1356 ret[i].type = C_BOOLEAN;
1358 ret[i].ival = FALSE;
1361 if (thegame.can_print_in_colour) {
1362 ret[i].name = "Print in colour";
1363 ret[i].type = C_BOOLEAN;
1365 ret[i].ival = FALSE;
1370 ret[i].type = C_END;
1377 assert(!"We should never get here");
1382 static char *frontend_set_config(frontend *fe, int which, config_item *cfg)
1384 if (which < CFG_FRONTEND_SPECIFIC) {
1385 return midend_set_config(fe->me, which, cfg);
1386 } else if (which == CFG_PRINT) {
1387 if ((fe->printcount = atoi(cfg[0].sval)) <= 0)
1388 return "Number of puzzles to print should be at least one";
1389 if ((fe->printw = atoi(cfg[1].sval)) <= 0)
1390 return "Number of puzzles across the page should be at least one";
1391 if ((fe->printh = atoi(cfg[2].sval)) <= 0)
1392 return "Number of puzzles down the page should be at least one";
1393 if ((fe->printscale = (float)atof(cfg[3].sval)) <= 0)
1394 return "Print size should be positive";
1395 fe->printcurr = cfg[4].ival;
1396 fe->printsolns = cfg[5].ival;
1397 fe->printcolour = thegame.can_print_in_colour && cfg[6].ival;
1400 assert(!"We should never get here");
1401 return "Internal error";
1405 static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg,
1406 WPARAM wParam, LPARAM lParam)
1408 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1418 * OK and Cancel are special cases.
1420 if ((HIWORD(wParam) == BN_CLICKED ||
1421 HIWORD(wParam) == BN_DOUBLECLICKED) &&
1422 (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) {
1423 if (LOWORD(wParam) == IDOK) {
1424 char *err = frontend_set_config(fe, fe->cfg_which, fe->cfg);
1427 MessageBox(hwnd, err, "Validation error",
1428 MB_ICONERROR | MB_OK);
1439 * First find the control whose id this is.
1441 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
1442 if (j->ctlid == LOWORD(wParam))
1445 if (i->type == C_END)
1446 return 0; /* not our problem */
1448 if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) {
1450 GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer));
1451 buffer[lenof(buffer)-1] = '\0';
1453 i->sval = dupstr(buffer);
1454 } else if (i->type == C_BOOLEAN &&
1455 (HIWORD(wParam) == BN_CLICKED ||
1456 HIWORD(wParam) == BN_DOUBLECLICKED)) {
1457 i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid);
1458 } else if (i->type == C_CHOICES &&
1459 HIWORD(wParam) == CBN_SELCHANGE) {
1460 i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid,
1461 CB_GETCURSEL, 0, 0);
1474 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
1475 char *wclass, int wstyle,
1476 int exstyle, const char *wtext, int wid)
1479 ret = CreateWindowEx(exstyle, wclass, wtext,
1480 wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1,
1481 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
1482 SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0));
1486 static void about(frontend *fe)
1496 int winwidth, winheight, y;
1497 int height, width, maxwid;
1498 const char *strings[16];
1503 sprintf(titlebuf, "About %.250s", thegame.name);
1505 strings[nstrings++] = thegame.name;
1506 strings[nstrings++] = "from Simon Tatham's Portable Puzzle Collection";
1507 strings[nstrings++] = ver;
1509 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
1510 wc.lpfnWndProc = DefDlgProc;
1512 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
1513 wc.hInstance = fe->inst;
1515 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
1516 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
1517 wc.lpszMenuName = NULL;
1518 wc.lpszClassName = "GameAboutBox";
1521 hdc = GetDC(fe->hwnd);
1522 SetMapMode(hdc, MM_TEXT);
1524 fe->dlg_done = FALSE;
1526 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
1528 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
1529 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
1534 oldfont = SelectObject(hdc, fe->cfgfont);
1535 if (GetTextMetrics(hdc, &tm)) {
1536 height = tm.tmAscent + tm.tmDescent;
1537 width = tm.tmAveCharWidth;
1539 height = width = 30;
1543 * Figure out the layout of the About box by measuring the
1544 * length of each piece of text.
1547 winheight = height/2;
1549 for (i = 0; i < nstrings; i++) {
1550 if (GetTextExtentPoint32(hdc, strings[i], strlen(strings[i]), &size))
1551 lengths[i] = size.cx;
1553 lengths[i] = 0; /* *shrug* */
1554 if (maxwid < lengths[i])
1555 maxwid = lengths[i];
1556 winheight += height * 3 / 2 + (height / 2);
1559 winheight += height + height * 7 / 4; /* OK button */
1560 winwidth = maxwid + 4*width;
1562 SelectObject(hdc, oldfont);
1563 ReleaseDC(fe->hwnd, hdc);
1566 * Create the dialog, now that we know its size.
1573 r.bottom = winheight;
1575 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
1576 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1577 WS_CAPTION | WS_SYSMENU*/) &~
1578 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
1582 * Centre the dialog on its parent window.
1586 GetWindowRect(fe->hwnd, &r2);
1587 r.left = (r2.left + r2.right - r.right) / 2;
1588 r.top = (r2.top + r2.bottom - r.bottom) / 2;
1592 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, titlebuf,
1593 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1594 WS_CAPTION | WS_SYSMENU,
1596 r.right-r.left, r.bottom-r.top,
1597 fe->hwnd, NULL, fe->inst, NULL);
1600 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
1602 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
1603 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)AboutDlgProc);
1607 for (i = 0; i < nstrings; i++) {
1608 int border = width*2 + (maxwid - lengths[i]) / 2;
1609 mkctrl(fe, border, border+lengths[i], y+height*1/8, y+height*9/8,
1610 "Static", 0, 0, strings[i], id++);
1613 assert(y < winheight);
1617 y += height/2; /* extra space before OK */
1618 mkctrl(fe, width*2, maxwid+width*2, y, y+height*7/4, "BUTTON",
1619 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
1622 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
1624 EnableWindow(fe->hwnd, FALSE);
1625 ShowWindow(fe->cfgbox, SW_NORMAL);
1626 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
1627 if (!IsDialogMessage(fe->cfgbox, &msg))
1628 DispatchMessage(&msg);
1632 EnableWindow(fe->hwnd, TRUE);
1633 SetForegroundWindow(fe->hwnd);
1634 DestroyWindow(fe->cfgbox);
1635 DeleteObject(fe->cfgfont);
1638 static int get_config(frontend *fe, int which)
1651 int winwidth, winheight, col1l, col1r, col2l, col2r, y;
1652 int height, width, maxlabel, maxcheckbox;
1654 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
1655 wc.lpfnWndProc = DefDlgProc;
1657 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
1658 wc.hInstance = fe->inst;
1660 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
1661 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
1662 wc.lpszMenuName = NULL;
1663 wc.lpszClassName = "GameConfigBox";
1666 hdc = GetDC(fe->hwnd);
1667 SetMapMode(hdc, MM_TEXT);
1669 fe->dlg_done = FALSE;
1671 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
1673 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
1674 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
1679 oldfont = SelectObject(hdc, fe->cfgfont);
1680 if (GetTextMetrics(hdc, &tm)) {
1681 height = tm.tmAscent + tm.tmDescent;
1682 width = tm.tmAveCharWidth;
1684 height = width = 30;
1687 fe->cfg = frontend_get_config(fe, which, &title);
1688 fe->cfg_which = which;
1691 * Figure out the layout of the config box by measuring the
1692 * length of each piece of text.
1694 maxlabel = maxcheckbox = 0;
1695 winheight = height/2;
1697 for (i = fe->cfg; i->type != C_END; i++) {
1702 * Both these control types have a label filling only
1703 * the left-hand column of the box.
1705 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
1708 winheight += height * 3 / 2 + (height / 2);
1713 * Checkboxes take up the whole of the box width.
1715 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
1716 maxcheckbox < size.cx)
1717 maxcheckbox = size.cx;
1718 winheight += height + (height / 2);
1723 winheight += height + height * 7 / 4; /* OK / Cancel buttons */
1726 col1r = col1l + maxlabel;
1727 col2l = col1r + 2*width;
1728 col2r = col2l + 30*width;
1729 if (col2r < col1l+2*height+maxcheckbox)
1730 col2r = col1l+2*height+maxcheckbox;
1731 winwidth = col2r + 2*width;
1733 SelectObject(hdc, oldfont);
1734 ReleaseDC(fe->hwnd, hdc);
1737 * Create the dialog, now that we know its size.
1744 r.bottom = winheight;
1746 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
1747 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1748 WS_CAPTION | WS_SYSMENU*/) &~
1749 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
1753 * Centre the dialog on its parent window.
1757 GetWindowRect(fe->hwnd, &r2);
1758 r.left = (r2.left + r2.right - r.right) / 2;
1759 r.top = (r2.top + r2.bottom - r.bottom) / 2;
1763 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title,
1764 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1765 WS_CAPTION | WS_SYSMENU,
1767 r.right-r.left, r.bottom-r.top,
1768 fe->hwnd, NULL, fe->inst, NULL);
1772 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
1774 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
1775 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc);
1778 * Count the controls so we can allocate cfgaux.
1780 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
1782 fe->cfgaux = snewn(nctrls, struct cfg_aux);
1786 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
1790 * Edit box with a label beside it.
1792 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
1793 "Static", 0, 0, i->name, id++);
1794 ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2,
1795 "EDIT", WS_TABSTOP | ES_AUTOHSCROLL,
1796 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
1797 SetWindowText(ctl, i->sval);
1805 mkctrl(fe, col1l, col2r, y, y+height, "BUTTON",
1806 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
1807 0, i->name, (j->ctlid = id++));
1808 CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0));
1814 * Drop-down list with a label beside it.
1816 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
1817 "STATIC", 0, 0, i->name, id++);
1818 ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2,
1819 "COMBOBOX", WS_TABSTOP |
1820 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
1821 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
1823 char c, *p, *q, *str;
1825 SendMessage(ctl, CB_RESETCONTENT, 0, 0);
1830 while (*q && *q != c) q++;
1831 str = snewn(q-p+1, char);
1832 strncpy(str, p, q-p);
1834 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str);
1841 SendMessage(ctl, CB_SETCURSEL, i->ival, 0);
1847 assert(y < winheight);
1851 y += height/2; /* extra space before OK and Cancel */
1852 mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON",
1853 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
1855 mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON",
1856 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL);
1858 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
1860 EnableWindow(fe->hwnd, FALSE);
1861 ShowWindow(fe->cfgbox, SW_NORMAL);
1862 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
1863 if (!IsDialogMessage(fe->cfgbox, &msg))
1864 DispatchMessage(&msg);
1868 EnableWindow(fe->hwnd, TRUE);
1869 SetForegroundWindow(fe->hwnd);
1870 DestroyWindow(fe->cfgbox);
1871 DeleteObject(fe->cfgfont);
1876 return (fe->dlg_done == 2);
1879 static void new_game_size(frontend *fe)
1885 get_max_puzzle_size(fe, &x, &y);
1886 midend_size(fe->me, &x, &y, FALSE);
1891 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1893 if (fe->statusbar != NULL) {
1894 GetWindowRect(fe->statusbar, &sr);
1896 sr.left = sr.right = sr.top = sr.bottom = 0;
1898 SetWindowPos(fe->hwnd, NULL, 0, 0,
1900 r.bottom - r.top + sr.bottom - sr.top,
1901 SWP_NOMOVE | SWP_NOZORDER);
1903 check_window_size(fe, &x, &y);
1905 if (fe->statusbar != NULL)
1906 SetWindowPos(fe->statusbar, NULL, 0, y, x,
1907 sr.bottom - sr.top, SWP_NOZORDER);
1909 DeleteObject(fe->bitmap);
1911 hdc = GetDC(fe->hwnd);
1912 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
1913 ReleaseDC(fe->hwnd, hdc);
1915 midend_redraw(fe->me);
1918 static void new_game_type(frontend *fe)
1920 midend_new_game(fe->me);
1924 static int is_alt_pressed(void)
1927 int r = GetKeyboardState(keystate);
1930 if (keystate[VK_MENU] & 0x80)
1932 if (keystate[VK_RMENU] & 0x80)
1937 static void savefile_write(void *wctx, void *buf, int len)
1939 FILE *fp = (FILE *)wctx;
1940 fwrite(buf, 1, len, fp);
1943 static int savefile_read(void *wctx, void *buf, int len)
1945 FILE *fp = (FILE *)wctx;
1948 ret = fread(buf, 1, len, fp);
1949 return (ret == len);
1952 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
1953 WPARAM wParam, LPARAM lParam)
1955 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1960 DestroyWindow(hwnd);
1963 cmd = wParam & ~0xF; /* low 4 bits reserved to Windows */
1966 if (!midend_process_key(fe->me, 0, 0, 'n'))
1970 midend_restart_game(fe->me);
1973 if (!midend_process_key(fe->me, 0, 0, 'u'))
1977 if (!midend_process_key(fe->me, 0, 0, '\x12'))
1982 char *text = midend_text_format(fe->me);
1984 write_clip(hwnd, text);
1986 MessageBeep(MB_ICONWARNING);
1992 char *msg = midend_solve(fe->me);
1994 MessageBox(hwnd, msg, "Unable to solve",
1995 MB_ICONERROR | MB_OK);
1999 if (!midend_process_key(fe->me, 0, 0, 'q'))
2003 if (get_config(fe, CFG_SETTINGS))
2007 if (get_config(fe, CFG_SEED))
2011 if (get_config(fe, CFG_DESC))
2015 if (get_config(fe, CFG_PRINT))
2025 char filename[FILENAME_MAX];
2028 memset(&of, 0, sizeof(of));
2029 of.hwndOwner = hwnd;
2030 of.lpstrFilter = "All Files (*.*)\0*\0\0\0";
2031 of.lpstrCustomFilter = NULL;
2032 of.nFilterIndex = 1;
2033 of.lpstrFile = filename;
2035 of.nMaxFile = lenof(filename);
2036 of.lpstrFileTitle = NULL;
2037 of.lpstrTitle = (cmd == IDM_SAVE ?
2038 "Enter name of game file to save" :
2039 "Enter name of saved game file to load");
2041 #ifdef OPENFILENAME_SIZE_VERSION_400
2042 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
2044 of.lStructSize = sizeof(of);
2046 of.lpstrInitialDir = NULL;
2048 if (cmd == IDM_SAVE)
2049 ret = GetSaveFileName(&of);
2051 ret = GetOpenFileName(&of);
2054 if (cmd == IDM_SAVE) {
2057 if ((fp = fopen(filename, "r")) != NULL) {
2058 char buf[256 + FILENAME_MAX];
2062 sprintf(buf, "Are you sure you want to overwrite"
2063 " the file \"%.*s\"?",
2064 FILENAME_MAX, filename);
2065 if (MessageBox(hwnd, buf, "Question",
2066 MB_YESNO | MB_ICONQUESTION)
2071 fp = fopen(filename, "w");
2074 MessageBox(hwnd, "Unable to open save file",
2075 "Error", MB_ICONERROR | MB_OK);
2079 midend_serialise(fe->me, savefile_write, fp);
2083 FILE *fp = fopen(filename, "r");
2087 MessageBox(hwnd, "Unable to open saved game file",
2088 "Error", MB_ICONERROR | MB_OK);
2092 err = midend_deserialise(fe->me, savefile_read, fp);
2097 MessageBox(hwnd, err, "Error", MB_ICONERROR|MB_OK);
2108 assert(fe->help_path);
2109 WinHelp(hwnd, fe->help_path,
2110 fe->help_has_contents ? HELP_FINDER : HELP_CONTENTS, 0);
2113 assert(fe->help_path);
2114 assert(thegame.winhelp_topic);
2116 char *cmd = snewn(10+strlen(thegame.winhelp_topic), char);
2117 sprintf(cmd, "JI(`',`%s')", thegame.winhelp_topic);
2118 WinHelp(hwnd, fe->help_path, HELP_COMMAND, (DWORD)cmd);
2124 int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10;
2126 if (p >= 0 && p < fe->npresets) {
2127 midend_set_params(fe->me, fe->presets[p]);
2143 hdc = BeginPaint(hwnd, &p);
2144 hdc2 = CreateCompatibleDC(hdc);
2145 prevbm = SelectObject(hdc2, fe->bitmap);
2147 p.rcPaint.left, p.rcPaint.top,
2148 p.rcPaint.right - p.rcPaint.left,
2149 p.rcPaint.bottom - p.rcPaint.top,
2151 p.rcPaint.left, p.rcPaint.top,
2153 SelectObject(hdc2, prevbm);
2162 int r = GetKeyboardState(keystate);
2163 int shift = (r && (keystate[VK_SHIFT] & 0x80)) ? MOD_SHFT : 0;
2164 int ctrl = (r && (keystate[VK_CONTROL] & 0x80)) ? MOD_CTRL : 0;
2168 if (!(lParam & 0x01000000))
2169 key = MOD_NUM_KEYPAD | '4';
2171 key = shift | ctrl | CURSOR_LEFT;
2174 if (!(lParam & 0x01000000))
2175 key = MOD_NUM_KEYPAD | '6';
2177 key = shift | ctrl | CURSOR_RIGHT;
2180 if (!(lParam & 0x01000000))
2181 key = MOD_NUM_KEYPAD | '8';
2183 key = shift | ctrl | CURSOR_UP;
2186 if (!(lParam & 0x01000000))
2187 key = MOD_NUM_KEYPAD | '2';
2189 key = shift | ctrl | CURSOR_DOWN;
2192 * Diagonal keys on the numeric keypad.
2195 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '9';
2198 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '3';
2201 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '7';
2204 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '1';
2207 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '0';
2210 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '5';
2213 * Numeric keypad keys with Num Lock on.
2215 case VK_NUMPAD4: key = MOD_NUM_KEYPAD | '4'; break;
2216 case VK_NUMPAD6: key = MOD_NUM_KEYPAD | '6'; break;
2217 case VK_NUMPAD8: key = MOD_NUM_KEYPAD | '8'; break;
2218 case VK_NUMPAD2: key = MOD_NUM_KEYPAD | '2'; break;
2219 case VK_NUMPAD5: key = MOD_NUM_KEYPAD | '5'; break;
2220 case VK_NUMPAD9: key = MOD_NUM_KEYPAD | '9'; break;
2221 case VK_NUMPAD3: key = MOD_NUM_KEYPAD | '3'; break;
2222 case VK_NUMPAD7: key = MOD_NUM_KEYPAD | '7'; break;
2223 case VK_NUMPAD1: key = MOD_NUM_KEYPAD | '1'; break;
2224 case VK_NUMPAD0: key = MOD_NUM_KEYPAD | '0'; break;
2228 if (!midend_process_key(fe->me, 0, 0, key))
2233 m.message = WM_KEYDOWN;
2235 m.lParam = lParam & 0xdfff;
2236 TranslateMessage(&m);
2240 case WM_LBUTTONDOWN:
2241 case WM_RBUTTONDOWN:
2242 case WM_MBUTTONDOWN:
2247 * Shift-clicks count as middle-clicks, since otherwise
2248 * two-button Windows users won't have any kind of
2249 * middle click to use.
2251 if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT))
2252 button = MIDDLE_BUTTON;
2253 else if (message == WM_RBUTTONDOWN || is_alt_pressed())
2254 button = RIGHT_BUTTON;
2256 button = LEFT_BUTTON;
2258 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
2259 (signed short)HIWORD(lParam), button))
2272 * Shift-clicks count as middle-clicks, since otherwise
2273 * two-button Windows users won't have any kind of
2274 * middle click to use.
2276 if (message == WM_MBUTTONUP || (wParam & MK_SHIFT))
2277 button = MIDDLE_RELEASE;
2278 else if (message == WM_RBUTTONUP || is_alt_pressed())
2279 button = RIGHT_RELEASE;
2281 button = LEFT_RELEASE;
2283 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
2284 (signed short)HIWORD(lParam), button))
2294 if (wParam & (MK_MBUTTON | MK_SHIFT))
2295 button = MIDDLE_DRAG;
2296 else if (wParam & MK_RBUTTON || is_alt_pressed())
2297 button = RIGHT_DRAG;
2301 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
2302 (signed short)HIWORD(lParam), button))
2307 if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam))
2312 DWORD now = GetTickCount();
2313 float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F;
2314 midend_timer(fe->me, elapsed);
2315 fe->timer_last_tickcount = now;
2320 return DefWindowProc(hwnd, message, wParam, lParam);
2323 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
2328 InitCommonControls();
2334 wndclass.lpfnWndProc = WndProc;
2335 wndclass.cbClsExtra = 0;
2336 wndclass.cbWndExtra = 0;
2337 wndclass.hInstance = inst;
2338 wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION);
2339 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
2340 wndclass.hbrBackground = NULL;
2341 wndclass.lpszMenuName = NULL;
2342 wndclass.lpszClassName = thegame.name;
2344 RegisterClass(&wndclass);
2347 while (*cmdline && isspace((unsigned char)*cmdline))
2350 if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) {
2352 sprintf(buf, "%.100s Error", thegame.name);
2353 MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
2357 while (GetMessage(&msg, NULL, 0, 0)) {
2358 DispatchMessage(&msg);