720a8fb7 |
1 | /* |
2 | * windows.c: Windows front end for my puzzle collection. |
be8d5aa1 |
3 | * |
4 | * TODO: |
5 | * |
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. |
720a8fb7 |
12 | */ |
4e7ef6e6 |
13 | |
14 | #include <windows.h> |
fd1a1a2b |
15 | #include <commctrl.h> |
4e7ef6e6 |
16 | |
17 | #include <stdio.h> |
4efb3868 |
18 | #include <assert.h> |
8b7938e7 |
19 | #include <ctype.h> |
4e7ef6e6 |
20 | #include <stdarg.h> |
21 | #include <stdlib.h> |
1e3e152d |
22 | #include <limits.h> |
4e7ef6e6 |
23 | #include <time.h> |
24 | |
25 | #include "puzzles.h" |
26 | |
eb2ad6f1 |
27 | #define IDM_NEW 0x0010 |
28 | #define IDM_RESTART 0x0020 |
29 | #define IDM_UNDO 0x0030 |
30 | #define IDM_REDO 0x0040 |
1cdfcdbd |
31 | #define IDM_COPY 0x0050 |
2ac6d24e |
32 | #define IDM_SOLVE 0x0060 |
33 | #define IDM_QUIT 0x0070 |
34 | #define IDM_CONFIG 0x0080 |
1185e3c5 |
35 | #define IDM_DESC 0x0090 |
36 | #define IDM_SEED 0x00A0 |
37 | #define IDM_HELPC 0x00B0 |
38 | #define IDM_GAMEHELP 0x00C0 |
fa7515b2 |
39 | #define IDM_ABOUT 0x00D0 |
eb2ad6f1 |
40 | #define IDM_PRESETS 0x0100 |
41 | |
e91825f8 |
42 | #define HELP_FILE_NAME "puzzles.hlp" |
43 | #define HELP_CNT_NAME "puzzles.cnt" |
44 | |
c71454c0 |
45 | #ifdef DEBUG |
46 | static FILE *debug_fp = NULL; |
47 | static HANDLE debug_hdl = INVALID_HANDLE_VALUE; |
48 | static int debug_got_console = 0; |
49 | |
50 | void dputs(char *buf) |
51 | { |
52 | DWORD dw; |
53 | |
54 | if (!debug_got_console) { |
55 | if (AllocConsole()) { |
56 | debug_got_console = 1; |
57 | debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE); |
58 | } |
59 | } |
60 | if (!debug_fp) { |
61 | debug_fp = fopen("debug.log", "w"); |
62 | } |
63 | |
64 | if (debug_hdl != INVALID_HANDLE_VALUE) { |
65 | WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL); |
66 | } |
67 | fputs(buf, debug_fp); |
68 | fflush(debug_fp); |
69 | } |
70 | |
71 | void debug_printf(char *fmt, ...) |
72 | { |
73 | char buf[4096]; |
74 | va_list ap; |
75 | |
76 | va_start(ap, fmt); |
77 | vsprintf(buf, fmt, ap); |
78 | dputs(buf); |
79 | va_end(ap); |
80 | } |
c71454c0 |
81 | #endif |
82 | |
4efb3868 |
83 | struct font { |
84 | HFONT font; |
85 | int type; |
86 | int size; |
87 | }; |
88 | |
077f3cbe |
89 | struct cfg_aux { |
90 | int ctlid; |
91 | }; |
92 | |
4e7ef6e6 |
93 | struct frontend { |
94 | midend_data *me; |
077f3cbe |
95 | HWND hwnd, statusbar, cfgbox; |
96 | HINSTANCE inst; |
4e7ef6e6 |
97 | HBITMAP bitmap, prevbm; |
98 | HDC hdc_bm; |
99 | COLORREF *colours; |
100 | HBRUSH *brushes; |
101 | HPEN *pens; |
4efb3868 |
102 | HRGN clip; |
4e7ef6e6 |
103 | UINT timer; |
8c1fd974 |
104 | DWORD timer_last_tickcount; |
eb2ad6f1 |
105 | int npresets; |
106 | game_params **presets; |
4efb3868 |
107 | struct font *fonts; |
108 | int nfonts, fontsize; |
077f3cbe |
109 | config_item *cfg; |
110 | struct cfg_aux *cfgaux; |
97098757 |
111 | int cfg_which, dlg_done; |
077f3cbe |
112 | HFONT cfgfont; |
e91825f8 |
113 | char *help_path; |
114 | int help_has_contents; |
48dcdd62 |
115 | char *laststatus; |
4e7ef6e6 |
116 | }; |
117 | |
118 | void fatal(char *fmt, ...) |
119 | { |
120 | char buf[2048]; |
121 | va_list ap; |
122 | |
123 | va_start(ap, fmt); |
124 | vsprintf(buf, fmt, ap); |
125 | va_end(ap); |
126 | |
127 | MessageBox(NULL, buf, "Fatal error", MB_ICONEXCLAMATION | MB_OK); |
128 | |
129 | exit(1); |
130 | } |
131 | |
cbb5549e |
132 | void get_random_seed(void **randseed, int *randseedsize) |
133 | { |
134 | time_t *tp = snew(time_t); |
135 | time(tp); |
136 | *randseed = (void *)tp; |
137 | *randseedsize = sizeof(time_t); |
138 | } |
139 | |
fd1a1a2b |
140 | void status_bar(frontend *fe, char *text) |
141 | { |
48dcdd62 |
142 | char *rewritten = midend_rewrite_statusbar(fe->me, text); |
143 | if (!fe->laststatus || strcmp(rewritten, fe->laststatus)) { |
144 | SetWindowText(fe->statusbar, rewritten); |
145 | sfree(fe->laststatus); |
146 | fe->laststatus = rewritten; |
147 | } else { |
148 | sfree(rewritten); |
149 | } |
fd1a1a2b |
150 | } |
151 | |
4e7ef6e6 |
152 | void frontend_default_colour(frontend *fe, float *output) |
153 | { |
154 | DWORD c = GetSysColor(COLOR_MENU); /* ick */ |
155 | |
156 | output[0] = (float)(GetRValue(c) / 255.0); |
157 | output[1] = (float)(GetGValue(c) / 255.0); |
158 | output[2] = (float)(GetBValue(c) / 255.0); |
159 | } |
160 | |
4efb3868 |
161 | void clip(frontend *fe, int x, int y, int w, int h) |
162 | { |
4efb3868 |
163 | IntersectClipRect(fe->hdc_bm, x, y, x+w, y+h); |
164 | } |
165 | |
166 | void unclip(frontend *fe) |
167 | { |
ea25d9da |
168 | SelectClipRgn(fe->hdc_bm, NULL); |
4efb3868 |
169 | } |
170 | |
171 | void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize, |
172 | int align, int colour, char *text) |
173 | { |
174 | int i; |
175 | |
176 | /* |
177 | * Find or create the font. |
178 | */ |
179 | for (i = 0; i < fe->nfonts; i++) |
180 | if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize) |
181 | break; |
182 | |
183 | if (i == fe->nfonts) { |
184 | if (fe->fontsize <= fe->nfonts) { |
185 | fe->fontsize = fe->nfonts + 10; |
186 | fe->fonts = sresize(fe->fonts, fe->fontsize, struct font); |
187 | } |
188 | |
189 | fe->nfonts++; |
190 | |
191 | fe->fonts[i].type = fonttype; |
192 | fe->fonts[i].size = fontsize; |
193 | |
f17b8728 |
194 | fe->fonts[i].font = CreateFont(-fontsize, 0, 0, 0, FW_BOLD, |
4efb3868 |
195 | FALSE, FALSE, FALSE, DEFAULT_CHARSET, |
196 | OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, |
197 | DEFAULT_QUALITY, |
198 | (fonttype == FONT_FIXED ? |
199 | FIXED_PITCH | FF_DONTCARE : |
200 | VARIABLE_PITCH | FF_SWISS), |
201 | NULL); |
202 | } |
203 | |
204 | /* |
205 | * Position and draw the text. |
206 | */ |
207 | { |
208 | HFONT oldfont; |
209 | TEXTMETRIC tm; |
210 | SIZE size; |
211 | |
212 | oldfont = SelectObject(fe->hdc_bm, fe->fonts[i].font); |
213 | if (GetTextMetrics(fe->hdc_bm, &tm)) { |
214 | if (align & ALIGN_VCENTRE) |
215 | y -= (tm.tmAscent+tm.tmDescent)/2; |
216 | else |
217 | y -= tm.tmAscent; |
218 | } |
219 | if (GetTextExtentPoint32(fe->hdc_bm, text, strlen(text), &size)) { |
220 | if (align & ALIGN_HCENTRE) |
221 | x -= size.cx / 2; |
222 | else if (align & ALIGN_HRIGHT) |
223 | x -= size.cx; |
224 | } |
225 | SetBkMode(fe->hdc_bm, TRANSPARENT); |
0572b5d8 |
226 | SetTextColor(fe->hdc_bm, fe->colours[colour]); |
4efb3868 |
227 | TextOut(fe->hdc_bm, x, y, text, strlen(text)); |
228 | SelectObject(fe->hdc_bm, oldfont); |
229 | } |
230 | } |
231 | |
4e7ef6e6 |
232 | void draw_rect(frontend *fe, int x, int y, int w, int h, int colour) |
233 | { |
fbdd7610 |
234 | if (w == 1 && h == 1) { |
235 | /* |
236 | * Rectangle() appears to get uppity if asked to draw a 1x1 |
237 | * rectangle, presumably on the grounds that that's beneath |
238 | * its dignity and you ought to be using SetPixel instead. |
239 | * So I will. |
240 | */ |
241 | SetPixel(fe->hdc_bm, x, y, fe->colours[colour]); |
242 | } else { |
243 | HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[colour]); |
244 | HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]); |
245 | Rectangle(fe->hdc_bm, x, y, x+w, y+h); |
246 | SelectObject(fe->hdc_bm, oldbrush); |
247 | SelectObject(fe->hdc_bm, oldpen); |
248 | } |
4e7ef6e6 |
249 | } |
250 | |
251 | void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour) |
252 | { |
253 | HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]); |
254 | MoveToEx(fe->hdc_bm, x1, y1, NULL); |
255 | LineTo(fe->hdc_bm, x2, y2); |
256 | SetPixel(fe->hdc_bm, x2, y2, fe->colours[colour]); |
257 | SelectObject(fe->hdc_bm, oldpen); |
258 | } |
259 | |
260 | void draw_polygon(frontend *fe, int *coords, int npoints, |
261 | int fill, int colour) |
262 | { |
263 | POINT *pts = snewn(npoints+1, POINT); |
264 | int i; |
265 | |
266 | for (i = 0; i <= npoints; i++) { |
267 | int j = (i < npoints ? i : 0); |
268 | pts[i].x = coords[j*2]; |
269 | pts[i].y = coords[j*2+1]; |
270 | } |
271 | |
272 | if (fill) { |
273 | HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[colour]); |
274 | HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]); |
275 | Polygon(fe->hdc_bm, pts, npoints); |
276 | SelectObject(fe->hdc_bm, oldbrush); |
277 | SelectObject(fe->hdc_bm, oldpen); |
278 | } else { |
279 | HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]); |
280 | Polyline(fe->hdc_bm, pts, npoints+1); |
281 | SelectObject(fe->hdc_bm, oldpen); |
282 | } |
283 | |
284 | sfree(pts); |
285 | } |
286 | |
287 | void start_draw(frontend *fe) |
288 | { |
289 | HDC hdc_win; |
290 | hdc_win = GetDC(fe->hwnd); |
291 | fe->hdc_bm = CreateCompatibleDC(hdc_win); |
292 | fe->prevbm = SelectObject(fe->hdc_bm, fe->bitmap); |
293 | ReleaseDC(fe->hwnd, hdc_win); |
4efb3868 |
294 | fe->clip = NULL; |
077f3cbe |
295 | SetMapMode(fe->hdc_bm, MM_TEXT); |
4e7ef6e6 |
296 | } |
297 | |
298 | void draw_update(frontend *fe, int x, int y, int w, int h) |
299 | { |
300 | RECT r; |
301 | |
302 | r.left = x; |
303 | r.top = y; |
304 | r.right = x + w; |
305 | r.bottom = y + h; |
306 | |
307 | InvalidateRect(fe->hwnd, &r, FALSE); |
308 | } |
309 | |
310 | void end_draw(frontend *fe) |
311 | { |
312 | SelectObject(fe->hdc_bm, fe->prevbm); |
313 | DeleteDC(fe->hdc_bm); |
4efb3868 |
314 | if (fe->clip) { |
315 | DeleteObject(fe->clip); |
316 | fe->clip = NULL; |
317 | } |
4e7ef6e6 |
318 | } |
319 | |
320 | void deactivate_timer(frontend *fe) |
321 | { |
322 | KillTimer(fe->hwnd, fe->timer); |
323 | fe->timer = 0; |
324 | } |
325 | |
326 | void activate_timer(frontend *fe) |
327 | { |
8c1fd974 |
328 | if (!fe->timer) { |
329 | fe->timer = SetTimer(fe->hwnd, fe->timer, 20, NULL); |
330 | fe->timer_last_tickcount = GetTickCount(); |
331 | } |
4e7ef6e6 |
332 | } |
333 | |
1cdfcdbd |
334 | void write_clip(HWND hwnd, char *data) |
335 | { |
336 | HGLOBAL clipdata; |
6d5cbf79 |
337 | int len, i, j; |
338 | char *data2; |
1cdfcdbd |
339 | void *lock; |
340 | |
6d5cbf79 |
341 | /* |
342 | * Windows expects CRLF in the clipboard, so we must convert |
343 | * any \n that has come out of the puzzle backend. |
344 | */ |
345 | len = 0; |
346 | for (i = 0; data[i]; i++) { |
347 | if (data[i] == '\n') |
348 | len++; |
349 | len++; |
350 | } |
351 | data2 = snewn(len+1, char); |
352 | j = 0; |
353 | for (i = 0; data[i]; i++) { |
354 | if (data[i] == '\n') |
355 | data2[j++] = '\r'; |
356 | data2[j++] = data[i]; |
357 | } |
358 | assert(j == len); |
359 | data2[j] = '\0'; |
360 | |
1cdfcdbd |
361 | clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1); |
362 | if (!clipdata) |
363 | return; |
364 | lock = GlobalLock(clipdata); |
365 | if (!lock) |
366 | return; |
6d5cbf79 |
367 | memcpy(lock, data2, len); |
1cdfcdbd |
368 | ((unsigned char *) lock)[len] = 0; |
369 | GlobalUnlock(clipdata); |
370 | |
371 | if (OpenClipboard(hwnd)) { |
372 | EmptyClipboard(); |
373 | SetClipboardData(CF_TEXT, clipdata); |
374 | CloseClipboard(); |
375 | } else |
376 | GlobalFree(clipdata); |
6d5cbf79 |
377 | |
378 | sfree(data2); |
1cdfcdbd |
379 | } |
380 | |
e91825f8 |
381 | /* |
382 | * See if we can find a help file. |
383 | */ |
384 | static void find_help_file(frontend *fe) |
385 | { |
386 | char b[2048], *p, *q, *r; |
387 | FILE *fp; |
388 | if (!fe->help_path) { |
389 | GetModuleFileName(NULL, b, sizeof(b) - 1); |
390 | r = b; |
391 | p = strrchr(b, '\\'); |
392 | if (p && p >= r) r = p+1; |
393 | q = strrchr(b, ':'); |
394 | if (q && q >= r) r = q+1; |
395 | strcpy(r, HELP_FILE_NAME); |
396 | if ( (fp = fopen(b, "r")) != NULL) { |
397 | fe->help_path = dupstr(b); |
398 | fclose(fp); |
399 | } else |
400 | fe->help_path = NULL; |
401 | strcpy(r, HELP_CNT_NAME); |
402 | if ( (fp = fopen(b, "r")) != NULL) { |
403 | fe->help_has_contents = TRUE; |
404 | fclose(fp); |
405 | } else |
406 | fe->help_has_contents = FALSE; |
407 | } |
408 | } |
409 | |
1e3e152d |
410 | static void check_window_size(frontend *fe, int *px, int *py) |
411 | { |
412 | RECT r; |
413 | int x, y, sy; |
414 | |
415 | if (fe->statusbar) { |
416 | RECT sr; |
417 | GetWindowRect(fe->statusbar, &sr); |
418 | sy = sr.bottom - sr.top; |
419 | } else { |
420 | sy = 0; |
421 | } |
422 | |
423 | /* |
424 | * See if we actually got the window size we wanted, and adjust |
425 | * the puzzle size if not. |
426 | */ |
427 | GetClientRect(fe->hwnd, &r); |
428 | x = r.right - r.left; |
429 | y = r.bottom - r.top - sy; |
430 | midend_size(fe->me, &x, &y, FALSE); |
431 | if (x != r.right - r.left || y != r.bottom - r.top) { |
432 | /* |
433 | * Resize the window, now we know what size we _really_ |
434 | * want it to be. |
435 | */ |
436 | r.left = r.top = 0; |
437 | r.right = x; |
438 | r.bottom = y + sy; |
439 | AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~ |
440 | (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED), |
441 | TRUE, 0); |
442 | SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left, r.bottom - r.top, |
443 | SWP_NOMOVE | SWP_NOZORDER); |
444 | } |
445 | |
446 | if (fe->statusbar) { |
447 | GetClientRect(fe->hwnd, &r); |
448 | SetWindowPos(fe->statusbar, NULL, 0, r.bottom-r.top-sy, r.right-r.left, |
449 | sy, SWP_NOZORDER); |
450 | } |
451 | |
452 | *px = x; |
453 | *py = y; |
454 | } |
455 | |
8b7938e7 |
456 | static frontend *new_window(HINSTANCE inst, char *game_id, char **error) |
4e7ef6e6 |
457 | { |
458 | frontend *fe; |
459 | int x, y; |
1e3e152d |
460 | RECT r; |
4e7ef6e6 |
461 | HDC hdc; |
462 | |
463 | fe = snew(frontend); |
48d70ca9 |
464 | |
be8d5aa1 |
465 | fe->me = midend_new(fe, &thegame); |
48d70ca9 |
466 | |
8b7938e7 |
467 | if (game_id) { |
1185e3c5 |
468 | *error = midend_game_id(fe->me, game_id); |
8b7938e7 |
469 | if (*error) { |
470 | midend_free(fe->me); |
471 | sfree(fe); |
472 | return NULL; |
473 | } |
474 | } |
475 | |
e91825f8 |
476 | fe->help_path = NULL; |
477 | find_help_file(fe); |
478 | |
077f3cbe |
479 | fe->inst = inst; |
5928817c |
480 | midend_new_game(fe->me); |
4e7ef6e6 |
481 | |
482 | fe->timer = 0; |
483 | |
6beae131 |
484 | fe->fonts = NULL; |
485 | fe->nfonts = fe->fontsize = 0; |
486 | |
48dcdd62 |
487 | fe->laststatus = NULL; |
488 | |
4e7ef6e6 |
489 | { |
490 | int i, ncolours; |
491 | float *colours; |
492 | |
493 | colours = midend_colours(fe->me, &ncolours); |
494 | |
495 | fe->colours = snewn(ncolours, COLORREF); |
496 | fe->brushes = snewn(ncolours, HBRUSH); |
497 | fe->pens = snewn(ncolours, HPEN); |
498 | |
499 | for (i = 0; i < ncolours; i++) { |
500 | fe->colours[i] = RGB(255 * colours[i*3+0], |
501 | 255 * colours[i*3+1], |
502 | 255 * colours[i*3+2]); |
503 | fe->brushes[i] = CreateSolidBrush(fe->colours[i]); |
4e7ef6e6 |
504 | fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]); |
505 | } |
506 | } |
507 | |
1e3e152d |
508 | x = y = INT_MAX; /* find puzzle's preferred size */ |
509 | midend_size(fe->me, &x, &y, FALSE); |
510 | |
4e7ef6e6 |
511 | r.left = r.top = 0; |
512 | r.right = x; |
513 | r.bottom = y; |
514 | AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~ |
515 | (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED), |
eb2ad6f1 |
516 | TRUE, 0); |
4e7ef6e6 |
517 | |
be8d5aa1 |
518 | fe->hwnd = CreateWindowEx(0, thegame.name, thegame.name, |
4e7ef6e6 |
519 | WS_OVERLAPPEDWINDOW &~ |
520 | (WS_THICKFRAME | WS_MAXIMIZEBOX), |
521 | CW_USEDEFAULT, CW_USEDEFAULT, |
522 | r.right - r.left, r.bottom - r.top, |
523 | NULL, NULL, inst, NULL); |
524 | |
1e3e152d |
525 | if (midend_wants_statusbar(fe->me)) |
526 | fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh", |
527 | WS_CHILD | WS_VISIBLE, |
528 | 0, 0, 0, 0, /* status bar does these */ |
529 | fe->hwnd, NULL, inst, NULL); |
530 | else |
531 | fe->statusbar = NULL; |
532 | |
eb2ad6f1 |
533 | { |
534 | HMENU bar = CreateMenu(); |
535 | HMENU menu = CreateMenu(); |
536 | |
537 | AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Game"); |
538 | AppendMenu(menu, MF_ENABLED, IDM_NEW, "New"); |
539 | AppendMenu(menu, MF_ENABLED, IDM_RESTART, "Restart"); |
1185e3c5 |
540 | AppendMenu(menu, MF_ENABLED, IDM_DESC, "Specific..."); |
541 | AppendMenu(menu, MF_ENABLED, IDM_SEED, "Random Seed..."); |
eb2ad6f1 |
542 | |
077f3cbe |
543 | if ((fe->npresets = midend_num_presets(fe->me)) > 0 || |
be8d5aa1 |
544 | thegame.can_configure) { |
eb2ad6f1 |
545 | HMENU sub = CreateMenu(); |
546 | int i; |
547 | |
077f3cbe |
548 | AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "Type"); |
eb2ad6f1 |
549 | |
550 | fe->presets = snewn(fe->npresets, game_params *); |
551 | |
552 | for (i = 0; i < fe->npresets; i++) { |
553 | char *name; |
554 | |
555 | midend_fetch_preset(fe->me, i, &name, &fe->presets[i]); |
556 | |
557 | /* |
558 | * FIXME: we ought to go through and do something |
559 | * with ampersands here. |
560 | */ |
561 | |
562 | AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name); |
563 | } |
077f3cbe |
564 | |
be8d5aa1 |
565 | if (thegame.can_configure) { |
077f3cbe |
566 | AppendMenu(sub, MF_ENABLED, IDM_CONFIG, "Custom..."); |
567 | } |
eb2ad6f1 |
568 | } |
569 | |
570 | AppendMenu(menu, MF_SEPARATOR, 0, 0); |
571 | AppendMenu(menu, MF_ENABLED, IDM_UNDO, "Undo"); |
572 | AppendMenu(menu, MF_ENABLED, IDM_REDO, "Redo"); |
1cdfcdbd |
573 | if (thegame.can_format_as_text) { |
574 | AppendMenu(menu, MF_SEPARATOR, 0, 0); |
575 | AppendMenu(menu, MF_ENABLED, IDM_COPY, "Copy"); |
576 | } |
2ac6d24e |
577 | if (thegame.can_solve) { |
578 | AppendMenu(menu, MF_SEPARATOR, 0, 0); |
579 | AppendMenu(menu, MF_ENABLED, IDM_SOLVE, "Solve"); |
580 | } |
eb2ad6f1 |
581 | AppendMenu(menu, MF_SEPARATOR, 0, 0); |
582 | AppendMenu(menu, MF_ENABLED, IDM_QUIT, "Exit"); |
97098757 |
583 | menu = CreateMenu(); |
584 | AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Help"); |
585 | AppendMenu(menu, MF_ENABLED, IDM_ABOUT, "About"); |
e91825f8 |
586 | if (fe->help_path) { |
97098757 |
587 | AppendMenu(menu, MF_SEPARATOR, 0, 0); |
588 | AppendMenu(menu, MF_ENABLED, IDM_HELPC, "Contents"); |
be8d5aa1 |
589 | if (thegame.winhelp_topic) { |
e91825f8 |
590 | char *item; |
be8d5aa1 |
591 | assert(thegame.name); |
592 | item = snewn(9+strlen(thegame.name), char); /*ick*/ |
593 | sprintf(item, "Help on %s", thegame.name); |
97098757 |
594 | AppendMenu(menu, MF_ENABLED, IDM_GAMEHELP, item); |
e91825f8 |
595 | sfree(item); |
596 | } |
597 | } |
eb2ad6f1 |
598 | SetMenu(fe->hwnd, bar); |
599 | } |
600 | |
1e3e152d |
601 | check_window_size(fe, &x, &y); |
fd1a1a2b |
602 | |
4e7ef6e6 |
603 | hdc = GetDC(fe->hwnd); |
604 | fe->bitmap = CreateCompatibleBitmap(hdc, x, y); |
605 | ReleaseDC(fe->hwnd, hdc); |
606 | |
607 | SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe); |
608 | |
609 | ShowWindow(fe->hwnd, SW_NORMAL); |
610 | SetForegroundWindow(fe->hwnd); |
611 | |
612 | midend_redraw(fe->me); |
613 | |
614 | return fe; |
615 | } |
616 | |
97098757 |
617 | static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg, |
618 | WPARAM wParam, LPARAM lParam) |
619 | { |
620 | frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA); |
621 | |
622 | switch (msg) { |
623 | case WM_INITDIALOG: |
624 | return 0; |
625 | |
626 | case WM_COMMAND: |
627 | if ((HIWORD(wParam) == BN_CLICKED || |
628 | HIWORD(wParam) == BN_DOUBLECLICKED) && |
629 | LOWORD(wParam) == IDOK) |
630 | fe->dlg_done = 1; |
631 | return 0; |
632 | |
633 | case WM_CLOSE: |
634 | fe->dlg_done = 1; |
635 | return 0; |
636 | } |
637 | |
638 | return 0; |
639 | } |
640 | |
077f3cbe |
641 | static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg, |
642 | WPARAM wParam, LPARAM lParam) |
643 | { |
644 | frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA); |
645 | config_item *i; |
646 | struct cfg_aux *j; |
647 | |
648 | switch (msg) { |
649 | case WM_INITDIALOG: |
650 | return 0; |
651 | |
652 | case WM_COMMAND: |
653 | /* |
654 | * OK and Cancel are special cases. |
655 | */ |
656 | if ((HIWORD(wParam) == BN_CLICKED || |
657 | HIWORD(wParam) == BN_DOUBLECLICKED) && |
658 | (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) { |
659 | if (LOWORD(wParam) == IDOK) { |
5928817c |
660 | char *err = midend_set_config(fe->me, fe->cfg_which, fe->cfg); |
077f3cbe |
661 | |
662 | if (err) { |
663 | MessageBox(hwnd, err, "Validation error", |
664 | MB_ICONERROR | MB_OK); |
665 | } else { |
97098757 |
666 | fe->dlg_done = 2; |
077f3cbe |
667 | } |
668 | } else { |
97098757 |
669 | fe->dlg_done = 1; |
077f3cbe |
670 | } |
671 | return 0; |
672 | } |
673 | |
674 | /* |
675 | * First find the control whose id this is. |
676 | */ |
677 | for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) { |
678 | if (j->ctlid == LOWORD(wParam)) |
679 | break; |
680 | } |
681 | if (i->type == C_END) |
682 | return 0; /* not our problem */ |
683 | |
684 | if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) { |
685 | char buffer[4096]; |
686 | GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer)); |
687 | buffer[lenof(buffer)-1] = '\0'; |
688 | sfree(i->sval); |
689 | i->sval = dupstr(buffer); |
690 | } else if (i->type == C_BOOLEAN && |
691 | (HIWORD(wParam) == BN_CLICKED || |
692 | HIWORD(wParam) == BN_DOUBLECLICKED)) { |
693 | i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid); |
694 | } else if (i->type == C_CHOICES && |
695 | HIWORD(wParam) == CBN_SELCHANGE) { |
696 | i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid, |
697 | CB_GETCURSEL, 0, 0); |
698 | } |
699 | |
700 | return 0; |
701 | |
702 | case WM_CLOSE: |
97098757 |
703 | fe->dlg_done = 1; |
077f3cbe |
704 | return 0; |
705 | } |
706 | |
707 | return 0; |
708 | } |
709 | |
710 | HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2, |
711 | char *wclass, int wstyle, |
97098757 |
712 | int exstyle, const char *wtext, int wid) |
077f3cbe |
713 | { |
714 | HWND ret; |
715 | ret = CreateWindowEx(exstyle, wclass, wtext, |
716 | wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1, |
717 | fe->cfgbox, (HMENU) wid, fe->inst, NULL); |
718 | SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0)); |
719 | return ret; |
720 | } |
721 | |
97098757 |
722 | static void about(frontend *fe) |
723 | { |
724 | int i; |
725 | WNDCLASS wc; |
726 | MSG msg; |
727 | TEXTMETRIC tm; |
728 | HDC hdc; |
729 | HFONT oldfont; |
730 | SIZE size; |
731 | int gm, id; |
732 | int winwidth, winheight, y; |
733 | int height, width, maxwid; |
734 | const char *strings[16]; |
735 | int lengths[16]; |
736 | int nstrings = 0; |
737 | char titlebuf[512]; |
738 | |
739 | sprintf(titlebuf, "About %.250s", thegame.name); |
740 | |
741 | strings[nstrings++] = thegame.name; |
742 | strings[nstrings++] = "from Simon Tatham's Portable Puzzle Collection"; |
743 | strings[nstrings++] = ver; |
744 | |
745 | wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW; |
746 | wc.lpfnWndProc = DefDlgProc; |
747 | wc.cbClsExtra = 0; |
748 | wc.cbWndExtra = DLGWINDOWEXTRA + 8; |
749 | wc.hInstance = fe->inst; |
750 | wc.hIcon = NULL; |
751 | wc.hCursor = LoadCursor(NULL, IDC_ARROW); |
752 | wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1); |
753 | wc.lpszMenuName = NULL; |
754 | wc.lpszClassName = "GameAboutBox"; |
755 | RegisterClass(&wc); |
756 | |
757 | hdc = GetDC(fe->hwnd); |
758 | SetMapMode(hdc, MM_TEXT); |
759 | |
760 | fe->dlg_done = FALSE; |
761 | |
762 | fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72), |
763 | 0, 0, 0, 0, |
764 | FALSE, FALSE, FALSE, DEFAULT_CHARSET, |
765 | OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, |
766 | DEFAULT_QUALITY, |
767 | FF_SWISS, |
768 | "MS Shell Dlg"); |
769 | |
770 | oldfont = SelectObject(hdc, fe->cfgfont); |
771 | if (GetTextMetrics(hdc, &tm)) { |
772 | height = tm.tmAscent + tm.tmDescent; |
773 | width = tm.tmAveCharWidth; |
774 | } else { |
775 | height = width = 30; |
776 | } |
777 | |
778 | /* |
779 | * Figure out the layout of the About box by measuring the |
780 | * length of each piece of text. |
781 | */ |
782 | maxwid = 0; |
783 | winheight = height/2; |
784 | |
785 | for (i = 0; i < nstrings; i++) { |
786 | if (GetTextExtentPoint32(hdc, strings[i], strlen(strings[i]), &size)) |
787 | lengths[i] = size.cx; |
788 | else |
789 | lengths[i] = 0; /* *shrug* */ |
790 | if (maxwid < lengths[i]) |
791 | maxwid = lengths[i]; |
792 | winheight += height * 3 / 2 + (height / 2); |
793 | } |
794 | |
795 | winheight += height + height * 7 / 4; /* OK button */ |
796 | winwidth = maxwid + 4*width; |
797 | |
798 | SelectObject(hdc, oldfont); |
799 | ReleaseDC(fe->hwnd, hdc); |
800 | |
801 | /* |
802 | * Create the dialog, now that we know its size. |
803 | */ |
804 | { |
805 | RECT r, r2; |
806 | |
807 | r.left = r.top = 0; |
808 | r.right = winwidth; |
809 | r.bottom = winheight; |
810 | |
811 | AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*| |
812 | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | |
813 | WS_CAPTION | WS_SYSMENU*/) &~ |
814 | (WS_MAXIMIZEBOX | WS_OVERLAPPED), |
815 | FALSE, 0); |
816 | |
817 | /* |
818 | * Centre the dialog on its parent window. |
819 | */ |
820 | r.right -= r.left; |
821 | r.bottom -= r.top; |
822 | GetWindowRect(fe->hwnd, &r2); |
823 | r.left = (r2.left + r2.right - r.right) / 2; |
824 | r.top = (r2.top + r2.bottom - r.bottom) / 2; |
825 | r.right += r.left; |
826 | r.bottom += r.top; |
827 | |
828 | fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, titlebuf, |
829 | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | |
830 | WS_CAPTION | WS_SYSMENU, |
831 | r.left, r.top, |
832 | r.right-r.left, r.bottom-r.top, |
833 | fe->hwnd, NULL, fe->inst, NULL); |
834 | } |
835 | |
836 | SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE); |
837 | |
838 | SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe); |
839 | SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)AboutDlgProc); |
840 | |
841 | id = 1000; |
842 | y = height/2; |
843 | for (i = 0; i < nstrings; i++) { |
844 | int border = width*2 + (maxwid - lengths[i]) / 2; |
845 | mkctrl(fe, border, border+lengths[i], y+height*1/8, y+height*9/8, |
846 | "Static", 0, 0, strings[i], id++); |
847 | y += height*3/2; |
848 | |
849 | assert(y < winheight); |
850 | y += height/2; |
851 | } |
852 | |
853 | y += height/2; /* extra space before OK */ |
854 | mkctrl(fe, width*2, maxwid+width*2, y, y+height*7/4, "BUTTON", |
855 | BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0, |
856 | "OK", IDOK); |
857 | |
858 | SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0); |
859 | |
860 | EnableWindow(fe->hwnd, FALSE); |
861 | ShowWindow(fe->cfgbox, SW_NORMAL); |
862 | while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) { |
863 | if (!IsDialogMessage(fe->cfgbox, &msg)) |
864 | DispatchMessage(&msg); |
865 | if (fe->dlg_done) |
866 | break; |
867 | } |
868 | EnableWindow(fe->hwnd, TRUE); |
869 | SetForegroundWindow(fe->hwnd); |
870 | DestroyWindow(fe->cfgbox); |
871 | DeleteObject(fe->cfgfont); |
872 | } |
873 | |
5928817c |
874 | static int get_config(frontend *fe, int which) |
077f3cbe |
875 | { |
876 | config_item *i; |
877 | struct cfg_aux *j; |
5928817c |
878 | char *title; |
077f3cbe |
879 | WNDCLASS wc; |
880 | MSG msg; |
881 | TEXTMETRIC tm; |
882 | HDC hdc; |
883 | HFONT oldfont; |
884 | SIZE size; |
885 | HWND ctl; |
886 | int gm, id, nctrls; |
887 | int winwidth, winheight, col1l, col1r, col2l, col2r, y; |
888 | int height, width, maxlabel, maxcheckbox; |
889 | |
890 | wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW; |
891 | wc.lpfnWndProc = DefDlgProc; |
892 | wc.cbClsExtra = 0; |
893 | wc.cbWndExtra = DLGWINDOWEXTRA + 8; |
894 | wc.hInstance = fe->inst; |
895 | wc.hIcon = NULL; |
896 | wc.hCursor = LoadCursor(NULL, IDC_ARROW); |
897 | wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1); |
898 | wc.lpszMenuName = NULL; |
899 | wc.lpszClassName = "GameConfigBox"; |
900 | RegisterClass(&wc); |
901 | |
902 | hdc = GetDC(fe->hwnd); |
903 | SetMapMode(hdc, MM_TEXT); |
904 | |
97098757 |
905 | fe->dlg_done = FALSE; |
077f3cbe |
906 | |
907 | fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72), |
908 | 0, 0, 0, 0, |
909 | FALSE, FALSE, FALSE, DEFAULT_CHARSET, |
910 | OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, |
911 | DEFAULT_QUALITY, |
912 | FF_SWISS, |
913 | "MS Shell Dlg"); |
914 | |
915 | oldfont = SelectObject(hdc, fe->cfgfont); |
916 | if (GetTextMetrics(hdc, &tm)) { |
917 | height = tm.tmAscent + tm.tmDescent; |
918 | width = tm.tmAveCharWidth; |
919 | } else { |
920 | height = width = 30; |
921 | } |
922 | |
5928817c |
923 | fe->cfg = midend_get_config(fe->me, which, &title); |
924 | fe->cfg_which = which; |
077f3cbe |
925 | |
926 | /* |
927 | * Figure out the layout of the config box by measuring the |
928 | * length of each piece of text. |
929 | */ |
930 | maxlabel = maxcheckbox = 0; |
931 | winheight = height/2; |
932 | |
933 | for (i = fe->cfg; i->type != C_END; i++) { |
934 | switch (i->type) { |
935 | case C_STRING: |
936 | case C_CHOICES: |
937 | /* |
938 | * Both these control types have a label filling only |
939 | * the left-hand column of the box. |
940 | */ |
941 | if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) && |
942 | maxlabel < size.cx) |
943 | maxlabel = size.cx; |
944 | winheight += height * 3 / 2 + (height / 2); |
945 | break; |
946 | |
947 | case C_BOOLEAN: |
948 | /* |
949 | * Checkboxes take up the whole of the box width. |
950 | */ |
951 | if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) && |
952 | maxcheckbox < size.cx) |
953 | maxcheckbox = size.cx; |
954 | winheight += height + (height / 2); |
955 | break; |
956 | } |
957 | } |
958 | |
959 | winheight += height + height * 7 / 4; /* OK / Cancel buttons */ |
960 | |
961 | col1l = 2*width; |
962 | col1r = col1l + maxlabel; |
963 | col2l = col1r + 2*width; |
964 | col2r = col2l + 30*width; |
965 | if (col2r < col1l+2*height+maxcheckbox) |
966 | col2r = col1l+2*height+maxcheckbox; |
967 | winwidth = col2r + 2*width; |
968 | |
97098757 |
969 | SelectObject(hdc, oldfont); |
077f3cbe |
970 | ReleaseDC(fe->hwnd, hdc); |
971 | |
972 | /* |
973 | * Create the dialog, now that we know its size. |
974 | */ |
975 | { |
976 | RECT r, r2; |
977 | |
978 | r.left = r.top = 0; |
979 | r.right = winwidth; |
980 | r.bottom = winheight; |
981 | |
982 | AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*| |
983 | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | |
984 | WS_CAPTION | WS_SYSMENU*/) &~ |
985 | (WS_MAXIMIZEBOX | WS_OVERLAPPED), |
986 | FALSE, 0); |
987 | |
988 | /* |
989 | * Centre the dialog on its parent window. |
990 | */ |
991 | r.right -= r.left; |
992 | r.bottom -= r.top; |
993 | GetWindowRect(fe->hwnd, &r2); |
994 | r.left = (r2.left + r2.right - r.right) / 2; |
995 | r.top = (r2.top + r2.bottom - r.bottom) / 2; |
996 | r.right += r.left; |
997 | r.bottom += r.top; |
998 | |
5928817c |
999 | fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title, |
077f3cbe |
1000 | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | |
1001 | WS_CAPTION | WS_SYSMENU, |
1002 | r.left, r.top, |
1003 | r.right-r.left, r.bottom-r.top, |
1004 | fe->hwnd, NULL, fe->inst, NULL); |
5928817c |
1005 | sfree(title); |
077f3cbe |
1006 | } |
1007 | |
1008 | SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE); |
1009 | |
1010 | SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe); |
1011 | SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc); |
1012 | |
1013 | /* |
1014 | * Count the controls so we can allocate cfgaux. |
1015 | */ |
1016 | for (nctrls = 0, i = fe->cfg; i->type != C_END; i++) |
1017 | nctrls++; |
1018 | fe->cfgaux = snewn(nctrls, struct cfg_aux); |
1019 | |
1020 | id = 1000; |
1021 | y = height/2; |
1022 | for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) { |
1023 | switch (i->type) { |
1024 | case C_STRING: |
1025 | /* |
1026 | * Edit box with a label beside it. |
1027 | */ |
1028 | mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8, |
1029 | "Static", 0, 0, i->name, id++); |
1030 | ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2, |
1031 | "EDIT", WS_TABSTOP | ES_AUTOHSCROLL, |
1032 | WS_EX_CLIENTEDGE, "", (j->ctlid = id++)); |
1033 | SetWindowText(ctl, i->sval); |
1034 | y += height*3/2; |
1035 | break; |
1036 | |
1037 | case C_BOOLEAN: |
1038 | /* |
1039 | * Simple checkbox. |
1040 | */ |
1041 | mkctrl(fe, col1l, col2r, y, y+height, "BUTTON", |
1042 | BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP, |
1043 | 0, i->name, (j->ctlid = id++)); |
42132f2d |
1044 | CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0)); |
077f3cbe |
1045 | y += height; |
1046 | break; |
1047 | |
1048 | case C_CHOICES: |
1049 | /* |
1050 | * Drop-down list with a label beside it. |
1051 | */ |
1052 | mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8, |
1053 | "STATIC", 0, 0, i->name, id++); |
1054 | ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2, |
1055 | "COMBOBOX", WS_TABSTOP | |
1056 | CBS_DROPDOWNLIST | CBS_HASSTRINGS, |
1057 | WS_EX_CLIENTEDGE, "", (j->ctlid = id++)); |
1058 | { |
1059 | char c, *p, *q, *str; |
1060 | |
1061 | SendMessage(ctl, CB_RESETCONTENT, 0, 0); |
1062 | p = i->sval; |
1063 | c = *p++; |
1064 | while (*p) { |
1065 | q = p; |
1066 | while (*q && *q != c) q++; |
1067 | str = snewn(q-p+1, char); |
1068 | strncpy(str, p, q-p); |
1069 | str[q-p] = '\0'; |
1070 | SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str); |
1071 | sfree(str); |
1072 | if (*q) q++; |
1073 | p = q; |
1074 | } |
1075 | } |
1076 | |
1077 | SendMessage(ctl, CB_SETCURSEL, i->ival, 0); |
1078 | |
1079 | y += height*3/2; |
1080 | break; |
1081 | } |
1082 | |
1083 | assert(y < winheight); |
1084 | y += height/2; |
1085 | } |
1086 | |
1087 | y += height/2; /* extra space before OK and Cancel */ |
1088 | mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON", |
1089 | BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0, |
1090 | "OK", IDOK); |
1091 | mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON", |
1092 | BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL); |
1093 | |
1094 | SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0); |
1095 | |
1096 | EnableWindow(fe->hwnd, FALSE); |
1097 | ShowWindow(fe->cfgbox, SW_NORMAL); |
1098 | while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) { |
1099 | if (!IsDialogMessage(fe->cfgbox, &msg)) |
1100 | DispatchMessage(&msg); |
97098757 |
1101 | if (fe->dlg_done) |
077f3cbe |
1102 | break; |
1103 | } |
1104 | EnableWindow(fe->hwnd, TRUE); |
1105 | SetForegroundWindow(fe->hwnd); |
1106 | DestroyWindow(fe->cfgbox); |
1107 | DeleteObject(fe->cfgfont); |
1108 | |
1109 | free_cfg(fe->cfg); |
1110 | sfree(fe->cfgaux); |
1111 | |
97098757 |
1112 | return (fe->dlg_done == 2); |
077f3cbe |
1113 | } |
1114 | |
1115 | static void new_game_type(frontend *fe) |
1116 | { |
1117 | RECT r, sr; |
1118 | HDC hdc; |
1119 | int x, y; |
1120 | |
5928817c |
1121 | midend_new_game(fe->me); |
1e3e152d |
1122 | x = y = INT_MAX; |
1123 | midend_size(fe->me, &x, &y, FALSE); |
077f3cbe |
1124 | |
1125 | r.left = r.top = 0; |
1126 | r.right = x; |
1127 | r.bottom = y; |
1128 | AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~ |
1129 | (WS_THICKFRAME | WS_MAXIMIZEBOX | |
1130 | WS_OVERLAPPED), |
1131 | TRUE, 0); |
1132 | |
1133 | if (fe->statusbar != NULL) { |
1134 | GetWindowRect(fe->statusbar, &sr); |
1135 | } else { |
1136 | sr.left = sr.right = sr.top = sr.bottom = 0; |
1137 | } |
1138 | SetWindowPos(fe->hwnd, NULL, 0, 0, |
1139 | r.right - r.left, |
1140 | r.bottom - r.top + sr.bottom - sr.top, |
1141 | SWP_NOMOVE | SWP_NOZORDER); |
1e3e152d |
1142 | |
1143 | check_window_size(fe, &x, &y); |
1144 | |
077f3cbe |
1145 | if (fe->statusbar != NULL) |
1146 | SetWindowPos(fe->statusbar, NULL, 0, y, x, |
1147 | sr.bottom - sr.top, SWP_NOZORDER); |
1148 | |
1149 | DeleteObject(fe->bitmap); |
1150 | |
1151 | hdc = GetDC(fe->hwnd); |
1152 | fe->bitmap = CreateCompatibleBitmap(hdc, x, y); |
1153 | ReleaseDC(fe->hwnd, hdc); |
1154 | |
1155 | midend_redraw(fe->me); |
1156 | } |
1157 | |
97c2790f |
1158 | static int is_alt_pressed(void) |
1159 | { |
1160 | BYTE keystate[256]; |
1161 | int r = GetKeyboardState(keystate); |
1162 | if (!r) |
1163 | return FALSE; |
1164 | if (keystate[VK_MENU] & 0x80) |
1165 | return TRUE; |
1166 | if (keystate[VK_RMENU] & 0x80) |
1167 | return TRUE; |
1168 | return FALSE; |
1169 | } |
1170 | |
4e7ef6e6 |
1171 | static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, |
1172 | WPARAM wParam, LPARAM lParam) |
1173 | { |
1174 | frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA); |
1175 | |
1176 | switch (message) { |
1177 | case WM_CLOSE: |
1178 | DestroyWindow(hwnd); |
1179 | return 0; |
eb2ad6f1 |
1180 | case WM_COMMAND: |
1181 | switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */ |
1182 | case IDM_NEW: |
1183 | if (!midend_process_key(fe->me, 0, 0, 'n')) |
1184 | PostQuitMessage(0); |
1185 | break; |
1186 | case IDM_RESTART: |
7f89707c |
1187 | midend_restart_game(fe->me); |
eb2ad6f1 |
1188 | break; |
1189 | case IDM_UNDO: |
1190 | if (!midend_process_key(fe->me, 0, 0, 'u')) |
1191 | PostQuitMessage(0); |
1192 | break; |
1193 | case IDM_REDO: |
1194 | if (!midend_process_key(fe->me, 0, 0, '\x12')) |
1195 | PostQuitMessage(0); |
1196 | break; |
1cdfcdbd |
1197 | case IDM_COPY: |
1198 | { |
1199 | char *text = midend_text_format(fe->me); |
1200 | if (text) |
1201 | write_clip(hwnd, text); |
1202 | else |
1203 | MessageBeep(MB_ICONWARNING); |
6d5cbf79 |
1204 | sfree(text); |
1cdfcdbd |
1205 | } |
1206 | break; |
2ac6d24e |
1207 | case IDM_SOLVE: |
1208 | { |
1209 | char *msg = midend_solve(fe->me); |
1210 | if (msg) |
1211 | MessageBox(hwnd, msg, "Unable to solve", |
1212 | MB_ICONERROR | MB_OK); |
1213 | } |
1214 | break; |
eb2ad6f1 |
1215 | case IDM_QUIT: |
1216 | if (!midend_process_key(fe->me, 0, 0, 'q')) |
1217 | PostQuitMessage(0); |
1218 | break; |
077f3cbe |
1219 | case IDM_CONFIG: |
5928817c |
1220 | if (get_config(fe, CFG_SETTINGS)) |
1221 | new_game_type(fe); |
1222 | break; |
1223 | case IDM_SEED: |
1224 | if (get_config(fe, CFG_SEED)) |
077f3cbe |
1225 | new_game_type(fe); |
1226 | break; |
1185e3c5 |
1227 | case IDM_DESC: |
1228 | if (get_config(fe, CFG_DESC)) |
1229 | new_game_type(fe); |
1230 | break; |
97098757 |
1231 | case IDM_ABOUT: |
1232 | about(fe); |
1233 | break; |
e91825f8 |
1234 | case IDM_HELPC: |
1235 | assert(fe->help_path); |
1236 | WinHelp(hwnd, fe->help_path, |
1237 | fe->help_has_contents ? HELP_FINDER : HELP_CONTENTS, 0); |
1238 | break; |
1239 | case IDM_GAMEHELP: |
1240 | assert(fe->help_path); |
be8d5aa1 |
1241 | assert(thegame.winhelp_topic); |
e91825f8 |
1242 | { |
be8d5aa1 |
1243 | char *cmd = snewn(10+strlen(thegame.winhelp_topic), char); |
1244 | sprintf(cmd, "JI(`',`%s')", thegame.winhelp_topic); |
e91825f8 |
1245 | WinHelp(hwnd, fe->help_path, HELP_COMMAND, (DWORD)cmd); |
1246 | sfree(cmd); |
1247 | } |
1248 | break; |
eb2ad6f1 |
1249 | default: |
1250 | { |
1251 | int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10; |
1252 | |
1253 | if (p >= 0 && p < fe->npresets) { |
eb2ad6f1 |
1254 | midend_set_params(fe->me, fe->presets[p]); |
077f3cbe |
1255 | new_game_type(fe); |
eb2ad6f1 |
1256 | } |
1257 | } |
1258 | break; |
1259 | } |
1260 | break; |
4e7ef6e6 |
1261 | case WM_DESTROY: |
1262 | PostQuitMessage(0); |
1263 | return 0; |
1264 | case WM_PAINT: |
1265 | { |
1266 | PAINTSTRUCT p; |
1267 | HDC hdc, hdc2; |
1268 | HBITMAP prevbm; |
1269 | |
1270 | hdc = BeginPaint(hwnd, &p); |
1271 | hdc2 = CreateCompatibleDC(hdc); |
1272 | prevbm = SelectObject(hdc2, fe->bitmap); |
1273 | BitBlt(hdc, |
1274 | p.rcPaint.left, p.rcPaint.top, |
1275 | p.rcPaint.right - p.rcPaint.left, |
1276 | p.rcPaint.bottom - p.rcPaint.top, |
1277 | hdc2, |
1278 | p.rcPaint.left, p.rcPaint.top, |
1279 | SRCCOPY); |
1280 | SelectObject(hdc2, prevbm); |
1281 | DeleteDC(hdc2); |
1282 | EndPaint(hwnd, &p); |
1283 | } |
1284 | return 0; |
1285 | case WM_KEYDOWN: |
1286 | { |
1287 | int key = -1; |
f0ee053c |
1288 | BYTE keystate[256]; |
1289 | int r = GetKeyboardState(keystate); |
1290 | int shift = (r && (keystate[VK_SHIFT] & 0x80)) ? MOD_SHFT : 0; |
1291 | int ctrl = (r && (keystate[VK_CONTROL] & 0x80)) ? MOD_CTRL : 0; |
4e7ef6e6 |
1292 | |
1293 | switch (wParam) { |
3c833d45 |
1294 | case VK_LEFT: |
1295 | if (!(lParam & 0x01000000)) |
1296 | key = MOD_NUM_KEYPAD | '4'; |
f0ee053c |
1297 | else |
1298 | key = shift | ctrl | CURSOR_LEFT; |
3c833d45 |
1299 | break; |
1300 | case VK_RIGHT: |
1301 | if (!(lParam & 0x01000000)) |
1302 | key = MOD_NUM_KEYPAD | '6'; |
f0ee053c |
1303 | else |
1304 | key = shift | ctrl | CURSOR_RIGHT; |
3c833d45 |
1305 | break; |
1306 | case VK_UP: |
1307 | if (!(lParam & 0x01000000)) |
1308 | key = MOD_NUM_KEYPAD | '8'; |
f0ee053c |
1309 | else |
1310 | key = shift | ctrl | CURSOR_UP; |
3c833d45 |
1311 | break; |
1312 | case VK_DOWN: |
1313 | if (!(lParam & 0x01000000)) |
1314 | key = MOD_NUM_KEYPAD | '2'; |
f0ee053c |
1315 | else |
1316 | key = shift | ctrl | CURSOR_DOWN; |
3c833d45 |
1317 | break; |
c71454c0 |
1318 | /* |
1319 | * Diagonal keys on the numeric keypad. |
1320 | */ |
1321 | case VK_PRIOR: |
3c833d45 |
1322 | if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '9'; |
c71454c0 |
1323 | break; |
1324 | case VK_NEXT: |
3c833d45 |
1325 | if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '3'; |
c71454c0 |
1326 | break; |
1327 | case VK_HOME: |
3c833d45 |
1328 | if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '7'; |
c71454c0 |
1329 | break; |
1330 | case VK_END: |
3c833d45 |
1331 | if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '1'; |
1332 | break; |
1333 | case VK_INSERT: |
1334 | if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '0'; |
1335 | break; |
1336 | case VK_CLEAR: |
1337 | if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '5'; |
c71454c0 |
1338 | break; |
1339 | /* |
1340 | * Numeric keypad keys with Num Lock on. |
1341 | */ |
3c833d45 |
1342 | case VK_NUMPAD4: key = MOD_NUM_KEYPAD | '4'; break; |
1343 | case VK_NUMPAD6: key = MOD_NUM_KEYPAD | '6'; break; |
1344 | case VK_NUMPAD8: key = MOD_NUM_KEYPAD | '8'; break; |
1345 | case VK_NUMPAD2: key = MOD_NUM_KEYPAD | '2'; break; |
1346 | case VK_NUMPAD5: key = MOD_NUM_KEYPAD | '5'; break; |
1347 | case VK_NUMPAD9: key = MOD_NUM_KEYPAD | '9'; break; |
1348 | case VK_NUMPAD3: key = MOD_NUM_KEYPAD | '3'; break; |
1349 | case VK_NUMPAD7: key = MOD_NUM_KEYPAD | '7'; break; |
1350 | case VK_NUMPAD1: key = MOD_NUM_KEYPAD | '1'; break; |
1351 | case VK_NUMPAD0: key = MOD_NUM_KEYPAD | '0'; break; |
4e7ef6e6 |
1352 | } |
1353 | |
1354 | if (key != -1) { |
eb2ad6f1 |
1355 | if (!midend_process_key(fe->me, 0, 0, key)) |
4e7ef6e6 |
1356 | PostQuitMessage(0); |
c71454c0 |
1357 | } else { |
1358 | MSG m; |
1359 | m.hwnd = hwnd; |
1360 | m.message = WM_KEYDOWN; |
1361 | m.wParam = wParam; |
1362 | m.lParam = lParam & 0xdfff; |
1363 | TranslateMessage(&m); |
4e7ef6e6 |
1364 | } |
1365 | } |
1366 | break; |
1367 | case WM_LBUTTONDOWN: |
1368 | case WM_RBUTTONDOWN: |
1369 | case WM_MBUTTONDOWN: |
b0f06719 |
1370 | { |
1371 | int button; |
1372 | |
1373 | /* |
1374 | * Shift-clicks count as middle-clicks, since otherwise |
1375 | * two-button Windows users won't have any kind of |
1376 | * middle click to use. |
1377 | */ |
1378 | if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT)) |
1379 | button = MIDDLE_BUTTON; |
97c2790f |
1380 | else if (message == WM_RBUTTONDOWN || is_alt_pressed()) |
b0f06719 |
1381 | button = RIGHT_BUTTON; |
97c2790f |
1382 | else |
1383 | button = LEFT_BUTTON; |
74a4e547 |
1384 | |
5dda68e8 |
1385 | if (!midend_process_key(fe->me, (signed short)LOWORD(lParam), |
1386 | (signed short)HIWORD(lParam), button)) |
74a4e547 |
1387 | PostQuitMessage(0); |
1388 | |
1389 | SetCapture(hwnd); |
1390 | } |
1391 | break; |
1392 | case WM_LBUTTONUP: |
1393 | case WM_RBUTTONUP: |
1394 | case WM_MBUTTONUP: |
1395 | { |
1396 | int button; |
1397 | |
1398 | /* |
1399 | * Shift-clicks count as middle-clicks, since otherwise |
1400 | * two-button Windows users won't have any kind of |
1401 | * middle click to use. |
1402 | */ |
1403 | if (message == WM_MBUTTONUP || (wParam & MK_SHIFT)) |
1404 | button = MIDDLE_RELEASE; |
97c2790f |
1405 | else if (message == WM_RBUTTONUP || is_alt_pressed()) |
74a4e547 |
1406 | button = RIGHT_RELEASE; |
97c2790f |
1407 | else |
1408 | button = LEFT_RELEASE; |
74a4e547 |
1409 | |
5dda68e8 |
1410 | if (!midend_process_key(fe->me, (signed short)LOWORD(lParam), |
1411 | (signed short)HIWORD(lParam), button)) |
74a4e547 |
1412 | PostQuitMessage(0); |
1413 | |
1414 | ReleaseCapture(); |
1415 | } |
1416 | break; |
1417 | case WM_MOUSEMOVE: |
1418 | { |
1419 | int button; |
1420 | |
1421 | if (wParam & (MK_MBUTTON | MK_SHIFT)) |
1422 | button = MIDDLE_DRAG; |
97c2790f |
1423 | else if (wParam & MK_RBUTTON || is_alt_pressed()) |
74a4e547 |
1424 | button = RIGHT_DRAG; |
97c2790f |
1425 | else |
1426 | button = LEFT_DRAG; |
74a4e547 |
1427 | |
5dda68e8 |
1428 | if (!midend_process_key(fe->me, (signed short)LOWORD(lParam), |
1429 | (signed short)HIWORD(lParam), button)) |
b0f06719 |
1430 | PostQuitMessage(0); |
1431 | } |
4e7ef6e6 |
1432 | break; |
1433 | case WM_CHAR: |
eb2ad6f1 |
1434 | if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam)) |
4e7ef6e6 |
1435 | PostQuitMessage(0); |
1436 | return 0; |
1437 | case WM_TIMER: |
8c1fd974 |
1438 | if (fe->timer) { |
1439 | DWORD now = GetTickCount(); |
1440 | float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F; |
1441 | midend_timer(fe->me, elapsed); |
1442 | fe->timer_last_tickcount = now; |
1443 | } |
4e7ef6e6 |
1444 | return 0; |
1445 | } |
1446 | |
1447 | return DefWindowProc(hwnd, message, wParam, lParam); |
1448 | } |
1449 | |
1450 | int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) |
1451 | { |
1452 | MSG msg; |
8b7938e7 |
1453 | char *error; |
4e7ef6e6 |
1454 | |
fd1a1a2b |
1455 | InitCommonControls(); |
1456 | |
4e7ef6e6 |
1457 | if (!prev) { |
1458 | WNDCLASS wndclass; |
1459 | |
1460 | wndclass.style = 0; |
1461 | wndclass.lpfnWndProc = WndProc; |
1462 | wndclass.cbClsExtra = 0; |
1463 | wndclass.cbWndExtra = 0; |
1464 | wndclass.hInstance = inst; |
1465 | wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION); |
1466 | wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); |
1467 | wndclass.hbrBackground = NULL; |
1468 | wndclass.lpszMenuName = NULL; |
be8d5aa1 |
1469 | wndclass.lpszClassName = thegame.name; |
4e7ef6e6 |
1470 | |
1471 | RegisterClass(&wndclass); |
1472 | } |
1473 | |
8b7938e7 |
1474 | while (*cmdline && isspace(*cmdline)) |
1475 | cmdline++; |
1476 | |
1477 | if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) { |
1478 | char buf[128]; |
be8d5aa1 |
1479 | sprintf(buf, "%.100s Error", thegame.name); |
8b7938e7 |
1480 | MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR); |
1481 | return 1; |
1482 | } |
4e7ef6e6 |
1483 | |
1484 | while (GetMessage(&msg, NULL, 0, 0)) { |
4e7ef6e6 |
1485 | DispatchMessage(&msg); |
1486 | } |
1487 | |
1488 | return msg.wParam; |
1489 | } |