Commit | Line | Data |
---|---|---|
dcfdbac7 | 1 | /* X Communication module for terminals which understand the X protocol. |
950eaee7 GM |
2 | Copyright (C) 1986, 88, 93, 94, 96, 99, 2000, 2001 |
3 | Free Software Foundation, Inc. | |
dcfdbac7 JB |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
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 | |
774910eb | 9 | the Free Software Foundation; either version 2, or (at your option) |
dcfdbac7 JB |
10 | any later version. |
11 | ||
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. | |
16 | ||
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 | |
3b7ad313 EN |
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
20 | Boston, MA 02111-1307, USA. */ | |
dcfdbac7 JB |
21 | |
22 | /* X pop-up deck-of-cards menu facility for gnuemacs. | |
23 | * | |
24 | * Written by Jon Arnold and Roman Budzianowski | |
25 | * Mods and rewrite by Robert Krawitz | |
26 | * | |
27 | */ | |
28 | ||
18686d47 RS |
29 | /* Modified by Fred Pierresteguy on December 93 |
30 | to make the popup menus and menubar use the Xt. */ | |
31 | ||
78589e07 RS |
32 | /* Rewritten for clarity and GC protection by rms in Feb 94. */ |
33 | ||
68c45bf0 PE |
34 | #include <config.h> |
35 | ||
dcfdbac7 JB |
36 | /* On 4.3 this loses if it comes after xterm.h. */ |
37 | #include <signal.h> | |
565620a5 RS |
38 | |
39 | #include <stdio.h> | |
dcfdbac7 | 40 | #include "lisp.h" |
18686d47 | 41 | #include "termhooks.h" |
02067692 | 42 | #include "keyboard.h" |
7708e9bd | 43 | #include "frame.h" |
dcfdbac7 | 44 | #include "window.h" |
9ac0d9e0 | 45 | #include "blockinput.h" |
88766961 | 46 | #include "buffer.h" |
dcfdbac7 | 47 | |
eeee3112 RS |
48 | #ifdef MSDOS |
49 | #include "msdos.h" | |
50 | #endif | |
51 | ||
87485d6f | 52 | #ifdef HAVE_X_WINDOWS |
dcfdbac7 JB |
53 | /* This may include sys/types.h, and that somehow loses |
54 | if this is not done before the other system files. */ | |
55 | #include "xterm.h" | |
87485d6f | 56 | #endif |
dcfdbac7 JB |
57 | |
58 | /* Load sys/types.h if not already loaded. | |
59 | In some systems loading it twice is suicidal. */ | |
60 | #ifndef makedev | |
61 | #include <sys/types.h> | |
62 | #endif | |
63 | ||
64 | #include "dispextern.h" | |
65 | ||
87485d6f | 66 | #ifdef HAVE_X_WINDOWS |
703dc2a8 | 67 | #undef HAVE_MULTILINGUAL_MENU |
18686d47 RS |
68 | #ifdef USE_X_TOOLKIT |
69 | #include <X11/Xlib.h> | |
70 | #include <X11/IntrinsicP.h> | |
71 | #include <X11/CoreP.h> | |
72 | #include <X11/StringDefs.h> | |
60f312e2 | 73 | #include <X11/Shell.h> |
5ee80bd3 | 74 | #ifdef USE_LUCID |
1d1c1567 | 75 | #include <X11/Xaw/Paned.h> |
5ee80bd3 | 76 | #endif /* USE_LUCID */ |
18686d47 | 77 | #include "../lwlib/lwlib.h" |
a352a815 RS |
78 | #else /* not USE_X_TOOLKIT */ |
79 | #include "../oldXMenu/XMenu.h" | |
80 | #endif /* not USE_X_TOOLKIT */ | |
81 | #endif /* HAVE_X_WINDOWS */ | |
18686d47 | 82 | |
3427a3db GM |
83 | #ifdef USE_MOTIF |
84 | #include <Xm/Xm.h> /* for LESSTIF_VERSION */ | |
85 | #endif | |
86 | ||
dcfdbac7 JB |
87 | #define min(x,y) (((x) < (y)) ? (x) : (y)) |
88 | #define max(x,y) (((x) > (y)) ? (x) : (y)) | |
89 | ||
dcfdbac7 JB |
90 | #ifndef TRUE |
91 | #define TRUE 1 | |
92 | #define FALSE 0 | |
78589e07 | 93 | #endif /* no TRUE */ |
dcfdbac7 | 94 | |
bfc524bc RS |
95 | Lisp_Object Vmenu_updating_frame; |
96 | ||
0314aacb RS |
97 | Lisp_Object Qdebug_on_next_call; |
98 | ||
18686d47 | 99 | extern Lisp_Object Qmenu_bar; |
92280f67 | 100 | extern Lisp_Object Qmouse_click, Qevent_kind; |
78589e07 | 101 | |
de57a39c | 102 | extern Lisp_Object QCtoggle, QCradio; |
8fbc986d | 103 | |
88766961 RS |
104 | extern Lisp_Object Voverriding_local_map; |
105 | extern Lisp_Object Voverriding_local_map_menu_flag; | |
106 | ||
107 | extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map; | |
108 | ||
109 | extern Lisp_Object Qmenu_bar_update_hook; | |
110 | ||
18686d47 | 111 | #ifdef USE_X_TOOLKIT |
88766961 | 112 | extern void set_frame_menubar (); |
78589e07 RS |
113 | extern void process_expose_from_menu (); |
114 | extern XtAppContext Xt_app_con; | |
115 | ||
165e1749 | 116 | static Lisp_Object xdialog_show (); |
4dedbfe0 | 117 | void popup_get_selection (); |
18686d47 RS |
118 | #endif |
119 | ||
3427a3db GM |
120 | #ifdef USE_X_TOOLKIT |
121 | ||
122 | /* Define HAVE_BOXES if meus can handle radio and toggle buttons. */ | |
123 | ||
124 | #define HAVE_BOXES 1 | |
125 | #endif | |
126 | ||
127 | static void push_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object, | |
128 | Lisp_Object, Lisp_Object, Lisp_Object, | |
850df50b | 129 | Lisp_Object, Lisp_Object)); |
78589e07 RS |
130 | static Lisp_Object xmenu_show (); |
131 | static void keymap_panes (); | |
132 | static void single_keymap_panes (); | |
de57a39c | 133 | static void single_menu_item (); |
78589e07 RS |
134 | static void list_of_panes (); |
135 | static void list_of_items (); | |
136 | \f | |
137 | /* This holds a Lisp vector that holds the results of decoding | |
138 | the keymaps or alist-of-alists that specify a menu. | |
dcfdbac7 | 139 | |
78589e07 | 140 | It describes the panes and items within the panes. |
dcfdbac7 | 141 | |
78589e07 RS |
142 | Each pane is described by 3 elements in the vector: |
143 | t, the pane name, the pane's prefix key. | |
a352a815 | 144 | Then follow the pane's items, with 5 elements per item: |
78589e07 | 145 | the item string, the enable flag, the item's value, |
a352a815 | 146 | the definition, and the equivalent keyboard key's description string. |
dcfdbac7 | 147 | |
101bb4a5 RS |
148 | In some cases, multiple levels of menus may be described. |
149 | A single vector slot containing nil indicates the start of a submenu. | |
150 | A single vector slot containing lambda indicates the end of a submenu. | |
151 | The submenu follows a menu item which is the way to reach the submenu. | |
152 | ||
fcaa7665 RS |
153 | A single vector slot containing quote indicates that the |
154 | following items should appear on the right of a dialog box. | |
155 | ||
78589e07 RS |
156 | Using a Lisp vector to hold this information while we decode it |
157 | takes care of protecting all the data from GC. */ | |
dcfdbac7 | 158 | |
78589e07 RS |
159 | #define MENU_ITEMS_PANE_NAME 1 |
160 | #define MENU_ITEMS_PANE_PREFIX 2 | |
161 | #define MENU_ITEMS_PANE_LENGTH 3 | |
088831f6 | 162 | |
850df50b GM |
163 | enum menu_item_idx |
164 | { | |
165 | MENU_ITEMS_ITEM_NAME = 0, | |
166 | MENU_ITEMS_ITEM_ENABLE, | |
167 | MENU_ITEMS_ITEM_VALUE, | |
168 | MENU_ITEMS_ITEM_EQUIV_KEY, | |
169 | MENU_ITEMS_ITEM_DEFINITION, | |
170 | MENU_ITEMS_ITEM_TYPE, | |
171 | MENU_ITEMS_ITEM_SELECTED, | |
172 | MENU_ITEMS_ITEM_HELP, | |
173 | MENU_ITEMS_ITEM_LENGTH | |
174 | }; | |
7da99777 | 175 | |
78589e07 | 176 | static Lisp_Object menu_items; |
18686d47 | 177 | |
78589e07 RS |
178 | /* Number of slots currently allocated in menu_items. */ |
179 | static int menu_items_allocated; | |
18686d47 | 180 | |
78589e07 RS |
181 | /* This is the index in menu_items of the first empty slot. */ |
182 | static int menu_items_used; | |
18686d47 | 183 | |
101bb4a5 RS |
184 | /* The number of panes currently recorded in menu_items, |
185 | excluding those within submenus. */ | |
78589e07 | 186 | static int menu_items_n_panes; |
18686d47 | 187 | |
101bb4a5 RS |
188 | /* Current depth within submenus. */ |
189 | static int menu_items_submenu_depth; | |
190 | ||
4dedbfe0 | 191 | /* Flag which when set indicates a dialog or menu has been posted by |
c98fcf4b | 192 | Xt on behalf of one of the widget sets. */ |
7555d825 | 193 | int popup_activated_flag; |
4dedbfe0 | 194 | |
88766961 | 195 | static int next_menubar_widget_id; |
745c34fb | 196 | |
a9be6839 RS |
197 | /* This is set nonzero after the user activates the menu bar, and set |
198 | to zero again after the menu bars are redisplayed by prepare_menu_bar. | |
199 | While it is nonzero, all calls to set_frame_menubar go deep. | |
200 | ||
201 | I don't understand why this is needed, but it does seem to be | |
202 | needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */ | |
203 | ||
204 | int pending_menu_activation; | |
bd3a4da2 | 205 | \f |
88766961 | 206 | #ifdef USE_X_TOOLKIT |
bd3a4da2 | 207 | |
7556890b | 208 | /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */ |
bd3a4da2 | 209 | |
88766961 RS |
210 | static struct frame * |
211 | menubar_id_to_frame (id) | |
212 | LWLIB_ID id; | |
213 | { | |
214 | Lisp_Object tail, frame; | |
215 | FRAME_PTR f; | |
bd3a4da2 | 216 | |
8e713be6 | 217 | for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail)) |
bd3a4da2 | 218 | { |
8e713be6 | 219 | frame = XCAR (tail); |
88766961 RS |
220 | if (!GC_FRAMEP (frame)) |
221 | continue; | |
222 | f = XFRAME (frame); | |
2d764c78 | 223 | if (!FRAME_WINDOW_P (f)) |
88766961 | 224 | continue; |
7556890b | 225 | if (f->output_data.x->id == id) |
88766961 | 226 | return f; |
bd3a4da2 | 227 | } |
88766961 | 228 | return 0; |
bd3a4da2 | 229 | } |
88766961 RS |
230 | |
231 | #endif | |
4dedbfe0 | 232 | \f |
78589e07 RS |
233 | /* Initialize the menu_items structure if we haven't already done so. |
234 | Also mark it as currently empty. */ | |
235 | ||
236 | static void | |
237 | init_menu_items () | |
238 | { | |
239 | if (NILP (menu_items)) | |
240 | { | |
241 | menu_items_allocated = 60; | |
242 | menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil); | |
18686d47 RS |
243 | } |
244 | ||
78589e07 RS |
245 | menu_items_used = 0; |
246 | menu_items_n_panes = 0; | |
101bb4a5 | 247 | menu_items_submenu_depth = 0; |
78589e07 | 248 | } |
18686d47 | 249 | |
78589e07 RS |
250 | /* Call at the end of generating the data in menu_items. |
251 | This fills in the number of items in the last pane. */ | |
1658603c | 252 | |
78589e07 RS |
253 | static void |
254 | finish_menu_items () | |
255 | { | |
256 | } | |
1658603c | 257 | |
78589e07 RS |
258 | /* Call when finished using the data for the current menu |
259 | in menu_items. */ | |
1658603c | 260 | |
78589e07 RS |
261 | static void |
262 | discard_menu_items () | |
263 | { | |
264 | /* Free the structure if it is especially large. | |
265 | Otherwise, hold on to it, to save time. */ | |
266 | if (menu_items_allocated > 200) | |
267 | { | |
268 | menu_items = Qnil; | |
269 | menu_items_allocated = 0; | |
270 | } | |
271 | } | |
1658603c | 272 | |
101bb4a5 RS |
273 | /* Make the menu_items vector twice as large. */ |
274 | ||
275 | static void | |
276 | grow_menu_items () | |
277 | { | |
278 | Lisp_Object old; | |
279 | int old_size = menu_items_allocated; | |
280 | old = menu_items; | |
281 | ||
282 | menu_items_allocated *= 2; | |
283 | menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil); | |
284 | bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents, | |
285 | old_size * sizeof (Lisp_Object)); | |
286 | } | |
287 | ||
288 | /* Begin a submenu. */ | |
289 | ||
290 | static void | |
291 | push_submenu_start () | |
292 | { | |
293 | if (menu_items_used + 1 > menu_items_allocated) | |
294 | grow_menu_items (); | |
295 | ||
296 | XVECTOR (menu_items)->contents[menu_items_used++] = Qnil; | |
297 | menu_items_submenu_depth++; | |
298 | } | |
299 | ||
300 | /* End a submenu. */ | |
301 | ||
302 | static void | |
303 | push_submenu_end () | |
304 | { | |
305 | if (menu_items_used + 1 > menu_items_allocated) | |
306 | grow_menu_items (); | |
307 | ||
308 | XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda; | |
309 | menu_items_submenu_depth--; | |
310 | } | |
311 | ||
fcaa7665 RS |
312 | /* Indicate boundary between left and right. */ |
313 | ||
314 | static void | |
315 | push_left_right_boundary () | |
316 | { | |
317 | if (menu_items_used + 1 > menu_items_allocated) | |
318 | grow_menu_items (); | |
319 | ||
320 | XVECTOR (menu_items)->contents[menu_items_used++] = Qquote; | |
321 | } | |
322 | ||
78589e07 RS |
323 | /* Start a new menu pane in menu_items.. |
324 | NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */ | |
1658603c | 325 | |
78589e07 RS |
326 | static void |
327 | push_menu_pane (name, prefix_vec) | |
328 | Lisp_Object name, prefix_vec; | |
329 | { | |
330 | if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated) | |
101bb4a5 | 331 | grow_menu_items (); |
dcfdbac7 | 332 | |
101bb4a5 RS |
333 | if (menu_items_submenu_depth == 0) |
334 | menu_items_n_panes++; | |
78589e07 RS |
335 | XVECTOR (menu_items)->contents[menu_items_used++] = Qt; |
336 | XVECTOR (menu_items)->contents[menu_items_used++] = name; | |
337 | XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec; | |
338 | } | |
dcfdbac7 | 339 | |
3427a3db GM |
340 | /* Push one menu item into the current pane. NAME is the string to |
341 | display. ENABLE if non-nil means this item can be selected. KEY | |
342 | is the key generated by choosing this item, or nil if this item | |
343 | doesn't really have a definition. DEF is the definition of this | |
344 | item. EQUIV is the textual description of the keyboard equivalent | |
345 | for this item (or nil if none). TYPE is the type of this menu | |
346 | item, one of nil, `toggle' or `radio'. */ | |
18686d47 | 347 | |
78589e07 | 348 | static void |
850df50b GM |
349 | push_menu_item (name, enable, key, def, equiv, type, selected, help) |
350 | Lisp_Object name, enable, key, def, equiv, type, selected, help; | |
78589e07 RS |
351 | { |
352 | if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated) | |
101bb4a5 | 353 | grow_menu_items (); |
088831f6 | 354 | |
78589e07 RS |
355 | XVECTOR (menu_items)->contents[menu_items_used++] = name; |
356 | XVECTOR (menu_items)->contents[menu_items_used++] = enable; | |
357 | XVECTOR (menu_items)->contents[menu_items_used++] = key; | |
358 | XVECTOR (menu_items)->contents[menu_items_used++] = equiv; | |
a352a815 | 359 | XVECTOR (menu_items)->contents[menu_items_used++] = def; |
3427a3db GM |
360 | XVECTOR (menu_items)->contents[menu_items_used++] = type; |
361 | XVECTOR (menu_items)->contents[menu_items_used++] = selected; | |
850df50b | 362 | XVECTOR (menu_items)->contents[menu_items_used++] = help; |
78589e07 RS |
363 | } |
364 | \f | |
78589e07 | 365 | /* Look through KEYMAPS, a vector of keymaps that is NMAPS long, |
101bb4a5 RS |
366 | and generate menu panes for them in menu_items. |
367 | If NOTREAL is nonzero, | |
368 | don't bother really computing whether an item is enabled. */ | |
18686d47 | 369 | |
78589e07 | 370 | static void |
101bb4a5 | 371 | keymap_panes (keymaps, nmaps, notreal) |
78589e07 RS |
372 | Lisp_Object *keymaps; |
373 | int nmaps; | |
101bb4a5 | 374 | int notreal; |
18686d47 | 375 | { |
78589e07 | 376 | int mapno; |
18686d47 | 377 | |
78589e07 | 378 | init_menu_items (); |
18686d47 | 379 | |
78589e07 RS |
380 | /* Loop over the given keymaps, making a pane for each map. |
381 | But don't make a pane that is empty--ignore that map instead. | |
382 | P is the number of panes we have made so far. */ | |
383 | for (mapno = 0; mapno < nmaps; mapno++) | |
488205c0 EZ |
384 | single_keymap_panes (keymaps[mapno], |
385 | map_prompt (keymaps[mapno]), Qnil, notreal, 10); | |
78589e07 RS |
386 | |
387 | finish_menu_items (); | |
388 | } | |
389 | ||
390 | /* This is a recursive subroutine of keymap_panes. | |
391 | It handles one keymap, KEYMAP. | |
392 | The other arguments are passed along | |
101bb4a5 | 393 | or point to local variables of the previous function. |
de57a39c RS |
394 | If NOTREAL is nonzero, only check for equivalent key bindings, don't |
395 | evaluate expressions in menu items and don't make any menu. | |
98381e42 RS |
396 | |
397 | If we encounter submenus deeper than MAXDEPTH levels, ignore them. */ | |
78589e07 RS |
398 | |
399 | static void | |
98381e42 | 400 | single_keymap_panes (keymap, pane_name, prefix, notreal, maxdepth) |
78589e07 RS |
401 | Lisp_Object keymap; |
402 | Lisp_Object pane_name; | |
403 | Lisp_Object prefix; | |
101bb4a5 | 404 | int notreal; |
98381e42 | 405 | int maxdepth; |
78589e07 | 406 | { |
de57a39c RS |
407 | Lisp_Object pending_maps = Qnil; |
408 | Lisp_Object tail, item; | |
409 | struct gcpro gcpro1, gcpro2; | |
410 | int notbuttons = 0; | |
78589e07 | 411 | |
98381e42 RS |
412 | if (maxdepth <= 0) |
413 | return; | |
414 | ||
78589e07 RS |
415 | push_menu_pane (pane_name, prefix); |
416 | ||
de57a39c RS |
417 | #ifndef HAVE_BOXES |
418 | /* Remember index for first item in this pane so we can go back and | |
419 | add a prefix when (if) we see the first button. After that, notbuttons | |
420 | is set to 0, to mark that we have seen a button and all non button | |
421 | items need a prefix. */ | |
422 | notbuttons = menu_items_used; | |
423 | #endif | |
424 | ||
8e713be6 | 425 | for (tail = keymap; CONSP (tail); tail = XCDR (tail)) |
18686d47 | 426 | { |
de57a39c RS |
427 | GCPRO2 (keymap, pending_maps); |
428 | /* Look at each key binding, and if it is a menu item add it | |
429 | to this menu. */ | |
8e713be6 | 430 | item = XCAR (tail); |
b5bb2705 | 431 | if (CONSP (item)) |
8e713be6 | 432 | single_menu_item (XCAR (item), XCDR (item), |
de57a39c | 433 | &pending_maps, notreal, maxdepth, ¬buttons); |
b5bb2705 | 434 | else if (VECTORP (item)) |
78589e07 RS |
435 | { |
436 | /* Loop over the char values represented in the vector. */ | |
437 | int len = XVECTOR (item)->size; | |
438 | int c; | |
439 | for (c = 0; c < len; c++) | |
440 | { | |
441 | Lisp_Object character; | |
33b43fa6 | 442 | XSETFASTINT (character, c); |
de57a39c RS |
443 | single_menu_item (character, XVECTOR (item)->contents[c], |
444 | &pending_maps, notreal, maxdepth, ¬buttons); | |
78589e07 | 445 | } |
18686d47 | 446 | } |
de57a39c | 447 | UNGCPRO; |
18686d47 | 448 | } |
78589e07 RS |
449 | |
450 | /* Process now any submenus which want to be panes at this level. */ | |
451 | while (!NILP (pending_maps)) | |
452 | { | |
101bb4a5 | 453 | Lisp_Object elt, eltcdr, string; |
78589e07 | 454 | elt = Fcar (pending_maps); |
8e713be6 KR |
455 | eltcdr = XCDR (elt); |
456 | string = XCAR (eltcdr); | |
101bb4a5 RS |
457 | /* We no longer discard the @ from the beginning of the string here. |
458 | Instead, we do this in xmenu_show. */ | |
459 | single_keymap_panes (Fcar (elt), string, | |
8e713be6 | 460 | XCDR (eltcdr), notreal, maxdepth - 1); |
78589e07 RS |
461 | pending_maps = Fcdr (pending_maps); |
462 | } | |
18686d47 | 463 | } |
78589e07 | 464 | \f |
de57a39c RS |
465 | /* This is a subroutine of single_keymap_panes that handles one |
466 | keymap entry. | |
467 | KEY is a key in a keymap and ITEM is its binding. | |
468 | PENDING_MAPS_PTR points to a list of keymaps waiting to be made into | |
469 | separate panes. | |
470 | If NOTREAL is nonzero, only check for equivalent key bindings, don't | |
471 | evaluate expressions in menu items and don't make any menu. | |
472 | If we encounter submenus deeper than MAXDEPTH levels, ignore them. | |
473 | NOTBUTTONS_PTR is only used when simulating toggle boxes and radio | |
474 | buttons. It points to variable notbuttons in single_keymap_panes, | |
475 | which keeps track of if we have seen a button in this menu or not. */ | |
476 | ||
477 | static void | |
478 | single_menu_item (key, item, pending_maps_ptr, notreal, maxdepth, | |
479 | notbuttons_ptr) | |
480 | Lisp_Object key, item; | |
481 | Lisp_Object *pending_maps_ptr; | |
482 | int maxdepth, notreal; | |
483 | int *notbuttons_ptr; | |
484 | { | |
faa935b6 | 485 | Lisp_Object map, item_string, enabled; |
de57a39c RS |
486 | struct gcpro gcpro1, gcpro2; |
487 | int res; | |
488 | ||
489 | /* Parse the menu item and leave the result in item_properties. */ | |
490 | GCPRO2 (key, item); | |
491 | res = parse_menu_item (item, notreal, 0); | |
492 | UNGCPRO; | |
493 | if (!res) | |
494 | return; /* Not a menu item. */ | |
495 | ||
496 | map = XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP]; | |
497 | ||
498 | if (notreal) | |
499 | { | |
500 | /* We don't want to make a menu, just traverse the keymaps to | |
501 | precompute equivalent key bindings. */ | |
502 | if (!NILP (map)) | |
503 | single_keymap_panes (map, Qnil, key, 1, maxdepth - 1); | |
504 | return; | |
505 | } | |
506 | ||
507 | enabled = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE]; | |
508 | item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME]; | |
509 | ||
510 | if (!NILP (map) && XSTRING (item_string)->data[0] == '@') | |
511 | { | |
512 | if (!NILP (enabled)) | |
513 | /* An enabled separate pane. Remember this to handle it later. */ | |
514 | *pending_maps_ptr = Fcons (Fcons (map, Fcons (item_string, key)), | |
515 | *pending_maps_ptr); | |
516 | return; | |
517 | } | |
518 | ||
519 | #ifndef HAVE_BOXES | |
520 | /* Simulate radio buttons and toggle boxes by putting a prefix in | |
521 | front of them. */ | |
522 | { | |
523 | Lisp_Object prefix = Qnil; | |
524 | Lisp_Object type = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE]; | |
525 | if (!NILP (type)) | |
526 | { | |
527 | Lisp_Object selected | |
528 | = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED]; | |
529 | ||
530 | if (*notbuttons_ptr) | |
531 | /* The first button. Line up previous items in this menu. */ | |
532 | { | |
533 | int index = *notbuttons_ptr; /* Index for first item this menu. */ | |
534 | int submenu = 0; | |
535 | Lisp_Object tem; | |
536 | while (index < menu_items_used) | |
537 | { | |
538 | tem | |
539 | = XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME]; | |
540 | if (NILP (tem)) | |
541 | { | |
542 | index++; | |
543 | submenu++; /* Skip sub menu. */ | |
544 | } | |
545 | else if (EQ (tem, Qlambda)) | |
546 | { | |
547 | index++; | |
548 | submenu--; /* End sub menu. */ | |
549 | } | |
550 | else if (EQ (tem, Qt)) | |
551 | index += 3; /* Skip new pane marker. */ | |
552 | else if (EQ (tem, Qquote)) | |
553 | index++; /* Skip a left, right divider. */ | |
554 | else | |
555 | { | |
556 | if (!submenu && XSTRING (tem)->data[0] != '\0' | |
557 | && XSTRING (tem)->data[0] != '-') | |
558 | XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME] | |
559 | = concat2 (build_string (" "), tem); | |
560 | index += MENU_ITEMS_ITEM_LENGTH; | |
561 | } | |
562 | } | |
563 | *notbuttons_ptr = 0; | |
564 | } | |
565 | ||
566 | /* Calculate prefix, if any, for this item. */ | |
567 | if (EQ (type, QCtoggle)) | |
568 | prefix = build_string (NILP (selected) ? "[ ] " : "[X] "); | |
569 | else if (EQ (type, QCradio)) | |
570 | prefix = build_string (NILP (selected) ? "( ) " : "(*) "); | |
571 | } | |
572 | /* Not a button. If we have earlier buttons, then we need a prefix. */ | |
573 | else if (!*notbuttons_ptr && XSTRING (item_string)->data[0] != '\0' | |
574 | && XSTRING (item_string)->data[0] != '-') | |
575 | prefix = build_string (" "); | |
576 | ||
577 | if (!NILP (prefix)) | |
578 | item_string = concat2 (prefix, item_string); | |
579 | } | |
580 | #endif /* not HAVE_BOXES */ | |
581 | ||
582 | #ifndef USE_X_TOOLKIT | |
583 | if (!NILP(map)) | |
584 | /* Indicate visually that this is a submenu. */ | |
585 | item_string = concat2 (item_string, build_string (" >")); | |
586 | #endif | |
587 | ||
588 | push_menu_item (item_string, enabled, key, | |
589 | XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF], | |
3427a3db GM |
590 | XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ], |
591 | XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE], | |
850df50b GM |
592 | XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED], |
593 | XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]); | |
de57a39c RS |
594 | |
595 | #ifdef USE_X_TOOLKIT | |
596 | /* Display a submenu using the toolkit. */ | |
597 | if (! (NILP (map) || NILP (enabled))) | |
598 | { | |
599 | push_submenu_start (); | |
600 | single_keymap_panes (map, Qnil, key, 0, maxdepth - 1); | |
601 | push_submenu_end (); | |
602 | } | |
603 | #endif | |
604 | } | |
605 | \f | |
8e6208c5 | 606 | /* Push all the panes and items of a menu described by the |
78589e07 RS |
607 | alist-of-alists MENU. |
608 | This handles old-fashioned calls to x-popup-menu. */ | |
18686d47 | 609 | |
78589e07 RS |
610 | static void |
611 | list_of_panes (menu) | |
18686d47 | 612 | Lisp_Object menu; |
18686d47 | 613 | { |
78589e07 RS |
614 | Lisp_Object tail; |
615 | ||
616 | init_menu_items (); | |
617 | ||
618 | for (tail = menu; !NILP (tail); tail = Fcdr (tail)) | |
619 | { | |
620 | Lisp_Object elt, pane_name, pane_data; | |
621 | elt = Fcar (tail); | |
622 | pane_name = Fcar (elt); | |
623 | CHECK_STRING (pane_name, 0); | |
624 | push_menu_pane (pane_name, Qnil); | |
625 | pane_data = Fcdr (elt); | |
626 | CHECK_CONS (pane_data, 0); | |
627 | list_of_items (pane_data); | |
628 | } | |
629 | ||
630 | finish_menu_items (); | |
631 | } | |
632 | ||
633 | /* Push the items in a single pane defined by the alist PANE. */ | |
634 | ||
635 | static void | |
636 | list_of_items (pane) | |
637 | Lisp_Object pane; | |
638 | { | |
639 | Lisp_Object tail, item, item1; | |
640 | ||
641 | for (tail = pane; !NILP (tail); tail = Fcdr (tail)) | |
642 | { | |
643 | item = Fcar (tail); | |
644 | if (STRINGP (item)) | |
850df50b | 645 | push_menu_item (item, Qnil, Qnil, Qt, Qnil, Qnil, Qnil, Qnil); |
fcaa7665 RS |
646 | else if (NILP (item)) |
647 | push_left_right_boundary (); | |
78589e07 RS |
648 | else |
649 | { | |
650 | CHECK_CONS (item, 0); | |
651 | item1 = Fcar (item); | |
652 | CHECK_STRING (item1, 1); | |
850df50b | 653 | push_menu_item (item1, Qt, Fcdr (item), Qt, Qnil, Qnil, Qnil, Qnil); |
78589e07 RS |
654 | } |
655 | } | |
656 | } | |
657 | \f | |
540e52d1 | 658 | DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0, |
78589e07 RS |
659 | "Pop up a deck-of-cards menu and return user's selection.\n\ |
660 | POSITION is a position specification. This is either a mouse button event\n\ | |
661 | or a list ((XOFFSET YOFFSET) WINDOW)\n\ | |
748a0e1f | 662 | where XOFFSET and YOFFSET are positions in pixels from the top left\n\ |
78589e07 RS |
663 | corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\ |
664 | This controls the position of the center of the first line\n\ | |
665 | in the first pane of the menu, not the top left of the menu as a whole.\n\ | |
666 | If POSITION is t, it means to use the current mouse position.\n\ | |
667 | \n\ | |
668 | MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\ | |
669 | The menu items come from key bindings that have a menu string as well as\n\ | |
670 | a definition; actually, the \"definition\" in such a key binding looks like\n\ | |
671 | \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\ | |
672 | the keymap as a top-level element.\n\n\ | |
a7ebd443 RS |
673 | If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.\n\ |
674 | Otherwise, REAL-DEFINITION should be a valid key binding definition.\n\ | |
675 | \n\ | |
78589e07 RS |
676 | You can also use a list of keymaps as MENU.\n\ |
677 | Then each keymap makes a separate pane.\n\ | |
678 | When MENU is a keymap or a list of keymaps, the return value\n\ | |
679 | is a list of events.\n\n\ | |
a7ebd443 | 680 | \n\ |
78589e07 RS |
681 | Alternatively, you can specify a menu of multiple panes\n\ |
682 | with a list of the form (TITLE PANE1 PANE2...),\n\ | |
683 | where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\ | |
684 | Each ITEM is normally a cons cell (STRING . VALUE);\n\ | |
685 | but a string can appear as an item--that makes a nonselectable line\n\ | |
686 | in the menu.\n\ | |
687 | With this form of menu, the return value is VALUE from the chosen item.\n\ | |
688 | \n\ | |
689 | If POSITION is nil, don't display the menu at all, just precalculate the\n\ | |
690 | cached information about equivalent key sequences.") | |
691 | (position, menu) | |
692 | Lisp_Object position, menu; | |
693 | { | |
18686d47 | 694 | Lisp_Object keymap, tem; |
6bbd7a29 | 695 | int xpos = 0, ypos = 0; |
78589e07 RS |
696 | Lisp_Object title; |
697 | char *error_name; | |
698 | Lisp_Object selection; | |
6bbd7a29 | 699 | struct frame *f = NULL; |
78589e07 RS |
700 | Lisp_Object x, y, window; |
701 | int keymaps = 0; | |
9685a93f | 702 | int for_click = 0; |
78589e07 RS |
703 | struct gcpro gcpro1; |
704 | ||
1e659e4c | 705 | #ifdef HAVE_MENUS |
78589e07 RS |
706 | if (! NILP (position)) |
707 | { | |
101bb4a5 RS |
708 | check_x (); |
709 | ||
78589e07 | 710 | /* Decode the first argument: find the window and the coordinates. */ |
9572375b | 711 | if (EQ (position, Qt) |
2b06561a DL |
712 | || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar) |
713 | || EQ (XCAR (position), Qtool_bar)))) | |
78589e07 RS |
714 | { |
715 | /* Use the mouse's current position. */ | |
b404828f | 716 | FRAME_PTR new_f = SELECTED_FRAME (); |
78589e07 | 717 | Lisp_Object bar_window; |
dfcf069d | 718 | enum scroll_bar_part part; |
78589e07 RS |
719 | unsigned long time; |
720 | ||
b137c582 | 721 | if (mouse_position_hook) |
46b657e2 RS |
722 | (*mouse_position_hook) (&new_f, 1, &bar_window, |
723 | &part, &x, &y, &time); | |
5ca2ef64 | 724 | if (new_f != 0) |
a39f0477 | 725 | XSETFRAME (window, new_f); |
5ca2ef64 RS |
726 | else |
727 | { | |
728 | window = selected_window; | |
33b43fa6 KH |
729 | XSETFASTINT (x, 0); |
730 | XSETFASTINT (y, 0); | |
5ca2ef64 | 731 | } |
78589e07 RS |
732 | } |
733 | else | |
734 | { | |
735 | tem = Fcar (position); | |
b5bb2705 | 736 | if (CONSP (tem)) |
78589e07 RS |
737 | { |
738 | window = Fcar (Fcdr (position)); | |
739 | x = Fcar (tem); | |
740 | y = Fcar (Fcdr (tem)); | |
741 | } | |
742 | else | |
743 | { | |
9685a93f | 744 | for_click = 1; |
78589e07 RS |
745 | tem = Fcar (Fcdr (position)); /* EVENT_START (position) */ |
746 | window = Fcar (tem); /* POSN_WINDOW (tem) */ | |
747 | tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */ | |
748 | x = Fcar (tem); | |
749 | y = Fcdr (tem); | |
78589e07 RS |
750 | } |
751 | } | |
752 | ||
753 | CHECK_NUMBER (x, 0); | |
754 | CHECK_NUMBER (y, 0); | |
755 | ||
756 | /* Decode where to put the menu. */ | |
757 | ||
b5bb2705 | 758 | if (FRAMEP (window)) |
78589e07 RS |
759 | { |
760 | f = XFRAME (window); | |
78589e07 RS |
761 | xpos = 0; |
762 | ypos = 0; | |
763 | } | |
b5bb2705 | 764 | else if (WINDOWP (window)) |
78589e07 RS |
765 | { |
766 | CHECK_LIVE_WINDOW (window, 0); | |
767 | f = XFRAME (WINDOW_FRAME (XWINDOW (window))); | |
768 | ||
99d3fac7 KH |
769 | xpos = (FONT_WIDTH (f->output_data.x->font) |
770 | * XFASTINT (XWINDOW (window)->left)); | |
771 | ypos = (f->output_data.x->line_height | |
772 | * XFASTINT (XWINDOW (window)->top)); | |
78589e07 RS |
773 | } |
774 | else | |
775 | /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME, | |
776 | but I don't want to make one now. */ | |
777 | CHECK_WINDOW (window, 0); | |
778 | ||
779 | xpos += XINT (x); | |
780 | ypos += XINT (y); | |
bfc524bc RS |
781 | |
782 | XSETFRAME (Vmenu_updating_frame, f); | |
78589e07 | 783 | } |
bfc524bc | 784 | Vmenu_updating_frame = Qnil; |
1e659e4c | 785 | #endif /* HAVE_MENUS */ |
78589e07 RS |
786 | |
787 | title = Qnil; | |
788 | GCPRO1 (title); | |
789 | ||
790 | /* Decode the menu items from what was specified. */ | |
18686d47 | 791 | |
02067692 SM |
792 | keymap = get_keymap (menu, 0, 0); |
793 | if (CONSP (keymap)) | |
18686d47 RS |
794 | { |
795 | /* We were given a keymap. Extract menu info from the keymap. */ | |
796 | Lisp_Object prompt; | |
18686d47 | 797 | |
78589e07 | 798 | /* Extract the detailed info to make one pane. */ |
101bb4a5 | 799 | keymap_panes (&menu, 1, NILP (position)); |
78589e07 | 800 | |
18686d47 RS |
801 | /* Search for a string appearing directly as an element of the keymap. |
802 | That string is the title of the menu. */ | |
803 | prompt = map_prompt (keymap); | |
4f9ad016 RS |
804 | if (NILP (title) && !NILP (prompt)) |
805 | title = prompt; | |
18686d47 | 806 | |
78589e07 RS |
807 | /* Make that be the pane title of the first pane. */ |
808 | if (!NILP (prompt) && menu_items_n_panes >= 0) | |
809 | XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt; | |
810 | ||
811 | keymaps = 1; | |
18686d47 | 812 | } |
02067692 | 813 | else if (CONSP (menu) && KEYMAPP (XCAR (menu))) |
18686d47 RS |
814 | { |
815 | /* We were given a list of keymaps. */ | |
18686d47 RS |
816 | int nmaps = XFASTINT (Flength (menu)); |
817 | Lisp_Object *maps | |
818 | = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object)); | |
819 | int i; | |
78589e07 RS |
820 | |
821 | title = Qnil; | |
18686d47 RS |
822 | |
823 | /* The first keymap that has a prompt string | |
824 | supplies the menu title. */ | |
b5bb2705 | 825 | for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem)) |
18686d47 | 826 | { |
78589e07 RS |
827 | Lisp_Object prompt; |
828 | ||
02067692 | 829 | maps[i++] = keymap = get_keymap (Fcar (tem), 1, 0); |
18686d47 RS |
830 | |
831 | prompt = map_prompt (keymap); | |
78589e07 RS |
832 | if (NILP (title) && !NILP (prompt)) |
833 | title = prompt; | |
18686d47 RS |
834 | } |
835 | ||
836 | /* Extract the detailed info to make one pane. */ | |
101bb4a5 | 837 | keymap_panes (maps, nmaps, NILP (position)); |
78589e07 RS |
838 | |
839 | /* Make the title be the pane title of the first pane. */ | |
840 | if (!NILP (title) && menu_items_n_panes >= 0) | |
841 | XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title; | |
842 | ||
843 | keymaps = 1; | |
18686d47 RS |
844 | } |
845 | else | |
846 | { | |
847 | /* We were given an old-fashioned menu. */ | |
78589e07 RS |
848 | title = Fcar (menu); |
849 | CHECK_STRING (title, 1); | |
18686d47 | 850 | |
78589e07 | 851 | list_of_panes (Fcdr (menu)); |
18686d47 | 852 | |
78589e07 RS |
853 | keymaps = 0; |
854 | } | |
18686d47 | 855 | |
78589e07 | 856 | if (NILP (position)) |
18686d47 | 857 | { |
78589e07 RS |
858 | discard_menu_items (); |
859 | UNGCPRO; | |
860 | return Qnil; | |
18686d47 RS |
861 | } |
862 | ||
1e659e4c | 863 | #ifdef HAVE_MENUS |
78589e07 RS |
864 | /* Display them in a menu. */ |
865 | BLOCK_INPUT; | |
18686d47 | 866 | |
673a6211 | 867 | selection = xmenu_show (f, xpos, ypos, for_click, |
78589e07 RS |
868 | keymaps, title, &error_name); |
869 | UNBLOCK_INPUT; | |
18686d47 | 870 | |
78589e07 | 871 | discard_menu_items (); |
18686d47 | 872 | |
78589e07 | 873 | UNGCPRO; |
1e659e4c | 874 | #endif /* HAVE_MENUS */ |
18686d47 | 875 | |
78589e07 RS |
876 | if (error_name) error (error_name); |
877 | return selection; | |
18686d47 | 878 | } |
165e1749 | 879 | |
1e659e4c RS |
880 | #ifdef HAVE_MENUS |
881 | ||
540e52d1 | 882 | DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0, |
165e1749 | 883 | "Pop up a dialog box and return user's selection.\n\ |
99fe880d RS |
884 | POSITION specifies which frame to use.\n\ |
885 | This is normally a mouse button event or a window or frame.\n\ | |
886 | If POSITION is t, it means to use the frame the mouse is on.\n\ | |
887 | The dialog box appears in the middle of the specified frame.\n\ | |
165e1749 | 888 | \n\ |
99fe880d RS |
889 | CONTENTS specifies the alternatives to display in the dialog box.\n\ |
890 | It is a list of the form (TITLE ITEM1 ITEM2...).\n\ | |
891 | Each ITEM is a cons cell (STRING . VALUE).\n\ | |
fcaa7665 RS |
892 | The return value is VALUE from the chosen item.\n\n\ |
893 | An ITEM may also be just a string--that makes a nonselectable item.\n\ | |
894 | An ITEM may also be nil--that means to put all preceding items\n\ | |
895 | on the left of the dialog box and all following items on the right.\n\ | |
896 | \(By default, approximately half appear on each side.)") | |
99fe880d RS |
897 | (position, contents) |
898 | Lisp_Object position, contents; | |
165e1749 | 899 | { |
6bbd7a29 | 900 | struct frame * f = NULL; |
99fe880d | 901 | Lisp_Object window; |
165e1749 FP |
902 | |
903 | check_x (); | |
904 | ||
99fe880d | 905 | /* Decode the first argument: find the window or frame to use. */ |
88d4f6ec | 906 | if (EQ (position, Qt) |
2b06561a DL |
907 | || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar) |
908 | || EQ (XCAR (position), Qtool_bar)))) | |
165e1749 | 909 | { |
b14db4d7 | 910 | #if 0 /* Using the frame the mouse is on may not be right. */ |
99fe880d | 911 | /* Use the mouse's current position. */ |
b404828f | 912 | FRAME_PTR new_f = SELECTED_FRAME (); |
99fe880d RS |
913 | Lisp_Object bar_window; |
914 | int part; | |
915 | unsigned long time; | |
916 | Lisp_Object x, y; | |
165e1749 | 917 | |
46b657e2 | 918 | (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time); |
5ca2ef64 | 919 | |
99fe880d | 920 | if (new_f != 0) |
a39f0477 | 921 | XSETFRAME (window, new_f); |
99fe880d RS |
922 | else |
923 | window = selected_window; | |
b14db4d7 | 924 | #endif |
88d4f6ec | 925 | window = selected_window; |
99fe880d RS |
926 | } |
927 | else if (CONSP (position)) | |
928 | { | |
929 | Lisp_Object tem; | |
930 | tem = Fcar (position); | |
b5bb2705 | 931 | if (CONSP (tem)) |
99fe880d | 932 | window = Fcar (Fcdr (position)); |
80670155 RS |
933 | else |
934 | { | |
99fe880d RS |
935 | tem = Fcar (Fcdr (position)); /* EVENT_START (position) */ |
936 | window = Fcar (tem); /* POSN_WINDOW (tem) */ | |
165e1749 | 937 | } |
165e1749 | 938 | } |
99fe880d RS |
939 | else if (WINDOWP (position) || FRAMEP (position)) |
940 | window = position; | |
a9be6839 RS |
941 | else |
942 | window = Qnil; | |
165e1749 | 943 | |
99fe880d | 944 | /* Decode where to put the menu. */ |
165e1749 | 945 | |
b5bb2705 | 946 | if (FRAMEP (window)) |
99fe880d | 947 | f = XFRAME (window); |
b5bb2705 | 948 | else if (WINDOWP (window)) |
165e1749 | 949 | { |
99fe880d RS |
950 | CHECK_LIVE_WINDOW (window, 0); |
951 | f = XFRAME (WINDOW_FRAME (XWINDOW (window))); | |
165e1749 | 952 | } |
99fe880d RS |
953 | else |
954 | /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME, | |
955 | but I don't want to make one now. */ | |
956 | CHECK_WINDOW (window, 0); | |
165e1749 | 957 | |
99fe880d RS |
958 | #ifndef USE_X_TOOLKIT |
959 | /* Display a menu with these alternatives | |
960 | in the middle of frame F. */ | |
961 | { | |
962 | Lisp_Object x, y, frame, newpos; | |
a39f0477 KH |
963 | XSETFRAME (frame, f); |
964 | XSETINT (x, x_pixel_width (f) / 2); | |
965 | XSETINT (y, x_pixel_height (f) / 2); | |
99fe880d RS |
966 | newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil)); |
967 | ||
968 | return Fx_popup_menu (newpos, | |
969 | Fcons (Fcar (contents), Fcons (contents, Qnil))); | |
970 | } | |
971 | #else | |
972 | { | |
973 | Lisp_Object title; | |
974 | char *error_name; | |
975 | Lisp_Object selection; | |
165e1749 | 976 | |
99fe880d RS |
977 | /* Decode the dialog items from what was specified. */ |
978 | title = Fcar (contents); | |
979 | CHECK_STRING (title, 1); | |
165e1749 | 980 | |
99fe880d | 981 | list_of_panes (Fcons (contents, Qnil)); |
165e1749 | 982 | |
99fe880d RS |
983 | /* Display them in a dialog box. */ |
984 | BLOCK_INPUT; | |
f8c2630d | 985 | selection = xdialog_show (f, 0, title, &error_name); |
99fe880d | 986 | UNBLOCK_INPUT; |
165e1749 | 987 | |
99fe880d RS |
988 | discard_menu_items (); |
989 | ||
990 | if (error_name) error (error_name); | |
991 | return selection; | |
992 | } | |
7464b131 | 993 | #endif |
392d3f4b | 994 | } |
78589e07 RS |
995 | \f |
996 | #ifdef USE_X_TOOLKIT | |
18686d47 | 997 | |
4dedbfe0 | 998 | /* Loop in Xt until the menu pulldown or dialog popup has been |
f56e7ad2 RS |
999 | popped down (deactivated). This is used for x-popup-menu |
1000 | and x-popup-dialog; it is not used for the menu bar any more. | |
c98fcf4b | 1001 | |
2e2b8e22 | 1002 | NOTE: All calls to popup_get_selection should be protected |
c98fcf4b | 1003 | with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */ |
aa669def | 1004 | |
4dedbfe0 | 1005 | void |
2e2b8e22 | 1006 | popup_get_selection (initial_event, dpyinfo, id) |
4dedbfe0 | 1007 | XEvent *initial_event; |
aa669def | 1008 | struct x_display_info *dpyinfo; |
2e2b8e22 | 1009 | LWLIB_ID id; |
78589e07 | 1010 | { |
4dedbfe0 | 1011 | XEvent event; |
78589e07 | 1012 | |
aa669def RS |
1013 | /* Define a queue to save up for later unreading |
1014 | all X events that don't pertain to the menu. */ | |
1015 | struct event_queue | |
1016 | { | |
1017 | XEvent event; | |
1018 | struct event_queue *next; | |
1019 | }; | |
1020 | ||
1021 | struct event_queue *queue = NULL; | |
1022 | struct event_queue *queue_tmp; | |
1023 | ||
4dedbfe0 PR |
1024 | if (initial_event) |
1025 | event = *initial_event; | |
1026 | else | |
1027 | XtAppNextEvent (Xt_app_con, &event); | |
78589e07 | 1028 | |
4dedbfe0 | 1029 | while (1) |
78589e07 | 1030 | { |
aa669def RS |
1031 | /* Handle expose events for editor frames right away. */ |
1032 | if (event.type == Expose) | |
1033 | process_expose_from_menu (event); | |
10624005 KH |
1034 | /* Make sure we don't consider buttons grabbed after menu goes. |
1035 | And make sure to deactivate for any ButtonRelease, | |
1036 | even if XtDispatchEvent doesn't do that. */ | |
aa669def RS |
1037 | else if (event.type == ButtonRelease |
1038 | && dpyinfo->display == event.xbutton.display) | |
10624005 KH |
1039 | { |
1040 | dpyinfo->grabbed &= ~(1 << event.xbutton.button); | |
1041 | popup_activated_flag = 0; | |
c8b5aa3d RS |
1042 | #ifdef USE_MOTIF /* Pretending that the event came from a |
1043 | Btn1Down seems the only way to convince Motif to | |
1044 | activate its callbacks; setting the XmNmenuPost | |
1045 | isn't working. --marcus@sysc.pdx.edu. */ | |
1046 | event.xbutton.button = 1; | |
1047 | #endif | |
10624005 | 1048 | } |
2e2b8e22 KH |
1049 | /* If the user presses a key, deactivate the menu. |
1050 | The user is likely to do that if we get wedged. */ | |
1051 | else if (event.type == KeyPress | |
1052 | && dpyinfo->display == event.xbutton.display) | |
1053 | { | |
09155689 AS |
1054 | KeySym keysym = XLookupKeysym (&event.xkey, 0); |
1055 | if (!IsModifierKey (keysym)) | |
1056 | { | |
1057 | popup_activated_flag = 0; | |
1058 | break; | |
1059 | } | |
2e2b8e22 | 1060 | } |
d31d42cc RS |
1061 | /* Button presses outside the menu also pop it down. */ |
1062 | else if (event.type == ButtonPress | |
1063 | && event.xany.display == dpyinfo->display | |
1064 | && x_any_window_to_frame (dpyinfo, event.xany.window)) | |
1065 | { | |
1066 | popup_activated_flag = 0; | |
1067 | break; | |
1068 | } | |
aa669def RS |
1069 | |
1070 | /* Queue all events not for this popup, | |
10624005 | 1071 | except for Expose, which we've already handled, and ButtonRelease. |
9572375b KH |
1072 | Note that the X window is associated with the frame if this |
1073 | is a menu bar popup, but not if it's a dialog box. So we use | |
1074 | x_non_menubar_window_to_frame, not x_any_window_to_frame. */ | |
aa669def | 1075 | if (event.type != Expose |
10624005 KH |
1076 | && !(event.type == ButtonRelease |
1077 | && dpyinfo->display == event.xbutton.display) | |
aa669def | 1078 | && (event.xany.display != dpyinfo->display |
9572375b | 1079 | || x_non_menubar_window_to_frame (dpyinfo, event.xany.window))) |
aa669def | 1080 | { |
3e40ba92 GM |
1081 | queue_tmp = (struct event_queue *) xmalloc (sizeof *queue_tmp); |
1082 | queue_tmp->event = event; | |
1083 | queue_tmp->next = queue; | |
1084 | queue = queue_tmp; | |
aa669def | 1085 | } |
9572375b KH |
1086 | else |
1087 | XtDispatchEvent (&event); | |
aa669def RS |
1088 | |
1089 | if (!popup_activated ()) | |
4dedbfe0 PR |
1090 | break; |
1091 | XtAppNextEvent (Xt_app_con, &event); | |
78589e07 | 1092 | } |
aa669def RS |
1093 | |
1094 | /* Unread any events that we got but did not handle. */ | |
1095 | while (queue != NULL) | |
1096 | { | |
1097 | queue_tmp = queue; | |
1098 | XPutBackEvent (queue_tmp->event.xany.display, &queue_tmp->event); | |
1099 | queue = queue_tmp->next; | |
36cc2d38 | 1100 | xfree ((char *)queue_tmp); |
aa669def RS |
1101 | /* Cause these events to get read as soon as we UNBLOCK_INPUT. */ |
1102 | interrupt_input_pending = 1; | |
1103 | } | |
78589e07 RS |
1104 | } |
1105 | ||
88766961 RS |
1106 | /* Activate the menu bar of frame F. |
1107 | This is called from keyboard.c when it gets the | |
1108 | menu_bar_activate_event out of the Emacs event queue. | |
1109 | ||
1110 | To activate the menu bar, we use the X button-press event | |
ac78b144 | 1111 | that was saved in saved_menu_event. |
88766961 RS |
1112 | That makes the toolkit do its thing. |
1113 | ||
1114 | But first we recompute the menu bar contents (the whole tree). | |
1115 | ||
1116 | The reason for saving the button event until here, instead of | |
1117 | passing it to the toolkit right away, is that we can safely | |
1118 | execute Lisp code. */ | |
1119 | ||
dfcf069d | 1120 | void |
88766961 RS |
1121 | x_activate_menubar (f) |
1122 | FRAME_PTR f; | |
1123 | { | |
ac78b144 | 1124 | if (!f->output_data.x->saved_menu_event->type) |
88766961 RS |
1125 | return; |
1126 | ||
a9be6839 | 1127 | set_frame_menubar (f, 0, 1); |
88766961 | 1128 | BLOCK_INPUT; |
ac78b144 | 1129 | XtDispatchEvent ((XEvent *) f->output_data.x->saved_menu_event); |
88766961 | 1130 | UNBLOCK_INPUT; |
a9be6839 | 1131 | #ifdef USE_MOTIF |
745c34fb RS |
1132 | if (f->output_data.x->saved_menu_event->type == ButtonRelease) |
1133 | pending_menu_activation = 1; | |
a9be6839 | 1134 | #endif |
745c34fb | 1135 | |
88766961 | 1136 | /* Ignore this if we get it a second time. */ |
ac78b144 | 1137 | f->output_data.x->saved_menu_event->type = 0; |
88766961 RS |
1138 | } |
1139 | ||
c98fcf4b | 1140 | /* Detect if a dialog or menu has been posted. */ |
aa669def | 1141 | |
4dedbfe0 PR |
1142 | int |
1143 | popup_activated () | |
1144 | { | |
1145 | return popup_activated_flag; | |
1146 | } | |
1147 | ||
4dedbfe0 PR |
1148 | /* This callback is invoked when the user selects a menubar cascade |
1149 | pushbutton, but before the pulldown menu is posted. */ | |
78589e07 RS |
1150 | |
1151 | static void | |
4dedbfe0 | 1152 | popup_activate_callback (widget, id, client_data) |
78589e07 RS |
1153 | Widget widget; |
1154 | LWLIB_ID id; | |
1155 | XtPointer client_data; | |
1156 | { | |
7555d825 GM |
1157 | #ifdef USE_MOTIF |
1158 | ++popup_activated_flag; | |
1159 | #else | |
4dedbfe0 | 1160 | popup_activated_flag = 1; |
7555d825 GM |
1161 | #endif |
1162 | } | |
1163 | ||
1164 | /* This callback is invoked when a dialog or menu is finished being | |
1165 | used and has been unposted. */ | |
1166 | ||
1167 | static void | |
1168 | popup_deactivate_callback (widget, id, client_data) | |
1169 | Widget widget; | |
1170 | LWLIB_ID id; | |
1171 | XtPointer client_data; | |
1172 | { | |
1173 | #ifdef USE_MOTIF | |
1174 | --popup_activated_flag; | |
1175 | #else | |
1176 | popup_activated_flag = 0; | |
1177 | #endif | |
78589e07 RS |
1178 | } |
1179 | ||
850df50b GM |
1180 | /* Lwlib callback called when menu items are highlighted/unhighlighted |
1181 | while moving the mouse over them. WIDGET is the menu bar or menu | |
1182 | popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to | |
1183 | the widget_value structure for the menu item, or null in case of | |
1184 | unhighlighting. */ | |
1185 | ||
1186 | void | |
1187 | menu_highlight_callback (widget, id, call_data) | |
1188 | Widget widget; | |
1189 | LWLIB_ID id; | |
1190 | void *call_data; | |
1191 | { | |
1192 | widget_value *wv = (widget_value *) call_data; | |
1193 | struct frame *f; | |
1194 | Lisp_Object frame, help; | |
850df50b | 1195 | |
9cd50434 GM |
1196 | help = wv && wv->help ? build_string (wv->help) : Qnil; |
1197 | ||
850df50b GM |
1198 | /* Determine the frame for the help event. */ |
1199 | f = menubar_id_to_frame (id); | |
1200 | if (f) | |
9cd50434 GM |
1201 | { |
1202 | XSETFRAME (frame, f); | |
1203 | kbd_buffer_store_help_event (frame, help); | |
1204 | } | |
850df50b GM |
1205 | else |
1206 | { | |
1207 | /* WIDGET is the popup menu. It's parent is the frame's | |
1208 | widget. See which frame that is. */ | |
1209 | Widget frame_widget = XtParent (widget); | |
1210 | Lisp_Object tail; | |
1211 | ||
1212 | for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail)) | |
1213 | { | |
1214 | frame = XCAR (tail); | |
1215 | if (GC_FRAMEP (frame) | |
1216 | && (f = XFRAME (frame), | |
1217 | FRAME_X_P (f) && f->output_data.x->widget == frame_widget)) | |
1218 | break; | |
1219 | } | |
850df50b | 1220 | |
9cd50434 GM |
1221 | show_help_echo (help, Qnil, Qnil, Qnil, 1); |
1222 | } | |
850df50b GM |
1223 | } |
1224 | ||
4dedbfe0 PR |
1225 | /* This callback is called from the menu bar pulldown menu |
1226 | when the user makes a selection. | |
1227 | Figure out what the user chose | |
1228 | and put the appropriate events into the keyboard buffer. */ | |
1229 | ||
78589e07 | 1230 | static void |
4dedbfe0 | 1231 | menubar_selection_callback (widget, id, client_data) |
78589e07 RS |
1232 | Widget widget; |
1233 | LWLIB_ID id; | |
1234 | XtPointer client_data; | |
1235 | { | |
c63f6952 | 1236 | Lisp_Object prefix, entry; |
88766961 | 1237 | FRAME_PTR f = menubar_id_to_frame (id); |
4dedbfe0 PR |
1238 | Lisp_Object vector; |
1239 | Lisp_Object *subprefix_stack; | |
1240 | int submenu_depth = 0; | |
1241 | int i; | |
1242 | ||
1243 | if (!f) | |
1244 | return; | |
6bbd7a29 | 1245 | entry = Qnil; |
4dedbfe0 PR |
1246 | subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object)); |
1247 | vector = f->menu_bar_vector; | |
1248 | prefix = Qnil; | |
1249 | i = 0; | |
1250 | while (i < f->menu_bar_items_used) | |
1251 | { | |
4dedbfe0 PR |
1252 | if (EQ (XVECTOR (vector)->contents[i], Qnil)) |
1253 | { | |
1254 | subprefix_stack[submenu_depth++] = prefix; | |
1255 | prefix = entry; | |
1256 | i++; | |
1257 | } | |
1258 | else if (EQ (XVECTOR (vector)->contents[i], Qlambda)) | |
1259 | { | |
1260 | prefix = subprefix_stack[--submenu_depth]; | |
1261 | i++; | |
1262 | } | |
1263 | else if (EQ (XVECTOR (vector)->contents[i], Qt)) | |
1264 | { | |
4cb35c39 | 1265 | prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX]; |
4dedbfe0 PR |
1266 | i += MENU_ITEMS_PANE_LENGTH; |
1267 | } | |
1268 | else | |
1269 | { | |
4cb35c39 | 1270 | entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE]; |
01d5e892 RS |
1271 | /* The EMACS_INT cast avoids a warning. There's no problem |
1272 | as long as pointers have enough bits to hold small integers. */ | |
1273 | if ((int) (EMACS_INT) client_data == i) | |
4dedbfe0 PR |
1274 | { |
1275 | int j; | |
1276 | struct input_event buf; | |
4cb35c39 | 1277 | Lisp_Object frame; |
4dedbfe0 | 1278 | |
4cb35c39 | 1279 | XSETFRAME (frame, f); |
9979315d GM |
1280 | buf.kind = MENU_BAR_EVENT; |
1281 | buf.frame_or_window = frame; | |
1282 | buf.arg = frame; | |
4dedbfe0 PR |
1283 | kbd_buffer_store_event (&buf); |
1284 | ||
1285 | for (j = 0; j < submenu_depth; j++) | |
1286 | if (!NILP (subprefix_stack[j])) | |
1287 | { | |
9979315d GM |
1288 | buf.kind = MENU_BAR_EVENT; |
1289 | buf.frame_or_window = frame; | |
1290 | buf.arg = subprefix_stack[j]; | |
4dedbfe0 PR |
1291 | kbd_buffer_store_event (&buf); |
1292 | } | |
1293 | ||
1294 | if (!NILP (prefix)) | |
1295 | { | |
9979315d GM |
1296 | buf.kind = MENU_BAR_EVENT; |
1297 | buf.frame_or_window = frame; | |
1298 | buf.arg = prefix; | |
4dedbfe0 PR |
1299 | kbd_buffer_store_event (&buf); |
1300 | } | |
1301 | ||
9979315d GM |
1302 | buf.kind = MENU_BAR_EVENT; |
1303 | buf.frame_or_window = frame; | |
1304 | buf.arg = entry; | |
4dedbfe0 PR |
1305 | kbd_buffer_store_event (&buf); |
1306 | ||
1307 | return; | |
1308 | } | |
1309 | i += MENU_ITEMS_ITEM_LENGTH; | |
1310 | } | |
1311 | } | |
18686d47 RS |
1312 | } |
1313 | ||
f9655c60 RS |
1314 | /* Allocate a widget_value, blocking input. */ |
1315 | ||
1316 | widget_value * | |
1317 | xmalloc_widget_value () | |
1318 | { | |
1319 | widget_value *value; | |
1320 | ||
1321 | BLOCK_INPUT; | |
1322 | value = malloc_widget_value (); | |
1323 | UNBLOCK_INPUT; | |
1324 | ||
1325 | return value; | |
1326 | } | |
4dedbfe0 PR |
1327 | |
1328 | /* This recursively calls free_widget_value on the tree of widgets. | |
18686d47 | 1329 | It must free all data that was malloc'ed for these widget_values. |
78589e07 RS |
1330 | In Emacs, many slots are pointers into the data of Lisp_Strings, and |
1331 | must be left alone. */ | |
1332 | ||
18686d47 RS |
1333 | void |
1334 | free_menubar_widget_value_tree (wv) | |
1335 | widget_value *wv; | |
1336 | { | |
1337 | if (! wv) return; | |
18686d47 RS |
1338 | |
1339 | wv->name = wv->value = wv->key = (char *) 0xDEADBEEF; | |
1340 | ||
1341 | if (wv->contents && (wv->contents != (widget_value*)1)) | |
1342 | { | |
1343 | free_menubar_widget_value_tree (wv->contents); | |
1344 | wv->contents = (widget_value *) 0xDEADBEEF; | |
1345 | } | |
1346 | if (wv->next) | |
1347 | { | |
1348 | free_menubar_widget_value_tree (wv->next); | |
1349 | wv->next = (widget_value *) 0xDEADBEEF; | |
1350 | } | |
1351 | BLOCK_INPUT; | |
1352 | free_widget_value (wv); | |
1353 | UNBLOCK_INPUT; | |
1354 | } | |
4dedbfe0 PR |
1355 | \f |
1356 | /* Return a tree of widget_value structures for a menu bar item | |
1357 | whose event type is ITEM_KEY (with string ITEM_NAME) | |
1358 | and whose contents come from the list of keymaps MAPS. */ | |
1359 | ||
1360 | static widget_value * | |
1361 | single_submenu (item_key, item_name, maps) | |
1362 | Lisp_Object item_key, item_name, maps; | |
1363 | { | |
1364 | widget_value *wv, *prev_wv, *save_wv, *first_wv; | |
1365 | int i; | |
1366 | int submenu_depth = 0; | |
1367 | Lisp_Object length; | |
1368 | int len; | |
1369 | Lisp_Object *mapvec; | |
1370 | widget_value **submenu_stack; | |
4dedbfe0 | 1371 | int previous_items = menu_items_used; |
71dca3e3 | 1372 | int top_level_items = 0; |
4dedbfe0 PR |
1373 | |
1374 | length = Flength (maps); | |
1375 | len = XINT (length); | |
1376 | ||
1377 | /* Convert the list MAPS into a vector MAPVEC. */ | |
1378 | mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object)); | |
1379 | for (i = 0; i < len; i++) | |
1380 | { | |
1381 | mapvec[i] = Fcar (maps); | |
1382 | maps = Fcdr (maps); | |
1383 | } | |
1384 | ||
1385 | menu_items_n_panes = 0; | |
1386 | ||
1387 | /* Loop over the given keymaps, making a pane for each map. | |
1388 | But don't make a pane that is empty--ignore that map instead. */ | |
1389 | for (i = 0; i < len; i++) | |
71dca3e3 | 1390 | { |
846e8c10 | 1391 | if (SYMBOLP (mapvec[i]) |
02067692 | 1392 | || (CONSP (mapvec[i]) && !KEYMAPP (mapvec[i]))) |
71dca3e3 | 1393 | { |
846e8c10 RS |
1394 | /* Here we have a command at top level in the menu bar |
1395 | as opposed to a submenu. */ | |
71dca3e3 RS |
1396 | top_level_items = 1; |
1397 | push_menu_pane (Qnil, Qnil); | |
850df50b GM |
1398 | push_menu_item (item_name, Qt, item_key, mapvec[i], |
1399 | Qnil, Qnil, Qnil, Qnil); | |
71dca3e3 RS |
1400 | } |
1401 | else | |
98381e42 | 1402 | single_keymap_panes (mapvec[i], item_name, item_key, 0, 10); |
71dca3e3 | 1403 | } |
4dedbfe0 PR |
1404 | |
1405 | /* Create a tree of widget_value objects | |
1406 | representing the panes and their items. */ | |
1407 | ||
1408 | submenu_stack | |
1409 | = (widget_value **) alloca (menu_items_used * sizeof (widget_value *)); | |
f7fab165 | 1410 | wv = xmalloc_widget_value (); |
4dedbfe0 PR |
1411 | wv->name = "menu"; |
1412 | wv->value = 0; | |
1413 | wv->enabled = 1; | |
3427a3db | 1414 | wv->button_type = BUTTON_TYPE_NONE; |
4dedbfe0 PR |
1415 | first_wv = wv; |
1416 | save_wv = 0; | |
71dca3e3 | 1417 | prev_wv = 0; |
4dedbfe0 PR |
1418 | |
1419 | /* Loop over all panes and items made during this call | |
1420 | and construct a tree of widget_value objects. | |
1421 | Ignore the panes and items made by previous calls to | |
1422 | single_submenu, even though those are also in menu_items. */ | |
1423 | i = previous_items; | |
1424 | while (i < menu_items_used) | |
1425 | { | |
1426 | if (EQ (XVECTOR (menu_items)->contents[i], Qnil)) | |
1427 | { | |
1428 | submenu_stack[submenu_depth++] = save_wv; | |
1429 | save_wv = prev_wv; | |
1430 | prev_wv = 0; | |
1431 | i++; | |
1432 | } | |
1433 | else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda)) | |
1434 | { | |
1435 | prev_wv = save_wv; | |
1436 | save_wv = submenu_stack[--submenu_depth]; | |
1437 | i++; | |
1438 | } | |
1439 | else if (EQ (XVECTOR (menu_items)->contents[i], Qt) | |
1440 | && submenu_depth != 0) | |
1441 | i += MENU_ITEMS_PANE_LENGTH; | |
1442 | /* Ignore a nil in the item list. | |
1443 | It's meaningful only for dialog boxes. */ | |
1444 | else if (EQ (XVECTOR (menu_items)->contents[i], Qquote)) | |
1445 | i += 1; | |
1446 | else if (EQ (XVECTOR (menu_items)->contents[i], Qt)) | |
1447 | { | |
1448 | /* Create a new pane. */ | |
1449 | Lisp_Object pane_name, prefix; | |
1450 | char *pane_string; | |
1451 | pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME]; | |
1452 | prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX]; | |
703dc2a8 KH |
1453 | #ifndef HAVE_MULTILINGUAL_MENU |
1454 | if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name)) | |
1455 | pane_name = string_make_unibyte (pane_name); | |
1456 | #endif | |
4dedbfe0 PR |
1457 | pane_string = (NILP (pane_name) |
1458 | ? "" : (char *) XSTRING (pane_name)->data); | |
1459 | /* If there is just one top-level pane, put all its items directly | |
1460 | under the top-level menu. */ | |
1461 | if (menu_items_n_panes == 1) | |
1462 | pane_string = ""; | |
1463 | ||
1464 | /* If the pane has a meaningful name, | |
1465 | make the pane a top-level menu item | |
1466 | with its items as a submenu beneath it. */ | |
1467 | if (strcmp (pane_string, "")) | |
1468 | { | |
f7fab165 | 1469 | wv = xmalloc_widget_value (); |
4dedbfe0 PR |
1470 | if (save_wv) |
1471 | save_wv->next = wv; | |
1472 | else | |
1473 | first_wv->contents = wv; | |
1474 | wv->name = pane_string; | |
ffcb5a51 RS |
1475 | /* Ignore the @ that means "separate pane". |
1476 | This is a kludge, but this isn't worth more time. */ | |
1477 | if (!NILP (prefix) && wv->name[0] == '@') | |
4dedbfe0 PR |
1478 | wv->name++; |
1479 | wv->value = 0; | |
1480 | wv->enabled = 1; | |
3427a3db | 1481 | wv->button_type = BUTTON_TYPE_NONE; |
4dedbfe0 PR |
1482 | } |
1483 | save_wv = wv; | |
1484 | prev_wv = 0; | |
1485 | i += MENU_ITEMS_PANE_LENGTH; | |
1486 | } | |
1487 | else | |
1488 | { | |
1489 | /* Create a new item within current pane. */ | |
3427a3db | 1490 | Lisp_Object item_name, enable, descrip, def, type, selected; |
850df50b GM |
1491 | Lisp_Object help; |
1492 | ||
4dedbfe0 PR |
1493 | item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME]; |
1494 | enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE]; | |
1495 | descrip | |
1496 | = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY]; | |
a352a815 | 1497 | def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION]; |
3427a3db GM |
1498 | type = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_TYPE]; |
1499 | selected = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_SELECTED]; | |
850df50b | 1500 | help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP]; |
3427a3db | 1501 | |
703dc2a8 | 1502 | #ifndef HAVE_MULTILINGUAL_MENU |
3427a3db GM |
1503 | if (STRING_MULTIBYTE (item_name)) |
1504 | item_name = string_make_unibyte (item_name); | |
1505 | if (STRINGP (descrip) && STRING_MULTIBYTE (descrip)) | |
1506 | descrip = string_make_unibyte (descrip); | |
703dc2a8 | 1507 | #endif |
3427a3db | 1508 | |
f7fab165 | 1509 | wv = xmalloc_widget_value (); |
4dedbfe0 PR |
1510 | if (prev_wv) |
1511 | prev_wv->next = wv; | |
71dca3e3 | 1512 | else |
4dedbfe0 | 1513 | save_wv->contents = wv; |
71dca3e3 | 1514 | |
4dedbfe0 PR |
1515 | wv->name = (char *) XSTRING (item_name)->data; |
1516 | if (!NILP (descrip)) | |
1517 | wv->key = (char *) XSTRING (descrip)->data; | |
1518 | wv->value = 0; | |
01d5e892 RS |
1519 | /* The EMACS_INT cast avoids a warning. There's no problem |
1520 | as long as pointers have enough bits to hold small integers. */ | |
1521 | wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0); | |
4dedbfe0 | 1522 | wv->enabled = !NILP (enable); |
3427a3db GM |
1523 | |
1524 | if (NILP (type)) | |
1525 | wv->button_type = BUTTON_TYPE_NONE; | |
1526 | else if (EQ (type, QCradio)) | |
1527 | wv->button_type = BUTTON_TYPE_RADIO; | |
1528 | else if (EQ (type, QCtoggle)) | |
1529 | wv->button_type = BUTTON_TYPE_TOGGLE; | |
1530 | else | |
1531 | abort (); | |
1532 | ||
1533 | wv->selected = !NILP (selected); | |
850df50b GM |
1534 | if (STRINGP (help)) |
1535 | wv->help = XSTRING (help)->data; | |
3427a3db | 1536 | |
4dedbfe0 PR |
1537 | prev_wv = wv; |
1538 | ||
1539 | i += MENU_ITEMS_ITEM_LENGTH; | |
1540 | } | |
1541 | } | |
1542 | ||
71dca3e3 RS |
1543 | /* If we have just one "menu item" |
1544 | that was originally a button, return it by itself. */ | |
1545 | if (top_level_items && first_wv->contents && first_wv->contents->next == 0) | |
1546 | { | |
1547 | wv = first_wv->contents; | |
1548 | free_widget_value (first_wv); | |
1549 | return wv; | |
1550 | } | |
1551 | ||
4dedbfe0 PR |
1552 | return first_wv; |
1553 | } | |
1554 | \f | |
cffa74ea FP |
1555 | extern void EmacsFrameSetCharSize (); |
1556 | ||
4bcdbab1 KH |
1557 | /* Recompute all the widgets of frame F, when the menu bar |
1558 | has been changed. */ | |
4dedbfe0 | 1559 | |
18686d47 | 1560 | static void |
6af6cbb5 | 1561 | update_frame_menubar (f) |
18686d47 RS |
1562 | FRAME_PTR f; |
1563 | { | |
7556890b | 1564 | struct x_output *x = f->output_data.x; |
cffa74ea | 1565 | int columns, rows; |
18686d47 RS |
1566 | int menubar_changed; |
1567 | ||
4dedbfe0 PR |
1568 | /* We assume the menubar contents has changed if the global flag is set, |
1569 | or if the current buffer has changed, or if the menubar has never | |
1570 | been updated before. | |
1571 | */ | |
18686d47 RS |
1572 | menubar_changed = (x->menubar_widget |
1573 | && !XtIsManaged (x->menubar_widget)); | |
1574 | ||
1575 | if (! (menubar_changed)) | |
1576 | return; | |
1577 | ||
1578 | BLOCK_INPUT; | |
cffa74ea FP |
1579 | /* Save the size of the frame because the pane widget doesn't accept to |
1580 | resize itself. So force it. */ | |
1581 | columns = f->width; | |
1582 | rows = f->height; | |
1583 | ||
4dedbfe0 PR |
1584 | /* Do the voodoo which means "I'm changing lots of things, don't try to |
1585 | refigure sizes until I'm done." */ | |
1586 | lw_refigure_widget (x->column_widget, False); | |
cffa74ea | 1587 | |
18686d47 RS |
1588 | /* the order in which children are managed is the top to |
1589 | bottom order in which they are displayed in the paned window. | |
1590 | First, remove the text-area widget. | |
1591 | */ | |
1592 | XtUnmanageChild (x->edit_widget); | |
1593 | ||
1594 | /* remove the menubar that is there now, and put up the menubar that | |
1595 | should be there. | |
1596 | */ | |
1597 | if (menubar_changed) | |
1598 | { | |
1599 | XtManageChild (x->menubar_widget); | |
1600 | XtMapWidget (x->menubar_widget); | |
6a2bfc84 | 1601 | XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, NULL); |
18686d47 RS |
1602 | } |
1603 | ||
c98fcf4b | 1604 | /* Re-manage the text-area widget, and then thrash the sizes. */ |
18686d47 | 1605 | XtManageChild (x->edit_widget); |
bf82bce4 | 1606 | x_set_menu_resources_from_menu_face (f, x->menubar_widget); |
4dedbfe0 | 1607 | lw_refigure_widget (x->column_widget, True); |
cffa74ea FP |
1608 | |
1609 | /* Force the pane widget to resize itself with the right values. */ | |
1610 | EmacsFrameSetCharSize (x->edit_widget, columns, rows); | |
18686d47 RS |
1611 | UNBLOCK_INPUT; |
1612 | } | |
1613 | ||
4bcdbab1 KH |
1614 | /* Set the contents of the menubar widgets of frame F. |
1615 | The argument FIRST_TIME is currently ignored; | |
1616 | it is set the first time this is called, from initialize_frame_menubar. */ | |
1617 | ||
18686d47 | 1618 | void |
88766961 | 1619 | set_frame_menubar (f, first_time, deep_p) |
18686d47 | 1620 | FRAME_PTR f; |
706aa2f2 | 1621 | int first_time; |
88766961 | 1622 | int deep_p; |
18686d47 | 1623 | { |
7556890b | 1624 | Widget menubar_widget = f->output_data.x->menubar_widget; |
faa935b6 | 1625 | Lisp_Object items; |
4d19cb8e | 1626 | widget_value *wv, *first_wv, *prev_wv = 0; |
5b3557df | 1627 | int i; |
88766961 | 1628 | LWLIB_ID id; |
18686d47 | 1629 | |
bfc524bc RS |
1630 | XSETFRAME (Vmenu_updating_frame, f); |
1631 | ||
7556890b RS |
1632 | if (f->output_data.x->id == 0) |
1633 | f->output_data.x->id = next_menubar_widget_id++; | |
1634 | id = f->output_data.x->id; | |
37a98547 | 1635 | |
88766961 RS |
1636 | if (! menubar_widget) |
1637 | deep_p = 1; | |
745c34fb | 1638 | else if (pending_menu_activation && !deep_p) |
a9be6839 RS |
1639 | deep_p = 1; |
1640 | /* Make the first call for any given frame always go deep. */ | |
1641 | else if (!f->output_data.x->saved_menu_event && !deep_p) | |
745c34fb RS |
1642 | { |
1643 | deep_p = 1; | |
a9be6839 RS |
1644 | f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent)); |
1645 | f->output_data.x->saved_menu_event->type = 0; | |
745c34fb | 1646 | } |
18686d47 | 1647 | |
f7fab165 | 1648 | wv = xmalloc_widget_value (); |
18686d47 RS |
1649 | wv->name = "menubar"; |
1650 | wv->value = 0; | |
1651 | wv->enabled = 1; | |
3427a3db | 1652 | wv->button_type = BUTTON_TYPE_NONE; |
4d19cb8e | 1653 | first_wv = wv; |
8d8a3494 | 1654 | |
88766961 | 1655 | if (deep_p) |
18686d47 | 1656 | { |
88766961 RS |
1657 | /* Make a widget-value tree representing the entire menu trees. */ |
1658 | ||
1659 | struct buffer *prev = current_buffer; | |
1660 | Lisp_Object buffer; | |
1661 | int specpdl_count = specpdl_ptr - specpdl; | |
1662 | int previous_menu_items_used = f->menu_bar_items_used; | |
1663 | Lisp_Object *previous_items | |
1664 | = (Lisp_Object *) alloca (previous_menu_items_used | |
1665 | * sizeof (Lisp_Object)); | |
1666 | ||
0b1cf399 RS |
1667 | /* If we are making a new widget, its contents are empty, |
1668 | do always reinitialize them. */ | |
1669 | if (! menubar_widget) | |
1670 | previous_menu_items_used = 0; | |
1671 | ||
88766961 RS |
1672 | buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer; |
1673 | specbind (Qinhibit_quit, Qt); | |
1674 | /* Don't let the debugger step into this code | |
1675 | because it is not reentrant. */ | |
1676 | specbind (Qdebug_on_next_call, Qnil); | |
1677 | ||
58226364 | 1678 | record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil)); |
88766961 RS |
1679 | if (NILP (Voverriding_local_map_menu_flag)) |
1680 | { | |
1681 | specbind (Qoverriding_terminal_local_map, Qnil); | |
1682 | specbind (Qoverriding_local_map, Qnil); | |
1683 | } | |
18686d47 | 1684 | |
88766961 | 1685 | set_buffer_internal_1 (XBUFFER (buffer)); |
18686d47 | 1686 | |
88766961 | 1687 | /* Run the Lucid hook. */ |
950eaee7 GM |
1688 | safe_run_hooks (Qactivate_menubar_hook); |
1689 | ||
88766961 RS |
1690 | /* If it has changed current-menubar from previous value, |
1691 | really recompute the menubar from the value. */ | |
1692 | if (! NILP (Vlucid_menu_bar_dirty_flag)) | |
1693 | call0 (Qrecompute_lucid_menubar); | |
a57634d4 | 1694 | safe_run_hooks (Qmenu_bar_update_hook); |
88766961 | 1695 | FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f)); |
18686d47 | 1696 | |
88766961 | 1697 | items = FRAME_MENU_BAR_ITEMS (f); |
8d8a3494 | 1698 | |
88766961 | 1699 | inhibit_garbage_collection (); |
8d8a3494 | 1700 | |
88766961 RS |
1701 | /* Save the frame's previous menu bar contents data. */ |
1702 | bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items, | |
1703 | previous_menu_items_used * sizeof (Lisp_Object)); | |
8d8a3494 | 1704 | |
88766961 RS |
1705 | /* Fill in the current menu bar contents. */ |
1706 | menu_items = f->menu_bar_vector; | |
1707 | menu_items_allocated = XVECTOR (menu_items)->size; | |
1708 | init_menu_items (); | |
f04366cb | 1709 | for (i = 0; i < XVECTOR (items)->size; i += 4) |
88766961 RS |
1710 | { |
1711 | Lisp_Object key, string, maps; | |
1712 | ||
1713 | key = XVECTOR (items)->contents[i]; | |
1714 | string = XVECTOR (items)->contents[i + 1]; | |
1715 | maps = XVECTOR (items)->contents[i + 2]; | |
1716 | if (NILP (string)) | |
1717 | break; | |
1718 | ||
1719 | wv = single_submenu (key, string, maps); | |
1720 | if (prev_wv) | |
1721 | prev_wv->next = wv; | |
1722 | else | |
1723 | first_wv->contents = wv; | |
1724 | /* Don't set wv->name here; GC during the loop might relocate it. */ | |
1725 | wv->enabled = 1; | |
3427a3db | 1726 | wv->button_type = BUTTON_TYPE_NONE; |
88766961 RS |
1727 | prev_wv = wv; |
1728 | } | |
1729 | ||
1730 | finish_menu_items (); | |
1731 | ||
1732 | set_buffer_internal_1 (prev); | |
8d8a3494 | 1733 | unbind_to (specpdl_count, Qnil); |
8d8a3494 | 1734 | |
88766961 RS |
1735 | /* If there has been no change in the Lisp-level contents |
1736 | of the menu bar, skip redisplaying it. Just exit. */ | |
1737 | ||
1738 | for (i = 0; i < previous_menu_items_used; i++) | |
1739 | if (menu_items_used == i | |
99d3fac7 | 1740 | || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i]))) |
88766961 | 1741 | break; |
62555c22 | 1742 | if (i == menu_items_used && i == previous_menu_items_used && i != 0) |
88766961 RS |
1743 | { |
1744 | free_menubar_widget_value_tree (first_wv); | |
1745 | menu_items = Qnil; | |
1746 | ||
1747 | return; | |
1748 | } | |
1749 | ||
1750 | /* Now GC cannot happen during the lifetime of the widget_value, | |
1751 | so it's safe to store data from a Lisp_String. */ | |
1752 | wv = first_wv->contents; | |
f04366cb | 1753 | for (i = 0; i < XVECTOR (items)->size; i += 4) |
88766961 RS |
1754 | { |
1755 | Lisp_Object string; | |
1756 | string = XVECTOR (items)->contents[i + 1]; | |
1757 | if (NILP (string)) | |
1758 | break; | |
1759 | wv->name = (char *) XSTRING (string)->data; | |
1760 | wv = wv->next; | |
1761 | } | |
1762 | ||
1763 | f->menu_bar_vector = menu_items; | |
1764 | f->menu_bar_items_used = menu_items_used; | |
1765 | menu_items = Qnil; | |
4d19cb8e | 1766 | } |
88766961 RS |
1767 | else |
1768 | { | |
1769 | /* Make a widget-value tree containing | |
1770 | just the top level menu bar strings. */ | |
4d19cb8e | 1771 | |
88766961 | 1772 | items = FRAME_MENU_BAR_ITEMS (f); |
f04366cb | 1773 | for (i = 0; i < XVECTOR (items)->size; i += 4) |
88766961 RS |
1774 | { |
1775 | Lisp_Object string; | |
1776 | ||
1777 | string = XVECTOR (items)->contents[i + 1]; | |
1778 | if (NILP (string)) | |
1779 | break; | |
1780 | ||
f7fab165 | 1781 | wv = xmalloc_widget_value (); |
88766961 RS |
1782 | wv->name = (char *) XSTRING (string)->data; |
1783 | wv->value = 0; | |
1784 | wv->enabled = 1; | |
3427a3db | 1785 | wv->button_type = BUTTON_TYPE_NONE; |
fe8fa62f RS |
1786 | /* This prevents lwlib from assuming this |
1787 | menu item is really supposed to be empty. */ | |
1788 | /* The EMACS_INT cast avoids a warning. | |
1789 | This value just has to be different from small integers. */ | |
1790 | wv->call_data = (void *) (EMACS_INT) (-1); | |
88766961 RS |
1791 | |
1792 | if (prev_wv) | |
1793 | prev_wv->next = wv; | |
1794 | else | |
1795 | first_wv->contents = wv; | |
1796 | prev_wv = wv; | |
1797 | } | |
62555c22 RS |
1798 | |
1799 | /* Forget what we thought we knew about what is in the | |
1800 | detailed contents of the menu bar menus. | |
1801 | Changing the top level always destroys the contents. */ | |
1802 | f->menu_bar_items_used = 0; | |
88766961 | 1803 | } |
4dedbfe0 | 1804 | |
88766961 | 1805 | /* Create or update the menu bar widget. */ |
aa669def RS |
1806 | |
1807 | BLOCK_INPUT; | |
1808 | ||
18686d47 | 1809 | if (menubar_widget) |
4dedbfe0 PR |
1810 | { |
1811 | /* Disable resizing (done for Motif!) */ | |
7556890b | 1812 | lw_allow_resizing (f->output_data.x->widget, False); |
4dedbfe0 PR |
1813 | |
1814 | /* The third arg is DEEP_P, which says to consider the entire | |
1815 | menu trees we supply, rather than just the menu bar item names. */ | |
88766961 | 1816 | lw_modify_all_widgets (id, first_wv, deep_p); |
4dedbfe0 | 1817 | |
c98fcf4b | 1818 | /* Re-enable the edit widget to resize. */ |
7556890b | 1819 | lw_allow_resizing (f->output_data.x->widget, True); |
4dedbfe0 | 1820 | } |
18686d47 RS |
1821 | else |
1822 | { | |
88766961 | 1823 | menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv, |
7556890b | 1824 | f->output_data.x->column_widget, |
4dedbfe0 PR |
1825 | 0, |
1826 | popup_activate_callback, | |
1827 | menubar_selection_callback, | |
850df50b GM |
1828 | popup_deactivate_callback, |
1829 | menu_highlight_callback); | |
7556890b | 1830 | f->output_data.x->menubar_widget = menubar_widget; |
18686d47 | 1831 | } |
1d1c1567 KH |
1832 | |
1833 | { | |
1834 | int menubar_size | |
7556890b RS |
1835 | = (f->output_data.x->menubar_widget |
1836 | ? (f->output_data.x->menubar_widget->core.height | |
1837 | + f->output_data.x->menubar_widget->core.border_width) | |
1d1c1567 KH |
1838 | : 0); |
1839 | ||
f42aa681 RS |
1840 | #if 0 /* Experimentally, we now get the right results |
1841 | for -geometry -0-0 without this. 24 Aug 96, rms. */ | |
5ee80bd3 | 1842 | #ifdef USE_LUCID |
1d1c1567 KH |
1843 | if (FRAME_EXTERNAL_MENU_BAR (f)) |
1844 | { | |
1845 | Dimension ibw = 0; | |
7556890b | 1846 | XtVaGetValues (f->output_data.x->column_widget, |
1d1c1567 KH |
1847 | XtNinternalBorderWidth, &ibw, NULL); |
1848 | menubar_size += ibw; | |
1849 | } | |
5ee80bd3 | 1850 | #endif /* USE_LUCID */ |
f42aa681 | 1851 | #endif /* 0 */ |
1d1c1567 | 1852 | |
7556890b | 1853 | f->output_data.x->menubar_height = menubar_size; |
1d1c1567 | 1854 | } |
18686d47 RS |
1855 | |
1856 | free_menubar_widget_value_tree (first_wv); | |
4bcdbab1 | 1857 | update_frame_menubar (f); |
18686d47 RS |
1858 | |
1859 | UNBLOCK_INPUT; | |
1860 | } | |
85f487d1 | 1861 | |
8e6208c5 | 1862 | /* Called from Fx_create_frame to create the initial menubar of a frame |
4dedbfe0 PR |
1863 | before it is mapped, so that the window is mapped with the menubar already |
1864 | there instead of us tacking it on later and thrashing the window after it | |
1865 | is visible. */ | |
1866 | ||
1867 | void | |
1868 | initialize_frame_menubar (f) | |
1869 | FRAME_PTR f; | |
1870 | { | |
1871 | /* This function is called before the first chance to redisplay | |
1872 | the frame. It has to be, so the frame will have the right size. */ | |
1873 | FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f)); | |
88766961 | 1874 | set_frame_menubar (f, 1, 1); |
4dedbfe0 PR |
1875 | } |
1876 | ||
1877 | /* Get rid of the menu bar of frame F, and free its storage. | |
1878 | This is used when deleting a frame, and when turning off the menu bar. */ | |
1879 | ||
85f487d1 FP |
1880 | void |
1881 | free_frame_menubar (f) | |
1882 | FRAME_PTR f; | |
1883 | { | |
1884 | Widget menubar_widget; | |
85f487d1 | 1885 | |
7556890b | 1886 | menubar_widget = f->output_data.x->menubar_widget; |
a45bad2a RS |
1887 | |
1888 | f->output_data.x->menubar_height = 0; | |
85f487d1 FP |
1889 | |
1890 | if (menubar_widget) | |
1891 | { | |
1892 | BLOCK_INPUT; | |
7556890b | 1893 | lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id); |
0c05dcd3 | 1894 | f->output_data.x->menubar_widget = NULL; |
85f487d1 FP |
1895 | UNBLOCK_INPUT; |
1896 | } | |
1897 | } | |
78589e07 | 1898 | |
78589e07 RS |
1899 | #endif /* USE_X_TOOLKIT */ |
1900 | \f | |
1901 | /* xmenu_show actually displays a menu using the panes and items in menu_items | |
1902 | and returns the value selected from it. | |
1903 | There are two versions of xmenu_show, one for Xt and one for Xlib. | |
1904 | Both assume input is blocked by the caller. */ | |
1905 | ||
1906 | /* F is the frame the menu is for. | |
1907 | X and Y are the frame-relative specified position, | |
1908 | relative to the inside upper left corner of the frame F. | |
c8b5aa3d | 1909 | FOR_CLICK is nonzero if this menu was invoked for a mouse click. |
78589e07 RS |
1910 | KEYMAPS is 1 if this menu was specified with keymaps; |
1911 | in that case, we return a list containing the chosen item's value | |
1912 | and perhaps also the pane's prefix. | |
1913 | TITLE is the specified menu title. | |
1914 | ERROR is a place to store an error message string in case of failure. | |
1915 | (We return nil on failure, but the value doesn't actually matter.) */ | |
18686d47 RS |
1916 | |
1917 | #ifdef USE_X_TOOLKIT | |
18686d47 | 1918 | |
8ed87156 | 1919 | /* We need a unique id for each widget handled by the Lucid Widget |
cc17e9bf KH |
1920 | library. |
1921 | ||
1922 | For the main windows, and popup menus, we use this counter, | |
88766961 | 1923 | which we increment each time after use. This starts from 1<<16. |
cc17e9bf | 1924 | |
88766961 RS |
1925 | For menu bars, we use numbers starting at 0, counted in |
1926 | next_menubar_widget_id. */ | |
8ed87156 | 1927 | LWLIB_ID widget_id_tick; |
165e1749 | 1928 | |
4dedbfe0 | 1929 | static Lisp_Object *volatile menu_item_selection; |
4dedbfe0 PR |
1930 | |
1931 | static void | |
1932 | popup_selection_callback (widget, id, client_data) | |
1933 | Widget widget; | |
1934 | LWLIB_ID id; | |
1935 | XtPointer client_data; | |
1936 | { | |
1937 | menu_item_selection = (Lisp_Object *) client_data; | |
1938 | } | |
1939 | ||
78589e07 | 1940 | static Lisp_Object |
673a6211 | 1941 | xmenu_show (f, x, y, for_click, keymaps, title, error) |
18686d47 | 1942 | FRAME_PTR f; |
18686d47 RS |
1943 | int x; |
1944 | int y; | |
9685a93f | 1945 | int for_click; |
78589e07 RS |
1946 | int keymaps; |
1947 | Lisp_Object title; | |
1948 | char **error; | |
18686d47 | 1949 | { |
78589e07 | 1950 | int i; |
cc17e9bf | 1951 | LWLIB_ID menu_id; |
18686d47 | 1952 | Widget menu; |
ffcb5a51 | 1953 | Arg av[2]; |
60f312e2 | 1954 | int ac = 0; |
78589e07 | 1955 | widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0; |
101bb4a5 RS |
1956 | widget_value **submenu_stack |
1957 | = (widget_value **) alloca (menu_items_used * sizeof (widget_value *)); | |
1958 | Lisp_Object *subprefix_stack | |
1959 | = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object)); | |
1960 | int submenu_depth = 0; | |
ffcb5a51 | 1961 | XButtonPressedEvent dummy; |
4e8d3549 | 1962 | |
78c8278d RS |
1963 | int first_pane; |
1964 | ||
78589e07 RS |
1965 | *error = NULL; |
1966 | ||
742f715d KH |
1967 | if (menu_items_used <= MENU_ITEMS_PANE_LENGTH) |
1968 | { | |
1969 | *error = "Empty menu"; | |
1970 | return Qnil; | |
1971 | } | |
63c414df | 1972 | |
78589e07 RS |
1973 | /* Create a tree of widget_value objects |
1974 | representing the panes and their items. */ | |
f7fab165 | 1975 | wv = xmalloc_widget_value (); |
78589e07 RS |
1976 | wv->name = "menu"; |
1977 | wv->value = 0; | |
1978 | wv->enabled = 1; | |
3427a3db | 1979 | wv->button_type = BUTTON_TYPE_NONE; |
78589e07 | 1980 | first_wv = wv; |
78c8278d | 1981 | first_pane = 1; |
78589e07 RS |
1982 | |
1983 | /* Loop over all panes and items, filling in the tree. */ | |
1984 | i = 0; | |
1985 | while (i < menu_items_used) | |
1986 | { | |
101bb4a5 RS |
1987 | if (EQ (XVECTOR (menu_items)->contents[i], Qnil)) |
1988 | { | |
1989 | submenu_stack[submenu_depth++] = save_wv; | |
1990 | save_wv = prev_wv; | |
1991 | prev_wv = 0; | |
78c8278d | 1992 | first_pane = 1; |
101bb4a5 RS |
1993 | i++; |
1994 | } | |
1995 | else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda)) | |
1996 | { | |
1997 | prev_wv = save_wv; | |
1998 | save_wv = submenu_stack[--submenu_depth]; | |
78c8278d | 1999 | first_pane = 0; |
101bb4a5 RS |
2000 | i++; |
2001 | } | |
2002 | else if (EQ (XVECTOR (menu_items)->contents[i], Qt) | |
2003 | && submenu_depth != 0) | |
2004 | i += MENU_ITEMS_PANE_LENGTH; | |
fcaa7665 RS |
2005 | /* Ignore a nil in the item list. |
2006 | It's meaningful only for dialog boxes. */ | |
2007 | else if (EQ (XVECTOR (menu_items)->contents[i], Qquote)) | |
2008 | i += 1; | |
101bb4a5 | 2009 | else if (EQ (XVECTOR (menu_items)->contents[i], Qt)) |
78589e07 RS |
2010 | { |
2011 | /* Create a new pane. */ | |
2012 | Lisp_Object pane_name, prefix; | |
2013 | char *pane_string; | |
2014 | pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME]; | |
2015 | prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX]; | |
703dc2a8 KH |
2016 | #ifndef HAVE_MULTILINGUAL_MENU |
2017 | if (!NILP (pane_name) && STRING_MULTIBYTE (pane_name)) | |
2018 | pane_name = string_make_unibyte (pane_name); | |
2019 | #endif | |
78589e07 RS |
2020 | pane_string = (NILP (pane_name) |
2021 | ? "" : (char *) XSTRING (pane_name)->data); | |
101bb4a5 | 2022 | /* If there is just one top-level pane, put all its items directly |
78589e07 RS |
2023 | under the top-level menu. */ |
2024 | if (menu_items_n_panes == 1) | |
2025 | pane_string = ""; | |
2026 | ||
2027 | /* If the pane has a meaningful name, | |
2028 | make the pane a top-level menu item | |
2029 | with its items as a submenu beneath it. */ | |
78c8278d | 2030 | if (!keymaps && strcmp (pane_string, "")) |
78589e07 | 2031 | { |
f7fab165 | 2032 | wv = xmalloc_widget_value (); |
78589e07 RS |
2033 | if (save_wv) |
2034 | save_wv->next = wv; | |
2035 | else | |
2036 | first_wv->contents = wv; | |
2037 | wv->name = pane_string; | |
2038 | if (keymaps && !NILP (prefix)) | |
2039 | wv->name++; | |
2040 | wv->value = 0; | |
2041 | wv->enabled = 1; | |
3427a3db | 2042 | wv->button_type = BUTTON_TYPE_NONE; |
78c8278d RS |
2043 | save_wv = wv; |
2044 | prev_wv = 0; | |
78589e07 | 2045 | } |
78c8278d RS |
2046 | else if (first_pane) |
2047 | { | |
2048 | save_wv = wv; | |
2049 | prev_wv = 0; | |
2050 | } | |
2051 | first_pane = 0; | |
78589e07 RS |
2052 | i += MENU_ITEMS_PANE_LENGTH; |
2053 | } | |
2054 | else | |
2055 | { | |
2056 | /* Create a new item within current pane. */ | |
9cd50434 | 2057 | Lisp_Object item_name, enable, descrip, def, type, selected, help; |
78589e07 RS |
2058 | item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME]; |
2059 | enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE]; | |
2060 | descrip | |
2061 | = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY]; | |
a352a815 | 2062 | def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION]; |
3427a3db GM |
2063 | type = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_TYPE]; |
2064 | selected = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_SELECTED]; | |
9cd50434 | 2065 | help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP]; |
3427a3db | 2066 | |
703dc2a8 | 2067 | #ifndef HAVE_MULTILINGUAL_MENU |
3427a3db GM |
2068 | if (STRINGP (item_name) && STRING_MULTIBYTE (item_name)) |
2069 | item_name = string_make_unibyte (item_name); | |
2070 | if (STRINGP (descrip) && STRING_MULTIBYTE (descrip)) | |
2071 | item_name = string_make_unibyte (descrip); | |
703dc2a8 | 2072 | #endif |
3427a3db | 2073 | |
f7fab165 | 2074 | wv = xmalloc_widget_value (); |
78589e07 RS |
2075 | if (prev_wv) |
2076 | prev_wv->next = wv; | |
2077 | else | |
2078 | save_wv->contents = wv; | |
4e8d3549 | 2079 | wv->name = (char *) XSTRING (item_name)->data; |
78589e07 | 2080 | if (!NILP (descrip)) |
4e8d3549 | 2081 | wv->key = (char *) XSTRING (descrip)->data; |
78589e07 | 2082 | wv->value = 0; |
a352a815 RS |
2083 | /* If this item has a null value, |
2084 | make the call_data null so that it won't display a box | |
2085 | when the mouse is on it. */ | |
2086 | wv->call_data | |
2087 | = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0); | |
78589e07 | 2088 | wv->enabled = !NILP (enable); |
3427a3db GM |
2089 | |
2090 | if (NILP (type)) | |
2091 | wv->button_type = BUTTON_TYPE_NONE; | |
2092 | else if (EQ (type, QCtoggle)) | |
2093 | wv->button_type = BUTTON_TYPE_TOGGLE; | |
2094 | else if (EQ (type, QCradio)) | |
2095 | wv->button_type = BUTTON_TYPE_RADIO; | |
2096 | else | |
2097 | abort (); | |
2098 | ||
2099 | wv->selected = !NILP (selected); | |
9cd50434 GM |
2100 | if (STRINGP (help)) |
2101 | wv->help = XSTRING (help)->data; | |
3427a3db | 2102 | |
78589e07 RS |
2103 | prev_wv = wv; |
2104 | ||
2105 | i += MENU_ITEMS_ITEM_LENGTH; | |
2106 | } | |
2107 | } | |
2108 | ||
c98fcf4b | 2109 | /* Deal with the title, if it is non-nil. */ |
4dedbfe0 PR |
2110 | if (!NILP (title)) |
2111 | { | |
f7fab165 RS |
2112 | widget_value *wv_title = xmalloc_widget_value (); |
2113 | widget_value *wv_sep1 = xmalloc_widget_value (); | |
2114 | widget_value *wv_sep2 = xmalloc_widget_value (); | |
4dedbfe0 PR |
2115 | |
2116 | wv_sep2->name = "--"; | |
2117 | wv_sep2->next = first_wv->contents; | |
2118 | ||
2119 | wv_sep1->name = "--"; | |
2120 | wv_sep1->next = wv_sep2; | |
2121 | ||
703dc2a8 KH |
2122 | #ifndef HAVE_MULTILINGUAL_MENU |
2123 | if (STRING_MULTIBYTE (title)) | |
2124 | title = string_make_unibyte (title); | |
2125 | #endif | |
4dedbfe0 PR |
2126 | wv_title->name = (char *) XSTRING (title)->data; |
2127 | wv_title->enabled = True; | |
3427a3db | 2128 | wv_title->button_type = BUTTON_TYPE_NONE; |
4dedbfe0 PR |
2129 | wv_title->next = wv_sep1; |
2130 | first_wv->contents = wv_title; | |
2131 | } | |
2132 | ||
78589e07 | 2133 | /* Actually create the menu. */ |
cc17e9bf | 2134 | menu_id = widget_id_tick++; |
78589e07 | 2135 | menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv, |
7556890b | 2136 | f->output_data.x->widget, 1, 0, |
4dedbfe0 | 2137 | popup_selection_callback, |
850df50b GM |
2138 | popup_deactivate_callback, |
2139 | menu_highlight_callback); | |
60f312e2 | 2140 | |
ffcb5a51 RS |
2141 | /* Adjust coordinates to relative to the outer (window manager) window. */ |
2142 | { | |
2143 | Window child; | |
2144 | int win_x = 0, win_y = 0; | |
2145 | ||
2146 | /* Find the position of the outside upper-left corner of | |
2147 | the inner window, with respect to the outer window. */ | |
2148 | if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window) | |
2149 | { | |
2150 | BLOCK_INPUT; | |
2151 | XTranslateCoordinates (FRAME_X_DISPLAY (f), | |
2152 | ||
2153 | /* From-window, to-window. */ | |
2154 | f->output_data.x->window_desc, | |
2155 | f->output_data.x->parent_desc, | |
2156 | ||
2157 | /* From-position, to-position. */ | |
2158 | 0, 0, &win_x, &win_y, | |
2159 | ||
2160 | /* Child of window. */ | |
2161 | &child); | |
2162 | UNBLOCK_INPUT; | |
2163 | x += win_x; | |
2164 | y += win_y; | |
2165 | } | |
2166 | } | |
2167 | ||
2168 | /* Adjust coordinates to be root-window-relative. */ | |
2169 | x += f->output_data.x->left_pos; | |
2170 | y += f->output_data.x->top_pos; | |
2171 | ||
2172 | dummy.type = ButtonPress; | |
2173 | dummy.serial = 0; | |
2174 | dummy.send_event = 0; | |
2175 | dummy.display = FRAME_X_DISPLAY (f); | |
2176 | dummy.time = CurrentTime; | |
ffcb5a51 RS |
2177 | dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window; |
2178 | dummy.window = dummy.root; | |
2179 | dummy.subwindow = dummy.root; | |
2180 | dummy.x_root = x; | |
2181 | dummy.y_root = y; | |
2182 | dummy.x = x; | |
2183 | dummy.y = y; | |
c8b5aa3d RS |
2184 | dummy.state = (FRAME_X_DISPLAY_INFO (f)->grabbed >> 1) * Button1Mask; |
2185 | dummy.button = 0; | |
2186 | for (i = 0; i < 5; i++) | |
2187 | if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i)) | |
2188 | dummy.button = i; | |
ffcb5a51 | 2189 | |
60f312e2 RS |
2190 | /* Don't allow any geometry request from the user. */ |
2191 | XtSetArg (av[ac], XtNgeometry, 0); ac++; | |
2192 | XtSetValues (menu, av, ac); | |
2193 | ||
78589e07 RS |
2194 | /* Free the widget_value objects we used to specify the contents. */ |
2195 | free_menubar_widget_value_tree (first_wv); | |
2196 | ||
357460d0 MB |
2197 | /* Override any default settings with ones from the `menu' face. */ |
2198 | x_set_menu_resources_from_menu_face (f, menu); | |
2199 | ||
78589e07 RS |
2200 | /* No selection has been chosen yet. */ |
2201 | menu_item_selection = 0; | |
2202 | ||
78589e07 | 2203 | /* Display the menu. */ |
a52445bd | 2204 | lw_popup_menu (menu, (XEvent *) &dummy); |
4dedbfe0 | 2205 | popup_activated_flag = 1; |
18686d47 | 2206 | |
78589e07 | 2207 | /* Process events that apply to the menu. */ |
2e2b8e22 | 2208 | popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id); |
78589e07 | 2209 | |
3427a3db GM |
2210 | #ifdef LESSTIF_VERSION |
2211 | /* Nov 1998: For an unknown reason a button grab remains active | |
2212 | after the popup menu has gone. */ | |
2213 | XUngrabButton (XtDisplay (f->output_data.x->widget), | |
2214 | AnyButton, AnyModifier, | |
2215 | XtWindow (f->output_data.x->widget)); | |
2216 | XUngrabButton (XtDisplay (f->output_data.x->edit_widget), | |
2217 | AnyButton, AnyModifier, | |
2218 | XtWindow (f->output_data.x->edit_widget)); | |
2219 | #endif /* LESSTIF_VERSION */ | |
2220 | ||
a9c90b7c RS |
2221 | /* fp turned off the following statement and wrote a comment |
2222 | that it is unnecessary--that the menu has already disappeared. | |
21af8a68 KH |
2223 | Nowadays the menu disappears ok, all right, but |
2224 | we need to delete the widgets or multiple ones will pile up. */ | |
78589e07 | 2225 | lw_destroy_all_widgets (menu_id); |
18686d47 | 2226 | |
78589e07 RS |
2227 | /* Find the selected item, and its pane, to return |
2228 | the proper value. */ | |
2229 | if (menu_item_selection != 0) | |
2230 | { | |
c63f6952 | 2231 | Lisp_Object prefix, entry; |
78589e07 | 2232 | |
6bbd7a29 | 2233 | prefix = entry = Qnil; |
78589e07 RS |
2234 | i = 0; |
2235 | while (i < menu_items_used) | |
2236 | { | |
101bb4a5 RS |
2237 | if (EQ (XVECTOR (menu_items)->contents[i], Qnil)) |
2238 | { | |
2239 | subprefix_stack[submenu_depth++] = prefix; | |
2240 | prefix = entry; | |
2241 | i++; | |
2242 | } | |
2243 | else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda)) | |
2244 | { | |
2245 | prefix = subprefix_stack[--submenu_depth]; | |
2246 | i++; | |
2247 | } | |
2248 | else if (EQ (XVECTOR (menu_items)->contents[i], Qt)) | |
78589e07 RS |
2249 | { |
2250 | prefix | |
2251 | = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX]; | |
2252 | i += MENU_ITEMS_PANE_LENGTH; | |
2253 | } | |
d31d42cc RS |
2254 | /* Ignore a nil in the item list. |
2255 | It's meaningful only for dialog boxes. */ | |
2256 | else if (EQ (XVECTOR (menu_items)->contents[i], Qquote)) | |
2257 | i += 1; | |
78589e07 RS |
2258 | else |
2259 | { | |
2260 | entry | |
2261 | = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE]; | |
2262 | if (menu_item_selection == &XVECTOR (menu_items)->contents[i]) | |
2263 | { | |
2264 | if (keymaps != 0) | |
2265 | { | |
101bb4a5 RS |
2266 | int j; |
2267 | ||
78589e07 RS |
2268 | entry = Fcons (entry, Qnil); |
2269 | if (!NILP (prefix)) | |
2270 | entry = Fcons (prefix, entry); | |
101bb4a5 | 2271 | for (j = submenu_depth - 1; j >= 0; j--) |
e48087b7 | 2272 | if (!NILP (subprefix_stack[j])) |
5964e450 | 2273 | entry = Fcons (subprefix_stack[j], entry); |
78589e07 RS |
2274 | } |
2275 | return entry; | |
2276 | } | |
2277 | i += MENU_ITEMS_ITEM_LENGTH; | |
2278 | } | |
2279 | } | |
2280 | } | |
2281 | ||
2282 | return Qnil; | |
18686d47 | 2283 | } |
4dedbfe0 PR |
2284 | \f |
2285 | static void | |
2286 | dialog_selection_callback (widget, id, client_data) | |
2287 | Widget widget; | |
2288 | LWLIB_ID id; | |
2289 | XtPointer client_data; | |
2290 | { | |
01d5e892 RS |
2291 | /* The EMACS_INT cast avoids a warning. There's no problem |
2292 | as long as pointers have enough bits to hold small integers. */ | |
2293 | if ((int) (EMACS_INT) client_data != -1) | |
4dedbfe0 PR |
2294 | menu_item_selection = (Lisp_Object *) client_data; |
2295 | BLOCK_INPUT; | |
2296 | lw_destroy_all_widgets (id); | |
2297 | UNBLOCK_INPUT; | |
9572375b | 2298 | popup_activated_flag = 0; |
4dedbfe0 | 2299 | } |
18686d47 | 2300 | |
165e1749 FP |
2301 | static char * button_names [] = { |
2302 | "button1", "button2", "button3", "button4", "button5", | |
2303 | "button6", "button7", "button8", "button9", "button10" }; | |
2304 | ||
2305 | static Lisp_Object | |
673a6211 | 2306 | xdialog_show (f, keymaps, title, error) |
165e1749 | 2307 | FRAME_PTR f; |
165e1749 FP |
2308 | int keymaps; |
2309 | Lisp_Object title; | |
2310 | char **error; | |
2311 | { | |
2312 | int i, nb_buttons=0; | |
cc17e9bf | 2313 | LWLIB_ID dialog_id; |
165e1749 | 2314 | Widget menu; |
80670155 | 2315 | char dialog_name[6]; |
165e1749 | 2316 | |
faa935b6 | 2317 | widget_value *wv, *first_wv = 0, *prev_wv = 0; |
165e1749 | 2318 | |
fcaa7665 RS |
2319 | /* Number of elements seen so far, before boundary. */ |
2320 | int left_count = 0; | |
2321 | /* 1 means we've seen the boundary between left-hand elts and right-hand. */ | |
2322 | int boundary_seen = 0; | |
2323 | ||
165e1749 FP |
2324 | *error = NULL; |
2325 | ||
80670155 RS |
2326 | if (menu_items_n_panes > 1) |
2327 | { | |
2328 | *error = "Multiple panes in dialog box"; | |
2329 | return Qnil; | |
2330 | } | |
2331 | ||
165e1749 FP |
2332 | /* Create a tree of widget_value objects |
2333 | representing the text label and buttons. */ | |
2334 | { | |
2335 | Lisp_Object pane_name, prefix; | |
2336 | char *pane_string; | |
2337 | pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME]; | |
2338 | prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX]; | |
2339 | pane_string = (NILP (pane_name) | |
2340 | ? "" : (char *) XSTRING (pane_name)->data); | |
f7fab165 | 2341 | prev_wv = xmalloc_widget_value (); |
165e1749 FP |
2342 | prev_wv->value = pane_string; |
2343 | if (keymaps && !NILP (prefix)) | |
2344 | prev_wv->name++; | |
2345 | prev_wv->enabled = 1; | |
2346 | prev_wv->name = "message"; | |
2347 | first_wv = prev_wv; | |
2348 | ||
2349 | /* Loop over all panes and items, filling in the tree. */ | |
2350 | i = MENU_ITEMS_PANE_LENGTH; | |
2351 | while (i < menu_items_used) | |
2352 | { | |
2353 | ||
2354 | /* Create a new item within current pane. */ | |
2355 | Lisp_Object item_name, enable, descrip; | |
2356 | item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME]; | |
2357 | enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE]; | |
2358 | descrip | |
2359 | = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY]; | |
2360 | ||
80670155 RS |
2361 | if (NILP (item_name)) |
2362 | { | |
2363 | free_menubar_widget_value_tree (first_wv); | |
2364 | *error = "Submenu in dialog items"; | |
2365 | return Qnil; | |
2366 | } | |
fcaa7665 RS |
2367 | if (EQ (item_name, Qquote)) |
2368 | { | |
2369 | /* This is the boundary between left-side elts | |
2370 | and right-side elts. Stop incrementing right_count. */ | |
2371 | boundary_seen = 1; | |
2372 | i++; | |
2373 | continue; | |
2374 | } | |
86e71abf | 2375 | if (nb_buttons >= 9) |
80670155 RS |
2376 | { |
2377 | free_menubar_widget_value_tree (first_wv); | |
2378 | *error = "Too many dialog items"; | |
2379 | return Qnil; | |
2380 | } | |
2381 | ||
f7fab165 | 2382 | wv = xmalloc_widget_value (); |
165e1749 | 2383 | prev_wv->next = wv; |
80670155 | 2384 | wv->name = (char *) button_names[nb_buttons]; |
165e1749 | 2385 | if (!NILP (descrip)) |
4e8d3549 RS |
2386 | wv->key = (char *) XSTRING (descrip)->data; |
2387 | wv->value = (char *) XSTRING (item_name)->data; | |
165e1749 FP |
2388 | wv->call_data = (void *) &XVECTOR (menu_items)->contents[i]; |
2389 | wv->enabled = !NILP (enable); | |
2390 | prev_wv = wv; | |
2391 | ||
fcaa7665 RS |
2392 | if (! boundary_seen) |
2393 | left_count++; | |
2394 | ||
165e1749 FP |
2395 | nb_buttons++; |
2396 | i += MENU_ITEMS_ITEM_LENGTH; | |
2397 | } | |
2398 | ||
fcaa7665 RS |
2399 | /* If the boundary was not specified, |
2400 | by default put half on the left and half on the right. */ | |
2401 | if (! boundary_seen) | |
2402 | left_count = nb_buttons - nb_buttons / 2; | |
2403 | ||
f7fab165 | 2404 | wv = xmalloc_widget_value (); |
80670155 RS |
2405 | wv->name = dialog_name; |
2406 | ||
2407 | /* Dialog boxes use a really stupid name encoding | |
2408 | which specifies how many buttons to use | |
2409 | and how many buttons are on the right. | |
2410 | The Q means something also. */ | |
2411 | dialog_name[0] = 'Q'; | |
2412 | dialog_name[1] = '0' + nb_buttons; | |
2413 | dialog_name[2] = 'B'; | |
2414 | dialog_name[3] = 'R'; | |
fcaa7665 RS |
2415 | /* Number of buttons to put on the right. */ |
2416 | dialog_name[4] = '0' + nb_buttons - left_count; | |
80670155 | 2417 | dialog_name[5] = 0; |
165e1749 FP |
2418 | wv->contents = first_wv; |
2419 | first_wv = wv; | |
165e1749 FP |
2420 | } |
2421 | ||
2422 | /* Actually create the dialog. */ | |
cc17e9bf | 2423 | dialog_id = widget_id_tick++; |
165e1749 | 2424 | menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv, |
7556890b | 2425 | f->output_data.x->widget, 1, 0, |
850df50b | 2426 | dialog_selection_callback, 0, 0); |
b5587215 | 2427 | lw_modify_all_widgets (dialog_id, first_wv->contents, True); |
165e1749 FP |
2428 | /* Free the widget_value objects we used to specify the contents. */ |
2429 | free_menubar_widget_value_tree (first_wv); | |
2430 | ||
2431 | /* No selection has been chosen yet. */ | |
2432 | menu_item_selection = 0; | |
2433 | ||
165e1749 FP |
2434 | /* Display the menu. */ |
2435 | lw_pop_up_all_widgets (dialog_id); | |
aa669def | 2436 | popup_activated_flag = 1; |
165e1749 FP |
2437 | |
2438 | /* Process events that apply to the menu. */ | |
2e2b8e22 | 2439 | popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id); |
165e1749 | 2440 | |
21af8a68 KH |
2441 | lw_destroy_all_widgets (dialog_id); |
2442 | ||
165e1749 FP |
2443 | /* Find the selected item, and its pane, to return |
2444 | the proper value. */ | |
2445 | if (menu_item_selection != 0) | |
2446 | { | |
2447 | Lisp_Object prefix; | |
2448 | ||
2449 | prefix = Qnil; | |
2450 | i = 0; | |
2451 | while (i < menu_items_used) | |
2452 | { | |
2453 | Lisp_Object entry; | |
2454 | ||
2455 | if (EQ (XVECTOR (menu_items)->contents[i], Qt)) | |
2456 | { | |
2457 | prefix | |
2458 | = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX]; | |
2459 | i += MENU_ITEMS_PANE_LENGTH; | |
2460 | } | |
85996cfb GM |
2461 | else if (EQ (XVECTOR (menu_items)->contents[i], Qquote)) |
2462 | { | |
2463 | /* This is the boundary between left-side elts and | |
2464 | right-side elts. */ | |
2465 | ++i; | |
2466 | } | |
165e1749 FP |
2467 | else |
2468 | { | |
2469 | entry | |
2470 | = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE]; | |
2471 | if (menu_item_selection == &XVECTOR (menu_items)->contents[i]) | |
2472 | { | |
2473 | if (keymaps != 0) | |
2474 | { | |
2475 | entry = Fcons (entry, Qnil); | |
2476 | if (!NILP (prefix)) | |
2477 | entry = Fcons (prefix, entry); | |
2478 | } | |
2479 | return entry; | |
2480 | } | |
2481 | i += MENU_ITEMS_ITEM_LENGTH; | |
2482 | } | |
2483 | } | |
2484 | } | |
2485 | ||
2486 | return Qnil; | |
2487 | } | |
ba461919 | 2488 | |
18686d47 | 2489 | #else /* not USE_X_TOOLKIT */ |
78589e07 | 2490 | |
3e703b25 GM |
2491 | /* The frame of the last activated non-toolkit menu bar. |
2492 | Used to generate menu help events. */ | |
2493 | ||
2494 | static struct frame *menu_help_frame; | |
2495 | ||
2496 | ||
62145073 GM |
2497 | /* Show help HELP_STRING, or clear help if HELP_STRING is null. |
2498 | ||
2499 | PANE is the pane number, and ITEM is the menu item number in | |
2500 | the menu (currently not used). | |
2501 | ||
2502 | This cannot be done with generating a HELP_EVENT because | |
2503 | XMenuActivate contains a loop that doesn't let Emacs process | |
2504 | keyboard events. */ | |
3e703b25 GM |
2505 | |
2506 | static void | |
62145073 | 2507 | menu_help_callback (help_string, pane, item) |
3e703b25 | 2508 | char *help_string; |
62145073 | 2509 | int pane, item; |
3e703b25 | 2510 | { |
62145073 GM |
2511 | extern Lisp_Object Qmenu_item; |
2512 | Lisp_Object *first_item; | |
2513 | Lisp_Object pane_name; | |
2514 | Lisp_Object menu_object; | |
2515 | ||
2516 | first_item = XVECTOR (menu_items)->contents; | |
2517 | if (EQ (first_item[0], Qt)) | |
2518 | pane_name = first_item[MENU_ITEMS_PANE_NAME]; | |
2519 | else if (EQ (first_item[0], Qquote)) | |
2520 | /* This shouldn't happen, see xmenu_show. */ | |
2521 | pane_name = build_string (""); | |
2522 | else | |
2523 | pane_name = first_item[MENU_ITEMS_ITEM_NAME]; | |
2524 | ||
2525 | /* (menu-item MENU-NAME PANE-NUMBER) */ | |
2526 | menu_object = Fcons (Qmenu_item, | |
2527 | Fcons (pane_name, | |
2528 | Fcons (make_number (pane), Qnil))); | |
ba461919 | 2529 | show_help_echo (help_string ? build_string (help_string) : Qnil, |
62145073 | 2530 | Qnil, menu_object, make_number (item), 1); |
3e703b25 | 2531 | } |
62145073 | 2532 | |
3e703b25 | 2533 | |
78589e07 | 2534 | static Lisp_Object |
673a6211 | 2535 | xmenu_show (f, x, y, for_click, keymaps, title, error) |
78589e07 RS |
2536 | FRAME_PTR f; |
2537 | int x, y; | |
9685a93f RS |
2538 | int for_click; |
2539 | int keymaps; | |
78589e07 RS |
2540 | Lisp_Object title; |
2541 | char **error; | |
dcfdbac7 | 2542 | { |
78589e07 RS |
2543 | Window root; |
2544 | XMenu *menu; | |
2545 | int pane, selidx, lpane, status; | |
2546 | Lisp_Object entry, pane_prefix; | |
dcfdbac7 JB |
2547 | char *datap; |
2548 | int ulx, uly, width, height; | |
2549 | int dispwidth, dispheight; | |
4e8d3549 RS |
2550 | int i, j; |
2551 | int maxwidth; | |
78589e07 RS |
2552 | int dummy_int; |
2553 | unsigned int dummy_uint; | |
088831f6 | 2554 | |
07a675b7 | 2555 | *error = 0; |
78589e07 RS |
2556 | if (menu_items_n_panes == 0) |
2557 | return Qnil; | |
088831f6 | 2558 | |
742f715d KH |
2559 | if (menu_items_used <= MENU_ITEMS_PANE_LENGTH) |
2560 | { | |
2561 | *error = "Empty menu"; | |
2562 | return Qnil; | |
2563 | } | |
2564 | ||
78589e07 | 2565 | /* Figure out which root window F is on. */ |
92280f67 | 2566 | XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root, |
78589e07 RS |
2567 | &dummy_int, &dummy_int, &dummy_uint, &dummy_uint, |
2568 | &dummy_uint, &dummy_uint); | |
18686d47 | 2569 | |
78589e07 | 2570 | /* Make the menu on that window. */ |
92280f67 | 2571 | menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs"); |
78589e07 | 2572 | if (menu == NULL) |
dcfdbac7 JB |
2573 | { |
2574 | *error = "Can't create menu"; | |
78589e07 | 2575 | return Qnil; |
dcfdbac7 | 2576 | } |
78589e07 | 2577 | |
87485d6f | 2578 | #ifdef HAVE_X_WINDOWS |
78589e07 | 2579 | /* Adjust coordinates to relative to the outer (window manager) window. */ |
78589e07 RS |
2580 | { |
2581 | Window child; | |
2582 | int win_x = 0, win_y = 0; | |
2583 | ||
2584 | /* Find the position of the outside upper-left corner of | |
2585 | the inner window, with respect to the outer window. */ | |
7556890b | 2586 | if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window) |
78589e07 RS |
2587 | { |
2588 | BLOCK_INPUT; | |
92280f67 | 2589 | XTranslateCoordinates (FRAME_X_DISPLAY (f), |
78589e07 RS |
2590 | |
2591 | /* From-window, to-window. */ | |
7556890b RS |
2592 | f->output_data.x->window_desc, |
2593 | f->output_data.x->parent_desc, | |
78589e07 RS |
2594 | |
2595 | /* From-position, to-position. */ | |
2596 | 0, 0, &win_x, &win_y, | |
2597 | ||
2598 | /* Child of window. */ | |
2599 | &child); | |
2600 | UNBLOCK_INPUT; | |
2601 | x += win_x; | |
2602 | y += win_y; | |
2603 | } | |
2604 | } | |
87485d6f | 2605 | #endif /* HAVE_X_WINDOWS */ |
78589e07 RS |
2606 | |
2607 | /* Adjust coordinates to be root-window-relative. */ | |
7556890b RS |
2608 | x += f->output_data.x->left_pos; |
2609 | y += f->output_data.x->top_pos; | |
18686d47 | 2610 | |
78589e07 RS |
2611 | /* Create all the necessary panes and their items. */ |
2612 | i = 0; | |
2613 | while (i < menu_items_used) | |
dcfdbac7 | 2614 | { |
78589e07 | 2615 | if (EQ (XVECTOR (menu_items)->contents[i], Qt)) |
dcfdbac7 | 2616 | { |
78589e07 RS |
2617 | /* Create a new pane. */ |
2618 | Lisp_Object pane_name, prefix; | |
2619 | char *pane_string; | |
2620 | ||
2621 | pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME]; | |
2622 | prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX]; | |
2623 | pane_string = (NILP (pane_name) | |
2624 | ? "" : (char *) XSTRING (pane_name)->data); | |
2625 | if (keymaps && !NILP (prefix)) | |
2626 | pane_string++; | |
2627 | ||
92280f67 | 2628 | lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE); |
78589e07 RS |
2629 | if (lpane == XM_FAILURE) |
2630 | { | |
92280f67 | 2631 | XMenuDestroy (FRAME_X_DISPLAY (f), menu); |
78589e07 RS |
2632 | *error = "Can't create pane"; |
2633 | return Qnil; | |
2634 | } | |
2635 | i += MENU_ITEMS_PANE_LENGTH; | |
4e8d3549 RS |
2636 | |
2637 | /* Find the width of the widest item in this pane. */ | |
2638 | maxwidth = 0; | |
2639 | j = i; | |
2640 | while (j < menu_items_used) | |
2641 | { | |
2642 | Lisp_Object item; | |
2643 | item = XVECTOR (menu_items)->contents[j]; | |
2644 | if (EQ (item, Qt)) | |
2645 | break; | |
2646 | if (NILP (item)) | |
2647 | { | |
2648 | j++; | |
2649 | continue; | |
2650 | } | |
fc932ac6 | 2651 | width = STRING_BYTES (XSTRING (item)); |
4e8d3549 RS |
2652 | if (width > maxwidth) |
2653 | maxwidth = width; | |
2654 | ||
2655 | j += MENU_ITEMS_ITEM_LENGTH; | |
2656 | } | |
dcfdbac7 | 2657 | } |
fcaa7665 RS |
2658 | /* Ignore a nil in the item list. |
2659 | It's meaningful only for dialog boxes. */ | |
2660 | else if (EQ (XVECTOR (menu_items)->contents[i], Qquote)) | |
2661 | i += 1; | |
78589e07 | 2662 | else |
dcfdbac7 | 2663 | { |
78589e07 | 2664 | /* Create a new item within current pane. */ |
3e703b25 | 2665 | Lisp_Object item_name, enable, descrip, help; |
4e8d3549 | 2666 | unsigned char *item_data; |
3e703b25 | 2667 | char *help_string; |
78589e07 RS |
2668 | |
2669 | item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME]; | |
2670 | enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE]; | |
2671 | descrip | |
2672 | = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY]; | |
3e703b25 GM |
2673 | help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP]; |
2674 | help_string = STRINGP (help) ? XSTRING (help)->data : NULL; | |
2675 | ||
78589e07 | 2676 | if (!NILP (descrip)) |
4e8d3549 | 2677 | { |
fc932ac6 | 2678 | int gap = maxwidth - STRING_BYTES (XSTRING (item_name)); |
4e8d3549 RS |
2679 | #ifdef C_ALLOCA |
2680 | Lisp_Object spacer; | |
2681 | spacer = Fmake_string (make_number (gap), make_number (' ')); | |
2682 | item_name = concat2 (item_name, spacer); | |
2683 | item_name = concat2 (item_name, descrip); | |
2684 | item_data = XSTRING (item_name)->data; | |
2685 | #else | |
2686 | /* if alloca is fast, use that to make the space, | |
2687 | to reduce gc needs. */ | |
2688 | item_data | |
2689 | = (unsigned char *) alloca (maxwidth | |
fc932ac6 | 2690 | + STRING_BYTES (XSTRING (descrip)) + 1); |
4e8d3549 | 2691 | bcopy (XSTRING (item_name)->data, item_data, |
fc932ac6 | 2692 | STRING_BYTES (XSTRING (item_name))); |
4e8d3549 RS |
2693 | for (j = XSTRING (item_name)->size; j < maxwidth; j++) |
2694 | item_data[j] = ' '; | |
2695 | bcopy (XSTRING (descrip)->data, item_data + j, | |
fc932ac6 RS |
2696 | STRING_BYTES (XSTRING (descrip))); |
2697 | item_data[j + STRING_BYTES (XSTRING (descrip))] = 0; | |
4e8d3549 RS |
2698 | #endif |
2699 | } | |
2700 | else | |
2701 | item_data = XSTRING (item_name)->data; | |
78589e07 | 2702 | |
92280f67 RS |
2703 | if (XMenuAddSelection (FRAME_X_DISPLAY (f), |
2704 | menu, lpane, 0, item_data, | |
3e703b25 | 2705 | !NILP (enable), help_string) |
dcfdbac7 JB |
2706 | == XM_FAILURE) |
2707 | { | |
92280f67 | 2708 | XMenuDestroy (FRAME_X_DISPLAY (f), menu); |
dcfdbac7 | 2709 | *error = "Can't add selection to menu"; |
78589e07 | 2710 | return Qnil; |
dcfdbac7 | 2711 | } |
78589e07 | 2712 | i += MENU_ITEMS_ITEM_LENGTH; |
dcfdbac7 JB |
2713 | } |
2714 | } | |
4e8d3549 | 2715 | |
78589e07 | 2716 | /* All set and ready to fly. */ |
92280f67 RS |
2717 | XMenuRecompute (FRAME_X_DISPLAY (f), menu); |
2718 | dispwidth = DisplayWidth (FRAME_X_DISPLAY (f), | |
f1847de3 | 2719 | XScreenNumberOfScreen (FRAME_X_SCREEN (f))); |
92280f67 | 2720 | dispheight = DisplayHeight (FRAME_X_DISPLAY (f), |
f1847de3 | 2721 | XScreenNumberOfScreen (FRAME_X_SCREEN (f))); |
78589e07 RS |
2722 | x = min (x, dispwidth); |
2723 | y = min (y, dispheight); | |
2724 | x = max (x, 1); | |
2725 | y = max (y, 1); | |
92280f67 | 2726 | XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y, |
dcfdbac7 JB |
2727 | &ulx, &uly, &width, &height); |
2728 | if (ulx+width > dispwidth) | |
2729 | { | |
78589e07 | 2730 | x -= (ulx + width) - dispwidth; |
dcfdbac7 JB |
2731 | ulx = dispwidth - width; |
2732 | } | |
2733 | if (uly+height > dispheight) | |
2734 | { | |
78589e07 | 2735 | y -= (uly + height) - dispheight; |
dcfdbac7 JB |
2736 | uly = dispheight - height; |
2737 | } | |
78589e07 RS |
2738 | if (ulx < 0) x -= ulx; |
2739 | if (uly < 0) y -= uly; | |
121e4555 KH |
2740 | |
2741 | XMenuSetAEQ (menu, TRUE); | |
78589e07 RS |
2742 | XMenuSetFreeze (menu, TRUE); |
2743 | pane = selidx = 0; | |
3e703b25 GM |
2744 | |
2745 | /* Help display under X won't work because XMenuActivate contains | |
2746 | a loop that doesn't give Emacs a chance to process it. */ | |
2747 | menu_help_frame = f; | |
92280f67 | 2748 | status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx, |
3e703b25 GM |
2749 | x, y, ButtonReleaseMask, &datap, |
2750 | menu_help_callback); | |
a352a815 RS |
2751 | |
2752 | ||
f1df80a8 | 2753 | #ifdef HAVE_X_WINDOWS |
a352a815 RS |
2754 | /* Assume the mouse has moved out of the X window. |
2755 | If it has actually moved in, we will get an EnterNotify. */ | |
29e460bd | 2756 | x_mouse_leave (FRAME_X_DISPLAY_INFO (f)); |
f1df80a8 | 2757 | #endif |
a352a815 | 2758 | |
dcfdbac7 JB |
2759 | switch (status) |
2760 | { | |
2761 | case XM_SUCCESS: | |
2762 | #ifdef XDEBUG | |
2763 | fprintf (stderr, "pane= %d line = %d\n", panes, selidx); | |
2764 | #endif | |
fa6d54d9 | 2765 | |
78589e07 RS |
2766 | /* Find the item number SELIDX in pane number PANE. */ |
2767 | i = 0; | |
2768 | while (i < menu_items_used) | |
fa6d54d9 | 2769 | { |
78589e07 | 2770 | if (EQ (XVECTOR (menu_items)->contents[i], Qt)) |
088831f6 | 2771 | { |
78589e07 RS |
2772 | if (pane == 0) |
2773 | pane_prefix | |
2774 | = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX]; | |
2775 | pane--; | |
2776 | i += MENU_ITEMS_PANE_LENGTH; | |
088831f6 | 2777 | } |
78589e07 | 2778 | else |
ab6ee1a0 | 2779 | { |
78589e07 | 2780 | if (pane == -1) |
ab6ee1a0 | 2781 | { |
78589e07 | 2782 | if (selidx == 0) |
ab6ee1a0 | 2783 | { |
78589e07 RS |
2784 | entry |
2785 | = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE]; | |
2786 | if (keymaps != 0) | |
ab6ee1a0 | 2787 | { |
78589e07 RS |
2788 | entry = Fcons (entry, Qnil); |
2789 | if (!NILP (pane_prefix)) | |
2790 | entry = Fcons (pane_prefix, entry); | |
ab6ee1a0 | 2791 | } |
78589e07 | 2792 | break; |
ab6ee1a0 | 2793 | } |
78589e07 | 2794 | selidx--; |
ab6ee1a0 | 2795 | } |
78589e07 | 2796 | i += MENU_ITEMS_ITEM_LENGTH; |
ab6ee1a0 RS |
2797 | } |
2798 | } | |
78589e07 | 2799 | break; |
dcfdbac7 | 2800 | |
78589e07 | 2801 | case XM_FAILURE: |
78589e07 RS |
2802 | *error = "Can't activate menu"; |
2803 | case XM_IA_SELECT: | |
2804 | case XM_NO_SELECT: | |
2805 | entry = Qnil; | |
2806 | break; | |
dcfdbac7 | 2807 | } |
92280f67 | 2808 | XMenuDestroy (FRAME_X_DISPLAY (f), menu); |
a5285df3 | 2809 | |
87485d6f | 2810 | #ifdef HAVE_X_WINDOWS |
a5285df3 RS |
2811 | /* State that no mouse buttons are now held. |
2812 | (The oldXMenu code doesn't track this info for us.) | |
2813 | That is not necessarily true, but the fiction leads to reasonable | |
2814 | results, and it is a pain to ask which are actually held now. */ | |
e9a79fb2 | 2815 | FRAME_X_DISPLAY_INFO (f)->grabbed = 0; |
87485d6f | 2816 | #endif |
a5285df3 | 2817 | |
78589e07 | 2818 | return entry; |
dcfdbac7 | 2819 | } |
4dedbfe0 | 2820 | |
78589e07 | 2821 | #endif /* not USE_X_TOOLKIT */ |
1e659e4c RS |
2822 | |
2823 | #endif /* HAVE_MENUS */ | |
088831f6 | 2824 | \f |
dfcf069d | 2825 | void |
78589e07 | 2826 | syms_of_xmenu () |
dcfdbac7 | 2827 | { |
78589e07 RS |
2828 | staticpro (&menu_items); |
2829 | menu_items = Qnil; | |
dcfdbac7 | 2830 | |
0314aacb RS |
2831 | Qdebug_on_next_call = intern ("debug-on-next-call"); |
2832 | staticpro (&Qdebug_on_next_call); | |
2833 | ||
bfc524bc RS |
2834 | DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame, |
2835 | "Frame for which we are updating a menu.\n\ | |
2836 | The enable predicate for a menu command should check this variable."); | |
2837 | Vmenu_updating_frame = Qnil; | |
2838 | ||
8ed87156 | 2839 | #ifdef USE_X_TOOLKIT |
4dedbfe0 | 2840 | widget_id_tick = (1<<16); |
88766961 | 2841 | next_menubar_widget_id = 1; |
8ed87156 RS |
2842 | #endif |
2843 | ||
78589e07 | 2844 | defsubr (&Sx_popup_menu); |
1e659e4c | 2845 | #ifdef HAVE_MENUS |
165e1749 | 2846 | defsubr (&Sx_popup_dialog); |
1e659e4c | 2847 | #endif |
dcfdbac7 | 2848 | } |