aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-alert-dialog.c
blob: 156c2729ac33e060056276cd37e21388266c35fe (plain) (blame)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *   Michael Zucchi <notzed@ximian.com>
 *   Jonathon Jongsma <jonathon.jongsma@collabora.co.uk>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 * Copyright (C) 2009 Intel Corporation
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib/gi18n.h>

#include "e-alert-dialog.h"

#define E_ALERT_DIALOG_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_ALERT_DIALOG, EAlertDialogPrivate))

struct _EAlertDialogPrivate {
    GtkWidget *content_area;  /* not referenced */
    EAlert *alert;
};

enum {
    PROP_0,
    PROP_ALERT
};

G_DEFINE_TYPE (
    EAlertDialog,
    e_alert_dialog,
    GTK_TYPE_DIALOG)

static void
alert_dialog_set_alert (EAlertDialog *dialog,
                        EAlert *alert)
{
    g_return_if_fail (E_IS_ALERT (alert));
    g_return_if_fail (dialog->priv->alert == NULL);

    dialog->priv->alert = g_object_ref (alert);
}

static void
alert_dialog_set_property (GObject *object,
                           guint property_id,
                           const GValue *value,
                           GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_ALERT:
            alert_dialog_set_alert (
                E_ALERT_DIALOG (object),
                g_value_get_object (value));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
alert_dialog_get_property (GObject *object,
                           guint property_id,
                           GValue *value,
                           GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_ALERT:
            g_value_set_object (
                value, e_alert_dialog_get_alert (
                E_ALERT_DIALOG (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
alert_dialog_dispose (GObject *object)
{
    EAlertDialogPrivate *priv;

    priv = E_ALERT_DIALOG_GET_PRIVATE (object);

    if (priv->alert) {
        g_signal_handlers_disconnect_matched (
            priv->alert, G_SIGNAL_MATCH_DATA,
            0, 0, NULL, NULL, object);
        g_object_unref (priv->alert);
        priv->alert = NULL;
    }

    /* Chain up to parent's dispose() method. */
    G_OBJECT_CLASS (e_alert_dialog_parent_class)->dispose (object);
}

static void
alert_dialog_constructed (GObject *object)
{
    EAlert *alert;
    EAlertDialog *dialog;
    GtkWidget *action_area;
    GtkWidget *content_area;
    GtkWidget *container;
    GtkWidget *widget;
    PangoAttribute *attr;
    PangoAttrList *list;
    GList *actions;
    const gchar *primary, *secondary;
    gint default_response;

    /* Chain up to parent's constructed() method. */
    G_OBJECT_CLASS (e_alert_dialog_parent_class)->constructed (object);

    dialog = E_ALERT_DIALOG (object);
    alert = e_alert_dialog_get_alert (dialog);

    default_response = e_alert_get_default_response (alert);

    gtk_window_set_title (GTK_WINDOW (dialog), " ");

    /* XXX Making the window non-resizable is the only way at
     *     present for GTK+ to pick a reasonable default size.
     *     See https://bugzilla.gnome.org/681937 for details. */
    gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);

    action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
    content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));

    gtk_widget_ensure_style (GTK_WIDGET (dialog));
    gtk_container_set_border_width (GTK_CONTAINER (action_area), 12);
    gtk_container_set_border_width (GTK_CONTAINER (content_area), 0);

    gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);

    /* Forward EAlert::response signals to GtkDialog::response. */
    g_signal_connect_swapped (
        alert, "response",
        G_CALLBACK (gtk_dialog_response), dialog);

    /* Add buttons from actions. */
    actions = e_alert_peek_actions (alert);
    if (!actions) {
        GtkAction *action;

        /* Make sure there is at least one action, thus the dialog can be closed. */
        action = gtk_action_new (
            "alert-response-0", _("_Dismiss"), NULL, NULL);
        e_alert_add_action (alert, action, GTK_RESPONSE_CLOSE);
        g_object_unref (action);

        actions = e_alert_peek_actions (alert);
    }

    while (actions != NULL) {
        GtkWidget *button;
        gpointer data;

        /* These actions are already wired to trigger an
         * EAlert::response signal when activated, which
         * will in turn call to gtk_dialog_response(),
         * so we can add buttons directly to the action
         * area without knowing their response IDs.
         * (XXX Well, kind of.  See below.) */

        button = gtk_button_new ();

        gtk_widget_set_can_default (button, TRUE);

        gtk_activatable_set_related_action (
            GTK_ACTIVATABLE (button),
            GTK_ACTION (actions->data));

        gtk_box_pack_end (
            GTK_BOX (action_area),
            button, FALSE, FALSE, 0);

        /* This is set in e_alert_add_action(). */
        data = g_object_get_data (
            actions->data, "e-alert-response-id");

        /* Normally GtkDialog sets the initial focus widget to
         * the button corresponding to the default response, but
         * because the buttons are not directly tied to response
         * IDs, we have set both the default widget and the
         * initial focus widget ourselves. */
        if (GPOINTER_TO_INT (data) == default_response) {
            gtk_widget_grab_default (button);
            gtk_widget_grab_focus (button);
        }

        actions = g_list_next (actions);
    }

    widget = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
    gtk_container_set_border_width (GTK_CONTAINER (widget), 12);
    gtk_box_pack_start (GTK_BOX (content_area), widget, FALSE, FALSE, 0);
    gtk_widget_show (widget);

    container = widget;

    widget = e_alert_create_image (alert, GTK_ICON_SIZE_DIALOG);
    gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.0);
    gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
    gtk_widget_show (widget);

    widget = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
    gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
    dialog->priv->content_area = widget;
    gtk_widget_show (widget);

    container = widget;

    primary = e_alert_get_primary_text (alert);
    secondary = e_alert_get_secondary_text (alert);

    list = pango_attr_list_new ();
    attr = pango_attr_scale_new (PANGO_SCALE_LARGE);
    pango_attr_list_insert (list, attr);
    attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
    pango_attr_list_insert (list, attr);

    widget = gtk_label_new (primary);
    gtk_label_set_attributes (GTK_LABEL (widget), list);
    gtk_label_set_line_wrap (GTK_LABEL (widget), TRUE);
    gtk_label_set_selectable (GTK_LABEL (widget), TRUE);
    gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.0);
    gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
    gtk_widget_set_can_focus (widget, FALSE);
    gtk_widget_show (widget);

    widget = gtk_label_new (secondary);
    gtk_label_set_line_wrap (GTK_LABEL (widget), TRUE);
    gtk_label_set_selectable (GTK_LABEL (widget), TRUE);
    gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.0);
    gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
    gtk_widget_set_can_focus (widget, FALSE);
    gtk_widget_show (widget);

    pango_attr_list_unref (list);
}

