]> www.fi.muni.cz Git - evince.git/blob - shell/ev-message-area.c
Include config.h. Bug #504721.
[evince.git] / shell / ev-message-area.c
1 /* ev-message-area.c
2  *  this file is part of evince, a gnome document viewer
3  *
4  * Copyright (C) 2007 Carlos Garcia Campos
5  *
6  * Author:
7  *   Carlos Garcia Campos <carlosgc@gnome.org>
8  *
9  * Evince is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Evince is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  */
23
24 #include <config.h>
25 #include <gtk/gtkaccessible.h>
26 #include <gtk/gtklabel.h>
27 #include <gtk/gtkhbox.h>
28 #include <gtk/gtkvbox.h>
29 #include <gtk/gtkimage.h>
30 #include <gtk/gtkstock.h>
31
32 #include "ev-message-area.h"
33
34 #define EV_MESSAGE_AREA_GET_PRIVATE(obj) \
35         (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EV_TYPE_MESSAGE_AREA, EvMessageAreaPrivate))
36
37 struct _EvMessageAreaPrivate {
38         GtkWidget *image;
39         GtkWidget *label;
40         GtkWidget *secondary_label;
41         
42         guint      message_type : 3;
43 };
44
45 enum {
46         PROP_0,
47         PROP_MESSAGE_TYPE,
48         PROP_TEXT,
49         PROP_SECONDARY_TEXT,
50         PROP_IMAGE
51 };
52
53 static void ev_message_area_set_property (GObject      *object,
54                                           guint         prop_id,
55                                           const GValue *value,
56                                           GParamSpec   *pspec);
57 static void ev_message_area_get_property (GObject      *object,
58                                           guint         prop_id,
59                                           GValue       *value,
60                                           GParamSpec   *pspec);
61
62 G_DEFINE_TYPE (EvMessageArea, ev_message_area, GEDIT_TYPE_MESSAGE_AREA)
63
64 static void
65 ev_message_area_class_init (EvMessageAreaClass *class)
66 {
67         GObjectClass *gobject_class = G_OBJECT_CLASS (class);
68
69         gobject_class->set_property = ev_message_area_set_property;
70         gobject_class->get_property = ev_message_area_get_property;
71
72         g_object_class_install_property (gobject_class,
73                                          PROP_MESSAGE_TYPE,
74                                          g_param_spec_enum ("message-type",
75                                                             "Message Type",
76                                                             "The type of message",
77                                                             GTK_TYPE_MESSAGE_TYPE,
78                                                             GTK_MESSAGE_INFO,
79                                                             G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
80         g_object_class_install_property (gobject_class,
81                                          PROP_TEXT,
82                                          g_param_spec_string ("text",
83                                                               "Text",
84                                                               "The primary text of the message dialog",
85                                                               NULL,
86                                                               G_PARAM_READWRITE));
87         g_object_class_install_property (gobject_class,
88                                          PROP_SECONDARY_TEXT,
89                                          g_param_spec_string ("secondary-text",
90                                                               "Secondary Text",
91                                                               "The secondary text of the message dialog",
92                                                               NULL,
93                                                               G_PARAM_READWRITE));
94         g_object_class_install_property (gobject_class,
95                                          PROP_IMAGE,
96                                          g_param_spec_object ("image",
97                                                               "Image",
98                                                               "The image",
99                                                               GTK_TYPE_WIDGET,
100                                                               G_PARAM_READWRITE));
101
102         g_type_class_add_private (gobject_class, sizeof (EvMessageAreaPrivate));
103 }
104
105 static void
106 ev_message_area_init (EvMessageArea *area)
107 {
108         GtkWidget *hbox, *vbox;
109         
110         area->priv = EV_MESSAGE_AREA_GET_PRIVATE (area);
111
112         hbox = gtk_hbox_new (FALSE, 12);
113         vbox = gtk_vbox_new (FALSE, 12);
114         
115         area->priv->label = gtk_label_new (NULL);
116         gtk_label_set_use_markup (GTK_LABEL (area->priv->label), TRUE);
117         gtk_label_set_line_wrap (GTK_LABEL (area->priv->label), TRUE);
118         gtk_label_set_selectable (GTK_LABEL (area->priv->label), TRUE);
119         gtk_misc_set_alignment (GTK_MISC (area->priv->label), 0.0, 0.5);
120         GTK_WIDGET_SET_FLAGS (area->priv->label, GTK_CAN_FOCUS);
121         gtk_box_pack_start (GTK_BOX (vbox), area->priv->label, TRUE, TRUE, 0);
122         gtk_widget_show (area->priv->label);
123         
124         area->priv->secondary_label = gtk_label_new (NULL);
125         gtk_label_set_use_markup (GTK_LABEL (area->priv->secondary_label), TRUE);
126         gtk_label_set_line_wrap (GTK_LABEL (area->priv->secondary_label), TRUE);
127         gtk_label_set_selectable (GTK_LABEL (area->priv->secondary_label), TRUE);
128         gtk_misc_set_alignment (GTK_MISC (area->priv->secondary_label), 0.0, 0.5);
129         GTK_WIDGET_SET_FLAGS (area->priv->secondary_label, GTK_CAN_FOCUS);
130         gtk_box_pack_start (GTK_BOX (vbox), area->priv->secondary_label, TRUE, TRUE, 0);
131
132         area->priv->image = gtk_image_new_from_stock (NULL, GTK_ICON_SIZE_DIALOG);
133         gtk_misc_set_alignment (GTK_MISC (area->priv->image), 0.5, 0.0);
134         gtk_box_pack_start (GTK_BOX (hbox), area->priv->image, FALSE, FALSE, 0);
135         gtk_widget_show (area->priv->image);
136
137         gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
138         gtk_widget_show (vbox);
139
140         gedit_message_area_set_contents (GEDIT_MESSAGE_AREA (area), hbox);
141         gtk_widget_show (hbox);
142 }
143
144 static void
145 ev_message_area_set_image_for_type (EvMessageArea *area,
146                                     GtkMessageType type)
147 {
148         const gchar *stock_id = NULL;
149         AtkObject   *atk_obj;
150
151         switch (type) {
152         case GTK_MESSAGE_INFO:
153                 stock_id = GTK_STOCK_DIALOG_INFO;
154                 break;
155         case GTK_MESSAGE_QUESTION:
156                 stock_id = GTK_STOCK_DIALOG_QUESTION;
157                 break;
158         case GTK_MESSAGE_WARNING:
159                 stock_id = GTK_STOCK_DIALOG_WARNING;
160                 break;
161         case GTK_MESSAGE_ERROR:
162                 stock_id = GTK_STOCK_DIALOG_ERROR;
163                 break;
164         case GTK_MESSAGE_OTHER:
165                 break;
166         default:
167                 g_warning ("Unknown GtkMessageType %u", type);
168                 break;
169         }
170
171         if (stock_id)
172                 gtk_image_set_from_stock (GTK_IMAGE (area->priv->image), stock_id,
173                                           GTK_ICON_SIZE_DIALOG);
174
175         atk_obj = gtk_widget_get_accessible (GTK_WIDGET (area));
176         if (GTK_IS_ACCESSIBLE (atk_obj)) {
177                 atk_object_set_role (atk_obj, ATK_ROLE_ALERT);
178                 if (stock_id) {
179                         GtkStockItem item;
180
181                         gtk_stock_lookup (stock_id, &item);
182                         atk_object_set_name (atk_obj, item.label);
183                 }
184         }
185 }
186
187 static void
188 ev_message_area_set_property (GObject      *object,
189                               guint         prop_id,
190                               const GValue *value,
191                               GParamSpec   *pspec)
192 {
193         EvMessageArea *area = EV_MESSAGE_AREA (object);
194
195         switch (prop_id) {
196         case PROP_MESSAGE_TYPE:
197                 area->priv->message_type = g_value_get_enum (value);
198                 ev_message_area_set_image_for_type (area, area->priv->message_type);
199                 break;
200         case PROP_TEXT:
201                 ev_message_area_set_text (area, g_value_get_string (value));
202                 break;
203         case PROP_SECONDARY_TEXT:
204                 ev_message_area_set_secondary_text (area, g_value_get_string (value));
205                 break;
206         case PROP_IMAGE:
207                 ev_message_area_set_image (area, (GtkWidget *)g_value_get_object (value));
208                 break;
209         default:
210                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
211                 break;
212         }
213 }
214
215 static void
216 ev_message_area_get_property (GObject     *object,
217                               guint        prop_id,
218                               GValue      *value,
219                               GParamSpec  *pspec)
220 {
221         EvMessageArea *area = EV_MESSAGE_AREA (object);
222
223         switch (prop_id) {
224         case PROP_MESSAGE_TYPE:
225                 g_value_set_enum (value, (GtkMessageType) area->priv->message_type);
226                 break;
227         case PROP_TEXT:
228                 g_value_set_string (value, gtk_label_get_label (GTK_LABEL (area->priv->label)));
229                 break;
230         case PROP_SECONDARY_TEXT:
231                 g_value_set_string (value, gtk_label_get_label (GTK_LABEL (area->priv->secondary_label)));
232                 break;
233         case PROP_IMAGE:
234                 g_value_set_object (value, area->priv->image);
235                 break;
236         default:
237                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238                 break;
239         }
240 }
241
242 GtkWidget *
243 ev_message_area_new (GtkMessageType type,
244                      const gchar   *text,
245                      const gchar   *first_button_text,
246                      ...)
247 {
248         GtkWidget *widget;
249
250         widget = g_object_new (EV_TYPE_MESSAGE_AREA,
251                                "message-type", type,
252                                "text", text,
253                                NULL);
254         if (first_button_text) {
255                 va_list args;
256                 
257                 va_start (args, first_button_text);
258                 gedit_message_area_add_buttons_valist (GEDIT_MESSAGE_AREA (widget),
259                                                        first_button_text,
260                                                        args);
261                 va_end (args);
262         }
263
264         return widget;
265 }
266
267 void
268 ev_message_area_set_image (EvMessageArea *area,
269                            GtkWidget     *image)
270 {
271         GtkWidget *parent;
272
273         g_return_if_fail (EV_IS_MESSAGE_AREA (area));
274
275         area->priv->message_type = GTK_MESSAGE_OTHER;
276
277         parent = area->priv->image->parent;
278         gtk_container_add (GTK_CONTAINER (parent), image);
279         gtk_container_remove (GTK_CONTAINER (parent), area->priv->image);
280         gtk_box_reorder_child (GTK_BOX (parent), image, 0);
281
282         area->priv->image = image;
283
284         g_object_notify (G_OBJECT (area), "image");
285 }
286
287 void
288 ev_message_area_set_text (EvMessageArea *area,
289                           const gchar   *str)
290 {
291         g_return_if_fail (EV_IS_MESSAGE_AREA (area));
292
293         if (str) {
294                 gchar *msg;
295
296                 msg = g_strdup_printf ("<b>%s</b>", str);
297                 gtk_label_set_markup (GTK_LABEL (area->priv->label), msg);
298                 g_free (msg);
299         } else {
300                 gtk_label_set_markup (GTK_LABEL (area->priv->label), NULL);
301         }
302
303         g_object_notify (G_OBJECT (area), "text");
304 }
305
306 void
307 ev_message_area_set_secondary_text (EvMessageArea *area,
308                                     const gchar   *str)
309 {
310         g_return_if_fail (EV_IS_MESSAGE_AREA (area));
311
312         if (str) {
313                 gchar *msg;
314
315                 msg = g_strdup_printf ("<small>%s</small>", str);
316                 gtk_label_set_markup (GTK_LABEL (area->priv->secondary_label), msg);
317                 g_free (msg);
318                 gtk_widget_show (area->priv->secondary_label);
319         } else {
320                 gtk_label_set_markup (GTK_LABEL (area->priv->secondary_label), NULL);
321                 gtk_widget_hide (area->priv->secondary_label);
322         }
323
324         g_object_notify (G_OBJECT (area), "secondary-text");
325 }