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