evolution-3.6.4/libevolution-utils/e-alert-dialog.c

No issues found

  1 /*
  2  * This program is free software; you can redistribute it and/or
  3  * modify it under the terms of the GNU Lesser General Public
  4  * License as published by the Free Software Foundation; either
  5  * version 2 of the License, or (at your option) version 3.
  6  *
  7  * This program is distributed in the hope that it will be useful,
  8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 10  * Lesser General Public License for more details.
 11  *
 12  * You should have received a copy of the GNU Lesser General Public
 13  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 14  *
 15  *
 16  * Authors:
 17  *   Michael Zucchi <notzed@ximian.com>
 18  *   Jonathon Jongsma <jonathon.jongsma@collabora.co.uk>
 19  *
 20  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 21  * Copyright (C) 2009 Intel Corporation
 22  */
 23 
 24 #ifdef HAVE_CONFIG_H
 25 #include <config.h>
 26 #endif
 27 
 28 #include <glib/gi18n.h>
 29 
 30 #include "e-alert-dialog.h"
 31 
 32 #define E_ALERT_DIALOG_GET_PRIVATE(obj) \
 33 	(G_TYPE_INSTANCE_GET_PRIVATE \
 34 	((obj), E_TYPE_ALERT_DIALOG, EAlertDialogPrivate))
 35 
 36 struct _EAlertDialogPrivate {
 37 	GtkWidget *content_area;  /* not referenced */
 38 	EAlert *alert;
 39 };
 40 
 41 enum {
 42 	PROP_0,
 43 	PROP_ALERT
 44 };
 45 
 46 G_DEFINE_TYPE (
 47 	EAlertDialog,
 48 	e_alert_dialog,
 49 	GTK_TYPE_DIALOG)
 50 
 51 static void
 52 alert_dialog_set_alert (EAlertDialog *dialog,
 53                         EAlert *alert)
 54 {
 55 	g_return_if_fail (E_IS_ALERT (alert));
 56 	g_return_if_fail (dialog->priv->alert == NULL);
 57 
 58 	dialog->priv->alert = g_object_ref (alert);
 59 }
 60 
 61 static void
 62 alert_dialog_set_property (GObject *object,
 63                            guint property_id,
 64                            const GValue *value,
 65                            GParamSpec *pspec)
 66 {
 67 	switch (property_id) {
 68 		case PROP_ALERT:
 69 			alert_dialog_set_alert (
 70 				E_ALERT_DIALOG (object),
 71 				g_value_get_object (value));
 72 			return;
 73 	}
 74 
 75 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 76 }
 77 
 78 static void
 79 alert_dialog_get_property (GObject *object,
 80                            guint property_id,
 81                            GValue *value,
 82                            GParamSpec *pspec)
 83 {
 84 	switch (property_id) {
 85 		case PROP_ALERT:
 86 			g_value_set_object (
 87 				value, e_alert_dialog_get_alert (
 88 				E_ALERT_DIALOG (object)));
 89 			return;
 90 	}
 91 
 92 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 93 }
 94 
 95 static void
 96 alert_dialog_dispose (GObject *object)
 97 {
 98 	EAlertDialogPrivate *priv;
 99 
100 	priv = E_ALERT_DIALOG_GET_PRIVATE (object);
101 
102 	if (priv->alert) {
103 		g_signal_handlers_disconnect_matched (
104 			priv->alert, G_SIGNAL_MATCH_DATA,
105 			0, 0, NULL, NULL, object);
106 		g_object_unref (priv->alert);
107 		priv->alert = NULL;
108 	}
109 
110 	/* Chain up to parent's dispose() method. */
111 	G_OBJECT_CLASS (e_alert_dialog_parent_class)->dispose (object);
112 }
113 
114 static void
115 alert_dialog_constructed (GObject *object)
116 {
117 	EAlert *alert;
118 	EAlertDialog *dialog;
119 	GtkWidget *action_area;
120 	GtkWidget *content_area;
121 	GtkWidget *container;
122 	GtkWidget *widget;
123 	PangoAttribute *attr;
124 	PangoAttrList *list;
125 	GList *actions;
126 	const gchar *primary, *secondary;
127 	gint default_response;
128 	gint min_width = -1, prefer_width = -1;
129 	gint height;
130 
131 	/* Chain up to parent's constructed() method. */
132 	G_OBJECT_CLASS (e_alert_dialog_parent_class)->constructed (object);
133 
134 	dialog = E_ALERT_DIALOG (object);
135 	alert = e_alert_dialog_get_alert (dialog);
136 
137 	default_response = e_alert_get_default_response (alert);
138 
139 	gtk_window_set_title (GTK_WINDOW (dialog), " ");
140 
141 	action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
142 	content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
143 
144 	gtk_widget_ensure_style (GTK_WIDGET (dialog));
145 	gtk_container_set_border_width (GTK_CONTAINER (action_area), 12);
146 	gtk_container_set_border_width (GTK_CONTAINER (content_area), 0);
147 
148 	gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
149 
150 	/* Forward EAlert::response signals to GtkDialog::response. */
151 	g_signal_connect_swapped (
152 		alert, "response",
153 		G_CALLBACK (gtk_dialog_response), dialog);
154 
155 	/* Add buttons from actions. */
156 	actions = e_alert_peek_actions (alert);
157 	if (!actions) {
158 		GtkAction *action;
159 
160 		/* Make sure there is at least one action, thus the dialog can be closed. */
161 		action = gtk_action_new (
162 			"alert-response-0", _("_Dismiss"), NULL, NULL);
163 		e_alert_add_action (alert, action, GTK_RESPONSE_CLOSE);
164 		g_object_unref (action);
165 
166 		actions = e_alert_peek_actions (alert);
167 	}
168 
169 	while (actions != NULL) {
170 		GtkWidget *button;
171 		gpointer data;
172 
173 		/* These actions are already wired to trigger an
174 		 * EAlert::response signal when activated, which
175 		 * will in turn call to gtk_dialog_response(),
176 		 * so we can add buttons directly to the action
177 		 * area without knowing their response IDs.
178 		 * (XXX Well, kind of.  See below.) */
179 
180 		button = gtk_button_new ();
181 
182 		gtk_widget_set_can_default (button, TRUE);
183 
184 		gtk_activatable_set_related_action (
185 			GTK_ACTIVATABLE (button),
186 			GTK_ACTION (actions->data));
187 
188 		gtk_box_pack_end (
189 			GTK_BOX (action_area),
190 			button, FALSE, FALSE, 0);
191 
192 		/* This is set in e_alert_add_action(). */
193 		data = g_object_get_data (
194 			actions->data, "e-alert-response-id");
195 
196 		/* Normally GtkDialog sets the initial focus widget to
197 		 * the button corresponding to the default response, but
198 		 * because the buttons are not directly tied to response
199 		 * IDs, we have set both the default widget and the
200 		 * initial focus widget ourselves. */
201 		if (GPOINTER_TO_INT (data) == default_response) {
202 			gtk_widget_grab_default (button);
203 			gtk_widget_grab_focus (button);
204 		}
205 
206 		actions = g_list_next (actions);
207 	}
208 
209 	widget = gtk_hbox_new (FALSE, 12);
210 	gtk_container_set_border_width (GTK_CONTAINER (widget), 12);
211 	gtk_box_pack_start (GTK_BOX (content_area), widget, FALSE, FALSE, 0);
212 	gtk_widget_show (widget);
213 
214 	container = widget;
215 
216 	widget = e_alert_create_image (alert, GTK_ICON_SIZE_DIALOG);
217 	gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.0);
218 	gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
219 	gtk_widget_show (widget);
220 
221 	widget = gtk_vbox_new (FALSE, 12);
222 	gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
223 	dialog->priv->content_area = widget;
224 	gtk_widget_show (widget);
225 
226 	container = widget;
227 
228 	primary = e_alert_get_primary_text (alert);
229 	secondary = e_alert_get_secondary_text (alert);
230 
231 	list = pango_attr_list_new ();
232 	attr = pango_attr_scale_new (PANGO_SCALE_LARGE);
233 	pango_attr_list_insert (list, attr);
234 	attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
235 	pango_attr_list_insert (list, attr);
236 
237 	widget = gtk_label_new (primary);
238 	gtk_label_set_attributes (GTK_LABEL (widget), list);
239 	gtk_label_set_line_wrap (GTK_LABEL (widget), TRUE);
240 	gtk_label_set_selectable (GTK_LABEL (widget), TRUE);
241 	gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.0);
242 	gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
243 	gtk_widget_set_can_focus (widget, FALSE);
244 	gtk_widget_show (widget);
245 
246 	widget = gtk_label_new (secondary);
247 	gtk_label_set_line_wrap (GTK_LABEL (widget), TRUE);
248 	gtk_label_set_selectable (GTK_LABEL (widget), TRUE);
249 	gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.0);
250 	gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
251 	gtk_widget_set_can_focus (widget, FALSE);
252 	gtk_widget_show (widget);
253 
254 	widget = GTK_WIDGET (dialog);
255 
256 	height = gtk_widget_get_allocated_height (widget);
257 	gtk_widget_get_preferred_width_for_height (
258 		widget, height, &min_width, &prefer_width);
259 	if (min_width < prefer_width)
260 		gtk_window_set_default_size (
261 			GTK_WINDOW (dialog), MIN (
262 			(min_width + prefer_width) / 2,
263 			min_width * 5 / 4), -1);
264 
265 	pango_attr_list_unref (list);
266 }
267 
268 static void
269 e_alert_dialog_class_init (EAlertDialogClass *class)
270 {
271 	GObjectClass *object_class;
272 
273 	g_type_class_add_private (class, sizeof (EAlertDialogPrivate));
274 
275 	object_class = G_OBJECT_CLASS (class);
276 	object_class->set_property = alert_dialog_set_property;
277 	object_class->get_property = alert_dialog_get_property;
278 	object_class->dispose = alert_dialog_dispose;
279 	object_class->constructed = alert_dialog_constructed;
280 
281 	g_object_class_install_property (
282 		object_class,
283 		PROP_ALERT,
284 		g_param_spec_object (
285 			"alert",
286 			"Alert",
287 			"Alert to be displayed",
288 			E_TYPE_ALERT,
289 			G_PARAM_READWRITE |
290 			G_PARAM_CONSTRUCT_ONLY |
291 			G_PARAM_STATIC_STRINGS));
292 }
293 
294 static void
295 e_alert_dialog_init (EAlertDialog *dialog)
296 {
297 	dialog->priv = E_ALERT_DIALOG_GET_PRIVATE (dialog);
298 }
299 
300 GtkWidget *
301 e_alert_dialog_new (GtkWindow *parent,
302                     EAlert *alert)
303 {
304 	g_return_val_if_fail (E_IS_ALERT (alert), NULL);
305 
306 	return g_object_new (
307 		E_TYPE_ALERT_DIALOG,
308 		"alert", alert, "transient-for", parent, NULL);
309 }
310 
311 GtkWidget *
312 e_alert_dialog_new_for_args (GtkWindow *parent,
313                              const gchar *tag,
314                              ...)
315 {
316 	GtkWidget *dialog;
317 	EAlert *alert;
318 	va_list ap;
319 
320 	g_return_val_if_fail (tag != NULL, NULL);
321 
322 	va_start (ap, tag);
323 	alert = e_alert_new_valist (tag, ap);
324 	va_end (ap);
325 
326 	dialog = e_alert_dialog_new (parent, alert);
327 
328 	g_object_unref (alert);
329 
330 	return dialog;
331 }
332 
333 gint
334 e_alert_run_dialog (GtkWindow *parent,
335                     EAlert *alert)
336 {
337 	GtkWidget *dialog;
338 	gint response;
339 
340 	g_return_val_if_fail (E_IS_ALERT (alert), 0);
341 
342 	dialog = e_alert_dialog_new (parent, alert);
343 	response = gtk_dialog_run (GTK_DIALOG (dialog));
344 	gtk_widget_destroy (dialog);
345 
346 	return response;
347 }
348 
349 gint
350 e_alert_run_dialog_for_args (GtkWindow *parent,
351                              const gchar *tag,
352                              ...)
353 {
354 	EAlert *alert;
355 	gint response;
356 	va_list ap;
357 
358 	g_return_val_if_fail (tag != NULL, 0);
359 
360 	va_start (ap, tag);
361 	alert = e_alert_new_valist (tag, ap);
362 	va_end (ap);
363 
364 	response = e_alert_run_dialog (parent, alert);
365 
366 	g_object_unref (alert);
367 
368 	return response;
369 }
370 
371 /**
372  * e_alert_dialog_get_alert:
373  * @dialog: an #EAlertDialog
374  *
375  * Returns the #EAlert associated with @dialog.
376  *
377  * Returns: the #EAlert associated with @dialog
378  **/
379 EAlert *
380 e_alert_dialog_get_alert (EAlertDialog *dialog)
381 {
382 	g_return_val_if_fail (E_IS_ALERT_DIALOG (dialog), NULL);
383 
384 	return dialog->priv->alert;
385 }
386 
387 /**
388  * e_alert_dialog_get_content_area:
389  * @dialog: an #EAlertDialog
390  *
391  * Returns the vertical box containing the primary and secondary labels.
392  * Use this to pack additional widgets into the dialog with the proper
393  * horizontal alignment (maintaining the left margin below the image).
394  *
395  * Returns: the content area #GtkBox
396  **/
397 GtkWidget *
398 e_alert_dialog_get_content_area (EAlertDialog *dialog)
399 {
400 	g_return_val_if_fail (E_IS_ALERT_DIALOG (dialog), NULL);
401 
402 	return dialog->priv->content_area;
403 }