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