2010-03-09 00:59:24 +00:00
|
|
|
/* Entry/Search Entry
|
2008-12-26 21:57:55 +00:00
|
|
|
*
|
2010-03-09 00:59:24 +00:00
|
|
|
* GtkEntry allows to display icons and progress information.
|
2008-12-26 21:57:55 +00:00
|
|
|
* This demo shows how to use these features in a search entry.
|
|
|
|
*/
|
|
|
|
|
2013-06-21 17:46:54 +00:00
|
|
|
#include <glib/gi18n.h>
|
2008-12-26 21:57:55 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
static GtkWidget *window = NULL;
|
|
|
|
static GtkWidget *notebook = NULL;
|
2019-01-27 05:57:42 +00:00
|
|
|
static GSimpleActionGroup *actions = NULL;
|
2008-12-26 21:57:55 +00:00
|
|
|
|
|
|
|
static guint search_progress_id = 0;
|
|
|
|
static guint finish_search_id = 0;
|
|
|
|
|
|
|
|
static void
|
|
|
|
show_find_button (void)
|
|
|
|
{
|
|
|
|
gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
show_cancel_button (void)
|
|
|
|
{
|
|
|
|
gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
search_progress (gpointer data)
|
|
|
|
{
|
|
|
|
gtk_entry_progress_pulse (GTK_ENTRY (data));
|
2012-01-31 00:12:27 +00:00
|
|
|
return G_SOURCE_CONTINUE;
|
2008-12-26 21:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
search_progress_done (GtkEntry *entry)
|
|
|
|
{
|
|
|
|
gtk_entry_set_progress_fraction (entry, 0.0);
|
2020-08-03 01:53:59 +00:00
|
|
|
g_object_unref (entry);
|
2008-12-26 21:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
finish_search (GtkButton *button)
|
|
|
|
{
|
|
|
|
show_find_button ();
|
2016-06-20 02:47:57 +00:00
|
|
|
if (search_progress_id)
|
|
|
|
{
|
|
|
|
g_source_remove (search_progress_id);
|
|
|
|
search_progress_id = 0;
|
|
|
|
}
|
2015-07-22 01:12:36 +00:00
|
|
|
return G_SOURCE_REMOVE;
|
2008-12-26 21:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
start_search_feedback (gpointer data)
|
|
|
|
{
|
2020-08-03 01:46:44 +00:00
|
|
|
gtk_entry_set_progress_fraction (GTK_ENTRY (data), 0.1);
|
2010-03-09 00:59:24 +00:00
|
|
|
search_progress_id = g_timeout_add_full (G_PRIORITY_DEFAULT, 100,
|
2020-08-03 01:53:59 +00:00
|
|
|
(GSourceFunc)search_progress, g_object_ref (data),
|
2008-12-26 21:57:55 +00:00
|
|
|
(GDestroyNotify)search_progress_done);
|
2020-08-03 01:53:59 +00:00
|
|
|
|
2015-07-22 01:12:36 +00:00
|
|
|
return G_SOURCE_REMOVE;
|
2008-12-26 21:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-03-09 00:59:24 +00:00
|
|
|
start_search (GtkButton *button,
|
2008-12-26 21:57:55 +00:00
|
|
|
GtkEntry *entry)
|
|
|
|
{
|
|
|
|
show_cancel_button ();
|
|
|
|
search_progress_id = g_timeout_add_seconds (1, (GSourceFunc)start_search_feedback, entry);
|
|
|
|
finish_search_id = g_timeout_add_seconds (15, (GSourceFunc)finish_search, button);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
stop_search (GtkButton *button,
|
|
|
|
gpointer data)
|
|
|
|
{
|
2016-06-20 02:47:57 +00:00
|
|
|
if (finish_search_id)
|
|
|
|
{
|
|
|
|
g_source_remove (finish_search_id);
|
|
|
|
finish_search_id = 0;
|
|
|
|
}
|
2008-12-26 21:57:55 +00:00
|
|
|
finish_search (button);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-01-27 05:57:42 +00:00
|
|
|
clear_entry (GSimpleAction *action,
|
|
|
|
GVariant *parameter,
|
|
|
|
gpointer user_data)
|
2008-12-26 21:57:55 +00:00
|
|
|
{
|
2019-01-27 05:57:42 +00:00
|
|
|
GtkEditable *editable = user_data;
|
2008-12-26 21:57:55 +00:00
|
|
|
|
2019-01-27 05:57:42 +00:00
|
|
|
gtk_editable_set_text (editable, "");
|
2010-03-09 00:59:24 +00:00
|
|
|
}
|
|
|
|
|
2008-12-26 21:57:55 +00:00
|
|
|
static void
|
2019-01-27 05:57:42 +00:00
|
|
|
set_search_by (GSimpleAction *action,
|
|
|
|
GVariant *value,
|
|
|
|
gpointer user_data)
|
2008-12-26 21:57:55 +00:00
|
|
|
{
|
2019-01-27 05:57:42 +00:00
|
|
|
GtkEntry *entry = user_data;
|
|
|
|
const char *term;
|
2013-06-21 17:46:54 +00:00
|
|
|
|
2019-01-27 05:57:42 +00:00
|
|
|
term = g_variant_get_string (value, NULL);
|
2010-03-09 00:59:24 +00:00
|
|
|
|
2019-01-27 05:57:42 +00:00
|
|
|
g_simple_action_set_state (action, value);
|
2010-03-09 00:59:24 +00:00
|
|
|
|
2019-01-27 05:57:42 +00:00
|
|
|
if (g_str_equal (term, "name"))
|
|
|
|
{
|
|
|
|
gtk_entry_set_icon_tooltip_text (entry, GTK_ENTRY_ICON_PRIMARY, "Search by name");
|
2020-11-02 17:01:11 +00:00
|
|
|
gtk_entry_set_placeholder_text (entry, "Name…");
|
2019-01-27 05:57:42 +00:00
|
|
|
}
|
|
|
|
else if (g_str_equal (term, "description"))
|
|
|
|
{
|
|
|
|
gtk_entry_set_icon_tooltip_text (entry, GTK_ENTRY_ICON_PRIMARY, "Search by description");
|
2020-11-02 17:01:11 +00:00
|
|
|
gtk_entry_set_placeholder_text (entry, "Description…");
|
2019-01-27 05:57:42 +00:00
|
|
|
}
|
|
|
|
else if (g_str_equal (term, "filename"))
|
|
|
|
{
|
|
|
|
gtk_entry_set_icon_tooltip_text (entry, GTK_ENTRY_ICON_PRIMARY, "Search by file name");
|
2020-11-02 17:01:11 +00:00
|
|
|
gtk_entry_set_placeholder_text (entry, "File name…");
|
2019-01-27 05:57:42 +00:00
|
|
|
}
|
2010-03-09 00:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
icon_press_cb (GtkEntry *entry,
|
2020-07-24 13:54:49 +00:00
|
|
|
int position,
|
2010-03-09 00:59:24 +00:00
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
if (position == GTK_ENTRY_ICON_PRIMARY)
|
2019-01-27 05:57:42 +00:00
|
|
|
{
|
|
|
|
GAction *action;
|
|
|
|
GVariant *state;
|
|
|
|
GVariant *new_state;
|
|
|
|
const char *s;
|
|
|
|
|
|
|
|
action = g_action_map_lookup_action (G_ACTION_MAP (actions), "search-by");
|
|
|
|
state = g_action_get_state (action);
|
|
|
|
s = g_variant_get_string (state, NULL);
|
|
|
|
|
|
|
|
if (g_str_equal (s, "name"))
|
|
|
|
new_state = g_variant_new_string ("description");
|
|
|
|
else if (g_str_equal (s, "description"))
|
|
|
|
new_state = g_variant_new_string ("filename");
|
|
|
|
else if (g_str_equal (s, "filename"))
|
|
|
|
new_state = g_variant_new_string ("name");
|
|
|
|
else
|
|
|
|
g_assert_not_reached ();
|
|
|
|
|
|
|
|
g_action_change_state (action, new_state);
|
|
|
|
g_variant_unref (state);
|
|
|
|
}
|
2010-03-09 00:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
activate_cb (GtkEntry *entry,
|
|
|
|
GtkButton *button)
|
|
|
|
{
|
|
|
|
if (search_progress_id != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
start_search (button, entry);
|
|
|
|
}
|
2009-01-18 15:50:29 +00:00
|
|
|
|
|
|
|
static void
|
2020-08-03 01:53:59 +00:00
|
|
|
search_entry_destroyed (gpointer data,
|
|
|
|
GObject *widget)
|
2009-01-18 15:50:29 +00:00
|
|
|
{
|
|
|
|
if (finish_search_id != 0)
|
2015-07-22 01:12:36 +00:00
|
|
|
{
|
|
|
|
g_source_remove (finish_search_id);
|
|
|
|
finish_search_id = 0;
|
|
|
|
}
|
2009-01-18 15:50:29 +00:00
|
|
|
|
|
|
|
if (search_progress_id != 0)
|
2015-07-22 01:12:36 +00:00
|
|
|
{
|
|
|
|
g_source_remove (search_progress_id);
|
|
|
|
search_progress_id = 0;
|
|
|
|
}
|
2009-01-18 15:50:29 +00:00
|
|
|
|
|
|
|
window = NULL;
|
|
|
|
}
|
|
|
|
|
2010-03-09 00:59:24 +00:00
|
|
|
static void
|
2019-01-27 05:57:42 +00:00
|
|
|
text_changed (GObject *object,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
gpointer data)
|
2010-03-09 00:59:24 +00:00
|
|
|
{
|
2019-01-27 05:57:42 +00:00
|
|
|
GtkEntry *entry = GTK_ENTRY (object);
|
2020-03-07 14:01:21 +00:00
|
|
|
GActionMap *action_map = data;
|
2019-01-27 05:57:42 +00:00
|
|
|
GAction *action;
|
2010-03-09 00:59:24 +00:00
|
|
|
gboolean has_text;
|
|
|
|
|
|
|
|
has_text = gtk_entry_get_text_length (entry) > 0;
|
|
|
|
|
2020-03-07 14:01:21 +00:00
|
|
|
action = g_action_map_lookup_action (action_map, "clear");
|
2019-01-27 05:57:42 +00:00
|
|
|
g_simple_action_set_enabled (G_SIMPLE_ACTION (action), has_text);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GMenuModel *
|
|
|
|
create_search_menu_model (void)
|
|
|
|
{
|
|
|
|
GMenu *menu = g_menu_new ();
|
|
|
|
g_menu_append (menu, _("Name"), "search.search-by::name");
|
|
|
|
g_menu_append (menu, _("Description"), "search.search-by::description");
|
|
|
|
g_menu_append (menu, _("File Name"), "search.search-by::filename");
|
|
|
|
|
|
|
|
return G_MENU_MODEL (menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
entry_add_to_context_menu (GtkEntry *entry)
|
|
|
|
{
|
|
|
|
GMenu *menu;
|
|
|
|
GActionEntry entries[] = {
|
|
|
|
{ "clear", clear_entry, NULL, NULL, NULL },
|
|
|
|
{ "search-by", NULL, "s", "'name'", set_search_by }
|
|
|
|
};
|
|
|
|
GMenuModel *submenu;
|
|
|
|
GMenuItem *item;
|
2020-03-27 20:24:20 +00:00
|
|
|
GAction *action;
|
|
|
|
GVariant *value;
|
2019-01-27 05:57:42 +00:00
|
|
|
|
|
|
|
actions = g_simple_action_group_new ();
|
|
|
|
g_action_map_add_action_entries (G_ACTION_MAP (actions), entries, G_N_ELEMENTS(entries), entry);
|
|
|
|
gtk_widget_insert_action_group (GTK_WIDGET (entry), "search", G_ACTION_GROUP (actions));
|
|
|
|
|
2020-03-27 20:24:20 +00:00
|
|
|
action = g_action_map_lookup_action (G_ACTION_MAP (actions), "search-by");
|
|
|
|
value = g_variant_ref_sink (g_variant_new_string ("name"));
|
|
|
|
set_search_by (G_SIMPLE_ACTION (action), value, entry);
|
|
|
|
g_variant_unref (value);
|
|
|
|
|
2019-01-27 05:57:42 +00:00
|
|
|
menu = g_menu_new ();
|
|
|
|
item = g_menu_item_new (_("C_lear"), "search.clear");
|
|
|
|
g_menu_item_set_attribute (item, "touch-icon", "s", "edit-clear-symbolic");
|
|
|
|
g_menu_append_item (G_MENU (menu), item);
|
|
|
|
g_object_unref (item);
|
|
|
|
|
|
|
|
submenu = create_search_menu_model ();
|
|
|
|
g_menu_append_submenu (menu, _("Search By"), submenu);
|
|
|
|
g_object_unref (submenu);
|
|
|
|
|
|
|
|
gtk_entry_set_extra_menu (entry, G_MENU_MODEL (menu));
|
|
|
|
|
|
|
|
g_object_unref (menu);
|
|
|
|
|
|
|
|
g_signal_connect (entry, "notify::text", G_CALLBACK (text_changed), actions);
|
2010-03-09 00:59:24 +00:00
|
|
|
}
|
|
|
|
|
2008-12-26 21:57:55 +00:00
|
|
|
GtkWidget *
|
|
|
|
do_search_entry (GtkWidget *do_widget)
|
|
|
|
{
|
|
|
|
GtkWidget *vbox;
|
|
|
|
GtkWidget *hbox;
|
|
|
|
GtkWidget *entry;
|
|
|
|
GtkWidget *find_button;
|
|
|
|
GtkWidget *cancel_button;
|
2010-03-09 00:59:24 +00:00
|
|
|
|
2008-12-26 21:57:55 +00:00
|
|
|
if (!window)
|
|
|
|
{
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2017-10-31 06:41:15 +00:00
|
|
|
gtk_window_set_display (GTK_WINDOW (window), gtk_widget_get_display (do_widget));
|
2015-06-28 13:23:28 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Search Entry");
|
2008-12-26 21:57:55 +00:00
|
|
|
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
|
2020-08-03 01:53:59 +00:00
|
|
|
|
|
|
|
g_object_weak_ref (G_OBJECT (window), search_entry_destroyed, &window);
|
2008-12-26 21:57:55 +00:00
|
|
|
|
2020-11-02 17:01:11 +00:00
|
|
|
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
|
|
|
|
gtk_widget_set_margin_start (vbox, 18);
|
|
|
|
gtk_widget_set_margin_end (vbox, 18);
|
|
|
|
gtk_widget_set_margin_top (vbox, 18);
|
|
|
|
gtk_widget_set_margin_bottom (vbox, 18);
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (window), vbox);
|
2008-12-26 21:57:55 +00:00
|
|
|
|
2020-11-02 17:01:11 +00:00
|
|
|
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), hbox);
|
2008-12-26 21:57:55 +00:00
|
|
|
|
|
|
|
/* Create our entry */
|
2019-03-03 17:42:20 +00:00
|
|
|
entry = gtk_entry_new ();
|
|
|
|
gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
|
|
|
|
GTK_ENTRY_ICON_PRIMARY,
|
|
|
|
"edit-find-symbolic");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (hbox), entry);
|
2008-12-26 21:57:55 +00:00
|
|
|
|
|
|
|
/* Create the find and cancel buttons */
|
|
|
|
notebook = gtk_notebook_new ();
|
|
|
|
gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook), FALSE);
|
|
|
|
gtk_notebook_set_show_border (GTK_NOTEBOOK (notebook), FALSE);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (hbox), notebook);
|
2008-12-26 21:57:55 +00:00
|
|
|
|
|
|
|
find_button = gtk_button_new_with_label ("Find");
|
2010-03-09 00:59:24 +00:00
|
|
|
g_signal_connect (find_button, "clicked",
|
2008-12-26 21:57:55 +00:00
|
|
|
G_CALLBACK (start_search), entry);
|
|
|
|
gtk_notebook_append_page (GTK_NOTEBOOK (notebook), find_button, NULL);
|
|
|
|
gtk_widget_show (find_button);
|
|
|
|
|
|
|
|
cancel_button = gtk_button_new_with_label ("Cancel");
|
2010-03-09 00:59:24 +00:00
|
|
|
g_signal_connect (cancel_button, "clicked",
|
2008-12-26 21:57:55 +00:00
|
|
|
G_CALLBACK (stop_search), NULL);
|
|
|
|
gtk_notebook_append_page (GTK_NOTEBOOK (notebook), cancel_button, NULL);
|
|
|
|
gtk_widget_show (cancel_button);
|
|
|
|
|
|
|
|
/* Set up the search icon */
|
2019-01-27 05:57:42 +00:00
|
|
|
gtk_entry_set_icon_activatable (GTK_ENTRY (entry), GTK_ENTRY_ICON_PRIMARY, TRUE);
|
|
|
|
gtk_entry_set_icon_sensitive (GTK_ENTRY (entry), GTK_ENTRY_ICON_PRIMARY, TRUE);
|
2008-12-26 21:57:55 +00:00
|
|
|
|
2019-01-27 05:57:42 +00:00
|
|
|
g_signal_connect (entry, "icon-press", G_CALLBACK (icon_press_cb), NULL);
|
|
|
|
g_signal_connect (entry, "activate", G_CALLBACK (activate_cb), NULL);
|
2008-12-26 21:57:55 +00:00
|
|
|
|
2010-03-09 16:14:50 +00:00
|
|
|
/* add accessible alternatives for icon functionality */
|
2019-01-27 05:57:42 +00:00
|
|
|
entry_add_to_context_menu (GTK_ENTRY (entry));
|
2010-03-09 16:14:50 +00:00
|
|
|
}
|
2010-03-09 00:59:24 +00:00
|
|
|
|
2010-03-01 06:47:38 +00:00
|
|
|
if (!gtk_widget_get_visible (window))
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2008-12-26 21:57:55 +00:00
|
|
|
else
|
|
|
|
{
|
2019-01-27 05:57:42 +00:00
|
|
|
g_clear_object (&actions);
|
2020-05-09 14:26:22 +00:00
|
|
|
gtk_window_destroy (GTK_WINDOW (window));
|
2008-12-26 21:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|