gtksearchbar.c 17.5 KB
Newer Older
Bastien Nocera's avatar
Bastien Nocera committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/* GTK - The GIMP Toolkit
 * Copyright (C) 2013 Red Hat, Inc.
 *
 * Authors:
 * - Bastien Nocera <bnocera@redhat.com>
 *
 * 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
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Modified by the GTK+ Team and others 2013.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
 */

#include "config.h"

#include "gtkentry.h"
#include "gtkentryprivate.h"
#include "gtkintl.h"
#include "gtkstylecontext.h"
#include "gtksearchbar.h"

/**
 * SECTION:gtksearchbar
38
 * @Short_description: A toolbar to integrate a search entry with
Bastien Nocera's avatar
Bastien Nocera committed
39 40 41
 * @Title: GtkSearchBar
 *
 * #GtkSearchBar is a container made to have a search entry (possibly
42 43
 * with additional connex widgets, such as drop-down menus, or buttons)
 * built-in. The search bar would appear when a search is started through
44
 * typing on the keyboard, or the application’s search mode is toggled on.
Bastien Nocera's avatar
Bastien Nocera committed
45
 *
46 47 48 49 50
 * For keyboard presses to start a search, events will need to be
 * forwarded from the top-level window that contains the search bar.
 * See gtk_search_bar_handle_event() for example code. Common shortcuts
 * such as Ctrl+F should be handled as an application action, or through
 * the menu items.
Bastien Nocera's avatar
Bastien Nocera committed
51
 *
52 53 54 55
 * You will also need to tell the search bar about which entry you
 * are using as your search entry using gtk_search_bar_connect_entry().
 * The following example shows you how to create a more complex search
 * entry.
Bastien Nocera's avatar
Bastien Nocera committed
56
 *
57 58
 * ## Creating a search bar
 *
59
 * [A simple example](https://git.gnome.org/browse/gtk+/tree/examples/search-bar.c)
Bastien Nocera's avatar
Bastien Nocera committed
60 61 62 63
 *
 * Since: 3.10
 */

64
typedef struct {
Bastien Nocera's avatar
Bastien Nocera committed
65 66
  /* Template widgets */
  GtkWidget   *revealer;
67
  GtkWidget   *tool_box;
Bastien Nocera's avatar
Bastien Nocera committed
68 69 70 71 72
  GtkWidget   *box_center;
  GtkWidget   *close_button;

  GtkWidget   *entry;
  gboolean     reveal_child;
73 74 75
} GtkSearchBarPrivate;

G_DEFINE_TYPE_WITH_PRIVATE (GtkSearchBar, gtk_search_bar, GTK_TYPE_BIN)
Bastien Nocera's avatar
Bastien Nocera committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

enum {
  PROP_0,
  PROP_SEARCH_MODE_ENABLED,
  PROP_SHOW_CLOSE_BUTTON,
  LAST_PROPERTY
};

static GParamSpec *widget_props[LAST_PROPERTY] = { NULL, };

static gboolean
is_keynav_event (GdkEvent *event,
                 guint     keyval)
{
  GdkModifierType state = 0;

  gdk_event_get_state (event, &state);

  if (keyval == GDK_KEY_Tab ||
      keyval == GDK_KEY_KP_Tab ||
      keyval == GDK_KEY_Up ||
      keyval == GDK_KEY_KP_Up ||
      keyval == GDK_KEY_Down ||
      keyval == GDK_KEY_KP_Down ||
      keyval == GDK_KEY_Left ||
      keyval == GDK_KEY_KP_Left ||
      keyval == GDK_KEY_Right ||
      keyval == GDK_KEY_KP_Right ||
      keyval == GDK_KEY_Home ||
      keyval == GDK_KEY_KP_Home ||
      keyval == GDK_KEY_End ||
      keyval == GDK_KEY_KP_End ||
      keyval == GDK_KEY_Page_Up ||
      keyval == GDK_KEY_KP_Page_Up ||
      keyval == GDK_KEY_Page_Down ||
      keyval == GDK_KEY_KP_Page_Down ||
      ((state & (GDK_CONTROL_MASK | GDK_MOD1_MASK)) != 0))
        return TRUE;

  /* Other navigation events should get automatically
   * ignored as they will not change the content of the entry
   */

  return FALSE;
}

