720a8fb7 |
1 | /* |
2 | * windows.c: Windows front end for my puzzle collection. |
3 | */ |
4e7ef6e6 |
4 | |
5 | #include <windows.h> |
fd1a1a2b |
6 | #include <commctrl.h> |
4e7ef6e6 |
7 | |
8 | #include <stdio.h> |
4efb3868 |
9 | #include <assert.h> |
4e7ef6e6 |
10 | #include <stdarg.h> |
11 | #include <stdlib.h> |
12 | #include <time.h> |
13 | |
14 | #include "puzzles.h" |
15 | |
eb2ad6f1 |
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 |
077f3cbe |
21 | #define IDM_CONFIG 0x0060 |
5928817c |
22 | #define IDM_SEED 0x0070 |
eb2ad6f1 |
23 | #define IDM_PRESETS 0x0100 |
24 | |
c71454c0 |
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 | |
4efb3868 |
70 | struct font { |
71 | HFONT font; |
72 | int type; |
73 | int size; |
74 | }; |
75 | |
077f3cbe |
76 | struct cfg_aux { |
77 | int ctlid; |
78 | }; |
79 | |
4e7ef6e6 |
80 | struct frontend { |
81 | midend_data *me; |
077f3cbe |
82 | HWND hwnd, statusbar, cfgbox; |
83 | HINSTANCE inst; |
4e7ef6e6 |
84 | HBITMAP bitmap, prevbm; |
85 | HDC hdc_bm; |
86 | COLORREF *colours; |
87 | HBRUSH *brushes; |
88 | HPEN *pens; |
4efb3868 |
89 | HRGN clip; |
4e7ef6e6 |
90 | UINT timer; |
8c1fd974 |
91 | DWORD timer_last_tickcount; |
eb2ad6f1 |
92 | int npresets; |
93 | game_params **presets; |
4efb3868 |
94 | struct font *fonts; |
95 | int nfonts, fontsize; |
077f3cbe |
96 | config_item *cfg; |
97 | struct cfg_aux *cfgaux; |
5928817c |
98 | int cfg_which, cfg_done; |
077f3cbe |
99 | HFONT cfgfont; |
4e7ef6e6 |
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 | |
fd1a1a2b |
116 | void status_bar(frontend *fe, char *text) |
117 | { |
118 | SetWindowText(fe->statusbar, text); |
119 | } |
120 | |
4e7ef6e6 |
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 | |
4efb3868 |
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 | |
4e7ef6e6 |
210 | void draw_rect(frontend *fe, int x, int y, int w, int h, int colour) |
211 | { |
fbdd7610 |
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 | } |
4e7ef6e6 |
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); |
4efb3868 |
272 | fe->clip = NULL; |
077f3cbe |
273 | SetMapMode(fe->hdc_bm, MM_TEXT); |
4e7ef6e6 |
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); |
4efb3868 |
292 | if (fe->clip) { |
293 | DeleteObject(fe->clip); |
294 | fe->clip = NULL; |
295 | } |
4e7ef6e6 |
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 | { |
8c1fd974 |
306 | if (!fe->timer) { |
307 | fe->timer = SetTimer(fe->hwnd, fe->timer, 20, NULL); |
308 | fe->timer_last_tickcount = GetTickCount(); |
309 | } |
4e7ef6e6 |
310 | } |
311 | |
312 | static frontend *new_window(HINSTANCE inst) |
313 | { |
314 | frontend *fe; |
315 | int x, y; |
fd1a1a2b |
316 | RECT r, sr; |
4e7ef6e6 |
317 | HDC hdc; |
48d70ca9 |
318 | time_t t; |
4e7ef6e6 |
319 | |
320 | fe = snew(frontend); |
48d70ca9 |
321 | |
322 | time(&t); |
323 | fe->me = midend_new(fe, &t, sizeof(t)); |
324 | |
077f3cbe |
325 | fe->inst = inst; |
5928817c |
326 | midend_new_game(fe->me); |
4e7ef6e6 |
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]); |
4e7ef6e6 |
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), |
eb2ad6f1 |
355 | TRUE, 0); |
4e7ef6e6 |
356 | |
0c490335 |
357 | fe->hwnd = CreateWindowEx(0, game_name, game_name, |
4e7ef6e6 |
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 | |
eb2ad6f1 |
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"); |
5928817c |
371 | AppendMenu(menu, MF_ENABLED, IDM_SEED, "Specific..."); |
eb2ad6f1 |
372 | |
077f3cbe |
373 | if ((fe->npresets = midend_num_presets(fe->me)) > 0 || |
374 | game_can_configure) { |
eb2ad6f1 |
375 | HMENU sub = CreateMenu(); |
376 | int i; |
377 | |
077f3cbe |
378 | AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "Type"); |
eb2ad6f1 |
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 | } |
077f3cbe |
394 | |
395 | if (game_can_configure) { |
396 | AppendMenu(sub, MF_ENABLED, IDM_CONFIG, "Custom..."); |
397 | } |
eb2ad6f1 |
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 | |
fd1a1a2b |
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 | |
4e7ef6e6 |
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 | |
077f3cbe |
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) { |
5928817c |
456 | char *err = midend_set_config(fe->me, fe->cfg_which, fe->cfg); |
077f3cbe |
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 | |
5928817c |
518 | static int get_config(frontend *fe, int which) |
077f3cbe |
519 | { |
520 | config_item *i; |
521 | struct cfg_aux *j; |
5928817c |
522 | char *title; |
077f3cbe |
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 | |
5928817c |
567 | fe->cfg = midend_get_config(fe->me, which, &title); |
568 | fe->cfg_which = which; |
077f3cbe |
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 | |
5928817c |
642 | fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title, |
077f3cbe |
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); |
5928817c |
648 | sfree(title); |
077f3cbe |
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++)); |
42132f2d |
687 | CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0)); |
077f3cbe |
688 | y += height; |
689 | break; |
690 | |
691 | case C_CHOICES: |
692 | /* |
693 | * Drop-down list with a label beside it. |
694 | */ |
695 | mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8, |
696 | "STATIC", 0, 0, i->name, id++); |
697 | ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2, |
698 | "COMBOBOX", WS_TABSTOP | |
699 | CBS_DROPDOWNLIST | CBS_HASSTRINGS, |
700 | WS_EX_CLIENTEDGE, "", (j->ctlid = id++)); |
701 | { |
702 | char c, *p, *q, *str; |
703 | |
704 | SendMessage(ctl, CB_RESETCONTENT, 0, 0); |
705 | p = i->sval; |
706 | c = *p++; |
707 | while (*p) { |
708 | q = p; |
709 | while (*q && *q != c) q++; |
710 | str = snewn(q-p+1, char); |
711 | strncpy(str, p, q-p); |
712 | str[q-p] = '\0'; |
713 | SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str); |
714 | sfree(str); |
715 | if (*q) q++; |
716 | p = q; |
717 | } |
718 | } |
719 | |
720 | SendMessage(ctl, CB_SETCURSEL, i->ival, 0); |
721 | |
722 | y += height*3/2; |
723 | break; |
724 | } |
725 | |
726 | assert(y < winheight); |
727 | y += height/2; |
728 | } |
729 | |
730 | y += height/2; /* extra space before OK and Cancel */ |
731 | mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON", |
732 | BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0, |
733 | "OK", IDOK); |
734 | mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON", |
735 | BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL); |
736 | |
737 | SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0); |
738 | |
739 | EnableWindow(fe->hwnd, FALSE); |
740 | ShowWindow(fe->cfgbox, SW_NORMAL); |
741 | while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) { |
742 | if (!IsDialogMessage(fe->cfgbox, &msg)) |
743 | DispatchMessage(&msg); |
744 | if (fe->cfg_done) |
745 | break; |
746 | } |
747 | EnableWindow(fe->hwnd, TRUE); |
748 | SetForegroundWindow(fe->hwnd); |
749 | DestroyWindow(fe->cfgbox); |
750 | DeleteObject(fe->cfgfont); |
751 | |
752 | free_cfg(fe->cfg); |
753 | sfree(fe->cfgaux); |
754 | |
755 | return (fe->cfg_done == 2); |
756 | } |
757 | |
758 | static void new_game_type(frontend *fe) |
759 | { |
760 | RECT r, sr; |
761 | HDC hdc; |
762 | int x, y; |
763 | |
5928817c |
764 | midend_new_game(fe->me); |
077f3cbe |
765 | midend_size(fe->me, &x, &y); |
766 | |
767 | r.left = r.top = 0; |
768 | r.right = x; |
769 | r.bottom = y; |
770 | AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~ |
771 | (WS_THICKFRAME | WS_MAXIMIZEBOX | |
772 | WS_OVERLAPPED), |
773 | TRUE, 0); |
774 | |
775 | if (fe->statusbar != NULL) { |
776 | GetWindowRect(fe->statusbar, &sr); |
777 | } else { |
778 | sr.left = sr.right = sr.top = sr.bottom = 0; |
779 | } |
780 | SetWindowPos(fe->hwnd, NULL, 0, 0, |
781 | r.right - r.left, |
782 | r.bottom - r.top + sr.bottom - sr.top, |
783 | SWP_NOMOVE | SWP_NOZORDER); |
784 | if (fe->statusbar != NULL) |
785 | SetWindowPos(fe->statusbar, NULL, 0, y, x, |
786 | sr.bottom - sr.top, SWP_NOZORDER); |
787 | |
788 | DeleteObject(fe->bitmap); |
789 | |
790 | hdc = GetDC(fe->hwnd); |
791 | fe->bitmap = CreateCompatibleBitmap(hdc, x, y); |
792 | ReleaseDC(fe->hwnd, hdc); |
793 | |
794 | midend_redraw(fe->me); |
795 | } |
796 | |
4e7ef6e6 |
797 | static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, |
798 | WPARAM wParam, LPARAM lParam) |
799 | { |
800 | frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA); |
801 | |
802 | switch (message) { |
803 | case WM_CLOSE: |
804 | DestroyWindow(hwnd); |
805 | return 0; |
eb2ad6f1 |
806 | case WM_COMMAND: |
807 | switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */ |
808 | case IDM_NEW: |
809 | if (!midend_process_key(fe->me, 0, 0, 'n')) |
810 | PostQuitMessage(0); |
811 | break; |
812 | case IDM_RESTART: |
813 | if (!midend_process_key(fe->me, 0, 0, 'r')) |
814 | PostQuitMessage(0); |
815 | break; |
816 | case IDM_UNDO: |
817 | if (!midend_process_key(fe->me, 0, 0, 'u')) |
818 | PostQuitMessage(0); |
819 | break; |
820 | case IDM_REDO: |
821 | if (!midend_process_key(fe->me, 0, 0, '\x12')) |
822 | PostQuitMessage(0); |
823 | break; |
824 | case IDM_QUIT: |
825 | if (!midend_process_key(fe->me, 0, 0, 'q')) |
826 | PostQuitMessage(0); |
827 | break; |
077f3cbe |
828 | case IDM_CONFIG: |
5928817c |
829 | if (get_config(fe, CFG_SETTINGS)) |
830 | new_game_type(fe); |
831 | break; |
832 | case IDM_SEED: |
833 | if (get_config(fe, CFG_SEED)) |
077f3cbe |
834 | new_game_type(fe); |
835 | break; |
eb2ad6f1 |
836 | default: |
837 | { |
838 | int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10; |
839 | |
840 | if (p >= 0 && p < fe->npresets) { |
eb2ad6f1 |
841 | midend_set_params(fe->me, fe->presets[p]); |
077f3cbe |
842 | new_game_type(fe); |
eb2ad6f1 |
843 | } |
844 | } |
845 | break; |
846 | } |
847 | break; |
4e7ef6e6 |
848 | case WM_DESTROY: |
849 | PostQuitMessage(0); |
850 | return 0; |
851 | case WM_PAINT: |
852 | { |
853 | PAINTSTRUCT p; |
854 | HDC hdc, hdc2; |
855 | HBITMAP prevbm; |
856 | |
857 | hdc = BeginPaint(hwnd, &p); |
858 | hdc2 = CreateCompatibleDC(hdc); |
859 | prevbm = SelectObject(hdc2, fe->bitmap); |
860 | BitBlt(hdc, |
861 | p.rcPaint.left, p.rcPaint.top, |
862 | p.rcPaint.right - p.rcPaint.left, |
863 | p.rcPaint.bottom - p.rcPaint.top, |
864 | hdc2, |
865 | p.rcPaint.left, p.rcPaint.top, |
866 | SRCCOPY); |
867 | SelectObject(hdc2, prevbm); |
868 | DeleteDC(hdc2); |
869 | EndPaint(hwnd, &p); |
870 | } |
871 | return 0; |
872 | case WM_KEYDOWN: |
873 | { |
874 | int key = -1; |
875 | |
876 | switch (wParam) { |
877 | case VK_LEFT: key = CURSOR_LEFT; break; |
878 | case VK_RIGHT: key = CURSOR_RIGHT; break; |
879 | case VK_UP: key = CURSOR_UP; break; |
880 | case VK_DOWN: key = CURSOR_DOWN; break; |
c71454c0 |
881 | /* |
882 | * Diagonal keys on the numeric keypad. |
883 | */ |
884 | case VK_PRIOR: |
885 | if (!(lParam & 0x01000000)) key = CURSOR_UP_RIGHT; |
886 | break; |
887 | case VK_NEXT: |
888 | if (!(lParam & 0x01000000)) key = CURSOR_DOWN_RIGHT; |
889 | break; |
890 | case VK_HOME: |
891 | if (!(lParam & 0x01000000)) key = CURSOR_UP_LEFT; |
892 | break; |
893 | case VK_END: |
894 | if (!(lParam & 0x01000000)) key = CURSOR_DOWN_LEFT; |
895 | break; |
896 | /* |
897 | * Numeric keypad keys with Num Lock on. |
898 | */ |
899 | case VK_NUMPAD4: key = CURSOR_LEFT; break; |
900 | case VK_NUMPAD6: key = CURSOR_RIGHT; break; |
901 | case VK_NUMPAD8: key = CURSOR_UP; break; |
902 | case VK_NUMPAD2: key = CURSOR_DOWN; break; |
903 | case VK_NUMPAD9: key = CURSOR_UP_RIGHT; break; |
904 | case VK_NUMPAD3: key = CURSOR_DOWN_RIGHT; break; |
905 | case VK_NUMPAD7: key = CURSOR_UP_LEFT; break; |
906 | case VK_NUMPAD1: key = CURSOR_DOWN_LEFT; break; |
4e7ef6e6 |
907 | } |
908 | |
909 | if (key != -1) { |
eb2ad6f1 |
910 | if (!midend_process_key(fe->me, 0, 0, key)) |
4e7ef6e6 |
911 | PostQuitMessage(0); |
c71454c0 |
912 | } else { |
913 | MSG m; |
914 | m.hwnd = hwnd; |
915 | m.message = WM_KEYDOWN; |
916 | m.wParam = wParam; |
917 | m.lParam = lParam & 0xdfff; |
918 | TranslateMessage(&m); |
4e7ef6e6 |
919 | } |
920 | } |
921 | break; |
922 | case WM_LBUTTONDOWN: |
923 | case WM_RBUTTONDOWN: |
924 | case WM_MBUTTONDOWN: |
b0f06719 |
925 | { |
926 | int button; |
927 | |
928 | /* |
929 | * Shift-clicks count as middle-clicks, since otherwise |
930 | * two-button Windows users won't have any kind of |
931 | * middle click to use. |
932 | */ |
933 | if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT)) |
934 | button = MIDDLE_BUTTON; |
935 | else if (message == WM_LBUTTONDOWN) |
936 | button = LEFT_BUTTON; |
937 | else |
938 | button = RIGHT_BUTTON; |
939 | |
940 | if (!midend_process_key(fe->me, LOWORD(lParam), |
941 | HIWORD(lParam), button)) |
942 | PostQuitMessage(0); |
943 | } |
4e7ef6e6 |
944 | break; |
945 | case WM_CHAR: |
eb2ad6f1 |
946 | if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam)) |
4e7ef6e6 |
947 | PostQuitMessage(0); |
948 | return 0; |
949 | case WM_TIMER: |
8c1fd974 |
950 | if (fe->timer) { |
951 | DWORD now = GetTickCount(); |
952 | float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F; |
953 | midend_timer(fe->me, elapsed); |
954 | fe->timer_last_tickcount = now; |
955 | } |
4e7ef6e6 |
956 | return 0; |
957 | } |
958 | |
959 | return DefWindowProc(hwnd, message, wParam, lParam); |
960 | } |
961 | |
962 | int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) |
963 | { |
964 | MSG msg; |
965 | |
fd1a1a2b |
966 | InitCommonControls(); |
967 | |
4e7ef6e6 |
968 | if (!prev) { |
969 | WNDCLASS wndclass; |
970 | |
971 | wndclass.style = 0; |
972 | wndclass.lpfnWndProc = WndProc; |
973 | wndclass.cbClsExtra = 0; |
974 | wndclass.cbWndExtra = 0; |
975 | wndclass.hInstance = inst; |
976 | wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION); |
977 | wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); |
978 | wndclass.hbrBackground = NULL; |
979 | wndclass.lpszMenuName = NULL; |
0c490335 |
980 | wndclass.lpszClassName = game_name; |
4e7ef6e6 |
981 | |
982 | RegisterClass(&wndclass); |
983 | } |
984 | |
985 | new_window(inst); |
986 | |
987 | while (GetMessage(&msg, NULL, 0, 0)) { |
4e7ef6e6 |
988 | DispatchMessage(&msg); |
989 | } |
990 | |
991 | return msg.wParam; |
992 | } |