]> code.delx.au - gnu-emacs/blob - src/macmenu.c
Changes from yesterday
[gnu-emacs] / src / macmenu.c
1 /* Menu support for GNU Emacs on the for Mac OS.
2 Copyright (C) 2000, 2001, 2002 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 2, or (at your option)
9 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; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 /* Contributed by Andrew Choi (akochoi@mac.com). */
22
23 #include <config.h>
24 #include <signal.h>
25
26 #include <stdio.h>
27 #include "lisp.h"
28 #include "termhooks.h"
29 #include "keyboard.h"
30 #include "keymap.h"
31 #include "frame.h"
32 #include "window.h"
33 #include "blockinput.h"
34 #include "buffer.h"
35 #include "charset.h"
36 #include "coding.h"
37
38 #ifdef MAC_OSX
39 #undef mktime
40 #undef DEBUG
41 #undef Z
42 #undef free
43 #undef malloc
44 #undef realloc
45 /* Macros max and min defined in lisp.h conflict with those in
46 precompiled header Carbon.h. */
47 #undef max
48 #undef min
49 #undef init_process
50 #include <Carbon/Carbon.h>
51 #undef Z
52 #define Z (current_buffer->text->z)
53 #undef free
54 #define free unexec_free
55 #undef malloc
56 #define malloc unexec_malloc
57 #undef realloc
58 #define realloc unexec_realloc
59 #undef min
60 #define min(a, b) ((a) < (b) ? (a) : (b))
61 #undef max
62 #define max(a, b) ((a) > (b) ? (a) : (b))
63 #undef init_process
64 #define init_process emacs_init_process
65 #else /* not MAC_OSX */
66 #include <MacTypes.h>
67 #include <Menus.h>
68 #include <QuickDraw.h>
69 #include <ToolUtils.h>
70 #include <Fonts.h>
71 #include <Controls.h>
72 #include <Windows.h>
73 #include <Events.h>
74 #if defined (__MRC__) || (__MSL__ >= 0x6000)
75 #include <ControlDefinitions.h>
76 #endif
77 #endif /* not MAC_OSX */
78
79 /* This may include sys/types.h, and that somehow loses
80 if this is not done before the other system files. */
81 #include "macterm.h"
82
83 /* Load sys/types.h if not already loaded.
84 In some systems loading it twice is suicidal. */
85 #ifndef makedev
86 #include <sys/types.h>
87 #endif
88
89 #include "dispextern.h"
90
91 #define POPUP_SUBMENU_ID 235
92 #define MIN_POPUP_SUBMENU_ID 512
93 #define MIN_MENU_ID 256
94 #define MIN_SUBMENU_ID 1
95
96 #define DIALOG_WINDOW_RESOURCE 130
97
98 #define HAVE_DIALOGS 1
99
100 #undef HAVE_MULTILINGUAL_MENU
101 #undef HAVE_DIALOGS /* TODO: Implement native dialogs. */
102
103 /******************************************************************/
104 /* Definitions copied from lwlib.h */
105
106 typedef void * XtPointer;
107
108 enum button_type
109 {
110 BUTTON_TYPE_NONE,
111 BUTTON_TYPE_TOGGLE,
112 BUTTON_TYPE_RADIO
113 };
114
115 /* This structure is based on the one in ../lwlib/lwlib.h, modified
116 for Mac OS. */
117 typedef struct _widget_value
118 {
119 /* name of widget */
120 char* name;
121 /* value (meaning depend on widget type) */
122 char* value;
123 /* keyboard equivalent. no implications for XtTranslations */
124 char* key;
125 /* Help string or nil if none.
126 GC finds this string through the frame's menu_bar_vector
127 or through menu_items. */
128 Lisp_Object help;
129 /* true if enabled */
130 Boolean enabled;
131 /* true if selected */
132 Boolean selected;
133 /* The type of a button. */
134 enum button_type button_type;
135 /* true if menu title */
136 Boolean title;
137 #if 0
138 /* true if was edited (maintained by get_value) */
139 Boolean edited;
140 /* true if has changed (maintained by lw library) */
141 change_type change;
142 /* true if this widget itself has changed,
143 but not counting the other widgets found in the `next' field. */
144 change_type this_one_change;
145 #endif
146 /* Contents of the sub-widgets, also selected slot for checkbox */
147 struct _widget_value* contents;
148 /* data passed to callback */
149 XtPointer call_data;
150 /* next one in the list */
151 struct _widget_value* next;
152 #if 0
153 /* slot for the toolkit dependent part. Always initialize to NULL. */
154 void* toolkit_data;
155 /* tell us if we should free the toolkit data slot when freeing the
156 widget_value itself. */
157 Boolean free_toolkit_data;
158
159 /* we resource the widget_value structures; this points to the next
160 one on the free list if this one has been deallocated.
161 */
162 struct _widget_value *free_list;
163 #endif
164 } widget_value;
165
166 /* Assumed by other routines to zero area returned. */
167 #define malloc_widget_value() (void *)memset (xmalloc (sizeof (widget_value)),\
168 0, (sizeof (widget_value)))
169 #define free_widget_value(wv) xfree (wv)
170
171 /******************************************************************/
172
173 #ifndef TRUE
174 #define TRUE 1
175 #define FALSE 0
176 #endif /* no TRUE */
177
178 Lisp_Object Vmenu_updating_frame;
179
180 Lisp_Object Qdebug_on_next_call;
181
182 extern Lisp_Object Qmenu_bar;
183
184 extern Lisp_Object QCtoggle, QCradio;
185
186 extern Lisp_Object Voverriding_local_map;
187 extern Lisp_Object Voverriding_local_map_menu_flag;
188
189 extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
190
191 extern Lisp_Object Qmenu_bar_update_hook;
192
193 void set_frame_menubar ();
194
195 static void push_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
196 Lisp_Object, Lisp_Object, Lisp_Object,
197 Lisp_Object, Lisp_Object));
198 #ifdef HAVE_DIALOGS
199 static Lisp_Object mac_dialog_show ();
200 #endif
201 static Lisp_Object mac_menu_show ();
202
203 static void keymap_panes ();
204 static void single_keymap_panes ();
205 static void single_menu_item ();
206 static void list_of_panes ();
207 static void list_of_items ();
208
209 static void fill_submenu (MenuHandle, widget_value *);
210 static void fill_menubar (widget_value *);
211
212 \f
213 /* This holds a Lisp vector that holds the results of decoding
214 the keymaps or alist-of-alists that specify a menu.
215
216 It describes the panes and items within the panes.
217
218 Each pane is described by 3 elements in the vector:
219 t, the pane name, the pane's prefix key.
220 Then follow the pane's items, with 5 elements per item:
221 the item string, the enable flag, the item's value,
222 the definition, and the equivalent keyboard key's description string.
223
224 In some cases, multiple levels of menus may be described.
225 A single vector slot containing nil indicates the start of a submenu.
226 A single vector slot containing lambda indicates the end of a submenu.
227 The submenu follows a menu item which is the way to reach the submenu.
228
229 A single vector slot containing quote indicates that the
230 following items should appear on the right of a dialog box.
231
232 Using a Lisp vector to hold this information while we decode it
233 takes care of protecting all the data from GC. */
234
235 #define MENU_ITEMS_PANE_NAME 1
236 #define MENU_ITEMS_PANE_PREFIX 2
237 #define MENU_ITEMS_PANE_LENGTH 3
238
239 enum menu_item_idx
240 {
241 MENU_ITEMS_ITEM_NAME = 0,
242 MENU_ITEMS_ITEM_ENABLE,
243 MENU_ITEMS_ITEM_VALUE,
244 MENU_ITEMS_ITEM_EQUIV_KEY,
245 MENU_ITEMS_ITEM_DEFINITION,
246 MENU_ITEMS_ITEM_TYPE,
247 MENU_ITEMS_ITEM_SELECTED,
248 MENU_ITEMS_ITEM_HELP,
249 MENU_ITEMS_ITEM_LENGTH
250 };
251
252 static Lisp_Object menu_items;
253
254 /* Number of slots currently allocated in menu_items. */
255 static int menu_items_allocated;
256
257 /* This is the index in menu_items of the first empty slot. */
258 static int menu_items_used;
259
260 /* The number of panes currently recorded in menu_items,
261 excluding those within submenus. */
262 static int menu_items_n_panes;
263
264 /* Current depth within submenus. */
265 static int menu_items_submenu_depth;
266
267 /* Flag which when set indicates a dialog or menu has been posted by
268 Xt on behalf of one of the widget sets. */
269 static int popup_activated_flag;
270
271 /* Index of the next submenu */
272 static int submenu_id;
273
274 static int next_menubar_widget_id;
275
276 /* This is set nonzero after the user activates the menu bar, and set
277 to zero again after the menu bars are redisplayed by prepare_menu_bar.
278 While it is nonzero, all calls to set_frame_menubar go deep.
279
280 I don't understand why this is needed, but it does seem to be
281 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
282
283 int pending_menu_activation;
284 \f
285 /* Initialize the menu_items structure if we haven't already done so.
286 Also mark it as currently empty. */
287
288 static void
289 init_menu_items ()
290 {
291 if (NILP (menu_items))
292 {
293 menu_items_allocated = 60;
294 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
295 }
296
297 menu_items_used = 0;
298 menu_items_n_panes = 0;
299 menu_items_submenu_depth = 0;
300 }
301
302 /* Call at the end of generating the data in menu_items.
303 This fills in the number of items in the last pane. */
304
305 static void
306 finish_menu_items ()
307 {
308 }
309
310 /* Call when finished using the data for the current menu
311 in menu_items. */
312
313 static void
314 discard_menu_items ()
315 {
316 /* Free the structure if it is especially large.
317 Otherwise, hold on to it, to save time. */
318 if (menu_items_allocated > 200)
319 {
320 menu_items = Qnil;
321 menu_items_allocated = 0;
322 }
323 }
324
325 /* Make the menu_items vector twice as large. */
326
327 static void
328 grow_menu_items ()
329 {
330 Lisp_Object old;
331 int old_size = menu_items_allocated;
332 old = menu_items;
333
334 menu_items_allocated *= 2;
335 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
336 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
337 old_size * sizeof (Lisp_Object));
338 }
339
340 /* Begin a submenu. */
341
342 static void
343 push_submenu_start ()
344 {
345 if (menu_items_used + 1 > menu_items_allocated)
346 grow_menu_items ();
347
348 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
349 menu_items_submenu_depth++;
350 }
351
352 /* End a submenu. */
353
354 static void
355 push_submenu_end ()
356 {
357 if (menu_items_used + 1 > menu_items_allocated)
358 grow_menu_items ();
359
360 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
361 menu_items_submenu_depth--;
362 }
363
364 /* Indicate boundary between left and right. */
365
366 static void
367 push_left_right_boundary ()
368 {
369 if (menu_items_used + 1 > menu_items_allocated)
370 grow_menu_items ();
371
372 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
373 }
374
375 /* Start a new menu pane in menu_items.
376 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
377
378 static void
379 push_menu_pane (name, prefix_vec)
380 Lisp_Object name, prefix_vec;
381 {
382 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
383 grow_menu_items ();
384
385 if (menu_items_submenu_depth == 0)
386 menu_items_n_panes++;
387 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
388 XVECTOR (menu_items)->contents[menu_items_used++] = name;
389 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
390 }
391
392 /* Push one menu item into the current pane. NAME is the string to
393 display. ENABLE if non-nil means this item can be selected. KEY
394 is the key generated by choosing this item, or nil if this item
395 doesn't really have a definition. DEF is the definition of this
396 item. EQUIV is the textual description of the keyboard equivalent
397 for this item (or nil if none). TYPE is the type of this menu
398 item, one of nil, `toggle' or `radio'. */
399
400 static void
401 push_menu_item (name, enable, key, def, equiv, type, selected, help)
402 Lisp_Object name, enable, key, def, equiv, type, selected, help;
403 {
404 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
405 grow_menu_items ();
406
407 XVECTOR (menu_items)->contents[menu_items_used++] = name;
408 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
409 XVECTOR (menu_items)->contents[menu_items_used++] = key;
410 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
411 XVECTOR (menu_items)->contents[menu_items_used++] = def;
412 XVECTOR (menu_items)->contents[menu_items_used++] = type;
413 XVECTOR (menu_items)->contents[menu_items_used++] = selected;
414 XVECTOR (menu_items)->contents[menu_items_used++] = help;
415 }
416 \f
417 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
418 and generate menu panes for them in menu_items.
419 If NOTREAL is nonzero,
420 don't bother really computing whether an item is enabled. */
421
422 static void
423 keymap_panes (keymaps, nmaps, notreal)
424 Lisp_Object *keymaps;
425 int nmaps;
426 int notreal;
427 {
428 int mapno;
429
430 init_menu_items ();
431
432 /* Loop over the given keymaps, making a pane for each map.
433 But don't make a pane that is empty--ignore that map instead.
434 P is the number of panes we have made so far. */
435 for (mapno = 0; mapno < nmaps; mapno++)
436 single_keymap_panes (keymaps[mapno],
437 Fkeymap_prompt (keymaps[mapno]), Qnil, notreal, 10);
438
439 finish_menu_items ();
440 }
441
442 /* This is a recursive subroutine of keymap_panes.
443 It handles one keymap, KEYMAP.
444 The other arguments are passed along
445 or point to local variables of the previous function.
446 If NOTREAL is nonzero, only check for equivalent key bindings, don't
447 evaluate expressions in menu items and don't make any menu.
448
449 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
450
451 static void
452 single_keymap_panes (keymap, pane_name, prefix, notreal, maxdepth)
453 Lisp_Object keymap;
454 Lisp_Object pane_name;
455 Lisp_Object prefix;
456 int notreal;
457 int maxdepth;
458 {
459 Lisp_Object pending_maps = Qnil;
460 Lisp_Object tail, item;
461 struct gcpro gcpro1, gcpro2;
462
463 if (maxdepth <= 0)
464 return;
465
466 push_menu_pane (pane_name, prefix);
467
468 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
469 {
470 GCPRO2 (keymap, pending_maps);
471 /* Look at each key binding, and if it is a menu item add it
472 to this menu. */
473 item = XCAR (tail);
474 if (CONSP (item))
475 single_menu_item (XCAR (item), XCDR (item),
476 &pending_maps, notreal, maxdepth);
477 else if (VECTORP (item))
478 {
479 /* Loop over the char values represented in the vector. */
480 int len = XVECTOR (item)->size;
481 int c;
482 for (c = 0; c < len; c++)
483 {
484 Lisp_Object character;
485 XSETFASTINT (character, c);
486 single_menu_item (character, XVECTOR (item)->contents[c],
487 &pending_maps, notreal, maxdepth);
488 }
489 }
490 UNGCPRO;
491 }
492
493 /* Process now any submenus which want to be panes at this level. */
494 while (!NILP (pending_maps))
495 {
496 Lisp_Object elt, eltcdr, string;
497 elt = Fcar (pending_maps);
498 eltcdr = XCDR (elt);
499 string = XCAR (eltcdr);
500 /* We no longer discard the @ from the beginning of the string here.
501 Instead, we do this in mac_menu_show. */
502 single_keymap_panes (Fcar (elt), string,
503 XCDR (eltcdr), notreal, maxdepth - 1);
504 pending_maps = Fcdr (pending_maps);
505 }
506 }
507 \f
508 /* This is a subroutine of single_keymap_panes that handles one
509 keymap entry.
510 KEY is a key in a keymap and ITEM is its binding.
511 PENDING_MAPS_PTR points to a list of keymaps waiting to be made into
512 separate panes.
513 If NOTREAL is nonzero, only check for equivalent key bindings, don't
514 evaluate expressions in menu items and don't make any menu.
515 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
516
517 static void
518 single_menu_item (key, item, pending_maps_ptr, notreal, maxdepth)
519 Lisp_Object key, item;
520 Lisp_Object *pending_maps_ptr;
521 int maxdepth, notreal;
522 {
523 Lisp_Object map, item_string, enabled;
524 struct gcpro gcpro1, gcpro2;
525 int res;
526
527 /* Parse the menu item and leave the result in item_properties. */
528 GCPRO2 (key, item);
529 res = parse_menu_item (item, notreal, 0);
530 UNGCPRO;
531 if (!res)
532 return; /* Not a menu item. */
533
534 map = XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP];
535
536 if (notreal)
537 {
538 /* We don't want to make a menu, just traverse the keymaps to
539 precompute equivalent key bindings. */
540 if (!NILP (map))
541 single_keymap_panes (map, Qnil, key, 1, maxdepth - 1);
542 return;
543 }
544
545 enabled = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
546 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
547
548 if (!NILP (map) && SREF (item_string, 0) == '@')
549 {
550 if (!NILP (enabled))
551 /* An enabled separate pane. Remember this to handle it later. */
552 *pending_maps_ptr = Fcons (Fcons (map, Fcons (item_string, key)),
553 *pending_maps_ptr);
554 return;
555 }
556
557 push_menu_item (item_string, enabled, key,
558 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF],
559 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ],
560 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE],
561 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED],
562 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]);
563
564 /* Display a submenu using the toolkit. */
565 if (! (NILP (map) || NILP (enabled)))
566 {
567 push_submenu_start ();
568 single_keymap_panes (map, Qnil, key, 0, maxdepth - 1);
569 push_submenu_end ();
570 }
571 }
572 \f
573 /* Push all the panes and items of a menu described by the
574 alist-of-alists MENU.
575 This handles old-fashioned calls to x-popup-menu. */
576
577 static void
578 list_of_panes (menu)
579 Lisp_Object menu;
580 {
581 Lisp_Object tail;
582
583 init_menu_items ();
584
585 for (tail = menu; !NILP (tail); tail = Fcdr (tail))
586 {
587 Lisp_Object elt, pane_name, pane_data;
588 elt = Fcar (tail);
589 pane_name = Fcar (elt);
590 CHECK_STRING (pane_name);
591 push_menu_pane (pane_name, Qnil);
592 pane_data = Fcdr (elt);
593 CHECK_CONS (pane_data);
594 list_of_items (pane_data);
595 }
596
597 finish_menu_items ();
598 }
599
600 /* Push the items in a single pane defined by the alist PANE. */
601
602 static void
603 list_of_items (pane)
604 Lisp_Object pane;
605 {
606 Lisp_Object tail, item, item1;
607
608 for (tail = pane; !NILP (tail); tail = Fcdr (tail))
609 {
610 item = Fcar (tail);
611 if (STRINGP (item))
612 push_menu_item (item, Qnil, Qnil, Qt, Qnil, Qnil, Qnil, Qnil);
613 else if (NILP (item))
614 push_left_right_boundary ();
615 else
616 {
617 CHECK_CONS (item);
618 item1 = Fcar (item);
619 CHECK_STRING (item1);
620 push_menu_item (item1, Qt, Fcdr (item), Qt, Qnil, Qnil, Qnil, Qnil);
621 }
622 }
623 }
624 \f
625 DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
626 doc: /* Pop up a deck-of-cards menu and return user's selection.
627 POSITION is a position specification. This is either a mouse button
628 event or a list ((XOFFSET YOFFSET) WINDOW) where XOFFSET and YOFFSET
629 are positions in pixels from the top left corner of WINDOW's frame
630 \(WINDOW may be a frame object instead of a window). This controls the
631 position of the center of the first line in the first pane of the
632 menu, not the top left of the menu as a whole. If POSITION is t, it
633 means to use the current mouse position.
634
635 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.
636 The menu items come from key bindings that have a menu string as well as
637 a definition; actually, the \"definition\" in such a key binding looks like
638 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into
639 the keymap as a top-level element.
640
641 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.
642 Otherwise, REAL-DEFINITION should be a valid key binding definition.
643
644 You can also use a list of keymaps as MENU. Then each keymap makes a
645 separate pane. When MENU is a keymap or a list of keymaps, the return
646 value is a list of events.
647
648 Alternatively, you can specify a menu of multiple panes with a list of
649 the form (TITLE PANE1 PANE2...), where each pane is a list of
650 form (TITLE ITEM1 ITEM2...).
651 Each ITEM is normally a cons cell (STRING . VALUE); but a string can
652 appear as an item--that makes a nonselectable line in the menu.
653 With this form of menu, the return value is VALUE from the chosen item.
654
655 If POSITION is nil, don't display the menu at all, just precalculate the
656 cached information about equivalent key sequences. */)
657 (position, menu)
658 Lisp_Object position, menu;
659 {
660 Lisp_Object keymap, tem;
661 int xpos = 0, ypos = 0;
662 Lisp_Object title;
663 char *error_name;
664 Lisp_Object selection;
665 FRAME_PTR f = NULL;
666 Lisp_Object x, y, window;
667 int keymaps = 0;
668 int for_click = 0;
669 struct gcpro gcpro1;
670
671 #ifdef HAVE_MENUS
672 if (! NILP (position))
673 {
674 check_mac ();
675
676 /* Decode the first argument: find the window and the coordinates. */
677 if (EQ (position, Qt)
678 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
679 || EQ (XCAR (position), Qtool_bar))))
680 {
681 /* Use the mouse's current position. */
682 FRAME_PTR new_f = SELECTED_FRAME ();
683 Lisp_Object bar_window;
684 enum scroll_bar_part part;
685 unsigned long time;
686
687 if (mouse_position_hook)
688 (*mouse_position_hook) (&new_f, 1, &bar_window,
689 &part, &x, &y, &time);
690 if (new_f != 0)
691 XSETFRAME (window, new_f);
692 else
693 {
694 window = selected_window;
695 XSETFASTINT (x, 0);
696 XSETFASTINT (y, 0);
697 }
698 }
699 else
700 {
701 tem = Fcar (position);
702 if (CONSP (tem))
703 {
704 window = Fcar (Fcdr (position));
705 x = Fcar (tem);
706 y = Fcar (Fcdr (tem));
707 }
708 else
709 {
710 for_click = 1;
711 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
712 window = Fcar (tem); /* POSN_WINDOW (tem) */
713 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
714 x = Fcar (tem);
715 y = Fcdr (tem);
716 }
717 }
718
719 CHECK_NUMBER (x);
720 CHECK_NUMBER (y);
721
722 /* Decode where to put the menu. */
723
724 if (FRAMEP (window))
725 {
726 f = XFRAME (window);
727 xpos = 0;
728 ypos = 0;
729 }
730 else if (WINDOWP (window))
731 {
732 CHECK_LIVE_WINDOW (window);
733 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
734
735 xpos = WINDOW_LEFT_EDGE_X (XWINDOW (window));
736 ypos = WINDOW_TOP_EDGE_Y (XWINDOW (window));
737 }
738 else
739 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
740 but I don't want to make one now. */
741 CHECK_WINDOW (window);
742
743 xpos += XINT (x);
744 ypos += XINT (y);
745
746 XSETFRAME (Vmenu_updating_frame, f);
747 }
748 Vmenu_updating_frame = Qnil;
749 #endif /* HAVE_MENUS */
750
751 title = Qnil;
752 GCPRO1 (title);
753
754 /* Decode the menu items from what was specified. */
755
756 keymap = get_keymap (menu, 0, 0);
757 if (CONSP (keymap))
758 {
759 /* We were given a keymap. Extract menu info from the keymap. */
760 Lisp_Object prompt;
761
762 /* Extract the detailed info to make one pane. */
763 keymap_panes (&menu, 1, NILP (position));
764
765 /* Search for a string appearing directly as an element of the keymap.
766 That string is the title of the menu. */
767 prompt = Fkeymap_prompt (keymap);
768 if (NILP (title) && !NILP (prompt))
769 title = prompt;
770
771 /* Make that be the pane title of the first pane. */
772 if (!NILP (prompt) && menu_items_n_panes >= 0)
773 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
774
775 keymaps = 1;
776 }
777 else if (CONSP (menu) && KEYMAPP (XCAR (menu)))
778 {
779 /* We were given a list of keymaps. */
780 int nmaps = XFASTINT (Flength (menu));
781 Lisp_Object *maps
782 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
783 int i;
784
785 title = Qnil;
786
787 /* The first keymap that has a prompt string
788 supplies the menu title. */
789 for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
790 {
791 Lisp_Object prompt;
792
793 maps[i++] = keymap = get_keymap (Fcar (tem), 1, 0);
794
795 prompt = Fkeymap_prompt (keymap);
796 if (NILP (title) && !NILP (prompt))
797 title = prompt;
798 }
799
800 /* Extract the detailed info to make one pane. */
801 keymap_panes (maps, nmaps, NILP (position));
802
803 /* Make the title be the pane title of the first pane. */
804 if (!NILP (title) && menu_items_n_panes >= 0)
805 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
806
807 keymaps = 1;
808 }
809 else
810 {
811 /* We were given an old-fashioned menu. */
812 title = Fcar (menu);
813 CHECK_STRING (title);
814
815 list_of_panes (Fcdr (menu));
816
817 keymaps = 0;
818 }
819
820 if (NILP (position))
821 {
822 discard_menu_items ();
823 UNGCPRO;
824 return Qnil;
825 }
826
827 #ifdef HAVE_MENUS
828 /* Display them in a menu. */
829 BLOCK_INPUT;
830
831 selection = mac_menu_show (f, xpos, ypos, for_click,
832 keymaps, title, &error_name);
833 UNBLOCK_INPUT;
834
835 discard_menu_items ();
836
837 UNGCPRO;
838 #endif /* HAVE_MENUS */
839
840 if (error_name) error (error_name);
841 return selection;
842 }
843
844 #ifdef HAVE_MENUS
845
846 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
847 doc: /* Pop up a dialog box and return user's selection.
848 POSITION specifies which frame to use.
849 This is normally a mouse button event or a window or frame.
850 If POSITION is t, it means to use the frame the mouse is on.
851 The dialog box appears in the middle of the specified frame.
852
853 CONTENTS specifies the alternatives to display in the dialog box.
854 It is a list of the form (TITLE ITEM1 ITEM2...).
855 Each ITEM is a cons cell (STRING . VALUE).
856 The return value is VALUE from the chosen item.
857
858 An ITEM may also be just a string--that makes a nonselectable item.
859 An ITEM may also be nil--that means to put all preceding items
860 on the left of the dialog box and all following items on the right.
861 \(By default, approximately half appear on each side.) */)
862 (position, contents)
863 Lisp_Object position, contents;
864 {
865 FRAME_PTR f = NULL;
866 Lisp_Object window;
867
868 check_mac ();
869
870 /* Decode the first argument: find the window or frame to use. */
871 if (EQ (position, Qt)
872 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
873 || EQ (XCAR (position), Qtool_bar))))
874 {
875 #if 0 /* Using the frame the mouse is on may not be right. */
876 /* Use the mouse's current position. */
877 FRAME_PTR new_f = SELECTED_FRAME ();
878 Lisp_Object bar_window;
879 enum scroll_bar_part part;
880 unsigned long time;
881 Lisp_Object x, y;
882
883 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
884
885 if (new_f != 0)
886 XSETFRAME (window, new_f);
887 else
888 window = selected_window;
889 #endif
890 window = selected_window;
891 }
892 else if (CONSP (position))
893 {
894 Lisp_Object tem;
895 tem = Fcar (position);
896 if (CONSP (tem))
897 window = Fcar (Fcdr (position));
898 else
899 {
900 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
901 window = Fcar (tem); /* POSN_WINDOW (tem) */
902 }
903 }
904 else if (WINDOWP (position) || FRAMEP (position))
905 window = position;
906 else
907 window = Qnil;
908
909 /* Decode where to put the menu. */
910
911 if (FRAMEP (window))
912 f = XFRAME (window);
913 else if (WINDOWP (window))
914 {
915 CHECK_LIVE_WINDOW (window);
916 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
917 }
918 else
919 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
920 but I don't want to make one now. */
921 CHECK_WINDOW (window);
922
923 #ifndef HAVE_DIALOGS
924 /* Display a menu with these alternatives
925 in the middle of frame F. */
926 {
927 Lisp_Object x, y, frame, newpos;
928 XSETFRAME (frame, f);
929 XSETINT (x, x_pixel_width (f) / 2);
930 XSETINT (y, x_pixel_height (f) / 2);
931 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
932
933 return Fx_popup_menu (newpos,
934 Fcons (Fcar (contents), Fcons (contents, Qnil)));
935 }
936 #else /* HAVE_DIALOGS */
937 {
938 Lisp_Object title;
939 char *error_name;
940 Lisp_Object selection;
941
942 /* Decode the dialog items from what was specified. */
943 title = Fcar (contents);
944 CHECK_STRING (title);
945
946 list_of_panes (Fcons (contents, Qnil));
947
948 /* Display them in a dialog box. */
949 BLOCK_INPUT;
950 selection = mac_dialog_show (f, 0, title, &error_name);
951 UNBLOCK_INPUT;
952
953 discard_menu_items ();
954
955 if (error_name) error (error_name);
956 return selection;
957 }
958 #endif /* HAVE_DIALOGS */
959 }
960
961 /* Activate the menu bar of frame F.
962 This is called from keyboard.c when it gets the
963 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
964
965 To activate the menu bar, we signal to the input thread that it can
966 return from the WM_INITMENU message, allowing the normal Windows
967 processing of the menus.
968
969 But first we recompute the menu bar contents (the whole tree).
970
971 This way we can safely execute Lisp code. */
972
973 void
974 x_activate_menubar (f)
975 FRAME_PTR f;
976 {
977 SInt32 menu_choice;
978 extern Point saved_menu_event_location;
979
980 set_frame_menubar (f, 0, 1);
981 BLOCK_INPUT;
982
983 menu_choice = MenuSelect (saved_menu_event_location);
984 do_menu_choice (menu_choice);
985
986 UNBLOCK_INPUT;
987 }
988
989 /* This callback is called from the menu bar pulldown menu
990 when the user makes a selection.
991 Figure out what the user chose
992 and put the appropriate events into the keyboard buffer. */
993
994 void
995 menubar_selection_callback (FRAME_PTR f, int client_data)
996 {
997 Lisp_Object prefix, entry;
998 Lisp_Object vector;
999 Lisp_Object *subprefix_stack;
1000 int submenu_depth = 0;
1001 int i;
1002
1003 if (!f)
1004 return;
1005 entry = Qnil;
1006 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object));
1007 vector = f->menu_bar_vector;
1008 prefix = Qnil;
1009 i = 0;
1010 while (i < f->menu_bar_items_used)
1011 {
1012 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1013 {
1014 subprefix_stack[submenu_depth++] = prefix;
1015 prefix = entry;
1016 i++;
1017 }
1018 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1019 {
1020 prefix = subprefix_stack[--submenu_depth];
1021 i++;
1022 }
1023 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1024 {
1025 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
1026 i += MENU_ITEMS_PANE_LENGTH;
1027 }
1028 else
1029 {
1030 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
1031 /* The EMACS_INT cast avoids a warning. There's no problem
1032 as long as pointers have enough bits to hold small integers. */
1033 if ((int) (EMACS_INT) client_data == i)
1034 {
1035 int j;
1036 struct input_event buf;
1037 Lisp_Object frame;
1038
1039 XSETFRAME (frame, f);
1040 buf.kind = MENU_BAR_EVENT;
1041 buf.frame_or_window = frame;
1042 buf.arg = frame;
1043 kbd_buffer_store_event (&buf);
1044
1045 for (j = 0; j < submenu_depth; j++)
1046 if (!NILP (subprefix_stack[j]))
1047 {
1048 buf.kind = MENU_BAR_EVENT;
1049 buf.frame_or_window = frame;
1050 buf.arg = subprefix_stack[j];
1051 kbd_buffer_store_event (&buf);
1052 }
1053
1054 if (!NILP (prefix))
1055 {
1056 buf.kind = MENU_BAR_EVENT;
1057 buf.frame_or_window = frame;
1058 buf.arg = prefix;
1059 kbd_buffer_store_event (&buf);
1060 }
1061
1062 buf.kind = MENU_BAR_EVENT;
1063 buf.frame_or_window = frame;
1064 buf.arg = entry;
1065 kbd_buffer_store_event (&buf);
1066
1067 f->output_data.mac->menu_command_in_progress = 0;
1068 f->output_data.mac->menubar_active = 0;
1069 return;
1070 }
1071 i += MENU_ITEMS_ITEM_LENGTH;
1072 }
1073 }
1074 f->output_data.mac->menu_command_in_progress = 0;
1075 f->output_data.mac->menubar_active = 0;
1076 }
1077
1078 /* Allocate a widget_value, blocking input. */
1079
1080 widget_value *
1081 xmalloc_widget_value ()
1082 {
1083 widget_value *value;
1084
1085 BLOCK_INPUT;
1086 value = malloc_widget_value ();
1087 UNBLOCK_INPUT;
1088
1089 return value;
1090 }
1091
1092 /* This recursively calls free_widget_value on the tree of widgets.
1093 It must free all data that was malloc'ed for these widget_values.
1094 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1095 must be left alone. */
1096
1097 void
1098 free_menubar_widget_value_tree (wv)
1099 widget_value *wv;
1100 {
1101 if (! wv) return;
1102
1103 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1104
1105 if (wv->contents && (wv->contents != (widget_value*)1))
1106 {
1107 free_menubar_widget_value_tree (wv->contents);
1108 wv->contents = (widget_value *) 0xDEADBEEF;
1109 }
1110 if (wv->next)
1111 {
1112 free_menubar_widget_value_tree (wv->next);
1113 wv->next = (widget_value *) 0xDEADBEEF;
1114 }
1115 BLOCK_INPUT;
1116 free_widget_value (wv);
1117 UNBLOCK_INPUT;
1118 }
1119 \f
1120 /* Return a tree of widget_value structures for a menu bar item
1121 whose event type is ITEM_KEY (with string ITEM_NAME)
1122 and whose contents come from the list of keymaps MAPS. */
1123
1124 static widget_value *
1125 single_submenu (item_key, item_name, maps)
1126 Lisp_Object item_key, item_name, maps;
1127 {
1128 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1129 int i;
1130 int submenu_depth = 0;
1131 Lisp_Object length;
1132 int len;
1133 Lisp_Object *mapvec;
1134 widget_value **submenu_stack;
1135 int previous_items = menu_items_used;
1136 int top_level_items = 0;
1137
1138 length = Flength (maps);
1139 len = XINT (length);
1140
1141 /* Convert the list MAPS into a vector MAPVEC. */
1142 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1143 for (i = 0; i < len; i++)
1144 {
1145 mapvec[i] = Fcar (maps);
1146 maps = Fcdr (maps);
1147 }
1148
1149 menu_items_n_panes = 0;
1150
1151 /* Loop over the given keymaps, making a pane for each map.
1152 But don't make a pane that is empty--ignore that map instead. */
1153 for (i = 0; i < len; i++)
1154 {
1155 if (SYMBOLP (mapvec[i])
1156 || (CONSP (mapvec[i]) && !KEYMAPP (mapvec[i])))
1157 {
1158 /* Here we have a command at top level in the menu bar
1159 as opposed to a submenu. */
1160 top_level_items = 1;
1161 push_menu_pane (Qnil, Qnil);
1162 push_menu_item (item_name, Qt, item_key, mapvec[i],
1163 Qnil, Qnil, Qnil, Qnil);
1164 }
1165 else
1166 single_keymap_panes (mapvec[i], item_name, item_key, 0, 10);
1167 }
1168
1169 /* Create a tree of widget_value objects
1170 representing the panes and their items. */
1171
1172 submenu_stack
1173 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1174 wv = xmalloc_widget_value ();
1175 wv->name = "menu";
1176 wv->value = 0;
1177 wv->enabled = 1;
1178 wv->button_type = BUTTON_TYPE_NONE;
1179 wv->help = Qnil;
1180 first_wv = wv;
1181 save_wv = 0;
1182 prev_wv = 0;
1183
1184 /* Loop over all panes and items made during this call
1185 and construct a tree of widget_value objects.
1186 Ignore the panes and items made by previous calls to
1187 single_submenu, even though those are also in menu_items. */
1188 i = previous_items;
1189 while (i < menu_items_used)
1190 {
1191 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1192 {
1193 submenu_stack[submenu_depth++] = save_wv;
1194 save_wv = prev_wv;
1195 prev_wv = 0;
1196 i++;
1197 }
1198 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1199 {
1200 prev_wv = save_wv;
1201 save_wv = submenu_stack[--submenu_depth];
1202 i++;
1203 }
1204 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1205 && submenu_depth != 0)
1206 i += MENU_ITEMS_PANE_LENGTH;
1207 /* Ignore a nil in the item list.
1208 It's meaningful only for dialog boxes. */
1209 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1210 i += 1;
1211 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1212 {
1213 /* Create a new pane. */
1214 Lisp_Object pane_name, prefix;
1215 char *pane_string;
1216
1217 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1218 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1219
1220 #ifndef HAVE_MULTILINGUAL_MENU
1221 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1222 {
1223 pane_name = ENCODE_SYSTEM (pane_name);
1224 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
1225 }
1226 #endif
1227 pane_string = (NILP (pane_name)
1228 ? "" : (char *) SDATA (pane_name));
1229 /* If there is just one top-level pane, put all its items directly
1230 under the top-level menu. */
1231 if (menu_items_n_panes == 1)
1232 pane_string = "";
1233
1234 /* If the pane has a meaningful name,
1235 make the pane a top-level menu item
1236 with its items as a submenu beneath it. */
1237 if (strcmp (pane_string, ""))
1238 {
1239 wv = xmalloc_widget_value ();
1240 if (save_wv)
1241 save_wv->next = wv;
1242 else
1243 first_wv->contents = wv;
1244 wv->name = pane_string;
1245 /* Ignore the @ that means "separate pane".
1246 This is a kludge, but this isn't worth more time. */
1247 if (!NILP (prefix) && wv->name[0] == '@')
1248 wv->name++;
1249 wv->value = 0;
1250 wv->enabled = 1;
1251 wv->button_type = BUTTON_TYPE_NONE;
1252 wv->help = Qnil;
1253 }
1254 save_wv = wv;
1255 prev_wv = 0;
1256 i += MENU_ITEMS_PANE_LENGTH;
1257 }
1258 else
1259 {
1260 /* Create a new item within current pane. */
1261 Lisp_Object item_name, enable, descrip, def, type, selected;
1262 Lisp_Object help;
1263
1264 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1265 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1266 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1267 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
1268 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
1269 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
1270 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
1271
1272 #ifndef HAVE_MULTILINGUAL_MENU
1273 if (STRING_MULTIBYTE (item_name))
1274 {
1275 item_name = ENCODE_SYSTEM (item_name);
1276 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
1277 }
1278
1279 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
1280 {
1281 descrip = ENCODE_SYSTEM (descrip);
1282 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
1283 }
1284 #endif /* not HAVE_MULTILINGUAL_MENU */
1285
1286 wv = xmalloc_widget_value ();
1287 if (prev_wv)
1288 prev_wv->next = wv;
1289 else
1290 save_wv->contents = wv;
1291
1292 wv->name = (char *) SDATA (item_name);
1293 if (!NILP (descrip))
1294 wv->key = (char *) SDATA (descrip);
1295 wv->value = 0;
1296 /* The EMACS_INT cast avoids a warning. There's no problem
1297 as long as pointers have enough bits to hold small integers. */
1298 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
1299 wv->enabled = !NILP (enable);
1300
1301 if (NILP (type))
1302 wv->button_type = BUTTON_TYPE_NONE;
1303 else if (EQ (type, QCradio))
1304 wv->button_type = BUTTON_TYPE_RADIO;
1305 else if (EQ (type, QCtoggle))
1306 wv->button_type = BUTTON_TYPE_TOGGLE;
1307 else
1308 abort ();
1309
1310 wv->selected = !NILP (selected);
1311 if (!STRINGP (help))
1312 help = Qnil;
1313
1314 wv->help = help;
1315
1316 prev_wv = wv;
1317
1318 i += MENU_ITEMS_ITEM_LENGTH;
1319 }
1320 }
1321
1322 /* If we have just one "menu item"
1323 that was originally a button, return it by itself. */
1324 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1325 {
1326 wv = first_wv->contents;
1327 free_widget_value (first_wv);
1328 return wv;
1329 }
1330
1331 return first_wv;
1332 }
1333 \f
1334 /* Set the contents of the menubar widgets of frame F.
1335 The argument FIRST_TIME is currently ignored;
1336 it is set the first time this is called, from initialize_frame_menubar. */
1337
1338 void
1339 set_frame_menubar (f, first_time, deep_p)
1340 FRAME_PTR f;
1341 int first_time;
1342 int deep_p;
1343 {
1344 int menubar_widget = f->output_data.mac->menubar_widget;
1345 Lisp_Object items;
1346 widget_value *wv, *first_wv, *prev_wv = 0;
1347 int i;
1348
1349 /* We must not change the menubar when actually in use. */
1350 if (f->output_data.mac->menubar_active)
1351 return;
1352
1353 XSETFRAME (Vmenu_updating_frame, f);
1354
1355 if (! menubar_widget)
1356 deep_p = 1;
1357 else if (pending_menu_activation && !deep_p)
1358 deep_p = 1;
1359
1360 wv = xmalloc_widget_value ();
1361 wv->name = "menubar";
1362 wv->value = 0;
1363 wv->enabled = 1;
1364 wv->button_type = BUTTON_TYPE_NONE;
1365 wv->help = Qnil;
1366 first_wv = wv;
1367
1368 if (deep_p)
1369 {
1370 /* Make a widget-value tree representing the entire menu trees. */
1371
1372 struct buffer *prev = current_buffer;
1373 Lisp_Object buffer;
1374 int specpdl_count = SPECPDL_INDEX ();
1375 int previous_menu_items_used = f->menu_bar_items_used;
1376 Lisp_Object *previous_items
1377 = (Lisp_Object *) alloca (previous_menu_items_used
1378 * sizeof (Lisp_Object));
1379
1380 /* If we are making a new widget, its contents are empty,
1381 do always reinitialize them. */
1382 if (! menubar_widget)
1383 previous_menu_items_used = 0;
1384
1385 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
1386 specbind (Qinhibit_quit, Qt);
1387 /* Don't let the debugger step into this code
1388 because it is not reentrant. */
1389 specbind (Qdebug_on_next_call, Qnil);
1390
1391 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1392 if (NILP (Voverriding_local_map_menu_flag))
1393 {
1394 specbind (Qoverriding_terminal_local_map, Qnil);
1395 specbind (Qoverriding_local_map, Qnil);
1396 }
1397
1398 set_buffer_internal_1 (XBUFFER (buffer));
1399
1400 /* Run the Lucid hook. */
1401 safe_run_hooks (Qactivate_menubar_hook);
1402 /* If it has changed current-menubar from previous value,
1403 really recompute the menubar from the value. */
1404 if (! NILP (Vlucid_menu_bar_dirty_flag))
1405 call0 (Qrecompute_lucid_menubar);
1406 safe_run_hooks (Qmenu_bar_update_hook);
1407 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1408
1409 items = FRAME_MENU_BAR_ITEMS (f);
1410
1411 inhibit_garbage_collection ();
1412
1413 /* Save the frame's previous menu bar contents data. */
1414 if (previous_menu_items_used)
1415 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
1416 previous_menu_items_used * sizeof (Lisp_Object));
1417
1418 /* Fill in the current menu bar contents. */
1419 menu_items = f->menu_bar_vector;
1420 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
1421 init_menu_items ();
1422 for (i = 0; i < XVECTOR (items)->size; i += 4)
1423 {
1424 Lisp_Object key, string, maps;
1425
1426 key = XVECTOR (items)->contents[i];
1427 string = XVECTOR (items)->contents[i + 1];
1428 maps = XVECTOR (items)->contents[i + 2];
1429 if (NILP (string))
1430 break;
1431
1432 wv = single_submenu (key, string, maps);
1433 if (prev_wv)
1434 prev_wv->next = wv;
1435 else
1436 first_wv->contents = wv;
1437 /* Don't set wv->name here; GC during the loop might relocate it. */
1438 wv->enabled = 1;
1439 wv->button_type = BUTTON_TYPE_NONE;
1440 prev_wv = wv;
1441 }
1442
1443 finish_menu_items ();
1444
1445 set_buffer_internal_1 (prev);
1446 unbind_to (specpdl_count, Qnil);
1447
1448 /* If there has been no change in the Lisp-level contents
1449 of the menu bar, skip redisplaying it. Just exit. */
1450
1451 for (i = 0; i < previous_menu_items_used; i++)
1452 if (menu_items_used == i
1453 || (!Fequal (previous_items[i], XVECTOR (menu_items)->contents[i])))
1454 break;
1455 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
1456 {
1457 free_menubar_widget_value_tree (first_wv);
1458 menu_items = Qnil;
1459
1460 return;
1461 }
1462
1463 /* Now GC cannot happen during the lifetime of the widget_value,
1464 so it's safe to store data from a Lisp_String, as long as
1465 local copies are made when the actual menu is created.
1466 Windows takes care of this for normal string items, but
1467 not for owner-drawn items or additional item-info. */
1468 wv = first_wv->contents;
1469 for (i = 0; i < XVECTOR (items)->size; i += 4)
1470 {
1471 Lisp_Object string;
1472 string = XVECTOR (items)->contents[i + 1];
1473 if (NILP (string))
1474 break;
1475 wv->name = (char *) SDATA (string);
1476 wv = wv->next;
1477 }
1478
1479 f->menu_bar_vector = menu_items;
1480 f->menu_bar_items_used = menu_items_used;
1481 menu_items = Qnil;
1482 }
1483 else
1484 {
1485 /* Make a widget-value tree containing
1486 just the top level menu bar strings. */
1487
1488 items = FRAME_MENU_BAR_ITEMS (f);
1489 for (i = 0; i < XVECTOR (items)->size; i += 4)
1490 {
1491 Lisp_Object string;
1492
1493 string = XVECTOR (items)->contents[i + 1];
1494 if (NILP (string))
1495 break;
1496
1497 wv = xmalloc_widget_value ();
1498 wv->name = (char *) SDATA (string);
1499 wv->value = 0;
1500 wv->enabled = 1;
1501 wv->button_type = BUTTON_TYPE_NONE;
1502 wv->help = Qnil;
1503 /* This prevents lwlib from assuming this
1504 menu item is really supposed to be empty. */
1505 /* The EMACS_INT cast avoids a warning.
1506 This value just has to be different from small integers. */
1507 wv->call_data = (void *) (EMACS_INT) (-1);
1508
1509 if (prev_wv)
1510 prev_wv->next = wv;
1511 else
1512 first_wv->contents = wv;
1513 prev_wv = wv;
1514 }
1515
1516 /* Forget what we thought we knew about what is in the
1517 detailed contents of the menu bar menus.
1518 Changing the top level always destroys the contents. */
1519 f->menu_bar_items_used = 0;
1520 }
1521
1522 /* Create or update the menu bar widget. */
1523
1524 BLOCK_INPUT;
1525
1526 /* Non-null value to indicate menubar has already been "created". */
1527 f->output_data.mac->menubar_widget = 1;
1528
1529 {
1530 int i = MIN_MENU_ID;
1531 MenuHandle menu = GetMenuHandle (i);
1532 while (menu != NULL)
1533 {
1534 DeleteMenu (i);
1535 DisposeMenu (menu);
1536 menu = GetMenuHandle (++i);
1537 }
1538
1539 i = MIN_SUBMENU_ID;
1540 menu = GetMenuHandle (i);
1541 while (menu != NULL)
1542 {
1543 DeleteMenu (i);
1544 DisposeMenu (menu);
1545 menu = GetMenuHandle (++i);
1546 }
1547 }
1548
1549 fill_menubar (first_wv->contents);
1550
1551 DrawMenuBar ();
1552
1553 free_menubar_widget_value_tree (first_wv);
1554
1555 UNBLOCK_INPUT;
1556 }
1557
1558 /* Called from Fx_create_frame to create the initial menubar of a frame
1559 before it is mapped, so that the window is mapped with the menubar already
1560 there instead of us tacking it on later and thrashing the window after it
1561 is visible. */
1562
1563 void
1564 initialize_frame_menubar (f)
1565 FRAME_PTR f;
1566 {
1567 /* This function is called before the first chance to redisplay
1568 the frame. It has to be, so the frame will have the right size. */
1569 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1570 set_frame_menubar (f, 1, 1);
1571 }
1572
1573 /* Get rid of the menu bar of frame F, and free its storage.
1574 This is used when deleting a frame, and when turning off the menu bar. */
1575
1576 void
1577 free_frame_menubar (f)
1578 FRAME_PTR f;
1579 {
1580 f->output_data.mac->menubar_widget = NULL;
1581 }
1582
1583 \f
1584 /* mac_menu_show actually displays a menu using the panes and items in
1585 menu_items and returns the value selected from it; we assume input
1586 is blocked by the caller. */
1587
1588 /* F is the frame the menu is for.
1589 X and Y are the frame-relative specified position,
1590 relative to the inside upper left corner of the frame F.
1591 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
1592 KEYMAPS is 1 if this menu was specified with keymaps;
1593 in that case, we return a list containing the chosen item's value
1594 and perhaps also the pane's prefix.
1595 TITLE is the specified menu title.
1596 ERROR is a place to store an error message string in case of failure.
1597 (We return nil on failure, but the value doesn't actually matter.) */
1598
1599 static Lisp_Object
1600 mac_menu_show (f, x, y, for_click, keymaps, title, error)
1601 FRAME_PTR f;
1602 int x;
1603 int y;
1604 int for_click;
1605 int keymaps;
1606 Lisp_Object title;
1607 char **error;
1608 {
1609 int i;
1610 UInt32 refcon;
1611 int menu_item_choice;
1612 int menu_item_selection;
1613 MenuHandle menu;
1614 Point pos;
1615 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1616 widget_value **submenu_stack
1617 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1618 Lisp_Object *subprefix_stack
1619 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1620 int submenu_depth = 0;
1621 int first_pane;
1622
1623 *error = NULL;
1624
1625 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1626 {
1627 *error = "Empty menu";
1628 return Qnil;
1629 }
1630
1631 /* Create a tree of widget_value objects
1632 representing the panes and their items. */
1633 wv = xmalloc_widget_value ();
1634 wv->name = "menu";
1635 wv->value = 0;
1636 wv->enabled = 1;
1637 wv->button_type = BUTTON_TYPE_NONE;
1638 wv->help = Qnil;
1639 first_wv = wv;
1640 first_pane = 1;
1641
1642 /* Loop over all panes and items, filling in the tree. */
1643 i = 0;
1644 while (i < menu_items_used)
1645 {
1646 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1647 {
1648 submenu_stack[submenu_depth++] = save_wv;
1649 save_wv = prev_wv;
1650 prev_wv = 0;
1651 first_pane = 1;
1652 i++;
1653 }
1654 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1655 {
1656 prev_wv = save_wv;
1657 save_wv = submenu_stack[--submenu_depth];
1658 first_pane = 0;
1659 i++;
1660 }
1661 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1662 && submenu_depth != 0)
1663 i += MENU_ITEMS_PANE_LENGTH;
1664 /* Ignore a nil in the item list.
1665 It's meaningful only for dialog boxes. */
1666 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1667 i += 1;
1668 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1669 {
1670 /* Create a new pane. */
1671 Lisp_Object pane_name, prefix;
1672 char *pane_string;
1673 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
1674 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
1675 #ifndef HAVE_MULTILINGUAL_MENU
1676 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1677 {
1678 pane_name = ENCODE_SYSTEM (pane_name);
1679 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
1680 }
1681 #endif
1682 pane_string = (NILP (pane_name)
1683 ? "" : (char *) SDATA (pane_name));
1684 /* If there is just one top-level pane, put all its items directly
1685 under the top-level menu. */
1686 if (menu_items_n_panes == 1)
1687 pane_string = "";
1688
1689 /* If the pane has a meaningful name,
1690 make the pane a top-level menu item
1691 with its items as a submenu beneath it. */
1692 if (!keymaps && strcmp (pane_string, ""))
1693 {
1694 wv = xmalloc_widget_value ();
1695 if (save_wv)
1696 save_wv->next = wv;
1697 else
1698 first_wv->contents = wv;
1699 wv->name = pane_string;
1700 if (keymaps && !NILP (prefix))
1701 wv->name++;
1702 wv->value = 0;
1703 wv->enabled = 1;
1704 wv->button_type = BUTTON_TYPE_NONE;
1705 wv->help = Qnil;
1706 save_wv = wv;
1707 prev_wv = 0;
1708 }
1709 else if (first_pane)
1710 {
1711 save_wv = wv;
1712 prev_wv = 0;
1713 }
1714 first_pane = 0;
1715 i += MENU_ITEMS_PANE_LENGTH;
1716 }
1717 else
1718 {
1719 /* Create a new item within current pane. */
1720 Lisp_Object item_name, enable, descrip, def, type, selected, help;
1721
1722 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1723 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1724 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1725 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
1726 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
1727 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
1728 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
1729
1730 #ifndef HAVE_MULTILINGUAL_MENU
1731 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
1732 {
1733 item_name = ENCODE_SYSTEM (item_name);
1734 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
1735 }
1736 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
1737 {
1738 descrip = ENCODE_SYSTEM (descrip);
1739 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
1740 }
1741 #endif /* not HAVE_MULTILINGUAL_MENU */
1742
1743 wv = xmalloc_widget_value ();
1744 if (prev_wv)
1745 prev_wv->next = wv;
1746 else
1747 save_wv->contents = wv;
1748 wv->name = (char *) SDATA (item_name);
1749 if (!NILP (descrip))
1750 wv->key = (char *) SDATA (descrip);
1751 wv->value = 0;
1752 /* Use the contents index as call_data, since we are
1753 restricted to 16-bits. */
1754 wv->call_data = !NILP (def) ? (void *) (EMACS_INT) i : 0;
1755 wv->enabled = !NILP (enable);
1756
1757 if (NILP (type))
1758 wv->button_type = BUTTON_TYPE_NONE;
1759 else if (EQ (type, QCtoggle))
1760 wv->button_type = BUTTON_TYPE_TOGGLE;
1761 else if (EQ (type, QCradio))
1762 wv->button_type = BUTTON_TYPE_RADIO;
1763 else
1764 abort ();
1765
1766 wv->selected = !NILP (selected);
1767 if (!STRINGP (help))
1768 help = Qnil;
1769
1770 wv->help = help;
1771
1772 prev_wv = wv;
1773
1774 i += MENU_ITEMS_ITEM_LENGTH;
1775 }
1776 }
1777
1778 /* Deal with the title, if it is non-nil. */
1779 if (!NILP (title))
1780 {
1781 widget_value *wv_title = xmalloc_widget_value ();
1782 widget_value *wv_sep = xmalloc_widget_value ();
1783
1784 /* Maybe replace this separator with a bitmap or owner-draw item
1785 so that it looks better. Having two separators looks odd. */
1786 wv_sep->name = "--";
1787 wv_sep->next = first_wv->contents;
1788 wv_sep->help = Qnil;
1789
1790 #ifndef HAVE_MULTILINGUAL_MENU
1791 if (STRING_MULTIBYTE (title))
1792 title = ENCODE_SYSTEM (title);
1793 #endif
1794 wv_title->name = (char *) SDATA (title);
1795 wv_title->enabled = TRUE;
1796 wv_title->title = TRUE;
1797 wv_title->button_type = BUTTON_TYPE_NONE;
1798 wv_title->help = Qnil;
1799 wv_title->next = wv_sep;
1800 first_wv->contents = wv_title;
1801 }
1802
1803 /* Actually create the menu. */
1804 menu = NewMenu (POPUP_SUBMENU_ID, "\p");
1805 submenu_id = MIN_POPUP_SUBMENU_ID;
1806 fill_submenu (menu, first_wv->contents);
1807
1808 /* Adjust coordinates to be root-window-relative. */
1809 pos.h = x;
1810 pos.v = y;
1811
1812 #if TARGET_API_MAC_CARBON
1813 SetPort (GetWindowPort (FRAME_MAC_WINDOW (f)));
1814 #else
1815 SetPort (FRAME_MAC_WINDOW (f));
1816 #endif
1817
1818 LocalToGlobal (&pos);
1819
1820 /* No selection has been chosen yet. */
1821 menu_item_choice = 0;
1822 menu_item_selection = 0;
1823
1824 InsertMenu (menu, -1);
1825
1826 /* Display the menu. */
1827 menu_item_choice = PopUpMenuSelect (menu, pos.v, pos.h, 0);
1828 menu_item_selection = LoWord (menu_item_choice);
1829
1830 /* Get the refcon to find the correct item*/
1831 if (menu_item_selection)
1832 {
1833 menu = GetMenuHandle (HiWord (menu_item_choice));
1834 if (menu) {
1835 GetMenuItemRefCon (menu, menu_item_selection, &refcon);
1836 }
1837 }
1838
1839 #if 0
1840 /* Clean up extraneous mouse events which might have been generated
1841 during the call. */
1842 discard_mouse_events ();
1843 #endif
1844
1845 /* Free the widget_value objects we used to specify the
1846 contents. */
1847 free_menubar_widget_value_tree (first_wv);
1848
1849 /* delete all menus */
1850 {
1851 int i = MIN_POPUP_SUBMENU_ID;
1852 MenuHandle submenu = GetMenuHandle (i);
1853 while (menu != NULL)
1854 {
1855 DeleteMenu (i);
1856 DisposeMenu (menu);
1857 menu = GetMenuHandle (++i);
1858 }
1859 }
1860
1861 DeleteMenu (POPUP_SUBMENU_ID);
1862 DisposeMenu (menu);
1863
1864 /* Find the selected item, and its pane, to return
1865 the proper value. */
1866 if (menu_item_selection != 0)
1867 {
1868 Lisp_Object prefix, entry;
1869
1870 prefix = entry = Qnil;
1871 i = 0;
1872 while (i < menu_items_used)
1873 {
1874 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1875 {
1876 subprefix_stack[submenu_depth++] = prefix;
1877 prefix = entry;
1878 i++;
1879 }
1880 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1881 {
1882 prefix = subprefix_stack[--submenu_depth];
1883 i++;
1884 }
1885 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1886 {
1887 prefix
1888 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1889 i += MENU_ITEMS_PANE_LENGTH;
1890 }
1891 /* Ignore a nil in the item list.
1892 It's meaningful only for dialog boxes. */
1893 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1894 i += 1;
1895 else
1896 {
1897 entry
1898 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
1899 if ((int) (EMACS_INT) refcon == i)
1900 {
1901 if (keymaps != 0)
1902 {
1903 int j;
1904
1905 entry = Fcons (entry, Qnil);
1906 if (!NILP (prefix))
1907 entry = Fcons (prefix, entry);
1908 for (j = submenu_depth - 1; j >= 0; j--)
1909 if (!NILP (subprefix_stack[j]))
1910 entry = Fcons (subprefix_stack[j], entry);
1911 }
1912 return entry;
1913 }
1914 i += MENU_ITEMS_ITEM_LENGTH;
1915 }
1916 }
1917 }
1918
1919 return Qnil;
1920 }
1921 \f
1922
1923 #ifdef HAVE_DIALOGS
1924 /* Construct native Mac OS menubar based on widget_value tree. */
1925
1926 static int
1927 mac_dialog (widget_value *wv)
1928 {
1929 char *dialog_name;
1930 char *prompt;
1931 char **button_labels;
1932 UInt32 *ref_cons;
1933 int nb_buttons;
1934 int left_count;
1935 int i;
1936 int dialog_width;
1937 Rect rect;
1938 WindowPtr window_ptr;
1939 ControlHandle ch;
1940 int left;
1941 EventRecord event_record;
1942 SInt16 part_code;
1943 int control_part_code;
1944 Point mouse;
1945
1946 dialog_name = wv->name;
1947 nb_buttons = dialog_name[1] - '0';
1948 left_count = nb_buttons - (dialog_name[4] - '0');
1949 button_labels = (char **) alloca (sizeof (char *) * nb_buttons);
1950 ref_cons = (UInt32 *) alloca (sizeof (UInt32) * nb_buttons);
1951
1952 wv = wv->contents;
1953 prompt = (char *) alloca (strlen (wv->value) + 1);
1954 strcpy (prompt, wv->value);
1955 c2pstr (prompt);
1956
1957 wv = wv->next;
1958 for (i = 0; i < nb_buttons; i++)
1959 {
1960 button_labels[i] = wv->value;
1961 button_labels[i] = (char *) alloca (strlen (wv->value) + 1);
1962 strcpy (button_labels[i], wv->value);
1963 c2pstr (button_labels[i]);
1964 ref_cons[i] = (UInt32) wv->call_data;
1965 wv = wv->next;
1966 }
1967
1968 window_ptr = GetNewCWindow (DIALOG_WINDOW_RESOURCE, NULL, (WindowPtr) -1);
1969
1970 #if TARGET_API_MAC_CARBON
1971 SetPort (GetWindowPort (window_ptr));
1972 #else
1973 SetPort (window_ptr);
1974 #endif
1975
1976 TextFont (0);
1977 /* Left and right margins in the dialog are 13 pixels each.*/
1978 dialog_width = 14;
1979 /* Calculate width of dialog box: 8 pixels on each side of the text
1980 label in each button, 12 pixels between buttons. */
1981 for (i = 0; i < nb_buttons; i++)
1982 dialog_width += StringWidth (button_labels[i]) + 16 + 12;
1983
1984 if (left_count != 0 && nb_buttons - left_count != 0)
1985 dialog_width += 12;
1986
1987 dialog_width = max (dialog_width, StringWidth (prompt) + 26);
1988
1989 SizeWindow (window_ptr, dialog_width, 78, 0);
1990 ShowWindow (window_ptr);
1991
1992 #if TARGET_API_MAC_CARBON
1993 SetPort (GetWindowPort (window_ptr));
1994 #else
1995 SetPort (window_ptr);
1996 #endif
1997
1998 TextFont (0);
1999
2000 MoveTo (13, 29);
2001 DrawString (prompt);
2002
2003 left = 13;
2004 for (i = 0; i < nb_buttons; i++)
2005 {
2006 int button_width = StringWidth (button_labels[i]) + 16;
2007 SetRect (&rect, left, 45, left + button_width, 65);
2008 ch = NewControl (window_ptr, &rect, button_labels[i], 1, 0, 0, 0,
2009 kControlPushButtonProc, ref_cons[i]);
2010 left += button_width + 12;
2011 if (i == left_count - 1)
2012 left += 12;
2013 }
2014
2015 i = 0;
2016 while (!i)
2017 {
2018 if (WaitNextEvent (mDownMask, &event_record, 10, NULL))
2019 if (event_record.what == mouseDown)
2020 {
2021 part_code = FindWindow (event_record.where, &window_ptr);
2022 if (part_code == inContent)
2023 {
2024 mouse = event_record.where;
2025 GlobalToLocal (&mouse);
2026 control_part_code = FindControl (mouse, window_ptr, &ch);
2027 if (control_part_code == kControlButtonPart)
2028 if (TrackControl (ch, mouse, NULL))
2029 i = GetControlReference (ch);
2030 }
2031 }
2032 }
2033
2034 DisposeWindow (window_ptr);
2035
2036 return i;
2037 }
2038
2039 static char * button_names [] = {
2040 "button1", "button2", "button3", "button4", "button5",
2041 "button6", "button7", "button8", "button9", "button10" };
2042
2043 static Lisp_Object
2044 mac_dialog_show (f, keymaps, title, error)
2045 FRAME_PTR f;
2046 int keymaps;
2047 Lisp_Object title;
2048 char **error;
2049 {
2050 int i, nb_buttons=0;
2051 char dialog_name[6];
2052 int menu_item_selection;
2053
2054 widget_value *wv, *first_wv = 0, *prev_wv = 0;
2055
2056 /* Number of elements seen so far, before boundary. */
2057 int left_count = 0;
2058 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2059 int boundary_seen = 0;
2060
2061 *error = NULL;
2062
2063 if (menu_items_n_panes > 1)
2064 {
2065 *error = "Multiple panes in dialog box";
2066 return Qnil;
2067 }
2068
2069 /* Create a tree of widget_value objects
2070 representing the text label and buttons. */
2071 {
2072 Lisp_Object pane_name, prefix;
2073 char *pane_string;
2074 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
2075 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
2076 pane_string = (NILP (pane_name)
2077 ? "" : (char *) SDATA (pane_name));
2078 prev_wv = xmalloc_widget_value ();
2079 prev_wv->value = pane_string;
2080 if (keymaps && !NILP (prefix))
2081 prev_wv->name++;
2082 prev_wv->enabled = 1;
2083 prev_wv->name = "message";
2084 prev_wv->help = Qnil;
2085 first_wv = prev_wv;
2086
2087 /* Loop over all panes and items, filling in the tree. */
2088 i = MENU_ITEMS_PANE_LENGTH;
2089 while (i < menu_items_used)
2090 {
2091
2092 /* Create a new item within current pane. */
2093 Lisp_Object item_name, enable, descrip, help;
2094
2095 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2096 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2097 descrip
2098 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2099 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
2100
2101 if (NILP (item_name))
2102 {
2103 free_menubar_widget_value_tree (first_wv);
2104 *error = "Submenu in dialog items";
2105 return Qnil;
2106 }
2107 if (EQ (item_name, Qquote))
2108 {
2109 /* This is the boundary between left-side elts
2110 and right-side elts. Stop incrementing right_count. */
2111 boundary_seen = 1;
2112 i++;
2113 continue;
2114 }
2115 if (nb_buttons >= 9)
2116 {
2117 free_menubar_widget_value_tree (first_wv);
2118 *error = "Too many dialog items";
2119 return Qnil;
2120 }
2121
2122 wv = xmalloc_widget_value ();
2123 prev_wv->next = wv;
2124 wv->name = (char *) button_names[nb_buttons];
2125 if (!NILP (descrip))
2126 wv->key = (char *) SDATA (descrip);
2127 wv->value = (char *) SDATA (item_name);
2128 wv->call_data = (void *) i;
2129 /* menu item is identified by its index in menu_items table */
2130 wv->enabled = !NILP (enable);
2131 wv->help = Qnil;
2132 prev_wv = wv;
2133
2134 if (! boundary_seen)
2135 left_count++;
2136
2137 nb_buttons++;
2138 i += MENU_ITEMS_ITEM_LENGTH;
2139 }
2140
2141 /* If the boundary was not specified,
2142 by default put half on the left and half on the right. */
2143 if (! boundary_seen)
2144 left_count = nb_buttons - nb_buttons / 2;
2145
2146 wv = xmalloc_widget_value ();
2147 wv->name = dialog_name;
2148 wv->help = Qnil;
2149
2150 /* Dialog boxes use a really stupid name encoding
2151 which specifies how many buttons to use
2152 and how many buttons are on the right.
2153 The Q means something also. */
2154 dialog_name[0] = 'Q';
2155 dialog_name[1] = '0' + nb_buttons;
2156 dialog_name[2] = 'B';
2157 dialog_name[3] = 'R';
2158 /* Number of buttons to put on the right. */
2159 dialog_name[4] = '0' + nb_buttons - left_count;
2160 dialog_name[5] = 0;
2161 wv->contents = first_wv;
2162 first_wv = wv;
2163 }
2164
2165 /* Actually create the dialog. */
2166 #ifdef HAVE_DIALOGS
2167 menu_item_selection = mac_dialog (first_wv);
2168 #else
2169 menu_item_selection = 0;
2170 #endif
2171
2172 /* Free the widget_value objects we used to specify the contents. */
2173 free_menubar_widget_value_tree (first_wv);
2174
2175 /* Find the selected item, and its pane, to return the proper
2176 value. */
2177 if (menu_item_selection != 0)
2178 {
2179 Lisp_Object prefix;
2180
2181 prefix = Qnil;
2182 i = 0;
2183 while (i < menu_items_used)
2184 {
2185 Lisp_Object entry;
2186
2187 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2188 {
2189 prefix
2190 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2191 i += MENU_ITEMS_PANE_LENGTH;
2192 }
2193 else
2194 {
2195 entry
2196 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2197 if (menu_item_selection == i)
2198 {
2199 if (keymaps != 0)
2200 {
2201 entry = Fcons (entry, Qnil);
2202 if (!NILP (prefix))
2203 entry = Fcons (prefix, entry);
2204 }
2205 return entry;
2206 }
2207 i += MENU_ITEMS_ITEM_LENGTH;
2208 }
2209 }
2210 }
2211
2212 return Qnil;
2213 }
2214 #endif /* HAVE_DIALOGS */
2215 \f
2216
2217 /* Is this item a separator? */
2218 static int
2219 name_is_separator (name)
2220 char *name;
2221 {
2222 char *start = name;
2223
2224 /* Check if name string consists of only dashes ('-'). */
2225 while (*name == '-') name++;
2226 /* Separators can also be of the form "--:TripleSuperMegaEtched"
2227 or "--deep-shadow". We don't implement them yet, se we just treat
2228 them like normal separators. */
2229 return (*name == '\0' || start + 2 == name);
2230 }
2231
2232 static void
2233 add_menu_item (MenuHandle menu, widget_value *wv, int submenu,
2234 int force_disable)
2235 {
2236 Str255 item_name;
2237 int pos, i;
2238
2239 if (name_is_separator (wv->name))
2240 AppendMenu (menu, "\p-");
2241 else
2242 {
2243 AppendMenu (menu, "\pX");
2244
2245 #if TARGET_API_MAC_CARBON
2246 pos = CountMenuItems (menu);
2247 #else
2248 pos = CountMItems (menu);
2249 #endif
2250
2251 strcpy (item_name, "");
2252 strncat (item_name, wv->name, 255);
2253 if (wv->key != NULL)
2254 {
2255 strncat (item_name, " ", 255);
2256 strncat (item_name, wv->key, 255);
2257 }
2258 item_name[255] = 0;
2259 c2pstr (item_name);
2260 SetMenuItemText (menu, pos, item_name);
2261
2262 if (wv->enabled && !force_disable)
2263 #if TARGET_API_MAC_CARBON
2264 EnableMenuItem (menu, pos);
2265 #else
2266 EnableItem (menu, pos);
2267 #endif
2268 else
2269 #if TARGET_API_MAC_CARBON
2270 DisableMenuItem (menu, pos);
2271 #else
2272 DisableItem (menu, pos);
2273 #endif
2274
2275 /* Draw radio buttons and tickboxes. */
2276 {
2277 if (wv->selected && (wv->button_type == BUTTON_TYPE_TOGGLE ||
2278 wv->button_type == BUTTON_TYPE_RADIO))
2279 SetItemMark (menu, pos, checkMark);
2280 else
2281 SetItemMark (menu, pos, noMark);
2282 }
2283 }
2284
2285 SetMenuItemRefCon (menu, pos, (UInt32) wv->call_data);
2286
2287 if (submenu != NULL)
2288 SetMenuItemHierarchicalID (menu, pos, submenu);
2289 }
2290
2291 /* Construct native Mac OS menubar based on widget_value tree. */
2292
2293 static void
2294 fill_submenu (MenuHandle menu, widget_value *wv)
2295 {
2296 for ( ; wv != NULL; wv = wv->next)
2297 if (wv->contents)
2298 {
2299 int cur_submenu = submenu_id++;
2300 MenuHandle submenu = NewMenu (cur_submenu, "\pX");
2301 fill_submenu (submenu, wv->contents);
2302 InsertMenu (submenu, -1);
2303 add_menu_item (menu, wv, cur_submenu, 0);
2304 }
2305 else
2306 add_menu_item (menu, wv, NULL, 0);
2307 }
2308
2309
2310 /* Construct native Mac OS menu based on widget_value tree. */
2311
2312 static void
2313 fill_menu (MenuHandle menu, widget_value *wv)
2314 {
2315 for ( ; wv != NULL; wv = wv->next)
2316 if (wv->contents)
2317 {
2318 int cur_submenu = submenu_id++;
2319 MenuHandle submenu = NewMenu (cur_submenu, "\pX");
2320 fill_submenu (submenu, wv->contents);
2321 InsertMenu (submenu, -1);
2322 add_menu_item (menu, wv, cur_submenu, 0);
2323 }
2324 else
2325 add_menu_item (menu, wv, NULL, 0);
2326 }
2327
2328 /* Construct native Mac OS menubar based on widget_value tree. */
2329
2330 static void
2331 fill_menubar (widget_value *wv)
2332 {
2333 int id;
2334
2335 submenu_id = MIN_SUBMENU_ID;
2336
2337 for (id = MIN_MENU_ID; wv != NULL; wv = wv->next, id++)
2338 {
2339 MenuHandle menu;
2340 Str255 title;
2341
2342 strncpy (title, wv->name, 255);
2343 title[255] = 0;
2344 c2pstr (title);
2345 menu = NewMenu (id, title);
2346
2347 if (wv->contents)
2348 fill_menu (menu, wv->contents);
2349
2350 InsertMenu (menu, 0);
2351 }
2352 }
2353
2354 #endif /* HAVE_MENUS */
2355
2356 \f
2357 void
2358 syms_of_macmenu ()
2359 {
2360 staticpro (&menu_items);
2361 menu_items = Qnil;
2362
2363 Qdebug_on_next_call = intern ("debug-on-next-call");
2364 staticpro (&Qdebug_on_next_call);
2365
2366 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
2367 doc: /* Frame for which we are updating a menu.
2368 The enable predicate for a menu command should check this variable. */);
2369 Vmenu_updating_frame = Qnil;
2370
2371 defsubr (&Sx_popup_menu);
2372 #ifdef HAVE_MENUS
2373 defsubr (&Sx_popup_dialog);
2374 #endif
2375 }