Simplify clip region handling under Windows, which also makes Solo's
[sgt/puzzles] / windows.c
1 /*
2 * windows.c: Windows front end for my puzzle collection.
3 *
4 * TODO:
5 *
6 * - Figure out what to do if a puzzle requests a size bigger than
7 * the screen will take. In principle we could put scrollbars in
8 * the window, although that would be pretty horrid. Another
9 * option is to detect in advance that this will be a problem -
10 * we can probably tell this using midend_size() before actually
11 * generating the puzzle - and simply refuse to do it.
12 */
13
14 #include <windows.h>
15 #include <commctrl.h>
16
17 #include <stdio.h>
18 #include <assert.h>
19 #include <ctype.h>
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <time.h>
23
24 #include "puzzles.h"
25
26 #define IDM_NEW 0x0010
27 #define IDM_RESTART 0x0020
28 #define IDM_UNDO 0x0030
29 #define IDM_REDO 0x0040
30 #define IDM_QUIT 0x0050
31 #define IDM_CONFIG 0x0060
32 #define IDM_SEED 0x0070
33 #define IDM_HELPC 0x0080
34 #define IDM_GAMEHELP 0x0090
35 #define IDM_PRESETS 0x0100
36
37 #define HELP_FILE_NAME "puzzles.hlp"
38 #define HELP_CNT_NAME "puzzles.cnt"
39
40 #ifdef DEBUG
41 static FILE *debug_fp = NULL;
42 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
43 static int debug_got_console = 0;
44
45 void dputs(char *buf)
46 {
47 DWORD dw;
48
49 if (!debug_got_console) {
50 if (AllocConsole()) {
51 debug_got_console = 1;
52 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
53 }
54 }
55 if (!debug_fp) {
56 debug_fp = fopen("debug.log", "w");
57 }
58
59 if (debug_hdl != INVALID_HANDLE_VALUE) {
60 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
61 }
62 fputs(buf, debug_fp);
63 fflush(debug_fp);
64 }
65
66 void debug_printf(char *fmt, ...)
67 {
68 char buf[4096];
69 va_list ap;
70
71 va_start(ap, fmt);
72 vsprintf(buf, fmt, ap);
73 dputs(buf);
74 va_end(ap);
75 }
76
77 #define debug(x) (debug_printf x)
78
79 #else
80
81 #define debug(x)
82
83 #endif
84
85 struct font {
86 HFONT font;
87 int type;
88 int size;
89 };
90
91 struct cfg_aux {
92 int ctlid;
93 };
94
95 struct frontend {
96 midend_data *me;
97 HWND hwnd, statusbar, cfgbox;
98 HINSTANCE inst;
99 HBITMAP bitmap, prevbm;
100 HDC hdc_bm;
101 COLORREF *colours;
102 HBRUSH *brushes;
103 HPEN *pens;
104 HRGN clip;
105 UINT timer;
106 DWORD timer_last_tickcount;
107 int npresets;
108 game_params **presets;
109 struct font *fonts;
110 int nfonts, fontsize;
111 config_item *cfg;
112 struct cfg_aux *cfgaux;
113 int cfg_which, cfg_done;
114 HFONT cfgfont;
115 char *help_path;
116 int help_has_contents;
117 };
118
119 void fatal(char *fmt, ...)
120 {
121 char buf[2048];
122 va_list ap;
123
124 va_start(ap, fmt);
125 vsprintf(buf, fmt, ap);
126 va_end(ap);
127
128 MessageBox(NULL, buf, "Fatal error", MB_ICONEXCLAMATION | MB_OK);
129
130 exit(1);
131 }
132
133 void get_random_seed(void **randseed, int *randseedsize)
134 {
135 time_t *tp = snew(time_t);
136 time(tp);
137 *randseed = (void *)tp;
138 *randseedsize = sizeof(time_t);
139 }
140
141 void status_bar(frontend *fe, char *text)
142 {
143 SetWindowText(fe->statusbar, text);
144 }
145
146 void frontend_default_colour(frontend *fe, float *output)
147 {
148 DWORD c = GetSysColor(COLOR_MENU); /* ick */
149
150 output[0] = (float)(GetRValue(c) / 255.0);
151 output[1] = (float)(GetGValue(c) / 255.0);
152 output[2] = (float)(GetBValue(c) / 255.0);
153 }
154
155 void clip(frontend *fe, int x, int y, int w, int h)
156 {
157 IntersectClipRect(fe->hdc_bm, x, y, x+w, y+h);
158 }
159
160 void unclip(frontend *fe)
161 {
162 SelectClipRgn(fe->hdc_bm, NULL);
163 }
164
165 void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize,
166 int align, int colour, char *text)
167 {
168 int i;
169
170 /*
171 * Find or create the font.
172 */
173 for (i = 0; i < fe->nfonts; i++)
174 if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
175 break;
176
177 if (i == fe->nfonts) {
178 if (fe->fontsize <= fe->nfonts) {
179 fe->fontsize = fe->nfonts + 10;
180 fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
181 }
182
183 fe->nfonts++;
184
185 fe->fonts[i].type = fonttype;
186 fe->fonts[i].size = fontsize;
187
188 /*
189 * FIXME: Really I should make at least _some_ effort to
190 * pick the correct font.
191 */
192 fe->fonts[i].font = CreateFont(-fontsize, 0, 0, 0, 0,
193 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
194 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
195 DEFAULT_QUALITY,
196 (fonttype == FONT_FIXED ?
197 FIXED_PITCH | FF_DONTCARE :
198 VARIABLE_PITCH | FF_SWISS),
199 NULL);
200 }
201
202 /*
203 * Position and draw the text.
204 */
205 {
206 HFONT oldfont;
207 TEXTMETRIC tm;
208 SIZE size;
209
210 oldfont = SelectObject(fe->hdc_bm, fe->fonts[i].font);
211 if (GetTextMetrics(fe->hdc_bm, &tm)) {
212 if (align & ALIGN_VCENTRE)
213 y -= (tm.tmAscent+tm.tmDescent)/2;
214 else
215 y -= tm.tmAscent;
216 }
217 if (GetTextExtentPoint32(fe->hdc_bm, text, strlen(text), &size)) {
218 if (align & ALIGN_HCENTRE)
219 x -= size.cx / 2;
220 else if (align & ALIGN_HRIGHT)
221 x -= size.cx;
222 }
223 SetBkMode(fe->hdc_bm, TRANSPARENT);
224 SetTextColor(fe->hdc_bm, fe->colours[colour]);
225 TextOut(fe->hdc_bm, x, y, text, strlen(text));
226 SelectObject(fe->hdc_bm, oldfont);
227 }
228 }
229
230 void draw_rect(frontend *fe, int x, int y, int w, int h, int colour)
231 {
232 if (w == 1 && h == 1) {
233 /*
234 * Rectangle() appears to get uppity if asked to draw a 1x1
235 * rectangle, presumably on the grounds that that's beneath
236 * its dignity and you ought to be using SetPixel instead.
237 * So I will.
238 */
239 SetPixel(fe->hdc_bm, x, y, fe->colours[colour]);
240 } else {
241 HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[colour]);
242 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
243 Rectangle(fe->hdc_bm, x, y, x+w, y+h);
244 SelectObject(fe->hdc_bm, oldbrush);
245 SelectObject(fe->hdc_bm, oldpen);
246 }
247 }
248
249 void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour)
250 {
251 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
252 MoveToEx(fe->hdc_bm, x1, y1, NULL);
253 LineTo(fe->hdc_bm, x2, y2);
254 SetPixel(fe->hdc_bm, x2, y2, fe->colours[colour]);
255 SelectObject(fe->hdc_bm, oldpen);
256 }
257
258 void draw_polygon(frontend *fe, int *coords, int npoints,
259 int fill, int colour)
260 {
261 POINT *pts = snewn(npoints+1, POINT);
262 int i;
263
264 for (i = 0; i <= npoints; i++) {
265 int j = (i < npoints ? i : 0);
266 pts[i].x = coords[j*2];
267 pts[i].y = coords[j*2+1];
268 }
269
270 if (fill) {
271 HBRUSH oldbrush = SelectObject(fe->hdc_bm, fe->brushes[colour]);
272 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
273 Polygon(fe->hdc_bm, pts, npoints);
274 SelectObject(fe->hdc_bm, oldbrush);
275 SelectObject(fe->hdc_bm, oldpen);
276 } else {
277 HPEN oldpen = SelectObject(fe->hdc_bm, fe->pens[colour]);
278 Polyline(fe->hdc_bm, pts, npoints+1);
279 SelectObject(fe->hdc_bm, oldpen);
280 }
281
282 sfree(pts);
283 }
284
285 void start_draw(frontend *fe)
286 {
287 HDC hdc_win;
288 hdc_win = GetDC(fe->hwnd);
289 fe->hdc_bm = CreateCompatibleDC(hdc_win);
290 fe->prevbm = SelectObject(fe->hdc_bm, fe->bitmap);
291 ReleaseDC(fe->hwnd, hdc_win);
292 fe->clip = NULL;
293 SetMapMode(fe->hdc_bm, MM_TEXT);
294 }
295
296 void draw_update(frontend *fe, int x, int y, int w, int h)
297 {
298 RECT r;
299
300 r.left = x;
301 r.top = y;
302 r.right = x + w;
303 r.bottom = y + h;
304
305 InvalidateRect(fe->hwnd, &r, FALSE);
306 }
307
308 void end_draw(frontend *fe)
309 {
310 SelectObject(fe->hdc_bm, fe->prevbm);
311 DeleteDC(fe->hdc_bm);
312 if (fe->clip) {
313 DeleteObject(fe->clip);
314 fe->clip = NULL;
315 }
316 }
317
318 void deactivate_timer(frontend *fe)
319 {
320 KillTimer(fe->hwnd, fe->timer);
321 fe->timer = 0;
322 }
323
324 void activate_timer(frontend *fe)
325 {
326 if (!fe->timer) {
327 fe->timer = SetTimer(fe->hwnd, fe->timer, 20, NULL);
328 fe->timer_last_tickcount = GetTickCount();
329 }
330 }
331
332 /*
333 * See if we can find a help file.
334 */
335 static void find_help_file(frontend *fe)
336 {
337 char b[2048], *p, *q, *r;
338 FILE *fp;
339 if (!fe->help_path) {
340 GetModuleFileName(NULL, b, sizeof(b) - 1);
341 r = b;
342 p = strrchr(b, '\\');
343 if (p && p >= r) r = p+1;
344 q = strrchr(b, ':');
345 if (q && q >= r) r = q+1;
346 strcpy(r, HELP_FILE_NAME);
347 if ( (fp = fopen(b, "r")) != NULL) {
348 fe->help_path = dupstr(b);
349 fclose(fp);
350 } else
351 fe->help_path = NULL;
352 strcpy(r, HELP_CNT_NAME);
353 if ( (fp = fopen(b, "r")) != NULL) {
354 fe->help_has_contents = TRUE;
355 fclose(fp);
356 } else
357 fe->help_has_contents = FALSE;
358 }
359 }
360
361 static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
362 {
363 frontend *fe;
364 int x, y;
365 RECT r, sr;
366 HDC hdc;
367
368 fe = snew(frontend);
369
370 fe->me = midend_new(fe, &thegame);
371
372 if (game_id) {
373 *error = midend_game_id(fe->me, game_id, FALSE);
374 if (*error) {
375 midend_free(fe->me);
376 sfree(fe);
377 return NULL;
378 }
379 }
380
381 fe->help_path = NULL;
382 find_help_file(fe);
383
384 fe->inst = inst;
385 midend_new_game(fe->me);
386 midend_size(fe->me, &x, &y);
387
388 fe->timer = 0;
389
390 fe->fonts = NULL;
391 fe->nfonts = fe->fontsize = 0;
392
393 {
394 int i, ncolours;
395 float *colours;
396
397 colours = midend_colours(fe->me, &ncolours);
398
399 fe->colours = snewn(ncolours, COLORREF);
400 fe->brushes = snewn(ncolours, HBRUSH);
401 fe->pens = snewn(ncolours, HPEN);
402
403 for (i = 0; i < ncolours; i++) {
404 fe->colours[i] = RGB(255 * colours[i*3+0],
405 255 * colours[i*3+1],
406 255 * colours[i*3+2]);
407 fe->brushes[i] = CreateSolidBrush(fe->colours[i]);
408 fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]);
409 }
410 }
411
412 r.left = r.top = 0;
413 r.right = x;
414 r.bottom = y;
415 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
416 (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED),
417 TRUE, 0);
418
419 fe->hwnd = CreateWindowEx(0, thegame.name, thegame.name,
420 WS_OVERLAPPEDWINDOW &~
421 (WS_THICKFRAME | WS_MAXIMIZEBOX),
422 CW_USEDEFAULT, CW_USEDEFAULT,
423 r.right - r.left, r.bottom - r.top,
424 NULL, NULL, inst, NULL);
425
426 {
427 HMENU bar = CreateMenu();
428 HMENU menu = CreateMenu();
429
430 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Game");
431 AppendMenu(menu, MF_ENABLED, IDM_NEW, "New");
432 AppendMenu(menu, MF_ENABLED, IDM_RESTART, "Restart");
433 AppendMenu(menu, MF_ENABLED, IDM_SEED, "Specific...");
434
435 if ((fe->npresets = midend_num_presets(fe->me)) > 0 ||
436 thegame.can_configure) {
437 HMENU sub = CreateMenu();
438 int i;
439
440 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "Type");
441
442 fe->presets = snewn(fe->npresets, game_params *);
443
444 for (i = 0; i < fe->npresets; i++) {
445 char *name;
446
447 midend_fetch_preset(fe->me, i, &name, &fe->presets[i]);
448
449 /*
450 * FIXME: we ought to go through and do something
451 * with ampersands here.
452 */
453
454 AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name);
455 }
456
457 if (thegame.can_configure) {
458 AppendMenu(sub, MF_ENABLED, IDM_CONFIG, "Custom...");
459 }
460 }
461
462 AppendMenu(menu, MF_SEPARATOR, 0, 0);
463 AppendMenu(menu, MF_ENABLED, IDM_UNDO, "Undo");
464 AppendMenu(menu, MF_ENABLED, IDM_REDO, "Redo");
465 AppendMenu(menu, MF_SEPARATOR, 0, 0);
466 AppendMenu(menu, MF_ENABLED, IDM_QUIT, "Exit");
467 if (fe->help_path) {
468 HMENU hmenu = CreateMenu();
469 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)hmenu, "Help");
470 AppendMenu(hmenu, MF_ENABLED, IDM_HELPC, "Contents");
471 if (thegame.winhelp_topic) {
472 char *item;
473 assert(thegame.name);
474 item = snewn(9+strlen(thegame.name), char); /*ick*/
475 sprintf(item, "Help on %s", thegame.name);
476 AppendMenu(hmenu, MF_ENABLED, IDM_GAMEHELP, item);
477 sfree(item);
478 }
479 }
480 SetMenu(fe->hwnd, bar);
481 }
482
483 if (midend_wants_statusbar(fe->me)) {
484 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh",
485 WS_CHILD | WS_VISIBLE,
486 0, 0, 0, 0, /* status bar does these */
487 fe->hwnd, NULL, inst, NULL);
488 GetWindowRect(fe->statusbar, &sr);
489 SetWindowPos(fe->hwnd, NULL, 0, 0,
490 r.right - r.left, r.bottom - r.top + sr.bottom - sr.top,
491 SWP_NOMOVE | SWP_NOZORDER);
492 SetWindowPos(fe->statusbar, NULL, 0, y, x, sr.bottom - sr.top,
493 SWP_NOZORDER);
494 } else {
495 fe->statusbar = NULL;
496 }
497
498 hdc = GetDC(fe->hwnd);
499 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
500 ReleaseDC(fe->hwnd, hdc);
501
502 SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe);
503
504 ShowWindow(fe->hwnd, SW_NORMAL);
505 SetForegroundWindow(fe->hwnd);
506
507 midend_redraw(fe->me);
508
509 return fe;
510 }
511
512 static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg,
513 WPARAM wParam, LPARAM lParam)
514 {
515 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
516 config_item *i;
517 struct cfg_aux *j;
518
519 switch (msg) {
520 case WM_INITDIALOG:
521 return 0;
522
523 case WM_COMMAND:
524 /*
525 * OK and Cancel are special cases.
526 */
527 if ((HIWORD(wParam) == BN_CLICKED ||
528 HIWORD(wParam) == BN_DOUBLECLICKED) &&
529 (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) {
530 if (LOWORD(wParam) == IDOK) {
531 char *err = midend_set_config(fe->me, fe->cfg_which, fe->cfg);
532
533 if (err) {
534 MessageBox(hwnd, err, "Validation error",
535 MB_ICONERROR | MB_OK);
536 } else {
537 fe->cfg_done = 2;
538 }
539 } else {
540 fe->cfg_done = 1;
541 }
542 return 0;
543 }
544
545 /*
546 * First find the control whose id this is.
547 */
548 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
549 if (j->ctlid == LOWORD(wParam))
550 break;
551 }
552 if (i->type == C_END)
553 return 0; /* not our problem */
554
555 if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) {
556 char buffer[4096];
557 GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer));
558 buffer[lenof(buffer)-1] = '\0';
559 sfree(i->sval);
560 i->sval = dupstr(buffer);
561 } else if (i->type == C_BOOLEAN &&
562 (HIWORD(wParam) == BN_CLICKED ||
563 HIWORD(wParam) == BN_DOUBLECLICKED)) {
564 i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid);
565 } else if (i->type == C_CHOICES &&
566 HIWORD(wParam) == CBN_SELCHANGE) {
567 i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid,
568 CB_GETCURSEL, 0, 0);
569 }
570
571 return 0;
572
573 case WM_CLOSE:
574 fe->cfg_done = 1;
575 return 0;
576 }
577
578 return 0;
579 }
580
581 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
582 char *wclass, int wstyle,
583 int exstyle, char *wtext, int wid)
584 {
585 HWND ret;
586 ret = CreateWindowEx(exstyle, wclass, wtext,
587 wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1,
588 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
589 SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0));
590 return ret;
591 }
592
593 static int get_config(frontend *fe, int which)
594 {
595 config_item *i;
596 struct cfg_aux *j;
597 char *title;
598 WNDCLASS wc;
599 MSG msg;
600 TEXTMETRIC tm;
601 HDC hdc;
602 HFONT oldfont;
603 SIZE size;
604 HWND ctl;
605 int gm, id, nctrls;
606 int winwidth, winheight, col1l, col1r, col2l, col2r, y;
607 int height, width, maxlabel, maxcheckbox;
608
609 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
610 wc.lpfnWndProc = DefDlgProc;
611 wc.cbClsExtra = 0;
612 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
613 wc.hInstance = fe->inst;
614 wc.hIcon = NULL;
615 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
616 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
617 wc.lpszMenuName = NULL;
618 wc.lpszClassName = "GameConfigBox";
619 RegisterClass(&wc);
620
621 hdc = GetDC(fe->hwnd);
622 SetMapMode(hdc, MM_TEXT);
623
624 fe->cfg_done = FALSE;
625
626 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
627 0, 0, 0, 0,
628 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
629 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
630 DEFAULT_QUALITY,
631 FF_SWISS,
632 "MS Shell Dlg");
633
634 oldfont = SelectObject(hdc, fe->cfgfont);
635 if (GetTextMetrics(hdc, &tm)) {
636 height = tm.tmAscent + tm.tmDescent;
637 width = tm.tmAveCharWidth;
638 } else {
639 height = width = 30;
640 }
641
642 fe->cfg = midend_get_config(fe->me, which, &title);
643 fe->cfg_which = which;
644
645 /*
646 * Figure out the layout of the config box by measuring the
647 * length of each piece of text.
648 */
649 maxlabel = maxcheckbox = 0;
650 winheight = height/2;
651
652 for (i = fe->cfg; i->type != C_END; i++) {
653 switch (i->type) {
654 case C_STRING:
655 case C_CHOICES:
656 /*
657 * Both these control types have a label filling only
658 * the left-hand column of the box.
659 */
660 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
661 maxlabel < size.cx)
662 maxlabel = size.cx;
663 winheight += height * 3 / 2 + (height / 2);
664 break;
665
666 case C_BOOLEAN:
667 /*
668 * Checkboxes take up the whole of the box width.
669 */
670 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
671 maxcheckbox < size.cx)
672 maxcheckbox = size.cx;
673 winheight += height + (height / 2);
674 break;
675 }
676 }
677
678 winheight += height + height * 7 / 4; /* OK / Cancel buttons */
679
680 col1l = 2*width;
681 col1r = col1l + maxlabel;
682 col2l = col1r + 2*width;
683 col2r = col2l + 30*width;
684 if (col2r < col1l+2*height+maxcheckbox)
685 col2r = col1l+2*height+maxcheckbox;
686 winwidth = col2r + 2*width;
687
688 ReleaseDC(fe->hwnd, hdc);
689
690 /*
691 * Create the dialog, now that we know its size.
692 */
693 {
694 RECT r, r2;
695
696 r.left = r.top = 0;
697 r.right = winwidth;
698 r.bottom = winheight;
699
700 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
701 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
702 WS_CAPTION | WS_SYSMENU*/) &~
703 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
704 FALSE, 0);
705
706 /*
707 * Centre the dialog on its parent window.
708 */
709 r.right -= r.left;
710 r.bottom -= r.top;
711 GetWindowRect(fe->hwnd, &r2);
712 r.left = (r2.left + r2.right - r.right) / 2;
713 r.top = (r2.top + r2.bottom - r.bottom) / 2;
714 r.right += r.left;
715 r.bottom += r.top;
716
717 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title,
718 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
719 WS_CAPTION | WS_SYSMENU,
720 r.left, r.top,
721 r.right-r.left, r.bottom-r.top,
722 fe->hwnd, NULL, fe->inst, NULL);
723 sfree(title);
724 }
725
726 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
727
728 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
729 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc);
730
731 /*
732 * Count the controls so we can allocate cfgaux.
733 */
734 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
735 nctrls++;
736 fe->cfgaux = snewn(nctrls, struct cfg_aux);
737
738 id = 1000;
739 y = height/2;
740 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
741 switch (i->type) {
742 case C_STRING:
743 /*
744 * Edit box with a label beside it.
745 */
746 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
747 "Static", 0, 0, i->name, id++);
748 ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2,
749 "EDIT", WS_TABSTOP | ES_AUTOHSCROLL,
750 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
751 SetWindowText(ctl, i->sval);
752 y += height*3/2;
753 break;
754
755 case C_BOOLEAN:
756 /*
757 * Simple checkbox.
758 */
759 mkctrl(fe, col1l, col2r, y, y+height, "BUTTON",
760 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
761 0, i->name, (j->ctlid = id++));
762 CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0));
763 y += height;
764 break;
765
766 case C_CHOICES:
767 /*
768 * Drop-down list with a label beside it.
769 */
770 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
771 "STATIC", 0, 0, i->name, id++);
772 ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2,
773 "COMBOBOX", WS_TABSTOP |
774 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
775 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
776 {
777 char c, *p, *q, *str;
778
779 SendMessage(ctl, CB_RESETCONTENT, 0, 0);
780 p = i->sval;
781 c = *p++;
782 while (*p) {
783 q = p;
784 while (*q && *q != c) q++;
785 str = snewn(q-p+1, char);
786 strncpy(str, p, q-p);
787 str[q-p] = '\0';
788 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str);
789 sfree(str);
790 if (*q) q++;
791 p = q;
792 }
793 }
794
795 SendMessage(ctl, CB_SETCURSEL, i->ival, 0);
796
797 y += height*3/2;
798 break;
799 }
800
801 assert(y < winheight);
802 y += height/2;
803 }
804
805 y += height/2; /* extra space before OK and Cancel */
806 mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON",
807 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
808 "OK", IDOK);
809 mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON",
810 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL);
811
812 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
813
814 EnableWindow(fe->hwnd, FALSE);
815 ShowWindow(fe->cfgbox, SW_NORMAL);
816 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
817 if (!IsDialogMessage(fe->cfgbox, &msg))
818 DispatchMessage(&msg);
819 if (fe->cfg_done)
820 break;
821 }
822 EnableWindow(fe->hwnd, TRUE);
823 SetForegroundWindow(fe->hwnd);
824 DestroyWindow(fe->cfgbox);
825 DeleteObject(fe->cfgfont);
826
827 free_cfg(fe->cfg);
828 sfree(fe->cfgaux);
829
830 return (fe->cfg_done == 2);
831 }
832
833 static void new_game_type(frontend *fe)
834 {
835 RECT r, sr;
836 HDC hdc;
837 int x, y;
838
839 midend_new_game(fe->me);
840 midend_size(fe->me, &x, &y);
841
842 r.left = r.top = 0;
843 r.right = x;
844 r.bottom = y;
845 AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~
846 (WS_THICKFRAME | WS_MAXIMIZEBOX |
847 WS_OVERLAPPED),
848 TRUE, 0);
849
850 if (fe->statusbar != NULL) {
851 GetWindowRect(fe->statusbar, &sr);
852 } else {
853 sr.left = sr.right = sr.top = sr.bottom = 0;
854 }
855 SetWindowPos(fe->hwnd, NULL, 0, 0,
856 r.right - r.left,
857 r.bottom - r.top + sr.bottom - sr.top,
858 SWP_NOMOVE | SWP_NOZORDER);
859 if (fe->statusbar != NULL)
860 SetWindowPos(fe->statusbar, NULL, 0, y, x,
861 sr.bottom - sr.top, SWP_NOZORDER);
862
863 DeleteObject(fe->bitmap);
864
865 hdc = GetDC(fe->hwnd);
866 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
867 ReleaseDC(fe->hwnd, hdc);
868
869 midend_redraw(fe->me);
870 }
871
872 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
873 WPARAM wParam, LPARAM lParam)
874 {
875 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
876
877 switch (message) {
878 case WM_CLOSE:
879 DestroyWindow(hwnd);
880 return 0;
881 case WM_COMMAND:
882 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
883 case IDM_NEW:
884 if (!midend_process_key(fe->me, 0, 0, 'n'))
885 PostQuitMessage(0);
886 break;
887 case IDM_RESTART:
888 if (!midend_process_key(fe->me, 0, 0, 'r'))
889 PostQuitMessage(0);
890 break;
891 case IDM_UNDO:
892 if (!midend_process_key(fe->me, 0, 0, 'u'))
893 PostQuitMessage(0);
894 break;
895 case IDM_REDO:
896 if (!midend_process_key(fe->me, 0, 0, '\x12'))
897 PostQuitMessage(0);
898 break;
899 case IDM_QUIT:
900 if (!midend_process_key(fe->me, 0, 0, 'q'))
901 PostQuitMessage(0);
902 break;
903 case IDM_CONFIG:
904 if (get_config(fe, CFG_SETTINGS))
905 new_game_type(fe);
906 break;
907 case IDM_SEED:
908 if (get_config(fe, CFG_SEED))
909 new_game_type(fe);
910 break;
911 case IDM_HELPC:
912 assert(fe->help_path);
913 WinHelp(hwnd, fe->help_path,
914 fe->help_has_contents ? HELP_FINDER : HELP_CONTENTS, 0);
915 break;
916 case IDM_GAMEHELP:
917 assert(fe->help_path);
918 assert(thegame.winhelp_topic);
919 {
920 char *cmd = snewn(10+strlen(thegame.winhelp_topic), char);
921 sprintf(cmd, "JI(`',`%s')", thegame.winhelp_topic);
922 WinHelp(hwnd, fe->help_path, HELP_COMMAND, (DWORD)cmd);
923 sfree(cmd);
924 }
925 break;
926 default:
927 {
928 int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10;
929
930 if (p >= 0 && p < fe->npresets) {
931 midend_set_params(fe->me, fe->presets[p]);
932 new_game_type(fe);
933 }
934 }
935 break;
936 }
937 break;
938 case WM_DESTROY:
939 PostQuitMessage(0);
940 return 0;
941 case WM_PAINT:
942 {
943 PAINTSTRUCT p;
944 HDC hdc, hdc2;
945 HBITMAP prevbm;
946
947 hdc = BeginPaint(hwnd, &p);
948 hdc2 = CreateCompatibleDC(hdc);
949 prevbm = SelectObject(hdc2, fe->bitmap);
950 BitBlt(hdc,
951 p.rcPaint.left, p.rcPaint.top,
952 p.rcPaint.right - p.rcPaint.left,
953 p.rcPaint.bottom - p.rcPaint.top,
954 hdc2,
955 p.rcPaint.left, p.rcPaint.top,
956 SRCCOPY);
957 SelectObject(hdc2, prevbm);
958 DeleteDC(hdc2);
959 EndPaint(hwnd, &p);
960 }
961 return 0;
962 case WM_KEYDOWN:
963 {
964 int key = -1;
965
966 switch (wParam) {
967 case VK_LEFT: key = CURSOR_LEFT; break;
968 case VK_RIGHT: key = CURSOR_RIGHT; break;
969 case VK_UP: key = CURSOR_UP; break;
970 case VK_DOWN: key = CURSOR_DOWN; break;
971 /*
972 * Diagonal keys on the numeric keypad.
973 */
974 case VK_PRIOR:
975 if (!(lParam & 0x01000000)) key = CURSOR_UP_RIGHT;
976 break;
977 case VK_NEXT:
978 if (!(lParam & 0x01000000)) key = CURSOR_DOWN_RIGHT;
979 break;
980 case VK_HOME:
981 if (!(lParam & 0x01000000)) key = CURSOR_UP_LEFT;
982 break;
983 case VK_END:
984 if (!(lParam & 0x01000000)) key = CURSOR_DOWN_LEFT;
985 break;
986 /*
987 * Numeric keypad keys with Num Lock on.
988 */
989 case VK_NUMPAD4: key = CURSOR_LEFT; break;
990 case VK_NUMPAD6: key = CURSOR_RIGHT; break;
991 case VK_NUMPAD8: key = CURSOR_UP; break;
992 case VK_NUMPAD2: key = CURSOR_DOWN; break;
993 case VK_NUMPAD9: key = CURSOR_UP_RIGHT; break;
994 case VK_NUMPAD3: key = CURSOR_DOWN_RIGHT; break;
995 case VK_NUMPAD7: key = CURSOR_UP_LEFT; break;
996 case VK_NUMPAD1: key = CURSOR_DOWN_LEFT; break;
997 }
998
999 if (key != -1) {
1000 if (!midend_process_key(fe->me, 0, 0, key))
1001 PostQuitMessage(0);
1002 } else {
1003 MSG m;
1004 m.hwnd = hwnd;
1005 m.message = WM_KEYDOWN;
1006 m.wParam = wParam;
1007 m.lParam = lParam & 0xdfff;
1008 TranslateMessage(&m);
1009 }
1010 }
1011 break;
1012 case WM_LBUTTONDOWN:
1013 case WM_RBUTTONDOWN:
1014 case WM_MBUTTONDOWN:
1015 {
1016 int button;
1017
1018 /*
1019 * Shift-clicks count as middle-clicks, since otherwise
1020 * two-button Windows users won't have any kind of
1021 * middle click to use.
1022 */
1023 if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT))
1024 button = MIDDLE_BUTTON;
1025 else if (message == WM_LBUTTONDOWN)
1026 button = LEFT_BUTTON;
1027 else
1028 button = RIGHT_BUTTON;
1029
1030 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1031 (signed short)HIWORD(lParam), button))
1032 PostQuitMessage(0);
1033
1034 SetCapture(hwnd);
1035 }
1036 break;
1037 case WM_LBUTTONUP:
1038 case WM_RBUTTONUP:
1039 case WM_MBUTTONUP:
1040 {
1041 int button;
1042
1043 /*
1044 * Shift-clicks count as middle-clicks, since otherwise
1045 * two-button Windows users won't have any kind of
1046 * middle click to use.
1047 */
1048 if (message == WM_MBUTTONUP || (wParam & MK_SHIFT))
1049 button = MIDDLE_RELEASE;
1050 else if (message == WM_LBUTTONUP)
1051 button = LEFT_RELEASE;
1052 else
1053 button = RIGHT_RELEASE;
1054
1055 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1056 (signed short)HIWORD(lParam), button))
1057 PostQuitMessage(0);
1058
1059 ReleaseCapture();
1060 }
1061 break;
1062 case WM_MOUSEMOVE:
1063 {
1064 int button;
1065
1066 if (wParam & (MK_MBUTTON | MK_SHIFT))
1067 button = MIDDLE_DRAG;
1068 else if (wParam & MK_LBUTTON)
1069 button = LEFT_DRAG;
1070 else
1071 button = RIGHT_DRAG;
1072
1073 if (!midend_process_key(fe->me, (signed short)LOWORD(lParam),
1074 (signed short)HIWORD(lParam), button))
1075 PostQuitMessage(0);
1076 }
1077 break;
1078 case WM_CHAR:
1079 if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam))
1080 PostQuitMessage(0);
1081 return 0;
1082 case WM_TIMER:
1083 if (fe->timer) {
1084 DWORD now = GetTickCount();
1085 float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F;
1086 midend_timer(fe->me, elapsed);
1087 fe->timer_last_tickcount = now;
1088 }
1089 return 0;
1090 }
1091
1092 return DefWindowProc(hwnd, message, wParam, lParam);
1093 }
1094
1095 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
1096 {
1097 MSG msg;
1098 char *error;
1099
1100 InitCommonControls();
1101
1102 if (!prev) {
1103 WNDCLASS wndclass;
1104
1105 wndclass.style = 0;
1106 wndclass.lpfnWndProc = WndProc;
1107 wndclass.cbClsExtra = 0;
1108 wndclass.cbWndExtra = 0;
1109 wndclass.hInstance = inst;
1110 wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION);
1111 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
1112 wndclass.hbrBackground = NULL;
1113 wndclass.lpszMenuName = NULL;
1114 wndclass.lpszClassName = thegame.name;
1115
1116 RegisterClass(&wndclass);
1117 }
1118
1119 while (*cmdline && isspace(*cmdline))
1120 cmdline++;
1121
1122 if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) {
1123 char buf[128];
1124 sprintf(buf, "%.100s Error", thegame.name);
1125 MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
1126 return 1;
1127 }
1128
1129 while (GetMessage(&msg, NULL, 0, 0)) {
1130 DispatchMessage(&msg);
1131 }
1132
1133 return msg.wParam;
1134 }