]> Pileus Git - ~andy/gtk/blob - gtk/gtkinfobar.c
Merge branch 'notebooks-without-mouse-scrolling'
[~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       GtkAllocation allocation;
314       const char *detail;
315
316       detail = type_detail[priv->message_type];
317
318       gtk_widget_get_allocation (widget, &allocation);
319
320       gtk_paint_box (gtk_widget_get_style (widget),
321                      gtk_widget_get_window (widget),
322                      GTK_STATE_NORMAL,
323                      GTK_SHADOW_OUT,
324                      NULL,
325                      widget,
326                      detail,
327                      allocation.x, allocation.y,
328                      allocation.width, allocation.height);
329     }
330
331   if (GTK_WIDGET_CLASS (gtk_info_bar_parent_class)->expose_event)
332     GTK_WIDGET_CLASS (gtk_info_bar_parent_class)->expose_event (widget, event);
333
334   return FALSE;
335 }
336
337 static void
338 gtk_info_bar_class_init (GtkInfoBarClass *klass)
339 {
340   GtkWidgetClass *widget_class;
341   GObjectClass *object_class;
342   GtkBindingSet *binding_set;
343
344   widget_class = GTK_WIDGET_CLASS (klass);
345   object_class = G_OBJECT_CLASS (klass);
346
347   object_class->get_property = gtk_info_bar_get_property;
348   object_class->set_property = gtk_info_bar_set_property;
349   object_class->finalize = gtk_info_bar_finalize;
350
351   widget_class->style_set = gtk_info_bar_style_set;
352   widget_class->expose_event = gtk_info_bar_expose;
353
354   klass->close = gtk_info_bar_close;
355
356   /**
357    * GtkInfoBar:message-type:
358    *
359    * The type of the message.
360    *
361    * The type is used to determine the colors to use in the info bar.
362    * The following symbolic color names can by used to customize
363    * these colors:
364    * "info_fg_color", "info_bg_color",
365    * "warning_fg_color", "warning_bg_color",
366    * "question_fg_color", "question_bg_color",
367    * "error_fg_color", "error_bg_color".
368    * "other_fg_color", "other_bg_color".
369    *
370    * If the type is #GTK_MESSAGE_OTHER, no info bar is painted but the
371    * colors are still set.
372    *
373    * Since: 2.18
374    */
375   g_object_class_install_property (object_class,
376                                    PROP_MESSAGE_TYPE,
377                                    g_param_spec_enum ("message-type",
378                                                       P_("Message Type"),
379                                                       P_("The type of message"),
380                                                       GTK_TYPE_MESSAGE_TYPE,
381                                                       GTK_MESSAGE_INFO,
382                                                       GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
383   /**
384    * GtkInfoBar::response:
385    * @info_bar: the object on which the signal is emitted
386    * @response_id: the response ID
387    *
388    * Emitted when an action widget is clicked or the application programmer
389    * calls gtk_dialog_response(). The @response_id depends on which action
390    * widget was clicked.
391    *
392    * Since: 2.18
393    */
394   signals[RESPONSE] = g_signal_new (I_("response"),
395                                     G_OBJECT_CLASS_TYPE (klass),
396                                     G_SIGNAL_RUN_LAST,
397                                     G_STRUCT_OFFSET (GtkInfoBarClass, response),
398                                     NULL, NULL,
399                                     g_cclosure_marshal_VOID__INT,
400                                     G_TYPE_NONE, 1,
401                                     G_TYPE_INT);
402
403   /**
404    * GtkInfoBar::close:
405    *
406    * The ::close signal is a
407    * <link linkend="keybinding-signals">keybinding signal</link>
408    * which gets emitted when the user uses a keybinding to dismiss
409    * the info bar.
410    *
411    * The default binding for this signal is the Escape key.
412    *
413    * Since: 2.18
414    */
415   signals[CLOSE] =  g_signal_new (I_("close"),
416                                   G_OBJECT_CLASS_TYPE (klass),
417                                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
418                                   G_STRUCT_OFFSET (GtkInfoBarClass, close),
419                                   NULL, NULL,
420                                   g_cclosure_marshal_VOID__VOID,
421                                   G_TYPE_NONE, 0);
422
423   /**
424    * GtkInfoBar:content-area-border:
425    *
426    * The width of the border around the content
427    * content area of the info bar.
428    *
429    * Since: 2.18
430    */
431   gtk_widget_class_install_style_property (widget_class,
432                                            g_param_spec_int ("content-area-border",
433                                                              P_("Content area border"),
434                                                              P_("Width of border around the content area"),
435                                                              0,
436                                                              G_MAXINT,
437                                                              8,
438                                                              GTK_PARAM_READABLE));
439
440   /**
441    * GtkInfoBar:content-area-spacing:
442    *
443    * The default spacing used between elements of the
444    * content area of the info bar.
445    *
446    * Since: 2.18
447    */
448   gtk_widget_class_install_style_property (widget_class,
449                                            g_param_spec_int ("content-area-spacing",
450                                                              P_("Content area spacing"),
451                                                              P_("Spacing between elements of the area"),
452                                                              0,
453                                                              G_MAXINT,
454                                                              16,
455                                                              GTK_PARAM_READABLE));
456
457   /**
458    * GtkInfoBar:button-spacing:
459    *
460    * Spacing between buttons in the action area of the info bar.
461    *
462    * Since: 2.18
463    */
464   gtk_widget_class_install_style_property (widget_class,
465                                            g_param_spec_int ("button-spacing",
466                                                              P_("Button spacing"),
467                                                              P_("Spacing between buttons"),
468                                                              0,
469                                                              G_MAXINT,
470                                                              6,
471                                                              GTK_PARAM_READABLE));
472
473   /**
474    * GtkInfoBar:action-area-border:
475    *
476    * Width of the border around the action area of the info bar.
477    *
478    * Since: 2.18
479    */
480   gtk_widget_class_install_style_property (widget_class,
481                                            g_param_spec_int ("action-area-border",
482                                                              P_("Action area border"),
483                                                              P_("Width of border around the action area"),
484                                                              0,
485                                                              G_MAXINT,
486                                                              5,
487                                                              GTK_PARAM_READABLE));
488
489   binding_set = gtk_binding_set_by_class (klass);
490
491   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0, "close", 0);
492
493   g_type_class_add_private (object_class, sizeof (GtkInfoBarPrivate));
494 }
495
496 static void
497 gtk_info_bar_update_colors (GtkInfoBar *info_bar)
498 {
499   GtkWidget *widget = GTK_WIDGET (info_bar);
500   GtkInfoBarPrivate *priv = info_bar->priv;
501   GdkColor info_default_border_color     = { 0, 0xb800, 0xad00, 0x9d00 };
502   GdkColor info_default_fill_color       = { 0, 0xff00, 0xff00, 0xbf00 };
503   GdkColor warning_default_border_color  = { 0, 0xb000, 0x7a00, 0x2b00 };
504   GdkColor warning_default_fill_color    = { 0, 0xfc00, 0xaf00, 0x3e00 };
505   GdkColor question_default_border_color = { 0, 0x6200, 0x7b00, 0xd960 };
506   GdkColor question_default_fill_color   = { 0, 0x8c00, 0xb000, 0xd700 };
507   GdkColor error_default_border_color    = { 0, 0xa800, 0x2700, 0x2700 };
508   GdkColor error_default_fill_color      = { 0, 0xf000, 0x3800, 0x3800 };
509   GdkColor other_default_border_color    = { 0, 0xb800, 0xad00, 0x9d00 };
510   GdkColor other_default_fill_color      = { 0, 0xff00, 0xff00, 0xbf00 };
511   GdkColor *fg, *bg;
512   GdkColor sym_fg, sym_bg;
513   GtkStyle *style;
514   const char* fg_color_name[] = {
515     "info_fg_color",
516     "warning_fg_color",
517     "question_fg_color",
518     "error_fg_color",
519     "other_fg_color"
520   };
521   const char* bg_color_name[] = {
522     "info_bg_color",
523     "warning_bg_color",
524     "question_bg_color",
525     "error_bg_color",
526     "other_bg_color"
527   };
528
529   style = gtk_widget_get_style (widget);
530
531   if (gtk_style_lookup_color (style, fg_color_name[priv->message_type], &sym_fg) &&
532       gtk_style_lookup_color (style, bg_color_name[priv->message_type], &sym_bg))
533     {
534       fg = &sym_fg;
535       bg = &sym_bg;
536     }
537   else
538     {
539       switch (priv->message_type)
540         {
541         case GTK_MESSAGE_INFO:
542           fg = &info_default_border_color;
543           bg = &info_default_fill_color;
544           break;
545
546         case GTK_MESSAGE_WARNING:
547           fg = &warning_default_border_color;
548           bg = &warning_default_fill_color;
549           break;
550
551         case GTK_MESSAGE_QUESTION:
552           fg = &question_default_border_color;
553           bg = &question_default_fill_color;
554           break;
555
556         case GTK_MESSAGE_ERROR:
557           fg = &error_default_border_color;
558           bg = &error_default_fill_color;
559           break;
560
561         case GTK_MESSAGE_OTHER:
562           fg = &other_default_border_color;
563           bg = &other_default_fill_color;
564           break;
565
566         default:
567           g_assert_not_reached();
568           fg = NULL;
569           bg = NULL;
570         }
571     }
572
573   if (!gdk_color_equal (bg, &style->bg[GTK_STATE_NORMAL]))
574     gtk_widget_modify_bg (widget, GTK_STATE_NORMAL, bg);
575   if (!gdk_color_equal (fg, &style->fg[GTK_STATE_NORMAL]))
576     gtk_widget_modify_fg (widget, GTK_STATE_NORMAL, fg);
577 }
578
579 static void
580 gtk_info_bar_style_set (GtkWidget *widget,
581                         GtkStyle  *prev_style)
582 {
583   GtkInfoBar *info_bar = GTK_INFO_BAR (widget);
584   gint button_spacing;
585   gint action_area_border;
586   gint content_area_spacing;
587   gint content_area_border;
588
589   gtk_widget_style_get (widget,
590                         "button-spacing", &button_spacing,
591                         "action-area-border", &action_area_border,
592                         "content-area-spacing", &content_area_spacing,
593                         "content-area-border", &content_area_border,
594                         NULL);
595
596   gtk_box_set_spacing (GTK_BOX (info_bar->priv->action_area), button_spacing);
597   gtk_container_set_border_width (GTK_CONTAINER (info_bar->priv->action_area),
598                                   action_area_border);
599   gtk_box_set_spacing (GTK_BOX (info_bar->priv->content_area), content_area_spacing);
600   gtk_container_set_border_width (GTK_CONTAINER (info_bar->priv->content_area),
601                                   content_area_border);
602
603   gtk_info_bar_update_colors (info_bar);
604 }
605
606 static void
607 gtk_info_bar_init (GtkInfoBar *info_bar)
608 {
609   GtkWidget *content_area;
610   GtkWidget *action_area;
611
612   gtk_widget_push_composite_child ();
613
614   info_bar->priv = G_TYPE_INSTANCE_GET_PRIVATE (info_bar,
615                                                 GTK_TYPE_INFO_BAR,
616                                                 GtkInfoBarPrivate);
617
618   content_area = gtk_hbox_new (FALSE, 0);
619   gtk_widget_show (content_area);
620   gtk_box_pack_start (GTK_BOX (info_bar), content_area, TRUE, TRUE, 0);
621
622   action_area = gtk_vbutton_box_new ();
623   gtk_widget_show (action_area);
624   gtk_button_box_set_layout (GTK_BUTTON_BOX (action_area), GTK_BUTTONBOX_END);
625   gtk_box_pack_start (GTK_BOX (info_bar), action_area, FALSE, TRUE, 0);
626
627   gtk_widget_set_app_paintable (GTK_WIDGET (info_bar), TRUE);
628   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (info_bar), TRUE);
629
630   info_bar->priv->content_area = content_area;
631   info_bar->priv->action_area = action_area;
632
633   gtk_widget_pop_composite_child ();
634 }
635
636 static GtkBuildableIface *parent_buildable_iface;
637
638 static void
639 gtk_info_bar_buildable_interface_init (GtkBuildableIface *iface)
640 {
641   parent_buildable_iface = g_type_interface_peek_parent (iface);
642   iface->get_internal_child = gtk_info_bar_buildable_get_internal_child;
643   iface->custom_tag_start = gtk_info_bar_buildable_custom_tag_start;
644   iface->custom_finished = gtk_info_bar_buildable_custom_finished;
645 }
646
647 static GObject *
648 gtk_info_bar_buildable_get_internal_child (GtkBuildable *buildable,
649                                            GtkBuilder   *builder,
650                                            const gchar  *childname)
651 {
652   if (strcmp (childname, "content_area") == 0)
653     return G_OBJECT (GTK_INFO_BAR (buildable)->priv->content_area);
654   else if (strcmp (childname, "action_area") == 0)
655     return G_OBJECT (GTK_INFO_BAR (buildable)->priv->action_area);
656
657   return parent_buildable_iface->get_internal_child (buildable,
658                                                      builder,
659                                                      childname);
660 }
661
662 static gint
663 get_response_for_widget (GtkInfoBar *info_bar,
664                          GtkWidget  *widget)
665 {
666   ResponseData *rd;
667
668   rd = get_response_data (widget, FALSE);
669   if (!rd)
670     return GTK_RESPONSE_NONE;
671   else
672     return rd->response_id;
673 }
674
675 static void
676 action_widget_activated (GtkWidget  *widget,
677                          GtkInfoBar *info_bar)
678 {
679   gint response_id;
680
681   response_id = get_response_for_widget (info_bar, widget);
682   gtk_info_bar_response (info_bar, response_id);
683 }
684
685 /**
686  * gtk_info_bar_add_action_widget:
687  * @info_bar: a #GtkInfoBar
688  * @child: an activatable widget
689  * @response_id: response ID for @child
690  *
691  * Add an activatable widget to the action area of a #GtkInfoBar,
692  * connecting a signal handler that will emit the #GtkInfoBar::response
693  * signal on the message area when the widget is activated. The widget
694  * is appended to the end of the message areas action area.
695  *
696  * Since: 2.18
697  */
698 void
699 gtk_info_bar_add_action_widget (GtkInfoBar *info_bar,
700                                 GtkWidget  *child,
701                                 gint        response_id)
702 {
703   ResponseData *ad;
704   guint signal_id;
705
706   g_return_if_fail (GTK_IS_INFO_BAR (info_bar));
707   g_return_if_fail (GTK_IS_WIDGET (child));
708
709   ad = get_response_data (child, TRUE);
710
711   ad->response_id = response_id;
712
713   if (GTK_IS_BUTTON (child))
714     signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
715   else
716     signal_id = GTK_WIDGET_GET_CLASS (child)->activate_signal;
717
718   if (signal_id)
719     {
720       GClosure *closure;
721
722       closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
723                                        G_OBJECT (info_bar));
724       g_signal_connect_closure_by_id (child, signal_id, 0, closure, FALSE);
725     }
726   else
727     g_warning ("Only 'activatable' widgets can be packed into the action area of a GtkInfoBar");
728
729   gtk_box_pack_end (GTK_BOX (info_bar->priv->action_area),
730                     child, FALSE, FALSE, 0);
731   if (response_id == GTK_RESPONSE_HELP)
732     gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (info_bar->priv->action_area),
733                                         child, TRUE);
734 }
735
736 /**
737  * gtk_info_bar_get_action_area:
738  * @info_bar: a #GtkInfoBar
739  *
740  * Returns the action area of @info_bar.
741  *
742  * Returns: (transfer none): the action area
743  *
744  * Since: 2.18
745  */
746 GtkWidget*
747 gtk_info_bar_get_action_area (GtkInfoBar *info_bar)
748 {
749   g_return_val_if_fail (GTK_IS_INFO_BAR (info_bar), NULL);
750
751   return info_bar->priv->action_area;
752 }
753
754 /**
755  * gtk_info_bar_get_content_area:
756  * @info_bar: a #GtkInfoBar
757  *
758  * Returns the content area of @info_bar.
759  *
760  * Returns: (transfer none): the content area
761  *
762  * Since: 2.18
763  */
764 GtkWidget*
765 gtk_info_bar_get_content_area (GtkInfoBar *info_bar)
766 {
767   g_return_val_if_fail (GTK_IS_INFO_BAR (info_bar), NULL);
768
769   return info_bar->priv->content_area;
770 }
771
772 /**
773  * gtk_info_bar_add_button:
774  * @info_bar: a #GtkInfoBar
775  * @button_text: text of button, or stock ID
776  * @response_id: response ID for the button
777  *
778  * Adds a button with the given text (or a stock button, if button_text
779  * is a stock ID) and sets things up so that clicking the button will emit
780  * the "response" signal with the given response_id. The button is appended
781  * to the end of the info bars's action area. The button widget is
782  * returned, but usually you don't need it.
783  *
784  * Returns: (transfer none): the button widget that was added
785  *
786  * Since: 2.18
787  */
788 GtkWidget*
789 gtk_info_bar_add_button (GtkInfoBar  *info_bar,
790                          const gchar *button_text,
791                          gint         response_id)
792 {
793   GtkWidget *button;
794
795   g_return_val_if_fail (GTK_IS_INFO_BAR (info_bar), NULL);
796   g_return_val_if_fail (button_text != NULL, NULL);
797
798   button = gtk_button_new_from_stock (button_text);
799
800   gtk_widget_set_can_default (button, TRUE);
801
802   gtk_widget_show (button);
803
804   gtk_info_bar_add_action_widget (info_bar, button, response_id);
805
806   return button;
807 }
808
809 static void
810 add_buttons_valist (GtkInfoBar  *info_bar,
811                     const gchar *first_button_text,
812                     va_list      args)
813 {
814   const gchar* text;
815   gint response_id;
816
817   g_return_if_fail (GTK_IS_INFO_BAR (info_bar));
818
819   if (first_button_text == NULL)
820     return;
821
822   text = first_button_text;
823   response_id = va_arg (args, gint);
824
825   while (text != NULL)
826     {
827       gtk_info_bar_add_button (info_bar, text, response_id);
828
829       text = va_arg (args, gchar*);
830       if (text == NULL)
831         break;
832
833       response_id = va_arg (args, int);
834     }
835 }
836
837 /**
838  * gtk_info_bar_add_buttons:
839  * @info_bar: a #GtkInfoBar
840  * @first_button_text: button text or stock ID
841  * @...: response ID for first button, then more text-response_id pairs,
842  *     ending with %NULL
843  *
844  * Adds more buttons, same as calling gtk_info_bar_add_button()
845  * repeatedly. The variable argument list should be %NULL-terminated
846  * as with gtk_info_bar_new_with_buttons(). Each button must have both
847  * text and response ID.
848  *
849  * Since: 2.18
850  */
851 void
852 gtk_info_bar_add_buttons (GtkInfoBar  *info_bar,
853                           const gchar *first_button_text,
854                           ...)
855 {
856   va_list args;
857
858   va_start (args, first_button_text);
859   add_buttons_valist (info_bar, first_button_text, args);
860   va_end (args);
861 }
862
863 /**
864  * gtk_info_bar_new:
865  *
866  * Creates a new #GtkInfoBar object.
867  *
868  * Returns: a new #GtkInfoBar object
869  *
870  * Since: 2.18
871  */
872 GtkWidget *
873 gtk_info_bar_new (void)
874 {
875    return g_object_new (GTK_TYPE_INFO_BAR, NULL);
876 }
877
878 /**
879  * gtk_info_bar_new_with_buttons:
880  * @first_button_text: (allow-none): stock ID or text to go in first button, or %NULL
881  * @...: response ID for first button, then additional buttons, ending
882  *    with %NULL
883  *
884  * Creates a new #GtkInfoBar with buttons. Button text/response ID
885  * pairs should be listed, with a %NULL pointer ending the list.
886  * Button text can be either a stock ID such as %GTK_STOCK_OK, or
887  * some arbitrary text. A response ID can be any positive number,
888  * or one of the values in the #GtkResponseType enumeration. If the
889  * user clicks one of these dialog buttons, GtkInfoBar will emit
890  * the "response" signal with the corresponding response ID.
891  *
892  * Returns: a new #GtkInfoBar
893  */
894 GtkWidget*
895 gtk_info_bar_new_with_buttons (const gchar *first_button_text,
896                                ...)
897 {
898   GtkInfoBar *info_bar;
899   va_list args;
900
901   info_bar = GTK_INFO_BAR (gtk_info_bar_new ());
902
903   va_start (args, first_button_text);
904   add_buttons_valist (info_bar, first_button_text, args);
905   va_end (args);
906
907   return GTK_WIDGET (info_bar);
908 }
909
910 /**
911  * gtk_info_bar_set_response_sensitive:
912  * @info_bar: a #GtkInfoBar
913  * @response_id: a response ID
914  * @setting: TRUE for sensitive
915  *
916  * Calls gtk_widget_set_sensitive (widget, setting) for each
917  * widget in the info bars's action area with the given response_id.
918  * A convenient way to sensitize/desensitize dialog buttons.
919  *
920  * Since: 2.18
921  */
922 void
923 gtk_info_bar_set_response_sensitive (GtkInfoBar *info_bar,
924                                      gint        response_id,
925                                      gboolean    setting)
926 {
927   GList *children, *list;
928
929   g_return_if_fail (GTK_IS_INFO_BAR (info_bar));
930
931   children = gtk_container_get_children (GTK_CONTAINER (info_bar->priv->action_area));
932
933   for (list = children; list; list = list->next)
934     {
935       GtkWidget *widget = list->data;
936       ResponseData *rd = get_response_data (widget, FALSE);
937
938       if (rd && rd->response_id == response_id)
939         gtk_widget_set_sensitive (widget, setting);
940     }
941
942   g_list_free (children);
943 }
944
945 /**
946  * gtk_info_bar_set_default_response:
947  * @info_bar: a #GtkInfoBar
948  * @response_id: a response ID
949  *
950  * Sets the last widget in the info bar's action area with
951  * the given response_id as the default widget for the dialog.
952  * Pressing "Enter" normally activates the default widget.
953  *
954  * Note that this function currently requires @info_bar to
955  * be added to a widget hierarchy. 
956  *
957  * Since: 2.18
958  */
959 void
960 gtk_info_bar_set_default_response (GtkInfoBar *info_bar,
961                                    gint        response_id)
962 {
963   GList *children, *list;
964
965   g_return_if_fail (GTK_IS_INFO_BAR (info_bar));
966
967   children = gtk_container_get_children (GTK_CONTAINER (info_bar->priv->action_area));
968
969   for (list = children; list; list = list->next)
970     {
971       GtkWidget *widget = list->data;
972       ResponseData *rd = get_response_data (widget, FALSE);
973
974       if (rd && rd->response_id == response_id)
975         gtk_widget_grab_default (widget);
976     }
977
978   g_list_free (children);
979 }
980
981 /**
982  * gtk_info_bar_response:
983  * @info_bar: a #GtkInfoBar
984  * @response_id: a response ID
985  *
986  * Emits the 'response' signal with the given @response_id.
987  *
988  * Since: 2.18
989  */
990 void
991 gtk_info_bar_response (GtkInfoBar *info_bar,
992                        gint        response_id)
993 {
994   g_return_if_fail (GTK_IS_INFO_BAR (info_bar));
995
996   g_signal_emit (info_bar, signals[RESPONSE], 0, response_id);
997 }
998
999 typedef struct
1000 {
1001   gchar *widget_name;
1002   gchar *response_id;
1003 } ActionWidgetInfo;
1004
1005 typedef struct
1006 {
1007   GtkInfoBar *info_bar;
1008   GtkBuilder *builder;
1009   GSList *items;
1010   gchar *response;
1011 } ActionWidgetsSubParserData;
1012
1013 static void
1014 attributes_start_element (GMarkupParseContext  *context,
1015                           const gchar          *element_name,
1016                           const gchar         **names,
1017                           const gchar         **values,
1018                           gpointer              user_data,
1019                           GError              **error)
1020 {
1021   ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1022   guint i;
1023
1024   if (strcmp (element_name, "action-widget") == 0)
1025     {
1026       for (i = 0; names[i]; i++)
1027         if (strcmp (names[i], "response") == 0)
1028           parser_data->response = g_strdup (values[i]);
1029     }
1030   else if (strcmp (element_name, "action-widgets") == 0)
1031     return;
1032   else
1033     g_warning ("Unsupported tag for GtkInfoBar: %s\n", element_name);
1034 }
1035
1036 static void
1037 attributes_text_element (GMarkupParseContext  *context,
1038                          const gchar          *text,
1039                          gsize                 text_len,
1040                          gpointer              user_data,
1041                          GError              **error)
1042 {
1043   ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1044   ActionWidgetInfo *item;
1045
1046   if (!parser_data->response)
1047     return;
1048
1049   item = g_new (ActionWidgetInfo, 1);
1050   item->widget_name = g_strndup (text, text_len);
1051   item->response_id = parser_data->response;
1052   parser_data->items = g_slist_prepend (parser_data->items, item);
1053   parser_data->response = NULL;
1054 }
1055
1056 static const GMarkupParser attributes_parser =
1057 {
1058   attributes_start_element,
1059   NULL,
1060   attributes_text_element,
1061 };
1062
1063 gboolean
1064 gtk_info_bar_buildable_custom_tag_start (GtkBuildable  *buildable,
1065                                          GtkBuilder    *builder,
1066                                          GObject       *child,
1067                                          const gchar   *tagname,
1068                                          GMarkupParser *parser,
1069                                          gpointer      *data)
1070 {
1071   ActionWidgetsSubParserData *parser_data;
1072
1073   if (child)
1074     return FALSE;
1075
1076   if (strcmp (tagname, "action-widgets") == 0)
1077     {
1078       parser_data = g_slice_new0 (ActionWidgetsSubParserData);
1079       parser_data->info_bar = GTK_INFO_BAR (buildable);
1080       parser_data->items = NULL;
1081
1082       *parser = attributes_parser;
1083       *data = parser_data;
1084       return TRUE;
1085     }
1086
1087   return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1088                                                    tagname, parser, data);
1089 }
1090
1091 static void
1092 gtk_info_bar_buildable_custom_finished (GtkBuildable *buildable,
1093                                         GtkBuilder   *builder,
1094                                         GObject      *child,
1095                                         const gchar  *tagname,
1096                                         gpointer      user_data)
1097 {
1098   GSList *l;
1099   ActionWidgetsSubParserData *parser_data;
1100   GObject *object;
1101   GtkInfoBar *info_bar;
1102   ResponseData *ad;
1103   guint signal_id;
1104
1105   if (strcmp (tagname, "action-widgets"))
1106     {
1107       parent_buildable_iface->custom_finished (buildable, builder, child,
1108                                                tagname, user_data);
1109       return;
1110     }
1111
1112   info_bar = GTK_INFO_BAR (buildable);
1113   parser_data = (ActionWidgetsSubParserData*)user_data;
1114   parser_data->items = g_slist_reverse (parser_data->items);
1115
1116   for (l = parser_data->items; l; l = l->next)
1117     {
1118       ActionWidgetInfo *item = l->data;
1119
1120       object = gtk_builder_get_object (builder, item->widget_name);
1121       if (!object)
1122         {
1123           g_warning ("Unknown object %s specified in action-widgets of %s",
1124                      item->widget_name,
1125                      gtk_buildable_get_name (GTK_BUILDABLE (buildable)));
1126           continue;
1127         }
1128
1129       ad = get_response_data (GTK_WIDGET (object), TRUE);
1130       ad->response_id = atoi (item->response_id);
1131
1132       if (GTK_IS_BUTTON (object))
1133         signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
1134       else
1135         signal_id = GTK_WIDGET_GET_CLASS (object)->activate_signal;
1136
1137       if (signal_id)
1138         {
1139           GClosure *closure;
1140
1141           closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
1142                                            G_OBJECT (info_bar));
1143           g_signal_connect_closure_by_id (object,
1144                                           signal_id,
1145                                           0,
1146                                           closure,
1147                                           FALSE);
1148         }
1149
1150       if (ad->response_id == GTK_RESPONSE_HELP)
1151         gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (info_bar->priv->action_area),
1152                                             GTK_WIDGET (object), TRUE);
1153
1154       g_free (item->widget_name);
1155       g_free (item->response_id);
1156       g_free (item);
1157     }
1158   g_slist_free (parser_data->items);
1159   g_slice_free (ActionWidgetsSubParserData, parser_data);
1160 }
1161
1162 /**
1163  * gtk_info_bar_set_message_type:
1164  * @info_bar: a #GtkInfoBar
1165  * @message_type: a #GtkMessageType
1166  *
1167  * Sets the message type of the message area.
1168  * GTK+ uses this type to determine what color to use
1169  * when drawing the message area.
1170  *
1171  * Since: 2.18
1172  */
1173 void
1174 gtk_info_bar_set_message_type (GtkInfoBar     *info_bar,
1175                                GtkMessageType  message_type)
1176 {
1177   GtkInfoBarPrivate *priv;
1178   AtkObject *atk_obj;
1179
1180   g_return_if_fail (GTK_IS_INFO_BAR (info_bar));
1181
1182   priv = info_bar->priv;
1183
1184   if (priv->message_type != message_type)
1185     {
1186       priv->message_type = message_type;
1187
1188       gtk_info_bar_update_colors (info_bar);
1189       gtk_widget_queue_draw (GTK_WIDGET (info_bar));
1190
1191       atk_obj = gtk_widget_get_accessible (GTK_WIDGET (info_bar));
1192       if (GTK_IS_ACCESSIBLE (atk_obj))
1193         {
1194           GtkStockItem item;
1195           const char *stock_id = NULL;
1196
1197           atk_object_set_role (atk_obj, ATK_ROLE_ALERT);
1198
1199           switch (message_type)
1200             {
1201             case GTK_MESSAGE_INFO:
1202               stock_id = GTK_STOCK_DIALOG_INFO;
1203               break;
1204
1205             case GTK_MESSAGE_QUESTION:
1206               stock_id = GTK_STOCK_DIALOG_QUESTION;
1207               break;
1208
1209             case GTK_MESSAGE_WARNING:
1210               stock_id = GTK_STOCK_DIALOG_WARNING;
1211               break;
1212
1213             case GTK_MESSAGE_ERROR:
1214               stock_id = GTK_STOCK_DIALOG_ERROR;
1215               break;
1216
1217             case GTK_MESSAGE_OTHER:
1218               break;
1219
1220             default:
1221               g_warning ("Unknown GtkMessageType %u", message_type);
1222               break;
1223             }
1224
1225           if (stock_id)
1226             {
1227               gtk_stock_lookup (stock_id, &item);
1228               atk_object_set_name (atk_obj, item.label);
1229             }
1230         }
1231
1232       g_object_notify (G_OBJECT (info_bar), "message-type");
1233     }
1234 }
1235
1236 /**
1237  * gtk_info_bar_get_message_type:
1238  * @info_bar: a #GtkInfoBar
1239  *
1240  * Returns the message type of the message area.
1241  *
1242  * Returns: the message type of the message area.
1243  *
1244  * Since: 2.18
1245  */
1246 GtkMessageType
1247 gtk_info_bar_get_message_type (GtkInfoBar *info_bar)
1248 {
1249   g_return_val_if_fail (GTK_IS_INFO_BAR (info_bar), GTK_MESSAGE_OTHER);
1250
1251   return info_bar->priv->message_type;
1252 }