2 * Mac OS X / Cocoa front end to puzzles.
6 * - I'd like to be able to call up context help for a specific
9 * Mac interface issues that possibly could be done better:
11 * - is there a better approach to frontend_default_colour?
13 * - do we need any more options in the Window menu?
15 * - can / should we be doing anything with the titles of the
16 * configuration boxes?
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
24 * - a brief frob of the Mac numeric keypad suggests that it
25 * generates numbers no matter what you do. I wonder if I should
26 * try to figure out a way of detecting keypad codes so I can
27 * implement UP_LEFT and friends. Alternatively, perhaps I
28 * should simply assign the number keys to UP_LEFT et al?
29 * They're not in use for anything else right now.
31 * - see if we can do anything to one-button-ise the multi-button
32 * dependent puzzle UIs:
33 * - Pattern is a _little_ unwieldy but not too bad (since
34 * generally you never need the middle button unless you've
35 * made a mistake, so it's just click versus command-click).
36 * - Net is utterly vile; having normal click be one rotate and
37 * command-click be the other introduces a horrid asymmetry,
38 * and yet requiring a shift key for _each_ click would be
39 * even worse because rotation feels as if it ought to be the
40 * default action. I fear this is why the Flash Net had the
43 * - Should we _return_ to a game configuration sheet once an
44 * error is reported by midend_set_config, to allow the user to
45 * correct the one faulty input and keep the other five OK ones?
46 * The Apple `one sheet at a time' restriction would require me
47 * to do this by closing the config sheet, opening the alert
48 * sheet, and then reopening the config sheet when the alert is
49 * closed; and the human interface types, who presumably
50 * invented the one-sheet-at-a-time rule for good reasons, might
51 * look with disfavour on me trying to get round them to fake a
52 * nested sheet. On the other hand I think there are good
53 * practical reasons for wanting it that way. Uncertain.
55 * Grotty implementation details that could probably be improved:
57 * - I am _utterly_ unconvinced that NSImageView was the right way
58 * to go about having a window with a reliable backing store! It
59 * just doesn't feel right; NSImageView is a _control_. Is there
62 * - Resizing is currently very bad; rather than bother to work
63 * out how to resize the NSImageView, I just splatter and
69 #import <Cocoa/Cocoa.h>
72 /* ----------------------------------------------------------------------
77 * The `Type' menu. We frob this dynamically to allow the user to
78 * choose a preset set of settings from the current game.
82 /* ----------------------------------------------------------------------
83 * Miscellaneous support routines that aren't part of any object or
84 * clearly defined subsystem.
87 void fatal(char *fmt, ...)
94 vsnprintf(errorbuf, lenof(errorbuf), fmt, ap);
97 alert = [NSAlert alloc];
99 * We may have come here because we ran out of memory, in which
100 * case it's entirely likely that that alloc will fail, so we
101 * should have a fallback of some sort.
104 fprintf(stderr, "fatal error (and NSAlert failed): %s\n", errorbuf);
106 alert = [[alert init] autorelease];
107 [alert addButtonWithTitle:@"Oh dear"];
108 [alert setInformativeText:[NSString stringWithCString:errorbuf]];
114 void frontend_default_colour(frontend *fe, float *output)
116 /* FIXME: Is there a system default we can tap into for this? */
117 output[0] = output[1] = output[2] = 0.8F;
120 void get_random_seed(void **randseed, int *randseedsize)
122 time_t *tp = snew(time_t);
124 *randseed = (void *)tp;
125 *randseedsize = sizeof(time_t);
128 /* ----------------------------------------------------------------------
129 * Tiny extension to NSMenuItem which carries a payload of a `void
130 * *', allowing several menu items to invoke the same message but
131 * pass different data through it.
133 @interface DataMenuItem : NSMenuItem
137 - (void)setPayload:(void *)d;
138 - (void *)getPayload;
140 @implementation DataMenuItem
141 - (void)setPayload:(void *)d
151 /* ----------------------------------------------------------------------
152 * Utility routines for constructing OS X menus.
155 NSMenu *newmenu(const char *title)
157 return [[[NSMenu allocWithZone:[NSMenu menuZone]]
158 initWithTitle:[NSString stringWithCString:title]]
162 NSMenu *newsubmenu(NSMenu *parent, const char *title)
167 item = [[[NSMenuItem allocWithZone:[NSMenu menuZone]]
168 initWithTitle:[NSString stringWithCString:title]
172 child = newmenu(title);
173 [item setEnabled:YES];
174 [item setSubmenu:child];
175 [parent addItem:item];
179 id initnewitem(NSMenuItem *item, NSMenu *parent, const char *title,
180 const char *key, id target, SEL action)
182 unsigned mask = NSCommandKeyMask;
184 if (key[strcspn(key, "-")]) {
185 while (*key && *key != '-') {
186 int c = tolower((unsigned char)*key);
188 mask |= NSShiftKeyMask;
189 } else if (c == 'o' || c == 'a') {
190 mask |= NSAlternateKeyMask;
198 item = [[item initWithTitle:[NSString stringWithCString:title]
200 keyEquivalent:[NSString stringWithCString:key]]
204 [item setKeyEquivalentModifierMask: mask];
206 [item setEnabled:YES];
207 [item setTarget:target];
208 [item setAction:action];
210 [parent addItem:item];
215 NSMenuItem *newitem(NSMenu *parent, char *title, char *key,
216 id target, SEL action)
218 return initnewitem([NSMenuItem allocWithZone:[NSMenu menuZone]],
219 parent, title, key, target, action);
222 /* ----------------------------------------------------------------------
223 * The front end presented to midend.c.
225 * This is mostly a subclass of NSWindow. The actual `frontend'
226 * structure passed to the midend contains a variety of pointers,
227 * including that window object but also including the image we
228 * draw on, an ImageView to display it in the window, and so on.
243 @interface MyImageView : NSImageView
247 - (void)setWindow:(GameWindow *)win;
249 - (void)mouseEvent:(NSEvent *)ev button:(int)b;
250 - (void)mouseDown:(NSEvent *)ev;
251 - (void)mouseDragged:(NSEvent *)ev;
252 - (void)mouseUp:(NSEvent *)ev;
253 - (void)rightMouseDown:(NSEvent *)ev;
254 - (void)rightMouseDragged:(NSEvent *)ev;
255 - (void)rightMouseUp:(NSEvent *)ev;
256 - (void)otherMouseDown:(NSEvent *)ev;
257 - (void)otherMouseDragged:(NSEvent *)ev;
258 - (void)otherMouseUp:(NSEvent *)ev;
261 @interface GameWindow : NSWindow
266 struct timeval last_time;
271 NSView **cfg_controls;
275 - (id)initWithGame:(const game *)g;
277 - (void)processButton:(int)b x:(int)x y:(int)y;
278 - (void)keyDown:(NSEvent *)ev;
279 - (void)activateTimer;
280 - (void)deactivateTimer;
281 - (void)setStatusLine:(NSString *)text;
284 @implementation MyImageView
286 - (void)setWindow:(GameWindow *)win
296 - (void)mouseEvent:(NSEvent *)ev button:(int)b
298 NSPoint point = [self convertPoint:[ev locationInWindow] fromView:nil];
299 [ourwin processButton:b x:point.x y:point.y];
302 - (void)mouseDown:(NSEvent *)ev
304 unsigned mod = [ev modifierFlags];
305 [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_BUTTON :
306 (mod & NSShiftKeyMask) ? MIDDLE_BUTTON :
309 - (void)mouseDragged:(NSEvent *)ev
311 unsigned mod = [ev modifierFlags];
312 [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_DRAG :
313 (mod & NSShiftKeyMask) ? MIDDLE_DRAG :
316 - (void)mouseUp:(NSEvent *)ev
318 unsigned mod = [ev modifierFlags];
319 [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_RELEASE :
320 (mod & NSShiftKeyMask) ? MIDDLE_RELEASE :
323 - (void)rightMouseDown:(NSEvent *)ev
325 unsigned mod = [ev modifierFlags];
326 [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_BUTTON :
329 - (void)rightMouseDragged:(NSEvent *)ev
331 unsigned mod = [ev modifierFlags];
332 [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_DRAG :
335 - (void)rightMouseUp:(NSEvent *)ev
337 unsigned mod = [ev modifierFlags];
338 [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_RELEASE :
341 - (void)otherMouseDown:(NSEvent *)ev
343 [self mouseEvent:ev button:MIDDLE_BUTTON];
345 - (void)otherMouseDragged:(NSEvent *)ev
347 [self mouseEvent:ev button:MIDDLE_DRAG];
349 - (void)otherMouseUp:(NSEvent *)ev
351 [self mouseEvent:ev button:MIDDLE_RELEASE];
355 @implementation GameWindow
356 - (void)setupContentView
362 frame = [status frame];
363 frame.origin.y = frame.size.height;
368 midend_size(me, &w, &h);
369 frame.size.width = w;
370 frame.size.height = h;
372 fe.image = [[NSImage alloc] initWithSize:frame.size];
373 [fe.image setFlipped:YES];
374 fe.view = [[MyImageView alloc] initWithFrame:frame];
375 [fe.view setImage:fe.image];
376 [fe.view setWindow:self];
380 [[self contentView] addSubview:fe.view];
382 - (id)initWithGame:(const game *)g
384 NSRect rect = { {0,0}, {0,0} }, rect2;
391 me = midend_new(&fe, ourgame);
393 * If we ever need to open a fresh window using a provided game
394 * ID, I think the right thing is to move most of this method
395 * into a new initWithGame:gameID: method, and have
396 * initWithGame: simply call that one and pass it NULL.
399 midend_size(me, &w, &h);
401 rect.size.height = h;
404 * Create the status bar, which will just be an NSTextField.
406 if (ourgame->wants_statusbar()) {
407 status = [[NSTextField alloc] initWithFrame:NSMakeRect(0,0,100,50)];
408 [status setEditable:NO];
409 [status setSelectable:NO];
410 [status setBordered:YES];
411 [status setBezeled:YES];
412 [status setBezelStyle:NSTextFieldSquareBezel];
413 [status setDrawsBackground:YES];
414 [[status cell] setTitle:@""];
416 rect2 = [status frame];
417 rect.size.height += rect2.size.height;
418 rect2.size.width = rect.size.width;
419 rect2.origin.x = rect2.origin.y = 0;
420 [status setFrame:rect2];
424 self = [super initWithContentRect:rect
425 styleMask:(NSTitledWindowMask | NSMiniaturizableWindowMask |
426 NSClosableWindowMask)
427 backing:NSBackingStoreBuffered
429 [self setTitle:[NSString stringWithCString:ourgame->name]];
435 colours = midend_colours(me, &ncolours);
436 fe.ncolours = ncolours;
437 fe.colours = snewn(ncolours, NSColor *);
439 for (i = 0; i < ncolours; i++) {
440 fe.colours[i] = [[NSColor colorWithDeviceRed:colours[i*3]
441 green:colours[i*3+1] blue:colours[i*3+2]
446 [self setupContentView];
448 [[self contentView] addSubview:status];
449 [self setIgnoresMouseEvents:NO];
451 [self center]; /* :-) */
459 for (i = 0; i < fe.ncolours; i++) {
460 [fe.colours[i] release];
464 return [super dealloc];
467 - (void)processButton:(int)b x:(int)x y:(int)y
469 if (!midend_process_key(me, x, y, b))
473 - (void)keyDown:(NSEvent *)ev
475 NSString *s = [ev characters];
476 int i, n = [s length];
478 for (i = 0; i < n; i++) {
479 int c = [s characterAtIndex:i];
482 * ASCII gets passed straight to midend_process_key.
483 * Anything above that has to be translated to our own
484 * function key codes.
488 case NSUpArrowFunctionKey:
491 case NSDownArrowFunctionKey:
494 case NSLeftArrowFunctionKey:
497 case NSRightArrowFunctionKey:
505 [self processButton:c x:-1 y:-1];
509 - (void)activateTimer
514 timer = [NSTimer scheduledTimerWithTimeInterval:0.02
515 target:self selector:@selector(timerTick:)
516 userInfo:nil repeats:YES];
517 gettimeofday(&last_time, NULL);
520 - (void)deactivateTimer
529 - (void)timerTick:(id)sender
533 gettimeofday(&now, NULL);
534 elapsed = ((now.tv_usec - last_time.tv_usec) * 0.000001F +
535 (now.tv_sec - last_time.tv_sec));
536 midend_timer(me, elapsed);
540 - (void)newGame:(id)sender
542 [self processButton:'n' x:-1 y:-1];
544 - (void)restartGame:(id)sender
546 [self processButton:'r' x:-1 y:-1];
548 - (void)undoMove:(id)sender
550 [self processButton:'u' x:-1 y:-1];
552 - (void)redoMove:(id)sender
554 [self processButton:'r'&0x1F x:-1 y:-1];
557 - (void)clearTypeMenu
559 while ([typemenu numberOfItems] > 1)
560 [typemenu removeItemAtIndex:0];
563 - (void)becomeKeyWindow
567 [self clearTypeMenu];
569 [super becomeKeyWindow];
571 n = midend_num_presets(me);
574 [typemenu insertItem:[NSMenuItem separatorItem] atIndex:0];
580 midend_fetch_preset(me, n, &name, ¶ms);
582 item = [[[DataMenuItem alloc]
583 initWithTitle:[NSString stringWithCString:name]
584 action:NULL keyEquivalent:@""]
587 [item setEnabled:YES];
588 [item setTarget:self];
589 [item setAction:@selector(presetGame:)];
590 [item setPayload:params];
592 [typemenu insertItem:item atIndex:0];
597 - (void)resignKeyWindow
599 [self clearTypeMenu];
600 [super resignKeyWindow];
605 [self clearTypeMenu];
609 - (void)resizeForNewGameParams
614 midend_size(me, &w, &h);
619 NSRect frame = [status frame];
620 size.height += frame.size.height;
621 frame.size.width = size.width;
622 [status setFrame:frame];
625 NSDisableScreenUpdates();
626 [self setContentSize:size];
627 [self setupContentView];
628 NSEnableScreenUpdates();
631 - (void)presetGame:(id)sender
633 game_params *params = [sender getPayload];
635 midend_set_params(me, params);
638 [self resizeForNewGameParams];
641 - (void)startConfigureSheet:(int)which
643 NSButton *ok, *cancel;
644 int actw, acth, leftw, rightw, totalw, h, thish, y;
646 NSRect rect, tmprect;
647 const int SPACING = 16;
655 assert(sheet == NULL);
658 * Every control we create here is going to have this size
659 * until we tell it to calculate a better one.
661 tmprect = NSMakeRect(0, 0, 100, 50);
664 * Set up OK and Cancel buttons. (Actually, MacOS doesn't seem
665 * to be fond of generic OK and Cancel wording, so I'm going to
666 * rename them to something nicer.)
670 cancel = [[NSButton alloc] initWithFrame:tmprect];
671 [cancel setBezelStyle:NSRoundedBezelStyle];
672 [cancel setTitle:@"Abandon"];
673 [cancel setTarget:self];
674 [cancel setKeyEquivalent:@"\033"];
675 [cancel setAction:@selector(sheetCancelButton:)];
677 rect = [cancel frame];
678 if (actw < rect.size.width) actw = rect.size.width;
679 if (acth < rect.size.height) acth = rect.size.height;
681 ok = [[NSButton alloc] initWithFrame:tmprect];
682 [ok setBezelStyle:NSRoundedBezelStyle];
683 [ok setTitle:@"Accept"];
685 [ok setKeyEquivalent:@"\r"];
686 [ok setAction:@selector(sheetOKButton:)];
689 if (actw < rect.size.width) actw = rect.size.width;
690 if (acth < rect.size.height) acth = rect.size.height;
692 totalw = SPACING + 2 * actw;
693 h = 2 * SPACING + acth;
696 * Now fetch the midend config data and go through it creating
699 cfg = midend_get_config(me, which, &title);
700 sfree(title); /* FIXME: should we use this somehow? */
703 cfg_ncontrols = cfg_controlsize = 0;
706 for (i = cfg; i->type != C_END; i++) {
707 if (cfg_controlsize < cfg_ncontrols + 5) {
708 cfg_controlsize = cfg_ncontrols + 32;
709 cfg_controls = sresize(cfg_controls, cfg_controlsize, NSView *);
717 * Two NSTextFields, one being a label and the other
721 tf = [[NSTextField alloc] initWithFrame:tmprect];
723 [tf setSelectable:NO];
725 [tf setDrawsBackground:NO];
726 [[tf cell] setTitle:[NSString stringWithCString:i->name]];
729 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
730 if (leftw < rect.size.width + 1) leftw = rect.size.width + 1;
731 cfg_controls[cfg_ncontrols++] = tf;
733 /* We impose a minimum width on editable NSTextFields to
734 * stop them looking _completely_ silly. */
735 if (rightw < 75) rightw = 75;
737 tf = [[NSTextField alloc] initWithFrame:tmprect];
738 [tf setEditable:YES];
739 [tf setSelectable:YES];
740 [tf setBordered:YES];
741 [[tf cell] setTitle:[NSString stringWithCString:i->sval]];
744 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
745 if (rightw < rect.size.width + 1) rightw = rect.size.width + 1;
746 cfg_controls[cfg_ncontrols++] = tf;
751 * A checkbox is an NSButton with a type of
754 b = [[NSButton alloc] initWithFrame:tmprect];
755 [b setBezelStyle:NSRoundedBezelStyle];
756 [b setButtonType:NSSwitchButton];
757 [b setTitle:[NSString stringWithCString:i->name]];
759 [b setState:(i->ival ? NSOnState : NSOffState)];
761 if (totalw < rect.size.width + 1) totalw = rect.size.width + 1;
762 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
763 cfg_controls[cfg_ncontrols++] = b;
768 * A pop-up menu control is an NSPopUpButton, which
769 * takes an embedded NSMenu. We also need an
770 * NSTextField to act as a label.
773 tf = [[NSTextField alloc] initWithFrame:tmprect];
775 [tf setSelectable:NO];
777 [tf setDrawsBackground:NO];
778 [[tf cell] setTitle:[NSString stringWithCString:i->name]];
781 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
782 if (leftw < rect.size.width + 1) leftw = rect.size.width + 1;
783 cfg_controls[cfg_ncontrols++] = tf;
785 pb = [[NSPopUpButton alloc] initWithFrame:tmprect pullsDown:NO];
786 [pb setBezelStyle:NSRoundedBezelStyle];
796 while (*p && *p != c) p++;
798 [pb addItemWithTitle:[NSString stringWithCString:q
804 [pb selectItemAtIndex:i->ival];
808 if (rightw < rect.size.width + 1) rightw = rect.size.width + 1;
809 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
810 cfg_controls[cfg_ncontrols++] = pb;
814 h += SPACING + thish;
817 if (totalw < leftw + SPACING + rightw)
818 totalw = leftw + SPACING + rightw;
819 if (totalw > leftw + SPACING + rightw) {
820 int excess = totalw - (leftw + SPACING + rightw);
821 int leftexcess = leftw * excess / (leftw + rightw);
822 int rightexcess = excess - leftexcess;
824 rightw += rightexcess;
828 * Now go through the list again, setting the final position
833 for (i = cfg; i->type != C_END; i++) {
840 * These two are treated identically, since both expect
841 * a control on the left and another on the right.
843 rect = [cfg_controls[k] frame];
844 if (thish < rect.size.height + 1)
845 thish = rect.size.height + 1;
846 rect = [cfg_controls[k+1] frame];
847 if (thish < rect.size.height + 1)
848 thish = rect.size.height + 1;
849 rect = [cfg_controls[k] frame];
850 rect.origin.y = y - thish/2 - rect.size.height/2;
851 rect.origin.x = SPACING;
852 rect.size.width = leftw;
853 [cfg_controls[k] setFrame:rect];
854 rect = [cfg_controls[k+1] frame];
855 rect.origin.y = y - thish/2 - rect.size.height/2;
856 rect.origin.x = 2 * SPACING + leftw;
857 rect.size.width = rightw;
858 [cfg_controls[k+1] setFrame:rect];
863 rect = [cfg_controls[k] frame];
864 if (thish < rect.size.height + 1)
865 thish = rect.size.height + 1;
866 rect.origin.y = y - thish/2 - rect.size.height/2;
867 rect.origin.x = SPACING;
868 rect.size.width = totalw;
869 [cfg_controls[k] setFrame:rect];
876 assert(k == cfg_ncontrols);
878 [cancel setFrame:NSMakeRect(SPACING+totalw/4-actw/2, SPACING, actw, acth)];
879 [ok setFrame:NSMakeRect(SPACING+3*totalw/4-actw/2, SPACING, actw, acth)];
881 sheet = [[NSWindow alloc]
882 initWithContentRect:NSMakeRect(0,0,totalw + 2*SPACING,h)
883 styleMask:NSTitledWindowMask | NSClosableWindowMask
884 backing:NSBackingStoreBuffered
887 [[sheet contentView] addSubview:cancel];
888 [[sheet contentView] addSubview:ok];
890 for (k = 0; k < cfg_ncontrols; k++)
891 [[sheet contentView] addSubview:cfg_controls[k]];
893 [NSApp beginSheet:sheet modalForWindow:self
894 modalDelegate:nil didEndSelector:nil contextInfo:nil];
897 - (void)specificGame:(id)sender
899 [self startConfigureSheet:CFG_SEED];
902 - (void)customGameType:(id)sender
904 [self startConfigureSheet:CFG_SETTINGS];
907 - (void)sheetEndWithStatus:(BOOL)update
909 assert(sheet != NULL);
910 [NSApp endSheet:sheet];
911 [sheet orderOut:self];
919 for (i = cfg; i->type != C_END; i++) {
923 i->sval = dupstr([[[(id)cfg_controls[k+1] cell]
928 i->ival = [(id)cfg_controls[k] state] == NSOnState;
932 i->ival = [(id)cfg_controls[k+1] indexOfSelectedItem];
938 error = midend_set_config(me, cfg_which, cfg);
940 NSAlert *alert = [[[NSAlert alloc] init] autorelease];
941 [alert addButtonWithTitle:@"Bah"];
942 [alert setInformativeText:[NSString stringWithCString:error]];
943 [alert beginSheetModalForWindow:self modalDelegate:nil
944 didEndSelector:nil contextInfo:nil];
947 [self resizeForNewGameParams];
953 - (void)sheetOKButton:(id)sender
955 [self sheetEndWithStatus:YES];
957 - (void)sheetCancelButton:(id)sender
959 [self sheetEndWithStatus:NO];
962 - (void)setStatusLine:(NSString *)text
964 [[status cell] setTitle:text];
970 * Drawing routines called by the midend.
972 void draw_polygon(frontend *fe, int *coords, int npoints,
973 int fill, int colour)
975 NSBezierPath *path = [NSBezierPath bezierPath];
978 [[NSGraphicsContext currentContext] setShouldAntialias:YES];
980 assert(colour >= 0 && colour < fe->ncolours);
981 [fe->colours[colour] set];
983 for (i = 0; i < npoints; i++) {
984 NSPoint p = { coords[i*2] + 0.5, coords[i*2+1] + 0.5 };
986 [path moveToPoint:p];
988 [path lineToPoint:p];
998 void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour)
1000 NSBezierPath *path = [NSBezierPath bezierPath];
1001 NSPoint p1 = { x1 + 0.5, y1 + 0.5 }, p2 = { x2 + 0.5, y2 + 0.5 };
1003 [[NSGraphicsContext currentContext] setShouldAntialias:NO];
1005 assert(colour >= 0 && colour < fe->ncolours);
1006 [fe->colours[colour] set];
1008 [path moveToPoint:p1];
1009 [path lineToPoint:p2];
1012 void draw_rect(frontend *fe, int x, int y, int w, int h, int colour)
1014 NSRect r = { {x,y}, {w,h} };
1016 [[NSGraphicsContext currentContext] setShouldAntialias:NO];
1018 assert(colour >= 0 && colour < fe->ncolours);
1019 [fe->colours[colour] set];
1023 void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize,
1024 int align, int colour, char *text)
1026 NSString *string = [NSString stringWithCString:text];
1032 [[NSGraphicsContext currentContext] setShouldAntialias:YES];
1034 assert(colour >= 0 && colour < fe->ncolours);
1036 if (fonttype == FONT_FIXED)
1037 font = [NSFont userFixedPitchFontOfSize:fontsize];
1039 font = [NSFont userFontOfSize:fontsize];
1041 attr = [NSDictionary dictionaryWithObjectsAndKeys:
1042 fe->colours[colour], NSForegroundColorAttributeName,
1043 font, NSFontAttributeName, nil];
1048 size = [string sizeWithAttributes:attr];
1049 if (align & ALIGN_HRIGHT)
1050 point.x -= size.width;
1051 else if (align & ALIGN_HCENTRE)
1052 point.x -= size.width / 2;
1053 if (align & ALIGN_VCENTRE)
1054 point.y -= size.height / 2;
1056 [string drawAtPoint:point withAttributes:attr];
1058 void draw_update(frontend *fe, int x, int y, int w, int h)
1061 * FIXME: It seems odd that nothing is required here, although
1062 * everything _seems_ to work with this routine empty. Possibly
1063 * we're always updating the entire window, and there's a
1064 * better way which would involve doing something in here?
1067 void clip(frontend *fe, int x, int y, int w, int h)
1069 NSRect r = { {x,y}, {w,h} };
1072 [[NSGraphicsContext currentContext] saveGraphicsState];
1073 [NSBezierPath clipRect:r];
1076 void unclip(frontend *fe)
1079 [[NSGraphicsContext currentContext] restoreGraphicsState];
1080 fe->clipped = FALSE;
1082 void start_draw(frontend *fe)
1084 [fe->image lockFocus];
1085 fe->clipped = FALSE;
1087 void end_draw(frontend *fe)
1089 [fe->image unlockFocus];
1090 [fe->view setNeedsDisplay];
1093 void deactivate_timer(frontend *fe)
1095 [fe->window deactivateTimer];
1097 void activate_timer(frontend *fe)
1099 [fe->window activateTimer];
1102 void status_bar(frontend *fe, char *text)
1104 [fe->window setStatusLine:[NSString stringWithCString:text]];
1107 /* ----------------------------------------------------------------------
1108 * AppController: the object which receives the messages from all
1109 * menu selections that aren't standard OS X functions.
1111 @interface AppController : NSObject
1114 - (void)newGame:(id)sender;
1117 @implementation AppController
1119 - (void)newGame:(id)sender
1121 const game *g = [sender getPayload];
1124 win = [[GameWindow alloc] initWithGame:g];
1125 [win makeKeyAndOrderFront:self];
1128 - (NSMenu *)applicationDockMenu:(NSApplication *)sender
1130 NSMenu *menu = newmenu("Dock Menu");
1134 for (i = 0; i < gamecount; i++) {
1136 initnewitem([DataMenuItem allocWithZone:[NSMenu menuZone]],
1137 menu, gamelist[i]->name, "", self,
1138 @selector(newGame:));
1139 [item setPayload:(void *)gamelist[i]];
1147 /* ----------------------------------------------------------------------
1148 * Main program. Constructs the menus and runs the application.
1150 int main(int argc, char **argv)
1152 NSAutoreleasePool *pool;
1155 AppController *controller;
1158 pool = [[NSAutoreleasePool alloc] init];
1160 icon = [NSImage imageNamed:@"NSApplicationIcon"];
1161 [NSApplication sharedApplication];
1162 [NSApp setApplicationIconImage:icon];
1164 controller = [[[AppController alloc] init] autorelease];
1165 [NSApp setDelegate:controller];
1167 [NSApp setMainMenu: newmenu("Main Menu")];
1169 menu = newsubmenu([NSApp mainMenu], "Apple Menu");
1170 [NSApp setServicesMenu:newsubmenu(menu, "Services")];
1171 [menu addItem:[NSMenuItem separatorItem]];
1172 item = newitem(menu, "Hide Puzzles", "h", NSApp, @selector(hide:));
1173 item = newitem(menu, "Hide Others", "o-h", NSApp, @selector(hideOtherApplications:));
1174 item = newitem(menu, "Show All", "", NSApp, @selector(unhideAllApplications:));
1175 [menu addItem:[NSMenuItem separatorItem]];
1176 item = newitem(menu, "Quit", "q", NSApp, @selector(terminate:));
1177 [NSApp setAppleMenu: menu];
1179 menu = newsubmenu([NSApp mainMenu], "Open");
1183 for (i = 0; i < gamecount; i++) {
1185 initnewitem([DataMenuItem allocWithZone:[NSMenu menuZone]],
1186 menu, gamelist[i]->name, "", controller,
1187 @selector(newGame:));
1188 [item setPayload:(void *)gamelist[i]];
1192 menu = newsubmenu([NSApp mainMenu], "Game");
1193 item = newitem(menu, "New", "n", NULL, @selector(newGame:));
1194 item = newitem(menu, "Restart", "r", NULL, @selector(restartGame:));
1195 item = newitem(menu, "Specific", "", NULL, @selector(specificGame:));
1196 [menu addItem:[NSMenuItem separatorItem]];
1197 item = newitem(menu, "Undo", "z", NULL, @selector(undoMove:));
1198 item = newitem(menu, "Redo", "S-z", NULL, @selector(redoMove:));
1199 [menu addItem:[NSMenuItem separatorItem]];
1200 item = newitem(menu, "Close", "w", NULL, @selector(performClose:));
1202 menu = newsubmenu([NSApp mainMenu], "Type");
1204 item = newitem(menu, "Custom", "", NULL, @selector(customGameType:));
1206 menu = newsubmenu([NSApp mainMenu], "Window");
1207 [NSApp setWindowsMenu: menu];
1208 item = newitem(menu, "Minimise Window", "m", NULL, @selector(performMiniaturize:));
1210 menu = newsubmenu([NSApp mainMenu], "Help");
1211 item = newitem(menu, "Puzzles Help", "?", NSApp, @selector(showHelp:));