1 /* Menu support for GNU Emacs on Mac OS.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2006 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 /* Contributed by Andrew Choi (akochoi@mac.com). */
29 #include "termhooks.h"
34 #include "blockinput.h"
39 #if !TARGET_API_MAC_CARBON
42 #include <QuickDraw.h>
43 #include <ToolUtils.h>
48 #if defined (__MRC__) || (__MSL__ >= 0x6000)
49 #include <ControlDefinitions.h>
51 #endif /* not TARGET_API_MAC_CARBON */
53 /* This may include sys/types.h, and that somehow loses
54 if this is not done before the other system files. */
57 /* Load sys/types.h if not already loaded.
58 In some systems loading it twice is suicidal. */
60 #include <sys/types.h>
63 #include "dispextern.h"
65 enum mac_menu_kind
{ /* Menu ID range */
66 MAC_MENU_APPLE
, /* 0 (Reserved by Apple) */
67 MAC_MENU_MENU_BAR
, /* 1 .. 234 */
68 MAC_MENU_POPUP
, /* 235 */
69 MAC_MENU_DRIVER
, /* 236 .. 255 (Reserved) */
70 MAC_MENU_MENU_BAR_SUB
, /* 256 .. 16383 */
71 MAC_MENU_POPUP_SUB
, /* 16384 .. 32767 */
72 MAC_MENU_END
/* 32768 */
75 static const int min_menu_id
[] = {0, 1, 235, 236, 256, 16384, 32768};
77 #define DIALOG_WINDOW_RESOURCE 130
79 #define HAVE_DIALOGS 1
81 #undef HAVE_MULTILINGUAL_MENU
82 #undef HAVE_DIALOGS /* TODO: Implement native dialogs. */
84 /******************************************************************/
85 /* Definitions copied from lwlib.h */
87 typedef void * XtPointer
;
96 /* This structure is based on the one in ../lwlib/lwlib.h, modified
98 typedef struct _widget_value
103 /* value (meaning depend on widget type) */
105 /* keyboard equivalent. no implications for XtTranslations */
108 /* Help string or nil if none.
109 GC finds this string through the frame's menu_bar_vector
110 or through menu_items. */
112 /* true if enabled */
114 /* true if selected */
116 /* The type of a button. */
117 enum button_type button_type
;
118 /* true if menu title */
121 /* true if was edited (maintained by get_value) */
123 /* true if has changed (maintained by lw library) */
125 /* true if this widget itself has changed,
126 but not counting the other widgets found in the `next' field. */
127 change_type this_one_change
;
129 /* Contents of the sub-widgets, also selected slot for checkbox */
130 struct _widget_value
* contents
;
131 /* data passed to callback */
133 /* next one in the list */
134 struct _widget_value
* next
;
136 /* slot for the toolkit dependent part. Always initialize to NULL. */
138 /* tell us if we should free the toolkit data slot when freeing the
139 widget_value itself. */
140 Boolean free_toolkit_data
;
142 /* we resource the widget_value structures; this points to the next
143 one on the free list if this one has been deallocated.
145 struct _widget_value
*free_list
;
149 /* Assumed by other routines to zero area returned. */
150 #define malloc_widget_value() (void *)memset (xmalloc (sizeof (widget_value)),\
151 0, (sizeof (widget_value)))
152 #define free_widget_value(wv) xfree (wv)
154 /******************************************************************/
161 Lisp_Object Vmenu_updating_frame
;
163 Lisp_Object Qdebug_on_next_call
;
165 extern Lisp_Object Qmenu_bar
, Qmac_apple_event
;
167 extern Lisp_Object QCtoggle
, QCradio
;
169 extern Lisp_Object Voverriding_local_map
;
170 extern Lisp_Object Voverriding_local_map_menu_flag
;
172 extern Lisp_Object Qoverriding_local_map
, Qoverriding_terminal_local_map
;
174 extern Lisp_Object Qmenu_bar_update_hook
;
176 void set_frame_menubar
P_ ((FRAME_PTR
, int, int));
178 #if TARGET_API_MAC_CARBON
179 #define ENCODE_MENU_STRING(str) ENCODE_UTF_8 (str)
181 #define ENCODE_MENU_STRING(str) ENCODE_SYSTEM (str)
184 static void push_menu_item
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
,
185 Lisp_Object
, Lisp_Object
, Lisp_Object
,
186 Lisp_Object
, Lisp_Object
));
188 static Lisp_Object mac_dialog_show
P_ ((FRAME_PTR
, int, Lisp_Object
,
189 Lisp_Object
, char **));
191 static Lisp_Object mac_menu_show
P_ ((struct frame
*, int, int, int, int,
192 Lisp_Object
, char **));
193 static void keymap_panes
P_ ((Lisp_Object
*, int, int));
194 static void single_keymap_panes
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
,
196 static void list_of_panes
P_ ((Lisp_Object
));
197 static void list_of_items
P_ ((Lisp_Object
));
199 static int fill_menu
P_ ((MenuHandle
, widget_value
*, enum mac_menu_kind
, int));
200 static void fill_menubar
P_ ((widget_value
*, int));
201 static void dispose_menus
P_ ((enum mac_menu_kind
, int));
204 /* This holds a Lisp vector that holds the results of decoding
205 the keymaps or alist-of-alists that specify a menu.
207 It describes the panes and items within the panes.
209 Each pane is described by 3 elements in the vector:
210 t, the pane name, the pane's prefix key.
211 Then follow the pane's items, with 5 elements per item:
212 the item string, the enable flag, the item's value,
213 the definition, and the equivalent keyboard key's description string.
215 In some cases, multiple levels of menus may be described.
216 A single vector slot containing nil indicates the start of a submenu.
217 A single vector slot containing lambda indicates the end of a submenu.
218 The submenu follows a menu item which is the way to reach the submenu.
220 A single vector slot containing quote indicates that the
221 following items should appear on the right of a dialog box.
223 Using a Lisp vector to hold this information while we decode it
224 takes care of protecting all the data from GC. */
226 #define MENU_ITEMS_PANE_NAME 1
227 #define MENU_ITEMS_PANE_PREFIX 2
228 #define MENU_ITEMS_PANE_LENGTH 3
232 MENU_ITEMS_ITEM_NAME
= 0,
233 MENU_ITEMS_ITEM_ENABLE
,
234 MENU_ITEMS_ITEM_VALUE
,
235 MENU_ITEMS_ITEM_EQUIV_KEY
,
236 MENU_ITEMS_ITEM_DEFINITION
,
237 MENU_ITEMS_ITEM_TYPE
,
238 MENU_ITEMS_ITEM_SELECTED
,
239 MENU_ITEMS_ITEM_HELP
,
240 MENU_ITEMS_ITEM_LENGTH
243 static Lisp_Object menu_items
;
245 /* Number of slots currently allocated in menu_items. */
246 static int menu_items_allocated
;
248 /* This is the index in menu_items of the first empty slot. */
249 static int menu_items_used
;
251 /* The number of panes currently recorded in menu_items,
252 excluding those within submenus. */
253 static int menu_items_n_panes
;
255 /* Current depth within submenus. */
256 static int menu_items_submenu_depth
;
258 /* This is set nonzero after the user activates the menu bar, and set
259 to zero again after the menu bars are redisplayed by prepare_menu_bar.
260 While it is nonzero, all calls to set_frame_menubar go deep.
262 I don't understand why this is needed, but it does seem to be
263 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
265 int pending_menu_activation
;
267 /* Initialize the menu_items structure if we haven't already done so.
268 Also mark it as currently empty. */
273 if (NILP (menu_items
))
275 menu_items_allocated
= 60;
276 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
280 menu_items_n_panes
= 0;
281 menu_items_submenu_depth
= 0;
284 /* Call at the end of generating the data in menu_items. */
291 /* Call when finished using the data for the current menu
295 discard_menu_items ()
297 /* Free the structure if it is especially large.
298 Otherwise, hold on to it, to save time. */
299 if (menu_items_allocated
> 200)
302 menu_items_allocated
= 0;
306 /* This undoes save_menu_items, and it is called by the specpdl unwind
310 restore_menu_items (saved
)
313 menu_items
= XCAR (saved
);
314 menu_items_allocated
= (VECTORP (menu_items
) ? ASIZE (menu_items
) : 0);
315 saved
= XCDR (saved
);
316 menu_items_used
= XINT (XCAR (saved
));
317 saved
= XCDR (saved
);
318 menu_items_n_panes
= XINT (XCAR (saved
));
319 saved
= XCDR (saved
);
320 menu_items_submenu_depth
= XINT (XCAR (saved
));
324 /* Push the whole state of menu_items processing onto the specpdl.
325 It will be restored when the specpdl is unwound. */
330 Lisp_Object saved
= list4 (menu_items
,
331 make_number (menu_items_used
),
332 make_number (menu_items_n_panes
),
333 make_number (menu_items_submenu_depth
));
334 record_unwind_protect (restore_menu_items
, saved
);
338 /* Make the menu_items vector twice as large. */
344 int old_size
= menu_items_allocated
;
347 menu_items_allocated
*= 2;
349 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
350 bcopy (XVECTOR (old
)->contents
, XVECTOR (menu_items
)->contents
,
351 old_size
* sizeof (Lisp_Object
));
354 /* Begin a submenu. */
357 push_submenu_start ()
359 if (menu_items_used
+ 1 > menu_items_allocated
)
362 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qnil
;
363 menu_items_submenu_depth
++;
371 if (menu_items_used
+ 1 > menu_items_allocated
)
374 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qlambda
;
375 menu_items_submenu_depth
--;
378 /* Indicate boundary between left and right. */
381 push_left_right_boundary ()
383 if (menu_items_used
+ 1 > menu_items_allocated
)
386 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qquote
;
389 /* Start a new menu pane in menu_items.
390 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
393 push_menu_pane (name
, prefix_vec
)
394 Lisp_Object name
, prefix_vec
;
396 if (menu_items_used
+ MENU_ITEMS_PANE_LENGTH
> menu_items_allocated
)
399 if (menu_items_submenu_depth
== 0)
400 menu_items_n_panes
++;
401 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qt
;
402 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
403 XVECTOR (menu_items
)->contents
[menu_items_used
++] = prefix_vec
;
406 /* Push one menu item into the current pane. NAME is the string to
407 display. ENABLE if non-nil means this item can be selected. KEY
408 is the key generated by choosing this item, or nil if this item
409 doesn't really have a definition. DEF is the definition of this
410 item. EQUIV is the textual description of the keyboard equivalent
411 for this item (or nil if none). TYPE is the type of this menu
412 item, one of nil, `toggle' or `radio'. */
415 push_menu_item (name
, enable
, key
, def
, equiv
, type
, selected
, help
)
416 Lisp_Object name
, enable
, key
, def
, equiv
, type
, selected
, help
;
418 if (menu_items_used
+ MENU_ITEMS_ITEM_LENGTH
> menu_items_allocated
)
421 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
422 XVECTOR (menu_items
)->contents
[menu_items_used
++] = enable
;
423 XVECTOR (menu_items
)->contents
[menu_items_used
++] = key
;
424 XVECTOR (menu_items
)->contents
[menu_items_used
++] = equiv
;
425 XVECTOR (menu_items
)->contents
[menu_items_used
++] = def
;
426 XVECTOR (menu_items
)->contents
[menu_items_used
++] = type
;
427 XVECTOR (menu_items
)->contents
[menu_items_used
++] = selected
;
428 XVECTOR (menu_items
)->contents
[menu_items_used
++] = help
;
431 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
432 and generate menu panes for them in menu_items.
433 If NOTREAL is nonzero,
434 don't bother really computing whether an item is enabled. */
437 keymap_panes (keymaps
, nmaps
, notreal
)
438 Lisp_Object
*keymaps
;
446 /* Loop over the given keymaps, making a pane for each map.
447 But don't make a pane that is empty--ignore that map instead.
448 P is the number of panes we have made so far. */
449 for (mapno
= 0; mapno
< nmaps
; mapno
++)
450 single_keymap_panes (keymaps
[mapno
],
451 Fkeymap_prompt (keymaps
[mapno
]), Qnil
, notreal
, 10);
453 finish_menu_items ();
456 /* Args passed between single_keymap_panes and single_menu_item. */
459 Lisp_Object pending_maps
;
460 int maxdepth
, notreal
;
463 static void single_menu_item
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
,
466 /* This is a recursive subroutine of keymap_panes.
467 It handles one keymap, KEYMAP.
468 The other arguments are passed along
469 or point to local variables of the previous function.
470 If NOTREAL is nonzero, only check for equivalent key bindings, don't
471 evaluate expressions in menu items and don't make any menu.
473 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
476 single_keymap_panes (keymap
, pane_name
, prefix
, notreal
, maxdepth
)
478 Lisp_Object pane_name
;
486 skp
.pending_maps
= Qnil
;
487 skp
.maxdepth
= maxdepth
;
488 skp
.notreal
= notreal
;
493 push_menu_pane (pane_name
, prefix
);
495 GCPRO1 (skp
.pending_maps
);
496 map_keymap (keymap
, single_menu_item
, Qnil
, &skp
, 1);
499 /* Process now any submenus which want to be panes at this level. */
500 while (CONSP (skp
.pending_maps
))
502 Lisp_Object elt
, eltcdr
, string
;
503 elt
= XCAR (skp
.pending_maps
);
505 string
= XCAR (eltcdr
);
506 /* We no longer discard the @ from the beginning of the string here.
507 Instead, we do this in mac_menu_show. */
508 single_keymap_panes (Fcar (elt
), string
,
509 XCDR (eltcdr
), notreal
, maxdepth
- 1);
510 skp
.pending_maps
= XCDR (skp
.pending_maps
);
514 /* This is a subroutine of single_keymap_panes that handles one
516 KEY is a key in a keymap and ITEM is its binding.
517 SKP->PENDING_MAPS_PTR is a list of keymaps waiting to be made into
519 If SKP->NOTREAL is nonzero, only check for equivalent key bindings, don't
520 evaluate expressions in menu items and don't make any menu.
521 If we encounter submenus deeper than SKP->MAXDEPTH levels, ignore them. */
524 single_menu_item (key
, item
, dummy
, skp_v
)
525 Lisp_Object key
, item
, dummy
;
528 Lisp_Object map
, item_string
, enabled
;
529 struct gcpro gcpro1
, gcpro2
;
531 struct skp
*skp
= skp_v
;
533 /* Parse the menu item and leave the result in item_properties. */
535 res
= parse_menu_item (item
, skp
->notreal
, 0);
538 return; /* Not a menu item. */
540 map
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_MAP
];
544 /* We don't want to make a menu, just traverse the keymaps to
545 precompute equivalent key bindings. */
547 single_keymap_panes (map
, Qnil
, key
, 1, skp
->maxdepth
- 1);
551 enabled
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_ENABLE
];
552 item_string
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_NAME
];
554 if (!NILP (map
) && SREF (item_string
, 0) == '@')
557 /* An enabled separate pane. Remember this to handle it later. */
558 skp
->pending_maps
= Fcons (Fcons (map
, Fcons (item_string
, key
)),
563 push_menu_item (item_string
, enabled
, key
,
564 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_DEF
],
565 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_KEYEQ
],
566 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_TYPE
],
567 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_SELECTED
],
568 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_HELP
]);
570 /* Display a submenu using the toolkit. */
571 if (! (NILP (map
) || NILP (enabled
)))
573 push_submenu_start ();
574 single_keymap_panes (map
, Qnil
, key
, 0, skp
->maxdepth
- 1);
579 /* Push all the panes and items of a menu described by the
580 alist-of-alists MENU.
581 This handles old-fashioned calls to x-popup-menu. */
591 for (tail
= menu
; CONSP (tail
); tail
= XCDR (tail
))
593 Lisp_Object elt
, pane_name
, pane_data
;
595 pane_name
= Fcar (elt
);
596 CHECK_STRING (pane_name
);
597 push_menu_pane (ENCODE_MENU_STRING (pane_name
), Qnil
);
598 pane_data
= Fcdr (elt
);
599 CHECK_CONS (pane_data
);
600 list_of_items (pane_data
);
603 finish_menu_items ();
606 /* Push the items in a single pane defined by the alist PANE. */
612 Lisp_Object tail
, item
, item1
;
614 for (tail
= pane
; CONSP (tail
); tail
= XCDR (tail
))
618 push_menu_item (ENCODE_MENU_STRING (item
), Qnil
, Qnil
, Qt
,
619 Qnil
, Qnil
, Qnil
, Qnil
);
620 else if (CONSP (item
))
623 CHECK_STRING (item1
);
624 push_menu_item (ENCODE_MENU_STRING (item1
), Qt
, XCDR (item
),
625 Qt
, Qnil
, Qnil
, Qnil
, Qnil
);
628 push_left_right_boundary ();
634 cleanup_popup_menu (arg
)
637 discard_menu_items ();
641 DEFUN ("x-popup-menu", Fx_popup_menu
, Sx_popup_menu
, 2, 2, 0,
642 doc
: /* Pop up a deck-of-cards menu and return user's selection.
643 POSITION is a position specification. This is either a mouse button event
644 or a list ((XOFFSET YOFFSET) WINDOW)
645 where XOFFSET and YOFFSET are positions in pixels from the top left
646 corner of WINDOW. (WINDOW may be a window or a frame object.)
647 This controls the position of the top left of the menu as a whole.
648 If POSITION is t, it means to use the current mouse position.
650 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.
651 The menu items come from key bindings that have a menu string as well as
652 a definition; actually, the "definition" in such a key binding looks like
653 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into
654 the keymap as a top-level element.
656 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.
657 Otherwise, REAL-DEFINITION should be a valid key binding definition.
659 You can also use a list of keymaps as MENU.
660 Then each keymap makes a separate pane.
662 When MENU is a keymap or a list of keymaps, the return value is the
663 list of events corresponding to the user's choice. Note that
664 `x-popup-menu' does not actually execute the command bound to that
667 Alternatively, you can specify a menu of multiple panes
668 with a list of the form (TITLE PANE1 PANE2...),
669 where each pane is a list of form (TITLE ITEM1 ITEM2...).
670 Each ITEM is normally a cons cell (STRING . VALUE);
671 but a string can appear as an item--that makes a nonselectable line
673 With this form of menu, the return value is VALUE from the chosen item.
675 If POSITION is nil, don't display the menu at all, just precalculate the
676 cached information about equivalent key sequences.
678 If the user gets rid of the menu without making a valid choice, for
679 instance by clicking the mouse away from a valid choice or by typing
680 keyboard input, then this normally results in a quit and
681 `x-popup-menu' does not return. But if POSITION is a mouse button
682 event (indicating that the user invoked the menu with the mouse) then
683 no quit occurs and `x-popup-menu' returns nil. */)
685 Lisp_Object position
, menu
;
687 Lisp_Object keymap
, tem
;
688 int xpos
= 0, ypos
= 0;
690 char *error_name
= NULL
;
691 Lisp_Object selection
;
693 Lisp_Object x
, y
, window
;
696 int specpdl_count
= SPECPDL_INDEX ();
700 if (! NILP (position
))
704 /* Decode the first argument: find the window and the coordinates. */
705 if (EQ (position
, Qt
)
706 || (CONSP (position
) && (EQ (XCAR (position
), Qmenu_bar
)
707 || EQ (XCAR (position
), Qtool_bar
)
708 || EQ (XCAR (position
), Qmac_apple_event
))))
710 /* Use the mouse's current position. */
711 FRAME_PTR new_f
= SELECTED_FRAME ();
712 Lisp_Object bar_window
;
713 enum scroll_bar_part part
;
716 if (mouse_position_hook
)
717 (*mouse_position_hook
) (&new_f
, 1, &bar_window
,
718 &part
, &x
, &y
, &time
);
720 XSETFRAME (window
, new_f
);
723 window
= selected_window
;
730 tem
= Fcar (position
);
733 window
= Fcar (Fcdr (position
));
735 y
= Fcar (XCDR (tem
));
740 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
741 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
742 tem
= Fcar (Fcdr (Fcdr (tem
))); /* POSN_WINDOW_POSN (tem) */
751 /* Decode where to put the menu. */
759 else if (WINDOWP (window
))
761 CHECK_LIVE_WINDOW (window
);
762 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
764 xpos
= WINDOW_LEFT_EDGE_X (XWINDOW (window
));
765 ypos
= WINDOW_TOP_EDGE_Y (XWINDOW (window
));
768 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
769 but I don't want to make one now. */
770 CHECK_WINDOW (window
);
775 XSETFRAME (Vmenu_updating_frame
, f
);
778 Vmenu_updating_frame
= Qnil
;
779 #endif /* HAVE_MENUS */
784 /* Decode the menu items from what was specified. */
786 keymap
= get_keymap (menu
, 0, 0);
789 /* We were given a keymap. Extract menu info from the keymap. */
792 /* Extract the detailed info to make one pane. */
793 keymap_panes (&menu
, 1, NILP (position
));
795 /* Search for a string appearing directly as an element of the keymap.
796 That string is the title of the menu. */
797 prompt
= Fkeymap_prompt (keymap
);
798 if (NILP (title
) && !NILP (prompt
))
801 /* Make that be the pane title of the first pane. */
802 if (!NILP (prompt
) && menu_items_n_panes
>= 0)
803 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = prompt
;
807 else if (CONSP (menu
) && KEYMAPP (XCAR (menu
)))
809 /* We were given a list of keymaps. */
810 int nmaps
= XFASTINT (Flength (menu
));
812 = (Lisp_Object
*) alloca (nmaps
* sizeof (Lisp_Object
));
817 /* The first keymap that has a prompt string
818 supplies the menu title. */
819 for (tem
= menu
, i
= 0; CONSP (tem
); tem
= XCDR (tem
))
823 maps
[i
++] = keymap
= get_keymap (XCAR (tem
), 1, 0);
825 prompt
= Fkeymap_prompt (keymap
);
826 if (NILP (title
) && !NILP (prompt
))
830 /* Extract the detailed info to make one pane. */
831 keymap_panes (maps
, nmaps
, NILP (position
));
833 /* Make the title be the pane title of the first pane. */
834 if (!NILP (title
) && menu_items_n_panes
>= 0)
835 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = title
;
841 /* We were given an old-fashioned menu. */
843 CHECK_STRING (title
);
845 list_of_panes (Fcdr (menu
));
852 discard_menu_items ();
858 /* Display them in a menu. */
859 record_unwind_protect (cleanup_popup_menu
, Qnil
);
862 selection
= mac_menu_show (f
, xpos
, ypos
, for_click
,
863 keymaps
, title
, &error_name
);
865 unbind_to (specpdl_count
, Qnil
);
868 #endif /* HAVE_MENUS */
870 if (error_name
) error (error_name
);
876 DEFUN ("x-popup-dialog", Fx_popup_dialog
, Sx_popup_dialog
, 2, 3, 0,
877 doc
: /* Pop up a dialog box and return user's selection.
878 POSITION specifies which frame to use.
879 This is normally a mouse button event or a window or frame.
880 If POSITION is t, it means to use the frame the mouse is on.
881 The dialog box appears in the middle of the specified frame.
883 CONTENTS specifies the alternatives to display in the dialog box.
884 It is a list of the form (DIALOG ITEM1 ITEM2...).
885 Each ITEM is a cons cell (STRING . VALUE).
886 The return value is VALUE from the chosen item.
888 An ITEM may also be just a string--that makes a nonselectable item.
889 An ITEM may also be nil--that means to put all preceding items
890 on the left of the dialog box and all following items on the right.
891 \(By default, approximately half appear on each side.)
893 If HEADER is non-nil, the frame title for the box is "Information",
894 otherwise it is "Question".
896 If the user gets rid of the dialog box without making a valid choice,
897 for instance using the window manager, then this produces a quit and
898 `x-popup-dialog' does not return. */)
899 (position
, contents
, header
)
900 Lisp_Object position
, contents
, header
;
907 /* Decode the first argument: find the window or frame to use. */
908 if (EQ (position
, Qt
)
909 || (CONSP (position
) && (EQ (XCAR (position
), Qmenu_bar
)
910 || EQ (XCAR (position
), Qtool_bar
)
911 || EQ (XCAR (position
), Qmac_apple_event
))))
913 #if 0 /* Using the frame the mouse is on may not be right. */
914 /* Use the mouse's current position. */
915 FRAME_PTR new_f
= SELECTED_FRAME ();
916 Lisp_Object bar_window
;
917 enum scroll_bar_part part
;
921 (*mouse_position_hook
) (&new_f
, 1, &bar_window
, &part
, &x
, &y
, &time
);
924 XSETFRAME (window
, new_f
);
926 window
= selected_window
;
928 window
= selected_window
;
930 else if (CONSP (position
))
933 tem
= Fcar (position
);
935 window
= Fcar (Fcdr (position
));
938 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
939 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
942 else if (WINDOWP (position
) || FRAMEP (position
))
947 /* Decode where to put the menu. */
951 else if (WINDOWP (window
))
953 CHECK_LIVE_WINDOW (window
);
954 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
957 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
958 but I don't want to make one now. */
959 CHECK_WINDOW (window
);
962 /* Display a menu with these alternatives
963 in the middle of frame F. */
965 Lisp_Object x
, y
, frame
, newpos
;
966 XSETFRAME (frame
, f
);
967 XSETINT (x
, x_pixel_width (f
) / 2);
968 XSETINT (y
, x_pixel_height (f
) / 2);
969 newpos
= Fcons (Fcons (x
, Fcons (y
, Qnil
)), Fcons (frame
, Qnil
));
971 return Fx_popup_menu (newpos
,
972 Fcons (Fcar (contents
), Fcons (contents
, Qnil
)));
974 #else /* HAVE_DIALOGS */
978 Lisp_Object selection
;
979 int specpdl_count
= SPECPDL_INDEX ();
981 /* Decode the dialog items from what was specified. */
982 title
= Fcar (contents
);
983 CHECK_STRING (title
);
985 list_of_panes (Fcons (contents
, Qnil
));
987 /* Display them in a dialog box. */
988 record_unwind_protect (cleanup_popup_menu
, Qnil
);
990 selection
= mac_dialog_show (f
, 0, title
, header
, &error_name
);
992 unbind_to (specpdl_count
, Qnil
);
994 if (error_name
) error (error_name
);
997 #endif /* HAVE_DIALOGS */
1000 /* Activate the menu bar of frame F.
1001 This is called from keyboard.c when it gets the
1002 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
1004 To activate the menu bar, we use the button-press event location
1005 that was saved in saved_menu_event_location.
1007 But first we recompute the menu bar contents (the whole tree).
1009 The reason for saving the button event until here, instead of
1010 passing it to the toolkit right away, is that we can safely
1011 execute Lisp code. */
1014 x_activate_menubar (f
)
1018 extern Point saved_menu_event_location
;
1020 set_frame_menubar (f
, 0, 1);
1023 menu_choice
= MenuSelect (saved_menu_event_location
);
1024 do_menu_choice (menu_choice
);
1029 /* This callback is called from the menu bar pulldown menu
1030 when the user makes a selection.
1031 Figure out what the user chose
1032 and put the appropriate events into the keyboard buffer. */
1035 menubar_selection_callback (FRAME_PTR f
, int client_data
)
1037 Lisp_Object prefix
, entry
;
1039 Lisp_Object
*subprefix_stack
;
1040 int submenu_depth
= 0;
1046 subprefix_stack
= (Lisp_Object
*) alloca (f
->menu_bar_items_used
* sizeof (Lisp_Object
));
1047 vector
= f
->menu_bar_vector
;
1050 while (i
< f
->menu_bar_items_used
)
1052 if (EQ (XVECTOR (vector
)->contents
[i
], Qnil
))
1054 subprefix_stack
[submenu_depth
++] = prefix
;
1058 else if (EQ (XVECTOR (vector
)->contents
[i
], Qlambda
))
1060 prefix
= subprefix_stack
[--submenu_depth
];
1063 else if (EQ (XVECTOR (vector
)->contents
[i
], Qt
))
1065 prefix
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1066 i
+= MENU_ITEMS_PANE_LENGTH
;
1070 entry
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1071 /* The EMACS_INT cast avoids a warning. There's no problem
1072 as long as pointers have enough bits to hold small integers. */
1073 if ((int) (EMACS_INT
) client_data
== i
)
1076 struct input_event buf
;
1080 XSETFRAME (frame
, f
);
1081 buf
.kind
= MENU_BAR_EVENT
;
1082 buf
.frame_or_window
= frame
;
1084 kbd_buffer_store_event (&buf
);
1086 for (j
= 0; j
< submenu_depth
; j
++)
1087 if (!NILP (subprefix_stack
[j
]))
1089 buf
.kind
= MENU_BAR_EVENT
;
1090 buf
.frame_or_window
= frame
;
1091 buf
.arg
= subprefix_stack
[j
];
1092 kbd_buffer_store_event (&buf
);
1097 buf
.kind
= MENU_BAR_EVENT
;
1098 buf
.frame_or_window
= frame
;
1100 kbd_buffer_store_event (&buf
);
1103 buf
.kind
= MENU_BAR_EVENT
;
1104 buf
.frame_or_window
= frame
;
1106 kbd_buffer_store_event (&buf
);
1108 f
->output_data
.mac
->menubar_active
= 0;
1111 i
+= MENU_ITEMS_ITEM_LENGTH
;
1114 f
->output_data
.mac
->menubar_active
= 0;
1117 /* Allocate a widget_value, blocking input. */
1120 xmalloc_widget_value ()
1122 widget_value
*value
;
1125 value
= malloc_widget_value ();
1131 /* This recursively calls free_widget_value on the tree of widgets.
1132 It must free all data that was malloc'ed for these widget_values.
1133 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1134 must be left alone. */
1137 free_menubar_widget_value_tree (wv
)
1142 wv
->name
= wv
->value
= wv
->key
= (char *) 0xDEADBEEF;
1144 if (wv
->contents
&& (wv
->contents
!= (widget_value
*)1))
1146 free_menubar_widget_value_tree (wv
->contents
);
1147 wv
->contents
= (widget_value
*) 0xDEADBEEF;
1151 free_menubar_widget_value_tree (wv
->next
);
1152 wv
->next
= (widget_value
*) 0xDEADBEEF;
1155 free_widget_value (wv
);
1159 /* Set up data in menu_items for a menu bar item
1160 whose event type is ITEM_KEY (with string ITEM_NAME)
1161 and whose contents come from the list of keymaps MAPS. */
1164 parse_single_submenu (item_key
, item_name
, maps
)
1165 Lisp_Object item_key
, item_name
, maps
;
1169 Lisp_Object
*mapvec
;
1171 int top_level_items
= 0;
1173 length
= Flength (maps
);
1174 len
= XINT (length
);
1176 /* Convert the list MAPS into a vector MAPVEC. */
1177 mapvec
= (Lisp_Object
*) alloca (len
* sizeof (Lisp_Object
));
1178 for (i
= 0; i
< len
; i
++)
1180 mapvec
[i
] = Fcar (maps
);
1184 /* Loop over the given keymaps, making a pane for each map.
1185 But don't make a pane that is empty--ignore that map instead. */
1186 for (i
= 0; i
< len
; i
++)
1188 if (!KEYMAPP (mapvec
[i
]))
1190 /* Here we have a command at top level in the menu bar
1191 as opposed to a submenu. */
1192 top_level_items
= 1;
1193 push_menu_pane (Qnil
, Qnil
);
1194 push_menu_item (item_name
, Qt
, item_key
, mapvec
[i
],
1195 Qnil
, Qnil
, Qnil
, Qnil
);
1200 prompt
= Fkeymap_prompt (mapvec
[i
]);
1201 single_keymap_panes (mapvec
[i
],
1202 !NILP (prompt
) ? prompt
: item_name
,
1207 return top_level_items
;
1210 /* Create a tree of widget_value objects
1211 representing the panes and items
1212 in menu_items starting at index START, up to index END. */
1214 static widget_value
*
1215 digest_single_submenu (start
, end
, top_level_items
)
1216 int start
, end
, top_level_items
;
1218 widget_value
*wv
, *prev_wv
, *save_wv
, *first_wv
;
1220 int submenu_depth
= 0;
1221 widget_value
**submenu_stack
;
1225 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1226 wv
= xmalloc_widget_value ();
1230 wv
->button_type
= BUTTON_TYPE_NONE
;
1236 /* Loop over all panes and items made by the preceding call
1237 to parse_single_submenu and construct a tree of widget_value objects.
1238 Ignore the panes and items used by previous calls to
1239 digest_single_submenu, even though those are also in menu_items. */
1243 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1245 submenu_stack
[submenu_depth
++] = save_wv
;
1250 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1253 save_wv
= submenu_stack
[--submenu_depth
];
1256 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1257 && submenu_depth
!= 0)
1258 i
+= MENU_ITEMS_PANE_LENGTH
;
1259 /* Ignore a nil in the item list.
1260 It's meaningful only for dialog boxes. */
1261 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1263 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1265 /* Create a new pane. */
1266 Lisp_Object pane_name
, prefix
;
1271 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1272 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1274 #ifndef HAVE_MULTILINGUAL_MENU
1275 if (STRINGP (pane_name
) && STRING_MULTIBYTE (pane_name
))
1277 pane_name
= ENCODE_MENU_STRING (pane_name
);
1278 AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
) = pane_name
;
1281 pane_string
= (NILP (pane_name
)
1282 ? "" : (char *) SDATA (pane_name
));
1283 /* If there is just one top-level pane, put all its items directly
1284 under the top-level menu. */
1285 if (menu_items_n_panes
== 1)
1288 /* If the pane has a meaningful name,
1289 make the pane a top-level menu item
1290 with its items as a submenu beneath it. */
1291 if (strcmp (pane_string
, ""))
1293 wv
= xmalloc_widget_value ();
1297 first_wv
->contents
= wv
;
1298 wv
->lname
= pane_name
;
1299 /* Set value to 1 so update_submenu_strings can handle '@' */
1300 wv
->value
= (char *)1;
1302 wv
->button_type
= BUTTON_TYPE_NONE
;
1310 i
+= MENU_ITEMS_PANE_LENGTH
;
1314 /* Create a new item within current pane. */
1315 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
;
1318 /* All items should be contained in panes. */
1319 if (panes_seen
== 0)
1322 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
1323 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
1324 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
1325 def
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_DEFINITION
);
1326 type
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_TYPE
);
1327 selected
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_SELECTED
);
1328 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
1330 #ifndef HAVE_MULTILINGUAL_MENU
1331 if (STRING_MULTIBYTE (item_name
))
1333 item_name
= ENCODE_MENU_STRING (item_name
);
1334 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
) = item_name
;
1337 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
1339 descrip
= ENCODE_MENU_STRING (descrip
);
1340 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
) = descrip
;
1342 #endif /* not HAVE_MULTILINGUAL_MENU */
1344 wv
= xmalloc_widget_value ();
1348 save_wv
->contents
= wv
;
1350 wv
->lname
= item_name
;
1351 if (!NILP (descrip
))
1354 /* The EMACS_INT cast avoids a warning. There's no problem
1355 as long as pointers have enough bits to hold small integers. */
1356 wv
->call_data
= (!NILP (def
) ? (void *) (EMACS_INT
) i
: 0);
1357 wv
->enabled
= !NILP (enable
);
1360 wv
->button_type
= BUTTON_TYPE_NONE
;
1361 else if (EQ (type
, QCradio
))
1362 wv
->button_type
= BUTTON_TYPE_RADIO
;
1363 else if (EQ (type
, QCtoggle
))
1364 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
1368 wv
->selected
= !NILP (selected
);
1369 if (! STRINGP (help
))
1376 i
+= MENU_ITEMS_ITEM_LENGTH
;
1380 /* If we have just one "menu item"
1381 that was originally a button, return it by itself. */
1382 if (top_level_items
&& first_wv
->contents
&& first_wv
->contents
->next
== 0)
1384 wv
= first_wv
->contents
;
1385 free_widget_value (first_wv
);
1392 /* Walk through the widget_value tree starting at FIRST_WV and update
1393 the char * pointers from the corresponding lisp values.
1394 We do this after building the whole tree, since GC may happen while the
1395 tree is constructed, and small strings are relocated. So we must wait
1396 until no GC can happen before storing pointers into lisp values. */
1398 update_submenu_strings (first_wv
)
1399 widget_value
*first_wv
;
1403 for (wv
= first_wv
; wv
; wv
= wv
->next
)
1405 if (STRINGP (wv
->lname
))
1407 wv
->name
= SDATA (wv
->lname
);
1409 /* Ignore the @ that means "separate pane".
1410 This is a kludge, but this isn't worth more time. */
1411 if (wv
->value
== (char *)1)
1413 if (wv
->name
[0] == '@')
1419 if (STRINGP (wv
->lkey
))
1420 wv
->key
= SDATA (wv
->lkey
);
1423 update_submenu_strings (wv
->contents
);
1428 /* Event handler function that pops down a menu on C-g. We can only pop
1429 down menus if CancelMenuTracking is present (OSX 10.3 or later). */
1431 #ifdef HAVE_CANCELMENUTRACKING
1432 static pascal OSStatus
1433 menu_quit_handler (nextHandler
, theEvent
, userData
)
1434 EventHandlerCallRef nextHandler
;
1440 UInt32 keyModifiers
;
1441 extern int mac_quit_char_modifiers
;
1442 extern int mac_quit_char_keycode
;
1444 err
= GetEventParameter (theEvent
, kEventParamKeyCode
,
1445 typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
1448 err
= GetEventParameter (theEvent
, kEventParamKeyModifiers
,
1449 typeUInt32
, NULL
, sizeof(UInt32
),
1450 NULL
, &keyModifiers
);
1452 if (err
== noErr
&& keyCode
== mac_quit_char_keycode
1453 && keyModifiers
== mac_quit_char_modifiers
)
1455 MenuRef menu
= userData
!= 0
1456 ? (MenuRef
)userData
: AcquireRootMenu ();
1458 CancelMenuTracking (menu
, true, 0);
1459 if (!userData
) ReleaseMenu (menu
);
1463 return CallNextEventHandler (nextHandler
, theEvent
);
1465 #endif /* HAVE_CANCELMENUTRACKING */
1467 /* Add event handler to all menus that belong to KIND so we can detect C-g.
1468 MENU_HANDLE is the root menu of the tracking session to dismiss
1469 when C-g is detected. NULL means the menu bar.
1470 If CancelMenuTracking isn't available, do nothing. */
1473 install_menu_quit_handler (kind
, menu_handle
)
1474 enum mac_menu_kind kind
;
1475 MenuHandle menu_handle
;
1477 #ifdef HAVE_CANCELMENUTRACKING
1478 EventTypeSpec typesList
[] = { { kEventClassKeyboard
, kEventRawKeyDown
} };
1481 for (id
= min_menu_id
[kind
]; id
< min_menu_id
[kind
+ 1]; id
++)
1483 MenuHandle menu
= GetMenuHandle (id
);
1487 InstallMenuEventHandler (menu
, menu_quit_handler
,
1488 GetEventTypeCount (typesList
),
1489 typesList
, menu_handle
, NULL
);
1491 #endif /* HAVE_CANCELMENUTRACKING */
1494 /* Set the contents of the menubar widgets of frame F.
1495 The argument FIRST_TIME is currently ignored;
1496 it is set the first time this is called, from initialize_frame_menubar. */
1499 set_frame_menubar (f
, first_time
, deep_p
)
1504 int menubar_widget
= f
->output_data
.mac
->menubar_widget
;
1506 widget_value
*wv
, *first_wv
, *prev_wv
= 0;
1508 int *submenu_start
, *submenu_end
;
1509 int *submenu_top_level_items
, *submenu_n_panes
;
1511 /* We must not change the menubar when actually in use. */
1512 if (f
->output_data
.mac
->menubar_active
)
1515 XSETFRAME (Vmenu_updating_frame
, f
);
1517 if (! menubar_widget
)
1519 else if (pending_menu_activation
&& !deep_p
)
1524 /* Make a widget-value tree representing the entire menu trees. */
1526 struct buffer
*prev
= current_buffer
;
1528 int specpdl_count
= SPECPDL_INDEX ();
1529 int previous_menu_items_used
= f
->menu_bar_items_used
;
1530 Lisp_Object
*previous_items
1531 = (Lisp_Object
*) alloca (previous_menu_items_used
1532 * sizeof (Lisp_Object
));
1534 /* If we are making a new widget, its contents are empty,
1535 do always reinitialize them. */
1536 if (! menubar_widget
)
1537 previous_menu_items_used
= 0;
1539 buffer
= XWINDOW (FRAME_SELECTED_WINDOW (f
))->buffer
;
1540 specbind (Qinhibit_quit
, Qt
);
1541 /* Don't let the debugger step into this code
1542 because it is not reentrant. */
1543 specbind (Qdebug_on_next_call
, Qnil
);
1545 record_unwind_save_match_data ();
1546 if (NILP (Voverriding_local_map_menu_flag
))
1548 specbind (Qoverriding_terminal_local_map
, Qnil
);
1549 specbind (Qoverriding_local_map
, Qnil
);
1552 set_buffer_internal_1 (XBUFFER (buffer
));
1554 /* Run the Lucid hook. */
1555 safe_run_hooks (Qactivate_menubar_hook
);
1557 /* If it has changed current-menubar from previous value,
1558 really recompute the menubar from the value. */
1559 if (! NILP (Vlucid_menu_bar_dirty_flag
))
1560 call0 (Qrecompute_lucid_menubar
);
1561 safe_run_hooks (Qmenu_bar_update_hook
);
1562 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
1564 items
= FRAME_MENU_BAR_ITEMS (f
);
1566 /* Save the frame's previous menu bar contents data. */
1567 if (previous_menu_items_used
)
1568 bcopy (XVECTOR (f
->menu_bar_vector
)->contents
, previous_items
,
1569 previous_menu_items_used
* sizeof (Lisp_Object
));
1571 /* Fill in menu_items with the current menu bar contents.
1572 This can evaluate Lisp code. */
1575 menu_items
= f
->menu_bar_vector
;
1576 menu_items_allocated
= VECTORP (menu_items
) ? ASIZE (menu_items
) : 0;
1577 submenu_start
= (int *) alloca (XVECTOR (items
)->size
* sizeof (int *));
1578 submenu_end
= (int *) alloca (XVECTOR (items
)->size
* sizeof (int *));
1579 submenu_n_panes
= (int *) alloca (XVECTOR (items
)->size
* sizeof (int));
1580 submenu_top_level_items
1581 = (int *) alloca (XVECTOR (items
)->size
* sizeof (int *));
1583 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
1585 Lisp_Object key
, string
, maps
;
1589 key
= XVECTOR (items
)->contents
[i
];
1590 string
= XVECTOR (items
)->contents
[i
+ 1];
1591 maps
= XVECTOR (items
)->contents
[i
+ 2];
1595 submenu_start
[i
] = menu_items_used
;
1597 menu_items_n_panes
= 0;
1598 submenu_top_level_items
[i
]
1599 = parse_single_submenu (key
, string
, maps
);
1600 submenu_n_panes
[i
] = menu_items_n_panes
;
1602 submenu_end
[i
] = menu_items_used
;
1605 finish_menu_items ();
1607 /* Convert menu_items into widget_value trees
1608 to display the menu. This cannot evaluate Lisp code. */
1610 wv
= xmalloc_widget_value ();
1611 wv
->name
= "menubar";
1614 wv
->button_type
= BUTTON_TYPE_NONE
;
1618 for (i
= 0; i
< last_i
; i
+= 4)
1620 menu_items_n_panes
= submenu_n_panes
[i
];
1621 wv
= digest_single_submenu (submenu_start
[i
], submenu_end
[i
],
1622 submenu_top_level_items
[i
]);
1626 first_wv
->contents
= wv
;
1627 /* Don't set wv->name here; GC during the loop might relocate it. */
1629 wv
->button_type
= BUTTON_TYPE_NONE
;
1633 set_buffer_internal_1 (prev
);
1635 /* If there has been no change in the Lisp-level contents
1636 of the menu bar, skip redisplaying it. Just exit. */
1638 /* Compare the new menu items with the ones computed last time. */
1639 for (i
= 0; i
< previous_menu_items_used
; i
++)
1640 if (menu_items_used
== i
1641 || (!EQ (previous_items
[i
], XVECTOR (menu_items
)->contents
[i
])))
1643 if (i
== menu_items_used
&& i
== previous_menu_items_used
&& i
!= 0)
1645 /* The menu items have not changed. Don't bother updating
1646 the menus in any form, since it would be a no-op. */
1647 free_menubar_widget_value_tree (first_wv
);
1648 discard_menu_items ();
1649 unbind_to (specpdl_count
, Qnil
);
1653 /* The menu items are different, so store them in the frame. */
1654 f
->menu_bar_vector
= menu_items
;
1655 f
->menu_bar_items_used
= menu_items_used
;
1657 /* This calls restore_menu_items to restore menu_items, etc.,
1658 as they were outside. */
1659 unbind_to (specpdl_count
, Qnil
);
1661 /* Now GC cannot happen during the lifetime of the widget_value,
1662 so it's safe to store data from a Lisp_String. */
1663 wv
= first_wv
->contents
;
1664 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
1667 string
= XVECTOR (items
)->contents
[i
+ 1];
1670 wv
->name
= (char *) SDATA (string
);
1671 update_submenu_strings (wv
->contents
);
1678 /* Make a widget-value tree containing
1679 just the top level menu bar strings. */
1681 wv
= xmalloc_widget_value ();
1682 wv
->name
= "menubar";
1685 wv
->button_type
= BUTTON_TYPE_NONE
;
1689 items
= FRAME_MENU_BAR_ITEMS (f
);
1690 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
1694 string
= XVECTOR (items
)->contents
[i
+ 1];
1698 wv
= xmalloc_widget_value ();
1699 wv
->name
= (char *) SDATA (string
);
1702 wv
->button_type
= BUTTON_TYPE_NONE
;
1704 /* This prevents lwlib from assuming this
1705 menu item is really supposed to be empty. */
1706 /* The EMACS_INT cast avoids a warning.
1707 This value just has to be different from small integers. */
1708 wv
->call_data
= (void *) (EMACS_INT
) (-1);
1713 first_wv
->contents
= wv
;
1717 /* Forget what we thought we knew about what is in the
1718 detailed contents of the menu bar menus.
1719 Changing the top level always destroys the contents. */
1720 f
->menu_bar_items_used
= 0;
1723 /* Create or update the menu bar widget. */
1727 /* Non-null value to indicate menubar has already been "created". */
1728 f
->output_data
.mac
->menubar_widget
= 1;
1730 fill_menubar (first_wv
->contents
, deep_p
);
1732 /* Add event handler so we can detect C-g. */
1733 install_menu_quit_handler (MAC_MENU_MENU_BAR
, NULL
);
1734 install_menu_quit_handler (MAC_MENU_MENU_BAR_SUB
, NULL
);
1735 free_menubar_widget_value_tree (first_wv
);
1740 /* Get rid of the menu bar of frame F, and free its storage.
1741 This is used when deleting a frame, and when turning off the menu bar. */
1744 free_frame_menubar (f
)
1747 f
->output_data
.mac
->menubar_widget
= 0;
1755 struct Lisp_Save_Value
*p
= XSAVE_VALUE (arg
);
1756 FRAME_PTR f
= p
->pointer
;
1757 MenuHandle menu
= GetMenuHandle (min_menu_id
[MAC_MENU_POPUP
]);
1761 /* Must reset this manually because the button release event is not
1762 passed to Emacs event loop. */
1763 FRAME_MAC_DISPLAY_INFO (f
)->grabbed
= 0;
1765 /* delete all menus */
1766 dispose_menus (MAC_MENU_POPUP_SUB
, 0);
1767 DeleteMenu (min_menu_id
[MAC_MENU_POPUP
]);
1775 /* Mac_menu_show actually displays a menu using the panes and items in
1776 menu_items and returns the value selected from it; we assume input
1777 is blocked by the caller. */
1779 /* F is the frame the menu is for.
1780 X and Y are the frame-relative specified position,
1781 relative to the inside upper left corner of the frame F.
1782 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
1783 KEYMAPS is 1 if this menu was specified with keymaps;
1784 in that case, we return a list containing the chosen item's value
1785 and perhaps also the pane's prefix.
1786 TITLE is the specified menu title.
1787 ERROR is a place to store an error message string in case of failure.
1788 (We return nil on failure, but the value doesn't actually matter.) */
1791 mac_menu_show (f
, x
, y
, for_click
, keymaps
, title
, error
)
1802 int menu_item_choice
;
1803 int menu_item_selection
;
1806 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1807 widget_value
**submenu_stack
1808 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1809 Lisp_Object
*subprefix_stack
1810 = (Lisp_Object
*) alloca (menu_items_used
* sizeof (Lisp_Object
));
1811 int submenu_depth
= 0;
1814 int specpdl_count
= SPECPDL_INDEX ();
1818 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
1820 *error
= "Empty menu";
1824 /* Create a tree of widget_value objects
1825 representing the panes and their items. */
1826 wv
= xmalloc_widget_value ();
1830 wv
->button_type
= BUTTON_TYPE_NONE
;
1835 /* Loop over all panes and items, filling in the tree. */
1837 while (i
< menu_items_used
)
1839 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1841 submenu_stack
[submenu_depth
++] = save_wv
;
1847 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1850 save_wv
= submenu_stack
[--submenu_depth
];
1854 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1855 && submenu_depth
!= 0)
1856 i
+= MENU_ITEMS_PANE_LENGTH
;
1857 /* Ignore a nil in the item list.
1858 It's meaningful only for dialog boxes. */
1859 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1861 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1863 /* Create a new pane. */
1864 Lisp_Object pane_name
, prefix
;
1867 pane_name
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
);
1868 prefix
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
1870 #ifndef HAVE_MULTILINGUAL_MENU
1871 if (STRINGP (pane_name
) && STRING_MULTIBYTE (pane_name
))
1873 pane_name
= ENCODE_MENU_STRING (pane_name
);
1874 AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
) = pane_name
;
1877 pane_string
= (NILP (pane_name
)
1878 ? "" : (char *) SDATA (pane_name
));
1879 /* If there is just one top-level pane, put all its items directly
1880 under the top-level menu. */
1881 if (menu_items_n_panes
== 1)
1884 /* If the pane has a meaningful name,
1885 make the pane a top-level menu item
1886 with its items as a submenu beneath it. */
1887 if (!keymaps
&& strcmp (pane_string
, ""))
1889 wv
= xmalloc_widget_value ();
1893 first_wv
->contents
= wv
;
1894 wv
->name
= pane_string
;
1895 if (keymaps
&& !NILP (prefix
))
1899 wv
->button_type
= BUTTON_TYPE_NONE
;
1904 else if (first_pane
)
1910 i
+= MENU_ITEMS_PANE_LENGTH
;
1914 /* Create a new item within current pane. */
1915 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
, help
;
1916 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
1917 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
1918 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
1919 def
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_DEFINITION
);
1920 type
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_TYPE
);
1921 selected
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_SELECTED
);
1922 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
1924 #ifndef HAVE_MULTILINGUAL_MENU
1925 if (STRINGP (item_name
) && STRING_MULTIBYTE (item_name
))
1927 item_name
= ENCODE_MENU_STRING (item_name
);
1928 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
) = item_name
;
1931 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
1933 descrip
= ENCODE_MENU_STRING (descrip
);
1934 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
) = descrip
;
1936 #endif /* not HAVE_MULTILINGUAL_MENU */
1938 wv
= xmalloc_widget_value ();
1942 save_wv
->contents
= wv
;
1943 wv
->name
= (char *) SDATA (item_name
);
1944 if (!NILP (descrip
))
1945 wv
->key
= (char *) SDATA (descrip
);
1947 /* Use the contents index as call_data, since we are
1948 restricted to 16-bits. */
1949 wv
->call_data
= !NILP (def
) ? (void *) (EMACS_INT
) i
: 0;
1950 wv
->enabled
= !NILP (enable
);
1953 wv
->button_type
= BUTTON_TYPE_NONE
;
1954 else if (EQ (type
, QCtoggle
))
1955 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
1956 else if (EQ (type
, QCradio
))
1957 wv
->button_type
= BUTTON_TYPE_RADIO
;
1961 wv
->selected
= !NILP (selected
);
1963 if (! STRINGP (help
))
1970 i
+= MENU_ITEMS_ITEM_LENGTH
;
1974 /* Deal with the title, if it is non-nil. */
1977 widget_value
*wv_title
= xmalloc_widget_value ();
1978 widget_value
*wv_sep
= xmalloc_widget_value ();
1980 /* Maybe replace this separator with a bitmap or owner-draw item
1981 so that it looks better. Having two separators looks odd. */
1982 wv_sep
->name
= "--";
1983 wv_sep
->next
= first_wv
->contents
;
1984 wv_sep
->help
= Qnil
;
1986 #ifndef HAVE_MULTILINGUAL_MENU
1987 if (STRING_MULTIBYTE (title
))
1988 title
= ENCODE_MENU_STRING (title
);
1991 wv_title
->name
= (char *) SDATA (title
);
1992 wv_title
->enabled
= FALSE
;
1993 wv_title
->title
= TRUE
;
1994 wv_title
->button_type
= BUTTON_TYPE_NONE
;
1995 wv_title
->help
= Qnil
;
1996 wv_title
->next
= wv_sep
;
1997 first_wv
->contents
= wv_title
;
2000 /* Actually create the menu. */
2001 menu
= NewMenu (min_menu_id
[MAC_MENU_POPUP
], "\p");
2002 InsertMenu (menu
, -1);
2003 fill_menu (menu
, first_wv
->contents
, MAC_MENU_POPUP_SUB
,
2004 min_menu_id
[MAC_MENU_POPUP_SUB
]);
2006 /* Free the widget_value objects we used to specify the
2008 free_menubar_widget_value_tree (first_wv
);
2010 /* Adjust coordinates to be root-window-relative. */
2014 SetPortWindowPort (FRAME_MAC_WINDOW (f
));
2015 LocalToGlobal (&pos
);
2017 /* No selection has been chosen yet. */
2018 menu_item_choice
= 0;
2019 menu_item_selection
= 0;
2021 record_unwind_protect (pop_down_menu
, make_save_value (f
, 0));
2023 /* Add event handler so we can detect C-g. */
2024 install_menu_quit_handler (MAC_MENU_POPUP
, menu
);
2025 install_menu_quit_handler (MAC_MENU_POPUP_SUB
, menu
);
2027 /* Display the menu. */
2028 menu_item_choice
= PopUpMenuSelect (menu
, pos
.v
, pos
.h
, 0);
2029 menu_item_selection
= LoWord (menu_item_choice
);
2031 /* Get the refcon to find the correct item */
2032 if (menu_item_selection
)
2034 MenuHandle sel_menu
= GetMenuHandle (HiWord (menu_item_choice
));
2036 GetMenuItemRefCon (sel_menu
, menu_item_selection
, &refcon
);
2039 else if (! for_click
)
2040 /* Make "Cancel" equivalent to C-g unless this menu was popped up by
2042 Fsignal (Qquit
, Qnil
);
2044 /* Find the selected item, and its pane, to return
2045 the proper value. */
2046 if (menu_item_selection
!= 0)
2048 Lisp_Object prefix
, entry
;
2050 prefix
= entry
= Qnil
;
2052 while (i
< menu_items_used
)
2054 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
2056 subprefix_stack
[submenu_depth
++] = prefix
;
2060 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
2062 prefix
= subprefix_stack
[--submenu_depth
];
2065 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2068 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2069 i
+= MENU_ITEMS_PANE_LENGTH
;
2071 /* Ignore a nil in the item list.
2072 It's meaningful only for dialog boxes. */
2073 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2078 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2079 if ((int) (EMACS_INT
) refcon
== i
)
2085 entry
= Fcons (entry
, Qnil
);
2087 entry
= Fcons (prefix
, entry
);
2088 for (j
= submenu_depth
- 1; j
>= 0; j
--)
2089 if (!NILP (subprefix_stack
[j
]))
2090 entry
= Fcons (subprefix_stack
[j
], entry
);
2094 i
+= MENU_ITEMS_ITEM_LENGTH
;
2098 else if (!for_click
)
2099 /* Make "Cancel" equivalent to C-g. */
2100 Fsignal (Qquit
, Qnil
);
2102 unbind_to (specpdl_count
, Qnil
);
2109 /* Construct native Mac OS menubar based on widget_value tree. */
2112 mac_dialog (widget_value
*wv
)
2116 char **button_labels
;
2123 WindowPtr window_ptr
;
2126 EventRecord event_record
;
2128 int control_part_code
;
2131 dialog_name
= wv
->name
;
2132 nb_buttons
= dialog_name
[1] - '0';
2133 left_count
= nb_buttons
- (dialog_name
[4] - '0');
2134 button_labels
= (char **) alloca (sizeof (char *) * nb_buttons
);
2135 ref_cons
= (UInt32
*) alloca (sizeof (UInt32
) * nb_buttons
);
2138 prompt
= (char *) alloca (strlen (wv
->value
) + 1);
2139 strcpy (prompt
, wv
->value
);
2143 for (i
= 0; i
< nb_buttons
; i
++)
2145 button_labels
[i
] = wv
->value
;
2146 button_labels
[i
] = (char *) alloca (strlen (wv
->value
) + 1);
2147 strcpy (button_labels
[i
], wv
->value
);
2148 c2pstr (button_labels
[i
]);
2149 ref_cons
[i
] = (UInt32
) wv
->call_data
;
2153 window_ptr
= GetNewCWindow (DIALOG_WINDOW_RESOURCE
, NULL
, (WindowPtr
) -1);
2155 SetPortWindowPort (window_ptr
);
2158 /* Left and right margins in the dialog are 13 pixels each.*/
2160 /* Calculate width of dialog box: 8 pixels on each side of the text
2161 label in each button, 12 pixels between buttons. */
2162 for (i
= 0; i
< nb_buttons
; i
++)
2163 dialog_width
+= StringWidth (button_labels
[i
]) + 16 + 12;
2165 if (left_count
!= 0 && nb_buttons
- left_count
!= 0)
2168 dialog_width
= max (dialog_width
, StringWidth (prompt
) + 26);
2170 SizeWindow (window_ptr
, dialog_width
, 78, 0);
2171 ShowWindow (window_ptr
);
2173 SetPortWindowPort (window_ptr
);
2178 DrawString (prompt
);
2181 for (i
= 0; i
< nb_buttons
; i
++)
2183 int button_width
= StringWidth (button_labels
[i
]) + 16;
2184 SetRect (&rect
, left
, 45, left
+ button_width
, 65);
2185 ch
= NewControl (window_ptr
, &rect
, button_labels
[i
], 1, 0, 0, 0,
2186 kControlPushButtonProc
, ref_cons
[i
]);
2187 left
+= button_width
+ 12;
2188 if (i
== left_count
- 1)
2195 if (WaitNextEvent (mDownMask
, &event_record
, 10, NULL
))
2196 if (event_record
.what
== mouseDown
)
2198 part_code
= FindWindow (event_record
.where
, &window_ptr
);
2199 if (part_code
== inContent
)
2201 mouse
= event_record
.where
;
2202 GlobalToLocal (&mouse
);
2203 control_part_code
= FindControl (mouse
, window_ptr
, &ch
);
2204 if (control_part_code
== kControlButtonPart
)
2205 if (TrackControl (ch
, mouse
, NULL
))
2206 i
= GetControlReference (ch
);
2211 DisposeWindow (window_ptr
);
2216 static char * button_names
[] = {
2217 "button1", "button2", "button3", "button4", "button5",
2218 "button6", "button7", "button8", "button9", "button10" };
2221 mac_dialog_show (f
, keymaps
, title
, header
, error_name
)
2224 Lisp_Object title
, header
;
2227 int i
, nb_buttons
=0;
2228 char dialog_name
[6];
2229 int menu_item_selection
;
2231 widget_value
*wv
, *first_wv
= 0, *prev_wv
= 0;
2233 /* Number of elements seen so far, before boundary. */
2235 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2236 int boundary_seen
= 0;
2240 if (menu_items_n_panes
> 1)
2242 *error_name
= "Multiple panes in dialog box";
2246 /* Create a tree of widget_value objects
2247 representing the text label and buttons. */
2249 Lisp_Object pane_name
, prefix
;
2251 pane_name
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
];
2252 prefix
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_PREFIX
];
2253 pane_string
= (NILP (pane_name
)
2254 ? "" : (char *) SDATA (pane_name
));
2255 prev_wv
= xmalloc_widget_value ();
2256 prev_wv
->value
= pane_string
;
2257 if (keymaps
&& !NILP (prefix
))
2259 prev_wv
->enabled
= 1;
2260 prev_wv
->name
= "message";
2261 prev_wv
->help
= Qnil
;
2264 /* Loop over all panes and items, filling in the tree. */
2265 i
= MENU_ITEMS_PANE_LENGTH
;
2266 while (i
< menu_items_used
)
2269 /* Create a new item within current pane. */
2270 Lisp_Object item_name
, enable
, descrip
;
2271 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
2272 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
2274 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
2276 if (NILP (item_name
))
2278 free_menubar_widget_value_tree (first_wv
);
2279 *error_name
= "Submenu in dialog items";
2282 if (EQ (item_name
, Qquote
))
2284 /* This is the boundary between left-side elts
2285 and right-side elts. Stop incrementing right_count. */
2290 if (nb_buttons
>= 9)
2292 free_menubar_widget_value_tree (first_wv
);
2293 *error_name
= "Too many dialog items";
2297 wv
= xmalloc_widget_value ();
2299 wv
->name
= (char *) button_names
[nb_buttons
];
2300 if (!NILP (descrip
))
2301 wv
->key
= (char *) SDATA (descrip
);
2302 wv
->value
= (char *) SDATA (item_name
);
2303 wv
->call_data
= (void *) i
;
2304 /* menu item is identified by its index in menu_items table */
2305 wv
->enabled
= !NILP (enable
);
2309 if (! boundary_seen
)
2313 i
+= MENU_ITEMS_ITEM_LENGTH
;
2316 /* If the boundary was not specified,
2317 by default put half on the left and half on the right. */
2318 if (! boundary_seen
)
2319 left_count
= nb_buttons
- nb_buttons
/ 2;
2321 wv
= xmalloc_widget_value ();
2322 wv
->name
= dialog_name
;
2325 /* Frame title: 'Q' = Question, 'I' = Information.
2326 Can also have 'E' = Error if, one day, we want
2327 a popup for errors. */
2329 dialog_name
[0] = 'Q';
2331 dialog_name
[0] = 'I';
2333 /* Dialog boxes use a really stupid name encoding
2334 which specifies how many buttons to use
2335 and how many buttons are on the right. */
2336 dialog_name
[1] = '0' + nb_buttons
;
2337 dialog_name
[2] = 'B';
2338 dialog_name
[3] = 'R';
2339 /* Number of buttons to put on the right. */
2340 dialog_name
[4] = '0' + nb_buttons
- left_count
;
2342 wv
->contents
= first_wv
;
2346 /* Actually create the dialog. */
2348 menu_item_selection
= mac_dialog (first_wv
);
2350 menu_item_selection
= 0;
2353 /* Free the widget_value objects we used to specify the contents. */
2354 free_menubar_widget_value_tree (first_wv
);
2356 /* Find the selected item, and its pane, to return
2357 the proper value. */
2358 if (menu_item_selection
!= 0)
2364 while (i
< menu_items_used
)
2368 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2371 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2372 i
+= MENU_ITEMS_PANE_LENGTH
;
2374 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2376 /* This is the boundary between left-side elts and
2383 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2384 if (menu_item_selection
== i
)
2388 entry
= Fcons (entry
, Qnil
);
2390 entry
= Fcons (prefix
, entry
);
2394 i
+= MENU_ITEMS_ITEM_LENGTH
;
2399 /* Make "Cancel" equivalent to C-g. */
2400 Fsignal (Qquit
, Qnil
);
2404 #endif /* HAVE_DIALOGS */
2407 /* Is this item a separator? */
2409 name_is_separator (name
)
2414 /* Check if name string consists of only dashes ('-'). */
2415 while (*name
== '-') name
++;
2416 /* Separators can also be of the form "--:TripleSuperMegaEtched"
2417 or "--deep-shadow". We don't implement them yet, se we just treat
2418 them like normal separators. */
2419 return (*name
== '\0' || start
+ 2 == name
);
2423 add_menu_item (menu
, pos
, wv
)
2428 #if TARGET_API_MAC_CARBON
2429 CFStringRef item_name
;
2434 if (name_is_separator (wv
->name
))
2435 AppendMenu (menu
, "\p-");
2438 AppendMenu (menu
, "\pX");
2440 #if TARGET_API_MAC_CARBON
2441 item_name
= cfstring_create_with_utf8_cstring (wv
->name
);
2443 if (wv
->key
!= NULL
)
2445 CFStringRef name
, key
;
2448 key
= cfstring_create_with_utf8_cstring (wv
->key
);
2449 item_name
= CFStringCreateWithFormat (NULL
, NULL
, CFSTR ("%@ %@"),
2455 SetMenuItemTextWithCFString (menu
, pos
, item_name
);
2456 CFRelease (item_name
);
2459 EnableMenuItem (menu
, pos
);
2461 DisableMenuItem (menu
, pos
);
2462 #else /* ! TARGET_API_MAC_CARBON */
2463 item_name
[sizeof (item_name
) - 1] = '\0';
2464 strncpy (item_name
, wv
->name
, sizeof (item_name
) - 1);
2465 if (wv
->key
!= NULL
)
2467 int len
= strlen (item_name
);
2469 strncpy (item_name
+ len
, " ", sizeof (item_name
) - 1 - len
);
2470 len
= strlen (item_name
);
2471 strncpy (item_name
+ len
, wv
->key
, sizeof (item_name
) - 1 - len
);
2474 SetMenuItemText (menu
, pos
, item_name
);
2477 EnableItem (menu
, pos
);
2479 DisableItem (menu
, pos
);
2480 #endif /* ! TARGET_API_MAC_CARBON */
2482 /* Draw radio buttons and tickboxes. */
2483 if (wv
->selected
&& (wv
->button_type
== BUTTON_TYPE_TOGGLE
||
2484 wv
->button_type
== BUTTON_TYPE_RADIO
))
2485 SetItemMark (menu
, pos
, checkMark
);
2487 SetItemMark (menu
, pos
, noMark
);
2489 SetMenuItemRefCon (menu
, pos
, (UInt32
) wv
->call_data
);
2493 /* Construct native Mac OS menu based on widget_value tree. */
2496 fill_menu (menu
, wv
, kind
, submenu_id
)
2499 enum mac_menu_kind kind
;
2504 for (pos
= 1; wv
!= NULL
; wv
= wv
->next
, pos
++)
2506 add_menu_item (menu
, pos
, wv
);
2507 if (wv
->contents
&& submenu_id
< min_menu_id
[kind
+ 1])
2509 MenuHandle submenu
= NewMenu (submenu_id
, "\pX");
2511 InsertMenu (submenu
, -1);
2512 SetMenuItemHierarchicalID (menu
, pos
, submenu_id
);
2513 submenu_id
= fill_menu (submenu
, wv
->contents
, kind
, submenu_id
+ 1);
2520 /* Construct native Mac OS menubar based on widget_value tree. */
2523 fill_menubar (wv
, deep_p
)
2530 #if !TARGET_API_MAC_CARBON
2531 int title_changed_p
= 0;
2534 /* Clean up the menu bar when filled by the entire menu trees. */
2537 dispose_menus (MAC_MENU_MENU_BAR
, 0);
2538 dispose_menus (MAC_MENU_MENU_BAR_SUB
, 0);
2539 #if !TARGET_API_MAC_CARBON
2540 title_changed_p
= 1;
2544 /* Fill menu bar titles and submenus. Reuse the existing menu bar
2545 titles as much as possible to minimize redraw (if !deep_p). */
2546 submenu_id
= min_menu_id
[MAC_MENU_MENU_BAR_SUB
];
2547 for (id
= min_menu_id
[MAC_MENU_MENU_BAR
];
2548 wv
!= NULL
&& id
< min_menu_id
[MAC_MENU_MENU_BAR
+ 1];
2549 wv
= wv
->next
, id
++)
2551 strncpy (title
, wv
->name
, 255);
2555 menu
= GetMenuHandle (id
);
2558 #if TARGET_API_MAC_CARBON
2561 GetMenuTitle (menu
, old_title
);
2562 if (!EqualString (title
, old_title
, false, false))
2563 SetMenuTitle (menu
, title
);
2564 #else /* !TARGET_API_MAC_CARBON */
2565 if (!EqualString (title
, (*menu
)->menuData
, false, false))
2569 menu
= NewMenu (id
, title
);
2570 InsertMenu (menu
, GetMenuHandle (id
+ 1) ? id
+ 1 : 0);
2571 title_changed_p
= 1;
2573 #endif /* !TARGET_API_MAC_CARBON */
2577 menu
= NewMenu (id
, title
);
2578 InsertMenu (menu
, 0);
2579 #if !TARGET_API_MAC_CARBON
2580 title_changed_p
= 1;
2585 submenu_id
= fill_menu (menu
, wv
->contents
, MAC_MENU_MENU_BAR_SUB
,
2589 if (id
< min_menu_id
[MAC_MENU_MENU_BAR
+ 1] && GetMenuHandle (id
))
2591 dispose_menus (MAC_MENU_MENU_BAR
, id
);
2592 #if !TARGET_API_MAC_CARBON
2593 title_changed_p
= 1;
2597 #if !TARGET_API_MAC_CARBON
2598 if (title_changed_p
)
2603 /* Dispose of menus that belong to KIND, and remove them from the menu
2604 list. ID is the lower bound of menu IDs that will be processed. */
2607 dispose_menus (kind
, id
)
2608 enum mac_menu_kind kind
;
2611 for (id
= max (id
, min_menu_id
[kind
]); id
< min_menu_id
[kind
+ 1]; id
++)
2613 MenuHandle menu
= GetMenuHandle (id
);
2622 #endif /* HAVE_MENUS */
2627 staticpro (&menu_items
);
2630 Qdebug_on_next_call
= intern ("debug-on-next-call");
2631 staticpro (&Qdebug_on_next_call
);
2633 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame
,
2634 doc
: /* Frame for which we are updating a menu.
2635 The enable predicate for a menu command should check this variable. */);
2636 Vmenu_updating_frame
= Qnil
;
2638 defsubr (&Sx_popup_menu
);
2640 defsubr (&Sx_popup_dialog
);
2644 /* arch-tag: 40b2c6c7-b8a9-4a49-b930-1b2707184cce
2645 (do not change this comment) */