XEmacs 21.2-b1
[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 "EmacsManager.h"
29 #include "EmacsFrame.h"
30 #include "EmacsShell.h"
31 #include "gui-x.h"
32
33 #include "buffer.h"
34 #include "commands.h"           /* zmacs_regions */
35 #include "events.h"
36 #include "frame.h"
37 #include "gui.h"
38 #include "opaque.h"
39 #include "window.h"
40
41
42 static void
43 maybe_run_dbox_text_callback (LWLIB_ID id)
44 {
45   /* !!#### This function has not been Mule-ized */
46   widget_value *wv;
47   int got_some;
48   wv = xmalloc_widget_value ();
49   wv->name = (char *) "value";
50   got_some = lw_get_some_values (id, wv);
51   if (got_some)
52     {
53       Lisp_Object text_field_callback;
54       char *text_field_value = wv->value;
55       VOID_TO_LISP (text_field_callback, wv->call_data);
56       if (text_field_value)
57         {
58           void *tmp = LISP_TO_VOID (list2 (text_field_callback,
59                                            build_string (text_field_value)));
60           popup_selection_callback (0, id, (XtPointer) tmp);
61           xfree (text_field_value);
62         }
63     }
64   free_widget_value (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;
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 = (char *) "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       if (!button_item_to_widget_value (button, wv, allow_text_p, 1))
169         {
170           free_widget_value (wv);
171           continue;
172         }
173
174       if (wv->type == TEXT_TYPE)
175         {
176           text_field_p = 1;
177           allow_text_p = 0;      /* only allow one */
178         }
179       else                      /* it's a button */
180         {
181           allow_text_p = 0;      /* only allow text field at the front */
182           wv->value = xstrdup (wv->name);       /* what a mess... */
183           wv->name = (char *) button_names [n];
184
185           if (partition_seen)
186             rbuttons++;
187           else
188             lbuttons++;
189           n++;
190
191           if (lbuttons > 9 || rbuttons > 9)
192             error ("too many buttons (9)"); /* #### this leaks */
193         }
194
195       prev->next = wv;
196       prev = wv;
197     }
198
199   if (n == 0)
200     error ("dialog boxes must have some buttons");
201   {
202     char type = (text_field_p ? 'P' : 'Q');
203     widget_value *dbox;
204     sprintf (tmp_dbox_name, "%c%dBR%d", type, lbuttons + rbuttons, rbuttons);
205     dbox = xmalloc_widget_value ();
206     dbox->name = tmp_dbox_name;
207     dbox->contents = kids;
208
209     /* No more need to free the half-filled-in structures. */
210     set_opaque_ptr (wv_closure, 0);
211     unbind_to (count, Qnil);
212     return dbox;
213   }
214 }
215
216 static void
217 x_popup_dialog_box (struct frame* f, Lisp_Object dbox_desc)
218 {
219   int dbox_id;
220   widget_value *data;
221   Widget parent, dbox;
222
223   data = dbox_descriptor_to_widget_value (dbox_desc);
224
225   parent = FRAME_X_SHELL_WIDGET (f);
226
227   dbox_id = new_lwlib_id ();
228   dbox = lw_create_widget (data->name, "dialog", dbox_id, data, parent, 1, 0,
229                            dbox_selection_callback, 0);
230   lw_modify_all_widgets (dbox_id, data, True);
231   lw_modify_all_widgets (dbox_id, data->contents, True);
232   free_popup_widget_value_tree (data);
233
234   gcpro_popup_callbacks (dbox_id);
235
236   /* Setting zmacs-region-stays is necessary here because executing a
237      command from a dialog is really a two-command process: the first
238      command (bound to the button-click) simply pops up the dialog,
239      and returns.  This causes a sequence of magic-events (destined
240      for the dialog widget) to begin.  Eventually, a dialog item is
241      selected, and a misc-user-event blip is pushed onto the end of
242      the input stream, which is then executed by the event loop.
243
244      So there are two command-events, with a bunch of magic-events
245      between them.  We don't want the *first* command event to alter
246      the state of the region, so that the region can be available as
247      an argument for the second command. */
248   if (zmacs_regions)
249     zmacs_region_stays = 1;
250
251   popup_up_p++;
252   lw_pop_up_all_widgets (dbox_id);
253 }
254
255 void
256 syms_of_dialog_x (void)
257 {
258 }
259
260 void
261 console_type_create_dialog_x (void)
262 {
263   CONSOLE_HAS_METHOD (x, popup_dialog_box);
264 }
265
266 void
267 vars_of_dialog_x (void)
268 {
269 #if defined (LWLIB_DIALOGS_LUCID)
270   Fprovide (intern ("lucid-dialogs"));
271 #elif defined (LWLIB_DIALOGS_MOTIF)
272   Fprovide (intern ("motif-dialogs"));
273 #elif defined (LWLIB_DIALOGS_ATHENA)
274   Fprovide (intern ("athena-dialogs"));
275 #endif
276 }