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
)
158 fe
->clip
= CreateRectRgn(0, 0, 1, 1);
159 GetClipRgn(fe
->hdc_bm
, fe
->clip
);
162 IntersectClipRect(fe
->hdc_bm
, x
, y
, x
+w
, y
+h
);
165 void unclip(frontend
*fe
)
168 SelectClipRgn(fe
->hdc_bm
, fe
->clip
);
171 void draw_text(frontend
*fe
, int x
, int y
, int fonttype
, int fontsize
,
172 int align
, int colour
, char *text
)
177 * Find or create the font.
179 for (i
= 0; i
< fe
->nfonts
; i
++)
180 if (fe
->fonts
[i
].type
== fonttype
&& fe
->fonts
[i
].size
== fontsize
)
183 if (i
== fe
->nfonts
) {
184 if (fe
->fontsize
<= fe
->nfonts
) {
185 fe
->fontsize
= fe
->nfonts
+ 10;
186 fe
->fonts
= sresize(fe
->fonts
, fe
->fontsize
, struct font
);
191 fe
->fonts
[i
].type
= fonttype
;
192 fe
->fonts
[i
].size
= fontsize
;
195 * FIXME: Really I should make at least _some_ effort to
196 * pick the correct font.
198 fe
->fonts
[i
].font
= CreateFont(-fontsize
, 0, 0, 0, 0,
199 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
200 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
202 (fonttype
== FONT_FIXED ?
203 FIXED_PITCH
| FF_DONTCARE
:
204 VARIABLE_PITCH
| FF_SWISS
),
209 * Position and draw the text.
216 oldfont
= SelectObject(fe
->hdc_bm
, fe
->fonts
[i
].font
);
217 if (GetTextMetrics(fe
->hdc_bm
, &tm
)) {
218 if (align
& ALIGN_VCENTRE
)
219 y
-= (tm
.tmAscent
+tm
.tmDescent
)/2;
223 if (GetTextExtentPoint32(fe
->hdc_bm
, text
, strlen(text
), &size
)) {
224 if (align
& ALIGN_HCENTRE
)
226 else if (align
& ALIGN_HRIGHT
)
229 SetBkMode(fe
->hdc_bm
, TRANSPARENT
);
230 TextOut(fe
->hdc_bm
, x
, y
, text
, strlen(text
));
231 SelectObject(fe
->hdc_bm
, oldfont
);
235 void draw_rect(frontend
*fe
, int x
, int y
, int w
, int h
, int colour
)
237 if (w
== 1 && h
== 1) {
239 * Rectangle() appears to get uppity if asked to draw a 1x1
240 * rectangle, presumably on the grounds that that's beneath
241 * its dignity and you ought to be using SetPixel instead.
244 SetPixel(fe
->hdc_bm
, x
, y
, fe
->colours
[colour
]);
246 HBRUSH oldbrush
= SelectObject(fe
->hdc_bm
, fe
->brushes
[colour
]);
247 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
248 Rectangle(fe
->hdc_bm
, x
, y
, x
+w
, y
+h
);
249 SelectObject(fe
->hdc_bm
, oldbrush
);
250 SelectObject(fe
->hdc_bm
, oldpen
);
254 void draw_line(frontend
*fe
, int x1
, int y1
, int x2
, int y2
, int colour
)
256 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
257 MoveToEx(fe
->hdc_bm
, x1
, y1
, NULL
);
258 LineTo(fe
->hdc_bm
, x2
, y2
);
259 SetPixel(fe
->hdc_bm
, x2
, y2
, fe
->colours
[colour
]);
260 SelectObject(fe
->hdc_bm
, oldpen
);
263 void draw_polygon(frontend
*fe
, int *coords
, int npoints
,
264 int fill
, int colour
)
266 POINT
*pts
= snewn(npoints
+1, POINT
);
269 for (i
= 0; i
<= npoints
; i
++) {
270 int j
= (i
< npoints ? i
: 0);
271 pts
[i
].x
= coords
[j
*2];
272 pts
[i
].y
= coords
[j
*2+1];
276 HBRUSH oldbrush
= SelectObject(fe
->hdc_bm
, fe
->brushes
[colour
]);
277 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
278 Polygon(fe
->hdc_bm
, pts
, npoints
);
279 SelectObject(fe
->hdc_bm
, oldbrush
);
280 SelectObject(fe
->hdc_bm
, oldpen
);
282 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
283 Polyline(fe
->hdc_bm
, pts
, npoints
+1);
284 SelectObject(fe
->hdc_bm
, oldpen
);
290 void start_draw(frontend
*fe
)
293 hdc_win
= GetDC(fe
->hwnd
);
294 fe
->hdc_bm
= CreateCompatibleDC(hdc_win
);
295 fe
->prevbm
= SelectObject(fe
->hdc_bm
, fe
->bitmap
);
296 ReleaseDC(fe
->hwnd
, hdc_win
);
298 SetMapMode(fe
->hdc_bm
, MM_TEXT
);
301 void draw_update(frontend
*fe
, int x
, int y
, int w
, int h
)
310 InvalidateRect(fe
->hwnd
, &r
, FALSE
);
313 void end_draw(frontend
*fe
)
315 SelectObject(fe
->hdc_bm
, fe
->prevbm
);
316 DeleteDC(fe
->hdc_bm
);
318 DeleteObject(fe
->clip
);
323 void deactivate_timer(frontend
*fe
)
325 KillTimer(fe
->hwnd
, fe
->timer
);
329 void activate_timer(frontend
*fe
)
332 fe
->timer
= SetTimer(fe
->hwnd
, fe
->timer
, 20, NULL
);
333 fe
->timer_last_tickcount
= GetTickCount();
338 * See if we can find a help file.
340 static void find_help_file(frontend
*fe
)
342 char b
[2048], *p
, *q
, *r
;
344 if (!fe
->help_path
) {
345 GetModuleFileName(NULL
, b
, sizeof(b
) - 1);
347 p
= strrchr(b
, '\\');
348 if (p
&& p
>= r
) r
= p
+1;
350 if (q
&& q
>= r
) r
= q
+1;
351 strcpy(r
, HELP_FILE_NAME
);
352 if ( (fp
= fopen(b
, "r")) != NULL
) {
353 fe
->help_path
= dupstr(b
);
356 fe
->help_path
= NULL
;
357 strcpy(r
, HELP_CNT_NAME
);
358 if ( (fp
= fopen(b
, "r")) != NULL
) {
359 fe
->help_has_contents
= TRUE
;
362 fe
->help_has_contents
= FALSE
;
366 static frontend
*new_window(HINSTANCE inst
, char *game_id
, char **error
)
375 fe
->me
= midend_new(fe
, &thegame
);
378 *error
= midend_game_id(fe
->me
, game_id
, FALSE
);
386 fe
->help_path
= NULL
;
390 midend_new_game(fe
->me
);
391 midend_size(fe
->me
, &x
, &y
);
396 fe
->nfonts
= fe
->fontsize
= 0;
402 colours
= midend_colours(fe
->me
, &ncolours
);
404 fe
->colours
= snewn(ncolours
, COLORREF
);
405 fe
->brushes
= snewn(ncolours
, HBRUSH
);
406 fe
->pens
= snewn(ncolours
, HPEN
);
408 for (i
= 0; i
< ncolours
; i
++) {
409 fe
->colours
[i
] = RGB(255 * colours
[i
*3+0],
410 255 * colours
[i
*3+1],
411 255 * colours
[i
*3+2]);
412 fe
->brushes
[i
] = CreateSolidBrush(fe
->colours
[i
]);
413 fe
->pens
[i
] = CreatePen(PS_SOLID
, 1, fe
->colours
[i
]);
420 AdjustWindowRectEx(&r
, WS_OVERLAPPEDWINDOW
&~
421 (WS_THICKFRAME
| WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
424 fe
->hwnd
= CreateWindowEx(0, thegame
.name
, thegame
.name
,
425 WS_OVERLAPPEDWINDOW
&~
426 (WS_THICKFRAME
| WS_MAXIMIZEBOX
),
427 CW_USEDEFAULT
, CW_USEDEFAULT
,
428 r
.right
- r
.left
, r
.bottom
- r
.top
,
429 NULL
, NULL
, inst
, NULL
);
432 HMENU bar
= CreateMenu();
433 HMENU menu
= CreateMenu();
435 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)menu
, "Game");
436 AppendMenu(menu
, MF_ENABLED
, IDM_NEW
, "New");
437 AppendMenu(menu
, MF_ENABLED
, IDM_RESTART
, "Restart");
438 AppendMenu(menu
, MF_ENABLED
, IDM_SEED
, "Specific...");
440 if ((fe
->npresets
= midend_num_presets(fe
->me
)) > 0 ||
441 thegame
.can_configure
) {
442 HMENU sub
= CreateMenu();
445 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)sub
, "Type");
447 fe
->presets
= snewn(fe
->npresets
, game_params
*);
449 for (i
= 0; i
< fe
->npresets
; i
++) {
452 midend_fetch_preset(fe
->me
, i
, &name
, &fe
->presets
[i
]);
455 * FIXME: we ought to go through and do something
456 * with ampersands here.
459 AppendMenu(sub
, MF_ENABLED
, IDM_PRESETS
+ 0x10 * i
, name
);
462 if (thegame
.can_configure
) {
463 AppendMenu(sub
, MF_ENABLED
, IDM_CONFIG
, "Custom...");
467 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
468 AppendMenu(menu
, MF_ENABLED
, IDM_UNDO
, "Undo");
469 AppendMenu(menu
, MF_ENABLED
, IDM_REDO
, "Redo");
470 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
471 AppendMenu(menu
, MF_ENABLED
, IDM_QUIT
, "Exit");
473 HMENU hmenu
= CreateMenu();
474 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)hmenu
, "Help");
475 AppendMenu(hmenu
, MF_ENABLED
, IDM_HELPC
, "Contents");
476 if (thegame
.winhelp_topic
) {
478 assert(thegame
.name
);
479 item
= snewn(9+strlen(thegame
.name
), char); /*ick*/
480 sprintf(item
, "Help on %s", thegame
.name
);
481 AppendMenu(hmenu
, MF_ENABLED
, IDM_GAMEHELP
, item
);
485 SetMenu(fe
->hwnd
, bar
);
488 if (midend_wants_statusbar(fe
->me
)) {
489 fe
->statusbar
= CreateWindowEx(0, STATUSCLASSNAME
, "ooh",
490 WS_CHILD
| WS_VISIBLE
,
491 0, 0, 0, 0, /* status bar does these */
492 fe
->hwnd
, NULL
, inst
, NULL
);
493 GetWindowRect(fe
->statusbar
, &sr
);
494 SetWindowPos(fe
->hwnd
, NULL
, 0, 0,
495 r
.right
- r
.left
, r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
496 SWP_NOMOVE
| SWP_NOZORDER
);
497 SetWindowPos(fe
->statusbar
, NULL
, 0, y
, x
, sr
.bottom
- sr
.top
,
500 fe
->statusbar
= NULL
;
503 hdc
= GetDC(fe
->hwnd
);
504 fe
->bitmap
= CreateCompatibleBitmap(hdc
, x
, y
);
505 ReleaseDC(fe
->hwnd
, hdc
);
507 SetWindowLong(fe
->hwnd
, GWL_USERDATA
, (LONG
)fe
);
509 ShowWindow(fe
->hwnd
, SW_NORMAL
);
510 SetForegroundWindow(fe
->hwnd
);
512 midend_redraw(fe
->me
);
517 static int CALLBACK
ConfigDlgProc(HWND hwnd
, UINT msg
,
518 WPARAM wParam
, LPARAM lParam
)
520 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
530 * OK and Cancel are special cases.
532 if ((HIWORD(wParam
) == BN_CLICKED
||
533 HIWORD(wParam
) == BN_DOUBLECLICKED
) &&
534 (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)) {
535 if (LOWORD(wParam
) == IDOK
) {
536 char *err
= midend_set_config(fe
->me
, fe
->cfg_which
, fe
->cfg
);
539 MessageBox(hwnd
, err
, "Validation error",
540 MB_ICONERROR
| MB_OK
);
551 * First find the control whose id this is.
553 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
554 if (j
->ctlid
== LOWORD(wParam
))
557 if (i
->type
== C_END
)
558 return 0; /* not our problem */
560 if (i
->type
== C_STRING
&& HIWORD(wParam
) == EN_CHANGE
) {
562 GetDlgItemText(fe
->cfgbox
, j
->ctlid
, buffer
, lenof(buffer
));
563 buffer
[lenof(buffer
)-1] = '\0';
565 i
->sval
= dupstr(buffer
);
566 } else if (i
->type
== C_BOOLEAN
&&
567 (HIWORD(wParam
) == BN_CLICKED
||
568 HIWORD(wParam
) == BN_DOUBLECLICKED
)) {
569 i
->ival
= IsDlgButtonChecked(fe
->cfgbox
, j
->ctlid
);
570 } else if (i
->type
== C_CHOICES
&&
571 HIWORD(wParam
) == CBN_SELCHANGE
) {
572 i
->ival
= SendDlgItemMessage(fe
->cfgbox
, j
->ctlid
,
586 HWND
mkctrl(frontend
*fe
, int x1
, int x2
, int y1
, int y2
,
587 char *wclass
, int wstyle
,
588 int exstyle
, char *wtext
, int wid
)
591 ret
= CreateWindowEx(exstyle
, wclass
, wtext
,
592 wstyle
| WS_CHILD
| WS_VISIBLE
, x1
, y1
, x2
-x1
, y2
-y1
,
593 fe
->cfgbox
, (HMENU
) wid
, fe
->inst
, NULL
);
594 SendMessage(ret
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, MAKELPARAM(TRUE
, 0));
598 static int get_config(frontend
*fe
, int which
)
611 int winwidth
, winheight
, col1l
, col1r
, col2l
, col2r
, y
;
612 int height
, width
, maxlabel
, maxcheckbox
;
614 wc
.style
= CS_DBLCLKS
| CS_SAVEBITS
| CS_BYTEALIGNWINDOW
;
615 wc
.lpfnWndProc
= DefDlgProc
;
617 wc
.cbWndExtra
= DLGWINDOWEXTRA
+ 8;
618 wc
.hInstance
= fe
->inst
;
620 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
621 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+1);
622 wc
.lpszMenuName
= NULL
;
623 wc
.lpszClassName
= "GameConfigBox";
626 hdc
= GetDC(fe
->hwnd
);
627 SetMapMode(hdc
, MM_TEXT
);
629 fe
->cfg_done
= FALSE
;
631 fe
->cfgfont
= CreateFont(-MulDiv(8, GetDeviceCaps(hdc
, LOGPIXELSY
), 72),
633 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
634 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
639 oldfont
= SelectObject(hdc
, fe
->cfgfont
);
640 if (GetTextMetrics(hdc
, &tm
)) {
641 height
= tm
.tmAscent
+ tm
.tmDescent
;
642 width
= tm
.tmAveCharWidth
;
647 fe
->cfg
= midend_get_config(fe
->me
, which
, &title
);
648 fe
->cfg_which
= which
;
651 * Figure out the layout of the config box by measuring the
652 * length of each piece of text.
654 maxlabel
= maxcheckbox
= 0;
655 winheight
= height
/2;
657 for (i
= fe
->cfg
; i
->type
!= C_END
; i
++) {
662 * Both these control types have a label filling only
663 * the left-hand column of the box.
665 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
668 winheight
+= height
* 3 / 2 + (height
/ 2);
673 * Checkboxes take up the whole of the box width.
675 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
676 maxcheckbox
< size
.cx
)
677 maxcheckbox
= size
.cx
;
678 winheight
+= height
+ (height
/ 2);
683 winheight
+= height
+ height
* 7 / 4; /* OK / Cancel buttons */
686 col1r
= col1l
+ maxlabel
;
687 col2l
= col1r
+ 2*width
;
688 col2r
= col2l
+ 30*width
;
689 if (col2r
< col1l
+2*height
+maxcheckbox
)
690 col2r
= col1l
+2*height
+maxcheckbox
;
691 winwidth
= col2r
+ 2*width
;
693 ReleaseDC(fe
->hwnd
, hdc
);
696 * Create the dialog, now that we know its size.
703 r
.bottom
= winheight
;
705 AdjustWindowRectEx(&r
, (WS_OVERLAPPEDWINDOW
/*|
706 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
707 WS_CAPTION | WS_SYSMENU*/) &~
708 (WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
712 * Centre the dialog on its parent window.
716 GetWindowRect(fe
->hwnd
, &r2
);
717 r
.left
= (r2
.left
+ r2
.right
- r
.right
) / 2;
718 r
.top
= (r2
.top
+ r2
.bottom
- r
.bottom
) / 2;
722 fe
->cfgbox
= CreateWindowEx(0, wc
.lpszClassName
, title
,
723 DS_MODALFRAME
| WS_POPUP
| WS_VISIBLE
|
724 WS_CAPTION
| WS_SYSMENU
,
726 r
.right
-r
.left
, r
.bottom
-r
.top
,
727 fe
->hwnd
, NULL
, fe
->inst
, NULL
);
731 SendMessage(fe
->cfgbox
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, FALSE
);
733 SetWindowLong(fe
->cfgbox
, GWL_USERDATA
, (LONG
)fe
);
734 SetWindowLong(fe
->cfgbox
, DWL_DLGPROC
, (LONG
)ConfigDlgProc
);
737 * Count the controls so we can allocate cfgaux.
739 for (nctrls
= 0, i
= fe
->cfg
; i
->type
!= C_END
; i
++)
741 fe
->cfgaux
= snewn(nctrls
, struct cfg_aux
);
745 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
749 * Edit box with a label beside it.
751 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
752 "Static", 0, 0, i
->name
, id
++);
753 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*3/2,
754 "EDIT", WS_TABSTOP
| ES_AUTOHSCROLL
,
755 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
756 SetWindowText(ctl
, i
->sval
);
764 mkctrl(fe
, col1l
, col2r
, y
, y
+height
, "BUTTON",
765 BS_NOTIFY
| BS_AUTOCHECKBOX
| WS_TABSTOP
,
766 0, i
->name
, (j
->ctlid
= id
++));
767 CheckDlgButton(fe
->cfgbox
, j
->ctlid
, (i
->ival
!= 0));
773 * Drop-down list with a label beside it.
775 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
776 "STATIC", 0, 0, i
->name
, id
++);
777 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*41/2,
778 "COMBOBOX", WS_TABSTOP
|
779 CBS_DROPDOWNLIST
| CBS_HASSTRINGS
,
780 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
782 char c
, *p
, *q
, *str
;
784 SendMessage(ctl
, CB_RESETCONTENT
, 0, 0);
789 while (*q
&& *q
!= c
) q
++;
790 str
= snewn(q
-p
+1, char);
791 strncpy(str
, p
, q
-p
);
793 SendMessage(ctl
, CB_ADDSTRING
, 0, (LPARAM
)str
);
800 SendMessage(ctl
, CB_SETCURSEL
, i
->ival
, 0);
806 assert(y
< winheight
);
810 y
+= height
/2; /* extra space before OK and Cancel */
811 mkctrl(fe
, col1l
, (col1l
+col2r
)/2-width
, y
, y
+height
*7/4, "BUTTON",
812 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
| BS_DEFPUSHBUTTON
, 0,
814 mkctrl(fe
, (col1l
+col2r
)/2+width
, col2r
, y
, y
+height
*7/4, "BUTTON",
815 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
, 0, "Cancel", IDCANCEL
);
817 SendMessage(fe
->cfgbox
, WM_INITDIALOG
, 0, 0);
819 EnableWindow(fe
->hwnd
, FALSE
);
820 ShowWindow(fe
->cfgbox
, SW_NORMAL
);
821 while ((gm
=GetMessage(&msg
, NULL
, 0, 0)) > 0) {
822 if (!IsDialogMessage(fe
->cfgbox
, &msg
))
823 DispatchMessage(&msg
);
827 EnableWindow(fe
->hwnd
, TRUE
);
828 SetForegroundWindow(fe
->hwnd
);
829 DestroyWindow(fe
->cfgbox
);
830 DeleteObject(fe
->cfgfont
);
835 return (fe
->cfg_done
== 2);
838 static void new_game_type(frontend
*fe
)
844 midend_new_game(fe
->me
);
845 midend_size(fe
->me
, &x
, &y
);
850 AdjustWindowRectEx(&r
, WS_OVERLAPPEDWINDOW
&~
851 (WS_THICKFRAME
| WS_MAXIMIZEBOX
|
855 if (fe
->statusbar
!= NULL
) {
856 GetWindowRect(fe
->statusbar
, &sr
);
858 sr
.left
= sr
.right
= sr
.top
= sr
.bottom
= 0;
860 SetWindowPos(fe
->hwnd
, NULL
, 0, 0,
862 r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
863 SWP_NOMOVE
| SWP_NOZORDER
);
864 if (fe
->statusbar
!= NULL
)
865 SetWindowPos(fe
->statusbar
, NULL
, 0, y
, x
,
866 sr
.bottom
- sr
.top
, SWP_NOZORDER
);
868 DeleteObject(fe
->bitmap
);
870 hdc
= GetDC(fe
->hwnd
);
871 fe
->bitmap
= CreateCompatibleBitmap(hdc
, x
, y
);
872 ReleaseDC(fe
->hwnd
, hdc
);
874 midend_redraw(fe
->me
);
877 static LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
,
878 WPARAM wParam
, LPARAM lParam
)
880 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
887 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
889 if (!midend_process_key(fe
->me
, 0, 0, 'n'))
893 if (!midend_process_key(fe
->me
, 0, 0, 'r'))
897 if (!midend_process_key(fe
->me
, 0, 0, 'u'))
901 if (!midend_process_key(fe
->me
, 0, 0, '\x12'))
905 if (!midend_process_key(fe
->me
, 0, 0, 'q'))
909 if (get_config(fe
, CFG_SETTINGS
))
913 if (get_config(fe
, CFG_SEED
))
917 assert(fe
->help_path
);
918 WinHelp(hwnd
, fe
->help_path
,
919 fe
->help_has_contents ? HELP_FINDER
: HELP_CONTENTS
, 0);
922 assert(fe
->help_path
);
923 assert(thegame
.winhelp_topic
);
925 char *cmd
= snewn(10+strlen(thegame
.winhelp_topic
), char);
926 sprintf(cmd
, "JI(`',`%s')", thegame
.winhelp_topic
);
927 WinHelp(hwnd
, fe
->help_path
, HELP_COMMAND
, (DWORD
)cmd
);
933 int p
= ((wParam
&~ 0xF) - IDM_PRESETS
) / 0x10;
935 if (p
>= 0 && p
< fe
->npresets
) {
936 midend_set_params(fe
->me
, fe
->presets
[p
]);
952 hdc
= BeginPaint(hwnd
, &p
);
953 hdc2
= CreateCompatibleDC(hdc
);
954 prevbm
= SelectObject(hdc2
, fe
->bitmap
);
956 p
.rcPaint
.left
, p
.rcPaint
.top
,
957 p
.rcPaint
.right
- p
.rcPaint
.left
,
958 p
.rcPaint
.bottom
- p
.rcPaint
.top
,
960 p
.rcPaint
.left
, p
.rcPaint
.top
,
962 SelectObject(hdc2
, prevbm
);
972 case VK_LEFT
: key
= CURSOR_LEFT
; break;
973 case VK_RIGHT
: key
= CURSOR_RIGHT
; break;
974 case VK_UP
: key
= CURSOR_UP
; break;
975 case VK_DOWN
: key
= CURSOR_DOWN
; break;
977 * Diagonal keys on the numeric keypad.
980 if (!(lParam
& 0x01000000)) key
= CURSOR_UP_RIGHT
;
983 if (!(lParam
& 0x01000000)) key
= CURSOR_DOWN_RIGHT
;
986 if (!(lParam
& 0x01000000)) key
= CURSOR_UP_LEFT
;
989 if (!(lParam
& 0x01000000)) key
= CURSOR_DOWN_LEFT
;
992 * Numeric keypad keys with Num Lock on.
994 case VK_NUMPAD4
: key
= CURSOR_LEFT
; break;
995 case VK_NUMPAD6
: key
= CURSOR_RIGHT
; break;
996 case VK_NUMPAD8
: key
= CURSOR_UP
; break;
997 case VK_NUMPAD2
: key
= CURSOR_DOWN
; break;
998 case VK_NUMPAD9
: key
= CURSOR_UP_RIGHT
; break;
999 case VK_NUMPAD3
: key
= CURSOR_DOWN_RIGHT
; break;
1000 case VK_NUMPAD7
: key
= CURSOR_UP_LEFT
; break;
1001 case VK_NUMPAD1
: key
= CURSOR_DOWN_LEFT
; break;
1005 if (!midend_process_key(fe
->me
, 0, 0, key
))
1010 m
.message
= WM_KEYDOWN
;
1012 m
.lParam
= lParam
& 0xdfff;
1013 TranslateMessage(&m
);
1017 case WM_LBUTTONDOWN
:
1018 case WM_RBUTTONDOWN
:
1019 case WM_MBUTTONDOWN
:
1024 * Shift-clicks count as middle-clicks, since otherwise
1025 * two-button Windows users won't have any kind of
1026 * middle click to use.
1028 if (message
== WM_MBUTTONDOWN
|| (wParam
& MK_SHIFT
))
1029 button
= MIDDLE_BUTTON
;
1030 else if (message
== WM_LBUTTONDOWN
)
1031 button
= LEFT_BUTTON
;
1033 button
= RIGHT_BUTTON
;
1035 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1036 (signed short)HIWORD(lParam
), button
))
1049 * Shift-clicks count as middle-clicks, since otherwise
1050 * two-button Windows users won't have any kind of
1051 * middle click to use.
1053 if (message
== WM_MBUTTONUP
|| (wParam
& MK_SHIFT
))
1054 button
= MIDDLE_RELEASE
;
1055 else if (message
== WM_LBUTTONUP
)
1056 button
= LEFT_RELEASE
;
1058 button
= RIGHT_RELEASE
;
1060 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1061 (signed short)HIWORD(lParam
), button
))
1071 if (wParam
& (MK_MBUTTON
| MK_SHIFT
))
1072 button
= MIDDLE_DRAG
;
1073 else if (wParam
& MK_LBUTTON
)
1076 button
= RIGHT_DRAG
;
1078 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1079 (signed short)HIWORD(lParam
), button
))
1084 if (!midend_process_key(fe
->me
, 0, 0, (unsigned char)wParam
))
1089 DWORD now
= GetTickCount();
1090 float elapsed
= (float) (now
- fe
->timer_last_tickcount
) * 0.001F
;
1091 midend_timer(fe
->me
, elapsed
);
1092 fe
->timer_last_tickcount
= now
;
1097 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
1100 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
)
1105 InitCommonControls();
1111 wndclass
.lpfnWndProc
= WndProc
;
1112 wndclass
.cbClsExtra
= 0;
1113 wndclass
.cbWndExtra
= 0;
1114 wndclass
.hInstance
= inst
;
1115 wndclass
.hIcon
= LoadIcon(inst
, IDI_APPLICATION
);
1116 wndclass
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
1117 wndclass
.hbrBackground
= NULL
;
1118 wndclass
.lpszMenuName
= NULL
;
1119 wndclass
.lpszClassName
= thegame
.name
;
1121 RegisterClass(&wndclass
);
1124 while (*cmdline
&& isspace(*cmdline
))
1127 if (!new_window(inst
, *cmdline ? cmdline
: NULL
, &error
)) {
1129 sprintf(buf
, "%.100s Error", thegame
.name
);
1130 MessageBox(NULL
, error
, buf
, MB_OK
|MB_ICONERROR
);
1134 while (GetMessage(&msg
, NULL
, 0, 0)) {
1135 DispatchMessage(&msg
);