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);
43 /* count of menus/dboxes currently up */
46 DEFUN ("popup-up-p", Fpopup_up_p, 0, 0, 0, /*
47 Return t if a popup menu or dialog box is up, nil otherwise.
48 See `popup-menu' and `popup-dialog-box'.
52 return popup_up_p ? Qt : Qnil;
54 #endif /* HAVE_POPUPS */
57 separator_string_p (const Bufbyte *s)
62 if (!s || s[0] == '\0')
65 if (first != '-' && first != '=')
67 for (p = s; *p == first; p++)
70 return (*p == '!' || *p == ':' || *p == '\0');
73 /* Massage DATA to find the correct function and argument. Used by
74 popup_selection_callback() and the msw code. */
76 get_gui_callback (Lisp_Object data, Lisp_Object *fn, Lisp_Object *arg)
81 *arg = list3 (Qsignal, list2 (Qquote, Qquit), Qnil);
84 else if (SYMBOLP (data)
85 || (COMPILED_FUNCTIONP (data)
86 && XCOMPILED_FUNCTION (data)->flags.interactivep)
87 || (CONSP (data) && (EQ (XCAR (data), Qlambda))
88 && !NILP (Fassq (Qinteractive, Fcdr (Fcdr (data))))))
90 *fn = Qcall_interactively;
93 else if (CONSP (data))
101 *arg = list3 (Qsignal,
102 list2 (Qquote, Qerror),
103 list2 (Qquote, list2 (build_translated_string
104 ("illegal callback"),
110 * Add a value VAL associated with keyword KEY into PGUI_ITEM
111 * structure. If KEY is not a keyword, or is an unknown keyword, then
115 gui_item_add_keyval_pair (Lisp_Object gui_item,
116 Lisp_Object key, Lisp_Object val,
119 Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);
122 syntax_error_2 ("Non-keyword in gui item", key, pgui_item->name);
124 if (EQ (key, Q_suffix)) pgui_item->suffix = val;
125 else if (EQ (key, Q_active)) pgui_item->active = val;
126 else if (EQ (key, Q_included)) pgui_item->included = val;
127 else if (EQ (key, Q_config)) pgui_item->config = val;
128 else if (EQ (key, Q_filter)) pgui_item->filter = val;
129 else if (EQ (key, Q_style)) pgui_item->style = val;
130 else if (EQ (key, Q_selected)) pgui_item->selected = val;
131 else if (EQ (key, Q_keys)) pgui_item->keys = val;
132 else if (EQ (key, Q_callback)) pgui_item->callback = val;
133 else if (EQ (key, Q_callback_ex)) pgui_item->callback_ex = val;
134 else if (EQ (key, Q_value)) pgui_item->value = val;
135 else if (EQ (key, Q_key_sequence)) ; /* ignored for FSF compatibility */
136 else if (EQ (key, Q_label)) ; /* ignored for 21.0 implement in 21.2 */
137 else if (EQ (key, Q_accelerator))
139 if (SYMBOLP (val) || CHARP (val))
140 pgui_item->accelerator = val;
141 else if (ERRB_EQ (errb, ERROR_ME))
142 syntax_error ("Bad keyboard accelerator", val);
144 else if (ERRB_EQ (errb, ERROR_ME))
145 syntax_error_2 ("Unknown keyword in gui item", key,
150 gui_item_init (Lisp_Object gui_item)
152 Lisp_Gui_Item *lp = XGUI_ITEM (gui_item);
156 lp->callback_ex = Qnil;
165 lp->accelerator = Qnil;
170 allocate_gui_item (void)
172 Lisp_Gui_Item *lp = alloc_lcrecord_type (Lisp_Gui_Item, &lrecord_gui_item);
176 XSETGUI_ITEM (val, lp);
184 * ITEM is a lisp vector, describing a menu item or a button. The
185 * function extracts the description of the item into the PGUI_ITEM
189 make_gui_item_from_keywords_internal (Lisp_Object item,
192 int length, plist_p, start;
193 Lisp_Object *contents;
194 Lisp_Object gui_item = allocate_gui_item ();
195 Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);
198 length = XVECTOR_LENGTH (item);
199 contents = XVECTOR_DATA (item);
202 syntax_error ("GUI item descriptors must be at least 1 elts long", item);
204 /* length 1: [ "name" ]
205 length 2: [ "name" callback ]
206 length 3: [ "name" callback active-p ]
207 or [ "name" keyword value ]
208 length 4: [ "name" callback active-p suffix ]
209 or [ "name" callback keyword value ]
210 length 5+: [ "name" callback [ keyword value ]+ ]
211 or [ "name" [ keyword value ]+ ]
213 plist_p = (length > 2 && (KEYWORDP (contents [1])
214 || KEYWORDP (contents [2])));
216 pgui_item->name = contents [0];
217 if (length > 1 && !KEYWORDP (contents [1]))
219 pgui_item->callback = contents [1];
225 if (!plist_p && length > 2)
228 pgui_item->active = contents [2];
230 pgui_item->suffix = contents [3];
236 if ((length - start) & 1)
238 "GUI item descriptor has an odd number of keywords and values",
241 for (i = start; i < length;)
243 Lisp_Object key = contents [i++];
244 Lisp_Object val = contents [i++];
245 gui_item_add_keyval_pair (gui_item, key, val, errb);
252 gui_parse_item_keywords (Lisp_Object item)
254 return make_gui_item_from_keywords_internal (item, ERROR_ME);
258 gui_parse_item_keywords_no_errors (Lisp_Object item)
260 return make_gui_item_from_keywords_internal (item, ERROR_ME_NOT);
263 /* convert a gui item into plist properties */
265 gui_add_item_keywords_to_plist (Lisp_Object plist, Lisp_Object gui_item)
267 Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);
269 if (!NILP (pgui_item->callback))
270 Fplist_put (plist, Q_callback, pgui_item->callback);
271 if (!NILP (pgui_item->callback_ex))
272 Fplist_put (plist, Q_callback_ex, pgui_item->callback_ex);
273 if (!NILP (pgui_item->suffix))
274 Fplist_put (plist, Q_suffix, pgui_item->suffix);
275 if (!NILP (pgui_item->active))
276 Fplist_put (plist, Q_active, pgui_item->active);
277 if (!NILP (pgui_item->included))
278 Fplist_put (plist, Q_included, pgui_item->included);
279 if (!NILP (pgui_item->config))
280 Fplist_put (plist, Q_config, pgui_item->config);
281 if (!NILP (pgui_item->filter))
282 Fplist_put (plist, Q_filter, pgui_item->filter);
283 if (!NILP (pgui_item->style))
284 Fplist_put (plist, Q_style, pgui_item->style);
285 if (!NILP (pgui_item->selected))
286 Fplist_put (plist, Q_selected, pgui_item->selected);
287 if (!NILP (pgui_item->keys))
288 Fplist_put (plist, Q_keys, pgui_item->keys);
289 if (!NILP (pgui_item->accelerator))
290 Fplist_put (plist, Q_accelerator, pgui_item->accelerator);
291 if (!NILP (pgui_item->value))
292 Fplist_put (plist, Q_value, pgui_item->value);
296 * Decide whether a GUI item is active by evaluating its :active form
300 gui_item_active_p (Lisp_Object gui_item)
302 /* This function can call lisp */
304 /* Shortcut to avoid evaluating Qt each time */
305 return (EQ (XGUI_ITEM (gui_item)->active, Qt)
306 || !NILP (Feval (XGUI_ITEM (gui_item)->active)));
309 /* set menu accelerator key to first underlined character in menu name */
311 gui_item_accelerator (Lisp_Object gui_item)
313 Lisp_Gui_Item *pgui = XGUI_ITEM (gui_item);
315 if (!NILP (pgui->accelerator))
316 return pgui->accelerator;
319 return gui_name_accelerator (pgui->name);
323 gui_name_accelerator (Lisp_Object nm)
325 Bufbyte *name = XSTRING_DATA (nm);
334 if (*name == '_' && *(name + 1))
336 Emchar accelerator = charptr_emchar (name + 1);
337 /* #### bogus current_buffer dependency */
338 return make_char (DOWNCASE (current_buffer, accelerator));
343 return make_char (DOWNCASE (current_buffer,
344 charptr_emchar (XSTRING_DATA (nm))));
348 * Decide whether a GUI item is selected by evaluating its :selected form
352 gui_item_selected_p (Lisp_Object gui_item)
354 /* This function can call lisp */
356 /* Shortcut to avoid evaluating Qt each time */
357 return (EQ (XGUI_ITEM (gui_item)->selected, Qt)
358 || !NILP (Feval (XGUI_ITEM (gui_item)->selected)));
362 gui_item_list_find_selected (Lisp_Object gui_item_list)
364 /* This function can GC. */
366 LIST_LOOP (rest, gui_item_list)
368 if (gui_item_selected_p (XCAR (rest)))
371 return XCAR (gui_item_list);
375 * Decide whether a GUI item is included by evaluating its :included
376 * form if given, and testing its :config form against supplied CONFLIST
377 * configuration variable
380 gui_item_included_p (Lisp_Object gui_item, Lisp_Object conflist)
382 /* This function can call lisp */
383 Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);
385 /* Evaluate :included first. Shortcut to avoid evaluating Qt each time */
386 if (!EQ (pgui_item->included, Qt)
387 && NILP (Feval (pgui_item->included)))
390 /* Do :config if conflist is given */
391 if (!NILP (conflist) && !NILP (pgui_item->config)
392 && NILP (Fmemq (pgui_item->config, conflist)))
399 signal_too_long_error (Lisp_Object name)
401 syntax_error ("GUI item produces too long displayable string", name);
404 #ifdef HAVE_WINDOW_SYSTEM
406 * Format "left flush" display portion of an item into BUF, guarded by
407 * maximum buffer size BUF_LEN. BUF_LEN does not count for terminating
408 * null character, so actual maximum size of buffer consumed is
409 * BUF_LEN + 1 bytes. If buffer is not big enough, then error is
411 * Return value is the offset to the terminating null character into the
415 gui_item_display_flush_left (Lisp_Object gui_item,
416 char *buf, Bytecount buf_len)
418 /* This function can call lisp */
421 Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);
423 /* Copy item name first */
424 CHECK_STRING (pgui_item->name);
425 len = XSTRING_LENGTH (pgui_item->name);
427 signal_too_long_error (pgui_item->name);
428 memcpy (p, XSTRING_DATA (pgui_item->name), len);
431 /* Add space and suffix, if there is a suffix.
432 * If suffix is not string evaluate it */
433 if (!NILP (pgui_item->suffix))
435 Lisp_Object suffix = pgui_item->suffix;
436 /* Shortcut to avoid evaluating suffix each time */
437 if (!STRINGP (suffix))
439 suffix = Feval (suffix);
440 CHECK_STRING (suffix);
443 len = XSTRING_LENGTH (suffix);
444 if (p + len + 1 > buf + buf_len)
445 signal_too_long_error (pgui_item->name);
447 memcpy (p, XSTRING_DATA (suffix), len);
455 * Format "right flush" display portion of an item into BUF, guarded by
456 * maximum buffer size BUF_LEN. BUF_LEN does not count for terminating
457 * null character, so actual maximum size of buffer consumed is
458 * BUF_LEN + 1 bytes. If buffer is not big enough, then error is
460 * Return value is the offset to the terminating null character into the
464 gui_item_display_flush_right (Lisp_Object gui_item,
465 char *buf, Bytecount buf_len)
467 Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);
472 if (!menubar_show_keybindings)
476 /* Try :keys first */
477 if (!NILP (pgui_item->keys))
479 CHECK_STRING (pgui_item->keys);
480 if (XSTRING_LENGTH (pgui_item->keys) + 1 > buf_len)
481 signal_too_long_error (pgui_item->name);
482 memcpy (buf, XSTRING_DATA (pgui_item->keys),
483 XSTRING_LENGTH (pgui_item->keys) + 1);
484 return XSTRING_LENGTH (pgui_item->keys);
487 /* See if we can derive keys out of callback symbol */
488 if (SYMBOLP (pgui_item->callback))
490 char buf2[1024]; /* #### */
493 where_is_to_char (pgui_item->callback, buf2);
496 signal_too_long_error (pgui_item->name);
501 /* No keys - no right flush display */
504 #endif /* HAVE_WINDOW_SYSTEM */
507 mark_gui_item (Lisp_Object obj)
509 Lisp_Gui_Item *p = XGUI_ITEM (obj);
511 mark_object (p->name);
512 mark_object (p->callback);
513 mark_object (p->callback_ex);
514 mark_object (p->config);
515 mark_object (p->suffix);
516 mark_object (p->active);
517 mark_object (p->included);
518 mark_object (p->config);
519 mark_object (p->filter);
520 mark_object (p->style);
521 mark_object (p->selected);
522 mark_object (p->keys);
523 mark_object (p->accelerator);
524 mark_object (p->value);
530 gui_item_hash (Lisp_Object obj, int depth)
532 Lisp_Gui_Item *p = XGUI_ITEM (obj);
534 return HASH2 (HASH6 (internal_hash (p->name, depth + 1),
535 internal_hash (p->callback, depth + 1),
536 internal_hash (p->callback_ex, depth + 1),
537 internal_hash (p->suffix, depth + 1),
538 internal_hash (p->active, depth + 1),
539 internal_hash (p->included, depth + 1)),
540 HASH6 (internal_hash (p->config, depth + 1),
541 internal_hash (p->filter, depth + 1),
542 internal_hash (p->style, depth + 1),
543 internal_hash (p->selected, depth + 1),
544 internal_hash (p->keys, depth + 1),
545 internal_hash (p->value, depth + 1)));
549 gui_item_id_hash (Lisp_Object hashtable, Lisp_Object gitem, int slot)
551 int hashid = gui_item_hash (gitem, 0);
552 int id = GUI_ITEM_ID_BITS (hashid, slot);
553 while (!NILP (Fgethash (make_int (id),
556 id = GUI_ITEM_ID_BITS (id + 1, slot);
562 gui_item_equal_sans_selected (Lisp_Object obj1, Lisp_Object obj2, int depth)
564 Lisp_Gui_Item *p1 = XGUI_ITEM (obj1);
565 Lisp_Gui_Item *p2 = XGUI_ITEM (obj2);
567 if (!(internal_equal (p1->name, p2->name, depth + 1)
569 internal_equal (p1->callback, p2->callback, depth + 1)
571 internal_equal (p1->callback_ex, p2->callback_ex, depth + 1)
573 EQ (p1->suffix, p2->suffix)
575 EQ (p1->active, p2->active)
577 EQ (p1->included, p2->included)
579 EQ (p1->config, p2->config)
581 EQ (p1->filter, p2->filter)
583 EQ (p1->style, p2->style)
585 EQ (p1->accelerator, p2->accelerator)
587 EQ (p1->keys, p2->keys)
589 EQ (p1->value, p2->value)))
595 gui_item_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
597 Lisp_Gui_Item *p1 = XGUI_ITEM (obj1);
598 Lisp_Gui_Item *p2 = XGUI_ITEM (obj2);
600 if (!(gui_item_equal_sans_selected (obj1, obj2, depth)
602 EQ (p1->selected, p2->selected)))
608 print_gui_item (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
610 Lisp_Gui_Item *g = XGUI_ITEM (obj);
614 error ("printing unreadable object #<gui-item 0x%x>", g->header.uid);
616 write_c_string ("#<gui-item ", printcharfun);
617 sprintf (buf, "0x%x>", g->header.uid);
618 write_c_string (buf, printcharfun);
622 copy_gui_item (Lisp_Object gui_item)
624 Lisp_Object ret = allocate_gui_item ();
625 Lisp_Gui_Item *lp, *g = XGUI_ITEM (gui_item);
627 lp = XGUI_ITEM (ret);
629 lp->callback = g->callback;
630 lp->callback_ex = g->callback_ex;
631 lp->suffix = g->suffix;
632 lp->active = g->active;
633 lp->included = g->included;
634 lp->config = g->config;
635 lp->filter = g->filter;
636 lp->style = g->style;
637 lp->selected = g->selected;
639 lp->accelerator = g->accelerator;
640 lp->value = g->value;
646 copy_gui_item_tree (Lisp_Object arg)
650 Lisp_Object rest = arg = Fcopy_sequence (arg);
653 XCAR (rest) = copy_gui_item_tree (XCAR (rest));
658 else if (GUI_ITEMP (arg))
659 return copy_gui_item (arg);
664 /* parse a glyph descriptor into a tree of gui items.
666 The gui_item slot of an image instance can be a single item or an
667 arbitrarily nested hierarchy of item lists. */
670 parse_gui_item_tree_item (Lisp_Object entry)
672 Lisp_Object ret = entry;
679 ret = gui_parse_item_keywords_no_errors (entry);
681 else if (STRINGP (entry))
683 CHECK_STRING (entry);
686 syntax_error ("item must be a vector or a string", entry);
688 RETURN_UNGCPRO (ret);
692 parse_gui_item_tree_children (Lisp_Object list)
694 Lisp_Object rest, ret = Qnil, sub = Qnil;
695 struct gcpro gcpro1, gcpro2;
699 /* recursively add items to the tree view */
700 LIST_LOOP (rest, list)
702 if (CONSP (XCAR (rest)))
703 sub = parse_gui_item_tree_list (XCAR (rest));
705 sub = parse_gui_item_tree_item (XCAR (rest));
707 ret = Fcons (sub, ret);
709 /* make the order the same as the items we have parsed */
710 RETURN_UNGCPRO (Fnreverse (ret));
714 parse_gui_item_tree_list (Lisp_Object list)
719 /* first one can never be a list */
720 ret = parse_gui_item_tree_item (XCAR (list));
722 ret = Fcons (ret, parse_gui_item_tree_children (XCDR (list)));
723 RETURN_UNGCPRO (ret);
727 finalize_gui_item (void* header, int for_disksave)
731 DEFINE_LRECORD_IMPLEMENTATION ("gui-item", gui_item,
732 mark_gui_item, print_gui_item,
733 finalize_gui_item, gui_item_equal,
741 INIT_LRECORD_IMPLEMENTATION (gui_item);
743 DEFSYMBOL (Qmenu_no_selection_hook);
746 DEFSUBR (Fpopup_up_p);
753 DEFVAR_LISP ("menu-no-selection-hook", &Vmenu_no_selection_hook /*
754 Function or functions to call when a menu or dialog box is dismissed
755 without a selection having been made.
757 Vmenu_no_selection_hook = Qnil;