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