2 * windows.c: Windows front end for my puzzle collection.
6 * - Figure out what to do if a puzzle requests a size bigger than
7 * the screen will take. In principle we could put scrollbars in
8 * the window, although that would be pretty horrid. Another
9 * option is to detect in advance that this will be a problem -
10 * we can probably tell this using midend_size() before actually
11 * generating the puzzle - and simply refuse to do it.
26 #define IDM_NEW 0x0010
27 #define IDM_RESTART 0x0020
28 #define IDM_UNDO 0x0030
29 #define IDM_REDO 0x0040
30 #define IDM_COPY 0x0050
31 #define IDM_SOLVE 0x0060
32 #define IDM_QUIT 0x0070
33 #define IDM_CONFIG 0x0080
34 #define IDM_SEED 0x0090
35 #define IDM_HELPC 0x00A0
36 #define IDM_GAMEHELP 0x00B0
37 #define IDM_PRESETS 0x0100
39 #define HELP_FILE_NAME "puzzles.hlp"
40 #define HELP_CNT_NAME "puzzles.cnt"
43 static FILE *debug_fp
= NULL
;
44 static HANDLE debug_hdl
= INVALID_HANDLE_VALUE
;
45 static int debug_got_console
= 0;
51 if (!debug_got_console
) {
53 debug_got_console
= 1;
54 debug_hdl
= GetStdHandle(STD_OUTPUT_HANDLE
);
58 debug_fp
= fopen("debug.log", "w");
61 if (debug_hdl
!= INVALID_HANDLE_VALUE
) {
62 WriteFile(debug_hdl
, buf
, strlen(buf
), &dw
, NULL
);
68 void debug_printf(char *fmt
, ...)
74 vsprintf(buf
, fmt
, ap
);
79 #define debug(x) (debug_printf x)
99 HWND hwnd
, statusbar
, cfgbox
;
101 HBITMAP bitmap
, prevbm
;
108 DWORD timer_last_tickcount
;
110 game_params
**presets
;
112 int nfonts
, fontsize
;
114 struct cfg_aux
*cfgaux
;
115 int cfg_which
, cfg_done
;
118 int help_has_contents
;
121 void fatal(char *fmt
, ...)
127 vsprintf(buf
, fmt
, ap
);
130 MessageBox(NULL
, buf
, "Fatal error", MB_ICONEXCLAMATION
| MB_OK
);
135 void get_random_seed(void **randseed
, int *randseedsize
)
137 time_t *tp
= snew(time_t);
139 *randseed
= (void *)tp
;
140 *randseedsize
= sizeof(time_t);
143 void status_bar(frontend
*fe
, char *text
)
145 SetWindowText(fe
->statusbar
, text
);
148 void frontend_default_colour(frontend
*fe
, float *output
)
150 DWORD c
= GetSysColor(COLOR_MENU
); /* ick */
152 output
[0] = (float)(GetRValue(c
) / 255.0);
153 output
[1] = (float)(GetGValue(c
) / 255.0);
154 output
[2] = (float)(GetBValue(c
) / 255.0);
157 void clip(frontend
*fe
, int x
, int y
, int w
, int h
)
159 IntersectClipRect(fe
->hdc_bm
, x
, y
, x
+w
, y
+h
);
162 void unclip(frontend
*fe
)
164 SelectClipRgn(fe
->hdc_bm
, NULL
);
167 void draw_text(frontend
*fe
, int x
, int y
, int fonttype
, int fontsize
,
168 int align
, int colour
, char *text
)
173 * Find or create the font.
175 for (i
= 0; i
< fe
->nfonts
; i
++)
176 if (fe
->fonts
[i
].type
== fonttype
&& fe
->fonts
[i
].size
== fontsize
)
179 if (i
== fe
->nfonts
) {
180 if (fe
->fontsize
<= fe
->nfonts
) {
181 fe
->fontsize
= fe
->nfonts
+ 10;
182 fe
->fonts
= sresize(fe
->fonts
, fe
->fontsize
, struct font
);
187 fe
->fonts
[i
].type
= fonttype
;
188 fe
->fonts
[i
].size
= fontsize
;
190 fe
->fonts
[i
].font
= CreateFont(-fontsize
, 0, 0, 0, FW_BOLD
,
191 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
192 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
194 (fonttype
== FONT_FIXED ?
195 FIXED_PITCH
| FF_DONTCARE
:
196 VARIABLE_PITCH
| FF_SWISS
),
201 * Position and draw the text.
208 oldfont
= SelectObject(fe
->hdc_bm
, fe
->fonts
[i
].font
);
209 if (GetTextMetrics(fe
->hdc_bm
, &tm
)) {
210 if (align
& ALIGN_VCENTRE
)
211 y
-= (tm
.tmAscent
+tm
.tmDescent
)/2;
215 if (GetTextExtentPoint32(fe
->hdc_bm
, text
, strlen(text
), &size
)) {
216 if (align
& ALIGN_HCENTRE
)
218 else if (align
& ALIGN_HRIGHT
)
221 SetBkMode(fe
->hdc_bm
, TRANSPARENT
);
222 SetTextColor(fe
->hdc_bm
, fe
->colours
[colour
]);
223 TextOut(fe
->hdc_bm
, x
, y
, text
, strlen(text
));
224 SelectObject(fe
->hdc_bm
, oldfont
);
228 void draw_rect(frontend
*fe
, int x
, int y
, int w
, int h
, int colour
)
230 if (w
== 1 && h
== 1) {
232 * Rectangle() appears to get uppity if asked to draw a 1x1
233 * rectangle, presumably on the grounds that that's beneath
234 * its dignity and you ought to be using SetPixel instead.
237 SetPixel(fe
->hdc_bm
, x
, y
, fe
->colours
[colour
]);
239 HBRUSH oldbrush
= SelectObject(fe
->hdc_bm
, fe
->brushes
[colour
]);
240 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
241 Rectangle(fe
->hdc_bm
, x
, y
, x
+w
, y
+h
);
242 SelectObject(fe
->hdc_bm
, oldbrush
);
243 SelectObject(fe
->hdc_bm
, oldpen
);
247 void draw_line(frontend
*fe
, int x1
, int y1
, int x2
, int y2
, int colour
)
249 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
250 MoveToEx(fe
->hdc_bm
, x1
, y1
, NULL
);
251 LineTo(fe
->hdc_bm
, x2
, y2
);
252 SetPixel(fe
->hdc_bm
, x2
, y2
, fe
->colours
[colour
]);
253 SelectObject(fe
->hdc_bm
, oldpen
);
256 void draw_polygon(frontend
*fe
, int *coords
, int npoints
,
257 int fill
, int colour
)
259 POINT
*pts
= snewn(npoints
+1, POINT
);
262 for (i
= 0; i
<= npoints
; i
++) {
263 int j
= (i
< npoints ? i
: 0);
264 pts
[i
].x
= coords
[j
*2];
265 pts
[i
].y
= coords
[j
*2+1];
269 HBRUSH oldbrush
= SelectObject(fe
->hdc_bm
, fe
->brushes
[colour
]);
270 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
271 Polygon(fe
->hdc_bm
, pts
, npoints
);
272 SelectObject(fe
->hdc_bm
, oldbrush
);
273 SelectObject(fe
->hdc_bm
, oldpen
);
275 HPEN oldpen
= SelectObject(fe
->hdc_bm
, fe
->pens
[colour
]);
276 Polyline(fe
->hdc_bm
, pts
, npoints
+1);
277 SelectObject(fe
->hdc_bm
, oldpen
);
283 void start_draw(frontend
*fe
)
286 hdc_win
= GetDC(fe
->hwnd
);
287 fe
->hdc_bm
= CreateCompatibleDC(hdc_win
);
288 fe
->prevbm
= SelectObject(fe
->hdc_bm
, fe
->bitmap
);
289 ReleaseDC(fe
->hwnd
, hdc_win
);
291 SetMapMode(fe
->hdc_bm
, MM_TEXT
);
294 void draw_update(frontend
*fe
, int x
, int y
, int w
, int h
)
303 InvalidateRect(fe
->hwnd
, &r
, FALSE
);
306 void end_draw(frontend
*fe
)
308 SelectObject(fe
->hdc_bm
, fe
->prevbm
);
309 DeleteDC(fe
->hdc_bm
);
311 DeleteObject(fe
->clip
);
316 void deactivate_timer(frontend
*fe
)
318 KillTimer(fe
->hwnd
, fe
->timer
);
322 void activate_timer(frontend
*fe
)
325 fe
->timer
= SetTimer(fe
->hwnd
, fe
->timer
, 20, NULL
);
326 fe
->timer_last_tickcount
= GetTickCount();
330 void write_clip(HWND hwnd
, char *data
)
338 * Windows expects CRLF in the clipboard, so we must convert
339 * any \n that has come out of the puzzle backend.
342 for (i
= 0; data
[i
]; i
++) {
347 data2
= snewn(len
+1, char);
349 for (i
= 0; data
[i
]; i
++) {
352 data2
[j
++] = data
[i
];
357 clipdata
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, len
+ 1);
360 lock
= GlobalLock(clipdata
);
363 memcpy(lock
, data2
, len
);
364 ((unsigned char *) lock
)[len
] = 0;
365 GlobalUnlock(clipdata
);
367 if (OpenClipboard(hwnd
)) {
369 SetClipboardData(CF_TEXT
, clipdata
);
372 GlobalFree(clipdata
);
378 * See if we can find a help file.
380 static void find_help_file(frontend
*fe
)
382 char b
[2048], *p
, *q
, *r
;
384 if (!fe
->help_path
) {
385 GetModuleFileName(NULL
, b
, sizeof(b
) - 1);
387 p
= strrchr(b
, '\\');
388 if (p
&& p
>= r
) r
= p
+1;
390 if (q
&& q
>= r
) r
= q
+1;
391 strcpy(r
, HELP_FILE_NAME
);
392 if ( (fp
= fopen(b
, "r")) != NULL
) {
393 fe
->help_path
= dupstr(b
);
396 fe
->help_path
= NULL
;
397 strcpy(r
, HELP_CNT_NAME
);
398 if ( (fp
= fopen(b
, "r")) != NULL
) {
399 fe
->help_has_contents
= TRUE
;
402 fe
->help_has_contents
= FALSE
;
406 static frontend
*new_window(HINSTANCE inst
, char *game_id
, char **error
)
415 fe
->me
= midend_new(fe
, &thegame
);
418 *error
= midend_game_id(fe
->me
, game_id
, FALSE
);
426 fe
->help_path
= NULL
;
430 midend_new_game(fe
->me
);
431 midend_size(fe
->me
, &x
, &y
);
436 fe
->nfonts
= fe
->fontsize
= 0;
442 colours
= midend_colours(fe
->me
, &ncolours
);
444 fe
->colours
= snewn(ncolours
, COLORREF
);
445 fe
->brushes
= snewn(ncolours
, HBRUSH
);
446 fe
->pens
= snewn(ncolours
, HPEN
);
448 for (i
= 0; i
< ncolours
; i
++) {
449 fe
->colours
[i
] = RGB(255 * colours
[i
*3+0],
450 255 * colours
[i
*3+1],
451 255 * colours
[i
*3+2]);
452 fe
->brushes
[i
] = CreateSolidBrush(fe
->colours
[i
]);
453 fe
->pens
[i
] = CreatePen(PS_SOLID
, 1, fe
->colours
[i
]);
460 AdjustWindowRectEx(&r
, WS_OVERLAPPEDWINDOW
&~
461 (WS_THICKFRAME
| WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
464 fe
->hwnd
= CreateWindowEx(0, thegame
.name
, thegame
.name
,
465 WS_OVERLAPPEDWINDOW
&~
466 (WS_THICKFRAME
| WS_MAXIMIZEBOX
),
467 CW_USEDEFAULT
, CW_USEDEFAULT
,
468 r
.right
- r
.left
, r
.bottom
- r
.top
,
469 NULL
, NULL
, inst
, NULL
);
472 HMENU bar
= CreateMenu();
473 HMENU menu
= CreateMenu();
475 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)menu
, "Game");
476 AppendMenu(menu
, MF_ENABLED
, IDM_NEW
, "New");
477 AppendMenu(menu
, MF_ENABLED
, IDM_RESTART
, "Restart");
478 AppendMenu(menu
, MF_ENABLED
, IDM_SEED
, "Specific...");
480 if ((fe
->npresets
= midend_num_presets(fe
->me
)) > 0 ||
481 thegame
.can_configure
) {
482 HMENU sub
= CreateMenu();
485 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)sub
, "Type");
487 fe
->presets
= snewn(fe
->npresets
, game_params
*);
489 for (i
= 0; i
< fe
->npresets
; i
++) {
492 midend_fetch_preset(fe
->me
, i
, &name
, &fe
->presets
[i
]);
495 * FIXME: we ought to go through and do something
496 * with ampersands here.
499 AppendMenu(sub
, MF_ENABLED
, IDM_PRESETS
+ 0x10 * i
, name
);
502 if (thegame
.can_configure
) {
503 AppendMenu(sub
, MF_ENABLED
, IDM_CONFIG
, "Custom...");
507 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
508 AppendMenu(menu
, MF_ENABLED
, IDM_UNDO
, "Undo");
509 AppendMenu(menu
, MF_ENABLED
, IDM_REDO
, "Redo");
510 if (thegame
.can_format_as_text
) {
511 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
512 AppendMenu(menu
, MF_ENABLED
, IDM_COPY
, "Copy");
514 if (thegame
.can_solve
) {
515 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
516 AppendMenu(menu
, MF_ENABLED
, IDM_SOLVE
, "Solve");
518 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
519 AppendMenu(menu
, MF_ENABLED
, IDM_QUIT
, "Exit");
521 HMENU hmenu
= CreateMenu();
522 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)hmenu
, "Help");
523 AppendMenu(hmenu
, MF_ENABLED
, IDM_HELPC
, "Contents");
524 if (thegame
.winhelp_topic
) {
526 assert(thegame
.name
);
527 item
= snewn(9+strlen(thegame
.name
), char); /*ick*/
528 sprintf(item
, "Help on %s", thegame
.name
);
529 AppendMenu(hmenu
, MF_ENABLED
, IDM_GAMEHELP
, item
);
533 SetMenu(fe
->hwnd
, bar
);
536 if (midend_wants_statusbar(fe
->me
)) {
537 fe
->statusbar
= CreateWindowEx(0, STATUSCLASSNAME
, "ooh",
538 WS_CHILD
| WS_VISIBLE
,
539 0, 0, 0, 0, /* status bar does these */
540 fe
->hwnd
, NULL
, inst
, NULL
);
541 GetWindowRect(fe
->statusbar
, &sr
);
542 SetWindowPos(fe
->hwnd
, NULL
, 0, 0,
543 r
.right
- r
.left
, r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
544 SWP_NOMOVE
| SWP_NOZORDER
);
545 SetWindowPos(fe
->statusbar
, NULL
, 0, y
, x
, sr
.bottom
- sr
.top
,
548 fe
->statusbar
= NULL
;
551 hdc
= GetDC(fe
->hwnd
);
552 fe
->bitmap
= CreateCompatibleBitmap(hdc
, x
, y
);
553 ReleaseDC(fe
->hwnd
, hdc
);
555 SetWindowLong(fe
->hwnd
, GWL_USERDATA
, (LONG
)fe
);
557 ShowWindow(fe
->hwnd
, SW_NORMAL
);
558 SetForegroundWindow(fe
->hwnd
);
560 midend_redraw(fe
->me
);
565 static int CALLBACK
ConfigDlgProc(HWND hwnd
, UINT msg
,
566 WPARAM wParam
, LPARAM lParam
)
568 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
578 * OK and Cancel are special cases.
580 if ((HIWORD(wParam
) == BN_CLICKED
||
581 HIWORD(wParam
) == BN_DOUBLECLICKED
) &&
582 (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)) {
583 if (LOWORD(wParam
) == IDOK
) {
584 char *err
= midend_set_config(fe
->me
, fe
->cfg_which
, fe
->cfg
);
587 MessageBox(hwnd
, err
, "Validation error",
588 MB_ICONERROR
| MB_OK
);
599 * First find the control whose id this is.
601 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
602 if (j
->ctlid
== LOWORD(wParam
))
605 if (i
->type
== C_END
)
606 return 0; /* not our problem */
608 if (i
->type
== C_STRING
&& HIWORD(wParam
) == EN_CHANGE
) {
610 GetDlgItemText(fe
->cfgbox
, j
->ctlid
, buffer
, lenof(buffer
));
611 buffer
[lenof(buffer
)-1] = '\0';
613 i
->sval
= dupstr(buffer
);
614 } else if (i
->type
== C_BOOLEAN
&&
615 (HIWORD(wParam
) == BN_CLICKED
||
616 HIWORD(wParam
) == BN_DOUBLECLICKED
)) {
617 i
->ival
= IsDlgButtonChecked(fe
->cfgbox
, j
->ctlid
);
618 } else if (i
->type
== C_CHOICES
&&
619 HIWORD(wParam
) == CBN_SELCHANGE
) {
620 i
->ival
= SendDlgItemMessage(fe
->cfgbox
, j
->ctlid
,
634 HWND
mkctrl(frontend
*fe
, int x1
, int x2
, int y1
, int y2
,
635 char *wclass
, int wstyle
,
636 int exstyle
, char *wtext
, int wid
)
639 ret
= CreateWindowEx(exstyle
, wclass
, wtext
,
640 wstyle
| WS_CHILD
| WS_VISIBLE
, x1
, y1
, x2
-x1
, y2
-y1
,
641 fe
->cfgbox
, (HMENU
) wid
, fe
->inst
, NULL
);
642 SendMessage(ret
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, MAKELPARAM(TRUE
, 0));
646 static int get_config(frontend
*fe
, int which
)
659 int winwidth
, winheight
, col1l
, col1r
, col2l
, col2r
, y
;
660 int height
, width
, maxlabel
, maxcheckbox
;
662 wc
.style
= CS_DBLCLKS
| CS_SAVEBITS
| CS_BYTEALIGNWINDOW
;
663 wc
.lpfnWndProc
= DefDlgProc
;
665 wc
.cbWndExtra
= DLGWINDOWEXTRA
+ 8;
666 wc
.hInstance
= fe
->inst
;
668 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
669 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+1);
670 wc
.lpszMenuName
= NULL
;
671 wc
.lpszClassName
= "GameConfigBox";
674 hdc
= GetDC(fe
->hwnd
);
675 SetMapMode(hdc
, MM_TEXT
);
677 fe
->cfg_done
= FALSE
;
679 fe
->cfgfont
= CreateFont(-MulDiv(8, GetDeviceCaps(hdc
, LOGPIXELSY
), 72),
681 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
682 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
687 oldfont
= SelectObject(hdc
, fe
->cfgfont
);
688 if (GetTextMetrics(hdc
, &tm
)) {
689 height
= tm
.tmAscent
+ tm
.tmDescent
;
690 width
= tm
.tmAveCharWidth
;
695 fe
->cfg
= midend_get_config(fe
->me
, which
, &title
);
696 fe
->cfg_which
= which
;
699 * Figure out the layout of the config box by measuring the
700 * length of each piece of text.
702 maxlabel
= maxcheckbox
= 0;
703 winheight
= height
/2;
705 for (i
= fe
->cfg
; i
->type
!= C_END
; i
++) {
710 * Both these control types have a label filling only
711 * the left-hand column of the box.
713 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
716 winheight
+= height
* 3 / 2 + (height
/ 2);
721 * Checkboxes take up the whole of the box width.
723 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
724 maxcheckbox
< size
.cx
)
725 maxcheckbox
= size
.cx
;
726 winheight
+= height
+ (height
/ 2);
731 winheight
+= height
+ height
* 7 / 4; /* OK / Cancel buttons */
734 col1r
= col1l
+ maxlabel
;
735 col2l
= col1r
+ 2*width
;
736 col2r
= col2l
+ 30*width
;
737 if (col2r
< col1l
+2*height
+maxcheckbox
)
738 col2r
= col1l
+2*height
+maxcheckbox
;
739 winwidth
= col2r
+ 2*width
;
741 ReleaseDC(fe
->hwnd
, hdc
);
744 * Create the dialog, now that we know its size.
751 r
.bottom
= winheight
;
753 AdjustWindowRectEx(&r
, (WS_OVERLAPPEDWINDOW
/*|
754 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
755 WS_CAPTION | WS_SYSMENU*/) &~
756 (WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
760 * Centre the dialog on its parent window.
764 GetWindowRect(fe
->hwnd
, &r2
);
765 r
.left
= (r2
.left
+ r2
.right
- r
.right
) / 2;
766 r
.top
= (r2
.top
+ r2
.bottom
- r
.bottom
) / 2;
770 fe
->cfgbox
= CreateWindowEx(0, wc
.lpszClassName
, title
,
771 DS_MODALFRAME
| WS_POPUP
| WS_VISIBLE
|
772 WS_CAPTION
| WS_SYSMENU
,
774 r
.right
-r
.left
, r
.bottom
-r
.top
,
775 fe
->hwnd
, NULL
, fe
->inst
, NULL
);
779 SendMessage(fe
->cfgbox
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, FALSE
);
781 SetWindowLong(fe
->cfgbox
, GWL_USERDATA
, (LONG
)fe
);
782 SetWindowLong(fe
->cfgbox
, DWL_DLGPROC
, (LONG
)ConfigDlgProc
);
785 * Count the controls so we can allocate cfgaux.
787 for (nctrls
= 0, i
= fe
->cfg
; i
->type
!= C_END
; i
++)
789 fe
->cfgaux
= snewn(nctrls
, struct cfg_aux
);
793 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
797 * Edit box with a label beside it.
799 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
800 "Static", 0, 0, i
->name
, id
++);
801 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*3/2,
802 "EDIT", WS_TABSTOP
| ES_AUTOHSCROLL
,
803 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
804 SetWindowText(ctl
, i
->sval
);
812 mkctrl(fe
, col1l
, col2r
, y
, y
+height
, "BUTTON",
813 BS_NOTIFY
| BS_AUTOCHECKBOX
| WS_TABSTOP
,
814 0, i
->name
, (j
->ctlid
= id
++));
815 CheckDlgButton(fe
->cfgbox
, j
->ctlid
, (i
->ival
!= 0));
821 * Drop-down list with a label beside it.
823 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
824 "STATIC", 0, 0, i
->name
, id
++);
825 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*41/2,
826 "COMBOBOX", WS_TABSTOP
|
827 CBS_DROPDOWNLIST
| CBS_HASSTRINGS
,
828 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
830 char c
, *p
, *q
, *str
;
832 SendMessage(ctl
, CB_RESETCONTENT
, 0, 0);
837 while (*q
&& *q
!= c
) q
++;
838 str
= snewn(q
-p
+1, char);
839 strncpy(str
, p
, q
-p
);
841 SendMessage(ctl
, CB_ADDSTRING
, 0, (LPARAM
)str
);
848 SendMessage(ctl
, CB_SETCURSEL
, i
->ival
, 0);
854 assert(y
< winheight
);
858 y
+= height
/2; /* extra space before OK and Cancel */
859 mkctrl(fe
, col1l
, (col1l
+col2r
)/2-width
, y
, y
+height
*7/4, "BUTTON",
860 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
| BS_DEFPUSHBUTTON
, 0,
862 mkctrl(fe
, (col1l
+col2r
)/2+width
, col2r
, y
, y
+height
*7/4, "BUTTON",
863 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
, 0, "Cancel", IDCANCEL
);
865 SendMessage(fe
->cfgbox
, WM_INITDIALOG
, 0, 0);
867 EnableWindow(fe
->hwnd
, FALSE
);
868 ShowWindow(fe
->cfgbox
, SW_NORMAL
);
869 while ((gm
=GetMessage(&msg
, NULL
, 0, 0)) > 0) {
870 if (!IsDialogMessage(fe
->cfgbox
, &msg
))
871 DispatchMessage(&msg
);
875 EnableWindow(fe
->hwnd
, TRUE
);
876 SetForegroundWindow(fe
->hwnd
);
877 DestroyWindow(fe
->cfgbox
);
878 DeleteObject(fe
->cfgfont
);
883 return (fe
->cfg_done
== 2);
886 static void new_game_type(frontend
*fe
)
892 midend_new_game(fe
->me
);
893 midend_size(fe
->me
, &x
, &y
);
898 AdjustWindowRectEx(&r
, WS_OVERLAPPEDWINDOW
&~
899 (WS_THICKFRAME
| WS_MAXIMIZEBOX
|
903 if (fe
->statusbar
!= NULL
) {
904 GetWindowRect(fe
->statusbar
, &sr
);
906 sr
.left
= sr
.right
= sr
.top
= sr
.bottom
= 0;
908 SetWindowPos(fe
->hwnd
, NULL
, 0, 0,
910 r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
911 SWP_NOMOVE
| SWP_NOZORDER
);
912 if (fe
->statusbar
!= NULL
)
913 SetWindowPos(fe
->statusbar
, NULL
, 0, y
, x
,
914 sr
.bottom
- sr
.top
, SWP_NOZORDER
);
916 DeleteObject(fe
->bitmap
);
918 hdc
= GetDC(fe
->hwnd
);
919 fe
->bitmap
= CreateCompatibleBitmap(hdc
, x
, y
);
920 ReleaseDC(fe
->hwnd
, hdc
);
922 midend_redraw(fe
->me
);
925 static LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
,
926 WPARAM wParam
, LPARAM lParam
)
928 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
935 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
937 if (!midend_process_key(fe
->me
, 0, 0, 'n'))
941 if (!midend_process_key(fe
->me
, 0, 0, 'r'))
945 if (!midend_process_key(fe
->me
, 0, 0, 'u'))
949 if (!midend_process_key(fe
->me
, 0, 0, '\x12'))
954 char *text
= midend_text_format(fe
->me
);
956 write_clip(hwnd
, text
);
958 MessageBeep(MB_ICONWARNING
);
964 char *msg
= midend_solve(fe
->me
);
966 MessageBox(hwnd
, msg
, "Unable to solve",
967 MB_ICONERROR
| MB_OK
);
971 if (!midend_process_key(fe
->me
, 0, 0, 'q'))
975 if (get_config(fe
, CFG_SETTINGS
))
979 if (get_config(fe
, CFG_SEED
))
983 assert(fe
->help_path
);
984 WinHelp(hwnd
, fe
->help_path
,
985 fe
->help_has_contents ? HELP_FINDER
: HELP_CONTENTS
, 0);
988 assert(fe
->help_path
);
989 assert(thegame
.winhelp_topic
);
991 char *cmd
= snewn(10+strlen(thegame
.winhelp_topic
), char);
992 sprintf(cmd
, "JI(`',`%s')", thegame
.winhelp_topic
);
993 WinHelp(hwnd
, fe
->help_path
, HELP_COMMAND
, (DWORD
)cmd
);
999 int p
= ((wParam
&~ 0xF) - IDM_PRESETS
) / 0x10;
1001 if (p
>= 0 && p
< fe
->npresets
) {
1002 midend_set_params(fe
->me
, fe
->presets
[p
]);
1018 hdc
= BeginPaint(hwnd
, &p
);
1019 hdc2
= CreateCompatibleDC(hdc
);
1020 prevbm
= SelectObject(hdc2
, fe
->bitmap
);
1022 p
.rcPaint
.left
, p
.rcPaint
.top
,
1023 p
.rcPaint
.right
- p
.rcPaint
.left
,
1024 p
.rcPaint
.bottom
- p
.rcPaint
.top
,
1026 p
.rcPaint
.left
, p
.rcPaint
.top
,
1028 SelectObject(hdc2
, prevbm
);
1038 case VK_LEFT
: key
= CURSOR_LEFT
; break;
1039 case VK_RIGHT
: key
= CURSOR_RIGHT
; break;
1040 case VK_UP
: key
= CURSOR_UP
; break;
1041 case VK_DOWN
: key
= CURSOR_DOWN
; break;
1043 * Diagonal keys on the numeric keypad.
1046 if (!(lParam
& 0x01000000)) key
= CURSOR_UP_RIGHT
;
1049 if (!(lParam
& 0x01000000)) key
= CURSOR_DOWN_RIGHT
;
1052 if (!(lParam
& 0x01000000)) key
= CURSOR_UP_LEFT
;
1055 if (!(lParam
& 0x01000000)) key
= CURSOR_DOWN_LEFT
;
1058 * Numeric keypad keys with Num Lock on.
1060 case VK_NUMPAD4
: key
= CURSOR_LEFT
; break;
1061 case VK_NUMPAD6
: key
= CURSOR_RIGHT
; break;
1062 case VK_NUMPAD8
: key
= CURSOR_UP
; break;
1063 case VK_NUMPAD2
: key
= CURSOR_DOWN
; break;
1064 case VK_NUMPAD9
: key
= CURSOR_UP_RIGHT
; break;
1065 case VK_NUMPAD3
: key
= CURSOR_DOWN_RIGHT
; break;
1066 case VK_NUMPAD7
: key
= CURSOR_UP_LEFT
; break;
1067 case VK_NUMPAD1
: key
= CURSOR_DOWN_LEFT
; break;
1071 if (!midend_process_key(fe
->me
, 0, 0, key
))
1076 m
.message
= WM_KEYDOWN
;
1078 m
.lParam
= lParam
& 0xdfff;
1079 TranslateMessage(&m
);
1083 case WM_LBUTTONDOWN
:
1084 case WM_RBUTTONDOWN
:
1085 case WM_MBUTTONDOWN
:
1090 * Shift-clicks count as middle-clicks, since otherwise
1091 * two-button Windows users won't have any kind of
1092 * middle click to use.
1094 if (message
== WM_MBUTTONDOWN
|| (wParam
& MK_SHIFT
))
1095 button
= MIDDLE_BUTTON
;
1096 else if (message
== WM_LBUTTONDOWN
)
1097 button
= LEFT_BUTTON
;
1099 button
= RIGHT_BUTTON
;
1101 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1102 (signed short)HIWORD(lParam
), button
))
1115 * Shift-clicks count as middle-clicks, since otherwise
1116 * two-button Windows users won't have any kind of
1117 * middle click to use.
1119 if (message
== WM_MBUTTONUP
|| (wParam
& MK_SHIFT
))
1120 button
= MIDDLE_RELEASE
;
1121 else if (message
== WM_LBUTTONUP
)
1122 button
= LEFT_RELEASE
;
1124 button
= RIGHT_RELEASE
;
1126 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1127 (signed short)HIWORD(lParam
), button
))
1137 if (wParam
& (MK_MBUTTON
| MK_SHIFT
))
1138 button
= MIDDLE_DRAG
;
1139 else if (wParam
& MK_LBUTTON
)
1142 button
= RIGHT_DRAG
;
1144 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1145 (signed short)HIWORD(lParam
), button
))
1150 if (!midend_process_key(fe
->me
, 0, 0, (unsigned char)wParam
))
1155 DWORD now
= GetTickCount();
1156 float elapsed
= (float) (now
- fe
->timer_last_tickcount
) * 0.001F
;
1157 midend_timer(fe
->me
, elapsed
);
1158 fe
->timer_last_tickcount
= now
;
1163 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
1166 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
)
1171 InitCommonControls();
1177 wndclass
.lpfnWndProc
= WndProc
;
1178 wndclass
.cbClsExtra
= 0;
1179 wndclass
.cbWndExtra
= 0;
1180 wndclass
.hInstance
= inst
;
1181 wndclass
.hIcon
= LoadIcon(inst
, IDI_APPLICATION
);
1182 wndclass
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
1183 wndclass
.hbrBackground
= NULL
;
1184 wndclass
.lpszMenuName
= NULL
;
1185 wndclass
.lpszClassName
= thegame
.name
;
1187 RegisterClass(&wndclass
);
1190 while (*cmdline
&& isspace(*cmdline
))
1193 if (!new_window(inst
, *cmdline ? cmdline
: NULL
, &error
)) {
1195 sprintf(buf
, "%.100s Error", thegame
.name
);
1196 MessageBox(NULL
, error
, buf
, MB_OK
|MB_ICONERROR
);
1200 while (GetMessage(&msg
, NULL
, 0, 0)) {
1201 DispatchMessage(&msg
);