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_PRESETS 0x0100
39 #define IDM_ABOUT 0x0110
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
);
81 #define debug(x) (debug_printf x)
101 HWND hwnd
, statusbar
, cfgbox
;
103 HBITMAP bitmap
, prevbm
;
110 DWORD timer_last_tickcount
;
112 game_params
**presets
;
114 int nfonts
, fontsize
;
116 struct cfg_aux
*cfgaux
;
117 int cfg_which
, dlg_done
;
120 int help_has_contents
;
123 void fatal(char *fmt
, ...)
129 vsprintf(buf
, fmt
, ap
);
132 MessageBox(NULL
, buf
, "Fatal error", MB_ICONEXCLAMATION
| MB_OK
);
137 void get_random_seed(void **randseed
, int *randseedsize
)
139 time_t *tp
= snew(time_t);
141 *randseed
= (void *)tp
;
142 *randseedsize
= sizeof(time_t);
145 void status_bar(frontend
*fe
, char *text
)
147 SetWindowText(fe
->statusbar
, text
);
150 void frontend_default_colour(frontend
*fe
, float *output
)
152 DWORD c
= GetSysColor(COLOR_MENU
); /* ick */
154 output
[0] = (float)(GetRValue(c
) / 255.0);
155 output
[1] = (float)(GetGValue(c
) / 255.0);
156 output
[2] = (float)(GetBValue(c
) / 255.0);
159 void clip(frontend
*fe
, int x
, int y
, int w
, int h
)
161 IntersectClipRect(fe
->hdc_bm
, x
, y
, x
+w
, y
+h
);
164 void unclip(frontend
*fe
)
166 SelectClipRgn(fe
->hdc_bm
, NULL
);
169 void draw_text(frontend
*fe
, int x
, int y
, int fonttype
, int fontsize
,
170 int align
, int colour
, char *text
)
175 * Find or create the font.
177 for (i
= 0; i
< fe
->nfonts
; i
++)
178 if (fe
->fonts
[i
].type
== fonttype
&& fe
->fonts
[i
].size
== fontsize
)
181 if (i
== fe
->nfonts
) {
182 if (fe
->fontsize
<= fe
->nfonts
) {
183 fe
->fontsize
= fe
->nfonts
+ 10;
184 fe
->fonts
= sresize(fe
->fonts
, fe
->fontsize
, struct font
);
189 fe
->fonts
[i
].type
= fonttype
;
190 fe
->fonts
[i
].size
= fontsize
;
192 fe
->fonts
[i
].font
= CreateFont(-fontsize
, 0, 0, 0, FW_BOLD
,
193 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
194 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
196 (fonttype
== FONT_FIXED ?
197 FIXED_PITCH
| FF_DONTCARE
:
198 VARIABLE_PITCH
| FF_SWISS
),
203 * Position and draw the text.
210 oldfont
= SelectObject(fe
->hdc_bm
, fe
->fonts
[i
].font
);
211 if (GetTextMetrics(fe
->hdc_bm
, &tm
)) {
212 if (align
& ALIGN_VCENTRE
)
213 y
-= (tm
.tmAscent
+tm
.tmDescent
)/2;
217 if (GetTextExtentPoint32(fe
->hdc_bm
, text
, strlen(text
), &size
)) {
218 if (align
& ALIGN_HCENTRE
)
220 else if (align
& ALIGN_HRIGHT
)
223 SetBkMode(fe
->hdc_bm
, TRANSPARENT
);
224 SetTextColor(fe
->hdc_bm
, fe
->colours
[colour
]);
225 TextOut(fe
->hdc_bm
, x
, y
, text
, strlen(text
));
226 SelectObject(fe
->hdc_bm
, oldfont
);
230 void draw_rect(frontend
*fe
, int x
, int y
, int w
, int h
, int colour
)
232 if (w
== 1 && h
== 1) {
234 * Rectangle() appears to get uppity if asked to draw a 1x1
235 * rectangle, presumably on the grounds that that's beneath
236 * its dignity and you ought to be using SetPixel instead.
239 SetPixel(fe
->hdc_bm
, x
, y
, fe
->colours
[colour
]);
241 HBRUSH oldbrush
= SelectObject(fe
->hdc_bm
, fe
->brushes
[colour
]);
242 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
243 Rectangle(fe
->hdc_bm
, x
, y
, x
+w
, y
+h
);
244 SelectObject(fe
->hdc_bm
, oldbrush
);
245 SelectObject(fe
->hdc_bm
, oldpen
);
249 void draw_line(frontend
*fe
, int x1
, int y1
, int x2
, int y2
, int colour
)
251 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
252 MoveToEx(fe
->hdc_bm
, x1
, y1
, NULL
);
253 LineTo(fe
->hdc_bm
, x2
, y2
);
254 SetPixel(fe
->hdc_bm
, x2
, y2
, fe
->colours
[colour
]);
255 SelectObject(fe
->hdc_bm
, oldpen
);
258 void draw_polygon(frontend
*fe
, int *coords
, int npoints
,
259 int fill
, int colour
)
261 POINT
*pts
= snewn(npoints
+1, POINT
);
264 for (i
= 0; i
<= npoints
; i
++) {
265 int j
= (i
< npoints ? i
: 0);
266 pts
[i
].x
= coords
[j
*2];
267 pts
[i
].y
= coords
[j
*2+1];
271 HBRUSH oldbrush
= SelectObject(fe
->hdc_bm
, fe
->brushes
[colour
]);
272 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
273 Polygon(fe
->hdc_bm
, pts
, npoints
);
274 SelectObject(fe
->hdc_bm
, oldbrush
);
275 SelectObject(fe
->hdc_bm
, oldpen
);
277 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
278 Polyline(fe
->hdc_bm
, pts
, npoints
+1);
279 SelectObject(fe
->hdc_bm
, oldpen
);
285 void start_draw(frontend
*fe
)
288 hdc_win
= GetDC(fe
->hwnd
);
289 fe
->hdc_bm
= CreateCompatibleDC(hdc_win
);
290 fe
->prevbm
= SelectObject(fe
->hdc_bm
, fe
->bitmap
);
291 ReleaseDC(fe
->hwnd
, hdc_win
);
293 SetMapMode(fe
->hdc_bm
, MM_TEXT
);
296 void draw_update(frontend
*fe
, int x
, int y
, int w
, int h
)
305 InvalidateRect(fe
->hwnd
, &r
, FALSE
);
308 void end_draw(frontend
*fe
)
310 SelectObject(fe
->hdc_bm
, fe
->prevbm
);
311 DeleteDC(fe
->hdc_bm
);
313 DeleteObject(fe
->clip
);
318 void deactivate_timer(frontend
*fe
)
320 KillTimer(fe
->hwnd
, fe
->timer
);
324 void activate_timer(frontend
*fe
)
327 fe
->timer
= SetTimer(fe
->hwnd
, fe
->timer
, 20, NULL
);
328 fe
->timer_last_tickcount
= GetTickCount();
332 void write_clip(HWND hwnd
, char *data
)
340 * Windows expects CRLF in the clipboard, so we must convert
341 * any \n that has come out of the puzzle backend.
344 for (i
= 0; data
[i
]; i
++) {
349 data2
= snewn(len
+1, char);
351 for (i
= 0; data
[i
]; i
++) {
354 data2
[j
++] = data
[i
];
359 clipdata
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, len
+ 1);
362 lock
= GlobalLock(clipdata
);
365 memcpy(lock
, data2
, len
);
366 ((unsigned char *) lock
)[len
] = 0;
367 GlobalUnlock(clipdata
);
369 if (OpenClipboard(hwnd
)) {
371 SetClipboardData(CF_TEXT
, clipdata
);
374 GlobalFree(clipdata
);
380 * See if we can find a help file.
382 static void find_help_file(frontend
*fe
)
384 char b
[2048], *p
, *q
, *r
;
386 if (!fe
->help_path
) {
387 GetModuleFileName(NULL
, b
, sizeof(b
) - 1);
389 p
= strrchr(b
, '\\');
390 if (p
&& p
>= r
) r
= p
+1;
392 if (q
&& q
>= r
) r
= q
+1;
393 strcpy(r
, HELP_FILE_NAME
);
394 if ( (fp
= fopen(b
, "r")) != NULL
) {
395 fe
->help_path
= dupstr(b
);
398 fe
->help_path
= NULL
;
399 strcpy(r
, HELP_CNT_NAME
);
400 if ( (fp
= fopen(b
, "r")) != NULL
) {
401 fe
->help_has_contents
= TRUE
;
404 fe
->help_has_contents
= FALSE
;
408 static frontend
*new_window(HINSTANCE inst
, char *game_id
, char **error
)
417 fe
->me
= midend_new(fe
, &thegame
);
420 *error
= midend_game_id(fe
->me
, game_id
);
428 fe
->help_path
= NULL
;
432 midend_new_game(fe
->me
);
433 midend_size(fe
->me
, &x
, &y
);
438 fe
->nfonts
= fe
->fontsize
= 0;
444 colours
= midend_colours(fe
->me
, &ncolours
);
446 fe
->colours
= snewn(ncolours
, COLORREF
);
447 fe
->brushes
= snewn(ncolours
, HBRUSH
);
448 fe
->pens
= snewn(ncolours
, HPEN
);
450 for (i
= 0; i
< ncolours
; i
++) {
451 fe
->colours
[i
] = RGB(255 * colours
[i
*3+0],
452 255 * colours
[i
*3+1],
453 255 * colours
[i
*3+2]);
454 fe
->brushes
[i
] = CreateSolidBrush(fe
->colours
[i
]);
455 fe
->pens
[i
] = CreatePen(PS_SOLID
, 1, fe
->colours
[i
]);
462 AdjustWindowRectEx(&r
, WS_OVERLAPPEDWINDOW
&~
463 (WS_THICKFRAME
| WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
466 fe
->hwnd
= CreateWindowEx(0, thegame
.name
, thegame
.name
,
467 WS_OVERLAPPEDWINDOW
&~
468 (WS_THICKFRAME
| WS_MAXIMIZEBOX
),
469 CW_USEDEFAULT
, CW_USEDEFAULT
,
470 r
.right
- r
.left
, r
.bottom
- r
.top
,
471 NULL
, NULL
, inst
, NULL
);
474 HMENU bar
= CreateMenu();
475 HMENU menu
= CreateMenu();
477 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)menu
, "Game");
478 AppendMenu(menu
, MF_ENABLED
, IDM_NEW
, "New");
479 AppendMenu(menu
, MF_ENABLED
, IDM_RESTART
, "Restart");
480 AppendMenu(menu
, MF_ENABLED
, IDM_DESC
, "Specific...");
481 AppendMenu(menu
, MF_ENABLED
, IDM_SEED
, "Random Seed...");
483 if ((fe
->npresets
= midend_num_presets(fe
->me
)) > 0 ||
484 thegame
.can_configure
) {
485 HMENU sub
= CreateMenu();
488 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)sub
, "Type");
490 fe
->presets
= snewn(fe
->npresets
, game_params
*);
492 for (i
= 0; i
< fe
->npresets
; i
++) {
495 midend_fetch_preset(fe
->me
, i
, &name
, &fe
->presets
[i
]);
498 * FIXME: we ought to go through and do something
499 * with ampersands here.
502 AppendMenu(sub
, MF_ENABLED
, IDM_PRESETS
+ 0x10 * i
, name
);
505 if (thegame
.can_configure
) {
506 AppendMenu(sub
, MF_ENABLED
, IDM_CONFIG
, "Custom...");
510 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
511 AppendMenu(menu
, MF_ENABLED
, IDM_UNDO
, "Undo");
512 AppendMenu(menu
, MF_ENABLED
, IDM_REDO
, "Redo");
513 if (thegame
.can_format_as_text
) {
514 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
515 AppendMenu(menu
, MF_ENABLED
, IDM_COPY
, "Copy");
517 if (thegame
.can_solve
) {
518 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
519 AppendMenu(menu
, MF_ENABLED
, IDM_SOLVE
, "Solve");
521 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
522 AppendMenu(menu
, MF_ENABLED
, IDM_QUIT
, "Exit");
524 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)menu
, "Help");
525 AppendMenu(menu
, MF_ENABLED
, IDM_ABOUT
, "About");
527 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
528 AppendMenu(menu
, MF_ENABLED
, IDM_HELPC
, "Contents");
529 if (thegame
.winhelp_topic
) {
531 assert(thegame
.name
);
532 item
= snewn(9+strlen(thegame
.name
), char); /*ick*/
533 sprintf(item
, "Help on %s", thegame
.name
);
534 AppendMenu(menu
, MF_ENABLED
, IDM_GAMEHELP
, item
);
538 SetMenu(fe
->hwnd
, bar
);
541 if (midend_wants_statusbar(fe
->me
)) {
542 fe
->statusbar
= CreateWindowEx(0, STATUSCLASSNAME
, "ooh",
543 WS_CHILD
| WS_VISIBLE
,
544 0, 0, 0, 0, /* status bar does these */
545 fe
->hwnd
, NULL
, inst
, NULL
);
546 GetWindowRect(fe
->statusbar
, &sr
);
547 SetWindowPos(fe
->hwnd
, NULL
, 0, 0,
548 r
.right
- r
.left
, r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
549 SWP_NOMOVE
| SWP_NOZORDER
);
550 SetWindowPos(fe
->statusbar
, NULL
, 0, y
, x
, sr
.bottom
- sr
.top
,
553 fe
->statusbar
= NULL
;
556 hdc
= GetDC(fe
->hwnd
);
557 fe
->bitmap
= CreateCompatibleBitmap(hdc
, x
, y
);
558 ReleaseDC(fe
->hwnd
, hdc
);
560 SetWindowLong(fe
->hwnd
, GWL_USERDATA
, (LONG
)fe
);
562 ShowWindow(fe
->hwnd
, SW_NORMAL
);
563 SetForegroundWindow(fe
->hwnd
);
565 midend_redraw(fe
->me
);
570 static int CALLBACK
AboutDlgProc(HWND hwnd
, UINT msg
,
571 WPARAM wParam
, LPARAM lParam
)
573 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
580 if ((HIWORD(wParam
) == BN_CLICKED
||
581 HIWORD(wParam
) == BN_DOUBLECLICKED
) &&
582 LOWORD(wParam
) == IDOK
)
594 static int CALLBACK
ConfigDlgProc(HWND hwnd
, UINT msg
,
595 WPARAM wParam
, LPARAM lParam
)
597 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
607 * OK and Cancel are special cases.
609 if ((HIWORD(wParam
) == BN_CLICKED
||
610 HIWORD(wParam
) == BN_DOUBLECLICKED
) &&
611 (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)) {
612 if (LOWORD(wParam
) == IDOK
) {
613 char *err
= midend_set_config(fe
->me
, fe
->cfg_which
, fe
->cfg
);
616 MessageBox(hwnd
, err
, "Validation error",
617 MB_ICONERROR
| MB_OK
);
628 * First find the control whose id this is.
630 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
631 if (j
->ctlid
== LOWORD(wParam
))
634 if (i
->type
== C_END
)
635 return 0; /* not our problem */
637 if (i
->type
== C_STRING
&& HIWORD(wParam
) == EN_CHANGE
) {
639 GetDlgItemText(fe
->cfgbox
, j
->ctlid
, buffer
, lenof(buffer
));
640 buffer
[lenof(buffer
)-1] = '\0';
642 i
->sval
= dupstr(buffer
);
643 } else if (i
->type
== C_BOOLEAN
&&
644 (HIWORD(wParam
) == BN_CLICKED
||
645 HIWORD(wParam
) == BN_DOUBLECLICKED
)) {
646 i
->ival
= IsDlgButtonChecked(fe
->cfgbox
, j
->ctlid
);
647 } else if (i
->type
== C_CHOICES
&&
648 HIWORD(wParam
) == CBN_SELCHANGE
) {
649 i
->ival
= SendDlgItemMessage(fe
->cfgbox
, j
->ctlid
,
663 HWND
mkctrl(frontend
*fe
, int x1
, int x2
, int y1
, int y2
,
664 char *wclass
, int wstyle
,
665 int exstyle
, const char *wtext
, int wid
)
668 ret
= CreateWindowEx(exstyle
, wclass
, wtext
,
669 wstyle
| WS_CHILD
| WS_VISIBLE
, x1
, y1
, x2
-x1
, y2
-y1
,
670 fe
->cfgbox
, (HMENU
) wid
, fe
->inst
, NULL
);
671 SendMessage(ret
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, MAKELPARAM(TRUE
, 0));
675 static void about(frontend
*fe
)
685 int winwidth
, winheight
, y
;
686 int height
, width
, maxwid
;
687 const char *strings
[16];
692 sprintf(titlebuf
, "About %.250s", thegame
.name
);
694 strings
[nstrings
++] = thegame
.name
;
695 strings
[nstrings
++] = "from Simon Tatham's Portable Puzzle Collection";
696 strings
[nstrings
++] = ver
;
698 wc
.style
= CS_DBLCLKS
| CS_SAVEBITS
| CS_BYTEALIGNWINDOW
;
699 wc
.lpfnWndProc
= DefDlgProc
;
701 wc
.cbWndExtra
= DLGWINDOWEXTRA
+ 8;
702 wc
.hInstance
= fe
->inst
;
704 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
705 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+1);
706 wc
.lpszMenuName
= NULL
;
707 wc
.lpszClassName
= "GameAboutBox";
710 hdc
= GetDC(fe
->hwnd
);
711 SetMapMode(hdc
, MM_TEXT
);
713 fe
->dlg_done
= FALSE
;
715 fe
->cfgfont
= CreateFont(-MulDiv(8, GetDeviceCaps(hdc
, LOGPIXELSY
), 72),
717 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
718 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
723 oldfont
= SelectObject(hdc
, fe
->cfgfont
);
724 if (GetTextMetrics(hdc
, &tm
)) {
725 height
= tm
.tmAscent
+ tm
.tmDescent
;
726 width
= tm
.tmAveCharWidth
;
732 * Figure out the layout of the About box by measuring the
733 * length of each piece of text.
736 winheight
= height
/2;
738 for (i
= 0; i
< nstrings
; i
++) {
739 if (GetTextExtentPoint32(hdc
, strings
[i
], strlen(strings
[i
]), &size
))
740 lengths
[i
] = size
.cx
;
742 lengths
[i
] = 0; /* *shrug* */
743 if (maxwid
< lengths
[i
])
745 winheight
+= height
* 3 / 2 + (height
/ 2);
748 winheight
+= height
+ height
* 7 / 4; /* OK button */
749 winwidth
= maxwid
+ 4*width
;
751 SelectObject(hdc
, oldfont
);
752 ReleaseDC(fe
->hwnd
, hdc
);
755 * Create the dialog, now that we know its size.
762 r
.bottom
= winheight
;
764 AdjustWindowRectEx(&r
, (WS_OVERLAPPEDWINDOW
/*|
765 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
766 WS_CAPTION | WS_SYSMENU*/) &~
767 (WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
771 * Centre the dialog on its parent window.
775 GetWindowRect(fe
->hwnd
, &r2
);
776 r
.left
= (r2
.left
+ r2
.right
- r
.right
) / 2;
777 r
.top
= (r2
.top
+ r2
.bottom
- r
.bottom
) / 2;
781 fe
->cfgbox
= CreateWindowEx(0, wc
.lpszClassName
, titlebuf
,
782 DS_MODALFRAME
| WS_POPUP
| WS_VISIBLE
|
783 WS_CAPTION
| WS_SYSMENU
,
785 r
.right
-r
.left
, r
.bottom
-r
.top
,
786 fe
->hwnd
, NULL
, fe
->inst
, NULL
);
789 SendMessage(fe
->cfgbox
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, FALSE
);
791 SetWindowLong(fe
->cfgbox
, GWL_USERDATA
, (LONG
)fe
);
792 SetWindowLong(fe
->cfgbox
, DWL_DLGPROC
, (LONG
)AboutDlgProc
);
796 for (i
= 0; i
< nstrings
; i
++) {
797 int border
= width
*2 + (maxwid
- lengths
[i
]) / 2;
798 mkctrl(fe
, border
, border
+lengths
[i
], y
+height
*1/8, y
+height
*9/8,
799 "Static", 0, 0, strings
[i
], id
++);
802 assert(y
< winheight
);
806 y
+= height
/2; /* extra space before OK */
807 mkctrl(fe
, width
*2, maxwid
+width
*2, y
, y
+height
*7/4, "BUTTON",
808 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
| BS_DEFPUSHBUTTON
, 0,
811 SendMessage(fe
->cfgbox
, WM_INITDIALOG
, 0, 0);
813 EnableWindow(fe
->hwnd
, FALSE
);
814 ShowWindow(fe
->cfgbox
, SW_NORMAL
);
815 while ((gm
=GetMessage(&msg
, NULL
, 0, 0)) > 0) {
816 if (!IsDialogMessage(fe
->cfgbox
, &msg
))
817 DispatchMessage(&msg
);
821 EnableWindow(fe
->hwnd
, TRUE
);
822 SetForegroundWindow(fe
->hwnd
);
823 DestroyWindow(fe
->cfgbox
);
824 DeleteObject(fe
->cfgfont
);
827 static int get_config(frontend
*fe
, int which
)
840 int winwidth
, winheight
, col1l
, col1r
, col2l
, col2r
, y
;
841 int height
, width
, maxlabel
, maxcheckbox
;
843 wc
.style
= CS_DBLCLKS
| CS_SAVEBITS
| CS_BYTEALIGNWINDOW
;
844 wc
.lpfnWndProc
= DefDlgProc
;
846 wc
.cbWndExtra
= DLGWINDOWEXTRA
+ 8;
847 wc
.hInstance
= fe
->inst
;
849 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
850 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+1);
851 wc
.lpszMenuName
= NULL
;
852 wc
.lpszClassName
= "GameConfigBox";
855 hdc
= GetDC(fe
->hwnd
);
856 SetMapMode(hdc
, MM_TEXT
);
858 fe
->dlg_done
= FALSE
;
860 fe
->cfgfont
= CreateFont(-MulDiv(8, GetDeviceCaps(hdc
, LOGPIXELSY
), 72),
862 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
863 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
868 oldfont
= SelectObject(hdc
, fe
->cfgfont
);
869 if (GetTextMetrics(hdc
, &tm
)) {
870 height
= tm
.tmAscent
+ tm
.tmDescent
;
871 width
= tm
.tmAveCharWidth
;
876 fe
->cfg
= midend_get_config(fe
->me
, which
, &title
);
877 fe
->cfg_which
= which
;
880 * Figure out the layout of the config box by measuring the
881 * length of each piece of text.
883 maxlabel
= maxcheckbox
= 0;
884 winheight
= height
/2;
886 for (i
= fe
->cfg
; i
->type
!= C_END
; i
++) {
891 * Both these control types have a label filling only
892 * the left-hand column of the box.
894 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
897 winheight
+= height
* 3 / 2 + (height
/ 2);
902 * Checkboxes take up the whole of the box width.
904 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
905 maxcheckbox
< size
.cx
)
906 maxcheckbox
= size
.cx
;
907 winheight
+= height
+ (height
/ 2);
912 winheight
+= height
+ height
* 7 / 4; /* OK / Cancel buttons */
915 col1r
= col1l
+ maxlabel
;
916 col2l
= col1r
+ 2*width
;
917 col2r
= col2l
+ 30*width
;
918 if (col2r
< col1l
+2*height
+maxcheckbox
)
919 col2r
= col1l
+2*height
+maxcheckbox
;
920 winwidth
= col2r
+ 2*width
;
922 SelectObject(hdc
, oldfont
);
923 ReleaseDC(fe
->hwnd
, hdc
);
926 * Create the dialog, now that we know its size.
933 r
.bottom
= winheight
;
935 AdjustWindowRectEx(&r
, (WS_OVERLAPPEDWINDOW
/*|
936 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
937 WS_CAPTION | WS_SYSMENU*/) &~
938 (WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
942 * Centre the dialog on its parent window.
946 GetWindowRect(fe
->hwnd
, &r2
);
947 r
.left
= (r2
.left
+ r2
.right
- r
.right
) / 2;
948 r
.top
= (r2
.top
+ r2
.bottom
- r
.bottom
) / 2;
952 fe
->cfgbox
= CreateWindowEx(0, wc
.lpszClassName
, title
,
953 DS_MODALFRAME
| WS_POPUP
| WS_VISIBLE
|
954 WS_CAPTION
| WS_SYSMENU
,
956 r
.right
-r
.left
, r
.bottom
-r
.top
,
957 fe
->hwnd
, NULL
, fe
->inst
, NULL
);
961 SendMessage(fe
->cfgbox
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, FALSE
);
963 SetWindowLong(fe
->cfgbox
, GWL_USERDATA
, (LONG
)fe
);
964 SetWindowLong(fe
->cfgbox
, DWL_DLGPROC
, (LONG
)ConfigDlgProc
);
967 * Count the controls so we can allocate cfgaux.
969 for (nctrls
= 0, i
= fe
->cfg
; i
->type
!= C_END
; i
++)
971 fe
->cfgaux
= snewn(nctrls
, struct cfg_aux
);
975 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
979 * Edit box with a label beside it.
981 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
982 "Static", 0, 0, i
->name
, id
++);
983 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*3/2,
984 "EDIT", WS_TABSTOP
| ES_AUTOHSCROLL
,
985 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
986 SetWindowText(ctl
, i
->sval
);
994 mkctrl(fe
, col1l
, col2r
, y
, y
+height
, "BUTTON",
995 BS_NOTIFY
| BS_AUTOCHECKBOX
| WS_TABSTOP
,
996 0, i
->name
, (j
->ctlid
= id
++));
997 CheckDlgButton(fe
->cfgbox
, j
->ctlid
, (i
->ival
!= 0));
1003 * Drop-down list with a label beside it.
1005 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
1006 "STATIC", 0, 0, i
->name
, id
++);
1007 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*41/2,
1008 "COMBOBOX", WS_TABSTOP
|
1009 CBS_DROPDOWNLIST
| CBS_HASSTRINGS
,
1010 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
1012 char c
, *p
, *q
, *str
;
1014 SendMessage(ctl
, CB_RESETCONTENT
, 0, 0);
1019 while (*q
&& *q
!= c
) q
++;
1020 str
= snewn(q
-p
+1, char);
1021 strncpy(str
, p
, q
-p
);
1023 SendMessage(ctl
, CB_ADDSTRING
, 0, (LPARAM
)str
);
1030 SendMessage(ctl
, CB_SETCURSEL
, i
->ival
, 0);
1036 assert(y
< winheight
);
1040 y
+= height
/2; /* extra space before OK and Cancel */
1041 mkctrl(fe
, col1l
, (col1l
+col2r
)/2-width
, y
, y
+height
*7/4, "BUTTON",
1042 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
| BS_DEFPUSHBUTTON
, 0,
1044 mkctrl(fe
, (col1l
+col2r
)/2+width
, col2r
, y
, y
+height
*7/4, "BUTTON",
1045 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
, 0, "Cancel", IDCANCEL
);
1047 SendMessage(fe
->cfgbox
, WM_INITDIALOG
, 0, 0);
1049 EnableWindow(fe
->hwnd
, FALSE
);
1050 ShowWindow(fe
->cfgbox
, SW_NORMAL
);
1051 while ((gm
=GetMessage(&msg
, NULL
, 0, 0)) > 0) {
1052 if (!IsDialogMessage(fe
->cfgbox
, &msg
))
1053 DispatchMessage(&msg
);
1057 EnableWindow(fe
->hwnd
, TRUE
);
1058 SetForegroundWindow(fe
->hwnd
);
1059 DestroyWindow(fe
->cfgbox
);
1060 DeleteObject(fe
->cfgfont
);
1065 return (fe
->dlg_done
== 2);
1068 static void new_game_type(frontend
*fe
)
1074 midend_new_game(fe
->me
);
1075 midend_size(fe
->me
, &x
, &y
);
1080 AdjustWindowRectEx(&r
, WS_OVERLAPPEDWINDOW
&~
1081 (WS_THICKFRAME
| WS_MAXIMIZEBOX
|
1085 if (fe
->statusbar
!= NULL
) {
1086 GetWindowRect(fe
->statusbar
, &sr
);
1088 sr
.left
= sr
.right
= sr
.top
= sr
.bottom
= 0;
1090 SetWindowPos(fe
->hwnd
, NULL
, 0, 0,
1092 r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
1093 SWP_NOMOVE
| SWP_NOZORDER
);
1094 if (fe
->statusbar
!= NULL
)
1095 SetWindowPos(fe
->statusbar
, NULL
, 0, y
, x
,
1096 sr
.bottom
- sr
.top
, SWP_NOZORDER
);
1098 DeleteObject(fe
->bitmap
);
1100 hdc
= GetDC(fe
->hwnd
);
1101 fe
->bitmap
= CreateCompatibleBitmap(hdc
, x
, y
);
1102 ReleaseDC(fe
->hwnd
, hdc
);
1104 midend_redraw(fe
->me
);
1107 static LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
,
1108 WPARAM wParam
, LPARAM lParam
)
1110 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
1114 DestroyWindow(hwnd
);
1117 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
1119 if (!midend_process_key(fe
->me
, 0, 0, 'n'))
1123 if (!midend_process_key(fe
->me
, 0, 0, 'r'))
1127 if (!midend_process_key(fe
->me
, 0, 0, 'u'))
1131 if (!midend_process_key(fe
->me
, 0, 0, '\x12'))
1136 char *text
= midend_text_format(fe
->me
);
1138 write_clip(hwnd
, text
);
1140 MessageBeep(MB_ICONWARNING
);
1146 char *msg
= midend_solve(fe
->me
);
1148 MessageBox(hwnd
, msg
, "Unable to solve",
1149 MB_ICONERROR
| MB_OK
);
1153 if (!midend_process_key(fe
->me
, 0, 0, 'q'))
1157 if (get_config(fe
, CFG_SETTINGS
))
1161 if (get_config(fe
, CFG_SEED
))
1165 if (get_config(fe
, CFG_DESC
))
1172 assert(fe
->help_path
);
1173 WinHelp(hwnd
, fe
->help_path
,
1174 fe
->help_has_contents ? HELP_FINDER
: HELP_CONTENTS
, 0);
1177 assert(fe
->help_path
);
1178 assert(thegame
.winhelp_topic
);
1180 char *cmd
= snewn(10+strlen(thegame
.winhelp_topic
), char);
1181 sprintf(cmd
, "JI(`',`%s')", thegame
.winhelp_topic
);
1182 WinHelp(hwnd
, fe
->help_path
, HELP_COMMAND
, (DWORD
)cmd
);
1188 int p
= ((wParam
&~ 0xF) - IDM_PRESETS
) / 0x10;
1190 if (p
>= 0 && p
< fe
->npresets
) {
1191 midend_set_params(fe
->me
, fe
->presets
[p
]);
1207 hdc
= BeginPaint(hwnd
, &p
);
1208 hdc2
= CreateCompatibleDC(hdc
);
1209 prevbm
= SelectObject(hdc2
, fe
->bitmap
);
1211 p
.rcPaint
.left
, p
.rcPaint
.top
,
1212 p
.rcPaint
.right
- p
.rcPaint
.left
,
1213 p
.rcPaint
.bottom
- p
.rcPaint
.top
,
1215 p
.rcPaint
.left
, p
.rcPaint
.top
,
1217 SelectObject(hdc2
, prevbm
);
1228 if (!(lParam
& 0x01000000))
1229 key
= MOD_NUM_KEYPAD
| '4';
1234 if (!(lParam
& 0x01000000))
1235 key
= MOD_NUM_KEYPAD
| '6';
1240 if (!(lParam
& 0x01000000))
1241 key
= MOD_NUM_KEYPAD
| '8';
1246 if (!(lParam
& 0x01000000))
1247 key
= MOD_NUM_KEYPAD
| '2';
1252 * Diagonal keys on the numeric keypad.
1255 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '9';
1258 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '3';
1261 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '7';
1264 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '1';
1267 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '0';
1270 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '5';
1273 * Numeric keypad keys with Num Lock on.
1275 case VK_NUMPAD4
: key
= MOD_NUM_KEYPAD
| '4'; break;
1276 case VK_NUMPAD6
: key
= MOD_NUM_KEYPAD
| '6'; break;
1277 case VK_NUMPAD8
: key
= MOD_NUM_KEYPAD
| '8'; break;
1278 case VK_NUMPAD2
: key
= MOD_NUM_KEYPAD
| '2'; break;
1279 case VK_NUMPAD5
: key
= MOD_NUM_KEYPAD
| '5'; break;
1280 case VK_NUMPAD9
: key
= MOD_NUM_KEYPAD
| '9'; break;
1281 case VK_NUMPAD3
: key
= MOD_NUM_KEYPAD
| '3'; break;
1282 case VK_NUMPAD7
: key
= MOD_NUM_KEYPAD
| '7'; break;
1283 case VK_NUMPAD1
: key
= MOD_NUM_KEYPAD
| '1'; break;
1284 case VK_NUMPAD0
: key
= MOD_NUM_KEYPAD
| '0'; break;
1288 if (!midend_process_key(fe
->me
, 0, 0, key
))
1293 m
.message
= WM_KEYDOWN
;
1295 m
.lParam
= lParam
& 0xdfff;
1296 TranslateMessage(&m
);
1300 case WM_LBUTTONDOWN
:
1301 case WM_RBUTTONDOWN
:
1302 case WM_MBUTTONDOWN
:
1307 * Shift-clicks count as middle-clicks, since otherwise
1308 * two-button Windows users won't have any kind of
1309 * middle click to use.
1311 if (message
== WM_MBUTTONDOWN
|| (wParam
& MK_SHIFT
))
1312 button
= MIDDLE_BUTTON
;
1313 else if (message
== WM_LBUTTONDOWN
)
1314 button
= LEFT_BUTTON
;
1316 button
= RIGHT_BUTTON
;
1318 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1319 (signed short)HIWORD(lParam
), button
))
1332 * Shift-clicks count as middle-clicks, since otherwise
1333 * two-button Windows users won't have any kind of
1334 * middle click to use.
1336 if (message
== WM_MBUTTONUP
|| (wParam
& MK_SHIFT
))
1337 button
= MIDDLE_RELEASE
;
1338 else if (message
== WM_LBUTTONUP
)
1339 button
= LEFT_RELEASE
;
1341 button
= RIGHT_RELEASE
;
1343 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1344 (signed short)HIWORD(lParam
), button
))
1354 if (wParam
& (MK_MBUTTON
| MK_SHIFT
))
1355 button
= MIDDLE_DRAG
;
1356 else if (wParam
& MK_LBUTTON
)
1359 button
= RIGHT_DRAG
;
1361 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1362 (signed short)HIWORD(lParam
), button
))
1367 if (!midend_process_key(fe
->me
, 0, 0, (unsigned char)wParam
))
1372 DWORD now
= GetTickCount();
1373 float elapsed
= (float) (now
- fe
->timer_last_tickcount
) * 0.001F
;
1374 midend_timer(fe
->me
, elapsed
);
1375 fe
->timer_last_tickcount
= now
;
1380 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
1383 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
)
1388 InitCommonControls();
1394 wndclass
.lpfnWndProc
= WndProc
;
1395 wndclass
.cbClsExtra
= 0;
1396 wndclass
.cbWndExtra
= 0;
1397 wndclass
.hInstance
= inst
;
1398 wndclass
.hIcon
= LoadIcon(inst
, IDI_APPLICATION
);
1399 wndclass
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
1400 wndclass
.hbrBackground
= NULL
;
1401 wndclass
.lpszMenuName
= NULL
;
1402 wndclass
.lpszClassName
= thegame
.name
;
1404 RegisterClass(&wndclass
);
1407 while (*cmdline
&& isspace(*cmdline
))
1410 if (!new_window(inst
, *cmdline ? cmdline
: NULL
, &error
)) {
1412 sprintf(buf
, "%.100s Error", thegame
.name
);
1413 MessageBox(NULL
, error
, buf
, MB_OK
|MB_ICONERROR
);
1417 while (GetMessage(&msg
, NULL
, 0, 0)) {
1418 DispatchMessage(&msg
);