2 * Mac OS X / Cocoa front end to puzzles.
6 * - status bar support.
8 * - not sure what I should be doing about default window
9 * placement. Centring new windows is a bit feeble, but what's
10 * better? Is there a standard way to tell the OS "here's the
11 * _size_ of window I want, now use your best judgment about the
14 * - a brief frob of the Mac numeric keypad suggests that it
15 * generates numbers no matter what you do. I wonder if I should
16 * try to figure out a way of detecting keypad codes so I can
17 * implement UP_LEFT and friends. Alternatively, perhaps I
18 * should simply assign the number keys to UP_LEFT et al?
19 * They're not in use for anything else right now.
21 * - proper fatal errors.
23 * - is there a better approach to frontend_default_colour?
25 * - do we need any more options in the Window menu?
27 * - see if we can do anything to one-button-ise the multi-button
28 * dependent puzzle UIs:
29 * - Pattern is a _little_ unwieldy but not too bad (since
30 * generally you never need the middle button unless you've
31 * made a mistake, so it's just click versus command-click).
32 * - Net is utterly vile; having normal click be one rotate and
33 * command-click be the other introduces a horrid asymmetry,
34 * and yet requiring a shift key for _each_ click would be
35 * even worse because rotation feels as if it ought to be the
36 * default action. I fear this is why the Flash Net had the
39 * - Find out how to do help, and do some. We have a help file; at
40 * _worst_ this should involve a new Halibut back end, but I
41 * think help is HTML round here anyway so perhaps we can work
42 * with what we already have.
44 * - Can we arrange for a pop-up menu from the Dock icon which
45 * launches specific games, perhaps?
46 * + apparently we can; see the NSApplication method
47 * `applicationDockMenu:'. Good good. Do so.
49 * - Why are the right and bottom edges of the Pattern grid one
50 * pixel thinner than they should be?
52 * - Should we _return_ to a game configuration sheet once an
53 * error is reported by midend_set_config, to allow the user to
54 * correct the one faulty input and keep the other five OK ones?
55 * The Apple `one sheet at a time' restriction would require me
56 * to do this by closing the config sheet, opening the alert
57 * sheet, and then reopening the config sheet when the alert is
58 * closed; and the human interface types, who presumably
59 * invented the one-sheet-at-a-time rule for good reasons, might
60 * look with disfavour on me trying to get round them to fake a
61 * nested sheet. On the other hand I think there are good
62 * practical reasons for wanting it that way. Uncertain.
64 * Grotty implementation details that could probably be improved:
66 * - I am _utterly_ unconvinced that NSImageView was the right way
67 * to go about having a window with a reliable backing store! It
68 * just doesn't feel right; NSImageView is a _control_. Is there
71 * - Resizing is currently very bad; rather than bother to work
72 * out how to resize the NSImageView, I just splatter and
78 #import <Cocoa/Cocoa.h>
81 void fatal(char *fmt, ...)
83 /* FIXME: This will do for testing, but should be GUI-ish instead. */
86 fprintf(stderr, "fatal error: ");
89 vfprintf(stderr, fmt, ap);
92 fprintf(stderr, "\n");
96 void frontend_default_colour(frontend *fe, float *output)
99 output[0] = output[1] = output[2] = 0.8F;
101 void status_bar(frontend *fe, char *text)
106 void get_random_seed(void **randseed, int *randseedsize)
108 time_t *tp = snew(time_t);
110 *randseed = (void *)tp;
111 *randseedsize = sizeof(time_t);
114 /* ----------------------------------------------------------------------
119 * The `Type' menu. We frob this dynamically to allow the user to
120 * choose a preset set of settings from the current game.
124 /* ----------------------------------------------------------------------
125 * Tiny extension to NSMenuItem which carries a payload of a `void
126 * *', allowing several menu items to invoke the same message but
127 * pass different data through it.
129 @interface DataMenuItem : NSMenuItem
133 - (void)setPayload:(void *)d;
134 - (void *)getPayload;
136 @implementation DataMenuItem
137 - (void)setPayload:(void *)d
147 /* ----------------------------------------------------------------------
148 * Utility routines for constructing OS X menus.
151 NSMenu *newmenu(const char *title)
153 return [[[NSMenu allocWithZone:[NSMenu menuZone]]
154 initWithTitle:[NSString stringWithCString:title]]
158 NSMenu *newsubmenu(NSMenu *parent, const char *title)
163 item = [[[NSMenuItem allocWithZone:[NSMenu menuZone]]
164 initWithTitle:[NSString stringWithCString:title]
168 child = newmenu(title);
169 [item setEnabled:YES];
170 [item setSubmenu:child];
171 [parent addItem:item];
175 id initnewitem(NSMenuItem *item, NSMenu *parent, const char *title,
176 const char *key, id target, SEL action)
178 unsigned mask = NSCommandKeyMask;
180 if (key[strcspn(key, "-")]) {
181 while (*key && *key != '-') {
182 int c = tolower((unsigned char)*key);
184 mask |= NSShiftKeyMask;
185 } else if (c == 'o' || c == 'a') {
186 mask |= NSAlternateKeyMask;
194 item = [[item initWithTitle:[NSString stringWithCString:title]
196 keyEquivalent:[NSString stringWithCString:key]]
200 [item setKeyEquivalentModifierMask: mask];
202 [item setEnabled:YES];
203 [item setTarget:target];
204 [item setAction:action];
206 [parent addItem:item];
211 NSMenuItem *newitem(NSMenu *parent, char *title, char *key,
212 id target, SEL action)
214 return initnewitem([NSMenuItem allocWithZone:[NSMenu menuZone]],
215 parent, title, key, target, action);
218 /* ----------------------------------------------------------------------
219 * The front end presented to midend.c.
221 * This is mostly a subclass of NSWindow. The actual `frontend'
222 * structure passed to the midend contains a variety of pointers,
223 * including that window object but also including the image we
224 * draw on, an ImageView to display it in the window, and so on.
239 @interface MyImageView : NSImageView
243 - (void)setWindow:(GameWindow *)win;
245 - (void)mouseEvent:(NSEvent *)ev button:(int)b;
246 - (void)mouseDown:(NSEvent *)ev;
247 - (void)mouseDragged:(NSEvent *)ev;
248 - (void)mouseUp:(NSEvent *)ev;
249 - (void)rightMouseDown:(NSEvent *)ev;
250 - (void)rightMouseDragged:(NSEvent *)ev;
251 - (void)rightMouseUp:(NSEvent *)ev;
252 - (void)otherMouseDown:(NSEvent *)ev;
253 - (void)otherMouseDragged:(NSEvent *)ev;
254 - (void)otherMouseUp:(NSEvent *)ev;
257 @interface GameWindow : NSWindow
262 struct timeval last_time;
267 NSView **cfg_controls;
270 - (id)initWithGame:(const game *)g;
272 - (void)processButton:(int)b x:(int)x y:(int)y;
273 - (void)keyDown:(NSEvent *)ev;
274 - (void)activateTimer;
275 - (void)deactivateTimer;
278 @implementation MyImageView
280 - (void)setWindow:(GameWindow *)win
290 - (void)mouseEvent:(NSEvent *)ev button:(int)b
292 NSPoint point = [self convertPoint:[ev locationInWindow] fromView:nil];
293 [ourwin processButton:b x:point.x y:point.y];
296 - (void)mouseDown:(NSEvent *)ev
298 unsigned mod = [ev modifierFlags];
299 [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_BUTTON :
300 (mod & NSShiftKeyMask) ? MIDDLE_BUTTON :
303 - (void)mouseDragged:(NSEvent *)ev
305 unsigned mod = [ev modifierFlags];
306 [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_DRAG :
307 (mod & NSShiftKeyMask) ? MIDDLE_DRAG :
310 - (void)mouseUp:(NSEvent *)ev
312 unsigned mod = [ev modifierFlags];
313 [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_RELEASE :
314 (mod & NSShiftKeyMask) ? MIDDLE_RELEASE :
317 - (void)rightMouseDown:(NSEvent *)ev
319 unsigned mod = [ev modifierFlags];
320 [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_BUTTON :
323 - (void)rightMouseDragged:(NSEvent *)ev
325 unsigned mod = [ev modifierFlags];
326 [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_DRAG :
329 - (void)rightMouseUp:(NSEvent *)ev
331 unsigned mod = [ev modifierFlags];
332 [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_RELEASE :
335 - (void)otherMouseDown:(NSEvent *)ev
337 [self mouseEvent:ev button:MIDDLE_BUTTON];
339 - (void)otherMouseDragged:(NSEvent *)ev
341 [self mouseEvent:ev button:MIDDLE_DRAG];
343 - (void)otherMouseUp:(NSEvent *)ev
345 [self mouseEvent:ev button:MIDDLE_RELEASE];
349 @implementation GameWindow
350 - (void)setupContentView
355 midend_size(me, &w, &h);
359 fe.image = [[NSImage alloc] initWithSize:size];
360 [fe.image setFlipped:YES];
361 fe.view = [[MyImageView alloc]
362 initWithFrame:[self contentRectForFrameRect:[self frame]]];
363 [fe.view setImage:fe.image];
364 [fe.view setWindow:self];
368 [self setContentView:fe.view];
370 - (id)initWithGame:(const game *)g
372 NSRect rect = { {0,0}, {0,0} };
379 me = midend_new(&fe, ourgame);
381 * If we ever need to open a fresh window using a provided game
382 * ID, I think the right thing is to move most of this method
383 * into a new initWithGame:gameID: method, and have
384 * initWithGame: simply call that one and pass it NULL.
387 midend_size(me, &w, &h);
389 rect.size.height = h;
391 self = [super initWithContentRect:rect
392 styleMask:(NSTitledWindowMask | NSMiniaturizableWindowMask |
393 NSClosableWindowMask)
394 backing:NSBackingStoreBuffered
396 [self setTitle:[NSString stringWithCString:ourgame->name]];
402 colours = midend_colours(me, &ncolours);
403 fe.ncolours = ncolours;
404 fe.colours = snewn(ncolours, NSColor *);
406 for (i = 0; i < ncolours; i++) {
407 fe.colours[i] = [[NSColor colorWithDeviceRed:colours[i*3]
408 green:colours[i*3+1] blue:colours[i*3+2]
413 [self setupContentView];
414 [self setIgnoresMouseEvents:NO];
416 [self center]; /* :-) */
424 for (i = 0; i < fe.ncolours; i++) {
425 [fe.colours[i] release];
429 return [super dealloc];
432 - (void)processButton:(int)b x:(int)x y:(int)y
434 if (!midend_process_key(me, x, y, b))
438 - (void)keyDown:(NSEvent *)ev
440 NSString *s = [ev characters];
441 int i, n = [s length];
443 for (i = 0; i < n; i++) {
444 int c = [s characterAtIndex:i];
447 * ASCII gets passed straight to midend_process_key.
448 * Anything above that has to be translated to our own
449 * function key codes.
453 case NSUpArrowFunctionKey:
456 case NSDownArrowFunctionKey:
459 case NSLeftArrowFunctionKey:
462 case NSRightArrowFunctionKey:
470 [self processButton:c x:-1 y:-1];
474 - (void)activateTimer
479 timer = [NSTimer scheduledTimerWithTimeInterval:0.02
480 target:self selector:@selector(timerTick:)
481 userInfo:nil repeats:YES];
482 gettimeofday(&last_time, NULL);
485 - (void)deactivateTimer
494 - (void)timerTick:(id)sender
498 gettimeofday(&now, NULL);
499 elapsed = ((now.tv_usec - last_time.tv_usec) * 0.000001F +
500 (now.tv_sec - last_time.tv_sec));
501 midend_timer(me, elapsed);
505 - (void)newGame:(id)sender
507 [self processButton:'n' x:-1 y:-1];
509 - (void)restartGame:(id)sender
511 [self processButton:'r' x:-1 y:-1];
513 - (void)undoMove:(id)sender
515 [self processButton:'u' x:-1 y:-1];
517 - (void)redoMove:(id)sender
519 [self processButton:'r'&0x1F x:-1 y:-1];
522 - (void)clearTypeMenu
524 while ([typemenu numberOfItems] > 1)
525 [typemenu removeItemAtIndex:0];
528 - (void)becomeKeyWindow
532 [self clearTypeMenu];
534 [super becomeKeyWindow];
536 n = midend_num_presets(me);
539 [typemenu insertItem:[NSMenuItem separatorItem] atIndex:0];
545 midend_fetch_preset(me, n, &name, ¶ms);
547 item = [[[DataMenuItem alloc]
548 initWithTitle:[NSString stringWithCString:name]
549 action:NULL keyEquivalent:@""]
552 [item setEnabled:YES];
553 [item setTarget:self];
554 [item setAction:@selector(presetGame:)];
555 [item setPayload:params];
557 [typemenu insertItem:item atIndex:0];
562 - (void)resignKeyWindow
564 [self clearTypeMenu];
565 [super resignKeyWindow];
570 [self clearTypeMenu];
574 - (void)resizeForNewGameParams
579 midend_size(me, &w, &h);
583 NSDisableScreenUpdates();
584 [self setContentSize:size];
585 [self setupContentView];
586 NSEnableScreenUpdates();
589 - (void)presetGame:(id)sender
591 game_params *params = [sender getPayload];
593 midend_set_params(me, params);
596 [self resizeForNewGameParams];
599 - (void)startConfigureSheet:(int)which
601 NSButton *ok, *cancel;
602 int actw, acth, leftw, rightw, totalw, h, thish, y;
604 NSRect rect, tmprect;
605 const int SPACING = 16;
613 assert(sheet == NULL);
616 * Every control we create here is going to have this size
617 * until we tell it to calculate a better one.
619 tmprect = NSMakeRect(0, 0, 100, 50);
622 * Set up OK and Cancel buttons. (Actually, MacOS doesn't seem
623 * to be fond of generic OK and Cancel wording, so I'm going to
624 * rename them to something nicer.)
628 cancel = [[NSButton alloc] initWithFrame:tmprect];
629 [cancel setBezelStyle:NSRoundedBezelStyle];
630 [cancel setTitle:@"Abandon"];
631 [cancel setTarget:self];
632 [cancel setKeyEquivalent:@"\033"];
633 [cancel setAction:@selector(sheetCancelButton:)];
635 rect = [cancel frame];
636 if (actw < rect.size.width) actw = rect.size.width;
637 if (acth < rect.size.height) acth = rect.size.height;
639 ok = [[NSButton alloc] initWithFrame:tmprect];
640 [ok setBezelStyle:NSRoundedBezelStyle];
641 [ok setTitle:@"Accept"];
643 [ok setKeyEquivalent:@"\r"];
644 [ok setAction:@selector(sheetOKButton:)];
647 if (actw < rect.size.width) actw = rect.size.width;
648 if (acth < rect.size.height) acth = rect.size.height;
650 totalw = SPACING + 2 * actw;
651 h = 2 * SPACING + acth;
654 * Now fetch the midend config data and go through it creating
657 cfg = midend_get_config(me, which, &title);
658 sfree(title); /* FIXME: should we use this somehow? */
661 cfg_ncontrols = cfg_controlsize = 0;
664 for (i = cfg; i->type != C_END; i++) {
665 if (cfg_controlsize < cfg_ncontrols + 5) {
666 cfg_controlsize = cfg_ncontrols + 32;
667 cfg_controls = sresize(cfg_controls, cfg_controlsize, NSView *);
675 * Two NSTextFields, one being a label and the other
679 tf = [[NSTextField alloc] initWithFrame:tmprect];
681 [tf setSelectable:NO];
683 [tf setDrawsBackground:NO];
684 [[tf cell] setTitle:[NSString stringWithCString:i->name]];
687 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
688 if (leftw < rect.size.width + 1) leftw = rect.size.width + 1;
689 cfg_controls[cfg_ncontrols++] = tf;
691 /* We impose a minimum width on editable NSTextFields to
692 * stop them looking _completely_ silly. */
693 if (rightw < 75) rightw = 75;
695 tf = [[NSTextField alloc] initWithFrame:tmprect];
696 [tf setEditable:YES];
697 [tf setSelectable:YES];
698 [tf setBordered:YES];
699 [[tf cell] setTitle:[NSString stringWithCString:i->sval]];
702 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
703 if (rightw < rect.size.width + 1) rightw = rect.size.width + 1;
704 cfg_controls[cfg_ncontrols++] = tf;
709 * A checkbox is an NSButton with a type of
712 b = [[NSButton alloc] initWithFrame:tmprect];
713 [b setBezelStyle:NSRoundedBezelStyle];
714 [b setButtonType:NSSwitchButton];
715 [b setTitle:[NSString stringWithCString:i->name]];
717 [b setState:(i->ival ? NSOnState : NSOffState)];
719 if (totalw < rect.size.width + 1) totalw = rect.size.width + 1;
720 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
721 cfg_controls[cfg_ncontrols++] = b;
726 * A pop-up menu control is an NSPopUpButton, which
727 * takes an embedded NSMenu. We also need an
728 * NSTextField to act as a label.
731 tf = [[NSTextField alloc] initWithFrame:tmprect];
733 [tf setSelectable:NO];
735 [tf setDrawsBackground:NO];
736 [[tf cell] setTitle:[NSString stringWithCString:i->name]];
739 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
740 if (leftw < rect.size.width + 1) leftw = rect.size.width + 1;
741 cfg_controls[cfg_ncontrols++] = tf;
743 pb = [[NSPopUpButton alloc] initWithFrame:tmprect pullsDown:NO];
744 [pb setBezelStyle:NSRoundedBezelStyle];
754 while (*p && *p != c) p++;
756 [pb addItemWithTitle:[NSString stringWithCString:q
762 [pb selectItemAtIndex:i->ival];
766 if (rightw < rect.size.width + 1) rightw = rect.size.width + 1;
767 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
768 cfg_controls[cfg_ncontrols++] = pb;
772 h += SPACING + thish;
775 if (totalw < leftw + SPACING + rightw)
776 totalw = leftw + SPACING + rightw;
777 if (totalw > leftw + SPACING + rightw) {
778 int excess = totalw - (leftw + SPACING + rightw);
779 int leftexcess = leftw * excess / (leftw + rightw);
780 int rightexcess = excess - leftexcess;
782 rightw += rightexcess;
786 * Now go through the list again, setting the final position
791 for (i = cfg; i->type != C_END; i++) {
798 * These two are treated identically, since both expect
799 * a control on the left and another on the right.
801 rect = [cfg_controls[k] frame];
802 if (thish < rect.size.height + 1)
803 thish = rect.size.height + 1;
804 rect = [cfg_controls[k+1] frame];
805 if (thish < rect.size.height + 1)
806 thish = rect.size.height + 1;
807 rect = [cfg_controls[k] frame];
808 rect.origin.y = y - thish/2 - rect.size.height/2;
809 rect.origin.x = SPACING;
810 rect.size.width = leftw;
811 [cfg_controls[k] setFrame:rect];
812 rect = [cfg_controls[k+1] frame];
813 rect.origin.y = y - thish/2 - rect.size.height/2;
814 rect.origin.x = 2 * SPACING + leftw;
815 rect.size.width = rightw;
816 [cfg_controls[k+1] setFrame:rect];
821 rect = [cfg_controls[k] frame];
822 if (thish < rect.size.height + 1)
823 thish = rect.size.height + 1;
824 rect.origin.y = y - thish/2 - rect.size.height/2;
825 rect.origin.x = SPACING;
826 rect.size.width = totalw;
827 [cfg_controls[k] setFrame:rect];
834 assert(k == cfg_ncontrols);
836 [cancel setFrame:NSMakeRect(SPACING+totalw/4-actw/2, SPACING, actw, acth)];
837 [ok setFrame:NSMakeRect(SPACING+3*totalw/4-actw/2, SPACING, actw, acth)];
839 sheet = [[NSWindow alloc]
840 initWithContentRect:NSMakeRect(0,0,totalw + 2*SPACING,h)
841 styleMask:NSTitledWindowMask | NSClosableWindowMask
842 backing:NSBackingStoreBuffered
845 [[sheet contentView] addSubview:cancel];
846 [[sheet contentView] addSubview:ok];
848 for (k = 0; k < cfg_ncontrols; k++)
849 [[sheet contentView] addSubview:cfg_controls[k]];
851 [NSApp beginSheet:sheet modalForWindow:self
852 modalDelegate:nil didEndSelector:nil contextInfo:nil];
855 - (void)specificGame:(id)sender
857 [self startConfigureSheet:CFG_SEED];
860 - (void)customGameType:(id)sender
862 [self startConfigureSheet:CFG_SETTINGS];
865 - (void)sheetEndWithStatus:(BOOL)update
867 assert(sheet != NULL);
868 [NSApp endSheet:sheet];
869 [sheet orderOut:self];
877 for (i = cfg; i->type != C_END; i++) {
881 i->sval = dupstr([[[(id)cfg_controls[k+1] cell]
886 i->ival = [(id)cfg_controls[k] state] == NSOnState;
890 i->ival = [(id)cfg_controls[k+1] indexOfSelectedItem];
896 error = midend_set_config(me, cfg_which, cfg);
898 NSAlert *alert = [[[NSAlert alloc] init] autorelease];
899 [alert addButtonWithTitle:@"OK"];
900 [alert setInformativeText:[NSString stringWithCString:error]];
901 [alert beginSheetModalForWindow:self modalDelegate:nil
902 didEndSelector:nil contextInfo:nil];
905 [self resizeForNewGameParams];
911 - (void)sheetOKButton:(id)sender
913 [self sheetEndWithStatus:YES];
915 - (void)sheetCancelButton:(id)sender
917 [self sheetEndWithStatus:NO];
923 * Drawing routines called by the midend.
925 void draw_polygon(frontend *fe, int *coords, int npoints,
926 int fill, int colour)
928 NSBezierPath *path = [NSBezierPath bezierPath];
931 [[NSGraphicsContext currentContext] setShouldAntialias:YES];
933 assert(colour >= 0 && colour < fe->ncolours);
934 [fe->colours[colour] set];
936 for (i = 0; i < npoints; i++) {
937 NSPoint p = { coords[i*2] + 0.5, coords[i*2+1] + 0.5 };
939 [path moveToPoint:p];
941 [path lineToPoint:p];
951 void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour)
953 NSBezierPath *path = [NSBezierPath bezierPath];
954 NSPoint p1 = { x1 + 0.5, y1 + 0.5 }, p2 = { x2 + 0.5, y2 + 0.5 };
956 [[NSGraphicsContext currentContext] setShouldAntialias:NO];
958 assert(colour >= 0 && colour < fe->ncolours);
959 [fe->colours[colour] set];
961 [path moveToPoint:p1];
962 [path lineToPoint:p2];
965 void draw_rect(frontend *fe, int x, int y, int w, int h, int colour)
967 NSRect r = { {x,y}, {w,h} };
969 [[NSGraphicsContext currentContext] setShouldAntialias:NO];
971 assert(colour >= 0 && colour < fe->ncolours);
972 [fe->colours[colour] set];
976 void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize,
977 int align, int colour, char *text)
979 NSString *string = [NSString stringWithCString:text];
985 [[NSGraphicsContext currentContext] setShouldAntialias:YES];
987 assert(colour >= 0 && colour < fe->ncolours);
989 if (fonttype == FONT_FIXED)
990 font = [NSFont userFixedPitchFontOfSize:fontsize];
992 font = [NSFont userFontOfSize:fontsize];
994 attr = [NSDictionary dictionaryWithObjectsAndKeys:
995 fe->colours[colour], NSForegroundColorAttributeName,
996 font, NSFontAttributeName, nil];
1001 size = [string sizeWithAttributes:attr];
1002 if (align & ALIGN_HRIGHT)
1003 point.x -= size.width;
1004 else if (align & ALIGN_HCENTRE)
1005 point.x -= size.width / 2;
1006 if (align & ALIGN_VCENTRE)
1007 point.y -= size.height / 2;
1009 [string drawAtPoint:point withAttributes:attr];
1011 void draw_update(frontend *fe, int x, int y, int w, int h)
1015 void clip(frontend *fe, int x, int y, int w, int h)
1017 NSRect r = { {x,y}, {w,h} };
1020 [[NSGraphicsContext currentContext] saveGraphicsState];
1021 [NSBezierPath clipRect:r];
1024 void unclip(frontend *fe)
1027 [[NSGraphicsContext currentContext] restoreGraphicsState];
1028 fe->clipped = FALSE;
1030 void start_draw(frontend *fe)
1032 [fe->image lockFocus];
1033 fe->clipped = FALSE;
1035 void end_draw(frontend *fe)
1037 [fe->image unlockFocus];
1038 [fe->view setNeedsDisplay];
1041 void deactivate_timer(frontend *fe)
1043 [fe->window deactivateTimer];
1045 void activate_timer(frontend *fe)
1047 [fe->window activateTimer];
1050 /* ----------------------------------------------------------------------
1051 * AppController: the object which receives the messages from all
1052 * menu selections that aren't standard OS X functions.
1054 @interface AppController : NSObject
1057 - (IBAction)newGame:(id)sender;
1060 @implementation AppController
1062 - (IBAction)newGame:(id)sender
1064 const game *g = [sender getPayload];
1067 win = [[GameWindow alloc] initWithGame:g];
1068 [win makeKeyAndOrderFront:self];
1073 /* ----------------------------------------------------------------------
1074 * Main program. Constructs the menus and runs the application.
1076 int main(int argc, char **argv)
1078 NSAutoreleasePool *pool;
1081 AppController *controller;
1084 pool = [[NSAutoreleasePool alloc] init];
1086 icon = [NSImage imageNamed:@"NSApplicationIcon"];
1087 [NSApplication sharedApplication];
1088 [NSApp setApplicationIconImage:icon];
1090 controller = [[[AppController alloc] init] autorelease];
1092 [NSApp setMainMenu: newmenu("Main Menu")];
1094 menu = newsubmenu([NSApp mainMenu], "Apple Menu");
1095 [NSApp setServicesMenu:newsubmenu(menu, "Services")];
1096 [menu addItem:[NSMenuItem separatorItem]];
1097 item = newitem(menu, "Hide Puzzles", "h", NSApp, @selector(hide:));
1098 item = newitem(menu, "Hide Others", "o-h", NSApp, @selector(hideOtherApplications:));
1099 item = newitem(menu, "Show All", "", NSApp, @selector(unhideAllApplications:));
1100 [menu addItem:[NSMenuItem separatorItem]];
1101 item = newitem(menu, "Quit", "q", NSApp, @selector(terminate:));
1102 [NSApp setAppleMenu: menu];
1104 menu = newsubmenu([NSApp mainMenu], "Open");
1108 for (i = 0; i < gamecount; i++) {
1110 initnewitem([DataMenuItem allocWithZone:[NSMenu menuZone]],
1111 menu, gamelist[i]->name, "", controller,
1112 @selector(newGame:));
1113 [item setPayload:(void *)gamelist[i]];
1117 menu = newsubmenu([NSApp mainMenu], "Game");
1118 item = newitem(menu, "New", "n", NULL, @selector(newGame:));
1119 item = newitem(menu, "Restart", "r", NULL, @selector(restartGame:));
1120 item = newitem(menu, "Specific", "", NULL, @selector(specificGame:));
1121 [menu addItem:[NSMenuItem separatorItem]];
1122 item = newitem(menu, "Undo", "z", NULL, @selector(undoMove:));
1123 item = newitem(menu, "Redo", "S-z", NULL, @selector(redoMove:));
1124 [menu addItem:[NSMenuItem separatorItem]];
1125 item = newitem(menu, "Close", "w", NULL, @selector(performClose:));
1127 menu = newsubmenu([NSApp mainMenu], "Type");
1129 item = newitem(menu, "Custom", "", NULL, @selector(customGameType:));
1131 menu = newsubmenu([NSApp mainMenu], "Window");
1132 [NSApp setWindowsMenu: menu];
1133 item = newitem(menu, "Minimise Window", "m", NULL, @selector(performMiniaturize:));