* nsmenu.m (ns_update_menubar): Exchange place of argument 2 and 3 to
[bpt/emacs.git] / src / nsmenu.m
1 /* NeXT/Open/GNUstep and MacOSX Cocoa menu and toolbar module.
2 Copyright (C) 2007-2012 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
18
19 /*
20 By Adrian Robert, based on code from original nsmenu.m (Carl Edman,
21 Christian Limpach, Scott Bender, Christophe de Dinechin) and code in the
22 Carbon version by Yamamoto Mitsuharu. */
23
24 /* This should be the first include, as it may set up #defines affecting
25 interpretation of even the system includes. */
26 #include <config.h>
27 #include <setjmp.h>
28
29 #include "lisp.h"
30 #include "window.h"
31 #include "character.h"
32 #include "buffer.h"
33 #include "keymap.h"
34 #include "coding.h"
35 #include "commands.h"
36 #include "blockinput.h"
37 #include "nsterm.h"
38 #include "termhooks.h"
39 #include "keyboard.h"
40 #include "menu.h"
41
42 #define NSMENUPROFILE 0
43
44 #if NSMENUPROFILE
45 #include <sys/timeb.h>
46 #include <sys/types.h>
47 #endif
48
49 #define MenuStagger 10.0
50
51 #if 0
52 int menu_trace_num = 0;
53 #define NSTRACE(x) fprintf (stderr, "%s:%d: [%d] " #x "\n", \
54 __FILE__, __LINE__, ++menu_trace_num)
55 #else
56 #define NSTRACE(x)
57 #endif
58
59 #if 0
60 /* Include lisp -> C common menu parsing code */
61 #define ENCODE_MENU_STRING(str) ENCODE_UTF_8 (str)
62 #include "nsmenu_common.c"
63 #endif
64
65 extern Lisp_Object Qundefined, Qmenu_enable, Qmenu_bar_update_hook;
66 extern Lisp_Object QCtoggle, QCradio;
67
68 Lisp_Object Qdebug_on_next_call;
69 extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
70
71 extern long context_menu_value;
72 EmacsMenu *mainMenu, *svcsMenu, *dockMenu;
73
74 /* Nonzero means a menu is currently active. */
75 static int popup_activated_flag;
76 static NSModalSession popupSession;
77
78 /* Nonzero means we are tracking and updating menus. */
79 static int trackingMenu;
80
81
82 /* NOTE: toolbar implementation is at end,
83 following complete menu implementation. */
84
85
86 /* ==========================================================================
87
88 Menu: Externally-called functions
89
90 ========================================================================== */
91
92
93 /* FIXME: not currently used, but should normalize with other terms. */
94 void
95 x_activate_menubar (struct frame *f)
96 {
97 fprintf (stderr, "XXX: Received x_activate_menubar event.\n");
98 }
99
100
101 /* Supposed to discard menubar and free storage. Since we share the
102 menubar among frames and update its context for the focused window,
103 there is nothing to do here. */
104 void
105 free_frame_menubar (struct frame *f)
106 {
107 return;
108 }
109
110
111 int
112 popup_activated (void)
113 {
114 return popup_activated_flag;
115 }
116
117
118 /* --------------------------------------------------------------------------
119 Update menubar. Three cases:
120 1) deep_p = 0, submenu = nil: Fresh switch onto a frame -- either set up
121 just top-level menu strings (OS X), or goto case (2) (GNUstep).
122 2) deep_p = 1, submenu = nil: Recompute all submenus.
123 3) deep_p = 1, submenu = non-nil: Update contents of a single submenu.
124 -------------------------------------------------------------------------- */
125 void
126 ns_update_menubar (struct frame *f, int deep_p, EmacsMenu *submenu)
127 {
128 NSAutoreleasePool *pool;
129 id menu = [NSApp mainMenu];
130 static EmacsMenu *last_submenu = nil;
131 BOOL needsSet = NO;
132 const char *submenuTitle = [[submenu title] UTF8String];
133 extern int waiting_for_input;
134 int owfi;
135 Lisp_Object items;
136 widget_value *wv, *first_wv, *prev_wv = 0;
137 int i;
138
139 #if NSMENUPROFILE
140 struct timeb tb;
141 long t;
142 #endif
143
144 NSTRACE (set_frame_menubar);
145
146 if (f != SELECTED_FRAME ())
147 return;
148 XSETFRAME (Vmenu_updating_frame, f);
149 /*fprintf (stderr, "ns_update_menubar: frame: %p\tdeep: %d\tsub: %p\n", f, deep_p, submenu); */
150
151 BLOCK_INPUT;
152 pool = [[NSAutoreleasePool alloc] init];
153
154 /* Menu may have been created automatically; if so, discard it. */
155 if ([menu isKindOfClass: [EmacsMenu class]] == NO)
156 {
157 [menu release];
158 menu = nil;
159 }
160
161 if (menu == nil)
162 {
163 menu = [[EmacsMenu alloc] initWithTitle: ns_app_name];
164 needsSet = YES;
165 }
166 else
167 { /* close up anything on there */
168 id attMenu = [menu attachedMenu];
169 if (attMenu != nil)
170 [attMenu close];
171 }
172
173 #if NSMENUPROFILE
174 ftime (&tb);
175 t = -(1000*tb.time+tb.millitm);
176 #endif
177
178 #ifdef NS_IMPL_GNUSTEP
179 deep_p = 1; /* until GNUstep NSMenu implements the Panther delegation model */
180 #endif
181
182 if (deep_p)
183 {
184 /* Fully parse one or more of the submenus. */
185 int n = 0;
186 int *submenu_start, *submenu_end;
187 int *submenu_top_level_items, *submenu_n_panes;
188 struct buffer *prev = current_buffer;
189 Lisp_Object buffer;
190 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
191 int previous_menu_items_used = f->menu_bar_items_used;
192 Lisp_Object *previous_items
193 = alloca (previous_menu_items_used * sizeof *previous_items);
194
195 /* lisp preliminaries */
196 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
197 specbind (Qinhibit_quit, Qt);
198 specbind (Qdebug_on_next_call, Qnil);
199 record_unwind_save_match_data ();
200 if (NILP (Voverriding_local_map_menu_flag))
201 {
202 specbind (Qoverriding_terminal_local_map, Qnil);
203 specbind (Qoverriding_local_map, Qnil);
204 }
205 set_buffer_internal_1 (XBUFFER (buffer));
206
207 /* TODO: for some reason this is not needed in other terms,
208 but some menu updates call Info-extract-pointer which causes
209 abort-on-error if waiting-for-input. Needs further investigation. */
210 owfi = waiting_for_input;
211 waiting_for_input = 0;
212
213 /* lucid hook and possible reset */
214 safe_run_hooks (Qactivate_menubar_hook);
215 if (! NILP (Vlucid_menu_bar_dirty_flag))
216 call0 (Qrecompute_lucid_menubar);
217 safe_run_hooks (Qmenu_bar_update_hook);
218 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
219
220 /* Now ready to go */
221 items = FRAME_MENU_BAR_ITEMS (f);
222
223 /* Save the frame's previous menu bar contents data */
224 if (previous_menu_items_used)
225 memcpy (previous_items, &AREF (f->menu_bar_vector, 0),
226 previous_menu_items_used * sizeof (Lisp_Object));
227
228 /* parse stage 1: extract from lisp */
229 save_menu_items ();
230
231 menu_items = f->menu_bar_vector;
232 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
233 submenu_start = alloca (ASIZE (items) * sizeof *submenu_start);
234 submenu_end = alloca (ASIZE (items) * sizeof *submenu_end);
235 submenu_n_panes = alloca (ASIZE (items) * sizeof *submenu_n_panes);
236 submenu_top_level_items = alloca (ASIZE (items)
237 * sizeof *submenu_top_level_items);
238 init_menu_items ();
239 for (i = 0; i < ASIZE (items); i += 4)
240 {
241 Lisp_Object key, string, maps;
242
243 key = AREF (items, i);
244 string = AREF (items, i + 1);
245 maps = AREF (items, i + 2);
246 if (NILP (string))
247 break;
248
249 /* FIXME: we'd like to only parse the needed submenu, but this
250 was causing crashes in the _common parsing code.. need to make
251 sure proper initialization done.. */
252 /* if (submenu && strcmp (submenuTitle, SDATA (string)))
253 continue; */
254
255 submenu_start[i] = menu_items_used;
256
257 menu_items_n_panes = 0;
258 submenu_top_level_items[i] = parse_single_submenu (key, string, maps);
259 submenu_n_panes[i] = menu_items_n_panes;
260 submenu_end[i] = menu_items_used;
261 n++;
262 }
263
264 finish_menu_items ();
265 waiting_for_input = owfi;
266
267
268 if (submenu && n == 0)
269 {
270 /* should have found a menu for this one but didn't */
271 fprintf (stderr, "ERROR: did not find lisp menu for submenu '%s'.\n",
272 submenuTitle);
273 discard_menu_items ();
274 unbind_to (specpdl_count, Qnil);
275 [pool release];
276 UNBLOCK_INPUT;
277 return;
278 }
279
280 /* parse stage 2: insert into lucid 'widget_value' structures
281 [comments in other terms say not to evaluate lisp code here] */
282 wv = xmalloc_widget_value ();
283 wv->name = "menubar";
284 wv->value = 0;
285 wv->enabled = 1;
286 wv->button_type = BUTTON_TYPE_NONE;
287 wv->help = Qnil;
288 first_wv = wv;
289
290 for (i = 0; i < 4*n; i += 4)
291 {
292 menu_items_n_panes = submenu_n_panes[i];
293 wv = digest_single_submenu (submenu_start[i], submenu_end[i],
294 submenu_top_level_items[i]);
295 if (prev_wv)
296 prev_wv->next = wv;
297 else
298 first_wv->contents = wv;
299 /* Don't set wv->name here; GC during the loop might relocate it. */
300 wv->enabled = 1;
301 wv->button_type = BUTTON_TYPE_NONE;
302 prev_wv = wv;
303 }
304
305 set_buffer_internal_1 (prev);
306
307 /* Compare the new menu items with previous, and leave off if no change */
308 /* FIXME: following other terms here, but seems like this should be
309 done before parse stage 2 above, since its results aren't used */
310 if (previous_menu_items_used
311 && (!submenu || (submenu && submenu == last_submenu))
312 && menu_items_used == previous_menu_items_used)
313 {
314 for (i = 0; i < previous_menu_items_used; i++)
315 /* FIXME: this ALWAYS fails on Buffers menu items.. something
316 about their strings causes them to change every time, so we
317 double-check failures */
318 if (!EQ (previous_items[i], AREF (menu_items, i)))
319 if (!(STRINGP (previous_items[i])
320 && STRINGP (AREF (menu_items, i))
321 && !strcmp (SDATA (previous_items[i]),
322 SDATA (AREF (menu_items, i)))))
323 break;
324 if (i == previous_menu_items_used)
325 {
326 /* No change.. */
327
328 #if NSMENUPROFILE
329 ftime (&tb);
330 t += 1000*tb.time+tb.millitm;
331 fprintf (stderr, "NO CHANGE! CUTTING OUT after %ld msec.\n", t);
332 #endif
333
334 free_menubar_widget_value_tree (first_wv);
335 discard_menu_items ();
336 unbind_to (specpdl_count, Qnil);
337 [pool release];
338 UNBLOCK_INPUT;
339 return;
340 }
341 }
342 /* The menu items are different, so store them in the frame */
343 /* FIXME: this is not correct for single-submenu case */
344 f->menu_bar_vector = menu_items;
345 f->menu_bar_items_used = menu_items_used;
346
347 /* Calls restore_menu_items, etc., as they were outside */
348 unbind_to (specpdl_count, Qnil);
349
350 /* Parse stage 2a: now GC cannot happen during the lifetime of the
351 widget_value, so it's safe to store data from a Lisp_String */
352 wv = first_wv->contents;
353 for (i = 0; i < ASIZE (items); i += 4)
354 {
355 Lisp_Object string;
356 string = AREF (items, i + 1);
357 if (NILP (string))
358 break;
359 /* if (submenu && strcmp (submenuTitle, SDATA (string)))
360 continue; */
361
362 wv->name = SSDATA (string);
363 update_submenu_strings (wv->contents);
364 wv = wv->next;
365 }
366
367 /* Now, update the NS menu; if we have a submenu, use that, otherwise
368 create a new menu for each sub and fill it. */
369 if (submenu)
370 {
371 for (wv = first_wv->contents; wv; wv = wv->next)
372 {
373 if (!strcmp (submenuTitle, wv->name))
374 {
375 [submenu fillWithWidgetValue: wv->contents];
376 last_submenu = submenu;
377 break;
378 }
379 }
380 }
381 else
382 {
383 [menu fillWithWidgetValue: first_wv->contents];
384 }
385
386 }
387 else
388 {
389 static int n_previous_strings = 0;
390 static char previous_strings[100][10];
391 static struct frame *last_f = NULL;
392 int n;
393 Lisp_Object string;
394
395 wv = xmalloc_widget_value ();
396 wv->name = "menubar";
397 wv->value = 0;
398 wv->enabled = 1;
399 wv->button_type = BUTTON_TYPE_NONE;
400 wv->help = Qnil;
401 first_wv = wv;
402
403 /* Make widget-value tree w/ just the top level menu bar strings */
404 items = FRAME_MENU_BAR_ITEMS (f);
405 if (NILP (items))
406 {
407 free_menubar_widget_value_tree (first_wv);
408 [pool release];
409 UNBLOCK_INPUT;
410 return;
411 }
412
413
414 /* check if no change.. this mechanism is a bit rough, but ready */
415 n = ASIZE (items) / 4;
416 if (f == last_f && n_previous_strings == n)
417 {
418 for (i = 0; i<n; i++)
419 {
420 string = AREF (items, 4*i+1);
421
422 if (EQ (string, make_number (0))) // FIXME: Why??? --Stef
423 continue;
424 if (NILP (string))
425 if (previous_strings[i][0])
426 break;
427 else
428 continue;
429 if (memcmp (previous_strings[i], SDATA (string),
430 min (10, SBYTES (string) + 1)))
431 break;
432 }
433
434 if (i == n)
435 {
436 free_menubar_widget_value_tree (first_wv);
437 [pool release];
438 UNBLOCK_INPUT;
439 return;
440 }
441 }
442
443 [menu clear];
444 for (i = 0; i < ASIZE (items); i += 4)
445 {
446 string = AREF (items, i + 1);
447 if (NILP (string))
448 break;
449
450 if (n < 100)
451 memcpy (previous_strings[i/4], SDATA (string),
452 min (10, SBYTES (string) + 1));
453
454 wv = xmalloc_widget_value ();
455 wv->name = SSDATA (string);
456 wv->value = 0;
457 wv->enabled = 1;
458 wv->button_type = BUTTON_TYPE_NONE;
459 wv->help = Qnil;
460 wv->call_data = (void *) (intptr_t) (-1);
461
462 #ifdef NS_IMPL_COCOA
463 /* we'll update the real copy under app menu when time comes */
464 if (!strcmp ("Services", wv->name))
465 {
466 /* but we need to make sure it will update on demand */
467 [svcsMenu setFrame: f];
468 }
469 else
470 #endif
471 [menu addSubmenuWithTitle: wv->name forFrame: f];
472
473 if (prev_wv)
474 prev_wv->next = wv;
475 else
476 first_wv->contents = wv;
477 prev_wv = wv;
478 }
479
480 last_f = f;
481 if (n < 100)
482 n_previous_strings = n;
483 else
484 n_previous_strings = 0;
485
486 }
487 free_menubar_widget_value_tree (first_wv);
488
489
490 #if NSMENUPROFILE
491 ftime (&tb);
492 t += 1000*tb.time+tb.millitm;
493 fprintf (stderr, "Menu update took %ld msec.\n", t);
494 #endif
495
496 /* set main menu */
497 if (needsSet)
498 [NSApp setMainMenu: menu];
499
500 [pool release];
501 UNBLOCK_INPUT;
502
503 }
504
505
506 /* Main emacs core entry point for menubar menus: called to indicate that the
507 frame's menus have changed, and the *step representation should be updated
508 from Lisp. */
509 void
510 set_frame_menubar (struct frame *f, int first_time, int deep_p)
511 {
512 ns_update_menubar (f, deep_p, nil);
513 }
514
515
516 /* ==========================================================================
517
518 Menu: class implementation
519
520 ========================================================================== */
521
522
523 /* Menu that can define itself from Emacs "widget_value"s and will lazily
524 update itself when user clicked. Based on Carbon/AppKit implementation
525 by Yamamoto Mitsuharu. */
526 @implementation EmacsMenu
527
528 /* override designated initializer */
529 - initWithTitle: (NSString *)title
530 {
531 if (self = [super initWithTitle: title])
532 [self setAutoenablesItems: NO];
533 return self;
534 }
535
536
537 /* used for top-level */
538 - initWithTitle: (NSString *)title frame: (struct frame *)f
539 {
540 [self initWithTitle: title];
541 frame = f;
542 #ifdef NS_IMPL_COCOA
543 [self setDelegate: self];
544 #endif
545 return self;
546 }
547
548
549 - (void)setFrame: (struct frame *)f
550 {
551 frame = f;
552 }
553
554 #ifdef NS_IMPL_COCOA
555 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
556 extern NSString *NSMenuDidBeginTrackingNotification;
557 #endif
558 #endif
559
560 #ifdef NS_IMPL_COCOA
561 -(void)trackingNotification:(NSNotification *)notification
562 {
563 /* Update menu in menuNeedsUpdate only while tracking menus. */
564 trackingMenu = ([notification name] == NSMenuDidBeginTrackingNotification
565 ? 1 : 0);
566 }
567 #endif
568
569 /* delegate method called when a submenu is being opened: run a 'deep' call
570 to set_frame_menubar */
571 - (void)menuNeedsUpdate: (NSMenu *)menu
572 {
573 if (!FRAME_LIVE_P (frame))
574 return;
575
576 /* Cocoa/Carbon will request update on every keystroke
577 via IsMenuKeyEvent -> CheckMenusForKeyEvent. These are not needed
578 since key equivalents are handled through emacs.
579 On Leopard, even keystroke events generate SystemDefined event.
580 Third-party applications that enhance mouse / trackpad
581 interaction, or also VNC/Remote Desktop will send events
582 of type AppDefined rather than SysDefined.
583 Menus will fail to show up if they haven't been initialized.
584 AppDefined events may lack timing data.
585
586 Thus, we rely on the didBeginTrackingNotification notification
587 as above to indicate the need for updates.
588 From 10.6 on, we could also use -[NSMenu propertiesToUpdate]: In the
589 key press case, NSMenuPropertyItemImage (e.g.) won't be set.
590 */
591 if (trackingMenu == 0
592 /* Also, don't try this if from an event picked up asynchronously,
593 as lots of lisp evaluation happens in ns_update_menubar. */
594 || handling_signal != 0)
595 return;
596 /*fprintf (stderr, "Updating menu '%s'\n", [[self title] UTF8String]); NSLog (@"%@\n", event); */
597 ns_update_menubar (frame, 1, self);
598 }
599
600
601 - (BOOL)performKeyEquivalent: (NSEvent *)theEvent
602 {
603 if (SELECTED_FRAME () && FRAME_NS_P (SELECTED_FRAME ())
604 && FRAME_NS_VIEW (SELECTED_FRAME ()))
605 [FRAME_NS_VIEW (SELECTED_FRAME ()) keyDown: theEvent];
606 return YES;
607 }
608
609
610 /* Parse a widget_value's key rep (examples: 's-p', 's-S', '(C-x C-s)', '<f13>')
611 into an accelerator string. We are only able to display a single character
612 for an accelerator, together with an optional modifier combination. (Under
613 Carbon more control was possible, but in Cocoa multi-char strings passed to
614 NSMenuItem get ignored. For now we try to display a super-single letter
615 combo, and return the others as strings to be appended to the item title.
616 (This is signaled by setting keyEquivModMask to 0 for now.) */
617 -(NSString *)parseKeyEquiv: (const char *)key
618 {
619 const char *tpos = key;
620 keyEquivModMask = NSCommandKeyMask;
621
622 if (!key || !strlen (key))
623 return @"";
624
625 while (*tpos == ' ' || *tpos == '(')
626 tpos++;
627 if ((*tpos == 's') && (*(tpos+1) == '-'))
628 {
629 return [NSString stringWithFormat: @"%c", tpos[2]];
630 }
631 keyEquivModMask = 0; /* signal */
632 return [NSString stringWithUTF8String: tpos];
633 }
634
635
636 - (NSMenuItem *)addItemWithWidgetValue: (void *)wvptr
637 {
638 NSMenuItem *item;
639 widget_value *wv = (widget_value *)wvptr;
640
641 if (menu_separator_name_p (wv->name))
642 {
643 item = [NSMenuItem separatorItem];
644 [self addItem: item];
645 }
646 else
647 {
648 NSString *title, *keyEq;
649 title = [NSString stringWithUTF8String: wv->name];
650 if (title == nil)
651 title = @"< ? >"; /* (get out in the open so we know about it) */
652
653 keyEq = [self parseKeyEquiv: wv->key];
654 #ifdef NS_IMPL_COCOA
655 /* OS X just ignores modifier strings longer than one character */
656 if (keyEquivModMask == 0)
657 title = [title stringByAppendingFormat: @" (%@)", keyEq];
658 #endif
659
660 item = [self addItemWithTitle: (NSString *)title
661 action: @selector (menuDown:)
662 keyEquivalent: keyEq];
663 [item setKeyEquivalentModifierMask: keyEquivModMask];
664
665 [item setEnabled: wv->enabled];
666
667 /* Draw radio buttons and tickboxes */
668 if (wv->selected && (wv->button_type == BUTTON_TYPE_TOGGLE ||
669 wv->button_type == BUTTON_TYPE_RADIO))
670 [item setState: NSOnState];
671 else
672 [item setState: NSOffState];
673
674 [item setTag: (NSInteger)wv->call_data];
675 }
676
677 return item;
678 }
679
680
681 /* convenience */
682 -(void)clear
683 {
684 int n;
685
686 for (n = [self numberOfItems]-1; n >= 0; n--)
687 {
688 NSMenuItem *item = [self itemAtIndex: n];
689 NSString *title = [item title];
690 if (([title length] == 0 /* OSX 10.5 */
691 || [ns_app_name isEqualToString: title] /* from 10.6 on */
692 || [@"Apple" isEqualToString: title]) /* older */
693 && ![item isSeparatorItem])
694 continue;
695 [self removeItemAtIndex: n];
696 }
697 }
698
699
700 - (void)fillWithWidgetValue: (void *)wvptr
701 {
702 widget_value *wv = (widget_value *)wvptr;
703
704 /* clear existing contents */
705 [self setMenuChangedMessagesEnabled: NO];
706 [self clear];
707
708 /* add new contents */
709 for (; wv != NULL; wv = wv->next)
710 {
711 NSMenuItem *item = [self addItemWithWidgetValue: wv];
712
713 if (wv->contents)
714 {
715 EmacsMenu *submenu = [[EmacsMenu alloc] initWithTitle: [item title]];
716
717 [self setSubmenu: submenu forItem: item];
718 [submenu fillWithWidgetValue: wv->contents];
719 [submenu release];
720 [item setAction: nil];
721 }
722 }
723
724 [self setMenuChangedMessagesEnabled: YES];
725 #ifdef NS_IMPL_GNUSTEP
726 if ([[self window] isVisible])
727 [self sizeToFit];
728 #else
729 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_2
730 if ([self supermenu] == nil)
731 [self sizeToFit];
732 #endif
733 #endif
734 }
735
736
737 /* adds an empty submenu and returns it */
738 - (EmacsMenu *)addSubmenuWithTitle: (const char *)title forFrame: (struct frame *)f
739 {
740 NSString *titleStr = [NSString stringWithUTF8String: title];
741 NSMenuItem *item = [self addItemWithTitle: titleStr
742 action: nil /*@selector (menuDown:) */
743 keyEquivalent: @""];
744 EmacsMenu *submenu = [[EmacsMenu alloc] initWithTitle: titleStr frame: f];
745 [self setSubmenu: submenu forItem: item];
746 [submenu release];
747 return submenu;
748 }
749
750 /* run a menu in popup mode */
751 - (Lisp_Object)runMenuAt: (NSPoint)p forFrame: (struct frame *)f
752 keymaps: (int)keymaps
753 {
754 EmacsView *view = FRAME_NS_VIEW (f);
755 NSEvent *e, *event;
756 long retVal;
757
758 /* p = [view convertPoint:p fromView: nil]; */
759 p.y = NSHeight ([view frame]) - p.y;
760 e = [[view window] currentEvent];
761 event = [NSEvent mouseEventWithType: NSRightMouseDown
762 location: p
763 modifierFlags: 0
764 timestamp: [e timestamp]
765 windowNumber: [[view window] windowNumber]
766 context: [e context]
767 eventNumber: 0/*[e eventNumber] */
768 clickCount: 1
769 pressure: 0];
770
771 context_menu_value = -1;
772 [NSMenu popUpContextMenu: self withEvent: event forView: view];
773 retVal = context_menu_value;
774 context_menu_value = 0;
775 return retVal > 0
776 ? find_and_return_menu_selection (f, keymaps, (void *)retVal)
777 : Qnil;
778 }
779
780 @end /* EmacsMenu */
781
782
783
784 /* ==========================================================================
785
786 Context Menu: implementing functions
787
788 ========================================================================== */
789
790 Lisp_Object
791 ns_menu_show (FRAME_PTR f, int x, int y, int for_click, int keymaps,
792 Lisp_Object title, const char **error)
793 {
794 EmacsMenu *pmenu;
795 NSPoint p;
796 Lisp_Object window, tem, keymap;
797 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
798 widget_value *wv, *first_wv = 0;
799
800 p.x = x; p.y = y;
801
802 /* now parse stage 2 as in ns_update_menubar */
803 wv = xmalloc_widget_value ();
804 wv->name = "contextmenu";
805 wv->value = 0;
806 wv->enabled = 1;
807 wv->button_type = BUTTON_TYPE_NONE;
808 wv->help = Qnil;
809 first_wv = wv;
810
811 #if 0
812 /* FIXME: a couple of one-line differences prevent reuse */
813 wv = digest_single_submenu (0, menu_items_used, Qnil);
814 #else
815 {
816 widget_value *save_wv = 0, *prev_wv = 0;
817 widget_value **submenu_stack
818 = alloca (menu_items_used * sizeof *submenu_stack);
819 /* Lisp_Object *subprefix_stack
820 = alloca (menu_items_used * sizeof *subprefix_stack); */
821 int submenu_depth = 0;
822 int first_pane = 1;
823 int i;
824
825 /* Loop over all panes and items, filling in the tree. */
826 i = 0;
827 while (i < menu_items_used)
828 {
829 if (EQ (AREF (menu_items, i), Qnil))
830 {
831 submenu_stack[submenu_depth++] = save_wv;
832 save_wv = prev_wv;
833 prev_wv = 0;
834 first_pane = 1;
835 i++;
836 }
837 else if (EQ (AREF (menu_items, i), Qlambda))
838 {
839 prev_wv = save_wv;
840 save_wv = submenu_stack[--submenu_depth];
841 first_pane = 0;
842 i++;
843 }
844 else if (EQ (AREF (menu_items, i), Qt)
845 && submenu_depth != 0)
846 i += MENU_ITEMS_PANE_LENGTH;
847 /* Ignore a nil in the item list.
848 It's meaningful only for dialog boxes. */
849 else if (EQ (AREF (menu_items, i), Qquote))
850 i += 1;
851 else if (EQ (AREF (menu_items, i), Qt))
852 {
853 /* Create a new pane. */
854 Lisp_Object pane_name, prefix;
855 const char *pane_string;
856
857 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
858 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
859
860 #ifndef HAVE_MULTILINGUAL_MENU
861 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
862 {
863 pane_name = ENCODE_MENU_STRING (pane_name);
864 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
865 }
866 #endif
867 pane_string = (NILP (pane_name)
868 ? "" : SSDATA (pane_name));
869 /* If there is just one top-level pane, put all its items directly
870 under the top-level menu. */
871 if (menu_items_n_panes == 1)
872 pane_string = "";
873
874 /* If the pane has a meaningful name,
875 make the pane a top-level menu item
876 with its items as a submenu beneath it. */
877 if (!keymaps && strcmp (pane_string, ""))
878 {
879 wv = xmalloc_widget_value ();
880 if (save_wv)
881 save_wv->next = wv;
882 else
883 first_wv->contents = wv;
884 wv->name = pane_string;
885 if (keymaps && !NILP (prefix))
886 wv->name++;
887 wv->value = 0;
888 wv->enabled = 1;
889 wv->button_type = BUTTON_TYPE_NONE;
890 wv->help = Qnil;
891 save_wv = wv;
892 prev_wv = 0;
893 }
894 else if (first_pane)
895 {
896 save_wv = wv;
897 prev_wv = 0;
898 }
899 first_pane = 0;
900 i += MENU_ITEMS_PANE_LENGTH;
901 }
902 else
903 {
904 /* Create a new item within current pane. */
905 Lisp_Object item_name, enable, descrip, def, type, selected, help;
906 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
907 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
908 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
909 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
910 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
911 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
912 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
913
914 #ifndef HAVE_MULTILINGUAL_MENU
915 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
916 {
917 item_name = ENCODE_MENU_STRING (item_name);
918 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
919 }
920
921 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
922 {
923 descrip = ENCODE_MENU_STRING (descrip);
924 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
925 }
926 #endif /* not HAVE_MULTILINGUAL_MENU */
927
928 wv = xmalloc_widget_value ();
929 if (prev_wv)
930 prev_wv->next = wv;
931 else
932 save_wv->contents = wv;
933 wv->name = SSDATA (item_name);
934 if (!NILP (descrip))
935 wv->key = SSDATA (descrip);
936 wv->value = 0;
937 /* If this item has a null value,
938 make the call_data null so that it won't display a box
939 when the mouse is on it. */
940 wv->call_data
941 = !NILP (def) ? (void *) &AREF (menu_items, i) : 0;
942 wv->enabled = !NILP (enable);
943
944 if (NILP (type))
945 wv->button_type = BUTTON_TYPE_NONE;
946 else if (EQ (type, QCtoggle))
947 wv->button_type = BUTTON_TYPE_TOGGLE;
948 else if (EQ (type, QCradio))
949 wv->button_type = BUTTON_TYPE_RADIO;
950 else
951 abort ();
952
953 wv->selected = !NILP (selected);
954
955 if (! STRINGP (help))
956 help = Qnil;
957
958 wv->help = help;
959
960 prev_wv = wv;
961
962 i += MENU_ITEMS_ITEM_LENGTH;
963 }
964 }
965 }
966 #endif
967
968 if (!NILP (title))
969 {
970 widget_value *wv_title = xmalloc_widget_value ();
971 widget_value *wv_sep = xmalloc_widget_value ();
972
973 /* Maybe replace this separator with a bitmap or owner-draw item
974 so that it looks better. Having two separators looks odd. */
975 wv_sep->name = "--";
976 wv_sep->next = first_wv->contents;
977 wv_sep->help = Qnil;
978
979 #ifndef HAVE_MULTILINGUAL_MENU
980 if (STRING_MULTIBYTE (title))
981 title = ENCODE_MENU_STRING (title);
982 #endif
983
984 wv_title->name = SSDATA (title);
985 wv_title->enabled = NO;
986 wv_title->button_type = BUTTON_TYPE_NONE;
987 wv_title->help = Qnil;
988 wv_title->next = wv_sep;
989 first_wv->contents = wv_title;
990 }
991
992 pmenu = [[EmacsMenu alloc] initWithTitle:
993 [NSString stringWithUTF8String: SDATA (title)]];
994 [pmenu fillWithWidgetValue: first_wv->contents];
995 free_menubar_widget_value_tree (first_wv);
996 unbind_to (specpdl_count, Qnil);
997
998 popup_activated_flag = 1;
999 tem = [pmenu runMenuAt: p forFrame: f keymaps: keymaps];
1000 popup_activated_flag = 0;
1001 [[FRAME_NS_VIEW (SELECTED_FRAME ()) window] makeKeyWindow];
1002
1003 return tem;
1004 }
1005
1006
1007 /* ==========================================================================
1008
1009 Toolbar: externally-called functions
1010
1011 ========================================================================== */
1012
1013 void
1014 free_frame_tool_bar (FRAME_PTR f)
1015 /* --------------------------------------------------------------------------
1016 Under NS we just hide the toolbar until it might be needed again.
1017 -------------------------------------------------------------------------- */
1018 {
1019 BLOCK_INPUT;
1020 [[FRAME_NS_VIEW (f) toolbar] setVisible: NO];
1021 FRAME_TOOLBAR_HEIGHT (f) = 0;
1022 UNBLOCK_INPUT;
1023 }
1024
1025 void
1026 update_frame_tool_bar (FRAME_PTR f)
1027 /* --------------------------------------------------------------------------
1028 Update toolbar contents
1029 -------------------------------------------------------------------------- */
1030 {
1031 int i;
1032 EmacsView *view = FRAME_NS_VIEW (f);
1033 NSWindow *window = [view window];
1034 EmacsToolbar *toolbar = [view toolbar];
1035
1036 BLOCK_INPUT;
1037 [toolbar clearActive];
1038
1039 /* update EmacsToolbar as in GtkUtils, build items list */
1040 for (i = 0; i < f->n_tool_bar_items; ++i)
1041 {
1042 #define TOOLPROP(IDX) AREF (f->tool_bar_items, \
1043 i * TOOL_BAR_ITEM_NSLOTS + (IDX))
1044
1045 BOOL enabled_p = !NILP (TOOLPROP (TOOL_BAR_ITEM_ENABLED_P));
1046 BOOL selected_p = !NILP (TOOLPROP (TOOL_BAR_ITEM_SELECTED_P));
1047 int idx;
1048 ptrdiff_t img_id;
1049 struct image *img;
1050 Lisp_Object image;
1051 Lisp_Object helpObj;
1052 const char *helpText;
1053
1054 /* If image is a vector, choose the image according to the
1055 button state. */
1056 image = TOOLPROP (TOOL_BAR_ITEM_IMAGES);
1057 if (VECTORP (image))
1058 {
1059 /* NS toolbar auto-computes disabled and selected images */
1060 idx = TOOL_BAR_IMAGE_ENABLED_SELECTED;
1061 eassert (ASIZE (image) >= idx);
1062 image = AREF (image, idx);
1063 }
1064 else
1065 {
1066 idx = -1;
1067 }
1068 helpObj = TOOLPROP (TOOL_BAR_ITEM_HELP);
1069 if (NILP (helpObj))
1070 helpObj = TOOLPROP (TOOL_BAR_ITEM_CAPTION);
1071 helpText = NILP (helpObj) ? "" : SSDATA (helpObj);
1072
1073 /* Ignore invalid image specifications. */
1074 if (!valid_image_p (image))
1075 {
1076 /* Don't log anything, GNUS makes invalid images all the time. */
1077 continue;
1078 }
1079
1080 img_id = lookup_image (f, image);
1081 img = IMAGE_FROM_ID (f, img_id);
1082 prepare_image_for_display (f, img);
1083
1084 if (img->load_failed_p || img->pixmap == nil)
1085 {
1086 NSLog (@"Could not prepare toolbar image for display.");
1087 continue;
1088 }
1089
1090 [toolbar addDisplayItemWithImage: img->pixmap idx: i helpText: helpText
1091 enabled: enabled_p];
1092 #undef TOOLPROP
1093 }
1094
1095 if (![toolbar isVisible])
1096 [toolbar setVisible: YES];
1097
1098 if ([toolbar changed])
1099 {
1100 /* inform app that toolbar has changed */
1101 NSDictionary *dict = [toolbar configurationDictionary];
1102 NSMutableDictionary *newDict = [dict mutableCopy];
1103 NSEnumerator *keys = [[dict allKeys] objectEnumerator];
1104 NSObject *key;
1105 while ((key = [keys nextObject]) != nil)
1106 {
1107 NSObject *val = [dict objectForKey: key];
1108 if ([val isKindOfClass: [NSArray class]])
1109 {
1110 [newDict setObject:
1111 [toolbar toolbarDefaultItemIdentifiers: toolbar]
1112 forKey: key];
1113 break;
1114 }
1115 }
1116 [toolbar setConfigurationFromDictionary: newDict];
1117 [newDict release];
1118 }
1119
1120 FRAME_TOOLBAR_HEIGHT (f) =
1121 NSHeight ([window frameRectForContentRect: NSMakeRect (0, 0, 0, 0)])
1122 - FRAME_NS_TITLEBAR_HEIGHT (f);
1123 UNBLOCK_INPUT;
1124 }
1125
1126
1127 /* ==========================================================================
1128
1129 Toolbar: class implementation
1130
1131 ========================================================================== */
1132
1133 @implementation EmacsToolbar
1134
1135 - initForView: (EmacsView *)view withIdentifier: (NSString *)identifier
1136 {
1137 self = [super initWithIdentifier: identifier];
1138 emacsView = view;
1139 [self setDisplayMode: NSToolbarDisplayModeIconOnly];
1140 [self setSizeMode: NSToolbarSizeModeSmall];
1141 [self setDelegate: self];
1142 identifierToItem = [[NSMutableDictionary alloc] initWithCapacity: 10];
1143 activeIdentifiers = [[NSMutableArray alloc] initWithCapacity: 8];
1144 prevEnablement = enablement = 0L;
1145 return self;
1146 }
1147
1148 - (void)dealloc
1149 {
1150 [prevIdentifiers release];
1151 [activeIdentifiers release];
1152 [identifierToItem release];
1153 [super dealloc];
1154 }
1155
1156 - (void) clearActive
1157 {
1158 [prevIdentifiers release];
1159 prevIdentifiers = [activeIdentifiers copy];
1160 [activeIdentifiers removeAllObjects];
1161 prevEnablement = enablement;
1162 enablement = 0L;
1163 }
1164
1165 - (BOOL) changed
1166 {
1167 return [activeIdentifiers isEqualToArray: prevIdentifiers] &&
1168 enablement == prevEnablement ? NO : YES;
1169 }
1170
1171 - (void) addDisplayItemWithImage: (EmacsImage *)img idx: (int)idx
1172 helpText: (const char *)help enabled: (BOOL)enabled
1173 {
1174 /* 1) come up w/identifier */
1175 NSString *identifier
1176 = [NSString stringWithFormat: @"%u", [img hash]];
1177
1178 /* 2) create / reuse item */
1179 NSToolbarItem *item = [identifierToItem objectForKey: identifier];
1180 if (item == nil)
1181 {
1182 item = [[[NSToolbarItem alloc] initWithItemIdentifier: identifier]
1183 autorelease];
1184 [item setImage: img];
1185 [item setToolTip: [NSString stringWithUTF8String: help]];
1186 [item setTarget: emacsView];
1187 [item setAction: @selector (toolbarClicked:)];
1188 }
1189
1190 [item setTag: idx];
1191 [item setEnabled: enabled];
1192
1193 /* 3) update state */
1194 [identifierToItem setObject: item forKey: identifier];
1195 [activeIdentifiers addObject: identifier];
1196 enablement = (enablement << 1) | (enabled == YES);
1197 }
1198
1199 /* This overrides super's implementation, which automatically sets
1200 all items to enabled state (for some reason). */
1201 - (void)validateVisibleItems { }
1202
1203
1204 /* delegate methods */
1205
1206 - (NSToolbarItem *)toolbar: (NSToolbar *)toolbar
1207 itemForItemIdentifier: (NSString *)itemIdentifier
1208 willBeInsertedIntoToolbar: (BOOL)flag
1209 {
1210 /* look up NSToolbarItem by identifier and return... */
1211 return [identifierToItem objectForKey: itemIdentifier];
1212 }
1213
1214 - (NSArray *)toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
1215 {
1216 /* return entire set.. */
1217 return activeIdentifiers;
1218 }
1219
1220 /* for configuration palette (not yet supported) */
1221 - (NSArray *)toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar
1222 {
1223 /* return entire set... */
1224 return [identifierToItem allKeys];
1225 }
1226
1227 /* optional and unneeded */
1228 /* - toolbarWillAddItem: (NSNotification *)notification { } */
1229 /* - toolbarDidRemoveItem: (NSNotification *)notification { } */
1230 /* - (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar */
1231
1232 @end /* EmacsToolbar */
1233
1234
1235
1236 /* ==========================================================================
1237
1238 Tooltip: class implementation
1239
1240 ========================================================================== */
1241
1242 /* Needed because NeXTstep does not provide enough control over tooltip
1243 display. */
1244 @implementation EmacsTooltip
1245
1246 - init
1247 {
1248 NSColor *col = [NSColor colorWithCalibratedRed: 1.0 green: 1.0
1249 blue: 0.792 alpha: 0.95];
1250 NSFont *font = [NSFont toolTipsFontOfSize: 0];
1251 NSFont *sfont = [font screenFont];
1252 int height = [sfont ascender] - [sfont descender];
1253 /*[font boundingRectForFont].size.height; */
1254 NSRect r = NSMakeRect (0, 0, 100, height+6);
1255
1256 textField = [[NSTextField alloc] initWithFrame: r];
1257 [textField setFont: font];
1258 [textField setBackgroundColor: col];
1259
1260 [textField setEditable: NO];
1261 [textField setSelectable: NO];
1262 [textField setBordered: NO];
1263 [textField setBezeled: NO];
1264 [textField setDrawsBackground: YES];
1265
1266 win = [[NSWindow alloc]
1267 initWithContentRect: [textField frame]
1268 styleMask: 0
1269 backing: NSBackingStoreBuffered
1270 defer: YES];
1271 [win setHasShadow: YES];
1272 [win setReleasedWhenClosed: NO];
1273 [win setDelegate: self];
1274 [[win contentView] addSubview: textField];
1275 /* [win setBackgroundColor: col]; */
1276 [win setOpaque: NO];
1277
1278 return self;
1279 }
1280
1281 - (void) dealloc
1282 {
1283 [win close];
1284 [win release];
1285 [textField release];
1286 [super dealloc];
1287 }
1288
1289 - (void) setText: (char *)text
1290 {
1291 NSString *str = [NSString stringWithUTF8String: text];
1292 NSRect r = [textField frame];
1293 NSSize tooltipDims;
1294
1295 [textField setStringValue: str];
1296 tooltipDims = [[textField cell] cellSize];
1297
1298 r.size.width = tooltipDims.width;
1299 r.size.height = tooltipDims.height;
1300 [textField setFrame: r];
1301 }
1302
1303 - (void) showAtX: (int)x Y: (int)y for: (int)seconds
1304 {
1305 NSRect wr = [win frame];
1306
1307 wr.origin = NSMakePoint (x, y);
1308 wr.size = [textField frame].size;
1309
1310 [win setFrame: wr display: YES];
1311 [win orderFront: self];
1312 [win display];
1313 timer = [NSTimer scheduledTimerWithTimeInterval: (float)seconds target: self
1314 selector: @selector (hide)
1315 userInfo: nil repeats: NO];
1316 [timer retain];
1317 }
1318
1319 - (void) hide
1320 {
1321 [win close];
1322 if (timer != nil)
1323 {
1324 if ([timer isValid])
1325 [timer invalidate];
1326 [timer release];
1327 timer = nil;
1328 }
1329 }
1330
1331 - (BOOL) isActive
1332 {
1333 return timer != nil;
1334 }
1335
1336 - (NSRect) frame
1337 {
1338 return [textField frame];
1339 }
1340
1341 @end /* EmacsTooltip */
1342
1343
1344
1345 /* ==========================================================================
1346
1347 Popup Dialog: implementing functions
1348
1349 ========================================================================== */
1350
1351
1352 static Lisp_Object
1353 pop_down_menu (Lisp_Object arg)
1354 {
1355 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
1356 if (popup_activated_flag)
1357 {
1358 popup_activated_flag = 0;
1359 BLOCK_INPUT;
1360 [NSApp endModalSession: popupSession];
1361 [((EmacsDialogPanel *) (p->pointer)) close];
1362 [[FRAME_NS_VIEW (SELECTED_FRAME ()) window] makeKeyWindow];
1363 UNBLOCK_INPUT;
1364 }
1365 return Qnil;
1366 }
1367
1368
1369 Lisp_Object
1370 ns_popup_dialog (Lisp_Object position, Lisp_Object contents, Lisp_Object header)
1371 {
1372 id dialog;
1373 Lisp_Object window, tem, title;
1374 struct frame *f;
1375 NSPoint p;
1376 BOOL isQ;
1377
1378 NSTRACE (x-popup-dialog);
1379
1380 check_ns ();
1381
1382 isQ = NILP (header);
1383
1384 if (EQ (position, Qt)
1385 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
1386 || EQ (XCAR (position), Qtool_bar))))
1387 {
1388 window = selected_window;
1389 }
1390 else if (CONSP (position))
1391 {
1392 Lisp_Object tem;
1393 tem = Fcar (position);
1394 if (XTYPE (tem) == Lisp_Cons)
1395 window = Fcar (Fcdr (position));
1396 else
1397 {
1398 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
1399 window = Fcar (tem); /* POSN_WINDOW (tem) */
1400 }
1401 }
1402 else if (WINDOWP (position) || FRAMEP (position))
1403 {
1404 window = position;
1405 }
1406 else
1407 window = Qnil;
1408
1409 if (FRAMEP (window))
1410 f = XFRAME (window);
1411 else if (WINDOWP (window))
1412 {
1413 CHECK_LIVE_WINDOW (window);
1414 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1415 }
1416 else
1417 CHECK_WINDOW (window);
1418
1419 p.x = (int)f->left_pos + ((int)FRAME_COLUMN_WIDTH (f) * f->text_cols)/2;
1420 p.y = (int)f->top_pos + (FRAME_LINE_HEIGHT (f) * f->text_lines)/2;
1421
1422 title = Fcar (contents);
1423 CHECK_STRING (title);
1424
1425 if (NILP (Fcar (Fcdr (contents))))
1426 /* No buttons specified, add an "Ok" button so users can pop down
1427 the dialog. */
1428 contents = Fcons (title, Fcons (Fcons (build_string ("Ok"), Qt), Qnil));
1429
1430 BLOCK_INPUT;
1431 dialog = [[EmacsDialogPanel alloc] initFromContents: contents
1432 isQuestion: isQ];
1433 {
1434 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
1435 record_unwind_protect (pop_down_menu, make_save_value (dialog, 0));
1436 popup_activated_flag = 1;
1437 tem = [dialog runDialogAt: p];
1438 unbind_to (specpdl_count, Qnil); /* calls pop_down_menu */
1439 }
1440 UNBLOCK_INPUT;
1441
1442 return tem;
1443 }
1444
1445
1446 /* ==========================================================================
1447
1448 Popup Dialog: class implementation
1449
1450 ========================================================================== */
1451
1452 @interface FlippedView : NSView
1453 {
1454 }
1455 @end
1456
1457 @implementation FlippedView
1458 - (BOOL)isFlipped
1459 {
1460 return YES;
1461 }
1462 @end
1463
1464 @implementation EmacsDialogPanel
1465
1466 #define SPACER 8.0
1467 #define ICONSIZE 64.0
1468 #define TEXTHEIGHT 20.0
1469 #define MINCELLWIDTH 90.0
1470
1471 - initWithContentRect: (NSRect)contentRect styleMask: (NSUInteger)aStyle
1472 backing: (NSBackingStoreType)backingType defer: (BOOL)flag
1473 {
1474 NSSize spacing = {SPACER, SPACER};
1475 NSRect area;
1476 char this_cmd_name[80];
1477 id cell;
1478 static NSImageView *imgView;
1479 static FlippedView *contentView;
1480
1481 if (imgView == nil)
1482 {
1483 NSImage *img;
1484 area.origin.x = 3*SPACER;
1485 area.origin.y = 2*SPACER;
1486 area.size.width = ICONSIZE;
1487 area.size.height= ICONSIZE;
1488 img = [[NSImage imageNamed: @"NSApplicationIcon"] copy];
1489 [img setScalesWhenResized: YES];
1490 [img setSize: NSMakeSize (ICONSIZE, ICONSIZE)];
1491 imgView = [[NSImageView alloc] initWithFrame: area];
1492 [imgView setImage: img];
1493 [imgView setEditable: NO];
1494 [img release];
1495 }
1496
1497 aStyle = NSTitledWindowMask;
1498 flag = YES;
1499 rows = 0;
1500 cols = 1;
1501 [super initWithContentRect: contentRect styleMask: aStyle
1502 backing: backingType defer: flag];
1503 contentView = [[FlippedView alloc] initWithFrame: [[self contentView] frame]];
1504 [self setContentView: contentView];
1505
1506 [[self contentView] setAutoresizesSubviews: YES];
1507
1508 [[self contentView] addSubview: imgView];
1509 [self setTitle: @""];
1510
1511 area.origin.x += ICONSIZE+2*SPACER;
1512 /* area.origin.y = TEXTHEIGHT; ICONSIZE/2-10+SPACER; */
1513 area.size.width = 400;
1514 area.size.height= TEXTHEIGHT;
1515 command = [[[NSTextField alloc] initWithFrame: area] autorelease];
1516 [[self contentView] addSubview: command];
1517 [command setStringValue: ns_app_name];
1518 [command setDrawsBackground: NO];
1519 [command setBezeled: NO];
1520 [command setSelectable: NO];
1521 [command setFont: [NSFont boldSystemFontOfSize: 13.0]];
1522
1523 /* area.origin.x = ICONSIZE+2*SPACER;
1524 area.origin.y = TEXTHEIGHT + 2*SPACER;
1525 area.size.width = 400;
1526 area.size.height= 2;
1527 tem = [[[NSBox alloc] initWithFrame: area] autorelease];
1528 [[self contentView] addSubview: tem];
1529 [tem setTitlePosition: NSNoTitle];
1530 [tem setAutoresizingMask: NSViewWidthSizable];*/
1531
1532 /* area.origin.x = ICONSIZE+2*SPACER; */
1533 area.origin.y += TEXTHEIGHT+SPACER;
1534 area.size.width = 400;
1535 area.size.height= TEXTHEIGHT;
1536 title = [[[NSTextField alloc] initWithFrame: area] autorelease];
1537 [[self contentView] addSubview: title];
1538 [title setDrawsBackground: NO];
1539 [title setBezeled: NO];
1540 [title setSelectable: NO];
1541 [title setFont: [NSFont systemFontOfSize: 11.0]];
1542
1543 cell = [[[NSButtonCell alloc] initTextCell: @""] autorelease];
1544 [cell setBordered: NO];
1545 [cell setEnabled: NO];
1546 [cell setCellAttribute: NSCellIsInsetButton to: 8];
1547 [cell setBezelStyle: NSRoundedBezelStyle];
1548
1549 matrix = [[NSMatrix alloc] initWithFrame: contentRect
1550 mode: NSHighlightModeMatrix
1551 prototype: cell
1552 numberOfRows: 0
1553 numberOfColumns: 1];
1554 [[self contentView] addSubview: matrix];
1555 [matrix release];
1556 [matrix setFrameOrigin: NSMakePoint (area.origin.x,
1557 area.origin.y + (TEXTHEIGHT+3*SPACER))];
1558 [matrix setIntercellSpacing: spacing];
1559
1560 [self setOneShot: YES];
1561 [self setReleasedWhenClosed: YES];
1562 [self setHidesOnDeactivate: YES];
1563 return self;
1564 }
1565
1566
1567 - (BOOL)windowShouldClose: (id)sender
1568 {
1569 [NSApp stopModalWithCode: XHASH (Qnil)]; // FIXME: BIG UGLY HACK!!
1570 return NO;
1571 }
1572
1573
1574 void process_dialog (id window, Lisp_Object list)
1575 {
1576 Lisp_Object item;
1577 int row = 0;
1578
1579 for (; XTYPE (list) == Lisp_Cons; list = XCDR (list))
1580 {
1581 item = XCAR (list);
1582 if (XTYPE (item) == Lisp_String)
1583 {
1584 [window addString: SDATA (item) row: row++];
1585 }
1586 else if (XTYPE (item) == Lisp_Cons)
1587 {
1588 [window addButton: SDATA (XCAR (item))
1589 value: XCDR (item) row: row++];
1590 }
1591 else if (NILP (item))
1592 {
1593 [window addSplit];
1594 row = 0;
1595 }
1596 }
1597 }
1598
1599
1600 - addButton: (char *)str value: (Lisp_Object)val row: (int)row
1601 {
1602 id cell;
1603
1604 if (row >= rows)
1605 {
1606 [matrix addRow];
1607 rows++;
1608 }
1609 cell = [matrix cellAtRow: row column: cols-1];
1610 [cell setTarget: self];
1611 [cell setAction: @selector (clicked: )];
1612 [cell setTitle: [NSString stringWithUTF8String: str]];
1613 [cell setTag: XHASH (val)]; // FIXME: BIG UGLY HACK!!
1614 [cell setBordered: YES];
1615 [cell setEnabled: YES];
1616
1617 return self;
1618 }
1619
1620
1621 - addString: (char *)str row: (int)row
1622 {
1623 id cell;
1624
1625 if (row >= rows)
1626 {
1627 [matrix addRow];
1628 rows++;
1629 }
1630 cell = [matrix cellAtRow: row column: cols-1];
1631 [cell setTitle: [NSString stringWithUTF8String: str]];
1632 [cell setBordered: YES];
1633 [cell setEnabled: NO];
1634
1635 return self;
1636 }
1637
1638
1639 - addSplit
1640 {
1641 [matrix addColumn];
1642 cols++;
1643 return self;
1644 }
1645
1646
1647 - clicked: sender
1648 {
1649 NSArray *sellist = nil;
1650 EMACS_INT seltag;
1651
1652 sellist = [sender selectedCells];
1653 if ([sellist count]<1)
1654 return self;
1655
1656 seltag = [[sellist objectAtIndex: 0] tag];
1657 if (seltag != XHASH (Qundefined)) // FIXME: BIG UGLY HACK!!
1658 [NSApp stopModalWithCode: seltag];
1659 return self;
1660 }
1661
1662
1663 - initFromContents: (Lisp_Object)contents isQuestion: (BOOL)isQ
1664 {
1665 Lisp_Object head;
1666 [super init];
1667
1668 if (XTYPE (contents) == Lisp_Cons)
1669 {
1670 head = Fcar (contents);
1671 process_dialog (self, Fcdr (contents));
1672 }
1673 else
1674 head = contents;
1675
1676 if (XTYPE (head) == Lisp_String)
1677 [title setStringValue:
1678 [NSString stringWithUTF8String: SDATA (head)]];
1679 else if (isQ == YES)
1680 [title setStringValue: @"Question"];
1681 else
1682 [title setStringValue: @"Information"];
1683
1684 {
1685 int i;
1686 NSRect r, s, t;
1687
1688 if (cols == 1 && rows > 1) /* Never told where to split */
1689 {
1690 [matrix addColumn];
1691 for (i = 0; i<rows/2; i++)
1692 {
1693 [matrix putCell: [matrix cellAtRow: (rows+1)/2 column: 0]
1694 atRow: i column: 1];
1695 [matrix removeRow: (rows+1)/2];
1696 }
1697 }
1698
1699 [matrix sizeToFit];
1700 {
1701 NSSize csize = [matrix cellSize];
1702 if (csize.width < MINCELLWIDTH)
1703 {
1704 csize.width = MINCELLWIDTH;
1705 [matrix setCellSize: csize];
1706 [matrix sizeToCells];
1707 }
1708 }
1709
1710 [title sizeToFit];
1711 [command sizeToFit];
1712
1713 t = [matrix frame];
1714 r = [title frame];
1715 if (r.size.width+r.origin.x > t.size.width+t.origin.x)
1716 {
1717 t.origin.x = r.origin.x;
1718 t.size.width = r.size.width;
1719 }
1720 r = [command frame];
1721 if (r.size.width+r.origin.x > t.size.width+t.origin.x)
1722 {
1723 t.origin.x = r.origin.x;
1724 t.size.width = r.size.width;
1725 }
1726
1727 r = [self frame];
1728 s = [(NSView *)[self contentView] frame];
1729 r.size.width += t.origin.x+t.size.width +2*SPACER-s.size.width;
1730 r.size.height += t.origin.y+t.size.height+SPACER-s.size.height;
1731 [self setFrame: r display: NO];
1732 }
1733
1734 return self;
1735 }
1736
1737
1738 - (void)dealloc
1739 {
1740 { [super dealloc]; return; };
1741 }
1742
1743
1744 - (Lisp_Object)runDialogAt: (NSPoint)p
1745 {
1746 NSInteger ret;
1747
1748 /* initiate a session that will be ended by pop_down_menu */
1749 popupSession = [NSApp beginModalSessionForWindow: self];
1750 while (popup_activated_flag
1751 && (ret = [NSApp runModalSession: popupSession])
1752 == NSRunContinuesResponse)
1753 {
1754 /* Run this for timers.el, indep of atimers; might not return.
1755 TODO: use return value to avoid calling every iteration. */
1756 timer_check ();
1757 [NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.1]];
1758 }
1759
1760 { /* FIXME: BIG UGLY HACK!!! */
1761 Lisp_Object tmp;
1762 *(EMACS_INT*)(&tmp) = ret;
1763 return tmp;
1764 }
1765 }
1766
1767 @end
1768
1769
1770 /* ==========================================================================
1771
1772 Lisp definitions
1773
1774 ========================================================================== */
1775
1776 DEFUN ("ns-reset-menu", Fns_reset_menu, Sns_reset_menu, 0, 0, 0,
1777 doc: /* Cause the NS menu to be re-calculated. */)
1778 (void)
1779 {
1780 set_frame_menubar (SELECTED_FRAME (), 1, 0);
1781 return Qnil;
1782 }
1783
1784
1785 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 3, 0,
1786 doc: /* Pop up a dialog box and return user's selection.
1787 POSITION specifies which frame to use.
1788 This is normally a mouse button event or a window or frame.
1789 If POSITION is t, it means to use the frame the mouse is on.
1790 The dialog box appears in the middle of the specified frame.
1791
1792 CONTENTS specifies the alternatives to display in the dialog box.
1793 It is a list of the form (DIALOG ITEM1 ITEM2...).
1794 Each ITEM is a cons cell (STRING . VALUE).
1795 The return value is VALUE from the chosen item.
1796
1797 An ITEM may also be just a string--that makes a nonselectable item.
1798 An ITEM may also be nil--that means to put all preceding items
1799 on the left of the dialog box and all following items on the right.
1800 \(By default, approximately half appear on each side.)
1801
1802 If HEADER is non-nil, the frame title for the box is "Information",
1803 otherwise it is "Question".
1804
1805 If the user gets rid of the dialog box without making a valid choice,
1806 for instance using the window manager, then this produces a quit and
1807 `x-popup-dialog' does not return. */)
1808 (Lisp_Object position, Lisp_Object contents, Lisp_Object header)
1809 {
1810 return ns_popup_dialog (position, contents, header);
1811 }
1812
1813 DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p, Smenu_or_popup_active_p, 0, 0, 0,
1814 doc: /* Return t if a menu or popup dialog is active. */)
1815 (void)
1816 {
1817 return popup_activated () ? Qt : Qnil;
1818 }
1819
1820 /* ==========================================================================
1821
1822 Lisp interface declaration
1823
1824 ========================================================================== */
1825
1826 void
1827 syms_of_nsmenu (void)
1828 {
1829 #ifndef NS_IMPL_COCOA
1830 /* Don't know how to keep track of this in Next/Open/Gnustep. Always
1831 update menus there. */
1832 trackingMenu = 1;
1833 #endif
1834 defsubr (&Sx_popup_dialog);
1835 defsubr (&Sns_reset_menu);
1836 defsubr (&Smenu_or_popup_active_p);
1837
1838 Qdebug_on_next_call = intern_c_string ("debug-on-next-call");
1839 staticpro (&Qdebug_on_next_call);
1840 }