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