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_SAVE 0x00E0
32 #define IDM_LOAD 0x00F0
33 #define IDM_PRINT 0x0100
34 #define IDM_PRESETS 0x0110
36 #define HELP_FILE_NAME "puzzles.hlp"
37 #define HELP_CNT_NAME "puzzles.cnt"
40 static FILE *debug_fp
= NULL
;
41 static HANDLE debug_hdl
= INVALID_HANDLE_VALUE
;
42 static int debug_got_console
= 0;
48 if (!debug_got_console
) {
50 debug_got_console
= 1;
51 debug_hdl
= GetStdHandle(STD_OUTPUT_HANDLE
);
55 debug_fp
= fopen("debug.log", "w");
58 if (debug_hdl
!= INVALID_HANDLE_VALUE
) {
59 WriteFile(debug_hdl
, buf
, strlen(buf
), &dw
, NULL
);
65 void debug_printf(char *fmt
, ...)
71 vsprintf(buf
, fmt
, ap
);
93 enum { CFG_PRINT
= CFG_FRONTEND_SPECIFIC
};
97 HWND hwnd
, statusbar
, cfgbox
;
99 HBITMAP bitmap
, prevbm
;
106 DWORD timer_last_tickcount
;
108 game_params
**presets
;
110 int nfonts
, fontsize
;
112 struct cfg_aux
*cfgaux
;
113 int cfg_which
, dlg_done
;
118 int help_has_contents
;
120 enum { DRAWING
, PRINTING
, NOTHING
} drawstatus
;
122 int printcount
, printw
, printh
, printsolns
, printcurr
, printcolour
;
124 int printoffsetx
, printoffsety
;
125 float printpixelscale
;
131 void fatal(char *fmt
, ...)
137 vsprintf(buf
, fmt
, ap
);
140 MessageBox(NULL
, buf
, "Fatal error", MB_ICONEXCLAMATION
| MB_OK
);
145 char *geterrstr(void)
148 DWORD dw
= GetLastError();
152 FORMAT_MESSAGE_ALLOCATE_BUFFER
|
153 FORMAT_MESSAGE_FROM_SYSTEM
,
156 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
160 ret
= dupstr(lpMsgBuf
);
167 void get_random_seed(void **randseed
, int *randseedsize
)
169 time_t *tp
= snew(time_t);
171 *randseed
= (void *)tp
;
172 *randseedsize
= sizeof(time_t);
175 static void win_status_bar(void *handle
, char *text
)
177 frontend
*fe
= (frontend
*)handle
;
180 assert(fe
->drawstatus
== DRAWING
);
182 rewritten
= midend_rewrite_statusbar(fe
->me
, text
);
183 if (!fe
->laststatus
|| strcmp(rewritten
, fe
->laststatus
)) {
184 SetWindowText(fe
->statusbar
, rewritten
);
185 sfree(fe
->laststatus
);
186 fe
->laststatus
= rewritten
;
192 static blitter
*win_blitter_new(void *handle
, int w
, int h
)
194 blitter
*bl
= snew(blitter
);
196 memset(bl
, 0, sizeof(blitter
));
204 static void win_blitter_free(void *handle
, blitter
*bl
)
206 if (bl
->bitmap
) DeleteObject(bl
->bitmap
);
210 static void blitter_mkbitmap(frontend
*fe
, blitter
*bl
)
212 HDC hdc
= GetDC(fe
->hwnd
);
213 bl
->bitmap
= CreateCompatibleBitmap(hdc
, bl
->w
, bl
->h
);
214 ReleaseDC(fe
->hwnd
, hdc
);
217 /* BitBlt(dstDC, dstX, dstY, dstW, dstH, srcDC, srcX, srcY, dType) */
219 static void win_blitter_save(void *handle
, blitter
*bl
, int x
, int y
)
221 frontend
*fe
= (frontend
*)handle
;
222 HDC hdc_win
, hdc_blit
;
225 assert(fe
->drawstatus
== DRAWING
);
227 if (!bl
->bitmap
) blitter_mkbitmap(fe
, bl
);
229 bl
->x
= x
; bl
->y
= y
;
231 hdc_win
= GetDC(fe
->hwnd
);
232 hdc_blit
= CreateCompatibleDC(hdc_win
);
233 if (!hdc_blit
) fatal("hdc_blit failed: 0x%x", GetLastError());
235 prev_blit
= SelectObject(hdc_blit
, bl
->bitmap
);
236 if (prev_blit
== NULL
|| prev_blit
== HGDI_ERROR
)
237 fatal("SelectObject for hdc_main failed: 0x%x", GetLastError());
239 if (!BitBlt(hdc_blit
, 0, 0, bl
->w
, bl
->h
,
240 fe
->hdc
, x
, y
, SRCCOPY
))
241 fatal("BitBlt failed: 0x%x", GetLastError());
243 SelectObject(hdc_blit
, prev_blit
);
245 ReleaseDC(fe
->hwnd
, hdc_win
);
248 static void win_blitter_load(void *handle
, blitter
*bl
, int x
, int y
)
250 frontend
*fe
= (frontend
*)handle
;
251 HDC hdc_win
, hdc_blit
;
254 assert(fe
->drawstatus
== DRAWING
);
256 assert(bl
->bitmap
); /* we should always have saved before loading */
258 if (x
== BLITTER_FROMSAVED
) x
= bl
->x
;
259 if (y
== BLITTER_FROMSAVED
) y
= bl
->y
;
261 hdc_win
= GetDC(fe
->hwnd
);
262 hdc_blit
= CreateCompatibleDC(hdc_win
);
264 prev_blit
= SelectObject(hdc_blit
, bl
->bitmap
);
266 BitBlt(fe
->hdc
, x
, y
, bl
->w
, bl
->h
,
267 hdc_blit
, 0, 0, SRCCOPY
);
269 SelectObject(hdc_blit
, prev_blit
);
271 ReleaseDC(fe
->hwnd
, hdc_win
);
274 void frontend_default_colour(frontend
*fe
, float *output
)
276 DWORD c
= GetSysColor(COLOR_MENU
); /* ick */
278 output
[0] = (float)(GetRValue(c
) / 255.0);
279 output
[1] = (float)(GetGValue(c
) / 255.0);
280 output
[2] = (float)(GetBValue(c
) / 255.0);
283 static POINT
win_transform_point(frontend
*fe
, int x
, int y
)
287 assert(fe
->drawstatus
!= NOTHING
);
289 if (fe
->drawstatus
== PRINTING
) {
290 ret
.x
= (int)(fe
->printoffsetx
+ fe
->printpixelscale
* x
);
291 ret
.y
= (int)(fe
->printoffsety
+ fe
->printpixelscale
* y
);
300 static void win_text_colour(frontend
*fe
, int colour
)
302 assert(fe
->drawstatus
!= NOTHING
);
304 if (fe
->drawstatus
== PRINTING
) {
307 print_get_colour(fe
->dr
, colour
, &hatch
, &r
, &g
, &b
);
309 SetTextColor(fe
->hdc
, RGB(r
* 255, g
* 255, b
* 255));
311 SetTextColor(fe
->hdc
,
312 hatch
== HATCH_CLEAR ?
RGB(255,255,255) : RGB(0,0,0));
314 SetTextColor(fe
->hdc
, fe
->colours
[colour
]);
318 static void win_set_brush(frontend
*fe
, int colour
)
321 assert(fe
->drawstatus
!= NOTHING
);
323 if (fe
->drawstatus
== PRINTING
) {
326 print_get_colour(fe
->dr
, colour
, &hatch
, &r
, &g
, &b
);
329 br
= CreateSolidBrush(RGB(r
* 255, g
* 255, b
* 255));
330 else if (hatch
== HATCH_SOLID
)
331 br
= CreateSolidBrush(RGB(0,0,0));
332 else if (hatch
== HATCH_CLEAR
)
333 br
= CreateSolidBrush(RGB(255,255,255));
335 br
= CreateHatchBrush(hatch
== HATCH_BACKSLASH ? HS_FDIAGONAL
:
336 hatch
== HATCH_SLASH ? HS_BDIAGONAL
:
337 hatch
== HATCH_HORIZ ? HS_HORIZONTAL
:
338 hatch
== HATCH_VERT ? HS_VERTICAL
:
339 hatch
== HATCH_PLUS ? HS_CROSS
:
340 /* hatch == HATCH_X ? */ HS_DIAGCROSS
,
343 br
= fe
->brushes
[colour
];
345 fe
->oldbr
= SelectObject(fe
->hdc
, br
);
348 static void win_reset_brush(frontend
*fe
)
352 assert(fe
->drawstatus
!= NOTHING
);
354 br
= SelectObject(fe
->hdc
, fe
->oldbr
);
355 if (fe
->drawstatus
== PRINTING
)
359 static void win_set_pen(frontend
*fe
, int colour
, int thin
)
362 assert(fe
->drawstatus
!= NOTHING
);
364 if (fe
->drawstatus
== PRINTING
) {
367 int width
= thin ?
0 : fe
->linewidth
;
369 print_get_colour(fe
->dr
, colour
, &hatch
, &r
, &g
, &b
);
371 pen
= CreatePen(PS_SOLID
, width
,
372 RGB(r
* 255, g
* 255, b
* 255));
373 else if (hatch
== HATCH_SOLID
)
374 pen
= CreatePen(PS_SOLID
, width
, RGB(0, 0, 0));
375 else if (hatch
== HATCH_CLEAR
)
376 pen
= CreatePen(PS_SOLID
, width
, RGB(255,255,255));
378 assert(!"This shouldn't happen");
379 pen
= CreatePen(PS_SOLID
, 1, RGB(0, 0, 0));
382 pen
= fe
->pens
[colour
];
384 fe
->oldpen
= SelectObject(fe
->hdc
, pen
);
387 static void win_reset_pen(frontend
*fe
)
391 assert(fe
->drawstatus
!= NOTHING
);
393 pen
= SelectObject(fe
->hdc
, fe
->oldpen
);
394 if (fe
->drawstatus
== PRINTING
)
398 static void win_clip(void *handle
, int x
, int y
, int w
, int h
)
400 frontend
*fe
= (frontend
*)handle
;
403 if (fe
->drawstatus
== NOTHING
)
406 p
= win_transform_point(fe
, x
, y
);
407 q
= win_transform_point(fe
, x
+w
, y
+h
);
408 IntersectClipRect(fe
->hdc
, p
.x
, p
.y
, q
.x
, q
.y
);
411 static void win_unclip(void *handle
)
413 frontend
*fe
= (frontend
*)handle
;
415 if (fe
->drawstatus
== NOTHING
)
418 SelectClipRgn(fe
->hdc
, NULL
);
421 static void win_draw_text(void *handle
, int x
, int y
, int fonttype
,
422 int fontsize
, int align
, int colour
, char *text
)
424 frontend
*fe
= (frontend
*)handle
;
428 if (fe
->drawstatus
== NOTHING
)
431 if (fe
->drawstatus
== PRINTING
)
432 fontsize
= (int)(fontsize
* fe
->printpixelscale
);
434 xy
= win_transform_point(fe
, x
, y
);
437 * Find or create the font.
439 for (i
= fe
->fontstart
; i
< fe
->nfonts
; i
++)
440 if (fe
->fonts
[i
].type
== fonttype
&& fe
->fonts
[i
].size
== fontsize
)
443 if (i
== fe
->nfonts
) {
444 if (fe
->fontsize
<= fe
->nfonts
) {
445 fe
->fontsize
= fe
->nfonts
+ 10;
446 fe
->fonts
= sresize(fe
->fonts
, fe
->fontsize
, struct font
);
451 fe
->fonts
[i
].type
= fonttype
;
452 fe
->fonts
[i
].size
= fontsize
;
454 fe
->fonts
[i
].font
= CreateFont(-fontsize
, 0, 0, 0,
455 fe
->drawstatus
== PRINTING ?
0 : FW_BOLD
,
456 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
457 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
459 (fonttype
== FONT_FIXED ?
460 FIXED_PITCH
| FF_DONTCARE
:
461 VARIABLE_PITCH
| FF_SWISS
),
466 * Position and draw the text.
473 oldfont
= SelectObject(fe
->hdc
, fe
->fonts
[i
].font
);
474 if (GetTextMetrics(fe
->hdc
, &tm
)) {
475 if (align
& ALIGN_VCENTRE
)
476 xy
.y
-= (tm
.tmAscent
+tm
.tmDescent
)/2;
480 if (GetTextExtentPoint32(fe
->hdc
, text
, strlen(text
), &size
)) {
481 if (align
& ALIGN_HCENTRE
)
483 else if (align
& ALIGN_HRIGHT
)
486 SetBkMode(fe
->hdc
, TRANSPARENT
);
487 win_text_colour(fe
, colour
);
488 TextOut(fe
->hdc
, xy
.x
, xy
.y
, text
, strlen(text
));
489 SelectObject(fe
->hdc
, oldfont
);
493 static void win_draw_rect(void *handle
, int x
, int y
, int w
, int h
, int colour
)
495 frontend
*fe
= (frontend
*)handle
;
498 if (fe
->drawstatus
== NOTHING
)
501 if (fe
->drawstatus
== DRAWING
&& w
== 1 && h
== 1) {
503 * Rectangle() appears to get uppity if asked to draw a 1x1
504 * rectangle, presumably on the grounds that that's beneath
505 * its dignity and you ought to be using SetPixel instead.
508 SetPixel(fe
->hdc
, x
, y
, fe
->colours
[colour
]);
510 win_set_brush(fe
, colour
);
511 win_set_pen(fe
, colour
, TRUE
);
512 p
= win_transform_point(fe
, x
, y
);
513 q
= win_transform_point(fe
, x
+w
, y
+h
);
514 Rectangle(fe
->hdc
, p
.x
, p
.y
, q
.x
, q
.y
);
520 static void win_draw_line(void *handle
, int x1
, int y1
, int x2
, int y2
, int colour
)
522 frontend
*fe
= (frontend
*)handle
;
525 if (fe
->drawstatus
== NOTHING
)
528 win_set_pen(fe
, colour
, FALSE
);
529 p
= win_transform_point(fe
, x1
, y1
);
530 q
= win_transform_point(fe
, x2
, y2
);
531 MoveToEx(fe
->hdc
, p
.x
, p
.y
, NULL
);
532 LineTo(fe
->hdc
, q
.x
, q
.y
);
533 if (fe
->drawstatus
== DRAWING
)
534 SetPixel(fe
->hdc
, q
.x
, q
.y
, fe
->colours
[colour
]);
538 static void win_draw_circle(void *handle
, int cx
, int cy
, int radius
,
539 int fillcolour
, int outlinecolour
)
541 frontend
*fe
= (frontend
*)handle
;
544 assert(outlinecolour
>= 0);
546 if (fe
->drawstatus
== NOTHING
)
549 if (fillcolour
>= 0) {
550 win_set_brush(fe
, fillcolour
);
551 win_set_pen(fe
, outlinecolour
, FALSE
);
552 p
= win_transform_point(fe
, cx
- radius
, cy
- radius
);
553 q
= win_transform_point(fe
, cx
+ radius
, cy
+ radius
);
554 Ellipse(fe
->hdc
, p
.x
, p
.y
, q
.x
+1, q
.y
+1);
558 win_set_pen(fe
, outlinecolour
, FALSE
);
559 p
= win_transform_point(fe
, cx
- radius
, cy
- radius
);
560 q
= win_transform_point(fe
, cx
+ radius
, cy
+ radius
);
561 r
= win_transform_point(fe
, cx
- radius
, cy
);
562 Arc(fe
->hdc
, p
.x
, p
.y
, q
.x
+1, q
.y
+1, r
.x
, r
.y
, r
.x
, r
.y
);
567 static void win_draw_polygon(void *handle
, int *coords
, int npoints
,
568 int fillcolour
, int outlinecolour
)
570 frontend
*fe
= (frontend
*)handle
;
574 if (fe
->drawstatus
== NOTHING
)
577 pts
= snewn(npoints
+1, POINT
);
579 for (i
= 0; i
<= npoints
; i
++) {
580 int j
= (i
< npoints ? i
: 0);
581 pts
[i
] = win_transform_point(fe
, coords
[j
*2], coords
[j
*2+1]);
584 assert(outlinecolour
>= 0);
586 if (fillcolour
>= 0) {
587 win_set_brush(fe
, fillcolour
);
588 win_set_pen(fe
, outlinecolour
, FALSE
);
589 Polygon(fe
->hdc
, pts
, npoints
);
593 win_set_pen(fe
, outlinecolour
, FALSE
);
594 Polyline(fe
->hdc
, pts
, npoints
+1);
601 static void win_start_draw(void *handle
)
603 frontend
*fe
= (frontend
*)handle
;
606 assert(fe
->drawstatus
== NOTHING
);
608 hdc_win
= GetDC(fe
->hwnd
);
609 fe
->hdc
= CreateCompatibleDC(hdc_win
);
610 fe
->prevbm
= SelectObject(fe
->hdc
, fe
->bitmap
);
611 ReleaseDC(fe
->hwnd
, hdc_win
);
613 SetMapMode(fe
->hdc
, MM_TEXT
);
614 fe
->drawstatus
= DRAWING
;
617 static void win_draw_update(void *handle
, int x
, int y
, int w
, int h
)
619 frontend
*fe
= (frontend
*)handle
;
622 if (fe
->drawstatus
!= DRAWING
)
630 InvalidateRect(fe
->hwnd
, &r
, FALSE
);
633 static void win_end_draw(void *handle
)
635 frontend
*fe
= (frontend
*)handle
;
636 assert(fe
->drawstatus
== DRAWING
);
637 SelectObject(fe
->hdc
, fe
->prevbm
);
640 DeleteObject(fe
->clip
);
643 fe
->drawstatus
= NOTHING
;
646 static void win_line_width(void *handle
, float width
)
648 frontend
*fe
= (frontend
*)handle
;
650 assert(fe
->drawstatus
!= DRAWING
);
651 if (fe
->drawstatus
== NOTHING
)
654 fe
->linewidth
= (int)(width
* fe
->printpixelscale
);
657 static void win_begin_doc(void *handle
, int pages
)
659 frontend
*fe
= (frontend
*)handle
;
661 assert(fe
->drawstatus
!= DRAWING
);
662 if (fe
->drawstatus
== NOTHING
)
665 if (StartDoc(fe
->hdc
, &fe
->di
) <= 0) {
666 char *e
= geterrstr();
667 MessageBox(fe
->hwnd
, e
, "Error starting to print",
668 MB_ICONERROR
| MB_OK
);
670 fe
->drawstatus
= NOTHING
;
674 * Push a marker on the font stack so that we won't use the
675 * same fonts for printing and drawing. (This is because
676 * drawing seems to look generally better in bold, but printing
677 * is better not in bold.)
679 fe
->fontstart
= fe
->nfonts
;
682 static void win_begin_page(void *handle
, int number
)
684 frontend
*fe
= (frontend
*)handle
;
686 assert(fe
->drawstatus
!= DRAWING
);
687 if (fe
->drawstatus
== NOTHING
)
690 if (StartPage(fe
->hdc
) <= 0) {
691 char *e
= geterrstr();
692 MessageBox(fe
->hwnd
, e
, "Error starting a page",
693 MB_ICONERROR
| MB_OK
);
695 fe
->drawstatus
= NOTHING
;
699 static void win_begin_puzzle(void *handle
, float xm
, float xc
,
700 float ym
, float yc
, int pw
, int ph
, float wmm
)
702 frontend
*fe
= (frontend
*)handle
;
703 int ppw
, pph
, pox
, poy
;
704 float mmpw
, mmph
, mmox
, mmoy
;
707 assert(fe
->drawstatus
!= DRAWING
);
708 if (fe
->drawstatus
== NOTHING
)
711 ppw
= GetDeviceCaps(fe
->hdc
, HORZRES
);
712 pph
= GetDeviceCaps(fe
->hdc
, VERTRES
);
713 mmpw
= (float)GetDeviceCaps(fe
->hdc
, HORZSIZE
);
714 mmph
= (float)GetDeviceCaps(fe
->hdc
, VERTSIZE
);
717 * Compute the puzzle's position on the logical page.
719 mmox
= xm
* mmpw
+ xc
;
720 mmoy
= ym
* mmph
+ yc
;
723 * Work out what that comes to in pixels.
725 pox
= (int)(mmox
* (float)ppw
/ mmpw
);
726 poy
= (int)(mmoy
* (float)ppw
/ mmpw
);
729 * And determine the scale.
731 * I need a scale such that the maximum puzzle-coordinate
732 * extent of the rectangle (pw * scale) is equal to the pixel
733 * equivalent of the puzzle's millimetre width (wmm * ppw /
736 scale
= (wmm
* ppw
) / (mmpw
* pw
);
739 * Now store pox, poy and scale for use in the main drawing
742 fe
->printoffsetx
= pox
;
743 fe
->printoffsety
= poy
;
744 fe
->printpixelscale
= scale
;
749 static void win_end_puzzle(void *handle
)
751 /* Nothing needs to be done here. */
754 static void win_end_page(void *handle
, int number
)
756 frontend
*fe
= (frontend
*)handle
;
758 assert(fe
->drawstatus
!= DRAWING
);
760 if (fe
->drawstatus
== NOTHING
)
763 if (EndPage(fe
->hdc
) <= 0) {
764 char *e
= geterrstr();
765 MessageBox(fe
->hwnd
, e
, "Error finishing a page",
766 MB_ICONERROR
| MB_OK
);
768 fe
->drawstatus
= NOTHING
;
772 static void win_end_doc(void *handle
)
774 frontend
*fe
= (frontend
*)handle
;
776 assert(fe
->drawstatus
!= DRAWING
);
779 * Free all the fonts created since we began printing.
781 while (fe
->nfonts
> fe
->fontstart
) {
783 DeleteObject(fe
->fonts
[fe
->nfonts
].font
);
788 * The MSDN web site sample code doesn't bother to call EndDoc
789 * if an error occurs half way through printing. I expect doing
790 * so would cause the erroneous document to actually be
791 * printed, or something equally undesirable.
793 if (fe
->drawstatus
== NOTHING
)
796 if (EndDoc(fe
->hdc
) <= 0) {
797 char *e
= geterrstr();
798 MessageBox(fe
->hwnd
, e
, "Error finishing printing",
799 MB_ICONERROR
| MB_OK
);
801 fe
->drawstatus
= NOTHING
;
805 const struct drawing_api win_drawing
= {
830 void print(frontend
*fe
)
835 midend
*nme
= NULL
; /* non-interactive midend for bulk puzzle generation */
840 * Create our document structure and fill it up with puzzles.
842 doc
= document_new(fe
->printw
, fe
->printh
, fe
->printscale
/ 100.0F
);
843 for (i
= 0; i
< fe
->printcount
; i
++) {
844 if (i
== 0 && fe
->printcurr
) {
845 err
= midend_print_puzzle(fe
->me
, doc
, fe
->printsolns
);
850 nme
= midend_new(NULL
, &thegame
, NULL
, NULL
);
853 * Set the non-interactive mid-end to have the same
854 * parameters as the standard one.
856 params
= midend_get_params(fe
->me
);
857 midend_set_params(nme
, params
);
858 thegame
.free_params(params
);
861 midend_new_game(nme
);
862 err
= midend_print_puzzle(nme
, doc
, fe
->printsolns
);
871 MessageBox(fe
->hwnd
, err
, "Error preparing puzzles for printing",
872 MB_ICONERROR
| MB_OK
);
877 memset(&pd
, 0, sizeof(pd
));
878 pd
.lStructSize
= sizeof(pd
);
879 pd
.hwndOwner
= fe
->hwnd
;
882 pd
.Flags
= PD_USEDEVMODECOPIESANDCOLLATE
| PD_RETURNDC
|
883 PD_NOPAGENUMS
| PD_NOSELECTION
;
885 pd
.nFromPage
= pd
.nToPage
= 0xFFFF;
886 pd
.nMinPage
= pd
.nMaxPage
= 1;
888 if (!PrintDlg(&pd
)) {
894 * Now pd.hDC is a device context for the printer.
898 * FIXME: IWBNI we put up an Abort box here.
901 memset(&fe
->di
, 0, sizeof(fe
->di
));
902 fe
->di
.cbSize
= sizeof(fe
->di
);
903 sprintf(doctitle
, "Printed puzzles from %s (from Simon Tatham's"
904 " Portable Puzzle Collection)", thegame
.name
);
905 fe
->di
.lpszDocName
= doctitle
;
906 fe
->di
.lpszOutput
= NULL
;
907 fe
->di
.lpszDatatype
= NULL
;
910 fe
->drawstatus
= PRINTING
;
913 fe
->dr
= drawing_init(&win_drawing
, fe
);
914 document_print(doc
, fe
->dr
);
915 drawing_free(fe
->dr
);
918 fe
->drawstatus
= NOTHING
;
924 void deactivate_timer(frontend
*fe
)
927 return; /* for non-interactive midend */
928 if (fe
->hwnd
) KillTimer(fe
->hwnd
, fe
->timer
);
932 void activate_timer(frontend
*fe
)
935 return; /* for non-interactive midend */
937 fe
->timer
= SetTimer(fe
->hwnd
, fe
->timer
, 20, NULL
);
938 fe
->timer_last_tickcount
= GetTickCount();
942 void write_clip(HWND hwnd
, char *data
)
950 * Windows expects CRLF in the clipboard, so we must convert
951 * any \n that has come out of the puzzle backend.
954 for (i
= 0; data
[i
]; i
++) {
959 data2
= snewn(len
+1, char);
961 for (i
= 0; data
[i
]; i
++) {
964 data2
[j
++] = data
[i
];
969 clipdata
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, len
+ 1);
972 lock
= GlobalLock(clipdata
);
975 memcpy(lock
, data2
, len
);
976 ((unsigned char *) lock
)[len
] = 0;
977 GlobalUnlock(clipdata
);
979 if (OpenClipboard(hwnd
)) {
981 SetClipboardData(CF_TEXT
, clipdata
);
984 GlobalFree(clipdata
);
990 * See if we can find a help file.
992 static void find_help_file(frontend
*fe
)
994 char b
[2048], *p
, *q
, *r
;
996 if (!fe
->help_path
) {
997 GetModuleFileName(NULL
, b
, sizeof(b
) - 1);
999 p
= strrchr(b
, '\\');
1000 if (p
&& p
>= r
) r
= p
+1;
1001 q
= strrchr(b
, ':');
1002 if (q
&& q
>= r
) r
= q
+1;
1003 strcpy(r
, HELP_FILE_NAME
);
1004 if ( (fp
= fopen(b
, "r")) != NULL
) {
1005 fe
->help_path
= dupstr(b
);
1008 fe
->help_path
= NULL
;
1009 strcpy(r
, HELP_CNT_NAME
);
1010 if ( (fp
= fopen(b
, "r")) != NULL
) {
1011 fe
->help_has_contents
= TRUE
;
1014 fe
->help_has_contents
= FALSE
;
1018 static void check_window_size(frontend
*fe
, int *px
, int *py
)
1023 if (fe
->statusbar
) {
1025 GetWindowRect(fe
->statusbar
, &sr
);
1026 sy
= sr
.bottom
- sr
.top
;
1032 * See if we actually got the window size we wanted, and adjust
1033 * the puzzle size if not.
1035 GetClientRect(fe
->hwnd
, &r
);
1036 x
= r
.right
- r
.left
;
1037 y
= r
.bottom
- r
.top
- sy
;
1038 midend_size(fe
->me
, &x
, &y
, FALSE
);
1039 if (x
!= r
.right
- r
.left
|| y
!= r
.bottom
- r
.top
) {
1041 * Resize the window, now we know what size we _really_
1047 AdjustWindowRectEx(&r
, WS_OVERLAPPEDWINDOW
&~
1048 (WS_THICKFRAME
| WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
1050 SetWindowPos(fe
->hwnd
, NULL
, 0, 0, r
.right
- r
.left
, r
.bottom
- r
.top
,
1051 SWP_NOMOVE
| SWP_NOZORDER
);
1054 if (fe
->statusbar
) {
1055 GetClientRect(fe
->hwnd
, &r
);
1056 SetWindowPos(fe
->statusbar
, NULL
, 0, r
.bottom
-r
.top
-sy
, r
.right
-r
.left
,
1064 static frontend
*new_window(HINSTANCE inst
, char *game_id
, char **error
)
1071 fe
= snew(frontend
);
1073 fe
->me
= midend_new(fe
, &thegame
, &win_drawing
, fe
);
1076 *error
= midend_game_id(fe
->me
, game_id
);
1078 midend_free(fe
->me
);
1084 fe
->help_path
= NULL
;
1092 fe
->drawstatus
= NOTHING
;
1096 midend_new_game(fe
->me
);
1099 fe
->nfonts
= fe
->fontsize
= 0;
1101 fe
->laststatus
= NULL
;
1107 colours
= midend_colours(fe
->me
, &ncolours
);
1109 fe
->colours
= snewn(ncolours
, COLORREF
);
1110 fe
->brushes
= snewn(ncolours
, HBRUSH
);
1111 fe
->pens
= snewn(ncolours
, HPEN
);
1113 for (i
= 0; i
< ncolours
; i
++) {
1114 fe
->colours
[i
] = RGB(255 * colours
[i
*3+0],
1115 255 * colours
[i
*3+1],
1116 255 * colours
[i
*3+2]);
1117 fe
->brushes
[i
] = CreateSolidBrush(fe
->colours
[i
]);
1118 fe
->pens
[i
] = CreatePen(PS_SOLID
, 1, fe
->colours
[i
]);
1122 x
= y
= INT_MAX
; /* find puzzle's preferred size */
1123 midend_size(fe
->me
, &x
, &y
, FALSE
);
1128 AdjustWindowRectEx(&r
, WS_OVERLAPPEDWINDOW
&~
1129 (WS_THICKFRAME
| WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
1132 fe
->hwnd
= CreateWindowEx(0, thegame
.name
, thegame
.name
,
1133 WS_OVERLAPPEDWINDOW
&~
1134 (WS_THICKFRAME
| WS_MAXIMIZEBOX
),
1135 CW_USEDEFAULT
, CW_USEDEFAULT
,
1136 r
.right
- r
.left
, r
.bottom
- r
.top
,
1137 NULL
, NULL
, inst
, NULL
);
1139 if (midend_wants_statusbar(fe
->me
)) {
1141 fe
->statusbar
= CreateWindowEx(0, STATUSCLASSNAME
, "ooh",
1142 WS_CHILD
| WS_VISIBLE
,
1143 0, 0, 0, 0, /* status bar does these */
1144 fe
->hwnd
, NULL
, inst
, NULL
);
1146 * Now resize the window to take account of the status bar.
1148 GetWindowRect(fe
->statusbar
, &sr
);
1149 GetWindowRect(fe
->hwnd
, &r
);
1150 SetWindowPos(fe
->hwnd
, NULL
, 0, 0, r
.right
- r
.left
,
1151 r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
1152 SWP_NOMOVE
| SWP_NOZORDER
);
1154 fe
->statusbar
= NULL
;
1158 HMENU bar
= CreateMenu();
1159 HMENU menu
= CreateMenu();
1161 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)menu
, "Game");
1162 AppendMenu(menu
, MF_ENABLED
, IDM_NEW
, "New");
1163 AppendMenu(menu
, MF_ENABLED
, IDM_RESTART
, "Restart");
1164 AppendMenu(menu
, MF_ENABLED
, IDM_DESC
, "Specific...");
1165 AppendMenu(menu
, MF_ENABLED
, IDM_SEED
, "Random Seed...");
1167 if ((fe
->npresets
= midend_num_presets(fe
->me
)) > 0 ||
1168 thegame
.can_configure
) {
1169 HMENU sub
= CreateMenu();
1172 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)sub
, "Type");
1174 fe
->presets
= snewn(fe
->npresets
, game_params
*);
1176 for (i
= 0; i
< fe
->npresets
; i
++) {
1179 midend_fetch_preset(fe
->me
, i
, &name
, &fe
->presets
[i
]);
1182 * FIXME: we ought to go through and do something
1183 * with ampersands here.
1186 AppendMenu(sub
, MF_ENABLED
, IDM_PRESETS
+ 0x10 * i
, name
);
1189 if (thegame
.can_configure
) {
1190 AppendMenu(sub
, MF_ENABLED
, IDM_CONFIG
, "Custom...");
1194 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
1195 AppendMenu(menu
, MF_ENABLED
, IDM_LOAD
, "Load");
1196 AppendMenu(menu
, MF_ENABLED
, IDM_SAVE
, "Save");
1197 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
1198 if (thegame
.can_print
) {
1199 AppendMenu(menu
, MF_ENABLED
, IDM_PRINT
, "Print");
1200 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
1202 AppendMenu(menu
, MF_ENABLED
, IDM_UNDO
, "Undo");
1203 AppendMenu(menu
, MF_ENABLED
, IDM_REDO
, "Redo");
1204 if (thegame
.can_format_as_text
) {
1205 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
1206 AppendMenu(menu
, MF_ENABLED
, IDM_COPY
, "Copy");
1208 if (thegame
.can_solve
) {
1209 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
1210 AppendMenu(menu
, MF_ENABLED
, IDM_SOLVE
, "Solve");
1212 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
1213 AppendMenu(menu
, MF_ENABLED
, IDM_QUIT
, "Exit");
1214 menu
= CreateMenu();
1215 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)menu
, "Help");
1216 AppendMenu(menu
, MF_ENABLED
, IDM_ABOUT
, "About");
1217 if (fe
->help_path
) {
1218 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
1219 AppendMenu(menu
, MF_ENABLED
, IDM_HELPC
, "Contents");
1220 if (thegame
.winhelp_topic
) {
1222 assert(thegame
.name
);
1223 item
= snewn(9+strlen(thegame
.name
), char); /*ick*/
1224 sprintf(item
, "Help on %s", thegame
.name
);
1225 AppendMenu(menu
, MF_ENABLED
, IDM_GAMEHELP
, item
);
1229 SetMenu(fe
->hwnd
, bar
);
1232 check_window_size(fe
, &x
, &y
);
1234 hdc
= GetDC(fe
->hwnd
);
1235 fe
->bitmap
= CreateCompatibleBitmap(hdc
, x
, y
);
1236 ReleaseDC(fe
->hwnd
, hdc
);
1238 SetWindowLong(fe
->hwnd
, GWL_USERDATA
, (LONG
)fe
);
1240 ShowWindow(fe
->hwnd
, SW_NORMAL
);
1241 SetForegroundWindow(fe
->hwnd
);
1243 midend_redraw(fe
->me
);
1248 static int CALLBACK
AboutDlgProc(HWND hwnd
, UINT msg
,
1249 WPARAM wParam
, LPARAM lParam
)
1251 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
1258 if ((HIWORD(wParam
) == BN_CLICKED
||
1259 HIWORD(wParam
) == BN_DOUBLECLICKED
) &&
1260 LOWORD(wParam
) == IDOK
)
1273 * Wrappers on midend_{get,set}_config, which extend the CFG_*
1274 * enumeration to add CFG_PRINT.
1276 static config_item
*frontend_get_config(frontend
*fe
, int which
,
1279 if (which
< CFG_FRONTEND_SPECIFIC
) {
1280 return midend_get_config(fe
->me
, which
, wintitle
);
1281 } else if (which
== CFG_PRINT
) {
1285 *wintitle
= snewn(40 + strlen(thegame
.name
), char);
1286 sprintf(*wintitle
, "%s print setup", thegame
.name
);
1288 ret
= snewn(8, config_item
);
1292 ret
[i
].name
= "Number of puzzles to print";
1293 ret
[i
].type
= C_STRING
;
1294 ret
[i
].sval
= dupstr("1");
1298 ret
[i
].name
= "Number of puzzles across the page";
1299 ret
[i
].type
= C_STRING
;
1300 ret
[i
].sval
= dupstr("1");
1304 ret
[i
].name
= "Number of puzzles down the page";
1305 ret
[i
].type
= C_STRING
;
1306 ret
[i
].sval
= dupstr("1");
1310 ret
[i
].name
= "Percentage of standard size";
1311 ret
[i
].type
= C_STRING
;
1312 ret
[i
].sval
= dupstr("100.0");
1316 ret
[i
].name
= "Include currently shown puzzle";
1317 ret
[i
].type
= C_BOOLEAN
;
1322 ret
[i
].name
= "Print solutions";
1323 ret
[i
].type
= C_BOOLEAN
;
1325 ret
[i
].ival
= FALSE
;
1328 if (thegame
.can_print_in_colour
) {
1329 ret
[i
].name
= "Print in colour";
1330 ret
[i
].type
= C_BOOLEAN
;
1332 ret
[i
].ival
= FALSE
;
1337 ret
[i
].type
= C_END
;
1344 assert(!"We should never get here");
1349 static char *frontend_set_config(frontend
*fe
, int which
, config_item
*cfg
)
1351 if (which
< CFG_FRONTEND_SPECIFIC
) {
1352 return midend_set_config(fe
->me
, which
, cfg
);
1353 } else if (which
== CFG_PRINT
) {
1354 if ((fe
->printcount
= atoi(cfg
[0].sval
)) <= 0)
1355 return "Number of puzzles to print should be at least one";
1356 if ((fe
->printw
= atoi(cfg
[1].sval
)) <= 0)
1357 return "Number of puzzles across the page should be at least one";
1358 if ((fe
->printh
= atoi(cfg
[2].sval
)) <= 0)
1359 return "Number of puzzles down the page should be at least one";
1360 if ((fe
->printscale
= (float)atof(cfg
[3].sval
)) <= 0)
1361 return "Print size should be positive";
1362 fe
->printcurr
= cfg
[4].ival
;
1363 fe
->printsolns
= cfg
[5].ival
;
1364 fe
->printcolour
= thegame
.can_print_in_colour
&& cfg
[6].ival
;
1367 assert(!"We should never get here");
1368 return "Internal error";
1372 static int CALLBACK
ConfigDlgProc(HWND hwnd
, UINT msg
,
1373 WPARAM wParam
, LPARAM lParam
)
1375 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
1385 * OK and Cancel are special cases.
1387 if ((HIWORD(wParam
) == BN_CLICKED
||
1388 HIWORD(wParam
) == BN_DOUBLECLICKED
) &&
1389 (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)) {
1390 if (LOWORD(wParam
) == IDOK
) {
1391 char *err
= frontend_set_config(fe
, fe
->cfg_which
, fe
->cfg
);
1394 MessageBox(hwnd
, err
, "Validation error",
1395 MB_ICONERROR
| MB_OK
);
1406 * First find the control whose id this is.
1408 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
1409 if (j
->ctlid
== LOWORD(wParam
))
1412 if (i
->type
== C_END
)
1413 return 0; /* not our problem */
1415 if (i
->type
== C_STRING
&& HIWORD(wParam
) == EN_CHANGE
) {
1417 GetDlgItemText(fe
->cfgbox
, j
->ctlid
, buffer
, lenof(buffer
));
1418 buffer
[lenof(buffer
)-1] = '\0';
1420 i
->sval
= dupstr(buffer
);
1421 } else if (i
->type
== C_BOOLEAN
&&
1422 (HIWORD(wParam
) == BN_CLICKED
||
1423 HIWORD(wParam
) == BN_DOUBLECLICKED
)) {
1424 i
->ival
= IsDlgButtonChecked(fe
->cfgbox
, j
->ctlid
);
1425 } else if (i
->type
== C_CHOICES
&&
1426 HIWORD(wParam
) == CBN_SELCHANGE
) {
1427 i
->ival
= SendDlgItemMessage(fe
->cfgbox
, j
->ctlid
,
1428 CB_GETCURSEL
, 0, 0);
1441 HWND
mkctrl(frontend
*fe
, int x1
, int x2
, int y1
, int y2
,
1442 char *wclass
, int wstyle
,
1443 int exstyle
, const char *wtext
, int wid
)
1446 ret
= CreateWindowEx(exstyle
, wclass
, wtext
,
1447 wstyle
| WS_CHILD
| WS_VISIBLE
, x1
, y1
, x2
-x1
, y2
-y1
,
1448 fe
->cfgbox
, (HMENU
) wid
, fe
->inst
, NULL
);
1449 SendMessage(ret
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, MAKELPARAM(TRUE
, 0));
1453 static void about(frontend
*fe
)
1463 int winwidth
, winheight
, y
;
1464 int height
, width
, maxwid
;
1465 const char *strings
[16];
1470 sprintf(titlebuf
, "About %.250s", thegame
.name
);
1472 strings
[nstrings
++] = thegame
.name
;
1473 strings
[nstrings
++] = "from Simon Tatham's Portable Puzzle Collection";
1474 strings
[nstrings
++] = ver
;
1476 wc
.style
= CS_DBLCLKS
| CS_SAVEBITS
| CS_BYTEALIGNWINDOW
;
1477 wc
.lpfnWndProc
= DefDlgProc
;
1479 wc
.cbWndExtra
= DLGWINDOWEXTRA
+ 8;
1480 wc
.hInstance
= fe
->inst
;
1482 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
1483 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+1);
1484 wc
.lpszMenuName
= NULL
;
1485 wc
.lpszClassName
= "GameAboutBox";
1488 hdc
= GetDC(fe
->hwnd
);
1489 SetMapMode(hdc
, MM_TEXT
);
1491 fe
->dlg_done
= FALSE
;
1493 fe
->cfgfont
= CreateFont(-MulDiv(8, GetDeviceCaps(hdc
, LOGPIXELSY
), 72),
1495 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
1496 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
1501 oldfont
= SelectObject(hdc
, fe
->cfgfont
);
1502 if (GetTextMetrics(hdc
, &tm
)) {
1503 height
= tm
.tmAscent
+ tm
.tmDescent
;
1504 width
= tm
.tmAveCharWidth
;
1506 height
= width
= 30;
1510 * Figure out the layout of the About box by measuring the
1511 * length of each piece of text.
1514 winheight
= height
/2;
1516 for (i
= 0; i
< nstrings
; i
++) {
1517 if (GetTextExtentPoint32(hdc
, strings
[i
], strlen(strings
[i
]), &size
))
1518 lengths
[i
] = size
.cx
;
1520 lengths
[i
] = 0; /* *shrug* */
1521 if (maxwid
< lengths
[i
])
1522 maxwid
= lengths
[i
];
1523 winheight
+= height
* 3 / 2 + (height
/ 2);
1526 winheight
+= height
+ height
* 7 / 4; /* OK button */
1527 winwidth
= maxwid
+ 4*width
;
1529 SelectObject(hdc
, oldfont
);
1530 ReleaseDC(fe
->hwnd
, hdc
);
1533 * Create the dialog, now that we know its size.
1540 r
.bottom
= winheight
;
1542 AdjustWindowRectEx(&r
, (WS_OVERLAPPEDWINDOW
/*|
1543 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1544 WS_CAPTION | WS_SYSMENU*/) &~
1545 (WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
1549 * Centre the dialog on its parent window.
1553 GetWindowRect(fe
->hwnd
, &r2
);
1554 r
.left
= (r2
.left
+ r2
.right
- r
.right
) / 2;
1555 r
.top
= (r2
.top
+ r2
.bottom
- r
.bottom
) / 2;
1559 fe
->cfgbox
= CreateWindowEx(0, wc
.lpszClassName
, titlebuf
,
1560 DS_MODALFRAME
| WS_POPUP
| WS_VISIBLE
|
1561 WS_CAPTION
| WS_SYSMENU
,
1563 r
.right
-r
.left
, r
.bottom
-r
.top
,
1564 fe
->hwnd
, NULL
, fe
->inst
, NULL
);
1567 SendMessage(fe
->cfgbox
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, FALSE
);
1569 SetWindowLong(fe
->cfgbox
, GWL_USERDATA
, (LONG
)fe
);
1570 SetWindowLong(fe
->cfgbox
, DWL_DLGPROC
, (LONG
)AboutDlgProc
);
1574 for (i
= 0; i
< nstrings
; i
++) {
1575 int border
= width
*2 + (maxwid
- lengths
[i
]) / 2;
1576 mkctrl(fe
, border
, border
+lengths
[i
], y
+height
*1/8, y
+height
*9/8,
1577 "Static", 0, 0, strings
[i
], id
++);
1580 assert(y
< winheight
);
1584 y
+= height
/2; /* extra space before OK */
1585 mkctrl(fe
, width
*2, maxwid
+width
*2, y
, y
+height
*7/4, "BUTTON",
1586 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
| BS_DEFPUSHBUTTON
, 0,
1589 SendMessage(fe
->cfgbox
, WM_INITDIALOG
, 0, 0);
1591 EnableWindow(fe
->hwnd
, FALSE
);
1592 ShowWindow(fe
->cfgbox
, SW_NORMAL
);
1593 while ((gm
=GetMessage(&msg
, NULL
, 0, 0)) > 0) {
1594 if (!IsDialogMessage(fe
->cfgbox
, &msg
))
1595 DispatchMessage(&msg
);
1599 EnableWindow(fe
->hwnd
, TRUE
);
1600 SetForegroundWindow(fe
->hwnd
);
1601 DestroyWindow(fe
->cfgbox
);
1602 DeleteObject(fe
->cfgfont
);
1605 static int get_config(frontend
*fe
, int which
)
1618 int winwidth
, winheight
, col1l
, col1r
, col2l
, col2r
, y
;
1619 int height
, width
, maxlabel
, maxcheckbox
;
1621 wc
.style
= CS_DBLCLKS
| CS_SAVEBITS
| CS_BYTEALIGNWINDOW
;
1622 wc
.lpfnWndProc
= DefDlgProc
;
1624 wc
.cbWndExtra
= DLGWINDOWEXTRA
+ 8;
1625 wc
.hInstance
= fe
->inst
;
1627 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
1628 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+1);
1629 wc
.lpszMenuName
= NULL
;
1630 wc
.lpszClassName
= "GameConfigBox";
1633 hdc
= GetDC(fe
->hwnd
);
1634 SetMapMode(hdc
, MM_TEXT
);
1636 fe
->dlg_done
= FALSE
;
1638 fe
->cfgfont
= CreateFont(-MulDiv(8, GetDeviceCaps(hdc
, LOGPIXELSY
), 72),
1640 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
1641 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
1646 oldfont
= SelectObject(hdc
, fe
->cfgfont
);
1647 if (GetTextMetrics(hdc
, &tm
)) {
1648 height
= tm
.tmAscent
+ tm
.tmDescent
;
1649 width
= tm
.tmAveCharWidth
;
1651 height
= width
= 30;
1654 fe
->cfg
= frontend_get_config(fe
, which
, &title
);
1655 fe
->cfg_which
= which
;
1658 * Figure out the layout of the config box by measuring the
1659 * length of each piece of text.
1661 maxlabel
= maxcheckbox
= 0;
1662 winheight
= height
/2;
1664 for (i
= fe
->cfg
; i
->type
!= C_END
; i
++) {
1669 * Both these control types have a label filling only
1670 * the left-hand column of the box.
1672 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
1675 winheight
+= height
* 3 / 2 + (height
/ 2);
1680 * Checkboxes take up the whole of the box width.
1682 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
1683 maxcheckbox
< size
.cx
)
1684 maxcheckbox
= size
.cx
;
1685 winheight
+= height
+ (height
/ 2);
1690 winheight
+= height
+ height
* 7 / 4; /* OK / Cancel buttons */
1693 col1r
= col1l
+ maxlabel
;
1694 col2l
= col1r
+ 2*width
;
1695 col2r
= col2l
+ 30*width
;
1696 if (col2r
< col1l
+2*height
+maxcheckbox
)
1697 col2r
= col1l
+2*height
+maxcheckbox
;
1698 winwidth
= col2r
+ 2*width
;
1700 SelectObject(hdc
, oldfont
);
1701 ReleaseDC(fe
->hwnd
, hdc
);
1704 * Create the dialog, now that we know its size.
1711 r
.bottom
= winheight
;
1713 AdjustWindowRectEx(&r
, (WS_OVERLAPPEDWINDOW
/*|
1714 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
1715 WS_CAPTION | WS_SYSMENU*/) &~
1716 (WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
1720 * Centre the dialog on its parent window.
1724 GetWindowRect(fe
->hwnd
, &r2
);
1725 r
.left
= (r2
.left
+ r2
.right
- r
.right
) / 2;
1726 r
.top
= (r2
.top
+ r2
.bottom
- r
.bottom
) / 2;
1730 fe
->cfgbox
= CreateWindowEx(0, wc
.lpszClassName
, title
,
1731 DS_MODALFRAME
| WS_POPUP
| WS_VISIBLE
|
1732 WS_CAPTION
| WS_SYSMENU
,
1734 r
.right
-r
.left
, r
.bottom
-r
.top
,
1735 fe
->hwnd
, NULL
, fe
->inst
, NULL
);
1739 SendMessage(fe
->cfgbox
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, FALSE
);
1741 SetWindowLong(fe
->cfgbox
, GWL_USERDATA
, (LONG
)fe
);
1742 SetWindowLong(fe
->cfgbox
, DWL_DLGPROC
, (LONG
)ConfigDlgProc
);
1745 * Count the controls so we can allocate cfgaux.
1747 for (nctrls
= 0, i
= fe
->cfg
; i
->type
!= C_END
; i
++)
1749 fe
->cfgaux
= snewn(nctrls
, struct cfg_aux
);
1753 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
1757 * Edit box with a label beside it.
1759 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
1760 "Static", 0, 0, i
->name
, id
++);
1761 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*3/2,
1762 "EDIT", WS_TABSTOP
| ES_AUTOHSCROLL
,
1763 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
1764 SetWindowText(ctl
, i
->sval
);
1772 mkctrl(fe
, col1l
, col2r
, y
, y
+height
, "BUTTON",
1773 BS_NOTIFY
| BS_AUTOCHECKBOX
| WS_TABSTOP
,
1774 0, i
->name
, (j
->ctlid
= id
++));
1775 CheckDlgButton(fe
->cfgbox
, j
->ctlid
, (i
->ival
!= 0));
1781 * Drop-down list with a label beside it.
1783 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
1784 "STATIC", 0, 0, i
->name
, id
++);
1785 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*41/2,
1786 "COMBOBOX", WS_TABSTOP
|
1787 CBS_DROPDOWNLIST
| CBS_HASSTRINGS
,
1788 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
1790 char c
, *p
, *q
, *str
;
1792 SendMessage(ctl
, CB_RESETCONTENT
, 0, 0);
1797 while (*q
&& *q
!= c
) q
++;
1798 str
= snewn(q
-p
+1, char);
1799 strncpy(str
, p
, q
-p
);
1801 SendMessage(ctl
, CB_ADDSTRING
, 0, (LPARAM
)str
);
1808 SendMessage(ctl
, CB_SETCURSEL
, i
->ival
, 0);
1814 assert(y
< winheight
);
1818 y
+= height
/2; /* extra space before OK and Cancel */
1819 mkctrl(fe
, col1l
, (col1l
+col2r
)/2-width
, y
, y
+height
*7/4, "BUTTON",
1820 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
| BS_DEFPUSHBUTTON
, 0,
1822 mkctrl(fe
, (col1l
+col2r
)/2+width
, col2r
, y
, y
+height
*7/4, "BUTTON",
1823 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
, 0, "Cancel", IDCANCEL
);
1825 SendMessage(fe
->cfgbox
, WM_INITDIALOG
, 0, 0);
1827 EnableWindow(fe
->hwnd
, FALSE
);
1828 ShowWindow(fe
->cfgbox
, SW_NORMAL
);
1829 while ((gm
=GetMessage(&msg
, NULL
, 0, 0)) > 0) {
1830 if (!IsDialogMessage(fe
->cfgbox
, &msg
))
1831 DispatchMessage(&msg
);
1835 EnableWindow(fe
->hwnd
, TRUE
);
1836 SetForegroundWindow(fe
->hwnd
);
1837 DestroyWindow(fe
->cfgbox
);
1838 DeleteObject(fe
->cfgfont
);
1843 return (fe
->dlg_done
== 2);
1846 static void new_game_size(frontend
*fe
)
1853 midend_size(fe
->me
, &x
, &y
, FALSE
);
1858 AdjustWindowRectEx(&r
, WS_OVERLAPPEDWINDOW
&~
1859 (WS_THICKFRAME
| WS_MAXIMIZEBOX
|
1863 if (fe
->statusbar
!= NULL
) {
1864 GetWindowRect(fe
->statusbar
, &sr
);
1866 sr
.left
= sr
.right
= sr
.top
= sr
.bottom
= 0;
1868 SetWindowPos(fe
->hwnd
, NULL
, 0, 0,
1870 r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
1871 SWP_NOMOVE
| SWP_NOZORDER
);
1873 check_window_size(fe
, &x
, &y
);
1875 if (fe
->statusbar
!= NULL
)
1876 SetWindowPos(fe
->statusbar
, NULL
, 0, y
, x
,
1877 sr
.bottom
- sr
.top
, SWP_NOZORDER
);
1879 DeleteObject(fe
->bitmap
);
1881 hdc
= GetDC(fe
->hwnd
);
1882 fe
->bitmap
= CreateCompatibleBitmap(hdc
, x
, y
);
1883 ReleaseDC(fe
->hwnd
, hdc
);
1885 midend_redraw(fe
->me
);
1888 static void new_game_type(frontend
*fe
)
1890 midend_new_game(fe
->me
);
1894 static int is_alt_pressed(void)
1897 int r
= GetKeyboardState(keystate
);
1900 if (keystate
[VK_MENU
] & 0x80)
1902 if (keystate
[VK_RMENU
] & 0x80)
1907 static void savefile_write(void *wctx
, void *buf
, int len
)
1909 FILE *fp
= (FILE *)wctx
;
1910 fwrite(buf
, 1, len
, fp
);
1913 static int savefile_read(void *wctx
, void *buf
, int len
)
1915 FILE *fp
= (FILE *)wctx
;
1918 ret
= fread(buf
, 1, len
, fp
);
1919 return (ret
== len
);
1922 static LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
,
1923 WPARAM wParam
, LPARAM lParam
)
1925 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
1930 DestroyWindow(hwnd
);
1933 cmd
= wParam
& ~0xF; /* low 4 bits reserved to Windows */
1936 if (!midend_process_key(fe
->me
, 0, 0, 'n'))
1940 midend_restart_game(fe
->me
);
1943 if (!midend_process_key(fe
->me
, 0, 0, 'u'))
1947 if (!midend_process_key(fe
->me
, 0, 0, '\x12'))
1952 char *text
= midend_text_format(fe
->me
);
1954 write_clip(hwnd
, text
);
1956 MessageBeep(MB_ICONWARNING
);
1962 char *msg
= midend_solve(fe
->me
);
1964 MessageBox(hwnd
, msg
, "Unable to solve",
1965 MB_ICONERROR
| MB_OK
);
1969 if (!midend_process_key(fe
->me
, 0, 0, 'q'))
1973 if (get_config(fe
, CFG_SETTINGS
))
1977 if (get_config(fe
, CFG_SEED
))
1981 if (get_config(fe
, CFG_DESC
))
1985 if (get_config(fe
, CFG_PRINT
))
1995 char filename
[FILENAME_MAX
];
1998 memset(&of
, 0, sizeof(of
));
1999 of
.hwndOwner
= hwnd
;
2000 of
.lpstrFilter
= "All Files (*.*)\0*\0\0\0";
2001 of
.lpstrCustomFilter
= NULL
;
2002 of
.nFilterIndex
= 1;
2003 of
.lpstrFile
= filename
;
2005 of
.nMaxFile
= lenof(filename
);
2006 of
.lpstrFileTitle
= NULL
;
2007 of
.lpstrTitle
= (cmd
== IDM_SAVE ?
2008 "Enter name of game file to save" :
2009 "Enter name of saved game file to load");
2011 #ifdef OPENFILENAME_SIZE_VERSION_400
2012 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
2014 of
.lStructSize
= sizeof(of
);
2016 of
.lpstrInitialDir
= NULL
;
2018 if (cmd
== IDM_SAVE
)
2019 ret
= GetSaveFileName(&of
);
2021 ret
= GetOpenFileName(&of
);
2024 if (cmd
== IDM_SAVE
) {
2027 if ((fp
= fopen(filename
, "r")) != NULL
) {
2028 char buf
[256 + FILENAME_MAX
];
2032 sprintf(buf
, "Are you sure you want to overwrite"
2033 " the file \"%.*s\"?",
2034 FILENAME_MAX
, filename
);
2035 if (MessageBox(hwnd
, buf
, "Question",
2036 MB_YESNO
| MB_ICONQUESTION
)
2041 fp
= fopen(filename
, "w");
2044 MessageBox(hwnd
, "Unable to open save file",
2045 "Error", MB_ICONERROR
| MB_OK
);
2049 midend_serialise(fe
->me
, savefile_write
, fp
);
2053 FILE *fp
= fopen(filename
, "r");
2057 MessageBox(hwnd
, "Unable to open saved game file",
2058 "Error", MB_ICONERROR
| MB_OK
);
2062 err
= midend_deserialise(fe
->me
, savefile_read
, fp
);
2067 MessageBox(hwnd
, err
, "Error", MB_ICONERROR
|MB_OK
);
2078 assert(fe
->help_path
);
2079 WinHelp(hwnd
, fe
->help_path
,
2080 fe
->help_has_contents ? HELP_FINDER
: HELP_CONTENTS
, 0);
2083 assert(fe
->help_path
);
2084 assert(thegame
.winhelp_topic
);
2086 char *cmd
= snewn(10+strlen(thegame
.winhelp_topic
), char);
2087 sprintf(cmd
, "JI(`',`%s')", thegame
.winhelp_topic
);
2088 WinHelp(hwnd
, fe
->help_path
, HELP_COMMAND
, (DWORD
)cmd
);
2094 int p
= ((wParam
&~ 0xF) - IDM_PRESETS
) / 0x10;
2096 if (p
>= 0 && p
< fe
->npresets
) {
2097 midend_set_params(fe
->me
, fe
->presets
[p
]);
2113 hdc
= BeginPaint(hwnd
, &p
);
2114 hdc2
= CreateCompatibleDC(hdc
);
2115 prevbm
= SelectObject(hdc2
, fe
->bitmap
);
2117 p
.rcPaint
.left
, p
.rcPaint
.top
,
2118 p
.rcPaint
.right
- p
.rcPaint
.left
,
2119 p
.rcPaint
.bottom
- p
.rcPaint
.top
,
2121 p
.rcPaint
.left
, p
.rcPaint
.top
,
2123 SelectObject(hdc2
, prevbm
);
2132 int r
= GetKeyboardState(keystate
);
2133 int shift
= (r
&& (keystate
[VK_SHIFT
] & 0x80)) ? MOD_SHFT
: 0;
2134 int ctrl
= (r
&& (keystate
[VK_CONTROL
] & 0x80)) ? MOD_CTRL
: 0;
2138 if (!(lParam
& 0x01000000))
2139 key
= MOD_NUM_KEYPAD
| '4';
2141 key
= shift
| ctrl
| CURSOR_LEFT
;
2144 if (!(lParam
& 0x01000000))
2145 key
= MOD_NUM_KEYPAD
| '6';
2147 key
= shift
| ctrl
| CURSOR_RIGHT
;
2150 if (!(lParam
& 0x01000000))
2151 key
= MOD_NUM_KEYPAD
| '8';
2153 key
= shift
| ctrl
| CURSOR_UP
;
2156 if (!(lParam
& 0x01000000))
2157 key
= MOD_NUM_KEYPAD
| '2';
2159 key
= shift
| ctrl
| CURSOR_DOWN
;
2162 * Diagonal keys on the numeric keypad.
2165 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '9';
2168 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '3';
2171 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '7';
2174 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '1';
2177 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '0';
2180 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '5';
2183 * Numeric keypad keys with Num Lock on.
2185 case VK_NUMPAD4
: key
= MOD_NUM_KEYPAD
| '4'; break;
2186 case VK_NUMPAD6
: key
= MOD_NUM_KEYPAD
| '6'; break;
2187 case VK_NUMPAD8
: key
= MOD_NUM_KEYPAD
| '8'; break;
2188 case VK_NUMPAD2
: key
= MOD_NUM_KEYPAD
| '2'; break;
2189 case VK_NUMPAD5
: key
= MOD_NUM_KEYPAD
| '5'; break;
2190 case VK_NUMPAD9
: key
= MOD_NUM_KEYPAD
| '9'; break;
2191 case VK_NUMPAD3
: key
= MOD_NUM_KEYPAD
| '3'; break;
2192 case VK_NUMPAD7
: key
= MOD_NUM_KEYPAD
| '7'; break;
2193 case VK_NUMPAD1
: key
= MOD_NUM_KEYPAD
| '1'; break;
2194 case VK_NUMPAD0
: key
= MOD_NUM_KEYPAD
| '0'; break;
2198 if (!midend_process_key(fe
->me
, 0, 0, key
))
2203 m
.message
= WM_KEYDOWN
;
2205 m
.lParam
= lParam
& 0xdfff;
2206 TranslateMessage(&m
);
2210 case WM_LBUTTONDOWN
:
2211 case WM_RBUTTONDOWN
:
2212 case WM_MBUTTONDOWN
:
2217 * Shift-clicks count as middle-clicks, since otherwise
2218 * two-button Windows users won't have any kind of
2219 * middle click to use.
2221 if (message
== WM_MBUTTONDOWN
|| (wParam
& MK_SHIFT
))
2222 button
= MIDDLE_BUTTON
;
2223 else if (message
== WM_RBUTTONDOWN
|| is_alt_pressed())
2224 button
= RIGHT_BUTTON
;
2226 button
= LEFT_BUTTON
;
2228 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
2229 (signed short)HIWORD(lParam
), button
))
2242 * Shift-clicks count as middle-clicks, since otherwise
2243 * two-button Windows users won't have any kind of
2244 * middle click to use.
2246 if (message
== WM_MBUTTONUP
|| (wParam
& MK_SHIFT
))
2247 button
= MIDDLE_RELEASE
;
2248 else if (message
== WM_RBUTTONUP
|| is_alt_pressed())
2249 button
= RIGHT_RELEASE
;
2251 button
= LEFT_RELEASE
;
2253 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
2254 (signed short)HIWORD(lParam
), button
))
2264 if (wParam
& (MK_MBUTTON
| MK_SHIFT
))
2265 button
= MIDDLE_DRAG
;
2266 else if (wParam
& MK_RBUTTON
|| is_alt_pressed())
2267 button
= RIGHT_DRAG
;
2271 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
2272 (signed short)HIWORD(lParam
), button
))
2277 if (!midend_process_key(fe
->me
, 0, 0, (unsigned char)wParam
))
2282 DWORD now
= GetTickCount();
2283 float elapsed
= (float) (now
- fe
->timer_last_tickcount
) * 0.001F
;
2284 midend_timer(fe
->me
, elapsed
);
2285 fe
->timer_last_tickcount
= now
;
2290 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
2293 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
)
2298 InitCommonControls();
2304 wndclass
.lpfnWndProc
= WndProc
;
2305 wndclass
.cbClsExtra
= 0;
2306 wndclass
.cbWndExtra
= 0;
2307 wndclass
.hInstance
= inst
;
2308 wndclass
.hIcon
= LoadIcon(inst
, IDI_APPLICATION
);
2309 wndclass
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
2310 wndclass
.hbrBackground
= NULL
;
2311 wndclass
.lpszMenuName
= NULL
;
2312 wndclass
.lpszClassName
= thegame
.name
;
2314 RegisterClass(&wndclass
);
2317 while (*cmdline
&& isspace((unsigned char)*cmdline
))
2320 if (!new_window(inst
, *cmdline ? cmdline
: NULL
, &error
)) {
2322 sprintf(buf
, "%.100s Error", thegame
.name
);
2323 MessageBox(NULL
, error
, buf
, MB_OK
|MB_ICONERROR
);
2327 while (GetMessage(&msg
, NULL
, 0, 0)) {
2328 DispatchMessage(&msg
);