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
};
201 HWND hwnd
, statusbar
, cfgbox
;
203 HWND numpad
; /* window handle for the numeric pad */
206 HBITMAP bitmap
, prevbm
;
207 RECT bitmapPosition
; /* game bitmap position within game window */
213 HMENU gamemenu
, typemenu
;
215 DWORD timer_last_tickcount
;
217 game_params
**presets
;
219 int nfonts
, fontsize
;
221 struct cfg_aux
*cfgaux
;
222 int cfg_which
, dlg_done
;
227 enum { DRAWING
, PRINTING
, NOTHING
} drawstatus
;
229 int printcount
, printw
, printh
, printsolns
, printcurr
, printcolour
;
231 int printoffsetx
, printoffsety
;
232 float printpixelscale
;
234 int linewidth
, linedotted
;
240 void frontend_free(frontend
*fe
)
253 static void update_type_menu_tick(frontend
*fe
);
254 static void update_copy_menu_greying(frontend
*fe
);
256 void fatal(char *fmt
, ...)
262 vsprintf(buf
, fmt
, ap
);
265 MessageBox(NULL
, buf
, "Fatal error", MB_ICONEXCLAMATION
| MB_OK
);
270 char *geterrstr(void)
273 DWORD dw
= GetLastError();
277 FORMAT_MESSAGE_ALLOCATE_BUFFER
|
278 FORMAT_MESSAGE_FROM_SYSTEM
,
281 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
285 ret
= dupstr(lpMsgBuf
);
292 void get_random_seed(void **randseed
, int *randseedsize
)
294 SYSTEMTIME
*st
= snew(SYSTEMTIME
);
298 *randseed
= (void *)st
;
299 *randseedsize
= sizeof(SYSTEMTIME
);
302 static void win_status_bar(void *handle
, char *text
)
307 frontend
*fe
= (frontend
*)handle
;
310 MultiByteToWideChar (CP_ACP
, 0, text
, -1, wText
, 255);
311 SendMessage(fe
->statusbar
, SB_SETTEXT
,
312 (WPARAM
) 255 | SBT_NOBORDERS
,
315 SetWindowText(fe
->statusbar
, text
);
319 static blitter
*win_blitter_new(void *handle
, int w
, int h
)
321 blitter
*bl
= snew(blitter
);
323 memset(bl
, 0, sizeof(blitter
));
331 static void win_blitter_free(void *handle
, blitter
*bl
)
333 if (bl
->bitmap
) DeleteObject(bl
->bitmap
);
337 static void blitter_mkbitmap(frontend
*fe
, blitter
*bl
)
339 HDC hdc
= GetDC(fe
->hwnd
);
340 bl
->bitmap
= CreateCompatibleBitmap(hdc
, bl
->w
, bl
->h
);
341 ReleaseDC(fe
->hwnd
, hdc
);
344 /* BitBlt(dstDC, dstX, dstY, dstW, dstH, srcDC, srcX, srcY, dType) */
346 static void win_blitter_save(void *handle
, blitter
*bl
, int x
, int y
)
348 frontend
*fe
= (frontend
*)handle
;
349 HDC hdc_win
, hdc_blit
;
352 assert(fe
->drawstatus
== DRAWING
);
354 if (!bl
->bitmap
) blitter_mkbitmap(fe
, bl
);
356 bl
->x
= x
; bl
->y
= y
;
358 hdc_win
= GetDC(fe
->hwnd
);
359 hdc_blit
= CreateCompatibleDC(hdc_win
);
360 if (!hdc_blit
) fatal("hdc_blit failed: 0x%x", GetLastError());
362 prev_blit
= SelectObject(hdc_blit
, bl
->bitmap
);
363 if (prev_blit
== NULL
|| prev_blit
== HGDI_ERROR
)
364 fatal("SelectObject for hdc_main failed: 0x%x", GetLastError());
366 if (!BitBlt(hdc_blit
, 0, 0, bl
->w
, bl
->h
,
367 fe
->hdc
, x
, y
, SRCCOPY
))
368 fatal("BitBlt failed: 0x%x", GetLastError());
370 SelectObject(hdc_blit
, prev_blit
);
372 ReleaseDC(fe
->hwnd
, hdc_win
);
375 static void win_blitter_load(void *handle
, blitter
*bl
, int x
, int y
)
377 frontend
*fe
= (frontend
*)handle
;
378 HDC hdc_win
, hdc_blit
;
381 assert(fe
->drawstatus
== DRAWING
);
383 assert(bl
->bitmap
); /* we should always have saved before loading */
385 if (x
== BLITTER_FROMSAVED
) x
= bl
->x
;
386 if (y
== BLITTER_FROMSAVED
) y
= bl
->y
;
388 hdc_win
= GetDC(fe
->hwnd
);
389 hdc_blit
= CreateCompatibleDC(hdc_win
);
391 prev_blit
= SelectObject(hdc_blit
, bl
->bitmap
);
393 BitBlt(fe
->hdc
, x
, y
, bl
->w
, bl
->h
,
394 hdc_blit
, 0, 0, SRCCOPY
);
396 SelectObject(hdc_blit
, prev_blit
);
398 ReleaseDC(fe
->hwnd
, hdc_win
);
401 void frontend_default_colour(frontend
*fe
, float *output
)
403 DWORD c
= GetSysColor(COLOR_MENU
); /* ick */
405 output
[0] = (float)(GetRValue(c
) / 255.0);
406 output
[1] = (float)(GetGValue(c
) / 255.0);
407 output
[2] = (float)(GetBValue(c
) / 255.0);
410 static POINT
win_transform_point(frontend
*fe
, int x
, int y
)
414 assert(fe
->drawstatus
!= NOTHING
);
416 if (fe
->drawstatus
== PRINTING
) {
417 ret
.x
= (int)(fe
->printoffsetx
+ fe
->printpixelscale
* x
);
418 ret
.y
= (int)(fe
->printoffsety
+ fe
->printpixelscale
* y
);
427 static void win_text_colour(frontend
*fe
, int colour
)
429 assert(fe
->drawstatus
!= NOTHING
);
431 if (fe
->drawstatus
== PRINTING
) {
434 print_get_colour(fe
->dr
, colour
, fe
->printcolour
, &hatch
, &r
, &g
, &b
);
437 * Displaying text in hatched colours is not permitted.
441 SetTextColor(fe
->hdc
, RGB(r
* 255, g
* 255, b
* 255));
443 SetTextColor(fe
->hdc
, fe
->colours
[colour
]);
447 static void win_set_brush(frontend
*fe
, int colour
)
450 assert(fe
->drawstatus
!= NOTHING
);
452 if (fe
->drawstatus
== PRINTING
) {
455 print_get_colour(fe
->dr
, colour
, fe
->printcolour
, &hatch
, &r
, &g
, &b
);
458 br
= CreateSolidBrush(RGB(r
* 255, g
* 255, b
* 255));
462 * This is only ever required during printing, and the
463 * PocketPC port doesn't support printing.
465 fatal("CreateHatchBrush not supported");
467 br
= CreateHatchBrush(hatch
== HATCH_BACKSLASH ? HS_FDIAGONAL
:
468 hatch
== HATCH_SLASH ? HS_BDIAGONAL
:
469 hatch
== HATCH_HORIZ ? HS_HORIZONTAL
:
470 hatch
== HATCH_VERT ? HS_VERTICAL
:
471 hatch
== HATCH_PLUS ? HS_CROSS
:
472 /* hatch == HATCH_X ? */ HS_DIAGCROSS
,
477 br
= fe
->brushes
[colour
];
479 fe
->oldbr
= SelectObject(fe
->hdc
, br
);
482 static void win_reset_brush(frontend
*fe
)
486 assert(fe
->drawstatus
!= NOTHING
);
488 br
= SelectObject(fe
->hdc
, fe
->oldbr
);
489 if (fe
->drawstatus
== PRINTING
)
493 static void win_set_pen(frontend
*fe
, int colour
, int thin
)
496 assert(fe
->drawstatus
!= NOTHING
);
498 if (fe
->drawstatus
== PRINTING
) {
501 int width
= thin ?
0 : fe
->linewidth
;
506 print_get_colour(fe
->dr
, colour
, fe
->printcolour
, &hatch
, &r
, &g
, &b
);
508 * Stroking in hatched colours is not permitted.
511 pen
= CreatePen(fe
->linedotted ? PS_DOT
: PS_SOLID
,
512 width
, RGB(r
* 255, g
* 255, b
* 255));
514 pen
= fe
->pens
[colour
];
516 fe
->oldpen
= SelectObject(fe
->hdc
, pen
);
519 static void win_reset_pen(frontend
*fe
)
523 assert(fe
->drawstatus
!= NOTHING
);
525 pen
= SelectObject(fe
->hdc
, fe
->oldpen
);
526 if (fe
->drawstatus
== PRINTING
)
530 static void win_clip(void *handle
, int x
, int y
, int w
, int h
)
532 frontend
*fe
= (frontend
*)handle
;
535 if (fe
->drawstatus
== NOTHING
)
538 p
= win_transform_point(fe
, x
, y
);
539 q
= win_transform_point(fe
, x
+w
, y
+h
);
540 IntersectClipRect(fe
->hdc
, p
.x
, p
.y
, q
.x
, q
.y
);
543 static void win_unclip(void *handle
)
545 frontend
*fe
= (frontend
*)handle
;
547 if (fe
->drawstatus
== NOTHING
)
550 SelectClipRgn(fe
->hdc
, NULL
);
553 static void win_draw_text(void *handle
, int x
, int y
, int fonttype
,
554 int fontsize
, int align
, int colour
, char *text
)
556 frontend
*fe
= (frontend
*)handle
;
561 if (fe
->drawstatus
== NOTHING
)
564 if (fe
->drawstatus
== PRINTING
)
565 fontsize
= (int)(fontsize
* fe
->printpixelscale
);
567 xy
= win_transform_point(fe
, x
, y
);
570 * Find or create the font.
572 for (i
= fe
->fontstart
; i
< fe
->nfonts
; i
++)
573 if (fe
->fonts
[i
].type
== fonttype
&& fe
->fonts
[i
].size
== fontsize
)
576 if (i
== fe
->nfonts
) {
577 if (fe
->fontsize
<= fe
->nfonts
) {
578 fe
->fontsize
= fe
->nfonts
+ 10;
579 fe
->fonts
= sresize(fe
->fonts
, fe
->fontsize
, struct font
);
584 fe
->fonts
[i
].type
= fonttype
;
585 fe
->fonts
[i
].size
= fontsize
;
587 memset (&lf
, 0, sizeof(LOGFONT
));
588 lf
.lfHeight
= -fontsize
;
589 lf
.lfWeight
= (fe
->drawstatus
== PRINTING ?
0 : FW_BOLD
);
590 lf
.lfCharSet
= DEFAULT_CHARSET
;
591 lf
.lfOutPrecision
= OUT_DEFAULT_PRECIS
;
592 lf
.lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
593 lf
.lfQuality
= DEFAULT_QUALITY
;
594 lf
.lfPitchAndFamily
= (fonttype
== FONT_FIXED ?
595 FIXED_PITCH
| FF_DONTCARE
:
596 VARIABLE_PITCH
| FF_SWISS
);
598 wcscpy(lf
.lfFaceName
, TEXT("Tahoma"));
601 fe
->fonts
[i
].font
= CreateFontIndirect(&lf
);
605 * Position and draw the text.
612 MultiByteToWideChar (CP_UTF8
, 0, text
, -1, wText
, 256);
614 oldfont
= SelectObject(fe
->hdc
, fe
->fonts
[i
].font
);
615 if (GetTextMetrics(fe
->hdc
, &tm
)) {
616 if (align
& ALIGN_VCENTRE
)
617 xy
.y
-= (tm
.tmAscent
+tm
.tmDescent
)/2;
621 if (GetTextExtentPoint32W(fe
->hdc
, wText
, wcslen(wText
), &size
))
623 if (align
& ALIGN_HCENTRE
)
625 else if (align
& ALIGN_HRIGHT
)
628 SetBkMode(fe
->hdc
, TRANSPARENT
);
629 win_text_colour(fe
, colour
);
630 ExtTextOutW(fe
->hdc
, xy
.x
, xy
.y
, 0, NULL
, wText
, wcslen(wText
), NULL
);
631 SelectObject(fe
->hdc
, oldfont
);
635 static void win_draw_rect(void *handle
, int x
, int y
, int w
, int h
, int colour
)
637 frontend
*fe
= (frontend
*)handle
;
640 if (fe
->drawstatus
== NOTHING
)
643 if (fe
->drawstatus
== DRAWING
&& w
== 1 && h
== 1) {
645 * Rectangle() appears to get uppity if asked to draw a 1x1
646 * rectangle, presumably on the grounds that that's beneath
647 * its dignity and you ought to be using SetPixel instead.
650 SetPixel(fe
->hdc
, x
, y
, fe
->colours
[colour
]);
652 win_set_brush(fe
, colour
);
653 win_set_pen(fe
, colour
, TRUE
);
654 p
= win_transform_point(fe
, x
, y
);
655 q
= win_transform_point(fe
, x
+w
, y
+h
);
656 Rectangle(fe
->hdc
, p
.x
, p
.y
, q
.x
, q
.y
);
662 static void win_draw_line(void *handle
, int x1
, int y1
, int x2
, int y2
, int colour
)
664 frontend
*fe
= (frontend
*)handle
;
667 if (fe
->drawstatus
== NOTHING
)
670 win_set_pen(fe
, colour
, FALSE
);
671 pp
[0] = win_transform_point(fe
, x1
, y1
);
672 pp
[1] = win_transform_point(fe
, x2
, y2
);
673 Polyline(fe
->hdc
, pp
, 2);
674 if (fe
->drawstatus
== DRAWING
)
675 SetPixel(fe
->hdc
, pp
[1].x
, pp
[1].y
, fe
->colours
[colour
]);
679 static void win_draw_circle(void *handle
, int cx
, int cy
, int radius
,
680 int fillcolour
, int outlinecolour
)
682 frontend
*fe
= (frontend
*)handle
;
685 assert(outlinecolour
>= 0);
687 if (fe
->drawstatus
== NOTHING
)
691 win_set_brush(fe
, fillcolour
);
693 fe
->oldbr
= SelectObject(fe
->hdc
, GetStockObject(NULL_BRUSH
));
695 win_set_pen(fe
, outlinecolour
, FALSE
);
696 p
= win_transform_point(fe
, cx
- radius
, cy
- radius
);
697 q
= win_transform_point(fe
, cx
+ radius
, cy
+ radius
);
698 Ellipse(fe
->hdc
, p
.x
, p
.y
, q
.x
+1, q
.y
+1);
703 static void win_draw_polygon(void *handle
, int *coords
, int npoints
,
704 int fillcolour
, int outlinecolour
)
706 frontend
*fe
= (frontend
*)handle
;
710 if (fe
->drawstatus
== NOTHING
)
713 pts
= snewn(npoints
+1, POINT
);
715 for (i
= 0; i
<= npoints
; i
++) {
716 int j
= (i
< npoints ? i
: 0);
717 pts
[i
] = win_transform_point(fe
, coords
[j
*2], coords
[j
*2+1]);
720 assert(outlinecolour
>= 0);
722 if (fillcolour
>= 0) {
723 win_set_brush(fe
, fillcolour
);
724 win_set_pen(fe
, outlinecolour
, FALSE
);
725 Polygon(fe
->hdc
, pts
, npoints
);
729 win_set_pen(fe
, outlinecolour
, FALSE
);
730 Polyline(fe
->hdc
, pts
, npoints
+1);
737 static void win_start_draw(void *handle
)
739 frontend
*fe
= (frontend
*)handle
;
742 assert(fe
->drawstatus
== NOTHING
);
744 hdc_win
= GetDC(fe
->hwnd
);
745 fe
->hdc
= CreateCompatibleDC(hdc_win
);
746 fe
->prevbm
= SelectObject(fe
->hdc
, fe
->bitmap
);
747 ReleaseDC(fe
->hwnd
, hdc_win
);
750 SetMapMode(fe
->hdc
, MM_TEXT
);
752 fe
->drawstatus
= DRAWING
;
755 static void win_draw_update(void *handle
, int x
, int y
, int w
, int h
)
757 frontend
*fe
= (frontend
*)handle
;
760 if (fe
->drawstatus
!= DRAWING
)
768 OffsetRect(&r
, fe
->bitmapPosition
.left
, fe
->bitmapPosition
.top
);
769 InvalidateRect(fe
->hwnd
, &r
, FALSE
);
772 static void win_end_draw(void *handle
)
774 frontend
*fe
= (frontend
*)handle
;
775 assert(fe
->drawstatus
== DRAWING
);
776 SelectObject(fe
->hdc
, fe
->prevbm
);
779 DeleteObject(fe
->clip
);
782 fe
->drawstatus
= NOTHING
;
785 static void win_line_width(void *handle
, float width
)
787 frontend
*fe
= (frontend
*)handle
;
789 assert(fe
->drawstatus
!= DRAWING
);
790 if (fe
->drawstatus
== NOTHING
)
793 fe
->linewidth
= (int)(width
* fe
->printpixelscale
);
796 static void win_line_dotted(void *handle
, int dotted
)
798 frontend
*fe
= (frontend
*)handle
;
800 assert(fe
->drawstatus
!= DRAWING
);
801 if (fe
->drawstatus
== NOTHING
)
804 fe
->linedotted
= dotted
;
807 static void win_begin_doc(void *handle
, int pages
)
809 frontend
*fe
= (frontend
*)handle
;
811 assert(fe
->drawstatus
!= DRAWING
);
812 if (fe
->drawstatus
== NOTHING
)
815 if (StartDoc(fe
->hdc
, &fe
->di
) <= 0) {
816 char *e
= geterrstr();
817 MessageBox(fe
->hwnd
, e
, "Error starting to print",
818 MB_ICONERROR
| MB_OK
);
820 fe
->drawstatus
= NOTHING
;
824 * Push a marker on the font stack so that we won't use the
825 * same fonts for printing and drawing. (This is because
826 * drawing seems to look generally better in bold, but printing
827 * is better not in bold.)
829 fe
->fontstart
= fe
->nfonts
;
832 static void win_begin_page(void *handle
, int number
)
834 frontend
*fe
= (frontend
*)handle
;
836 assert(fe
->drawstatus
!= DRAWING
);
837 if (fe
->drawstatus
== NOTHING
)
840 if (StartPage(fe
->hdc
) <= 0) {
841 char *e
= geterrstr();
842 MessageBox(fe
->hwnd
, e
, "Error starting a page",
843 MB_ICONERROR
| MB_OK
);
845 fe
->drawstatus
= NOTHING
;
849 static void win_begin_puzzle(void *handle
, float xm
, float xc
,
850 float ym
, float yc
, int pw
, int ph
, float wmm
)
852 frontend
*fe
= (frontend
*)handle
;
853 int ppw
, pph
, pox
, poy
;
854 float mmpw
, mmph
, mmox
, mmoy
;
857 assert(fe
->drawstatus
!= DRAWING
);
858 if (fe
->drawstatus
== NOTHING
)
861 ppw
= GetDeviceCaps(fe
->hdc
, HORZRES
);
862 pph
= GetDeviceCaps(fe
->hdc
, VERTRES
);
863 mmpw
= (float)GetDeviceCaps(fe
->hdc
, HORZSIZE
);
864 mmph
= (float)GetDeviceCaps(fe
->hdc
, VERTSIZE
);
867 * Compute the puzzle's position on the logical page.
869 mmox
= xm
* mmpw
+ xc
;
870 mmoy
= ym
* mmph
+ yc
;
873 * Work out what that comes to in pixels.
875 pox
= (int)(mmox
* (float)ppw
/ mmpw
);
876 poy
= (int)(mmoy
* (float)pph
/ mmph
);
879 * And determine the scale.
881 * I need a scale such that the maximum puzzle-coordinate
882 * extent of the rectangle (pw * scale) is equal to the pixel
883 * equivalent of the puzzle's millimetre width (wmm * ppw /
886 scale
= (wmm
* ppw
) / (mmpw
* pw
);
889 * Now store pox, poy and scale for use in the main drawing
892 fe
->printoffsetx
= pox
;
893 fe
->printoffsety
= poy
;
894 fe
->printpixelscale
= scale
;
897 fe
->linedotted
= FALSE
;
900 static void win_end_puzzle(void *handle
)
902 /* Nothing needs to be done here. */
905 static void win_end_page(void *handle
, int number
)
907 frontend
*fe
= (frontend
*)handle
;
909 assert(fe
->drawstatus
!= DRAWING
);
911 if (fe
->drawstatus
== NOTHING
)
914 if (EndPage(fe
->hdc
) <= 0) {
915 char *e
= geterrstr();
916 MessageBox(fe
->hwnd
, e
, "Error finishing a page",
917 MB_ICONERROR
| MB_OK
);
919 fe
->drawstatus
= NOTHING
;
923 static void win_end_doc(void *handle
)
925 frontend
*fe
= (frontend
*)handle
;
927 assert(fe
->drawstatus
!= DRAWING
);
930 * Free all the fonts created since we began printing.
932 while (fe
->nfonts
> fe
->fontstart
) {
934 DeleteObject(fe
->fonts
[fe
->nfonts
].font
);
939 * The MSDN web site sample code doesn't bother to call EndDoc
940 * if an error occurs half way through printing. I expect doing
941 * so would cause the erroneous document to actually be
942 * printed, or something equally undesirable.
944 if (fe
->drawstatus
== NOTHING
)
947 if (EndDoc(fe
->hdc
) <= 0) {
948 char *e
= geterrstr();
949 MessageBox(fe
->hwnd
, e
, "Error finishing printing",
950 MB_ICONERROR
| MB_OK
);
952 fe
->drawstatus
= NOTHING
;
956 char *win_text_fallback(void *handle
, const char *const *strings
, int nstrings
)
959 * We assume Windows can cope with any UTF-8 likely to be
960 * emitted by a puzzle.
962 return dupstr(strings
[0]);
965 const struct drawing_api win_drawing
= {
992 void print(frontend
*fe
)
998 midend
*nme
= NULL
; /* non-interactive midend for bulk puzzle generation */
1003 * Create our document structure and fill it up with puzzles.
1005 doc
= document_new(fe
->printw
, fe
->printh
, fe
->printscale
/ 100.0F
);
1006 for (i
= 0; i
< fe
->printcount
; i
++) {
1007 if (i
== 0 && fe
->printcurr
) {
1008 err
= midend_print_puzzle(fe
->me
, doc
, fe
->printsolns
);
1011 game_params
*params
;
1013 nme
= midend_new(NULL
, fe
->game
, NULL
, NULL
);
1016 * Set the non-interactive mid-end to have the same
1017 * parameters as the standard one.
1019 params
= midend_get_params(fe
->me
);
1020 midend_set_params(nme
, params
);
1021 fe
->game
->free_params(params
);
1024 midend_new_game(nme
);
1025 err
= midend_print_puzzle(nme
, doc
, fe
->printsolns
);
1034 MessageBox(fe
->hwnd
, err
, "Error preparing puzzles for printing",
1035 MB_ICONERROR
| MB_OK
);
1040 memset(&pd
, 0, sizeof(pd
));
1041 pd
.lStructSize
= sizeof(pd
);
1042 pd
.hwndOwner
= fe
->hwnd
;
1044 pd
.hDevNames
= NULL
;
1045 pd
.Flags
= PD_USEDEVMODECOPIESANDCOLLATE
| PD_RETURNDC
|
1046 PD_NOPAGENUMS
| PD_NOSELECTION
;
1048 pd
.nFromPage
= pd
.nToPage
= 0xFFFF;
1049 pd
.nMinPage
= pd
.nMaxPage
= 1;
1051 if (!PrintDlg(&pd
)) {
1057 * Now pd.hDC is a device context for the printer.
1061 * FIXME: IWBNI we put up an Abort box here.
1064 memset(&fe
->di
, 0, sizeof(fe
->di
));
1065 fe
->di
.cbSize
= sizeof(fe
->di
);
1066 sprintf(doctitle
, "Printed puzzles from %s (from Simon Tatham's"
1067 " Portable Puzzle Collection)", fe
->game
->name
);
1068 fe
->di
.lpszDocName
= doctitle
;
1069 fe
->di
.lpszOutput
= NULL
;
1070 fe
->di
.lpszDatatype
= NULL
;
1073 fe
->drawstatus
= PRINTING
;
1076 fe
->dr
= drawing_new(&win_drawing
, NULL
, fe
);
1077 document_print(doc
, fe
->dr
);
1078 drawing_free(fe
->dr
);
1081 fe
->drawstatus
= NOTHING
;
1088 void deactivate_timer(frontend
*fe
)
1091 return; /* for non-interactive midend */
1092 if (fe
->hwnd
) KillTimer(fe
->hwnd
, fe
->timer
);
1096 void activate_timer(frontend
*fe
)
1099 return; /* for non-interactive midend */
1101 fe
->timer
= SetTimer(fe
->hwnd
, 1, 20, NULL
);
1102 fe
->timer_last_tickcount
= GetTickCount();
1106 void write_clip(HWND hwnd
, char *data
)
1114 * Windows expects CRLF in the clipboard, so we must convert
1115 * any \n that has come out of the puzzle backend.
1118 for (i
= 0; data
[i
]; i
++) {
1119 if (data
[i
] == '\n')
1123 data2
= snewn(len
+1, char);
1125 for (i
= 0; data
[i
]; i
++) {
1126 if (data
[i
] == '\n')
1128 data2
[j
++] = data
[i
];
1133 clipdata
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, len
+ 1);
1138 lock
= GlobalLock(clipdata
);
1140 GlobalFree(clipdata
);
1144 memcpy(lock
, data2
, len
);
1145 ((unsigned char *) lock
)[len
] = 0;
1146 GlobalUnlock(clipdata
);
1148 if (OpenClipboard(hwnd
)) {
1150 SetClipboardData(CF_TEXT
, clipdata
);
1153 GlobalFree(clipdata
);
1159 * Set up Help and see if we can find a help file.
1161 static void init_help(void)
1164 char b
[2048], *p
, *q
, *r
;
1168 * Find the executable file path, so we can look alongside
1169 * it for help files. Trim the filename off the end.
1171 GetModuleFileName(NULL
, b
, sizeof(b
) - 1);
1173 p
= strrchr(b
, '\\');
1174 if (p
&& p
>= r
) r
= p
+1;
1175 q
= strrchr(b
, ':');
1176 if (q
&& q
>= r
) r
= q
+1;
1180 * Try HTML Help first.
1182 strcpy(r
, CHM_FILE_NAME
);
1183 if ( (fp
= fopen(b
, "r")) != NULL
) {
1187 * We have a .CHM. See if we can use it.
1189 hh_dll
= LoadLibrary("hhctrl.ocx");
1191 htmlhelp
= (htmlhelp_t
)GetProcAddress(hh_dll
, "HtmlHelpA");
1193 FreeLibrary(hh_dll
);
1196 help_path
= dupstr(b
);
1201 #endif /* NO_HTMLHELP */
1204 * Now try old-style .HLP.
1206 strcpy(r
, HELP_FILE_NAME
);
1207 if ( (fp
= fopen(b
, "r")) != NULL
) {
1210 help_path
= dupstr(b
);
1214 * See if there's a .CNT file alongside it.
1216 strcpy(r
, HELP_CNT_NAME
);
1217 if ( (fp
= fopen(b
, "r")) != NULL
) {
1219 help_has_contents
= TRUE
;
1221 help_has_contents
= FALSE
;
1226 help_type
= NONE
; /* didn't find any */
1235 static void start_help(frontend
*fe
, const char *topic
)
1240 switch (help_type
) {
1244 str
= snewn(10+strlen(topic
), char);
1245 sprintf(str
, "JI(`',`%s')", topic
);
1247 } else if (help_has_contents
) {
1250 cmd
= HELP_CONTENTS
;
1252 WinHelp(fe
->hwnd
, help_path
, cmd
, (DWORD
)str
);
1253 fe
->help_running
= TRUE
;
1260 str
= snewn(20 + strlen(topic
) + strlen(help_path
), char);
1261 sprintf(str
, "%s::/%s.html>main", help_path
, topic
);
1263 str
= dupstr(help_path
);
1265 htmlhelp(fe
->hwnd
, str
, HH_DISPLAY_TOPIC
, 0);
1266 fe
->help_running
= TRUE
;
1268 #endif /* NO_HTMLHELP */
1270 assert(!"This shouldn't happen");
1278 * Stop Help on window cleanup.
1280 static void stop_help(frontend
*fe
)
1282 if (fe
->help_running
) {
1283 switch (help_type
) {
1285 WinHelp(fe
->hwnd
, help_path
, HELP_QUIT
, 0);
1290 htmlhelp(NULL
, NULL
, HH_CLOSE_ALL
, 0);
1292 #endif /* NO_HTMLHELP */
1294 assert(!"This shouldn't happen");
1297 fe
->help_running
= FALSE
;
1304 * Terminate Help on process exit.
1306 static void cleanup_help(void)
1308 /* Nothing to do currently.
1309 * (If we were running HTML Help single-threaded, this is where we'd
1310 * call HH_UNINITIALIZE.) */
1313 static int get_statusbar_height(frontend
*fe
)
1316 if (fe
->statusbar
) {
1318 GetWindowRect(fe
->statusbar
, &sr
);
1319 sy
= sr
.bottom
- sr
.top
;
1326 static void adjust_statusbar(frontend
*fe
, RECT
*r
)
1330 if (!fe
->statusbar
) return;
1332 sy
= get_statusbar_height(fe
);
1334 SetWindowPos(fe
->statusbar
, NULL
, 0, r
->bottom
-r
->top
-sy
, r
->right
-r
->left
,
1339 static void get_menu_size(HWND wh
, RECT
*r
)
1341 HMENU bar
= GetMenu(wh
);
1345 SetRect(r
, 0, 0, 0, 0);
1346 for (i
= 0; i
< GetMenuItemCount(bar
); i
++) {
1347 GetMenuItemRect(wh
, bar
, i
, &rect
);
1348 UnionRect(r
, r
, &rect
);
1353 * Given a proposed new puzzle size (cx,cy), work out the actual
1354 * puzzle size that would be (px,py) and the window size including
1355 * furniture (wx,wy).
1358 static int check_window_resize(frontend
*fe
, int cx
, int cy
,
1363 int x
, y
, sy
= get_statusbar_height(fe
), changed
= 0;
1365 /* disallow making window thinner than menu bar */
1366 x
= max(cx
, fe
->xmin
);
1367 y
= max(cy
- sy
, fe
->ymin
);
1370 * See if we actually got the window size we wanted, and adjust
1371 * the puzzle size if not.
1373 midend_size(fe
->me
, &x
, &y
, TRUE
);
1374 if (x
!= cx
|| y
!= cy
) {
1376 * Resize the window, now we know what size we _really_
1382 AdjustWindowRectEx(&r
, WINFLAGS
, TRUE
, 0);
1383 *wx
= r
.right
- r
.left
;
1384 *wy
= r
.bottom
- r
.top
;
1392 (float)midend_tilesize(fe
->me
) / (float)fe
->game
->preferred_tilesize
;
1398 * Given the current window size, make sure it's sane for the
1399 * current puzzle and resize if necessary.
1402 static void check_window_size(frontend
*fe
, int *px
, int *py
)
1407 GetClientRect(fe
->hwnd
, &r
);
1408 cx
= r
.right
- r
.left
;
1409 cy
= r
.bottom
- r
.top
;
1411 if (check_window_resize(fe
, cx
, cy
, px
, py
, &wx
, &wy
)) {
1413 SetWindowPos(fe
->hwnd
, NULL
, 0, 0, wx
, wy
,
1414 SWP_NOMOVE
| SWP_NOZORDER
);
1419 GetClientRect(fe
->hwnd
, &r
);
1420 adjust_statusbar(fe
, &r
);
1423 static void get_max_puzzle_size(frontend
*fe
, int *x
, int *y
)
1427 if (SystemParametersInfo(SPI_GETWORKAREA
, 0, &sr
, FALSE
)) {
1428 *x
= sr
.right
- sr
.left
;
1429 *y
= sr
.bottom
- sr
.top
;
1434 AdjustWindowRectEx(&r
, WINFLAGS
, TRUE
, 0);
1435 *x
-= r
.right
- r
.left
- 100;
1436 *y
-= r
.bottom
- r
.top
- 100;
1441 if (fe
->statusbar
!= NULL
) {
1442 GetWindowRect(fe
->statusbar
, &sr
);
1443 *y
-= sr
.bottom
- sr
.top
;
1448 /* Toolbar buttons on the numeric pad */
1449 static TBBUTTON tbNumpadButtons
[] =
1451 {0, IDM_KEYEMUL
+ '1', TBSTATE_ENABLED
, TBSTYLE_BUTTON
, 0, -1},
1452 {1, IDM_KEYEMUL
+ '2', TBSTATE_ENABLED
, TBSTYLE_BUTTON
, 0, -1},
1453 {2, IDM_KEYEMUL
+ '3', TBSTATE_ENABLED
, TBSTYLE_BUTTON
, 0, -1},
1454 {3, IDM_KEYEMUL
+ '4', TBSTATE_ENABLED
, TBSTYLE_BUTTON
, 0, -1},
1455 {4, IDM_KEYEMUL
+ '5', TBSTATE_ENABLED
, TBSTYLE_BUTTON
, 0, -1},
1456 {5, IDM_KEYEMUL
+ '6', TBSTATE_ENABLED
, TBSTYLE_BUTTON
, 0, -1},
1457 {6, IDM_KEYEMUL
+ '7', TBSTATE_ENABLED
, TBSTYLE_BUTTON
, 0, -1},
1458 {7, IDM_KEYEMUL
+ '8', TBSTATE_ENABLED
, TBSTYLE_BUTTON
, 0, -1},
1459 {8, IDM_KEYEMUL
+ '9', TBSTATE_ENABLED
, TBSTYLE_BUTTON
, 0, -1},
1460 {9, IDM_KEYEMUL
+ ' ', TBSTATE_ENABLED
, TBSTYLE_BUTTON
, 0, -1}
1465 * Allocate a new frontend structure and create its main window.
1467 static frontend
*frontend_new(HINSTANCE inst
)
1470 const char *nogame
= "Puzzles (no game selected)";
1472 fe
= snew(frontend
);
1482 fe
->help_running
= FALSE
;
1484 fe
->drawstatus
= NOTHING
;
1489 fe
->nfonts
= fe
->fontsize
= 0;
1495 fe
->puzz_scale
= 1.0;
1498 MultiByteToWideChar (CP_ACP
, 0, nogame
, -1, wGameName
, 256);
1499 fe
->hwnd
= CreateWindowEx(0, wClassName
, wGameName
,
1501 CW_USEDEFAULT
, CW_USEDEFAULT
,
1502 CW_USEDEFAULT
, CW_USEDEFAULT
,
1503 NULL
, NULL
, inst
, NULL
);
1507 RECT rc
, rcBar
, rcTB
, rcClient
;
1509 memset (&mbi
, 0, sizeof(SHMENUBARINFO
));
1510 mbi
.cbSize
= sizeof(SHMENUBARINFO
);
1511 mbi
.hwndParent
= fe
->hwnd
;
1512 mbi
.nToolBarId
= IDR_MENUBAR1
;
1513 mbi
.hInstRes
= inst
;
1515 SHCreateMenuBar(&mbi
);
1517 GetWindowRect(fe
->hwnd
, &rc
);
1518 GetWindowRect(mbi
.hwndMB
, &rcBar
);
1519 rc
.bottom
-= rcBar
.bottom
- rcBar
.top
;
1520 MoveWindow(fe
->hwnd
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
, FALSE
);
1525 fe
->hwnd
= CreateWindowEx(0, CLASSNAME
, nogame
,
1526 WS_OVERLAPPEDWINDOW
&~
1528 CW_USEDEFAULT
, CW_USEDEFAULT
,
1529 CW_USEDEFAULT
, CW_USEDEFAULT
,
1530 NULL
, NULL
, inst
, NULL
);
1532 DWORD lerr
= GetLastError();
1533 printf("no window: 0x%x\n", lerr
);
1537 fe
->gamemenu
= NULL
;
1540 fe
->statusbar
= NULL
;
1543 SetWindowLong(fe
->hwnd
, GWL_USERDATA
, (LONG
)fe
);
1548 static void savefile_write(void *wctx
, void *buf
, int len
)
1550 FILE *fp
= (FILE *)wctx
;
1551 fwrite(buf
, 1, len
, fp
);
1554 static int savefile_read(void *wctx
, void *buf
, int len
)
1556 FILE *fp
= (FILE *)wctx
;
1559 ret
= fread(buf
, 1, len
, fp
);
1560 return (ret
== len
);
1564 * Create an appropriate midend structure to go in a puzzle window,
1565 * given a game type and/or a command-line argument.
1567 * 'arg' can be either a game ID string (descriptive, random, or a
1568 * plain set of parameters) or the filename of a save file. The two
1569 * boolean flag arguments indicate which possibilities are
1572 static midend
*midend_for_new_game(frontend
*fe
, const game
*cgame
,
1573 char *arg
, int maybe_game_id
,
1574 int maybe_save_file
, char **error
)
1579 if (me
) midend_free(me
);
1580 me
= midend_new(fe
, cgame
, &win_drawing
, fe
);
1581 midend_new_game(me
);
1584 char *err_param
, *err_load
;
1587 * See if arg is a valid filename of a save game file.
1590 if (maybe_save_file
&& (fp
= fopen(arg
, "r")) != NULL
) {
1591 const game
*loadgame
;
1595 * Find out what kind of game is stored in the save
1596 * file; if we're going to end up loading that, it
1597 * will have to override our caller's judgment as to
1598 * what game to initialise our midend with.
1601 err_load
= identify_game(&id_name
, savefile_read
, fp
);
1604 for (i
= 0; i
< gamecount
; i
++)
1605 if (!strcmp(id_name
, gamelist
[i
]->name
))
1607 if (i
== gamecount
) {
1608 err_load
= "Save file is for a game not supported by"
1611 loadgame
= gamelist
[i
];
1612 rewind(fp
); /* go back to the start for actual load */
1619 if (me
) midend_free(me
);
1620 me
= midend_new(fe
, loadgame
, &win_drawing
, fe
);
1621 err_load
= midend_deserialise(me
, savefile_read
, fp
);
1624 err_load
= "Unable to open file";
1627 if (maybe_game_id
&& (!maybe_save_file
|| err_load
)) {
1629 * See if arg is a game description.
1631 if (me
) midend_free(me
);
1632 me
= midend_new(fe
, cgame
, &win_drawing
, fe
);
1633 err_param
= midend_game_id(me
, arg
);
1635 midend_new_game(me
);
1637 if (maybe_save_file
) {
1638 *error
= snewn(256 + strlen(arg
) + strlen(err_param
) +
1639 strlen(err_load
), char);
1640 sprintf(*error
, "Supplied argument \"%s\" is neither a"
1641 " game ID (%s) nor a save file (%s)",
1642 arg
, err_param
, err_load
);
1644 *error
= dupstr(err_param
);
1650 } else if (err_load
) {
1651 *error
= dupstr(err_load
);
1662 * Populate a frontend structure with a new midend structure, and
1663 * create any window furniture that it needs.
1665 * Previously-allocated memory and window furniture will be freed by
1669 static int fe_set_midend(frontend
*fe
, midend
*me
)
1674 if (fe
->me
) midend_free(fe
->me
);
1676 fe
->game
= midend_which_game(fe
->me
);
1682 colours
= midend_colours(fe
->me
, &ncolours
);
1684 if (fe
->colours
) sfree(fe
->colours
);
1685 if (fe
->brushes
) sfree(fe
->brushes
);
1686 if (fe
->pens
) sfree(fe
->pens
);
1688 fe
->colours
= snewn(ncolours
, COLORREF
);
1689 fe
->brushes
= snewn(ncolours
, HBRUSH
);
1690 fe
->pens
= snewn(ncolours
, HPEN
);
1692 for (i
= 0; i
< ncolours
; i
++) {
1693 fe
->colours
[i
] = RGB(255 * colours
[i
*3+0],
1694 255 * colours
[i
*3+1],
1695 255 * colours
[i
*3+2]);
1696 fe
->brushes
[i
] = CreateSolidBrush(fe
->colours
[i
]);
1697 fe
->pens
[i
] = CreatePen(PS_SOLID
, 1, fe
->colours
[i
]);
1703 DestroyWindow(fe
->statusbar
);
1704 if (midend_wants_statusbar(fe
->me
)) {
1705 fe
->statusbar
= CreateWindowEx(0, STATUSCLASSNAME
, TEXT("ooh"),
1706 WS_CHILD
| WS_VISIBLE
,
1707 0, 0, 0, 0, /* status bar does these */
1708 NULL
, NULL
, fe
->inst
, NULL
);
1710 fe
->statusbar
= NULL
;
1712 get_max_puzzle_size(fe
, &x
, &y
);
1713 midend_size(fe
->me
, &x
, &y
, FALSE
);
1718 AdjustWindowRectEx(&r
, WINFLAGS
, TRUE
, 0);
1722 DestroyWindow(fe
->numpad
);
1723 if (fe
->game
->flags
& REQUIRE_NUMPAD
)
1725 fe
->numpad
= CreateToolbarEx (fe
->hwnd
,
1726 WS_VISIBLE
| WS_CHILD
| CCS_NOPARENTALIGN
| TBSTYLE_FLAT
,
1727 0, 10, fe
->inst
, IDR_PADTOOLBAR
,
1728 tbNumpadButtons
, sizeof (tbNumpadButtons
) / sizeof (TBBUTTON
),
1729 0, 0, 14, 15, sizeof (TBBUTTON
));
1730 GetWindowRect(fe
->numpad
, &rcTB
);
1731 GetClientRect(fe
->hwnd
, &rcClient
);
1732 MoveWindow(fe
->numpad
,
1734 rcClient
.bottom
- (rcTB
.bottom
- rcTB
.top
) - 1,
1736 rcTB
.bottom
- rcTB
.top
,
1738 SendMessage(fe
->numpad
, TB_SETINDENT
, (rcClient
.right
- (10 * 21)) / 2, 0);
1743 MultiByteToWideChar (CP_ACP
, 0, fe
->game
->name
, -1, wGameName
, 256);
1744 SetWindowText(fe
->hwnd
, wGameName
);
1746 SetWindowText(fe
->hwnd
, fe
->game
->name
);
1750 DestroyWindow(fe
->statusbar
);
1751 if (midend_wants_statusbar(fe
->me
)) {
1753 fe
->statusbar
= CreateWindowEx(0, STATUSCLASSNAME
, TEXT("ooh"),
1754 WS_CHILD
| WS_VISIBLE
,
1755 0, 0, 0, 0, /* status bar does these */
1756 fe
->hwnd
, NULL
, fe
->inst
, NULL
);
1758 /* Flat status bar looks better on the Pocket PC */
1759 SendMessage(fe
->statusbar
, SB_SIMPLE
, (WPARAM
) TRUE
, 0);
1760 SendMessage(fe
->statusbar
, SB_SETTEXT
,
1761 (WPARAM
) 255 | SBT_NOBORDERS
,
1766 * Now resize the window to take account of the status bar.
1768 GetWindowRect(fe
->statusbar
, &sr
);
1769 GetWindowRect(fe
->hwnd
, &r
);
1771 SetWindowPos(fe
->hwnd
, NULL
, 0, 0, r
.right
- r
.left
,
1772 r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
1773 SWP_NOMOVE
| SWP_NOZORDER
);
1776 fe
->statusbar
= NULL
;
1780 HMENU oldmenu
= GetMenu(fe
->hwnd
);
1783 HMENU bar
= CreateMenu();
1784 HMENU menu
= CreateMenu();
1787 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)menu
, "&Game");
1789 HMENU menu
= SHGetSubMenu(SHFindMenuBar(fe
->hwnd
), ID_GAME
);
1790 DeleteMenu(menu
, 0, MF_BYPOSITION
);
1792 fe
->gamemenu
= menu
;
1793 AppendMenu(menu
, MF_ENABLED
, IDM_NEW
, TEXT("&New"));
1794 AppendMenu(menu
, MF_ENABLED
, IDM_RESTART
, TEXT("&Restart"));
1796 /* ...here I run out of sensible accelerator characters. */
1797 AppendMenu(menu
, MF_ENABLED
, IDM_DESC
, TEXT("Speci&fic..."));
1798 AppendMenu(menu
, MF_ENABLED
, IDM_SEED
, TEXT("Rando&m Seed..."));
1803 if ((fe
->npresets
= midend_num_presets(fe
->me
)) > 0 ||
1804 fe
->game
->can_configure
) {
1807 HMENU sub
= CreateMenu();
1809 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)sub
, "&Type");
1811 HMENU sub
= SHGetSubMenu(SHFindMenuBar(fe
->hwnd
), ID_TYPE
);
1812 DeleteMenu(sub
, 0, MF_BYPOSITION
);
1814 fe
->presets
= snewn(fe
->npresets
, game_params
*);
1816 for (i
= 0; i
< fe
->npresets
; i
++) {
1822 midend_fetch_preset(fe
->me
, i
, &name
, &fe
->presets
[i
]);
1825 * FIXME: we ought to go through and do something
1826 * with ampersands here.
1830 AppendMenu(sub
, MF_ENABLED
, IDM_PRESETS
+ 0x10 * i
, name
);
1832 MultiByteToWideChar (CP_ACP
, 0, name
, -1, wName
, 255);
1833 AppendMenu(sub
, MF_ENABLED
, IDM_PRESETS
+ 0x10 * i
, wName
);
1836 if (fe
->game
->can_configure
) {
1837 AppendMenu(sub
, MF_ENABLED
, IDM_CONFIG
, TEXT("&Custom..."));
1842 fe
->typemenu
= INVALID_HANDLE_VALUE
;
1848 #error Windows CE does not support COMBINED build.
1851 HMENU games
= CreateMenu();
1854 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
1855 AppendMenu(menu
, MF_ENABLED
|MF_POPUP
, (UINT
)games
, "&Other");
1856 for (i
= 0; i
< gamecount
; i
++) {
1857 if (strcmp(gamelist
[i
]->name
, fe
->game
->name
) != 0) {
1858 /* only include those games that aren't the same as the
1859 * game we're currently playing. */
1860 AppendMenu(games
, MF_ENABLED
, IDM_GAMES
+ i
, gamelist
[i
]->name
);
1866 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
1868 AppendMenu(menu
, MF_ENABLED
, IDM_LOAD
, TEXT("&Load..."));
1869 AppendMenu(menu
, MF_ENABLED
, IDM_SAVE
, TEXT("&Save..."));
1870 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
1871 if (fe
->game
->can_print
) {
1872 AppendMenu(menu
, MF_ENABLED
, IDM_PRINT
, TEXT("&Print..."));
1873 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
1876 AppendMenu(menu
, MF_ENABLED
, IDM_UNDO
, TEXT("Undo"));
1877 AppendMenu(menu
, MF_ENABLED
, IDM_REDO
, TEXT("Redo"));
1879 if (fe
->game
->can_format_as_text_ever
) {
1880 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
1881 AppendMenu(menu
, MF_ENABLED
, IDM_COPY
, TEXT("&Copy"));
1884 if (fe
->game
->can_solve
) {
1885 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
1886 AppendMenu(menu
, MF_ENABLED
, IDM_SOLVE
, TEXT("Sol&ve"));
1888 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
1890 AppendMenu(menu
, MF_ENABLED
, IDM_QUIT
, TEXT("E&xit"));
1891 menu
= CreateMenu();
1892 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)menu
, TEXT("&Help"));
1894 AppendMenu(menu
, MF_ENABLED
, IDM_ABOUT
, TEXT("&About"));
1896 if (help_type
!= NONE
) {
1898 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
1899 AppendMenu(menu
, MF_ENABLED
, IDM_HELPC
, TEXT("&Contents"));
1900 assert(fe
->game
->name
);
1901 item
= snewn(10+strlen(fe
->game
->name
), char); /*ick*/
1902 sprintf(item
, "&Help on %s", fe
->game
->name
);
1903 AppendMenu(menu
, MF_ENABLED
, IDM_GAMEHELP
, item
);
1906 DestroyMenu(oldmenu
);
1907 SetMenu(fe
->hwnd
, bar
);
1908 get_menu_size(fe
->hwnd
, &menusize
);
1909 fe
->xmin
= (menusize
.right
- menusize
.left
) + 25;
1913 if (fe
->bitmap
) DeleteObject(fe
->bitmap
);
1915 new_game_size(fe
, fe
->puzz_scale
); /* initialises fe->bitmap */
1920 static void show_window(frontend
*fe
)
1922 ShowWindow(fe
->hwnd
, SW_SHOWNORMAL
);
1923 SetForegroundWindow(fe
->hwnd
);
1925 update_type_menu_tick(fe
);
1926 update_copy_menu_greying(fe
);
1928 midend_redraw(fe
->me
);
1932 static HFONT
dialog_title_font()
1934 static HFONT hf
= NULL
;
1940 memset (&lf
, 0, sizeof(LOGFONT
));
1941 lf
.lfHeight
= -11; /* - ((8 * GetDeviceCaps(hdc, LOGPIXELSY)) / 72) */
1942 lf
.lfWeight
= FW_BOLD
;
1943 wcscpy(lf
.lfFaceName
, TEXT("Tahoma"));
1945 return hf
= CreateFontIndirect(&lf
);
1948 static void make_dialog_full_screen(HWND hwnd
)
1950 SHINITDLGINFO shidi
;
1952 /* Make dialog full screen */
1953 shidi
.dwMask
= SHIDIM_FLAGS
;
1954 shidi
.dwFlags
= SHIDIF_DONEBUTTON
| SHIDIF_SIZEDLGFULLSCREEN
|
1957 SHInitDialog(&shidi
);
1961 static int CALLBACK
AboutDlgProc(HWND hwnd
, UINT msg
,
1962 WPARAM wParam
, LPARAM lParam
)
1964 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
1972 make_dialog_full_screen(hwnd
);
1974 sprintf(title
, "About %.250s", fe
->game
->name
);
1975 SetDlgItemTextA(hwnd
, IDC_ABOUT_CAPTION
, title
);
1977 SendDlgItemMessage(hwnd
, IDC_ABOUT_CAPTION
, WM_SETFONT
,
1978 (WPARAM
) dialog_title_font(), 0);
1980 SetDlgItemTextA(hwnd
, IDC_ABOUT_GAME
, fe
->game
->name
);
1981 SetDlgItemTextA(hwnd
, IDC_ABOUT_VERSION
, ver
);
1987 if (LOWORD(wParam
) == IDOK
)
2008 * Wrappers on midend_{get,set}_config, which extend the CFG_*
2009 * enumeration to add CFG_PRINT.
2011 static config_item
*frontend_get_config(frontend
*fe
, int which
,
2014 if (which
< CFG_FRONTEND_SPECIFIC
) {
2015 return midend_get_config(fe
->me
, which
, wintitle
);
2016 } else if (which
== CFG_PRINT
) {
2020 *wintitle
= snewn(40 + strlen(fe
->game
->name
), char);
2021 sprintf(*wintitle
, "%s print setup", fe
->game
->name
);
2023 ret
= snewn(8, config_item
);
2027 ret
[i
].name
= "Number of puzzles to print";
2028 ret
[i
].type
= C_STRING
;
2029 ret
[i
].sval
= dupstr("1");
2033 ret
[i
].name
= "Number of puzzles across the page";
2034 ret
[i
].type
= C_STRING
;
2035 ret
[i
].sval
= dupstr("1");
2039 ret
[i
].name
= "Number of puzzles down the page";
2040 ret
[i
].type
= C_STRING
;
2041 ret
[i
].sval
= dupstr("1");
2045 ret
[i
].name
= "Percentage of standard size";
2046 ret
[i
].type
= C_STRING
;
2047 ret
[i
].sval
= dupstr("100.0");
2051 ret
[i
].name
= "Include currently shown puzzle";
2052 ret
[i
].type
= C_BOOLEAN
;
2057 ret
[i
].name
= "Print solutions";
2058 ret
[i
].type
= C_BOOLEAN
;
2060 ret
[i
].ival
= FALSE
;
2063 if (fe
->game
->can_print_in_colour
) {
2064 ret
[i
].name
= "Print in colour";
2065 ret
[i
].type
= C_BOOLEAN
;
2067 ret
[i
].ival
= FALSE
;
2072 ret
[i
].type
= C_END
;
2079 assert(!"We should never get here");
2084 static char *frontend_set_config(frontend
*fe
, int which
, config_item
*cfg
)
2086 if (which
< CFG_FRONTEND_SPECIFIC
) {
2087 return midend_set_config(fe
->me
, which
, cfg
);
2088 } else if (which
== CFG_PRINT
) {
2089 if ((fe
->printcount
= atoi(cfg
[0].sval
)) <= 0)
2090 return "Number of puzzles to print should be at least one";
2091 if ((fe
->printw
= atoi(cfg
[1].sval
)) <= 0)
2092 return "Number of puzzles across the page should be at least one";
2093 if ((fe
->printh
= atoi(cfg
[2].sval
)) <= 0)
2094 return "Number of puzzles down the page should be at least one";
2095 if ((fe
->printscale
= (float)atof(cfg
[3].sval
)) <= 0)
2096 return "Print size should be positive";
2097 fe
->printcurr
= cfg
[4].ival
;
2098 fe
->printsolns
= cfg
[5].ival
;
2099 fe
->printcolour
= fe
->game
->can_print_in_colour
&& cfg
[6].ival
;
2102 assert(!"We should never get here");
2103 return "Internal error";
2108 /* Separate version of mkctrl function for the Pocket PC. */
2109 /* Control coordinates should be specified in dialog units. */
2110 HWND
mkctrl(frontend
*fe
, int x1
, int x2
, int y1
, int y2
,
2111 LPCTSTR wclass
, int wstyle
,
2112 int exstyle
, const char *wtext
, int wid
)
2117 /* Convert dialog units into pixels */
2118 rc
.left
= x1
; rc
.right
= x2
;
2119 rc
.top
= y1
; rc
.bottom
= y2
;
2120 MapDialogRect(fe
->cfgbox
, &rc
);
2122 MultiByteToWideChar (CP_ACP
, 0, wtext
, -1, wwtext
, 256);
2124 return CreateWindowEx(exstyle
, wclass
, wwtext
,
2125 wstyle
| WS_CHILD
| WS_VISIBLE
,
2127 rc
.right
- rc
.left
, rc
.bottom
- rc
.top
,
2128 fe
->cfgbox
, (HMENU
) wid
, fe
->inst
, NULL
);
2131 static void create_config_controls(frontend
* fe
)
2134 int col1l
, col1r
, col2l
, col2r
, y
;
2139 /* Control placement done in dialog units */
2140 col1l
= 4; col1r
= 96; /* Label column */
2141 col2l
= 100; col2r
= 154; /* Input column (edit boxes and combo boxes) */
2144 * Count the controls so we can allocate cfgaux.
2146 for (nctrls
= 0, i
= fe
->cfg
; i
->type
!= C_END
; i
++)
2148 fe
->cfgaux
= snewn(nctrls
, struct cfg_aux
);
2151 y
= 22; /* Leave some room for the dialog title */
2152 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
2156 * Edit box with a label beside it.
2158 mkctrl(fe
, col1l
, col1r
, y
+ 1, y
+ 11,
2159 TEXT("Static"), SS_LEFTNOWORDWRAP
, 0, i
->name
, id
++);
2160 mkctrl(fe
, col2l
, col2r
, y
, y
+ 12,
2161 TEXT("EDIT"), WS_BORDER
| WS_TABSTOP
| ES_AUTOHSCROLL
,
2162 0, "", (j
->ctlid
= id
++));
2163 SetDlgItemTextA(fe
->cfgbox
, j
->ctlid
, i
->sval
);
2170 mkctrl(fe
, col1l
, col2r
, y
+ 1, y
+ 11, TEXT("BUTTON"),
2171 BS_NOTIFY
| BS_AUTOCHECKBOX
| WS_TABSTOP
,
2172 0, i
->name
, (j
->ctlid
= id
++));
2173 CheckDlgButton(fe
->cfgbox
, j
->ctlid
, (i
->ival
!= 0));
2178 * Drop-down list with a label beside it.
2180 mkctrl(fe
, col1l
, col1r
, y
+ 1, y
+ 11,
2181 TEXT("STATIC"), SS_LEFTNOWORDWRAP
, 0, i
->name
, id
++);
2182 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+ 48,
2183 TEXT("COMBOBOX"), WS_BORDER
| WS_TABSTOP
|
2184 CBS_DROPDOWNLIST
| CBS_HASSTRINGS
,
2185 0, "", (j
->ctlid
= id
++));
2187 char c
, *p
, *q
, *str
;
2193 while (*q
&& *q
!= c
) q
++;
2194 str
= snewn(q
-p
+1, char);
2195 strncpy(str
, p
, q
-p
);
2199 MultiByteToWideChar (CP_ACP
, 0, str
, -1, ws
, 50);
2200 SendMessage(ctl
, CB_ADDSTRING
, 0, (LPARAM
)ws
);
2208 SendMessage(ctl
, CB_SETCURSEL
, i
->ival
, 0);
2218 static int CALLBACK
ConfigDlgProc(HWND hwnd
, UINT msg
,
2219 WPARAM wParam
, LPARAM lParam
)
2221 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
2231 fe
= (frontend
*) lParam
;
2232 SetWindowLong(hwnd
, GWL_USERDATA
, lParam
);
2235 fe
->cfg
= frontend_get_config(fe
, fe
->cfg_which
, &title
);
2237 make_dialog_full_screen(hwnd
);
2239 SetDlgItemTextA(hwnd
, IDC_CONFIG_CAPTION
, title
);
2240 SendDlgItemMessage(hwnd
, IDC_CONFIG_CAPTION
, WM_SETFONT
,
2241 (WPARAM
) dialog_title_font(), 0);
2243 create_config_controls(fe
);
2250 * OK and Cancel are special cases.
2252 if ((LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)) {
2253 if (LOWORD(wParam
) == IDOK
) {
2254 char *err
= frontend_set_config(fe
, fe
->cfg_which
, fe
->cfg
);
2257 MessageBox(hwnd
, err
, "Validation error",
2258 MB_ICONERROR
| MB_OK
);
2277 * First find the control whose id this is.
2279 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
2280 if (j
->ctlid
== LOWORD(wParam
))
2283 if (i
->type
== C_END
)
2284 return 0; /* not our problem */
2286 if (i
->type
== C_STRING
&& HIWORD(wParam
) == EN_CHANGE
) {
2289 TCHAR wBuffer
[4096];
2290 GetDlgItemText(fe
->cfgbox
, j
->ctlid
, wBuffer
, 4096);
2291 WideCharToMultiByte(CP_ACP
, 0, wBuffer
, -1, buffer
, 4096, NULL
, NULL
);
2293 GetDlgItemText(fe
->cfgbox
, j
->ctlid
, buffer
, lenof(buffer
));
2295 buffer
[lenof(buffer
)-1] = '\0';
2297 i
->sval
= dupstr(buffer
);
2298 } else if (i
->type
== C_BOOLEAN
&&
2299 (HIWORD(wParam
) == BN_CLICKED
||
2300 HIWORD(wParam
) == BN_DBLCLK
)) {
2301 i
->ival
= IsDlgButtonChecked(fe
->cfgbox
, j
->ctlid
);
2302 } else if (i
->type
== C_CHOICES
&&
2303 HIWORD(wParam
) == CBN_SELCHANGE
) {
2304 i
->ival
= SendDlgItemMessage(fe
->cfgbox
, j
->ctlid
,
2305 CB_GETCURSEL
, 0, 0);
2319 HWND
mkctrl(frontend
*fe
, int x1
, int x2
, int y1
, int y2
,
2320 char *wclass
, int wstyle
,
2321 int exstyle
, const char *wtext
, int wid
)
2324 ret
= CreateWindowEx(exstyle
, wclass
, wtext
,
2325 wstyle
| WS_CHILD
| WS_VISIBLE
, x1
, y1
, x2
-x1
, y2
-y1
,
2326 fe
->cfgbox
, (HMENU
) wid
, fe
->inst
, NULL
);
2327 SendMessage(ret
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, MAKELPARAM(TRUE
, 0));
2332 static void about(frontend
*fe
)
2335 DialogBox(fe
->inst
, MAKEINTRESOURCE(IDD_ABOUT
), fe
->hwnd
, AboutDlgProc
);
2345 int winwidth
, winheight
, y
;
2346 int height
, width
, maxwid
;
2347 const char *strings
[16];
2352 sprintf(titlebuf
, "About %.250s", fe
->game
->name
);
2354 strings
[nstrings
++] = fe
->game
->name
;
2355 strings
[nstrings
++] = "from Simon Tatham's Portable Puzzle Collection";
2356 strings
[nstrings
++] = ver
;
2358 wc
.style
= CS_DBLCLKS
| CS_SAVEBITS
;
2359 wc
.lpfnWndProc
= DefDlgProc
;
2361 wc
.cbWndExtra
= DLGWINDOWEXTRA
+ 8;
2362 wc
.hInstance
= fe
->inst
;
2364 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
2365 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+1);
2366 wc
.lpszMenuName
= NULL
;
2367 wc
.lpszClassName
= "GameAboutBox";
2370 hdc
= GetDC(fe
->hwnd
);
2371 SetMapMode(hdc
, MM_TEXT
);
2373 fe
->dlg_done
= FALSE
;
2375 fe
->cfgfont
= CreateFont(-MulDiv(8, GetDeviceCaps(hdc
, LOGPIXELSY
), 72),
2377 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
2378 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
2383 oldfont
= SelectObject(hdc
, fe
->cfgfont
);
2384 if (GetTextMetrics(hdc
, &tm
)) {
2385 height
= tm
.tmAscent
+ tm
.tmDescent
;
2386 width
= tm
.tmAveCharWidth
;
2388 height
= width
= 30;
2392 * Figure out the layout of the About box by measuring the
2393 * length of each piece of text.
2396 winheight
= height
/2;
2398 for (i
= 0; i
< nstrings
; i
++) {
2399 if (GetTextExtentPoint32(hdc
, strings
[i
], strlen(strings
[i
]), &size
))
2400 lengths
[i
] = size
.cx
;
2402 lengths
[i
] = 0; /* *shrug* */
2403 if (maxwid
< lengths
[i
])
2404 maxwid
= lengths
[i
];
2405 winheight
+= height
* 3 / 2 + (height
/ 2);
2408 winheight
+= height
+ height
* 7 / 4; /* OK button */
2409 winwidth
= maxwid
+ 4*width
;
2411 SelectObject(hdc
, oldfont
);
2412 ReleaseDC(fe
->hwnd
, hdc
);
2415 * Create the dialog, now that we know its size.
2422 r
.bottom
= winheight
;
2424 AdjustWindowRectEx(&r
, (WS_OVERLAPPEDWINDOW
/*|
2425 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
2426 WS_CAPTION | WS_SYSMENU*/) &~
2427 (WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
2431 * Centre the dialog on its parent window.
2435 GetWindowRect(fe
->hwnd
, &r2
);
2436 r
.left
= (r2
.left
+ r2
.right
- r
.right
) / 2;
2437 r
.top
= (r2
.top
+ r2
.bottom
- r
.bottom
) / 2;
2441 fe
->cfgbox
= CreateWindowEx(0, wc
.lpszClassName
, titlebuf
,
2442 DS_MODALFRAME
| WS_POPUP
| WS_VISIBLE
|
2443 WS_CAPTION
| WS_SYSMENU
,
2445 r
.right
-r
.left
, r
.bottom
-r
.top
,
2446 fe
->hwnd
, NULL
, fe
->inst
, NULL
);
2449 SendMessage(fe
->cfgbox
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, FALSE
);
2451 SetWindowLong(fe
->cfgbox
, GWL_USERDATA
, (LONG
)fe
);
2452 SetWindowLong(fe
->cfgbox
, DWL_DLGPROC
, (LONG
)AboutDlgProc
);
2456 for (i
= 0; i
< nstrings
; i
++) {
2457 int border
= width
*2 + (maxwid
- lengths
[i
]) / 2;
2458 mkctrl(fe
, border
, border
+lengths
[i
], y
+height
*1/8, y
+height
*9/8,
2459 "Static", 0, 0, strings
[i
], id
++);
2462 assert(y
< winheight
);
2466 y
+= height
/2; /* extra space before OK */
2467 mkctrl(fe
, width
*2, maxwid
+width
*2, y
, y
+height
*7/4, "BUTTON",
2468 BS_PUSHBUTTON
| WS_TABSTOP
| BS_DEFPUSHBUTTON
, 0,
2471 SendMessage(fe
->cfgbox
, WM_INITDIALOG
, 0, 0);
2473 EnableWindow(fe
->hwnd
, FALSE
);
2474 ShowWindow(fe
->cfgbox
, SW_SHOWNORMAL
);
2475 while ((gm
=GetMessage(&msg
, NULL
, 0, 0)) > 0) {
2476 if (!IsDialogMessage(fe
->cfgbox
, &msg
))
2477 DispatchMessage(&msg
);
2481 EnableWindow(fe
->hwnd
, TRUE
);
2482 SetForegroundWindow(fe
->hwnd
);
2483 DestroyWindow(fe
->cfgbox
);
2484 DeleteObject(fe
->cfgfont
);
2488 static int get_config(frontend
*fe
, int which
)
2491 fe
->cfg_which
= which
;
2493 return DialogBoxParam(fe
->inst
,
2494 MAKEINTRESOURCE(IDD_CONFIG
),
2495 fe
->hwnd
, ConfigDlgProc
,
2509 int winwidth
, winheight
, col1l
, col1r
, col2l
, col2r
, y
;
2510 int height
, width
, maxlabel
, maxcheckbox
;
2512 wc
.style
= CS_DBLCLKS
| CS_SAVEBITS
;
2513 wc
.lpfnWndProc
= DefDlgProc
;
2515 wc
.cbWndExtra
= DLGWINDOWEXTRA
+ 8;
2516 wc
.hInstance
= fe
->inst
;
2518 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
2519 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+1);
2520 wc
.lpszMenuName
= NULL
;
2521 wc
.lpszClassName
= "GameConfigBox";
2524 hdc
= GetDC(fe
->hwnd
);
2525 SetMapMode(hdc
, MM_TEXT
);
2527 fe
->dlg_done
= FALSE
;
2529 fe
->cfgfont
= CreateFont(-MulDiv(8, GetDeviceCaps(hdc
, LOGPIXELSY
), 72),
2531 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
2532 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
2537 oldfont
= SelectObject(hdc
, fe
->cfgfont
);
2538 if (GetTextMetrics(hdc
, &tm
)) {
2539 height
= tm
.tmAscent
+ tm
.tmDescent
;
2540 width
= tm
.tmAveCharWidth
;
2542 height
= width
= 30;
2545 fe
->cfg
= frontend_get_config(fe
, which
, &title
);
2546 fe
->cfg_which
= which
;
2549 * Figure out the layout of the config box by measuring the
2550 * length of each piece of text.
2552 maxlabel
= maxcheckbox
= 0;
2553 winheight
= height
/2;
2555 for (i
= fe
->cfg
; i
->type
!= C_END
; i
++) {
2560 * Both these control types have a label filling only
2561 * the left-hand column of the box.
2563 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
2566 winheight
+= height
* 3 / 2 + (height
/ 2);
2571 * Checkboxes take up the whole of the box width.
2573 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
2574 maxcheckbox
< size
.cx
)
2575 maxcheckbox
= size
.cx
;
2576 winheight
+= height
+ (height
/ 2);
2581 winheight
+= height
+ height
* 7 / 4; /* OK / Cancel buttons */
2584 col1r
= col1l
+ maxlabel
;
2585 col2l
= col1r
+ 2*width
;
2586 col2r
= col2l
+ 30*width
;
2587 if (col2r
< col1l
+2*height
+maxcheckbox
)
2588 col2r
= col1l
+2*height
+maxcheckbox
;
2589 winwidth
= col2r
+ 2*width
;
2591 SelectObject(hdc
, oldfont
);
2592 ReleaseDC(fe
->hwnd
, hdc
);
2595 * Create the dialog, now that we know its size.
2602 r
.bottom
= winheight
;
2604 AdjustWindowRectEx(&r
, (WS_OVERLAPPEDWINDOW
/*|
2605 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
2606 WS_CAPTION | WS_SYSMENU*/) &~
2607 (WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
2611 * Centre the dialog on its parent window.
2615 GetWindowRect(fe
->hwnd
, &r2
);
2616 r
.left
= (r2
.left
+ r2
.right
- r
.right
) / 2;
2617 r
.top
= (r2
.top
+ r2
.bottom
- r
.bottom
) / 2;
2621 fe
->cfgbox
= CreateWindowEx(0, wc
.lpszClassName
, title
,
2622 DS_MODALFRAME
| WS_POPUP
| WS_VISIBLE
|
2623 WS_CAPTION
| WS_SYSMENU
,
2625 r
.right
-r
.left
, r
.bottom
-r
.top
,
2626 fe
->hwnd
, NULL
, fe
->inst
, NULL
);
2630 SendMessage(fe
->cfgbox
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, FALSE
);
2632 SetWindowLong(fe
->cfgbox
, GWL_USERDATA
, (LONG
)fe
);
2633 SetWindowLong(fe
->cfgbox
, DWL_DLGPROC
, (LONG
)ConfigDlgProc
);
2636 * Count the controls so we can allocate cfgaux.
2638 for (nctrls
= 0, i
= fe
->cfg
; i
->type
!= C_END
; i
++)
2640 fe
->cfgaux
= snewn(nctrls
, struct cfg_aux
);
2644 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
2648 * Edit box with a label beside it.
2650 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
2651 "Static", 0, 0, i
->name
, id
++);
2652 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*3/2,
2653 "EDIT", WS_TABSTOP
| ES_AUTOHSCROLL
,
2654 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
2655 SetWindowText(ctl
, i
->sval
);
2663 mkctrl(fe
, col1l
, col2r
, y
, y
+height
, "BUTTON",
2664 BS_NOTIFY
| BS_AUTOCHECKBOX
| WS_TABSTOP
,
2665 0, i
->name
, (j
->ctlid
= id
++));
2666 CheckDlgButton(fe
->cfgbox
, j
->ctlid
, (i
->ival
!= 0));
2672 * Drop-down list with a label beside it.
2674 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
2675 "STATIC", 0, 0, i
->name
, id
++);
2676 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*41/2,
2677 "COMBOBOX", WS_TABSTOP
|
2678 CBS_DROPDOWNLIST
| CBS_HASSTRINGS
,
2679 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
2681 char c
, *p
, *q
, *str
;
2683 SendMessage(ctl
, CB_RESETCONTENT
, 0, 0);
2688 while (*q
&& *q
!= c
) q
++;
2689 str
= snewn(q
-p
+1, char);
2690 strncpy(str
, p
, q
-p
);
2692 SendMessage(ctl
, CB_ADDSTRING
, 0, (LPARAM
)str
);
2699 SendMessage(ctl
, CB_SETCURSEL
, i
->ival
, 0);
2705 assert(y
< winheight
);
2709 y
+= height
/2; /* extra space before OK and Cancel */
2710 mkctrl(fe
, col1l
, (col1l
+col2r
)/2-width
, y
, y
+height
*7/4, "BUTTON",
2711 BS_PUSHBUTTON
| WS_TABSTOP
| BS_DEFPUSHBUTTON
, 0,
2713 mkctrl(fe
, (col1l
+col2r
)/2+width
, col2r
, y
, y
+height
*7/4, "BUTTON",
2714 BS_PUSHBUTTON
| WS_TABSTOP
, 0, "Cancel", IDCANCEL
);
2716 SendMessage(fe
->cfgbox
, WM_INITDIALOG
, 0, 0);
2718 EnableWindow(fe
->hwnd
, FALSE
);
2719 ShowWindow(fe
->cfgbox
, SW_SHOWNORMAL
);
2720 while ((gm
=GetMessage(&msg
, NULL
, 0, 0)) > 0) {
2721 if (!IsDialogMessage(fe
->cfgbox
, &msg
))
2722 DispatchMessage(&msg
);
2726 EnableWindow(fe
->hwnd
, TRUE
);
2727 SetForegroundWindow(fe
->hwnd
);
2728 DestroyWindow(fe
->cfgbox
);
2729 DeleteObject(fe
->cfgfont
);
2734 return (fe
->dlg_done
== 2);
2739 static void calculate_bitmap_position(frontend
*fe
, int x
, int y
)
2741 /* Pocket PC - center the game in the full screen window */
2745 GetClientRect(fe
->hwnd
, &rcClient
);
2746 fe
->bitmapPosition
.left
= (rcClient
.right
- x
) / 2;
2747 yMargin
= rcClient
.bottom
- y
;
2749 if (fe
->numpad
!= NULL
) {
2751 GetWindowRect(fe
->numpad
, &rcPad
);
2752 yMargin
-= rcPad
.bottom
- rcPad
.top
;
2755 if (fe
->statusbar
!= NULL
) {
2757 GetWindowRect(fe
->statusbar
, &rcStatus
);
2758 yMargin
-= rcStatus
.bottom
- rcStatus
.top
;
2761 fe
->bitmapPosition
.top
= yMargin
/ 2;
2763 fe
->bitmapPosition
.right
= fe
->bitmapPosition
.left
+ x
;
2764 fe
->bitmapPosition
.bottom
= fe
->bitmapPosition
.top
+ y
;
2767 static void calculate_bitmap_position(frontend
*fe
, int x
, int y
)
2769 /* Plain Windows - position the game in the upper-left corner */
2770 fe
->bitmapPosition
.left
= 0;
2771 fe
->bitmapPosition
.top
= 0;
2772 fe
->bitmapPosition
.right
= fe
->bitmapPosition
.left
+ x
;
2773 fe
->bitmapPosition
.bottom
= fe
->bitmapPosition
.top
+ y
;
2777 static void new_bitmap(frontend
*fe
, int x
, int y
)
2781 if (fe
->bitmap
) DeleteObject(fe
->bitmap
);
2783 hdc
= GetDC(fe
->hwnd
);
2784 fe
->bitmap
= CreateCompatibleBitmap(hdc
, x
, y
);
2785 calculate_bitmap_position(fe
, x
, y
);
2786 ReleaseDC(fe
->hwnd
, hdc
);
2789 static void new_game_size(frontend
*fe
, float scale
)
2794 get_max_puzzle_size(fe
, &x
, &y
);
2795 midend_size(fe
->me
, &x
, &y
, FALSE
);
2798 x
= (int)((float)x
* fe
->puzz_scale
);
2799 y
= (int)((float)y
* fe
->puzz_scale
);
2800 midend_size(fe
->me
, &x
, &y
, TRUE
);
2802 fe
->ymin
= (fe
->xmin
* y
) / x
;
2807 AdjustWindowRectEx(&r
, WINFLAGS
, TRUE
, 0);
2809 if (fe
->statusbar
!= NULL
) {
2810 GetWindowRect(fe
->statusbar
, &sr
);
2812 sr
.left
= sr
.right
= sr
.top
= sr
.bottom
= 0;
2815 SetWindowPos(fe
->hwnd
, NULL
, 0, 0,
2817 r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
2818 SWP_NOMOVE
| SWP_NOZORDER
);
2821 check_window_size(fe
, &x
, &y
);
2824 if (fe
->statusbar
!= NULL
)
2825 SetWindowPos(fe
->statusbar
, NULL
, 0, y
, x
,
2826 sr
.bottom
- sr
.top
, SWP_NOZORDER
);
2829 new_bitmap(fe
, x
, y
);
2832 InvalidateRect(fe
->hwnd
, NULL
, TRUE
);
2834 midend_redraw(fe
->me
);
2838 * Given a proposed new window rect, work out the resulting
2839 * difference in client size (from current), and use to try
2840 * and resize the puzzle, returning (wx,wy) as the actual
2844 static void adjust_game_size(frontend
*fe
, RECT
*proposed
, int isedge
,
2845 int *wx_r
, int *wy_r
)
2848 int nx
, ny
, xdiff
, ydiff
, wx
, wy
;
2850 /* Work out the current window sizing, and thus the
2851 * difference in size we're asking for. */
2852 GetClientRect(fe
->hwnd
, &cr
);
2854 AdjustWindowRectEx(&wr
, WINFLAGS
, TRUE
, 0);
2856 xdiff
= (proposed
->right
- proposed
->left
) - (wr
.right
- wr
.left
);
2857 ydiff
= (proposed
->bottom
- proposed
->top
) - (wr
.bottom
- wr
.top
);
2860 /* These next four lines work around the fact that midend_size
2861 * is happy to shrink _but not grow_ if you change one dimension
2862 * but not the other. */
2863 if (xdiff
> 0 && ydiff
== 0)
2864 ydiff
= (xdiff
* (wr
.right
- wr
.left
)) / (wr
.bottom
- wr
.top
);
2865 if (xdiff
== 0 && ydiff
> 0)
2866 xdiff
= (ydiff
* (wr
.bottom
- wr
.top
)) / (wr
.right
- wr
.left
);
2869 if (check_window_resize(fe
,
2870 (cr
.right
- cr
.left
) + xdiff
,
2871 (cr
.bottom
- cr
.top
) + ydiff
,
2872 &nx
, &ny
, &wx
, &wy
)) {
2873 new_bitmap(fe
, nx
, ny
);
2874 midend_force_redraw(fe
->me
);
2876 /* reset size to current window size */
2877 wx
= wr
.right
- wr
.left
;
2878 wy
= wr
.bottom
- wr
.top
;
2880 /* Re-fetch rectangle; size limits mean we might not have
2881 * taken it quite to the mouse drag positions. */
2882 GetClientRect(fe
->hwnd
, &cr
);
2883 adjust_statusbar(fe
, &cr
);
2885 *wx_r
= wx
; *wy_r
= wy
;
2888 static void update_type_menu_tick(frontend
*fe
)
2892 if (fe
->typemenu
== INVALID_HANDLE_VALUE
)
2895 total
= GetMenuItemCount(fe
->typemenu
);
2896 n
= midend_which_preset(fe
->me
);
2898 n
= total
- 1; /* "Custom" item */
2900 for (i
= 0; i
< total
; i
++) {
2901 int flag
= (i
== n ? MF_CHECKED
: MF_UNCHECKED
);
2902 CheckMenuItem(fe
->typemenu
, i
, MF_BYPOSITION
| flag
);
2905 DrawMenuBar(fe
->hwnd
);
2908 static void update_copy_menu_greying(frontend
*fe
)
2910 UINT enable
= (midend_can_format_as_text_now(fe
->me
) ?
2911 MF_ENABLED
: MF_GRAYED
);
2912 EnableMenuItem(fe
->gamemenu
, IDM_COPY
, MF_BYCOMMAND
| enable
);
2915 static void new_game_type(frontend
*fe
)
2917 midend_new_game(fe
->me
);
2918 new_game_size(fe
, 1.0);
2919 update_type_menu_tick(fe
);
2920 update_copy_menu_greying(fe
);
2923 static int is_alt_pressed(void)
2926 int r
= GetKeyboardState(keystate
);
2929 if (keystate
[VK_MENU
] & 0x80)
2931 if (keystate
[VK_RMENU
] & 0x80)
2936 static LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
,
2937 WPARAM wParam
, LPARAM lParam
)
2939 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
2944 DestroyWindow(hwnd
);
2948 /* Numeric pad sends WM_COMMAND messages */
2949 if ((wParam
>= IDM_KEYEMUL
) && (wParam
< IDM_KEYEMUL
+ 256))
2951 midend_process_key(fe
->me
, 0, 0, wParam
- IDM_KEYEMUL
);
2954 cmd
= wParam
& ~0xF; /* low 4 bits reserved to Windows */
2957 if (!midend_process_key(fe
->me
, 0, 0, 'n'))
2961 midend_restart_game(fe
->me
);
2964 if (!midend_process_key(fe
->me
, 0, 0, 'u'))
2968 if (!midend_process_key(fe
->me
, 0, 0, '\x12'))
2973 char *text
= midend_text_format(fe
->me
);
2975 write_clip(hwnd
, text
);
2977 MessageBeep(MB_ICONWARNING
);
2983 char *msg
= midend_solve(fe
->me
);
2985 MessageBox(hwnd
, msg
, "Unable to solve",
2986 MB_ICONERROR
| MB_OK
);
2990 if (!midend_process_key(fe
->me
, 0, 0, 'q'))
2994 if (get_config(fe
, CFG_SETTINGS
))
2998 if (get_config(fe
, CFG_SEED
))
3002 if (get_config(fe
, CFG_DESC
))
3006 if (get_config(fe
, CFG_PRINT
))
3016 char filename
[FILENAME_MAX
];
3019 memset(&of
, 0, sizeof(of
));
3020 of
.hwndOwner
= hwnd
;
3021 of
.lpstrFilter
= "All Files (*.*)\0*\0\0\0";
3022 of
.lpstrCustomFilter
= NULL
;
3023 of
.nFilterIndex
= 1;
3024 of
.lpstrFile
= filename
;
3026 of
.nMaxFile
= lenof(filename
);
3027 of
.lpstrFileTitle
= NULL
;
3028 of
.lpstrTitle
= (cmd
== IDM_SAVE ?
3029 "Enter name of game file to save" :
3030 "Enter name of saved game file to load");
3032 #ifdef OPENFILENAME_SIZE_VERSION_400
3033 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
3035 of
.lStructSize
= sizeof(of
);
3037 of
.lpstrInitialDir
= NULL
;
3039 if (cmd
== IDM_SAVE
)
3040 ret
= GetSaveFileName(&of
);
3042 ret
= GetOpenFileName(&of
);
3045 if (cmd
== IDM_SAVE
) {
3048 if ((fp
= fopen(filename
, "r")) != NULL
) {
3049 char buf
[256 + FILENAME_MAX
];
3053 sprintf(buf
, "Are you sure you want to overwrite"
3054 " the file \"%.*s\"?",
3055 FILENAME_MAX
, filename
);
3056 if (MessageBox(hwnd
, buf
, "Question",
3057 MB_YESNO
| MB_ICONQUESTION
)
3062 fp
= fopen(filename
, "w");
3065 MessageBox(hwnd
, "Unable to open save file",
3066 "Error", MB_ICONERROR
| MB_OK
);
3070 midend_serialise(fe
->me
, savefile_write
, fp
);
3074 FILE *fp
= fopen(filename
, "r");
3076 midend
*me
= fe
->me
;
3082 MessageBox(hwnd
, "Unable to open saved game file",
3083 "Error", MB_ICONERROR
| MB_OK
);
3089 * This save file might be from a different
3092 err
= identify_game(&id_name
, savefile_read
, fp
);
3095 for (i
= 0; i
< gamecount
; i
++)
3096 if (!strcmp(id_name
, gamelist
[i
]->name
))
3098 if (i
== gamecount
) {
3099 err
= "Save file is for a game not "
3100 "supported by this program";
3102 me
= midend_for_new_game(fe
, gamelist
[i
], NULL
,
3103 FALSE
, FALSE
, &err
);
3104 rewind(fp
); /* for the actual load */
3110 err
= midend_deserialise(me
, savefile_read
, fp
);
3115 MessageBox(hwnd
, err
, "Error", MB_ICONERROR
|MB_OK
);
3120 fe_set_midend(fe
, me
);
3121 new_game_size(fe
, 1.0);
3129 start_help(fe
, NULL
);
3132 assert(help_type
!= NONE
);
3133 start_help(fe
, help_type
== CHM ?
3134 fe
->game
->htmlhelp_topic
: fe
->game
->winhelp_topic
);
3139 if (wParam
>= IDM_GAMES
&& wParam
< (IDM_GAMES
+ (WPARAM
)gamecount
)) {
3140 int p
= wParam
- IDM_GAMES
;
3142 fe_set_midend(fe
, midend_for_new_game(fe
, gamelist
[p
], NULL
,
3143 FALSE
, FALSE
, &error
));
3148 int p
= ((wParam
&~ 0xF) - IDM_PRESETS
) / 0x10;
3150 if (p
>= 0 && p
< fe
->npresets
) {
3151 midend_set_params(fe
->me
, fe
->presets
[p
]);
3172 hdc
= BeginPaint(hwnd
, &p
);
3173 hdc2
= CreateCompatibleDC(hdc
);
3174 prevbm
= SelectObject(hdc2
, fe
->bitmap
);
3176 FillRect(hdc
, &(p
.rcPaint
), (HBRUSH
) GetStockObject(WHITE_BRUSH
));
3178 IntersectRect(&rcDest
, &(fe
->bitmapPosition
), &(p
.rcPaint
));
3180 rcDest
.left
, rcDest
.top
,
3181 rcDest
.right
- rcDest
.left
,
3182 rcDest
.bottom
- rcDest
.top
,
3184 rcDest
.left
- fe
->bitmapPosition
.left
,
3185 rcDest
.top
- fe
->bitmapPosition
.top
,
3187 SelectObject(hdc2
, prevbm
);
3196 int r
= GetKeyboardState(keystate
);
3197 int shift
= (r
&& (keystate
[VK_SHIFT
] & 0x80)) ? MOD_SHFT
: 0;
3198 int ctrl
= (r
&& (keystate
[VK_CONTROL
] & 0x80)) ? MOD_CTRL
: 0;
3202 if (!(lParam
& 0x01000000))
3203 key
= MOD_NUM_KEYPAD
| '4';
3205 key
= shift
| ctrl
| CURSOR_LEFT
;
3208 if (!(lParam
& 0x01000000))
3209 key
= MOD_NUM_KEYPAD
| '6';
3211 key
= shift
| ctrl
| CURSOR_RIGHT
;
3214 if (!(lParam
& 0x01000000))
3215 key
= MOD_NUM_KEYPAD
| '8';
3217 key
= shift
| ctrl
| CURSOR_UP
;
3220 if (!(lParam
& 0x01000000))
3221 key
= MOD_NUM_KEYPAD
| '2';
3223 key
= shift
| ctrl
| CURSOR_DOWN
;
3226 * Diagonal keys on the numeric keypad.
3229 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '9';
3232 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '3';
3235 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '7';
3238 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '1';
3241 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '0';
3244 if (!(lParam
& 0x01000000)) key
= MOD_NUM_KEYPAD
| '5';
3247 * Numeric keypad keys with Num Lock on.
3249 case VK_NUMPAD4
: key
= MOD_NUM_KEYPAD
| '4'; break;
3250 case VK_NUMPAD6
: key
= MOD_NUM_KEYPAD
| '6'; break;
3251 case VK_NUMPAD8
: key
= MOD_NUM_KEYPAD
| '8'; break;
3252 case VK_NUMPAD2
: key
= MOD_NUM_KEYPAD
| '2'; break;
3253 case VK_NUMPAD5
: key
= MOD_NUM_KEYPAD
| '5'; break;
3254 case VK_NUMPAD9
: key
= MOD_NUM_KEYPAD
| '9'; break;
3255 case VK_NUMPAD3
: key
= MOD_NUM_KEYPAD
| '3'; break;
3256 case VK_NUMPAD7
: key
= MOD_NUM_KEYPAD
| '7'; break;
3257 case VK_NUMPAD1
: key
= MOD_NUM_KEYPAD
| '1'; break;
3258 case VK_NUMPAD0
: key
= MOD_NUM_KEYPAD
| '0'; break;
3262 if (!midend_process_key(fe
->me
, 0, 0, key
))
3267 m
.message
= WM_KEYDOWN
;
3269 m
.lParam
= lParam
& 0xdfff;
3270 TranslateMessage(&m
);
3274 case WM_LBUTTONDOWN
:
3275 case WM_RBUTTONDOWN
:
3276 case WM_MBUTTONDOWN
:
3281 * Shift-clicks count as middle-clicks, since otherwise
3282 * two-button Windows users won't have any kind of
3283 * middle click to use.
3285 if (message
== WM_MBUTTONDOWN
|| (wParam
& MK_SHIFT
))
3286 button
= MIDDLE_BUTTON
;
3287 else if (message
== WM_RBUTTONDOWN
|| is_alt_pressed())
3288 button
= RIGHT_BUTTON
;
3291 button
= LEFT_BUTTON
;
3293 if ((fe
->game
->flags
& REQUIRE_RBUTTON
) == 0)
3294 button
= LEFT_BUTTON
;
3299 shrgi
.cbSize
= sizeof(SHRGINFO
);
3300 shrgi
.hwndClient
= hwnd
;
3301 shrgi
.ptDown
.x
= (signed short)LOWORD(lParam
);
3302 shrgi
.ptDown
.y
= (signed short)HIWORD(lParam
);
3303 shrgi
.dwFlags
= SHRG_RETURNCMD
;
3305 if (GN_CONTEXTMENU
== SHRecognizeGesture(&shrgi
))
3306 button
= RIGHT_BUTTON
;
3308 button
= LEFT_BUTTON
;
3312 if (!midend_process_key(fe
->me
,
3313 (signed short)LOWORD(lParam
) - fe
->bitmapPosition
.left
,
3314 (signed short)HIWORD(lParam
) - fe
->bitmapPosition
.top
,
3328 * Shift-clicks count as middle-clicks, since otherwise
3329 * two-button Windows users won't have any kind of
3330 * middle click to use.
3332 if (message
== WM_MBUTTONUP
|| (wParam
& MK_SHIFT
))
3333 button
= MIDDLE_RELEASE
;
3334 else if (message
== WM_RBUTTONUP
|| is_alt_pressed())
3335 button
= RIGHT_RELEASE
;
3337 button
= LEFT_RELEASE
;
3339 if (!midend_process_key(fe
->me
,
3340 (signed short)LOWORD(lParam
) - fe
->bitmapPosition
.left
,
3341 (signed short)HIWORD(lParam
) - fe
->bitmapPosition
.top
,
3352 if (wParam
& (MK_MBUTTON
| MK_SHIFT
))
3353 button
= MIDDLE_DRAG
;
3354 else if (wParam
& MK_RBUTTON
|| is_alt_pressed())
3355 button
= RIGHT_DRAG
;
3359 if (!midend_process_key(fe
->me
,
3360 (signed short)LOWORD(lParam
) - fe
->bitmapPosition
.left
,
3361 (signed short)HIWORD(lParam
) - fe
->bitmapPosition
.top
,
3367 if (!midend_process_key(fe
->me
, 0, 0, (unsigned char)wParam
))
3372 DWORD now
= GetTickCount();
3373 float elapsed
= (float) (now
- fe
->timer_last_tickcount
) * 0.001F
;
3374 midend_timer(fe
->me
, elapsed
);
3375 fe
->timer_last_tickcount
= now
;
3381 RECT
*sr
= (RECT
*)lParam
;
3382 int wx
, wy
, isedge
= 0;
3384 if (wParam
== WMSZ_TOP
||
3385 wParam
== WMSZ_RIGHT
||
3386 wParam
== WMSZ_BOTTOM
||
3387 wParam
== WMSZ_LEFT
) isedge
= 1;
3388 adjust_game_size(fe
, sr
, isedge
, &wx
, &wy
);
3390 /* Given the window size the puzzles constrain
3391 * us to, work out which edge we should be moving. */
3392 if (wParam
== WMSZ_TOP
||
3393 wParam
== WMSZ_TOPLEFT
||
3394 wParam
== WMSZ_TOPRIGHT
) {
3395 sr
->top
= sr
->bottom
- wy
;
3397 sr
->bottom
= sr
->top
+ wy
;
3399 if (wParam
== WMSZ_LEFT
||
3400 wParam
== WMSZ_TOPLEFT
||
3401 wParam
== WMSZ_BOTTOMLEFT
) {
3402 sr
->left
= sr
->right
- wx
;
3404 sr
->right
= sr
->left
+ wx
;
3412 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
3416 static int FindPreviousInstance()
3418 /* Check if application is running. If it's running then focus on the window */
3419 HWND hOtherWnd
= NULL
;
3421 hOtherWnd
= FindWindow (wGameName
, wGameName
);
3424 SetForegroundWindow (hOtherWnd
);
3433 * Split a complete command line into argc/argv, attempting to do it
3434 * exactly the same way the Visual Studio C library would do it (so
3435 * that our console utilities, which receive argc and argv already
3436 * broken apart by the C library, will have their command lines
3437 * processed in the same way as the GUI utilities which get a whole
3438 * command line and must call this function).
3440 * Does not modify the input command line.
3442 * The final parameter (argstart) is used to return a second array
3443 * of char * pointers, the same length as argv, each one pointing
3444 * at the start of the corresponding element of argv in the
3445 * original command line. So if you get half way through processing
3446 * your command line in argc/argv form and then decide you want to
3447 * treat the rest as a raw string, you can. If you don't want to,
3448 * `argstart' can be safely left NULL.
3450 void split_into_argv(char *cmdline
, int *argc
, char ***argv
,
3454 char *outputline
, *q
;
3455 char **outputargv
, **outputargstart
;
3459 * These argument-breaking rules apply to Visual Studio 7, which
3460 * is currently the compiler expected to be used for the Windows
3461 * port of my puzzles. Visual Studio 10 has different rules,
3462 * lacking the curious mod 3 behaviour of consecutive quotes
3463 * described below; I presume they fixed a bug. As and when we
3464 * migrate to a newer compiler, we'll have to adjust this to
3465 * match; however, for the moment we faithfully imitate in our GUI
3466 * utilities what our CLI utilities can't be prevented from doing.
3468 * When I investigated this, at first glance the rules appeared to
3471 * - Single quotes are not special characters.
3473 * - Double quotes are removed, but within them spaces cease
3476 * - Backslashes are _only_ special when a sequence of them
3477 * appear just before a double quote. In this situation,
3478 * they are treated like C backslashes: so \" just gives a
3479 * literal quote, \\" gives a literal backslash and then
3480 * opens or closes a double-quoted segment, \\\" gives a
3481 * literal backslash and then a literal quote, \\\\" gives
3482 * two literal backslashes and then opens/closes a
3483 * double-quoted segment, and so forth. Note that this
3484 * behaviour is identical inside and outside double quotes.
3486 * - Two successive double quotes become one literal double
3487 * quote, but only _inside_ a double-quoted segment.
3488 * Outside, they just form an empty double-quoted segment
3489 * (which may cause an empty argument word).
3491 * - That only leaves the interesting question of what happens
3492 * when one or more backslashes precedes two or more double
3493 * quotes, starting inside a double-quoted string. And the
3494 * answer to that appears somewhat bizarre. Here I tabulate
3495 * number of backslashes (across the top) against number of
3496 * quotes (down the left), and indicate how many backslashes
3497 * are output, how many quotes are output, and whether a
3498 * quoted segment is open at the end of the sequence:
3504 * 0 0,0,y | 1,0,y 2,0,y 3,0,y 4,0,y
3505 * --------+-----------------------------
3506 * 1 0,0,n | 0,1,y 1,0,n 1,1,y 2,0,n
3507 * q 2 0,1,n | 0,1,n 1,1,n 1,1,n 2,1,n
3508 * u 3 0,1,y | 0,2,n 1,1,y 1,2,n 2,1,y
3509 * o 4 0,1,n | 0,2,y 1,1,n 1,2,y 2,1,n
3510 * t 5 0,2,n | 0,2,n 1,2,n 1,2,n 2,2,n
3511 * e 6 0,2,y | 0,3,n 1,2,y 1,3,n 2,2,y
3512 * s 7 0,2,n | 0,3,y 1,2,n 1,3,y 2,2,n
3513 * 8 0,3,n | 0,3,n 1,3,n 1,3,n 2,3,n
3514 * 9 0,3,y | 0,4,n 1,3,y 1,4,n 2,3,y
3515 * 10 0,3,n | 0,4,y 1,3,n 1,4,y 2,3,n
3516 * 11 0,4,n | 0,4,n 1,4,n 1,4,n 2,4,n
3519 * [Test fragment was of the form "a\\\"""b c" d.]
3521 * There is very weird mod-3 behaviour going on here in the
3522 * number of quotes, and it even applies when there aren't any
3523 * backslashes! How ghastly.
3525 * With a bit of thought, this extremely odd diagram suddenly
3526 * coalesced itself into a coherent, if still ghastly, model of
3529 * - As before, backslashes are only special when one or more
3530 * of them appear contiguously before at least one double
3531 * quote. In this situation the backslashes do exactly what
3532 * you'd expect: each one quotes the next thing in front of
3533 * it, so you end up with n/2 literal backslashes (if n is
3534 * even) or (n-1)/2 literal backslashes and a literal quote
3535 * (if n is odd). In the latter case the double quote
3536 * character right after the backslashes is used up.
3538 * - After that, any remaining double quotes are processed. A
3539 * string of contiguous unescaped double quotes has a mod-3
3542 * * inside a quoted segment, a quote ends the segment.
3543 * * _immediately_ after ending a quoted segment, a quote
3544 * simply produces a literal quote.
3545 * * otherwise, outside a quoted segment, a quote begins a
3548 * So, for example, if we started inside a quoted segment
3549 * then two contiguous quotes would close the segment and
3550 * produce a literal quote; three would close the segment,
3551 * produce a literal quote, and open a new segment. If we
3552 * started outside a quoted segment, then two contiguous
3553 * quotes would open and then close a segment, producing no
3554 * output (but potentially creating a zero-length argument);
3555 * but three quotes would open and close a segment and then
3556 * produce a literal quote.
3560 * First deal with the simplest of all special cases: if there
3561 * aren't any arguments, return 0,NULL,NULL.
3563 while (*cmdline
&& isspace(*cmdline
)) cmdline
++;
3565 if (argc
) *argc
= 0;
3566 if (argv
) *argv
= NULL
;
3567 if (argstart
) *argstart
= NULL
;
3572 * This will guaranteeably be big enough; we can realloc it
3575 outputline
= snewn(1+strlen(cmdline
), char);
3576 outputargv
= snewn(strlen(cmdline
)+1 / 2, char *);
3577 outputargstart
= snewn(strlen(cmdline
)+1 / 2, char *);
3579 p
= cmdline
; q
= outputline
; outputargc
= 0;
3584 /* Skip whitespace searching for start of argument. */
3585 while (*p
&& isspace(*p
)) p
++;
3588 /* We have an argument; start it. */
3589 outputargv
[outputargc
] = q
;
3590 outputargstart
[outputargc
] = p
;
3594 /* Copy data into the argument until it's finished. */
3596 if (!quote
&& isspace(*p
))
3597 break; /* argument is finished */
3599 if (*p
== '"' || *p
== '\\') {
3601 * We have a sequence of zero or more backslashes
3602 * followed by a sequence of zero or more quotes.
3603 * Count up how many of each, and then deal with
3604 * them as appropriate.
3606 int i
, slashes
= 0, quotes
= 0;
3607 while (*p
== '\\') slashes
++, p
++;
3608 while (*p
== '"') quotes
++, p
++;
3612 * Special case: if there are no quotes,
3613 * slashes are not special at all, so just copy
3614 * n slashes to the output string.
3616 while (slashes
--) *q
++ = '\\';
3618 /* Slashes annihilate in pairs. */
3619 while (slashes
>= 2) slashes
-= 2, *q
++ = '\\';
3621 /* One remaining slash takes out the first quote. */
3622 if (slashes
) quotes
--, *q
++ = '"';
3625 /* Outside a quote segment, a quote starts one. */
3626 if (!quote
) quotes
--, quote
= 1;
3628 /* Now we produce (n+1)/3 literal quotes... */
3629 for (i
= 3; i
<= quotes
+1; i
+= 3) *q
++ = '"';
3631 /* ... and end in a quote segment iff 3 divides n. */
3632 quote
= (quotes
% 3 == 0);
3640 /* At the end of an argument, just append a trailing NUL. */
3644 outputargv
= sresize(outputargv
, outputargc
, char *);
3645 outputargstart
= sresize(outputargstart
, outputargc
, char *);
3647 if (argc
) *argc
= outputargc
;
3648 if (argv
) *argv
= outputargv
; else sfree(outputargv
);
3649 if (argstart
) *argstart
= outputargstart
; else sfree(outputargstart
);
3652 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
)
3662 split_into_argv(cmdline
, &argc
, &argv
, NULL
);
3665 MultiByteToWideChar (CP_ACP
, 0, CLASSNAME
, -1, wClassName
, 256);
3666 if (FindPreviousInstance ())
3670 InitCommonControls();
3676 wndclass
.lpfnWndProc
= WndProc
;
3677 wndclass
.cbClsExtra
= 0;
3678 wndclass
.cbWndExtra
= 0;
3679 wndclass
.hInstance
= inst
;
3680 wndclass
.hIcon
= LoadIcon(inst
, MAKEINTRESOURCE(200));
3682 if (!wndclass
.hIcon
) /* in case resource file is absent */
3683 wndclass
.hIcon
= LoadIcon(inst
, IDI_APPLICATION
);
3685 wndclass
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
3686 wndclass
.hbrBackground
= NULL
;
3687 wndclass
.lpszMenuName
= NULL
;
3689 wndclass
.lpszClassName
= wClassName
;
3691 wndclass
.lpszClassName
= CLASSNAME
;
3694 RegisterClass(&wndclass
);
3697 while (*cmdline
&& isspace((unsigned char)*cmdline
))
3706 for (i
= 0; i
< gamecount
; i
++) {
3707 const char *p
= gamelist
[i
]->name
;
3710 if (isspace((unsigned char)*p
)) {
3711 while (*q
&& isspace((unsigned char)*q
))
3714 if (tolower((unsigned char)*p
) !=
3715 tolower((unsigned char)*q
))
3733 fe
= frontend_new(inst
);
3734 me
= midend_for_new_game(fe
, gg
, argc
> 0 ? argv
[0] : NULL
,
3735 TRUE
, TRUE
, &error
);
3739 sprintf(buf
, "Puzzles Error");
3741 sprintf(buf
, "%.100s Error", gg
->name
);
3743 MessageBox(NULL
, error
, buf
, MB_OK
|MB_ICONERROR
);
3747 fe_set_midend(fe
, me
);
3750 while (GetMessage(&msg
, NULL
, 0, 0)) {
3751 DispatchMessage(&msg
);
3754 DestroyWindow(fe
->hwnd
);
3759 /* vim: set shiftwidth=4 tabstop=8: */