HdySearchBar

HdySearchBar — A toolbar to integrate a search entry with.

Functions

Properties

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

Types and Values

Object Hierarchy

    GObject
    ╰── GInitiallyUnowned
        ╰── GtkWidget
            ╰── GtkContainer
                ╰── GtkBin
                    ╰── HdySearchBar

Implemented Interfaces

HdySearchBar implements AtkImplementorIface and GtkBuildable.

Description

HdySearchBar 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 hdy_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 hdy_search_bar_connect_entry(). The following example shows you how to create a more complex search entry.

HdySearchBar is very similar to GtkSearchBar, the main difference being that it allows the search entry to fill all the available space. This allows you to control your search entry's width with a HdyColumn.

CSS nodes

HdySearchBar has a single CSS node with name searchbar.

Functions

hdy_search_bar_new ()

GtkWidget *
hdy_search_bar_new (void);

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

Returns

a new HdySearchBar

Since: 0.0.6


hdy_search_bar_connect_entry ()

void
hdy_search_bar_connect_entry (HdySearchBar *self,
                              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).

Parameters

self

a HdySearchBar

 

entry

a GtkEntry

 

Since: 0.0.6


hdy_search_bar_get_search_mode ()

gboolean
hdy_search_bar_get_search_mode (HdySearchBar *self);

Returns whether the search mode is on or off.

Parameters

self

a HdySearchBar

 

Returns

whether search mode is toggled on

Since: 0.0.6


hdy_search_bar_set_search_mode ()

void
hdy_search_bar_set_search_mode (HdySearchBar *self,
                                gboolean search_mode);

Switches the search mode on or off.

Parameters

self

a HdySearchBar

 

search_mode

the new state of the search mode

 

Since: 0.0.6


hdy_search_bar_get_show_close_button ()

gboolean
hdy_search_bar_get_show_close_button (HdySearchBar *self);

Returns whether the close button is shown.

Parameters

self

a HdySearchBar

 

Returns

whether the close button is shown

Since: 0.0.6


hdy_search_bar_set_show_close_button ()

void
hdy_search_bar_set_show_close_button (HdySearchBar *self,
                                      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.

Parameters

self

a HdySearchBar

 

visible

whether the close button will be shown or not

 

Since: 0.0.6


hdy_search_bar_handle_event ()

gboolean
hdy_search_bar_handle_event (HdySearchBar *self,
                             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 hdy_search_bar_connect_entry(), this function will return immediately with a warning.

Showing the search bar on key presses

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
static gboolean
on_key_press_event (GtkWidget *widget,
                    GdkEvent  *event,
                    gpointer   user_data)
{
  HdySearchBar *bar = HDY_SEARCH_BAR (user_data);
  return hdy_search_bar_handle_event (self, event);
}

static void
create_toplevel (void)
{
  GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  GtkWindow *search_bar = hdy_search_bar_new ();

 // Add more widgets to the window...

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

Parameters

self

a HdySearchBar

 

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: 0.0.6

Types and Values

HDY_TYPE_SEARCH_BAR

#define HDY_TYPE_SEARCH_BAR (hdy_search_bar_get_type())

struct HdySearchBarClass

struct HdySearchBarClass {
  GtkBinClass parent_class;
};

HdySearchBar

typedef struct _HdySearchBar HdySearchBar;

Property Details

The “search-mode-enabled” property

  “search-mode-enabled”      gboolean

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

See hdy_search_bar_set_search_mode() for details.

Flags: Read / Write

Default value: FALSE


The “show-close-button” property

  “show-close-button”        gboolean

Whether to show the close button in the toolbar.

Flags: Read / Write / Construct

Default value: FALSE