gtkinfobar.c 38.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * gtkinfobar.c
 * This file is part of GTK+
 *
 * Copyright (C) 2005 - Paolo Maggi
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
18
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 */

/*
 * Modified by the gedit Team, 2005. See the AUTHORS file for a
 * list of people on the gtk Team.
 * See the gedit ChangeLog files for a list of changes.
 *
 * Modified by the GTK+ team, 2008-2009.
 */


#include "config.h"

#include <stdlib.h>

#include "gtkinfobar.h"
#include "gtkaccessible.h"
#include "gtkbuildable.h"
37
#include "gtkbuilderprivate.h"
Matthias Clasen's avatar
Matthias Clasen committed
38
#include "gtkbbox.h"
39 40 41 42 43 44
#include "gtkbox.h"
#include "gtklabel.h"
#include "gtkbutton.h"
#include "gtkenums.h"
#include "gtkbindings.h"
#include "gtkdialog.h"
45
#include "gtkrevealer.h"
46 47
#include "gtkintl.h"
#include "gtkprivate.h"
48
#include "gtkorientable.h"
49
#include "gtkrender.h"
50
#include "gtktypebuiltins.h"
51
#include "gtkwidgetprivate.h"
52
#include "deprecated/gtkstock.h"
53 54 55 56 57 58 59 60 61 62

/**
 * SECTION:gtkinfobar
 * @short_description: Report important messages to the user
 * @include: gtk/gtk.h
 * @see_also: #GtkStatusbar, #GtkMessageDialog
 *
 * #GtkInfoBar is a widget that can be used to show messages to
 * the user without showing a dialog. It is often temporarily shown
 * at the top or bottom of a document. In contrast to #GtkDialog, which
63 64
 * has a action area at the bottom, #GtkInfoBar has an action area
 * at the side.
65 66 67 68 69 70
 *
 * The API of #GtkInfoBar is very similar to #GtkDialog, allowing you
 * to add buttons to the action area with gtk_info_bar_add_button() or
 * gtk_info_bar_new_with_buttons(). The sensitivity of action widgets
 * can be controlled with gtk_info_bar_set_response_sensitive().
 * To add widgets to the main content area of a #GtkInfoBar, use
71
 * gtk_info_bar_get_content_area() and add your widgets to the container.
72 73 74
 *
 * Similar to #GtkMessageDialog, the contents of a #GtkInfoBar can by
 * classified as error message, warning, informational message, etc,
75 76
 * by using gtk_info_bar_set_message_type(). GTK+ may use the message type
 * to determine how the message is displayed.
77
 *
78
 * A simple example for using a #GtkInfoBar:
79
 * |[<!-- language="C" -->
80 81
 * GtkWidget *widget, *message_label, *content_area;
 * GtkWidget *grid;
82 83
 * GtkInfoBar *bar;
 *
84
 * // set up info bar
85
 * widget = gtk_info_bar_new ();
86
 * bar = GTK_INFO_BAR (widget);
87
 * grid = gtk_grid_new ();
88 89
 *
 * gtk_widget_set_no_show_all (widget, TRUE);
90
 * message_label = gtk_label_new ("");
91 92 93 94 95 96 97 98 99 100
 * content_area = gtk_info_bar_get_content_area (bar);
 * gtk_container_add (GTK_CONTAINER (content_area),
 *                    message_label);
 * gtk_info_bar_add_button (bar,
 *                          _("_OK"),
 *                          GTK_RESPONSE_OK);
 * g_signal_connect (bar,
 *                   "response",
 *                   G_CALLBACK (gtk_widget_hide),
 *                   NULL);
101
 * gtk_grid_attach (GTK_GRID (grid),
102
 *                  widget,
103
 *                  0, 2, 1, 1);
104
 *
105
 * // ...
106
 *
107
 * // show an error message
108
 * gtk_label_set_text (GTK_LABEL (message_label), "An error occurred!");
109
 * gtk_info_bar_set_message_type (bar,
110
 *                                GTK_MESSAGE_ERROR);
111
 * gtk_widget_show (bar);
112
 * ]|
113
 *
114
 * # GtkInfoBar as GtkBuildable
115
 *
116 117
 * The GtkInfoBar implementation of the GtkBuildable interface exposes
 * the content area and action area as internal children with the names
118
 * “content_area” and “action_area”.
119
 *
120 121 122 123
 * GtkInfoBar supports a custom <action-widgets> element, which can contain
 * multiple <action-widget> elements. The “response” attribute specifies a
 * numeric response, and the content of the element is the id of widget
 * (which should be a child of the dialogs @action_area).
124 125 126 127
 *
 * # CSS nodes
 *
 * GtkInfoBar has a single CSS node with name infobar. The node may get
Timm Bäder's avatar
Timm Bäder committed
128
 * one of the style classes .info, .warning, .error or .question, depending
129
 * on the message type.
130 131 132 133 134
 */

enum
{
  PROP_0,
135
  PROP_MESSAGE_TYPE,
136
  PROP_SHOW_CLOSE_BUTTON,
137
  PROP_REVEALED,
138
  LAST_PROP
139 140 141 142 143 144
};

