0
|
1 /* Implements an elisp-programmable menubar -- X interface.
|
|
2 Copyright (C) 1993, 1994 Free Software Foundation, Inc.
|
|
3 Copyright (C) 1995 Tinker Systems and INS Engineering Corp.
|
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 /* Synched up with: Not in FSF. */
|
|
23
|
|
24 /* created 16-dec-91 by jwz */
|
|
25
|
|
26 #include <config.h>
|
|
27 #include "lisp.h"
|
|
28
|
|
29 #include "console-x.h"
|
|
30 #include "EmacsManager.h"
|
|
31 #include "EmacsFrame.h"
|
|
32 #include "EmacsShell.h"
|
|
33 #include "gui-x.h"
|
|
34
|
|
35 #include "buffer.h"
|
|
36 #include "commands.h" /* zmacs_regions */
|
|
37 #include "events.h"
|
|
38 #include "frame.h"
|
|
39 #include "opaque.h"
|
|
40 #include "window.h"
|
|
41
|
|
42 static int set_frame_menubar (struct frame *f,
|
|
43 int deep_p,
|
|
44 int first_time_p);
|
|
45
|
|
46 #define FRAME_MENUBAR_DATA(frame) ((frame)->menubar_data)
|
|
47 #define XFRAME_MENUBAR_DATA(frame) XPOPUP_DATA ((frame)->menubar_data)
|
|
48
|
|
49 #define MENUBAR_TYPE 0
|
|
50 #define SUBMENU_TYPE 1
|
|
51 #define POPUP_TYPE 2
|
|
52
|
|
53
|
|
54 /* Converting Lisp menu tree descriptions to lwlib's `widget_value' form.
|
|
55
|
|
56 menu_item_descriptor_to_widget_value() converts a lisp description of a
|
|
57 menubar into a tree of widget_value structures. It allocates widget_values
|
|
58 with malloc_widget_value() and allocates other storage only for the `key'
|
|
59 slot. All other slots are filled with pointers to Lisp_String data. We
|
|
60 allocate a widget_value description of the menu or menubar, and hand it to
|
|
61 lwlib, which then makes a copy of it, which it manages internally. We then
|
|
62 immediately free our widget_value tree; it will not be referenced again.
|
|
63
|
|
64 Incremental menu construction callbacks operate just a bit differently.
|
|
65 They allocate widget_values and call replace_widget_value_tree() to tell
|
|
66 lwlib to destructively modify the incremental stub (subtree) of its
|
|
67 separate widget_value tree.
|
|
68
|
|
69 This function is highly recursive (it follows the menu trees) and may call
|
|
70 eval. The reason we keep pointers to lisp string data instead of copying
|
|
71 it and freeing it later is to avoid the speed penalty that would entail
|
|
72 (since this needs to be fast, in the simple cases at least). (The reason
|
|
73 we malloc/free the keys slot is because there's not a lisp string around
|
|
74 for us to use in that case.)
|
|
75
|
|
76 Since we keep pointers to lisp strings, and we call eval, we could lose if
|
|
77 GC relocates (or frees) those strings. It's not easy to gc protect the
|
|
78 strings because of the recursive nature of this function, and the fact that
|
|
79 it returns a data structure that gets freed later. So... we do the
|
|
80 sleaziest thing possible and inhibit GC for the duration. This is probably
|
|
81 not a big deal...
|
|
82
|
|
83 We do not have to worry about the pointers to Lisp_String data after
|
|
84 this function successfully finishes. lwlib copies all such data with
|
16
|
85 strdup(). */
|
0
|
86
|
|
87 static widget_value *
|
185
|
88 menu_item_descriptor_to_widget_value_1 (Lisp_Object desc,
|
0
|
89 int menu_type, int deep_p,
|
|
90 int filter_p,
|
|
91 int depth)
|
|
92 {
|
|
93 /* This function cannot GC.
|
|
94 It is only called from menu_item_descriptor_to_widget_value, which
|
|
95 prohibits GC. */
|
|
96 /* !!#### This function has not been Mule-ized */
|
|
97 int menubar_root_p = (menu_type == MENUBAR_TYPE && depth == 0);
|
|
98 widget_value *wv;
|
|
99 Lisp_Object wv_closure;
|
|
100 int count = specpdl_depth ();
|
|
101 int partition_seen = 0;
|
|
102
|
|
103 wv = xmalloc_widget_value ();
|
|
104
|
|
105 wv_closure = make_opaque_ptr (wv);
|
|
106 record_unwind_protect (widget_value_unwind, wv_closure);
|
|
107
|
|
108 if (STRINGP (desc))
|
|
109 {
|
16
|
110 char *string_chars = (char *) XSTRING_DATA (desc);
|
0
|
111 wv->type = (separator_string_p (string_chars) ? SEPARATOR_TYPE :
|
|
112 TEXT_TYPE);
|
|
113 #if 1
|
|
114 /* #### - should internationalize with X resources instead.
|
|
115 Not so! --ben */
|
|
116 string_chars = GETTEXT (string_chars);
|
|
117 #endif
|
|
118 if (wv->type == SEPARATOR_TYPE)
|
|
119 {
|
|
120 wv->value = menu_separator_style (string_chars);
|
185
|
121 }
|
0
|
122 else
|
|
123 {
|
|
124 wv->name = string_chars;
|
|
125 wv->enabled = 1;
|
|
126 }
|
|
127 }
|
|
128 else if (VECTORP (desc))
|
|
129 {
|
|
130 if (!button_item_to_widget_value (desc, wv, 1,
|
|
131 (menu_type == MENUBAR_TYPE
|
|
132 && depth <= 1)))
|
|
133 {
|
|
134 /* :included form was nil */
|
|
135 wv = NULL;
|
|
136 goto menu_item_done;
|
|
137 }
|
|
138 }
|
|
139 else if (CONSP (desc))
|
|
140 {
|
|
141 Lisp_Object incremental_data = desc;
|
|
142 widget_value *prev = 0;
|
|
143
|
|
144 if (STRINGP (XCAR (desc)))
|
|
145 {
|
|
146 Lisp_Object key, val;
|
173
|
147 Lisp_Object include_p = Qnil, hook_fn = Qnil, config_tag = Qnil;
|
175
|
148 Lisp_Object accel;
|
0
|
149 int included_spec = 0;
|
|
150 wv->type = CASCADE_TYPE;
|
|
151 wv->enabled = 1;
|
16
|
152 wv->name = (char *) XSTRING_DATA (LISP_GETTEXT (XCAR (desc)));
|
175
|
153
|
|
154 accel = menu_name_to_accelerator (wv->name);
|
|
155 wv->accel = LISP_TO_VOID (accel);
|
|
156
|
0
|
157 desc = Fcdr (desc);
|
|
158
|
|
159 while (key = Fcar (desc), KEYWORDP (key))
|
|
160 {
|
|
161 Lisp_Object cascade = desc;
|
|
162 desc = Fcdr (desc);
|
|
163 if (NILP (desc))
|
|
164 signal_simple_error ("keyword in menu lacks a value",
|
185
|
165 cascade);
|
0
|
166 val = Fcar (desc);
|
|
167 desc = Fcdr (desc);
|
|
168 if (EQ (key, Q_included))
|
|
169 include_p = val, included_spec = 1;
|
|
170 else if (EQ (key, Q_config))
|
|
171 config_tag = val;
|
|
172 else if (EQ (key, Q_filter))
|
|
173 hook_fn = val;
|
175
|
174 else if (EQ (key, Q_accelerator))
|
|
175 {
|
|
176 if ( SYMBOLP (val)
|
|
177 || CHARP (val))
|
|
178 wv->accel = LISP_TO_VOID (val);
|
|
179 else
|
|
180 signal_simple_error ("bad keyboard accelerator", val);
|
|
181 }
|
185
|
182 else
|
0
|
183 signal_simple_error ("unknown menu cascade keyword", cascade);
|
|
184 }
|
|
185
|
|
186 if ((!NILP (config_tag)
|
|
187 && NILP (Fmemq (config_tag, Vmenubar_configuration)))
|
|
188 || (included_spec && NILP (Feval (include_p))))
|
|
189 {
|
|
190 wv = NULL;
|
|
191 goto menu_item_done;
|
|
192 }
|
|
193 if (!NILP (hook_fn))
|
|
194 {
|
219
|
195 #if defined LWLIB_MENUBARS_LUCID || defined LWLIB_MENUBARS_MOTIF
|
0
|
196 if (filter_p || depth == 0)
|
|
197 {
|
185
|
198 #endif
|
0
|
199 desc = call1_trapping_errors ("Error in menubar filter",
|
|
200 hook_fn, desc);
|
|
201 if (UNBOUNDP (desc))
|
|
202 desc = Qnil;
|
219
|
203 #if defined LWLIB_MENUBARS_LUCID || defined LWLIB_MENUBARS_MOTIF
|
0
|
204 }
|
|
205 else
|
|
206 {
|
|
207 widget_value *incr_wv = xmalloc_widget_value ();
|
|
208 wv->contents = incr_wv;
|
|
209 incr_wv->type = INCREMENTAL_TYPE;
|
|
210 incr_wv->enabled = 1;
|
|
211 incr_wv->name = wv->name;
|
|
212 /* This is automatically GC protected through
|
|
213 the call to lw_map_widget_values(); no need
|
|
214 to worry. */
|
|
215 incr_wv->call_data = LISP_TO_VOID (incremental_data);
|
|
216 goto menu_item_done;
|
|
217 }
|
16
|
218 #endif /* LWLIB_MENUBARS_LUCID */
|
0
|
219 }
|
|
220 if (menu_type == POPUP_TYPE && popup_menu_titles && depth == 0)
|
|
221 {
|
|
222 /* Simply prepend three more widget values to the contents of
|
|
223 the menu: a label, and two separators (to get a double
|
|
224 line). */
|
|
225 widget_value *title_wv = xmalloc_widget_value ();
|
|
226 widget_value *sep_wv = xmalloc_widget_value ();
|
|
227 title_wv->type = TEXT_TYPE;
|
|
228 title_wv->name = wv->name;
|
|
229 title_wv->enabled = 1;
|
|
230 title_wv->next = sep_wv;
|
|
231 sep_wv->type = SEPARATOR_TYPE;
|
|
232 sep_wv->value = menu_separator_style ("==");
|
|
233 sep_wv->next = 0;
|
185
|
234
|
0
|
235 wv->contents = title_wv;
|
|
236 prev = sep_wv;
|
|
237 }
|
|
238 }
|
|
239 else if (menubar_root_p)
|
|
240 {
|
2
|
241 wv->name = (char *) "menubar";
|
0
|
242 wv->type = CASCADE_TYPE; /* Well, nothing else seems to fit and
|
|
243 this is ignored anyway... */
|
|
244 }
|
|
245 else
|
|
246 {
|
|
247 signal_simple_error ("menu name (first element) must be a string",
|
|
248 desc);
|
|
249 }
|
|
250
|
|
251 wv->enabled = 1;
|
|
252 if (deep_p || menubar_root_p)
|
|
253 {
|
|
254 widget_value *next;
|
|
255 for (; !NILP (desc); desc = Fcdr (desc))
|
|
256 {
|
|
257 Lisp_Object child = Fcar (desc);
|
|
258 if (menubar_root_p && NILP (child)) /* the partition */
|
|
259 {
|
|
260 if (partition_seen)
|
|
261 error (
|
|
262 "more than one partition (nil) in menubar description");
|
|
263 partition_seen = 1;
|
|
264 next = xmalloc_widget_value ();
|
|
265 next->type = PUSHRIGHT_TYPE;
|
|
266 }
|
|
267 else
|
|
268 {
|
16
|
269 next = menu_item_descriptor_to_widget_value_1
|
|
270 (child, menu_type, deep_p, filter_p, depth + 1);
|
0
|
271 }
|
|
272 if (! next)
|
|
273 continue;
|
|
274 else if (prev)
|
|
275 prev->next = next;
|
|
276 else
|
|
277 wv->contents = next;
|
|
278 prev = next;
|
|
279 }
|
|
280 }
|
|
281 if (deep_p && !wv->contents)
|
|
282 wv = NULL;
|
|
283 }
|
|
284 else if (NILP (desc))
|
|
285 error ("nil may not appear in menu descriptions");
|
|
286 else
|
|
287 signal_simple_error ("unrecognized menu descriptor", desc);
|
|
288
|
|
289 menu_item_done:
|
|
290
|
|
291 if (wv)
|
|
292 {
|
|
293 /* Completed normally. Clear out the object that widget_value_unwind()
|
|
294 will be called with to tell it not to free the wv (as we are
|
|
295 returning it.) */
|
|
296 set_opaque_ptr (wv_closure, 0);
|
|
297 }
|
|
298
|
|
299 unbind_to (count, Qnil);
|
|
300 return wv;
|
|
301 }
|
|
302
|
|
303 static widget_value *
|
185
|
304 menu_item_descriptor_to_widget_value (Lisp_Object desc,
|
0
|
305 int menu_type, /* if this is a menubar,
|
|
306 popup or sub menu */
|
|
307 int deep_p, /* */
|
|
308 int filter_p) /* if :filter forms
|
185
|
309 should run now */
|
0
|
310 {
|
|
311 widget_value *wv;
|
|
312 int count = specpdl_depth ();
|
|
313 record_unwind_protect (restore_gc_inhibit,
|
|
314 make_int (gc_currently_forbidden));
|
|
315 gc_currently_forbidden = 1;
|
|
316 /* Can't GC! */
|
|
317 wv = menu_item_descriptor_to_widget_value_1 (desc, menu_type, deep_p,
|
|
318 filter_p, 0);
|
|
319 unbind_to (count, Qnil);
|
|
320 return wv;
|
|
321 }
|
|
322
|
|
323
|
219
|
324 #if defined LWLIB_MENUBARS_LUCID || defined LWLIB_MENUBARS_MOTIF
|
138
|
325 int in_menu_callback;
|
|
326
|
169
|
327 static Lisp_Object
|
|
328 restore_in_menu_callback (Lisp_Object val)
|
138
|
329 {
|
|
330 in_menu_callback = XINT(val);
|
|
331 return Qnil;
|
|
332 }
|
|
333 #endif /* LWLIB_MENUBARS_LUCID */
|
|
334
|
|
335
|
0
|
336 /* The order in which callbacks are run is funny to say the least.
|
|
337 It's sometimes tricky to avoid running a callback twice, and to
|
|
338 avoid returning prematurely. So, this function returns true
|
|
339 if the menu's callbacks are no longer gc protected. So long
|
|
340 as we unprotect them before allowing other callbacks to run,
|
|
341 everything should be ok.
|
|
342
|
|
343 The pre_activate_callback() *IS* intentionally called multiple times.
|
|
344 If client_data == NULL, then it's being called before the menu is posted.
|
|
345 If client_data != NULL, then client_data is a (widget_value *) and
|
|
346 client_data->data is a Lisp_Object pointing to a lisp submenu description
|
|
347 that must be converted into widget_values. *client_data is destructively
|
185
|
348 modified.
|
0
|
349
|
|
350 #### Stig thinks that there may be a GC problem here due to the
|
|
351 fact that pre_activate_callback() is called multiple times, but I
|
|
352 think he's wrong.
|
|
353
|
|
354 */
|
|
355
|
|
356 static void
|
|
357 pre_activate_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
|
|
358 {
|
|
359 /* This function can GC */
|
|
360 struct gcpro gcpro1;
|
|
361 struct device *d = get_device_from_display (XtDisplay (widget));
|
|
362 struct frame *f = x_any_window_to_frame (d, XtWindow (widget));
|
|
363 Lisp_Object rest = Qnil;
|
82
|
364 Lisp_Object frame;
|
0
|
365 int any_changes = 0;
|
138
|
366 int count;
|
0
|
367
|
175
|
368 /* set in lwlib to the time stamp associated with the most recent menu
|
|
369 operation */
|
|
370 extern Time x_focus_timestamp_really_sucks_fix_me_better;
|
|
371
|
0
|
372 if (!f)
|
|
373 f = x_any_window_to_frame (d, XtWindow (XtParent (widget)));
|
|
374 if (!f)
|
|
375 return;
|
|
376
|
82
|
377 /* make sure f is the selected frame */
|
|
378 XSETFRAME (frame, f);
|
|
379 Fselect_frame (frame);
|
|
380
|
0
|
381 if (client_data)
|
|
382 {
|
|
383 /* this is an incremental menu construction callback */
|
|
384 widget_value *hack_wv = (widget_value *) client_data;
|
|
385 Lisp_Object submenu_desc;
|
|
386 widget_value *wv;
|
|
387
|
|
388 assert (hack_wv->type == INCREMENTAL_TYPE);
|
|
389 VOID_TO_LISP (submenu_desc, hack_wv->call_data);
|
138
|
390
|
|
391 /*
|
|
392 * #### Fix the menu code so this isn't necessary.
|
|
393 *
|
|
394 * Protect against reentering the menu code otherwise we will
|
|
395 * crash later when the code gets confused at the state
|
|
396 * changes.
|
|
397 */
|
|
398 count = specpdl_depth ();
|
|
399 record_unwind_protect (restore_in_menu_callback,
|
|
400 make_int (in_menu_callback));
|
|
401 in_menu_callback = 1;
|
0
|
402 wv = menu_item_descriptor_to_widget_value (submenu_desc, SUBMENU_TYPE,
|
114
|
403 1, 0);
|
138
|
404 unbind_to (count, Qnil);
|
|
405
|
0
|
406 if (!wv)
|
|
407 {
|
|
408 wv = xmalloc_widget_value ();
|
|
409 wv->type = CASCADE_TYPE;
|
|
410 wv->next = NULL;
|
|
411 wv->contents = xmalloc_widget_value ();
|
|
412 wv->contents->type = TEXT_TYPE;
|
2
|
413 wv->contents->name = (char *) "No menu";
|
0
|
414 wv->contents->next = NULL;
|
|
415 }
|
|
416 assert (wv && wv->type == CASCADE_TYPE && wv->contents);
|
|
417 replace_widget_value_tree (hack_wv, wv->contents);
|
|
418 free_popup_widget_value_tree (wv);
|
|
419 }
|
|
420 else
|
|
421 {
|
|
422 if (!POPUP_DATAP (FRAME_MENUBAR_DATA (f)))
|
|
423 return;
|
|
424 /* #### - this menubar update mechanism is expensively anti-social and
|
|
425 the activate-menubar-hook is now mostly obsolete. */
|
|
426 /* make the activate-menubar-hook be a list of functions, not a single
|
|
427 function, just to simplify things. */
|
|
428 if (!NILP (Vactivate_menubar_hook) &&
|
|
429 (!CONSP (Vactivate_menubar_hook) ||
|
|
430 EQ (XCAR (Vactivate_menubar_hook), Qlambda)))
|
|
431 Vactivate_menubar_hook = Fcons (Vactivate_menubar_hook, Qnil);
|
185
|
432
|
0
|
433 GCPRO1 (rest);
|
|
434 for (rest = Vactivate_menubar_hook; !NILP (rest); rest = Fcdr (rest))
|
|
435 if (!EQ (call0 (XCAR (rest)), Qt))
|
|
436 any_changes = 1;
|
|
437 #if 0
|
|
438 /* #### - It is necessary to *ALWAYS* call set_frame_menubar() now that
|
|
439 incremental menus are implemented. If a subtree of a menu has been
|
|
440 updated incrementally (a destructive operation), then that subtree
|
|
441 must somehow be wiped.
|
|
442
|
|
443 It is difficult to undo the destructive operation in lwlib because
|
|
444 a pointer back to lisp data needs to be hidden away somewhere. So
|
|
445 that an INCREMENTAL_TYPE widget_value can be recreated... Hmmmmm. */
|
|
446 if (any_changes ||
|
|
447 !XFRAME_MENUBAR_DATA (f)->menubar_contents_up_to_date)
|
185
|
448 #endif
|
0
|
449 set_frame_menubar (f, 1, 0);
|
175
|
450 DEVICE_X_MOUSE_TIMESTAMP (XDEVICE (FRAME_DEVICE (f))) =
|
|
451 DEVICE_X_GLOBAL_MOUSE_TIMESTAMP (XDEVICE (FRAME_DEVICE (f))) =
|
|
452 x_focus_timestamp_really_sucks_fix_me_better;
|
0
|
453 UNGCPRO;
|
|
454 }
|
|
455 }
|
|
456
|
|
457 static widget_value *
|
|
458 compute_menubar_data (struct frame *f, Lisp_Object menubar, int deep_p)
|
|
459 {
|
|
460 widget_value *data;
|
|
461
|
|
462 if (NILP (menubar))
|
|
463 data = 0;
|
|
464 else
|
|
465 {
|
82
|
466 Lisp_Object old_buffer;
|
|
467 int count = specpdl_depth ();
|
|
468
|
|
469 old_buffer = Fcurrent_buffer ();
|
|
470 record_unwind_protect (Fset_buffer, old_buffer);
|
|
471 Fset_buffer ( XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer);
|
0
|
472 data = menu_item_descriptor_to_widget_value (menubar, MENUBAR_TYPE,
|
|
473 deep_p, 0);
|
82
|
474 Fset_buffer (old_buffer);
|
|
475 unbind_to (count, Qnil);
|
0
|
476 }
|
|
477 return data;
|
|
478 }
|
|
479
|
|
480 static int
|
|
481 set_frame_menubar (struct frame *f, int deep_p, int first_time_p)
|
|
482 {
|
|
483 widget_value *data;
|
|
484 Lisp_Object menubar;
|
|
485 int menubar_visible;
|
|
486 long id;
|
|
487 /* As for the toolbar, the minibuffer does not have its own menubar. */
|
|
488 struct window *w = XWINDOW (FRAME_LAST_NONMINIBUF_WINDOW (f));
|
|
489
|
|
490 if (! FRAME_X_P (f))
|
|
491 return 0;
|
|
492
|
|
493 /***** first compute the contents of the menubar *****/
|
|
494
|
|
495 if (! first_time_p)
|
|
496 {
|
|
497 /* evaluate `current-menubar' in the buffer of the selected window
|
|
498 of the frame in question. */
|
|
499 menubar = symbol_value_in_buffer (Qcurrent_menubar, w->buffer);
|
|
500 }
|
|
501 else
|
|
502 {
|
|
503 /* That's a little tricky the first time since the frame isn't
|
|
504 fully initialized yet. */
|
|
505 menubar = Fsymbol_value (Qcurrent_menubar);
|
|
506 }
|
|
507
|
|
508 if (NILP (menubar))
|
|
509 {
|
|
510 menubar = Vblank_menubar;
|
|
511 menubar_visible = 0;
|
|
512 }
|
|
513 else
|
|
514 menubar_visible = !NILP (w->menubar_visible_p);
|
|
515
|
|
516 data = compute_menubar_data (f, menubar, deep_p);
|
|
517 if (!data || (!data->next && !data->contents))
|
|
518 abort ();
|
185
|
519
|
0
|
520 if (NILP (FRAME_MENUBAR_DATA (f)))
|
|
521 {
|
|
522 struct popup_data *mdata =
|
185
|
523 alloc_lcrecord_type (struct popup_data, lrecord_popup_data);
|
0
|
524
|
|
525 mdata->id = new_lwlib_id ();
|
|
526 mdata->last_menubar_buffer = Qnil;
|
|
527 mdata->menubar_contents_up_to_date = 0;
|
|
528 XSETPOPUP_DATA (FRAME_MENUBAR_DATA (f), mdata);
|
|
529 }
|
|
530
|
|
531 /***** now store into the menubar widget, creating it if necessary *****/
|
|
532
|
|
533 id = XFRAME_MENUBAR_DATA (f)->id;
|
|
534 if (!FRAME_X_MENUBAR_WIDGET (f))
|
|
535 {
|
|
536 Widget parent = FRAME_X_CONTAINER_WIDGET (f);
|
|
537
|
|
538 assert (first_time_p);
|
|
539
|
|
540 /* It's the first time we've mapped the menubar so compute its
|
|
541 contents completely once. This makes sure that the menubar
|
|
542 components are created with the right type. */
|
|
543 if (!deep_p)
|
|
544 {
|
|
545 free_popup_widget_value_tree (data);
|
|
546 data = compute_menubar_data (f, menubar, 1);
|
|
547 }
|
|
548
|
|
549
|
|
550 FRAME_X_MENUBAR_WIDGET (f) =
|
|
551 lw_create_widget ("menubar", "menubar", id, data, parent,
|
|
552 0, pre_activate_callback,
|
|
553 popup_selection_callback, 0);
|
|
554
|
|
555 }
|
|
556 else
|
|
557 {
|
|
558 lw_modify_all_widgets (id, data, deep_p ? True : False);
|
|
559 }
|
|
560 free_popup_widget_value_tree (data);
|
185
|
561
|
0
|
562 XFRAME_MENUBAR_DATA (f)->menubar_contents_up_to_date = deep_p;
|
|
563 XFRAME_MENUBAR_DATA (f)->last_menubar_buffer =
|
|
564 XWINDOW (FRAME_LAST_NONMINIBUF_WINDOW (f))->buffer;
|
|
565 return menubar_visible;
|
|
566 }
|
|
567
|
|
568
|
|
569 /* Called from x_create_widgets() to create the inital menubar of a frame
|
|
570 before it is mapped, so that the window is mapped with the menubar already
|
|
571 there instead of us tacking it on later and thrashing the window after it
|
|
572 is visible. */
|
|
573 int
|
|
574 x_initialize_frame_menubar (struct frame *f)
|
|
575 {
|
|
576 return set_frame_menubar (f, 1, 1);
|
|
577 }
|
|
578
|
|
579
|
|
580 static LWLIB_ID last_popup_menu_selection_callback_id;
|
|
581
|
|
582 static void
|
|
583 popup_menu_selection_callback (Widget widget, LWLIB_ID id,
|
|
584 XtPointer client_data)
|
|
585 {
|
|
586 last_popup_menu_selection_callback_id = id;
|
|
587 popup_selection_callback (widget, id, client_data);
|
|
588 /* lw_destroy_all_widgets() will be called from popup_down_callback() */
|
|
589 }
|
|
590
|
|
591 static void
|
|
592 popup_menu_down_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
|
|
593 {
|
|
594 if (popup_handled_p (id))
|
|
595 return;
|
|
596 assert (popup_up_p != 0);
|
|
597 ungcpro_popup_callbacks (id);
|
|
598 popup_up_p--;
|
|
599 /* if this isn't called immediately after the selection callback, then
|
|
600 there wasn't a menu selection. */
|
|
601 if (id != last_popup_menu_selection_callback_id)
|
|
602 popup_selection_callback (widget, id, (XtPointer) -1);
|
|
603 lw_destroy_all_widgets (id);
|
|
604 }
|
|
605
|
|
606
|
|
607 static void
|
|
608 make_dummy_xbutton_event (XEvent *dummy,
|
185
|
609 Widget daddy,
|
0
|
610 struct Lisp_Event *eev)
|
|
611 /* NULL for eev means query pointer */
|
|
612 {
|
|
613 XButtonPressedEvent *btn = (XButtonPressedEvent *) dummy;
|
|
614
|
|
615 btn->type = ButtonPress;
|
|
616 btn->serial = 0;
|
|
617 btn->send_event = 0;
|
|
618 btn->display = XtDisplay (daddy);
|
|
619 btn->window = XtWindow (daddy);
|
|
620 if (eev)
|
|
621 {
|
|
622 Position shellx, shelly, framex, framey;
|
|
623 Widget shell = XtParent (daddy);
|
165
|
624 Arg al [2];
|
0
|
625 btn->time = eev->timestamp;
|
|
626 btn->button = eev->event.button.button;
|
|
627 btn->root = RootWindowOfScreen (XtScreen (daddy));
|
|
628 btn->subwindow = (Window) NULL;
|
|
629 btn->x = eev->event.button.x;
|
|
630 btn->y = eev->event.button.y;
|
165
|
631 XtSetArg (al [0], XtNx, &shellx);
|
|
632 XtSetArg (al [1], XtNy, &shelly);
|
|
633 XtGetValues (shell, al, 2);
|
|
634 XtSetArg (al [0], XtNx, &framex);
|
|
635 XtSetArg (al [1], XtNy, &framey);
|
|
636 XtGetValues (daddy, al, 2);
|
0
|
637 btn->x_root = shellx + framex + btn->x;
|
|
638 btn->y_root = shelly + framey + btn->y;;
|
|
639 btn->state = ButtonPressMask; /* all buttons pressed */
|
|
640 }
|
|
641 else
|
|
642 {
|
|
643 /* CurrentTime is just ZERO, so it's worthless for
|
|
644 determining relative click times. */
|
|
645 struct device *d = get_device_from_display (XtDisplay (daddy));
|
|
646 btn->time = DEVICE_X_MOUSE_TIMESTAMP (d); /* event-Xt maintains this */
|
|
647 btn->button = 0;
|
|
648 XQueryPointer (btn->display, btn->window, &btn->root,
|
|
649 &btn->subwindow, &btn->x_root, &btn->y_root,
|
|
650 &btn->x, &btn->y, &btn->state);
|
|
651 }
|
|
652 }
|
|
653
|
|
654
|
|
655
|
|
656 static void
|
|
657 x_update_frame_menubar_internal (struct frame *f)
|
|
658 {
|
|
659 /* We assume the menubar contents has changed if the global flag is set,
|
|
660 or if the current buffer has changed, or if the menubar has never
|
|
661 been updated before.
|
|
662 */
|
|
663 int menubar_contents_changed =
|
|
664 (f->menubar_changed
|
|
665 || NILP (FRAME_MENUBAR_DATA (f))
|
|
666 || (!EQ (XFRAME_MENUBAR_DATA (f)->last_menubar_buffer,
|
|
667 XWINDOW (FRAME_LAST_NONMINIBUF_WINDOW (f))->buffer)));
|
|
668
|
102
|
669 Boolean menubar_was_visible = XtIsManaged (FRAME_X_MENUBAR_WIDGET (f));
|
|
670 Boolean menubar_will_be_visible = menubar_was_visible;
|
|
671 Boolean menubar_visibility_changed;
|
0
|
672 Cardinal new_num_top_widgets = 1; /* for the menubar */
|
|
673 Widget container = FRAME_X_CONTAINER_WIDGET (f);
|
185
|
674
|
0
|
675 if (menubar_contents_changed)
|
|
676 menubar_will_be_visible = set_frame_menubar (f, 0, 0);
|
|
677
|
|
678 menubar_visibility_changed = menubar_was_visible != menubar_will_be_visible;
|
|
679
|
|
680 if (! (menubar_visibility_changed
|
|
681 ))
|
|
682 return;
|
|
683
|
|
684
|
|
685 /* Set menubar visibility */
|
|
686 if (menubar_visibility_changed)
|
|
687 (menubar_will_be_visible ? XtManageChild : XtUnmanageChild)
|
|
688 (FRAME_X_MENUBAR_WIDGET (f));
|
185
|
689
|
0
|
690 /* Note that new_num_top_widgets doesn't need to reflect the actual
|
|
691 number of top widgets, but just the limit of FRAME_X_TOP_WIDGETS (f)[]. */
|
|
692 FRAME_X_NUM_TOP_WIDGETS (f) = new_num_top_widgets;
|
|
693 {
|
|
694 /* We want to end up as close in size as possible to what we
|
165
|
695 were before. So, ask the EmacsManager what size it wants to be
|
|
696 (suggesting the current size), and resize it to that size. It
|
|
697 in turn will call our query-geometry callback, which will round
|
|
698 the size to something that exactly fits the text widget. */
|
0
|
699 XtWidgetGeometry req, repl;
|
165
|
700 Arg al [2];
|
0
|
701
|
|
702 req.request_mode = CWWidth | CWHeight;
|
165
|
703 XtSetArg (al [0], XtNwidth, &req.width);
|
|
704 XtSetArg (al [1], XtNheight, &req.height);
|
|
705 XtGetValues (container, al, 2);
|
0
|
706 XtQueryGeometry (container, &req, &repl);
|
165
|
707 EmacsManagerChangeSize (container, repl.width, repl.height);
|
0
|
708 /* The window size might not have changed but the text size
|
165
|
709 did; thus, the base size might be incorrect. So update it. */
|
0
|
710 EmacsShellUpdateSizeHints (FRAME_X_SHELL_WIDGET (f));
|
|
711 }
|
|
712
|
|
713 }
|
|
714
|
|
715 static void
|
|
716 x_update_frame_menubars (struct frame *f)
|
|
717 {
|
|
718 assert (FRAME_X_P (f));
|
|
719
|
|
720 x_update_frame_menubar_internal (f);
|
|
721
|
|
722 /* #### This isn't going to work right now that this function works on
|
|
723 a per-frame, not per-device basis. Guess what? I don't care. */
|
|
724 }
|
|
725
|
|
726 static void
|
|
727 x_free_frame_menubars (struct frame *f)
|
|
728 {
|
|
729 Widget menubar_widget;
|
|
730
|
|
731 assert (FRAME_X_P (f));
|
185
|
732
|
0
|
733 menubar_widget = FRAME_X_MENUBAR_WIDGET (f);
|
|
734 if (menubar_widget)
|
|
735 {
|
|
736 LWLIB_ID id = XFRAME_MENUBAR_DATA (f)->id;
|
|
737 lw_destroy_all_widgets (id);
|
|
738 XFRAME_MENUBAR_DATA (f)->id = 0;
|
|
739 }
|
|
740 }
|
|
741
|
|
742 static void
|
|
743 x_popup_menu (Lisp_Object menu_desc, Lisp_Object event)
|
|
744 {
|
|
745 int menu_id;
|
|
746 struct frame *f = selected_frame ();
|
|
747 widget_value *data;
|
|
748 Widget parent;
|
|
749 Widget menu;
|
|
750 struct Lisp_Event *eev = NULL;
|
|
751 XEvent xev;
|
|
752 Lisp_Object frame = Qnil;
|
|
753
|
|
754 XSETFRAME (frame, f);
|
|
755 CHECK_X_FRAME (frame);
|
|
756 parent = FRAME_X_SHELL_WIDGET (f);
|
|
757
|
|
758 if (!NILP (event))
|
|
759 {
|
|
760 CHECK_LIVE_EVENT (event);
|
|
761 eev= XEVENT (event);
|
|
762 if (eev->event_type != button_press_event
|
|
763 && eev->event_type != button_release_event)
|
|
764 wrong_type_argument (Qmouse_event_p, event);
|
|
765 }
|
|
766 else if (!NILP (Vthis_command_keys))
|
|
767 {
|
|
768 /* if an event wasn't passed, use the last event of the event sequence
|
|
769 currently being executed, if that event is a mouse event */
|
|
770 eev = XEVENT (Vthis_command_keys); /* last event first */
|
|
771 if (eev->event_type != button_press_event
|
|
772 && eev->event_type != button_release_event)
|
|
773 eev = NULL;
|
|
774 }
|
|
775 make_dummy_xbutton_event (&xev, parent, eev);
|
|
776
|
|
777 if (SYMBOLP (menu_desc))
|
|
778 menu_desc = Fsymbol_value (menu_desc);
|
|
779 CHECK_CONS (menu_desc);
|
|
780 CHECK_STRING (XCAR (menu_desc));
|
|
781 data = menu_item_descriptor_to_widget_value (menu_desc, POPUP_TYPE, 1, 1);
|
|
782
|
|
783 if (! data) error ("no menu");
|
185
|
784
|
0
|
785 menu_id = new_lwlib_id ();
|
|
786 menu = lw_create_widget ("popup", "popup" /* data->name */, menu_id, data,
|
|
787 parent, 1, 0,
|
|
788 popup_menu_selection_callback,
|
|
789 popup_menu_down_callback);
|
|
790 free_popup_widget_value_tree (data);
|
|
791
|
|
792 gcpro_popup_callbacks (menu_id);
|
|
793
|
|
794 /* Setting zmacs-region-stays is necessary here because executing a command
|
|
795 from a menu is really a two-command process: the first command (bound to
|
|
796 the button-click) simply pops up the menu, and returns. This causes a
|
|
797 sequence of magic-events (destined for the popup-menu widget) to begin.
|
|
798 Eventually, a menu item is selected, and a menu-event blip is pushed onto
|
|
799 the end of the input stream, which is then executed by the event loop.
|
185
|
800
|
0
|
801 So there are two command-events, with a bunch of magic-events between
|
|
802 them. We don't want the *first* command event to alter the state of the
|
|
803 region, so that the region can be available as an argument for the second
|
|
804 command.
|
|
805 */
|
|
806 if (zmacs_regions)
|
|
807 zmacs_region_stays = 1;
|
|
808
|
|
809 popup_up_p++;
|
|
810 lw_popup_menu (menu, &xev);
|
|
811 /* this speeds up display of pop-up menus */
|
|
812 XFlush (XtDisplay (parent));
|
|
813 }
|
|
814
|
|
815
|
|
816 void
|
|
817 syms_of_menubar_x (void)
|
|
818 {
|
|
819 }
|
|
820
|
|
821 void
|
|
822 console_type_create_menubar_x (void)
|
|
823 {
|
|
824 CONSOLE_HAS_METHOD (x, update_frame_menubars);
|
|
825 CONSOLE_HAS_METHOD (x, free_frame_menubars);
|
|
826 CONSOLE_HAS_METHOD (x, popup_menu);
|
|
827 }
|
|
828
|
|
829 void
|
|
830 vars_of_menubar_x (void)
|
|
831 {
|
|
832 last_popup_menu_selection_callback_id = -1;
|
|
833
|
|
834 #if defined (LWLIB_MENUBARS_LUCID)
|
|
835 Fprovide (intern ("lucid-menubars"));
|
|
836 #elif defined (LWLIB_MENUBARS_MOTIF)
|
|
837 Fprovide (intern ("motif-menubars"));
|
|
838 #elif defined (LWLIB_MENUBARS_ATHENA)
|
|
839 Fprovide (intern ("athena-menubars"));
|
|
840 #endif
|
|
841 }
|