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