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.
21 #include "gtkentrycompletion.h"
22 #include "gtkentryprivate.h"
23 #include "gtkcelllayout.h"
26 #include "gtkcellrenderertext.h"
27 #include "gtktreeselection.h"
28 #include "gtktreeview.h"
29 #include "gtkscrolledwindow.h"
31 #include "gtkwindow.h"
34 #include "gtksignal.h"
35 #include "gtkmarshalers.h"
53 PROP_MINIMUM_KEY_LENGTH
56 #define GTK_ENTRY_COMPLETION_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ENTRY_COMPLETION, GtkEntryCompletionPrivate))
58 static void gtk_entry_completion_class_init (GtkEntryCompletionClass *klass);
59 static void gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface);
60 static void gtk_entry_completion_init (GtkEntryCompletion *completion);
61 static void gtk_entry_completion_set_property (GObject *object,
65 static void gtk_entry_completion_get_property (GObject *object,
69 static void gtk_entry_completion_finalize (GObject *object);
71 static void gtk_entry_completion_pack_start (GtkCellLayout *cell_layout,
72 GtkCellRenderer *cell,
74 static void gtk_entry_completion_pack_end (GtkCellLayout *cell_layout,
75 GtkCellRenderer *cell,
77 static void gtk_entry_completion_clear (GtkCellLayout *cell_layout);
78 static void gtk_entry_completion_add_attribute (GtkCellLayout *cell_layout,
79 GtkCellRenderer *cell,
80 const char *attribute,
82 static void gtk_entry_completion_set_cell_data_func (GtkCellLayout *cell_layout,
83 GtkCellRenderer *cell,
84 GtkCellLayoutDataFunc func,
86 GDestroyNotify destroy);
87 static void gtk_entry_completion_clear_attributes (GtkCellLayout *cell_layout,
88 GtkCellRenderer *cell);
89 static void gtk_entry_completion_reorder (GtkCellLayout *cell_layout,
90 GtkCellRenderer *cell,
93 static gboolean gtk_entry_completion_visible_func (GtkTreeModel *model,
96 static gboolean gtk_entry_completion_popup_key_press (GtkWidget *widget,
99 static gboolean gtk_entry_completion_popup_button_press (GtkWidget *widget,
100 GdkEventButton *event,
102 static gboolean gtk_entry_completion_list_button_press (GtkWidget *widget,
103 GdkEventButton *event,
105 static gboolean gtk_entry_completion_action_button_press (GtkWidget *widget,
106 GdkEventButton *event,
108 static void gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
111 static void gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
115 static void gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
116 GtkCellRenderer *cell,
122 static GObjectClass *parent_class = NULL;
123 static guint entry_completion_signals[LAST_SIGNAL] = { 0 };
127 gtk_entry_completion_get_type (void)
129 static GType entry_completion_type = 0;
131 if (!entry_completion_type)
133 static const GTypeInfo entry_completion_info =
135 sizeof (GtkEntryCompletionClass),
138 (GClassInitFunc) gtk_entry_completion_class_init,
141 sizeof (GtkEntryCompletion),
143 (GInstanceInitFunc) gtk_entry_completion_init
146 static const GInterfaceInfo cell_layout_info =
148 (GInterfaceInitFunc) gtk_entry_completion_cell_layout_init,
153 entry_completion_type =
154 g_type_register_static (G_TYPE_OBJECT, "GtkEntryCompletion",
155 &entry_completion_info, 0);
157 g_type_add_interface_static (entry_completion_type,
158 GTK_TYPE_CELL_LAYOUT,
162 return entry_completion_type;
166 gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
168 GObjectClass *object_class;
170 parent_class = g_type_class_peek_parent (klass);
171 object_class = (GObjectClass *)klass;
173 object_class->set_property = gtk_entry_completion_set_property;
174 object_class->get_property = gtk_entry_completion_get_property;
175 object_class->finalize = gtk_entry_completion_finalize;
178 * GtkEntryCompletion::match-selected:
179 * @widget: the object which received the signal
180 * @model: the #GtkTreeModel containing the matches
181 * @iter: a #GtkTreeIter positioned at the selected match
183 * The ::match-selected signal is emitted when a match from the list
184 * is selected. The default behaviour is to replace the contents of the
185 * entry with the contents of the text column in the row pointed to by
188 * Return value: %TRUE if the signal has been handled
190 entry_completion_signals[MATCH_SELECTED] =
191 g_signal_new ("match_selected",
192 G_TYPE_FROM_CLASS (klass),
194 G_STRUCT_OFFSET (GtkEntryCompletionClass, match_selected),
195 _gtk_boolean_handled_accumulator, NULL,
196 _gtk_marshal_BOOLEAN__OBJECT_BOXED,
202 * GtkEntryCompletion::action-activated:
203 * @widget: the object which received the signal
204 * @index: the index of the activated action
206 * The ::action-activated signal is emitted when an action
209 entry_completion_signals[ACTION_ACTIVATED] =
210 g_signal_new ("action_activated",
211 G_TYPE_FROM_CLASS (klass),
213 G_STRUCT_OFFSET (GtkEntryCompletionClass, action_activated),
215 _gtk_marshal_NONE__INT,
219 g_object_class_install_property (object_class,
221 g_param_spec_object ("model",
222 P_("Completion Model"),
223 P_("The model to find matches in"),
226 g_object_class_install_property (object_class,
227 PROP_MINIMUM_KEY_LENGTH,
228 g_param_spec_int ("minimum_key_length",
229 P_("Minimum Key Length"),
230 P_("Minimum length of the search key in order to look up matches"),
236 g_type_class_add_private (object_class, sizeof (GtkEntryCompletionPrivate));
240 gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface)
242 iface->pack_start = gtk_entry_completion_pack_start;
243 iface->pack_end = gtk_entry_completion_pack_end;
244 iface->clear = gtk_entry_completion_clear;
245 iface->add_attribute = gtk_entry_completion_add_attribute;
246 iface->set_cell_data_func = gtk_entry_completion_set_cell_data_func;
247 iface->clear_attributes = gtk_entry_completion_clear_attributes;
248 iface->reorder = gtk_entry_completion_reorder;
252 gtk_entry_completion_init (GtkEntryCompletion *completion)
254 GtkCellRenderer *cell;
255 GtkTreeSelection *sel;
256 GtkEntryCompletionPrivate *priv;
258 /* yes, also priv, need to keep the code readable */
259 priv = completion->priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (completion);
261 priv->minimum_key_length = 1;
262 priv->text_column = -1;
265 priv->filter_model = NULL;
267 priv->tree_view = gtk_tree_view_new ();
268 g_signal_connect (priv->tree_view, "button_press_event",
269 G_CALLBACK (gtk_entry_completion_list_button_press),
271 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
273 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
274 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
275 gtk_tree_selection_unselect_all (sel);
276 g_signal_connect (sel, "changed",
277 G_CALLBACK (gtk_entry_completion_selection_changed),
279 priv->first_sel_changed = TRUE;
281 priv->column = gtk_tree_view_column_new ();
282 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
284 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
285 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
287 GTK_POLICY_AUTOMATIC);
288 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
289 GTK_SHADOW_ETCHED_IN);
291 /* a nasty hack to get the completions treeview to size nicely */
292 gtk_widget_set_size_request (GTK_SCROLLED_WINDOW (priv->scrolled_window)->vscrollbar, -1, 0);
295 priv->actions = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_BOOLEAN);
298 gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->actions));
299 g_signal_connect (priv->action_view, "button_press_event",
300 G_CALLBACK (gtk_entry_completion_action_button_press),
302 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->action_view), FALSE);
304 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->action_view));
305 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
306 gtk_tree_selection_unselect_all (sel);
308 cell = gtk_cell_renderer_text_new ();
309 gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (priv->action_view),
312 gtk_entry_completion_action_data_func,
317 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
318 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
319 g_signal_connect (priv->popup_window, "key_press_event",
320 G_CALLBACK (gtk_entry_completion_popup_key_press),
322 g_signal_connect (priv->popup_window, "button_press_event",
323 G_CALLBACK (gtk_entry_completion_popup_button_press),
326 priv->vbox = gtk_vbox_new (FALSE, 0);
327 gtk_container_add (GTK_CONTAINER (priv->popup_window), priv->vbox);
329 gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->tree_view);
330 gtk_box_pack_start (GTK_BOX (priv->vbox), priv->scrolled_window,
333 /* we don't want to see the action treeview when no actions have
334 * been inserted, so we pack the action treeview after the first
335 * action has been added
340 gtk_entry_completion_set_property (GObject *object,
345 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
350 gtk_entry_completion_set_model (completion,
351 g_value_get_object (value));
354 case PROP_MINIMUM_KEY_LENGTH:
355 gtk_entry_completion_set_minimum_key_length (completion,
356 g_value_get_int (value));
360 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
366 gtk_entry_completion_get_property (GObject *object,
371 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
376 g_value_set_object (value,
377 gtk_entry_completion_get_model (completion));
380 case PROP_MINIMUM_KEY_LENGTH:
381 g_value_set_int (value, gtk_entry_completion_get_minimum_key_length (completion));
385 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
391 gtk_entry_completion_finalize (GObject *object)
393 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
395 if (completion->priv->tree_view)
396 gtk_widget_destroy (completion->priv->tree_view);
398 if (completion->priv->entry)
399 gtk_entry_set_completion (GTK_ENTRY (completion->priv->entry), NULL);
401 if (completion->priv->actions)
402 g_object_unref (completion->priv->actions);
404 if (completion->priv->case_normalized_key)
405 g_free (completion->priv->case_normalized_key);
407 if (completion->priv->popup_window)
408 gtk_widget_destroy (completion->priv->popup_window);
410 G_OBJECT_CLASS (parent_class)->finalize (object);
413 /* implement cell layout interface */
415 gtk_entry_completion_pack_start (GtkCellLayout *cell_layout,
416 GtkCellRenderer *cell,
419 GtkEntryCompletionPrivate *priv;
421 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
423 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
425 gtk_tree_view_column_pack_start (priv->column, cell, expand);
429 gtk_entry_completion_pack_end (GtkCellLayout *cell_layout,
430 GtkCellRenderer *cell,
433 GtkEntryCompletionPrivate *priv;
435 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
437 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
439 gtk_tree_view_column_pack_end (priv->column, cell, expand);
443 gtk_entry_completion_clear (GtkCellLayout *cell_layout)
445 GtkEntryCompletionPrivate *priv;
447 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
449 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
451 gtk_tree_view_column_clear (priv->column);
455 gtk_entry_completion_add_attribute (GtkCellLayout *cell_layout,
456 GtkCellRenderer *cell,
457 const gchar *attribute,
460 GtkEntryCompletionPrivate *priv;
462 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
464 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
466 gtk_tree_view_column_add_attribute (priv->column, cell, attribute, column);
470 gtk_entry_completion_set_cell_data_func (GtkCellLayout *cell_layout,
471 GtkCellRenderer *cell,
472 GtkCellLayoutDataFunc func,
474 GDestroyNotify destroy)
476 GtkEntryCompletionPrivate *priv;
478 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
480 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
482 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column),
483 cell, func, func_data, destroy);
487 gtk_entry_completion_clear_attributes (GtkCellLayout *cell_layout,
488 GtkCellRenderer *cell)
490 GtkEntryCompletionPrivate *priv;
492 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
494 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
496 gtk_tree_view_column_clear_attributes (priv->column, cell);
500 gtk_entry_completion_reorder (GtkCellLayout *cell_layout,
501 GtkCellRenderer *cell,
504 GtkEntryCompletionPrivate *priv;
506 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
508 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
510 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column), cell, position);
513 /* all those callbacks */
515 gtk_entry_completion_default_completion_func (GtkEntryCompletion *completion,
521 gchar *normalized_string;
522 gchar *case_normalized_string;
524 gboolean ret = FALSE;
528 model = gtk_tree_model_filter_get_model (completion->priv->filter_model);
530 gtk_tree_model_get (model, iter,
531 completion->priv->text_column, &item,
536 normalized_string = g_utf8_normalize (item, -1, G_NORMALIZE_ALL);
537 case_normalized_string = g_utf8_casefold (normalized_string, -1);
539 if (!strncmp (key, case_normalized_string, strlen (key)))
543 g_free (normalized_string);
544 g_free (case_normalized_string);
551 gtk_entry_completion_visible_func (GtkTreeModel *model,
555 gboolean ret = FALSE;
557 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
559 if (!completion->priv->case_normalized_key)
562 if (completion->priv->match_func)
563 ret = (* completion->priv->match_func) (completion,
564 completion->priv->case_normalized_key,
566 completion->priv->match_data);
567 else if (completion->priv->text_column >= 0)
568 ret = gtk_entry_completion_default_completion_func (completion,
569 completion->priv->case_normalized_key,
577 gtk_entry_completion_popup_key_press (GtkWidget *widget,
581 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
583 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
586 /* propagate event to the entry */
587 gtk_widget_event (completion->priv->entry, (GdkEvent *)event);
593 gtk_entry_completion_popup_button_press (GtkWidget *widget,
594 GdkEventButton *event,
597 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
599 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
602 /* if we come here, it's usually time to popdown */
603 _gtk_entry_completion_popdown (completion);
609 gtk_entry_completion_list_button_press (GtkWidget *widget,
610 GdkEventButton *event,
613 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
614 GtkTreePath *path = NULL;
616 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
619 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
621 &path, NULL, NULL, NULL))
626 gtk_tree_model_get_iter (GTK_TREE_MODEL (completion->priv->filter_model),
628 gtk_tree_path_free (path);
630 g_signal_handler_block (completion->priv->entry,
631 completion->priv->changed_id);
632 g_signal_emit (completion, entry_completion_signals[MATCH_SELECTED],
633 0, GTK_TREE_MODEL (completion->priv->filter_model),
635 g_signal_handler_unblock (completion->priv->entry,
636 completion->priv->changed_id);
642 gtk_tree_model_get (GTK_TREE_MODEL (completion->priv->filter_model),
644 completion->priv->text_column, &str,
647 g_signal_handler_block (completion->priv->entry,
648 completion->priv->changed_id);
649 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), str);
650 g_signal_handler_unblock (completion->priv->entry,
651 completion->priv->changed_id);
653 /* move cursor to the end */
654 gtk_editable_set_position (GTK_EDITABLE (completion->priv->entry),
660 _gtk_entry_completion_popdown (completion);
668 gtk_entry_completion_action_button_press (GtkWidget *widget,
669 GdkEventButton *event,
672 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
673 GtkTreePath *path = NULL;
675 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
678 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
680 &path, NULL, NULL, NULL))
682 g_signal_emit (completion, entry_completion_signals[ACTION_ACTIVATED],
683 0, gtk_tree_path_get_indices (path)[0]);
684 gtk_tree_path_free (path);
686 _gtk_entry_completion_popdown (completion);
694 gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
695 GtkCellRenderer *cell,
700 gchar *string = NULL;
703 gtk_tree_model_get (model, iter,
712 g_object_set (G_OBJECT (cell),
717 g_object_set (G_OBJECT (cell),
726 gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
729 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
731 if (completion->priv->first_sel_changed)
733 completion->priv->first_sel_changed = FALSE;
734 if (gtk_widget_is_focus (completion->priv->tree_view))
735 gtk_tree_selection_unselect_all (selection);
742 * gtk_entry_completion_new:
744 * Creates a new #GtkEntryCompletion object.
746 * Return value: A newly created #GtkEntryCompletion object.
751 gtk_entry_completion_new (void)
753 GtkEntryCompletion *completion;
755 completion = g_object_new (GTK_TYPE_ENTRY_COMPLETION, NULL);
761 * gtk_entry_completion_get_entry:
762 * @completion: A #GtkEntryCompletion.
764 * Gets the entry @completion has been attached to.
766 * Return value: The entry @completion has been attached to.
771 gtk_entry_completion_get_entry (GtkEntryCompletion *completion)
773 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
775 return completion->priv->entry;
779 * gtk_entry_completion_set_model:
780 * @completion: A #GtkEntryCompletion.
781 * @model: The #GtkTreeModel.
783 * Sets the model for a #GtkEntryCompletion. If @completion already has
784 * a model set, it will remove it before setting the new model.
789 gtk_entry_completion_set_model (GtkEntryCompletion *completion,
792 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
793 g_return_if_fail (GTK_IS_TREE_MODEL (model));
795 /* code will unref the old filter model (if any) */
796 completion->priv->filter_model =
797 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
798 gtk_tree_model_filter_set_visible_func (completion->priv->filter_model,
799 gtk_entry_completion_visible_func,
802 gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
803 GTK_TREE_MODEL (completion->priv->filter_model));
804 g_object_unref (G_OBJECT (completion->priv->filter_model));
808 * gtk_entry_completion_get_model:
809 * @completion: A #GtkEntryCompletion.
811 * Returns the model the #GtkEntryCompletion is using as data source.
812 * Returns %NULL if the model is unset.
814 * Return value: A #GtkTreeModel, or %NULL if none is currently being used.
819 gtk_entry_completion_get_model (GtkEntryCompletion *completion)
821 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
823 return gtk_tree_model_filter_get_model (completion->priv->filter_model);
827 * gtk_entry_completion_set_match_func:
828 * @completion: A #GtkEntryCompletion.
829 * @func: The #GtkEntryCompletionMatchFunc to use.
830 * @func_data: The user data for @func.
831 * @func_notify: Destroy notifier for @func_data.
833 * Sets the match function for @completion to be @func. The match function
834 * is used to determine if a row should or should not be in the completion
840 gtk_entry_completion_set_match_func (GtkEntryCompletion *completion,
841 GtkEntryCompletionMatchFunc func,
843 GDestroyNotify func_notify)
845 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
847 if (completion->priv->match_notify)
848 (* completion->priv->match_notify) (completion->priv->match_data);
850 completion->priv->match_func = func;
851 completion->priv->match_data = func_data;
852 completion->priv->match_notify = func_notify;
856 * gtk_entry_completion_set_minimum_key_length:
857 * @completion: A #GtkEntryCompletion.
858 * @length: The minimum length of the key in order to start completing.
860 * Requires the length of the search key for @completion to be at least
861 * @length. This is useful for long lists, where completing using a small
862 * key takes a lot of time and will come up with meaningless results anyway
863 * (ie, a too large dataset).
868 gtk_entry_completion_set_minimum_key_length (GtkEntryCompletion *completion,
871 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
872 g_return_if_fail (length >= 1);
874 completion->priv->minimum_key_length = length;
878 * gtk_entry_completion_get_minimum_key_length:
879 * @completion: A #GtkEntryCompletion.
881 * Returns the minimum key length as set for @completion.
883 * Return value: The currently used minimum key length.
888 gtk_entry_completion_get_minimum_key_length (GtkEntryCompletion *completion)
890 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), 0);
892 return completion->priv->minimum_key_length;
896 * gtk_entry_completion_complete:
897 * @completion: A #GtkEntryCompletion.
899 * Requests a completion operation, or in other words a refiltering of the
900 * current list with completions, using the current key. The completion list
901 * view will be updated accordingly.
906 gtk_entry_completion_complete (GtkEntryCompletion *completion)
910 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
911 g_return_if_fail (completion->priv->filter_model != NULL);
913 if (completion->priv->case_normalized_key)
914 g_free (completion->priv->case_normalized_key);
916 tmp = g_utf8_normalize (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)),
917 -1, G_NORMALIZE_ALL);
918 completion->priv->case_normalized_key = g_utf8_casefold (tmp, -1);
921 gtk_tree_model_filter_refilter (completion->priv->filter_model);
925 gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
932 gtk_list_store_insert (completion->priv->actions, &iter, index);
933 gtk_list_store_set (completion->priv->actions, &iter,
938 if (!completion->priv->action_view->parent)
940 GtkTreePath *path = gtk_tree_path_new_from_indices (0, -1);
942 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
944 gtk_tree_path_free (path);
946 gtk_box_pack_start (GTK_BOX (completion->priv->vbox),
947 completion->priv->action_view, FALSE, FALSE, 0);
948 gtk_widget_show (completion->priv->action_view);
953 * gtk_entry_completion_insert_action_text:
954 * @completion: A #GtkEntryCompletion.
955 * @index: The index of the item to insert.
956 * @text: Text of the item to insert.
958 * Inserts an action in @completion's action item list at position @index
959 * with text @text. If you want the action item to have markup, use
960 * gtk_entry_completion_insert_action_markup().
965 gtk_entry_completion_insert_action_text (GtkEntryCompletion *completion,
969 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
970 g_return_if_fail (text != NULL);
972 gtk_entry_completion_insert_action (completion, index, text, FALSE);
976 * gtk_entry_completion_insert_action_markup:
977 * @completion: A #GtkEntryCompletion.
978 * @index: The index of the item to insert.
979 * @markup: Markup of the item to insert.
981 * Inserts an action in @completion's action item list at position @index
982 * with markup @markup.
987 gtk_entry_completion_insert_action_markup (GtkEntryCompletion *completion,
991 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
992 g_return_if_fail (markup != NULL);
994 gtk_entry_completion_insert_action (completion, index, markup, TRUE);
998 * gtk_entry_completion_delete_action:
999 * @completion: A #GtkEntryCompletion.
1000 * @index: The index of the item to Delete.
1002 * Deletes the action at @index from @completion's action list.
1007 gtk_entry_completion_delete_action (GtkEntryCompletion *completion,
1012 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1013 g_return_if_fail (index >= 0);
1015 gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (completion->priv->actions),
1016 &iter, NULL, index);
1017 gtk_list_store_remove (completion->priv->actions, &iter);
1021 * gtk_entry_completion_set_text_column:
1022 * @completion: A #GtkEntryCompletion.
1023 * @column: The column in the model of @completion to get strings from.
1025 * Convenience function for setting up the most used case of this code: a
1026 * completion list with just strings. This function will set up @completion
1027 * to have a list displaying all (and just) strings in the completion list,
1028 * and to get those strings from @column in the model of @completion.
1030 * This functions creates and adds a GtkCellRendererText for the selected column.
1035 gtk_entry_completion_set_text_column (GtkEntryCompletion *completion,
1038 GtkCellRenderer *cell;
1040 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1041 g_return_if_fail (column >= 0);
1043 completion->priv->text_column = column;
1045 cell = gtk_cell_renderer_text_new ();
1046 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion),
1048 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (completion),
1055 /* lame copy from gtkentry.c */
1057 get_borders (GtkEntry *entry,
1061 GtkWidget *widget = GTK_WIDGET (entry);
1063 gboolean interior_focus;
1065 gtk_widget_style_get (widget,
1066 "interior-focus", &interior_focus,
1067 "focus-line-width", &focus_width,
1070 if (entry->has_frame)
1072 *xborder = widget->style->xthickness;
1073 *yborder = widget->style->ythickness;
1081 if (!interior_focus)
1083 *xborder += focus_width;
1084 *yborder += focus_width;
1088 /* some nasty size requisition */
1090 _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
1092 gint items, height, x_border, y_border;
1094 get_borders (GTK_ENTRY (completion->priv->entry), &x_border, &y_border);
1096 items = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
1098 items = MIN (items, 15);
1100 gtk_tree_view_column_cell_get_size (completion->priv->column, NULL,
1101 NULL, NULL, NULL, &height);
1104 gtk_widget_hide (completion->priv->scrolled_window);
1106 gtk_widget_show (completion->priv->scrolled_window);
1108 gtk_widget_set_size_request (completion->priv->tree_view,
1109 completion->priv->entry->allocation.width - 2 * x_border,
1112 /* default on no match */
1113 completion->priv->current_selected = -1;
1115 items = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
1119 gtk_widget_show (completion->priv->action_view);
1121 gtk_tree_view_column_cell_get_size (gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0),
1122 NULL, NULL, NULL, NULL,
1125 gtk_widget_set_size_request (completion->priv->action_view,
1126 completion->priv->entry->allocation.width - 2 * x_border,
1130 gtk_widget_hide (completion->priv->action_view);
1136 _gtk_entry_completion_popup (GtkEntryCompletion *completion)
1138 gint x, y, x_border, y_border;
1142 GdkRectangle monitor;
1143 GtkRequisition popup_req;
1144 GtkTreeViewColumn *column;
1147 if (GTK_WIDGET_MAPPED (completion->priv->popup_window))
1150 column = gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0);
1151 renderers = gtk_tree_view_column_get_cell_renderers (column);
1152 gtk_widget_ensure_style (completion->priv->tree_view);
1153 g_object_set (GTK_CELL_RENDERER (renderers->data), "cell_background_gdk",
1154 &completion->priv->tree_view->style->bg[GTK_STATE_NORMAL],
1156 g_list_free (renderers);
1158 gtk_widget_show_all (completion->priv->vbox);
1160 gdk_window_get_origin (completion->priv->entry->window, &x, &y);
1161 get_borders (GTK_ENTRY (completion->priv->entry), &x_border, &y_border);
1166 height = _gtk_entry_completion_resize_popup (completion);
1168 gtk_widget_size_request (completion->priv->popup_window, &popup_req);
1170 screen = gtk_widget_get_screen (GTK_WIDGET (completion->priv->entry));
1171 monitor_num = gdk_screen_get_monitor_at_window (screen,
1172 GTK_WIDGET (completion->priv->entry)->window);
1173 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1177 else if (x + popup_req.width > monitor.x + monitor.width)
1178 x = monitor.x + monitor.width - popup_req.width;
1180 if (y + height + popup_req.height <= monitor.y + monitor.height)
1183 y -= popup_req.height;
1185 gtk_window_move (GTK_WINDOW (completion->priv->popup_window), x, y);
1187 gtk_widget_show (completion->priv->popup_window);
1189 gtk_grab_add (completion->priv->popup_window);
1190 gdk_pointer_grab (completion->priv->popup_window->window, TRUE,
1191 GDK_BUTTON_PRESS_MASK |
1192 GDK_BUTTON_RELEASE_MASK |
1193 GDK_POINTER_MOTION_MASK,
1194 NULL, NULL, GDK_CURRENT_TIME);
1198 _gtk_entry_completion_popdown (GtkEntryCompletion *completion)
1200 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1201 gtk_grab_remove (completion->priv->popup_window);
1203 gtk_widget_hide (completion->priv->popup_window);