static void
e_alert_dialog_class_init (EAlertDialogClass *class)
{
    GObjectClass *object_class;

    g_type_class_add_private (class, sizeof (EAlertDialogPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->set_property = alert_dialog_set_property;
    object_class->get_property = alert_dialog_get_property;
    object_class->dispose = alert_dialog_dispose;
    object_class->constructed = alert_dialog_constructed;

    g_object_class_install_property (
        object_class,
        PROP_ALERT,
        g_param_spec_object (
            "alert",
            "Alert",
            "Alert to be displayed",
            E_TYPE_ALERT,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT_ONLY |
            G_PARAM_STATIC_STRINGS));
}

static void
e_alert_dialog_init (EAlertDialog *dialog)
{
    dialog->priv = E_ALERT_DIALOG_GET_PRIVATE (dialog);
}

GtkWidget *
e_alert_dialog_new (GtkWindow *parent,
                    EAlert *alert)
{
    g_return_val_if_fail (E_IS_ALERT (alert), NULL);

    return g_object_new (
        E_TYPE_ALERT_DIALOG,
        "alert", alert, "transient-for", parent, NULL);
}

GtkWidget *
e_alert_dialog_new_for_args (GtkWindow *parent,
                             const gchar *tag,
                             ...)
{
    GtkWidget *dialog;
    EAlert *alert;
    va_list ap;

    g_return_val_if_fail (tag != NULL, NULL);

    va_start (ap, tag);
    alert = e_alert_new_valist (tag, ap);
    va_end (ap);

    dialog = e_alert_dialog_new (parent, alert);

    g_object_unref (alert);

    return dialog;
}

static gboolean
dialog_focus_in_event_cb (GtkWindow *dialog,
              GdkEvent *event,
              GtkWindow *parent)
{
    gtk_window_set_urgency_hint (parent, FALSE);

    return FALSE;
}

gint
e_alert_run_dialog (GtkWindow *parent,
                    EAlert *alert)
{
    GtkWidget *dialog;
    gint response;
    gulong signal_id = 0;

    g_return_val_if_fail (E_IS_ALERT (alert), 0);

    dialog = e_alert_dialog_new (parent, alert);

    if (parent) {
        gtk_window_set_urgency_hint (parent, TRUE);
        signal_id = g_signal_connect (dialog, "focus-in-event", G_CALLBACK (dialog_focus_in_event_cb), parent);
    } else {
        gtk_window_set_urgency_hint (GTK_WINDOW (dialog), TRUE);
    }

    response = gtk_dialog_run (GTK_DIALOG (dialog));

    if (parent) {
        gtk_window_set_urgency_hint (parent, FALSE);
        if (signal_id)
            g_signal_handler_disconnect (dialog, signal_id);
    }

    gtk_widget_destroy (dialog);

    return response;
}

gint
e_alert_run_dialog_for_args (GtkWindow *parent,
                             const gchar *tag,
                             ...)
{
    EAlert *alert;
    gint response;
    va_list ap;

    g_return_val_if_fail (tag != NULL, 0);

    va_start (ap, tag);
    alert = e_alert_new_valist (tag, ap);
    va_end (ap);

    response = e_alert_run_dialog (parent, alert);

    g_object_unref (alert);

    return response;
}

/**
 * e_alert_dialog_get_alert:
 * @dialog: an #EAlertDialog
 *
 * Returns the #EAlert associated with @dialog.
 *
 * Returns: the #EAlert associated with @dialog
 **/
EAlert *
e_alert_dialog_get_alert (EAlertDialog *dialog)
{
    g_return_val_if_fail (E_IS_ALERT_DIALOG (dialog), NULL);

    return dialog->priv->alert;
}

/**
 * e_alert_dialog_get_content_area:
 * @dialog: an #EAlertDialog
 *
 * Returns the vertical box containing the primary and secondary labels.
 * Use this to pack additional widgets into the dialog with the proper
 * horizontal alignment (maintaining the left margin below the image).
 *
 * Returns: the content area #GtkBox
 **/
GtkWidget *
e_alert_dialog_get_content_area (EAlertDialog *dialog)
{
    g_return_val_if_fail (E_IS_ALERT_DIALOG (dialog), NULL);

    return dialog->priv->content_area;
}