static gboolean
entry_key_pressed_event_cb (GtkWidget    *widget,
                            GdkEvent     *event,
                            GtkSearchBar *bar)
{
127
  GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
Bastien Nocera's avatar
Bastien Nocera committed
128 129 130 131 132 133
  guint keyval;

  if (!gdk_event_get_keyval (event, &keyval) ||
      keyval != GDK_KEY_Escape)
    return GDK_EVENT_PROPAGATE;

134 135
  gtk_revealer_set_reveal_child (GTK_REVEALER (priv->revealer), FALSE);

Bastien Nocera's avatar
Bastien Nocera committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  return GDK_EVENT_STOP;
}

static void
preedit_changed_cb (GtkEntry  *entry,
                    GtkWidget *popup,
                    gboolean  *preedit_changed)
{
  *preedit_changed = TRUE;
}

/**
 * gtk_search_bar_handle_event:
 * @bar: a #GtkSearchBar
 * @event: a #GdkEvent containing key press events
 *
 * This function should be called when the top-level
 * window which contains the search bar received a key event.
 *
 * If the key event is handled by the search bar, the bar will
 * be shown, the entry populated with the entered text and %GDK_EVENT_STOP
 * will be returned. The caller should ensure that events are
 * not propagated further.
 *
 * If no entry has been connected to the search bar, using
161 162
 * gtk_search_bar_connect_entry(), this function will return
 * immediately with a warning.
Bastien Nocera's avatar
Bastien Nocera committed
163
 *
164 165
 * ## Showing the search bar on key presses
 *
166
 * |[<!-- language="C" -->
Bastien Nocera's avatar
Bastien Nocera committed
167 168 169 170 171 172 173 174 175 176
 * static gboolean
 * window_key_press_event_cb (GtkWidget *widget,
 *                            GdkEvent  *event,
 *                            gpointer   user_data)
 * {
 *   return gtk_search_bar_handle_event (GTK_SEARCH_BAR (user_data), event);
 * }
 *
 * g_signal_connect (window, "key-press-event",
 *                   G_CALLBACK (window_key_press_event_cb), search_bar);
177
 * ]|
Bastien Nocera's avatar
Bastien Nocera committed
178
 *
179 180 181
 * Return value: %GDK_EVENT_STOP if the key press event resulted
 *     in text being entered in the search entry (and revealing
 *     the search bar if necessary), %GDK_EVENT_PROPAGATE otherwise.
Bastien Nocera's avatar
Bastien Nocera committed
182 183
 *
 * Since: 3.10
184
 */
Bastien Nocera's avatar
Bastien Nocera committed
185 186 187 188
gboolean
gtk_search_bar_handle_event (GtkSearchBar *bar,
                             GdkEvent     *event)
{
189
  GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
Bastien Nocera's avatar
Bastien Nocera committed
190 191 192 193 194 195 196
  guint keyval;
  gboolean handled;
  gboolean preedit_changed;
  guint preedit_change_id;
  gboolean res;
  char *old_text, *new_text;

197
  if (priv->entry == NULL)
Bastien Nocera's avatar
Bastien Nocera committed
198 199 200 201 202 203 204 205 206
    {
      g_warning ("The search bar does not have an entry connected to it. Call gtk_search_bar_connect_entry() to connect one.");
      return GDK_EVENT_PROPAGATE;
    }

  /* Exit early if the search bar is already shown,
   * the event doesn't contain a key press,
   * or the event is a navigation or space bar key press
   */
207
  if (priv->reveal_child ||
Bastien Nocera's avatar
Bastien Nocera committed
208 209
      !gdk_event_get_keyval (event, &keyval) ||
      is_keynav_event (event, keyval) ||
210 211
      keyval == GDK_KEY_space ||
      keyval == GDK_KEY_Menu)
Bastien Nocera's avatar
Bastien Nocera committed
212 213
    return GDK_EVENT_PROPAGATE;

214 215
  if (!gtk_widget_get_realized (priv->entry))
    gtk_widget_realize (priv->entry);
Bastien Nocera's avatar
Bastien Nocera committed
216 217 218

  handled = GDK_EVENT_PROPAGATE;
  preedit_changed = FALSE;
219
  preedit_change_id = g_signal_connect (priv->entry, "preedit-changed",
Bastien Nocera's avatar
Bastien Nocera committed
220 221
                                        G_CALLBACK (preedit_changed_cb), &preedit_changed);

222 223 224
  old_text = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry)));
  res = gtk_widget_event (priv->entry, event);
  new_text = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry)));
