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 * - Can we arrange for a pop-up menu from the Dock icon which
14 * launches specific games, perhaps?
15 * + apparently we can; see the NSApplication method
16 * `applicationDockMenu:'. Good good. Do so.
18 * - Why are the right and bottom edges of the Pattern grid one
19 * pixel thinner than they should be?
21 * Mac interface issues that possibly could be done better:
23 * - is there a better approach to frontend_default_colour?
25 * - do we need any more options in the Window menu?
27 * - not sure what I should be doing about default window
28 * placement. Centring new windows is a bit feeble, but what's
29 * better? Is there a standard way to tell the OS "here's the
30 * _size_ of window I want, now use your best judgment about the
33 * - a brief frob of the Mac numeric keypad suggests that it
34 * generates numbers no matter what you do. I wonder if I should
35 * try to figure out a way of detecting keypad codes so I can
36 * implement UP_LEFT and friends. Alternatively, perhaps I
37 * should simply assign the number keys to UP_LEFT et al?
38 * They're not in use for anything else right now.
40 * - see if we can do anything to one-button-ise the multi-button
41 * dependent puzzle UIs:
42 * - Pattern is a _little_ unwieldy but not too bad (since
43 * generally you never need the middle button unless you've
44 * made a mistake, so it's just click versus command-click).
45 * - Net is utterly vile; having normal click be one rotate and
46 * command-click be the other introduces a horrid asymmetry,
47 * and yet requiring a shift key for _each_ click would be
48 * even worse because rotation feels as if it ought to be the
49 * default action. I fear this is why the Flash Net had the
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;
102 void get_random_seed(void **randseed, int *randseedsize)
104 time_t *tp = snew(time_t);
106 *randseed = (void *)tp;
107 *randseedsize = sizeof(time_t);
110 /* ----------------------------------------------------------------------
115 * The `Type' menu. We frob this dynamically to allow the user to
116 * choose a preset set of settings from the current game.
120 /* ----------------------------------------------------------------------
121 * Tiny extension to NSMenuItem which carries a payload of a `void
122 * *', allowing several menu items to invoke the same message but
123 * pass different data through it.
125 @interface DataMenuItem : NSMenuItem
129 - (void)setPayload:(void *)d;
130 - (void *)getPayload;
132 @implementation DataMenuItem
133 - (void)setPayload:(void *)d
143 /* ----------------------------------------------------------------------
144 * Utility routines for constructing OS X menus.
147 NSMenu *newmenu(const char *title)
149 return [[[NSMenu allocWithZone:[NSMenu menuZone]]
150 initWithTitle:[NSString stringWithCString:title]]
154 NSMenu *newsubmenu(NSMenu *parent, const char *title)
159 item = [[[NSMenuItem allocWithZone:[NSMenu menuZone]]
160 initWithTitle:[NSString stringWithCString:title]
164 child = newmenu(title);
165 [item setEnabled:YES];
166 [item setSubmenu:child];
167 [parent addItem:item];
171 id initnewitem(NSMenuItem *item, NSMenu *parent, const char *title,
172 const char *key, id target, SEL action)
174 unsigned mask = NSCommandKeyMask;
176 if (key[strcspn(key, "-")]) {
177 while (*key && *key != '-') {
178 int c = tolower((unsigned char)*key);
180 mask |= NSShiftKeyMask;
181 } else if (c == 'o' || c == 'a') {
182 mask |= NSAlternateKeyMask;
190 item = [[item initWithTitle:[NSString stringWithCString:title]
192 keyEquivalent:[NSString stringWithCString:key]]
196 [item setKeyEquivalentModifierMask: mask];
198 [item setEnabled:YES];
199 [item setTarget:target];
200 [item setAction:action];
202 [parent addItem:item];
207 NSMenuItem *newitem(NSMenu *parent, char *title, char *key,
208 id target, SEL action)
210 return initnewitem([NSMenuItem allocWithZone:[NSMenu menuZone]],
211 parent, title, key, target, action);
214 /* ----------------------------------------------------------------------
215 * The front end presented to midend.c.
217 * This is mostly a subclass of NSWindow. The actual `frontend'
218 * structure passed to the midend contains a variety of pointers,
219 * including that window object but also including the image we
220 * draw on, an ImageView to display it in the window, and so on.
235 @interface MyImageView : NSImageView
239 - (void)setWindow:(GameWindow *)win;
241 - (void)mouseEvent:(NSEvent *)ev button:(int)b;
242 - (void)mouseDown:(NSEvent *)ev;
243 - (void)mouseDragged:(NSEvent *)ev;
244 - (void)mouseUp:(NSEvent *)ev;
245 - (void)rightMouseDown:(NSEvent *)ev;
246 - (void)rightMouseDragged:(NSEvent *)ev;
247 - (void)rightMouseUp:(NSEvent *)ev;
248 - (void)otherMouseDown:(NSEvent *)ev;
249 - (void)otherMouseDragged:(NSEvent *)ev;
250 - (void)otherMouseUp:(NSEvent *)ev;
253 @interface GameWindow : NSWindow
258 struct timeval last_time;
263 NSView **cfg_controls;
267 - (id)initWithGame:(const game *)g;
269 - (void)processButton:(int)b x:(int)x y:(int)y;
270 - (void)keyDown:(NSEvent *)ev;
271 - (void)activateTimer;
272 - (void)deactivateTimer;
273 - (void)setStatusLine:(NSString *)text;
276 @implementation MyImageView
278 - (void)setWindow:(GameWindow *)win
288 - (void)mouseEvent:(NSEvent *)ev button:(int)b
290 NSPoint point = [self convertPoint:[ev locationInWindow] fromView:nil];
291 [ourwin processButton:b x:point.x y:point.y];
294 - (void)mouseDown:(NSEvent *)ev
296 unsigned mod = [ev modifierFlags];
297 [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_BUTTON :
298 (mod & NSShiftKeyMask) ? MIDDLE_BUTTON :
301 - (void)mouseDragged:(NSEvent *)ev
303 unsigned mod = [ev modifierFlags];
304 [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_DRAG :
305 (mod & NSShiftKeyMask) ? MIDDLE_DRAG :
308 - (void)mouseUp:(NSEvent *)ev
310 unsigned mod = [ev modifierFlags];
311 [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_RELEASE :
312 (mod & NSShiftKeyMask) ? MIDDLE_RELEASE :
315 - (void)rightMouseDown:(NSEvent *)ev
317 unsigned mod = [ev modifierFlags];
318 [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_BUTTON :
321 - (void)rightMouseDragged:(NSEvent *)ev
323 unsigned mod = [ev modifierFlags];
324 [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_DRAG :
327 - (void)rightMouseUp:(NSEvent *)ev
329 unsigned mod = [ev modifierFlags];
330 [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_RELEASE :
333 - (void)otherMouseDown:(NSEvent *)ev
335 [self mouseEvent:ev button:MIDDLE_BUTTON];
337 - (void)otherMouseDragged:(NSEvent *)ev
339 [self mouseEvent:ev button:MIDDLE_DRAG];
341 - (void)otherMouseUp:(NSEvent *)ev
343 [self mouseEvent:ev button:MIDDLE_RELEASE];
347 @implementation GameWindow
348 - (void)setupContentView
354 frame = [status frame];
355 frame.origin.y = frame.size.height;
360 midend_size(me, &w, &h);
361 frame.size.width = w;
362 frame.size.height = h;
364 fe.image = [[NSImage alloc] initWithSize:frame.size];
365 [fe.image setFlipped:YES];
366 fe.view = [[MyImageView alloc] initWithFrame:frame];
367 [fe.view setImage:fe.image];
368 [fe.view setWindow:self];
372 [[self contentView] addSubview:fe.view];
374 - (id)initWithGame:(const game *)g
376 NSRect rect = { {0,0}, {0,0} }, rect2;
383 me = midend_new(&fe, ourgame);
385 * If we ever need to open a fresh window using a provided game
386 * ID, I think the right thing is to move most of this method
387 * into a new initWithGame:gameID: method, and have
388 * initWithGame: simply call that one and pass it NULL.
391 midend_size(me, &w, &h);
393 rect.size.height = h;
396 * Create the status bar, which will just be an NSTextField.
398 if (ourgame->wants_statusbar()) {
399 status = [[NSTextField alloc] initWithFrame:NSMakeRect(0,0,100,50)];
400 [status setEditable:NO];
401 [status setSelectable:NO];
402 [status setBordered:YES];
403 [status setBezeled:YES];
404 [status setBezelStyle:NSTextFieldSquareBezel];
405 [status setDrawsBackground:YES];
406 [[status cell] setTitle:@""];
408 rect2 = [status frame];
409 rect.size.height += rect2.size.height;
410 rect2.size.width = rect.size.width;
411 rect2.origin.x = rect2.origin.y = 0;
412 [status setFrame:rect2];
416 self = [super initWithContentRect:rect
417 styleMask:(NSTitledWindowMask | NSMiniaturizableWindowMask |
418 NSClosableWindowMask)
419 backing:NSBackingStoreBuffered
421 [self setTitle:[NSString stringWithCString:ourgame->name]];
427 colours = midend_colours(me, &ncolours);
428 fe.ncolours = ncolours;
429 fe.colours = snewn(ncolours, NSColor *);
431 for (i = 0; i < ncolours; i++) {
432 fe.colours[i] = [[NSColor colorWithDeviceRed:colours[i*3]
433 green:colours[i*3+1] blue:colours[i*3+2]
438 [self setupContentView];
440 [[self contentView] addSubview:status];
441 [self setIgnoresMouseEvents:NO];
443 [self center]; /* :-) */
451 for (i = 0; i < fe.ncolours; i++) {
452 [fe.colours[i] release];
456 return [super dealloc];
459 - (void)processButton:(int)b x:(int)x y:(int)y
461 if (!midend_process_key(me, x, y, b))
465 - (void)keyDown:(NSEvent *)ev
467 NSString *s = [ev characters];
468 int i, n = [s length];
470 for (i = 0; i < n; i++) {
471 int c = [s characterAtIndex:i];
474 * ASCII gets passed straight to midend_process_key.
475 * Anything above that has to be translated to our own
476 * function key codes.
480 case NSUpArrowFunctionKey:
483 case NSDownArrowFunctionKey:
486 case NSLeftArrowFunctionKey:
489 case NSRightArrowFunctionKey:
497 [self processButton:c x:-1 y:-1];
501 - (void)activateTimer
506 timer = [NSTimer scheduledTimerWithTimeInterval:0.02
507 target:self selector:@selector(timerTick:)
508 userInfo:nil repeats:YES];
509 gettimeofday(&last_time, NULL);
512 - (void)deactivateTimer
521 - (void)timerTick:(id)sender
525 gettimeofday(&now, NULL);
526 elapsed = ((now.tv_usec - last_time.tv_usec) * 0.000001F +
527 (now.tv_sec - last_time.tv_sec));
528 midend_timer(me, elapsed);
532 - (void)newGame:(id)sender
534 [self processButton:'n' x:-1 y:-1];
536 - (void)restartGame:(id)sender
538 [self processButton:'r' x:-1 y:-1];
540 - (void)undoMove:(id)sender
542 [self processButton:'u' x:-1 y:-1];
544 - (void)redoMove:(id)sender
546 [self processButton:'r'&0x1F x:-1 y:-1];
549 - (void)clearTypeMenu
551 while ([typemenu numberOfItems] > 1)
552 [typemenu removeItemAtIndex:0];
555 - (void)becomeKeyWindow
559 [self clearTypeMenu];
561 [super becomeKeyWindow];
563 n = midend_num_presets(me);
566 [typemenu insertItem:[NSMenuItem separatorItem] atIndex:0];
572 midend_fetch_preset(me, n, &name, ¶ms);
574 item = [[[DataMenuItem alloc]
575 initWithTitle:[NSString stringWithCString:name]
576 action:NULL keyEquivalent:@""]
579 [item setEnabled:YES];
580 [item setTarget:self];
581 [item setAction:@selector(presetGame:)];
582 [item setPayload:params];
584 [typemenu insertItem:item atIndex:0];
589 - (void)resignKeyWindow
591 [self clearTypeMenu];
592 [super resignKeyWindow];
597 [self clearTypeMenu];
601 - (void)resizeForNewGameParams
606 midend_size(me, &w, &h);
610 NSDisableScreenUpdates();
611 [self setContentSize:size];
612 [self setupContentView];
613 NSEnableScreenUpdates();
616 - (void)presetGame:(id)sender
618 game_params *params = [sender getPayload];
620 midend_set_params(me, params);
623 [self resizeForNewGameParams];
626 - (void)startConfigureSheet:(int)which
628 NSButton *ok, *cancel;
629 int actw, acth, leftw, rightw, totalw, h, thish, y;
631 NSRect rect, tmprect;
632 const int SPACING = 16;
640 assert(sheet == NULL);
643 * Every control we create here is going to have this size
644 * until we tell it to calculate a better one.
646 tmprect = NSMakeRect(0, 0, 100, 50);
649 * Set up OK and Cancel buttons. (Actually, MacOS doesn't seem
650 * to be fond of generic OK and Cancel wording, so I'm going to
651 * rename them to something nicer.)
655 cancel = [[NSButton alloc] initWithFrame:tmprect];
656 [cancel setBezelStyle:NSRoundedBezelStyle];
657 [cancel setTitle:@"Abandon"];
658 [cancel setTarget:self];
659 [cancel setKeyEquivalent:@"\033"];
660 [cancel setAction:@selector(sheetCancelButton:)];
662 rect = [cancel frame];
663 if (actw < rect.size.width) actw = rect.size.width;
664 if (acth < rect.size.height) acth = rect.size.height;
666 ok = [[NSButton alloc] initWithFrame:tmprect];
667 [ok setBezelStyle:NSRoundedBezelStyle];
668 [ok setTitle:@"Accept"];
670 [ok setKeyEquivalent:@"\r"];
671 [ok setAction:@selector(sheetOKButton:)];
674 if (actw < rect.size.width) actw = rect.size.width;
675 if (acth < rect.size.height) acth = rect.size.height;
677 totalw = SPACING + 2 * actw;
678 h = 2 * SPACING + acth;
681 * Now fetch the midend config data and go through it creating
684 cfg = midend_get_config(me, which, &title);
685 sfree(title); /* FIXME: should we use this somehow? */
688 cfg_ncontrols = cfg_controlsize = 0;
691 for (i = cfg; i->type != C_END; i++) {
692 if (cfg_controlsize < cfg_ncontrols + 5) {
693 cfg_controlsize = cfg_ncontrols + 32;
694 cfg_controls = sresize(cfg_controls, cfg_controlsize, NSView *);
702 * Two NSTextFields, one being a label and the other
706 tf = [[NSTextField alloc] initWithFrame:tmprect];
708 [tf setSelectable:NO];
710 [tf setDrawsBackground:NO];
711 [[tf cell] setTitle:[NSString stringWithCString:i->name]];
714 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
715 if (leftw < rect.size.width + 1) leftw = rect.size.width + 1;
716 cfg_controls[cfg_ncontrols++] = tf;
718 /* We impose a minimum width on editable NSTextFields to
719 * stop them looking _completely_ silly. */
720 if (rightw < 75) rightw = 75;
722 tf = [[NSTextField alloc] initWithFrame:tmprect];
723 [tf setEditable:YES];
724 [tf setSelectable:YES];
725 [tf setBordered:YES];
726 [[tf cell] setTitle:[NSString stringWithCString:i->sval]];
729 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
730 if (rightw < rect.size.width + 1) rightw = rect.size.width + 1;
731 cfg_controls[cfg_ncontrols++] = tf;
736 * A checkbox is an NSButton with a type of
739 b = [[NSButton alloc] initWithFrame:tmprect];
740 [b setBezelStyle:NSRoundedBezelStyle];
741 [b setButtonType:NSSwitchButton];
742 [b setTitle:[NSString stringWithCString:i->name]];
744 [b setState:(i->ival ? NSOnState : NSOffState)];
746 if (totalw < rect.size.width + 1) totalw = rect.size.width + 1;
747 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
748 cfg_controls[cfg_ncontrols++] = b;
753 * A pop-up menu control is an NSPopUpButton, which
754 * takes an embedded NSMenu. We also need an
755 * NSTextField to act as a label.
758 tf = [[NSTextField alloc] initWithFrame:tmprect];
760 [tf setSelectable:NO];
762 [tf setDrawsBackground:NO];
763 [[tf cell] setTitle:[NSString stringWithCString:i->name]];
766 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
767 if (leftw < rect.size.width + 1) leftw = rect.size.width + 1;
768 cfg_controls[cfg_ncontrols++] = tf;
770 pb = [[NSPopUpButton alloc] initWithFrame:tmprect pullsDown:NO];
771 [pb setBezelStyle:NSRoundedBezelStyle];
781 while (*p && *p != c) p++;
783 [pb addItemWithTitle:[NSString stringWithCString:q
789 [pb selectItemAtIndex:i->ival];
793 if (rightw < rect.size.width + 1) rightw = rect.size.width + 1;
794 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
795 cfg_controls[cfg_ncontrols++] = pb;
799 h += SPACING + thish;
802 if (totalw < leftw + SPACING + rightw)
803 totalw = leftw + SPACING + rightw;
804 if (totalw > leftw + SPACING + rightw) {
805 int excess = totalw - (leftw + SPACING + rightw);
806 int leftexcess = leftw * excess / (leftw + rightw);
807 int rightexcess = excess - leftexcess;
809 rightw += rightexcess;
813 * Now go through the list again, setting the final position
818 for (i = cfg; i->type != C_END; i++) {
825 * These two are treated identically, since both expect
826 * a control on the left and another on the right.
828 rect = [cfg_controls[k] frame];
829 if (thish < rect.size.height + 1)
830 thish = rect.size.height + 1;
831 rect = [cfg_controls[k+1] frame];
832 if (thish < rect.size.height + 1)
833 thish = rect.size.height + 1;
834 rect = [cfg_controls[k] frame];
835 rect.origin.y = y - thish/2 - rect.size.height/2;
836 rect.origin.x = SPACING;
837 rect.size.width = leftw;
838 [cfg_controls[k] setFrame:rect];
839 rect = [cfg_controls[k+1] frame];
840 rect.origin.y = y - thish/2 - rect.size.height/2;
841 rect.origin.x = 2 * SPACING + leftw;
842 rect.size.width = rightw;
843 [cfg_controls[k+1] setFrame:rect];
848 rect = [cfg_controls[k] frame];
849 if (thish < rect.size.height + 1)
850 thish = rect.size.height + 1;
851 rect.origin.y = y - thish/2 - rect.size.height/2;
852 rect.origin.x = SPACING;
853 rect.size.width = totalw;
854 [cfg_controls[k] setFrame:rect];
861 assert(k == cfg_ncontrols);
863 [cancel setFrame:NSMakeRect(SPACING+totalw/4-actw/2, SPACING, actw, acth)];
864 [ok setFrame:NSMakeRect(SPACING+3*totalw/4-actw/2, SPACING, actw, acth)];
866 sheet = [[NSWindow alloc]
867 initWithContentRect:NSMakeRect(0,0,totalw + 2*SPACING,h)
868 styleMask:NSTitledWindowMask | NSClosableWindowMask
869 backing:NSBackingStoreBuffered
872 [[sheet contentView] addSubview:cancel];
873 [[sheet contentView] addSubview:ok];
875 for (k = 0; k < cfg_ncontrols; k++)
876 [[sheet contentView] addSubview:cfg_controls[k]];
878 [NSApp beginSheet:sheet modalForWindow:self
879 modalDelegate:nil didEndSelector:nil contextInfo:nil];
882 - (void)specificGame:(id)sender
884 [self startConfigureSheet:CFG_SEED];
887 - (void)customGameType:(id)sender
889 [self startConfigureSheet:CFG_SETTINGS];
892 - (void)sheetEndWithStatus:(BOOL)update
894 assert(sheet != NULL);
895 [NSApp endSheet:sheet];
896 [sheet orderOut:self];
904 for (i = cfg; i->type != C_END; i++) {
908 i->sval = dupstr([[[(id)cfg_controls[k+1] cell]
913 i->ival = [(id)cfg_controls[k] state] == NSOnState;
917 i->ival = [(id)cfg_controls[k+1] indexOfSelectedItem];
923 error = midend_set_config(me, cfg_which, cfg);
925 NSAlert *alert = [[[NSAlert alloc] init] autorelease];
926 [alert addButtonWithTitle:@"OK"];
927 [alert setInformativeText:[NSString stringWithCString:error]];
928 [alert beginSheetModalForWindow:self modalDelegate:nil
929 didEndSelector:nil contextInfo:nil];
932 [self resizeForNewGameParams];
938 - (void)sheetOKButton:(id)sender
940 [self sheetEndWithStatus:YES];
942 - (void)sheetCancelButton:(id)sender
944 [self sheetEndWithStatus:NO];
947 - (void)setStatusLine:(NSString *)text
949 [[status cell] setTitle:text];
955 * Drawing routines called by the midend.
957 void draw_polygon(frontend *fe, int *coords, int npoints,
958 int fill, int colour)
960 NSBezierPath *path = [NSBezierPath bezierPath];
963 [[NSGraphicsContext currentContext] setShouldAntialias:YES];
965 assert(colour >= 0 && colour < fe->ncolours);
966 [fe->colours[colour] set];
968 for (i = 0; i < npoints; i++) {
969 NSPoint p = { coords[i*2] + 0.5, coords[i*2+1] + 0.5 };
971 [path moveToPoint:p];
973 [path lineToPoint:p];
983 void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour)
985 NSBezierPath *path = [NSBezierPath bezierPath];
986 NSPoint p1 = { x1 + 0.5, y1 + 0.5 }, p2 = { x2 + 0.5, y2 + 0.5 };
988 [[NSGraphicsContext currentContext] setShouldAntialias:NO];
990 assert(colour >= 0 && colour < fe->ncolours);
991 [fe->colours[colour] set];
993 [path moveToPoint:p1];
994 [path lineToPoint:p2];
997 void draw_rect(frontend *fe, int x, int y, int w, int h, int colour)
999 NSRect r = { {x,y}, {w,h} };
1001 [[NSGraphicsContext currentContext] setShouldAntialias:NO];
1003 assert(colour >= 0 && colour < fe->ncolours);
1004 [fe->colours[colour] set];
1008 void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize,
1009 int align, int colour, char *text)
1011 NSString *string = [NSString stringWithCString:text];
1017 [[NSGraphicsContext currentContext] setShouldAntialias:YES];
1019 assert(colour >= 0 && colour < fe->ncolours);
1021 if (fonttype == FONT_FIXED)
1022 font = [NSFont userFixedPitchFontOfSize:fontsize];
1024 font = [NSFont userFontOfSize:fontsize];
1026 attr = [NSDictionary dictionaryWithObjectsAndKeys:
1027 fe->colours[colour], NSForegroundColorAttributeName,
1028 font, NSFontAttributeName, nil];
1033 size = [string sizeWithAttributes:attr];
1034 if (align & ALIGN_HRIGHT)
1035 point.x -= size.width;
1036 else if (align & ALIGN_HCENTRE)
1037 point.x -= size.width / 2;
1038 if (align & ALIGN_VCENTRE)
1039 point.y -= size.height / 2;
1041 [string drawAtPoint:point withAttributes:attr];
1043 void draw_update(frontend *fe, int x, int y, int w, int h)
1047 void clip(frontend *fe, int x, int y, int w, int h)
1049 NSRect r = { {x,y}, {w,h} };
1052 [[NSGraphicsContext currentContext] saveGraphicsState];
1053 [NSBezierPath clipRect:r];
1056 void unclip(frontend *fe)
1059 [[NSGraphicsContext currentContext] restoreGraphicsState];
1060 fe->clipped = FALSE;
1062 void start_draw(frontend *fe)
1064 [fe->image lockFocus];
1065 fe->clipped = FALSE;
1067 void end_draw(frontend *fe)
1069 [fe->image unlockFocus];
1070 [fe->view setNeedsDisplay];
1073 void deactivate_timer(frontend *fe)
1075 [fe->window deactivateTimer];
1077 void activate_timer(frontend *fe)
1079 [fe->window activateTimer];
1082 void status_bar(frontend *fe, char *text)
1084 [fe->window setStatusLine:[NSString stringWithCString:text]];
1087 /* ----------------------------------------------------------------------
1088 * AppController: the object which receives the messages from all
1089 * menu selections that aren't standard OS X functions.
1091 @interface AppController : NSObject
1094 - (IBAction)newGame:(id)sender;
1097 @implementation AppController
1099 - (IBAction)newGame:(id)sender
1101 const game *g = [sender getPayload];
1104 win = [[GameWindow alloc] initWithGame:g];
1105 [win makeKeyAndOrderFront:self];
1110 /* ----------------------------------------------------------------------
1111 * Main program. Constructs the menus and runs the application.
1113 int main(int argc, char **argv)
1115 NSAutoreleasePool *pool;
1118 AppController *controller;
1121 pool = [[NSAutoreleasePool alloc] init];
1123 icon = [NSImage imageNamed:@"NSApplicationIcon"];
1124 [NSApplication sharedApplication];
1125 [NSApp setApplicationIconImage:icon];
1127 controller = [[[AppController alloc] init] autorelease];
1129 [NSApp setMainMenu: newmenu("Main Menu")];
1131 menu = newsubmenu([NSApp mainMenu], "Apple Menu");
1132 [NSApp setServicesMenu:newsubmenu(menu, "Services")];
1133 [menu addItem:[NSMenuItem separatorItem]];
1134 item = newitem(menu, "Hide Puzzles", "h", NSApp, @selector(hide:));
1135 item = newitem(menu, "Hide Others", "o-h", NSApp, @selector(hideOtherApplications:));
1136 item = newitem(menu, "Show All", "", NSApp, @selector(unhideAllApplications:));
1137 [menu addItem:[NSMenuItem separatorItem]];
1138 item = newitem(menu, "Quit", "q", NSApp, @selector(terminate:));
1139 [NSApp setAppleMenu: menu];
1141 menu = newsubmenu([NSApp mainMenu], "Open");
1145 for (i = 0; i < gamecount; i++) {
1147 initnewitem([DataMenuItem allocWithZone:[NSMenu menuZone]],
1148 menu, gamelist[i]->name, "", controller,
1149 @selector(newGame:));
1150 [item setPayload:(void *)gamelist[i]];
1154 menu = newsubmenu([NSApp mainMenu], "Game");
1155 item = newitem(menu, "New", "n", NULL, @selector(newGame:));
1156 item = newitem(menu, "Restart", "r", NULL, @selector(restartGame:));
1157 item = newitem(menu, "Specific", "", NULL, @selector(specificGame:));
1158 [menu addItem:[NSMenuItem separatorItem]];
1159 item = newitem(menu, "Undo", "z", NULL, @selector(undoMove:));
1160 item = newitem(menu, "Redo", "S-z", NULL, @selector(redoMove:));
1161 [menu addItem:[NSMenuItem separatorItem]];
1162 item = newitem(menu, "Close", "w", NULL, @selector(performClose:));
1164 menu = newsubmenu([NSApp mainMenu], "Type");
1166 item = newitem(menu, "Custom", "", NULL, @selector(customGameType:));
1168 menu = newsubmenu([NSApp mainMenu], "Window");
1169 [NSApp setWindowsMenu: menu];
1170 item = newitem(menu, "Minimise Window", "m", NULL, @selector(performMiniaturize:));