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.
5 This file is part of XEmacs.
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
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
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. */
22 /* Synched up with: Not in FSF. */
27 #include "console-x.h"
28 #include "EmacsFrame.h"
32 #include "commands.h" /* zmacs_regions */
41 maybe_run_dbox_text_callback (LWLIB_ID id)
43 /* !!#### This function has not been Mule-ized */
46 wv = xmalloc_widget_value ();
47 wv->name = xstrdup ("value");
48 got_some = lw_get_some_values (id, wv);
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));
58 LISP_TO_VOID (cons3 (Qnil,
59 list2 (text_field_callback,
60 build_string (text_field_value)),
62 popup_selection_callback (0, id, (XtPointer) tmp);
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);
72 dbox_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
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));
78 Widget cur_widget = widget;
80 /* The parent which is actually connected to our EmacsFrame may be a
82 while (!f && cur_widget)
84 f = x_any_window_to_frame (d, XtWindow (cur_widget));
85 cur_widget = XtParent (cur_widget);
88 if (popup_handled_p (id))
90 assert (popup_up_p != 0);
91 ungcpro_popup_callbacks (id);
93 maybe_run_dbox_text_callback (id);
94 popup_selection_callback (widget, id, client_data);
95 lw_destroy_all_widgets (id);
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
100 #ifdef EXTERNAL_WIDGET
101 /* #### Not sure if this special case is necessary. */
102 if (!FRAME_X_EXTERNAL_WINDOW_P (f) && f)
106 lw_set_keyboard_focus (FRAME_X_SHELL_WIDGET (f), FRAME_X_TEXT_WIDGET (f));
109 static const char * const button_names [] = {
110 "button1", "button2", "button3", "button4", "button5",
111 "button6", "button7", "button8", "button9", "button10" };
113 /* can't have static frame locals because of some broken compilers */
114 static char tmp_dbox_name [255];
116 static widget_value *
117 dbox_descriptor_to_widget_value (Lisp_Object desc)
119 /* !!#### This function has not been Mule-ized */
120 /* This function can GC */
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;
128 int count = specpdl_depth ();
129 Lisp_Object wv_closure, gui_item;
132 CHECK_STRING (XCAR (desc));
133 name = (char *) XSTRING_DATA (LISP_GETTEXT (XCAR (desc)));
136 error ("dialog boxes must have some buttons");
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. */
142 record_unwind_protect (restore_gc_inhibit,
143 make_int (gc_currently_forbidden));
144 gc_currently_forbidden = 1;
146 kids = prev = xmalloc_widget_value ();
148 /* Also make sure that we free the partially-created widget_value
149 tree on Lisp error. */
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);
157 for (; !NILP (desc); desc = Fcdr (desc))
159 Lisp_Object button = XCAR (desc);
165 error ("more than one partition (nil) seen in dbox spec");
169 CHECK_VECTOR (button);
170 wv = xmalloc_widget_value ();
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))
176 free_widget_value_tree (wv);
180 if (wv->type == TEXT_TYPE)
183 allow_text_p = 0; /* only allow one */
185 else /* it's a button */
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]);
198 if (lbuttons > 9 || rbuttons > 9)
199 error ("too many buttons (9)"); /* #### this leaks */
207 error ("dialog boxes must have some buttons");
209 char type = (text_field_p ? 'P' : 'Q');
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;
216 /* No more need to free the half-filled-in structures. */
217 set_opaque_ptr (wv_closure, 0);
218 unbind_to (count, Qnil);
224 x_popup_dialog_box (struct frame* f, Lisp_Object dbox_desc)
230 data = dbox_descriptor_to_widget_value (dbox_desc);
232 parent = FRAME_X_SHELL_WIDGET (f);
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);
241 gcpro_popup_callbacks (dbox_id);
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.
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. */
256 zmacs_region_stays = 1;
259 lw_pop_up_all_widgets (dbox_id);
263 syms_of_dialog_x (void)
268 console_type_create_dialog_x (void)
270 CONSOLE_HAS_METHOD (x, popup_dialog_box);
274 vars_of_dialog_x (void)
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"));