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_SAVE 0x00E0
32 #define IDM_LOAD 0x00F0
33 #define IDM_PRESETS 0x0100
35 #define HELP_FILE_NAME "puzzles.hlp"
36 #define HELP_CNT_NAME "puzzles.cnt"
39 static FILE *debug_fp = NULL;
40 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
41 static int debug_got_console = 0;
47 if (!debug_got_console) {
49 debug_got_console = 1;
50 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
54 debug_fp = fopen("debug.log", "w");
57 if (debug_hdl != INVALID_HANDLE_VALUE) {
58 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
64 void debug_printf(char *fmt, ...)
70 vsprintf(buf, fmt, ap);
94 HWND hwnd, statusbar, cfgbox;
96 HBITMAP bitmap, prevbm;
103 DWORD timer_last_tickcount;
105 game_params **presets;
107 int nfonts, fontsize;
109 struct cfg_aux *cfgaux;
110 int cfg_which, dlg_done;
113 int help_has_contents;
117 void fatal(char *fmt, ...)
123 vsprintf(buf, fmt, ap);
126 MessageBox(NULL, buf, "Fatal error", MB_ICONEXCLAMATION | MB_OK);
131 void get_random_seed(void **randseed, int *randseedsize)
133 time_t *tp = snew(time_t);
135 *randseed = (void *)tp;
136 *randseedsize = sizeof(time_t);
139 void status_bar(frontend *fe, char *text)
141 char *rewritten = midend_rewrite_statusbar(fe->me, text);
142 if (!fe->laststatus || strcmp(rewritten, fe->laststatus)) {
143 SetWindowText(fe->statusbar, rewritten);
144 sfree(fe->laststatus);
145 fe->laststatus = rewritten;
151 blitter *blitter_new(int w, int h)
153 blitter *bl = snew(blitter);
155 memset(bl, 0, sizeof(blitter));
163 void blitter_free(blitter *bl)
165 if (bl->bitmap) DeleteObject(bl->bitmap);
169 static void blitter_mkbitmap(frontend *fe, blitter *bl)
171 HDC hdc = GetDC(fe->hwnd);
172 bl->bitmap = CreateCompatibleBitmap(hdc, bl->w, bl->h);
173 ReleaseDC(fe->hwnd, hdc);
176 /* BitBlt(dstDC, dstX, dstY, dstW, dstH, srcDC, srcX, srcY, dType) */
178 void blitter_save(frontend *fe, blitter *bl, int x, int y)
180 HDC hdc_win, hdc_blit;
183 if (!bl->bitmap) blitter_mkbitmap(fe, bl);
185 bl->x = x; bl->y = y;
187 hdc_win = GetDC(fe->hwnd);
188 hdc_blit = CreateCompatibleDC(hdc_win);
189 if (!hdc_blit) fatal("hdc_blit failed: 0x%x", GetLastError());
191 prev_blit = SelectObject(hdc_blit, bl->bitmap);
192 if (prev_blit == NULL || prev_blit == HGDI_ERROR)
193 fatal("SelectObject for hdc_main failed: 0x%x", GetLastError());
195 if (!BitBlt(hdc_blit, 0, 0, bl->w, bl->h,
196 fe->hdc_bm, x, y, SRCCOPY))
197 fatal("BitBlt failed: 0x%x", GetLastError());
199 SelectObject(hdc_blit, prev_blit);
201 ReleaseDC(fe->hwnd, hdc_win);
204 void blitter_load(frontend *fe, blitter *bl, int x, int y)
206 HDC hdc_win, hdc_blit;
209 assert(bl->bitmap); /* we should always have saved before loading */
211 if (x == BLITTER_FROMSAVED) x = bl->x;
212 if (y == BLITTER_FROMSAVED) y = bl->y;
214 hdc_win = GetDC(fe->hwnd);
215 hdc_blit = CreateCompatibleDC(hdc_win);
217 prev_blit = SelectObject(hdc_blit, bl->bitmap);
219 BitBlt(fe->hdc_bm, x, y, bl->w, bl->h,
220 hdc_blit, 0, 0, SRCCOPY);
222 SelectObject(hdc_blit, prev_blit);
224 ReleaseDC(fe->hwnd, hdc_win);
227 void frontend_default_colour(frontend *fe, float *output)
229 DWORD c = GetSysColor(COLOR_MENU); /* ick */
231 output[0] = (float)(GetRValue(c) / 255.0);
232 output[1] = (float)(GetGValue(c) / 255.0);
233 output[2] = (float)(GetBValue(c) / 255.0);
236 void clip(frontend *fe, int x, int y, int w, int h)
238 IntersectClipRect(fe->hdc_bm, x, y, x+w, y+h);
241 void unclip(frontend *fe)
243 SelectClipRgn(fe->hdc_bm, NULL);
246 void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize,
247 int align, int colour, char *text)
252 * Find or create the font.
254 for (i = 0; i < fe->nfonts; i++)
255 if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
258 if (i == fe->nfonts) {
259 if (fe->fontsize <= fe->nfonts) {
260 fe->fontsize = fe->nfonts + 10;
261 fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
266 fe->fonts[i].type = fonttype;
267 fe->fonts[i].size = fontsize;
269 fe->fonts[i].font = CreateFont(-fontsize, 0, 0, 0, FW_BOLD,
270 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
271 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
273 (fonttype == FONT_FIXED ?
274 FIXED_PITCH | FF_DONTCARE :
275 VARIABLE_PITCH | FF_SWISS),
280 * Position and draw the text.
287 oldfont = SelectObject(fe->hdc_bm, fe->fonts[i].font);
288 if (GetTextMetrics(fe->hdc_bm, &tm)) {
289 if (align & ALIGN_VCENTRE)
290 y -= (tm.tmAscent+tm.tmDescent)/2;
294 if (GetTextExtentPoint32(fe->hdc_bm, text, strlen(text), &size)) {
295 if (align & ALIGN_HCENTRE)
297 else if (align & ALIGN_HRIGHT)
300 SetBkMode(fe->hdc_bm, TRANSPARENT);
301 SetTextColor(fe->hdc_bm, fe->colours[colour]);
302 TextOut(fe->hdc_bm, x, y, text, strlen(text));
303 SelectObject(fe->hdc_bm, oldfont);
307 void draw_rect(frontend *fe, int x, int y, int w, int h, int colour)
309 if (w == 1 && h == 1) {
311 * Rectangle() appears to get uppity if asked to draw a 1x1
312 * rectangle, presumably on the grounds that that's beneath
313 * its dignity and you ought to be using SetPixel instead.
316 SetPixel(fe->hdc_bm, x, y, fe->colours[colour]);
318 HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[colour]);
319 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
320 Rectangle(fe->hdc_bm, x, y, x+w, y+h);
321 SelectObject(fe->hdc_bm, oldbrush);
322 SelectObject(fe->hdc_bm, oldpen);
326 void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour)
328 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
329 MoveToEx(fe->hdc_bm, x1, y1, NULL);
330 LineTo(fe->hdc_bm, x2, y2);
331 SetPixel(fe->hdc_bm, x2, y2, fe->colours[colour]);
332 SelectObject(fe->hdc_bm, oldpen);
335 void draw_circle(frontend *fe, int cx, int cy, int radius,
336 int fillcolour, int outlinecolour)
338 assert(outlinecolour >= 0);
340 if (fillcolour >= 0) {
341 HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[fillcolour]);
342 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[outlinecolour]);
343 Ellipse(fe->hdc_bm, cx - radius, cy - radius,
344 cx + radius + 1, cy + radius + 1);
345 SelectObject(fe->hdc_bm, oldbrush);
346 SelectObject(fe->hdc_bm, oldpen);
348 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[outlinecolour]);
349 Arc(fe->hdc_bm, cx - radius, cy - radius,
350 cx + radius + 1, cy + radius + 1,
351 cx - radius, cy, cx - radius, cy);
352 SelectObject(fe->hdc_bm, oldpen);
356 void draw_polygon(frontend *fe, int *coords, int npoints,
357 int fillcolour, int outlinecolour)
359 POINT *pts = snewn(npoints+1, POINT);
362 for (i = 0; i <= npoints; i++) {
363 int j = (i < npoints ? i : 0);
364 pts[i].x = coords[j*2];
365 pts[i].y = coords[j*2+1];
368 assert(outlinecolour >= 0);
370 if (fillcolour >= 0) {
371 HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[fillcolour]);
372 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[outlinecolour]);
373 Polygon(fe->hdc_bm, pts, npoints);
374 SelectObject(fe->hdc_bm, oldbrush);
375 SelectObject(fe->hdc_bm, oldpen);
377 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[outlinecolour]);
378 Polyline(fe->hdc_bm, pts, npoints+1);
379 SelectObject(fe->hdc_bm, oldpen);
385 void start_draw(frontend *fe)
388 hdc_win = GetDC(fe->hwnd);
389 fe->hdc_bm = CreateCompatibleDC(hdc_win);
390 fe->prevbm = SelectObject(fe->hdc_bm, fe->bitmap);
391 ReleaseDC(fe->hwnd, hdc_win);
393 SetMapMode(fe->hdc_bm, MM_TEXT);
396 void draw_update(frontend *fe, int x, int y, int w, int h)
405 InvalidateRect(fe->hwnd, &r, FALSE);
408 void end_draw(frontend *fe)
410 SelectObject(fe->hdc_bm, fe->prevbm);
411 DeleteDC(fe->hdc_bm);
413 DeleteObject(fe->clip);
418 void deactivate_timer(frontend *fe)
420 if (fe->hwnd) KillTimer(fe->hwnd, fe->timer);
424 void activate_timer(frontend *fe)
427 fe->timer = SetTimer(fe->hwnd, fe->timer, 20, NULL);
428 fe->timer_last_tickcount = GetTickCount();
432 void write_clip(HWND hwnd, char *data)
440 * Windows expects CRLF in the clipboard, so we must convert
441 * any \n that has come out of the puzzle backend.
444 for (i = 0; data[i]; i++) {
449 data2 = snewn(len+1, char);
451 for (i = 0; data[i]; i++) {
454 data2[j++] = data[i];
459 clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
462 lock = GlobalLock(clipdata);
465 memcpy(lock, data2, len);
466 ((unsigned char *) lock)[len] = 0;
467 GlobalUnlock(clipdata);
469 if (OpenClipboard(hwnd)) {
471 SetClipboardData(CF_TEXT, clipdata);
474 GlobalFree(clipdata);
480 * See if we can find a help file.
482 static void find_help_file(frontend *fe)
484 char b[2048], *p, *q, *r;
486 if (!fe->help_path) {
487 GetModuleFileName(NULL, b, sizeof(b) - 1);
489 p = strrchr(b, '\\');
490 if (p && p >= r) r = p+1;
492 if (q && q >= r) r = q+1;
493 strcpy(r, HELP_FILE_NAME);
494 if ( (fp = fopen(b, "r")) != NULL) {
495 fe->help_path = dupstr(b);
498 fe->help_path = NULL;
499 strcpy(r, HELP_CNT_NAME);
500 if ( (fp = fopen(b, "r")) != NULL) {
501 fe->help_has_contents = TRUE;
504 fe->help_has_contents = FALSE;
508 static void check_window_size(frontend *fe, int *px, int *py)
515 GetWindowRect(fe->statusbar, &sr);
516 sy = sr.bottom - sr.top;
522 * See if we actually got the window size we wanted, and adjust
523 * the puzzle size if not.
525 GetClientRect(fe->hwnd, &r);
526 x = r.right - r.left;
527 y = r.bottom - r.top - sy;
528 midend_size(fe->me, &x, &y, FALSE);
529 if (x != r.right - r.left || y != r.bottom - r.top) {
531 * Resize the window, now we know what size we _really_
537 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
538 (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED),
540 SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left, r.bottom - r.top,
541 SWP_NOMOVE | SWP_NOZORDER);
545 GetClientRect(fe->hwnd, &r);
546 SetWindowPos(fe->statusbar, NULL, 0, r.bottom-r.top-sy, r.right-r.left,
554 static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
563 fe->me = midend_new(fe, &thegame);
566 *error = midend_game_id(fe->me, game_id);
574 fe->help_path = NULL;
582 midend_new_game(fe->me);
585 fe->nfonts = fe->fontsize = 0;
587 fe->laststatus = NULL;
593 colours = midend_colours(fe->me, &ncolours);
595 fe->colours = snewn(ncolours, COLORREF);
596 fe->brushes = snewn(ncolours, HBRUSH);
597 fe->pens = snewn(ncolours, HPEN);
599 for (i = 0; i < ncolours; i++) {
600 fe->colours[i] = RGB(255 * colours[i*3+0],
601 255 * colours[i*3+1],
602 255 * colours[i*3+2]);
603 fe->brushes[i] = CreateSolidBrush(fe->colours[i]);
604 fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]);
608 x = y = INT_MAX; /* find puzzle's preferred size */
609 midend_size(fe->me, &x, &y, FALSE);
614 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
615 (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED),
618 fe->hwnd = CreateWindowEx(0, thegame.name, thegame.name,
619 WS_OVERLAPPEDWINDOW &~
620 (WS_THICKFRAME | WS_MAXIMIZEBOX),
621 CW_USEDEFAULT, CW_USEDEFAULT,
622 r.right - r.left, r.bottom - r.top,
623 NULL, NULL, inst, NULL);
625 if (midend_wants_statusbar(fe->me)) {
627 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh",
628 WS_CHILD | WS_VISIBLE,
629 0, 0, 0, 0, /* status bar does these */
630 fe->hwnd, NULL, inst, NULL);
632 * Now resize the window to take account of the status bar.
634 GetWindowRect(fe->statusbar, &sr);
635 GetWindowRect(fe->hwnd, &r);
636 SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left,
637 r.bottom - r.top + sr.bottom - sr.top,
638 SWP_NOMOVE | SWP_NOZORDER);
640 fe->statusbar = NULL;
644 HMENU bar = CreateMenu();
645 HMENU menu = CreateMenu();
647 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Game");
648 AppendMenu(menu, MF_ENABLED, IDM_NEW, "New");
649 AppendMenu(menu, MF_ENABLED, IDM_RESTART, "Restart");
650 AppendMenu(menu, MF_ENABLED, IDM_DESC, "Specific...");
651 AppendMenu(menu, MF_ENABLED, IDM_SEED, "Random Seed...");
653 if ((fe->npresets = midend_num_presets(fe->me)) > 0 ||
654 thegame.can_configure) {
655 HMENU sub = CreateMenu();
658 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "Type");
660 fe->presets = snewn(fe->npresets, game_params *);
662 for (i = 0; i < fe->npresets; i++) {
665 midend_fetch_preset(fe->me, i, &name, &fe->presets[i]);
668 * FIXME: we ought to go through and do something
669 * with ampersands here.
672 AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name);
675 if (thegame.can_configure) {
676 AppendMenu(sub, MF_ENABLED, IDM_CONFIG, "Custom...");
680 AppendMenu(menu, MF_SEPARATOR, 0, 0);
681 AppendMenu(menu, MF_ENABLED, IDM_LOAD, "Load");
682 AppendMenu(menu, MF_ENABLED, IDM_SAVE, "Save");
683 AppendMenu(menu, MF_SEPARATOR, 0, 0);
684 AppendMenu(menu, MF_ENABLED, IDM_UNDO, "Undo");
685 AppendMenu(menu, MF_ENABLED, IDM_REDO, "Redo");
686 if (thegame.can_format_as_text) {
687 AppendMenu(menu, MF_SEPARATOR, 0, 0);
688 AppendMenu(menu, MF_ENABLED, IDM_COPY, "Copy");
690 if (thegame.can_solve) {
691 AppendMenu(menu, MF_SEPARATOR, 0, 0);
692 AppendMenu(menu, MF_ENABLED, IDM_SOLVE, "Solve");
694 AppendMenu(menu, MF_SEPARATOR, 0, 0);
695 AppendMenu(menu, MF_ENABLED, IDM_QUIT, "Exit");
697 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Help");
698 AppendMenu(menu, MF_ENABLED, IDM_ABOUT, "About");
700 AppendMenu(menu, MF_SEPARATOR, 0, 0);
701 AppendMenu(menu, MF_ENABLED, IDM_HELPC, "Contents");
702 if (thegame.winhelp_topic) {
704 assert(thegame.name);
705 item = snewn(9+strlen(thegame.name), char); /*ick*/
706 sprintf(item, "Help on %s", thegame.name);
707 AppendMenu(menu, MF_ENABLED, IDM_GAMEHELP, item);
711 SetMenu(fe->hwnd, bar);
714 check_window_size(fe, &x, &y);
716 hdc = GetDC(fe->hwnd);
717 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
718 ReleaseDC(fe->hwnd, hdc);
720 SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe);
722 ShowWindow(fe->hwnd, SW_NORMAL);
723 SetForegroundWindow(fe->hwnd);
725 midend_redraw(fe->me);
730 static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg,
731 WPARAM wParam, LPARAM lParam)
733 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
740 if ((HIWORD(wParam) == BN_CLICKED ||
741 HIWORD(wParam) == BN_DOUBLECLICKED) &&
742 LOWORD(wParam) == IDOK)
754 static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg,
755 WPARAM wParam, LPARAM lParam)
757 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
767 * OK and Cancel are special cases.
769 if ((HIWORD(wParam) == BN_CLICKED ||
770 HIWORD(wParam) == BN_DOUBLECLICKED) &&
771 (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) {
772 if (LOWORD(wParam) == IDOK) {
773 char *err = midend_set_config(fe->me, fe->cfg_which, fe->cfg);
776 MessageBox(hwnd, err, "Validation error",
777 MB_ICONERROR | MB_OK);
788 * First find the control whose id this is.
790 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
791 if (j->ctlid == LOWORD(wParam))
794 if (i->type == C_END)
795 return 0; /* not our problem */
797 if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) {
799 GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer));
800 buffer[lenof(buffer)-1] = '\0';
802 i->sval = dupstr(buffer);
803 } else if (i->type == C_BOOLEAN &&
804 (HIWORD(wParam) == BN_CLICKED ||
805 HIWORD(wParam) == BN_DOUBLECLICKED)) {
806 i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid);
807 } else if (i->type == C_CHOICES &&
808 HIWORD(wParam) == CBN_SELCHANGE) {
809 i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid,
823 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
824 char *wclass, int wstyle,
825 int exstyle, const char *wtext, int wid)
828 ret = CreateWindowEx(exstyle, wclass, wtext,
829 wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1,
830 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
831 SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0));
835 static void about(frontend *fe)
845 int winwidth, winheight, y;
846 int height, width, maxwid;
847 const char *strings[16];
852 sprintf(titlebuf, "About %.250s", thegame.name);
854 strings[nstrings++] = thegame.name;
855 strings[nstrings++] = "from Simon Tatham's Portable Puzzle Collection";
856 strings[nstrings++] = ver;
858 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
859 wc.lpfnWndProc = DefDlgProc;
861 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
862 wc.hInstance = fe->inst;
864 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
865 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
866 wc.lpszMenuName = NULL;
867 wc.lpszClassName = "GameAboutBox";
870 hdc = GetDC(fe->hwnd);
871 SetMapMode(hdc, MM_TEXT);
873 fe->dlg_done = FALSE;
875 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
877 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
878 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
883 oldfont = SelectObject(hdc, fe->cfgfont);
884 if (GetTextMetrics(hdc, &tm)) {
885 height = tm.tmAscent + tm.tmDescent;
886 width = tm.tmAveCharWidth;
892 * Figure out the layout of the About box by measuring the
893 * length of each piece of text.
896 winheight = height/2;
898 for (i = 0; i < nstrings; i++) {
899 if (GetTextExtentPoint32(hdc, strings[i], strlen(strings[i]), &size))
900 lengths[i] = size.cx;
902 lengths[i] = 0; /* *shrug* */
903 if (maxwid < lengths[i])
905 winheight += height * 3 / 2 + (height / 2);
908 winheight += height + height * 7 / 4; /* OK button */
909 winwidth = maxwid + 4*width;
911 SelectObject(hdc, oldfont);
912 ReleaseDC(fe->hwnd, hdc);
915 * Create the dialog, now that we know its size.
922 r.bottom = winheight;
924 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
925 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
926 WS_CAPTION | WS_SYSMENU*/) &~
927 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
931 * Centre the dialog on its parent window.
935 GetWindowRect(fe->hwnd, &r2);
936 r.left = (r2.left + r2.right - r.right) / 2;
937 r.top = (r2.top + r2.bottom - r.bottom) / 2;
941 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, titlebuf,
942 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
943 WS_CAPTION | WS_SYSMENU,
945 r.right-r.left, r.bottom-r.top,
946 fe->hwnd, NULL, fe->inst, NULL);
949 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
951 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
952 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)AboutDlgProc);
956 for (i = 0; i < nstrings; i++) {
957 int border = width*2 + (maxwid - lengths[i]) / 2;
958 mkctrl(fe, border, border+lengths[i], y+height*1/8, y+height*9/8,
959 "Static", 0, 0, strings[i], id++);
962 assert(y < winheight);
966 y += height/2; /* extra space before OK */
967 mkctrl(fe, width*2, maxwid+width*2, y, y+height*7/4, "BUTTON",
968 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
971 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
973 EnableWindow(fe->hwnd, FALSE);
974 ShowWindow(fe->cfgbox, SW_NORMAL);
975 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
976 if (!IsDialogMessage(fe->cfgbox, &msg))
977 DispatchMessage(&msg);
981 EnableWindow(fe->hwnd, TRUE);
982 SetForegroundWindow(fe->hwnd);
983 DestroyWindow(fe->cfgbox);
984 DeleteObject(fe->cfgfont);
987 static int get_config(frontend *fe, int which)
1000 int winwidth, winheight, col1l, col1r, col2l, col2r, y;
1001 int height, width, maxlabel, maxcheckbox;
1003 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
1004 wc.lpfnWndProc = DefDlgProc;
1006 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
1007 wc.hInstance = fe->inst;
1009 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
1010 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
1011 wc.lpszMenuName = NULL;
1012 wc.lpszClassName = "GameConfigBox";
1015 hdc = GetDC(fe->hwnd);
1016 SetMapMode(hdc, MM_TEXT);
1018 fe->dlg_done = FALSE;
1020 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
1022 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
1023 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
1028 oldfont = SelectObject(hdc, fe->cfgfont);
1029 if (GetTextMetrics(hdc, &tm)) {
1030 height = tm.tmAscent + tm.tmDescent;
1031 width = tm.tmAveCharWidth;
1033 height = width = 30;
1036 fe->cfg = midend_get_config(fe->me, which, &title);
1037 fe->cfg_which = which;
1040 * Figure out the layout of the config box by measuring the
1041 * length of each piece of text.
1043 maxlabel = maxcheckbox = 0;
1044 winheight = height/2;
1046 for (i = fe->cfg; i->type != C_END; i++) {
1051 * Both these control types have a label filling only
1052 * the left-hand column of the box.
1054 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
1057 winheight += height * 3 / 2 + (height / 2);
1062 * Checkboxes take up the whole of the box width.
1064 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
1065 maxcheckbox < size.cx)
1066 maxcheckbox = size.cx;
1067 winheight += height + (height / 2);
1072 winheight += height + height * 7 / 4; /* OK / Cancel buttons */
1075 col1r = col1l + maxlabel;
1076 col2l = col1r + 2*width;
1077 col2r = col2l + 30*width;
1078 if (col2r < col1l+2*height+maxcheckbox)
1079 col2r = col1l+2*height+maxcheckbox;
1080 winwidth = col2r + 2*width;
1082 SelectObject(hdc, oldfont);
1083 ReleaseDC(fe->hwnd, hdc);
1086 * Create the dialog, now that we know its size.
1093 r.bottom = winheight;
1095 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
1096 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1097 WS_CAPTION | WS_SYSMENU*/) &~
1098 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
1102 * Centre the dialog on its parent window.
1106 GetWindowRect(fe->hwnd, &r2);
1107 r.left = (r2.left + r2.right - r.right) / 2;
1108 r.top = (r2.top + r2.bottom - r.bottom) / 2;
1112 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title,
1113 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1114 WS_CAPTION | WS_SYSMENU,
1116 r.right-r.left, r.bottom-r.top,
1117 fe->hwnd, NULL, fe->inst, NULL);
1121 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
1123 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
1124 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc);
1127 * Count the controls so we can allocate cfgaux.
1129 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
1131 fe->cfgaux = snewn(nctrls, struct cfg_aux);
1135 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
1139 * Edit box with a label beside it.
1141 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
1142 "Static", 0, 0, i->name, id++);
1143 ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2,
1144 "EDIT", WS_TABSTOP | ES_AUTOHSCROLL,
1145 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
1146 SetWindowText(ctl, i->sval);
1154 mkctrl(fe, col1l, col2r, y, y+height, "BUTTON",
1155 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
1156 0, i->name, (j->ctlid = id++));
1157 CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0));
1163 * Drop-down list with a label beside it.
1165 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
1166 "STATIC", 0, 0, i->name, id++);
1167 ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2,
1168 "COMBOBOX", WS_TABSTOP |
1169 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
1170 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
1172 char c, *p, *q, *str;
1174 SendMessage(ctl, CB_RESETCONTENT, 0, 0);
1179 while (*q && *q != c) q++;
1180 str = snewn(q-p+1, char);
1181 strncpy(str, p, q-p);
1183 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str);
1190 SendMessage(ctl, CB_SETCURSEL, i->ival, 0);
1196 assert(y < winheight);
1200 y += height/2; /* extra space before OK and Cancel */
1201 mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON",
1202 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
1204 mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON",
1205 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL);
1207 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
1209 EnableWindow(fe->hwnd, FALSE);
1210 ShowWindow(fe->cfgbox, SW_NORMAL);
1211 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
1212 if (!IsDialogMessage(fe->cfgbox, &msg))
1213 DispatchMessage(&msg);
1217 EnableWindow(fe->hwnd, TRUE);
1218 SetForegroundWindow(fe->hwnd);
1219 DestroyWindow(fe->cfgbox);
1220 DeleteObject(fe->cfgfont);
1225 return (fe->dlg_done == 2);
1228 static void new_game_size(frontend *fe)
1235 midend_size(fe->me, &x, &y, FALSE);
1240 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
1241 (WS_THICKFRAME | WS_MAXIMIZEBOX |
1245 if (fe->statusbar != NULL) {
1246 GetWindowRect(fe->statusbar, &sr);
1248 sr.left = sr.right = sr.top = sr.bottom = 0;
1250 SetWindowPos(fe->hwnd, NULL, 0, 0,
1252 r.bottom - r.top + sr.bottom - sr.top,
1253 SWP_NOMOVE | SWP_NOZORDER);
1255 check_window_size(fe, &x, &y);
1257 if (fe->statusbar != NULL)
1258 SetWindowPos(fe->statusbar, NULL, 0, y, x,
1259 sr.bottom - sr.top, SWP_NOZORDER);
1261 DeleteObject(fe->bitmap);
1263 hdc = GetDC(fe->hwnd);
1264 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
1265 ReleaseDC(fe->hwnd, hdc);
1267 midend_redraw(fe->me);
1270 static void new_game_type(frontend *fe)
1272 midend_new_game(fe->me);
1276 static int is_alt_pressed(void)
1279 int r = GetKeyboardState(keystate);
1282 if (keystate[VK_MENU] & 0x80)
1284 if (keystate[VK_RMENU] & 0x80)
1289 static void savefile_write(void *wctx, void *buf, int len)
1291 FILE *fp = (FILE *)wctx;
1292 fwrite(buf, 1, len, fp);
1295 static int savefile_read(void *wctx, void *buf, int len)
1297 FILE *fp = (FILE *)wctx;
1300 ret = fread(buf, 1, len, fp);
1301 return (ret == len);
1304 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
1305 WPARAM wParam, LPARAM lParam)
1307 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1312 DestroyWindow(hwnd);
1315 cmd = wParam & ~0xF; /* low 4 bits reserved to Windows */
1318 if (!midend_process_key(fe->me, 0, 0, 'n'))
1322 midend_restart_game(fe->me);
1325 if (!midend_process_key(fe->me, 0, 0, 'u'))
1329 if (!midend_process_key(fe->me, 0, 0, '\x12'))
1334 char *text = midend_text_format(fe->me);
1336 write_clip(hwnd, text);
1338 MessageBeep(MB_ICONWARNING);
1344 char *msg = midend_solve(fe->me);
1346 MessageBox(hwnd, msg, "Unable to solve",
1347 MB_ICONERROR | MB_OK);
1351 if (!midend_process_key(fe->me, 0, 0, 'q'))
1355 if (get_config(fe, CFG_SETTINGS))
1359 if (get_config(fe, CFG_SEED))
1363 if (get_config(fe, CFG_DESC))
1373 char filename[FILENAME_MAX];
1376 memset(&of, 0, sizeof(of));
1377 of.hwndOwner = hwnd;
1378 of.lpstrFilter = "All Files (*.*)\0*\0\0\0";
1379 of.lpstrCustomFilter = NULL;
1380 of.nFilterIndex = 1;
1381 of.lpstrFile = filename;
1383 of.nMaxFile = lenof(filename);
1384 of.lpstrFileTitle = NULL;
1385 of.lpstrTitle = (cmd == IDM_SAVE ?
1386 "Enter name of game file to save" :
1387 "Enter name of saved game file to load");
1389 #ifdef OPENFILENAME_SIZE_VERSION_400
1390 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
1392 of.lStructSize = sizeof(of);
1394 of.lpstrInitialDir = NULL;
1396 if (cmd == IDM_SAVE)
1397 ret = GetSaveFileName(&of);
1399 ret = GetOpenFileName(&of);
1402 if (cmd == IDM_SAVE) {
1405 if ((fp = fopen(filename, "r")) != NULL) {
1406 char buf[256 + FILENAME_MAX];
1410 sprintf(buf, "Are you sure you want to overwrite"
1411 " the file \"%.*s\"?",
1412 FILENAME_MAX, filename);
1413 if (MessageBox(hwnd, buf, "Question",
1414 MB_YESNO | MB_ICONQUESTION)
1419 fp = fopen(filename, "w");
1422 MessageBox(hwnd, "Unable to open save file",
1423 "Error", MB_ICONERROR | MB_OK);
1427 midend_serialise(fe->me, savefile_write, fp);
1431 FILE *fp = fopen(filename, "r");
1435 MessageBox(hwnd, "Unable to open saved game file",
1436 "Error", MB_ICONERROR | MB_OK);
1440 err = midend_deserialise(fe->me, savefile_read, fp);
1445 MessageBox(hwnd, err, "Error", MB_ICONERROR|MB_OK);
1456 assert(fe->help_path);
1457 WinHelp(hwnd, fe->help_path,
1458 fe->help_has_contents ? HELP_FINDER : HELP_CONTENTS, 0);
1461 assert(fe->help_path);
1462 assert(thegame.winhelp_topic);
1464 char *cmd = snewn(10+strlen(thegame.winhelp_topic), char);
1465 sprintf(cmd, "JI(`',`%s')", thegame.winhelp_topic);
1466 WinHelp(hwnd, fe->help_path, HELP_COMMAND, (DWORD)cmd);
1472 int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10;
1474 if (p >= 0 && p < fe->npresets) {
1475 midend_set_params(fe->me, fe->presets[p]);
1491 hdc = BeginPaint(hwnd, &p);
1492 hdc2 = CreateCompatibleDC(hdc);
1493 prevbm = SelectObject(hdc2, fe->bitmap);
1495 p.rcPaint.left, p.rcPaint.top,
1496 p.rcPaint.right - p.rcPaint.left,
1497 p.rcPaint.bottom - p.rcPaint.top,
1499 p.rcPaint.left, p.rcPaint.top,
1501 SelectObject(hdc2, prevbm);
1510 int r = GetKeyboardState(keystate);
1511 int shift = (r && (keystate[VK_SHIFT] & 0x80)) ? MOD_SHFT : 0;
1512 int ctrl = (r && (keystate[VK_CONTROL] & 0x80)) ? MOD_CTRL : 0;
1516 if (!(lParam & 0x01000000))
1517 key = MOD_NUM_KEYPAD | '4';
1519 key = shift | ctrl | CURSOR_LEFT;
1522 if (!(lParam & 0x01000000))
1523 key = MOD_NUM_KEYPAD | '6';
1525 key = shift | ctrl | CURSOR_RIGHT;
1528 if (!(lParam & 0x01000000))
1529 key = MOD_NUM_KEYPAD | '8';
1531 key = shift | ctrl | CURSOR_UP;
1534 if (!(lParam & 0x01000000))
1535 key = MOD_NUM_KEYPAD | '2';
1537 key = shift | ctrl | CURSOR_DOWN;
1540 * Diagonal keys on the numeric keypad.
1543 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '9';
1546 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '3';
1549 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '7';
1552 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '1';
1555 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '0';
1558 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '5';
1561 * Numeric keypad keys with Num Lock on.
1563 case VK_NUMPAD4: key = MOD_NUM_KEYPAD | '4'; break;
1564 case VK_NUMPAD6: key = MOD_NUM_KEYPAD | '6'; break;
1565 case VK_NUMPAD8: key = MOD_NUM_KEYPAD | '8'; break;
1566 case VK_NUMPAD2: key = MOD_NUM_KEYPAD | '2'; break;
1567 case VK_NUMPAD5: key = MOD_NUM_KEYPAD | '5'; break;
1568 case VK_NUMPAD9: key = MOD_NUM_KEYPAD | '9'; break;
1569 case VK_NUMPAD3: key = MOD_NUM_KEYPAD | '3'; break;
1570 case VK_NUMPAD7: key = MOD_NUM_KEYPAD | '7'; break;
1571 case VK_NUMPAD1: key = MOD_NUM_KEYPAD | '1'; break;
1572 case VK_NUMPAD0: key = MOD_NUM_KEYPAD | '0'; break;
1576 if (!midend_process_key(fe->me, 0, 0, key))
1581 m.message = WM_KEYDOWN;
1583 m.lParam = lParam & 0xdfff;
1584 TranslateMessage(&m);
1588 case WM_LBUTTONDOWN:
1589 case WM_RBUTTONDOWN:
1590 case WM_MBUTTONDOWN:
1595 * Shift-clicks count as middle-clicks, since otherwise
1596 * two-button Windows users won't have any kind of
1597 * middle click to use.
1599 if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT))
1600 button = MIDDLE_BUTTON;
1601 else if (message == WM_RBUTTONDOWN || is_alt_pressed())
1602 button = RIGHT_BUTTON;
1604 button = LEFT_BUTTON;
1606 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1607 (signed short)HIWORD(lParam), button))
1620 * Shift-clicks count as middle-clicks, since otherwise
1621 * two-button Windows users won't have any kind of
1622 * middle click to use.
1624 if (message == WM_MBUTTONUP || (wParam & MK_SHIFT))
1625 button = MIDDLE_RELEASE;
1626 else if (message == WM_RBUTTONUP || is_alt_pressed())
1627 button = RIGHT_RELEASE;
1629 button = LEFT_RELEASE;
1631 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1632 (signed short)HIWORD(lParam), button))
1642 if (wParam & (MK_MBUTTON | MK_SHIFT))
1643 button = MIDDLE_DRAG;
1644 else if (wParam & MK_RBUTTON || is_alt_pressed())
1645 button = RIGHT_DRAG;
1649 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1650 (signed short)HIWORD(lParam), button))
1655 if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam))
1660 DWORD now = GetTickCount();
1661 float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F;
1662 midend_timer(fe->me, elapsed);
1663 fe->timer_last_tickcount = now;
1668 return DefWindowProc(hwnd, message, wParam, lParam);
1671 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
1676 InitCommonControls();
1682 wndclass.lpfnWndProc = WndProc;
1683 wndclass.cbClsExtra = 0;
1684 wndclass.cbWndExtra = 0;
1685 wndclass.hInstance = inst;
1686 wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION);
1687 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1688 wndclass.hbrBackground = NULL;
1689 wndclass.lpszMenuName = NULL;
1690 wndclass.lpszClassName = thegame.name;
1692 RegisterClass(&wndclass);
1695 while (*cmdline && isspace((unsigned char)*cmdline))
1698 if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) {
1700 sprintf(buf, "%.100s Error", thegame.name);
1701 MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
1705 while (GetMessage(&msg, NULL, 0, 0)) {
1706 DispatchMessage(&msg);