## GtkSearchBar GtkSearchBar — A toolbar to integrate a search entry with |

## Synopsis

    #include <gtk/gtk.h>

    struct              GtkSearchBar;
    GtkWidget *         gtk_search_bar_new                  (void);
    void                gtk_search_bar_connect_entry        (GtkSearchBar *bar,
                                                             GtkEntry *entry);
    gboolean            gtk_search_bar_get_search_mode      (GtkSearchBar *bar);
    void                gtk_search_bar_set_search_mode      (GtkSearchBar *bar,
                                                             gboolean search_mode);
    gboolean            gtk_search_bar_get_show_close_button
                                                            (GtkSearchBar *bar);
    void                gtk_search_bar_set_show_close_button
                                                            (GtkSearchBar *bar,
                                                             gboolean visible);
    gboolean            gtk_search_bar_handle_event         (GtkSearchBar *bar,
                                                             GdkEvent *event);

## Object Hierarchy

      GObject
       +----GInitiallyUnowned
             +----GtkWidget
                   +----GtkContainer
                         +----GtkBin
                               +----GtkSearchBar

## Implemented Interfaces

GtkSearchBar implements AtkImplementorIface and GtkBuildable.

## Properties

      "search-mode-enabled"      gboolean              : Read / Write
      "show-close-button"        gboolean              : Read / Write / Construct

## Description

GtkSearchBar is a container made to have a search entry (possibly with additional connex widgets, such as drop-down menus, or buttons) built-in. The search bar would appear when a search is started through typing on the keyboard, or the application's search mode is toggled on.

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.

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.

Example 27. Creating a search bar

    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
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    #include <gtk/gtk.h>

    static gboolean
    window_key_press_event_cb (GtkWidget *window,
        GdkEvent *event,
        GtkSearchBar *search_bar)
    {
      return gtk_search_bar_handle_event (search_bar, event);
    }

    static void
    activate_cb (GtkApplication *app,
        gpointer user_data)
    {
      GtkWidget *window;
      GtkWidget *search_bar;
      GtkWidget *box;
      GtkWidget *entry;
      GtkWidget *menu_button;

      window = gtk_application_window_new (app);
      gtk_widget_show (window);

      search_bar = gtk_search_bar_new ();
      gtk_container_add (GTK_CONTAINER (window), search_bar);
      gtk_widget_show (search_bar);

      box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
      gtk_container_add (GTK_CONTAINER (search_bar), box);
      gtk_widget_show (box);

      entry = gtk_search_entry_new ();
      gtk_box_pack_start (GTK_BOX (box), entry, TRUE, TRUE, 0);
      gtk_widget_show (entry);

      menu_button = gtk_menu_button_new ();
      gtk_box_pack_start (GTK_BOX (box), menu_button, FALSE, FALSE, 0);
      gtk_widget_show (menu_button);

      gtk_search_bar_connect_entry (GTK_SEARCH_BAR (search_bar), GTK_ENTRY (entry));

      g_signal_connect (window, "key-press-event",
          G_CALLBACK (window_key_press_event_cb), search_bar);
    }

    gint
    main (gint argc,
        gchar *argv[])
    {
      GtkApplication *app;

      app = gtk_application_new ("org.gtk.Example.GtkSearchBar",
          G_APPLICATION_FLAGS_NONE);
      g_signal_connect (app, "activate",
          G_CALLBACK (activate_cb), NULL);

      return g_application_run (G_APPLICATION (app), argc, argv);
    }

## Details

### struct GtkSearchBar

    struct GtkSearchBar;

### gtk_search_bar_new ()

    GtkWidget *         gtk_search_bar_new                  (void);

Creates a GtkSearchBar. You will need to tell it about which widget is going to be your text entry using `gtk_search_bar_set_entry()`.

Returns : | a new GtkSearchBar

Since 3.10

### gtk_search_bar_connect_entry ()

    void                gtk_search_bar_connect_entry        (GtkSearchBar *bar,
                                                             GtkEntry *entry);

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).

`bar` : | a GtkSearchBar
`entry` : | a GtkEntry

Since 3.10

### gtk_search_bar_get_search_mode ()

    gboolean            gtk_search_bar_get_search_mode      (GtkSearchBar *bar);

Returns whether the search mode is on or off.

`bar` : | a GtkSearchBar
Returns : | whether search mode is toggled on

Since 3.10

### gtk_search_bar_set_search_mode ()

    void                gtk_search_bar_set_search_mode      (GtkSearchBar *bar,
                                                             gboolean search_mode);

Switches the search mode on or off.

`bar` : | a GtkSearchBar
`search_mode` : | the new state of the search mode

Since 3.10

### gtk_search_bar_get_show_close_button ()

    gboolean            gtk_search_bar_get_show_close_button
                                                            (GtkSearchBar *bar);

Returns whether the close button is shown.

`bar` : | a GtkSearchBar
Returns : | whether the close button is shown

Since 3.10

### gtk_search_bar_set_show_close_button ()

    void                gtk_search_bar_set_show_close_button
                                                            (GtkSearchBar *bar,
                                                             gboolean visible);

Shows or hides the close button. Applications that already have a "search" toggle button should not show a close button in their search bar, as it duplicates the role of the toggle button.

`bar` : | a GtkSearchBar
`visible` : | whether the close button will be shown or not

Since 3.10

### gtk_search_bar_handle_event ()

    gboolean            gtk_search_bar_handle_event         (GtkSearchBar *bar,
                                                             GdkEvent *event);

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 `gtk_search_bar_connect_entry()`, this function will return immediately with a warning.

Example 28. Showing the search bar on key presses

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    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);

`bar` : | a GtkSearchBar
`event` : | a GdkEvent containing key press events
Returns : | `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.

Since 3.10

## Property Details

### The `"search-mode-enabled"` property

      "search-mode-enabled"      gboolean              : Read / Write

Whether the search mode is on and the search bar shown.

Default value: FALSE

### The `"show-close-button"` property

      "show-close-button"        gboolean              : Read / Write / Construct

Whether to show the close button in the toolbar.

Default value: FALSE