1/* Entry/Type to Search
2 *
3 * GtkSearchEntry provides an entry that is ready for search.
4 *
5 * Search entries have their "search-changed" signal delayed and
6 * should be used when the search operation is slow, such as big
7 * datasets to search, or online searches.
8 *
9 * GtkSearchBar allows have a hidden search entry that 'springs
10 * into action' upon keyboard input.
11 */
12
13#include <gtk/gtk.h>
14
15static void
16search_changed_cb (GtkSearchEntry *entry,
17 GtkLabel *result_label)
18{
19 const char *text;
20 text = gtk_editable_get_text (GTK_EDITABLE (entry));
21 gtk_label_set_text (self: result_label, str: text ? text : "");
22}
23
24GtkWidget *
25do_search_entry2 (GtkWidget *do_widget)
26{
27 static GtkWidget *window = NULL;
28 GtkWidget *vbox;
29 GtkWidget *hbox;
30 GtkWidget *box;
31 GtkWidget *label;
32 GtkWidget *entry;
33 GtkWidget *searchbar;
34 GtkWidget *button;
35 GtkWidget *header;
36
37 if (!window)
38 {
39 window = gtk_window_new ();
40 gtk_window_set_title (GTK_WINDOW (window), title: "Type to Search");
41 gtk_window_set_transient_for (GTK_WINDOW (window), GTK_WINDOW (do_widget));
42 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
43 gtk_widget_set_size_request (widget: window, width: 200, height: -1);
44 g_object_add_weak_pointer (G_OBJECT (window), weak_pointer_location: (gpointer *)&window);
45
46 header = gtk_header_bar_new ();
47 gtk_window_set_titlebar (GTK_WINDOW (window), titlebar: header);
48
49 vbox = gtk_box_new (orientation: GTK_ORIENTATION_VERTICAL, spacing: 0);
50 gtk_window_set_child (GTK_WINDOW (window), child: vbox);
51
52 entry = gtk_search_entry_new ();
53 gtk_widget_set_halign (widget: entry, align: GTK_ALIGN_CENTER);
54 searchbar = gtk_search_bar_new ();
55 gtk_search_bar_connect_entry (GTK_SEARCH_BAR (searchbar), GTK_EDITABLE (entry));
56 gtk_search_bar_set_show_close_button (GTK_SEARCH_BAR (searchbar), FALSE);
57 gtk_search_bar_set_child (GTK_SEARCH_BAR (searchbar), child: entry);
58 gtk_box_append (GTK_BOX (vbox), child: searchbar);
59
60 /* Hook the search bar to key presses */
61 gtk_search_bar_set_key_capture_widget (GTK_SEARCH_BAR (searchbar), widget: window);
62
63 box = gtk_box_new (orientation: GTK_ORIENTATION_VERTICAL, spacing: 18);
64 gtk_widget_set_margin_start (widget: box, margin: 18);
65 gtk_widget_set_margin_end (widget: box, margin: 18);
66 gtk_widget_set_margin_top (widget: box, margin: 18);
67 gtk_widget_set_margin_bottom (widget: box, margin: 18);
68 gtk_box_append (GTK_BOX (vbox), child: box);
69
70 /* Toggle button */
71 button = gtk_toggle_button_new ();
72 gtk_button_set_icon_name (GTK_BUTTON (button), icon_name: "system-search-symbolic");
73 g_object_bind_property (source: button, source_property: "active",
74 target: searchbar, target_property: "search-mode-enabled",
75 flags: G_BINDING_BIDIRECTIONAL);
76 gtk_header_bar_pack_end (GTK_HEADER_BAR (header), child: button);
77
78 /* Result */
79 hbox = gtk_box_new (orientation: GTK_ORIENTATION_HORIZONTAL, spacing: 10);
80 gtk_box_append (GTK_BOX (box), child: hbox);
81
82 label = gtk_label_new (str: "Searching for:");
83 gtk_label_set_xalign (GTK_LABEL (label), xalign: 0.0);
84 gtk_box_append (GTK_BOX (hbox), child: label);
85
86 label = gtk_label_new (str: "");
87 gtk_box_append (GTK_BOX (hbox), child: label);
88
89 g_signal_connect (entry, "search-changed",
90 G_CALLBACK (search_changed_cb), label);
91 }
92
93 if (!gtk_widget_get_visible (widget: window))
94 gtk_widget_show (widget: window);
95 else
96 gtk_window_destroy (GTK_WINDOW (window));
97
98 return window;
99}
100

source code of gtk/demos/gtk-demo/search_entry2.c