| 1 |
/* Entry/Search Entry |
| 2 |
* |
| 3 |
* GtkEntry allows to display icons and progress information. |
| 4 |
* This demo shows how to use these features in a search entry. |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <glib/gi18n.h> |
| 8 |
#include <gtk/gtk.h> |
| 9 |
|
| 10 |
static GtkWidget *window = NULL; |
| 11 |
static GtkWidget *menu = NULL; |
| 12 |
static GtkWidget *notebook = NULL; |
| 13 |
|
| 14 |
static guint search_progress_id = 0; |
| 15 |
static guint finish_search_id = 0; |
| 16 |
|
| 17 |
static void |
| 18 |
show_find_button (void) |
| 19 |
{ |
| 20 |
gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), 0); |
| 21 |
} |
| 22 |
|
| 23 |
static void |
| 24 |
show_cancel_button (void) |
| 25 |
{ |
| 26 |
gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), 1); |
| 27 |
} |
| 28 |
|
| 29 |
static gboolean |
| 30 |
search_progress (gpointer data) |
| 31 |
{ |
| 32 |
gtk_entry_progress_pulse (GTK_ENTRY (data)); |
| 33 |
return G_SOURCE_CONTINUE; |
| 34 |
} |
| 35 |
|
| 36 |
static void |
| 37 |
search_progress_done (GtkEntry *entry) |
| 38 |
{ |
| 39 |
gtk_entry_set_progress_fraction (entry, 0.0); |
| 40 |
} |
| 41 |
|
| 42 |
static gboolean |
| 43 |
finish_search (GtkButton *button) |
| 44 |
{ |
| 45 |
show_find_button (); |
| 46 |
if (search_progress_id) |
| 47 |
{ |
| 48 |
g_source_remove (search_progress_id); |
| 49 |
search_progress_id = 0; |
| 50 |
} |
| 51 |
return G_SOURCE_REMOVE; |
| 52 |
} |
| 53 |
|
| 54 |
static gboolean |
| 55 |
start_search_feedback (gpointer data) |
| 56 |
{ |
| 57 |
search_progress_id = g_timeout_add_full (G_PRIORITY_DEFAULT, 100, |
| 58 |
(GSourceFunc)search_progress, data, |
| 59 |
(GDestroyNotify)search_progress_done); |
| 60 |
return G_SOURCE_REMOVE; |
| 61 |
} |
| 62 |
|
| 63 |
static void |
| 64 |
start_search (GtkButton *button, |
| 65 |
GtkEntry *entry) |
| 66 |
{ |
| 67 |
show_cancel_button (); |
| 68 |
search_progress_id = g_timeout_add_seconds (1, (GSourceFunc)start_search_feedback, entry); |
| 69 |
finish_search_id = g_timeout_add_seconds (15, (GSourceFunc)finish_search, button); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
static void |
| 74 |
stop_search (GtkButton *button, |
| 75 |
gpointer data) |
| 76 |
{ |
| 77 |
if (finish_search_id) |
| 78 |
{ |
| 79 |
g_source_remove (finish_search_id); |
| 80 |
finish_search_id = 0; |
| 81 |
} |
| 82 |
finish_search (button); |
| 83 |
} |
| 84 |
|
| 85 |
static void |
| 86 |
clear_entry (GtkEntry *entry) |
| 87 |
{ |
| 88 |
gtk_entry_set_text (entry, ""); |
| 89 |
} |
| 90 |
|
| 91 |
static void |
| 92 |
search_by_name (GtkWidget *item, |
| 93 |
GtkEntry *entry) |
| 94 |
{ |
| 95 |
gtk_entry_set_icon_tooltip_text (entry, |
| 96 |
GTK_ENTRY_ICON_PRIMARY, |
| 97 |
"Search by name\n" |
| 98 |
"Click here to change the search type"); |
| 99 |
gtk_entry_set_placeholder_text (entry, "name"); |
| 100 |
} |
| 101 |
|
| 102 |
static void |
| 103 |
search_by_description (GtkWidget *item, |
| 104 |
GtkEntry *entry) |
| 105 |
{ |
| 106 |
|
| 107 |
gtk_entry_set_icon_tooltip_text (entry, |
| 108 |
GTK_ENTRY_ICON_PRIMARY, |
| 109 |
"Search by description\n" |
| 110 |
"Click here to change the search type"); |
| 111 |
gtk_entry_set_placeholder_text (entry, "description"); |
| 112 |
} |
| 113 |
|
| 114 |
static void |
| 115 |
search_by_file (GtkWidget *item, |
| 116 |
GtkEntry *entry) |
| 117 |
{ |
| 118 |
gtk_entry_set_icon_tooltip_text (entry, |
| 119 |
GTK_ENTRY_ICON_PRIMARY, |
| 120 |
"Search by file name\n" |
| 121 |
"Click here to change the search type"); |
| 122 |
gtk_entry_set_placeholder_text (entry, "file name"); |
| 123 |
} |
| 124 |
|
| 125 |
GtkWidget * |
| 126 |
create_search_menu (GtkWidget *entry) |
| 127 |
{ |
| 128 |
GtkWidget *menu; |
| 129 |
GtkWidget *item; |
| 130 |
|
| 131 |
menu = gtk_menu_new (); |
| 132 |
|
| 133 |
item = gtk_menu_item_new_with_mnemonic ("Search by _name"); |
| 134 |
g_signal_connect (item, "activate", |
| 135 |
G_CALLBACK (search_by_name), entry); |
| 136 |
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); |
| 137 |
|
| 138 |
item = gtk_menu_item_new_with_mnemonic ("Search by _description"); |
| 139 |
g_signal_connect (item, "activate", |
| 140 |
G_CALLBACK (search_by_description), entry); |
| 141 |
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); |
| 142 |
|
| 143 |
item = gtk_menu_item_new_with_mnemonic ("Search by _file name"); |
| 144 |
g_signal_connect (item, "activate", |
| 145 |
G_CALLBACK (search_by_file), entry); |
| 146 |
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); |
| 147 |
|
| 148 |
gtk_widget_show_all (menu); |
| 149 |
|
| 150 |
return menu; |
| 151 |
} |
| 152 |
|
| 153 |
static void |
| 154 |
icon_press_cb (GtkEntry *entry, |
| 155 |
gint position, |
| 156 |
GdkEventButton *event, |
| 157 |
gpointer data) |
| 158 |
{ |
| 159 |
if (position == GTK_ENTRY_ICON_PRIMARY) |
| 160 |
gtk_menu_popup_at_pointer (GTK_MENU (menu), (GdkEvent *) event); |
| 161 |
} |
| 162 |
|
| 163 |
static void |
| 164 |
activate_cb (GtkEntry *entry, |
| 165 |
GtkButton *button) |
| 166 |
{ |
| 167 |
if (search_progress_id != 0) |
| 168 |
return; |
| 169 |
|
| 170 |
start_search (button, entry); |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
static void |
| 175 |
search_entry_destroyed (GtkWidget *widget) |
| 176 |
{ |
| 177 |
if (finish_search_id != 0) |
| 178 |
{ |
| 179 |
g_source_remove (finish_search_id); |
| 180 |
finish_search_id = 0; |
| 181 |
} |
| 182 |
|
| 183 |
if (search_progress_id != 0) |
| 184 |
{ |
| 185 |
g_source_remove (search_progress_id); |
| 186 |
search_progress_id = 0; |
| 187 |
} |
| 188 |
|
| 189 |
window = NULL; |
| 190 |
} |
| 191 |
|
| 192 |
static void |
| 193 |
entry_populate_popup (GtkEntry *entry, |
| 194 |
GtkMenu *menu, |
| 195 |
gpointer user_data) |
| 196 |
{ |
| 197 |
GtkWidget *item; |
| 198 |
GtkWidget *search_menu; |
| 199 |
gboolean has_text; |
| 200 |
|
| 201 |
has_text = gtk_entry_get_text_length (entry) > 0; |
| 202 |
|
| 203 |
item = gtk_separator_menu_item_new (); |
| 204 |
gtk_widget_show (item); |
| 205 |
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); |
| 206 |
|
| 207 |
item = gtk_menu_item_new_with_mnemonic ("C_lear"); |
| 208 |
gtk_widget_show (item); |
| 209 |
g_signal_connect_swapped (item, "activate", |
| 210 |
G_CALLBACK (clear_entry), entry); |
| 211 |
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); |
| 212 |
gtk_widget_set_sensitive (item, has_text); |
| 213 |
|
| 214 |
search_menu = create_search_menu (GTK_WIDGET (entry)); |
| 215 |
item = gtk_menu_item_new_with_label ("Search by"); |
| 216 |
gtk_widget_show (item); |
| 217 |
gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), search_menu); |
| 218 |
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); |
| 219 |
} |
| 220 |
|
| 221 |
GtkWidget * |
| 222 |
do_search_entry (GtkWidget *do_widget) |
| 223 |
{ |
| 224 |
GtkWidget *vbox; |
| 225 |
GtkWidget *hbox; |
| 226 |
GtkWidget *label; |
| 227 |
GtkWidget *entry; |
| 228 |
GtkWidget *find_button; |
| 229 |
GtkWidget *cancel_button; |
| 230 |
|
| 231 |
if (!window) |
| 232 |
{ |
| 233 |
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); |
| 234 |
gtk_window_set_screen (GTK_WINDOW (window), gtk_widget_get_screen (do_widget)); |
| 235 |
gtk_window_set_title (GTK_WINDOW (window), "Search Entry"); |
| 236 |
gtk_window_set_resizable (GTK_WINDOW (window), FALSE); |
| 237 |
g_signal_connect (window, "destroy", |
| 238 |
G_CALLBACK (search_entry_destroyed), &window); |
| 239 |
|
| 240 |
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5); |
| 241 |
gtk_container_add (GTK_CONTAINER (window), vbox); |
| 242 |
gtk_container_set_border_width (GTK_CONTAINER (vbox), 5); |
| 243 |
|
| 244 |
label = gtk_label_new (NULL); |
| 245 |
gtk_label_set_markup (GTK_LABEL (label), "Search entry demo"); |
| 246 |
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0); |
| 247 |
|
| 248 |
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10); |
| 249 |
gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0); |
| 250 |
gtk_container_set_border_width (GTK_CONTAINER (hbox), 0); |
| 251 |
|
| 252 |
/* Create our entry */ |
| 253 |
entry = gtk_search_entry_new (); |
| 254 |
gtk_box_pack_start (GTK_BOX (hbox), entry, FALSE, FALSE, 0); |
| 255 |
|
| 256 |
/* Create the find and cancel buttons */ |
| 257 |
notebook = gtk_notebook_new (); |
| 258 |
gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook), FALSE); |
| 259 |
gtk_notebook_set_show_border (GTK_NOTEBOOK (notebook), FALSE); |
| 260 |
gtk_box_pack_start (GTK_BOX (hbox), notebook, FALSE, FALSE, 0); |
| 261 |
|
| 262 |
find_button = gtk_button_new_with_label ("Find"); |
| 263 |
g_signal_connect (find_button, "clicked", |
| 264 |
G_CALLBACK (start_search), entry); |
| 265 |
gtk_notebook_append_page (GTK_NOTEBOOK (notebook), find_button, NULL); |
| 266 |
gtk_widget_show (find_button); |
| 267 |
|
| 268 |
cancel_button = gtk_button_new_with_label ("Cancel"); |
| 269 |
g_signal_connect (cancel_button, "clicked", |
| 270 |
G_CALLBACK (stop_search), NULL); |
| 271 |
gtk_notebook_append_page (GTK_NOTEBOOK (notebook), cancel_button, NULL); |
| 272 |
gtk_widget_show (cancel_button); |
| 273 |
|
| 274 |
/* Set up the search icon */ |
| 275 |
search_by_name (NULL, GTK_ENTRY (entry)); |
| 276 |
|
| 277 |
/* Set up the clear icon */ |
| 278 |
g_signal_connect (entry, "icon-press", |
| 279 |
G_CALLBACK (icon_press_cb), NULL); |
| 280 |
g_signal_connect (entry, "activate", |
| 281 |
G_CALLBACK (activate_cb), NULL); |
| 282 |
|
| 283 |
/* Create the menu */ |
| 284 |
menu = create_search_menu (entry); |
| 285 |
gtk_menu_attach_to_widget (GTK_MENU (menu), entry, NULL); |
| 286 |
|
| 287 |
/* add accessible alternatives for icon functionality */ |
| 288 |
g_object_set (entry, "populate-all", TRUE, NULL); |
| 289 |
g_signal_connect (entry, "populate-popup", |
| 290 |
G_CALLBACK (entry_populate_popup), NULL); |
| 291 |
} |
| 292 |
|
| 293 |
if (!gtk_widget_get_visible (window)) |
| 294 |
gtk_widget_show_all (window); |
| 295 |
else |
| 296 |
{ |
| 297 |
gtk_widget_destroy (menu); |
| 298 |
gtk_widget_destroy (window); |
| 299 |
} |
| 300 |
|
| 301 |
return window; |
| 302 |
} |