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