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 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 compatibility */
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 Lisp_Gui_Item *lp = XGUI_ITEM (gui_item);
153 lp->accelerator = Qnil;
157 allocate_gui_item (void)
159 Lisp_Gui_Item *lp = alloc_lcrecord_type (Lisp_Gui_Item, &lrecord_gui_item);
163 XSETGUI_ITEM (val, lp);
171 * ITEM is a lisp vector, describing a menu item or a button. The
172 * function extracts the description of the item into the PGUI_ITEM
176 make_gui_item_from_keywords_internal (Lisp_Object item,
179 int length, plist_p, start;
180 Lisp_Object *contents;
181 Lisp_Object gui_item = allocate_gui_item ();
182 Lisp_Gui_Item* pgui_item = XGUI_ITEM (gui_item);
185 length = XVECTOR_LENGTH (item);
186 contents = XVECTOR_DATA (item);
189 signal_simple_error ("GUI item descriptors must be at least 1 elts long", item);
191 /* length 1: [ "name" ]
192 length 2: [ "name" callback ]
193 length 3: [ "name" callback active-p ]
194 or [ "name" keyword value ]
195 length 4: [ "name" callback active-p suffix ]
196 or [ "name" callback keyword value ]
197 length 5+: [ "name" callback [ keyword value ]+ ]
198 or [ "name" [ keyword value ]+ ]
200 plist_p = (length > 2 && (KEYWORDP (contents [1])
201 || KEYWORDP (contents [2])));
203 pgui_item->name = contents [0];
204 if (length > 1 && !KEYWORDP (contents [1]))
206 pgui_item->callback = contents [1];
212 if (!plist_p && length > 2)
215 pgui_item->active = contents [2];
217 pgui_item->suffix = contents [3];
223 if ((length - start) & 1)
224 signal_simple_error (
225 "GUI item descriptor has an odd number of keywords and values",
228 for (i = start; i < length;)
230 Lisp_Object key = contents [i++];
231 Lisp_Object val = contents [i++];
232 gui_item_add_keyval_pair (gui_item, key, val, errb);
239 gui_parse_item_keywords (Lisp_Object item)
241 return make_gui_item_from_keywords_internal (item, ERROR_ME);
245 gui_parse_item_keywords_no_errors (Lisp_Object item)
247 return make_gui_item_from_keywords_internal (item, ERROR_ME_NOT);
250 /* convert a gui item into plist properties */
252 gui_add_item_keywords_to_plist (Lisp_Object plist, Lisp_Object gui_item)
254 Lisp_Gui_Item* pgui_item = XGUI_ITEM (gui_item);
256 if (!NILP (pgui_item->callback))
257 Fplist_put (plist, Q_callback, pgui_item->callback);
258 if (!NILP (pgui_item->suffix))
259 Fplist_put (plist, Q_suffix, pgui_item->suffix);
260 if (!NILP (pgui_item->active))
261 Fplist_put (plist, Q_active, pgui_item->active);
262 if (!NILP (pgui_item->included))
263 Fplist_put (plist, Q_included, pgui_item->included);
264 if (!NILP (pgui_item->config))
265 Fplist_put (plist, Q_config, pgui_item->config);
266 if (!NILP (pgui_item->filter))
267 Fplist_put (plist, Q_filter, pgui_item->filter);
268 if (!NILP (pgui_item->style))
269 Fplist_put (plist, Q_style, pgui_item->style);
270 if (!NILP (pgui_item->selected))
271 Fplist_put (plist, Q_selected, pgui_item->selected);
272 if (!NILP (pgui_item->keys))
273 Fplist_put (plist, Q_keys, pgui_item->keys);
274 if (!NILP (pgui_item->accelerator))
275 Fplist_put (plist, Q_accelerator, pgui_item->accelerator);
279 * Decide whether a GUI item is active by evaluating its :active form
283 gui_item_active_p (Lisp_Object gui_item)
285 /* This function can call lisp */
287 /* Shortcut to avoid evaluating Qt each time */
288 return (EQ (XGUI_ITEM (gui_item)->active, Qt)
289 || !NILP (Feval (XGUI_ITEM (gui_item)->active)));
292 /* set menu accelerator key to first underlined character in menu name */
294 gui_item_accelerator (Lisp_Object gui_item)
296 Lisp_Gui_Item* pgui = XGUI_ITEM (gui_item);
298 if (!NILP (pgui->accelerator))
299 return pgui->accelerator;
306 gui_name_accelerator (Lisp_Object nm)
308 /* !!#### This function has not been Mule-ized */
309 char* name = (char*)XSTRING_DATA (nm);
316 if (*name=='_' && *(name+1))
318 int accelerator = (int) (unsigned char) (*(name+1));
319 return make_char (tolower (accelerator));
328 * Decide whether a GUI item is selected by evaluating its :selected form
332 gui_item_selected_p (Lisp_Object gui_item)
334 /* This function can call lisp */
336 /* Shortcut to avoid evaluating Qt each time */
337 return (EQ (XGUI_ITEM (gui_item)->selected, Qt)
338 || !NILP (Feval (XGUI_ITEM (gui_item)->selected)));
342 * Decide whether a GUI item is included by evaluating its :included
343 * form if given, and testing its :config form against supplied CONFLIST
344 * configuration variable
347 gui_item_included_p (Lisp_Object gui_item, Lisp_Object conflist)
349 /* This function can call lisp */
350 Lisp_Gui_Item* pgui_item = XGUI_ITEM (gui_item);
352 /* Evaluate :included first. Shortcut to avoid evaluating Qt each time */
353 if (!EQ (pgui_item->included, Qt)
354 && NILP (Feval (pgui_item->included)))
357 /* Do :config if conflist is given */
358 if (!NILP (conflist) && !NILP (pgui_item->config)
359 && NILP (Fmemq (pgui_item->config, conflist)))
366 signal_too_long_error (Lisp_Object name)
368 signal_simple_error ("GUI item produces too long displayable string", name);
371 #ifdef HAVE_WINDOW_SYSTEM
373 * Format "left flush" display portion of an item into BUF, guarded by
374 * maximum buffer size BUF_LEN. BUF_LEN does not count for terminating
375 * null character, so actual maximum size of buffer consumed is
376 * BUF_LEN + 1 bytes. If buffer is not big enough, then error is
378 * Return value is the offset to the terminating null character into the
382 gui_item_display_flush_left (Lisp_Object gui_item,
383 char* buf, Bytecount buf_len)
385 /* This function can call lisp */
388 Lisp_Gui_Item* pgui_item = XGUI_ITEM (gui_item);
390 /* Copy item name first */
391 CHECK_STRING (pgui_item->name);
392 len = XSTRING_LENGTH (pgui_item->name);
394 signal_too_long_error (pgui_item->name);
395 memcpy (p, XSTRING_DATA (pgui_item->name), len);
398 /* Add space and suffix, if there is a suffix.
399 * If suffix is not string evaluate it */
400 if (!NILP (pgui_item->suffix))
402 Lisp_Object suffix = pgui_item->suffix;
403 /* Shortcut to avoid evaluating suffix each time */
404 if (!STRINGP (suffix))
406 suffix = Feval (suffix);
407 CHECK_STRING (suffix);
410 len = XSTRING_LENGTH (suffix);
411 if (p + len + 1 > buf + buf_len)
412 signal_too_long_error (pgui_item->name);
414 memcpy (p, XSTRING_DATA (suffix), len);
422 * Format "right flush" display portion of an item into BUF, guarded by
423 * maximum buffer size BUF_LEN. BUF_LEN does not count for terminating
424 * null character, so actual maximum size of buffer consumed is
425 * BUF_LEN + 1 bytes. If buffer is not big enough, then error is
427 * Return value is the offset to the terminating null character into the
431 gui_item_display_flush_right (Lisp_Object gui_item,
432 char* buf, Bytecount buf_len)
434 Lisp_Gui_Item* pgui_item = XGUI_ITEM (gui_item);
439 if (!menubar_show_keybindings)
443 /* Try :keys first */
444 if (!NILP (pgui_item->keys))
446 CHECK_STRING (pgui_item->keys);
447 if (XSTRING_LENGTH (pgui_item->keys) > buf_len)
448 signal_too_long_error (pgui_item->name);
449 strcpy (buf, (CONST char *) XSTRING_DATA (pgui_item->keys));
450 return XSTRING_LENGTH (pgui_item->keys);
453 /* See if we can derive keys out of callback symbol */
454 if (SYMBOLP (pgui_item->callback))
459 where_is_to_char (pgui_item->callback, buf2);
462 signal_too_long_error (pgui_item->name);
467 /* No keys - no right flush display */
470 #endif /* HAVE_WINDOW_SYSTEM */
473 mark_gui_item (Lisp_Object obj)
475 Lisp_Gui_Item *p = XGUI_ITEM (obj);
477 mark_object (p->name);
478 mark_object (p->callback);
479 mark_object (p->config);
480 mark_object (p->suffix);
481 mark_object (p->active);
482 mark_object (p->included);
483 mark_object (p->config);
484 mark_object (p->filter);
485 mark_object (p->style);
486 mark_object (p->selected);
487 mark_object (p->keys);
488 mark_object (p->accelerator);
494 gui_item_hash (Lisp_Object obj, int depth)
496 Lisp_Gui_Item *p = XGUI_ITEM (obj);
498 return HASH2 (HASH5 (internal_hash (p->name, depth + 1),
499 internal_hash (p->callback, depth + 1),
500 internal_hash (p->suffix, depth + 1),
501 internal_hash (p->active, depth + 1),
502 internal_hash (p->included, depth + 1)),
503 HASH5 (internal_hash (p->config, depth + 1),
504 internal_hash (p->filter, depth + 1),
505 internal_hash (p->style, depth + 1),
506 internal_hash (p->selected, depth + 1),
507 internal_hash (p->keys, depth + 1)));
511 gui_item_id_hash (Lisp_Object hashtable, Lisp_Object gitem, int slot)
513 int hashid = gui_item_hash (gitem, 0);
514 int id = GUI_ITEM_ID_BITS (hashid, slot);
515 while (!NILP (Fgethash (make_int (id),
518 id = GUI_ITEM_ID_BITS (id + 1, slot);
524 gui_item_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
526 Lisp_Gui_Item *p1 = XGUI_ITEM (obj1);
527 Lisp_Gui_Item *p2 = XGUI_ITEM (obj2);
529 if (!(internal_equal (p1->name, p2->name, depth + 1)
531 internal_equal (p1->callback, p2->callback, depth + 1)
533 EQ (p1->suffix, p2->suffix)
535 EQ (p1->active, p2->active)
537 EQ (p1->included, p2->included)
539 EQ (p1->config, p2->config)
541 EQ (p1->filter, p2->filter)
543 EQ (p1->style, p2->style)
545 EQ (p1->selected, p2->selected)
547 EQ (p1->accelerator, p2->accelerator)
549 EQ (p1->keys, p2->keys)))
555 print_gui_item (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
557 Lisp_Gui_Item *g = XGUI_ITEM (obj);
561 error ("printing unreadable object #<gui-item 0x%x>", g->header.uid);
563 write_c_string ("#<gui-item ", printcharfun);
564 sprintf (buf, "0x%x>", g->header.uid);
565 write_c_string (buf, printcharfun);
568 /* parse a glyph descriptor into a tree of gui items.
570 The gui_item slot of an image instance can be a single item or an
571 arbitrarily nested hierarchy of item lists. */
573 static Lisp_Object parse_gui_item_tree_item (Lisp_Object entry)
575 Lisp_Object ret = entry;
578 ret = gui_parse_item_keywords_no_errors (entry);
580 else if (STRINGP (entry))
582 CHECK_STRING (entry);
585 signal_simple_error ("item must be a vector or a string", entry);
590 Lisp_Object parse_gui_item_tree_children (Lisp_Object list)
592 Lisp_Object rest, ret = Qnil;
594 /* recursively add items to the tree view */
595 LIST_LOOP (rest, list)
598 if (CONSP (XCAR (rest)))
599 sub = parse_gui_item_tree_list (XCAR (rest));
601 sub = parse_gui_item_tree_item (XCAR (rest));
603 ret = Fcons (sub, ret);
605 /* make the order the same as the items we have parsed */
606 return Fnreverse (ret);
609 static Lisp_Object parse_gui_item_tree_list (Lisp_Object list)
613 /* first one can never be a list */
614 ret = parse_gui_item_tree_item (XCAR (list));
615 return Fcons (ret, parse_gui_item_tree_children (XCDR (list)));
618 DEFINE_LRECORD_IMPLEMENTATION ("gui-item", gui_item,
619 mark_gui_item, print_gui_item,
628 defkeyword (&Q_active, ":active");
629 defkeyword (&Q_suffix, ":suffix");
630 defkeyword (&Q_keys, ":keys");
631 defkeyword (&Q_key_sequence,":key-sequence");
632 defkeyword (&Q_style, ":style");
633 defkeyword (&Q_selected, ":selected");
634 defkeyword (&Q_filter, ":filter");
635 defkeyword (&Q_config, ":config");
636 defkeyword (&Q_included, ":included");
637 defkeyword (&Q_accelerator, ":accelerator");
638 defkeyword (&Q_label, ":label");
639 defkeyword (&Q_callback, ":callback");
641 defsymbol (&Qtoggle, "toggle");
642 defsymbol (&Qradio, "radio");
645 DEFSUBR (Fpopup_up_p);