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
)
333 int len
= strlen(data
);
336 clipdata
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, len
+ 1);
339 lock
= GlobalLock(clipdata
);
342 memcpy(lock
, data
, len
);
343 ((unsigned char *) lock
)[len
] = 0;
344 GlobalUnlock(clipdata
);
346 if (OpenClipboard(hwnd
)) {
348 SetClipboardData(CF_TEXT
, clipdata
);
351 GlobalFree(clipdata
);
355 * See if we can find a help file.
357 static void find_help_file(frontend
*fe
)
359 char b
[2048], *p
, *q
, *r
;
361 if (!fe
->help_path
) {
362 GetModuleFileName(NULL
, b
, sizeof(b
) - 1);
364 p
= strrchr(b
, '\\');
365 if (p
&& p
>= r
) r
= p
+1;
367 if (q
&& q
>= r
) r
= q
+1;
368 strcpy(r
, HELP_FILE_NAME
);
369 if ( (fp
= fopen(b
, "r")) != NULL
) {
370 fe
->help_path
= dupstr(b
);
373 fe
->help_path
= NULL
;
374 strcpy(r
, HELP_CNT_NAME
);
375 if ( (fp
= fopen(b
, "r")) != NULL
) {
376 fe
->help_has_contents
= TRUE
;
379 fe
->help_has_contents
= FALSE
;
383 static frontend
*new_window(HINSTANCE inst
, char *game_id
, char **error
)
392 fe
->me
= midend_new(fe
, &thegame
);
395 *error
= midend_game_id(fe
->me
, game_id
, FALSE
);
403 fe
->help_path
= NULL
;
407 midend_new_game(fe
->me
);
408 midend_size(fe
->me
, &x
, &y
);
413 fe
->nfonts
= fe
->fontsize
= 0;
419 colours
= midend_colours(fe
->me
, &ncolours
);
421 fe
->colours
= snewn(ncolours
, COLORREF
);
422 fe
->brushes
= snewn(ncolours
, HBRUSH
);
423 fe
->pens
= snewn(ncolours
, HPEN
);
425 for (i
= 0; i
< ncolours
; i
++) {
426 fe
->colours
[i
] = RGB(255 * colours
[i
*3+0],
427 255 * colours
[i
*3+1],
428 255 * colours
[i
*3+2]);
429 fe
->brushes
[i
] = CreateSolidBrush(fe
->colours
[i
]);
430 fe
->pens
[i
] = CreatePen(PS_SOLID
, 1, fe
->colours
[i
]);
437 AdjustWindowRectEx(&r
, WS_OVERLAPPEDWINDOW
&~
438 (WS_THICKFRAME
| WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
441 fe
->hwnd
= CreateWindowEx(0, thegame
.name
, thegame
.name
,
442 WS_OVERLAPPEDWINDOW
&~
443 (WS_THICKFRAME
| WS_MAXIMIZEBOX
),
444 CW_USEDEFAULT
, CW_USEDEFAULT
,
445 r
.right
- r
.left
, r
.bottom
- r
.top
,
446 NULL
, NULL
, inst
, NULL
);
449 HMENU bar
= CreateMenu();
450 HMENU menu
= CreateMenu();
452 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)menu
, "Game");
453 AppendMenu(menu
, MF_ENABLED
, IDM_NEW
, "New");
454 AppendMenu(menu
, MF_ENABLED
, IDM_RESTART
, "Restart");
455 AppendMenu(menu
, MF_ENABLED
, IDM_SEED
, "Specific...");
457 if ((fe
->npresets
= midend_num_presets(fe
->me
)) > 0 ||
458 thegame
.can_configure
) {
459 HMENU sub
= CreateMenu();
462 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)sub
, "Type");
464 fe
->presets
= snewn(fe
->npresets
, game_params
*);
466 for (i
= 0; i
< fe
->npresets
; i
++) {
469 midend_fetch_preset(fe
->me
, i
, &name
, &fe
->presets
[i
]);
472 * FIXME: we ought to go through and do something
473 * with ampersands here.
476 AppendMenu(sub
, MF_ENABLED
, IDM_PRESETS
+ 0x10 * i
, name
);
479 if (thegame
.can_configure
) {
480 AppendMenu(sub
, MF_ENABLED
, IDM_CONFIG
, "Custom...");
484 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
485 AppendMenu(menu
, MF_ENABLED
, IDM_UNDO
, "Undo");
486 AppendMenu(menu
, MF_ENABLED
, IDM_REDO
, "Redo");
487 if (thegame
.can_format_as_text
) {
488 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
489 AppendMenu(menu
, MF_ENABLED
, IDM_COPY
, "Copy");
491 if (thegame
.can_solve
) {
492 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
493 AppendMenu(menu
, MF_ENABLED
, IDM_SOLVE
, "Solve");
495 AppendMenu(menu
, MF_SEPARATOR
, 0, 0);
496 AppendMenu(menu
, MF_ENABLED
, IDM_QUIT
, "Exit");
498 HMENU hmenu
= CreateMenu();
499 AppendMenu(bar
, MF_ENABLED
|MF_POPUP
, (UINT
)hmenu
, "Help");
500 AppendMenu(hmenu
, MF_ENABLED
, IDM_HELPC
, "Contents");
501 if (thegame
.winhelp_topic
) {
503 assert(thegame
.name
);
504 item
= snewn(9+strlen(thegame
.name
), char); /*ick*/
505 sprintf(item
, "Help on %s", thegame
.name
);
506 AppendMenu(hmenu
, MF_ENABLED
, IDM_GAMEHELP
, item
);
510 SetMenu(fe
->hwnd
, bar
);
513 if (midend_wants_statusbar(fe
->me
)) {
514 fe
->statusbar
= CreateWindowEx(0, STATUSCLASSNAME
, "ooh",
515 WS_CHILD
| WS_VISIBLE
,
516 0, 0, 0, 0, /* status bar does these */
517 fe
->hwnd
, NULL
, inst
, NULL
);
518 GetWindowRect(fe
->statusbar
, &sr
);
519 SetWindowPos(fe
->hwnd
, NULL
, 0, 0,
520 r
.right
- r
.left
, r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
521 SWP_NOMOVE
| SWP_NOZORDER
);
522 SetWindowPos(fe
->statusbar
, NULL
, 0, y
, x
, sr
.bottom
- sr
.top
,
525 fe
->statusbar
= NULL
;
528 hdc
= GetDC(fe
->hwnd
);
529 fe
->bitmap
= CreateCompatibleBitmap(hdc
, x
, y
);
530 ReleaseDC(fe
->hwnd
, hdc
);
532 SetWindowLong(fe
->hwnd
, GWL_USERDATA
, (LONG
)fe
);
534 ShowWindow(fe
->hwnd
, SW_NORMAL
);
535 SetForegroundWindow(fe
->hwnd
);
537 midend_redraw(fe
->me
);
542 static int CALLBACK
ConfigDlgProc(HWND hwnd
, UINT msg
,
543 WPARAM wParam
, LPARAM lParam
)
545 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
555 * OK and Cancel are special cases.
557 if ((HIWORD(wParam
) == BN_CLICKED
||
558 HIWORD(wParam
) == BN_DOUBLECLICKED
) &&
559 (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)) {
560 if (LOWORD(wParam
) == IDOK
) {
561 char *err
= midend_set_config(fe
->me
, fe
->cfg_which
, fe
->cfg
);
564 MessageBox(hwnd
, err
, "Validation error",
565 MB_ICONERROR
| MB_OK
);
576 * First find the control whose id this is.
578 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
579 if (j
->ctlid
== LOWORD(wParam
))
582 if (i
->type
== C_END
)
583 return 0; /* not our problem */
585 if (i
->type
== C_STRING
&& HIWORD(wParam
) == EN_CHANGE
) {
587 GetDlgItemText(fe
->cfgbox
, j
->ctlid
, buffer
, lenof(buffer
));
588 buffer
[lenof(buffer
)-1] = '\0';
590 i
->sval
= dupstr(buffer
);
591 } else if (i
->type
== C_BOOLEAN
&&
592 (HIWORD(wParam
) == BN_CLICKED
||
593 HIWORD(wParam
) == BN_DOUBLECLICKED
)) {
594 i
->ival
= IsDlgButtonChecked(fe
->cfgbox
, j
->ctlid
);
595 } else if (i
->type
== C_CHOICES
&&
596 HIWORD(wParam
) == CBN_SELCHANGE
) {
597 i
->ival
= SendDlgItemMessage(fe
->cfgbox
, j
->ctlid
,
611 HWND
mkctrl(frontend
*fe
, int x1
, int x2
, int y1
, int y2
,
612 char *wclass
, int wstyle
,
613 int exstyle
, char *wtext
, int wid
)
616 ret
= CreateWindowEx(exstyle
, wclass
, wtext
,
617 wstyle
| WS_CHILD
| WS_VISIBLE
, x1
, y1
, x2
-x1
, y2
-y1
,
618 fe
->cfgbox
, (HMENU
) wid
, fe
->inst
, NULL
);
619 SendMessage(ret
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, MAKELPARAM(TRUE
, 0));
623 static int get_config(frontend
*fe
, int which
)
636 int winwidth
, winheight
, col1l
, col1r
, col2l
, col2r
, y
;
637 int height
, width
, maxlabel
, maxcheckbox
;
639 wc
.style
= CS_DBLCLKS
| CS_SAVEBITS
| CS_BYTEALIGNWINDOW
;
640 wc
.lpfnWndProc
= DefDlgProc
;
642 wc
.cbWndExtra
= DLGWINDOWEXTRA
+ 8;
643 wc
.hInstance
= fe
->inst
;
645 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
646 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+1);
647 wc
.lpszMenuName
= NULL
;
648 wc
.lpszClassName
= "GameConfigBox";
651 hdc
= GetDC(fe
->hwnd
);
652 SetMapMode(hdc
, MM_TEXT
);
654 fe
->cfg_done
= FALSE
;
656 fe
->cfgfont
= CreateFont(-MulDiv(8, GetDeviceCaps(hdc
, LOGPIXELSY
), 72),
658 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
,
659 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
664 oldfont
= SelectObject(hdc
, fe
->cfgfont
);
665 if (GetTextMetrics(hdc
, &tm
)) {
666 height
= tm
.tmAscent
+ tm
.tmDescent
;
667 width
= tm
.tmAveCharWidth
;
672 fe
->cfg
= midend_get_config(fe
->me
, which
, &title
);
673 fe
->cfg_which
= which
;
676 * Figure out the layout of the config box by measuring the
677 * length of each piece of text.
679 maxlabel
= maxcheckbox
= 0;
680 winheight
= height
/2;
682 for (i
= fe
->cfg
; i
->type
!= C_END
; i
++) {
687 * Both these control types have a label filling only
688 * the left-hand column of the box.
690 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
693 winheight
+= height
* 3 / 2 + (height
/ 2);
698 * Checkboxes take up the whole of the box width.
700 if (GetTextExtentPoint32(hdc
, i
->name
, strlen(i
->name
), &size
) &&
701 maxcheckbox
< size
.cx
)
702 maxcheckbox
= size
.cx
;
703 winheight
+= height
+ (height
/ 2);
708 winheight
+= height
+ height
* 7 / 4; /* OK / Cancel buttons */
711 col1r
= col1l
+ maxlabel
;
712 col2l
= col1r
+ 2*width
;
713 col2r
= col2l
+ 30*width
;
714 if (col2r
< col1l
+2*height
+maxcheckbox
)
715 col2r
= col1l
+2*height
+maxcheckbox
;
716 winwidth
= col2r
+ 2*width
;
718 ReleaseDC(fe
->hwnd
, hdc
);
721 * Create the dialog, now that we know its size.
728 r
.bottom
= winheight
;
730 AdjustWindowRectEx(&r
, (WS_OVERLAPPEDWINDOW
/*|
731 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
732 WS_CAPTION | WS_SYSMENU*/) &~
733 (WS_MAXIMIZEBOX
| WS_OVERLAPPED
),
737 * Centre the dialog on its parent window.
741 GetWindowRect(fe
->hwnd
, &r2
);
742 r
.left
= (r2
.left
+ r2
.right
- r
.right
) / 2;
743 r
.top
= (r2
.top
+ r2
.bottom
- r
.bottom
) / 2;
747 fe
->cfgbox
= CreateWindowEx(0, wc
.lpszClassName
, title
,
748 DS_MODALFRAME
| WS_POPUP
| WS_VISIBLE
|
749 WS_CAPTION
| WS_SYSMENU
,
751 r
.right
-r
.left
, r
.bottom
-r
.top
,
752 fe
->hwnd
, NULL
, fe
->inst
, NULL
);
756 SendMessage(fe
->cfgbox
, WM_SETFONT
, (WPARAM
)fe
->cfgfont
, FALSE
);
758 SetWindowLong(fe
->cfgbox
, GWL_USERDATA
, (LONG
)fe
);
759 SetWindowLong(fe
->cfgbox
, DWL_DLGPROC
, (LONG
)ConfigDlgProc
);
762 * Count the controls so we can allocate cfgaux.
764 for (nctrls
= 0, i
= fe
->cfg
; i
->type
!= C_END
; i
++)
766 fe
->cfgaux
= snewn(nctrls
, struct cfg_aux
);
770 for (i
= fe
->cfg
, j
= fe
->cfgaux
; i
->type
!= C_END
; i
++, j
++) {
774 * Edit box with a label beside it.
776 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
777 "Static", 0, 0, i
->name
, id
++);
778 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*3/2,
779 "EDIT", WS_TABSTOP
| ES_AUTOHSCROLL
,
780 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
781 SetWindowText(ctl
, i
->sval
);
789 mkctrl(fe
, col1l
, col2r
, y
, y
+height
, "BUTTON",
790 BS_NOTIFY
| BS_AUTOCHECKBOX
| WS_TABSTOP
,
791 0, i
->name
, (j
->ctlid
= id
++));
792 CheckDlgButton(fe
->cfgbox
, j
->ctlid
, (i
->ival
!= 0));
798 * Drop-down list with a label beside it.
800 mkctrl(fe
, col1l
, col1r
, y
+height
*1/8, y
+height
*9/8,
801 "STATIC", 0, 0, i
->name
, id
++);
802 ctl
= mkctrl(fe
, col2l
, col2r
, y
, y
+height
*41/2,
803 "COMBOBOX", WS_TABSTOP
|
804 CBS_DROPDOWNLIST
| CBS_HASSTRINGS
,
805 WS_EX_CLIENTEDGE
, "", (j
->ctlid
= id
++));
807 char c
, *p
, *q
, *str
;
809 SendMessage(ctl
, CB_RESETCONTENT
, 0, 0);
814 while (*q
&& *q
!= c
) q
++;
815 str
= snewn(q
-p
+1, char);
816 strncpy(str
, p
, q
-p
);
818 SendMessage(ctl
, CB_ADDSTRING
, 0, (LPARAM
)str
);
825 SendMessage(ctl
, CB_SETCURSEL
, i
->ival
, 0);
831 assert(y
< winheight
);
835 y
+= height
/2; /* extra space before OK and Cancel */
836 mkctrl(fe
, col1l
, (col1l
+col2r
)/2-width
, y
, y
+height
*7/4, "BUTTON",
837 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
| BS_DEFPUSHBUTTON
, 0,
839 mkctrl(fe
, (col1l
+col2r
)/2+width
, col2r
, y
, y
+height
*7/4, "BUTTON",
840 BS_PUSHBUTTON
| BS_NOTIFY
| WS_TABSTOP
, 0, "Cancel", IDCANCEL
);
842 SendMessage(fe
->cfgbox
, WM_INITDIALOG
, 0, 0);
844 EnableWindow(fe
->hwnd
, FALSE
);
845 ShowWindow(fe
->cfgbox
, SW_NORMAL
);
846 while ((gm
=GetMessage(&msg
, NULL
, 0, 0)) > 0) {
847 if (!IsDialogMessage(fe
->cfgbox
, &msg
))
848 DispatchMessage(&msg
);
852 EnableWindow(fe
->hwnd
, TRUE
);
853 SetForegroundWindow(fe
->hwnd
);
854 DestroyWindow(fe
->cfgbox
);
855 DeleteObject(fe
->cfgfont
);
860 return (fe
->cfg_done
== 2);
863 static void new_game_type(frontend
*fe
)
869 midend_new_game(fe
->me
);
870 midend_size(fe
->me
, &x
, &y
);
875 AdjustWindowRectEx(&r
, WS_OVERLAPPEDWINDOW
&~
876 (WS_THICKFRAME
| WS_MAXIMIZEBOX
|
880 if (fe
->statusbar
!= NULL
) {
881 GetWindowRect(fe
->statusbar
, &sr
);
883 sr
.left
= sr
.right
= sr
.top
= sr
.bottom
= 0;
885 SetWindowPos(fe
->hwnd
, NULL
, 0, 0,
887 r
.bottom
- r
.top
+ sr
.bottom
- sr
.top
,
888 SWP_NOMOVE
| SWP_NOZORDER
);
889 if (fe
->statusbar
!= NULL
)
890 SetWindowPos(fe
->statusbar
, NULL
, 0, y
, x
,
891 sr
.bottom
- sr
.top
, SWP_NOZORDER
);
893 DeleteObject(fe
->bitmap
);
895 hdc
= GetDC(fe
->hwnd
);
896 fe
->bitmap
= CreateCompatibleBitmap(hdc
, x
, y
);
897 ReleaseDC(fe
->hwnd
, hdc
);
899 midend_redraw(fe
->me
);
902 static LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
,
903 WPARAM wParam
, LPARAM lParam
)
905 frontend
*fe
= (frontend
*)GetWindowLong(hwnd
, GWL_USERDATA
);
912 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
914 if (!midend_process_key(fe
->me
, 0, 0, 'n'))
918 if (!midend_process_key(fe
->me
, 0, 0, 'r'))
922 if (!midend_process_key(fe
->me
, 0, 0, 'u'))
926 if (!midend_process_key(fe
->me
, 0, 0, '\x12'))
931 char *text
= midend_text_format(fe
->me
);
933 write_clip(hwnd
, text
);
935 MessageBeep(MB_ICONWARNING
);
940 char *msg
= midend_solve(fe
->me
);
942 MessageBox(hwnd
, msg
, "Unable to solve",
943 MB_ICONERROR
| MB_OK
);
947 if (!midend_process_key(fe
->me
, 0, 0, 'q'))
951 if (get_config(fe
, CFG_SETTINGS
))
955 if (get_config(fe
, CFG_SEED
))
959 assert(fe
->help_path
);
960 WinHelp(hwnd
, fe
->help_path
,
961 fe
->help_has_contents ? HELP_FINDER
: HELP_CONTENTS
, 0);
964 assert(fe
->help_path
);
965 assert(thegame
.winhelp_topic
);
967 char *cmd
= snewn(10+strlen(thegame
.winhelp_topic
), char);
968 sprintf(cmd
, "JI(`',`%s')", thegame
.winhelp_topic
);
969 WinHelp(hwnd
, fe
->help_path
, HELP_COMMAND
, (DWORD
)cmd
);
975 int p
= ((wParam
&~ 0xF) - IDM_PRESETS
) / 0x10;
977 if (p
>= 0 && p
< fe
->npresets
) {
978 midend_set_params(fe
->me
, fe
->presets
[p
]);
994 hdc
= BeginPaint(hwnd
, &p
);
995 hdc2
= CreateCompatibleDC(hdc
);
996 prevbm
= SelectObject(hdc2
, fe
->bitmap
);
998 p
.rcPaint
.left
, p
.rcPaint
.top
,
999 p
.rcPaint
.right
- p
.rcPaint
.left
,
1000 p
.rcPaint
.bottom
- p
.rcPaint
.top
,
1002 p
.rcPaint
.left
, p
.rcPaint
.top
,
1004 SelectObject(hdc2
, prevbm
);
1014 case VK_LEFT
: key
= CURSOR_LEFT
; break;
1015 case VK_RIGHT
: key
= CURSOR_RIGHT
; break;
1016 case VK_UP
: key
= CURSOR_UP
; break;
1017 case VK_DOWN
: key
= CURSOR_DOWN
; break;
1019 * Diagonal keys on the numeric keypad.
1022 if (!(lParam
& 0x01000000)) key
= CURSOR_UP_RIGHT
;
1025 if (!(lParam
& 0x01000000)) key
= CURSOR_DOWN_RIGHT
;
1028 if (!(lParam
& 0x01000000)) key
= CURSOR_UP_LEFT
;
1031 if (!(lParam
& 0x01000000)) key
= CURSOR_DOWN_LEFT
;
1034 * Numeric keypad keys with Num Lock on.
1036 case VK_NUMPAD4
: key
= CURSOR_LEFT
; break;
1037 case VK_NUMPAD6
: key
= CURSOR_RIGHT
; break;
1038 case VK_NUMPAD8
: key
= CURSOR_UP
; break;
1039 case VK_NUMPAD2
: key
= CURSOR_DOWN
; break;
1040 case VK_NUMPAD9
: key
= CURSOR_UP_RIGHT
; break;
1041 case VK_NUMPAD3
: key
= CURSOR_DOWN_RIGHT
; break;
1042 case VK_NUMPAD7
: key
= CURSOR_UP_LEFT
; break;
1043 case VK_NUMPAD1
: key
= CURSOR_DOWN_LEFT
; break;
1047 if (!midend_process_key(fe
->me
, 0, 0, key
))
1052 m
.message
= WM_KEYDOWN
;
1054 m
.lParam
= lParam
& 0xdfff;
1055 TranslateMessage(&m
);
1059 case WM_LBUTTONDOWN
:
1060 case WM_RBUTTONDOWN
:
1061 case WM_MBUTTONDOWN
:
1066 * Shift-clicks count as middle-clicks, since otherwise
1067 * two-button Windows users won't have any kind of
1068 * middle click to use.
1070 if (message
== WM_MBUTTONDOWN
|| (wParam
& MK_SHIFT
))
1071 button
= MIDDLE_BUTTON
;
1072 else if (message
== WM_LBUTTONDOWN
)
1073 button
= LEFT_BUTTON
;
1075 button
= RIGHT_BUTTON
;
1077 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1078 (signed short)HIWORD(lParam
), button
))
1091 * Shift-clicks count as middle-clicks, since otherwise
1092 * two-button Windows users won't have any kind of
1093 * middle click to use.
1095 if (message
== WM_MBUTTONUP
|| (wParam
& MK_SHIFT
))
1096 button
= MIDDLE_RELEASE
;
1097 else if (message
== WM_LBUTTONUP
)
1098 button
= LEFT_RELEASE
;
1100 button
= RIGHT_RELEASE
;
1102 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1103 (signed short)HIWORD(lParam
), button
))
1113 if (wParam
& (MK_MBUTTON
| MK_SHIFT
))
1114 button
= MIDDLE_DRAG
;
1115 else if (wParam
& MK_LBUTTON
)
1118 button
= RIGHT_DRAG
;
1120 if (!midend_process_key(fe
->me
, (signed short)LOWORD(lParam
),
1121 (signed short)HIWORD(lParam
), button
))
1126 if (!midend_process_key(fe
->me
, 0, 0, (unsigned char)wParam
))
1131 DWORD now
= GetTickCount();
1132 float elapsed
= (float) (now
- fe
->timer_last_tickcount
) * 0.001F
;
1133 midend_timer(fe
->me
, elapsed
);
1134 fe
->timer_last_tickcount
= now
;
1139 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
1142 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
)
1147 InitCommonControls();
1153 wndclass
.lpfnWndProc
= WndProc
;
1154 wndclass
.cbClsExtra
= 0;
1155 wndclass
.cbWndExtra
= 0;
1156 wndclass
.hInstance
= inst
;
1157 wndclass
.hIcon
= LoadIcon(inst
, IDI_APPLICATION
);
1158 wndclass
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
1159 wndclass
.hbrBackground
= NULL
;
1160 wndclass
.lpszMenuName
= NULL
;
1161 wndclass
.lpszClassName
= thegame
.name
;
1163 RegisterClass(&wndclass
);
1166 while (*cmdline
&& isspace(*cmdline
))
1169 if (!new_window(inst
, *cmdline ? cmdline
: NULL
, &error
)) {
1171 sprintf(buf
, "%.100s Error", thegame
.name
);
1172 MessageBox(NULL
, error
, buf
, MB_OK
|MB_ICONERROR
);
1176 while (GetMessage(&msg
, NULL
, 0, 0)) {
1177 DispatchMessage(&msg
);