2 * windows.c: Windows front end for my puzzle collection.
18 #define IDM_NEW 0x0010
19 #define IDM_RESTART 0x0020
20 #define IDM_UNDO 0x0030
21 #define IDM_REDO 0x0040
22 #define IDM_COPY 0x0050
23 #define IDM_SOLVE 0x0060
24 #define IDM_QUIT 0x0070
25 #define IDM_CONFIG 0x0080
26 #define IDM_DESC 0x0090
27 #define IDM_SEED 0x00A0
28 #define IDM_HELPC 0x00B0
29 #define IDM_GAMEHELP 0x00C0
30 #define IDM_ABOUT 0x00D0
31 #define IDM_PRESETS 0x0100
33 #define HELP_FILE_NAME "puzzles.hlp"
34 #define HELP_CNT_NAME "puzzles.cnt"
37 static FILE *debug_fp = NULL;
38 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
39 static int debug_got_console = 0;
45 if (!debug_got_console) {
47 debug_got_console = 1;
48 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
52 debug_fp = fopen("debug.log", "w");
55 if (debug_hdl != INVALID_HANDLE_VALUE) {
56 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
62 void debug_printf(char *fmt, ...)
68 vsprintf(buf, fmt, ap);
92 HWND hwnd, statusbar, cfgbox;
94 HBITMAP bitmap, prevbm;
101 DWORD timer_last_tickcount;
103 game_params **presets;
105 int nfonts, fontsize;
107 struct cfg_aux *cfgaux;
108 int cfg_which, dlg_done;
111 int help_has_contents;
115 void fatal(char *fmt, ...)
121 vsprintf(buf, fmt, ap);
124 MessageBox(NULL, buf, "Fatal error", MB_ICONEXCLAMATION | MB_OK);
129 void get_random_seed(void **randseed, int *randseedsize)
131 time_t *tp = snew(time_t);
133 *randseed = (void *)tp;
134 *randseedsize = sizeof(time_t);
137 void status_bar(frontend *fe, char *text)
139 char *rewritten = midend_rewrite_statusbar(fe->me, text);
140 if (!fe->laststatus || strcmp(rewritten, fe->laststatus)) {
141 SetWindowText(fe->statusbar, rewritten);
142 sfree(fe->laststatus);
143 fe->laststatus = rewritten;
149 blitter *blitter_new(int w, int h)
151 blitter *bl = snew(blitter);
153 memset(bl, 0, sizeof(blitter));
161 void blitter_free(blitter *bl)
163 if (bl->bitmap) DeleteObject(bl->bitmap);
167 static void blitter_mkbitmap(frontend *fe, blitter *bl)
169 HDC hdc = GetDC(fe->hwnd);
170 bl->bitmap = CreateCompatibleBitmap(hdc, bl->w, bl->h);
171 ReleaseDC(fe->hwnd, hdc);
174 /* BitBlt(dstDC, dstX, dstY, dstW, dstH, srcDC, srcX, srcY, dType) */
176 void blitter_save(frontend *fe, blitter *bl, int x, int y)
178 HDC hdc_win, hdc_blit;
181 if (!bl->bitmap) blitter_mkbitmap(fe, bl);
183 bl->x = x; bl->y = y;
185 hdc_win = GetDC(fe->hwnd);
186 hdc_blit = CreateCompatibleDC(hdc_win);
187 if (!hdc_blit) fatal("hdc_blit failed: 0x%x", GetLastError());
189 prev_blit = SelectObject(hdc_blit, bl->bitmap);
190 if (prev_blit == NULL || prev_blit == HGDI_ERROR)
191 fatal("SelectObject for hdc_main failed: 0x%x", GetLastError());
193 if (!BitBlt(hdc_blit, 0, 0, bl->w, bl->h,
194 fe->hdc_bm, x, y, SRCCOPY))
195 fatal("BitBlt failed: 0x%x", GetLastError());
197 SelectObject(hdc_blit, prev_blit);
199 ReleaseDC(fe->hwnd, hdc_win);
202 void blitter_load(frontend *fe, blitter *bl, int x, int y)
204 HDC hdc_win, hdc_blit;
207 assert(bl->bitmap); /* we should always have saved before loading */
209 if (x == BLITTER_FROMSAVED) x = bl->x;
210 if (y == BLITTER_FROMSAVED) y = bl->y;
212 hdc_win = GetDC(fe->hwnd);
213 hdc_blit = CreateCompatibleDC(hdc_win);
215 prev_blit = SelectObject(hdc_blit, bl->bitmap);
217 BitBlt(fe->hdc_bm, x, y, bl->w, bl->h,
218 hdc_blit, 0, 0, SRCCOPY);
220 SelectObject(hdc_blit, prev_blit);
222 ReleaseDC(fe->hwnd, hdc_win);
225 void frontend_default_colour(frontend *fe, float *output)
227 DWORD c = GetSysColor(COLOR_MENU); /* ick */
229 output[0] = (float)(GetRValue(c) / 255.0);
230 output[1] = (float)(GetGValue(c) / 255.0);
231 output[2] = (float)(GetBValue(c) / 255.0);
234 void clip(frontend *fe, int x, int y, int w, int h)
236 IntersectClipRect(fe->hdc_bm, x, y, x+w, y+h);
239 void unclip(frontend *fe)
241 SelectClipRgn(fe->hdc_bm, NULL);
244 void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize,
245 int align, int colour, char *text)
250 * Find or create the font.
252 for (i = 0; i < fe->nfonts; i++)
253 if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
256 if (i == fe->nfonts) {
257 if (fe->fontsize <= fe->nfonts) {
258 fe->fontsize = fe->nfonts + 10;
259 fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
264 fe->fonts[i].type = fonttype;
265 fe->fonts[i].size = fontsize;
267 fe->fonts[i].font = CreateFont(-fontsize, 0, 0, 0, FW_BOLD,
268 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
269 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
271 (fonttype == FONT_FIXED ?
272 FIXED_PITCH | FF_DONTCARE :
273 VARIABLE_PITCH | FF_SWISS),
278 * Position and draw the text.
285 oldfont = SelectObject(fe->hdc_bm, fe->fonts[i].font);
286 if (GetTextMetrics(fe->hdc_bm, &tm)) {
287 if (align & ALIGN_VCENTRE)
288 y -= (tm.tmAscent+tm.tmDescent)/2;
292 if (GetTextExtentPoint32(fe->hdc_bm, text, strlen(text), &size)) {
293 if (align & ALIGN_HCENTRE)
295 else if (align & ALIGN_HRIGHT)
298 SetBkMode(fe->hdc_bm, TRANSPARENT);
299 SetTextColor(fe->hdc_bm, fe->colours[colour]);
300 TextOut(fe->hdc_bm, x, y, text, strlen(text));
301 SelectObject(fe->hdc_bm, oldfont);
305 void draw_rect(frontend *fe, int x, int y, int w, int h, int colour)
307 if (w == 1 && h == 1) {
309 * Rectangle() appears to get uppity if asked to draw a 1x1
310 * rectangle, presumably on the grounds that that's beneath
311 * its dignity and you ought to be using SetPixel instead.
314 SetPixel(fe->hdc_bm, x, y, fe->colours[colour]);
316 HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[colour]);
317 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
318 Rectangle(fe->hdc_bm, x, y, x+w, y+h);
319 SelectObject(fe->hdc_bm, oldbrush);
320 SelectObject(fe->hdc_bm, oldpen);
324 void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour)
326 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
327 MoveToEx(fe->hdc_bm, x1, y1, NULL);
328 LineTo(fe->hdc_bm, x2, y2);
329 SetPixel(fe->hdc_bm, x2, y2, fe->colours[colour]);
330 SelectObject(fe->hdc_bm, oldpen);
333 void draw_polygon(frontend *fe, int *coords, int npoints,
334 int fill, int colour)
336 POINT *pts = snewn(npoints+1, POINT);
339 for (i = 0; i <= npoints; i++) {
340 int j = (i < npoints ? i : 0);
341 pts[i].x = coords[j*2];
342 pts[i].y = coords[j*2+1];
346 HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[colour]);
347 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
348 Polygon(fe->hdc_bm, pts, npoints);
349 SelectObject(fe->hdc_bm, oldbrush);
350 SelectObject(fe->hdc_bm, oldpen);
352 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
353 Polyline(fe->hdc_bm, pts, npoints+1);
354 SelectObject(fe->hdc_bm, oldpen);
360 void start_draw(frontend *fe)
363 hdc_win = GetDC(fe->hwnd);
364 fe->hdc_bm = CreateCompatibleDC(hdc_win);
365 fe->prevbm = SelectObject(fe->hdc_bm, fe->bitmap);
366 ReleaseDC(fe->hwnd, hdc_win);
368 SetMapMode(fe->hdc_bm, MM_TEXT);
371 void draw_update(frontend *fe, int x, int y, int w, int h)
380 InvalidateRect(fe->hwnd, &r, FALSE);
383 void end_draw(frontend *fe)
385 SelectObject(fe->hdc_bm, fe->prevbm);
386 DeleteDC(fe->hdc_bm);
388 DeleteObject(fe->clip);
393 void deactivate_timer(frontend *fe)
395 KillTimer(fe->hwnd, fe->timer);
399 void activate_timer(frontend *fe)
402 fe->timer = SetTimer(fe->hwnd, fe->timer, 20, NULL);
403 fe->timer_last_tickcount = GetTickCount();
407 void write_clip(HWND hwnd, char *data)
415 * Windows expects CRLF in the clipboard, so we must convert
416 * any \n that has come out of the puzzle backend.
419 for (i = 0; data[i]; i++) {
424 data2 = snewn(len+1, char);
426 for (i = 0; data[i]; i++) {
429 data2[j++] = data[i];
434 clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
437 lock = GlobalLock(clipdata);
440 memcpy(lock, data2, len);
441 ((unsigned char *) lock)[len] = 0;
442 GlobalUnlock(clipdata);
444 if (OpenClipboard(hwnd)) {
446 SetClipboardData(CF_TEXT, clipdata);
449 GlobalFree(clipdata);
455 * See if we can find a help file.
457 static void find_help_file(frontend *fe)
459 char b[2048], *p, *q, *r;
461 if (!fe->help_path) {
462 GetModuleFileName(NULL, b, sizeof(b) - 1);
464 p = strrchr(b, '\\');
465 if (p && p >= r) r = p+1;
467 if (q && q >= r) r = q+1;
468 strcpy(r, HELP_FILE_NAME);
469 if ( (fp = fopen(b, "r")) != NULL) {
470 fe->help_path = dupstr(b);
473 fe->help_path = NULL;
474 strcpy(r, HELP_CNT_NAME);
475 if ( (fp = fopen(b, "r")) != NULL) {
476 fe->help_has_contents = TRUE;
479 fe->help_has_contents = FALSE;
483 static void check_window_size(frontend *fe, int *px, int *py)
490 GetWindowRect(fe->statusbar, &sr);
491 sy = sr.bottom - sr.top;
497 * See if we actually got the window size we wanted, and adjust
498 * the puzzle size if not.
500 GetClientRect(fe->hwnd, &r);
501 x = r.right - r.left;
502 y = r.bottom - r.top - sy;
503 midend_size(fe->me, &x, &y, FALSE);
504 if (x != r.right - r.left || y != r.bottom - r.top) {
506 * Resize the window, now we know what size we _really_
512 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
513 (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED),
515 SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left, r.bottom - r.top,
516 SWP_NOMOVE | SWP_NOZORDER);
520 GetClientRect(fe->hwnd, &r);
521 SetWindowPos(fe->statusbar, NULL, 0, r.bottom-r.top-sy, r.right-r.left,
529 static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
538 fe->me = midend_new(fe, &thegame);
541 *error = midend_game_id(fe->me, game_id);
549 fe->help_path = NULL;
553 midend_new_game(fe->me);
558 fe->nfonts = fe->fontsize = 0;
560 fe->laststatus = NULL;
566 colours = midend_colours(fe->me, &ncolours);
568 fe->colours = snewn(ncolours, COLORREF);
569 fe->brushes = snewn(ncolours, HBRUSH);
570 fe->pens = snewn(ncolours, HPEN);
572 for (i = 0; i < ncolours; i++) {
573 fe->colours[i] = RGB(255 * colours[i*3+0],
574 255 * colours[i*3+1],
575 255 * colours[i*3+2]);
576 fe->brushes[i] = CreateSolidBrush(fe->colours[i]);
577 fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]);
581 x = y = INT_MAX; /* find puzzle's preferred size */
582 midend_size(fe->me, &x, &y, FALSE);
587 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
588 (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED),
591 fe->hwnd = CreateWindowEx(0, thegame.name, thegame.name,
592 WS_OVERLAPPEDWINDOW &~
593 (WS_THICKFRAME | WS_MAXIMIZEBOX),
594 CW_USEDEFAULT, CW_USEDEFAULT,
595 r.right - r.left, r.bottom - r.top,
596 NULL, NULL, inst, NULL);
598 if (midend_wants_statusbar(fe->me)) {
600 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh",
601 WS_CHILD | WS_VISIBLE,
602 0, 0, 0, 0, /* status bar does these */
603 fe->hwnd, NULL, inst, NULL);
605 * Now resize the window to take account of the status bar.
607 GetWindowRect(fe->statusbar, &sr);
608 GetWindowRect(fe->hwnd, &r);
609 SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left,
610 r.bottom - r.top + sr.bottom - sr.top,
611 SWP_NOMOVE | SWP_NOZORDER);
613 fe->statusbar = NULL;
617 HMENU bar = CreateMenu();
618 HMENU menu = CreateMenu();
620 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Game");
621 AppendMenu(menu, MF_ENABLED, IDM_NEW, "New");
622 AppendMenu(menu, MF_ENABLED, IDM_RESTART, "Restart");
623 AppendMenu(menu, MF_ENABLED, IDM_DESC, "Specific...");
624 AppendMenu(menu, MF_ENABLED, IDM_SEED, "Random Seed...");
626 if ((fe->npresets = midend_num_presets(fe->me)) > 0 ||
627 thegame.can_configure) {
628 HMENU sub = CreateMenu();
631 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "Type");
633 fe->presets = snewn(fe->npresets, game_params *);
635 for (i = 0; i < fe->npresets; i++) {
638 midend_fetch_preset(fe->me, i, &name, &fe->presets[i]);
641 * FIXME: we ought to go through and do something
642 * with ampersands here.
645 AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name);
648 if (thegame.can_configure) {
649 AppendMenu(sub, MF_ENABLED, IDM_CONFIG, "Custom...");
653 AppendMenu(menu, MF_SEPARATOR, 0, 0);
654 AppendMenu(menu, MF_ENABLED, IDM_UNDO, "Undo");
655 AppendMenu(menu, MF_ENABLED, IDM_REDO, "Redo");
656 if (thegame.can_format_as_text) {
657 AppendMenu(menu, MF_SEPARATOR, 0, 0);
658 AppendMenu(menu, MF_ENABLED, IDM_COPY, "Copy");
660 if (thegame.can_solve) {
661 AppendMenu(menu, MF_SEPARATOR, 0, 0);
662 AppendMenu(menu, MF_ENABLED, IDM_SOLVE, "Solve");
664 AppendMenu(menu, MF_SEPARATOR, 0, 0);
665 AppendMenu(menu, MF_ENABLED, IDM_QUIT, "Exit");
667 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Help");
668 AppendMenu(menu, MF_ENABLED, IDM_ABOUT, "About");
670 AppendMenu(menu, MF_SEPARATOR, 0, 0);
671 AppendMenu(menu, MF_ENABLED, IDM_HELPC, "Contents");
672 if (thegame.winhelp_topic) {
674 assert(thegame.name);
675 item = snewn(9+strlen(thegame.name), char); /*ick*/
676 sprintf(item, "Help on %s", thegame.name);
677 AppendMenu(menu, MF_ENABLED, IDM_GAMEHELP, item);
681 SetMenu(fe->hwnd, bar);
684 check_window_size(fe, &x, &y);
686 hdc = GetDC(fe->hwnd);
687 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
688 ReleaseDC(fe->hwnd, hdc);
690 SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe);
692 ShowWindow(fe->hwnd, SW_NORMAL);
693 SetForegroundWindow(fe->hwnd);
695 midend_redraw(fe->me);
700 static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg,
701 WPARAM wParam, LPARAM lParam)
703 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
710 if ((HIWORD(wParam) == BN_CLICKED ||
711 HIWORD(wParam) == BN_DOUBLECLICKED) &&
712 LOWORD(wParam) == IDOK)
724 static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg,
725 WPARAM wParam, LPARAM lParam)
727 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
737 * OK and Cancel are special cases.
739 if ((HIWORD(wParam) == BN_CLICKED ||
740 HIWORD(wParam) == BN_DOUBLECLICKED) &&
741 (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) {
742 if (LOWORD(wParam) == IDOK) {
743 char *err = midend_set_config(fe->me, fe->cfg_which, fe->cfg);
746 MessageBox(hwnd, err, "Validation error",
747 MB_ICONERROR | MB_OK);
758 * First find the control whose id this is.
760 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
761 if (j->ctlid == LOWORD(wParam))
764 if (i->type == C_END)
765 return 0; /* not our problem */
767 if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) {
769 GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer));
770 buffer[lenof(buffer)-1] = '\0';
772 i->sval = dupstr(buffer);
773 } else if (i->type == C_BOOLEAN &&
774 (HIWORD(wParam) == BN_CLICKED ||
775 HIWORD(wParam) == BN_DOUBLECLICKED)) {
776 i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid);
777 } else if (i->type == C_CHOICES &&
778 HIWORD(wParam) == CBN_SELCHANGE) {
779 i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid,
793 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
794 char *wclass, int wstyle,
795 int exstyle, const char *wtext, int wid)
798 ret = CreateWindowEx(exstyle, wclass, wtext,
799 wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1,
800 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
801 SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0));
805 static void about(frontend *fe)
815 int winwidth, winheight, y;
816 int height, width, maxwid;
817 const char *strings[16];
822 sprintf(titlebuf, "About %.250s", thegame.name);
824 strings[nstrings++] = thegame.name;
825 strings[nstrings++] = "from Simon Tatham's Portable Puzzle Collection";
826 strings[nstrings++] = ver;
828 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
829 wc.lpfnWndProc = DefDlgProc;
831 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
832 wc.hInstance = fe->inst;
834 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
835 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
836 wc.lpszMenuName = NULL;
837 wc.lpszClassName = "GameAboutBox";
840 hdc = GetDC(fe->hwnd);
841 SetMapMode(hdc, MM_TEXT);
843 fe->dlg_done = FALSE;
845 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
847 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
848 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
853 oldfont = SelectObject(hdc, fe->cfgfont);
854 if (GetTextMetrics(hdc, &tm)) {
855 height = tm.tmAscent + tm.tmDescent;
856 width = tm.tmAveCharWidth;
862 * Figure out the layout of the About box by measuring the
863 * length of each piece of text.
866 winheight = height/2;
868 for (i = 0; i < nstrings; i++) {
869 if (GetTextExtentPoint32(hdc, strings[i], strlen(strings[i]), &size))
870 lengths[i] = size.cx;
872 lengths[i] = 0; /* *shrug* */
873 if (maxwid < lengths[i])
875 winheight += height * 3 / 2 + (height / 2);
878 winheight += height + height * 7 / 4; /* OK button */
879 winwidth = maxwid + 4*width;
881 SelectObject(hdc, oldfont);
882 ReleaseDC(fe->hwnd, hdc);
885 * Create the dialog, now that we know its size.
892 r.bottom = winheight;
894 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
895 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
896 WS_CAPTION | WS_SYSMENU*/) &~
897 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
901 * Centre the dialog on its parent window.
905 GetWindowRect(fe->hwnd, &r2);
906 r.left = (r2.left + r2.right - r.right) / 2;
907 r.top = (r2.top + r2.bottom - r.bottom) / 2;
911 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, titlebuf,
912 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
913 WS_CAPTION | WS_SYSMENU,
915 r.right-r.left, r.bottom-r.top,
916 fe->hwnd, NULL, fe->inst, NULL);
919 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
921 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
922 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)AboutDlgProc);
926 for (i = 0; i < nstrings; i++) {
927 int border = width*2 + (maxwid - lengths[i]) / 2;
928 mkctrl(fe, border, border+lengths[i], y+height*1/8, y+height*9/8,
929 "Static", 0, 0, strings[i], id++);
932 assert(y < winheight);
936 y += height/2; /* extra space before OK */
937 mkctrl(fe, width*2, maxwid+width*2, y, y+height*7/4, "BUTTON",
938 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
941 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
943 EnableWindow(fe->hwnd, FALSE);
944 ShowWindow(fe->cfgbox, SW_NORMAL);
945 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
946 if (!IsDialogMessage(fe->cfgbox, &msg))
947 DispatchMessage(&msg);
951 EnableWindow(fe->hwnd, TRUE);
952 SetForegroundWindow(fe->hwnd);
953 DestroyWindow(fe->cfgbox);
954 DeleteObject(fe->cfgfont);
957 static int get_config(frontend *fe, int which)
970 int winwidth, winheight, col1l, col1r, col2l, col2r, y;
971 int height, width, maxlabel, maxcheckbox;
973 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
974 wc.lpfnWndProc = DefDlgProc;
976 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
977 wc.hInstance = fe->inst;
979 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
980 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
981 wc.lpszMenuName = NULL;
982 wc.lpszClassName = "GameConfigBox";
985 hdc = GetDC(fe->hwnd);
986 SetMapMode(hdc, MM_TEXT);
988 fe->dlg_done = FALSE;
990 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
992 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
993 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
998 oldfont = SelectObject(hdc, fe->cfgfont);
999 if (GetTextMetrics(hdc, &tm)) {
1000 height = tm.tmAscent + tm.tmDescent;
1001 width = tm.tmAveCharWidth;
1003 height = width = 30;
1006 fe->cfg = midend_get_config(fe->me, which, &title);
1007 fe->cfg_which = which;
1010 * Figure out the layout of the config box by measuring the
1011 * length of each piece of text.
1013 maxlabel = maxcheckbox = 0;
1014 winheight = height/2;
1016 for (i = fe->cfg; i->type != C_END; i++) {
1021 * Both these control types have a label filling only
1022 * the left-hand column of the box.
1024 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
1027 winheight += height * 3 / 2 + (height / 2);
1032 * Checkboxes take up the whole of the box width.
1034 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
1035 maxcheckbox < size.cx)
1036 maxcheckbox = size.cx;
1037 winheight += height + (height / 2);
1042 winheight += height + height * 7 / 4; /* OK / Cancel buttons */
1045 col1r = col1l + maxlabel;
1046 col2l = col1r + 2*width;
1047 col2r = col2l + 30*width;
1048 if (col2r < col1l+2*height+maxcheckbox)
1049 col2r = col1l+2*height+maxcheckbox;
1050 winwidth = col2r + 2*width;
1052 SelectObject(hdc, oldfont);
1053 ReleaseDC(fe->hwnd, hdc);
1056 * Create the dialog, now that we know its size.
1063 r.bottom = winheight;
1065 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
1066 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1067 WS_CAPTION | WS_SYSMENU*/) &~
1068 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
1072 * Centre the dialog on its parent window.
1076 GetWindowRect(fe->hwnd, &r2);
1077 r.left = (r2.left + r2.right - r.right) / 2;
1078 r.top = (r2.top + r2.bottom - r.bottom) / 2;
1082 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title,
1083 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1084 WS_CAPTION | WS_SYSMENU,
1086 r.right-r.left, r.bottom-r.top,
1087 fe->hwnd, NULL, fe->inst, NULL);
1091 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
1093 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
1094 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc);
1097 * Count the controls so we can allocate cfgaux.
1099 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
1101 fe->cfgaux = snewn(nctrls, struct cfg_aux);
1105 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
1109 * Edit box with a label beside it.
1111 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
1112 "Static", 0, 0, i->name, id++);
1113 ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2,
1114 "EDIT", WS_TABSTOP | ES_AUTOHSCROLL,
1115 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
1116 SetWindowText(ctl, i->sval);
1124 mkctrl(fe, col1l, col2r, y, y+height, "BUTTON",
1125 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
1126 0, i->name, (j->ctlid = id++));
1127 CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0));
1133 * Drop-down list with a label beside it.
1135 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
1136 "STATIC", 0, 0, i->name, id++);
1137 ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2,
1138 "COMBOBOX", WS_TABSTOP |
1139 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
1140 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
1142 char c, *p, *q, *str;
1144 SendMessage(ctl, CB_RESETCONTENT, 0, 0);
1149 while (*q && *q != c) q++;
1150 str = snewn(q-p+1, char);
1151 strncpy(str, p, q-p);
1153 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str);
1160 SendMessage(ctl, CB_SETCURSEL, i->ival, 0);
1166 assert(y < winheight);
1170 y += height/2; /* extra space before OK and Cancel */
1171 mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON",
1172 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
1174 mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON",
1175 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL);
1177 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
1179 EnableWindow(fe->hwnd, FALSE);
1180 ShowWindow(fe->cfgbox, SW_NORMAL);
1181 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
1182 if (!IsDialogMessage(fe->cfgbox, &msg))
1183 DispatchMessage(&msg);
1187 EnableWindow(fe->hwnd, TRUE);
1188 SetForegroundWindow(fe->hwnd);
1189 DestroyWindow(fe->cfgbox);
1190 DeleteObject(fe->cfgfont);
1195 return (fe->dlg_done == 2);
1198 static void new_game_type(frontend *fe)
1204 midend_new_game(fe->me);
1206 midend_size(fe->me, &x, &y, FALSE);
1211 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
1212 (WS_THICKFRAME | WS_MAXIMIZEBOX |
1216 if (fe->statusbar != NULL) {
1217 GetWindowRect(fe->statusbar, &sr);
1219 sr.left = sr.right = sr.top = sr.bottom = 0;
1221 SetWindowPos(fe->hwnd, NULL, 0, 0,
1223 r.bottom - r.top + sr.bottom - sr.top,
1224 SWP_NOMOVE | SWP_NOZORDER);
1226 check_window_size(fe, &x, &y);
1228 if (fe->statusbar != NULL)
1229 SetWindowPos(fe->statusbar, NULL, 0, y, x,
1230 sr.bottom - sr.top, SWP_NOZORDER);
1232 DeleteObject(fe->bitmap);
1234 hdc = GetDC(fe->hwnd);
1235 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
1236 ReleaseDC(fe->hwnd, hdc);
1238 midend_redraw(fe->me);
1241 static int is_alt_pressed(void)
1244 int r = GetKeyboardState(keystate);
1247 if (keystate[VK_MENU] & 0x80)
1249 if (keystate[VK_RMENU] & 0x80)
1254 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
1255 WPARAM wParam, LPARAM lParam)
1257 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1261 DestroyWindow(hwnd);
1264 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
1266 if (!midend_process_key(fe->me, 0, 0, 'n'))
1270 midend_restart_game(fe->me);
1273 if (!midend_process_key(fe->me, 0, 0, 'u'))
1277 if (!midend_process_key(fe->me, 0, 0, '\x12'))
1282 char *text = midend_text_format(fe->me);
1284 write_clip(hwnd, text);
1286 MessageBeep(MB_ICONWARNING);
1292 char *msg = midend_solve(fe->me);
1294 MessageBox(hwnd, msg, "Unable to solve",
1295 MB_ICONERROR | MB_OK);
1299 if (!midend_process_key(fe->me, 0, 0, 'q'))
1303 if (get_config(fe, CFG_SETTINGS))
1307 if (get_config(fe, CFG_SEED))
1311 if (get_config(fe, CFG_DESC))
1318 assert(fe->help_path);
1319 WinHelp(hwnd, fe->help_path,
1320 fe->help_has_contents ? HELP_FINDER : HELP_CONTENTS, 0);
1323 assert(fe->help_path);
1324 assert(thegame.winhelp_topic);
1326 char *cmd = snewn(10+strlen(thegame.winhelp_topic), char);
1327 sprintf(cmd, "JI(`',`%s')", thegame.winhelp_topic);
1328 WinHelp(hwnd, fe->help_path, HELP_COMMAND, (DWORD)cmd);
1334 int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10;
1336 if (p >= 0 && p < fe->npresets) {
1337 midend_set_params(fe->me, fe->presets[p]);
1353 hdc = BeginPaint(hwnd, &p);
1354 hdc2 = CreateCompatibleDC(hdc);
1355 prevbm = SelectObject(hdc2, fe->bitmap);
1357 p.rcPaint.left, p.rcPaint.top,
1358 p.rcPaint.right - p.rcPaint.left,
1359 p.rcPaint.bottom - p.rcPaint.top,
1361 p.rcPaint.left, p.rcPaint.top,
1363 SelectObject(hdc2, prevbm);
1372 int r = GetKeyboardState(keystate);
1373 int shift = (r && (keystate[VK_SHIFT] & 0x80)) ? MOD_SHFT : 0;
1374 int ctrl = (r && (keystate[VK_CONTROL] & 0x80)) ? MOD_CTRL : 0;
1378 if (!(lParam & 0x01000000))
1379 key = MOD_NUM_KEYPAD | '4';
1381 key = shift | ctrl | CURSOR_LEFT;
1384 if (!(lParam & 0x01000000))
1385 key = MOD_NUM_KEYPAD | '6';
1387 key = shift | ctrl | CURSOR_RIGHT;
1390 if (!(lParam & 0x01000000))
1391 key = MOD_NUM_KEYPAD | '8';
1393 key = shift | ctrl | CURSOR_UP;
1396 if (!(lParam & 0x01000000))
1397 key = MOD_NUM_KEYPAD | '2';
1399 key = shift | ctrl | CURSOR_DOWN;
1402 * Diagonal keys on the numeric keypad.
1405 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '9';
1408 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '3';
1411 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '7';
1414 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '1';
1417 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '0';
1420 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '5';
1423 * Numeric keypad keys with Num Lock on.
1425 case VK_NUMPAD4: key = MOD_NUM_KEYPAD | '4'; break;
1426 case VK_NUMPAD6: key = MOD_NUM_KEYPAD | '6'; break;
1427 case VK_NUMPAD8: key = MOD_NUM_KEYPAD | '8'; break;
1428 case VK_NUMPAD2: key = MOD_NUM_KEYPAD | '2'; break;
1429 case VK_NUMPAD5: key = MOD_NUM_KEYPAD | '5'; break;
1430 case VK_NUMPAD9: key = MOD_NUM_KEYPAD | '9'; break;
1431 case VK_NUMPAD3: key = MOD_NUM_KEYPAD | '3'; break;
1432 case VK_NUMPAD7: key = MOD_NUM_KEYPAD | '7'; break;
1433 case VK_NUMPAD1: key = MOD_NUM_KEYPAD | '1'; break;
1434 case VK_NUMPAD0: key = MOD_NUM_KEYPAD | '0'; break;
1438 if (!midend_process_key(fe->me, 0, 0, key))
1443 m.message = WM_KEYDOWN;
1445 m.lParam = lParam & 0xdfff;
1446 TranslateMessage(&m);
1450 case WM_LBUTTONDOWN:
1451 case WM_RBUTTONDOWN:
1452 case WM_MBUTTONDOWN:
1457 * Shift-clicks count as middle-clicks, since otherwise
1458 * two-button Windows users won't have any kind of
1459 * middle click to use.
1461 if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT))
1462 button = MIDDLE_BUTTON;
1463 else if (message == WM_RBUTTONDOWN || is_alt_pressed())
1464 button = RIGHT_BUTTON;
1466 button = LEFT_BUTTON;
1468 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1469 (signed short)HIWORD(lParam), button))
1482 * Shift-clicks count as middle-clicks, since otherwise
1483 * two-button Windows users won't have any kind of
1484 * middle click to use.
1486 if (message == WM_MBUTTONUP || (wParam & MK_SHIFT))
1487 button = MIDDLE_RELEASE;
1488 else if (message == WM_RBUTTONUP || is_alt_pressed())
1489 button = RIGHT_RELEASE;
1491 button = LEFT_RELEASE;
1493 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1494 (signed short)HIWORD(lParam), button))
1504 if (wParam & (MK_MBUTTON | MK_SHIFT))
1505 button = MIDDLE_DRAG;
1506 else if (wParam & MK_RBUTTON || is_alt_pressed())
1507 button = RIGHT_DRAG;
1511 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1512 (signed short)HIWORD(lParam), button))
1517 if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam))
1522 DWORD now = GetTickCount();
1523 float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F;
1524 midend_timer(fe->me, elapsed);
1525 fe->timer_last_tickcount = now;
1530 return DefWindowProc(hwnd, message, wParam, lParam);
1533 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
1538 InitCommonControls();
1544 wndclass.lpfnWndProc = WndProc;
1545 wndclass.cbClsExtra = 0;
1546 wndclass.cbWndExtra = 0;
1547 wndclass.hInstance = inst;
1548 wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION);
1549 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1550 wndclass.hbrBackground = NULL;
1551 wndclass.lpszMenuName = NULL;
1552 wndclass.lpszClassName = thegame.name;
1554 RegisterClass(&wndclass);
1557 while (*cmdline && isspace(*cmdline))
1560 if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) {
1562 sprintf(buf, "%.100s Error", thegame.name);
1563 MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
1567 while (GetMessage(&msg, NULL, 0, 0)) {
1568 DispatchMessage(&msg);