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_circle(frontend *fe, int cx, int cy, int radius,
334 int fill, int colour)
337 HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[colour]);
338 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
339 Ellipse(fe->hdc_bm, cx - radius, cy - radius,
340 cx + radius + 1, cy + radius + 1);
341 SelectObject(fe->hdc_bm, oldbrush);
342 SelectObject(fe->hdc_bm, oldpen);
344 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
345 MoveToEx(fe->hdc_bm, cx + radius, cy, NULL);
346 AngleArc(fe->hdc_bm, cx, cy, radius, 0.0F, 360.0F);
347 SelectObject(fe->hdc_bm, oldpen);
351 void draw_polygon(frontend *fe, int *coords, int npoints,
352 int fill, int colour)
354 POINT *pts = snewn(npoints+1, POINT);
357 for (i = 0; i <= npoints; i++) {
358 int j = (i < npoints ? i : 0);
359 pts[i].x = coords[j*2];
360 pts[i].y = coords[j*2+1];
364 HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[colour]);
365 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
366 Polygon(fe->hdc_bm, pts, npoints);
367 SelectObject(fe->hdc_bm, oldbrush);
368 SelectObject(fe->hdc_bm, oldpen);
370 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
371 Polyline(fe->hdc_bm, pts, npoints+1);
372 SelectObject(fe->hdc_bm, oldpen);
378 void start_draw(frontend *fe)
381 hdc_win = GetDC(fe->hwnd);
382 fe->hdc_bm = CreateCompatibleDC(hdc_win);
383 fe->prevbm = SelectObject(fe->hdc_bm, fe->bitmap);
384 ReleaseDC(fe->hwnd, hdc_win);
386 SetMapMode(fe->hdc_bm, MM_TEXT);
389 void draw_update(frontend *fe, int x, int y, int w, int h)
398 InvalidateRect(fe->hwnd, &r, FALSE);
401 void end_draw(frontend *fe)
403 SelectObject(fe->hdc_bm, fe->prevbm);
404 DeleteDC(fe->hdc_bm);
406 DeleteObject(fe->clip);
411 void deactivate_timer(frontend *fe)
413 KillTimer(fe->hwnd, fe->timer);
417 void activate_timer(frontend *fe)
420 fe->timer = SetTimer(fe->hwnd, fe->timer, 20, NULL);
421 fe->timer_last_tickcount = GetTickCount();
425 void write_clip(HWND hwnd, char *data)
433 * Windows expects CRLF in the clipboard, so we must convert
434 * any \n that has come out of the puzzle backend.
437 for (i = 0; data[i]; i++) {
442 data2 = snewn(len+1, char);
444 for (i = 0; data[i]; i++) {
447 data2[j++] = data[i];
452 clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
455 lock = GlobalLock(clipdata);
458 memcpy(lock, data2, len);
459 ((unsigned char *) lock)[len] = 0;
460 GlobalUnlock(clipdata);
462 if (OpenClipboard(hwnd)) {
464 SetClipboardData(CF_TEXT, clipdata);
467 GlobalFree(clipdata);
473 * See if we can find a help file.
475 static void find_help_file(frontend *fe)
477 char b[2048], *p, *q, *r;
479 if (!fe->help_path) {
480 GetModuleFileName(NULL, b, sizeof(b) - 1);
482 p = strrchr(b, '\\');
483 if (p && p >= r) r = p+1;
485 if (q && q >= r) r = q+1;
486 strcpy(r, HELP_FILE_NAME);
487 if ( (fp = fopen(b, "r")) != NULL) {
488 fe->help_path = dupstr(b);
491 fe->help_path = NULL;
492 strcpy(r, HELP_CNT_NAME);
493 if ( (fp = fopen(b, "r")) != NULL) {
494 fe->help_has_contents = TRUE;
497 fe->help_has_contents = FALSE;
501 static void check_window_size(frontend *fe, int *px, int *py)
508 GetWindowRect(fe->statusbar, &sr);
509 sy = sr.bottom - sr.top;
515 * See if we actually got the window size we wanted, and adjust
516 * the puzzle size if not.
518 GetClientRect(fe->hwnd, &r);
519 x = r.right - r.left;
520 y = r.bottom - r.top - sy;
521 midend_size(fe->me, &x, &y, FALSE);
522 if (x != r.right - r.left || y != r.bottom - r.top) {
524 * Resize the window, now we know what size we _really_
530 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
531 (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED),
533 SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left, r.bottom - r.top,
534 SWP_NOMOVE | SWP_NOZORDER);
538 GetClientRect(fe->hwnd, &r);
539 SetWindowPos(fe->statusbar, NULL, 0, r.bottom-r.top-sy, r.right-r.left,
547 static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
556 fe->me = midend_new(fe, &thegame);
559 *error = midend_game_id(fe->me, game_id);
567 fe->help_path = NULL;
571 midend_new_game(fe->me);
576 fe->nfonts = fe->fontsize = 0;
578 fe->laststatus = NULL;
584 colours = midend_colours(fe->me, &ncolours);
586 fe->colours = snewn(ncolours, COLORREF);
587 fe->brushes = snewn(ncolours, HBRUSH);
588 fe->pens = snewn(ncolours, HPEN);
590 for (i = 0; i < ncolours; i++) {
591 fe->colours[i] = RGB(255 * colours[i*3+0],
592 255 * colours[i*3+1],
593 255 * colours[i*3+2]);
594 fe->brushes[i] = CreateSolidBrush(fe->colours[i]);
595 fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]);
599 x = y = INT_MAX; /* find puzzle's preferred size */
600 midend_size(fe->me, &x, &y, FALSE);
605 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
606 (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED),
609 fe->hwnd = CreateWindowEx(0, thegame.name, thegame.name,
610 WS_OVERLAPPEDWINDOW &~
611 (WS_THICKFRAME | WS_MAXIMIZEBOX),
612 CW_USEDEFAULT, CW_USEDEFAULT,
613 r.right - r.left, r.bottom - r.top,
614 NULL, NULL, inst, NULL);
616 if (midend_wants_statusbar(fe->me)) {
618 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh",
619 WS_CHILD | WS_VISIBLE,
620 0, 0, 0, 0, /* status bar does these */
621 fe->hwnd, NULL, inst, NULL);
623 * Now resize the window to take account of the status bar.
625 GetWindowRect(fe->statusbar, &sr);
626 GetWindowRect(fe->hwnd, &r);
627 SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left,
628 r.bottom - r.top + sr.bottom - sr.top,
629 SWP_NOMOVE | SWP_NOZORDER);
631 fe->statusbar = NULL;
635 HMENU bar = CreateMenu();
636 HMENU menu = CreateMenu();
638 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Game");
639 AppendMenu(menu, MF_ENABLED, IDM_NEW, "New");
640 AppendMenu(menu, MF_ENABLED, IDM_RESTART, "Restart");
641 AppendMenu(menu, MF_ENABLED, IDM_DESC, "Specific...");
642 AppendMenu(menu, MF_ENABLED, IDM_SEED, "Random Seed...");
644 if ((fe->npresets = midend_num_presets(fe->me)) > 0 ||
645 thegame.can_configure) {
646 HMENU sub = CreateMenu();
649 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "Type");
651 fe->presets = snewn(fe->npresets, game_params *);
653 for (i = 0; i < fe->npresets; i++) {
656 midend_fetch_preset(fe->me, i, &name, &fe->presets[i]);
659 * FIXME: we ought to go through and do something
660 * with ampersands here.
663 AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name);
666 if (thegame.can_configure) {
667 AppendMenu(sub, MF_ENABLED, IDM_CONFIG, "Custom...");
671 AppendMenu(menu, MF_SEPARATOR, 0, 0);
672 AppendMenu(menu, MF_ENABLED, IDM_UNDO, "Undo");
673 AppendMenu(menu, MF_ENABLED, IDM_REDO, "Redo");
674 if (thegame.can_format_as_text) {
675 AppendMenu(menu, MF_SEPARATOR, 0, 0);
676 AppendMenu(menu, MF_ENABLED, IDM_COPY, "Copy");
678 if (thegame.can_solve) {
679 AppendMenu(menu, MF_SEPARATOR, 0, 0);
680 AppendMenu(menu, MF_ENABLED, IDM_SOLVE, "Solve");
682 AppendMenu(menu, MF_SEPARATOR, 0, 0);
683 AppendMenu(menu, MF_ENABLED, IDM_QUIT, "Exit");
685 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Help");
686 AppendMenu(menu, MF_ENABLED, IDM_ABOUT, "About");
688 AppendMenu(menu, MF_SEPARATOR, 0, 0);
689 AppendMenu(menu, MF_ENABLED, IDM_HELPC, "Contents");
690 if (thegame.winhelp_topic) {
692 assert(thegame.name);
693 item = snewn(9+strlen(thegame.name), char); /*ick*/
694 sprintf(item, "Help on %s", thegame.name);
695 AppendMenu(menu, MF_ENABLED, IDM_GAMEHELP, item);
699 SetMenu(fe->hwnd, bar);
702 check_window_size(fe, &x, &y);
704 hdc = GetDC(fe->hwnd);
705 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
706 ReleaseDC(fe->hwnd, hdc);
708 SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe);
710 ShowWindow(fe->hwnd, SW_NORMAL);
711 SetForegroundWindow(fe->hwnd);
713 midend_redraw(fe->me);
718 static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg,
719 WPARAM wParam, LPARAM lParam)
721 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
728 if ((HIWORD(wParam) == BN_CLICKED ||
729 HIWORD(wParam) == BN_DOUBLECLICKED) &&
730 LOWORD(wParam) == IDOK)
742 static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg,
743 WPARAM wParam, LPARAM lParam)
745 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
755 * OK and Cancel are special cases.
757 if ((HIWORD(wParam) == BN_CLICKED ||
758 HIWORD(wParam) == BN_DOUBLECLICKED) &&
759 (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) {
760 if (LOWORD(wParam) == IDOK) {
761 char *err = midend_set_config(fe->me, fe->cfg_which, fe->cfg);
764 MessageBox(hwnd, err, "Validation error",
765 MB_ICONERROR | MB_OK);
776 * First find the control whose id this is.
778 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
779 if (j->ctlid == LOWORD(wParam))
782 if (i->type == C_END)
783 return 0; /* not our problem */
785 if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) {
787 GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer));
788 buffer[lenof(buffer)-1] = '\0';
790 i->sval = dupstr(buffer);
791 } else if (i->type == C_BOOLEAN &&
792 (HIWORD(wParam) == BN_CLICKED ||
793 HIWORD(wParam) == BN_DOUBLECLICKED)) {
794 i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid);
795 } else if (i->type == C_CHOICES &&
796 HIWORD(wParam) == CBN_SELCHANGE) {
797 i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid,
811 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
812 char *wclass, int wstyle,
813 int exstyle, const char *wtext, int wid)
816 ret = CreateWindowEx(exstyle, wclass, wtext,
817 wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1,
818 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
819 SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0));
823 static void about(frontend *fe)
833 int winwidth, winheight, y;
834 int height, width, maxwid;
835 const char *strings[16];
840 sprintf(titlebuf, "About %.250s", thegame.name);
842 strings[nstrings++] = thegame.name;
843 strings[nstrings++] = "from Simon Tatham's Portable Puzzle Collection";
844 strings[nstrings++] = ver;
846 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
847 wc.lpfnWndProc = DefDlgProc;
849 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
850 wc.hInstance = fe->inst;
852 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
853 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
854 wc.lpszMenuName = NULL;
855 wc.lpszClassName = "GameAboutBox";
858 hdc = GetDC(fe->hwnd);
859 SetMapMode(hdc, MM_TEXT);
861 fe->dlg_done = FALSE;
863 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
865 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
866 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
871 oldfont = SelectObject(hdc, fe->cfgfont);
872 if (GetTextMetrics(hdc, &tm)) {
873 height = tm.tmAscent + tm.tmDescent;
874 width = tm.tmAveCharWidth;
880 * Figure out the layout of the About box by measuring the
881 * length of each piece of text.
884 winheight = height/2;
886 for (i = 0; i < nstrings; i++) {
887 if (GetTextExtentPoint32(hdc, strings[i], strlen(strings[i]), &size))
888 lengths[i] = size.cx;
890 lengths[i] = 0; /* *shrug* */
891 if (maxwid < lengths[i])
893 winheight += height * 3 / 2 + (height / 2);
896 winheight += height + height * 7 / 4; /* OK button */
897 winwidth = maxwid + 4*width;
899 SelectObject(hdc, oldfont);
900 ReleaseDC(fe->hwnd, hdc);
903 * Create the dialog, now that we know its size.
910 r.bottom = winheight;
912 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
913 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
914 WS_CAPTION | WS_SYSMENU*/) &~
915 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
919 * Centre the dialog on its parent window.
923 GetWindowRect(fe->hwnd, &r2);
924 r.left = (r2.left + r2.right - r.right) / 2;
925 r.top = (r2.top + r2.bottom - r.bottom) / 2;
929 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, titlebuf,
930 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
931 WS_CAPTION | WS_SYSMENU,
933 r.right-r.left, r.bottom-r.top,
934 fe->hwnd, NULL, fe->inst, NULL);
937 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
939 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
940 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)AboutDlgProc);
944 for (i = 0; i < nstrings; i++) {
945 int border = width*2 + (maxwid - lengths[i]) / 2;
946 mkctrl(fe, border, border+lengths[i], y+height*1/8, y+height*9/8,
947 "Static", 0, 0, strings[i], id++);
950 assert(y < winheight);
954 y += height/2; /* extra space before OK */
955 mkctrl(fe, width*2, maxwid+width*2, y, y+height*7/4, "BUTTON",
956 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
959 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
961 EnableWindow(fe->hwnd, FALSE);
962 ShowWindow(fe->cfgbox, SW_NORMAL);
963 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
964 if (!IsDialogMessage(fe->cfgbox, &msg))
965 DispatchMessage(&msg);
969 EnableWindow(fe->hwnd, TRUE);
970 SetForegroundWindow(fe->hwnd);
971 DestroyWindow(fe->cfgbox);
972 DeleteObject(fe->cfgfont);
975 static int get_config(frontend *fe, int which)
988 int winwidth, winheight, col1l, col1r, col2l, col2r, y;
989 int height, width, maxlabel, maxcheckbox;
991 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
992 wc.lpfnWndProc = DefDlgProc;
994 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
995 wc.hInstance = fe->inst;
997 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
998 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
999 wc.lpszMenuName = NULL;
1000 wc.lpszClassName = "GameConfigBox";
1003 hdc = GetDC(fe->hwnd);
1004 SetMapMode(hdc, MM_TEXT);
1006 fe->dlg_done = FALSE;
1008 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
1010 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
1011 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
1016 oldfont = SelectObject(hdc, fe->cfgfont);
1017 if (GetTextMetrics(hdc, &tm)) {
1018 height = tm.tmAscent + tm.tmDescent;
1019 width = tm.tmAveCharWidth;
1021 height = width = 30;
1024 fe->cfg = midend_get_config(fe->me, which, &title);
1025 fe->cfg_which = which;
1028 * Figure out the layout of the config box by measuring the
1029 * length of each piece of text.
1031 maxlabel = maxcheckbox = 0;
1032 winheight = height/2;
1034 for (i = fe->cfg; i->type != C_END; i++) {
1039 * Both these control types have a label filling only
1040 * the left-hand column of the box.
1042 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
1045 winheight += height * 3 / 2 + (height / 2);
1050 * Checkboxes take up the whole of the box width.
1052 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
1053 maxcheckbox < size.cx)
1054 maxcheckbox = size.cx;
1055 winheight += height + (height / 2);
1060 winheight += height + height * 7 / 4; /* OK / Cancel buttons */
1063 col1r = col1l + maxlabel;
1064 col2l = col1r + 2*width;
1065 col2r = col2l + 30*width;
1066 if (col2r < col1l+2*height+maxcheckbox)
1067 col2r = col1l+2*height+maxcheckbox;
1068 winwidth = col2r + 2*width;
1070 SelectObject(hdc, oldfont);
1071 ReleaseDC(fe->hwnd, hdc);
1074 * Create the dialog, now that we know its size.
1081 r.bottom = winheight;
1083 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
1084 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1085 WS_CAPTION | WS_SYSMENU*/) &~
1086 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
1090 * Centre the dialog on its parent window.
1094 GetWindowRect(fe->hwnd, &r2);
1095 r.left = (r2.left + r2.right - r.right) / 2;
1096 r.top = (r2.top + r2.bottom - r.bottom) / 2;
1100 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title,
1101 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1102 WS_CAPTION | WS_SYSMENU,
1104 r.right-r.left, r.bottom-r.top,
1105 fe->hwnd, NULL, fe->inst, NULL);
1109 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
1111 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
1112 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc);
1115 * Count the controls so we can allocate cfgaux.
1117 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
1119 fe->cfgaux = snewn(nctrls, struct cfg_aux);
1123 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
1127 * Edit box with a label beside it.
1129 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
1130 "Static", 0, 0, i->name, id++);
1131 ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2,
1132 "EDIT", WS_TABSTOP | ES_AUTOHSCROLL,
1133 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
1134 SetWindowText(ctl, i->sval);
1142 mkctrl(fe, col1l, col2r, y, y+height, "BUTTON",
1143 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
1144 0, i->name, (j->ctlid = id++));
1145 CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0));
1151 * Drop-down list with a label beside it.
1153 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
1154 "STATIC", 0, 0, i->name, id++);
1155 ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2,
1156 "COMBOBOX", WS_TABSTOP |
1157 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
1158 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
1160 char c, *p, *q, *str;
1162 SendMessage(ctl, CB_RESETCONTENT, 0, 0);
1167 while (*q && *q != c) q++;
1168 str = snewn(q-p+1, char);
1169 strncpy(str, p, q-p);
1171 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str);
1178 SendMessage(ctl, CB_SETCURSEL, i->ival, 0);
1184 assert(y < winheight);
1188 y += height/2; /* extra space before OK and Cancel */
1189 mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON",
1190 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
1192 mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON",
1193 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL);
1195 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
1197 EnableWindow(fe->hwnd, FALSE);
1198 ShowWindow(fe->cfgbox, SW_NORMAL);
1199 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
1200 if (!IsDialogMessage(fe->cfgbox, &msg))
1201 DispatchMessage(&msg);
1205 EnableWindow(fe->hwnd, TRUE);
1206 SetForegroundWindow(fe->hwnd);
1207 DestroyWindow(fe->cfgbox);
1208 DeleteObject(fe->cfgfont);
1213 return (fe->dlg_done == 2);
1216 static void new_game_type(frontend *fe)
1222 midend_new_game(fe->me);
1224 midend_size(fe->me, &x, &y, FALSE);
1229 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
1230 (WS_THICKFRAME | WS_MAXIMIZEBOX |
1234 if (fe->statusbar != NULL) {
1235 GetWindowRect(fe->statusbar, &sr);
1237 sr.left = sr.right = sr.top = sr.bottom = 0;
1239 SetWindowPos(fe->hwnd, NULL, 0, 0,
1241 r.bottom - r.top + sr.bottom - sr.top,
1242 SWP_NOMOVE | SWP_NOZORDER);
1244 check_window_size(fe, &x, &y);
1246 if (fe->statusbar != NULL)
1247 SetWindowPos(fe->statusbar, NULL, 0, y, x,
1248 sr.bottom - sr.top, SWP_NOZORDER);
1250 DeleteObject(fe->bitmap);
1252 hdc = GetDC(fe->hwnd);
1253 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
1254 ReleaseDC(fe->hwnd, hdc);
1256 midend_redraw(fe->me);
1259 static int is_alt_pressed(void)
1262 int r = GetKeyboardState(keystate);
1265 if (keystate[VK_MENU] & 0x80)
1267 if (keystate[VK_RMENU] & 0x80)
1272 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
1273 WPARAM wParam, LPARAM lParam)
1275 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1279 DestroyWindow(hwnd);
1282 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
1284 if (!midend_process_key(fe->me, 0, 0, 'n'))
1288 midend_restart_game(fe->me);
1291 if (!midend_process_key(fe->me, 0, 0, 'u'))
1295 if (!midend_process_key(fe->me, 0, 0, '\x12'))
1300 char *text = midend_text_format(fe->me);
1302 write_clip(hwnd, text);
1304 MessageBeep(MB_ICONWARNING);
1310 char *msg = midend_solve(fe->me);
1312 MessageBox(hwnd, msg, "Unable to solve",
1313 MB_ICONERROR | MB_OK);
1317 if (!midend_process_key(fe->me, 0, 0, 'q'))
1321 if (get_config(fe, CFG_SETTINGS))
1325 if (get_config(fe, CFG_SEED))
1329 if (get_config(fe, CFG_DESC))
1336 assert(fe->help_path);
1337 WinHelp(hwnd, fe->help_path,
1338 fe->help_has_contents ? HELP_FINDER : HELP_CONTENTS, 0);
1341 assert(fe->help_path);
1342 assert(thegame.winhelp_topic);
1344 char *cmd = snewn(10+strlen(thegame.winhelp_topic), char);
1345 sprintf(cmd, "JI(`',`%s')", thegame.winhelp_topic);
1346 WinHelp(hwnd, fe->help_path, HELP_COMMAND, (DWORD)cmd);
1352 int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10;
1354 if (p >= 0 && p < fe->npresets) {
1355 midend_set_params(fe->me, fe->presets[p]);
1371 hdc = BeginPaint(hwnd, &p);
1372 hdc2 = CreateCompatibleDC(hdc);
1373 prevbm = SelectObject(hdc2, fe->bitmap);
1375 p.rcPaint.left, p.rcPaint.top,
1376 p.rcPaint.right - p.rcPaint.left,
1377 p.rcPaint.bottom - p.rcPaint.top,
1379 p.rcPaint.left, p.rcPaint.top,
1381 SelectObject(hdc2, prevbm);
1390 int r = GetKeyboardState(keystate);
1391 int shift = (r && (keystate[VK_SHIFT] & 0x80)) ? MOD_SHFT : 0;
1392 int ctrl = (r && (keystate[VK_CONTROL] & 0x80)) ? MOD_CTRL : 0;
1396 if (!(lParam & 0x01000000))
1397 key = MOD_NUM_KEYPAD | '4';
1399 key = shift | ctrl | CURSOR_LEFT;
1402 if (!(lParam & 0x01000000))
1403 key = MOD_NUM_KEYPAD | '6';
1405 key = shift | ctrl | CURSOR_RIGHT;
1408 if (!(lParam & 0x01000000))
1409 key = MOD_NUM_KEYPAD | '8';
1411 key = shift | ctrl | CURSOR_UP;
1414 if (!(lParam & 0x01000000))
1415 key = MOD_NUM_KEYPAD | '2';
1417 key = shift | ctrl | CURSOR_DOWN;
1420 * Diagonal keys on the numeric keypad.
1423 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '9';
1426 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '3';
1429 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '7';
1432 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '1';
1435 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '0';
1438 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '5';
1441 * Numeric keypad keys with Num Lock on.
1443 case VK_NUMPAD4: key = MOD_NUM_KEYPAD | '4'; break;
1444 case VK_NUMPAD6: key = MOD_NUM_KEYPAD | '6'; break;
1445 case VK_NUMPAD8: key = MOD_NUM_KEYPAD | '8'; break;
1446 case VK_NUMPAD2: key = MOD_NUM_KEYPAD | '2'; break;
1447 case VK_NUMPAD5: key = MOD_NUM_KEYPAD | '5'; break;
1448 case VK_NUMPAD9: key = MOD_NUM_KEYPAD | '9'; break;
1449 case VK_NUMPAD3: key = MOD_NUM_KEYPAD | '3'; break;
1450 case VK_NUMPAD7: key = MOD_NUM_KEYPAD | '7'; break;
1451 case VK_NUMPAD1: key = MOD_NUM_KEYPAD | '1'; break;
1452 case VK_NUMPAD0: key = MOD_NUM_KEYPAD | '0'; break;
1456 if (!midend_process_key(fe->me, 0, 0, key))
1461 m.message = WM_KEYDOWN;
1463 m.lParam = lParam & 0xdfff;
1464 TranslateMessage(&m);
1468 case WM_LBUTTONDOWN:
1469 case WM_RBUTTONDOWN:
1470 case WM_MBUTTONDOWN:
1475 * Shift-clicks count as middle-clicks, since otherwise
1476 * two-button Windows users won't have any kind of
1477 * middle click to use.
1479 if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT))
1480 button = MIDDLE_BUTTON;
1481 else if (message == WM_RBUTTONDOWN || is_alt_pressed())
1482 button = RIGHT_BUTTON;
1484 button = LEFT_BUTTON;
1486 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1487 (signed short)HIWORD(lParam), button))
1500 * Shift-clicks count as middle-clicks, since otherwise
1501 * two-button Windows users won't have any kind of
1502 * middle click to use.
1504 if (message == WM_MBUTTONUP || (wParam & MK_SHIFT))
1505 button = MIDDLE_RELEASE;
1506 else if (message == WM_RBUTTONUP || is_alt_pressed())
1507 button = RIGHT_RELEASE;
1509 button = LEFT_RELEASE;
1511 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1512 (signed short)HIWORD(lParam), button))
1522 if (wParam & (MK_MBUTTON | MK_SHIFT))
1523 button = MIDDLE_DRAG;
1524 else if (wParam & MK_RBUTTON || is_alt_pressed())
1525 button = RIGHT_DRAG;
1529 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1530 (signed short)HIWORD(lParam), button))
1535 if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam))
1540 DWORD now = GetTickCount();
1541 float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F;
1542 midend_timer(fe->me, elapsed);
1543 fe->timer_last_tickcount = now;
1548 return DefWindowProc(hwnd, message, wParam, lParam);
1551 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
1556 InitCommonControls();
1562 wndclass.lpfnWndProc = WndProc;
1563 wndclass.cbClsExtra = 0;
1564 wndclass.cbWndExtra = 0;
1565 wndclass.hInstance = inst;
1566 wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION);
1567 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1568 wndclass.hbrBackground = NULL;
1569 wndclass.lpszMenuName = NULL;
1570 wndclass.lpszClassName = thegame.name;
1572 RegisterClass(&wndclass);
1575 while (*cmdline && isspace(*cmdline))
1578 if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) {
1580 sprintf(buf, "%.100s Error", thegame.name);
1581 MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
1585 while (GetMessage(&msg, NULL, 0, 0)) {
1586 DispatchMessage(&msg);