9494d866 |
1 | /* |
2 | * Mac OS X / Cocoa front end to puzzles. |
00461804 |
3 | * |
a5102461 |
4 | * Actually unfinished things left to do: |
00461804 |
5 | * |
a5102461 |
6 | * - proper fatal errors. |
7 | * |
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. |
12 | * |
a5102461 |
13 | * - Why are the right and bottom edges of the Pattern grid one |
14 | * pixel thinner than they should be? |
15 | * |
16 | * Mac interface issues that possibly could be done better: |
17 | * |
18 | * - is there a better approach to frontend_default_colour? |
19 | * |
20 | * - do we need any more options in the Window menu? |
00461804 |
21 | * |
9494d866 |
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 |
26 | * initial position"? |
00461804 |
27 | * |
9494d866 |
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 |
6d634196 |
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. |
00461804 |
34 | * |
00461804 |
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 |
45 | * UI it did... |
46 | * |
e1107f8b |
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. |
58 | * |
7e04cb48 |
59 | * Grotty implementation details that could probably be improved: |
60 | * |
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 |
64 | * a simpler way? |
65 | * |
66 | * - Resizing is currently very bad; rather than bother to work |
67 | * out how to resize the NSImageView, I just splatter and |
68 | * recreate it. |
9494d866 |
69 | */ |
70 | |
71 | #include <ctype.h> |
72 | #include <sys/time.h> |
73 | #import <Cocoa/Cocoa.h> |
74 | #include "puzzles.h" |
75 | |
76 | void fatal(char *fmt, ...) |
77 | { |
78 | /* FIXME: This will do for testing, but should be GUI-ish instead. */ |
79 | va_list ap; |
80 | |
81 | fprintf(stderr, "fatal error: "); |
82 | |
83 | va_start(ap, fmt); |
84 | vfprintf(stderr, fmt, ap); |
85 | va_end(ap); |
86 | |
87 | fprintf(stderr, "\n"); |
88 | exit(1); |
89 | } |
90 | |
91 | void frontend_default_colour(frontend *fe, float *output) |
92 | { |
93 | /* FIXME */ |
94 | output[0] = output[1] = output[2] = 0.8F; |
95 | } |
9494d866 |
96 | |
97 | void get_random_seed(void **randseed, int *randseedsize) |
98 | { |
99 | time_t *tp = snew(time_t); |
100 | time(tp); |
101 | *randseed = (void *)tp; |
102 | *randseedsize = sizeof(time_t); |
103 | } |
104 | |
105 | /* ---------------------------------------------------------------------- |
7e04cb48 |
106 | * Global variables. |
107 | */ |
108 | |
109 | /* |
110 | * The `Type' menu. We frob this dynamically to allow the user to |
111 | * choose a preset set of settings from the current game. |
112 | */ |
113 | NSMenu *typemenu; |
114 | |
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. |
119 | */ |
120 | @interface DataMenuItem : NSMenuItem |
121 | { |
122 | void *payload; |
7e04cb48 |
123 | } |
124 | - (void)setPayload:(void *)d; |
7e04cb48 |
125 | - (void *)getPayload; |
126 | @end |
127 | @implementation DataMenuItem |
7e04cb48 |
128 | - (void)setPayload:(void *)d |
129 | { |
130 | payload = d; |
131 | } |
7e04cb48 |
132 | - (void *)getPayload |
133 | { |
134 | return payload; |
135 | } |
7e04cb48 |
136 | @end |
137 | |
138 | /* ---------------------------------------------------------------------- |
e1107f8b |
139 | * Utility routines for constructing OS X menus. |
140 | */ |
141 | |
142 | NSMenu *newmenu(const char *title) |
143 | { |
144 | return [[[NSMenu allocWithZone:[NSMenu menuZone]] |
145 | initWithTitle:[NSString stringWithCString:title]] |
146 | autorelease]; |
147 | } |
148 | |
149 | NSMenu *newsubmenu(NSMenu *parent, const char *title) |
150 | { |
151 | NSMenuItem *item; |
152 | NSMenu *child; |
153 | |
154 | item = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] |
155 | initWithTitle:[NSString stringWithCString:title] |
156 | action:NULL |
157 | keyEquivalent:@""] |
158 | autorelease]; |
159 | child = newmenu(title); |
160 | [item setEnabled:YES]; |
161 | [item setSubmenu:child]; |
162 | [parent addItem:item]; |
163 | return child; |
164 | } |
165 | |
166 | id initnewitem(NSMenuItem *item, NSMenu *parent, const char *title, |
167 | const char *key, id target, SEL action) |
168 | { |
169 | unsigned mask = NSCommandKeyMask; |
170 | |
171 | if (key[strcspn(key, "-")]) { |
172 | while (*key && *key != '-') { |
173 | int c = tolower((unsigned char)*key); |
174 | if (c == 's') { |
175 | mask |= NSShiftKeyMask; |
176 | } else if (c == 'o' || c == 'a') { |
177 | mask |= NSAlternateKeyMask; |
178 | } |
179 | key++; |
180 | } |
181 | if (*key) |
182 | key++; |
183 | } |
184 | |
185 | item = [[item initWithTitle:[NSString stringWithCString:title] |
186 | action:NULL |
187 | keyEquivalent:[NSString stringWithCString:key]] |
188 | autorelease]; |
189 | |
190 | if (*key) |
191 | [item setKeyEquivalentModifierMask: mask]; |
192 | |
193 | [item setEnabled:YES]; |
194 | [item setTarget:target]; |
195 | [item setAction:action]; |
196 | |
197 | [parent addItem:item]; |
198 | |
199 | return item; |
200 | } |
201 | |
202 | NSMenuItem *newitem(NSMenu *parent, char *title, char *key, |
203 | id target, SEL action) |
204 | { |
205 | return initnewitem([NSMenuItem allocWithZone:[NSMenu menuZone]], |
206 | parent, title, key, target, action); |
207 | } |
208 | |
209 | /* ---------------------------------------------------------------------- |
9494d866 |
210 | * The front end presented to midend.c. |
211 | * |
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. |
216 | */ |
217 | |
218 | @class GameWindow; |
219 | @class MyImageView; |
220 | |
221 | struct frontend { |
222 | GameWindow *window; |
223 | NSImage *image; |
224 | MyImageView *view; |
225 | NSColor **colours; |
226 | int ncolours; |
227 | int clipped; |
228 | }; |
229 | |
230 | @interface MyImageView : NSImageView |
231 | { |
232 | GameWindow *ourwin; |
233 | } |
234 | - (void)setWindow:(GameWindow *)win; |
235 | - (BOOL)isFlipped; |
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; |
246 | @end |
247 | |
248 | @interface GameWindow : NSWindow |
249 | { |
250 | const game *ourgame; |
251 | midend_data *me; |
252 | struct frontend fe; |
253 | struct timeval last_time; |
254 | NSTimer *timer; |
e1107f8b |
255 | NSWindow *sheet; |
256 | config_item *cfg; |
257 | int cfg_which; |
258 | NSView **cfg_controls; |
259 | int cfg_ncontrols; |
a5102461 |
260 | NSTextField *status; |
9494d866 |
261 | } |
262 | - (id)initWithGame:(const game *)g; |
263 | - dealloc; |
264 | - (void)processButton:(int)b x:(int)x y:(int)y; |
265 | - (void)keyDown:(NSEvent *)ev; |
266 | - (void)activateTimer; |
267 | - (void)deactivateTimer; |
a5102461 |
268 | - (void)setStatusLine:(NSString *)text; |
9494d866 |
269 | @end |
270 | |
271 | @implementation MyImageView |
272 | |
273 | - (void)setWindow:(GameWindow *)win |
274 | { |
275 | ourwin = win; |
276 | } |
277 | |
278 | - (BOOL)isFlipped |
279 | { |
280 | return YES; |
281 | } |
282 | |
283 | - (void)mouseEvent:(NSEvent *)ev button:(int)b |
284 | { |
285 | NSPoint point = [self convertPoint:[ev locationInWindow] fromView:nil]; |
286 | [ourwin processButton:b x:point.x y:point.y]; |
287 | } |
288 | |
289 | - (void)mouseDown:(NSEvent *)ev |
290 | { |
291 | unsigned mod = [ev modifierFlags]; |
292 | [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_BUTTON : |
293 | (mod & NSShiftKeyMask) ? MIDDLE_BUTTON : |
294 | LEFT_BUTTON)]; |
295 | } |
296 | - (void)mouseDragged:(NSEvent *)ev |
297 | { |
298 | unsigned mod = [ev modifierFlags]; |
299 | [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_DRAG : |
300 | (mod & NSShiftKeyMask) ? MIDDLE_DRAG : |
301 | LEFT_DRAG)]; |
302 | } |
303 | - (void)mouseUp:(NSEvent *)ev |
304 | { |
305 | unsigned mod = [ev modifierFlags]; |
306 | [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_RELEASE : |
307 | (mod & NSShiftKeyMask) ? MIDDLE_RELEASE : |
308 | LEFT_RELEASE)]; |
309 | } |
310 | - (void)rightMouseDown:(NSEvent *)ev |
311 | { |
312 | unsigned mod = [ev modifierFlags]; |
313 | [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_BUTTON : |
314 | RIGHT_BUTTON)]; |
315 | } |
316 | - (void)rightMouseDragged:(NSEvent *)ev |
317 | { |
318 | unsigned mod = [ev modifierFlags]; |
319 | [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_DRAG : |
320 | RIGHT_DRAG)]; |
321 | } |
322 | - (void)rightMouseUp:(NSEvent *)ev |
323 | { |
324 | unsigned mod = [ev modifierFlags]; |
325 | [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_RELEASE : |
326 | RIGHT_RELEASE)]; |
327 | } |
328 | - (void)otherMouseDown:(NSEvent *)ev |
329 | { |
330 | [self mouseEvent:ev button:MIDDLE_BUTTON]; |
331 | } |
332 | - (void)otherMouseDragged:(NSEvent *)ev |
333 | { |
334 | [self mouseEvent:ev button:MIDDLE_DRAG]; |
335 | } |
336 | - (void)otherMouseUp:(NSEvent *)ev |
337 | { |
338 | [self mouseEvent:ev button:MIDDLE_RELEASE]; |
339 | } |
340 | @end |
341 | |
342 | @implementation GameWindow |
7e04cb48 |
343 | - (void)setupContentView |
344 | { |
a5102461 |
345 | NSRect frame; |
7e04cb48 |
346 | int w, h; |
347 | |
a5102461 |
348 | if (status) { |
349 | frame = [status frame]; |
350 | frame.origin.y = frame.size.height; |
351 | } else |
352 | frame.origin.y = 0; |
353 | frame.origin.x = 0; |
354 | |
7e04cb48 |
355 | midend_size(me, &w, &h); |
a5102461 |
356 | frame.size.width = w; |
357 | frame.size.height = h; |
7e04cb48 |
358 | |
a5102461 |
359 | fe.image = [[NSImage alloc] initWithSize:frame.size]; |
7e04cb48 |
360 | [fe.image setFlipped:YES]; |
a5102461 |
361 | fe.view = [[MyImageView alloc] initWithFrame:frame]; |
7e04cb48 |
362 | [fe.view setImage:fe.image]; |
363 | [fe.view setWindow:self]; |
364 | |
365 | midend_redraw(me); |
366 | |
a5102461 |
367 | [[self contentView] addSubview:fe.view]; |
7e04cb48 |
368 | } |
9494d866 |
369 | - (id)initWithGame:(const game *)g |
370 | { |
a5102461 |
371 | NSRect rect = { {0,0}, {0,0} }, rect2; |
9494d866 |
372 | int w, h; |
373 | |
374 | ourgame = g; |
375 | |
376 | fe.window = self; |
377 | |
378 | me = midend_new(&fe, ourgame); |
379 | /* |
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. |
384 | */ |
385 | midend_new_game(me); |
386 | midend_size(me, &w, &h); |
387 | rect.size.width = w; |
388 | rect.size.height = h; |
389 | |
a5102461 |
390 | /* |
391 | * Create the status bar, which will just be an NSTextField. |
392 | */ |
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:@""]; |
402 | [status sizeToFit]; |
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]; |
408 | } else |
409 | status = nil; |
410 | |
9494d866 |
411 | self = [super initWithContentRect:rect |
412 | styleMask:(NSTitledWindowMask | NSMiniaturizableWindowMask | |
413 | NSClosableWindowMask) |
414 | backing:NSBackingStoreBuffered |
e1107f8b |
415 | defer:YES]; |
9494d866 |
416 | [self setTitle:[NSString stringWithCString:ourgame->name]]; |
417 | |
418 | { |
419 | float *colours; |
420 | int i, ncolours; |
421 | |
422 | colours = midend_colours(me, &ncolours); |
423 | fe.ncolours = ncolours; |
424 | fe.colours = snewn(ncolours, NSColor *); |
425 | |
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] |
429 | alpha:1.0] retain]; |
430 | } |
431 | } |
432 | |
7e04cb48 |
433 | [self setupContentView]; |
a5102461 |
434 | if (status) |
435 | [[self contentView] addSubview:status]; |
9494d866 |
436 | [self setIgnoresMouseEvents:NO]; |
437 | |
9494d866 |
438 | [self center]; /* :-) */ |
439 | |
440 | return self; |
441 | } |
442 | |
443 | - dealloc |
444 | { |
445 | int i; |
446 | for (i = 0; i < fe.ncolours; i++) { |
447 | [fe.colours[i] release]; |
448 | } |
449 | sfree(fe.colours); |
450 | midend_free(me); |
451 | return [super dealloc]; |
452 | } |
453 | |
454 | - (void)processButton:(int)b x:(int)x y:(int)y |
455 | { |
456 | if (!midend_process_key(me, x, y, b)) |
457 | [self close]; |
458 | } |
459 | |
460 | - (void)keyDown:(NSEvent *)ev |
461 | { |
462 | NSString *s = [ev characters]; |
463 | int i, n = [s length]; |
464 | |
465 | for (i = 0; i < n; i++) { |
466 | int c = [s characterAtIndex:i]; |
467 | |
468 | /* |
469 | * ASCII gets passed straight to midend_process_key. |
470 | * Anything above that has to be translated to our own |
471 | * function key codes. |
472 | */ |
473 | if (c >= 0x80) { |
474 | switch (c) { |
475 | case NSUpArrowFunctionKey: |
476 | c = CURSOR_UP; |
477 | break; |
478 | case NSDownArrowFunctionKey: |
479 | c = CURSOR_DOWN; |
480 | break; |
481 | case NSLeftArrowFunctionKey: |
482 | c = CURSOR_LEFT; |
483 | break; |
484 | case NSRightArrowFunctionKey: |
485 | c = CURSOR_RIGHT; |
486 | break; |
487 | default: |
488 | continue; |
489 | } |
490 | } |
491 | |
492 | [self processButton:c x:-1 y:-1]; |
493 | } |
494 | } |
495 | |
496 | - (void)activateTimer |
497 | { |
498 | if (timer != nil) |
499 | return; |
500 | |
501 | timer = [NSTimer scheduledTimerWithTimeInterval:0.02 |
502 | target:self selector:@selector(timerTick:) |
503 | userInfo:nil repeats:YES]; |
504 | gettimeofday(&last_time, NULL); |
505 | } |
506 | |
507 | - (void)deactivateTimer |
508 | { |
509 | if (timer == nil) |
510 | return; |
511 | |
512 | [timer invalidate]; |
513 | timer = nil; |
514 | } |
515 | |
516 | - (void)timerTick:(id)sender |
517 | { |
518 | struct timeval now; |
519 | float elapsed; |
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); |
524 | last_time = now; |
525 | } |
526 | |
00461804 |
527 | - (void)newGame:(id)sender |
528 | { |
529 | [self processButton:'n' x:-1 y:-1]; |
530 | } |
531 | - (void)restartGame:(id)sender |
532 | { |
533 | [self processButton:'r' x:-1 y:-1]; |
534 | } |
535 | - (void)undoMove:(id)sender |
536 | { |
537 | [self processButton:'u' x:-1 y:-1]; |
538 | } |
539 | - (void)redoMove:(id)sender |
540 | { |
541 | [self processButton:'r'&0x1F x:-1 y:-1]; |
542 | } |
543 | |
7e04cb48 |
544 | - (void)clearTypeMenu |
545 | { |
546 | while ([typemenu numberOfItems] > 1) |
547 | [typemenu removeItemAtIndex:0]; |
548 | } |
549 | |
550 | - (void)becomeKeyWindow |
551 | { |
552 | int n; |
553 | |
554 | [self clearTypeMenu]; |
555 | |
556 | [super becomeKeyWindow]; |
557 | |
558 | n = midend_num_presets(me); |
559 | |
560 | if (n > 0) { |
561 | [typemenu insertItem:[NSMenuItem separatorItem] atIndex:0]; |
562 | while (n--) { |
563 | char *name; |
564 | game_params *params; |
565 | DataMenuItem *item; |
566 | |
567 | midend_fetch_preset(me, n, &name, ¶ms); |
568 | |
569 | item = [[[DataMenuItem alloc] |
570 | initWithTitle:[NSString stringWithCString:name] |
571 | action:NULL keyEquivalent:@""] |
572 | autorelease]; |
573 | |
574 | [item setEnabled:YES]; |
575 | [item setTarget:self]; |
576 | [item setAction:@selector(presetGame:)]; |
577 | [item setPayload:params]; |
7e04cb48 |
578 | |
579 | [typemenu insertItem:item atIndex:0]; |
580 | } |
581 | } |
582 | } |
583 | |
584 | - (void)resignKeyWindow |
585 | { |
586 | [self clearTypeMenu]; |
587 | [super resignKeyWindow]; |
588 | } |
589 | |
590 | - (void)close |
591 | { |
592 | [self clearTypeMenu]; |
593 | [super close]; |
594 | } |
595 | |
596 | - (void)resizeForNewGameParams |
597 | { |
598 | NSSize size = {0,0}; |
599 | int w, h; |
600 | |
601 | midend_size(me, &w, &h); |
602 | size.width = w; |
603 | size.height = h; |
604 | |
605 | NSDisableScreenUpdates(); |
606 | [self setContentSize:size]; |
607 | [self setupContentView]; |
608 | NSEnableScreenUpdates(); |
609 | } |
610 | |
611 | - (void)presetGame:(id)sender |
612 | { |
613 | game_params *params = [sender getPayload]; |
614 | |
615 | midend_set_params(me, params); |
616 | midend_new_game(me); |
617 | |
618 | [self resizeForNewGameParams]; |
619 | } |
620 | |
e1107f8b |
621 | - (void)startConfigureSheet:(int)which |
622 | { |
623 | NSButton *ok, *cancel; |
624 | int actw, acth, leftw, rightw, totalw, h, thish, y; |
625 | int k; |
626 | NSRect rect, tmprect; |
627 | const int SPACING = 16; |
628 | char *title; |
629 | config_item *i; |
630 | int cfg_controlsize; |
631 | NSTextField *tf; |
632 | NSButton *b; |
633 | NSPopUpButton *pb; |
634 | |
635 | assert(sheet == NULL); |
636 | |
637 | /* |
638 | * Every control we create here is going to have this size |
639 | * until we tell it to calculate a better one. |
640 | */ |
641 | tmprect = NSMakeRect(0, 0, 100, 50); |
642 | |
643 | /* |
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.) |
647 | */ |
648 | actw = acth = 0; |
649 | |
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:)]; |
656 | [cancel sizeToFit]; |
657 | rect = [cancel frame]; |
658 | if (actw < rect.size.width) actw = rect.size.width; |
659 | if (acth < rect.size.height) acth = rect.size.height; |
660 | |
661 | ok = [[NSButton alloc] initWithFrame:tmprect]; |
662 | [ok setBezelStyle:NSRoundedBezelStyle]; |
663 | [ok setTitle:@"Accept"]; |
664 | [ok setTarget:self]; |
665 | [ok setKeyEquivalent:@"\r"]; |
666 | [ok setAction:@selector(sheetOKButton:)]; |
667 | [ok sizeToFit]; |
668 | rect = [ok frame]; |
669 | if (actw < rect.size.width) actw = rect.size.width; |
670 | if (acth < rect.size.height) acth = rect.size.height; |
671 | |
672 | totalw = SPACING + 2 * actw; |
673 | h = 2 * SPACING + acth; |
674 | |
675 | /* |
676 | * Now fetch the midend config data and go through it creating |
677 | * controls. |
678 | */ |
679 | cfg = midend_get_config(me, which, &title); |
680 | sfree(title); /* FIXME: should we use this somehow? */ |
681 | cfg_which = which; |
682 | |
683 | cfg_ncontrols = cfg_controlsize = 0; |
684 | cfg_controls = NULL; |
685 | leftw = rightw = 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 *); |
690 | } |
691 | |
692 | thish = 0; |
693 | |
694 | switch (i->type) { |
695 | case C_STRING: |
696 | /* |
697 | * Two NSTextFields, one being a label and the other |
698 | * being an edit box. |
699 | */ |
700 | |
701 | tf = [[NSTextField alloc] initWithFrame:tmprect]; |
702 | [tf setEditable:NO]; |
703 | [tf setSelectable:NO]; |
704 | [tf setBordered:NO]; |
705 | [tf setDrawsBackground:NO]; |
706 | [[tf cell] setTitle:[NSString stringWithCString:i->name]]; |
707 | [tf sizeToFit]; |
708 | rect = [tf frame]; |
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; |
712 | |
713 | /* We impose a minimum width on editable NSTextFields to |
714 | * stop them looking _completely_ silly. */ |
715 | if (rightw < 75) rightw = 75; |
716 | |
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]]; |
722 | [tf sizeToFit]; |
723 | rect = [tf frame]; |
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; |
727 | break; |
728 | |
729 | case C_BOOLEAN: |
730 | /* |
731 | * A checkbox is an NSButton with a type of |
732 | * NSSwitchButton. |
733 | */ |
734 | b = [[NSButton alloc] initWithFrame:tmprect]; |
735 | [b setBezelStyle:NSRoundedBezelStyle]; |
736 | [b setButtonType:NSSwitchButton]; |
737 | [b setTitle:[NSString stringWithCString:i->name]]; |
738 | [b sizeToFit]; |
739 | [b setState:(i->ival ? NSOnState : NSOffState)]; |
740 | rect = [b frame]; |
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; |
744 | break; |
745 | |
746 | case C_CHOICES: |
747 | /* |
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. |
751 | */ |
752 | |
753 | tf = [[NSTextField alloc] initWithFrame:tmprect]; |
754 | [tf setEditable:NO]; |
755 | [tf setSelectable:NO]; |
756 | [tf setBordered:NO]; |
757 | [tf setDrawsBackground:NO]; |
758 | [[tf cell] setTitle:[NSString stringWithCString:i->name]]; |
759 | [tf sizeToFit]; |
760 | rect = [tf frame]; |
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; |
764 | |
765 | pb = [[NSPopUpButton alloc] initWithFrame:tmprect pullsDown:NO]; |
766 | [pb setBezelStyle:NSRoundedBezelStyle]; |
767 | { |
768 | char c, *p; |
769 | |
770 | p = i->sval; |
771 | c = *p++; |
772 | while (*p) { |
773 | char *q; |
774 | |
775 | q = p; |
776 | while (*p && *p != c) p++; |
777 | |
778 | [pb addItemWithTitle:[NSString stringWithCString:q |
779 | length:p-q]]; |
780 | |
781 | if (*p) p++; |
782 | } |
783 | } |
784 | [pb selectItemAtIndex:i->ival]; |
785 | [pb sizeToFit]; |
786 | |
787 | rect = [pb frame]; |
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; |
791 | break; |
792 | } |
793 | |
794 | h += SPACING + thish; |
795 | } |
796 | |
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; |
803 | leftw += leftexcess; |
804 | rightw += rightexcess; |
805 | } |
806 | |
807 | /* |
808 | * Now go through the list again, setting the final position |
809 | * for each control. |
810 | */ |
811 | k = 0; |
812 | y = h; |
813 | for (i = cfg; i->type != C_END; i++) { |
814 | y -= SPACING; |
815 | thish = 0; |
816 | switch (i->type) { |
817 | case C_STRING: |
818 | case C_CHOICES: |
819 | /* |
820 | * These two are treated identically, since both expect |
821 | * a control on the left and another on the right. |
822 | */ |
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]; |
839 | k += 2; |
840 | break; |
841 | |
842 | case C_BOOLEAN: |
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]; |
850 | k++; |
851 | break; |
852 | } |
853 | y -= thish; |
854 | } |
855 | |
856 | assert(k == cfg_ncontrols); |
857 | |
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)]; |
860 | |
861 | sheet = [[NSWindow alloc] |
862 | initWithContentRect:NSMakeRect(0,0,totalw + 2*SPACING,h) |
863 | styleMask:NSTitledWindowMask | NSClosableWindowMask |
864 | backing:NSBackingStoreBuffered |
865 | defer:YES]; |
866 | |
867 | [[sheet contentView] addSubview:cancel]; |
868 | [[sheet contentView] addSubview:ok]; |
869 | |
870 | for (k = 0; k < cfg_ncontrols; k++) |
871 | [[sheet contentView] addSubview:cfg_controls[k]]; |
872 | |
873 | [NSApp beginSheet:sheet modalForWindow:self |
874 | modalDelegate:nil didEndSelector:nil contextInfo:nil]; |
875 | } |
876 | |
877 | - (void)specificGame:(id)sender |
878 | { |
879 | [self startConfigureSheet:CFG_SEED]; |
880 | } |
881 | |
882 | - (void)customGameType:(id)sender |
883 | { |
884 | [self startConfigureSheet:CFG_SETTINGS]; |
885 | } |
886 | |
887 | - (void)sheetEndWithStatus:(BOOL)update |
888 | { |
889 | assert(sheet != NULL); |
890 | [NSApp endSheet:sheet]; |
891 | [sheet orderOut:self]; |
892 | sheet = NULL; |
893 | if (update) { |
894 | int k; |
895 | config_item *i; |
896 | char *error; |
897 | |
898 | k = 0; |
899 | for (i = cfg; i->type != C_END; i++) { |
900 | switch (i->type) { |
901 | case C_STRING: |
902 | sfree(i->sval); |
903 | i->sval = dupstr([[[(id)cfg_controls[k+1] cell] |
904 | title] cString]); |
905 | k += 2; |
906 | break; |
907 | case C_BOOLEAN: |
908 | i->ival = [(id)cfg_controls[k] state] == NSOnState; |
909 | k++; |
910 | break; |
911 | case C_CHOICES: |
912 | i->ival = [(id)cfg_controls[k+1] indexOfSelectedItem]; |
913 | k += 2; |
914 | break; |
915 | } |
916 | } |
917 | |
918 | error = midend_set_config(me, cfg_which, cfg); |
919 | if (error) { |
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]; |
925 | } else { |
926 | midend_new_game(me); |
927 | [self resizeForNewGameParams]; |
928 | } |
929 | } |
930 | sfree(cfg_controls); |
931 | cfg_controls = NULL; |
932 | } |
933 | - (void)sheetOKButton:(id)sender |
934 | { |
935 | [self sheetEndWithStatus:YES]; |
936 | } |
937 | - (void)sheetCancelButton:(id)sender |
938 | { |
939 | [self sheetEndWithStatus:NO]; |
940 | } |
941 | |
a5102461 |
942 | - (void)setStatusLine:(NSString *)text |
943 | { |
944 | [[status cell] setTitle:text]; |
945 | } |
946 | |
9494d866 |
947 | @end |
948 | |
949 | /* |
950 | * Drawing routines called by the midend. |
951 | */ |
952 | void draw_polygon(frontend *fe, int *coords, int npoints, |
953 | int fill, int colour) |
954 | { |
955 | NSBezierPath *path = [NSBezierPath bezierPath]; |
956 | int i; |
957 | |
958 | [[NSGraphicsContext currentContext] setShouldAntialias:YES]; |
959 | |
960 | assert(colour >= 0 && colour < fe->ncolours); |
961 | [fe->colours[colour] set]; |
962 | |
963 | for (i = 0; i < npoints; i++) { |
964 | NSPoint p = { coords[i*2] + 0.5, coords[i*2+1] + 0.5 }; |
965 | if (i == 0) |
966 | [path moveToPoint:p]; |
967 | else |
968 | [path lineToPoint:p]; |
969 | } |
970 | |
971 | [path closePath]; |
972 | |
973 | if (fill) |
974 | [path fill]; |
975 | else |
976 | [path stroke]; |
977 | } |
978 | void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour) |
979 | { |
980 | NSBezierPath *path = [NSBezierPath bezierPath]; |
981 | NSPoint p1 = { x1 + 0.5, y1 + 0.5 }, p2 = { x2 + 0.5, y2 + 0.5 }; |
982 | |
983 | [[NSGraphicsContext currentContext] setShouldAntialias:NO]; |
984 | |
985 | assert(colour >= 0 && colour < fe->ncolours); |
986 | [fe->colours[colour] set]; |
987 | |
988 | [path moveToPoint:p1]; |
989 | [path lineToPoint:p2]; |
990 | [path stroke]; |
991 | } |
992 | void draw_rect(frontend *fe, int x, int y, int w, int h, int colour) |
993 | { |
994 | NSRect r = { {x,y}, {w,h} }; |
995 | |
996 | [[NSGraphicsContext currentContext] setShouldAntialias:NO]; |
997 | |
998 | assert(colour >= 0 && colour < fe->ncolours); |
999 | [fe->colours[colour] set]; |
1000 | |
1001 | NSRectFill(r); |
1002 | } |
1003 | void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize, |
1004 | int align, int colour, char *text) |
1005 | { |
1006 | NSString *string = [NSString stringWithCString:text]; |
1007 | NSDictionary *attr; |
1008 | NSFont *font; |
1009 | NSSize size; |
1010 | NSPoint point; |
1011 | |
1012 | [[NSGraphicsContext currentContext] setShouldAntialias:YES]; |
1013 | |
1014 | assert(colour >= 0 && colour < fe->ncolours); |
1015 | |
1016 | if (fonttype == FONT_FIXED) |
1017 | font = [NSFont userFixedPitchFontOfSize:fontsize]; |
1018 | else |
1019 | font = [NSFont userFontOfSize:fontsize]; |
1020 | |
1021 | attr = [NSDictionary dictionaryWithObjectsAndKeys: |
1022 | fe->colours[colour], NSForegroundColorAttributeName, |
1023 | font, NSFontAttributeName, nil]; |
1024 | |
1025 | point.x = x; |
1026 | point.y = y; |
1027 | |
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; |
1035 | |
1036 | [string drawAtPoint:point withAttributes:attr]; |
1037 | } |
1038 | void draw_update(frontend *fe, int x, int y, int w, int h) |
1039 | { |
1040 | /* FIXME */ |
1041 | } |
1042 | void clip(frontend *fe, int x, int y, int w, int h) |
1043 | { |
1044 | NSRect r = { {x,y}, {w,h} }; |
1045 | |
1046 | if (!fe->clipped) |
1047 | [[NSGraphicsContext currentContext] saveGraphicsState]; |
1048 | [NSBezierPath clipRect:r]; |
1049 | fe->clipped = TRUE; |
1050 | } |
1051 | void unclip(frontend *fe) |
1052 | { |
1053 | if (fe->clipped) |
1054 | [[NSGraphicsContext currentContext] restoreGraphicsState]; |
1055 | fe->clipped = FALSE; |
1056 | } |
1057 | void start_draw(frontend *fe) |
1058 | { |
1059 | [fe->image lockFocus]; |
1060 | fe->clipped = FALSE; |
1061 | } |
1062 | void end_draw(frontend *fe) |
1063 | { |
1064 | [fe->image unlockFocus]; |
1065 | [fe->view setNeedsDisplay]; |
1066 | } |
1067 | |
1068 | void deactivate_timer(frontend *fe) |
1069 | { |
1070 | [fe->window deactivateTimer]; |
1071 | } |
1072 | void activate_timer(frontend *fe) |
1073 | { |
1074 | [fe->window activateTimer]; |
1075 | } |
1076 | |
a5102461 |
1077 | void status_bar(frontend *fe, char *text) |
1078 | { |
1079 | [fe->window setStatusLine:[NSString stringWithCString:text]]; |
1080 | } |
1081 | |
9494d866 |
1082 | /* ---------------------------------------------------------------------- |
9494d866 |
1083 | * AppController: the object which receives the messages from all |
1084 | * menu selections that aren't standard OS X functions. |
1085 | */ |
1086 | @interface AppController : NSObject |
1087 | { |
1088 | } |
1089 | - (IBAction)newGame:(id)sender; |
1090 | @end |
1091 | |
1092 | @implementation AppController |
1093 | |
1094 | - (IBAction)newGame:(id)sender |
1095 | { |
7e04cb48 |
1096 | const game *g = [sender getPayload]; |
9494d866 |
1097 | id win; |
1098 | |
1099 | win = [[GameWindow alloc] initWithGame:g]; |
1100 | [win makeKeyAndOrderFront:self]; |
1101 | } |
1102 | |
3041164c |
1103 | - (NSMenu *)applicationDockMenu:(NSApplication *)sender |
1104 | { |
1105 | NSMenu *menu = newmenu("Dock Menu"); |
1106 | { |
1107 | int i; |
1108 | |
1109 | for (i = 0; i < gamecount; i++) { |
1110 | id item = |
1111 | initnewitem([DataMenuItem allocWithZone:[NSMenu menuZone]], |
1112 | menu, gamelist[i]->name, "", self, |
1113 | @selector(newGame:)); |
1114 | [item setPayload:(void *)gamelist[i]]; |
1115 | } |
1116 | } |
1117 | return menu; |
1118 | } |
1119 | |
9494d866 |
1120 | @end |
1121 | |
1122 | /* ---------------------------------------------------------------------- |
1123 | * Main program. Constructs the menus and runs the application. |
1124 | */ |
1125 | int main(int argc, char **argv) |
1126 | { |
1127 | NSAutoreleasePool *pool; |
1128 | NSMenu *menu; |
1129 | NSMenuItem *item; |
1130 | AppController *controller; |
a96edf8a |
1131 | NSImage *icon; |
9494d866 |
1132 | |
1133 | pool = [[NSAutoreleasePool alloc] init]; |
a96edf8a |
1134 | |
1135 | icon = [NSImage imageNamed:@"NSApplicationIcon"]; |
9494d866 |
1136 | [NSApplication sharedApplication]; |
a96edf8a |
1137 | [NSApp setApplicationIconImage:icon]; |
9494d866 |
1138 | |
1139 | controller = [[[AppController alloc] init] autorelease]; |
3041164c |
1140 | [NSApp setDelegate:controller]; |
9494d866 |
1141 | |
1142 | [NSApp setMainMenu: newmenu("Main Menu")]; |
1143 | |
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]; |
1153 | |
00461804 |
1154 | menu = newsubmenu([NSApp mainMenu], "Open"); |
9494d866 |
1155 | { |
1156 | int i; |
1157 | |
1158 | for (i = 0; i < gamecount; i++) { |
1159 | id item = |
7e04cb48 |
1160 | initnewitem([DataMenuItem allocWithZone:[NSMenu menuZone]], |
9494d866 |
1161 | menu, gamelist[i]->name, "", controller, |
1162 | @selector(newGame:)); |
7e04cb48 |
1163 | [item setPayload:(void *)gamelist[i]]; |
9494d866 |
1164 | } |
1165 | } |
1166 | |
00461804 |
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:)); |
1176 | |
1177 | menu = newsubmenu([NSApp mainMenu], "Type"); |
7e04cb48 |
1178 | typemenu = menu; |
00461804 |
1179 | item = newitem(menu, "Custom", "", NULL, @selector(customGameType:)); |
1180 | |
1181 | menu = newsubmenu([NSApp mainMenu], "Window"); |
9494d866 |
1182 | [NSApp setWindowsMenu: menu]; |
00461804 |
1183 | item = newitem(menu, "Minimise Window", "m", NULL, @selector(performMiniaturize:)); |
9494d866 |
1184 | |
1185 | [NSApp run]; |
1186 | [pool release]; |
1187 | } |