Commit | Line | Data |
---|---|---|
dcfdbac7 | 1 | /* X Communication module for terminals which understand the X protocol. |
b5b4d636 | 2 | Copyright (C) 1986, 1988, 1993, 1994 Free Software Foundation, Inc. |
dcfdbac7 JB |
3 | |
4 | This file is part of GNU Emacs. | |
5 | ||
6 | GNU Emacs is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
774910eb | 8 | the Free Software Foundation; either version 2, or (at your option) |
dcfdbac7 JB |
9 | any later version. |
10 | ||
11 | GNU Emacs is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU Emacs; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
20 | /* X pop-up deck-of-cards menu facility for gnuemacs. | |
21 | * | |
22 | * Written by Jon Arnold and Roman Budzianowski | |
23 | * Mods and rewrite by Robert Krawitz | |
24 | * | |
25 | */ | |
26 | ||
18686d47 RS |
27 | /* Modified by Fred Pierresteguy on December 93 |
28 | to make the popup menus and menubar use the Xt. */ | |
29 | ||
78589e07 RS |
30 | /* Rewritten for clarity and GC protection by rms in Feb 94. */ |
31 | ||
dcfdbac7 JB |
32 | /* On 4.3 this loses if it comes after xterm.h. */ |
33 | #include <signal.h> | |
18160b98 | 34 | #include <config.h> |
565620a5 RS |
35 | |
36 | #include <stdio.h> | |
dcfdbac7 | 37 | #include "lisp.h" |
18686d47 | 38 | #include "termhooks.h" |
7708e9bd | 39 | #include "frame.h" |
dcfdbac7 | 40 | #include "window.h" |
031b0e31 | 41 | #include "keyboard.h" |
9ac0d9e0 | 42 | #include "blockinput.h" |
62010a73 | 43 | #include "puresize.h" |
dcfdbac7 | 44 | |
87485d6f | 45 | #ifdef HAVE_X_WINDOWS |
dcfdbac7 JB |
46 | /* This may include sys/types.h, and that somehow loses |
47 | if this is not done before the other system files. */ | |
48 | #include "xterm.h" | |
87485d6f | 49 | #endif |
dcfdbac7 JB |
50 | |
51 | /* Load sys/types.h if not already loaded. | |
52 | In some systems loading it twice is suicidal. */ | |
53 | #ifndef makedev | |
54 | #include <sys/types.h> | |
55 | #endif | |
56 | ||
57 | #include "dispextern.h" | |
58 | ||
87485d6f | 59 | #ifdef HAVE_X_WINDOWS |
dcfdbac7 | 60 | #include "../oldXMenu/XMenu.h" |
87485d6f | 61 | #endif |
dcfdbac7 | 62 | |
18686d47 RS |
63 | #ifdef USE_X_TOOLKIT |
64 | #include <X11/Xlib.h> | |
65 | #include <X11/IntrinsicP.h> | |
66 | #include <X11/CoreP.h> | |
67 | #include <X11/StringDefs.h> | |
60f312e2 | 68 | #include <X11/Shell.h> |
18686d47 | 69 | #include "../lwlib/lwlib.h" |
18686d47 RS |
70 | #endif /* USE_X_TOOLKIT */ |
71 | ||
dcfdbac7 JB |
72 | #define min(x,y) (((x) < (y)) ? (x) : (y)) |
73 | #define max(x,y) (((x) > (y)) ? (x) : (y)) | |
74 | ||
dcfdbac7 JB |
75 | #ifndef TRUE |
76 | #define TRUE 1 | |
77 | #define FALSE 0 | |
78589e07 | 78 | #endif /* no TRUE */ |
dcfdbac7 | 79 | |
6904bdcd | 80 | extern Lisp_Object Qmenu_enable; |
18686d47 | 81 | extern Lisp_Object Qmenu_bar; |
92280f67 | 82 | extern Lisp_Object Qmouse_click, Qevent_kind; |
78589e07 | 83 | |
18686d47 | 84 | #ifdef USE_X_TOOLKIT |
78589e07 RS |
85 | extern void process_expose_from_menu (); |
86 | extern XtAppContext Xt_app_con; | |
87 | ||
165e1749 | 88 | static Lisp_Object xdialog_show (); |
4dedbfe0 | 89 | void popup_get_selection (); |
18686d47 RS |
90 | #endif |
91 | ||
78589e07 RS |
92 | static Lisp_Object xmenu_show (); |
93 | static void keymap_panes (); | |
94 | static void single_keymap_panes (); | |
95 | static void list_of_panes (); | |
96 | static void list_of_items (); | |
97 | \f | |
98 | /* This holds a Lisp vector that holds the results of decoding | |
99 | the keymaps or alist-of-alists that specify a menu. | |
dcfdbac7 | 100 | |
78589e07 | 101 | It describes the panes and items within the panes. |
dcfdbac7 | 102 | |
78589e07 RS |
103 | Each pane is described by 3 elements in the vector: |
104 | t, the pane name, the pane's prefix key. | |
105 | Then follow the pane's items, with 4 elements per item: | |
106 | the item string, the enable flag, the item's value, | |
107 | and the equivalent keyboard key's description string. | |
dcfdbac7 | 108 | |
101bb4a5 RS |
109 | In some cases, multiple levels of menus may be described. |
110 | A single vector slot containing nil indicates the start of a submenu. | |
111 | A single vector slot containing lambda indicates the end of a submenu. | |
112 | The submenu follows a menu item which is the way to reach the submenu. | |
113 | ||
fcaa7665 RS |
114 | A single vector slot containing quote indicates that the |
115 | following items should appear on the right of a dialog box. | |
116 | ||
78589e07 RS |
117 | Using a Lisp vector to hold this information while we decode it |
118 | takes care of protecting all the data from GC. */ | |
dcfdbac7 | 119 | |
78589e07 RS |
120 | #define MENU_ITEMS_PANE_NAME 1 |
121 | #define MENU_ITEMS_PANE_PREFIX 2 | |
122 | #define MENU_ITEMS_PANE_LENGTH 3 | |
088831f6 | 123 | |
78589e07 RS |
124 | #define MENU_ITEMS_ITEM_NAME 0 |
125 | #define MENU_ITEMS_ITEM_ENABLE 1 | |
126 | #define MENU_ITEMS_ITEM_VALUE 2 | |
127 | #define MENU_ITEMS_ITEM_EQUIV_KEY 3 | |
128 | #define MENU_ITEMS_ITEM_LENGTH 4 | |
7da99777 | 129 | |
78589e07 | 130 | static Lisp_Object menu_items; |
18686d47 | 131 | |
78589e07 RS |
132 | /* Number of slots currently allocated in menu_items. */ |
133 | static int menu_items_allocated; | |
18686d47 | 134 | |
78589e07 RS |
135 | /* This is the index in menu_items of the first empty slot. */ |
136 | static int menu_items_used; | |
18686d47 | 137 | |
101bb4a5 RS |
138 | /* The number of panes currently recorded in menu_items, |
139 | excluding those within submenus. */ | |
78589e07 | 140 | static int menu_items_n_panes; |
18686d47 | 141 | |
101bb4a5 RS |
142 | /* Current depth within submenus. */ |
143 | static int menu_items_submenu_depth; | |
144 | ||
4dedbfe0 | 145 | /* Flag which when set indicates a dialog or menu has been posted by |
c98fcf4b | 146 | Xt on behalf of one of the widget sets. */ |
4dedbfe0 PR |
147 | static int popup_activated_flag; |
148 | ||
149 | \f | |
78589e07 RS |
150 | /* Initialize the menu_items structure if we haven't already done so. |
151 | Also mark it as currently empty. */ | |
152 | ||
153 | static void | |
154 | init_menu_items () | |
155 | { | |
156 | if (NILP (menu_items)) | |
157 | { | |
158 | menu_items_allocated = 60; | |
159 | menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil); | |
18686d47 RS |
160 | } |
161 | ||
78589e07 RS |
162 | menu_items_used = 0; |
163 | menu_items_n_panes = 0; | |
101bb4a5 | 164 | menu_items_submenu_depth = 0; |
78589e07 | 165 | } |
18686d47 | 166 | |
78589e07 RS |
167 | /* Call at the end of generating the data in menu_items. |
168 | This fills in the number of items in the last pane. */ | |
1658603c | 169 | |
78589e07 RS |
170 | static void |
171 | finish_menu_items () | |
172 | { | |
173 | } | |
1658603c | 174 | |
78589e07 RS |
175 | /* Call when finished using the data for the current menu |
176 | in menu_items. */ | |
1658603c | 177 | |
78589e07 RS |
178 | static void |
179 | discard_menu_items () | |
180 | { | |
181 | /* Free the structure if it is especially large. | |
182 | Otherwise, hold on to it, to save time. */ | |
183 | if (menu_items_allocated > 200) | |
184 | { | |
185 | menu_items = Qnil; | |
186 | menu_items_allocated = 0; | |
187 | } | |
188 | } | |
1658603c | 189 | |
101bb4a5 RS |
190 | /* Make the menu_items vector twice as large. */ |
191 | ||
192 | static void | |
193 | grow_menu_items () | |
194 | { | |
195 | Lisp_Object old; | |
196 | int old_size = menu_items_allocated; | |
197 | old = menu_items; | |
198 | ||
199 | menu_items_allocated *= 2; | |
200 | menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil); | |
201 | bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents, | |
202 | old_size * sizeof (Lisp_Object)); | |
203 | } | |
204 | ||
205 | /* Begin a submenu. */ | |
206 | ||
207 | static void | |
208 | push_submenu_start () | |
209 | { | |
210 | if (menu_items_used + 1 > menu_items_allocated) | |
211 | grow_menu_items (); | |
212 | ||
213 | XVECTOR (menu_items)->contents[menu_items_used++] = Qnil; | |
214 | menu_items_submenu_depth++; | |
215 | } | |
216 | ||
217 | /* End a submenu. */ | |
218 | ||
219 | static void | |
220 | push_submenu_end () | |
221 | { | |
222 | if (menu_items_used + 1 > menu_items_allocated) | |
223 | grow_menu_items (); | |
224 | ||
225 | XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda; | |
226 | menu_items_submenu_depth--; | |
227 | } | |
228 | ||
fcaa7665 RS |
229 | /* Indicate boundary between left and right. */ |
230 | ||
231 | static void | |
232 | push_left_right_boundary () | |
233 | { | |
234 | if (menu_items_used + 1 > menu_items_allocated) | |
235 | grow_menu_items (); | |
236 | ||
237 | XVECTOR (menu_items)->contents[menu_items_used++] = Qquote; | |
238 | } | |
239 | ||
78589e07 RS |
240 | /* Start a new menu pane in menu_items.. |
241 | NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */ | |
1658603c | 242 | |
78589e07 RS |
243 | static void |
244 | push_menu_pane (name, prefix_vec) | |
245 | Lisp_Object name, prefix_vec; | |
246 | { | |
247 | if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated) | |
101bb4a5 | 248 | grow_menu_items (); |
dcfdbac7 | 249 | |
101bb4a5 RS |
250 | if (menu_items_submenu_depth == 0) |
251 | menu_items_n_panes++; | |
78589e07 RS |
252 | XVECTOR (menu_items)->contents[menu_items_used++] = Qt; |
253 | XVECTOR (menu_items)->contents[menu_items_used++] = name; | |
254 | XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec; | |
255 | } | |
dcfdbac7 | 256 | |
78589e07 RS |
257 | /* Push one menu item into the current pane. |
258 | NAME is the string to display. ENABLE if non-nil means | |
259 | this item can be selected. KEY is the key generated by | |
260 | choosing this item. EQUIV is the textual description | |
261 | of the keyboard equivalent for this item (or nil if none). */ | |
18686d47 | 262 | |
78589e07 RS |
263 | static void |
264 | push_menu_item (name, enable, key, equiv) | |
265 | Lisp_Object name, enable, key, equiv; | |
266 | { | |
267 | if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated) | |
101bb4a5 | 268 | grow_menu_items (); |
088831f6 | 269 | |
78589e07 RS |
270 | XVECTOR (menu_items)->contents[menu_items_used++] = name; |
271 | XVECTOR (menu_items)->contents[menu_items_used++] = enable; | |
272 | XVECTOR (menu_items)->contents[menu_items_used++] = key; | |
273 | XVECTOR (menu_items)->contents[menu_items_used++] = equiv; | |
274 | } | |
275 | \f | |
276 | /* Figure out the current keyboard equivalent of a menu item ITEM1. | |
277 | The item string for menu display should be ITEM_STRING. | |
278 | Store the equivalent keyboard key sequence's | |
279 | textual description into *DESCRIP_PTR. | |
280 | Also cache them in the item itself. | |
281 | Return the real definition to execute. */ | |
088831f6 | 282 | |
78589e07 RS |
283 | static Lisp_Object |
284 | menu_item_equiv_key (item_string, item1, descrip_ptr) | |
285 | Lisp_Object item_string; | |
286 | Lisp_Object item1; | |
287 | Lisp_Object *descrip_ptr; | |
288 | { | |
289 | /* This is the real definition--the function to run. */ | |
290 | Lisp_Object def; | |
291 | /* This is the sublist that records cached equiv key data | |
292 | so we can save time. */ | |
293 | Lisp_Object cachelist; | |
294 | /* These are the saved equivalent keyboard key sequence | |
295 | and its key-description. */ | |
296 | Lisp_Object savedkey, descrip; | |
297 | Lisp_Object def1; | |
298 | int changed = 0; | |
8c512fcb | 299 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; |
088831f6 | 300 | |
78589e07 RS |
301 | /* If a help string follows the item string, skip it. */ |
302 | if (CONSP (XCONS (item1)->cdr) | |
303 | && STRINGP (XCONS (XCONS (item1)->cdr)->car)) | |
304 | item1 = XCONS (item1)->cdr; | |
088831f6 | 305 | |
78589e07 | 306 | def = Fcdr (item1); |
088831f6 | 307 | |
78589e07 RS |
308 | /* Get out the saved equivalent-keyboard-key info. */ |
309 | cachelist = savedkey = descrip = Qnil; | |
310 | if (CONSP (def) && CONSP (XCONS (def)->car) | |
311 | && (NILP (XCONS (XCONS (def)->car)->car) | |
312 | || VECTORP (XCONS (XCONS (def)->car)->car))) | |
088831f6 | 313 | { |
78589e07 RS |
314 | cachelist = XCONS (def)->car; |
315 | def = XCONS (def)->cdr; | |
316 | savedkey = XCONS (cachelist)->car; | |
317 | descrip = XCONS (cachelist)->cdr; | |
088831f6 | 318 | } |
78589e07 | 319 | |
8c512fcb RS |
320 | GCPRO4 (def, def1, savedkey, descrip); |
321 | ||
78589e07 RS |
322 | /* Is it still valid? */ |
323 | def1 = Qnil; | |
324 | if (!NILP (savedkey)) | |
325 | def1 = Fkey_binding (savedkey, Qnil); | |
326 | /* If not, update it. */ | |
327 | if (! EQ (def1, def) | |
cc1032b3 RS |
328 | /* If the command is an alias for another |
329 | (such as easymenu.el and lmenu.el set it up), | |
c98fcf4b | 330 | check if the original command matches the cached command. */ |
cc1032b3 RS |
331 | && !(SYMBOLP (def) && SYMBOLP (XSYMBOL (def)->function) |
332 | && EQ (def1, XSYMBOL (def)->function)) | |
78589e07 RS |
333 | /* If something had no key binding before, don't recheck it-- |
334 | doing that takes too much time and makes menus too slow. */ | |
335 | && !(!NILP (cachelist) && NILP (savedkey))) | |
088831f6 | 336 | { |
78589e07 RS |
337 | changed = 1; |
338 | descrip = Qnil; | |
339 | savedkey = Fwhere_is_internal (def, Qnil, Qt, Qnil); | |
10bba266 RS |
340 | /* If the command is an alias for another |
341 | (such as easymenu.el and lmenu.el set it up), | |
342 | see if the original command name has equivalent keys. */ | |
343 | if (SYMBOLP (def) && SYMBOLP (XSYMBOL (def)->function)) | |
344 | savedkey = Fwhere_is_internal (XSYMBOL (def)->function, | |
345 | Qnil, Qt, Qnil); | |
346 | ||
78589e07 RS |
347 | if (VECTORP (savedkey) |
348 | && EQ (XVECTOR (savedkey)->contents[0], Qmenu_bar)) | |
349 | savedkey = Qnil; | |
92280f67 RS |
350 | /* Reject two-key sequences that start with a mouse click. |
351 | These are probably menu items. */ | |
352 | if (VECTORP (savedkey) | |
353 | && XVECTOR (savedkey)->size > 1 | |
354 | && SYMBOLP (XVECTOR (savedkey)->contents[0])) | |
355 | { | |
356 | Lisp_Object tem; | |
357 | ||
358 | tem = Fget (XVECTOR (savedkey)->contents[0], Qevent_kind); | |
359 | if (EQ (tem, Qmouse_click)) | |
360 | savedkey = Qnil; | |
361 | } | |
78589e07 RS |
362 | if (!NILP (savedkey)) |
363 | { | |
364 | descrip = Fkey_description (savedkey); | |
365 | descrip = concat2 (make_string (" (", 3), descrip); | |
366 | descrip = concat2 (descrip, make_string (")", 1)); | |
367 | } | |
dcfdbac7 | 368 | } |
18686d47 | 369 | |
78589e07 RS |
370 | /* Cache the data we just got in a sublist of the menu binding. */ |
371 | if (NILP (cachelist)) | |
62010a73 RS |
372 | { |
373 | CHECK_IMPURE (item1); | |
374 | XCONS (item1)->cdr = Fcons (Fcons (savedkey, descrip), def); | |
375 | } | |
78589e07 | 376 | else if (changed) |
dcfdbac7 | 377 | { |
78589e07 RS |
378 | XCONS (cachelist)->car = savedkey; |
379 | XCONS (cachelist)->cdr = descrip; | |
dcfdbac7 | 380 | } |
18686d47 | 381 | |
8c512fcb | 382 | UNGCPRO; |
78589e07 RS |
383 | *descrip_ptr = descrip; |
384 | return def; | |
18686d47 RS |
385 | } |
386 | ||
78589e07 RS |
387 | /* This is used as the handler when calling internal_condition_case_1. */ |
388 | ||
389 | static Lisp_Object | |
390 | menu_item_enabled_p_1 (arg) | |
391 | Lisp_Object arg; | |
18686d47 | 392 | { |
78589e07 | 393 | return Qnil; |
dcfdbac7 JB |
394 | } |
395 | ||
78589e07 | 396 | /* Return non-nil if the command DEF is enabled when used as a menu item. |
101bb4a5 RS |
397 | This is based on looking for a menu-enable property. |
398 | If NOTREAL is set, don't bother really computing this. */ | |
78589e07 RS |
399 | |
400 | static Lisp_Object | |
101bb4a5 | 401 | menu_item_enabled_p (def, notreal) |
78589e07 | 402 | Lisp_Object def; |
0f5e911d | 403 | int notreal; |
18686d47 | 404 | { |
78589e07 | 405 | Lisp_Object enabled, tem; |
18686d47 | 406 | |
78589e07 | 407 | enabled = Qt; |
101bb4a5 RS |
408 | if (notreal) |
409 | return enabled; | |
b5bb2705 | 410 | if (SYMBOLP (def)) |
78589e07 RS |
411 | { |
412 | /* No property, or nil, means enable. | |
413 | Otherwise, enable if value is not nil. */ | |
414 | tem = Fget (def, Qmenu_enable); | |
415 | if (!NILP (tem)) | |
416 | /* (condition-case nil (eval tem) | |
417 | (error nil)) */ | |
418 | enabled = internal_condition_case_1 (Feval, tem, Qerror, | |
419 | menu_item_enabled_p_1); | |
420 | } | |
421 | return enabled; | |
422 | } | |
423 | \f | |
424 | /* Look through KEYMAPS, a vector of keymaps that is NMAPS long, | |
101bb4a5 RS |
425 | and generate menu panes for them in menu_items. |
426 | If NOTREAL is nonzero, | |
427 | don't bother really computing whether an item is enabled. */ | |
18686d47 | 428 | |
78589e07 | 429 | static void |
101bb4a5 | 430 | keymap_panes (keymaps, nmaps, notreal) |
78589e07 RS |
431 | Lisp_Object *keymaps; |
432 | int nmaps; | |
101bb4a5 | 433 | int notreal; |
18686d47 | 434 | { |
78589e07 | 435 | int mapno; |
18686d47 | 436 | |
78589e07 | 437 | init_menu_items (); |
18686d47 | 438 | |
78589e07 RS |
439 | /* Loop over the given keymaps, making a pane for each map. |
440 | But don't make a pane that is empty--ignore that map instead. | |
441 | P is the number of panes we have made so far. */ | |
442 | for (mapno = 0; mapno < nmaps; mapno++) | |
101bb4a5 | 443 | single_keymap_panes (keymaps[mapno], Qnil, Qnil, notreal); |
78589e07 RS |
444 | |
445 | finish_menu_items (); | |
446 | } | |
447 | ||
448 | /* This is a recursive subroutine of keymap_panes. | |
449 | It handles one keymap, KEYMAP. | |
450 | The other arguments are passed along | |
101bb4a5 RS |
451 | or point to local variables of the previous function. |
452 | If NOTREAL is nonzero, | |
453 | don't bother really computing whether an item is enabled. */ | |
78589e07 RS |
454 | |
455 | static void | |
101bb4a5 | 456 | single_keymap_panes (keymap, pane_name, prefix, notreal) |
78589e07 RS |
457 | Lisp_Object keymap; |
458 | Lisp_Object pane_name; | |
459 | Lisp_Object prefix; | |
101bb4a5 | 460 | int notreal; |
78589e07 RS |
461 | { |
462 | Lisp_Object pending_maps; | |
463 | Lisp_Object tail, item, item1, item_string, table; | |
464 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | |
465 | ||
466 | pending_maps = Qnil; | |
467 | ||
468 | push_menu_pane (pane_name, prefix); | |
469 | ||
b5bb2705 | 470 | for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr) |
18686d47 | 471 | { |
78589e07 RS |
472 | /* Look at each key binding, and if it has a menu string, |
473 | make a menu item from it. */ | |
474 | item = XCONS (tail)->car; | |
b5bb2705 | 475 | if (CONSP (item)) |
18686d47 | 476 | { |
78589e07 | 477 | item1 = XCONS (item)->cdr; |
b5bb2705 | 478 | if (CONSP (item1)) |
78589e07 RS |
479 | { |
480 | item_string = XCONS (item1)->car; | |
b5bb2705 | 481 | if (STRINGP (item_string)) |
78589e07 RS |
482 | { |
483 | /* This is the real definition--the function to run. */ | |
484 | Lisp_Object def; | |
485 | /* These are the saved equivalent keyboard key sequence | |
486 | and its key-description. */ | |
487 | Lisp_Object descrip; | |
488 | Lisp_Object tem, enabled; | |
489 | ||
8c512fcb RS |
490 | /* GCPRO because ...enabled_p will call eval |
491 | and ..._equiv_key may autoload something. | |
78589e07 RS |
492 | Protecting KEYMAP preserves everything we use; |
493 | aside from that, must protect whatever might be | |
494 | a string. Since there's no GCPRO5, we refetch | |
495 | item_string instead of protecting it. */ | |
8c512fcb | 496 | descrip = def = Qnil; |
78589e07 | 497 | GCPRO4 (keymap, pending_maps, def, descrip); |
8c512fcb RS |
498 | |
499 | def = menu_item_equiv_key (item_string, item1, &descrip); | |
101bb4a5 RS |
500 | enabled = menu_item_enabled_p (def, notreal); |
501 | ||
78589e07 RS |
502 | UNGCPRO; |
503 | ||
504 | item_string = XCONS (item1)->car; | |
505 | ||
506 | tem = Fkeymapp (def); | |
507 | if (XSTRING (item_string)->data[0] == '@' && !NILP (tem)) | |
508 | pending_maps = Fcons (Fcons (def, Fcons (item_string, XCONS (item)->car)), | |
509 | pending_maps); | |
510 | else | |
101bb4a5 RS |
511 | { |
512 | Lisp_Object submap; | |
bc28c440 | 513 | GCPRO4 (keymap, pending_maps, descrip, item_string); |
101bb4a5 | 514 | submap = get_keymap_1 (def, 0, 1); |
bc28c440 | 515 | UNGCPRO; |
101bb4a5 RS |
516 | #ifndef USE_X_TOOLKIT |
517 | /* Indicate visually that this is a submenu. */ | |
518 | if (!NILP (submap)) | |
519 | item_string = concat2 (item_string, | |
520 | build_string (" >")); | |
521 | #endif | |
522 | push_menu_item (item_string, enabled, XCONS (item)->car, | |
523 | descrip); | |
524 | #ifdef USE_X_TOOLKIT | |
525 | /* Display a submenu using the toolkit. */ | |
526 | if (! NILP (submap)) | |
527 | { | |
528 | push_submenu_start (); | |
529 | single_keymap_panes (submap, Qnil, | |
530 | XCONS (item)->car, notreal); | |
531 | push_submenu_end (); | |
532 | } | |
533 | #endif | |
534 | } | |
78589e07 RS |
535 | } |
536 | } | |
537 | } | |
b5bb2705 | 538 | else if (VECTORP (item)) |
78589e07 RS |
539 | { |
540 | /* Loop over the char values represented in the vector. */ | |
541 | int len = XVECTOR (item)->size; | |
542 | int c; | |
543 | for (c = 0; c < len; c++) | |
544 | { | |
545 | Lisp_Object character; | |
33b43fa6 | 546 | XSETFASTINT (character, c); |
78589e07 | 547 | item1 = XVECTOR (item)->contents[c]; |
b5bb2705 | 548 | if (CONSP (item1)) |
78589e07 RS |
549 | { |
550 | item_string = XCONS (item1)->car; | |
b5bb2705 | 551 | if (STRINGP (item_string)) |
78589e07 RS |
552 | { |
553 | Lisp_Object def; | |
554 | ||
555 | /* These are the saved equivalent keyboard key sequence | |
556 | and its key-description. */ | |
557 | Lisp_Object descrip; | |
558 | Lisp_Object tem, enabled; | |
559 | ||
8c512fcb RS |
560 | /* GCPRO because ...enabled_p will call eval |
561 | and ..._equiv_key may autoload something. | |
78589e07 RS |
562 | Protecting KEYMAP preserves everything we use; |
563 | aside from that, must protect whatever might be | |
564 | a string. Since there's no GCPRO5, we refetch | |
565 | item_string instead of protecting it. */ | |
566 | GCPRO4 (keymap, pending_maps, def, descrip); | |
8c512fcb RS |
567 | descrip = def = Qnil; |
568 | ||
569 | def = menu_item_equiv_key (item_string, item1, &descrip); | |
101bb4a5 | 570 | enabled = menu_item_enabled_p (def, notreal); |
8c512fcb | 571 | |
78589e07 RS |
572 | UNGCPRO; |
573 | ||
574 | item_string = XCONS (item1)->car; | |
575 | ||
576 | tem = Fkeymapp (def); | |
577 | if (XSTRING (item_string)->data[0] == '@' && !NILP (tem)) | |
578 | pending_maps = Fcons (Fcons (def, Fcons (item_string, character)), | |
579 | pending_maps); | |
580 | else | |
101bb4a5 RS |
581 | { |
582 | Lisp_Object submap; | |
bc28c440 | 583 | GCPRO4 (keymap, pending_maps, descrip, item_string); |
101bb4a5 | 584 | submap = get_keymap_1 (def, 0, 1); |
bc28c440 | 585 | UNGCPRO; |
101bb4a5 RS |
586 | #ifndef USE_X_TOOLKIT |
587 | if (!NILP (submap)) | |
588 | item_string = concat2 (item_string, | |
589 | build_string (" >")); | |
590 | #endif | |
591 | push_menu_item (item_string, enabled, character, | |
592 | descrip); | |
593 | #ifdef USE_X_TOOLKIT | |
594 | if (! NILP (submap)) | |
595 | { | |
596 | push_submenu_start (); | |
597 | single_keymap_panes (submap, Qnil, | |
598 | character, notreal); | |
599 | push_submenu_end (); | |
600 | } | |
601 | #endif | |
602 | } | |
78589e07 RS |
603 | } |
604 | } | |
605 | } | |
18686d47 RS |
606 | } |
607 | } | |
78589e07 RS |
608 | |
609 | /* Process now any submenus which want to be panes at this level. */ | |
610 | while (!NILP (pending_maps)) | |
611 | { | |
101bb4a5 | 612 | Lisp_Object elt, eltcdr, string; |
78589e07 RS |
613 | elt = Fcar (pending_maps); |
614 | eltcdr = XCONS (elt)->cdr; | |
101bb4a5 RS |
615 | string = XCONS (eltcdr)->car; |
616 | /* We no longer discard the @ from the beginning of the string here. | |
617 | Instead, we do this in xmenu_show. */ | |
618 | single_keymap_panes (Fcar (elt), string, | |
619 | XCONS (eltcdr)->cdr, notreal); | |
78589e07 RS |
620 | pending_maps = Fcdr (pending_maps); |
621 | } | |
18686d47 | 622 | } |
78589e07 RS |
623 | \f |
624 | /* Push all the panes and items of a menu decsribed by the | |
625 | alist-of-alists MENU. | |
626 | This handles old-fashioned calls to x-popup-menu. */ | |
18686d47 | 627 | |
78589e07 RS |
628 | static void |
629 | list_of_panes (menu) | |
18686d47 | 630 | Lisp_Object menu; |
18686d47 | 631 | { |
78589e07 RS |
632 | Lisp_Object tail; |
633 | ||
634 | init_menu_items (); | |
635 | ||
636 | for (tail = menu; !NILP (tail); tail = Fcdr (tail)) | |
637 | { | |
638 | Lisp_Object elt, pane_name, pane_data; | |
639 | elt = Fcar (tail); | |
640 | pane_name = Fcar (elt); | |
641 | CHECK_STRING (pane_name, 0); | |
642 | push_menu_pane (pane_name, Qnil); | |
643 | pane_data = Fcdr (elt); | |
644 | CHECK_CONS (pane_data, 0); | |
645 | list_of_items (pane_data); | |
646 | } | |
647 | ||
648 | finish_menu_items (); | |
649 | } | |
650 | ||
651 | /* Push the items in a single pane defined by the alist PANE. */ | |
652 | ||
653 | static void | |
654 | list_of_items (pane) | |
655 | Lisp_Object pane; | |
656 | { | |
657 | Lisp_Object tail, item, item1; | |
658 | ||
659 | for (tail = pane; !NILP (tail); tail = Fcdr (tail)) | |
660 | { | |
661 | item = Fcar (tail); | |
662 | if (STRINGP (item)) | |
2436c029 | 663 | push_menu_item (item, Qnil, Qnil, Qnil); |
fcaa7665 RS |
664 | else if (NILP (item)) |
665 | push_left_right_boundary (); | |
78589e07 RS |
666 | else |
667 | { | |
668 | CHECK_CONS (item, 0); | |
669 | item1 = Fcar (item); | |
670 | CHECK_STRING (item1, 1); | |
671 | push_menu_item (item1, Qt, Fcdr (item), Qnil); | |
672 | } | |
673 | } | |
674 | } | |
675 | \f | |
540e52d1 | 676 | DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0, |
78589e07 RS |
677 | "Pop up a deck-of-cards menu and return user's selection.\n\ |
678 | POSITION is a position specification. This is either a mouse button event\n\ | |
679 | or a list ((XOFFSET YOFFSET) WINDOW)\n\ | |
748a0e1f | 680 | where XOFFSET and YOFFSET are positions in pixels from the top left\n\ |
78589e07 RS |
681 | corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\ |
682 | This controls the position of the center of the first line\n\ | |
683 | in the first pane of the menu, not the top left of the menu as a whole.\n\ | |
684 | If POSITION is t, it means to use the current mouse position.\n\ | |
685 | \n\ | |
686 | MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\ | |
687 | The menu items come from key bindings that have a menu string as well as\n\ | |
688 | a definition; actually, the \"definition\" in such a key binding looks like\n\ | |
689 | \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\ | |
690 | the keymap as a top-level element.\n\n\ | |
691 | You can also use a list of keymaps as MENU.\n\ | |
692 | Then each keymap makes a separate pane.\n\ | |
693 | When MENU is a keymap or a list of keymaps, the return value\n\ | |
694 | is a list of events.\n\n\ | |
695 | Alternatively, you can specify a menu of multiple panes\n\ | |
696 | with a list of the form (TITLE PANE1 PANE2...),\n\ | |
697 | where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\ | |
698 | Each ITEM is normally a cons cell (STRING . VALUE);\n\ | |
699 | but a string can appear as an item--that makes a nonselectable line\n\ | |
700 | in the menu.\n\ | |
701 | With this form of menu, the return value is VALUE from the chosen item.\n\ | |
702 | \n\ | |
703 | If POSITION is nil, don't display the menu at all, just precalculate the\n\ | |
704 | cached information about equivalent key sequences.") | |
705 | (position, menu) | |
706 | Lisp_Object position, menu; | |
707 | { | |
708 | int number_of_panes, panes; | |
18686d47 | 709 | Lisp_Object keymap, tem; |
78589e07 RS |
710 | int xpos, ypos; |
711 | Lisp_Object title; | |
712 | char *error_name; | |
713 | Lisp_Object selection; | |
18686d47 | 714 | int i, j; |
78589e07 RS |
715 | FRAME_PTR f; |
716 | Lisp_Object x, y, window; | |
717 | int keymaps = 0; | |
718 | int menubarp = 0; | |
719 | struct gcpro gcpro1; | |
720 | ||
78589e07 RS |
721 | if (! NILP (position)) |
722 | { | |
101bb4a5 RS |
723 | check_x (); |
724 | ||
78589e07 RS |
725 | /* Decode the first argument: find the window and the coordinates. */ |
726 | if (EQ (position, Qt)) | |
727 | { | |
728 | /* Use the mouse's current position. */ | |
5ca2ef64 | 729 | FRAME_PTR new_f = 0; |
78589e07 RS |
730 | Lisp_Object bar_window; |
731 | int part; | |
732 | unsigned long time; | |
733 | ||
b137c582 KH |
734 | if (mouse_position_hook) |
735 | (*mouse_position_hook) (&new_f, &bar_window, &part, &x, &y, &time); | |
5ca2ef64 | 736 | if (new_f != 0) |
a39f0477 | 737 | XSETFRAME (window, new_f); |
5ca2ef64 RS |
738 | else |
739 | { | |
740 | window = selected_window; | |
33b43fa6 KH |
741 | XSETFASTINT (x, 0); |
742 | XSETFASTINT (y, 0); | |
5ca2ef64 | 743 | } |
78589e07 RS |
744 | } |
745 | else | |
746 | { | |
747 | tem = Fcar (position); | |
b5bb2705 | 748 | if (CONSP (tem)) |
78589e07 RS |
749 | { |
750 | window = Fcar (Fcdr (position)); | |
751 | x = Fcar (tem); | |
752 | y = Fcar (Fcdr (tem)); | |
753 | } | |
754 | else | |
755 | { | |
756 | tem = Fcar (Fcdr (position)); /* EVENT_START (position) */ | |
757 | window = Fcar (tem); /* POSN_WINDOW (tem) */ | |
758 | tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */ | |
759 | x = Fcar (tem); | |
760 | y = Fcdr (tem); | |
761 | ||
762 | /* Determine whether this menu is handling a menu bar click. */ | |
763 | tem = Fcar (Fcdr (Fcar (Fcdr (position)))); | |
adb0462c | 764 | if (CONSP (tem) && EQ (Fcar (tem), Qmenu_bar)) |
78589e07 RS |
765 | menubarp = 1; |
766 | } | |
767 | } | |
768 | ||
769 | CHECK_NUMBER (x, 0); | |
770 | CHECK_NUMBER (y, 0); | |
771 | ||
772 | /* Decode where to put the menu. */ | |
773 | ||
b5bb2705 | 774 | if (FRAMEP (window)) |
78589e07 RS |
775 | { |
776 | f = XFRAME (window); | |
78589e07 RS |
777 | xpos = 0; |
778 | ypos = 0; | |
779 | } | |
b5bb2705 | 780 | else if (WINDOWP (window)) |
78589e07 RS |
781 | { |
782 | CHECK_LIVE_WINDOW (window, 0); | |
783 | f = XFRAME (WINDOW_FRAME (XWINDOW (window))); | |
784 | ||
785 | xpos = (FONT_WIDTH (f->display.x->font) * XWINDOW (window)->left); | |
a8097b6c | 786 | ypos = (f->display.x->line_height * XWINDOW (window)->top); |
78589e07 RS |
787 | } |
788 | else | |
789 | /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME, | |
790 | but I don't want to make one now. */ | |
791 | CHECK_WINDOW (window, 0); | |
792 | ||
793 | xpos += XINT (x); | |
794 | ypos += XINT (y); | |
795 | } | |
796 | ||
797 | title = Qnil; | |
798 | GCPRO1 (title); | |
799 | ||
800 | /* Decode the menu items from what was specified. */ | |
18686d47 RS |
801 | |
802 | keymap = Fkeymapp (menu); | |
803 | tem = Qnil; | |
b5bb2705 | 804 | if (CONSP (menu)) |
18686d47 RS |
805 | tem = Fkeymapp (Fcar (menu)); |
806 | if (!NILP (keymap)) | |
807 | { | |
808 | /* We were given a keymap. Extract menu info from the keymap. */ | |
809 | Lisp_Object prompt; | |
810 | keymap = get_keymap (menu); | |
811 | ||
78589e07 | 812 | /* Extract the detailed info to make one pane. */ |
101bb4a5 | 813 | keymap_panes (&menu, 1, NILP (position)); |
78589e07 | 814 | |
18686d47 RS |
815 | /* Search for a string appearing directly as an element of the keymap. |
816 | That string is the title of the menu. */ | |
817 | prompt = map_prompt (keymap); | |
18686d47 | 818 | |
78589e07 RS |
819 | /* Make that be the pane title of the first pane. */ |
820 | if (!NILP (prompt) && menu_items_n_panes >= 0) | |
821 | XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt; | |
822 | ||
823 | keymaps = 1; | |
18686d47 RS |
824 | } |
825 | else if (!NILP (tem)) | |
826 | { | |
827 | /* We were given a list of keymaps. */ | |
18686d47 RS |
828 | int nmaps = XFASTINT (Flength (menu)); |
829 | Lisp_Object *maps | |
830 | = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object)); | |
831 | int i; | |
78589e07 RS |
832 | |
833 | title = Qnil; | |
18686d47 RS |
834 | |
835 | /* The first keymap that has a prompt string | |
836 | supplies the menu title. */ | |
b5bb2705 | 837 | for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem)) |
18686d47 | 838 | { |
78589e07 RS |
839 | Lisp_Object prompt; |
840 | ||
18686d47 RS |
841 | maps[i++] = keymap = get_keymap (Fcar (tem)); |
842 | ||
843 | prompt = map_prompt (keymap); | |
78589e07 RS |
844 | if (NILP (title) && !NILP (prompt)) |
845 | title = prompt; | |
18686d47 RS |
846 | } |
847 | ||
848 | /* Extract the detailed info to make one pane. */ | |
101bb4a5 | 849 | keymap_panes (maps, nmaps, NILP (position)); |
78589e07 RS |
850 | |
851 | /* Make the title be the pane title of the first pane. */ | |
852 | if (!NILP (title) && menu_items_n_panes >= 0) | |
853 | XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title; | |
854 | ||
855 | keymaps = 1; | |
18686d47 RS |
856 | } |
857 | else | |
858 | { | |
859 | /* We were given an old-fashioned menu. */ | |
78589e07 RS |
860 | title = Fcar (menu); |
861 | CHECK_STRING (title, 1); | |
18686d47 | 862 | |
78589e07 | 863 | list_of_panes (Fcdr (menu)); |
18686d47 | 864 | |
78589e07 RS |
865 | keymaps = 0; |
866 | } | |
18686d47 | 867 | |
78589e07 | 868 | if (NILP (position)) |
18686d47 | 869 | { |
78589e07 RS |
870 | discard_menu_items (); |
871 | UNGCPRO; | |
872 | return Qnil; | |
18686d47 RS |
873 | } |
874 | ||
78589e07 RS |
875 | /* Display them in a menu. */ |
876 | BLOCK_INPUT; | |
18686d47 | 877 | |
78589e07 RS |
878 | selection = xmenu_show (f, xpos, ypos, menubarp, |
879 | keymaps, title, &error_name); | |
880 | UNBLOCK_INPUT; | |
18686d47 | 881 | |
78589e07 | 882 | discard_menu_items (); |
18686d47 | 883 | |
78589e07 | 884 | UNGCPRO; |
18686d47 | 885 | |
78589e07 RS |
886 | if (error_name) error (error_name); |
887 | return selection; | |
18686d47 | 888 | } |
165e1749 | 889 | |
540e52d1 | 890 | DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0, |
165e1749 | 891 | "Pop up a dialog box and return user's selection.\n\ |
99fe880d RS |
892 | POSITION specifies which frame to use.\n\ |
893 | This is normally a mouse button event or a window or frame.\n\ | |
894 | If POSITION is t, it means to use the frame the mouse is on.\n\ | |
895 | The dialog box appears in the middle of the specified frame.\n\ | |
165e1749 | 896 | \n\ |
99fe880d RS |
897 | CONTENTS specifies the alternatives to display in the dialog box.\n\ |
898 | It is a list of the form (TITLE ITEM1 ITEM2...).\n\ | |
899 | Each ITEM is a cons cell (STRING . VALUE).\n\ | |
fcaa7665 RS |
900 | The return value is VALUE from the chosen item.\n\n\ |
901 | An ITEM may also be just a string--that makes a nonselectable item.\n\ | |
902 | An ITEM may also be nil--that means to put all preceding items\n\ | |
903 | on the left of the dialog box and all following items on the right.\n\ | |
904 | \(By default, approximately half appear on each side.)") | |
99fe880d RS |
905 | (position, contents) |
906 | Lisp_Object position, contents; | |
165e1749 | 907 | { |
165e1749 | 908 | FRAME_PTR f; |
99fe880d | 909 | Lisp_Object window; |
165e1749 FP |
910 | |
911 | check_x (); | |
912 | ||
99fe880d RS |
913 | /* Decode the first argument: find the window or frame to use. */ |
914 | if (EQ (position, Qt)) | |
165e1749 | 915 | { |
b14db4d7 | 916 | #if 0 /* Using the frame the mouse is on may not be right. */ |
99fe880d RS |
917 | /* Use the mouse's current position. */ |
918 | FRAME_PTR new_f = 0; | |
919 | Lisp_Object bar_window; | |
920 | int part; | |
921 | unsigned long time; | |
922 | Lisp_Object x, y; | |
165e1749 | 923 | |
99fe880d | 924 | (*mouse_position_hook) (&new_f, &bar_window, &part, &x, &y, &time); |
5ca2ef64 | 925 | |
99fe880d | 926 | if (new_f != 0) |
a39f0477 | 927 | XSETFRAME (window, new_f); |
99fe880d RS |
928 | else |
929 | window = selected_window; | |
b14db4d7 RS |
930 | #endif |
931 | /* Decode the first argument: find the window and the coordinates. */ | |
932 | if (EQ (position, Qt)) | |
5a7d50ba | 933 | window = selected_window; |
99fe880d RS |
934 | } |
935 | else if (CONSP (position)) | |
936 | { | |
937 | Lisp_Object tem; | |
938 | tem = Fcar (position); | |
b5bb2705 | 939 | if (CONSP (tem)) |
99fe880d | 940 | window = Fcar (Fcdr (position)); |
80670155 RS |
941 | else |
942 | { | |
99fe880d RS |
943 | tem = Fcar (Fcdr (position)); /* EVENT_START (position) */ |
944 | window = Fcar (tem); /* POSN_WINDOW (tem) */ | |
165e1749 | 945 | } |
165e1749 | 946 | } |
99fe880d RS |
947 | else if (WINDOWP (position) || FRAMEP (position)) |
948 | window = position; | |
165e1749 | 949 | |
99fe880d | 950 | /* Decode where to put the menu. */ |
165e1749 | 951 | |
b5bb2705 | 952 | if (FRAMEP (window)) |
99fe880d | 953 | f = XFRAME (window); |
b5bb2705 | 954 | else if (WINDOWP (window)) |
165e1749 | 955 | { |
99fe880d RS |
956 | CHECK_LIVE_WINDOW (window, 0); |
957 | f = XFRAME (WINDOW_FRAME (XWINDOW (window))); | |
165e1749 | 958 | } |
99fe880d RS |
959 | else |
960 | /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME, | |
961 | but I don't want to make one now. */ | |
962 | CHECK_WINDOW (window, 0); | |
165e1749 | 963 | |
99fe880d RS |
964 | #ifndef USE_X_TOOLKIT |
965 | /* Display a menu with these alternatives | |
966 | in the middle of frame F. */ | |
967 | { | |
968 | Lisp_Object x, y, frame, newpos; | |
a39f0477 KH |
969 | XSETFRAME (frame, f); |
970 | XSETINT (x, x_pixel_width (f) / 2); | |
971 | XSETINT (y, x_pixel_height (f) / 2); | |
99fe880d RS |
972 | newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil)); |
973 | ||
974 | return Fx_popup_menu (newpos, | |
975 | Fcons (Fcar (contents), Fcons (contents, Qnil))); | |
976 | } | |
977 | #else | |
978 | { | |
979 | Lisp_Object title; | |
980 | char *error_name; | |
981 | Lisp_Object selection; | |
165e1749 | 982 | |
99fe880d RS |
983 | /* Decode the dialog items from what was specified. */ |
984 | title = Fcar (contents); | |
985 | CHECK_STRING (title, 1); | |
165e1749 | 986 | |
99fe880d | 987 | list_of_panes (Fcons (contents, Qnil)); |
165e1749 | 988 | |
99fe880d RS |
989 | /* Display them in a dialog box. */ |
990 | BLOCK_INPUT; | |
991 | selection = xdialog_show (f, 0, 0, title, &error_name); | |
992 | UNBLOCK_INPUT; | |
165e1749 | 993 | |
99fe880d RS |
994 | discard_menu_items (); |
995 | ||
996 | if (error_name) error (error_name); | |
997 | return selection; | |
998 | } | |
7464b131 | 999 | #endif |
392d3f4b | 1000 | } |
78589e07 RS |
1001 | \f |
1002 | #ifdef USE_X_TOOLKIT | |
18686d47 | 1003 | |
4dedbfe0 | 1004 | /* Loop in Xt until the menu pulldown or dialog popup has been |
c98fcf4b PR |
1005 | popped down (deactivated). |
1006 | ||
1007 | NOTE: All calls to popup_get_selection() should be protected | |
1008 | with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */ | |
4dedbfe0 PR |
1009 | void |
1010 | popup_get_selection (initial_event) | |
1011 | XEvent *initial_event; | |
78589e07 | 1012 | { |
4dedbfe0 | 1013 | XEvent event; |
78589e07 | 1014 | |
4dedbfe0 PR |
1015 | if (initial_event) |
1016 | event = *initial_event; | |
1017 | else | |
1018 | XtAppNextEvent (Xt_app_con, &event); | |
78589e07 | 1019 | |
4dedbfe0 | 1020 | while (1) |
78589e07 | 1021 | { |
4dedbfe0 | 1022 | XtDispatchEvent (&event); |
4dedbfe0 PR |
1023 | if (!popup_activated()) |
1024 | break; | |
1025 | XtAppNextEvent (Xt_app_con, &event); | |
78589e07 | 1026 | } |
78589e07 RS |
1027 | } |
1028 | ||
c98fcf4b | 1029 | /* Detect if a dialog or menu has been posted. */ |
4dedbfe0 PR |
1030 | int |
1031 | popup_activated () | |
1032 | { | |
1033 | return popup_activated_flag; | |
1034 | } | |
1035 | ||
1036 | ||
1037 | /* This callback is invoked when the user selects a menubar cascade | |
1038 | pushbutton, but before the pulldown menu is posted. */ | |
78589e07 RS |
1039 | |
1040 | static void | |
4dedbfe0 | 1041 | popup_activate_callback (widget, id, client_data) |
78589e07 RS |
1042 | Widget widget; |
1043 | LWLIB_ID id; | |
1044 | XtPointer client_data; | |
1045 | { | |
4dedbfe0 | 1046 | popup_activated_flag = 1; |
78589e07 RS |
1047 | } |
1048 | ||
4dedbfe0 PR |
1049 | /* This callback is called from the menu bar pulldown menu |
1050 | when the user makes a selection. | |
1051 | Figure out what the user chose | |
1052 | and put the appropriate events into the keyboard buffer. */ | |
1053 | ||
78589e07 | 1054 | static void |
4dedbfe0 | 1055 | menubar_selection_callback (widget, id, client_data) |
78589e07 RS |
1056 | Widget widget; |
1057 | LWLIB_ID id; | |
1058 | XtPointer client_data; | |
1059 | { | |
4dedbfe0 PR |
1060 | Lisp_Object prefix; |
1061 | FRAME_PTR f = (FRAME_PTR) id; | |
1062 | Lisp_Object vector; | |
1063 | Lisp_Object *subprefix_stack; | |
1064 | int submenu_depth = 0; | |
1065 | int i; | |
1066 | ||
1067 | if (!f) | |
1068 | return; | |
1069 | subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object)); | |
1070 | vector = f->menu_bar_vector; | |
1071 | prefix = Qnil; | |
1072 | i = 0; | |
1073 | while (i < f->menu_bar_items_used) | |
1074 | { | |
1075 | Lisp_Object entry; | |
1076 | ||
1077 | if (EQ (XVECTOR (vector)->contents[i], Qnil)) | |
1078 | { | |
1079 | subprefix_stack[submenu_depth++] = prefix; | |
1080 | prefix = entry; | |
1081 | i++; | |
1082 | } | |
1083 | else if (EQ (XVECTOR (vector)->contents[i], Qlambda)) | |
1084 | { | |
1085 | prefix = subprefix_stack[--submenu_depth]; | |
1086 | i++; | |
1087 | } | |
1088 | else if (EQ (XVECTOR (vector)->contents[i], Qt)) | |
1089 | { | |
1090 | prefix | |
1091 | = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX]; | |
1092 | i += MENU_ITEMS_PANE_LENGTH; | |
1093 | } | |
1094 | else | |
1095 | { | |
1096 | entry | |
1097 | = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE]; | |
1098 | if ((int) client_data == i) | |
1099 | { | |
1100 | int j; | |
1101 | struct input_event buf; | |
1102 | ||
1103 | buf.kind = menu_bar_event; | |
1104 | buf.frame_or_window = Qmenu_bar; | |
1105 | kbd_buffer_store_event (&buf); | |
1106 | ||
1107 | for (j = 0; j < submenu_depth; j++) | |
1108 | if (!NILP (subprefix_stack[j])) | |
1109 | { | |
1110 | buf.kind = menu_bar_event; | |
1111 | buf.frame_or_window = subprefix_stack[j]; | |
1112 | kbd_buffer_store_event (&buf); | |
1113 | } | |
1114 | ||
1115 | if (!NILP (prefix)) | |
1116 | { | |
1117 | buf.kind = menu_bar_event; | |
1118 | buf.frame_or_window = prefix; | |
1119 | kbd_buffer_store_event (&buf); | |
1120 | } | |
1121 | ||
1122 | buf.kind = menu_bar_event; | |
1123 | buf.frame_or_window = entry; | |
1124 | kbd_buffer_store_event (&buf); | |
1125 | ||
1126 | return; | |
1127 | } | |
1128 | i += MENU_ITEMS_ITEM_LENGTH; | |
1129 | } | |
1130 | } | |
18686d47 RS |
1131 | } |
1132 | ||
4dedbfe0 | 1133 | /* This callback is invoked when a dialog or menu is finished being |
c98fcf4b | 1134 | used and has been unposted. */ |
4dedbfe0 | 1135 | |
165e1749 | 1136 | static void |
4dedbfe0 | 1137 | popup_deactivate_callback (widget, id, client_data) |
165e1749 FP |
1138 | Widget widget; |
1139 | LWLIB_ID id; | |
1140 | XtPointer client_data; | |
1141 | { | |
4dedbfe0 | 1142 | popup_activated_flag = 0; |
165e1749 FP |
1143 | } |
1144 | ||
4dedbfe0 PR |
1145 | |
1146 | /* This recursively calls free_widget_value on the tree of widgets. | |
18686d47 | 1147 | It must free all data that was malloc'ed for these widget_values. |
78589e07 RS |
1148 | In Emacs, many slots are pointers into the data of Lisp_Strings, and |
1149 | must be left alone. */ | |
1150 | ||
18686d47 RS |
1151 | void |
1152 | free_menubar_widget_value_tree (wv) | |
1153 | widget_value *wv; | |
1154 | { | |
1155 | if (! wv) return; | |
18686d47 RS |
1156 | |
1157 | wv->name = wv->value = wv->key = (char *) 0xDEADBEEF; | |
1158 | ||
1159 | if (wv->contents && (wv->contents != (widget_value*)1)) | |
1160 | { | |
1161 | free_menubar_widget_value_tree (wv->contents); | |
1162 | wv->contents = (widget_value *) 0xDEADBEEF; | |
1163 | } | |
1164 | if (wv->next) | |
1165 | { | |
1166 | free_menubar_widget_value_tree (wv->next); | |
1167 | wv->next = (widget_value *) 0xDEADBEEF; | |
1168 | } | |
1169 | BLOCK_INPUT; | |
1170 | free_widget_value (wv); | |
1171 | UNBLOCK_INPUT; | |
1172 | } | |
4dedbfe0 PR |
1173 | \f |
1174 | /* Return a tree of widget_value structures for a menu bar item | |
1175 | whose event type is ITEM_KEY (with string ITEM_NAME) | |
1176 | and whose contents come from the list of keymaps MAPS. */ | |
1177 | ||
1178 | static widget_value * | |
1179 | single_submenu (item_key, item_name, maps) | |
1180 | Lisp_Object item_key, item_name, maps; | |
1181 | { | |
1182 | widget_value *wv, *prev_wv, *save_wv, *first_wv; | |
1183 | int i; | |
1184 | int submenu_depth = 0; | |
1185 | Lisp_Object length; | |
1186 | int len; | |
1187 | Lisp_Object *mapvec; | |
1188 | widget_value **submenu_stack; | |
1189 | int mapno; | |
1190 | int previous_items = menu_items_used; | |
1191 | ||
1192 | length = Flength (maps); | |
1193 | len = XINT (length); | |
1194 | ||
1195 | /* Convert the list MAPS into a vector MAPVEC. */ | |
1196 | mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object)); | |
1197 | for (i = 0; i < len; i++) | |
1198 | { | |
1199 | mapvec[i] = Fcar (maps); | |
1200 | maps = Fcdr (maps); | |
1201 | } | |
1202 | ||
1203 | menu_items_n_panes = 0; | |
1204 | ||
1205 | /* Loop over the given keymaps, making a pane for each map. | |
1206 | But don't make a pane that is empty--ignore that map instead. */ | |
1207 | for (i = 0; i < len; i++) | |
1208 | single_keymap_panes (mapvec[i], item_name, item_key, 0); | |
1209 | ||
1210 | /* Create a tree of widget_value objects | |
1211 | representing the panes and their items. */ | |
1212 | ||
1213 | submenu_stack | |
1214 | = (widget_value **) alloca (menu_items_used * sizeof (widget_value *)); | |
1215 | wv = malloc_widget_value (); | |
1216 | wv->name = "menu"; | |
1217 | wv->value = 0; | |
1218 | wv->enabled = 1; | |
1219 | first_wv = wv; | |
1220 | save_wv = 0; | |
1221 | ||
1222 | /* Loop over all panes and items made during this call | |
1223 | and construct a tree of widget_value objects. | |
1224 | Ignore the panes and items made by previous calls to | |
1225 | single_submenu, even though those are also in menu_items. */ | |
1226 | i = previous_items; | |
1227 | while (i < menu_items_used) | |
1228 | { | |
1229 | if (EQ (XVECTOR (menu_items)->contents[i], Qnil)) | |
1230 | { | |
1231 | submenu_stack[submenu_depth++] = save_wv; | |
1232 | save_wv = prev_wv; | |
1233 | prev_wv = 0; | |
1234 | i++; | |
1235 | } | |
1236 | else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda)) | |
1237 | { | |
1238 | prev_wv = save_wv; | |
1239 | save_wv = submenu_stack[--submenu_depth]; | |
1240 | i++; | |
1241 | } | |
1242 | else if (EQ (XVECTOR (menu_items)->contents[i], Qt) | |
1243 | && submenu_depth != 0) | |
1244 | i += MENU_ITEMS_PANE_LENGTH; | |
1245 | /* Ignore a nil in the item list. | |
1246 | It's meaningful only for dialog boxes. */ | |
1247 | else if (EQ (XVECTOR (menu_items)->contents[i], Qquote)) | |
1248 | i += 1; | |
1249 | else if (EQ (XVECTOR (menu_items)->contents[i], Qt)) | |
1250 | { | |
1251 | /* Create a new pane. */ | |
1252 | Lisp_Object pane_name, prefix; | |
1253 | char *pane_string; | |
1254 | pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME]; | |
1255 | prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX]; | |
1256 | pane_string = (NILP (pane_name) | |
1257 | ? "" : (char *) XSTRING (pane_name)->data); | |
1258 | /* If there is just one top-level pane, put all its items directly | |
1259 | under the top-level menu. */ | |
1260 | if (menu_items_n_panes == 1) | |
1261 | pane_string = ""; | |
1262 | ||
1263 | /* If the pane has a meaningful name, | |
1264 | make the pane a top-level menu item | |
1265 | with its items as a submenu beneath it. */ | |
1266 | if (strcmp (pane_string, "")) | |
1267 | { | |
1268 | wv = malloc_widget_value (); | |
1269 | if (save_wv) | |
1270 | save_wv->next = wv; | |
1271 | else | |
1272 | first_wv->contents = wv; | |
1273 | wv->name = pane_string; | |
1274 | if (!NILP (prefix)) | |
1275 | wv->name++; | |
1276 | wv->value = 0; | |
1277 | wv->enabled = 1; | |
1278 | } | |
1279 | save_wv = wv; | |
1280 | prev_wv = 0; | |
1281 | i += MENU_ITEMS_PANE_LENGTH; | |
1282 | } | |
1283 | else | |
1284 | { | |
1285 | /* Create a new item within current pane. */ | |
1286 | Lisp_Object item_name, enable, descrip; | |
1287 | item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME]; | |
1288 | enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE]; | |
1289 | descrip | |
1290 | = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY]; | |
18686d47 | 1291 | |
4dedbfe0 PR |
1292 | wv = malloc_widget_value (); |
1293 | if (prev_wv) | |
1294 | prev_wv->next = wv; | |
1295 | else | |
1296 | save_wv->contents = wv; | |
1297 | wv->name = (char *) XSTRING (item_name)->data; | |
1298 | if (!NILP (descrip)) | |
1299 | wv->key = (char *) XSTRING (descrip)->data; | |
1300 | wv->value = 0; | |
1301 | wv->call_data = (void *) i; | |
1302 | wv->enabled = !NILP (enable); | |
1303 | prev_wv = wv; | |
1304 | ||
1305 | i += MENU_ITEMS_ITEM_LENGTH; | |
1306 | } | |
1307 | } | |
1308 | ||
1309 | return first_wv; | |
1310 | } | |
1311 | \f | |
cffa74ea FP |
1312 | extern void EmacsFrameSetCharSize (); |
1313 | ||
4dedbfe0 PR |
1314 | /* Recompute the menu bar of frame F. */ |
1315 | ||
18686d47 | 1316 | static void |
6af6cbb5 | 1317 | update_frame_menubar (f) |
18686d47 RS |
1318 | FRAME_PTR f; |
1319 | { | |
1320 | struct x_display *x = f->display.x; | |
cffa74ea | 1321 | int columns, rows; |
18686d47 RS |
1322 | int menubar_changed; |
1323 | ||
4dedbfe0 PR |
1324 | Dimension shell_height; |
1325 | ||
1326 | /* We assume the menubar contents has changed if the global flag is set, | |
1327 | or if the current buffer has changed, or if the menubar has never | |
1328 | been updated before. | |
1329 | */ | |
18686d47 RS |
1330 | menubar_changed = (x->menubar_widget |
1331 | && !XtIsManaged (x->menubar_widget)); | |
1332 | ||
1333 | if (! (menubar_changed)) | |
1334 | return; | |
1335 | ||
1336 | BLOCK_INPUT; | |
cffa74ea FP |
1337 | /* Save the size of the frame because the pane widget doesn't accept to |
1338 | resize itself. So force it. */ | |
1339 | columns = f->width; | |
1340 | rows = f->height; | |
1341 | ||
4dedbfe0 PR |
1342 | /* Do the voodoo which means "I'm changing lots of things, don't try to |
1343 | refigure sizes until I'm done." */ | |
1344 | lw_refigure_widget (x->column_widget, False); | |
cffa74ea | 1345 | |
18686d47 RS |
1346 | /* the order in which children are managed is the top to |
1347 | bottom order in which they are displayed in the paned window. | |
1348 | First, remove the text-area widget. | |
1349 | */ | |
1350 | XtUnmanageChild (x->edit_widget); | |
1351 | ||
1352 | /* remove the menubar that is there now, and put up the menubar that | |
1353 | should be there. | |
1354 | */ | |
1355 | if (menubar_changed) | |
1356 | { | |
1357 | XtManageChild (x->menubar_widget); | |
1358 | XtMapWidget (x->menubar_widget); | |
1359 | XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, 0); | |
1360 | } | |
1361 | ||
c98fcf4b | 1362 | /* Re-manage the text-area widget, and then thrash the sizes. */ |
18686d47 | 1363 | XtManageChild (x->edit_widget); |
4dedbfe0 | 1364 | lw_refigure_widget (x->column_widget, True); |
cffa74ea FP |
1365 | |
1366 | /* Force the pane widget to resize itself with the right values. */ | |
1367 | EmacsFrameSetCharSize (x->edit_widget, columns, rows); | |
1368 | ||
18686d47 RS |
1369 | UNBLOCK_INPUT; |
1370 | } | |
1371 | ||
1372 | void | |
706aa2f2 | 1373 | set_frame_menubar (f, first_time) |
18686d47 | 1374 | FRAME_PTR f; |
706aa2f2 | 1375 | int first_time; |
18686d47 RS |
1376 | { |
1377 | Widget menubar_widget = f->display.x->menubar_widget; | |
1378 | int id = (int) f; | |
5b3557df | 1379 | Lisp_Object tail, items; |
18686d47 | 1380 | widget_value *wv, *save_wv, *first_wv, *prev_wv = 0; |
5b3557df | 1381 | int i; |
18686d47 RS |
1382 | |
1383 | BLOCK_INPUT; | |
1384 | ||
1385 | wv = malloc_widget_value (); | |
1386 | wv->name = "menubar"; | |
1387 | wv->value = 0; | |
1388 | wv->enabled = 1; | |
1389 | save_wv = first_wv = wv; | |
4dedbfe0 PR |
1390 | items = FRAME_MENU_BAR_ITEMS (f); |
1391 | menu_items = f->menu_bar_vector; | |
1392 | menu_items_allocated = XVECTOR (menu_items)->size; | |
1393 | init_menu_items (); | |
18686d47 | 1394 | |
5b3557df | 1395 | for (i = 0; i < XVECTOR (items)->size; i += 3) |
18686d47 | 1396 | { |
4dedbfe0 | 1397 | Lisp_Object key, string, maps; |
18686d47 | 1398 | |
4dedbfe0 | 1399 | key = XVECTOR (items)->contents[i]; |
5b3557df | 1400 | string = XVECTOR (items)->contents[i + 1]; |
4dedbfe0 | 1401 | maps = XVECTOR (items)->contents[i + 2]; |
5b3557df RS |
1402 | if (NILP (string)) |
1403 | break; | |
18686d47 | 1404 | |
4dedbfe0 | 1405 | wv = single_submenu (key, string, maps); |
18686d47 RS |
1406 | if (prev_wv) |
1407 | prev_wv->next = wv; | |
1408 | else | |
1409 | save_wv->contents = wv; | |
4e8d3549 | 1410 | wv->name = (char *) XSTRING (string)->data; |
18686d47 RS |
1411 | wv->enabled = 1; |
1412 | prev_wv = wv; | |
1413 | } | |
1414 | ||
4dedbfe0 PR |
1415 | finish_menu_items (); |
1416 | ||
1417 | f->menu_bar_vector = menu_items; | |
1418 | f->menu_bar_items_used = menu_items_used; | |
1419 | menu_items = Qnil; | |
1420 | ||
18686d47 | 1421 | if (menubar_widget) |
4dedbfe0 PR |
1422 | { |
1423 | /* Disable resizing (done for Motif!) */ | |
1424 | lw_allow_resizing (f->display.x->widget, False); | |
1425 | ||
1426 | /* The third arg is DEEP_P, which says to consider the entire | |
1427 | menu trees we supply, rather than just the menu bar item names. */ | |
1428 | lw_modify_all_widgets (id, first_wv, 1); | |
1429 | ||
c98fcf4b | 1430 | /* Re-enable the edit widget to resize. */ |
4dedbfe0 PR |
1431 | lw_allow_resizing (f->display.x->widget, True); |
1432 | } | |
18686d47 RS |
1433 | else |
1434 | { | |
1435 | menubar_widget = lw_create_widget ("menubar", "menubar", | |
1436 | id, first_wv, | |
4dedbfe0 PR |
1437 | f->display.x->column_widget, |
1438 | 0, | |
1439 | popup_activate_callback, | |
1440 | menubar_selection_callback, | |
1441 | popup_deactivate_callback); | |
18686d47 | 1442 | f->display.x->menubar_widget = menubar_widget; |
18686d47 RS |
1443 | } |
1444 | ||
1445 | free_menubar_widget_value_tree (first_wv); | |
1446 | ||
706aa2f2 FP |
1447 | /* Don't update the menubar the first time it is created via x_window. */ |
1448 | if (!first_time) | |
1449 | update_frame_menubar (f); | |
18686d47 RS |
1450 | |
1451 | UNBLOCK_INPUT; | |
1452 | } | |
85f487d1 | 1453 | |
4dedbfe0 PR |
1454 | /* Called from Fx_create_frame to create the inital menubar of a frame |
1455 | before it is mapped, so that the window is mapped with the menubar already | |
1456 | there instead of us tacking it on later and thrashing the window after it | |
1457 | is visible. */ | |
1458 | ||
1459 | void | |
1460 | initialize_frame_menubar (f) | |
1461 | FRAME_PTR f; | |
1462 | { | |
1463 | /* This function is called before the first chance to redisplay | |
1464 | the frame. It has to be, so the frame will have the right size. */ | |
1465 | FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f)); | |
1466 | set_frame_menubar (f, 1); | |
1467 | } | |
1468 | ||
1469 | /* Get rid of the menu bar of frame F, and free its storage. | |
1470 | This is used when deleting a frame, and when turning off the menu bar. */ | |
1471 | ||
85f487d1 FP |
1472 | void |
1473 | free_frame_menubar (f) | |
1474 | FRAME_PTR f; | |
1475 | { | |
1476 | Widget menubar_widget; | |
1477 | int id; | |
1478 | ||
1479 | menubar_widget = f->display.x->menubar_widget; | |
1480 | id = (int) f; | |
1481 | ||
1482 | if (menubar_widget) | |
1483 | { | |
1484 | BLOCK_INPUT; | |
1485 | lw_destroy_all_widgets (id); | |
1486 | UNBLOCK_INPUT; | |
1487 | } | |
1488 | } | |
78589e07 | 1489 | |
78589e07 RS |
1490 | #endif /* USE_X_TOOLKIT */ |
1491 | \f | |
1492 | /* xmenu_show actually displays a menu using the panes and items in menu_items | |
1493 | and returns the value selected from it. | |
1494 | There are two versions of xmenu_show, one for Xt and one for Xlib. | |
1495 | Both assume input is blocked by the caller. */ | |
1496 | ||
1497 | /* F is the frame the menu is for. | |
1498 | X and Y are the frame-relative specified position, | |
1499 | relative to the inside upper left corner of the frame F. | |
1500 | MENUBARP is 1 if the click that asked for this menu came from the menu bar. | |
1501 | KEYMAPS is 1 if this menu was specified with keymaps; | |
1502 | in that case, we return a list containing the chosen item's value | |
1503 | and perhaps also the pane's prefix. | |
1504 | TITLE is the specified menu title. | |
1505 | ERROR is a place to store an error message string in case of failure. | |
1506 | (We return nil on failure, but the value doesn't actually matter.) */ | |
18686d47 RS |
1507 | |
1508 | #ifdef USE_X_TOOLKIT | |
18686d47 | 1509 | |
8ed87156 RS |
1510 | /* We need a unique id for each widget handled by the Lucid Widget |
1511 | library. This includes the frame main windows, popup menu and | |
1512 | dialog box. */ | |
1513 | LWLIB_ID widget_id_tick; | |
165e1749 | 1514 | |
4dedbfe0 PR |
1515 | #ifdef __STDC__ |
1516 | static Lisp_Object *volatile menu_item_selection; | |
1517 | #else | |
1518 | static Lisp_Object *menu_item_selection; | |
1519 | #endif | |
1520 | ||
1521 | static void | |
1522 | popup_selection_callback (widget, id, client_data) | |
1523 | Widget widget; | |
1524 | LWLIB_ID id; | |
1525 | XtPointer client_data; | |
1526 | { | |
1527 | menu_item_selection = (Lisp_Object *) client_data; | |
1528 | } | |
1529 | ||
78589e07 RS |
1530 | static Lisp_Object |
1531 | xmenu_show (f, x, y, menubarp, keymaps, title, error) | |
18686d47 | 1532 | FRAME_PTR f; |
18686d47 RS |
1533 | int x; |
1534 | int y; | |
4dedbfe0 PR |
1535 | int menubarp; /* Dummy parameter for Xt version of |
1536 | xmenu_show() */ | |
78589e07 RS |
1537 | int keymaps; |
1538 | Lisp_Object title; | |
1539 | char **error; | |
18686d47 | 1540 | { |
78589e07 RS |
1541 | int i; |
1542 | int menu_id; | |
18686d47 | 1543 | Widget menu; |
60f312e2 RS |
1544 | Arg av [2]; |
1545 | int ac = 0; | |
78589e07 | 1546 | widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0; |
101bb4a5 RS |
1547 | widget_value **submenu_stack |
1548 | = (widget_value **) alloca (menu_items_used * sizeof (widget_value *)); | |
1549 | Lisp_Object *subprefix_stack | |
1550 | = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object)); | |
1551 | int submenu_depth = 0; | |
78589e07 RS |
1552 | |
1553 | /* Define a queue to save up for later unreading | |
1554 | all X events that don't pertain to the menu. */ | |
1555 | struct event_queue | |
18686d47 RS |
1556 | { |
1557 | XEvent event; | |
78589e07 RS |
1558 | struct event_queue *next; |
1559 | }; | |
18686d47 | 1560 | |
78589e07 RS |
1561 | struct event_queue *queue = NULL; |
1562 | struct event_queue *queue_tmp; | |
1563 | ||
4e8d3549 RS |
1564 | Position root_x, root_y; |
1565 | ||
78c8278d | 1566 | int first_pane; |
a51b963c | 1567 | int next_release_must_exit = 0; |
78c8278d | 1568 | |
78589e07 RS |
1569 | *error = NULL; |
1570 | ||
742f715d KH |
1571 | if (menu_items_used <= MENU_ITEMS_PANE_LENGTH) |
1572 | { | |
1573 | *error = "Empty menu"; | |
1574 | return Qnil; | |
1575 | } | |
63c414df | 1576 | |
78589e07 RS |
1577 | /* Create a tree of widget_value objects |
1578 | representing the panes and their items. */ | |
1579 | wv = malloc_widget_value (); | |
1580 | wv->name = "menu"; | |
1581 | wv->value = 0; | |
1582 | wv->enabled = 1; | |
1583 | first_wv = wv; | |
78c8278d | 1584 | first_pane = 1; |
78589e07 RS |
1585 | |
1586 | /* Loop over all panes and items, filling in the tree. */ | |
1587 | i = 0; | |
1588 | while (i < menu_items_used) | |
1589 | { | |
101bb4a5 RS |
1590 | if (EQ (XVECTOR (menu_items)->contents[i], Qnil)) |
1591 | { | |
1592 | submenu_stack[submenu_depth++] = save_wv; | |
1593 | save_wv = prev_wv; | |
1594 | prev_wv = 0; | |
78c8278d | 1595 | first_pane = 1; |
101bb4a5 RS |
1596 | i++; |
1597 | } | |
1598 | else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda)) | |
1599 | { | |
1600 | prev_wv = save_wv; | |
1601 | save_wv = submenu_stack[--submenu_depth]; | |
78c8278d | 1602 | first_pane = 0; |
101bb4a5 RS |
1603 | i++; |
1604 | } | |
1605 | else if (EQ (XVECTOR (menu_items)->contents[i], Qt) | |
1606 | && submenu_depth != 0) | |
1607 | i += MENU_ITEMS_PANE_LENGTH; | |
fcaa7665 RS |
1608 | /* Ignore a nil in the item list. |
1609 | It's meaningful only for dialog boxes. */ | |
1610 | else if (EQ (XVECTOR (menu_items)->contents[i], Qquote)) | |
1611 | i += 1; | |
101bb4a5 | 1612 | else if (EQ (XVECTOR (menu_items)->contents[i], Qt)) |
78589e07 RS |
1613 | { |
1614 | /* Create a new pane. */ | |
1615 | Lisp_Object pane_name, prefix; | |
1616 | char *pane_string; | |
1617 | pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME]; | |
1618 | prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX]; | |
1619 | pane_string = (NILP (pane_name) | |
1620 | ? "" : (char *) XSTRING (pane_name)->data); | |
101bb4a5 | 1621 | /* If there is just one top-level pane, put all its items directly |
78589e07 RS |
1622 | under the top-level menu. */ |
1623 | if (menu_items_n_panes == 1) | |
1624 | pane_string = ""; | |
1625 | ||
1626 | /* If the pane has a meaningful name, | |
1627 | make the pane a top-level menu item | |
1628 | with its items as a submenu beneath it. */ | |
78c8278d | 1629 | if (!keymaps && strcmp (pane_string, "")) |
78589e07 RS |
1630 | { |
1631 | wv = malloc_widget_value (); | |
1632 | if (save_wv) | |
1633 | save_wv->next = wv; | |
1634 | else | |
1635 | first_wv->contents = wv; | |
1636 | wv->name = pane_string; | |
1637 | if (keymaps && !NILP (prefix)) | |
1638 | wv->name++; | |
1639 | wv->value = 0; | |
1640 | wv->enabled = 1; | |
78c8278d RS |
1641 | save_wv = wv; |
1642 | prev_wv = 0; | |
78589e07 | 1643 | } |
78c8278d RS |
1644 | else if (first_pane) |
1645 | { | |
1646 | save_wv = wv; | |
1647 | prev_wv = 0; | |
1648 | } | |
1649 | first_pane = 0; | |
78589e07 RS |
1650 | i += MENU_ITEMS_PANE_LENGTH; |
1651 | } | |
1652 | else | |
1653 | { | |
1654 | /* Create a new item within current pane. */ | |
1655 | Lisp_Object item_name, enable, descrip; | |
1656 | item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME]; | |
1657 | enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE]; | |
1658 | descrip | |
1659 | = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY]; | |
1660 | ||
1661 | wv = malloc_widget_value (); | |
1662 | if (prev_wv) | |
1663 | prev_wv->next = wv; | |
1664 | else | |
1665 | save_wv->contents = wv; | |
4e8d3549 | 1666 | wv->name = (char *) XSTRING (item_name)->data; |
78589e07 | 1667 | if (!NILP (descrip)) |
4e8d3549 | 1668 | wv->key = (char *) XSTRING (descrip)->data; |
78589e07 RS |
1669 | wv->value = 0; |
1670 | wv->call_data = (void *) &XVECTOR (menu_items)->contents[i]; | |
1671 | wv->enabled = !NILP (enable); | |
1672 | prev_wv = wv; | |
1673 | ||
1674 | i += MENU_ITEMS_ITEM_LENGTH; | |
1675 | } | |
1676 | } | |
1677 | ||
c98fcf4b | 1678 | /* Deal with the title, if it is non-nil. */ |
4dedbfe0 PR |
1679 | if (!NILP (title)) |
1680 | { | |
1681 | widget_value *wv_title = malloc_widget_value (); | |
1682 | widget_value *wv_sep1 = malloc_widget_value (); | |
1683 | widget_value *wv_sep2 = malloc_widget_value (); | |
1684 | ||
1685 | wv_sep2->name = "--"; | |
1686 | wv_sep2->next = first_wv->contents; | |
1687 | ||
1688 | wv_sep1->name = "--"; | |
1689 | wv_sep1->next = wv_sep2; | |
1690 | ||
1691 | wv_title->name = (char *) XSTRING (title)->data; | |
1692 | wv_title->enabled = True; | |
1693 | wv_title->next = wv_sep1; | |
1694 | first_wv->contents = wv_title; | |
1695 | } | |
1696 | ||
78589e07 | 1697 | /* Actually create the menu. */ |
4dedbfe0 | 1698 | menu_id = ++widget_id_tick; |
78589e07 | 1699 | menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv, |
18686d47 | 1700 | f->display.x->widget, 1, 0, |
4dedbfe0 PR |
1701 | popup_selection_callback, |
1702 | popup_deactivate_callback); | |
60f312e2 RS |
1703 | |
1704 | /* Don't allow any geometry request from the user. */ | |
1705 | XtSetArg (av[ac], XtNgeometry, 0); ac++; | |
1706 | XtSetValues (menu, av, ac); | |
1707 | ||
78589e07 RS |
1708 | /* Free the widget_value objects we used to specify the contents. */ |
1709 | free_menubar_widget_value_tree (first_wv); | |
1710 | ||
1711 | /* No selection has been chosen yet. */ | |
1712 | menu_item_selection = 0; | |
1713 | ||
78589e07 | 1714 | /* Display the menu. */ |
4dedbfe0 PR |
1715 | lw_popup_menu (menu); |
1716 | popup_activated_flag = 1; | |
18686d47 | 1717 | |
78589e07 | 1718 | /* Process events that apply to the menu. */ |
4dedbfe0 | 1719 | popup_get_selection ((XEvent *) 0); |
78589e07 RS |
1720 | |
1721 | pop_down: | |
a9c90b7c RS |
1722 | /* fp turned off the following statement and wrote a comment |
1723 | that it is unnecessary--that the menu has already disappeared. | |
1724 | I observer that is not so. -- rms. */ | |
78589e07 RS |
1725 | /* Make sure the menu disappears. */ |
1726 | lw_destroy_all_widgets (menu_id); | |
1727 | ||
1728 | /* Unread any events that we got but did not handle. */ | |
1729 | while (queue != NULL) | |
18686d47 | 1730 | { |
78589e07 | 1731 | queue_tmp = queue; |
92280f67 | 1732 | XPutBackEvent (FRAME_X_DISPLAY (f), &queue_tmp->event); |
78589e07 RS |
1733 | queue = queue_tmp->next; |
1734 | free ((char *)queue_tmp); | |
d9209dee RS |
1735 | /* Cause these events to get read as soon as we UNBLOCK_INPUT. */ |
1736 | interrupt_input_pending = 1; | |
18686d47 RS |
1737 | } |
1738 | ||
78589e07 RS |
1739 | /* Find the selected item, and its pane, to return |
1740 | the proper value. */ | |
1741 | if (menu_item_selection != 0) | |
1742 | { | |
1743 | Lisp_Object prefix; | |
1744 | ||
1745 | prefix = Qnil; | |
1746 | i = 0; | |
1747 | while (i < menu_items_used) | |
1748 | { | |
1749 | Lisp_Object entry; | |
18686d47 | 1750 | |
101bb4a5 RS |
1751 | if (EQ (XVECTOR (menu_items)->contents[i], Qnil)) |
1752 | { | |
1753 | subprefix_stack[submenu_depth++] = prefix; | |
1754 | prefix = entry; | |
1755 | i++; | |
1756 | } | |
1757 | else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda)) | |
1758 | { | |
1759 | prefix = subprefix_stack[--submenu_depth]; | |
1760 | i++; | |
1761 | } | |
1762 | else if (EQ (XVECTOR (menu_items)->contents[i], Qt)) | |
78589e07 RS |
1763 | { |
1764 | prefix | |
1765 | = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX]; | |
1766 | i += MENU_ITEMS_PANE_LENGTH; | |
1767 | } | |
1768 | else | |
1769 | { | |
1770 | entry | |
1771 | = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE]; | |
1772 | if (menu_item_selection == &XVECTOR (menu_items)->contents[i]) | |
1773 | { | |
1774 | if (keymaps != 0) | |
1775 | { | |
101bb4a5 RS |
1776 | int j; |
1777 | ||
78589e07 RS |
1778 | entry = Fcons (entry, Qnil); |
1779 | if (!NILP (prefix)) | |
1780 | entry = Fcons (prefix, entry); | |
101bb4a5 | 1781 | for (j = submenu_depth - 1; j >= 0; j--) |
e48087b7 | 1782 | if (!NILP (subprefix_stack[j])) |
5964e450 | 1783 | entry = Fcons (subprefix_stack[j], entry); |
78589e07 RS |
1784 | } |
1785 | return entry; | |
1786 | } | |
1787 | i += MENU_ITEMS_ITEM_LENGTH; | |
1788 | } | |
1789 | } | |
1790 | } | |
1791 | ||
1792 | return Qnil; | |
18686d47 | 1793 | } |
4dedbfe0 PR |
1794 | \f |
1795 | static void | |
1796 | dialog_selection_callback (widget, id, client_data) | |
1797 | Widget widget; | |
1798 | LWLIB_ID id; | |
1799 | XtPointer client_data; | |
1800 | { | |
1801 | if ((int)client_data != -1) | |
1802 | menu_item_selection = (Lisp_Object *) client_data; | |
1803 | BLOCK_INPUT; | |
1804 | lw_destroy_all_widgets (id); | |
1805 | UNBLOCK_INPUT; | |
1806 | } | |
18686d47 | 1807 | |
165e1749 FP |
1808 | static char * button_names [] = { |
1809 | "button1", "button2", "button3", "button4", "button5", | |
1810 | "button6", "button7", "button8", "button9", "button10" }; | |
1811 | ||
1812 | static Lisp_Object | |
99fe880d | 1813 | xdialog_show (f, menubarp, keymaps, title, error) |
165e1749 | 1814 | FRAME_PTR f; |
165e1749 FP |
1815 | int menubarp; |
1816 | int keymaps; | |
1817 | Lisp_Object title; | |
1818 | char **error; | |
1819 | { | |
1820 | int i, nb_buttons=0; | |
1821 | int dialog_id; | |
1822 | Widget menu; | |
80670155 | 1823 | char dialog_name[6]; |
165e1749 | 1824 | |
165e1749 FP |
1825 | widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0; |
1826 | ||
1827 | /* Define a queue to save up for later unreading | |
1828 | all X events that don't pertain to the menu. */ | |
1829 | struct event_queue | |
1830 | { | |
1831 | XEvent event; | |
1832 | struct event_queue *next; | |
1833 | }; | |
1834 | ||
1835 | struct event_queue *queue = NULL; | |
1836 | struct event_queue *queue_tmp; | |
1837 | ||
fcaa7665 RS |
1838 | /* Number of elements seen so far, before boundary. */ |
1839 | int left_count = 0; | |
1840 | /* 1 means we've seen the boundary between left-hand elts and right-hand. */ | |
1841 | int boundary_seen = 0; | |
1842 | ||
165e1749 FP |
1843 | *error = NULL; |
1844 | ||
80670155 RS |
1845 | if (menu_items_n_panes > 1) |
1846 | { | |
1847 | *error = "Multiple panes in dialog box"; | |
1848 | return Qnil; | |
1849 | } | |
1850 | ||
165e1749 FP |
1851 | /* Create a tree of widget_value objects |
1852 | representing the text label and buttons. */ | |
1853 | { | |
1854 | Lisp_Object pane_name, prefix; | |
1855 | char *pane_string; | |
1856 | pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME]; | |
1857 | prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX]; | |
1858 | pane_string = (NILP (pane_name) | |
1859 | ? "" : (char *) XSTRING (pane_name)->data); | |
1860 | prev_wv = malloc_widget_value (); | |
1861 | prev_wv->value = pane_string; | |
1862 | if (keymaps && !NILP (prefix)) | |
1863 | prev_wv->name++; | |
1864 | prev_wv->enabled = 1; | |
1865 | prev_wv->name = "message"; | |
1866 | first_wv = prev_wv; | |
1867 | ||
1868 | /* Loop over all panes and items, filling in the tree. */ | |
1869 | i = MENU_ITEMS_PANE_LENGTH; | |
1870 | while (i < menu_items_used) | |
1871 | { | |
1872 | ||
1873 | /* Create a new item within current pane. */ | |
1874 | Lisp_Object item_name, enable, descrip; | |
1875 | item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME]; | |
1876 | enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE]; | |
1877 | descrip | |
1878 | = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY]; | |
1879 | ||
80670155 RS |
1880 | if (NILP (item_name)) |
1881 | { | |
1882 | free_menubar_widget_value_tree (first_wv); | |
1883 | *error = "Submenu in dialog items"; | |
1884 | return Qnil; | |
1885 | } | |
fcaa7665 RS |
1886 | if (EQ (item_name, Qquote)) |
1887 | { | |
1888 | /* This is the boundary between left-side elts | |
1889 | and right-side elts. Stop incrementing right_count. */ | |
1890 | boundary_seen = 1; | |
1891 | i++; | |
1892 | continue; | |
1893 | } | |
80670155 RS |
1894 | if (nb_buttons >= 10) |
1895 | { | |
1896 | free_menubar_widget_value_tree (first_wv); | |
1897 | *error = "Too many dialog items"; | |
1898 | return Qnil; | |
1899 | } | |
1900 | ||
165e1749 FP |
1901 | wv = malloc_widget_value (); |
1902 | prev_wv->next = wv; | |
80670155 | 1903 | wv->name = (char *) button_names[nb_buttons]; |
165e1749 | 1904 | if (!NILP (descrip)) |
4e8d3549 RS |
1905 | wv->key = (char *) XSTRING (descrip)->data; |
1906 | wv->value = (char *) XSTRING (item_name)->data; | |
165e1749 FP |
1907 | wv->call_data = (void *) &XVECTOR (menu_items)->contents[i]; |
1908 | wv->enabled = !NILP (enable); | |
1909 | prev_wv = wv; | |
1910 | ||
fcaa7665 RS |
1911 | if (! boundary_seen) |
1912 | left_count++; | |
1913 | ||
165e1749 FP |
1914 | nb_buttons++; |
1915 | i += MENU_ITEMS_ITEM_LENGTH; | |
1916 | } | |
1917 | ||
fcaa7665 RS |
1918 | /* If the boundary was not specified, |
1919 | by default put half on the left and half on the right. */ | |
1920 | if (! boundary_seen) | |
1921 | left_count = nb_buttons - nb_buttons / 2; | |
1922 | ||
165e1749 | 1923 | wv = malloc_widget_value (); |
80670155 RS |
1924 | wv->name = dialog_name; |
1925 | ||
1926 | /* Dialog boxes use a really stupid name encoding | |
1927 | which specifies how many buttons to use | |
1928 | and how many buttons are on the right. | |
1929 | The Q means something also. */ | |
1930 | dialog_name[0] = 'Q'; | |
1931 | dialog_name[1] = '0' + nb_buttons; | |
1932 | dialog_name[2] = 'B'; | |
1933 | dialog_name[3] = 'R'; | |
fcaa7665 RS |
1934 | /* Number of buttons to put on the right. */ |
1935 | dialog_name[4] = '0' + nb_buttons - left_count; | |
80670155 | 1936 | dialog_name[5] = 0; |
165e1749 FP |
1937 | wv->contents = first_wv; |
1938 | first_wv = wv; | |
165e1749 FP |
1939 | } |
1940 | ||
1941 | /* Actually create the dialog. */ | |
4dedbfe0 | 1942 | dialog_id = ++widget_id_tick; |
165e1749 FP |
1943 | menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv, |
1944 | f->display.x->widget, 1, 0, | |
1945 | dialog_selection_callback, 0); | |
b5587215 | 1946 | lw_modify_all_widgets (dialog_id, first_wv->contents, True); |
165e1749 FP |
1947 | /* Free the widget_value objects we used to specify the contents. */ |
1948 | free_menubar_widget_value_tree (first_wv); | |
1949 | ||
1950 | /* No selection has been chosen yet. */ | |
1951 | menu_item_selection = 0; | |
1952 | ||
165e1749 FP |
1953 | /* Display the menu. */ |
1954 | lw_pop_up_all_widgets (dialog_id); | |
1955 | ||
1956 | /* Process events that apply to the menu. */ | |
1957 | while (1) | |
1958 | { | |
1959 | XEvent event; | |
1960 | ||
1961 | XtAppNextEvent (Xt_app_con, &event); | |
1962 | if (event.type == ButtonRelease) | |
1963 | { | |
1964 | XtDispatchEvent (&event); | |
1965 | break; | |
1966 | } | |
1967 | else if (event.type == Expose) | |
1968 | process_expose_from_menu (event); | |
1969 | XtDispatchEvent (&event); | |
92280f67 | 1970 | if (XtWindowToWidget (FRAME_X_DISPLAY (f), event.xany.window) != menu) |
165e1749 FP |
1971 | { |
1972 | queue_tmp = (struct event_queue *) malloc (sizeof (struct event_queue)); | |
1973 | ||
1974 | if (queue_tmp != NULL) | |
1975 | { | |
1976 | queue_tmp->event = event; | |
1977 | queue_tmp->next = queue; | |
1978 | queue = queue_tmp; | |
1979 | } | |
1980 | } | |
1981 | } | |
1982 | pop_down: | |
1983 | ||
87485d6f | 1984 | #ifdef HAVE_X_WINDOWS |
a5285df3 RS |
1985 | /* State that no mouse buttons are now held. |
1986 | That is not necessarily true, but the fiction leads to reasonable | |
1987 | results, and it is a pain to ask which are actually held now | |
1988 | or track this in the loop above. */ | |
e9a79fb2 | 1989 | FRAME_X_DISPLAY_INFO (f)->grabbed = 0; |
87485d6f | 1990 | #endif |
a5285df3 | 1991 | |
165e1749 FP |
1992 | /* Unread any events that we got but did not handle. */ |
1993 | while (queue != NULL) | |
1994 | { | |
1995 | queue_tmp = queue; | |
92280f67 | 1996 | XPutBackEvent (FRAME_X_DISPLAY (f), &queue_tmp->event); |
165e1749 FP |
1997 | queue = queue_tmp->next; |
1998 | free ((char *)queue_tmp); | |
d9209dee RS |
1999 | /* Cause these events to get read as soon as we UNBLOCK_INPUT. */ |
2000 | interrupt_input_pending = 1; | |
165e1749 FP |
2001 | } |
2002 | ||
2003 | /* Find the selected item, and its pane, to return | |
2004 | the proper value. */ | |
2005 | if (menu_item_selection != 0) | |
2006 | { | |
2007 | Lisp_Object prefix; | |
2008 | ||
2009 | prefix = Qnil; | |
2010 | i = 0; | |
2011 | while (i < menu_items_used) | |
2012 | { | |
2013 | Lisp_Object entry; | |
2014 | ||
2015 | if (EQ (XVECTOR (menu_items)->contents[i], Qt)) | |
2016 | { | |
2017 | prefix | |
2018 | = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX]; | |
2019 | i += MENU_ITEMS_PANE_LENGTH; | |
2020 | } | |
2021 | else | |
2022 | { | |
2023 | entry | |
2024 | = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE]; | |
2025 | if (menu_item_selection == &XVECTOR (menu_items)->contents[i]) | |
2026 | { | |
2027 | if (keymaps != 0) | |
2028 | { | |
2029 | entry = Fcons (entry, Qnil); | |
2030 | if (!NILP (prefix)) | |
2031 | entry = Fcons (prefix, entry); | |
2032 | } | |
2033 | return entry; | |
2034 | } | |
2035 | i += MENU_ITEMS_ITEM_LENGTH; | |
2036 | } | |
2037 | } | |
2038 | } | |
2039 | ||
2040 | return Qnil; | |
2041 | } | |
18686d47 | 2042 | #else /* not USE_X_TOOLKIT */ |
78589e07 RS |
2043 | |
2044 | static Lisp_Object | |
2045 | xmenu_show (f, x, y, menubarp, keymaps, title, error) | |
2046 | FRAME_PTR f; | |
2047 | int x, y; | |
2048 | int keymaps; | |
2049 | int menubarp; | |
2050 | Lisp_Object title; | |
2051 | char **error; | |
dcfdbac7 | 2052 | { |
78589e07 RS |
2053 | Window root; |
2054 | XMenu *menu; | |
2055 | int pane, selidx, lpane, status; | |
2056 | Lisp_Object entry, pane_prefix; | |
dcfdbac7 JB |
2057 | char *datap; |
2058 | int ulx, uly, width, height; | |
2059 | int dispwidth, dispheight; | |
4e8d3549 RS |
2060 | int i, j; |
2061 | int maxwidth; | |
78589e07 RS |
2062 | int dummy_int; |
2063 | unsigned int dummy_uint; | |
088831f6 | 2064 | |
07a675b7 | 2065 | *error = 0; |
78589e07 RS |
2066 | if (menu_items_n_panes == 0) |
2067 | return Qnil; | |
088831f6 | 2068 | |
742f715d KH |
2069 | if (menu_items_used <= MENU_ITEMS_PANE_LENGTH) |
2070 | { | |
2071 | *error = "Empty menu"; | |
2072 | return Qnil; | |
2073 | } | |
2074 | ||
78589e07 | 2075 | /* Figure out which root window F is on. */ |
92280f67 | 2076 | XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root, |
78589e07 RS |
2077 | &dummy_int, &dummy_int, &dummy_uint, &dummy_uint, |
2078 | &dummy_uint, &dummy_uint); | |
18686d47 | 2079 | |
78589e07 | 2080 | /* Make the menu on that window. */ |
92280f67 | 2081 | menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs"); |
78589e07 | 2082 | if (menu == NULL) |
dcfdbac7 JB |
2083 | { |
2084 | *error = "Can't create menu"; | |
78589e07 | 2085 | return Qnil; |
dcfdbac7 | 2086 | } |
78589e07 | 2087 | |
87485d6f | 2088 | #ifdef HAVE_X_WINDOWS |
78589e07 | 2089 | /* Adjust coordinates to relative to the outer (window manager) window. */ |
78589e07 RS |
2090 | { |
2091 | Window child; | |
2092 | int win_x = 0, win_y = 0; | |
2093 | ||
2094 | /* Find the position of the outside upper-left corner of | |
2095 | the inner window, with respect to the outer window. */ | |
2096 | if (f->display.x->parent_desc != ROOT_WINDOW) | |
2097 | { | |
2098 | BLOCK_INPUT; | |
92280f67 | 2099 | XTranslateCoordinates (FRAME_X_DISPLAY (f), |
78589e07 RS |
2100 | |
2101 | /* From-window, to-window. */ | |
2102 | f->display.x->window_desc, | |
2103 | f->display.x->parent_desc, | |
2104 | ||
2105 | /* From-position, to-position. */ | |
2106 | 0, 0, &win_x, &win_y, | |
2107 | ||
2108 | /* Child of window. */ | |
2109 | &child); | |
2110 | UNBLOCK_INPUT; | |
2111 | x += win_x; | |
2112 | y += win_y; | |
2113 | } | |
2114 | } | |
87485d6f | 2115 | #endif /* HAVE_X_WINDOWS */ |
78589e07 RS |
2116 | |
2117 | /* Adjust coordinates to be root-window-relative. */ | |
2118 | x += f->display.x->left_pos; | |
2119 | y += f->display.x->top_pos; | |
18686d47 | 2120 | |
78589e07 RS |
2121 | /* Create all the necessary panes and their items. */ |
2122 | i = 0; | |
2123 | while (i < menu_items_used) | |
dcfdbac7 | 2124 | { |
78589e07 | 2125 | if (EQ (XVECTOR (menu_items)->contents[i], Qt)) |
dcfdbac7 | 2126 | { |
78589e07 RS |
2127 | /* Create a new pane. */ |
2128 | Lisp_Object pane_name, prefix; | |
2129 | char *pane_string; | |
2130 | ||
2131 | pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME]; | |
2132 | prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX]; | |
2133 | pane_string = (NILP (pane_name) | |
2134 | ? "" : (char *) XSTRING (pane_name)->data); | |
2135 | if (keymaps && !NILP (prefix)) | |
2136 | pane_string++; | |
2137 | ||
92280f67 | 2138 | lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE); |
78589e07 RS |
2139 | if (lpane == XM_FAILURE) |
2140 | { | |
92280f67 | 2141 | XMenuDestroy (FRAME_X_DISPLAY (f), menu); |
78589e07 RS |
2142 | *error = "Can't create pane"; |
2143 | return Qnil; | |
2144 | } | |
2145 | i += MENU_ITEMS_PANE_LENGTH; | |
4e8d3549 RS |
2146 | |
2147 | /* Find the width of the widest item in this pane. */ | |
2148 | maxwidth = 0; | |
2149 | j = i; | |
2150 | while (j < menu_items_used) | |
2151 | { | |
2152 | Lisp_Object item; | |
2153 | item = XVECTOR (menu_items)->contents[j]; | |
2154 | if (EQ (item, Qt)) | |
2155 | break; | |
2156 | if (NILP (item)) | |
2157 | { | |
2158 | j++; | |
2159 | continue; | |
2160 | } | |
2161 | width = XSTRING (item)->size; | |
2162 | if (width > maxwidth) | |
2163 | maxwidth = width; | |
2164 | ||
2165 | j += MENU_ITEMS_ITEM_LENGTH; | |
2166 | } | |
dcfdbac7 | 2167 | } |
fcaa7665 RS |
2168 | /* Ignore a nil in the item list. |
2169 | It's meaningful only for dialog boxes. */ | |
2170 | else if (EQ (XVECTOR (menu_items)->contents[i], Qquote)) | |
2171 | i += 1; | |
78589e07 | 2172 | else |
dcfdbac7 | 2173 | { |
78589e07 RS |
2174 | /* Create a new item within current pane. */ |
2175 | Lisp_Object item_name, enable, descrip; | |
4e8d3549 | 2176 | unsigned char *item_data; |
78589e07 RS |
2177 | |
2178 | item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME]; | |
2179 | enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE]; | |
2180 | descrip | |
2181 | = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY]; | |
2182 | if (!NILP (descrip)) | |
4e8d3549 RS |
2183 | { |
2184 | int gap = maxwidth - XSTRING (item_name)->size; | |
2185 | #ifdef C_ALLOCA | |
2186 | Lisp_Object spacer; | |
2187 | spacer = Fmake_string (make_number (gap), make_number (' ')); | |
2188 | item_name = concat2 (item_name, spacer); | |
2189 | item_name = concat2 (item_name, descrip); | |
2190 | item_data = XSTRING (item_name)->data; | |
2191 | #else | |
2192 | /* if alloca is fast, use that to make the space, | |
2193 | to reduce gc needs. */ | |
2194 | item_data | |
2195 | = (unsigned char *) alloca (maxwidth | |
2196 | + XSTRING (descrip)->size + 1); | |
2197 | bcopy (XSTRING (item_name)->data, item_data, | |
2198 | XSTRING (item_name)->size); | |
2199 | for (j = XSTRING (item_name)->size; j < maxwidth; j++) | |
2200 | item_data[j] = ' '; | |
2201 | bcopy (XSTRING (descrip)->data, item_data + j, | |
2202 | XSTRING (descrip)->size); | |
2203 | item_data[j + XSTRING (descrip)->size] = 0; | |
2204 | #endif | |
2205 | } | |
2206 | else | |
2207 | item_data = XSTRING (item_name)->data; | |
78589e07 | 2208 | |
92280f67 RS |
2209 | if (XMenuAddSelection (FRAME_X_DISPLAY (f), |
2210 | menu, lpane, 0, item_data, | |
78589e07 | 2211 | !NILP (enable)) |
dcfdbac7 JB |
2212 | == XM_FAILURE) |
2213 | { | |
92280f67 | 2214 | XMenuDestroy (FRAME_X_DISPLAY (f), menu); |
dcfdbac7 | 2215 | *error = "Can't add selection to menu"; |
78589e07 | 2216 | return Qnil; |
dcfdbac7 | 2217 | } |
78589e07 | 2218 | i += MENU_ITEMS_ITEM_LENGTH; |
dcfdbac7 JB |
2219 | } |
2220 | } | |
4e8d3549 | 2221 | |
78589e07 | 2222 | /* All set and ready to fly. */ |
92280f67 RS |
2223 | XMenuRecompute (FRAME_X_DISPLAY (f), menu); |
2224 | dispwidth = DisplayWidth (FRAME_X_DISPLAY (f), | |
e9a79fb2 | 2225 | FRAME_X_DISPLAY_INFO (f)->screen); |
92280f67 | 2226 | dispheight = DisplayHeight (FRAME_X_DISPLAY (f), |
e9a79fb2 | 2227 | FRAME_X_DISPLAY_INFO (f)->screen); |
78589e07 RS |
2228 | x = min (x, dispwidth); |
2229 | y = min (y, dispheight); | |
2230 | x = max (x, 1); | |
2231 | y = max (y, 1); | |
92280f67 | 2232 | XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y, |
dcfdbac7 JB |
2233 | &ulx, &uly, &width, &height); |
2234 | if (ulx+width > dispwidth) | |
2235 | { | |
78589e07 | 2236 | x -= (ulx + width) - dispwidth; |
dcfdbac7 JB |
2237 | ulx = dispwidth - width; |
2238 | } | |
2239 | if (uly+height > dispheight) | |
2240 | { | |
78589e07 | 2241 | y -= (uly + height) - dispheight; |
dcfdbac7 JB |
2242 | uly = dispheight - height; |
2243 | } | |
78589e07 RS |
2244 | if (ulx < 0) x -= ulx; |
2245 | if (uly < 0) y -= uly; | |
121e4555 KH |
2246 | |
2247 | XMenuSetAEQ (menu, TRUE); | |
78589e07 RS |
2248 | XMenuSetFreeze (menu, TRUE); |
2249 | pane = selidx = 0; | |
dcfdbac7 | 2250 | |
92280f67 | 2251 | status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx, |
78589e07 | 2252 | x, y, ButtonReleaseMask, &datap); |
dcfdbac7 JB |
2253 | switch (status) |
2254 | { | |
2255 | case XM_SUCCESS: | |
2256 | #ifdef XDEBUG | |
2257 | fprintf (stderr, "pane= %d line = %d\n", panes, selidx); | |
2258 | #endif | |
fa6d54d9 | 2259 | |
78589e07 RS |
2260 | /* Find the item number SELIDX in pane number PANE. */ |
2261 | i = 0; | |
2262 | while (i < menu_items_used) | |
fa6d54d9 | 2263 | { |
78589e07 | 2264 | if (EQ (XVECTOR (menu_items)->contents[i], Qt)) |
088831f6 | 2265 | { |
78589e07 RS |
2266 | if (pane == 0) |
2267 | pane_prefix | |
2268 | = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX]; | |
2269 | pane--; | |
2270 | i += MENU_ITEMS_PANE_LENGTH; | |
088831f6 | 2271 | } |
78589e07 | 2272 | else |
ab6ee1a0 | 2273 | { |
78589e07 | 2274 | if (pane == -1) |
ab6ee1a0 | 2275 | { |
78589e07 | 2276 | if (selidx == 0) |
ab6ee1a0 | 2277 | { |
78589e07 RS |
2278 | entry |
2279 | = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE]; | |
2280 | if (keymaps != 0) | |
ab6ee1a0 | 2281 | { |
78589e07 RS |
2282 | entry = Fcons (entry, Qnil); |
2283 | if (!NILP (pane_prefix)) | |
2284 | entry = Fcons (pane_prefix, entry); | |
ab6ee1a0 | 2285 | } |
78589e07 | 2286 | break; |
ab6ee1a0 | 2287 | } |
78589e07 | 2288 | selidx--; |
ab6ee1a0 | 2289 | } |
78589e07 | 2290 | i += MENU_ITEMS_ITEM_LENGTH; |
ab6ee1a0 RS |
2291 | } |
2292 | } | |
78589e07 | 2293 | break; |
dcfdbac7 | 2294 | |
78589e07 | 2295 | case XM_FAILURE: |
78589e07 RS |
2296 | *error = "Can't activate menu"; |
2297 | case XM_IA_SELECT: | |
2298 | case XM_NO_SELECT: | |
2299 | entry = Qnil; | |
2300 | break; | |
dcfdbac7 | 2301 | } |
92280f67 | 2302 | XMenuDestroy (FRAME_X_DISPLAY (f), menu); |
a5285df3 | 2303 | |
87485d6f | 2304 | #ifdef HAVE_X_WINDOWS |
a5285df3 RS |
2305 | /* State that no mouse buttons are now held. |
2306 | (The oldXMenu code doesn't track this info for us.) | |
2307 | That is not necessarily true, but the fiction leads to reasonable | |
2308 | results, and it is a pain to ask which are actually held now. */ | |
e9a79fb2 | 2309 | FRAME_X_DISPLAY_INFO (f)->grabbed = 0; |
87485d6f | 2310 | #endif |
a5285df3 | 2311 | |
78589e07 | 2312 | return entry; |
dcfdbac7 | 2313 | } |
4dedbfe0 | 2314 | |
78589e07 | 2315 | #endif /* not USE_X_TOOLKIT */ |
088831f6 | 2316 | \f |
78589e07 | 2317 | syms_of_xmenu () |
dcfdbac7 | 2318 | { |
78589e07 RS |
2319 | staticpro (&menu_items); |
2320 | menu_items = Qnil; | |
dcfdbac7 | 2321 | |
8ed87156 | 2322 | #ifdef USE_X_TOOLKIT |
4dedbfe0 | 2323 | widget_id_tick = (1<<16); |
8ed87156 RS |
2324 | #endif |
2325 | ||
78589e07 | 2326 | defsubr (&Sx_popup_menu); |
165e1749 | 2327 | defsubr (&Sx_popup_dialog); |
dcfdbac7 | 2328 | } |