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);
95 HWND hwnd, statusbar, cfgbox;
97 HBITMAP bitmap, prevbm;
104 DWORD timer_last_tickcount;
106 game_params **presets;
108 int nfonts, fontsize;
110 struct cfg_aux *cfgaux;
111 int cfg_which, dlg_done;
114 int help_has_contents;
118 void fatal(char *fmt, ...)
124 vsprintf(buf, fmt, ap);
127 MessageBox(NULL, buf, "Fatal error", MB_ICONEXCLAMATION | MB_OK);
132 void get_random_seed(void **randseed, int *randseedsize)
134 time_t *tp = snew(time_t);
136 *randseed = (void *)tp;
137 *randseedsize = sizeof(time_t);
140 void status_bar(frontend *fe, char *text)
142 char *rewritten = midend_rewrite_statusbar(fe->me, text);
143 if (!fe->laststatus || strcmp(rewritten, fe->laststatus)) {
144 SetWindowText(fe->statusbar, rewritten);
145 sfree(fe->laststatus);
146 fe->laststatus = rewritten;
152 void frontend_default_colour(frontend *fe, float *output)
154 DWORD c = GetSysColor(COLOR_MENU); /* ick */
156 output[0] = (float)(GetRValue(c) / 255.0);
157 output[1] = (float)(GetGValue(c) / 255.0);
158 output[2] = (float)(GetBValue(c) / 255.0);
161 void clip(frontend *fe, int x, int y, int w, int h)
163 IntersectClipRect(fe->hdc_bm, x, y, x+w, y+h);
166 void unclip(frontend *fe)
168 SelectClipRgn(fe->hdc_bm, NULL);
171 void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize,
172 int align, int colour, char *text)
177 * Find or create the font.
179 for (i = 0; i < fe->nfonts; i++)
180 if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
183 if (i == fe->nfonts) {
184 if (fe->fontsize <= fe->nfonts) {
185 fe->fontsize = fe->nfonts + 10;
186 fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
191 fe->fonts[i].type = fonttype;
192 fe->fonts[i].size = fontsize;
194 fe->fonts[i].font = CreateFont(-fontsize, 0, 0, 0, FW_BOLD,
195 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
196 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
198 (fonttype == FONT_FIXED ?
199 FIXED_PITCH | FF_DONTCARE :
200 VARIABLE_PITCH | FF_SWISS),
205 * Position and draw the text.
212 oldfont = SelectObject(fe->hdc_bm, fe->fonts[i].font);
213 if (GetTextMetrics(fe->hdc_bm, &tm)) {
214 if (align & ALIGN_VCENTRE)
215 y -= (tm.tmAscent+tm.tmDescent)/2;
219 if (GetTextExtentPoint32(fe->hdc_bm, text, strlen(text), &size)) {
220 if (align & ALIGN_HCENTRE)
222 else if (align & ALIGN_HRIGHT)
225 SetBkMode(fe->hdc_bm, TRANSPARENT);
226 SetTextColor(fe->hdc_bm, fe->colours[colour]);
227 TextOut(fe->hdc_bm, x, y, text, strlen(text));
228 SelectObject(fe->hdc_bm, oldfont);
232 void draw_rect(frontend *fe, int x, int y, int w, int h, int colour)
234 if (w == 1 && h == 1) {
236 * Rectangle() appears to get uppity if asked to draw a 1x1
237 * rectangle, presumably on the grounds that that's beneath
238 * its dignity and you ought to be using SetPixel instead.
241 SetPixel(fe->hdc_bm, x, y, fe->colours[colour]);
243 HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[colour]);
244 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
245 Rectangle(fe->hdc_bm, x, y, x+w, y+h);
246 SelectObject(fe->hdc_bm, oldbrush);
247 SelectObject(fe->hdc_bm, oldpen);
251 void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour)
253 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
254 MoveToEx(fe->hdc_bm, x1, y1, NULL);
255 LineTo(fe->hdc_bm, x2, y2);
256 SetPixel(fe->hdc_bm, x2, y2, fe->colours[colour]);
257 SelectObject(fe->hdc_bm, oldpen);
260 void draw_polygon(frontend *fe, int *coords, int npoints,
261 int fill, int colour)
263 POINT *pts = snewn(npoints+1, POINT);
266 for (i = 0; i <= npoints; i++) {
267 int j = (i < npoints ? i : 0);
268 pts[i].x = coords[j*2];
269 pts[i].y = coords[j*2+1];
273 HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[colour]);
274 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
275 Polygon(fe->hdc_bm, pts, npoints);
276 SelectObject(fe->hdc_bm, oldbrush);
277 SelectObject(fe->hdc_bm, oldpen);
279 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
280 Polyline(fe->hdc_bm, pts, npoints+1);
281 SelectObject(fe->hdc_bm, oldpen);
287 void start_draw(frontend *fe)
290 hdc_win = GetDC(fe->hwnd);
291 fe->hdc_bm = CreateCompatibleDC(hdc_win);
292 fe->prevbm = SelectObject(fe->hdc_bm, fe->bitmap);
293 ReleaseDC(fe->hwnd, hdc_win);
295 SetMapMode(fe->hdc_bm, MM_TEXT);
298 void draw_update(frontend *fe, int x, int y, int w, int h)
307 InvalidateRect(fe->hwnd, &r, FALSE);
310 void end_draw(frontend *fe)
312 SelectObject(fe->hdc_bm, fe->prevbm);
313 DeleteDC(fe->hdc_bm);
315 DeleteObject(fe->clip);
320 void deactivate_timer(frontend *fe)
322 KillTimer(fe->hwnd, fe->timer);
326 void activate_timer(frontend *fe)
329 fe->timer = SetTimer(fe->hwnd, fe->timer, 20, NULL);
330 fe->timer_last_tickcount = GetTickCount();
334 void write_clip(HWND hwnd, char *data)
342 * Windows expects CRLF in the clipboard, so we must convert
343 * any \n that has come out of the puzzle backend.
346 for (i = 0; data[i]; i++) {
351 data2 = snewn(len+1, char);
353 for (i = 0; data[i]; i++) {
356 data2[j++] = data[i];
361 clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
364 lock = GlobalLock(clipdata);
367 memcpy(lock, data2, len);
368 ((unsigned char *) lock)[len] = 0;
369 GlobalUnlock(clipdata);
371 if (OpenClipboard(hwnd)) {
373 SetClipboardData(CF_TEXT, clipdata);
376 GlobalFree(clipdata);
382 * See if we can find a help file.
384 static void find_help_file(frontend *fe)
386 char b[2048], *p, *q, *r;
388 if (!fe->help_path) {
389 GetModuleFileName(NULL, b, sizeof(b) - 1);
391 p = strrchr(b, '\\');
392 if (p && p >= r) r = p+1;
394 if (q && q >= r) r = q+1;
395 strcpy(r, HELP_FILE_NAME);
396 if ( (fp = fopen(b, "r")) != NULL) {
397 fe->help_path = dupstr(b);
400 fe->help_path = NULL;
401 strcpy(r, HELP_CNT_NAME);
402 if ( (fp = fopen(b, "r")) != NULL) {
403 fe->help_has_contents = TRUE;
406 fe->help_has_contents = FALSE;
410 static void check_window_size(frontend *fe, int *px, int *py)
417 GetWindowRect(fe->statusbar, &sr);
418 sy = sr.bottom - sr.top;
424 * See if we actually got the window size we wanted, and adjust
425 * the puzzle size if not.
427 GetClientRect(fe->hwnd, &r);
428 x = r.right - r.left;
429 y = r.bottom - r.top - sy;
430 midend_size(fe->me, &x, &y, FALSE);
431 if (x != r.right - r.left || y != r.bottom - r.top) {
433 * Resize the window, now we know what size we _really_
439 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
440 (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED),
442 SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left, r.bottom - r.top,
443 SWP_NOMOVE | SWP_NOZORDER);
447 GetClientRect(fe->hwnd, &r);
448 SetWindowPos(fe->statusbar, NULL, 0, r.bottom-r.top-sy, r.right-r.left,
456 static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
465 fe->me = midend_new(fe, &thegame);
468 *error = midend_game_id(fe->me, game_id);
476 fe->help_path = NULL;
480 midend_new_game(fe->me);
485 fe->nfonts = fe->fontsize = 0;
487 fe->laststatus = NULL;
493 colours = midend_colours(fe->me, &ncolours);
495 fe->colours = snewn(ncolours, COLORREF);
496 fe->brushes = snewn(ncolours, HBRUSH);
497 fe->pens = snewn(ncolours, HPEN);
499 for (i = 0; i < ncolours; i++) {
500 fe->colours[i] = RGB(255 * colours[i*3+0],
501 255 * colours[i*3+1],
502 255 * colours[i*3+2]);
503 fe->brushes[i] = CreateSolidBrush(fe->colours[i]);
504 fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]);
508 x = y = INT_MAX; /* find puzzle's preferred size */
509 midend_size(fe->me, &x, &y, FALSE);
514 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
515 (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED),
518 fe->hwnd = CreateWindowEx(0, thegame.name, thegame.name,
519 WS_OVERLAPPEDWINDOW &~
520 (WS_THICKFRAME | WS_MAXIMIZEBOX),
521 CW_USEDEFAULT, CW_USEDEFAULT,
522 r.right - r.left, r.bottom - r.top,
523 NULL, NULL, inst, NULL);
525 if (midend_wants_statusbar(fe->me))
526 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh",
527 WS_CHILD | WS_VISIBLE,
528 0, 0, 0, 0, /* status bar does these */
529 fe->hwnd, NULL, inst, NULL);
531 fe->statusbar = NULL;
534 HMENU bar = CreateMenu();
535 HMENU menu = CreateMenu();
537 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Game");
538 AppendMenu(menu, MF_ENABLED, IDM_NEW, "New");
539 AppendMenu(menu, MF_ENABLED, IDM_RESTART, "Restart");
540 AppendMenu(menu, MF_ENABLED, IDM_DESC, "Specific...");
541 AppendMenu(menu, MF_ENABLED, IDM_SEED, "Random Seed...");
543 if ((fe->npresets = midend_num_presets(fe->me)) > 0 ||
544 thegame.can_configure) {
545 HMENU sub = CreateMenu();
548 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "Type");
550 fe->presets = snewn(fe->npresets, game_params *);
552 for (i = 0; i < fe->npresets; i++) {
555 midend_fetch_preset(fe->me, i, &name, &fe->presets[i]);
558 * FIXME: we ought to go through and do something
559 * with ampersands here.
562 AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name);
565 if (thegame.can_configure) {
566 AppendMenu(sub, MF_ENABLED, IDM_CONFIG, "Custom...");
570 AppendMenu(menu, MF_SEPARATOR, 0, 0);
571 AppendMenu(menu, MF_ENABLED, IDM_UNDO, "Undo");
572 AppendMenu(menu, MF_ENABLED, IDM_REDO, "Redo");
573 if (thegame.can_format_as_text) {
574 AppendMenu(menu, MF_SEPARATOR, 0, 0);
575 AppendMenu(menu, MF_ENABLED, IDM_COPY, "Copy");
577 if (thegame.can_solve) {
578 AppendMenu(menu, MF_SEPARATOR, 0, 0);
579 AppendMenu(menu, MF_ENABLED, IDM_SOLVE, "Solve");
581 AppendMenu(menu, MF_SEPARATOR, 0, 0);
582 AppendMenu(menu, MF_ENABLED, IDM_QUIT, "Exit");
584 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Help");
585 AppendMenu(menu, MF_ENABLED, IDM_ABOUT, "About");
587 AppendMenu(menu, MF_SEPARATOR, 0, 0);
588 AppendMenu(menu, MF_ENABLED, IDM_HELPC, "Contents");
589 if (thegame.winhelp_topic) {
591 assert(thegame.name);
592 item = snewn(9+strlen(thegame.name), char); /*ick*/
593 sprintf(item, "Help on %s", thegame.name);
594 AppendMenu(menu, MF_ENABLED, IDM_GAMEHELP, item);
598 SetMenu(fe->hwnd, bar);
601 check_window_size(fe, &x, &y);
603 hdc = GetDC(fe->hwnd);
604 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
605 ReleaseDC(fe->hwnd, hdc);
607 SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe);
609 ShowWindow(fe->hwnd, SW_NORMAL);
610 SetForegroundWindow(fe->hwnd);
612 midend_redraw(fe->me);
617 static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg,
618 WPARAM wParam, LPARAM lParam)
620 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
627 if ((HIWORD(wParam) == BN_CLICKED ||
628 HIWORD(wParam) == BN_DOUBLECLICKED) &&
629 LOWORD(wParam) == IDOK)
641 static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg,
642 WPARAM wParam, LPARAM lParam)
644 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
654 * OK and Cancel are special cases.
656 if ((HIWORD(wParam) == BN_CLICKED ||
657 HIWORD(wParam) == BN_DOUBLECLICKED) &&
658 (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) {
659 if (LOWORD(wParam) == IDOK) {
660 char *err = midend_set_config(fe->me, fe->cfg_which, fe->cfg);
663 MessageBox(hwnd, err, "Validation error",
664 MB_ICONERROR | MB_OK);
675 * First find the control whose id this is.
677 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
678 if (j->ctlid == LOWORD(wParam))
681 if (i->type == C_END)
682 return 0; /* not our problem */
684 if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) {
686 GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer));
687 buffer[lenof(buffer)-1] = '\0';
689 i->sval = dupstr(buffer);
690 } else if (i->type == C_BOOLEAN &&
691 (HIWORD(wParam) == BN_CLICKED ||
692 HIWORD(wParam) == BN_DOUBLECLICKED)) {
693 i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid);
694 } else if (i->type == C_CHOICES &&
695 HIWORD(wParam) == CBN_SELCHANGE) {
696 i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid,
710 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
711 char *wclass, int wstyle,
712 int exstyle, const char *wtext, int wid)
715 ret = CreateWindowEx(exstyle, wclass, wtext,
716 wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1,
717 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
718 SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0));
722 static void about(frontend *fe)
732 int winwidth, winheight, y;
733 int height, width, maxwid;
734 const char *strings[16];
739 sprintf(titlebuf, "About %.250s", thegame.name);
741 strings[nstrings++] = thegame.name;
742 strings[nstrings++] = "from Simon Tatham's Portable Puzzle Collection";
743 strings[nstrings++] = ver;
745 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
746 wc.lpfnWndProc = DefDlgProc;
748 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
749 wc.hInstance = fe->inst;
751 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
752 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
753 wc.lpszMenuName = NULL;
754 wc.lpszClassName = "GameAboutBox";
757 hdc = GetDC(fe->hwnd);
758 SetMapMode(hdc, MM_TEXT);
760 fe->dlg_done = FALSE;
762 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
764 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
765 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
770 oldfont = SelectObject(hdc, fe->cfgfont);
771 if (GetTextMetrics(hdc, &tm)) {
772 height = tm.tmAscent + tm.tmDescent;
773 width = tm.tmAveCharWidth;
779 * Figure out the layout of the About box by measuring the
780 * length of each piece of text.
783 winheight = height/2;
785 for (i = 0; i < nstrings; i++) {
786 if (GetTextExtentPoint32(hdc, strings[i], strlen(strings[i]), &size))
787 lengths[i] = size.cx;
789 lengths[i] = 0; /* *shrug* */
790 if (maxwid < lengths[i])
792 winheight += height * 3 / 2 + (height / 2);
795 winheight += height + height * 7 / 4; /* OK button */
796 winwidth = maxwid + 4*width;
798 SelectObject(hdc, oldfont);
799 ReleaseDC(fe->hwnd, hdc);
802 * Create the dialog, now that we know its size.
809 r.bottom = winheight;
811 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
812 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
813 WS_CAPTION | WS_SYSMENU*/) &~
814 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
818 * Centre the dialog on its parent window.
822 GetWindowRect(fe->hwnd, &r2);
823 r.left = (r2.left + r2.right - r.right) / 2;
824 r.top = (r2.top + r2.bottom - r.bottom) / 2;
828 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, titlebuf,
829 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
830 WS_CAPTION | WS_SYSMENU,
832 r.right-r.left, r.bottom-r.top,
833 fe->hwnd, NULL, fe->inst, NULL);
836 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
838 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
839 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)AboutDlgProc);
843 for (i = 0; i < nstrings; i++) {
844 int border = width*2 + (maxwid - lengths[i]) / 2;
845 mkctrl(fe, border, border+lengths[i], y+height*1/8, y+height*9/8,
846 "Static", 0, 0, strings[i], id++);
849 assert(y < winheight);
853 y += height/2; /* extra space before OK */
854 mkctrl(fe, width*2, maxwid+width*2, y, y+height*7/4, "BUTTON",
855 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
858 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
860 EnableWindow(fe->hwnd, FALSE);
861 ShowWindow(fe->cfgbox, SW_NORMAL);
862 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
863 if (!IsDialogMessage(fe->cfgbox, &msg))
864 DispatchMessage(&msg);
868 EnableWindow(fe->hwnd, TRUE);
869 SetForegroundWindow(fe->hwnd);
870 DestroyWindow(fe->cfgbox);
871 DeleteObject(fe->cfgfont);
874 static int get_config(frontend *fe, int which)
887 int winwidth, winheight, col1l, col1r, col2l, col2r, y;
888 int height, width, maxlabel, maxcheckbox;
890 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
891 wc.lpfnWndProc = DefDlgProc;
893 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
894 wc.hInstance = fe->inst;
896 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
897 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
898 wc.lpszMenuName = NULL;
899 wc.lpszClassName = "GameConfigBox";
902 hdc = GetDC(fe->hwnd);
903 SetMapMode(hdc, MM_TEXT);
905 fe->dlg_done = FALSE;
907 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
909 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
910 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
915 oldfont = SelectObject(hdc, fe->cfgfont);
916 if (GetTextMetrics(hdc, &tm)) {
917 height = tm.tmAscent + tm.tmDescent;
918 width = tm.tmAveCharWidth;
923 fe->cfg = midend_get_config(fe->me, which, &title);
924 fe->cfg_which = which;
927 * Figure out the layout of the config box by measuring the
928 * length of each piece of text.
930 maxlabel = maxcheckbox = 0;
931 winheight = height/2;
933 for (i = fe->cfg; i->type != C_END; i++) {
938 * Both these control types have a label filling only
939 * the left-hand column of the box.
941 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
944 winheight += height * 3 / 2 + (height / 2);
949 * Checkboxes take up the whole of the box width.
951 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
952 maxcheckbox < size.cx)
953 maxcheckbox = size.cx;
954 winheight += height + (height / 2);
959 winheight += height + height * 7 / 4; /* OK / Cancel buttons */
962 col1r = col1l + maxlabel;
963 col2l = col1r + 2*width;
964 col2r = col2l + 30*width;
965 if (col2r < col1l+2*height+maxcheckbox)
966 col2r = col1l+2*height+maxcheckbox;
967 winwidth = col2r + 2*width;
969 SelectObject(hdc, oldfont);
970 ReleaseDC(fe->hwnd, hdc);
973 * Create the dialog, now that we know its size.
980 r.bottom = winheight;
982 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
983 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
984 WS_CAPTION | WS_SYSMENU*/) &~
985 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
989 * Centre the dialog on its parent window.
993 GetWindowRect(fe->hwnd, &r2);
994 r.left = (r2.left + r2.right - r.right) / 2;
995 r.top = (r2.top + r2.bottom - r.bottom) / 2;
999 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title,
1000 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1001 WS_CAPTION | WS_SYSMENU,
1003 r.right-r.left, r.bottom-r.top,
1004 fe->hwnd, NULL, fe->inst, NULL);
1008 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
1010 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
1011 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc);
1014 * Count the controls so we can allocate cfgaux.
1016 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
1018 fe->cfgaux = snewn(nctrls, struct cfg_aux);
1022 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
1026 * Edit box with a label beside it.
1028 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
1029 "Static", 0, 0, i->name, id++);
1030 ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2,
1031 "EDIT", WS_TABSTOP | ES_AUTOHSCROLL,
1032 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
1033 SetWindowText(ctl, i->sval);
1041 mkctrl(fe, col1l, col2r, y, y+height, "BUTTON",
1042 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
1043 0, i->name, (j->ctlid = id++));
1044 CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0));
1050 * Drop-down list with a label beside it.
1052 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
1053 "STATIC", 0, 0, i->name, id++);
1054 ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2,
1055 "COMBOBOX", WS_TABSTOP |
1056 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
1057 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
1059 char c, *p, *q, *str;
1061 SendMessage(ctl, CB_RESETCONTENT, 0, 0);
1066 while (*q && *q != c) q++;
1067 str = snewn(q-p+1, char);
1068 strncpy(str, p, q-p);
1070 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str);
1077 SendMessage(ctl, CB_SETCURSEL, i->ival, 0);
1083 assert(y < winheight);
1087 y += height/2; /* extra space before OK and Cancel */
1088 mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON",
1089 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
1091 mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON",
1092 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL);
1094 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
1096 EnableWindow(fe->hwnd, FALSE);
1097 ShowWindow(fe->cfgbox, SW_NORMAL);
1098 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
1099 if (!IsDialogMessage(fe->cfgbox, &msg))
1100 DispatchMessage(&msg);
1104 EnableWindow(fe->hwnd, TRUE);
1105 SetForegroundWindow(fe->hwnd);
1106 DestroyWindow(fe->cfgbox);
1107 DeleteObject(fe->cfgfont);
1112 return (fe->dlg_done == 2);
1115 static void new_game_type(frontend *fe)
1121 midend_new_game(fe->me);
1123 midend_size(fe->me, &x, &y, FALSE);
1128 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
1129 (WS_THICKFRAME | WS_MAXIMIZEBOX |
1133 if (fe->statusbar != NULL) {
1134 GetWindowRect(fe->statusbar, &sr);
1136 sr.left = sr.right = sr.top = sr.bottom = 0;
1138 SetWindowPos(fe->hwnd, NULL, 0, 0,
1140 r.bottom - r.top + sr.bottom - sr.top,
1141 SWP_NOMOVE | SWP_NOZORDER);
1143 check_window_size(fe, &x, &y);
1145 if (fe->statusbar != NULL)
1146 SetWindowPos(fe->statusbar, NULL, 0, y, x,
1147 sr.bottom - sr.top, SWP_NOZORDER);
1149 DeleteObject(fe->bitmap);
1151 hdc = GetDC(fe->hwnd);
1152 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
1153 ReleaseDC(fe->hwnd, hdc);
1155 midend_redraw(fe->me);
1158 static int is_alt_pressed(void)
1161 int r = GetKeyboardState(keystate);
1164 if (keystate[VK_MENU] & 0x80)
1166 if (keystate[VK_RMENU] & 0x80)
1171 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
1172 WPARAM wParam, LPARAM lParam)
1174 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1178 DestroyWindow(hwnd);
1181 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
1183 if (!midend_process_key(fe->me, 0, 0, 'n'))
1187 midend_restart_game(fe->me);
1190 if (!midend_process_key(fe->me, 0, 0, 'u'))
1194 if (!midend_process_key(fe->me, 0, 0, '\x12'))
1199 char *text = midend_text_format(fe->me);
1201 write_clip(hwnd, text);
1203 MessageBeep(MB_ICONWARNING);
1209 char *msg = midend_solve(fe->me);
1211 MessageBox(hwnd, msg, "Unable to solve",
1212 MB_ICONERROR | MB_OK);
1216 if (!midend_process_key(fe->me, 0, 0, 'q'))
1220 if (get_config(fe, CFG_SETTINGS))
1224 if (get_config(fe, CFG_SEED))
1228 if (get_config(fe, CFG_DESC))
1235 assert(fe->help_path);
1236 WinHelp(hwnd, fe->help_path,
1237 fe->help_has_contents ? HELP_FINDER : HELP_CONTENTS, 0);
1240 assert(fe->help_path);
1241 assert(thegame.winhelp_topic);
1243 char *cmd = snewn(10+strlen(thegame.winhelp_topic), char);
1244 sprintf(cmd, "JI(`',`%s')", thegame.winhelp_topic);
1245 WinHelp(hwnd, fe->help_path, HELP_COMMAND, (DWORD)cmd);
1251 int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10;
1253 if (p >= 0 && p < fe->npresets) {
1254 midend_set_params(fe->me, fe->presets[p]);
1270 hdc = BeginPaint(hwnd, &p);
1271 hdc2 = CreateCompatibleDC(hdc);
1272 prevbm = SelectObject(hdc2, fe->bitmap);
1274 p.rcPaint.left, p.rcPaint.top,
1275 p.rcPaint.right - p.rcPaint.left,
1276 p.rcPaint.bottom - p.rcPaint.top,
1278 p.rcPaint.left, p.rcPaint.top,
1280 SelectObject(hdc2, prevbm);
1289 int r = GetKeyboardState(keystate);
1290 int shift = (r && (keystate[VK_SHIFT] & 0x80)) ? MOD_SHFT : 0;
1291 int ctrl = (r && (keystate[VK_CONTROL] & 0x80)) ? MOD_CTRL : 0;
1295 if (!(lParam & 0x01000000))
1296 key = MOD_NUM_KEYPAD | '4';
1298 key = shift | ctrl | CURSOR_LEFT;
1301 if (!(lParam & 0x01000000))
1302 key = MOD_NUM_KEYPAD | '6';
1304 key = shift | ctrl | CURSOR_RIGHT;
1307 if (!(lParam & 0x01000000))
1308 key = MOD_NUM_KEYPAD | '8';
1310 key = shift | ctrl | CURSOR_UP;
1313 if (!(lParam & 0x01000000))
1314 key = MOD_NUM_KEYPAD | '2';
1316 key = shift | ctrl | CURSOR_DOWN;
1319 * Diagonal keys on the numeric keypad.
1322 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '9';
1325 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '3';
1328 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '7';
1331 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '1';
1334 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '0';
1337 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '5';
1340 * Numeric keypad keys with Num Lock on.
1342 case VK_NUMPAD4: key = MOD_NUM_KEYPAD | '4'; break;
1343 case VK_NUMPAD6: key = MOD_NUM_KEYPAD | '6'; break;
1344 case VK_NUMPAD8: key = MOD_NUM_KEYPAD | '8'; break;
1345 case VK_NUMPAD2: key = MOD_NUM_KEYPAD | '2'; break;
1346 case VK_NUMPAD5: key = MOD_NUM_KEYPAD | '5'; break;
1347 case VK_NUMPAD9: key = MOD_NUM_KEYPAD | '9'; break;
1348 case VK_NUMPAD3: key = MOD_NUM_KEYPAD | '3'; break;
1349 case VK_NUMPAD7: key = MOD_NUM_KEYPAD | '7'; break;
1350 case VK_NUMPAD1: key = MOD_NUM_KEYPAD | '1'; break;
1351 case VK_NUMPAD0: key = MOD_NUM_KEYPAD | '0'; break;
1355 if (!midend_process_key(fe->me, 0, 0, key))
1360 m.message = WM_KEYDOWN;
1362 m.lParam = lParam & 0xdfff;
1363 TranslateMessage(&m);
1367 case WM_LBUTTONDOWN:
1368 case WM_RBUTTONDOWN:
1369 case WM_MBUTTONDOWN:
1374 * Shift-clicks count as middle-clicks, since otherwise
1375 * two-button Windows users won't have any kind of
1376 * middle click to use.
1378 if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT))
1379 button = MIDDLE_BUTTON;
1380 else if (message == WM_RBUTTONDOWN || is_alt_pressed())
1381 button = RIGHT_BUTTON;
1383 button = LEFT_BUTTON;
1385 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1386 (signed short)HIWORD(lParam), button))
1399 * Shift-clicks count as middle-clicks, since otherwise
1400 * two-button Windows users won't have any kind of
1401 * middle click to use.
1403 if (message == WM_MBUTTONUP || (wParam & MK_SHIFT))
1404 button = MIDDLE_RELEASE;
1405 else if (message == WM_RBUTTONUP || is_alt_pressed())
1406 button = RIGHT_RELEASE;
1408 button = LEFT_RELEASE;
1410 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1411 (signed short)HIWORD(lParam), button))
1421 if (wParam & (MK_MBUTTON | MK_SHIFT))
1422 button = MIDDLE_DRAG;
1423 else if (wParam & MK_RBUTTON || is_alt_pressed())
1424 button = RIGHT_DRAG;
1428 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1429 (signed short)HIWORD(lParam), button))
1434 if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam))
1439 DWORD now = GetTickCount();
1440 float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F;
1441 midend_timer(fe->me, elapsed);
1442 fe->timer_last_tickcount = now;
1447 return DefWindowProc(hwnd, message, wParam, lParam);
1450 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
1455 InitCommonControls();
1461 wndclass.lpfnWndProc = WndProc;
1462 wndclass.cbClsExtra = 0;
1463 wndclass.cbWndExtra = 0;
1464 wndclass.hInstance = inst;
1465 wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION);
1466 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1467 wndclass.hbrBackground = NULL;
1468 wndclass.lpszMenuName = NULL;
1469 wndclass.lpszClassName = thegame.name;
1471 RegisterClass(&wndclass);
1474 while (*cmdline && isspace(*cmdline))
1477 if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) {
1479 sprintf(buf, "%.100s Error", thegame.name);
1480 MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
1484 while (GetMessage(&msg, NULL, 0, 0)) {
1485 DispatchMessage(&msg);