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 .. 233 */
68 MAC_MENU_M_APPLE
, /* 234 (== M_APPLE) */
69 MAC_MENU_POPUP
, /* 235 */
70 MAC_MENU_DRIVER
, /* 236 .. 255 (Reserved) */
71 MAC_MENU_MENU_BAR_SUB
, /* 256 .. 16383 */
72 MAC_MENU_POPUP_SUB
, /* 16384 .. 32767 */
73 MAC_MENU_END
/* 32768 */
76 static const int min_menu_id
[] = {0, 1, 234, 235, 236, 256, 16384, 32768};
78 #define DIALOG_WINDOW_RESOURCE 130
80 #define HAVE_DIALOGS 1
82 #undef HAVE_MULTILINGUAL_MENU
83 #undef HAVE_DIALOGS /* TODO: Implement native dialogs. */
85 /******************************************************************/
86 /* Definitions copied from lwlib.h */
88 typedef void * XtPointer
;
97 /* This structure is based on the one in ../lwlib/lwlib.h, modified
99 typedef struct _widget_value
104 /* value (meaning depend on widget type) */
106 /* keyboard equivalent. no implications for XtTranslations */
109 /* Help string or nil if none.
110 GC finds this string through the frame's menu_bar_vector
111 or through menu_items. */
113 /* true if enabled */
115 /* true if selected */
117 /* The type of a button. */
118 enum button_type button_type
;
119 /* true if menu title */
122 /* true if was edited (maintained by get_value) */
124 /* true if has changed (maintained by lw library) */
126 /* true if this widget itself has changed,
127 but not counting the other widgets found in the `next' field. */
128 change_type this_one_change
;
130 /* Contents of the sub-widgets, also selected slot for checkbox */
131 struct _widget_value
* contents
;
132 /* data passed to callback */
134 /* next one in the list */
135 struct _widget_value
* next
;
137 /* slot for the toolkit dependent part. Always initialize to NULL. */
139 /* tell us if we should free the toolkit data slot when freeing the
140 widget_value itself. */
141 Boolean free_toolkit_data
;
143 /* we resource the widget_value structures; this points to the next
144 one on the free list if this one has been deallocated.
146 struct _widget_value
*free_list
;
150 /* Assumed by other routines to zero area returned. */
151 #define malloc_widget_value() (void *)memset (xmalloc (sizeof (widget_value)),\
152 0, (sizeof (widget_value)))
153 #define free_widget_value(wv) xfree (wv)
155 /******************************************************************/
162 Lisp_Object Qdebug_on_next_call
;
164 extern Lisp_Object Vmenu_updating_frame
;
166 extern Lisp_Object Qmenu_bar
, Qmac_apple_event
;
168 extern Lisp_Object QCtoggle
, QCradio
;
170 extern Lisp_Object Voverriding_local_map
;
171 extern Lisp_Object Voverriding_local_map_menu_flag
;
173 extern Lisp_Object Qoverriding_local_map
, Qoverriding_terminal_local_map
;
175 extern Lisp_Object Qmenu_bar_update_hook
;
177 void set_frame_menubar
P_ ((FRAME_PTR
, int, int));
179 #if TARGET_API_MAC_CARBON
180 #define ENCODE_MENU_STRING(str) ENCODE_UTF_8 (str)
182 #define ENCODE_MENU_STRING(str) ENCODE_SYSTEM (str)
185 static void push_menu_item
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
,
186 Lisp_Object
, Lisp_Object
, Lisp_Object
,
187 Lisp_Object
, Lisp_Object
));
189 static Lisp_Object mac_dialog_show
P_ ((FRAME_PTR
, int, Lisp_Object
,
190 Lisp_Object
, char **));
192 static Lisp_Object mac_menu_show
P_ ((struct frame
*, int, int, int, int,
193 Lisp_Object
, char **));
194 static void keymap_panes
P_ ((Lisp_Object
*, int, int));
195 static void single_keymap_panes
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
,
197 static void list_of_panes
P_ ((Lisp_Object
));
198 static void list_of_items
P_ ((Lisp_Object
));
200 static void find_and_call_menu_selection
P_ ((FRAME_PTR
, int, Lisp_Object
,
202 static int fill_menu
P_ ((MenuHandle
, widget_value
*, enum mac_menu_kind
, int));
203 static void fill_menubar
P_ ((widget_value
*, int));
204 static void dispose_menus
P_ ((enum mac_menu_kind
, int));
207 /* This holds a Lisp vector that holds the results of decoding
208 the keymaps or alist-of-alists that specify a menu.
210 It describes the panes and items within the panes.
212 Each pane is described by 3 elements in the vector:
213 t, the pane name, the pane's prefix key.
214 Then follow the pane's items, with 5 elements per item:
215 the item string, the enable flag, the item's value,
216 the definition, and the equivalent keyboard key's description string.
218 In some cases, multiple levels of menus may be described.
219 A single vector slot containing nil indicates the start of a submenu.
220 A single vector slot containing lambda indicates the end of a submenu.
221 The submenu follows a menu item which is the way to reach the submenu.
223 A single vector slot containing quote indicates that the
224 following items should appear on the right of a dialog box.
226 Using a Lisp vector to hold this information while we decode it
227 takes care of protecting all the data from GC. */
229 #define MENU_ITEMS_PANE_NAME 1
230 #define MENU_ITEMS_PANE_PREFIX 2
231 #define MENU_ITEMS_PANE_LENGTH 3
235 MENU_ITEMS_ITEM_NAME
= 0,
236 MENU_ITEMS_ITEM_ENABLE
,
237 MENU_ITEMS_ITEM_VALUE
,
238 MENU_ITEMS_ITEM_EQUIV_KEY
,
239 MENU_ITEMS_ITEM_DEFINITION
,
240 MENU_ITEMS_ITEM_TYPE
,
241 MENU_ITEMS_ITEM_SELECTED
,
242 MENU_ITEMS_ITEM_HELP
,
243 MENU_ITEMS_ITEM_LENGTH
246 static Lisp_Object menu_items
;
248 /* Number of slots currently allocated in menu_items. */
249 static int menu_items_allocated
;
251 /* This is the index in menu_items of the first empty slot. */
252 static int menu_items_used
;
254 /* The number of panes currently recorded in menu_items,
255 excluding those within submenus. */
256 static int menu_items_n_panes
;
258 /* Current depth within submenus. */
259 static int menu_items_submenu_depth
;
261 /* This is set nonzero after the user activates the menu bar, and set
262 to zero again after the menu bars are redisplayed by prepare_menu_bar.
263 While it is nonzero, all calls to set_frame_menubar go deep.
265 I don't understand why this is needed, but it does seem to be
266 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
268 int pending_menu_activation
;
270 /* Initialize the menu_items structure if we haven't already done so.
271 Also mark it as currently empty. */
276 if (NILP (menu_items
))
278 menu_items_allocated
= 60;
279 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
283 menu_items_n_panes
= 0;
284 menu_items_submenu_depth
= 0;
287 /* Call at the end of generating the data in menu_items. */
294 /* Call when finished using the data for the current menu
298 discard_menu_items ()
300 /* Free the structure if it is especially large.
301 Otherwise, hold on to it, to save time. */
302 if (menu_items_allocated
> 200)
305 menu_items_allocated
= 0;
309 /* This undoes save_menu_items, and it is called by the specpdl unwind
313 restore_menu_items (saved
)
316 menu_items
= XCAR (saved
);
317 menu_items_allocated
= (VECTORP (menu_items
) ? ASIZE (menu_items
) : 0);
318 saved
= XCDR (saved
);
319 menu_items_used
= XINT (XCAR (saved
));
320 saved
= XCDR (saved
);
321 menu_items_n_panes
= XINT (XCAR (saved
));
322 saved
= XCDR (saved
);
323 menu_items_submenu_depth
= XINT (XCAR (saved
));
327 /* Push the whole state of menu_items processing onto the specpdl.
328 It will be restored when the specpdl is unwound. */
333 Lisp_Object saved
= list4 (menu_items
,
334 make_number (menu_items_used
),
335 make_number (menu_items_n_panes
),
336 make_number (menu_items_submenu_depth
));
337 record_unwind_protect (restore_menu_items
, saved
);
341 /* Make the menu_items vector twice as large. */
347 int old_size
= menu_items_allocated
;
350 menu_items_allocated
*= 2;
352 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
353 bcopy (XVECTOR (old
)->contents
, XVECTOR (menu_items
)->contents
,
354 old_size
* sizeof (Lisp_Object
));
357 /* Begin a submenu. */
360 push_submenu_start ()
362 if (menu_items_used
+ 1 > menu_items_allocated
)
365 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qnil
;
366 menu_items_submenu_depth
++;
374 if (menu_items_used
+ 1 > menu_items_allocated
)
377 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qlambda
;
378 menu_items_submenu_depth
--;
381 /* Indicate boundary between left and right. */
384 push_left_right_boundary ()
386 if (menu_items_used
+ 1 > menu_items_allocated
)
389 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qquote
;
392 /* Start a new menu pane in menu_items.
393 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
396 push_menu_pane (name
, prefix_vec
)
397 Lisp_Object name
, prefix_vec
;
399 if (menu_items_used
+ MENU_ITEMS_PANE_LENGTH
> menu_items_allocated
)
402 if (menu_items_submenu_depth
== 0)
403 menu_items_n_panes
++;
404 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qt
;
405 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
406 XVECTOR (menu_items
)->contents
[menu_items_used
++] = prefix_vec
;
409 /* Push one menu item into the current pane. NAME is the string to
410 display. ENABLE if non-nil means this item can be selected. KEY
411 is the key generated by choosing this item, or nil if this item
412 doesn't really have a definition. DEF is the definition of this
413 item. EQUIV is the textual description of the keyboard equivalent
414 for this item (or nil if none). TYPE is the type of this menu
415 item, one of nil, `toggle' or `radio'. */
418 push_menu_item (name
, enable
, key
, def
, equiv
, type
, selected
, help
)
419 Lisp_Object name
, enable
, key
, def
, equiv
, type
, selected
, help
;
421 if (menu_items_used
+ MENU_ITEMS_ITEM_LENGTH
> menu_items_allocated
)
424 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
425 XVECTOR (menu_items
)->contents
[menu_items_used
++] = enable
;
426 XVECTOR (menu_items
)->contents
[menu_items_used
++] = key
;
427 XVECTOR (menu_items
)->contents
[menu_items_used
++] = equiv
;
428 XVECTOR (menu_items
)->contents
[menu_items_used
++] = def
;
429 XVECTOR (menu_items
)->contents
[menu_items_used
++] = type
;
430 XVECTOR (menu_items
)->contents
[menu_items_used
++] = selected
;
431 XVECTOR (menu_items
)->contents
[menu_items_used
++] = help
;
434 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
435 and generate menu panes for them in menu_items.
436 If NOTREAL is nonzero,
437 don't bother really computing whether an item is enabled. */
440 keymap_panes (keymaps
, nmaps
, notreal
)
441 Lisp_Object
*keymaps
;
449 /* Loop over the given keymaps, making a pane for each map.
450 But don't make a pane that is empty--ignore that map instead.
451 P is the number of panes we have made so far. */
452 for (mapno
= 0; mapno
< nmaps
; mapno
++)
453 single_keymap_panes (keymaps
[mapno
],
454 Fkeymap_prompt (keymaps
[mapno
]), Qnil
, notreal
, 10);
456 finish_menu_items ();
459 /* Args passed between single_keymap_panes and single_menu_item. */
462 Lisp_Object pending_maps
;
463 int maxdepth
, notreal
;
466 static void single_menu_item
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
,
469 /* This is a recursive subroutine of keymap_panes.
470 It handles one keymap, KEYMAP.
471 The other arguments are passed along
472 or point to local variables of the previous function.
473 If NOTREAL is nonzero, only check for equivalent key bindings, don't
474 evaluate expressions in menu items and don't make any menu.
476 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
479 single_keymap_panes (keymap
, pane_name
, prefix
, notreal
, maxdepth
)
481 Lisp_Object pane_name
;
489 skp
.pending_maps
= Qnil
;
490 skp
.maxdepth
= maxdepth
;
491 skp
.notreal
= notreal
;
496 push_menu_pane (pane_name
, prefix
);
498 GCPRO1 (skp
.pending_maps
);
499 map_keymap (keymap
, single_menu_item
, Qnil
, &skp
, 1);
502 /* Process now any submenus which want to be panes at this level. */
503 while (CONSP (skp
.pending_maps
))
505 Lisp_Object elt
, eltcdr
, string
;
506 elt
= XCAR (skp
.pending_maps
);
508 string
= XCAR (eltcdr
);
509 /* We no longer discard the @ from the beginning of the string here.
510 Instead, we do this in mac_menu_show. */
511 single_keymap_panes (Fcar (elt
), string
,
512 XCDR (eltcdr
), notreal
, maxdepth
- 1);
513 skp
.pending_maps
= XCDR (skp
.pending_maps
);
517 /* This is a subroutine of single_keymap_panes that handles one
519 KEY is a key in a keymap and ITEM is its binding.
520 SKP->PENDING_MAPS_PTR is a list of keymaps waiting to be made into
522 If SKP->NOTREAL is nonzero, only check for equivalent key bindings, don't
523 evaluate expressions in menu items and don't make any menu.
524 If we encounter submenus deeper than SKP->MAXDEPTH levels, ignore them. */
527 single_menu_item (key
, item
, dummy
, skp_v
)
528 Lisp_Object key
, item
, dummy
;
531 Lisp_Object map
, item_string
, enabled
;
532 struct gcpro gcpro1
, gcpro2
;
534 struct skp
*skp
= skp_v
;
536 /* Parse the menu item and leave the result in item_properties. */
538 res
= parse_menu_item (item
, skp
->notreal
, 0);
541 return; /* Not a menu item. */
543 map
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_MAP
];
547 /* We don't want to make a menu, just traverse the keymaps to
548 precompute equivalent key bindings. */
550 single_keymap_panes (map
, Qnil
, key
, 1, skp
->maxdepth
- 1);
554 enabled
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_ENABLE
];
555 item_string
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_NAME
];
557 if (!NILP (map
) && SREF (item_string
, 0) == '@')
560 /* An enabled separate pane. Remember this to handle it later. */
561 skp
->pending_maps
= Fcons (Fcons (map
, Fcons (item_string
, key
)),
566 push_menu_item (item_string
, enabled
, key
,
567 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_DEF
],
568 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_KEYEQ
],
569 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_TYPE
],
570 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_SELECTED
],
571 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_HELP
]);
573 /* Display a submenu using the toolkit. */
574 if (! (NILP (map
) || NILP (enabled
)))
576 push_submenu_start ();
577 single_keymap_panes (map
, Qnil
, key
, 0, skp
->maxdepth
- 1);
582 /* Push all the panes and items of a menu described by the
583 alist-of-alists MENU.
584 This handles old-fashioned calls to x-popup-menu. */
594 for (tail
= menu
; CONSP (tail
); tail
= XCDR (tail
))
596 Lisp_Object elt
, pane_name
, pane_data
;
598 pane_name
= Fcar (elt
);
599 CHECK_STRING (pane_name
);
600 push_menu_pane (ENCODE_MENU_STRING (pane_name
), Qnil
);
601 pane_data
= Fcdr (elt
);
602 CHECK_CONS (pane_data
);
603 list_of_items (pane_data
);
606 finish_menu_items ();
609 /* Push the items in a single pane defined by the alist PANE. */
615 Lisp_Object tail
, item
, item1
;
617 for (tail
= pane
; CONSP (tail
); tail
= XCDR (tail
))
621 push_menu_item (ENCODE_MENU_STRING (item
), Qnil
, Qnil
, Qt
,
622 Qnil
, Qnil
, Qnil
, Qnil
);
623 else if (CONSP (item
))
626 CHECK_STRING (item1
);
627 push_menu_item (ENCODE_MENU_STRING (item1
), Qt
, XCDR (item
),
628 Qt
, Qnil
, Qnil
, Qnil
, Qnil
);
631 push_left_right_boundary ();
637 cleanup_popup_menu (arg
)
640 discard_menu_items ();
644 DEFUN ("x-popup-menu", Fx_popup_menu
, Sx_popup_menu
, 2, 2, 0,
645 doc
: /* Pop up a deck-of-cards menu and return user's selection.
646 POSITION is a position specification. This is either a mouse button event
647 or a list ((XOFFSET YOFFSET) WINDOW)
648 where XOFFSET and YOFFSET are positions in pixels from the top left
649 corner of WINDOW. (WINDOW may be a window or a frame object.)
650 This controls the position of the top left of the menu as a whole.
651 If POSITION is t, it means to use the current mouse position.
653 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.
654 The menu items come from key bindings that have a menu string as well as
655 a definition; actually, the "definition" in such a key binding looks like
656 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into
657 the keymap as a top-level element.
659 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.
660 Otherwise, REAL-DEFINITION should be a valid key binding definition.
662 You can also use a list of keymaps as MENU.
663 Then each keymap makes a separate pane.
665 When MENU is a keymap or a list of keymaps, the return value is the
666 list of events corresponding to the user's choice. Note that
667 `x-popup-menu' does not actually execute the command bound to that
670 Alternatively, you can specify a menu of multiple panes
671 with a list of the form (TITLE PANE1 PANE2...),
672 where each pane is a list of form (TITLE ITEM1 ITEM2...).
673 Each ITEM is normally a cons cell (STRING . VALUE);
674 but a string can appear as an item--that makes a nonselectable line
676 With this form of menu, the return value is VALUE from the chosen item.
678 If POSITION is nil, don't display the menu at all, just precalculate the
679 cached information about equivalent key sequences.
681 If the user gets rid of the menu without making a valid choice, for
682 instance by clicking the mouse away from a valid choice or by typing
683 keyboard input, then this normally results in a quit and
684 `x-popup-menu' does not return. But if POSITION is a mouse button
685 event (indicating that the user invoked the menu with the mouse) then
686 no quit occurs and `x-popup-menu' returns nil. */)
688 Lisp_Object position
, menu
;
690 Lisp_Object keymap
, tem
;
691 int xpos
= 0, ypos
= 0;
693 char *error_name
= NULL
;
694 Lisp_Object selection
;
696 Lisp_Object x
, y
, window
;
699 int specpdl_count
= SPECPDL_INDEX ();
703 if (! NILP (position
))
707 /* Decode the first argument: find the window and the coordinates. */
708 if (EQ (position
, Qt
)
709 || (CONSP (position
) && (EQ (XCAR (position
), Qmenu_bar
)
710 || EQ (XCAR (position
), Qtool_bar
)
711 || EQ (XCAR (position
), Qmac_apple_event
))))
713 /* Use the mouse's current position. */
714 FRAME_PTR new_f
= SELECTED_FRAME ();
715 Lisp_Object bar_window
;
716 enum scroll_bar_part part
;
719 if (mouse_position_hook
)
720 (*mouse_position_hook
) (&new_f
, 1, &bar_window
,
721 &part
, &x
, &y
, &time
);
723 XSETFRAME (window
, new_f
);
726 window
= selected_window
;
733 tem
= Fcar (position
);
736 window
= Fcar (Fcdr (position
));
738 y
= Fcar (XCDR (tem
));
743 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
744 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
745 tem
= Fcar (Fcdr (Fcdr (tem
))); /* POSN_WINDOW_POSN (tem) */
754 /* Decode where to put the menu. */
762 else if (WINDOWP (window
))
764 CHECK_LIVE_WINDOW (window
);
765 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
767 xpos
= WINDOW_LEFT_EDGE_X (XWINDOW (window
));
768 ypos
= WINDOW_TOP_EDGE_Y (XWINDOW (window
));
771 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
772 but I don't want to make one now. */
773 CHECK_WINDOW (window
);
778 XSETFRAME (Vmenu_updating_frame
, f
);
781 Vmenu_updating_frame
= Qnil
;
782 #endif /* HAVE_MENUS */
787 /* Decode the menu items from what was specified. */
789 keymap
= get_keymap (menu
, 0, 0);
792 /* We were given a keymap. Extract menu info from the keymap. */
795 /* Extract the detailed info to make one pane. */
796 keymap_panes (&menu
, 1, NILP (position
));
798 /* Search for a string appearing directly as an element of the keymap.
799 That string is the title of the menu. */
800 prompt
= Fkeymap_prompt (keymap
);
801 if (NILP (title
) && !NILP (prompt
))
804 /* Make that be the pane title of the first pane. */
805 if (!NILP (prompt
) && menu_items_n_panes
>= 0)
806 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = prompt
;
810 else if (CONSP (menu
) && KEYMAPP (XCAR (menu
)))
812 /* We were given a list of keymaps. */
813 int nmaps
= XFASTINT (Flength (menu
));
815 = (Lisp_Object
*) alloca (nmaps
* sizeof (Lisp_Object
));
820 /* The first keymap that has a prompt string
821 supplies the menu title. */
822 for (tem
= menu
, i
= 0; CONSP (tem
); tem
= XCDR (tem
))
826 maps
[i
++] = keymap
= get_keymap (XCAR (tem
), 1, 0);
828 prompt
= Fkeymap_prompt (keymap
);
829 if (NILP (title
) && !NILP (prompt
))
833 /* Extract the detailed info to make one pane. */
834 keymap_panes (maps
, nmaps
, NILP (position
));
836 /* Make the title be the pane title of the first pane. */
837 if (!NILP (title
) && menu_items_n_panes
>= 0)
838 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = title
;
844 /* We were given an old-fashioned menu. */
846 CHECK_STRING (title
);
848 list_of_panes (Fcdr (menu
));
855 discard_menu_items ();
861 /* Display them in a menu. */
862 record_unwind_protect (cleanup_popup_menu
, Qnil
);
865 selection
= mac_menu_show (f
, xpos
, ypos
, for_click
,
866 keymaps
, title
, &error_name
);
868 unbind_to (specpdl_count
, Qnil
);
871 #endif /* HAVE_MENUS */
873 if (error_name
) error (error_name
);
879 DEFUN ("x-popup-dialog", Fx_popup_dialog
, Sx_popup_dialog
, 2, 3, 0,
880 doc
: /* Pop up a dialog box and return user's selection.
881 POSITION specifies which frame to use.
882 This is normally a mouse button event or a window or frame.
883 If POSITION is t, it means to use the frame the mouse is on.
884 The dialog box appears in the middle of the specified frame.
886 CONTENTS specifies the alternatives to display in the dialog box.
887 It is a list of the form (DIALOG ITEM1 ITEM2...).
888 Each ITEM is a cons cell (STRING . VALUE).
889 The return value is VALUE from the chosen item.
891 An ITEM may also be just a string--that makes a nonselectable item.
892 An ITEM may also be nil--that means to put all preceding items
893 on the left of the dialog box and all following items on the right.
894 \(By default, approximately half appear on each side.)
896 If HEADER is non-nil, the frame title for the box is "Information",
897 otherwise it is "Question".
899 If the user gets rid of the dialog box without making a valid choice,
900 for instance using the window manager, then this produces a quit and
901 `x-popup-dialog' does not return. */)
902 (position
, contents
, header
)
903 Lisp_Object position
, contents
, header
;
910 /* Decode the first argument: find the window or frame to use. */
911 if (EQ (position
, Qt
)
912 || (CONSP (position
) && (EQ (XCAR (position
), Qmenu_bar
)
913 || EQ (XCAR (position
), Qtool_bar
)
914 || EQ (XCAR (position
), Qmac_apple_event
))))
916 #if 0 /* Using the frame the mouse is on may not be right. */
917 /* Use the mouse's current position. */
918 FRAME_PTR new_f
= SELECTED_FRAME ();
919 Lisp_Object bar_window
;
920 enum scroll_bar_part part
;
924 (*mouse_position_hook
) (&new_f
, 1, &bar_window
, &part
, &x
, &y
, &time
);
927 XSETFRAME (window
, new_f
);
929 window
= selected_window
;
931 window
= selected_window
;
933 else if (CONSP (position
))
936 tem
= Fcar (position
);
938 window
= Fcar (Fcdr (position
));
941 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
942 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
945 else if (WINDOWP (position
) || FRAMEP (position
))
950 /* Decode where to put the menu. */
954 else if (WINDOWP (window
))
956 CHECK_LIVE_WINDOW (window
);
957 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
960 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
961 but I don't want to make one now. */
962 CHECK_WINDOW (window
);
965 /* Display a menu with these alternatives
966 in the middle of frame F. */
968 Lisp_Object x
, y
, frame
, newpos
;
969 XSETFRAME (frame
, f
);
970 XSETINT (x
, x_pixel_width (f
) / 2);
971 XSETINT (y
, x_pixel_height (f
) / 2);
972 newpos
= Fcons (Fcons (x
, Fcons (y
, Qnil
)), Fcons (frame
, Qnil
));
974 return Fx_popup_menu (newpos
,
975 Fcons (Fcar (contents
), Fcons (contents
, Qnil
)));
977 #else /* HAVE_DIALOGS */
981 Lisp_Object selection
;
982 int specpdl_count
= SPECPDL_INDEX ();
984 /* Decode the dialog items from what was specified. */
985 title
= Fcar (contents
);
986 CHECK_STRING (title
);
988 list_of_panes (Fcons (contents
, Qnil
));
990 /* Display them in a dialog box. */
991 record_unwind_protect (cleanup_popup_menu
, Qnil
);
993 selection
= mac_dialog_show (f
, 0, title
, header
, &error_name
);
995 unbind_to (specpdl_count
, Qnil
);
997 if (error_name
) error (error_name
);
1000 #endif /* HAVE_DIALOGS */
1003 /* Activate the menu bar of frame F.
1004 This is called from keyboard.c when it gets the
1005 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
1007 To activate the menu bar, we use the button-press event location
1008 that was saved in saved_menu_event_location.
1010 But first we recompute the menu bar contents (the whole tree).
1012 The reason for saving the button event until here, instead of
1013 passing it to the toolkit right away, is that we can safely
1014 execute Lisp code. */
1017 x_activate_menubar (f
)
1021 SInt16 menu_id
, menu_item
;
1022 extern Point saved_menu_event_location
;
1024 set_frame_menubar (f
, 0, 1);
1027 menu_choice
= MenuSelect (saved_menu_event_location
);
1028 menu_id
= HiWord (menu_choice
);
1029 menu_item
= LoWord (menu_choice
);
1031 #if !TARGET_API_MAC_CARBON
1032 if (menu_id
== min_menu_id
[MAC_MENU_M_APPLE
])
1033 do_apple_menu (menu_item
);
1038 MenuHandle menu
= GetMenuHandle (menu_id
);
1044 GetMenuItemRefCon (menu
, menu_item
, &refcon
);
1045 find_and_call_menu_selection (f
, f
->menu_bar_items_used
,
1046 f
->menu_bar_vector
, (void *) refcon
);
1055 /* Find the menu selection and store it in the keyboard buffer.
1056 F is the frame the menu is on.
1057 MENU_BAR_ITEMS_USED is the length of VECTOR.
1058 VECTOR is an array of menu events for the whole menu. */
1061 find_and_call_menu_selection (f
, menu_bar_items_used
, vector
, client_data
)
1063 int menu_bar_items_used
;
1067 Lisp_Object prefix
, entry
;
1068 Lisp_Object
*subprefix_stack
;
1069 int submenu_depth
= 0;
1073 subprefix_stack
= (Lisp_Object
*) alloca (menu_bar_items_used
* sizeof (Lisp_Object
));
1077 while (i
< menu_bar_items_used
)
1079 if (EQ (XVECTOR (vector
)->contents
[i
], Qnil
))
1081 subprefix_stack
[submenu_depth
++] = prefix
;
1085 else if (EQ (XVECTOR (vector
)->contents
[i
], Qlambda
))
1087 prefix
= subprefix_stack
[--submenu_depth
];
1090 else if (EQ (XVECTOR (vector
)->contents
[i
], Qt
))
1092 prefix
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1093 i
+= MENU_ITEMS_PANE_LENGTH
;
1097 entry
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1098 /* The EMACS_INT cast avoids a warning. There's no problem
1099 as long as pointers have enough bits to hold small integers. */
1100 if ((int) (EMACS_INT
) client_data
== i
)
1103 struct input_event buf
;
1107 XSETFRAME (frame
, f
);
1108 buf
.kind
= MENU_BAR_EVENT
;
1109 buf
.frame_or_window
= frame
;
1111 kbd_buffer_store_event (&buf
);
1113 for (j
= 0; j
< submenu_depth
; j
++)
1114 if (!NILP (subprefix_stack
[j
]))
1116 buf
.kind
= MENU_BAR_EVENT
;
1117 buf
.frame_or_window
= frame
;
1118 buf
.arg
= subprefix_stack
[j
];
1119 kbd_buffer_store_event (&buf
);
1124 buf
.kind
= MENU_BAR_EVENT
;
1125 buf
.frame_or_window
= frame
;
1127 kbd_buffer_store_event (&buf
);
1130 buf
.kind
= MENU_BAR_EVENT
;
1131 buf
.frame_or_window
= frame
;
1133 kbd_buffer_store_event (&buf
);
1137 i
+= MENU_ITEMS_ITEM_LENGTH
;
1142 /* Allocate a widget_value, blocking input. */
1145 xmalloc_widget_value ()
1147 widget_value
*value
;
1150 value
= malloc_widget_value ();
1156 /* This recursively calls free_widget_value on the tree of widgets.
1157 It must free all data that was malloc'ed for these widget_values.
1158 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1159 must be left alone. */
1162 free_menubar_widget_value_tree (wv
)
1167 wv
->name
= wv
->value
= wv
->key
= (char *) 0xDEADBEEF;
1169 if (wv
->contents
&& (wv
->contents
!= (widget_value
*)1))
1171 free_menubar_widget_value_tree (wv
->contents
);
1172 wv
->contents
= (widget_value
*) 0xDEADBEEF;
1176 free_menubar_widget_value_tree (wv
->next
);
1177 wv
->next
= (widget_value
*) 0xDEADBEEF;
1180 free_widget_value (wv
);
1184 /* Set up data in menu_items for a menu bar item
1185 whose event type is ITEM_KEY (with string ITEM_NAME)
1186 and whose contents come from the list of keymaps MAPS. */
1189 parse_single_submenu (item_key
, item_name
, maps
)
1190 Lisp_Object item_key
, item_name
, maps
;
1194 Lisp_Object
*mapvec
;
1196 int top_level_items
= 0;
1198 length
= Flength (maps
);
1199 len
= XINT (length
);
1201 /* Convert the list MAPS into a vector MAPVEC. */
1202 mapvec
= (Lisp_Object
*) alloca (len
* sizeof (Lisp_Object
));
1203 for (i
= 0; i
< len
; i
++)
1205 mapvec
[i
] = Fcar (maps
);
1209 /* Loop over the given keymaps, making a pane for each map.
1210 But don't make a pane that is empty--ignore that map instead. */
1211 for (i
= 0; i
< len
; i
++)
1213 if (!KEYMAPP (mapvec
[i
]))
1215 /* Here we have a command at top level in the menu bar
1216 as opposed to a submenu. */
1217 top_level_items
= 1;
1218 push_menu_pane (Qnil
, Qnil
);
1219 push_menu_item (item_name
, Qt
, item_key
, mapvec
[i
],
1220 Qnil
, Qnil
, Qnil
, Qnil
);
1225 prompt
= Fkeymap_prompt (mapvec
[i
]);
1226 single_keymap_panes (mapvec
[i
],
1227 !NILP (prompt
) ? prompt
: item_name
,
1232 return top_level_items
;
1235 /* Create a tree of widget_value objects
1236 representing the panes and items
1237 in menu_items starting at index START, up to index END. */
1239 static widget_value
*
1240 digest_single_submenu (start
, end
, top_level_items
)
1241 int start
, end
, top_level_items
;
1243 widget_value
*wv
, *prev_wv
, *save_wv
, *first_wv
;
1245 int submenu_depth
= 0;
1246 widget_value
**submenu_stack
;
1250 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1251 wv
= xmalloc_widget_value ();
1255 wv
->button_type
= BUTTON_TYPE_NONE
;
1261 /* Loop over all panes and items made by the preceding call
1262 to parse_single_submenu and construct a tree of widget_value objects.
1263 Ignore the panes and items used by previous calls to
1264 digest_single_submenu, even though those are also in menu_items. */
1268 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1270 submenu_stack
[submenu_depth
++] = save_wv
;
1275 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1278 save_wv
= submenu_stack
[--submenu_depth
];
1281 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1282 && submenu_depth
!= 0)
1283 i
+= MENU_ITEMS_PANE_LENGTH
;
1284 /* Ignore a nil in the item list.
1285 It's meaningful only for dialog boxes. */
1286 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1288 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1290 /* Create a new pane. */
1291 Lisp_Object pane_name
, prefix
;
1296 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1297 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1299 #ifndef HAVE_MULTILINGUAL_MENU
1300 if (STRINGP (pane_name
) && STRING_MULTIBYTE (pane_name
))
1302 pane_name
= ENCODE_MENU_STRING (pane_name
);
1303 AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
) = pane_name
;
1306 pane_string
= (NILP (pane_name
)
1307 ? "" : (char *) SDATA (pane_name
));
1308 /* If there is just one top-level pane, put all its items directly
1309 under the top-level menu. */
1310 if (menu_items_n_panes
== 1)
1313 /* If the pane has a meaningful name,
1314 make the pane a top-level menu item
1315 with its items as a submenu beneath it. */
1316 if (strcmp (pane_string
, ""))
1318 wv
= xmalloc_widget_value ();
1322 first_wv
->contents
= wv
;
1323 wv
->lname
= pane_name
;
1324 /* Set value to 1 so update_submenu_strings can handle '@' */
1325 wv
->value
= (char *)1;
1327 wv
->button_type
= BUTTON_TYPE_NONE
;
1335 i
+= MENU_ITEMS_PANE_LENGTH
;
1339 /* Create a new item within current pane. */
1340 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
;
1343 /* All items should be contained in panes. */
1344 if (panes_seen
== 0)
1347 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
1348 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
1349 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
1350 def
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_DEFINITION
);
1351 type
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_TYPE
);
1352 selected
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_SELECTED
);
1353 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
1355 #ifndef HAVE_MULTILINGUAL_MENU
1356 if (STRING_MULTIBYTE (item_name
))
1358 item_name
= ENCODE_MENU_STRING (item_name
);
1359 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
) = item_name
;
1362 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
1364 descrip
= ENCODE_MENU_STRING (descrip
);
1365 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
) = descrip
;
1367 #endif /* not HAVE_MULTILINGUAL_MENU */
1369 wv
= xmalloc_widget_value ();
1373 save_wv
->contents
= wv
;
1375 wv
->lname
= item_name
;
1376 if (!NILP (descrip
))
1379 /* The EMACS_INT cast avoids a warning. There's no problem
1380 as long as pointers have enough bits to hold small integers. */
1381 wv
->call_data
= (!NILP (def
) ? (void *) (EMACS_INT
) i
: 0);
1382 wv
->enabled
= !NILP (enable
);
1385 wv
->button_type
= BUTTON_TYPE_NONE
;
1386 else if (EQ (type
, QCradio
))
1387 wv
->button_type
= BUTTON_TYPE_RADIO
;
1388 else if (EQ (type
, QCtoggle
))
1389 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
1393 wv
->selected
= !NILP (selected
);
1394 if (! STRINGP (help
))
1401 i
+= MENU_ITEMS_ITEM_LENGTH
;
1405 /* If we have just one "menu item"
1406 that was originally a button, return it by itself. */
1407 if (top_level_items
&& first_wv
->contents
&& first_wv
->contents
->next
== 0)
1409 wv
= first_wv
->contents
;
1410 free_widget_value (first_wv
);
1417 /* Walk through the widget_value tree starting at FIRST_WV and update
1418 the char * pointers from the corresponding lisp values.
1419 We do this after building the whole tree, since GC may happen while the
1420 tree is constructed, and small strings are relocated. So we must wait
1421 until no GC can happen before storing pointers into lisp values. */
1423 update_submenu_strings (first_wv
)
1424 widget_value
*first_wv
;
1428 for (wv
= first_wv
; wv
; wv
= wv
->next
)
1430 if (STRINGP (wv
->lname
))
1432 wv
->name
= SDATA (wv
->lname
);
1434 /* Ignore the @ that means "separate pane".
1435 This is a kludge, but this isn't worth more time. */
1436 if (wv
->value
== (char *)1)
1438 if (wv
->name
[0] == '@')
1444 if (STRINGP (wv
->lkey
))
1445 wv
->key
= SDATA (wv
->lkey
);
1448 update_submenu_strings (wv
->contents
);
1453 /* Event handler function that pops down a menu on C-g. We can only pop
1454 down menus if CancelMenuTracking is present (OSX 10.3 or later). */
1456 #ifdef HAVE_CANCELMENUTRACKING
1457 static pascal OSStatus
1458 menu_quit_handler (nextHandler
, theEvent
, userData
)
1459 EventHandlerCallRef nextHandler
;
1465 UInt32 keyModifiers
;
1466 extern int mac_quit_char_modifiers
;
1467 extern int mac_quit_char_keycode
;
1469 err
= GetEventParameter (theEvent
, kEventParamKeyCode
,
1470 typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
1473 err
= GetEventParameter (theEvent
, kEventParamKeyModifiers
,
1474 typeUInt32
, NULL
, sizeof(UInt32
),
1475 NULL
, &keyModifiers
);
1477 if (err
== noErr
&& keyCode
== mac_quit_char_keycode
1478 && keyModifiers
== mac_quit_char_modifiers
)
1480 MenuRef menu
= userData
!= 0
1481 ? (MenuRef
)userData
: AcquireRootMenu ();
1483 CancelMenuTracking (menu
, true, 0);
1484 if (!userData
) ReleaseMenu (menu
);
1488 return CallNextEventHandler (nextHandler
, theEvent
);
1490 #endif /* HAVE_CANCELMENUTRACKING */
1492 /* Add event handler to all menus that belong to KIND so we can detect C-g.
1493 MENU_HANDLE is the root menu of the tracking session to dismiss
1494 when C-g is detected. NULL means the menu bar.
1495 If CancelMenuTracking isn't available, do nothing. */
1498 install_menu_quit_handler (kind
, menu_handle
)
1499 enum mac_menu_kind kind
;
1500 MenuHandle menu_handle
;
1502 #ifdef HAVE_CANCELMENUTRACKING
1503 static const EventTypeSpec typesList
[] =
1504 {{kEventClassKeyboard
, kEventRawKeyDown
}};
1507 for (id
= min_menu_id
[kind
]; id
< min_menu_id
[kind
+ 1]; id
++)
1509 MenuHandle menu
= GetMenuHandle (id
);
1513 InstallMenuEventHandler (menu
, menu_quit_handler
,
1514 GetEventTypeCount (typesList
),
1515 typesList
, menu_handle
, NULL
);
1517 #endif /* HAVE_CANCELMENUTRACKING */
1520 /* Set the contents of the menubar widgets of frame F.
1521 The argument FIRST_TIME is currently ignored;
1522 it is set the first time this is called, from initialize_frame_menubar. */
1525 set_frame_menubar (f
, first_time
, deep_p
)
1530 int menubar_widget
= f
->output_data
.mac
->menubar_widget
;
1532 widget_value
*wv
, *first_wv
, *prev_wv
= 0;
1534 int *submenu_start
, *submenu_end
;
1535 int *submenu_top_level_items
, *submenu_n_panes
;
1537 XSETFRAME (Vmenu_updating_frame
, f
);
1539 if (! menubar_widget
)
1541 else if (pending_menu_activation
&& !deep_p
)
1546 /* Make a widget-value tree representing the entire menu trees. */
1548 struct buffer
*prev
= current_buffer
;
1550 int specpdl_count
= SPECPDL_INDEX ();
1551 int previous_menu_items_used
= f
->menu_bar_items_used
;
1552 Lisp_Object
*previous_items
1553 = (Lisp_Object
*) alloca (previous_menu_items_used
1554 * sizeof (Lisp_Object
));
1556 /* If we are making a new widget, its contents are empty,
1557 do always reinitialize them. */
1558 if (! menubar_widget
)
1559 previous_menu_items_used
= 0;
1561 buffer
= XWINDOW (FRAME_SELECTED_WINDOW (f
))->buffer
;
1562 specbind (Qinhibit_quit
, Qt
);
1563 /* Don't let the debugger step into this code
1564 because it is not reentrant. */
1565 specbind (Qdebug_on_next_call
, Qnil
);
1567 record_unwind_save_match_data ();
1568 if (NILP (Voverriding_local_map_menu_flag
))
1570 specbind (Qoverriding_terminal_local_map
, Qnil
);
1571 specbind (Qoverriding_local_map
, Qnil
);
1574 set_buffer_internal_1 (XBUFFER (buffer
));
1576 /* Run the Lucid hook. */
1577 safe_run_hooks (Qactivate_menubar_hook
);
1579 /* If it has changed current-menubar from previous value,
1580 really recompute the menubar from the value. */
1581 if (! NILP (Vlucid_menu_bar_dirty_flag
))
1582 call0 (Qrecompute_lucid_menubar
);
1583 safe_run_hooks (Qmenu_bar_update_hook
);
1584 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
1586 items
= FRAME_MENU_BAR_ITEMS (f
);
1588 /* Save the frame's previous menu bar contents data. */
1589 if (previous_menu_items_used
)
1590 bcopy (XVECTOR (f
->menu_bar_vector
)->contents
, previous_items
,
1591 previous_menu_items_used
* sizeof (Lisp_Object
));
1593 /* Fill in menu_items with the current menu bar contents.
1594 This can evaluate Lisp code. */
1597 menu_items
= f
->menu_bar_vector
;
1598 menu_items_allocated
= VECTORP (menu_items
) ? ASIZE (menu_items
) : 0;
1599 submenu_start
= (int *) alloca (XVECTOR (items
)->size
* sizeof (int *));
1600 submenu_end
= (int *) alloca (XVECTOR (items
)->size
* sizeof (int *));
1601 submenu_n_panes
= (int *) alloca (XVECTOR (items
)->size
* sizeof (int));
1602 submenu_top_level_items
1603 = (int *) alloca (XVECTOR (items
)->size
* sizeof (int *));
1605 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
1607 Lisp_Object key
, string
, maps
;
1611 key
= XVECTOR (items
)->contents
[i
];
1612 string
= XVECTOR (items
)->contents
[i
+ 1];
1613 maps
= XVECTOR (items
)->contents
[i
+ 2];
1617 submenu_start
[i
] = menu_items_used
;
1619 menu_items_n_panes
= 0;
1620 submenu_top_level_items
[i
]
1621 = parse_single_submenu (key
, string
, maps
);
1622 submenu_n_panes
[i
] = menu_items_n_panes
;
1624 submenu_end
[i
] = menu_items_used
;
1627 finish_menu_items ();
1629 /* Convert menu_items into widget_value trees
1630 to display the menu. This cannot evaluate Lisp code. */
1632 wv
= xmalloc_widget_value ();
1633 wv
->name
= "menubar";
1636 wv
->button_type
= BUTTON_TYPE_NONE
;
1640 for (i
= 0; i
< last_i
; i
+= 4)
1642 menu_items_n_panes
= submenu_n_panes
[i
];
1643 wv
= digest_single_submenu (submenu_start
[i
], submenu_end
[i
],
1644 submenu_top_level_items
[i
]);
1648 first_wv
->contents
= wv
;
1649 /* Don't set wv->name here; GC during the loop might relocate it. */
1651 wv
->button_type
= BUTTON_TYPE_NONE
;
1655 set_buffer_internal_1 (prev
);
1657 /* If there has been no change in the Lisp-level contents
1658 of the menu bar, skip redisplaying it. Just exit. */
1660 /* Compare the new menu items with the ones computed last time. */
1661 for (i
= 0; i
< previous_menu_items_used
; i
++)
1662 if (menu_items_used
== i
1663 || (!EQ (previous_items
[i
], XVECTOR (menu_items
)->contents
[i
])))
1665 if (i
== menu_items_used
&& i
== previous_menu_items_used
&& i
!= 0)
1667 /* The menu items have not changed. Don't bother updating
1668 the menus in any form, since it would be a no-op. */
1669 free_menubar_widget_value_tree (first_wv
);
1670 discard_menu_items ();
1671 unbind_to (specpdl_count
, Qnil
);
1675 /* The menu items are different, so store them in the frame. */
1676 f
->menu_bar_vector
= menu_items
;
1677 f
->menu_bar_items_used
= menu_items_used
;
1679 /* This calls restore_menu_items to restore menu_items, etc.,
1680 as they were outside. */
1681 unbind_to (specpdl_count
, Qnil
);
1683 /* Now GC cannot happen during the lifetime of the widget_value,
1684 so it's safe to store data from a Lisp_String. */
1685 wv
= first_wv
->contents
;
1686 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
1689 string
= XVECTOR (items
)->contents
[i
+ 1];
1692 wv
->name
= (char *) SDATA (string
);
1693 update_submenu_strings (wv
->contents
);
1700 /* Make a widget-value tree containing
1701 just the top level menu bar strings. */
1703 wv
= xmalloc_widget_value ();
1704 wv
->name
= "menubar";
1707 wv
->button_type
= BUTTON_TYPE_NONE
;
1711 items
= FRAME_MENU_BAR_ITEMS (f
);
1712 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
1716 string
= XVECTOR (items
)->contents
[i
+ 1];
1720 wv
= xmalloc_widget_value ();
1721 wv
->name
= (char *) SDATA (string
);
1724 wv
->button_type
= BUTTON_TYPE_NONE
;
1726 /* This prevents lwlib from assuming this
1727 menu item is really supposed to be empty. */
1728 /* The EMACS_INT cast avoids a warning.
1729 This value just has to be different from small integers. */
1730 wv
->call_data
= (void *) (EMACS_INT
) (-1);
1735 first_wv
->contents
= wv
;
1739 /* Forget what we thought we knew about what is in the
1740 detailed contents of the menu bar menus.
1741 Changing the top level always destroys the contents. */
1742 f
->menu_bar_items_used
= 0;
1745 /* Create or update the menu bar widget. */
1749 /* Non-null value to indicate menubar has already been "created". */
1750 f
->output_data
.mac
->menubar_widget
= 1;
1752 fill_menubar (first_wv
->contents
, deep_p
);
1754 /* Add event handler so we can detect C-g. */
1755 install_menu_quit_handler (MAC_MENU_MENU_BAR
, NULL
);
1756 install_menu_quit_handler (MAC_MENU_MENU_BAR_SUB
, NULL
);
1757 free_menubar_widget_value_tree (first_wv
);
1762 /* Get rid of the menu bar of frame F, and free its storage.
1763 This is used when deleting a frame, and when turning off the menu bar. */
1766 free_frame_menubar (f
)
1769 f
->output_data
.mac
->menubar_widget
= 0;
1777 struct Lisp_Save_Value
*p
= XSAVE_VALUE (arg
);
1778 FRAME_PTR f
= p
->pointer
;
1779 MenuHandle menu
= GetMenuHandle (min_menu_id
[MAC_MENU_POPUP
]);
1783 /* Must reset this manually because the button release event is not
1784 passed to Emacs event loop. */
1785 FRAME_MAC_DISPLAY_INFO (f
)->grabbed
= 0;
1787 /* delete all menus */
1788 dispose_menus (MAC_MENU_POPUP_SUB
, 0);
1789 DeleteMenu (min_menu_id
[MAC_MENU_POPUP
]);
1797 /* Mac_menu_show actually displays a menu using the panes and items in
1798 menu_items and returns the value selected from it; we assume input
1799 is blocked by the caller. */
1801 /* F is the frame the menu is for.
1802 X and Y are the frame-relative specified position,
1803 relative to the inside upper left corner of the frame F.
1804 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
1805 KEYMAPS is 1 if this menu was specified with keymaps;
1806 in that case, we return a list containing the chosen item's value
1807 and perhaps also the pane's prefix.
1808 TITLE is the specified menu title.
1809 ERROR is a place to store an error message string in case of failure.
1810 (We return nil on failure, but the value doesn't actually matter.) */
1813 mac_menu_show (f
, x
, y
, for_click
, keymaps
, title
, error
)
1824 int menu_item_choice
;
1825 int menu_item_selection
;
1828 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1829 widget_value
**submenu_stack
1830 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1831 Lisp_Object
*subprefix_stack
1832 = (Lisp_Object
*) alloca (menu_items_used
* sizeof (Lisp_Object
));
1833 int submenu_depth
= 0;
1836 int specpdl_count
= SPECPDL_INDEX ();
1840 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
1842 *error
= "Empty menu";
1846 /* Create a tree of widget_value objects
1847 representing the panes and their items. */
1848 wv
= xmalloc_widget_value ();
1852 wv
->button_type
= BUTTON_TYPE_NONE
;
1857 /* Loop over all panes and items, filling in the tree. */
1859 while (i
< menu_items_used
)
1861 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1863 submenu_stack
[submenu_depth
++] = save_wv
;
1869 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1872 save_wv
= submenu_stack
[--submenu_depth
];
1876 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1877 && submenu_depth
!= 0)
1878 i
+= MENU_ITEMS_PANE_LENGTH
;
1879 /* Ignore a nil in the item list.
1880 It's meaningful only for dialog boxes. */
1881 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1883 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1885 /* Create a new pane. */
1886 Lisp_Object pane_name
, prefix
;
1889 pane_name
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
);
1890 prefix
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
1892 #ifndef HAVE_MULTILINGUAL_MENU
1893 if (STRINGP (pane_name
) && STRING_MULTIBYTE (pane_name
))
1895 pane_name
= ENCODE_MENU_STRING (pane_name
);
1896 AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
) = pane_name
;
1899 pane_string
= (NILP (pane_name
)
1900 ? "" : (char *) SDATA (pane_name
));
1901 /* If there is just one top-level pane, put all its items directly
1902 under the top-level menu. */
1903 if (menu_items_n_panes
== 1)
1906 /* If the pane has a meaningful name,
1907 make the pane a top-level menu item
1908 with its items as a submenu beneath it. */
1909 if (!keymaps
&& strcmp (pane_string
, ""))
1911 wv
= xmalloc_widget_value ();
1915 first_wv
->contents
= wv
;
1916 wv
->name
= pane_string
;
1917 if (keymaps
&& !NILP (prefix
))
1921 wv
->button_type
= BUTTON_TYPE_NONE
;
1926 else if (first_pane
)
1932 i
+= MENU_ITEMS_PANE_LENGTH
;
1936 /* Create a new item within current pane. */
1937 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
, help
;
1938 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
1939 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
1940 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
1941 def
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_DEFINITION
);
1942 type
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_TYPE
);
1943 selected
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_SELECTED
);
1944 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
1946 #ifndef HAVE_MULTILINGUAL_MENU
1947 if (STRINGP (item_name
) && STRING_MULTIBYTE (item_name
))
1949 item_name
= ENCODE_MENU_STRING (item_name
);
1950 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
) = item_name
;
1953 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
1955 descrip
= ENCODE_MENU_STRING (descrip
);
1956 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
) = descrip
;
1958 #endif /* not HAVE_MULTILINGUAL_MENU */
1960 wv
= xmalloc_widget_value ();
1964 save_wv
->contents
= wv
;
1965 wv
->name
= (char *) SDATA (item_name
);
1966 if (!NILP (descrip
))
1967 wv
->key
= (char *) SDATA (descrip
);
1969 /* Use the contents index as call_data, since we are
1970 restricted to 16-bits. */
1971 wv
->call_data
= !NILP (def
) ? (void *) (EMACS_INT
) i
: 0;
1972 wv
->enabled
= !NILP (enable
);
1975 wv
->button_type
= BUTTON_TYPE_NONE
;
1976 else if (EQ (type
, QCtoggle
))
1977 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
1978 else if (EQ (type
, QCradio
))
1979 wv
->button_type
= BUTTON_TYPE_RADIO
;
1983 wv
->selected
= !NILP (selected
);
1985 if (! STRINGP (help
))
1992 i
+= MENU_ITEMS_ITEM_LENGTH
;
1996 /* Deal with the title, if it is non-nil. */
1999 widget_value
*wv_title
= xmalloc_widget_value ();
2000 widget_value
*wv_sep
= xmalloc_widget_value ();
2002 /* Maybe replace this separator with a bitmap or owner-draw item
2003 so that it looks better. Having two separators looks odd. */
2004 wv_sep
->name
= "--";
2005 wv_sep
->next
= first_wv
->contents
;
2006 wv_sep
->help
= Qnil
;
2008 #ifndef HAVE_MULTILINGUAL_MENU
2009 if (STRING_MULTIBYTE (title
))
2010 title
= ENCODE_MENU_STRING (title
);
2013 wv_title
->name
= (char *) SDATA (title
);
2014 wv_title
->enabled
= FALSE
;
2015 wv_title
->title
= TRUE
;
2016 wv_title
->button_type
= BUTTON_TYPE_NONE
;
2017 wv_title
->help
= Qnil
;
2018 wv_title
->next
= wv_sep
;
2019 first_wv
->contents
= wv_title
;
2022 /* Actually create the menu. */
2023 menu
= NewMenu (min_menu_id
[MAC_MENU_POPUP
], "\p");
2024 InsertMenu (menu
, -1);
2025 fill_menu (menu
, first_wv
->contents
, MAC_MENU_POPUP_SUB
,
2026 min_menu_id
[MAC_MENU_POPUP_SUB
]);
2028 /* Free the widget_value objects we used to specify the
2030 free_menubar_widget_value_tree (first_wv
);
2032 /* Adjust coordinates to be root-window-relative. */
2036 SetPortWindowPort (FRAME_MAC_WINDOW (f
));
2037 LocalToGlobal (&pos
);
2039 /* No selection has been chosen yet. */
2040 menu_item_choice
= 0;
2041 menu_item_selection
= 0;
2043 record_unwind_protect (pop_down_menu
, make_save_value (f
, 0));
2045 /* Add event handler so we can detect C-g. */
2046 install_menu_quit_handler (MAC_MENU_POPUP
, menu
);
2047 install_menu_quit_handler (MAC_MENU_POPUP_SUB
, menu
);
2049 /* Display the menu. */
2050 menu_item_choice
= PopUpMenuSelect (menu
, pos
.v
, pos
.h
, 0);
2051 menu_item_selection
= LoWord (menu_item_choice
);
2053 /* Get the refcon to find the correct item */
2054 if (menu_item_selection
)
2056 MenuHandle sel_menu
= GetMenuHandle (HiWord (menu_item_choice
));
2058 GetMenuItemRefCon (sel_menu
, menu_item_selection
, &refcon
);
2061 else if (! for_click
)
2062 /* Make "Cancel" equivalent to C-g unless this menu was popped up by
2064 Fsignal (Qquit
, Qnil
);
2066 /* Find the selected item, and its pane, to return
2067 the proper value. */
2068 if (menu_item_selection
!= 0)
2070 Lisp_Object prefix
, entry
;
2072 prefix
= entry
= Qnil
;
2074 while (i
< menu_items_used
)
2076 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
2078 subprefix_stack
[submenu_depth
++] = prefix
;
2082 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
2084 prefix
= subprefix_stack
[--submenu_depth
];
2087 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2090 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2091 i
+= MENU_ITEMS_PANE_LENGTH
;
2093 /* Ignore a nil in the item list.
2094 It's meaningful only for dialog boxes. */
2095 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2100 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2101 if ((int) (EMACS_INT
) refcon
== i
)
2107 entry
= Fcons (entry
, Qnil
);
2109 entry
= Fcons (prefix
, entry
);
2110 for (j
= submenu_depth
- 1; j
>= 0; j
--)
2111 if (!NILP (subprefix_stack
[j
]))
2112 entry
= Fcons (subprefix_stack
[j
], entry
);
2116 i
+= MENU_ITEMS_ITEM_LENGTH
;
2120 else if (!for_click
)
2121 /* Make "Cancel" equivalent to C-g. */
2122 Fsignal (Qquit
, Qnil
);
2124 unbind_to (specpdl_count
, Qnil
);
2131 /* Construct native Mac OS menubar based on widget_value tree. */
2134 mac_dialog (widget_value
*wv
)
2138 char **button_labels
;
2145 WindowPtr window_ptr
;
2148 EventRecord event_record
;
2150 int control_part_code
;
2153 dialog_name
= wv
->name
;
2154 nb_buttons
= dialog_name
[1] - '0';
2155 left_count
= nb_buttons
- (dialog_name
[4] - '0');
2156 button_labels
= (char **) alloca (sizeof (char *) * nb_buttons
);
2157 ref_cons
= (UInt32
*) alloca (sizeof (UInt32
) * nb_buttons
);
2160 prompt
= (char *) alloca (strlen (wv
->value
) + 1);
2161 strcpy (prompt
, wv
->value
);
2165 for (i
= 0; i
< nb_buttons
; i
++)
2167 button_labels
[i
] = wv
->value
;
2168 button_labels
[i
] = (char *) alloca (strlen (wv
->value
) + 1);
2169 strcpy (button_labels
[i
], wv
->value
);
2170 c2pstr (button_labels
[i
]);
2171 ref_cons
[i
] = (UInt32
) wv
->call_data
;
2175 window_ptr
= GetNewCWindow (DIALOG_WINDOW_RESOURCE
, NULL
, (WindowPtr
) -1);
2177 SetPortWindowPort (window_ptr
);
2180 /* Left and right margins in the dialog are 13 pixels each.*/
2182 /* Calculate width of dialog box: 8 pixels on each side of the text
2183 label in each button, 12 pixels between buttons. */
2184 for (i
= 0; i
< nb_buttons
; i
++)
2185 dialog_width
+= StringWidth (button_labels
[i
]) + 16 + 12;
2187 if (left_count
!= 0 && nb_buttons
- left_count
!= 0)
2190 dialog_width
= max (dialog_width
, StringWidth (prompt
) + 26);
2192 SizeWindow (window_ptr
, dialog_width
, 78, 0);
2193 ShowWindow (window_ptr
);
2195 SetPortWindowPort (window_ptr
);
2200 DrawString (prompt
);
2203 for (i
= 0; i
< nb_buttons
; i
++)
2205 int button_width
= StringWidth (button_labels
[i
]) + 16;
2206 SetRect (&rect
, left
, 45, left
+ button_width
, 65);
2207 ch
= NewControl (window_ptr
, &rect
, button_labels
[i
], 1, 0, 0, 0,
2208 kControlPushButtonProc
, ref_cons
[i
]);
2209 left
+= button_width
+ 12;
2210 if (i
== left_count
- 1)
2217 if (WaitNextEvent (mDownMask
, &event_record
, 10, NULL
))
2218 if (event_record
.what
== mouseDown
)
2220 part_code
= FindWindow (event_record
.where
, &window_ptr
);
2221 if (part_code
== inContent
)
2223 mouse
= event_record
.where
;
2224 GlobalToLocal (&mouse
);
2225 control_part_code
= FindControl (mouse
, window_ptr
, &ch
);
2226 if (control_part_code
== kControlButtonPart
)
2227 if (TrackControl (ch
, mouse
, NULL
))
2228 i
= GetControlReference (ch
);
2233 DisposeWindow (window_ptr
);
2238 static char * button_names
[] = {
2239 "button1", "button2", "button3", "button4", "button5",
2240 "button6", "button7", "button8", "button9", "button10" };
2243 mac_dialog_show (f
, keymaps
, title
, header
, error_name
)
2246 Lisp_Object title
, header
;
2249 int i
, nb_buttons
=0;
2250 char dialog_name
[6];
2251 int menu_item_selection
;
2253 widget_value
*wv
, *first_wv
= 0, *prev_wv
= 0;
2255 /* Number of elements seen so far, before boundary. */
2257 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2258 int boundary_seen
= 0;
2262 if (menu_items_n_panes
> 1)
2264 *error_name
= "Multiple panes in dialog box";
2268 /* Create a tree of widget_value objects
2269 representing the text label and buttons. */
2271 Lisp_Object pane_name
, prefix
;
2273 pane_name
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
];
2274 prefix
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_PREFIX
];
2275 pane_string
= (NILP (pane_name
)
2276 ? "" : (char *) SDATA (pane_name
));
2277 prev_wv
= xmalloc_widget_value ();
2278 prev_wv
->value
= pane_string
;
2279 if (keymaps
&& !NILP (prefix
))
2281 prev_wv
->enabled
= 1;
2282 prev_wv
->name
= "message";
2283 prev_wv
->help
= Qnil
;
2286 /* Loop over all panes and items, filling in the tree. */
2287 i
= MENU_ITEMS_PANE_LENGTH
;
2288 while (i
< menu_items_used
)
2291 /* Create a new item within current pane. */
2292 Lisp_Object item_name
, enable
, descrip
;
2293 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
2294 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
2296 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
2298 if (NILP (item_name
))
2300 free_menubar_widget_value_tree (first_wv
);
2301 *error_name
= "Submenu in dialog items";
2304 if (EQ (item_name
, Qquote
))
2306 /* This is the boundary between left-side elts
2307 and right-side elts. Stop incrementing right_count. */
2312 if (nb_buttons
>= 9)
2314 free_menubar_widget_value_tree (first_wv
);
2315 *error_name
= "Too many dialog items";
2319 wv
= xmalloc_widget_value ();
2321 wv
->name
= (char *) button_names
[nb_buttons
];
2322 if (!NILP (descrip
))
2323 wv
->key
= (char *) SDATA (descrip
);
2324 wv
->value
= (char *) SDATA (item_name
);
2325 wv
->call_data
= (void *) i
;
2326 /* menu item is identified by its index in menu_items table */
2327 wv
->enabled
= !NILP (enable
);
2331 if (! boundary_seen
)
2335 i
+= MENU_ITEMS_ITEM_LENGTH
;
2338 /* If the boundary was not specified,
2339 by default put half on the left and half on the right. */
2340 if (! boundary_seen
)
2341 left_count
= nb_buttons
- nb_buttons
/ 2;
2343 wv
= xmalloc_widget_value ();
2344 wv
->name
= dialog_name
;
2347 /* Frame title: 'Q' = Question, 'I' = Information.
2348 Can also have 'E' = Error if, one day, we want
2349 a popup for errors. */
2351 dialog_name
[0] = 'Q';
2353 dialog_name
[0] = 'I';
2355 /* Dialog boxes use a really stupid name encoding
2356 which specifies how many buttons to use
2357 and how many buttons are on the right. */
2358 dialog_name
[1] = '0' + nb_buttons
;
2359 dialog_name
[2] = 'B';
2360 dialog_name
[3] = 'R';
2361 /* Number of buttons to put on the right. */
2362 dialog_name
[4] = '0' + nb_buttons
- left_count
;
2364 wv
->contents
= first_wv
;
2368 /* Actually create the dialog. */
2370 menu_item_selection
= mac_dialog (first_wv
);
2372 menu_item_selection
= 0;
2375 /* Free the widget_value objects we used to specify the contents. */
2376 free_menubar_widget_value_tree (first_wv
);
2378 /* Find the selected item, and its pane, to return
2379 the proper value. */
2380 if (menu_item_selection
!= 0)
2386 while (i
< menu_items_used
)
2390 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2393 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2394 i
+= MENU_ITEMS_PANE_LENGTH
;
2396 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2398 /* This is the boundary between left-side elts and
2405 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2406 if (menu_item_selection
== i
)
2410 entry
= Fcons (entry
, Qnil
);
2412 entry
= Fcons (prefix
, entry
);
2416 i
+= MENU_ITEMS_ITEM_LENGTH
;
2421 /* Make "Cancel" equivalent to C-g. */
2422 Fsignal (Qquit
, Qnil
);
2426 #endif /* HAVE_DIALOGS */
2429 /* Is this item a separator? */
2431 name_is_separator (name
)
2434 const char *start
= name
;
2436 /* Check if name string consists of only dashes ('-'). */
2437 while (*name
== '-') name
++;
2438 /* Separators can also be of the form "--:TripleSuperMegaEtched"
2439 or "--deep-shadow". We don't implement them yet, se we just treat
2440 them like normal separators. */
2441 return (*name
== '\0' || start
+ 2 == name
);
2445 add_menu_item (menu
, pos
, wv
)
2450 #if TARGET_API_MAC_CARBON
2451 CFStringRef item_name
;
2456 if (name_is_separator (wv
->name
))
2457 AppendMenu (menu
, "\p-");
2460 AppendMenu (menu
, "\pX");
2462 #if TARGET_API_MAC_CARBON
2463 item_name
= cfstring_create_with_utf8_cstring (wv
->name
);
2465 if (wv
->key
!= NULL
)
2467 CFStringRef name
, key
;
2470 key
= cfstring_create_with_utf8_cstring (wv
->key
);
2471 item_name
= CFStringCreateWithFormat (NULL
, NULL
, CFSTR ("%@ %@"),
2477 SetMenuItemTextWithCFString (menu
, pos
, item_name
);
2478 CFRelease (item_name
);
2481 EnableMenuItem (menu
, pos
);
2483 DisableMenuItem (menu
, pos
);
2484 #else /* ! TARGET_API_MAC_CARBON */
2485 item_name
[sizeof (item_name
) - 1] = '\0';
2486 strncpy (item_name
, wv
->name
, sizeof (item_name
) - 1);
2487 if (wv
->key
!= NULL
)
2489 int len
= strlen (item_name
);
2491 strncpy (item_name
+ len
, " ", sizeof (item_name
) - 1 - len
);
2492 len
= strlen (item_name
);
2493 strncpy (item_name
+ len
, wv
->key
, sizeof (item_name
) - 1 - len
);
2496 SetMenuItemText (menu
, pos
, item_name
);
2499 EnableItem (menu
, pos
);
2501 DisableItem (menu
, pos
);
2502 #endif /* ! TARGET_API_MAC_CARBON */
2504 /* Draw radio buttons and tickboxes. */
2505 if (wv
->selected
&& (wv
->button_type
== BUTTON_TYPE_TOGGLE
||
2506 wv
->button_type
== BUTTON_TYPE_RADIO
))
2507 SetItemMark (menu
, pos
, checkMark
);
2509 SetItemMark (menu
, pos
, noMark
);
2511 SetMenuItemRefCon (menu
, pos
, (UInt32
) wv
->call_data
);
2515 /* Construct native Mac OS menu based on widget_value tree. */
2518 fill_menu (menu
, wv
, kind
, submenu_id
)
2521 enum mac_menu_kind kind
;
2526 for (pos
= 1; wv
!= NULL
; wv
= wv
->next
, pos
++)
2528 add_menu_item (menu
, pos
, wv
);
2529 if (wv
->contents
&& submenu_id
< min_menu_id
[kind
+ 1])
2531 MenuHandle submenu
= NewMenu (submenu_id
, "\pX");
2533 InsertMenu (submenu
, -1);
2534 SetMenuItemHierarchicalID (menu
, pos
, submenu_id
);
2535 submenu_id
= fill_menu (submenu
, wv
->contents
, kind
, submenu_id
+ 1);
2542 /* Construct native Mac OS menubar based on widget_value tree. */
2545 fill_menubar (wv
, deep_p
)
2552 #if !TARGET_API_MAC_CARBON
2553 int title_changed_p
= 0;
2556 /* Clean up the menu bar when filled by the entire menu trees. */
2559 dispose_menus (MAC_MENU_MENU_BAR
, 0);
2560 dispose_menus (MAC_MENU_MENU_BAR_SUB
, 0);
2561 #if !TARGET_API_MAC_CARBON
2562 title_changed_p
= 1;
2566 /* Fill menu bar titles and submenus. Reuse the existing menu bar
2567 titles as much as possible to minimize redraw (if !deep_p). */
2568 submenu_id
= min_menu_id
[MAC_MENU_MENU_BAR_SUB
];
2569 for (id
= min_menu_id
[MAC_MENU_MENU_BAR
];
2570 wv
!= NULL
&& id
< min_menu_id
[MAC_MENU_MENU_BAR
+ 1];
2571 wv
= wv
->next
, id
++)
2573 strncpy (title
, wv
->name
, 255);
2577 menu
= GetMenuHandle (id
);
2580 #if TARGET_API_MAC_CARBON
2583 GetMenuTitle (menu
, old_title
);
2584 if (!EqualString (title
, old_title
, false, false))
2585 SetMenuTitle (menu
, title
);
2586 #else /* !TARGET_API_MAC_CARBON */
2587 if (!EqualString (title
, (*menu
)->menuData
, false, false))
2591 menu
= NewMenu (id
, title
);
2592 InsertMenu (menu
, GetMenuHandle (id
+ 1) ? id
+ 1 : 0);
2593 title_changed_p
= 1;
2595 #endif /* !TARGET_API_MAC_CARBON */
2599 menu
= NewMenu (id
, title
);
2600 InsertMenu (menu
, 0);
2601 #if !TARGET_API_MAC_CARBON
2602 title_changed_p
= 1;
2607 submenu_id
= fill_menu (menu
, wv
->contents
, MAC_MENU_MENU_BAR_SUB
,
2611 if (id
< min_menu_id
[MAC_MENU_MENU_BAR
+ 1] && GetMenuHandle (id
))
2613 dispose_menus (MAC_MENU_MENU_BAR
, id
);
2614 #if !TARGET_API_MAC_CARBON
2615 title_changed_p
= 1;
2619 #if !TARGET_API_MAC_CARBON
2620 if (title_changed_p
)
2625 /* Dispose of menus that belong to KIND, and remove them from the menu
2626 list. ID is the lower bound of menu IDs that will be processed. */
2629 dispose_menus (kind
, id
)
2630 enum mac_menu_kind kind
;
2633 for (id
= max (id
, min_menu_id
[kind
]); id
< min_menu_id
[kind
+ 1]; id
++)
2635 MenuHandle menu
= GetMenuHandle (id
);
2644 #endif /* HAVE_MENUS */
2646 /* The following is used by delayed window autoselection. */
2648 DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p
, Smenu_or_popup_active_p
, 0, 0, 0,
2649 doc
: /* Return t if a menu or popup dialog is active. */)
2652 /* Always return Qnil since menu selection functions do not return
2653 until a selection has been made or cancelled. */
2660 staticpro (&menu_items
);
2663 Qdebug_on_next_call
= intern ("debug-on-next-call");
2664 staticpro (&Qdebug_on_next_call
);
2666 defsubr (&Sx_popup_menu
);
2667 defsubr (&Smenu_or_popup_active_p
);
2669 defsubr (&Sx_popup_dialog
);
2673 /* arch-tag: 40b2c6c7-b8a9-4a49-b930-1b2707184cce
2674 (do not change this comment) */