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 * - Why are the right and bottom edges of the Pattern grid one
12 * pixel thinner than they should be?
14 * Mac interface issues that possibly could be done better:
16 * - is there a better approach to frontend_default_colour?
18 * - do we need any more options in the Window menu?
20 * - can / should we be doing anything with the titles of the
21 * configuration boxes?
23 * - not sure what I should be doing about default window
24 * placement. Centring new windows is a bit feeble, but what's
25 * better? Is there a standard way to tell the OS "here's the
26 * _size_ of window I want, now use your best judgment about the
29 * - a brief frob of the Mac numeric keypad suggests that it
30 * generates numbers no matter what you do. I wonder if I should
31 * try to figure out a way of detecting keypad codes so I can
32 * implement UP_LEFT and friends. Alternatively, perhaps I
33 * should simply assign the number keys to UP_LEFT et al?
34 * They're not in use for anything else right now.
36 * - see if we can do anything to one-button-ise the multi-button
37 * dependent puzzle UIs:
38 * - Pattern is a _little_ unwieldy but not too bad (since
39 * generally you never need the middle button unless you've
40 * made a mistake, so it's just click versus command-click).
41 * - Net is utterly vile; having normal click be one rotate and
42 * command-click be the other introduces a horrid asymmetry,
43 * and yet requiring a shift key for _each_ click would be
44 * even worse because rotation feels as if it ought to be the
45 * default action. I fear this is why the Flash Net had the
48 * - Should we _return_ to a game configuration sheet once an
49 * error is reported by midend_set_config, to allow the user to
50 * correct the one faulty input and keep the other five OK ones?
51 * The Apple `one sheet at a time' restriction would require me
52 * to do this by closing the config sheet, opening the alert
53 * sheet, and then reopening the config sheet when the alert is
54 * closed; and the human interface types, who presumably
55 * invented the one-sheet-at-a-time rule for good reasons, might
56 * look with disfavour on me trying to get round them to fake a
57 * nested sheet. On the other hand I think there are good
58 * practical reasons for wanting it that way. Uncertain.
60 * Grotty implementation details that could probably be improved:
62 * - I am _utterly_ unconvinced that NSImageView was the right way
63 * to go about having a window with a reliable backing store! It
64 * just doesn't feel right; NSImageView is a _control_. Is there
67 * - Resizing is currently very bad; rather than bother to work
68 * out how to resize the NSImageView, I just splatter and
74 #import <Cocoa/Cocoa.h>
77 /* ----------------------------------------------------------------------
82 * The `Type' menu. We frob this dynamically to allow the user to
83 * choose a preset set of settings from the current game.
87 /* ----------------------------------------------------------------------
88 * Miscellaneous support routines that aren't part of any object or
89 * clearly defined subsystem.
92 void fatal(char *fmt, ...)
99 vsnprintf(errorbuf, lenof(errorbuf), fmt, ap);
102 alert = [NSAlert alloc];
104 * We may have come here because we ran out of memory, in which
105 * case it's entirely likely that that alloc will fail, so we
106 * should have a fallback of some sort.
109 fprintf(stderr, "fatal error (and NSAlert failed): %s\n", errorbuf);
111 alert = [[alert init] autorelease];
112 [alert addButtonWithTitle:@"Oh dear"];
113 [alert setInformativeText:[NSString stringWithCString:errorbuf]];
119 void frontend_default_colour(frontend *fe, float *output)
121 /* FIXME: Is there a system default we can tap into for this? */
122 output[0] = output[1] = output[2] = 0.8F;
125 void get_random_seed(void **randseed, int *randseedsize)
127 time_t *tp = snew(time_t);
129 *randseed = (void *)tp;
130 *randseedsize = sizeof(time_t);
133 /* ----------------------------------------------------------------------
134 * Tiny extension to NSMenuItem which carries a payload of a `void
135 * *', allowing several menu items to invoke the same message but
136 * pass different data through it.
138 @interface DataMenuItem : NSMenuItem
142 - (void)setPayload:(void *)d;
143 - (void *)getPayload;
145 @implementation DataMenuItem
146 - (void)setPayload:(void *)d
156 /* ----------------------------------------------------------------------
157 * Utility routines for constructing OS X menus.
160 NSMenu *newmenu(const char *title)
162 return [[[NSMenu allocWithZone:[NSMenu menuZone]]
163 initWithTitle:[NSString stringWithCString:title]]
167 NSMenu *newsubmenu(NSMenu *parent, const char *title)
172 item = [[[NSMenuItem allocWithZone:[NSMenu menuZone]]
173 initWithTitle:[NSString stringWithCString:title]
177 child = newmenu(title);
178 [item setEnabled:YES];
179 [item setSubmenu:child];
180 [parent addItem:item];
184 id initnewitem(NSMenuItem *item, NSMenu *parent, const char *title,
185 const char *key, id target, SEL action)
187 unsigned mask = NSCommandKeyMask;
189 if (key[strcspn(key, "-")]) {
190 while (*key && *key != '-') {
191 int c = tolower((unsigned char)*key);
193 mask |= NSShiftKeyMask;
194 } else if (c == 'o' || c == 'a') {
195 mask |= NSAlternateKeyMask;
203 item = [[item initWithTitle:[NSString stringWithCString:title]
205 keyEquivalent:[NSString stringWithCString:key]]
209 [item setKeyEquivalentModifierMask: mask];
211 [item setEnabled:YES];
212 [item setTarget:target];
213 [item setAction:action];
215 [parent addItem:item];
220 NSMenuItem *newitem(NSMenu *parent, char *title, char *key,
221 id target, SEL action)
223 return initnewitem([NSMenuItem allocWithZone:[NSMenu menuZone]],
224 parent, title, key, target, action);
227 /* ----------------------------------------------------------------------
228 * The front end presented to midend.c.
230 * This is mostly a subclass of NSWindow. The actual `frontend'
231 * structure passed to the midend contains a variety of pointers,
232 * including that window object but also including the image we
233 * draw on, an ImageView to display it in the window, and so on.
248 @interface MyImageView : NSImageView
252 - (void)setWindow:(GameWindow *)win;
254 - (void)mouseEvent:(NSEvent *)ev button:(int)b;
255 - (void)mouseDown:(NSEvent *)ev;
256 - (void)mouseDragged:(NSEvent *)ev;
257 - (void)mouseUp:(NSEvent *)ev;
258 - (void)rightMouseDown:(NSEvent *)ev;
259 - (void)rightMouseDragged:(NSEvent *)ev;
260 - (void)rightMouseUp:(NSEvent *)ev;
261 - (void)otherMouseDown:(NSEvent *)ev;
262 - (void)otherMouseDragged:(NSEvent *)ev;
263 - (void)otherMouseUp:(NSEvent *)ev;
266 @interface GameWindow : NSWindow
271 struct timeval last_time;
276 NSView **cfg_controls;
280 - (id)initWithGame:(const game *)g;
282 - (void)processButton:(int)b x:(int)x y:(int)y;
283 - (void)keyDown:(NSEvent *)ev;
284 - (void)activateTimer;
285 - (void)deactivateTimer;
286 - (void)setStatusLine:(NSString *)text;
289 @implementation MyImageView
291 - (void)setWindow:(GameWindow *)win
301 - (void)mouseEvent:(NSEvent *)ev button:(int)b
303 NSPoint point = [self convertPoint:[ev locationInWindow] fromView:nil];
304 [ourwin processButton:b x:point.x y:point.y];
307 - (void)mouseDown:(NSEvent *)ev
309 unsigned mod = [ev modifierFlags];
310 [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_BUTTON :
311 (mod & NSShiftKeyMask) ? MIDDLE_BUTTON :
314 - (void)mouseDragged:(NSEvent *)ev
316 unsigned mod = [ev modifierFlags];
317 [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_DRAG :
318 (mod & NSShiftKeyMask) ? MIDDLE_DRAG :
321 - (void)mouseUp:(NSEvent *)ev
323 unsigned mod = [ev modifierFlags];
324 [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_RELEASE :
325 (mod & NSShiftKeyMask) ? MIDDLE_RELEASE :
328 - (void)rightMouseDown:(NSEvent *)ev
330 unsigned mod = [ev modifierFlags];
331 [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_BUTTON :
334 - (void)rightMouseDragged:(NSEvent *)ev
336 unsigned mod = [ev modifierFlags];
337 [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_DRAG :
340 - (void)rightMouseUp:(NSEvent *)ev
342 unsigned mod = [ev modifierFlags];
343 [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_RELEASE :
346 - (void)otherMouseDown:(NSEvent *)ev
348 [self mouseEvent:ev button:MIDDLE_BUTTON];
350 - (void)otherMouseDragged:(NSEvent *)ev
352 [self mouseEvent:ev button:MIDDLE_DRAG];
354 - (void)otherMouseUp:(NSEvent *)ev
356 [self mouseEvent:ev button:MIDDLE_RELEASE];
360 @implementation GameWindow
361 - (void)setupContentView
367 frame = [status frame];
368 frame.origin.y = frame.size.height;
373 midend_size(me, &w, &h);
374 frame.size.width = w;
375 frame.size.height = h;
377 fe.image = [[NSImage alloc] initWithSize:frame.size];
378 [fe.image setFlipped:YES];
379 fe.view = [[MyImageView alloc] initWithFrame:frame];
380 [fe.view setImage:fe.image];
381 [fe.view setWindow:self];
385 [[self contentView] addSubview:fe.view];
387 - (id)initWithGame:(const game *)g
389 NSRect rect = { {0,0}, {0,0} }, rect2;
396 me = midend_new(&fe, ourgame);
398 * If we ever need to open a fresh window using a provided game
399 * ID, I think the right thing is to move most of this method
400 * into a new initWithGame:gameID: method, and have
401 * initWithGame: simply call that one and pass it NULL.
404 midend_size(me, &w, &h);
406 rect.size.height = h;
409 * Create the status bar, which will just be an NSTextField.
411 if (ourgame->wants_statusbar()) {
412 status = [[NSTextField alloc] initWithFrame:NSMakeRect(0,0,100,50)];
413 [status setEditable:NO];
414 [status setSelectable:NO];
415 [status setBordered:YES];
416 [status setBezeled:YES];
417 [status setBezelStyle:NSTextFieldSquareBezel];
418 [status setDrawsBackground:YES];
419 [[status cell] setTitle:@""];
421 rect2 = [status frame];
422 rect.size.height += rect2.size.height;
423 rect2.size.width = rect.size.width;
424 rect2.origin.x = rect2.origin.y = 0;
425 [status setFrame:rect2];
429 self = [super initWithContentRect:rect
430 styleMask:(NSTitledWindowMask | NSMiniaturizableWindowMask |
431 NSClosableWindowMask)
432 backing:NSBackingStoreBuffered
434 [self setTitle:[NSString stringWithCString:ourgame->name]];
440 colours = midend_colours(me, &ncolours);
441 fe.ncolours = ncolours;
442 fe.colours = snewn(ncolours, NSColor *);
444 for (i = 0; i < ncolours; i++) {
445 fe.colours[i] = [[NSColor colorWithDeviceRed:colours[i*3]
446 green:colours[i*3+1] blue:colours[i*3+2]
451 [self setupContentView];
453 [[self contentView] addSubview:status];
454 [self setIgnoresMouseEvents:NO];
456 [self center]; /* :-) */
464 for (i = 0; i < fe.ncolours; i++) {
465 [fe.colours[i] release];
469 return [super dealloc];
472 - (void)processButton:(int)b x:(int)x y:(int)y
474 if (!midend_process_key(me, x, y, b))
478 - (void)keyDown:(NSEvent *)ev
480 NSString *s = [ev characters];
481 int i, n = [s length];
483 for (i = 0; i < n; i++) {
484 int c = [s characterAtIndex:i];
487 * ASCII gets passed straight to midend_process_key.
488 * Anything above that has to be translated to our own
489 * function key codes.
493 case NSUpArrowFunctionKey:
496 case NSDownArrowFunctionKey:
499 case NSLeftArrowFunctionKey:
502 case NSRightArrowFunctionKey:
510 [self processButton:c x:-1 y:-1];
514 - (void)activateTimer
519 timer = [NSTimer scheduledTimerWithTimeInterval:0.02
520 target:self selector:@selector(timerTick:)
521 userInfo:nil repeats:YES];
522 gettimeofday(&last_time, NULL);
525 - (void)deactivateTimer
534 - (void)timerTick:(id)sender
538 gettimeofday(&now, NULL);
539 elapsed = ((now.tv_usec - last_time.tv_usec) * 0.000001F +
540 (now.tv_sec - last_time.tv_sec));
541 midend_timer(me, elapsed);
545 - (void)newGame:(id)sender
547 [self processButton:'n' x:-1 y:-1];
549 - (void)restartGame:(id)sender
551 [self processButton:'r' x:-1 y:-1];
553 - (void)undoMove:(id)sender
555 [self processButton:'u' x:-1 y:-1];
557 - (void)redoMove:(id)sender
559 [self processButton:'r'&0x1F x:-1 y:-1];
562 - (void)clearTypeMenu
564 while ([typemenu numberOfItems] > 1)
565 [typemenu removeItemAtIndex:0];
568 - (void)becomeKeyWindow
572 [self clearTypeMenu];
574 [super becomeKeyWindow];
576 n = midend_num_presets(me);
579 [typemenu insertItem:[NSMenuItem separatorItem] atIndex:0];
585 midend_fetch_preset(me, n, &name, ¶ms);
587 item = [[[DataMenuItem alloc]
588 initWithTitle:[NSString stringWithCString:name]
589 action:NULL keyEquivalent:@""]
592 [item setEnabled:YES];
593 [item setTarget:self];
594 [item setAction:@selector(presetGame:)];
595 [item setPayload:params];
597 [typemenu insertItem:item atIndex:0];
602 - (void)resignKeyWindow
604 [self clearTypeMenu];
605 [super resignKeyWindow];
610 [self clearTypeMenu];
614 - (void)resizeForNewGameParams
619 midend_size(me, &w, &h);
624 NSRect frame = [status frame];
625 size.height += frame.size.height;
626 frame.size.width = size.width;
627 [status setFrame:frame];
630 NSDisableScreenUpdates();
631 [self setContentSize:size];
632 [self setupContentView];
633 NSEnableScreenUpdates();
636 - (void)presetGame:(id)sender
638 game_params *params = [sender getPayload];
640 midend_set_params(me, params);
643 [self resizeForNewGameParams];
646 - (void)startConfigureSheet:(int)which
648 NSButton *ok, *cancel;
649 int actw, acth, leftw, rightw, totalw, h, thish, y;
651 NSRect rect, tmprect;
652 const int SPACING = 16;
660 assert(sheet == NULL);
663 * Every control we create here is going to have this size
664 * until we tell it to calculate a better one.
666 tmprect = NSMakeRect(0, 0, 100, 50);
669 * Set up OK and Cancel buttons. (Actually, MacOS doesn't seem
670 * to be fond of generic OK and Cancel wording, so I'm going to
671 * rename them to something nicer.)
675 cancel = [[NSButton alloc] initWithFrame:tmprect];
676 [cancel setBezelStyle:NSRoundedBezelStyle];
677 [cancel setTitle:@"Abandon"];
678 [cancel setTarget:self];
679 [cancel setKeyEquivalent:@"\033"];
680 [cancel setAction:@selector(sheetCancelButton:)];
682 rect = [cancel frame];
683 if (actw < rect.size.width) actw = rect.size.width;
684 if (acth < rect.size.height) acth = rect.size.height;
686 ok = [[NSButton alloc] initWithFrame:tmprect];
687 [ok setBezelStyle:NSRoundedBezelStyle];
688 [ok setTitle:@"Accept"];
690 [ok setKeyEquivalent:@"\r"];
691 [ok setAction:@selector(sheetOKButton:)];
694 if (actw < rect.size.width) actw = rect.size.width;
695 if (acth < rect.size.height) acth = rect.size.height;
697 totalw = SPACING + 2 * actw;
698 h = 2 * SPACING + acth;
701 * Now fetch the midend config data and go through it creating
704 cfg = midend_get_config(me, which, &title);
705 sfree(title); /* FIXME: should we use this somehow? */
708 cfg_ncontrols = cfg_controlsize = 0;
711 for (i = cfg; i->type != C_END; i++) {
712 if (cfg_controlsize < cfg_ncontrols + 5) {
713 cfg_controlsize = cfg_ncontrols + 32;
714 cfg_controls = sresize(cfg_controls, cfg_controlsize, NSView *);
722 * Two NSTextFields, one being a label and the other
726 tf = [[NSTextField alloc] initWithFrame:tmprect];
728 [tf setSelectable:NO];
730 [tf setDrawsBackground:NO];
731 [[tf cell] setTitle:[NSString stringWithCString:i->name]];
734 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
735 if (leftw < rect.size.width + 1) leftw = rect.size.width + 1;
736 cfg_controls[cfg_ncontrols++] = tf;
738 /* We impose a minimum width on editable NSTextFields to
739 * stop them looking _completely_ silly. */
740 if (rightw < 75) rightw = 75;
742 tf = [[NSTextField alloc] initWithFrame:tmprect];
743 [tf setEditable:YES];
744 [tf setSelectable:YES];
745 [tf setBordered:YES];
746 [[tf cell] setTitle:[NSString stringWithCString:i->sval]];
749 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
750 if (rightw < rect.size.width + 1) rightw = rect.size.width + 1;
751 cfg_controls[cfg_ncontrols++] = tf;
756 * A checkbox is an NSButton with a type of
759 b = [[NSButton alloc] initWithFrame:tmprect];
760 [b setBezelStyle:NSRoundedBezelStyle];
761 [b setButtonType:NSSwitchButton];
762 [b setTitle:[NSString stringWithCString:i->name]];
764 [b setState:(i->ival ? NSOnState : NSOffState)];
766 if (totalw < rect.size.width + 1) totalw = rect.size.width + 1;
767 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
768 cfg_controls[cfg_ncontrols++] = b;
773 * A pop-up menu control is an NSPopUpButton, which
774 * takes an embedded NSMenu. We also need an
775 * NSTextField to act as a label.
778 tf = [[NSTextField alloc] initWithFrame:tmprect];
780 [tf setSelectable:NO];
782 [tf setDrawsBackground:NO];
783 [[tf cell] setTitle:[NSString stringWithCString:i->name]];
786 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
787 if (leftw < rect.size.width + 1) leftw = rect.size.width + 1;
788 cfg_controls[cfg_ncontrols++] = tf;
790 pb = [[NSPopUpButton alloc] initWithFrame:tmprect pullsDown:NO];
791 [pb setBezelStyle:NSRoundedBezelStyle];
801 while (*p && *p != c) p++;
803 [pb addItemWithTitle:[NSString stringWithCString:q
809 [pb selectItemAtIndex:i->ival];
813 if (rightw < rect.size.width + 1) rightw = rect.size.width + 1;
814 if (thish < rect.size.height + 1) thish = rect.size.height + 1;
815 cfg_controls[cfg_ncontrols++] = pb;
819 h += SPACING + thish;
822 if (totalw < leftw + SPACING + rightw)
823 totalw = leftw + SPACING + rightw;
824 if (totalw > leftw + SPACING + rightw) {
825 int excess = totalw - (leftw + SPACING + rightw);
826 int leftexcess = leftw * excess / (leftw + rightw);
827 int rightexcess = excess - leftexcess;
829 rightw += rightexcess;
833 * Now go through the list again, setting the final position
838 for (i = cfg; i->type != C_END; i++) {
845 * These two are treated identically, since both expect
846 * a control on the left and another on the right.
848 rect = [cfg_controls[k] frame];
849 if (thish < rect.size.height + 1)
850 thish = rect.size.height + 1;
851 rect = [cfg_controls[k+1] frame];
852 if (thish < rect.size.height + 1)
853 thish = rect.size.height + 1;
854 rect = [cfg_controls[k] frame];
855 rect.origin.y = y - thish/2 - rect.size.height/2;
856 rect.origin.x = SPACING;
857 rect.size.width = leftw;
858 [cfg_controls[k] setFrame:rect];
859 rect = [cfg_controls[k+1] frame];
860 rect.origin.y = y - thish/2 - rect.size.height/2;
861 rect.origin.x = 2 * SPACING + leftw;
862 rect.size.width = rightw;
863 [cfg_controls[k+1] setFrame:rect];
868 rect = [cfg_controls[k] frame];
869 if (thish < rect.size.height + 1)
870 thish = rect.size.height + 1;
871 rect.origin.y = y - thish/2 - rect.size.height/2;
872 rect.origin.x = SPACING;
873 rect.size.width = totalw;
874 [cfg_controls[k] setFrame:rect];
881 assert(k == cfg_ncontrols);
883 [cancel setFrame:NSMakeRect(SPACING+totalw/4-actw/2, SPACING, actw, acth)];
884 [ok setFrame:NSMakeRect(SPACING+3*totalw/4-actw/2, SPACING, actw, acth)];
886 sheet = [[NSWindow alloc]
887 initWithContentRect:NSMakeRect(0,0,totalw + 2*SPACING,h)
888 styleMask:NSTitledWindowMask | NSClosableWindowMask
889 backing:NSBackingStoreBuffered
892 [[sheet contentView] addSubview:cancel];
893 [[sheet contentView] addSubview:ok];
895 for (k = 0; k < cfg_ncontrols; k++)
896 [[sheet contentView] addSubview:cfg_controls[k]];
898 [NSApp beginSheet:sheet modalForWindow:self
899 modalDelegate:nil didEndSelector:nil contextInfo:nil];
902 - (void)specificGame:(id)sender
904 [self startConfigureSheet:CFG_SEED];
907 - (void)customGameType:(id)sender
909 [self startConfigureSheet:CFG_SETTINGS];
912 - (void)sheetEndWithStatus:(BOOL)update
914 assert(sheet != NULL);
915 [NSApp endSheet:sheet];
916 [sheet orderOut:self];
924 for (i = cfg; i->type != C_END; i++) {
928 i->sval = dupstr([[[(id)cfg_controls[k+1] cell]
933 i->ival = [(id)cfg_controls[k] state] == NSOnState;
937 i->ival = [(id)cfg_controls[k+1] indexOfSelectedItem];
943 error = midend_set_config(me, cfg_which, cfg);
945 NSAlert *alert = [[[NSAlert alloc] init] autorelease];
946 [alert addButtonWithTitle:@"Bah"];
947 [alert setInformativeText:[NSString stringWithCString:error]];
948 [alert beginSheetModalForWindow:self modalDelegate:nil
949 didEndSelector:nil contextInfo:nil];
952 [self resizeForNewGameParams];
958 - (void)sheetOKButton:(id)sender
960 [self sheetEndWithStatus:YES];
962 - (void)sheetCancelButton:(id)sender
964 [self sheetEndWithStatus:NO];
967 - (void)setStatusLine:(NSString *)text
969 [[status cell] setTitle:text];
975 * Drawing routines called by the midend.
977 void draw_polygon(frontend *fe, int *coords, int npoints,
978 int fill, int colour)
980 NSBezierPath *path = [NSBezierPath bezierPath];
983 [[NSGraphicsContext currentContext] setShouldAntialias:YES];
985 assert(colour >= 0 && colour < fe->ncolours);
986 [fe->colours[colour] set];
988 for (i = 0; i < npoints; i++) {
989 NSPoint p = { coords[i*2] + 0.5, coords[i*2+1] + 0.5 };
991 [path moveToPoint:p];
993 [path lineToPoint:p];
1003 void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour)
1005 NSBezierPath *path = [NSBezierPath bezierPath];
1006 NSPoint p1 = { x1 + 0.5, y1 + 0.5 }, p2 = { x2 + 0.5, y2 + 0.5 };
1008 [[NSGraphicsContext currentContext] setShouldAntialias:NO];
1010 assert(colour >= 0 && colour < fe->ncolours);
1011 [fe->colours[colour] set];
1013 [path moveToPoint:p1];
1014 [path lineToPoint:p2];
1017 void draw_rect(frontend *fe, int x, int y, int w, int h, int colour)
1019 NSRect r = { {x,y}, {w,h} };
1021 [[NSGraphicsContext currentContext] setShouldAntialias:NO];
1023 assert(colour >= 0 && colour < fe->ncolours);
1024 [fe->colours[colour] set];
1028 void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize,
1029 int align, int colour, char *text)
1031 NSString *string = [NSString stringWithCString:text];
1037 [[NSGraphicsContext currentContext] setShouldAntialias:YES];
1039 assert(colour >= 0 && colour < fe->ncolours);
1041 if (fonttype == FONT_FIXED)
1042 font = [NSFont userFixedPitchFontOfSize:fontsize];
1044 font = [NSFont userFontOfSize:fontsize];
1046 attr = [NSDictionary dictionaryWithObjectsAndKeys:
1047 fe->colours[colour], NSForegroundColorAttributeName,
1048 font, NSFontAttributeName, nil];
1053 size = [string sizeWithAttributes:attr];
1054 if (align & ALIGN_HRIGHT)
1055 point.x -= size.width;
1056 else if (align & ALIGN_HCENTRE)
1057 point.x -= size.width / 2;
1058 if (align & ALIGN_VCENTRE)
1059 point.y -= size.height / 2;
1061 [string drawAtPoint:point withAttributes:attr];
1063 void draw_update(frontend *fe, int x, int y, int w, int h)
1066 * FIXME: It seems odd that nothing is required here, although
1067 * everything _seems_ to work with this routine empty. Possibly
1068 * we're always updating the entire window, and there's a
1069 * better way which would involve doing something in here?
1072 void clip(frontend *fe, int x, int y, int w, int h)
1074 NSRect r = { {x,y}, {w,h} };
1077 [[NSGraphicsContext currentContext] saveGraphicsState];
1078 [NSBezierPath clipRect:r];
1081 void unclip(frontend *fe)
1084 [[NSGraphicsContext currentContext] restoreGraphicsState];
1085 fe->clipped = FALSE;
1087 void start_draw(frontend *fe)
1089 [fe->image lockFocus];
1090 fe->clipped = FALSE;
1092 void end_draw(frontend *fe)
1094 [fe->image unlockFocus];
1095 [fe->view setNeedsDisplay];
1098 void deactivate_timer(frontend *fe)
1100 [fe->window deactivateTimer];
1102 void activate_timer(frontend *fe)
1104 [fe->window activateTimer];
1107 void status_bar(frontend *fe, char *text)
1109 [fe->window setStatusLine:[NSString stringWithCString:text]];
1112 /* ----------------------------------------------------------------------
1113 * AppController: the object which receives the messages from all
1114 * menu selections that aren't standard OS X functions.
1116 @interface AppController : NSObject
1119 - (IBAction)newGame:(id)sender;
1122 @implementation AppController
1124 - (IBAction)newGame:(id)sender
1126 const game *g = [sender getPayload];
1129 win = [[GameWindow alloc] initWithGame:g];
1130 [win makeKeyAndOrderFront:self];
1133 - (NSMenu *)applicationDockMenu:(NSApplication *)sender
1135 NSMenu *menu = newmenu("Dock Menu");
1139 for (i = 0; i < gamecount; i++) {
1141 initnewitem([DataMenuItem allocWithZone:[NSMenu menuZone]],
1142 menu, gamelist[i]->name, "", self,
1143 @selector(newGame:));
1144 [item setPayload:(void *)gamelist[i]];
1152 /* ----------------------------------------------------------------------
1153 * Main program. Constructs the menus and runs the application.
1155 int main(int argc, char **argv)
1157 NSAutoreleasePool *pool;
1160 AppController *controller;
1163 pool = [[NSAutoreleasePool alloc] init];
1165 icon = [NSImage imageNamed:@"NSApplicationIcon"];
1166 [NSApplication sharedApplication];
1167 [NSApp setApplicationIconImage:icon];
1169 controller = [[[AppController alloc] init] autorelease];
1170 [NSApp setDelegate:controller];
1172 [NSApp setMainMenu: newmenu("Main Menu")];
1174 menu = newsubmenu([NSApp mainMenu], "Apple Menu");
1175 [NSApp setServicesMenu:newsubmenu(menu, "Services")];
1176 [menu addItem:[NSMenuItem separatorItem]];
1177 item = newitem(menu, "Hide Puzzles", "h", NSApp, @selector(hide:));
1178 item = newitem(menu, "Hide Others", "o-h", NSApp, @selector(hideOtherApplications:));
1179 item = newitem(menu, "Show All", "", NSApp, @selector(unhideAllApplications:));
1180 [menu addItem:[NSMenuItem separatorItem]];
1181 item = newitem(menu, "Quit", "q", NSApp, @selector(terminate:));
1182 [NSApp setAppleMenu: menu];
1184 menu = newsubmenu([NSApp mainMenu], "Open");
1188 for (i = 0; i < gamecount; i++) {
1190 initnewitem([DataMenuItem allocWithZone:[NSMenu menuZone]],
1191 menu, gamelist[i]->name, "", controller,
1192 @selector(newGame:));
1193 [item setPayload:(void *)gamelist[i]];
1197 menu = newsubmenu([NSApp mainMenu], "Game");
1198 item = newitem(menu, "New", "n", NULL, @selector(newGame:));
1199 item = newitem(menu, "Restart", "r", NULL, @selector(restartGame:));
1200 item = newitem(menu, "Specific", "", NULL, @selector(specificGame:));
1201 [menu addItem:[NSMenuItem separatorItem]];
1202 item = newitem(menu, "Undo", "z", NULL, @selector(undoMove:));
1203 item = newitem(menu, "Redo", "S-z", NULL, @selector(redoMove:));
1204 [menu addItem:[NSMenuItem separatorItem]];
1205 item = newitem(menu, "Close", "w", NULL, @selector(performClose:));
1207 menu = newsubmenu([NSApp mainMenu], "Type");
1209 item = newitem(menu, "Custom", "", NULL, @selector(customGameType:));
1211 menu = newsubmenu([NSApp mainMenu], "Window");
1212 [NSApp setWindowsMenu: menu];
1213 item = newitem(menu, "Minimise Window", "m", NULL, @selector(performMiniaturize:));