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));
32 return G_SOURCE_CONTINUE;
36 search_progress_done (GtkEntry *entry)
38 gtk_entry_set_progress_fraction (entry, 0.0);
42 finish_search (GtkButton *button)
45 g_source_remove (search_progress_id);
46 search_progress_id = 0;
52 start_search_feedback (gpointer data)
54 search_progress_id = g_timeout_add_full (G_PRIORITY_DEFAULT, 100,
55 (GSourceFunc)search_progress, data,
56 (GDestroyNotify)search_progress_done);
61 start_search (GtkButton *button,
64 show_cancel_button ();
65 search_progress_id = g_timeout_add_seconds (1, (GSourceFunc)start_search_feedback, entry);
66 finish_search_id = g_timeout_add_seconds (15, (GSourceFunc)finish_search, button);
71 stop_search (GtkButton *button,
74 g_source_remove (finish_search_id);
75 finish_search (button);
79 clear_entry (GtkEntry *entry)
81 gtk_entry_set_text (entry, "");
85 search_by_name (GtkWidget *item,
88 gtk_entry_set_icon_from_stock (entry,
89 GTK_ENTRY_ICON_PRIMARY,
91 gtk_entry_set_icon_tooltip_text (entry,
92 GTK_ENTRY_ICON_PRIMARY,
94 "Click here to change the search type");
95 gtk_entry_set_placeholder_text (entry, "name");
99 search_by_description (GtkWidget *item,
102 gtk_entry_set_icon_from_stock (entry,
103 GTK_ENTRY_ICON_PRIMARY,
105 gtk_entry_set_icon_tooltip_text (entry,
106 GTK_ENTRY_ICON_PRIMARY,
107 "Search by description\n"
108 "Click here to change the search type");
109 gtk_entry_set_placeholder_text (entry, "description");
113 search_by_file (GtkWidget *item,
116 gtk_entry_set_icon_from_stock (entry,
117 GTK_ENTRY_ICON_PRIMARY,
119 gtk_entry_set_icon_tooltip_text (entry,
120 GTK_ENTRY_ICON_PRIMARY,
121 "Search by file name\n"
122 "Click here to change the search type");
123 gtk_entry_set_placeholder_text (entry, "file name");
127 create_search_menu (GtkWidget *entry)
133 menu = gtk_menu_new ();
135 item = gtk_image_menu_item_new_with_mnemonic ("Search by _name");
136 image = gtk_image_new_from_stock (GTK_STOCK_FIND, GTK_ICON_SIZE_MENU);
137 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
138 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
139 g_signal_connect (item, "activate",
140 G_CALLBACK (search_by_name), entry);
141 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
143 item = gtk_image_menu_item_new_with_mnemonic ("Search by _description");
144 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
145 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
146 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
147 g_signal_connect (item, "activate",
148 G_CALLBACK (search_by_description), entry);
149 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
151 item = gtk_image_menu_item_new_with_mnemonic ("Search by _file name");
152 image = gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_MENU);
153 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
154 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
155 g_signal_connect (item, "activate",
156 G_CALLBACK (search_by_file), entry);
157 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
159 gtk_widget_show_all (menu);
165 icon_press_cb (GtkEntry *entry,
167 GdkEventButton *event,
170 if (position == GTK_ENTRY_ICON_PRIMARY)
171 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
172 event->button, event->time);
178 text_changed_cb (GtkEntry *entry,
184 has_text = gtk_entry_get_text_length (entry) > 0;
185 gtk_entry_set_icon_sensitive (entry,
186 GTK_ENTRY_ICON_SECONDARY,
188 gtk_widget_set_sensitive (button, has_text);
192 activate_cb (GtkEntry *entry,
195 if (search_progress_id != 0)
198 start_search (button, entry);
203 search_entry_destroyed (GtkWidget *widget)
205 if (finish_search_id != 0)
206 g_source_remove (finish_search_id);
208 if (search_progress_id != 0)
209 g_source_remove (search_progress_id);
215 entry_populate_popup (GtkEntry *entry,
220 GtkWidget *search_menu;
223 has_text = gtk_entry_get_text_length (entry) > 0;
225 item = gtk_separator_menu_item_new ();
226 gtk_widget_show (item);
227 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
229 item = gtk_menu_item_new_with_mnemonic ("C_lear");
230 gtk_widget_show (item);
231 g_signal_connect_swapped (item, "activate",
232 G_CALLBACK (clear_entry), entry);
233 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
234 gtk_widget_set_sensitive (item, has_text);
236 search_menu = create_search_menu (GTK_WIDGET (entry));
237 item = gtk_menu_item_new_with_label ("Search by");
238 gtk_widget_show (item);
239 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), search_menu);
240 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
244 do_search_entry (GtkWidget *do_widget)
246 GtkWidget *content_area;
252 GtkWidget *find_button;
253 GtkWidget *cancel_button;
257 window = gtk_dialog_new_with_buttons ("Search Entry",
258 GTK_WINDOW (do_widget),
263 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
265 g_signal_connect (window, "response",
266 G_CALLBACK (gtk_widget_destroy), NULL);
267 g_signal_connect (window, "destroy",
268 G_CALLBACK (search_entry_destroyed), &window);
270 content_area = gtk_dialog_get_content_area (GTK_DIALOG (window));
272 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
273 gtk_box_pack_start (GTK_BOX (content_area), vbox, TRUE, TRUE, 0);
274 gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
276 label = gtk_label_new (NULL);
277 gtk_label_set_markup (GTK_LABEL (label), "Search entry demo");
278 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
280 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
281 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
282 gtk_container_set_border_width (GTK_CONTAINER (hbox), 0);
284 /* Create our entry */
285 entry = gtk_entry_new ();
286 gtk_box_pack_start (GTK_BOX (hbox), entry, FALSE, FALSE, 0);
288 /* Create the find and cancel buttons */
289 notebook = gtk_notebook_new ();
290 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook), FALSE);
291 gtk_notebook_set_show_border (GTK_NOTEBOOK (notebook), FALSE);
292 gtk_box_pack_start (GTK_BOX (hbox), notebook, FALSE, FALSE, 0);
294 find_button = gtk_button_new_with_label ("Find");
295 g_signal_connect (find_button, "clicked",
296 G_CALLBACK (start_search), entry);
297 gtk_notebook_append_page (GTK_NOTEBOOK (notebook), find_button, NULL);
298 gtk_widget_show (find_button);
300 cancel_button = gtk_button_new_with_label ("Cancel");
301 g_signal_connect (cancel_button, "clicked",
302 G_CALLBACK (stop_search), NULL);
303 gtk_notebook_append_page (GTK_NOTEBOOK (notebook), cancel_button, NULL);
304 gtk_widget_show (cancel_button);
306 /* Set up the search icon */
307 search_by_name (NULL, GTK_ENTRY (entry));
309 /* Set up the clear icon */
310 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
311 GTK_ENTRY_ICON_SECONDARY,
313 text_changed_cb (GTK_ENTRY (entry), NULL, find_button);
315 g_signal_connect (entry, "icon-press",
316 G_CALLBACK (icon_press_cb), NULL);
317 g_signal_connect (entry, "notify::text",
318 G_CALLBACK (text_changed_cb), find_button);
319 g_signal_connect (entry, "activate",
320 G_CALLBACK (activate_cb), NULL);
322 /* Create the menu */
323 menu = create_search_menu (entry);
324 gtk_menu_attach_to_widget (GTK_MENU (menu), entry, NULL);
326 /* add accessible alternatives for icon functionality */
327 g_signal_connect (entry, "populate-popup",
328 G_CALLBACK (entry_populate_popup), NULL);
330 /* Give the focus to the close button */
331 button = gtk_dialog_get_widget_for_response (GTK_DIALOG (window), GTK_RESPONSE_NONE);
332 gtk_widget_grab_focus (button);
335 if (!gtk_widget_get_visible (window))
336 gtk_widget_show_all (window);
339 gtk_widget_destroy (menu);
340 gtk_widget_destroy (window);