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