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