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 * SECTION:gtkentrycompletion
22 * @Short_description: Completion functionality for GtkEntry
23 * @Title: GtkEntryCompletion
25 * #GtkEntryCompletion is an auxiliary object to be used in conjunction with
26 * #GtkEntry to provide the completion functionality. It implements the
27 * #GtkCellLayout interface, to allow the user to add extra cells to the
28 * #GtkTreeView with completion matches.
30 * "Completion functionality" means that when the user modifies the text
31 * in the entry, #GtkEntryCompletion checks which rows in the model match
32 * the current content of the entry, and displays a list of matches.
33 * By default, the matching is done by comparing the entry text
34 * case-insensitively against the text column of the model (see
35 * gtk_entry_completion_set_text_column()), but this can be overridden
36 * with a custom match function (see gtk_entry_completion_set_match_func()).
38 * When the user selects a completion, the content of the entry is
39 * updated. By default, the content of the entry is replaced by the
40 * text column of the model, but this can be overridden by connecting
41 * to the #GtkEntryCompletion::match-selected signal and updating the
42 * entry in the signal handler. Note that you should return %TRUE from
43 * the signal handler to suppress the default behaviour.
45 * To add completion functionality to an entry, use gtk_entry_set_completion().
47 * In addition to regular completion matches, which will be inserted into the
48 * entry when they are selected, #GtkEntryCompletion also allows to display
49 * "actions" in the popup window. Their appearance is similar to menuitems,
50 * to differentiate them clearly from completion strings. When an action is
51 * selected, the #GtkEntryCompletion::action-activated signal is emitted.
53 * GtkEntryCompletion uses a #GtkTreeModelFilter model to represent the
54 * subset of the entire model that is currently matching. While the
55 * GtkEntryCompletion signals #GtkEntryCompletion::match-selected and
56 * #GtkEntryCompletion::cursor-on-match take the original model and an
57 * iter pointing to that model as arguments, other callbacks and signals
58 * (such as #GtkCellLayoutDataFuncs or #GtkCellArea::apply-attributes)
59 * will generally take the filter model as argument. As long as you are
60 * only calling gtk_tree_model_get(), this will make no difference to
61 * you. If for some reason, you need the original model, use
62 * gtk_tree_model_filter_get_model(). Don't forget to use
63 * gtk_tree_model_filter_convert_iter_to_child_iter() to obtain a
69 #include "gtkentrycompletion.h"
71 #include "gtkentryprivate.h"
72 #include "gtkcelllayout.h"
73 #include "gtkcellareabox.h"
76 #include "gtkcellrenderertext.h"
78 #include "gtktreeselection.h"
79 #include "gtktreeview.h"
80 #include "gtkscrolledwindow.h"
81 #include "gtksizerequest.h"
83 #include "gtkwindow.h"
85 #include "gtkmainprivate.h"
86 #include "gtkmarshalers.h"
88 #include "gtkprivate.h"
108 PROP_MINIMUM_KEY_LENGTH,
110 PROP_INLINE_COMPLETION,
111 PROP_POPUP_COMPLETION,
112 PROP_POPUP_SET_WIDTH,
113 PROP_POPUP_SINGLE_MATCH,
114 PROP_INLINE_SELECTION,
119 static void gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface);
120 static GtkCellArea* gtk_entry_completion_get_area (GtkCellLayout *cell_layout);
122 static GObject *gtk_entry_completion_constructor (GType type,
123 guint n_construct_properties,
124 GObjectConstructParam *construct_properties);
125 static void gtk_entry_completion_set_property (GObject *object,
129 static void gtk_entry_completion_get_property (GObject *object,
133 static void gtk_entry_completion_finalize (GObject *object);
134 static void gtk_entry_completion_dispose (GObject *object);
136 static gboolean gtk_entry_completion_visible_func (GtkTreeModel *model,
139 static gboolean gtk_entry_completion_popup_key_event (GtkWidget *widget,
142 static gboolean gtk_entry_completion_popup_button_press (GtkWidget *widget,
143 GdkEventButton *event,
145 static gboolean gtk_entry_completion_list_button_press (GtkWidget *widget,
146 GdkEventButton *event,
148 static gboolean gtk_entry_completion_action_button_press (GtkWidget *widget,
149 GdkEventButton *event,
151 static void gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
153 static gboolean gtk_entry_completion_list_enter_notify (GtkWidget *widget,
154 GdkEventCrossing *event,
156 static gboolean gtk_entry_completion_list_motion_notify (GtkWidget *widget,
157 GdkEventMotion *event,
159 static void gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
163 static void gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
164 GtkCellRenderer *cell,
169 static gboolean gtk_entry_completion_match_selected (GtkEntryCompletion *completion,
172 static gboolean gtk_entry_completion_real_insert_prefix (GtkEntryCompletion *completion,
173 const gchar *prefix);
174 static gboolean gtk_entry_completion_cursor_on_match (GtkEntryCompletion *completion,
177 static gboolean gtk_entry_completion_insert_completion (GtkEntryCompletion *completion,
180 static void gtk_entry_completion_insert_completion_text (GtkEntryCompletion *completion,
183 static guint entry_completion_signals[LAST_SIGNAL] = { 0 };
186 static void gtk_entry_completion_buildable_init (GtkBuildableIface *iface);
188 G_DEFINE_TYPE_WITH_CODE (GtkEntryCompletion, gtk_entry_completion, G_TYPE_OBJECT,
189 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
190 gtk_entry_completion_cell_layout_init)
191 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
192 gtk_entry_completion_buildable_init))
196 gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
198 GObjectClass *object_class;
200 object_class = (GObjectClass *)klass;
202 object_class->constructor = gtk_entry_completion_constructor;
203 object_class->set_property = gtk_entry_completion_set_property;
204 object_class->get_property = gtk_entry_completion_get_property;
205 object_class->dispose = gtk_entry_completion_dispose;
206 object_class->finalize = gtk_entry_completion_finalize;
208 klass->match_selected = gtk_entry_completion_match_selected;
209 klass->insert_prefix = gtk_entry_completion_real_insert_prefix;
210 klass->cursor_on_match = gtk_entry_completion_cursor_on_match;
213 * GtkEntryCompletion::insert-prefix:
214 * @widget: the object which received the signal
215 * @prefix: the common prefix of all possible completions
217 * Gets emitted when the inline autocompletion is triggered.
218 * The default behaviour is to make the entry display the
219 * whole prefix and select the newly inserted part.
221 * Applications may connect to this signal in order to insert only a
222 * smaller part of the @prefix into the entry - e.g. the entry used in
223 * the #GtkFileChooser inserts only the part of the prefix up to the
226 * Return value: %TRUE if the signal has been handled
230 entry_completion_signals[INSERT_PREFIX] =
231 g_signal_new (I_("insert-prefix"),
232 G_TYPE_FROM_CLASS (klass),
234 G_STRUCT_OFFSET (GtkEntryCompletionClass, insert_prefix),
235 _gtk_boolean_handled_accumulator, NULL,
236 _gtk_marshal_BOOLEAN__STRING,
241 * GtkEntryCompletion::match-selected:
242 * @widget: the object which received the signal
243 * @model: the #GtkTreeModel containing the matches
244 * @iter: a #GtkTreeIter positioned at the selected match
246 * Gets emitted when a match from the list is selected.
247 * The default behaviour is to replace the contents of the
248 * entry with the contents of the text column in the row
249 * pointed to by @iter.
251 * Note that @model is the model that was passed to
252 * gtk_entry_completion_set_model().
254 * Return value: %TRUE if the signal has been handled
258 entry_completion_signals[MATCH_SELECTED] =
259 g_signal_new (I_("match-selected"),
260 G_TYPE_FROM_CLASS (klass),
262 G_STRUCT_OFFSET (GtkEntryCompletionClass, match_selected),
263 _gtk_boolean_handled_accumulator, NULL,
264 _gtk_marshal_BOOLEAN__OBJECT_BOXED,
270 * GtkEntryCompletion::cursor-on-match:
271 * @widget: the object which received the signal
272 * @model: the #GtkTreeModel containing the matches
273 * @iter: a #GtkTreeIter positioned at the selected match
275 * Gets emitted when a match from the cursor is on a match
276 * of the list. The default behaviour is to replace the contents
277 * of the entry with the contents of the text column in the row
278 * pointed to by @iter.
280 * Note that @model is the model that was passed to
281 * gtk_entry_completion_set_model().
283 * Return value: %TRUE if the signal has been handled
287 entry_completion_signals[CURSOR_ON_MATCH] =
288 g_signal_new (I_("cursor-on-match"),
289 G_TYPE_FROM_CLASS (klass),
291 G_STRUCT_OFFSET (GtkEntryCompletionClass, cursor_on_match),
292 _gtk_boolean_handled_accumulator, NULL,
293 _gtk_marshal_BOOLEAN__OBJECT_BOXED,
299 * GtkEntryCompletion::action-activated:
300 * @widget: the object which received the signal
301 * @index: the index of the activated action
303 * Gets emitted when an action is activated.
307 entry_completion_signals[ACTION_ACTIVATED] =
308 g_signal_new (I_("action-activated"),
309 G_TYPE_FROM_CLASS (klass),
311 G_STRUCT_OFFSET (GtkEntryCompletionClass, action_activated),
313 _gtk_marshal_VOID__INT,
317 g_object_class_install_property (object_class,
319 g_param_spec_object ("model",
320 P_("Completion Model"),
321 P_("The model to find matches in"),
323 GTK_PARAM_READWRITE));
324 g_object_class_install_property (object_class,
325 PROP_MINIMUM_KEY_LENGTH,
326 g_param_spec_int ("minimum-key-length",
327 P_("Minimum Key Length"),
328 P_("Minimum length of the search key in order to look up matches"),
332 GTK_PARAM_READWRITE));
334 * GtkEntryCompletion:text-column:
336 * The column of the model containing the strings.
337 * Note that the strings must be UTF-8.
341 g_object_class_install_property (object_class,
343 g_param_spec_int ("text-column",
345 P_("The column of the model containing the strings."),
349 GTK_PARAM_READWRITE));
352 * GtkEntryCompletion:inline-completion:
354 * Determines whether the common prefix of the possible completions
355 * should be inserted automatically in the entry. Note that this
356 * requires text-column to be set, even if you are using a custom
361 g_object_class_install_property (object_class,
362 PROP_INLINE_COMPLETION,
363 g_param_spec_boolean ("inline-completion",
364 P_("Inline completion"),
365 P_("Whether the common prefix should be inserted automatically"),
367 GTK_PARAM_READWRITE));
369 * GtkEntryCompletion:popup-completion:
371 * Determines whether the possible completions should be
372 * shown in a popup window.
376 g_object_class_install_property (object_class,
377 PROP_POPUP_COMPLETION,
378 g_param_spec_boolean ("popup-completion",
379 P_("Popup completion"),
380 P_("Whether the completions should be shown in a popup window"),
382 GTK_PARAM_READWRITE));
385 * GtkEntryCompletion:popup-set-width:
387 * Determines whether the completions popup window will be
388 * resized to the width of the entry.
392 g_object_class_install_property (object_class,
393 PROP_POPUP_SET_WIDTH,
394 g_param_spec_boolean ("popup-set-width",
395 P_("Popup set width"),
396 P_("If TRUE, the popup window will have the same size as the entry"),
398 GTK_PARAM_READWRITE));
401 * GtkEntryCompletion:popup-single-match:
403 * Determines whether the completions popup window will shown
404 * for a single possible completion. You probably want to set
405 * this to %FALSE if you are using
406 * <link linkend="GtkEntryCompletion--inline-completion">inline
411 g_object_class_install_property (object_class,
412 PROP_POPUP_SINGLE_MATCH,
413 g_param_spec_boolean ("popup-single-match",
414 P_("Popup single match"),
415 P_("If TRUE, the popup window will appear for a single match."),
417 GTK_PARAM_READWRITE));
419 * GtkEntryCompletion:inline-selection:
421 * Determines whether the possible completions on the popup
422 * will appear in the entry as you navigate through them.
426 g_object_class_install_property (object_class,
427 PROP_INLINE_SELECTION,
428 g_param_spec_boolean ("inline-selection",
429 P_("Inline selection"),
430 P_("Your description here"),
432 GTK_PARAM_READWRITE));
436 * GtkEntryCompletion:cell-area:
438 * The #GtkCellArea used to layout cell renderers in the treeview column.
440 * If no area is specified when creating the entry completion with gtk_entry_completion_new_with_area()
441 * a horizontally oriented #GtkCellAreaBox will be used.
445 g_object_class_install_property (object_class,
447 g_param_spec_object ("cell-area",
449 P_("The GtkCellArea used to layout cells"),
451 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
453 g_type_class_add_private (object_class, sizeof (GtkEntryCompletionPrivate));
458 gtk_entry_completion_buildable_custom_tag_end (GtkBuildable *buildable,
461 const gchar *tagname,
464 /* Just ignore the boolean return from here */
465 _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname, data);
469 gtk_entry_completion_buildable_init (GtkBuildableIface *iface)
471 iface->add_child = _gtk_cell_layout_buildable_add_child;
472 iface->custom_tag_start = _gtk_cell_layout_buildable_custom_tag_start;
473 iface->custom_tag_end = gtk_entry_completion_buildable_custom_tag_end;
477 gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface)
479 iface->get_area = gtk_entry_completion_get_area;
483 gtk_entry_completion_init (GtkEntryCompletion *completion)
485 GtkEntryCompletionPrivate *priv;
487 /* yes, also priv, need to keep the code readable */
488 completion->priv = G_TYPE_INSTANCE_GET_PRIVATE (completion,
489 GTK_TYPE_ENTRY_COMPLETION,
490 GtkEntryCompletionPrivate);
491 priv = completion->priv;
493 priv->minimum_key_length = 1;
494 priv->text_column = -1;
495 priv->has_completion = FALSE;
496 priv->inline_completion = FALSE;
497 priv->popup_completion = TRUE;
498 priv->popup_set_width = TRUE;
499 priv->popup_single_match = TRUE;
500 priv->inline_selection = FALSE;
502 priv->filter_model = NULL;
506 gtk_entry_completion_constructor (GType type,
507 guint n_construct_properties,
508 GObjectConstructParam *construct_properties)
510 GtkEntryCompletion *completion;
511 GtkEntryCompletionPrivate *priv;
513 GtkCellRenderer *cell;
514 GtkTreeSelection *sel;
515 GtkWidget *popup_frame;
517 object = G_OBJECT_CLASS (gtk_entry_completion_parent_class)->constructor
518 (type, n_construct_properties, construct_properties);
520 completion = (GtkEntryCompletion *) object;
521 priv = completion->priv;
523 if (!priv->cell_area)
525 priv->cell_area = gtk_cell_area_box_new ();
526 g_object_ref_sink (priv->cell_area);
530 priv->tree_view = gtk_tree_view_new ();
531 g_signal_connect (priv->tree_view, "button-press-event",
532 G_CALLBACK (gtk_entry_completion_list_button_press),
534 g_signal_connect (priv->tree_view, "enter-notify-event",
535 G_CALLBACK (gtk_entry_completion_list_enter_notify),
537 g_signal_connect (priv->tree_view, "motion-notify-event",
538 G_CALLBACK (gtk_entry_completion_list_motion_notify),
541 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
542 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view), TRUE);
544 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
545 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
546 gtk_tree_selection_unselect_all (sel);
547 g_signal_connect (sel, "changed",
548 G_CALLBACK (gtk_entry_completion_selection_changed),
550 priv->first_sel_changed = TRUE;
552 priv->column = gtk_tree_view_column_new_with_area (priv->cell_area);
553 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
555 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
556 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
558 GTK_POLICY_AUTOMATIC);
559 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
562 /* a nasty hack to get the completions treeview to size nicely */
563 gtk_widget_set_size_request (gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (priv->scrolled_window)),
567 priv->actions = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_BOOLEAN);
570 gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->actions));
571 g_object_ref_sink (priv->action_view);
572 g_signal_connect (priv->action_view, "button-press-event",
573 G_CALLBACK (gtk_entry_completion_action_button_press),
575 g_signal_connect (priv->action_view, "enter-notify-event",
576 G_CALLBACK (gtk_entry_completion_list_enter_notify),
578 g_signal_connect (priv->action_view, "motion-notify-event",
579 G_CALLBACK (gtk_entry_completion_list_motion_notify),
581 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->action_view), FALSE);
582 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->action_view), TRUE);
584 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->action_view));
585 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
586 gtk_tree_selection_unselect_all (sel);
588 cell = gtk_cell_renderer_text_new ();
589 gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (priv->action_view),
592 gtk_entry_completion_action_data_func,
597 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
598 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
599 gtk_window_set_type_hint (GTK_WINDOW(priv->popup_window),
600 GDK_WINDOW_TYPE_HINT_COMBO);
601 g_signal_connect (priv->popup_window, "key-press-event",
602 G_CALLBACK (gtk_entry_completion_popup_key_event),
604 g_signal_connect (priv->popup_window, "key-release-event",
605 G_CALLBACK (gtk_entry_completion_popup_key_event),
607 g_signal_connect (priv->popup_window, "button-press-event",
608 G_CALLBACK (gtk_entry_completion_popup_button_press),
611 popup_frame = gtk_frame_new (NULL);
612 gtk_frame_set_shadow_type (GTK_FRAME (popup_frame),
613 GTK_SHADOW_ETCHED_IN);
614 gtk_widget_show (popup_frame);
615 gtk_container_add (GTK_CONTAINER (priv->popup_window), popup_frame);
617 priv->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
618 gtk_container_add (GTK_CONTAINER (popup_frame), priv->vbox);
620 gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->tree_view);
621 gtk_box_pack_start (GTK_BOX (priv->vbox), priv->scrolled_window,
624 /* we don't want to see the action treeview when no actions have
625 * been inserted, so we pack the action treeview after the first
626 * action has been added
634 gtk_entry_completion_set_property (GObject *object,
639 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
640 GtkEntryCompletionPrivate *priv = completion->priv;
646 gtk_entry_completion_set_model (completion,
647 g_value_get_object (value));
650 case PROP_MINIMUM_KEY_LENGTH:
651 gtk_entry_completion_set_minimum_key_length (completion,
652 g_value_get_int (value));
655 case PROP_TEXT_COLUMN:
656 priv->text_column = g_value_get_int (value);
659 case PROP_INLINE_COMPLETION:
660 priv->inline_completion = g_value_get_boolean (value);
663 case PROP_POPUP_COMPLETION:
664 priv->popup_completion = g_value_get_boolean (value);
667 case PROP_POPUP_SET_WIDTH:
668 priv->popup_set_width = g_value_get_boolean (value);
671 case PROP_POPUP_SINGLE_MATCH:
672 priv->popup_single_match = g_value_get_boolean (value);
675 case PROP_INLINE_SELECTION:
676 priv->inline_selection = g_value_get_boolean (value);
680 /* Construct-only, can only be assigned once */
681 area = g_value_get_object (value);
684 if (priv->cell_area != NULL)
686 g_warning ("cell-area has already been set, ignoring construct property");
687 g_object_ref_sink (area);
688 g_object_unref (area);
691 priv->cell_area = g_object_ref_sink (area);
696 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
702 gtk_entry_completion_get_property (GObject *object,
707 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
712 g_value_set_object (value,
713 gtk_entry_completion_get_model (completion));
716 case PROP_MINIMUM_KEY_LENGTH:
717 g_value_set_int (value, gtk_entry_completion_get_minimum_key_length (completion));
720 case PROP_TEXT_COLUMN:
721 g_value_set_int (value, gtk_entry_completion_get_text_column (completion));
724 case PROP_INLINE_COMPLETION:
725 g_value_set_boolean (value, gtk_entry_completion_get_inline_completion (completion));
728 case PROP_POPUP_COMPLETION:
729 g_value_set_boolean (value, gtk_entry_completion_get_popup_completion (completion));
732 case PROP_POPUP_SET_WIDTH:
733 g_value_set_boolean (value, gtk_entry_completion_get_popup_set_width (completion));
736 case PROP_POPUP_SINGLE_MATCH:
737 g_value_set_boolean (value, gtk_entry_completion_get_popup_single_match (completion));
740 case PROP_INLINE_SELECTION:
741 g_value_set_boolean (value, gtk_entry_completion_get_inline_selection (completion));
745 g_value_set_object (value, completion->priv->cell_area);
749 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
755 gtk_entry_completion_finalize (GObject *object)
757 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
758 GtkEntryCompletionPrivate *priv = completion->priv;
760 g_free (priv->case_normalized_key);
761 g_free (priv->completion_prefix);
763 if (priv->match_notify)
764 (* priv->match_notify) (priv->match_data);
766 G_OBJECT_CLASS (gtk_entry_completion_parent_class)->finalize (object);
770 gtk_entry_completion_dispose (GObject *object)
772 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
773 GtkEntryCompletionPrivate *priv = completion->priv;
777 gtk_widget_destroy (priv->tree_view);
778 priv->tree_view = NULL;
782 gtk_entry_set_completion (GTK_ENTRY (priv->entry), NULL);
786 g_object_unref (priv->actions);
787 priv->actions = NULL;
790 if (priv->action_view)
792 g_object_unref (priv->action_view);
793 priv->action_view = NULL;
796 if (priv->popup_window)
798 gtk_widget_destroy (priv->popup_window);
799 priv->popup_window = NULL;
804 g_object_unref (priv->cell_area);
805 priv->cell_area = NULL;
808 G_OBJECT_CLASS (gtk_entry_completion_parent_class)->dispose (object);
811 /* implement cell layout interface (only need to return the underlying cell area) */
813 gtk_entry_completion_get_area (GtkCellLayout *cell_layout)
815 GtkEntryCompletionPrivate *priv;
817 priv = GTK_ENTRY_COMPLETION (cell_layout)->priv;
819 if (G_UNLIKELY (!priv->cell_area))
821 priv->cell_area = gtk_cell_area_box_new ();
822 g_object_ref_sink (priv->cell_area);
825 return priv->cell_area;
828 /* all those callbacks */
830 gtk_entry_completion_default_completion_func (GtkEntryCompletion *completion,
836 gchar *normalized_string;
837 gchar *case_normalized_string;
839 gboolean ret = FALSE;
843 model = gtk_tree_model_filter_get_model (completion->priv->filter_model);
845 g_return_val_if_fail (gtk_tree_model_get_column_type (model, completion->priv->text_column) == G_TYPE_STRING,
848 gtk_tree_model_get (model, iter,
849 completion->priv->text_column, &item,
854 normalized_string = g_utf8_normalize (item, -1, G_NORMALIZE_ALL);
856 if (normalized_string != NULL)
858 case_normalized_string = g_utf8_casefold (normalized_string, -1);
860 if (!strncmp (key, case_normalized_string, strlen (key)))
863 g_free (case_normalized_string);
865 g_free (normalized_string);
873 gtk_entry_completion_visible_func (GtkTreeModel *model,
877 gboolean ret = FALSE;
879 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
881 if (!completion->priv->case_normalized_key)
884 if (completion->priv->match_func)
885 ret = (* completion->priv->match_func) (completion,
886 completion->priv->case_normalized_key,
888 completion->priv->match_data);
889 else if (completion->priv->text_column >= 0)
890 ret = gtk_entry_completion_default_completion_func (completion,
891 completion->priv->case_normalized_key,
899 gtk_entry_completion_popup_key_event (GtkWidget *widget,
903 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
905 if (!gtk_widget_get_mapped (completion->priv->popup_window))
908 /* propagate event to the entry */
909 gtk_widget_event (completion->priv->entry, (GdkEvent *)event);
915 gtk_entry_completion_popup_button_press (GtkWidget *widget,
916 GdkEventButton *event,
919 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
921 if (!gtk_widget_get_mapped (completion->priv->popup_window))
924 /* if we come here, it's usually time to popdown */
925 _gtk_entry_completion_popdown (completion);
931 gtk_entry_completion_list_button_press (GtkWidget *widget,
932 GdkEventButton *event,
935 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
936 GtkTreePath *path = NULL;
938 if (!gtk_widget_get_mapped (completion->priv->popup_window))
941 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
943 &path, NULL, NULL, NULL))
948 GtkTreeIter child_iter;
950 gtk_tree_model_get_iter (GTK_TREE_MODEL (completion->priv->filter_model),
952 gtk_tree_path_free (path);
953 gtk_tree_model_filter_convert_iter_to_child_iter (completion->priv->filter_model,
956 model = gtk_tree_model_filter_get_model (completion->priv->filter_model);
958 g_signal_handler_block (completion->priv->entry,
959 completion->priv->changed_id);
960 g_signal_emit (completion, entry_completion_signals[MATCH_SELECTED],
961 0, model, &child_iter, &entry_set);
962 g_signal_handler_unblock (completion->priv->entry,
963 completion->priv->changed_id);
965 _gtk_entry_completion_popdown (completion);
974 gtk_entry_completion_action_button_press (GtkWidget *widget,
975 GdkEventButton *event,
978 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
979 GtkTreePath *path = NULL;
981 if (!gtk_widget_get_mapped (completion->priv->popup_window))
984 _gtk_entry_reset_im_context (GTK_ENTRY (completion->priv->entry));
986 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
988 &path, NULL, NULL, NULL))
990 g_signal_emit (completion, entry_completion_signals[ACTION_ACTIVATED],
991 0, gtk_tree_path_get_indices (path)[0]);
992 gtk_tree_path_free (path);
994 _gtk_entry_completion_popdown (completion);
1002 gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
1003 GtkCellRenderer *cell,
1004 GtkTreeModel *model,
1008 gchar *string = NULL;
1011 gtk_tree_model_get (model, iter,
1034 gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
1037 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1039 if (completion->priv->first_sel_changed)
1041 completion->priv->first_sel_changed = FALSE;
1042 if (gtk_widget_is_focus (completion->priv->tree_view))
1043 gtk_tree_selection_unselect_all (selection);
1050 * gtk_entry_completion_new:
1052 * Creates a new #GtkEntryCompletion object.
1054 * Return value: A newly created #GtkEntryCompletion object
1058 GtkEntryCompletion *
1059 gtk_entry_completion_new (void)
1061 GtkEntryCompletion *completion;
1063 completion = g_object_new (GTK_TYPE_ENTRY_COMPLETION, NULL);
1069 * gtk_entry_completion_new_with_area:
1070 * @area: the #GtkCellArea used to layout cells
1072 * Creates a new #GtkEntryCompletion object using the
1073 * specified @area to layout cells in the underlying
1074 * #GtkTreeViewColumn for the drop-down menu.
1076 * Return value: A newly created #GtkEntryCompletion object
1080 GtkEntryCompletion *
1081 gtk_entry_completion_new_with_area (GtkCellArea *area)
1083 GtkEntryCompletion *completion;
1085 completion = g_object_new (GTK_TYPE_ENTRY_COMPLETION, "cell-area", area, NULL);
1091 * gtk_entry_completion_get_entry:
1092 * @completion: a #GtkEntryCompletion
1094 * Gets the entry @completion has been attached to.
1096 * Return value: (transfer none): The entry @completion has been attached to
1101 gtk_entry_completion_get_entry (GtkEntryCompletion *completion)
1103 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
1105 return completion->priv->entry;
1109 * gtk_entry_completion_set_model:
1110 * @completion: a #GtkEntryCompletion
1111 * @model: (allow-none): the #GtkTreeModel
1113 * Sets the model for a #GtkEntryCompletion. If @completion already has
1114 * a model set, it will remove it before setting the new model.
1115 * If model is %NULL, then it will unset the model.
1120 gtk_entry_completion_set_model (GtkEntryCompletion *completion,
1121 GtkTreeModel *model)
1123 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1124 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
1128 gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
1130 _gtk_entry_completion_popdown (completion);
1131 completion->priv->filter_model = NULL;
1135 /* code will unref the old filter model (if any) */
1136 completion->priv->filter_model =
1137 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
1138 gtk_tree_model_filter_set_visible_func (completion->priv->filter_model,
1139 gtk_entry_completion_visible_func,
1143 gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
1144 GTK_TREE_MODEL (completion->priv->filter_model));
1145 g_object_unref (completion->priv->filter_model);
1147 g_object_notify (G_OBJECT (completion), "model");
1149 if (gtk_widget_get_visible (completion->priv->popup_window))
1150 _gtk_entry_completion_resize_popup (completion);
1154 * gtk_entry_completion_get_model:
1155 * @completion: a #GtkEntryCompletion
1157 * Returns the model the #GtkEntryCompletion is using as data source.
1158 * Returns %NULL if the model is unset.
1160 * Return value: (transfer none): A #GtkTreeModel, or %NULL if none
1161 * is currently being used
1166 gtk_entry_completion_get_model (GtkEntryCompletion *completion)
1168 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
1170 if (!completion->priv->filter_model)
1173 return gtk_tree_model_filter_get_model (completion->priv->filter_model);
1177 * gtk_entry_completion_set_match_func:
1178 * @completion: a #GtkEntryCompletion
1179 * @func: the #GtkEntryCompletionMatchFunc to use
1180 * @func_data: user data for @func
1181 * @func_notify: destroy notify for @func_data.
1183 * Sets the match function for @completion to be @func. The match function
1184 * is used to determine if a row should or should not be in the completion
1190 gtk_entry_completion_set_match_func (GtkEntryCompletion *completion,
1191 GtkEntryCompletionMatchFunc func,
1193 GDestroyNotify func_notify)
1195 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1197 if (completion->priv->match_notify)
1198 (* completion->priv->match_notify) (completion->priv->match_data);
1200 completion->priv->match_func = func;
1201 completion->priv->match_data = func_data;
1202 completion->priv->match_notify = func_notify;
1206 * gtk_entry_completion_set_minimum_key_length:
1207 * @completion: a #GtkEntryCompletion
1208 * @length: the minimum length of the key in order to start completing
1210 * Requires the length of the search key for @completion to be at least
1211 * @length. This is useful for long lists, where completing using a small
1212 * key takes a lot of time and will come up with meaningless results anyway
1213 * (ie, a too large dataset).
1218 gtk_entry_completion_set_minimum_key_length (GtkEntryCompletion *completion,
1221 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1222 g_return_if_fail (length >= 0);
1224 if (completion->priv->minimum_key_length != length)
1226 completion->priv->minimum_key_length = length;
1228 g_object_notify (G_OBJECT (completion), "minimum-key-length");
1233 * gtk_entry_completion_get_minimum_key_length:
1234 * @completion: a #GtkEntryCompletion
1236 * Returns the minimum key length as set for @completion.
1238 * Return value: The currently used minimum key length
1243 gtk_entry_completion_get_minimum_key_length (GtkEntryCompletion *completion)
1245 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), 0);
1247 return completion->priv->minimum_key_length;
1251 * gtk_entry_completion_complete:
1252 * @completion: a #GtkEntryCompletion
1254 * Requests a completion operation, or in other words a refiltering of the
1255 * current list with completions, using the current key. The completion list
1256 * view will be updated accordingly.
1261 gtk_entry_completion_complete (GtkEntryCompletion *completion)
1265 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1266 g_return_if_fail (GTK_IS_ENTRY (completion->priv->entry));
1268 if (!completion->priv->filter_model)
1271 g_free (completion->priv->case_normalized_key);
1273 tmp = g_utf8_normalize (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)),
1274 -1, G_NORMALIZE_ALL);
1275 completion->priv->case_normalized_key = g_utf8_casefold (tmp, -1);
1278 gtk_tree_model_filter_refilter (completion->priv->filter_model);
1280 if (gtk_widget_get_visible (completion->priv->popup_window))
1281 _gtk_entry_completion_resize_popup (completion);
1285 gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
1287 const gchar *string,
1292 gtk_list_store_insert (completion->priv->actions, &iter, index);
1293 gtk_list_store_set (completion->priv->actions, &iter,
1298 if (!gtk_widget_get_parent (completion->priv->action_view))
1300 GtkTreePath *path = gtk_tree_path_new_from_indices (0, -1);
1302 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
1304 gtk_tree_path_free (path);
1306 gtk_box_pack_start (GTK_BOX (completion->priv->vbox),
1307 completion->priv->action_view, FALSE, FALSE, 0);
1308 gtk_widget_show (completion->priv->action_view);
1313 * gtk_entry_completion_insert_action_text:
1314 * @completion: a #GtkEntryCompletion
1315 * @index_: the index of the item to insert
1316 * @text: text of the item to insert
1318 * Inserts an action in @completion's action item list at position @index_
1319 * with text @text. If you want the action item to have markup, use
1320 * gtk_entry_completion_insert_action_markup().
1325 gtk_entry_completion_insert_action_text (GtkEntryCompletion *completion,
1329 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1330 g_return_if_fail (text != NULL);
1332 gtk_entry_completion_insert_action (completion, index_, text, FALSE);
1336 * gtk_entry_completion_insert_action_markup:
1337 * @completion: a #GtkEntryCompletion
1338 * @index_: the index of the item to insert
1339 * @markup: markup of the item to insert
1341 * Inserts an action in @completion's action item list at position @index_
1342 * with markup @markup.
1347 gtk_entry_completion_insert_action_markup (GtkEntryCompletion *completion,
1349 const gchar *markup)
1351 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1352 g_return_if_fail (markup != NULL);
1354 gtk_entry_completion_insert_action (completion, index_, markup, TRUE);
1358 * gtk_entry_completion_delete_action:
1359 * @completion: a #GtkEntryCompletion
1360 * @index_: the index of the item to delete
1362 * Deletes the action at @index_ from @completion's action list.
1367 gtk_entry_completion_delete_action (GtkEntryCompletion *completion,
1372 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1373 g_return_if_fail (index_ >= 0);
1375 gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (completion->priv->actions),
1376 &iter, NULL, index_);
1377 gtk_list_store_remove (completion->priv->actions, &iter);
1381 * gtk_entry_completion_set_text_column:
1382 * @completion: a #GtkEntryCompletion
1383 * @column: the column in the model of @completion to get strings from
1385 * Convenience function for setting up the most used case of this code: a
1386 * completion list with just strings. This function will set up @completion
1387 * to have a list displaying all (and just) strings in the completion list,
1388 * and to get those strings from @column in the model of @completion.
1390 * This functions creates and adds a #GtkCellRendererText for the selected
1391 * column. If you need to set the text column, but don't want the cell
1392 * renderer, use g_object_set() to set the #GtkEntryCompletion:text-column
1393 * property directly.
1398 gtk_entry_completion_set_text_column (GtkEntryCompletion *completion,
1401 GtkCellRenderer *cell;
1403 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1404 g_return_if_fail (column >= 0);
1406 completion->priv->text_column = column;
1408 cell = gtk_cell_renderer_text_new ();
1409 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion),
1411 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (completion),
1415 g_object_notify (G_OBJECT (completion), "text-column");
1419 * gtk_entry_completion_get_text_column:
1420 * @completion: a #GtkEntryCompletion
1422 * Returns the column in the model of @completion to get strings from.
1424 * Return value: the column containing the strings
1429 gtk_entry_completion_get_text_column (GtkEntryCompletion *completion)
1431 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), -1);
1433 return completion->priv->text_column;
1439 gtk_entry_completion_list_enter_notify (GtkWidget *widget,
1440 GdkEventCrossing *event,
1443 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1445 return completion->priv->ignore_enter;
1449 gtk_entry_completion_list_motion_notify (GtkWidget *widget,
1450 GdkEventMotion *event,
1453 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1455 completion->priv->ignore_enter = FALSE;
1461 /* some nasty size requisition */
1463 _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
1465 GtkAllocation allocation;
1467 gint matches, actions, items, height, x_border, y_border;
1470 gint vertical_separator;
1471 GdkRectangle monitor;
1473 GtkRequisition popup_req;
1474 GtkRequisition entry_req;
1478 GtkTreeViewColumn *action_column;
1481 window = gtk_widget_get_window (completion->priv->entry);
1486 gtk_widget_get_allocation (completion->priv->entry, &allocation);
1487 gtk_widget_get_preferred_size (completion->priv->entry,
1490 gdk_window_get_origin (window, &x, &y);
1492 y += allocation.y + (allocation.height - entry_req.height) / 2;
1494 _gtk_entry_get_borders (GTK_ENTRY (completion->priv->entry), &x_border, &y_border);
1496 matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
1497 actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
1498 action_column = gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0);
1500 gtk_tree_view_column_cell_get_size (completion->priv->column, NULL,
1501 NULL, NULL, NULL, &height);
1502 gtk_tree_view_column_cell_get_size (action_column, NULL,
1503 NULL, NULL, NULL, &action_height);
1505 gtk_widget_style_get (GTK_WIDGET (completion->priv->tree_view),
1506 "vertical-separator", &vertical_separator,
1509 height += vertical_separator;
1511 gtk_widget_realize (completion->priv->tree_view);
1513 screen = gtk_widget_get_screen (GTK_WIDGET (completion->priv->entry));
1514 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
1515 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1519 if (y > monitor.height / 2)
1520 items = MIN (matches, (((monitor.y + y) - (actions * action_height)) / height) - 1);
1522 items = MIN (matches, (((monitor.height - y) - (actions * action_height)) / height) - 1);
1525 gtk_widget_hide (completion->priv->scrolled_window);
1527 gtk_widget_show (completion->priv->scrolled_window);
1529 if (completion->priv->popup_set_width)
1530 width = MIN (allocation.width, monitor.width) - 2 * x_border;
1534 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (completion->priv->tree_view));
1535 gtk_scrolled_window_set_min_content_width (GTK_SCROLLED_WINDOW (completion->priv->scrolled_window), width);
1536 gtk_widget_set_size_request (completion->priv->scrolled_window, width, -1);
1537 gtk_scrolled_window_set_min_content_height (GTK_SCROLLED_WINDOW (completion->priv->scrolled_window), items * height);
1541 gtk_widget_show (completion->priv->action_view);
1542 gtk_widget_set_size_request (completion->priv->action_view, width, -1);
1545 gtk_widget_hide (completion->priv->action_view);
1547 gtk_widget_get_preferred_size (completion->priv->popup_window,
1552 else if (x + popup_req.width > monitor.x + monitor.width)
1553 x = monitor.x + monitor.width - popup_req.width;
1555 if (y + entry_req.height + popup_req.height <= monitor.y + monitor.height ||
1556 y - monitor.y < (monitor.y + monitor.height) - (y + entry_req.height))
1558 y += entry_req.height;
1563 y -= popup_req.height;
1569 path = gtk_tree_path_new_from_indices (above ? matches - 1 : 0, -1);
1570 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (completion->priv->tree_view), path,
1571 NULL, FALSE, 0.0, 0.0);
1572 gtk_tree_path_free (path);
1575 gtk_window_move (GTK_WINDOW (completion->priv->popup_window), x, y);
1581 _gtk_entry_completion_popup (GtkEntryCompletion *completion,
1584 GtkTreeViewColumn *column;
1585 GtkStyleContext *context;
1588 GtkWidget *toplevel;
1590 if (gtk_widget_get_mapped (completion->priv->popup_window))
1593 if (!gtk_widget_get_mapped (completion->priv->entry))
1596 if (!gtk_widget_has_focus (completion->priv->entry))
1599 if (completion->priv->grab_device)
1602 completion->priv->ignore_enter = TRUE;
1604 column = gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0);
1605 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
1607 context = gtk_widget_get_style_context (completion->priv->tree_view);
1608 gtk_style_context_get_background_color (context, 0, &color);
1610 g_object_set (GTK_CELL_RENDERER (renderers->data),
1611 "cell-background-rgba", &color,
1613 g_list_free (renderers);
1615 gtk_widget_show_all (completion->priv->vbox);
1617 /* default on no match */
1618 completion->priv->current_selected = -1;
1620 _gtk_entry_completion_resize_popup (completion);
1622 toplevel = gtk_widget_get_toplevel (completion->priv->entry);
1623 if (GTK_IS_WINDOW (toplevel))
1624 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1625 GTK_WINDOW (completion->priv->popup_window));
1627 /* prevent the first row being focused */
1628 gtk_widget_grab_focus (completion->priv->tree_view);
1630 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
1631 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
1633 gtk_window_set_screen (GTK_WINDOW (completion->priv->popup_window),
1634 gtk_widget_get_screen (completion->priv->entry));
1636 gtk_widget_show (completion->priv->popup_window);
1638 gtk_device_grab_add (completion->priv->popup_window, device, TRUE);
1639 gdk_device_grab (device, gtk_widget_get_window (completion->priv->popup_window),
1640 GDK_OWNERSHIP_WINDOW, TRUE,
1641 GDK_BUTTON_PRESS_MASK |
1642 GDK_BUTTON_RELEASE_MASK |
1643 GDK_POINTER_MOTION_MASK,
1644 NULL, GDK_CURRENT_TIME);
1646 completion->priv->grab_device = device;
1650 _gtk_entry_completion_popdown (GtkEntryCompletion *completion)
1652 if (!gtk_widget_get_mapped (completion->priv->popup_window))
1655 completion->priv->ignore_enter = FALSE;
1657 if (completion->priv->grab_device)
1659 gdk_device_ungrab (completion->priv->grab_device, GDK_CURRENT_TIME);
1660 gtk_device_grab_remove (completion->priv->popup_window,
1661 completion->priv->grab_device);
1662 completion->priv->grab_device = NULL;
1665 gtk_widget_hide (completion->priv->popup_window);
1669 gtk_entry_completion_match_selected (GtkEntryCompletion *completion,
1670 GtkTreeModel *model,
1675 gtk_tree_model_get (model, iter, completion->priv->text_column, &str, -1);
1676 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), str ? str : "");
1678 /* move cursor to the end */
1679 gtk_editable_set_position (GTK_EDITABLE (completion->priv->entry), -1);
1687 gtk_entry_completion_cursor_on_match (GtkEntryCompletion *completion,
1688 GtkTreeModel *model,
1691 gtk_entry_completion_insert_completion (completion, model, iter);
1697 gtk_entry_completion_compute_prefix (GtkEntryCompletion *completion)
1700 gchar *prefix = NULL;
1704 if (completion->priv->text_column < 0)
1707 key = gtk_entry_get_text (GTK_ENTRY (completion->priv->entry));
1709 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (completion->priv->filter_model),
1716 gtk_tree_model_get (GTK_TREE_MODEL (completion->priv->filter_model),
1717 &iter, completion->priv->text_column, &text,
1720 if (text && g_str_has_prefix (text, key))
1723 prefix = g_strdup (text);
1729 while (*p && *p == *q)
1739 /* strip a partial multibyte character */
1740 q = g_utf8_find_prev_char (prefix, p);
1741 switch (g_utf8_get_char_validated (q, p - q))
1753 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (completion->priv->filter_model),
1762 gtk_entry_completion_real_insert_prefix (GtkEntryCompletion *completion,
1763 const gchar *prefix)
1771 prefix_len = g_utf8_strlen (prefix, -1);
1773 key = gtk_entry_get_text (GTK_ENTRY (completion->priv->entry));
1774 key_len = g_utf8_strlen (key, -1);
1776 if (prefix_len > key_len)
1778 gint pos = prefix_len;
1780 gtk_editable_insert_text (GTK_EDITABLE (completion->priv->entry),
1781 prefix + strlen (key), -1, &pos);
1782 gtk_editable_select_region (GTK_EDITABLE (completion->priv->entry),
1783 key_len, prefix_len);
1785 completion->priv->has_completion = TRUE;
1793 * gtk_entry_completion_get_completion_prefix:
1794 * @completion: a #GtkEntryCompletion
1796 * Get the original text entered by the user that triggered
1797 * the completion or %NULL if there's no completion ongoing.
1799 * Returns: the prefix for the current completion
1804 gtk_entry_completion_get_completion_prefix (GtkEntryCompletion *completion)
1806 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
1808 return completion->priv->completion_prefix;
1812 gtk_entry_completion_insert_completion_text (GtkEntryCompletion *completion,
1815 GtkEntryCompletionPrivate *priv = completion->priv;
1818 priv = completion->priv;
1820 if (priv->changed_id > 0)
1821 g_signal_handler_block (priv->entry, priv->changed_id);
1823 if (priv->insert_text_id > 0)
1824 g_signal_handler_block (priv->entry, priv->insert_text_id);
1826 gtk_entry_set_text (GTK_ENTRY (priv->entry), text);
1828 len = strlen (priv->completion_prefix);
1829 gtk_editable_select_region (GTK_EDITABLE (priv->entry), len, -1);
1831 if (priv->changed_id > 0)
1832 g_signal_handler_unblock (priv->entry, priv->changed_id);
1834 if (priv->insert_text_id > 0)
1835 g_signal_handler_unblock (priv->entry, priv->insert_text_id);
1839 gtk_entry_completion_insert_completion (GtkEntryCompletion *completion,
1840 GtkTreeModel *model,
1845 if (completion->priv->text_column < 0)
1848 gtk_tree_model_get (model, iter,
1849 completion->priv->text_column, &str,
1852 gtk_entry_completion_insert_completion_text (completion, str);
1860 * gtk_entry_completion_insert_prefix:
1861 * @completion: a #GtkEntryCompletion
1863 * Requests a prefix insertion.
1868 gtk_entry_completion_insert_prefix (GtkEntryCompletion *completion)
1873 if (completion->priv->insert_text_id > 0)
1874 g_signal_handler_block (completion->priv->entry,
1875 completion->priv->insert_text_id);
1877 prefix = gtk_entry_completion_compute_prefix (completion);
1880 g_signal_emit (completion, entry_completion_signals[INSERT_PREFIX],
1885 if (completion->priv->insert_text_id > 0)
1886 g_signal_handler_unblock (completion->priv->entry,
1887 completion->priv->insert_text_id);
1891 * gtk_entry_completion_set_inline_completion:
1892 * @completion: a #GtkEntryCompletion
1893 * @inline_completion: %TRUE to do inline completion
1895 * Sets whether the common prefix of the possible completions should
1896 * be automatically inserted in the entry.
1901 gtk_entry_completion_set_inline_completion (GtkEntryCompletion *completion,
1902 gboolean inline_completion)
1904 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1906 inline_completion = inline_completion != FALSE;
1908 if (completion->priv->inline_completion != inline_completion)
1910 completion->priv->inline_completion = inline_completion;
1912 g_object_notify (G_OBJECT (completion), "inline-completion");
1917 * gtk_entry_completion_get_inline_completion:
1918 * @completion: a #GtkEntryCompletion
1920 * Returns whether the common prefix of the possible completions should
1921 * be automatically inserted in the entry.
1923 * Return value: %TRUE if inline completion is turned on
1928 gtk_entry_completion_get_inline_completion (GtkEntryCompletion *completion)
1930 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), FALSE);
1932 return completion->priv->inline_completion;
1936 * gtk_entry_completion_set_popup_completion:
1937 * @completion: a #GtkEntryCompletion
1938 * @popup_completion: %TRUE to do popup completion
1940 * Sets whether the completions should be presented in a popup window.
1945 gtk_entry_completion_set_popup_completion (GtkEntryCompletion *completion,
1946 gboolean popup_completion)
1948 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1950 popup_completion = popup_completion != FALSE;
1952 if (completion->priv->popup_completion != popup_completion)
1954 completion->priv->popup_completion = popup_completion;
1956 g_object_notify (G_OBJECT (completion), "popup-completion");
1962 * gtk_entry_completion_get_popup_completion:
1963 * @completion: a #GtkEntryCompletion
1965 * Returns whether the completions should be presented in a popup window.
1967 * Return value: %TRUE if popup completion is turned on
1972 gtk_entry_completion_get_popup_completion (GtkEntryCompletion *completion)
1974 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), TRUE);
1976 return completion->priv->popup_completion;
1980 * gtk_entry_completion_set_popup_set_width:
1981 * @completion: a #GtkEntryCompletion
1982 * @popup_set_width: %TRUE to make the width of the popup the same as the entry
1984 * Sets whether the completion popup window will be resized to be the same
1985 * width as the entry.
1990 gtk_entry_completion_set_popup_set_width (GtkEntryCompletion *completion,
1991 gboolean popup_set_width)
1993 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1995 popup_set_width = popup_set_width != FALSE;
1997 if (completion->priv->popup_set_width != popup_set_width)
1999 completion->priv->popup_set_width = popup_set_width;
2001 g_object_notify (G_OBJECT (completion), "popup-set-width");
2006 * gtk_entry_completion_get_popup_set_width:
2007 * @completion: a #GtkEntryCompletion
2009 * Returns whether the completion popup window will be resized to the
2010 * width of the entry.
2012 * Return value: %TRUE if the popup window will be resized to the width of
2018 gtk_entry_completion_get_popup_set_width (GtkEntryCompletion *completion)
2020 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), TRUE);
2022 return completion->priv->popup_set_width;
2027 * gtk_entry_completion_set_popup_single_match:
2028 * @completion: a #GtkEntryCompletion
2029 * @popup_single_match: %TRUE if the popup should appear even for a single
2032 * Sets whether the completion popup window will appear even if there is
2033 * only a single match. You may want to set this to %FALSE if you
2034 * are using <link linkend="GtkEntryCompletion--inline-completion">inline
2035 * completion</link>.
2040 gtk_entry_completion_set_popup_single_match (GtkEntryCompletion *completion,
2041 gboolean popup_single_match)
2043 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
2045 popup_single_match = popup_single_match != FALSE;
2047 if (completion->priv->popup_single_match != popup_single_match)
2049 completion->priv->popup_single_match = popup_single_match;
2051 g_object_notify (G_OBJECT (completion), "popup-single-match");
2056 * gtk_entry_completion_get_popup_single_match:
2057 * @completion: a #GtkEntryCompletion
2059 * Returns whether the completion popup window will appear even if there is
2060 * only a single match.
2062 * Return value: %TRUE if the popup window will appear regardless of the
2068 gtk_entry_completion_get_popup_single_match (GtkEntryCompletion *completion)
2070 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), TRUE);
2072 return completion->priv->popup_single_match;
2076 * gtk_entry_completion_set_inline_selection:
2077 * @completion: a #GtkEntryCompletion
2078 * @inline_selection: %TRUE to do inline selection
2080 * Sets whether it is possible to cycle through the possible completions
2086 gtk_entry_completion_set_inline_selection (GtkEntryCompletion *completion,
2087 gboolean inline_selection)
2089 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
2091 inline_selection = inline_selection != FALSE;
2093 if (completion->priv->inline_selection != inline_selection)
2095 completion->priv->inline_selection = inline_selection;
2097 g_object_notify (G_OBJECT (completion), "inline-selection");
2102 * gtk_entry_completion_get_inline_selection:
2103 * @completion: a #GtkEntryCompletion
2105 * Returns %TRUE if inline-selection mode is turned on.
2107 * Returns: %TRUE if inline-selection mode is on
2112 gtk_entry_completion_get_inline_selection (GtkEntryCompletion *completion)
2114 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), FALSE);
2116 return completion->priv->inline_selection;