]> Pileus Git - ~andy/gtk/blob - gtk/gtkinfobar.c
GtkInfoBar: use private pointer instead GET_PRIV() macro
[~andy/gtk] / gtk / gtkinfobar.c
1 /*
2  * gtkinfobar.c
3  * This file is part of GTK+
4  *
5  * Copyright (C) 2005 - Paolo Maggi
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /*
24  * Modified by the gedit Team, 2005. See the AUTHORS file for a
25  * list of people on the gtk Team.
26  * See the gedit ChangeLog files for a list of changes.
27  *
28  * Modified by the GTK+ team, 2008-2009.
29  */
30
31
32 #include "config.h"
33
34 #include <stdlib.h>
35
36 #include "gtkinfobar.h"
37 #include "gtkaccessible.h"
38 #include "gtkbuildable.h"
39 #include "gtkbox.h"
40 #include "gtkvbbox.h"
41 #include "gtklabel.h"
42 #include "gtkbutton.h"
43 #include "gtkenums.h"
44 #include "gtkbindings.h"
45 #include "gtkdialog.h"
46 #include "gtkintl.h"
47 #include "gtkprivate.h"
48 #include "gtkstock.h"
49 #include "gdkkeysyms.h"
50
51 /**
52  * SECTION:gtkinfobar
53  * @short_description: Report important messages to the user
54  * @include: gtk/gtk.h
55  * @see_also: #GtkStatusbar, #GtkMessageDialog
56  *
57  * #GtkInfoBar is a widget that can be used to show messages to
58  * the user without showing a dialog. It is often temporarily shown
59  * at the top or bottom of a document. In contrast to #GtkDialog, which
60  * has a horizontal action area at the bottom, #GtkInfoBar has a
61  * vertical action area at the side.
62  *
63  * The API of #GtkInfoBar is very similar to #GtkDialog, allowing you
64  * to add buttons to the action area with gtk_info_bar_add_button() or
65  * gtk_info_bar_new_with_buttons(). The sensitivity of action widgets
66  * can be controlled with gtk_info_bar_set_response_sensitive().
67  * To add widgets to the main content area of a #GtkInfoBar, use
68  * gtk_info_bar_get_content_area() and add your widgets to the container.
69  *
70  * Similar to #GtkMessageDialog, the contents of a #GtkInfoBar can by
71  * classified as error message, warning, informational message, etc,
72  * by using gtk_info_bar_set_message_type(). GTK+ uses the message type
73  * to determine the background color of the message area.
74  *
75  * <example>
76  * <title>Simple GtkInfoBar usage.</title>
77  * <programlisting>
78  * /&ast; set up info bar &ast;/
79  * info_bar = gtk_info_bar_new ();
80  * gtk_widget_set_no_show_all (info_bar, TRUE);
81  * message_label = gtk_label_new ("");
82  * gtk_widget_show (message_label);
83  * content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
84  * gtk_container_add (GTK_CONTAINER (content_area), message_label);
85  * gtk_info_bar_add_button (GTK_INFO_BAR (info_bar),
86  *                          GTK_STOCK_OK, GTK_RESPONSE_OK);
87  * g_signal_connect (info_bar, "response",
88  *                   G_CALLBACK (gtk_widget_hide), NULL);
89  * gtk_table_attach (GTK_TABLE (table),
90  *                   info_bar,
91  *                   0, 1, 2, 3,
92  *                   GTK_EXPAND | GTK_FILL,  0,
93  *                   0,                      0);
94  *
95  * /&ast; ... &ast;/
96  *
97  * /&ast; show an error message &ast;/
98  * gtk_label_set_text (GTK_LABEL (message_label), error_message);
99  * gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
100  *                                GTK_MESSAGE_ERROR);
101  * gtk_widget_show (info_bar);
102  * </programlisting>
103  * </example>
104  *
105  * <refsect2 id="GtkInfoBar-BUILDER-UI">
106  * <title>GtkInfoBar as GtkBuildable</title>
107  * <para>
108  * The GtkInfoBar implementation of the GtkBuildable interface exposes
109  * the content area and action area as internal children with the names
110  * "content_area" and "action_area".
111  * </para>
112  * <para>
113  * GtkInfoBar supports a custom &lt;action-widgets&gt; element, which
114  * can contain multiple &lt;action-widget&gt; elements. The "response"
115  * attribute specifies a numeric response, and the content of the element
116  * is the id of widget (which should be a child of the dialogs @action_area).
117  * </para>
118  * </refsect2>
119  */
120
121 enum
122 {
123   PROP_0,
124   PROP_MESSAGE_TYPE
125 };
126
127 struct _GtkInfoBarPrivate
128 {
129   GtkWidget *content_area;
130   GtkWidget *action_area;
131
132   GtkMessageType message_type;
133 };
134
135 typedef struct _ResponseData ResponseData;
136
137 struct _ResponseData
138 {
139   gint response_id;
140 };
141
142 enum
143 {
144   RESPONSE,
145   CLOSE,
146   LAST_SIGNAL
147 };
148
149 static guint signals[LAST_SIGNAL];
150
151
152 static void     gtk_info_bar_set_property (GObject        *object,
153                                            guint           prop_id,
154                                            const GValue   *value,
155                                            GParamSpec     *pspec);
156 static void     gtk_info_bar_get_property (GObject        *object,
157                                            guint           prop_id,
158                                            GValue         *value,
159                                            GParamSpec     *pspec);
160 static void     gtk_info_bar_style_set    (GtkWidget      *widget,
161                                            GtkStyle       *prev_style);
162 static gboolean gtk_info_bar_expose       (GtkWidget      *widget,
163                                            GdkEventExpose *event);
164 static void     gtk_info_bar_buildable_interface_init     (GtkBuildableIface *iface);
165 static GObject *gtk_info_bar_buildable_get_internal_child (GtkBuildable  *buildable,
166                                                            GtkBuilder    *builder,
167                                                            const gchar   *childname);
168 static gboolean  gtk_info_bar_buildable_custom_tag_start   (GtkBuildable  *buildable,
169                                                             GtkBuilder    *builder,
170                                                             GObject       *child,
171                                                             const gchar   *tagname,
172                                                             GMarkupParser *parser,
173                                                             gpointer      *data);
174 static void      gtk_info_bar_buildable_custom_finished    (GtkBuildable  *buildable,
175                                                             GtkBuilder    *builder,
176                                                             GObject       *child,
177                                                             const gchar   *tagname,
178                                                             gpointer       user_data);
179
180
181 G_DEFINE_TYPE_WITH_CODE (GtkInfoBar, gtk_info_bar, GTK_TYPE_HBOX,
182                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
183                                                 gtk_info_bar_buildable_interface_init))
184
185 static void
186 gtk_info_bar_set_property (GObject      *object,
187                            guint         prop_id,
188                            const GValue *value,
189                            GParamSpec   *pspec)
190 {
191   GtkInfoBar *info_bar;
192   GtkInfoBarPrivate *priv;
193
194   info_bar = GTK_INFO_BAR (object);
195   priv = info_bar->priv;
196
197   switch (prop_id)
198     {
199     case PROP_MESSAGE_TYPE:
200       gtk_info_bar_set_message_type (info_bar, g_value_get_enum (value));
201       break;
202     default:
203       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
204       break;
205     }
206 }
207
208 static void
209 gtk_info_bar_get_property (GObject    *object,
210                            guint       prop_id,
211                            GValue     *value,
212                            GParamSpec *pspec)
213 {
214   GtkInfoBar *info_bar;
215   GtkInfoBarPrivate *priv;
216
217   info_bar = GTK_INFO_BAR (object);
218   priv = info_bar->priv;
219
220   switch (prop_id)
221     {
222     case PROP_MESSAGE_TYPE:
223       g_value_set_enum (value, gtk_info_bar_get_message_type (info_bar));
224       break;
225     default:
226       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
227       break;
228     }
229 }
230
231 static void
232 gtk_info_bar_finalize (GObject *object)
233 {
234   G_OBJECT_CLASS (gtk_info_bar_parent_class)->finalize (object);
235 }
236
237 static void
238 response_data_free (gpointer data)
239 {
240   g_slice_free (ResponseData, data);
241 }
242
243 static ResponseData *
244 get_response_data (GtkWidget *widget,
245                    gboolean   create)
246 {
247   ResponseData *ad = g_object_get_data (G_OBJECT (widget),
248                                         "gtk-info-bar-response-data");
249
250   if (ad == NULL && create)
251     {
252       ad = g_slice_new (ResponseData);
253
254       g_object_set_data_full (G_OBJECT (widget),
255                               I_("gtk-info-bar-response-data"),
256                               ad,
257                               response_data_free);
258     }
259
260   return ad;
261 }
262
263 static GtkWidget *
264 find_button (GtkInfoBar *info_bar,
265              gint        response_id)
266 {
267   GList *children, *list;
268   GtkWidget *child = NULL;
269
270   children = gtk_container_get_children (GTK_CONTAINER (info_bar->priv->action_area));
271
272   for (list = children; list; list = list->next)
273     {
274       ResponseData *rd = get_response_data (list->data, FALSE);
275
276       if (rd && rd->response_id == response_id)
277         {
278           child = list->data;
279           break;
280         }
281     }
282
283   g_list_free (children);
284
285   return child;
286 }
287
288 static void
289 gtk_info_bar_close (GtkInfoBar *info_bar)
290 {
291   if (!find_button (info_bar, GTK_RESPONSE_CANCEL))
292     return;
293
294   gtk_info_bar_response (GTK_INFO_BAR (info_bar),
295                          GTK_RESPONSE_CANCEL);
296 }
297
298 static gboolean
299 gtk_info_bar_expose (GtkWidget      *widget,
300                      GdkEventExpose *event)
301 {
302   GtkInfoBarPrivate *priv = GTK_INFO_BAR (widget)->priv;
303   const char* type_detail[] = {
304     "infobar-info",
305     "infobar-warning",
306     "infobar-question",
307     "infobar-error",
308     "infobar"
309   };
310
311   if (priv->message_type != GTK_MESSAGE_OTHER)
312     {
313       const char *detail;
314
315       detail = type_detail[priv->message_type];
316
317       gtk_paint_box (widget->style,
318                      widget->window,
319                      GTK_STATE_NORMAL,
320                      GTK_SHADOW_OUT,
321                      NULL,
322                      widget,
323                      detail,
324                      widget->allocation.x,
325                      widget->allocation.y,
326                      widget->allocation.width,
327                      widget->allocation.height);
328     }
329
330   if (GTK_WIDGET_CLASS (gtk_info_bar_parent_class)->expose_event)
331     GTK_WIDGET_CLASS (gtk_info_bar_parent_class)->expose_event (widget, event);
332
333   return FALSE;
334 }
335
336 static void
337 gtk_info_bar_class_init (GtkInfoBarClass *klass)
338 {
339   GtkWidgetClass *widget_class;
340   GObjectClass *object_class;
341   GtkBindingSet *binding_set;
342
343   widget_class = GTK_WIDGET_CLASS (klass);
344   object_class = G_OBJECT_CLASS (klass);
345
346   object_class->get_property = gtk_info_bar_get_property;
347   object_class->set_property = gtk_info_bar_set_property;
348   object_class->finalize = gtk_info_bar_finalize;
349
350   widget_class->style_set = gtk_info_bar_style_set;
351   widget_class->expose_event = gtk_info_bar_expose;
352
353   klass->close = gtk_info_bar_close;
354
355   /**
356    * GtkInfoBar:message-type:
357    *
358    * The type of the message.
359    *
360    * The type is used to determine the colors to use in the info bar.
361    * The following symbolic color names can by used to customize
362    * these colors:
363    * "info_fg_color", "info_bg_color",
364    * "warning_fg_color", "warning_bg_color",
365    * "question_fg_color", "question_bg_color",
366    * "error_fg_color", "error_bg_color".
367    * "other_fg_color", "other_bg_color".
368    *
369    * If the type is #GTK_MESSAGE_OTHER, no info bar is painted but the
370    * colors are still set.
371    *
372    * Since: 2.18
373    */
374   g_object_class_install_property (object_class,
375                                    PROP_MESSAGE_TYPE,
376                                    g_param_spec_enum ("message-type",
377                                                       P_("Message Type"),
378                                                       P_("The type of message"),
379                                                       GTK_TYPE_MESSAGE_TYPE,
380                                                       GTK_MESSAGE_INFO,
381                                                       GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
382   /**
383    * GtkInfoBar::response:
384    * @info_bar: the object on which the signal is emitted
385    * @response_id: the response ID
386    *
387    * Emitted when an action widget is clicked or the application programmer
388    * calls gtk_dialog_response(). The @response_id depends on which action
389    * widget was clicked.
390    *
391    * Since: 2.18
392    */
393   signals[RESPONSE] = g_signal_new (I_("response"),
394                                     G_OBJECT_CLASS_TYPE (klass),
395                                     G_SIGNAL_RUN_LAST,
396                                     G_STRUCT_OFFSET (GtkInfoBarClass, response),
397                                     NULL, NULL,
398                                     g_cclosure_marshal_VOID__INT,
399                                     G_TYPE_NONE, 1,
400                                     G_TYPE_INT);
401
402   /**
403    * GtkInfoBar::close:
404    *
405    * The ::close signal is a
406    * <link linkend="keybinding-signals">keybinding signal</link>
407    * which gets emitted when the user uses a keybinding to dismiss
408    * the info bar.
409    *
410    * The default binding for this signal is the Escape key.
411    *
412    * Since: 2.18
413    */
414   signals[CLOSE] =  g_signal_new (I_("close"),
415                                   G_OBJECT_CLASS_TYPE (klass),
416                                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
417                                   G_STRUCT_OFFSET (GtkInfoBarClass, close),
418                                   NULL, NULL,
419                                   g_cclosure_marshal_VOID__VOID,
420                                   G_TYPE_NONE, 0);
421
422   /**
423    * GtkInfoBar:content-area-border:
424    *
425    * The width of the border around the content
426    * content area of the info bar.
427    *
428    * Since: 2.18
429    */
430   gtk_widget_class_install_style_property (widget_class,
431                                            g_param_spec_int ("content-area-border",
432                                                              P_("Content area border"),
433                                                              P_("Width of border around the content area"),
434                                                              0,
435                                                              G_MAXINT,
436                                                              8,
437                                                              GTK_PARAM_READABLE));
438
439   /**
440    * GtkInfoBar:content-area-spacing:
441    *
442    * The default spacing used between elements of the
443    * content area of the info bar.
444    *
445    * Since: 2.18
446    */
447   gtk_widget_class_install_style_property (widget_class,
448                                            g_param_spec_int ("content-area-spacing",
449                                                              P_("Content area spacing"),
450                                                              P_("Spacing between elements of the area"),
451                                                              0,
452                                                              G_MAXINT,
453                                                              16,
454                                                              GTK_PARAM_READABLE));
455
456   /**
457    * GtkInfoBar:button-spacing:
458    *
459    * Spacing between buttons in the action area of the info bar.
460    *
461    * Since: 2.18
462    */
463   gtk_widget_class_install_style_property (widget_class,
464                                            g_param_spec_int ("button-spacing",
465                                                              P_("Button spacing"),
466                                                              P_("Spacing between buttons"),
467                                                              0,
468                                                              G_MAXINT,
469                                                              6,
470                                                              GTK_PARAM_READABLE));
471
472   /**
473    * GtkInfoBar:action-area-border:
474    *
475    * Width of the border around the action area of the info bar.
476    *
477    * Since: 2.18
478    */
479   gtk_widget_class_install_style_property (widget_class,
480                                            g_param_spec_int ("action-area-border",
481                                                              P_("Action area border"),
482                                                              P_("Width of border around the action area"),
483                                                              0,
484                                                              G_MAXINT,
485                                                              5,
486                                                              GTK_PARAM_READABLE));
487
488   binding_set = gtk_binding_set_by_class (klass);
489
490   gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0, "close", 0);
491
492   g_type_class_add_private (object_class, sizeof (GtkInfoBarPrivate));
493 }
494
495 static void
496 gtk_info_bar_update_colors (GtkInfoBar *info_bar)
497 {
498   GtkWidget *widget = GTK_WIDGET (info_bar);
499   GtkInfoBarPrivate *priv = info_bar->priv;
500   GdkColor info_default_border_color     = { 0, 0xb800, 0xad00, 0x9d00 };
501   GdkColor info_default_fill_color       = { 0, 0xff00, 0xff00, 0xbf00 };
502   GdkColor warning_default_border_color  = { 0, 0xb000, 0x7a00, 0x2b00 };
503   GdkColor warning_default_fill_color    = { 0, 0xfc00, 0xaf00, 0x3e00 };
504   GdkColor question_default_border_color = { 0, 0x6200, 0x7b00, 0xd960 };
505   GdkColor question_default_fill_color   = { 0, 0x8c00, 0xb000, 0xd700 };
506   GdkColor error_default_border_color    = { 0, 0xa800, 0x2700, 0x2700 };
507   GdkColor error_default_fill_color      = { 0, 0xf000, 0x3800, 0x3800 };
508   GdkColor other_default_border_color    = { 0, 0xb800, 0xad00, 0x9d00 };
509   GdkColor other_default_fill_color      = { 0, 0xff00, 0xff00, 0xbf00 };
510   GdkColor *fg, *bg;
511   GdkColor sym_fg, sym_bg;
512   GtkStyle *style;
513   const char* fg_color_name[] = {
514     "info_fg_color",
515     "warning_fg_color",
516     "question_fg_color",
517     "error_fg_color",
518     "other_fg_color"
519   };
520   const char* bg_color_name[] = {
521     "info_bg_color",
522     "warning_bg_color",
523     "question_bg_color",
524     "error_bg_color",
525     "other_bg_color"
526   };
527
528   style = gtk_widget_get_style (widget);
529
530   if (gtk_style_lookup_color (style, fg_color_name[priv->message_type], &sym_fg) &&
531       gtk_style_lookup_color (style, bg_color_name[priv->message_type], &sym_bg))
532     {
533       fg = &sym_fg;
534       bg = &sym_bg;
535     }
536   else
537     {
538       switch (priv->message_type)
539         {
540         case GTK_MESSAGE_INFO:
541           fg = &info_default_border_color;
542           bg = &info_default_fill_color;
543           break;
544
545         case GTK_MESSAGE_WARNING:
546           fg = &warning_default_border_color;
547           bg = &warning_default_fill_color;
548           break;
549
550         case GTK_MESSAGE_QUESTION:
551           fg = &question_default_border_color;
552           bg = &question_default_fill_color;
553           break;
554
555         case GTK_MESSAGE_ERROR:
556           fg = &error_default_border_color;
557           bg = &error_default_fill_color;
558           break;
559
560         case GTK_MESSAGE_OTHER:
561           fg = &other_default_border_color;
562           bg = &other_default_fill_color;
563           break;
564
565         default:
566           g_assert_not_reached();
567           fg = NULL;
568           bg = NULL;
569         }
570     }
571
572   if (!gdk_color_equal (bg, &widget->style->bg[GTK_STATE_NORMAL]))
573     gtk_widget_modify_bg (widget, GTK_STATE_NORMAL, bg);
574   if (!gdk_color_equal (fg, &widget->style->fg[GTK_STATE_NORMAL]))
575     gtk_widget_modify_fg (widget, GTK_STATE_NORMAL, fg);
576 }
577
578 static void
579 gtk_info_bar_style_set (GtkWidget *widget,
580                         GtkStyle  *prev_style)
581 {
582   GtkInfoBar *info_bar = GTK_INFO_BAR (widget);
583   gint button_spacing;
584   gint action_area_border;
585   gint content_area_spacing;
586   gint content_area_border;
587
588   gtk_widget_style_get (widget,
589                         "button-spacing", &button_spacing,
590                         "action-area-border", &action_area_border,
591                         "content-area-spacing", &content_area_spacing,
592                         "content-area-border", &content_area_border,
593                         NULL);
594
595   gtk_box_set_spacing (GTK_BOX (info_bar->priv->action_area), button_spacing);
596   gtk_container_set_border_width (GTK_CONTAINER (info_bar->priv->action_area),
597                                   action_area_border);
598   gtk_box_set_spacing (GTK_BOX (info_bar->priv->content_area), content_area_spacing);
599   gtk_container_set_border_width (GTK_CONTAINER (info_bar->priv->content_area),
600                                   content_area_border);
601
602   gtk_info_bar_update_colors (info_bar);
603 }
604
605 static void
606 gtk_info_bar_init (GtkInfoBar *info_bar)
607 {
608   GtkWidget *content_area;
609   GtkWidget *action_area;
610
611   gtk_widget_push_composite_child ();
612
613   info_bar->priv = G_TYPE_INSTANCE_GET_PRIVATE (info_bar,
614                                                 GTK_TYPE_INFO_BAR,
615                                                 GtkInfoBarPrivate);
616
617   content_area = gtk_hbox_new (FALSE, 0);
618   gtk_widget_show (content_area);
619   gtk_box_pack_start (GTK_BOX (info_bar), content_area, TRUE, TRUE, 0);
620
621   action_area = gtk_vbutton_box_new ();
622   gtk_widget_show (action_area);
623   gtk_button_box_set_layout (GTK_BUTTON_BOX (action_area), GTK_BUTTONBOX_END);
624   gtk_box_pack_start (GTK_BOX (info_bar), action_area, FALSE, TRUE, 0);
625
626   gtk_widget_set_app_paintable (GTK_WIDGET (info_bar), TRUE);
627   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (info_bar), TRUE);
628
629   info_bar->priv->content_area = content_area;
630   info_bar->priv->action_area = action_area;
631
632   gtk_widget_pop_composite_child ();
633 }
634
635 static GtkBuildableIface *parent_buildable_iface;
636
637 static void
638 gtk_info_bar_buildable_interface_init (GtkBuildableIface *iface)
639 {
640   parent_buildable_iface = g_type_interface_peek_parent (iface);
641   iface->get_internal_child = gtk_info_bar_buildable_get_internal_child;
642   iface->custom_tag_start = gtk_info_bar_buildable_custom_tag_start;
643   iface->custom_finished = gtk_info_bar_buildable_custom_finished;
644 }
645
646 static GObject *
647 gtk_info_bar_buildable_get_internal_child (GtkBuildable *buildable,
648                                            GtkBuilder   *builder,
649                                            const gchar  *childname)
650 {
651   if (strcmp (childname, "content_area") == 0)
652     return G_OBJECT (GTK_INFO_BAR (buildable)->priv->content_area);
653   else if (strcmp (childname, "action_area") == 0)
654     return G_OBJECT (GTK_INFO_BAR (buildable)->priv->action_area);
655
656   return parent_buildable_iface->get_internal_child (buildable,
657                                                      builder,
658                                                      childname);
659 }
660
661 static gint
662 get_response_for_widget (GtkInfoBar *info_bar,
663                          GtkWidget  *widget)
664 {
665   ResponseData *rd;
666
667   rd = get_response_data (widget, FALSE);
668   if (!rd)
669     return GTK_RESPONSE_NONE;
670   else
671     return rd->response_id;
672 }
673
674 static void
675 action_widget_activated (GtkWidget  *widget,
676                          GtkInfoBar *info_bar)
677 {
678   gint response_id;
679
680   response_id = get_response_for_widget (info_bar, widget);
681   gtk_info_bar_response (info_bar, response_id);
682 }
683
684 /**
685  * gtk_info_bar_add_action_widget:
686  * @info_bar: a #GtkInfoBar
687  * @child: an activatable widget
688  * @response_id: response ID for @child
689  *
690  * Add an activatable widget to the action area of a #GtkInfoBar,
691  * connecting a signal handler that will emit the #GtkInfoBar::response
692  * signal on the message area when the widget is activated. The widget
693  * is appended to the end of the message areas action area.
694  *
695  * Since: 2.18
696  */
697 void
698 gtk_info_bar_add_action_widget (GtkInfoBar *info_bar,
699                                 GtkWidget  *child,
700                                 gint        response_id)
701 {
702   ResponseData *ad;
703   guint signal_id;
704
705   g_return_if_fail (GTK_IS_INFO_BAR (info_bar));
706   g_return_if_fail (GTK_IS_WIDGET (child));
707
708   ad = get_response_data (child, TRUE);
709
710   ad->response_id = response_id;
711
712   if (GTK_IS_BUTTON (child))
713     signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
714   else
715     signal_id = GTK_WIDGET_GET_CLASS (child)->activate_signal;
716
717   if (signal_id)
718     {
719       GClosure *closure;
720
721       closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
722                                        G_OBJECT (info_bar));
723       g_signal_connect_closure_by_id (child, signal_id, 0, closure, FALSE);
724     }
725   else
726     g_warning ("Only 'activatable' widgets can be packed into the action area of a GtkInfoBar");
727
728   gtk_box_pack_end (GTK_BOX (info_bar->priv->action_area),
729                     child, FALSE, FALSE, 0);
730   if (response_id == GTK_RESPONSE_HELP)
731     gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (info_bar->priv->action_area),
732                                         child, TRUE);
733 }
734
735 /**
736  * gtk_info_bar_get_action_area:
737  * @info_bar: a #GtkInfoBar
738  *
739  * Returns the action area of @info_bar.
740  *
741  * Returns: the action area.
742  *
743  * Since: 2.18
744  */
745 GtkWidget*
746 gtk_info_bar_get_action_area (GtkInfoBar *info_bar)
747 {
748   g_return_val_if_fail (GTK_IS_INFO_BAR (info_bar), NULL);
749
750   return info_bar->priv->action_area;
751 }
752
753 /**
754  * gtk_info_bar_get_content_area:
755  * @info_bar: a #GtkInfoBar
756  *
757  * Returns the content area of @info_bar.
758  *
759  * Returns: the content area.
760  *
761  * Since: 2.18
762  */
763 GtkWidget*
764 gtk_info_bar_get_content_area (GtkInfoBar *info_bar)
765 {
766   g_return_val_if_fail (GTK_IS_INFO_BAR (info_bar), NULL);
767
768   return info_bar->priv->content_area;
769 }
770
771 /**
772  * gtk_info_bar_add_button:
773  * @info_bar: a #GtkInfoBar
774  * @button_text: text of button, or stock ID
775  * @response_id: response ID for the button
776  *
777  * Adds a button with the given text (or a stock button, if button_text
778  * is a stock ID) and sets things up so that clicking the button will emit
779  * the "response" signal with the given response_id. The button is appended
780  * to the end of the info bars's action area. The button widget is
781  * returned, but usually you don't need it.
782  *
783  * Returns: the button widget that was added
784  *
785  * Since: 2.18
786  */
787 GtkWidget*
788 gtk_info_bar_add_button (GtkInfoBar  *info_bar,
789                          const gchar *button_text,
790                          gint         response_id)
791 {
792   GtkWidget *button;
793
794   g_return_val_if_fail (GTK_IS_INFO_BAR (info_bar), NULL);
795   g_return_val_if_fail (button_text != NULL, NULL);
796
797   button = gtk_button_new_from_stock (button_text);
798
799   gtk_widget_set_can_default (button, TRUE);
800
801   gtk_widget_show (button);
802
803   gtk_info_bar_add_action_widget (info_bar, button, response_id);
804
805   return button;
806 }
807
808 static void
809 add_buttons_valist (GtkInfoBar  *info_bar,
810                     const gchar *first_button_text,
811                     va_list      args)
812 {
813   const gchar* text;
814   gint response_id;
815
816   g_return_if_fail (GTK_IS_INFO_BAR (info_bar));
817
818   if (first_button_text == NULL)
819     return;
820
821   text = first_button_text;
822   response_id = va_arg (args, gint);
823
824   while (text != NULL)
825     {
826       gtk_info_bar_add_button (info_bar, text, response_id);
827
828       text = va_arg (args, gchar*);
829       if (text == NULL)
830         break;
831
832       response_id = va_arg (args, int);
833     }
834 }
835
836 /**
837  * gtk_info_bar_add_buttons:
838  * @info_bar: a #GtkInfoBar
839  * @first_button_text: button text or stock ID
840  * @...: response ID for first button, then more text-response_id pairs,
841  *     ending with %NULL
842  *
843  * Adds more buttons, same as calling gtk_info_bar_add_button()
844  * repeatedly. The variable argument list should be %NULL-terminated
845  * as with gtk_info_bar_new_with_buttons(). Each button must have both
846  * text and response ID.
847  *
848  * Since: 2.18
849  */
850 void
851 gtk_info_bar_add_buttons (GtkInfoBar  *info_bar,
852                           const gchar *first_button_text,
853                           ...)
854 {
855   va_list args;
856
857   va_start (args, first_button_text);
858   add_buttons_valist (info_bar, first_button_text, args);
859   va_end (args);
860 }
861
862 /**
863  * gtk_info_bar_new:
864  *
865  * Creates a new #GtkInfoBar object.
866  *
867  * Returns: a new #GtkInfoBar object
868  *
869  * Since: 2.18
870  */
871 GtkWidget *
872 gtk_info_bar_new (void)
873 {
874    return g_object_new (GTK_TYPE_INFO_BAR, NULL);
875 }
876
877 /**
878  * gtk_info_bar_new_with_buttons:
879  * @first_button_text: (allow-none): stock ID or text to go in first button, or %NULL
880  * @...: response ID for first button, then additional buttons, ending
881  *    with %NULL
882  *
883  * Creates a new #GtkInfoBar with buttons. Button text/response ID
884  * pairs should be listed, with a %NULL pointer ending the list.
885  * Button text can be either a stock ID such as %GTK_STOCK_OK, or
886  * some arbitrary text. A response ID can be any positive number,
887  * or one of the values in the #GtkResponseType enumeration. If the
888  * user clicks one of these dialog buttons, GtkInfoBar will emit
889  * the "response" signal with the corresponding response ID.
890  *
891  * Returns: a new #GtkInfoBar
892  */
893 GtkWidget*
894 gtk_info_bar_new_with_buttons (const gchar *first_button_text,
895                                ...)
896 {
897   GtkInfoBar *info_bar;
898   va_list args;
899
900   info_bar = GTK_INFO_BAR (gtk_info_bar_new ());
901
902   va_start (args, first_button_text);
903   add_buttons_valist (info_bar, first_button_text, args);
904   va_end (args);
905
906   return GTK_WIDGET (info_bar);
907 }
908
909 /**
910  * gtk_info_bar_set_response_sensitive:
911  * @info_bar: a #GtkInfoBar
912  * @response_id: a response ID
913  * @setting: TRUE for sensitive
914  *
915  * Calls gtk_widget_set_sensitive (widget, setting) for each
916  * widget in the info bars's action area with the given response_id.
917  * A convenient way to sensitize/desensitize dialog buttons.
918  *
919  * Since: 2.18
920  */
921 void
922 gtk_info_bar_set_response_sensitive (GtkInfoBar *info_bar,
923                                      gint        response_id,
924                                      gboolean    setting)
925 {
926   GList *children, *list;
927
928   g_return_if_fail (GTK_IS_INFO_BAR (info_bar));
929
930   children = gtk_container_get_children (GTK_CONTAINER (info_bar->priv->action_area));
931
932   for (list = children; list; list = list->next)
933     {
934       GtkWidget *widget = list->data;
935       ResponseData *rd = get_response_data (widget, FALSE);
936
937       if (rd && rd->response_id == response_id)
938         gtk_widget_set_sensitive (widget, setting);
939     }
940
941   g_list_free (children);
942 }
943
944 /**
945  * gtk_info_bar_set_default_response:
946  * @info_bar: a #GtkInfoBar
947  * @response_id: a response ID
948  *
949  * Sets the last widget in the info bar's action area with
950  * the given response_id as the default widget for the dialog.
951  * Pressing "Enter" normally activates the default widget.
952  *
953  * Note that this function currently requires @info_bar to
954  * be added to a widget hierarchy. 
955  *
956  * Since: 2.18
957  */
958 void
959 gtk_info_bar_set_default_response (GtkInfoBar *info_bar,
960                                    gint        response_id)
961 {
962   GList *children, *list;
963
964   g_return_if_fail (GTK_IS_INFO_BAR (info_bar));
965
966   children = gtk_container_get_children (GTK_CONTAINER (info_bar->priv->action_area));
967
968   for (list = children; list; list = list->next)
969     {
970       GtkWidget *widget = list->data;
971       ResponseData *rd = get_response_data (widget, FALSE);
972
973       if (rd && rd->response_id == response_id)
974         gtk_widget_grab_default (widget);
975     }
976
977   g_list_free (children);
978 }
979
980 /**
981  * gtk_info_bar_response:
982  * @info_bar: a #GtkInfoBar
983  * @response_id: a response ID
984  *
985  * Emits the 'response' signal with the given @response_id.
986  *
987  * Since: 2.18
988  */
989 void
990 gtk_info_bar_response (GtkInfoBar *info_bar,
991                        gint        response_id)
992 {
993   g_return_if_fail (GTK_IS_INFO_BAR (info_bar));
994
995   g_signal_emit (info_bar, signals[RESPONSE], 0, response_id);
996 }
997
998 typedef struct
999 {
1000   gchar *widget_name;
1001   gchar *response_id;
1002 } ActionWidgetInfo;
1003
1004 typedef struct
1005 {
1006   GtkInfoBar *info_bar;
1007   GtkBuilder *builder;
1008   GSList *items;
1009   gchar *response;
1010 } ActionWidgetsSubParserData;
1011
1012 static void
1013 attributes_start_element (GMarkupParseContext  *context,
1014                           const gchar          *element_name,
1015                           const gchar         **names,
1016                           const gchar         **values,
1017                           gpointer              user_data,
1018                           GError              **error)
1019 {
1020   ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1021   guint i;
1022
1023   if (strcmp (element_name, "action-widget") == 0)
1024     {
1025       for (i = 0; names[i]; i++)
1026         if (strcmp (names[i], "response") == 0)
1027           parser_data->response = g_strdup (values[i]);
1028     }
1029   else if (strcmp (element_name, "action-widgets") == 0)
1030     return;
1031   else
1032     g_warning ("Unsupported tag for GtkInfoBar: %s\n", element_name);
1033 }
1034
1035 static void
1036 attributes_text_element (GMarkupParseContext  *context,
1037                          const gchar          *text,
1038                          gsize                 text_len,
1039                          gpointer              user_data,
1040                          GError              **error)
1041 {
1042   ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1043   ActionWidgetInfo *item;
1044
1045   if (!parser_data->response)
1046     return;
1047
1048   item = g_new (ActionWidgetInfo, 1);
1049   item->widget_name = g_strndup (text, text_len);
1050   item->response_id = parser_data->response;
1051   parser_data->items = g_slist_prepend (parser_data->items, item);
1052   parser_data->response = NULL;
1053 }
1054
1055 static const GMarkupParser attributes_parser =
1056 {
1057   attributes_start_element,
1058   NULL,
1059   attributes_text_element,
1060 };
1061
1062 gboolean
1063 gtk_info_bar_buildable_custom_tag_start (GtkBuildable  *buildable,
1064                                          GtkBuilder    *builder,
1065                                          GObject       *child,
1066                                          const gchar   *tagname,
1067                                          GMarkupParser *parser,
1068                                          gpointer      *data)
1069 {
1070   ActionWidgetsSubParserData *parser_data;
1071
1072   if (child)
1073     return FALSE;
1074
1075   if (strcmp (tagname, "action-widgets") == 0)
1076     {
1077       parser_data = g_slice_new0 (ActionWidgetsSubParserData);
1078       parser_data->info_bar = GTK_INFO_BAR (buildable);
1079       parser_data->items = NULL;
1080
1081       *parser = attributes_parser;
1082       *data = parser_data;
1083       return TRUE;
1084     }
1085
1086   return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1087                                                    tagname, parser, data);
1088 }
1089
1090 static void
1091 gtk_info_bar_buildable_custom_finished (GtkBuildable *buildable,
1092                                         GtkBuilder   *builder,
1093                                         GObject      *child,
1094                                         const gchar  *tagname,
1095                                         gpointer      user_data)
1096 {
1097   GSList *l;
1098   ActionWidgetsSubParserData *parser_data;
1099   GObject *object;
1100   GtkInfoBar *info_bar;
1101   ResponseData *ad;
1102   guint signal_id;
1103
1104   if (strcmp (tagname, "action-widgets"))
1105     {
1106       parent_buildable_iface->custom_finished (buildable, builder, child,
1107                                                tagname, user_data);
1108       return;
1109     }
1110
1111   info_bar = GTK_INFO_BAR (buildable);
1112   parser_data = (ActionWidgetsSubParserData*)user_data;
1113   parser_data->items = g_slist_reverse (parser_data->items);
1114
1115   for (l = parser_data->items; l; l = l->next)
1116     {
1117       ActionWidgetInfo *item = l->data;
1118
1119       object = gtk_builder_get_object (builder, item->widget_name);
1120       if (!object)
1121         {
1122           g_warning ("Unknown object %s specified in action-widgets of %s",
1123                      item->widget_name,
1124                      gtk_buildable_get_name (GTK_BUILDABLE (buildable)));
1125           continue;
1126         }
1127
1128       ad = get_response_data (GTK_WIDGET (object), TRUE);
1129       ad->response_id = atoi (item->response_id);
1130
1131       if (GTK_IS_BUTTON (object))
1132         signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
1133       else
1134         signal_id = GTK_WIDGET_GET_CLASS (object)->activate_signal;
1135
1136       if (signal_id)
1137         {
1138           GClosure *closure;
1139
1140           closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
1141                                            G_OBJECT (info_bar));
1142           g_signal_connect_closure_by_id (object,
1143                                           signal_id,
1144                                           0,
1145                                           closure,
1146                                           FALSE);
1147         }
1148
1149       if (ad->response_id == GTK_RESPONSE_HELP)
1150         gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (info_bar->priv->action_area),
1151                                             GTK_WIDGET (object), TRUE);
1152
1153       g_free (item->widget_name);
1154       g_free (item->response_id);
1155       g_free (item);
1156     }
1157   g_slist_free (parser_data->items);
1158   g_slice_free (ActionWidgetsSubParserData, parser_data);
1159 }
1160
1161 /**
1162  * gtk_info_bar_set_message_type:
1163  * @info_bar: a #GtkInfoBar
1164  * @message_type: a #GtkMessageType
1165  *
1166  * Sets the message type of the message area.
1167  * GTK+ uses this type to determine what color to use
1168  * when drawing the message area.
1169  *
1170  * Since: 2.18
1171  */
1172 void
1173 gtk_info_bar_set_message_type (GtkInfoBar     *info_bar,
1174                                GtkMessageType  message_type)
1175 {
1176   GtkInfoBarPrivate *priv;
1177   AtkObject *atk_obj;
1178
1179   g_return_if_fail (GTK_IS_INFO_BAR (info_bar));
1180
1181   priv = info_bar->priv;
1182
1183   if (priv->message_type != message_type)
1184     {
1185       priv->message_type = message_type;
1186
1187       gtk_info_bar_update_colors (info_bar);
1188       gtk_widget_queue_draw (GTK_WIDGET (info_bar));
1189
1190       atk_obj = gtk_widget_get_accessible (GTK_WIDGET (info_bar));
1191       if (GTK_IS_ACCESSIBLE (atk_obj))
1192         {
1193           GtkStockItem item;
1194           const char *stock_id = NULL;
1195
1196           atk_object_set_role (atk_obj, ATK_ROLE_ALERT);
1197
1198           switch (message_type)
1199             {
1200             case GTK_MESSAGE_INFO:
1201               stock_id = GTK_STOCK_DIALOG_INFO;
1202               break;
1203
1204             case GTK_MESSAGE_QUESTION:
1205               stock_id = GTK_STOCK_DIALOG_QUESTION;
1206               break;
1207
1208             case GTK_MESSAGE_WARNING:
1209               stock_id = GTK_STOCK_DIALOG_WARNING;
1210               break;
1211
1212             case GTK_MESSAGE_ERROR:
1213               stock_id = GTK_STOCK_DIALOG_ERROR;
1214               break;
1215
1216             case GTK_MESSAGE_OTHER:
1217               break;
1218
1219             default:
1220               g_warning ("Unknown GtkMessageType %u", message_type);
1221               break;
1222             }
1223
1224           if (stock_id)
1225             {
1226               gtk_stock_lookup (stock_id, &item);
1227               atk_object_set_name (atk_obj, item.label);
1228             }
1229         }
1230
1231       g_object_notify (G_OBJECT (info_bar), "message-type");
1232     }
1233 }
1234
1235 /**
1236  * gtk_info_bar_get_message_type:
1237  * @info_bar: a #GtkInfoBar
1238  *
1239  * Returns the message type of the message area.
1240  *
1241  * Returns: the message type of the message area.
1242  *
1243  * Since: 2.18
1244  */
1245 GtkMessageType
1246 gtk_info_bar_get_message_type (GtkInfoBar *info_bar)
1247 {
1248   g_return_val_if_fail (GTK_IS_INFO_BAR (info_bar), GTK_MESSAGE_OTHER);
1249
1250   return info_bar->priv->message_type;
1251 }