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.
26 #define IDM_NEW 0x0010
27 #define IDM_RESTART 0x0020
28 #define IDM_UNDO 0x0030
29 #define IDM_REDO 0x0040
30 #define IDM_COPY 0x0050
31 #define IDM_SOLVE 0x0060
32 #define IDM_QUIT 0x0070
33 #define IDM_CONFIG 0x0080
34 #define IDM_SEED 0x0090
35 #define IDM_HELPC 0x00A0
36 #define IDM_GAMEHELP 0x00B0
37 #define IDM_PRESETS 0x0100
38 #define IDM_ABOUT 0x0110
40 #define HELP_FILE_NAME "puzzles.hlp"
41 #define HELP_CNT_NAME "puzzles.cnt"
44 static FILE *debug_fp = NULL;
45 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
46 static int debug_got_console = 0;
52 if (!debug_got_console) {
54 debug_got_console = 1;
55 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
59 debug_fp = fopen("debug.log", "w");
62 if (debug_hdl != INVALID_HANDLE_VALUE) {
63 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
69 void debug_printf(char *fmt, ...)
75 vsprintf(buf, fmt, ap);
80 #define debug(x) (debug_printf x)
100 HWND hwnd, statusbar, cfgbox;
102 HBITMAP bitmap, prevbm;
109 DWORD timer_last_tickcount;
111 game_params **presets;
113 int nfonts, fontsize;
115 struct cfg_aux *cfgaux;
116 int cfg_which, dlg_done;
119 int help_has_contents;
122 void fatal(char *fmt, ...)
128 vsprintf(buf, fmt, ap);
131 MessageBox(NULL, buf, "Fatal error", MB_ICONEXCLAMATION | MB_OK);
136 void get_random_seed(void **randseed, int *randseedsize)
138 time_t *tp = snew(time_t);
140 *randseed = (void *)tp;
141 *randseedsize = sizeof(time_t);
144 void status_bar(frontend *fe, char *text)
146 SetWindowText(fe->statusbar, text);
149 void frontend_default_colour(frontend *fe, float *output)
151 DWORD c = GetSysColor(COLOR_MENU); /* ick */
153 output[0] = (float)(GetRValue(c) / 255.0);
154 output[1] = (float)(GetGValue(c) / 255.0);
155 output[2] = (float)(GetBValue(c) / 255.0);
158 void clip(frontend *fe, int x, int y, int w, int h)
160 IntersectClipRect(fe->hdc_bm, x, y, x+w, y+h);
163 void unclip(frontend *fe)
165 SelectClipRgn(fe->hdc_bm, NULL);
168 void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize,
169 int align, int colour, char *text)
174 * Find or create the font.
176 for (i = 0; i < fe->nfonts; i++)
177 if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
180 if (i == fe->nfonts) {
181 if (fe->fontsize <= fe->nfonts) {
182 fe->fontsize = fe->nfonts + 10;
183 fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
188 fe->fonts[i].type = fonttype;
189 fe->fonts[i].size = fontsize;
191 fe->fonts[i].font = CreateFont(-fontsize, 0, 0, 0, FW_BOLD,
192 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
193 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
195 (fonttype == FONT_FIXED ?
196 FIXED_PITCH | FF_DONTCARE :
197 VARIABLE_PITCH | FF_SWISS),
202 * Position and draw the text.
209 oldfont = SelectObject(fe->hdc_bm, fe->fonts[i].font);
210 if (GetTextMetrics(fe->hdc_bm, &tm)) {
211 if (align & ALIGN_VCENTRE)
212 y -= (tm.tmAscent+tm.tmDescent)/2;
216 if (GetTextExtentPoint32(fe->hdc_bm, text, strlen(text), &size)) {
217 if (align & ALIGN_HCENTRE)
219 else if (align & ALIGN_HRIGHT)
222 SetBkMode(fe->hdc_bm, TRANSPARENT);
223 SetTextColor(fe->hdc_bm, fe->colours[colour]);
224 TextOut(fe->hdc_bm, x, y, text, strlen(text));
225 SelectObject(fe->hdc_bm, oldfont);
229 void draw_rect(frontend *fe, int x, int y, int w, int h, int colour)
231 if (w == 1 && h == 1) {
233 * Rectangle() appears to get uppity if asked to draw a 1x1
234 * rectangle, presumably on the grounds that that's beneath
235 * its dignity and you ought to be using SetPixel instead.
238 SetPixel(fe->hdc_bm, x, y, fe->colours[colour]);
240 HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[colour]);
241 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
242 Rectangle(fe->hdc_bm, x, y, x+w, y+h);
243 SelectObject(fe->hdc_bm, oldbrush);
244 SelectObject(fe->hdc_bm, oldpen);
248 void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour)
250 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
251 MoveToEx(fe->hdc_bm, x1, y1, NULL);
252 LineTo(fe->hdc_bm, x2, y2);
253 SetPixel(fe->hdc_bm, x2, y2, fe->colours[colour]);
254 SelectObject(fe->hdc_bm, oldpen);
257 void draw_polygon(frontend *fe, int *coords, int npoints,
258 int fill, int colour)
260 POINT *pts = snewn(npoints+1, POINT);
263 for (i = 0; i <= npoints; i++) {
264 int j = (i < npoints ? i : 0);
265 pts[i].x = coords[j*2];
266 pts[i].y = coords[j*2+1];
270 HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[colour]);
271 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
272 Polygon(fe->hdc_bm, pts, npoints);
273 SelectObject(fe->hdc_bm, oldbrush);
274 SelectObject(fe->hdc_bm, oldpen);
276 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
277 Polyline(fe->hdc_bm, pts, npoints+1);
278 SelectObject(fe->hdc_bm, oldpen);
284 void start_draw(frontend *fe)
287 hdc_win = GetDC(fe->hwnd);
288 fe->hdc_bm = CreateCompatibleDC(hdc_win);
289 fe->prevbm = SelectObject(fe->hdc_bm, fe->bitmap);
290 ReleaseDC(fe->hwnd, hdc_win);
292 SetMapMode(fe->hdc_bm, MM_TEXT);
295 void draw_update(frontend *fe, int x, int y, int w, int h)
304 InvalidateRect(fe->hwnd, &r, FALSE);
307 void end_draw(frontend *fe)
309 SelectObject(fe->hdc_bm, fe->prevbm);
310 DeleteDC(fe->hdc_bm);
312 DeleteObject(fe->clip);
317 void deactivate_timer(frontend *fe)
319 KillTimer(fe->hwnd, fe->timer);
323 void activate_timer(frontend *fe)
326 fe->timer = SetTimer(fe->hwnd, fe->timer, 20, NULL);
327 fe->timer_last_tickcount = GetTickCount();
331 void write_clip(HWND hwnd, char *data)
339 * Windows expects CRLF in the clipboard, so we must convert
340 * any \n that has come out of the puzzle backend.
343 for (i = 0; data[i]; i++) {
348 data2 = snewn(len+1, char);
350 for (i = 0; data[i]; i++) {
353 data2[j++] = data[i];
358 clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
361 lock = GlobalLock(clipdata);
364 memcpy(lock, data2, len);
365 ((unsigned char *) lock)[len] = 0;
366 GlobalUnlock(clipdata);
368 if (OpenClipboard(hwnd)) {
370 SetClipboardData(CF_TEXT, clipdata);
373 GlobalFree(clipdata);
379 * See if we can find a help file.
381 static void find_help_file(frontend *fe)
383 char b[2048], *p, *q, *r;
385 if (!fe->help_path) {
386 GetModuleFileName(NULL, b, sizeof(b) - 1);
388 p = strrchr(b, '\\');
389 if (p && p >= r) r = p+1;
391 if (q && q >= r) r = q+1;
392 strcpy(r, HELP_FILE_NAME);
393 if ( (fp = fopen(b, "r")) != NULL) {
394 fe->help_path = dupstr(b);
397 fe->help_path = NULL;
398 strcpy(r, HELP_CNT_NAME);
399 if ( (fp = fopen(b, "r")) != NULL) {
400 fe->help_has_contents = TRUE;
403 fe->help_has_contents = FALSE;
407 static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
416 fe->me = midend_new(fe, &thegame);
419 *error = midend_game_id(fe->me, game_id, FALSE);
427 fe->help_path = NULL;
431 midend_new_game(fe->me);
432 midend_size(fe->me, &x, &y);
437 fe->nfonts = fe->fontsize = 0;
443 colours = midend_colours(fe->me, &ncolours);
445 fe->colours = snewn(ncolours, COLORREF);
446 fe->brushes = snewn(ncolours, HBRUSH);
447 fe->pens = snewn(ncolours, HPEN);
449 for (i = 0; i < ncolours; i++) {
450 fe->colours[i] = RGB(255 * colours[i*3+0],
451 255 * colours[i*3+1],
452 255 * colours[i*3+2]);
453 fe->brushes[i] = CreateSolidBrush(fe->colours[i]);
454 fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]);
461 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
462 (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED),
465 fe->hwnd = CreateWindowEx(0, thegame.name, thegame.name,
466 WS_OVERLAPPEDWINDOW &~
467 (WS_THICKFRAME | WS_MAXIMIZEBOX),
468 CW_USEDEFAULT, CW_USEDEFAULT,
469 r.right - r.left, r.bottom - r.top,
470 NULL, NULL, inst, NULL);
473 HMENU bar = CreateMenu();
474 HMENU menu = CreateMenu();
476 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Game");
477 AppendMenu(menu, MF_ENABLED, IDM_NEW, "New");
478 AppendMenu(menu, MF_ENABLED, IDM_RESTART, "Restart");
479 AppendMenu(menu, MF_ENABLED, IDM_SEED, "Specific...");
481 if ((fe->npresets = midend_num_presets(fe->me)) > 0 ||
482 thegame.can_configure) {
483 HMENU sub = CreateMenu();
486 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "Type");
488 fe->presets = snewn(fe->npresets, game_params *);
490 for (i = 0; i < fe->npresets; i++) {
493 midend_fetch_preset(fe->me, i, &name, &fe->presets[i]);
496 * FIXME: we ought to go through and do something
497 * with ampersands here.
500 AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name);
503 if (thegame.can_configure) {
504 AppendMenu(sub, MF_ENABLED, IDM_CONFIG, "Custom...");
508 AppendMenu(menu, MF_SEPARATOR, 0, 0);
509 AppendMenu(menu, MF_ENABLED, IDM_UNDO, "Undo");
510 AppendMenu(menu, MF_ENABLED, IDM_REDO, "Redo");
511 if (thegame.can_format_as_text) {
512 AppendMenu(menu, MF_SEPARATOR, 0, 0);
513 AppendMenu(menu, MF_ENABLED, IDM_COPY, "Copy");
515 if (thegame.can_solve) {
516 AppendMenu(menu, MF_SEPARATOR, 0, 0);
517 AppendMenu(menu, MF_ENABLED, IDM_SOLVE, "Solve");
519 AppendMenu(menu, MF_SEPARATOR, 0, 0);
520 AppendMenu(menu, MF_ENABLED, IDM_QUIT, "Exit");
522 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Help");
523 AppendMenu(menu, MF_ENABLED, IDM_ABOUT, "About");
525 AppendMenu(menu, MF_SEPARATOR, 0, 0);
526 AppendMenu(menu, MF_ENABLED, IDM_HELPC, "Contents");
527 if (thegame.winhelp_topic) {
529 assert(thegame.name);
530 item = snewn(9+strlen(thegame.name), char); /*ick*/
531 sprintf(item, "Help on %s", thegame.name);
532 AppendMenu(menu, MF_ENABLED, IDM_GAMEHELP, item);
536 SetMenu(fe->hwnd, bar);
539 if (midend_wants_statusbar(fe->me)) {
540 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh",
541 WS_CHILD | WS_VISIBLE,
542 0, 0, 0, 0, /* status bar does these */
543 fe->hwnd, NULL, inst, NULL);
544 GetWindowRect(fe->statusbar, &sr);
545 SetWindowPos(fe->hwnd, NULL, 0, 0,
546 r.right - r.left, r.bottom - r.top + sr.bottom - sr.top,
547 SWP_NOMOVE | SWP_NOZORDER);
548 SetWindowPos(fe->statusbar, NULL, 0, y, x, sr.bottom - sr.top,
551 fe->statusbar = NULL;
554 hdc = GetDC(fe->hwnd);
555 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
556 ReleaseDC(fe->hwnd, hdc);
558 SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe);
560 ShowWindow(fe->hwnd, SW_NORMAL);
561 SetForegroundWindow(fe->hwnd);
563 midend_redraw(fe->me);
568 static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg,
569 WPARAM wParam, LPARAM lParam)
571 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
578 if ((HIWORD(wParam) == BN_CLICKED ||
579 HIWORD(wParam) == BN_DOUBLECLICKED) &&
580 LOWORD(wParam) == IDOK)
592 static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg,
593 WPARAM wParam, LPARAM lParam)
595 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
605 * OK and Cancel are special cases.
607 if ((HIWORD(wParam) == BN_CLICKED ||
608 HIWORD(wParam) == BN_DOUBLECLICKED) &&
609 (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) {
610 if (LOWORD(wParam) == IDOK) {
611 char *err = midend_set_config(fe->me, fe->cfg_which, fe->cfg);
614 MessageBox(hwnd, err, "Validation error",
615 MB_ICONERROR | MB_OK);
626 * First find the control whose id this is.
628 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
629 if (j->ctlid == LOWORD(wParam))
632 if (i->type == C_END)
633 return 0; /* not our problem */
635 if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) {
637 GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer));
638 buffer[lenof(buffer)-1] = '\0';
640 i->sval = dupstr(buffer);
641 } else if (i->type == C_BOOLEAN &&
642 (HIWORD(wParam) == BN_CLICKED ||
643 HIWORD(wParam) == BN_DOUBLECLICKED)) {
644 i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid);
645 } else if (i->type == C_CHOICES &&
646 HIWORD(wParam) == CBN_SELCHANGE) {
647 i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid,
661 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
662 char *wclass, int wstyle,
663 int exstyle, const char *wtext, int wid)
666 ret = CreateWindowEx(exstyle, wclass, wtext,
667 wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1,
668 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
669 SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0));
673 static void about(frontend *fe)
683 int winwidth, winheight, y;
684 int height, width, maxwid;
685 const char *strings[16];
690 sprintf(titlebuf, "About %.250s", thegame.name);
692 strings[nstrings++] = thegame.name;
693 strings[nstrings++] = "from Simon Tatham's Portable Puzzle Collection";
694 strings[nstrings++] = ver;
696 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
697 wc.lpfnWndProc = DefDlgProc;
699 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
700 wc.hInstance = fe->inst;
702 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
703 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
704 wc.lpszMenuName = NULL;
705 wc.lpszClassName = "GameAboutBox";
708 hdc = GetDC(fe->hwnd);
709 SetMapMode(hdc, MM_TEXT);
711 fe->dlg_done = FALSE;
713 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
715 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
716 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
721 oldfont = SelectObject(hdc, fe->cfgfont);
722 if (GetTextMetrics(hdc, &tm)) {
723 height = tm.tmAscent + tm.tmDescent;
724 width = tm.tmAveCharWidth;
730 * Figure out the layout of the About box by measuring the
731 * length of each piece of text.
734 winheight = height/2;
736 for (i = 0; i < nstrings; i++) {
737 if (GetTextExtentPoint32(hdc, strings[i], strlen(strings[i]), &size))
738 lengths[i] = size.cx;
740 lengths[i] = 0; /* *shrug* */
741 if (maxwid < lengths[i])
743 winheight += height * 3 / 2 + (height / 2);
746 winheight += height + height * 7 / 4; /* OK button */
747 winwidth = maxwid + 4*width;
749 SelectObject(hdc, oldfont);
750 ReleaseDC(fe->hwnd, hdc);
753 * Create the dialog, now that we know its size.
760 r.bottom = winheight;
762 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
763 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
764 WS_CAPTION | WS_SYSMENU*/) &~
765 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
769 * Centre the dialog on its parent window.
773 GetWindowRect(fe->hwnd, &r2);
774 r.left = (r2.left + r2.right - r.right) / 2;
775 r.top = (r2.top + r2.bottom - r.bottom) / 2;
779 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, titlebuf,
780 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
781 WS_CAPTION | WS_SYSMENU,
783 r.right-r.left, r.bottom-r.top,
784 fe->hwnd, NULL, fe->inst, NULL);
787 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
789 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
790 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)AboutDlgProc);
794 for (i = 0; i < nstrings; i++) {
795 int border = width*2 + (maxwid - lengths[i]) / 2;
796 mkctrl(fe, border, border+lengths[i], y+height*1/8, y+height*9/8,
797 "Static", 0, 0, strings[i], id++);
800 assert(y < winheight);
804 y += height/2; /* extra space before OK */
805 mkctrl(fe, width*2, maxwid+width*2, y, y+height*7/4, "BUTTON",
806 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
809 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
811 EnableWindow(fe->hwnd, FALSE);
812 ShowWindow(fe->cfgbox, SW_NORMAL);
813 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
814 if (!IsDialogMessage(fe->cfgbox, &msg))
815 DispatchMessage(&msg);
819 EnableWindow(fe->hwnd, TRUE);
820 SetForegroundWindow(fe->hwnd);
821 DestroyWindow(fe->cfgbox);
822 DeleteObject(fe->cfgfont);
825 static int get_config(frontend *fe, int which)
838 int winwidth, winheight, col1l, col1r, col2l, col2r, y;
839 int height, width, maxlabel, maxcheckbox;
841 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
842 wc.lpfnWndProc = DefDlgProc;
844 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
845 wc.hInstance = fe->inst;
847 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
848 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
849 wc.lpszMenuName = NULL;
850 wc.lpszClassName = "GameConfigBox";
853 hdc = GetDC(fe->hwnd);
854 SetMapMode(hdc, MM_TEXT);
856 fe->dlg_done = FALSE;
858 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
860 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
861 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
866 oldfont = SelectObject(hdc, fe->cfgfont);
867 if (GetTextMetrics(hdc, &tm)) {
868 height = tm.tmAscent + tm.tmDescent;
869 width = tm.tmAveCharWidth;
874 fe->cfg = midend_get_config(fe->me, which, &title);
875 fe->cfg_which = which;
878 * Figure out the layout of the config box by measuring the
879 * length of each piece of text.
881 maxlabel = maxcheckbox = 0;
882 winheight = height/2;
884 for (i = fe->cfg; i->type != C_END; i++) {
889 * Both these control types have a label filling only
890 * the left-hand column of the box.
892 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
895 winheight += height * 3 / 2 + (height / 2);
900 * Checkboxes take up the whole of the box width.
902 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
903 maxcheckbox < size.cx)
904 maxcheckbox = size.cx;
905 winheight += height + (height / 2);
910 winheight += height + height * 7 / 4; /* OK / Cancel buttons */
913 col1r = col1l + maxlabel;
914 col2l = col1r + 2*width;
915 col2r = col2l + 30*width;
916 if (col2r < col1l+2*height+maxcheckbox)
917 col2r = col1l+2*height+maxcheckbox;
918 winwidth = col2r + 2*width;
920 SelectObject(hdc, oldfont);
921 ReleaseDC(fe->hwnd, hdc);
924 * Create the dialog, now that we know its size.
931 r.bottom = winheight;
933 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
934 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
935 WS_CAPTION | WS_SYSMENU*/) &~
936 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
940 * Centre the dialog on its parent window.
944 GetWindowRect(fe->hwnd, &r2);
945 r.left = (r2.left + r2.right - r.right) / 2;
946 r.top = (r2.top + r2.bottom - r.bottom) / 2;
950 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title,
951 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
952 WS_CAPTION | WS_SYSMENU,
954 r.right-r.left, r.bottom-r.top,
955 fe->hwnd, NULL, fe->inst, NULL);
959 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
961 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
962 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc);
965 * Count the controls so we can allocate cfgaux.
967 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
969 fe->cfgaux = snewn(nctrls, struct cfg_aux);
973 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
977 * Edit box with a label beside it.
979 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
980 "Static", 0, 0, i->name, id++);
981 ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2,
982 "EDIT", WS_TABSTOP | ES_AUTOHSCROLL,
983 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
984 SetWindowText(ctl, i->sval);
992 mkctrl(fe, col1l, col2r, y, y+height, "BUTTON",
993 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
994 0, i->name, (j->ctlid = id++));
995 CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0));
1001 * Drop-down list with a label beside it.
1003 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
1004 "STATIC", 0, 0, i->name, id++);
1005 ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2,
1006 "COMBOBOX", WS_TABSTOP |
1007 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
1008 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
1010 char c, *p, *q, *str;
1012 SendMessage(ctl, CB_RESETCONTENT, 0, 0);
1017 while (*q && *q != c) q++;
1018 str = snewn(q-p+1, char);
1019 strncpy(str, p, q-p);
1021 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str);
1028 SendMessage(ctl, CB_SETCURSEL, i->ival, 0);
1034 assert(y < winheight);
1038 y += height/2; /* extra space before OK and Cancel */
1039 mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON",
1040 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
1042 mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON",
1043 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL);
1045 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
1047 EnableWindow(fe->hwnd, FALSE);
1048 ShowWindow(fe->cfgbox, SW_NORMAL);
1049 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
1050 if (!IsDialogMessage(fe->cfgbox, &msg))
1051 DispatchMessage(&msg);
1055 EnableWindow(fe->hwnd, TRUE);
1056 SetForegroundWindow(fe->hwnd);
1057 DestroyWindow(fe->cfgbox);
1058 DeleteObject(fe->cfgfont);
1063 return (fe->dlg_done == 2);
1066 static void new_game_type(frontend *fe)
1072 midend_new_game(fe->me);
1073 midend_size(fe->me, &x, &y);
1078 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
1079 (WS_THICKFRAME | WS_MAXIMIZEBOX |
1083 if (fe->statusbar != NULL) {
1084 GetWindowRect(fe->statusbar, &sr);
1086 sr.left = sr.right = sr.top = sr.bottom = 0;
1088 SetWindowPos(fe->hwnd, NULL, 0, 0,
1090 r.bottom - r.top + sr.bottom - sr.top,
1091 SWP_NOMOVE | SWP_NOZORDER);
1092 if (fe->statusbar != NULL)
1093 SetWindowPos(fe->statusbar, NULL, 0, y, x,
1094 sr.bottom - sr.top, SWP_NOZORDER);
1096 DeleteObject(fe->bitmap);
1098 hdc = GetDC(fe->hwnd);
1099 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
1100 ReleaseDC(fe->hwnd, hdc);
1102 midend_redraw(fe->me);
1105 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
1106 WPARAM wParam, LPARAM lParam)
1108 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1112 DestroyWindow(hwnd);
1115 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
1117 if (!midend_process_key(fe->me, 0, 0, 'n'))
1121 if (!midend_process_key(fe->me, 0, 0, 'r'))
1125 if (!midend_process_key(fe->me, 0, 0, 'u'))
1129 if (!midend_process_key(fe->me, 0, 0, '\x12'))
1134 char *text = midend_text_format(fe->me);
1136 write_clip(hwnd, text);
1138 MessageBeep(MB_ICONWARNING);
1144 char *msg = midend_solve(fe->me);
1146 MessageBox(hwnd, msg, "Unable to solve",
1147 MB_ICONERROR | MB_OK);
1151 if (!midend_process_key(fe->me, 0, 0, 'q'))
1155 if (get_config(fe, CFG_SETTINGS))
1159 if (get_config(fe, CFG_SEED))
1166 assert(fe->help_path);
1167 WinHelp(hwnd, fe->help_path,
1168 fe->help_has_contents ? HELP_FINDER : HELP_CONTENTS, 0);
1171 assert(fe->help_path);
1172 assert(thegame.winhelp_topic);
1174 char *cmd = snewn(10+strlen(thegame.winhelp_topic), char);
1175 sprintf(cmd, "JI(`',`%s')", thegame.winhelp_topic);
1176 WinHelp(hwnd, fe->help_path, HELP_COMMAND, (DWORD)cmd);
1182 int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10;
1184 if (p >= 0 && p < fe->npresets) {
1185 midend_set_params(fe->me, fe->presets[p]);
1201 hdc = BeginPaint(hwnd, &p);
1202 hdc2 = CreateCompatibleDC(hdc);
1203 prevbm = SelectObject(hdc2, fe->bitmap);
1205 p.rcPaint.left, p.rcPaint.top,
1206 p.rcPaint.right - p.rcPaint.left,
1207 p.rcPaint.bottom - p.rcPaint.top,
1209 p.rcPaint.left, p.rcPaint.top,
1211 SelectObject(hdc2, prevbm);
1222 if (!(lParam & 0x01000000))
1223 key = MOD_NUM_KEYPAD | '4';
1228 if (!(lParam & 0x01000000))
1229 key = MOD_NUM_KEYPAD | '6';
1234 if (!(lParam & 0x01000000))
1235 key = MOD_NUM_KEYPAD | '8';
1240 if (!(lParam & 0x01000000))
1241 key = MOD_NUM_KEYPAD | '2';
1246 * Diagonal keys on the numeric keypad.
1249 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '9';
1252 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '3';
1255 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '7';
1258 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '1';
1261 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '0';
1264 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '5';
1267 * Numeric keypad keys with Num Lock on.
1269 case VK_NUMPAD4: key = MOD_NUM_KEYPAD | '4'; break;
1270 case VK_NUMPAD6: key = MOD_NUM_KEYPAD | '6'; break;
1271 case VK_NUMPAD8: key = MOD_NUM_KEYPAD | '8'; break;
1272 case VK_NUMPAD2: key = MOD_NUM_KEYPAD | '2'; break;
1273 case VK_NUMPAD5: key = MOD_NUM_KEYPAD | '5'; break;
1274 case VK_NUMPAD9: key = MOD_NUM_KEYPAD | '9'; break;
1275 case VK_NUMPAD3: key = MOD_NUM_KEYPAD | '3'; break;
1276 case VK_NUMPAD7: key = MOD_NUM_KEYPAD | '7'; break;
1277 case VK_NUMPAD1: key = MOD_NUM_KEYPAD | '1'; break;
1278 case VK_NUMPAD0: key = MOD_NUM_KEYPAD | '0'; break;
1282 if (!midend_process_key(fe->me, 0, 0, key))
1287 m.message = WM_KEYDOWN;
1289 m.lParam = lParam & 0xdfff;
1290 TranslateMessage(&m);
1294 case WM_LBUTTONDOWN:
1295 case WM_RBUTTONDOWN:
1296 case WM_MBUTTONDOWN:
1301 * Shift-clicks count as middle-clicks, since otherwise
1302 * two-button Windows users won't have any kind of
1303 * middle click to use.
1305 if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT))
1306 button = MIDDLE_BUTTON;
1307 else if (message == WM_LBUTTONDOWN)
1308 button = LEFT_BUTTON;
1310 button = RIGHT_BUTTON;
1312 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1313 (signed short)HIWORD(lParam), button))
1326 * Shift-clicks count as middle-clicks, since otherwise
1327 * two-button Windows users won't have any kind of
1328 * middle click to use.
1330 if (message == WM_MBUTTONUP || (wParam & MK_SHIFT))
1331 button = MIDDLE_RELEASE;
1332 else if (message == WM_LBUTTONUP)
1333 button = LEFT_RELEASE;
1335 button = RIGHT_RELEASE;
1337 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1338 (signed short)HIWORD(lParam), button))
1348 if (wParam & (MK_MBUTTON | MK_SHIFT))
1349 button = MIDDLE_DRAG;
1350 else if (wParam & MK_LBUTTON)
1353 button = RIGHT_DRAG;
1355 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1356 (signed short)HIWORD(lParam), button))
1361 if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam))
1366 DWORD now = GetTickCount();
1367 float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F;
1368 midend_timer(fe->me, elapsed);
1369 fe->timer_last_tickcount = now;
1374 return DefWindowProc(hwnd, message, wParam, lParam);
1377 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
1382 InitCommonControls();
1388 wndclass.lpfnWndProc = WndProc;
1389 wndclass.cbClsExtra = 0;
1390 wndclass.cbWndExtra = 0;
1391 wndclass.hInstance = inst;
1392 wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION);
1393 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1394 wndclass.hbrBackground = NULL;
1395 wndclass.lpszMenuName = NULL;
1396 wndclass.lpszClassName = thegame.name;
1398 RegisterClass(&wndclass);
1401 while (*cmdline && isspace(*cmdline))
1404 if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) {
1406 sprintf(buf, "%.100s Error", thegame.name);
1407 MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
1411 while (GetMessage(&msg, NULL, 0, 0)) {
1412 DispatchMessage(&msg);