1 /* Generic GUI code. (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. */
26 /* This file not quite Mule-ized yet but will be when merged with my
27 Mule workspace. --ben */
36 Lisp_Object Qmenu_no_selection_hook;
37 Lisp_Object Vmenu_no_selection_hook;
39 static Lisp_Object parse_gui_item_tree_list (Lisp_Object list);
40 Lisp_Object find_keyword_in_vector (Lisp_Object vector, Lisp_Object keyword);
44 /* count of menus/dboxes currently up */
47 DEFUN ("popup-up-p", Fpopup_up_p, 0, 0, 0, /*
48 Return t if a popup menu or dialog box is up, nil otherwise.
49 See `popup-menu' and `popup-dialog-box'.
53 return popup_up_p ? Qt : Qnil;
55 #endif /* HAVE_POPUPS */
58 separator_string_p (const Bufbyte *s)
63 if (!s || s[0] == '\0')
66 if (first != '-' && first != '=')
68 for (p = s; *p == first; p++)
71 return (*p == '!' || *p == ':' || *p == '\0');
74 /* Massage DATA to find the correct function and argument. Used by
75 popup_selection_callback() and the msw code. */
77 get_gui_callback (Lisp_Object data, Lisp_Object *fn, Lisp_Object *arg)
82 *arg = list3 (Qsignal, list2 (Qquote, Qquit), Qnil);
85 else if (SYMBOLP (data)
86 || (COMPILED_FUNCTIONP (data)
87 && XCOMPILED_FUNCTION (data)->flags.interactivep)
88 || (CONSP (data) && (EQ (XCAR (data), Qlambda))
89 && !NILP (Fassq (Qinteractive, Fcdr (Fcdr (data))))))
91 *fn = Qcall_interactively;
94 else if (CONSP (data))
102 *arg = list3 (Qsignal,
103 list2 (Qquote, Qerror),
104 list2 (Qquote, list2 (build_translated_string
105 ("illegal callback"),
111 * Add a value VAL associated with keyword KEY into PGUI_ITEM
112 * structure. If KEY is not a keyword, or is an unknown keyword, then
116 gui_item_add_keyval_pair (Lisp_Object gui_item,
117 Lisp_Object key, Lisp_Object val,
120 Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);
124 syntax_error_2 ("Non-keyword in gui item", key, pgui_item->name);
126 if (EQ (key, Q_descriptor))
128 if (!EQ (pgui_item->name, val))
131 pgui_item->name = val;
135 else if (EQ (key, Q_##slot)) \
137 if (!EQ (pgui_item->slot, val)) \
140 pgui_item->slot = val; \
155 else if (EQ (key, Q_key_sequence)) ; /* ignored for FSF compatibility */
156 else if (EQ (key, Q_label)) ; /* ignored for 21.0 implement in 21.2 */
157 else if (EQ (key, Q_accelerator))
159 if (!EQ (pgui_item->accelerator, val))
162 if (SYMBOLP (val) || CHARP (val))
163 pgui_item->accelerator = val;
164 else if (ERRB_EQ (errb, ERROR_ME))
165 syntax_error ("Bad keyboard accelerator", val);
168 else if (ERRB_EQ (errb, ERROR_ME))
169 syntax_error_2 ("Unknown keyword in gui item", key,
175 gui_item_init (Lisp_Object gui_item)
177 Lisp_Gui_Item *lp = XGUI_ITEM (gui_item);
181 lp->callback_ex = Qnil;
190 lp->accelerator = Qnil;
195 allocate_gui_item (void)
197 Lisp_Gui_Item *lp = alloc_lcrecord_type (Lisp_Gui_Item, &lrecord_gui_item);
201 XSETGUI_ITEM (val, lp);
209 * ITEM is a lisp vector, describing a menu item or a button. The
210 * function extracts the description of the item into the PGUI_ITEM
214 make_gui_item_from_keywords_internal (Lisp_Object item,
217 int length, plist_p, start;
218 Lisp_Object *contents;
219 Lisp_Object gui_item = allocate_gui_item ();
220 Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);
223 length = XVECTOR_LENGTH (item);
224 contents = XVECTOR_DATA (item);
227 syntax_error ("GUI item descriptors must be at least 1 elts long", item);
229 /* length 1: [ "name" ]
230 length 2: [ "name" callback ]
231 length 3: [ "name" callback active-p ]
232 or [ "name" keyword value ]
233 length 4: [ "name" callback active-p suffix ]
234 or [ "name" callback keyword value ]
235 length 5+: [ "name" callback [ keyword value ]+ ]
236 or [ "name" [ keyword value ]+ ]
238 plist_p = (length > 2 && (KEYWORDP (contents [1])
239 || KEYWORDP (contents [2])));
241 pgui_item->name = contents [0];
242 if (length > 1 && !KEYWORDP (contents [1]))
244 pgui_item->callback = contents [1];
250 if (!plist_p && length > 2)
253 pgui_item->active = contents [2];
255 pgui_item->suffix = contents [3];
261 if ((length - start) & 1)
263 "GUI item descriptor has an odd number of keywords and values",
266 for (i = start; i < length;)
268 Lisp_Object key = contents [i++];
269 Lisp_Object val = contents [i++];
270 gui_item_add_keyval_pair (gui_item, key, val, errb);
276 /* This will only work with descriptors in the new format. */
278 widget_gui_parse_item_keywords (Lisp_Object item)
281 Lisp_Object *contents;
282 Lisp_Object gui_item = allocate_gui_item ();
283 Lisp_Object desc = find_keyword_in_vector (item, Q_descriptor);
286 length = XVECTOR_LENGTH (item);
287 contents = XVECTOR_DATA (item);
289 if (!NILP (desc) && !STRINGP (desc) && !VECTORP (desc))
290 syntax_error ("Invalid GUI item descriptor", item);
294 if (!SYMBOLP (contents [0]))
295 syntax_error ("Invalid GUI item descriptor", item);
296 contents++; /* Ignore the leading symbol. */
300 for (i = 0; i < length;)
302 Lisp_Object key = contents [i++];
303 Lisp_Object val = contents [i++];
304 gui_item_add_keyval_pair (gui_item, key, val, ERROR_ME_NOT);
310 /* Update a gui item from a partial descriptor. */
312 update_gui_item_keywords (Lisp_Object gui_item, Lisp_Object item)
314 int i, length, retval = 0;
315 Lisp_Object *contents;
318 length = XVECTOR_LENGTH (item);
319 contents = XVECTOR_DATA (item);
323 if (!SYMBOLP (contents [0]))
324 syntax_error ("Invalid GUI item descriptor", item);
325 contents++; /* Ignore the leading symbol. */
329 for (i = 0; i < length;)
331 Lisp_Object key = contents [i++];
332 Lisp_Object val = contents [i++];
333 if (gui_item_add_keyval_pair (gui_item, key, val, ERROR_ME_NOT))
340 gui_parse_item_keywords (Lisp_Object item)
342 return make_gui_item_from_keywords_internal (item, ERROR_ME);
346 gui_parse_item_keywords_no_errors (Lisp_Object item)
348 return make_gui_item_from_keywords_internal (item, ERROR_ME_NOT);
351 /* convert a gui item into plist properties */
353 gui_add_item_keywords_to_plist (Lisp_Object plist, Lisp_Object gui_item)
355 Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);
357 if (!NILP (pgui_item->callback))
358 Fplist_put (plist, Q_callback, pgui_item->callback);
359 if (!NILP (pgui_item->callback_ex))
360 Fplist_put (plist, Q_callback_ex, pgui_item->callback_ex);
361 if (!NILP (pgui_item->suffix))
362 Fplist_put (plist, Q_suffix, pgui_item->suffix);
363 if (!NILP (pgui_item->active))
364 Fplist_put (plist, Q_active, pgui_item->active);
365 if (!NILP (pgui_item->included))
366 Fplist_put (plist, Q_included, pgui_item->included);
367 if (!NILP (pgui_item->config))
368 Fplist_put (plist, Q_config, pgui_item->config);
369 if (!NILP (pgui_item->filter))
370 Fplist_put (plist, Q_filter, pgui_item->filter);
371 if (!NILP (pgui_item->style))
372 Fplist_put (plist, Q_style, pgui_item->style);
373 if (!NILP (pgui_item->selected))
374 Fplist_put (plist, Q_selected, pgui_item->selected);
375 if (!NILP (pgui_item->keys))
376 Fplist_put (plist, Q_keys, pgui_item->keys);
377 if (!NILP (pgui_item->accelerator))
378 Fplist_put (plist, Q_accelerator, pgui_item->accelerator);
379 if (!NILP (pgui_item->value))
380 Fplist_put (plist, Q_value, pgui_item->value);
384 * Decide whether a GUI item is active by evaluating its :active form
388 gui_item_active_p (Lisp_Object gui_item)
390 /* This function can call lisp */
392 /* Shortcut to avoid evaluating Qt each time */
393 return (EQ (XGUI_ITEM (gui_item)->active, Qt)
394 || !NILP (Feval (XGUI_ITEM (gui_item)->active)));
397 /* set menu accelerator key to first underlined character in menu name */
399 gui_item_accelerator (Lisp_Object gui_item)
401 Lisp_Gui_Item *pgui = XGUI_ITEM (gui_item);
403 if (!NILP (pgui->accelerator))
404 return pgui->accelerator;
407 return gui_name_accelerator (pgui->name);
411 gui_name_accelerator (Lisp_Object nm)
413 Bufbyte *name = XSTRING_DATA (nm);
422 if (*name == '_' && *(name + 1))
424 Emchar accelerator = charptr_emchar (name + 1);
425 /* #### bogus current_buffer dependency */
426 return make_char (DOWNCASE (current_buffer, accelerator));
431 return make_char (DOWNCASE (current_buffer,
432 charptr_emchar (XSTRING_DATA (nm))));
436 * Decide whether a GUI item is selected by evaluating its :selected form
440 gui_item_selected_p (Lisp_Object gui_item)
442 /* This function can call lisp */
444 /* Shortcut to avoid evaluating Qt each time */
445 return (EQ (XGUI_ITEM (gui_item)->selected, Qt)
446 || !NILP (Feval (XGUI_ITEM (gui_item)->selected)));
450 gui_item_list_find_selected (Lisp_Object gui_item_list)
452 /* This function can GC. */
454 LIST_LOOP (rest, gui_item_list)
456 if (gui_item_selected_p (XCAR (rest)))
459 return XCAR (gui_item_list);
463 * Decide whether a GUI item is included by evaluating its :included
464 * form if given, and testing its :config form against supplied CONFLIST
465 * configuration variable
468 gui_item_included_p (Lisp_Object gui_item, Lisp_Object conflist)
470 /* This function can call lisp */
471 Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);
473 /* Evaluate :included first. Shortcut to avoid evaluating Qt each time */
474 if (!EQ (pgui_item->included, Qt)
475 && NILP (Feval (pgui_item->included)))
478 /* Do :config if conflist is given */
479 if (!NILP (conflist) && !NILP (pgui_item->config)
480 && NILP (Fmemq (pgui_item->config, conflist)))
487 signal_too_long_error (Lisp_Object name)
489 syntax_error ("GUI item produces too long displayable string", name);
492 #ifdef HAVE_WINDOW_SYSTEM
494 * Format "left flush" display portion of an item into BUF, guarded by
495 * maximum buffer size BUF_LEN. BUF_LEN does not count for terminating
496 * null character, so actual maximum size of buffer consumed is
497 * BUF_LEN + 1 bytes. If buffer is not big enough, then error is
499 * Return value is the offset to the terminating null character into the
503 gui_item_display_flush_left (Lisp_Object gui_item,
504 char *buf, Bytecount buf_len)
506 /* This function can call lisp */
509 Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);
511 /* Copy item name first */
512 CHECK_STRING (pgui_item->name);
513 len = XSTRING_LENGTH (pgui_item->name);
515 signal_too_long_error (pgui_item->name);
516 memcpy (p, XSTRING_DATA (pgui_item->name), len);
519 /* Add space and suffix, if there is a suffix.
520 * If suffix is not string evaluate it */
521 if (!NILP (pgui_item->suffix))
523 Lisp_Object suffix = pgui_item->suffix;
524 /* Shortcut to avoid evaluating suffix each time */
525 if (!STRINGP (suffix))
527 suffix = Feval (suffix);
528 CHECK_STRING (suffix);
531 len = XSTRING_LENGTH (suffix);
532 if (p + len + 1 > buf + buf_len)
533 signal_too_long_error (pgui_item->name);
535 memcpy (p, XSTRING_DATA (suffix), len);
543 * Format "right flush" display portion of an item into BUF, guarded by
544 * maximum buffer size BUF_LEN. BUF_LEN does not count for terminating
545 * null character, so actual maximum size of buffer consumed is
546 * BUF_LEN + 1 bytes. If buffer is not big enough, then error is
548 * Return value is the offset to the terminating null character into the
552 gui_item_display_flush_right (Lisp_Object gui_item,
553 char *buf, Bytecount buf_len)
555 Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);
560 if (!menubar_show_keybindings)
564 /* Try :keys first */
565 if (!NILP (pgui_item->keys))
567 CHECK_STRING (pgui_item->keys);
568 if (XSTRING_LENGTH (pgui_item->keys) + 1 > buf_len)
569 signal_too_long_error (pgui_item->name);
570 memcpy (buf, XSTRING_DATA (pgui_item->keys),
571 XSTRING_LENGTH (pgui_item->keys) + 1);
572 return XSTRING_LENGTH (pgui_item->keys);
575 /* See if we can derive keys out of callback symbol */
576 if (SYMBOLP (pgui_item->callback))
578 char buf2[1024]; /* #### */
581 where_is_to_char (pgui_item->callback, buf2);
584 signal_too_long_error (pgui_item->name);
589 /* No keys - no right flush display */
592 #endif /* HAVE_WINDOW_SYSTEM */
595 mark_gui_item (Lisp_Object obj)
597 Lisp_Gui_Item *p = XGUI_ITEM (obj);
599 mark_object (p->name);
600 mark_object (p->callback);
601 mark_object (p->callback_ex);
602 mark_object (p->config);
603 mark_object (p->suffix);
604 mark_object (p->active);
605 mark_object (p->included);
606 mark_object (p->config);
607 mark_object (p->filter);
608 mark_object (p->style);
609 mark_object (p->selected);
610 mark_object (p->keys);
611 mark_object (p->accelerator);
612 mark_object (p->value);
618 gui_item_hash (Lisp_Object obj, int depth)
620 Lisp_Gui_Item *p = XGUI_ITEM (obj);
622 return HASH2 (HASH6 (internal_hash (p->name, depth + 1),
623 internal_hash (p->callback, depth + 1),
624 internal_hash (p->callback_ex, depth + 1),
625 internal_hash (p->suffix, depth + 1),
626 internal_hash (p->active, depth + 1),
627 internal_hash (p->included, depth + 1)),
628 HASH6 (internal_hash (p->config, depth + 1),
629 internal_hash (p->filter, depth + 1),
630 internal_hash (p->style, depth + 1),
631 internal_hash (p->selected, depth + 1),
632 internal_hash (p->keys, depth + 1),
633 internal_hash (p->value, depth + 1)));
637 gui_item_id_hash (Lisp_Object hashtable, Lisp_Object gitem, int slot)
639 int hashid = gui_item_hash (gitem, 0);
640 int id = GUI_ITEM_ID_BITS (hashid, slot);
641 while (!NILP (Fgethash (make_int (id),
644 id = GUI_ITEM_ID_BITS (id + 1, slot);
650 gui_item_equal_sans_selected (Lisp_Object obj1, Lisp_Object obj2, int depth)
652 Lisp_Gui_Item *p1 = XGUI_ITEM (obj1);
653 Lisp_Gui_Item *p2 = XGUI_ITEM (obj2);
655 if (!(internal_equal (p1->name, p2->name, depth + 1)
657 internal_equal (p1->callback, p2->callback, depth + 1)
659 internal_equal (p1->callback_ex, p2->callback_ex, depth + 1)
661 EQ (p1->suffix, p2->suffix)
663 EQ (p1->active, p2->active)
665 EQ (p1->included, p2->included)
667 EQ (p1->config, p2->config)
669 EQ (p1->filter, p2->filter)
671 EQ (p1->style, p2->style)
673 EQ (p1->accelerator, p2->accelerator)
675 EQ (p1->keys, p2->keys)
677 EQ (p1->value, p2->value)))
683 gui_item_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
685 Lisp_Gui_Item *p1 = XGUI_ITEM (obj1);
686 Lisp_Gui_Item *p2 = XGUI_ITEM (obj2);
688 if (!(gui_item_equal_sans_selected (obj1, obj2, depth)
690 EQ (p1->selected, p2->selected)))
696 print_gui_item (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
698 Lisp_Gui_Item *g = XGUI_ITEM (obj);
702 error ("printing unreadable object #<gui-item 0x%x>", g->header.uid);
704 write_c_string ("#<gui-item ", printcharfun);
705 sprintf (buf, "0x%x>", g->header.uid);
706 write_c_string (buf, printcharfun);
710 copy_gui_item (Lisp_Object gui_item)
712 Lisp_Object ret = allocate_gui_item ();
713 Lisp_Gui_Item *lp, *g = XGUI_ITEM (gui_item);
715 lp = XGUI_ITEM (ret);
717 lp->callback = g->callback;
718 lp->callback_ex = g->callback_ex;
719 lp->suffix = g->suffix;
720 lp->active = g->active;
721 lp->included = g->included;
722 lp->config = g->config;
723 lp->filter = g->filter;
724 lp->style = g->style;
725 lp->selected = g->selected;
727 lp->accelerator = g->accelerator;
728 lp->value = g->value;
734 copy_gui_item_tree (Lisp_Object arg)
738 Lisp_Object rest = arg = Fcopy_sequence (arg);
741 XCAR (rest) = copy_gui_item_tree (XCAR (rest));
746 else if (GUI_ITEMP (arg))
747 return copy_gui_item (arg);
752 /* parse a glyph descriptor into a tree of gui items.
754 The gui_item slot of an image instance can be a single item or an
755 arbitrarily nested hierarchy of item lists. */
758 parse_gui_item_tree_item (Lisp_Object entry)
760 Lisp_Object ret = entry;
767 ret = gui_parse_item_keywords_no_errors (entry);
769 else if (STRINGP (entry))
771 CHECK_STRING (entry);
774 syntax_error ("item must be a vector or a string", entry);
776 RETURN_UNGCPRO (ret);
780 parse_gui_item_tree_children (Lisp_Object list)
782 Lisp_Object rest, ret = Qnil, sub = Qnil;
783 struct gcpro gcpro1, gcpro2;
787 /* recursively add items to the tree view */
788 LIST_LOOP (rest, list)
790 if (CONSP (XCAR (rest)))
791 sub = parse_gui_item_tree_list (XCAR (rest));
793 sub = parse_gui_item_tree_item (XCAR (rest));
795 ret = Fcons (sub, ret);
797 /* make the order the same as the items we have parsed */
798 RETURN_UNGCPRO (Fnreverse (ret));
802 parse_gui_item_tree_list (Lisp_Object list)
807 /* first one can never be a list */
808 ret = parse_gui_item_tree_item (XCAR (list));
810 ret = Fcons (ret, parse_gui_item_tree_children (XCDR (list)));
811 RETURN_UNGCPRO (ret);
815 finalize_gui_item (void* header, int for_disksave)
819 DEFINE_LRECORD_IMPLEMENTATION ("gui-item", gui_item,
820 mark_gui_item, print_gui_item,
821 finalize_gui_item, gui_item_equal,
829 INIT_LRECORD_IMPLEMENTATION (gui_item);
831 DEFSYMBOL (Qmenu_no_selection_hook);
834 DEFSUBR (Fpopup_up_p);
841 DEFVAR_LISP ("menu-no-selection-hook", &Vmenu_no_selection_hook /*
842 Function or functions to call when a menu or dialog box is dismissed
843 without a selection having been made.
845 Vmenu_no_selection_hook = Qnil;