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_COPY 0x0050
31 #define IDM_SOLVE 0x0060
32 #define IDM_QUIT 0x0070
33 #define IDM_CONFIG 0x0080
34 #define IDM_DESC 0x0090
35 #define IDM_SEED 0x00A0
36 #define IDM_HELPC 0x00B0
37 #define IDM_GAMEHELP 0x00C0
38 #define IDM_ABOUT 0x00D0
39 #define IDM_PRESETS 0x0100
41 #define HELP_FILE_NAME "puzzles.hlp"
42 #define HELP_CNT_NAME "puzzles.cnt"
45 static FILE *debug_fp
= NULL
;
46 static HANDLE debug_hdl
= INVALID_HANDLE_VALUE
;
47 static int debug_got_console
= 0;
53 if (!debug_got_console
) {
55 debug_got_console
= 1;
56 debug_hdl
= GetStdHandle(STD_OUTPUT_HANDLE
);
60 debug_fp
= fopen("debug.log", "w");
63 if (debug_hdl
!= INVALID_HANDLE_VALUE
) {
64 WriteFile(debug_hdl
, buf
, strlen(buf
), &dw
, NULL
);
70 void debug_printf(char *fmt
, ...)
76 vsprintf(buf
, fmt
, ap
);
94 HWND hwnd
, statusbar
, cfgbox
;
96 HBITMAP bitmap
, prevbm
;
103 DWORD timer_last_tickcount
;
105 game_params
**presets
;
107 int nfonts
, fontsize
;
109 struct cfg_aux
*cfgaux
;
110 int cfg_which
, dlg_done
;
113 int help_has_contents
;
117 void fatal(char *fmt
, ...)
123 vsprintf(buf
, fmt
, ap
);
126 MessageBox(NULL
, buf
, "Fatal error", MB_ICONEXCLAMATION
| MB_OK
);
131 void get_random_seed(void **randseed
, int *randseedsize
)
133 time_t *tp
= snew(time_t);
135 *randseed
= (void *)tp
;
136 *randseedsize
= sizeof(time_t);
139 void status_bar(frontend
*fe
, char *text
)
141 char *rewritten
= midend_rewrite_statusbar(fe
->me
, text
);
142 if (!fe
->laststatus
|| strcmp(rewritten
, fe
->laststatus
)) {
143 SetWindowText(fe
->statusbar
, rewritten
);
144 sfree(fe
->laststatus
);
145 fe
->laststatus
= rewritten
;
151 void frontend_default_colour(frontend
*fe
, float *output
)
153 DWORD c
= GetSysColor(COLOR_MENU
); /* ick */
155 output
[0] = (float)(GetRValue(c
) / 255.0);
156 output
[1] = (float)(GetGValue(c
) / 255.0);
157 output
[2] = (float)(GetBValue(c
) / 255.0);
160 void clip(frontend
*fe
, int x
, int y
, int w
, int h
)
162 IntersectClipRect(fe
->hdc_bm
, x
, y
, x
+w
, y
+h
);
165 void unclip(frontend
*fe
)
167 SelectClipRgn(fe
->hdc_bm
, NULL
);
170 void draw_text(frontend
*fe
, int x
, int y
, int fonttype
, int fontsize
,
171 int align
, int colour
, char *text
)
176 * Find or create the font.
178 for (i
= 0; i
< fe
->nfonts
; i
++)
179 if (fe
->fonts
[i
].type
== fonttype
&& fe
->fonts
[i
].size
== fontsize
)
182 if (i
== fe
->nfonts
) {
183 if (fe
->fontsize
<= fe
->nfonts
) {
184 fe
->fontsize
= fe
->nfonts
+ 10;
185 fe
->fonts
= sresize(fe
->fonts
, fe
->fontsize
, struct font
);
190 fe
->fonts
[i
].type
= fonttype
;
191 fe
->fonts
[i
].size
= fontsize
;
193 fe
->fonts
[i
].font
= CreateFont(-fontsize
, 0, 0, 0, FW_BOLD
,
194 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
195 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
197 (fonttype
== FONT_FIXED ?
198 FIXED_PITCH
| FF_DONTCARE
:
199 VARIABLE_PITCH
| FF_SWISS
),
204 * Position and draw the text.
211 oldfont
= SelectObject(fe
->hdc_bm
, fe
->fonts
[i
].font
);
212 if (GetTextMetrics(fe
->hdc_bm
, &tm
)) {
213 if (align
& ALIGN_VCENTRE
)
214 y
-= (tm
.tmAscent
+tm
.tmDescent
)/2;
218 if (GetTextExtentPoint32(fe
->hdc_bm
, text
, strlen(text
), &size
)) {
219 if (align
& ALIGN_HCENTRE
)
221 else if (align
& ALIGN_HRIGHT
)
224 SetBkMode(fe
->hdc_bm
, TRANSPARENT
);
225 SetTextColor(fe
->hdc_bm
, fe
->colours
[colour
]);
226 TextOut(fe
->hdc_bm
, x
, y
, text
, strlen(text
));
227 SelectObject(fe
->hdc_bm
, oldfont
);
231 void draw_rect(frontend
*fe
, int x
, int y
, int w
, int h
, int colour
)
233 if (w
== 1 && h
== 1) {
235 * Rectangle() appears to get uppity if asked to draw a 1x1
236 * rectangle, presumably on the grounds that that's beneath
237 * its dignity and you ought to be using SetPixel instead.
240 SetPixel(fe
->hdc_bm
, x
, y
, fe
->colours
[colour
]);
242 HBRUSH oldbrush
= SelectObject(fe
->hdc_bm
, fe
->brushes
[colour
]);
243 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
244 Rectangle(fe
->hdc_bm
, x
, y
, x
+w
, y
+h
);
245 SelectObject(fe
->hdc_bm
, oldbrush
);
246 SelectObject(fe
->hdc_bm
, oldpen
);
250 void draw_line(frontend
*fe
, int x1
, int y1
, int x2
, int y2
, int colour
)
252 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
253 MoveToEx(fe
->hdc_bm
, x1
, y1
, NULL
);
254 LineTo(fe
->hdc_bm
, x2
, y2
);
255 SetPixel(fe
->hdc_bm
, x2
, y2
, fe
->colours
[colour
]);
256 SelectObject(fe
->hdc_bm
, oldpen
);
259 void draw_polygon(frontend
*fe
, int *coords
, int npoints
,
260 int fill
, int colour
)
262 POINT
*pts
= snewn(npoints
+1, POINT
);
265 for (i
= 0; i
<= npoints
; i
++) {
266 int j
= (i
< npoints ? i
: 0);
267 pts
[i
].x
= coords
[j
*2];
268 pts
[i
].y
= coords
[j
*2+1];
272 HBRUSH oldbrush
= SelectObject(fe
->hdc_bm
, fe
->brushes
[colour
]);
273 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
274 Polygon(fe
->hdc_bm
, pts
, npoints
);
275 SelectObject(fe
->hdc_bm
, oldbrush
);
276 SelectObject(fe
->hdc_bm
, oldpen
);
278 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
279 Polyline(fe
->hdc_bm
, pts
, npoints
+1);
280 SelectObject(fe
->hdc_bm
, oldpen
);
286 void start_draw(frontend
*fe
)
289 hdc_win
= GetDC(fe
->hwnd
);
290 fe
->hdc_bm
= CreateCompatibleDC(hdc_win
);
291 fe
->prevbm
= SelectObject(fe
->hdc_bm
, fe
->bitmap
);
292 ReleaseDC(fe
->hwnd
, hdc_win
);
294 SetMapMode(fe
->hdc_bm
, MM_TEXT
);
297 void draw_update(frontend
*fe
, int x
, int y
, int w
, int h
)
306 InvalidateRect(fe
->hwnd
, &r
, FALSE
);
309 void end_draw(frontend
*fe
)
311 SelectObject(fe
->hdc_bm
, fe
->prevbm
);
312 DeleteDC(fe
->hdc_bm
);
314 DeleteObject(fe
->clip
);
319 void deactivate_timer(frontend
*fe
)
321 KillTimer(fe
->hwnd
, fe
->timer
);
325 void activate_timer(frontend
*fe
)
328 fe
->timer
= SetTimer(fe
->hwnd
, fe
->timer
, 20, NULL
);
329 fe
->timer_last_tickcount
= GetTickCount();
333 void write_clip(HWND hwnd
, char *data
)
341 * Windows expects CRLF in the clipboard, so we must convert
342 * any \n that has come out of the puzzle backend.
345 for (i
= 0; data
[i
]; i
++) {
350 data2
= snewn(len
+1, char);
352 for (i
= 0; data
[i
]; i
++) {
355 data2
[j
++] = data
[i
];
360 clipdata
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, len
+ 1);
363 lock
= GlobalLock(clipdata
);
366 memcpy(lock
, data2
, len
);
367 ((unsigned char *) lock
)[len
] = 0;
368 GlobalUnlock(clipdata
);
370 if (OpenClipboard(hwnd
)) {
372 SetClipboardData(CF_TEXT
, clipdata
);
375 GlobalFree(clipdata
);
381 * See if we can find a help file.
383 static void find_help_file(frontend
*fe
)
385 char b
[2048], *p
, *q
, *r
;
387 if (!fe
->help_path
) {
388 GetModuleFileName(NULL
, b
, sizeof(b
) - 1);
390 p
= strrchr(b
, '\\');
391 if (p
&& p
>= r
) r
= p
+1;
393 if (q
&& q
>= r
) r
= q
+1;
394 strcpy(r
, HELP_FILE_NAME
);
395 if ( (fp
= fopen(b
, "r")) != NULL
) {
396 fe
->help_path
= dupstr(b
);
399 fe
->help_path
= NULL
;
400 strcpy(r
, HELP_CNT_NAME
);
401 if ( (fp
= fopen(b
, "r")) != NULL
) {
402 fe
->help_has_contents
= TRUE
;
405 fe
->help_has_contents
= FALSE
;
409 static frontend
*new_window(HINSTANCE inst
, char *game_id
, char **error
)
418 fe
->me
= midend_new(fe
, &thegame
);
421 *error
= midend_game_id(fe
->me
, game_id
);
429 fe
->help_path
= NULL
;
433 midend_new_game(fe
->me
);
434 midend_size(fe
->me
, &x
, &y
);
439 fe
->nfonts
= fe
->fontsize
= 0;
441 fe
->laststatus
= NULL
;
447 colours
= midend_colours(fe
->me
, &ncolours
);
449 fe
->colours
= snewn(ncolours
, COLORREF
);
450 fe
->brushes
= snewn(ncolours
, HBRUSH
);
451 fe
->pens
= snewn(ncolours
, HPEN
);
453 for (i
= 0; i
< ncolours
; i
++) {
454 fe
->colours
[i
] = RGB(255 * colours
[i
*3+0],
455 255 * colours
[i
*3+1],
456 255 * colours
[i
*3+2]);
457 fe
->brushes
[i
] = CreateSolidBrush(fe
->colours
[i
]);
458 fe
->pens
[i
] = CreatePen(PS_SOLID
, 1, fe
->colours
[i
]);
465 AdjustWindowRectEx(&r
, WS_OVERLAPPEDWINDOW
&~
466 (WS_THICKFRAME
| WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
469 fe
->hwnd
= CreateWindowEx(0, thegame
.name
, thegame
.name
,
470 WS_OVERLAPPEDWINDOW
&~
471 (WS_THICKFRAME
| WS_MAXIMIZEBOX
),
472 CW_USEDEFAULT
, CW_USEDEFAULT
,
473 r
.right
- r
.left
, r
.bottom
- r
.top
,
474 NULL
, NULL
, inst
, NULL
);
477 HMENU bar
= CreateMenu();
478 HMENU menu
= CreateMenu();
480 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)menu
, "Game");
481 AppendMenu(menu
, MF_ENABLED
, IDM_NEW
, "New");
482 AppendMenu(menu
, MF_ENABLED
, IDM_RESTART
, "Restart");
483 AppendMenu(menu
, MF_ENABLED
, IDM_DESC
, "Specific...");
484 AppendMenu(menu
, MF_ENABLED
, IDM_SEED
, "Random Seed...");
486 if ((fe
->npresets
= midend_num_presets(fe
->me
)) > 0 ||
487 thegame
.can_configure
) {
488 HMENU sub
= CreateMenu();
491 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)sub
, "Type");
493 fe
->presets
= snewn(fe
->npresets
, game_params
*);
495 for (i
= 0; i
< fe
->npresets
; i
++) {
498 midend_fetch_preset(fe
->me
, i
, &name
, &fe
->presets
[i
]);
501 * FIXME: we ought to go through and do something
502 * with ampersands here.
505 AppendMenu(sub
, MF_ENABLED
, IDM_PRESETS
+ 0x10 * i
, name
);
508 if (thegame
.can_configure
) {
509 AppendMenu(sub
, MF_ENABLED
, IDM_CONFIG
, "Custom...");
513 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
514 AppendMenu(menu
, MF_ENABLED
, IDM_UNDO
, "Undo");
515 AppendMenu(menu
, MF_ENABLED
, IDM_REDO
, "Redo");
516 if (thegame
.can_format_as_text
) {
517 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
518 AppendMenu(menu
, MF_ENABLED
, IDM_COPY
, "Copy");
520 if (thegame
.can_solve
) {
521 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
522 AppendMenu(menu
, MF_ENABLED
, IDM_SOLVE
, "Solve");
524 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
525 AppendMenu(menu
, MF_ENABLED
, IDM_QUIT
, "Exit");
527 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)menu
, "Help");
528 AppendMenu(menu
, MF_ENABLED
, IDM_ABOUT
, "About");
530 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
531 AppendMenu(menu
, MF_ENABLED
, IDM_HELPC
, "Contents");
532 if (thegame
.winhelp_topic
) {
534 assert(thegame
.name
);
535 item
= snewn(9+strlen(thegame
.name
), char); /*ick*/
536 sprintf(item
, "Help on %s", thegame
.name
);
537 AppendMenu(menu
, MF_ENABLED
, IDM_GAMEHELP
, item
);
541 SetMenu(fe
->hwnd
, bar
);
544 if (midend_wants_statusbar(fe
->me
)) {
545 fe
->statusbar
= CreateWindowEx(0, STATUSCLASSNAME
, "ooh",
546 WS_CHILD
| WS_VISIBLE
,
547 0, 0, 0, 0, /* status bar does these */
548 fe
->hwnd
, NULL
, inst
, NULL
);
549 GetWindowRect(fe
->statusbar
, &sr
);
550 SetWindowPos(fe
->hwnd
, NULL
, 0, 0,
551 r
.right
- r
.left
, r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
552 SWP_NOMOVE
| SWP_NOZORDER
);
553 SetWindowPos(fe
->statusbar
, NULL
, 0, y
, x
, sr
.bottom
- sr
.top
,
556 fe
->statusbar
= NULL
;
559 hdc
= GetDC(fe
->hwnd
);
560 fe
->bitmap
= CreateCompatibleBitmap(hdc
, x
, y
);
561 ReleaseDC(fe
->hwnd
, hdc
);
563 SetWindowLong(fe
->hwnd
, GWL_USERDATA
, (LONG
)fe
);
565 ShowWindow(fe
->hwnd
, SW_NORMAL
);
566 SetForegroundWindow(fe
->hwnd
);
568 midend_redraw(fe
->me
);
573 static int CALLBACK
AboutDlgProc(HWND hwnd
, UINT msg
,
574 WPARAM wParam
, LPARAM lParam
)
576 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
583 if ((HIWORD(wParam
) == BN_CLICKED
||
584 HIWORD(wParam
) == BN_DOUBLECLICKED
) &&
585 LOWORD(wParam
) == IDOK
)
597 static int CALLBACK
ConfigDlgProc(HWND hwnd
, UINT msg
,
598 WPARAM wParam
, LPARAM lParam
)
600 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
610 * OK and Cancel are special cases.
612 if ((HIWORD(wParam
) == BN_CLICKED
||
613 HIWORD(wParam
) == BN_DOUBLECLICKED
) &&
614 (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)) {
615 if (LOWORD(wParam
) == IDOK
) {
616 char *err
= midend_set_config(fe
->me
, fe
->cfg_which
, fe
->cfg
);
619 MessageBox(hwnd
, err
, "Validation error",
620 MB_ICONERROR
| MB_OK
);
631 * First find the control whose id this is.
633 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
634 if (j
->ctlid
== LOWORD(wParam
))
637 if (i
->type
== C_END
)
638 return 0; /* not our problem */
640 if (i
->type
== C_STRING
&& HIWORD(wParam
) == EN_CHANGE
) {
642 GetDlgItemText(fe
->cfgbox
, j
->ctlid
, buffer
, lenof(buffer
));
643 buffer
[lenof(buffer
)-1] = '\0';
645 i
->sval
= dupstr(buffer
);
646 } else if (i
->type
== C_BOOLEAN
&&
647 (HIWORD(wParam
) == BN_CLICKED
||
648 HIWORD(wParam
) == BN_DOUBLECLICKED
)) {
649 i
->ival
= IsDlgButtonChecked(fe
->cfgbox
, j
->ctlid
);
650 } else if (i
->type
== C_CHOICES
&&
651 HIWORD(wParam
) == CBN_SELCHANGE
) {
652 i
->ival
= SendDlgItemMessage(fe
->cfgbox
, j
->ctlid
,
666 HWND
mkctrl(frontend
*fe
, int x1
, int x2
, int y1
, int y2
,
667 char *wclass
, int wstyle
,
668 int exstyle
, const char *wtext
, int wid
)
671 ret
= CreateWindowEx(exstyle
, wclass
, wtext
,
672 wstyle
| WS_CHILD
| WS_VISIBLE
, x1
, y1
, x2
-x1
, y2
-y1
,
673 fe
->cfgbox
, (HMENU
) wid
, fe
->inst
, NULL
);
674 SendMessage(ret
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, MAKELPARAM(TRUE
, 0));
678 static void about(frontend
*fe
)
688 int winwidth
, winheight
, y
;
689 int height
, width
, maxwid
;
690 const char *strings
[16];
695 sprintf(titlebuf
, "About %.250s", thegame
.name
);
697 strings
[nstrings
++] = thegame
.name
;
698 strings
[nstrings
++] = "from Simon Tatham's Portable Puzzle Collection";
699 strings
[nstrings
++] = ver
;
701 wc
.style
= CS_DBLCLKS
| CS_SAVEBITS
| CS_BYTEALIGNWINDOW
;
702 wc
.lpfnWndProc
= DefDlgProc
;
704 wc
.cbWndExtra
= DLGWINDOWEXTRA
+ 8;
705 wc
.hInstance
= fe
->inst
;
707 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
708 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+1);
709 wc
.lpszMenuName
= NULL
;
710 wc
.lpszClassName
= "GameAboutBox";
713 hdc
= GetDC(fe
->hwnd
);
714 SetMapMode(hdc
, MM_TEXT
);
716 fe
->dlg_done
= FALSE
;
718 fe
->cfgfont
= CreateFont(-MulDiv(8, GetDeviceCaps(hdc
, LOGPIXELSY
), 72),
720 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
721 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
726 oldfont
= SelectObject(hdc
, fe
->cfgfont
);
727 if (GetTextMetrics(hdc
, &tm
)) {
728 height
= tm
.tmAscent
+ tm
.tmDescent
;
729 width
= tm
.tmAveCharWidth
;
735 * Figure out the layout of the About box by measuring the
736 * length of each piece of text.
739 winheight
= height
/2;
741 for (i
= 0; i
< nstrings
; i
++) {
742 if (GetTextExtentPoint32(hdc
, strings
[i
], strlen(strings
[i
]), &size
))
743 lengths
[i
] = size
.cx
;
745 lengths
[i
] = 0; /* *shrug* */
746 if (maxwid
< lengths
[i
])
748 winheight
+= height
* 3 / 2 + (height
/ 2);
751 winheight
+= height
+ height
* 7 / 4; /* OK button */
752 winwidth
= maxwid
+ 4*width
;
754 SelectObject(hdc
, oldfont
);
755 ReleaseDC(fe
->hwnd
, hdc
);
758 * Create the dialog, now that we know its size.
765 r
.bottom
= winheight
;
767 AdjustWindowRectEx(&r
, (WS_OVERLAPPEDWINDOW
/*|
768 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
769 WS_CAPTION | WS_SYSMENU*/) &~
770 (WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
774 * Centre the dialog on its parent window.
778 GetWindowRect(fe
->hwnd
, &r2
);
779 r
.left
= (r2
.left
+ r2
.right
- r
.right
) / 2;
780 r
.top
= (r2
.top
+ r2
.bottom
- r
.bottom
) / 2;
784 fe
->cfgbox
= CreateWindowEx(0, wc
.lpszClassName
, titlebuf
,
785 DS_MODALFRAME
| WS_POPUP
| WS_VISIBLE
|
786 WS_CAPTION
| WS_SYSMENU
,
788 r
.right
-r
.left
, r
.bottom
-r
.top
,
789 fe
->hwnd
, NULL
, fe
->inst
, NULL
);
792 SendMessage(fe
->cfgbox
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, FALSE
);
794 SetWindowLong(fe
->cfgbox
, GWL_USERDATA
, (LONG
)fe
);
795 SetWindowLong(fe
->cfgbox
, DWL_DLGPROC
, (LONG
)AboutDlgProc
);
799 for (i
= 0; i
< nstrings
; i
++) {
800 int border
= width
*2 + (maxwid
- lengths
[i
]) / 2;
801 mkctrl(fe
, border
, border
+lengths
[i
], y
+height
*1/8, y
+height
*9/8,
802 "Static", 0, 0, strings
[i
], id
++);
805 assert(y
< winheight
);
809 y
+= height
/2; /* extra space before OK */
810 mkctrl(fe
, width
*2, maxwid
+width
*2, y
, y
+height
*7/4, "BUTTON",
811 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
| BS_DEFPUSHBUTTON
, 0,
814 SendMessage(fe
->cfgbox
, WM_INITDIALOG
, 0, 0);
816 EnableWindow(fe
->hwnd
, FALSE
);
817 ShowWindow(fe
->cfgbox
, SW_NORMAL
);
818 while ((gm
=GetMessage(&msg
, NULL
, 0, 0)) > 0) {
819 if (!IsDialogMessage(fe
->cfgbox
, &msg
))
820 DispatchMessage(&msg
);
824 EnableWindow(fe
->hwnd
, TRUE
);
825 SetForegroundWindow(fe
->hwnd
);
826 DestroyWindow(fe
->cfgbox
);
827 DeleteObject(fe
->cfgfont
);
830 static int get_config(frontend
*fe
, int which
)
843 int winwidth
, winheight
, col1l
, col1r
, col2l
, col2r
, y
;
844 int height
, width
, maxlabel
, maxcheckbox
;
846 wc
.style
= CS_DBLCLKS
| CS_SAVEBITS
| CS_BYTEALIGNWINDOW
;
847 wc
.lpfnWndProc
= DefDlgProc
;
849 wc
.cbWndExtra
= DLGWINDOWEXTRA
+ 8;
850 wc
.hInstance
= fe
->inst
;
852 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
853 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+1);
854 wc
.lpszMenuName
= NULL
;
855 wc
.lpszClassName
= "GameConfigBox";
858 hdc
= GetDC(fe
->hwnd
);
859 SetMapMode(hdc
, MM_TEXT
);
861 fe
->dlg_done
= FALSE
;
863 fe
->cfgfont
= CreateFont(-MulDiv(8, GetDeviceCaps(hdc
, LOGPIXELSY
), 72),
865 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
866 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
871 oldfont
= SelectObject(hdc
, fe
->cfgfont
);
872 if (GetTextMetrics(hdc
, &tm
)) {
873 height
= tm
.tmAscent
+ tm
.tmDescent
;
874 width
= tm
.tmAveCharWidth
;
879 fe
->cfg
= midend_get_config(fe
->me
, which
, &title
);
880 fe
->cfg_which
= which
;
883 * Figure out the layout of the config box by measuring the
884 * length of each piece of text.
886 maxlabel
= maxcheckbox
= 0;
887 winheight
= height
/2;
889 for (i
= fe
->cfg
; i
->type
!= C_END
; i
++) {
894 * Both these control types have a label filling only
895 * the left-hand column of the box.
897 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
900 winheight
+= height
* 3 / 2 + (height
/ 2);
905 * Checkboxes take up the whole of the box width.
907 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
908 maxcheckbox
< size
.cx
)
909 maxcheckbox
= size
.cx
;
910 winheight
+= height
+ (height
/ 2);
915 winheight
+= height
+ height
* 7 / 4; /* OK / Cancel buttons */
918 col1r
= col1l
+ maxlabel
;
919 col2l
= col1r
+ 2*width
;
920 col2r
= col2l
+ 30*width
;
921 if (col2r
< col1l
+2*height
+maxcheckbox
)
922 col2r
= col1l
+2*height
+maxcheckbox
;
923 winwidth
= col2r
+ 2*width
;
925 SelectObject(hdc
, oldfont
);
926 ReleaseDC(fe
->hwnd
, hdc
);
929 * Create the dialog, now that we know its size.
936 r
.bottom
= winheight
;
938 AdjustWindowRectEx(&r
, (WS_OVERLAPPEDWINDOW
/*|
939 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
940 WS_CAPTION | WS_SYSMENU*/) &~
941 (WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
945 * Centre the dialog on its parent window.
949 GetWindowRect(fe
->hwnd
, &r2
);
950 r
.left
= (r2
.left
+ r2
.right
- r
.right
) / 2;
951 r
.top
= (r2
.top
+ r2
.bottom
- r
.bottom
) / 2;
955 fe
->cfgbox
= CreateWindowEx(0, wc
.lpszClassName
, title
,
956 DS_MODALFRAME
| WS_POPUP
| WS_VISIBLE
|
957 WS_CAPTION
| WS_SYSMENU
,
959 r
.right
-r
.left
, r
.bottom
-r
.top
,
960 fe
->hwnd
, NULL
, fe
->inst
, NULL
);
964 SendMessage(fe
->cfgbox
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, FALSE
);
966 SetWindowLong(fe
->cfgbox
, GWL_USERDATA
, (LONG
)fe
);
967 SetWindowLong(fe
->cfgbox
, DWL_DLGPROC
, (LONG
)ConfigDlgProc
);
970 * Count the controls so we can allocate cfgaux.
972 for (nctrls
= 0, i
= fe
->cfg
; i
->type
!= C_END
; i
++)
974 fe
->cfgaux
= snewn(nctrls
, struct cfg_aux
);
978 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
982 * Edit box with a label beside it.
984 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
985 "Static", 0, 0, i
->name
, id
++);
986 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*3/2,
987 "EDIT", WS_TABSTOP
| ES_AUTOHSCROLL
,
988 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
989 SetWindowText(ctl
, i
->sval
);
997 mkctrl(fe
, col1l
, col2r
, y
, y
+height
, "BUTTON",
998 BS_NOTIFY
| BS_AUTOCHECKBOX
| WS_TABSTOP
,
999 0, i
->name
, (j
->ctlid
= id
++));
1000 CheckDlgButton(fe
->cfgbox
, j
->ctlid
, (i
->ival
!= 0));
1006 * Drop-down list with a label beside it.
1008 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
1009 "STATIC", 0, 0, i
->name
, id
++);
1010 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*41/2,
1011 "COMBOBOX", WS_TABSTOP
|
1012 CBS_DROPDOWNLIST
| CBS_HASSTRINGS
,
1013 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
1015 char c
, *p
, *q
, *str
;
1017 SendMessage(ctl
, CB_RESETCONTENT
, 0, 0);
1022 while (*q
&& *q
!= c
) q
++;
1023 str
= snewn(q
-p
+1, char);
1024 strncpy(str
, p
, q
-p
);
1026 SendMessage(ctl
, CB_ADDSTRING
, 0, (LPARAM
)str
);
1033 SendMessage(ctl
, CB_SETCURSEL
, i
->ival
, 0);
1039 assert(y
< winheight
);
1043 y
+= height
/2; /* extra space before OK and Cancel */
1044 mkctrl(fe
, col1l
, (col1l
+col2r
)/2-width
, y
, y
+height
*7/4, "BUTTON",
1045 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
| BS_DEFPUSHBUTTON
, 0,
1047 mkctrl(fe
, (col1l
+col2r
)/2+width
, col2r
, y
, y
+height
*7/4, "BUTTON",
1048 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
, 0, "Cancel", IDCANCEL
);
1050 SendMessage(fe
->cfgbox
, WM_INITDIALOG
, 0, 0);
1052 EnableWindow(fe
->hwnd
, FALSE
);
1053 ShowWindow(fe
->cfgbox
, SW_NORMAL
);
1054 while ((gm
=GetMessage(&msg
, NULL
, 0, 0)) > 0) {
1055 if (!IsDialogMessage(fe
->cfgbox
, &msg
))
1056 DispatchMessage(&msg
);
1060 EnableWindow(fe
->hwnd
, TRUE
);
1061 SetForegroundWindow(fe
->hwnd
);
1062 DestroyWindow(fe
->cfgbox
);
1063 DeleteObject(fe
->cfgfont
);
1068 return (fe
->dlg_done
== 2);
1071 static void new_game_type(frontend
*fe
)
1077 midend_new_game(fe
->me
);
1078 midend_size(fe
->me
, &x
, &y
);
1083 AdjustWindowRectEx(&r
, WS_OVERLAPPEDWINDOW
&~
1084 (WS_THICKFRAME
| WS_MAXIMIZEBOX
|
1088 if (fe
->statusbar
!= NULL
) {
1089 GetWindowRect(fe
->statusbar
, &sr
);
1091 sr
.left
= sr
.right
= sr
.top
= sr
.bottom
= 0;
1093 SetWindowPos(fe
->hwnd
, NULL
, 0, 0,
1095 r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
1096 SWP_NOMOVE
| SWP_NOZORDER
);
1097 if (fe
->statusbar
!= NULL
)
1098 SetWindowPos(fe
->statusbar
, NULL
, 0, y
, x
,
1099 sr
.bottom
- sr
.top
, SWP_NOZORDER
);
1101 DeleteObject(fe
->bitmap
);
1103 hdc
= GetDC(fe
->hwnd
);
1104 fe
->bitmap
= CreateCompatibleBitmap(hdc
, x
, y
);
1105 ReleaseDC(fe
->hwnd
, hdc
);
1107 midend_redraw(fe
->me
);
1110 static int is_alt_pressed(void)
1113 int r
= GetKeyboardState(keystate
);
1116 if (keystate
[VK_MENU
] & 0x80)
1118 if (keystate
[VK_RMENU
] & 0x80)
1123 static LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
,
1124 WPARAM wParam
, LPARAM lParam
)
1126 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
1130 DestroyWindow(hwnd
);
1133 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
1135 if (!midend_process_key(fe
->me
, 0, 0, 'n'))
1139 midend_restart_game(fe
->me
);
1142 if (!midend_process_key(fe
->me
, 0, 0, 'u'))
1146 if (!midend_process_key(fe
->me
, 0, 0, '\x12'))
1151 char *text
= midend_text_format(fe
->me
);
1153 write_clip(hwnd
, text
);
1155 MessageBeep(MB_ICONWARNING
);
1161 char *msg
= midend_solve(fe
->me
);
1163 MessageBox(hwnd
, msg
, "Unable to solve",
1164 MB_ICONERROR
| MB_OK
);
1168 if (!midend_process_key(fe
->me
, 0, 0, 'q'))
1172 if (get_config(fe
, CFG_SETTINGS
))
1176 if (get_config(fe
, CFG_SEED
))
1180 if (get_config(fe
, CFG_DESC
))
1187 assert(fe
->help_path
);
1188 WinHelp(hwnd
, fe
->help_path
,
1189 fe
->help_has_contents ? HELP_FINDER
: HELP_CONTENTS
, 0);
1192 assert(fe
->help_path
);
1193 assert(thegame
.winhelp_topic
);
1195 char *cmd
= snewn(10+strlen(thegame
.winhelp_topic
), char);
1196 sprintf(cmd
, "JI(`',`%s')", thegame
.winhelp_topic
);
1197 WinHelp(hwnd
, fe
->help_path
, HELP_COMMAND
, (DWORD
)cmd
);
1203 int p
= ((wParam
&~ 0xF) - IDM_PRESETS
) / 0x10;
1205 if (p
>= 0 && p
< fe
->npresets
) {
1206 midend_set_params(fe
->me
, fe
->presets
[p
]);
1222 hdc
= BeginPaint(hwnd
, &p
);
1223 hdc2
= CreateCompatibleDC(hdc
);
1224 prevbm
= SelectObject(hdc2
, fe
->bitmap
);
1226 p
.rcPaint
.left
, p
.rcPaint
.top
,
1227 p
.rcPaint
.right
- p
.rcPaint
.left
,
1228 p
.rcPaint
.bottom
- p
.rcPaint
.top
,
1230 p
.rcPaint
.left
, p
.rcPaint
.top
,
1232 SelectObject(hdc2
, prevbm
);
1241 int r
= GetKeyboardState(keystate
);
1242 int shift
= (r
&& (keystate
[VK_SHIFT
] & 0x80)) ? MOD_SHFT
: 0;
1243 int ctrl
= (r
&& (keystate
[VK_CONTROL
] & 0x80)) ? MOD_CTRL
: 0;
1247 if (!(lParam
& 0x01000000))
1248 key
= MOD_NUM_KEYPAD
| '4';
1250 key
= shift
| ctrl
| CURSOR_LEFT
;
1253 if (!(lParam
& 0x01000000))
1254 key
= MOD_NUM_KEYPAD
| '6';
1256 key
= shift
| ctrl
| CURSOR_RIGHT
;
1259 if (!(lParam
& 0x01000000))
1260 key
= MOD_NUM_KEYPAD
| '8';
1262 key
= shift
| ctrl
| CURSOR_UP
;
1265 if (!(lParam
& 0x01000000))
1266 key
= MOD_NUM_KEYPAD
| '2';
1268 key
= shift
| ctrl
| CURSOR_DOWN
;
1271 * Diagonal keys on the numeric keypad.
1274 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '9';
1277 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '3';
1280 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '7';
1283 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '1';
1286 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '0';
1289 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '5';
1292 * Numeric keypad keys with Num Lock on.
1294 case VK_NUMPAD4
: key
= MOD_NUM_KEYPAD
| '4'; break;
1295 case VK_NUMPAD6
: key
= MOD_NUM_KEYPAD
| '6'; break;
1296 case VK_NUMPAD8
: key
= MOD_NUM_KEYPAD
| '8'; break;
1297 case VK_NUMPAD2
: key
= MOD_NUM_KEYPAD
| '2'; break;
1298 case VK_NUMPAD5
: key
= MOD_NUM_KEYPAD
| '5'; break;
1299 case VK_NUMPAD9
: key
= MOD_NUM_KEYPAD
| '9'; break;
1300 case VK_NUMPAD3
: key
= MOD_NUM_KEYPAD
| '3'; break;
1301 case VK_NUMPAD7
: key
= MOD_NUM_KEYPAD
| '7'; break;
1302 case VK_NUMPAD1
: key
= MOD_NUM_KEYPAD
| '1'; break;
1303 case VK_NUMPAD0
: key
= MOD_NUM_KEYPAD
| '0'; break;
1307 if (!midend_process_key(fe
->me
, 0, 0, key
))
1312 m
.message
= WM_KEYDOWN
;
1314 m
.lParam
= lParam
& 0xdfff;
1315 TranslateMessage(&m
);
1319 case WM_LBUTTONDOWN
:
1320 case WM_RBUTTONDOWN
:
1321 case WM_MBUTTONDOWN
:
1326 * Shift-clicks count as middle-clicks, since otherwise
1327 * two-button Windows users won't have any kind of
1328 * middle click to use.
1330 if (message
== WM_MBUTTONDOWN
|| (wParam
& MK_SHIFT
))
1331 button
= MIDDLE_BUTTON
;
1332 else if (message
== WM_RBUTTONDOWN
|| is_alt_pressed())
1333 button
= RIGHT_BUTTON
;
1335 button
= LEFT_BUTTON
;
1337 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1338 (signed short)HIWORD(lParam
), button
))
1351 * Shift-clicks count as middle-clicks, since otherwise
1352 * two-button Windows users won't have any kind of
1353 * middle click to use.
1355 if (message
== WM_MBUTTONUP
|| (wParam
& MK_SHIFT
))
1356 button
= MIDDLE_RELEASE
;
1357 else if (message
== WM_RBUTTONUP
|| is_alt_pressed())
1358 button
= RIGHT_RELEASE
;
1360 button
= LEFT_RELEASE
;
1362 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1363 (signed short)HIWORD(lParam
), button
))
1373 if (wParam
& (MK_MBUTTON
| MK_SHIFT
))
1374 button
= MIDDLE_DRAG
;
1375 else if (wParam
& MK_RBUTTON
|| is_alt_pressed())
1376 button
= RIGHT_DRAG
;
1380 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1381 (signed short)HIWORD(lParam
), button
))
1386 if (!midend_process_key(fe
->me
, 0, 0, (unsigned char)wParam
))
1391 DWORD now
= GetTickCount();
1392 float elapsed
= (float) (now
- fe
->timer_last_tickcount
) * 0.001F
;
1393 midend_timer(fe
->me
, elapsed
);
1394 fe
->timer_last_tickcount
= now
;
1399 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
1402 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
)
1407 InitCommonControls();
1413 wndclass
.lpfnWndProc
= WndProc
;
1414 wndclass
.cbClsExtra
= 0;
1415 wndclass
.cbWndExtra
= 0;
1416 wndclass
.hInstance
= inst
;
1417 wndclass
.hIcon
= LoadIcon(inst
, IDI_APPLICATION
);
1418 wndclass
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
1419 wndclass
.hbrBackground
= NULL
;
1420 wndclass
.lpszMenuName
= NULL
;
1421 wndclass
.lpszClassName
= thegame
.name
;
1423 RegisterClass(&wndclass
);
1426 while (*cmdline
&& isspace(*cmdline
))
1429 if (!new_window(inst
, *cmdline ? cmdline
: NULL
, &error
)) {
1431 sprintf(buf
, "%.100s Error", thegame
.name
);
1432 MessageBox(NULL
, error
, buf
, MB_OK
|MB_ICONERROR
);
1436 while (GetMessage(&msg
, NULL
, 0, 0)) {
1437 DispatchMessage(&msg
);