118
|
1 /* X Communication module for terminals which understand the X protocol.
|
|
2 Copyright (C) 1986, 1988 Free Software Foundation, Inc.
|
|
3
|
|
4 This file is part of GNU Emacs.
|
|
5
|
|
6 GNU Emacs is free software; you can redistribute it and/or modify
|
|
7 it under the terms of the GNU General Public License as published by
|
|
8 the Free Software Foundation; either version 1, or (at your option)
|
|
9 any later version.
|
|
10
|
|
11 GNU Emacs is distributed in the hope that it will be useful,
|
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 GNU General Public License for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with GNU Emacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, 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
|
|
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"
|
770
|
35 #include "frame.h"
|
118
|
36 #include "window.h"
|
|
37
|
|
38 /* This may include sys/types.h, and that somehow loses
|
|
39 if this is not done before the other system files. */
|
|
40 #include "xterm.h"
|
|
41
|
|
42 /* Load sys/types.h if not already loaded.
|
|
43 In some systems loading it twice is suicidal. */
|
|
44 #ifndef makedev
|
|
45 #include <sys/types.h>
|
|
46 #endif
|
|
47
|
|
48 #include "dispextern.h"
|
|
49
|
|
50 #ifdef HAVE_X11
|
|
51 #include "../oldXMenu/XMenu.h"
|
|
52 #else
|
|
53 #include <X/XMenu.h>
|
|
54 #endif
|
|
55
|
|
56 #define min(x,y) (((x) < (y)) ? (x) : (y))
|
|
57 #define max(x,y) (((x) > (y)) ? (x) : (y))
|
|
58
|
|
59 #define NUL 0
|
|
60
|
|
61 #ifndef TRUE
|
|
62 #define TRUE 1
|
|
63 #define FALSE 0
|
|
64 #endif TRUE
|
|
65
|
|
66 #ifdef HAVE_X11
|
|
67 extern Display *x_current_display;
|
|
68 #else
|
|
69 #define ButtonReleaseMask ButtonReleased
|
|
70 #endif /* not HAVE_X11 */
|
|
71
|
|
72 Lisp_Object xmenu_show ();
|
|
73 extern int x_error_handler ();
|
|
74
|
|
75 /*************************************************************/
|
|
76
|
|
77 #if 0
|
|
78 /* Ignoring the args is easiest. */
|
|
79 xmenu_quit ()
|
|
80 {
|
|
81 error ("Unknown XMenu error");
|
|
82 }
|
|
83 #endif
|
|
84
|
|
85 DEFUN ("x-popup-menu",Fx_popup_menu, Sx_popup_menu, 1, 2, 0,
|
|
86 "Pop up a deck-of-cards menu and return user's selection.\n\
|
|
87 ARG is a position specification: a list ((XOFFSET YOFFSET) WINDOW)\n\
|
|
88 where XOFFSET and YOFFSET are positions in characters from the top left\n\
|
770
|
89 corner of WINDOW's frame. A mouse-event list will serve for this.\n\
|
118
|
90 This controls the position of the center of the first line\n\
|
|
91 in the first pane of the menu, not the top left of the menu as a whole.\n\
|
|
92 \n\
|
|
93 MENU is a specifier for a menu. It is a list of the form\n\
|
|
94 \(TITLE PANE1 PANE2...), and each pane is a list of form\n\
|
|
95 \(TITLE (LINE ITEM)...). Each line should be a string, and item should\n\
|
|
96 be the return value for that line (i.e. if it is selected.")
|
|
97 (arg, menu)
|
|
98 Lisp_Object arg, menu;
|
|
99 {
|
|
100 int number_of_panes;
|
|
101 Lisp_Object XMenu_return;
|
|
102 int XMenu_xpos, XMenu_ypos;
|
|
103 char **menus;
|
|
104 char ***names;
|
|
105 Lisp_Object **obj_list;
|
|
106 int *items;
|
|
107 char *title;
|
|
108 char *error_name;
|
|
109 Lisp_Object ltitle, selection;
|
|
110 int i, j;
|
770
|
111 FRAME_PTR f;
|
118
|
112 Lisp_Object x, y, window;
|
|
113
|
|
114 window = Fcar (Fcdr (arg));
|
|
115 x = Fcar (Fcar (arg));
|
|
116 y = Fcar (Fcdr (Fcar (arg)));
|
|
117 CHECK_WINDOW (window, 0);
|
|
118 CHECK_NUMBER (x, 0);
|
|
119 CHECK_NUMBER (y, 0);
|
770
|
120 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
|
118
|
121
|
770
|
122 XMenu_xpos = FONT_WIDTH (f->display.x->font) * XINT (x);
|
|
123 XMenu_ypos = FONT_HEIGHT (f->display.x->font) * XINT (y);
|
|
124 XMenu_xpos += f->display.x->left_pos;
|
|
125 XMenu_ypos += f->display.x->top_pos;
|
118
|
126
|
|
127 ltitle = Fcar (menu);
|
|
128 CHECK_STRING (ltitle, 1);
|
|
129 title = (char *) XSTRING (ltitle)->data;
|
|
130 number_of_panes=list_of_panes (&obj_list, &menus, &names, &items, Fcdr (menu));
|
|
131 #ifdef XDEBUG
|
|
132 fprintf (stderr, "Panes= %d\n", number_of_panes);
|
|
133 for (i=0; i < number_of_panes; i++)
|
|
134 {
|
|
135 fprintf (stderr, "Pane %d lines %d title %s\n", i, items[i], menus[i]);
|
|
136 for (j=0; j < items[i]; j++)
|
|
137 {
|
|
138 fprintf (stderr, " Item %d %s\n", j, names[i][j]);
|
|
139 }
|
|
140 }
|
|
141 #endif
|
|
142 BLOCK_INPUT;
|
|
143 selection = xmenu_show (ROOT_WINDOW, XMenu_xpos, XMenu_ypos, names, menus,
|
|
144 items, number_of_panes, obj_list, title, &error_name);
|
|
145 UNBLOCK_INPUT;
|
|
146 /** fprintf (stderr, "selection = %x\n", selection); **/
|
|
147 if (selection != NUL)
|
|
148 { /* selected something */
|
|
149 XMenu_return = selection;
|
|
150 }
|
|
151 else
|
|
152 { /* nothing selected */
|
|
153 XMenu_return = Qnil;
|
|
154 }
|
|
155 /* now free up the strings */
|
|
156 for (i=0; i < number_of_panes; i++)
|
|
157 {
|
|
158 free (names[i]);
|
|
159 free (obj_list[i]);
|
|
160 }
|
|
161 free (menus);
|
|
162 free (obj_list);
|
|
163 free (names);
|
|
164 free (items);
|
|
165 /* free (title); */
|
|
166 if (error_name) error (error_name);
|
|
167 return XMenu_return;
|
|
168 }
|
|
169
|
|
170 struct indices {
|
|
171 int pane;
|
|
172 int line;
|
|
173 };
|
|
174
|
|
175 Lisp_Object
|
|
176 xmenu_show (parent, startx, starty, line_list, pane_list, line_cnt,
|
|
177 pane_cnt, item_list, title, error)
|
|
178 Window parent;
|
|
179 int startx, starty; /* upper left corner position BROKEN */
|
|
180 char **line_list[]; /* list of strings for items */
|
|
181 char *pane_list[]; /* list of pane titles */
|
|
182 char *title;
|
|
183 int pane_cnt; /* total number of panes */
|
|
184 Lisp_Object *item_list[]; /* All items */
|
|
185 int line_cnt[]; /* Lines in each pane */
|
|
186 char **error; /* Error returned */
|
|
187 {
|
|
188 XMenu *GXMenu;
|
|
189 int last, panes, selidx, lpane, status;
|
|
190 int lines, sofar;
|
|
191 Lisp_Object entry;
|
|
192 /* struct indices *datap, *datap_save; */
|
|
193 char *datap;
|
|
194 int ulx, uly, width, height;
|
|
195 int dispwidth, dispheight;
|
|
196
|
|
197 *error = (char *) 0; /* Initialize error pointer to null */
|
|
198 GXMenu = XMenuCreate (XDISPLAY parent, "emacs");
|
|
199 if (GXMenu == NUL)
|
|
200 {
|
|
201 *error = "Can't create menu";
|
|
202 return (0);
|
|
203 }
|
|
204
|
|
205 for (panes=0, lines=0; panes < pane_cnt; lines += line_cnt[panes], panes++)
|
|
206 ;
|
|
207 /* datap = (struct indices *) xmalloc (lines * sizeof (struct indices)); */
|
|
208 /*datap = (char *) xmalloc (lines * sizeof (char));
|
|
209 datap_save = datap;*/
|
|
210
|
|
211 for (panes = 0, sofar=0;panes < pane_cnt;sofar +=line_cnt[panes], panes++)
|
|
212 {
|
|
213 /* create all the necessary panes */
|
|
214 lpane = XMenuAddPane (XDISPLAY GXMenu, pane_list[panes], TRUE);
|
|
215 if (lpane == XM_FAILURE)
|
|
216 {
|
|
217 XMenuDestroy (XDISPLAY GXMenu);
|
|
218 *error = "Can't create pane";
|
|
219 return (0);
|
|
220 }
|
|
221 for (selidx = 0; selidx < line_cnt[panes] ; selidx++)
|
|
222 {
|
|
223 /* add the selection stuff to the menus */
|
|
224 /* datap[selidx+sofar].pane = panes;
|
|
225 datap[selidx+sofar].line = selidx; */
|
|
226 if (XMenuAddSelection (XDISPLAY GXMenu, lpane, 0,
|
|
227 line_list[panes][selidx], TRUE)
|
|
228 == XM_FAILURE)
|
|
229 {
|
|
230 XMenuDestroy (XDISPLAY GXMenu);
|
|
231 /* free (datap); */
|
|
232 *error = "Can't add selection to menu";
|
|
233 /* error ("Can't add selection to menu"); */
|
|
234 return (0);
|
|
235 }
|
|
236 }
|
|
237 }
|
|
238 /* all set and ready to fly */
|
|
239 XMenuRecompute (XDISPLAY GXMenu);
|
|
240 dispwidth = DisplayWidth (x_current_display, XDefaultScreen (x_current_display));
|
|
241 dispheight = DisplayHeight (x_current_display, XDefaultScreen (x_current_display));
|
|
242 startx = min (startx, dispwidth);
|
|
243 starty = min (starty, dispheight);
|
|
244 startx = max (startx, 1);
|
|
245 starty = max (starty, 1);
|
|
246 XMenuLocate (XDISPLAY GXMenu, 0, 0, startx, starty,
|
|
247 &ulx, &uly, &width, &height);
|
|
248 if (ulx+width > dispwidth)
|
|
249 {
|
|
250 startx -= (ulx + width) - dispwidth;
|
|
251 ulx = dispwidth - width;
|
|
252 }
|
|
253 if (uly+height > dispheight)
|
|
254 {
|
|
255 starty -= (uly + height) - dispheight;
|
|
256 uly = dispheight - height;
|
|
257 }
|
|
258 if (ulx < 0) startx -= ulx;
|
|
259 if (uly < 0) starty -= uly;
|
|
260
|
|
261 XMenuSetFreeze (GXMenu, TRUE);
|
|
262 panes = selidx = 0;
|
|
263
|
|
264 status = XMenuActivate (XDISPLAY GXMenu, &panes, &selidx,
|
|
265 startx, starty, ButtonReleaseMask, &datap);
|
|
266 switch (status)
|
|
267 {
|
|
268 case XM_SUCCESS:
|
|
269 #ifdef XDEBUG
|
|
270 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
|
|
271 #endif
|
|
272 entry = item_list[panes][selidx];
|
|
273 break;
|
|
274 case XM_FAILURE:
|
|
275 /*free (datap_save); */
|
|
276 XMenuDestroy (XDISPLAY GXMenu);
|
|
277 *error = "Can't activate menu";
|
|
278 /* error ("Can't activate menu"); */
|
|
279 case XM_IA_SELECT:
|
|
280 case XM_NO_SELECT:
|
|
281 entry = Qnil;
|
|
282 break;
|
|
283 }
|
|
284 XMenuDestroy (XDISPLAY GXMenu);
|
|
285 /*free (datap_save);*/
|
|
286 return (entry);
|
|
287 }
|
|
288
|
|
289 syms_of_xmenu ()
|
|
290 {
|
|
291 defsubr (&Sx_popup_menu);
|
|
292 }
|
|
293
|
|
294 list_of_panes (vector, panes, names, items, menu)
|
|
295 Lisp_Object ***vector; /* RETURN all menu objects */
|
|
296 char ***panes; /* RETURN pane names */
|
|
297 char ****names; /* RETURN all line names */
|
|
298 int **items; /* RETURN number of items per pane */
|
|
299 Lisp_Object menu;
|
|
300 {
|
|
301 Lisp_Object tail, item, item1;
|
|
302 int i;
|
|
303
|
|
304 if (XTYPE (menu) != Lisp_Cons) menu = wrong_type_argument (Qlistp, menu);
|
|
305
|
|
306 i= XFASTINT (Flength (menu, 1));
|
|
307
|
|
308 *vector = (Lisp_Object **) xmalloc (i * sizeof (Lisp_Object *));
|
|
309 *panes = (char **) xmalloc (i * sizeof (char *));
|
|
310 *items = (int *) xmalloc (i * sizeof (int));
|
|
311 *names = (char ***) xmalloc (i * sizeof (char **));
|
|
312
|
485
|
313 for (i=0, tail = menu; !NILP (tail); tail = Fcdr (tail), i++)
|
118
|
314 {
|
|
315 item = Fcdr (Fcar (tail));
|
|
316 if (XTYPE (item) != Lisp_Cons) (void) wrong_type_argument (Qlistp, item);
|
|
317 #ifdef XDEBUG
|
|
318 fprintf (stderr, "list_of_panes check tail, i=%d\n", i);
|
|
319 #endif
|
|
320 item1 = Fcar (Fcar (tail));
|
|
321 CHECK_STRING (item1, 1);
|
|
322 #ifdef XDEBUG
|
|
323 fprintf (stderr, "list_of_panes check pane, i=%d%s\n", i,
|
|
324 XSTRING (item1)->data);
|
|
325 #endif
|
|
326 (*panes)[i] = (char *) XSTRING (item1)->data;
|
|
327 (*items)[i] = list_of_items ((*vector)+i, (*names)+i, item);
|
|
328 /* (*panes)[i] = (char *) xmalloc ((XSTRING (item1)->size)+1);
|
|
329 bcopy (XSTRING (item1)->data, (*panes)[i], XSTRING (item1)->size + 1)
|
|
330 ; */
|
|
331 }
|
|
332 return i;
|
|
333 }
|
|
334
|
|
335
|
|
336 list_of_items (vector, names, pane) /* get list from emacs and put to vector */
|
|
337 Lisp_Object **vector; /* RETURN menu "objects" */
|
|
338 char ***names; /* RETURN line names */
|
|
339 Lisp_Object pane;
|
|
340 {
|
|
341 Lisp_Object tail, item, item1;
|
|
342 int i;
|
|
343
|
|
344 if (XTYPE (pane) != Lisp_Cons) pane = wrong_type_argument (Qlistp, pane);
|
|
345
|
|
346 i= XFASTINT (Flength (pane, 1));
|
|
347
|
|
348 *vector = (Lisp_Object *) xmalloc (i * sizeof (Lisp_Object));
|
|
349 *names = (char **) xmalloc (i * sizeof (char *));
|
|
350
|
485
|
351 for (i=0, tail = pane; !NILP (tail); tail = Fcdr (tail), i++)
|
118
|
352 {
|
|
353 item = Fcar (tail);
|
|
354 if (XTYPE (item) != Lisp_Cons) (void) wrong_type_argument (Qlistp, item);
|
|
355 #ifdef XDEBUG
|
|
356 fprintf (stderr, "list_of_items check tail, i=%d\n", i);
|
|
357 #endif
|
|
358 (*vector)[i] = Fcdr (item);
|
|
359 item1 = Fcar (item);
|
|
360 CHECK_STRING (item1, 1);
|
|
361 #ifdef XDEBUG
|
|
362 fprintf (stderr, "list_of_items check item, i=%d%s\n", i,
|
|
363 XSTRING (item1)->data);
|
|
364 #endif
|
|
365 (*names)[i] = (char *) XSTRING (item1)->data;
|
|
366 }
|
|
367 return i;
|
|
368 }
|