9494d866 |
1 | /* |
2 | * Mac OS X / Cocoa front end to puzzles. |
00461804 |
3 | * |
fccfd04d |
4 | * Still to do: |
5 | * |
8709d5d9 |
6 | * - I'd like to be able to call up context help for a specific |
7 | * game at a time. |
a5102461 |
8 | * |
a5102461 |
9 | * Mac interface issues that possibly could be done better: |
10 | * |
11 | * - is there a better approach to frontend_default_colour? |
12 | * |
13 | * - do we need any more options in the Window menu? |
00461804 |
14 | * |
2411c162 |
15 | * - can / should we be doing anything with the titles of the |
16 | * configuration boxes? |
17 | * |
9494d866 |
18 | * - not sure what I should be doing about default window |
19 | * placement. Centring new windows is a bit feeble, but what's |
20 | * better? Is there a standard way to tell the OS "here's the |
21 | * _size_ of window I want, now use your best judgment about the |
22 | * initial position"? |
364722d2 |
23 | * + there's a standard _policy_ on window placement, given in |
24 | * the HI guidelines. Have to implement it ourselves though, |
25 | * bah. |
00461804 |
26 | * |
9494d866 |
27 | * - a brief frob of the Mac numeric keypad suggests that it |
28 | * generates numbers no matter what you do. I wonder if I should |
29 | * try to figure out a way of detecting keypad codes so I can |
6d634196 |
30 | * implement UP_LEFT and friends. Alternatively, perhaps I |
31 | * should simply assign the number keys to UP_LEFT et al? |
32 | * They're not in use for anything else right now. |
00461804 |
33 | * |
00461804 |
34 | * - see if we can do anything to one-button-ise the multi-button |
35 | * dependent puzzle UIs: |
36 | * - Pattern is a _little_ unwieldy but not too bad (since |
37 | * generally you never need the middle button unless you've |
38 | * made a mistake, so it's just click versus command-click). |
39 | * - Net is utterly vile; having normal click be one rotate and |
40 | * command-click be the other introduces a horrid asymmetry, |
41 | * and yet requiring a shift key for _each_ click would be |
42 | * even worse because rotation feels as if it ought to be the |
43 | * default action. I fear this is why the Flash Net had the |
44 | * UI it did... |
364722d2 |
45 | * + I've tried out an alternative dragging interface for |
46 | * Net; it might work nicely for stylus-based platforms |
47 | * where you have better hand/eye feedback for the thing |
48 | * you're clicking on, but it's rather unwieldy on the |
49 | * Mac. I fear even shift-clicking is better than that. |
00461804 |
50 | * |
e1107f8b |
51 | * - Should we _return_ to a game configuration sheet once an |
52 | * error is reported by midend_set_config, to allow the user to |
53 | * correct the one faulty input and keep the other five OK ones? |
54 | * The Apple `one sheet at a time' restriction would require me |
55 | * to do this by closing the config sheet, opening the alert |
56 | * sheet, and then reopening the config sheet when the alert is |
57 | * closed; and the human interface types, who presumably |
58 | * invented the one-sheet-at-a-time rule for good reasons, might |
59 | * look with disfavour on me trying to get round them to fake a |
60 | * nested sheet. On the other hand I think there are good |
61 | * practical reasons for wanting it that way. Uncertain. |
62 | * |
bacaa96e |
63 | * - User feedback dislikes nothing happening when you start the |
64 | * app; they suggest a finder-like window containing an icon for |
65 | * each puzzle type, enabling you to start one easily. Needs |
364722d2 |
66 | * thought. |
67 | * |
7e04cb48 |
68 | * Grotty implementation details that could probably be improved: |
69 | * |
70 | * - I am _utterly_ unconvinced that NSImageView was the right way |
71 | * to go about having a window with a reliable backing store! It |
72 | * just doesn't feel right; NSImageView is a _control_. Is there |
73 | * a simpler way? |
74 | * |
75 | * - Resizing is currently very bad; rather than bother to work |
76 | * out how to resize the NSImageView, I just splatter and |
77 | * recreate it. |
9494d866 |
78 | */ |
79 | |
80 | #include <ctype.h> |
81 | #include <sys/time.h> |
82 | #import <Cocoa/Cocoa.h> |
83 | #include "puzzles.h" |
84 | |
e7fabdfa |
85 | /* ---------------------------------------------------------------------- |
86 | * Global variables. |
87 | */ |
88 | |
89 | /* |
90 | * The `Type' menu. We frob this dynamically to allow the user to |
91 | * choose a preset set of settings from the current game. |
92 | */ |
93 | NSMenu *typemenu; |
94 | |
dafd6cf6 |
95 | /* |
96 | * Forward reference. |
97 | */ |
98 | extern const struct drawing_api osx_drawing; |
99 | |
e7fabdfa |
100 | /* ---------------------------------------------------------------------- |
101 | * Miscellaneous support routines that aren't part of any object or |
102 | * clearly defined subsystem. |
103 | */ |
104 | |
9494d866 |
105 | void fatal(char *fmt, ...) |
106 | { |
9494d866 |
107 | va_list ap; |
e7fabdfa |
108 | char errorbuf[2048]; |
109 | NSAlert *alert; |
9494d866 |
110 | |
111 | va_start(ap, fmt); |
e7fabdfa |
112 | vsnprintf(errorbuf, lenof(errorbuf), fmt, ap); |
9494d866 |
113 | va_end(ap); |
114 | |
e7fabdfa |
115 | alert = [NSAlert alloc]; |
116 | /* |
117 | * We may have come here because we ran out of memory, in which |
118 | * case it's entirely likely that that alloc will fail, so we |
119 | * should have a fallback of some sort. |
120 | */ |
121 | if (!alert) { |
122 | fprintf(stderr, "fatal error (and NSAlert failed): %s\n", errorbuf); |
123 | } else { |
124 | alert = [[alert init] autorelease]; |
125 | [alert addButtonWithTitle:@"Oh dear"]; |
126 | [alert setInformativeText:[NSString stringWithCString:errorbuf]]; |
127 | [alert runModal]; |
128 | } |
9494d866 |
129 | exit(1); |
130 | } |
131 | |
132 | void frontend_default_colour(frontend *fe, float *output) |
133 | { |
e7fabdfa |
134 | /* FIXME: Is there a system default we can tap into for this? */ |
9494d866 |
135 | output[0] = output[1] = output[2] = 0.8F; |
136 | } |
9494d866 |
137 | |
138 | void get_random_seed(void **randseed, int *randseedsize) |
139 | { |
140 | time_t *tp = snew(time_t); |
141 | time(tp); |
142 | *randseed = (void *)tp; |
143 | *randseedsize = sizeof(time_t); |
144 | } |
145 | |
668be019 |
146 | static void savefile_write(void *wctx, void *buf, int len) |
147 | { |
148 | FILE *fp = (FILE *)wctx; |
149 | fwrite(buf, 1, len, fp); |
150 | } |
151 | |
152 | static int savefile_read(void *wctx, void *buf, int len) |
153 | { |
154 | FILE *fp = (FILE *)wctx; |
155 | int ret; |
156 | |
157 | ret = fread(buf, 1, len, fp); |
158 | return (ret == len); |
159 | } |
160 | |
dafd6cf6 |
161 | /* |
162 | * Since this front end does not support printing (yet), we need |
163 | * this stub to satisfy the reference in midend_print_puzzle(). |
164 | */ |
165 | void document_add_puzzle(document *doc, const game *game, game_params *par, |
166 | game_state *st, game_state *st2) |
167 | { |
168 | } |
169 | |
eed62081 |
170 | /* |
171 | * setAppleMenu isn't listed in the NSApplication header, but an |
172 | * NSApp responds to it, so we're adding it here to silence |
173 | * warnings. (This was removed from the headers in 10.4, so we |
174 | * only need to include it for 10.4+.) |
175 | */ |
176 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1040 |
177 | @interface NSApplication(NSAppleMenu) |
178 | - (void)setAppleMenu:(NSMenu *)menu; |
179 | @end |
180 | #endif |
181 | |
9494d866 |
182 | /* ---------------------------------------------------------------------- |
7e04cb48 |
183 | * Tiny extension to NSMenuItem which carries a payload of a `void |
184 | * *', allowing several menu items to invoke the same message but |
185 | * pass different data through it. |
186 | */ |
187 | @interface DataMenuItem : NSMenuItem |
188 | { |
189 | void *payload; |
7e04cb48 |
190 | } |
191 | - (void)setPayload:(void *)d; |
7e04cb48 |
192 | - (void *)getPayload; |
193 | @end |
194 | @implementation DataMenuItem |
7e04cb48 |
195 | - (void)setPayload:(void *)d |
196 | { |
197 | payload = d; |
198 | } |
7e04cb48 |
199 | - (void *)getPayload |
200 | { |
201 | return payload; |
202 | } |
7e04cb48 |
203 | @end |
204 | |
205 | /* ---------------------------------------------------------------------- |
e1107f8b |
206 | * Utility routines for constructing OS X menus. |
207 | */ |
208 | |
209 | NSMenu *newmenu(const char *title) |
210 | { |
211 | return [[[NSMenu allocWithZone:[NSMenu menuZone]] |
212 | initWithTitle:[NSString stringWithCString:title]] |
213 | autorelease]; |
214 | } |
215 | |
216 | NSMenu *newsubmenu(NSMenu *parent, const char *title) |
217 | { |
218 | NSMenuItem *item; |
219 | NSMenu *child; |
220 | |
221 | item = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] |
222 | initWithTitle:[NSString stringWithCString:title] |
223 | action:NULL |
224 | keyEquivalent:@""] |
225 | autorelease]; |
226 | child = newmenu(title); |
227 | [item setEnabled:YES]; |
228 | [item setSubmenu:child]; |
229 | [parent addItem:item]; |
230 | return child; |
231 | } |
232 | |
233 | id initnewitem(NSMenuItem *item, NSMenu *parent, const char *title, |
234 | const char *key, id target, SEL action) |
235 | { |
236 | unsigned mask = NSCommandKeyMask; |
237 | |
238 | if (key[strcspn(key, "-")]) { |
239 | while (*key && *key != '-') { |
240 | int c = tolower((unsigned char)*key); |
241 | if (c == 's') { |
242 | mask |= NSShiftKeyMask; |
243 | } else if (c == 'o' || c == 'a') { |
244 | mask |= NSAlternateKeyMask; |
245 | } |
246 | key++; |
247 | } |
248 | if (*key) |
249 | key++; |
250 | } |
251 | |
252 | item = [[item initWithTitle:[NSString stringWithCString:title] |
253 | action:NULL |
254 | keyEquivalent:[NSString stringWithCString:key]] |
255 | autorelease]; |
256 | |
257 | if (*key) |
258 | [item setKeyEquivalentModifierMask: mask]; |
259 | |
260 | [item setEnabled:YES]; |
261 | [item setTarget:target]; |
262 | [item setAction:action]; |
263 | |
264 | [parent addItem:item]; |
265 | |
266 | return item; |
267 | } |
268 | |
269 | NSMenuItem *newitem(NSMenu *parent, char *title, char *key, |
270 | id target, SEL action) |
271 | { |
272 | return initnewitem([NSMenuItem allocWithZone:[NSMenu menuZone]], |
273 | parent, title, key, target, action); |
274 | } |
275 | |
276 | /* ---------------------------------------------------------------------- |
97098757 |
277 | * About box. |
278 | */ |
279 | |
280 | @class AboutBox; |
281 | |
282 | @interface AboutBox : NSWindow |
283 | { |
284 | } |
285 | - (id)init; |
286 | @end |
287 | |
288 | @implementation AboutBox |
289 | - (id)init |
290 | { |
291 | NSRect totalrect; |
292 | NSView *views[16]; |
293 | int nviews = 0; |
294 | NSImageView *iv; |
295 | NSTextField *tf; |
296 | NSFont *font1 = [NSFont systemFontOfSize:0]; |
297 | NSFont *font2 = [NSFont boldSystemFontOfSize:[font1 pointSize] * 1.1]; |
298 | const int border = 24; |
299 | int i; |
300 | double y; |
301 | |
302 | /* |
303 | * Construct the controls that go in the About box. |
304 | */ |
305 | |
306 | iv = [[NSImageView alloc] initWithFrame:NSMakeRect(0,0,64,64)]; |
307 | [iv setImage:[NSImage imageNamed:@"NSApplicationIcon"]]; |
308 | views[nviews++] = iv; |
309 | |
310 | tf = [[NSTextField alloc] |
311 | initWithFrame:NSMakeRect(0,0,400,1)]; |
312 | [tf setEditable:NO]; |
313 | [tf setSelectable:NO]; |
314 | [tf setBordered:NO]; |
315 | [tf setDrawsBackground:NO]; |
316 | [tf setFont:font2]; |
317 | [tf setStringValue:@"Simon Tatham's Portable Puzzle Collection"]; |
318 | [tf sizeToFit]; |
319 | views[nviews++] = tf; |
320 | |
321 | tf = [[NSTextField alloc] |
322 | initWithFrame:NSMakeRect(0,0,400,1)]; |
323 | [tf setEditable:NO]; |
324 | [tf setSelectable:NO]; |
325 | [tf setBordered:NO]; |
326 | [tf setDrawsBackground:NO]; |
327 | [tf setFont:font1]; |
328 | [tf setStringValue:[NSString stringWithCString:ver]]; |
329 | [tf sizeToFit]; |
330 | views[nviews++] = tf; |
331 | |
332 | /* |
333 | * Lay the controls out. |
334 | */ |
335 | totalrect = NSMakeRect(0,0,0,0); |
336 | for (i = 0; i < nviews; i++) { |
337 | NSRect r = [views[i] frame]; |
338 | if (totalrect.size.width < r.size.width) |
339 | totalrect.size.width = r.size.width; |
340 | totalrect.size.height += border + r.size.height; |
341 | } |
342 | totalrect.size.width += 2 * border; |
343 | totalrect.size.height += border; |
344 | y = totalrect.size.height; |
345 | for (i = 0; i < nviews; i++) { |
346 | NSRect r = [views[i] frame]; |
347 | r.origin.x = (totalrect.size.width - r.size.width) / 2; |
348 | y -= border + r.size.height; |
349 | r.origin.y = y; |
350 | [views[i] setFrame:r]; |
351 | } |
352 | |
353 | self = [super initWithContentRect:totalrect |
354 | styleMask:(NSTitledWindowMask | NSMiniaturizableWindowMask | |
355 | NSClosableWindowMask) |
356 | backing:NSBackingStoreBuffered |
357 | defer:YES]; |
358 | |
359 | for (i = 0; i < nviews; i++) |
360 | [[self contentView] addSubview:views[i]]; |
361 | |
362 | [self center]; /* :-) */ |
363 | |
364 | return self; |
365 | } |
366 | @end |
367 | |
368 | /* ---------------------------------------------------------------------- |
9494d866 |
369 | * The front end presented to midend.c. |
370 | * |
371 | * This is mostly a subclass of NSWindow. The actual `frontend' |
372 | * structure passed to the midend contains a variety of pointers, |
373 | * including that window object but also including the image we |
374 | * draw on, an ImageView to display it in the window, and so on. |
375 | */ |
376 | |
377 | @class GameWindow; |
378 | @class MyImageView; |
379 | |
380 | struct frontend { |
381 | GameWindow *window; |
382 | NSImage *image; |
383 | MyImageView *view; |
384 | NSColor **colours; |
385 | int ncolours; |
386 | int clipped; |
387 | }; |
388 | |
389 | @interface MyImageView : NSImageView |
390 | { |
391 | GameWindow *ourwin; |
392 | } |
393 | - (void)setWindow:(GameWindow *)win; |
394 | - (BOOL)isFlipped; |
395 | - (void)mouseEvent:(NSEvent *)ev button:(int)b; |
396 | - (void)mouseDown:(NSEvent *)ev; |
397 | - (void)mouseDragged:(NSEvent *)ev; |
398 | - (void)mouseUp:(NSEvent *)ev; |
399 | - (void)rightMouseDown:(NSEvent *)ev; |
400 | - (void)rightMouseDragged:(NSEvent *)ev; |
401 | - (void)rightMouseUp:(NSEvent *)ev; |
402 | - (void)otherMouseDown:(NSEvent *)ev; |
403 | - (void)otherMouseDragged:(NSEvent *)ev; |
404 | - (void)otherMouseUp:(NSEvent *)ev; |
405 | @end |
406 | |
407 | @interface GameWindow : NSWindow |
408 | { |
409 | const game *ourgame; |
dafd6cf6 |
410 | midend *me; |
9494d866 |
411 | struct frontend fe; |
412 | struct timeval last_time; |
413 | NSTimer *timer; |
e1107f8b |
414 | NSWindow *sheet; |
415 | config_item *cfg; |
416 | int cfg_which; |
417 | NSView **cfg_controls; |
418 | int cfg_ncontrols; |
a5102461 |
419 | NSTextField *status; |
9494d866 |
420 | } |
421 | - (id)initWithGame:(const game *)g; |
eed62081 |
422 | - (void)dealloc; |
9494d866 |
423 | - (void)processButton:(int)b x:(int)x y:(int)y; |
424 | - (void)keyDown:(NSEvent *)ev; |
425 | - (void)activateTimer; |
426 | - (void)deactivateTimer; |
48dcdd62 |
427 | - (void)setStatusLine:(char *)text; |
668be019 |
428 | - (void)resizeForNewGameParams; |
ae2cfcdd |
429 | - (void)updateTypeMenuTick; |
9494d866 |
430 | @end |
431 | |
432 | @implementation MyImageView |
433 | |
434 | - (void)setWindow:(GameWindow *)win |
435 | { |
436 | ourwin = win; |
437 | } |
438 | |
439 | - (BOOL)isFlipped |
440 | { |
441 | return YES; |
442 | } |
443 | |
444 | - (void)mouseEvent:(NSEvent *)ev button:(int)b |
445 | { |
446 | NSPoint point = [self convertPoint:[ev locationInWindow] fromView:nil]; |
447 | [ourwin processButton:b x:point.x y:point.y]; |
448 | } |
449 | |
450 | - (void)mouseDown:(NSEvent *)ev |
451 | { |
452 | unsigned mod = [ev modifierFlags]; |
453 | [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_BUTTON : |
454 | (mod & NSShiftKeyMask) ? MIDDLE_BUTTON : |
455 | LEFT_BUTTON)]; |
456 | } |
457 | - (void)mouseDragged:(NSEvent *)ev |
458 | { |
459 | unsigned mod = [ev modifierFlags]; |
460 | [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_DRAG : |
461 | (mod & NSShiftKeyMask) ? MIDDLE_DRAG : |
462 | LEFT_DRAG)]; |
463 | } |
464 | - (void)mouseUp:(NSEvent *)ev |
465 | { |
466 | unsigned mod = [ev modifierFlags]; |
467 | [self mouseEvent:ev button:((mod & NSCommandKeyMask) ? RIGHT_RELEASE : |
468 | (mod & NSShiftKeyMask) ? MIDDLE_RELEASE : |
469 | LEFT_RELEASE)]; |
470 | } |
471 | - (void)rightMouseDown:(NSEvent *)ev |
472 | { |
473 | unsigned mod = [ev modifierFlags]; |
474 | [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_BUTTON : |
475 | RIGHT_BUTTON)]; |
476 | } |
477 | - (void)rightMouseDragged:(NSEvent *)ev |
478 | { |
479 | unsigned mod = [ev modifierFlags]; |
480 | [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_DRAG : |
481 | RIGHT_DRAG)]; |
482 | } |
483 | - (void)rightMouseUp:(NSEvent *)ev |
484 | { |
485 | unsigned mod = [ev modifierFlags]; |
486 | [self mouseEvent:ev button:((mod & NSShiftKeyMask) ? MIDDLE_RELEASE : |
487 | RIGHT_RELEASE)]; |
488 | } |
489 | - (void)otherMouseDown:(NSEvent *)ev |
490 | { |
491 | [self mouseEvent:ev button:MIDDLE_BUTTON]; |
492 | } |
493 | - (void)otherMouseDragged:(NSEvent *)ev |
494 | { |
495 | [self mouseEvent:ev button:MIDDLE_DRAG]; |
496 | } |
497 | - (void)otherMouseUp:(NSEvent *)ev |
498 | { |
499 | [self mouseEvent:ev button:MIDDLE_RELEASE]; |
500 | } |
501 | @end |
502 | |
503 | @implementation GameWindow |
7e04cb48 |
504 | - (void)setupContentView |
505 | { |
a5102461 |
506 | NSRect frame; |
7e04cb48 |
507 | int w, h; |
508 | |
a5102461 |
509 | if (status) { |
510 | frame = [status frame]; |
511 | frame.origin.y = frame.size.height; |
512 | } else |
513 | frame.origin.y = 0; |
514 | frame.origin.x = 0; |
515 | |
1e3e152d |
516 | w = h = INT_MAX; |
517 | midend_size(me, &w, &h, FALSE); |
a5102461 |
518 | frame.size.width = w; |
519 | frame.size.height = h; |
7e04cb48 |
520 | |
a5102461 |
521 | fe.image = [[NSImage alloc] initWithSize:frame.size]; |
7e04cb48 |
522 | [fe.image setFlipped:YES]; |
a5102461 |
523 | fe.view = [[MyImageView alloc] initWithFrame:frame]; |
7e04cb48 |
524 | [fe.view setImage:fe.image]; |
525 | [fe.view setWindow:self]; |
526 | |
527 | midend_redraw(me); |
528 | |
a5102461 |
529 | [[self contentView] addSubview:fe.view]; |
7e04cb48 |
530 | } |
9494d866 |
531 | - (id)initWithGame:(const game *)g |
532 | { |
a5102461 |
533 | NSRect rect = { {0,0}, {0,0} }, rect2; |
9494d866 |
534 | int w, h; |
535 | |
536 | ourgame = g; |
537 | |
538 | fe.window = self; |
539 | |
c3b4e35a |
540 | me = midend_new(&fe, ourgame, &osx_drawing, &fe); |
9494d866 |
541 | /* |
542 | * If we ever need to open a fresh window using a provided game |
543 | * ID, I think the right thing is to move most of this method |
544 | * into a new initWithGame:gameID: method, and have |
545 | * initWithGame: simply call that one and pass it NULL. |
546 | */ |
547 | midend_new_game(me); |
1e3e152d |
548 | w = h = INT_MAX; |
549 | midend_size(me, &w, &h, FALSE); |
9494d866 |
550 | rect.size.width = w; |
551 | rect.size.height = h; |
552 | |
a5102461 |
553 | /* |
554 | * Create the status bar, which will just be an NSTextField. |
555 | */ |
910c21c0 |
556 | if (midend_wants_statusbar(me)) { |
a5102461 |
557 | status = [[NSTextField alloc] initWithFrame:NSMakeRect(0,0,100,50)]; |
558 | [status setEditable:NO]; |
559 | [status setSelectable:NO]; |
560 | [status setBordered:YES]; |
561 | [status setBezeled:YES]; |
562 | [status setBezelStyle:NSTextFieldSquareBezel]; |
563 | [status setDrawsBackground:YES]; |
564 | [[status cell] setTitle:@""]; |
565 | [status sizeToFit]; |
566 | rect2 = [status frame]; |
567 | rect.size.height += rect2.size.height; |
568 | rect2.size.width = rect.size.width; |
569 | rect2.origin.x = rect2.origin.y = 0; |
570 | [status setFrame:rect2]; |
571 | } else |
572 | status = nil; |
573 | |
9494d866 |
574 | self = [super initWithContentRect:rect |
575 | styleMask:(NSTitledWindowMask | NSMiniaturizableWindowMask | |
576 | NSClosableWindowMask) |
577 | backing:NSBackingStoreBuffered |
e1107f8b |
578 | defer:YES]; |
9494d866 |
579 | [self setTitle:[NSString stringWithCString:ourgame->name]]; |
580 | |
581 | { |
582 | float *colours; |
583 | int i, ncolours; |
584 | |
585 | colours = midend_colours(me, &ncolours); |
586 | fe.ncolours = ncolours; |
587 | fe.colours = snewn(ncolours, NSColor *); |
588 | |
589 | for (i = 0; i < ncolours; i++) { |
590 | fe.colours[i] = [[NSColor colorWithDeviceRed:colours[i*3] |
591 | green:colours[i*3+1] blue:colours[i*3+2] |
592 | alpha:1.0] retain]; |
593 | } |
594 | } |
595 | |
7e04cb48 |
596 | [self setupContentView]; |
a5102461 |
597 | if (status) |
598 | [[self contentView] addSubview:status]; |
9494d866 |
599 | [self setIgnoresMouseEvents:NO]; |
600 | |
9494d866 |
601 | [self center]; /* :-) */ |
602 | |
603 | return self; |
604 | } |
605 | |
eed62081 |
606 | - (void)dealloc |
9494d866 |
607 | { |
608 | int i; |
609 | for (i = 0; i < fe.ncolours; i++) { |
610 | [fe.colours[i] release]; |
611 | } |
612 | sfree(fe.colours); |
613 | midend_free(me); |
eed62081 |
614 | [super dealloc]; |
9494d866 |
615 | } |
616 | |
617 | - (void)processButton:(int)b x:(int)x y:(int)y |
618 | { |
619 | if (!midend_process_key(me, x, y, b)) |
620 | [self close]; |
621 | } |
622 | |
623 | - (void)keyDown:(NSEvent *)ev |
624 | { |
625 | NSString *s = [ev characters]; |
626 | int i, n = [s length]; |
627 | |
628 | for (i = 0; i < n; i++) { |
629 | int c = [s characterAtIndex:i]; |
630 | |
631 | /* |
632 | * ASCII gets passed straight to midend_process_key. |
633 | * Anything above that has to be translated to our own |
634 | * function key codes. |
635 | */ |
636 | if (c >= 0x80) { |
65a92074 |
637 | int mods = FALSE; |
9494d866 |
638 | switch (c) { |
639 | case NSUpArrowFunctionKey: |
640 | c = CURSOR_UP; |
65a92074 |
641 | mods = TRUE; |
9494d866 |
642 | break; |
643 | case NSDownArrowFunctionKey: |
644 | c = CURSOR_DOWN; |
65a92074 |
645 | mods = TRUE; |
9494d866 |
646 | break; |
647 | case NSLeftArrowFunctionKey: |
648 | c = CURSOR_LEFT; |
65a92074 |
649 | mods = TRUE; |
9494d866 |
650 | break; |
651 | case NSRightArrowFunctionKey: |
652 | c = CURSOR_RIGHT; |
65a92074 |
653 | mods = TRUE; |
9494d866 |
654 | break; |
655 | default: |
656 | continue; |
657 | } |
65a92074 |
658 | |
659 | if (mods) { |
660 | if ([ev modifierFlags] & NSShiftKeyMask) |
661 | c |= MOD_SHFT; |
662 | if ([ev modifierFlags] & NSControlKeyMask) |
663 | c |= MOD_CTRL; |
664 | } |
9494d866 |
665 | } |
666 | |
3c833d45 |
667 | if (c >= '0' && c <= '9' && ([ev modifierFlags] & NSNumericPadKeyMask)) |
668 | c |= MOD_NUM_KEYPAD; |
669 | |
9494d866 |
670 | [self processButton:c x:-1 y:-1]; |
671 | } |
672 | } |
673 | |
674 | - (void)activateTimer |
675 | { |
676 | if (timer != nil) |
677 | return; |
678 | |
679 | timer = [NSTimer scheduledTimerWithTimeInterval:0.02 |
680 | target:self selector:@selector(timerTick:) |
681 | userInfo:nil repeats:YES]; |
682 | gettimeofday(&last_time, NULL); |
683 | } |
684 | |
685 | - (void)deactivateTimer |
686 | { |
687 | if (timer == nil) |
688 | return; |
689 | |
690 | [timer invalidate]; |
691 | timer = nil; |
692 | } |
693 | |
694 | - (void)timerTick:(id)sender |
695 | { |
696 | struct timeval now; |
697 | float elapsed; |
698 | gettimeofday(&now, NULL); |
699 | elapsed = ((now.tv_usec - last_time.tv_usec) * 0.000001F + |
700 | (now.tv_sec - last_time.tv_sec)); |
701 | midend_timer(me, elapsed); |
702 | last_time = now; |
703 | } |
704 | |
668be019 |
705 | - (void)showError:(char *)message |
706 | { |
707 | NSAlert *alert; |
708 | |
709 | alert = [[[NSAlert alloc] init] autorelease]; |
710 | [alert addButtonWithTitle:@"Bah"]; |
711 | [alert setInformativeText:[NSString stringWithCString:message]]; |
712 | [alert beginSheetModalForWindow:self modalDelegate:nil |
713 | didEndSelector:nil contextInfo:nil]; |
714 | } |
715 | |
00461804 |
716 | - (void)newGame:(id)sender |
717 | { |
718 | [self processButton:'n' x:-1 y:-1]; |
719 | } |
720 | - (void)restartGame:(id)sender |
721 | { |
7f89707c |
722 | midend_restart_game(me); |
00461804 |
723 | } |
668be019 |
724 | - (void)saveGame:(id)sender |
725 | { |
726 | NSSavePanel *sp = [NSSavePanel savePanel]; |
727 | |
728 | if ([sp runModal] == NSFileHandlingPanelOKButton) { |
eed62081 |
729 | const char *name = [[sp filename] UTF8String]; |
668be019 |
730 | |
731 | FILE *fp = fopen(name, "w"); |
732 | |
733 | if (!fp) { |
734 | [self showError:"Unable to open save file"]; |
735 | return; |
736 | } |
737 | |
738 | midend_serialise(me, savefile_write, fp); |
739 | |
740 | fclose(fp); |
741 | } |
742 | } |
743 | - (void)loadSavedGame:(id)sender |
744 | { |
745 | NSOpenPanel *op = [NSOpenPanel openPanel]; |
746 | |
747 | [op setAllowsMultipleSelection:NO]; |
748 | |
749 | if ([op runModalForTypes:nil] == NSOKButton) { |
750 | const char *name = [[[op filenames] objectAtIndex:0] cString]; |
751 | char *err; |
752 | |
753 | FILE *fp = fopen(name, "r"); |
754 | |
755 | if (!fp) { |
756 | [self showError:"Unable to open saved game file"]; |
757 | return; |
758 | } |
759 | |
760 | err = midend_deserialise(me, savefile_read, fp); |
761 | |
762 | fclose(fp); |
763 | |
764 | if (err) { |
765 | [self showError:err]; |
766 | return; |
767 | } |
768 | |
769 | [self resizeForNewGameParams]; |
ae2cfcdd |
770 | [self updateTypeMenuTick]; |
668be019 |
771 | } |
772 | } |
00461804 |
773 | - (void)undoMove:(id)sender |
774 | { |
775 | [self processButton:'u' x:-1 y:-1]; |
776 | } |
777 | - (void)redoMove:(id)sender |
778 | { |
779 | [self processButton:'r'&0x1F x:-1 y:-1]; |
780 | } |
781 | |
9b4b03d3 |
782 | - (void)copy:(id)sender |
783 | { |
784 | char *text; |
785 | |
786 | if ((text = midend_text_format(me)) != NULL) { |
787 | NSPasteboard *pb = [NSPasteboard generalPasteboard]; |
788 | NSArray *a = [NSArray arrayWithObject:NSStringPboardType]; |
789 | [pb declareTypes:a owner:nil]; |
790 | [pb setString:[NSString stringWithCString:text] |
791 | forType:NSStringPboardType]; |
792 | } else |
793 | NSBeep(); |
794 | } |
795 | |
2ac6d24e |
796 | - (void)solveGame:(id)sender |
797 | { |
798 | char *msg; |
2ac6d24e |
799 | |
800 | msg = midend_solve(me); |
801 | |
668be019 |
802 | if (msg) |
803 | [self showError:msg]; |
2ac6d24e |
804 | } |
805 | |
9b4b03d3 |
806 | - (BOOL)validateMenuItem:(NSMenuItem *)item |
807 | { |
808 | if ([item action] == @selector(copy:)) |
fa3abef5 |
809 | return (ourgame->can_format_as_text_ever && |
810 | midend_can_format_as_text_now(me) ? YES : NO); |
2ac6d24e |
811 | else if ([item action] == @selector(solveGame:)) |
812 | return (ourgame->can_solve ? YES : NO); |
9b4b03d3 |
813 | else |
814 | return [super validateMenuItem:item]; |
815 | } |
816 | |
7e04cb48 |
817 | - (void)clearTypeMenu |
818 | { |
819 | while ([typemenu numberOfItems] > 1) |
820 | [typemenu removeItemAtIndex:0]; |
ae2cfcdd |
821 | [[typemenu itemAtIndex:0] setState:NSOffState]; |
822 | } |
823 | |
824 | - (void)updateTypeMenuTick |
825 | { |
826 | int i, total, n; |
827 | |
828 | total = [typemenu numberOfItems]; |
829 | n = midend_which_preset(me); |
830 | if (n < 0) |
831 | n = total - 1; /* that's always where "Custom" lives */ |
832 | for (i = 0; i < total; i++) |
833 | [[typemenu itemAtIndex:i] setState:(i == n ? NSOnState : NSOffState)]; |
7e04cb48 |
834 | } |
835 | |
836 | - (void)becomeKeyWindow |
837 | { |
838 | int n; |
839 | |
840 | [self clearTypeMenu]; |
841 | |
842 | [super becomeKeyWindow]; |
843 | |
844 | n = midend_num_presets(me); |
845 | |
846 | if (n > 0) { |
847 | [typemenu insertItem:[NSMenuItem separatorItem] atIndex:0]; |
848 | while (n--) { |
849 | char *name; |
850 | game_params *params; |
851 | DataMenuItem *item; |
852 | |
853 | midend_fetch_preset(me, n, &name, ¶ms); |
854 | |
855 | item = [[[DataMenuItem alloc] |
856 | initWithTitle:[NSString stringWithCString:name] |
857 | action:NULL keyEquivalent:@""] |
858 | autorelease]; |
859 | |
860 | [item setEnabled:YES]; |
861 | [item setTarget:self]; |
862 | [item setAction:@selector(presetGame:)]; |
863 | [item setPayload:params]; |
7e04cb48 |
864 | |
865 | [typemenu insertItem:item atIndex:0]; |
866 | } |
867 | } |
ae2cfcdd |
868 | |
869 | [self updateTypeMenuTick]; |
7e04cb48 |
870 | } |
871 | |
872 | - (void)resignKeyWindow |
873 | { |
874 | [self clearTypeMenu]; |
875 | [super resignKeyWindow]; |
876 | } |
877 | |
878 | - (void)close |
879 | { |
880 | [self clearTypeMenu]; |
881 | [super close]; |
882 | } |
883 | |
884 | - (void)resizeForNewGameParams |
885 | { |
886 | NSSize size = {0,0}; |
887 | int w, h; |
888 | |
1e3e152d |
889 | w = h = INT_MAX; |
890 | midend_size(me, &w, &h, FALSE); |
7e04cb48 |
891 | size.width = w; |
892 | size.height = h; |
893 | |
2411c162 |
894 | if (status) { |
895 | NSRect frame = [status frame]; |
896 | size.height += frame.size.height; |
897 | frame.size.width = size.width; |
898 | [status setFrame:frame]; |
899 | } |
900 | |
7e04cb48 |
901 | NSDisableScreenUpdates(); |
902 | [self setContentSize:size]; |
903 | [self setupContentView]; |
904 | NSEnableScreenUpdates(); |
905 | } |
906 | |
907 | - (void)presetGame:(id)sender |
908 | { |
909 | game_params *params = [sender getPayload]; |
910 | |
911 | midend_set_params(me, params); |
912 | midend_new_game(me); |
913 | |
914 | [self resizeForNewGameParams]; |
ae2cfcdd |
915 | [self updateTypeMenuTick]; |
7e04cb48 |
916 | } |
917 | |
e1107f8b |
918 | - (void)startConfigureSheet:(int)which |
919 | { |
920 | NSButton *ok, *cancel; |
921 | int actw, acth, leftw, rightw, totalw, h, thish, y; |
922 | int k; |
923 | NSRect rect, tmprect; |
924 | const int SPACING = 16; |
925 | char *title; |
926 | config_item *i; |
927 | int cfg_controlsize; |
928 | NSTextField *tf; |
929 | NSButton *b; |
930 | NSPopUpButton *pb; |
931 | |
932 | assert(sheet == NULL); |
933 | |
934 | /* |
935 | * Every control we create here is going to have this size |
936 | * until we tell it to calculate a better one. |
937 | */ |
938 | tmprect = NSMakeRect(0, 0, 100, 50); |
939 | |
940 | /* |
941 | * Set up OK and Cancel buttons. (Actually, MacOS doesn't seem |
942 | * to be fond of generic OK and Cancel wording, so I'm going to |
943 | * rename them to something nicer.) |
944 | */ |
945 | actw = acth = 0; |
946 | |
947 | cancel = [[NSButton alloc] initWithFrame:tmprect]; |
948 | [cancel setBezelStyle:NSRoundedBezelStyle]; |
949 | [cancel setTitle:@"Abandon"]; |
950 | [cancel setTarget:self]; |
951 | [cancel setKeyEquivalent:@"\033"]; |
952 | [cancel setAction:@selector(sheetCancelButton:)]; |
953 | [cancel sizeToFit]; |
954 | rect = [cancel frame]; |
955 | if (actw < rect.size.width) actw = rect.size.width; |
956 | if (acth < rect.size.height) acth = rect.size.height; |
957 | |
958 | ok = [[NSButton alloc] initWithFrame:tmprect]; |
959 | [ok setBezelStyle:NSRoundedBezelStyle]; |
960 | [ok setTitle:@"Accept"]; |
961 | [ok setTarget:self]; |
962 | [ok setKeyEquivalent:@"\r"]; |
963 | [ok setAction:@selector(sheetOKButton:)]; |
964 | [ok sizeToFit]; |
965 | rect = [ok frame]; |
966 | if (actw < rect.size.width) actw = rect.size.width; |
967 | if (acth < rect.size.height) acth = rect.size.height; |
968 | |
969 | totalw = SPACING + 2 * actw; |
970 | h = 2 * SPACING + acth; |
971 | |
972 | /* |
973 | * Now fetch the midend config data and go through it creating |
974 | * controls. |
975 | */ |
976 | cfg = midend_get_config(me, which, &title); |
977 | sfree(title); /* FIXME: should we use this somehow? */ |
978 | cfg_which = which; |
979 | |
980 | cfg_ncontrols = cfg_controlsize = 0; |
981 | cfg_controls = NULL; |
982 | leftw = rightw = 0; |
983 | for (i = cfg; i->type != C_END; i++) { |
984 | if (cfg_controlsize < cfg_ncontrols + 5) { |
985 | cfg_controlsize = cfg_ncontrols + 32; |
986 | cfg_controls = sresize(cfg_controls, cfg_controlsize, NSView *); |
987 | } |
988 | |
989 | thish = 0; |
990 | |
991 | switch (i->type) { |
992 | case C_STRING: |
993 | /* |
994 | * Two NSTextFields, one being a label and the other |
995 | * being an edit box. |
996 | */ |
997 | |
998 | tf = [[NSTextField alloc] initWithFrame:tmprect]; |
999 | [tf setEditable:NO]; |
1000 | [tf setSelectable:NO]; |
1001 | [tf setBordered:NO]; |
1002 | [tf setDrawsBackground:NO]; |
1003 | [[tf cell] setTitle:[NSString stringWithCString:i->name]]; |
1004 | [tf sizeToFit]; |
1005 | rect = [tf frame]; |
1006 | if (thish < rect.size.height + 1) thish = rect.size.height + 1; |
1007 | if (leftw < rect.size.width + 1) leftw = rect.size.width + 1; |
1008 | cfg_controls[cfg_ncontrols++] = tf; |
1009 | |
e1107f8b |
1010 | tf = [[NSTextField alloc] initWithFrame:tmprect]; |
1011 | [tf setEditable:YES]; |
1012 | [tf setSelectable:YES]; |
1013 | [tf setBordered:YES]; |
1014 | [[tf cell] setTitle:[NSString stringWithCString:i->sval]]; |
1015 | [tf sizeToFit]; |
1016 | rect = [tf frame]; |
6958e513 |
1017 | /* |
1018 | * We impose a minimum and maximum width on editable |
1019 | * NSTextFields. If we allow them to size themselves to |
1020 | * the contents of the text within them, then they will |
1021 | * look very silly if that text is only one or two |
1022 | * characters, and equally silly if it's an absolutely |
1023 | * enormous Rectangles or Pattern game ID! |
1024 | */ |
1025 | if (rect.size.width < 75) rect.size.width = 75; |
1026 | if (rect.size.width > 400) rect.size.width = 400; |
1027 | |
e1107f8b |
1028 | if (thish < rect.size.height + 1) thish = rect.size.height + 1; |
1029 | if (rightw < rect.size.width + 1) rightw = rect.size.width + 1; |
1030 | cfg_controls[cfg_ncontrols++] = tf; |
1031 | break; |
1032 | |
1033 | case C_BOOLEAN: |
1034 | /* |
1035 | * A checkbox is an NSButton with a type of |
1036 | * NSSwitchButton. |
1037 | */ |
1038 | b = [[NSButton alloc] initWithFrame:tmprect]; |
1039 | [b setBezelStyle:NSRoundedBezelStyle]; |
1040 | [b setButtonType:NSSwitchButton]; |
1041 | [b setTitle:[NSString stringWithCString:i->name]]; |
1042 | [b sizeToFit]; |
1043 | [b setState:(i->ival ? NSOnState : NSOffState)]; |
1044 | rect = [b frame]; |
1045 | if (totalw < rect.size.width + 1) totalw = rect.size.width + 1; |
1046 | if (thish < rect.size.height + 1) thish = rect.size.height + 1; |
1047 | cfg_controls[cfg_ncontrols++] = b; |
1048 | break; |
1049 | |
1050 | case C_CHOICES: |
1051 | /* |
1052 | * A pop-up menu control is an NSPopUpButton, which |
1053 | * takes an embedded NSMenu. We also need an |
1054 | * NSTextField to act as a label. |
1055 | */ |
1056 | |
1057 | tf = [[NSTextField alloc] initWithFrame:tmprect]; |
1058 | [tf setEditable:NO]; |
1059 | [tf setSelectable:NO]; |
1060 | [tf setBordered:NO]; |
1061 | [tf setDrawsBackground:NO]; |
1062 | [[tf cell] setTitle:[NSString stringWithCString:i->name]]; |
1063 | [tf sizeToFit]; |
1064 | rect = [tf frame]; |
1065 | if (thish < rect.size.height + 1) thish = rect.size.height + 1; |
1066 | if (leftw < rect.size.width + 1) leftw = rect.size.width + 1; |
1067 | cfg_controls[cfg_ncontrols++] = tf; |
1068 | |
1069 | pb = [[NSPopUpButton alloc] initWithFrame:tmprect pullsDown:NO]; |
1070 | [pb setBezelStyle:NSRoundedBezelStyle]; |
1071 | { |
1072 | char c, *p; |
1073 | |
1074 | p = i->sval; |
1075 | c = *p++; |
1076 | while (*p) { |
1077 | char *q; |
1078 | |
1079 | q = p; |
1080 | while (*p && *p != c) p++; |
1081 | |
1082 | [pb addItemWithTitle:[NSString stringWithCString:q |
1083 | length:p-q]]; |
1084 | |
1085 | if (*p) p++; |
1086 | } |
1087 | } |
1088 | [pb selectItemAtIndex:i->ival]; |
1089 | [pb sizeToFit]; |
1090 | |
1091 | rect = [pb frame]; |
1092 | if (rightw < rect.size.width + 1) rightw = rect.size.width + 1; |
1093 | if (thish < rect.size.height + 1) thish = rect.size.height + 1; |
1094 | cfg_controls[cfg_ncontrols++] = pb; |
1095 | break; |
1096 | } |
1097 | |
1098 | h += SPACING + thish; |
1099 | } |
1100 | |
1101 | if (totalw < leftw + SPACING + rightw) |
1102 | totalw = leftw + SPACING + rightw; |
1103 | if (totalw > leftw + SPACING + rightw) { |
1104 | int excess = totalw - (leftw + SPACING + rightw); |
1105 | int leftexcess = leftw * excess / (leftw + rightw); |
1106 | int rightexcess = excess - leftexcess; |
1107 | leftw += leftexcess; |
1108 | rightw += rightexcess; |
1109 | } |
1110 | |
1111 | /* |
1112 | * Now go through the list again, setting the final position |
1113 | * for each control. |
1114 | */ |
1115 | k = 0; |
1116 | y = h; |
1117 | for (i = cfg; i->type != C_END; i++) { |
1118 | y -= SPACING; |
1119 | thish = 0; |
1120 | switch (i->type) { |
1121 | case C_STRING: |
1122 | case C_CHOICES: |
1123 | /* |
1124 | * These two are treated identically, since both expect |
1125 | * a control on the left and another on the right. |
1126 | */ |
1127 | rect = [cfg_controls[k] frame]; |
1128 | if (thish < rect.size.height + 1) |
1129 | thish = rect.size.height + 1; |
1130 | rect = [cfg_controls[k+1] frame]; |
1131 | if (thish < rect.size.height + 1) |
1132 | thish = rect.size.height + 1; |
1133 | rect = [cfg_controls[k] frame]; |
1134 | rect.origin.y = y - thish/2 - rect.size.height/2; |
1135 | rect.origin.x = SPACING; |
1136 | rect.size.width = leftw; |
1137 | [cfg_controls[k] setFrame:rect]; |
1138 | rect = [cfg_controls[k+1] frame]; |
1139 | rect.origin.y = y - thish/2 - rect.size.height/2; |
1140 | rect.origin.x = 2 * SPACING + leftw; |
1141 | rect.size.width = rightw; |
1142 | [cfg_controls[k+1] setFrame:rect]; |
1143 | k += 2; |
1144 | break; |
1145 | |
1146 | case C_BOOLEAN: |
1147 | rect = [cfg_controls[k] frame]; |
1148 | if (thish < rect.size.height + 1) |
1149 | thish = rect.size.height + 1; |
1150 | rect.origin.y = y - thish/2 - rect.size.height/2; |
1151 | rect.origin.x = SPACING; |
1152 | rect.size.width = totalw; |
1153 | [cfg_controls[k] setFrame:rect]; |
1154 | k++; |
1155 | break; |
1156 | } |
1157 | y -= thish; |
1158 | } |
1159 | |
1160 | assert(k == cfg_ncontrols); |
1161 | |
1162 | [cancel setFrame:NSMakeRect(SPACING+totalw/4-actw/2, SPACING, actw, acth)]; |
1163 | [ok setFrame:NSMakeRect(SPACING+3*totalw/4-actw/2, SPACING, actw, acth)]; |
1164 | |
1165 | sheet = [[NSWindow alloc] |
1166 | initWithContentRect:NSMakeRect(0,0,totalw + 2*SPACING,h) |
1167 | styleMask:NSTitledWindowMask | NSClosableWindowMask |
1168 | backing:NSBackingStoreBuffered |
1169 | defer:YES]; |
1170 | |
1171 | [[sheet contentView] addSubview:cancel]; |
1172 | [[sheet contentView] addSubview:ok]; |
1173 | |
1174 | for (k = 0; k < cfg_ncontrols; k++) |
1175 | [[sheet contentView] addSubview:cfg_controls[k]]; |
1176 | |
1177 | [NSApp beginSheet:sheet modalForWindow:self |
1178 | modalDelegate:nil didEndSelector:nil contextInfo:nil]; |
1179 | } |
1180 | |
1181 | - (void)specificGame:(id)sender |
1182 | { |
1185e3c5 |
1183 | [self startConfigureSheet:CFG_DESC]; |
1184 | } |
1185 | |
1186 | - (void)specificRandomGame:(id)sender |
1187 | { |
e1107f8b |
1188 | [self startConfigureSheet:CFG_SEED]; |
1189 | } |
1190 | |
1191 | - (void)customGameType:(id)sender |
1192 | { |
1193 | [self startConfigureSheet:CFG_SETTINGS]; |
1194 | } |
1195 | |
1196 | - (void)sheetEndWithStatus:(BOOL)update |
1197 | { |
1198 | assert(sheet != NULL); |
1199 | [NSApp endSheet:sheet]; |
1200 | [sheet orderOut:self]; |
1201 | sheet = NULL; |
1202 | if (update) { |
1203 | int k; |
1204 | config_item *i; |
1205 | char *error; |
1206 | |
1207 | k = 0; |
1208 | for (i = cfg; i->type != C_END; i++) { |
1209 | switch (i->type) { |
1210 | case C_STRING: |
1211 | sfree(i->sval); |
1212 | i->sval = dupstr([[[(id)cfg_controls[k+1] cell] |
eed62081 |
1213 | title] UTF8String]); |
e1107f8b |
1214 | k += 2; |
1215 | break; |
1216 | case C_BOOLEAN: |
1217 | i->ival = [(id)cfg_controls[k] state] == NSOnState; |
1218 | k++; |
1219 | break; |
1220 | case C_CHOICES: |
1221 | i->ival = [(id)cfg_controls[k+1] indexOfSelectedItem]; |
1222 | k += 2; |
1223 | break; |
1224 | } |
1225 | } |
1226 | |
1227 | error = midend_set_config(me, cfg_which, cfg); |
1228 | if (error) { |
1229 | NSAlert *alert = [[[NSAlert alloc] init] autorelease]; |
e7fabdfa |
1230 | [alert addButtonWithTitle:@"Bah"]; |
e1107f8b |
1231 | [alert setInformativeText:[NSString stringWithCString:error]]; |
1232 | [alert beginSheetModalForWindow:self modalDelegate:nil |
1233 | didEndSelector:nil contextInfo:nil]; |
1234 | } else { |
1235 | midend_new_game(me); |
1236 | [self resizeForNewGameParams]; |
ae2cfcdd |
1237 | [self updateTypeMenuTick]; |
e1107f8b |
1238 | } |
1239 | } |
1240 | sfree(cfg_controls); |
1241 | cfg_controls = NULL; |
1242 | } |
1243 | - (void)sheetOKButton:(id)sender |
1244 | { |
1245 | [self sheetEndWithStatus:YES]; |
1246 | } |
1247 | - (void)sheetCancelButton:(id)sender |
1248 | { |
1249 | [self sheetEndWithStatus:NO]; |
1250 | } |
1251 | |
48dcdd62 |
1252 | - (void)setStatusLine:(char *)text |
a5102461 |
1253 | { |
eb65b278 |
1254 | [[status cell] setTitle:[NSString stringWithCString:text]]; |
a5102461 |
1255 | } |
1256 | |
9494d866 |
1257 | @end |
1258 | |
1259 | /* |
1260 | * Drawing routines called by the midend. |
1261 | */ |
dafd6cf6 |
1262 | static void osx_draw_polygon(void *handle, int *coords, int npoints, |
1263 | int fillcolour, int outlinecolour) |
9494d866 |
1264 | { |
dafd6cf6 |
1265 | frontend *fe = (frontend *)handle; |
9494d866 |
1266 | NSBezierPath *path = [NSBezierPath bezierPath]; |
1267 | int i; |
1268 | |
1269 | [[NSGraphicsContext currentContext] setShouldAntialias:YES]; |
1270 | |
9494d866 |
1271 | for (i = 0; i < npoints; i++) { |
1272 | NSPoint p = { coords[i*2] + 0.5, coords[i*2+1] + 0.5 }; |
1273 | if (i == 0) |
1274 | [path moveToPoint:p]; |
1275 | else |
1276 | [path lineToPoint:p]; |
1277 | } |
1278 | |
1279 | [path closePath]; |
1280 | |
28b5987d |
1281 | if (fillcolour >= 0) { |
1282 | assert(fillcolour >= 0 && fillcolour < fe->ncolours); |
1283 | [fe->colours[fillcolour] set]; |
9494d866 |
1284 | [path fill]; |
28b5987d |
1285 | } |
1286 | |
1287 | assert(outlinecolour >= 0 && outlinecolour < fe->ncolours); |
1288 | [fe->colours[outlinecolour] set]; |
1289 | [path stroke]; |
9494d866 |
1290 | } |
dafd6cf6 |
1291 | static void osx_draw_circle(void *handle, int cx, int cy, int radius, |
1292 | int fillcolour, int outlinecolour) |
550efd7f |
1293 | { |
dafd6cf6 |
1294 | frontend *fe = (frontend *)handle; |
550efd7f |
1295 | NSBezierPath *path = [NSBezierPath bezierPath]; |
1296 | |
1297 | [[NSGraphicsContext currentContext] setShouldAntialias:YES]; |
1298 | |
550efd7f |
1299 | [path appendBezierPathWithArcWithCenter:NSMakePoint(cx + 0.5, cy + 0.5) |
1300 | radius:radius startAngle:0.0 endAngle:360.0]; |
1301 | |
1302 | [path closePath]; |
1303 | |
28b5987d |
1304 | if (fillcolour >= 0) { |
1305 | assert(fillcolour >= 0 && fillcolour < fe->ncolours); |
1306 | [fe->colours[fillcolour] set]; |
550efd7f |
1307 | [path fill]; |
28b5987d |
1308 | } |
1309 | |
1310 | assert(outlinecolour >= 0 && outlinecolour < fe->ncolours); |
1311 | [fe->colours[outlinecolour] set]; |
1312 | [path stroke]; |
550efd7f |
1313 | } |
dafd6cf6 |
1314 | static void osx_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour) |
9494d866 |
1315 | { |
dafd6cf6 |
1316 | frontend *fe = (frontend *)handle; |
9494d866 |
1317 | NSBezierPath *path = [NSBezierPath bezierPath]; |
1318 | NSPoint p1 = { x1 + 0.5, y1 + 0.5 }, p2 = { x2 + 0.5, y2 + 0.5 }; |
1319 | |
1320 | [[NSGraphicsContext currentContext] setShouldAntialias:NO]; |
1321 | |
1322 | assert(colour >= 0 && colour < fe->ncolours); |
1323 | [fe->colours[colour] set]; |
1324 | |
1325 | [path moveToPoint:p1]; |
1326 | [path lineToPoint:p2]; |
1327 | [path stroke]; |
1328 | } |
dafd6cf6 |
1329 | static void osx_draw_rect(void *handle, int x, int y, int w, int h, int colour) |
9494d866 |
1330 | { |
dafd6cf6 |
1331 | frontend *fe = (frontend *)handle; |
9494d866 |
1332 | NSRect r = { {x,y}, {w,h} }; |
dafd6cf6 |
1333 | |
9494d866 |
1334 | [[NSGraphicsContext currentContext] setShouldAntialias:NO]; |
1335 | |
1336 | assert(colour >= 0 && colour < fe->ncolours); |
1337 | [fe->colours[colour] set]; |
1338 | |
1339 | NSRectFill(r); |
1340 | } |
dafd6cf6 |
1341 | static void osx_draw_text(void *handle, int x, int y, int fonttype, |
1342 | int fontsize, int align, int colour, char *text) |
9494d866 |
1343 | { |
dafd6cf6 |
1344 | frontend *fe = (frontend *)handle; |
9494d866 |
1345 | NSString *string = [NSString stringWithCString:text]; |
1346 | NSDictionary *attr; |
1347 | NSFont *font; |
1348 | NSSize size; |
1349 | NSPoint point; |
1350 | |
1351 | [[NSGraphicsContext currentContext] setShouldAntialias:YES]; |
1352 | |
1353 | assert(colour >= 0 && colour < fe->ncolours); |
1354 | |
1355 | if (fonttype == FONT_FIXED) |
1356 | font = [NSFont userFixedPitchFontOfSize:fontsize]; |
1357 | else |
1358 | font = [NSFont userFontOfSize:fontsize]; |
1359 | |
1360 | attr = [NSDictionary dictionaryWithObjectsAndKeys: |
1361 | fe->colours[colour], NSForegroundColorAttributeName, |
1362 | font, NSFontAttributeName, nil]; |
1363 | |
1364 | point.x = x; |
1365 | point.y = y; |
1366 | |
1367 | size = [string sizeWithAttributes:attr]; |
1368 | if (align & ALIGN_HRIGHT) |
1369 | point.x -= size.width; |
1370 | else if (align & ALIGN_HCENTRE) |
1371 | point.x -= size.width / 2; |
1372 | if (align & ALIGN_VCENTRE) |
1373 | point.y -= size.height / 2; |
86e60e3d |
1374 | else |
1375 | point.y -= size.height; |
9494d866 |
1376 | |
1377 | [string drawAtPoint:point withAttributes:attr]; |
1378 | } |
3161048d |
1379 | struct blitter { |
1380 | int w, h; |
1381 | int x, y; |
1382 | NSImage *img; |
1383 | }; |
dafd6cf6 |
1384 | static blitter *osx_blitter_new(void *handle, int w, int h) |
3161048d |
1385 | { |
1386 | blitter *bl = snew(blitter); |
1387 | bl->x = bl->y = -1; |
1388 | bl->w = w; |
1389 | bl->h = h; |
1390 | bl->img = [[NSImage alloc] initWithSize:NSMakeSize(w, h)]; |
1391 | [bl->img setFlipped:YES]; |
1392 | return bl; |
1393 | } |
dafd6cf6 |
1394 | static void osx_blitter_free(void *handle, blitter *bl) |
3161048d |
1395 | { |
1396 | [bl->img release]; |
1397 | sfree(bl); |
1398 | } |
dafd6cf6 |
1399 | static void osx_blitter_save(void *handle, blitter *bl, int x, int y) |
3161048d |
1400 | { |
dafd6cf6 |
1401 | frontend *fe = (frontend *)handle; |
3161048d |
1402 | [fe->image unlockFocus]; |
1403 | [bl->img lockFocus]; |
1404 | [fe->image drawInRect:NSMakeRect(0, 0, bl->w, bl->h) |
1405 | fromRect:NSMakeRect(x, y, bl->w, bl->h) |
1406 | operation:NSCompositeCopy fraction:1.0]; |
1407 | [bl->img unlockFocus]; |
1408 | [fe->image lockFocus]; |
1409 | bl->x = x; |
1410 | bl->y = y; |
1411 | } |
dafd6cf6 |
1412 | static void osx_blitter_load(void *handle, blitter *bl, int x, int y) |
3161048d |
1413 | { |
c3b4e35a |
1414 | /* frontend *fe = (frontend *)handle; */ |
3161048d |
1415 | if (x == BLITTER_FROMSAVED && y == BLITTER_FROMSAVED) { |
1416 | x = bl->x; |
1417 | y = bl->y; |
1418 | } |
1419 | [bl->img drawInRect:NSMakeRect(x, y, bl->w, bl->h) |
1420 | fromRect:NSMakeRect(0, 0, bl->w, bl->h) |
1421 | operation:NSCompositeCopy fraction:1.0]; |
1422 | } |
dafd6cf6 |
1423 | static void osx_draw_update(void *handle, int x, int y, int w, int h) |
9494d866 |
1424 | { |
dafd6cf6 |
1425 | frontend *fe = (frontend *)handle; |
364722d2 |
1426 | [fe->view setNeedsDisplayInRect:NSMakeRect(x,y,w,h)]; |
9494d866 |
1427 | } |
dafd6cf6 |
1428 | static void osx_clip(void *handle, int x, int y, int w, int h) |
9494d866 |
1429 | { |
dafd6cf6 |
1430 | frontend *fe = (frontend *)handle; |
9494d866 |
1431 | NSRect r = { {x,y}, {w,h} }; |
dafd6cf6 |
1432 | |
9494d866 |
1433 | if (!fe->clipped) |
1434 | [[NSGraphicsContext currentContext] saveGraphicsState]; |
1435 | [NSBezierPath clipRect:r]; |
1436 | fe->clipped = TRUE; |
1437 | } |
dafd6cf6 |
1438 | static void osx_unclip(void *handle) |
9494d866 |
1439 | { |
dafd6cf6 |
1440 | frontend *fe = (frontend *)handle; |
9494d866 |
1441 | if (fe->clipped) |
1442 | [[NSGraphicsContext currentContext] restoreGraphicsState]; |
1443 | fe->clipped = FALSE; |
1444 | } |
dafd6cf6 |
1445 | static void osx_start_draw(void *handle) |
9494d866 |
1446 | { |
dafd6cf6 |
1447 | frontend *fe = (frontend *)handle; |
9494d866 |
1448 | [fe->image lockFocus]; |
1449 | fe->clipped = FALSE; |
1450 | } |
dafd6cf6 |
1451 | static void osx_end_draw(void *handle) |
9494d866 |
1452 | { |
dafd6cf6 |
1453 | frontend *fe = (frontend *)handle; |
9494d866 |
1454 | [fe->image unlockFocus]; |
9494d866 |
1455 | } |
dafd6cf6 |
1456 | static void osx_status_bar(void *handle, char *text) |
1457 | { |
1458 | frontend *fe = (frontend *)handle; |
1459 | [fe->window setStatusLine:text]; |
1460 | } |
1461 | |
1462 | const struct drawing_api osx_drawing = { |
1463 | osx_draw_text, |
1464 | osx_draw_rect, |
1465 | osx_draw_line, |
c3b4e35a |
1466 | osx_draw_polygon, |
dafd6cf6 |
1467 | osx_draw_circle, |
1468 | osx_draw_update, |
1469 | osx_clip, |
1470 | osx_unclip, |
1471 | osx_start_draw, |
1472 | osx_end_draw, |
1473 | osx_status_bar, |
1474 | osx_blitter_new, |
1475 | osx_blitter_free, |
1476 | osx_blitter_save, |
1477 | osx_blitter_load, |
1478 | NULL, NULL, NULL, NULL, NULL, NULL, /* {begin,end}_{doc,page,puzzle} */ |
1479 | NULL, /* line_width */ |
1480 | }; |
9494d866 |
1481 | |
1482 | void deactivate_timer(frontend *fe) |
1483 | { |
1484 | [fe->window deactivateTimer]; |
1485 | } |
1486 | void activate_timer(frontend *fe) |
1487 | { |
1488 | [fe->window activateTimer]; |
1489 | } |
1490 | |
1491 | /* ---------------------------------------------------------------------- |
9494d866 |
1492 | * AppController: the object which receives the messages from all |
1493 | * menu selections that aren't standard OS X functions. |
1494 | */ |
1495 | @interface AppController : NSObject |
1496 | { |
1497 | } |
bacaa96e |
1498 | - (void)newGameWindow:(id)sender; |
97098757 |
1499 | - (void)about:(id)sender; |
9494d866 |
1500 | @end |
1501 | |
1502 | @implementation AppController |
1503 | |
bacaa96e |
1504 | - (void)newGameWindow:(id)sender |
9494d866 |
1505 | { |
7e04cb48 |
1506 | const game *g = [sender getPayload]; |
9494d866 |
1507 | id win; |
1508 | |
1509 | win = [[GameWindow alloc] initWithGame:g]; |
1510 | [win makeKeyAndOrderFront:self]; |
1511 | } |
1512 | |
97098757 |
1513 | - (void)about:(id)sender |
1514 | { |
1515 | id win; |
1516 | |
1517 | win = [[AboutBox alloc] init]; |
1518 | [win makeKeyAndOrderFront:self]; |
1519 | } |
1520 | |
3041164c |
1521 | - (NSMenu *)applicationDockMenu:(NSApplication *)sender |
1522 | { |
1523 | NSMenu *menu = newmenu("Dock Menu"); |
1524 | { |
1525 | int i; |
1526 | |
1527 | for (i = 0; i < gamecount; i++) { |
1528 | id item = |
1529 | initnewitem([DataMenuItem allocWithZone:[NSMenu menuZone]], |
1530 | menu, gamelist[i]->name, "", self, |
bacaa96e |
1531 | @selector(newGameWindow:)); |
3041164c |
1532 | [item setPayload:(void *)gamelist[i]]; |
1533 | } |
1534 | } |
1535 | return menu; |
1536 | } |
1537 | |
9494d866 |
1538 | @end |
1539 | |
1540 | /* ---------------------------------------------------------------------- |
1541 | * Main program. Constructs the menus and runs the application. |
1542 | */ |
1543 | int main(int argc, char **argv) |
1544 | { |
1545 | NSAutoreleasePool *pool; |
1546 | NSMenu *menu; |
1547 | NSMenuItem *item; |
1548 | AppController *controller; |
a96edf8a |
1549 | NSImage *icon; |
9494d866 |
1550 | |
1551 | pool = [[NSAutoreleasePool alloc] init]; |
a96edf8a |
1552 | |
1553 | icon = [NSImage imageNamed:@"NSApplicationIcon"]; |
9494d866 |
1554 | [NSApplication sharedApplication]; |
a96edf8a |
1555 | [NSApp setApplicationIconImage:icon]; |
9494d866 |
1556 | |
1557 | controller = [[[AppController alloc] init] autorelease]; |
3041164c |
1558 | [NSApp setDelegate:controller]; |
9494d866 |
1559 | |
1560 | [NSApp setMainMenu: newmenu("Main Menu")]; |
1561 | |
1562 | menu = newsubmenu([NSApp mainMenu], "Apple Menu"); |
97098757 |
1563 | item = newitem(menu, "About Puzzles", "", NULL, @selector(about:)); |
1564 | [menu addItem:[NSMenuItem separatorItem]]; |
9494d866 |
1565 | [NSApp setServicesMenu:newsubmenu(menu, "Services")]; |
1566 | [menu addItem:[NSMenuItem separatorItem]]; |
1567 | item = newitem(menu, "Hide Puzzles", "h", NSApp, @selector(hide:)); |
1568 | item = newitem(menu, "Hide Others", "o-h", NSApp, @selector(hideOtherApplications:)); |
1569 | item = newitem(menu, "Show All", "", NSApp, @selector(unhideAllApplications:)); |
1570 | [menu addItem:[NSMenuItem separatorItem]]; |
1571 | item = newitem(menu, "Quit", "q", NSApp, @selector(terminate:)); |
1572 | [NSApp setAppleMenu: menu]; |
1573 | |
bacaa96e |
1574 | menu = newsubmenu([NSApp mainMenu], "File"); |
668be019 |
1575 | item = newitem(menu, "Open", "o", NULL, @selector(loadSavedGame:)); |
1576 | item = newitem(menu, "Save As", "s", NULL, @selector(saveGame:)); |
bacaa96e |
1577 | item = newitem(menu, "New Game", "n", NULL, @selector(newGame:)); |
1578 | item = newitem(menu, "Restart Game", "r", NULL, @selector(restartGame:)); |
1579 | item = newitem(menu, "Specific Game", "", NULL, @selector(specificGame:)); |
1185e3c5 |
1580 | item = newitem(menu, "Specific Random Seed", "", NULL, |
1581 | @selector(specificRandomGame:)); |
bacaa96e |
1582 | [menu addItem:[NSMenuItem separatorItem]]; |
9494d866 |
1583 | { |
bacaa96e |
1584 | NSMenu *submenu = newsubmenu(menu, "New Window"); |
9494d866 |
1585 | int i; |
1586 | |
1587 | for (i = 0; i < gamecount; i++) { |
1588 | id item = |
7e04cb48 |
1589 | initnewitem([DataMenuItem allocWithZone:[NSMenu menuZone]], |
bacaa96e |
1590 | submenu, gamelist[i]->name, "", controller, |
1591 | @selector(newGameWindow:)); |
7e04cb48 |
1592 | [item setPayload:(void *)gamelist[i]]; |
9494d866 |
1593 | } |
1594 | } |
00461804 |
1595 | [menu addItem:[NSMenuItem separatorItem]]; |
bacaa96e |
1596 | item = newitem(menu, "Close", "w", NULL, @selector(performClose:)); |
1597 | |
1598 | menu = newsubmenu([NSApp mainMenu], "Edit"); |
00461804 |
1599 | item = newitem(menu, "Undo", "z", NULL, @selector(undoMove:)); |
1600 | item = newitem(menu, "Redo", "S-z", NULL, @selector(redoMove:)); |
1601 | [menu addItem:[NSMenuItem separatorItem]]; |
8b738d61 |
1602 | item = newitem(menu, "Cut", "x", NULL, @selector(cut:)); |
9b4b03d3 |
1603 | item = newitem(menu, "Copy", "c", NULL, @selector(copy:)); |
8b738d61 |
1604 | item = newitem(menu, "Paste", "v", NULL, @selector(paste:)); |
2ac6d24e |
1605 | [menu addItem:[NSMenuItem separatorItem]]; |
1606 | item = newitem(menu, "Solve", "S-s", NULL, @selector(solveGame:)); |
00461804 |
1607 | |
1608 | menu = newsubmenu([NSApp mainMenu], "Type"); |
7e04cb48 |
1609 | typemenu = menu; |
00461804 |
1610 | item = newitem(menu, "Custom", "", NULL, @selector(customGameType:)); |
1611 | |
1612 | menu = newsubmenu([NSApp mainMenu], "Window"); |
9494d866 |
1613 | [NSApp setWindowsMenu: menu]; |
00461804 |
1614 | item = newitem(menu, "Minimise Window", "m", NULL, @selector(performMiniaturize:)); |
9494d866 |
1615 | |
fccfd04d |
1616 | menu = newsubmenu([NSApp mainMenu], "Help"); |
171ee031 |
1617 | item = newitem(menu, "Puzzles Help", "?", NSApp, @selector(showHelp:)); |
fccfd04d |
1618 | |
9494d866 |
1619 | [NSApp run]; |
1620 | [pool release]; |
eed62081 |
1621 | |
1622 | return 0; |
9494d866 |
1623 | } |