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 *menu = NULL;
|
|
|
|
static GtkWidget *notebook = NULL;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2010-03-09 00:59:24 +00:00
|
|
|
search_progress_id = g_timeout_add_full (G_PRIORITY_DEFAULT, 100,
|
2008-12-26 21:57:55 +00:00
|
|
|
(GSourceFunc)search_progress, data,
|
|
|
|
(GDestroyNotify)search_progress_done);
|
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
|
2010-03-09 00:59:24 +00:00
|
|
|
clear_entry (GtkEntry *entry)
|
2008-12-26 21:57:55 +00:00
|
|
|
{
|
2019-02-28 19:31:36 +00:00
|
|
|
gtk_editable_set_text (GTK_EDITABLE (entry), "");
|
2008-12-26 21:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-03-09 00:59:24 +00:00
|
|
|
search_by_name (GtkWidget *item,
|
2008-12-26 21:57:55 +00:00
|
|
|
GtkEntry *entry)
|
|
|
|
{
|
|
|
|
gtk_entry_set_icon_tooltip_text (entry,
|
|
|
|
GTK_ENTRY_ICON_PRIMARY,
|
|
|
|
"Search by name\n"
|
|
|
|
"Click here to change the search type");
|
2011-03-22 11:47:50 +00:00
|
|
|
gtk_entry_set_placeholder_text (entry, "name");
|
2010-03-09 00:59:24 +00:00
|
|
|
}
|
|
|
|
|
2008-12-26 21:57:55 +00:00
|
|
|
static void
|
2010-03-09 00:59:24 +00:00
|
|
|
search_by_description (GtkWidget *item,
|
2008-12-26 21:57:55 +00:00
|
|
|
GtkEntry *entry)
|
|
|
|
{
|
2013-06-21 17:46:54 +00:00
|
|
|
|
2008-12-26 21:57:55 +00:00
|
|
|
gtk_entry_set_icon_tooltip_text (entry,
|
|
|
|
GTK_ENTRY_ICON_PRIMARY,
|
|
|
|
"Search by description\n"
|
|
|
|
"Click here to change the search type");
|
2011-03-22 11:47:50 +00:00
|
|
|
gtk_entry_set_placeholder_text (entry, "description");
|
2010-03-09 00:59:24 +00:00
|
|
|
}
|
|
|
|
|
2008-12-26 21:57:55 +00:00
|
|
|
static void
|
2010-03-09 00:59:24 +00:00
|
|
|
search_by_file (GtkWidget *item,
|
2008-12-26 21:57:55 +00:00
|
|
|
GtkEntry *entry)
|
|
|
|
{
|
|
|
|
gtk_entry_set_icon_tooltip_text (entry,
|
|
|
|
GTK_ENTRY_ICON_PRIMARY,
|
|
|
|
"Search by file name\n"
|
|
|
|
"Click here to change the search type");
|
2011-03-22 11:47:50 +00:00
|
|
|
gtk_entry_set_placeholder_text (entry, "file name");
|
2010-03-09 00:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
create_search_menu (GtkWidget *entry)
|
|
|
|
{
|
|
|
|
GtkWidget *menu;
|
|
|
|
GtkWidget *item;
|
|
|
|
|
|
|
|
menu = gtk_menu_new ();
|
|
|
|
|
2013-06-21 17:46:54 +00:00
|
|
|
item = gtk_menu_item_new_with_mnemonic ("Search by _name");
|
2010-03-09 00:59:24 +00:00
|
|
|
g_signal_connect (item, "activate",
|
|
|
|
G_CALLBACK (search_by_name), entry);
|
|
|
|
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
|
|
|
|
|
2013-06-21 17:46:54 +00:00
|
|
|
item = gtk_menu_item_new_with_mnemonic ("Search by _description");
|
2010-03-09 00:59:24 +00:00
|
|
|
g_signal_connect (item, "activate",
|
|
|
|
G_CALLBACK (search_by_description), entry);
|
|
|
|
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
|
|
|
|
|
2013-06-21 17:46:54 +00:00
|
|
|
item = gtk_menu_item_new_with_mnemonic ("Search by _file name");
|
2010-03-09 00:59:24 +00:00
|
|
|
g_signal_connect (item, "activate",
|
|
|
|
G_CALLBACK (search_by_file), entry);
|
|
|
|
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
|
|
|
|
|
|
|
|
return menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
icon_press_cb (GtkEntry *entry,
|
|
|
|
gint position,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
if (position == GTK_ENTRY_ICON_PRIMARY)
|
2018-06-06 11:52:08 +00:00
|
|
|
gtk_menu_popup_at_pointer (GTK_MENU (menu), NULL);
|
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
|
2015-06-28 13:23:28 +00:00
|
|
|
search_entry_destroyed (GtkWidget *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
|
|
|
|
entry_populate_popup (GtkEntry *entry,
|
|
|
|
GtkMenu *menu,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GtkWidget *item;
|
|
|
|
GtkWidget *search_menu;
|
|
|
|
gboolean has_text;
|
|
|
|
|
|
|
|
has_text = gtk_entry_get_text_length (entry) > 0;
|
|
|
|
|
|
|
|
item = gtk_separator_menu_item_new ();
|
|
|
|
gtk_widget_show (item);
|
|
|
|
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
|
|
|
|
|
|
|
|
item = gtk_menu_item_new_with_mnemonic ("C_lear");
|
|
|
|
gtk_widget_show (item);
|
|
|
|
g_signal_connect_swapped (item, "activate",
|
|
|
|
G_CALLBACK (clear_entry), entry);
|
|
|
|
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
|
|
|
|
gtk_widget_set_sensitive (item, has_text);
|
|
|
|
|
|
|
|
search_menu = create_search_menu (GTK_WIDGET (entry));
|
|
|
|
item = gtk_menu_item_new_with_label ("Search by");
|
|
|
|
gtk_widget_show (item);
|
|
|
|
gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), search_menu);
|
|
|
|
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
|
|
|
|
}
|
|
|
|
|
2008-12-26 21:57:55 +00:00
|
|
|
GtkWidget *
|
|
|
|
do_search_entry (GtkWidget *do_widget)
|
|
|
|
{
|
|
|
|
GtkWidget *vbox;
|
|
|
|
GtkWidget *hbox;
|
|
|
|
GtkWidget *label;
|
|
|
|
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)
|
|
|
|
{
|
2015-06-28 13:23:28 +00:00
|
|
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
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);
|
|
|
|
g_signal_connect (window, "destroy",
|
2009-01-18 15:50:29 +00:00
|
|
|
G_CALLBACK (search_entry_destroyed), &window);
|
2008-12-26 21:57:55 +00:00
|
|
|
|
2010-10-31 17:07:20 +00:00
|
|
|
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
|
2016-10-26 19:11:18 +00:00
|
|
|
g_object_set (vbox, "margin", 5, NULL);
|
2015-06-28 13:23:28 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (window), vbox);
|
2008-12-26 21:57:55 +00:00
|
|
|
|
|
|
|
label = gtk_label_new (NULL);
|
|
|
|
gtk_label_set_markup (GTK_LABEL (label), "Search entry demo");
|
2018-12-29 08:49:24 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (vbox), label);
|
2008-12-26 21:57:55 +00:00
|
|
|
|
2010-10-31 17:07:20 +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);
|
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");
|
2018-12-29 08:49:24 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (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);
|
2018-12-29 08:49:24 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (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 */
|
|
|
|
search_by_name (NULL, GTK_ENTRY (entry));
|
2010-03-09 00:59:24 +00:00
|
|
|
|
2008-12-26 21:57:55 +00:00
|
|
|
/* Set up the clear icon */
|
2010-03-09 00:59:24 +00:00
|
|
|
g_signal_connect (entry, "icon-press",
|
2008-12-31 07:29:23 +00:00
|
|
|
G_CALLBACK (icon_press_cb), NULL);
|
2010-03-09 00:59:24 +00:00
|
|
|
g_signal_connect (entry, "activate",
|
2008-12-26 21:57:55 +00:00
|
|
|
G_CALLBACK (activate_cb), NULL);
|
|
|
|
|
|
|
|
/* Create the menu */
|
2010-03-09 00:59:24 +00:00
|
|
|
menu = create_search_menu (entry);
|
2008-12-26 21:57:55 +00:00
|
|
|
gtk_menu_attach_to_widget (GTK_MENU (menu), entry, NULL);
|
|
|
|
|
2010-03-09 16:14:50 +00:00
|
|
|
/* add accessible alternatives for icon functionality */
|
2015-06-28 13:23:28 +00:00
|
|
|
g_object_set (entry, "populate-all", TRUE, NULL);
|
2010-03-09 16:14:50 +00:00
|
|
|
g_signal_connect (entry, "populate-popup",
|
|
|
|
G_CALLBACK (entry_populate_popup), NULL);
|
|
|
|
}
|
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
|
|
|
|
{
|
|
|
|
gtk_widget_destroy (menu);
|
|
|
|
gtk_widget_destroy (window);
|
|
|
|
}
|
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|