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