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