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