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
|
/*
* e-alert-bar.c
*
* 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/>
*
*/
#include "e-alert-bar.h"
#include <config.h>
#include <glib/gi18n-lib.h>
#define E_ALERT_BAR_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE \
((obj), E_TYPE_ALERT_BAR, EAlertBarPrivate))
#define ICON_SIZE GTK_ICON_SIZE_DIALOG
struct _EAlertBarPrivate {
GQueue alerts;
GtkWidget *image; /* not referenced */
GtkWidget *primary_label; /* not referenced */
GtkWidget *secondary_label; /* not referenced */
};
G_DEFINE_TYPE (
EAlertBar,
e_alert_bar,
GTK_TYPE_INFO_BAR)
static void
alert_bar_show_alert (EAlertBar *alert_bar)
{
GtkImage *image;
GtkLabel *label;
GtkInfoBar *info_bar;
GtkWidget *action_area;
EAlertButton *buttons;
EAlert *alert;
GList *children;
GtkMessageType message_type;
const gchar *stock_id;
const gchar *text;
gint response_id;
info_bar = GTK_INFO_BAR (alert_bar);
action_area = gtk_info_bar_get_action_area (info_bar);
alert = g_queue_peek_head (&alert_bar->priv->alerts);
g_return_if_fail (E_IS_ALERT (alert));
/* Remove all buttons from the previous alert. */
children = gtk_container_get_children (GTK_CONTAINER (action_area));
while (children != NULL) {
GtkWidget *child = GTK_WIDGET (children->data);
gtk_container_remove (GTK_CONTAINER (action_area), child);
children = g_list_delete_link (children, children);
}
/* Add new buttons. */
buttons = e_alert_peek_buttons (alert);
if (buttons == NULL) {
gtk_info_bar_add_button (
info_bar, _("_Dismiss"), GTK_RESPONSE_CLOSE);
} else while (buttons != NULL) {
const gchar *button_text;
if (buttons->stock != NULL)
button_text = buttons->stock;
else
button_text = buttons->label;
gtk_info_bar_add_button (
info_bar, button_text, buttons->response);
buttons = buttons->next;
}
response_id = e_alert_get_default_response (alert);
gtk_info_bar_set_default_response (info_bar, response_id);
message_type = e_alert_get_message_type (alert);
gtk_info_bar_set_message_type (info_bar, message_type);
text = e_alert_get_primary_text (alert);
label = GTK_LABEL (alert_bar->priv->primary_label);
gtk_label_set_text (label, text);
text = e_alert_get_secondary_text (alert);
label = GTK_LABEL (alert_bar->priv->secondary_label);
gtk_label_set_text (label, text);
stock_id = e_alert_get_stock_id (alert);
image = GTK_IMAGE (alert_bar->priv->image);
gtk_image_set_from_stock (image, stock_id, ICON_SIZE);
gtk_widget_show (GTK_WIDGET (alert_bar));
}
static void
alert_bar_dispose (GObject *object)
{
EAlertBarPrivate *priv;
priv = E_ALERT_BAR_GET_PRIVATE (object);
while (!g_queue_is_empty (&priv->alerts))
g_object_unref (g_queue_pop_head (&priv->alerts));
/* Chain up to parent's dispose() method. */
G_OBJECT_CLASS (e_alert_bar_parent_class)->dispose (object);
}
static void
alert_bar_response (GtkInfoBar *info_bar,
gint response_id)
{
EAlertBar *alert_bar;
EAlert *alert;
alert_bar = E_ALERT_BAR (info_bar);
alert = g_queue_pop_head (&alert_bar->priv->alerts);
e_alert_response (alert, response_id);
g_object_unref (alert);
if (!g_queue_is_empty (&alert_bar->priv->alerts))
alert_bar_show_alert (alert_bar);
else
gtk_widget_hide (GTK_WIDGET (alert_bar));
}
static void
e_alert_bar_class_init (EAlertBarClass *class)
{
GObjectClass *object_class;
GtkInfoBarClass *info_bar_class;
g_type_class_add_private (class, sizeof (EAlertBarPrivate));
object_class = G_OBJECT_CLASS (class);
object_class->dispose = alert_bar_dispose;
info_bar_class = GTK_INFO_BAR_CLASS (class);
info_bar_class->response = alert_bar_response;
}
static void
e_alert_bar_init (EAlertBar *alert_bar)
{
GtkWidget *container;
GtkWidget *widget;
PangoAttribute *attr;
PangoAttrList *attr_list;
alert_bar->priv = E_ALERT_BAR_GET_PRIVATE (alert_bar);
g_queue_init (&alert_bar->priv->alerts);
container = gtk_info_bar_get_content_area (GTK_INFO_BAR (alert_bar));
widget = gtk_hbox_new (FALSE, 12);
gtk_container_add (GTK_CONTAINER (container), widget);
gtk_widget_show (widget);
container = widget;
widget = gtk_image_new ();
gtk_misc_set_alignment (GTK_MISC (widget), 0.5, 0.0);
gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
alert_bar->priv->image = widget;
gtk_widget_show (widget);
widget = gtk_vbox_new (FALSE, 12);
gtk_box_pack_start (GTK_BOX (container), widget, TRUE, TRUE, 0);
gtk_widget_show (widget);
container = widget;
attr_list = pango_attr_list_new ();
attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
pango_attr_list_insert (attr_list, attr);
widget = gtk_label_new (NULL);
gtk_label_set_attributes (GTK_LABEL (widget), attr_list);
gtk_label_set_line_wrap (GTK_LABEL (widget), TRUE);
gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
alert_bar->priv->primary_label = widget;
gtk_widget_show (widget);
pango_attr_list_unref (attr_list);
attr_list = pango_attr_list_new ();
attr = pango_attr_scale_new (PANGO_SCALE_SMALL);
pango_attr_list_insert (attr_list, attr);
widget = gtk_label_new (NULL);
gtk_label_set_attributes (GTK_LABEL (widget), attr_list);
gtk_label_set_line_wrap (GTK_LABEL (widget), TRUE);
gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
alert_bar->priv->secondary_label = widget;
gtk_widget_show (widget);
pango_attr_list_unref (attr_list);
}
GtkWidget *
e_alert_bar_new (void)
{
return g_object_new (E_TYPE_ALERT_BAR, NULL);
}
void
e_alert_bar_add_alert (EAlertBar *alert_bar,
EAlert *alert)
{
g_return_if_fail (E_IS_ALERT_BAR (alert_bar));
g_return_if_fail (E_IS_ALERT (alert));
g_queue_push_head (&alert_bar->priv->alerts, g_object_ref (alert));
alert_bar_show_alert (alert_bar);
}
|