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)) {
527 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh",
528 WS_CHILD | WS_VISIBLE,
529 0, 0, 0, 0, /* status bar does these */
530 fe->hwnd, NULL, inst, NULL);
532 * Now resize the window to take account of the status bar.
534 GetWindowRect(fe->statusbar, &sr);
535 GetWindowRect(fe->hwnd, &r);
536 SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left,
537 r.bottom - r.top + sr.bottom - sr.top,
538 SWP_NOMOVE | SWP_NOZORDER);
540 fe->statusbar = NULL;
544 HMENU bar = CreateMenu();
545 HMENU menu = CreateMenu();
547 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Game");
548 AppendMenu(menu, MF_ENABLED, IDM_NEW, "New");
549 AppendMenu(menu, MF_ENABLED, IDM_RESTART, "Restart");
550 AppendMenu(menu, MF_ENABLED, IDM_DESC, "Specific...");
551 AppendMenu(menu, MF_ENABLED, IDM_SEED, "Random Seed...");
553 if ((fe->npresets = midend_num_presets(fe->me)) > 0 ||
554 thegame.can_configure) {
555 HMENU sub = CreateMenu();
558 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "Type");
560 fe->presets = snewn(fe->npresets, game_params *);
562 for (i = 0; i < fe->npresets; i++) {
565 midend_fetch_preset(fe->me, i, &name, &fe->presets[i]);
568 * FIXME: we ought to go through and do something
569 * with ampersands here.
572 AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name);
575 if (thegame.can_configure) {
576 AppendMenu(sub, MF_ENABLED, IDM_CONFIG, "Custom...");
580 AppendMenu(menu, MF_SEPARATOR, 0, 0);
581 AppendMenu(menu, MF_ENABLED, IDM_UNDO, "Undo");
582 AppendMenu(menu, MF_ENABLED, IDM_REDO, "Redo");
583 if (thegame.can_format_as_text) {
584 AppendMenu(menu, MF_SEPARATOR, 0, 0);
585 AppendMenu(menu, MF_ENABLED, IDM_COPY, "Copy");
587 if (thegame.can_solve) {
588 AppendMenu(menu, MF_SEPARATOR, 0, 0);
589 AppendMenu(menu, MF_ENABLED, IDM_SOLVE, "Solve");
591 AppendMenu(menu, MF_SEPARATOR, 0, 0);
592 AppendMenu(menu, MF_ENABLED, IDM_QUIT, "Exit");
594 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Help");
595 AppendMenu(menu, MF_ENABLED, IDM_ABOUT, "About");
597 AppendMenu(menu, MF_SEPARATOR, 0, 0);
598 AppendMenu(menu, MF_ENABLED, IDM_HELPC, "Contents");
599 if (thegame.winhelp_topic) {
601 assert(thegame.name);
602 item = snewn(9+strlen(thegame.name), char); /*ick*/
603 sprintf(item, "Help on %s", thegame.name);
604 AppendMenu(menu, MF_ENABLED, IDM_GAMEHELP, item);
608 SetMenu(fe->hwnd, bar);
611 check_window_size(fe, &x, &y);
613 hdc = GetDC(fe->hwnd);
614 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
615 ReleaseDC(fe->hwnd, hdc);
617 SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe);
619 ShowWindow(fe->hwnd, SW_NORMAL);
620 SetForegroundWindow(fe->hwnd);
622 midend_redraw(fe->me);
627 static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg,
628 WPARAM wParam, LPARAM lParam)
630 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
637 if ((HIWORD(wParam) == BN_CLICKED ||
638 HIWORD(wParam) == BN_DOUBLECLICKED) &&
639 LOWORD(wParam) == IDOK)
651 static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg,
652 WPARAM wParam, LPARAM lParam)
654 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
664 * OK and Cancel are special cases.
666 if ((HIWORD(wParam) == BN_CLICKED ||
667 HIWORD(wParam) == BN_DOUBLECLICKED) &&
668 (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) {
669 if (LOWORD(wParam) == IDOK) {
670 char *err = midend_set_config(fe->me, fe->cfg_which, fe->cfg);
673 MessageBox(hwnd, err, "Validation error",
674 MB_ICONERROR | MB_OK);
685 * First find the control whose id this is.
687 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
688 if (j->ctlid == LOWORD(wParam))
691 if (i->type == C_END)
692 return 0; /* not our problem */
694 if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) {
696 GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer));
697 buffer[lenof(buffer)-1] = '\0';
699 i->sval = dupstr(buffer);
700 } else if (i->type == C_BOOLEAN &&
701 (HIWORD(wParam) == BN_CLICKED ||
702 HIWORD(wParam) == BN_DOUBLECLICKED)) {
703 i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid);
704 } else if (i->type == C_CHOICES &&
705 HIWORD(wParam) == CBN_SELCHANGE) {
706 i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid,
720 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
721 char *wclass, int wstyle,
722 int exstyle, const char *wtext, int wid)
725 ret = CreateWindowEx(exstyle, wclass, wtext,
726 wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1,
727 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
728 SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0));
732 static void about(frontend *fe)
742 int winwidth, winheight, y;
743 int height, width, maxwid;
744 const char *strings[16];
749 sprintf(titlebuf, "About %.250s", thegame.name);
751 strings[nstrings++] = thegame.name;
752 strings[nstrings++] = "from Simon Tatham's Portable Puzzle Collection";
753 strings[nstrings++] = ver;
755 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
756 wc.lpfnWndProc = DefDlgProc;
758 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
759 wc.hInstance = fe->inst;
761 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
762 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
763 wc.lpszMenuName = NULL;
764 wc.lpszClassName = "GameAboutBox";
767 hdc = GetDC(fe->hwnd);
768 SetMapMode(hdc, MM_TEXT);
770 fe->dlg_done = FALSE;
772 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
774 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
775 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
780 oldfont = SelectObject(hdc, fe->cfgfont);
781 if (GetTextMetrics(hdc, &tm)) {
782 height = tm.tmAscent + tm.tmDescent;
783 width = tm.tmAveCharWidth;
789 * Figure out the layout of the About box by measuring the
790 * length of each piece of text.
793 winheight = height/2;
795 for (i = 0; i < nstrings; i++) {
796 if (GetTextExtentPoint32(hdc, strings[i], strlen(strings[i]), &size))
797 lengths[i] = size.cx;
799 lengths[i] = 0; /* *shrug* */
800 if (maxwid < lengths[i])
802 winheight += height * 3 / 2 + (height / 2);
805 winheight += height + height * 7 / 4; /* OK button */
806 winwidth = maxwid + 4*width;
808 SelectObject(hdc, oldfont);
809 ReleaseDC(fe->hwnd, hdc);
812 * Create the dialog, now that we know its size.
819 r.bottom = winheight;
821 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
822 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
823 WS_CAPTION | WS_SYSMENU*/) &~
824 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
828 * Centre the dialog on its parent window.
832 GetWindowRect(fe->hwnd, &r2);
833 r.left = (r2.left + r2.right - r.right) / 2;
834 r.top = (r2.top + r2.bottom - r.bottom) / 2;
838 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, titlebuf,
839 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
840 WS_CAPTION | WS_SYSMENU,
842 r.right-r.left, r.bottom-r.top,
843 fe->hwnd, NULL, fe->inst, NULL);
846 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
848 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
849 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)AboutDlgProc);
853 for (i = 0; i < nstrings; i++) {
854 int border = width*2 + (maxwid - lengths[i]) / 2;
855 mkctrl(fe, border, border+lengths[i], y+height*1/8, y+height*9/8,
856 "Static", 0, 0, strings[i], id++);
859 assert(y < winheight);
863 y += height/2; /* extra space before OK */
864 mkctrl(fe, width*2, maxwid+width*2, y, y+height*7/4, "BUTTON",
865 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
868 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
870 EnableWindow(fe->hwnd, FALSE);
871 ShowWindow(fe->cfgbox, SW_NORMAL);
872 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
873 if (!IsDialogMessage(fe->cfgbox, &msg))
874 DispatchMessage(&msg);
878 EnableWindow(fe->hwnd, TRUE);
879 SetForegroundWindow(fe->hwnd);
880 DestroyWindow(fe->cfgbox);
881 DeleteObject(fe->cfgfont);
884 static int get_config(frontend *fe, int which)
897 int winwidth, winheight, col1l, col1r, col2l, col2r, y;
898 int height, width, maxlabel, maxcheckbox;
900 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
901 wc.lpfnWndProc = DefDlgProc;
903 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
904 wc.hInstance = fe->inst;
906 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
907 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
908 wc.lpszMenuName = NULL;
909 wc.lpszClassName = "GameConfigBox";
912 hdc = GetDC(fe->hwnd);
913 SetMapMode(hdc, MM_TEXT);
915 fe->dlg_done = FALSE;
917 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
919 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
920 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
925 oldfont = SelectObject(hdc, fe->cfgfont);
926 if (GetTextMetrics(hdc, &tm)) {
927 height = tm.tmAscent + tm.tmDescent;
928 width = tm.tmAveCharWidth;
933 fe->cfg = midend_get_config(fe->me, which, &title);
934 fe->cfg_which = which;
937 * Figure out the layout of the config box by measuring the
938 * length of each piece of text.
940 maxlabel = maxcheckbox = 0;
941 winheight = height/2;
943 for (i = fe->cfg; i->type != C_END; i++) {
948 * Both these control types have a label filling only
949 * the left-hand column of the box.
951 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
954 winheight += height * 3 / 2 + (height / 2);
959 * Checkboxes take up the whole of the box width.
961 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
962 maxcheckbox < size.cx)
963 maxcheckbox = size.cx;
964 winheight += height + (height / 2);
969 winheight += height + height * 7 / 4; /* OK / Cancel buttons */
972 col1r = col1l + maxlabel;
973 col2l = col1r + 2*width;
974 col2r = col2l + 30*width;
975 if (col2r < col1l+2*height+maxcheckbox)
976 col2r = col1l+2*height+maxcheckbox;
977 winwidth = col2r + 2*width;
979 SelectObject(hdc, oldfont);
980 ReleaseDC(fe->hwnd, hdc);
983 * Create the dialog, now that we know its size.
990 r.bottom = winheight;
992 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
993 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
994 WS_CAPTION | WS_SYSMENU*/) &~
995 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
999 * Centre the dialog on its parent window.
1003 GetWindowRect(fe->hwnd, &r2);
1004 r.left = (r2.left + r2.right - r.right) / 2;
1005 r.top = (r2.top + r2.bottom - r.bottom) / 2;
1009 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title,
1010 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1011 WS_CAPTION | WS_SYSMENU,
1013 r.right-r.left, r.bottom-r.top,
1014 fe->hwnd, NULL, fe->inst, NULL);
1018 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
1020 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
1021 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc);
1024 * Count the controls so we can allocate cfgaux.
1026 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
1028 fe->cfgaux = snewn(nctrls, struct cfg_aux);
1032 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
1036 * Edit box with a label beside it.
1038 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
1039 "Static", 0, 0, i->name, id++);
1040 ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2,
1041 "EDIT", WS_TABSTOP | ES_AUTOHSCROLL,
1042 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
1043 SetWindowText(ctl, i->sval);
1051 mkctrl(fe, col1l, col2r, y, y+height, "BUTTON",
1052 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
1053 0, i->name, (j->ctlid = id++));
1054 CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0));
1060 * Drop-down list with a label beside it.
1062 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
1063 "STATIC", 0, 0, i->name, id++);
1064 ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2,
1065 "COMBOBOX", WS_TABSTOP |
1066 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
1067 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
1069 char c, *p, *q, *str;
1071 SendMessage(ctl, CB_RESETCONTENT, 0, 0);
1076 while (*q && *q != c) q++;
1077 str = snewn(q-p+1, char);
1078 strncpy(str, p, q-p);
1080 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str);
1087 SendMessage(ctl, CB_SETCURSEL, i->ival, 0);
1093 assert(y < winheight);
1097 y += height/2; /* extra space before OK and Cancel */
1098 mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON",
1099 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
1101 mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON",
1102 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL);
1104 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
1106 EnableWindow(fe->hwnd, FALSE);
1107 ShowWindow(fe->cfgbox, SW_NORMAL);
1108 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
1109 if (!IsDialogMessage(fe->cfgbox, &msg))
1110 DispatchMessage(&msg);
1114 EnableWindow(fe->hwnd, TRUE);
1115 SetForegroundWindow(fe->hwnd);
1116 DestroyWindow(fe->cfgbox);
1117 DeleteObject(fe->cfgfont);
1122 return (fe->dlg_done == 2);
1125 static void new_game_type(frontend *fe)
1131 midend_new_game(fe->me);
1133 midend_size(fe->me, &x, &y, FALSE);
1138 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
1139 (WS_THICKFRAME | WS_MAXIMIZEBOX |
1143 if (fe->statusbar != NULL) {
1144 GetWindowRect(fe->statusbar, &sr);
1146 sr.left = sr.right = sr.top = sr.bottom = 0;
1148 SetWindowPos(fe->hwnd, NULL, 0, 0,
1150 r.bottom - r.top + sr.bottom - sr.top,
1151 SWP_NOMOVE | SWP_NOZORDER);
1153 check_window_size(fe, &x, &y);
1155 if (fe->statusbar != NULL)
1156 SetWindowPos(fe->statusbar, NULL, 0, y, x,
1157 sr.bottom - sr.top, SWP_NOZORDER);
1159 DeleteObject(fe->bitmap);
1161 hdc = GetDC(fe->hwnd);
1162 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
1163 ReleaseDC(fe->hwnd, hdc);
1165 midend_redraw(fe->me);
1168 static int is_alt_pressed(void)
1171 int r = GetKeyboardState(keystate);
1174 if (keystate[VK_MENU] & 0x80)
1176 if (keystate[VK_RMENU] & 0x80)
1181 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
1182 WPARAM wParam, LPARAM lParam)
1184 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1188 DestroyWindow(hwnd);
1191 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
1193 if (!midend_process_key(fe->me, 0, 0, 'n'))
1197 midend_restart_game(fe->me);
1200 if (!midend_process_key(fe->me, 0, 0, 'u'))
1204 if (!midend_process_key(fe->me, 0, 0, '\x12'))
1209 char *text = midend_text_format(fe->me);
1211 write_clip(hwnd, text);
1213 MessageBeep(MB_ICONWARNING);
1219 char *msg = midend_solve(fe->me);
1221 MessageBox(hwnd, msg, "Unable to solve",
1222 MB_ICONERROR | MB_OK);
1226 if (!midend_process_key(fe->me, 0, 0, 'q'))
1230 if (get_config(fe, CFG_SETTINGS))
1234 if (get_config(fe, CFG_SEED))
1238 if (get_config(fe, CFG_DESC))
1245 assert(fe->help_path);
1246 WinHelp(hwnd, fe->help_path,
1247 fe->help_has_contents ? HELP_FINDER : HELP_CONTENTS, 0);
1250 assert(fe->help_path);
1251 assert(thegame.winhelp_topic);
1253 char *cmd = snewn(10+strlen(thegame.winhelp_topic), char);
1254 sprintf(cmd, "JI(`',`%s')", thegame.winhelp_topic);
1255 WinHelp(hwnd, fe->help_path, HELP_COMMAND, (DWORD)cmd);
1261 int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10;
1263 if (p >= 0 && p < fe->npresets) {
1264 midend_set_params(fe->me, fe->presets[p]);
1280 hdc = BeginPaint(hwnd, &p);
1281 hdc2 = CreateCompatibleDC(hdc);
1282 prevbm = SelectObject(hdc2, fe->bitmap);
1284 p.rcPaint.left, p.rcPaint.top,
1285 p.rcPaint.right - p.rcPaint.left,
1286 p.rcPaint.bottom - p.rcPaint.top,
1288 p.rcPaint.left, p.rcPaint.top,
1290 SelectObject(hdc2, prevbm);
1299 int r = GetKeyboardState(keystate);
1300 int shift = (r && (keystate[VK_SHIFT] & 0x80)) ? MOD_SHFT : 0;
1301 int ctrl = (r && (keystate[VK_CONTROL] & 0x80)) ? MOD_CTRL : 0;
1305 if (!(lParam & 0x01000000))
1306 key = MOD_NUM_KEYPAD | '4';
1308 key = shift | ctrl | CURSOR_LEFT;
1311 if (!(lParam & 0x01000000))
1312 key = MOD_NUM_KEYPAD | '6';
1314 key = shift | ctrl | CURSOR_RIGHT;
1317 if (!(lParam & 0x01000000))
1318 key = MOD_NUM_KEYPAD | '8';
1320 key = shift | ctrl | CURSOR_UP;
1323 if (!(lParam & 0x01000000))
1324 key = MOD_NUM_KEYPAD | '2';
1326 key = shift | ctrl | CURSOR_DOWN;
1329 * Diagonal keys on the numeric keypad.
1332 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '9';
1335 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '3';
1338 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '7';
1341 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '1';
1344 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '0';
1347 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '5';
1350 * Numeric keypad keys with Num Lock on.
1352 case VK_NUMPAD4: key = MOD_NUM_KEYPAD | '4'; break;
1353 case VK_NUMPAD6: key = MOD_NUM_KEYPAD | '6'; break;
1354 case VK_NUMPAD8: key = MOD_NUM_KEYPAD | '8'; break;
1355 case VK_NUMPAD2: key = MOD_NUM_KEYPAD | '2'; break;
1356 case VK_NUMPAD5: key = MOD_NUM_KEYPAD | '5'; break;
1357 case VK_NUMPAD9: key = MOD_NUM_KEYPAD | '9'; break;
1358 case VK_NUMPAD3: key = MOD_NUM_KEYPAD | '3'; break;
1359 case VK_NUMPAD7: key = MOD_NUM_KEYPAD | '7'; break;
1360 case VK_NUMPAD1: key = MOD_NUM_KEYPAD | '1'; break;
1361 case VK_NUMPAD0: key = MOD_NUM_KEYPAD | '0'; break;
1365 if (!midend_process_key(fe->me, 0, 0, key))
1370 m.message = WM_KEYDOWN;
1372 m.lParam = lParam & 0xdfff;
1373 TranslateMessage(&m);
1377 case WM_LBUTTONDOWN:
1378 case WM_RBUTTONDOWN:
1379 case WM_MBUTTONDOWN:
1384 * Shift-clicks count as middle-clicks, since otherwise
1385 * two-button Windows users won't have any kind of
1386 * middle click to use.
1388 if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT))
1389 button = MIDDLE_BUTTON;
1390 else if (message == WM_RBUTTONDOWN || is_alt_pressed())
1391 button = RIGHT_BUTTON;
1393 button = LEFT_BUTTON;
1395 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1396 (signed short)HIWORD(lParam), button))
1409 * Shift-clicks count as middle-clicks, since otherwise
1410 * two-button Windows users won't have any kind of
1411 * middle click to use.
1413 if (message == WM_MBUTTONUP || (wParam & MK_SHIFT))
1414 button = MIDDLE_RELEASE;
1415 else if (message == WM_RBUTTONUP || is_alt_pressed())
1416 button = RIGHT_RELEASE;
1418 button = LEFT_RELEASE;
1420 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1421 (signed short)HIWORD(lParam), button))
1431 if (wParam & (MK_MBUTTON | MK_SHIFT))
1432 button = MIDDLE_DRAG;
1433 else if (wParam & MK_RBUTTON || is_alt_pressed())
1434 button = RIGHT_DRAG;
1438 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1439 (signed short)HIWORD(lParam), button))
1444 if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam))
1449 DWORD now = GetTickCount();
1450 float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F;
1451 midend_timer(fe->me, elapsed);
1452 fe->timer_last_tickcount = now;
1457 return DefWindowProc(hwnd, message, wParam, lParam);
1460 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
1465 InitCommonControls();
1471 wndclass.lpfnWndProc = WndProc;
1472 wndclass.cbClsExtra = 0;
1473 wndclass.cbWndExtra = 0;
1474 wndclass.hInstance = inst;
1475 wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION);
1476 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1477 wndclass.hbrBackground = NULL;
1478 wndclass.lpszMenuName = NULL;
1479 wndclass.lpszClassName = thegame.name;
1481 RegisterClass(&wndclass);
1484 while (*cmdline && isspace(*cmdline))
1487 if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) {
1489 sprintf(buf, "%.100s Error", thegame.name);
1490 MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
1494 while (GetMessage(&msg, NULL, 0, 0)) {
1495 DispatchMessage(&msg);