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_QUIT 0x0050
31 #define IDM_CONFIG 0x0060
32 #define IDM_SEED 0x0070
33 #define IDM_HELPC 0x0080
34 #define IDM_GAMEHELP 0x0090
35 #define IDM_PRESETS 0x0100
37 #define HELP_FILE_NAME "puzzles.hlp"
38 #define HELP_CNT_NAME "puzzles.cnt"
41 static FILE *debug_fp = NULL;
42 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
43 static int debug_got_console = 0;
49 if (!debug_got_console) {
51 debug_got_console = 1;
52 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
56 debug_fp = fopen("debug.log", "w");
59 if (debug_hdl != INVALID_HANDLE_VALUE) {
60 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
66 void debug_printf(char *fmt, ...)
72 vsprintf(buf, fmt, ap);
77 #define debug(x) (debug_printf x)
97 HWND hwnd, statusbar, cfgbox;
99 HBITMAP bitmap, prevbm;
106 DWORD timer_last_tickcount;
108 game_params **presets;
110 int nfonts, fontsize;
112 struct cfg_aux *cfgaux;
113 int cfg_which, cfg_done;
116 int help_has_contents;
119 void fatal(char *fmt, ...)
125 vsprintf(buf, fmt, ap);
128 MessageBox(NULL, buf, "Fatal error", MB_ICONEXCLAMATION | MB_OK);
133 void get_random_seed(void **randseed, int *randseedsize)
135 time_t *tp = snew(time_t);
137 *randseed = (void *)tp;
138 *randseedsize = sizeof(time_t);
141 void status_bar(frontend *fe, char *text)
143 SetWindowText(fe->statusbar, text);
146 void frontend_default_colour(frontend *fe, float *output)
148 DWORD c = GetSysColor(COLOR_MENU); /* ick */
150 output[0] = (float)(GetRValue(c) / 255.0);
151 output[1] = (float)(GetGValue(c) / 255.0);
152 output[2] = (float)(GetBValue(c) / 255.0);
155 void clip(frontend *fe, int x, int y, int w, int h)
157 IntersectClipRect(fe->hdc_bm, x, y, x+w, y+h);
160 void unclip(frontend *fe)
162 SelectClipRgn(fe->hdc_bm, NULL);
165 void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize,
166 int align, int colour, char *text)
171 * Find or create the font.
173 for (i = 0; i < fe->nfonts; i++)
174 if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
177 if (i == fe->nfonts) {
178 if (fe->fontsize <= fe->nfonts) {
179 fe->fontsize = fe->nfonts + 10;
180 fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
185 fe->fonts[i].type = fonttype;
186 fe->fonts[i].size = fontsize;
189 * FIXME: Really I should make at least _some_ effort to
190 * pick the correct font.
192 fe->fonts[i].font = CreateFont(-fontsize, 0, 0, 0, 0,
193 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
194 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
196 (fonttype == FONT_FIXED ?
197 FIXED_PITCH | FF_DONTCARE :
198 VARIABLE_PITCH | FF_SWISS),
203 * Position and draw the text.
210 oldfont = SelectObject(fe->hdc_bm, fe->fonts[i].font);
211 if (GetTextMetrics(fe->hdc_bm, &tm)) {
212 if (align & ALIGN_VCENTRE)
213 y -= (tm.tmAscent+tm.tmDescent)/2;
217 if (GetTextExtentPoint32(fe->hdc_bm, text, strlen(text), &size)) {
218 if (align & ALIGN_HCENTRE)
220 else if (align & ALIGN_HRIGHT)
223 SetBkMode(fe->hdc_bm, TRANSPARENT);
224 SetTextColor(fe->hdc_bm, fe->colours[colour]);
225 TextOut(fe->hdc_bm, x, y, text, strlen(text));
226 SelectObject(fe->hdc_bm, oldfont);
230 void draw_rect(frontend *fe, int x, int y, int w, int h, int colour)
232 if (w == 1 && h == 1) {
234 * Rectangle() appears to get uppity if asked to draw a 1x1
235 * rectangle, presumably on the grounds that that's beneath
236 * its dignity and you ought to be using SetPixel instead.
239 SetPixel(fe->hdc_bm, x, y, fe->colours[colour]);
241 HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[colour]);
242 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
243 Rectangle(fe->hdc_bm, x, y, x+w, y+h);
244 SelectObject(fe->hdc_bm, oldbrush);
245 SelectObject(fe->hdc_bm, oldpen);
249 void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour)
251 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
252 MoveToEx(fe->hdc_bm, x1, y1, NULL);
253 LineTo(fe->hdc_bm, x2, y2);
254 SetPixel(fe->hdc_bm, x2, y2, fe->colours[colour]);
255 SelectObject(fe->hdc_bm, oldpen);
258 void draw_polygon(frontend *fe, int *coords, int npoints,
259 int fill, int colour)
261 POINT *pts = snewn(npoints+1, POINT);
264 for (i = 0; i <= npoints; i++) {
265 int j = (i < npoints ? i : 0);
266 pts[i].x = coords[j*2];
267 pts[i].y = coords[j*2+1];
271 HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[colour]);
272 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
273 Polygon(fe->hdc_bm, pts, npoints);
274 SelectObject(fe->hdc_bm, oldbrush);
275 SelectObject(fe->hdc_bm, oldpen);
277 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
278 Polyline(fe->hdc_bm, pts, npoints+1);
279 SelectObject(fe->hdc_bm, oldpen);
285 void start_draw(frontend *fe)
288 hdc_win = GetDC(fe->hwnd);
289 fe->hdc_bm = CreateCompatibleDC(hdc_win);
290 fe->prevbm = SelectObject(fe->hdc_bm, fe->bitmap);
291 ReleaseDC(fe->hwnd, hdc_win);
293 SetMapMode(fe->hdc_bm, MM_TEXT);
296 void draw_update(frontend *fe, int x, int y, int w, int h)
305 InvalidateRect(fe->hwnd, &r, FALSE);
308 void end_draw(frontend *fe)
310 SelectObject(fe->hdc_bm, fe->prevbm);
311 DeleteDC(fe->hdc_bm);
313 DeleteObject(fe->clip);
318 void deactivate_timer(frontend *fe)
320 KillTimer(fe->hwnd, fe->timer);
324 void activate_timer(frontend *fe)
327 fe->timer = SetTimer(fe->hwnd, fe->timer, 20, NULL);
328 fe->timer_last_tickcount = GetTickCount();
333 * See if we can find a help file.
335 static void find_help_file(frontend *fe)
337 char b[2048], *p, *q, *r;
339 if (!fe->help_path) {
340 GetModuleFileName(NULL, b, sizeof(b) - 1);
342 p = strrchr(b, '\\');
343 if (p && p >= r) r = p+1;
345 if (q && q >= r) r = q+1;
346 strcpy(r, HELP_FILE_NAME);
347 if ( (fp = fopen(b, "r")) != NULL) {
348 fe->help_path = dupstr(b);
351 fe->help_path = NULL;
352 strcpy(r, HELP_CNT_NAME);
353 if ( (fp = fopen(b, "r")) != NULL) {
354 fe->help_has_contents = TRUE;
357 fe->help_has_contents = FALSE;
361 static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
370 fe->me = midend_new(fe, &thegame);
373 *error = midend_game_id(fe->me, game_id, FALSE);
381 fe->help_path = NULL;
385 midend_new_game(fe->me);
386 midend_size(fe->me, &x, &y);
391 fe->nfonts = fe->fontsize = 0;
397 colours = midend_colours(fe->me, &ncolours);
399 fe->colours = snewn(ncolours, COLORREF);
400 fe->brushes = snewn(ncolours, HBRUSH);
401 fe->pens = snewn(ncolours, HPEN);
403 for (i = 0; i < ncolours; i++) {
404 fe->colours[i] = RGB(255 * colours[i*3+0],
405 255 * colours[i*3+1],
406 255 * colours[i*3+2]);
407 fe->brushes[i] = CreateSolidBrush(fe->colours[i]);
408 fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]);
415 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
416 (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED),
419 fe->hwnd = CreateWindowEx(0, thegame.name, thegame.name,
420 WS_OVERLAPPEDWINDOW &~
421 (WS_THICKFRAME | WS_MAXIMIZEBOX),
422 CW_USEDEFAULT, CW_USEDEFAULT,
423 r.right - r.left, r.bottom - r.top,
424 NULL, NULL, inst, NULL);
427 HMENU bar = CreateMenu();
428 HMENU menu = CreateMenu();
430 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Game");
431 AppendMenu(menu, MF_ENABLED, IDM_NEW, "New");
432 AppendMenu(menu, MF_ENABLED, IDM_RESTART, "Restart");
433 AppendMenu(menu, MF_ENABLED, IDM_SEED, "Specific...");
435 if ((fe->npresets = midend_num_presets(fe->me)) > 0 ||
436 thegame.can_configure) {
437 HMENU sub = CreateMenu();
440 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "Type");
442 fe->presets = snewn(fe->npresets, game_params *);
444 for (i = 0; i < fe->npresets; i++) {
447 midend_fetch_preset(fe->me, i, &name, &fe->presets[i]);
450 * FIXME: we ought to go through and do something
451 * with ampersands here.
454 AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name);
457 if (thegame.can_configure) {
458 AppendMenu(sub, MF_ENABLED, IDM_CONFIG, "Custom...");
462 AppendMenu(menu, MF_SEPARATOR, 0, 0);
463 AppendMenu(menu, MF_ENABLED, IDM_UNDO, "Undo");
464 AppendMenu(menu, MF_ENABLED, IDM_REDO, "Redo");
465 AppendMenu(menu, MF_SEPARATOR, 0, 0);
466 AppendMenu(menu, MF_ENABLED, IDM_QUIT, "Exit");
468 HMENU hmenu = CreateMenu();
469 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)hmenu, "Help");
470 AppendMenu(hmenu, MF_ENABLED, IDM_HELPC, "Contents");
471 if (thegame.winhelp_topic) {
473 assert(thegame.name);
474 item = snewn(9+strlen(thegame.name), char); /*ick*/
475 sprintf(item, "Help on %s", thegame.name);
476 AppendMenu(hmenu, MF_ENABLED, IDM_GAMEHELP, item);
480 SetMenu(fe->hwnd, bar);
483 if (midend_wants_statusbar(fe->me)) {
484 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh",
485 WS_CHILD | WS_VISIBLE,
486 0, 0, 0, 0, /* status bar does these */
487 fe->hwnd, NULL, inst, NULL);
488 GetWindowRect(fe->statusbar, &sr);
489 SetWindowPos(fe->hwnd, NULL, 0, 0,
490 r.right - r.left, r.bottom - r.top + sr.bottom - sr.top,
491 SWP_NOMOVE | SWP_NOZORDER);
492 SetWindowPos(fe->statusbar, NULL, 0, y, x, sr.bottom - sr.top,
495 fe->statusbar = NULL;
498 hdc = GetDC(fe->hwnd);
499 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
500 ReleaseDC(fe->hwnd, hdc);
502 SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe);
504 ShowWindow(fe->hwnd, SW_NORMAL);
505 SetForegroundWindow(fe->hwnd);
507 midend_redraw(fe->me);
512 static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg,
513 WPARAM wParam, LPARAM lParam)
515 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
525 * OK and Cancel are special cases.
527 if ((HIWORD(wParam) == BN_CLICKED ||
528 HIWORD(wParam) == BN_DOUBLECLICKED) &&
529 (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) {
530 if (LOWORD(wParam) == IDOK) {
531 char *err = midend_set_config(fe->me, fe->cfg_which, fe->cfg);
534 MessageBox(hwnd, err, "Validation error",
535 MB_ICONERROR | MB_OK);
546 * First find the control whose id this is.
548 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
549 if (j->ctlid == LOWORD(wParam))
552 if (i->type == C_END)
553 return 0; /* not our problem */
555 if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) {
557 GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer));
558 buffer[lenof(buffer)-1] = '\0';
560 i->sval = dupstr(buffer);
561 } else if (i->type == C_BOOLEAN &&
562 (HIWORD(wParam) == BN_CLICKED ||
563 HIWORD(wParam) == BN_DOUBLECLICKED)) {
564 i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid);
565 } else if (i->type == C_CHOICES &&
566 HIWORD(wParam) == CBN_SELCHANGE) {
567 i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid,
581 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
582 char *wclass, int wstyle,
583 int exstyle, char *wtext, int wid)
586 ret = CreateWindowEx(exstyle, wclass, wtext,
587 wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1,
588 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
589 SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0));
593 static int get_config(frontend *fe, int which)
606 int winwidth, winheight, col1l, col1r, col2l, col2r, y;
607 int height, width, maxlabel, maxcheckbox;
609 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
610 wc.lpfnWndProc = DefDlgProc;
612 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
613 wc.hInstance = fe->inst;
615 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
616 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
617 wc.lpszMenuName = NULL;
618 wc.lpszClassName = "GameConfigBox";
621 hdc = GetDC(fe->hwnd);
622 SetMapMode(hdc, MM_TEXT);
624 fe->cfg_done = FALSE;
626 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
628 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
629 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
634 oldfont = SelectObject(hdc, fe->cfgfont);
635 if (GetTextMetrics(hdc, &tm)) {
636 height = tm.tmAscent + tm.tmDescent;
637 width = tm.tmAveCharWidth;
642 fe->cfg = midend_get_config(fe->me, which, &title);
643 fe->cfg_which = which;
646 * Figure out the layout of the config box by measuring the
647 * length of each piece of text.
649 maxlabel = maxcheckbox = 0;
650 winheight = height/2;
652 for (i = fe->cfg; i->type != C_END; i++) {
657 * Both these control types have a label filling only
658 * the left-hand column of the box.
660 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
663 winheight += height * 3 / 2 + (height / 2);
668 * Checkboxes take up the whole of the box width.
670 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
671 maxcheckbox < size.cx)
672 maxcheckbox = size.cx;
673 winheight += height + (height / 2);
678 winheight += height + height * 7 / 4; /* OK / Cancel buttons */
681 col1r = col1l + maxlabel;
682 col2l = col1r + 2*width;
683 col2r = col2l + 30*width;
684 if (col2r < col1l+2*height+maxcheckbox)
685 col2r = col1l+2*height+maxcheckbox;
686 winwidth = col2r + 2*width;
688 ReleaseDC(fe->hwnd, hdc);
691 * Create the dialog, now that we know its size.
698 r.bottom = winheight;
700 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
701 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
702 WS_CAPTION | WS_SYSMENU*/) &~
703 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
707 * Centre the dialog on its parent window.
711 GetWindowRect(fe->hwnd, &r2);
712 r.left = (r2.left + r2.right - r.right) / 2;
713 r.top = (r2.top + r2.bottom - r.bottom) / 2;
717 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title,
718 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
719 WS_CAPTION | WS_SYSMENU,
721 r.right-r.left, r.bottom-r.top,
722 fe->hwnd, NULL, fe->inst, NULL);
726 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
728 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
729 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc);
732 * Count the controls so we can allocate cfgaux.
734 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
736 fe->cfgaux = snewn(nctrls, struct cfg_aux);
740 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
744 * Edit box with a label beside it.
746 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
747 "Static", 0, 0, i->name, id++);
748 ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2,
749 "EDIT", WS_TABSTOP | ES_AUTOHSCROLL,
750 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
751 SetWindowText(ctl, i->sval);
759 mkctrl(fe, col1l, col2r, y, y+height, "BUTTON",
760 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
761 0, i->name, (j->ctlid = id++));
762 CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0));
768 * Drop-down list with a label beside it.
770 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
771 "STATIC", 0, 0, i->name, id++);
772 ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2,
773 "COMBOBOX", WS_TABSTOP |
774 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
775 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
777 char c, *p, *q, *str;
779 SendMessage(ctl, CB_RESETCONTENT, 0, 0);
784 while (*q && *q != c) q++;
785 str = snewn(q-p+1, char);
786 strncpy(str, p, q-p);
788 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str);
795 SendMessage(ctl, CB_SETCURSEL, i->ival, 0);
801 assert(y < winheight);
805 y += height/2; /* extra space before OK and Cancel */
806 mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON",
807 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
809 mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON",
810 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL);
812 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
814 EnableWindow(fe->hwnd, FALSE);
815 ShowWindow(fe->cfgbox, SW_NORMAL);
816 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
817 if (!IsDialogMessage(fe->cfgbox, &msg))
818 DispatchMessage(&msg);
822 EnableWindow(fe->hwnd, TRUE);
823 SetForegroundWindow(fe->hwnd);
824 DestroyWindow(fe->cfgbox);
825 DeleteObject(fe->cfgfont);
830 return (fe->cfg_done == 2);
833 static void new_game_type(frontend *fe)
839 midend_new_game(fe->me);
840 midend_size(fe->me, &x, &y);
845 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
846 (WS_THICKFRAME | WS_MAXIMIZEBOX |
850 if (fe->statusbar != NULL) {
851 GetWindowRect(fe->statusbar, &sr);
853 sr.left = sr.right = sr.top = sr.bottom = 0;
855 SetWindowPos(fe->hwnd, NULL, 0, 0,
857 r.bottom - r.top + sr.bottom - sr.top,
858 SWP_NOMOVE | SWP_NOZORDER);
859 if (fe->statusbar != NULL)
860 SetWindowPos(fe->statusbar, NULL, 0, y, x,
861 sr.bottom - sr.top, SWP_NOZORDER);
863 DeleteObject(fe->bitmap);
865 hdc = GetDC(fe->hwnd);
866 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
867 ReleaseDC(fe->hwnd, hdc);
869 midend_redraw(fe->me);
872 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
873 WPARAM wParam, LPARAM lParam)
875 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
882 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
884 if (!midend_process_key(fe->me, 0, 0, 'n'))
888 if (!midend_process_key(fe->me, 0, 0, 'r'))
892 if (!midend_process_key(fe->me, 0, 0, 'u'))
896 if (!midend_process_key(fe->me, 0, 0, '\x12'))
900 if (!midend_process_key(fe->me, 0, 0, 'q'))
904 if (get_config(fe, CFG_SETTINGS))
908 if (get_config(fe, CFG_SEED))
912 assert(fe->help_path);
913 WinHelp(hwnd, fe->help_path,
914 fe->help_has_contents ? HELP_FINDER : HELP_CONTENTS, 0);
917 assert(fe->help_path);
918 assert(thegame.winhelp_topic);
920 char *cmd = snewn(10+strlen(thegame.winhelp_topic), char);
921 sprintf(cmd, "JI(`',`%s')", thegame.winhelp_topic);
922 WinHelp(hwnd, fe->help_path, HELP_COMMAND, (DWORD)cmd);
928 int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10;
930 if (p >= 0 && p < fe->npresets) {
931 midend_set_params(fe->me, fe->presets[p]);
947 hdc = BeginPaint(hwnd, &p);
948 hdc2 = CreateCompatibleDC(hdc);
949 prevbm = SelectObject(hdc2, fe->bitmap);
951 p.rcPaint.left, p.rcPaint.top,
952 p.rcPaint.right - p.rcPaint.left,
953 p.rcPaint.bottom - p.rcPaint.top,
955 p.rcPaint.left, p.rcPaint.top,
957 SelectObject(hdc2, prevbm);
967 case VK_LEFT: key = CURSOR_LEFT; break;
968 case VK_RIGHT: key = CURSOR_RIGHT; break;
969 case VK_UP: key = CURSOR_UP; break;
970 case VK_DOWN: key = CURSOR_DOWN; break;
972 * Diagonal keys on the numeric keypad.
975 if (!(lParam & 0x01000000)) key = CURSOR_UP_RIGHT;
978 if (!(lParam & 0x01000000)) key = CURSOR_DOWN_RIGHT;
981 if (!(lParam & 0x01000000)) key = CURSOR_UP_LEFT;
984 if (!(lParam & 0x01000000)) key = CURSOR_DOWN_LEFT;
987 * Numeric keypad keys with Num Lock on.
989 case VK_NUMPAD4: key = CURSOR_LEFT; break;
990 case VK_NUMPAD6: key = CURSOR_RIGHT; break;
991 case VK_NUMPAD8: key = CURSOR_UP; break;
992 case VK_NUMPAD2: key = CURSOR_DOWN; break;
993 case VK_NUMPAD9: key = CURSOR_UP_RIGHT; break;
994 case VK_NUMPAD3: key = CURSOR_DOWN_RIGHT; break;
995 case VK_NUMPAD7: key = CURSOR_UP_LEFT; break;
996 case VK_NUMPAD1: key = CURSOR_DOWN_LEFT; break;
1000 if (!midend_process_key(fe->me, 0, 0, key))
1005 m.message = WM_KEYDOWN;
1007 m.lParam = lParam & 0xdfff;
1008 TranslateMessage(&m);
1012 case WM_LBUTTONDOWN:
1013 case WM_RBUTTONDOWN:
1014 case WM_MBUTTONDOWN:
1019 * Shift-clicks count as middle-clicks, since otherwise
1020 * two-button Windows users won't have any kind of
1021 * middle click to use.
1023 if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT))
1024 button = MIDDLE_BUTTON;
1025 else if (message == WM_LBUTTONDOWN)
1026 button = LEFT_BUTTON;
1028 button = RIGHT_BUTTON;
1030 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1031 (signed short)HIWORD(lParam), button))
1044 * Shift-clicks count as middle-clicks, since otherwise
1045 * two-button Windows users won't have any kind of
1046 * middle click to use.
1048 if (message == WM_MBUTTONUP || (wParam & MK_SHIFT))
1049 button = MIDDLE_RELEASE;
1050 else if (message == WM_LBUTTONUP)
1051 button = LEFT_RELEASE;
1053 button = RIGHT_RELEASE;
1055 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1056 (signed short)HIWORD(lParam), button))
1066 if (wParam & (MK_MBUTTON | MK_SHIFT))
1067 button = MIDDLE_DRAG;
1068 else if (wParam & MK_LBUTTON)
1071 button = RIGHT_DRAG;
1073 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1074 (signed short)HIWORD(lParam), button))
1079 if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam))
1084 DWORD now = GetTickCount();
1085 float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F;
1086 midend_timer(fe->me, elapsed);
1087 fe->timer_last_tickcount = now;
1092 return DefWindowProc(hwnd, message, wParam, lParam);
1095 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
1100 InitCommonControls();
1106 wndclass.lpfnWndProc = WndProc;
1107 wndclass.cbClsExtra = 0;
1108 wndclass.cbWndExtra = 0;
1109 wndclass.hInstance = inst;
1110 wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION);
1111 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1112 wndclass.hbrBackground = NULL;
1113 wndclass.lpszMenuName = NULL;
1114 wndclass.lpszClassName = thegame.name;
1116 RegisterClass(&wndclass);
1119 while (*cmdline && isspace(*cmdline))
1122 if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) {
1124 sprintf(buf, "%.100s Error", thegame.name);
1125 MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
1129 while (GetMessage(&msg, NULL, 0, 0)) {
1130 DispatchMessage(&msg);