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