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
);
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
;
124 void fatal(char *fmt
, ...)
130 vsprintf(buf
, fmt
, ap
);
133 MessageBox(NULL
, buf
, "Fatal error", MB_ICONEXCLAMATION
| MB_OK
);
138 void get_random_seed(void **randseed
, int *randseedsize
)
140 time_t *tp
= snew(time_t);
142 *randseed
= (void *)tp
;
143 *randseedsize
= sizeof(time_t);
146 void status_bar(frontend
*fe
, char *text
)
148 char *rewritten
= midend_rewrite_statusbar(fe
->me
, text
);
149 if (!fe
->laststatus
|| strcmp(rewritten
, fe
->laststatus
)) {
150 SetWindowText(fe
->statusbar
, rewritten
);
151 sfree(fe
->laststatus
);
152 fe
->laststatus
= rewritten
;
158 void frontend_default_colour(frontend
*fe
, float *output
)
160 DWORD c
= GetSysColor(COLOR_MENU
); /* ick */
162 output
[0] = (float)(GetRValue(c
) / 255.0);
163 output
[1] = (float)(GetGValue(c
) / 255.0);
164 output
[2] = (float)(GetBValue(c
) / 255.0);
167 void clip(frontend
*fe
, int x
, int y
, int w
, int h
)
169 IntersectClipRect(fe
->hdc_bm
, x
, y
, x
+w
, y
+h
);
172 void unclip(frontend
*fe
)
174 SelectClipRgn(fe
->hdc_bm
, NULL
);
177 void draw_text(frontend
*fe
, int x
, int y
, int fonttype
, int fontsize
,
178 int align
, int colour
, char *text
)
183 * Find or create the font.
185 for (i
= 0; i
< fe
->nfonts
; i
++)
186 if (fe
->fonts
[i
].type
== fonttype
&& fe
->fonts
[i
].size
== fontsize
)
189 if (i
== fe
->nfonts
) {
190 if (fe
->fontsize
<= fe
->nfonts
) {
191 fe
->fontsize
= fe
->nfonts
+ 10;
192 fe
->fonts
= sresize(fe
->fonts
, fe
->fontsize
, struct font
);
197 fe
->fonts
[i
].type
= fonttype
;
198 fe
->fonts
[i
].size
= fontsize
;
200 fe
->fonts
[i
].font
= CreateFont(-fontsize
, 0, 0, 0, FW_BOLD
,
201 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
202 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
204 (fonttype
== FONT_FIXED ?
205 FIXED_PITCH
| FF_DONTCARE
:
206 VARIABLE_PITCH
| FF_SWISS
),
211 * Position and draw the text.
218 oldfont
= SelectObject(fe
->hdc_bm
, fe
->fonts
[i
].font
);
219 if (GetTextMetrics(fe
->hdc_bm
, &tm
)) {
220 if (align
& ALIGN_VCENTRE
)
221 y
-= (tm
.tmAscent
+tm
.tmDescent
)/2;
225 if (GetTextExtentPoint32(fe
->hdc_bm
, text
, strlen(text
), &size
)) {
226 if (align
& ALIGN_HCENTRE
)
228 else if (align
& ALIGN_HRIGHT
)
231 SetBkMode(fe
->hdc_bm
, TRANSPARENT
);
232 SetTextColor(fe
->hdc_bm
, fe
->colours
[colour
]);
233 TextOut(fe
->hdc_bm
, x
, y
, text
, strlen(text
));
234 SelectObject(fe
->hdc_bm
, oldfont
);
238 void draw_rect(frontend
*fe
, int x
, int y
, int w
, int h
, int colour
)
240 if (w
== 1 && h
== 1) {
242 * Rectangle() appears to get uppity if asked to draw a 1x1
243 * rectangle, presumably on the grounds that that's beneath
244 * its dignity and you ought to be using SetPixel instead.
247 SetPixel(fe
->hdc_bm
, x
, y
, fe
->colours
[colour
]);
249 HBRUSH oldbrush
= SelectObject(fe
->hdc_bm
, fe
->brushes
[colour
]);
250 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
251 Rectangle(fe
->hdc_bm
, x
, y
, x
+w
, y
+h
);
252 SelectObject(fe
->hdc_bm
, oldbrush
);
253 SelectObject(fe
->hdc_bm
, oldpen
);
257 void draw_line(frontend
*fe
, int x1
, int y1
, int x2
, int y2
, int colour
)
259 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
260 MoveToEx(fe
->hdc_bm
, x1
, y1
, NULL
);
261 LineTo(fe
->hdc_bm
, x2
, y2
);
262 SetPixel(fe
->hdc_bm
, x2
, y2
, fe
->colours
[colour
]);
263 SelectObject(fe
->hdc_bm
, oldpen
);
266 void draw_polygon(frontend
*fe
, int *coords
, int npoints
,
267 int fill
, int colour
)
269 POINT
*pts
= snewn(npoints
+1, POINT
);
272 for (i
= 0; i
<= npoints
; i
++) {
273 int j
= (i
< npoints ? i
: 0);
274 pts
[i
].x
= coords
[j
*2];
275 pts
[i
].y
= coords
[j
*2+1];
279 HBRUSH oldbrush
= SelectObject(fe
->hdc_bm
, fe
->brushes
[colour
]);
280 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
281 Polygon(fe
->hdc_bm
, pts
, npoints
);
282 SelectObject(fe
->hdc_bm
, oldbrush
);
283 SelectObject(fe
->hdc_bm
, oldpen
);
285 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
286 Polyline(fe
->hdc_bm
, pts
, npoints
+1);
287 SelectObject(fe
->hdc_bm
, oldpen
);
293 void start_draw(frontend
*fe
)
296 hdc_win
= GetDC(fe
->hwnd
);
297 fe
->hdc_bm
= CreateCompatibleDC(hdc_win
);
298 fe
->prevbm
= SelectObject(fe
->hdc_bm
, fe
->bitmap
);
299 ReleaseDC(fe
->hwnd
, hdc_win
);
301 SetMapMode(fe
->hdc_bm
, MM_TEXT
);
304 void draw_update(frontend
*fe
, int x
, int y
, int w
, int h
)
313 InvalidateRect(fe
->hwnd
, &r
, FALSE
);
316 void end_draw(frontend
*fe
)
318 SelectObject(fe
->hdc_bm
, fe
->prevbm
);
319 DeleteDC(fe
->hdc_bm
);
321 DeleteObject(fe
->clip
);
326 void deactivate_timer(frontend
*fe
)
328 KillTimer(fe
->hwnd
, fe
->timer
);
332 void activate_timer(frontend
*fe
)
335 fe
->timer
= SetTimer(fe
->hwnd
, fe
->timer
, 20, NULL
);
336 fe
->timer_last_tickcount
= GetTickCount();
340 void write_clip(HWND hwnd
, char *data
)
348 * Windows expects CRLF in the clipboard, so we must convert
349 * any \n that has come out of the puzzle backend.
352 for (i
= 0; data
[i
]; i
++) {
357 data2
= snewn(len
+1, char);
359 for (i
= 0; data
[i
]; i
++) {
362 data2
[j
++] = data
[i
];
367 clipdata
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, len
+ 1);
370 lock
= GlobalLock(clipdata
);
373 memcpy(lock
, data2
, len
);
374 ((unsigned char *) lock
)[len
] = 0;
375 GlobalUnlock(clipdata
);
377 if (OpenClipboard(hwnd
)) {
379 SetClipboardData(CF_TEXT
, clipdata
);
382 GlobalFree(clipdata
);
388 * See if we can find a help file.
390 static void find_help_file(frontend
*fe
)
392 char b
[2048], *p
, *q
, *r
;
394 if (!fe
->help_path
) {
395 GetModuleFileName(NULL
, b
, sizeof(b
) - 1);
397 p
= strrchr(b
, '\\');
398 if (p
&& p
>= r
) r
= p
+1;
400 if (q
&& q
>= r
) r
= q
+1;
401 strcpy(r
, HELP_FILE_NAME
);
402 if ( (fp
= fopen(b
, "r")) != NULL
) {
403 fe
->help_path
= dupstr(b
);
406 fe
->help_path
= NULL
;
407 strcpy(r
, HELP_CNT_NAME
);
408 if ( (fp
= fopen(b
, "r")) != NULL
) {
409 fe
->help_has_contents
= TRUE
;
412 fe
->help_has_contents
= FALSE
;
416 static frontend
*new_window(HINSTANCE inst
, char *game_id
, char **error
)
425 fe
->me
= midend_new(fe
, &thegame
);
428 *error
= midend_game_id(fe
->me
, game_id
);
436 fe
->help_path
= NULL
;
440 midend_new_game(fe
->me
);
441 midend_size(fe
->me
, &x
, &y
);
446 fe
->nfonts
= fe
->fontsize
= 0;
448 fe
->laststatus
= NULL
;
454 colours
= midend_colours(fe
->me
, &ncolours
);
456 fe
->colours
= snewn(ncolours
, COLORREF
);
457 fe
->brushes
= snewn(ncolours
, HBRUSH
);
458 fe
->pens
= snewn(ncolours
, HPEN
);
460 for (i
= 0; i
< ncolours
; i
++) {
461 fe
->colours
[i
] = RGB(255 * colours
[i
*3+0],
462 255 * colours
[i
*3+1],
463 255 * colours
[i
*3+2]);
464 fe
->brushes
[i
] = CreateSolidBrush(fe
->colours
[i
]);
465 fe
->pens
[i
] = CreatePen(PS_SOLID
, 1, fe
->colours
[i
]);
472 AdjustWindowRectEx(&r
, WS_OVERLAPPEDWINDOW
&~
473 (WS_THICKFRAME
| WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
476 fe
->hwnd
= CreateWindowEx(0, thegame
.name
, thegame
.name
,
477 WS_OVERLAPPEDWINDOW
&~
478 (WS_THICKFRAME
| WS_MAXIMIZEBOX
),
479 CW_USEDEFAULT
, CW_USEDEFAULT
,
480 r
.right
- r
.left
, r
.bottom
- r
.top
,
481 NULL
, NULL
, inst
, NULL
);
484 HMENU bar
= CreateMenu();
485 HMENU menu
= CreateMenu();
487 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)menu
, "Game");
488 AppendMenu(menu
, MF_ENABLED
, IDM_NEW
, "New");
489 AppendMenu(menu
, MF_ENABLED
, IDM_RESTART
, "Restart");
490 AppendMenu(menu
, MF_ENABLED
, IDM_DESC
, "Specific...");
491 AppendMenu(menu
, MF_ENABLED
, IDM_SEED
, "Random Seed...");
493 if ((fe
->npresets
= midend_num_presets(fe
->me
)) > 0 ||
494 thegame
.can_configure
) {
495 HMENU sub
= CreateMenu();
498 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)sub
, "Type");
500 fe
->presets
= snewn(fe
->npresets
, game_params
*);
502 for (i
= 0; i
< fe
->npresets
; i
++) {
505 midend_fetch_preset(fe
->me
, i
, &name
, &fe
->presets
[i
]);
508 * FIXME: we ought to go through and do something
509 * with ampersands here.
512 AppendMenu(sub
, MF_ENABLED
, IDM_PRESETS
+ 0x10 * i
, name
);
515 if (thegame
.can_configure
) {
516 AppendMenu(sub
, MF_ENABLED
, IDM_CONFIG
, "Custom...");
520 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
521 AppendMenu(menu
, MF_ENABLED
, IDM_UNDO
, "Undo");
522 AppendMenu(menu
, MF_ENABLED
, IDM_REDO
, "Redo");
523 if (thegame
.can_format_as_text
) {
524 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
525 AppendMenu(menu
, MF_ENABLED
, IDM_COPY
, "Copy");
527 if (thegame
.can_solve
) {
528 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
529 AppendMenu(menu
, MF_ENABLED
, IDM_SOLVE
, "Solve");
531 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
532 AppendMenu(menu
, MF_ENABLED
, IDM_QUIT
, "Exit");
534 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)menu
, "Help");
535 AppendMenu(menu
, MF_ENABLED
, IDM_ABOUT
, "About");
537 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
538 AppendMenu(menu
, MF_ENABLED
, IDM_HELPC
, "Contents");
539 if (thegame
.winhelp_topic
) {
541 assert(thegame
.name
);
542 item
= snewn(9+strlen(thegame
.name
), char); /*ick*/
543 sprintf(item
, "Help on %s", thegame
.name
);
544 AppendMenu(menu
, MF_ENABLED
, IDM_GAMEHELP
, item
);
548 SetMenu(fe
->hwnd
, bar
);
551 if (midend_wants_statusbar(fe
->me
)) {
552 fe
->statusbar
= CreateWindowEx(0, STATUSCLASSNAME
, "ooh",
553 WS_CHILD
| WS_VISIBLE
,
554 0, 0, 0, 0, /* status bar does these */
555 fe
->hwnd
, NULL
, inst
, NULL
);
556 GetWindowRect(fe
->statusbar
, &sr
);
557 SetWindowPos(fe
->hwnd
, NULL
, 0, 0,
558 r
.right
- r
.left
, r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
559 SWP_NOMOVE
| SWP_NOZORDER
);
560 SetWindowPos(fe
->statusbar
, NULL
, 0, y
, x
, sr
.bottom
- sr
.top
,
563 fe
->statusbar
= NULL
;
566 hdc
= GetDC(fe
->hwnd
);
567 fe
->bitmap
= CreateCompatibleBitmap(hdc
, x
, y
);
568 ReleaseDC(fe
->hwnd
, hdc
);
570 SetWindowLong(fe
->hwnd
, GWL_USERDATA
, (LONG
)fe
);
572 ShowWindow(fe
->hwnd
, SW_NORMAL
);
573 SetForegroundWindow(fe
->hwnd
);
575 midend_redraw(fe
->me
);
580 static int CALLBACK
AboutDlgProc(HWND hwnd
, UINT msg
,
581 WPARAM wParam
, LPARAM lParam
)
583 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
590 if ((HIWORD(wParam
) == BN_CLICKED
||
591 HIWORD(wParam
) == BN_DOUBLECLICKED
) &&
592 LOWORD(wParam
) == IDOK
)
604 static int CALLBACK
ConfigDlgProc(HWND hwnd
, UINT msg
,
605 WPARAM wParam
, LPARAM lParam
)
607 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
617 * OK and Cancel are special cases.
619 if ((HIWORD(wParam
) == BN_CLICKED
||
620 HIWORD(wParam
) == BN_DOUBLECLICKED
) &&
621 (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)) {
622 if (LOWORD(wParam
) == IDOK
) {
623 char *err
= midend_set_config(fe
->me
, fe
->cfg_which
, fe
->cfg
);
626 MessageBox(hwnd
, err
, "Validation error",
627 MB_ICONERROR
| MB_OK
);
638 * First find the control whose id this is.
640 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
641 if (j
->ctlid
== LOWORD(wParam
))
644 if (i
->type
== C_END
)
645 return 0; /* not our problem */
647 if (i
->type
== C_STRING
&& HIWORD(wParam
) == EN_CHANGE
) {
649 GetDlgItemText(fe
->cfgbox
, j
->ctlid
, buffer
, lenof(buffer
));
650 buffer
[lenof(buffer
)-1] = '\0';
652 i
->sval
= dupstr(buffer
);
653 } else if (i
->type
== C_BOOLEAN
&&
654 (HIWORD(wParam
) == BN_CLICKED
||
655 HIWORD(wParam
) == BN_DOUBLECLICKED
)) {
656 i
->ival
= IsDlgButtonChecked(fe
->cfgbox
, j
->ctlid
);
657 } else if (i
->type
== C_CHOICES
&&
658 HIWORD(wParam
) == CBN_SELCHANGE
) {
659 i
->ival
= SendDlgItemMessage(fe
->cfgbox
, j
->ctlid
,
673 HWND
mkctrl(frontend
*fe
, int x1
, int x2
, int y1
, int y2
,
674 char *wclass
, int wstyle
,
675 int exstyle
, const char *wtext
, int wid
)
678 ret
= CreateWindowEx(exstyle
, wclass
, wtext
,
679 wstyle
| WS_CHILD
| WS_VISIBLE
, x1
, y1
, x2
-x1
, y2
-y1
,
680 fe
->cfgbox
, (HMENU
) wid
, fe
->inst
, NULL
);
681 SendMessage(ret
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, MAKELPARAM(TRUE
, 0));
685 static void about(frontend
*fe
)
695 int winwidth
, winheight
, y
;
696 int height
, width
, maxwid
;
697 const char *strings
[16];
702 sprintf(titlebuf
, "About %.250s", thegame
.name
);
704 strings
[nstrings
++] = thegame
.name
;
705 strings
[nstrings
++] = "from Simon Tatham's Portable Puzzle Collection";
706 strings
[nstrings
++] = ver
;
708 wc
.style
= CS_DBLCLKS
| CS_SAVEBITS
| CS_BYTEALIGNWINDOW
;
709 wc
.lpfnWndProc
= DefDlgProc
;
711 wc
.cbWndExtra
= DLGWINDOWEXTRA
+ 8;
712 wc
.hInstance
= fe
->inst
;
714 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
715 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+1);
716 wc
.lpszMenuName
= NULL
;
717 wc
.lpszClassName
= "GameAboutBox";
720 hdc
= GetDC(fe
->hwnd
);
721 SetMapMode(hdc
, MM_TEXT
);
723 fe
->dlg_done
= FALSE
;
725 fe
->cfgfont
= CreateFont(-MulDiv(8, GetDeviceCaps(hdc
, LOGPIXELSY
), 72),
727 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
728 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
733 oldfont
= SelectObject(hdc
, fe
->cfgfont
);
734 if (GetTextMetrics(hdc
, &tm
)) {
735 height
= tm
.tmAscent
+ tm
.tmDescent
;
736 width
= tm
.tmAveCharWidth
;
742 * Figure out the layout of the About box by measuring the
743 * length of each piece of text.
746 winheight
= height
/2;
748 for (i
= 0; i
< nstrings
; i
++) {
749 if (GetTextExtentPoint32(hdc
, strings
[i
], strlen(strings
[i
]), &size
))
750 lengths
[i
] = size
.cx
;
752 lengths
[i
] = 0; /* *shrug* */
753 if (maxwid
< lengths
[i
])
755 winheight
+= height
* 3 / 2 + (height
/ 2);
758 winheight
+= height
+ height
* 7 / 4; /* OK button */
759 winwidth
= maxwid
+ 4*width
;
761 SelectObject(hdc
, oldfont
);
762 ReleaseDC(fe
->hwnd
, hdc
);
765 * Create the dialog, now that we know its size.
772 r
.bottom
= winheight
;
774 AdjustWindowRectEx(&r
, (WS_OVERLAPPEDWINDOW
/*|
775 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
776 WS_CAPTION | WS_SYSMENU*/) &~
777 (WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
781 * Centre the dialog on its parent window.
785 GetWindowRect(fe
->hwnd
, &r2
);
786 r
.left
= (r2
.left
+ r2
.right
- r
.right
) / 2;
787 r
.top
= (r2
.top
+ r2
.bottom
- r
.bottom
) / 2;
791 fe
->cfgbox
= CreateWindowEx(0, wc
.lpszClassName
, titlebuf
,
792 DS_MODALFRAME
| WS_POPUP
| WS_VISIBLE
|
793 WS_CAPTION
| WS_SYSMENU
,
795 r
.right
-r
.left
, r
.bottom
-r
.top
,
796 fe
->hwnd
, NULL
, fe
->inst
, NULL
);
799 SendMessage(fe
->cfgbox
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, FALSE
);
801 SetWindowLong(fe
->cfgbox
, GWL_USERDATA
, (LONG
)fe
);
802 SetWindowLong(fe
->cfgbox
, DWL_DLGPROC
, (LONG
)AboutDlgProc
);
806 for (i
= 0; i
< nstrings
; i
++) {
807 int border
= width
*2 + (maxwid
- lengths
[i
]) / 2;
808 mkctrl(fe
, border
, border
+lengths
[i
], y
+height
*1/8, y
+height
*9/8,
809 "Static", 0, 0, strings
[i
], id
++);
812 assert(y
< winheight
);
816 y
+= height
/2; /* extra space before OK */
817 mkctrl(fe
, width
*2, maxwid
+width
*2, y
, y
+height
*7/4, "BUTTON",
818 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
| BS_DEFPUSHBUTTON
, 0,
821 SendMessage(fe
->cfgbox
, WM_INITDIALOG
, 0, 0);
823 EnableWindow(fe
->hwnd
, FALSE
);
824 ShowWindow(fe
->cfgbox
, SW_NORMAL
);
825 while ((gm
=GetMessage(&msg
, NULL
, 0, 0)) > 0) {
826 if (!IsDialogMessage(fe
->cfgbox
, &msg
))
827 DispatchMessage(&msg
);
831 EnableWindow(fe
->hwnd
, TRUE
);
832 SetForegroundWindow(fe
->hwnd
);
833 DestroyWindow(fe
->cfgbox
);
834 DeleteObject(fe
->cfgfont
);
837 static int get_config(frontend
*fe
, int which
)
850 int winwidth
, winheight
, col1l
, col1r
, col2l
, col2r
, y
;
851 int height
, width
, maxlabel
, maxcheckbox
;
853 wc
.style
= CS_DBLCLKS
| CS_SAVEBITS
| CS_BYTEALIGNWINDOW
;
854 wc
.lpfnWndProc
= DefDlgProc
;
856 wc
.cbWndExtra
= DLGWINDOWEXTRA
+ 8;
857 wc
.hInstance
= fe
->inst
;
859 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
860 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+1);
861 wc
.lpszMenuName
= NULL
;
862 wc
.lpszClassName
= "GameConfigBox";
865 hdc
= GetDC(fe
->hwnd
);
866 SetMapMode(hdc
, MM_TEXT
);
868 fe
->dlg_done
= FALSE
;
870 fe
->cfgfont
= CreateFont(-MulDiv(8, GetDeviceCaps(hdc
, LOGPIXELSY
), 72),
872 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
873 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
878 oldfont
= SelectObject(hdc
, fe
->cfgfont
);
879 if (GetTextMetrics(hdc
, &tm
)) {
880 height
= tm
.tmAscent
+ tm
.tmDescent
;
881 width
= tm
.tmAveCharWidth
;
886 fe
->cfg
= midend_get_config(fe
->me
, which
, &title
);
887 fe
->cfg_which
= which
;
890 * Figure out the layout of the config box by measuring the
891 * length of each piece of text.
893 maxlabel
= maxcheckbox
= 0;
894 winheight
= height
/2;
896 for (i
= fe
->cfg
; i
->type
!= C_END
; i
++) {
901 * Both these control types have a label filling only
902 * the left-hand column of the box.
904 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
907 winheight
+= height
* 3 / 2 + (height
/ 2);
912 * Checkboxes take up the whole of the box width.
914 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
915 maxcheckbox
< size
.cx
)
916 maxcheckbox
= size
.cx
;
917 winheight
+= height
+ (height
/ 2);
922 winheight
+= height
+ height
* 7 / 4; /* OK / Cancel buttons */
925 col1r
= col1l
+ maxlabel
;
926 col2l
= col1r
+ 2*width
;
927 col2r
= col2l
+ 30*width
;
928 if (col2r
< col1l
+2*height
+maxcheckbox
)
929 col2r
= col1l
+2*height
+maxcheckbox
;
930 winwidth
= col2r
+ 2*width
;
932 SelectObject(hdc
, oldfont
);
933 ReleaseDC(fe
->hwnd
, hdc
);
936 * Create the dialog, now that we know its size.
943 r
.bottom
= winheight
;
945 AdjustWindowRectEx(&r
, (WS_OVERLAPPEDWINDOW
/*|
946 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
947 WS_CAPTION | WS_SYSMENU*/) &~
948 (WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
952 * Centre the dialog on its parent window.
956 GetWindowRect(fe
->hwnd
, &r2
);
957 r
.left
= (r2
.left
+ r2
.right
- r
.right
) / 2;
958 r
.top
= (r2
.top
+ r2
.bottom
- r
.bottom
) / 2;
962 fe
->cfgbox
= CreateWindowEx(0, wc
.lpszClassName
, title
,
963 DS_MODALFRAME
| WS_POPUP
| WS_VISIBLE
|
964 WS_CAPTION
| WS_SYSMENU
,
966 r
.right
-r
.left
, r
.bottom
-r
.top
,
967 fe
->hwnd
, NULL
, fe
->inst
, NULL
);
971 SendMessage(fe
->cfgbox
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, FALSE
);
973 SetWindowLong(fe
->cfgbox
, GWL_USERDATA
, (LONG
)fe
);
974 SetWindowLong(fe
->cfgbox
, DWL_DLGPROC
, (LONG
)ConfigDlgProc
);
977 * Count the controls so we can allocate cfgaux.
979 for (nctrls
= 0, i
= fe
->cfg
; i
->type
!= C_END
; i
++)
981 fe
->cfgaux
= snewn(nctrls
, struct cfg_aux
);
985 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
989 * Edit box with a label beside it.
991 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
992 "Static", 0, 0, i
->name
, id
++);
993 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*3/2,
994 "EDIT", WS_TABSTOP
| ES_AUTOHSCROLL
,
995 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
996 SetWindowText(ctl
, i
->sval
);
1004 mkctrl(fe
, col1l
, col2r
, y
, y
+height
, "BUTTON",
1005 BS_NOTIFY
| BS_AUTOCHECKBOX
| WS_TABSTOP
,
1006 0, i
->name
, (j
->ctlid
= id
++));
1007 CheckDlgButton(fe
->cfgbox
, j
->ctlid
, (i
->ival
!= 0));
1013 * Drop-down list with a label beside it.
1015 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
1016 "STATIC", 0, 0, i
->name
, id
++);
1017 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*41/2,
1018 "COMBOBOX", WS_TABSTOP
|
1019 CBS_DROPDOWNLIST
| CBS_HASSTRINGS
,
1020 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
1022 char c
, *p
, *q
, *str
;
1024 SendMessage(ctl
, CB_RESETCONTENT
, 0, 0);
1029 while (*q
&& *q
!= c
) q
++;
1030 str
= snewn(q
-p
+1, char);
1031 strncpy(str
, p
, q
-p
);
1033 SendMessage(ctl
, CB_ADDSTRING
, 0, (LPARAM
)str
);
1040 SendMessage(ctl
, CB_SETCURSEL
, i
->ival
, 0);
1046 assert(y
< winheight
);
1050 y
+= height
/2; /* extra space before OK and Cancel */
1051 mkctrl(fe
, col1l
, (col1l
+col2r
)/2-width
, y
, y
+height
*7/4, "BUTTON",
1052 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
| BS_DEFPUSHBUTTON
, 0,
1054 mkctrl(fe
, (col1l
+col2r
)/2+width
, col2r
, y
, y
+height
*7/4, "BUTTON",
1055 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
, 0, "Cancel", IDCANCEL
);
1057 SendMessage(fe
->cfgbox
, WM_INITDIALOG
, 0, 0);
1059 EnableWindow(fe
->hwnd
, FALSE
);
1060 ShowWindow(fe
->cfgbox
, SW_NORMAL
);
1061 while ((gm
=GetMessage(&msg
, NULL
, 0, 0)) > 0) {
1062 if (!IsDialogMessage(fe
->cfgbox
, &msg
))
1063 DispatchMessage(&msg
);
1067 EnableWindow(fe
->hwnd
, TRUE
);
1068 SetForegroundWindow(fe
->hwnd
);
1069 DestroyWindow(fe
->cfgbox
);
1070 DeleteObject(fe
->cfgfont
);
1075 return (fe
->dlg_done
== 2);
1078 static void new_game_type(frontend
*fe
)
1084 midend_new_game(fe
->me
);
1085 midend_size(fe
->me
, &x
, &y
);
1090 AdjustWindowRectEx(&r
, WS_OVERLAPPEDWINDOW
&~
1091 (WS_THICKFRAME
| WS_MAXIMIZEBOX
|
1095 if (fe
->statusbar
!= NULL
) {
1096 GetWindowRect(fe
->statusbar
, &sr
);
1098 sr
.left
= sr
.right
= sr
.top
= sr
.bottom
= 0;
1100 SetWindowPos(fe
->hwnd
, NULL
, 0, 0,
1102 r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
1103 SWP_NOMOVE
| SWP_NOZORDER
);
1104 if (fe
->statusbar
!= NULL
)
1105 SetWindowPos(fe
->statusbar
, NULL
, 0, y
, x
,
1106 sr
.bottom
- sr
.top
, SWP_NOZORDER
);
1108 DeleteObject(fe
->bitmap
);
1110 hdc
= GetDC(fe
->hwnd
);
1111 fe
->bitmap
= CreateCompatibleBitmap(hdc
, x
, y
);
1112 ReleaseDC(fe
->hwnd
, hdc
);
1114 midend_redraw(fe
->me
);
1117 static LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
,
1118 WPARAM wParam
, LPARAM lParam
)
1120 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
1124 DestroyWindow(hwnd
);
1127 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
1129 if (!midend_process_key(fe
->me
, 0, 0, 'n'))
1133 midend_restart_game(fe
->me
);
1136 if (!midend_process_key(fe
->me
, 0, 0, 'u'))
1140 if (!midend_process_key(fe
->me
, 0, 0, '\x12'))
1145 char *text
= midend_text_format(fe
->me
);
1147 write_clip(hwnd
, text
);
1149 MessageBeep(MB_ICONWARNING
);
1155 char *msg
= midend_solve(fe
->me
);
1157 MessageBox(hwnd
, msg
, "Unable to solve",
1158 MB_ICONERROR
| MB_OK
);
1162 if (!midend_process_key(fe
->me
, 0, 0, 'q'))
1166 if (get_config(fe
, CFG_SETTINGS
))
1170 if (get_config(fe
, CFG_SEED
))
1174 if (get_config(fe
, CFG_DESC
))
1181 assert(fe
->help_path
);
1182 WinHelp(hwnd
, fe
->help_path
,
1183 fe
->help_has_contents ? HELP_FINDER
: HELP_CONTENTS
, 0);
1186 assert(fe
->help_path
);
1187 assert(thegame
.winhelp_topic
);
1189 char *cmd
= snewn(10+strlen(thegame
.winhelp_topic
), char);
1190 sprintf(cmd
, "JI(`',`%s')", thegame
.winhelp_topic
);
1191 WinHelp(hwnd
, fe
->help_path
, HELP_COMMAND
, (DWORD
)cmd
);
1197 int p
= ((wParam
&~ 0xF) - IDM_PRESETS
) / 0x10;
1199 if (p
>= 0 && p
< fe
->npresets
) {
1200 midend_set_params(fe
->me
, fe
->presets
[p
]);
1216 hdc
= BeginPaint(hwnd
, &p
);
1217 hdc2
= CreateCompatibleDC(hdc
);
1218 prevbm
= SelectObject(hdc2
, fe
->bitmap
);
1220 p
.rcPaint
.left
, p
.rcPaint
.top
,
1221 p
.rcPaint
.right
- p
.rcPaint
.left
,
1222 p
.rcPaint
.bottom
- p
.rcPaint
.top
,
1224 p
.rcPaint
.left
, p
.rcPaint
.top
,
1226 SelectObject(hdc2
, prevbm
);
1235 int r
= GetKeyboardState(keystate
);
1236 int shift
= (r
&& (keystate
[VK_SHIFT
] & 0x80)) ? MOD_SHFT
: 0;
1237 int ctrl
= (r
&& (keystate
[VK_CONTROL
] & 0x80)) ? MOD_CTRL
: 0;
1241 if (!(lParam
& 0x01000000))
1242 key
= MOD_NUM_KEYPAD
| '4';
1244 key
= shift
| ctrl
| CURSOR_LEFT
;
1247 if (!(lParam
& 0x01000000))
1248 key
= MOD_NUM_KEYPAD
| '6';
1250 key
= shift
| ctrl
| CURSOR_RIGHT
;
1253 if (!(lParam
& 0x01000000))
1254 key
= MOD_NUM_KEYPAD
| '8';
1256 key
= shift
| ctrl
| CURSOR_UP
;
1259 if (!(lParam
& 0x01000000))
1260 key
= MOD_NUM_KEYPAD
| '2';
1262 key
= shift
| ctrl
| CURSOR_DOWN
;
1265 * Diagonal keys on the numeric keypad.
1268 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '9';
1271 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '3';
1274 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '7';
1277 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '1';
1280 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '0';
1283 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '5';
1286 * Numeric keypad keys with Num Lock on.
1288 case VK_NUMPAD4
: key
= MOD_NUM_KEYPAD
| '4'; break;
1289 case VK_NUMPAD6
: key
= MOD_NUM_KEYPAD
| '6'; break;
1290 case VK_NUMPAD8
: key
= MOD_NUM_KEYPAD
| '8'; break;
1291 case VK_NUMPAD2
: key
= MOD_NUM_KEYPAD
| '2'; break;
1292 case VK_NUMPAD5
: key
= MOD_NUM_KEYPAD
| '5'; break;
1293 case VK_NUMPAD9
: key
= MOD_NUM_KEYPAD
| '9'; break;
1294 case VK_NUMPAD3
: key
= MOD_NUM_KEYPAD
| '3'; break;
1295 case VK_NUMPAD7
: key
= MOD_NUM_KEYPAD
| '7'; break;
1296 case VK_NUMPAD1
: key
= MOD_NUM_KEYPAD
| '1'; break;
1297 case VK_NUMPAD0
: key
= MOD_NUM_KEYPAD
| '0'; break;
1301 if (!midend_process_key(fe
->me
, 0, 0, key
))
1306 m
.message
= WM_KEYDOWN
;
1308 m
.lParam
= lParam
& 0xdfff;
1309 TranslateMessage(&m
);
1313 case WM_LBUTTONDOWN
:
1314 case WM_RBUTTONDOWN
:
1315 case WM_MBUTTONDOWN
:
1320 * Shift-clicks count as middle-clicks, since otherwise
1321 * two-button Windows users won't have any kind of
1322 * middle click to use.
1324 if (message
== WM_MBUTTONDOWN
|| (wParam
& MK_SHIFT
))
1325 button
= MIDDLE_BUTTON
;
1326 else if (message
== WM_LBUTTONDOWN
)
1327 button
= LEFT_BUTTON
;
1329 button
= RIGHT_BUTTON
;
1331 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1332 (signed short)HIWORD(lParam
), button
))
1345 * Shift-clicks count as middle-clicks, since otherwise
1346 * two-button Windows users won't have any kind of
1347 * middle click to use.
1349 if (message
== WM_MBUTTONUP
|| (wParam
& MK_SHIFT
))
1350 button
= MIDDLE_RELEASE
;
1351 else if (message
== WM_LBUTTONUP
)
1352 button
= LEFT_RELEASE
;
1354 button
= RIGHT_RELEASE
;
1356 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1357 (signed short)HIWORD(lParam
), button
))
1367 if (wParam
& (MK_MBUTTON
| MK_SHIFT
))
1368 button
= MIDDLE_DRAG
;
1369 else if (wParam
& MK_LBUTTON
)
1372 button
= RIGHT_DRAG
;
1374 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1375 (signed short)HIWORD(lParam
), button
))
1380 if (!midend_process_key(fe
->me
, 0, 0, (unsigned char)wParam
))
1385 DWORD now
= GetTickCount();
1386 float elapsed
= (float) (now
- fe
->timer_last_tickcount
) * 0.001F
;
1387 midend_timer(fe
->me
, elapsed
);
1388 fe
->timer_last_tickcount
= now
;
1393 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
1396 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
)
1401 InitCommonControls();
1407 wndclass
.lpfnWndProc
= WndProc
;
1408 wndclass
.cbClsExtra
= 0;
1409 wndclass
.cbWndExtra
= 0;
1410 wndclass
.hInstance
= inst
;
1411 wndclass
.hIcon
= LoadIcon(inst
, IDI_APPLICATION
);
1412 wndclass
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
1413 wndclass
.hbrBackground
= NULL
;
1414 wndclass
.lpszMenuName
= NULL
;
1415 wndclass
.lpszClassName
= thegame
.name
;
1417 RegisterClass(&wndclass
);
1420 while (*cmdline
&& isspace(*cmdline
))
1423 if (!new_window(inst
, *cmdline ? cmdline
: NULL
, &error
)) {
1425 sprintf(buf
, "%.100s Error", thegame
.name
);
1426 MessageBox(NULL
, error
, buf
, MB_OK
|MB_ICONERROR
);
1430 while (GetMessage(&msg
, NULL
, 0, 0)) {
1431 DispatchMessage(&msg
);