Contents in 1999-06-04-13 of release-21-2.
[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 = (char *) "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           xfree (text_field_value);
60         }
61     }
62   free_widget_value (wv);
63 }
64
65 static void
66 dbox_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
67 {
68   /* This is called with client_data == -1 when WM_DELETE_WINDOW is sent
69      instead of a button being selected. */
70   struct device *d = get_device_from_display (XtDisplay (widget));
71   struct frame *f = 0;
72   Widget cur_widget = widget;
73
74   /* The parent which is actually connected to our EmacsFrame may be a
75      ways up the tree. */
76   while (!f && cur_widget)
77     {
78       f = x_any_window_to_frame (d, XtWindow (cur_widget));
79       cur_widget = XtParent (cur_widget);
80     }
81
82   if (popup_handled_p (id))
83     return;
84   assert (popup_up_p != 0);
85   ungcpro_popup_callbacks (id);
86   popup_up_p--;
87   maybe_run_dbox_text_callback (id);
88   popup_selection_callback (widget, id, client_data);
89   lw_destroy_all_widgets (id);
90
91   /* The Motif dialog box sets the keyboard focus to itself.  When it
92      goes away we have to take care of getting the focus back
93      ourselves. */
94 #ifdef EXTERNAL_WIDGET
95   /* #### Not sure if this special case is necessary. */
96   if (!FRAME_X_EXTERNAL_WINDOW_P (f) && f)
97 #else
98   if (f)
99 #endif
100     lw_set_keyboard_focus (FRAME_X_SHELL_WIDGET (f), FRAME_X_TEXT_WIDGET (f));
101 }
102
103 static CONST char * CONST button_names [] = {
104   "button1", "button2", "button3", "button4", "button5",
105   "button6", "button7", "button8", "button9", "button10" };
106
107 /* can't have static frame locals because of some broken compilers */
108 static char tmp_dbox_name [255];
109
110 static widget_value *
111 dbox_descriptor_to_widget_value (Lisp_Object desc)
112 {
113   /* !!#### This function has not been Mule-ized */
114   /* This function can GC */
115   char *name;
116   int lbuttons = 0, rbuttons = 0;
117   int partition_seen = 0;
118   int text_field_p = 0;
119   int allow_text_p = 1;
120   widget_value *prev = 0, *kids = 0;
121   int n = 0;
122   int count = specpdl_depth ();
123   Lisp_Object wv_closure;
124
125   CHECK_CONS (desc);
126   CHECK_STRING (XCAR (desc));
127   name = (char *) XSTRING_DATA (LISP_GETTEXT (XCAR (desc)));
128   desc = XCDR (desc);
129   if (!CONSP (desc))
130     error ("dialog boxes must have some buttons");
131
132   /* Inhibit GC during this conversion.  The reasons for this are
133      the same as in menu_item_descriptor_to_widget_value(); see
134      the large comment above that function. */
135
136   record_unwind_protect (restore_gc_inhibit,
137                          make_int (gc_currently_forbidden));
138   gc_currently_forbidden = 1;
139
140   kids = prev = xmalloc_widget_value ();
141
142   /* Also make sure that we free the partially-created widget_value
143      tree on Lisp error. */
144
145   wv_closure = make_opaque_ptr (kids);
146   record_unwind_protect (widget_value_unwind, wv_closure);
147   prev->name = (char *) "message";
148   prev->value = xstrdup (name);
149   prev->enabled = 1;
150
151   for (; !NILP (desc); desc = Fcdr (desc))
152     {
153       Lisp_Object button = XCAR (desc);
154       widget_value *wv;
155
156       if (NILP (button))
157         {
158           if (partition_seen)
159             error ("more than one partition (nil) seen in dbox spec");
160           partition_seen = 1;
161           continue;
162         }
163       CHECK_VECTOR (button);
164       wv = xmalloc_widget_value ();
165
166       if (!button_item_to_widget_value (button, wv, allow_text_p, 1))
167         {
168           free_widget_value (wv);
169           continue;
170         }
171
172       if (wv->type == TEXT_TYPE)
173         {
174           text_field_p = 1;
175           allow_text_p = 0;      /* only allow one */
176         }
177       else                      /* it's a button */
178         {
179           allow_text_p = 0;      /* only allow text field at the front */
180           wv->value = xstrdup (wv->name);       /* what a mess... */
181           wv->name = (char *) button_names [n];
182
183           if (partition_seen)
184             rbuttons++;
185           else
186             lbuttons++;
187           n++;
188
189           if (lbuttons > 9 || rbuttons > 9)
190             error ("too many buttons (9)"); /* #### this leaks */
191         }
192
193       prev->next = wv;
194       prev = wv;
195     }
196
197   if (n == 0)
198     error ("dialog boxes must have some buttons");
199   {
200     char type = (text_field_p ? 'P' : 'Q');
201     widget_value *dbox;
202     sprintf (tmp_dbox_name, "%c%dBR%d", type, lbuttons + rbuttons, rbuttons);
203     dbox = xmalloc_widget_value ();
204     dbox->name = tmp_dbox_name;
205     dbox->contents = kids;
206
207     /* No more need to free the half-filled-in structures. */
208     set_opaque_ptr (wv_closure, 0);
209     unbind_to (count, Qnil);
210     return dbox;
211   }
212 }
213
214 static void
215 x_popup_dialog_box (struct frame* f, Lisp_Object dbox_desc)
216 {
217   int dbox_id;
218   widget_value *data;
219   Widget parent, dbox;
220
221   data = dbox_descriptor_to_widget_value (dbox_desc);
222
223   parent = FRAME_X_SHELL_WIDGET (f);
224
225   dbox_id = new_lwlib_id ();
226   dbox = lw_create_widget (data->name, "dialog", dbox_id, data, parent, 1, 0,
227                            dbox_selection_callback, 0);
228   lw_modify_all_widgets (dbox_id, data, True);
229   lw_modify_all_widgets (dbox_id, data->contents, True);
230   free_popup_widget_value_tree (data);
231
232   gcpro_popup_callbacks (dbox_id);
233
234   /* Setting zmacs-region-stays is necessary here because executing a
235      command from a dialog is really a two-command process: the first
236      command (bound to the button-click) simply pops up the dialog,
237      and returns.  This causes a sequence of magic-events (destined
238      for the dialog widget) to begin.  Eventually, a dialog item is
239      selected, and a misc-user-event blip is pushed onto the end of
240      the input stream, which is then executed by the event loop.
241
242      So there are two command-events, with a bunch of magic-events
243      between them.  We don't want the *first* command event to alter
244      the state of the region, so that the region can be available as
245      an argument for the second command. */
246   if (zmacs_regions)
247     zmacs_region_stays = 1;
248
249   popup_up_p++;
250   lw_pop_up_all_widgets (dbox_id);
251 }
252
253 void
254 syms_of_dialog_x (void)
255 {
256 }
257
258 void
259 console_type_create_dialog_x (void)
260 {
261   CONSOLE_HAS_METHOD (x, popup_dialog_box);
262 }
263
264 void
265 vars_of_dialog_x (void)
266 {
267 #if defined (LWLIB_DIALOGS_LUCID)
268   Fprovide (intern ("lucid-dialogs"));
269 #elif defined (LWLIB_DIALOGS_MOTIF)
270   Fprovide (intern ("motif-dialogs"));
271 #elif defined (LWLIB_DIALOGS_ATHENA)
272   Fprovide (intern ("athena-dialogs"));
273 #endif
274 }