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 | |
cb0c7d4a |
11 | #ifdef _WIN32_WCE |
12 | #include <commdlg.h> |
13 | #include <aygshell.h> |
14 | #endif |
15 | |
4e7ef6e6 |
16 | #include <stdio.h> |
4efb3868 |
17 | #include <assert.h> |
8b7938e7 |
18 | #include <ctype.h> |
4e7ef6e6 |
19 | #include <stdarg.h> |
20 | #include <stdlib.h> |
1e3e152d |
21 | #include <limits.h> |
4e7ef6e6 |
22 | #include <time.h> |
23 | |
24 | #include "puzzles.h" |
25 | |
cb0c7d4a |
26 | #ifdef _WIN32_WCE |
27 | #include "resource.h" |
28 | #endif |
29 | |
eb2ad6f1 |
30 | #define IDM_NEW 0x0010 |
31 | #define IDM_RESTART 0x0020 |
32 | #define IDM_UNDO 0x0030 |
33 | #define IDM_REDO 0x0040 |
1cdfcdbd |
34 | #define IDM_COPY 0x0050 |
2ac6d24e |
35 | #define IDM_SOLVE 0x0060 |
36 | #define IDM_QUIT 0x0070 |
37 | #define IDM_CONFIG 0x0080 |
1185e3c5 |
38 | #define IDM_DESC 0x0090 |
39 | #define IDM_SEED 0x00A0 |
40 | #define IDM_HELPC 0x00B0 |
41 | #define IDM_GAMEHELP 0x00C0 |
fa7515b2 |
42 | #define IDM_ABOUT 0x00D0 |
668be019 |
43 | #define IDM_SAVE 0x00E0 |
44 | #define IDM_LOAD 0x00F0 |
821ab2c6 |
45 | #define IDM_PRINT 0x0100 |
46 | #define IDM_PRESETS 0x0110 |
dc3de726 |
47 | #define IDM_GAMES 0x0300 |
eb2ad6f1 |
48 | |
cb0c7d4a |
49 | #define IDM_KEYEMUL 0x0400 |
50 | |
e91825f8 |
51 | #define HELP_FILE_NAME "puzzles.hlp" |
52 | #define HELP_CNT_NAME "puzzles.cnt" |
291c690f |
53 | #ifndef NO_HTMLHELP |
750037d7 |
54 | #define CHM_FILE_NAME "puzzles.chm" |
291c690f |
55 | #endif /* NO_HTMLHELP */ |
750037d7 |
56 | |
291c690f |
57 | #ifndef NO_HTMLHELP |
750037d7 |
58 | typedef HWND (CALLBACK *htmlhelp_t)(HWND, LPCSTR, UINT, DWORD); |
750037d7 |
59 | static htmlhelp_t htmlhelp; |
60 | static HINSTANCE hh_dll; |
291c690f |
61 | #endif /* NO_HTMLHELP */ |
750037d7 |
62 | enum { NONE, HLP, CHM } help_type; |
63 | char *help_path; |
750037d7 |
64 | int help_has_contents; |
e91825f8 |
65 | |
cb0c7d4a |
66 | #ifndef FILENAME_MAX |
67 | #define FILENAME_MAX (260) |
68 | #endif |
69 | |
70 | #ifndef HGDI_ERROR |
71 | #define HGDI_ERROR ((HANDLE)GDI_ERROR) |
72 | #endif |
73 | |
dc3de726 |
74 | #ifdef COMBINED |
75 | #define CLASSNAME "Puzzles" |
76 | #else |
77 | #define CLASSNAME thegame.name |
78 | #endif |
79 | |
cb0c7d4a |
80 | #ifdef _WIN32_WCE |
81 | |
82 | /* |
83 | * Wrapper implementations of functions not supplied by the |
84 | * PocketPC API. |
85 | */ |
86 | |
87 | #define SHGetSubMenu(hWndMB,ID_MENU) (HMENU)SendMessage((hWndMB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)ID_MENU) |
88 | |
89 | #undef MessageBox |
90 | |
91 | int MessageBox(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType) |
92 | { |
93 | TCHAR wText[2048]; |
94 | TCHAR wCaption[2048]; |
95 | |
96 | MultiByteToWideChar (CP_ACP, 0, lpText, -1, wText, 2048); |
97 | MultiByteToWideChar (CP_ACP, 0, lpCaption, -1, wCaption, 2048); |
98 | |
99 | return MessageBoxW (hWnd, wText, wCaption, uType); |
100 | } |
101 | |
102 | BOOL SetDlgItemTextA(HWND hDlg, int nIDDlgItem, LPCSTR lpString) |
103 | { |
104 | TCHAR wText[256]; |
105 | |
106 | MultiByteToWideChar (CP_ACP, 0, lpString, -1, wText, 256); |
107 | return SetDlgItemTextW(hDlg, nIDDlgItem, wText); |
108 | } |
109 | |
110 | LPCSTR getenv(LPCSTR buf) |
111 | { |
112 | return NULL; |
113 | } |
114 | |
115 | BOOL GetKeyboardState(PBYTE pb) |
116 | { |
117 | return FALSE; |
118 | } |
119 | |
dc3de726 |
120 | static TCHAR wClassName[256], wGameName[256]; |
cb0c7d4a |
121 | |
122 | #endif |
123 | |
7b010604 |
124 | #ifdef DEBUGGING |
c71454c0 |
125 | static FILE *debug_fp = NULL; |
126 | static HANDLE debug_hdl = INVALID_HANDLE_VALUE; |
127 | static int debug_got_console = 0; |
128 | |
129 | void dputs(char *buf) |
130 | { |
dc3de726 |
131 | /*DWORD dw; |
c71454c0 |
132 | |
133 | if (!debug_got_console) { |
134 | if (AllocConsole()) { |
135 | debug_got_console = 1; |
136 | debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE); |
137 | } |
138 | } |
139 | if (!debug_fp) { |
140 | debug_fp = fopen("debug.log", "w"); |
141 | } |
142 | |
143 | if (debug_hdl != INVALID_HANDLE_VALUE) { |
144 | WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL); |
145 | } |
dc3de726 |
146 | if (debug_fp) { |
147 | fputs(buf, debug_fp); |
148 | fflush(debug_fp); |
149 | }*/ |
6b12623f |
150 | OutputDebugString(buf); |
c71454c0 |
151 | } |
152 | |
153 | void debug_printf(char *fmt, ...) |
154 | { |
155 | char buf[4096]; |
156 | va_list ap; |
157 | |
158 | va_start(ap, fmt); |
ab3a1e43 |
159 | _vsnprintf(buf, 4095, fmt, ap); |
c71454c0 |
160 | dputs(buf); |
161 | va_end(ap); |
162 | } |
c71454c0 |
163 | #endif |
164 | |
cb0c7d4a |
165 | #ifndef _WIN32_WCE |
35ee2001 |
166 | #define WINFLAGS (WS_OVERLAPPEDWINDOW &~ \ |
6b12623f |
167 | (WS_MAXIMIZEBOX | WS_OVERLAPPED)) |
cb0c7d4a |
168 | #else |
169 | #define WINFLAGS (WS_CAPTION | WS_SYSMENU) |
170 | #endif |
35ee2001 |
171 | |
dc3de726 |
172 | static void new_game_size(frontend *fe, float scale); |
35ee2001 |
173 | |
4efb3868 |
174 | struct font { |
175 | HFONT font; |
176 | int type; |
177 | int size; |
178 | }; |
179 | |
077f3cbe |
180 | struct cfg_aux { |
181 | int ctlid; |
182 | }; |
183 | |
3161048d |
184 | struct blitter { |
185 | HBITMAP bitmap; |
186 | frontend *fe; |
187 | int x, y, w, h; |
188 | }; |
189 | |
821ab2c6 |
190 | enum { CFG_PRINT = CFG_FRONTEND_SPECIFIC }; |
191 | |
4e7ef6e6 |
192 | struct frontend { |
dc3de726 |
193 | const game *game; |
dafd6cf6 |
194 | midend *me; |
077f3cbe |
195 | HWND hwnd, statusbar, cfgbox; |
cb0c7d4a |
196 | #ifdef _WIN32_WCE |
197 | HWND numpad; /* window handle for the numeric pad */ |
198 | #endif |
077f3cbe |
199 | HINSTANCE inst; |
4e7ef6e6 |
200 | HBITMAP bitmap, prevbm; |
cb0c7d4a |
201 | RECT bitmapPosition; /* game bitmap position within game window */ |
821ab2c6 |
202 | HDC hdc; |
4e7ef6e6 |
203 | COLORREF *colours; |
204 | HBRUSH *brushes; |
205 | HPEN *pens; |
4efb3868 |
206 | HRGN clip; |
fa3abef5 |
207 | HMENU gamemenu, typemenu; |
4e7ef6e6 |
208 | UINT timer; |
8c1fd974 |
209 | DWORD timer_last_tickcount; |
eb2ad6f1 |
210 | int npresets; |
211 | game_params **presets; |
4efb3868 |
212 | struct font *fonts; |
213 | int nfonts, fontsize; |
077f3cbe |
214 | config_item *cfg; |
215 | struct cfg_aux *cfgaux; |
97098757 |
216 | int cfg_which, dlg_done; |
077f3cbe |
217 | HFONT cfgfont; |
821ab2c6 |
218 | HBRUSH oldbr; |
219 | HPEN oldpen; |
750037d7 |
220 | int help_running; |
821ab2c6 |
221 | enum { DRAWING, PRINTING, NOTHING } drawstatus; |
222 | DOCINFO di; |
223 | int printcount, printw, printh, printsolns, printcurr, printcolour; |
224 | float printscale; |
225 | int printoffsetx, printoffsety; |
226 | float printpixelscale; |
227 | int fontstart; |
228 | int linewidth; |
229 | drawing *dr; |
6b12623f |
230 | int xmin, ymin; |
dc3de726 |
231 | float puzz_scale; |
4e7ef6e6 |
232 | }; |
233 | |
dc3de726 |
234 | void frontend_free(frontend *fe) |
235 | { |
236 | midend_free(fe->me); |
237 | |
238 | sfree(fe->colours); |
239 | sfree(fe->brushes); |
240 | sfree(fe->pens); |
241 | sfree(fe->presets); |
242 | |
243 | sfree(fe); |
244 | } |
245 | |
64e360d3 |
246 | static void update_type_menu_tick(frontend *fe); |
fa3abef5 |
247 | static void update_copy_menu_greying(frontend *fe); |
64e360d3 |
248 | |
4e7ef6e6 |
249 | void fatal(char *fmt, ...) |
250 | { |
251 | char buf[2048]; |
252 | va_list ap; |
253 | |
254 | va_start(ap, fmt); |
255 | vsprintf(buf, fmt, ap); |
256 | va_end(ap); |
257 | |
258 | MessageBox(NULL, buf, "Fatal error", MB_ICONEXCLAMATION | MB_OK); |
259 | |
260 | exit(1); |
261 | } |
262 | |
821ab2c6 |
263 | char *geterrstr(void) |
264 | { |
265 | LPVOID lpMsgBuf; |
266 | DWORD dw = GetLastError(); |
267 | char *ret; |
268 | |
269 | FormatMessage( |
270 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
271 | FORMAT_MESSAGE_FROM_SYSTEM, |
272 | NULL, |
273 | dw, |
274 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
275 | (LPTSTR) &lpMsgBuf, |
276 | 0, NULL ); |
277 | |
278 | ret = dupstr(lpMsgBuf); |
279 | |
280 | LocalFree(lpMsgBuf); |
281 | |
282 | return ret; |
283 | } |
284 | |
cbb5549e |
285 | void get_random_seed(void **randseed, int *randseedsize) |
286 | { |
cb0c7d4a |
287 | SYSTEMTIME *st = snew(SYSTEMTIME); |
288 | |
289 | GetLocalTime(st); |
290 | |
291 | *randseed = (void *)st; |
292 | *randseedsize = sizeof(SYSTEMTIME); |
cbb5549e |
293 | } |
294 | |
dafd6cf6 |
295 | static void win_status_bar(void *handle, char *text) |
fd1a1a2b |
296 | { |
cb0c7d4a |
297 | #ifdef _WIN32_WCE |
298 | TCHAR wText[255]; |
299 | #endif |
dafd6cf6 |
300 | frontend *fe = (frontend *)handle; |
821ab2c6 |
301 | |
cb0c7d4a |
302 | #ifdef _WIN32_WCE |
303 | MultiByteToWideChar (CP_ACP, 0, text, -1, wText, 255); |
304 | SendMessage(fe->statusbar, SB_SETTEXT, |
305 | (WPARAM) 255 | SBT_NOBORDERS, |
306 | (LPARAM) wText); |
307 | #else |
83c0438f |
308 | SetWindowText(fe->statusbar, text); |
cb0c7d4a |
309 | #endif |
fd1a1a2b |
310 | } |
311 | |
dafd6cf6 |
312 | static blitter *win_blitter_new(void *handle, int w, int h) |
3161048d |
313 | { |
314 | blitter *bl = snew(blitter); |
315 | |
316 | memset(bl, 0, sizeof(blitter)); |
317 | bl->w = w; |
318 | bl->h = h; |
319 | bl->bitmap = 0; |
320 | |
321 | return bl; |
322 | } |
323 | |
dafd6cf6 |
324 | static void win_blitter_free(void *handle, blitter *bl) |
3161048d |
325 | { |
326 | if (bl->bitmap) DeleteObject(bl->bitmap); |
327 | sfree(bl); |
328 | } |
329 | |
330 | static void blitter_mkbitmap(frontend *fe, blitter *bl) |
331 | { |
332 | HDC hdc = GetDC(fe->hwnd); |
333 | bl->bitmap = CreateCompatibleBitmap(hdc, bl->w, bl->h); |
334 | ReleaseDC(fe->hwnd, hdc); |
335 | } |
336 | |
337 | /* BitBlt(dstDC, dstX, dstY, dstW, dstH, srcDC, srcX, srcY, dType) */ |
338 | |
dafd6cf6 |
339 | static void win_blitter_save(void *handle, blitter *bl, int x, int y) |
3161048d |
340 | { |
dafd6cf6 |
341 | frontend *fe = (frontend *)handle; |
3161048d |
342 | HDC hdc_win, hdc_blit; |
343 | HBITMAP prev_blit; |
344 | |
821ab2c6 |
345 | assert(fe->drawstatus == DRAWING); |
346 | |
3161048d |
347 | if (!bl->bitmap) blitter_mkbitmap(fe, bl); |
348 | |
349 | bl->x = x; bl->y = y; |
350 | |
351 | hdc_win = GetDC(fe->hwnd); |
352 | hdc_blit = CreateCompatibleDC(hdc_win); |
353 | if (!hdc_blit) fatal("hdc_blit failed: 0x%x", GetLastError()); |
354 | |
355 | prev_blit = SelectObject(hdc_blit, bl->bitmap); |
356 | if (prev_blit == NULL || prev_blit == HGDI_ERROR) |
357 | fatal("SelectObject for hdc_main failed: 0x%x", GetLastError()); |
358 | |
359 | if (!BitBlt(hdc_blit, 0, 0, bl->w, bl->h, |
821ab2c6 |
360 | fe->hdc, x, y, SRCCOPY)) |
3161048d |
361 | fatal("BitBlt failed: 0x%x", GetLastError()); |
362 | |
363 | SelectObject(hdc_blit, prev_blit); |
364 | DeleteDC(hdc_blit); |
365 | ReleaseDC(fe->hwnd, hdc_win); |
366 | } |
367 | |
dafd6cf6 |
368 | static void win_blitter_load(void *handle, blitter *bl, int x, int y) |
3161048d |
369 | { |
dafd6cf6 |
370 | frontend *fe = (frontend *)handle; |
3161048d |
371 | HDC hdc_win, hdc_blit; |
372 | HBITMAP prev_blit; |
373 | |
821ab2c6 |
374 | assert(fe->drawstatus == DRAWING); |
375 | |
3161048d |
376 | assert(bl->bitmap); /* we should always have saved before loading */ |
377 | |
378 | if (x == BLITTER_FROMSAVED) x = bl->x; |
379 | if (y == BLITTER_FROMSAVED) y = bl->y; |
380 | |
381 | hdc_win = GetDC(fe->hwnd); |
382 | hdc_blit = CreateCompatibleDC(hdc_win); |
383 | |
384 | prev_blit = SelectObject(hdc_blit, bl->bitmap); |
385 | |
821ab2c6 |
386 | BitBlt(fe->hdc, x, y, bl->w, bl->h, |
3161048d |
387 | hdc_blit, 0, 0, SRCCOPY); |
388 | |
389 | SelectObject(hdc_blit, prev_blit); |
390 | DeleteDC(hdc_blit); |
391 | ReleaseDC(fe->hwnd, hdc_win); |
392 | } |
393 | |
4e7ef6e6 |
394 | void frontend_default_colour(frontend *fe, float *output) |
395 | { |
396 | DWORD c = GetSysColor(COLOR_MENU); /* ick */ |
397 | |
398 | output[0] = (float)(GetRValue(c) / 255.0); |
399 | output[1] = (float)(GetGValue(c) / 255.0); |
400 | output[2] = (float)(GetBValue(c) / 255.0); |
401 | } |
402 | |
821ab2c6 |
403 | static POINT win_transform_point(frontend *fe, int x, int y) |
404 | { |
405 | POINT ret; |
406 | |
407 | assert(fe->drawstatus != NOTHING); |
408 | |
409 | if (fe->drawstatus == PRINTING) { |
410 | ret.x = (int)(fe->printoffsetx + fe->printpixelscale * x); |
411 | ret.y = (int)(fe->printoffsety + fe->printpixelscale * y); |
412 | } else { |
413 | ret.x = x; |
414 | ret.y = y; |
415 | } |
416 | |
417 | return ret; |
418 | } |
419 | |
420 | static void win_text_colour(frontend *fe, int colour) |
421 | { |
422 | assert(fe->drawstatus != NOTHING); |
423 | |
424 | if (fe->drawstatus == PRINTING) { |
425 | int hatch; |
426 | float r, g, b; |
60aa1c74 |
427 | print_get_colour(fe->dr, colour, fe->printcolour, &hatch, &r, &g, &b); |
428 | |
429 | /* |
430 | * Displaying text in hatched colours is not permitted. |
431 | */ |
432 | assert(hatch < 0); |
433 | |
434 | SetTextColor(fe->hdc, RGB(r * 255, g * 255, b * 255)); |
821ab2c6 |
435 | } else { |
436 | SetTextColor(fe->hdc, fe->colours[colour]); |
437 | } |
438 | } |
439 | |
440 | static void win_set_brush(frontend *fe, int colour) |
441 | { |
442 | HBRUSH br; |
443 | assert(fe->drawstatus != NOTHING); |
444 | |
445 | if (fe->drawstatus == PRINTING) { |
446 | int hatch; |
447 | float r, g, b; |
60aa1c74 |
448 | print_get_colour(fe->dr, colour, fe->printcolour, &hatch, &r, &g, &b); |
821ab2c6 |
449 | |
60aa1c74 |
450 | if (hatch < 0) { |
821ab2c6 |
451 | br = CreateSolidBrush(RGB(r * 255, g * 255, b * 255)); |
cb0c7d4a |
452 | } else { |
453 | #ifdef _WIN32_WCE |
454 | /* |
455 | * This is only ever required during printing, and the |
456 | * PocketPC port doesn't support printing. |
457 | */ |
458 | fatal("CreateHatchBrush not supported"); |
459 | #else |
821ab2c6 |
460 | br = CreateHatchBrush(hatch == HATCH_BACKSLASH ? HS_FDIAGONAL : |
461 | hatch == HATCH_SLASH ? HS_BDIAGONAL : |
462 | hatch == HATCH_HORIZ ? HS_HORIZONTAL : |
463 | hatch == HATCH_VERT ? HS_VERTICAL : |
464 | hatch == HATCH_PLUS ? HS_CROSS : |
465 | /* hatch == HATCH_X ? */ HS_DIAGCROSS, |
466 | RGB(0,0,0)); |
cb0c7d4a |
467 | #endif |
468 | } |
821ab2c6 |
469 | } else { |
470 | br = fe->brushes[colour]; |
471 | } |
472 | fe->oldbr = SelectObject(fe->hdc, br); |
473 | } |
474 | |
475 | static void win_reset_brush(frontend *fe) |
476 | { |
477 | HBRUSH br; |
478 | |
479 | assert(fe->drawstatus != NOTHING); |
480 | |
481 | br = SelectObject(fe->hdc, fe->oldbr); |
482 | if (fe->drawstatus == PRINTING) |
483 | DeleteObject(br); |
484 | } |
485 | |
486 | static void win_set_pen(frontend *fe, int colour, int thin) |
487 | { |
488 | HPEN pen; |
489 | assert(fe->drawstatus != NOTHING); |
490 | |
491 | if (fe->drawstatus == PRINTING) { |
492 | int hatch; |
493 | float r, g, b; |
494 | int width = thin ? 0 : fe->linewidth; |
495 | |
60aa1c74 |
496 | print_get_colour(fe->dr, colour, fe->printcolour, &hatch, &r, &g, &b); |
497 | /* |
498 | * Stroking in hatched colours is not permitted. |
499 | */ |
500 | assert(hatch < 0); |
501 | pen = CreatePen(PS_SOLID, width, RGB(r * 255, g * 255, b * 255)); |
821ab2c6 |
502 | } else { |
503 | pen = fe->pens[colour]; |
504 | } |
505 | fe->oldpen = SelectObject(fe->hdc, pen); |
506 | } |
507 | |
508 | static void win_reset_pen(frontend *fe) |
509 | { |
510 | HPEN pen; |
511 | |
512 | assert(fe->drawstatus != NOTHING); |
513 | |
514 | pen = SelectObject(fe->hdc, fe->oldpen); |
515 | if (fe->drawstatus == PRINTING) |
516 | DeleteObject(pen); |
517 | } |
518 | |
dafd6cf6 |
519 | static void win_clip(void *handle, int x, int y, int w, int h) |
4efb3868 |
520 | { |
dafd6cf6 |
521 | frontend *fe = (frontend *)handle; |
821ab2c6 |
522 | POINT p, q; |
523 | |
524 | if (fe->drawstatus == NOTHING) |
525 | return; |
526 | |
527 | p = win_transform_point(fe, x, y); |
528 | q = win_transform_point(fe, x+w, y+h); |
529 | IntersectClipRect(fe->hdc, p.x, p.y, q.x, q.y); |
4efb3868 |
530 | } |
531 | |
dafd6cf6 |
532 | static void win_unclip(void *handle) |
4efb3868 |
533 | { |
dafd6cf6 |
534 | frontend *fe = (frontend *)handle; |
821ab2c6 |
535 | |
536 | if (fe->drawstatus == NOTHING) |
537 | return; |
538 | |
539 | SelectClipRgn(fe->hdc, NULL); |
4efb3868 |
540 | } |
541 | |
dafd6cf6 |
542 | static void win_draw_text(void *handle, int x, int y, int fonttype, |
543 | int fontsize, int align, int colour, char *text) |
4efb3868 |
544 | { |
dafd6cf6 |
545 | frontend *fe = (frontend *)handle; |
821ab2c6 |
546 | POINT xy; |
4efb3868 |
547 | int i; |
cb0c7d4a |
548 | LOGFONT lf; |
4efb3868 |
549 | |
821ab2c6 |
550 | if (fe->drawstatus == NOTHING) |
551 | return; |
552 | |
553 | if (fe->drawstatus == PRINTING) |
554 | fontsize = (int)(fontsize * fe->printpixelscale); |
555 | |
556 | xy = win_transform_point(fe, x, y); |
557 | |
4efb3868 |
558 | /* |
559 | * Find or create the font. |
560 | */ |
821ab2c6 |
561 | for (i = fe->fontstart; i < fe->nfonts; i++) |
4efb3868 |
562 | if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize) |
563 | break; |
564 | |
565 | if (i == fe->nfonts) { |
566 | if (fe->fontsize <= fe->nfonts) { |
567 | fe->fontsize = fe->nfonts + 10; |
568 | fe->fonts = sresize(fe->fonts, fe->fontsize, struct font); |
569 | } |
570 | |
571 | fe->nfonts++; |
572 | |
573 | fe->fonts[i].type = fonttype; |
574 | fe->fonts[i].size = fontsize; |
575 | |
cb0c7d4a |
576 | memset (&lf, 0, sizeof(LOGFONT)); |
577 | lf.lfHeight = -fontsize; |
578 | lf.lfWeight = (fe->drawstatus == PRINTING ? 0 : FW_BOLD); |
579 | lf.lfCharSet = DEFAULT_CHARSET; |
580 | lf.lfOutPrecision = OUT_DEFAULT_PRECIS; |
581 | lf.lfClipPrecision = CLIP_DEFAULT_PRECIS; |
582 | lf.lfQuality = DEFAULT_QUALITY; |
583 | lf.lfPitchAndFamily = (fonttype == FONT_FIXED ? |
584 | FIXED_PITCH | FF_DONTCARE : |
585 | VARIABLE_PITCH | FF_SWISS); |
586 | #ifdef _WIN32_WCE |
587 | wcscpy(lf.lfFaceName, TEXT("Tahoma")); |
588 | #endif |
589 | |
590 | fe->fonts[i].font = CreateFontIndirect(&lf); |
4efb3868 |
591 | } |
592 | |
593 | /* |
594 | * Position and draw the text. |
595 | */ |
596 | { |
597 | HFONT oldfont; |
598 | TEXTMETRIC tm; |
599 | SIZE size; |
cb0c7d4a |
600 | #ifdef _WIN32_WCE |
601 | TCHAR wText[256]; |
602 | MultiByteToWideChar (CP_ACP, 0, text, -1, wText, 256); |
603 | #endif |
4efb3868 |
604 | |
821ab2c6 |
605 | oldfont = SelectObject(fe->hdc, fe->fonts[i].font); |
606 | if (GetTextMetrics(fe->hdc, &tm)) { |
4efb3868 |
607 | if (align & ALIGN_VCENTRE) |
821ab2c6 |
608 | xy.y -= (tm.tmAscent+tm.tmDescent)/2; |
4efb3868 |
609 | else |
821ab2c6 |
610 | xy.y -= tm.tmAscent; |
4efb3868 |
611 | } |
cb0c7d4a |
612 | #ifndef _WIN32_WCE |
64e360d3 |
613 | if (GetTextExtentPoint32(fe->hdc, text, strlen(text), &size)) |
cb0c7d4a |
614 | #else |
64e360d3 |
615 | if (GetTextExtentPoint32(fe->hdc, wText, wcslen(wText), &size)) |
cb0c7d4a |
616 | #endif |
64e360d3 |
617 | { |
4efb3868 |
618 | if (align & ALIGN_HCENTRE) |
821ab2c6 |
619 | xy.x -= size.cx / 2; |
4efb3868 |
620 | else if (align & ALIGN_HRIGHT) |
821ab2c6 |
621 | xy.x -= size.cx; |
4efb3868 |
622 | } |
821ab2c6 |
623 | SetBkMode(fe->hdc, TRANSPARENT); |
624 | win_text_colour(fe, colour); |
cb0c7d4a |
625 | #ifndef _WIN32_WCE |
821ab2c6 |
626 | TextOut(fe->hdc, xy.x, xy.y, text, strlen(text)); |
cb0c7d4a |
627 | #else |
628 | ExtTextOut(fe->hdc, xy.x, xy.y, 0, NULL, wText, wcslen(wText), NULL); |
629 | #endif |
821ab2c6 |
630 | SelectObject(fe->hdc, oldfont); |
4efb3868 |
631 | } |
632 | } |
633 | |
dafd6cf6 |
634 | static void win_draw_rect(void *handle, int x, int y, int w, int h, int colour) |
4e7ef6e6 |
635 | { |
dafd6cf6 |
636 | frontend *fe = (frontend *)handle; |
821ab2c6 |
637 | POINT p, q; |
638 | |
639 | if (fe->drawstatus == NOTHING) |
640 | return; |
641 | |
642 | if (fe->drawstatus == DRAWING && w == 1 && h == 1) { |
fbdd7610 |
643 | /* |
644 | * Rectangle() appears to get uppity if asked to draw a 1x1 |
645 | * rectangle, presumably on the grounds that that's beneath |
646 | * its dignity and you ought to be using SetPixel instead. |
647 | * So I will. |
648 | */ |
821ab2c6 |
649 | SetPixel(fe->hdc, x, y, fe->colours[colour]); |
fbdd7610 |
650 | } else { |
821ab2c6 |
651 | win_set_brush(fe, colour); |
652 | win_set_pen(fe, colour, TRUE); |
653 | p = win_transform_point(fe, x, y); |
654 | q = win_transform_point(fe, x+w, y+h); |
655 | Rectangle(fe->hdc, p.x, p.y, q.x, q.y); |
656 | win_reset_brush(fe); |
657 | win_reset_pen(fe); |
fbdd7610 |
658 | } |
4e7ef6e6 |
659 | } |
660 | |
dafd6cf6 |
661 | static void win_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour) |
4e7ef6e6 |
662 | { |
dafd6cf6 |
663 | frontend *fe = (frontend *)handle; |
cb0c7d4a |
664 | POINT pp[2]; |
821ab2c6 |
665 | |
666 | if (fe->drawstatus == NOTHING) |
667 | return; |
668 | |
669 | win_set_pen(fe, colour, FALSE); |
cb0c7d4a |
670 | pp[0] = win_transform_point(fe, x1, y1); |
671 | pp[1] = win_transform_point(fe, x2, y2); |
672 | Polyline(fe->hdc, pp, 2); |
821ab2c6 |
673 | if (fe->drawstatus == DRAWING) |
cb0c7d4a |
674 | SetPixel(fe->hdc, pp[1].x, pp[1].y, fe->colours[colour]); |
821ab2c6 |
675 | win_reset_pen(fe); |
4e7ef6e6 |
676 | } |
677 | |
dafd6cf6 |
678 | static void win_draw_circle(void *handle, int cx, int cy, int radius, |
679 | int fillcolour, int outlinecolour) |
550efd7f |
680 | { |
dafd6cf6 |
681 | frontend *fe = (frontend *)handle; |
cb0c7d4a |
682 | POINT p, q; |
821ab2c6 |
683 | |
28b5987d |
684 | assert(outlinecolour >= 0); |
685 | |
821ab2c6 |
686 | if (fe->drawstatus == NOTHING) |
687 | return; |
688 | |
cb0c7d4a |
689 | if (fillcolour >= 0) |
821ab2c6 |
690 | win_set_brush(fe, fillcolour); |
cb0c7d4a |
691 | else |
692 | fe->oldbr = SelectObject(fe->hdc, GetStockObject(NULL_BRUSH)); |
693 | |
694 | win_set_pen(fe, outlinecolour, FALSE); |
695 | p = win_transform_point(fe, cx - radius, cy - radius); |
696 | q = win_transform_point(fe, cx + radius, cy + radius); |
697 | Ellipse(fe->hdc, p.x, p.y, q.x+1, q.y+1); |
698 | win_reset_brush(fe); |
699 | win_reset_pen(fe); |
550efd7f |
700 | } |
701 | |
dafd6cf6 |
702 | static void win_draw_polygon(void *handle, int *coords, int npoints, |
703 | int fillcolour, int outlinecolour) |
4e7ef6e6 |
704 | { |
dafd6cf6 |
705 | frontend *fe = (frontend *)handle; |
821ab2c6 |
706 | POINT *pts; |
4e7ef6e6 |
707 | int i; |
708 | |
821ab2c6 |
709 | if (fe->drawstatus == NOTHING) |
710 | return; |
711 | |
712 | pts = snewn(npoints+1, POINT); |
713 | |
4e7ef6e6 |
714 | for (i = 0; i <= npoints; i++) { |
715 | int j = (i < npoints ? i : 0); |
821ab2c6 |
716 | pts[i] = win_transform_point(fe, coords[j*2], coords[j*2+1]); |
4e7ef6e6 |
717 | } |
718 | |
28b5987d |
719 | assert(outlinecolour >= 0); |
720 | |
721 | if (fillcolour >= 0) { |
821ab2c6 |
722 | win_set_brush(fe, fillcolour); |
723 | win_set_pen(fe, outlinecolour, FALSE); |
724 | Polygon(fe->hdc, pts, npoints); |
725 | win_reset_brush(fe); |
726 | win_reset_pen(fe); |
4e7ef6e6 |
727 | } else { |
821ab2c6 |
728 | win_set_pen(fe, outlinecolour, FALSE); |
729 | Polyline(fe->hdc, pts, npoints+1); |
730 | win_reset_pen(fe); |
4e7ef6e6 |
731 | } |
732 | |
733 | sfree(pts); |
734 | } |
735 | |
dafd6cf6 |
736 | static void win_start_draw(void *handle) |
4e7ef6e6 |
737 | { |
dafd6cf6 |
738 | frontend *fe = (frontend *)handle; |
4e7ef6e6 |
739 | HDC hdc_win; |
821ab2c6 |
740 | |
741 | assert(fe->drawstatus == NOTHING); |
742 | |
4e7ef6e6 |
743 | hdc_win = GetDC(fe->hwnd); |
821ab2c6 |
744 | fe->hdc = CreateCompatibleDC(hdc_win); |
745 | fe->prevbm = SelectObject(fe->hdc, fe->bitmap); |
4e7ef6e6 |
746 | ReleaseDC(fe->hwnd, hdc_win); |
4efb3868 |
747 | fe->clip = NULL; |
cb0c7d4a |
748 | #ifndef _WIN32_WCE |
821ab2c6 |
749 | SetMapMode(fe->hdc, MM_TEXT); |
cb0c7d4a |
750 | #endif |
821ab2c6 |
751 | fe->drawstatus = DRAWING; |
4e7ef6e6 |
752 | } |
753 | |
dafd6cf6 |
754 | static void win_draw_update(void *handle, int x, int y, int w, int h) |
4e7ef6e6 |
755 | { |
dafd6cf6 |
756 | frontend *fe = (frontend *)handle; |
4e7ef6e6 |
757 | RECT r; |
758 | |
821ab2c6 |
759 | if (fe->drawstatus != DRAWING) |
760 | return; |
761 | |
4e7ef6e6 |
762 | r.left = x; |
763 | r.top = y; |
764 | r.right = x + w; |
765 | r.bottom = y + h; |
766 | |
cb0c7d4a |
767 | OffsetRect(&r, fe->bitmapPosition.left, fe->bitmapPosition.top); |
4e7ef6e6 |
768 | InvalidateRect(fe->hwnd, &r, FALSE); |
769 | } |
770 | |
dafd6cf6 |
771 | static void win_end_draw(void *handle) |
4e7ef6e6 |
772 | { |
dafd6cf6 |
773 | frontend *fe = (frontend *)handle; |
821ab2c6 |
774 | assert(fe->drawstatus == DRAWING); |
775 | SelectObject(fe->hdc, fe->prevbm); |
776 | DeleteDC(fe->hdc); |
4efb3868 |
777 | if (fe->clip) { |
778 | DeleteObject(fe->clip); |
779 | fe->clip = NULL; |
780 | } |
821ab2c6 |
781 | fe->drawstatus = NOTHING; |
782 | } |
783 | |
784 | static void win_line_width(void *handle, float width) |
785 | { |
786 | frontend *fe = (frontend *)handle; |
787 | |
788 | assert(fe->drawstatus != DRAWING); |
789 | if (fe->drawstatus == NOTHING) |
790 | return; |
791 | |
792 | fe->linewidth = (int)(width * fe->printpixelscale); |
793 | } |
794 | |
795 | static void win_begin_doc(void *handle, int pages) |
796 | { |
797 | frontend *fe = (frontend *)handle; |
798 | |
799 | assert(fe->drawstatus != DRAWING); |
800 | if (fe->drawstatus == NOTHING) |
801 | return; |
802 | |
803 | if (StartDoc(fe->hdc, &fe->di) <= 0) { |
804 | char *e = geterrstr(); |
805 | MessageBox(fe->hwnd, e, "Error starting to print", |
806 | MB_ICONERROR | MB_OK); |
807 | sfree(e); |
808 | fe->drawstatus = NOTHING; |
809 | } |
810 | |
811 | /* |
812 | * Push a marker on the font stack so that we won't use the |
813 | * same fonts for printing and drawing. (This is because |
814 | * drawing seems to look generally better in bold, but printing |
815 | * is better not in bold.) |
816 | */ |
817 | fe->fontstart = fe->nfonts; |
818 | } |
819 | |
820 | static void win_begin_page(void *handle, int number) |
821 | { |
822 | frontend *fe = (frontend *)handle; |
823 | |
824 | assert(fe->drawstatus != DRAWING); |
825 | if (fe->drawstatus == NOTHING) |
826 | return; |
827 | |
828 | if (StartPage(fe->hdc) <= 0) { |
829 | char *e = geterrstr(); |
830 | MessageBox(fe->hwnd, e, "Error starting a page", |
831 | MB_ICONERROR | MB_OK); |
832 | sfree(e); |
833 | fe->drawstatus = NOTHING; |
834 | } |
835 | } |
836 | |
837 | static void win_begin_puzzle(void *handle, float xm, float xc, |
838 | float ym, float yc, int pw, int ph, float wmm) |
839 | { |
840 | frontend *fe = (frontend *)handle; |
841 | int ppw, pph, pox, poy; |
842 | float mmpw, mmph, mmox, mmoy; |
843 | float scale; |
844 | |
845 | assert(fe->drawstatus != DRAWING); |
846 | if (fe->drawstatus == NOTHING) |
847 | return; |
848 | |
849 | ppw = GetDeviceCaps(fe->hdc, HORZRES); |
850 | pph = GetDeviceCaps(fe->hdc, VERTRES); |
851 | mmpw = (float)GetDeviceCaps(fe->hdc, HORZSIZE); |
852 | mmph = (float)GetDeviceCaps(fe->hdc, VERTSIZE); |
853 | |
854 | /* |
855 | * Compute the puzzle's position on the logical page. |
856 | */ |
857 | mmox = xm * mmpw + xc; |
858 | mmoy = ym * mmph + yc; |
859 | |
860 | /* |
861 | * Work out what that comes to in pixels. |
862 | */ |
863 | pox = (int)(mmox * (float)ppw / mmpw); |
89555f7a |
864 | poy = (int)(mmoy * (float)pph / mmph); |
821ab2c6 |
865 | |
866 | /* |
867 | * And determine the scale. |
868 | * |
869 | * I need a scale such that the maximum puzzle-coordinate |
870 | * extent of the rectangle (pw * scale) is equal to the pixel |
871 | * equivalent of the puzzle's millimetre width (wmm * ppw / |
872 | * mmpw). |
873 | */ |
874 | scale = (wmm * ppw) / (mmpw * pw); |
875 | |
876 | /* |
877 | * Now store pox, poy and scale for use in the main drawing |
878 | * functions. |
879 | */ |
880 | fe->printoffsetx = pox; |
881 | fe->printoffsety = poy; |
882 | fe->printpixelscale = scale; |
883 | |
884 | fe->linewidth = 1; |
885 | } |
886 | |
887 | static void win_end_puzzle(void *handle) |
888 | { |
889 | /* Nothing needs to be done here. */ |
890 | } |
891 | |
892 | static void win_end_page(void *handle, int number) |
893 | { |
894 | frontend *fe = (frontend *)handle; |
895 | |
896 | assert(fe->drawstatus != DRAWING); |
92efb79d |
897 | |
821ab2c6 |
898 | if (fe->drawstatus == NOTHING) |
899 | return; |
900 | |
901 | if (EndPage(fe->hdc) <= 0) { |
902 | char *e = geterrstr(); |
903 | MessageBox(fe->hwnd, e, "Error finishing a page", |
904 | MB_ICONERROR | MB_OK); |
905 | sfree(e); |
906 | fe->drawstatus = NOTHING; |
907 | } |
908 | } |
909 | |
910 | static void win_end_doc(void *handle) |
911 | { |
912 | frontend *fe = (frontend *)handle; |
913 | |
914 | assert(fe->drawstatus != DRAWING); |
915 | |
916 | /* |
917 | * Free all the fonts created since we began printing. |
918 | */ |
919 | while (fe->nfonts > fe->fontstart) { |
920 | fe->nfonts--; |
921 | DeleteObject(fe->fonts[fe->nfonts].font); |
922 | } |
923 | fe->fontstart = 0; |
924 | |
92efb79d |
925 | /* |
926 | * The MSDN web site sample code doesn't bother to call EndDoc |
927 | * if an error occurs half way through printing. I expect doing |
928 | * so would cause the erroneous document to actually be |
929 | * printed, or something equally undesirable. |
930 | */ |
821ab2c6 |
931 | if (fe->drawstatus == NOTHING) |
932 | return; |
933 | |
934 | if (EndDoc(fe->hdc) <= 0) { |
935 | char *e = geterrstr(); |
936 | MessageBox(fe->hwnd, e, "Error finishing printing", |
937 | MB_ICONERROR | MB_OK); |
938 | sfree(e); |
939 | fe->drawstatus = NOTHING; |
940 | } |
4e7ef6e6 |
941 | } |
942 | |
dafd6cf6 |
943 | const struct drawing_api win_drawing = { |
944 | win_draw_text, |
945 | win_draw_rect, |
946 | win_draw_line, |
947 | win_draw_polygon, |
948 | win_draw_circle, |
949 | win_draw_update, |
950 | win_clip, |
951 | win_unclip, |
952 | win_start_draw, |
953 | win_end_draw, |
954 | win_status_bar, |
955 | win_blitter_new, |
956 | win_blitter_free, |
957 | win_blitter_save, |
958 | win_blitter_load, |
821ab2c6 |
959 | win_begin_doc, |
960 | win_begin_page, |
961 | win_begin_puzzle, |
962 | win_end_puzzle, |
963 | win_end_page, |
964 | win_end_doc, |
965 | win_line_width, |
dafd6cf6 |
966 | }; |
967 | |
821ab2c6 |
968 | void print(frontend *fe) |
969 | { |
cb0c7d4a |
970 | #ifndef _WIN32_WCE |
821ab2c6 |
971 | PRINTDLG pd; |
972 | char doctitle[256]; |
973 | document *doc; |
974 | midend *nme = NULL; /* non-interactive midend for bulk puzzle generation */ |
975 | int i; |
976 | char *err = NULL; |
977 | |
978 | /* |
979 | * Create our document structure and fill it up with puzzles. |
980 | */ |
981 | doc = document_new(fe->printw, fe->printh, fe->printscale / 100.0F); |
982 | for (i = 0; i < fe->printcount; i++) { |
983 | if (i == 0 && fe->printcurr) { |
984 | err = midend_print_puzzle(fe->me, doc, fe->printsolns); |
985 | } else { |
986 | if (!nme) { |
987 | game_params *params; |
988 | |
dc3de726 |
989 | nme = midend_new(NULL, fe->game, NULL, NULL); |
821ab2c6 |
990 | |
991 | /* |
992 | * Set the non-interactive mid-end to have the same |
993 | * parameters as the standard one. |
994 | */ |
995 | params = midend_get_params(fe->me); |
996 | midend_set_params(nme, params); |
dc3de726 |
997 | fe->game->free_params(params); |
821ab2c6 |
998 | } |
999 | |
1000 | midend_new_game(nme); |
1001 | err = midend_print_puzzle(nme, doc, fe->printsolns); |
1002 | } |
1003 | if (err) |
1004 | break; |
1005 | } |
1006 | if (nme) |
1007 | midend_free(nme); |
1008 | |
1009 | if (err) { |
1010 | MessageBox(fe->hwnd, err, "Error preparing puzzles for printing", |
1011 | MB_ICONERROR | MB_OK); |
1012 | document_free(doc); |
1013 | return; |
1014 | } |
1015 | |
1016 | memset(&pd, 0, sizeof(pd)); |
1017 | pd.lStructSize = sizeof(pd); |
1018 | pd.hwndOwner = fe->hwnd; |
1019 | pd.hDevMode = NULL; |
1020 | pd.hDevNames = NULL; |
1021 | pd.Flags = PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC | |
1022 | PD_NOPAGENUMS | PD_NOSELECTION; |
1023 | pd.nCopies = 1; |
1024 | pd.nFromPage = pd.nToPage = 0xFFFF; |
1025 | pd.nMinPage = pd.nMaxPage = 1; |
1026 | |
92efb79d |
1027 | if (!PrintDlg(&pd)) { |
1028 | document_free(doc); |
821ab2c6 |
1029 | return; |
92efb79d |
1030 | } |
821ab2c6 |
1031 | |
1032 | /* |
1033 | * Now pd.hDC is a device context for the printer. |
1034 | */ |
1035 | |
1036 | /* |
1037 | * FIXME: IWBNI we put up an Abort box here. |
1038 | */ |
1039 | |
1040 | memset(&fe->di, 0, sizeof(fe->di)); |
1041 | fe->di.cbSize = sizeof(fe->di); |
1042 | sprintf(doctitle, "Printed puzzles from %s (from Simon Tatham's" |
dc3de726 |
1043 | " Portable Puzzle Collection)", fe->game->name); |
821ab2c6 |
1044 | fe->di.lpszDocName = doctitle; |
1045 | fe->di.lpszOutput = NULL; |
1046 | fe->di.lpszDatatype = NULL; |
1047 | fe->di.fwType = 0; |
1048 | |
1049 | fe->drawstatus = PRINTING; |
1050 | fe->hdc = pd.hDC; |
1051 | |
83c0438f |
1052 | fe->dr = drawing_new(&win_drawing, NULL, fe); |
821ab2c6 |
1053 | document_print(doc, fe->dr); |
1054 | drawing_free(fe->dr); |
1055 | fe->dr = NULL; |
1056 | |
1057 | fe->drawstatus = NOTHING; |
1058 | |
1059 | DeleteDC(pd.hDC); |
92efb79d |
1060 | document_free(doc); |
cb0c7d4a |
1061 | #endif |
821ab2c6 |
1062 | } |
1063 | |
4e7ef6e6 |
1064 | void deactivate_timer(frontend *fe) |
1065 | { |
821ab2c6 |
1066 | if (!fe) |
1067 | return; /* for non-interactive midend */ |
986cc2de |
1068 | if (fe->hwnd) KillTimer(fe->hwnd, fe->timer); |
4e7ef6e6 |
1069 | fe->timer = 0; |
1070 | } |
1071 | |
1072 | void activate_timer(frontend *fe) |
1073 | { |
821ab2c6 |
1074 | if (!fe) |
1075 | return; /* for non-interactive midend */ |
8c1fd974 |
1076 | if (!fe->timer) { |
cb0c7d4a |
1077 | fe->timer = SetTimer(fe->hwnd, 1, 20, NULL); |
8c1fd974 |
1078 | fe->timer_last_tickcount = GetTickCount(); |
1079 | } |
4e7ef6e6 |
1080 | } |
1081 | |
1cdfcdbd |
1082 | void write_clip(HWND hwnd, char *data) |
1083 | { |
1084 | HGLOBAL clipdata; |
6d5cbf79 |
1085 | int len, i, j; |
1086 | char *data2; |
1cdfcdbd |
1087 | void *lock; |
1088 | |
6d5cbf79 |
1089 | /* |
1090 | * Windows expects CRLF in the clipboard, so we must convert |
1091 | * any \n that has come out of the puzzle backend. |
1092 | */ |
1093 | len = 0; |
1094 | for (i = 0; data[i]; i++) { |
1095 | if (data[i] == '\n') |
1096 | len++; |
1097 | len++; |
1098 | } |
1099 | data2 = snewn(len+1, char); |
1100 | j = 0; |
1101 | for (i = 0; data[i]; i++) { |
1102 | if (data[i] == '\n') |
1103 | data2[j++] = '\r'; |
1104 | data2[j++] = data[i]; |
1105 | } |
1106 | assert(j == len); |
1107 | data2[j] = '\0'; |
1108 | |
1cdfcdbd |
1109 | clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1); |
dc3de726 |
1110 | if (!clipdata) { |
1111 | sfree(data2); |
1cdfcdbd |
1112 | return; |
dc3de726 |
1113 | } |
1cdfcdbd |
1114 | lock = GlobalLock(clipdata); |
dc3de726 |
1115 | if (!lock) { |
1116 | GlobalFree(clipdata); |
1117 | sfree(data2); |
1cdfcdbd |
1118 | return; |
dc3de726 |
1119 | } |
6d5cbf79 |
1120 | memcpy(lock, data2, len); |
1cdfcdbd |
1121 | ((unsigned char *) lock)[len] = 0; |
1122 | GlobalUnlock(clipdata); |
1123 | |
1124 | if (OpenClipboard(hwnd)) { |
1125 | EmptyClipboard(); |
1126 | SetClipboardData(CF_TEXT, clipdata); |
1127 | CloseClipboard(); |
1128 | } else |
1129 | GlobalFree(clipdata); |
6d5cbf79 |
1130 | |
1131 | sfree(data2); |
1cdfcdbd |
1132 | } |
1133 | |
e91825f8 |
1134 | /* |
750037d7 |
1135 | * Set up Help and see if we can find a help file. |
e91825f8 |
1136 | */ |
750037d7 |
1137 | static void init_help(void) |
e91825f8 |
1138 | { |
cb0c7d4a |
1139 | #ifndef _WIN32_WCE |
e91825f8 |
1140 | char b[2048], *p, *q, *r; |
1141 | FILE *fp; |
750037d7 |
1142 | |
1143 | /* |
1144 | * Find the executable file path, so we can look alongside |
1145 | * it for help files. Trim the filename off the end. |
1146 | */ |
1147 | GetModuleFileName(NULL, b, sizeof(b) - 1); |
1148 | r = b; |
1149 | p = strrchr(b, '\\'); |
1150 | if (p && p >= r) r = p+1; |
1151 | q = strrchr(b, ':'); |
1152 | if (q && q >= r) r = q+1; |
1153 | |
291c690f |
1154 | #ifndef NO_HTMLHELP |
750037d7 |
1155 | /* |
1156 | * Try HTML Help first. |
1157 | */ |
1158 | strcpy(r, CHM_FILE_NAME); |
1159 | if ( (fp = fopen(b, "r")) != NULL) { |
1160 | fclose(fp); |
1161 | |
1162 | /* |
1163 | * We have a .CHM. See if we can use it. |
1164 | */ |
1165 | hh_dll = LoadLibrary("hhctrl.ocx"); |
1166 | if (hh_dll) { |
1167 | htmlhelp = (htmlhelp_t)GetProcAddress(hh_dll, "HtmlHelpA"); |
1168 | if (!htmlhelp) |
1169 | FreeLibrary(hh_dll); |
1170 | } |
1171 | if (htmlhelp) { |
750037d7 |
1172 | help_path = dupstr(b); |
1173 | help_type = CHM; |
750037d7 |
1174 | return; |
1175 | } |
1176 | } |
291c690f |
1177 | #endif /* NO_HTMLHELP */ |
750037d7 |
1178 | |
1179 | /* |
1180 | * Now try old-style .HLP. |
1181 | */ |
1182 | strcpy(r, HELP_FILE_NAME); |
1183 | if ( (fp = fopen(b, "r")) != NULL) { |
1184 | fclose(fp); |
1185 | |
1186 | help_path = dupstr(b); |
1187 | help_type = HLP; |
1188 | |
750037d7 |
1189 | /* |
1190 | * See if there's a .CNT file alongside it. |
1191 | */ |
1192 | strcpy(r, HELP_CNT_NAME); |
1193 | if ( (fp = fopen(b, "r")) != NULL) { |
1194 | fclose(fp); |
1195 | help_has_contents = TRUE; |
1196 | } else |
1197 | help_has_contents = FALSE; |
1198 | |
1199 | return; |
1200 | } |
1201 | |
1202 | help_type = NONE; /* didn't find any */ |
cb0c7d4a |
1203 | #endif |
750037d7 |
1204 | } |
1205 | |
cb0c7d4a |
1206 | #ifndef _WIN32_WCE |
1207 | |
750037d7 |
1208 | /* |
1209 | * Start Help. |
1210 | */ |
ed656ed4 |
1211 | static void start_help(frontend *fe, const char *topic) |
750037d7 |
1212 | { |
1213 | char *str = NULL; |
1214 | int cmd; |
1215 | |
1216 | switch (help_type) { |
1217 | case HLP: |
1218 | assert(help_path); |
1219 | if (topic) { |
1220 | str = snewn(10+strlen(topic), char); |
1221 | sprintf(str, "JI(`',`%s')", topic); |
1222 | cmd = HELP_COMMAND; |
1223 | } else if (help_has_contents) { |
1224 | cmd = HELP_FINDER; |
1225 | } else { |
1226 | cmd = HELP_CONTENTS; |
1227 | } |
1228 | WinHelp(fe->hwnd, help_path, cmd, (DWORD)str); |
1229 | fe->help_running = TRUE; |
1230 | break; |
1231 | case CHM: |
291c690f |
1232 | #ifndef NO_HTMLHELP |
750037d7 |
1233 | assert(help_path); |
1234 | assert(htmlhelp); |
1235 | if (topic) { |
1236 | str = snewn(20 + strlen(topic) + strlen(help_path), char); |
1237 | sprintf(str, "%s::/%s.html>main", help_path, topic); |
1238 | } else { |
1239 | str = dupstr(help_path); |
1240 | } |
1241 | htmlhelp(fe->hwnd, str, HH_DISPLAY_TOPIC, 0); |
1242 | fe->help_running = TRUE; |
1243 | break; |
291c690f |
1244 | #endif /* NO_HTMLHELP */ |
750037d7 |
1245 | case NONE: |
1246 | assert(!"This shouldn't happen"); |
1247 | break; |
1248 | } |
1249 | |
1250 | sfree(str); |
1251 | } |
1252 | |
1253 | /* |
1254 | * Stop Help on window cleanup. |
1255 | */ |
1256 | static void stop_help(frontend *fe) |
1257 | { |
1258 | if (fe->help_running) { |
1259 | switch (help_type) { |
1260 | case HLP: |
1261 | WinHelp(fe->hwnd, help_path, HELP_QUIT, 0); |
1262 | break; |
1263 | case CHM: |
291c690f |
1264 | #ifndef NO_HTMLHELP |
750037d7 |
1265 | assert(htmlhelp); |
1266 | htmlhelp(NULL, NULL, HH_CLOSE_ALL, 0); |
1267 | break; |
291c690f |
1268 | #endif /* NO_HTMLHELP */ |
750037d7 |
1269 | case NONE: |
1270 | assert(!"This shouldn't happen"); |
1271 | break; |
1272 | } |
1273 | fe->help_running = FALSE; |
1274 | } |
1275 | } |
1276 | |
cb0c7d4a |
1277 | #endif |
1278 | |
750037d7 |
1279 | /* |
1280 | * Terminate Help on process exit. |
1281 | */ |
1282 | static void cleanup_help(void) |
1283 | { |
39384997 |
1284 | /* Nothing to do currently. |
1285 | * (If we were running HTML Help single-threaded, this is where we'd |
1286 | * call HH_UNINITIALIZE.) */ |
e91825f8 |
1287 | } |
1288 | |
6b12623f |
1289 | static int get_statusbar_height(frontend *fe) |
1e3e152d |
1290 | { |
6b12623f |
1291 | int sy; |
1e3e152d |
1292 | if (fe->statusbar) { |
1293 | RECT sr; |
1294 | GetWindowRect(fe->statusbar, &sr); |
1295 | sy = sr.bottom - sr.top; |
1296 | } else { |
1297 | sy = 0; |
1298 | } |
6b12623f |
1299 | return sy; |
1300 | } |
1301 | |
1302 | static void adjust_statusbar(frontend *fe, RECT *r) |
1303 | { |
1304 | int sy; |
1305 | |
1306 | if (!fe->statusbar) return; |
1307 | |
1308 | sy = get_statusbar_height(fe); |
1309 | #ifndef _WIN32_WCE |
1310 | SetWindowPos(fe->statusbar, NULL, 0, r->bottom-r->top-sy, r->right-r->left, |
1311 | sy, SWP_NOZORDER); |
1312 | #endif |
1313 | } |
1314 | |
1315 | static void get_menu_size(HWND wh, RECT *r) |
1316 | { |
1317 | HMENU bar = GetMenu(wh); |
1318 | RECT rect; |
1319 | int i; |
1320 | |
1321 | SetRect(r, 0, 0, 0, 0); |
1322 | for (i = 0; i < GetMenuItemCount(bar); i++) { |
1323 | GetMenuItemRect(wh, bar, i, &rect); |
1324 | UnionRect(r, r, &rect); |
1325 | } |
1326 | } |
1327 | |
1328 | /* |
1329 | * Given a proposed new puzzle size (cx,cy), work out the actual |
1330 | * puzzle size that would be (px,py) and the window size including |
1331 | * furniture (wx,wy). |
1332 | */ |
1333 | |
1334 | static int check_window_resize(frontend *fe, int cx, int cy, |
1335 | int *px, int *py, |
dc3de726 |
1336 | int *wx, int *wy) |
6b12623f |
1337 | { |
1338 | RECT r; |
1339 | int x, y, sy = get_statusbar_height(fe), changed = 0; |
1340 | |
1341 | /* disallow making window thinner than menu bar */ |
1342 | x = max(cx, fe->xmin); |
1343 | y = max(cy - sy, fe->ymin); |
1e3e152d |
1344 | |
1345 | /* |
1346 | * See if we actually got the window size we wanted, and adjust |
1347 | * the puzzle size if not. |
1348 | */ |
dc3de726 |
1349 | midend_size(fe->me, &x, &y, TRUE); |
6b12623f |
1350 | if (x != cx || y != cy) { |
1351 | /* |
1352 | * Resize the window, now we know what size we _really_ |
1353 | * want it to be. |
1354 | */ |
1355 | r.left = r.top = 0; |
1356 | r.right = x; |
1357 | r.bottom = y + sy; |
1358 | AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0); |
1359 | *wx = r.right - r.left; |
1360 | *wy = r.bottom - r.top; |
1361 | changed = 1; |
1e3e152d |
1362 | } |
1363 | |
6b12623f |
1364 | *px = x; |
1365 | *py = y; |
1366 | |
dc3de726 |
1367 | fe->puzz_scale = |
1368 | (float)midend_tilesize(fe->me) / (float)fe->game->preferred_tilesize; |
1369 | |
6b12623f |
1370 | return changed; |
1371 | } |
1372 | |
1373 | /* |
1374 | * Given the current window size, make sure it's sane for the |
1375 | * current puzzle and resize if necessary. |
1376 | */ |
1377 | |
1378 | static void check_window_size(frontend *fe, int *px, int *py) |
1379 | { |
1380 | RECT r; |
1381 | int wx, wy, cx, cy; |
1382 | |
1383 | GetClientRect(fe->hwnd, &r); |
1384 | cx = r.right - r.left; |
1385 | cy = r.bottom - r.top; |
1386 | |
dc3de726 |
1387 | if (check_window_resize(fe, cx, cy, px, py, &wx, &wy)) { |
6b12623f |
1388 | #ifdef _WIN32_WCE |
1389 | SetWindowPos(fe->hwnd, NULL, 0, 0, wx, wy, |
1390 | SWP_NOMOVE | SWP_NOZORDER); |
cb0c7d4a |
1391 | #endif |
6b12623f |
1392 | ; |
1e3e152d |
1393 | } |
1394 | |
6b12623f |
1395 | GetClientRect(fe->hwnd, &r); |
1396 | adjust_statusbar(fe, &r); |
1e3e152d |
1397 | } |
1398 | |
35ee2001 |
1399 | static void get_max_puzzle_size(frontend *fe, int *x, int *y) |
1400 | { |
1401 | RECT r, sr; |
1402 | |
1403 | if (SystemParametersInfo(SPI_GETWORKAREA, 0, &sr, FALSE)) { |
1404 | *x = sr.right - sr.left; |
1405 | *y = sr.bottom - sr.top; |
1406 | r.left = 100; |
1407 | r.right = 200; |
1408 | r.top = 100; |
1409 | r.bottom = 200; |
1410 | AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0); |
1411 | *x -= r.right - r.left - 100; |
1412 | *y -= r.bottom - r.top - 100; |
1413 | } else { |
1414 | *x = *y = INT_MAX; |
1415 | } |
1416 | |
1417 | if (fe->statusbar != NULL) { |
1418 | GetWindowRect(fe->statusbar, &sr); |
1419 | *y -= sr.bottom - sr.top; |
1420 | } |
1421 | } |
1422 | |
cb0c7d4a |
1423 | #ifdef _WIN32_WCE |
1424 | /* Toolbar buttons on the numeric pad */ |
1425 | static TBBUTTON tbNumpadButtons[] = |
1426 | { |
1427 | {0, IDM_KEYEMUL + '1', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1}, |
1428 | {1, IDM_KEYEMUL + '2', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1}, |
1429 | {2, IDM_KEYEMUL + '3', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1}, |
1430 | {3, IDM_KEYEMUL + '4', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1}, |
1431 | {4, IDM_KEYEMUL + '5', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1}, |
1432 | {5, IDM_KEYEMUL + '6', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1}, |
1433 | {6, IDM_KEYEMUL + '7', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1}, |
1434 | {7, IDM_KEYEMUL + '8', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1}, |
1435 | {8, IDM_KEYEMUL + '9', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1}, |
1436 | {9, IDM_KEYEMUL + ' ', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1} |
1437 | }; |
1438 | #endif |
1439 | |
dc3de726 |
1440 | /* |
1441 | * Allocate a new frontend structure and create its main window. |
1442 | */ |
1443 | static frontend *frontend_new(HINSTANCE inst) |
4e7ef6e6 |
1444 | { |
1445 | frontend *fe; |
dc3de726 |
1446 | const char *nogame = "Puzzles (no game selected)"; |
4e7ef6e6 |
1447 | |
1448 | fe = snew(frontend); |
48d70ca9 |
1449 | |
077f3cbe |
1450 | fe->inst = inst; |
4e7ef6e6 |
1451 | |
dc3de726 |
1452 | fe->game = NULL; |
1453 | fe->me = NULL; |
1454 | |
4e7ef6e6 |
1455 | fe->timer = 0; |
986cc2de |
1456 | fe->hwnd = NULL; |
1457 | |
750037d7 |
1458 | fe->help_running = FALSE; |
1459 | |
821ab2c6 |
1460 | fe->drawstatus = NOTHING; |
1461 | fe->dr = NULL; |
1462 | fe->fontstart = 0; |
1463 | |
6beae131 |
1464 | fe->fonts = NULL; |
1465 | fe->nfonts = fe->fontsize = 0; |
1466 | |
dc3de726 |
1467 | fe->colours = NULL; |
1468 | fe->brushes = NULL; |
1469 | fe->pens = NULL; |
1470 | |
1471 | fe->puzz_scale = 1.0; |
1472 | |
1473 | #ifdef _WIN32_WCE |
1474 | MultiByteToWideChar (CP_ACP, 0, nogame, -1, wGameName, 256); |
1475 | fe->hwnd = CreateWindowEx(0, wClassName, wGameName, |
1476 | WS_VISIBLE, |
1477 | CW_USEDEFAULT, CW_USEDEFAULT, |
1478 | CW_USEDEFAULT, CW_USEDEFAULT, |
1479 | NULL, NULL, inst, NULL); |
1480 | |
1481 | { |
1482 | SHMENUBARINFO mbi; |
1483 | RECT rc, rcBar, rcTB, rcClient; |
1484 | |
1485 | memset (&mbi, 0, sizeof(SHMENUBARINFO)); |
1486 | mbi.cbSize = sizeof(SHMENUBARINFO); |
1487 | mbi.hwndParent = fe->hwnd; |
1488 | mbi.nToolBarId = IDR_MENUBAR1; |
1489 | mbi.hInstRes = inst; |
1490 | |
1491 | SHCreateMenuBar(&mbi); |
1492 | |
1493 | GetWindowRect(fe->hwnd, &rc); |
1494 | GetWindowRect(mbi.hwndMB, &rcBar); |
1495 | rc.bottom -= rcBar.bottom - rcBar.top; |
1496 | MoveWindow(fe->hwnd, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, FALSE); |
1497 | |
1498 | fe->numpad = NULL; |
1499 | } |
1500 | #else |
1501 | fe->hwnd = CreateWindowEx(0, CLASSNAME, nogame, |
1502 | WS_OVERLAPPEDWINDOW &~ |
1503 | (WS_MAXIMIZEBOX), |
1504 | CW_USEDEFAULT, CW_USEDEFAULT, |
1505 | CW_USEDEFAULT, CW_USEDEFAULT, |
1506 | NULL, NULL, inst, NULL); |
1507 | if (!fe->hwnd) { |
1508 | DWORD lerr = GetLastError(); |
1509 | printf("no window: 0x%x\n", lerr); |
1510 | } |
1511 | #endif |
1512 | |
1513 | fe->gamemenu = NULL; |
1514 | fe->presets = NULL; |
1515 | |
1516 | fe->statusbar = NULL; |
1517 | fe->bitmap = NULL; |
1518 | |
1519 | SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe); |
1520 | |
1521 | return fe; |
1522 | } |
1523 | |
1524 | /* |
1525 | * Populate a frontend structure with a (new) game and midend structure, and |
1526 | * create any window furniture that it needs. |
1527 | * |
1528 | * Previously-allocated memory and window furniture will be freed by this function. |
1529 | */ |
1530 | static int new_game(frontend *fe, const game *game, char *game_id, char **error) |
1531 | { |
1532 | int x, y; |
1533 | RECT r; |
1534 | |
1535 | fe->game = game; |
1536 | |
1537 | if (fe->me) midend_free(fe->me); |
1538 | fe->me = midend_new(fe, fe->game, &win_drawing, fe); |
1539 | |
1540 | if (game_id) { |
1541 | *error = midend_game_id(fe->me, game_id); |
1542 | if (*error) { |
1543 | midend_free(fe->me); |
1544 | sfree(fe); |
1545 | return -1; |
1546 | } |
1547 | } |
1548 | |
1549 | midend_new_game(fe->me); |
1550 | |
4e7ef6e6 |
1551 | { |
1552 | int i, ncolours; |
1553 | float *colours; |
1554 | |
1555 | colours = midend_colours(fe->me, &ncolours); |
1556 | |
dc3de726 |
1557 | if (fe->colours) sfree(fe->colours); |
1558 | if (fe->brushes) sfree(fe->brushes); |
1559 | if (fe->pens) sfree(fe->pens); |
1560 | |
4e7ef6e6 |
1561 | fe->colours = snewn(ncolours, COLORREF); |
1562 | fe->brushes = snewn(ncolours, HBRUSH); |
1563 | fe->pens = snewn(ncolours, HPEN); |
1564 | |
1565 | for (i = 0; i < ncolours; i++) { |
1566 | fe->colours[i] = RGB(255 * colours[i*3+0], |
1567 | 255 * colours[i*3+1], |
1568 | 255 * colours[i*3+2]); |
1569 | fe->brushes[i] = CreateSolidBrush(fe->colours[i]); |
4e7ef6e6 |
1570 | fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]); |
1571 | } |
9cfc03b7 |
1572 | sfree(colours); |
4e7ef6e6 |
1573 | } |
1574 | |
dc3de726 |
1575 | if (fe->statusbar) |
1576 | DestroyWindow(fe->statusbar); |
35ee2001 |
1577 | if (midend_wants_statusbar(fe->me)) { |
cb0c7d4a |
1578 | fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, TEXT("ooh"), |
35ee2001 |
1579 | WS_CHILD | WS_VISIBLE, |
1580 | 0, 0, 0, 0, /* status bar does these */ |
dc3de726 |
1581 | NULL, NULL, fe->inst, NULL); |
e490a96e |
1582 | } else |
1583 | fe->statusbar = NULL; |
35ee2001 |
1584 | |
1585 | get_max_puzzle_size(fe, &x, &y); |
1e3e152d |
1586 | midend_size(fe->me, &x, &y, FALSE); |
1587 | |
4e7ef6e6 |
1588 | r.left = r.top = 0; |
1589 | r.right = x; |
1590 | r.bottom = y; |
35ee2001 |
1591 | AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0); |
4e7ef6e6 |
1592 | |
cb0c7d4a |
1593 | #ifdef _WIN32_WCE |
dc3de726 |
1594 | if (fe->numpad) |
1595 | DestroyWindow(fe->numpad); |
1596 | if (fe->game->flags & REQUIRE_NUMPAD) |
cb0c7d4a |
1597 | { |
dc3de726 |
1598 | fe->numpad = CreateToolbarEx (fe->hwnd, |
1599 | WS_VISIBLE | WS_CHILD | CCS_NOPARENTALIGN | TBSTYLE_FLAT, |
1600 | 0, 10, fe->inst, IDR_PADTOOLBAR, |
1601 | tbNumpadButtons, sizeof (tbNumpadButtons) / sizeof (TBBUTTON), |
1602 | 0, 0, 14, 15, sizeof (TBBUTTON)); |
1603 | GetWindowRect(fe->numpad, &rcTB); |
1604 | GetClientRect(fe->hwnd, &rcClient); |
1605 | MoveWindow(fe->numpad, |
1606 | 0, |
1607 | rcClient.bottom - (rcTB.bottom - rcTB.top) - 1, |
1608 | rcClient.right, |
1609 | rcTB.bottom - rcTB.top, |
1610 | FALSE); |
1611 | SendMessage(fe->numpad, TB_SETINDENT, (rcClient.right - (10 * 21)) / 2, 0); |
1612 | } |
1613 | else { |
1614 | fe->numpad = NULL; |
1615 | } |
1616 | MultiByteToWideChar (CP_ACP, 0, fe->game->name, -1, wGameName, 256); |
1617 | SetWindowText(fe->hwnd, wGameName); |
cb0c7d4a |
1618 | #else |
dc3de726 |
1619 | SetWindowText(fe->hwnd, fe->game->name); |
cb0c7d4a |
1620 | #endif |
4e7ef6e6 |
1621 | |
dc3de726 |
1622 | if (fe->statusbar) |
1623 | DestroyWindow(fe->statusbar); |
3fee7a57 |
1624 | if (midend_wants_statusbar(fe->me)) { |
1625 | RECT sr; |
cb0c7d4a |
1626 | fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, TEXT("ooh"), |
1e3e152d |
1627 | WS_CHILD | WS_VISIBLE, |
1628 | 0, 0, 0, 0, /* status bar does these */ |
dc3de726 |
1629 | fe->hwnd, NULL, fe->inst, NULL); |
cb0c7d4a |
1630 | #ifdef _WIN32_WCE |
1631 | /* Flat status bar looks better on the Pocket PC */ |
1632 | SendMessage(fe->statusbar, SB_SIMPLE, (WPARAM) TRUE, 0); |
1633 | SendMessage(fe->statusbar, SB_SETTEXT, |
1634 | (WPARAM) 255 | SBT_NOBORDERS, |
1635 | (LPARAM) L""); |
1636 | #endif |
1637 | |
3fee7a57 |
1638 | /* |
1639 | * Now resize the window to take account of the status bar. |
1640 | */ |
1641 | GetWindowRect(fe->statusbar, &sr); |
1642 | GetWindowRect(fe->hwnd, &r); |
cb0c7d4a |
1643 | #ifndef _WIN32_WCE |
3fee7a57 |
1644 | SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left, |
1645 | r.bottom - r.top + sr.bottom - sr.top, |
1646 | SWP_NOMOVE | SWP_NOZORDER); |
cb0c7d4a |
1647 | #endif |
3fee7a57 |
1648 | } else { |
1e3e152d |
1649 | fe->statusbar = NULL; |
3fee7a57 |
1650 | } |
1e3e152d |
1651 | |
eb2ad6f1 |
1652 | { |
dc3de726 |
1653 | HMENU oldmenu = GetMenu(fe->hwnd); |
1654 | |
cb0c7d4a |
1655 | #ifndef _WIN32_WCE |
eb2ad6f1 |
1656 | HMENU bar = CreateMenu(); |
1657 | HMENU menu = CreateMenu(); |
6b12623f |
1658 | RECT menusize; |
eb2ad6f1 |
1659 | |
6b12623f |
1660 | AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "&Game"); |
cb0c7d4a |
1661 | #else |
1662 | HMENU menu = SHGetSubMenu(SHFindMenuBar(fe->hwnd), ID_GAME); |
1663 | DeleteMenu(menu, 0, MF_BYPOSITION); |
1664 | #endif |
fa3abef5 |
1665 | fe->gamemenu = menu; |
6b12623f |
1666 | AppendMenu(menu, MF_ENABLED, IDM_NEW, TEXT("&New")); |
1667 | AppendMenu(menu, MF_ENABLED, IDM_RESTART, TEXT("&Restart")); |
cb0c7d4a |
1668 | #ifndef _WIN32_WCE |
6b12623f |
1669 | /* ...here I run out of sensible accelerator characters. */ |
1670 | AppendMenu(menu, MF_ENABLED, IDM_DESC, TEXT("Speci&fic...")); |
1671 | AppendMenu(menu, MF_ENABLED, IDM_SEED, TEXT("Rando&m Seed...")); |
cb0c7d4a |
1672 | #endif |
eb2ad6f1 |
1673 | |
dc3de726 |
1674 | if (fe->presets) |
1675 | sfree(fe->presets); |
077f3cbe |
1676 | if ((fe->npresets = midend_num_presets(fe->me)) > 0 || |
dc3de726 |
1677 | fe->game->can_configure) { |
eb2ad6f1 |
1678 | int i; |
cb0c7d4a |
1679 | #ifndef _WIN32_WCE |
1680 | HMENU sub = CreateMenu(); |
eb2ad6f1 |
1681 | |
6b12623f |
1682 | AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "&Type"); |
cb0c7d4a |
1683 | #else |
1684 | HMENU sub = SHGetSubMenu(SHFindMenuBar(fe->hwnd), ID_TYPE); |
1685 | DeleteMenu(sub, 0, MF_BYPOSITION); |
1686 | #endif |
eb2ad6f1 |
1687 | fe->presets = snewn(fe->npresets, game_params *); |
1688 | |
1689 | for (i = 0; i < fe->npresets; i++) { |
1690 | char *name; |
cb0c7d4a |
1691 | #ifdef _WIN32_WCE |
1692 | TCHAR wName[255]; |
1693 | #endif |
eb2ad6f1 |
1694 | |
1695 | midend_fetch_preset(fe->me, i, &name, &fe->presets[i]); |
1696 | |
1697 | /* |
1698 | * FIXME: we ought to go through and do something |
1699 | * with ampersands here. |
1700 | */ |
1701 | |
cb0c7d4a |
1702 | #ifndef _WIN32_WCE |
eb2ad6f1 |
1703 | AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name); |
cb0c7d4a |
1704 | #else |
1705 | MultiByteToWideChar (CP_ACP, 0, name, -1, wName, 255); |
1706 | AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, wName); |
1707 | #endif |
eb2ad6f1 |
1708 | } |
dc3de726 |
1709 | if (fe->game->can_configure) { |
6b12623f |
1710 | AppendMenu(sub, MF_ENABLED, IDM_CONFIG, TEXT("&Custom...")); |
077f3cbe |
1711 | } |
64e360d3 |
1712 | |
1713 | fe->typemenu = sub; |
dc3de726 |
1714 | } else { |
64e360d3 |
1715 | fe->typemenu = INVALID_HANDLE_VALUE; |
dc3de726 |
1716 | fe->presets = NULL; |
1717 | } |
1718 | |
1719 | #ifdef COMBINED |
1720 | #ifdef _WIN32_WCE |
1721 | #error Windows CE does not support COMBINED build. |
1722 | #endif |
1723 | { |
1724 | HMENU games = CreateMenu(); |
1725 | int i; |
1726 | |
1727 | AppendMenu(menu, MF_SEPARATOR, 0, 0); |
1728 | AppendMenu(menu, MF_ENABLED|MF_POPUP, (UINT)games, "&Other"); |
1729 | for (i = 0; i < gamecount; i++) { |
1730 | if (strcmp(gamelist[i]->name, fe->game->name) != 0) { |
1731 | /* only include those games that aren't the same as the |
1732 | * game we're currently playing. */ |
1733 | AppendMenu(games, MF_ENABLED, IDM_GAMES + i, gamelist[i]->name); |
1734 | } |
1735 | } |
1736 | } |
1737 | #endif |
eb2ad6f1 |
1738 | |
1739 | AppendMenu(menu, MF_SEPARATOR, 0, 0); |
cb0c7d4a |
1740 | #ifndef _WIN32_WCE |
6b12623f |
1741 | AppendMenu(menu, MF_ENABLED, IDM_LOAD, TEXT("&Load...")); |
1742 | AppendMenu(menu, MF_ENABLED, IDM_SAVE, TEXT("&Save...")); |
668be019 |
1743 | AppendMenu(menu, MF_SEPARATOR, 0, 0); |
dc3de726 |
1744 | if (fe->game->can_print) { |
6b12623f |
1745 | AppendMenu(menu, MF_ENABLED, IDM_PRINT, TEXT("&Print...")); |
821ab2c6 |
1746 | AppendMenu(menu, MF_SEPARATOR, 0, 0); |
1747 | } |
cb0c7d4a |
1748 | #endif |
1749 | AppendMenu(menu, MF_ENABLED, IDM_UNDO, TEXT("Undo")); |
1750 | AppendMenu(menu, MF_ENABLED, IDM_REDO, TEXT("Redo")); |
1751 | #ifndef _WIN32_WCE |
dc3de726 |
1752 | if (fe->game->can_format_as_text_ever) { |
1cdfcdbd |
1753 | AppendMenu(menu, MF_SEPARATOR, 0, 0); |
6b12623f |
1754 | AppendMenu(menu, MF_ENABLED, IDM_COPY, TEXT("&Copy")); |
1cdfcdbd |
1755 | } |
cb0c7d4a |
1756 | #endif |
dc3de726 |
1757 | if (fe->game->can_solve) { |
2ac6d24e |
1758 | AppendMenu(menu, MF_SEPARATOR, 0, 0); |
6b12623f |
1759 | AppendMenu(menu, MF_ENABLED, IDM_SOLVE, TEXT("Sol&ve")); |
2ac6d24e |
1760 | } |
eb2ad6f1 |
1761 | AppendMenu(menu, MF_SEPARATOR, 0, 0); |
cb0c7d4a |
1762 | #ifndef _WIN32_WCE |
6b12623f |
1763 | AppendMenu(menu, MF_ENABLED, IDM_QUIT, TEXT("E&xit")); |
97098757 |
1764 | menu = CreateMenu(); |
6b12623f |
1765 | AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, TEXT("&Help")); |
cb0c7d4a |
1766 | #endif |
6b12623f |
1767 | AppendMenu(menu, MF_ENABLED, IDM_ABOUT, TEXT("&About")); |
cb0c7d4a |
1768 | #ifndef _WIN32_WCE |
750037d7 |
1769 | if (help_type != NONE) { |
dc3de726 |
1770 | char *item; |
1771 | AppendMenu(menu, MF_SEPARATOR, 0, 0); |
6b12623f |
1772 | AppendMenu(menu, MF_ENABLED, IDM_HELPC, TEXT("&Contents")); |
dc3de726 |
1773 | assert(fe->game->name); |
1774 | item = snewn(10+strlen(fe->game->name), char); /*ick*/ |
1775 | sprintf(item, "&Help on %s", fe->game->name); |
1776 | AppendMenu(menu, MF_ENABLED, IDM_GAMEHELP, item); |
1777 | sfree(item); |
e91825f8 |
1778 | } |
dc3de726 |
1779 | DestroyMenu(oldmenu); |
eb2ad6f1 |
1780 | SetMenu(fe->hwnd, bar); |
6b12623f |
1781 | get_menu_size(fe->hwnd, &menusize); |
1782 | fe->xmin = (menusize.right - menusize.left) + 25; |
cb0c7d4a |
1783 | #endif |
eb2ad6f1 |
1784 | } |
1785 | |
dc3de726 |
1786 | if (fe->bitmap) DeleteObject(fe->bitmap); |
e490a96e |
1787 | fe->bitmap = NULL; |
dc3de726 |
1788 | new_game_size(fe, fe->puzz_scale); /* initialises fe->bitmap */ |
fd1a1a2b |
1789 | |
dc3de726 |
1790 | return 0; |
1791 | } |
4e7ef6e6 |
1792 | |
dc3de726 |
1793 | static void show_window(frontend *fe) |
1794 | { |
cb0c7d4a |
1795 | ShowWindow(fe->hwnd, SW_SHOWNORMAL); |
4e7ef6e6 |
1796 | SetForegroundWindow(fe->hwnd); |
1797 | |
64e360d3 |
1798 | update_type_menu_tick(fe); |
fa3abef5 |
1799 | update_copy_menu_greying(fe); |
64e360d3 |
1800 | |
4e7ef6e6 |
1801 | midend_redraw(fe->me); |
4e7ef6e6 |
1802 | } |
1803 | |
cb0c7d4a |
1804 | #ifdef _WIN32_WCE |
1805 | static HFONT dialog_title_font() |
1806 | { |
1807 | static HFONT hf = NULL; |
1808 | LOGFONT lf; |
1809 | |
1810 | if (hf) |
1811 | return hf; |
1812 | |
1813 | memset (&lf, 0, sizeof(LOGFONT)); |
1814 | lf.lfHeight = -11; /* - ((8 * GetDeviceCaps(hdc, LOGPIXELSY)) / 72) */ |
1815 | lf.lfWeight = FW_BOLD; |
1816 | wcscpy(lf.lfFaceName, TEXT("Tahoma")); |
1817 | |
1818 | return hf = CreateFontIndirect(&lf); |
1819 | } |
1820 | |
1821 | static void make_dialog_full_screen(HWND hwnd) |
1822 | { |
1823 | SHINITDLGINFO shidi; |
1824 | |
1825 | /* Make dialog full screen */ |
1826 | shidi.dwMask = SHIDIM_FLAGS; |
1827 | shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIZEDLGFULLSCREEN | |
1828 | SHIDIF_EMPTYMENU; |
1829 | shidi.hDlg = hwnd; |
1830 | SHInitDialog(&shidi); |
1831 | } |
1832 | #endif |
1833 | |
97098757 |
1834 | static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg, |
1835 | WPARAM wParam, LPARAM lParam) |
1836 | { |
1837 | frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA); |
1838 | |
1839 | switch (msg) { |
1840 | case WM_INITDIALOG: |
cb0c7d4a |
1841 | #ifdef _WIN32_WCE |
1842 | { |
1843 | char title[256]; |
1844 | |
1845 | make_dialog_full_screen(hwnd); |
1846 | |
dc3de726 |
1847 | sprintf(title, "About %.250s", fe->game->name); |
cb0c7d4a |
1848 | SetDlgItemTextA(hwnd, IDC_ABOUT_CAPTION, title); |
1849 | |
1850 | SendDlgItemMessage(hwnd, IDC_ABOUT_CAPTION, WM_SETFONT, |
1851 | (WPARAM) dialog_title_font(), 0); |
1852 | |
dc3de726 |
1853 | SetDlgItemTextA(hwnd, IDC_ABOUT_GAME, fe->game->name); |
cb0c7d4a |
1854 | SetDlgItemTextA(hwnd, IDC_ABOUT_VERSION, ver); |
1855 | } |
1856 | #endif |
1857 | return TRUE; |
97098757 |
1858 | |
1859 | case WM_COMMAND: |
cb0c7d4a |
1860 | if (LOWORD(wParam) == IDOK) |
1861 | #ifdef _WIN32_WCE |
1862 | EndDialog(hwnd, 1); |
1863 | #else |
97098757 |
1864 | fe->dlg_done = 1; |
cb0c7d4a |
1865 | #endif |
97098757 |
1866 | return 0; |
1867 | |
1868 | case WM_CLOSE: |
cb0c7d4a |
1869 | #ifdef _WIN32_WCE |
1870 | EndDialog(hwnd, 1); |
1871 | #else |
97098757 |
1872 | fe->dlg_done = 1; |
cb0c7d4a |
1873 | #endif |
97098757 |
1874 | return 0; |
1875 | } |
1876 | |
1877 | return 0; |
1878 | } |
1879 | |
821ab2c6 |
1880 | /* |
1881 | * Wrappers on midend_{get,set}_config, which extend the CFG_* |
1882 | * enumeration to add CFG_PRINT. |
1883 | */ |
1884 | static config_item *frontend_get_config(frontend *fe, int which, |
1885 | char **wintitle) |
1886 | { |
1887 | if (which < CFG_FRONTEND_SPECIFIC) { |
1888 | return midend_get_config(fe->me, which, wintitle); |
1889 | } else if (which == CFG_PRINT) { |
1890 | config_item *ret; |
1891 | int i; |
1892 | |
dc3de726 |
1893 | *wintitle = snewn(40 + strlen(fe->game->name), char); |
1894 | sprintf(*wintitle, "%s print setup", fe->game->name); |
821ab2c6 |
1895 | |
1896 | ret = snewn(8, config_item); |
1897 | |
1898 | i = 0; |
1899 | |
1900 | ret[i].name = "Number of puzzles to print"; |
1901 | ret[i].type = C_STRING; |
1902 | ret[i].sval = dupstr("1"); |
1903 | ret[i].ival = 0; |
1904 | i++; |
1905 | |
1906 | ret[i].name = "Number of puzzles across the page"; |
1907 | ret[i].type = C_STRING; |
1908 | ret[i].sval = dupstr("1"); |
1909 | ret[i].ival = 0; |
1910 | i++; |
1911 | |
1912 | ret[i].name = "Number of puzzles down the page"; |
1913 | ret[i].type = C_STRING; |
1914 | ret[i].sval = dupstr("1"); |
1915 | ret[i].ival = 0; |
1916 | i++; |
1917 | |
1918 | ret[i].name = "Percentage of standard size"; |
1919 | ret[i].type = C_STRING; |
1920 | ret[i].sval = dupstr("100.0"); |
1921 | ret[i].ival = 0; |
1922 | i++; |
1923 | |
1924 | ret[i].name = "Include currently shown puzzle"; |
1925 | ret[i].type = C_BOOLEAN; |
1926 | ret[i].sval = NULL; |
1927 | ret[i].ival = TRUE; |
1928 | i++; |
1929 | |
1930 | ret[i].name = "Print solutions"; |
1931 | ret[i].type = C_BOOLEAN; |
1932 | ret[i].sval = NULL; |
1933 | ret[i].ival = FALSE; |
1934 | i++; |
1935 | |
dc3de726 |
1936 | if (fe->game->can_print_in_colour) { |
821ab2c6 |
1937 | ret[i].name = "Print in colour"; |
1938 | ret[i].type = C_BOOLEAN; |
1939 | ret[i].sval = NULL; |
1940 | ret[i].ival = FALSE; |
1941 | i++; |
1942 | } |
1943 | |
1944 | ret[i].name = NULL; |
1945 | ret[i].type = C_END; |
1946 | ret[i].sval = NULL; |
1947 | ret[i].ival = 0; |
1948 | i++; |
1949 | |
1950 | return ret; |
1951 | } else { |
1952 | assert(!"We should never get here"); |
1953 | return NULL; |
1954 | } |
1955 | } |
1956 | |
1957 | static char *frontend_set_config(frontend *fe, int which, config_item *cfg) |
1958 | { |
1959 | if (which < CFG_FRONTEND_SPECIFIC) { |
1960 | return midend_set_config(fe->me, which, cfg); |
1961 | } else if (which == CFG_PRINT) { |
1962 | if ((fe->printcount = atoi(cfg[0].sval)) <= 0) |
1963 | return "Number of puzzles to print should be at least one"; |
1964 | if ((fe->printw = atoi(cfg[1].sval)) <= 0) |
1965 | return "Number of puzzles across the page should be at least one"; |
1966 | if ((fe->printh = atoi(cfg[2].sval)) <= 0) |
1967 | return "Number of puzzles down the page should be at least one"; |
1968 | if ((fe->printscale = (float)atof(cfg[3].sval)) <= 0) |
1969 | return "Print size should be positive"; |
1970 | fe->printcurr = cfg[4].ival; |
1971 | fe->printsolns = cfg[5].ival; |
dc3de726 |
1972 | fe->printcolour = fe->game->can_print_in_colour && cfg[6].ival; |
821ab2c6 |
1973 | return NULL; |
1974 | } else { |
1975 | assert(!"We should never get here"); |
1976 | return "Internal error"; |
1977 | } |
1978 | } |
1979 | |
cb0c7d4a |
1980 | #ifdef _WIN32_WCE |
1981 | /* Separate version of mkctrl function for the Pocket PC. */ |
1982 | /* Control coordinates should be specified in dialog units. */ |
1983 | HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2, |
1984 | LPCTSTR wclass, int wstyle, |
1985 | int exstyle, const char *wtext, int wid) |
1986 | { |
1987 | RECT rc; |
1988 | TCHAR wwtext[256]; |
1989 | |
1990 | /* Convert dialog units into pixels */ |
1991 | rc.left = x1; rc.right = x2; |
1992 | rc.top = y1; rc.bottom = y2; |
1993 | MapDialogRect(fe->cfgbox, &rc); |
1994 | |
1995 | MultiByteToWideChar (CP_ACP, 0, wtext, -1, wwtext, 256); |
1996 | |
1997 | return CreateWindowEx(exstyle, wclass, wwtext, |
1998 | wstyle | WS_CHILD | WS_VISIBLE, |
1999 | rc.left, rc.top, |
2000 | rc.right - rc.left, rc.bottom - rc.top, |
2001 | fe->cfgbox, (HMENU) wid, fe->inst, NULL); |
2002 | } |
2003 | |
2004 | static void create_config_controls(frontend * fe) |
2005 | { |
2006 | int id, nctrls; |
2007 | int col1l, col1r, col2l, col2r, y; |
2008 | config_item *i; |
2009 | struct cfg_aux *j; |
2010 | HWND ctl; |
2011 | |
2012 | /* Control placement done in dialog units */ |
2013 | col1l = 4; col1r = 96; /* Label column */ |
2014 | col2l = 100; col2r = 154; /* Input column (edit boxes and combo boxes) */ |
2015 | |
2016 | /* |
2017 | * Count the controls so we can allocate cfgaux. |
2018 | */ |
2019 | for (nctrls = 0, i = fe->cfg; i->type != C_END; i++) |
2020 | nctrls++; |
2021 | fe->cfgaux = snewn(nctrls, struct cfg_aux); |
2022 | |
2023 | id = 1000; |
2024 | y = 22; /* Leave some room for the dialog title */ |
2025 | for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) { |
2026 | switch (i->type) { |
2027 | case C_STRING: |
2028 | /* |
2029 | * Edit box with a label beside it. |
2030 | */ |
2031 | mkctrl(fe, col1l, col1r, y + 1, y + 11, |
2032 | TEXT("Static"), SS_LEFTNOWORDWRAP, 0, i->name, id++); |
2033 | mkctrl(fe, col2l, col2r, y, y + 12, |
2034 | TEXT("EDIT"), WS_BORDER | WS_TABSTOP | ES_AUTOHSCROLL, |
2035 | 0, "", (j->ctlid = id++)); |
2036 | SetDlgItemTextA(fe->cfgbox, j->ctlid, i->sval); |
2037 | break; |
2038 | |
2039 | case C_BOOLEAN: |
2040 | /* |
2041 | * Simple checkbox. |
2042 | */ |
2043 | mkctrl(fe, col1l, col2r, y + 1, y + 11, TEXT("BUTTON"), |
2044 | BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP, |
2045 | 0, i->name, (j->ctlid = id++)); |
2046 | CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0)); |
2047 | break; |
2048 | |
2049 | case C_CHOICES: |
2050 | /* |
2051 | * Drop-down list with a label beside it. |
2052 | */ |
2053 | mkctrl(fe, col1l, col1r, y + 1, y + 11, |
2054 | TEXT("STATIC"), SS_LEFTNOWORDWRAP, 0, i->name, id++); |
2055 | ctl = mkctrl(fe, col2l, col2r, y, y + 48, |
2056 | TEXT("COMBOBOX"), WS_BORDER | WS_TABSTOP | |
2057 | CBS_DROPDOWNLIST | CBS_HASSTRINGS, |
2058 | 0, "", (j->ctlid = id++)); |
2059 | { |
2060 | char c, *p, *q, *str; |
2061 | |
2062 | p = i->sval; |
2063 | c = *p++; |
2064 | while (*p) { |
2065 | q = p; |
2066 | while (*q && *q != c) q++; |
2067 | str = snewn(q-p+1, char); |
2068 | strncpy(str, p, q-p); |
2069 | str[q-p] = '\0'; |
2070 | { |
2071 | TCHAR ws[50]; |
2072 | MultiByteToWideChar (CP_ACP, 0, str, -1, ws, 50); |
2073 | SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)ws); |
2074 | } |
2075 | |
2076 | sfree(str); |
2077 | if (*q) q++; |
2078 | p = q; |
2079 | } |
2080 | } |
2081 | SendMessage(ctl, CB_SETCURSEL, i->ival, 0); |
2082 | break; |
2083 | } |
2084 | |
2085 | y += 15; |
2086 | } |
2087 | |
2088 | } |
2089 | #endif |
2090 | |
077f3cbe |
2091 | static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg, |
2092 | WPARAM wParam, LPARAM lParam) |
2093 | { |
2094 | frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA); |
2095 | config_item *i; |
2096 | struct cfg_aux *j; |
2097 | |
2098 | switch (msg) { |
2099 | case WM_INITDIALOG: |
cb0c7d4a |
2100 | #ifdef _WIN32_WCE |
2101 | { |
2102 | char *title; |
2103 | |
2104 | fe = (frontend *) lParam; |
2105 | SetWindowLong(hwnd, GWL_USERDATA, lParam); |
2106 | fe->cfgbox = hwnd; |
2107 | |
2108 | fe->cfg = frontend_get_config(fe, fe->cfg_which, &title); |
2109 | |
2110 | make_dialog_full_screen(hwnd); |
2111 | |
2112 | SetDlgItemTextA(hwnd, IDC_CONFIG_CAPTION, title); |
2113 | SendDlgItemMessage(hwnd, IDC_CONFIG_CAPTION, WM_SETFONT, |
2114 | (WPARAM) dialog_title_font(), 0); |
2115 | |
2116 | create_config_controls(fe); |
2117 | } |
2118 | #endif |
2119 | return TRUE; |
077f3cbe |
2120 | |
2121 | case WM_COMMAND: |
2122 | /* |
2123 | * OK and Cancel are special cases. |
2124 | */ |
cb0c7d4a |
2125 | if ((LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) { |
077f3cbe |
2126 | if (LOWORD(wParam) == IDOK) { |
821ab2c6 |
2127 | char *err = frontend_set_config(fe, fe->cfg_which, fe->cfg); |
077f3cbe |
2128 | |
2129 | if (err) { |
2130 | MessageBox(hwnd, err, "Validation error", |
2131 | MB_ICONERROR | MB_OK); |
2132 | } else { |
cb0c7d4a |
2133 | #ifdef _WIN32_WCE |
2134 | EndDialog(hwnd, 2); |
2135 | #else |
97098757 |
2136 | fe->dlg_done = 2; |
cb0c7d4a |
2137 | #endif |
077f3cbe |
2138 | } |
2139 | } else { |
cb0c7d4a |
2140 | #ifdef _WIN32_WCE |
2141 | EndDialog(hwnd, 1); |
2142 | #else |
97098757 |
2143 | fe->dlg_done = 1; |
cb0c7d4a |
2144 | #endif |
077f3cbe |
2145 | } |
2146 | return 0; |
2147 | } |
2148 | |
2149 | /* |
2150 | * First find the control whose id this is. |
2151 | */ |
2152 | for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) { |
2153 | if (j->ctlid == LOWORD(wParam)) |
2154 | break; |
2155 | } |
2156 | if (i->type == C_END) |
2157 | return 0; /* not our problem */ |
2158 | |
2159 | if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) { |
2160 | char buffer[4096]; |
cb0c7d4a |
2161 | #ifdef _WIN32_WCE |
2162 | TCHAR wBuffer[4096]; |
2163 | GetDlgItemText(fe->cfgbox, j->ctlid, wBuffer, 4096); |
2164 | WideCharToMultiByte(CP_ACP, 0, wBuffer, -1, buffer, 4096, NULL, NULL); |
2165 | #else |
077f3cbe |
2166 | GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer)); |
cb0c7d4a |
2167 | #endif |
077f3cbe |
2168 | buffer[lenof(buffer)-1] = '\0'; |
2169 | sfree(i->sval); |
2170 | i->sval = dupstr(buffer); |
2171 | } else if (i->type == C_BOOLEAN && |
2172 | (HIWORD(wParam) == BN_CLICKED || |
cb0c7d4a |
2173 | HIWORD(wParam) == BN_DBLCLK)) { |
077f3cbe |
2174 | i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid); |
2175 | } else if (i->type == C_CHOICES && |
2176 | HIWORD(wParam) == CBN_SELCHANGE) { |
2177 | i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid, |
2178 | CB_GETCURSEL, 0, 0); |
2179 | } |
2180 | |
2181 | return 0; |
2182 | |
2183 | case WM_CLOSE: |
97098757 |
2184 | fe->dlg_done = 1; |
077f3cbe |
2185 | return 0; |
2186 | } |
2187 | |
2188 | return 0; |
2189 | } |
2190 | |
cb0c7d4a |
2191 | #ifndef _WIN32_WCE |
077f3cbe |
2192 | HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2, |
2193 | char *wclass, int wstyle, |
97098757 |
2194 | int exstyle, const char *wtext, int wid) |
077f3cbe |
2195 | { |
2196 | HWND ret; |
2197 | ret = CreateWindowEx(exstyle, wclass, wtext, |
2198 | wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1, |
2199 | fe->cfgbox, (HMENU) wid, fe->inst, NULL); |
2200 | SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0)); |
2201 | return ret; |
2202 | } |
cb0c7d4a |
2203 | #endif |
077f3cbe |
2204 | |
97098757 |
2205 | static void about(frontend *fe) |
2206 | { |
cb0c7d4a |
2207 | #ifdef _WIN32_WCE |
2208 | DialogBox(fe->inst, MAKEINTRESOURCE(IDD_ABOUT), fe->hwnd, AboutDlgProc); |
2209 | #else |
97098757 |
2210 | int i; |
2211 | WNDCLASS wc; |
2212 | MSG msg; |
2213 | TEXTMETRIC tm; |
2214 | HDC hdc; |
2215 | HFONT oldfont; |
2216 | SIZE size; |
2217 | int gm, id; |
2218 | int winwidth, winheight, y; |
2219 | int height, width, maxwid; |
2220 | const char *strings[16]; |
2221 | int lengths[16]; |
2222 | int nstrings = 0; |
2223 | char titlebuf[512]; |
2224 | |
dc3de726 |
2225 | sprintf(titlebuf, "About %.250s", fe->game->name); |
97098757 |
2226 | |
dc3de726 |
2227 | strings[nstrings++] = fe->game->name; |
97098757 |
2228 | strings[nstrings++] = "from Simon Tatham's Portable Puzzle Collection"; |
2229 | strings[nstrings++] = ver; |
2230 | |
cb0c7d4a |
2231 | wc.style = CS_DBLCLKS | CS_SAVEBITS; |
97098757 |
2232 | wc.lpfnWndProc = DefDlgProc; |
2233 | wc.cbClsExtra = 0; |
2234 | wc.cbWndExtra = DLGWINDOWEXTRA + 8; |
2235 | wc.hInstance = fe->inst; |
2236 | wc.hIcon = NULL; |
2237 | wc.hCursor = LoadCursor(NULL, IDC_ARROW); |
2238 | wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1); |
2239 | wc.lpszMenuName = NULL; |
2240 | wc.lpszClassName = "GameAboutBox"; |
2241 | RegisterClass(&wc); |
2242 | |
2243 | hdc = GetDC(fe->hwnd); |
2244 | SetMapMode(hdc, MM_TEXT); |
2245 | |
2246 | fe->dlg_done = FALSE; |
2247 | |
2248 | fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72), |
2249 | 0, 0, 0, 0, |
2250 | FALSE, FALSE, FALSE, DEFAULT_CHARSET, |
2251 | OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, |
2252 | DEFAULT_QUALITY, |
2253 | FF_SWISS, |
2254 | "MS Shell Dlg"); |
2255 | |
2256 | oldfont = SelectObject(hdc, fe->cfgfont); |
2257 | if (GetTextMetrics(hdc, &tm)) { |
2258 | height = tm.tmAscent + tm.tmDescent; |
2259 | width = tm.tmAveCharWidth; |
2260 | } else { |
2261 | height = width = 30; |
2262 | } |
2263 | |
2264 | /* |
2265 | * Figure out the layout of the About box by measuring the |
2266 | * length of each piece of text. |
2267 | */ |
2268 | maxwid = 0; |
2269 | winheight = height/2; |
2270 | |
2271 | for (i = 0; i < nstrings; i++) { |
2272 | if (GetTextExtentPoint32(hdc, strings[i], strlen(strings[i]), &size)) |
2273 | lengths[i] = size.cx; |
2274 | else |
2275 | lengths[i] = 0; /* *shrug* */ |
2276 | if (maxwid < lengths[i]) |
2277 | maxwid = lengths[i]; |
2278 | winheight += height * 3 / 2 + (height / 2); |
2279 | } |
2280 | |
2281 | winheight += height + height * 7 / 4; /* OK button */ |
2282 | winwidth = maxwid + 4*width; |
2283 | |
2284 | SelectObject(hdc, oldfont); |
2285 | ReleaseDC(fe->hwnd, hdc); |
2286 | |
2287 | /* |
2288 | * Create the dialog, now that we know its size. |
2289 | */ |
2290 | { |
2291 | RECT r, r2; |
2292 | |
2293 | r.left = r.top = 0; |
2294 | r.right = winwidth; |
2295 | r.bottom = winheight; |
2296 | |
2297 | AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*| |
2298 | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | |
2299 | WS_CAPTION | WS_SYSMENU*/) &~ |
2300 | (WS_MAXIMIZEBOX | WS_OVERLAPPED), |
2301 | FALSE, 0); |
2302 | |
2303 | /* |
2304 | * Centre the dialog on its parent window. |
2305 | */ |
2306 | r.right -= r.left; |
2307 | r.bottom -= r.top; |
2308 | GetWindowRect(fe->hwnd, &r2); |
2309 | r.left = (r2.left + r2.right - r.right) / 2; |
2310 | r.top = (r2.top + r2.bottom - r.bottom) / 2; |
2311 | r.right += r.left; |
2312 | r.bottom += r.top; |
2313 | |
2314 | fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, titlebuf, |
2315 | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | |
2316 | WS_CAPTION | WS_SYSMENU, |
2317 | r.left, r.top, |
2318 | r.right-r.left, r.bottom-r.top, |
2319 | fe->hwnd, NULL, fe->inst, NULL); |
2320 | } |
2321 | |
2322 | SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE); |
2323 | |
2324 | SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe); |
2325 | SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)AboutDlgProc); |
2326 | |
2327 | id = 1000; |
2328 | y = height/2; |
2329 | for (i = 0; i < nstrings; i++) { |
2330 | int border = width*2 + (maxwid - lengths[i]) / 2; |
2331 | mkctrl(fe, border, border+lengths[i], y+height*1/8, y+height*9/8, |
2332 | "Static", 0, 0, strings[i], id++); |
2333 | y += height*3/2; |
2334 | |
2335 | assert(y < winheight); |
2336 | y += height/2; |
2337 | } |
2338 | |
2339 | y += height/2; /* extra space before OK */ |
2340 | mkctrl(fe, width*2, maxwid+width*2, y, y+height*7/4, "BUTTON", |
be39fd6d |
2341 | BS_PUSHBUTTON | WS_TABSTOP | BS_DEFPUSHBUTTON, 0, |
97098757 |
2342 | "OK", IDOK); |
2343 | |
2344 | SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0); |
2345 | |
2346 | EnableWindow(fe->hwnd, FALSE); |
cb0c7d4a |
2347 | ShowWindow(fe->cfgbox, SW_SHOWNORMAL); |
97098757 |
2348 | while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) { |
2349 | if (!IsDialogMessage(fe->cfgbox, &msg)) |
2350 | DispatchMessage(&msg); |
2351 | if (fe->dlg_done) |
2352 | break; |
2353 | } |
2354 | EnableWindow(fe->hwnd, TRUE); |
2355 | SetForegroundWindow(fe->hwnd); |
2356 | DestroyWindow(fe->cfgbox); |
2357 | DeleteObject(fe->cfgfont); |
cb0c7d4a |
2358 | #endif |
97098757 |
2359 | } |
2360 | |
5928817c |
2361 | static int get_config(frontend *fe, int which) |
077f3cbe |
2362 | { |
cb0c7d4a |
2363 | #ifdef _WIN32_WCE |
2364 | fe->cfg_which = which; |
2365 | |
2366 | return DialogBoxParam(fe->inst, |
2367 | MAKEINTRESOURCE(IDD_CONFIG), |
2368 | fe->hwnd, ConfigDlgProc, |
2369 | (LPARAM) fe) == 2; |
2370 | #else |
077f3cbe |
2371 | config_item *i; |
2372 | struct cfg_aux *j; |
5928817c |
2373 | char *title; |
077f3cbe |
2374 | WNDCLASS wc; |
2375 | MSG msg; |
2376 | TEXTMETRIC tm; |
2377 | HDC hdc; |
2378 | HFONT oldfont; |
2379 | SIZE size; |
2380 | HWND ctl; |
2381 | int gm, id, nctrls; |
2382 | int winwidth, winheight, col1l, col1r, col2l, col2r, y; |
2383 | int height, width, maxlabel, maxcheckbox; |
2384 | |
cb0c7d4a |
2385 | wc.style = CS_DBLCLKS | CS_SAVEBITS; |
077f3cbe |
2386 | wc.lpfnWndProc = DefDlgProc; |
2387 | wc.cbClsExtra = 0; |
2388 | wc.cbWndExtra = DLGWINDOWEXTRA + 8; |
2389 | wc.hInstance = fe->inst; |
2390 | wc.hIcon = NULL; |
2391 | wc.hCursor = LoadCursor(NULL, IDC_ARROW); |
2392 | wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1); |
2393 | wc.lpszMenuName = NULL; |
2394 | wc.lpszClassName = "GameConfigBox"; |
2395 | RegisterClass(&wc); |
2396 | |
2397 | hdc = GetDC(fe->hwnd); |
2398 | SetMapMode(hdc, MM_TEXT); |
2399 | |
97098757 |
2400 | fe->dlg_done = FALSE; |
077f3cbe |
2401 | |
2402 | fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72), |
2403 | 0, 0, 0, 0, |
2404 | FALSE, FALSE, FALSE, DEFAULT_CHARSET, |
2405 | OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, |
2406 | DEFAULT_QUALITY, |
2407 | FF_SWISS, |
2408 | "MS Shell Dlg"); |
2409 | |
2410 | oldfont = SelectObject(hdc, fe->cfgfont); |
2411 | if (GetTextMetrics(hdc, &tm)) { |
2412 | height = tm.tmAscent + tm.tmDescent; |
2413 | width = tm.tmAveCharWidth; |
2414 | } else { |
2415 | height = width = 30; |
2416 | } |
2417 | |
821ab2c6 |
2418 | fe->cfg = frontend_get_config(fe, which, &title); |
5928817c |
2419 | fe->cfg_which = which; |
077f3cbe |
2420 | |
2421 | /* |
2422 | * Figure out the layout of the config box by measuring the |
2423 | * length of each piece of text. |
2424 | */ |
2425 | maxlabel = maxcheckbox = 0; |
2426 | winheight = height/2; |
2427 | |
2428 | for (i = fe->cfg; i->type != C_END; i++) { |
2429 | switch (i->type) { |
2430 | case C_STRING: |
2431 | case C_CHOICES: |
2432 | /* |
2433 | * Both these control types have a label filling only |
2434 | * the left-hand column of the box. |
2435 | */ |
2436 | if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) && |
2437 | maxlabel < size.cx) |
2438 | maxlabel = size.cx; |
2439 | winheight += height * 3 / 2 + (height / 2); |
2440 | break; |
2441 | |
2442 | case C_BOOLEAN: |
2443 | /* |
2444 | * Checkboxes take up the whole of the box width. |
2445 | */ |
2446 | if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) && |
2447 | maxcheckbox < size.cx) |
2448 | maxcheckbox = size.cx; |
2449 | winheight += height + (height / 2); |
2450 | break; |
2451 | } |
2452 | } |
2453 | |
2454 | winheight += height + height * 7 / 4; /* OK / Cancel buttons */ |
2455 | |
2456 | col1l = 2*width; |
2457 | col1r = col1l + maxlabel; |
2458 | col2l = col1r + 2*width; |
2459 | col2r = col2l + 30*width; |
2460 | if (col2r < col1l+2*height+maxcheckbox) |
2461 | col2r = col1l+2*height+maxcheckbox; |
2462 | winwidth = col2r + 2*width; |
2463 | |
97098757 |
2464 | SelectObject(hdc, oldfont); |
077f3cbe |
2465 | ReleaseDC(fe->hwnd, hdc); |
2466 | |
2467 | /* |
2468 | * Create the dialog, now that we know its size. |
2469 | */ |
2470 | { |
2471 | RECT r, r2; |
2472 | |
2473 | r.left = r.top = 0; |
2474 | r.right = winwidth; |
2475 | r.bottom = winheight; |
2476 | |
2477 | AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*| |
2478 | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | |
2479 | WS_CAPTION | WS_SYSMENU*/) &~ |
2480 | (WS_MAXIMIZEBOX | WS_OVERLAPPED), |
2481 | FALSE, 0); |
2482 | |
2483 | /* |
2484 | * Centre the dialog on its parent window. |
2485 | */ |
2486 | r.right -= r.left; |
2487 | r.bottom -= r.top; |
2488 | GetWindowRect(fe->hwnd, &r2); |
2489 | r.left = (r2.left + r2.right - r.right) / 2; |
2490 | r.top = (r2.top + r2.bottom - r.bottom) / 2; |
2491 | r.right += r.left; |
2492 | r.bottom += r.top; |
2493 | |
5928817c |
2494 | fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title, |
077f3cbe |
2495 | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | |
2496 | WS_CAPTION | WS_SYSMENU, |
2497 | r.left, r.top, |
2498 | r.right-r.left, r.bottom-r.top, |
2499 | fe->hwnd, NULL, fe->inst, NULL); |
5928817c |
2500 | sfree(title); |
077f3cbe |
2501 | } |
2502 | |
2503 | SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE); |
2504 | |
2505 | SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe); |
2506 | SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc); |
2507 | |
2508 | /* |
2509 | * Count the controls so we can allocate cfgaux. |
2510 | */ |
2511 | for (nctrls = 0, i = fe->cfg; i->type != C_END; i++) |
2512 | nctrls++; |
2513 | fe->cfgaux = snewn(nctrls, struct cfg_aux); |
2514 | |
2515 | id = 1000; |
2516 | y = height/2; |
2517 | for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) { |
2518 | switch (i->type) { |
2519 | case C_STRING: |
2520 | /* |
2521 | * Edit box with a label beside it. |
2522 | */ |
2523 | mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8, |
2524 | "Static", 0, 0, i->name, id++); |
2525 | ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2, |
2526 | "EDIT", WS_TABSTOP | ES_AUTOHSCROLL, |
2527 | WS_EX_CLIENTEDGE, "", (j->ctlid = id++)); |
2528 | SetWindowText(ctl, i->sval); |
2529 | y += height*3/2; |
2530 | break; |
2531 | |
2532 | case C_BOOLEAN: |
2533 | /* |
2534 | * Simple checkbox. |
2535 | */ |
2536 | mkctrl(fe, col1l, col2r, y, y+height, "BUTTON", |
2537 | BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP, |
2538 | 0, i->name, (j->ctlid = id++)); |
42132f2d |
2539 | CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0)); |
077f3cbe |
2540 | y += height; |
2541 | break; |
2542 | |
2543 | case C_CHOICES: |
2544 | /* |
2545 | * Drop-down list with a label beside it. |
2546 | */ |
2547 | mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8, |
2548 | "STATIC", 0, 0, i->name, id++); |
2549 | ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2, |
2550 | "COMBOBOX", WS_TABSTOP | |
2551 | CBS_DROPDOWNLIST | CBS_HASSTRINGS, |
2552 | WS_EX_CLIENTEDGE, "", (j->ctlid = id++)); |
2553 | { |
2554 | char c, *p, *q, *str; |
2555 | |
2556 | SendMessage(ctl, CB_RESETCONTENT, 0, 0); |
2557 | p = i->sval; |
2558 | c = *p++; |
2559 | while (*p) { |
2560 | q = p; |
2561 | while (*q && *q != c) q++; |
2562 | str = snewn(q-p+1, char); |
2563 | strncpy(str, p, q-p); |
2564 | str[q-p] = '\0'; |
2565 | SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str); |
2566 | sfree(str); |
2567 | if (*q) q++; |
2568 | p = q; |
2569 | } |
2570 | } |
2571 | |
2572 | SendMessage(ctl, CB_SETCURSEL, i->ival, 0); |
2573 | |
2574 | y += height*3/2; |
2575 | break; |
2576 | } |
2577 | |
2578 | assert(y < winheight); |
2579 | y += height/2; |
2580 | } |
2581 | |
2582 | y += height/2; /* extra space before OK and Cancel */ |
2583 | mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON", |
be39fd6d |
2584 | BS_PUSHBUTTON | WS_TABSTOP | BS_DEFPUSHBUTTON, 0, |
077f3cbe |
2585 | "OK", IDOK); |
2586 | mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON", |
be39fd6d |
2587 | BS_PUSHBUTTON | WS_TABSTOP, 0, "Cancel", IDCANCEL); |
077f3cbe |
2588 | |
2589 | SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0); |
2590 | |
2591 | EnableWindow(fe->hwnd, FALSE); |
cb0c7d4a |
2592 | ShowWindow(fe->cfgbox, SW_SHOWNORMAL); |
077f3cbe |
2593 | while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) { |
2594 | if (!IsDialogMessage(fe->cfgbox, &msg)) |
2595 | DispatchMessage(&msg); |
97098757 |
2596 | if (fe->dlg_done) |
077f3cbe |
2597 | break; |
2598 | } |
2599 | EnableWindow(fe->hwnd, TRUE); |
2600 | SetForegroundWindow(fe->hwnd); |
2601 | DestroyWindow(fe->cfgbox); |
2602 | DeleteObject(fe->cfgfont); |
2603 | |
2604 | free_cfg(fe->cfg); |
2605 | sfree(fe->cfgaux); |
2606 | |
97098757 |
2607 | return (fe->dlg_done == 2); |
cb0c7d4a |
2608 | #endif |
077f3cbe |
2609 | } |
2610 | |
cb0c7d4a |
2611 | #ifdef _WIN32_WCE |
2612 | static void calculate_bitmap_position(frontend *fe, int x, int y) |
2613 | { |
2614 | /* Pocket PC - center the game in the full screen window */ |
2615 | int yMargin; |
2616 | RECT rcClient; |
2617 | |
2618 | GetClientRect(fe->hwnd, &rcClient); |
2619 | fe->bitmapPosition.left = (rcClient.right - x) / 2; |
2620 | yMargin = rcClient.bottom - y; |
2621 | |
2622 | if (fe->numpad != NULL) { |
2623 | RECT rcPad; |
2624 | GetWindowRect(fe->numpad, &rcPad); |
2625 | yMargin -= rcPad.bottom - rcPad.top; |
2626 | } |
2627 | |
2628 | if (fe->statusbar != NULL) { |
2629 | RECT rcStatus; |
2630 | GetWindowRect(fe->statusbar, &rcStatus); |
2631 | yMargin -= rcStatus.bottom - rcStatus.top; |
2632 | } |
2633 | |
2634 | fe->bitmapPosition.top = yMargin / 2; |
2635 | |
2636 | fe->bitmapPosition.right = fe->bitmapPosition.left + x; |
2637 | fe->bitmapPosition.bottom = fe->bitmapPosition.top + y; |
2638 | } |
2639 | #else |
2640 | static void calculate_bitmap_position(frontend *fe, int x, int y) |
2641 | { |
2642 | /* Plain Windows - position the game in the upper-left corner */ |
2643 | fe->bitmapPosition.left = 0; |
2644 | fe->bitmapPosition.top = 0; |
2645 | fe->bitmapPosition.right = fe->bitmapPosition.left + x; |
2646 | fe->bitmapPosition.bottom = fe->bitmapPosition.top + y; |
2647 | } |
2648 | #endif |
2649 | |
6b12623f |
2650 | static void new_bitmap(frontend *fe, int x, int y) |
2651 | { |
2652 | HDC hdc; |
2653 | |
2654 | if (fe->bitmap) DeleteObject(fe->bitmap); |
2655 | |
2656 | hdc = GetDC(fe->hwnd); |
2657 | fe->bitmap = CreateCompatibleBitmap(hdc, x, y); |
2658 | calculate_bitmap_position(fe, x, y); |
2659 | ReleaseDC(fe->hwnd, hdc); |
2660 | } |
2661 | |
dc3de726 |
2662 | static void new_game_size(frontend *fe, float scale) |
077f3cbe |
2663 | { |
2664 | RECT r, sr; |
077f3cbe |
2665 | int x, y; |
2666 | |
35ee2001 |
2667 | get_max_puzzle_size(fe, &x, &y); |
1e3e152d |
2668 | midend_size(fe->me, &x, &y, FALSE); |
dc3de726 |
2669 | |
2670 | if (scale != 1.0) { |
2671 | x = (int)((float)x * fe->puzz_scale); |
2672 | y = (int)((float)y * fe->puzz_scale); |
2673 | midend_size(fe->me, &x, &y, TRUE); |
2674 | } |
6b12623f |
2675 | fe->ymin = (fe->xmin * y) / x; |
077f3cbe |
2676 | |
2677 | r.left = r.top = 0; |
2678 | r.right = x; |
2679 | r.bottom = y; |
35ee2001 |
2680 | AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0); |
077f3cbe |
2681 | |
2682 | if (fe->statusbar != NULL) { |
2683 | GetWindowRect(fe->statusbar, &sr); |
2684 | } else { |
2685 | sr.left = sr.right = sr.top = sr.bottom = 0; |
2686 | } |
cb0c7d4a |
2687 | #ifndef _WIN32_WCE |
077f3cbe |
2688 | SetWindowPos(fe->hwnd, NULL, 0, 0, |
2689 | r.right - r.left, |
2690 | r.bottom - r.top + sr.bottom - sr.top, |
2691 | SWP_NOMOVE | SWP_NOZORDER); |
cb0c7d4a |
2692 | #endif |
1e3e152d |
2693 | |
2694 | check_window_size(fe, &x, &y); |
2695 | |
cb0c7d4a |
2696 | #ifndef _WIN32_WCE |
077f3cbe |
2697 | if (fe->statusbar != NULL) |
2698 | SetWindowPos(fe->statusbar, NULL, 0, y, x, |
2699 | sr.bottom - sr.top, SWP_NOZORDER); |
cb0c7d4a |
2700 | #endif |
077f3cbe |
2701 | |
6b12623f |
2702 | new_bitmap(fe, x, y); |
077f3cbe |
2703 | |
cb0c7d4a |
2704 | #ifdef _WIN32_WCE |
2705 | InvalidateRect(fe->hwnd, NULL, TRUE); |
2706 | #endif |
077f3cbe |
2707 | midend_redraw(fe->me); |
2708 | } |
2709 | |
6b12623f |
2710 | /* |
2711 | * Given a proposed new window rect, work out the resulting |
2712 | * difference in client size (from current), and use to try |
2713 | * and resize the puzzle, returning (wx,wy) as the actual |
2714 | * new window size. |
2715 | */ |
2716 | |
2717 | static void adjust_game_size(frontend *fe, RECT *proposed, int isedge, |
2718 | int *wx_r, int *wy_r) |
2719 | { |
2720 | RECT cr, wr; |
2721 | int nx, ny, xdiff, ydiff, wx, wy; |
2722 | |
2723 | /* Work out the current window sizing, and thus the |
2724 | * difference in size we're asking for. */ |
2725 | GetClientRect(fe->hwnd, &cr); |
2726 | wr = cr; |
2727 | AdjustWindowRectEx(&wr, WINFLAGS, TRUE, 0); |
2728 | |
2729 | xdiff = (proposed->right - proposed->left) - (wr.right - wr.left); |
2730 | ydiff = (proposed->bottom - proposed->top) - (wr.bottom - wr.top); |
2731 | |
2732 | if (isedge) { |
2733 | /* These next four lines work around the fact that midend_size |
2734 | * is happy to shrink _but not grow_ if you change one dimension |
2735 | * but not the other. */ |
2736 | if (xdiff > 0 && ydiff == 0) |
2737 | ydiff = (xdiff * (wr.right - wr.left)) / (wr.bottom - wr.top); |
2738 | if (xdiff == 0 && ydiff > 0) |
2739 | xdiff = (ydiff * (wr.bottom - wr.top)) / (wr.right - wr.left); |
2740 | } |
2741 | |
2742 | if (check_window_resize(fe, |
2743 | (cr.right - cr.left) + xdiff, |
2744 | (cr.bottom - cr.top) + ydiff, |
dc3de726 |
2745 | &nx, &ny, &wx, &wy)) { |
6b12623f |
2746 | new_bitmap(fe, nx, ny); |
2747 | midend_force_redraw(fe->me); |
2748 | } else { |
2749 | /* reset size to current window size */ |
2750 | wx = wr.right - wr.left; |
2751 | wy = wr.bottom - wr.top; |
2752 | } |
2753 | /* Re-fetch rectangle; size limits mean we might not have |
2754 | * taken it quite to the mouse drag positions. */ |
2755 | GetClientRect(fe->hwnd, &cr); |
2756 | adjust_statusbar(fe, &cr); |
2757 | |
2758 | *wx_r = wx; *wy_r = wy; |
2759 | } |
2760 | |
64e360d3 |
2761 | static void update_type_menu_tick(frontend *fe) |
2762 | { |
2763 | int total, n, i; |
2764 | |
2765 | if (fe->typemenu == INVALID_HANDLE_VALUE) |
2766 | return; |
2767 | |
2768 | total = GetMenuItemCount(fe->typemenu); |
2769 | n = midend_which_preset(fe->me); |
2770 | if (n < 0) |
2771 | n = total - 1; /* "Custom" item */ |
2772 | |
2773 | for (i = 0; i < total; i++) { |
2774 | int flag = (i == n ? MF_CHECKED : MF_UNCHECKED); |
2775 | CheckMenuItem(fe->typemenu, i, MF_BYPOSITION | flag); |
2776 | } |
2777 | |
2778 | DrawMenuBar(fe->hwnd); |
2779 | } |
2780 | |
fa3abef5 |
2781 | static void update_copy_menu_greying(frontend *fe) |
2782 | { |
2783 | UINT enable = (midend_can_format_as_text_now(fe->me) ? |
2784 | MF_ENABLED : MF_GRAYED); |
2785 | EnableMenuItem(fe->gamemenu, IDM_COPY, MF_BYCOMMAND | enable); |
2786 | } |
2787 | |
668be019 |
2788 | static void new_game_type(frontend *fe) |
2789 | { |
2790 | midend_new_game(fe->me); |
dc3de726 |
2791 | new_game_size(fe, 1.0); |
64e360d3 |
2792 | update_type_menu_tick(fe); |
fa3abef5 |
2793 | update_copy_menu_greying(fe); |
668be019 |
2794 | } |
2795 | |
97c2790f |
2796 | static int is_alt_pressed(void) |
2797 | { |
2798 | BYTE keystate[256]; |
2799 | int r = GetKeyboardState(keystate); |
2800 | if (!r) |
2801 | return FALSE; |
2802 | if (keystate[VK_MENU] & 0x80) |
2803 | return TRUE; |
2804 | if (keystate[VK_RMENU] & 0x80) |
2805 | return TRUE; |
2806 | return FALSE; |
2807 | } |
2808 | |
668be019 |
2809 | static void savefile_write(void *wctx, void *buf, int len) |
2810 | { |
2811 | FILE *fp = (FILE *)wctx; |
2812 | fwrite(buf, 1, len, fp); |
2813 | } |
2814 | |
2815 | static int savefile_read(void *wctx, void *buf, int len) |
2816 | { |
2817 | FILE *fp = (FILE *)wctx; |
2818 | int ret; |
2819 | |
2820 | ret = fread(buf, 1, len, fp); |
2821 | return (ret == len); |
2822 | } |
2823 | |
4e7ef6e6 |
2824 | static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, |
2825 | WPARAM wParam, LPARAM lParam) |
2826 | { |
2827 | frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA); |
668be019 |
2828 | int cmd; |
4e7ef6e6 |
2829 | |
2830 | switch (message) { |
2831 | case WM_CLOSE: |
2832 | DestroyWindow(hwnd); |
2833 | return 0; |
eb2ad6f1 |
2834 | case WM_COMMAND: |
cb0c7d4a |
2835 | #ifdef _WIN32_WCE |
2836 | /* Numeric pad sends WM_COMMAND messages */ |
2837 | if ((wParam >= IDM_KEYEMUL) && (wParam < IDM_KEYEMUL + 256)) |
2838 | { |
2839 | midend_process_key(fe->me, 0, 0, wParam - IDM_KEYEMUL); |
2840 | } |
2841 | #endif |
668be019 |
2842 | cmd = wParam & ~0xF; /* low 4 bits reserved to Windows */ |
2843 | switch (cmd) { |
eb2ad6f1 |
2844 | case IDM_NEW: |
2845 | if (!midend_process_key(fe->me, 0, 0, 'n')) |
2846 | PostQuitMessage(0); |
2847 | break; |
2848 | case IDM_RESTART: |
7f89707c |
2849 | midend_restart_game(fe->me); |
eb2ad6f1 |
2850 | break; |
2851 | case IDM_UNDO: |
2852 | if (!midend_process_key(fe->me, 0, 0, 'u')) |
2853 | PostQuitMessage(0); |
2854 | break; |
2855 | case IDM_REDO: |
2856 | if (!midend_process_key(fe->me, 0, 0, '\x12')) |
2857 | PostQuitMessage(0); |
2858 | break; |
1cdfcdbd |
2859 | case IDM_COPY: |
2860 | { |
2861 | char *text = midend_text_format(fe->me); |
2862 | if (text) |
2863 | write_clip(hwnd, text); |
2864 | else |
2865 | MessageBeep(MB_ICONWARNING); |
6d5cbf79 |
2866 | sfree(text); |
1cdfcdbd |
2867 | } |
2868 | break; |
2ac6d24e |
2869 | case IDM_SOLVE: |
2870 | { |
2871 | char *msg = midend_solve(fe->me); |
2872 | if (msg) |
2873 | MessageBox(hwnd, msg, "Unable to solve", |
2874 | MB_ICONERROR | MB_OK); |
2875 | } |
2876 | break; |
eb2ad6f1 |
2877 | case IDM_QUIT: |
2878 | if (!midend_process_key(fe->me, 0, 0, 'q')) |
2879 | PostQuitMessage(0); |
2880 | break; |
077f3cbe |
2881 | case IDM_CONFIG: |
5928817c |
2882 | if (get_config(fe, CFG_SETTINGS)) |
2883 | new_game_type(fe); |
2884 | break; |
2885 | case IDM_SEED: |
2886 | if (get_config(fe, CFG_SEED)) |
077f3cbe |
2887 | new_game_type(fe); |
2888 | break; |
1185e3c5 |
2889 | case IDM_DESC: |
2890 | if (get_config(fe, CFG_DESC)) |
2891 | new_game_type(fe); |
2892 | break; |
821ab2c6 |
2893 | case IDM_PRINT: |
2894 | if (get_config(fe, CFG_PRINT)) |
2895 | print(fe); |
2896 | break; |
97098757 |
2897 | case IDM_ABOUT: |
2898 | about(fe); |
2899 | break; |
668be019 |
2900 | case IDM_LOAD: |
2901 | case IDM_SAVE: |
2902 | { |
2903 | OPENFILENAME of; |
2904 | char filename[FILENAME_MAX]; |
2905 | int ret; |
2906 | |
2907 | memset(&of, 0, sizeof(of)); |
2908 | of.hwndOwner = hwnd; |
2909 | of.lpstrFilter = "All Files (*.*)\0*\0\0\0"; |
2910 | of.lpstrCustomFilter = NULL; |
2911 | of.nFilterIndex = 1; |
2912 | of.lpstrFile = filename; |
2913 | filename[0] = '\0'; |
2914 | of.nMaxFile = lenof(filename); |
2915 | of.lpstrFileTitle = NULL; |
2916 | of.lpstrTitle = (cmd == IDM_SAVE ? |
2917 | "Enter name of game file to save" : |
2918 | "Enter name of saved game file to load"); |
2919 | of.Flags = 0; |
2920 | #ifdef OPENFILENAME_SIZE_VERSION_400 |
2921 | of.lStructSize = OPENFILENAME_SIZE_VERSION_400; |
2922 | #else |
2923 | of.lStructSize = sizeof(of); |
2924 | #endif |
2925 | of.lpstrInitialDir = NULL; |
2926 | |
2927 | if (cmd == IDM_SAVE) |
2928 | ret = GetSaveFileName(&of); |
2929 | else |
2930 | ret = GetOpenFileName(&of); |
2931 | |
2932 | if (ret) { |
2933 | if (cmd == IDM_SAVE) { |
2934 | FILE *fp; |
2935 | |
2936 | if ((fp = fopen(filename, "r")) != NULL) { |
2937 | char buf[256 + FILENAME_MAX]; |
2938 | fclose(fp); |
2939 | /* file exists */ |
2940 | |
2941 | sprintf(buf, "Are you sure you want to overwrite" |
2942 | " the file \"%.*s\"?", |
2943 | FILENAME_MAX, filename); |
2944 | if (MessageBox(hwnd, buf, "Question", |
2945 | MB_YESNO | MB_ICONQUESTION) |
2946 | != IDYES) |
2947 | break; |
2948 | } |
2949 | |
2950 | fp = fopen(filename, "w"); |
2951 | |
2952 | if (!fp) { |
2953 | MessageBox(hwnd, "Unable to open save file", |
2954 | "Error", MB_ICONERROR | MB_OK); |
2955 | break; |
2956 | } |
2957 | |
2958 | midend_serialise(fe->me, savefile_write, fp); |
2959 | |
2960 | fclose(fp); |
2961 | } else { |
2962 | FILE *fp = fopen(filename, "r"); |
2963 | char *err; |
2964 | |
2965 | if (!fp) { |
2966 | MessageBox(hwnd, "Unable to open saved game file", |
2967 | "Error", MB_ICONERROR | MB_OK); |
2968 | break; |
2969 | } |
2970 | |
2971 | err = midend_deserialise(fe->me, savefile_read, fp); |
2972 | |
2973 | fclose(fp); |
2974 | |
2975 | if (err) { |
2976 | MessageBox(hwnd, err, "Error", MB_ICONERROR|MB_OK); |
2977 | break; |
2978 | } |
2979 | |
dc3de726 |
2980 | new_game_size(fe, 1.0); |
668be019 |
2981 | } |
2982 | } |
2983 | } |
2984 | |
2985 | break; |
cb0c7d4a |
2986 | #ifndef _WIN32_WCE |
e91825f8 |
2987 | case IDM_HELPC: |
750037d7 |
2988 | start_help(fe, NULL); |
2989 | break; |
e91825f8 |
2990 | case IDM_GAMEHELP: |
dc3de726 |
2991 | assert(help_type != NONE); |
2992 | start_help(fe, help_type == CHM ? |
2993 | fe->game->htmlhelp_topic : fe->game->winhelp_topic); |
e91825f8 |
2994 | break; |
cb0c7d4a |
2995 | #endif |
eb2ad6f1 |
2996 | default: |
dc3de726 |
2997 | #ifdef COMBINED |
2998 | if (wParam >= IDM_GAMES && wParam < (IDM_GAMES + (WPARAM)gamecount)) { |
2999 | int p = wParam - IDM_GAMES; |
3000 | char *error; |
3001 | |
3002 | new_game(fe, gamelist[p], NULL, &error); |
3003 | } else |
3004 | #endif |
eb2ad6f1 |
3005 | { |
3006 | int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10; |
3007 | |
3008 | if (p >= 0 && p < fe->npresets) { |
eb2ad6f1 |
3009 | midend_set_params(fe->me, fe->presets[p]); |
077f3cbe |
3010 | new_game_type(fe); |
eb2ad6f1 |
3011 | } |
3012 | } |
3013 | break; |
3014 | } |
3015 | break; |
4e7ef6e6 |
3016 | case WM_DESTROY: |
cb0c7d4a |
3017 | #ifndef _WIN32_WCE |
750037d7 |
3018 | stop_help(fe); |
cb0c7d4a |
3019 | #endif |
dc3de726 |
3020 | frontend_free(fe); |
3021 | PostQuitMessage(0); |
4e7ef6e6 |
3022 | return 0; |
3023 | case WM_PAINT: |
3024 | { |
3025 | PAINTSTRUCT p; |
3026 | HDC hdc, hdc2; |
3027 | HBITMAP prevbm; |
cb0c7d4a |
3028 | RECT rcDest; |
4e7ef6e6 |
3029 | |
3030 | hdc = BeginPaint(hwnd, &p); |
3031 | hdc2 = CreateCompatibleDC(hdc); |
3032 | prevbm = SelectObject(hdc2, fe->bitmap); |
cb0c7d4a |
3033 | #ifdef _WIN32_WCE |
3034 | FillRect(hdc, &(p.rcPaint), (HBRUSH) GetStockObject(WHITE_BRUSH)); |
3035 | #endif |
3036 | IntersectRect(&rcDest, &(fe->bitmapPosition), &(p.rcPaint)); |
4e7ef6e6 |
3037 | BitBlt(hdc, |
cb0c7d4a |
3038 | rcDest.left, rcDest.top, |
3039 | rcDest.right - rcDest.left, |
3040 | rcDest.bottom - rcDest.top, |
4e7ef6e6 |
3041 | hdc2, |
cb0c7d4a |
3042 | rcDest.left - fe->bitmapPosition.left, |
3043 | rcDest.top - fe->bitmapPosition.top, |
4e7ef6e6 |
3044 | SRCCOPY); |
3045 | SelectObject(hdc2, prevbm); |
3046 | DeleteDC(hdc2); |
3047 | EndPaint(hwnd, &p); |
3048 | } |
3049 | return 0; |
3050 | case WM_KEYDOWN: |
3051 | { |
3052 | int key = -1; |
f0ee053c |
3053 | BYTE keystate[256]; |
3054 | int r = GetKeyboardState(keystate); |
3055 | int shift = (r && (keystate[VK_SHIFT] & 0x80)) ? MOD_SHFT : 0; |
3056 | int ctrl = (r && (keystate[VK_CONTROL] & 0x80)) ? MOD_CTRL : 0; |
4e7ef6e6 |
3057 | |
3058 | switch (wParam) { |
3c833d45 |
3059 | case VK_LEFT: |
3060 | if (!(lParam & 0x01000000)) |
3061 | key = MOD_NUM_KEYPAD | '4'; |
f0ee053c |
3062 | else |
3063 | key = shift | ctrl | CURSOR_LEFT; |
3c833d45 |
3064 | break; |
3065 | case VK_RIGHT: |
3066 | if (!(lParam & 0x01000000)) |
3067 | key = MOD_NUM_KEYPAD | '6'; |
f0ee053c |
3068 | else |
3069 | key = shift | ctrl | CURSOR_RIGHT; |
3c833d45 |
3070 | break; |
3071 | case VK_UP: |
3072 | if (!(lParam & 0x01000000)) |
3073 | key = MOD_NUM_KEYPAD | '8'; |
f0ee053c |
3074 | else |
3075 | key = shift | ctrl | CURSOR_UP; |
3c833d45 |
3076 | break; |
3077 | case VK_DOWN: |
3078 | if (!(lParam & 0x01000000)) |
3079 | key = MOD_NUM_KEYPAD | '2'; |
f0ee053c |
3080 | else |
3081 | key = shift | ctrl | CURSOR_DOWN; |
3c833d45 |
3082 | break; |
c71454c0 |
3083 | /* |
3084 | * Diagonal keys on the numeric keypad. |
3085 | */ |
3086 | case VK_PRIOR: |
3c833d45 |
3087 | if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '9'; |
c71454c0 |
3088 | break; |
3089 | case VK_NEXT: |
3c833d45 |
3090 | if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '3'; |
c71454c0 |
3091 | break; |
3092 | case VK_HOME: |
3c833d45 |
3093 | if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '7'; |
c71454c0 |
3094 | break; |
3095 | case VK_END: |
3c833d45 |
3096 | if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '1'; |
3097 | break; |
3098 | case VK_INSERT: |
3099 | if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '0'; |
3100 | break; |
3101 | case VK_CLEAR: |
3102 | if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '5'; |
c71454c0 |
3103 | break; |
3104 | /* |
3105 | * Numeric keypad keys with Num Lock on. |
3106 | */ |
3c833d45 |
3107 | case VK_NUMPAD4: key = MOD_NUM_KEYPAD | '4'; break; |
3108 | case VK_NUMPAD6: key = MOD_NUM_KEYPAD | '6'; break; |
3109 | case VK_NUMPAD8: key = MOD_NUM_KEYPAD | '8'; break; |
3110 | case VK_NUMPAD2: key = MOD_NUM_KEYPAD | '2'; break; |
3111 | case VK_NUMPAD5: key = MOD_NUM_KEYPAD | '5'; break; |
3112 | case VK_NUMPAD9: key = MOD_NUM_KEYPAD | '9'; break; |
3113 | case VK_NUMPAD3: key = MOD_NUM_KEYPAD | '3'; break; |
3114 | case VK_NUMPAD7: key = MOD_NUM_KEYPAD | '7'; break; |
3115 | case VK_NUMPAD1: key = MOD_NUM_KEYPAD | '1'; break; |
3116 | case VK_NUMPAD0: key = MOD_NUM_KEYPAD | '0'; break; |
4e7ef6e6 |
3117 | } |
3118 | |
3119 | if (key != -1) { |
eb2ad6f1 |
3120 | if (!midend_process_key(fe->me, 0, 0, key)) |
4e7ef6e6 |
3121 | PostQuitMessage(0); |
c71454c0 |
3122 | } else { |
3123 | MSG m; |
3124 | m.hwnd = hwnd; |
3125 | m.message = WM_KEYDOWN; |
3126 | m.wParam = wParam; |
3127 | m.lParam = lParam & 0xdfff; |
3128 | TranslateMessage(&m); |
4e7ef6e6 |
3129 | } |
3130 | } |
3131 | break; |
3132 | case WM_LBUTTONDOWN: |
3133 | case WM_RBUTTONDOWN: |
3134 | case WM_MBUTTONDOWN: |
b0f06719 |
3135 | { |
3136 | int button; |
3137 | |
3138 | /* |
3139 | * Shift-clicks count as middle-clicks, since otherwise |
3140 | * two-button Windows users won't have any kind of |
3141 | * middle click to use. |
3142 | */ |
3143 | if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT)) |
3144 | button = MIDDLE_BUTTON; |
97c2790f |
3145 | else if (message == WM_RBUTTONDOWN || is_alt_pressed()) |
b0f06719 |
3146 | button = RIGHT_BUTTON; |
97c2790f |
3147 | else |
cb0c7d4a |
3148 | #ifndef _WIN32_WCE |
97c2790f |
3149 | button = LEFT_BUTTON; |
cb0c7d4a |
3150 | #else |
dc3de726 |
3151 | if ((fe->game->flags & REQUIRE_RBUTTON) == 0) |
cb0c7d4a |
3152 | button = LEFT_BUTTON; |
3153 | else |
3154 | { |
3155 | SHRGINFO shrgi; |
3156 | |
3157 | shrgi.cbSize = sizeof(SHRGINFO); |
3158 | shrgi.hwndClient = hwnd; |
3159 | shrgi.ptDown.x = (signed short)LOWORD(lParam); |
3160 | shrgi.ptDown.y = (signed short)HIWORD(lParam); |
3161 | shrgi.dwFlags = SHRG_RETURNCMD; |
3162 | |
3163 | if (GN_CONTEXTMENU == SHRecognizeGesture(&shrgi)) |
3164 | button = RIGHT_BUTTON; |
3165 | else |
3166 | button = LEFT_BUTTON; |
3167 | } |
3168 | #endif |
74a4e547 |
3169 | |
cb0c7d4a |
3170 | if (!midend_process_key(fe->me, |
3171 | (signed short)LOWORD(lParam) - fe->bitmapPosition.left, |
3172 | (signed short)HIWORD(lParam) - fe->bitmapPosition.top, |
3173 | button)) |
74a4e547 |
3174 | PostQuitMessage(0); |
3175 | |
3176 | SetCapture(hwnd); |
3177 | } |
3178 | break; |
3179 | case WM_LBUTTONUP: |
3180 | case WM_RBUTTONUP: |
3181 | case WM_MBUTTONUP: |
3182 | { |
3183 | int button; |
3184 | |
3185 | /* |
3186 | * Shift-clicks count as middle-clicks, since otherwise |
3187 | * two-button Windows users won't have any kind of |
3188 | * middle click to use. |
3189 | */ |
3190 | if (message == WM_MBUTTONUP || (wParam & MK_SHIFT)) |
3191 | button = MIDDLE_RELEASE; |
97c2790f |
3192 | else if (message == WM_RBUTTONUP || is_alt_pressed()) |
74a4e547 |
3193 | button = RIGHT_RELEASE; |
97c2790f |
3194 | else |
3195 | button = LEFT_RELEASE; |
74a4e547 |
3196 | |
cb0c7d4a |
3197 | if (!midend_process_key(fe->me, |
3198 | (signed short)LOWORD(lParam) - fe->bitmapPosition.left, |
3199 | (signed short)HIWORD(lParam) - fe->bitmapPosition.top, |
3200 | button)) |
74a4e547 |
3201 | PostQuitMessage(0); |
3202 | |
3203 | ReleaseCapture(); |
3204 | } |
3205 | break; |
3206 | case WM_MOUSEMOVE: |
3207 | { |
3208 | int button; |
3209 | |
3210 | if (wParam & (MK_MBUTTON | MK_SHIFT)) |
3211 | button = MIDDLE_DRAG; |
97c2790f |
3212 | else if (wParam & MK_RBUTTON || is_alt_pressed()) |
74a4e547 |
3213 | button = RIGHT_DRAG; |
97c2790f |
3214 | else |
3215 | button = LEFT_DRAG; |
74a4e547 |
3216 | |
cb0c7d4a |
3217 | if (!midend_process_key(fe->me, |
3218 | (signed short)LOWORD(lParam) - fe->bitmapPosition.left, |
3219 | (signed short)HIWORD(lParam) - fe->bitmapPosition.top, |
3220 | button)) |
b0f06719 |
3221 | PostQuitMessage(0); |
3222 | } |
4e7ef6e6 |
3223 | break; |
3224 | case WM_CHAR: |
eb2ad6f1 |
3225 | if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam)) |
4e7ef6e6 |
3226 | PostQuitMessage(0); |
3227 | return 0; |
3228 | case WM_TIMER: |
8c1fd974 |
3229 | if (fe->timer) { |
3230 | DWORD now = GetTickCount(); |
3231 | float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F; |
3232 | midend_timer(fe->me, elapsed); |
3233 | fe->timer_last_tickcount = now; |
3234 | } |
4e7ef6e6 |
3235 | return 0; |
6b12623f |
3236 | #ifndef _WIN32_WCE |
3237 | case WM_SIZING: |
3238 | { |
3239 | RECT *sr = (RECT *)lParam; |
3240 | int wx, wy, isedge = 0; |
3241 | |
3242 | if (wParam == WMSZ_TOP || |
3243 | wParam == WMSZ_RIGHT || |
3244 | wParam == WMSZ_BOTTOM || |
3245 | wParam == WMSZ_LEFT) isedge = 1; |
3246 | adjust_game_size(fe, sr, isedge, &wx, &wy); |
3247 | |
3248 | /* Given the window size the puzzles constrain |
3249 | * us to, work out which edge we should be moving. */ |
3250 | if (wParam == WMSZ_TOP || |
3251 | wParam == WMSZ_TOPLEFT || |
3252 | wParam == WMSZ_TOPRIGHT) { |
3253 | sr->top = sr->bottom - wy; |
3254 | } else { |
3255 | sr->bottom = sr->top + wy; |
3256 | } |
3257 | if (wParam == WMSZ_LEFT || |
3258 | wParam == WMSZ_TOPLEFT || |
3259 | wParam == WMSZ_BOTTOMLEFT) { |
3260 | sr->left = sr->right - wx; |
3261 | } else { |
3262 | sr->right = sr->left + wx; |
3263 | } |
3264 | return TRUE; |
3265 | } |
3266 | break; |
3267 | #endif |
4e7ef6e6 |
3268 | } |
3269 | |
3270 | return DefWindowProc(hwnd, message, wParam, lParam); |
3271 | } |
3272 | |
cb0c7d4a |
3273 | #ifdef _WIN32_WCE |
3274 | static int FindPreviousInstance() |
3275 | { |
3276 | /* Check if application is running. If it's running then focus on the window */ |
3277 | HWND hOtherWnd = NULL; |
3278 | |
3279 | hOtherWnd = FindWindow (wGameName, wGameName); |
3280 | if (hOtherWnd) |
3281 | { |
3282 | SetForegroundWindow (hOtherWnd); |
3283 | return TRUE; |
3284 | } |
3285 | |
3286 | return FALSE; |
3287 | } |
3288 | #endif |
3289 | |
4e7ef6e6 |
3290 | int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) |
3291 | { |
3292 | MSG msg; |
8b7938e7 |
3293 | char *error; |
dc3de726 |
3294 | const game *gg; |
3295 | frontend *fe; |
4e7ef6e6 |
3296 | |
cb0c7d4a |
3297 | #ifdef _WIN32_WCE |
dc3de726 |
3298 | MultiByteToWideChar (CP_ACP, 0, CLASSNAME, -1, wClassName, 256); |
cb0c7d4a |
3299 | if (FindPreviousInstance ()) |
3300 | return 0; |
3301 | #endif |
3302 | |
fd1a1a2b |
3303 | InitCommonControls(); |
3304 | |
4e7ef6e6 |
3305 | if (!prev) { |
3306 | WNDCLASS wndclass; |
3307 | |
3308 | wndclass.style = 0; |
3309 | wndclass.lpfnWndProc = WndProc; |
3310 | wndclass.cbClsExtra = 0; |
3311 | wndclass.cbWndExtra = 0; |
3312 | wndclass.hInstance = inst; |
fa2cb13d |
3313 | wndclass.hIcon = LoadIcon(inst, MAKEINTRESOURCE(200)); |
cb0c7d4a |
3314 | #ifndef _WIN32_WCE |
fa2cb13d |
3315 | if (!wndclass.hIcon) /* in case resource file is absent */ |
3316 | wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION); |
cb0c7d4a |
3317 | #endif |
4e7ef6e6 |
3318 | wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); |
3319 | wndclass.hbrBackground = NULL; |
3320 | wndclass.lpszMenuName = NULL; |
cb0c7d4a |
3321 | #ifdef _WIN32_WCE |
dc3de726 |
3322 | wndclass.lpszClassName = wClassName; |
cb0c7d4a |
3323 | #else |
dc3de726 |
3324 | wndclass.lpszClassName = CLASSNAME; |
cb0c7d4a |
3325 | #endif |
4e7ef6e6 |
3326 | |
3327 | RegisterClass(&wndclass); |
3328 | } |
3329 | |
89167dad |
3330 | while (*cmdline && isspace((unsigned char)*cmdline)) |
8b7938e7 |
3331 | cmdline++; |
3332 | |
750037d7 |
3333 | init_help(); |
3334 | |
dc3de726 |
3335 | #ifdef COMBINED |
3336 | gg = gamelist[0]; |
3337 | { |
3338 | int i; |
3339 | for (i = 0; i < gamecount; i++) { |
3340 | const char *p = gamelist[i]->name; |
3341 | char *q = cmdline; |
3342 | while (*q && isspace((unsigned char)*q)) |
3343 | q++; |
3344 | while (*p && *q) { |
3345 | if (isspace((unsigned char)*p)) { |
3346 | while (*q && isspace((unsigned char)*q)) |
3347 | q++; |
3348 | } else { |
3349 | if (tolower((unsigned char)*p) != |
3350 | tolower((unsigned char)*q)) |
3351 | break; |
3352 | q++; |
3353 | } |
3354 | p++; |
3355 | } |
3356 | if (!*p) { |
3357 | gg = gamelist[i]; |
3358 | cmdline = q; |
3359 | if (*cmdline) { |
3360 | while (*cmdline && isspace((unsigned char)*cmdline)) |
3361 | cmdline++; |
3362 | } |
3363 | } |
3364 | } |
3365 | } |
3366 | #else |
3367 | gg = &thegame; |
3368 | #endif |
3369 | |
3370 | fe = frontend_new(inst); |
3371 | if (new_game(fe, gg, *cmdline ? cmdline : NULL, &error)) { |
8b7938e7 |
3372 | char buf[128]; |
dc3de726 |
3373 | sprintf(buf, "%.100s Error", gg->name); |
8b7938e7 |
3374 | MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR); |
3375 | return 1; |
3376 | } |
dc3de726 |
3377 | show_window(fe); |
4e7ef6e6 |
3378 | |
3379 | while (GetMessage(&msg, NULL, 0, 0)) { |
4e7ef6e6 |
3380 | DispatchMessage(&msg); |
3381 | } |
3382 | |
750037d7 |
3383 | cleanup_help(); |
3384 | |
4e7ef6e6 |
3385 | return msg.wParam; |
3386 | } |
dc3de726 |
3387 | /* vim: set shiftwidth=4 tabstop=8: */ |