2 * windows.c: Windows front end for my puzzle collection.
18 #define IDM_NEW 0x0010
19 #define IDM_RESTART 0x0020
20 #define IDM_UNDO 0x0030
21 #define IDM_REDO 0x0040
22 #define IDM_COPY 0x0050
23 #define IDM_SOLVE 0x0060
24 #define IDM_QUIT 0x0070
25 #define IDM_CONFIG 0x0080
26 #define IDM_DESC 0x0090
27 #define IDM_SEED 0x00A0
28 #define IDM_HELPC 0x00B0
29 #define IDM_GAMEHELP 0x00C0
30 #define IDM_ABOUT 0x00D0
31 #define IDM_PRESETS 0x0100
33 #define HELP_FILE_NAME "puzzles.hlp"
34 #define HELP_CNT_NAME "puzzles.cnt"
37 static FILE *debug_fp
= NULL
;
38 static HANDLE debug_hdl
= INVALID_HANDLE_VALUE
;
39 static int debug_got_console
= 0;
45 if (!debug_got_console
) {
47 debug_got_console
= 1;
48 debug_hdl
= GetStdHandle(STD_OUTPUT_HANDLE
);
52 debug_fp
= fopen("debug.log", "w");
55 if (debug_hdl
!= INVALID_HANDLE_VALUE
) {
56 WriteFile(debug_hdl
, buf
, strlen(buf
), &dw
, NULL
);
62 void debug_printf(char *fmt
, ...)
68 vsprintf(buf
, fmt
, ap
);
92 HWND hwnd
, statusbar
, cfgbox
;
94 HBITMAP bitmap
, prevbm
;
101 DWORD timer_last_tickcount
;
103 game_params
**presets
;
105 int nfonts
, fontsize
;
107 struct cfg_aux
*cfgaux
;
108 int cfg_which
, dlg_done
;
111 int help_has_contents
;
115 void fatal(char *fmt
, ...)
121 vsprintf(buf
, fmt
, ap
);
124 MessageBox(NULL
, buf
, "Fatal error", MB_ICONEXCLAMATION
| MB_OK
);
129 void get_random_seed(void **randseed
, int *randseedsize
)
131 time_t *tp
= snew(time_t);
133 *randseed
= (void *)tp
;
134 *randseedsize
= sizeof(time_t);
137 void status_bar(frontend
*fe
, char *text
)
139 char *rewritten
= midend_rewrite_statusbar(fe
->me
, text
);
140 if (!fe
->laststatus
|| strcmp(rewritten
, fe
->laststatus
)) {
141 SetWindowText(fe
->statusbar
, rewritten
);
142 sfree(fe
->laststatus
);
143 fe
->laststatus
= rewritten
;
149 blitter
*blitter_new(int w
, int h
)
151 blitter
*bl
= snew(blitter
);
153 memset(bl
, 0, sizeof(blitter
));
161 void blitter_free(blitter
*bl
)
163 if (bl
->bitmap
) DeleteObject(bl
->bitmap
);
167 static void blitter_mkbitmap(frontend
*fe
, blitter
*bl
)
169 HDC hdc
= GetDC(fe
->hwnd
);
170 bl
->bitmap
= CreateCompatibleBitmap(hdc
, bl
->w
, bl
->h
);
171 ReleaseDC(fe
->hwnd
, hdc
);
174 /* BitBlt(dstDC, dstX, dstY, dstW, dstH, srcDC, srcX, srcY, dType) */
176 void blitter_save(frontend
*fe
, blitter
*bl
, int x
, int y
)
178 HDC hdc_win
, hdc_blit
;
181 if (!bl
->bitmap
) blitter_mkbitmap(fe
, bl
);
183 bl
->x
= x
; bl
->y
= y
;
185 hdc_win
= GetDC(fe
->hwnd
);
186 hdc_blit
= CreateCompatibleDC(hdc_win
);
187 if (!hdc_blit
) fatal("hdc_blit failed: 0x%x", GetLastError());
189 prev_blit
= SelectObject(hdc_blit
, bl
->bitmap
);
190 if (prev_blit
== NULL
|| prev_blit
== HGDI_ERROR
)
191 fatal("SelectObject for hdc_main failed: 0x%x", GetLastError());
193 if (!BitBlt(hdc_blit
, 0, 0, bl
->w
, bl
->h
,
194 fe
->hdc_bm
, x
, y
, SRCCOPY
))
195 fatal("BitBlt failed: 0x%x", GetLastError());
197 SelectObject(hdc_blit
, prev_blit
);
199 ReleaseDC(fe
->hwnd
, hdc_win
);
202 void blitter_load(frontend
*fe
, blitter
*bl
, int x
, int y
)
204 HDC hdc_win
, hdc_blit
;
207 assert(bl
->bitmap
); /* we should always have saved before loading */
209 if (x
== BLITTER_FROMSAVED
) x
= bl
->x
;
210 if (y
== BLITTER_FROMSAVED
) y
= bl
->y
;
212 hdc_win
= GetDC(fe
->hwnd
);
213 hdc_blit
= CreateCompatibleDC(hdc_win
);
215 prev_blit
= SelectObject(hdc_blit
, bl
->bitmap
);
217 BitBlt(fe
->hdc_bm
, x
, y
, bl
->w
, bl
->h
,
218 hdc_blit
, 0, 0, SRCCOPY
);
220 SelectObject(hdc_blit
, prev_blit
);
222 ReleaseDC(fe
->hwnd
, hdc_win
);
225 void frontend_default_colour(frontend
*fe
, float *output
)
227 DWORD c
= GetSysColor(COLOR_MENU
); /* ick */
229 output
[0] = (float)(GetRValue(c
) / 255.0);
230 output
[1] = (float)(GetGValue(c
) / 255.0);
231 output
[2] = (float)(GetBValue(c
) / 255.0);
234 void clip(frontend
*fe
, int x
, int y
, int w
, int h
)
236 IntersectClipRect(fe
->hdc_bm
, x
, y
, x
+w
, y
+h
);
239 void unclip(frontend
*fe
)
241 SelectClipRgn(fe
->hdc_bm
, NULL
);
244 void draw_text(frontend
*fe
, int x
, int y
, int fonttype
, int fontsize
,
245 int align
, int colour
, char *text
)
250 * Find or create the font.
252 for (i
= 0; i
< fe
->nfonts
; i
++)
253 if (fe
->fonts
[i
].type
== fonttype
&& fe
->fonts
[i
].size
== fontsize
)
256 if (i
== fe
->nfonts
) {
257 if (fe
->fontsize
<= fe
->nfonts
) {
258 fe
->fontsize
= fe
->nfonts
+ 10;
259 fe
->fonts
= sresize(fe
->fonts
, fe
->fontsize
, struct font
);
264 fe
->fonts
[i
].type
= fonttype
;
265 fe
->fonts
[i
].size
= fontsize
;
267 fe
->fonts
[i
].font
= CreateFont(-fontsize
, 0, 0, 0, FW_BOLD
,
268 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
269 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
271 (fonttype
== FONT_FIXED ?
272 FIXED_PITCH
| FF_DONTCARE
:
273 VARIABLE_PITCH
| FF_SWISS
),
278 * Position and draw the text.
285 oldfont
= SelectObject(fe
->hdc_bm
, fe
->fonts
[i
].font
);
286 if (GetTextMetrics(fe
->hdc_bm
, &tm
)) {
287 if (align
& ALIGN_VCENTRE
)
288 y
-= (tm
.tmAscent
+tm
.tmDescent
)/2;
292 if (GetTextExtentPoint32(fe
->hdc_bm
, text
, strlen(text
), &size
)) {
293 if (align
& ALIGN_HCENTRE
)
295 else if (align
& ALIGN_HRIGHT
)
298 SetBkMode(fe
->hdc_bm
, TRANSPARENT
);
299 SetTextColor(fe
->hdc_bm
, fe
->colours
[colour
]);
300 TextOut(fe
->hdc_bm
, x
, y
, text
, strlen(text
));
301 SelectObject(fe
->hdc_bm
, oldfont
);
305 void draw_rect(frontend
*fe
, int x
, int y
, int w
, int h
, int colour
)
307 if (w
== 1 && h
== 1) {
309 * Rectangle() appears to get uppity if asked to draw a 1x1
310 * rectangle, presumably on the grounds that that's beneath
311 * its dignity and you ought to be using SetPixel instead.
314 SetPixel(fe
->hdc_bm
, x
, y
, fe
->colours
[colour
]);
316 HBRUSH oldbrush
= SelectObject(fe
->hdc_bm
, fe
->brushes
[colour
]);
317 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
318 Rectangle(fe
->hdc_bm
, x
, y
, x
+w
, y
+h
);
319 SelectObject(fe
->hdc_bm
, oldbrush
);
320 SelectObject(fe
->hdc_bm
, oldpen
);
324 void draw_line(frontend
*fe
, int x1
, int y1
, int x2
, int y2
, int colour
)
326 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
327 MoveToEx(fe
->hdc_bm
, x1
, y1
, NULL
);
328 LineTo(fe
->hdc_bm
, x2
, y2
);
329 SetPixel(fe
->hdc_bm
, x2
, y2
, fe
->colours
[colour
]);
330 SelectObject(fe
->hdc_bm
, oldpen
);
333 void draw_circle(frontend
*fe
, int cx
, int cy
, int radius
,
334 int fill
, int colour
)
337 HBRUSH oldbrush
= SelectObject(fe
->hdc_bm
, fe
->brushes
[colour
]);
338 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
339 Ellipse(fe
->hdc_bm
, cx
- radius
, cy
- radius
,
340 cx
+ radius
+ 1, cy
+ radius
+ 1);
341 SelectObject(fe
->hdc_bm
, oldbrush
);
342 SelectObject(fe
->hdc_bm
, oldpen
);
344 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
345 Arc(fe
->hdc_bm
, cx
- radius
, cy
- radius
,
346 cx
+ radius
+ 1, cy
+ radius
+ 1,
347 cx
- radius
, cy
, cx
- radius
, cy
);
348 SelectObject(fe
->hdc_bm
, oldpen
);
352 void draw_polygon(frontend
*fe
, int *coords
, int npoints
,
353 int fill
, int colour
)
355 POINT
*pts
= snewn(npoints
+1, POINT
);
358 for (i
= 0; i
<= npoints
; i
++) {
359 int j
= (i
< npoints ? i
: 0);
360 pts
[i
].x
= coords
[j
*2];
361 pts
[i
].y
= coords
[j
*2+1];
365 HBRUSH oldbrush
= SelectObject(fe
->hdc_bm
, fe
->brushes
[colour
]);
366 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
367 Polygon(fe
->hdc_bm
, pts
, npoints
);
368 SelectObject(fe
->hdc_bm
, oldbrush
);
369 SelectObject(fe
->hdc_bm
, oldpen
);
371 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
372 Polyline(fe
->hdc_bm
, pts
, npoints
+1);
373 SelectObject(fe
->hdc_bm
, oldpen
);
379 void start_draw(frontend
*fe
)
382 hdc_win
= GetDC(fe
->hwnd
);
383 fe
->hdc_bm
= CreateCompatibleDC(hdc_win
);
384 fe
->prevbm
= SelectObject(fe
->hdc_bm
, fe
->bitmap
);
385 ReleaseDC(fe
->hwnd
, hdc_win
);
387 SetMapMode(fe
->hdc_bm
, MM_TEXT
);
390 void draw_update(frontend
*fe
, int x
, int y
, int w
, int h
)
399 InvalidateRect(fe
->hwnd
, &r
, FALSE
);
402 void end_draw(frontend
*fe
)
404 SelectObject(fe
->hdc_bm
, fe
->prevbm
);
405 DeleteDC(fe
->hdc_bm
);
407 DeleteObject(fe
->clip
);
412 void deactivate_timer(frontend
*fe
)
414 KillTimer(fe
->hwnd
, fe
->timer
);
418 void activate_timer(frontend
*fe
)
421 fe
->timer
= SetTimer(fe
->hwnd
, fe
->timer
, 20, NULL
);
422 fe
->timer_last_tickcount
= GetTickCount();
426 void write_clip(HWND hwnd
, char *data
)
434 * Windows expects CRLF in the clipboard, so we must convert
435 * any \n that has come out of the puzzle backend.
438 for (i
= 0; data
[i
]; i
++) {
443 data2
= snewn(len
+1, char);
445 for (i
= 0; data
[i
]; i
++) {
448 data2
[j
++] = data
[i
];
453 clipdata
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, len
+ 1);
456 lock
= GlobalLock(clipdata
);
459 memcpy(lock
, data2
, len
);
460 ((unsigned char *) lock
)[len
] = 0;
461 GlobalUnlock(clipdata
);
463 if (OpenClipboard(hwnd
)) {
465 SetClipboardData(CF_TEXT
, clipdata
);
468 GlobalFree(clipdata
);
474 * See if we can find a help file.
476 static void find_help_file(frontend
*fe
)
478 char b
[2048], *p
, *q
, *r
;
480 if (!fe
->help_path
) {
481 GetModuleFileName(NULL
, b
, sizeof(b
) - 1);
483 p
= strrchr(b
, '\\');
484 if (p
&& p
>= r
) r
= p
+1;
486 if (q
&& q
>= r
) r
= q
+1;
487 strcpy(r
, HELP_FILE_NAME
);
488 if ( (fp
= fopen(b
, "r")) != NULL
) {
489 fe
->help_path
= dupstr(b
);
492 fe
->help_path
= NULL
;
493 strcpy(r
, HELP_CNT_NAME
);
494 if ( (fp
= fopen(b
, "r")) != NULL
) {
495 fe
->help_has_contents
= TRUE
;
498 fe
->help_has_contents
= FALSE
;
502 static void check_window_size(frontend
*fe
, int *px
, int *py
)
509 GetWindowRect(fe
->statusbar
, &sr
);
510 sy
= sr
.bottom
- sr
.top
;
516 * See if we actually got the window size we wanted, and adjust
517 * the puzzle size if not.
519 GetClientRect(fe
->hwnd
, &r
);
520 x
= r
.right
- r
.left
;
521 y
= r
.bottom
- r
.top
- sy
;
522 midend_size(fe
->me
, &x
, &y
, FALSE
);
523 if (x
!= r
.right
- r
.left
|| y
!= r
.bottom
- r
.top
) {
525 * Resize the window, now we know what size we _really_
531 AdjustWindowRectEx(&r
, WS_OVERLAPPEDWINDOW
&~
532 (WS_THICKFRAME
| WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
534 SetWindowPos(fe
->hwnd
, NULL
, 0, 0, r
.right
- r
.left
, r
.bottom
- r
.top
,
535 SWP_NOMOVE
| SWP_NOZORDER
);
539 GetClientRect(fe
->hwnd
, &r
);
540 SetWindowPos(fe
->statusbar
, NULL
, 0, r
.bottom
-r
.top
-sy
, r
.right
-r
.left
,
548 static frontend
*new_window(HINSTANCE inst
, char *game_id
, char **error
)
557 fe
->me
= midend_new(fe
, &thegame
);
560 *error
= midend_game_id(fe
->me
, game_id
);
568 fe
->help_path
= NULL
;
572 midend_new_game(fe
->me
);
577 fe
->nfonts
= fe
->fontsize
= 0;
579 fe
->laststatus
= NULL
;
585 colours
= midend_colours(fe
->me
, &ncolours
);
587 fe
->colours
= snewn(ncolours
, COLORREF
);
588 fe
->brushes
= snewn(ncolours
, HBRUSH
);
589 fe
->pens
= snewn(ncolours
, HPEN
);
591 for (i
= 0; i
< ncolours
; i
++) {
592 fe
->colours
[i
] = RGB(255 * colours
[i
*3+0],
593 255 * colours
[i
*3+1],
594 255 * colours
[i
*3+2]);
595 fe
->brushes
[i
] = CreateSolidBrush(fe
->colours
[i
]);
596 fe
->pens
[i
] = CreatePen(PS_SOLID
, 1, fe
->colours
[i
]);
600 x
= y
= INT_MAX
; /* find puzzle's preferred size */
601 midend_size(fe
->me
, &x
, &y
, FALSE
);
606 AdjustWindowRectEx(&r
, WS_OVERLAPPEDWINDOW
&~
607 (WS_THICKFRAME
| WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
610 fe
->hwnd
= CreateWindowEx(0, thegame
.name
, thegame
.name
,
611 WS_OVERLAPPEDWINDOW
&~
612 (WS_THICKFRAME
| WS_MAXIMIZEBOX
),
613 CW_USEDEFAULT
, CW_USEDEFAULT
,
614 r
.right
- r
.left
, r
.bottom
- r
.top
,
615 NULL
, NULL
, inst
, NULL
);
617 if (midend_wants_statusbar(fe
->me
)) {
619 fe
->statusbar
= CreateWindowEx(0, STATUSCLASSNAME
, "ooh",
620 WS_CHILD
| WS_VISIBLE
,
621 0, 0, 0, 0, /* status bar does these */
622 fe
->hwnd
, NULL
, inst
, NULL
);
624 * Now resize the window to take account of the status bar.
626 GetWindowRect(fe
->statusbar
, &sr
);
627 GetWindowRect(fe
->hwnd
, &r
);
628 SetWindowPos(fe
->hwnd
, NULL
, 0, 0, r
.right
- r
.left
,
629 r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
630 SWP_NOMOVE
| SWP_NOZORDER
);
632 fe
->statusbar
= NULL
;
636 HMENU bar
= CreateMenu();
637 HMENU menu
= CreateMenu();
639 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)menu
, "Game");
640 AppendMenu(menu
, MF_ENABLED
, IDM_NEW
, "New");
641 AppendMenu(menu
, MF_ENABLED
, IDM_RESTART
, "Restart");
642 AppendMenu(menu
, MF_ENABLED
, IDM_DESC
, "Specific...");
643 AppendMenu(menu
, MF_ENABLED
, IDM_SEED
, "Random Seed...");
645 if ((fe
->npresets
= midend_num_presets(fe
->me
)) > 0 ||
646 thegame
.can_configure
) {
647 HMENU sub
= CreateMenu();
650 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)sub
, "Type");
652 fe
->presets
= snewn(fe
->npresets
, game_params
*);
654 for (i
= 0; i
< fe
->npresets
; i
++) {
657 midend_fetch_preset(fe
->me
, i
, &name
, &fe
->presets
[i
]);
660 * FIXME: we ought to go through and do something
661 * with ampersands here.
664 AppendMenu(sub
, MF_ENABLED
, IDM_PRESETS
+ 0x10 * i
, name
);
667 if (thegame
.can_configure
) {
668 AppendMenu(sub
, MF_ENABLED
, IDM_CONFIG
, "Custom...");
672 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
673 AppendMenu(menu
, MF_ENABLED
, IDM_UNDO
, "Undo");
674 AppendMenu(menu
, MF_ENABLED
, IDM_REDO
, "Redo");
675 if (thegame
.can_format_as_text
) {
676 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
677 AppendMenu(menu
, MF_ENABLED
, IDM_COPY
, "Copy");
679 if (thegame
.can_solve
) {
680 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
681 AppendMenu(menu
, MF_ENABLED
, IDM_SOLVE
, "Solve");
683 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
684 AppendMenu(menu
, MF_ENABLED
, IDM_QUIT
, "Exit");
686 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)menu
, "Help");
687 AppendMenu(menu
, MF_ENABLED
, IDM_ABOUT
, "About");
689 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
690 AppendMenu(menu
, MF_ENABLED
, IDM_HELPC
, "Contents");
691 if (thegame
.winhelp_topic
) {
693 assert(thegame
.name
);
694 item
= snewn(9+strlen(thegame
.name
), char); /*ick*/
695 sprintf(item
, "Help on %s", thegame
.name
);
696 AppendMenu(menu
, MF_ENABLED
, IDM_GAMEHELP
, item
);
700 SetMenu(fe
->hwnd
, bar
);
703 check_window_size(fe
, &x
, &y
);
705 hdc
= GetDC(fe
->hwnd
);
706 fe
->bitmap
= CreateCompatibleBitmap(hdc
, x
, y
);
707 ReleaseDC(fe
->hwnd
, hdc
);
709 SetWindowLong(fe
->hwnd
, GWL_USERDATA
, (LONG
)fe
);
711 ShowWindow(fe
->hwnd
, SW_NORMAL
);
712 SetForegroundWindow(fe
->hwnd
);
714 midend_redraw(fe
->me
);
719 static int CALLBACK
AboutDlgProc(HWND hwnd
, UINT msg
,
720 WPARAM wParam
, LPARAM lParam
)
722 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
729 if ((HIWORD(wParam
) == BN_CLICKED
||
730 HIWORD(wParam
) == BN_DOUBLECLICKED
) &&
731 LOWORD(wParam
) == IDOK
)
743 static int CALLBACK
ConfigDlgProc(HWND hwnd
, UINT msg
,
744 WPARAM wParam
, LPARAM lParam
)
746 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
756 * OK and Cancel are special cases.
758 if ((HIWORD(wParam
) == BN_CLICKED
||
759 HIWORD(wParam
) == BN_DOUBLECLICKED
) &&
760 (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)) {
761 if (LOWORD(wParam
) == IDOK
) {
762 char *err
= midend_set_config(fe
->me
, fe
->cfg_which
, fe
->cfg
);
765 MessageBox(hwnd
, err
, "Validation error",
766 MB_ICONERROR
| MB_OK
);
777 * First find the control whose id this is.
779 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
780 if (j
->ctlid
== LOWORD(wParam
))
783 if (i
->type
== C_END
)
784 return 0; /* not our problem */
786 if (i
->type
== C_STRING
&& HIWORD(wParam
) == EN_CHANGE
) {
788 GetDlgItemText(fe
->cfgbox
, j
->ctlid
, buffer
, lenof(buffer
));
789 buffer
[lenof(buffer
)-1] = '\0';
791 i
->sval
= dupstr(buffer
);
792 } else if (i
->type
== C_BOOLEAN
&&
793 (HIWORD(wParam
) == BN_CLICKED
||
794 HIWORD(wParam
) == BN_DOUBLECLICKED
)) {
795 i
->ival
= IsDlgButtonChecked(fe
->cfgbox
, j
->ctlid
);
796 } else if (i
->type
== C_CHOICES
&&
797 HIWORD(wParam
) == CBN_SELCHANGE
) {
798 i
->ival
= SendDlgItemMessage(fe
->cfgbox
, j
->ctlid
,
812 HWND
mkctrl(frontend
*fe
, int x1
, int x2
, int y1
, int y2
,
813 char *wclass
, int wstyle
,
814 int exstyle
, const char *wtext
, int wid
)
817 ret
= CreateWindowEx(exstyle
, wclass
, wtext
,
818 wstyle
| WS_CHILD
| WS_VISIBLE
, x1
, y1
, x2
-x1
, y2
-y1
,
819 fe
->cfgbox
, (HMENU
) wid
, fe
->inst
, NULL
);
820 SendMessage(ret
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, MAKELPARAM(TRUE
, 0));
824 static void about(frontend
*fe
)
834 int winwidth
, winheight
, y
;
835 int height
, width
, maxwid
;
836 const char *strings
[16];
841 sprintf(titlebuf
, "About %.250s", thegame
.name
);
843 strings
[nstrings
++] = thegame
.name
;
844 strings
[nstrings
++] = "from Simon Tatham's Portable Puzzle Collection";
845 strings
[nstrings
++] = ver
;
847 wc
.style
= CS_DBLCLKS
| CS_SAVEBITS
| CS_BYTEALIGNWINDOW
;
848 wc
.lpfnWndProc
= DefDlgProc
;
850 wc
.cbWndExtra
= DLGWINDOWEXTRA
+ 8;
851 wc
.hInstance
= fe
->inst
;
853 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
854 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+1);
855 wc
.lpszMenuName
= NULL
;
856 wc
.lpszClassName
= "GameAboutBox";
859 hdc
= GetDC(fe
->hwnd
);
860 SetMapMode(hdc
, MM_TEXT
);
862 fe
->dlg_done
= FALSE
;
864 fe
->cfgfont
= CreateFont(-MulDiv(8, GetDeviceCaps(hdc
, LOGPIXELSY
), 72),
866 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
867 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
872 oldfont
= SelectObject(hdc
, fe
->cfgfont
);
873 if (GetTextMetrics(hdc
, &tm
)) {
874 height
= tm
.tmAscent
+ tm
.tmDescent
;
875 width
= tm
.tmAveCharWidth
;
881 * Figure out the layout of the About box by measuring the
882 * length of each piece of text.
885 winheight
= height
/2;
887 for (i
= 0; i
< nstrings
; i
++) {
888 if (GetTextExtentPoint32(hdc
, strings
[i
], strlen(strings
[i
]), &size
))
889 lengths
[i
] = size
.cx
;
891 lengths
[i
] = 0; /* *shrug* */
892 if (maxwid
< lengths
[i
])
894 winheight
+= height
* 3 / 2 + (height
/ 2);
897 winheight
+= height
+ height
* 7 / 4; /* OK button */
898 winwidth
= maxwid
+ 4*width
;
900 SelectObject(hdc
, oldfont
);
901 ReleaseDC(fe
->hwnd
, hdc
);
904 * Create the dialog, now that we know its size.
911 r
.bottom
= winheight
;
913 AdjustWindowRectEx(&r
, (WS_OVERLAPPEDWINDOW
/*|
914 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
915 WS_CAPTION | WS_SYSMENU*/) &~
916 (WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
920 * Centre the dialog on its parent window.
924 GetWindowRect(fe
->hwnd
, &r2
);
925 r
.left
= (r2
.left
+ r2
.right
- r
.right
) / 2;
926 r
.top
= (r2
.top
+ r2
.bottom
- r
.bottom
) / 2;
930 fe
->cfgbox
= CreateWindowEx(0, wc
.lpszClassName
, titlebuf
,
931 DS_MODALFRAME
| WS_POPUP
| WS_VISIBLE
|
932 WS_CAPTION
| WS_SYSMENU
,
934 r
.right
-r
.left
, r
.bottom
-r
.top
,
935 fe
->hwnd
, NULL
, fe
->inst
, NULL
);
938 SendMessage(fe
->cfgbox
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, FALSE
);
940 SetWindowLong(fe
->cfgbox
, GWL_USERDATA
, (LONG
)fe
);
941 SetWindowLong(fe
->cfgbox
, DWL_DLGPROC
, (LONG
)AboutDlgProc
);
945 for (i
= 0; i
< nstrings
; i
++) {
946 int border
= width
*2 + (maxwid
- lengths
[i
]) / 2;
947 mkctrl(fe
, border
, border
+lengths
[i
], y
+height
*1/8, y
+height
*9/8,
948 "Static", 0, 0, strings
[i
], id
++);
951 assert(y
< winheight
);
955 y
+= height
/2; /* extra space before OK */
956 mkctrl(fe
, width
*2, maxwid
+width
*2, y
, y
+height
*7/4, "BUTTON",
957 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
| BS_DEFPUSHBUTTON
, 0,
960 SendMessage(fe
->cfgbox
, WM_INITDIALOG
, 0, 0);
962 EnableWindow(fe
->hwnd
, FALSE
);
963 ShowWindow(fe
->cfgbox
, SW_NORMAL
);
964 while ((gm
=GetMessage(&msg
, NULL
, 0, 0)) > 0) {
965 if (!IsDialogMessage(fe
->cfgbox
, &msg
))
966 DispatchMessage(&msg
);
970 EnableWindow(fe
->hwnd
, TRUE
);
971 SetForegroundWindow(fe
->hwnd
);
972 DestroyWindow(fe
->cfgbox
);
973 DeleteObject(fe
->cfgfont
);
976 static int get_config(frontend
*fe
, int which
)
989 int winwidth
, winheight
, col1l
, col1r
, col2l
, col2r
, y
;
990 int height
, width
, maxlabel
, maxcheckbox
;
992 wc
.style
= CS_DBLCLKS
| CS_SAVEBITS
| CS_BYTEALIGNWINDOW
;
993 wc
.lpfnWndProc
= DefDlgProc
;
995 wc
.cbWndExtra
= DLGWINDOWEXTRA
+ 8;
996 wc
.hInstance
= fe
->inst
;
998 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
999 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+1);
1000 wc
.lpszMenuName
= NULL
;
1001 wc
.lpszClassName
= "GameConfigBox";
1004 hdc
= GetDC(fe
->hwnd
);
1005 SetMapMode(hdc
, MM_TEXT
);
1007 fe
->dlg_done
= FALSE
;
1009 fe
->cfgfont
= CreateFont(-MulDiv(8, GetDeviceCaps(hdc
, LOGPIXELSY
), 72),
1011 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
1012 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
1017 oldfont
= SelectObject(hdc
, fe
->cfgfont
);
1018 if (GetTextMetrics(hdc
, &tm
)) {
1019 height
= tm
.tmAscent
+ tm
.tmDescent
;
1020 width
= tm
.tmAveCharWidth
;
1022 height
= width
= 30;
1025 fe
->cfg
= midend_get_config(fe
->me
, which
, &title
);
1026 fe
->cfg_which
= which
;
1029 * Figure out the layout of the config box by measuring the
1030 * length of each piece of text.
1032 maxlabel
= maxcheckbox
= 0;
1033 winheight
= height
/2;
1035 for (i
= fe
->cfg
; i
->type
!= C_END
; i
++) {
1040 * Both these control types have a label filling only
1041 * the left-hand column of the box.
1043 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
1046 winheight
+= height
* 3 / 2 + (height
/ 2);
1051 * Checkboxes take up the whole of the box width.
1053 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
1054 maxcheckbox
< size
.cx
)
1055 maxcheckbox
= size
.cx
;
1056 winheight
+= height
+ (height
/ 2);
1061 winheight
+= height
+ height
* 7 / 4; /* OK / Cancel buttons */
1064 col1r
= col1l
+ maxlabel
;
1065 col2l
= col1r
+ 2*width
;
1066 col2r
= col2l
+ 30*width
;
1067 if (col2r
< col1l
+2*height
+maxcheckbox
)
1068 col2r
= col1l
+2*height
+maxcheckbox
;
1069 winwidth
= col2r
+ 2*width
;
1071 SelectObject(hdc
, oldfont
);
1072 ReleaseDC(fe
->hwnd
, hdc
);
1075 * Create the dialog, now that we know its size.
1082 r
.bottom
= winheight
;
1084 AdjustWindowRectEx(&r
, (WS_OVERLAPPEDWINDOW
/*|
1085 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1086 WS_CAPTION | WS_SYSMENU*/) &~
1087 (WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
1091 * Centre the dialog on its parent window.
1095 GetWindowRect(fe
->hwnd
, &r2
);
1096 r
.left
= (r2
.left
+ r2
.right
- r
.right
) / 2;
1097 r
.top
= (r2
.top
+ r2
.bottom
- r
.bottom
) / 2;
1101 fe
->cfgbox
= CreateWindowEx(0, wc
.lpszClassName
, title
,
1102 DS_MODALFRAME
| WS_POPUP
| WS_VISIBLE
|
1103 WS_CAPTION
| WS_SYSMENU
,
1105 r
.right
-r
.left
, r
.bottom
-r
.top
,
1106 fe
->hwnd
, NULL
, fe
->inst
, NULL
);
1110 SendMessage(fe
->cfgbox
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, FALSE
);
1112 SetWindowLong(fe
->cfgbox
, GWL_USERDATA
, (LONG
)fe
);
1113 SetWindowLong(fe
->cfgbox
, DWL_DLGPROC
, (LONG
)ConfigDlgProc
);
1116 * Count the controls so we can allocate cfgaux.
1118 for (nctrls
= 0, i
= fe
->cfg
; i
->type
!= C_END
; i
++)
1120 fe
->cfgaux
= snewn(nctrls
, struct cfg_aux
);
1124 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
1128 * Edit box with a label beside it.
1130 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
1131 "Static", 0, 0, i
->name
, id
++);
1132 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*3/2,
1133 "EDIT", WS_TABSTOP
| ES_AUTOHSCROLL
,
1134 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
1135 SetWindowText(ctl
, i
->sval
);
1143 mkctrl(fe
, col1l
, col2r
, y
, y
+height
, "BUTTON",
1144 BS_NOTIFY
| BS_AUTOCHECKBOX
| WS_TABSTOP
,
1145 0, i
->name
, (j
->ctlid
= id
++));
1146 CheckDlgButton(fe
->cfgbox
, j
->ctlid
, (i
->ival
!= 0));
1152 * Drop-down list with a label beside it.
1154 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
1155 "STATIC", 0, 0, i
->name
, id
++);
1156 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*41/2,
1157 "COMBOBOX", WS_TABSTOP
|
1158 CBS_DROPDOWNLIST
| CBS_HASSTRINGS
,
1159 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
1161 char c
, *p
, *q
, *str
;
1163 SendMessage(ctl
, CB_RESETCONTENT
, 0, 0);
1168 while (*q
&& *q
!= c
) q
++;
1169 str
= snewn(q
-p
+1, char);
1170 strncpy(str
, p
, q
-p
);
1172 SendMessage(ctl
, CB_ADDSTRING
, 0, (LPARAM
)str
);
1179 SendMessage(ctl
, CB_SETCURSEL
, i
->ival
, 0);
1185 assert(y
< winheight
);
1189 y
+= height
/2; /* extra space before OK and Cancel */
1190 mkctrl(fe
, col1l
, (col1l
+col2r
)/2-width
, y
, y
+height
*7/4, "BUTTON",
1191 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
| BS_DEFPUSHBUTTON
, 0,
1193 mkctrl(fe
, (col1l
+col2r
)/2+width
, col2r
, y
, y
+height
*7/4, "BUTTON",
1194 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
, 0, "Cancel", IDCANCEL
);
1196 SendMessage(fe
->cfgbox
, WM_INITDIALOG
, 0, 0);
1198 EnableWindow(fe
->hwnd
, FALSE
);
1199 ShowWindow(fe
->cfgbox
, SW_NORMAL
);
1200 while ((gm
=GetMessage(&msg
, NULL
, 0, 0)) > 0) {
1201 if (!IsDialogMessage(fe
->cfgbox
, &msg
))
1202 DispatchMessage(&msg
);
1206 EnableWindow(fe
->hwnd
, TRUE
);
1207 SetForegroundWindow(fe
->hwnd
);
1208 DestroyWindow(fe
->cfgbox
);
1209 DeleteObject(fe
->cfgfont
);
1214 return (fe
->dlg_done
== 2);
1217 static void new_game_type(frontend
*fe
)
1223 midend_new_game(fe
->me
);
1225 midend_size(fe
->me
, &x
, &y
, FALSE
);
1230 AdjustWindowRectEx(&r
, WS_OVERLAPPEDWINDOW
&~
1231 (WS_THICKFRAME
| WS_MAXIMIZEBOX
|
1235 if (fe
->statusbar
!= NULL
) {
1236 GetWindowRect(fe
->statusbar
, &sr
);
1238 sr
.left
= sr
.right
= sr
.top
= sr
.bottom
= 0;
1240 SetWindowPos(fe
->hwnd
, NULL
, 0, 0,
1242 r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
1243 SWP_NOMOVE
| SWP_NOZORDER
);
1245 check_window_size(fe
, &x
, &y
);
1247 if (fe
->statusbar
!= NULL
)
1248 SetWindowPos(fe
->statusbar
, NULL
, 0, y
, x
,
1249 sr
.bottom
- sr
.top
, SWP_NOZORDER
);
1251 DeleteObject(fe
->bitmap
);
1253 hdc
= GetDC(fe
->hwnd
);
1254 fe
->bitmap
= CreateCompatibleBitmap(hdc
, x
, y
);
1255 ReleaseDC(fe
->hwnd
, hdc
);
1257 midend_redraw(fe
->me
);
1260 static int is_alt_pressed(void)
1263 int r
= GetKeyboardState(keystate
);
1266 if (keystate
[VK_MENU
] & 0x80)
1268 if (keystate
[VK_RMENU
] & 0x80)
1273 static LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
,
1274 WPARAM wParam
, LPARAM lParam
)
1276 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
1280 DestroyWindow(hwnd
);
1283 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
1285 if (!midend_process_key(fe
->me
, 0, 0, 'n'))
1289 midend_restart_game(fe
->me
);
1292 if (!midend_process_key(fe
->me
, 0, 0, 'u'))
1296 if (!midend_process_key(fe
->me
, 0, 0, '\x12'))
1301 char *text
= midend_text_format(fe
->me
);
1303 write_clip(hwnd
, text
);
1305 MessageBeep(MB_ICONWARNING
);
1311 char *msg
= midend_solve(fe
->me
);
1313 MessageBox(hwnd
, msg
, "Unable to solve",
1314 MB_ICONERROR
| MB_OK
);
1318 if (!midend_process_key(fe
->me
, 0, 0, 'q'))
1322 if (get_config(fe
, CFG_SETTINGS
))
1326 if (get_config(fe
, CFG_SEED
))
1330 if (get_config(fe
, CFG_DESC
))
1337 assert(fe
->help_path
);
1338 WinHelp(hwnd
, fe
->help_path
,
1339 fe
->help_has_contents ? HELP_FINDER
: HELP_CONTENTS
, 0);
1342 assert(fe
->help_path
);
1343 assert(thegame
.winhelp_topic
);
1345 char *cmd
= snewn(10+strlen(thegame
.winhelp_topic
), char);
1346 sprintf(cmd
, "JI(`',`%s')", thegame
.winhelp_topic
);
1347 WinHelp(hwnd
, fe
->help_path
, HELP_COMMAND
, (DWORD
)cmd
);
1353 int p
= ((wParam
&~ 0xF) - IDM_PRESETS
) / 0x10;
1355 if (p
>= 0 && p
< fe
->npresets
) {
1356 midend_set_params(fe
->me
, fe
->presets
[p
]);
1372 hdc
= BeginPaint(hwnd
, &p
);
1373 hdc2
= CreateCompatibleDC(hdc
);
1374 prevbm
= SelectObject(hdc2
, fe
->bitmap
);
1376 p
.rcPaint
.left
, p
.rcPaint
.top
,
1377 p
.rcPaint
.right
- p
.rcPaint
.left
,
1378 p
.rcPaint
.bottom
- p
.rcPaint
.top
,
1380 p
.rcPaint
.left
, p
.rcPaint
.top
,
1382 SelectObject(hdc2
, prevbm
);
1391 int r
= GetKeyboardState(keystate
);
1392 int shift
= (r
&& (keystate
[VK_SHIFT
] & 0x80)) ? MOD_SHFT
: 0;
1393 int ctrl
= (r
&& (keystate
[VK_CONTROL
] & 0x80)) ? MOD_CTRL
: 0;
1397 if (!(lParam
& 0x01000000))
1398 key
= MOD_NUM_KEYPAD
| '4';
1400 key
= shift
| ctrl
| CURSOR_LEFT
;
1403 if (!(lParam
& 0x01000000))
1404 key
= MOD_NUM_KEYPAD
| '6';
1406 key
= shift
| ctrl
| CURSOR_RIGHT
;
1409 if (!(lParam
& 0x01000000))
1410 key
= MOD_NUM_KEYPAD
| '8';
1412 key
= shift
| ctrl
| CURSOR_UP
;
1415 if (!(lParam
& 0x01000000))
1416 key
= MOD_NUM_KEYPAD
| '2';
1418 key
= shift
| ctrl
| CURSOR_DOWN
;
1421 * Diagonal keys on the numeric keypad.
1424 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '9';
1427 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '3';
1430 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '7';
1433 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '1';
1436 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '0';
1439 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '5';
1442 * Numeric keypad keys with Num Lock on.
1444 case VK_NUMPAD4
: key
= MOD_NUM_KEYPAD
| '4'; break;
1445 case VK_NUMPAD6
: key
= MOD_NUM_KEYPAD
| '6'; break;
1446 case VK_NUMPAD8
: key
= MOD_NUM_KEYPAD
| '8'; break;
1447 case VK_NUMPAD2
: key
= MOD_NUM_KEYPAD
| '2'; break;
1448 case VK_NUMPAD5
: key
= MOD_NUM_KEYPAD
| '5'; break;
1449 case VK_NUMPAD9
: key
= MOD_NUM_KEYPAD
| '9'; break;
1450 case VK_NUMPAD3
: key
= MOD_NUM_KEYPAD
| '3'; break;
1451 case VK_NUMPAD7
: key
= MOD_NUM_KEYPAD
| '7'; break;
1452 case VK_NUMPAD1
: key
= MOD_NUM_KEYPAD
| '1'; break;
1453 case VK_NUMPAD0
: key
= MOD_NUM_KEYPAD
| '0'; break;
1457 if (!midend_process_key(fe
->me
, 0, 0, key
))
1462 m
.message
= WM_KEYDOWN
;
1464 m
.lParam
= lParam
& 0xdfff;
1465 TranslateMessage(&m
);
1469 case WM_LBUTTONDOWN
:
1470 case WM_RBUTTONDOWN
:
1471 case WM_MBUTTONDOWN
:
1476 * Shift-clicks count as middle-clicks, since otherwise
1477 * two-button Windows users won't have any kind of
1478 * middle click to use.
1480 if (message
== WM_MBUTTONDOWN
|| (wParam
& MK_SHIFT
))
1481 button
= MIDDLE_BUTTON
;
1482 else if (message
== WM_RBUTTONDOWN
|| is_alt_pressed())
1483 button
= RIGHT_BUTTON
;
1485 button
= LEFT_BUTTON
;
1487 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1488 (signed short)HIWORD(lParam
), button
))
1501 * Shift-clicks count as middle-clicks, since otherwise
1502 * two-button Windows users won't have any kind of
1503 * middle click to use.
1505 if (message
== WM_MBUTTONUP
|| (wParam
& MK_SHIFT
))
1506 button
= MIDDLE_RELEASE
;
1507 else if (message
== WM_RBUTTONUP
|| is_alt_pressed())
1508 button
= RIGHT_RELEASE
;
1510 button
= LEFT_RELEASE
;
1512 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1513 (signed short)HIWORD(lParam
), button
))
1523 if (wParam
& (MK_MBUTTON
| MK_SHIFT
))
1524 button
= MIDDLE_DRAG
;
1525 else if (wParam
& MK_RBUTTON
|| is_alt_pressed())
1526 button
= RIGHT_DRAG
;
1530 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1531 (signed short)HIWORD(lParam
), button
))
1536 if (!midend_process_key(fe
->me
, 0, 0, (unsigned char)wParam
))
1541 DWORD now
= GetTickCount();
1542 float elapsed
= (float) (now
- fe
->timer_last_tickcount
) * 0.001F
;
1543 midend_timer(fe
->me
, elapsed
);
1544 fe
->timer_last_tickcount
= now
;
1549 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
1552 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
)
1557 InitCommonControls();
1563 wndclass
.lpfnWndProc
= WndProc
;
1564 wndclass
.cbClsExtra
= 0;
1565 wndclass
.cbWndExtra
= 0;
1566 wndclass
.hInstance
= inst
;
1567 wndclass
.hIcon
= LoadIcon(inst
, IDI_APPLICATION
);
1568 wndclass
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
1569 wndclass
.hbrBackground
= NULL
;
1570 wndclass
.lpszMenuName
= NULL
;
1571 wndclass
.lpszClassName
= thegame
.name
;
1573 RegisterClass(&wndclass
);
1576 while (*cmdline
&& isspace(*cmdline
))
1579 if (!new_window(inst
, *cmdline ? cmdline
: NULL
, &error
)) {
1581 sprintf(buf
, "%.100s Error", thegame
.name
);
1582 MessageBox(NULL
, error
, buf
, MB_OK
|MB_ICONERROR
);
1586 while (GetMessage(&msg
, NULL
, 0, 0)) {
1587 DispatchMessage(&msg
);