2020-11-02 17:00:24 +00:00
|
|
|
/* Entry/Type to Search
|
2013-05-16 16:20:19 +00:00
|
|
|
*
|
2020-09-13 18:59:33 +00:00
|
|
|
* GtkSearchEntry provides an entry that is ready for search.
|
|
|
|
*
|
2020-11-02 17:00:24 +00:00
|
|
|
* Search entries have their "search-changed" signal delayed and
|
|
|
|
* should be used when the search operation is slow, such as big
|
|
|
|
* datasets to search, or online searches.
|
2020-09-13 18:59:33 +00:00
|
|
|
*
|
|
|
|
* GtkSearchBar allows have a hidden search entry that 'springs
|
|
|
|
* into action' upon keyboard input.
|
2013-05-16 16:20:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
static void
|
|
|
|
search_changed_cb (GtkSearchEntry *entry,
|
|
|
|
GtkLabel *result_label)
|
|
|
|
{
|
|
|
|
const char *text;
|
2019-02-17 04:59:39 +00:00
|
|
|
text = gtk_editable_get_text (GTK_EDITABLE (entry));
|
2013-05-16 16:20:19 +00:00
|
|
|
gtk_label_set_text (result_label, text ? text : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
do_search_entry2 (GtkWidget *do_widget)
|
|
|
|
{
|
2015-06-28 13:23:28 +00:00
|
|
|
static GtkWidget *window = NULL;
|
2013-05-16 16:20:19 +00:00
|
|
|
GtkWidget *vbox;
|
|
|
|
GtkWidget *hbox;
|
2020-11-02 17:00:24 +00:00
|
|
|
GtkWidget *box;
|
2013-05-16 16:20:19 +00:00
|
|
|
GtkWidget *label;
|
|
|
|
GtkWidget *entry;
|
2013-05-21 15:26:37 +00:00
|
|
|
GtkWidget *searchbar;
|
2013-05-16 16:20:19 +00:00
|
|
|
GtkWidget *button;
|
2020-11-02 17:00:24 +00:00
|
|
|
GtkWidget *header;
|
2013-05-16 16:20:19 +00:00
|
|
|
|
|
|
|
if (!window)
|
|
|
|
{
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2020-11-02 17:00:24 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Type to Search");
|
2013-05-21 15:26:37 +00:00
|
|
|
gtk_window_set_transient_for (GTK_WINDOW (window), GTK_WINDOW (do_widget));
|
2020-11-02 17:00:24 +00:00
|
|
|
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
|
2013-05-21 15:26:37 +00:00
|
|
|
gtk_widget_set_size_request (window, 200, -1);
|
2020-05-09 16:03:11 +00:00
|
|
|
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window);
|
2013-05-16 16:20:19 +00:00
|
|
|
|
2020-11-02 17:00:24 +00:00
|
|
|
header = gtk_header_bar_new ();
|
|
|
|
gtk_window_set_titlebar (GTK_WINDOW (window), header);
|
|
|
|
|
2013-05-21 15:26:37 +00:00
|
|
|
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (window), vbox);
|
2013-05-16 16:20:19 +00:00
|
|
|
|
|
|
|
entry = gtk_search_entry_new ();
|
2020-11-02 17:00:24 +00:00
|
|
|
gtk_widget_set_halign (entry, GTK_ALIGN_CENTER);
|
2013-05-21 15:26:37 +00:00
|
|
|
searchbar = gtk_search_bar_new ();
|
2019-02-17 04:59:39 +00:00
|
|
|
gtk_search_bar_connect_entry (GTK_SEARCH_BAR (searchbar), GTK_EDITABLE (entry));
|
2013-05-21 15:26:37 +00:00
|
|
|
gtk_search_bar_set_show_close_button (GTK_SEARCH_BAR (searchbar), FALSE);
|
2020-11-02 17:00:24 +00:00
|
|
|
gtk_search_bar_set_child (GTK_SEARCH_BAR (searchbar), entry);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), searchbar);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
|
|
|
/* Hook the search bar to key presses */
|
2018-06-26 20:35:03 +00:00
|
|
|
gtk_search_bar_set_key_capture_widget (GTK_SEARCH_BAR (searchbar), window);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
2020-11-02 17:00:24 +00:00
|
|
|
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 18);
|
|
|
|
gtk_widget_set_margin_start (box, 18);
|
|
|
|
gtk_widget_set_margin_end (box, 18);
|
|
|
|
gtk_widget_set_margin_top (box, 18);
|
|
|
|
gtk_widget_set_margin_bottom (box, 18);
|
|
|
|
gtk_box_append (GTK_BOX (vbox), box);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
|
|
|
/* Toggle button */
|
2020-11-02 17:00:24 +00:00
|
|
|
button = gtk_toggle_button_new ();
|
|
|
|
gtk_button_set_icon_name (GTK_BUTTON (button), "system-search-symbolic");
|
2013-05-21 15:26:37 +00:00
|
|
|
g_object_bind_property (button, "active",
|
|
|
|
searchbar, "search-mode-enabled",
|
|
|
|
G_BINDING_BIDIRECTIONAL);
|
2020-11-02 17:00:24 +00:00
|
|
|
gtk_header_bar_pack_end (GTK_HEADER_BAR (header), button);
|
2013-05-16 16:20:19 +00:00
|
|
|
|
|
|
|
/* Result */
|
|
|
|
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
|
2020-11-02 17:00:24 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), hbox);
|
2013-05-16 16:20:19 +00:00
|
|
|
|
2020-11-02 17:00:24 +00:00
|
|
|
label = gtk_label_new ("Searching for:");
|
2014-12-27 23:53:21 +00:00
|
|
|
gtk_label_set_xalign (GTK_LABEL (label), 0.0);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (hbox), label);
|
2013-05-16 16:20:19 +00:00
|
|
|
|
|
|
|
label = gtk_label_new ("");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (hbox), label);
|
2013-05-16 16:20:19 +00:00
|
|
|
|
2013-07-29 01:51:45 +00:00
|
|
|
g_signal_connect (entry, "search-changed",
|
2013-05-16 16:20:19 +00:00
|
|
|
G_CALLBACK (search_changed_cb), label);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gtk_widget_get_visible (window))
|
2022-11-28 04:03:14 +00:00
|
|
|
gtk_widget_set_visible (window, TRUE);
|
2013-05-16 16:20:19 +00:00
|
|
|
else
|
2020-05-09 14:26:22 +00:00
|
|
|
gtk_window_destroy (GTK_WINDOW (window));
|
2013-05-16 16:20:19 +00:00
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|