2 * windows.c: Windows front end for my puzzle collection.
9 #endif /* NO_HTMLHELP */
30 #define IDM_NEW 0x0010
31 #define IDM_RESTART 0x0020
32 #define IDM_UNDO 0x0030
33 #define IDM_REDO 0x0040
34 #define IDM_COPY 0x0050
35 #define IDM_SOLVE 0x0060
36 #define IDM_QUIT 0x0070
37 #define IDM_CONFIG 0x0080
38 #define IDM_DESC 0x0090
39 #define IDM_SEED 0x00A0
40 #define IDM_HELPC 0x00B0
41 #define IDM_GAMEHELP 0x00C0
42 #define IDM_ABOUT 0x00D0
43 #define IDM_SAVE 0x00E0
44 #define IDM_LOAD 0x00F0
45 #define IDM_PRINT 0x0100
46 #define IDM_PRESETS 0x0110
47 #define IDM_GAMES 0x0300
49 #define IDM_KEYEMUL 0x0400
51 #define HELP_FILE_NAME "puzzles.hlp"
52 #define HELP_CNT_NAME "puzzles.cnt"
54 #define CHM_FILE_NAME "puzzles.chm"
55 #endif /* NO_HTMLHELP */
58 typedef HWND (CALLBACK
*htmlhelp_t
)(HWND
, LPCSTR
, UINT
, DWORD
);
59 static htmlhelp_t htmlhelp
;
60 static HINSTANCE hh_dll
;
61 #endif /* NO_HTMLHELP */
62 enum { NONE
, HLP
, CHM
} help_type
;
64 int help_has_contents
;
67 #define FILENAME_MAX (260)
71 #define HGDI_ERROR ((HANDLE)GDI_ERROR)
75 #define CLASSNAME "Puzzles"
77 #define CLASSNAME thegame.name
83 * Wrapper implementations of functions not supplied by the
87 #define SHGetSubMenu(hWndMB,ID_MENU) (HMENU)SendMessage((hWndMB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)ID_MENU)
91 int MessageBox(HWND hWnd
, LPCSTR lpText
, LPCSTR lpCaption
, UINT uType
)
96 MultiByteToWideChar (CP_ACP
, 0, lpText
, -1, wText
, 2048);
97 MultiByteToWideChar (CP_ACP
, 0, lpCaption
, -1, wCaption
, 2048);
99 return MessageBoxW (hWnd
, wText
, wCaption
, uType
);
102 BOOL
SetDlgItemTextA(HWND hDlg
, int nIDDlgItem
, LPCSTR lpString
)
106 MultiByteToWideChar (CP_ACP
, 0, lpString
, -1, wText
, 256);
107 return SetDlgItemTextW(hDlg
, nIDDlgItem
, wText
);
110 LPCSTR
getenv(LPCSTR buf
)
115 BOOL
GetKeyboardState(PBYTE pb
)
120 static TCHAR wClassName
[256], wGameName
[256];
125 static FILE *debug_fp
= NULL
;
126 static HANDLE debug_hdl
= INVALID_HANDLE_VALUE
;
127 static int debug_got_console
= 0;
129 void dputs(char *buf
)
133 if (!debug_got_console) {
134 if (AllocConsole()) {
135 debug_got_console = 1;
136 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
140 debug_fp = fopen("debug.log", "w");
143 if (debug_hdl != INVALID_HANDLE_VALUE) {
144 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
147 fputs(buf, debug_fp);
150 OutputDebugString(buf
);
153 void debug_printf(char *fmt
, ...)
157 static int debugging
= -1;
160 debugging
= getenv("DEBUG_PUZZLES") ? 1 : 0;
164 _vsnprintf(buf
, 4095, fmt
, ap
);
172 #define WINFLAGS (WS_OVERLAPPEDWINDOW &~ \
173 (WS_MAXIMIZEBOX | WS_OVERLAPPED))
175 #define WINFLAGS (WS_CAPTION | WS_SYSMENU)
178 static void new_game_size(frontend
*fe
, float scale
);
196 enum { CFG_PRINT
= CFG_FRONTEND_SPECIFIC
};
198 struct preset_menuitemref
{
206 HWND hwnd
, statusbar
, cfgbox
;
208 HWND numpad
; /* window handle for the numeric pad */
211 HBITMAP bitmap
, prevbm
;
212 RECT bitmapPosition
; /* game bitmap position within game window */
218 HMENU gamemenu
, typemenu
;
220 DWORD timer_last_tickcount
;
221 struct preset_menu
*preset_menu
;
222 struct preset_menuitemref
*preset_menuitems
;
223 int n_preset_menuitems
;
225 int nfonts
, fontsize
;
227 struct cfg_aux
*cfgaux
;
228 int cfg_which
, dlg_done
;
233 enum { DRAWING
, PRINTING
, NOTHING
} drawstatus
;
235 int printcount
, printw
, printh
, printsolns
, printcurr
, printcolour
;
237 int printoffsetx
, printoffsety
;
238 float printpixelscale
;
240 int linewidth
, linedotted
;
246 void frontend_free(frontend
*fe
)
258 static void update_type_menu_tick(frontend
*fe
);
259 static void update_copy_menu_greying(frontend
*fe
);
261 void fatal(char *fmt
, ...)
267 vsprintf(buf
, fmt
, ap
);
270 MessageBox(NULL
, buf
, "Fatal error", MB_ICONEXCLAMATION
| MB_OK
);
275 char *geterrstr(void)
278 DWORD dw
= GetLastError();
282 FORMAT_MESSAGE_ALLOCATE_BUFFER
|
283 FORMAT_MESSAGE_FROM_SYSTEM
,
286 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
290 ret
= dupstr(lpMsgBuf
);
297 void get_random_seed(void **randseed
, int *randseedsize
)
299 SYSTEMTIME
*st
= snew(SYSTEMTIME
);
303 *randseed
= (void *)st
;
304 *randseedsize
= sizeof(SYSTEMTIME
);
307 static void win_status_bar(void *handle
, char *text
)
312 frontend
*fe
= (frontend
*)handle
;
315 MultiByteToWideChar (CP_ACP
, 0, text
, -1, wText
, 255);
316 SendMessage(fe
->statusbar
, SB_SETTEXT
,
317 (WPARAM
) 255 | SBT_NOBORDERS
,
320 SetWindowText(fe
->statusbar
, text
);
324 static blitter
*win_blitter_new(void *handle
, int w
, int h
)
326 blitter
*bl
= snew(blitter
);
328 memset(bl
, 0, sizeof(blitter
));
336 static void win_blitter_free(void *handle
, blitter
*bl
)
338 if (bl
->bitmap
) DeleteObject(bl
->bitmap
);
342 static void blitter_mkbitmap(frontend
*fe
, blitter
*bl
)
344 HDC hdc
= GetDC(fe
->hwnd
);
345 bl
->bitmap
= CreateCompatibleBitmap(hdc
, bl
->w
, bl
->h
);
346 ReleaseDC(fe
->hwnd
, hdc
);
349 /* BitBlt(dstDC, dstX, dstY, dstW, dstH, srcDC, srcX, srcY, dType) */
351 static void win_blitter_save(void *handle
, blitter
*bl
, int x
, int y
)
353 frontend
*fe
= (frontend
*)handle
;
354 HDC hdc_win
, hdc_blit
;
357 assert(fe
->drawstatus
== DRAWING
);
359 if (!bl
->bitmap
) blitter_mkbitmap(fe
, bl
);
361 bl
->x
= x
; bl
->y
= y
;
363 hdc_win
= GetDC(fe
->hwnd
);
364 hdc_blit
= CreateCompatibleDC(hdc_win
);
365 if (!hdc_blit
) fatal("hdc_blit failed: 0x%x", GetLastError());
367 prev_blit
= SelectObject(hdc_blit
, bl
->bitmap
);
368 if (prev_blit
== NULL
|| prev_blit
== HGDI_ERROR
)
369 fatal("SelectObject for hdc_main failed: 0x%x", GetLastError());
371 if (!BitBlt(hdc_blit
, 0, 0, bl
->w
, bl
->h
,
372 fe
->hdc
, x
, y
, SRCCOPY
))
373 fatal("BitBlt failed: 0x%x", GetLastError());
375 SelectObject(hdc_blit
, prev_blit
);
377 ReleaseDC(fe
->hwnd
, hdc_win
);
380 static void win_blitter_load(void *handle
, blitter
*bl
, int x
, int y
)
382 frontend
*fe
= (frontend
*)handle
;
383 HDC hdc_win
, hdc_blit
;
386 assert(fe
->drawstatus
== DRAWING
);
388 assert(bl
->bitmap
); /* we should always have saved before loading */
390 if (x
== BLITTER_FROMSAVED
) x
= bl
->x
;
391 if (y
== BLITTER_FROMSAVED
) y
= bl
->y
;
393 hdc_win
= GetDC(fe
->hwnd
);
394 hdc_blit
= CreateCompatibleDC(hdc_win
);
396 prev_blit
= SelectObject(hdc_blit
, bl
->bitmap
);
398 BitBlt(fe
->hdc
, x
, y
, bl
->w
, bl
->h
,
399 hdc_blit
, 0, 0, SRCCOPY
);
401 SelectObject(hdc_blit
, prev_blit
);
403 ReleaseDC(fe
->hwnd
, hdc_win
);
406 void frontend_default_colour(frontend
*fe
, float *output
)
408 DWORD c
= GetSysColor(COLOR_MENU
); /* ick */
410 output
[0] = (float)(GetRValue(c
) / 255.0);
411 output
[1] = (float)(GetGValue(c
) / 255.0);
412 output
[2] = (float)(GetBValue(c
) / 255.0);
415 static POINT
win_transform_point(frontend
*fe
, int x
, int y
)
419 assert(fe
->drawstatus
!= NOTHING
);
421 if (fe
->drawstatus
== PRINTING
) {
422 ret
.x
= (int)(fe
->printoffsetx
+ fe
->printpixelscale
* x
);
423 ret
.y
= (int)(fe
->printoffsety
+ fe
->printpixelscale
* y
);
432 static void win_text_colour(frontend
*fe
, int colour
)
434 assert(fe
->drawstatus
!= NOTHING
);
436 if (fe
->drawstatus
== PRINTING
) {
439 print_get_colour(fe
->dr
, colour
, fe
->printcolour
, &hatch
, &r
, &g
, &b
);
442 * Displaying text in hatched colours is not permitted.
446 SetTextColor(fe
->hdc
, RGB(r
* 255, g
* 255, b
* 255));
448 SetTextColor(fe
->hdc
, fe
->colours
[colour
]);
452 static void win_set_brush(frontend
*fe
, int colour
)
455 assert(fe
->drawstatus
!= NOTHING
);
457 if (fe
->drawstatus
== PRINTING
) {
460 print_get_colour(fe
->dr
, colour
, fe
->printcolour
, &hatch
, &r
, &g
, &b
);
463 br
= CreateSolidBrush(RGB(r
* 255, g
* 255, b
* 255));
467 * This is only ever required during printing, and the
468 * PocketPC port doesn't support printing.
470 fatal("CreateHatchBrush not supported");
472 br
= CreateHatchBrush(hatch
== HATCH_BACKSLASH
? HS_FDIAGONAL
:
473 hatch
== HATCH_SLASH
? HS_BDIAGONAL
:
474 hatch
== HATCH_HORIZ
? HS_HORIZONTAL
:
475 hatch
== HATCH_VERT
? HS_VERTICAL
:
476 hatch
== HATCH_PLUS
? HS_CROSS
:
477 /* hatch == HATCH_X ? */ HS_DIAGCROSS
,
482 br
= fe
->brushes
[colour
];
484 fe
->oldbr
= SelectObject(fe
->hdc
, br
);
487 static void win_reset_brush(frontend
*fe
)
491 assert(fe
->drawstatus
!= NOTHING
);
493 br
= SelectObject(fe
->hdc
, fe
->oldbr
);
494 if (fe
->drawstatus
== PRINTING
)
498 static void win_set_pen(frontend
*fe
, int colour
, int thin
)
501 assert(fe
->drawstatus
!= NOTHING
);
503 if (fe
->drawstatus
== PRINTING
) {
506 int width
= thin
? 0 : fe
->linewidth
;
511 print_get_colour(fe
->dr
, colour
, fe
->printcolour
, &hatch
, &r
, &g
, &b
);
513 * Stroking in hatched colours is not permitted.
516 pen
= CreatePen(fe
->linedotted
? PS_DOT
: PS_SOLID
,
517 width
, RGB(r
* 255, g
* 255, b
* 255));
519 pen
= fe
->pens
[colour
];
521 fe
->oldpen
= SelectObject(fe
->hdc
, pen
);
524 static void win_reset_pen(frontend
*fe
)
528 assert(fe
->drawstatus
!= NOTHING
);
530 pen
= SelectObject(fe
->hdc
, fe
->oldpen
);
531 if (fe
->drawstatus
== PRINTING
)
535 static void win_clip(void *handle
, int x
, int y
, int w
, int h
)
537 frontend
*fe
= (frontend
*)handle
;
540 if (fe
->drawstatus
== NOTHING
)
543 p
= win_transform_point(fe
, x
, y
);
544 q
= win_transform_point(fe
, x
+w
, y
+h
);
545 IntersectClipRect(fe
->hdc
, p
.x
, p
.y
, q
.x
, q
.y
);
548 static void win_unclip(void *handle
)
550 frontend
*fe
= (frontend
*)handle
;
552 if (fe
->drawstatus
== NOTHING
)
555 SelectClipRgn(fe
->hdc
, NULL
);
558 static void win_draw_text(void *handle
, int x
, int y
, int fonttype
,
559 int fontsize
, int align
, int colour
, char *text
)
561 frontend
*fe
= (frontend
*)handle
;
566 if (fe
->drawstatus
== NOTHING
)
569 if (fe
->drawstatus
== PRINTING
)
570 fontsize
= (int)(fontsize
* fe
->printpixelscale
);
572 xy
= win_transform_point(fe
, x
, y
);
575 * Find or create the font.
577 for (i
= fe
->fontstart
; i
< fe
->nfonts
; i
++)
578 if (fe
->fonts
[i
].type
== fonttype
&& fe
->fonts
[i
].size
== fontsize
)
581 if (i
== fe
->nfonts
) {
582 if (fe
->fontsize
<= fe
->nfonts
) {
583 fe
->fontsize
= fe
->nfonts
+ 10;
584 fe
->fonts
= sresize(fe
->fonts
, fe
->fontsize
, struct font
);
589 fe
->fonts
[i
].type
= fonttype
;
590 fe
->fonts
[i
].size
= fontsize
;
592 memset (&lf
, 0, sizeof(LOGFONT
));
593 lf
.lfHeight
= -fontsize
;
594 lf
.lfWeight
= (fe
->drawstatus
== PRINTING
? 0 : FW_BOLD
);
595 lf
.lfCharSet
= DEFAULT_CHARSET
;
596 lf
.lfOutPrecision
= OUT_DEFAULT_PRECIS
;
597 lf
.lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
598 lf
.lfQuality
= DEFAULT_QUALITY
;
599 lf
.lfPitchAndFamily
= (fonttype
== FONT_FIXED
?
600 FIXED_PITCH
| FF_DONTCARE
:
601 VARIABLE_PITCH
| FF_SWISS
);
603 wcscpy(lf
.lfFaceName
, TEXT("Tahoma"));
606 fe
->fonts
[i
].font
= CreateFontIndirect(&lf
);
610 * Position and draw the text.
617 MultiByteToWideChar (CP_UTF8
, 0, text
, -1, wText
, 256);
619 oldfont
= SelectObject(fe
->hdc
, fe
->fonts
[i
].font
);
620 if (GetTextMetrics(fe
->hdc
, &tm
)) {
621 if (align
& ALIGN_VCENTRE
)
622 xy
.y
-= (tm
.tmAscent
+tm
.tmDescent
)/2;
626 if (GetTextExtentPoint32W(fe
->hdc
, wText
, wcslen(wText
), &size
))
628 if (align
& ALIGN_HCENTRE
)
630 else if (align
& ALIGN_HRIGHT
)
633 SetBkMode(fe
->hdc
, TRANSPARENT
);
634 win_text_colour(fe
, colour
);
635 ExtTextOutW(fe
->hdc
, xy
.x
, xy
.y
, 0, NULL
, wText
, wcslen(wText
), NULL
);
636 SelectObject(fe
->hdc
, oldfont
);
640 static void win_draw_rect(void *handle
, int x
, int y
, int w
, int h
, int colour
)
642 frontend
*fe
= (frontend
*)handle
;
645 if (fe
->drawstatus
== NOTHING
)
648 if (fe
->drawstatus
== DRAWING
&& w
== 1 && h
== 1) {
650 * Rectangle() appears to get uppity if asked to draw a 1x1
651 * rectangle, presumably on the grounds that that's beneath
652 * its dignity and you ought to be using SetPixel instead.
655 SetPixel(fe
->hdc
, x
, y
, fe
->colours
[colour
]);
657 win_set_brush(fe
, colour
);
658 win_set_pen(fe
, colour
, TRUE
);
659 p
= win_transform_point(fe
, x
, y
);
660 q
= win_transform_point(fe
, x
+w
, y
+h
);
661 Rectangle(fe
->hdc
, p
.x
, p
.y
, q
.x
, q
.y
);
667 static void win_draw_line(void *handle
, int x1
, int y1
, int x2
, int y2
, int colour
)
669 frontend
*fe
= (frontend
*)handle
;
672 if (fe
->drawstatus
== NOTHING
)
675 win_set_pen(fe
, colour
, FALSE
);
676 pp
[0] = win_transform_point(fe
, x1
, y1
);
677 pp
[1] = win_transform_point(fe
, x2
, y2
);
678 Polyline(fe
->hdc
, pp
, 2);
679 if (fe
->drawstatus
== DRAWING
)
680 SetPixel(fe
->hdc
, pp
[1].x
, pp
[1].y
, fe
->colours
[colour
]);
684 static void win_draw_circle(void *handle
, int cx
, int cy
, int radius
,
685 int fillcolour
, int outlinecolour
)
687 frontend
*fe
= (frontend
*)handle
;
690 assert(outlinecolour
>= 0);
692 if (fe
->drawstatus
== NOTHING
)
696 win_set_brush(fe
, fillcolour
);
698 fe
->oldbr
= SelectObject(fe
->hdc
, GetStockObject(NULL_BRUSH
));
700 win_set_pen(fe
, outlinecolour
, FALSE
);
701 p
= win_transform_point(fe
, cx
- radius
, cy
- radius
);
702 q
= win_transform_point(fe
, cx
+ radius
, cy
+ radius
);
703 Ellipse(fe
->hdc
, p
.x
, p
.y
, q
.x
+1, q
.y
+1);
708 static void win_draw_polygon(void *handle
, int *coords
, int npoints
,
709 int fillcolour
, int outlinecolour
)
711 frontend
*fe
= (frontend
*)handle
;
715 if (fe
->drawstatus
== NOTHING
)
718 pts
= snewn(npoints
+1, POINT
);
720 for (i
= 0; i
<= npoints
; i
++) {
721 int j
= (i
< npoints
? i
: 0);
722 pts
[i
] = win_transform_point(fe
, coords
[j
*2], coords
[j
*2+1]);
725 assert(outlinecolour
>= 0);
727 if (fillcolour
>= 0) {
728 win_set_brush(fe
, fillcolour
);
729 win_set_pen(fe
, outlinecolour
, FALSE
);
730 Polygon(fe
->hdc
, pts
, npoints
);
734 win_set_pen(fe
, outlinecolour
, FALSE
);
735 Polyline(fe
->hdc
, pts
, npoints
+1);
742 static void win_start_draw(void *handle
)
744 frontend
*fe
= (frontend
*)handle
;
747 assert(fe
->drawstatus
== NOTHING
);
749 hdc_win
= GetDC(fe
->hwnd
);
750 fe
->hdc
= CreateCompatibleDC(hdc_win
);
751 fe
->prevbm
= SelectObject(fe
->hdc
, fe
->bitmap
);
752 ReleaseDC(fe
->hwnd
, hdc_win
);
755 SetMapMode(fe
->hdc
, MM_TEXT
);
757 fe
->drawstatus
= DRAWING
;
760 static void win_draw_update(void *handle
, int x
, int y
, int w
, int h
)
762 frontend
*fe
= (frontend
*)handle
;
765 if (fe
->drawstatus
!= DRAWING
)
773 OffsetRect(&r
, fe
->bitmapPosition
.left
, fe
->bitmapPosition
.top
);
774 InvalidateRect(fe
->hwnd
, &r
, FALSE
);
777 static void win_end_draw(void *handle
)
779 frontend
*fe
= (frontend
*)handle
;
780 assert(fe
->drawstatus
== DRAWING
);
781 SelectObject(fe
->hdc
, fe
->prevbm
);
784 DeleteObject(fe
->clip
);
787 fe
->drawstatus
= NOTHING
;
790 static void win_line_width(void *handle
, float width
)
792 frontend
*fe
= (frontend
*)handle
;
794 assert(fe
->drawstatus
!= DRAWING
);
795 if (fe
->drawstatus
== NOTHING
)
798 fe
->linewidth
= (int)(width
* fe
->printpixelscale
);
801 static void win_line_dotted(void *handle
, int dotted
)
803 frontend
*fe
= (frontend
*)handle
;
805 assert(fe
->drawstatus
!= DRAWING
);
806 if (fe
->drawstatus
== NOTHING
)
809 fe
->linedotted
= dotted
;
812 static void win_begin_doc(void *handle
, int pages
)
814 frontend
*fe
= (frontend
*)handle
;
816 assert(fe
->drawstatus
!= DRAWING
);
817 if (fe
->drawstatus
== NOTHING
)
820 if (StartDoc(fe
->hdc
, &fe
->di
) <= 0) {
821 char *e
= geterrstr();
822 MessageBox(fe
->hwnd
, e
, "Error starting to print",
823 MB_ICONERROR
| MB_OK
);
825 fe
->drawstatus
= NOTHING
;
829 * Push a marker on the font stack so that we won't use the
830 * same fonts for printing and drawing. (This is because
831 * drawing seems to look generally better in bold, but printing
832 * is better not in bold.)
834 fe
->fontstart
= fe
->nfonts
;
837 static void win_begin_page(void *handle
, int number
)
839 frontend
*fe
= (frontend
*)handle
;
841 assert(fe
->drawstatus
!= DRAWING
);
842 if (fe
->drawstatus
== NOTHING
)
845 if (StartPage(fe
->hdc
) <= 0) {
846 char *e
= geterrstr();
847 MessageBox(fe
->hwnd
, e
, "Error starting a page",
848 MB_ICONERROR
| MB_OK
);
850 fe
->drawstatus
= NOTHING
;
854 static void win_begin_puzzle(void *handle
, float xm
, float xc
,
855 float ym
, float yc
, int pw
, int ph
, float wmm
)
857 frontend
*fe
= (frontend
*)handle
;
858 int ppw
, pph
, pox
, poy
;
859 float mmpw
, mmph
, mmox
, mmoy
;
862 assert(fe
->drawstatus
!= DRAWING
);
863 if (fe
->drawstatus
== NOTHING
)
866 ppw
= GetDeviceCaps(fe
->hdc
, HORZRES
);
867 pph
= GetDeviceCaps(fe
->hdc
, VERTRES
);
868 mmpw
= (float)GetDeviceCaps(fe
->hdc
, HORZSIZE
);
869 mmph
= (float)GetDeviceCaps(fe
->hdc
, VERTSIZE
);
872 * Compute the puzzle's position on the logical page.
874 mmox
= xm
* mmpw
+ xc
;
875 mmoy
= ym
* mmph
+ yc
;
878 * Work out what that comes to in pixels.
880 pox
= (int)(mmox
* (float)ppw
/ mmpw
);
881 poy
= (int)(mmoy
* (float)pph
/ mmph
);
884 * And determine the scale.
886 * I need a scale such that the maximum puzzle-coordinate
887 * extent of the rectangle (pw * scale) is equal to the pixel
888 * equivalent of the puzzle's millimetre width (wmm * ppw /
891 scale
= (wmm
* ppw
) / (mmpw
* pw
);
894 * Now store pox, poy and scale for use in the main drawing
897 fe
->printoffsetx
= pox
;
898 fe
->printoffsety
= poy
;
899 fe
->printpixelscale
= scale
;
902 fe
->linedotted
= FALSE
;
905 static void win_end_puzzle(void *handle
)
907 /* Nothing needs to be done here. */
910 static void win_end_page(void *handle
, int number
)
912 frontend
*fe
= (frontend
*)handle
;
914 assert(fe
->drawstatus
!= DRAWING
);
916 if (fe
->drawstatus
== NOTHING
)
919 if (EndPage(fe
->hdc
) <= 0) {
920 char *e
= geterrstr();
921 MessageBox(fe
->hwnd
, e
, "Error finishing a page",
922 MB_ICONERROR
| MB_OK
);
924 fe
->drawstatus
= NOTHING
;
928 static void win_end_doc(void *handle
)
930 frontend
*fe
= (frontend
*)handle
;
932 assert(fe
->drawstatus
!= DRAWING
);
935 * Free all the fonts created since we began printing.
937 while (fe
->nfonts
> fe
->fontstart
) {
939 DeleteObject(fe
->fonts
[fe
->nfonts
].font
);
944 * The MSDN web site sample code doesn't bother to call EndDoc
945 * if an error occurs half way through printing. I expect doing
946 * so would cause the erroneous document to actually be
947 * printed, or something equally undesirable.
949 if (fe
->drawstatus
== NOTHING
)
952 if (EndDoc(fe
->hdc
) <= 0) {
953 char *e
= geterrstr();
954 MessageBox(fe
->hwnd
, e
, "Error finishing printing",
955 MB_ICONERROR
| MB_OK
);
957 fe
->drawstatus
= NOTHING
;
961 char *win_text_fallback(void *handle
, const char *const *strings
, int nstrings
)
964 * We assume Windows can cope with any UTF-8 likely to be
965 * emitted by a puzzle.
967 return dupstr(strings
[0]);
970 const struct drawing_api win_drawing
= {
997 void print(frontend
*fe
)
1003 midend
*nme
= NULL
; /* non-interactive midend for bulk puzzle generation */
1008 * Create our document structure and fill it up with puzzles.
1010 doc
= document_new(fe
->printw
, fe
->printh
, fe
->printscale
/ 100.0F
);
1011 for (i
= 0; i
< fe
->printcount
; i
++) {
1012 if (i
== 0 && fe
->printcurr
) {
1013 err
= midend_print_puzzle(fe
->me
, doc
, fe
->printsolns
);
1016 game_params
*params
;
1018 nme
= midend_new(NULL
, fe
->game
, NULL
, NULL
);
1021 * Set the non-interactive mid-end to have the same
1022 * parameters as the standard one.
1024 params
= midend_get_params(fe
->me
);
1025 midend_set_params(nme
, params
);
1026 fe
->game
->free_params(params
);
1029 midend_new_game(nme
);
1030 err
= midend_print_puzzle(nme
, doc
, fe
->printsolns
);
1039 MessageBox(fe
->hwnd
, err
, "Error preparing puzzles for printing",
1040 MB_ICONERROR
| MB_OK
);
1045 memset(&pd
, 0, sizeof(pd
));
1046 pd
.lStructSize
= sizeof(pd
);
1047 pd
.hwndOwner
= fe
->hwnd
;
1049 pd
.hDevNames
= NULL
;
1050 pd
.Flags
= PD_USEDEVMODECOPIESANDCOLLATE
| PD_RETURNDC
|
1051 PD_NOPAGENUMS
| PD_NOSELECTION
;
1053 pd
.nFromPage
= pd
.nToPage
= 0xFFFF;
1054 pd
.nMinPage
= pd
.nMaxPage
= 1;
1056 if (!PrintDlg(&pd
)) {
1062 * Now pd.hDC is a device context for the printer.
1066 * FIXME: IWBNI we put up an Abort box here.
1069 memset(&fe
->di
, 0, sizeof(fe
->di
));
1070 fe
->di
.cbSize
= sizeof(fe
->di
);
1071 sprintf(doctitle
, "Printed puzzles from %s (from Simon Tatham's"
1072 " Portable Puzzle Collection)", fe
->game
->name
);
1073 fe
->di
.lpszDocName
= doctitle
;
1074 fe
->di
.lpszOutput
= NULL
;
1075 fe
->di
.lpszDatatype
= NULL
;
1078 fe
->drawstatus
= PRINTING
;
1081 fe
->dr
= drawing_new(&win_drawing
, NULL
, fe
);
1082 document_print(doc
, fe
->dr
);
1083 drawing_free(fe
->dr
);
1086 fe
->drawstatus
= NOTHING
;
1093 void deactivate_timer(frontend
*fe
)
1096 return; /* for non-interactive midend */
1097 if (fe
->hwnd
) KillTimer(fe
->hwnd
, fe
->timer
);
1101 void activate_timer(frontend
*fe
)
1104 return; /* for non-interactive midend */
1106 fe
->timer
= SetTimer(fe
->hwnd
, 1, 20, NULL
);
1107 fe
->timer_last_tickcount
= GetTickCount();
1111 void write_clip(HWND hwnd
, char *data
)
1119 * Windows expects CRLF in the clipboard, so we must convert
1120 * any \n that has come out of the puzzle backend.
1123 for (i
= 0; data
[i
]; i
++) {
1124 if (data
[i
] == '\n')
1128 data2
= snewn(len
+1, char);
1130 for (i
= 0; data
[i
]; i
++) {
1131 if (data
[i
] == '\n')
1133 data2
[j
++] = data
[i
];
1138 clipdata
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, len
+ 1);
1143 lock
= GlobalLock(clipdata
);
1145 GlobalFree(clipdata
);
1149 memcpy(lock
, data2
, len
);
1150 ((unsigned char *) lock
)[len
] = 0;
1151 GlobalUnlock(clipdata
);
1153 if (OpenClipboard(hwnd
)) {
1155 SetClipboardData(CF_TEXT
, clipdata
);
1158 GlobalFree(clipdata
);
1164 * Set up Help and see if we can find a help file.
1166 static void init_help(void)
1169 char b
[2048], *p
, *q
, *r
;
1173 * Find the executable file path, so we can look alongside
1174 * it for help files. Trim the filename off the end.
1176 GetModuleFileName(NULL
, b
, sizeof(b
) - 1);
1178 p
= strrchr(b
, '\\');
1179 if (p
&& p
>= r
) r
= p
+1;
1180 q
= strrchr(b
, ':');
1181 if (q
&& q
>= r
) r
= q
+1;
1185 * Try HTML Help first.
1187 strcpy(r
, CHM_FILE_NAME
);
1188 if ( (fp
= fopen(b
, "r")) != NULL
) {
1192 * We have a .CHM. See if we can use it.
1194 hh_dll
= LoadLibrary("hhctrl.ocx");
1196 htmlhelp
= (htmlhelp_t
)GetProcAddress(hh_dll
, "HtmlHelpA");
1198 FreeLibrary(hh_dll
);
1201 help_path
= dupstr(b
);
1206 #endif /* NO_HTMLHELP */
1209 * Now try old-style .HLP.
1211 strcpy(r
, HELP_FILE_NAME
);
1212 if ( (fp
= fopen(b
, "r")) != NULL
) {
1215 help_path
= dupstr(b
);
1219 * See if there's a .CNT file alongside it.
1221 strcpy(r
, HELP_CNT_NAME
);
1222 if ( (fp
= fopen(b
, "r")) != NULL
) {
1224 help_has_contents
= TRUE
;
1226 help_has_contents
= FALSE
;
1231 help_type
= NONE
; /* didn't find any */
1240 static void start_help(frontend
*fe
, const char *topic
)
1245 switch (help_type
) {
1249 str
= snewn(10+strlen(topic
), char);
1250 sprintf(str
, "JI(`',`%s')", topic
);
1252 } else if (help_has_contents
) {
1255 cmd
= HELP_CONTENTS
;
1257 WinHelp(fe
->hwnd
, help_path
, cmd
, (DWORD
)str
);
1258 fe
->help_running
= TRUE
;
1265 str
= snewn(20 + strlen(topic
) + strlen(help_path
), char);
1266 sprintf(str
, "%s::/%s.html>main", help_path
, topic
);
1268 str
= dupstr(help_path
);
1270 htmlhelp(fe
->hwnd
, str
, HH_DISPLAY_TOPIC
, 0);
1271 fe
->help_running
= TRUE
;
1273 #endif /* NO_HTMLHELP */
1275 assert(!"This shouldn't happen");
1283 * Stop Help on window cleanup.
1285 static void stop_help(frontend
*fe
)
1287 if (fe
->help_running
) {
1288 switch (help_type
) {
1290 WinHelp(fe
->hwnd
, help_path
, HELP_QUIT
, 0);
1295 htmlhelp(NULL
, NULL
, HH_CLOSE_ALL
, 0);
1297 #endif /* NO_HTMLHELP */
1299 assert(!"This shouldn't happen");
1302 fe
->help_running
= FALSE
;
1309 * Terminate Help on process exit.
1311 static void cleanup_help(void)
1313 /* Nothing to do currently.
1314 * (If we were running HTML Help single-threaded, this is where we'd
1315 * call HH_UNINITIALIZE.) */
1318 static int get_statusbar_height(frontend
*fe
)
1321 if (fe
->statusbar
) {
1323 GetWindowRect(fe
->statusbar
, &sr
);
1324 sy
= sr
.bottom
- sr
.top
;
1331 static void adjust_statusbar(frontend
*fe
, RECT
*r
)
1335 if (!fe
->statusbar
) return;
1337 sy
= get_statusbar_height(fe
);
1339 SetWindowPos(fe
->statusbar
, NULL
, 0, r
->bottom
-r
->top
-sy
, r
->right
-r
->left
,
1344 static void get_menu_size(HWND wh
, RECT
*r
)
1346 HMENU bar
= GetMenu(wh
);
1350 SetRect(r
, 0, 0, 0, 0);
1351 for (i
= 0; i
< GetMenuItemCount(bar
); i
++) {
1352 GetMenuItemRect(wh
, bar
, i
, &rect
);
1353 UnionRect(r
, r
, &rect
);
1358 * Given a proposed new puzzle size (cx,cy), work out the actual
1359 * puzzle size that would be (px,py) and the window size including
1360 * furniture (wx,wy).
1363 static int check_window_resize(frontend
*fe
, int cx
, int cy
,
1368 int x
, y
, sy
= get_statusbar_height(fe
), changed
= 0;
1370 /* disallow making window thinner than menu bar */
1371 x
= max(cx
, fe
->xmin
);
1372 y
= max(cy
- sy
, fe
->ymin
);
1375 * See if we actually got the window size we wanted, and adjust
1376 * the puzzle size if not.
1378 midend_size(fe
->me
, &x
, &y
, TRUE
);
1379 if (x
!= cx
|| y
!= cy
) {
1381 * Resize the window, now we know what size we _really_
1387 AdjustWindowRectEx(&r
, WINFLAGS
, TRUE
, 0);
1388 *wx
= r
.right
- r
.left
;
1389 *wy
= r
.bottom
- r
.top
;
1397 (float)midend_tilesize(fe
->me
) / (float)fe
->game
->preferred_tilesize
;
1403 * Given the current window size, make sure it's sane for the
1404 * current puzzle and resize if necessary.
1407 static void check_window_size(frontend
*fe
, int *px
, int *py
)
1412 GetClientRect(fe
->hwnd
, &r
);
1413 cx
= r
.right
- r
.left
;
1414 cy
= r
.bottom
- r
.top
;
1416 if (check_window_resize(fe
, cx
, cy
, px
, py
, &wx
, &wy
)) {
1418 SetWindowPos(fe
->hwnd
, NULL
, 0, 0, wx
, wy
,
1419 SWP_NOMOVE
| SWP_NOZORDER
);
1424 GetClientRect(fe
->hwnd
, &r
);
1425 adjust_statusbar(fe
, &r
);
1428 static void get_max_puzzle_size(frontend
*fe
, int *x
, int *y
)
1432 if (SystemParametersInfo(SPI_GETWORKAREA
, 0, &sr
, FALSE
)) {
1433 *x
= sr
.right
- sr
.left
;
1434 *y
= sr
.bottom
- sr
.top
;
1439 AdjustWindowRectEx(&r
, WINFLAGS
, TRUE
, 0);
1440 *x
-= r
.right
- r
.left
- 100;
1441 *y
-= r
.bottom
- r
.top
- 100;
1446 if (fe
->statusbar
!= NULL
) {
1447 GetWindowRect(fe
->statusbar
, &sr
);
1448 *y
-= sr
.bottom
- sr
.top
;
1453 /* Toolbar buttons on the numeric pad */
1454 static TBBUTTON tbNumpadButtons
[] =
1456 {0, IDM_KEYEMUL
+ '1', TBSTATE_ENABLED
, TBSTYLE_BUTTON
, 0, -1},
1457 {1, IDM_KEYEMUL
+ '2', TBSTATE_ENABLED
, TBSTYLE_BUTTON
, 0, -1},
1458 {2, IDM_KEYEMUL
+ '3', TBSTATE_ENABLED
, TBSTYLE_BUTTON
, 0, -1},
1459 {3, IDM_KEYEMUL
+ '4', TBSTATE_ENABLED
, TBSTYLE_BUTTON
, 0, -1},
1460 {4, IDM_KEYEMUL
+ '5', TBSTATE_ENABLED
, TBSTYLE_BUTTON
, 0, -1},
1461 {5, IDM_KEYEMUL
+ '6', TBSTATE_ENABLED
, TBSTYLE_BUTTON
, 0, -1},
1462 {6, IDM_KEYEMUL
+ '7', TBSTATE_ENABLED
, TBSTYLE_BUTTON
, 0, -1},
1463 {7, IDM_KEYEMUL
+ '8', TBSTATE_ENABLED
, TBSTYLE_BUTTON
, 0, -1},
1464 {8, IDM_KEYEMUL
+ '9', TBSTATE_ENABLED
, TBSTYLE_BUTTON
, 0, -1},
1465 {9, IDM_KEYEMUL
+ ' ', TBSTATE_ENABLED
, TBSTYLE_BUTTON
, 0, -1}
1470 * Allocate a new frontend structure and create its main window.
1472 static frontend
*frontend_new(HINSTANCE inst
)
1475 const char *nogame
= "Puzzles (no game selected)";
1477 fe
= snew(frontend
);
1487 fe
->help_running
= FALSE
;
1489 fe
->drawstatus
= NOTHING
;
1494 fe
->nfonts
= fe
->fontsize
= 0;
1500 fe
->puzz_scale
= 1.0;
1503 MultiByteToWideChar (CP_ACP
, 0, nogame
, -1, wGameName
, 256);
1504 fe
->hwnd
= CreateWindowEx(0, wClassName
, wGameName
,
1506 CW_USEDEFAULT
, CW_USEDEFAULT
,
1507 CW_USEDEFAULT
, CW_USEDEFAULT
,
1508 NULL
, NULL
, inst
, NULL
);
1512 RECT rc
, rcBar
, rcTB
, rcClient
;
1514 memset (&mbi
, 0, sizeof(SHMENUBARINFO
));
1515 mbi
.cbSize
= sizeof(SHMENUBARINFO
);
1516 mbi
.hwndParent
= fe
->hwnd
;
1517 mbi
.nToolBarId
= IDR_MENUBAR1
;
1518 mbi
.hInstRes
= inst
;
1520 SHCreateMenuBar(&mbi
);
1522 GetWindowRect(fe
->hwnd
, &rc
);
1523 GetWindowRect(mbi
.hwndMB
, &rcBar
);
1524 rc
.bottom
-= rcBar
.bottom
- rcBar
.top
;
1525 MoveWindow(fe
->hwnd
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
, FALSE
);
1530 fe
->hwnd
= CreateWindowEx(0, CLASSNAME
, nogame
,
1531 WS_OVERLAPPEDWINDOW
&~
1533 CW_USEDEFAULT
, CW_USEDEFAULT
,
1534 CW_USEDEFAULT
, CW_USEDEFAULT
,
1535 NULL
, NULL
, inst
, NULL
);
1537 DWORD lerr
= GetLastError();
1538 printf("no window: 0x%x\n", (unsigned)lerr
);
1542 fe
->gamemenu
= NULL
;
1543 fe
->preset_menu
= NULL
;
1545 fe
->statusbar
= NULL
;
1548 SetWindowLong(fe
->hwnd
, GWL_USERDATA
, (LONG
)fe
);
1553 static void savefile_write(void *wctx
, void *buf
, int len
)
1555 FILE *fp
= (FILE *)wctx
;
1556 fwrite(buf
, 1, len
, fp
);
1559 static int savefile_read(void *wctx
, void *buf
, int len
)
1561 FILE *fp
= (FILE *)wctx
;
1564 ret
= fread(buf
, 1, len
, fp
);
1565 return (ret
== len
);
1569 * Create an appropriate midend structure to go in a puzzle window,
1570 * given a game type and/or a command-line argument.
1572 * 'arg' can be either a game ID string (descriptive, random, or a
1573 * plain set of parameters) or the filename of a save file. The two
1574 * boolean flag arguments indicate which possibilities are
1577 static midend
*midend_for_new_game(frontend
*fe
, const game
*cgame
,
1578 char *arg
, int maybe_game_id
,
1579 int maybe_save_file
, char **error
)
1584 if (me
) midend_free(me
);
1585 me
= midend_new(fe
, cgame
, &win_drawing
, fe
);
1586 midend_new_game(me
);
1589 char *err_param
, *err_load
;
1592 * See if arg is a valid filename of a save game file.
1595 if (maybe_save_file
&& (fp
= fopen(arg
, "r")) != NULL
) {
1596 const game
*loadgame
;
1600 * Find out what kind of game is stored in the save
1601 * file; if we're going to end up loading that, it
1602 * will have to override our caller's judgment as to
1603 * what game to initialise our midend with.
1606 err_load
= identify_game(&id_name
, savefile_read
, fp
);
1609 for (i
= 0; i
< gamecount
; i
++)
1610 if (!strcmp(id_name
, gamelist
[i
]->name
))
1612 if (i
== gamecount
) {
1613 err_load
= "Save file is for a game not supported by"
1616 loadgame
= gamelist
[i
];
1617 rewind(fp
); /* go back to the start for actual load */
1624 if (me
) midend_free(me
);
1625 me
= midend_new(fe
, loadgame
, &win_drawing
, fe
);
1626 err_load
= midend_deserialise(me
, savefile_read
, fp
);
1629 err_load
= "Unable to open file";
1632 if (maybe_game_id
&& (!maybe_save_file
|| err_load
)) {
1634 * See if arg is a game description.
1636 if (me
) midend_free(me
);
1637 me
= midend_new(fe
, cgame
, &win_drawing
, fe
);
1638 err_param
= midend_game_id(me
, arg
);
1640 midend_new_game(me
);
1642 if (maybe_save_file
) {
1643 *error
= snewn(256 + strlen(arg
) + strlen(err_param
) +
1644 strlen(err_load
), char);
1645 sprintf(*error
, "Supplied argument \"%s\" is neither a"
1646 " game ID (%s) nor a save file (%s)",
1647 arg
, err_param
, err_load
);
1649 *error
= dupstr(err_param
);
1655 } else if (err_load
) {
1656 *error
= dupstr(err_load
);
1666 static void populate_preset_menu(frontend
*fe
,
1667 struct preset_menu
*menu
, HMENU winmenu
)
1670 for (i
= 0; i
< menu
->n_entries
; i
++) {
1671 struct preset_menu_entry
*entry
= &menu
->entries
[i
];
1673 UINT flags
= MF_ENABLED
;
1675 if (entry
->params
) {
1676 id_or_sub
= (UINT_PTR
)(IDM_PRESETS
+ 0x10 * entry
->id
);
1678 fe
->preset_menuitems
[entry
->id
].which_menu
= winmenu
;
1679 fe
->preset_menuitems
[entry
->id
].item_index
=
1680 GetMenuItemCount(winmenu
);
1682 HMENU winsubmenu
= CreateMenu();
1683 id_or_sub
= (UINT_PTR
)winsubmenu
;
1686 populate_preset_menu(fe
, entry
->submenu
, winsubmenu
);
1690 * FIXME: we ought to go through and do something with ampersands
1695 AppendMenu(winmenu
, flags
, id_or_sub
, entry
->title
);
1699 MultiByteToWideChar(CP_ACP
, 0, entry
->title
, -1, wName
, 255);
1700 AppendMenu(winmenu
, flags
, id_or_sub
, wName
);
1707 * Populate a frontend structure with a new midend structure, and
1708 * create any window furniture that it needs.
1710 * Previously-allocated memory and window furniture will be freed by
1714 static int fe_set_midend(frontend
*fe
, midend
*me
)
1719 if (fe
->me
) midend_free(fe
->me
);
1721 fe
->game
= midend_which_game(fe
->me
);
1727 colours
= midend_colours(fe
->me
, &ncolours
);
1729 if (fe
->colours
) sfree(fe
->colours
);
1730 if (fe
->brushes
) sfree(fe
->brushes
);
1731 if (fe
->pens
) sfree(fe
->pens
);
1733 fe
->colours
= snewn(ncolours
, COLORREF
);
1734 fe
->brushes
= snewn(ncolours
, HBRUSH
);
1735 fe
->pens
= snewn(ncolours
, HPEN
);
1737 for (i
= 0; i
< ncolours
; i
++) {
1738 fe
->colours
[i
] = RGB(255 * colours
[i
*3+0],
1739 255 * colours
[i
*3+1],
1740 255 * colours
[i
*3+2]);
1741 fe
->brushes
[i
] = CreateSolidBrush(fe
->colours
[i
]);
1742 fe
->pens
[i
] = CreatePen(PS_SOLID
, 1, fe
->colours
[i
]);
1748 DestroyWindow(fe
->statusbar
);
1749 if (midend_wants_statusbar(fe
->me
)) {
1750 fe
->statusbar
= CreateWindowEx(0, STATUSCLASSNAME
,
1751 TEXT(DEFAULT_STATUSBAR_TEXT
),
1752 WS_CHILD
| WS_VISIBLE
,
1753 0, 0, 0, 0, /* status bar does these */
1754 NULL
, NULL
, fe
->inst
, NULL
);
1756 fe
->statusbar
= NULL
;
1758 get_max_puzzle_size(fe
, &x
, &y
);
1759 midend_size(fe
->me
, &x
, &y
, FALSE
);
1764 AdjustWindowRectEx(&r
, WINFLAGS
, TRUE
, 0);
1768 DestroyWindow(fe
->numpad
);
1769 if (fe
->game
->flags
& REQUIRE_NUMPAD
)
1771 fe
->numpad
= CreateToolbarEx (fe
->hwnd
,
1772 WS_VISIBLE
| WS_CHILD
| CCS_NOPARENTALIGN
| TBSTYLE_FLAT
,
1773 0, 10, fe
->inst
, IDR_PADTOOLBAR
,
1774 tbNumpadButtons
, sizeof (tbNumpadButtons
) / sizeof (TBBUTTON
),
1775 0, 0, 14, 15, sizeof (TBBUTTON
));
1776 GetWindowRect(fe
->numpad
, &rcTB
);
1777 GetClientRect(fe
->hwnd
, &rcClient
);
1778 MoveWindow(fe
->numpad
,
1780 rcClient
.bottom
- (rcTB
.bottom
- rcTB
.top
) - 1,
1782 rcTB
.bottom
- rcTB
.top
,
1784 SendMessage(fe
->numpad
, TB_SETINDENT
, (rcClient
.right
- (10 * 21)) / 2, 0);
1789 MultiByteToWideChar (CP_ACP
, 0, fe
->game
->name
, -1, wGameName
, 256);
1790 SetWindowText(fe
->hwnd
, wGameName
);
1792 SetWindowText(fe
->hwnd
, fe
->game
->name
);
1796 DestroyWindow(fe
->statusbar
);
1797 if (midend_wants_statusbar(fe
->me
)) {
1799 fe
->statusbar
= CreateWindowEx(0, STATUSCLASSNAME
, TEXT("ooh"),
1800 WS_CHILD
| WS_VISIBLE
,
1801 0, 0, 0, 0, /* status bar does these */
1802 fe
->hwnd
, NULL
, fe
->inst
, NULL
);
1804 /* Flat status bar looks better on the Pocket PC */
1805 SendMessage(fe
->statusbar
, SB_SIMPLE
, (WPARAM
) TRUE
, 0);
1806 SendMessage(fe
->statusbar
, SB_SETTEXT
,
1807 (WPARAM
) 255 | SBT_NOBORDERS
,
1812 * Now resize the window to take account of the status bar.
1814 GetWindowRect(fe
->statusbar
, &sr
);
1815 GetWindowRect(fe
->hwnd
, &r
);
1817 SetWindowPos(fe
->hwnd
, NULL
, 0, 0, r
.right
- r
.left
,
1818 r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
1819 SWP_NOMOVE
| SWP_NOZORDER
);
1822 fe
->statusbar
= NULL
;
1826 HMENU oldmenu
= GetMenu(fe
->hwnd
);
1829 HMENU bar
= CreateMenu();
1830 HMENU menu
= CreateMenu();
1833 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)menu
, "&Game");
1835 HMENU menu
= SHGetSubMenu(SHFindMenuBar(fe
->hwnd
), ID_GAME
);
1836 DeleteMenu(menu
, 0, MF_BYPOSITION
);
1838 fe
->gamemenu
= menu
;
1839 AppendMenu(menu
, MF_ENABLED
, IDM_NEW
, TEXT("&New"));
1840 AppendMenu(menu
, MF_ENABLED
, IDM_RESTART
, TEXT("&Restart"));
1842 /* ...here I run out of sensible accelerator characters. */
1843 AppendMenu(menu
, MF_ENABLED
, IDM_DESC
, TEXT("Speci&fic..."));
1844 AppendMenu(menu
, MF_ENABLED
, IDM_SEED
, TEXT("Rando&m Seed..."));
1847 if (!fe
->preset_menu
) {
1849 fe
->preset_menu
= midend_get_presets(
1850 fe
->me
, &fe
->n_preset_menuitems
);
1851 fe
->preset_menuitems
= snewn(fe
->n_preset_menuitems
,
1852 struct preset_menuitemref
);
1853 for (i
= 0; i
< fe
->n_preset_menuitems
; i
++)
1854 fe
->preset_menuitems
[i
].which_menu
= NULL
;
1856 if (fe
->preset_menu
->n_entries
> 0 || fe
->game
->can_configure
) {
1858 HMENU sub
= CreateMenu();
1860 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)sub
, "&Type");
1862 HMENU sub
= SHGetSubMenu(SHFindMenuBar(fe
->hwnd
), ID_TYPE
);
1863 DeleteMenu(sub
, 0, MF_BYPOSITION
);
1866 populate_preset_menu(fe
, fe
->preset_menu
, sub
);
1868 if (fe
->game
->can_configure
) {
1869 AppendMenu(sub
, MF_ENABLED
, IDM_CONFIG
, TEXT("&Custom..."));
1874 fe
->typemenu
= INVALID_HANDLE_VALUE
;
1879 #error Windows CE does not support COMBINED build.
1882 HMENU games
= CreateMenu();
1885 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
1886 AppendMenu(menu
, MF_ENABLED
|MF_POPUP
, (UINT
)games
, "&Other");
1887 for (i
= 0; i
< gamecount
; i
++) {
1888 if (strcmp(gamelist
[i
]->name
, fe
->game
->name
) != 0) {
1889 /* only include those games that aren't the same as the
1890 * game we're currently playing. */
1891 AppendMenu(games
, MF_ENABLED
, IDM_GAMES
+ i
, gamelist
[i
]->name
);
1897 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
1899 AppendMenu(menu
, MF_ENABLED
, IDM_LOAD
, TEXT("&Load..."));
1900 AppendMenu(menu
, MF_ENABLED
, IDM_SAVE
, TEXT("&Save..."));
1901 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
1902 if (fe
->game
->can_print
) {
1903 AppendMenu(menu
, MF_ENABLED
, IDM_PRINT
, TEXT("&Print..."));
1904 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
1907 AppendMenu(menu
, MF_ENABLED
, IDM_UNDO
, TEXT("Undo"));
1908 AppendMenu(menu
, MF_ENABLED
, IDM_REDO
, TEXT("Redo"));
1910 if (fe
->game
->can_format_as_text_ever
) {
1911 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
1912 AppendMenu(menu
, MF_ENABLED
, IDM_COPY
, TEXT("&Copy"));
1915 if (fe
->game
->can_solve
) {
1916 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
1917 AppendMenu(menu
, MF_ENABLED
, IDM_SOLVE
, TEXT("Sol&ve"));
1919 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
1921 AppendMenu(menu
, MF_ENABLED
, IDM_QUIT
, TEXT("E&xit"));
1922 menu
= CreateMenu();
1923 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)menu
, TEXT("&Help"));
1925 AppendMenu(menu
, MF_ENABLED
, IDM_ABOUT
, TEXT("&About"));
1927 if (help_type
!= NONE
) {
1929 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
1930 AppendMenu(menu
, MF_ENABLED
, IDM_HELPC
, TEXT("&Contents"));
1931 assert(fe
->game
->name
);
1932 item
= snewn(10+strlen(fe
->game
->name
), char); /*ick*/
1933 sprintf(item
, "&Help on %s", fe
->game
->name
);
1934 AppendMenu(menu
, MF_ENABLED
, IDM_GAMEHELP
, item
);
1937 DestroyMenu(oldmenu
);
1938 SetMenu(fe
->hwnd
, bar
);
1939 get_menu_size(fe
->hwnd
, &menusize
);
1940 fe
->xmin
= (menusize
.right
- menusize
.left
) + 25;
1944 if (fe
->bitmap
) DeleteObject(fe
->bitmap
);
1946 new_game_size(fe
, fe
->puzz_scale
); /* initialises fe->bitmap */
1951 static void show_window(frontend
*fe
)
1953 ShowWindow(fe
->hwnd
, SW_SHOWNORMAL
);
1954 SetForegroundWindow(fe
->hwnd
);
1956 update_type_menu_tick(fe
);
1957 update_copy_menu_greying(fe
);
1959 midend_redraw(fe
->me
);
1963 static HFONT
dialog_title_font()
1965 static HFONT hf
= NULL
;
1971 memset (&lf
, 0, sizeof(LOGFONT
));
1972 lf
.lfHeight
= -11; /* - ((8 * GetDeviceCaps(hdc, LOGPIXELSY)) / 72) */
1973 lf
.lfWeight
= FW_BOLD
;
1974 wcscpy(lf
.lfFaceName
, TEXT("Tahoma"));
1976 return hf
= CreateFontIndirect(&lf
);
1979 static void make_dialog_full_screen(HWND hwnd
)
1981 SHINITDLGINFO shidi
;
1983 /* Make dialog full screen */
1984 shidi
.dwMask
= SHIDIM_FLAGS
;
1985 shidi
.dwFlags
= SHIDIF_DONEBUTTON
| SHIDIF_SIZEDLGFULLSCREEN
|
1988 SHInitDialog(&shidi
);
1992 static int CALLBACK
AboutDlgProc(HWND hwnd
, UINT msg
,
1993 WPARAM wParam
, LPARAM lParam
)
1995 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
2003 make_dialog_full_screen(hwnd
);
2005 sprintf(title
, "About %.250s", fe
->game
->name
);
2006 SetDlgItemTextA(hwnd
, IDC_ABOUT_CAPTION
, title
);
2008 SendDlgItemMessage(hwnd
, IDC_ABOUT_CAPTION
, WM_SETFONT
,
2009 (WPARAM
) dialog_title_font(), 0);
2011 SetDlgItemTextA(hwnd
, IDC_ABOUT_GAME
, fe
->game
->name
);
2012 SetDlgItemTextA(hwnd
, IDC_ABOUT_VERSION
, ver
);
2018 if (LOWORD(wParam
) == IDOK
)
2039 * Wrappers on midend_{get,set}_config, which extend the CFG_*
2040 * enumeration to add CFG_PRINT.
2042 static config_item
*frontend_get_config(frontend
*fe
, int which
,
2045 if (which
< CFG_FRONTEND_SPECIFIC
) {
2046 return midend_get_config(fe
->me
, which
, wintitle
);
2047 } else if (which
== CFG_PRINT
) {
2051 *wintitle
= snewn(40 + strlen(fe
->game
->name
), char);
2052 sprintf(*wintitle
, "%s print setup", fe
->game
->name
);
2054 ret
= snewn(8, config_item
);
2058 ret
[i
].name
= "Number of puzzles to print";
2059 ret
[i
].type
= C_STRING
;
2060 ret
[i
].sval
= dupstr("1");
2064 ret
[i
].name
= "Number of puzzles across the page";
2065 ret
[i
].type
= C_STRING
;
2066 ret
[i
].sval
= dupstr("1");
2070 ret
[i
].name
= "Number of puzzles down the page";
2071 ret
[i
].type
= C_STRING
;
2072 ret
[i
].sval
= dupstr("1");
2076 ret
[i
].name
= "Percentage of standard size";
2077 ret
[i
].type
= C_STRING
;
2078 ret
[i
].sval
= dupstr("100.0");
2082 ret
[i
].name
= "Include currently shown puzzle";
2083 ret
[i
].type
= C_BOOLEAN
;
2088 ret
[i
].name
= "Print solutions";
2089 ret
[i
].type
= C_BOOLEAN
;
2091 ret
[i
].ival
= FALSE
;
2094 if (fe
->game
->can_print_in_colour
) {
2095 ret
[i
].name
= "Print in colour";
2096 ret
[i
].type
= C_BOOLEAN
;
2098 ret
[i
].ival
= FALSE
;
2103 ret
[i
].type
= C_END
;
2110 assert(!"We should never get here");
2115 static char *frontend_set_config(frontend
*fe
, int which
, config_item
*cfg
)
2117 if (which
< CFG_FRONTEND_SPECIFIC
) {
2118 return midend_set_config(fe
->me
, which
, cfg
);
2119 } else if (which
== CFG_PRINT
) {
2120 if ((fe
->printcount
= atoi(cfg
[0].sval
)) <= 0)
2121 return "Number of puzzles to print should be at least one";
2122 if ((fe
->printw
= atoi(cfg
[1].sval
)) <= 0)
2123 return "Number of puzzles across the page should be at least one";
2124 if ((fe
->printh
= atoi(cfg
[2].sval
)) <= 0)
2125 return "Number of puzzles down the page should be at least one";
2126 if ((fe
->printscale
= (float)atof(cfg
[3].sval
)) <= 0)
2127 return "Print size should be positive";
2128 fe
->printcurr
= cfg
[4].ival
;
2129 fe
->printsolns
= cfg
[5].ival
;
2130 fe
->printcolour
= fe
->game
->can_print_in_colour
&& cfg
[6].ival
;
2133 assert(!"We should never get here");
2134 return "Internal error";
2139 /* Separate version of mkctrl function for the Pocket PC. */
2140 /* Control coordinates should be specified in dialog units. */
2141 HWND
mkctrl(frontend
*fe
, int x1
, int x2
, int y1
, int y2
,
2142 LPCTSTR wclass
, int wstyle
,
2143 int exstyle
, const char *wtext
, int wid
)
2148 /* Convert dialog units into pixels */
2149 rc
.left
= x1
; rc
.right
= x2
;
2150 rc
.top
= y1
; rc
.bottom
= y2
;
2151 MapDialogRect(fe
->cfgbox
, &rc
);
2153 MultiByteToWideChar (CP_ACP
, 0, wtext
, -1, wwtext
, 256);
2155 return CreateWindowEx(exstyle
, wclass
, wwtext
,
2156 wstyle
| WS_CHILD
| WS_VISIBLE
,
2158 rc
.right
- rc
.left
, rc
.bottom
- rc
.top
,
2159 fe
->cfgbox
, (HMENU
) wid
, fe
->inst
, NULL
);
2162 static void create_config_controls(frontend
* fe
)
2165 int col1l
, col1r
, col2l
, col2r
, y
;
2170 /* Control placement done in dialog units */
2171 col1l
= 4; col1r
= 96; /* Label column */
2172 col2l
= 100; col2r
= 154; /* Input column (edit boxes and combo boxes) */
2175 * Count the controls so we can allocate cfgaux.
2177 for (nctrls
= 0, i
= fe
->cfg
; i
->type
!= C_END
; i
++)
2179 fe
->cfgaux
= snewn(nctrls
, struct cfg_aux
);
2182 y
= 22; /* Leave some room for the dialog title */
2183 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
2187 * Edit box with a label beside it.
2189 mkctrl(fe
, col1l
, col1r
, y
+ 1, y
+ 11,
2190 TEXT("Static"), SS_LEFTNOWORDWRAP
, 0, i
->name
, id
++);
2191 mkctrl(fe
, col2l
, col2r
, y
, y
+ 12,
2192 TEXT("EDIT"), WS_BORDER
| WS_TABSTOP
| ES_AUTOHSCROLL
,
2193 0, "", (j
->ctlid
= id
++));
2194 SetDlgItemTextA(fe
->cfgbox
, j
->ctlid
, i
->sval
);
2201 mkctrl(fe
, col1l
, col2r
, y
+ 1, y
+ 11, TEXT("BUTTON"),
2202 BS_NOTIFY
| BS_AUTOCHECKBOX
| WS_TABSTOP
,
2203 0, i
->name
, (j
->ctlid
= id
++));
2204 CheckDlgButton(fe
->cfgbox
, j
->ctlid
, (i
->ival
!= 0));
2209 * Drop-down list with a label beside it.
2211 mkctrl(fe
, col1l
, col1r
, y
+ 1, y
+ 11,
2212 TEXT("STATIC"), SS_LEFTNOWORDWRAP
, 0, i
->name
, id
++);
2213 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+ 48,
2214 TEXT("COMBOBOX"), WS_BORDER
| WS_TABSTOP
|
2215 CBS_DROPDOWNLIST
| CBS_HASSTRINGS
,
2216 0, "", (j
->ctlid
= id
++));
2218 char c
, *p
, *q
, *str
;
2224 while (*q
&& *q
!= c
) q
++;
2225 str
= snewn(q
-p
+1, char);
2226 strncpy(str
, p
, q
-p
);
2230 MultiByteToWideChar (CP_ACP
, 0, str
, -1, ws
, 50);
2231 SendMessage(ctl
, CB_ADDSTRING
, 0, (LPARAM
)ws
);
2239 SendMessage(ctl
, CB_SETCURSEL
, i
->ival
, 0);
2249 static int CALLBACK
ConfigDlgProc(HWND hwnd
, UINT msg
,
2250 WPARAM wParam
, LPARAM lParam
)
2252 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
2262 fe
= (frontend
*) lParam
;
2263 SetWindowLong(hwnd
, GWL_USERDATA
, lParam
);
2266 fe
->cfg
= frontend_get_config(fe
, fe
->cfg_which
, &title
);
2268 make_dialog_full_screen(hwnd
);
2270 SetDlgItemTextA(hwnd
, IDC_CONFIG_CAPTION
, title
);
2271 SendDlgItemMessage(hwnd
, IDC_CONFIG_CAPTION
, WM_SETFONT
,
2272 (WPARAM
) dialog_title_font(), 0);
2274 create_config_controls(fe
);
2281 * OK and Cancel are special cases.
2283 if ((LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)) {
2284 if (LOWORD(wParam
) == IDOK
) {
2285 char *err
= frontend_set_config(fe
, fe
->cfg_which
, fe
->cfg
);
2288 MessageBox(hwnd
, err
, "Validation error",
2289 MB_ICONERROR
| MB_OK
);
2308 * First find the control whose id this is.
2310 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
2311 if (j
->ctlid
== LOWORD(wParam
))
2314 if (i
->type
== C_END
)
2315 return 0; /* not our problem */
2317 if (i
->type
== C_STRING
&& HIWORD(wParam
) == EN_CHANGE
) {
2320 TCHAR wBuffer
[4096];
2321 GetDlgItemText(fe
->cfgbox
, j
->ctlid
, wBuffer
, 4096);
2322 WideCharToMultiByte(CP_ACP
, 0, wBuffer
, -1, buffer
, 4096, NULL
, NULL
);
2324 GetDlgItemText(fe
->cfgbox
, j
->ctlid
, buffer
, lenof(buffer
));
2326 buffer
[lenof(buffer
)-1] = '\0';
2328 i
->sval
= dupstr(buffer
);
2329 } else if (i
->type
== C_BOOLEAN
&&
2330 (HIWORD(wParam
) == BN_CLICKED
||
2331 HIWORD(wParam
) == BN_DBLCLK
)) {
2332 i
->ival
= IsDlgButtonChecked(fe
->cfgbox
, j
->ctlid
);
2333 } else if (i
->type
== C_CHOICES
&&
2334 HIWORD(wParam
) == CBN_SELCHANGE
) {
2335 i
->ival
= SendDlgItemMessage(fe
->cfgbox
, j
->ctlid
,
2336 CB_GETCURSEL
, 0, 0);
2350 HWND
mkctrl(frontend
*fe
, int x1
, int x2
, int y1
, int y2
,
2351 char *wclass
, int wstyle
,
2352 int exstyle
, const char *wtext
, int wid
)
2355 ret
= CreateWindowEx(exstyle
, wclass
, wtext
,
2356 wstyle
| WS_CHILD
| WS_VISIBLE
, x1
, y1
, x2
-x1
, y2
-y1
,
2357 fe
->cfgbox
, (HMENU
) wid
, fe
->inst
, NULL
);
2358 SendMessage(ret
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, MAKELPARAM(TRUE
, 0));
2363 static void about(frontend
*fe
)
2366 DialogBox(fe
->inst
, MAKEINTRESOURCE(IDD_ABOUT
), fe
->hwnd
, AboutDlgProc
);
2376 int winwidth
, winheight
, y
;
2377 int height
, width
, maxwid
;
2378 const char *strings
[16];
2383 sprintf(titlebuf
, "About %.250s", fe
->game
->name
);
2385 strings
[nstrings
++] = fe
->game
->name
;
2386 strings
[nstrings
++] = "from Simon Tatham's Portable Puzzle Collection";
2387 strings
[nstrings
++] = ver
;
2389 wc
.style
= CS_DBLCLKS
| CS_SAVEBITS
;
2390 wc
.lpfnWndProc
= DefDlgProc
;
2392 wc
.cbWndExtra
= DLGWINDOWEXTRA
+ 8;
2393 wc
.hInstance
= fe
->inst
;
2395 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
2396 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+1);
2397 wc
.lpszMenuName
= NULL
;
2398 wc
.lpszClassName
= "GameAboutBox";
2401 hdc
= GetDC(fe
->hwnd
);
2402 SetMapMode(hdc
, MM_TEXT
);
2404 fe
->dlg_done
= FALSE
;
2406 fe
->cfgfont
= CreateFont(-MulDiv(8, GetDeviceCaps(hdc
, LOGPIXELSY
), 72),
2408 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
2409 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
2414 oldfont
= SelectObject(hdc
, fe
->cfgfont
);
2415 if (GetTextMetrics(hdc
, &tm
)) {
2416 height
= tm
.tmAscent
+ tm
.tmDescent
;
2417 width
= tm
.tmAveCharWidth
;
2419 height
= width
= 30;
2423 * Figure out the layout of the About box by measuring the
2424 * length of each piece of text.
2427 winheight
= height
/2;
2429 for (i
= 0; i
< nstrings
; i
++) {
2430 if (GetTextExtentPoint32(hdc
, strings
[i
], strlen(strings
[i
]), &size
))
2431 lengths
[i
] = size
.cx
;
2433 lengths
[i
] = 0; /* *shrug* */
2434 if (maxwid
< lengths
[i
])
2435 maxwid
= lengths
[i
];
2436 winheight
+= height
* 3 / 2 + (height
/ 2);
2439 winheight
+= height
+ height
* 7 / 4; /* OK button */
2440 winwidth
= maxwid
+ 4*width
;
2442 SelectObject(hdc
, oldfont
);
2443 ReleaseDC(fe
->hwnd
, hdc
);
2446 * Create the dialog, now that we know its size.
2453 r
.bottom
= winheight
;
2455 AdjustWindowRectEx(&r
, (WS_OVERLAPPEDWINDOW
/*|
2456 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
2457 WS_CAPTION | WS_SYSMENU*/) &~
2458 (WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
2462 * Centre the dialog on its parent window.
2466 GetWindowRect(fe
->hwnd
, &r2
);
2467 r
.left
= (r2
.left
+ r2
.right
- r
.right
) / 2;
2468 r
.top
= (r2
.top
+ r2
.bottom
- r
.bottom
) / 2;
2472 fe
->cfgbox
= CreateWindowEx(0, wc
.lpszClassName
, titlebuf
,
2473 DS_MODALFRAME
| WS_POPUP
| WS_VISIBLE
|
2474 WS_CAPTION
| WS_SYSMENU
,
2476 r
.right
-r
.left
, r
.bottom
-r
.top
,
2477 fe
->hwnd
, NULL
, fe
->inst
, NULL
);
2480 SendMessage(fe
->cfgbox
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, FALSE
);
2482 SetWindowLong(fe
->cfgbox
, GWL_USERDATA
, (LONG
)fe
);
2483 SetWindowLong(fe
->cfgbox
, DWL_DLGPROC
, (LONG
)AboutDlgProc
);
2487 for (i
= 0; i
< nstrings
; i
++) {
2488 int border
= width
*2 + (maxwid
- lengths
[i
]) / 2;
2489 mkctrl(fe
, border
, border
+lengths
[i
], y
+height
*1/8, y
+height
*9/8,
2490 "Static", 0, 0, strings
[i
], id
++);
2493 assert(y
< winheight
);
2497 y
+= height
/2; /* extra space before OK */
2498 mkctrl(fe
, width
*2, maxwid
+width
*2, y
, y
+height
*7/4, "BUTTON",
2499 BS_PUSHBUTTON
| WS_TABSTOP
| BS_DEFPUSHBUTTON
, 0,
2502 SendMessage(fe
->cfgbox
, WM_INITDIALOG
, 0, 0);
2504 EnableWindow(fe
->hwnd
, FALSE
);
2505 ShowWindow(fe
->cfgbox
, SW_SHOWNORMAL
);
2506 while ((gm
=GetMessage(&msg
, NULL
, 0, 0)) > 0) {
2507 if (!IsDialogMessage(fe
->cfgbox
, &msg
))
2508 DispatchMessage(&msg
);
2512 EnableWindow(fe
->hwnd
, TRUE
);
2513 SetForegroundWindow(fe
->hwnd
);
2514 DestroyWindow(fe
->cfgbox
);
2515 DeleteObject(fe
->cfgfont
);
2519 static int get_config(frontend
*fe
, int which
)
2522 fe
->cfg_which
= which
;
2524 return DialogBoxParam(fe
->inst
,
2525 MAKEINTRESOURCE(IDD_CONFIG
),
2526 fe
->hwnd
, ConfigDlgProc
,
2540 int winwidth
, winheight
, col1l
, col1r
, col2l
, col2r
, y
;
2541 int height
, width
, maxlabel
, maxcheckbox
;
2543 wc
.style
= CS_DBLCLKS
| CS_SAVEBITS
;
2544 wc
.lpfnWndProc
= DefDlgProc
;
2546 wc
.cbWndExtra
= DLGWINDOWEXTRA
+ 8;
2547 wc
.hInstance
= fe
->inst
;
2549 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
2550 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+1);
2551 wc
.lpszMenuName
= NULL
;
2552 wc
.lpszClassName
= "GameConfigBox";
2555 hdc
= GetDC(fe
->hwnd
);
2556 SetMapMode(hdc
, MM_TEXT
);
2558 fe
->dlg_done
= FALSE
;
2560 fe
->cfgfont
= CreateFont(-MulDiv(8, GetDeviceCaps(hdc
, LOGPIXELSY
), 72),
2562 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
2563 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
2568 oldfont
= SelectObject(hdc
, fe
->cfgfont
);
2569 if (GetTextMetrics(hdc
, &tm
)) {
2570 height
= tm
.tmAscent
+ tm
.tmDescent
;
2571 width
= tm
.tmAveCharWidth
;
2573 height
= width
= 30;
2576 fe
->cfg
= frontend_get_config(fe
, which
, &title
);
2577 fe
->cfg_which
= which
;
2580 * Figure out the layout of the config box by measuring the
2581 * length of each piece of text.
2583 maxlabel
= maxcheckbox
= 0;
2584 winheight
= height
/2;
2586 for (i
= fe
->cfg
; i
->type
!= C_END
; i
++) {
2591 * Both these control types have a label filling only
2592 * the left-hand column of the box.
2594 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
2597 winheight
+= height
* 3 / 2 + (height
/ 2);
2602 * Checkboxes take up the whole of the box width.
2604 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
2605 maxcheckbox
< size
.cx
)
2606 maxcheckbox
= size
.cx
;
2607 winheight
+= height
+ (height
/ 2);
2612 winheight
+= height
+ height
* 7 / 4; /* OK / Cancel buttons */
2615 col1r
= col1l
+ maxlabel
;
2616 col2l
= col1r
+ 2*width
;
2617 col2r
= col2l
+ 30*width
;
2618 if (col2r
< col1l
+2*height
+maxcheckbox
)
2619 col2r
= col1l
+2*height
+maxcheckbox
;
2620 winwidth
= col2r
+ 2*width
;
2622 SelectObject(hdc
, oldfont
);
2623 ReleaseDC(fe
->hwnd
, hdc
);
2626 * Create the dialog, now that we know its size.
2633 r
.bottom
= winheight
;
2635 AdjustWindowRectEx(&r
, (WS_OVERLAPPEDWINDOW
/*|
2636 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
2637 WS_CAPTION | WS_SYSMENU*/) &~
2638 (WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
2642 * Centre the dialog on its parent window.
2646 GetWindowRect(fe
->hwnd
, &r2
);
2647 r
.left
= (r2
.left
+ r2
.right
- r
.right
) / 2;
2648 r
.top
= (r2
.top
+ r2
.bottom
- r
.bottom
) / 2;
2652 fe
->cfgbox
= CreateWindowEx(0, wc
.lpszClassName
, title
,
2653 DS_MODALFRAME
| WS_POPUP
| WS_VISIBLE
|
2654 WS_CAPTION
| WS_SYSMENU
,
2656 r
.right
-r
.left
, r
.bottom
-r
.top
,
2657 fe
->hwnd
, NULL
, fe
->inst
, NULL
);
2661 SendMessage(fe
->cfgbox
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, FALSE
);
2663 SetWindowLong(fe
->cfgbox
, GWL_USERDATA
, (LONG
)fe
);
2664 SetWindowLong(fe
->cfgbox
, DWL_DLGPROC
, (LONG
)ConfigDlgProc
);
2667 * Count the controls so we can allocate cfgaux.
2669 for (nctrls
= 0, i
= fe
->cfg
; i
->type
!= C_END
; i
++)
2671 fe
->cfgaux
= snewn(nctrls
, struct cfg_aux
);
2675 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
2679 * Edit box with a label beside it.
2681 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
2682 "Static", 0, 0, i
->name
, id
++);
2683 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*3/2,
2684 "EDIT", WS_TABSTOP
| ES_AUTOHSCROLL
,
2685 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
2686 SetWindowText(ctl
, i
->sval
);
2694 mkctrl(fe
, col1l
, col2r
, y
, y
+height
, "BUTTON",
2695 BS_NOTIFY
| BS_AUTOCHECKBOX
| WS_TABSTOP
,
2696 0, i
->name
, (j
->ctlid
= id
++));
2697 CheckDlgButton(fe
->cfgbox
, j
->ctlid
, (i
->ival
!= 0));
2703 * Drop-down list with a label beside it.
2705 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
2706 "STATIC", 0, 0, i
->name
, id
++);
2707 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*41/2,
2708 "COMBOBOX", WS_TABSTOP
|
2709 CBS_DROPDOWNLIST
| CBS_HASSTRINGS
,
2710 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
2712 char c
, *p
, *q
, *str
;
2714 SendMessage(ctl
, CB_RESETCONTENT
, 0, 0);
2719 while (*q
&& *q
!= c
) q
++;
2720 str
= snewn(q
-p
+1, char);
2721 strncpy(str
, p
, q
-p
);
2723 SendMessage(ctl
, CB_ADDSTRING
, 0, (LPARAM
)str
);
2730 SendMessage(ctl
, CB_SETCURSEL
, i
->ival
, 0);
2736 assert(y
< winheight
);
2740 y
+= height
/2; /* extra space before OK and Cancel */
2741 mkctrl(fe
, col1l
, (col1l
+col2r
)/2-width
, y
, y
+height
*7/4, "BUTTON",
2742 BS_PUSHBUTTON
| WS_TABSTOP
| BS_DEFPUSHBUTTON
, 0,
2744 mkctrl(fe
, (col1l
+col2r
)/2+width
, col2r
, y
, y
+height
*7/4, "BUTTON",
2745 BS_PUSHBUTTON
| WS_TABSTOP
, 0, "Cancel", IDCANCEL
);
2747 SendMessage(fe
->cfgbox
, WM_INITDIALOG
, 0, 0);
2749 EnableWindow(fe
->hwnd
, FALSE
);
2750 ShowWindow(fe
->cfgbox
, SW_SHOWNORMAL
);
2751 while ((gm
=GetMessage(&msg
, NULL
, 0, 0)) > 0) {
2752 if (!IsDialogMessage(fe
->cfgbox
, &msg
))
2753 DispatchMessage(&msg
);
2757 EnableWindow(fe
->hwnd
, TRUE
);
2758 SetForegroundWindow(fe
->hwnd
);
2759 DestroyWindow(fe
->cfgbox
);
2760 DeleteObject(fe
->cfgfont
);
2765 return (fe
->dlg_done
== 2);
2770 static void calculate_bitmap_position(frontend
*fe
, int x
, int y
)
2772 /* Pocket PC - center the game in the full screen window */
2776 GetClientRect(fe
->hwnd
, &rcClient
);
2777 fe
->bitmapPosition
.left
= (rcClient
.right
- x
) / 2;
2778 yMargin
= rcClient
.bottom
- y
;
2780 if (fe
->numpad
!= NULL
) {
2782 GetWindowRect(fe
->numpad
, &rcPad
);
2783 yMargin
-= rcPad
.bottom
- rcPad
.top
;
2786 if (fe
->statusbar
!= NULL
) {
2788 GetWindowRect(fe
->statusbar
, &rcStatus
);
2789 yMargin
-= rcStatus
.bottom
- rcStatus
.top
;
2792 fe
->bitmapPosition
.top
= yMargin
/ 2;
2794 fe
->bitmapPosition
.right
= fe
->bitmapPosition
.left
+ x
;
2795 fe
->bitmapPosition
.bottom
= fe
->bitmapPosition
.top
+ y
;
2798 static void calculate_bitmap_position(frontend
*fe
, int x
, int y
)
2800 /* Plain Windows - position the game in the upper-left corner */
2801 fe
->bitmapPosition
.left
= 0;
2802 fe
->bitmapPosition
.top
= 0;
2803 fe
->bitmapPosition
.right
= fe
->bitmapPosition
.left
+ x
;
2804 fe
->bitmapPosition
.bottom
= fe
->bitmapPosition
.top
+ y
;
2808 static void new_bitmap(frontend
*fe
, int x
, int y
)
2812 if (fe
->bitmap
) DeleteObject(fe
->bitmap
);
2814 hdc
= GetDC(fe
->hwnd
);
2815 fe
->bitmap
= CreateCompatibleBitmap(hdc
, x
, y
);
2816 calculate_bitmap_position(fe
, x
, y
);
2817 ReleaseDC(fe
->hwnd
, hdc
);
2820 static void new_game_size(frontend
*fe
, float scale
)
2825 get_max_puzzle_size(fe
, &x
, &y
);
2826 midend_size(fe
->me
, &x
, &y
, FALSE
);
2829 x
= (int)((float)x
* fe
->puzz_scale
);
2830 y
= (int)((float)y
* fe
->puzz_scale
);
2831 midend_size(fe
->me
, &x
, &y
, TRUE
);
2833 fe
->ymin
= (fe
->xmin
* y
) / x
;
2838 AdjustWindowRectEx(&r
, WINFLAGS
, TRUE
, 0);
2840 if (fe
->statusbar
!= NULL
) {
2841 GetWindowRect(fe
->statusbar
, &sr
);
2843 sr
.left
= sr
.right
= sr
.top
= sr
.bottom
= 0;
2846 SetWindowPos(fe
->hwnd
, NULL
, 0, 0,
2848 r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
2849 SWP_NOMOVE
| SWP_NOZORDER
);
2852 check_window_size(fe
, &x
, &y
);
2855 if (fe
->statusbar
!= NULL
)
2856 SetWindowPos(fe
->statusbar
, NULL
, 0, y
, x
,
2857 sr
.bottom
- sr
.top
, SWP_NOZORDER
);
2860 new_bitmap(fe
, x
, y
);
2863 InvalidateRect(fe
->hwnd
, NULL
, TRUE
);
2865 midend_redraw(fe
->me
);
2869 * Given a proposed new window rect, work out the resulting
2870 * difference in client size (from current), and use to try
2871 * and resize the puzzle, returning (wx,wy) as the actual
2875 static void adjust_game_size(frontend
*fe
, RECT
*proposed
, int isedge
,
2876 int *wx_r
, int *wy_r
)
2879 int nx
, ny
, xdiff
, ydiff
, wx
, wy
;
2881 /* Work out the current window sizing, and thus the
2882 * difference in size we're asking for. */
2883 GetClientRect(fe
->hwnd
, &cr
);
2885 AdjustWindowRectEx(&wr
, WINFLAGS
, TRUE
, 0);
2887 xdiff
= (proposed
->right
- proposed
->left
) - (wr
.right
- wr
.left
);
2888 ydiff
= (proposed
->bottom
- proposed
->top
) - (wr
.bottom
- wr
.top
);
2891 /* These next four lines work around the fact that midend_size
2892 * is happy to shrink _but not grow_ if you change one dimension
2893 * but not the other. */
2894 if (xdiff
> 0 && ydiff
== 0)
2895 ydiff
= (xdiff
* (wr
.right
- wr
.left
)) / (wr
.bottom
- wr
.top
);
2896 if (xdiff
== 0 && ydiff
> 0)
2897 xdiff
= (ydiff
* (wr
.bottom
- wr
.top
)) / (wr
.right
- wr
.left
);
2900 if (check_window_resize(fe
,
2901 (cr
.right
- cr
.left
) + xdiff
,
2902 (cr
.bottom
- cr
.top
) + ydiff
,
2903 &nx
, &ny
, &wx
, &wy
)) {
2904 new_bitmap(fe
, nx
, ny
);
2905 midend_force_redraw(fe
->me
);
2907 /* reset size to current window size */
2908 wx
= wr
.right
- wr
.left
;
2909 wy
= wr
.bottom
- wr
.top
;
2911 /* Re-fetch rectangle; size limits mean we might not have
2912 * taken it quite to the mouse drag positions. */
2913 GetClientRect(fe
->hwnd
, &cr
);
2914 adjust_statusbar(fe
, &cr
);
2916 *wx_r
= wx
; *wy_r
= wy
;
2919 static void update_type_menu_tick(frontend
*fe
)
2923 if (fe
->typemenu
== INVALID_HANDLE_VALUE
)
2926 n
= midend_which_preset(fe
->me
);
2928 for (i
= 0; i
< fe
->n_preset_menuitems
; i
++) {
2929 if (fe
->preset_menuitems
[i
].which_menu
) {
2930 int flag
= (i
== n
? MF_CHECKED
: MF_UNCHECKED
);
2931 CheckMenuItem(fe
->preset_menuitems
[i
].which_menu
,
2932 fe
->preset_menuitems
[i
].item_index
,
2933 MF_BYPOSITION
| flag
);
2937 if (fe
->game
->can_configure
) {
2938 int flag
= (n
< 0 ? MF_CHECKED
: MF_UNCHECKED
);
2939 /* "Custom" menu item is at the bottom of the top-level Type menu */
2940 total
= GetMenuItemCount(fe
->typemenu
);
2941 CheckMenuItem(fe
->typemenu
, total
- 1, MF_BYPOSITION
| flag
);
2944 DrawMenuBar(fe
->hwnd
);
2947 static void update_copy_menu_greying(frontend
*fe
)
2949 UINT enable
= (midend_can_format_as_text_now(fe
->me
) ?
2950 MF_ENABLED
: MF_GRAYED
);
2951 EnableMenuItem(fe
->gamemenu
, IDM_COPY
, MF_BYCOMMAND
| enable
);
2954 static void new_game_type(frontend
*fe
)
2956 midend_new_game(fe
->me
);
2957 new_game_size(fe
, 1.0);
2958 update_type_menu_tick(fe
);
2959 update_copy_menu_greying(fe
);
2962 static int is_alt_pressed(void)
2965 int r
= GetKeyboardState(keystate
);
2968 if (keystate
[VK_MENU
] & 0x80)
2970 if (keystate
[VK_RMENU
] & 0x80)
2975 static LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
,
2976 WPARAM wParam
, LPARAM lParam
)
2978 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
2983 DestroyWindow(hwnd
);
2987 /* Numeric pad sends WM_COMMAND messages */
2988 if ((wParam
>= IDM_KEYEMUL
) && (wParam
< IDM_KEYEMUL
+ 256))
2990 midend_process_key(fe
->me
, 0, 0, wParam
- IDM_KEYEMUL
);
2993 cmd
= wParam
& ~0xF; /* low 4 bits reserved to Windows */
2996 if (!midend_process_key(fe
->me
, 0, 0, 'n'))
3000 midend_restart_game(fe
->me
);
3003 if (!midend_process_key(fe
->me
, 0, 0, 'u'))
3007 if (!midend_process_key(fe
->me
, 0, 0, '\x12'))
3012 char *text
= midend_text_format(fe
->me
);
3014 write_clip(hwnd
, text
);
3016 MessageBeep(MB_ICONWARNING
);
3022 char *msg
= midend_solve(fe
->me
);
3024 MessageBox(hwnd
, msg
, "Unable to solve",
3025 MB_ICONERROR
| MB_OK
);
3029 if (!midend_process_key(fe
->me
, 0, 0, 'q'))
3033 if (get_config(fe
, CFG_SETTINGS
))
3037 if (get_config(fe
, CFG_SEED
))
3041 if (get_config(fe
, CFG_DESC
))
3045 if (get_config(fe
, CFG_PRINT
))
3055 char filename
[FILENAME_MAX
];
3058 memset(&of
, 0, sizeof(of
));
3059 of
.hwndOwner
= hwnd
;
3060 of
.lpstrFilter
= "All Files (*.*)\0*\0\0\0";
3061 of
.lpstrCustomFilter
= NULL
;
3062 of
.nFilterIndex
= 1;
3063 of
.lpstrFile
= filename
;
3065 of
.nMaxFile
= lenof(filename
);
3066 of
.lpstrFileTitle
= NULL
;
3067 of
.lpstrTitle
= (cmd
== IDM_SAVE
?
3068 "Enter name of game file to save" :
3069 "Enter name of saved game file to load");
3071 #ifdef OPENFILENAME_SIZE_VERSION_400
3072 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
3074 of
.lStructSize
= sizeof(of
);
3076 of
.lpstrInitialDir
= NULL
;
3078 if (cmd
== IDM_SAVE
)
3079 ret
= GetSaveFileName(&of
);
3081 ret
= GetOpenFileName(&of
);
3084 if (cmd
== IDM_SAVE
) {
3087 if ((fp
= fopen(filename
, "r")) != NULL
) {
3088 char buf
[256 + FILENAME_MAX
];
3092 sprintf(buf
, "Are you sure you want to overwrite"
3093 " the file \"%.*s\"?",
3094 FILENAME_MAX
, filename
);
3095 if (MessageBox(hwnd
, buf
, "Question",
3096 MB_YESNO
| MB_ICONQUESTION
)
3101 fp
= fopen(filename
, "w");
3104 MessageBox(hwnd
, "Unable to open save file",
3105 "Error", MB_ICONERROR
| MB_OK
);
3109 midend_serialise(fe
->me
, savefile_write
, fp
);
3113 FILE *fp
= fopen(filename
, "r");
3115 midend
*me
= fe
->me
;
3121 MessageBox(hwnd
, "Unable to open saved game file",
3122 "Error", MB_ICONERROR
| MB_OK
);
3128 * This save file might be from a different
3131 err
= identify_game(&id_name
, savefile_read
, fp
);
3134 for (i
= 0; i
< gamecount
; i
++)
3135 if (!strcmp(id_name
, gamelist
[i
]->name
))
3137 if (i
== gamecount
) {
3138 err
= "Save file is for a game not "
3139 "supported by this program";
3141 me
= midend_for_new_game(fe
, gamelist
[i
], NULL
,
3142 FALSE
, FALSE
, &err
);
3143 rewind(fp
); /* for the actual load */
3149 err
= midend_deserialise(me
, savefile_read
, fp
);
3154 MessageBox(hwnd
, err
, "Error", MB_ICONERROR
|MB_OK
);
3159 fe_set_midend(fe
, me
);
3160 new_game_size(fe
, 1.0);
3168 start_help(fe
, NULL
);
3171 assert(help_type
!= NONE
);
3172 start_help(fe
, help_type
== CHM
?
3173 fe
->game
->htmlhelp_topic
: fe
->game
->winhelp_topic
);
3178 if (wParam
>= IDM_GAMES
&& wParam
< (IDM_GAMES
+ (WPARAM
)gamecount
)) {
3179 int p
= wParam
- IDM_GAMES
;
3181 fe_set_midend(fe
, midend_for_new_game(fe
, gamelist
[p
], NULL
,
3182 FALSE
, FALSE
, &error
));
3187 game_params
*preset
= preset_menu_lookup_by_id(
3189 ((wParam
&~ 0xF) - IDM_PRESETS
) / 0x10);
3192 midend_set_params(fe
->me
, preset
);
3213 hdc
= BeginPaint(hwnd
, &p
);
3214 hdc2
= CreateCompatibleDC(hdc
);
3215 prevbm
= SelectObject(hdc2
, fe
->bitmap
);
3217 FillRect(hdc
, &(p
.rcPaint
), (HBRUSH
) GetStockObject(WHITE_BRUSH
));
3219 IntersectRect(&rcDest
, &(fe
->bitmapPosition
), &(p
.rcPaint
));
3221 rcDest
.left
, rcDest
.top
,
3222 rcDest
.right
- rcDest
.left
,
3223 rcDest
.bottom
- rcDest
.top
,
3225 rcDest
.left
- fe
->bitmapPosition
.left
,
3226 rcDest
.top
- fe
->bitmapPosition
.top
,
3228 SelectObject(hdc2
, prevbm
);
3237 int r
= GetKeyboardState(keystate
);
3238 int shift
= (r
&& (keystate
[VK_SHIFT
] & 0x80)) ? MOD_SHFT
: 0;
3239 int ctrl
= (r
&& (keystate
[VK_CONTROL
] & 0x80)) ? MOD_CTRL
: 0;
3243 if (!(lParam
& 0x01000000))
3244 key
= MOD_NUM_KEYPAD
| '4';
3246 key
= shift
| ctrl
| CURSOR_LEFT
;
3249 if (!(lParam
& 0x01000000))
3250 key
= MOD_NUM_KEYPAD
| '6';
3252 key
= shift
| ctrl
| CURSOR_RIGHT
;
3255 if (!(lParam
& 0x01000000))
3256 key
= MOD_NUM_KEYPAD
| '8';
3258 key
= shift
| ctrl
| CURSOR_UP
;
3261 if (!(lParam
& 0x01000000))
3262 key
= MOD_NUM_KEYPAD
| '2';
3264 key
= shift
| ctrl
| CURSOR_DOWN
;
3267 * Diagonal keys on the numeric keypad.
3270 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '9';
3273 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '3';
3276 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '7';
3279 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '1';
3282 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '0';
3285 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '5';
3288 * Numeric keypad keys with Num Lock on.
3290 case VK_NUMPAD4
: key
= MOD_NUM_KEYPAD
| '4'; break;
3291 case VK_NUMPAD6
: key
= MOD_NUM_KEYPAD
| '6'; break;
3292 case VK_NUMPAD8
: key
= MOD_NUM_KEYPAD
| '8'; break;
3293 case VK_NUMPAD2
: key
= MOD_NUM_KEYPAD
| '2'; break;
3294 case VK_NUMPAD5
: key
= MOD_NUM_KEYPAD
| '5'; break;
3295 case VK_NUMPAD9
: key
= MOD_NUM_KEYPAD
| '9'; break;
3296 case VK_NUMPAD3
: key
= MOD_NUM_KEYPAD
| '3'; break;
3297 case VK_NUMPAD7
: key
= MOD_NUM_KEYPAD
| '7'; break;
3298 case VK_NUMPAD1
: key
= MOD_NUM_KEYPAD
| '1'; break;
3299 case VK_NUMPAD0
: key
= MOD_NUM_KEYPAD
| '0'; break;
3303 if (!midend_process_key(fe
->me
, 0, 0, key
))
3308 m
.message
= WM_KEYDOWN
;
3310 m
.lParam
= lParam
& 0xdfff;
3311 TranslateMessage(&m
);
3315 case WM_LBUTTONDOWN
:
3316 case WM_RBUTTONDOWN
:
3317 case WM_MBUTTONDOWN
:
3322 * Shift-clicks count as middle-clicks, since otherwise
3323 * two-button Windows users won't have any kind of
3324 * middle click to use.
3326 if (message
== WM_MBUTTONDOWN
|| (wParam
& MK_SHIFT
))
3327 button
= MIDDLE_BUTTON
;
3328 else if (message
== WM_RBUTTONDOWN
|| is_alt_pressed())
3329 button
= RIGHT_BUTTON
;
3332 button
= LEFT_BUTTON
;
3334 if ((fe
->game
->flags
& REQUIRE_RBUTTON
) == 0)
3335 button
= LEFT_BUTTON
;
3340 shrgi
.cbSize
= sizeof(SHRGINFO
);
3341 shrgi
.hwndClient
= hwnd
;
3342 shrgi
.ptDown
.x
= (signed short)LOWORD(lParam
);
3343 shrgi
.ptDown
.y
= (signed short)HIWORD(lParam
);
3344 shrgi
.dwFlags
= SHRG_RETURNCMD
;
3346 if (GN_CONTEXTMENU
== SHRecognizeGesture(&shrgi
))
3347 button
= RIGHT_BUTTON
;
3349 button
= LEFT_BUTTON
;
3353 if (!midend_process_key(fe
->me
,
3354 (signed short)LOWORD(lParam
) - fe
->bitmapPosition
.left
,
3355 (signed short)HIWORD(lParam
) - fe
->bitmapPosition
.top
,
3369 * Shift-clicks count as middle-clicks, since otherwise
3370 * two-button Windows users won't have any kind of
3371 * middle click to use.
3373 if (message
== WM_MBUTTONUP
|| (wParam
& MK_SHIFT
))
3374 button
= MIDDLE_RELEASE
;
3375 else if (message
== WM_RBUTTONUP
|| is_alt_pressed())
3376 button
= RIGHT_RELEASE
;
3378 button
= LEFT_RELEASE
;
3380 if (!midend_process_key(fe
->me
,
3381 (signed short)LOWORD(lParam
) - fe
->bitmapPosition
.left
,
3382 (signed short)HIWORD(lParam
) - fe
->bitmapPosition
.top
,
3393 if (wParam
& (MK_MBUTTON
| MK_SHIFT
))
3394 button
= MIDDLE_DRAG
;
3395 else if (wParam
& MK_RBUTTON
|| is_alt_pressed())
3396 button
= RIGHT_DRAG
;
3400 if (!midend_process_key(fe
->me
,
3401 (signed short)LOWORD(lParam
) - fe
->bitmapPosition
.left
,
3402 (signed short)HIWORD(lParam
) - fe
->bitmapPosition
.top
,
3408 if (!midend_process_key(fe
->me
, 0, 0, (unsigned char)wParam
))
3413 DWORD now
= GetTickCount();
3414 float elapsed
= (float) (now
- fe
->timer_last_tickcount
) * 0.001F
;
3415 midend_timer(fe
->me
, elapsed
);
3416 fe
->timer_last_tickcount
= now
;
3422 RECT
*sr
= (RECT
*)lParam
;
3423 int wx
, wy
, isedge
= 0;
3425 if (wParam
== WMSZ_TOP
||
3426 wParam
== WMSZ_RIGHT
||
3427 wParam
== WMSZ_BOTTOM
||
3428 wParam
== WMSZ_LEFT
) isedge
= 1;
3429 adjust_game_size(fe
, sr
, isedge
, &wx
, &wy
);
3431 /* Given the window size the puzzles constrain
3432 * us to, work out which edge we should be moving. */
3433 if (wParam
== WMSZ_TOP
||
3434 wParam
== WMSZ_TOPLEFT
||
3435 wParam
== WMSZ_TOPRIGHT
) {
3436 sr
->top
= sr
->bottom
- wy
;
3438 sr
->bottom
= sr
->top
+ wy
;
3440 if (wParam
== WMSZ_LEFT
||
3441 wParam
== WMSZ_TOPLEFT
||
3442 wParam
== WMSZ_BOTTOMLEFT
) {
3443 sr
->left
= sr
->right
- wx
;
3445 sr
->right
= sr
->left
+ wx
;
3453 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
3457 static int FindPreviousInstance()
3459 /* Check if application is running. If it's running then focus on the window */
3460 HWND hOtherWnd
= NULL
;
3462 hOtherWnd
= FindWindow (wGameName
, wGameName
);
3465 SetForegroundWindow (hOtherWnd
);
3474 * Split a complete command line into argc/argv, attempting to do it
3475 * exactly the same way the Visual Studio C library would do it (so
3476 * that our console utilities, which receive argc and argv already
3477 * broken apart by the C library, will have their command lines
3478 * processed in the same way as the GUI utilities which get a whole
3479 * command line and must call this function).
3481 * Does not modify the input command line.
3483 * The final parameter (argstart) is used to return a second array
3484 * of char * pointers, the same length as argv, each one pointing
3485 * at the start of the corresponding element of argv in the
3486 * original command line. So if you get half way through processing
3487 * your command line in argc/argv form and then decide you want to
3488 * treat the rest as a raw string, you can. If you don't want to,
3489 * `argstart' can be safely left NULL.
3491 void split_into_argv(char *cmdline
, int *argc
, char ***argv
,
3495 char *outputline
, *q
;
3496 char **outputargv
, **outputargstart
;
3500 * These argument-breaking rules apply to Visual Studio 7, which
3501 * is currently the compiler expected to be used for the Windows
3502 * port of my puzzles. Visual Studio 10 has different rules,
3503 * lacking the curious mod 3 behaviour of consecutive quotes
3504 * described below; I presume they fixed a bug. As and when we
3505 * migrate to a newer compiler, we'll have to adjust this to
3506 * match; however, for the moment we faithfully imitate in our GUI
3507 * utilities what our CLI utilities can't be prevented from doing.
3509 * When I investigated this, at first glance the rules appeared to
3512 * - Single quotes are not special characters.
3514 * - Double quotes are removed, but within them spaces cease
3517 * - Backslashes are _only_ special when a sequence of them
3518 * appear just before a double quote. In this situation,
3519 * they are treated like C backslashes: so \" just gives a
3520 * literal quote, \\" gives a literal backslash and then
3521 * opens or closes a double-quoted segment, \\\" gives a
3522 * literal backslash and then a literal quote, \\\\" gives
3523 * two literal backslashes and then opens/closes a
3524 * double-quoted segment, and so forth. Note that this
3525 * behaviour is identical inside and outside double quotes.
3527 * - Two successive double quotes become one literal double
3528 * quote, but only _inside_ a double-quoted segment.
3529 * Outside, they just form an empty double-quoted segment
3530 * (which may cause an empty argument word).
3532 * - That only leaves the interesting question of what happens
3533 * when one or more backslashes precedes two or more double
3534 * quotes, starting inside a double-quoted string. And the
3535 * answer to that appears somewhat bizarre. Here I tabulate
3536 * number of backslashes (across the top) against number of
3537 * quotes (down the left), and indicate how many backslashes
3538 * are output, how many quotes are output, and whether a
3539 * quoted segment is open at the end of the sequence:
3545 * 0 0,0,y | 1,0,y 2,0,y 3,0,y 4,0,y
3546 * --------+-----------------------------
3547 * 1 0,0,n | 0,1,y 1,0,n 1,1,y 2,0,n
3548 * q 2 0,1,n | 0,1,n 1,1,n 1,1,n 2,1,n
3549 * u 3 0,1,y | 0,2,n 1,1,y 1,2,n 2,1,y
3550 * o 4 0,1,n | 0,2,y 1,1,n 1,2,y 2,1,n
3551 * t 5 0,2,n | 0,2,n 1,2,n 1,2,n 2,2,n
3552 * e 6 0,2,y | 0,3,n 1,2,y 1,3,n 2,2,y
3553 * s 7 0,2,n | 0,3,y 1,2,n 1,3,y 2,2,n
3554 * 8 0,3,n | 0,3,n 1,3,n 1,3,n 2,3,n
3555 * 9 0,3,y | 0,4,n 1,3,y 1,4,n 2,3,y
3556 * 10 0,3,n | 0,4,y 1,3,n 1,4,y 2,3,n
3557 * 11 0,4,n | 0,4,n 1,4,n 1,4,n 2,4,n
3560 * [Test fragment was of the form "a\\\"""b c" d.]
3562 * There is very weird mod-3 behaviour going on here in the
3563 * number of quotes, and it even applies when there aren't any
3564 * backslashes! How ghastly.
3566 * With a bit of thought, this extremely odd diagram suddenly
3567 * coalesced itself into a coherent, if still ghastly, model of
3570 * - As before, backslashes are only special when one or more
3571 * of them appear contiguously before at least one double
3572 * quote. In this situation the backslashes do exactly what
3573 * you'd expect: each one quotes the next thing in front of
3574 * it, so you end up with n/2 literal backslashes (if n is
3575 * even) or (n-1)/2 literal backslashes and a literal quote
3576 * (if n is odd). In the latter case the double quote
3577 * character right after the backslashes is used up.
3579 * - After that, any remaining double quotes are processed. A
3580 * string of contiguous unescaped double quotes has a mod-3
3583 * * inside a quoted segment, a quote ends the segment.
3584 * * _immediately_ after ending a quoted segment, a quote
3585 * simply produces a literal quote.
3586 * * otherwise, outside a quoted segment, a quote begins a
3589 * So, for example, if we started inside a quoted segment
3590 * then two contiguous quotes would close the segment and
3591 * produce a literal quote; three would close the segment,
3592 * produce a literal quote, and open a new segment. If we
3593 * started outside a quoted segment, then two contiguous
3594 * quotes would open and then close a segment, producing no
3595 * output (but potentially creating a zero-length argument);
3596 * but three quotes would open and close a segment and then
3597 * produce a literal quote.
3601 * First deal with the simplest of all special cases: if there
3602 * aren't any arguments, return 0,NULL,NULL.
3604 while (*cmdline
&& isspace(*cmdline
)) cmdline
++;
3606 if (argc
) *argc
= 0;
3607 if (argv
) *argv
= NULL
;
3608 if (argstart
) *argstart
= NULL
;
3613 * This will guaranteeably be big enough; we can realloc it
3616 outputline
= snewn(1+strlen(cmdline
), char);
3617 outputargv
= snewn(strlen(cmdline
)+1 / 2, char *);
3618 outputargstart
= snewn(strlen(cmdline
)+1 / 2, char *);
3620 p
= cmdline
; q
= outputline
; outputargc
= 0;
3625 /* Skip whitespace searching for start of argument. */
3626 while (*p
&& isspace(*p
)) p
++;
3629 /* We have an argument; start it. */
3630 outputargv
[outputargc
] = q
;
3631 outputargstart
[outputargc
] = p
;
3635 /* Copy data into the argument until it's finished. */
3637 if (!quote
&& isspace(*p
))
3638 break; /* argument is finished */
3640 if (*p
== '"' || *p
== '\\') {
3642 * We have a sequence of zero or more backslashes
3643 * followed by a sequence of zero or more quotes.
3644 * Count up how many of each, and then deal with
3645 * them as appropriate.
3647 int i
, slashes
= 0, quotes
= 0;
3648 while (*p
== '\\') slashes
++, p
++;
3649 while (*p
== '"') quotes
++, p
++;
3653 * Special case: if there are no quotes,
3654 * slashes are not special at all, so just copy
3655 * n slashes to the output string.
3657 while (slashes
--) *q
++ = '\\';
3659 /* Slashes annihilate in pairs. */
3660 while (slashes
>= 2) slashes
-= 2, *q
++ = '\\';
3662 /* One remaining slash takes out the first quote. */
3663 if (slashes
) quotes
--, *q
++ = '"';
3666 /* Outside a quote segment, a quote starts one. */
3667 if (!quote
) quotes
--, quote
= 1;
3669 /* Now we produce (n+1)/3 literal quotes... */
3670 for (i
= 3; i
<= quotes
+1; i
+= 3) *q
++ = '"';
3672 /* ... and end in a quote segment iff 3 divides n. */
3673 quote
= (quotes
% 3 == 0);
3681 /* At the end of an argument, just append a trailing NUL. */
3685 outputargv
= sresize(outputargv
, outputargc
, char *);
3686 outputargstart
= sresize(outputargstart
, outputargc
, char *);
3688 if (argc
) *argc
= outputargc
;
3689 if (argv
) *argv
= outputargv
; else sfree(outputargv
);
3690 if (argstart
) *argstart
= outputargstart
; else sfree(outputargstart
);
3693 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
)
3703 split_into_argv(cmdline
, &argc
, &argv
, NULL
);
3706 MultiByteToWideChar (CP_ACP
, 0, CLASSNAME
, -1, wClassName
, 256);
3707 if (FindPreviousInstance ())
3711 InitCommonControls();
3717 wndclass
.lpfnWndProc
= WndProc
;
3718 wndclass
.cbClsExtra
= 0;
3719 wndclass
.cbWndExtra
= 0;
3720 wndclass
.hInstance
= inst
;
3721 wndclass
.hIcon
= LoadIcon(inst
, MAKEINTRESOURCE(200));
3723 if (!wndclass
.hIcon
) /* in case resource file is absent */
3724 wndclass
.hIcon
= LoadIcon(inst
, IDI_APPLICATION
);
3726 wndclass
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
3727 wndclass
.hbrBackground
= NULL
;
3728 wndclass
.lpszMenuName
= NULL
;
3730 wndclass
.lpszClassName
= wClassName
;
3732 wndclass
.lpszClassName
= CLASSNAME
;
3735 RegisterClass(&wndclass
);
3738 while (*cmdline
&& isspace((unsigned char)*cmdline
))
3747 for (i
= 0; i
< gamecount
; i
++) {
3748 const char *p
= gamelist
[i
]->name
;
3751 if (isspace((unsigned char)*p
)) {
3752 while (*q
&& isspace((unsigned char)*q
))
3755 if (tolower((unsigned char)*p
) !=
3756 tolower((unsigned char)*q
))
3774 fe
= frontend_new(inst
);
3775 me
= midend_for_new_game(fe
, gg
, argc
> 0 ? argv
[0] : NULL
,
3776 TRUE
, TRUE
, &error
);
3780 sprintf(buf
, "Puzzles Error");
3782 sprintf(buf
, "%.100s Error", gg
->name
);
3784 MessageBox(NULL
, error
, buf
, MB_OK
|MB_ICONERROR
);
3788 fe_set_midend(fe
, me
);
3791 while (GetMessage(&msg
, NULL
, 0, 0)) {
3792 DispatchMessage(&msg
);
3795 DestroyWindow(fe
->hwnd
);
3800 /* vim: set shiftwidth=4 tabstop=8: */