2 * Mac OS X / Cocoa front end to puzzles.
4 * Actually unfinished things left to do:
6 * - Find out how to do help, and do some. We have a help file; at
7 * _worst_ this should involve a new Halibut back end, but I
8 * think help is HTML round here anyway so perhaps we can work
9 * with what we already have.
11 * Mac interface issues that possibly could be done better:
13 * - is there a better approach to frontend_default_colour?
15 * - do we need any more options in the Window menu?
17 * - can / should we be doing anything with the titles of the
18 * configuration boxes?
20 * - not sure what I should be doing about default window
21 * placement. Centring new windows is a bit feeble, but what's
22 * better? Is there a standard way to tell the OS "here's the
23 * _size_ of window I want, now use your best judgment about the
26 * - a brief frob of the Mac numeric keypad suggests that it
27 * generates numbers no matter what you do. I wonder if I should
28 * try to figure out a way of detecting keypad codes so I can
29 * implement UP_LEFT and friends. Alternatively, perhaps I
30 * should simply assign the number keys to UP_LEFT et al?
31 * They're not in use for anything else right now.
33 * - see if we can do anything to one-button-ise the multi-button
34 * dependent puzzle UIs:
35 * - Pattern is a _little_ unwieldy but not too bad (since
36 * generally you never need the middle button unless you've
37 * made a mistake, so it's just click versus command-click).
38 * - Net is utterly vile; having normal click be one rotate and
39 * command-click be the other introduces a horrid asymmetry,
40 * and yet requiring a shift key for _each_ click would be
41 * even worse because rotation feels as if it ought to be the
42 * default action. I fear this is why the Flash Net had the
45 * - Should we _return_ to a game configuration sheet once an
46 * error is reported by midend_set_config, to allow the user to
47 * correct the one faulty input and keep the other five OK ones?
48 * The Apple `one sheet at a time' restriction would require me
49 * to do this by closing the config sheet, opening the alert
50 * sheet, and then reopening the config sheet when the alert is
51 * closed; and the human interface types, who presumably
52 * invented the one-sheet-at-a-time rule for good reasons, might
53 * look with disfavour on me trying to get round them to fake a
54 * nested sheet. On the other hand I think there are good
55 * practical reasons for wanting it that way. Uncertain.
57 * Grotty implementation details that could probably be improved:
59 * - I am _utterly_ unconvinced that NSImageView was the right way
60 * to go about having a window with a reliable backing store! It
61 * just doesn't feel right; NSImageView is a _control_. Is there
64 * - Resizing is currently very bad; rather than bother to work
65 * out how to resize the NSImageView, I just splatter and
71 #import <Cocoa/Cocoa.h>
74 /* ----------------------------------------------------------------------
79 * The `Type' menu. We frob this dynamically to allow the user to
80 * choose a preset set of settings from the current game.
84 /* ----------------------------------------------------------------------
85 * Miscellaneous support routines that aren't part of any object or
86 * clearly defined subsystem.
89 void fatal(char *fmt, ...)
96 vsnprintf(errorbuf, lenof(errorbuf), fmt, ap);
99 alert = [NSAlert alloc];
101 * We may have come here because we ran out of memory, in which
102 * case it's entirely likely that that alloc will fail, so we
103 * should have a fallback of some sort.
106 fprintf(stderr, "fatal error (and NSAlert failed): %s\n", errorbuf);
108 alert = [[alert init] autorelease];
109 [alert addButtonWithTitle:@"Oh dear"];
110 [alert setInformativeText:[NSString stringWithCString:errorbuf]];
116 void frontend_default_colour(frontend *fe, float *output)
118 /* FIXME: Is there a system default we can tap into for this? */
119 output[0] = output[1] = output[2] = 0.8F;
122 void get_random_seed(void **randseed, int *randseedsize)
124 time_t *tp = snew(time_t);
126 *randseed = (void *)tp;
127 *randseedsize = sizeof(time_t);
130 /* ----------------------------------------------------------------------
131 * Tiny extension to NSMenuItem which carries a payload of a `void
132 * *', allowing several menu items to invoke the same message but
133 * pass different data through it.
135 @interface DataMenuItem : NSMenuItem
139 - (void)setPayload:(void *)d;
140 - (void *)getPayload;
142 @implementation DataMenuItem
143 - (void)setPayload:(void *)d
153 /* ----------------------------------------------------------------------
154 * Utility routines for constructing OS X menus.
157 NSMenu *newmenu(const char *title)
159 return [[[NSMenu allocWithZone:[NSMenu menuZone]]
160 initWithTitle:[NSString stringWithCString:title]]
164 NSMenu *newsubmenu(NSMenu *parent, const char *title)
169 item = [[[NSMenuItem allocWithZone:[NSMenu menuZone]]
170 initWithTitle:[NSString stringWithCString:title]
174 child = newmenu(title);
175 [item setEnabled:YES];
176 [item setSubmenu:child];
177 [parent addItem:item];
181 id initnewitem(NSMenuItem *item, NSMenu *parent, const char *title,
182 const char *key, id target, SEL action)
184 unsigned mask = NSCommandKeyMask;
186 if (key[strcspn(key, "-")]) {
187 while (*key && *key != '-') {
188 int c = tolower((unsigned char)*key);
190 mask |= NSShiftKeyMask;
191 } else if (c == 'o' || c == 'a') {
192 mask |= NSAlternateKeyMask;
200 item = [[item initWithTitle:[NSString stringWithCString:title]
202 keyEquivalent:[NSString stringWithCString:key]]
206 [item setKeyEquivalentModifierMask: mask];
208 [item setEnabled:YES];
209 [item setTarget:target];
210 [item setAction:action];
212 [parent addItem:item];
217 NSMenuItem *newitem(NSMenu *parent, char *title, char *key,
218 id target, SEL action)
220 return initnewitem([NSMenuItem allocWithZone:[NSMenu menuZone]],
221 parent, title, key, target, action);
224 /* ----------------------------------------------------------------------
225 * The front end presented to midend.c.
227 * This is mostly a subclass of NSWindow. The actual `frontend'
228 * structure passed to the midend contains a variety of pointers,
229 * including that window object but also including the image we
230 * draw on, an ImageView to display it in the window, and so on.
245 @interface MyImageView : NSImageView
249 - (void)setWindow:(GameWindow *)win;
251 - (void)mouseEvent:(NSEvent *)ev button:(int)b;
252 - (void)mouseDown:(NSEvent *)ev;
253 - (void)mouseDragged:(NSEvent *)ev;
254 - (void)mouseUp:(NSEvent *)ev;
255 - (void)rightMouseDown:(NSEvent *)ev;
256 - (void)rightMouseDragged:(NSEvent *)ev;
257 - (void)rightMouseUp:(NSEvent *)ev;
258 - (void)otherMouseDown:(NSEvent *)ev;
259 - (void)otherMouseDragged:(NSEvent *)ev;
260 - (void)otherMouseUp:(NSEvent *)ev;
263 @interface GameWindow : NSWindow
268 struct timeval last_time;
273 NSView **cfg_controls;
277 - (id)initWithGame:(const game *)g;
279 - (void)processButton:(int)b x:(int)x y:(int)y;
280 - (void)keyDown:(NSEvent *)ev;
281 - (void)activateTimer;
282 - (void)deactivateTimer;
283 - (void)setStatusLine:(NSString *)text;
286 @implementation MyImageView
288 - (void)setWindow:(GameWindow *)win
298 - (void)mouseEvent:(NSEvent *)ev button:(int)b
300 NSPoint point = [self convertPoint:[ev locationInWindow] fromView:nil];
301 [ourwin processButton:b x:point.x y:point.y];
304 - (void)mouseDown:(NSEvent *)ev
306 unsigned mod = [ev modifierFlags];
307 [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_BUTTON :
308 (mod & NSShiftKeyMask) ? MIDDLE_BUTTON :
311 - (void)mouseDragged:(NSEvent *)ev
313 unsigned mod = [ev modifierFlags];
314 [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_DRAG :
315 (mod & NSShiftKeyMask) ? MIDDLE_DRAG :
318 - (void)mouseUp:(NSEvent *)ev
320 unsigned mod = [ev modifierFlags];
321 [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_RELEASE :
322 (mod & NSShiftKeyMask) ? MIDDLE_RELEASE :
325 - (void)rightMouseDown:(NSEvent *)ev
327 unsigned mod = [ev modifierFlags];
328 [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_BUTTON :
331 - (void)rightMouseDragged:(NSEvent *)ev
333 unsigned mod = [ev modifierFlags];
334 [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_DRAG :
337 - (void)rightMouseUp:(NSEvent *)ev
339 unsigned mod = [ev modifierFlags];
340 [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_RELEASE :
343 - (void)otherMouseDown:(NSEvent *)ev
345 [self mouseEvent:ev button:MIDDLE_BUTTON];
347 - (void)otherMouseDragged:(NSEvent *)ev
349 [self mouseEvent:ev button:MIDDLE_DRAG];
351 - (void)otherMouseUp:(NSEvent *)ev
353 [self mouseEvent:ev button:MIDDLE_RELEASE];
357 @implementation GameWindow
358 - (void)setupContentView
364 frame = [status frame];
365 frame.origin.y = frame.size.height;
370 midend_size(me, &w, &h);
371 frame.size.width = w;
372 frame.size.height = h;
374 fe.image = [[NSImage alloc] initWithSize:frame.size];
375 [fe.image setFlipped:YES];
376 fe.view = [[MyImageView alloc] initWithFrame:frame];
377 [fe.view setImage:fe.image];
378 [fe.view setWindow:self];
382 [[self contentView] addSubview:fe.view];
384 - (id)initWithGame:(const game *)g
386 NSRect rect = { {0,0}, {0,0} }, rect2;
393 me = midend_new(&fe, ourgame);
395 * If we ever need to open a fresh window using a provided game
396 * ID, I think the right thing is to move most of this method
397 * into a new initWithGame:gameID: method, and have
398 * initWithGame: simply call that one and pass it NULL.
401 midend_size(me, &w, &h);
403 rect.size.height = h;
406 * Create the status bar, which will just be an NSTextField.
408 if (ourgame->wants_statusbar()) {
409 status = [[NSTextField alloc] initWithFrame:NSMakeRect(0,0,100,50)];
410 [status setEditable:NO];
411 [status setSelectable:NO];
412 [status setBordered:YES];
413 [status setBezeled:YES];
414 [status setBezelStyle:NSTextFieldSquareBezel];
415 [status setDrawsBackground:YES];
416 [[status cell] setTitle:@""];
418 rect2 = [status frame];
419 rect.size.height += rect2.size.height;
420 rect2.size.width = rect.size.width;
421 rect2.origin.x = rect2.origin.y = 0;
422 [status setFrame:rect2];
426 self = [super initWithContentRect:rect
427 styleMask:(NSTitledWindowMask | NSMiniaturizableWindowMask |
428 NSClosableWindowMask)
429 backing:NSBackingStoreBuffered
431 [self setTitle:[NSString stringWithCString:ourgame->name]];
437 colours = midend_colours(me, &ncolours);
438 fe.ncolours = ncolours;
439 fe.colours = snewn(ncolours, NSColor *);
441 for (i = 0; i < ncolours; i++) {
442 fe.colours[i] = [[NSColor colorWithDeviceRed:colours[i*3]
443 green:colours[i*3+1] blue:colours[i*3+2]
448 [self setupContentView];
450 [[self contentView] addSubview:status];
451 [self setIgnoresMouseEvents:NO];
453 [self center]; /* :-) */
461 for (i = 0; i < fe.ncolours; i++) {
462 [fe.colours[i] release];
466 return [super dealloc];
469 - (void)processButton:(int)b x:(int)x y:(int)y
471 if (!midend_process_key(me, x, y, b))
475 - (void)keyDown:(NSEvent *)ev
477 NSString *s = [ev characters];
478 int i, n = [s length];
480 for (i = 0; i < n; i++) {
481 int c = [s characterAtIndex:i];
484 * ASCII gets passed straight to midend_process_key.
485 * Anything above that has to be translated to our own
486 * function key codes.
490 case NSUpArrowFunctionKey:
493 case NSDownArrowFunctionKey:
496 case NSLeftArrowFunctionKey:
499 case NSRightArrowFunctionKey:
507 [self processButton:c x:-1 y:-1];
511 - (void)activateTimer
516 timer = [NSTimer scheduledTimerWithTimeInterval:0.02
517 target:self selector:@selector(timerTick:)
518 userInfo:nil repeats:YES];
519 gettimeofday(&last_time, NULL);
522 - (void)deactivateTimer
531 - (void)timerTick:(id)sender
535 gettimeofday(&now, NULL);
536 elapsed = ((now.tv_usec - last_time.tv_usec) * 0.000001F +
537 (now.tv_sec - last_time.tv_sec));
538 midend_timer(me, elapsed);
542 - (void)newGame:(id)sender
544 [self processButton:'n' x:-1 y:-1];
546 - (void)restartGame:(id)sender
548 [self processButton:'r' x:-1 y:-1];
550 - (void)undoMove:(id)sender
552 [self processButton:'u' x:-1 y:-1];
554 - (void)redoMove:(id)sender
556 [self processButton:'r'&0x1F x:-1 y:-1];
559 - (void)clearTypeMenu
561 while ([typemenu numberOfItems] > 1)
562 [typemenu removeItemAtIndex:0];
565 - (void)becomeKeyWindow
569 [self clearTypeMenu];
571 [super becomeKeyWindow];
573 n = midend_num_presets(me);
576 [typemenu insertItem:[NSMenuItem separatorItem] atIndex:0];
582 midend_fetch_preset(me, n, &name, ¶ms);
584 item = [[[DataMenuItem alloc]
585 initWithTitle:[NSString stringWithCString:name]
586 action:NULL keyEquivalent:@""]
589 [item setEnabled:YES];
590 [item setTarget:self];
591 [item setAction:@selector(presetGame:)];
592 [item setPayload:params];
594 [typemenu insertItem:item atIndex:0];
599 - (void)resignKeyWindow
601 [self clearTypeMenu];
602 [super resignKeyWindow];
607 [self clearTypeMenu];
611 - (void)resizeForNewGameParams
616 midend_size(me, &w, &h);
621 NSRect frame = [status frame];
622 size.height += frame.size.height;
623 frame.size.width = size.width;
624 [status setFrame:frame];
627 NSDisableScreenUpdates();
628 [self setContentSize:size];
629 [self setupContentView];
630 NSEnableScreenUpdates();
633 - (void)presetGame:(id)sender
635 game_params *params = [sender getPayload];
637 midend_set_params(me, params);
640 [self resizeForNewGameParams];
643 - (void)startConfigureSheet:(int)which
645 NSButton *ok, *cancel;
646 int actw, acth, leftw, rightw, totalw, h, thish, y;
648 NSRect rect, tmprect;
649 const int SPACING = 16;
657 assert(sheet == NULL);
660 * Every control we create here is going to have this size
661 * until we tell it to calculate a better one.
663 tmprect = NSMakeRect(0, 0, 100, 50);
666 * Set up OK and Cancel buttons. (Actually, MacOS doesn't seem
667 * to be fond of generic OK and Cancel wording, so I'm going to
668 * rename them to something nicer.)
672 cancel = [[NSButton alloc] initWithFrame:tmprect];
673 [cancel setBezelStyle:NSRoundedBezelStyle];
674 [cancel setTitle:@"Abandon"];
675 [cancel setTarget:self];
676 [cancel setKeyEquivalent:@"\033"];
677 [cancel setAction:@selector(sheetCancelButton:)];
679 rect = [cancel frame];
680 if (actw < rect.size.width) actw = rect.size.width;
681 if (acth < rect.size.height) acth = rect.size.height;
683 ok = [[NSButton alloc] initWithFrame:tmprect];
684 [ok setBezelStyle:NSRoundedBezelStyle];
685 [ok setTitle:@"Accept"];
687 [ok setKeyEquivalent:@"\r"];
688 [ok setAction:@selector(sheetOKButton:)];
691 if (actw < rect.size.width) actw = rect.size.width;
692 if (acth < rect.size.height) acth = rect.size.height;
694 totalw = SPACING + 2 * actw;
695 h = 2 * SPACING + acth;
698 * Now fetch the midend config data and go through it creating
701 cfg = midend_get_config(me, which, &title);
702 sfree(title); /* FIXME: should we use this somehow? */
705 cfg_ncontrols = cfg_controlsize = 0;
708 for (i = cfg; i->type != C_END; i++) {
709 if (cfg_controlsize < cfg_ncontrols + 5) {
710 cfg_controlsize = cfg_ncontrols + 32;
711 cfg_controls = sresize(cfg_controls, cfg_controlsize, NSView *);
719 * Two NSTextFields, one being a label and the other
723 tf = [[NSTextField alloc] initWithFrame:tmprect];
725 [tf setSelectable:NO];
727 [tf setDrawsBackground:NO];
728 [[tf cell] setTitle:[NSString stringWithCString:i->name]];
731 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
732 if (leftw < rect.size.width + 1) leftw = rect.size.width + 1;
733 cfg_controls[cfg_ncontrols++] = tf;
735 /* We impose a minimum width on editable NSTextFields to
736 * stop them looking _completely_ silly. */
737 if (rightw < 75) rightw = 75;
739 tf = [[NSTextField alloc] initWithFrame:tmprect];
740 [tf setEditable:YES];
741 [tf setSelectable:YES];
742 [tf setBordered:YES];
743 [[tf cell] setTitle:[NSString stringWithCString:i->sval]];
746 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
747 if (rightw < rect.size.width + 1) rightw = rect.size.width + 1;
748 cfg_controls[cfg_ncontrols++] = tf;
753 * A checkbox is an NSButton with a type of
756 b = [[NSButton alloc] initWithFrame:tmprect];
757 [b setBezelStyle:NSRoundedBezelStyle];
758 [b setButtonType:NSSwitchButton];
759 [b setTitle:[NSString stringWithCString:i->name]];
761 [b setState:(i->ival ? NSOnState : NSOffState)];
763 if (totalw < rect.size.width + 1) totalw = rect.size.width + 1;
764 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
765 cfg_controls[cfg_ncontrols++] = b;
770 * A pop-up menu control is an NSPopUpButton, which
771 * takes an embedded NSMenu. We also need an
772 * NSTextField to act as a label.
775 tf = [[NSTextField alloc] initWithFrame:tmprect];
777 [tf setSelectable:NO];
779 [tf setDrawsBackground:NO];
780 [[tf cell] setTitle:[NSString stringWithCString:i->name]];
783 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
784 if (leftw < rect.size.width + 1) leftw = rect.size.width + 1;
785 cfg_controls[cfg_ncontrols++] = tf;
787 pb = [[NSPopUpButton alloc] initWithFrame:tmprect pullsDown:NO];
788 [pb setBezelStyle:NSRoundedBezelStyle];
798 while (*p && *p != c) p++;
800 [pb addItemWithTitle:[NSString stringWithCString:q
806 [pb selectItemAtIndex:i->ival];
810 if (rightw < rect.size.width + 1) rightw = rect.size.width + 1;
811 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
812 cfg_controls[cfg_ncontrols++] = pb;
816 h += SPACING + thish;
819 if (totalw < leftw + SPACING + rightw)
820 totalw = leftw + SPACING + rightw;
821 if (totalw > leftw + SPACING + rightw) {
822 int excess = totalw - (leftw + SPACING + rightw);
823 int leftexcess = leftw * excess / (leftw + rightw);
824 int rightexcess = excess - leftexcess;
826 rightw += rightexcess;
830 * Now go through the list again, setting the final position
835 for (i = cfg; i->type != C_END; i++) {
842 * These two are treated identically, since both expect
843 * a control on the left and another on the right.
845 rect = [cfg_controls[k] frame];
846 if (thish < rect.size.height + 1)
847 thish = rect.size.height + 1;
848 rect = [cfg_controls[k+1] frame];
849 if (thish < rect.size.height + 1)
850 thish = rect.size.height + 1;
851 rect = [cfg_controls[k] frame];
852 rect.origin.y = y - thish/2 - rect.size.height/2;
853 rect.origin.x = SPACING;
854 rect.size.width = leftw;
855 [cfg_controls[k] setFrame:rect];
856 rect = [cfg_controls[k+1] frame];
857 rect.origin.y = y - thish/2 - rect.size.height/2;
858 rect.origin.x = 2 * SPACING + leftw;
859 rect.size.width = rightw;
860 [cfg_controls[k+1] setFrame:rect];
865 rect = [cfg_controls[k] frame];
866 if (thish < rect.size.height + 1)
867 thish = rect.size.height + 1;
868 rect.origin.y = y - thish/2 - rect.size.height/2;
869 rect.origin.x = SPACING;
870 rect.size.width = totalw;
871 [cfg_controls[k] setFrame:rect];
878 assert(k == cfg_ncontrols);
880 [cancel setFrame:NSMakeRect(SPACING+totalw/4-actw/2, SPACING, actw, acth)];
881 [ok setFrame:NSMakeRect(SPACING+3*totalw/4-actw/2, SPACING, actw, acth)];
883 sheet = [[NSWindow alloc]
884 initWithContentRect:NSMakeRect(0,0,totalw + 2*SPACING,h)
885 styleMask:NSTitledWindowMask | NSClosableWindowMask
886 backing:NSBackingStoreBuffered
889 [[sheet contentView] addSubview:cancel];
890 [[sheet contentView] addSubview:ok];
892 for (k = 0; k < cfg_ncontrols; k++)
893 [[sheet contentView] addSubview:cfg_controls[k]];
895 [NSApp beginSheet:sheet modalForWindow:self
896 modalDelegate:nil didEndSelector:nil contextInfo:nil];
899 - (void)specificGame:(id)sender
901 [self startConfigureSheet:CFG_SEED];
904 - (void)customGameType:(id)sender
906 [self startConfigureSheet:CFG_SETTINGS];
909 - (void)sheetEndWithStatus:(BOOL)update
911 assert(sheet != NULL);
912 [NSApp endSheet:sheet];
913 [sheet orderOut:self];
921 for (i = cfg; i->type != C_END; i++) {
925 i->sval = dupstr([[[(id)cfg_controls[k+1] cell]
930 i->ival = [(id)cfg_controls[k] state] == NSOnState;
934 i->ival = [(id)cfg_controls[k+1] indexOfSelectedItem];
940 error = midend_set_config(me, cfg_which, cfg);
942 NSAlert *alert = [[[NSAlert alloc] init] autorelease];
943 [alert addButtonWithTitle:@"Bah"];
944 [alert setInformativeText:[NSString stringWithCString:error]];
945 [alert beginSheetModalForWindow:self modalDelegate:nil
946 didEndSelector:nil contextInfo:nil];
949 [self resizeForNewGameParams];
955 - (void)sheetOKButton:(id)sender
957 [self sheetEndWithStatus:YES];
959 - (void)sheetCancelButton:(id)sender
961 [self sheetEndWithStatus:NO];
964 - (void)setStatusLine:(NSString *)text
966 [[status cell] setTitle:text];
972 * Drawing routines called by the midend.
974 void draw_polygon(frontend *fe, int *coords, int npoints,
975 int fill, int colour)
977 NSBezierPath *path = [NSBezierPath bezierPath];
980 [[NSGraphicsContext currentContext] setShouldAntialias:YES];
982 assert(colour >= 0 && colour < fe->ncolours);
983 [fe->colours[colour] set];
985 for (i = 0; i < npoints; i++) {
986 NSPoint p = { coords[i*2] + 0.5, coords[i*2+1] + 0.5 };
988 [path moveToPoint:p];
990 [path lineToPoint:p];
1000 void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour)
1002 NSBezierPath *path = [NSBezierPath bezierPath];
1003 NSPoint p1 = { x1 + 0.5, y1 + 0.5 }, p2 = { x2 + 0.5, y2 + 0.5 };
1005 [[NSGraphicsContext currentContext] setShouldAntialias:NO];
1007 assert(colour >= 0 && colour < fe->ncolours);
1008 [fe->colours[colour] set];
1010 [path moveToPoint:p1];
1011 [path lineToPoint:p2];
1014 void draw_rect(frontend *fe, int x, int y, int w, int h, int colour)
1016 NSRect r = { {x,y}, {w,h} };
1018 [[NSGraphicsContext currentContext] setShouldAntialias:NO];
1020 assert(colour >= 0 && colour < fe->ncolours);
1021 [fe->colours[colour] set];
1025 void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize,
1026 int align, int colour, char *text)
1028 NSString *string = [NSString stringWithCString:text];
1034 [[NSGraphicsContext currentContext] setShouldAntialias:YES];
1036 assert(colour >= 0 && colour < fe->ncolours);
1038 if (fonttype == FONT_FIXED)
1039 font = [NSFont userFixedPitchFontOfSize:fontsize];
1041 font = [NSFont userFontOfSize:fontsize];
1043 attr = [NSDictionary dictionaryWithObjectsAndKeys:
1044 fe->colours[colour], NSForegroundColorAttributeName,
1045 font, NSFontAttributeName, nil];
1050 size = [string sizeWithAttributes:attr];
1051 if (align & ALIGN_HRIGHT)
1052 point.x -= size.width;
1053 else if (align & ALIGN_HCENTRE)
1054 point.x -= size.width / 2;
1055 if (align & ALIGN_VCENTRE)
1056 point.y -= size.height / 2;
1058 [string drawAtPoint:point withAttributes:attr];
1060 void draw_update(frontend *fe, int x, int y, int w, int h)
1063 * FIXME: It seems odd that nothing is required here, although
1064 * everything _seems_ to work with this routine empty. Possibly
1065 * we're always updating the entire window, and there's a
1066 * better way which would involve doing something in here?
1069 void clip(frontend *fe, int x, int y, int w, int h)
1071 NSRect r = { {x,y}, {w,h} };
1074 [[NSGraphicsContext currentContext] saveGraphicsState];
1075 [NSBezierPath clipRect:r];
1078 void unclip(frontend *fe)
1081 [[NSGraphicsContext currentContext] restoreGraphicsState];
1082 fe->clipped = FALSE;
1084 void start_draw(frontend *fe)
1086 [fe->image lockFocus];
1087 fe->clipped = FALSE;
1089 void end_draw(frontend *fe)
1091 [fe->image unlockFocus];
1092 [fe->view setNeedsDisplay];
1095 void deactivate_timer(frontend *fe)
1097 [fe->window deactivateTimer];
1099 void activate_timer(frontend *fe)
1101 [fe->window activateTimer];
1104 void status_bar(frontend *fe, char *text)
1106 [fe->window setStatusLine:[NSString stringWithCString:text]];
1109 /* ----------------------------------------------------------------------
1110 * AppController: the object which receives the messages from all
1111 * menu selections that aren't standard OS X functions.
1113 @interface AppController : NSObject
1116 - (IBAction)newGame:(id)sender;
1119 @implementation AppController
1121 - (IBAction)newGame:(id)sender
1123 const game *g = [sender getPayload];
1126 win = [[GameWindow alloc] initWithGame:g];
1127 [win makeKeyAndOrderFront:self];
1130 - (NSMenu *)applicationDockMenu:(NSApplication *)sender
1132 NSMenu *menu = newmenu("Dock Menu");
1136 for (i = 0; i < gamecount; i++) {
1138 initnewitem([DataMenuItem allocWithZone:[NSMenu menuZone]],
1139 menu, gamelist[i]->name, "", self,
1140 @selector(newGame:));
1141 [item setPayload:(void *)gamelist[i]];
1149 /* ----------------------------------------------------------------------
1150 * Main program. Constructs the menus and runs the application.
1152 int main(int argc, char **argv)
1154 NSAutoreleasePool *pool;
1157 AppController *controller;
1160 pool = [[NSAutoreleasePool alloc] init];
1162 icon = [NSImage imageNamed:@"NSApplicationIcon"];
1163 [NSApplication sharedApplication];
1164 [NSApp setApplicationIconImage:icon];
1166 controller = [[[AppController alloc] init] autorelease];
1167 [NSApp setDelegate:controller];
1169 [NSApp setMainMenu: newmenu("Main Menu")];
1171 menu = newsubmenu([NSApp mainMenu], "Apple Menu");
1172 [NSApp setServicesMenu:newsubmenu(menu, "Services")];
1173 [menu addItem:[NSMenuItem separatorItem]];
1174 item = newitem(menu, "Hide Puzzles", "h", NSApp, @selector(hide:));
1175 item = newitem(menu, "Hide Others", "o-h", NSApp, @selector(hideOtherApplications:));
1176 item = newitem(menu, "Show All", "", NSApp, @selector(unhideAllApplications:));
1177 [menu addItem:[NSMenuItem separatorItem]];
1178 item = newitem(menu, "Quit", "q", NSApp, @selector(terminate:));
1179 [NSApp setAppleMenu: menu];
1181 menu = newsubmenu([NSApp mainMenu], "Open");
1185 for (i = 0; i < gamecount; i++) {
1187 initnewitem([DataMenuItem allocWithZone:[NSMenu menuZone]],
1188 menu, gamelist[i]->name, "", controller,
1189 @selector(newGame:));
1190 [item setPayload:(void *)gamelist[i]];
1194 menu = newsubmenu([NSApp mainMenu], "Game");
1195 item = newitem(menu, "New", "n", NULL, @selector(newGame:));
1196 item = newitem(menu, "Restart", "r", NULL, @selector(restartGame:));
1197 item = newitem(menu, "Specific", "", NULL, @selector(specificGame:));
1198 [menu addItem:[NSMenuItem separatorItem]];
1199 item = newitem(menu, "Undo", "z", NULL, @selector(undoMove:));
1200 item = newitem(menu, "Redo", "S-z", NULL, @selector(redoMove:));
1201 [menu addItem:[NSMenuItem separatorItem]];
1202 item = newitem(menu, "Close", "w", NULL, @selector(performClose:));
1204 menu = newsubmenu([NSApp mainMenu], "Type");
1206 item = newitem(menu, "Custom", "", NULL, @selector(customGameType:));
1208 menu = newsubmenu([NSApp mainMenu], "Window");
1209 [NSApp setWindowsMenu: menu];
1210 item = newitem(menu, "Minimise Window", "m", NULL, @selector(performMiniaturize:));