1 /* General GUI code -- X-specific. (menubars, scrollbars, toolbars, dialogs)
2 Copyright (C) 1995 Board of Trustees, University of Illinois.
3 Copyright (C) 1995, 1996 Ben Wing.
4 Copyright (C) 1995 Sun Microsystems, Inc.
5 Copyright (C) 1998 Free Software Foundation, Inc.
7 This file is part of XEmacs.
9 XEmacs is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 2, or (at your option) any
14 XEmacs is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with XEmacs; see the file COPYING. If not, write to
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 /* Synched up with: Not in FSF. */
29 #include "console-x.h"
30 #ifdef LWLIB_USES_MOTIF
31 #include <Xm/Xm.h> /* for XmVersion */
38 #include "redisplay.h"
41 Lisp_Object Qmenu_no_selection_hook;
43 /* we need a unique id for each popup menu, dialog box, and scrollbar */
44 static unsigned int lwlib_id_tick;
49 return ++lwlib_id_tick;
53 xmalloc_widget_value (void)
55 widget_value *tmp = malloc_widget_value ();
56 if (!tmp) memory_full ();
62 mark_widget_value_mapper (widget_value *val, void *closure)
67 VOID_TO_LISP (markee, val->call_data);
73 VOID_TO_LISP (markee, val->accel);
80 mark_popup_data (Lisp_Object obj)
82 struct popup_data *data = (struct popup_data *) XPOPUP_DATA (obj);
84 /* Now mark the callbacks and such that are hidden in the lwlib
88 lw_map_widget_values (data->id, mark_widget_value_mapper, 0);
90 return data->last_menubar_buffer;
93 DEFINE_LRECORD_IMPLEMENTATION ("popup-data", popup_data,
94 mark_popup_data, internal_object_printer,
95 0, 0, 0, 0, struct popup_data);
97 /* This is like FRAME_MENUBAR_DATA (f), but contains an alist of
98 (id . popup-data) for GCPRO'ing the callbacks of the popup menus
100 static Lisp_Object Vpopup_callbacks;
103 gcpro_popup_callbacks (LWLIB_ID id)
105 struct popup_data *pdata;
106 Lisp_Object lid = make_int (id);
109 assert (NILP (assq_no_quit (lid, Vpopup_callbacks)));
110 pdata = alloc_lcrecord_type (struct popup_data, &lrecord_popup_data);
112 pdata->last_menubar_buffer = Qnil;
113 pdata->menubar_contents_up_to_date = 0;
114 XSETPOPUP_DATA (lpdata, pdata);
115 Vpopup_callbacks = Fcons (Fcons (lid, lpdata), Vpopup_callbacks);
119 ungcpro_popup_callbacks (LWLIB_ID id)
121 Lisp_Object lid = make_int (id);
122 Lisp_Object this = assq_no_quit (lid, Vpopup_callbacks);
123 assert (!NILP (this));
124 Vpopup_callbacks = delq_no_quit (this, Vpopup_callbacks);
128 popup_handled_p (LWLIB_ID id)
130 return NILP (assq_no_quit (make_int (id), Vpopup_callbacks));
133 /* menu_item_descriptor_to_widget_value() et al. mallocs a
134 widget_value, but then may signal lisp errors. If an error does
135 not occur, the opaque ptr we have here has had its pointer set to 0
136 to tell us not to do anything. Otherwise we free the widget value.
137 (This has nothing to do with GC, it's just about not dropping
138 pointers to malloc'd data when errors happen.) */
141 widget_value_unwind (Lisp_Object closure)
143 widget_value *wv = (widget_value *) get_opaque_ptr (closure);
144 free_opaque_ptr (closure);
146 free_widget_value_tree (wv);
152 print_widget_value (widget_value *wv, int depth)
154 /* !!#### This function has not been Mule-ized */
157 for (i = 0; i < depth; i++) d[i] = ' ';
159 /* #### - print type field */
160 printf ("%sname: %s\n", d, (wv->name ? wv->name : "(null)"));
161 if (wv->value) printf ("%svalue: %s\n", d, wv->value);
162 if (wv->key) printf ("%skey: %s\n", d, wv->key);
163 printf ("%senabled: %d\n", d, wv->enabled);
166 printf ("\n%scontents: \n", d);
167 print_widget_value (wv->contents, depth + 5);
172 print_widget_value (wv->next, depth);
177 /* This recursively calls free_widget_value() on the tree of widgets.
178 It must free all data that was malloc'ed for these widget_values.
180 It used to be that emacs only allocated new storage for the `key' slot.
181 All other slots are pointers into the data of Lisp_Strings, and must be
184 free_popup_widget_value_tree (widget_value *wv)
187 if (wv->key) xfree (wv->key);
188 if (wv->value) xfree (wv->value);
189 if (wv->name) xfree (wv->name);
191 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
193 if (wv->contents && (wv->contents != (widget_value*)1))
195 free_popup_widget_value_tree (wv->contents);
196 wv->contents = (widget_value *) 0xDEADBEEF;
200 free_popup_widget_value_tree (wv->next);
201 wv->next = (widget_value *) 0xDEADBEEF;
203 free_widget_value (wv);
206 /* The following is actually called from somewhere within XtDispatchEvent(),
207 called from XtAppProcessEvent() in event-Xt.c */
210 popup_selection_callback (Widget widget, LWLIB_ID ignored_id,
211 XtPointer client_data)
216 struct device *d = get_device_from_display (XtDisplay (widget));
217 struct frame *f = x_any_widget_or_parent_to_frame (d, widget);
219 /* set in lwlib to the time stamp associated with the most recent menu
221 extern Time x_focus_timestamp_really_sucks_fix_me_better;
225 if (((EMACS_INT) client_data) == 0)
227 VOID_TO_LISP (data, client_data);
228 XSETFRAME (frame, f);
231 /* #### What the hell? I can't understand why this call is here,
232 and doing it is really courting disaster in the new event
233 model, since popup_selection_callback is called from
234 within next_event_internal() and Faccept_process_output()
235 itself calls next_event_internal(). --Ben */
237 /* Flush the X and process input */
238 Faccept_process_output (Qnil, Qnil, Qnil);
241 if (((EMACS_INT) client_data) == -1)
244 arg = Qmenu_no_selection_hook;
248 MARK_SUBWINDOWS_STATE_CHANGED;
249 get_gui_callback (data, &fn, &arg);
252 /* This is the timestamp used for asserting focus so we need to get an
253 up-to-date value event if no events has been dispatched to emacs
255 #if defined(HAVE_MENUBARS)
256 DEVICE_X_MOUSE_TIMESTAMP (d) = x_focus_timestamp_really_sucks_fix_me_better;
258 DEVICE_X_MOUSE_TIMESTAMP (d) = DEVICE_X_GLOBAL_MOUSE_TIMESTAMP (d);
260 signal_special_Xt_user_event (frame, fn, arg);
264 /* Eval the activep slot of the menu item */
265 # define wv_set_evalable_slot(slot,form) do { \
266 Lisp_Object wses_form = (form); \
267 (slot) = (NILP (wses_form) ? 0 : \
268 EQ (wses_form, Qt) ? 1 : \
269 !NILP (Feval (wses_form))); \
272 /* Treat the activep slot of the menu item as a boolean */
273 # define wv_set_evalable_slot(slot,form) \
274 ((void) (slot = (!NILP (form))))
278 menu_separator_style (const char *s)
283 if (!s || s[0] == '\0')
286 if (first != '-' && first != '=')
288 for (p = s; *p == first; p++)
291 /* #### - cannot currently specify a separator tag "--!tag" and a
292 separator style "--:style" at the same time. */
293 /* #### - Also, the motif menubar code doesn't deal with the
294 double etched style yet, so it's not good to get into the habit of
295 using "===" in menubars to get double-etched lines */
296 if (*p == '!' || *p == '\0')
297 return ((first == '-')
298 ? NULL /* single etched is the default */
299 : xstrdup ("shadowDoubleEtchedIn"));
301 return xstrdup (p+1);
307 /* This does the dirty work. gc_currently_forbidden is 1 when this is called.
310 button_item_to_widget_value (Lisp_Object gui_item, widget_value *wv,
311 int allow_text_field_p, int no_keys_p)
313 /* !!#### This function has not been Mule-ized */
314 /* This function cannot GC because gc_currently_forbidden is set when
316 Lisp_Gui_Item* pgui = 0;
318 /* degenerate case */
319 if (STRINGP (gui_item))
321 wv->type = TEXT_TYPE;
322 wv->name = (char *) XSTRING_DATA (gui_item);
323 wv->name = xstrdup (wv->name);
326 else if (!GUI_ITEMP (gui_item))
327 signal_simple_error("need a string or a gui_item here", gui_item);
329 pgui = XGUI_ITEM (gui_item);
331 if (!NILP (pgui->filter))
332 signal_simple_error(":filter keyword not permitted on leaf nodes", gui_item);
335 if (!gui_item_included_p (gui_item, Vmenubar_configuration))
337 /* the include specification says to ignore this item. */
340 #endif /* HAVE_MENUBARS */
342 CHECK_STRING (pgui->name);
343 wv->name = (char *) XSTRING_DATA (pgui->name);
344 wv->name = xstrdup (wv->name);
345 wv->accel = LISP_TO_VOID (gui_item_accelerator (gui_item));
347 if (!NILP (pgui->suffix))
349 const char *const_bogosity;
352 /* Shortcut to avoid evaluating suffix each time */
353 if (STRINGP (pgui->suffix))
354 suffix2 = pgui->suffix;
357 suffix2 = Feval (pgui->suffix);
358 CHECK_STRING (suffix2);
361 TO_EXTERNAL_FORMAT (LISP_STRING, suffix2,
362 C_STRING_ALLOCA, const_bogosity,
364 wv->value = (char *) const_bogosity;
365 wv->value = xstrdup (wv->value);
368 wv_set_evalable_slot (wv->enabled, pgui->active);
369 wv_set_evalable_slot (wv->selected, pgui->selected);
371 if (!NILP (pgui->callback))
372 wv->call_data = LISP_TO_VOID (pgui->callback);
376 || !menubar_show_keybindings
380 else if (!NILP (pgui->keys)) /* Use this string to generate key bindings */
382 CHECK_STRING (pgui->keys);
383 pgui->keys = Fsubstitute_command_keys (pgui->keys);
384 if (XSTRING_LENGTH (pgui->keys) > 0)
385 wv->key = xstrdup ((char *) XSTRING_DATA (pgui->keys));
389 else if (SYMBOLP (pgui->callback)) /* Show the binding of this command. */
392 /* #### Warning, dependency here on current_buffer and point */
393 where_is_to_char (pgui->callback, buf);
395 wv->key = xstrdup (buf);
400 CHECK_SYMBOL (pgui->style);
401 if (NILP (pgui->style))
403 /* If the callback is nil, treat this item like unselectable text.
404 This way, dashes will show up as a separator. */
406 wv->type = BUTTON_TYPE;
407 if (separator_string_p (wv->name))
409 wv->type = SEPARATOR_TYPE;
410 wv->value = menu_separator_style (wv->name);
415 /* #### - this is generally desirable for menubars, but it breaks
416 a package that uses dialog boxes and next_command_event magic
417 to use the callback slot in dialog buttons for data instead of
420 Code is data, right? The beauty of LISP abuse. --Stig */
422 wv->type = TEXT_TYPE;
425 wv->type = BUTTON_TYPE;
428 else if (EQ (pgui->style, Qbutton))
429 wv->type = BUTTON_TYPE;
430 else if (EQ (pgui->style, Qtoggle))
431 wv->type = TOGGLE_TYPE;
432 else if (EQ (pgui->style, Qradio))
433 wv->type = RADIO_TYPE;
434 else if (EQ (pgui->style, Qtext))
436 wv->type = TEXT_TYPE;
438 wv->value = wv->name;
443 signal_simple_error_2 ("Unknown style", pgui->style, gui_item);
445 if (!allow_text_field_p && (wv->type == TEXT_TYPE))
446 signal_simple_error ("Text field not allowed in this context", gui_item);
448 if (!NILP (pgui->selected) && EQ (pgui->style, Qtext))
449 signal_simple_error (
450 ":selected only makes sense with :style toggle, radio or button",
455 /* parse tree's of gui items into widget_value hierarchies */
456 static void gui_item_children_to_widget_values (Lisp_Object items, widget_value* parent);
458 static widget_value *
459 gui_items_to_widget_values_1 (Lisp_Object items, widget_value* parent,
462 widget_value* wv = 0;
464 assert ((parent || prev) && !(parent && prev));
465 /* now walk the tree creating widget_values as appropriate */
468 wv = xmalloc_widget_value();
470 parent->contents = wv;
473 if (!button_item_to_widget_value (items, wv, 0, 1))
475 free_widget_value_tree (wv);
477 parent->contents = 0;
483 wv->value = xstrdup (wv->name); /* what a mess... */
488 /* first one is the parent */
489 if (CONSP (XCAR (items)))
490 signal_simple_error ("parent item must not be a list", XCAR (items));
493 wv = gui_items_to_widget_values_1 (XCAR (items), parent, 0);
495 wv = gui_items_to_widget_values_1 (XCAR (items), 0, prev);
496 /* the rest are the children */
497 gui_item_children_to_widget_values (XCDR (items), wv);
503 gui_item_children_to_widget_values (Lisp_Object items, widget_value* parent)
505 widget_value* wv = 0, *prev = 0;
509 /* first one is master */
510 prev = gui_items_to_widget_values_1 (XCAR (items), parent, 0);
511 /* the rest are the children */
512 LIST_LOOP (rest, XCDR (items))
514 Lisp_Object tab = XCAR (rest);
515 wv = gui_items_to_widget_values_1 (tab, 0, prev);
521 gui_items_to_widget_values (Lisp_Object items)
523 /* !!#### This function has not been Mule-ized */
524 /* This function can GC */
525 widget_value *control = 0, *tmp = 0;
526 int count = specpdl_depth ();
527 Lisp_Object wv_closure;
530 signal_simple_error ("must have some items", items);
532 /* Inhibit GC during this conversion. The reasons for this are
533 the same as in menu_item_descriptor_to_widget_value(); see
534 the large comment above that function. */
535 record_unwind_protect (restore_gc_inhibit,
536 make_int (gc_currently_forbidden));
537 gc_currently_forbidden = 1;
539 /* Also make sure that we free the partially-created widget_value
540 tree on Lisp error. */
541 control = xmalloc_widget_value();
542 wv_closure = make_opaque_ptr (control);
543 record_unwind_protect (widget_value_unwind, wv_closure);
545 gui_items_to_widget_values_1 (items, control, 0);
547 /* mess about getting the data we really want */
549 control = control->contents;
552 free_widget_value_tree (tmp);
554 /* No more need to free the half-filled-in structures. */
555 set_opaque_ptr (wv_closure, 0);
556 unbind_to (count, Qnil);
561 /* This is a kludge to make sure emacs can only link against a version of
562 lwlib that was compiled in the right way. Emacs references symbols which
563 correspond to the way it thinks lwlib was compiled, and if lwlib wasn't
564 compiled in that way, then somewhat meaningful link errors will result.
565 The alternatives to this range from obscure link errors, to obscure
566 runtime errors that look a lot like bugs.
570 sanity_check_lwlib (void)
572 #define MACROLET(v) { extern int v; v = 1; }
574 #if (XlibSpecificationRelease == 4)
575 MACROLET (lwlib_uses_x11r4);
576 #elif (XlibSpecificationRelease == 5)
577 MACROLET (lwlib_uses_x11r5);
578 #elif (XlibSpecificationRelease == 6)
579 MACROLET (lwlib_uses_x11r6);
581 MACROLET (lwlib_uses_unknown_x11);
583 #ifdef LWLIB_USES_MOTIF
584 MACROLET (lwlib_uses_motif);
586 MACROLET (lwlib_does_not_use_motif);
588 #if (XmVersion >= 1002)
589 MACROLET (lwlib_uses_motif_1_2);
591 MACROLET (lwlib_does_not_use_motif_1_2);
593 #ifdef LWLIB_MENUBARS_LUCID
594 MACROLET (lwlib_menubars_lucid);
595 #elif defined (HAVE_MENUBARS)
596 MACROLET (lwlib_menubars_motif);
598 #ifdef LWLIB_SCROLLBARS_LUCID
599 MACROLET (lwlib_scrollbars_lucid);
600 #elif defined (LWLIB_SCROLLBARS_MOTIF)
601 MACROLET (lwlib_scrollbars_motif);
602 #elif defined (HAVE_SCROLLBARS)
603 MACROLET (lwlib_scrollbars_athena);
605 #ifdef LWLIB_DIALOGS_MOTIF
606 MACROLET (lwlib_dialogs_motif);
607 #elif defined (HAVE_DIALOGS)
608 MACROLET (lwlib_dialogs_athena);
610 #ifdef LWLIB_WIDGETS_MOTIF
611 MACROLET (lwlib_widgets_motif);
612 #elif defined (HAVE_WIDGETS)
613 MACROLET (lwlib_widgets_athena);
622 INIT_LRECORD_IMPLEMENTATION (popup_data);
624 defsymbol (&Qmenu_no_selection_hook, "menu-no-selection-hook");
628 reinit_vars_of_gui_x (void)
630 lwlib_id_tick = (1<<16); /* start big, to not conflict with Energize */
635 /* this makes only safe calls as in emacs.c */
636 sanity_check_lwlib ();
642 reinit_vars_of_gui_x ();
644 Vpopup_callbacks = Qnil;
645 staticpro (&Vpopup_callbacks);
648 /* This DEFVAR_LISP is just for the benefit of make-docfile. */
650 DEFVAR_LISP ("menu-no-selection-hook", &Vmenu_no_selection_hook /*
651 Function or functions to call when a menu or dialog box is dismissed
652 without a selection having been made.
655 Fset (Qmenu_no_selection_hook, Qnil);