2 * windows.c: Windows front end for my puzzle collection.
6 * - Figure out what to do if a puzzle requests a size bigger than
7 * the screen will take. In principle we could put scrollbars in
8 * the window, although that would be pretty horrid. Another
9 * option is to detect in advance that this will be a problem -
10 * we can probably tell this using midend_size() before actually
11 * generating the puzzle - and simply refuse to do it.
26 #define IDM_NEW 0x0010
27 #define IDM_RESTART 0x0020
28 #define IDM_UNDO 0x0030
29 #define IDM_REDO 0x0040
30 #define IDM_COPY 0x0050
31 #define IDM_QUIT 0x0060
32 #define IDM_CONFIG 0x0070
33 #define IDM_SEED 0x0080
34 #define IDM_HELPC 0x0090
35 #define IDM_GAMEHELP 0x00A0
36 #define IDM_PRESETS 0x0100
38 #define HELP_FILE_NAME "puzzles.hlp"
39 #define HELP_CNT_NAME "puzzles.cnt"
42 static FILE *debug_fp = NULL;
43 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
44 static int debug_got_console = 0;
50 if (!debug_got_console) {
52 debug_got_console = 1;
53 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
57 debug_fp = fopen("debug.log", "w");
60 if (debug_hdl != INVALID_HANDLE_VALUE) {
61 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
67 void debug_printf(char *fmt, ...)
73 vsprintf(buf, fmt, ap);
78 #define debug(x) (debug_printf x)
98 HWND hwnd, statusbar, cfgbox;
100 HBITMAP bitmap, prevbm;
107 DWORD timer_last_tickcount;
109 game_params **presets;
111 int nfonts, fontsize;
113 struct cfg_aux *cfgaux;
114 int cfg_which, cfg_done;
117 int help_has_contents;
120 void fatal(char *fmt, ...)
126 vsprintf(buf, fmt, ap);
129 MessageBox(NULL, buf, "Fatal error", MB_ICONEXCLAMATION | MB_OK);
134 void get_random_seed(void **randseed, int *randseedsize)
136 time_t *tp = snew(time_t);
138 *randseed = (void *)tp;
139 *randseedsize = sizeof(time_t);
142 void status_bar(frontend *fe, char *text)
144 SetWindowText(fe->statusbar, text);
147 void frontend_default_colour(frontend *fe, float *output)
149 DWORD c = GetSysColor(COLOR_MENU); /* ick */
151 output[0] = (float)(GetRValue(c) / 255.0);
152 output[1] = (float)(GetGValue(c) / 255.0);
153 output[2] = (float)(GetBValue(c) / 255.0);
156 void clip(frontend *fe, int x, int y, int w, int h)
158 IntersectClipRect(fe->hdc_bm, x, y, x+w, y+h);
161 void unclip(frontend *fe)
163 SelectClipRgn(fe->hdc_bm, NULL);
166 void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize,
167 int align, int colour, char *text)
172 * Find or create the font.
174 for (i = 0; i < fe->nfonts; i++)
175 if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
178 if (i == fe->nfonts) {
179 if (fe->fontsize <= fe->nfonts) {
180 fe->fontsize = fe->nfonts + 10;
181 fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
186 fe->fonts[i].type = fonttype;
187 fe->fonts[i].size = fontsize;
189 fe->fonts[i].font = CreateFont(-fontsize, 0, 0, 0, FW_BOLD,
190 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
191 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
193 (fonttype == FONT_FIXED ?
194 FIXED_PITCH | FF_DONTCARE :
195 VARIABLE_PITCH | FF_SWISS),
200 * Position and draw the text.
207 oldfont = SelectObject(fe->hdc_bm, fe->fonts[i].font);
208 if (GetTextMetrics(fe->hdc_bm, &tm)) {
209 if (align & ALIGN_VCENTRE)
210 y -= (tm.tmAscent+tm.tmDescent)/2;
214 if (GetTextExtentPoint32(fe->hdc_bm, text, strlen(text), &size)) {
215 if (align & ALIGN_HCENTRE)
217 else if (align & ALIGN_HRIGHT)
220 SetBkMode(fe->hdc_bm, TRANSPARENT);
221 SetTextColor(fe->hdc_bm, fe->colours[colour]);
222 TextOut(fe->hdc_bm, x, y, text, strlen(text));
223 SelectObject(fe->hdc_bm, oldfont);
227 void draw_rect(frontend *fe, int x, int y, int w, int h, int colour)
229 if (w == 1 && h == 1) {
231 * Rectangle() appears to get uppity if asked to draw a 1x1
232 * rectangle, presumably on the grounds that that's beneath
233 * its dignity and you ought to be using SetPixel instead.
236 SetPixel(fe->hdc_bm, x, y, fe->colours[colour]);
238 HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[colour]);
239 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
240 Rectangle(fe->hdc_bm, x, y, x+w, y+h);
241 SelectObject(fe->hdc_bm, oldbrush);
242 SelectObject(fe->hdc_bm, oldpen);
246 void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour)
248 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
249 MoveToEx(fe->hdc_bm, x1, y1, NULL);
250 LineTo(fe->hdc_bm, x2, y2);
251 SetPixel(fe->hdc_bm, x2, y2, fe->colours[colour]);
252 SelectObject(fe->hdc_bm, oldpen);
255 void draw_polygon(frontend *fe, int *coords, int npoints,
256 int fill, int colour)
258 POINT *pts = snewn(npoints+1, POINT);
261 for (i = 0; i <= npoints; i++) {
262 int j = (i < npoints ? i : 0);
263 pts[i].x = coords[j*2];
264 pts[i].y = coords[j*2+1];
268 HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[colour]);
269 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
270 Polygon(fe->hdc_bm, pts, npoints);
271 SelectObject(fe->hdc_bm, oldbrush);
272 SelectObject(fe->hdc_bm, oldpen);
274 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
275 Polyline(fe->hdc_bm, pts, npoints+1);
276 SelectObject(fe->hdc_bm, oldpen);
282 void start_draw(frontend *fe)
285 hdc_win = GetDC(fe->hwnd);
286 fe->hdc_bm = CreateCompatibleDC(hdc_win);
287 fe->prevbm = SelectObject(fe->hdc_bm, fe->bitmap);
288 ReleaseDC(fe->hwnd, hdc_win);
290 SetMapMode(fe->hdc_bm, MM_TEXT);
293 void draw_update(frontend *fe, int x, int y, int w, int h)
302 InvalidateRect(fe->hwnd, &r, FALSE);
305 void end_draw(frontend *fe)
307 SelectObject(fe->hdc_bm, fe->prevbm);
308 DeleteDC(fe->hdc_bm);
310 DeleteObject(fe->clip);
315 void deactivate_timer(frontend *fe)
317 KillTimer(fe->hwnd, fe->timer);
321 void activate_timer(frontend *fe)
324 fe->timer = SetTimer(fe->hwnd, fe->timer, 20, NULL);
325 fe->timer_last_tickcount = GetTickCount();
329 void write_clip(HWND hwnd, char *data)
332 int len = strlen(data);
335 clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
338 lock = GlobalLock(clipdata);
341 memcpy(lock, data, len);
342 ((unsigned char *) lock)[len] = 0;
343 GlobalUnlock(clipdata);
345 if (OpenClipboard(hwnd)) {
347 SetClipboardData(CF_TEXT, clipdata);
350 GlobalFree(clipdata);
354 * See if we can find a help file.
356 static void find_help_file(frontend *fe)
358 char b[2048], *p, *q, *r;
360 if (!fe->help_path) {
361 GetModuleFileName(NULL, b, sizeof(b) - 1);
363 p = strrchr(b, '\\');
364 if (p && p >= r) r = p+1;
366 if (q && q >= r) r = q+1;
367 strcpy(r, HELP_FILE_NAME);
368 if ( (fp = fopen(b, "r")) != NULL) {
369 fe->help_path = dupstr(b);
372 fe->help_path = NULL;
373 strcpy(r, HELP_CNT_NAME);
374 if ( (fp = fopen(b, "r")) != NULL) {
375 fe->help_has_contents = TRUE;
378 fe->help_has_contents = FALSE;
382 static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
391 fe->me = midend_new(fe, &thegame);
394 *error = midend_game_id(fe->me, game_id, FALSE);
402 fe->help_path = NULL;
406 midend_new_game(fe->me);
407 midend_size(fe->me, &x, &y);
412 fe->nfonts = fe->fontsize = 0;
418 colours = midend_colours(fe->me, &ncolours);
420 fe->colours = snewn(ncolours, COLORREF);
421 fe->brushes = snewn(ncolours, HBRUSH);
422 fe->pens = snewn(ncolours, HPEN);
424 for (i = 0; i < ncolours; i++) {
425 fe->colours[i] = RGB(255 * colours[i*3+0],
426 255 * colours[i*3+1],
427 255 * colours[i*3+2]);
428 fe->brushes[i] = CreateSolidBrush(fe->colours[i]);
429 fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]);
436 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
437 (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED),
440 fe->hwnd = CreateWindowEx(0, thegame.name, thegame.name,
441 WS_OVERLAPPEDWINDOW &~
442 (WS_THICKFRAME | WS_MAXIMIZEBOX),
443 CW_USEDEFAULT, CW_USEDEFAULT,
444 r.right - r.left, r.bottom - r.top,
445 NULL, NULL, inst, NULL);
448 HMENU bar = CreateMenu();
449 HMENU menu = CreateMenu();
451 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Game");
452 AppendMenu(menu, MF_ENABLED, IDM_NEW, "New");
453 AppendMenu(menu, MF_ENABLED, IDM_RESTART, "Restart");
454 AppendMenu(menu, MF_ENABLED, IDM_SEED, "Specific...");
456 if ((fe->npresets = midend_num_presets(fe->me)) > 0 ||
457 thegame.can_configure) {
458 HMENU sub = CreateMenu();
461 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "Type");
463 fe->presets = snewn(fe->npresets, game_params *);
465 for (i = 0; i < fe->npresets; i++) {
468 midend_fetch_preset(fe->me, i, &name, &fe->presets[i]);
471 * FIXME: we ought to go through and do something
472 * with ampersands here.
475 AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name);
478 if (thegame.can_configure) {
479 AppendMenu(sub, MF_ENABLED, IDM_CONFIG, "Custom...");
483 AppendMenu(menu, MF_SEPARATOR, 0, 0);
484 AppendMenu(menu, MF_ENABLED, IDM_UNDO, "Undo");
485 AppendMenu(menu, MF_ENABLED, IDM_REDO, "Redo");
486 if (thegame.can_format_as_text) {
487 AppendMenu(menu, MF_SEPARATOR, 0, 0);
488 AppendMenu(menu, MF_ENABLED, IDM_COPY, "Copy");
490 AppendMenu(menu, MF_SEPARATOR, 0, 0);
491 AppendMenu(menu, MF_ENABLED, IDM_QUIT, "Exit");
493 HMENU hmenu = CreateMenu();
494 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)hmenu, "Help");
495 AppendMenu(hmenu, MF_ENABLED, IDM_HELPC, "Contents");
496 if (thegame.winhelp_topic) {
498 assert(thegame.name);
499 item = snewn(9+strlen(thegame.name), char); /*ick*/
500 sprintf(item, "Help on %s", thegame.name);
501 AppendMenu(hmenu, MF_ENABLED, IDM_GAMEHELP, item);
505 SetMenu(fe->hwnd, bar);
508 if (midend_wants_statusbar(fe->me)) {
509 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh",
510 WS_CHILD | WS_VISIBLE,
511 0, 0, 0, 0, /* status bar does these */
512 fe->hwnd, NULL, inst, NULL);
513 GetWindowRect(fe->statusbar, &sr);
514 SetWindowPos(fe->hwnd, NULL, 0, 0,
515 r.right - r.left, r.bottom - r.top + sr.bottom - sr.top,
516 SWP_NOMOVE | SWP_NOZORDER);
517 SetWindowPos(fe->statusbar, NULL, 0, y, x, sr.bottom - sr.top,
520 fe->statusbar = NULL;
523 hdc = GetDC(fe->hwnd);
524 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
525 ReleaseDC(fe->hwnd, hdc);
527 SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe);
529 ShowWindow(fe->hwnd, SW_NORMAL);
530 SetForegroundWindow(fe->hwnd);
532 midend_redraw(fe->me);
537 static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg,
538 WPARAM wParam, LPARAM lParam)
540 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
550 * OK and Cancel are special cases.
552 if ((HIWORD(wParam) == BN_CLICKED ||
553 HIWORD(wParam) == BN_DOUBLECLICKED) &&
554 (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) {
555 if (LOWORD(wParam) == IDOK) {
556 char *err = midend_set_config(fe->me, fe->cfg_which, fe->cfg);
559 MessageBox(hwnd, err, "Validation error",
560 MB_ICONERROR | MB_OK);
571 * First find the control whose id this is.
573 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
574 if (j->ctlid == LOWORD(wParam))
577 if (i->type == C_END)
578 return 0; /* not our problem */
580 if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) {
582 GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer));
583 buffer[lenof(buffer)-1] = '\0';
585 i->sval = dupstr(buffer);
586 } else if (i->type == C_BOOLEAN &&
587 (HIWORD(wParam) == BN_CLICKED ||
588 HIWORD(wParam) == BN_DOUBLECLICKED)) {
589 i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid);
590 } else if (i->type == C_CHOICES &&
591 HIWORD(wParam) == CBN_SELCHANGE) {
592 i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid,
606 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
607 char *wclass, int wstyle,
608 int exstyle, char *wtext, int wid)
611 ret = CreateWindowEx(exstyle, wclass, wtext,
612 wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1,
613 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
614 SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0));
618 static int get_config(frontend *fe, int which)
631 int winwidth, winheight, col1l, col1r, col2l, col2r, y;
632 int height, width, maxlabel, maxcheckbox;
634 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
635 wc.lpfnWndProc = DefDlgProc;
637 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
638 wc.hInstance = fe->inst;
640 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
641 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
642 wc.lpszMenuName = NULL;
643 wc.lpszClassName = "GameConfigBox";
646 hdc = GetDC(fe->hwnd);
647 SetMapMode(hdc, MM_TEXT);
649 fe->cfg_done = FALSE;
651 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
653 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
654 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
659 oldfont = SelectObject(hdc, fe->cfgfont);
660 if (GetTextMetrics(hdc, &tm)) {
661 height = tm.tmAscent + tm.tmDescent;
662 width = tm.tmAveCharWidth;
667 fe->cfg = midend_get_config(fe->me, which, &title);
668 fe->cfg_which = which;
671 * Figure out the layout of the config box by measuring the
672 * length of each piece of text.
674 maxlabel = maxcheckbox = 0;
675 winheight = height/2;
677 for (i = fe->cfg; i->type != C_END; i++) {
682 * Both these control types have a label filling only
683 * the left-hand column of the box.
685 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
688 winheight += height * 3 / 2 + (height / 2);
693 * Checkboxes take up the whole of the box width.
695 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
696 maxcheckbox < size.cx)
697 maxcheckbox = size.cx;
698 winheight += height + (height / 2);
703 winheight += height + height * 7 / 4; /* OK / Cancel buttons */
706 col1r = col1l + maxlabel;
707 col2l = col1r + 2*width;
708 col2r = col2l + 30*width;
709 if (col2r < col1l+2*height+maxcheckbox)
710 col2r = col1l+2*height+maxcheckbox;
711 winwidth = col2r + 2*width;
713 ReleaseDC(fe->hwnd, hdc);
716 * Create the dialog, now that we know its size.
723 r.bottom = winheight;
725 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
726 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
727 WS_CAPTION | WS_SYSMENU*/) &~
728 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
732 * Centre the dialog on its parent window.
736 GetWindowRect(fe->hwnd, &r2);
737 r.left = (r2.left + r2.right - r.right) / 2;
738 r.top = (r2.top + r2.bottom - r.bottom) / 2;
742 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title,
743 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
744 WS_CAPTION | WS_SYSMENU,
746 r.right-r.left, r.bottom-r.top,
747 fe->hwnd, NULL, fe->inst, NULL);
751 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
753 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
754 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc);
757 * Count the controls so we can allocate cfgaux.
759 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
761 fe->cfgaux = snewn(nctrls, struct cfg_aux);
765 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
769 * Edit box with a label beside it.
771 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
772 "Static", 0, 0, i->name, id++);
773 ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2,
774 "EDIT", WS_TABSTOP | ES_AUTOHSCROLL,
775 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
776 SetWindowText(ctl, i->sval);
784 mkctrl(fe, col1l, col2r, y, y+height, "BUTTON",
785 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
786 0, i->name, (j->ctlid = id++));
787 CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0));
793 * Drop-down list with a label beside it.
795 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
796 "STATIC", 0, 0, i->name, id++);
797 ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2,
798 "COMBOBOX", WS_TABSTOP |
799 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
800 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
802 char c, *p, *q, *str;
804 SendMessage(ctl, CB_RESETCONTENT, 0, 0);
809 while (*q && *q != c) q++;
810 str = snewn(q-p+1, char);
811 strncpy(str, p, q-p);
813 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str);
820 SendMessage(ctl, CB_SETCURSEL, i->ival, 0);
826 assert(y < winheight);
830 y += height/2; /* extra space before OK and Cancel */
831 mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON",
832 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
834 mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON",
835 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL);
837 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
839 EnableWindow(fe->hwnd, FALSE);
840 ShowWindow(fe->cfgbox, SW_NORMAL);
841 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
842 if (!IsDialogMessage(fe->cfgbox, &msg))
843 DispatchMessage(&msg);
847 EnableWindow(fe->hwnd, TRUE);
848 SetForegroundWindow(fe->hwnd);
849 DestroyWindow(fe->cfgbox);
850 DeleteObject(fe->cfgfont);
855 return (fe->cfg_done == 2);
858 static void new_game_type(frontend *fe)
864 midend_new_game(fe->me);
865 midend_size(fe->me, &x, &y);
870 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
871 (WS_THICKFRAME | WS_MAXIMIZEBOX |
875 if (fe->statusbar != NULL) {
876 GetWindowRect(fe->statusbar, &sr);
878 sr.left = sr.right = sr.top = sr.bottom = 0;
880 SetWindowPos(fe->hwnd, NULL, 0, 0,
882 r.bottom - r.top + sr.bottom - sr.top,
883 SWP_NOMOVE | SWP_NOZORDER);
884 if (fe->statusbar != NULL)
885 SetWindowPos(fe->statusbar, NULL, 0, y, x,
886 sr.bottom - sr.top, SWP_NOZORDER);
888 DeleteObject(fe->bitmap);
890 hdc = GetDC(fe->hwnd);
891 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
892 ReleaseDC(fe->hwnd, hdc);
894 midend_redraw(fe->me);
897 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
898 WPARAM wParam, LPARAM lParam)
900 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
907 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
909 if (!midend_process_key(fe->me, 0, 0, 'n'))
913 if (!midend_process_key(fe->me, 0, 0, 'r'))
917 if (!midend_process_key(fe->me, 0, 0, 'u'))
921 if (!midend_process_key(fe->me, 0, 0, '\x12'))
926 char *text = midend_text_format(fe->me);
928 write_clip(hwnd, text);
930 MessageBeep(MB_ICONWARNING);
934 if (!midend_process_key(fe->me, 0, 0, 'q'))
938 if (get_config(fe, CFG_SETTINGS))
942 if (get_config(fe, CFG_SEED))
946 assert(fe->help_path);
947 WinHelp(hwnd, fe->help_path,
948 fe->help_has_contents ? HELP_FINDER : HELP_CONTENTS, 0);
951 assert(fe->help_path);
952 assert(thegame.winhelp_topic);
954 char *cmd = snewn(10+strlen(thegame.winhelp_topic), char);
955 sprintf(cmd, "JI(`',`%s')", thegame.winhelp_topic);
956 WinHelp(hwnd, fe->help_path, HELP_COMMAND, (DWORD)cmd);
962 int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10;
964 if (p >= 0 && p < fe->npresets) {
965 midend_set_params(fe->me, fe->presets[p]);
981 hdc = BeginPaint(hwnd, &p);
982 hdc2 = CreateCompatibleDC(hdc);
983 prevbm = SelectObject(hdc2, fe->bitmap);
985 p.rcPaint.left, p.rcPaint.top,
986 p.rcPaint.right - p.rcPaint.left,
987 p.rcPaint.bottom - p.rcPaint.top,
989 p.rcPaint.left, p.rcPaint.top,
991 SelectObject(hdc2, prevbm);
1001 case VK_LEFT: key = CURSOR_LEFT; break;
1002 case VK_RIGHT: key = CURSOR_RIGHT; break;
1003 case VK_UP: key = CURSOR_UP; break;
1004 case VK_DOWN: key = CURSOR_DOWN; break;
1006 * Diagonal keys on the numeric keypad.
1009 if (!(lParam & 0x01000000)) key = CURSOR_UP_RIGHT;
1012 if (!(lParam & 0x01000000)) key = CURSOR_DOWN_RIGHT;
1015 if (!(lParam & 0x01000000)) key = CURSOR_UP_LEFT;
1018 if (!(lParam & 0x01000000)) key = CURSOR_DOWN_LEFT;
1021 * Numeric keypad keys with Num Lock on.
1023 case VK_NUMPAD4: key = CURSOR_LEFT; break;
1024 case VK_NUMPAD6: key = CURSOR_RIGHT; break;
1025 case VK_NUMPAD8: key = CURSOR_UP; break;
1026 case VK_NUMPAD2: key = CURSOR_DOWN; break;
1027 case VK_NUMPAD9: key = CURSOR_UP_RIGHT; break;
1028 case VK_NUMPAD3: key = CURSOR_DOWN_RIGHT; break;
1029 case VK_NUMPAD7: key = CURSOR_UP_LEFT; break;
1030 case VK_NUMPAD1: key = CURSOR_DOWN_LEFT; break;
1034 if (!midend_process_key(fe->me, 0, 0, key))
1039 m.message = WM_KEYDOWN;
1041 m.lParam = lParam & 0xdfff;
1042 TranslateMessage(&m);
1046 case WM_LBUTTONDOWN:
1047 case WM_RBUTTONDOWN:
1048 case WM_MBUTTONDOWN:
1053 * Shift-clicks count as middle-clicks, since otherwise
1054 * two-button Windows users won't have any kind of
1055 * middle click to use.
1057 if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT))
1058 button = MIDDLE_BUTTON;
1059 else if (message == WM_LBUTTONDOWN)
1060 button = LEFT_BUTTON;
1062 button = RIGHT_BUTTON;
1064 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1065 (signed short)HIWORD(lParam), button))
1078 * Shift-clicks count as middle-clicks, since otherwise
1079 * two-button Windows users won't have any kind of
1080 * middle click to use.
1082 if (message == WM_MBUTTONUP || (wParam & MK_SHIFT))
1083 button = MIDDLE_RELEASE;
1084 else if (message == WM_LBUTTONUP)
1085 button = LEFT_RELEASE;
1087 button = RIGHT_RELEASE;
1089 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1090 (signed short)HIWORD(lParam), button))
1100 if (wParam & (MK_MBUTTON | MK_SHIFT))
1101 button = MIDDLE_DRAG;
1102 else if (wParam & MK_LBUTTON)
1105 button = RIGHT_DRAG;
1107 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1108 (signed short)HIWORD(lParam), button))
1113 if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam))
1118 DWORD now = GetTickCount();
1119 float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F;
1120 midend_timer(fe->me, elapsed);
1121 fe->timer_last_tickcount = now;
1126 return DefWindowProc(hwnd, message, wParam, lParam);
1129 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
1134 InitCommonControls();
1140 wndclass.lpfnWndProc = WndProc;
1141 wndclass.cbClsExtra = 0;
1142 wndclass.cbWndExtra = 0;
1143 wndclass.hInstance = inst;
1144 wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION);
1145 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1146 wndclass.hbrBackground = NULL;
1147 wndclass.lpszMenuName = NULL;
1148 wndclass.lpszClassName = thegame.name;
1150 RegisterClass(&wndclass);
1153 while (*cmdline && isspace(*cmdline))
1156 if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) {
1158 sprintf(buf, "%.100s Error", thegame.name);
1159 MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
1163 while (GetMessage(&msg, NULL, 0, 0)) {
1164 DispatchMessage(&msg);