summaryrefslogtreecommitdiff
path: root/libnautilus-private/nautilus-stock-dialogs.c
blob: 24a8418725d72f8526d3be2cf1e030ed44848c52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* nautilus-stock-dialogs.c: Various standard dialogs for Nautilus.

   Copyright (C) 2000 Eazel, Inc.

   The Gnome Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Authors: Darin Adler <darin@eazel.com>
*/

#include <config.h>
#include "nautilus-stock-dialogs.h"

#include <gtk/gtkbox.h>
#include <gtk/gtklabel.h>
#include <gtk/gtksignal.h>
#include <libgnome/gnome-i18n.h>
#include <libgnomeui/gnome-messagebox.h>
#include <libgnomeui/gnome-stock.h>
#include <libgnomeui/gnome-uidefs.h>
#include "nautilus-string.h"
#include "nautilus-gnome-extensions.h"

struct NautilusTimedWait {
	char *window_title;
	char *wait_message;
	NautilusCancelCallback cancel_callback;
	gpointer callback_data;
	GDestroyNotify destroy_notify;
	GtkWindow *parent_window;
};

static void find_message_label_callback (GtkWidget *widget, gpointer callback_data);

NautilusTimedWait *
nautilus_timed_wait_start (const char *window_title,
			   const char *wait_message,
			   NautilusCancelCallback cancel_callback,
			   gpointer callback_data,
			   GDestroyNotify destroy_notify,
			   GtkWindow *parent_window)
{
	NautilusTimedWait *timed_wait;

	g_return_val_if_fail (window_title != NULL, NULL);
	g_return_val_if_fail (wait_message != NULL, NULL);
	g_return_val_if_fail (cancel_callback != NULL, NULL);
	g_return_val_if_fail (parent_window == NULL || GTK_IS_WINDOW (parent_window), NULL);

	/* Create the timed wait record. */
	timed_wait = g_new (NautilusTimedWait, 1);
	timed_wait->window_title = g_strdup (window_title);
	timed_wait->wait_message = g_strdup (wait_message);
	timed_wait->cancel_callback = cancel_callback;
	timed_wait->callback_data = callback_data;
	timed_wait->destroy_notify = destroy_notify;
	timed_wait->parent_window = parent_window;
	if (parent_window != NULL) {
		gtk_widget_ref (GTK_WIDGET (parent_window));
	}

	return timed_wait;
}

static void
nautilus_timed_wait_free (NautilusTimedWait *timed_wait)
{
	/* Let the caller destroy the callback data. */
	if (timed_wait->destroy_notify != NULL) {
		(* timed_wait->destroy_notify) (timed_wait->callback_data);
	}

	/* Now free the other stuff we were holding onto. */
	g_free (timed_wait->window_title);
	g_free (timed_wait->wait_message);
	if (timed_wait->parent_window != NULL) {
		gtk_widget_unref (GTK_WIDGET (timed_wait->parent_window));
	}

	/* And the wait object itself. */
	g_free (timed_wait);
}

void
nautilus_timed_wait_stop (NautilusTimedWait *timed_wait)
{
	g_return_if_fail (timed_wait != NULL);

	nautilus_timed_wait_free (timed_wait);
}

static const char **
convert_varargs_to_name_array (va_list args)
{
	GPtrArray *resizeable_array;
	const char *name;
	const char **plain_ole_array;
	
	resizeable_array = g_ptr_array_new ();

	do {
		name = va_arg (args, const char *);
		g_ptr_array_add (resizeable_array, (gpointer) name);
	} while (name != NULL);

	plain_ole_array = (const char **) resizeable_array->pdata;
	
	g_ptr_array_free (resizeable_array, FALSE);

	return plain_ole_array;
}

int
nautilus_simple_dialog (GtkWidget *parent, const char *text, const char *title, ...)
{
	va_list button_title_args;
	const char **button_titles;
        GtkWidget *dialog;
        GtkWidget *top_widget;
        GtkWidget *prompt_widget;
	
	/* Create the dialog. */
	va_start (button_title_args, title);
	button_titles = convert_varargs_to_name_array (button_title_args);
	va_end (button_title_args);
        dialog = gnome_dialog_newv (title, button_titles);
	g_free (button_titles);
	
	/* Allow close. */
        gnome_dialog_set_close (GNOME_DIALOG (dialog), TRUE);
        gnome_dialog_close_hides (GNOME_DIALOG (dialog), TRUE);
	
	/* Parent it if asked to. */
        if (parent != NULL) {
		top_widget = gtk_widget_get_toplevel (parent);
		if (GTK_IS_WINDOW (top_widget)) {
			gnome_dialog_set_parent (GNOME_DIALOG (dialog), GTK_WINDOW (top_widget));
		}
	}
	
	/* Title it if asked to. */
	if (text != NULL) {
		prompt_widget = gtk_label_new (text);
		gtk_label_set_line_wrap (GTK_LABEL (prompt_widget), TRUE);
		gtk_label_set_justify (GTK_LABEL (prompt_widget),
				       GTK_JUSTIFY_LEFT);
		gtk_box_pack_start (GTK_BOX (GNOME_DIALOG (dialog)->vbox),
				    prompt_widget,
				    TRUE, TRUE, GNOME_PAD);
	}
	
	/* Run it. */
        gtk_widget_show_all (dialog);
        return gnome_dialog_run_and_close (GNOME_DIALOG (dialog));
}

