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