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)
1095 fe = snew(frontend);
1097 fe->me = midend_new(fe, &thegame, &win_drawing, fe);
1100 *error = midend_game_id(fe->me, game_id);
1102 midend_free(fe->me);
1108 fe->help_path = NULL;
1116 fe->drawstatus = NOTHING;
1120 midend_new_game(fe->me);
1123 fe->nfonts = fe->fontsize = 0;
1125 fe->laststatus = NULL;
1131 colours = midend_colours(fe->me, &ncolours);
1133 fe->colours = snewn(ncolours, COLORREF);
1134 fe->brushes = snewn(ncolours, HBRUSH);
1135 fe->pens = snewn(ncolours, HPEN);
1137 for (i = 0; i < ncolours; i++) {
1138 fe->colours[i] = RGB(255 * colours[i*3+0],
1139 255 * colours[i*3+1],
1140 255 * colours[i*3+2]);
1141 fe->brushes[i] = CreateSolidBrush(fe->colours[i]);
1142 fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]);
1147 if (midend_wants_statusbar(fe->me)) {
1148 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh",
1149 WS_CHILD | WS_VISIBLE,
1150 0, 0, 0, 0, /* status bar does these */
1151 NULL, NULL, inst, NULL);
1153 fe->statusbar = 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 new_game_size(fe); /* initialises fe->bitmap */
1266 check_window_size(fe, &x, &y);
1268 SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe);
1270 ShowWindow(fe->hwnd, SW_NORMAL);
1271 SetForegroundWindow(fe->hwnd);
1273 midend_redraw(fe->me);
1278 static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg,
1279 WPARAM wParam, LPARAM lParam)
1281 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1288 if ((HIWORD(wParam) == BN_CLICKED ||
1289 HIWORD(wParam) == BN_DOUBLECLICKED) &&
1290 LOWORD(wParam) == IDOK)
1303 * Wrappers on midend_{get,set}_config, which extend the CFG_*
1304 * enumeration to add CFG_PRINT.
1306 static config_item *frontend_get_config(frontend *fe, int which,
1309 if (which < CFG_FRONTEND_SPECIFIC) {
1310 return midend_get_config(fe->me, which, wintitle);
1311 } else if (which == CFG_PRINT) {
1315 *wintitle = snewn(40 + strlen(thegame.name), char);
1316 sprintf(*wintitle, "%s print setup", thegame.name);
1318 ret = snewn(8, config_item);
1322 ret[i].name = "Number of puzzles to print";
1323 ret[i].type = C_STRING;
1324 ret[i].sval = dupstr("1");
1328 ret[i].name = "Number of puzzles across the page";
1329 ret[i].type = C_STRING;
1330 ret[i].sval = dupstr("1");
1334 ret[i].name = "Number of puzzles down the page";
1335 ret[i].type = C_STRING;
1336 ret[i].sval = dupstr("1");
1340 ret[i].name = "Percentage of standard size";
1341 ret[i].type = C_STRING;
1342 ret[i].sval = dupstr("100.0");
1346 ret[i].name = "Include currently shown puzzle";
1347 ret[i].type = C_BOOLEAN;
1352 ret[i].name = "Print solutions";
1353 ret[i].type = C_BOOLEAN;
1355 ret[i].ival = FALSE;
1358 if (thegame.can_print_in_colour) {
1359 ret[i].name = "Print in colour";
1360 ret[i].type = C_BOOLEAN;
1362 ret[i].ival = FALSE;
1367 ret[i].type = C_END;
1374 assert(!"We should never get here");
1379 static char *frontend_set_config(frontend *fe, int which, config_item *cfg)
1381 if (which < CFG_FRONTEND_SPECIFIC) {
1382 return midend_set_config(fe->me, which, cfg);
1383 } else if (which == CFG_PRINT) {
1384 if ((fe->printcount = atoi(cfg[0].sval)) <= 0)
1385 return "Number of puzzles to print should be at least one";
1386 if ((fe->printw = atoi(cfg[1].sval)) <= 0)
1387 return "Number of puzzles across the page should be at least one";
1388 if ((fe->printh = atoi(cfg[2].sval)) <= 0)
1389 return "Number of puzzles down the page should be at least one";
1390 if ((fe->printscale = (float)atof(cfg[3].sval)) <= 0)
1391 return "Print size should be positive";
1392 fe->printcurr = cfg[4].ival;
1393 fe->printsolns = cfg[5].ival;
1394 fe->printcolour = thegame.can_print_in_colour && cfg[6].ival;
1397 assert(!"We should never get here");
1398 return "Internal error";
1402 static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg,
1403 WPARAM wParam, LPARAM lParam)
1405 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1415 * OK and Cancel are special cases.
1417 if ((HIWORD(wParam) == BN_CLICKED ||
1418 HIWORD(wParam) == BN_DOUBLECLICKED) &&
1419 (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) {
1420 if (LOWORD(wParam) == IDOK) {
1421 char *err = frontend_set_config(fe, fe->cfg_which, fe->cfg);
1424 MessageBox(hwnd, err, "Validation error",
1425 MB_ICONERROR | MB_OK);
1436 * First find the control whose id this is.
1438 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
1439 if (j->ctlid == LOWORD(wParam))
1442 if (i->type == C_END)
1443 return 0; /* not our problem */
1445 if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) {
1447 GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer));
1448 buffer[lenof(buffer)-1] = '\0';
1450 i->sval = dupstr(buffer);
1451 } else if (i->type == C_BOOLEAN &&
1452 (HIWORD(wParam) == BN_CLICKED ||
1453 HIWORD(wParam) == BN_DOUBLECLICKED)) {
1454 i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid);
1455 } else if (i->type == C_CHOICES &&
1456 HIWORD(wParam) == CBN_SELCHANGE) {
1457 i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid,
1458 CB_GETCURSEL, 0, 0);
1471 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
1472 char *wclass, int wstyle,
1473 int exstyle, const char *wtext, int wid)
1476 ret = CreateWindowEx(exstyle, wclass, wtext,
1477 wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1,
1478 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
1479 SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0));
1483 static void about(frontend *fe)
1493 int winwidth, winheight, y;
1494 int height, width, maxwid;
1495 const char *strings[16];
1500 sprintf(titlebuf, "About %.250s", thegame.name);
1502 strings[nstrings++] = thegame.name;
1503 strings[nstrings++] = "from Simon Tatham's Portable Puzzle Collection";
1504 strings[nstrings++] = ver;
1506 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
1507 wc.lpfnWndProc = DefDlgProc;
1509 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
1510 wc.hInstance = fe->inst;
1512 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
1513 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
1514 wc.lpszMenuName = NULL;
1515 wc.lpszClassName = "GameAboutBox";
1518 hdc = GetDC(fe->hwnd);
1519 SetMapMode(hdc, MM_TEXT);
1521 fe->dlg_done = FALSE;
1523 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
1525 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
1526 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
1531 oldfont = SelectObject(hdc, fe->cfgfont);
1532 if (GetTextMetrics(hdc, &tm)) {
1533 height = tm.tmAscent + tm.tmDescent;
1534 width = tm.tmAveCharWidth;
1536 height = width = 30;
1540 * Figure out the layout of the About box by measuring the
1541 * length of each piece of text.
1544 winheight = height/2;
1546 for (i = 0; i < nstrings; i++) {
1547 if (GetTextExtentPoint32(hdc, strings[i], strlen(strings[i]), &size))
1548 lengths[i] = size.cx;
1550 lengths[i] = 0; /* *shrug* */
1551 if (maxwid < lengths[i])
1552 maxwid = lengths[i];
1553 winheight += height * 3 / 2 + (height / 2);
1556 winheight += height + height * 7 / 4; /* OK button */
1557 winwidth = maxwid + 4*width;
1559 SelectObject(hdc, oldfont);
1560 ReleaseDC(fe->hwnd, hdc);
1563 * Create the dialog, now that we know its size.
1570 r.bottom = winheight;
1572 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
1573 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1574 WS_CAPTION | WS_SYSMENU*/) &~
1575 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
1579 * Centre the dialog on its parent window.
1583 GetWindowRect(fe->hwnd, &r2);
1584 r.left = (r2.left + r2.right - r.right) / 2;
1585 r.top = (r2.top + r2.bottom - r.bottom) / 2;
1589 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, titlebuf,
1590 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1591 WS_CAPTION | WS_SYSMENU,
1593 r.right-r.left, r.bottom-r.top,
1594 fe->hwnd, NULL, fe->inst, NULL);
1597 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
1599 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
1600 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)AboutDlgProc);
1604 for (i = 0; i < nstrings; i++) {
1605 int border = width*2 + (maxwid - lengths[i]) / 2;
1606 mkctrl(fe, border, border+lengths[i], y+height*1/8, y+height*9/8,
1607 "Static", 0, 0, strings[i], id++);
1610 assert(y < winheight);
1614 y += height/2; /* extra space before OK */
1615 mkctrl(fe, width*2, maxwid+width*2, y, y+height*7/4, "BUTTON",
1616 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
1619 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
1621 EnableWindow(fe->hwnd, FALSE);
1622 ShowWindow(fe->cfgbox, SW_NORMAL);
1623 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
1624 if (!IsDialogMessage(fe->cfgbox, &msg))
1625 DispatchMessage(&msg);
1629 EnableWindow(fe->hwnd, TRUE);
1630 SetForegroundWindow(fe->hwnd);
1631 DestroyWindow(fe->cfgbox);
1632 DeleteObject(fe->cfgfont);
1635 static int get_config(frontend *fe, int which)
1648 int winwidth, winheight, col1l, col1r, col2l, col2r, y;
1649 int height, width, maxlabel, maxcheckbox;
1651 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
1652 wc.lpfnWndProc = DefDlgProc;
1654 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
1655 wc.hInstance = fe->inst;
1657 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
1658 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
1659 wc.lpszMenuName = NULL;
1660 wc.lpszClassName = "GameConfigBox";
1663 hdc = GetDC(fe->hwnd);
1664 SetMapMode(hdc, MM_TEXT);
1666 fe->dlg_done = FALSE;
1668 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
1670 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
1671 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
1676 oldfont = SelectObject(hdc, fe->cfgfont);
1677 if (GetTextMetrics(hdc, &tm)) {
1678 height = tm.tmAscent + tm.tmDescent;
1679 width = tm.tmAveCharWidth;
1681 height = width = 30;
1684 fe->cfg = frontend_get_config(fe, which, &title);
1685 fe->cfg_which = which;
1688 * Figure out the layout of the config box by measuring the
1689 * length of each piece of text.
1691 maxlabel = maxcheckbox = 0;
1692 winheight = height/2;
1694 for (i = fe->cfg; i->type != C_END; i++) {
1699 * Both these control types have a label filling only
1700 * the left-hand column of the box.
1702 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
1705 winheight += height * 3 / 2 + (height / 2);
1710 * Checkboxes take up the whole of the box width.
1712 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
1713 maxcheckbox < size.cx)
1714 maxcheckbox = size.cx;
1715 winheight += height + (height / 2);
1720 winheight += height + height * 7 / 4; /* OK / Cancel buttons */
1723 col1r = col1l + maxlabel;
1724 col2l = col1r + 2*width;
1725 col2r = col2l + 30*width;
1726 if (col2r < col1l+2*height+maxcheckbox)
1727 col2r = col1l+2*height+maxcheckbox;
1728 winwidth = col2r + 2*width;
1730 SelectObject(hdc, oldfont);
1731 ReleaseDC(fe->hwnd, hdc);
1734 * Create the dialog, now that we know its size.
1741 r.bottom = winheight;
1743 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
1744 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1745 WS_CAPTION | WS_SYSMENU*/) &~
1746 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
1750 * Centre the dialog on its parent window.
1754 GetWindowRect(fe->hwnd, &r2);
1755 r.left = (r2.left + r2.right - r.right) / 2;
1756 r.top = (r2.top + r2.bottom - r.bottom) / 2;
1760 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title,
1761 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1762 WS_CAPTION | WS_SYSMENU,
1764 r.right-r.left, r.bottom-r.top,
1765 fe->hwnd, NULL, fe->inst, NULL);
1769 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
1771 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
1772 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc);
1775 * Count the controls so we can allocate cfgaux.
1777 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
1779 fe->cfgaux = snewn(nctrls, struct cfg_aux);
1783 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
1787 * Edit box with a label beside it.
1789 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
1790 "Static", 0, 0, i->name, id++);
1791 ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2,
1792 "EDIT", WS_TABSTOP | ES_AUTOHSCROLL,
1793 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
1794 SetWindowText(ctl, i->sval);
1802 mkctrl(fe, col1l, col2r, y, y+height, "BUTTON",
1803 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
1804 0, i->name, (j->ctlid = id++));
1805 CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0));
1811 * Drop-down list with a label beside it.
1813 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
1814 "STATIC", 0, 0, i->name, id++);
1815 ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2,
1816 "COMBOBOX", WS_TABSTOP |
1817 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
1818 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
1820 char c, *p, *q, *str;
1822 SendMessage(ctl, CB_RESETCONTENT, 0, 0);
1827 while (*q && *q != c) q++;
1828 str = snewn(q-p+1, char);
1829 strncpy(str, p, q-p);
1831 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str);
1838 SendMessage(ctl, CB_SETCURSEL, i->ival, 0);
1844 assert(y < winheight);
1848 y += height/2; /* extra space before OK and Cancel */
1849 mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON",
1850 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
1852 mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON",
1853 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL);
1855 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
1857 EnableWindow(fe->hwnd, FALSE);
1858 ShowWindow(fe->cfgbox, SW_NORMAL);
1859 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
1860 if (!IsDialogMessage(fe->cfgbox, &msg))
1861 DispatchMessage(&msg);
1865 EnableWindow(fe->hwnd, TRUE);
1866 SetForegroundWindow(fe->hwnd);
1867 DestroyWindow(fe->cfgbox);
1868 DeleteObject(fe->cfgfont);
1873 return (fe->dlg_done == 2);
1876 static void new_game_size(frontend *fe)
1882 get_max_puzzle_size(fe, &x, &y);
1883 midend_size(fe->me, &x, &y, FALSE);
1888 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1890 if (fe->statusbar != NULL) {
1891 GetWindowRect(fe->statusbar, &sr);
1893 sr.left = sr.right = sr.top = sr.bottom = 0;
1895 SetWindowPos(fe->hwnd, NULL, 0, 0,
1897 r.bottom - r.top + sr.bottom - sr.top,
1898 SWP_NOMOVE | SWP_NOZORDER);
1900 check_window_size(fe, &x, &y);
1902 if (fe->statusbar != NULL)
1903 SetWindowPos(fe->statusbar, NULL, 0, y, x,
1904 sr.bottom - sr.top, SWP_NOZORDER);
1906 if (fe->bitmap) DeleteObject(fe->bitmap);
1908 hdc = GetDC(fe->hwnd);
1909 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
1910 ReleaseDC(fe->hwnd, hdc);
1912 midend_redraw(fe->me);
1915 static void new_game_type(frontend *fe)
1917 midend_new_game(fe->me);
1921 static int is_alt_pressed(void)
1924 int r = GetKeyboardState(keystate);
1927 if (keystate[VK_MENU] & 0x80)
1929 if (keystate[VK_RMENU] & 0x80)
1934 static void savefile_write(void *wctx, void *buf, int len)
1936 FILE *fp = (FILE *)wctx;
1937 fwrite(buf, 1, len, fp);
1940 static int savefile_read(void *wctx, void *buf, int len)
1942 FILE *fp = (FILE *)wctx;
1945 ret = fread(buf, 1, len, fp);
1946 return (ret == len);
1949 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
1950 WPARAM wParam, LPARAM lParam)
1952 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1957 DestroyWindow(hwnd);
1960 cmd = wParam & ~0xF; /* low 4 bits reserved to Windows */
1963 if (!midend_process_key(fe->me, 0, 0, 'n'))
1967 midend_restart_game(fe->me);
1970 if (!midend_process_key(fe->me, 0, 0, 'u'))
1974 if (!midend_process_key(fe->me, 0, 0, '\x12'))
1979 char *text = midend_text_format(fe->me);
1981 write_clip(hwnd, text);
1983 MessageBeep(MB_ICONWARNING);
1989 char *msg = midend_solve(fe->me);
1991 MessageBox(hwnd, msg, "Unable to solve",
1992 MB_ICONERROR | MB_OK);
1996 if (!midend_process_key(fe->me, 0, 0, 'q'))
2000 if (get_config(fe, CFG_SETTINGS))
2004 if (get_config(fe, CFG_SEED))
2008 if (get_config(fe, CFG_DESC))
2012 if (get_config(fe, CFG_PRINT))
2022 char filename[FILENAME_MAX];
2025 memset(&of, 0, sizeof(of));
2026 of.hwndOwner = hwnd;
2027 of.lpstrFilter = "All Files (*.*)\0*\0\0\0";
2028 of.lpstrCustomFilter = NULL;
2029 of.nFilterIndex = 1;
2030 of.lpstrFile = filename;
2032 of.nMaxFile = lenof(filename);
2033 of.lpstrFileTitle = NULL;
2034 of.lpstrTitle = (cmd == IDM_SAVE ?
2035 "Enter name of game file to save" :
2036 "Enter name of saved game file to load");
2038 #ifdef OPENFILENAME_SIZE_VERSION_400
2039 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
2041 of.lStructSize = sizeof(of);
2043 of.lpstrInitialDir = NULL;
2045 if (cmd == IDM_SAVE)
2046 ret = GetSaveFileName(&of);
2048 ret = GetOpenFileName(&of);
2051 if (cmd == IDM_SAVE) {
2054 if ((fp = fopen(filename, "r")) != NULL) {
2055 char buf[256 + FILENAME_MAX];
2059 sprintf(buf, "Are you sure you want to overwrite"
2060 " the file \"%.*s\"?",
2061 FILENAME_MAX, filename);
2062 if (MessageBox(hwnd, buf, "Question",
2063 MB_YESNO | MB_ICONQUESTION)
2068 fp = fopen(filename, "w");
2071 MessageBox(hwnd, "Unable to open save file",
2072 "Error", MB_ICONERROR | MB_OK);
2076 midend_serialise(fe->me, savefile_write, fp);
2080 FILE *fp = fopen(filename, "r");
2084 MessageBox(hwnd, "Unable to open saved game file",
2085 "Error", MB_ICONERROR | MB_OK);
2089 err = midend_deserialise(fe->me, savefile_read, fp);
2094 MessageBox(hwnd, err, "Error", MB_ICONERROR|MB_OK);
2105 assert(fe->help_path);
2106 WinHelp(hwnd, fe->help_path,
2107 fe->help_has_contents ? HELP_FINDER : HELP_CONTENTS, 0);
2110 assert(fe->help_path);
2111 assert(thegame.winhelp_topic);
2113 char *cmd = snewn(10+strlen(thegame.winhelp_topic), char);
2114 sprintf(cmd, "JI(`',`%s')", thegame.winhelp_topic);
2115 WinHelp(hwnd, fe->help_path, HELP_COMMAND, (DWORD)cmd);
2121 int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10;
2123 if (p >= 0 && p < fe->npresets) {
2124 midend_set_params(fe->me, fe->presets[p]);
2140 hdc = BeginPaint(hwnd, &p);
2141 hdc2 = CreateCompatibleDC(hdc);
2142 prevbm = SelectObject(hdc2, fe->bitmap);
2144 p.rcPaint.left, p.rcPaint.top,
2145 p.rcPaint.right - p.rcPaint.left,
2146 p.rcPaint.bottom - p.rcPaint.top,
2148 p.rcPaint.left, p.rcPaint.top,
2150 SelectObject(hdc2, prevbm);
2159 int r = GetKeyboardState(keystate);
2160 int shift = (r && (keystate[VK_SHIFT] & 0x80)) ? MOD_SHFT : 0;
2161 int ctrl = (r && (keystate[VK_CONTROL] & 0x80)) ? MOD_CTRL : 0;
2165 if (!(lParam & 0x01000000))
2166 key = MOD_NUM_KEYPAD | '4';
2168 key = shift | ctrl | CURSOR_LEFT;
2171 if (!(lParam & 0x01000000))
2172 key = MOD_NUM_KEYPAD | '6';
2174 key = shift | ctrl | CURSOR_RIGHT;
2177 if (!(lParam & 0x01000000))
2178 key = MOD_NUM_KEYPAD | '8';
2180 key = shift | ctrl | CURSOR_UP;
2183 if (!(lParam & 0x01000000))
2184 key = MOD_NUM_KEYPAD | '2';
2186 key = shift | ctrl | CURSOR_DOWN;
2189 * Diagonal keys on the numeric keypad.
2192 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '9';
2195 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '3';
2198 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '7';
2201 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '1';
2204 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '0';
2207 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '5';
2210 * Numeric keypad keys with Num Lock on.
2212 case VK_NUMPAD4: key = MOD_NUM_KEYPAD | '4'; break;
2213 case VK_NUMPAD6: key = MOD_NUM_KEYPAD | '6'; break;
2214 case VK_NUMPAD8: key = MOD_NUM_KEYPAD | '8'; break;
2215 case VK_NUMPAD2: key = MOD_NUM_KEYPAD | '2'; break;
2216 case VK_NUMPAD5: key = MOD_NUM_KEYPAD | '5'; break;
2217 case VK_NUMPAD9: key = MOD_NUM_KEYPAD | '9'; break;
2218 case VK_NUMPAD3: key = MOD_NUM_KEYPAD | '3'; break;
2219 case VK_NUMPAD7: key = MOD_NUM_KEYPAD | '7'; break;
2220 case VK_NUMPAD1: key = MOD_NUM_KEYPAD | '1'; break;
2221 case VK_NUMPAD0: key = MOD_NUM_KEYPAD | '0'; break;
2225 if (!midend_process_key(fe->me, 0, 0, key))
2230 m.message = WM_KEYDOWN;
2232 m.lParam = lParam & 0xdfff;
2233 TranslateMessage(&m);
2237 case WM_LBUTTONDOWN:
2238 case WM_RBUTTONDOWN:
2239 case WM_MBUTTONDOWN:
2244 * Shift-clicks count as middle-clicks, since otherwise
2245 * two-button Windows users won't have any kind of
2246 * middle click to use.
2248 if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT))
2249 button = MIDDLE_BUTTON;
2250 else if (message == WM_RBUTTONDOWN || is_alt_pressed())
2251 button = RIGHT_BUTTON;
2253 button = LEFT_BUTTON;
2255 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
2256 (signed short)HIWORD(lParam), button))
2269 * Shift-clicks count as middle-clicks, since otherwise
2270 * two-button Windows users won't have any kind of
2271 * middle click to use.
2273 if (message == WM_MBUTTONUP || (wParam & MK_SHIFT))
2274 button = MIDDLE_RELEASE;
2275 else if (message == WM_RBUTTONUP || is_alt_pressed())
2276 button = RIGHT_RELEASE;
2278 button = LEFT_RELEASE;
2280 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
2281 (signed short)HIWORD(lParam), button))
2291 if (wParam & (MK_MBUTTON | MK_SHIFT))
2292 button = MIDDLE_DRAG;
2293 else if (wParam & MK_RBUTTON || is_alt_pressed())
2294 button = RIGHT_DRAG;
2298 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
2299 (signed short)HIWORD(lParam), button))
2304 if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam))
2309 DWORD now = GetTickCount();
2310 float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F;
2311 midend_timer(fe->me, elapsed);
2312 fe->timer_last_tickcount = now;
2317 return DefWindowProc(hwnd, message, wParam, lParam);
2320 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
2325 InitCommonControls();
2331 wndclass.lpfnWndProc = WndProc;
2332 wndclass.cbClsExtra = 0;
2333 wndclass.cbWndExtra = 0;
2334 wndclass.hInstance = inst;
2335 wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION);
2336 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
2337 wndclass.hbrBackground = NULL;
2338 wndclass.lpszMenuName = NULL;
2339 wndclass.lpszClassName = thegame.name;
2341 RegisterClass(&wndclass);
2344 while (*cmdline && isspace((unsigned char)*cmdline))
2347 if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) {
2349 sprintf(buf, "%.100s Error", thegame.name);
2350 MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
2354 while (GetMessage(&msg, NULL, 0, 0)) {
2355 DispatchMessage(&msg);