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