XEmacs 21.2.28 "Hermes".
[chise/xemacs-chise.git.1] / src / dialog-x.c
1 /* Implements elisp-programmable dialog boxes -- X interface.
2    Copyright (C) 1993, 1994 Free Software Foundation, Inc.
3    Copyright (C) 1995 Tinker Systems and INS Engineering Corp.
4
5 This file is part of XEmacs.
6
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
11
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING.  If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 /* Synched up with: Not in FSF. */
23
24 #include <config.h>
25 #include "lisp.h"
26
27 #include "console-x.h"
28 #include "EmacsFrame.h"
29 #include "gui-x.h"
30
31 #include "buffer.h"
32 #include "commands.h"           /* zmacs_regions */
33 #include "events.h"
34 #include "frame.h"
35 #include "gui.h"
36 #include "opaque.h"
37 #include "window.h"
38
39
40 static void
41 maybe_run_dbox_text_callback (LWLIB_ID id)
42 {
43   /* !!#### This function has not been Mule-ized */
44   widget_value *wv;
45   int got_some;
46   wv = xmalloc_widget_value ();
47   wv->name = xstrdup ("value");
48   got_some = lw_get_some_values (id, wv);
49   if (got_some)
50     {
51       Lisp_Object text_field_callback;
52       char *text_field_value = wv->value;
53       VOID_TO_LISP (text_field_callback, wv->call_data);
54       if (text_field_value)
55         {
56           void *tmp = LISP_TO_VOID (list2 (text_field_callback,
57                                            build_string (text_field_value)));
58           popup_selection_callback (0, id, (XtPointer) tmp);
59         }
60     }
61   /* This code tried to optimize, newing/freeing. This is generally
62      unsafe so we will alwats strdup and always use
63      free_widget_value_tree. */
64   free_widget_value_tree (wv);
65 }
66
67 static void
68 dbox_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
69 {
70   /* This is called with client_data == -1 when WM_DELETE_WINDOW is sent
71      instead of a button being selected. */
72   struct device *d = get_device_from_display (XtDisplay (widget));
73   struct frame *f = 0;
74   Widget cur_widget = widget;
75
76   /* The parent which is actually connected to our EmacsFrame may be a
77      ways up the tree. */
78   while (!f && cur_widget)
79     {
80       f = x_any_window_to_frame (d, XtWindow (cur_widget));
81       cur_widget = XtParent (cur_widget);
82     }
83
84   if (popup_handled_p (id))
85     return;
86   assert (popup_up_p != 0);
87   ungcpro_popup_callbacks (id);
88   popup_up_p--;
89   maybe_run_dbox_text_callback (id);
90   popup_selection_callback (widget, id, client_data);
91   lw_destroy_all_widgets (id);
92
93   /* The Motif dialog box sets the keyboard focus to itself.  When it
94      goes away we have to take care of getting the focus back
95      ourselves. */
96 #ifdef EXTERNAL_WIDGET
97   /* #### Not sure if this special case is necessary. */
98   if (!FRAME_X_EXTERNAL_WINDOW_P (f) && f)
99 #else
100   if (f)
101 #endif
102     lw_set_keyboard_focus (FRAME_X_SHELL_WIDGET (f), FRAME_X_TEXT_WIDGET (f));
103 }
104
105 static CONST char * CONST button_names [] = {
106   "button1", "button2", "button3", "button4", "button5",
107   "button6", "button7", "button8", "button9", "button10" };
108
109 /* can't have static frame locals because of some broken compilers */
110 static char tmp_dbox_name [255];
111
112 static widget_value *
113 dbox_descriptor_to_widget_value (Lisp_Object desc)
114 {
115   /* !!#### This function has not been Mule-ized */
116   /* This function can GC */
117   char *name;
118   int lbuttons = 0, rbuttons = 0;
119   int partition_seen = 0;
120   int text_field_p = 0;
121   int allow_text_p = 1;
122   widget_value *prev = 0, *kids = 0;
123   int n = 0;
124   int count = specpdl_depth ();
125   Lisp_Object wv_closure, gui_item;
126
127   CHECK_CONS (desc);
128   CHECK_STRING (XCAR (desc));
129   name = (char *) XSTRING_DATA (LISP_GETTEXT (XCAR (desc)));
130   desc = XCDR (desc);
131   if (!CONSP (desc))
132     error ("dialog boxes must have some buttons");
133
134   /* Inhibit GC during this conversion.  The reasons for this are
135      the same as in menu_item_descriptor_to_widget_value(); see
136      the large comment above that function. */
137
138   record_unwind_protect (restore_gc_inhibit,
139                          make_int (gc_currently_forbidden));
140   gc_currently_forbidden = 1;
141
142   kids = prev = xmalloc_widget_value ();
143
144   /* Also make sure that we free the partially-created widget_value
145      tree on Lisp error. */
146
147   wv_closure = make_opaque_ptr (kids);
148   record_unwind_protect (widget_value_unwind, wv_closure);
149   prev->name = xstrdup ("message");
150   prev->value = xstrdup (name);
151   prev->enabled = 1;
152
153   for (; !NILP (desc); desc = Fcdr (desc))
154     {
155       Lisp_Object button = XCAR (desc);
156       widget_value *wv;
157
158       if (NILP (button))
159         {
160           if (partition_seen)
161             error ("more than one partition (nil) seen in dbox spec");
162           partition_seen = 1;
163           continue;
164         }
165       CHECK_VECTOR (button);
166       wv = xmalloc_widget_value ();
167
168       gui_item = gui_parse_item_keywords (button);
169       if (!button_item_to_widget_value (gui_item, wv, allow_text_p, 1))
170         {
171           free_widget_value_tree (wv);
172           continue;
173         }
174
175       if (wv->type == TEXT_TYPE)
176         {
177           text_field_p = 1;
178           allow_text_p = 0;      /* only allow one */
179         }
180       else                      /* it's a button */
181         {
182           allow_text_p = 0;      /* only allow text field at the front */
183           if (wv->value)        xfree (wv->value);
184           wv->value = wv->name; /* what a mess... */
185           wv->name = xstrdup (button_names [n]);
186
187           if (partition_seen)
188             rbuttons++;
189           else
190             lbuttons++;
191           n++;
192
193           if (lbuttons > 9 || rbuttons > 9)
194             error ("too many buttons (9)"); /* #### this leaks */
195         }
196
197       prev->next = wv;
198       prev = wv;
199     }
200
201   if (n == 0)
202     error ("dialog boxes must have some buttons");
203   {
204     char type = (text_field_p ? 'P' : 'Q');
205     widget_value *dbox;
206     sprintf (tmp_dbox_name, "%c%dBR%d", type, lbuttons + rbuttons, rbuttons);
207     dbox = xmalloc_widget_value ();
208     dbox->name = xstrdup (tmp_dbox_name);
209     dbox->contents = kids;
210
211     /* No more need to free the half-filled-in structures. */
212     set_opaque_ptr (wv_closure, 0);
213     unbind_to (count, Qnil);
214     return dbox;
215   }
216 }
217
218 static void
219 x_popup_dialog_box (struct frame* f, Lisp_Object dbox_desc)
220 {
221   int dbox_id;
222   widget_value *data;
223   Widget parent, dbox;
224
225   data = dbox_descriptor_to_widget_value (dbox_desc);
226
227   parent = FRAME_X_SHELL_WIDGET (f);
228
229   dbox_id = new_lwlib_id ();
230   dbox = lw_create_widget (data->name, "dialog", dbox_id, data, parent, 1, 0,
231                            dbox_selection_callback, 0);
232   lw_modify_all_widgets (dbox_id, data, True);
233   lw_modify_all_widgets (dbox_id, data->contents, True);
234   free_popup_widget_value_tree (data);
235
236   gcpro_popup_callbacks (dbox_id);
237
238   /* Setting zmacs-region-stays is necessary here because executing a
239      command from a dialog is really a two-command process: the first
240      command (bound to the button-click) simply pops up the dialog,
241      and returns.  This causes a sequence of magic-events (destined
242      for the dialog widget) to begin.  Eventually, a dialog item is
243      selected, and a misc-user-event blip is pushed onto the end of
244      the input stream, which is then executed by the event loop.
245
246      So there are two command-events, with a bunch of magic-events
247      between them.  We don't want the *first* command event to alter
248      the state of the region, so that the region can be available as
249      an argument for the second command. */
250   if (zmacs_regions)
251     zmacs_region_stays = 1;
252
253   popup_up_p++;
254   lw_pop_up_all_widgets (dbox_id);
255 }
256
257 void
258 syms_of_dialog_x (void)
259 {
260 }
261
262 void
263 console_type_create_dialog_x (void)
264 {
265   CONSOLE_HAS_METHOD (x, popup_dialog_box);
266 }
267
268 void
269 vars_of_dialog_x (void)
270 {
271 #if defined (LWLIB_DIALOGS_LUCID)
272   Fprovide (intern ("lucid-dialogs"));
273 #elif defined (LWLIB_DIALOGS_MOTIF)
274   Fprovide (intern ("motif-dialogs"));
275 #elif defined (LWLIB_DIALOGS_ATHENA)
276   Fprovide (intern ("athena-dialogs"));
277 #endif
278 }