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 status_bar(frontend *fe, char *text)
126 SetWindowText(fe->statusbar, text);
129 void frontend_default_colour(frontend *fe, float *output)
131 DWORD c = GetSysColor(COLOR_MENU); /* ick */
133 output[0] = (float)(GetRValue(c) / 255.0);
134 output[1] = (float)(GetGValue(c) / 255.0);
135 output[2] = (float)(GetBValue(c) / 255.0);
138 void clip(frontend *fe, int x, int y, int w, int h)
141 fe->clip = CreateRectRgn(0, 0, 1, 1);
142 GetClipRgn(fe->hdc_bm, fe->clip);
145 IntersectClipRect(fe->hdc_bm, x, y, x+w, y+h);
148 void unclip(frontend *fe)
151 SelectClipRgn(fe->hdc_bm, fe->clip);
154 void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize,
155 int align, int colour, char *text)
160 * Find or create the font.
162 for (i = 0; i < fe->nfonts; i++)
163 if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
166 if (i == fe->nfonts) {
167 if (fe->fontsize <= fe->nfonts) {
168 fe->fontsize = fe->nfonts + 10;
169 fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
174 fe->fonts[i].type = fonttype;
175 fe->fonts[i].size = fontsize;
178 * FIXME: Really I should make at least _some_ effort to
179 * pick the correct font.
181 fe->fonts[i].font = CreateFont(-fontsize, 0, 0, 0, 0,
182 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
183 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
185 (fonttype == FONT_FIXED ?
186 FIXED_PITCH | FF_DONTCARE :
187 VARIABLE_PITCH | FF_SWISS),
192 * Position and draw the text.
199 oldfont = SelectObject(fe->hdc_bm, fe->fonts[i].font);
200 if (GetTextMetrics(fe->hdc_bm, &tm)) {
201 if (align & ALIGN_VCENTRE)
202 y -= (tm.tmAscent+tm.tmDescent)/2;
206 if (GetTextExtentPoint32(fe->hdc_bm, text, strlen(text), &size)) {
207 if (align & ALIGN_HCENTRE)
209 else if (align & ALIGN_HRIGHT)
212 SetBkMode(fe->hdc_bm, TRANSPARENT);
213 TextOut(fe->hdc_bm, x, y, text, strlen(text));
214 SelectObject(fe->hdc_bm, oldfont);
218 void draw_rect(frontend *fe, int x, int y, int w, int h, int colour)
220 if (w == 1 && h == 1) {
222 * Rectangle() appears to get uppity if asked to draw a 1x1
223 * rectangle, presumably on the grounds that that's beneath
224 * its dignity and you ought to be using SetPixel instead.
227 SetPixel(fe->hdc_bm, x, y, fe->colours[colour]);
229 HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[colour]);
230 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
231 Rectangle(fe->hdc_bm, x, y, x+w, y+h);
232 SelectObject(fe->hdc_bm, oldbrush);
233 SelectObject(fe->hdc_bm, oldpen);
237 void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour)
239 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
240 MoveToEx(fe->hdc_bm, x1, y1, NULL);
241 LineTo(fe->hdc_bm, x2, y2);
242 SetPixel(fe->hdc_bm, x2, y2, fe->colours[colour]);
243 SelectObject(fe->hdc_bm, oldpen);
246 void draw_polygon(frontend *fe, int *coords, int npoints,
247 int fill, int colour)
249 POINT *pts = snewn(npoints+1, POINT);
252 for (i = 0; i <= npoints; i++) {
253 int j = (i < npoints ? i : 0);
254 pts[i].x = coords[j*2];
255 pts[i].y = coords[j*2+1];
259 HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[colour]);
260 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
261 Polygon(fe->hdc_bm, pts, npoints);
262 SelectObject(fe->hdc_bm, oldbrush);
263 SelectObject(fe->hdc_bm, oldpen);
265 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
266 Polyline(fe->hdc_bm, pts, npoints+1);
267 SelectObject(fe->hdc_bm, oldpen);
273 void start_draw(frontend *fe)
276 hdc_win = GetDC(fe->hwnd);
277 fe->hdc_bm = CreateCompatibleDC(hdc_win);
278 fe->prevbm = SelectObject(fe->hdc_bm, fe->bitmap);
279 ReleaseDC(fe->hwnd, hdc_win);
281 SetMapMode(fe->hdc_bm, MM_TEXT);
284 void draw_update(frontend *fe, int x, int y, int w, int h)
293 InvalidateRect(fe->hwnd, &r, FALSE);
296 void end_draw(frontend *fe)
298 SelectObject(fe->hdc_bm, fe->prevbm);
299 DeleteDC(fe->hdc_bm);
301 DeleteObject(fe->clip);
306 void deactivate_timer(frontend *fe)
308 KillTimer(fe->hwnd, fe->timer);
312 void activate_timer(frontend *fe)
315 fe->timer = SetTimer(fe->hwnd, fe->timer, 20, NULL);
316 fe->timer_last_tickcount = GetTickCount();
321 * See if we can find a help file.
323 static void find_help_file(frontend *fe)
325 char b[2048], *p, *q, *r;
327 if (!fe->help_path) {
328 GetModuleFileName(NULL, b, sizeof(b) - 1);
330 p = strrchr(b, '\\');
331 if (p && p >= r) r = p+1;
333 if (q && q >= r) r = q+1;
334 strcpy(r, HELP_FILE_NAME);
335 if ( (fp = fopen(b, "r")) != NULL) {
336 fe->help_path = dupstr(b);
339 fe->help_path = NULL;
340 strcpy(r, HELP_CNT_NAME);
341 if ( (fp = fopen(b, "r")) != NULL) {
342 fe->help_has_contents = TRUE;
345 fe->help_has_contents = FALSE;
349 static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
360 fe->me = midend_new(fe, &t, sizeof(t));
363 *error = midend_game_id(fe->me, game_id, FALSE);
371 fe->help_path = NULL;
375 midend_new_game(fe->me);
376 midend_size(fe->me, &x, &y);
381 fe->nfonts = fe->fontsize = 0;
387 colours = midend_colours(fe->me, &ncolours);
389 fe->colours = snewn(ncolours, COLORREF);
390 fe->brushes = snewn(ncolours, HBRUSH);
391 fe->pens = snewn(ncolours, HPEN);
393 for (i = 0; i < ncolours; i++) {
394 fe->colours[i] = RGB(255 * colours[i*3+0],
395 255 * colours[i*3+1],
396 255 * colours[i*3+2]);
397 fe->brushes[i] = CreateSolidBrush(fe->colours[i]);
398 fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]);
405 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
406 (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED),
409 fe->hwnd = CreateWindowEx(0, game_name, game_name,
410 WS_OVERLAPPEDWINDOW &~
411 (WS_THICKFRAME | WS_MAXIMIZEBOX),
412 CW_USEDEFAULT, CW_USEDEFAULT,
413 r.right - r.left, r.bottom - r.top,
414 NULL, NULL, inst, NULL);
417 HMENU bar = CreateMenu();
418 HMENU menu = CreateMenu();
420 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Game");
421 AppendMenu(menu, MF_ENABLED, IDM_NEW, "New");
422 AppendMenu(menu, MF_ENABLED, IDM_RESTART, "Restart");
423 AppendMenu(menu, MF_ENABLED, IDM_SEED, "Specific...");
425 if ((fe->npresets = midend_num_presets(fe->me)) > 0 ||
426 game_can_configure) {
427 HMENU sub = CreateMenu();
430 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "Type");
432 fe->presets = snewn(fe->npresets, game_params *);
434 for (i = 0; i < fe->npresets; i++) {
437 midend_fetch_preset(fe->me, i, &name, &fe->presets[i]);
440 * FIXME: we ought to go through and do something
441 * with ampersands here.
444 AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name);
447 if (game_can_configure) {
448 AppendMenu(sub, MF_ENABLED, IDM_CONFIG, "Custom...");
452 AppendMenu(menu, MF_SEPARATOR, 0, 0);
453 AppendMenu(menu, MF_ENABLED, IDM_UNDO, "Undo");
454 AppendMenu(menu, MF_ENABLED, IDM_REDO, "Redo");
455 AppendMenu(menu, MF_SEPARATOR, 0, 0);
456 AppendMenu(menu, MF_ENABLED, IDM_QUIT, "Exit");
458 HMENU hmenu = CreateMenu();
459 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)hmenu, "Help");
460 AppendMenu(hmenu, MF_ENABLED, IDM_HELPC, "Contents");
461 if (game_winhelp_topic) {
464 item = snewn(9+strlen(game_name), char); /*ick*/
465 sprintf(item, "Help on %s", game_name);
466 AppendMenu(hmenu, MF_ENABLED, IDM_GAMEHELP, item);
470 SetMenu(fe->hwnd, bar);
473 if (midend_wants_statusbar(fe->me)) {
474 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh",
475 WS_CHILD | WS_VISIBLE,
476 0, 0, 0, 0, /* status bar does these */
477 fe->hwnd, NULL, inst, NULL);
478 GetWindowRect(fe->statusbar, &sr);
479 SetWindowPos(fe->hwnd, NULL, 0, 0,
480 r.right - r.left, r.bottom - r.top + sr.bottom - sr.top,
481 SWP_NOMOVE | SWP_NOZORDER);
482 SetWindowPos(fe->statusbar, NULL, 0, y, x, sr.bottom - sr.top,
485 fe->statusbar = NULL;
488 hdc = GetDC(fe->hwnd);
489 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
490 ReleaseDC(fe->hwnd, hdc);
492 SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe);
494 ShowWindow(fe->hwnd, SW_NORMAL);
495 SetForegroundWindow(fe->hwnd);
497 midend_redraw(fe->me);
502 static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg,
503 WPARAM wParam, LPARAM lParam)
505 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
515 * OK and Cancel are special cases.
517 if ((HIWORD(wParam) == BN_CLICKED ||
518 HIWORD(wParam) == BN_DOUBLECLICKED) &&
519 (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) {
520 if (LOWORD(wParam) == IDOK) {
521 char *err = midend_set_config(fe->me, fe->cfg_which, fe->cfg);
524 MessageBox(hwnd, err, "Validation error",
525 MB_ICONERROR | MB_OK);
536 * First find the control whose id this is.
538 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
539 if (j->ctlid == LOWORD(wParam))
542 if (i->type == C_END)
543 return 0; /* not our problem */
545 if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) {
547 GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer));
548 buffer[lenof(buffer)-1] = '\0';
550 i->sval = dupstr(buffer);
551 } else if (i->type == C_BOOLEAN &&
552 (HIWORD(wParam) == BN_CLICKED ||
553 HIWORD(wParam) == BN_DOUBLECLICKED)) {
554 i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid);
555 } else if (i->type == C_CHOICES &&
556 HIWORD(wParam) == CBN_SELCHANGE) {
557 i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid,
571 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
572 char *wclass, int wstyle,
573 int exstyle, char *wtext, int wid)
576 ret = CreateWindowEx(exstyle, wclass, wtext,
577 wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1,
578 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
579 SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0));
583 static int get_config(frontend *fe, int which)
596 int winwidth, winheight, col1l, col1r, col2l, col2r, y;
597 int height, width, maxlabel, maxcheckbox;
599 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
600 wc.lpfnWndProc = DefDlgProc;
602 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
603 wc.hInstance = fe->inst;
605 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
606 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
607 wc.lpszMenuName = NULL;
608 wc.lpszClassName = "GameConfigBox";
611 hdc = GetDC(fe->hwnd);
612 SetMapMode(hdc, MM_TEXT);
614 fe->cfg_done = FALSE;
616 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
618 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
619 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
624 oldfont = SelectObject(hdc, fe->cfgfont);
625 if (GetTextMetrics(hdc, &tm)) {
626 height = tm.tmAscent + tm.tmDescent;
627 width = tm.tmAveCharWidth;
632 fe->cfg = midend_get_config(fe->me, which, &title);
633 fe->cfg_which = which;
636 * Figure out the layout of the config box by measuring the
637 * length of each piece of text.
639 maxlabel = maxcheckbox = 0;
640 winheight = height/2;
642 for (i = fe->cfg; i->type != C_END; i++) {
647 * Both these control types have a label filling only
648 * the left-hand column of the box.
650 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
653 winheight += height * 3 / 2 + (height / 2);
658 * Checkboxes take up the whole of the box width.
660 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
661 maxcheckbox < size.cx)
662 maxcheckbox = size.cx;
663 winheight += height + (height / 2);
668 winheight += height + height * 7 / 4; /* OK / Cancel buttons */
671 col1r = col1l + maxlabel;
672 col2l = col1r + 2*width;
673 col2r = col2l + 30*width;
674 if (col2r < col1l+2*height+maxcheckbox)
675 col2r = col1l+2*height+maxcheckbox;
676 winwidth = col2r + 2*width;
678 ReleaseDC(fe->hwnd, hdc);
681 * Create the dialog, now that we know its size.
688 r.bottom = winheight;
690 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
691 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
692 WS_CAPTION | WS_SYSMENU*/) &~
693 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
697 * Centre the dialog on its parent window.
701 GetWindowRect(fe->hwnd, &r2);
702 r.left = (r2.left + r2.right - r.right) / 2;
703 r.top = (r2.top + r2.bottom - r.bottom) / 2;
707 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title,
708 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
709 WS_CAPTION | WS_SYSMENU,
711 r.right-r.left, r.bottom-r.top,
712 fe->hwnd, NULL, fe->inst, NULL);
716 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
718 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
719 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc);
722 * Count the controls so we can allocate cfgaux.
724 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
726 fe->cfgaux = snewn(nctrls, struct cfg_aux);
730 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
734 * Edit box with a label beside it.
736 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
737 "Static", 0, 0, i->name, id++);
738 ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2,
739 "EDIT", WS_TABSTOP | ES_AUTOHSCROLL,
740 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
741 SetWindowText(ctl, i->sval);
749 mkctrl(fe, col1l, col2r, y, y+height, "BUTTON",
750 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
751 0, i->name, (j->ctlid = id++));
752 CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0));
758 * Drop-down list with a label beside it.
760 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
761 "STATIC", 0, 0, i->name, id++);
762 ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2,
763 "COMBOBOX", WS_TABSTOP |
764 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
765 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
767 char c, *p, *q, *str;
769 SendMessage(ctl, CB_RESETCONTENT, 0, 0);
774 while (*q && *q != c) q++;
775 str = snewn(q-p+1, char);
776 strncpy(str, p, q-p);
778 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str);
785 SendMessage(ctl, CB_SETCURSEL, i->ival, 0);
791 assert(y < winheight);
795 y += height/2; /* extra space before OK and Cancel */
796 mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON",
797 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
799 mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON",
800 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL);
802 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
804 EnableWindow(fe->hwnd, FALSE);
805 ShowWindow(fe->cfgbox, SW_NORMAL);
806 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
807 if (!IsDialogMessage(fe->cfgbox, &msg))
808 DispatchMessage(&msg);
812 EnableWindow(fe->hwnd, TRUE);
813 SetForegroundWindow(fe->hwnd);
814 DestroyWindow(fe->cfgbox);
815 DeleteObject(fe->cfgfont);
820 return (fe->cfg_done == 2);
823 static void new_game_type(frontend *fe)
829 midend_new_game(fe->me);
830 midend_size(fe->me, &x, &y);
835 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
836 (WS_THICKFRAME | WS_MAXIMIZEBOX |
840 if (fe->statusbar != NULL) {
841 GetWindowRect(fe->statusbar, &sr);
843 sr.left = sr.right = sr.top = sr.bottom = 0;
845 SetWindowPos(fe->hwnd, NULL, 0, 0,
847 r.bottom - r.top + sr.bottom - sr.top,
848 SWP_NOMOVE | SWP_NOZORDER);
849 if (fe->statusbar != NULL)
850 SetWindowPos(fe->statusbar, NULL, 0, y, x,
851 sr.bottom - sr.top, SWP_NOZORDER);
853 DeleteObject(fe->bitmap);
855 hdc = GetDC(fe->hwnd);
856 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
857 ReleaseDC(fe->hwnd, hdc);
859 midend_redraw(fe->me);
862 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
863 WPARAM wParam, LPARAM lParam)
865 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
872 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
874 if (!midend_process_key(fe->me, 0, 0, 'n'))
878 if (!midend_process_key(fe->me, 0, 0, 'r'))
882 if (!midend_process_key(fe->me, 0, 0, 'u'))
886 if (!midend_process_key(fe->me, 0, 0, '\x12'))
890 if (!midend_process_key(fe->me, 0, 0, 'q'))
894 if (get_config(fe, CFG_SETTINGS))
898 if (get_config(fe, CFG_SEED))
902 assert(fe->help_path);
903 WinHelp(hwnd, fe->help_path,
904 fe->help_has_contents ? HELP_FINDER : HELP_CONTENTS, 0);
907 assert(fe->help_path);
908 assert(game_winhelp_topic);
910 char *cmd = snewn(10+strlen(game_winhelp_topic), char); /*ick*/
911 sprintf(cmd, "JI(`',`%s')", game_winhelp_topic);
912 WinHelp(hwnd, fe->help_path, HELP_COMMAND, (DWORD)cmd);
918 int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10;
920 if (p >= 0 && p < fe->npresets) {
921 midend_set_params(fe->me, fe->presets[p]);
937 hdc = BeginPaint(hwnd, &p);
938 hdc2 = CreateCompatibleDC(hdc);
939 prevbm = SelectObject(hdc2, fe->bitmap);
941 p.rcPaint.left, p.rcPaint.top,
942 p.rcPaint.right - p.rcPaint.left,
943 p.rcPaint.bottom - p.rcPaint.top,
945 p.rcPaint.left, p.rcPaint.top,
947 SelectObject(hdc2, prevbm);
957 case VK_LEFT: key = CURSOR_LEFT; break;
958 case VK_RIGHT: key = CURSOR_RIGHT; break;
959 case VK_UP: key = CURSOR_UP; break;
960 case VK_DOWN: key = CURSOR_DOWN; break;
962 * Diagonal keys on the numeric keypad.
965 if (!(lParam & 0x01000000)) key = CURSOR_UP_RIGHT;
968 if (!(lParam & 0x01000000)) key = CURSOR_DOWN_RIGHT;
971 if (!(lParam & 0x01000000)) key = CURSOR_UP_LEFT;
974 if (!(lParam & 0x01000000)) key = CURSOR_DOWN_LEFT;
977 * Numeric keypad keys with Num Lock on.
979 case VK_NUMPAD4: key = CURSOR_LEFT; break;
980 case VK_NUMPAD6: key = CURSOR_RIGHT; break;
981 case VK_NUMPAD8: key = CURSOR_UP; break;
982 case VK_NUMPAD2: key = CURSOR_DOWN; break;
983 case VK_NUMPAD9: key = CURSOR_UP_RIGHT; break;
984 case VK_NUMPAD3: key = CURSOR_DOWN_RIGHT; break;
985 case VK_NUMPAD7: key = CURSOR_UP_LEFT; break;
986 case VK_NUMPAD1: key = CURSOR_DOWN_LEFT; break;
990 if (!midend_process_key(fe->me, 0, 0, key))
995 m.message = WM_KEYDOWN;
997 m.lParam = lParam & 0xdfff;
998 TranslateMessage(&m);
1002 case WM_LBUTTONDOWN:
1003 case WM_RBUTTONDOWN:
1004 case WM_MBUTTONDOWN:
1009 * Shift-clicks count as middle-clicks, since otherwise
1010 * two-button Windows users won't have any kind of
1011 * middle click to use.
1013 if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT))
1014 button = MIDDLE_BUTTON;
1015 else if (message == WM_LBUTTONDOWN)
1016 button = LEFT_BUTTON;
1018 button = RIGHT_BUTTON;
1020 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1021 (signed short)HIWORD(lParam), button))
1034 * Shift-clicks count as middle-clicks, since otherwise
1035 * two-button Windows users won't have any kind of
1036 * middle click to use.
1038 if (message == WM_MBUTTONUP || (wParam & MK_SHIFT))
1039 button = MIDDLE_RELEASE;
1040 else if (message == WM_LBUTTONUP)
1041 button = LEFT_RELEASE;
1043 button = RIGHT_RELEASE;
1045 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1046 (signed short)HIWORD(lParam), button))
1056 if (wParam & (MK_MBUTTON | MK_SHIFT))
1057 button = MIDDLE_DRAG;
1058 else if (wParam & MK_LBUTTON)
1061 button = RIGHT_DRAG;
1063 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1064 (signed short)HIWORD(lParam), button))
1069 if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam))
1074 DWORD now = GetTickCount();
1075 float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F;
1076 midend_timer(fe->me, elapsed);
1077 fe->timer_last_tickcount = now;
1082 return DefWindowProc(hwnd, message, wParam, lParam);
1085 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
1090 InitCommonControls();
1096 wndclass.lpfnWndProc = WndProc;
1097 wndclass.cbClsExtra = 0;
1098 wndclass.cbWndExtra = 0;
1099 wndclass.hInstance = inst;
1100 wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION);
1101 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1102 wndclass.hbrBackground = NULL;
1103 wndclass.lpszMenuName = NULL;
1104 wndclass.lpszClassName = game_name;
1106 RegisterClass(&wndclass);
1109 while (*cmdline && isspace(*cmdline))
1112 if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) {
1114 sprintf(buf, "%.100s Error", game_name);
1115 MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
1119 while (GetMessage(&msg, NULL, 0, 0)) {
1120 DispatchMessage(&msg);