1 /* gtkentrycompletion.c
2 * Copyright (C) 2003 Kristian Rietveld <kris@gtk.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include "gtkentrycompletion.h"
21 #include "gtkentryprivate.h"
22 #include "gtkcelllayout.h"
25 #include "gtkcellrenderertext.h"
26 #include "gtktreeselection.h"
27 #include "gtktreeview.h"
28 #include "gtkscrolledwindow.h"
30 #include "gtkwindow.h"
33 #include "gtksignal.h"
34 #include "gtkmarshalers.h"
52 PROP_MINIMUM_KEY_LENGTH
55 #define GTK_ENTRY_COMPLETION_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ENTRY_COMPLETION, GtkEntryCompletionPrivate))
57 static void gtk_entry_completion_class_init (GtkEntryCompletionClass *klass);
58 static void gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface);
59 static void gtk_entry_completion_init (GtkEntryCompletion *completion);
60 static void gtk_entry_completion_set_property (GObject *object,
64 static void gtk_entry_completion_get_property (GObject *object,
68 static void gtk_entry_completion_finalize (GObject *object);
70 static void gtk_entry_completion_pack_start (GtkCellLayout *cell_layout,
71 GtkCellRenderer *cell,
73 static void gtk_entry_completion_pack_end (GtkCellLayout *cell_layout,
74 GtkCellRenderer *cell,
76 static void gtk_entry_completion_clear (GtkCellLayout *cell_layout);
77 static void gtk_entry_completion_add_attribute (GtkCellLayout *cell_layout,
78 GtkCellRenderer *cell,
79 const char *attribute,
81 static void gtk_entry_completion_set_cell_data_func (GtkCellLayout *cell_layout,
82 GtkCellRenderer *cell,
83 GtkCellLayoutDataFunc func,
85 GDestroyNotify destroy);
86 static void gtk_entry_completion_clear_attributes (GtkCellLayout *cell_layout,
87 GtkCellRenderer *cell);
88 static void gtk_entry_completion_reorder (GtkCellLayout *cell_layout,
89 GtkCellRenderer *cell,
92 static gboolean gtk_entry_completion_visible_func (GtkTreeModel *model,
95 static gboolean gtk_entry_completion_popup_key_press (GtkWidget *widget,
98 static gboolean gtk_entry_completion_popup_button_press (GtkWidget *widget,
99 GdkEventButton *event,
101 static gboolean gtk_entry_completion_list_button_press (GtkWidget *widget,
102 GdkEventButton *event,
104 static gboolean gtk_entry_completion_action_button_press (GtkWidget *widget,
105 GdkEventButton *event,
107 static void gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
110 static void gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
114 static void gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
115 GtkCellRenderer *cell,
121 static GObjectClass *parent_class = NULL;
122 static guint entry_completion_signals[LAST_SIGNAL] = { 0 };
126 gtk_entry_completion_get_type (void)
128 static GType entry_completion_type = 0;
130 if (!entry_completion_type)
132 static const GTypeInfo entry_completion_info =
134 sizeof (GtkEntryCompletionClass),
137 (GClassInitFunc) gtk_entry_completion_class_init,
140 sizeof (GtkEntryCompletion),
142 (GInstanceInitFunc) gtk_entry_completion_init
145 static const GInterfaceInfo cell_layout_info =
147 (GInterfaceInitFunc) gtk_entry_completion_cell_layout_init,
152 entry_completion_type =
153 g_type_register_static (G_TYPE_OBJECT, "GtkEntryCompletion",
154 &entry_completion_info, 0);
156 g_type_add_interface_static (entry_completion_type,
157 GTK_TYPE_CELL_LAYOUT,
161 return entry_completion_type;
165 gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
167 GObjectClass *object_class;
169 parent_class = g_type_class_peek_parent (klass);
170 object_class = (GObjectClass *)klass;
172 object_class->set_property = gtk_entry_completion_set_property;
173 object_class->get_property = gtk_entry_completion_get_property;
174 object_class->finalize = gtk_entry_completion_finalize;
176 entry_completion_signals[MATCH_SELECTED] =
177 g_signal_new ("match_selected",
178 G_TYPE_FROM_CLASS (klass),
180 G_STRUCT_OFFSET (GtkEntryCompletionClass, match_selected),
181 _gtk_boolean_handled_accumulator, NULL,
182 _gtk_marshal_BOOLEAN__OBJECT_BOXED,
186 entry_completion_signals[ACTION_ACTIVATED] =
187 g_signal_new ("action_activated",
188 G_TYPE_FROM_CLASS (klass),
190 G_STRUCT_OFFSET (GtkEntryCompletionClass, action_activated),
192 _gtk_marshal_NONE__INT,
196 g_object_class_install_property (object_class,
198 g_param_spec_object ("model",
199 P_("Completion Model"),
200 P_("The model to find matches in"),
203 g_object_class_install_property (object_class,
204 PROP_MINIMUM_KEY_LENGTH,
205 g_param_spec_int ("minimum_key_length",
206 P_("Minimum Key Length"),
207 P_("Minimum length of the search key in order to look up matches"),
213 g_type_class_add_private (object_class, sizeof (GtkEntryCompletionPrivate));
217 gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface)
219 iface->pack_start = gtk_entry_completion_pack_start;
220 iface->pack_end = gtk_entry_completion_pack_end;
221 iface->clear = gtk_entry_completion_clear;
222 iface->add_attribute = gtk_entry_completion_add_attribute;
223 iface->set_cell_data_func = gtk_entry_completion_set_cell_data_func;
224 iface->clear_attributes = gtk_entry_completion_clear_attributes;
225 iface->reorder = gtk_entry_completion_reorder;
229 gtk_entry_completion_init (GtkEntryCompletion *completion)
231 GtkCellRenderer *cell;
232 GtkTreeSelection *sel;
233 GtkEntryCompletionPrivate *priv;
235 /* yes, also priv, need to keep the code readable */
236 priv = completion->priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (completion);
238 priv->minimum_key_length = 1;
239 priv->text_column = -1;
242 priv->filter_model = NULL;
244 priv->tree_view = gtk_tree_view_new ();
245 g_signal_connect (priv->tree_view, "button_press_event",
246 G_CALLBACK (gtk_entry_completion_list_button_press),
248 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
250 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
251 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
252 gtk_tree_selection_unselect_all (sel);
253 g_signal_connect (sel, "changed",
254 G_CALLBACK (gtk_entry_completion_selection_changed),
256 priv->first_sel_changed = TRUE;
258 priv->column = gtk_tree_view_column_new ();
259 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
261 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
262 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
264 GTK_POLICY_AUTOMATIC);
265 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
266 GTK_SHADOW_ETCHED_IN);
268 /* a nasty hack to get the completions treeview to size nicely */
269 gtk_widget_set_size_request (GTK_SCROLLED_WINDOW (priv->scrolled_window)->vscrollbar, -1, 0);
272 priv->actions = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_BOOLEAN);
275 gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->actions));
276 g_signal_connect (priv->action_view, "button_press_event",
277 G_CALLBACK (gtk_entry_completion_action_button_press),
279 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->action_view), FALSE);
281 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->action_view));
282 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
283 gtk_tree_selection_unselect_all (sel);
285 cell = gtk_cell_renderer_text_new ();
286 g_object_set (cell, "cell_background_gdk",
287 &priv->tree_view->style->bg[GTK_STATE_NORMAL],
289 gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (priv->action_view),
292 gtk_entry_completion_action_data_func,
297 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
298 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
299 g_signal_connect (priv->popup_window, "key_press_event",
300 G_CALLBACK (gtk_entry_completion_popup_key_press),
302 g_signal_connect (priv->popup_window, "button_press_event",
303 G_CALLBACK (gtk_entry_completion_popup_button_press),
306 priv->vbox = gtk_vbox_new (FALSE, 0);
307 gtk_container_add (GTK_CONTAINER (priv->popup_window), priv->vbox);
309 gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->tree_view);
310 gtk_box_pack_start (GTK_BOX (priv->vbox), priv->scrolled_window,
313 /* we don't want to see the action treeview when no actions have
314 * been inserted, so we pack the action treeview after the first
315 * action has been added
320 gtk_entry_completion_set_property (GObject *object,
325 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
330 gtk_entry_completion_set_model (completion,
331 g_value_get_object (value));
334 case PROP_MINIMUM_KEY_LENGTH:
335 gtk_entry_completion_set_minimum_key_length (completion,
336 g_value_get_int (value));
340 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
346 gtk_entry_completion_get_property (GObject *object,
351 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
356 g_value_set_object (value,
357 gtk_entry_completion_get_model (completion));
360 case PROP_MINIMUM_KEY_LENGTH:
361 g_value_set_int (value, gtk_entry_completion_get_minimum_key_length (completion));
365 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
371 gtk_entry_completion_finalize (GObject *object)
373 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
375 if (completion->priv->tree_view)
376 gtk_widget_destroy (completion->priv->tree_view);
378 if (completion->priv->entry)
379 gtk_entry_set_completion (GTK_ENTRY (completion->priv->entry), NULL);
381 if (completion->priv->actions)
382 g_object_unref (completion->priv->actions);
384 if (completion->priv->case_normalized_key)
385 g_free (completion->priv->case_normalized_key);
387 if (completion->priv->popup_window)
388 gtk_widget_destroy (completion->priv->popup_window);
390 G_OBJECT_CLASS (parent_class)->finalize (object);
393 /* implement cell layout interface */
395 gtk_entry_completion_pack_start (GtkCellLayout *cell_layout,
396 GtkCellRenderer *cell,
399 GtkEntryCompletionPrivate *priv;
401 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
403 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
405 gtk_tree_view_column_pack_start (priv->column, cell, expand);
409 gtk_entry_completion_pack_end (GtkCellLayout *cell_layout,
410 GtkCellRenderer *cell,
413 GtkEntryCompletionPrivate *priv;
415 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
417 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
419 gtk_tree_view_column_pack_end (priv->column, cell, expand);
423 gtk_entry_completion_clear (GtkCellLayout *cell_layout)
425 GtkEntryCompletionPrivate *priv;
427 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
429 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
431 gtk_tree_view_column_clear (priv->column);
435 gtk_entry_completion_add_attribute (GtkCellLayout *cell_layout,
436 GtkCellRenderer *cell,
437 const gchar *attribute,
440 GtkEntryCompletionPrivate *priv;
442 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
444 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
446 gtk_tree_view_column_add_attribute (priv->column, cell, attribute, column);
450 gtk_entry_completion_set_cell_data_func (GtkCellLayout *cell_layout,
451 GtkCellRenderer *cell,
452 GtkCellLayoutDataFunc func,
454 GDestroyNotify destroy)
456 GtkEntryCompletionPrivate *priv;
458 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
460 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
462 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column),
463 cell, func, func_data, destroy);
467 gtk_entry_completion_clear_attributes (GtkCellLayout *cell_layout,
468 GtkCellRenderer *cell)
470 GtkEntryCompletionPrivate *priv;
472 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
474 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
476 gtk_tree_view_column_clear_attributes (priv->column, cell);
480 gtk_entry_completion_reorder (GtkCellLayout *cell_layout,
481 GtkCellRenderer *cell,
484 GtkEntryCompletionPrivate *priv;
486 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
488 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
490 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column), cell, position);
493 /* all those callbacks */
495 gtk_entry_completion_default_completion_func (GtkEntryCompletion *completion,
501 gchar *normalized_string;
502 gchar *case_normalized_string;
504 gboolean ret = FALSE;
508 model = gtk_tree_model_filter_get_model (completion->priv->filter_model);
510 gtk_tree_model_get (model, iter,
511 completion->priv->text_column, &item,
516 normalized_string = g_utf8_normalize (item, -1, G_NORMALIZE_ALL);
517 case_normalized_string = g_utf8_casefold (normalized_string, -1);
519 if (!strncmp (key, case_normalized_string, strlen (key)))
523 g_free (normalized_string);
524 g_free (case_normalized_string);
531 gtk_entry_completion_visible_func (GtkTreeModel *model,
535 gboolean ret = FALSE;
537 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
539 if (!completion->priv->case_normalized_key)
542 if (completion->priv->text_column >= 0)
543 ret = gtk_entry_completion_default_completion_func (completion,
544 completion->priv->case_normalized_key,
548 else if (completion->priv->match_func)
549 ret = (* completion->priv->match_func) (completion,
550 completion->priv->case_normalized_key,
552 completion->priv->match_data);
558 gtk_entry_completion_popup_key_press (GtkWidget *widget,
562 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
564 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
567 /* propagate event to the entry */
568 gtk_widget_event (completion->priv->entry, (GdkEvent *)event);
574 gtk_entry_completion_popup_button_press (GtkWidget *widget,
575 GdkEventButton *event,
578 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
580 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
583 /* if we come here, it's usually time to popdown */
584 _gtk_entry_completion_popdown (completion);
590 gtk_entry_completion_list_button_press (GtkWidget *widget,
591 GdkEventButton *event,
594 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
595 GtkTreePath *path = NULL;
597 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
600 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
602 &path, NULL, NULL, NULL))
607 gtk_tree_model_get_iter (GTK_TREE_MODEL (completion->priv->filter_model),
609 gtk_tree_path_free (path);
611 g_signal_emit (completion, entry_completion_signals[MATCH_SELECTED],
612 0, GTK_TREE_MODEL (completion->priv->filter_model),
619 gtk_tree_model_get (GTK_TREE_MODEL (completion->priv->filter_model),
621 completion->priv->text_column, &str,
624 g_signal_handler_block (completion->priv->entry,
625 completion->priv->changed_id);
626 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), str);
627 g_signal_handler_unblock (completion->priv->entry,
628 completion->priv->changed_id);
630 /* move cursor to the end */
631 gtk_editable_set_position (GTK_EDITABLE (completion->priv->entry),
637 _gtk_entry_completion_popdown (completion);
645 gtk_entry_completion_action_button_press (GtkWidget *widget,
646 GdkEventButton *event,
649 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
650 GtkTreePath *path = NULL;
652 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
655 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
657 &path, NULL, NULL, NULL))
659 g_signal_emit (completion, entry_completion_signals[ACTION_ACTIVATED],
660 0, gtk_tree_path_get_indices (path)[0]);
661 gtk_tree_path_free (path);
663 _gtk_entry_completion_popdown (completion);
671 gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
672 GtkCellRenderer *cell,
677 gchar *string = NULL;
680 gtk_tree_model_get (model, iter,
689 g_object_set (G_OBJECT (cell),
694 g_object_set (G_OBJECT (cell),
703 gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
706 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
708 if (completion->priv->first_sel_changed)
710 completion->priv->first_sel_changed = FALSE;
711 if (gtk_widget_is_focus (completion->priv->tree_view))
712 gtk_tree_selection_unselect_all (selection);
719 * gtk_entry_completion_new:
721 * Creates a new #GtkEntryCompletion object.
723 * Return value: A newly created #GtkEntryCompletion object.
728 gtk_entry_completion_new (void)
730 GtkEntryCompletion *completion;
732 completion = g_object_new (GTK_TYPE_ENTRY_COMPLETION, NULL);
738 * gtk_entry_completion_get_entry:
739 * @completion: A #GtkEntryCompletion.
741 * Gets the entry @completion has been attached to.
743 * Return value: The entry @completion has been attached to.
748 gtk_entry_completion_get_entry (GtkEntryCompletion *completion)
750 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
752 return completion->priv->entry;
756 * gtk_entry_completion_set_model:
757 * @completion: A #GtkEntryCompletion.
758 * @model: The #GtkTreeModel.
760 * Sets the model for a #GtkEntryCompletion. If @completion already has
761 * a model set, it will remove it before setting the new model.
766 gtk_entry_completion_set_model (GtkEntryCompletion *completion,
769 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
770 g_return_if_fail (GTK_IS_TREE_MODEL (model));
772 /* code will unref the old filter model (if any) */
773 completion->priv->filter_model =
774 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
775 gtk_tree_model_filter_set_visible_func (completion->priv->filter_model,
776 gtk_entry_completion_visible_func,
779 gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
780 GTK_TREE_MODEL (completion->priv->filter_model));
781 g_object_unref (G_OBJECT (completion->priv->filter_model));
785 * gtk_entry_completion_get_model:
786 * @completion: A #GtkEntryCompletion.
788 * Returns the model the #GtkEntryCompletion is using as data source.
789 * Returns %NULL if the model is unset.
791 * Return value: A #GtkTreeModel, or %NULL if none is currently being used.
796 gtk_entry_completion_get_model (GtkEntryCompletion *completion)
798 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
800 return gtk_tree_model_filter_get_model (completion->priv->filter_model);
804 * gtk_entry_completion_set_match_func:
805 * @completion: A #GtkEntryCompletion.
806 * @func: The #GtkEntryCompletionMatchFunc to use.
807 * @func_data: The user data for @func.
808 * @func_notify: Destroy notifier for @func_data.
810 * Sets the match function for @completion to be @func. The match function
811 * is used to determine if a row should or should not be in the completion
817 gtk_entry_completion_set_match_func (GtkEntryCompletion *completion,
818 GtkEntryCompletionMatchFunc func,
820 GDestroyNotify func_notify)
822 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
824 if (completion->priv->match_notify)
825 (* completion->priv->match_notify) (completion->priv->match_data);
827 completion->priv->match_func = func;
828 completion->priv->match_data = func_data;
829 completion->priv->match_notify = func_notify;
833 * gtk_entry_completion_set_minimum_key_length:
834 * @completion: A #GtkEntryCompletion.
835 * @length: The minimum length of the key in order to start completing.
837 * Requires the length of the search key for @completion to be at least
838 * @length. This is useful for long lists, where completing using a small
839 * key takes a lot of time and will come up with meaningless results anyway
840 * (ie, a too large dataset).
845 gtk_entry_completion_set_minimum_key_length (GtkEntryCompletion *completion,
848 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
849 g_return_if_fail (length >= 1);
851 completion->priv->minimum_key_length = length;
855 * gtk_entry_completion_get_minimum_key_length:
856 * @completion: A #GtkEntryCompletion.
858 * Returns the minimum key length as set for @completion.
860 * Return value: The currently used minimum key length.
865 gtk_entry_completion_get_minimum_key_length (GtkEntryCompletion *completion)
867 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), 0);
869 return completion->priv->minimum_key_length;
873 * gtk_entry_completion_complete:
874 * @completion: A #GtkEntryCompletion.
876 * Requests a completion operation, or in other words a refiltering of the
877 * current list with completions, using the current key. The completion list
878 * view will be updated accordingly.
883 gtk_entry_completion_complete (GtkEntryCompletion *completion)
887 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
888 g_return_if_fail (completion->priv->filter_model != NULL);
890 if (completion->priv->case_normalized_key)
891 g_free (completion->priv->case_normalized_key);
893 tmp = g_utf8_normalize (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)),
894 -1, G_NORMALIZE_ALL);
895 completion->priv->case_normalized_key = g_utf8_casefold (tmp, -1);
898 gtk_tree_model_filter_refilter (completion->priv->filter_model);
902 gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
909 gtk_list_store_insert (completion->priv->actions, &iter, index);
910 gtk_list_store_set (completion->priv->actions, &iter,
915 if (!completion->priv->action_view->parent)
917 GtkTreePath *path = gtk_tree_path_new_from_indices (0, -1);
919 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
921 gtk_tree_path_free (path);
923 gtk_box_pack_start (GTK_BOX (completion->priv->vbox),
924 completion->priv->action_view, FALSE, FALSE, 0);
925 gtk_widget_show (completion->priv->action_view);
930 * gtk_entry_completion_insert_action_text:
931 * @completion: A #GtkEntryCompletion.
932 * @index: The index of the item to insert.
933 * @text: Text of the item to insert.
935 * Inserts an action in @completion's action item list at position @index
936 * with text @text. If you want the action item to have markup, use
937 * gtk_entry_completion_insert_action_markup().
942 gtk_entry_completion_insert_action_text (GtkEntryCompletion *completion,
946 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
947 g_return_if_fail (text != NULL);
949 gtk_entry_completion_insert_action (completion, index, text, FALSE);
953 * gtk_entry_completion_insert_action_markup:
954 * @completion: A #GtkEntryCompletion.
955 * @index: The index of the item to insert.
956 * @markup: Markup of the item to insert.
958 * Inserts an action in @completion's action item list at position @index
959 * with markup @markup.
964 gtk_entry_completion_insert_action_markup (GtkEntryCompletion *completion,
968 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
969 g_return_if_fail (markup != NULL);
971 gtk_entry_completion_insert_action (completion, index, markup, TRUE);
975 * gtk_entry_completion_delete_action:
976 * @completion: A #GtkEntryCompletion.
977 * @index: The index of the item to Delete.
979 * Deletes the action at @index from @completion's action list.
984 gtk_entry_completion_delete_action (GtkEntryCompletion *completion,
989 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
990 g_return_if_fail (index >= 0);
992 gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (completion->priv->actions),
994 gtk_list_store_remove (completion->priv->actions, &iter);
998 * gtk_entry_completion_set_text_column:
999 * @completion: A #GtkEntryCompletion.
1000 * @column: The column in the model of @completion to get strings from.
1002 * Convience function for setting up the most used case of this code: a
1003 * completion list with just strings. This function will set up @completion
1004 * to have a list displaying all (and just) strings in the completion list,
1005 * and to get those strings from @column in the model of @completion.
1010 gtk_entry_completion_set_text_column (GtkEntryCompletion *completion,
1013 GtkCellRenderer *cell;
1015 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1016 g_return_if_fail (column >= 0);
1018 completion->priv->text_column = column;
1020 cell = gtk_cell_renderer_text_new ();
1021 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion),
1023 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (completion),
1030 /* lame copy from gtkentry.c */
1032 get_borders (GtkEntry *entry,
1036 GtkWidget *widget = GTK_WIDGET (entry);
1038 gboolean interior_focus;
1040 gtk_widget_style_get (widget,
1041 "interior-focus", &interior_focus,
1042 "focus-line-width", &focus_width,
1045 if (entry->has_frame)
1047 *xborder = widget->style->xthickness;
1048 *yborder = widget->style->ythickness;
1056 if (!interior_focus)
1058 *xborder += focus_width;
1059 *yborder += focus_width;
1063 /* some nasty size requisition */
1065 _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
1067 gint items, height, x_border, y_border;
1069 get_borders (GTK_ENTRY (completion->priv->entry), &x_border, &y_border);
1071 items = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
1073 items = MIN (items, 15);
1075 gtk_tree_view_column_cell_get_size (completion->priv->column, NULL,
1076 NULL, NULL, NULL, &height);
1079 gtk_widget_hide (completion->priv->scrolled_window);
1081 gtk_widget_show (completion->priv->scrolled_window);
1083 gtk_widget_set_size_request (completion->priv->tree_view,
1084 completion->priv->entry->allocation.width - 2 * x_border,
1087 /* default on no match */
1088 completion->priv->current_selected = -1;
1090 items = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
1094 gtk_widget_show (completion->priv->action_view);
1096 gtk_tree_view_column_cell_get_size (gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0),
1097 NULL, NULL, NULL, NULL,
1100 gtk_widget_set_size_request (completion->priv->action_view,
1101 completion->priv->entry->allocation.width - 2 * x_border,
1105 gtk_widget_hide (completion->priv->action_view);
1111 _gtk_entry_completion_popup (GtkEntryCompletion *completion)
1113 gint x, y, x_border, y_border;
1116 if (GTK_WIDGET_MAPPED (completion->priv->popup_window))
1119 gtk_widget_show_all (completion->priv->vbox);
1121 gdk_window_get_origin (completion->priv->entry->window, &x, &y);
1122 get_borders (GTK_ENTRY (completion->priv->entry), &x_border, &y_border);
1127 height = _gtk_entry_completion_resize_popup (completion);
1129 gtk_window_move (GTK_WINDOW (completion->priv->popup_window), x, y + height);
1131 gtk_widget_show (completion->priv->popup_window);
1133 gtk_grab_add (completion->priv->popup_window);
1134 gdk_pointer_grab (completion->priv->popup_window->window, TRUE,
1135 GDK_BUTTON_PRESS_MASK |
1136 GDK_BUTTON_RELEASE_MASK |
1137 GDK_POINTER_MOTION_MASK,
1138 NULL, NULL, GDK_CURRENT_TIME);
1142 _gtk_entry_completion_popdown (GtkEntryCompletion *completion)
1144 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1145 gtk_grab_remove (completion->priv->popup_window);
1147 gtk_widget_hide (completion->priv->popup_window);