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. */
32 Lisp_Object Q_active, Q_suffix, Q_keys, Q_style, Q_selected;
33 Lisp_Object Q_filter, Q_config, Q_included, Q_key_sequence;
34 Lisp_Object Q_accelerator, Q_label, Q_callback;
35 Lisp_Object Qtoggle, Qradio;
37 static Lisp_Object parse_gui_item_tree_list (Lisp_Object list);
41 /* count of menus/dboxes currently up */
44 DEFUN ("popup-up-p", Fpopup_up_p, 0, 0, 0, /*
45 Return t if a popup menu or dialog box is up, nil otherwise.
46 See `popup-menu' and `popup-dialog-box'.
50 return popup_up_p ? Qt : Qnil;
52 #endif /* HAVE_POPUPS */
55 separator_string_p (CONST char *s)
60 if (!s || s[0] == '\0')
63 if (first != '-' && first != '=')
65 for (p = s; *p == first; p++)
68 return (*p == '!' || *p == ':' || *p == '\0');
71 /* Massage DATA to find the correct function and argument. Used by
72 popup_selection_callback() and the msw code. */
74 get_gui_callback (Lisp_Object data, Lisp_Object *fn, Lisp_Object *arg)
77 || (COMPILED_FUNCTIONP (data)
78 && XCOMPILED_FUNCTION (data)->flags.interactivep)
79 || (CONSP (data) && (EQ (XCAR (data), Qlambda))
80 && !NILP (Fassq (Qinteractive, Fcdr (Fcdr (data))))))
82 *fn = Qcall_interactively;
85 else if (CONSP (data))
93 *arg = list3 (Qsignal,
94 list2 (Qquote, Qerror),
95 list2 (Qquote, list2 (build_translated_string
102 * Add a value VAL associated with keyword KEY into PGUI_ITEM
103 * structure. If KEY is not a keyword, or is an unknown keyword, then
107 gui_item_add_keyval_pair (Lisp_Object gui_item,
108 Lisp_Object key, Lisp_Object val,
111 struct Lisp_Gui_Item* pgui_item = XGUI_ITEM (gui_item);
114 signal_simple_error_2 ("Non-keyword in gui item", key, pgui_item->name);
116 if (EQ (key, Q_suffix)) pgui_item->suffix = val;
117 else if (EQ (key, Q_active)) pgui_item->active = val;
118 else if (EQ (key, Q_included)) pgui_item->included = val;
119 else if (EQ (key, Q_config)) pgui_item->config = val;
120 else if (EQ (key, Q_filter)) pgui_item->filter = val;
121 else if (EQ (key, Q_style)) pgui_item->style = val;
122 else if (EQ (key, Q_selected)) pgui_item->selected = val;
123 else if (EQ (key, Q_keys)) pgui_item->keys = val;
124 else if (EQ (key, Q_callback)) pgui_item->callback = val;
125 else if (EQ (key, Q_key_sequence)) ; /* ignored for FSF compatability */
126 else if (EQ (key, Q_label)) ; /* ignored for 21.0 implement in 21.2 */
127 else if (EQ (key, Q_accelerator))
129 if (SYMBOLP (val) || CHARP (val))
130 pgui_item->accelerator = val;
131 else if (ERRB_EQ (errb, ERROR_ME))
132 signal_simple_error ("Bad keyboard accelerator", val);
134 else if (ERRB_EQ (errb, ERROR_ME))
135 signal_simple_error_2 ("Unknown keyword in gui item", key, pgui_item->name);
139 gui_item_init (Lisp_Object gui_item)
141 struct Lisp_Gui_Item *lp = XGUI_ITEM (gui_item);
153 lp->accelerator = Qnil;
157 allocate_gui_item (void)
159 struct Lisp_Gui_Item *lp =
160 alloc_lcrecord_type (struct Lisp_Gui_Item, &lrecord_gui_item);
164 XSETGUI_ITEM (val, lp);
172 * ITEM is a lisp vector, describing a menu item or a button. The
173 * function extracts the description of the item into the PGUI_ITEM
177 make_gui_item_from_keywords_internal (Lisp_Object item,
180 int length, plist_p, start;
181 Lisp_Object *contents;
182 Lisp_Object gui_item = allocate_gui_item ();
183 struct Lisp_Gui_Item* pgui_item = XGUI_ITEM (gui_item);
186 length = XVECTOR_LENGTH (item);
187 contents = XVECTOR_DATA (item);
190 signal_simple_error ("GUI item descriptors must be at least 1 elts long", item);
192 /* length 1: [ "name" ]
193 length 2: [ "name" callback ]
194 length 3: [ "name" callback active-p ]
195 or [ "name" keyword value ]
196 length 4: [ "name" callback active-p suffix ]
197 or [ "name" callback keyword value ]
198 length 5+: [ "name" callback [ keyword value ]+ ]
199 or [ "name" [ keyword value ]+ ]
201 plist_p = (length > 2 && (KEYWORDP (contents [1])
202 || KEYWORDP (contents [2])));
204 pgui_item->name = contents [0];
205 if (length > 1 && !KEYWORDP (contents [1]))
207 pgui_item->callback = contents [1];
213 if (!plist_p && length > 2)
216 pgui_item->active = contents [2];
218 pgui_item->suffix = contents [3];
224 if ((length - start) & 1)
225 signal_simple_error (
226 "GUI item descriptor has an odd number of keywords and values",
229 for (i = start; i < length;)
231 Lisp_Object key = contents [i++];
232 Lisp_Object val = contents [i++];
233 gui_item_add_keyval_pair (gui_item, key, val, errb);
240 gui_parse_item_keywords (Lisp_Object item)
242 return make_gui_item_from_keywords_internal (item, ERROR_ME);
246 gui_parse_item_keywords_no_errors (Lisp_Object item)
248 return make_gui_item_from_keywords_internal (item, ERROR_ME_NOT);
251 /* convert a gui item into plist properties */
253 gui_add_item_keywords_to_plist (Lisp_Object plist, Lisp_Object gui_item)
255 struct Lisp_Gui_Item* pgui_item = XGUI_ITEM (gui_item);
257 if (!NILP (pgui_item->callback))
258 Fplist_put (plist, Q_callback, pgui_item->callback);
259 if (!NILP (pgui_item->suffix))
260 Fplist_put (plist, Q_suffix, pgui_item->suffix);
261 if (!NILP (pgui_item->active))
262 Fplist_put (plist, Q_active, pgui_item->active);
263 if (!NILP (pgui_item->included))
264 Fplist_put (plist, Q_included, pgui_item->included);
265 if (!NILP (pgui_item->config))
266 Fplist_put (plist, Q_config, pgui_item->config);
267 if (!NILP (pgui_item->filter))
268 Fplist_put (plist, Q_filter, pgui_item->filter);
269 if (!NILP (pgui_item->style))
270 Fplist_put (plist, Q_style, pgui_item->style);
271 if (!NILP (pgui_item->selected))
272 Fplist_put (plist, Q_selected, pgui_item->selected);
273 if (!NILP (pgui_item->keys))
274 Fplist_put (plist, Q_keys, pgui_item->keys);
275 if (!NILP (pgui_item->accelerator))
276 Fplist_put (plist, Q_accelerator, pgui_item->accelerator);
280 * Decide whether a GUI item is active by evaluating its :active form
284 gui_item_active_p (Lisp_Object gui_item)
286 /* This function can call lisp */
288 /* Shortcut to avoid evaluating Qt each time */
289 return (EQ (XGUI_ITEM (gui_item)->active, Qt)
290 || !NILP (Feval (XGUI_ITEM (gui_item)->active)));
293 /* set menu accelerator key to first underlined character in menu name */
295 gui_item_accelerator (Lisp_Object gui_item)
297 struct Lisp_Gui_Item* pgui = XGUI_ITEM (gui_item);
299 if (!NILP (pgui->accelerator))
300 return pgui->accelerator;
307 gui_name_accelerator (Lisp_Object nm)
309 /* !!#### This function has not been Mule-ized */
310 char* name = (char*)XSTRING_DATA (nm);
317 if (*name=='_' && *(name+1))
319 int accelerator = (int) (unsigned char) (*(name+1));
320 return make_char (tolower (accelerator));
329 * Decide whether a GUI item is selected by evaluating its :selected form
333 gui_item_selected_p (Lisp_Object gui_item)
335 /* This function can call lisp */
337 /* Shortcut to avoid evaluating Qt each time */
338 return (EQ (XGUI_ITEM (gui_item)->selected, Qt)
339 || !NILP (Feval (XGUI_ITEM (gui_item)->selected)));
343 * Decide whether a GUI item is included by evaluating its :included
344 * form if given, and testing its :config form against supplied CONFLIST
345 * configuration variable
348 gui_item_included_p (Lisp_Object gui_item, Lisp_Object conflist)
350 /* This function can call lisp */
351 struct Lisp_Gui_Item* pgui_item = XGUI_ITEM (gui_item);
353 /* Evaluate :included first. Shortcut to avoid evaluating Qt each time */
354 if (!EQ (pgui_item->included, Qt)
355 && NILP (Feval (pgui_item->included)))
358 /* Do :config if conflist is given */
359 if (!NILP (conflist) && !NILP (pgui_item->config)
360 && NILP (Fmemq (pgui_item->config, conflist)))
367 signal_too_long_error (Lisp_Object name)
369 signal_simple_error ("GUI item produces too long displayable string", name);
372 #ifdef HAVE_WINDOW_SYSTEM
374 * Format "left flush" display portion of an item into BUF, guarded by
375 * maximum buffer size BUF_LEN. BUF_LEN does not count for terminating
376 * null character, so actual maximum size of buffer consumed is
377 * BUF_LEN + 1 bytes. If buffer is not big enough, then error is
379 * Return value is the offset to the terminating null character into the
383 gui_item_display_flush_left (Lisp_Object gui_item,
384 char* buf, Bytecount buf_len)
386 /* This function can call lisp */
389 struct Lisp_Gui_Item* pgui_item = XGUI_ITEM (gui_item);
391 /* Copy item name first */
392 CHECK_STRING (pgui_item->name);
393 len = XSTRING_LENGTH (pgui_item->name);
395 signal_too_long_error (pgui_item->name);
396 memcpy (p, XSTRING_DATA (pgui_item->name), len);
399 /* Add space and suffix, if there is a suffix.
400 * If suffix is not string evaluate it */
401 if (!NILP (pgui_item->suffix))
403 Lisp_Object suffix = pgui_item->suffix;
404 /* Shortcut to avoid evaluating suffix each time */
405 if (!STRINGP (suffix))
407 suffix = Feval (suffix);
408 CHECK_STRING (suffix);
411 len = XSTRING_LENGTH (suffix);
412 if (p + len + 1 > buf + buf_len)
413 signal_too_long_error (pgui_item->name);
415 memcpy (p, XSTRING_DATA (suffix), len);
423 * Format "right flush" display portion of an item into BUF, guarded by
424 * maximum buffer size BUF_LEN. BUF_LEN does not count for terminating
425 * null character, so actual maximum size of buffer consumed is
426 * BUF_LEN + 1 bytes. If buffer is not big enough, then error is
428 * Return value is the offset to the terminating null character into the
432 gui_item_display_flush_right (Lisp_Object gui_item,
433 char* buf, Bytecount buf_len)
435 struct Lisp_Gui_Item* pgui_item = XGUI_ITEM (gui_item);
440 if (!menubar_show_keybindings)
444 /* Try :keys first */
445 if (!NILP (pgui_item->keys))
447 CHECK_STRING (pgui_item->keys);
448 if (XSTRING_LENGTH (pgui_item->keys) > buf_len)
449 signal_too_long_error (pgui_item->name);
450 strcpy (buf, (CONST char *) XSTRING_DATA (pgui_item->keys));
451 return XSTRING_LENGTH (pgui_item->keys);
454 /* See if we can derive keys out of callback symbol */
455 if (SYMBOLP (pgui_item->callback))
460 where_is_to_char (pgui_item->callback, buf2);
463 signal_too_long_error (pgui_item->name);
468 /* No keys - no right flush display */
471 #endif /* HAVE_WINDOW_SYSTEM */
474 mark_gui_item (Lisp_Object obj)
476 struct Lisp_Gui_Item *p = XGUI_ITEM (obj);
478 mark_object (p->name);
479 mark_object (p->callback);
480 mark_object (p->config);
481 mark_object (p->suffix);
482 mark_object (p->active);
483 mark_object (p->included);
484 mark_object (p->config);
485 mark_object (p->filter);
486 mark_object (p->style);
487 mark_object (p->selected);
488 mark_object (p->keys);
489 mark_object (p->accelerator);
495 gui_item_hash (Lisp_Object obj, int depth)
497 struct Lisp_Gui_Item *p = XGUI_ITEM (obj);
499 return HASH2 (HASH5 (internal_hash (p->name, depth + 1),
500 internal_hash (p->callback, depth + 1),
501 internal_hash (p->suffix, depth + 1),
502 internal_hash (p->active, depth + 1),
503 internal_hash (p->included, depth + 1)),
504 HASH5 (internal_hash (p->config, depth + 1),
505 internal_hash (p->filter, depth + 1),
506 internal_hash (p->style, depth + 1),
507 internal_hash (p->selected, depth + 1),
508 internal_hash (p->keys, depth + 1)));
512 gui_item_id_hash (Lisp_Object hashtable, Lisp_Object gitem, int slot)
514 int hashid = gui_item_hash (gitem, 0);
515 int id = GUI_ITEM_ID_BITS (hashid, slot);
516 while (!NILP (Fgethash (make_int (id),
519 id = GUI_ITEM_ID_BITS (id + 1, slot);
525 gui_item_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
527 struct Lisp_Gui_Item *p1 = XGUI_ITEM (obj1);
528 struct Lisp_Gui_Item *p2 = XGUI_ITEM (obj2);
530 if (!(internal_equal (p1->name, p2->name, depth + 1)
532 internal_equal (p1->callback, p2->callback, depth + 1)
534 EQ (p1->suffix, p2->suffix)
536 EQ (p1->active, p2->active)
538 EQ (p1->included, p2->included)
540 EQ (p1->config, p2->config)
542 EQ (p1->filter, p2->filter)
544 EQ (p1->style, p2->style)
546 EQ (p1->selected, p2->selected)
548 EQ (p1->accelerator, p2->accelerator)
550 EQ (p1->keys, p2->keys)))
556 print_gui_item (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
558 struct Lisp_Gui_Item *g = XGUI_ITEM (obj);
562 error ("printing unreadable object #<gui-item 0x%x>", g->header.uid);
564 write_c_string ("#<gui-item ", printcharfun);
565 sprintf (buf, "0x%x>", g->header.uid);
566 write_c_string (buf, printcharfun);
569 /* parse a glyph descriptor into a tree of gui items.
571 The gui_item slot of an image instance can be a single item or an
572 arbitrarily nested hierarchy of item lists. */
574 static Lisp_Object parse_gui_item_tree_item (Lisp_Object entry)
576 Lisp_Object ret = entry;
579 ret = gui_parse_item_keywords_no_errors (entry);
581 else if (STRINGP (entry))
583 CHECK_STRING (entry);
586 signal_simple_error ("item must be a vector or a string", entry);
591 Lisp_Object parse_gui_item_tree_children (Lisp_Object list)
593 Lisp_Object rest, ret = Qnil;
595 /* recursively add items to the tree view */
596 LIST_LOOP (rest, list)
599 if (CONSP (XCAR (rest)))
600 sub = parse_gui_item_tree_list (XCAR (rest));
602 sub = parse_gui_item_tree_item (XCAR (rest));
604 ret = Fcons (sub, ret);
606 /* make the order the same as the items we have parsed */
607 return Fnreverse (ret);
610 static Lisp_Object parse_gui_item_tree_list (Lisp_Object list)
614 /* first one can never be a list */
615 ret = parse_gui_item_tree_item (XCAR (list));
616 return Fcons (ret, parse_gui_item_tree_children (XCDR (list)));
619 DEFINE_LRECORD_IMPLEMENTATION ("gui-item", gui_item,
620 mark_gui_item, print_gui_item,
624 struct Lisp_Gui_Item);
629 defkeyword (&Q_active, ":active");
630 defkeyword (&Q_suffix, ":suffix");
631 defkeyword (&Q_keys, ":keys");
632 defkeyword (&Q_key_sequence,":key-sequence");
633 defkeyword (&Q_style, ":style");
634 defkeyword (&Q_selected, ":selected");
635 defkeyword (&Q_filter, ":filter");
636 defkeyword (&Q_config, ":config");
637 defkeyword (&Q_included, ":included");
638 defkeyword (&Q_accelerator, ":accelerator");
639 defkeyword (&Q_label, ":label");
640 defkeyword (&Q_callback, ":callback");
642 defsymbol (&Qtoggle, "toggle");
643 defsymbol (&Qradio, "radio");
646 DEFSUBR (Fpopup_up_p);