static void
find_message_label (GtkWidget *widget, const char *message)
{
	char *text;

	/* Turn on the flag if we find a label with the message
	 * in it.
	 */
	if (GTK_IS_LABEL (widget)) {
		gtk_label_get (GTK_LABEL (widget), &text);
		if (nautilus_strcmp (text, message) == 0) {
			gtk_object_set_data (GTK_OBJECT (gtk_widget_get_toplevel (widget)),
					     "message label", widget);
		}
	}

	/* Recurse for children. */
	if (GTK_IS_CONTAINER (widget)) {
		gtk_container_foreach (GTK_CONTAINER (widget),
				       find_message_label_callback,
				       (char *) message);
	}
}

static void
find_message_label_callback (GtkWidget *widget, gpointer callback_data)
{
	find_message_label (widget, callback_data);
}

/* Shamelessly stolen from gnome-dialog-utils.c: */
static GnomeDialog *
show_message_box (const char *message,
		  const char *type,
		  const char *button_one,
		  const char *button_two,
		  GtkWindow *parent)
{  
	GtkWidget *box;
	GtkLabel *message_label;

	box = gnome_message_box_new (message, type, button_one, button_two, NULL);
	
	/* A bit of a hack. We want to use gnome_message_box_new,
	 * but we want the message to be wrapped. So, we search
	 * for the label with this message so we can mark it.
	 */
	find_message_label (box, message);
	message_label = GTK_LABEL (gtk_object_get_data (GTK_OBJECT (box), "message label"));
	gtk_label_set_line_wrap (message_label, TRUE);

	if (parent != NULL) {
		gnome_dialog_set_parent (GNOME_DIALOG (box), parent);
	}
	gtk_widget_show (box);
	return GNOME_DIALOG (box);
}

static GnomeDialog *
show_ok_box (const char *message,
	     const char *type,
	     GtkWindow *parent)
{  
	return show_message_box	(message, type, GNOME_STOCK_BUTTON_OK, NULL, parent);
}

GnomeDialog *
nautilus_info_dialog (const char *info,
		      GtkWindow *parent)
{
	return show_ok_box (info, GNOME_MESSAGE_BOX_INFO, parent);
}

GnomeDialog *
nautilus_warning_dialog (const char *warning,
			 GtkWindow *parent)
{
	return show_ok_box (warning, GNOME_MESSAGE_BOX_WARNING, parent);
}

GnomeDialog *
nautilus_error_dialog (const char *error,
		       GtkWindow *parent)
{
	return show_ok_box (error, GNOME_MESSAGE_BOX_ERROR, parent);
}

static void
clicked_callback (GnomeDialog *dialog,
		  int button_number,
		  const char *detailed_error_message)
{
	GtkLabel *label;

	switch (button_number) {
	case 0: /* Details */
		label = GTK_LABEL (gtk_object_get_data (GTK_OBJECT (dialog), "message label"));
		gtk_label_set_text (label, detailed_error_message);
		gtk_widget_hide (GTK_WIDGET (nautilus_gnome_dialog_get_button_by_index (dialog, 0)));
		break;
	case 1: /* OK */
		gnome_dialog_close (dialog);
		break;
	}
}

GnomeDialog *
nautilus_error_dialog_with_details (const char *error_message,
				    const char *detailed_error_message,
				    GtkWindow *parent)
{
	GnomeDialog *dialog;

	g_return_val_if_fail (error_message != NULL, NULL);
	g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);

	if (detailed_error_message == NULL
	    || strcmp (error_message, detailed_error_message) == 0) {
		return nautilus_error_dialog (error_message, parent);
	}

	dialog = show_message_box (error_message, GNOME_MESSAGE_BOX_ERROR,
				   _("Details"), GNOME_STOCK_BUTTON_OK, parent);

	/* Show the details when you click on the details button. */
	gnome_dialog_set_close (dialog, FALSE);
	gtk_signal_connect_full (GTK_OBJECT (dialog), "clicked",
				 clicked_callback, NULL, g_strdup (detailed_error_message),
				 g_free, FALSE, FALSE);

	return dialog;
}

/**
 * nautilus_yes_no_dialog:
 * 
 * Create a dialog asking a question with two choices.
 * The caller needs to set up any necessary callbacks 
 * for the buttons.
 * @question: The text of the question.
 * @yes_label: The label of the "yes" button.
 * @no_label: The label of the "no" button.
 * @parent: The parent window for this dialog.
 */
GnomeDialog *
nautilus_yes_no_dialog (const char *question, 
			const char *yes_label,
			const char *no_label,
			GtkWindow *parent)
{
	return show_message_box (question, 
				 GNOME_MESSAGE_BOX_QUESTION,
				 yes_label,
				 no_label,
				 parent);
}