Commit | Line | Data |
---|---|---|
dcfdbac7 | 1 | /* X Communication module for terminals which understand the X protocol. |
774910eb | 2 | Copyright (C) 1986, 1988, 1992 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 | ||
dcfdbac7 JB |
27 | #ifdef XDEBUG |
28 | #include <stdio.h> | |
29 | #endif | |
30 | ||
31 | /* On 4.3 this loses if it comes after xterm.h. */ | |
32 | #include <signal.h> | |
33 | #include "config.h" | |
34 | #include "lisp.h" | |
7708e9bd | 35 | #include "frame.h" |
dcfdbac7 | 36 | #include "window.h" |
031b0e31 | 37 | #include "keyboard.h" |
dcfdbac7 JB |
38 | |
39 | /* This may include sys/types.h, and that somehow loses | |
40 | if this is not done before the other system files. */ | |
41 | #include "xterm.h" | |
42 | ||
43 | /* Load sys/types.h if not already loaded. | |
44 | In some systems loading it twice is suicidal. */ | |
45 | #ifndef makedev | |
46 | #include <sys/types.h> | |
47 | #endif | |
48 | ||
49 | #include "dispextern.h" | |
50 | ||
51 | #ifdef HAVE_X11 | |
52 | #include "../oldXMenu/XMenu.h" | |
53 | #else | |
54 | #include <X/XMenu.h> | |
55 | #endif | |
56 | ||
57 | #define min(x,y) (((x) < (y)) ? (x) : (y)) | |
58 | #define max(x,y) (((x) > (y)) ? (x) : (y)) | |
59 | ||
60 | #define NUL 0 | |
61 | ||
62 | #ifndef TRUE | |
63 | #define TRUE 1 | |
64 | #define FALSE 0 | |
65 | #endif TRUE | |
66 | ||
67 | #ifdef HAVE_X11 | |
68 | extern Display *x_current_display; | |
69 | #else | |
70 | #define ButtonReleaseMask ButtonReleased | |
71 | #endif /* not HAVE_X11 */ | |
72 | ||
d9dcaf49 | 73 | Lisp_Object Qmenu_enable; |
dcfdbac7 JB |
74 | Lisp_Object xmenu_show (); |
75 | extern int x_error_handler (); | |
76 | ||
77 | /*************************************************************/ | |
78 | ||
79 | #if 0 | |
80 | /* Ignoring the args is easiest. */ | |
81 | xmenu_quit () | |
82 | { | |
83 | error ("Unknown XMenu error"); | |
84 | } | |
85 | #endif | |
86 | ||
87 | DEFUN ("x-popup-menu",Fx_popup_menu, Sx_popup_menu, 1, 2, 0, | |
88 | "Pop up a deck-of-cards menu and return user's selection.\n\ | |
088831f6 RS |
89 | POSITION is a position specification. This is either a mouse button event\n\ |
90 | or a list ((XOFFSET YOFFSET) WINDOW)\n\ | |
dcfdbac7 | 91 | where XOFFSET and YOFFSET are positions in characters from the top left\n\ |
7da99777 | 92 | corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\ |
dcfdbac7 JB |
93 | This controls the position of the center of the first line\n\ |
94 | in the first pane of the menu, not the top left of the menu as a whole.\n\ | |
95 | \n\ | |
088831f6 RS |
96 | MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\ |
97 | The menu items come from key bindings that have a menu string as well as\n\ | |
98 | a definition; actually, the \"definition\" in such a key binding looks like\n\ | |
99 | \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\ | |
100 | the keymap as a top-level element.\n\n\ | |
101 | You can also use a list of keymaps as MENU.\n\ | |
102 | Then each keymap makes a separate pane.\n\n\ | |
103 | Alternatively, you can specify a menu of multiple panes\n\ | |
104 | with a list of the form\n\ | |
105 | \(TITLE PANE1 PANE2...), where each pane is a list of form\n\ | |
dcfdbac7 | 106 | \(TITLE (LINE ITEM)...). Each line should be a string, and item should\n\ |
dbc4e1c1 | 107 | be the return value for that line (i.e. if it is selected).") |
088831f6 RS |
108 | (position, menu) |
109 | Lisp_Object position, menu; | |
dcfdbac7 JB |
110 | { |
111 | int number_of_panes; | |
088831f6 | 112 | Lisp_Object XMenu_return, keymap, tem; |
dcfdbac7 JB |
113 | int XMenu_xpos, XMenu_ypos; |
114 | char **menus; | |
115 | char ***names; | |
aedaff8d | 116 | int **enables; |
dcfdbac7 JB |
117 | Lisp_Object **obj_list; |
118 | int *items; | |
119 | char *title; | |
120 | char *error_name; | |
121 | Lisp_Object ltitle, selection; | |
122 | int i, j; | |
7708e9bd | 123 | FRAME_PTR f; |
dcfdbac7 JB |
124 | Lisp_Object x, y, window; |
125 | ||
088831f6 RS |
126 | /* Decode the first argument: find the window and the coordinates. */ |
127 | tem = Fcar (position); | |
128 | if (XTYPE (tem) == Lisp_Cons) | |
129 | { | |
130 | window = Fcar (Fcdr (position)); | |
131 | x = Fcar (tem); | |
132 | y = Fcar (Fcdr (tem)); | |
133 | } | |
134 | else | |
135 | { | |
933ff472 RS |
136 | tem = Fcar (Fcdr (position)); /* EVENT_START (position) */ |
137 | window = Fcar (tem); /* POSN_WINDOW (tem) */ | |
138 | tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */ | |
088831f6 RS |
139 | x = Fcar (tem); |
140 | y = Fcdr (tem); | |
141 | } | |
dcfdbac7 JB |
142 | CHECK_NUMBER (x, 0); |
143 | CHECK_NUMBER (y, 0); | |
088831f6 | 144 | |
7da99777 RS |
145 | if (XTYPE (window) == Lisp_Frame) |
146 | { | |
147 | f = XFRAME (window); | |
148 | ||
149 | XMenu_xpos = 0; | |
150 | XMenu_ypos = 0; | |
151 | } | |
152 | else if (XTYPE (window) == Lisp_Window) | |
153 | { | |
154 | CHECK_LIVE_WINDOW (window, 0); | |
155 | f = XFRAME (WINDOW_FRAME (XWINDOW (window))); | |
156 | ||
157 | XMenu_xpos = FONT_WIDTH (f->display.x->font) * XWINDOW (window)->left; | |
158 | XMenu_ypos = FONT_HEIGHT (f->display.x->font) * XWINDOW (window)->top; | |
159 | } | |
160 | ||
161 | XMenu_xpos += FONT_WIDTH (f->display.x->font) * XINT (x); | |
162 | XMenu_ypos += FONT_HEIGHT (f->display.x->font) * XINT (y); | |
dcfdbac7 | 163 | |
7708e9bd JB |
164 | XMenu_xpos += f->display.x->left_pos; |
165 | XMenu_ypos += f->display.x->top_pos; | |
dcfdbac7 | 166 | |
088831f6 RS |
167 | keymap = Fkeymapp (menu); |
168 | tem = Qnil; | |
169 | if (XTYPE (menu) == Lisp_Cons) | |
170 | tem = Fkeymapp (Fcar (menu)); | |
171 | if (!NILP (keymap)) | |
172 | { | |
173 | /* We were given a keymap. Extract menu info from the keymap. */ | |
174 | Lisp_Object prompt; | |
175 | keymap = get_keymap (menu); | |
176 | ||
177 | /* Search for a string appearing directly as an element of the keymap. | |
178 | That string is the title of the menu. */ | |
179 | prompt = map_prompt (keymap); | |
180 | if (!NILP (prompt)) | |
181 | title = (char *) XSTRING (prompt)->data; | |
182 | ||
183 | /* Extract the detailed info to make one pane. */ | |
aedaff8d RS |
184 | number_of_panes = keymap_panes (&obj_list, &menus, &names, &enables, |
185 | &items, &menu, 1); | |
088831f6 RS |
186 | /* The menu title seems to be ignored, |
187 | so put it in the pane title. */ | |
188 | if (menus[0] == 0) | |
189 | menus[0] = title; | |
190 | } | |
191 | else if (!NILP (tem)) | |
dcfdbac7 | 192 | { |
088831f6 RS |
193 | /* We were given a list of keymaps. */ |
194 | Lisp_Object prompt; | |
195 | int nmaps = XFASTINT (Flength (menu)); | |
196 | Lisp_Object *maps | |
197 | = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object)); | |
198 | int i; | |
199 | title = 0; | |
200 | ||
201 | /* The first keymap that has a prompt string | |
202 | supplies the menu title. */ | |
203 | for (tem = menu, i = 0; XTYPE (tem) == Lisp_Cons; tem = Fcdr (tem)) | |
dcfdbac7 | 204 | { |
088831f6 RS |
205 | maps[i++] = keymap = get_keymap (Fcar (tem)); |
206 | ||
207 | prompt = map_prompt (keymap); | |
208 | if (title == 0 && !NILP (prompt)) | |
209 | title = (char *) XSTRING (prompt)->data; | |
dcfdbac7 | 210 | } |
088831f6 RS |
211 | |
212 | /* Extract the detailed info to make one pane. */ | |
aedaff8d RS |
213 | number_of_panes = keymap_panes (&obj_list, &menus, &names, &enables, |
214 | &items, maps, nmaps); | |
088831f6 RS |
215 | /* The menu title seems to be ignored, |
216 | so put it in the pane title. */ | |
217 | if (menus[0] == 0) | |
218 | menus[0] = title; | |
219 | } | |
220 | else | |
221 | { | |
222 | /* We were given an old-fashioned menu. */ | |
223 | ltitle = Fcar (menu); | |
224 | CHECK_STRING (ltitle, 1); | |
225 | title = (char *) XSTRING (ltitle)->data; | |
aedaff8d RS |
226 | number_of_panes = list_of_panes (&obj_list, &menus, &names, &enables, |
227 | &items, Fcdr (menu)); | |
088831f6 RS |
228 | } |
229 | #ifdef XDEBUG | |
230 | fprintf (stderr, "Panes = %d\n", number_of_panes); | |
231 | for (i = 0; i < number_of_panes; i++) | |
232 | { | |
233 | fprintf (stderr, "Pane %d has lines %d title %s\n", | |
234 | i, items[i], menus[i]); | |
235 | for (j = 0; j < items[i]; j++) | |
236 | fprintf (stderr, " Item %d %s\n", j, names[i][j]); | |
dcfdbac7 JB |
237 | } |
238 | #endif | |
239 | BLOCK_INPUT; | |
c4e5d591 JB |
240 | { |
241 | Window root; | |
242 | int root_x, root_y; | |
243 | int dummy_int; | |
244 | unsigned int dummy_uint; | |
245 | Window dummy_window; | |
246 | ||
247 | /* Figure out which root window F is on. */ | |
248 | XGetGeometry (x_current_display, FRAME_X_WINDOW (f), &root, | |
249 | &dummy_int, &dummy_int, &dummy_uint, &dummy_uint, | |
250 | &dummy_uint, &dummy_uint); | |
251 | ||
252 | /* Translate the menu co-ordinates within f to menu co-ordinates | |
253 | on that root window. */ | |
254 | if (! XTranslateCoordinates (x_current_display, | |
255 | FRAME_X_WINDOW (f), root, | |
256 | XMenu_xpos, XMenu_ypos, &root_x, &root_y, | |
257 | &dummy_window)) | |
258 | /* But XGetGeometry said root was the root window of f's screen! */ | |
259 | abort (); | |
260 | ||
aedaff8d RS |
261 | selection = xmenu_show (root, XMenu_xpos, XMenu_ypos, names, enables, |
262 | menus, items, number_of_panes, obj_list, title, | |
c4e5d591 JB |
263 | &error_name); |
264 | } | |
dcfdbac7 | 265 | UNBLOCK_INPUT; |
088831f6 | 266 | /* fprintf (stderr, "selection = %x\n", selection); */ |
dcfdbac7 JB |
267 | if (selection != NUL) |
268 | { /* selected something */ | |
269 | XMenu_return = selection; | |
270 | } | |
271 | else | |
272 | { /* nothing selected */ | |
273 | XMenu_return = Qnil; | |
274 | } | |
275 | /* now free up the strings */ | |
088831f6 | 276 | for (i = 0; i < number_of_panes; i++) |
dcfdbac7 JB |
277 | { |
278 | free (names[i]); | |
aedaff8d | 279 | free (enables[i]); |
dcfdbac7 JB |
280 | free (obj_list[i]); |
281 | } | |
282 | free (menus); | |
283 | free (obj_list); | |
284 | free (names); | |
aedaff8d | 285 | free (enables); |
dcfdbac7 | 286 | free (items); |
088831f6 | 287 | /* free (title); */ |
dcfdbac7 JB |
288 | if (error_name) error (error_name); |
289 | return XMenu_return; | |
290 | } | |
291 | ||
292 | struct indices { | |
293 | int pane; | |
294 | int line; | |
295 | }; | |
296 | ||
297 | Lisp_Object | |
aedaff8d RS |
298 | xmenu_show (parent, startx, starty, line_list, enable_list, pane_list, |
299 | line_cnt, pane_cnt, item_list, title, error) | |
dcfdbac7 JB |
300 | Window parent; |
301 | int startx, starty; /* upper left corner position BROKEN */ | |
302 | char **line_list[]; /* list of strings for items */ | |
aedaff8d | 303 | int *enable_list[]; /* list of strings for items */ |
dcfdbac7 JB |
304 | char *pane_list[]; /* list of pane titles */ |
305 | char *title; | |
306 | int pane_cnt; /* total number of panes */ | |
307 | Lisp_Object *item_list[]; /* All items */ | |
308 | int line_cnt[]; /* Lines in each pane */ | |
309 | char **error; /* Error returned */ | |
310 | { | |
311 | XMenu *GXMenu; | |
312 | int last, panes, selidx, lpane, status; | |
313 | int lines, sofar; | |
314 | Lisp_Object entry; | |
315 | /* struct indices *datap, *datap_save; */ | |
316 | char *datap; | |
317 | int ulx, uly, width, height; | |
318 | int dispwidth, dispheight; | |
088831f6 RS |
319 | |
320 | if (pane_cnt == 0) | |
321 | return 0; | |
322 | ||
dcfdbac7 JB |
323 | *error = (char *) 0; /* Initialize error pointer to null */ |
324 | GXMenu = XMenuCreate (XDISPLAY parent, "emacs"); | |
325 | if (GXMenu == NUL) | |
326 | { | |
327 | *error = "Can't create menu"; | |
328 | return (0); | |
329 | } | |
330 | ||
088831f6 RS |
331 | for (panes = 0, lines = 0; panes < pane_cnt; |
332 | lines += line_cnt[panes], panes++) | |
dcfdbac7 JB |
333 | ; |
334 | /* datap = (struct indices *) xmalloc (lines * sizeof (struct indices)); */ | |
088831f6 | 335 | /* datap = (char *) xmalloc (lines * sizeof (char)); |
dcfdbac7 JB |
336 | datap_save = datap;*/ |
337 | ||
088831f6 RS |
338 | for (panes = 0, sofar = 0; panes < pane_cnt; |
339 | sofar += line_cnt[panes], panes++) | |
dcfdbac7 JB |
340 | { |
341 | /* create all the necessary panes */ | |
342 | lpane = XMenuAddPane (XDISPLAY GXMenu, pane_list[panes], TRUE); | |
343 | if (lpane == XM_FAILURE) | |
344 | { | |
345 | XMenuDestroy (XDISPLAY GXMenu); | |
346 | *error = "Can't create pane"; | |
347 | return (0); | |
348 | } | |
088831f6 | 349 | for (selidx = 0; selidx < line_cnt[panes]; selidx++) |
dcfdbac7 JB |
350 | { |
351 | /* add the selection stuff to the menus */ | |
352 | /* datap[selidx+sofar].pane = panes; | |
353 | datap[selidx+sofar].line = selidx; */ | |
354 | if (XMenuAddSelection (XDISPLAY GXMenu, lpane, 0, | |
aedaff8d RS |
355 | line_list[panes][selidx], |
356 | enable_list[panes][selidx]) | |
dcfdbac7 JB |
357 | == XM_FAILURE) |
358 | { | |
359 | XMenuDestroy (XDISPLAY GXMenu); | |
360 | /* free (datap); */ | |
361 | *error = "Can't add selection to menu"; | |
362 | /* error ("Can't add selection to menu"); */ | |
363 | return (0); | |
364 | } | |
365 | } | |
366 | } | |
367 | /* all set and ready to fly */ | |
368 | XMenuRecompute (XDISPLAY GXMenu); | |
369 | dispwidth = DisplayWidth (x_current_display, XDefaultScreen (x_current_display)); | |
370 | dispheight = DisplayHeight (x_current_display, XDefaultScreen (x_current_display)); | |
371 | startx = min (startx, dispwidth); | |
372 | starty = min (starty, dispheight); | |
373 | startx = max (startx, 1); | |
374 | starty = max (starty, 1); | |
375 | XMenuLocate (XDISPLAY GXMenu, 0, 0, startx, starty, | |
376 | &ulx, &uly, &width, &height); | |
377 | if (ulx+width > dispwidth) | |
378 | { | |
379 | startx -= (ulx + width) - dispwidth; | |
380 | ulx = dispwidth - width; | |
381 | } | |
382 | if (uly+height > dispheight) | |
383 | { | |
384 | starty -= (uly + height) - dispheight; | |
385 | uly = dispheight - height; | |
386 | } | |
387 | if (ulx < 0) startx -= ulx; | |
388 | if (uly < 0) starty -= uly; | |
389 | ||
390 | XMenuSetFreeze (GXMenu, TRUE); | |
391 | panes = selidx = 0; | |
392 | ||
393 | status = XMenuActivate (XDISPLAY GXMenu, &panes, &selidx, | |
394 | startx, starty, ButtonReleaseMask, &datap); | |
395 | switch (status) | |
396 | { | |
397 | case XM_SUCCESS: | |
398 | #ifdef XDEBUG | |
399 | fprintf (stderr, "pane= %d line = %d\n", panes, selidx); | |
400 | #endif | |
401 | entry = item_list[panes][selidx]; | |
402 | break; | |
403 | case XM_FAILURE: | |
088831f6 | 404 | /* free (datap_save); */ |
dcfdbac7 JB |
405 | XMenuDestroy (XDISPLAY GXMenu); |
406 | *error = "Can't activate menu"; | |
407 | /* error ("Can't activate menu"); */ | |
408 | case XM_IA_SELECT: | |
409 | case XM_NO_SELECT: | |
410 | entry = Qnil; | |
411 | break; | |
412 | } | |
413 | XMenuDestroy (XDISPLAY GXMenu); | |
088831f6 | 414 | /* free (datap_save);*/ |
dcfdbac7 JB |
415 | return (entry); |
416 | } | |
417 | ||
418 | syms_of_xmenu () | |
419 | { | |
d9dcaf49 RS |
420 | Qmenu_enable = intern ("menu-enable"); |
421 | ||
422 | staticpro (&Qmenu_enable); | |
dcfdbac7 JB |
423 | defsubr (&Sx_popup_menu); |
424 | } | |
088831f6 RS |
425 | \f |
426 | /* Construct the vectors that describe a menu | |
aedaff8d | 427 | and store them in *VECTOR, *PANES, *NAMES, *ENABLES and *ITEMS. |
088831f6 RS |
428 | Each of those four values is a vector indexed by pane number. |
429 | Return the number of panes. | |
430 | ||
431 | KEYMAPS is a vector of keymaps. NMAPS gives the length of KEYMAPS. */ | |
432 | ||
433 | int | |
aedaff8d | 434 | keymap_panes (vector, panes, names, enables, items, keymaps, nmaps) |
088831f6 RS |
435 | Lisp_Object ***vector; /* RETURN all menu objects */ |
436 | char ***panes; /* RETURN pane names */ | |
437 | char ****names; /* RETURN all line names */ | |
aedaff8d | 438 | int ***enables; /* RETURN enable-flags of lines */ |
088831f6 RS |
439 | int **items; /* RETURN number of items per pane */ |
440 | Lisp_Object *keymaps; | |
441 | int nmaps; | |
442 | { | |
443 | /* Number of panes we have made. */ | |
444 | int p = 0; | |
445 | /* Number of panes we have space for. */ | |
446 | int npanes_allocated = nmaps; | |
447 | int mapno; | |
448 | ||
449 | if (npanes_allocated < 4) | |
450 | npanes_allocated = 4; | |
451 | ||
452 | /* Make space for an estimated number of panes. */ | |
453 | *vector = (Lisp_Object **) xmalloc (npanes_allocated * sizeof (Lisp_Object *)); | |
454 | *panes = (char **) xmalloc (npanes_allocated * sizeof (char *)); | |
455 | *items = (int *) xmalloc (npanes_allocated * sizeof (int)); | |
456 | *names = (char ***) xmalloc (npanes_allocated * sizeof (char **)); | |
aedaff8d | 457 | *enables = (int **) xmalloc (npanes_allocated * sizeof (int *)); |
088831f6 RS |
458 | |
459 | /* Loop over the given keymaps, making a pane for each map. | |
460 | But don't make a pane that is empty--ignore that map instead. | |
461 | P is the number of panes we have made so far. */ | |
462 | for (mapno = 0; mapno < nmaps; mapno++) | |
aedaff8d | 463 | single_keymap_panes (keymaps[mapno], panes, vector, names, enables, items, |
088831f6 RS |
464 | &p, &npanes_allocated, ""); |
465 | ||
466 | /* Return the number of panes. */ | |
467 | return p; | |
468 | } | |
469 | ||
470 | /* This is a recursive subroutine of the previous function. | |
471 | It handles one keymap, KEYMAP. | |
472 | The other arguments are passed along | |
473 | or point to local variables of the previous function. */ | |
474 | ||
aedaff8d | 475 | single_keymap_panes (keymap, panes, vector, names, enables, items, |
088831f6 RS |
476 | p_ptr, npanes_allocated_ptr, pane_name) |
477 | Lisp_Object keymap; | |
478 | Lisp_Object ***vector; /* RETURN all menu objects */ | |
479 | char ***panes; /* RETURN pane names */ | |
480 | char ****names; /* RETURN all line names */ | |
aedaff8d | 481 | int ***enables; /* RETURN enable flags of lines */ |
088831f6 RS |
482 | int **items; /* RETURN number of items per pane */ |
483 | int *p_ptr; | |
484 | int *npanes_allocated_ptr; | |
485 | char *pane_name; | |
486 | { | |
487 | int i; | |
488 | Lisp_Object pending_maps; | |
489 | Lisp_Object tail, item, item1, item2, table; | |
490 | ||
491 | pending_maps = Qnil; | |
492 | ||
493 | /* Make sure we have room for another pane. */ | |
494 | if (*p_ptr == *npanes_allocated_ptr) | |
495 | { | |
496 | *npanes_allocated_ptr *= 2; | |
497 | ||
498 | *vector | |
499 | = (Lisp_Object **) xrealloc (*vector, | |
500 | *npanes_allocated_ptr * sizeof (Lisp_Object *)); | |
501 | *panes | |
502 | = (char **) xrealloc (*panes, | |
503 | *npanes_allocated_ptr * sizeof (char *)); | |
504 | *items | |
505 | = (int *) xrealloc (*items, | |
506 | *npanes_allocated_ptr * sizeof (int)); | |
507 | *names | |
508 | = (char ***) xrealloc (*names, | |
509 | *npanes_allocated_ptr * sizeof (char **)); | |
aedaff8d RS |
510 | *enables |
511 | = (int **) xrealloc (*enables, | |
512 | *npanes_allocated_ptr * sizeof (int *)); | |
088831f6 RS |
513 | } |
514 | ||
515 | /* When a menu comes from keymaps, don't give names to the panes. */ | |
516 | (*panes)[*p_ptr] = pane_name; | |
517 | ||
518 | /* Get the length of the list level of the keymap. */ | |
519 | i = XFASTINT (Flength (keymap)); | |
520 | ||
ab6ee1a0 RS |
521 | /* Add in lengths of any arrays. */ |
522 | for (tail = keymap; XTYPE (tail) == Lisp_Cons; tail = XCONS (tail)->cdr) | |
523 | if (XTYPE (XCONS (tail)->car) == Lisp_Vector) | |
524 | i += XVECTOR (XCONS (tail)->car)->size; | |
088831f6 RS |
525 | |
526 | /* Create vectors for the names and values of the items in the pane. | |
527 | I is an upper bound for the number of items. */ | |
528 | (*vector)[*p_ptr] = (Lisp_Object *) xmalloc (i * sizeof (Lisp_Object)); | |
529 | (*names)[*p_ptr] = (char **) xmalloc (i * sizeof (char *)); | |
aedaff8d | 530 | (*enables)[*p_ptr] = (int *) xmalloc (i * sizeof (int)); |
088831f6 RS |
531 | |
532 | /* I is now the index of the next unused slots. */ | |
533 | i = 0; | |
534 | for (tail = keymap; XTYPE (tail) == Lisp_Cons; tail = XCONS (tail)->cdr) | |
535 | { | |
536 | /* Look at each key binding, and if it has a menu string, | |
537 | make a menu item from it. */ | |
538 | item = XCONS (tail)->car; | |
539 | if (XTYPE (item) == Lisp_Cons) | |
540 | { | |
541 | item1 = XCONS (item)->cdr; | |
542 | if (XTYPE (item1) == Lisp_Cons) | |
543 | { | |
544 | item2 = XCONS (item1)->car; | |
545 | if (XTYPE (item2) == Lisp_String) | |
546 | { | |
d9dcaf49 RS |
547 | Lisp_Object def, tem; |
548 | Lisp_Object enabled; | |
549 | ||
550 | def = Fcdr (item1); | |
551 | enabled = Qt; | |
552 | if (XTYPE (def) == Lisp_Symbol) | |
553 | { | |
554 | /* No property, or nil, means enable. | |
555 | Otherwise, enable if value is not nil. */ | |
556 | tem = Fget (def, Qmenu_enable); | |
557 | if (!NILP (tem)) | |
558 | enabled = Feval (tem); | |
559 | } | |
560 | tem = Fkeymapp (def); | |
088831f6 | 561 | if (XSTRING (item2)->data[0] == '@' && !NILP (tem)) |
d9dcaf49 | 562 | pending_maps = Fcons (Fcons (def, item2), |
088831f6 | 563 | pending_maps); |
aedaff8d | 564 | else |
088831f6 RS |
565 | { |
566 | (*names)[*p_ptr][i] = (char *) XSTRING (item2)->data; | |
567 | /* The menu item "value" is the key bound here. */ | |
568 | (*vector)[*p_ptr][i] = XCONS (item)->car; | |
aedaff8d RS |
569 | (*enables)[*p_ptr][i] |
570 | = (!NILP (enabled) ? 1 | |
571 | : NILP (def) ? -1 : 0); | |
088831f6 RS |
572 | i++; |
573 | } | |
574 | } | |
575 | } | |
576 | } | |
ab6ee1a0 RS |
577 | else if (XTYPE (item) == Lisp_Vector) |
578 | { | |
579 | /* Loop over the char values represented in the vector. */ | |
580 | int len = XVECTOR (item)->size; | |
581 | int c; | |
582 | for (c = 0; c < len; c++) | |
583 | { | |
584 | Lisp_Object character; | |
585 | XFASTINT (character) = c; | |
586 | item1 = XVECTOR (item)->contents[c]; | |
587 | if (XTYPE (item1) == Lisp_Cons) | |
588 | { | |
589 | item2 = XCONS (item1)->car; | |
590 | if (XTYPE (item2) == Lisp_String) | |
591 | { | |
592 | Lisp_Object tem; | |
d9dcaf49 RS |
593 | Lisp_Object def; |
594 | Lisp_Object enabled; | |
595 | ||
596 | def = Fcdr (item1); | |
597 | enabled = Qt; | |
598 | if (XTYPE (def) == Lisp_Symbol) | |
599 | { | |
600 | tem = Fget (def, Qmenu_enable); | |
601 | /* No property, or nil, means enable. | |
602 | Otherwise, enable if value is not nil. */ | |
603 | if (!NILP (tem)) | |
604 | enabled = Feval (tem); | |
605 | } | |
606 | ||
607 | tem = Fkeymapp (def); | |
ab6ee1a0 | 608 | if (XSTRING (item2)->data[0] == '@' && !NILP (tem)) |
d9dcaf49 | 609 | pending_maps = Fcons (Fcons (def, item2), |
ab6ee1a0 | 610 | pending_maps); |
aedaff8d | 611 | else |
ab6ee1a0 RS |
612 | { |
613 | (*names)[*p_ptr][i] = (char *) XSTRING (item2)->data; | |
614 | /* The menu item "value" is the key bound here. */ | |
615 | (*vector)[*p_ptr][i] = character; | |
aedaff8d RS |
616 | (*enables)[*p_ptr][i] |
617 | = (!NILP (enabled) ? 1 | |
618 | : NILP (def) ? -1 : 0); | |
ab6ee1a0 RS |
619 | i++; |
620 | } | |
621 | } | |
622 | } | |
623 | } | |
624 | } | |
088831f6 RS |
625 | } |
626 | /* Record the number of items in the pane. */ | |
627 | (*items)[*p_ptr] = i; | |
628 | ||
629 | /* If we just made an empty pane, get rid of it. */ | |
630 | if (i == 0) | |
631 | { | |
632 | free ((*vector)[*p_ptr]); | |
633 | free ((*names)[*p_ptr]); | |
aedaff8d | 634 | free ((*enables)[*p_ptr]); |
088831f6 RS |
635 | } |
636 | /* Otherwise, advance past it. */ | |
637 | else | |
638 | (*p_ptr)++; | |
639 | ||
640 | /* Process now any submenus which want to be panes at this level. */ | |
641 | while (!NILP (pending_maps)) | |
642 | { | |
643 | Lisp_Object elt; | |
644 | elt = Fcar (pending_maps); | |
aedaff8d | 645 | single_keymap_panes (Fcar (elt), panes, vector, names, enables, items, |
088831f6 RS |
646 | p_ptr, npanes_allocated_ptr, |
647 | /* Add 1 to discard the @. */ | |
648 | (char *) XSTRING (XCONS (elt)->cdr)->data + 1); | |
649 | pending_maps = Fcdr (pending_maps); | |
650 | } | |
651 | } | |
652 | \f | |
653 | /* Construct the vectors that describe a menu | |
aedaff8d | 654 | and store them in *VECTOR, *PANES, *NAMES, *ENABLES and *ITEMS. |
088831f6 RS |
655 | Each of those four values is a vector indexed by pane number. |
656 | Return the number of panes. | |
657 | ||
658 | MENU is the argument that was given to Fx_popup_menu. */ | |
dcfdbac7 | 659 | |
088831f6 | 660 | int |
aedaff8d | 661 | list_of_panes (vector, panes, names, enables, items, menu) |
dcfdbac7 JB |
662 | Lisp_Object ***vector; /* RETURN all menu objects */ |
663 | char ***panes; /* RETURN pane names */ | |
664 | char ****names; /* RETURN all line names */ | |
aedaff8d | 665 | int ***enables; /* RETURN enable flags of lines */ |
dcfdbac7 JB |
666 | int **items; /* RETURN number of items per pane */ |
667 | Lisp_Object menu; | |
668 | { | |
669 | Lisp_Object tail, item, item1; | |
670 | int i; | |
671 | ||
672 | if (XTYPE (menu) != Lisp_Cons) menu = wrong_type_argument (Qlistp, menu); | |
673 | ||
088831f6 | 674 | i = XFASTINT (Flength (menu)); |
dcfdbac7 JB |
675 | |
676 | *vector = (Lisp_Object **) xmalloc (i * sizeof (Lisp_Object *)); | |
677 | *panes = (char **) xmalloc (i * sizeof (char *)); | |
678 | *items = (int *) xmalloc (i * sizeof (int)); | |
679 | *names = (char ***) xmalloc (i * sizeof (char **)); | |
aedaff8d | 680 | *enables = (int **) xmalloc (i * sizeof (int *)); |
dcfdbac7 | 681 | |
088831f6 | 682 | for (i = 0, tail = menu; !NILP (tail); tail = Fcdr (tail), i++) |
dcfdbac7 | 683 | { |
088831f6 RS |
684 | item = Fcdr (Fcar (tail)); |
685 | if (XTYPE (item) != Lisp_Cons) (void) wrong_type_argument (Qlistp, item); | |
dcfdbac7 | 686 | #ifdef XDEBUG |
088831f6 | 687 | fprintf (stderr, "list_of_panes check tail, i=%d\n", i); |
dcfdbac7 | 688 | #endif |
088831f6 RS |
689 | item1 = Fcar (Fcar (tail)); |
690 | CHECK_STRING (item1, 1); | |
dcfdbac7 | 691 | #ifdef XDEBUG |
088831f6 RS |
692 | fprintf (stderr, "list_of_panes check pane, i=%d%s\n", i, |
693 | XSTRING (item1)->data); | |
dcfdbac7 | 694 | #endif |
088831f6 | 695 | (*panes)[i] = (char *) XSTRING (item1)->data; |
aedaff8d | 696 | (*items)[i] = list_of_items ((*vector)+i, (*names)+i, (*enables)+i, item); |
088831f6 RS |
697 | /* (*panes)[i] = (char *) xmalloc ((XSTRING (item1)->size)+1); |
698 | bcopy (XSTRING (item1)->data, (*panes)[i], XSTRING (item1)->size + 1) | |
699 | ; */ | |
dcfdbac7 JB |
700 | } |
701 | return i; | |
702 | } | |
088831f6 RS |
703 | \f |
704 | /* Construct the lists of values and names for a single pane, from the | |
aedaff8d RS |
705 | alist PANE. Put them in *VECTOR and *NAMES. Put the enable flags |
706 | int *ENABLES. Return the number of items. */ | |
dcfdbac7 | 707 | |
088831f6 | 708 | int |
aedaff8d | 709 | list_of_items (vector, names, enables, pane) |
dcfdbac7 JB |
710 | Lisp_Object **vector; /* RETURN menu "objects" */ |
711 | char ***names; /* RETURN line names */ | |
aedaff8d | 712 | int **enables; /* RETURN enable flags of lines */ |
dcfdbac7 JB |
713 | Lisp_Object pane; |
714 | { | |
715 | Lisp_Object tail, item, item1; | |
716 | int i; | |
717 | ||
718 | if (XTYPE (pane) != Lisp_Cons) pane = wrong_type_argument (Qlistp, pane); | |
719 | ||
f1b28218 | 720 | i = XFASTINT (Flength (pane)); |
dcfdbac7 JB |
721 | |
722 | *vector = (Lisp_Object *) xmalloc (i * sizeof (Lisp_Object)); | |
723 | *names = (char **) xmalloc (i * sizeof (char *)); | |
aedaff8d | 724 | *enables = (int *) xmalloc (i * sizeof (int)); |
dcfdbac7 | 725 | |
088831f6 | 726 | for (i = 0, tail = pane; !NILP (tail); tail = Fcdr (tail), i++) |
dcfdbac7 | 727 | { |
088831f6 RS |
728 | item = Fcar (tail); |
729 | if (XTYPE (item) != Lisp_Cons) (void) wrong_type_argument (Qlistp, item); | |
dcfdbac7 | 730 | #ifdef XDEBUG |
088831f6 | 731 | fprintf (stderr, "list_of_items check tail, i=%d\n", i); |
dcfdbac7 | 732 | #endif |
088831f6 RS |
733 | (*vector)[i] = Fcdr (item); |
734 | item1 = Fcar (item); | |
735 | CHECK_STRING (item1, 1); | |
dcfdbac7 | 736 | #ifdef XDEBUG |
088831f6 RS |
737 | fprintf (stderr, "list_of_items check item, i=%d%s\n", i, |
738 | XSTRING (item1)->data); | |
dcfdbac7 | 739 | #endif |
088831f6 | 740 | (*names)[i] = (char *) XSTRING (item1)->data; |
aedaff8d | 741 | (*enables)[i] = 1; |
dcfdbac7 JB |
742 | } |
743 | return i; | |
744 | } |