2 * Mac OS X / Cocoa front end to puzzles.
4 * Actually unfinished things left to do:
6 * - proper fatal errors.
8 * - Find out how to do help, and do some. We have a help file; at
9 * _worst_ this should involve a new Halibut back end, but I
10 * think help is HTML round here anyway so perhaps we can work
11 * with what we already have.
13 * - Why are the right and bottom edges of the Pattern grid one
14 * pixel thinner than they should be?
16 * Mac interface issues that possibly could be done better:
18 * - is there a better approach to frontend_default_colour?
20 * - do we need any more options in the Window menu?
22 * - not sure what I should be doing about default window
23 * placement. Centring new windows is a bit feeble, but what's
24 * better? Is there a standard way to tell the OS "here's the
25 * _size_ of window I want, now use your best judgment about the
28 * - a brief frob of the Mac numeric keypad suggests that it
29 * generates numbers no matter what you do. I wonder if I should
30 * try to figure out a way of detecting keypad codes so I can
31 * implement UP_LEFT and friends. Alternatively, perhaps I
32 * should simply assign the number keys to UP_LEFT et al?
33 * They're not in use for anything else right now.
35 * - see if we can do anything to one-button-ise the multi-button
36 * dependent puzzle UIs:
37 * - Pattern is a _little_ unwieldy but not too bad (since
38 * generally you never need the middle button unless you've
39 * made a mistake, so it's just click versus command-click).
40 * - Net is utterly vile; having normal click be one rotate and
41 * command-click be the other introduces a horrid asymmetry,
42 * and yet requiring a shift key for _each_ click would be
43 * even worse because rotation feels as if it ought to be the
44 * default action. I fear this is why the Flash Net had the
47 * - Should we _return_ to a game configuration sheet once an
48 * error is reported by midend_set_config, to allow the user to
49 * correct the one faulty input and keep the other five OK ones?
50 * The Apple `one sheet at a time' restriction would require me
51 * to do this by closing the config sheet, opening the alert
52 * sheet, and then reopening the config sheet when the alert is
53 * closed; and the human interface types, who presumably
54 * invented the one-sheet-at-a-time rule for good reasons, might
55 * look with disfavour on me trying to get round them to fake a
56 * nested sheet. On the other hand I think there are good
57 * practical reasons for wanting it that way. Uncertain.
59 * Grotty implementation details that could probably be improved:
61 * - I am _utterly_ unconvinced that NSImageView was the right way
62 * to go about having a window with a reliable backing store! It
63 * just doesn't feel right; NSImageView is a _control_. Is there
66 * - Resizing is currently very bad; rather than bother to work
67 * out how to resize the NSImageView, I just splatter and
73 #import <Cocoa/Cocoa.h>
76 void fatal(char *fmt, ...)
78 /* FIXME: This will do for testing, but should be GUI-ish instead. */
81 fprintf(stderr, "fatal error: ");
84 vfprintf(stderr, fmt, ap);
87 fprintf(stderr, "\n");
91 void frontend_default_colour(frontend *fe, float *output)
94 output[0] = output[1] = output[2] = 0.8F;
97 void get_random_seed(void **randseed, int *randseedsize)
99 time_t *tp = snew(time_t);
101 *randseed = (void *)tp;
102 *randseedsize = sizeof(time_t);
105 /* ----------------------------------------------------------------------
110 * The `Type' menu. We frob this dynamically to allow the user to
111 * choose a preset set of settings from the current game.
115 /* ----------------------------------------------------------------------
116 * Tiny extension to NSMenuItem which carries a payload of a `void
117 * *', allowing several menu items to invoke the same message but
118 * pass different data through it.
120 @interface DataMenuItem : NSMenuItem
124 - (void)setPayload:(void *)d;
125 - (void *)getPayload;
127 @implementation DataMenuItem
128 - (void)setPayload:(void *)d
138 /* ----------------------------------------------------------------------
139 * Utility routines for constructing OS X menus.
142 NSMenu *newmenu(const char *title)
144 return [[[NSMenu allocWithZone:[NSMenu menuZone]]
145 initWithTitle:[NSString stringWithCString:title]]
149 NSMenu *newsubmenu(NSMenu *parent, const char *title)
154 item = [[[NSMenuItem allocWithZone:[NSMenu menuZone]]
155 initWithTitle:[NSString stringWithCString:title]
159 child = newmenu(title);
160 [item setEnabled:YES];
161 [item setSubmenu:child];
162 [parent addItem:item];
166 id initnewitem(NSMenuItem *item, NSMenu *parent, const char *title,
167 const char *key, id target, SEL action)
169 unsigned mask = NSCommandKeyMask;
171 if (key[strcspn(key, "-")]) {
172 while (*key && *key != '-') {
173 int c = tolower((unsigned char)*key);
175 mask |= NSShiftKeyMask;
176 } else if (c == 'o' || c == 'a') {
177 mask |= NSAlternateKeyMask;
185 item = [[item initWithTitle:[NSString stringWithCString:title]
187 keyEquivalent:[NSString stringWithCString:key]]
191 [item setKeyEquivalentModifierMask: mask];
193 [item setEnabled:YES];
194 [item setTarget:target];
195 [item setAction:action];
197 [parent addItem:item];
202 NSMenuItem *newitem(NSMenu *parent, char *title, char *key,
203 id target, SEL action)
205 return initnewitem([NSMenuItem allocWithZone:[NSMenu menuZone]],
206 parent, title, key, target, action);
209 /* ----------------------------------------------------------------------
210 * The front end presented to midend.c.
212 * This is mostly a subclass of NSWindow. The actual `frontend'
213 * structure passed to the midend contains a variety of pointers,
214 * including that window object but also including the image we
215 * draw on, an ImageView to display it in the window, and so on.
230 @interface MyImageView : NSImageView
234 - (void)setWindow:(GameWindow *)win;
236 - (void)mouseEvent:(NSEvent *)ev button:(int)b;
237 - (void)mouseDown:(NSEvent *)ev;
238 - (void)mouseDragged:(NSEvent *)ev;
239 - (void)mouseUp:(NSEvent *)ev;
240 - (void)rightMouseDown:(NSEvent *)ev;
241 - (void)rightMouseDragged:(NSEvent *)ev;
242 - (void)rightMouseUp:(NSEvent *)ev;
243 - (void)otherMouseDown:(NSEvent *)ev;
244 - (void)otherMouseDragged:(NSEvent *)ev;
245 - (void)otherMouseUp:(NSEvent *)ev;
248 @interface GameWindow : NSWindow
253 struct timeval last_time;
258 NSView **cfg_controls;
262 - (id)initWithGame:(const game *)g;
264 - (void)processButton:(int)b x:(int)x y:(int)y;
265 - (void)keyDown:(NSEvent *)ev;
266 - (void)activateTimer;
267 - (void)deactivateTimer;
268 - (void)setStatusLine:(NSString *)text;
271 @implementation MyImageView
273 - (void)setWindow:(GameWindow *)win
283 - (void)mouseEvent:(NSEvent *)ev button:(int)b
285 NSPoint point = [self convertPoint:[ev locationInWindow] fromView:nil];
286 [ourwin processButton:b x:point.x y:point.y];
289 - (void)mouseDown:(NSEvent *)ev
291 unsigned mod = [ev modifierFlags];
292 [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_BUTTON :
293 (mod & NSShiftKeyMask) ? MIDDLE_BUTTON :
296 - (void)mouseDragged:(NSEvent *)ev
298 unsigned mod = [ev modifierFlags];
299 [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_DRAG :
300 (mod & NSShiftKeyMask) ? MIDDLE_DRAG :
303 - (void)mouseUp:(NSEvent *)ev
305 unsigned mod = [ev modifierFlags];
306 [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_RELEASE :
307 (mod & NSShiftKeyMask) ? MIDDLE_RELEASE :
310 - (void)rightMouseDown:(NSEvent *)ev
312 unsigned mod = [ev modifierFlags];
313 [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_BUTTON :
316 - (void)rightMouseDragged:(NSEvent *)ev
318 unsigned mod = [ev modifierFlags];
319 [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_DRAG :
322 - (void)rightMouseUp:(NSEvent *)ev
324 unsigned mod = [ev modifierFlags];
325 [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_RELEASE :
328 - (void)otherMouseDown:(NSEvent *)ev
330 [self mouseEvent:ev button:MIDDLE_BUTTON];
332 - (void)otherMouseDragged:(NSEvent *)ev
334 [self mouseEvent:ev button:MIDDLE_DRAG];
336 - (void)otherMouseUp:(NSEvent *)ev
338 [self mouseEvent:ev button:MIDDLE_RELEASE];
342 @implementation GameWindow
343 - (void)setupContentView
349 frame = [status frame];
350 frame.origin.y = frame.size.height;
355 midend_size(me, &w, &h);
356 frame.size.width = w;
357 frame.size.height = h;
359 fe.image = [[NSImage alloc] initWithSize:frame.size];
360 [fe.image setFlipped:YES];
361 fe.view = [[MyImageView alloc] initWithFrame:frame];
362 [fe.view setImage:fe.image];
363 [fe.view setWindow:self];
367 [[self contentView] addSubview:fe.view];
369 - (id)initWithGame:(const game *)g
371 NSRect rect = { {0,0}, {0,0} }, rect2;
378 me = midend_new(&fe, ourgame);
380 * If we ever need to open a fresh window using a provided game
381 * ID, I think the right thing is to move most of this method
382 * into a new initWithGame:gameID: method, and have
383 * initWithGame: simply call that one and pass it NULL.
386 midend_size(me, &w, &h);
388 rect.size.height = h;
391 * Create the status bar, which will just be an NSTextField.
393 if (ourgame->wants_statusbar()) {
394 status = [[NSTextField alloc] initWithFrame:NSMakeRect(0,0,100,50)];
395 [status setEditable:NO];
396 [status setSelectable:NO];
397 [status setBordered:YES];
398 [status setBezeled:YES];
399 [status setBezelStyle:NSTextFieldSquareBezel];
400 [status setDrawsBackground:YES];
401 [[status cell] setTitle:@""];
403 rect2 = [status frame];
404 rect.size.height += rect2.size.height;
405 rect2.size.width = rect.size.width;
406 rect2.origin.x = rect2.origin.y = 0;
407 [status setFrame:rect2];
411 self = [super initWithContentRect:rect
412 styleMask:(NSTitledWindowMask | NSMiniaturizableWindowMask |
413 NSClosableWindowMask)
414 backing:NSBackingStoreBuffered
416 [self setTitle:[NSString stringWithCString:ourgame->name]];
422 colours = midend_colours(me, &ncolours);
423 fe.ncolours = ncolours;
424 fe.colours = snewn(ncolours, NSColor *);
426 for (i = 0; i < ncolours; i++) {
427 fe.colours[i] = [[NSColor colorWithDeviceRed:colours[i*3]
428 green:colours[i*3+1] blue:colours[i*3+2]
433 [self setupContentView];
435 [[self contentView] addSubview:status];
436 [self setIgnoresMouseEvents:NO];
438 [self center]; /* :-) */
446 for (i = 0; i < fe.ncolours; i++) {
447 [fe.colours[i] release];
451 return [super dealloc];
454 - (void)processButton:(int)b x:(int)x y:(int)y
456 if (!midend_process_key(me, x, y, b))
460 - (void)keyDown:(NSEvent *)ev
462 NSString *s = [ev characters];
463 int i, n = [s length];
465 for (i = 0; i < n; i++) {
466 int c = [s characterAtIndex:i];
469 * ASCII gets passed straight to midend_process_key.
470 * Anything above that has to be translated to our own
471 * function key codes.
475 case NSUpArrowFunctionKey:
478 case NSDownArrowFunctionKey:
481 case NSLeftArrowFunctionKey:
484 case NSRightArrowFunctionKey:
492 [self processButton:c x:-1 y:-1];
496 - (void)activateTimer
501 timer = [NSTimer scheduledTimerWithTimeInterval:0.02
502 target:self selector:@selector(timerTick:)
503 userInfo:nil repeats:YES];
504 gettimeofday(&last_time, NULL);
507 - (void)deactivateTimer
516 - (void)timerTick:(id)sender
520 gettimeofday(&now, NULL);
521 elapsed = ((now.tv_usec - last_time.tv_usec) * 0.000001F +
522 (now.tv_sec - last_time.tv_sec));
523 midend_timer(me, elapsed);
527 - (void)newGame:(id)sender
529 [self processButton:'n' x:-1 y:-1];
531 - (void)restartGame:(id)sender
533 [self processButton:'r' x:-1 y:-1];
535 - (void)undoMove:(id)sender
537 [self processButton:'u' x:-1 y:-1];
539 - (void)redoMove:(id)sender
541 [self processButton:'r'&0x1F x:-1 y:-1];
544 - (void)clearTypeMenu
546 while ([typemenu numberOfItems] > 1)
547 [typemenu removeItemAtIndex:0];
550 - (void)becomeKeyWindow
554 [self clearTypeMenu];
556 [super becomeKeyWindow];
558 n = midend_num_presets(me);
561 [typemenu insertItem:[NSMenuItem separatorItem] atIndex:0];
567 midend_fetch_preset(me, n, &name, ¶ms);
569 item = [[[DataMenuItem alloc]
570 initWithTitle:[NSString stringWithCString:name]
571 action:NULL keyEquivalent:@""]
574 [item setEnabled:YES];
575 [item setTarget:self];
576 [item setAction:@selector(presetGame:)];
577 [item setPayload:params];
579 [typemenu insertItem:item atIndex:0];
584 - (void)resignKeyWindow
586 [self clearTypeMenu];
587 [super resignKeyWindow];
592 [self clearTypeMenu];
596 - (void)resizeForNewGameParams
601 midend_size(me, &w, &h);
605 NSDisableScreenUpdates();
606 [self setContentSize:size];
607 [self setupContentView];
608 NSEnableScreenUpdates();
611 - (void)presetGame:(id)sender
613 game_params *params = [sender getPayload];
615 midend_set_params(me, params);
618 [self resizeForNewGameParams];
621 - (void)startConfigureSheet:(int)which
623 NSButton *ok, *cancel;
624 int actw, acth, leftw, rightw, totalw, h, thish, y;
626 NSRect rect, tmprect;
627 const int SPACING = 16;
635 assert(sheet == NULL);
638 * Every control we create here is going to have this size
639 * until we tell it to calculate a better one.
641 tmprect = NSMakeRect(0, 0, 100, 50);
644 * Set up OK and Cancel buttons. (Actually, MacOS doesn't seem
645 * to be fond of generic OK and Cancel wording, so I'm going to
646 * rename them to something nicer.)
650 cancel = [[NSButton alloc] initWithFrame:tmprect];
651 [cancel setBezelStyle:NSRoundedBezelStyle];
652 [cancel setTitle:@"Abandon"];
653 [cancel setTarget:self];
654 [cancel setKeyEquivalent:@"\033"];
655 [cancel setAction:@selector(sheetCancelButton:)];
657 rect = [cancel frame];
658 if (actw < rect.size.width) actw = rect.size.width;
659 if (acth < rect.size.height) acth = rect.size.height;
661 ok = [[NSButton alloc] initWithFrame:tmprect];
662 [ok setBezelStyle:NSRoundedBezelStyle];
663 [ok setTitle:@"Accept"];
665 [ok setKeyEquivalent:@"\r"];
666 [ok setAction:@selector(sheetOKButton:)];
669 if (actw < rect.size.width) actw = rect.size.width;
670 if (acth < rect.size.height) acth = rect.size.height;
672 totalw = SPACING + 2 * actw;
673 h = 2 * SPACING + acth;
676 * Now fetch the midend config data and go through it creating
679 cfg = midend_get_config(me, which, &title);
680 sfree(title); /* FIXME: should we use this somehow? */
683 cfg_ncontrols = cfg_controlsize = 0;
686 for (i = cfg; i->type != C_END; i++) {
687 if (cfg_controlsize < cfg_ncontrols + 5) {
688 cfg_controlsize = cfg_ncontrols + 32;
689 cfg_controls = sresize(cfg_controls, cfg_controlsize, NSView *);
697 * Two NSTextFields, one being a label and the other
701 tf = [[NSTextField alloc] initWithFrame:tmprect];
703 [tf setSelectable:NO];
705 [tf setDrawsBackground:NO];
706 [[tf cell] setTitle:[NSString stringWithCString:i->name]];
709 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
710 if (leftw < rect.size.width + 1) leftw = rect.size.width + 1;
711 cfg_controls[cfg_ncontrols++] = tf;
713 /* We impose a minimum width on editable NSTextFields to
714 * stop them looking _completely_ silly. */
715 if (rightw < 75) rightw = 75;
717 tf = [[NSTextField alloc] initWithFrame:tmprect];
718 [tf setEditable:YES];
719 [tf setSelectable:YES];
720 [tf setBordered:YES];
721 [[tf cell] setTitle:[NSString stringWithCString:i->sval]];
724 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
725 if (rightw < rect.size.width + 1) rightw = rect.size.width + 1;
726 cfg_controls[cfg_ncontrols++] = tf;
731 * A checkbox is an NSButton with a type of
734 b = [[NSButton alloc] initWithFrame:tmprect];
735 [b setBezelStyle:NSRoundedBezelStyle];
736 [b setButtonType:NSSwitchButton];
737 [b setTitle:[NSString stringWithCString:i->name]];
739 [b setState:(i->ival ? NSOnState : NSOffState)];
741 if (totalw < rect.size.width + 1) totalw = rect.size.width + 1;
742 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
743 cfg_controls[cfg_ncontrols++] = b;
748 * A pop-up menu control is an NSPopUpButton, which
749 * takes an embedded NSMenu. We also need an
750 * NSTextField to act as a label.
753 tf = [[NSTextField alloc] initWithFrame:tmprect];
755 [tf setSelectable:NO];
757 [tf setDrawsBackground:NO];
758 [[tf cell] setTitle:[NSString stringWithCString:i->name]];
761 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
762 if (leftw < rect.size.width + 1) leftw = rect.size.width + 1;
763 cfg_controls[cfg_ncontrols++] = tf;
765 pb = [[NSPopUpButton alloc] initWithFrame:tmprect pullsDown:NO];
766 [pb setBezelStyle:NSRoundedBezelStyle];
776 while (*p && *p != c) p++;
778 [pb addItemWithTitle:[NSString stringWithCString:q
784 [pb selectItemAtIndex:i->ival];
788 if (rightw < rect.size.width + 1) rightw = rect.size.width + 1;
789 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
790 cfg_controls[cfg_ncontrols++] = pb;
794 h += SPACING + thish;
797 if (totalw < leftw + SPACING + rightw)
798 totalw = leftw + SPACING + rightw;
799 if (totalw > leftw + SPACING + rightw) {
800 int excess = totalw - (leftw + SPACING + rightw);
801 int leftexcess = leftw * excess / (leftw + rightw);
802 int rightexcess = excess - leftexcess;
804 rightw += rightexcess;
808 * Now go through the list again, setting the final position
813 for (i = cfg; i->type != C_END; i++) {
820 * These two are treated identically, since both expect
821 * a control on the left and another on the right.
823 rect = [cfg_controls[k] frame];
824 if (thish < rect.size.height + 1)
825 thish = rect.size.height + 1;
826 rect = [cfg_controls[k+1] frame];
827 if (thish < rect.size.height + 1)
828 thish = rect.size.height + 1;
829 rect = [cfg_controls[k] frame];
830 rect.origin.y = y - thish/2 - rect.size.height/2;
831 rect.origin.x = SPACING;
832 rect.size.width = leftw;
833 [cfg_controls[k] setFrame:rect];
834 rect = [cfg_controls[k+1] frame];
835 rect.origin.y = y - thish/2 - rect.size.height/2;
836 rect.origin.x = 2 * SPACING + leftw;
837 rect.size.width = rightw;
838 [cfg_controls[k+1] setFrame:rect];
843 rect = [cfg_controls[k] frame];
844 if (thish < rect.size.height + 1)
845 thish = rect.size.height + 1;
846 rect.origin.y = y - thish/2 - rect.size.height/2;
847 rect.origin.x = SPACING;
848 rect.size.width = totalw;
849 [cfg_controls[k] setFrame:rect];
856 assert(k == cfg_ncontrols);
858 [cancel setFrame:NSMakeRect(SPACING+totalw/4-actw/2, SPACING, actw, acth)];
859 [ok setFrame:NSMakeRect(SPACING+3*totalw/4-actw/2, SPACING, actw, acth)];
861 sheet = [[NSWindow alloc]
862 initWithContentRect:NSMakeRect(0,0,totalw + 2*SPACING,h)
863 styleMask:NSTitledWindowMask | NSClosableWindowMask
864 backing:NSBackingStoreBuffered
867 [[sheet contentView] addSubview:cancel];
868 [[sheet contentView] addSubview:ok];
870 for (k = 0; k < cfg_ncontrols; k++)
871 [[sheet contentView] addSubview:cfg_controls[k]];
873 [NSApp beginSheet:sheet modalForWindow:self
874 modalDelegate:nil didEndSelector:nil contextInfo:nil];
877 - (void)specificGame:(id)sender
879 [self startConfigureSheet:CFG_SEED];
882 - (void)customGameType:(id)sender
884 [self startConfigureSheet:CFG_SETTINGS];
887 - (void)sheetEndWithStatus:(BOOL)update
889 assert(sheet != NULL);
890 [NSApp endSheet:sheet];
891 [sheet orderOut:self];
899 for (i = cfg; i->type != C_END; i++) {
903 i->sval = dupstr([[[(id)cfg_controls[k+1] cell]
908 i->ival = [(id)cfg_controls[k] state] == NSOnState;
912 i->ival = [(id)cfg_controls[k+1] indexOfSelectedItem];
918 error = midend_set_config(me, cfg_which, cfg);
920 NSAlert *alert = [[[NSAlert alloc] init] autorelease];
921 [alert addButtonWithTitle:@"OK"];
922 [alert setInformativeText:[NSString stringWithCString:error]];
923 [alert beginSheetModalForWindow:self modalDelegate:nil
924 didEndSelector:nil contextInfo:nil];
927 [self resizeForNewGameParams];
933 - (void)sheetOKButton:(id)sender
935 [self sheetEndWithStatus:YES];
937 - (void)sheetCancelButton:(id)sender
939 [self sheetEndWithStatus:NO];
942 - (void)setStatusLine:(NSString *)text
944 [[status cell] setTitle:text];
950 * Drawing routines called by the midend.
952 void draw_polygon(frontend *fe, int *coords, int npoints,
953 int fill, int colour)
955 NSBezierPath *path = [NSBezierPath bezierPath];
958 [[NSGraphicsContext currentContext] setShouldAntialias:YES];
960 assert(colour >= 0 && colour < fe->ncolours);
961 [fe->colours[colour] set];
963 for (i = 0; i < npoints; i++) {
964 NSPoint p = { coords[i*2] + 0.5, coords[i*2+1] + 0.5 };
966 [path moveToPoint:p];
968 [path lineToPoint:p];
978 void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour)
980 NSBezierPath *path = [NSBezierPath bezierPath];
981 NSPoint p1 = { x1 + 0.5, y1 + 0.5 }, p2 = { x2 + 0.5, y2 + 0.5 };
983 [[NSGraphicsContext currentContext] setShouldAntialias:NO];
985 assert(colour >= 0 && colour < fe->ncolours);
986 [fe->colours[colour] set];
988 [path moveToPoint:p1];
989 [path lineToPoint:p2];
992 void draw_rect(frontend *fe, int x, int y, int w, int h, int colour)
994 NSRect r = { {x,y}, {w,h} };
996 [[NSGraphicsContext currentContext] setShouldAntialias:NO];
998 assert(colour >= 0 && colour < fe->ncolours);
999 [fe->colours[colour] set];
1003 void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize,
1004 int align, int colour, char *text)
1006 NSString *string = [NSString stringWithCString:text];
1012 [[NSGraphicsContext currentContext] setShouldAntialias:YES];
1014 assert(colour >= 0 && colour < fe->ncolours);
1016 if (fonttype == FONT_FIXED)
1017 font = [NSFont userFixedPitchFontOfSize:fontsize];
1019 font = [NSFont userFontOfSize:fontsize];
1021 attr = [NSDictionary dictionaryWithObjectsAndKeys:
1022 fe->colours[colour], NSForegroundColorAttributeName,
1023 font, NSFontAttributeName, nil];
1028 size = [string sizeWithAttributes:attr];
1029 if (align & ALIGN_HRIGHT)
1030 point.x -= size.width;
1031 else if (align & ALIGN_HCENTRE)
1032 point.x -= size.width / 2;
1033 if (align & ALIGN_VCENTRE)
1034 point.y -= size.height / 2;
1036 [string drawAtPoint:point withAttributes:attr];
1038 void draw_update(frontend *fe, int x, int y, int w, int h)
1042 void clip(frontend *fe, int x, int y, int w, int h)
1044 NSRect r = { {x,y}, {w,h} };
1047 [[NSGraphicsContext currentContext] saveGraphicsState];
1048 [NSBezierPath clipRect:r];
1051 void unclip(frontend *fe)
1054 [[NSGraphicsContext currentContext] restoreGraphicsState];
1055 fe->clipped = FALSE;
1057 void start_draw(frontend *fe)
1059 [fe->image lockFocus];
1060 fe->clipped = FALSE;
1062 void end_draw(frontend *fe)
1064 [fe->image unlockFocus];
1065 [fe->view setNeedsDisplay];
1068 void deactivate_timer(frontend *fe)
1070 [fe->window deactivateTimer];
1072 void activate_timer(frontend *fe)
1074 [fe->window activateTimer];
1077 void status_bar(frontend *fe, char *text)
1079 [fe->window setStatusLine:[NSString stringWithCString:text]];
1082 /* ----------------------------------------------------------------------
1083 * AppController: the object which receives the messages from all
1084 * menu selections that aren't standard OS X functions.
1086 @interface AppController : NSObject
1089 - (IBAction)newGame:(id)sender;
1092 @implementation AppController
1094 - (IBAction)newGame:(id)sender
1096 const game *g = [sender getPayload];
1099 win = [[GameWindow alloc] initWithGame:g];
1100 [win makeKeyAndOrderFront:self];
1103 - (NSMenu *)applicationDockMenu:(NSApplication *)sender
1105 NSMenu *menu = newmenu("Dock Menu");
1109 for (i = 0; i < gamecount; i++) {
1111 initnewitem([DataMenuItem allocWithZone:[NSMenu menuZone]],
1112 menu, gamelist[i]->name, "", self,
1113 @selector(newGame:));
1114 [item setPayload:(void *)gamelist[i]];
1122 /* ----------------------------------------------------------------------
1123 * Main program. Constructs the menus and runs the application.
1125 int main(int argc, char **argv)
1127 NSAutoreleasePool *pool;
1130 AppController *controller;
1133 pool = [[NSAutoreleasePool alloc] init];
1135 icon = [NSImage imageNamed:@"NSApplicationIcon"];
1136 [NSApplication sharedApplication];
1137 [NSApp setApplicationIconImage:icon];
1139 controller = [[[AppController alloc] init] autorelease];
1140 [NSApp setDelegate:controller];
1142 [NSApp setMainMenu: newmenu("Main Menu")];
1144 menu = newsubmenu([NSApp mainMenu], "Apple Menu");
1145 [NSApp setServicesMenu:newsubmenu(menu, "Services")];
1146 [menu addItem:[NSMenuItem separatorItem]];
1147 item = newitem(menu, "Hide Puzzles", "h", NSApp, @selector(hide:));
1148 item = newitem(menu, "Hide Others", "o-h", NSApp, @selector(hideOtherApplications:));
1149 item = newitem(menu, "Show All", "", NSApp, @selector(unhideAllApplications:));
1150 [menu addItem:[NSMenuItem separatorItem]];
1151 item = newitem(menu, "Quit", "q", NSApp, @selector(terminate:));
1152 [NSApp setAppleMenu: menu];
1154 menu = newsubmenu([NSApp mainMenu], "Open");
1158 for (i = 0; i < gamecount; i++) {
1160 initnewitem([DataMenuItem allocWithZone:[NSMenu menuZone]],
1161 menu, gamelist[i]->name, "", controller,
1162 @selector(newGame:));
1163 [item setPayload:(void *)gamelist[i]];
1167 menu = newsubmenu([NSApp mainMenu], "Game");
1168 item = newitem(menu, "New", "n", NULL, @selector(newGame:));
1169 item = newitem(menu, "Restart", "r", NULL, @selector(restartGame:));
1170 item = newitem(menu, "Specific", "", NULL, @selector(specificGame:));
1171 [menu addItem:[NSMenuItem separatorItem]];
1172 item = newitem(menu, "Undo", "z", NULL, @selector(undoMove:));
1173 item = newitem(menu, "Redo", "S-z", NULL, @selector(redoMove:));
1174 [menu addItem:[NSMenuItem separatorItem]];
1175 item = newitem(menu, "Close", "w", NULL, @selector(performClose:));
1177 menu = newsubmenu([NSApp mainMenu], "Type");
1179 item = newitem(menu, "Custom", "", NULL, @selector(customGameType:));
1181 menu = newsubmenu([NSApp mainMenu], "Window");
1182 [NSApp setWindowsMenu: menu];
1183 item = newitem(menu, "Minimise Window", "m", NULL, @selector(performMiniaturize:));