GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-gui-query.c
1 /********************************************************************\
2  * gnc-gui-query.c -- functions for creating dialogs for GnuCash *
3  * Copyright (C) 1998, 1999, 2000 Linas Vepstas *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License*
16  * along with this program; if not, contact: *
17  * *
18  * Free Software Foundation Voice: +1-617-542-5942 *
19  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20  * Boston, MA 02110-1301, USA [email protected] *
21 \********************************************************************/
22 
23 #include "config.h"
24 
25 #include <glib/gi18n.h>
26 
27 #include "dialog-utils.h"
28 #include "qof.h"
29 #include "gnc-gui-query.h"
30 #include "gnc-ui.h"
31 
32 #define INDEX_LABEL "index"
33 
34 /* This static indicates the debugging module that this .o belongs to. */
35 /* static short module = MOD_GUI; */
36 
37 /********************************************************************\
38  * gnc_ok_cancel_dialog *
39  * display a message, and asks the user to press "Ok" or "Cancel" *
40  * *
41  * NOTE: This function does not return until the dialog is closed *
42  * *
43  * Args: parent - the parent window *
44  * default - the button that will be the default *
45  * message - the message to display *
46  * format - the format string for the message to display *
47  * This is a standard 'printf' style string. *
48  * args - a pointer to the first argument for the format *
49  * string. *
50  * Return: the result the user selected *
51 \********************************************************************/
52 gint
53 gnc_ok_cancel_dialog(GtkWidget *parent,
54  gint default_result,
55  const gchar *format, ...)
56 {
57  GtkWidget *dialog = NULL;
58  gint result;
59  gchar *buffer;
60  va_list args;
61 
62  if (parent == NULL)
63  parent = gnc_ui_get_toplevel();
64 
65  va_start(args, format);
66  buffer = g_strdup_vprintf(format, args);
67  dialog = gtk_message_dialog_new (GTK_WINDOW(parent),
68  GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
69  GTK_MESSAGE_QUESTION,
70  GTK_BUTTONS_OK_CANCEL,
71  "%s",
72  buffer);
73  g_free(buffer);
74  va_end(args);
75 
76  if (parent == NULL)
77  gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), FALSE);
78 
79  gtk_dialog_set_default_response (GTK_DIALOG(dialog), default_result);
80  result = gtk_dialog_run(GTK_DIALOG(dialog));
81  gtk_widget_destroy (dialog);
82  return(result);
83 }
84 
85 
86 
87 /********************************************************************\
88  * gnc_verify_dialog *
89  * display a message, and asks the user to press "Yes" or "No" *
90  * *
91  * NOTE: This function does not return until the dialog is closed *
92  * *
93  * Args: parent - the parent window *
94  * yes_is_default - If true, "Yes" is default, *
95  * "No" is the default button. *
96  * format - the format string for the message to display *
97  * This is a standard 'printf' style string. *
98  * args - a pointer to the first argument for the format *
99  * string. *
100 \********************************************************************/
101 gboolean
102 gnc_verify_dialog(GtkWidget *parent, gboolean yes_is_default,
103  const gchar *format, ...)
104 {
105  GtkWidget *dialog;
106  gchar *buffer;
107  gint result;
108  va_list args;
109 
110  if (parent == NULL)
111  parent = gnc_ui_get_toplevel();
112 
113  va_start(args, format);
114  buffer = g_strdup_vprintf(format, args);
115  dialog = gtk_message_dialog_new (GTK_WINDOW(parent),
116  GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
117  GTK_MESSAGE_QUESTION,
118  GTK_BUTTONS_YES_NO,
119  "%s",
120  buffer);
121  g_free(buffer);
122  va_end(args);
123 
124  if (parent == NULL)
125  gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), FALSE);
126 
127  gtk_dialog_set_default_response(GTK_DIALOG(dialog),
128  (yes_is_default ? GTK_RESPONSE_YES : GTK_RESPONSE_NO));
129  result = gtk_dialog_run(GTK_DIALOG(dialog));
130  gtk_widget_destroy (dialog);
131  return (result == GTK_RESPONSE_YES);
132 }
133 
134 
135 /********************************************************************\
136  * gnc_info_dialog *
137  * displays an information dialog box *
138  * *
139  * Args: parent - the parent window *
140  * format - the format string for the message to display *
141  * This is a standard 'printf' style string. *
142  * args - a pointer to the first argument for the format *
143  * string. *
144  * Return: none *
145 \********************************************************************/
146 void
147 gnc_info_dialog(GtkWidget *parent, const gchar *format, ...)
148 {
149  GtkWidget *dialog;
150  gchar *buffer;
151  va_list args;
152 
153  if (parent == NULL)
154  parent = gnc_ui_get_toplevel();
155 
156  va_start(args, format);
157  buffer = g_strdup_vprintf(format, args);
158  dialog = gtk_message_dialog_new (GTK_WINDOW(parent),
159  GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
160  GTK_MESSAGE_INFO,
161  GTK_BUTTONS_CLOSE,
162  "%s",
163  buffer);
164  va_end(args);
165 
166  if (parent == NULL)
167  gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), FALSE);
168 
169  gtk_dialog_run(GTK_DIALOG(dialog));
170  gtk_widget_destroy (dialog);
171 }
172 
173 
174 
175 /********************************************************************\
176  * gnc_warning_dialog_common *
177  * displays a warning dialog box *
178  * *
179  * Args: parent - the parent window *
180  * format - the format string for the message to display *
181  * This is a standard 'printf' style string. *
182  * args - a pointer to the first argument for the format *
183  * string. *
184  * Return: none *
185 \********************************************************************/
186 static void
187 gnc_warning_dialog_common(GtkWidget *parent, const gchar *format, va_list args)
188 {
189  GtkWidget *dialog = NULL;
190  gchar *buffer;
191 
192  if (parent == NULL)
193  parent = GTK_WIDGET(gnc_ui_get_toplevel());
194 
195  buffer = g_strdup_vprintf(format, args);
196  dialog = gtk_message_dialog_new (GTK_WINDOW(parent),
197  GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
198  GTK_MESSAGE_WARNING,
199  GTK_BUTTONS_CLOSE,
200  "%s",
201  buffer);
202  g_free(buffer);
203 
204  if (parent == NULL)
205  gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), FALSE);
206 
207  gtk_dialog_run (GTK_DIALOG (dialog));
208  gtk_widget_destroy (dialog);
209 }
210 
211 void
212 gnc_warning_dialog(GtkWidget *parent, const gchar *format, ...)
213 {
214  va_list args;
215 
216  va_start(args, format);
217  gnc_warning_dialog_common(parent, format, args);
218  va_end(args);
219 }
220 
221 
222 /********************************************************************\
223  * gnc_error_dialog_common *
224  * displays an error dialog box *
225  * *
226  * Args: parent - the parent window *
227  * format - the format string for the message to display *
228  * This is a standard 'printf' style string. *
229  * args - a pointer to the first argument for the format *
230  * string. *
231  * Return: none *
232 \********************************************************************/
233 static void
234 gnc_error_dialog_common(GtkWidget *parent, const gchar *format, va_list args)
235 {
236  GtkWidget *dialog;
237  gchar *buffer;
238 
239  if (parent == NULL)
240  parent = GTK_WIDGET(gnc_ui_get_toplevel());
241 
242  buffer = g_strdup_vprintf(format, args);
243  dialog = gtk_message_dialog_new (GTK_WINDOW(parent),
244  GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
245  GTK_MESSAGE_ERROR,
246  GTK_BUTTONS_CLOSE,
247  "%s",
248  buffer);
249  g_free(buffer);
250 
251  if (parent == NULL)
252  gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), FALSE);
253 
254  gtk_dialog_run(GTK_DIALOG(dialog));
255  gtk_widget_destroy (dialog);
256 }
257 
258 void
259 gnc_error_dialog(GtkWidget *parent, const gchar *format, ...)
260 {
261  va_list args;
262 
263  va_start(args, format);
264  gnc_error_dialog_common(parent, format, args);
265  va_end(args);
266 }
267 
268 static void
269 gnc_choose_radio_button_cb(GtkWidget *w, gpointer data)
270 {
271  int *result = data;
272 
273  if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)))
274  *result = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w), INDEX_LABEL));
275 }
276 
277 /********************************************************************
278  gnc_choose_radio_option_dialog
279 
280  display a group of radio_buttons and return the index of
281  the selected one
282 */
283 
284 int
285 gnc_choose_radio_option_dialog(GtkWidget *parent,
286  const char *title,
287  const char *msg,
288  const char *button_name,
289  int default_value,
290  GList *radio_list)
291 {
292  int radio_result = 0; /* initial selected value is first one */
293  GtkWidget *vbox;
294  GtkWidget *main_vbox;
295  GtkWidget *label;
296  GtkWidget *alignment;
297  GtkWidget *radio_button;
298  GtkWidget *dialog;
299  GtkWidget *dvbox;
300  GSList *group = NULL;
301  GList *node;
302  int i;
303 
304  main_vbox = gtk_vbox_new(FALSE, 3);
305  gtk_container_set_border_width(GTK_CONTAINER(main_vbox), 6);
306  gtk_widget_show(main_vbox);
307 
308  label = gtk_label_new(msg);
309  gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
310  gtk_box_pack_start(GTK_BOX(main_vbox), label, FALSE, FALSE, 0);
311  gtk_widget_show(label);
312 
313  alignment = gtk_alignment_new(0.0, 0.0, 1.0, 1.0);
314  gtk_alignment_set_padding (GTK_ALIGNMENT(alignment), 0, 0, 12, 0);
315  gtk_box_pack_start(GTK_BOX(main_vbox), alignment, FALSE, FALSE, 0);
316  gtk_widget_show(alignment);
317 
318  vbox = gtk_vbox_new(TRUE, 3);
319  gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
320  gtk_container_add(GTK_CONTAINER(alignment), vbox);
321  gtk_widget_show(vbox);
322 
323  for (node = radio_list, i = 0; node; node = node->next, i++)
324  {
325  radio_button = gtk_radio_button_new_with_mnemonic(group, node->data);
326  group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(radio_button));
327 
328  if (i == default_value) /* default is first radio button */
329  {
330  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio_button), TRUE);
331  radio_result = default_value;
332  }
333 
334  gtk_widget_show(radio_button);
335  gtk_box_pack_start(GTK_BOX(vbox), radio_button, FALSE, FALSE, 0);
336  g_object_set_data(G_OBJECT(radio_button), INDEX_LABEL, GINT_TO_POINTER(i));
337  g_signal_connect(radio_button, "clicked",
338  G_CALLBACK(gnc_choose_radio_button_cb),
339  &radio_result);
340  }
341 
342  if (!button_name)
343  button_name = GTK_STOCK_OK;
344  dialog = gtk_dialog_new_with_buttons (title, GTK_WINDOW(parent),
345  GTK_DIALOG_DESTROY_WITH_PARENT,
346  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
347  button_name, GTK_RESPONSE_OK,
348  NULL);
349 
350  /* default to ok */
351  gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
352 
353  dvbox = GTK_DIALOG(dialog)->vbox;
354 
355  gtk_box_pack_start(GTK_BOX(dvbox), main_vbox, TRUE, TRUE, 0);
356 
357  if (gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_OK)
358  radio_result = -1;
359 
360  gtk_widget_destroy (dialog);
361 
362  return radio_result;
363 }
GtkWidget * gnc_ui_get_toplevel(void)