]> Pileus Git - ~andy/gtk/blob - gtk/gtkinfobar.c
f3855de95664899d7057207d5905efcb96202345
[~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   GdkColor info_default_border_color     = { 0, 0xb800, 0xad00, 0x9d00 };
499   GdkColor info_default_fill_color       = { 0, 0xff00, 0xff00, 0xbf00 };
500   GdkColor warning_default_border_color  = { 0, 0xb000, 0x7a00, 0x2b00 };
501   GdkColor warning_default_fill_color    = { 0, 0xfc00, 0xaf00, 0x3e00 };
502   GdkColor question_default_border_color = { 0, 0x6200, 0x7b00, 0xd960 };
503   GdkColor question_default_fill_color   = { 0, 0x8c00, 0xb000, 0xd700 };
504   GdkColor error_default_border_color    = { 0, 0xa800, 0x2700, 0x2700 };
505   GdkColor error_default_fill_color      = { 0, 0xf000, 0x3800, 0x3800 };
506   GdkColor other_default_border_color    = { 0, 0xb800, 0xad00, 0x9d00 };
507   GdkColor other_default_fill_color      = { 0, 0xff00, 0xff00, 0xbf00 };
508   GdkColor *fg, *bg;
509   GdkColor sym_fg, sym_bg;
510   GtkStyle *style;
511   const char* fg_color_name[] = {
512     "info_fg_color",
513     "warning_fg_color",
514     "question_fg_color",
515     "error_fg_color",
516     "other_fg_color"
517   };
518   const char* bg_color_name[] = {
519     "info_bg_color",
520     "warning_bg_color",
521     "question_bg_color",
522     "error_bg_color",
523     "other_bg_color"
524   };
525
526   style = gtk_widget_get_style (widget);
527
528   if (gtk_style_lookup_color (style, fg_color_name[priv->message_type], &sym_fg) &&
529       gtk_style_lookup_color (style, bg_color_name[priv->message_type], &sym_bg))
530     {
531       fg = &sym_fg;
532       bg = &sym_bg;
533     }
534   else
535     {
536       switch (priv->message_type)
537         {
538         case GTK_MESSAGE_INFO:
539           fg = &info_default_border_color;
540           bg = &info_default_fill_color;
541           break;
542
543         case GTK_MESSAGE_WARNING:
544           fg = &warning_default_border_color;
545           bg = &warning_default_fill_color;
546           break;
547
548         case GTK_MESSAGE_QUESTION:
549           fg = &question_default_border_color;
550           bg = &question_default_fill_color;
551           break;
552
553         case GTK_MESSAGE_ERROR:
554           fg = &error_default_border_color;
555           bg = &error_default_fill_color;
556           break;
557
558         case GTK_MESSAGE_OTHER:
559           fg = &other_default_border_color;
560           bg = &other_default_fill_color;
561           break;
562
563         default:
564           g_assert_not_reached();
565           fg = NULL;
566           bg = NULL;
567         }
568     }
569
570   if (!gdk_color_equal (bg, &style->bg[GTK_STATE_NORMAL]))
571     gtk_widget_modify_bg (widget, GTK_STATE_NORMAL, bg);
572   if (!gdk_color_equal (fg, &style->fg[GTK_STATE_NORMAL]))
573     gtk_widget_modify_fg (widget, GTK_STATE_NORMAL, fg);
574 }
575
576 static void
577 gtk_info_bar_style_set (GtkWidget *widget,
578                         GtkStyle  *prev_style)
579 {
580   GtkInfoBar *info_bar = GTK_INFO_BAR (widget);
581   gint button_spacing;
582   gint action_area_border;
583   gint content_area_spacing;
584   gint content_area_border;
585
586   gtk_widget_style_get (widget,
587                         "button-spacing", &button_spacing,
588                         "action-area-border", &action_area_border,
589                         "content-area-spacing", &content_area_spacing,
590                         "content-area-border", &content_area_border,
591                         NULL);
592
593   gtk_box_set_spacing (GTK_BOX (info_bar->priv->action_area), button_spacing);
594   gtk_container_set_border_width (GTK_CONTAINER (info_bar->priv->action_area),
595                                   action_area_border);
596   gtk_box_set_spacing (GTK_BOX (info_bar->priv->content_area), content_area_spacing);
597   gtk_container_set_border_width (GTK_CONTAINER (info_bar->priv->content_area),
598                                   content_area_border);
599
600   gtk_info_bar_update_colors (info_bar);
601 }
602
603 static void
604 gtk_info_bar_init (GtkInfoBar *info_bar)
605 {
606   GtkWidget *content_area;
607   GtkWidget *action_area;
608
609   gtk_widget_push_composite_child ();
610
611   info_bar->priv = G_TYPE_INSTANCE_GET_PRIVATE (info_bar,
612                                                 GTK_TYPE_INFO_BAR,
613                                                 GtkInfoBarPrivate);
614
615   content_area = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, FALSE, 0);
616   gtk_widget_show (content_area);
617   gtk_box_pack_start (GTK_BOX (info_bar), content_area, TRUE, TRUE, 0);
618
619   action_area = gtk_button_box_new (GTK_ORIENTATION_VERTICAL);
620   gtk_widget_show (action_area);
621   gtk_button_box_set_layout (GTK_BUTTON_BOX (action_area), GTK_BUTTONBOX_END);
622   gtk_box_pack_start (GTK_BOX (info_bar), action_area, FALSE, TRUE, 0);
623
624   gtk_widget_set_app_paintable (GTK_WIDGET (info_bar), TRUE);
625   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (info_bar), TRUE);
626
627   info_bar->priv->content_area = content_area;
628   info_bar->priv->action_area = action_area;
629
630   gtk_widget_pop_composite_child ();
631 }
632
633 static GtkBuildableIface *parent_buildable_iface;
634
635 static void
636 gtk_info_bar_buildable_interface_init (GtkBuildableIface *iface)
637 {
638   parent_buildable_iface = g_type_interface_peek_parent (iface);
639   iface->get_internal_child = gtk_info_bar_buildable_get_internal_child;
640   iface->custom_tag_start = gtk_info_bar_buildable_custom_tag_start;
641   iface->custom_finished = gtk_info_bar_buildable_custom_finished;
642 }
643
644 static GObject *
645 gtk_info_bar_buildable_get_internal_child (GtkBuildable *buildable,
646                                            GtkBuilder   *builder,
647                                            const gchar  *childname)
648 {
649   if (strcmp (childname, "content_area") == 0)
650     return G_OBJECT (GTK_INFO_BAR (buildable)->priv->content_area);
651   else if (strcmp (childname, "action_area") == 0)
652     return G_OBJECT (GTK_INFO_BAR (buildable)->priv->action_area);
653
654   return parent_buildable_iface->get_internal_child (buildable,
655                                                      builder,
656                                                      childname);
657 }
658
659 static gint
660 get_response_for_widget (GtkInfoBar *info_bar,
661                          GtkWidget  *widget)
662 {
663   ResponseData *rd;
664
665   rd = get_response_data (widget, FALSE);
666   if (!rd)
667     return GTK_RESPONSE_NONE;
668   else
669     return rd->response_id;
670 }
671
672 static void
673 action_widget_activated (GtkWidget  *widget,
674                          GtkInfoBar *info_bar)
675 {
676   gint response_id;
677
678   response_id = get_response_for_widget (info_bar, widget);
679   gtk_info_bar_response (info_bar, response_id);
680 }
681
682 /**
683  * gtk_info_bar_add_action_widget:
684  * @info_bar: a #GtkInfoBar
685  * @child: an activatable widget
686  * @response_id: response ID for @child
687  *
688  * Add an activatable widget to the action area of a #GtkInfoBar,
689  * connecting a signal handler that will emit the #GtkInfoBar::response
690  * signal on the message area when the widget is activated. The widget
691  * is appended to the end of the message areas action area.
692  *
693  * Since: 2.18
694  */
695 void
696 gtk_info_bar_add_action_widget (GtkInfoBar *info_bar,
697                                 GtkWidget  *child,
698                                 gint        response_id)
699 {
700   ResponseData *ad;
701   guint signal_id;
702
703   g_return_if_fail (GTK_IS_INFO_BAR (info_bar));
704   g_return_if_fail (GTK_IS_WIDGET (child));
705
706   ad = get_response_data (child, TRUE);
707
708   ad->response_id = response_id;
709
710   if (GTK_IS_BUTTON (child))
711     signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
712   else
713     signal_id = GTK_WIDGET_GET_CLASS (child)->activate_signal;
714
715   if (signal_id)
716     {
717       GClosure *closure;
718
719       closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
720                                        G_OBJECT (info_bar));
721       g_signal_connect_closure_by_id (child, signal_id, 0, closure, FALSE);
722     }
723   else
724     g_warning ("Only 'activatable' widgets can be packed into the action area of a GtkInfoBar");
725
726   gtk_box_pack_end (GTK_BOX (info_bar->priv->action_area),
727                     child, FALSE, FALSE, 0);
728   if (response_id == GTK_RESPONSE_HELP)
729     gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (info_bar->priv->action_area),
730                                         child, TRUE);
731 }
732
733 /**
734  * gtk_info_bar_get_action_area:
735  * @info_bar: a #GtkInfoBar
736  *
737  * Returns the action area of @info_bar.
738  *
739  * Returns: (transfer none): the action area
740  *
741  * Since: 2.18
742  */
743 GtkWidget*
744 gtk_info_bar_get_action_area (GtkInfoBar *info_bar)
745 {
746   g_return_val_if_fail (GTK_IS_INFO_BAR (info_bar), NULL);
747
748   return info_bar->priv->action_area;
749 }
750
751 /**
752  * gtk_info_bar_get_content_area:
753  * @info_bar: a #GtkInfoBar
754  *
755  * Returns the content area of @info_bar.
756  *
757  * Returns: (transfer none): the content area
758  *
759  * Since: 2.18
760  */
761 GtkWidget*
762 gtk_info_bar_get_content_area (GtkInfoBar *info_bar)
763 {
764   g_return_val_if_fail (GTK_IS_INFO_BAR (info_bar), NULL);
765
766   return info_bar->priv->content_area;
767 }
768
769 /**
770  * gtk_info_bar_add_button:
771  * @info_bar: a #GtkInfoBar
772  * @button_text: text of button, or stock ID
773  * @response_id: response ID for the button
774  *
775  * Adds a button with the given text (or a stock button, if button_text
776  * is a stock ID) and sets things up so that clicking the button will emit
777  * the "response" signal with the given response_id. The button is appended
778  * to the end of the info bars's action area. The button widget is
779  * returned, but usually you don't need it.
780  *
781  * Returns: (transfer none): the button widget that was added
782  *
783  * Since: 2.18
784  */
785 GtkWidget*
786 gtk_info_bar_add_button (GtkInfoBar  *info_bar,
787                          const gchar *button_text,
788                          gint         response_id)
789 {
790   GtkWidget *button;
791
792   g_return_val_if_fail (GTK_IS_INFO_BAR (info_bar), NULL);
793   g_return_val_if_fail (button_text != NULL, NULL);
794
795   button = gtk_button_new_from_stock (button_text);
796
797   gtk_widget_set_can_default (button, TRUE);
798
799   gtk_widget_show (button);
800
801   gtk_info_bar_add_action_widget (info_bar, button, response_id);
802
803   return button;
804 }
805
806 static void
807 add_buttons_valist (GtkInfoBar  *info_bar,
808                     const gchar *first_button_text,
809                     va_list      args)
810 {
811   const gchar* text;
812   gint response_id;
813
814   g_return_if_fail (GTK_IS_INFO_BAR (info_bar));
815
816   if (first_button_text == NULL)
817     return;
818
819   text = first_button_text;
820   response_id = va_arg (args, gint);
821
822   while (text != NULL)
823     {
824       gtk_info_bar_add_button (info_bar, text, response_id);
825
826       text = va_arg (args, gchar*);
827       if (text == NULL)
828         break;
829
830       response_id = va_arg (args, int);
831     }
832 }
833
834 /**
835  * gtk_info_bar_add_buttons:
836  * @info_bar: a #GtkInfoBar
837  * @first_button_text: button text or stock ID
838  * @...: response ID for first button, then more text-response_id pairs,
839  *     ending with %NULL
840  *
841  * Adds more buttons, same as calling gtk_info_bar_add_button()
842  * repeatedly. The variable argument list should be %NULL-terminated
843  * as with gtk_info_bar_new_with_buttons(). Each button must have both
844  * text and response ID.
845  *
846  * Since: 2.18
847  */
848 void
849 gtk_info_bar_add_buttons (GtkInfoBar  *info_bar,
850                           const gchar *first_button_text,
851                           ...)
852 {
853   va_list args;
854
855   va_start (args, first_button_text);
856   add_buttons_valist (info_bar, first_button_text, args);
857   va_end (args);
858 }
859
860 /**
861  * gtk_info_bar_new:
862  *
863  * Creates a new #GtkInfoBar object.
864  *
865  * Returns: a new #GtkInfoBar object
866  *
867  * Since: 2.18
868  */
869 GtkWidget *
870 gtk_info_bar_new (void)
871 {
872    return g_object_new (GTK_TYPE_INFO_BAR, NULL);
873 }
874
875 /**
876  * gtk_info_bar_new_with_buttons:
877  * @first_button_text: (allow-none): stock ID or text to go in first button, or %NULL
878  * @...: response ID for first button, then additional buttons, ending
879  *    with %NULL
880  *
881  * Creates a new #GtkInfoBar with buttons. Button text/response ID
882  * pairs should be listed, with a %NULL pointer ending the list.
883  * Button text can be either a stock ID such as %GTK_STOCK_OK, or
884  * some arbitrary text. A response ID can be any positive number,
885  * or one of the values in the #GtkResponseType enumeration. If the
886  * user clicks one of these dialog buttons, GtkInfoBar will emit
887  * the "response" signal with the corresponding response ID.
888  *
889  * Returns: a new #GtkInfoBar
890  */
891 GtkWidget*
892 gtk_info_bar_new_with_buttons (const gchar *first_button_text,
893                                ...)
894 {
895   GtkInfoBar *info_bar;
896   va_list args;
897
898   info_bar = GTK_INFO_BAR (gtk_info_bar_new ());
899
900   va_start (args, first_button_text);
901   add_buttons_valist (info_bar, first_button_text, args);
902   va_end (args);
903
904   return GTK_WIDGET (info_bar);
905 }
906
907 /**
908  * gtk_info_bar_set_response_sensitive:
909  * @info_bar: a #GtkInfoBar
910  * @response_id: a response ID
911  * @setting: TRUE for sensitive
912  *
913  * Calls gtk_widget_set_sensitive (widget, setting) for each
914  * widget in the info bars's action area with the given response_id.
915  * A convenient way to sensitize/desensitize dialog buttons.
916  *
917  * Since: 2.18
918  */
919 void
920 gtk_info_bar_set_response_sensitive (GtkInfoBar *info_bar,
921                                      gint        response_id,
922                                      gboolean    setting)
923 {
924   GList *children, *list;
925
926   g_return_if_fail (GTK_IS_INFO_BAR (info_bar));
927
928   children = gtk_container_get_children (GTK_CONTAINER (info_bar->priv->action_area));
929
930   for (list = children; list; list = list->next)
931     {
932       GtkWidget *widget = list->data;
933       ResponseData *rd = get_response_data (widget, FALSE);
934
935       if (rd && rd->response_id == response_id)
936         gtk_widget_set_sensitive (widget, setting);
937     }
938
939   g_list_free (children);
940 }
941
942 /**
943  * gtk_info_bar_set_default_response:
944  * @info_bar: a #GtkInfoBar
945  * @response_id: a response ID
946  *
947  * Sets the last widget in the info bar's action area with
948  * the given response_id as the default widget for the dialog.
949  * Pressing "Enter" normally activates the default widget.
950  *
951  * Note that this function currently requires @info_bar to
952  * be added to a widget hierarchy. 
953  *
954  * Since: 2.18
955  */
956 void
957 gtk_info_bar_set_default_response (GtkInfoBar *info_bar,
958                                    gint        response_id)
959 {
960   GList *children, *list;
961
962   g_return_if_fail (GTK_IS_INFO_BAR (info_bar));
963
964   children = gtk_container_get_children (GTK_CONTAINER (info_bar->priv->action_area));
965
966   for (list = children; list; list = list->next)
967     {
968       GtkWidget *widget = list->data;
969       ResponseData *rd = get_response_data (widget, FALSE);
970
971       if (rd && rd->response_id == response_id)
972         gtk_widget_grab_default (widget);
973     }
974
975   g_list_free (children);
976 }
977
978 /**
979  * gtk_info_bar_response:
980  * @info_bar: a #GtkInfoBar
981  * @response_id: a response ID
982  *
983  * Emits the 'response' signal with the given @response_id.
984  *
985  * Since: 2.18
986  */
987 void
988 gtk_info_bar_response (GtkInfoBar *info_bar,
989                        gint        response_id)
990 {
991   g_return_if_fail (GTK_IS_INFO_BAR (info_bar));
992
993   g_signal_emit (info_bar, signals[RESPONSE], 0, response_id);
994 }
995
996 typedef struct
997 {
998   gchar *widget_name;
999   gchar *response_id;
1000 } ActionWidgetInfo;
1001
1002 typedef struct
1003 {
1004   GtkInfoBar *info_bar;
1005   GtkBuilder *builder;
1006   GSList *items;
1007   gchar *response;
1008 } ActionWidgetsSubParserData;
1009
1010 static void
1011 attributes_start_element (GMarkupParseContext  *context,
1012                           const gchar          *element_name,
1013                           const gchar         **names,
1014                           const gchar         **values,
1015                           gpointer              user_data,
1016                           GError              **error)
1017 {
1018   ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1019   guint i;
1020
1021   if (strcmp (element_name, "action-widget") == 0)
1022     {
1023       for (i = 0; names[i]; i++)
1024         if (strcmp (names[i], "response") == 0)
1025           parser_data->response = g_strdup (values[i]);
1026     }
1027   else if (strcmp (element_name, "action-widgets") == 0)
1028     return;
1029   else
1030     g_warning ("Unsupported tag for GtkInfoBar: %s\n", element_name);
1031 }
1032
1033 static void
1034 attributes_text_element (GMarkupParseContext  *context,
1035                          const gchar          *text,
1036                          gsize                 text_len,
1037                          gpointer              user_data,
1038                          GError              **error)
1039 {
1040   ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data;
1041   ActionWidgetInfo *item;
1042
1043   if (!parser_data->response)
1044     return;
1045
1046   item = g_new (ActionWidgetInfo, 1);
1047   item->widget_name = g_strndup (text, text_len);
1048   item->response_id = parser_data->response;
1049   parser_data->items = g_slist_prepend (parser_data->items, item);
1050   parser_data->response = NULL;
1051 }
1052
1053 static const GMarkupParser attributes_parser =
1054 {
1055   attributes_start_element,
1056   NULL,
1057   attributes_text_element,
1058 };
1059
1060 gboolean
1061 gtk_info_bar_buildable_custom_tag_start (GtkBuildable  *buildable,
1062                                          GtkBuilder    *builder,
1063                                          GObject       *child,
1064                                          const gchar   *tagname,
1065                                          GMarkupParser *parser,
1066                                          gpointer      *data)
1067 {
1068   ActionWidgetsSubParserData *parser_data;
1069
1070   if (child)
1071     return FALSE;
1072
1073   if (strcmp (tagname, "action-widgets") == 0)
1074     {
1075       parser_data = g_slice_new0 (ActionWidgetsSubParserData);
1076       parser_data->info_bar = GTK_INFO_BAR (buildable);
1077       parser_data->items = NULL;
1078
1079       *parser = attributes_parser;
1080       *data = parser_data;
1081       return TRUE;
1082     }
1083
1084   return parent_buildable_iface->custom_tag_start (buildable, builder, child,
1085                                                    tagname, parser, data);
1086 }
1087
1088 static void
1089 gtk_info_bar_buildable_custom_finished (GtkBuildable *buildable,
1090                                         GtkBuilder   *builder,
1091                                         GObject      *child,
1092                                         const gchar  *tagname,
1093                                         gpointer      user_data)
1094 {
1095   GSList *l;
1096   ActionWidgetsSubParserData *parser_data;
1097   GObject *object;
1098   GtkInfoBar *info_bar;
1099   ResponseData *ad;
1100   guint signal_id;
1101
1102   if (strcmp (tagname, "action-widgets"))
1103     {
1104       parent_buildable_iface->custom_finished (buildable, builder, child,
1105                                                tagname, user_data);
1106       return;
1107     }
1108
1109   info_bar = GTK_INFO_BAR (buildable);
1110   parser_data = (ActionWidgetsSubParserData*)user_data;
1111   parser_data->items = g_slist_reverse (parser_data->items);
1112
1113   for (l = parser_data->items; l; l = l->next)
1114     {
1115       ActionWidgetInfo *item = l->data;
1116
1117       object = gtk_builder_get_object (builder, item->widget_name);
1118       if (!object)
1119         {
1120           g_warning ("Unknown object %s specified in action-widgets of %s",
1121                      item->widget_name,
1122                      gtk_buildable_get_name (GTK_BUILDABLE (buildable)));
1123           continue;
1124         }
1125
1126       ad = get_response_data (GTK_WIDGET (object), TRUE);
1127       ad->response_id = atoi (item->response_id);
1128
1129       if (GTK_IS_BUTTON (object))
1130         signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
1131       else
1132         signal_id = GTK_WIDGET_GET_CLASS (object)->activate_signal;
1133
1134       if (signal_id)
1135         {
1136           GClosure *closure;
1137
1138           closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
1139                                            G_OBJECT (info_bar));
1140           g_signal_connect_closure_by_id (object,
1141                                           signal_id,
1142                                           0,
1143                                           closure,
1144                                           FALSE);
1145         }
1146
1147       if (ad->response_id == GTK_RESPONSE_HELP)
1148         gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (info_bar->priv->action_area),
1149                                             GTK_WIDGET (object), TRUE);
1150
1151       g_free (item->widget_name);
1152       g_free (item->response_id);
1153       g_free (item);
1154     }
1155   g_slist_free (parser_data->items);
1156   g_slice_free (ActionWidgetsSubParserData, parser_data);
1157 }
1158
1159 /**
1160  * gtk_info_bar_set_message_type:
1161  * @info_bar: a #GtkInfoBar
1162  * @message_type: a #GtkMessageType
1163  *
1164  * Sets the message type of the message area.
1165  * GTK+ uses this type to determine what color to use
1166  * when drawing the message area.
1167  *
1168  * Since: 2.18
1169  */
1170 void
1171 gtk_info_bar_set_message_type (GtkInfoBar     *info_bar,
1172                                GtkMessageType  message_type)
1173 {
1174   GtkInfoBarPrivate *priv;
1175   AtkObject *atk_obj;
1176
1177   g_return_if_fail (GTK_IS_INFO_BAR (info_bar));
1178
1179   priv = info_bar->priv;
1180
1181   if (priv->message_type != message_type)
1182     {
1183       priv->message_type = message_type;
1184
1185       gtk_info_bar_update_colors (info_bar);
1186       gtk_widget_queue_draw (GTK_WIDGET (info_bar));
1187
1188       atk_obj = gtk_widget_get_accessible (GTK_WIDGET (info_bar));
1189       if (GTK_IS_ACCESSIBLE (atk_obj))
1190         {
1191           GtkStockItem item;
1192           const char *stock_id = NULL;
1193
1194           atk_object_set_role (atk_obj, ATK_ROLE_ALERT);
1195
1196           switch (message_type)
1197             {
1198             case GTK_MESSAGE_INFO:
1199               stock_id = GTK_STOCK_DIALOG_INFO;
1200               break;
1201
1202             case GTK_MESSAGE_QUESTION:
1203               stock_id = GTK_STOCK_DIALOG_QUESTION;
1204               break;
1205
1206             case GTK_MESSAGE_WARNING:
1207               stock_id = GTK_STOCK_DIALOG_WARNING;
1208               break;
1209
1210             case GTK_MESSAGE_ERROR:
1211               stock_id = GTK_STOCK_DIALOG_ERROR;
1212               break;
1213
1214             case GTK_MESSAGE_OTHER:
1215               break;
1216
1217             default:
1218               g_warning ("Unknown GtkMessageType %u", message_type);
1219               break;
1220             }
1221
1222           if (stock_id)
1223             {
1224               gtk_stock_lookup (stock_id, &item);
1225               atk_object_set_name (atk_obj, item.label);
1226             }
1227         }
1228
1229       g_object_notify (G_OBJECT (info_bar), "message-type");
1230     }
1231 }
1232
1233 /**
1234  * gtk_info_bar_get_message_type:
1235  * @info_bar: a #GtkInfoBar
1236  *
1237  * Returns the message type of the message area.
1238  *
1239  * Returns: the message type of the message area.
1240  *
1241  * Since: 2.18
1242  */
1243 GtkMessageType
1244 gtk_info_bar_get_message_type (GtkInfoBar *info_bar)
1245 {
1246   g_return_val_if_fail (GTK_IS_INFO_BAR (info_bar), GTK_MESSAGE_OTHER);
1247
1248   return info_bar->priv->message_type;
1249 }