2 * windows.c: Windows front end for my puzzle collection.
6 * - Figure out what to do if a puzzle requests a size bigger than
7 * the screen will take. In principle we could put scrollbars in
8 * the window, although that would be pretty horrid. Another
9 * option is to detect in advance that this will be a problem -
10 * we can probably tell this using midend_size() before actually
11 * generating the puzzle - and simply refuse to do it.
27 #define IDM_NEW 0x0010
28 #define IDM_RESTART 0x0020
29 #define IDM_UNDO 0x0030
30 #define IDM_REDO 0x0040
31 #define IDM_COPY 0x0050
32 #define IDM_SOLVE 0x0060
33 #define IDM_QUIT 0x0070
34 #define IDM_CONFIG 0x0080
35 #define IDM_DESC 0x0090
36 #define IDM_SEED 0x00A0
37 #define IDM_HELPC 0x00B0
38 #define IDM_GAMEHELP 0x00C0
39 #define IDM_ABOUT 0x00D0
40 #define IDM_PRESETS 0x0100
42 #define HELP_FILE_NAME "puzzles.hlp"
43 #define HELP_CNT_NAME "puzzles.cnt"
46 static FILE *debug_fp = NULL;
47 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
48 static int debug_got_console = 0;
54 if (!debug_got_console) {
56 debug_got_console = 1;
57 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
61 debug_fp = fopen("debug.log", "w");
64 if (debug_hdl != INVALID_HANDLE_VALUE) {
65 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
71 void debug_printf(char *fmt, ...)
77 vsprintf(buf, fmt, ap);
101 HWND hwnd, statusbar, cfgbox;
103 HBITMAP bitmap, prevbm;
110 DWORD timer_last_tickcount;
112 game_params **presets;
114 int nfonts, fontsize;
116 struct cfg_aux *cfgaux;
117 int cfg_which, dlg_done;
120 int help_has_contents;
124 void fatal(char *fmt, ...)
130 vsprintf(buf, fmt, ap);
133 MessageBox(NULL, buf, "Fatal error", MB_ICONEXCLAMATION | MB_OK);
138 void get_random_seed(void **randseed, int *randseedsize)
140 time_t *tp = snew(time_t);
142 *randseed = (void *)tp;
143 *randseedsize = sizeof(time_t);
146 void status_bar(frontend *fe, char *text)
148 char *rewritten = midend_rewrite_statusbar(fe->me, text);
149 if (!fe->laststatus || strcmp(rewritten, fe->laststatus)) {
150 SetWindowText(fe->statusbar, rewritten);
151 sfree(fe->laststatus);
152 fe->laststatus = rewritten;
158 blitter *blitter_new(int w, int h)
160 blitter *bl = snew(blitter);
162 memset(bl, 0, sizeof(blitter));
170 void blitter_free(blitter *bl)
172 if (bl->bitmap) DeleteObject(bl->bitmap);
176 static void blitter_mkbitmap(frontend *fe, blitter *bl)
178 HDC hdc = GetDC(fe->hwnd);
179 bl->bitmap = CreateCompatibleBitmap(hdc, bl->w, bl->h);
180 ReleaseDC(fe->hwnd, hdc);
183 /* BitBlt(dstDC, dstX, dstY, dstW, dstH, srcDC, srcX, srcY, dType) */
185 void blitter_save(frontend *fe, blitter *bl, int x, int y)
187 HDC hdc_win, hdc_blit;
190 if (!bl->bitmap) blitter_mkbitmap(fe, bl);
192 bl->x = x; bl->y = y;
194 hdc_win = GetDC(fe->hwnd);
195 hdc_blit = CreateCompatibleDC(hdc_win);
196 if (!hdc_blit) fatal("hdc_blit failed: 0x%x", GetLastError());
198 prev_blit = SelectObject(hdc_blit, bl->bitmap);
199 if (prev_blit == NULL || prev_blit == HGDI_ERROR)
200 fatal("SelectObject for hdc_main failed: 0x%x", GetLastError());
202 if (!BitBlt(hdc_blit, 0, 0, bl->w, bl->h,
203 fe->hdc_bm, x, y, SRCCOPY))
204 fatal("BitBlt failed: 0x%x", GetLastError());
206 SelectObject(hdc_blit, prev_blit);
208 ReleaseDC(fe->hwnd, hdc_win);
211 void blitter_load(frontend *fe, blitter *bl, int x, int y)
213 HDC hdc_win, hdc_blit;
216 assert(bl->bitmap); /* we should always have saved before loading */
218 if (x == BLITTER_FROMSAVED) x = bl->x;
219 if (y == BLITTER_FROMSAVED) y = bl->y;
221 hdc_win = GetDC(fe->hwnd);
222 hdc_blit = CreateCompatibleDC(hdc_win);
224 prev_blit = SelectObject(hdc_blit, bl->bitmap);
226 BitBlt(fe->hdc_bm, x, y, bl->w, bl->h,
227 hdc_blit, 0, 0, SRCCOPY);
229 SelectObject(hdc_blit, prev_blit);
231 ReleaseDC(fe->hwnd, hdc_win);
234 void frontend_default_colour(frontend *fe, float *output)
236 DWORD c = GetSysColor(COLOR_MENU); /* ick */
238 output[0] = (float)(GetRValue(c) / 255.0);
239 output[1] = (float)(GetGValue(c) / 255.0);
240 output[2] = (float)(GetBValue(c) / 255.0);
243 void clip(frontend *fe, int x, int y, int w, int h)
245 IntersectClipRect(fe->hdc_bm, x, y, x+w, y+h);
248 void unclip(frontend *fe)
250 SelectClipRgn(fe->hdc_bm, NULL);
253 void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize,
254 int align, int colour, char *text)
259 * Find or create the font.
261 for (i = 0; i < fe->nfonts; i++)
262 if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
265 if (i == fe->nfonts) {
266 if (fe->fontsize <= fe->nfonts) {
267 fe->fontsize = fe->nfonts + 10;
268 fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
273 fe->fonts[i].type = fonttype;
274 fe->fonts[i].size = fontsize;
276 fe->fonts[i].font = CreateFont(-fontsize, 0, 0, 0, FW_BOLD,
277 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
278 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
280 (fonttype == FONT_FIXED ?
281 FIXED_PITCH | FF_DONTCARE :
282 VARIABLE_PITCH | FF_SWISS),
287 * Position and draw the text.
294 oldfont = SelectObject(fe->hdc_bm, fe->fonts[i].font);
295 if (GetTextMetrics(fe->hdc_bm, &tm)) {
296 if (align & ALIGN_VCENTRE)
297 y -= (tm.tmAscent+tm.tmDescent)/2;
301 if (GetTextExtentPoint32(fe->hdc_bm, text, strlen(text), &size)) {
302 if (align & ALIGN_HCENTRE)
304 else if (align & ALIGN_HRIGHT)
307 SetBkMode(fe->hdc_bm, TRANSPARENT);
308 SetTextColor(fe->hdc_bm, fe->colours[colour]);
309 TextOut(fe->hdc_bm, x, y, text, strlen(text));
310 SelectObject(fe->hdc_bm, oldfont);
314 void draw_rect(frontend *fe, int x, int y, int w, int h, int colour)
316 if (w == 1 && h == 1) {
318 * Rectangle() appears to get uppity if asked to draw a 1x1
319 * rectangle, presumably on the grounds that that's beneath
320 * its dignity and you ought to be using SetPixel instead.
323 SetPixel(fe->hdc_bm, x, y, fe->colours[colour]);
325 HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[colour]);
326 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
327 Rectangle(fe->hdc_bm, x, y, x+w, y+h);
328 SelectObject(fe->hdc_bm, oldbrush);
329 SelectObject(fe->hdc_bm, oldpen);
333 void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour)
335 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
336 MoveToEx(fe->hdc_bm, x1, y1, NULL);
337 LineTo(fe->hdc_bm, x2, y2);
338 SetPixel(fe->hdc_bm, x2, y2, fe->colours[colour]);
339 SelectObject(fe->hdc_bm, oldpen);
342 void draw_polygon(frontend *fe, int *coords, int npoints,
343 int fill, int colour)
345 POINT *pts = snewn(npoints+1, POINT);
348 for (i = 0; i <= npoints; i++) {
349 int j = (i < npoints ? i : 0);
350 pts[i].x = coords[j*2];
351 pts[i].y = coords[j*2+1];
355 HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[colour]);
356 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
357 Polygon(fe->hdc_bm, pts, npoints);
358 SelectObject(fe->hdc_bm, oldbrush);
359 SelectObject(fe->hdc_bm, oldpen);
361 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
362 Polyline(fe->hdc_bm, pts, npoints+1);
363 SelectObject(fe->hdc_bm, oldpen);
369 void start_draw(frontend *fe)
372 hdc_win = GetDC(fe->hwnd);
373 fe->hdc_bm = CreateCompatibleDC(hdc_win);
374 fe->prevbm = SelectObject(fe->hdc_bm, fe->bitmap);
375 ReleaseDC(fe->hwnd, hdc_win);
377 SetMapMode(fe->hdc_bm, MM_TEXT);
380 void draw_update(frontend *fe, int x, int y, int w, int h)
389 InvalidateRect(fe->hwnd, &r, FALSE);
392 void end_draw(frontend *fe)
394 SelectObject(fe->hdc_bm, fe->prevbm);
395 DeleteDC(fe->hdc_bm);
397 DeleteObject(fe->clip);
402 void deactivate_timer(frontend *fe)
404 KillTimer(fe->hwnd, fe->timer);
408 void activate_timer(frontend *fe)
411 fe->timer = SetTimer(fe->hwnd, fe->timer, 20, NULL);
412 fe->timer_last_tickcount = GetTickCount();
416 void write_clip(HWND hwnd, char *data)
424 * Windows expects CRLF in the clipboard, so we must convert
425 * any \n that has come out of the puzzle backend.
428 for (i = 0; data[i]; i++) {
433 data2 = snewn(len+1, char);
435 for (i = 0; data[i]; i++) {
438 data2[j++] = data[i];
443 clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
446 lock = GlobalLock(clipdata);
449 memcpy(lock, data2, len);
450 ((unsigned char *) lock)[len] = 0;
451 GlobalUnlock(clipdata);
453 if (OpenClipboard(hwnd)) {
455 SetClipboardData(CF_TEXT, clipdata);
458 GlobalFree(clipdata);
464 * See if we can find a help file.
466 static void find_help_file(frontend *fe)
468 char b[2048], *p, *q, *r;
470 if (!fe->help_path) {
471 GetModuleFileName(NULL, b, sizeof(b) - 1);
473 p = strrchr(b, '\\');
474 if (p && p >= r) r = p+1;
476 if (q && q >= r) r = q+1;
477 strcpy(r, HELP_FILE_NAME);
478 if ( (fp = fopen(b, "r")) != NULL) {
479 fe->help_path = dupstr(b);
482 fe->help_path = NULL;
483 strcpy(r, HELP_CNT_NAME);
484 if ( (fp = fopen(b, "r")) != NULL) {
485 fe->help_has_contents = TRUE;
488 fe->help_has_contents = FALSE;
492 static void check_window_size(frontend *fe, int *px, int *py)
499 GetWindowRect(fe->statusbar, &sr);
500 sy = sr.bottom - sr.top;
506 * See if we actually got the window size we wanted, and adjust
507 * the puzzle size if not.
509 GetClientRect(fe->hwnd, &r);
510 x = r.right - r.left;
511 y = r.bottom - r.top - sy;
512 midend_size(fe->me, &x, &y, FALSE);
513 if (x != r.right - r.left || y != r.bottom - r.top) {
515 * Resize the window, now we know what size we _really_
521 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
522 (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED),
524 SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left, r.bottom - r.top,
525 SWP_NOMOVE | SWP_NOZORDER);
529 GetClientRect(fe->hwnd, &r);
530 SetWindowPos(fe->statusbar, NULL, 0, r.bottom-r.top-sy, r.right-r.left,
538 static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
547 fe->me = midend_new(fe, &thegame);
550 *error = midend_game_id(fe->me, game_id);
558 fe->help_path = NULL;
562 midend_new_game(fe->me);
567 fe->nfonts = fe->fontsize = 0;
569 fe->laststatus = NULL;
575 colours = midend_colours(fe->me, &ncolours);
577 fe->colours = snewn(ncolours, COLORREF);
578 fe->brushes = snewn(ncolours, HBRUSH);
579 fe->pens = snewn(ncolours, HPEN);
581 for (i = 0; i < ncolours; i++) {
582 fe->colours[i] = RGB(255 * colours[i*3+0],
583 255 * colours[i*3+1],
584 255 * colours[i*3+2]);
585 fe->brushes[i] = CreateSolidBrush(fe->colours[i]);
586 fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]);
590 x = y = INT_MAX; /* find puzzle's preferred size */
591 midend_size(fe->me, &x, &y, FALSE);
596 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
597 (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED),
600 fe->hwnd = CreateWindowEx(0, thegame.name, thegame.name,
601 WS_OVERLAPPEDWINDOW &~
602 (WS_THICKFRAME | WS_MAXIMIZEBOX),
603 CW_USEDEFAULT, CW_USEDEFAULT,
604 r.right - r.left, r.bottom - r.top,
605 NULL, NULL, inst, NULL);
607 if (midend_wants_statusbar(fe->me)) {
609 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh",
610 WS_CHILD | WS_VISIBLE,
611 0, 0, 0, 0, /* status bar does these */
612 fe->hwnd, NULL, inst, NULL);
614 * Now resize the window to take account of the status bar.
616 GetWindowRect(fe->statusbar, &sr);
617 GetWindowRect(fe->hwnd, &r);
618 SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left,
619 r.bottom - r.top + sr.bottom - sr.top,
620 SWP_NOMOVE | SWP_NOZORDER);
622 fe->statusbar = NULL;
626 HMENU bar = CreateMenu();
627 HMENU menu = CreateMenu();
629 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Game");
630 AppendMenu(menu, MF_ENABLED, IDM_NEW, "New");
631 AppendMenu(menu, MF_ENABLED, IDM_RESTART, "Restart");
632 AppendMenu(menu, MF_ENABLED, IDM_DESC, "Specific...");
633 AppendMenu(menu, MF_ENABLED, IDM_SEED, "Random Seed...");
635 if ((fe->npresets = midend_num_presets(fe->me)) > 0 ||
636 thegame.can_configure) {
637 HMENU sub = CreateMenu();
640 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "Type");
642 fe->presets = snewn(fe->npresets, game_params *);
644 for (i = 0; i < fe->npresets; i++) {
647 midend_fetch_preset(fe->me, i, &name, &fe->presets[i]);
650 * FIXME: we ought to go through and do something
651 * with ampersands here.
654 AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name);
657 if (thegame.can_configure) {
658 AppendMenu(sub, MF_ENABLED, IDM_CONFIG, "Custom...");
662 AppendMenu(menu, MF_SEPARATOR, 0, 0);
663 AppendMenu(menu, MF_ENABLED, IDM_UNDO, "Undo");
664 AppendMenu(menu, MF_ENABLED, IDM_REDO, "Redo");
665 if (thegame.can_format_as_text) {
666 AppendMenu(menu, MF_SEPARATOR, 0, 0);
667 AppendMenu(menu, MF_ENABLED, IDM_COPY, "Copy");
669 if (thegame.can_solve) {
670 AppendMenu(menu, MF_SEPARATOR, 0, 0);
671 AppendMenu(menu, MF_ENABLED, IDM_SOLVE, "Solve");
673 AppendMenu(menu, MF_SEPARATOR, 0, 0);
674 AppendMenu(menu, MF_ENABLED, IDM_QUIT, "Exit");
676 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Help");
677 AppendMenu(menu, MF_ENABLED, IDM_ABOUT, "About");
679 AppendMenu(menu, MF_SEPARATOR, 0, 0);
680 AppendMenu(menu, MF_ENABLED, IDM_HELPC, "Contents");
681 if (thegame.winhelp_topic) {
683 assert(thegame.name);
684 item = snewn(9+strlen(thegame.name), char); /*ick*/
685 sprintf(item, "Help on %s", thegame.name);
686 AppendMenu(menu, MF_ENABLED, IDM_GAMEHELP, item);
690 SetMenu(fe->hwnd, bar);
693 check_window_size(fe, &x, &y);
695 hdc = GetDC(fe->hwnd);
696 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
697 ReleaseDC(fe->hwnd, hdc);
699 SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe);
701 ShowWindow(fe->hwnd, SW_NORMAL);
702 SetForegroundWindow(fe->hwnd);
704 midend_redraw(fe->me);
709 static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg,
710 WPARAM wParam, LPARAM lParam)
712 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
719 if ((HIWORD(wParam) == BN_CLICKED ||
720 HIWORD(wParam) == BN_DOUBLECLICKED) &&
721 LOWORD(wParam) == IDOK)
733 static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg,
734 WPARAM wParam, LPARAM lParam)
736 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
746 * OK and Cancel are special cases.
748 if ((HIWORD(wParam) == BN_CLICKED ||
749 HIWORD(wParam) == BN_DOUBLECLICKED) &&
750 (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) {
751 if (LOWORD(wParam) == IDOK) {
752 char *err = midend_set_config(fe->me, fe->cfg_which, fe->cfg);
755 MessageBox(hwnd, err, "Validation error",
756 MB_ICONERROR | MB_OK);
767 * First find the control whose id this is.
769 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
770 if (j->ctlid == LOWORD(wParam))
773 if (i->type == C_END)
774 return 0; /* not our problem */
776 if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) {
778 GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer));
779 buffer[lenof(buffer)-1] = '\0';
781 i->sval = dupstr(buffer);
782 } else if (i->type == C_BOOLEAN &&
783 (HIWORD(wParam) == BN_CLICKED ||
784 HIWORD(wParam) == BN_DOUBLECLICKED)) {
785 i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid);
786 } else if (i->type == C_CHOICES &&
787 HIWORD(wParam) == CBN_SELCHANGE) {
788 i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid,
802 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
803 char *wclass, int wstyle,
804 int exstyle, const char *wtext, int wid)
807 ret = CreateWindowEx(exstyle, wclass, wtext,
808 wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1,
809 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
810 SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0));
814 static void about(frontend *fe)
824 int winwidth, winheight, y;
825 int height, width, maxwid;
826 const char *strings[16];
831 sprintf(titlebuf, "About %.250s", thegame.name);
833 strings[nstrings++] = thegame.name;
834 strings[nstrings++] = "from Simon Tatham's Portable Puzzle Collection";
835 strings[nstrings++] = ver;
837 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
838 wc.lpfnWndProc = DefDlgProc;
840 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
841 wc.hInstance = fe->inst;
843 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
844 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
845 wc.lpszMenuName = NULL;
846 wc.lpszClassName = "GameAboutBox";
849 hdc = GetDC(fe->hwnd);
850 SetMapMode(hdc, MM_TEXT);
852 fe->dlg_done = FALSE;
854 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
856 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
857 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
862 oldfont = SelectObject(hdc, fe->cfgfont);
863 if (GetTextMetrics(hdc, &tm)) {
864 height = tm.tmAscent + tm.tmDescent;
865 width = tm.tmAveCharWidth;
871 * Figure out the layout of the About box by measuring the
872 * length of each piece of text.
875 winheight = height/2;
877 for (i = 0; i < nstrings; i++) {
878 if (GetTextExtentPoint32(hdc, strings[i], strlen(strings[i]), &size))
879 lengths[i] = size.cx;
881 lengths[i] = 0; /* *shrug* */
882 if (maxwid < lengths[i])
884 winheight += height * 3 / 2 + (height / 2);
887 winheight += height + height * 7 / 4; /* OK button */
888 winwidth = maxwid + 4*width;
890 SelectObject(hdc, oldfont);
891 ReleaseDC(fe->hwnd, hdc);
894 * Create the dialog, now that we know its size.
901 r.bottom = winheight;
903 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
904 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
905 WS_CAPTION | WS_SYSMENU*/) &~
906 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
910 * Centre the dialog on its parent window.
914 GetWindowRect(fe->hwnd, &r2);
915 r.left = (r2.left + r2.right - r.right) / 2;
916 r.top = (r2.top + r2.bottom - r.bottom) / 2;
920 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, titlebuf,
921 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
922 WS_CAPTION | WS_SYSMENU,
924 r.right-r.left, r.bottom-r.top,
925 fe->hwnd, NULL, fe->inst, NULL);
928 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
930 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
931 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)AboutDlgProc);
935 for (i = 0; i < nstrings; i++) {
936 int border = width*2 + (maxwid - lengths[i]) / 2;
937 mkctrl(fe, border, border+lengths[i], y+height*1/8, y+height*9/8,
938 "Static", 0, 0, strings[i], id++);
941 assert(y < winheight);
945 y += height/2; /* extra space before OK */
946 mkctrl(fe, width*2, maxwid+width*2, y, y+height*7/4, "BUTTON",
947 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
950 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
952 EnableWindow(fe->hwnd, FALSE);
953 ShowWindow(fe->cfgbox, SW_NORMAL);
954 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
955 if (!IsDialogMessage(fe->cfgbox, &msg))
956 DispatchMessage(&msg);
960 EnableWindow(fe->hwnd, TRUE);
961 SetForegroundWindow(fe->hwnd);
962 DestroyWindow(fe->cfgbox);
963 DeleteObject(fe->cfgfont);
966 static int get_config(frontend *fe, int which)
979 int winwidth, winheight, col1l, col1r, col2l, col2r, y;
980 int height, width, maxlabel, maxcheckbox;
982 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
983 wc.lpfnWndProc = DefDlgProc;
985 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
986 wc.hInstance = fe->inst;
988 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
989 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
990 wc.lpszMenuName = NULL;
991 wc.lpszClassName = "GameConfigBox";
994 hdc = GetDC(fe->hwnd);
995 SetMapMode(hdc, MM_TEXT);
997 fe->dlg_done = FALSE;
999 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
1001 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
1002 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
1007 oldfont = SelectObject(hdc, fe->cfgfont);
1008 if (GetTextMetrics(hdc, &tm)) {
1009 height = tm.tmAscent + tm.tmDescent;
1010 width = tm.tmAveCharWidth;
1012 height = width = 30;
1015 fe->cfg = midend_get_config(fe->me, which, &title);
1016 fe->cfg_which = which;
1019 * Figure out the layout of the config box by measuring the
1020 * length of each piece of text.
1022 maxlabel = maxcheckbox = 0;
1023 winheight = height/2;
1025 for (i = fe->cfg; i->type != C_END; i++) {
1030 * Both these control types have a label filling only
1031 * the left-hand column of the box.
1033 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
1036 winheight += height * 3 / 2 + (height / 2);
1041 * Checkboxes take up the whole of the box width.
1043 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
1044 maxcheckbox < size.cx)
1045 maxcheckbox = size.cx;
1046 winheight += height + (height / 2);
1051 winheight += height + height * 7 / 4; /* OK / Cancel buttons */
1054 col1r = col1l + maxlabel;
1055 col2l = col1r + 2*width;
1056 col2r = col2l + 30*width;
1057 if (col2r < col1l+2*height+maxcheckbox)
1058 col2r = col1l+2*height+maxcheckbox;
1059 winwidth = col2r + 2*width;
1061 SelectObject(hdc, oldfont);
1062 ReleaseDC(fe->hwnd, hdc);
1065 * Create the dialog, now that we know its size.
1072 r.bottom = winheight;
1074 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
1075 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1076 WS_CAPTION | WS_SYSMENU*/) &~
1077 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
1081 * Centre the dialog on its parent window.
1085 GetWindowRect(fe->hwnd, &r2);
1086 r.left = (r2.left + r2.right - r.right) / 2;
1087 r.top = (r2.top + r2.bottom - r.bottom) / 2;
1091 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title,
1092 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1093 WS_CAPTION | WS_SYSMENU,
1095 r.right-r.left, r.bottom-r.top,
1096 fe->hwnd, NULL, fe->inst, NULL);
1100 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
1102 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
1103 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc);
1106 * Count the controls so we can allocate cfgaux.
1108 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
1110 fe->cfgaux = snewn(nctrls, struct cfg_aux);
1114 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
1118 * Edit box with a label beside it.
1120 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
1121 "Static", 0, 0, i->name, id++);
1122 ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2,
1123 "EDIT", WS_TABSTOP | ES_AUTOHSCROLL,
1124 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
1125 SetWindowText(ctl, i->sval);
1133 mkctrl(fe, col1l, col2r, y, y+height, "BUTTON",
1134 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
1135 0, i->name, (j->ctlid = id++));
1136 CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0));
1142 * Drop-down list with a label beside it.
1144 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
1145 "STATIC", 0, 0, i->name, id++);
1146 ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2,
1147 "COMBOBOX", WS_TABSTOP |
1148 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
1149 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
1151 char c, *p, *q, *str;
1153 SendMessage(ctl, CB_RESETCONTENT, 0, 0);
1158 while (*q && *q != c) q++;
1159 str = snewn(q-p+1, char);
1160 strncpy(str, p, q-p);
1162 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str);
1169 SendMessage(ctl, CB_SETCURSEL, i->ival, 0);
1175 assert(y < winheight);
1179 y += height/2; /* extra space before OK and Cancel */
1180 mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON",
1181 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
1183 mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON",
1184 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL);
1186 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
1188 EnableWindow(fe->hwnd, FALSE);
1189 ShowWindow(fe->cfgbox, SW_NORMAL);
1190 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
1191 if (!IsDialogMessage(fe->cfgbox, &msg))
1192 DispatchMessage(&msg);
1196 EnableWindow(fe->hwnd, TRUE);
1197 SetForegroundWindow(fe->hwnd);
1198 DestroyWindow(fe->cfgbox);
1199 DeleteObject(fe->cfgfont);
1204 return (fe->dlg_done == 2);
1207 static void new_game_type(frontend *fe)
1213 midend_new_game(fe->me);
1215 midend_size(fe->me, &x, &y, FALSE);
1220 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
1221 (WS_THICKFRAME | WS_MAXIMIZEBOX |
1225 if (fe->statusbar != NULL) {
1226 GetWindowRect(fe->statusbar, &sr);
1228 sr.left = sr.right = sr.top = sr.bottom = 0;
1230 SetWindowPos(fe->hwnd, NULL, 0, 0,
1232 r.bottom - r.top + sr.bottom - sr.top,
1233 SWP_NOMOVE | SWP_NOZORDER);
1235 check_window_size(fe, &x, &y);
1237 if (fe->statusbar != NULL)
1238 SetWindowPos(fe->statusbar, NULL, 0, y, x,
1239 sr.bottom - sr.top, SWP_NOZORDER);
1241 DeleteObject(fe->bitmap);
1243 hdc = GetDC(fe->hwnd);
1244 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
1245 ReleaseDC(fe->hwnd, hdc);
1247 midend_redraw(fe->me);
1250 static int is_alt_pressed(void)
1253 int r = GetKeyboardState(keystate);
1256 if (keystate[VK_MENU] & 0x80)
1258 if (keystate[VK_RMENU] & 0x80)
1263 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
1264 WPARAM wParam, LPARAM lParam)
1266 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1270 DestroyWindow(hwnd);
1273 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
1275 if (!midend_process_key(fe->me, 0, 0, 'n'))
1279 midend_restart_game(fe->me);
1282 if (!midend_process_key(fe->me, 0, 0, 'u'))
1286 if (!midend_process_key(fe->me, 0, 0, '\x12'))
1291 char *text = midend_text_format(fe->me);
1293 write_clip(hwnd, text);
1295 MessageBeep(MB_ICONWARNING);
1301 char *msg = midend_solve(fe->me);
1303 MessageBox(hwnd, msg, "Unable to solve",
1304 MB_ICONERROR | MB_OK);
1308 if (!midend_process_key(fe->me, 0, 0, 'q'))
1312 if (get_config(fe, CFG_SETTINGS))
1316 if (get_config(fe, CFG_SEED))
1320 if (get_config(fe, CFG_DESC))
1327 assert(fe->help_path);
1328 WinHelp(hwnd, fe->help_path,
1329 fe->help_has_contents ? HELP_FINDER : HELP_CONTENTS, 0);
1332 assert(fe->help_path);
1333 assert(thegame.winhelp_topic);
1335 char *cmd = snewn(10+strlen(thegame.winhelp_topic), char);
1336 sprintf(cmd, "JI(`',`%s')", thegame.winhelp_topic);
1337 WinHelp(hwnd, fe->help_path, HELP_COMMAND, (DWORD)cmd);
1343 int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10;
1345 if (p >= 0 && p < fe->npresets) {
1346 midend_set_params(fe->me, fe->presets[p]);
1362 hdc = BeginPaint(hwnd, &p);
1363 hdc2 = CreateCompatibleDC(hdc);
1364 prevbm = SelectObject(hdc2, fe->bitmap);
1366 p.rcPaint.left, p.rcPaint.top,
1367 p.rcPaint.right - p.rcPaint.left,
1368 p.rcPaint.bottom - p.rcPaint.top,
1370 p.rcPaint.left, p.rcPaint.top,
1372 SelectObject(hdc2, prevbm);
1381 int r = GetKeyboardState(keystate);
1382 int shift = (r && (keystate[VK_SHIFT] & 0x80)) ? MOD_SHFT : 0;
1383 int ctrl = (r && (keystate[VK_CONTROL] & 0x80)) ? MOD_CTRL : 0;
1387 if (!(lParam & 0x01000000))
1388 key = MOD_NUM_KEYPAD | '4';
1390 key = shift | ctrl | CURSOR_LEFT;
1393 if (!(lParam & 0x01000000))
1394 key = MOD_NUM_KEYPAD | '6';
1396 key = shift | ctrl | CURSOR_RIGHT;
1399 if (!(lParam & 0x01000000))
1400 key = MOD_NUM_KEYPAD | '8';
1402 key = shift | ctrl | CURSOR_UP;
1405 if (!(lParam & 0x01000000))
1406 key = MOD_NUM_KEYPAD | '2';
1408 key = shift | ctrl | CURSOR_DOWN;
1411 * Diagonal keys on the numeric keypad.
1414 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '9';
1417 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '3';
1420 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '7';
1423 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '1';
1426 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '0';
1429 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '5';
1432 * Numeric keypad keys with Num Lock on.
1434 case VK_NUMPAD4: key = MOD_NUM_KEYPAD | '4'; break;
1435 case VK_NUMPAD6: key = MOD_NUM_KEYPAD | '6'; break;
1436 case VK_NUMPAD8: key = MOD_NUM_KEYPAD | '8'; break;
1437 case VK_NUMPAD2: key = MOD_NUM_KEYPAD | '2'; break;
1438 case VK_NUMPAD5: key = MOD_NUM_KEYPAD | '5'; break;
1439 case VK_NUMPAD9: key = MOD_NUM_KEYPAD | '9'; break;
1440 case VK_NUMPAD3: key = MOD_NUM_KEYPAD | '3'; break;
1441 case VK_NUMPAD7: key = MOD_NUM_KEYPAD | '7'; break;
1442 case VK_NUMPAD1: key = MOD_NUM_KEYPAD | '1'; break;
1443 case VK_NUMPAD0: key = MOD_NUM_KEYPAD | '0'; break;
1447 if (!midend_process_key(fe->me, 0, 0, key))
1452 m.message = WM_KEYDOWN;
1454 m.lParam = lParam & 0xdfff;
1455 TranslateMessage(&m);
1459 case WM_LBUTTONDOWN:
1460 case WM_RBUTTONDOWN:
1461 case WM_MBUTTONDOWN:
1466 * Shift-clicks count as middle-clicks, since otherwise
1467 * two-button Windows users won't have any kind of
1468 * middle click to use.
1470 if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT))
1471 button = MIDDLE_BUTTON;
1472 else if (message == WM_RBUTTONDOWN || is_alt_pressed())
1473 button = RIGHT_BUTTON;
1475 button = LEFT_BUTTON;
1477 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1478 (signed short)HIWORD(lParam), button))
1491 * Shift-clicks count as middle-clicks, since otherwise
1492 * two-button Windows users won't have any kind of
1493 * middle click to use.
1495 if (message == WM_MBUTTONUP || (wParam & MK_SHIFT))
1496 button = MIDDLE_RELEASE;
1497 else if (message == WM_RBUTTONUP || is_alt_pressed())
1498 button = RIGHT_RELEASE;
1500 button = LEFT_RELEASE;
1502 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1503 (signed short)HIWORD(lParam), button))
1513 if (wParam & (MK_MBUTTON | MK_SHIFT))
1514 button = MIDDLE_DRAG;
1515 else if (wParam & MK_RBUTTON || is_alt_pressed())
1516 button = RIGHT_DRAG;
1520 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1521 (signed short)HIWORD(lParam), button))
1526 if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam))
1531 DWORD now = GetTickCount();
1532 float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F;
1533 midend_timer(fe->me, elapsed);
1534 fe->timer_last_tickcount = now;
1539 return DefWindowProc(hwnd, message, wParam, lParam);
1542 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
1547 InitCommonControls();
1553 wndclass.lpfnWndProc = WndProc;
1554 wndclass.cbClsExtra = 0;
1555 wndclass.cbWndExtra = 0;
1556 wndclass.hInstance = inst;
1557 wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION);
1558 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1559 wndclass.hbrBackground = NULL;
1560 wndclass.lpszMenuName = NULL;
1561 wndclass.lpszClassName = thegame.name;
1563 RegisterClass(&wndclass);
1566 while (*cmdline && isspace(*cmdline))
1569 if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) {
1571 sprintf(buf, "%.100s Error", thegame.name);
1572 MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
1576 while (GetMessage(&msg, NULL, 0, 0)) {
1577 DispatchMessage(&msg);