Bastien Nocera's avatar
Bastien Nocera committed
225

226
  g_signal_handler_disconnect (priv->entry, preedit_change_id);
Bastien Nocera's avatar
Bastien Nocera committed
227 228 229 230

  if ((res && g_strcmp0 (new_text, old_text) != 0) || preedit_changed)
    {
      handled = GDK_EVENT_STOP;
231
      gtk_revealer_set_reveal_child (GTK_REVEALER (priv->revealer), TRUE);
Bastien Nocera's avatar
Bastien Nocera committed
232 233 234 235 236 237 238 239 240 241 242 243 244
    }

  g_free (old_text);
  g_free (new_text);

  return handled;
}

static void
reveal_child_changed_cb (GObject      *object,
                         GParamSpec   *pspec,
                         GtkSearchBar *bar)
{
245
  GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
Bastien Nocera's avatar
Bastien Nocera committed
246 247 248
  gboolean reveal_child;

  g_object_get (object, "reveal-child", &reveal_child, NULL);
249
  if (reveal_child == priv->reveal_child)
Bastien Nocera's avatar
Bastien Nocera committed
250 251
    return;

252
  priv->reveal_child = reveal_child;
Bastien Nocera's avatar
Bastien Nocera committed
253 254

  if (reveal_child)
255
    _gtk_entry_grab_focus (GTK_ENTRY (priv->entry), FALSE);
Bastien Nocera's avatar
Bastien Nocera committed
256
  else
257
    gtk_entry_set_text (GTK_ENTRY (priv->entry), "");
Bastien Nocera's avatar
Bastien Nocera committed
258 259 260 261 262 263 264 265

  g_object_notify (G_OBJECT (bar), "search-mode-enabled");
}

static void
close_button_clicked_cb (GtkWidget    *button,
                         GtkSearchBar *bar)
{
266 267 268
  GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);

  gtk_revealer_set_reveal_child (GTK_REVEALER (priv->revealer), FALSE);
Bastien Nocera's avatar
Bastien Nocera committed
269 270 271 272 273 274 275
}

static void
gtk_search_bar_add (GtkContainer *container,
                    GtkWidget    *child)
{
  GtkSearchBar *bar = GTK_SEARCH_BAR (container);
276
  GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
Bastien Nocera's avatar
Bastien Nocera committed
277 278 279 280

  /* When constructing the widget, we want the revealer to be added
   * as the first child of the search bar, as an implementation detail.
   * After that, the child added by the application should be added
281
   * to box_center.
Bastien Nocera's avatar
Bastien Nocera committed
282
   */
283
  if (priv->box_center == NULL)
Bastien Nocera's avatar
Bastien Nocera committed
284 285
    {
      GTK_CONTAINER_CLASS (gtk_search_bar_parent_class)->add (container, child);
286 287 288
    }
  else
    {
289
      gtk_container_add (GTK_CONTAINER (priv->box_center), child);
Bastien Nocera's avatar
Bastien Nocera committed
290
      /* If an entry is the only child, save the developer a couple of
291 292
       * lines of code
       */
Bastien Nocera's avatar
Bastien Nocera committed
293 294 295 296 297 298
      if (GTK_IS_ENTRY (child))
        gtk_search_bar_connect_entry (bar, GTK_ENTRY (child));
    }
}

