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 SetTextColor(fe
->hdc_bm
, fe
->colours
[colour
]);
231 TextOut(fe
->hdc_bm
, x
, y
, text
, strlen(text
));
232 SelectObject(fe
->hdc_bm
, oldfont
);
236 void draw_rect(frontend
*fe
, int x
, int y
, int w
, int h
, int colour
)
238 if (w
== 1 && h
== 1) {
240 * Rectangle() appears to get uppity if asked to draw a 1x1
241 * rectangle, presumably on the grounds that that's beneath
242 * its dignity and you ought to be using SetPixel instead.
245 SetPixel(fe
->hdc_bm
, x
, y
, fe
->colours
[colour
]);
247 HBRUSH oldbrush
= SelectObject(fe
->hdc_bm
, fe
->brushes
[colour
]);
248 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
249 Rectangle(fe
->hdc_bm
, x
, y
, x
+w
, y
+h
);
250 SelectObject(fe
->hdc_bm
, oldbrush
);
251 SelectObject(fe
->hdc_bm
, oldpen
);
255 void draw_line(frontend
*fe
, int x1
, int y1
, int x2
, int y2
, int colour
)
257 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
258 MoveToEx(fe
->hdc_bm
, x1
, y1
, NULL
);
259 LineTo(fe
->hdc_bm
, x2
, y2
);
260 SetPixel(fe
->hdc_bm
, x2
, y2
, fe
->colours
[colour
]);
261 SelectObject(fe
->hdc_bm
, oldpen
);
264 void draw_polygon(frontend
*fe
, int *coords
, int npoints
,
265 int fill
, int colour
)
267 POINT
*pts
= snewn(npoints
+1, POINT
);
270 for (i
= 0; i
<= npoints
; i
++) {
271 int j
= (i
< npoints ? i
: 0);
272 pts
[i
].x
= coords
[j
*2];
273 pts
[i
].y
= coords
[j
*2+1];
277 HBRUSH oldbrush
= SelectObject(fe
->hdc_bm
, fe
->brushes
[colour
]);
278 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
279 Polygon(fe
->hdc_bm
, pts
, npoints
);
280 SelectObject(fe
->hdc_bm
, oldbrush
);
281 SelectObject(fe
->hdc_bm
, oldpen
);
283 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
284 Polyline(fe
->hdc_bm
, pts
, npoints
+1);
285 SelectObject(fe
->hdc_bm
, oldpen
);
291 void start_draw(frontend
*fe
)
294 hdc_win
= GetDC(fe
->hwnd
);
295 fe
->hdc_bm
= CreateCompatibleDC(hdc_win
);
296 fe
->prevbm
= SelectObject(fe
->hdc_bm
, fe
->bitmap
);
297 ReleaseDC(fe
->hwnd
, hdc_win
);
299 SetMapMode(fe
->hdc_bm
, MM_TEXT
);
302 void draw_update(frontend
*fe
, int x
, int y
, int w
, int h
)
311 InvalidateRect(fe
->hwnd
, &r
, FALSE
);
314 void end_draw(frontend
*fe
)
316 SelectObject(fe
->hdc_bm
, fe
->prevbm
);
317 DeleteDC(fe
->hdc_bm
);
319 DeleteObject(fe
->clip
);
324 void deactivate_timer(frontend
*fe
)
326 KillTimer(fe
->hwnd
, fe
->timer
);
330 void activate_timer(frontend
*fe
)
333 fe
->timer
= SetTimer(fe
->hwnd
, fe
->timer
, 20, NULL
);
334 fe
->timer_last_tickcount
= GetTickCount();
339 * See if we can find a help file.
341 static void find_help_file(frontend
*fe
)
343 char b
[2048], *p
, *q
, *r
;
345 if (!fe
->help_path
) {
346 GetModuleFileName(NULL
, b
, sizeof(b
) - 1);
348 p
= strrchr(b
, '\\');
349 if (p
&& p
>= r
) r
= p
+1;
351 if (q
&& q
>= r
) r
= q
+1;
352 strcpy(r
, HELP_FILE_NAME
);
353 if ( (fp
= fopen(b
, "r")) != NULL
) {
354 fe
->help_path
= dupstr(b
);
357 fe
->help_path
= NULL
;
358 strcpy(r
, HELP_CNT_NAME
);
359 if ( (fp
= fopen(b
, "r")) != NULL
) {
360 fe
->help_has_contents
= TRUE
;
363 fe
->help_has_contents
= FALSE
;
367 static frontend
*new_window(HINSTANCE inst
, char *game_id
, char **error
)
376 fe
->me
= midend_new(fe
, &thegame
);
379 *error
= midend_game_id(fe
->me
, game_id
, FALSE
);
387 fe
->help_path
= NULL
;
391 midend_new_game(fe
->me
);
392 midend_size(fe
->me
, &x
, &y
);
397 fe
->nfonts
= fe
->fontsize
= 0;
403 colours
= midend_colours(fe
->me
, &ncolours
);
405 fe
->colours
= snewn(ncolours
, COLORREF
);
406 fe
->brushes
= snewn(ncolours
, HBRUSH
);
407 fe
->pens
= snewn(ncolours
, HPEN
);
409 for (i
= 0; i
< ncolours
; i
++) {
410 fe
->colours
[i
] = RGB(255 * colours
[i
*3+0],
411 255 * colours
[i
*3+1],
412 255 * colours
[i
*3+2]);
413 fe
->brushes
[i
] = CreateSolidBrush(fe
->colours
[i
]);
414 fe
->pens
[i
] = CreatePen(PS_SOLID
, 1, fe
->colours
[i
]);
421 AdjustWindowRectEx(&r
, WS_OVERLAPPEDWINDOW
&~
422 (WS_THICKFRAME
| WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
425 fe
->hwnd
= CreateWindowEx(0, thegame
.name
, thegame
.name
,
426 WS_OVERLAPPEDWINDOW
&~
427 (WS_THICKFRAME
| WS_MAXIMIZEBOX
),
428 CW_USEDEFAULT
, CW_USEDEFAULT
,
429 r
.right
- r
.left
, r
.bottom
- r
.top
,
430 NULL
, NULL
, inst
, NULL
);
433 HMENU bar
= CreateMenu();
434 HMENU menu
= CreateMenu();
436 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)menu
, "Game");
437 AppendMenu(menu
, MF_ENABLED
, IDM_NEW
, "New");
438 AppendMenu(menu
, MF_ENABLED
, IDM_RESTART
, "Restart");
439 AppendMenu(menu
, MF_ENABLED
, IDM_SEED
, "Specific...");
441 if ((fe
->npresets
= midend_num_presets(fe
->me
)) > 0 ||
442 thegame
.can_configure
) {
443 HMENU sub
= CreateMenu();
446 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)sub
, "Type");
448 fe
->presets
= snewn(fe
->npresets
, game_params
*);
450 for (i
= 0; i
< fe
->npresets
; i
++) {
453 midend_fetch_preset(fe
->me
, i
, &name
, &fe
->presets
[i
]);
456 * FIXME: we ought to go through and do something
457 * with ampersands here.
460 AppendMenu(sub
, MF_ENABLED
, IDM_PRESETS
+ 0x10 * i
, name
);
463 if (thegame
.can_configure
) {
464 AppendMenu(sub
, MF_ENABLED
, IDM_CONFIG
, "Custom...");
468 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
469 AppendMenu(menu
, MF_ENABLED
, IDM_UNDO
, "Undo");
470 AppendMenu(menu
, MF_ENABLED
, IDM_REDO
, "Redo");
471 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
472 AppendMenu(menu
, MF_ENABLED
, IDM_QUIT
, "Exit");
474 HMENU hmenu
= CreateMenu();
475 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)hmenu
, "Help");
476 AppendMenu(hmenu
, MF_ENABLED
, IDM_HELPC
, "Contents");
477 if (thegame
.winhelp_topic
) {
479 assert(thegame
.name
);
480 item
= snewn(9+strlen(thegame
.name
), char); /*ick*/
481 sprintf(item
, "Help on %s", thegame
.name
);
482 AppendMenu(hmenu
, MF_ENABLED
, IDM_GAMEHELP
, item
);
486 SetMenu(fe
->hwnd
, bar
);
489 if (midend_wants_statusbar(fe
->me
)) {
490 fe
->statusbar
= CreateWindowEx(0, STATUSCLASSNAME
, "ooh",
491 WS_CHILD
| WS_VISIBLE
,
492 0, 0, 0, 0, /* status bar does these */
493 fe
->hwnd
, NULL
, inst
, NULL
);
494 GetWindowRect(fe
->statusbar
, &sr
);
495 SetWindowPos(fe
->hwnd
, NULL
, 0, 0,
496 r
.right
- r
.left
, r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
497 SWP_NOMOVE
| SWP_NOZORDER
);
498 SetWindowPos(fe
->statusbar
, NULL
, 0, y
, x
, sr
.bottom
- sr
.top
,
501 fe
->statusbar
= NULL
;
504 hdc
= GetDC(fe
->hwnd
);
505 fe
->bitmap
= CreateCompatibleBitmap(hdc
, x
, y
);
506 ReleaseDC(fe
->hwnd
, hdc
);
508 SetWindowLong(fe
->hwnd
, GWL_USERDATA
, (LONG
)fe
);
510 ShowWindow(fe
->hwnd
, SW_NORMAL
);
511 SetForegroundWindow(fe
->hwnd
);
513 midend_redraw(fe
->me
);
518 static int CALLBACK
ConfigDlgProc(HWND hwnd
, UINT msg
,
519 WPARAM wParam
, LPARAM lParam
)
521 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
531 * OK and Cancel are special cases.
533 if ((HIWORD(wParam
) == BN_CLICKED
||
534 HIWORD(wParam
) == BN_DOUBLECLICKED
) &&
535 (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)) {
536 if (LOWORD(wParam
) == IDOK
) {
537 char *err
= midend_set_config(fe
->me
, fe
->cfg_which
, fe
->cfg
);
540 MessageBox(hwnd
, err
, "Validation error",
541 MB_ICONERROR
| MB_OK
);
552 * First find the control whose id this is.
554 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
555 if (j
->ctlid
== LOWORD(wParam
))
558 if (i
->type
== C_END
)
559 return 0; /* not our problem */
561 if (i
->type
== C_STRING
&& HIWORD(wParam
) == EN_CHANGE
) {
563 GetDlgItemText(fe
->cfgbox
, j
->ctlid
, buffer
, lenof(buffer
));
564 buffer
[lenof(buffer
)-1] = '\0';
566 i
->sval
= dupstr(buffer
);
567 } else if (i
->type
== C_BOOLEAN
&&
568 (HIWORD(wParam
) == BN_CLICKED
||
569 HIWORD(wParam
) == BN_DOUBLECLICKED
)) {
570 i
->ival
= IsDlgButtonChecked(fe
->cfgbox
, j
->ctlid
);
571 } else if (i
->type
== C_CHOICES
&&
572 HIWORD(wParam
) == CBN_SELCHANGE
) {
573 i
->ival
= SendDlgItemMessage(fe
->cfgbox
, j
->ctlid
,
587 HWND
mkctrl(frontend
*fe
, int x1
, int x2
, int y1
, int y2
,
588 char *wclass
, int wstyle
,
589 int exstyle
, char *wtext
, int wid
)
592 ret
= CreateWindowEx(exstyle
, wclass
, wtext
,
593 wstyle
| WS_CHILD
| WS_VISIBLE
, x1
, y1
, x2
-x1
, y2
-y1
,
594 fe
->cfgbox
, (HMENU
) wid
, fe
->inst
, NULL
);
595 SendMessage(ret
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, MAKELPARAM(TRUE
, 0));
599 static int get_config(frontend
*fe
, int which
)
612 int winwidth
, winheight
, col1l
, col1r
, col2l
, col2r
, y
;
613 int height
, width
, maxlabel
, maxcheckbox
;
615 wc
.style
= CS_DBLCLKS
| CS_SAVEBITS
| CS_BYTEALIGNWINDOW
;
616 wc
.lpfnWndProc
= DefDlgProc
;
618 wc
.cbWndExtra
= DLGWINDOWEXTRA
+ 8;
619 wc
.hInstance
= fe
->inst
;
621 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
622 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+1);
623 wc
.lpszMenuName
= NULL
;
624 wc
.lpszClassName
= "GameConfigBox";
627 hdc
= GetDC(fe
->hwnd
);
628 SetMapMode(hdc
, MM_TEXT
);
630 fe
->cfg_done
= FALSE
;
632 fe
->cfgfont
= CreateFont(-MulDiv(8, GetDeviceCaps(hdc
, LOGPIXELSY
), 72),
634 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
635 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
640 oldfont
= SelectObject(hdc
, fe
->cfgfont
);
641 if (GetTextMetrics(hdc
, &tm
)) {
642 height
= tm
.tmAscent
+ tm
.tmDescent
;
643 width
= tm
.tmAveCharWidth
;
648 fe
->cfg
= midend_get_config(fe
->me
, which
, &title
);
649 fe
->cfg_which
= which
;
652 * Figure out the layout of the config box by measuring the
653 * length of each piece of text.
655 maxlabel
= maxcheckbox
= 0;
656 winheight
= height
/2;
658 for (i
= fe
->cfg
; i
->type
!= C_END
; i
++) {
663 * Both these control types have a label filling only
664 * the left-hand column of the box.
666 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
669 winheight
+= height
* 3 / 2 + (height
/ 2);
674 * Checkboxes take up the whole of the box width.
676 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
677 maxcheckbox
< size
.cx
)
678 maxcheckbox
= size
.cx
;
679 winheight
+= height
+ (height
/ 2);
684 winheight
+= height
+ height
* 7 / 4; /* OK / Cancel buttons */
687 col1r
= col1l
+ maxlabel
;
688 col2l
= col1r
+ 2*width
;
689 col2r
= col2l
+ 30*width
;
690 if (col2r
< col1l
+2*height
+maxcheckbox
)
691 col2r
= col1l
+2*height
+maxcheckbox
;
692 winwidth
= col2r
+ 2*width
;
694 ReleaseDC(fe
->hwnd
, hdc
);
697 * Create the dialog, now that we know its size.
704 r
.bottom
= winheight
;
706 AdjustWindowRectEx(&r
, (WS_OVERLAPPEDWINDOW
/*|
707 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
708 WS_CAPTION | WS_SYSMENU*/) &~
709 (WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
713 * Centre the dialog on its parent window.
717 GetWindowRect(fe
->hwnd
, &r2
);
718 r
.left
= (r2
.left
+ r2
.right
- r
.right
) / 2;
719 r
.top
= (r2
.top
+ r2
.bottom
- r
.bottom
) / 2;
723 fe
->cfgbox
= CreateWindowEx(0, wc
.lpszClassName
, title
,
724 DS_MODALFRAME
| WS_POPUP
| WS_VISIBLE
|
725 WS_CAPTION
| WS_SYSMENU
,
727 r
.right
-r
.left
, r
.bottom
-r
.top
,
728 fe
->hwnd
, NULL
, fe
->inst
, NULL
);
732 SendMessage(fe
->cfgbox
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, FALSE
);
734 SetWindowLong(fe
->cfgbox
, GWL_USERDATA
, (LONG
)fe
);
735 SetWindowLong(fe
->cfgbox
, DWL_DLGPROC
, (LONG
)ConfigDlgProc
);
738 * Count the controls so we can allocate cfgaux.
740 for (nctrls
= 0, i
= fe
->cfg
; i
->type
!= C_END
; i
++)
742 fe
->cfgaux
= snewn(nctrls
, struct cfg_aux
);
746 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
750 * Edit box with a label beside it.
752 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
753 "Static", 0, 0, i
->name
, id
++);
754 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*3/2,
755 "EDIT", WS_TABSTOP
| ES_AUTOHSCROLL
,
756 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
757 SetWindowText(ctl
, i
->sval
);
765 mkctrl(fe
, col1l
, col2r
, y
, y
+height
, "BUTTON",
766 BS_NOTIFY
| BS_AUTOCHECKBOX
| WS_TABSTOP
,
767 0, i
->name
, (j
->ctlid
= id
++));
768 CheckDlgButton(fe
->cfgbox
, j
->ctlid
, (i
->ival
!= 0));
774 * Drop-down list with a label beside it.
776 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
777 "STATIC", 0, 0, i
->name
, id
++);
778 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*41/2,
779 "COMBOBOX", WS_TABSTOP
|
780 CBS_DROPDOWNLIST
| CBS_HASSTRINGS
,
781 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
783 char c
, *p
, *q
, *str
;
785 SendMessage(ctl
, CB_RESETCONTENT
, 0, 0);
790 while (*q
&& *q
!= c
) q
++;
791 str
= snewn(q
-p
+1, char);
792 strncpy(str
, p
, q
-p
);
794 SendMessage(ctl
, CB_ADDSTRING
, 0, (LPARAM
)str
);
801 SendMessage(ctl
, CB_SETCURSEL
, i
->ival
, 0);
807 assert(y
< winheight
);
811 y
+= height
/2; /* extra space before OK and Cancel */
812 mkctrl(fe
, col1l
, (col1l
+col2r
)/2-width
, y
, y
+height
*7/4, "BUTTON",
813 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
| BS_DEFPUSHBUTTON
, 0,
815 mkctrl(fe
, (col1l
+col2r
)/2+width
, col2r
, y
, y
+height
*7/4, "BUTTON",
816 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
, 0, "Cancel", IDCANCEL
);
818 SendMessage(fe
->cfgbox
, WM_INITDIALOG
, 0, 0);
820 EnableWindow(fe
->hwnd
, FALSE
);
821 ShowWindow(fe
->cfgbox
, SW_NORMAL
);
822 while ((gm
=GetMessage(&msg
, NULL
, 0, 0)) > 0) {
823 if (!IsDialogMessage(fe
->cfgbox
, &msg
))
824 DispatchMessage(&msg
);
828 EnableWindow(fe
->hwnd
, TRUE
);
829 SetForegroundWindow(fe
->hwnd
);
830 DestroyWindow(fe
->cfgbox
);
831 DeleteObject(fe
->cfgfont
);
836 return (fe
->cfg_done
== 2);
839 static void new_game_type(frontend
*fe
)
845 midend_new_game(fe
->me
);
846 midend_size(fe
->me
, &x
, &y
);
851 AdjustWindowRectEx(&r
, WS_OVERLAPPEDWINDOW
&~
852 (WS_THICKFRAME
| WS_MAXIMIZEBOX
|
856 if (fe
->statusbar
!= NULL
) {
857 GetWindowRect(fe
->statusbar
, &sr
);
859 sr
.left
= sr
.right
= sr
.top
= sr
.bottom
= 0;
861 SetWindowPos(fe
->hwnd
, NULL
, 0, 0,
863 r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
864 SWP_NOMOVE
| SWP_NOZORDER
);
865 if (fe
->statusbar
!= NULL
)
866 SetWindowPos(fe
->statusbar
, NULL
, 0, y
, x
,
867 sr
.bottom
- sr
.top
, SWP_NOZORDER
);
869 DeleteObject(fe
->bitmap
);
871 hdc
= GetDC(fe
->hwnd
);
872 fe
->bitmap
= CreateCompatibleBitmap(hdc
, x
, y
);
873 ReleaseDC(fe
->hwnd
, hdc
);
875 midend_redraw(fe
->me
);
878 static LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
,
879 WPARAM wParam
, LPARAM lParam
)
881 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
888 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
890 if (!midend_process_key(fe
->me
, 0, 0, 'n'))
894 if (!midend_process_key(fe
->me
, 0, 0, 'r'))
898 if (!midend_process_key(fe
->me
, 0, 0, 'u'))
902 if (!midend_process_key(fe
->me
, 0, 0, '\x12'))
906 if (!midend_process_key(fe
->me
, 0, 0, 'q'))
910 if (get_config(fe
, CFG_SETTINGS
))
914 if (get_config(fe
, CFG_SEED
))
918 assert(fe
->help_path
);
919 WinHelp(hwnd
, fe
->help_path
,
920 fe
->help_has_contents ? HELP_FINDER
: HELP_CONTENTS
, 0);
923 assert(fe
->help_path
);
924 assert(thegame
.winhelp_topic
);
926 char *cmd
= snewn(10+strlen(thegame
.winhelp_topic
), char);
927 sprintf(cmd
, "JI(`',`%s')", thegame
.winhelp_topic
);
928 WinHelp(hwnd
, fe
->help_path
, HELP_COMMAND
, (DWORD
)cmd
);
934 int p
= ((wParam
&~ 0xF) - IDM_PRESETS
) / 0x10;
936 if (p
>= 0 && p
< fe
->npresets
) {
937 midend_set_params(fe
->me
, fe
->presets
[p
]);
953 hdc
= BeginPaint(hwnd
, &p
);
954 hdc2
= CreateCompatibleDC(hdc
);
955 prevbm
= SelectObject(hdc2
, fe
->bitmap
);
957 p
.rcPaint
.left
, p
.rcPaint
.top
,
958 p
.rcPaint
.right
- p
.rcPaint
.left
,
959 p
.rcPaint
.bottom
- p
.rcPaint
.top
,
961 p
.rcPaint
.left
, p
.rcPaint
.top
,
963 SelectObject(hdc2
, prevbm
);
973 case VK_LEFT
: key
= CURSOR_LEFT
; break;
974 case VK_RIGHT
: key
= CURSOR_RIGHT
; break;
975 case VK_UP
: key
= CURSOR_UP
; break;
976 case VK_DOWN
: key
= CURSOR_DOWN
; break;
978 * Diagonal keys on the numeric keypad.
981 if (!(lParam
& 0x01000000)) key
= CURSOR_UP_RIGHT
;
984 if (!(lParam
& 0x01000000)) key
= CURSOR_DOWN_RIGHT
;
987 if (!(lParam
& 0x01000000)) key
= CURSOR_UP_LEFT
;
990 if (!(lParam
& 0x01000000)) key
= CURSOR_DOWN_LEFT
;
993 * Numeric keypad keys with Num Lock on.
995 case VK_NUMPAD4
: key
= CURSOR_LEFT
; break;
996 case VK_NUMPAD6
: key
= CURSOR_RIGHT
; break;
997 case VK_NUMPAD8
: key
= CURSOR_UP
; break;
998 case VK_NUMPAD2
: key
= CURSOR_DOWN
; break;
999 case VK_NUMPAD9
: key
= CURSOR_UP_RIGHT
; break;
1000 case VK_NUMPAD3
: key
= CURSOR_DOWN_RIGHT
; break;
1001 case VK_NUMPAD7
: key
= CURSOR_UP_LEFT
; break;
1002 case VK_NUMPAD1
: key
= CURSOR_DOWN_LEFT
; break;
1006 if (!midend_process_key(fe
->me
, 0, 0, key
))
1011 m
.message
= WM_KEYDOWN
;
1013 m
.lParam
= lParam
& 0xdfff;
1014 TranslateMessage(&m
);
1018 case WM_LBUTTONDOWN
:
1019 case WM_RBUTTONDOWN
:
1020 case WM_MBUTTONDOWN
:
1025 * Shift-clicks count as middle-clicks, since otherwise
1026 * two-button Windows users won't have any kind of
1027 * middle click to use.
1029 if (message
== WM_MBUTTONDOWN
|| (wParam
& MK_SHIFT
))
1030 button
= MIDDLE_BUTTON
;
1031 else if (message
== WM_LBUTTONDOWN
)
1032 button
= LEFT_BUTTON
;
1034 button
= RIGHT_BUTTON
;
1036 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1037 (signed short)HIWORD(lParam
), button
))
1050 * Shift-clicks count as middle-clicks, since otherwise
1051 * two-button Windows users won't have any kind of
1052 * middle click to use.
1054 if (message
== WM_MBUTTONUP
|| (wParam
& MK_SHIFT
))
1055 button
= MIDDLE_RELEASE
;
1056 else if (message
== WM_LBUTTONUP
)
1057 button
= LEFT_RELEASE
;
1059 button
= RIGHT_RELEASE
;
1061 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1062 (signed short)HIWORD(lParam
), button
))
1072 if (wParam
& (MK_MBUTTON
| MK_SHIFT
))
1073 button
= MIDDLE_DRAG
;
1074 else if (wParam
& MK_LBUTTON
)
1077 button
= RIGHT_DRAG
;
1079 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1080 (signed short)HIWORD(lParam
), button
))
1085 if (!midend_process_key(fe
->me
, 0, 0, (unsigned char)wParam
))
1090 DWORD now
= GetTickCount();
1091 float elapsed
= (float) (now
- fe
->timer_last_tickcount
) * 0.001F
;
1092 midend_timer(fe
->me
, elapsed
);
1093 fe
->timer_last_tickcount
= now
;
1098 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
1101 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
)
1106 InitCommonControls();
1112 wndclass
.lpfnWndProc
= WndProc
;
1113 wndclass
.cbClsExtra
= 0;
1114 wndclass
.cbWndExtra
= 0;
1115 wndclass
.hInstance
= inst
;
1116 wndclass
.hIcon
= LoadIcon(inst
, IDI_APPLICATION
);
1117 wndclass
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
1118 wndclass
.hbrBackground
= NULL
;
1119 wndclass
.lpszMenuName
= NULL
;
1120 wndclass
.lpszClassName
= thegame
.name
;
1122 RegisterClass(&wndclass
);
1125 while (*cmdline
&& isspace(*cmdline
))
1128 if (!new_window(inst
, *cmdline ? cmdline
: NULL
, &error
)) {
1130 sprintf(buf
, "%.100s Error", thegame
.name
);
1131 MessageBox(NULL
, error
, buf
, MB_OK
|MB_ICONERROR
);
1135 while (GetMessage(&msg
, NULL
, 0, 0)) {
1136 DispatchMessage(&msg
);