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