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