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