2 * windows.c: Windows front end for my puzzle collection.
16 #define IDM_NEW 0x0010
17 #define IDM_RESTART 0x0020
18 #define IDM_UNDO 0x0030
19 #define IDM_REDO 0x0040
20 #define IDM_QUIT 0x0050
21 #define IDM_CONFIG 0x0060
22 #define IDM_SEED 0x0070
23 #define IDM_PRESETS 0x0100
26 static FILE *debug_fp
= NULL
;
27 static HANDLE debug_hdl
= INVALID_HANDLE_VALUE
;
28 static int debug_got_console
= 0;
34 if (!debug_got_console
) {
36 debug_got_console
= 1;
37 debug_hdl
= GetStdHandle(STD_OUTPUT_HANDLE
);
41 debug_fp
= fopen("debug.log", "w");
44 if (debug_hdl
!= INVALID_HANDLE_VALUE
) {
45 WriteFile(debug_hdl
, buf
, strlen(buf
), &dw
, NULL
);
51 void debug_printf(char *fmt
, ...)
57 vsprintf(buf
, fmt
, ap
);
62 #define debug(x) (debug_printf x)
82 HWND hwnd
, statusbar
, cfgbox
;
84 HBITMAP bitmap
, prevbm
;
92 game_params
**presets
;
96 struct cfg_aux
*cfgaux
;
97 int cfg_which
, cfg_done
;
101 void fatal(char *fmt
, ...)
107 vsprintf(buf
, fmt
, ap
);
110 MessageBox(NULL
, buf
, "Fatal error", MB_ICONEXCLAMATION
| MB_OK
);
115 void status_bar(frontend
*fe
, char *text
)
117 SetWindowText(fe
->statusbar
, text
);
120 void frontend_default_colour(frontend
*fe
, float *output
)
122 DWORD c
= GetSysColor(COLOR_MENU
); /* ick */
124 output
[0] = (float)(GetRValue(c
) / 255.0);
125 output
[1] = (float)(GetGValue(c
) / 255.0);
126 output
[2] = (float)(GetBValue(c
) / 255.0);
129 void clip(frontend
*fe
, int x
, int y
, int w
, int h
)
132 fe
->clip
= CreateRectRgn(0, 0, 1, 1);
133 GetClipRgn(fe
->hdc_bm
, fe
->clip
);
136 IntersectClipRect(fe
->hdc_bm
, x
, y
, x
+w
, y
+h
);
139 void unclip(frontend
*fe
)
142 SelectClipRgn(fe
->hdc_bm
, fe
->clip
);
145 void draw_text(frontend
*fe
, int x
, int y
, int fonttype
, int fontsize
,
146 int align
, int colour
, char *text
)
151 * Find or create the font.
153 for (i
= 0; i
< fe
->nfonts
; i
++)
154 if (fe
->fonts
[i
].type
== fonttype
&& fe
->fonts
[i
].size
== fontsize
)
157 if (i
== fe
->nfonts
) {
158 if (fe
->fontsize
<= fe
->nfonts
) {
159 fe
->fontsize
= fe
->nfonts
+ 10;
160 fe
->fonts
= sresize(fe
->fonts
, fe
->fontsize
, struct font
);
165 fe
->fonts
[i
].type
= fonttype
;
166 fe
->fonts
[i
].size
= fontsize
;
169 * FIXME: Really I should make at least _some_ effort to
170 * pick the correct font.
172 fe
->fonts
[i
].font
= CreateFont(-fontsize
, 0, 0, 0, 0,
173 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
174 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
176 (fonttype
== FONT_FIXED ?
177 FIXED_PITCH
| FF_DONTCARE
:
178 VARIABLE_PITCH
| FF_SWISS
),
183 * Position and draw the text.
190 oldfont
= SelectObject(fe
->hdc_bm
, fe
->fonts
[i
].font
);
191 if (GetTextMetrics(fe
->hdc_bm
, &tm
)) {
192 if (align
& ALIGN_VCENTRE
)
193 y
-= (tm
.tmAscent
+tm
.tmDescent
)/2;
197 if (GetTextExtentPoint32(fe
->hdc_bm
, text
, strlen(text
), &size
)) {
198 if (align
& ALIGN_HCENTRE
)
200 else if (align
& ALIGN_HRIGHT
)
203 SetBkMode(fe
->hdc_bm
, TRANSPARENT
);
204 TextOut(fe
->hdc_bm
, x
, y
, text
, strlen(text
));
205 SelectObject(fe
->hdc_bm
, oldfont
);
209 void draw_rect(frontend
*fe
, int x
, int y
, int w
, int h
, int colour
)
211 if (w
== 1 && h
== 1) {
213 * Rectangle() appears to get uppity if asked to draw a 1x1
214 * rectangle, presumably on the grounds that that's beneath
215 * its dignity and you ought to be using SetPixel instead.
218 SetPixel(fe
->hdc_bm
, x
, y
, fe
->colours
[colour
]);
220 HBRUSH oldbrush
= SelectObject(fe
->hdc_bm
, fe
->brushes
[colour
]);
221 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
222 Rectangle(fe
->hdc_bm
, x
, y
, x
+w
, y
+h
);
223 SelectObject(fe
->hdc_bm
, oldbrush
);
224 SelectObject(fe
->hdc_bm
, oldpen
);
228 void draw_line(frontend
*fe
, int x1
, int y1
, int x2
, int y2
, int colour
)
230 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
231 MoveToEx(fe
->hdc_bm
, x1
, y1
, NULL
);
232 LineTo(fe
->hdc_bm
, x2
, y2
);
233 SetPixel(fe
->hdc_bm
, x2
, y2
, fe
->colours
[colour
]);
234 SelectObject(fe
->hdc_bm
, oldpen
);
237 void draw_polygon(frontend
*fe
, int *coords
, int npoints
,
238 int fill
, int colour
)
240 POINT
*pts
= snewn(npoints
+1, POINT
);
243 for (i
= 0; i
<= npoints
; i
++) {
244 int j
= (i
< npoints ? i
: 0);
245 pts
[i
].x
= coords
[j
*2];
246 pts
[i
].y
= coords
[j
*2+1];
250 HBRUSH oldbrush
= SelectObject(fe
->hdc_bm
, fe
->brushes
[colour
]);
251 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
252 Polygon(fe
->hdc_bm
, pts
, npoints
);
253 SelectObject(fe
->hdc_bm
, oldbrush
);
254 SelectObject(fe
->hdc_bm
, oldpen
);
256 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
257 Polyline(fe
->hdc_bm
, pts
, npoints
+1);
258 SelectObject(fe
->hdc_bm
, oldpen
);
264 void start_draw(frontend
*fe
)
267 hdc_win
= GetDC(fe
->hwnd
);
268 fe
->hdc_bm
= CreateCompatibleDC(hdc_win
);
269 fe
->prevbm
= SelectObject(fe
->hdc_bm
, fe
->bitmap
);
270 ReleaseDC(fe
->hwnd
, hdc_win
);
272 SetMapMode(fe
->hdc_bm
, MM_TEXT
);
275 void draw_update(frontend
*fe
, int x
, int y
, int w
, int h
)
284 InvalidateRect(fe
->hwnd
, &r
, FALSE
);
287 void end_draw(frontend
*fe
)
289 SelectObject(fe
->hdc_bm
, fe
->prevbm
);
290 DeleteDC(fe
->hdc_bm
);
292 DeleteObject(fe
->clip
);
297 void deactivate_timer(frontend
*fe
)
299 KillTimer(fe
->hwnd
, fe
->timer
);
303 void activate_timer(frontend
*fe
)
305 fe
->timer
= SetTimer(fe
->hwnd
, fe
->timer
, 20, NULL
);
308 static frontend
*new_window(HINSTANCE inst
)
319 fe
->me
= midend_new(fe
, &t
, sizeof(t
));
322 midend_new_game(fe
->me
);
323 midend_size(fe
->me
, &x
, &y
);
331 colours
= midend_colours(fe
->me
, &ncolours
);
333 fe
->colours
= snewn(ncolours
, COLORREF
);
334 fe
->brushes
= snewn(ncolours
, HBRUSH
);
335 fe
->pens
= snewn(ncolours
, HPEN
);
337 for (i
= 0; i
< ncolours
; i
++) {
338 fe
->colours
[i
] = RGB(255 * colours
[i
*3+0],
339 255 * colours
[i
*3+1],
340 255 * colours
[i
*3+2]);
341 fe
->brushes
[i
] = CreateSolidBrush(fe
->colours
[i
]);
342 fe
->pens
[i
] = CreatePen(PS_SOLID
, 1, fe
->colours
[i
]);
349 AdjustWindowRectEx(&r
, WS_OVERLAPPEDWINDOW
&~
350 (WS_THICKFRAME
| WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
353 fe
->hwnd
= CreateWindowEx(0, game_name
, game_name
,
354 WS_OVERLAPPEDWINDOW
&~
355 (WS_THICKFRAME
| WS_MAXIMIZEBOX
),
356 CW_USEDEFAULT
, CW_USEDEFAULT
,
357 r
.right
- r
.left
, r
.bottom
- r
.top
,
358 NULL
, NULL
, inst
, NULL
);
361 HMENU bar
= CreateMenu();
362 HMENU menu
= CreateMenu();
364 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)menu
, "Game");
365 AppendMenu(menu
, MF_ENABLED
, IDM_NEW
, "New");
366 AppendMenu(menu
, MF_ENABLED
, IDM_RESTART
, "Restart");
367 AppendMenu(menu
, MF_ENABLED
, IDM_SEED
, "Specific...");
369 if ((fe
->npresets
= midend_num_presets(fe
->me
)) > 0 ||
370 game_can_configure
) {
371 HMENU sub
= CreateMenu();
374 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)sub
, "Type");
376 fe
->presets
= snewn(fe
->npresets
, game_params
*);
378 for (i
= 0; i
< fe
->npresets
; i
++) {
381 midend_fetch_preset(fe
->me
, i
, &name
, &fe
->presets
[i
]);
384 * FIXME: we ought to go through and do something
385 * with ampersands here.
388 AppendMenu(sub
, MF_ENABLED
, IDM_PRESETS
+ 0x10 * i
, name
);
391 if (game_can_configure
) {
392 AppendMenu(sub
, MF_ENABLED
, IDM_CONFIG
, "Custom...");
396 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
397 AppendMenu(menu
, MF_ENABLED
, IDM_UNDO
, "Undo");
398 AppendMenu(menu
, MF_ENABLED
, IDM_REDO
, "Redo");
399 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
400 AppendMenu(menu
, MF_ENABLED
, IDM_QUIT
, "Exit");
401 SetMenu(fe
->hwnd
, bar
);
404 if (midend_wants_statusbar(fe
->me
)) {
405 fe
->statusbar
= CreateWindowEx(0, STATUSCLASSNAME
, "ooh",
406 WS_CHILD
| WS_VISIBLE
,
407 0, 0, 0, 0, /* status bar does these */
408 fe
->hwnd
, NULL
, inst
, NULL
);
409 GetWindowRect(fe
->statusbar
, &sr
);
410 SetWindowPos(fe
->hwnd
, NULL
, 0, 0,
411 r
.right
- r
.left
, r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
412 SWP_NOMOVE
| SWP_NOZORDER
);
413 SetWindowPos(fe
->statusbar
, NULL
, 0, y
, x
, sr
.bottom
- sr
.top
,
416 fe
->statusbar
= NULL
;
419 hdc
= GetDC(fe
->hwnd
);
420 fe
->bitmap
= CreateCompatibleBitmap(hdc
, x
, y
);
421 ReleaseDC(fe
->hwnd
, hdc
);
423 SetWindowLong(fe
->hwnd
, GWL_USERDATA
, (LONG
)fe
);
425 ShowWindow(fe
->hwnd
, SW_NORMAL
);
426 SetForegroundWindow(fe
->hwnd
);
428 midend_redraw(fe
->me
);
433 static int CALLBACK
ConfigDlgProc(HWND hwnd
, UINT msg
,
434 WPARAM wParam
, LPARAM lParam
)
436 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
446 * OK and Cancel are special cases.
448 if ((HIWORD(wParam
) == BN_CLICKED
||
449 HIWORD(wParam
) == BN_DOUBLECLICKED
) &&
450 (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)) {
451 if (LOWORD(wParam
) == IDOK
) {
452 char *err
= midend_set_config(fe
->me
, fe
->cfg_which
, fe
->cfg
);
455 MessageBox(hwnd
, err
, "Validation error",
456 MB_ICONERROR
| MB_OK
);
467 * First find the control whose id this is.
469 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
470 if (j
->ctlid
== LOWORD(wParam
))
473 if (i
->type
== C_END
)
474 return 0; /* not our problem */
476 if (i
->type
== C_STRING
&& HIWORD(wParam
) == EN_CHANGE
) {
478 GetDlgItemText(fe
->cfgbox
, j
->ctlid
, buffer
, lenof(buffer
));
479 buffer
[lenof(buffer
)-1] = '\0';
481 i
->sval
= dupstr(buffer
);
482 } else if (i
->type
== C_BOOLEAN
&&
483 (HIWORD(wParam
) == BN_CLICKED
||
484 HIWORD(wParam
) == BN_DOUBLECLICKED
)) {
485 i
->ival
= IsDlgButtonChecked(fe
->cfgbox
, j
->ctlid
);
486 } else if (i
->type
== C_CHOICES
&&
487 HIWORD(wParam
) == CBN_SELCHANGE
) {
488 i
->ival
= SendDlgItemMessage(fe
->cfgbox
, j
->ctlid
,
502 HWND
mkctrl(frontend
*fe
, int x1
, int x2
, int y1
, int y2
,
503 char *wclass
, int wstyle
,
504 int exstyle
, char *wtext
, int wid
)
507 ret
= CreateWindowEx(exstyle
, wclass
, wtext
,
508 wstyle
| WS_CHILD
| WS_VISIBLE
, x1
, y1
, x2
-x1
, y2
-y1
,
509 fe
->cfgbox
, (HMENU
) wid
, fe
->inst
, NULL
);
510 SendMessage(ret
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, MAKELPARAM(TRUE
, 0));
514 static int get_config(frontend
*fe
, int which
)
527 int winwidth
, winheight
, col1l
, col1r
, col2l
, col2r
, y
;
528 int height
, width
, maxlabel
, maxcheckbox
;
530 wc
.style
= CS_DBLCLKS
| CS_SAVEBITS
| CS_BYTEALIGNWINDOW
;
531 wc
.lpfnWndProc
= DefDlgProc
;
533 wc
.cbWndExtra
= DLGWINDOWEXTRA
+ 8;
534 wc
.hInstance
= fe
->inst
;
536 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
537 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+1);
538 wc
.lpszMenuName
= NULL
;
539 wc
.lpszClassName
= "GameConfigBox";
542 hdc
= GetDC(fe
->hwnd
);
543 SetMapMode(hdc
, MM_TEXT
);
545 fe
->cfg_done
= FALSE
;
547 fe
->cfgfont
= CreateFont(-MulDiv(8, GetDeviceCaps(hdc
, LOGPIXELSY
), 72),
549 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
550 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
555 oldfont
= SelectObject(hdc
, fe
->cfgfont
);
556 if (GetTextMetrics(hdc
, &tm
)) {
557 height
= tm
.tmAscent
+ tm
.tmDescent
;
558 width
= tm
.tmAveCharWidth
;
563 fe
->cfg
= midend_get_config(fe
->me
, which
, &title
);
564 fe
->cfg_which
= which
;
567 * Figure out the layout of the config box by measuring the
568 * length of each piece of text.
570 maxlabel
= maxcheckbox
= 0;
571 winheight
= height
/2;
573 for (i
= fe
->cfg
; i
->type
!= C_END
; i
++) {
578 * Both these control types have a label filling only
579 * the left-hand column of the box.
581 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
584 winheight
+= height
* 3 / 2 + (height
/ 2);
589 * Checkboxes take up the whole of the box width.
591 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
592 maxcheckbox
< size
.cx
)
593 maxcheckbox
= size
.cx
;
594 winheight
+= height
+ (height
/ 2);
599 winheight
+= height
+ height
* 7 / 4; /* OK / Cancel buttons */
602 col1r
= col1l
+ maxlabel
;
603 col2l
= col1r
+ 2*width
;
604 col2r
= col2l
+ 30*width
;
605 if (col2r
< col1l
+2*height
+maxcheckbox
)
606 col2r
= col1l
+2*height
+maxcheckbox
;
607 winwidth
= col2r
+ 2*width
;
609 ReleaseDC(fe
->hwnd
, hdc
);
612 * Create the dialog, now that we know its size.
619 r
.bottom
= winheight
;
621 AdjustWindowRectEx(&r
, (WS_OVERLAPPEDWINDOW
/*|
622 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
623 WS_CAPTION | WS_SYSMENU*/) &~
624 (WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
628 * Centre the dialog on its parent window.
632 GetWindowRect(fe
->hwnd
, &r2
);
633 r
.left
= (r2
.left
+ r2
.right
- r
.right
) / 2;
634 r
.top
= (r2
.top
+ r2
.bottom
- r
.bottom
) / 2;
638 fe
->cfgbox
= CreateWindowEx(0, wc
.lpszClassName
, title
,
639 DS_MODALFRAME
| WS_POPUP
| WS_VISIBLE
|
640 WS_CAPTION
| WS_SYSMENU
,
642 r
.right
-r
.left
, r
.bottom
-r
.top
,
643 fe
->hwnd
, NULL
, fe
->inst
, NULL
);
647 SendMessage(fe
->cfgbox
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, FALSE
);
649 SetWindowLong(fe
->cfgbox
, GWL_USERDATA
, (LONG
)fe
);
650 SetWindowLong(fe
->cfgbox
, DWL_DLGPROC
, (LONG
)ConfigDlgProc
);
653 * Count the controls so we can allocate cfgaux.
655 for (nctrls
= 0, i
= fe
->cfg
; i
->type
!= C_END
; i
++)
657 fe
->cfgaux
= snewn(nctrls
, struct cfg_aux
);
661 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
665 * Edit box with a label beside it.
667 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
668 "Static", 0, 0, i
->name
, id
++);
669 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*3/2,
670 "EDIT", WS_TABSTOP
| ES_AUTOHSCROLL
,
671 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
672 SetWindowText(ctl
, i
->sval
);
680 mkctrl(fe
, col1l
, col2r
, y
, y
+height
, "BUTTON",
681 BS_NOTIFY
| BS_AUTOCHECKBOX
| WS_TABSTOP
,
682 0, i
->name
, (j
->ctlid
= id
++));
688 * Drop-down list with a label beside it.
690 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
691 "STATIC", 0, 0, i
->name
, id
++);
692 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*41/2,
693 "COMBOBOX", WS_TABSTOP
|
694 CBS_DROPDOWNLIST
| CBS_HASSTRINGS
,
695 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
697 char c
, *p
, *q
, *str
;
699 SendMessage(ctl
, CB_RESETCONTENT
, 0, 0);
704 while (*q
&& *q
!= c
) q
++;
705 str
= snewn(q
-p
+1, char);
706 strncpy(str
, p
, q
-p
);
708 SendMessage(ctl
, CB_ADDSTRING
, 0, (LPARAM
)str
);
715 SendMessage(ctl
, CB_SETCURSEL
, i
->ival
, 0);
721 assert(y
< winheight
);
725 y
+= height
/2; /* extra space before OK and Cancel */
726 mkctrl(fe
, col1l
, (col1l
+col2r
)/2-width
, y
, y
+height
*7/4, "BUTTON",
727 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
| BS_DEFPUSHBUTTON
, 0,
729 mkctrl(fe
, (col1l
+col2r
)/2+width
, col2r
, y
, y
+height
*7/4, "BUTTON",
730 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
, 0, "Cancel", IDCANCEL
);
732 SendMessage(fe
->cfgbox
, WM_INITDIALOG
, 0, 0);
734 EnableWindow(fe
->hwnd
, FALSE
);
735 ShowWindow(fe
->cfgbox
, SW_NORMAL
);
736 while ((gm
=GetMessage(&msg
, NULL
, 0, 0)) > 0) {
737 if (!IsDialogMessage(fe
->cfgbox
, &msg
))
738 DispatchMessage(&msg
);
742 EnableWindow(fe
->hwnd
, TRUE
);
743 SetForegroundWindow(fe
->hwnd
);
744 DestroyWindow(fe
->cfgbox
);
745 DeleteObject(fe
->cfgfont
);
750 return (fe
->cfg_done
== 2);
753 static void new_game_type(frontend
*fe
)
759 midend_new_game(fe
->me
);
760 midend_size(fe
->me
, &x
, &y
);
765 AdjustWindowRectEx(&r
, WS_OVERLAPPEDWINDOW
&~
766 (WS_THICKFRAME
| WS_MAXIMIZEBOX
|
770 if (fe
->statusbar
!= NULL
) {
771 GetWindowRect(fe
->statusbar
, &sr
);
773 sr
.left
= sr
.right
= sr
.top
= sr
.bottom
= 0;
775 SetWindowPos(fe
->hwnd
, NULL
, 0, 0,
777 r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
778 SWP_NOMOVE
| SWP_NOZORDER
);
779 if (fe
->statusbar
!= NULL
)
780 SetWindowPos(fe
->statusbar
, NULL
, 0, y
, x
,
781 sr
.bottom
- sr
.top
, SWP_NOZORDER
);
783 DeleteObject(fe
->bitmap
);
785 hdc
= GetDC(fe
->hwnd
);
786 fe
->bitmap
= CreateCompatibleBitmap(hdc
, x
, y
);
787 ReleaseDC(fe
->hwnd
, hdc
);
789 midend_redraw(fe
->me
);
792 static LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
,
793 WPARAM wParam
, LPARAM lParam
)
795 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
802 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
804 if (!midend_process_key(fe
->me
, 0, 0, 'n'))
808 if (!midend_process_key(fe
->me
, 0, 0, 'r'))
812 if (!midend_process_key(fe
->me
, 0, 0, 'u'))
816 if (!midend_process_key(fe
->me
, 0, 0, '\x12'))
820 if (!midend_process_key(fe
->me
, 0, 0, 'q'))
824 if (get_config(fe
, CFG_SETTINGS
))
828 if (get_config(fe
, CFG_SEED
))
833 int p
= ((wParam
&~ 0xF) - IDM_PRESETS
) / 0x10;
835 if (p
>= 0 && p
< fe
->npresets
) {
836 midend_set_params(fe
->me
, fe
->presets
[p
]);
852 hdc
= BeginPaint(hwnd
, &p
);
853 hdc2
= CreateCompatibleDC(hdc
);
854 prevbm
= SelectObject(hdc2
, fe
->bitmap
);
856 p
.rcPaint
.left
, p
.rcPaint
.top
,
857 p
.rcPaint
.right
- p
.rcPaint
.left
,
858 p
.rcPaint
.bottom
- p
.rcPaint
.top
,
860 p
.rcPaint
.left
, p
.rcPaint
.top
,
862 SelectObject(hdc2
, prevbm
);
872 case VK_LEFT
: key
= CURSOR_LEFT
; break;
873 case VK_RIGHT
: key
= CURSOR_RIGHT
; break;
874 case VK_UP
: key
= CURSOR_UP
; break;
875 case VK_DOWN
: key
= CURSOR_DOWN
; break;
877 * Diagonal keys on the numeric keypad.
880 if (!(lParam
& 0x01000000)) key
= CURSOR_UP_RIGHT
;
883 if (!(lParam
& 0x01000000)) key
= CURSOR_DOWN_RIGHT
;
886 if (!(lParam
& 0x01000000)) key
= CURSOR_UP_LEFT
;
889 if (!(lParam
& 0x01000000)) key
= CURSOR_DOWN_LEFT
;
892 * Numeric keypad keys with Num Lock on.
894 case VK_NUMPAD4
: key
= CURSOR_LEFT
; break;
895 case VK_NUMPAD6
: key
= CURSOR_RIGHT
; break;
896 case VK_NUMPAD8
: key
= CURSOR_UP
; break;
897 case VK_NUMPAD2
: key
= CURSOR_DOWN
; break;
898 case VK_NUMPAD9
: key
= CURSOR_UP_RIGHT
; break;
899 case VK_NUMPAD3
: key
= CURSOR_DOWN_RIGHT
; break;
900 case VK_NUMPAD7
: key
= CURSOR_UP_LEFT
; break;
901 case VK_NUMPAD1
: key
= CURSOR_DOWN_LEFT
; break;
905 if (!midend_process_key(fe
->me
, 0, 0, key
))
910 m
.message
= WM_KEYDOWN
;
912 m
.lParam
= lParam
& 0xdfff;
913 TranslateMessage(&m
);
924 * Shift-clicks count as middle-clicks, since otherwise
925 * two-button Windows users won't have any kind of
926 * middle click to use.
928 if (message
== WM_MBUTTONDOWN
|| (wParam
& MK_SHIFT
))
929 button
= MIDDLE_BUTTON
;
930 else if (message
== WM_LBUTTONDOWN
)
931 button
= LEFT_BUTTON
;
933 button
= RIGHT_BUTTON
;
935 if (!midend_process_key(fe
->me
, LOWORD(lParam
),
936 HIWORD(lParam
), button
))
941 if (!midend_process_key(fe
->me
, 0, 0, (unsigned char)wParam
))
946 midend_timer(fe
->me
, (float)0.02);
950 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
953 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
)
957 InitCommonControls();
963 wndclass
.lpfnWndProc
= WndProc
;
964 wndclass
.cbClsExtra
= 0;
965 wndclass
.cbWndExtra
= 0;
966 wndclass
.hInstance
= inst
;
967 wndclass
.hIcon
= LoadIcon(inst
, IDI_APPLICATION
);
968 wndclass
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
969 wndclass
.hbrBackground
= NULL
;
970 wndclass
.lpszMenuName
= NULL
;
971 wndclass
.lpszClassName
= game_name
;
973 RegisterClass(&wndclass
);
978 while (GetMessage(&msg
, NULL
, 0, 0)) {
979 DispatchMessage(&msg
);