struct _GtkInfoBarPrivate
{
  GtkWidget *content_area;
  GtkWidget *action_area;
145
  GtkWidget *close_button;
146
  GtkWidget *revealer;
147

148
  gboolean show_close_button;
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  GtkMessageType message_type;
};

typedef struct _ResponseData ResponseData;

struct _ResponseData
{
  gint response_id;
};

enum
{
  RESPONSE,
  CLOSE,
  LAST_SIGNAL
};

166
static GParamSpec *props[LAST_PROP] = { NULL, };
167 168
static guint signals[LAST_SIGNAL];

169 170 171 172
#define ACTION_AREA_DEFAULT_BORDER 5
#define ACTION_AREA_DEFAULT_SPACING 6
#define CONTENT_AREA_DEFAULT_BORDER 8
#define CONTENT_AREA_DEFAULT_SPACING 16
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

static void     gtk_info_bar_set_property (GObject        *object,
                                           guint           prop_id,
                                           const GValue   *value,
                                           GParamSpec     *pspec);
static void     gtk_info_bar_get_property (GObject        *object,
                                           guint           prop_id,
                                           GValue         *value,
                                           GParamSpec     *pspec);
static void     gtk_info_bar_buildable_interface_init     (GtkBuildableIface *iface);
static gboolean  gtk_info_bar_buildable_custom_tag_start   (GtkBuildable  *buildable,
                                                            GtkBuilder    *builder,
                                                            GObject       *child,
                                                            const gchar   *tagname,
                                                            GMarkupParser *parser,
                                                            gpointer      *data);
static void      gtk_info_bar_buildable_custom_finished    (GtkBuildable  *buildable,
                                                            GtkBuilder    *builder,
                                                            GObject       *child,
                                                            const gchar   *tagname,
                                                            gpointer       user_data);


196
G_DEFINE_TYPE_WITH_CODE (GtkInfoBar, gtk_info_bar, GTK_TYPE_BOX,
197
                         G_ADD_PRIVATE (GtkInfoBar)
198 199 200 201 202 203 204 205 206
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
                                                gtk_info_bar_buildable_interface_init))

