2013-05-16 16:20:19 +00:00
|
|
|
/* Entry/Delayed Search Entry
|
|
|
|
*
|
|
|
|
* GtkSearchEntry sets up GtkEntries ready for search. Search entries
|
|
|
|
* have their "changed" signal delayed and should be used
|
|
|
|
* when the searched operation is slow such as loads of entries
|
|
|
|
* to search, or online searches.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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
|
|
|
g_message ("search changed: %s", text);
|
|
|
|
gtk_label_set_text (result_label, text ? text : "");
|
|
|
|
}
|
|
|
|
|
2013-07-29 01:51:45 +00:00
|
|
|
static void
|
|
|
|
changed_cb (GtkEditable *editable)
|
|
|
|
{
|
|
|
|
const char *text;
|
2019-02-17 04:59:39 +00:00
|
|
|
text = gtk_editable_get_text (GTK_EDITABLE (editable));
|
2013-07-29 01:51:45 +00:00
|
|
|
g_message ("changed: %s", text);
|
|
|
|
}
|
|
|
|
|
2014-12-27 23:53:21 +00:00
|
|
|
static void
|
|
|
|
search_changed (GtkSearchEntry *entry,
|
|
|
|
GtkLabel *label)
|
|
|
|
{
|
|
|
|
gtk_label_set_text (label, "search-changed");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
next_match (GtkSearchEntry *entry,
|
|
|
|
GtkLabel *label)
|
|
|
|
{
|
|
|
|
gtk_label_set_text (label, "next-match");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
previous_match (GtkSearchEntry *entry,
|
|
|
|
GtkLabel *label)
|
|
|
|
{
|
|
|
|
gtk_label_set_text (label, "previous-match");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
stop_search (GtkSearchEntry *entry,
|
|
|
|
GtkLabel *label)
|
|
|
|
{
|
|
|
|
gtk_label_set_text (label, "stop-search");
|
|
|
|
}
|
|
|
|
|
2013-05-16 16:20:19 +00:00
|
|
|
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;
|
|
|
|
GtkWidget *label;
|
|
|
|
GtkWidget *entry;
|
2013-05-21 15:26:37 +00:00
|
|
|
GtkWidget *container;
|
|
|
|
GtkWidget *searchbar;
|
2013-05-16 16:20:19 +00:00
|
|
|
GtkWidget *button;
|
|
|
|
|
|
|
|
if (!window)
|
|
|
|
{
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2015-06-20 01:59:15 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Delayed Search Entry");
|
2013-05-21 15:26:37 +00:00
|
|
|
gtk_window_set_transient_for (GTK_WINDOW (window), GTK_WINDOW (do_widget));
|
|
|
|
gtk_window_set_resizable (GTK_WINDOW (window), TRUE);
|
|
|
|
gtk_widget_set_size_request (window, 200, -1);
|
|
|
|
|
2013-05-16 16:20:19 +00:00
|
|
|
g_signal_connect (window, "destroy",
|
2015-06-28 13:23:28 +00:00
|
|
|
G_CALLBACK (gtk_widget_destroyed), &window);
|
2013-05-16 16:20:19 +00:00
|
|
|
|
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 ();
|
2013-05-21 15:26:37 +00:00
|
|
|
container = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
|
|
|
|
gtk_widget_set_halign (container, GTK_ALIGN_CENTER);
|
2018-12-29 08:49:24 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (container), entry);
|
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);
|
|
|
|
gtk_container_add (GTK_CONTAINER (searchbar), container);
|
2018-12-29 08:49:24 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (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
|
|
|
|
|
|
|
/* Help */
|
|
|
|
label = gtk_label_new ("Start Typing to search");
|
2018-12-29 08:49:24 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (vbox), label);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
|
|
|
/* Toggle button */
|
|
|
|
button = gtk_toggle_button_new_with_label ("Search");
|
|
|
|
g_object_bind_property (button, "active",
|
|
|
|
searchbar, "search-mode-enabled",
|
|
|
|
G_BINDING_BIDIRECTIONAL);
|
2018-12-29 08:49:24 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (vbox), button);
|
2013-05-16 16:20:19 +00:00
|
|
|
|
|
|
|
/* Result */
|
|
|
|
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
|
2018-12-29 08:49:24 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (vbox), hbox);
|
2013-05-16 16:20:19 +00:00
|
|
|
|
|
|
|
label = gtk_label_new ("Result:");
|
2014-12-27 23:53:21 +00:00
|
|
|
gtk_label_set_xalign (GTK_LABEL (label), 0.0);
|
|
|
|
gtk_widget_set_margin_start (label, 6);
|
2018-12-29 08:49:24 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (hbox), label);
|
2013-05-16 16:20:19 +00:00
|
|
|
|
|
|
|
label = gtk_label_new ("");
|
2018-12-29 08:49:24 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (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);
|
2013-07-29 01:51:45 +00:00
|
|
|
g_signal_connect (entry, "changed",
|
|
|
|
G_CALLBACK (changed_cb), label);
|
2014-12-27 23:53:21 +00:00
|
|
|
|
|
|
|
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
|
2018-12-29 08:49:24 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (vbox), hbox);
|
2014-12-27 23:53:21 +00:00
|
|
|
|
|
|
|
label = gtk_label_new ("Signal:");
|
|
|
|
gtk_label_set_xalign (GTK_LABEL (label), 0.0);
|
|
|
|
gtk_widget_set_margin_start (label, 6);
|
2018-12-29 08:49:24 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (hbox), label);
|
2014-12-27 23:53:21 +00:00
|
|
|
|
|
|
|
label = gtk_label_new ("");
|
2018-12-29 08:49:24 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (hbox), label);
|
2014-12-27 23:53:21 +00:00
|
|
|
|
|
|
|
g_signal_connect (entry, "search-changed",
|
|
|
|
G_CALLBACK (search_changed), label);
|
|
|
|
g_signal_connect (entry, "next-match",
|
|
|
|
G_CALLBACK (next_match), label);
|
|
|
|
g_signal_connect (entry, "previous-match",
|
|
|
|
G_CALLBACK (previous_match), label);
|
|
|
|
g_signal_connect (entry, "stop-search",
|
|
|
|
G_CALLBACK (stop_search), label);
|
2013-05-16 16:20:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!gtk_widget_get_visible (window))
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2013-05-16 16:20:19 +00:00
|
|
|
else
|
2015-06-28 13:23:28 +00:00
|
|
|
gtk_widget_destroy (window);
|
2013-05-16 16:20:19 +00:00
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|