| 1 | /* |
| 2 | * Mac OS X / Cocoa front end to puzzles. |
| 3 | * |
| 4 | * Still to do: |
| 5 | * |
| 6 | * - I'd like to be able to call up context help for a specific |
| 7 | * game at a time. |
| 8 | * |
| 9 | * Mac interface issues that possibly could be done better: |
| 10 | * |
| 11 | * - is there a better approach to frontend_default_colour? |
| 12 | * |
| 13 | * - do we need any more options in the Window menu? |
| 14 | * |
| 15 | * - can / should we be doing anything with the titles of the |
| 16 | * configuration boxes? |
| 17 | * |
| 18 | * - not sure what I should be doing about default window |
| 19 | * placement. Centring new windows is a bit feeble, but what's |
| 20 | * better? Is there a standard way to tell the OS "here's the |
| 21 | * _size_ of window I want, now use your best judgment about the |
| 22 | * initial position"? |
| 23 | * + there's a standard _policy_ on window placement, given in |
| 24 | * the HI guidelines. Have to implement it ourselves though, |
| 25 | * bah. |
| 26 | * |
| 27 | * - a brief frob of the Mac numeric keypad suggests that it |
| 28 | * generates numbers no matter what you do. I wonder if I should |
| 29 | * try to figure out a way of detecting keypad codes so I can |
| 30 | * implement UP_LEFT and friends. Alternatively, perhaps I |
| 31 | * should simply assign the number keys to UP_LEFT et al? |
| 32 | * They're not in use for anything else right now. |
| 33 | * |
| 34 | * - see if we can do anything to one-button-ise the multi-button |
| 35 | * dependent puzzle UIs: |
| 36 | * - Pattern is a _little_ unwieldy but not too bad (since |
| 37 | * generally you never need the middle button unless you've |
| 38 | * made a mistake, so it's just click versus command-click). |
| 39 | * - Net is utterly vile; having normal click be one rotate and |
| 40 | * command-click be the other introduces a horrid asymmetry, |
| 41 | * and yet requiring a shift key for _each_ click would be |
| 42 | * even worse because rotation feels as if it ought to be the |
| 43 | * default action. I fear this is why the Flash Net had the |
| 44 | * UI it did... |
| 45 | * + I've tried out an alternative dragging interface for |
| 46 | * Net; it might work nicely for stylus-based platforms |
| 47 | * where you have better hand/eye feedback for the thing |
| 48 | * you're clicking on, but it's rather unwieldy on the |
| 49 | * Mac. I fear even shift-clicking is better than that. |
| 50 | * |
| 51 | * - Should we _return_ to a game configuration sheet once an |
| 52 | * error is reported by midend_set_config, to allow the user to |
| 53 | * correct the one faulty input and keep the other five OK ones? |
| 54 | * The Apple `one sheet at a time' restriction would require me |
| 55 | * to do this by closing the config sheet, opening the alert |
| 56 | * sheet, and then reopening the config sheet when the alert is |
| 57 | * closed; and the human interface types, who presumably |
| 58 | * invented the one-sheet-at-a-time rule for good reasons, might |
| 59 | * look with disfavour on me trying to get round them to fake a |
| 60 | * nested sheet. On the other hand I think there are good |
| 61 | * practical reasons for wanting it that way. Uncertain. |
| 62 | * |
| 63 | * - User feedback dislikes nothing happening when you start the |
| 64 | * app; they suggest a finder-like window containing an icon for |
| 65 | * each puzzle type, enabling you to start one easily. Needs |
| 66 | * thought. |
| 67 | * |
| 68 | * Grotty implementation details that could probably be improved: |
| 69 | * |
| 70 | * - I am _utterly_ unconvinced that NSImageView was the right way |
| 71 | * to go about having a window with a reliable backing store! It |
| 72 | * just doesn't feel right; NSImageView is a _control_. Is there |
| 73 | * a simpler way? |
| 74 | * |
| 75 | * - Resizing is currently very bad; rather than bother to work |
| 76 | * out how to resize the NSImageView, I just splatter and |
| 77 | * recreate it. |
| 78 | */ |
| 79 | |
| 80 | #include <ctype.h> |
| 81 | #include <sys/time.h> |
| 82 | #import <Cocoa/Cocoa.h> |
| 83 | #include "puzzles.h" |
| 84 | |
| 85 | /* ---------------------------------------------------------------------- |
| 86 | * Global variables. |
| 87 | */ |
| 88 | |
| 89 | /* |
| 90 | * The `Type' menu. We frob this dynamically to allow the user to |
| 91 | * choose a preset set of settings from the current game. |
| 92 | */ |
| 93 | NSMenu *typemenu; |
| 94 | |
| 95 | /* ---------------------------------------------------------------------- |
| 96 | * Miscellaneous support routines that aren't part of any object or |
| 97 | * clearly defined subsystem. |
| 98 | */ |
| 99 | |
| 100 | void fatal(char *fmt, ...) |
| 101 | { |
| 102 | va_list ap; |
| 103 | char errorbuf[2048]; |
| 104 | NSAlert *alert; |
| 105 | |
| 106 | va_start(ap, fmt); |
| 107 | vsnprintf(errorbuf, lenof(errorbuf), fmt, ap); |
| 108 | va_end(ap); |
| 109 | |
| 110 | alert = [NSAlert alloc]; |
| 111 | /* |
| 112 | * We may have come here because we ran out of memory, in which |
| 113 | * case it's entirely likely that that alloc will fail, so we |
| 114 | * should have a fallback of some sort. |
| 115 | */ |
| 116 | if (!alert) { |
| 117 | fprintf(stderr, "fatal error (and NSAlert failed): %s\n", errorbuf); |
| 118 | } else { |
| 119 | alert = [[alert init] autorelease]; |
| 120 | [alert addButtonWithTitle:@"Oh dear"]; |
| 121 | [alert setInformativeText:[NSString stringWithCString:errorbuf]]; |
| 122 | [alert runModal]; |
| 123 | } |
| 124 | exit(1); |
| 125 | } |
| 126 | |
| 127 | void frontend_default_colour(frontend *fe, float *output) |
| 128 | { |
| 129 | /* FIXME: Is there a system default we can tap into for this? */ |
| 130 | output[0] = output[1] = output[2] = 0.8F; |
| 131 | } |
| 132 | |
| 133 | void get_random_seed(void **randseed, int *randseedsize) |
| 134 | { |
| 135 | time_t *tp = snew(time_t); |
| 136 | time(tp); |
| 137 | *randseed = (void *)tp; |
| 138 | *randseedsize = sizeof(time_t); |
| 139 | } |
| 140 | |
| 141 | /* ---------------------------------------------------------------------- |
| 142 | * Tiny extension to NSMenuItem which carries a payload of a `void |
| 143 | * *', allowing several menu items to invoke the same message but |
| 144 | * pass different data through it. |
| 145 | */ |
| 146 | @interface DataMenuItem : NSMenuItem |
| 147 | { |
| 148 | void *payload; |
| 149 | } |
| 150 | - (void)setPayload:(void *)d; |
| 151 | - (void *)getPayload; |
| 152 | @end |
| 153 | @implementation DataMenuItem |
| 154 | - (void)setPayload:(void *)d |
| 155 | { |
| 156 | payload = d; |
| 157 | } |
| 158 | - (void *)getPayload |
| 159 | { |
| 160 | return payload; |
| 161 | } |
| 162 | @end |
| 163 | |
| 164 | /* ---------------------------------------------------------------------- |
| 165 | * Utility routines for constructing OS X menus. |
| 166 | */ |
| 167 | |
| 168 | NSMenu *newmenu(const char *title) |
| 169 | { |
| 170 | return [[[NSMenu allocWithZone:[NSMenu menuZone]] |
| 171 | initWithTitle:[NSString stringWithCString:title]] |
| 172 | autorelease]; |
| 173 | } |
| 174 | |
| 175 | NSMenu *newsubmenu(NSMenu *parent, const char *title) |
| 176 | { |
| 177 | NSMenuItem *item; |
| 178 | NSMenu *child; |
| 179 | |
| 180 | item = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] |
| 181 | initWithTitle:[NSString stringWithCString:title] |
| 182 | action:NULL |
| 183 | keyEquivalent:@""] |
| 184 | autorelease]; |
| 185 | child = newmenu(title); |
| 186 | [item setEnabled:YES]; |
| 187 | [item setSubmenu:child]; |
| 188 | [parent addItem:item]; |
| 189 | return child; |
| 190 | } |
| 191 | |
| 192 | id initnewitem(NSMenuItem *item, NSMenu *parent, const char *title, |
| 193 | const char *key, id target, SEL action) |
| 194 | { |
| 195 | unsigned mask = NSCommandKeyMask; |
| 196 | |
| 197 | if (key[strcspn(key, "-")]) { |
| 198 | while (*key && *key != '-') { |
| 199 | int c = tolower((unsigned char)*key); |
| 200 | if (c == 's') { |
| 201 | mask |= NSShiftKeyMask; |
| 202 | } else if (c == 'o' || c == 'a') { |
| 203 | mask |= NSAlternateKeyMask; |
| 204 | } |
| 205 | key++; |
| 206 | } |
| 207 | if (*key) |
| 208 | key++; |
| 209 | } |
| 210 | |
| 211 | item = [[item initWithTitle:[NSString stringWithCString:title] |
| 212 | action:NULL |
| 213 | keyEquivalent:[NSString stringWithCString:key]] |
| 214 | autorelease]; |
| 215 | |
| 216 | if (*key) |
| 217 | [item setKeyEquivalentModifierMask: mask]; |
| 218 | |
| 219 | [item setEnabled:YES]; |
| 220 | [item setTarget:target]; |
| 221 | [item setAction:action]; |
| 222 | |
| 223 | [parent addItem:item]; |
| 224 | |
| 225 | return item; |
| 226 | } |
| 227 | |
| 228 | NSMenuItem *newitem(NSMenu *parent, char *title, char *key, |
| 229 | id target, SEL action) |
| 230 | { |
| 231 | return initnewitem([NSMenuItem allocWithZone:[NSMenu menuZone]], |
| 232 | parent, title, key, target, action); |
| 233 | } |
| 234 | |
| 235 | /* ---------------------------------------------------------------------- |
| 236 | * The front end presented to midend.c. |
| 237 | * |
| 238 | * This is mostly a subclass of NSWindow. The actual `frontend' |
| 239 | * structure passed to the midend contains a variety of pointers, |
| 240 | * including that window object but also including the image we |
| 241 | * draw on, an ImageView to display it in the window, and so on. |
| 242 | */ |
| 243 | |
| 244 | @class GameWindow; |
| 245 | @class MyImageView; |
| 246 | |
| 247 | struct frontend { |
| 248 | GameWindow *window; |
| 249 | NSImage *image; |
| 250 | MyImageView *view; |
| 251 | NSColor **colours; |
| 252 | int ncolours; |
| 253 | int clipped; |
| 254 | }; |
| 255 | |
| 256 | @interface MyImageView : NSImageView |
| 257 | { |
| 258 | GameWindow *ourwin; |
| 259 | } |
| 260 | - (void)setWindow:(GameWindow *)win; |
| 261 | - (BOOL)isFlipped; |
| 262 | - (void)mouseEvent:(NSEvent *)ev button:(int)b; |
| 263 | - (void)mouseDown:(NSEvent *)ev; |
| 264 | - (void)mouseDragged:(NSEvent *)ev; |
| 265 | - (void)mouseUp:(NSEvent *)ev; |
| 266 | - (void)rightMouseDown:(NSEvent *)ev; |
| 267 | - (void)rightMouseDragged:(NSEvent *)ev; |
| 268 | - (void)rightMouseUp:(NSEvent *)ev; |
| 269 | - (void)otherMouseDown:(NSEvent *)ev; |
| 270 | - (void)otherMouseDragged:(NSEvent *)ev; |
| 271 | - (void)otherMouseUp:(NSEvent *)ev; |
| 272 | @end |
| 273 | |
| 274 | @interface GameWindow : NSWindow |
| 275 | { |
| 276 | const game *ourgame; |
| 277 | midend_data *me; |
| 278 | struct frontend fe; |
| 279 | struct timeval last_time; |
| 280 | NSTimer *timer; |
| 281 | NSWindow *sheet; |
| 282 | config_item *cfg; |
| 283 | int cfg_which; |
| 284 | NSView **cfg_controls; |
| 285 | int cfg_ncontrols; |
| 286 | NSTextField *status; |
| 287 | } |
| 288 | - (id)initWithGame:(const game *)g; |
| 289 | - dealloc; |
| 290 | - (void)processButton:(int)b x:(int)x y:(int)y; |
| 291 | - (void)keyDown:(NSEvent *)ev; |
| 292 | - (void)activateTimer; |
| 293 | - (void)deactivateTimer; |
| 294 | - (void)setStatusLine:(NSString *)text; |
| 295 | @end |
| 296 | |
| 297 | @implementation MyImageView |
| 298 | |
| 299 | - (void)setWindow:(GameWindow *)win |
| 300 | { |
| 301 | ourwin = win; |
| 302 | } |
| 303 | |
| 304 | - (BOOL)isFlipped |
| 305 | { |
| 306 | return YES; |
| 307 | } |
| 308 | |
| 309 | - (void)mouseEvent:(NSEvent *)ev button:(int)b |
| 310 | { |
| 311 | NSPoint point = [self convertPoint:[ev locationInWindow] fromView:nil]; |
| 312 | [ourwin processButton:b x:point.x y:point.y]; |
| 313 | } |
| 314 | |
| 315 | - (void)mouseDown:(NSEvent *)ev |
| 316 | { |
| 317 | unsigned mod = [ev modifierFlags]; |
| 318 | [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_BUTTON : |
| 319 | (mod & NSShiftKeyMask) ? MIDDLE_BUTTON : |
| 320 | LEFT_BUTTON)]; |
| 321 | } |
| 322 | - (void)mouseDragged:(NSEvent *)ev |
| 323 | { |
| 324 | unsigned mod = [ev modifierFlags]; |
| 325 | [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_DRAG : |
| 326 | (mod & NSShiftKeyMask) ? MIDDLE_DRAG : |
| 327 | LEFT_DRAG)]; |
| 328 | } |
| 329 | - (void)mouseUp:(NSEvent *)ev |
| 330 | { |
| 331 | unsigned mod = [ev modifierFlags]; |
| 332 | [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_RELEASE : |
| 333 | (mod & NSShiftKeyMask) ? MIDDLE_RELEASE : |
| 334 | LEFT_RELEASE)]; |
| 335 | } |
| 336 | - (void)rightMouseDown:(NSEvent *)ev |
| 337 | { |
| 338 | unsigned mod = [ev modifierFlags]; |
| 339 | [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_BUTTON : |
| 340 | RIGHT_BUTTON)]; |
| 341 | } |
| 342 | - (void)rightMouseDragged:(NSEvent *)ev |
| 343 | { |
| 344 | unsigned mod = [ev modifierFlags]; |
| 345 | [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_DRAG : |
| 346 | RIGHT_DRAG)]; |
| 347 | } |
| 348 | - (void)rightMouseUp:(NSEvent *)ev |
| 349 | { |
| 350 | unsigned mod = [ev modifierFlags]; |
| 351 | [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_RELEASE : |
| 352 | RIGHT_RELEASE)]; |
| 353 | } |
| 354 | - (void)otherMouseDown:(NSEvent *)ev |
| 355 | { |
| 356 | [self mouseEvent:ev button:MIDDLE_BUTTON]; |
| 357 | } |
| 358 | - (void)otherMouseDragged:(NSEvent *)ev |
| 359 | { |
| 360 | [self mouseEvent:ev button:MIDDLE_DRAG]; |
| 361 | } |
| 362 | - (void)otherMouseUp:(NSEvent *)ev |
| 363 | { |
| 364 | [self mouseEvent:ev button:MIDDLE_RELEASE]; |
| 365 | } |
| 366 | @end |
| 367 | |
| 368 | @implementation GameWindow |
| 369 | - (void)setupContentView |
| 370 | { |
| 371 | NSRect frame; |
| 372 | int w, h; |
| 373 | |
| 374 | if (status) { |
| 375 | frame = [status frame]; |
| 376 | frame.origin.y = frame.size.height; |
| 377 | } else |
| 378 | frame.origin.y = 0; |
| 379 | frame.origin.x = 0; |
| 380 | |
| 381 | midend_size(me, &w, &h); |
| 382 | frame.size.width = w; |
| 383 | frame.size.height = h; |
| 384 | |
| 385 | fe.image = [[NSImage alloc] initWithSize:frame.size]; |
| 386 | [fe.image setFlipped:YES]; |
| 387 | fe.view = [[MyImageView alloc] initWithFrame:frame]; |
| 388 | [fe.view setImage:fe.image]; |
| 389 | [fe.view setWindow:self]; |
| 390 | |
| 391 | midend_redraw(me); |
| 392 | |
| 393 | [[self contentView] addSubview:fe.view]; |
| 394 | } |
| 395 | - (id)initWithGame:(const game *)g |
| 396 | { |
| 397 | NSRect rect = { {0,0}, {0,0} }, rect2; |
| 398 | int w, h; |
| 399 | |
| 400 | ourgame = g; |
| 401 | |
| 402 | fe.window = self; |
| 403 | |
| 404 | me = midend_new(&fe, ourgame); |
| 405 | /* |
| 406 | * If we ever need to open a fresh window using a provided game |
| 407 | * ID, I think the right thing is to move most of this method |
| 408 | * into a new initWithGame:gameID: method, and have |
| 409 | * initWithGame: simply call that one and pass it NULL. |
| 410 | */ |
| 411 | midend_new_game(me); |
| 412 | midend_size(me, &w, &h); |
| 413 | rect.size.width = w; |
| 414 | rect.size.height = h; |
| 415 | |
| 416 | /* |
| 417 | * Create the status bar, which will just be an NSTextField. |
| 418 | */ |
| 419 | if (ourgame->wants_statusbar()) { |
| 420 | status = [[NSTextField alloc] initWithFrame:NSMakeRect(0,0,100,50)]; |
| 421 | [status setEditable:NO]; |
| 422 | [status setSelectable:NO]; |
| 423 | [status setBordered:YES]; |
| 424 | [status setBezeled:YES]; |
| 425 | [status setBezelStyle:NSTextFieldSquareBezel]; |
| 426 | [status setDrawsBackground:YES]; |
| 427 | [[status cell] setTitle:@""]; |
| 428 | [status sizeToFit]; |
| 429 | rect2 = [status frame]; |
| 430 | rect.size.height += rect2.size.height; |
| 431 | rect2.size.width = rect.size.width; |
| 432 | rect2.origin.x = rect2.origin.y = 0; |
| 433 | [status setFrame:rect2]; |
| 434 | } else |
| 435 | status = nil; |
| 436 | |
| 437 | self = [super initWithContentRect:rect |
| 438 | styleMask:(NSTitledWindowMask | NSMiniaturizableWindowMask | |
| 439 | NSClosableWindowMask) |
| 440 | backing:NSBackingStoreBuffered |
| 441 | defer:YES]; |
| 442 | [self setTitle:[NSString stringWithCString:ourgame->name]]; |
| 443 | |
| 444 | { |
| 445 | float *colours; |
| 446 | int i, ncolours; |
| 447 | |
| 448 | colours = midend_colours(me, &ncolours); |
| 449 | fe.ncolours = ncolours; |
| 450 | fe.colours = snewn(ncolours, NSColor *); |
| 451 | |
| 452 | for (i = 0; i < ncolours; i++) { |
| 453 | fe.colours[i] = [[NSColor colorWithDeviceRed:colours[i*3] |
| 454 | green:colours[i*3+1] blue:colours[i*3+2] |
| 455 | alpha:1.0] retain]; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | [self setupContentView]; |
| 460 | if (status) |
| 461 | [[self contentView] addSubview:status]; |
| 462 | [self setIgnoresMouseEvents:NO]; |
| 463 | |
| 464 | [self center]; /* :-) */ |
| 465 | |
| 466 | return self; |
| 467 | } |
| 468 | |
| 469 | - dealloc |
| 470 | { |
| 471 | int i; |
| 472 | for (i = 0; i < fe.ncolours; i++) { |
| 473 | [fe.colours[i] release]; |
| 474 | } |
| 475 | sfree(fe.colours); |
| 476 | midend_free(me); |
| 477 | return [super dealloc]; |
| 478 | } |
| 479 | |
| 480 | - (void)processButton:(int)b x:(int)x y:(int)y |
| 481 | { |
| 482 | if (!midend_process_key(me, x, y, b)) |
| 483 | [self close]; |
| 484 | } |
| 485 | |
| 486 | - (void)keyDown:(NSEvent *)ev |
| 487 | { |
| 488 | NSString *s = [ev characters]; |
| 489 | int i, n = [s length]; |
| 490 | |
| 491 | for (i = 0; i < n; i++) { |
| 492 | int c = [s characterAtIndex:i]; |
| 493 | |
| 494 | /* |
| 495 | * ASCII gets passed straight to midend_process_key. |
| 496 | * Anything above that has to be translated to our own |
| 497 | * function key codes. |
| 498 | */ |
| 499 | if (c >= 0x80) { |
| 500 | switch (c) { |
| 501 | case NSUpArrowFunctionKey: |
| 502 | c = CURSOR_UP; |
| 503 | break; |
| 504 | case NSDownArrowFunctionKey: |
| 505 | c = CURSOR_DOWN; |
| 506 | break; |
| 507 | case NSLeftArrowFunctionKey: |
| 508 | c = CURSOR_LEFT; |
| 509 | break; |
| 510 | case NSRightArrowFunctionKey: |
| 511 | c = CURSOR_RIGHT; |
| 512 | break; |
| 513 | default: |
| 514 | continue; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | [self processButton:c x:-1 y:-1]; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | - (void)activateTimer |
| 523 | { |
| 524 | if (timer != nil) |
| 525 | return; |
| 526 | |
| 527 | timer = [NSTimer scheduledTimerWithTimeInterval:0.02 |
| 528 | target:self selector:@selector(timerTick:) |
| 529 | userInfo:nil repeats:YES]; |
| 530 | gettimeofday(&last_time, NULL); |
| 531 | } |
| 532 | |
| 533 | - (void)deactivateTimer |
| 534 | { |
| 535 | if (timer == nil) |
| 536 | return; |
| 537 | |
| 538 | [timer invalidate]; |
| 539 | timer = nil; |
| 540 | } |
| 541 | |
| 542 | - (void)timerTick:(id)sender |
| 543 | { |
| 544 | struct timeval now; |
| 545 | float elapsed; |
| 546 | gettimeofday(&now, NULL); |
| 547 | elapsed = ((now.tv_usec - last_time.tv_usec) * 0.000001F + |
| 548 | (now.tv_sec - last_time.tv_sec)); |
| 549 | midend_timer(me, elapsed); |
| 550 | last_time = now; |
| 551 | } |
| 552 | |
| 553 | - (void)newGame:(id)sender |
| 554 | { |
| 555 | [self processButton:'n' x:-1 y:-1]; |
| 556 | } |
| 557 | - (void)restartGame:(id)sender |
| 558 | { |
| 559 | [self processButton:'r' x:-1 y:-1]; |
| 560 | } |
| 561 | - (void)undoMove:(id)sender |
| 562 | { |
| 563 | [self processButton:'u' x:-1 y:-1]; |
| 564 | } |
| 565 | - (void)redoMove:(id)sender |
| 566 | { |
| 567 | [self processButton:'r'&0x1F x:-1 y:-1]; |
| 568 | } |
| 569 | |
| 570 | - (void)copy:(id)sender |
| 571 | { |
| 572 | char *text; |
| 573 | |
| 574 | if ((text = midend_text_format(me)) != NULL) { |
| 575 | NSPasteboard *pb = [NSPasteboard generalPasteboard]; |
| 576 | NSArray *a = [NSArray arrayWithObject:NSStringPboardType]; |
| 577 | [pb declareTypes:a owner:nil]; |
| 578 | [pb setString:[NSString stringWithCString:text] |
| 579 | forType:NSStringPboardType]; |
| 580 | } else |
| 581 | NSBeep(); |
| 582 | } |
| 583 | |
| 584 | - (void)solveGame:(id)sender |
| 585 | { |
| 586 | char *msg; |
| 587 | NSAlert *alert; |
| 588 | |
| 589 | msg = midend_solve(me); |
| 590 | |
| 591 | if (msg) { |
| 592 | alert = [[[NSAlert alloc] init] autorelease]; |
| 593 | [alert addButtonWithTitle:@"Bah"]; |
| 594 | [alert setInformativeText:[NSString stringWithCString:msg]]; |
| 595 | [alert beginSheetModalForWindow:self modalDelegate:nil |
| 596 | didEndSelector:nil contextInfo:nil]; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | - (BOOL)validateMenuItem:(NSMenuItem *)item |
| 601 | { |
| 602 | if ([item action] == @selector(copy:)) |
| 603 | return (ourgame->can_format_as_text ? YES : NO); |
| 604 | else if ([item action] == @selector(solveGame:)) |
| 605 | return (ourgame->can_solve ? YES : NO); |
| 606 | else |
| 607 | return [super validateMenuItem:item]; |
| 608 | } |
| 609 | |
| 610 | - (void)clearTypeMenu |
| 611 | { |
| 612 | while ([typemenu numberOfItems] > 1) |
| 613 | [typemenu removeItemAtIndex:0]; |
| 614 | } |
| 615 | |
| 616 | - (void)becomeKeyWindow |
| 617 | { |
| 618 | int n; |
| 619 | |
| 620 | [self clearTypeMenu]; |
| 621 | |
| 622 | [super becomeKeyWindow]; |
| 623 | |
| 624 | n = midend_num_presets(me); |
| 625 | |
| 626 | if (n > 0) { |
| 627 | [typemenu insertItem:[NSMenuItem separatorItem] atIndex:0]; |
| 628 | while (n--) { |
| 629 | char *name; |
| 630 | game_params *params; |
| 631 | DataMenuItem *item; |
| 632 | |
| 633 | midend_fetch_preset(me, n, &name, ¶ms); |
| 634 | |
| 635 | item = [[[DataMenuItem alloc] |
| 636 | initWithTitle:[NSString stringWithCString:name] |
| 637 | action:NULL keyEquivalent:@""] |
| 638 | autorelease]; |
| 639 | |
| 640 | [item setEnabled:YES]; |
| 641 | [item setTarget:self]; |
| 642 | [item setAction:@selector(presetGame:)]; |
| 643 | [item setPayload:params]; |
| 644 | |
| 645 | [typemenu insertItem:item atIndex:0]; |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | - (void)resignKeyWindow |
| 651 | { |
| 652 | [self clearTypeMenu]; |
| 653 | [super resignKeyWindow]; |
| 654 | } |
| 655 | |
| 656 | - (void)close |
| 657 | { |
| 658 | [self clearTypeMenu]; |
| 659 | [super close]; |
| 660 | } |
| 661 | |
| 662 | - (void)resizeForNewGameParams |
| 663 | { |
| 664 | NSSize size = {0,0}; |
| 665 | int w, h; |
| 666 | |
| 667 | midend_size(me, &w, &h); |
| 668 | size.width = w; |
| 669 | size.height = h; |
| 670 | |
| 671 | if (status) { |
| 672 | NSRect frame = [status frame]; |
| 673 | size.height += frame.size.height; |
| 674 | frame.size.width = size.width; |
| 675 | [status setFrame:frame]; |
| 676 | } |
| 677 | |
| 678 | NSDisableScreenUpdates(); |
| 679 | [self setContentSize:size]; |
| 680 | [self setupContentView]; |
| 681 | NSEnableScreenUpdates(); |
| 682 | } |
| 683 | |
| 684 | - (void)presetGame:(id)sender |
| 685 | { |
| 686 | game_params *params = [sender getPayload]; |
| 687 | |
| 688 | midend_set_params(me, params); |
| 689 | midend_new_game(me); |
| 690 | |
| 691 | [self resizeForNewGameParams]; |
| 692 | } |
| 693 | |
| 694 | - (void)startConfigureSheet:(int)which |
| 695 | { |
| 696 | NSButton *ok, *cancel; |
| 697 | int actw, acth, leftw, rightw, totalw, h, thish, y; |
| 698 | int k; |
| 699 | NSRect rect, tmprect; |
| 700 | const int SPACING = 16; |
| 701 | char *title; |
| 702 | config_item *i; |
| 703 | int cfg_controlsize; |
| 704 | NSTextField *tf; |
| 705 | NSButton *b; |
| 706 | NSPopUpButton *pb; |
| 707 | |
| 708 | assert(sheet == NULL); |
| 709 | |
| 710 | /* |
| 711 | * Every control we create here is going to have this size |
| 712 | * until we tell it to calculate a better one. |
| 713 | */ |
| 714 | tmprect = NSMakeRect(0, 0, 100, 50); |
| 715 | |
| 716 | /* |
| 717 | * Set up OK and Cancel buttons. (Actually, MacOS doesn't seem |
| 718 | * to be fond of generic OK and Cancel wording, so I'm going to |
| 719 | * rename them to something nicer.) |
| 720 | */ |
| 721 | actw = acth = 0; |
| 722 | |
| 723 | cancel = [[NSButton alloc] initWithFrame:tmprect]; |
| 724 | [cancel setBezelStyle:NSRoundedBezelStyle]; |
| 725 | [cancel setTitle:@"Abandon"]; |
| 726 | [cancel setTarget:self]; |
| 727 | [cancel setKeyEquivalent:@"\033"]; |
| 728 | [cancel setAction:@selector(sheetCancelButton:)]; |
| 729 | [cancel sizeToFit]; |
| 730 | rect = [cancel frame]; |
| 731 | if (actw < rect.size.width) actw = rect.size.width; |
| 732 | if (acth < rect.size.height) acth = rect.size.height; |
| 733 | |
| 734 | ok = [[NSButton alloc] initWithFrame:tmprect]; |
| 735 | [ok setBezelStyle:NSRoundedBezelStyle]; |
| 736 | [ok setTitle:@"Accept"]; |
| 737 | [ok setTarget:self]; |
| 738 | [ok setKeyEquivalent:@"\r"]; |
| 739 | [ok setAction:@selector(sheetOKButton:)]; |
| 740 | [ok sizeToFit]; |
| 741 | rect = [ok frame]; |
| 742 | if (actw < rect.size.width) actw = rect.size.width; |
| 743 | if (acth < rect.size.height) acth = rect.size.height; |
| 744 | |
| 745 | totalw = SPACING + 2 * actw; |
| 746 | h = 2 * SPACING + acth; |
| 747 | |
| 748 | /* |
| 749 | * Now fetch the midend config data and go through it creating |
| 750 | * controls. |
| 751 | */ |
| 752 | cfg = midend_get_config(me, which, &title); |
| 753 | sfree(title); /* FIXME: should we use this somehow? */ |
| 754 | cfg_which = which; |
| 755 | |
| 756 | cfg_ncontrols = cfg_controlsize = 0; |
| 757 | cfg_controls = NULL; |
| 758 | leftw = rightw = 0; |
| 759 | for (i = cfg; i->type != C_END; i++) { |
| 760 | if (cfg_controlsize < cfg_ncontrols + 5) { |
| 761 | cfg_controlsize = cfg_ncontrols + 32; |
| 762 | cfg_controls = sresize(cfg_controls, cfg_controlsize, NSView *); |
| 763 | } |
| 764 | |
| 765 | thish = 0; |
| 766 | |
| 767 | switch (i->type) { |
| 768 | case C_STRING: |
| 769 | /* |
| 770 | * Two NSTextFields, one being a label and the other |
| 771 | * being an edit box. |
| 772 | */ |
| 773 | |
| 774 | tf = [[NSTextField alloc] initWithFrame:tmprect]; |
| 775 | [tf setEditable:NO]; |
| 776 | [tf setSelectable:NO]; |
| 777 | [tf setBordered:NO]; |
| 778 | [tf setDrawsBackground:NO]; |
| 779 | [[tf cell] setTitle:[NSString stringWithCString:i->name]]; |
| 780 | [tf sizeToFit]; |
| 781 | rect = [tf frame]; |
| 782 | if (thish < rect.size.height + 1) thish = rect.size.height + 1; |
| 783 | if (leftw < rect.size.width + 1) leftw = rect.size.width + 1; |
| 784 | cfg_controls[cfg_ncontrols++] = tf; |
| 785 | |
| 786 | tf = [[NSTextField alloc] initWithFrame:tmprect]; |
| 787 | [tf setEditable:YES]; |
| 788 | [tf setSelectable:YES]; |
| 789 | [tf setBordered:YES]; |
| 790 | [[tf cell] setTitle:[NSString stringWithCString:i->sval]]; |
| 791 | [tf sizeToFit]; |
| 792 | rect = [tf frame]; |
| 793 | /* |
| 794 | * We impose a minimum and maximum width on editable |
| 795 | * NSTextFields. If we allow them to size themselves to |
| 796 | * the contents of the text within them, then they will |
| 797 | * look very silly if that text is only one or two |
| 798 | * characters, and equally silly if it's an absolutely |
| 799 | * enormous Rectangles or Pattern game ID! |
| 800 | */ |
| 801 | if (rect.size.width < 75) rect.size.width = 75; |
| 802 | if (rect.size.width > 400) rect.size.width = 400; |
| 803 | |
| 804 | if (thish < rect.size.height + 1) thish = rect.size.height + 1; |
| 805 | if (rightw < rect.size.width + 1) rightw = rect.size.width + 1; |
| 806 | cfg_controls[cfg_ncontrols++] = tf; |
| 807 | break; |
| 808 | |
| 809 | case C_BOOLEAN: |
| 810 | /* |
| 811 | * A checkbox is an NSButton with a type of |
| 812 | * NSSwitchButton. |
| 813 | */ |
| 814 | b = [[NSButton alloc] initWithFrame:tmprect]; |
| 815 | [b setBezelStyle:NSRoundedBezelStyle]; |
| 816 | [b setButtonType:NSSwitchButton]; |
| 817 | [b setTitle:[NSString stringWithCString:i->name]]; |
| 818 | [b sizeToFit]; |
| 819 | [b setState:(i->ival ? NSOnState : NSOffState)]; |
| 820 | rect = [b frame]; |
| 821 | if (totalw < rect.size.width + 1) totalw = rect.size.width + 1; |
| 822 | if (thish < rect.size.height + 1) thish = rect.size.height + 1; |
| 823 | cfg_controls[cfg_ncontrols++] = b; |
| 824 | break; |
| 825 | |
| 826 | case C_CHOICES: |
| 827 | /* |
| 828 | * A pop-up menu control is an NSPopUpButton, which |
| 829 | * takes an embedded NSMenu. We also need an |
| 830 | * NSTextField to act as a label. |
| 831 | */ |
| 832 | |
| 833 | tf = [[NSTextField alloc] initWithFrame:tmprect]; |
| 834 | [tf setEditable:NO]; |
| 835 | [tf setSelectable:NO]; |
| 836 | [tf setBordered:NO]; |
| 837 | [tf setDrawsBackground:NO]; |
| 838 | [[tf cell] setTitle:[NSString stringWithCString:i->name]]; |
| 839 | [tf sizeToFit]; |
| 840 | rect = [tf frame]; |
| 841 | if (thish < rect.size.height + 1) thish = rect.size.height + 1; |
| 842 | if (leftw < rect.size.width + 1) leftw = rect.size.width + 1; |
| 843 | cfg_controls[cfg_ncontrols++] = tf; |
| 844 | |
| 845 | pb = [[NSPopUpButton alloc] initWithFrame:tmprect pullsDown:NO]; |
| 846 | [pb setBezelStyle:NSRoundedBezelStyle]; |
| 847 | { |
| 848 | char c, *p; |
| 849 | |
| 850 | p = i->sval; |
| 851 | c = *p++; |
| 852 | while (*p) { |
| 853 | char *q; |
| 854 | |
| 855 | q = p; |
| 856 | while (*p && *p != c) p++; |
| 857 | |
| 858 | [pb addItemWithTitle:[NSString stringWithCString:q |
| 859 | length:p-q]]; |
| 860 | |
| 861 | if (*p) p++; |
| 862 | } |
| 863 | } |
| 864 | [pb selectItemAtIndex:i->ival]; |
| 865 | [pb sizeToFit]; |
| 866 | |
| 867 | rect = [pb frame]; |
| 868 | if (rightw < rect.size.width + 1) rightw = rect.size.width + 1; |
| 869 | if (thish < rect.size.height + 1) thish = rect.size.height + 1; |
| 870 | cfg_controls[cfg_ncontrols++] = pb; |
| 871 | break; |
| 872 | } |
| 873 | |
| 874 | h += SPACING + thish; |
| 875 | } |
| 876 | |
| 877 | if (totalw < leftw + SPACING + rightw) |
| 878 | totalw = leftw + SPACING + rightw; |
| 879 | if (totalw > leftw + SPACING + rightw) { |
| 880 | int excess = totalw - (leftw + SPACING + rightw); |
| 881 | int leftexcess = leftw * excess / (leftw + rightw); |
| 882 | int rightexcess = excess - leftexcess; |
| 883 | leftw += leftexcess; |
| 884 | rightw += rightexcess; |
| 885 | } |
| 886 | |
| 887 | /* |
| 888 | * Now go through the list again, setting the final position |
| 889 | * for each control. |
| 890 | */ |
| 891 | k = 0; |
| 892 | y = h; |
| 893 | for (i = cfg; i->type != C_END; i++) { |
| 894 | y -= SPACING; |
| 895 | thish = 0; |
| 896 | switch (i->type) { |
| 897 | case C_STRING: |
| 898 | case C_CHOICES: |
| 899 | /* |
| 900 | * These two are treated identically, since both expect |
| 901 | * a control on the left and another on the right. |
| 902 | */ |
| 903 | rect = [cfg_controls[k] frame]; |
| 904 | if (thish < rect.size.height + 1) |
| 905 | thish = rect.size.height + 1; |
| 906 | rect = [cfg_controls[k+1] frame]; |
| 907 | if (thish < rect.size.height + 1) |
| 908 | thish = rect.size.height + 1; |
| 909 | rect = [cfg_controls[k] frame]; |
| 910 | rect.origin.y = y - thish/2 - rect.size.height/2; |
| 911 | rect.origin.x = SPACING; |
| 912 | rect.size.width = leftw; |
| 913 | [cfg_controls[k] setFrame:rect]; |
| 914 | rect = [cfg_controls[k+1] frame]; |
| 915 | rect.origin.y = y - thish/2 - rect.size.height/2; |
| 916 | rect.origin.x = 2 * SPACING + leftw; |
| 917 | rect.size.width = rightw; |
| 918 | [cfg_controls[k+1] setFrame:rect]; |
| 919 | k += 2; |
| 920 | break; |
| 921 | |
| 922 | case C_BOOLEAN: |
| 923 | rect = [cfg_controls[k] frame]; |
| 924 | if (thish < rect.size.height + 1) |
| 925 | thish = rect.size.height + 1; |
| 926 | rect.origin.y = y - thish/2 - rect.size.height/2; |
| 927 | rect.origin.x = SPACING; |
| 928 | rect.size.width = totalw; |
| 929 | [cfg_controls[k] setFrame:rect]; |
| 930 | k++; |
| 931 | break; |
| 932 | } |
| 933 | y -= thish; |
| 934 | } |
| 935 | |
| 936 | assert(k == cfg_ncontrols); |
| 937 | |
| 938 | [cancel setFrame:NSMakeRect(SPACING+totalw/4-actw/2, SPACING, actw, acth)]; |
| 939 | [ok setFrame:NSMakeRect(SPACING+3*totalw/4-actw/2, SPACING, actw, acth)]; |
| 940 | |
| 941 | sheet = [[NSWindow alloc] |
| 942 | initWithContentRect:NSMakeRect(0,0,totalw + 2*SPACING,h) |
| 943 | styleMask:NSTitledWindowMask | NSClosableWindowMask |
| 944 | backing:NSBackingStoreBuffered |
| 945 | defer:YES]; |
| 946 | |
| 947 | [[sheet contentView] addSubview:cancel]; |
| 948 | [[sheet contentView] addSubview:ok]; |
| 949 | |
| 950 | for (k = 0; k < cfg_ncontrols; k++) |
| 951 | [[sheet contentView] addSubview:cfg_controls[k]]; |
| 952 | |
| 953 | [NSApp beginSheet:sheet modalForWindow:self |
| 954 | modalDelegate:nil didEndSelector:nil contextInfo:nil]; |
| 955 | } |
| 956 | |
| 957 | - (void)specificGame:(id)sender |
| 958 | { |
| 959 | [self startConfigureSheet:CFG_SEED]; |
| 960 | } |
| 961 | |
| 962 | - (void)customGameType:(id)sender |
| 963 | { |
| 964 | [self startConfigureSheet:CFG_SETTINGS]; |
| 965 | } |
| 966 | |
| 967 | - (void)sheetEndWithStatus:(BOOL)update |
| 968 | { |
| 969 | assert(sheet != NULL); |
| 970 | [NSApp endSheet:sheet]; |
| 971 | [sheet orderOut:self]; |
| 972 | sheet = NULL; |
| 973 | if (update) { |
| 974 | int k; |
| 975 | config_item *i; |
| 976 | char *error; |
| 977 | |
| 978 | k = 0; |
| 979 | for (i = cfg; i->type != C_END; i++) { |
| 980 | switch (i->type) { |
| 981 | case C_STRING: |
| 982 | sfree(i->sval); |
| 983 | i->sval = dupstr([[[(id)cfg_controls[k+1] cell] |
| 984 | title] cString]); |
| 985 | k += 2; |
| 986 | break; |
| 987 | case C_BOOLEAN: |
| 988 | i->ival = [(id)cfg_controls[k] state] == NSOnState; |
| 989 | k++; |
| 990 | break; |
| 991 | case C_CHOICES: |
| 992 | i->ival = [(id)cfg_controls[k+1] indexOfSelectedItem]; |
| 993 | k += 2; |
| 994 | break; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | error = midend_set_config(me, cfg_which, cfg); |
| 999 | if (error) { |
| 1000 | NSAlert *alert = [[[NSAlert alloc] init] autorelease]; |
| 1001 | [alert addButtonWithTitle:@"Bah"]; |
| 1002 | [alert setInformativeText:[NSString stringWithCString:error]]; |
| 1003 | [alert beginSheetModalForWindow:self modalDelegate:nil |
| 1004 | didEndSelector:nil contextInfo:nil]; |
| 1005 | } else { |
| 1006 | midend_new_game(me); |
| 1007 | [self resizeForNewGameParams]; |
| 1008 | } |
| 1009 | } |
| 1010 | sfree(cfg_controls); |
| 1011 | cfg_controls = NULL; |
| 1012 | } |
| 1013 | - (void)sheetOKButton:(id)sender |
| 1014 | { |
| 1015 | [self sheetEndWithStatus:YES]; |
| 1016 | } |
| 1017 | - (void)sheetCancelButton:(id)sender |
| 1018 | { |
| 1019 | [self sheetEndWithStatus:NO]; |
| 1020 | } |
| 1021 | |
| 1022 | - (void)setStatusLine:(NSString *)text |
| 1023 | { |
| 1024 | [[status cell] setTitle:text]; |
| 1025 | } |
| 1026 | |
| 1027 | @end |
| 1028 | |
| 1029 | /* |
| 1030 | * Drawing routines called by the midend. |
| 1031 | */ |
| 1032 | void draw_polygon(frontend *fe, int *coords, int npoints, |
| 1033 | int fill, int colour) |
| 1034 | { |
| 1035 | NSBezierPath *path = [NSBezierPath bezierPath]; |
| 1036 | int i; |
| 1037 | |
| 1038 | [[NSGraphicsContext currentContext] setShouldAntialias:YES]; |
| 1039 | |
| 1040 | assert(colour >= 0 && colour < fe->ncolours); |
| 1041 | [fe->colours[colour] set]; |
| 1042 | |
| 1043 | for (i = 0; i < npoints; i++) { |
| 1044 | NSPoint p = { coords[i*2] + 0.5, coords[i*2+1] + 0.5 }; |
| 1045 | if (i == 0) |
| 1046 | [path moveToPoint:p]; |
| 1047 | else |
| 1048 | [path lineToPoint:p]; |
| 1049 | } |
| 1050 | |
| 1051 | [path closePath]; |
| 1052 | |
| 1053 | if (fill) |
| 1054 | [path fill]; |
| 1055 | else |
| 1056 | [path stroke]; |
| 1057 | } |
| 1058 | void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour) |
| 1059 | { |
| 1060 | NSBezierPath *path = [NSBezierPath bezierPath]; |
| 1061 | NSPoint p1 = { x1 + 0.5, y1 + 0.5 }, p2 = { x2 + 0.5, y2 + 0.5 }; |
| 1062 | |
| 1063 | [[NSGraphicsContext currentContext] setShouldAntialias:NO]; |
| 1064 | |
| 1065 | assert(colour >= 0 && colour < fe->ncolours); |
| 1066 | [fe->colours[colour] set]; |
| 1067 | |
| 1068 | [path moveToPoint:p1]; |
| 1069 | [path lineToPoint:p2]; |
| 1070 | [path stroke]; |
| 1071 | } |
| 1072 | void draw_rect(frontend *fe, int x, int y, int w, int h, int colour) |
| 1073 | { |
| 1074 | NSRect r = { {x,y}, {w,h} }; |
| 1075 | |
| 1076 | [[NSGraphicsContext currentContext] setShouldAntialias:NO]; |
| 1077 | |
| 1078 | assert(colour >= 0 && colour < fe->ncolours); |
| 1079 | [fe->colours[colour] set]; |
| 1080 | |
| 1081 | NSRectFill(r); |
| 1082 | } |
| 1083 | void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize, |
| 1084 | int align, int colour, char *text) |
| 1085 | { |
| 1086 | NSString *string = [NSString stringWithCString:text]; |
| 1087 | NSDictionary *attr; |
| 1088 | NSFont *font; |
| 1089 | NSSize size; |
| 1090 | NSPoint point; |
| 1091 | |
| 1092 | [[NSGraphicsContext currentContext] setShouldAntialias:YES]; |
| 1093 | |
| 1094 | assert(colour >= 0 && colour < fe->ncolours); |
| 1095 | |
| 1096 | if (fonttype == FONT_FIXED) |
| 1097 | font = [NSFont userFixedPitchFontOfSize:fontsize]; |
| 1098 | else |
| 1099 | font = [NSFont userFontOfSize:fontsize]; |
| 1100 | |
| 1101 | attr = [NSDictionary dictionaryWithObjectsAndKeys: |
| 1102 | fe->colours[colour], NSForegroundColorAttributeName, |
| 1103 | font, NSFontAttributeName, nil]; |
| 1104 | |
| 1105 | point.x = x; |
| 1106 | point.y = y; |
| 1107 | |
| 1108 | size = [string sizeWithAttributes:attr]; |
| 1109 | if (align & ALIGN_HRIGHT) |
| 1110 | point.x -= size.width; |
| 1111 | else if (align & ALIGN_HCENTRE) |
| 1112 | point.x -= size.width / 2; |
| 1113 | if (align & ALIGN_VCENTRE) |
| 1114 | point.y -= size.height / 2; |
| 1115 | |
| 1116 | [string drawAtPoint:point withAttributes:attr]; |
| 1117 | } |
| 1118 | void draw_update(frontend *fe, int x, int y, int w, int h) |
| 1119 | { |
| 1120 | [fe->view setNeedsDisplayInRect:NSMakeRect(x,y,w,h)]; |
| 1121 | } |
| 1122 | void clip(frontend *fe, int x, int y, int w, int h) |
| 1123 | { |
| 1124 | NSRect r = { {x,y}, {w,h} }; |
| 1125 | |
| 1126 | if (!fe->clipped) |
| 1127 | [[NSGraphicsContext currentContext] saveGraphicsState]; |
| 1128 | [NSBezierPath clipRect:r]; |
| 1129 | fe->clipped = TRUE; |
| 1130 | } |
| 1131 | void unclip(frontend *fe) |
| 1132 | { |
| 1133 | if (fe->clipped) |
| 1134 | [[NSGraphicsContext currentContext] restoreGraphicsState]; |
| 1135 | fe->clipped = FALSE; |
| 1136 | } |
| 1137 | void start_draw(frontend *fe) |
| 1138 | { |
| 1139 | [fe->image lockFocus]; |
| 1140 | fe->clipped = FALSE; |
| 1141 | } |
| 1142 | void end_draw(frontend *fe) |
| 1143 | { |
| 1144 | [fe->image unlockFocus]; |
| 1145 | } |
| 1146 | |
| 1147 | void deactivate_timer(frontend *fe) |
| 1148 | { |
| 1149 | [fe->window deactivateTimer]; |
| 1150 | } |
| 1151 | void activate_timer(frontend *fe) |
| 1152 | { |
| 1153 | [fe->window activateTimer]; |
| 1154 | } |
| 1155 | |
| 1156 | void status_bar(frontend *fe, char *text) |
| 1157 | { |
| 1158 | [fe->window setStatusLine:[NSString stringWithCString:text]]; |
| 1159 | } |
| 1160 | |
| 1161 | /* ---------------------------------------------------------------------- |
| 1162 | * AppController: the object which receives the messages from all |
| 1163 | * menu selections that aren't standard OS X functions. |
| 1164 | */ |
| 1165 | @interface AppController : NSObject |
| 1166 | { |
| 1167 | } |
| 1168 | - (void)newGameWindow:(id)sender; |
| 1169 | @end |
| 1170 | |
| 1171 | @implementation AppController |
| 1172 | |
| 1173 | - (void)newGameWindow:(id)sender |
| 1174 | { |
| 1175 | const game *g = [sender getPayload]; |
| 1176 | id win; |
| 1177 | |
| 1178 | win = [[GameWindow alloc] initWithGame:g]; |
| 1179 | [win makeKeyAndOrderFront:self]; |
| 1180 | } |
| 1181 | |
| 1182 | - (NSMenu *)applicationDockMenu:(NSApplication *)sender |
| 1183 | { |
| 1184 | NSMenu *menu = newmenu("Dock Menu"); |
| 1185 | { |
| 1186 | int i; |
| 1187 | |
| 1188 | for (i = 0; i < gamecount; i++) { |
| 1189 | id item = |
| 1190 | initnewitem([DataMenuItem allocWithZone:[NSMenu menuZone]], |
| 1191 | menu, gamelist[i]->name, "", self, |
| 1192 | @selector(newGameWindow:)); |
| 1193 | [item setPayload:(void *)gamelist[i]]; |
| 1194 | } |
| 1195 | } |
| 1196 | return menu; |
| 1197 | } |
| 1198 | |
| 1199 | @end |
| 1200 | |
| 1201 | /* ---------------------------------------------------------------------- |
| 1202 | * Main program. Constructs the menus and runs the application. |
| 1203 | */ |
| 1204 | int main(int argc, char **argv) |
| 1205 | { |
| 1206 | NSAutoreleasePool *pool; |
| 1207 | NSMenu *menu; |
| 1208 | NSMenuItem *item; |
| 1209 | AppController *controller; |
| 1210 | NSImage *icon; |
| 1211 | |
| 1212 | pool = [[NSAutoreleasePool alloc] init]; |
| 1213 | |
| 1214 | icon = [NSImage imageNamed:@"NSApplicationIcon"]; |
| 1215 | [NSApplication sharedApplication]; |
| 1216 | [NSApp setApplicationIconImage:icon]; |
| 1217 | |
| 1218 | controller = [[[AppController alloc] init] autorelease]; |
| 1219 | [NSApp setDelegate:controller]; |
| 1220 | |
| 1221 | [NSApp setMainMenu: newmenu("Main Menu")]; |
| 1222 | |
| 1223 | menu = newsubmenu([NSApp mainMenu], "Apple Menu"); |
| 1224 | [NSApp setServicesMenu:newsubmenu(menu, "Services")]; |
| 1225 | [menu addItem:[NSMenuItem separatorItem]]; |
| 1226 | item = newitem(menu, "Hide Puzzles", "h", NSApp, @selector(hide:)); |
| 1227 | item = newitem(menu, "Hide Others", "o-h", NSApp, @selector(hideOtherApplications:)); |
| 1228 | item = newitem(menu, "Show All", "", NSApp, @selector(unhideAllApplications:)); |
| 1229 | [menu addItem:[NSMenuItem separatorItem]]; |
| 1230 | item = newitem(menu, "Quit", "q", NSApp, @selector(terminate:)); |
| 1231 | [NSApp setAppleMenu: menu]; |
| 1232 | |
| 1233 | menu = newsubmenu([NSApp mainMenu], "File"); |
| 1234 | item = newitem(menu, "New Game", "n", NULL, @selector(newGame:)); |
| 1235 | item = newitem(menu, "Restart Game", "r", NULL, @selector(restartGame:)); |
| 1236 | item = newitem(menu, "Specific Game", "", NULL, @selector(specificGame:)); |
| 1237 | [menu addItem:[NSMenuItem separatorItem]]; |
| 1238 | { |
| 1239 | NSMenu *submenu = newsubmenu(menu, "New Window"); |
| 1240 | int i; |
| 1241 | |
| 1242 | for (i = 0; i < gamecount; i++) { |
| 1243 | id item = |
| 1244 | initnewitem([DataMenuItem allocWithZone:[NSMenu menuZone]], |
| 1245 | submenu, gamelist[i]->name, "", controller, |
| 1246 | @selector(newGameWindow:)); |
| 1247 | [item setPayload:(void *)gamelist[i]]; |
| 1248 | } |
| 1249 | } |
| 1250 | [menu addItem:[NSMenuItem separatorItem]]; |
| 1251 | item = newitem(menu, "Close", "w", NULL, @selector(performClose:)); |
| 1252 | |
| 1253 | menu = newsubmenu([NSApp mainMenu], "Edit"); |
| 1254 | item = newitem(menu, "Undo", "z", NULL, @selector(undoMove:)); |
| 1255 | item = newitem(menu, "Redo", "S-z", NULL, @selector(redoMove:)); |
| 1256 | [menu addItem:[NSMenuItem separatorItem]]; |
| 1257 | item = newitem(menu, "Cut", "x", NULL, @selector(cut:)); |
| 1258 | item = newitem(menu, "Copy", "c", NULL, @selector(copy:)); |
| 1259 | item = newitem(menu, "Paste", "v", NULL, @selector(paste:)); |
| 1260 | [menu addItem:[NSMenuItem separatorItem]]; |
| 1261 | item = newitem(menu, "Solve", "S-s", NULL, @selector(solveGame:)); |
| 1262 | |
| 1263 | menu = newsubmenu([NSApp mainMenu], "Type"); |
| 1264 | typemenu = menu; |
| 1265 | item = newitem(menu, "Custom", "", NULL, @selector(customGameType:)); |
| 1266 | |
| 1267 | menu = newsubmenu([NSApp mainMenu], "Window"); |
| 1268 | [NSApp setWindowsMenu: menu]; |
| 1269 | item = newitem(menu, "Minimise Window", "m", NULL, @selector(performMiniaturize:)); |
| 1270 | |
| 1271 | menu = newsubmenu([NSApp mainMenu], "Help"); |
| 1272 | item = newitem(menu, "Puzzles Help", "?", NSApp, @selector(showHelp:)); |
| 1273 | |
| 1274 | [NSApp run]; |
| 1275 | [pool release]; |
| 1276 | } |