720a8fb7 |
1 | /* |
2 | * windows.c: Windows front end for my puzzle collection. |
3 | */ |
4e7ef6e6 |
4 | |
5 | #include <windows.h> |
fd1a1a2b |
6 | #include <commctrl.h> |
291c690f |
7 | #ifndef NO_HTMLHELP |
750037d7 |
8 | #include <htmlhelp.h> |
291c690f |
9 | #endif /* NO_HTMLHELP */ |
4e7ef6e6 |
10 | |
11 | #include <stdio.h> |
4efb3868 |
12 | #include <assert.h> |
8b7938e7 |
13 | #include <ctype.h> |
4e7ef6e6 |
14 | #include <stdarg.h> |
15 | #include <stdlib.h> |
1e3e152d |
16 | #include <limits.h> |
4e7ef6e6 |
17 | #include <time.h> |
18 | |
19 | #include "puzzles.h" |
20 | |
eb2ad6f1 |
21 | #define IDM_NEW 0x0010 |
22 | #define IDM_RESTART 0x0020 |
23 | #define IDM_UNDO 0x0030 |
24 | #define IDM_REDO 0x0040 |
1cdfcdbd |
25 | #define IDM_COPY 0x0050 |
2ac6d24e |
26 | #define IDM_SOLVE 0x0060 |
27 | #define IDM_QUIT 0x0070 |
28 | #define IDM_CONFIG 0x0080 |
1185e3c5 |
29 | #define IDM_DESC 0x0090 |
30 | #define IDM_SEED 0x00A0 |
31 | #define IDM_HELPC 0x00B0 |
32 | #define IDM_GAMEHELP 0x00C0 |
fa7515b2 |
33 | #define IDM_ABOUT 0x00D0 |
668be019 |
34 | #define IDM_SAVE 0x00E0 |
35 | #define IDM_LOAD 0x00F0 |
821ab2c6 |
36 | #define IDM_PRINT 0x0100 |
37 | #define IDM_PRESETS 0x0110 |
eb2ad6f1 |
38 | |
e91825f8 |
39 | #define HELP_FILE_NAME "puzzles.hlp" |
40 | #define HELP_CNT_NAME "puzzles.cnt" |
291c690f |
41 | #ifndef NO_HTMLHELP |
750037d7 |
42 | #define CHM_FILE_NAME "puzzles.chm" |
291c690f |
43 | #endif /* NO_HTMLHELP */ |
750037d7 |
44 | |
291c690f |
45 | #ifndef NO_HTMLHELP |
750037d7 |
46 | typedef HWND (CALLBACK *htmlhelp_t)(HWND, LPCSTR, UINT, DWORD); |
750037d7 |
47 | static htmlhelp_t htmlhelp; |
48 | static HINSTANCE hh_dll; |
291c690f |
49 | #endif /* NO_HTMLHELP */ |
750037d7 |
50 | enum { NONE, HLP, CHM } help_type; |
51 | char *help_path; |
52 | const char *help_topic; |
53 | int help_has_contents; |
e91825f8 |
54 | |
7b010604 |
55 | #ifdef DEBUGGING |
c71454c0 |
56 | static FILE *debug_fp = NULL; |
57 | static HANDLE debug_hdl = INVALID_HANDLE_VALUE; |
58 | static int debug_got_console = 0; |
59 | |
60 | void dputs(char *buf) |
61 | { |
62 | DWORD dw; |
63 | |
64 | if (!debug_got_console) { |
65 | if (AllocConsole()) { |
66 | debug_got_console = 1; |
67 | debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE); |
68 | } |
69 | } |
70 | if (!debug_fp) { |
71 | debug_fp = fopen("debug.log", "w"); |
72 | } |
73 | |
74 | if (debug_hdl != INVALID_HANDLE_VALUE) { |
75 | WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL); |
76 | } |
77 | fputs(buf, debug_fp); |
78 | fflush(debug_fp); |
79 | } |
80 | |
81 | void debug_printf(char *fmt, ...) |
82 | { |
83 | char buf[4096]; |
84 | va_list ap; |
85 | |
86 | va_start(ap, fmt); |
ab3a1e43 |
87 | _vsnprintf(buf, 4095, fmt, ap); |
c71454c0 |
88 | dputs(buf); |
89 | va_end(ap); |
90 | } |
c71454c0 |
91 | #endif |
92 | |
35ee2001 |
93 | #define WINFLAGS (WS_OVERLAPPEDWINDOW &~ \ |
94 | (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED)) |
95 | |
96 | static void new_game_size(frontend *fe); |
97 | |
4efb3868 |
98 | struct font { |
99 | HFONT font; |
100 | int type; |
101 | int size; |
102 | }; |
103 | |
077f3cbe |
104 | struct cfg_aux { |
105 | int ctlid; |
106 | }; |
107 | |
3161048d |
108 | struct blitter { |
109 | HBITMAP bitmap; |
110 | frontend *fe; |
111 | int x, y, w, h; |
112 | }; |
113 | |
821ab2c6 |
114 | enum { CFG_PRINT = CFG_FRONTEND_SPECIFIC }; |
115 | |
4e7ef6e6 |
116 | struct frontend { |
dafd6cf6 |
117 | midend *me; |
077f3cbe |
118 | HWND hwnd, statusbar, cfgbox; |
119 | HINSTANCE inst; |
4e7ef6e6 |
120 | HBITMAP bitmap, prevbm; |
821ab2c6 |
121 | HDC hdc; |
4e7ef6e6 |
122 | COLORREF *colours; |
123 | HBRUSH *brushes; |
124 | HPEN *pens; |
4efb3868 |
125 | HRGN clip; |
4e7ef6e6 |
126 | UINT timer; |
8c1fd974 |
127 | DWORD timer_last_tickcount; |
eb2ad6f1 |
128 | int npresets; |
129 | game_params **presets; |
4efb3868 |
130 | struct font *fonts; |
131 | int nfonts, fontsize; |
077f3cbe |
132 | config_item *cfg; |
133 | struct cfg_aux *cfgaux; |
97098757 |
134 | int cfg_which, dlg_done; |
077f3cbe |
135 | HFONT cfgfont; |
821ab2c6 |
136 | HBRUSH oldbr; |
137 | HPEN oldpen; |
750037d7 |
138 | int help_running; |
821ab2c6 |
139 | enum { DRAWING, PRINTING, NOTHING } drawstatus; |
140 | DOCINFO di; |
141 | int printcount, printw, printh, printsolns, printcurr, printcolour; |
142 | float printscale; |
143 | int printoffsetx, printoffsety; |
144 | float printpixelscale; |
145 | int fontstart; |
146 | int linewidth; |
147 | drawing *dr; |
4e7ef6e6 |
148 | }; |
149 | |
150 | void fatal(char *fmt, ...) |
151 | { |
152 | char buf[2048]; |
153 | va_list ap; |
154 | |
155 | va_start(ap, fmt); |
156 | vsprintf(buf, fmt, ap); |
157 | va_end(ap); |
158 | |
159 | MessageBox(NULL, buf, "Fatal error", MB_ICONEXCLAMATION | MB_OK); |
160 | |
161 | exit(1); |
162 | } |
163 | |
821ab2c6 |
164 | char *geterrstr(void) |
165 | { |
166 | LPVOID lpMsgBuf; |
167 | DWORD dw = GetLastError(); |
168 | char *ret; |
169 | |
170 | FormatMessage( |
171 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
172 | FORMAT_MESSAGE_FROM_SYSTEM, |
173 | NULL, |
174 | dw, |
175 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
176 | (LPTSTR) &lpMsgBuf, |
177 | 0, NULL ); |
178 | |
179 | ret = dupstr(lpMsgBuf); |
180 | |
181 | LocalFree(lpMsgBuf); |
182 | |
183 | return ret; |
184 | } |
185 | |
cbb5549e |
186 | void get_random_seed(void **randseed, int *randseedsize) |
187 | { |
188 | time_t *tp = snew(time_t); |
189 | time(tp); |
190 | *randseed = (void *)tp; |
191 | *randseedsize = sizeof(time_t); |
192 | } |
193 | |
dafd6cf6 |
194 | static void win_status_bar(void *handle, char *text) |
fd1a1a2b |
195 | { |
dafd6cf6 |
196 | frontend *fe = (frontend *)handle; |
821ab2c6 |
197 | |
83c0438f |
198 | SetWindowText(fe->statusbar, text); |
fd1a1a2b |
199 | } |
200 | |
dafd6cf6 |
201 | static blitter *win_blitter_new(void *handle, int w, int h) |
3161048d |
202 | { |
203 | blitter *bl = snew(blitter); |
204 | |
205 | memset(bl, 0, sizeof(blitter)); |
206 | bl->w = w; |
207 | bl->h = h; |
208 | bl->bitmap = 0; |
209 | |
210 | return bl; |
211 | } |
212 | |
dafd6cf6 |
213 | static void win_blitter_free(void *handle, blitter *bl) |
3161048d |
214 | { |
215 | if (bl->bitmap) DeleteObject(bl->bitmap); |
216 | sfree(bl); |
217 | } |
218 | |
219 | static void blitter_mkbitmap(frontend *fe, blitter *bl) |
220 | { |
221 | HDC hdc = GetDC(fe->hwnd); |
222 | bl->bitmap = CreateCompatibleBitmap(hdc, bl->w, bl->h); |
223 | ReleaseDC(fe->hwnd, hdc); |
224 | } |
225 | |
226 | /* BitBlt(dstDC, dstX, dstY, dstW, dstH, srcDC, srcX, srcY, dType) */ |
227 | |
dafd6cf6 |
228 | static void win_blitter_save(void *handle, blitter *bl, int x, int y) |
3161048d |
229 | { |
dafd6cf6 |
230 | frontend *fe = (frontend *)handle; |
3161048d |
231 | HDC hdc_win, hdc_blit; |
232 | HBITMAP prev_blit; |
233 | |
821ab2c6 |
234 | assert(fe->drawstatus == DRAWING); |
235 | |
3161048d |
236 | if (!bl->bitmap) blitter_mkbitmap(fe, bl); |
237 | |
238 | bl->x = x; bl->y = y; |
239 | |
240 | hdc_win = GetDC(fe->hwnd); |
241 | hdc_blit = CreateCompatibleDC(hdc_win); |
242 | if (!hdc_blit) fatal("hdc_blit failed: 0x%x", GetLastError()); |
243 | |
244 | prev_blit = SelectObject(hdc_blit, bl->bitmap); |
245 | if (prev_blit == NULL || prev_blit == HGDI_ERROR) |
246 | fatal("SelectObject for hdc_main failed: 0x%x", GetLastError()); |
247 | |
248 | if (!BitBlt(hdc_blit, 0, 0, bl->w, bl->h, |
821ab2c6 |
249 | fe->hdc, x, y, SRCCOPY)) |
3161048d |
250 | fatal("BitBlt failed: 0x%x", GetLastError()); |
251 | |
252 | SelectObject(hdc_blit, prev_blit); |
253 | DeleteDC(hdc_blit); |
254 | ReleaseDC(fe->hwnd, hdc_win); |
255 | } |
256 | |
dafd6cf6 |
257 | static void win_blitter_load(void *handle, blitter *bl, int x, int y) |
3161048d |
258 | { |
dafd6cf6 |
259 | frontend *fe = (frontend *)handle; |
3161048d |
260 | HDC hdc_win, hdc_blit; |
261 | HBITMAP prev_blit; |
262 | |
821ab2c6 |
263 | assert(fe->drawstatus == DRAWING); |
264 | |
3161048d |
265 | assert(bl->bitmap); /* we should always have saved before loading */ |
266 | |
267 | if (x == BLITTER_FROMSAVED) x = bl->x; |
268 | if (y == BLITTER_FROMSAVED) y = bl->y; |
269 | |
270 | hdc_win = GetDC(fe->hwnd); |
271 | hdc_blit = CreateCompatibleDC(hdc_win); |
272 | |
273 | prev_blit = SelectObject(hdc_blit, bl->bitmap); |
274 | |
821ab2c6 |
275 | BitBlt(fe->hdc, x, y, bl->w, bl->h, |
3161048d |
276 | hdc_blit, 0, 0, SRCCOPY); |
277 | |
278 | SelectObject(hdc_blit, prev_blit); |
279 | DeleteDC(hdc_blit); |
280 | ReleaseDC(fe->hwnd, hdc_win); |
281 | } |
282 | |
4e7ef6e6 |
283 | void frontend_default_colour(frontend *fe, float *output) |
284 | { |
285 | DWORD c = GetSysColor(COLOR_MENU); /* ick */ |
286 | |
287 | output[0] = (float)(GetRValue(c) / 255.0); |
288 | output[1] = (float)(GetGValue(c) / 255.0); |
289 | output[2] = (float)(GetBValue(c) / 255.0); |
290 | } |
291 | |
821ab2c6 |
292 | static POINT win_transform_point(frontend *fe, int x, int y) |
293 | { |
294 | POINT ret; |
295 | |
296 | assert(fe->drawstatus != NOTHING); |
297 | |
298 | if (fe->drawstatus == PRINTING) { |
299 | ret.x = (int)(fe->printoffsetx + fe->printpixelscale * x); |
300 | ret.y = (int)(fe->printoffsety + fe->printpixelscale * y); |
301 | } else { |
302 | ret.x = x; |
303 | ret.y = y; |
304 | } |
305 | |
306 | return ret; |
307 | } |
308 | |
309 | static void win_text_colour(frontend *fe, int colour) |
310 | { |
311 | assert(fe->drawstatus != NOTHING); |
312 | |
313 | if (fe->drawstatus == PRINTING) { |
314 | int hatch; |
315 | float r, g, b; |
316 | print_get_colour(fe->dr, colour, &hatch, &r, &g, &b); |
317 | if (fe->printcolour) |
318 | SetTextColor(fe->hdc, RGB(r * 255, g * 255, b * 255)); |
319 | else |
320 | SetTextColor(fe->hdc, |
321 | hatch == HATCH_CLEAR ? RGB(255,255,255) : RGB(0,0,0)); |
322 | } else { |
323 | SetTextColor(fe->hdc, fe->colours[colour]); |
324 | } |
325 | } |
326 | |
327 | static void win_set_brush(frontend *fe, int colour) |
328 | { |
329 | HBRUSH br; |
330 | assert(fe->drawstatus != NOTHING); |
331 | |
332 | if (fe->drawstatus == PRINTING) { |
333 | int hatch; |
334 | float r, g, b; |
335 | print_get_colour(fe->dr, colour, &hatch, &r, &g, &b); |
336 | |
337 | if (fe->printcolour) |
338 | br = CreateSolidBrush(RGB(r * 255, g * 255, b * 255)); |
339 | else if (hatch == HATCH_SOLID) |
340 | br = CreateSolidBrush(RGB(0,0,0)); |
341 | else if (hatch == HATCH_CLEAR) |
342 | br = CreateSolidBrush(RGB(255,255,255)); |
343 | else |
344 | br = CreateHatchBrush(hatch == HATCH_BACKSLASH ? HS_FDIAGONAL : |
345 | hatch == HATCH_SLASH ? HS_BDIAGONAL : |
346 | hatch == HATCH_HORIZ ? HS_HORIZONTAL : |
347 | hatch == HATCH_VERT ? HS_VERTICAL : |
348 | hatch == HATCH_PLUS ? HS_CROSS : |
349 | /* hatch == HATCH_X ? */ HS_DIAGCROSS, |
350 | RGB(0,0,0)); |
351 | } else { |
352 | br = fe->brushes[colour]; |
353 | } |
354 | fe->oldbr = SelectObject(fe->hdc, br); |
355 | } |
356 | |
357 | static void win_reset_brush(frontend *fe) |
358 | { |
359 | HBRUSH br; |
360 | |
361 | assert(fe->drawstatus != NOTHING); |
362 | |
363 | br = SelectObject(fe->hdc, fe->oldbr); |
364 | if (fe->drawstatus == PRINTING) |
365 | DeleteObject(br); |
366 | } |
367 | |
368 | static void win_set_pen(frontend *fe, int colour, int thin) |
369 | { |
370 | HPEN pen; |
371 | assert(fe->drawstatus != NOTHING); |
372 | |
373 | if (fe->drawstatus == PRINTING) { |
374 | int hatch; |
375 | float r, g, b; |
376 | int width = thin ? 0 : fe->linewidth; |
377 | |
378 | print_get_colour(fe->dr, colour, &hatch, &r, &g, &b); |
821ab2c6 |
379 | if (fe->printcolour) |
380 | pen = CreatePen(PS_SOLID, width, |
381 | RGB(r * 255, g * 255, b * 255)); |
382 | else if (hatch == HATCH_SOLID) |
383 | pen = CreatePen(PS_SOLID, width, RGB(0, 0, 0)); |
384 | else if (hatch == HATCH_CLEAR) |
385 | pen = CreatePen(PS_SOLID, width, RGB(255,255,255)); |
386 | else { |
387 | assert(!"This shouldn't happen"); |
388 | pen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0)); |
389 | } |
390 | } else { |
391 | pen = fe->pens[colour]; |
392 | } |
393 | fe->oldpen = SelectObject(fe->hdc, pen); |
394 | } |
395 | |
396 | static void win_reset_pen(frontend *fe) |
397 | { |
398 | HPEN pen; |
399 | |
400 | assert(fe->drawstatus != NOTHING); |
401 | |
402 | pen = SelectObject(fe->hdc, fe->oldpen); |
403 | if (fe->drawstatus == PRINTING) |
404 | DeleteObject(pen); |
405 | } |
406 | |
dafd6cf6 |
407 | static void win_clip(void *handle, int x, int y, int w, int h) |
4efb3868 |
408 | { |
dafd6cf6 |
409 | frontend *fe = (frontend *)handle; |
821ab2c6 |
410 | POINT p, q; |
411 | |
412 | if (fe->drawstatus == NOTHING) |
413 | return; |
414 | |
415 | p = win_transform_point(fe, x, y); |
416 | q = win_transform_point(fe, x+w, y+h); |
417 | IntersectClipRect(fe->hdc, p.x, p.y, q.x, q.y); |
4efb3868 |
418 | } |
419 | |
dafd6cf6 |
420 | static void win_unclip(void *handle) |
4efb3868 |
421 | { |
dafd6cf6 |
422 | frontend *fe = (frontend *)handle; |
821ab2c6 |
423 | |
424 | if (fe->drawstatus == NOTHING) |
425 | return; |
426 | |
427 | SelectClipRgn(fe->hdc, NULL); |
4efb3868 |
428 | } |
429 | |
dafd6cf6 |
430 | static void win_draw_text(void *handle, int x, int y, int fonttype, |
431 | int fontsize, int align, int colour, char *text) |
4efb3868 |
432 | { |
dafd6cf6 |
433 | frontend *fe = (frontend *)handle; |
821ab2c6 |
434 | POINT xy; |
4efb3868 |
435 | int i; |
436 | |
821ab2c6 |
437 | if (fe->drawstatus == NOTHING) |
438 | return; |
439 | |
440 | if (fe->drawstatus == PRINTING) |
441 | fontsize = (int)(fontsize * fe->printpixelscale); |
442 | |
443 | xy = win_transform_point(fe, x, y); |
444 | |
4efb3868 |
445 | /* |
446 | * Find or create the font. |
447 | */ |
821ab2c6 |
448 | for (i = fe->fontstart; i < fe->nfonts; i++) |
4efb3868 |
449 | if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize) |
450 | break; |
451 | |
452 | if (i == fe->nfonts) { |
453 | if (fe->fontsize <= fe->nfonts) { |
454 | fe->fontsize = fe->nfonts + 10; |
455 | fe->fonts = sresize(fe->fonts, fe->fontsize, struct font); |
456 | } |
457 | |
458 | fe->nfonts++; |
459 | |
460 | fe->fonts[i].type = fonttype; |
461 | fe->fonts[i].size = fontsize; |
462 | |
821ab2c6 |
463 | fe->fonts[i].font = CreateFont(-fontsize, 0, 0, 0, |
464 | fe->drawstatus == PRINTING ? 0 : FW_BOLD, |
4efb3868 |
465 | FALSE, FALSE, FALSE, DEFAULT_CHARSET, |
466 | OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, |
467 | DEFAULT_QUALITY, |
468 | (fonttype == FONT_FIXED ? |
469 | FIXED_PITCH | FF_DONTCARE : |
470 | VARIABLE_PITCH | FF_SWISS), |
471 | NULL); |
472 | } |
473 | |
474 | /* |
475 | * Position and draw the text. |
476 | */ |
477 | { |
478 | HFONT oldfont; |
479 | TEXTMETRIC tm; |
480 | SIZE size; |
481 | |
821ab2c6 |
482 | oldfont = SelectObject(fe->hdc, fe->fonts[i].font); |
483 | if (GetTextMetrics(fe->hdc, &tm)) { |
4efb3868 |
484 | if (align & ALIGN_VCENTRE) |
821ab2c6 |
485 | xy.y -= (tm.tmAscent+tm.tmDescent)/2; |
4efb3868 |
486 | else |
821ab2c6 |
487 | xy.y -= tm.tmAscent; |
4efb3868 |
488 | } |
821ab2c6 |
489 | if (GetTextExtentPoint32(fe->hdc, text, strlen(text), &size)) { |
4efb3868 |
490 | if (align & ALIGN_HCENTRE) |
821ab2c6 |
491 | xy.x -= size.cx / 2; |
4efb3868 |
492 | else if (align & ALIGN_HRIGHT) |
821ab2c6 |
493 | xy.x -= size.cx; |
4efb3868 |
494 | } |
821ab2c6 |
495 | SetBkMode(fe->hdc, TRANSPARENT); |
496 | win_text_colour(fe, colour); |
497 | TextOut(fe->hdc, xy.x, xy.y, text, strlen(text)); |
498 | SelectObject(fe->hdc, oldfont); |
4efb3868 |
499 | } |
500 | } |
501 | |
dafd6cf6 |
502 | static void win_draw_rect(void *handle, int x, int y, int w, int h, int colour) |
4e7ef6e6 |
503 | { |
dafd6cf6 |
504 | frontend *fe = (frontend *)handle; |
821ab2c6 |
505 | POINT p, q; |
506 | |
507 | if (fe->drawstatus == NOTHING) |
508 | return; |
509 | |
510 | if (fe->drawstatus == DRAWING && w == 1 && h == 1) { |
fbdd7610 |
511 | /* |
512 | * Rectangle() appears to get uppity if asked to draw a 1x1 |
513 | * rectangle, presumably on the grounds that that's beneath |
514 | * its dignity and you ought to be using SetPixel instead. |
515 | * So I will. |
516 | */ |
821ab2c6 |
517 | SetPixel(fe->hdc, x, y, fe->colours[colour]); |
fbdd7610 |
518 | } else { |
821ab2c6 |
519 | win_set_brush(fe, colour); |
520 | win_set_pen(fe, colour, TRUE); |
521 | p = win_transform_point(fe, x, y); |
522 | q = win_transform_point(fe, x+w, y+h); |
523 | Rectangle(fe->hdc, p.x, p.y, q.x, q.y); |
524 | win_reset_brush(fe); |
525 | win_reset_pen(fe); |
fbdd7610 |
526 | } |
4e7ef6e6 |
527 | } |
528 | |
dafd6cf6 |
529 | static void win_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour) |
4e7ef6e6 |
530 | { |
dafd6cf6 |
531 | frontend *fe = (frontend *)handle; |
821ab2c6 |
532 | POINT p, q; |
533 | |
534 | if (fe->drawstatus == NOTHING) |
535 | return; |
536 | |
537 | win_set_pen(fe, colour, FALSE); |
538 | p = win_transform_point(fe, x1, y1); |
539 | q = win_transform_point(fe, x2, y2); |
540 | MoveToEx(fe->hdc, p.x, p.y, NULL); |
541 | LineTo(fe->hdc, q.x, q.y); |
542 | if (fe->drawstatus == DRAWING) |
543 | SetPixel(fe->hdc, q.x, q.y, fe->colours[colour]); |
544 | win_reset_pen(fe); |
4e7ef6e6 |
545 | } |
546 | |
dafd6cf6 |
547 | static void win_draw_circle(void *handle, int cx, int cy, int radius, |
548 | int fillcolour, int outlinecolour) |
550efd7f |
549 | { |
dafd6cf6 |
550 | frontend *fe = (frontend *)handle; |
821ab2c6 |
551 | POINT p, q, r; |
552 | |
28b5987d |
553 | assert(outlinecolour >= 0); |
554 | |
821ab2c6 |
555 | if (fe->drawstatus == NOTHING) |
556 | return; |
557 | |
28b5987d |
558 | if (fillcolour >= 0) { |
821ab2c6 |
559 | win_set_brush(fe, fillcolour); |
560 | win_set_pen(fe, outlinecolour, FALSE); |
561 | p = win_transform_point(fe, cx - radius, cy - radius); |
562 | q = win_transform_point(fe, cx + radius, cy + radius); |
563 | Ellipse(fe->hdc, p.x, p.y, q.x+1, q.y+1); |
564 | win_reset_brush(fe); |
565 | win_reset_pen(fe); |
550efd7f |
566 | } else { |
821ab2c6 |
567 | win_set_pen(fe, outlinecolour, FALSE); |
568 | p = win_transform_point(fe, cx - radius, cy - radius); |
569 | q = win_transform_point(fe, cx + radius, cy + radius); |
570 | r = win_transform_point(fe, cx - radius, cy); |
571 | Arc(fe->hdc, p.x, p.y, q.x+1, q.y+1, r.x, r.y, r.x, r.y); |
572 | win_reset_pen(fe); |
84cf18f7 |
573 | } |
550efd7f |
574 | } |
575 | |
dafd6cf6 |
576 | static void win_draw_polygon(void *handle, int *coords, int npoints, |
577 | int fillcolour, int outlinecolour) |
4e7ef6e6 |
578 | { |
dafd6cf6 |
579 | frontend *fe = (frontend *)handle; |
821ab2c6 |
580 | POINT *pts; |
4e7ef6e6 |
581 | int i; |
582 | |
821ab2c6 |
583 | if (fe->drawstatus == NOTHING) |
584 | return; |
585 | |
586 | pts = snewn(npoints+1, POINT); |
587 | |
4e7ef6e6 |
588 | for (i = 0; i <= npoints; i++) { |
589 | int j = (i < npoints ? i : 0); |
821ab2c6 |
590 | pts[i] = win_transform_point(fe, coords[j*2], coords[j*2+1]); |
4e7ef6e6 |
591 | } |
592 | |
28b5987d |
593 | assert(outlinecolour >= 0); |
594 | |
595 | if (fillcolour >= 0) { |
821ab2c6 |
596 | win_set_brush(fe, fillcolour); |
597 | win_set_pen(fe, outlinecolour, FALSE); |
598 | Polygon(fe->hdc, pts, npoints); |
599 | win_reset_brush(fe); |
600 | win_reset_pen(fe); |
4e7ef6e6 |
601 | } else { |
821ab2c6 |
602 | win_set_pen(fe, outlinecolour, FALSE); |
603 | Polyline(fe->hdc, pts, npoints+1); |
604 | win_reset_pen(fe); |
4e7ef6e6 |
605 | } |
606 | |
607 | sfree(pts); |
608 | } |
609 | |
dafd6cf6 |
610 | static void win_start_draw(void *handle) |
4e7ef6e6 |
611 | { |
dafd6cf6 |
612 | frontend *fe = (frontend *)handle; |
4e7ef6e6 |
613 | HDC hdc_win; |
821ab2c6 |
614 | |
615 | assert(fe->drawstatus == NOTHING); |
616 | |
4e7ef6e6 |
617 | hdc_win = GetDC(fe->hwnd); |
821ab2c6 |
618 | fe->hdc = CreateCompatibleDC(hdc_win); |
619 | fe->prevbm = SelectObject(fe->hdc, fe->bitmap); |
4e7ef6e6 |
620 | ReleaseDC(fe->hwnd, hdc_win); |
4efb3868 |
621 | fe->clip = NULL; |
821ab2c6 |
622 | SetMapMode(fe->hdc, MM_TEXT); |
623 | fe->drawstatus = DRAWING; |
4e7ef6e6 |
624 | } |
625 | |
dafd6cf6 |
626 | static void win_draw_update(void *handle, int x, int y, int w, int h) |
4e7ef6e6 |
627 | { |
dafd6cf6 |
628 | frontend *fe = (frontend *)handle; |
4e7ef6e6 |
629 | RECT r; |
630 | |
821ab2c6 |
631 | if (fe->drawstatus != DRAWING) |
632 | return; |
633 | |
4e7ef6e6 |
634 | r.left = x; |
635 | r.top = y; |
636 | r.right = x + w; |
637 | r.bottom = y + h; |
638 | |
639 | InvalidateRect(fe->hwnd, &r, FALSE); |
640 | } |
641 | |
dafd6cf6 |
642 | static void win_end_draw(void *handle) |
4e7ef6e6 |
643 | { |
dafd6cf6 |
644 | frontend *fe = (frontend *)handle; |
821ab2c6 |
645 | assert(fe->drawstatus == DRAWING); |
646 | SelectObject(fe->hdc, fe->prevbm); |
647 | DeleteDC(fe->hdc); |
4efb3868 |
648 | if (fe->clip) { |
649 | DeleteObject(fe->clip); |
650 | fe->clip = NULL; |
651 | } |
821ab2c6 |
652 | fe->drawstatus = NOTHING; |
653 | } |
654 | |
655 | static void win_line_width(void *handle, float width) |
656 | { |
657 | frontend *fe = (frontend *)handle; |
658 | |
659 | assert(fe->drawstatus != DRAWING); |
660 | if (fe->drawstatus == NOTHING) |
661 | return; |
662 | |
663 | fe->linewidth = (int)(width * fe->printpixelscale); |
664 | } |
665 | |
666 | static void win_begin_doc(void *handle, int pages) |
667 | { |
668 | frontend *fe = (frontend *)handle; |
669 | |
670 | assert(fe->drawstatus != DRAWING); |
671 | if (fe->drawstatus == NOTHING) |
672 | return; |
673 | |
674 | if (StartDoc(fe->hdc, &fe->di) <= 0) { |
675 | char *e = geterrstr(); |
676 | MessageBox(fe->hwnd, e, "Error starting to print", |
677 | MB_ICONERROR | MB_OK); |
678 | sfree(e); |
679 | fe->drawstatus = NOTHING; |
680 | } |
681 | |
682 | /* |
683 | * Push a marker on the font stack so that we won't use the |
684 | * same fonts for printing and drawing. (This is because |
685 | * drawing seems to look generally better in bold, but printing |
686 | * is better not in bold.) |
687 | */ |
688 | fe->fontstart = fe->nfonts; |
689 | } |
690 | |
691 | static void win_begin_page(void *handle, int number) |
692 | { |
693 | frontend *fe = (frontend *)handle; |
694 | |
695 | assert(fe->drawstatus != DRAWING); |
696 | if (fe->drawstatus == NOTHING) |
697 | return; |
698 | |
699 | if (StartPage(fe->hdc) <= 0) { |
700 | char *e = geterrstr(); |
701 | MessageBox(fe->hwnd, e, "Error starting a page", |
702 | MB_ICONERROR | MB_OK); |
703 | sfree(e); |
704 | fe->drawstatus = NOTHING; |
705 | } |
706 | } |
707 | |
708 | static void win_begin_puzzle(void *handle, float xm, float xc, |
709 | float ym, float yc, int pw, int ph, float wmm) |
710 | { |
711 | frontend *fe = (frontend *)handle; |
712 | int ppw, pph, pox, poy; |
713 | float mmpw, mmph, mmox, mmoy; |
714 | float scale; |
715 | |
716 | assert(fe->drawstatus != DRAWING); |
717 | if (fe->drawstatus == NOTHING) |
718 | return; |
719 | |
720 | ppw = GetDeviceCaps(fe->hdc, HORZRES); |
721 | pph = GetDeviceCaps(fe->hdc, VERTRES); |
722 | mmpw = (float)GetDeviceCaps(fe->hdc, HORZSIZE); |
723 | mmph = (float)GetDeviceCaps(fe->hdc, VERTSIZE); |
724 | |
725 | /* |
726 | * Compute the puzzle's position on the logical page. |
727 | */ |
728 | mmox = xm * mmpw + xc; |
729 | mmoy = ym * mmph + yc; |
730 | |
731 | /* |
732 | * Work out what that comes to in pixels. |
733 | */ |
734 | pox = (int)(mmox * (float)ppw / mmpw); |
735 | poy = (int)(mmoy * (float)ppw / mmpw); |
736 | |
737 | /* |
738 | * And determine the scale. |
739 | * |
740 | * I need a scale such that the maximum puzzle-coordinate |
741 | * extent of the rectangle (pw * scale) is equal to the pixel |
742 | * equivalent of the puzzle's millimetre width (wmm * ppw / |
743 | * mmpw). |
744 | */ |
745 | scale = (wmm * ppw) / (mmpw * pw); |
746 | |
747 | /* |
748 | * Now store pox, poy and scale for use in the main drawing |
749 | * functions. |
750 | */ |
751 | fe->printoffsetx = pox; |
752 | fe->printoffsety = poy; |
753 | fe->printpixelscale = scale; |
754 | |
755 | fe->linewidth = 1; |
756 | } |
757 | |
758 | static void win_end_puzzle(void *handle) |
759 | { |
760 | /* Nothing needs to be done here. */ |
761 | } |
762 | |
763 | static void win_end_page(void *handle, int number) |
764 | { |
765 | frontend *fe = (frontend *)handle; |
766 | |
767 | assert(fe->drawstatus != DRAWING); |
92efb79d |
768 | |
821ab2c6 |
769 | if (fe->drawstatus == NOTHING) |
770 | return; |
771 | |
772 | if (EndPage(fe->hdc) <= 0) { |
773 | char *e = geterrstr(); |
774 | MessageBox(fe->hwnd, e, "Error finishing a page", |
775 | MB_ICONERROR | MB_OK); |
776 | sfree(e); |
777 | fe->drawstatus = NOTHING; |
778 | } |
779 | } |
780 | |
781 | static void win_end_doc(void *handle) |
782 | { |
783 | frontend *fe = (frontend *)handle; |
784 | |
785 | assert(fe->drawstatus != DRAWING); |
786 | |
787 | /* |
788 | * Free all the fonts created since we began printing. |
789 | */ |
790 | while (fe->nfonts > fe->fontstart) { |
791 | fe->nfonts--; |
792 | DeleteObject(fe->fonts[fe->nfonts].font); |
793 | } |
794 | fe->fontstart = 0; |
795 | |
92efb79d |
796 | /* |
797 | * The MSDN web site sample code doesn't bother to call EndDoc |
798 | * if an error occurs half way through printing. I expect doing |
799 | * so would cause the erroneous document to actually be |
800 | * printed, or something equally undesirable. |
801 | */ |
821ab2c6 |
802 | if (fe->drawstatus == NOTHING) |
803 | return; |
804 | |
805 | if (EndDoc(fe->hdc) <= 0) { |
806 | char *e = geterrstr(); |
807 | MessageBox(fe->hwnd, e, "Error finishing printing", |
808 | MB_ICONERROR | MB_OK); |
809 | sfree(e); |
810 | fe->drawstatus = NOTHING; |
811 | } |
4e7ef6e6 |
812 | } |
813 | |
dafd6cf6 |
814 | const struct drawing_api win_drawing = { |
815 | win_draw_text, |
816 | win_draw_rect, |
817 | win_draw_line, |
818 | win_draw_polygon, |
819 | win_draw_circle, |
820 | win_draw_update, |
821 | win_clip, |
822 | win_unclip, |
823 | win_start_draw, |
824 | win_end_draw, |
825 | win_status_bar, |
826 | win_blitter_new, |
827 | win_blitter_free, |
828 | win_blitter_save, |
829 | win_blitter_load, |
821ab2c6 |
830 | win_begin_doc, |
831 | win_begin_page, |
832 | win_begin_puzzle, |
833 | win_end_puzzle, |
834 | win_end_page, |
835 | win_end_doc, |
836 | win_line_width, |
dafd6cf6 |
837 | }; |
838 | |
821ab2c6 |
839 | void print(frontend *fe) |
840 | { |
841 | PRINTDLG pd; |
842 | char doctitle[256]; |
843 | document *doc; |
844 | midend *nme = NULL; /* non-interactive midend for bulk puzzle generation */ |
845 | int i; |
846 | char *err = NULL; |
847 | |
848 | /* |
849 | * Create our document structure and fill it up with puzzles. |
850 | */ |
851 | doc = document_new(fe->printw, fe->printh, fe->printscale / 100.0F); |
852 | for (i = 0; i < fe->printcount; i++) { |
853 | if (i == 0 && fe->printcurr) { |
854 | err = midend_print_puzzle(fe->me, doc, fe->printsolns); |
855 | } else { |
856 | if (!nme) { |
857 | game_params *params; |
858 | |
859 | nme = midend_new(NULL, &thegame, NULL, NULL); |
860 | |
861 | /* |
862 | * Set the non-interactive mid-end to have the same |
863 | * parameters as the standard one. |
864 | */ |
865 | params = midend_get_params(fe->me); |
866 | midend_set_params(nme, params); |
867 | thegame.free_params(params); |
868 | } |
869 | |
870 | midend_new_game(nme); |
871 | err = midend_print_puzzle(nme, doc, fe->printsolns); |
872 | } |
873 | if (err) |
874 | break; |
875 | } |
876 | if (nme) |
877 | midend_free(nme); |
878 | |
879 | if (err) { |
880 | MessageBox(fe->hwnd, err, "Error preparing puzzles for printing", |
881 | MB_ICONERROR | MB_OK); |
882 | document_free(doc); |
883 | return; |
884 | } |
885 | |
886 | memset(&pd, 0, sizeof(pd)); |
887 | pd.lStructSize = sizeof(pd); |
888 | pd.hwndOwner = fe->hwnd; |
889 | pd.hDevMode = NULL; |
890 | pd.hDevNames = NULL; |
891 | pd.Flags = PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC | |
892 | PD_NOPAGENUMS | PD_NOSELECTION; |
893 | pd.nCopies = 1; |
894 | pd.nFromPage = pd.nToPage = 0xFFFF; |
895 | pd.nMinPage = pd.nMaxPage = 1; |
896 | |
92efb79d |
897 | if (!PrintDlg(&pd)) { |
898 | document_free(doc); |
821ab2c6 |
899 | return; |
92efb79d |
900 | } |
821ab2c6 |
901 | |
902 | /* |
903 | * Now pd.hDC is a device context for the printer. |
904 | */ |
905 | |
906 | /* |
907 | * FIXME: IWBNI we put up an Abort box here. |
908 | */ |
909 | |
910 | memset(&fe->di, 0, sizeof(fe->di)); |
911 | fe->di.cbSize = sizeof(fe->di); |
912 | sprintf(doctitle, "Printed puzzles from %s (from Simon Tatham's" |
913 | " Portable Puzzle Collection)", thegame.name); |
914 | fe->di.lpszDocName = doctitle; |
915 | fe->di.lpszOutput = NULL; |
916 | fe->di.lpszDatatype = NULL; |
917 | fe->di.fwType = 0; |
918 | |
919 | fe->drawstatus = PRINTING; |
920 | fe->hdc = pd.hDC; |
921 | |
83c0438f |
922 | fe->dr = drawing_new(&win_drawing, NULL, fe); |
821ab2c6 |
923 | document_print(doc, fe->dr); |
924 | drawing_free(fe->dr); |
925 | fe->dr = NULL; |
926 | |
927 | fe->drawstatus = NOTHING; |
928 | |
929 | DeleteDC(pd.hDC); |
92efb79d |
930 | document_free(doc); |
821ab2c6 |
931 | } |
932 | |
4e7ef6e6 |
933 | void deactivate_timer(frontend *fe) |
934 | { |
821ab2c6 |
935 | if (!fe) |
936 | return; /* for non-interactive midend */ |
986cc2de |
937 | if (fe->hwnd) KillTimer(fe->hwnd, fe->timer); |
4e7ef6e6 |
938 | fe->timer = 0; |
939 | } |
940 | |
941 | void activate_timer(frontend *fe) |
942 | { |
821ab2c6 |
943 | if (!fe) |
944 | return; /* for non-interactive midend */ |
8c1fd974 |
945 | if (!fe->timer) { |
946 | fe->timer = SetTimer(fe->hwnd, fe->timer, 20, NULL); |
947 | fe->timer_last_tickcount = GetTickCount(); |
948 | } |
4e7ef6e6 |
949 | } |
950 | |
1cdfcdbd |
951 | void write_clip(HWND hwnd, char *data) |
952 | { |
953 | HGLOBAL clipdata; |
6d5cbf79 |
954 | int len, i, j; |
955 | char *data2; |
1cdfcdbd |
956 | void *lock; |
957 | |
6d5cbf79 |
958 | /* |
959 | * Windows expects CRLF in the clipboard, so we must convert |
960 | * any \n that has come out of the puzzle backend. |
961 | */ |
962 | len = 0; |
963 | for (i = 0; data[i]; i++) { |
964 | if (data[i] == '\n') |
965 | len++; |
966 | len++; |
967 | } |
968 | data2 = snewn(len+1, char); |
969 | j = 0; |
970 | for (i = 0; data[i]; i++) { |
971 | if (data[i] == '\n') |
972 | data2[j++] = '\r'; |
973 | data2[j++] = data[i]; |
974 | } |
975 | assert(j == len); |
976 | data2[j] = '\0'; |
977 | |
1cdfcdbd |
978 | clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1); |
979 | if (!clipdata) |
980 | return; |
981 | lock = GlobalLock(clipdata); |
982 | if (!lock) |
983 | return; |
6d5cbf79 |
984 | memcpy(lock, data2, len); |
1cdfcdbd |
985 | ((unsigned char *) lock)[len] = 0; |
986 | GlobalUnlock(clipdata); |
987 | |
988 | if (OpenClipboard(hwnd)) { |
989 | EmptyClipboard(); |
990 | SetClipboardData(CF_TEXT, clipdata); |
991 | CloseClipboard(); |
992 | } else |
993 | GlobalFree(clipdata); |
6d5cbf79 |
994 | |
995 | sfree(data2); |
1cdfcdbd |
996 | } |
997 | |
e91825f8 |
998 | /* |
750037d7 |
999 | * Set up Help and see if we can find a help file. |
e91825f8 |
1000 | */ |
750037d7 |
1001 | static void init_help(void) |
e91825f8 |
1002 | { |
1003 | char b[2048], *p, *q, *r; |
1004 | FILE *fp; |
750037d7 |
1005 | |
1006 | /* |
1007 | * Find the executable file path, so we can look alongside |
1008 | * it for help files. Trim the filename off the end. |
1009 | */ |
1010 | GetModuleFileName(NULL, b, sizeof(b) - 1); |
1011 | r = b; |
1012 | p = strrchr(b, '\\'); |
1013 | if (p && p >= r) r = p+1; |
1014 | q = strrchr(b, ':'); |
1015 | if (q && q >= r) r = q+1; |
1016 | |
291c690f |
1017 | #ifndef NO_HTMLHELP |
750037d7 |
1018 | /* |
1019 | * Try HTML Help first. |
1020 | */ |
1021 | strcpy(r, CHM_FILE_NAME); |
1022 | if ( (fp = fopen(b, "r")) != NULL) { |
1023 | fclose(fp); |
1024 | |
1025 | /* |
1026 | * We have a .CHM. See if we can use it. |
1027 | */ |
1028 | hh_dll = LoadLibrary("hhctrl.ocx"); |
1029 | if (hh_dll) { |
1030 | htmlhelp = (htmlhelp_t)GetProcAddress(hh_dll, "HtmlHelpA"); |
1031 | if (!htmlhelp) |
1032 | FreeLibrary(hh_dll); |
1033 | } |
1034 | if (htmlhelp) { |
750037d7 |
1035 | help_path = dupstr(b); |
1036 | help_type = CHM; |
1037 | help_topic = thegame.htmlhelp_topic; |
1038 | return; |
1039 | } |
1040 | } |
291c690f |
1041 | #endif /* NO_HTMLHELP */ |
750037d7 |
1042 | |
1043 | /* |
1044 | * Now try old-style .HLP. |
1045 | */ |
1046 | strcpy(r, HELP_FILE_NAME); |
1047 | if ( (fp = fopen(b, "r")) != NULL) { |
1048 | fclose(fp); |
1049 | |
1050 | help_path = dupstr(b); |
1051 | help_type = HLP; |
1052 | |
1053 | help_topic = thegame.winhelp_topic; |
1054 | |
1055 | /* |
1056 | * See if there's a .CNT file alongside it. |
1057 | */ |
1058 | strcpy(r, HELP_CNT_NAME); |
1059 | if ( (fp = fopen(b, "r")) != NULL) { |
1060 | fclose(fp); |
1061 | help_has_contents = TRUE; |
1062 | } else |
1063 | help_has_contents = FALSE; |
1064 | |
1065 | return; |
1066 | } |
1067 | |
1068 | help_type = NONE; /* didn't find any */ |
1069 | } |
1070 | |
1071 | /* |
1072 | * Start Help. |
1073 | */ |
ed656ed4 |
1074 | static void start_help(frontend *fe, const char *topic) |
750037d7 |
1075 | { |
1076 | char *str = NULL; |
1077 | int cmd; |
1078 | |
1079 | switch (help_type) { |
1080 | case HLP: |
1081 | assert(help_path); |
1082 | if (topic) { |
1083 | str = snewn(10+strlen(topic), char); |
1084 | sprintf(str, "JI(`',`%s')", topic); |
1085 | cmd = HELP_COMMAND; |
1086 | } else if (help_has_contents) { |
1087 | cmd = HELP_FINDER; |
1088 | } else { |
1089 | cmd = HELP_CONTENTS; |
1090 | } |
1091 | WinHelp(fe->hwnd, help_path, cmd, (DWORD)str); |
1092 | fe->help_running = TRUE; |
1093 | break; |
1094 | case CHM: |
291c690f |
1095 | #ifndef NO_HTMLHELP |
750037d7 |
1096 | assert(help_path); |
1097 | assert(htmlhelp); |
1098 | if (topic) { |
1099 | str = snewn(20 + strlen(topic) + strlen(help_path), char); |
1100 | sprintf(str, "%s::/%s.html>main", help_path, topic); |
1101 | } else { |
1102 | str = dupstr(help_path); |
1103 | } |
1104 | htmlhelp(fe->hwnd, str, HH_DISPLAY_TOPIC, 0); |
1105 | fe->help_running = TRUE; |
1106 | break; |
291c690f |
1107 | #endif /* NO_HTMLHELP */ |
750037d7 |
1108 | case NONE: |
1109 | assert(!"This shouldn't happen"); |
1110 | break; |
1111 | } |
1112 | |
1113 | sfree(str); |
1114 | } |
1115 | |
1116 | /* |
1117 | * Stop Help on window cleanup. |
1118 | */ |
1119 | static void stop_help(frontend *fe) |
1120 | { |
1121 | if (fe->help_running) { |
1122 | switch (help_type) { |
1123 | case HLP: |
1124 | WinHelp(fe->hwnd, help_path, HELP_QUIT, 0); |
1125 | break; |
1126 | case CHM: |
291c690f |
1127 | #ifndef NO_HTMLHELP |
750037d7 |
1128 | assert(htmlhelp); |
1129 | htmlhelp(NULL, NULL, HH_CLOSE_ALL, 0); |
1130 | break; |
291c690f |
1131 | #endif /* NO_HTMLHELP */ |
750037d7 |
1132 | case NONE: |
1133 | assert(!"This shouldn't happen"); |
1134 | break; |
1135 | } |
1136 | fe->help_running = FALSE; |
1137 | } |
1138 | } |
1139 | |
1140 | /* |
1141 | * Terminate Help on process exit. |
1142 | */ |
1143 | static void cleanup_help(void) |
1144 | { |
39384997 |
1145 | /* Nothing to do currently. |
1146 | * (If we were running HTML Help single-threaded, this is where we'd |
1147 | * call HH_UNINITIALIZE.) */ |
e91825f8 |
1148 | } |
1149 | |
1e3e152d |
1150 | static void check_window_size(frontend *fe, int *px, int *py) |
1151 | { |
1152 | RECT r; |
1153 | int x, y, sy; |
1154 | |
1155 | if (fe->statusbar) { |
1156 | RECT sr; |
1157 | GetWindowRect(fe->statusbar, &sr); |
1158 | sy = sr.bottom - sr.top; |
1159 | } else { |
1160 | sy = 0; |
1161 | } |
1162 | |
1163 | /* |
1164 | * See if we actually got the window size we wanted, and adjust |
1165 | * the puzzle size if not. |
1166 | */ |
1167 | GetClientRect(fe->hwnd, &r); |
1168 | x = r.right - r.left; |
1169 | y = r.bottom - r.top - sy; |
1170 | midend_size(fe->me, &x, &y, FALSE); |
1171 | if (x != r.right - r.left || y != r.bottom - r.top) { |
1172 | /* |
1173 | * Resize the window, now we know what size we _really_ |
1174 | * want it to be. |
1175 | */ |
1176 | r.left = r.top = 0; |
1177 | r.right = x; |
1178 | r.bottom = y + sy; |
35ee2001 |
1179 | AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0); |
1e3e152d |
1180 | SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left, r.bottom - r.top, |
1181 | SWP_NOMOVE | SWP_NOZORDER); |
1182 | } |
1183 | |
1184 | if (fe->statusbar) { |
1185 | GetClientRect(fe->hwnd, &r); |
1186 | SetWindowPos(fe->statusbar, NULL, 0, r.bottom-r.top-sy, r.right-r.left, |
1187 | sy, SWP_NOZORDER); |
1188 | } |
1189 | |
1190 | *px = x; |
1191 | *py = y; |
1192 | } |
1193 | |
35ee2001 |
1194 | static void get_max_puzzle_size(frontend *fe, int *x, int *y) |
1195 | { |
1196 | RECT r, sr; |
1197 | |
1198 | if (SystemParametersInfo(SPI_GETWORKAREA, 0, &sr, FALSE)) { |
1199 | *x = sr.right - sr.left; |
1200 | *y = sr.bottom - sr.top; |
1201 | r.left = 100; |
1202 | r.right = 200; |
1203 | r.top = 100; |
1204 | r.bottom = 200; |
1205 | AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0); |
1206 | *x -= r.right - r.left - 100; |
1207 | *y -= r.bottom - r.top - 100; |
1208 | } else { |
1209 | *x = *y = INT_MAX; |
1210 | } |
1211 | |
1212 | if (fe->statusbar != NULL) { |
1213 | GetWindowRect(fe->statusbar, &sr); |
1214 | *y -= sr.bottom - sr.top; |
1215 | } |
1216 | } |
1217 | |
8b7938e7 |
1218 | static frontend *new_window(HINSTANCE inst, char *game_id, char **error) |
4e7ef6e6 |
1219 | { |
1220 | frontend *fe; |
1221 | int x, y; |
1e3e152d |
1222 | RECT r; |
4e7ef6e6 |
1223 | |
1224 | fe = snew(frontend); |
48d70ca9 |
1225 | |
dafd6cf6 |
1226 | fe->me = midend_new(fe, &thegame, &win_drawing, fe); |
48d70ca9 |
1227 | |
8b7938e7 |
1228 | if (game_id) { |
1185e3c5 |
1229 | *error = midend_game_id(fe->me, game_id); |
8b7938e7 |
1230 | if (*error) { |
1231 | midend_free(fe->me); |
1232 | sfree(fe); |
1233 | return NULL; |
1234 | } |
1235 | } |
1236 | |
077f3cbe |
1237 | fe->inst = inst; |
4e7ef6e6 |
1238 | |
1239 | fe->timer = 0; |
986cc2de |
1240 | fe->hwnd = NULL; |
1241 | |
750037d7 |
1242 | fe->help_running = FALSE; |
1243 | |
821ab2c6 |
1244 | fe->drawstatus = NOTHING; |
1245 | fe->dr = NULL; |
1246 | fe->fontstart = 0; |
1247 | |
986cc2de |
1248 | midend_new_game(fe->me); |
4e7ef6e6 |
1249 | |
6beae131 |
1250 | fe->fonts = NULL; |
1251 | fe->nfonts = fe->fontsize = 0; |
1252 | |
4e7ef6e6 |
1253 | { |
1254 | int i, ncolours; |
1255 | float *colours; |
1256 | |
1257 | colours = midend_colours(fe->me, &ncolours); |
1258 | |
1259 | fe->colours = snewn(ncolours, COLORREF); |
1260 | fe->brushes = snewn(ncolours, HBRUSH); |
1261 | fe->pens = snewn(ncolours, HPEN); |
1262 | |
1263 | for (i = 0; i < ncolours; i++) { |
1264 | fe->colours[i] = RGB(255 * colours[i*3+0], |
1265 | 255 * colours[i*3+1], |
1266 | 255 * colours[i*3+2]); |
1267 | fe->brushes[i] = CreateSolidBrush(fe->colours[i]); |
4e7ef6e6 |
1268 | fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]); |
1269 | } |
9cfc03b7 |
1270 | sfree(colours); |
4e7ef6e6 |
1271 | } |
1272 | |
35ee2001 |
1273 | if (midend_wants_statusbar(fe->me)) { |
1274 | fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh", |
1275 | WS_CHILD | WS_VISIBLE, |
1276 | 0, 0, 0, 0, /* status bar does these */ |
1277 | NULL, NULL, inst, NULL); |
e490a96e |
1278 | } else |
1279 | fe->statusbar = NULL; |
35ee2001 |
1280 | |
1281 | get_max_puzzle_size(fe, &x, &y); |
1e3e152d |
1282 | midend_size(fe->me, &x, &y, FALSE); |
1283 | |
4e7ef6e6 |
1284 | r.left = r.top = 0; |
1285 | r.right = x; |
1286 | r.bottom = y; |
35ee2001 |
1287 | AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0); |
4e7ef6e6 |
1288 | |
be8d5aa1 |
1289 | fe->hwnd = CreateWindowEx(0, thegame.name, thegame.name, |
4e7ef6e6 |
1290 | WS_OVERLAPPEDWINDOW &~ |
1291 | (WS_THICKFRAME | WS_MAXIMIZEBOX), |
1292 | CW_USEDEFAULT, CW_USEDEFAULT, |
1293 | r.right - r.left, r.bottom - r.top, |
1294 | NULL, NULL, inst, NULL); |
1295 | |
3fee7a57 |
1296 | if (midend_wants_statusbar(fe->me)) { |
1297 | RECT sr; |
35ee2001 |
1298 | DestroyWindow(fe->statusbar); |
1e3e152d |
1299 | fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh", |
1300 | WS_CHILD | WS_VISIBLE, |
1301 | 0, 0, 0, 0, /* status bar does these */ |
1302 | fe->hwnd, NULL, inst, NULL); |
3fee7a57 |
1303 | /* |
1304 | * Now resize the window to take account of the status bar. |
1305 | */ |
1306 | GetWindowRect(fe->statusbar, &sr); |
1307 | GetWindowRect(fe->hwnd, &r); |
1308 | SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left, |
1309 | r.bottom - r.top + sr.bottom - sr.top, |
1310 | SWP_NOMOVE | SWP_NOZORDER); |
1311 | } else { |
1e3e152d |
1312 | fe->statusbar = NULL; |
3fee7a57 |
1313 | } |
1e3e152d |
1314 | |
eb2ad6f1 |
1315 | { |
1316 | HMENU bar = CreateMenu(); |
1317 | HMENU menu = CreateMenu(); |
1318 | |
1319 | AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Game"); |
1320 | AppendMenu(menu, MF_ENABLED, IDM_NEW, "New"); |
1321 | AppendMenu(menu, MF_ENABLED, IDM_RESTART, "Restart"); |
1185e3c5 |
1322 | AppendMenu(menu, MF_ENABLED, IDM_DESC, "Specific..."); |
1323 | AppendMenu(menu, MF_ENABLED, IDM_SEED, "Random Seed..."); |
eb2ad6f1 |
1324 | |
077f3cbe |
1325 | if ((fe->npresets = midend_num_presets(fe->me)) > 0 || |
be8d5aa1 |
1326 | thegame.can_configure) { |
eb2ad6f1 |
1327 | HMENU sub = CreateMenu(); |
1328 | int i; |
1329 | |
077f3cbe |
1330 | AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "Type"); |
eb2ad6f1 |
1331 | |
1332 | fe->presets = snewn(fe->npresets, game_params *); |
1333 | |
1334 | for (i = 0; i < fe->npresets; i++) { |
1335 | char *name; |
1336 | |
1337 | midend_fetch_preset(fe->me, i, &name, &fe->presets[i]); |
1338 | |
1339 | /* |
1340 | * FIXME: we ought to go through and do something |
1341 | * with ampersands here. |
1342 | */ |
1343 | |
1344 | AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name); |
1345 | } |
077f3cbe |
1346 | |
be8d5aa1 |
1347 | if (thegame.can_configure) { |
077f3cbe |
1348 | AppendMenu(sub, MF_ENABLED, IDM_CONFIG, "Custom..."); |
1349 | } |
eb2ad6f1 |
1350 | } |
1351 | |
1352 | AppendMenu(menu, MF_SEPARATOR, 0, 0); |
cccb7f09 |
1353 | AppendMenu(menu, MF_ENABLED, IDM_LOAD, "Load..."); |
1354 | AppendMenu(menu, MF_ENABLED, IDM_SAVE, "Save..."); |
668be019 |
1355 | AppendMenu(menu, MF_SEPARATOR, 0, 0); |
821ab2c6 |
1356 | if (thegame.can_print) { |
cccb7f09 |
1357 | AppendMenu(menu, MF_ENABLED, IDM_PRINT, "Print..."); |
821ab2c6 |
1358 | AppendMenu(menu, MF_SEPARATOR, 0, 0); |
1359 | } |
eb2ad6f1 |
1360 | AppendMenu(menu, MF_ENABLED, IDM_UNDO, "Undo"); |
1361 | AppendMenu(menu, MF_ENABLED, IDM_REDO, "Redo"); |
1cdfcdbd |
1362 | if (thegame.can_format_as_text) { |
1363 | AppendMenu(menu, MF_SEPARATOR, 0, 0); |
1364 | AppendMenu(menu, MF_ENABLED, IDM_COPY, "Copy"); |
1365 | } |
2ac6d24e |
1366 | if (thegame.can_solve) { |
1367 | AppendMenu(menu, MF_SEPARATOR, 0, 0); |
1368 | AppendMenu(menu, MF_ENABLED, IDM_SOLVE, "Solve"); |
1369 | } |
eb2ad6f1 |
1370 | AppendMenu(menu, MF_SEPARATOR, 0, 0); |
1371 | AppendMenu(menu, MF_ENABLED, IDM_QUIT, "Exit"); |
97098757 |
1372 | menu = CreateMenu(); |
1373 | AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Help"); |
1374 | AppendMenu(menu, MF_ENABLED, IDM_ABOUT, "About"); |
750037d7 |
1375 | if (help_type != NONE) { |
97098757 |
1376 | AppendMenu(menu, MF_SEPARATOR, 0, 0); |
1377 | AppendMenu(menu, MF_ENABLED, IDM_HELPC, "Contents"); |
750037d7 |
1378 | if (help_topic) { |
e91825f8 |
1379 | char *item; |
be8d5aa1 |
1380 | assert(thegame.name); |
1381 | item = snewn(9+strlen(thegame.name), char); /*ick*/ |
1382 | sprintf(item, "Help on %s", thegame.name); |
97098757 |
1383 | AppendMenu(menu, MF_ENABLED, IDM_GAMEHELP, item); |
e91825f8 |
1384 | sfree(item); |
1385 | } |
1386 | } |
eb2ad6f1 |
1387 | SetMenu(fe->hwnd, bar); |
1388 | } |
1389 | |
e490a96e |
1390 | fe->bitmap = NULL; |
1391 | new_game_size(fe); /* initialises fe->bitmap */ |
1e3e152d |
1392 | check_window_size(fe, &x, &y); |
fd1a1a2b |
1393 | |
4e7ef6e6 |
1394 | SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe); |
1395 | |
1396 | ShowWindow(fe->hwnd, SW_NORMAL); |
1397 | SetForegroundWindow(fe->hwnd); |
1398 | |
1399 | midend_redraw(fe->me); |
1400 | |
1401 | return fe; |
1402 | } |
1403 | |
97098757 |
1404 | static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg, |
1405 | WPARAM wParam, LPARAM lParam) |
1406 | { |
1407 | frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA); |
1408 | |
1409 | switch (msg) { |
1410 | case WM_INITDIALOG: |
1411 | return 0; |
1412 | |
1413 | case WM_COMMAND: |
1414 | if ((HIWORD(wParam) == BN_CLICKED || |
1415 | HIWORD(wParam) == BN_DOUBLECLICKED) && |
1416 | LOWORD(wParam) == IDOK) |
1417 | fe->dlg_done = 1; |
1418 | return 0; |
1419 | |
1420 | case WM_CLOSE: |
1421 | fe->dlg_done = 1; |
1422 | return 0; |
1423 | } |
1424 | |
1425 | return 0; |
1426 | } |
1427 | |
821ab2c6 |
1428 | /* |
1429 | * Wrappers on midend_{get,set}_config, which extend the CFG_* |
1430 | * enumeration to add CFG_PRINT. |
1431 | */ |
1432 | static config_item *frontend_get_config(frontend *fe, int which, |
1433 | char **wintitle) |
1434 | { |
1435 | if (which < CFG_FRONTEND_SPECIFIC) { |
1436 | return midend_get_config(fe->me, which, wintitle); |
1437 | } else if (which == CFG_PRINT) { |
1438 | config_item *ret; |
1439 | int i; |
1440 | |
1441 | *wintitle = snewn(40 + strlen(thegame.name), char); |
1442 | sprintf(*wintitle, "%s print setup", thegame.name); |
1443 | |
1444 | ret = snewn(8, config_item); |
1445 | |
1446 | i = 0; |
1447 | |
1448 | ret[i].name = "Number of puzzles to print"; |
1449 | ret[i].type = C_STRING; |
1450 | ret[i].sval = dupstr("1"); |
1451 | ret[i].ival = 0; |
1452 | i++; |
1453 | |
1454 | ret[i].name = "Number of puzzles across the page"; |
1455 | ret[i].type = C_STRING; |
1456 | ret[i].sval = dupstr("1"); |
1457 | ret[i].ival = 0; |
1458 | i++; |
1459 | |
1460 | ret[i].name = "Number of puzzles down the page"; |
1461 | ret[i].type = C_STRING; |
1462 | ret[i].sval = dupstr("1"); |
1463 | ret[i].ival = 0; |
1464 | i++; |
1465 | |
1466 | ret[i].name = "Percentage of standard size"; |
1467 | ret[i].type = C_STRING; |
1468 | ret[i].sval = dupstr("100.0"); |
1469 | ret[i].ival = 0; |
1470 | i++; |
1471 | |
1472 | ret[i].name = "Include currently shown puzzle"; |
1473 | ret[i].type = C_BOOLEAN; |
1474 | ret[i].sval = NULL; |
1475 | ret[i].ival = TRUE; |
1476 | i++; |
1477 | |
1478 | ret[i].name = "Print solutions"; |
1479 | ret[i].type = C_BOOLEAN; |
1480 | ret[i].sval = NULL; |
1481 | ret[i].ival = FALSE; |
1482 | i++; |
1483 | |
1484 | if (thegame.can_print_in_colour) { |
1485 | ret[i].name = "Print in colour"; |
1486 | ret[i].type = C_BOOLEAN; |
1487 | ret[i].sval = NULL; |
1488 | ret[i].ival = FALSE; |
1489 | i++; |
1490 | } |
1491 | |
1492 | ret[i].name = NULL; |
1493 | ret[i].type = C_END; |
1494 | ret[i].sval = NULL; |
1495 | ret[i].ival = 0; |
1496 | i++; |
1497 | |
1498 | return ret; |
1499 | } else { |
1500 | assert(!"We should never get here"); |
1501 | return NULL; |
1502 | } |
1503 | } |
1504 | |
1505 | static char *frontend_set_config(frontend *fe, int which, config_item *cfg) |
1506 | { |
1507 | if (which < CFG_FRONTEND_SPECIFIC) { |
1508 | return midend_set_config(fe->me, which, cfg); |
1509 | } else if (which == CFG_PRINT) { |
1510 | if ((fe->printcount = atoi(cfg[0].sval)) <= 0) |
1511 | return "Number of puzzles to print should be at least one"; |
1512 | if ((fe->printw = atoi(cfg[1].sval)) <= 0) |
1513 | return "Number of puzzles across the page should be at least one"; |
1514 | if ((fe->printh = atoi(cfg[2].sval)) <= 0) |
1515 | return "Number of puzzles down the page should be at least one"; |
1516 | if ((fe->printscale = (float)atof(cfg[3].sval)) <= 0) |
1517 | return "Print size should be positive"; |
1518 | fe->printcurr = cfg[4].ival; |
1519 | fe->printsolns = cfg[5].ival; |
1520 | fe->printcolour = thegame.can_print_in_colour && cfg[6].ival; |
1521 | return NULL; |
1522 | } else { |
1523 | assert(!"We should never get here"); |
1524 | return "Internal error"; |
1525 | } |
1526 | } |
1527 | |
077f3cbe |
1528 | static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg, |
1529 | WPARAM wParam, LPARAM lParam) |
1530 | { |
1531 | frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA); |
1532 | config_item *i; |
1533 | struct cfg_aux *j; |
1534 | |
1535 | switch (msg) { |
1536 | case WM_INITDIALOG: |
1537 | return 0; |
1538 | |
1539 | case WM_COMMAND: |
1540 | /* |
1541 | * OK and Cancel are special cases. |
1542 | */ |
1543 | if ((HIWORD(wParam) == BN_CLICKED || |
1544 | HIWORD(wParam) == BN_DOUBLECLICKED) && |
1545 | (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) { |
1546 | if (LOWORD(wParam) == IDOK) { |
821ab2c6 |
1547 | char *err = frontend_set_config(fe, fe->cfg_which, fe->cfg); |
077f3cbe |
1548 | |
1549 | if (err) { |
1550 | MessageBox(hwnd, err, "Validation error", |
1551 | MB_ICONERROR | MB_OK); |
1552 | } else { |
97098757 |
1553 | fe->dlg_done = 2; |
077f3cbe |
1554 | } |
1555 | } else { |
97098757 |
1556 | fe->dlg_done = 1; |
077f3cbe |
1557 | } |
1558 | return 0; |
1559 | } |
1560 | |
1561 | /* |
1562 | * First find the control whose id this is. |
1563 | */ |
1564 | for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) { |
1565 | if (j->ctlid == LOWORD(wParam)) |
1566 | break; |
1567 | } |
1568 | if (i->type == C_END) |
1569 | return 0; /* not our problem */ |
1570 | |
1571 | if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) { |
1572 | char buffer[4096]; |
1573 | GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer)); |
1574 | buffer[lenof(buffer)-1] = '\0'; |
1575 | sfree(i->sval); |
1576 | i->sval = dupstr(buffer); |
1577 | } else if (i->type == C_BOOLEAN && |
1578 | (HIWORD(wParam) == BN_CLICKED || |
1579 | HIWORD(wParam) == BN_DOUBLECLICKED)) { |
1580 | i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid); |
1581 | } else if (i->type == C_CHOICES && |
1582 | HIWORD(wParam) == CBN_SELCHANGE) { |
1583 | i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid, |
1584 | CB_GETCURSEL, 0, 0); |
1585 | } |
1586 | |
1587 | return 0; |
1588 | |
1589 | case WM_CLOSE: |
97098757 |
1590 | fe->dlg_done = 1; |
077f3cbe |
1591 | return 0; |
1592 | } |
1593 | |
1594 | return 0; |
1595 | } |
1596 | |
1597 | HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2, |
1598 | char *wclass, int wstyle, |
97098757 |
1599 | int exstyle, const char *wtext, int wid) |
077f3cbe |
1600 | { |
1601 | HWND ret; |
1602 | ret = CreateWindowEx(exstyle, wclass, wtext, |
1603 | wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1, |
1604 | fe->cfgbox, (HMENU) wid, fe->inst, NULL); |
1605 | SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0)); |
1606 | return ret; |
1607 | } |
1608 | |
97098757 |
1609 | static void about(frontend *fe) |
1610 | { |
1611 | int i; |
1612 | WNDCLASS wc; |
1613 | MSG msg; |
1614 | TEXTMETRIC tm; |
1615 | HDC hdc; |
1616 | HFONT oldfont; |
1617 | SIZE size; |
1618 | int gm, id; |
1619 | int winwidth, winheight, y; |
1620 | int height, width, maxwid; |
1621 | const char *strings[16]; |
1622 | int lengths[16]; |
1623 | int nstrings = 0; |
1624 | char titlebuf[512]; |
1625 | |
1626 | sprintf(titlebuf, "About %.250s", thegame.name); |
1627 | |
1628 | strings[nstrings++] = thegame.name; |
1629 | strings[nstrings++] = "from Simon Tatham's Portable Puzzle Collection"; |
1630 | strings[nstrings++] = ver; |
1631 | |
1632 | wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW; |
1633 | wc.lpfnWndProc = DefDlgProc; |
1634 | wc.cbClsExtra = 0; |
1635 | wc.cbWndExtra = DLGWINDOWEXTRA + 8; |
1636 | wc.hInstance = fe->inst; |
1637 | wc.hIcon = NULL; |
1638 | wc.hCursor = LoadCursor(NULL, IDC_ARROW); |
1639 | wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1); |
1640 | wc.lpszMenuName = NULL; |
1641 | wc.lpszClassName = "GameAboutBox"; |
1642 | RegisterClass(&wc); |
1643 | |
1644 | hdc = GetDC(fe->hwnd); |
1645 | SetMapMode(hdc, MM_TEXT); |
1646 | |
1647 | fe->dlg_done = FALSE; |
1648 | |
1649 | fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72), |
1650 | 0, 0, 0, 0, |
1651 | FALSE, FALSE, FALSE, DEFAULT_CHARSET, |
1652 | OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, |
1653 | DEFAULT_QUALITY, |
1654 | FF_SWISS, |
1655 | "MS Shell Dlg"); |
1656 | |
1657 | oldfont = SelectObject(hdc, fe->cfgfont); |
1658 | if (GetTextMetrics(hdc, &tm)) { |
1659 | height = tm.tmAscent + tm.tmDescent; |
1660 | width = tm.tmAveCharWidth; |
1661 | } else { |
1662 | height = width = 30; |
1663 | } |
1664 | |
1665 | /* |
1666 | * Figure out the layout of the About box by measuring the |
1667 | * length of each piece of text. |
1668 | */ |
1669 | maxwid = 0; |
1670 | winheight = height/2; |
1671 | |
1672 | for (i = 0; i < nstrings; i++) { |
1673 | if (GetTextExtentPoint32(hdc, strings[i], strlen(strings[i]), &size)) |
1674 | lengths[i] = size.cx; |
1675 | else |
1676 | lengths[i] = 0; /* *shrug* */ |
1677 | if (maxwid < lengths[i]) |
1678 | maxwid = lengths[i]; |
1679 | winheight += height * 3 / 2 + (height / 2); |
1680 | } |
1681 | |
1682 | winheight += height + height * 7 / 4; /* OK button */ |
1683 | winwidth = maxwid + 4*width; |
1684 | |
1685 | SelectObject(hdc, oldfont); |
1686 | ReleaseDC(fe->hwnd, hdc); |
1687 | |
1688 | /* |
1689 | * Create the dialog, now that we know its size. |
1690 | */ |
1691 | { |
1692 | RECT r, r2; |
1693 | |
1694 | r.left = r.top = 0; |
1695 | r.right = winwidth; |
1696 | r.bottom = winheight; |
1697 | |
1698 | AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*| |
1699 | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | |
1700 | WS_CAPTION | WS_SYSMENU*/) &~ |
1701 | (WS_MAXIMIZEBOX | WS_OVERLAPPED), |
1702 | FALSE, 0); |
1703 | |
1704 | /* |
1705 | * Centre the dialog on its parent window. |
1706 | */ |
1707 | r.right -= r.left; |
1708 | r.bottom -= r.top; |
1709 | GetWindowRect(fe->hwnd, &r2); |
1710 | r.left = (r2.left + r2.right - r.right) / 2; |
1711 | r.top = (r2.top + r2.bottom - r.bottom) / 2; |
1712 | r.right += r.left; |
1713 | r.bottom += r.top; |
1714 | |
1715 | fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, titlebuf, |
1716 | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | |
1717 | WS_CAPTION | WS_SYSMENU, |
1718 | r.left, r.top, |
1719 | r.right-r.left, r.bottom-r.top, |
1720 | fe->hwnd, NULL, fe->inst, NULL); |
1721 | } |
1722 | |
1723 | SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE); |
1724 | |
1725 | SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe); |
1726 | SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)AboutDlgProc); |
1727 | |
1728 | id = 1000; |
1729 | y = height/2; |
1730 | for (i = 0; i < nstrings; i++) { |
1731 | int border = width*2 + (maxwid - lengths[i]) / 2; |
1732 | mkctrl(fe, border, border+lengths[i], y+height*1/8, y+height*9/8, |
1733 | "Static", 0, 0, strings[i], id++); |
1734 | y += height*3/2; |
1735 | |
1736 | assert(y < winheight); |
1737 | y += height/2; |
1738 | } |
1739 | |
1740 | y += height/2; /* extra space before OK */ |
1741 | mkctrl(fe, width*2, maxwid+width*2, y, y+height*7/4, "BUTTON", |
1742 | BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0, |
1743 | "OK", IDOK); |
1744 | |
1745 | SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0); |
1746 | |
1747 | EnableWindow(fe->hwnd, FALSE); |
1748 | ShowWindow(fe->cfgbox, SW_NORMAL); |
1749 | while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) { |
1750 | if (!IsDialogMessage(fe->cfgbox, &msg)) |
1751 | DispatchMessage(&msg); |
1752 | if (fe->dlg_done) |
1753 | break; |
1754 | } |
1755 | EnableWindow(fe->hwnd, TRUE); |
1756 | SetForegroundWindow(fe->hwnd); |
1757 | DestroyWindow(fe->cfgbox); |
1758 | DeleteObject(fe->cfgfont); |
1759 | } |
1760 | |
5928817c |
1761 | static int get_config(frontend *fe, int which) |
077f3cbe |
1762 | { |
1763 | config_item *i; |
1764 | struct cfg_aux *j; |
5928817c |
1765 | char *title; |
077f3cbe |
1766 | WNDCLASS wc; |
1767 | MSG msg; |
1768 | TEXTMETRIC tm; |
1769 | HDC hdc; |
1770 | HFONT oldfont; |
1771 | SIZE size; |
1772 | HWND ctl; |
1773 | int gm, id, nctrls; |
1774 | int winwidth, winheight, col1l, col1r, col2l, col2r, y; |
1775 | int height, width, maxlabel, maxcheckbox; |
1776 | |
1777 | wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW; |
1778 | wc.lpfnWndProc = DefDlgProc; |
1779 | wc.cbClsExtra = 0; |
1780 | wc.cbWndExtra = DLGWINDOWEXTRA + 8; |
1781 | wc.hInstance = fe->inst; |
1782 | wc.hIcon = NULL; |
1783 | wc.hCursor = LoadCursor(NULL, IDC_ARROW); |
1784 | wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1); |
1785 | wc.lpszMenuName = NULL; |
1786 | wc.lpszClassName = "GameConfigBox"; |
1787 | RegisterClass(&wc); |
1788 | |
1789 | hdc = GetDC(fe->hwnd); |
1790 | SetMapMode(hdc, MM_TEXT); |
1791 | |
97098757 |
1792 | fe->dlg_done = FALSE; |
077f3cbe |
1793 | |
1794 | fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72), |
1795 | 0, 0, 0, 0, |
1796 | FALSE, FALSE, FALSE, DEFAULT_CHARSET, |
1797 | OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, |
1798 | DEFAULT_QUALITY, |
1799 | FF_SWISS, |
1800 | "MS Shell Dlg"); |
1801 | |
1802 | oldfont = SelectObject(hdc, fe->cfgfont); |
1803 | if (GetTextMetrics(hdc, &tm)) { |
1804 | height = tm.tmAscent + tm.tmDescent; |
1805 | width = tm.tmAveCharWidth; |
1806 | } else { |
1807 | height = width = 30; |
1808 | } |
1809 | |
821ab2c6 |
1810 | fe->cfg = frontend_get_config(fe, which, &title); |
5928817c |
1811 | fe->cfg_which = which; |
077f3cbe |
1812 | |
1813 | /* |
1814 | * Figure out the layout of the config box by measuring the |
1815 | * length of each piece of text. |
1816 | */ |
1817 | maxlabel = maxcheckbox = 0; |
1818 | winheight = height/2; |
1819 | |
1820 | for (i = fe->cfg; i->type != C_END; i++) { |
1821 | switch (i->type) { |
1822 | case C_STRING: |
1823 | case C_CHOICES: |
1824 | /* |
1825 | * Both these control types have a label filling only |
1826 | * the left-hand column of the box. |
1827 | */ |
1828 | if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) && |
1829 | maxlabel < size.cx) |
1830 | maxlabel = size.cx; |
1831 | winheight += height * 3 / 2 + (height / 2); |
1832 | break; |
1833 | |
1834 | case C_BOOLEAN: |
1835 | /* |
1836 | * Checkboxes take up the whole of the box width. |
1837 | */ |
1838 | if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) && |
1839 | maxcheckbox < size.cx) |
1840 | maxcheckbox = size.cx; |
1841 | winheight += height + (height / 2); |
1842 | break; |
1843 | } |
1844 | } |
1845 | |
1846 | winheight += height + height * 7 / 4; /* OK / Cancel buttons */ |
1847 | |
1848 | col1l = 2*width; |
1849 | col1r = col1l + maxlabel; |
1850 | col2l = col1r + 2*width; |
1851 | col2r = col2l + 30*width; |
1852 | if (col2r < col1l+2*height+maxcheckbox) |
1853 | col2r = col1l+2*height+maxcheckbox; |
1854 | winwidth = col2r + 2*width; |
1855 | |
97098757 |
1856 | SelectObject(hdc, oldfont); |
077f3cbe |
1857 | ReleaseDC(fe->hwnd, hdc); |
1858 | |
1859 | /* |
1860 | * Create the dialog, now that we know its size. |
1861 | */ |
1862 | { |
1863 | RECT r, r2; |
1864 | |
1865 | r.left = r.top = 0; |
1866 | r.right = winwidth; |
1867 | r.bottom = winheight; |
1868 | |
1869 | AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*| |
1870 | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | |
1871 | WS_CAPTION | WS_SYSMENU*/) &~ |
1872 | (WS_MAXIMIZEBOX | WS_OVERLAPPED), |
1873 | FALSE, 0); |
1874 | |
1875 | /* |
1876 | * Centre the dialog on its parent window. |
1877 | */ |
1878 | r.right -= r.left; |
1879 | r.bottom -= r.top; |
1880 | GetWindowRect(fe->hwnd, &r2); |
1881 | r.left = (r2.left + r2.right - r.right) / 2; |
1882 | r.top = (r2.top + r2.bottom - r.bottom) / 2; |
1883 | r.right += r.left; |
1884 | r.bottom += r.top; |
1885 | |
5928817c |
1886 | fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title, |
077f3cbe |
1887 | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | |
1888 | WS_CAPTION | WS_SYSMENU, |
1889 | r.left, r.top, |
1890 | r.right-r.left, r.bottom-r.top, |
1891 | fe->hwnd, NULL, fe->inst, NULL); |
5928817c |
1892 | sfree(title); |
077f3cbe |
1893 | } |
1894 | |
1895 | SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE); |
1896 | |
1897 | SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe); |
1898 | SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc); |
1899 | |
1900 | /* |
1901 | * Count the controls so we can allocate cfgaux. |
1902 | */ |
1903 | for (nctrls = 0, i = fe->cfg; i->type != C_END; i++) |
1904 | nctrls++; |
1905 | fe->cfgaux = snewn(nctrls, struct cfg_aux); |
1906 | |
1907 | id = 1000; |
1908 | y = height/2; |
1909 | for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) { |
1910 | switch (i->type) { |
1911 | case C_STRING: |
1912 | /* |
1913 | * Edit box with a label beside it. |
1914 | */ |
1915 | mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8, |
1916 | "Static", 0, 0, i->name, id++); |
1917 | ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2, |
1918 | "EDIT", WS_TABSTOP | ES_AUTOHSCROLL, |
1919 | WS_EX_CLIENTEDGE, "", (j->ctlid = id++)); |
1920 | SetWindowText(ctl, i->sval); |
1921 | y += height*3/2; |
1922 | break; |
1923 | |
1924 | case C_BOOLEAN: |
1925 | /* |
1926 | * Simple checkbox. |
1927 | */ |
1928 | mkctrl(fe, col1l, col2r, y, y+height, "BUTTON", |
1929 | BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP, |
1930 | 0, i->name, (j->ctlid = id++)); |
42132f2d |
1931 | CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0)); |
077f3cbe |
1932 | y += height; |
1933 | break; |
1934 | |
1935 | case C_CHOICES: |
1936 | /* |
1937 | * Drop-down list with a label beside it. |
1938 | */ |
1939 | mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8, |
1940 | "STATIC", 0, 0, i->name, id++); |
1941 | ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2, |
1942 | "COMBOBOX", WS_TABSTOP | |
1943 | CBS_DROPDOWNLIST | CBS_HASSTRINGS, |
1944 | WS_EX_CLIENTEDGE, "", (j->ctlid = id++)); |
1945 | { |
1946 | char c, *p, *q, *str; |
1947 | |
1948 | SendMessage(ctl, CB_RESETCONTENT, 0, 0); |
1949 | p = i->sval; |
1950 | c = *p++; |
1951 | while (*p) { |
1952 | q = p; |
1953 | while (*q && *q != c) q++; |
1954 | str = snewn(q-p+1, char); |
1955 | strncpy(str, p, q-p); |
1956 | str[q-p] = '\0'; |
1957 | SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str); |
1958 | sfree(str); |
1959 | if (*q) q++; |
1960 | p = q; |
1961 | } |
1962 | } |
1963 | |
1964 | SendMessage(ctl, CB_SETCURSEL, i->ival, 0); |
1965 | |
1966 | y += height*3/2; |
1967 | break; |
1968 | } |
1969 | |
1970 | assert(y < winheight); |
1971 | y += height/2; |
1972 | } |
1973 | |
1974 | y += height/2; /* extra space before OK and Cancel */ |
1975 | mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON", |
1976 | BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0, |
1977 | "OK", IDOK); |
1978 | mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON", |
1979 | BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL); |
1980 | |
1981 | SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0); |
1982 | |
1983 | EnableWindow(fe->hwnd, FALSE); |
1984 | ShowWindow(fe->cfgbox, SW_NORMAL); |
1985 | while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) { |
1986 | if (!IsDialogMessage(fe->cfgbox, &msg)) |
1987 | DispatchMessage(&msg); |
97098757 |
1988 | if (fe->dlg_done) |
077f3cbe |
1989 | break; |
1990 | } |
1991 | EnableWindow(fe->hwnd, TRUE); |
1992 | SetForegroundWindow(fe->hwnd); |
1993 | DestroyWindow(fe->cfgbox); |
1994 | DeleteObject(fe->cfgfont); |
1995 | |
1996 | free_cfg(fe->cfg); |
1997 | sfree(fe->cfgaux); |
1998 | |
97098757 |
1999 | return (fe->dlg_done == 2); |
077f3cbe |
2000 | } |
2001 | |
668be019 |
2002 | static void new_game_size(frontend *fe) |
077f3cbe |
2003 | { |
2004 | RECT r, sr; |
2005 | HDC hdc; |
2006 | int x, y; |
2007 | |
35ee2001 |
2008 | get_max_puzzle_size(fe, &x, &y); |
1e3e152d |
2009 | midend_size(fe->me, &x, &y, FALSE); |
077f3cbe |
2010 | |
2011 | r.left = r.top = 0; |
2012 | r.right = x; |
2013 | r.bottom = y; |
35ee2001 |
2014 | AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0); |
077f3cbe |
2015 | |
2016 | if (fe->statusbar != NULL) { |
2017 | GetWindowRect(fe->statusbar, &sr); |
2018 | } else { |
2019 | sr.left = sr.right = sr.top = sr.bottom = 0; |
2020 | } |
2021 | SetWindowPos(fe->hwnd, NULL, 0, 0, |
2022 | r.right - r.left, |
2023 | r.bottom - r.top + sr.bottom - sr.top, |
2024 | SWP_NOMOVE | SWP_NOZORDER); |
1e3e152d |
2025 | |
2026 | check_window_size(fe, &x, &y); |
2027 | |
077f3cbe |
2028 | if (fe->statusbar != NULL) |
2029 | SetWindowPos(fe->statusbar, NULL, 0, y, x, |
2030 | sr.bottom - sr.top, SWP_NOZORDER); |
2031 | |
e490a96e |
2032 | if (fe->bitmap) DeleteObject(fe->bitmap); |
077f3cbe |
2033 | |
2034 | hdc = GetDC(fe->hwnd); |
2035 | fe->bitmap = CreateCompatibleBitmap(hdc, x, y); |
2036 | ReleaseDC(fe->hwnd, hdc); |
2037 | |
2038 | midend_redraw(fe->me); |
2039 | } |
2040 | |
668be019 |
2041 | static void new_game_type(frontend *fe) |
2042 | { |
2043 | midend_new_game(fe->me); |
2044 | new_game_size(fe); |
2045 | } |
2046 | |
97c2790f |
2047 | static int is_alt_pressed(void) |
2048 | { |
2049 | BYTE keystate[256]; |
2050 | int r = GetKeyboardState(keystate); |
2051 | if (!r) |
2052 | return FALSE; |
2053 | if (keystate[VK_MENU] & 0x80) |
2054 | return TRUE; |
2055 | if (keystate[VK_RMENU] & 0x80) |
2056 | return TRUE; |
2057 | return FALSE; |
2058 | } |
2059 | |
668be019 |
2060 | static void savefile_write(void *wctx, void *buf, int len) |
2061 | { |
2062 | FILE *fp = (FILE *)wctx; |
2063 | fwrite(buf, 1, len, fp); |
2064 | } |
2065 | |
2066 | static int savefile_read(void *wctx, void *buf, int len) |
2067 | { |
2068 | FILE *fp = (FILE *)wctx; |
2069 | int ret; |
2070 | |
2071 | ret = fread(buf, 1, len, fp); |
2072 | return (ret == len); |
2073 | } |
2074 | |
4e7ef6e6 |
2075 | static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, |
2076 | WPARAM wParam, LPARAM lParam) |
2077 | { |
2078 | frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA); |
668be019 |
2079 | int cmd; |
4e7ef6e6 |
2080 | |
2081 | switch (message) { |
2082 | case WM_CLOSE: |
2083 | DestroyWindow(hwnd); |
2084 | return 0; |
eb2ad6f1 |
2085 | case WM_COMMAND: |
668be019 |
2086 | cmd = wParam & ~0xF; /* low 4 bits reserved to Windows */ |
2087 | switch (cmd) { |
eb2ad6f1 |
2088 | case IDM_NEW: |
2089 | if (!midend_process_key(fe->me, 0, 0, 'n')) |
2090 | PostQuitMessage(0); |
2091 | break; |
2092 | case IDM_RESTART: |
7f89707c |
2093 | midend_restart_game(fe->me); |
eb2ad6f1 |
2094 | break; |
2095 | case IDM_UNDO: |
2096 | if (!midend_process_key(fe->me, 0, 0, 'u')) |
2097 | PostQuitMessage(0); |
2098 | break; |
2099 | case IDM_REDO: |
2100 | if (!midend_process_key(fe->me, 0, 0, '\x12')) |
2101 | PostQuitMessage(0); |
2102 | break; |
1cdfcdbd |
2103 | case IDM_COPY: |
2104 | { |
2105 | char *text = midend_text_format(fe->me); |
2106 | if (text) |
2107 | write_clip(hwnd, text); |
2108 | else |
2109 | MessageBeep(MB_ICONWARNING); |
6d5cbf79 |
2110 | sfree(text); |
1cdfcdbd |
2111 | } |
2112 | break; |
2ac6d24e |
2113 | case IDM_SOLVE: |
2114 | { |
2115 | char *msg = midend_solve(fe->me); |
2116 | if (msg) |
2117 | MessageBox(hwnd, msg, "Unable to solve", |
2118 | MB_ICONERROR | MB_OK); |
2119 | } |
2120 | break; |
eb2ad6f1 |
2121 | case IDM_QUIT: |
2122 | if (!midend_process_key(fe->me, 0, 0, 'q')) |
2123 | PostQuitMessage(0); |
2124 | break; |
077f3cbe |
2125 | case IDM_CONFIG: |
5928817c |
2126 | if (get_config(fe, CFG_SETTINGS)) |
2127 | new_game_type(fe); |
2128 | break; |
2129 | case IDM_SEED: |
2130 | if (get_config(fe, CFG_SEED)) |
077f3cbe |
2131 | new_game_type(fe); |
2132 | break; |
1185e3c5 |
2133 | case IDM_DESC: |
2134 | if (get_config(fe, CFG_DESC)) |
2135 | new_game_type(fe); |
2136 | break; |
821ab2c6 |
2137 | case IDM_PRINT: |
2138 | if (get_config(fe, CFG_PRINT)) |
2139 | print(fe); |
2140 | break; |
97098757 |
2141 | case IDM_ABOUT: |
2142 | about(fe); |
2143 | break; |
668be019 |
2144 | case IDM_LOAD: |
2145 | case IDM_SAVE: |
2146 | { |
2147 | OPENFILENAME of; |
2148 | char filename[FILENAME_MAX]; |
2149 | int ret; |
2150 | |
2151 | memset(&of, 0, sizeof(of)); |
2152 | of.hwndOwner = hwnd; |
2153 | of.lpstrFilter = "All Files (*.*)\0*\0\0\0"; |
2154 | of.lpstrCustomFilter = NULL; |
2155 | of.nFilterIndex = 1; |
2156 | of.lpstrFile = filename; |
2157 | filename[0] = '\0'; |
2158 | of.nMaxFile = lenof(filename); |
2159 | of.lpstrFileTitle = NULL; |
2160 | of.lpstrTitle = (cmd == IDM_SAVE ? |
2161 | "Enter name of game file to save" : |
2162 | "Enter name of saved game file to load"); |
2163 | of.Flags = 0; |
2164 | #ifdef OPENFILENAME_SIZE_VERSION_400 |
2165 | of.lStructSize = OPENFILENAME_SIZE_VERSION_400; |
2166 | #else |
2167 | of.lStructSize = sizeof(of); |
2168 | #endif |
2169 | of.lpstrInitialDir = NULL; |
2170 | |
2171 | if (cmd == IDM_SAVE) |
2172 | ret = GetSaveFileName(&of); |
2173 | else |
2174 | ret = GetOpenFileName(&of); |
2175 | |
2176 | if (ret) { |
2177 | if (cmd == IDM_SAVE) { |
2178 | FILE *fp; |
2179 | |
2180 | if ((fp = fopen(filename, "r")) != NULL) { |
2181 | char buf[256 + FILENAME_MAX]; |
2182 | fclose(fp); |
2183 | /* file exists */ |
2184 | |
2185 | sprintf(buf, "Are you sure you want to overwrite" |
2186 | " the file \"%.*s\"?", |
2187 | FILENAME_MAX, filename); |
2188 | if (MessageBox(hwnd, buf, "Question", |
2189 | MB_YESNO | MB_ICONQUESTION) |
2190 | != IDYES) |
2191 | break; |
2192 | } |
2193 | |
2194 | fp = fopen(filename, "w"); |
2195 | |
2196 | if (!fp) { |
2197 | MessageBox(hwnd, "Unable to open save file", |
2198 | "Error", MB_ICONERROR | MB_OK); |
2199 | break; |
2200 | } |
2201 | |
2202 | midend_serialise(fe->me, savefile_write, fp); |
2203 | |
2204 | fclose(fp); |
2205 | } else { |
2206 | FILE *fp = fopen(filename, "r"); |
2207 | char *err; |
2208 | |
2209 | if (!fp) { |
2210 | MessageBox(hwnd, "Unable to open saved game file", |
2211 | "Error", MB_ICONERROR | MB_OK); |
2212 | break; |
2213 | } |
2214 | |
2215 | err = midend_deserialise(fe->me, savefile_read, fp); |
2216 | |
2217 | fclose(fp); |
2218 | |
2219 | if (err) { |
2220 | MessageBox(hwnd, err, "Error", MB_ICONERROR|MB_OK); |
2221 | break; |
2222 | } |
2223 | |
2224 | new_game_size(fe); |
2225 | } |
2226 | } |
2227 | } |
2228 | |
2229 | break; |
e91825f8 |
2230 | case IDM_HELPC: |
750037d7 |
2231 | start_help(fe, NULL); |
2232 | break; |
e91825f8 |
2233 | case IDM_GAMEHELP: |
750037d7 |
2234 | start_help(fe, help_topic); |
e91825f8 |
2235 | break; |
eb2ad6f1 |
2236 | default: |
2237 | { |
2238 | int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10; |
2239 | |
2240 | if (p >= 0 && p < fe->npresets) { |
eb2ad6f1 |
2241 | midend_set_params(fe->me, fe->presets[p]); |
077f3cbe |
2242 | new_game_type(fe); |
eb2ad6f1 |
2243 | } |
2244 | } |
2245 | break; |
2246 | } |
2247 | break; |
4e7ef6e6 |
2248 | case WM_DESTROY: |
750037d7 |
2249 | stop_help(fe); |
4e7ef6e6 |
2250 | PostQuitMessage(0); |
2251 | return 0; |
2252 | case WM_PAINT: |
2253 | { |
2254 | PAINTSTRUCT p; |
2255 | HDC hdc, hdc2; |
2256 | HBITMAP prevbm; |
2257 | |
2258 | hdc = BeginPaint(hwnd, &p); |
2259 | hdc2 = CreateCompatibleDC(hdc); |
2260 | prevbm = SelectObject(hdc2, fe->bitmap); |
2261 | BitBlt(hdc, |
2262 | p.rcPaint.left, p.rcPaint.top, |
2263 | p.rcPaint.right - p.rcPaint.left, |
2264 | p.rcPaint.bottom - p.rcPaint.top, |
2265 | hdc2, |
2266 | p.rcPaint.left, p.rcPaint.top, |
2267 | SRCCOPY); |
2268 | SelectObject(hdc2, prevbm); |
2269 | DeleteDC(hdc2); |
2270 | EndPaint(hwnd, &p); |
2271 | } |
2272 | return 0; |
2273 | case WM_KEYDOWN: |
2274 | { |
2275 | int key = -1; |
f0ee053c |
2276 | BYTE keystate[256]; |
2277 | int r = GetKeyboardState(keystate); |
2278 | int shift = (r && (keystate[VK_SHIFT] & 0x80)) ? MOD_SHFT : 0; |
2279 | int ctrl = (r && (keystate[VK_CONTROL] & 0x80)) ? MOD_CTRL : 0; |
4e7ef6e6 |
2280 | |
2281 | switch (wParam) { |
3c833d45 |
2282 | case VK_LEFT: |
2283 | if (!(lParam & 0x01000000)) |
2284 | key = MOD_NUM_KEYPAD | '4'; |
f0ee053c |
2285 | else |
2286 | key = shift | ctrl | CURSOR_LEFT; |
3c833d45 |
2287 | break; |
2288 | case VK_RIGHT: |
2289 | if (!(lParam & 0x01000000)) |
2290 | key = MOD_NUM_KEYPAD | '6'; |
f0ee053c |
2291 | else |
2292 | key = shift | ctrl | CURSOR_RIGHT; |
3c833d45 |
2293 | break; |
2294 | case VK_UP: |
2295 | if (!(lParam & 0x01000000)) |
2296 | key = MOD_NUM_KEYPAD | '8'; |
f0ee053c |
2297 | else |
2298 | key = shift | ctrl | CURSOR_UP; |
3c833d45 |
2299 | break; |
2300 | case VK_DOWN: |
2301 | if (!(lParam & 0x01000000)) |
2302 | key = MOD_NUM_KEYPAD | '2'; |
f0ee053c |
2303 | else |
2304 | key = shift | ctrl | CURSOR_DOWN; |
3c833d45 |
2305 | break; |
c71454c0 |
2306 | /* |
2307 | * Diagonal keys on the numeric keypad. |
2308 | */ |
2309 | case VK_PRIOR: |
3c833d45 |
2310 | if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '9'; |
c71454c0 |
2311 | break; |
2312 | case VK_NEXT: |
3c833d45 |
2313 | if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '3'; |
c71454c0 |
2314 | break; |
2315 | case VK_HOME: |
3c833d45 |
2316 | if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '7'; |
c71454c0 |
2317 | break; |
2318 | case VK_END: |
3c833d45 |
2319 | if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '1'; |
2320 | break; |
2321 | case VK_INSERT: |
2322 | if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '0'; |
2323 | break; |
2324 | case VK_CLEAR: |
2325 | if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '5'; |
c71454c0 |
2326 | break; |
2327 | /* |
2328 | * Numeric keypad keys with Num Lock on. |
2329 | */ |
3c833d45 |
2330 | case VK_NUMPAD4: key = MOD_NUM_KEYPAD | '4'; break; |
2331 | case VK_NUMPAD6: key = MOD_NUM_KEYPAD | '6'; break; |
2332 | case VK_NUMPAD8: key = MOD_NUM_KEYPAD | '8'; break; |
2333 | case VK_NUMPAD2: key = MOD_NUM_KEYPAD | '2'; break; |
2334 | case VK_NUMPAD5: key = MOD_NUM_KEYPAD | '5'; break; |
2335 | case VK_NUMPAD9: key = MOD_NUM_KEYPAD | '9'; break; |
2336 | case VK_NUMPAD3: key = MOD_NUM_KEYPAD | '3'; break; |
2337 | case VK_NUMPAD7: key = MOD_NUM_KEYPAD | '7'; break; |
2338 | case VK_NUMPAD1: key = MOD_NUM_KEYPAD | '1'; break; |
2339 | case VK_NUMPAD0: key = MOD_NUM_KEYPAD | '0'; break; |
4e7ef6e6 |
2340 | } |
2341 | |
2342 | if (key != -1) { |
eb2ad6f1 |
2343 | if (!midend_process_key(fe->me, 0, 0, key)) |
4e7ef6e6 |
2344 | PostQuitMessage(0); |
c71454c0 |
2345 | } else { |
2346 | MSG m; |
2347 | m.hwnd = hwnd; |
2348 | m.message = WM_KEYDOWN; |
2349 | m.wParam = wParam; |
2350 | m.lParam = lParam & 0xdfff; |
2351 | TranslateMessage(&m); |
4e7ef6e6 |
2352 | } |
2353 | } |
2354 | break; |
2355 | case WM_LBUTTONDOWN: |
2356 | case WM_RBUTTONDOWN: |
2357 | case WM_MBUTTONDOWN: |
b0f06719 |
2358 | { |
2359 | int button; |
2360 | |
2361 | /* |
2362 | * Shift-clicks count as middle-clicks, since otherwise |
2363 | * two-button Windows users won't have any kind of |
2364 | * middle click to use. |
2365 | */ |
2366 | if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT)) |
2367 | button = MIDDLE_BUTTON; |
97c2790f |
2368 | else if (message == WM_RBUTTONDOWN || is_alt_pressed()) |
b0f06719 |
2369 | button = RIGHT_BUTTON; |
97c2790f |
2370 | else |
2371 | button = LEFT_BUTTON; |
74a4e547 |
2372 | |
5dda68e8 |
2373 | if (!midend_process_key(fe->me, (signed short)LOWORD(lParam), |
2374 | (signed short)HIWORD(lParam), button)) |
74a4e547 |
2375 | PostQuitMessage(0); |
2376 | |
2377 | SetCapture(hwnd); |
2378 | } |
2379 | break; |
2380 | case WM_LBUTTONUP: |
2381 | case WM_RBUTTONUP: |
2382 | case WM_MBUTTONUP: |
2383 | { |
2384 | int button; |
2385 | |
2386 | /* |
2387 | * Shift-clicks count as middle-clicks, since otherwise |
2388 | * two-button Windows users won't have any kind of |
2389 | * middle click to use. |
2390 | */ |
2391 | if (message == WM_MBUTTONUP || (wParam & MK_SHIFT)) |
2392 | button = MIDDLE_RELEASE; |
97c2790f |
2393 | else if (message == WM_RBUTTONUP || is_alt_pressed()) |
74a4e547 |
2394 | button = RIGHT_RELEASE; |
97c2790f |
2395 | else |
2396 | button = LEFT_RELEASE; |
74a4e547 |
2397 | |
5dda68e8 |
2398 | if (!midend_process_key(fe->me, (signed short)LOWORD(lParam), |
2399 | (signed short)HIWORD(lParam), button)) |
74a4e547 |
2400 | PostQuitMessage(0); |
2401 | |
2402 | ReleaseCapture(); |
2403 | } |
2404 | break; |
2405 | case WM_MOUSEMOVE: |
2406 | { |
2407 | int button; |
2408 | |
2409 | if (wParam & (MK_MBUTTON | MK_SHIFT)) |
2410 | button = MIDDLE_DRAG; |
97c2790f |
2411 | else if (wParam & MK_RBUTTON || is_alt_pressed()) |
74a4e547 |
2412 | button = RIGHT_DRAG; |
97c2790f |
2413 | else |
2414 | button = LEFT_DRAG; |
74a4e547 |
2415 | |
5dda68e8 |
2416 | if (!midend_process_key(fe->me, (signed short)LOWORD(lParam), |
2417 | (signed short)HIWORD(lParam), button)) |
b0f06719 |
2418 | PostQuitMessage(0); |
2419 | } |
4e7ef6e6 |
2420 | break; |
2421 | case WM_CHAR: |
eb2ad6f1 |
2422 | if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam)) |
4e7ef6e6 |
2423 | PostQuitMessage(0); |
2424 | return 0; |
2425 | case WM_TIMER: |
8c1fd974 |
2426 | if (fe->timer) { |
2427 | DWORD now = GetTickCount(); |
2428 | float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F; |
2429 | midend_timer(fe->me, elapsed); |
2430 | fe->timer_last_tickcount = now; |
2431 | } |
4e7ef6e6 |
2432 | return 0; |
2433 | } |
2434 | |
2435 | return DefWindowProc(hwnd, message, wParam, lParam); |
2436 | } |
2437 | |
2438 | int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) |
2439 | { |
2440 | MSG msg; |
8b7938e7 |
2441 | char *error; |
4e7ef6e6 |
2442 | |
fd1a1a2b |
2443 | InitCommonControls(); |
2444 | |
4e7ef6e6 |
2445 | if (!prev) { |
2446 | WNDCLASS wndclass; |
2447 | |
2448 | wndclass.style = 0; |
2449 | wndclass.lpfnWndProc = WndProc; |
2450 | wndclass.cbClsExtra = 0; |
2451 | wndclass.cbWndExtra = 0; |
2452 | wndclass.hInstance = inst; |
fa2cb13d |
2453 | wndclass.hIcon = LoadIcon(inst, MAKEINTRESOURCE(200)); |
2454 | if (!wndclass.hIcon) /* in case resource file is absent */ |
2455 | wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION); |
4e7ef6e6 |
2456 | wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); |
2457 | wndclass.hbrBackground = NULL; |
2458 | wndclass.lpszMenuName = NULL; |
be8d5aa1 |
2459 | wndclass.lpszClassName = thegame.name; |
4e7ef6e6 |
2460 | |
2461 | RegisterClass(&wndclass); |
2462 | } |
2463 | |
89167dad |
2464 | while (*cmdline && isspace((unsigned char)*cmdline)) |
8b7938e7 |
2465 | cmdline++; |
2466 | |
750037d7 |
2467 | init_help(); |
2468 | |
8b7938e7 |
2469 | if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) { |
2470 | char buf[128]; |
be8d5aa1 |
2471 | sprintf(buf, "%.100s Error", thegame.name); |
8b7938e7 |
2472 | MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR); |
2473 | return 1; |
2474 | } |
4e7ef6e6 |
2475 | |
2476 | while (GetMessage(&msg, NULL, 0, 0)) { |
4e7ef6e6 |
2477 | DispatchMessage(&msg); |
2478 | } |
2479 | |
750037d7 |
2480 | cleanup_help(); |
2481 | |
4e7ef6e6 |
2482 | return msg.wParam; |
2483 | } |