static void
299 300 301 302
gtk_search_bar_set_property (GObject      *object,
                             guint         prop_id,
                             const GValue *value,
                             GParamSpec   *pspec)
Bastien Nocera's avatar
Bastien Nocera committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
{
  GtkSearchBar *bar = GTK_SEARCH_BAR (object);

  switch (prop_id)
    {
    case PROP_SEARCH_MODE_ENABLED:
      gtk_search_bar_set_search_mode (bar, g_value_get_boolean (value));
      break;
    case PROP_SHOW_CLOSE_BUTTON:
      gtk_search_bar_set_show_close_button (bar, g_value_get_boolean (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
321 322 323 324
gtk_search_bar_get_property (GObject    *object,
                             guint       prop_id,
                             GValue     *value,
                             GParamSpec *pspec)
Bastien Nocera's avatar
Bastien Nocera committed
325 326
{
  GtkSearchBar *bar = GTK_SEARCH_BAR (object);
327
  GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
Bastien Nocera's avatar
Bastien Nocera committed
328 329 330 331

  switch (prop_id)
    {
    case PROP_SEARCH_MODE_ENABLED:
332
      g_value_set_boolean (value, priv->reveal_child);
Bastien Nocera's avatar
Bastien Nocera committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346
      break;
    case PROP_SHOW_CLOSE_BUTTON:
      g_value_set_boolean (value, gtk_search_bar_get_show_close_button (bar));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_search_bar_dispose (GObject *object)
{
  GtkSearchBar *bar = GTK_SEARCH_BAR (object);
347
  GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
Bastien Nocera's avatar
Bastien Nocera committed
348

349
  if (priv->entry)
Bastien Nocera's avatar
Bastien Nocera committed
350
    {
351 352 353
      g_signal_handlers_disconnect_by_func (priv->entry, entry_key_pressed_event_cb, bar);
      g_object_remove_weak_pointer (G_OBJECT (priv->entry), (gpointer *) &priv->entry);
      priv->entry = NULL;
Bastien Nocera's avatar
Bastien Nocera committed
354 355 356 357 358
    }

  G_OBJECT_CLASS (gtk_search_bar_parent_class)->dispose (object);
}

359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
static gboolean
gtk_search_bar_draw (GtkWidget *widget,
                     cairo_t *cr)
{
  gint width, height;
  GtkStyleContext *context;

  width = gtk_widget_get_allocated_width (widget);
  height = gtk_widget_get_allocated_height (widget);
  context = gtk_widget_get_style_context (widget);

  gtk_render_background (context, cr, 0, 0, width, height);
  gtk_render_frame (context, cr, 0, 0, width, height);

  GTK_WIDGET_CLASS (gtk_search_bar_parent_class)->draw (widget, cr);

  return FALSE;
}

Bastien Nocera's avatar
Bastien Nocera committed
378 379 380 381 382 383 384 385 386 387
static void
gtk_search_bar_class_init (GtkSearchBarClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);

  object_class->dispose = gtk_search_bar_dispose;
  object_class->set_property = gtk_search_bar_set_property;
  object_class->get_property = gtk_search_bar_get_property;
388
  widget_class->draw = gtk_search_bar_draw;
Bastien Nocera's avatar
Bastien Nocera committed
389 390 391 392 393 394 395 396 397

  container_class->add = gtk_search_bar_add;

  /**
   * GtkEntry:search-mode-enabled:
   *
   * Whether the search mode is on and the search bar shown.
   *
   * See gtk_search_bar_set_search_mode() for details.
398
   */
Bastien Nocera's avatar
Bastien Nocera committed
399 400 401 402 403
  widget_props[PROP_SEARCH_MODE_ENABLED] = g_param_spec_boolean ("search-mode-enabled",
                                                                 P_("Search Mode Enabled"),
                                                                 P_("Whether the search mode is on and the search bar shown"),
                                                                 FALSE,
                                                                 G_PARAM_READWRITE);
404

Bastien Nocera's avatar
Bastien Nocera committed
405 406 407 408
  /**
   * GtkEntry:show-close-button:
   *
   * Whether to show the close button in the toolbar.
409
   */
Bastien Nocera's avatar
Bastien Nocera committed
410 411 412
  widget_props[PROP_SHOW_CLOSE_BUTTON] = g_param_spec_boolean ("show-close-button",
                                                               P_("Show Close Button"),
                                                               P_("Whether to show the close button in the toolbar"),
413
                                                               FALSE,
Bastien Nocera's avatar
Bastien Nocera committed
414 415 416 417
                                                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT);

  g_object_class_install_properties (object_class, LAST_PROPERTY, widget_props);

418
  gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/libgtk/ui/gtksearchbar.ui");
419
  gtk_widget_class_bind_template_child_internal_private (widget_class, GtkSearchBar, tool_box);
420 421 422
  gtk_widget_class_bind_template_child_internal_private (widget_class, GtkSearchBar, revealer);
  gtk_widget_class_bind_template_child_internal_private (widget_class, GtkSearchBar, box_center);
  gtk_widget_class_bind_template_child_internal_private (widget_class, GtkSearchBar, close_button);
Bastien Nocera's avatar
Bastien Nocera committed
423 424 425 426 427
}

static void
gtk_search_bar_init (GtkSearchBar *bar)
{
428
  GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
429
  GtkStyleContext *context;
Bastien Nocera's avatar
Bastien Nocera committed
430 431 432

  gtk_widget_init_template (GTK_WIDGET (bar));

433
  gtk_widget_show_all (priv->tool_box);
Bastien Nocera's avatar
Bastien Nocera committed
434

435
  g_signal_connect (priv->revealer, "notify::reveal-child",
Bastien Nocera's avatar
Bastien Nocera committed
436 437
                    G_CALLBACK (reveal_child_changed_cb), bar);

438 439
  gtk_widget_set_no_show_all (priv->close_button, TRUE);
  g_signal_connect (priv->close_button, "clicked",
Bastien Nocera's avatar
Bastien Nocera committed
440
                    G_CALLBACK (close_button_clicked_cb), bar);
441 442 443 444 445

  context = gtk_widget_get_style_context (GTK_WIDGET (bar));
  gtk_style_context_add_class (context, "search-bar");
  gtk_style_context_add_class (context, GTK_STYLE_CLASS_HORIZONTAL);

Bastien Nocera's avatar
Bastien Nocera committed
446 447 448 449 450 451 452
};

/**
 * gtk_search_bar_new:
 *
 * Creates a #GtkSearchBar. You will need to tell it about
 * which widget is going to be your text entry using
453
 * gtk_search_bar_connect_entry().
Bastien Nocera's avatar
Bastien Nocera committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
 *
 * Return value: a new #GtkSearchBar
 *
 * Since: 3.10
 */
GtkWidget *
gtk_search_bar_new (void)
{
  return g_object_new (GTK_TYPE_SEARCH_BAR, NULL);
}

/**
 * gtk_search_bar_connect_entry:
 * @bar: a #GtkSearchBar
 * @entry: a #GtkEntry
 *
 * Connects the #GtkEntry widget passed as the one to be used in
 * this search bar. The entry should be a descendant of the search bar.
 * This is only required if the entry isn't the direct child of the
 * search bar (as in our main example).
 *
 * Since: 3.10
476
 */
Bastien Nocera's avatar
Bastien Nocera committed
477 478 479 480
void
gtk_search_bar_connect_entry (GtkSearchBar *bar,
                              GtkEntry     *entry)
{
481 482
  GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);

Bastien Nocera's avatar
Bastien Nocera committed
483 484 485
  g_return_if_fail (GTK_IS_SEARCH_BAR (bar));
  g_return_if_fail (entry == NULL || GTK_IS_ENTRY (entry));

486
  if (priv->entry != NULL)
Bastien Nocera's avatar
Bastien Nocera committed
487
    {
488 489 490
      g_signal_handlers_disconnect_by_func (priv->entry, entry_key_pressed_event_cb, bar);
      g_object_remove_weak_pointer (G_OBJECT (priv->entry), (gpointer *) &priv->entry);
      priv->entry = NULL;
Bastien Nocera's avatar
Bastien Nocera committed
491 492 493 494
    }

  if (entry != NULL)
    {
495 496 497
      priv->entry = GTK_WIDGET (entry);
      g_object_add_weak_pointer (G_OBJECT (priv->entry), (gpointer *) &priv->entry);
      g_signal_connect (priv->entry, "key-press-event",
Bastien Nocera's avatar
Bastien Nocera committed
498 499 500 501 502 503 504 505 506 507 508 509 510
                        G_CALLBACK (entry_key_pressed_event_cb), bar);
    }
}

/**
 * gtk_search_bar_get_search_mode:
 * @bar: a #GtkSearchBar
 *
 * Returns whether the search mode is on or off.
 *
 * Return value: whether search mode is toggled on
 *
 * Since: 3.10
511
 */
Bastien Nocera's avatar
Bastien Nocera committed
512 513 514
gboolean
gtk_search_bar_get_search_mode (GtkSearchBar *bar)
{
515 516
  GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);

Bastien Nocera's avatar
Bastien Nocera committed
517 518
  g_return_val_if_fail (GTK_IS_SEARCH_BAR (bar), FALSE);

519
  return priv->reveal_child;
Bastien Nocera's avatar
Bastien Nocera committed
520 521 522 523 524 525 526 527 528 529
}

/**
 * gtk_search_bar_set_search_mode:
 * @bar: a #GtkSearchBar
 * @search_mode: the new state of the search mode
 *
 * Switches the search mode on or off.
 *
 * Since: 3.10
530
 */
Bastien Nocera's avatar
Bastien Nocera committed
531 532 533 534
void
gtk_search_bar_set_search_mode (GtkSearchBar *bar,
                                gboolean      search_mode)
{
535 536
  GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);

Bastien Nocera's avatar
Bastien Nocera committed
537 538
  g_return_if_fail (GTK_IS_SEARCH_BAR (bar));

539
  gtk_revealer_set_reveal_child (GTK_REVEALER (priv->revealer), search_mode);
Bastien Nocera's avatar
Bastien Nocera committed
540 541 542 543 544 545 546 547 548 549 550
}

/**
 * gtk_search_bar_get_show_close_button:
 * @bar: a #GtkSearchBar
 *
 * Returns whether the close button is shown.
 *
 * Return value: whether the close button is shown
 *
 * Since: 3.10
551
 */
Bastien Nocera's avatar
Bastien Nocera committed
552 553 554
gboolean
gtk_search_bar_get_show_close_button (GtkSearchBar *bar)
{
555 556
  GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);

Bastien Nocera's avatar
Bastien Nocera committed
557 558
  g_return_val_if_fail (GTK_IS_SEARCH_BAR (bar), FALSE);

559
  return gtk_widget_get_visible (priv->close_button);
Bastien Nocera's avatar
Bastien Nocera committed
560 561 562 563 564
}

/**
 * gtk_search_bar_set_show_close_button:
 * @bar: a #GtkSearchBar
565
 * @visible: whether the close button will be shown or not
Bastien Nocera's avatar
Bastien Nocera committed
566
 *
567
 * Shows or hides the close button. Applications that
568
 * already have a “search” toggle button should not show a close
569 570
 * button in their search bar, as it duplicates the role of the
 * toggle button.
Bastien Nocera's avatar
Bastien Nocera committed
571 572
 *
 * Since: 3.10
573
 */
Bastien Nocera's avatar
Bastien Nocera committed
574 575 576 577
void
gtk_search_bar_set_show_close_button (GtkSearchBar *bar,
                                      gboolean      visible)
{
578 579
  GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);

Bastien Nocera's avatar
Bastien Nocera committed
580 581
  g_return_if_fail (GTK_IS_SEARCH_BAR (bar));

582
  gtk_widget_set_visible (priv->close_button, visible);
Bastien Nocera's avatar
Bastien Nocera committed
583
}