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. */
33 Lisp_Object Q_active, Q_suffix, Q_keys, Q_style, Q_selected;
34 Lisp_Object Q_filter, Q_config, Q_included, Q_key_sequence;
35 Lisp_Object Q_accelerator, Q_label, Q_callback;
36 Lisp_Object Qtoggle, Qradio;
38 static Lisp_Object parse_gui_item_tree_list (Lisp_Object list);
42 /* count of menus/dboxes currently up */
45 DEFUN ("popup-up-p", Fpopup_up_p, 0, 0, 0, /*
46 Return t if a popup menu or dialog box is up, nil otherwise.
47 See `popup-menu' and `popup-dialog-box'.
51 return popup_up_p ? Qt : Qnil;
53 #endif /* HAVE_POPUPS */
56 separator_string_p (const char *s)
61 if (!s || s[0] == '\0')
64 if (first != '-' && first != '=')
66 for (p = s; *p == first; p++)
69 return (*p == '!' || *p == ':' || *p == '\0');
72 /* Massage DATA to find the correct function and argument. Used by
73 popup_selection_callback() and the msw code. */
75 get_gui_callback (Lisp_Object data, Lisp_Object *fn, Lisp_Object *arg)
78 || (COMPILED_FUNCTIONP (data)
79 && XCOMPILED_FUNCTION (data)->flags.interactivep)
80 || (CONSP (data) && (EQ (XCAR (data), Qlambda))
81 && !NILP (Fassq (Qinteractive, Fcdr (Fcdr (data))))))
83 *fn = Qcall_interactively;
86 else if (CONSP (data))
94 *arg = list3 (Qsignal,
95 list2 (Qquote, Qerror),
96 list2 (Qquote, list2 (build_translated_string
103 * Add a value VAL associated with keyword KEY into PGUI_ITEM
104 * structure. If KEY is not a keyword, or is an unknown keyword, then
108 gui_item_add_keyval_pair (Lisp_Object gui_item,
109 Lisp_Object key, Lisp_Object val,
112 Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);
115 signal_simple_error_2 ("Non-keyword in gui item", key, pgui_item->name);
117 if (EQ (key, Q_suffix)) pgui_item->suffix = val;
118 else if (EQ (key, Q_active)) pgui_item->active = val;
119 else if (EQ (key, Q_included)) pgui_item->included = val;
120 else if (EQ (key, Q_config)) pgui_item->config = val;
121 else if (EQ (key, Q_filter)) pgui_item->filter = val;
122 else if (EQ (key, Q_style)) pgui_item->style = val;
123 else if (EQ (key, Q_selected)) pgui_item->selected = val;
124 else if (EQ (key, Q_keys)) pgui_item->keys = val;
125 else if (EQ (key, Q_callback)) pgui_item->callback = val;
126 else if (EQ (key, Q_key_sequence)) ; /* ignored for FSF compatibility */
127 else if (EQ (key, Q_label)) ; /* ignored for 21.0 implement in 21.2 */
128 else if (EQ (key, Q_accelerator))
130 if (SYMBOLP (val) || CHARP (val))
131 pgui_item->accelerator = val;
132 else if (ERRB_EQ (errb, ERROR_ME))
133 signal_simple_error ("Bad keyboard accelerator", val);
135 else if (ERRB_EQ (errb, ERROR_ME))
136 signal_simple_error_2 ("Unknown keyword in gui item", key,
141 gui_item_init (Lisp_Object gui_item)
143 Lisp_Gui_Item *lp = XGUI_ITEM (gui_item);
155 lp->accelerator = Qnil;
159 allocate_gui_item (void)
161 Lisp_Gui_Item *lp = alloc_lcrecord_type (Lisp_Gui_Item, &lrecord_gui_item);
165 XSETGUI_ITEM (val, lp);
173 * ITEM is a lisp vector, describing a menu item or a button. The
174 * function extracts the description of the item into the PGUI_ITEM
178 make_gui_item_from_keywords_internal (Lisp_Object item,
181 int length, plist_p, start;
182 Lisp_Object *contents;
183 Lisp_Object gui_item = allocate_gui_item ();
184 Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);
187 length = XVECTOR_LENGTH (item);
188 contents = XVECTOR_DATA (item);
191 signal_simple_error ("GUI item descriptors must be at least 1 elts long", item);
193 /* length 1: [ "name" ]
194 length 2: [ "name" callback ]
195 length 3: [ "name" callback active-p ]
196 or [ "name" keyword value ]
197 length 4: [ "name" callback active-p suffix ]
198 or [ "name" callback keyword value ]
199 length 5+: [ "name" callback [ keyword value ]+ ]
200 or [ "name" [ keyword value ]+ ]
202 plist_p = (length > 2 && (KEYWORDP (contents [1])
203 || KEYWORDP (contents [2])));
205 pgui_item->name = contents [0];
206 if (length > 1 && !KEYWORDP (contents [1]))
208 pgui_item->callback = contents [1];
214 if (!plist_p && length > 2)
217 pgui_item->active = contents [2];
219 pgui_item->suffix = contents [3];
225 if ((length - start) & 1)
226 signal_simple_error (
227 "GUI item descriptor has an odd number of keywords and values",
230 for (i = start; i < length;)
232 Lisp_Object key = contents [i++];
233 Lisp_Object val = contents [i++];
234 gui_item_add_keyval_pair (gui_item, key, val, errb);
241 gui_parse_item_keywords (Lisp_Object item)
243 return make_gui_item_from_keywords_internal (item, ERROR_ME);
247 gui_parse_item_keywords_no_errors (Lisp_Object item)
249 return make_gui_item_from_keywords_internal (item, ERROR_ME_NOT);
252 /* convert a gui item into plist properties */
254 gui_add_item_keywords_to_plist (Lisp_Object plist, Lisp_Object gui_item)
256 Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);
258 if (!NILP (pgui_item->callback))
259 Fplist_put (plist, Q_callback, pgui_item->callback);
260 if (!NILP (pgui_item->suffix))
261 Fplist_put (plist, Q_suffix, pgui_item->suffix);
262 if (!NILP (pgui_item->active))
263 Fplist_put (plist, Q_active, pgui_item->active);
264 if (!NILP (pgui_item->included))
265 Fplist_put (plist, Q_included, pgui_item->included);
266 if (!NILP (pgui_item->config))
267 Fplist_put (plist, Q_config, pgui_item->config);
268 if (!NILP (pgui_item->filter))
269 Fplist_put (plist, Q_filter, pgui_item->filter);
270 if (!NILP (pgui_item->style))
271 Fplist_put (plist, Q_style, pgui_item->style);
272 if (!NILP (pgui_item->selected))
273 Fplist_put (plist, Q_selected, pgui_item->selected);
274 if (!NILP (pgui_item->keys))
275 Fplist_put (plist, Q_keys, pgui_item->keys);
276 if (!NILP (pgui_item->accelerator))
277 Fplist_put (plist, Q_accelerator, pgui_item->accelerator);
281 * Decide whether a GUI item is active by evaluating its :active form
285 gui_item_active_p (Lisp_Object gui_item)
287 /* This function can call lisp */
289 /* Shortcut to avoid evaluating Qt each time */
290 return (EQ (XGUI_ITEM (gui_item)->active, Qt)
291 || !NILP (Feval (XGUI_ITEM (gui_item)->active)));
294 /* set menu accelerator key to first underlined character in menu name */
296 gui_item_accelerator (Lisp_Object gui_item)
298 Lisp_Gui_Item *pgui = XGUI_ITEM (gui_item);
300 if (!NILP (pgui->accelerator))
301 return pgui->accelerator;
304 return gui_name_accelerator (pgui->name);
308 gui_name_accelerator (Lisp_Object nm)
310 Bufbyte *name = XSTRING_DATA (nm);
319 if (*name == '_' && *(name + 1))
321 Emchar accelerator = charptr_emchar (name + 1);
322 /* #### bogus current_buffer dependency */
323 return make_char (DOWNCASE (current_buffer, accelerator));
328 return make_char (DOWNCASE (current_buffer,
329 charptr_emchar (XSTRING_DATA (nm))));
333 * Decide whether a GUI item is selected by evaluating its :selected form
337 gui_item_selected_p (Lisp_Object gui_item)
339 /* This function can call lisp */
341 /* Shortcut to avoid evaluating Qt each time */
342 return (EQ (XGUI_ITEM (gui_item)->selected, Qt)
343 || !NILP (Feval (XGUI_ITEM (gui_item)->selected)));
347 * Decide whether a GUI item is included by evaluating its :included
348 * form if given, and testing its :config form against supplied CONFLIST
349 * configuration variable
352 gui_item_included_p (Lisp_Object gui_item, Lisp_Object conflist)
354 /* This function can call lisp */
355 Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);
357 /* Evaluate :included first. Shortcut to avoid evaluating Qt each time */
358 if (!EQ (pgui_item->included, Qt)
359 && NILP (Feval (pgui_item->included)))
362 /* Do :config if conflist is given */
363 if (!NILP (conflist) && !NILP (pgui_item->config)
364 && NILP (Fmemq (pgui_item->config, conflist)))
371 signal_too_long_error (Lisp_Object name)
373 signal_simple_error ("GUI item produces too long displayable string", name);
376 #ifdef HAVE_WINDOW_SYSTEM
378 * Format "left flush" display portion of an item into BUF, guarded by
379 * maximum buffer size BUF_LEN. BUF_LEN does not count for terminating
380 * null character, so actual maximum size of buffer consumed is
381 * BUF_LEN + 1 bytes. If buffer is not big enough, then error is
383 * Return value is the offset to the terminating null character into the
387 gui_item_display_flush_left (Lisp_Object gui_item,
388 char *buf, Bytecount buf_len)
390 /* This function can call lisp */
393 Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);
395 /* Copy item name first */
396 CHECK_STRING (pgui_item->name);
397 len = XSTRING_LENGTH (pgui_item->name);
399 signal_too_long_error (pgui_item->name);
400 memcpy (p, XSTRING_DATA (pgui_item->name), len);
403 /* Add space and suffix, if there is a suffix.
404 * If suffix is not string evaluate it */
405 if (!NILP (pgui_item->suffix))
407 Lisp_Object suffix = pgui_item->suffix;
408 /* Shortcut to avoid evaluating suffix each time */
409 if (!STRINGP (suffix))
411 suffix = Feval (suffix);
412 CHECK_STRING (suffix);
415 len = XSTRING_LENGTH (suffix);
416 if (p + len + 1 > buf + buf_len)
417 signal_too_long_error (pgui_item->name);
419 memcpy (p, XSTRING_DATA (suffix), len);
427 * Format "right flush" display portion of an item into BUF, guarded by
428 * maximum buffer size BUF_LEN. BUF_LEN does not count for terminating
429 * null character, so actual maximum size of buffer consumed is
430 * BUF_LEN + 1 bytes. If buffer is not big enough, then error is
432 * Return value is the offset to the terminating null character into the
436 gui_item_display_flush_right (Lisp_Object gui_item,
437 char *buf, Bytecount buf_len)
439 Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);
444 if (!menubar_show_keybindings)
448 /* Try :keys first */
449 if (!NILP (pgui_item->keys))
451 CHECK_STRING (pgui_item->keys);
452 if (XSTRING_LENGTH (pgui_item->keys) + 1 > buf_len)
453 signal_too_long_error (pgui_item->name);
454 memcpy (buf, XSTRING_DATA (pgui_item->keys),
455 XSTRING_LENGTH (pgui_item->keys) + 1);
456 return XSTRING_LENGTH (pgui_item->keys);
459 /* See if we can derive keys out of callback symbol */
460 if (SYMBOLP (pgui_item->callback))
462 char buf2[1024]; /* #### */
465 where_is_to_char (pgui_item->callback, buf2);
468 signal_too_long_error (pgui_item->name);
473 /* No keys - no right flush display */
476 #endif /* HAVE_WINDOW_SYSTEM */
479 mark_gui_item (Lisp_Object obj)
481 Lisp_Gui_Item *p = XGUI_ITEM (obj);
483 mark_object (p->name);
484 mark_object (p->callback);
485 mark_object (p->config);
486 mark_object (p->suffix);
487 mark_object (p->active);
488 mark_object (p->included);
489 mark_object (p->config);
490 mark_object (p->filter);
491 mark_object (p->style);
492 mark_object (p->selected);
493 mark_object (p->keys);
494 mark_object (p->accelerator);
500 gui_item_hash_internal (Lisp_Object obj, int depth)
502 Lisp_Gui_Item *p = XGUI_ITEM (obj);
504 return HASH2 (HASH5 (internal_hash (p->name, depth + 1),
505 internal_hash (p->callback, depth + 1),
506 internal_hash (p->suffix, depth + 1),
507 internal_hash (p->active, depth + 1),
508 internal_hash (p->included, depth + 1)),
509 HASH5 (internal_hash (p->config, depth + 1),
510 internal_hash (p->filter, depth + 1),
511 internal_hash (p->style, depth + 1),
512 internal_hash (p->selected, depth + 1),
513 internal_hash (p->keys, depth + 1)));
517 gui_item_hash (Lisp_Object obj, int depth)
519 Lisp_Gui_Item *p = XGUI_ITEM (obj);
521 /* Note that this evaluates the active and selected slots so that
522 the hash changes when the result of these changes. */
523 return HASH2 (HASH5 (internal_hash (p->name, depth + 1),
524 internal_hash (p->callback, depth + 1),
525 internal_hash (p->suffix, depth + 1),
526 gui_item_active_p (obj),
527 internal_hash (p->included, depth + 1)),
528 HASH5 (internal_hash (p->config, depth + 1),
529 internal_hash (p->filter, depth + 1),
530 internal_hash (p->style, depth + 1),
531 gui_item_selected_p (obj),
532 internal_hash (p->keys, depth + 1)));
536 gui_item_id_hash (Lisp_Object hashtable, Lisp_Object gitem, int slot)
538 int hashid = gui_item_hash_internal (gitem, 0);
539 int id = GUI_ITEM_ID_BITS (hashid, slot);
540 while (!NILP (Fgethash (make_int (id),
543 id = GUI_ITEM_ID_BITS (id + 1, slot);
549 gui_item_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
551 Lisp_Gui_Item *p1 = XGUI_ITEM (obj1);
552 Lisp_Gui_Item *p2 = XGUI_ITEM (obj2);
554 if (!(internal_equal (p1->name, p2->name, depth + 1)
556 internal_equal (p1->callback, p2->callback, depth + 1)
558 EQ (p1->suffix, p2->suffix)
560 EQ (p1->active, p2->active)
562 EQ (p1->included, p2->included)
564 EQ (p1->config, p2->config)
566 EQ (p1->filter, p2->filter)
568 EQ (p1->style, p2->style)
570 EQ (p1->selected, p2->selected)
572 EQ (p1->accelerator, p2->accelerator)
574 EQ (p1->keys, p2->keys)))
580 print_gui_item (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
582 Lisp_Gui_Item *g = XGUI_ITEM (obj);
586 error ("printing unreadable object #<gui-item 0x%x>", g->header.uid);
588 write_c_string ("#<gui-item ", printcharfun);
589 sprintf (buf, "0x%x>", g->header.uid);
590 write_c_string (buf, printcharfun);
593 /* parse a glyph descriptor into a tree of gui items.
595 The gui_item slot of an image instance can be a single item or an
596 arbitrarily nested hierarchy of item lists. */
599 parse_gui_item_tree_item (Lisp_Object entry)
601 Lisp_Object ret = entry;
608 ret = gui_parse_item_keywords_no_errors (entry);
610 else if (STRINGP (entry))
612 CHECK_STRING (entry);
615 signal_simple_error ("item must be a vector or a string", entry);
617 RETURN_UNGCPRO (ret);
621 parse_gui_item_tree_children (Lisp_Object list)
623 Lisp_Object rest, ret = Qnil, sub = Qnil;
624 struct gcpro gcpro1, gcpro2;
628 /* recursively add items to the tree view */
629 LIST_LOOP (rest, list)
631 if (CONSP (XCAR (rest)))
632 sub = parse_gui_item_tree_list (XCAR (rest));
634 sub = parse_gui_item_tree_item (XCAR (rest));
636 ret = Fcons (sub, ret);
638 /* make the order the same as the items we have parsed */
639 RETURN_UNGCPRO (Fnreverse (ret));
643 parse_gui_item_tree_list (Lisp_Object list)
648 /* first one can never be a list */
649 ret = parse_gui_item_tree_item (XCAR (list));
651 ret = Fcons (ret, parse_gui_item_tree_children (XCDR (list)));
652 RETURN_UNGCPRO (ret);
656 finalize_gui_item (void* header, int for_disksave)
660 DEFINE_LRECORD_IMPLEMENTATION ("gui-item", gui_item,
661 mark_gui_item, print_gui_item,
662 finalize_gui_item, gui_item_equal,
670 INIT_LRECORD_IMPLEMENTATION (gui_item);
672 defkeyword (&Q_active, ":active");
673 defkeyword (&Q_suffix, ":suffix");
674 defkeyword (&Q_keys, ":keys");
675 defkeyword (&Q_key_sequence,":key-sequence");
676 defkeyword (&Q_style, ":style");
677 defkeyword (&Q_selected, ":selected");
678 defkeyword (&Q_filter, ":filter");
679 defkeyword (&Q_config, ":config");
680 defkeyword (&Q_included, ":included");
681 defkeyword (&Q_accelerator, ":accelerator");
682 defkeyword (&Q_label, ":label");
683 defkeyword (&Q_callback, ":callback");
685 defsymbol (&Qtoggle, "toggle");
686 defsymbol (&Qradio, "radio");
689 DEFSUBR (Fpopup_up_p);