1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 1988, 1993 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
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.
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. */
20 /* X pop-up deck-of-cards menu facility for gnuemacs.
22 * Written by Jon Arnold and Roman Budzianowski
23 * Mods and rewrite by Robert Krawitz
27 /* Modified by Fred Pierresteguy on December 93
28 to make the popup menus and menubar use the Xt. */
32 /* On 4.3 this loses if it comes after xterm.h. */
36 #include "termhooks.h"
40 #include "blockinput.h"
42 /* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
46 /* Load sys/types.h if not already loaded.
47 In some systems loading it twice is suicidal. */
49 #include <sys/types.h>
52 #include "dispextern.h"
55 #include "../oldXMenu/XMenu.h"
62 #include <X11/IntrinsicP.h>
63 #include <X11/CoreP.h>
64 #include <X11/StringDefs.h>
65 #include <X11/Xaw/Paned.h>
66 #include "../lwlib/lwlib.h"
67 #include "../lwlib/xlwmenuP.h"
68 #endif /* USE_X_TOOLKIT */
70 #define min(x,y) (((x) < (y)) ? (x) : (y))
71 #define max(x,y) (((x) > (y)) ? (x) : (y))
81 extern Display
*x_current_display
;
83 #define ButtonReleaseMask ButtonReleased
84 #endif /* not HAVE_X11 */
86 extern Lisp_Object Qmenu_enable
;
87 extern Lisp_Object Qmenu_bar
;
88 Lisp_Object
xmenu_show ();
89 extern int x_error_handler ();
91 static widget_value
*set_menu_items ();
92 static int string_width ();
93 static void free_menu_items ();
96 /* we need a unique id for each popup menu and dialog box */
97 unsigned int popup_id_tick
;
99 /*************************************************************/
102 /* Ignoring the args is easiest. */
105 error ("Unknown XMenu error");
110 DEFUN ("x-popup-menu",Fx_popup_menu
, Sx_popup_menu
, 1, 2, 0,
111 "Pop up a deck-of-cards menu and return user's selection.\n\
112 POSITION is a position specification. This is either a mouse button event\n\
113 or a list ((XOFFSET YOFFSET) WINDOW)\n\
114 where XOFFSET and YOFFSET are positions in characters from the top left\n\
115 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
116 This controls the position of the center of the first line\n\
117 in the first pane of the menu, not the top left of the menu as a whole.\n\
119 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
120 The menu items come from key bindings that have a menu string as well as\n\
121 a definition; actually, the \"definition\" in such a key binding looks like\n\
122 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
123 the keymap as a top-level element.\n\n\
124 You can also use a list of keymaps as MENU.\n\
125 Then each keymap makes a separate pane.\n\
126 When MENU is a keymap or a list of keymaps, the return value\n\
127 is a list of events.\n\n\
128 Alternatively, you can specify a menu of multiple panes\n\
129 with a list of the form (TITLE PANE1 PANE2...),\n\
130 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
131 Each ITEM is normally a cons cell (STRING . VALUE);\n\
132 but a string can appear as an item--that makes a nonselectable line\n\
134 With this form of menu, the return value is VALUE from the chosen item.")
136 Lisp_Object position
, menu
;
138 int number_of_panes
, panes
;
139 Lisp_Object XMenu_return
, keymap
, tem
;
140 int XMenu_xpos
, XMenu_ypos
;
144 Lisp_Object
**obj_list
;
145 Lisp_Object
*prefixes
;
149 Lisp_Object ltitle
, selection
;
150 int i
, j
, menubarp
= 0;
152 Lisp_Object x
, y
, window
;
154 widget_value
*val
, *vw
= 0;
155 #endif /* USE_X_TOOLKIT */
157 /* Decode the first argument: find the window and the coordinates. */
158 tem
= Fcar (position
);
159 if (XTYPE (tem
) == Lisp_Cons
)
161 window
= Fcar (Fcdr (position
));
163 y
= Fcar (Fcdr (tem
));
167 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
168 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
169 tem
= Fcar (Fcdr (Fcdr (tem
))); /* POSN_WINDOW_POSN (tem) */
176 if (XTYPE (window
) == Lisp_Frame
)
183 else if (XTYPE (window
) == Lisp_Window
)
185 CHECK_LIVE_WINDOW (window
, 0);
186 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
188 XMenu_xpos
= FONT_WIDTH (f
->display
.x
->font
)
189 * XWINDOW (window
)->left
;
190 XMenu_ypos
= FONT_HEIGHT (f
->display
.x
->font
)
191 * XWINDOW (window
)->top
;
194 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
195 but I don't want to make one now. */
196 CHECK_WINDOW (window
, 0);
199 tem
= Fcar (Fcdr (Fcar (Fcdr (position
))));
200 if (XTYPE (Fcar (position
)) != Lisp_Cons
202 && EQ (Fcar (tem
), Qmenu_bar
))
204 /* We are in the menubar */
208 mw
= (XlwMenuWidget
)f
->display
.x
->menubar_widget
;
210 for (vw
= mw
->menu
.old_stack
[0]->contents
; vw
; vw
= vw
->next
)
213 w1
+= string_width (mw
, vw
->name
)
214 + 2 * (mw
->menu
.horizontal_spacing
+
215 mw
->menu
.shadow_thickness
);
226 XMenu_xpos
+= FONT_WIDTH (f
->display
.x
->font
) * XINT (x
);
227 XMenu_ypos
+= FONT_HEIGHT (f
->display
.x
->font
) * XINT (y
);
231 XMenu_xpos
+= (f
->display
.x
->widget
->core
.x
232 + f
->display
.x
->widget
->core
.border_width
);
233 XMenu_ypos
+= (f
->display
.x
->widget
->core
.y
234 + f
->display
.x
->widget
->core
.border_width
235 + f
->display
.x
->menubar_widget
->core
.height
);
238 val
= set_menu_items (menu
, &prefixes
, &panes
, &names
,
239 &enables
, &menus
, &items
, &number_of_panes
, &obj_list
,
240 &title
, &error_name
);
241 selection
= xmenu_show (f
, val
, XMenu_xpos
, XMenu_ypos
,
244 free_menu_items (names
, enables
, menus
, items
, number_of_panes
, obj_list
,
247 if (selection
!= NUL
)
248 { /* selected something */
249 XMenu_return
= selection
;
252 { /* nothing selected */
258 #else /* not USE_X_TOOLKIT */
262 int win_x
= 0, win_y
= 0;
264 /* Find the position of the outside upper-left corner of
265 the inner window, with respect to the outer window. */
266 if (f
->display
.x
->parent_desc
!= ROOT_WINDOW
)
269 XTranslateCoordinates (x_current_display
,
271 /* From-window, to-window. */
272 f
->display
.x
->window_desc
,
273 f
->display
.x
->parent_desc
,
275 /* From-position, to-position. */
276 0, 0, &win_x
, &win_y
,
278 /* Child of window. */
285 #endif /* HAVE_X11 */
287 XMenu_xpos
+= FONT_WIDTH (f
->display
.x
->font
) * XINT (x
);
288 XMenu_ypos
+= FONT_HEIGHT (f
->display
.x
->font
) * XINT (y
);
290 XMenu_xpos
+= f
->display
.x
->left_pos
;
291 XMenu_ypos
+= f
->display
.x
->top_pos
;
294 keymap
= Fkeymapp (menu
);
296 if (XTYPE (menu
) == Lisp_Cons
)
297 tem
= Fkeymapp (Fcar (menu
));
300 /* We were given a keymap. Extract menu info from the keymap. */
302 keymap
= get_keymap (menu
);
304 /* Search for a string appearing directly as an element of the keymap.
305 That string is the title of the menu. */
306 prompt
= map_prompt (keymap
);
308 title
= (char *) XSTRING (prompt
)->data
;
310 /* Extract the detailed info to make one pane. */
311 number_of_panes
= keymap_panes (&obj_list
, &menus
, &names
, &enables
,
312 &items
, &prefixes
, &menu
, 1);
313 /* The menu title seems to be ignored,
314 so put it in the pane title. */
318 else if (!NILP (tem
))
320 /* We were given a list of keymaps. */
322 int nmaps
= XFASTINT (Flength (menu
));
324 = (Lisp_Object
*) alloca (nmaps
* sizeof (Lisp_Object
));
328 /* The first keymap that has a prompt string
329 supplies the menu title. */
330 for (tem
= menu
, i
= 0; XTYPE (tem
) == Lisp_Cons
; tem
= Fcdr (tem
))
332 maps
[i
++] = keymap
= get_keymap (Fcar (tem
));
334 prompt
= map_prompt (keymap
);
335 if (title
== 0 && !NILP (prompt
))
336 title
= (char *) XSTRING (prompt
)->data
;
339 /* Extract the detailed info to make one pane. */
340 number_of_panes
= keymap_panes (&obj_list
, &menus
, &names
, &enables
,
341 &items
, &prefixes
, maps
, nmaps
);
342 /* The menu title seems to be ignored,
343 so put it in the pane title. */
349 /* We were given an old-fashioned menu. */
350 ltitle
= Fcar (menu
);
351 CHECK_STRING (ltitle
, 1);
352 title
= (char *) XSTRING (ltitle
)->data
;
354 number_of_panes
= list_of_panes (&obj_list
, &menus
, &names
, &enables
,
355 &items
, Fcdr (menu
));
358 fprintf (stderr
, "Panes = %d\n", number_of_panes
);
359 for (i
= 0; i
< number_of_panes
; i
++)
361 fprintf (stderr
, "Pane %d has lines %d title %s\n",
362 i
, items
[i
], menus
[i
]);
363 for (j
= 0; j
< items
[i
]; j
++)
364 fprintf (stderr
, " Item %d %s\n", j
, names
[i
][j
]);
373 unsigned int dummy_uint
;
376 /* Figure out which root window F is on. */
377 XGetGeometry (x_current_display
, FRAME_X_WINDOW (f
), &root
,
378 &dummy_int
, &dummy_int
, &dummy_uint
, &dummy_uint
,
379 &dummy_uint
, &dummy_uint
);
381 /* Translate the menu co-ordinates within f to menu co-ordinates
382 on that root window. */
383 if (! XTranslateCoordinates (x_current_display
,
384 FRAME_X_WINDOW (f
), root
,
385 XMenu_xpos
, XMenu_ypos
, &root_x
, &root_y
,
387 /* But XGetGeometry said root was the root window of f's screen! */
389 selection
= xmenu_show (root
, XMenu_xpos
, XMenu_ypos
, names
, enables
,
390 menus
, prefixes
, items
, number_of_panes
, obj_list
,
394 /* fprintf (stderr, "selection = %x\n", selection); */
395 if (selection
!= NUL
)
396 { /* selected something */
397 XMenu_return
= selection
;
400 { /* nothing selected */
403 /* now free up the strings */
404 for (i
= 0; i
< number_of_panes
; i
++)
416 if (error_name
) error (error_name
);
418 #endif /* not USE_X_TOOLKIT */
424 dispatch_dummy_expose (w
, x
, y
)
432 dummy
.window
= XtWindow (w
);
435 dummy
.send_event
= 0;
436 dummy
.display
= XtDisplay (w
);
440 XtDispatchEvent (&dummy
);
451 XTextExtents (mw
->menu
.font
, s
, strlen (s
), &drop
, &drop
, &drop
, &xcs
);
456 event_is_in_menu_item (mw
, event
, name
, string_w
)
458 struct input_event
*event
;
462 *string_w
+= string_width (mw
, name
)
463 + 2 * (mw
->menu
.horizontal_spacing
+ mw
->menu
.shadow_thickness
);
464 return (XINT (event
->x
) < *string_w
);
469 map_event_to_object (event
, f
)
470 struct input_event
*event
;
475 XlwMenuWidget mw
= (XlwMenuWidget
) f
->display
.x
->menubar_widget
;
480 /* Find the window */
481 for (val
= mw
->menu
.old_stack
[0]->contents
; val
; val
= val
->next
)
483 ws
= &mw
->menu
.windows
[0];
484 if (ws
&& event_is_in_menu_item (mw
, event
, val
->name
, &string_w
))
487 items
= FRAME_MENU_BAR_ITEMS (f
);
488 for (; CONSP (items
); items
= XCONS (items
)->cdr
)
489 if (!strcmp (val
->name
,
490 XSTRING (Fcar (Fcdr (Fcar (items
))))->data
))
497 static widget_value
*
498 set_menu_items (menu
, prefixes
, panes
, names
, enables
, menus
,
499 items
, number_of_panes
, obj_list
, title
, error_name
)
501 Lisp_Object
**prefixes
;
507 int *number_of_panes
;
508 Lisp_Object
***obj_list
;
512 Lisp_Object keymap
, tem
;
513 Lisp_Object ltitle
, selection
;
515 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
516 int last
, selidx
, lpane
, status
;
519 keymap
= Fkeymapp (menu
);
522 if (XTYPE (menu
) == Lisp_Cons
)
523 tem
= Fkeymapp (Fcar (menu
));
526 /* We were given a keymap. Extract menu info from the keymap. */
528 keymap
= get_keymap (menu
);
530 /* Search for a string appearing directly as an element of the keymap.
531 That string is the title of the menu. */
532 prompt
= map_prompt (keymap
);
534 *title
= (char *) XSTRING (prompt
)->data
;
536 /* Extract the detailed info to make one pane. */
537 *number_of_panes
= keymap_panes (obj_list
, menus
, names
, enables
,
538 items
, prefixes
, menu
, 1);
539 /* The menu title seems to be ignored,
540 so put it in the pane title. */
541 if ((*menus
)[0] == 0)
542 (*menus
)[0] = *title
;
544 else if (!NILP (tem
))
546 /* We were given a list of keymaps. */
548 int nmaps
= XFASTINT (Flength (menu
));
550 = (Lisp_Object
*) alloca (nmaps
* sizeof (Lisp_Object
));
554 /* The first keymap that has a prompt string
555 supplies the menu title. */
556 for (tem
= menu
, i
= 0; XTYPE (tem
) == Lisp_Cons
; tem
= Fcdr (tem
))
558 maps
[i
++] = keymap
= get_keymap (Fcar (tem
));
560 prompt
= map_prompt (keymap
);
561 if (*title
== 0 && !NILP (prompt
))
562 *title
= (char *) XSTRING (prompt
)->data
;
565 /* Extract the detailed info to make one pane. */
566 *number_of_panes
= keymap_panes (obj_list
, menus
, names
, enables
,
567 items
, prefixes
, maps
, nmaps
);
568 /* The menu title seems to be ignored,
569 so put it in the pane title. */
570 if ((*menus
)[0] == 0)
571 (*menus
)[0] = *title
;
575 /* We were given an old-fashioned menu. */
576 ltitle
= Fcar (menu
);
577 CHECK_STRING (ltitle
, 1);
578 *title
= (char *) XSTRING (ltitle
)->data
;
580 *number_of_panes
= list_of_panes (obj_list
, menus
, names
, enables
,
585 if (*number_of_panes
== 0)
588 *error_name
= (char *) 0; /* Initialize error pointer to null */
590 wv
= malloc_widget_value ();
596 for (*panes
= 0, lines
= 0; *panes
< *number_of_panes
;
597 lines
+= (*items
)[*panes
], (*panes
)++)
599 /* datap = (struct indices *) xmalloc (lines * sizeof (struct indices)); */
600 /* datap = (char *) xmalloc (lines * sizeof (char));
601 datap_save = datap;*/
603 for (*panes
= 0, sofar
= 0; *panes
< *number_of_panes
;
604 sofar
+= (*items
)[*panes
], (*panes
)++)
606 if (strcmp((*menus
)[*panes
], ""))
608 wv
= malloc_widget_value ();
612 first_wv
->contents
= wv
;
613 wv
->name
= (*menus
)[*panes
];
620 for (selidx
= 0; selidx
< (*items
)[*panes
]; selidx
++)
622 wv
= malloc_widget_value ();
626 save_wv
->contents
= wv
;
627 wv
->name
= (*names
)[*panes
][selidx
];
629 selection
= (*obj_list
)[*panes
][selidx
];
632 selection
= Fcons (selection
, Qnil
);
633 if (!NILP ((*prefixes
)[*panes
]))
634 selection
= Fcons ((*prefixes
)[*panes
], selection
);
636 wv
->call_data
= LISP_TO_VOID(selection
);
637 wv
->enabled
= (*enables
)[*panes
][selidx
];
646 free_menu_items (names
, enables
, menus
, items
, number_of_panes
,
647 obj_list
, title
, error_name
)
653 Lisp_Object
**obj_list
;
658 /* now free up the strings */
659 for (i
= 0; i
< number_of_panes
; i
++)
671 if (error_name
) error (error_name
);
675 static Lisp_Object menu_item_selection
;
678 popup_selection_callback (widget
, id
, client_data
)
681 XtPointer client_data
;
684 last_popup_selection_callback_id
= id
;
685 menubar_selection_callback (widget
, id
, client_data
);
686 /* lw_destroy_all_widgets() will be called from popup_down_callback() */
688 VOID_TO_LISP (menu_item_selection
, client_data
);
692 popup_down_callback (widget
, id
, client_data
)
695 XtPointer client_data
;
698 if (popup_menu_up_p
== 0) abort ();
700 /* if this isn't called immediately after the selection callback, then
701 there wasn't a menu selection. */
702 if (id
!= last_popup_selection_callback_id
)
703 menubar_selection_callback (widget
, id
, (XtPointer
) -1);
706 lw_destroy_all_widgets (id
);
708 /* ungcpro_popup_callbacks (make_number (id)); */
711 /* This recursively calls free_widget_value() on the tree of widgets.
712 It must free all data that was malloc'ed for these widget_values.
713 Currently, emacs only allocates new storage for the `key' slot.
714 All other slots are pointers into the data of Lisp_Strings, and
718 free_menubar_widget_value_tree (wv
)
722 if (wv
->key
) xfree (wv
->key
);
724 wv
->name
= wv
->value
= wv
->key
= (char *) 0xDEADBEEF;
726 if (wv
->contents
&& (wv
->contents
!= (widget_value
*)1))
728 free_menubar_widget_value_tree (wv
->contents
);
729 wv
->contents
= (widget_value
*) 0xDEADBEEF;
733 free_menubar_widget_value_tree (wv
->next
);
734 wv
->next
= (widget_value
*) 0xDEADBEEF;
737 free_widget_value (wv
);
742 update_one_frame_psheets (f
)
745 struct x_display
*x
= f
->display
.x
;
749 menubar_changed
= (x
->menubar_widget
750 && !XtIsManaged (x
->menubar_widget
));
752 if (! (menubar_changed
))
756 XawPanedSetRefigureMode (x
->column_widget
, 0);
758 /* the order in which children are managed is the top to
759 bottom order in which they are displayed in the paned window.
760 First, remove the text-area widget.
762 XtUnmanageChild (x
->edit_widget
);
764 /* remove the menubar that is there now, and put up the menubar that
769 XtManageChild (x
->menubar_widget
);
770 XtMapWidget (x
->menubar_widget
);
771 XtVaSetValues (x
->menubar_widget
, XtNmappedWhenManaged
, 1, 0);
775 /* Re-manage the text-area widget */
776 XtManageChild (x
->edit_widget
);
778 /* and now thrash the sizes */
779 XawPanedSetRefigureMode (x
->column_widget
, 1);
784 set_frame_menubar (f
)
787 Widget menubar_widget
= f
->display
.x
->menubar_widget
;
790 widget_value
*wv
, *save_wv
, *first_wv
, *prev_wv
= 0;
794 wv
= malloc_widget_value ();
795 wv
->name
= "menubar";
798 save_wv
= first_wv
= wv
;
801 for (tail
= FRAME_MENU_BAR_ITEMS (f
); CONSP (tail
); tail
= XCONS (tail
)->cdr
)
805 string
= Fcar (Fcdr (Fcar (tail
)));
807 wv
= malloc_widget_value ();
811 save_wv
->contents
= wv
;
812 wv
->name
= XSTRING (string
)->data
;
819 lw_modify_all_widgets (id
, first_wv
, False
);
822 menubar_widget
= lw_create_widget ("menubar", "menubar",
824 f
->display
.x
->column_widget
,
827 f
->display
.x
->menubar_widget
= menubar_widget
;
828 XtVaSetValues (menubar_widget
,
830 XtNresizeToPreferred
, 1,
835 free_menubar_widget_value_tree (first_wv
);
837 update_one_frame_psheets (f
);
841 #endif /* USE_X_TOOLKIT */
848 extern void process_expose_from_menu ();
851 extern XtAppContext Xt_app_con
;
854 xmenu_show (f
, val
, x
, y
, menubarp
, vw
)
862 int menu_id
, id
= (int)f
;
863 Lisp_Object selection
;
867 * Define and allocate a foreign event queue to hold events
868 * that don't belong to XMenu. These events are later restored
869 * to the X event queue.
871 typedef struct _xmeventque
874 struct _xmeventque
*next
;
877 XMEventQue
*feq
= NULL
; /* Foreign event queue. */
878 XMEventQue
*feq_tmp
; /* Foreign event queue temporary. */
881 menu_id
= ++popup_id_tick
;
882 menu
= lw_create_widget ("popup", val
->name
, menu_id
, val
,
883 f
->display
.x
->widget
, 1, 0,
884 popup_selection_callback
, popup_down_callback
);
885 free_menubar_widget_value_tree (val
);
887 /* reset the selection */
888 menu_item_selection
= Qnil
;
891 XButtonPressedEvent dummy
;
894 mw
= ((XlwMenuWidget
)
895 ((CompositeWidget
)menu
)->composite
.children
[0]);
897 dummy
.type
= ButtonPress
;
899 dummy
.send_event
= 0;
900 dummy
.display
= XtDisplay (menu
);
901 dummy
.window
= XtWindow (XtParent (menu
));
902 dummy
.time
= CurrentTime
;
910 /* we are in the menubar */
911 XtUnmapWidget (f
->display
.x
->menubar_widget
);
912 vw
->call_data
= (XtPointer
) 1;
913 XtMapWidget (f
->display
.x
->menubar_widget
);
915 vw
->call_data
= (XtPointer
) 1;
916 dispatch_dummy_expose (f
->display
.x
->menubar_widget
, x
, y
);
921 /* We activate directly the lucid implementation */
922 pop_up_menu (mw
, &dummy
);
925 /* Enters XEvent loop */
930 XtAppNextEvent (Xt_app_con
, &event
);
931 if (event
.type
== ButtonRelease
)
933 XtDispatchEvent (&event
);
937 if (event
.type
== Expose
)
938 process_expose_from_menu (event
);
939 XtDispatchEvent (&event
);
940 feq_tmp
= (XMEventQue
*) malloc (sizeof (XMEventQue
));
945 feq_tmp
->event
= event
;
953 XtUnmapWidget (f
->display
.x
->menubar_widget
);
954 vw
->call_data
= (XtPointer
) 0;
955 XtMapWidget (f
->display
.x
->menubar_widget
);
957 vw
->call_data
= (XtPointer
) 0;
958 dispatch_dummy_expose (f
->display
.x
->menubar_widget
, 0, 0);
963 /* Return any foreign events that were queued to the X event queue. */
967 XPutBackEvent (XDISPLAY
&feq_tmp
->event
);
969 free ((char *)feq_tmp
);
974 return menu_item_selection
;
977 #else /* not USE_X_TOOLKIT */
978 xmenu_show (parent
, startx
, starty
, line_list
, enable_list
, pane_list
,
979 prefixes
, line_cnt
, pane_cnt
, item_list
, title
, error
)
981 int startx
, starty
; /* upper left corner position BROKEN */
982 char **line_list
[]; /* list of strings for items */
983 int *enable_list
[]; /* enable flags of lines */
984 char *pane_list
[]; /* list of pane titles */
985 Lisp_Object
*prefixes
; /* Prefix key for each pane */
987 int pane_cnt
; /* total number of panes */
988 Lisp_Object
*item_list
[]; /* All items */
989 int line_cnt
[]; /* Lines in each pane */
990 char **error
; /* Error returned */
993 int last
, panes
, selidx
, lpane
, status
;
996 /* struct indices *datap, *datap_save; */
998 int ulx
, uly
, width
, height
;
999 int dispwidth
, dispheight
;
1006 *error
= (char *) 0; /* Initialize error pointer to null */
1008 GXMenu
= XMenuCreate (XDISPLAY parent
, "emacs");
1011 *error
= "Can't create menu";
1016 for (panes
= 0, lines
= 0; panes
< pane_cnt
;
1017 lines
+= line_cnt
[panes
], panes
++)
1019 /* datap = (struct indices *) xmalloc (lines * sizeof (struct indices)); */
1020 /* datap = (char *) xmalloc (lines * sizeof (char));
1021 datap_save = datap;*/
1023 for (panes
= 0, sofar
= 0; panes
< pane_cnt
;
1024 sofar
+= line_cnt
[panes
], panes
++)
1026 /* create all the necessary panes */
1027 lpane
= XMenuAddPane (XDISPLAY GXMenu
, pane_list
[panes
], TRUE
);
1028 if (lpane
== XM_FAILURE
)
1030 XMenuDestroy (XDISPLAY GXMenu
);
1031 *error
= "Can't create pane";
1036 for (selidx
= 0; selidx
< line_cnt
[panes
]; selidx
++)
1038 /* add the selection stuff to the menus */
1039 /* datap[selidx+sofar].pane = panes;
1040 datap[selidx+sofar].line = selidx; */
1041 if (XMenuAddSelection (XDISPLAY GXMenu
, lpane
, 0,
1042 line_list
[panes
][selidx
],
1043 enable_list
[panes
][selidx
])
1046 XMenuDestroy (XDISPLAY GXMenu
);
1048 *error
= "Can't add selection to menu";
1049 /* error ("Can't add selection to menu"); */
1055 /* all set and ready to fly */
1056 XMenuRecompute (XDISPLAY GXMenu
);
1057 dispwidth
= DisplayWidth (x_current_display
, XDefaultScreen (x_current_display
));
1058 dispheight
= DisplayHeight (x_current_display
, XDefaultScreen (x_current_display
));
1059 startx
= min (startx
, dispwidth
);
1060 starty
= min (starty
, dispheight
);
1061 startx
= max (startx
, 1);
1062 starty
= max (starty
, 1);
1063 XMenuLocate (XDISPLAY GXMenu
, 0, 0, startx
, starty
,
1064 &ulx
, &uly
, &width
, &height
);
1065 if (ulx
+width
> dispwidth
)
1067 startx
-= (ulx
+ width
) - dispwidth
;
1068 ulx
= dispwidth
- width
;
1070 if (uly
+height
> dispheight
)
1072 starty
-= (uly
+ height
) - dispheight
;
1073 uly
= dispheight
- height
;
1075 if (ulx
< 0) startx
-= ulx
;
1076 if (uly
< 0) starty
-= uly
;
1078 XMenuSetFreeze (GXMenu
, TRUE
);
1081 status
= XMenuActivate (XDISPLAY GXMenu
, &panes
, &selidx
,
1082 startx
, starty
, ButtonReleaseMask
, &datap
);
1087 fprintf (stderr
, "pane= %d line = %d\n", panes
, selidx
);
1089 entry
= item_list
[panes
][selidx
];
1092 entry
= Fcons (entry
, Qnil
);
1093 if (!NILP (prefixes
[panes
]))
1094 entry
= Fcons (prefixes
[panes
], entry
);
1098 /* free (datap_save); */
1099 XMenuDestroy (XDISPLAY GXMenu
);
1100 *error
= "Can't activate menu";
1101 /* error ("Can't activate menu"); */
1107 XMenuDestroy (XDISPLAY GXMenu
);
1109 /* free (datap_save);*/
1112 #endif /* not USE_X_TOOLKIT */
1116 popup_id_tick
= (1<<16);
1117 defsubr (&Sx_popup_menu
);
1120 /* Construct the vectors that describe a menu
1121 and store them in *VECTOR, *PANES, *NAMES, *ENABLES and *ITEMS.
1122 Each of those four values is a vector indexed by pane number.
1123 Return the number of panes.
1125 KEYMAPS is a vector of keymaps. NMAPS gives the length of KEYMAPS. */
1128 keymap_panes (vector
, panes
, names
, enables
, items
, prefixes
, keymaps
, nmaps
)
1129 Lisp_Object
***vector
; /* RETURN all menu objects */
1130 char ***panes
; /* RETURN pane names */
1131 char ****names
; /* RETURN all line names */
1132 int ***enables
; /* RETURN enable-flags of lines */
1133 int **items
; /* RETURN number of items per pane */
1134 Lisp_Object
**prefixes
; /* RETURN vector of prefix keys, per pane */
1135 Lisp_Object
*keymaps
;
1138 /* Number of panes we have made. */
1140 /* Number of panes we have space for. */
1141 int npanes_allocated
= nmaps
;
1144 if (npanes_allocated
< 4)
1145 npanes_allocated
= 4;
1147 /* Make space for an estimated number of panes. */
1148 *vector
= (Lisp_Object
**) xmalloc (npanes_allocated
* sizeof (Lisp_Object
*));
1149 *panes
= (char **) xmalloc (npanes_allocated
* sizeof (char *));
1150 *items
= (int *) xmalloc (npanes_allocated
* sizeof (int));
1151 *names
= (char ***) xmalloc (npanes_allocated
* sizeof (char **));
1152 *enables
= (int **) xmalloc (npanes_allocated
* sizeof (int *));
1153 *prefixes
= (Lisp_Object
*) xmalloc (npanes_allocated
* sizeof (Lisp_Object
));
1155 /* Loop over the given keymaps, making a pane for each map.
1156 But don't make a pane that is empty--ignore that map instead.
1157 P is the number of panes we have made so far. */
1158 for (mapno
= 0; mapno
< nmaps
; mapno
++)
1159 single_keymap_panes (keymaps
[mapno
], panes
, vector
, names
, enables
, items
,
1160 prefixes
, &p
, &npanes_allocated
, "");
1162 /* Return the number of panes. */
1166 /* This is a recursive subroutine of the previous function.
1167 It handles one keymap, KEYMAP.
1168 The other arguments are passed along
1169 or point to local variables of the previous function. */
1171 single_keymap_panes (keymap
, panes
, vector
, names
, enables
, items
, prefixes
,
1172 p_ptr
, npanes_allocated_ptr
, pane_name
)
1174 Lisp_Object
***vector
; /* RETURN all menu objects */
1175 char ***panes
; /* RETURN pane names */
1176 char ****names
; /* RETURN all line names */
1177 int ***enables
; /* RETURN enable flags of lines */
1178 int **items
; /* RETURN number of items per pane */
1179 Lisp_Object
**prefixes
; /* RETURN vector of prefix keys, per pane */
1181 int *npanes_allocated_ptr
;
1185 Lisp_Object pending_maps
;
1186 Lisp_Object tail
, item
, item1
, item2
, table
;
1188 pending_maps
= Qnil
;
1190 /* Make sure we have room for another pane. */
1191 if (*p_ptr
== *npanes_allocated_ptr
)
1193 *npanes_allocated_ptr
*= 2;
1196 = (Lisp_Object
**) xrealloc (*vector
,
1197 *npanes_allocated_ptr
* sizeof (Lisp_Object
*));
1199 = (char **) xrealloc (*panes
,
1200 *npanes_allocated_ptr
* sizeof (char *));
1202 = (int *) xrealloc (*items
,
1203 *npanes_allocated_ptr
* sizeof (int));
1205 = (Lisp_Object
*) xrealloc (*prefixes
,
1206 (*npanes_allocated_ptr
1207 * sizeof (Lisp_Object
)));
1209 = (char ***) xrealloc (*names
,
1210 *npanes_allocated_ptr
* sizeof (char **));
1212 = (int **) xrealloc (*enables
,
1213 *npanes_allocated_ptr
* sizeof (int *));
1216 /* When a menu comes from keymaps, don't give names to the panes. */
1217 (*panes
)[*p_ptr
] = pane_name
;
1219 /* Normally put nil as pane's prefix key.
1220 Caller will override this if appropriate. */
1221 (*prefixes
)[*p_ptr
] = Qnil
;
1223 /* Get the length of the list level of the keymap. */
1224 i
= XFASTINT (Flength (keymap
));
1226 /* Add in lengths of any arrays. */
1227 for (tail
= keymap
; XTYPE (tail
) == Lisp_Cons
; tail
= XCONS (tail
)->cdr
)
1228 if (XTYPE (XCONS (tail
)->car
) == Lisp_Vector
)
1229 i
+= XVECTOR (XCONS (tail
)->car
)->size
;
1231 /* Create vectors for the names and values of the items in the pane.
1232 I is an upper bound for the number of items. */
1233 (*vector
)[*p_ptr
] = (Lisp_Object
*) xmalloc (i
* sizeof (Lisp_Object
));
1234 (*names
)[*p_ptr
] = (char **) xmalloc (i
* sizeof (char *));
1235 (*enables
)[*p_ptr
] = (int *) xmalloc (i
* sizeof (int));
1237 /* I is now the index of the next unused slots. */
1239 for (tail
= keymap
; XTYPE (tail
) == Lisp_Cons
; tail
= XCONS (tail
)->cdr
)
1241 /* Look at each key binding, and if it has a menu string,
1242 make a menu item from it. */
1243 item
= XCONS (tail
)->car
;
1244 if (XTYPE (item
) == Lisp_Cons
)
1246 item1
= XCONS (item
)->cdr
;
1247 if (XTYPE (item1
) == Lisp_Cons
)
1249 item2
= XCONS (item1
)->car
;
1250 if (XTYPE (item2
) == Lisp_String
)
1252 Lisp_Object def
, tem
;
1253 Lisp_Object enabled
;
1257 if (XTYPE (def
) == Lisp_Symbol
)
1259 /* No property, or nil, means enable.
1260 Otherwise, enable if value is not nil. */
1261 tem
= Fget (def
, Qmenu_enable
);
1263 enabled
= Feval (tem
);
1265 tem
= Fkeymapp (def
);
1266 if (XSTRING (item2
)->data
[0] == '@' && !NILP (tem
))
1267 pending_maps
= Fcons (Fcons (def
, Fcons (item2
, XCONS (item
)->car
)),
1271 (*names
)[*p_ptr
][i
] = (char *) XSTRING (item2
)->data
;
1272 /* The menu item "value" is the key bound here. */
1273 (*vector
)[*p_ptr
][i
] = XCONS (item
)->car
;
1274 (*enables
)[*p_ptr
][i
]
1275 = (NILP (def
) ? -1 : !NILP (enabled
) ? 1 : 0);
1281 else if (XTYPE (item
) == Lisp_Vector
)
1283 /* Loop over the char values represented in the vector. */
1284 int len
= XVECTOR (item
)->size
;
1286 for (c
= 0; c
< len
; c
++)
1288 Lisp_Object character
;
1289 XFASTINT (character
) = c
;
1290 item1
= XVECTOR (item
)->contents
[c
];
1291 if (XTYPE (item1
) == Lisp_Cons
)
1293 item2
= XCONS (item1
)->car
;
1294 if (XTYPE (item2
) == Lisp_String
)
1298 Lisp_Object enabled
;
1302 if (XTYPE (def
) == Lisp_Symbol
)
1304 tem
= Fget (def
, Qmenu_enable
);
1305 /* No property, or nil, means enable.
1306 Otherwise, enable if value is not nil. */
1308 enabled
= Feval (tem
);
1311 tem
= Fkeymapp (def
);
1312 if (XSTRING (item2
)->data
[0] == '@' && !NILP (tem
))
1313 pending_maps
= Fcons (Fcons (def
, Fcons (item2
, character
)),
1317 (*names
)[*p_ptr
][i
] = (char *) XSTRING (item2
)->data
;
1318 /* The menu item "value" is the key bound here. */
1319 (*vector
)[*p_ptr
][i
] = character
;
1320 (*enables
)[*p_ptr
][i
]
1321 = (NILP (def
) ? -1 : !NILP (enabled
) ? 1 : 0);
1329 /* Record the number of items in the pane. */
1330 (*items
)[*p_ptr
] = i
;
1332 /* If we just made an empty pane, get rid of it. */
1335 xfree ((*vector
)[*p_ptr
]);
1336 xfree ((*names
)[*p_ptr
]);
1337 xfree ((*enables
)[*p_ptr
]);
1339 /* Otherwise, advance past it. */
1343 /* Process now any submenus which want to be panes at this level. */
1344 while (!NILP (pending_maps
))
1346 Lisp_Object elt
, eltcdr
;
1347 int panenum
= *p_ptr
;
1348 elt
= Fcar (pending_maps
);
1349 eltcdr
= XCONS (elt
)->cdr
;
1350 single_keymap_panes (Fcar (elt
), panes
, vector
, names
, enables
, items
,
1351 prefixes
, p_ptr
, npanes_allocated_ptr
,
1352 /* Add 1 to discard the @. */
1353 (char *) XSTRING (XCONS (eltcdr
)->car
)->data
+ 1);
1354 (*prefixes
)[panenum
] = XCONS (eltcdr
)->cdr
;
1355 pending_maps
= Fcdr (pending_maps
);
1359 /* Construct the vectors that describe a menu
1360 and store them in *VECTOR, *PANES, *NAMES, *ENABLES and *ITEMS.
1361 Each of those four values is a vector indexed by pane number.
1362 Return the number of panes.
1364 MENU is the argument that was given to Fx_popup_menu. */
1367 list_of_panes (vector
, panes
, names
, enables
, items
, menu
)
1368 Lisp_Object
***vector
; /* RETURN all menu objects */
1369 char ***panes
; /* RETURN pane names */
1370 char ****names
; /* RETURN all line names */
1371 int ***enables
; /* RETURN enable flags of lines */
1372 int **items
; /* RETURN number of items per pane */
1375 Lisp_Object tail
, item
, item1
;
1378 if (XTYPE (menu
) != Lisp_Cons
) menu
= wrong_type_argument (Qlistp
, menu
);
1380 i
= XFASTINT (Flength (menu
));
1382 *vector
= (Lisp_Object
**) xmalloc (i
* sizeof (Lisp_Object
*));
1383 *panes
= (char **) xmalloc (i
* sizeof (char *));
1384 *items
= (int *) xmalloc (i
* sizeof (int));
1385 *names
= (char ***) xmalloc (i
* sizeof (char **));
1386 *enables
= (int **) xmalloc (i
* sizeof (int *));
1388 for (i
= 0, tail
= menu
; !NILP (tail
); tail
= Fcdr (tail
), i
++)
1390 item
= Fcdr (Fcar (tail
));
1391 if (XTYPE (item
) != Lisp_Cons
) (void) wrong_type_argument (Qlistp
, item
);
1393 fprintf (stderr
, "list_of_panes check tail, i=%d\n", i
);
1395 item1
= Fcar (Fcar (tail
));
1396 CHECK_STRING (item1
, 1);
1398 fprintf (stderr
, "list_of_panes check pane, i=%d%s\n", i
,
1399 XSTRING (item1
)->data
);
1401 (*panes
)[i
] = (char *) XSTRING (item1
)->data
;
1402 (*items
)[i
] = list_of_items ((*vector
)+i
, (*names
)+i
, (*enables
)+i
, item
);
1403 /* (*panes)[i] = (char *) xmalloc ((XSTRING (item1)->size)+1);
1404 bcopy (XSTRING (item1)->data, (*panes)[i], XSTRING (item1)->size + 1)
1410 /* Construct the lists of values and names for a single pane, from the
1411 alist PANE. Put them in *VECTOR and *NAMES. Put the enable flags
1412 int *ENABLES. Return the number of items. */
1415 list_of_items (vector
, names
, enables
, pane
)
1416 Lisp_Object
**vector
; /* RETURN menu "objects" */
1417 char ***names
; /* RETURN line names */
1418 int **enables
; /* RETURN enable flags of lines */
1421 Lisp_Object tail
, item
, item1
;
1424 if (XTYPE (pane
) != Lisp_Cons
) pane
= wrong_type_argument (Qlistp
, pane
);
1426 i
= XFASTINT (Flength (pane
));
1428 *vector
= (Lisp_Object
*) xmalloc (i
* sizeof (Lisp_Object
));
1429 *names
= (char **) xmalloc (i
* sizeof (char *));
1430 *enables
= (int *) xmalloc (i
* sizeof (int));
1432 for (i
= 0, tail
= pane
; !NILP (tail
); tail
= Fcdr (tail
), i
++)
1437 (*vector
)[i
] = Qnil
;
1438 (*names
)[i
] = (char *) XSTRING (item
)->data
;
1443 CHECK_CONS (item
, 0);
1444 (*vector
)[i
] = Fcdr (item
);
1445 item1
= Fcar (item
);
1446 CHECK_STRING (item1
, 1);
1447 (*names
)[i
] = (char *) XSTRING (item1
)->data
;