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