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"
91 #define COMPLETION_TIMEOUT 300
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,
182 static void connect_completion_signals (GtkEntryCompletion *completion);
183 static void disconnect_completion_signals (GtkEntryCompletion *completion);
186 static guint entry_completion_signals[LAST_SIGNAL] = { 0 };
189 static void gtk_entry_completion_buildable_init (GtkBuildableIface *iface);
191 G_DEFINE_TYPE_WITH_CODE (GtkEntryCompletion, gtk_entry_completion, G_TYPE_OBJECT,
192 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
193 gtk_entry_completion_cell_layout_init)
194 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
195 gtk_entry_completion_buildable_init))
199 gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
201 GObjectClass *object_class;
203 object_class = (GObjectClass *)klass;
205 object_class->constructor = gtk_entry_completion_constructor;
206 object_class->set_property = gtk_entry_completion_set_property;
207 object_class->get_property = gtk_entry_completion_get_property;
208 object_class->dispose = gtk_entry_completion_dispose;
209 object_class->finalize = gtk_entry_completion_finalize;
211 klass->match_selected = gtk_entry_completion_match_selected;
212 klass->insert_prefix = gtk_entry_completion_real_insert_prefix;
213 klass->cursor_on_match = gtk_entry_completion_cursor_on_match;
216 * GtkEntryCompletion::insert-prefix:
217 * @widget: the object which received the signal
218 * @prefix: the common prefix of all possible completions
220 * Gets emitted when the inline autocompletion is triggered.
221 * The default behaviour is to make the entry display the
222 * whole prefix and select the newly inserted part.
224 * Applications may connect to this signal in order to insert only a
225 * smaller part of the @prefix into the entry - e.g. the entry used in
226 * the #GtkFileChooser inserts only the part of the prefix up to the
229 * Return value: %TRUE if the signal has been handled
233 entry_completion_signals[INSERT_PREFIX] =
234 g_signal_new (I_("insert-prefix"),
235 G_TYPE_FROM_CLASS (klass),
237 G_STRUCT_OFFSET (GtkEntryCompletionClass, insert_prefix),
238 _gtk_boolean_handled_accumulator, NULL,
239 _gtk_marshal_BOOLEAN__STRING,
244 * GtkEntryCompletion::match-selected:
245 * @widget: the object which received the signal
246 * @model: the #GtkTreeModel containing the matches
247 * @iter: a #GtkTreeIter positioned at the selected match
249 * Gets emitted when a match from the list is selected.
250 * The default behaviour is to replace the contents of the
251 * entry with the contents of the text column in the row
252 * pointed to by @iter.
254 * Note that @model is the model that was passed to
255 * gtk_entry_completion_set_model().
257 * Return value: %TRUE if the signal has been handled
261 entry_completion_signals[MATCH_SELECTED] =
262 g_signal_new (I_("match-selected"),
263 G_TYPE_FROM_CLASS (klass),
265 G_STRUCT_OFFSET (GtkEntryCompletionClass, match_selected),
266 _gtk_boolean_handled_accumulator, NULL,
267 _gtk_marshal_BOOLEAN__OBJECT_BOXED,
273 * GtkEntryCompletion::cursor-on-match:
274 * @widget: the object which received the signal
275 * @model: the #GtkTreeModel containing the matches
276 * @iter: a #GtkTreeIter positioned at the selected match
278 * Gets emitted when a match from the cursor is on a match
279 * of the list. The default behaviour is to replace the contents
280 * of the entry with the contents of the text column in the row
281 * pointed to by @iter.
283 * Note that @model is the model that was passed to
284 * gtk_entry_completion_set_model().
286 * Return value: %TRUE if the signal has been handled
290 entry_completion_signals[CURSOR_ON_MATCH] =
291 g_signal_new (I_("cursor-on-match"),
292 G_TYPE_FROM_CLASS (klass),
294 G_STRUCT_OFFSET (GtkEntryCompletionClass, cursor_on_match),
295 _gtk_boolean_handled_accumulator, NULL,
296 _gtk_marshal_BOOLEAN__OBJECT_BOXED,
302 * GtkEntryCompletion::action-activated:
303 * @widget: the object which received the signal
304 * @index: the index of the activated action
306 * Gets emitted when an action is activated.
310 entry_completion_signals[ACTION_ACTIVATED] =
311 g_signal_new (I_("action-activated"),
312 G_TYPE_FROM_CLASS (klass),
314 G_STRUCT_OFFSET (GtkEntryCompletionClass, action_activated),
316 _gtk_marshal_VOID__INT,
320 g_object_class_install_property (object_class,
322 g_param_spec_object ("model",
323 P_("Completion Model"),
324 P_("The model to find matches in"),
326 GTK_PARAM_READWRITE));
327 g_object_class_install_property (object_class,
328 PROP_MINIMUM_KEY_LENGTH,
329 g_param_spec_int ("minimum-key-length",
330 P_("Minimum Key Length"),
331 P_("Minimum length of the search key in order to look up matches"),
335 GTK_PARAM_READWRITE));
337 * GtkEntryCompletion:text-column:
339 * The column of the model containing the strings.
340 * Note that the strings must be UTF-8.
344 g_object_class_install_property (object_class,
346 g_param_spec_int ("text-column",
348 P_("The column of the model containing the strings."),
352 GTK_PARAM_READWRITE));
355 * GtkEntryCompletion:inline-completion:
357 * Determines whether the common prefix of the possible completions
358 * should be inserted automatically in the entry. Note that this
359 * requires text-column to be set, even if you are using a custom
364 g_object_class_install_property (object_class,
365 PROP_INLINE_COMPLETION,
366 g_param_spec_boolean ("inline-completion",
367 P_("Inline completion"),
368 P_("Whether the common prefix should be inserted automatically"),
370 GTK_PARAM_READWRITE));
372 * GtkEntryCompletion:popup-completion:
374 * Determines whether the possible completions should be
375 * shown in a popup window.
379 g_object_class_install_property (object_class,
380 PROP_POPUP_COMPLETION,
381 g_param_spec_boolean ("popup-completion",
382 P_("Popup completion"),
383 P_("Whether the completions should be shown in a popup window"),
385 GTK_PARAM_READWRITE));
388 * GtkEntryCompletion:popup-set-width:
390 * Determines whether the completions popup window will be
391 * resized to the width of the entry.
395 g_object_class_install_property (object_class,
396 PROP_POPUP_SET_WIDTH,
397 g_param_spec_boolean ("popup-set-width",
398 P_("Popup set width"),
399 P_("If TRUE, the popup window will have the same size as the entry"),
401 GTK_PARAM_READWRITE));
404 * GtkEntryCompletion:popup-single-match:
406 * Determines whether the completions popup window will shown
407 * for a single possible completion. You probably want to set
408 * this to %FALSE if you are using
409 * <link linkend="GtkEntryCompletion--inline-completion">inline
414 g_object_class_install_property (object_class,
415 PROP_POPUP_SINGLE_MATCH,
416 g_param_spec_boolean ("popup-single-match",
417 P_("Popup single match"),
418 P_("If TRUE, the popup window will appear for a single match."),
420 GTK_PARAM_READWRITE));
422 * GtkEntryCompletion:inline-selection:
424 * Determines whether the possible completions on the popup
425 * will appear in the entry as you navigate through them.
429 g_object_class_install_property (object_class,
430 PROP_INLINE_SELECTION,
431 g_param_spec_boolean ("inline-selection",
432 P_("Inline selection"),
433 P_("Your description here"),
435 GTK_PARAM_READWRITE));
439 * GtkEntryCompletion:cell-area:
441 * The #GtkCellArea used to layout cell renderers in the treeview column.
443 * If no area is specified when creating the entry completion with gtk_entry_completion_new_with_area()
444 * a horizontally oriented #GtkCellAreaBox will be used.
448 g_object_class_install_property (object_class,
450 g_param_spec_object ("cell-area",
452 P_("The GtkCellArea used to layout cells"),
454 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
456 g_type_class_add_private (object_class, sizeof (GtkEntryCompletionPrivate));
461 gtk_entry_completion_buildable_custom_tag_end (GtkBuildable *buildable,
464 const gchar *tagname,
467 /* Just ignore the boolean return from here */
468 _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname, data);
472 gtk_entry_completion_buildable_init (GtkBuildableIface *iface)
474 iface->add_child = _gtk_cell_layout_buildable_add_child;
475 iface->custom_tag_start = _gtk_cell_layout_buildable_custom_tag_start;
476 iface->custom_tag_end = gtk_entry_completion_buildable_custom_tag_end;
480 gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface)
482 iface->get_area = gtk_entry_completion_get_area;
486 gtk_entry_completion_init (GtkEntryCompletion *completion)
488 GtkEntryCompletionPrivate *priv;
490 /* yes, also priv, need to keep the code readable */
491 completion->priv = G_TYPE_INSTANCE_GET_PRIVATE (completion,
492 GTK_TYPE_ENTRY_COMPLETION,
493 GtkEntryCompletionPrivate);
494 priv = completion->priv;
496 priv->minimum_key_length = 1;
497 priv->text_column = -1;
498 priv->has_completion = FALSE;
499 priv->inline_completion = FALSE;
500 priv->popup_completion = TRUE;
501 priv->popup_set_width = TRUE;
502 priv->popup_single_match = TRUE;
503 priv->inline_selection = FALSE;
505 priv->filter_model = NULL;
509 gtk_entry_completion_constructor (GType type,
510 guint n_construct_properties,
511 GObjectConstructParam *construct_properties)
513 GtkEntryCompletion *completion;
514 GtkEntryCompletionPrivate *priv;
516 GtkCellRenderer *cell;
517 GtkTreeSelection *sel;
518 GtkWidget *popup_frame;
520 object = G_OBJECT_CLASS (gtk_entry_completion_parent_class)->constructor
521 (type, n_construct_properties, construct_properties);
523 completion = (GtkEntryCompletion *) object;
524 priv = completion->priv;
526 if (!priv->cell_area)
528 priv->cell_area = gtk_cell_area_box_new ();
529 g_object_ref_sink (priv->cell_area);
533 priv->tree_view = gtk_tree_view_new ();
534 g_signal_connect (priv->tree_view, "button-press-event",
535 G_CALLBACK (gtk_entry_completion_list_button_press),
537 g_signal_connect (priv->tree_view, "enter-notify-event",
538 G_CALLBACK (gtk_entry_completion_list_enter_notify),
540 g_signal_connect (priv->tree_view, "motion-notify-event",
541 G_CALLBACK (gtk_entry_completion_list_motion_notify),
544 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
545 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view), TRUE);
547 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
548 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
549 gtk_tree_selection_unselect_all (sel);
550 g_signal_connect (sel, "changed",
551 G_CALLBACK (gtk_entry_completion_selection_changed),
553 priv->first_sel_changed = TRUE;
555 priv->column = gtk_tree_view_column_new_with_area (priv->cell_area);
556 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
558 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
559 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
561 GTK_POLICY_AUTOMATIC);
562 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
565 /* a nasty hack to get the completions treeview to size nicely */
566 gtk_widget_set_size_request (gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (priv->scrolled_window)),
570 priv->actions = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_BOOLEAN);
573 gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->actions));
574 g_object_ref_sink (priv->action_view);
575 g_signal_connect (priv->action_view, "button-press-event",
576 G_CALLBACK (gtk_entry_completion_action_button_press),
578 g_signal_connect (priv->action_view, "enter-notify-event",
579 G_CALLBACK (gtk_entry_completion_list_enter_notify),
581 g_signal_connect (priv->action_view, "motion-notify-event",
582 G_CALLBACK (gtk_entry_completion_list_motion_notify),
584 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->action_view), FALSE);
585 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->action_view), TRUE);
587 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->action_view));
588 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
589 gtk_tree_selection_unselect_all (sel);
591 cell = gtk_cell_renderer_text_new ();
592 gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (priv->action_view),
595 gtk_entry_completion_action_data_func,
600 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
601 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
602 gtk_window_set_type_hint (GTK_WINDOW(priv->popup_window),
603 GDK_WINDOW_TYPE_HINT_COMBO);
604 g_signal_connect (priv->popup_window, "key-press-event",
605 G_CALLBACK (gtk_entry_completion_popup_key_event),
607 g_signal_connect (priv->popup_window, "key-release-event",
608 G_CALLBACK (gtk_entry_completion_popup_key_event),
610 g_signal_connect (priv->popup_window, "button-press-event",
611 G_CALLBACK (gtk_entry_completion_popup_button_press),
614 popup_frame = gtk_frame_new (NULL);
615 gtk_frame_set_shadow_type (GTK_FRAME (popup_frame),
616 GTK_SHADOW_ETCHED_IN);
617 gtk_widget_show (popup_frame);
618 gtk_container_add (GTK_CONTAINER (priv->popup_window), popup_frame);
620 priv->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
621 gtk_container_add (GTK_CONTAINER (popup_frame), priv->vbox);
623 gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->tree_view);
624 gtk_box_pack_start (GTK_BOX (priv->vbox), priv->scrolled_window,
627 /* we don't want to see the action treeview when no actions have
628 * been inserted, so we pack the action treeview after the first
629 * action has been added
637 gtk_entry_completion_set_property (GObject *object,
642 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
643 GtkEntryCompletionPrivate *priv = completion->priv;
649 gtk_entry_completion_set_model (completion,
650 g_value_get_object (value));
653 case PROP_MINIMUM_KEY_LENGTH:
654 gtk_entry_completion_set_minimum_key_length (completion,
655 g_value_get_int (value));
658 case PROP_TEXT_COLUMN:
659 priv->text_column = g_value_get_int (value);
662 case PROP_INLINE_COMPLETION:
663 gtk_entry_completion_set_inline_completion (completion,
664 g_value_get_boolean (value));
667 case PROP_POPUP_COMPLETION:
668 gtk_entry_completion_set_popup_completion (completion,
669 g_value_get_boolean (value));
672 case PROP_POPUP_SET_WIDTH:
673 gtk_entry_completion_set_popup_set_width (completion,
674 g_value_get_boolean (value));
677 case PROP_POPUP_SINGLE_MATCH:
678 gtk_entry_completion_set_popup_single_match (completion,
679 g_value_get_boolean (value));
682 case PROP_INLINE_SELECTION:
683 gtk_entry_completion_set_inline_selection (completion,
684 g_value_get_boolean (value));
688 /* Construct-only, can only be assigned once */
689 area = g_value_get_object (value);
692 if (priv->cell_area != NULL)
694 g_warning ("cell-area has already been set, ignoring construct property");
695 g_object_ref_sink (area);
696 g_object_unref (area);
699 priv->cell_area = g_object_ref_sink (area);
704 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
710 gtk_entry_completion_get_property (GObject *object,
715 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
720 g_value_set_object (value,
721 gtk_entry_completion_get_model (completion));
724 case PROP_MINIMUM_KEY_LENGTH:
725 g_value_set_int (value, gtk_entry_completion_get_minimum_key_length (completion));
728 case PROP_TEXT_COLUMN:
729 g_value_set_int (value, gtk_entry_completion_get_text_column (completion));
732 case PROP_INLINE_COMPLETION:
733 g_value_set_boolean (value, gtk_entry_completion_get_inline_completion (completion));
736 case PROP_POPUP_COMPLETION:
737 g_value_set_boolean (value, gtk_entry_completion_get_popup_completion (completion));
740 case PROP_POPUP_SET_WIDTH:
741 g_value_set_boolean (value, gtk_entry_completion_get_popup_set_width (completion));
744 case PROP_POPUP_SINGLE_MATCH:
745 g_value_set_boolean (value, gtk_entry_completion_get_popup_single_match (completion));
748 case PROP_INLINE_SELECTION:
749 g_value_set_boolean (value, gtk_entry_completion_get_inline_selection (completion));
753 g_value_set_object (value, completion->priv->cell_area);
757 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
763 gtk_entry_completion_finalize (GObject *object)
765 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
766 GtkEntryCompletionPrivate *priv = completion->priv;
768 g_free (priv->case_normalized_key);
769 g_free (priv->completion_prefix);
771 if (priv->match_notify)
772 (* priv->match_notify) (priv->match_data);
774 G_OBJECT_CLASS (gtk_entry_completion_parent_class)->finalize (object);
778 gtk_entry_completion_dispose (GObject *object)
780 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
781 GtkEntryCompletionPrivate *priv = completion->priv;
785 gtk_widget_destroy (priv->tree_view);
786 priv->tree_view = NULL;
790 gtk_entry_set_completion (GTK_ENTRY (priv->entry), NULL);
794 g_object_unref (priv->actions);
795 priv->actions = NULL;
798 if (priv->action_view)
800 g_object_unref (priv->action_view);
801 priv->action_view = NULL;
804 if (priv->popup_window)
806 gtk_widget_destroy (priv->popup_window);
807 priv->popup_window = NULL;
812 g_object_unref (priv->cell_area);
813 priv->cell_area = NULL;
816 G_OBJECT_CLASS (gtk_entry_completion_parent_class)->dispose (object);
819 /* implement cell layout interface (only need to return the underlying cell area) */
821 gtk_entry_completion_get_area (GtkCellLayout *cell_layout)
823 GtkEntryCompletionPrivate *priv;
825 priv = GTK_ENTRY_COMPLETION (cell_layout)->priv;
827 if (G_UNLIKELY (!priv->cell_area))
829 priv->cell_area = gtk_cell_area_box_new ();
830 g_object_ref_sink (priv->cell_area);
833 return priv->cell_area;
836 /* all those callbacks */
838 gtk_entry_completion_default_completion_func (GtkEntryCompletion *completion,
844 gchar *normalized_string;
845 gchar *case_normalized_string;
847 gboolean ret = FALSE;
851 model = gtk_tree_model_filter_get_model (completion->priv->filter_model);
853 g_return_val_if_fail (gtk_tree_model_get_column_type (model, completion->priv->text_column) == G_TYPE_STRING,
856 gtk_tree_model_get (model, iter,
857 completion->priv->text_column, &item,
862 normalized_string = g_utf8_normalize (item, -1, G_NORMALIZE_ALL);
864 if (normalized_string != NULL)
866 case_normalized_string = g_utf8_casefold (normalized_string, -1);
868 if (!strncmp (key, case_normalized_string, strlen (key)))
871 g_free (case_normalized_string);
873 g_free (normalized_string);
881 gtk_entry_completion_visible_func (GtkTreeModel *model,
885 gboolean ret = FALSE;
887 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
889 if (!completion->priv->case_normalized_key)
892 if (completion->priv->match_func)
893 ret = (* completion->priv->match_func) (completion,
894 completion->priv->case_normalized_key,
896 completion->priv->match_data);
897 else if (completion->priv->text_column >= 0)
898 ret = gtk_entry_completion_default_completion_func (completion,
899 completion->priv->case_normalized_key,
907 gtk_entry_completion_popup_key_event (GtkWidget *widget,
911 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
913 if (!gtk_widget_get_mapped (completion->priv->popup_window))
916 /* propagate event to the entry */
917 gtk_widget_event (completion->priv->entry, (GdkEvent *)event);
923 gtk_entry_completion_popup_button_press (GtkWidget *widget,
924 GdkEventButton *event,
927 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
929 if (!gtk_widget_get_mapped (completion->priv->popup_window))
932 /* if we come here, it's usually time to popdown */
933 _gtk_entry_completion_popdown (completion);
939 gtk_entry_completion_list_button_press (GtkWidget *widget,
940 GdkEventButton *event,
943 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
944 GtkTreePath *path = NULL;
946 if (!gtk_widget_get_mapped (completion->priv->popup_window))
949 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
951 &path, NULL, NULL, NULL))
956 GtkTreeIter child_iter;
958 gtk_tree_model_get_iter (GTK_TREE_MODEL (completion->priv->filter_model),
960 gtk_tree_path_free (path);
961 gtk_tree_model_filter_convert_iter_to_child_iter (completion->priv->filter_model,
964 model = gtk_tree_model_filter_get_model (completion->priv->filter_model);
966 g_signal_handler_block (completion->priv->entry,
967 completion->priv->changed_id);
968 g_signal_emit (completion, entry_completion_signals[MATCH_SELECTED],
969 0, model, &child_iter, &entry_set);
970 g_signal_handler_unblock (completion->priv->entry,
971 completion->priv->changed_id);
973 _gtk_entry_completion_popdown (completion);
982 gtk_entry_completion_action_button_press (GtkWidget *widget,
983 GdkEventButton *event,
986 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
987 GtkTreePath *path = NULL;
989 if (!gtk_widget_get_mapped (completion->priv->popup_window))
992 gtk_entry_reset_im_context (GTK_ENTRY (completion->priv->entry));
994 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
996 &path, NULL, NULL, NULL))
998 g_signal_emit (completion, entry_completion_signals[ACTION_ACTIVATED],
999 0, gtk_tree_path_get_indices (path)[0]);
1000 gtk_tree_path_free (path);
1002 _gtk_entry_completion_popdown (completion);
1010 gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
1011 GtkCellRenderer *cell,
1012 GtkTreeModel *model,
1016 gchar *string = NULL;
1019 gtk_tree_model_get (model, iter,
1042 gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
1045 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1047 if (completion->priv->first_sel_changed)
1049 completion->priv->first_sel_changed = FALSE;
1050 if (gtk_widget_is_focus (completion->priv->tree_view))
1051 gtk_tree_selection_unselect_all (selection);
1058 * gtk_entry_completion_new:
1060 * Creates a new #GtkEntryCompletion object.
1062 * Return value: A newly created #GtkEntryCompletion object
1066 GtkEntryCompletion *
1067 gtk_entry_completion_new (void)
1069 GtkEntryCompletion *completion;
1071 completion = g_object_new (GTK_TYPE_ENTRY_COMPLETION, NULL);
1077 * gtk_entry_completion_new_with_area:
1078 * @area: the #GtkCellArea used to layout cells
1080 * Creates a new #GtkEntryCompletion object using the
1081 * specified @area to layout cells in the underlying
1082 * #GtkTreeViewColumn for the drop-down menu.
1084 * Return value: A newly created #GtkEntryCompletion object
1088 GtkEntryCompletion *
1089 gtk_entry_completion_new_with_area (GtkCellArea *area)
1091 GtkEntryCompletion *completion;
1093 completion = g_object_new (GTK_TYPE_ENTRY_COMPLETION, "cell-area", area, NULL);
1099 * gtk_entry_completion_get_entry:
1100 * @completion: a #GtkEntryCompletion
1102 * Gets the entry @completion has been attached to.
1104 * Return value: (transfer none): The entry @completion has been attached to
1109 gtk_entry_completion_get_entry (GtkEntryCompletion *completion)
1111 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
1113 return completion->priv->entry;
1117 * gtk_entry_completion_set_model:
1118 * @completion: a #GtkEntryCompletion
1119 * @model: (allow-none): the #GtkTreeModel
1121 * Sets the model for a #GtkEntryCompletion. If @completion already has
1122 * a model set, it will remove it before setting the new model.
1123 * If model is %NULL, then it will unset the model.
1128 gtk_entry_completion_set_model (GtkEntryCompletion *completion,
1129 GtkTreeModel *model)
1131 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1132 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
1136 gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
1138 _gtk_entry_completion_popdown (completion);
1139 completion->priv->filter_model = NULL;
1143 /* code will unref the old filter model (if any) */
1144 completion->priv->filter_model =
1145 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
1146 gtk_tree_model_filter_set_visible_func (completion->priv->filter_model,
1147 gtk_entry_completion_visible_func,
1151 gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
1152 GTK_TREE_MODEL (completion->priv->filter_model));
1153 g_object_unref (completion->priv->filter_model);
1155 g_object_notify (G_OBJECT (completion), "model");
1157 if (gtk_widget_get_visible (completion->priv->popup_window))
1158 _gtk_entry_completion_resize_popup (completion);
1162 * gtk_entry_completion_get_model:
1163 * @completion: a #GtkEntryCompletion
1165 * Returns the model the #GtkEntryCompletion is using as data source.
1166 * Returns %NULL if the model is unset.
1168 * Return value: (transfer none): A #GtkTreeModel, or %NULL if none
1169 * is currently being used
1174 gtk_entry_completion_get_model (GtkEntryCompletion *completion)
1176 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
1178 if (!completion->priv->filter_model)
1181 return gtk_tree_model_filter_get_model (completion->priv->filter_model);
1185 * gtk_entry_completion_set_match_func:
1186 * @completion: a #GtkEntryCompletion
1187 * @func: the #GtkEntryCompletionMatchFunc to use
1188 * @func_data: user data for @func
1189 * @func_notify: destroy notify for @func_data.
1191 * Sets the match function for @completion to be @func. The match function
1192 * is used to determine if a row should or should not be in the completion
1198 gtk_entry_completion_set_match_func (GtkEntryCompletion *completion,
1199 GtkEntryCompletionMatchFunc func,
1201 GDestroyNotify func_notify)
1203 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1205 if (completion->priv->match_notify)
1206 (* completion->priv->match_notify) (completion->priv->match_data);
1208 completion->priv->match_func = func;
1209 completion->priv->match_data = func_data;
1210 completion->priv->match_notify = func_notify;
1214 * gtk_entry_completion_set_minimum_key_length:
1215 * @completion: a #GtkEntryCompletion
1216 * @length: the minimum length of the key in order to start completing
1218 * Requires the length of the search key for @completion to be at least
1219 * @length. This is useful for long lists, where completing using a small
1220 * key takes a lot of time and will come up with meaningless results anyway
1221 * (ie, a too large dataset).
1226 gtk_entry_completion_set_minimum_key_length (GtkEntryCompletion *completion,
1229 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1230 g_return_if_fail (length >= 0);
1232 if (completion->priv->minimum_key_length != length)
1234 completion->priv->minimum_key_length = length;
1236 g_object_notify (G_OBJECT (completion), "minimum-key-length");
1241 * gtk_entry_completion_get_minimum_key_length:
1242 * @completion: a #GtkEntryCompletion
1244 * Returns the minimum key length as set for @completion.
1246 * Return value: The currently used minimum key length
1251 gtk_entry_completion_get_minimum_key_length (GtkEntryCompletion *completion)
1253 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), 0);
1255 return completion->priv->minimum_key_length;
1259 * gtk_entry_completion_complete:
1260 * @completion: a #GtkEntryCompletion
1262 * Requests a completion operation, or in other words a refiltering of the
1263 * current list with completions, using the current key. The completion list
1264 * view will be updated accordingly.
1269 gtk_entry_completion_complete (GtkEntryCompletion *completion)
1273 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1274 g_return_if_fail (GTK_IS_ENTRY (completion->priv->entry));
1276 if (!completion->priv->filter_model)
1279 g_free (completion->priv->case_normalized_key);
1281 tmp = g_utf8_normalize (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)),
1282 -1, G_NORMALIZE_ALL);
1283 completion->priv->case_normalized_key = g_utf8_casefold (tmp, -1);
1286 gtk_tree_model_filter_refilter (completion->priv->filter_model);
1288 if (gtk_widget_get_visible (completion->priv->popup_window))
1289 _gtk_entry_completion_resize_popup (completion);
1293 gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
1295 const gchar *string,
1300 gtk_list_store_insert (completion->priv->actions, &iter, index);
1301 gtk_list_store_set (completion->priv->actions, &iter,
1306 if (!gtk_widget_get_parent (completion->priv->action_view))
1308 GtkTreePath *path = gtk_tree_path_new_from_indices (0, -1);
1310 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
1312 gtk_tree_path_free (path);
1314 gtk_box_pack_start (GTK_BOX (completion->priv->vbox),
1315 completion->priv->action_view, FALSE, FALSE, 0);
1316 gtk_widget_show (completion->priv->action_view);
1321 * gtk_entry_completion_insert_action_text:
1322 * @completion: a #GtkEntryCompletion
1323 * @index_: the index of the item to insert
1324 * @text: text of the item to insert
1326 * Inserts an action in @completion's action item list at position @index_
1327 * with text @text. If you want the action item to have markup, use
1328 * gtk_entry_completion_insert_action_markup().
1333 gtk_entry_completion_insert_action_text (GtkEntryCompletion *completion,
1337 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1338 g_return_if_fail (text != NULL);
1340 gtk_entry_completion_insert_action (completion, index_, text, FALSE);
1344 * gtk_entry_completion_insert_action_markup:
1345 * @completion: a #GtkEntryCompletion
1346 * @index_: the index of the item to insert
1347 * @markup: markup of the item to insert
1349 * Inserts an action in @completion's action item list at position @index_
1350 * with markup @markup.
1355 gtk_entry_completion_insert_action_markup (GtkEntryCompletion *completion,
1357 const gchar *markup)
1359 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1360 g_return_if_fail (markup != NULL);
1362 gtk_entry_completion_insert_action (completion, index_, markup, TRUE);
1366 * gtk_entry_completion_delete_action:
1367 * @completion: a #GtkEntryCompletion
1368 * @index_: the index of the item to delete
1370 * Deletes the action at @index_ from @completion's action list.
1375 gtk_entry_completion_delete_action (GtkEntryCompletion *completion,
1380 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1381 g_return_if_fail (index_ >= 0);
1383 gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (completion->priv->actions),
1384 &iter, NULL, index_);
1385 gtk_list_store_remove (completion->priv->actions, &iter);
1389 * gtk_entry_completion_set_text_column:
1390 * @completion: a #GtkEntryCompletion
1391 * @column: the column in the model of @completion to get strings from
1393 * Convenience function for setting up the most used case of this code: a
1394 * completion list with just strings. This function will set up @completion
1395 * to have a list displaying all (and just) strings in the completion list,
1396 * and to get those strings from @column in the model of @completion.
1398 * This functions creates and adds a #GtkCellRendererText for the selected
1399 * column. If you need to set the text column, but don't want the cell
1400 * renderer, use g_object_set() to set the #GtkEntryCompletion:text-column
1401 * property directly.
1406 gtk_entry_completion_set_text_column (GtkEntryCompletion *completion,
1409 GtkCellRenderer *cell;
1411 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1412 g_return_if_fail (column >= 0);
1414 completion->priv->text_column = column;
1416 cell = gtk_cell_renderer_text_new ();
1417 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion),
1419 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (completion),
1423 g_object_notify (G_OBJECT (completion), "text-column");
1427 * gtk_entry_completion_get_text_column:
1428 * @completion: a #GtkEntryCompletion
1430 * Returns the column in the model of @completion to get strings from.
1432 * Return value: the column containing the strings
1437 gtk_entry_completion_get_text_column (GtkEntryCompletion *completion)
1439 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), -1);
1441 return completion->priv->text_column;
1447 gtk_entry_completion_list_enter_notify (GtkWidget *widget,
1448 GdkEventCrossing *event,
1451 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1453 return completion->priv->ignore_enter;
1457 gtk_entry_completion_list_motion_notify (GtkWidget *widget,
1458 GdkEventMotion *event,
1461 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1463 completion->priv->ignore_enter = FALSE;
1469 /* some nasty size requisition */
1471 _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
1473 GtkAllocation allocation;
1475 gint matches, actions, items, height;
1478 gint vertical_separator;
1479 GdkRectangle monitor;
1481 GtkRequisition popup_req;
1482 GtkRequisition entry_req;
1486 GtkTreeViewColumn *action_column;
1489 window = gtk_widget_get_window (completion->priv->entry);
1494 if (!completion->priv->filter_model)
1497 gtk_widget_get_allocation (completion->priv->entry, &allocation);
1498 gtk_widget_get_preferred_size (completion->priv->entry,
1501 gdk_window_get_origin (window, &x, &y);
1503 y += allocation.y + (allocation.height - entry_req.height) / 2;
1505 matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
1506 actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
1507 action_column = gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0);
1509 gtk_tree_view_column_cell_get_size (completion->priv->column, NULL,
1510 NULL, NULL, NULL, &height);
1511 gtk_tree_view_column_cell_get_size (action_column, NULL,
1512 NULL, NULL, NULL, &action_height);
1514 gtk_widget_style_get (GTK_WIDGET (completion->priv->tree_view),
1515 "vertical-separator", &vertical_separator,
1518 height += vertical_separator;
1520 gtk_widget_realize (completion->priv->tree_view);
1522 screen = gtk_widget_get_screen (GTK_WIDGET (completion->priv->entry));
1523 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
1524 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
1528 else if (y > monitor.height / 2)
1529 items = MIN (matches, (((monitor.y + y) - (actions * action_height)) / height) - 1);
1531 items = MIN (matches, (((monitor.height - y) - (actions * action_height)) / height) - 1);
1534 gtk_widget_hide (completion->priv->scrolled_window);
1536 gtk_widget_show (completion->priv->scrolled_window);
1538 if (completion->priv->popup_set_width)
1539 width = MIN (allocation.width, monitor.width);
1543 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (completion->priv->tree_view));
1544 gtk_scrolled_window_set_min_content_width (GTK_SCROLLED_WINDOW (completion->priv->scrolled_window), width);
1545 gtk_widget_set_size_request (completion->priv->popup_window, width, -1);
1546 gtk_scrolled_window_set_min_content_height (GTK_SCROLLED_WINDOW (completion->priv->scrolled_window), items * height);
1549 gtk_widget_show (completion->priv->action_view);
1551 gtk_widget_hide (completion->priv->action_view);
1553 gtk_widget_get_preferred_size (completion->priv->popup_window,
1558 else if (x + popup_req.width > monitor.x + monitor.width)
1559 x = monitor.x + monitor.width - popup_req.width;
1561 if (y + entry_req.height + popup_req.height <= monitor.y + monitor.height ||
1562 y - monitor.y < (monitor.y + monitor.height) - (y + entry_req.height))
1564 y += entry_req.height;
1569 y -= popup_req.height;
1575 path = gtk_tree_path_new_from_indices (above ? matches - 1 : 0, -1);
1576 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (completion->priv->tree_view), path,
1577 NULL, FALSE, 0.0, 0.0);
1578 gtk_tree_path_free (path);
1581 gtk_window_move (GTK_WINDOW (completion->priv->popup_window), x, y);
1587 gtk_entry_completion_popup (GtkEntryCompletion *completion)
1589 GtkTreeViewColumn *column;
1590 GtkStyleContext *context;
1593 GtkWidget *toplevel;
1595 if (gtk_widget_get_mapped (completion->priv->popup_window))
1598 if (!gtk_widget_get_mapped (completion->priv->entry))
1601 if (!gtk_widget_has_focus (completion->priv->entry))
1604 if (completion->priv->has_grab)
1607 completion->priv->ignore_enter = TRUE;
1609 column = gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0);
1610 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
1612 context = gtk_widget_get_style_context (completion->priv->tree_view);
1613 gtk_style_context_get_background_color (context, 0, &color);
1615 g_object_set (GTK_CELL_RENDERER (renderers->data),
1616 "cell-background-rgba", &color,
1618 g_list_free (renderers);
1620 gtk_widget_show_all (completion->priv->vbox);
1622 /* default on no match */
1623 completion->priv->current_selected = -1;
1625 _gtk_entry_completion_resize_popup (completion);
1627 toplevel = gtk_widget_get_toplevel (completion->priv->entry);
1628 if (GTK_IS_WINDOW (toplevel))
1629 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1630 GTK_WINDOW (completion->priv->popup_window));
1632 /* prevent the first row being focused */
1633 gtk_widget_grab_focus (completion->priv->tree_view);
1635 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
1636 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
1638 gtk_window_set_screen (GTK_WINDOW (completion->priv->popup_window),
1639 gtk_widget_get_screen (completion->priv->entry));
1641 gtk_widget_show (completion->priv->popup_window);
1643 gtk_device_grab_add (completion->priv->popup_window, completion->priv->device, TRUE);
1644 gdk_device_grab (completion->priv->device, gtk_widget_get_window (completion->priv->popup_window),
1645 GDK_OWNERSHIP_WINDOW, TRUE,
1646 GDK_BUTTON_PRESS_MASK |
1647 GDK_BUTTON_RELEASE_MASK |
1648 GDK_POINTER_MOTION_MASK,
1649 NULL, GDK_CURRENT_TIME);
1651 completion->priv->has_grab = TRUE;
1655 _gtk_entry_completion_popdown (GtkEntryCompletion *completion)
1657 if (!gtk_widget_get_mapped (completion->priv->popup_window))
1660 completion->priv->ignore_enter = FALSE;
1662 if (completion->priv->has_grab)
1664 gdk_device_ungrab (completion->priv->device, GDK_CURRENT_TIME);
1665 gtk_device_grab_remove (completion->priv->popup_window,
1666 completion->priv->device);
1667 completion->priv->has_grab = FALSE;
1670 gtk_widget_hide (completion->priv->popup_window);
1674 gtk_entry_completion_match_selected (GtkEntryCompletion *completion,
1675 GtkTreeModel *model,
1680 gtk_tree_model_get (model, iter, completion->priv->text_column, &str, -1);
1681 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), str ? str : "");
1683 /* move cursor to the end */
1684 gtk_editable_set_position (GTK_EDITABLE (completion->priv->entry), -1);
1692 gtk_entry_completion_cursor_on_match (GtkEntryCompletion *completion,
1693 GtkTreeModel *model,
1696 gtk_entry_completion_insert_completion (completion, model, iter);
1702 * gtk_entry_completion_compute_prefix:
1703 * @completion: the entry completion
1704 * @key: The text to complete for
1706 * Computes the common prefix that is shared by all rows in @completion
1707 * that start with @key. If no row matches @key, %NULL will be returned.
1708 * Note that a text column must have been set for this function to work,
1709 * see gtk_entry_completion_set_text_column() for details.
1711 * Returns: (transfer full): The common prefix all rows starting with @key
1712 * or %NULL if no row matches @key.
1717 gtk_entry_completion_compute_prefix (GtkEntryCompletion *completion,
1721 gchar *prefix = NULL;
1724 if (completion->priv->text_column < 0)
1727 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (completion->priv->filter_model),
1734 gtk_tree_model_get (GTK_TREE_MODEL (completion->priv->filter_model),
1735 &iter, completion->priv->text_column, &text,
1738 if (text && g_str_has_prefix (text, key))
1741 prefix = g_strdup (text);
1747 while (*p && *p == *q)
1757 /* strip a partial multibyte character */
1758 q = g_utf8_find_prev_char (prefix, p);
1759 switch (g_utf8_get_char_validated (q, p - q))
1771 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (completion->priv->filter_model),
1780 gtk_entry_completion_real_insert_prefix (GtkEntryCompletion *completion,
1781 const gchar *prefix)
1789 prefix_len = g_utf8_strlen (prefix, -1);
1791 key = gtk_entry_get_text (GTK_ENTRY (completion->priv->entry));
1792 key_len = g_utf8_strlen (key, -1);
1794 if (prefix_len > key_len)
1796 gint pos = prefix_len;
1798 gtk_editable_insert_text (GTK_EDITABLE (completion->priv->entry),
1799 prefix + strlen (key), -1, &pos);
1800 gtk_editable_select_region (GTK_EDITABLE (completion->priv->entry),
1801 key_len, prefix_len);
1803 completion->priv->has_completion = TRUE;
1811 * gtk_entry_completion_get_completion_prefix:
1812 * @completion: a #GtkEntryCompletion
1814 * Get the original text entered by the user that triggered
1815 * the completion or %NULL if there's no completion ongoing.
1817 * Returns: the prefix for the current completion
1822 gtk_entry_completion_get_completion_prefix (GtkEntryCompletion *completion)
1824 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
1826 return completion->priv->completion_prefix;
1830 gtk_entry_completion_insert_completion_text (GtkEntryCompletion *completion,
1833 GtkEntryCompletionPrivate *priv = completion->priv;
1836 priv = completion->priv;
1838 if (priv->changed_id > 0)
1839 g_signal_handler_block (priv->entry, priv->changed_id);
1841 if (priv->insert_text_id > 0)
1842 g_signal_handler_block (priv->entry, priv->insert_text_id);
1844 gtk_entry_set_text (GTK_ENTRY (priv->entry), text);
1846 len = strlen (priv->completion_prefix);
1847 gtk_editable_select_region (GTK_EDITABLE (priv->entry), len, -1);
1849 if (priv->changed_id > 0)
1850 g_signal_handler_unblock (priv->entry, priv->changed_id);
1852 if (priv->insert_text_id > 0)
1853 g_signal_handler_unblock (priv->entry, priv->insert_text_id);
1857 gtk_entry_completion_insert_completion (GtkEntryCompletion *completion,
1858 GtkTreeModel *model,
1863 if (completion->priv->text_column < 0)
1866 gtk_tree_model_get (model, iter,
1867 completion->priv->text_column, &str,
1870 gtk_entry_completion_insert_completion_text (completion, str);
1878 * gtk_entry_completion_insert_prefix:
1879 * @completion: a #GtkEntryCompletion
1881 * Requests a prefix insertion.
1886 gtk_entry_completion_insert_prefix (GtkEntryCompletion *completion)
1891 if (completion->priv->insert_text_id > 0)
1892 g_signal_handler_block (completion->priv->entry,
1893 completion->priv->insert_text_id);
1895 prefix = gtk_entry_completion_compute_prefix (completion,
1896 gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)));
1900 g_signal_emit (completion, entry_completion_signals[INSERT_PREFIX],
1905 if (completion->priv->insert_text_id > 0)
1906 g_signal_handler_unblock (completion->priv->entry,
1907 completion->priv->insert_text_id);
1911 * gtk_entry_completion_set_inline_completion:
1912 * @completion: a #GtkEntryCompletion
1913 * @inline_completion: %TRUE to do inline completion
1915 * Sets whether the common prefix of the possible completions should
1916 * be automatically inserted in the entry.
1921 gtk_entry_completion_set_inline_completion (GtkEntryCompletion *completion,
1922 gboolean inline_completion)
1924 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1926 inline_completion = inline_completion != FALSE;
1928 if (completion->priv->inline_completion != inline_completion)
1930 completion->priv->inline_completion = inline_completion;
1932 g_object_notify (G_OBJECT (completion), "inline-completion");
1937 * gtk_entry_completion_get_inline_completion:
1938 * @completion: a #GtkEntryCompletion
1940 * Returns whether the common prefix of the possible completions should
1941 * be automatically inserted in the entry.
1943 * Return value: %TRUE if inline completion is turned on
1948 gtk_entry_completion_get_inline_completion (GtkEntryCompletion *completion)
1950 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), FALSE);
1952 return completion->priv->inline_completion;
1956 * gtk_entry_completion_set_popup_completion:
1957 * @completion: a #GtkEntryCompletion
1958 * @popup_completion: %TRUE to do popup completion
1960 * Sets whether the completions should be presented in a popup window.
1965 gtk_entry_completion_set_popup_completion (GtkEntryCompletion *completion,
1966 gboolean popup_completion)
1968 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1970 popup_completion = popup_completion != FALSE;
1972 if (completion->priv->popup_completion != popup_completion)
1974 completion->priv->popup_completion = popup_completion;
1976 g_object_notify (G_OBJECT (completion), "popup-completion");
1982 * gtk_entry_completion_get_popup_completion:
1983 * @completion: a #GtkEntryCompletion
1985 * Returns whether the completions should be presented in a popup window.
1987 * Return value: %TRUE if popup completion is turned on
1992 gtk_entry_completion_get_popup_completion (GtkEntryCompletion *completion)
1994 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), TRUE);
1996 return completion->priv->popup_completion;
2000 * gtk_entry_completion_set_popup_set_width:
2001 * @completion: a #GtkEntryCompletion
2002 * @popup_set_width: %TRUE to make the width of the popup the same as the entry
2004 * Sets whether the completion popup window will be resized to be the same
2005 * width as the entry.
2010 gtk_entry_completion_set_popup_set_width (GtkEntryCompletion *completion,
2011 gboolean popup_set_width)
2013 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
2015 popup_set_width = popup_set_width != FALSE;
2017 if (completion->priv->popup_set_width != popup_set_width)
2019 completion->priv->popup_set_width = popup_set_width;
2021 g_object_notify (G_OBJECT (completion), "popup-set-width");
2026 * gtk_entry_completion_get_popup_set_width:
2027 * @completion: a #GtkEntryCompletion
2029 * Returns whether the completion popup window will be resized to the
2030 * width of the entry.
2032 * Return value: %TRUE if the popup window will be resized to the width of
2038 gtk_entry_completion_get_popup_set_width (GtkEntryCompletion *completion)
2040 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), TRUE);
2042 return completion->priv->popup_set_width;
2047 * gtk_entry_completion_set_popup_single_match:
2048 * @completion: a #GtkEntryCompletion
2049 * @popup_single_match: %TRUE if the popup should appear even for a single
2052 * Sets whether the completion popup window will appear even if there is
2053 * only a single match. You may want to set this to %FALSE if you
2054 * are using <link linkend="GtkEntryCompletion--inline-completion">inline
2055 * completion</link>.
2060 gtk_entry_completion_set_popup_single_match (GtkEntryCompletion *completion,
2061 gboolean popup_single_match)
2063 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
2065 popup_single_match = popup_single_match != FALSE;
2067 if (completion->priv->popup_single_match != popup_single_match)
2069 completion->priv->popup_single_match = popup_single_match;
2071 g_object_notify (G_OBJECT (completion), "popup-single-match");
2076 * gtk_entry_completion_get_popup_single_match:
2077 * @completion: a #GtkEntryCompletion
2079 * Returns whether the completion popup window will appear even if there is
2080 * only a single match.
2082 * Return value: %TRUE if the popup window will appear regardless of the
2088 gtk_entry_completion_get_popup_single_match (GtkEntryCompletion *completion)
2090 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), TRUE);
2092 return completion->priv->popup_single_match;
2096 * gtk_entry_completion_set_inline_selection:
2097 * @completion: a #GtkEntryCompletion
2098 * @inline_selection: %TRUE to do inline selection
2100 * Sets whether it is possible to cycle through the possible completions
2106 gtk_entry_completion_set_inline_selection (GtkEntryCompletion *completion,
2107 gboolean inline_selection)
2109 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
2111 inline_selection = inline_selection != FALSE;
2113 if (completion->priv->inline_selection != inline_selection)
2115 completion->priv->inline_selection = inline_selection;
2117 g_object_notify (G_OBJECT (completion), "inline-selection");
2122 * gtk_entry_completion_get_inline_selection:
2123 * @completion: a #GtkEntryCompletion
2125 * Returns %TRUE if inline-selection mode is turned on.
2127 * Returns: %TRUE if inline-selection mode is on
2132 gtk_entry_completion_get_inline_selection (GtkEntryCompletion *completion)
2134 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), FALSE);
2136 return completion->priv->inline_selection;
2141 gtk_entry_completion_timeout (gpointer data)
2143 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
2145 completion->priv->completion_timeout = 0;
2147 if (completion->priv->filter_model &&
2148 g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)), -1)
2149 >= completion->priv->minimum_key_length)
2153 GtkTreeSelection *s;
2154 gboolean popup_single;
2156 gtk_entry_completion_complete (completion);
2157 matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
2158 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
2160 s = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
2162 gtk_tree_selection_unselect_all (s);
2164 actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
2166 g_object_get (completion, "popup-single-match", &popup_single, NULL);
2167 if ((matches > (popup_single ? 0: 1)) || actions > 0)
2169 if (gtk_widget_get_visible (completion->priv->popup_window))
2170 _gtk_entry_completion_resize_popup (completion);
2172 gtk_entry_completion_popup (completion);
2175 _gtk_entry_completion_popdown (completion);
2177 else if (gtk_widget_get_visible (completion->priv->popup_window))
2178 _gtk_entry_completion_popdown (completion);
2183 static inline gboolean
2184 keyval_is_cursor_move (guint keyval)
2186 if (keyval == GDK_KEY_Up || keyval == GDK_KEY_KP_Up)
2189 if (keyval == GDK_KEY_Down || keyval == GDK_KEY_KP_Down)
2192 if (keyval == GDK_KEY_Page_Up)
2195 if (keyval == GDK_KEY_Page_Down)
2202 gtk_entry_completion_key_press (GtkWidget *widget,
2206 gint matches, actions = 0;
2207 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
2209 if (!completion->priv->popup_completion)
2212 if (event->keyval == GDK_KEY_Return ||
2213 event->keyval == GDK_KEY_KP_Enter ||
2214 event->keyval == GDK_KEY_ISO_Enter ||
2215 event->keyval == GDK_KEY_Escape)
2217 if (completion && completion->priv->completion_timeout)
2219 g_source_remove (completion->priv->completion_timeout);
2220 completion->priv->completion_timeout = 0;
2224 if (!gtk_widget_get_mapped (completion->priv->popup_window))
2227 matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
2229 if (completion->priv->actions)
2230 actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
2232 if (keyval_is_cursor_move (event->keyval))
2234 GtkTreePath *path = NULL;
2236 if (event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_KP_Up)
2238 if (completion->priv->current_selected < 0)
2239 completion->priv->current_selected = matches + actions - 1;
2241 completion->priv->current_selected--;
2243 else if (event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_KP_Down)
2245 if (completion->priv->current_selected < matches + actions - 1)
2246 completion->priv->current_selected++;
2248 completion->priv->current_selected = -1;
2250 else if (event->keyval == GDK_KEY_Page_Up)
2252 if (completion->priv->current_selected < 0)
2253 completion->priv->current_selected = matches + actions - 1;
2254 else if (completion->priv->current_selected == 0)
2255 completion->priv->current_selected = -1;
2256 else if (completion->priv->current_selected < matches)
2258 completion->priv->current_selected -= PAGE_STEP;
2259 if (completion->priv->current_selected < 0)
2260 completion->priv->current_selected = 0;
2264 completion->priv->current_selected -= PAGE_STEP;
2265 if (completion->priv->current_selected < matches - 1)
2266 completion->priv->current_selected = matches - 1;
2269 else if (event->keyval == GDK_KEY_Page_Down)
2271 if (completion->priv->current_selected < 0)
2272 completion->priv->current_selected = 0;
2273 else if (completion->priv->current_selected < matches - 1)
2275 completion->priv->current_selected += PAGE_STEP;
2276 if (completion->priv->current_selected > matches - 1)
2277 completion->priv->current_selected = matches - 1;
2279 else if (completion->priv->current_selected == matches + actions - 1)
2281 completion->priv->current_selected = -1;
2285 completion->priv->current_selected += PAGE_STEP;
2286 if (completion->priv->current_selected > matches + actions - 1)
2287 completion->priv->current_selected = matches + actions - 1;
2291 if (completion->priv->current_selected < 0)
2293 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
2294 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
2296 if (completion->priv->inline_selection &&
2297 completion->priv->completion_prefix)
2299 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry),
2300 completion->priv->completion_prefix);
2301 gtk_editable_set_position (GTK_EDITABLE (widget), -1);
2304 else if (completion->priv->current_selected < matches)
2306 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
2308 path = gtk_tree_path_new_from_indices (completion->priv->current_selected, -1);
2309 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->tree_view),
2312 if (completion->priv->inline_selection)
2316 GtkTreeIter child_iter;
2317 GtkTreeModel *model = NULL;
2318 GtkTreeSelection *sel;
2321 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
2322 if (!gtk_tree_selection_get_selected (sel, &model, &iter))
2324 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_iter, &iter);
2325 model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
2327 if (completion->priv->completion_prefix == NULL)
2328 completion->priv->completion_prefix = g_strdup (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)));
2330 g_signal_emit_by_name (completion, "cursor-on-match", model,
2331 &child_iter, &entry_set);
2334 else if (completion->priv->current_selected - matches >= 0)
2336 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
2338 path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
2339 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
2342 if (completion->priv->inline_selection &&
2343 completion->priv->completion_prefix)
2345 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry),
2346 completion->priv->completion_prefix);
2347 gtk_editable_set_position (GTK_EDITABLE (widget), -1);
2351 gtk_tree_path_free (path);
2355 else if (event->keyval == GDK_KEY_Escape ||
2356 event->keyval == GDK_KEY_Left ||
2357 event->keyval == GDK_KEY_KP_Left ||
2358 event->keyval == GDK_KEY_Right ||
2359 event->keyval == GDK_KEY_KP_Right)
2361 gboolean retval = TRUE;
2363 gtk_entry_reset_im_context (GTK_ENTRY (widget));
2364 _gtk_entry_completion_popdown (completion);
2366 if (completion->priv->current_selected < 0)
2369 goto keypress_completion_out;
2371 else if (completion->priv->inline_selection)
2373 /* Escape rejects the tentative completion */
2374 if (event->keyval == GDK_KEY_Escape)
2376 if (completion->priv->completion_prefix)
2377 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry),
2378 completion->priv->completion_prefix);
2380 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), "");
2383 /* Move the cursor to the end for Right/Esc */
2384 if (event->keyval == GDK_KEY_Right ||
2385 event->keyval == GDK_KEY_KP_Right ||
2386 event->keyval == GDK_KEY_Escape)
2387 gtk_editable_set_position (GTK_EDITABLE (widget), -1);
2388 /* Let the default keybindings run for Left, i.e. either move to the
2389 * * previous character or select word if a modifier is used */
2394 keypress_completion_out:
2395 if (completion->priv->inline_selection)
2397 g_free (completion->priv->completion_prefix);
2398 completion->priv->completion_prefix = NULL;
2403 else if (event->keyval == GDK_KEY_Tab ||
2404 event->keyval == GDK_KEY_KP_Tab ||
2405 event->keyval == GDK_KEY_ISO_Left_Tab)
2407 gtk_entry_reset_im_context (GTK_ENTRY (widget));
2408 _gtk_entry_completion_popdown (completion);
2410 g_free (completion->priv->completion_prefix);
2411 completion->priv->completion_prefix = NULL;
2415 else if (event->keyval == GDK_KEY_ISO_Enter ||
2416 event->keyval == GDK_KEY_KP_Enter ||
2417 event->keyval == GDK_KEY_Return)
2420 GtkTreeModel *model = NULL;
2421 GtkTreeModel *child_model;
2422 GtkTreeIter child_iter;
2423 GtkTreeSelection *sel;
2424 gboolean retval = TRUE;
2426 gtk_entry_reset_im_context (GTK_ENTRY (widget));
2427 _gtk_entry_completion_popdown (completion);
2429 if (completion->priv->current_selected < matches)
2433 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
2434 if (gtk_tree_selection_get_selected (sel, &model, &iter))
2436 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_iter, &iter);
2437 child_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
2438 g_signal_handler_block (widget, completion->priv->changed_id);
2439 g_signal_emit_by_name (completion, "match-selected",
2440 child_model, &child_iter, &entry_set);
2441 g_signal_handler_unblock (widget, completion->priv->changed_id);
2447 gtk_tree_model_get (model, &iter,
2448 completion->priv->text_column, &str,
2451 gtk_entry_set_text (GTK_ENTRY (widget), str);
2453 /* move the cursor to the end */
2454 gtk_editable_set_position (GTK_EDITABLE (widget), -1);
2461 else if (completion->priv->current_selected - matches >= 0)
2463 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
2464 if (gtk_tree_selection_get_selected (sel, &model, &iter))
2468 path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
2469 g_signal_emit_by_name (completion, "action-activated",
2470 gtk_tree_path_get_indices (path)[0]);
2471 gtk_tree_path_free (path);
2477 g_free (completion->priv->completion_prefix);
2478 completion->priv->completion_prefix = NULL;
2487 gtk_entry_completion_changed (GtkWidget *widget,
2490 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
2491 GtkEntry *entry = GTK_ENTRY (widget);
2494 if (!completion->priv->popup_completion)
2497 /* (re)install completion timeout */
2498 if (completion->priv->completion_timeout)
2499 g_source_remove (completion->priv->completion_timeout);
2501 if (!gtk_entry_get_text (entry))
2504 /* no need to normalize for this test */
2505 if (completion->priv->minimum_key_length > 0 &&
2506 strcmp ("", gtk_entry_get_text (entry)) == 0)
2508 if (gtk_widget_get_visible (completion->priv->popup_window))
2509 _gtk_entry_completion_popdown (completion);
2513 device = gtk_get_current_event_device ();
2515 if (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
2516 device = gdk_device_get_associated_device (device);
2519 completion->priv->device = device;
2521 completion->priv->completion_timeout =
2522 gdk_threads_add_timeout (COMPLETION_TIMEOUT,
2523 gtk_entry_completion_timeout,
2528 check_completion_callback (GtkEntryCompletion *completion)
2530 completion->priv->check_completion_idle = NULL;
2532 gtk_entry_completion_complete (completion);
2533 gtk_entry_completion_insert_prefix (completion);
2539 clear_completion_callback (GtkEntry *entry,
2542 GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
2544 if (!completion->priv->inline_completion)
2547 if (pspec->name == I_("cursor-position") ||
2548 pspec->name == I_("selection-bound"))
2549 completion->priv->has_completion = FALSE;
2553 accept_completion_callback (GtkEntry *entry)
2555 GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
2557 if (!completion->priv->inline_completion)
2560 if (completion->priv->has_completion)
2561 gtk_editable_set_position (GTK_EDITABLE (entry),
2562 gtk_entry_buffer_get_length (gtk_entry_get_buffer (entry)));
2568 completion_insert_text_callback (GtkEntry *entry,
2572 GtkEntryCompletion *completion)
2574 if (!completion->priv->inline_completion)
2577 /* idle to update the selection based on the file list */
2578 if (completion->priv->check_completion_idle == NULL)
2580 completion->priv->check_completion_idle = g_idle_source_new ();
2581 g_source_set_priority (completion->priv->check_completion_idle, G_PRIORITY_HIGH);
2582 g_source_set_closure (completion->priv->check_completion_idle,
2583 g_cclosure_new_object (G_CALLBACK (check_completion_callback),
2584 G_OBJECT (completion)));
2585 g_source_attach (completion->priv->check_completion_idle, NULL);
2590 connect_completion_signals (GtkEntryCompletion *completion)
2592 completion->priv->changed_id =
2593 g_signal_connect (completion->priv->entry, "changed",
2594 G_CALLBACK (gtk_entry_completion_changed), completion);
2595 g_signal_connect (completion->priv->entry, "key-press-event",
2596 G_CALLBACK (gtk_entry_completion_key_press), completion);
2598 completion->priv->insert_text_id =
2599 g_signal_connect (completion->priv->entry, "insert-text",
2600 G_CALLBACK (completion_insert_text_callback), completion);
2601 g_signal_connect (completion->priv->entry, "notify",
2602 G_CALLBACK (clear_completion_callback), completion);
2603 g_signal_connect (completion->priv->entry, "activate",
2604 G_CALLBACK (accept_completion_callback), completion);
2605 g_signal_connect (completion->priv->entry, "focus-out-event",
2606 G_CALLBACK (accept_completion_callback), completion);
2610 set_accessible_relation (GtkWidget *window,
2613 AtkObject *window_accessible;
2614 AtkObject *entry_accessible;
2616 window_accessible = gtk_widget_get_accessible (window);
2617 entry_accessible = gtk_widget_get_accessible (entry);
2619 atk_object_add_relationship (window_accessible,
2620 ATK_RELATION_POPUP_FOR,
2625 unset_accessible_relation (GtkWidget *window,
2628 AtkObject *window_accessible;
2629 AtkObject *entry_accessible;
2631 window_accessible = gtk_widget_get_accessible (window);
2632 entry_accessible = gtk_widget_get_accessible (entry);
2634 atk_object_remove_relationship (window_accessible,
2635 ATK_RELATION_POPUP_FOR,
2640 disconnect_completion_signals (GtkEntryCompletion *completion)
2642 if (completion->priv->changed_id > 0 &&
2643 g_signal_handler_is_connected (completion->priv->entry,
2644 completion->priv->changed_id))
2646 g_signal_handler_disconnect (completion->priv->entry,
2647 completion->priv->changed_id);
2648 completion->priv->changed_id = 0;
2650 g_signal_handlers_disconnect_by_func (completion->priv->entry,
2651 G_CALLBACK (gtk_entry_completion_key_press), completion);
2652 if (completion->priv->insert_text_id > 0 &&
2653 g_signal_handler_is_connected (completion->priv->entry,
2654 completion->priv->insert_text_id))
2656 g_signal_handler_disconnect (completion->priv->entry,
2657 completion->priv->insert_text_id);
2658 completion->priv->insert_text_id = 0;
2660 g_signal_handlers_disconnect_by_func (completion->priv->entry,
2661 G_CALLBACK (completion_insert_text_callback), completion);
2662 g_signal_handlers_disconnect_by_func (completion->priv->entry,
2663 G_CALLBACK (clear_completion_callback), completion);
2664 g_signal_handlers_disconnect_by_func (completion->priv->entry,
2665 G_CALLBACK (accept_completion_callback), completion);
2669 _gtk_entry_completion_disconnect (GtkEntryCompletion *completion)
2671 if (completion->priv->completion_timeout)
2673 g_source_remove (completion->priv->completion_timeout);
2674 completion->priv->completion_timeout = 0;
2676 if (completion->priv->check_completion_idle)
2678 g_source_destroy (completion->priv->check_completion_idle);
2679 completion->priv->check_completion_idle = NULL;
2682 if (gtk_widget_get_mapped (completion->priv->popup_window))
2683 _gtk_entry_completion_popdown (completion);
2685 disconnect_completion_signals (completion);
2687 unset_accessible_relation (completion->priv->popup_window,
2688 completion->priv->entry);
2690 completion->priv->entry = NULL;
2694 _gtk_entry_completion_connect (GtkEntryCompletion *completion,
2697 completion->priv->entry = GTK_WIDGET (entry);
2699 set_accessible_relation (completion->priv->popup_window,
2700 completion->priv->entry);
2702 connect_completion_signals (completion);