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 | |
6beae131 |
331 | fe->fonts = NULL; |
332 | fe->nfonts = fe->fontsize = 0; |
333 | |
4e7ef6e6 |
334 | { |
335 | int i, ncolours; |
336 | float *colours; |
337 | |
338 | colours = midend_colours(fe->me, &ncolours); |
339 | |
340 | fe->colours = snewn(ncolours, COLORREF); |
341 | fe->brushes = snewn(ncolours, HBRUSH); |
342 | fe->pens = snewn(ncolours, HPEN); |
343 | |
344 | for (i = 0; i < ncolours; i++) { |
345 | fe->colours[i] = RGB(255 * colours[i*3+0], |
346 | 255 * colours[i*3+1], |
347 | 255 * colours[i*3+2]); |
348 | fe->brushes[i] = CreateSolidBrush(fe->colours[i]); |
4e7ef6e6 |
349 | fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]); |
350 | } |
351 | } |
352 | |
353 | r.left = r.top = 0; |
354 | r.right = x; |
355 | r.bottom = y; |
356 | AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~ |
357 | (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED), |
eb2ad6f1 |
358 | TRUE, 0); |
4e7ef6e6 |
359 | |
0c490335 |
360 | fe->hwnd = CreateWindowEx(0, game_name, game_name, |
4e7ef6e6 |
361 | WS_OVERLAPPEDWINDOW &~ |
362 | (WS_THICKFRAME | WS_MAXIMIZEBOX), |
363 | CW_USEDEFAULT, CW_USEDEFAULT, |
364 | r.right - r.left, r.bottom - r.top, |
365 | NULL, NULL, inst, NULL); |
366 | |
eb2ad6f1 |
367 | { |
368 | HMENU bar = CreateMenu(); |
369 | HMENU menu = CreateMenu(); |
370 | |
371 | AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Game"); |
372 | AppendMenu(menu, MF_ENABLED, IDM_NEW, "New"); |
373 | AppendMenu(menu, MF_ENABLED, IDM_RESTART, "Restart"); |
5928817c |
374 | AppendMenu(menu, MF_ENABLED, IDM_SEED, "Specific..."); |
eb2ad6f1 |
375 | |
077f3cbe |
376 | if ((fe->npresets = midend_num_presets(fe->me)) > 0 || |
377 | game_can_configure) { |
eb2ad6f1 |
378 | HMENU sub = CreateMenu(); |
379 | int i; |
380 | |
077f3cbe |
381 | AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "Type"); |
eb2ad6f1 |
382 | |
383 | fe->presets = snewn(fe->npresets, game_params *); |
384 | |
385 | for (i = 0; i < fe->npresets; i++) { |
386 | char *name; |
387 | |
388 | midend_fetch_preset(fe->me, i, &name, &fe->presets[i]); |
389 | |
390 | /* |
391 | * FIXME: we ought to go through and do something |
392 | * with ampersands here. |
393 | */ |
394 | |
395 | AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name); |
396 | } |
077f3cbe |
397 | |
398 | if (game_can_configure) { |
399 | AppendMenu(sub, MF_ENABLED, IDM_CONFIG, "Custom..."); |
400 | } |
eb2ad6f1 |
401 | } |
402 | |
403 | AppendMenu(menu, MF_SEPARATOR, 0, 0); |
404 | AppendMenu(menu, MF_ENABLED, IDM_UNDO, "Undo"); |
405 | AppendMenu(menu, MF_ENABLED, IDM_REDO, "Redo"); |
406 | AppendMenu(menu, MF_SEPARATOR, 0, 0); |
407 | AppendMenu(menu, MF_ENABLED, IDM_QUIT, "Exit"); |
408 | SetMenu(fe->hwnd, bar); |
409 | } |
410 | |
fd1a1a2b |
411 | if (midend_wants_statusbar(fe->me)) { |
412 | fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh", |
413 | WS_CHILD | WS_VISIBLE, |
414 | 0, 0, 0, 0, /* status bar does these */ |
415 | fe->hwnd, NULL, inst, NULL); |
416 | GetWindowRect(fe->statusbar, &sr); |
417 | SetWindowPos(fe->hwnd, NULL, 0, 0, |
418 | r.right - r.left, r.bottom - r.top + sr.bottom - sr.top, |
419 | SWP_NOMOVE | SWP_NOZORDER); |
420 | SetWindowPos(fe->statusbar, NULL, 0, y, x, sr.bottom - sr.top, |
421 | SWP_NOZORDER); |
422 | } else { |
423 | fe->statusbar = NULL; |
424 | } |
425 | |
4e7ef6e6 |
426 | hdc = GetDC(fe->hwnd); |
427 | fe->bitmap = CreateCompatibleBitmap(hdc, x, y); |
428 | ReleaseDC(fe->hwnd, hdc); |
429 | |
430 | SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe); |
431 | |
432 | ShowWindow(fe->hwnd, SW_NORMAL); |
433 | SetForegroundWindow(fe->hwnd); |
434 | |
435 | midend_redraw(fe->me); |
436 | |
437 | return fe; |
438 | } |
439 | |
077f3cbe |
440 | static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg, |
441 | WPARAM wParam, LPARAM lParam) |
442 | { |
443 | frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA); |
444 | config_item *i; |
445 | struct cfg_aux *j; |
446 | |
447 | switch (msg) { |
448 | case WM_INITDIALOG: |
449 | return 0; |
450 | |
451 | case WM_COMMAND: |
452 | /* |
453 | * OK and Cancel are special cases. |
454 | */ |
455 | if ((HIWORD(wParam) == BN_CLICKED || |
456 | HIWORD(wParam) == BN_DOUBLECLICKED) && |
457 | (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) { |
458 | if (LOWORD(wParam) == IDOK) { |
5928817c |
459 | char *err = midend_set_config(fe->me, fe->cfg_which, fe->cfg); |
077f3cbe |
460 | |
461 | if (err) { |
462 | MessageBox(hwnd, err, "Validation error", |
463 | MB_ICONERROR | MB_OK); |
464 | } else { |
465 | fe->cfg_done = 2; |
466 | } |
467 | } else { |
468 | fe->cfg_done = 1; |
469 | } |
470 | return 0; |
471 | } |
472 | |
473 | /* |
474 | * First find the control whose id this is. |
475 | */ |
476 | for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) { |
477 | if (j->ctlid == LOWORD(wParam)) |
478 | break; |
479 | } |
480 | if (i->type == C_END) |
481 | return 0; /* not our problem */ |
482 | |
483 | if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) { |
484 | char buffer[4096]; |
485 | GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer)); |
486 | buffer[lenof(buffer)-1] = '\0'; |
487 | sfree(i->sval); |
488 | i->sval = dupstr(buffer); |
489 | } else if (i->type == C_BOOLEAN && |
490 | (HIWORD(wParam) == BN_CLICKED || |
491 | HIWORD(wParam) == BN_DOUBLECLICKED)) { |
492 | i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid); |
493 | } else if (i->type == C_CHOICES && |
494 | HIWORD(wParam) == CBN_SELCHANGE) { |
495 | i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid, |
496 | CB_GETCURSEL, 0, 0); |
497 | } |
498 | |
499 | return 0; |
500 | |
501 | case WM_CLOSE: |
502 | fe->cfg_done = 1; |
503 | return 0; |
504 | } |
505 | |
506 | return 0; |
507 | } |
508 | |
509 | HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2, |
510 | char *wclass, int wstyle, |
511 | int exstyle, char *wtext, int wid) |
512 | { |
513 | HWND ret; |
514 | ret = CreateWindowEx(exstyle, wclass, wtext, |
515 | wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1, |
516 | fe->cfgbox, (HMENU) wid, fe->inst, NULL); |
517 | SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0)); |
518 | return ret; |
519 | } |
520 | |
5928817c |
521 | static int get_config(frontend *fe, int which) |
077f3cbe |
522 | { |
523 | config_item *i; |
524 | struct cfg_aux *j; |
5928817c |
525 | char *title; |
077f3cbe |
526 | WNDCLASS wc; |
527 | MSG msg; |
528 | TEXTMETRIC tm; |
529 | HDC hdc; |
530 | HFONT oldfont; |
531 | SIZE size; |
532 | HWND ctl; |
533 | int gm, id, nctrls; |
534 | int winwidth, winheight, col1l, col1r, col2l, col2r, y; |
535 | int height, width, maxlabel, maxcheckbox; |
536 | |
537 | wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW; |
538 | wc.lpfnWndProc = DefDlgProc; |
539 | wc.cbClsExtra = 0; |
540 | wc.cbWndExtra = DLGWINDOWEXTRA + 8; |
541 | wc.hInstance = fe->inst; |
542 | wc.hIcon = NULL; |
543 | wc.hCursor = LoadCursor(NULL, IDC_ARROW); |
544 | wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1); |
545 | wc.lpszMenuName = NULL; |
546 | wc.lpszClassName = "GameConfigBox"; |
547 | RegisterClass(&wc); |
548 | |
549 | hdc = GetDC(fe->hwnd); |
550 | SetMapMode(hdc, MM_TEXT); |
551 | |
552 | fe->cfg_done = FALSE; |
553 | |
554 | fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72), |
555 | 0, 0, 0, 0, |
556 | FALSE, FALSE, FALSE, DEFAULT_CHARSET, |
557 | OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, |
558 | DEFAULT_QUALITY, |
559 | FF_SWISS, |
560 | "MS Shell Dlg"); |
561 | |
562 | oldfont = SelectObject(hdc, fe->cfgfont); |
563 | if (GetTextMetrics(hdc, &tm)) { |
564 | height = tm.tmAscent + tm.tmDescent; |
565 | width = tm.tmAveCharWidth; |
566 | } else { |
567 | height = width = 30; |
568 | } |
569 | |
5928817c |
570 | fe->cfg = midend_get_config(fe->me, which, &title); |
571 | fe->cfg_which = which; |
077f3cbe |
572 | |
573 | /* |
574 | * Figure out the layout of the config box by measuring the |
575 | * length of each piece of text. |
576 | */ |
577 | maxlabel = maxcheckbox = 0; |
578 | winheight = height/2; |
579 | |
580 | for (i = fe->cfg; i->type != C_END; i++) { |
581 | switch (i->type) { |
582 | case C_STRING: |
583 | case C_CHOICES: |
584 | /* |
585 | * Both these control types have a label filling only |
586 | * the left-hand column of the box. |
587 | */ |
588 | if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) && |
589 | maxlabel < size.cx) |
590 | maxlabel = size.cx; |
591 | winheight += height * 3 / 2 + (height / 2); |
592 | break; |
593 | |
594 | case C_BOOLEAN: |
595 | /* |
596 | * Checkboxes take up the whole of the box width. |
597 | */ |
598 | if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) && |
599 | maxcheckbox < size.cx) |
600 | maxcheckbox = size.cx; |
601 | winheight += height + (height / 2); |
602 | break; |
603 | } |
604 | } |
605 | |
606 | winheight += height + height * 7 / 4; /* OK / Cancel buttons */ |
607 | |
608 | col1l = 2*width; |
609 | col1r = col1l + maxlabel; |
610 | col2l = col1r + 2*width; |
611 | col2r = col2l + 30*width; |
612 | if (col2r < col1l+2*height+maxcheckbox) |
613 | col2r = col1l+2*height+maxcheckbox; |
614 | winwidth = col2r + 2*width; |
615 | |
616 | ReleaseDC(fe->hwnd, hdc); |
617 | |
618 | /* |
619 | * Create the dialog, now that we know its size. |
620 | */ |
621 | { |
622 | RECT r, r2; |
623 | |
624 | r.left = r.top = 0; |
625 | r.right = winwidth; |
626 | r.bottom = winheight; |
627 | |
628 | AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*| |
629 | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | |
630 | WS_CAPTION | WS_SYSMENU*/) &~ |
631 | (WS_MAXIMIZEBOX | WS_OVERLAPPED), |
632 | FALSE, 0); |
633 | |
634 | /* |
635 | * Centre the dialog on its parent window. |
636 | */ |
637 | r.right -= r.left; |
638 | r.bottom -= r.top; |
639 | GetWindowRect(fe->hwnd, &r2); |
640 | r.left = (r2.left + r2.right - r.right) / 2; |
641 | r.top = (r2.top + r2.bottom - r.bottom) / 2; |
642 | r.right += r.left; |
643 | r.bottom += r.top; |
644 | |
5928817c |
645 | fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title, |
077f3cbe |
646 | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | |
647 | WS_CAPTION | WS_SYSMENU, |
648 | r.left, r.top, |
649 | r.right-r.left, r.bottom-r.top, |
650 | fe->hwnd, NULL, fe->inst, NULL); |
5928817c |
651 | sfree(title); |
077f3cbe |
652 | } |
653 | |
654 | SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE); |
655 | |
656 | SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe); |
657 | SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc); |
658 | |
659 | /* |
660 | * Count the controls so we can allocate cfgaux. |
661 | */ |
662 | for (nctrls = 0, i = fe->cfg; i->type != C_END; i++) |
663 | nctrls++; |
664 | fe->cfgaux = snewn(nctrls, struct cfg_aux); |
665 | |
666 | id = 1000; |
667 | y = height/2; |
668 | for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) { |
669 | switch (i->type) { |
670 | case C_STRING: |
671 | /* |
672 | * Edit box with a label beside it. |
673 | */ |
674 | mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8, |
675 | "Static", 0, 0, i->name, id++); |
676 | ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2, |
677 | "EDIT", WS_TABSTOP | ES_AUTOHSCROLL, |
678 | WS_EX_CLIENTEDGE, "", (j->ctlid = id++)); |
679 | SetWindowText(ctl, i->sval); |
680 | y += height*3/2; |
681 | break; |
682 | |
683 | case C_BOOLEAN: |
684 | /* |
685 | * Simple checkbox. |
686 | */ |
687 | mkctrl(fe, col1l, col2r, y, y+height, "BUTTON", |
688 | BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP, |
689 | 0, i->name, (j->ctlid = id++)); |
42132f2d |
690 | CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0)); |
077f3cbe |
691 | y += height; |
692 | break; |
693 | |
694 | case C_CHOICES: |
695 | /* |
696 | * Drop-down list with a label beside it. |
697 | */ |
698 | mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8, |
699 | "STATIC", 0, 0, i->name, id++); |
700 | ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2, |
701 | "COMBOBOX", WS_TABSTOP | |
702 | CBS_DROPDOWNLIST | CBS_HASSTRINGS, |
703 | WS_EX_CLIENTEDGE, "", (j->ctlid = id++)); |
704 | { |
705 | char c, *p, *q, *str; |
706 | |
707 | SendMessage(ctl, CB_RESETCONTENT, 0, 0); |
708 | p = i->sval; |
709 | c = *p++; |
710 | while (*p) { |
711 | q = p; |
712 | while (*q && *q != c) q++; |
713 | str = snewn(q-p+1, char); |
714 | strncpy(str, p, q-p); |
715 | str[q-p] = '\0'; |
716 | SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str); |
717 | sfree(str); |
718 | if (*q) q++; |
719 | p = q; |
720 | } |
721 | } |
722 | |
723 | SendMessage(ctl, CB_SETCURSEL, i->ival, 0); |
724 | |
725 | y += height*3/2; |
726 | break; |
727 | } |
728 | |
729 | assert(y < winheight); |
730 | y += height/2; |
731 | } |
732 | |
733 | y += height/2; /* extra space before OK and Cancel */ |
734 | mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON", |
735 | BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0, |
736 | "OK", IDOK); |
737 | mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON", |
738 | BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL); |
739 | |
740 | SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0); |
741 | |
742 | EnableWindow(fe->hwnd, FALSE); |
743 | ShowWindow(fe->cfgbox, SW_NORMAL); |
744 | while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) { |
745 | if (!IsDialogMessage(fe->cfgbox, &msg)) |
746 | DispatchMessage(&msg); |
747 | if (fe->cfg_done) |
748 | break; |
749 | } |
750 | EnableWindow(fe->hwnd, TRUE); |
751 | SetForegroundWindow(fe->hwnd); |
752 | DestroyWindow(fe->cfgbox); |
753 | DeleteObject(fe->cfgfont); |
754 | |
755 | free_cfg(fe->cfg); |
756 | sfree(fe->cfgaux); |
757 | |
758 | return (fe->cfg_done == 2); |
759 | } |
760 | |
761 | static void new_game_type(frontend *fe) |
762 | { |
763 | RECT r, sr; |
764 | HDC hdc; |
765 | int x, y; |
766 | |
5928817c |
767 | midend_new_game(fe->me); |
077f3cbe |
768 | midend_size(fe->me, &x, &y); |
769 | |
770 | r.left = r.top = 0; |
771 | r.right = x; |
772 | r.bottom = y; |
773 | AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~ |
774 | (WS_THICKFRAME | WS_MAXIMIZEBOX | |
775 | WS_OVERLAPPED), |
776 | TRUE, 0); |
777 | |
778 | if (fe->statusbar != NULL) { |
779 | GetWindowRect(fe->statusbar, &sr); |
780 | } else { |
781 | sr.left = sr.right = sr.top = sr.bottom = 0; |
782 | } |
783 | SetWindowPos(fe->hwnd, NULL, 0, 0, |
784 | r.right - r.left, |
785 | r.bottom - r.top + sr.bottom - sr.top, |
786 | SWP_NOMOVE | SWP_NOZORDER); |
787 | if (fe->statusbar != NULL) |
788 | SetWindowPos(fe->statusbar, NULL, 0, y, x, |
789 | sr.bottom - sr.top, SWP_NOZORDER); |
790 | |
791 | DeleteObject(fe->bitmap); |
792 | |
793 | hdc = GetDC(fe->hwnd); |
794 | fe->bitmap = CreateCompatibleBitmap(hdc, x, y); |
795 | ReleaseDC(fe->hwnd, hdc); |
796 | |
797 | midend_redraw(fe->me); |
798 | } |
799 | |
4e7ef6e6 |
800 | static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, |
801 | WPARAM wParam, LPARAM lParam) |
802 | { |
803 | frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA); |
804 | |
805 | switch (message) { |
806 | case WM_CLOSE: |
807 | DestroyWindow(hwnd); |
808 | return 0; |
eb2ad6f1 |
809 | case WM_COMMAND: |
810 | switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */ |
811 | case IDM_NEW: |
812 | if (!midend_process_key(fe->me, 0, 0, 'n')) |
813 | PostQuitMessage(0); |
814 | break; |
815 | case IDM_RESTART: |
816 | if (!midend_process_key(fe->me, 0, 0, 'r')) |
817 | PostQuitMessage(0); |
818 | break; |
819 | case IDM_UNDO: |
820 | if (!midend_process_key(fe->me, 0, 0, 'u')) |
821 | PostQuitMessage(0); |
822 | break; |
823 | case IDM_REDO: |
824 | if (!midend_process_key(fe->me, 0, 0, '\x12')) |
825 | PostQuitMessage(0); |
826 | break; |
827 | case IDM_QUIT: |
828 | if (!midend_process_key(fe->me, 0, 0, 'q')) |
829 | PostQuitMessage(0); |
830 | break; |
077f3cbe |
831 | case IDM_CONFIG: |
5928817c |
832 | if (get_config(fe, CFG_SETTINGS)) |
833 | new_game_type(fe); |
834 | break; |
835 | case IDM_SEED: |
836 | if (get_config(fe, CFG_SEED)) |
077f3cbe |
837 | new_game_type(fe); |
838 | break; |
eb2ad6f1 |
839 | default: |
840 | { |
841 | int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10; |
842 | |
843 | if (p >= 0 && p < fe->npresets) { |
eb2ad6f1 |
844 | midend_set_params(fe->me, fe->presets[p]); |
077f3cbe |
845 | new_game_type(fe); |
eb2ad6f1 |
846 | } |
847 | } |
848 | break; |
849 | } |
850 | break; |
4e7ef6e6 |
851 | case WM_DESTROY: |
852 | PostQuitMessage(0); |
853 | return 0; |
854 | case WM_PAINT: |
855 | { |
856 | PAINTSTRUCT p; |
857 | HDC hdc, hdc2; |
858 | HBITMAP prevbm; |
859 | |
860 | hdc = BeginPaint(hwnd, &p); |
861 | hdc2 = CreateCompatibleDC(hdc); |
862 | prevbm = SelectObject(hdc2, fe->bitmap); |
863 | BitBlt(hdc, |
864 | p.rcPaint.left, p.rcPaint.top, |
865 | p.rcPaint.right - p.rcPaint.left, |
866 | p.rcPaint.bottom - p.rcPaint.top, |
867 | hdc2, |
868 | p.rcPaint.left, p.rcPaint.top, |
869 | SRCCOPY); |
870 | SelectObject(hdc2, prevbm); |
871 | DeleteDC(hdc2); |
872 | EndPaint(hwnd, &p); |
873 | } |
874 | return 0; |
875 | case WM_KEYDOWN: |
876 | { |
877 | int key = -1; |
878 | |
879 | switch (wParam) { |
880 | case VK_LEFT: key = CURSOR_LEFT; break; |
881 | case VK_RIGHT: key = CURSOR_RIGHT; break; |
882 | case VK_UP: key = CURSOR_UP; break; |
883 | case VK_DOWN: key = CURSOR_DOWN; break; |
c71454c0 |
884 | /* |
885 | * Diagonal keys on the numeric keypad. |
886 | */ |
887 | case VK_PRIOR: |
888 | if (!(lParam & 0x01000000)) key = CURSOR_UP_RIGHT; |
889 | break; |
890 | case VK_NEXT: |
891 | if (!(lParam & 0x01000000)) key = CURSOR_DOWN_RIGHT; |
892 | break; |
893 | case VK_HOME: |
894 | if (!(lParam & 0x01000000)) key = CURSOR_UP_LEFT; |
895 | break; |
896 | case VK_END: |
897 | if (!(lParam & 0x01000000)) key = CURSOR_DOWN_LEFT; |
898 | break; |
899 | /* |
900 | * Numeric keypad keys with Num Lock on. |
901 | */ |
902 | case VK_NUMPAD4: key = CURSOR_LEFT; break; |
903 | case VK_NUMPAD6: key = CURSOR_RIGHT; break; |
904 | case VK_NUMPAD8: key = CURSOR_UP; break; |
905 | case VK_NUMPAD2: key = CURSOR_DOWN; break; |
906 | case VK_NUMPAD9: key = CURSOR_UP_RIGHT; break; |
907 | case VK_NUMPAD3: key = CURSOR_DOWN_RIGHT; break; |
908 | case VK_NUMPAD7: key = CURSOR_UP_LEFT; break; |
909 | case VK_NUMPAD1: key = CURSOR_DOWN_LEFT; break; |
4e7ef6e6 |
910 | } |
911 | |
912 | if (key != -1) { |
eb2ad6f1 |
913 | if (!midend_process_key(fe->me, 0, 0, key)) |
4e7ef6e6 |
914 | PostQuitMessage(0); |
c71454c0 |
915 | } else { |
916 | MSG m; |
917 | m.hwnd = hwnd; |
918 | m.message = WM_KEYDOWN; |
919 | m.wParam = wParam; |
920 | m.lParam = lParam & 0xdfff; |
921 | TranslateMessage(&m); |
4e7ef6e6 |
922 | } |
923 | } |
924 | break; |
925 | case WM_LBUTTONDOWN: |
926 | case WM_RBUTTONDOWN: |
927 | case WM_MBUTTONDOWN: |
b0f06719 |
928 | { |
929 | int button; |
930 | |
931 | /* |
932 | * Shift-clicks count as middle-clicks, since otherwise |
933 | * two-button Windows users won't have any kind of |
934 | * middle click to use. |
935 | */ |
936 | if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT)) |
937 | button = MIDDLE_BUTTON; |
938 | else if (message == WM_LBUTTONDOWN) |
939 | button = LEFT_BUTTON; |
940 | else |
941 | button = RIGHT_BUTTON; |
74a4e547 |
942 | |
943 | if (!midend_process_key(fe->me, LOWORD(lParam), |
944 | HIWORD(lParam), button)) |
945 | PostQuitMessage(0); |
946 | |
947 | SetCapture(hwnd); |
948 | } |
949 | break; |
950 | case WM_LBUTTONUP: |
951 | case WM_RBUTTONUP: |
952 | case WM_MBUTTONUP: |
953 | { |
954 | int button; |
955 | |
956 | /* |
957 | * Shift-clicks count as middle-clicks, since otherwise |
958 | * two-button Windows users won't have any kind of |
959 | * middle click to use. |
960 | */ |
961 | if (message == WM_MBUTTONUP || (wParam & MK_SHIFT)) |
962 | button = MIDDLE_RELEASE; |
963 | else if (message == WM_LBUTTONUP) |
964 | button = LEFT_RELEASE; |
965 | else |
966 | button = RIGHT_RELEASE; |
967 | |
968 | if (!midend_process_key(fe->me, LOWORD(lParam), |
969 | HIWORD(lParam), button)) |
970 | PostQuitMessage(0); |
971 | |
972 | ReleaseCapture(); |
973 | } |
974 | break; |
975 | case WM_MOUSEMOVE: |
976 | { |
977 | int button; |
978 | |
979 | if (wParam & (MK_MBUTTON | MK_SHIFT)) |
980 | button = MIDDLE_DRAG; |
981 | else if (wParam & MK_LBUTTON) |
982 | button = LEFT_DRAG; |
983 | else |
984 | button = RIGHT_DRAG; |
985 | |
b0f06719 |
986 | if (!midend_process_key(fe->me, LOWORD(lParam), |
987 | HIWORD(lParam), button)) |
988 | PostQuitMessage(0); |
989 | } |
4e7ef6e6 |
990 | break; |
991 | case WM_CHAR: |
eb2ad6f1 |
992 | if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam)) |
4e7ef6e6 |
993 | PostQuitMessage(0); |
994 | return 0; |
995 | case WM_TIMER: |
8c1fd974 |
996 | if (fe->timer) { |
997 | DWORD now = GetTickCount(); |
998 | float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F; |
999 | midend_timer(fe->me, elapsed); |
1000 | fe->timer_last_tickcount = now; |
1001 | } |
4e7ef6e6 |
1002 | return 0; |
1003 | } |
1004 | |
1005 | return DefWindowProc(hwnd, message, wParam, lParam); |
1006 | } |
1007 | |
1008 | int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) |
1009 | { |
1010 | MSG msg; |
1011 | |
fd1a1a2b |
1012 | InitCommonControls(); |
1013 | |
4e7ef6e6 |
1014 | if (!prev) { |
1015 | WNDCLASS wndclass; |
1016 | |
1017 | wndclass.style = 0; |
1018 | wndclass.lpfnWndProc = WndProc; |
1019 | wndclass.cbClsExtra = 0; |
1020 | wndclass.cbWndExtra = 0; |
1021 | wndclass.hInstance = inst; |
1022 | wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION); |
1023 | wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); |
1024 | wndclass.hbrBackground = NULL; |
1025 | wndclass.lpszMenuName = NULL; |
0c490335 |
1026 | wndclass.lpszClassName = game_name; |
4e7ef6e6 |
1027 | |
1028 | RegisterClass(&wndclass); |
1029 | } |
1030 | |
1031 | new_window(inst); |
1032 | |
1033 | while (GetMessage(&msg, NULL, 0, 0)) { |
4e7ef6e6 |
1034 | DispatchMessage(&msg); |
1035 | } |
1036 | |
1037 | return msg.wParam; |
1038 | } |