2 * windows.c: Windows front end for my puzzle collection.
17 #define IDM_NEW 0x0010
18 #define IDM_RESTART 0x0020
19 #define IDM_UNDO 0x0030
20 #define IDM_REDO 0x0040
21 #define IDM_QUIT 0x0050
22 #define IDM_CONFIG 0x0060
23 #define IDM_SEED 0x0070
24 #define IDM_HELPC 0x0080
25 #define IDM_GAMEHELP 0x0090
26 #define IDM_PRESETS 0x0100
28 #define HELP_FILE_NAME "puzzles.hlp"
29 #define HELP_CNT_NAME "puzzles.cnt"
32 static FILE *debug_fp = NULL;
33 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
34 static int debug_got_console = 0;
40 if (!debug_got_console) {
42 debug_got_console = 1;
43 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
47 debug_fp = fopen("debug.log", "w");
50 if (debug_hdl != INVALID_HANDLE_VALUE) {
51 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
57 void debug_printf(char *fmt, ...)
63 vsprintf(buf, fmt, ap);
68 #define debug(x) (debug_printf x)
88 HWND hwnd, statusbar, cfgbox;
90 HBITMAP bitmap, prevbm;
97 DWORD timer_last_tickcount;
99 game_params **presets;
101 int nfonts, fontsize;
103 struct cfg_aux *cfgaux;
104 int cfg_which, cfg_done;
107 int help_has_contents;
110 void fatal(char *fmt, ...)
116 vsprintf(buf, fmt, ap);
119 MessageBox(NULL, buf, "Fatal error", MB_ICONEXCLAMATION | MB_OK);
124 void get_random_seed(void **randseed, int *randseedsize)
126 time_t *tp = snew(time_t);
128 *randseed = (void *)tp;
129 *randseedsize = sizeof(time_t);
132 void status_bar(frontend *fe, char *text)
134 SetWindowText(fe->statusbar, text);
137 void frontend_default_colour(frontend *fe, float *output)
139 DWORD c = GetSysColor(COLOR_MENU); /* ick */
141 output[0] = (float)(GetRValue(c) / 255.0);
142 output[1] = (float)(GetGValue(c) / 255.0);
143 output[2] = (float)(GetBValue(c) / 255.0);
146 void clip(frontend *fe, int x, int y, int w, int h)
149 fe->clip = CreateRectRgn(0, 0, 1, 1);
150 GetClipRgn(fe->hdc_bm, fe->clip);
153 IntersectClipRect(fe->hdc_bm, x, y, x+w, y+h);
156 void unclip(frontend *fe)
159 SelectClipRgn(fe->hdc_bm, fe->clip);
162 void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize,
163 int align, int colour, char *text)
168 * Find or create the font.
170 for (i = 0; i < fe->nfonts; i++)
171 if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
174 if (i == fe->nfonts) {
175 if (fe->fontsize <= fe->nfonts) {
176 fe->fontsize = fe->nfonts + 10;
177 fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
182 fe->fonts[i].type = fonttype;
183 fe->fonts[i].size = fontsize;
186 * FIXME: Really I should make at least _some_ effort to
187 * pick the correct font.
189 fe->fonts[i].font = CreateFont(-fontsize, 0, 0, 0, 0,
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 TextOut(fe->hdc_bm, x, y, text, strlen(text));
222 SelectObject(fe->hdc_bm, oldfont);
226 void draw_rect(frontend *fe, int x, int y, int w, int h, int colour)
228 if (w == 1 && h == 1) {
230 * Rectangle() appears to get uppity if asked to draw a 1x1
231 * rectangle, presumably on the grounds that that's beneath
232 * its dignity and you ought to be using SetPixel instead.
235 SetPixel(fe->hdc_bm, x, y, fe->colours[colour]);
237 HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[colour]);
238 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
239 Rectangle(fe->hdc_bm, x, y, x+w, y+h);
240 SelectObject(fe->hdc_bm, oldbrush);
241 SelectObject(fe->hdc_bm, oldpen);
245 void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour)
247 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
248 MoveToEx(fe->hdc_bm, x1, y1, NULL);
249 LineTo(fe->hdc_bm, x2, y2);
250 SetPixel(fe->hdc_bm, x2, y2, fe->colours[colour]);
251 SelectObject(fe->hdc_bm, oldpen);
254 void draw_polygon(frontend *fe, int *coords, int npoints,
255 int fill, int colour)
257 POINT *pts = snewn(npoints+1, POINT);
260 for (i = 0; i <= npoints; i++) {
261 int j = (i < npoints ? i : 0);
262 pts[i].x = coords[j*2];
263 pts[i].y = coords[j*2+1];
267 HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[colour]);
268 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
269 Polygon(fe->hdc_bm, pts, npoints);
270 SelectObject(fe->hdc_bm, oldbrush);
271 SelectObject(fe->hdc_bm, oldpen);
273 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
274 Polyline(fe->hdc_bm, pts, npoints+1);
275 SelectObject(fe->hdc_bm, oldpen);
281 void start_draw(frontend *fe)
284 hdc_win = GetDC(fe->hwnd);
285 fe->hdc_bm = CreateCompatibleDC(hdc_win);
286 fe->prevbm = SelectObject(fe->hdc_bm, fe->bitmap);
287 ReleaseDC(fe->hwnd, hdc_win);
289 SetMapMode(fe->hdc_bm, MM_TEXT);
292 void draw_update(frontend *fe, int x, int y, int w, int h)
301 InvalidateRect(fe->hwnd, &r, FALSE);
304 void end_draw(frontend *fe)
306 SelectObject(fe->hdc_bm, fe->prevbm);
307 DeleteDC(fe->hdc_bm);
309 DeleteObject(fe->clip);
314 void deactivate_timer(frontend *fe)
316 KillTimer(fe->hwnd, fe->timer);
320 void activate_timer(frontend *fe)
323 fe->timer = SetTimer(fe->hwnd, fe->timer, 20, NULL);
324 fe->timer_last_tickcount = GetTickCount();
329 * See if we can find a help file.
331 static void find_help_file(frontend *fe)
333 char b[2048], *p, *q, *r;
335 if (!fe->help_path) {
336 GetModuleFileName(NULL, b, sizeof(b) - 1);
338 p = strrchr(b, '\\');
339 if (p && p >= r) r = p+1;
341 if (q && q >= r) r = q+1;
342 strcpy(r, HELP_FILE_NAME);
343 if ( (fp = fopen(b, "r")) != NULL) {
344 fe->help_path = dupstr(b);
347 fe->help_path = NULL;
348 strcpy(r, HELP_CNT_NAME);
349 if ( (fp = fopen(b, "r")) != NULL) {
350 fe->help_has_contents = TRUE;
353 fe->help_has_contents = FALSE;
357 static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
366 fe->me = midend_new(fe);
369 *error = midend_game_id(fe->me, game_id, FALSE);
377 fe->help_path = NULL;
381 midend_new_game(fe->me);
382 midend_size(fe->me, &x, &y);
387 fe->nfonts = fe->fontsize = 0;
393 colours = midend_colours(fe->me, &ncolours);
395 fe->colours = snewn(ncolours, COLORREF);
396 fe->brushes = snewn(ncolours, HBRUSH);
397 fe->pens = snewn(ncolours, HPEN);
399 for (i = 0; i < ncolours; i++) {
400 fe->colours[i] = RGB(255 * colours[i*3+0],
401 255 * colours[i*3+1],
402 255 * colours[i*3+2]);
403 fe->brushes[i] = CreateSolidBrush(fe->colours[i]);
404 fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]);
411 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
412 (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED),
415 fe->hwnd = CreateWindowEx(0, game_name, game_name,
416 WS_OVERLAPPEDWINDOW &~
417 (WS_THICKFRAME | WS_MAXIMIZEBOX),
418 CW_USEDEFAULT, CW_USEDEFAULT,
419 r.right - r.left, r.bottom - r.top,
420 NULL, NULL, inst, NULL);
423 HMENU bar = CreateMenu();
424 HMENU menu = CreateMenu();
426 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Game");
427 AppendMenu(menu, MF_ENABLED, IDM_NEW, "New");
428 AppendMenu(menu, MF_ENABLED, IDM_RESTART, "Restart");
429 AppendMenu(menu, MF_ENABLED, IDM_SEED, "Specific...");
431 if ((fe->npresets = midend_num_presets(fe->me)) > 0 ||
432 game_can_configure) {
433 HMENU sub = CreateMenu();
436 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "Type");
438 fe->presets = snewn(fe->npresets, game_params *);
440 for (i = 0; i < fe->npresets; i++) {
443 midend_fetch_preset(fe->me, i, &name, &fe->presets[i]);
446 * FIXME: we ought to go through and do something
447 * with ampersands here.
450 AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name);
453 if (game_can_configure) {
454 AppendMenu(sub, MF_ENABLED, IDM_CONFIG, "Custom...");
458 AppendMenu(menu, MF_SEPARATOR, 0, 0);
459 AppendMenu(menu, MF_ENABLED, IDM_UNDO, "Undo");
460 AppendMenu(menu, MF_ENABLED, IDM_REDO, "Redo");
461 AppendMenu(menu, MF_SEPARATOR, 0, 0);
462 AppendMenu(menu, MF_ENABLED, IDM_QUIT, "Exit");
464 HMENU hmenu = CreateMenu();
465 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)hmenu, "Help");
466 AppendMenu(hmenu, MF_ENABLED, IDM_HELPC, "Contents");
467 if (game_winhelp_topic) {
470 item = snewn(9+strlen(game_name), char); /*ick*/
471 sprintf(item, "Help on %s", game_name);
472 AppendMenu(hmenu, MF_ENABLED, IDM_GAMEHELP, item);
476 SetMenu(fe->hwnd, bar);
479 if (midend_wants_statusbar(fe->me)) {
480 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh",
481 WS_CHILD | WS_VISIBLE,
482 0, 0, 0, 0, /* status bar does these */
483 fe->hwnd, NULL, inst, NULL);
484 GetWindowRect(fe->statusbar, &sr);
485 SetWindowPos(fe->hwnd, NULL, 0, 0,
486 r.right - r.left, r.bottom - r.top + sr.bottom - sr.top,
487 SWP_NOMOVE | SWP_NOZORDER);
488 SetWindowPos(fe->statusbar, NULL, 0, y, x, sr.bottom - sr.top,
491 fe->statusbar = NULL;
494 hdc = GetDC(fe->hwnd);
495 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
496 ReleaseDC(fe->hwnd, hdc);
498 SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe);
500 ShowWindow(fe->hwnd, SW_NORMAL);
501 SetForegroundWindow(fe->hwnd);
503 midend_redraw(fe->me);
508 static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg,
509 WPARAM wParam, LPARAM lParam)
511 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
521 * OK and Cancel are special cases.
523 if ((HIWORD(wParam) == BN_CLICKED ||
524 HIWORD(wParam) == BN_DOUBLECLICKED) &&
525 (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) {
526 if (LOWORD(wParam) == IDOK) {
527 char *err = midend_set_config(fe->me, fe->cfg_which, fe->cfg);
530 MessageBox(hwnd, err, "Validation error",
531 MB_ICONERROR | MB_OK);
542 * First find the control whose id this is.
544 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
545 if (j->ctlid == LOWORD(wParam))
548 if (i->type == C_END)
549 return 0; /* not our problem */
551 if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) {
553 GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer));
554 buffer[lenof(buffer)-1] = '\0';
556 i->sval = dupstr(buffer);
557 } else if (i->type == C_BOOLEAN &&
558 (HIWORD(wParam) == BN_CLICKED ||
559 HIWORD(wParam) == BN_DOUBLECLICKED)) {
560 i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid);
561 } else if (i->type == C_CHOICES &&
562 HIWORD(wParam) == CBN_SELCHANGE) {
563 i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid,
577 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
578 char *wclass, int wstyle,
579 int exstyle, char *wtext, int wid)
582 ret = CreateWindowEx(exstyle, wclass, wtext,
583 wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1,
584 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
585 SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0));
589 static int get_config(frontend *fe, int which)
602 int winwidth, winheight, col1l, col1r, col2l, col2r, y;
603 int height, width, maxlabel, maxcheckbox;
605 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
606 wc.lpfnWndProc = DefDlgProc;
608 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
609 wc.hInstance = fe->inst;
611 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
612 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
613 wc.lpszMenuName = NULL;
614 wc.lpszClassName = "GameConfigBox";
617 hdc = GetDC(fe->hwnd);
618 SetMapMode(hdc, MM_TEXT);
620 fe->cfg_done = FALSE;
622 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
624 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
625 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
630 oldfont = SelectObject(hdc, fe->cfgfont);
631 if (GetTextMetrics(hdc, &tm)) {
632 height = tm.tmAscent + tm.tmDescent;
633 width = tm.tmAveCharWidth;
638 fe->cfg = midend_get_config(fe->me, which, &title);
639 fe->cfg_which = which;
642 * Figure out the layout of the config box by measuring the
643 * length of each piece of text.
645 maxlabel = maxcheckbox = 0;
646 winheight = height/2;
648 for (i = fe->cfg; i->type != C_END; i++) {
653 * Both these control types have a label filling only
654 * the left-hand column of the box.
656 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
659 winheight += height * 3 / 2 + (height / 2);
664 * Checkboxes take up the whole of the box width.
666 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
667 maxcheckbox < size.cx)
668 maxcheckbox = size.cx;
669 winheight += height + (height / 2);
674 winheight += height + height * 7 / 4; /* OK / Cancel buttons */
677 col1r = col1l + maxlabel;
678 col2l = col1r + 2*width;
679 col2r = col2l + 30*width;
680 if (col2r < col1l+2*height+maxcheckbox)
681 col2r = col1l+2*height+maxcheckbox;
682 winwidth = col2r + 2*width;
684 ReleaseDC(fe->hwnd, hdc);
687 * Create the dialog, now that we know its size.
694 r.bottom = winheight;
696 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
697 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
698 WS_CAPTION | WS_SYSMENU*/) &~
699 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
703 * Centre the dialog on its parent window.
707 GetWindowRect(fe->hwnd, &r2);
708 r.left = (r2.left + r2.right - r.right) / 2;
709 r.top = (r2.top + r2.bottom - r.bottom) / 2;
713 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title,
714 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
715 WS_CAPTION | WS_SYSMENU,
717 r.right-r.left, r.bottom-r.top,
718 fe->hwnd, NULL, fe->inst, NULL);
722 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
724 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
725 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc);
728 * Count the controls so we can allocate cfgaux.
730 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
732 fe->cfgaux = snewn(nctrls, struct cfg_aux);
736 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
740 * Edit box with a label beside it.
742 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
743 "Static", 0, 0, i->name, id++);
744 ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2,
745 "EDIT", WS_TABSTOP | ES_AUTOHSCROLL,
746 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
747 SetWindowText(ctl, i->sval);
755 mkctrl(fe, col1l, col2r, y, y+height, "BUTTON",
756 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
757 0, i->name, (j->ctlid = id++));
758 CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0));
764 * Drop-down list with a label beside it.
766 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
767 "STATIC", 0, 0, i->name, id++);
768 ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2,
769 "COMBOBOX", WS_TABSTOP |
770 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
771 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
773 char c, *p, *q, *str;
775 SendMessage(ctl, CB_RESETCONTENT, 0, 0);
780 while (*q && *q != c) q++;
781 str = snewn(q-p+1, char);
782 strncpy(str, p, q-p);
784 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str);
791 SendMessage(ctl, CB_SETCURSEL, i->ival, 0);
797 assert(y < winheight);
801 y += height/2; /* extra space before OK and Cancel */
802 mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON",
803 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
805 mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON",
806 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL);
808 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
810 EnableWindow(fe->hwnd, FALSE);
811 ShowWindow(fe->cfgbox, SW_NORMAL);
812 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
813 if (!IsDialogMessage(fe->cfgbox, &msg))
814 DispatchMessage(&msg);
818 EnableWindow(fe->hwnd, TRUE);
819 SetForegroundWindow(fe->hwnd);
820 DestroyWindow(fe->cfgbox);
821 DeleteObject(fe->cfgfont);
826 return (fe->cfg_done == 2);
829 static void new_game_type(frontend *fe)
835 midend_new_game(fe->me);
836 midend_size(fe->me, &x, &y);
841 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
842 (WS_THICKFRAME | WS_MAXIMIZEBOX |
846 if (fe->statusbar != NULL) {
847 GetWindowRect(fe->statusbar, &sr);
849 sr.left = sr.right = sr.top = sr.bottom = 0;
851 SetWindowPos(fe->hwnd, NULL, 0, 0,
853 r.bottom - r.top + sr.bottom - sr.top,
854 SWP_NOMOVE | SWP_NOZORDER);
855 if (fe->statusbar != NULL)
856 SetWindowPos(fe->statusbar, NULL, 0, y, x,
857 sr.bottom - sr.top, SWP_NOZORDER);
859 DeleteObject(fe->bitmap);
861 hdc = GetDC(fe->hwnd);
862 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
863 ReleaseDC(fe->hwnd, hdc);
865 midend_redraw(fe->me);
868 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
869 WPARAM wParam, LPARAM lParam)
871 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
878 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
880 if (!midend_process_key(fe->me, 0, 0, 'n'))
884 if (!midend_process_key(fe->me, 0, 0, 'r'))
888 if (!midend_process_key(fe->me, 0, 0, 'u'))
892 if (!midend_process_key(fe->me, 0, 0, '\x12'))
896 if (!midend_process_key(fe->me, 0, 0, 'q'))
900 if (get_config(fe, CFG_SETTINGS))
904 if (get_config(fe, CFG_SEED))
908 assert(fe->help_path);
909 WinHelp(hwnd, fe->help_path,
910 fe->help_has_contents ? HELP_FINDER : HELP_CONTENTS, 0);
913 assert(fe->help_path);
914 assert(game_winhelp_topic);
916 char *cmd = snewn(10+strlen(game_winhelp_topic), char); /*ick*/
917 sprintf(cmd, "JI(`',`%s')", game_winhelp_topic);
918 WinHelp(hwnd, fe->help_path, HELP_COMMAND, (DWORD)cmd);
924 int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10;
926 if (p >= 0 && p < fe->npresets) {
927 midend_set_params(fe->me, fe->presets[p]);
943 hdc = BeginPaint(hwnd, &p);
944 hdc2 = CreateCompatibleDC(hdc);
945 prevbm = SelectObject(hdc2, fe->bitmap);
947 p.rcPaint.left, p.rcPaint.top,
948 p.rcPaint.right - p.rcPaint.left,
949 p.rcPaint.bottom - p.rcPaint.top,
951 p.rcPaint.left, p.rcPaint.top,
953 SelectObject(hdc2, prevbm);
963 case VK_LEFT: key = CURSOR_LEFT; break;
964 case VK_RIGHT: key = CURSOR_RIGHT; break;
965 case VK_UP: key = CURSOR_UP; break;
966 case VK_DOWN: key = CURSOR_DOWN; break;
968 * Diagonal keys on the numeric keypad.
971 if (!(lParam & 0x01000000)) key = CURSOR_UP_RIGHT;
974 if (!(lParam & 0x01000000)) key = CURSOR_DOWN_RIGHT;
977 if (!(lParam & 0x01000000)) key = CURSOR_UP_LEFT;
980 if (!(lParam & 0x01000000)) key = CURSOR_DOWN_LEFT;
983 * Numeric keypad keys with Num Lock on.
985 case VK_NUMPAD4: key = CURSOR_LEFT; break;
986 case VK_NUMPAD6: key = CURSOR_RIGHT; break;
987 case VK_NUMPAD8: key = CURSOR_UP; break;
988 case VK_NUMPAD2: key = CURSOR_DOWN; break;
989 case VK_NUMPAD9: key = CURSOR_UP_RIGHT; break;
990 case VK_NUMPAD3: key = CURSOR_DOWN_RIGHT; break;
991 case VK_NUMPAD7: key = CURSOR_UP_LEFT; break;
992 case VK_NUMPAD1: key = CURSOR_DOWN_LEFT; break;
996 if (!midend_process_key(fe->me, 0, 0, key))
1001 m.message = WM_KEYDOWN;
1003 m.lParam = lParam & 0xdfff;
1004 TranslateMessage(&m);
1008 case WM_LBUTTONDOWN:
1009 case WM_RBUTTONDOWN:
1010 case WM_MBUTTONDOWN:
1015 * Shift-clicks count as middle-clicks, since otherwise
1016 * two-button Windows users won't have any kind of
1017 * middle click to use.
1019 if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT))
1020 button = MIDDLE_BUTTON;
1021 else if (message == WM_LBUTTONDOWN)
1022 button = LEFT_BUTTON;
1024 button = RIGHT_BUTTON;
1026 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1027 (signed short)HIWORD(lParam), button))
1040 * Shift-clicks count as middle-clicks, since otherwise
1041 * two-button Windows users won't have any kind of
1042 * middle click to use.
1044 if (message == WM_MBUTTONUP || (wParam & MK_SHIFT))
1045 button = MIDDLE_RELEASE;
1046 else if (message == WM_LBUTTONUP)
1047 button = LEFT_RELEASE;
1049 button = RIGHT_RELEASE;
1051 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1052 (signed short)HIWORD(lParam), button))
1062 if (wParam & (MK_MBUTTON | MK_SHIFT))
1063 button = MIDDLE_DRAG;
1064 else if (wParam & MK_LBUTTON)
1067 button = RIGHT_DRAG;
1069 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1070 (signed short)HIWORD(lParam), button))
1075 if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam))
1080 DWORD now = GetTickCount();
1081 float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F;
1082 midend_timer(fe->me, elapsed);
1083 fe->timer_last_tickcount = now;
1088 return DefWindowProc(hwnd, message, wParam, lParam);
1091 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
1096 InitCommonControls();
1102 wndclass.lpfnWndProc = WndProc;
1103 wndclass.cbClsExtra = 0;
1104 wndclass.cbWndExtra = 0;
1105 wndclass.hInstance = inst;
1106 wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION);
1107 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1108 wndclass.hbrBackground = NULL;
1109 wndclass.lpszMenuName = NULL;
1110 wndclass.lpszClassName = game_name;
1112 RegisterClass(&wndclass);
1115 while (*cmdline && isspace(*cmdline))
1118 if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) {
1120 sprintf(buf, "%.100s Error", game_name);
1121 MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
1125 while (GetMessage(&msg, NULL, 0, 0)) {
1126 DispatchMessage(&msg);