3 * GtkEntry allows to display icons and progress information.
4 * This demo shows how to use these features in a search entry.
9 static GtkWidget *window = NULL;
10 static GtkWidget *menu = NULL;
11 static GtkWidget *notebook = NULL;
13 static guint search_progress_id = 0;
14 static guint finish_search_id = 0;
17 show_find_button (void)
19 gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), 0);
23 show_cancel_button (void)
25 gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), 1);
29 search_progress (gpointer data)
31 gtk_entry_progress_pulse (GTK_ENTRY (data));
37 search_progress_done (GtkEntry *entry)
39 gtk_entry_set_progress_fraction (entry, 0.0);
43 finish_search (GtkButton *button)
46 g_source_remove (search_progress_id);
47 search_progress_id = 0;
53 start_search_feedback (gpointer data)
55 search_progress_id = g_timeout_add_full (G_PRIORITY_DEFAULT, 100,
56 (GSourceFunc)search_progress, data,
57 (GDestroyNotify)search_progress_done);
62 start_search (GtkButton *button,
65 show_cancel_button ();
66 search_progress_id = g_timeout_add_seconds (1, (GSourceFunc)start_search_feedback, entry);
67 finish_search_id = g_timeout_add_seconds (15, (GSourceFunc)finish_search, button);
72 stop_search (GtkButton *button,
75 g_source_remove (finish_search_id);
76 finish_search (button);
80 clear_entry (GtkEntry *entry)
82 gtk_entry_set_text (entry, "");
86 search_by_name (GtkWidget *item,
89 gtk_entry_set_icon_from_stock (entry,
90 GTK_ENTRY_ICON_PRIMARY,
92 gtk_entry_set_icon_tooltip_text (entry,
93 GTK_ENTRY_ICON_PRIMARY,
95 "Click here to change the search type");
96 gtk_entry_set_placeholder_text (entry, "name");
100 search_by_description (GtkWidget *item,
103 gtk_entry_set_icon_from_stock (entry,
104 GTK_ENTRY_ICON_PRIMARY,
106 gtk_entry_set_icon_tooltip_text (entry,
107 GTK_ENTRY_ICON_PRIMARY,
108 "Search by description\n"
109 "Click here to change the search type");
110 gtk_entry_set_placeholder_text (entry, "description");
114 search_by_file (GtkWidget *item,
117 gtk_entry_set_icon_from_stock (entry,
118 GTK_ENTRY_ICON_PRIMARY,
120 gtk_entry_set_icon_tooltip_text (entry,
121 GTK_ENTRY_ICON_PRIMARY,
122 "Search by file name\n"
123 "Click here to change the search type");
124 gtk_entry_set_placeholder_text (entry, "file name");
128 create_search_menu (GtkWidget *entry)
134 menu = gtk_menu_new ();
136 item = gtk_image_menu_item_new_with_mnemonic ("Search by _name");
137 image = gtk_image_new_from_stock (GTK_STOCK_FIND, GTK_ICON_SIZE_MENU);
138 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
139 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
140 g_signal_connect (item, "activate",
141 G_CALLBACK (search_by_name), entry);
142 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
144 item = gtk_image_menu_item_new_with_mnemonic ("Search by _description");
145 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
146 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
147 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
148 g_signal_connect (item, "activate",
149 G_CALLBACK (search_by_description), entry);
150 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
152 item = gtk_image_menu_item_new_with_mnemonic ("Search by _file name");
153 image = gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_MENU);
154 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
155 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
156 g_signal_connect (item, "activate",
157 G_CALLBACK (search_by_file), entry);
158 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
160 gtk_widget_show_all (menu);
166 icon_press_cb (GtkEntry *entry,
168 GdkEventButton *event,
171 if (position == GTK_ENTRY_ICON_PRIMARY)
172 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
173 event->button, event->time);
179 text_changed_cb (GtkEntry *entry,
185 has_text = gtk_entry_get_text_length (entry) > 0;
186 gtk_entry_set_icon_sensitive (entry,
187 GTK_ENTRY_ICON_SECONDARY,
189 gtk_widget_set_sensitive (button, has_text);
193 activate_cb (GtkEntry *entry,
196 if (search_progress_id != 0)
199 start_search (button, entry);
204 search_entry_destroyed (GtkWidget *widget)
206 if (finish_search_id != 0)
207 g_source_remove (finish_search_id);
209 if (search_progress_id != 0)
210 g_source_remove (search_progress_id);
216 entry_populate_popup (GtkEntry *entry,
221 GtkWidget *search_menu;
224 has_text = gtk_entry_get_text_length (entry) > 0;
226 item = gtk_separator_menu_item_new ();
227 gtk_widget_show (item);
228 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
230 item = gtk_menu_item_new_with_mnemonic ("C_lear");
231 gtk_widget_show (item);
232 g_signal_connect_swapped (item, "activate",
233 G_CALLBACK (clear_entry), entry);
234 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
235 gtk_widget_set_sensitive (item, has_text);
237 search_menu = create_search_menu (GTK_WIDGET (entry));
238 item = gtk_menu_item_new_with_label ("Search by");
239 gtk_widget_show (item);
240 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), search_menu);
241 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
245 do_search_entry (GtkWidget *do_widget)
247 GtkWidget *content_area;
253 GtkWidget *find_button;
254 GtkWidget *cancel_button;
258 window = gtk_dialog_new_with_buttons ("Search Entry",
259 GTK_WINDOW (do_widget),
264 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
266 g_signal_connect (window, "response",
267 G_CALLBACK (gtk_widget_destroy), NULL);
268 g_signal_connect (window, "destroy",
269 G_CALLBACK (search_entry_destroyed), &window);
271 content_area = gtk_dialog_get_content_area (GTK_DIALOG (window));
273 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
274 gtk_box_pack_start (GTK_BOX (content_area), vbox, TRUE, TRUE, 0);
275 gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
277 label = gtk_label_new (NULL);
278 gtk_label_set_markup (GTK_LABEL (label), "Search entry demo");
279 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
281 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
282 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
283 gtk_container_set_border_width (GTK_CONTAINER (hbox), 0);
285 /* Create our entry */
286 entry = gtk_entry_new ();
287 gtk_box_pack_start (GTK_BOX (hbox), entry, FALSE, FALSE, 0);
289 /* Create the find and cancel buttons */
290 notebook = gtk_notebook_new ();
291 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook), FALSE);
292 gtk_notebook_set_show_border (GTK_NOTEBOOK (notebook), FALSE);
293 gtk_box_pack_start (GTK_BOX (hbox), notebook, FALSE, FALSE, 0);
295 find_button = gtk_button_new_with_label ("Find");
296 g_signal_connect (find_button, "clicked",
297 G_CALLBACK (start_search), entry);
298 gtk_notebook_append_page (GTK_NOTEBOOK (notebook), find_button, NULL);
299 gtk_widget_show (find_button);
301 cancel_button = gtk_button_new_with_label ("Cancel");
302 g_signal_connect (cancel_button, "clicked",
303 G_CALLBACK (stop_search), NULL);
304 gtk_notebook_append_page (GTK_NOTEBOOK (notebook), cancel_button, NULL);
305 gtk_widget_show (cancel_button);
307 /* Set up the search icon */
308 search_by_name (NULL, GTK_ENTRY (entry));
310 /* Set up the clear icon */
311 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
312 GTK_ENTRY_ICON_SECONDARY,
314 text_changed_cb (GTK_ENTRY (entry), NULL, find_button);
316 g_signal_connect (entry, "icon-press",
317 G_CALLBACK (icon_press_cb), NULL);
318 g_signal_connect (entry, "notify::text",
319 G_CALLBACK (text_changed_cb), find_button);
320 g_signal_connect (entry, "activate",
321 G_CALLBACK (activate_cb), NULL);
323 /* Create the menu */
324 menu = create_search_menu (entry);
325 gtk_menu_attach_to_widget (GTK_MENU (menu), entry, NULL);
327 /* add accessible alternatives for icon functionality */
328 g_signal_connect (entry, "populate-popup",
329 G_CALLBACK (entry_populate_popup), NULL);
331 /* Give the focus to the close button */
332 button = gtk_dialog_get_widget_for_response (GTK_DIALOG (window), GTK_RESPONSE_NONE);
333 gtk_widget_grab_focus (button);
336 if (!gtk_widget_get_visible (window))
337 gtk_widget_show_all (window);
340 gtk_widget_destroy (menu);
341 gtk_widget_destroy (window);