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