static void
gtk_info_bar_set_property (GObject      *object,
                           guint         prop_id,
                           const GValue *value,
                           GParamSpec   *pspec)
{
207
  GtkInfoBar *info_bar = GTK_INFO_BAR (object);
208 209 210 211 212 213

  switch (prop_id)
    {
    case PROP_MESSAGE_TYPE:
      gtk_info_bar_set_message_type (info_bar, g_value_get_enum (value));
      break;
214 215 216
    case PROP_SHOW_CLOSE_BUTTON:
      gtk_info_bar_set_show_close_button (info_bar, g_value_get_boolean (value));
      break;
217 218 219
    case PROP_REVEALED:
      gtk_info_bar_set_revealed (info_bar, g_value_get_boolean (value));
      break;
220 221 222 223 224 225 226 227 228 229 230 231
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_info_bar_get_property (GObject    *object,
                           guint       prop_id,
                           GValue     *value,
                           GParamSpec *pspec)
{
232
  GtkInfoBar *info_bar = GTK_INFO_BAR (object);
233 234 235 236 237 238

  switch (prop_id)
    {
    case PROP_MESSAGE_TYPE:
      g_value_set_enum (value, gtk_info_bar_get_message_type (info_bar));
      break;
239 240 241
    case PROP_SHOW_CLOSE_BUTTON:
      g_value_set_boolean (value, gtk_info_bar_get_show_close_button (info_bar));
      break;
242 243 244
    case PROP_REVEALED:
      g_value_set_boolean (value, gtk_info_bar_get_revealed (info_bar));
      break;
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
response_data_free (gpointer data)
{
  g_slice_free (ResponseData, data);
}

static ResponseData *
get_response_data (GtkWidget *widget,
                   gboolean   create)
{
  ResponseData *ad = g_object_get_data (G_OBJECT (widget),
                                        "gtk-info-bar-response-data");

  if (ad == NULL && create)
    {
      ad = g_slice_new (ResponseData);

      g_object_set_data_full (G_OBJECT (widget),
                              I_("gtk-info-bar-response-data"),
                              ad,
                              response_data_free);
    }

  return ad;
}

static GtkWidget *
find_button (GtkInfoBar *info_bar,
             gint        response_id)
{
  GList *children, *list;
  GtkWidget *child = NULL;

  children = gtk_container_get_children (GTK_CONTAINER (info_bar->priv->action_area));

  for (list = children; list; list = list->next)
    {
      ResponseData *rd = get_response_data (list->data, FALSE);

      if (rd && rd->response_id == response_id)
        {
          child = list->data;
          break;
        }
    }

  g_list_free (children);

  return child;
}

static void
gtk_info_bar_close (GtkInfoBar *info_bar)
{
305 306
  if (!gtk_widget_get_visible (info_bar->priv->close_button)
      && !find_button (info_bar, GTK_RESPONSE_CANCEL))
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
    return;

  gtk_info_bar_response (GTK_INFO_BAR (info_bar),
                         GTK_RESPONSE_CANCEL);
}

static void
gtk_info_bar_class_init (GtkInfoBarClass *klass)
{
  GtkWidgetClass *widget_class;
  GObjectClass *object_class;
  GtkBindingSet *binding_set;

  widget_class = GTK_WIDGET_CLASS (klass);
  object_class = G_OBJECT_CLASS (klass);

  object_class->get_property = gtk_info_bar_get_property;
  object_class->set_property = gtk_info_bar_set_property;

  klass->close = gtk_info_bar_close;

  /**
   * GtkInfoBar:message-type:
   *
   * The type of the message.
   *
333
   * The type may be used to determine the appearance of the info bar.
334
   *
335 336
   * Since: 2.18
   */
337 338 339 340 341 342 343
  props[PROP_MESSAGE_TYPE] =
    g_param_spec_enum ("message-type",
                       P_("Message Type"),
                       P_("The type of message"),
                       GTK_TYPE_MESSAGE_TYPE,
                       GTK_MESSAGE_INFO,
                       GTK_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_EXPLICIT_NOTIFY);
344 345 346 347 348 349 350 351

  /**
   * GtkInfoBar:show-close-button:
   *
   * Whether to include a standard close button.
   *
   * Since: 3.10
   */
352 353 354 355 356 357 358
  props[PROP_SHOW_CLOSE_BUTTON] =
    g_param_spec_boolean ("show-close-button",
                          P_("Show Close Button"),
                          P_("Whether to include a standard close button"),
                          FALSE,
                          GTK_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_EXPLICIT_NOTIFY);

359 360 361 362 363 364 365
  props[PROP_REVEALED] =
    g_param_spec_boolean ("revealed",
                          P_("Reveal"),
                          P_("Controls whether the action bar shows its contents or not"),
                          TRUE,
                          GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);

366 367
  g_object_class_install_properties (object_class, LAST_PROP, props);

368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
  /**
   * GtkInfoBar::response:
   * @info_bar: the object on which the signal is emitted
   * @response_id: the response ID
   *
   * Emitted when an action widget is clicked or the application programmer
   * calls gtk_dialog_response(). The @response_id depends on which action
   * widget was clicked.
   *
   * Since: 2.18
   */
  signals[RESPONSE] = g_signal_new (I_("response"),
                                    G_OBJECT_CLASS_TYPE (klass),
                                    G_SIGNAL_RUN_LAST,
                                    G_STRUCT_OFFSET (GtkInfoBarClass, response),
                                    NULL, NULL,
                                    g_cclosure_marshal_VOID__INT,
                                    G_TYPE_NONE, 1,
                                    G_TYPE_INT);

  /**
   * GtkInfoBar::close:
   *
   * The ::close signal is a
392
   * [keybinding signal][GtkBindingSignal]
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
   * which gets emitted when the user uses a keybinding to dismiss
   * the info bar.
   *
   * The default binding for this signal is the Escape key.
   *
   * Since: 2.18
   */
  signals[CLOSE] =  g_signal_new (I_("close"),
                                  G_OBJECT_CLASS_TYPE (klass),
                                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                                  G_STRUCT_OFFSET (GtkInfoBarClass, close),
                                  NULL, NULL,
                                  g_cclosure_marshal_VOID__VOID,
                                  G_TYPE_NONE, 0);

  /**
   * GtkInfoBar:content-area-border:
   *
   * The width of the border around the content
   * content area of the info bar.
   *
   * Since: 2.18
415
   * Deprecated: 3.6: Use gtk_container_set_border_width()
416 417 418 419 420 421 422
   */
  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_int ("content-area-border",
                                                             P_("Content area border"),
                                                             P_("Width of border around the content area"),
                                                             0,
                                                             G_MAXINT,
423
                                                             CONTENT_AREA_DEFAULT_BORDER,
424
                                                             GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
425 426 427 428 429 430 431 432

  /**
   * GtkInfoBar:content-area-spacing:
   *
   * The default spacing used between elements of the
   * content area of the info bar.
   *
   * Since: 2.18
433
   * Deprecated: 3.6: Use gtk_box_set_spacing()
434 435 436 437 438 439 440
   */
  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_int ("content-area-spacing",
                                                             P_("Content area spacing"),
                                                             P_("Spacing between elements of the area"),
                                                             0,
                                                             G_MAXINT,
441
                                                             CONTENT_AREA_DEFAULT_SPACING,
442
                                                             GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
443 444 445 446 447 448 449

  /**
   * GtkInfoBar:button-spacing:
   *
   * Spacing between buttons in the action area of the info bar.
   *
   * Since: 2.18
450
   * Deprecated: 3.6: Use gtk_box_set_spacing()
451 452 453 454 455 456 457
   */
  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_int ("button-spacing",
                                                             P_("Button spacing"),
                                                             P_("Spacing between buttons"),
                                                             0,
                                                             G_MAXINT,
458
                                                             ACTION_AREA_DEFAULT_SPACING,
459
                                                             GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
460 461 462 463 464 465 466

  /**
   * GtkInfoBar:action-area-border:
   *
   * Width of the border around the action area of the info bar.
   *
   * Since: 2.18
467
   * Deprecated: 3.6: Use gtk_container_set_border_width()
468 469 470 471 472 473 474
   */
  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_int ("action-area-border",
                                                             P_("Action area border"),
                                                             P_("Width of border around the action area"),
                                                             0,
                                                             G_MAXINT,
475
                                                             ACTION_AREA_DEFAULT_BORDER,
476
                                                             GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
477 478 479

  binding_set = gtk_binding_set_by_class (klass);

480
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0, "close", 0);
481

482 483
  /* Bind class to template
   */
484
  gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/libgtk/ui/gtkinfobar.ui");
485 486
  gtk_widget_class_bind_template_child_internal_private (widget_class, GtkInfoBar, content_area);
  gtk_widget_class_bind_template_child_internal_private (widget_class, GtkInfoBar, action_area);
487 488
  gtk_widget_class_bind_template_child_private (widget_class, GtkInfoBar, close_button);
  gtk_widget_class_bind_template_child_private (widget_class, GtkInfoBar, revealer);
489 490

  gtk_widget_class_set_css_name (widget_class, "infobar");
491 492 493 494 495 496 497 498
}

static void
close_button_clicked_cb (GtkWidget  *button,
                         GtkInfoBar *info_bar)
{
  gtk_info_bar_response (GTK_INFO_BAR (info_bar),
                         GTK_RESPONSE_CLOSE);
499 500 501 502 503
}

static void
gtk_info_bar_init (GtkInfoBar *info_bar)
{
504
  GtkInfoBarPrivate *priv;
Matthias Clasen's avatar
Matthias Clasen committed
505
  GtkWidget *widget = GTK_WIDGET (info_bar);
506

507
  priv = info_bar->priv = gtk_info_bar_get_instance_private (info_bar);
508

Matthias Clasen's avatar
Matthias Clasen committed
509
  gtk_widget_set_redraw_on_allocate (widget, TRUE);
510

511 512 513
  /* message-type is a CONSTRUCT property, so we init to a value
   * different from its default to trigger its property setter
   * during construction */
514
  priv->message_type = GTK_MESSAGE_OTHER;
515

516
  gtk_widget_init_template (widget);
517

518 519
  gtk_widget_set_no_show_all (priv->close_button, TRUE);
  g_signal_connect (priv->close_button, "clicked",
520
                    G_CALLBACK (close_button_clicked_cb), info_bar);
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
}

static GtkBuildableIface *parent_buildable_iface;

static void
gtk_info_bar_buildable_interface_init (GtkBuildableIface *iface)
{
  parent_buildable_iface = g_type_interface_peek_parent (iface);
  iface->custom_tag_start = gtk_info_bar_buildable_custom_tag_start;
  iface->custom_finished = gtk_info_bar_buildable_custom_finished;
}

static gint
get_response_for_widget (GtkInfoBar *info_bar,
                         GtkWidget  *widget)
{
  ResponseData *rd;

  rd = get_response_data (widget, FALSE);
  if (!rd)
    return GTK_RESPONSE_NONE;
  else
    return rd->response_id;
}

static void
action_widget_activated (GtkWidget  *widget,
                         GtkInfoBar *info_bar)
{
  gint response_id;

  response_id = get_response_for_widget (info_bar, widget);
  gtk_info_bar_response (info_bar, response_id);
}

/**
 * gtk_info_bar_add_action_widget:
 * @info_bar: a #GtkInfoBar
 * @child: an activatable widget
 * @response_id: response ID for @child
 *
 * Add an activatable widget to the action area of a #GtkInfoBar,
 * connecting a signal handler that will emit the #GtkInfoBar::response
 * signal on the message area when the widget is activated. The widget
 * is appended to the end of the message areas action area.
 *
 * Since: 2.18
 */
void
gtk_info_bar_add_action_widget (GtkInfoBar *info_bar,
                                GtkWidget  *child,
                                gint        response_id)
{
  ResponseData *ad;
  guint signal_id;

  g_return_if_fail (GTK_IS_INFO_BAR (info_bar));
  g_return_if_fail (GTK_IS_WIDGET (child));

  ad = get_response_data (child, TRUE);

  ad->response_id = response_id;

  if (GTK_IS_BUTTON (child))
    signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
  else
    signal_id = GTK_WIDGET_GET_CLASS (child)->activate_signal;

  if (signal_id)
    {
      GClosure *closure;

      closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
                                       G_OBJECT (info_bar));
      g_signal_connect_closure_by_id (child, signal_id, 0, closure, FALSE);
    }
  else
    g_warning ("Only 'activatable' widgets can be packed into the action area of a GtkInfoBar");

  gtk_box_pack_end (GTK_BOX (info_bar->priv->action_area),
                    child, FALSE, FALSE, 0);
  if (response_id == GTK_RESPONSE_HELP)
    gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (info_bar->priv->action_area),
                                        child, TRUE);
}

/**
 * gtk_info_bar_get_action_area:
 * @info_bar: a #GtkInfoBar
 *
 * Returns the action area of @info_bar.
 *
613
 * Returns: (transfer none): the action area
614 615 616 617 618 619
 *
 * Since: 2.18
 */
GtkWidget*
gtk_info_bar_get_action_area (GtkInfoBar *info_bar)
{
620
  g_return_val_if_fail (GTK_IS_INFO_BAR (info_bar), NULL);
621 622 623 624 625 626 627 628 629 630

  return info_bar->priv->action_area;
}

/**
 * gtk_info_bar_get_content_area:
 * @info_bar: a #GtkInfoBar
 *
 * Returns the content area of @info_bar.
 *
631
 * Returns: (transfer none): the content area
632 633 634 635 636 637
 *
 * Since: 2.18
 */
GtkWidget*
gtk_info_bar_get_content_area (GtkInfoBar *info_bar)
{
638
  g_return_val_if_fail (GTK_IS_INFO_BAR (info_bar), NULL);
639 640 641 642 643 644 645

  return info_bar->priv->content_area;
}

/**
 * gtk_info_bar_add_button:
 * @info_bar: a #GtkInfoBar
646
 * @button_text: text of button
647 648
 * @response_id: response ID for the button
 *
649
 * Adds a button with the given text and sets things up so that
650
 * clicking the button will emit the “response” signal with the given
651 652 653
 * response_id. The button is appended to the end of the info bars's
 * action area. The button widget is returned, but usually you don't
 * need it.
654
 *
655 656
 * Returns: (transfer none) (type Gtk.Button): the #GtkButton widget
 * that was added
657 658 659 660 661 662 663 664 665 666 667 668 669
 *
 * Since: 2.18
 */
GtkWidget*
gtk_info_bar_add_button (GtkInfoBar  *info_bar,
                         const gchar *button_text,
                         gint         response_id)
{
  GtkWidget *button;

  g_return_val_if_fail (GTK_IS_INFO_BAR (info_bar), NULL);
  g_return_val_if_fail (button_text != NULL, NULL);

670 671 672
  button = gtk_button_new_with_label (button_text);
  gtk_button_set_use_underline (GTK_BUTTON (button), TRUE);

673 674
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;

675 676 677 678 679 680
  if (button_text)
    {
      GtkStockItem item;
      if (gtk_stock_lookup (button_text, &item))
        g_object_set (button, "use-stock", TRUE, NULL);
    }
681

682 683
  G_GNUC_END_IGNORE_DEPRECATIONS;

684
  gtk_widget_set_can_default (button, TRUE);
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763

  gtk_widget_show (button);

  gtk_info_bar_add_action_widget (info_bar, button, response_id);

  return button;
}

static void
add_buttons_valist (GtkInfoBar  *info_bar,
                    const gchar *first_button_text,
                    va_list      args)
{
  const gchar* text;
  gint response_id;

  g_return_if_fail (GTK_IS_INFO_BAR (info_bar));

  if (first_button_text == NULL)
    return;

  text = first_button_text;
  response_id = va_arg (args, gint);

  while (text != NULL)
    {
      gtk_info_bar_add_button (info_bar, text, response_id);

      text = va_arg (args, gchar*);
      if (text == NULL)
        break;

      response_id = va_arg (args, int);
    }
}

/**
 * gtk_info_bar_add_buttons:
 * @info_bar: a #GtkInfoBar
 * @first_button_text: button text or stock ID
 * @...: response ID for first button, then more text-response_id pairs,
 *     ending with %NULL
 *
 * Adds more buttons, same as calling gtk_info_bar_add_button()
 * repeatedly. The variable argument list should be %NULL-terminated
 * as with gtk_info_bar_new_with_buttons(). Each button must have both
 * text and response ID.
 *
 * Since: 2.18
 */
void
gtk_info_bar_add_buttons (GtkInfoBar  *info_bar,
                          const gchar *first_button_text,
                          ...)
{
  va_list args;

  va_start (args, first_button_text);
  add_buttons_valist (info_bar, first_button_text, args);
  va_end (args);
}

/**
 * gtk_info_bar_new:
 *
 * Creates a new #GtkInfoBar object.
 *
 * Returns: a new #GtkInfoBar object
 *
 * Since: 2.18
 */
GtkWidget *
gtk_info_bar_new (void)
{
   return g_object_new (GTK_TYPE_INFO_BAR, NULL);
}

/**
 * gtk_info_bar_new_with_buttons:
764
 * @first_button_text: (allow-none): stock ID or text to go in first button, or %NULL
765 766 767 768 769 770 771 772 773
 * @...: response ID for first button, then additional buttons, ending
 *    with %NULL
 *
 * Creates a new #GtkInfoBar with buttons. Button text/response ID
 * pairs should be listed, with a %NULL pointer ending the list.
 * Button text can be either a stock ID such as %GTK_STOCK_OK, or
 * some arbitrary text. A response ID can be any positive number,
 * or one of the values in the #GtkResponseType enumeration. If the
 * user clicks one of these dialog buttons, GtkInfoBar will emit
774
 * the “response” signal with the corresponding response ID.
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
 *
 * Returns: a new #GtkInfoBar
 */
GtkWidget*
gtk_info_bar_new_with_buttons (const gchar *first_button_text,
                               ...)
{
  GtkInfoBar *info_bar;
  va_list args;

  info_bar = GTK_INFO_BAR (gtk_info_bar_new ());

  va_start (args, first_button_text);
  add_buttons_valist (info_bar, first_button_text, args);
  va_end (args);

  return GTK_WIDGET (info_bar);
}

/**
 * gtk_info_bar_set_response_sensitive:
 * @info_bar: a #GtkInfoBar
 * @response_id: a response ID
 * @setting: TRUE for sensitive
 *
 * Calls gtk_widget_set_sensitive (widget, setting) for each
801
 * widget in the info bars’s action area with the given response_id.
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
 * A convenient way to sensitize/desensitize dialog buttons.
 *
 * Since: 2.18
 */
void
gtk_info_bar_set_response_sensitive (GtkInfoBar *info_bar,
                                     gint        response_id,
                                     gboolean    setting)
{
  GList *children, *list;

  g_return_if_fail (GTK_IS_INFO_BAR (info_bar));

  children = gtk_container_get_children (GTK_CONTAINER (info_bar->priv->action_area));

  for (list = children; list; list = list->next)
    {
      GtkWidget *widget = list->data;
      ResponseData *rd = get_response_data (widget, FALSE);

      if (rd && rd->response_id == response_id)
        gtk_widget_set_sensitive (widget, setting);
    }

  g_list_free (children);
}

/**
 * gtk_info_bar_set_default_response:
 * @info_bar: a #GtkInfoBar
 * @response_id: a response ID
 *
834
 * Sets the last widget in the info bar’s action area with
835
 * the given response_id as the default widget for the dialog.
836
 * Pressing “Enter” normally activates the default widget.
837
 *
838 839 840
 * Note that this function currently requires @info_bar to
 * be added to a widget hierarchy. 
 *
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
 * Since: 2.18
 */
void
gtk_info_bar_set_default_response (GtkInfoBar *info_bar,
                                   gint        response_id)
{
  GList *children, *list;

  g_return_if_fail (GTK_IS_INFO_BAR (info_bar));

  children = gtk_container_get_children (GTK_CONTAINER (info_bar->priv->action_area));

  for (list = children; list; list = list->next)
    {
      GtkWidget *widget = list->data;
      ResponseData *rd = get_response_data (widget, FALSE);

      if (rd && rd->response_id == response_id)
        gtk_widget_grab_default (widget);
    }

  g_list_free (children);
}

/**
866
 * gtk_info_bar_response:
867 868 869
 * @info_bar: a #GtkInfoBar
 * @response_id: a response ID
 *
870
 * Emits the “response” signal with the given @response_id.
871 872 873 874 875 876 877 878 879 880 881 882 883 884
 *
 * Since: 2.18
 */
void
gtk_info_bar_response (GtkInfoBar *info_bar,
                       gint        response_id)
{
  g_return_if_fail (GTK_IS_INFO_BAR (info_bar));

  g_signal_emit (info_bar, signals[RESPONSE], 0, response_id);
}

typedef struct
{
885
  gchar *name;
886
  gint response_id;
887 888
  gint line;
  gint col;
889 890 891 892 893 894 895
} ActionWidgetInfo;

typedef struct
{
  GtkInfoBar *info_bar;
  GtkBuilder *builder;
  GSList *items;
896 897 898
  gint response_id;
  gboolean is_text;
  GString *string;
899 900
  gint line;
  gint col;
901
} SubParserData;
902

903 904 905 906 907 908 909 910 911
static void
action_widget_info_free (gpointer data)
{
  ActionWidgetInfo *item = data;

  g_free (item->name);
  g_free (item);
}

912
static void
913 914 915 916 917 918
parser_start_element (GMarkupParseContext  *context,
                      const gchar          *element_name,
                      const gchar         **names,
                      const gchar         **values,
                      gpointer              user_data,
                      GError              **error)
919
{
920
  SubParserData *data = (SubParserData*)user_data;
921 922 923

  if (strcmp (element_name, "action-widget") == 0)
    {
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
      const gchar *response;
      GValue gvalue = G_VALUE_INIT;

      if (!_gtk_builder_check_parent (data->builder, context, "action-widgets", error))
        return;

      if (!g_markup_collect_attributes (element_name, names, values, error,
                                        G_MARKUP_COLLECT_STRING, "response", &response,
                                        G_MARKUP_COLLECT_INVALID))
        {
          _gtk_builder_prefix_error (data->builder, context, error);
          return;
        }

      if (!gtk_builder_value_from_string_type (data->builder, GTK_TYPE_RESPONSE_TYPE, response, &gvalue, error))
        {
          _gtk_builder_prefix_error (data->builder, context, error);
          return;
        }

      data->response_id = g_value_get_enum (&gvalue);
      data->is_text = TRUE;
      g_string_set_size (data->string, 0);
947
      g_markup_parse_context_get_position (context, &data->line, &data->col);
948 949
    }
  else if (strcmp (element_name, "action-widgets") == 0)
950 951 952 953 954 955 956 957 958
    {
      if (!_gtk_builder_check_parent (data->builder, context, "object", error))
        return;

      if (!g_markup_collect_attributes (element_name, names, values, error,
                                        G_MARKUP_COLLECT_INVALID, NULL, NULL,
                                        G_MARKUP_COLLECT_INVALID))
        _gtk_builder_prefix_error (data->builder, context, error);
    }
959
  else
960 961 962 963 964
    {
      _gtk_builder_error_unhandled_tag (data->builder, context,
                                        "GtkInfoBar", element_name,
                                        error);
    }
965 966 967
}

static void
968 969 970 971 972
parser_text_element (GMarkupParseContext  *context,
                     const gchar          *text,
                     gsize                 text_len,
                     gpointer              user_data,
                     GError              **error)
973
{
974
  SubParserData *data = (SubParserData*)user_data;
975

976 977 978 979 980 981 982 983 984 985 986
  if (data->is_text)
    g_string_append_len (data->string, text, text_len);
}

static void
parser_end_element (GMarkupParseContext  *context,
                    const gchar          *element_name,
                    gpointer              user_data,
                    GError              **error)
{
  SubParserData *data = (SubParserData*)user_data;
987

988 989 990 991 992
  if (data->is_text)
    {
      ActionWidgetInfo *item;

      item = g_new (ActionWidgetInfo, 1);
993
      item->name = g_strdup (data->string->str);
994
      item->response_id = data->response_id;
995 996
      item->line = data->line;
      item->col = data->col;
997 998 999 1000

      data->items = g_slist_prepend (data->items, item);
      data->is_text = FALSE;
    }
1001 1002
}

1003
static const GMarkupParser sub_parser =
1004
{
1005 1006 1007
  parser_start_element,
  parser_end_element,
  parser_text_element,
1008 1009 1010 1011 1012 1013 1014 1015
};

gboolean
gtk_info_bar_buildable_custom_tag_start (GtkBuildable  *buildable,
                                         GtkBuilder    *builder,
                                         GObject       *child,
                                         const gchar   *tagname,
                                         GMarkupParser *parser,
1016
                                         gpointer      *parser_data)
1017
{
1018
  SubParserData *data;
1019

1020
  if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
1021
                                                tagname, parser, parser_data))
1022
    return TRUE;
1023

1024
  if (!child && strcmp (tagname, "action-widgets") == 0)
1025
    {
1026 1027 1028 1029 1030 1031 1032 1033
      data = g_slice_new0 (SubParserData);
      data->info_bar = GTK_INFO_BAR (buildable);
      data->builder = builder;
      data->string = g_string_new ("");
      data->items = NULL;

      *parser = sub_parser;
      *parser_data = data;
1034 1035 1036
      return TRUE;
    }

1037
  return FALSE;
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
}

static void
gtk_info_bar_buildable_custom_finished (GtkBuildable *buildable,
                                        GtkBuilder   *builder,
                                        GObject      *child,
                                        const gchar  *tagname,
                                        gpointer      user_data)
{
  GSList *l;
1048
  SubParserData *data;
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
  GObject *object;
  GtkInfoBar *info_bar;
  ResponseData *ad;
  guint signal_id;

  if (strcmp (tagname, "action-widgets"))
    {
      parent_buildable_iface->custom_finished (buildable, builder, child,
                                               tagname, user_data);
      return;
    }

  info_bar = GTK_INFO_BAR (buildable);
1062 1063
  data = (SubParserData*)user_data;
  data->items = g_slist_reverse (data->items);
1064

1065
  for (l = data->items; l; l = l->next)
1066 1067 1068
    {
      ActionWidgetInfo *item = l->data;

1069
      object = _gtk_builder_lookup_object (builder, item->name, item->line, item->col);
1070
      if (!object)
1071
        continue;
1072 1073

      ad = get_response_data (GTK_WIDGET (object), TRUE);
1074
      ad->response_id = item->response_id;
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086

      if (GTK_IS_BUTTON (object))
        signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
      else
        signal_id = GTK_WIDGET_GET_CLASS (object)->activate_signal;

      if (signal_id)
        {
          GClosure *closure;

          closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
                                           G_OBJECT (info_bar));
1087
          g_signal_connect_closure_by_id (object, signal_id, 0, closure, FALSE);
1088 1089 1090 1091 1092 1093
        }

      if (ad->response_id == GTK_RESPONSE_HELP)
        gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (info_bar->priv->action_area),
                                            GTK_WIDGET (object), TRUE);
    }
1094

1095
  g_slist_free_full (data->items, action_widget_info_free);
1096 1097
  g_string_free (data->string, TRUE);
  g_slice_free (SubParserData, data);
1098 1099 1100 1101 1102 1103 1104 1105
}

/**
 * gtk_info_bar_set_message_type:
 * @info_bar: a #GtkInfoBar
 * @message_type: a #GtkMessageType
 *
 * Sets the message type of the message area.
1106 1107
 *
 * GTK+ uses this type to determine how the message is displayed.
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
 *
 * Since: 2.18
 */
void
gtk_info_bar_set_message_type (GtkInfoBar     *info_bar,
                               GtkMessageType  message_type)
{
  GtkInfoBarPrivate *priv;

  g_return_if_fail (GTK_IS_INFO_BAR (info_bar));

1119
  priv = info_bar->priv;
1120 1121 1122

  if (priv->message_type != message_type)
    {
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
      GtkStyleContext *context;
      AtkObject *atk_obj;
      const char *type_class[] = {
        GTK_STYLE_CLASS_INFO,
        GTK_STYLE_CLASS_WARNING,
        GTK_STYLE_CLASS_QUESTION,
        GTK_STYLE_CLASS_ERROR,
        NULL
      };

      context = gtk_widget_get_style_context (GTK_WIDGET (info_bar));

      if (type_class[priv->message_type])
        gtk_style_context_remove_class (context, type_class[priv->message_type]);

1138 1139 1140 1141 1142 1143 1144
      priv->message_type = message_type;

      gtk_widget_queue_draw (GTK_WIDGET (info_bar));

      atk_obj = gtk_widget_get_accessible (GTK_WIDGET (info_bar));
      if (GTK_IS_ACCESSIBLE (atk_obj))
        {
1145
          const char *name = NULL;
1146

1147
          atk_object_set_role (atk_obj, ATK_ROLE_INFO_BAR);
1148 1149 1150 1151

          switch (message_type)
            {
            case GTK_MESSAGE_INFO:
1152
              name = _("Information");
1153 1154 1155
              break;

            case GTK_MESSAGE_QUESTION:
1156
              name = _("Question");
1157 1158 1159
              break;

            case GTK_MESSAGE_WARNING:
1160
              name = _("Warning");
1161 1162 1163
              break;

            case GTK_MESSAGE_ERROR:
1164
              name = _("Error");
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
              break;

            case GTK_MESSAGE_OTHER:
              break;

            default:
              g_warning ("Unknown GtkMessageType %u", message_type);
              break;
            }

1175
          if (name)
1176
            atk_object_set_name (atk_obj, name);
1177 1178
        }

1179 1180 1181
      if (type_class[priv->message_type])
        gtk_style_context_add_class (context, type_class[priv->message_type]);

1182
      g_object_notify_by_pspec (G_OBJECT (info_bar), props[PROP_MESSAGE_TYPE]);
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
    }
}

/**
 * gtk_info_bar_get_message_type:
 * @info_bar: a #GtkInfoBar
 *
 * Returns the message type of the message area.
 *
 * Returns: the message type of the message area.
 *
 * Since: 2.18
 */
GtkMessageType
gtk_info_bar_get_message_type (GtkInfoBar *info_bar)
{
1199
  g_return_val_if_fail (GTK_IS_INFO_BAR (info_bar), GTK_MESSAGE_OTHER);
1200

1201
  return info_bar->priv->message_type;
1202
}
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224


/**
 * gtk_info_bar_set_show_close_button:
 * @info_bar: a #GtkInfoBar
 * @setting: %TRUE to include a close button
 *
 * If true, a standard close button is shown. When clicked it emits
 * the response %GTK_RESPONSE_CLOSE.
 *
 * Since: 3.10
 */
void
gtk_info_bar_set_show_close_button (GtkInfoBar *info_bar,
                                    gboolean    setting)
{
  g_return_if_fail (GTK_IS_INFO_BAR (info_bar));

  if (setting != info_bar->priv->show_close_button)
    {
      info_bar->priv->show_close_button = setting;
      gtk_widget_set_visible (info_bar->priv->close_button, setting);
1225
      g_object_notify_by_pspec (G_OBJECT (info_bar), props[PROP_SHOW_CLOSE_BUTTON]);
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
    }
}

/**
 * gtk_info_bar_get_show_close_button:
 * @info_bar: a #GtkInfoBar
 *
 * Returns whether the widget will display a standard close button.
 *
 * Returns: %TRUE if the widget displays standard close button
 *
 * Since: 3.10
 */
gboolean
gtk_info_bar_get_show_close_button (GtkInfoBar *info_bar)
{
  g_return_val_if_fail (GTK_IS_INFO_BAR (info_bar), FALSE);

  return info_bar->priv->show_close_button;
}
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257

/**
 * gtk_info_bar_set_revealed:
 * @info_bar: a #GtkActionBar
 * @revealed: The new value of the property
 *
 * Sets the GtkInfoBar:revealed property to @revealed. This will cause
 * @info_bar to show up with a slide-in transition.
 *
 * Note that this property does not automatically show @info_bar and thus won’t
 * have any effect if it is invisible.
 *
1258
 * Since: 3.22.29
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
 */
void
gtk_info_bar_set_revealed (GtkInfoBar *info_bar,
                           gboolean    revealed)
{
  GtkInfoBarPrivate *priv = gtk_info_bar_get_instance_private (info_bar);

  g_return_if_fail (GTK_IS_INFO_BAR (info_bar));

  revealed = !!revealed;
  if (revealed != gtk_revealer_get_reveal_child (GTK_REVEALER (priv->revealer)))
    {
      gtk_revealer_set_reveal_child (GTK_REVEALER (priv->revealer), revealed);
      g_object_notify_by_pspec (G_OBJECT (info_bar), props[PROP_REVEALED]);
    }
}

/**
 * gtk_info_bar_get_revealed:
 * @info_bar: a #GtkInfoBar
 *
 * Returns: the current value of the GtkInfoBar:revealed property.
 *
1282
 * Since: 3.22.29
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
 */
gboolean
gtk_info_bar_get_revealed (GtkInfoBar *info_bar)
{
  GtkInfoBarPrivate *priv = gtk_info_bar_get_instance_private (info_bar);

  g_return_val_if_fail (GTK_IS_INFO_BAR (info_bar), FALSE);

  return gtk_revealer_get_reveal_child (GTK_REVEALER (priv->revealer));
}