XEmacs 21.2.29 "Hestia".
[chise/xemacs-chise.git.1] / src / gui.c
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.
6
7 This file is part of XEmacs.
8
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
12 later version.
13
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
17 for more details.
18
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.  */
23
24 /* Synched up with: Not in FSF. */
25
26 #include <config.h>
27 #include "lisp.h"
28 #include "gui.h"
29 #include "elhash.h"
30 #include "bytecode.h"
31
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;
36
37 static Lisp_Object parse_gui_item_tree_list (Lisp_Object list);
38
39 #ifdef HAVE_POPUPS
40
41 /* count of menus/dboxes currently up */
42 int popup_up_p;
43
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'.
47 */
48        ())
49 {
50   return popup_up_p ? Qt : Qnil;
51 }
52 #endif /* HAVE_POPUPS */
53
54 int
55 separator_string_p (const char *s)
56 {
57   const char *p;
58   char first;
59
60   if (!s || s[0] == '\0')
61     return 0;
62   first = s[0];
63   if (first != '-' && first != '=')
64     return 0;
65   for (p = s; *p == first; p++)
66     ;
67
68   return (*p == '!' || *p == ':' || *p == '\0');
69 }
70
71 /* Massage DATA to find the correct function and argument.  Used by
72    popup_selection_callback() and the msw code. */
73 void
74 get_gui_callback (Lisp_Object data, Lisp_Object *fn, Lisp_Object *arg)
75 {
76   if (SYMBOLP (data)
77       || (COMPILED_FUNCTIONP (data)
78           && XCOMPILED_FUNCTION (data)->flags.interactivep)
79       || (CONSP (data) && (EQ (XCAR (data), Qlambda))
80           && !NILP (Fassq (Qinteractive, Fcdr (Fcdr (data))))))
81     {
82       *fn = Qcall_interactively;
83       *arg = data;
84     }
85   else if (CONSP (data))
86     {
87       *fn = Qeval;
88       *arg = data;
89     }
90   else
91     {
92       *fn = Qeval;
93       *arg = list3 (Qsignal,
94                     list2 (Qquote, Qerror),
95                     list2 (Qquote, list2 (build_translated_string
96                                           ("illegal callback"),
97                                           data)));
98     }
99 }
100
101 /*
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
104  * error is signaled.
105  */
106 void
107 gui_item_add_keyval_pair (Lisp_Object gui_item,
108                           Lisp_Object key, Lisp_Object val,
109                           Error_behavior errb)
110 {
111   Lisp_Gui_Item* pgui_item = XGUI_ITEM (gui_item);
112
113   if (!KEYWORDP (key))
114     signal_simple_error_2 ("Non-keyword in gui item", key, pgui_item->name);
115
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))
128     {
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);
133     }
134   else if (ERRB_EQ (errb, ERROR_ME))
135     signal_simple_error_2 ("Unknown keyword in gui item", key, pgui_item->name);
136 }
137
138 void
139 gui_item_init (Lisp_Object gui_item)
140 {
141   Lisp_Gui_Item *lp = XGUI_ITEM (gui_item);
142
143   lp->name     = Qnil;
144   lp->callback = Qnil;
145   lp->suffix   = Qnil;
146   lp->active   = Qt;
147   lp->included = Qt;
148   lp->config   = Qnil;
149   lp->filter   = Qnil;
150   lp->style    = Qnil;
151   lp->selected = Qnil;
152   lp->keys     = Qnil;
153   lp->accelerator     = Qnil;
154 }
155
156 Lisp_Object
157 allocate_gui_item (void)
158 {
159   Lisp_Gui_Item *lp = alloc_lcrecord_type (Lisp_Gui_Item, &lrecord_gui_item);
160   Lisp_Object val;
161
162   zero_lcrecord (lp);
163   XSETGUI_ITEM (val, lp);
164
165   gui_item_init (val);
166
167   return val;
168 }
169
170 /*
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
173  * structure.
174  */
175 static Lisp_Object
176 make_gui_item_from_keywords_internal (Lisp_Object item,
177                                       Error_behavior errb)
178 {
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);
183
184   CHECK_VECTOR (item);
185   length = XVECTOR_LENGTH (item);
186   contents = XVECTOR_DATA (item);
187
188   if (length < 1)
189     signal_simple_error ("GUI item descriptors must be at least 1 elts long", item);
190
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 ]+ ]
199   */
200   plist_p = (length > 2 && (KEYWORDP (contents [1])
201                             || KEYWORDP (contents [2])));
202
203   pgui_item->name = contents [0];
204   if (length > 1 && !KEYWORDP (contents [1]))
205     {
206       pgui_item->callback = contents [1];
207       start = 2;
208     }
209   else
210     start =1;
211
212   if (!plist_p && length > 2)
213     /* the old way */
214     {
215       pgui_item->active = contents [2];
216       if (length == 4)
217         pgui_item->suffix = contents [3];
218     }
219   else
220     /* the new way */
221     {
222       int i;
223       if ((length - start) & 1)
224         signal_simple_error (
225                 "GUI item descriptor has an odd number of keywords and values",
226                              item);
227
228       for (i = start; i < length;)
229         {
230           Lisp_Object key = contents [i++];
231           Lisp_Object val = contents [i++];
232           gui_item_add_keyval_pair (gui_item, key, val, errb);
233         }
234     }
235   return gui_item;
236 }
237
238 Lisp_Object
239 gui_parse_item_keywords (Lisp_Object item)
240 {
241   return make_gui_item_from_keywords_internal (item, ERROR_ME);
242 }
243
244 Lisp_Object
245 gui_parse_item_keywords_no_errors (Lisp_Object item)
246 {
247   return make_gui_item_from_keywords_internal (item, ERROR_ME_NOT);
248 }
249
250 /* convert a gui item into plist properties */
251 void
252 gui_add_item_keywords_to_plist (Lisp_Object plist, Lisp_Object gui_item)
253 {
254   Lisp_Gui_Item* pgui_item = XGUI_ITEM (gui_item);
255
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);
276 }
277
278 /*
279  * Decide whether a GUI item is active by evaluating its :active form
280  * if any
281  */
282 int
283 gui_item_active_p (Lisp_Object gui_item)
284 {
285   /* This function can call lisp */
286
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)));
290 }
291
292 /* set menu accelerator key to first underlined character in menu name */
293 Lisp_Object
294 gui_item_accelerator (Lisp_Object gui_item)
295 {
296   Lisp_Gui_Item* pgui = XGUI_ITEM (gui_item);
297
298   if (!NILP (pgui->accelerator))
299     return pgui->accelerator;
300
301   else
302     return gui_name_accelerator (pgui->name);
303 }
304
305 Lisp_Object
306 gui_name_accelerator (Lisp_Object nm)
307 {
308   /* !!#### This function has not been Mule-ized */
309   char* name = (char*)XSTRING_DATA (nm);
310
311   while (*name) {
312     if (*name=='%') {
313       ++name;
314       if (!(*name))
315         return Qnil;
316       if (*name=='_' && *(name+1))
317         {
318           int accelerator = (int) (unsigned char) (*(name+1));
319           return make_char (tolower (accelerator));
320         }
321     }
322     ++name;
323   }
324   return Qnil;
325 }
326
327 /*
328  * Decide whether a GUI item is selected by evaluating its :selected form
329  * if any
330  */
331 int
332 gui_item_selected_p (Lisp_Object gui_item)
333 {
334   /* This function can call lisp */
335
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)));
339 }
340
341 /*
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
345  */
346 int
347 gui_item_included_p (Lisp_Object gui_item, Lisp_Object conflist)
348 {
349   /* This function can call lisp */
350   Lisp_Gui_Item* pgui_item = XGUI_ITEM (gui_item);
351
352   /* Evaluate :included first. Shortcut to avoid evaluating Qt each time */
353   if (!EQ (pgui_item->included, Qt)
354       && NILP (Feval (pgui_item->included)))
355     return 0;
356
357   /* Do :config if conflist is given */
358   if (!NILP (conflist) && !NILP (pgui_item->config)
359       && NILP (Fmemq (pgui_item->config, conflist)))
360     return 0;
361
362   return 1;
363 }
364
365 static DOESNT_RETURN
366 signal_too_long_error (Lisp_Object name)
367 {
368   signal_simple_error ("GUI item produces too long displayable string", name);
369 }
370
371 #ifdef HAVE_WINDOW_SYSTEM
372 /*
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
377  * signaled.
378  * Return value is the offset to the terminating null character into the
379  * buffer.
380  */
381 unsigned int
382 gui_item_display_flush_left  (Lisp_Object gui_item,
383                               char* buf, Bytecount buf_len)
384 {
385   /* This function can call lisp */
386   char *p = buf;
387   Bytecount len;
388   Lisp_Gui_Item* pgui_item = XGUI_ITEM (gui_item);
389
390   /* Copy item name first */
391   CHECK_STRING (pgui_item->name);
392   len = XSTRING_LENGTH (pgui_item->name);
393   if (len > buf_len)
394     signal_too_long_error (pgui_item->name);
395   memcpy (p, XSTRING_DATA (pgui_item->name), len);
396   p += len;
397
398   /* Add space and suffix, if there is a suffix.
399    * If suffix is not string evaluate it */
400   if (!NILP (pgui_item->suffix))
401     {
402       Lisp_Object suffix = pgui_item->suffix;
403       /* Shortcut to avoid evaluating suffix each time */
404       if (!STRINGP (suffix))
405         {
406           suffix = Feval (suffix);
407           CHECK_STRING (suffix);
408         }
409
410       len = XSTRING_LENGTH (suffix);
411       if (p + len + 1 > buf + buf_len)
412         signal_too_long_error (pgui_item->name);
413       *(p++) = ' ';
414       memcpy (p, XSTRING_DATA (suffix), len);
415       p += len;
416     }
417   *p = '\0';
418   return p - buf;
419 }
420
421 /*
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
426  * signaled.
427  * Return value is the offset to the terminating null character into the
428  * buffer.
429  */
430 unsigned int
431 gui_item_display_flush_right (Lisp_Object gui_item,
432                               char* buf, Bytecount buf_len)
433 {
434   Lisp_Gui_Item* pgui_item = XGUI_ITEM (gui_item);
435   *buf = 0;
436
437 #ifdef HAVE_MENUBARS
438   /* Have keys? */
439   if (!menubar_show_keybindings)
440     return 0;
441 #endif
442
443   /* Try :keys first */
444   if (!NILP (pgui_item->keys))
445     {
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);
451     }
452
453   /* See if we can derive keys out of callback symbol */
454   if (SYMBOLP (pgui_item->callback))
455     {
456       char buf2 [1024];
457       Bytecount len;
458
459       where_is_to_char (pgui_item->callback, buf2);
460       len = strlen (buf2);
461       if (len > buf_len)
462         signal_too_long_error (pgui_item->name);
463       strcpy (buf, buf2);
464       return len;
465     }
466
467   /* No keys - no right flush display */
468   return 0;
469 }
470 #endif /* HAVE_WINDOW_SYSTEM */
471
472 static Lisp_Object
473 mark_gui_item (Lisp_Object obj)
474 {
475   Lisp_Gui_Item *p = XGUI_ITEM (obj);
476
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);
489
490   return Qnil;
491 }
492
493 static unsigned long
494 gui_item_hash (Lisp_Object obj, int depth)
495 {
496   Lisp_Gui_Item *p = XGUI_ITEM (obj);
497
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)));
508 }
509
510 int
511 gui_item_id_hash (Lisp_Object hashtable, Lisp_Object gitem, int slot)
512 {
513   int hashid = gui_item_hash (gitem, 0);
514   int id = GUI_ITEM_ID_BITS (hashid, slot);
515   while (!NILP (Fgethash (make_int (id),
516                           hashtable, Qnil)))
517     {
518       id = GUI_ITEM_ID_BITS (id + 1, slot);
519     }
520   return id;
521 }
522
523 static int
524 gui_item_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
525 {
526   Lisp_Gui_Item *p1 = XGUI_ITEM (obj1);
527   Lisp_Gui_Item *p2 = XGUI_ITEM (obj2);
528
529   if (!(internal_equal (p1->name, p2->name, depth + 1)
530         &&
531         internal_equal (p1->callback, p2->callback, depth + 1)
532         &&
533         EQ (p1->suffix, p2->suffix)
534         &&
535         EQ (p1->active, p2->active)
536         &&
537         EQ (p1->included, p2->included)
538         &&
539         EQ (p1->config, p2->config)
540         &&
541         EQ (p1->filter, p2->filter)
542         &&
543         EQ (p1->style, p2->style)
544         &&
545         EQ (p1->selected, p2->selected)
546         &&
547         EQ (p1->accelerator, p2->accelerator)
548         &&
549         EQ (p1->keys, p2->keys)))
550     return 0;
551   return 1;
552 }
553
554 static void
555 print_gui_item (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
556 {
557   Lisp_Gui_Item *g = XGUI_ITEM (obj);
558   char buf[20];
559
560   if (print_readably)
561     error ("printing unreadable object #<gui-item 0x%x>", g->header.uid);
562
563   write_c_string ("#<gui-item ", printcharfun);
564   sprintf (buf, "0x%x>", g->header.uid);
565   write_c_string (buf, printcharfun);
566 }
567
568 /* parse a glyph descriptor into a tree of gui items.
569
570    The gui_item slot of an image instance can be a single item or an
571    arbitrarily nested hierarchy of item lists. */
572
573 static Lisp_Object parse_gui_item_tree_item (Lisp_Object entry)
574 {
575   Lisp_Object ret = entry;
576   if (VECTORP (entry))
577     {
578       ret =  gui_parse_item_keywords_no_errors (entry);
579     }
580   else if (STRINGP (entry))
581     {
582       CHECK_STRING (entry);
583     }
584   else
585     signal_simple_error ("item must be a vector or a string", entry);
586
587   return ret;
588 }
589
590 Lisp_Object parse_gui_item_tree_children (Lisp_Object list)
591 {
592   Lisp_Object rest, ret = Qnil;
593   CHECK_CONS (list);
594   /* recursively add items to the tree view */
595   LIST_LOOP (rest, list)
596     {
597       Lisp_Object sub;
598       if (CONSP (XCAR (rest)))
599         sub = parse_gui_item_tree_list (XCAR (rest));
600       else
601         sub = parse_gui_item_tree_item (XCAR (rest));
602
603       ret = Fcons (sub, ret);
604     }
605   /* make the order the same as the items we have parsed */
606   return Fnreverse (ret);
607 }
608
609 static Lisp_Object parse_gui_item_tree_list (Lisp_Object list)
610 {
611   Lisp_Object ret;
612   CHECK_CONS (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)));
616 }
617
618 DEFINE_LRECORD_IMPLEMENTATION ("gui-item", gui_item,
619                                mark_gui_item, print_gui_item,
620                                0, gui_item_equal,
621                                gui_item_hash,
622                                0,
623                                Lisp_Gui_Item);
624
625 void
626 syms_of_gui (void)
627 {
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");
640
641   defsymbol (&Qtoggle, "toggle");
642   defsymbol (&Qradio, "radio");
643
644 #ifdef HAVE_POPUPS
645   DEFSUBR (Fpopup_up_p);
646 #endif
647 }
648
649 void
650 vars_of_gui (void)
651 {
652 }