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, see <http://www.gnu.org/licenses/>.
19 * SECTION:gtkentrycompletion
20 * @Short_description: Completion functionality for GtkEntry
21 * @Title: GtkEntryCompletion
23 * #GtkEntryCompletion is an auxiliary object to be used in conjunction with
24 * #GtkEntry to provide the completion functionality. It implements the
25 * #GtkCellLayout interface, to allow the user to add extra cells to the
26 * #GtkTreeView with completion matches.
28 * "Completion functionality" means that when the user modifies the text
29 * in the entry, #GtkEntryCompletion checks which rows in the model match
30 * the current content of the entry, and displays a list of matches.
31 * By default, the matching is done by comparing the entry text
32 * case-insensitively against the text column of the model (see
33 * gtk_entry_completion_set_text_column()), but this can be overridden
34 * with a custom match function (see gtk_entry_completion_set_match_func()).
36 * When the user selects a completion, the content of the entry is
37 * updated. By default, the content of the entry is replaced by the
38 * text column of the model, but this can be overridden by connecting
39 * to the #GtkEntryCompletion::match-selected signal and updating the
40 * entry in the signal handler. Note that you should return %TRUE from
41 * the signal handler to suppress the default behaviour.
43 * To add completion functionality to an entry, use gtk_entry_set_completion().
45 * In addition to regular completion matches, which will be inserted into the
46 * entry when they are selected, #GtkEntryCompletion also allows to display
47 * "actions" in the popup window. Their appearance is similar to menuitems,
48 * to differentiate them clearly from completion strings. When an action is
49 * selected, the #GtkEntryCompletion::action-activated signal is emitted.
51 * GtkEntryCompletion uses a #GtkTreeModelFilter model to represent the
52 * subset of the entire model that is currently matching. While the
53 * GtkEntryCompletion signals #GtkEntryCompletion::match-selected and
54 * #GtkEntryCompletion::cursor-on-match take the original model and an
55 * iter pointing to that model as arguments, other callbacks and signals
56 * (such as #GtkCellLayoutDataFuncs or #GtkCellArea::apply-attributes)
57 * will generally take the filter model as argument. As long as you are
58 * only calling gtk_tree_model_get(), this will make no difference to
59 * you. If for some reason, you need the original model, use
60 * gtk_tree_model_filter_get_model(). Don't forget to use
61 * gtk_tree_model_filter_convert_iter_to_child_iter() to obtain a
67 #include "gtkentrycompletion.h"
69 #include "gtkentryprivate.h"
70 #include "gtkcelllayout.h"
71 #include "gtkcellareabox.h"
74 #include "gtkcellrenderertext.h"
76 #include "gtktreeselection.h"
77 #include "gtktreeview.h"
78 #include "gtkscrolledwindow.h"
79 #include "gtksizerequest.h"
81 #include "gtkwindow.h"
84 #include "gtkmarshalers.h"
86 #include "gtkprivate.h"
106 PROP_MINIMUM_KEY_LENGTH,
108 PROP_INLINE_COMPLETION,
109 PROP_POPUP_COMPLETION,
110 PROP_POPUP_SET_WIDTH,
111 PROP_POPUP_SINGLE_MATCH,
112 PROP_INLINE_SELECTION,
117 static void gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface);
118 static GtkCellArea* gtk_entry_completion_get_area (GtkCellLayout *cell_layout);
120 static GObject *gtk_entry_completion_constructor (GType type,
121 guint n_construct_properties,
122 GObjectConstructParam *construct_properties);
123 static void gtk_entry_completion_set_property (GObject *object,
127 static void gtk_entry_completion_get_property (GObject *object,
131 static void gtk_entry_completion_finalize (GObject *object);
132 static void gtk_entry_completion_dispose (GObject *object);
134 static gboolean gtk_entry_completion_visible_func (GtkTreeModel *model,
137 static gboolean gtk_entry_completion_popup_key_event (GtkWidget *widget,
140 static gboolean gtk_entry_completion_popup_button_press (GtkWidget *widget,
141 GdkEventButton *event,
143 static gboolean gtk_entry_completion_list_button_press (GtkWidget *widget,
144 GdkEventButton *event,
146 static gboolean gtk_entry_completion_action_button_press (GtkWidget *widget,
147 GdkEventButton *event,
149 static void gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
151 static gboolean gtk_entry_completion_list_enter_notify (GtkWidget *widget,
152 GdkEventCrossing *event,
154 static gboolean gtk_entry_completion_list_motion_notify (GtkWidget *widget,
155 GdkEventMotion *event,
157 static void gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
161 static void gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
162 GtkCellRenderer *cell,
167 static gboolean gtk_entry_completion_match_selected (GtkEntryCompletion *completion,
170 static gboolean gtk_entry_completion_real_insert_prefix (GtkEntryCompletion *completion,
171 const gchar *prefix);
172 static gboolean gtk_entry_completion_cursor_on_match (GtkEntryCompletion *completion,
175 static gboolean gtk_entry_completion_insert_completion (GtkEntryCompletion *completion,
178 static void gtk_entry_completion_insert_completion_text (GtkEntryCompletion *completion,
181 static guint entry_completion_signals[LAST_SIGNAL] = { 0 };
184 static void gtk_entry_completion_buildable_init (GtkBuildableIface *iface);
186 G_DEFINE_TYPE_WITH_CODE (GtkEntryCompletion, gtk_entry_completion, G_TYPE_OBJECT,
187 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
188 gtk_entry_completion_cell_layout_init)
189 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
190 gtk_entry_completion_buildable_init))
194 gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
196 GObjectClass *object_class;
198 object_class = (GObjectClass *)klass;
200 object_class->constructor = gtk_entry_completion_constructor;
201 object_class->set_property = gtk_entry_completion_set_property;
202 object_class->get_property = gtk_entry_completion_get_property;
203 object_class->dispose = gtk_entry_completion_dispose;
204 object_class->finalize = gtk_entry_completion_finalize;
206 klass->match_selected = gtk_entry_completion_match_selected;
207 klass->insert_prefix = gtk_entry_completion_real_insert_prefix;
208 klass->cursor_on_match = gtk_entry_completion_cursor_on_match;
211 * GtkEntryCompletion::insert-prefix:
212 * @widget: the object which received the signal
213 * @prefix: the common prefix of all possible completions
215 * Gets emitted when the inline autocompletion is triggered.
216 * The default behaviour is to make the entry display the
217 * whole prefix and select the newly inserted part.
219 * Applications may connect to this signal in order to insert only a
220 * smaller part of the @prefix into the entry - e.g. the entry used in
221 * the #GtkFileChooser inserts only the part of the prefix up to the
224 * Return value: %TRUE if the signal has been handled
228 entry_completion_signals[INSERT_PREFIX] =
229 g_signal_new (I_("insert-prefix"),
230 G_TYPE_FROM_CLASS (klass),
232 G_STRUCT_OFFSET (GtkEntryCompletionClass, insert_prefix),
233 _gtk_boolean_handled_accumulator, NULL,
234 _gtk_marshal_BOOLEAN__STRING,
239 * GtkEntryCompletion::match-selected:
240 * @widget: the object which received the signal
241 * @model: the #GtkTreeModel containing the matches
242 * @iter: a #GtkTreeIter positioned at the selected match
244 * Gets emitted when a match from the list is selected.
245 * The default behaviour is to replace the contents of the
246 * entry with the contents of the text column in the row
247 * pointed to by @iter.
249 * Note that @model is the model that was passed to
250 * gtk_entry_completion_set_model().
252 * Return value: %TRUE if the signal has been handled
256 entry_completion_signals[MATCH_SELECTED] =
257 g_signal_new (I_("match-selected"),
258 G_TYPE_FROM_CLASS (klass),
260 G_STRUCT_OFFSET (GtkEntryCompletionClass, match_selected),
261 _gtk_boolean_handled_accumulator, NULL,
262 _gtk_marshal_BOOLEAN__OBJECT_BOXED,
268 * GtkEntryCompletion::cursor-on-match:
269 * @widget: the object which received the signal
270 * @model: the #GtkTreeModel containing the matches
271 * @iter: a #GtkTreeIter positioned at the selected match
273 * Gets emitted when a match from the cursor is on a match
274 * of the list. The default behaviour is to replace the contents
275 * of the entry with the contents of the text column in the row
276 * pointed to by @iter.
278 * Note that @model is the model that was passed to
279 * gtk_entry_completion_set_model().
281 * Return value: %TRUE if the signal has been handled
285 entry_completion_signals[CURSOR_ON_MATCH] =
286 g_signal_new (I_("cursor-on-match"),
287 G_TYPE_FROM_CLASS (klass),
289 G_STRUCT_OFFSET (GtkEntryCompletionClass, cursor_on_match),
290 _gtk_boolean_handled_accumulator, NULL,
291 _gtk_marshal_BOOLEAN__OBJECT_BOXED,
297 * GtkEntryCompletion::action-activated:
298 * @widget: the object which received the signal
299 * @index: the index of the activated action
301 * Gets emitted when an action is activated.
305 entry_completion_signals[ACTION_ACTIVATED] =
306 g_signal_new (I_("action-activated"),
307 G_TYPE_FROM_CLASS (klass),
309 G_STRUCT_OFFSET (GtkEntryCompletionClass, action_activated),
311 _gtk_marshal_VOID__INT,
315 g_object_class_install_property (object_class,
317 g_param_spec_object ("model",
318 P_("Completion Model"),
319 P_("The model to find matches in"),
321 GTK_PARAM_READWRITE));
322 g_object_class_install_property (object_class,
323 PROP_MINIMUM_KEY_LENGTH,
324 g_param_spec_int ("minimum-key-length",
325 P_("Minimum Key Length"),
326 P_("Minimum length of the search key in order to look up matches"),
330 GTK_PARAM_READWRITE));
332 * GtkEntryCompletion:text-column:
334 * The column of the model containing the strings.
335 * Note that the strings must be UTF-8.
339 g_object_class_install_property (object_class,
341 g_param_spec_int ("text-column",
343 P_("The column of the model containing the strings."),
347 GTK_PARAM_READWRITE));
350 * GtkEntryCompletion:inline-completion:
352 * Determines whether the common prefix of the possible completions
353 * should be inserted automatically in the entry. Note that this
354 * requires text-column to be set, even if you are using a custom
359 g_object_class_install_property (object_class,
360 PROP_INLINE_COMPLETION,
361 g_param_spec_boolean ("inline-completion",
362 P_("Inline completion"),
363 P_("Whether the common prefix should be inserted automatically"),
365 GTK_PARAM_READWRITE));
367 * GtkEntryCompletion:popup-completion:
369 * Determines whether the possible completions should be
370 * shown in a popup window.
374 g_object_class_install_property (object_class,
375 PROP_POPUP_COMPLETION,
376 g_param_spec_boolean ("popup-completion",
377 P_("Popup completion"),
378 P_("Whether the completions should be shown in a popup window"),
380 GTK_PARAM_READWRITE));
383 * GtkEntryCompletion:popup-set-width:
385 * Determines whether the completions popup window will be
386 * resized to the width of the entry.
390 g_object_class_install_property (object_class,
391 PROP_POPUP_SET_WIDTH,
392 g_param_spec_boolean ("popup-set-width",
393 P_("Popup set width"),
394 P_("If TRUE, the popup window will have the same size as the entry"),
396 GTK_PARAM_READWRITE));
399 * GtkEntryCompletion:popup-single-match:
401 * Determines whether the completions popup window will shown
402 * for a single possible completion. You probably want to set
403 * this to %FALSE if you are using
404 * <link linkend="GtkEntryCompletion--inline-completion">inline
409 g_object_class_install_property (object_class,
410 PROP_POPUP_SINGLE_MATCH,
411 g_param_spec_boolean ("popup-single-match",
412 P_("Popup single match"),
413 P_("If TRUE, the popup window will appear for a single match."),
415 GTK_PARAM_READWRITE));
417 * GtkEntryCompletion:inline-selection:
419 * Determines whether the possible completions on the popup
420 * will appear in the entry as you navigate through them.
424 g_object_class_install_property (object_class,
425 PROP_INLINE_SELECTION,
426 g_param_spec_boolean ("inline-selection",
427 P_("Inline selection"),
428 P_("Your description here"),
430 GTK_PARAM_READWRITE));
434 * GtkEntryCompletion:cell-area:
436 * The #GtkCellArea used to layout cell renderers in the treeview column.
438 * If no area is specified when creating the entry completion with gtk_entry_completion_new_with_area()
439 * a horizontally oriented #GtkCellAreaBox will be used.
443 g_object_class_install_property (object_class,
445 g_param_spec_object ("cell-area",
447 P_("The GtkCellArea used to layout cells"),
449 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
451 g_type_class_add_private (object_class, sizeof (GtkEntryCompletionPrivate));
456 gtk_entry_completion_buildable_custom_tag_end (GtkBuildable *buildable,
459 const gchar *tagname,
462 /* Just ignore the boolean return from here */
463 _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname, data);
467 gtk_entry_completion_buildable_init (GtkBuildableIface *iface)
469 iface->add_child = _gtk_cell_layout_buildable_add_child;
470 iface->custom_tag_start = _gtk_cell_layout_buildable_custom_tag_start;
471 iface->custom_tag_end = gtk_entry_completion_buildable_custom_tag_end;
475 gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface)
477 iface->get_area = gtk_entry_completion_get_area;
481 gtk_entry_completion_init (GtkEntryCompletion *completion)
483 GtkEntryCompletionPrivate *priv;
485 /* yes, also priv, need to keep the code readable */
486 completion->priv = G_TYPE_INSTANCE_GET_PRIVATE (completion,
487 GTK_TYPE_ENTRY_COMPLETION,
488 GtkEntryCompletionPrivate);
489 priv = completion->priv;
491 priv->minimum_key_length = 1;
492 priv->text_column = -1;
493 priv->has_completion = FALSE;
494 priv->inline_completion = FALSE;
495 priv->popup_completion = TRUE;
496 priv->popup_set_width = TRUE;
497 priv->popup_single_match = TRUE;
498 priv->inline_selection = FALSE;
500 priv->filter_model = NULL;
504 gtk_entry_completion_constructor (GType type,
505 guint n_construct_properties,
506 GObjectConstructParam *construct_properties)
508 GtkEntryCompletion *completion;
509 GtkEntryCompletionPrivate *priv;
511 GtkCellRenderer *cell;
512 GtkTreeSelection *sel;
513 GtkWidget *popup_frame;
515 object = G_OBJECT_CLASS (gtk_entry_completion_parent_class)->constructor
516 (type, n_construct_properties, construct_properties);
518 completion = (GtkEntryCompletion *) object;
519 priv = completion->priv;
521 if (!priv->cell_area)
523 priv->cell_area = gtk_cell_area_box_new ();
524 g_object_ref_sink (priv->cell_area);
528 priv->tree_view = gtk_tree_view_new ();
529 g_signal_connect (priv->tree_view, "button-press-event",
530 G_CALLBACK (gtk_entry_completion_list_button_press),
532 g_signal_connect (priv->tree_view, "enter-notify-event",
533 G_CALLBACK (gtk_entry_completion_list_enter_notify),
535 g_signal_connect (priv->tree_view, "motion-notify-event",
536 G_CALLBACK (gtk_entry_completion_list_motion_notify),
539 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
540 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view), TRUE);
542 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
543 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
544 gtk_tree_selection_unselect_all (sel);
545 g_signal_connect (sel, "changed",
546 G_CALLBACK (gtk_entry_completion_selection_changed),
548 priv->first_sel_changed = TRUE;
550 priv->column = gtk_tree_view_column_new_with_area (priv->cell_area);
551 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
553 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
554 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
556 GTK_POLICY_AUTOMATIC);
557 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
560 /* a nasty hack to get the completions treeview to size nicely */
561 gtk_widget_set_size_request (gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (priv->scrolled_window)),
565 priv->actions = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_BOOLEAN);
568 gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->actions));
569 g_object_ref_sink (priv->action_view);
570 g_signal_connect (priv->action_view, "button-press-event",
571 G_CALLBACK (gtk_entry_completion_action_button_press),
573 g_signal_connect (priv->action_view, "enter-notify-event",
574 G_CALLBACK (gtk_entry_completion_list_enter_notify),
576 g_signal_connect (priv->action_view, "motion-notify-event",
577 G_CALLBACK (gtk_entry_completion_list_motion_notify),
579 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->action_view), FALSE);
580 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->action_view), TRUE);
582 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->action_view));
583 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
584 gtk_tree_selection_unselect_all (sel);
586 cell = gtk_cell_renderer_text_new ();
587 gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (priv->action_view),
590 gtk_entry_completion_action_data_func,
595 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
596 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
597 gtk_window_set_type_hint (GTK_WINDOW(priv->popup_window),
598 GDK_WINDOW_TYPE_HINT_COMBO);
599 g_signal_connect (priv->popup_window, "key-press-event",
600 G_CALLBACK (gtk_entry_completion_popup_key_event),
602 g_signal_connect (priv->popup_window, "key-release-event",
603 G_CALLBACK (gtk_entry_completion_popup_key_event),
605 g_signal_connect (priv->popup_window, "button-press-event",
606 G_CALLBACK (gtk_entry_completion_popup_button_press),
609 popup_frame = gtk_frame_new (NULL);
610 gtk_frame_set_shadow_type (GTK_FRAME (popup_frame),
611 GTK_SHADOW_ETCHED_IN);
612 gtk_widget_show (popup_frame);
613 gtk_container_add (GTK_CONTAINER (priv->popup_window), popup_frame);
615 priv->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
616 gtk_container_add (GTK_CONTAINER (popup_frame), priv->vbox);
618 gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->tree_view);
619 gtk_box_pack_start (GTK_BOX (priv->vbox), priv->scrolled_window,
622 /* we don't want to see the action treeview when no actions have
623 * been inserted, so we pack the action treeview after the first
624 * action has been added
632 gtk_entry_completion_set_property (GObject *object,
637 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
638 GtkEntryCompletionPrivate *priv = completion->priv;
644 gtk_entry_completion_set_model (completion,
645 g_value_get_object (value));
648 case PROP_MINIMUM_KEY_LENGTH:
649 gtk_entry_completion_set_minimum_key_length (completion,
650 g_value_get_int (value));
653 case PROP_TEXT_COLUMN:
654 priv->text_column = g_value_get_int (value);
657 case PROP_INLINE_COMPLETION:
658 priv->inline_completion = g_value_get_boolean (value);
661 case PROP_POPUP_COMPLETION:
662 priv->popup_completion = g_value_get_boolean (value);
665 case PROP_POPUP_SET_WIDTH:
666 priv->popup_set_width = g_value_get_boolean (value);
669 case PROP_POPUP_SINGLE_MATCH:
670 priv->popup_single_match = g_value_get_boolean (value);
673 case PROP_INLINE_SELECTION:
674 priv->inline_selection = g_value_get_boolean (value);
678 /* Construct-only, can only be assigned once */
679 area = g_value_get_object (value);
682 if (priv->cell_area != NULL)
684 g_warning ("cell-area has already been set, ignoring construct property");
685 g_object_ref_sink (area);
686 g_object_unref (area);
689 priv->cell_area = g_object_ref_sink (area);
694 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
700 gtk_entry_completion_get_property (GObject *object,
705 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
710 g_value_set_object (value,
711 gtk_entry_completion_get_model (completion));
714 case PROP_MINIMUM_KEY_LENGTH:
715 g_value_set_int (value, gtk_entry_completion_get_minimum_key_length (completion));
718 case PROP_TEXT_COLUMN:
719 g_value_set_int (value, gtk_entry_completion_get_text_column (completion));
722 case PROP_INLINE_COMPLETION:
723 g_value_set_boolean (value, gtk_entry_completion_get_inline_completion (completion));
726 case PROP_POPUP_COMPLETION:
727 g_value_set_boolean (value, gtk_entry_completion_get_popup_completion (completion));
730 case PROP_POPUP_SET_WIDTH:
731 g_value_set_boolean (value, gtk_entry_completion_get_popup_set_width (completion));
734 case PROP_POPUP_SINGLE_MATCH:
735 g_value_set_boolean (value, gtk_entry_completion_get_popup_single_match (completion));
738 case PROP_INLINE_SELECTION:
739 g_value_set_boolean (value, gtk_entry_completion_get_inline_selection (completion));
743 g_value_set_object (value, completion->priv->cell_area);
747 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
753 gtk_entry_completion_finalize (GObject *object)
755 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
756 GtkEntryCompletionPrivate *priv = completion->priv;
758 g_free (priv->case_normalized_key);
759 g_free (priv->completion_prefix);
761 if (priv->match_notify)
762 (* priv->match_notify) (priv->match_data);
764 G_OBJECT_CLASS (gtk_entry_completion_parent_class)->finalize (object);
768 gtk_entry_completion_dispose (GObject *object)
770 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
771 GtkEntryCompletionPrivate *priv = completion->priv;
775 gtk_widget_destroy (priv->tree_view);
776 priv->tree_view = NULL;
780 gtk_entry_set_completion (GTK_ENTRY (priv->entry), NULL);
784 g_object_unref (priv->actions);
785 priv->actions = NULL;
788 if (priv->action_view)
790 g_object_unref (priv->action_view);
791 priv->action_view = NULL;
794 if (priv->popup_window)
796 gtk_widget_destroy (priv->popup_window);
797 priv->popup_window = NULL;
802 g_object_unref (priv->cell_area);
803 priv->cell_area = NULL;
806 G_OBJECT_CLASS (gtk_entry_completion_parent_class)->dispose (object);
809 /* implement cell layout interface (only need to return the underlying cell area) */
811 gtk_entry_completion_get_area (GtkCellLayout *cell_layout)
813 GtkEntryCompletionPrivate *priv;
815 priv = GTK_ENTRY_COMPLETION (cell_layout)->priv;
817 if (G_UNLIKELY (!priv->cell_area))
819 priv->cell_area = gtk_cell_area_box_new ();
820 g_object_ref_sink (priv->cell_area);
823 return priv->cell_area;
826 /* all those callbacks */
828 gtk_entry_completion_default_completion_func (GtkEntryCompletion *completion,
834 gchar *normalized_string;
835 gchar *case_normalized_string;
837 gboolean ret = FALSE;
841 model = gtk_tree_model_filter_get_model (completion->priv->filter_model);
843 g_return_val_if_fail (gtk_tree_model_get_column_type (model, completion->priv->text_column) == G_TYPE_STRING,
846 gtk_tree_model_get (model, iter,
847 completion->priv->text_column, &item,
852 normalized_string = g_utf8_normalize (item, -1, G_NORMALIZE_ALL);
854 if (normalized_string != NULL)
856 case_normalized_string = g_utf8_casefold (normalized_string, -1);
858 if (!strncmp (key, case_normalized_string, strlen (key)))
861 g_free (case_normalized_string);
863 g_free (normalized_string);
871 gtk_entry_completion_visible_func (GtkTreeModel *model,
875 gboolean ret = FALSE;
877 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
879 if (!completion->priv->case_normalized_key)
882 if (completion->priv->match_func)
883 ret = (* completion->priv->match_func) (completion,
884 completion->priv->case_normalized_key,
886 completion->priv->match_data);
887 else if (completion->priv->text_column >= 0)
888 ret = gtk_entry_completion_default_completion_func (completion,
889 completion->priv->case_normalized_key,
897 gtk_entry_completion_popup_key_event (GtkWidget *widget,
901 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
903 if (!gtk_widget_get_mapped (completion->priv->popup_window))
906 /* propagate event to the entry */
907 gtk_widget_event (completion->priv->entry, (GdkEvent *)event);
913 gtk_entry_completion_popup_button_press (GtkWidget *widget,
914 GdkEventButton *event,
917 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
919 if (!gtk_widget_get_mapped (completion->priv->popup_window))
922 /* if we come here, it's usually time to popdown */
923 _gtk_entry_completion_popdown (completion);
929 gtk_entry_completion_list_button_press (GtkWidget *widget,
930 GdkEventButton *event,
933 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
934 GtkTreePath *path = NULL;
936 if (!gtk_widget_get_mapped (completion->priv->popup_window))
939 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
941 &path, NULL, NULL, NULL))
946 GtkTreeIter child_iter;
948 gtk_tree_model_get_iter (GTK_TREE_MODEL (completion->priv->filter_model),
950 gtk_tree_path_free (path);
951 gtk_tree_model_filter_convert_iter_to_child_iter (completion->priv->filter_model,
954 model = gtk_tree_model_filter_get_model (completion->priv->filter_model);
956 g_signal_handler_block (completion->priv->entry,
957 completion->priv->changed_id);
958 g_signal_emit (completion, entry_completion_signals[MATCH_SELECTED],
959 0, model, &child_iter, &entry_set);
960 g_signal_handler_unblock (completion->priv->entry,
961 completion->priv->changed_id);
963 _gtk_entry_completion_popdown (completion);
972 gtk_entry_completion_action_button_press (GtkWidget *widget,
973 GdkEventButton *event,
976 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
977 GtkTreePath *path = NULL;
979 if (!gtk_widget_get_mapped (completion->priv->popup_window))
982 _gtk_entry_reset_im_context (GTK_ENTRY (completion->priv->entry));
984 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
986 &path, NULL, NULL, NULL))
988 g_signal_emit (completion, entry_completion_signals[ACTION_ACTIVATED],
989 0, gtk_tree_path_get_indices (path)[0]);
990 gtk_tree_path_free (path);
992 _gtk_entry_completion_popdown (completion);
1000 gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
1001 GtkCellRenderer *cell,
1002 GtkTreeModel *model,
1006 gchar *string = NULL;
1009 gtk_tree_model_get (model, iter,
1032 gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
1035 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1037 if (completion->priv->first_sel_changed)
1039 completion->priv->first_sel_changed = FALSE;
1040 if (gtk_widget_is_focus (completion->priv->tree_view))
1041 gtk_tree_selection_unselect_all (selection);
1048 * gtk_entry_completion_new:
1050 * Creates a new #GtkEntryCompletion object.
1052 * Return value: A newly created #GtkEntryCompletion object
1056 GtkEntryCompletion *
1057 gtk_entry_completion_new (void)
1059 GtkEntryCompletion *completion;
1061 completion = g_object_new (GTK_TYPE_ENTRY_COMPLETION, NULL);
1067 * gtk_entry_completion_new_with_area:
1068 * @area: the #GtkCellArea used to layout cells
1070 * Creates a new #GtkEntryCompletion object using the
1071 * specified @area to layout cells in the underlying
1072 * #GtkTreeViewColumn for the drop-down menu.
1074 * Return value: A newly created #GtkEntryCompletion object
1078 GtkEntryCompletion *
1079 gtk_entry_completion_new_with_area (GtkCellArea *area)
1081 GtkEntryCompletion *completion;
1083 completion = g_object_new (GTK_TYPE_ENTRY_COMPLETION, "cell-area", area, NULL);
1089 * gtk_entry_completion_get_entry:
1090 * @completion: a #GtkEntryCompletion
1092 * Gets the entry @completion has been attached to.
1094 * Return value: (transfer none): The entry @completion has been attached to
1099 gtk_entry_completion_get_entry (GtkEntryCompletion *completion)
1101 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
1103 return completion->priv->entry;
1107 * gtk_entry_completion_set_model:
1108 * @completion: a #GtkEntryCompletion
1109 * @model: (allow-none): the #GtkTreeModel
1111 * Sets the model for a #GtkEntryCompletion. If @completion already has
1112 * a model set, it will remove it before setting the new model.
1113 * If model is %NULL, then it will unset the model.
1118 gtk_entry_completion_set_model (GtkEntryCompletion *completion,
1119 GtkTreeModel *model)
1121 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1122 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
1126 gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
1128 _gtk_entry_completion_popdown (completion);
1129 completion->priv->filter_model = NULL;
1133 /* code will unref the old filter model (if any) */
1134 completion->priv->filter_model =
1135 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
1136 gtk_tree_model_filter_set_visible_func (completion->priv->filter_model,
1137 gtk_entry_completion_visible_func,
1141 gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
1142 GTK_TREE_MODEL (completion->priv->filter_model));
1143 g_object_unref (completion->priv->filter_model);
1145 g_object_notify (G_OBJECT (completion), "model");
1147 if (gtk_widget_get_visible (completion->priv->popup_window))
1148 _gtk_entry_completion_resize_popup (completion);
1152 * gtk_entry_completion_get_model:
1153 * @completion: a #GtkEntryCompletion
1155 * Returns the model the #GtkEntryCompletion is using as data source.
1156 * Returns %NULL if the model is unset.
1158 * Return value: (transfer none): A #GtkTreeModel, or %NULL if none
1159 * is currently being used
1164 gtk_entry_completion_get_model (GtkEntryCompletion *completion)
1166 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
1168 if (!completion->priv->filter_model)
1171 return gtk_tree_model_filter_get_model (completion->priv->filter_model);
1175 * gtk_entry_completion_set_match_func:
1176 * @completion: a #GtkEntryCompletion
1177 * @func: the #GtkEntryCompletionMatchFunc to use
1178 * @func_data: user data for @func
1179 * @func_notify: destroy notify for @func_data.
1181 * Sets the match function for @completion to be @func. The match function
1182 * is used to determine if a row should or should not be in the completion
1188 gtk_entry_completion_set_match_func (GtkEntryCompletion *completion,
1189 GtkEntryCompletionMatchFunc func,
1191 GDestroyNotify func_notify)
1193 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1195 if (completion->priv->match_notify)
1196 (* completion->priv->match_notify) (completion->priv->match_data);
1198 completion->priv->match_func = func;
1199 completion->priv->match_data = func_data;
1200 completion->priv->match_notify = func_notify;
1204 * gtk_entry_completion_set_minimum_key_length:
1205 * @completion: a #GtkEntryCompletion
1206 * @length: the minimum length of the key in order to start completing
1208 * Requires the length of the search key for @completion to be at least
1209 * @length. This is useful for long lists, where completing using a small
1210 * key takes a lot of time and will come up with meaningless results anyway
1211 * (ie, a too large dataset).
1216 gtk_entry_completion_set_minimum_key_length (GtkEntryCompletion *completion,
1219 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1220 g_return_if_fail (length >= 0);
1222 if (completion->priv->minimum_key_length != length)
1224 completion->priv->minimum_key_length = length;
1226 g_object_notify (G_OBJECT (completion), "minimum-key-length");
1231 * gtk_entry_completion_get_minimum_key_length:
1232 * @completion: a #GtkEntryCompletion
1234 * Returns the minimum key length as set for @completion.
1236 * Return value: The currently used minimum key length
1241 gtk_entry_completion_get_minimum_key_length (GtkEntryCompletion *completion)
1243 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), 0);
1245 return completion->priv->minimum_key_length;
1249 * gtk_entry_completion_complete:
1250 * @completion: a #GtkEntryCompletion
1252 * Requests a completion operation, or in other words a refiltering of the
1253 * current list with completions, using the current key. The completion list
1254 * view will be updated accordingly.
1259 gtk_entry_completion_complete (GtkEntryCompletion *completion)
1263 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1264 g_return_if_fail (GTK_IS_ENTRY (completion->priv->entry));
1266 if (!completion->priv->filter_model)
1269 g_free (completion->priv->case_normalized_key);
1271 tmp = g_utf8_normalize (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)),
1272 -1, G_NORMALIZE_ALL);
1273 completion->priv->case_normalized_key = g_utf8_casefold (tmp, -1);
1276 gtk_tree_model_filter_refilter (completion->priv->filter_model);
1278 if (gtk_widget_get_visible (completion->priv->popup_window))
1279 _gtk_entry_completion_resize_popup (completion);
1283 gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
1285 const gchar *string,
1290 gtk_list_store_insert (completion->priv->actions, &iter, index);
1291 gtk_list_store_set (completion->priv->actions, &iter,
1296 if (!gtk_widget_get_parent (completion->priv->action_view))
1298 GtkTreePath *path = gtk_tree_path_new_from_indices (0, -1);
1300 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
1302 gtk_tree_path_free (path);
1304 gtk_box_pack_start (GTK_BOX (completion->priv->vbox),
1305 completion->priv->action_view, FALSE, FALSE, 0);
1306 gtk_widget_show (completion->priv->action_view);
1311 * gtk_entry_completion_insert_action_text:
1312 * @completion: a #GtkEntryCompletion
1313 * @index_: the index of the item to insert
1314 * @text: text of the item to insert
1316 * Inserts an action in @completion's action item list at position @index_
1317 * with text @text. If you want the action item to have markup, use
1318 * gtk_entry_completion_insert_action_markup().
1323 gtk_entry_completion_insert_action_text (GtkEntryCompletion *completion,
1327 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1328 g_return_if_fail (text != NULL);
1330 gtk_entry_completion_insert_action (completion, index_, text, FALSE);
1334 * gtk_entry_completion_insert_action_markup:
1335 * @completion: a #GtkEntryCompletion
1336 * @index_: the index of the item to insert
1337 * @markup: markup of the item to insert
1339 * Inserts an action in @completion's action item list at position @index_
1340 * with markup @markup.
1345 gtk_entry_completion_insert_action_markup (GtkEntryCompletion *completion,
1347 const gchar *markup)
1349 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1350 g_return_if_fail (markup != NULL);
1352 gtk_entry_completion_insert_action (completion, index_, markup, TRUE);
1356 * gtk_entry_completion_delete_action:
1357 * @completion: a #GtkEntryCompletion
1358 * @index_: the index of the item to delete
1360 * Deletes the action at @index_ from @completion's action list.
1365 gtk_entry_completion_delete_action (GtkEntryCompletion *completion,
1370 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1371 g_return_if_fail (index_ >= 0);
1373 gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (completion->priv->actions),
1374 &iter, NULL, index_);
1375 gtk_list_store_remove (completion->priv->actions, &iter);
1379 * gtk_entry_completion_set_text_column:
1380 * @completion: a #GtkEntryCompletion
1381 * @column: the column in the model of @completion to get strings from
1383 * Convenience function for setting up the most used case of this code: a
1384 * completion list with just strings. This function will set up @completion
1385 * to have a list displaying all (and just) strings in the completion list,
1386 * and to get those strings from @column in the model of @completion.
1388 * This functions creates and adds a #GtkCellRendererText for the selected
1389 * column. If you need to set the text column, but don't want the cell
1390 * renderer, use g_object_set() to set the #GtkEntryCompletion:text-column
1391 * property directly.
1396 gtk_entry_completion_set_text_column (GtkEntryCompletion *completion,
1399 GtkCellRenderer *cell;
1401 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1402 g_return_if_fail (column >= 0);
1404 completion->priv->text_column = column;
1406 cell = gtk_cell_renderer_text_new ();
1407 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion),
1409 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (completion),
1413 g_object_notify (G_OBJECT (completion), "text-column");
1417 * gtk_entry_completion_get_text_column:
1418 * @completion: a #GtkEntryCompletion
1420 * Returns the column in the model of @completion to get strings from.
1422 * Return value: the column containing the strings
1427 gtk_entry_completion_get_text_column (GtkEntryCompletion *completion)
1429 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), -1);
1431 return completion->priv->text_column;
1437 gtk_entry_completion_list_enter_notify (GtkWidget *widget,
1438 GdkEventCrossing *event,
1441 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1443 return completion->priv->ignore_enter;
1447 gtk_entry_completion_list_motion_notify (GtkWidget *widget,
1448 GdkEventMotion *event,
1451 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1453 completion->priv->ignore_enter = FALSE;
1459 /* some nasty size requisition */
1461 _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
1463 GtkAllocation allocation;
1465 gint matches, actions, items, height;
1469 gint vertical_separator;
1470 GdkRectangle monitor;
1472 GtkRequisition popup_req;
1473 GtkRequisition entry_req;
1477 GtkTreeViewColumn *action_column;
1480 window = gtk_widget_get_window (completion->priv->entry);
1485 gtk_widget_get_allocation (completion->priv->entry, &allocation);
1486 gtk_widget_get_preferred_size (completion->priv->entry,
1489 gdk_window_get_origin (window, &x, &y);
1491 y += allocation.y + (allocation.height - entry_req.height) / 2;
1493 _gtk_entry_get_borders (GTK_ENTRY (completion->priv->entry), &borders);
1495 matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
1496 actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
1497 action_column = gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0);
1499 gtk_tree_view_column_cell_get_size (completion->priv->column, NULL,
1500 NULL, NULL, NULL, &height);
1501 gtk_tree_view_column_cell_get_size (action_column, NULL,
1502 NULL, NULL, NULL, &action_height);
1504 gtk_widget_style_get (GTK_WIDGET (completion->priv->tree_view),
1505 "vertical-separator", &vertical_separator,
1508 height += vertical_separator;
1510 gtk_widget_realize (completion->priv->tree_view);
1512 screen = gtk_widget_get_screen (GTK_WIDGET (completion->priv->entry));
1513 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
1514 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
1516 if (y > monitor.height / 2)
1517 items = MIN (matches, (((monitor.y + y) - (actions * action_height)) / height) - 1);
1519 items = MIN (matches, (((monitor.height - y) - (actions * action_height)) / height) - 1);
1522 gtk_widget_hide (completion->priv->scrolled_window);
1524 gtk_widget_show (completion->priv->scrolled_window);
1526 if (completion->priv->popup_set_width)
1527 width = MIN (allocation.width, monitor.width) - borders.left - borders.right;
1531 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (completion->priv->tree_view));
1532 gtk_scrolled_window_set_min_content_width (GTK_SCROLLED_WINDOW (completion->priv->scrolled_window), width);
1533 gtk_widget_set_size_request (completion->priv->scrolled_window, width, -1);
1534 gtk_scrolled_window_set_min_content_height (GTK_SCROLLED_WINDOW (completion->priv->scrolled_window), items * height);
1538 gtk_widget_show (completion->priv->action_view);
1539 gtk_widget_set_size_request (completion->priv->action_view, width, -1);
1542 gtk_widget_hide (completion->priv->action_view);
1544 gtk_widget_get_preferred_size (completion->priv->popup_window,
1549 else if (x + popup_req.width > monitor.x + monitor.width)
1550 x = monitor.x + monitor.width - popup_req.width;
1552 if (y + entry_req.height + popup_req.height <= monitor.y + monitor.height ||
1553 y - monitor.y < (monitor.y + monitor.height) - (y + entry_req.height))
1555 y += entry_req.height;
1560 y -= popup_req.height;
1566 path = gtk_tree_path_new_from_indices (above ? matches - 1 : 0, -1);
1567 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (completion->priv->tree_view), path,
1568 NULL, FALSE, 0.0, 0.0);
1569 gtk_tree_path_free (path);
1572 gtk_window_move (GTK_WINDOW (completion->priv->popup_window), x, y);
1578 _gtk_entry_completion_popup (GtkEntryCompletion *completion,
1581 GtkTreeViewColumn *column;
1582 GtkStyleContext *context;
1585 GtkWidget *toplevel;
1587 if (gtk_widget_get_mapped (completion->priv->popup_window))
1590 if (!gtk_widget_get_mapped (completion->priv->entry))
1593 if (!gtk_widget_has_focus (completion->priv->entry))
1596 if (completion->priv->grab_device)
1599 completion->priv->ignore_enter = TRUE;
1601 column = gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0);
1602 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
1604 context = gtk_widget_get_style_context (completion->priv->tree_view);
1605 gtk_style_context_get_background_color (context, 0, &color);
1607 g_object_set (GTK_CELL_RENDERER (renderers->data),
1608 "cell-background-rgba", &color,
1610 g_list_free (renderers);
1612 gtk_widget_show_all (completion->priv->vbox);
1614 /* default on no match */
1615 completion->priv->current_selected = -1;
1617 _gtk_entry_completion_resize_popup (completion);
1619 toplevel = gtk_widget_get_toplevel (completion->priv->entry);
1620 if (GTK_IS_WINDOW (toplevel))
1621 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1622 GTK_WINDOW (completion->priv->popup_window));
1624 /* prevent the first row being focused */
1625 gtk_widget_grab_focus (completion->priv->tree_view);
1627 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
1628 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
1630 gtk_window_set_screen (GTK_WINDOW (completion->priv->popup_window),
1631 gtk_widget_get_screen (completion->priv->entry));
1633 gtk_widget_show (completion->priv->popup_window);
1635 gtk_device_grab_add (completion->priv->popup_window, device, TRUE);
1636 gdk_device_grab (device, gtk_widget_get_window (completion->priv->popup_window),
1637 GDK_OWNERSHIP_WINDOW, TRUE,
1638 GDK_BUTTON_PRESS_MASK |
1639 GDK_BUTTON_RELEASE_MASK |
1640 GDK_POINTER_MOTION_MASK,
1641 NULL, GDK_CURRENT_TIME);
1643 completion->priv->grab_device = device;
1647 _gtk_entry_completion_popdown (GtkEntryCompletion *completion)
1649 if (!gtk_widget_get_mapped (completion->priv->popup_window))
1652 completion->priv->ignore_enter = FALSE;
1654 if (completion->priv->grab_device)
1656 gdk_device_ungrab (completion->priv->grab_device, GDK_CURRENT_TIME);
1657 gtk_device_grab_remove (completion->priv->popup_window,
1658 completion->priv->grab_device);
1659 completion->priv->grab_device = NULL;
1662 gtk_widget_hide (completion->priv->popup_window);
1666 gtk_entry_completion_match_selected (GtkEntryCompletion *completion,
1667 GtkTreeModel *model,
1672 gtk_tree_model_get (model, iter, completion->priv->text_column, &str, -1);
1673 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), str ? str : "");
1675 /* move cursor to the end */
1676 gtk_editable_set_position (GTK_EDITABLE (completion->priv->entry), -1);
1684 gtk_entry_completion_cursor_on_match (GtkEntryCompletion *completion,
1685 GtkTreeModel *model,
1688 gtk_entry_completion_insert_completion (completion, model, iter);
1694 * gtk_entry_completion_compute_prefix:
1695 * @completion: the entry completion
1696 * @key: The text to complete for
1698 * Computes the common prefix that is shared by all rows in @completion
1699 * that start with @key. If no row matches @key, %NULL will be returned.
1700 * Note that a text column must have been set for this function to work,
1701 * see gtk_entry_completion_set_text_column() for details.
1703 * Returns: (transfer: full): The common prefix all rows starting with @key
1704 * or %NULL if no row matches @key.
1709 gtk_entry_completion_compute_prefix (GtkEntryCompletion *completion,
1713 gchar *prefix = NULL;
1716 if (completion->priv->text_column < 0)
1719 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (completion->priv->filter_model),
1726 gtk_tree_model_get (GTK_TREE_MODEL (completion->priv->filter_model),
1727 &iter, completion->priv->text_column, &text,
1730 if (text && g_str_has_prefix (text, key))
1733 prefix = g_strdup (text);
1739 while (*p && *p == *q)
1749 /* strip a partial multibyte character */
1750 q = g_utf8_find_prev_char (prefix, p);
1751 switch (g_utf8_get_char_validated (q, p - q))
1763 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (completion->priv->filter_model),
1772 gtk_entry_completion_real_insert_prefix (GtkEntryCompletion *completion,
1773 const gchar *prefix)
1781 prefix_len = g_utf8_strlen (prefix, -1);
1783 key = gtk_entry_get_text (GTK_ENTRY (completion->priv->entry));
1784 key_len = g_utf8_strlen (key, -1);
1786 if (prefix_len > key_len)
1788 gint pos = prefix_len;
1790 gtk_editable_insert_text (GTK_EDITABLE (completion->priv->entry),
1791 prefix + strlen (key), -1, &pos);
1792 gtk_editable_select_region (GTK_EDITABLE (completion->priv->entry),
1793 key_len, prefix_len);
1795 completion->priv->has_completion = TRUE;
1803 * gtk_entry_completion_get_completion_prefix:
1804 * @completion: a #GtkEntryCompletion
1806 * Get the original text entered by the user that triggered
1807 * the completion or %NULL if there's no completion ongoing.
1809 * Returns: the prefix for the current completion
1814 gtk_entry_completion_get_completion_prefix (GtkEntryCompletion *completion)
1816 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
1818 return completion->priv->completion_prefix;
1822 gtk_entry_completion_insert_completion_text (GtkEntryCompletion *completion,
1825 GtkEntryCompletionPrivate *priv = completion->priv;
1828 priv = completion->priv;
1830 if (priv->changed_id > 0)
1831 g_signal_handler_block (priv->entry, priv->changed_id);
1833 if (priv->insert_text_id > 0)
1834 g_signal_handler_block (priv->entry, priv->insert_text_id);
1836 gtk_entry_set_text (GTK_ENTRY (priv->entry), text);
1838 len = strlen (priv->completion_prefix);
1839 gtk_editable_select_region (GTK_EDITABLE (priv->entry), len, -1);
1841 if (priv->changed_id > 0)
1842 g_signal_handler_unblock (priv->entry, priv->changed_id);
1844 if (priv->insert_text_id > 0)
1845 g_signal_handler_unblock (priv->entry, priv->insert_text_id);
1849 gtk_entry_completion_insert_completion (GtkEntryCompletion *completion,
1850 GtkTreeModel *model,
1855 if (completion->priv->text_column < 0)
1858 gtk_tree_model_get (model, iter,
1859 completion->priv->text_column, &str,
1862 gtk_entry_completion_insert_completion_text (completion, str);
1870 * gtk_entry_completion_insert_prefix:
1871 * @completion: a #GtkEntryCompletion
1873 * Requests a prefix insertion.
1878 gtk_entry_completion_insert_prefix (GtkEntryCompletion *completion)
1883 if (completion->priv->insert_text_id > 0)
1884 g_signal_handler_block (completion->priv->entry,
1885 completion->priv->insert_text_id);
1887 prefix = gtk_entry_completion_compute_prefix (completion,
1888 gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)));
1892 g_signal_emit (completion, entry_completion_signals[INSERT_PREFIX],
1897 if (completion->priv->insert_text_id > 0)
1898 g_signal_handler_unblock (completion->priv->entry,
1899 completion->priv->insert_text_id);
1903 * gtk_entry_completion_set_inline_completion:
1904 * @completion: a #GtkEntryCompletion
1905 * @inline_completion: %TRUE to do inline completion
1907 * Sets whether the common prefix of the possible completions should
1908 * be automatically inserted in the entry.
1913 gtk_entry_completion_set_inline_completion (GtkEntryCompletion *completion,
1914 gboolean inline_completion)
1916 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1918 inline_completion = inline_completion != FALSE;
1920 if (completion->priv->inline_completion != inline_completion)
1922 completion->priv->inline_completion = inline_completion;
1924 g_object_notify (G_OBJECT (completion), "inline-completion");
1929 * gtk_entry_completion_get_inline_completion:
1930 * @completion: a #GtkEntryCompletion
1932 * Returns whether the common prefix of the possible completions should
1933 * be automatically inserted in the entry.
1935 * Return value: %TRUE if inline completion is turned on
1940 gtk_entry_completion_get_inline_completion (GtkEntryCompletion *completion)
1942 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), FALSE);
1944 return completion->priv->inline_completion;
1948 * gtk_entry_completion_set_popup_completion:
1949 * @completion: a #GtkEntryCompletion
1950 * @popup_completion: %TRUE to do popup completion
1952 * Sets whether the completions should be presented in a popup window.
1957 gtk_entry_completion_set_popup_completion (GtkEntryCompletion *completion,
1958 gboolean popup_completion)
1960 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1962 popup_completion = popup_completion != FALSE;
1964 if (completion->priv->popup_completion != popup_completion)
1966 completion->priv->popup_completion = popup_completion;
1968 g_object_notify (G_OBJECT (completion), "popup-completion");
1974 * gtk_entry_completion_get_popup_completion:
1975 * @completion: a #GtkEntryCompletion
1977 * Returns whether the completions should be presented in a popup window.
1979 * Return value: %TRUE if popup completion is turned on
1984 gtk_entry_completion_get_popup_completion (GtkEntryCompletion *completion)
1986 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), TRUE);
1988 return completion->priv->popup_completion;
1992 * gtk_entry_completion_set_popup_set_width:
1993 * @completion: a #GtkEntryCompletion
1994 * @popup_set_width: %TRUE to make the width of the popup the same as the entry
1996 * Sets whether the completion popup window will be resized to be the same
1997 * width as the entry.
2002 gtk_entry_completion_set_popup_set_width (GtkEntryCompletion *completion,
2003 gboolean popup_set_width)
2005 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
2007 popup_set_width = popup_set_width != FALSE;
2009 if (completion->priv->popup_set_width != popup_set_width)
2011 completion->priv->popup_set_width = popup_set_width;
2013 g_object_notify (G_OBJECT (completion), "popup-set-width");
2018 * gtk_entry_completion_get_popup_set_width:
2019 * @completion: a #GtkEntryCompletion
2021 * Returns whether the completion popup window will be resized to the
2022 * width of the entry.
2024 * Return value: %TRUE if the popup window will be resized to the width of
2030 gtk_entry_completion_get_popup_set_width (GtkEntryCompletion *completion)
2032 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), TRUE);
2034 return completion->priv->popup_set_width;
2039 * gtk_entry_completion_set_popup_single_match:
2040 * @completion: a #GtkEntryCompletion
2041 * @popup_single_match: %TRUE if the popup should appear even for a single
2044 * Sets whether the completion popup window will appear even if there is
2045 * only a single match. You may want to set this to %FALSE if you
2046 * are using <link linkend="GtkEntryCompletion--inline-completion">inline
2047 * completion</link>.
2052 gtk_entry_completion_set_popup_single_match (GtkEntryCompletion *completion,
2053 gboolean popup_single_match)
2055 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
2057 popup_single_match = popup_single_match != FALSE;
2059 if (completion->priv->popup_single_match != popup_single_match)
2061 completion->priv->popup_single_match = popup_single_match;
2063 g_object_notify (G_OBJECT (completion), "popup-single-match");
2068 * gtk_entry_completion_get_popup_single_match:
2069 * @completion: a #GtkEntryCompletion
2071 * Returns whether the completion popup window will appear even if there is
2072 * only a single match.
2074 * Return value: %TRUE if the popup window will appear regardless of the
2080 gtk_entry_completion_get_popup_single_match (GtkEntryCompletion *completion)
2082 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), TRUE);
2084 return completion->priv->popup_single_match;
2088 * gtk_entry_completion_set_inline_selection:
2089 * @completion: a #GtkEntryCompletion
2090 * @inline_selection: %TRUE to do inline selection
2092 * Sets whether it is possible to cycle through the possible completions
2098 gtk_entry_completion_set_inline_selection (GtkEntryCompletion *completion,
2099 gboolean inline_selection)
2101 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
2103 inline_selection = inline_selection != FALSE;
2105 if (completion->priv->inline_selection != inline_selection)
2107 completion->priv->inline_selection = inline_selection;
2109 g_object_notify (G_OBJECT (completion), "inline-selection");
2114 * gtk_entry_completion_get_inline_selection:
2115 * @completion: a #GtkEntryCompletion
2117 * Returns %TRUE if inline-selection mode is turned on.
2119 * Returns: %TRUE if inline-selection mode is on
2124 gtk_entry_completion_get_inline_selection (GtkEntryCompletion *completion)
2126 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), FALSE);
2128 return completion->priv->inline_selection;