1 /* gtkentrycompletion.c
2 * Copyright (C) 2003 Kristian Rietveld <kris@gtk.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * SECTION:gtkentrycompletion
22 * @Short_description: Completion functionality for GtkEntry
23 * @Title: GtkEntryCompletion
25 * #GtkEntryCompletion is an auxiliary object to be used in conjunction with
26 * #GtkEntry to provide the completion functionality. It implements the
27 * #GtkCellLayout interface, to allow the user to add extra cells to the
28 * #GtkTreeView with completion matches.
30 * "Completion functionality" means that when the user modifies the text
31 * in the entry, #GtkEntryCompletion checks which rows in the model match
32 * the current content of the entry, and displays a list of matches.
33 * By default, the matching is done by comparing the entry text
34 * case-insensitively against the text column of the model (see
35 * gtk_entry_completion_set_text_column()), but this can be overridden
36 * with a custom match function (see gtk_entry_completion_set_match_func()).
38 * When the user selects a completion, the content of the entry is
39 * updated. By default, the content of the entry is replaced by the
40 * text column of the model, but this can be overridden by connecting
41 * to the #GtkEntryCompletion::match-selected signal and updating the
42 * entry in the signal handler. Note that you should return %TRUE from
43 * the signal handler to suppress the default behaviour.
45 * To add completion functionality to an entry, use gtk_entry_set_completion().
47 * In addition to regular completion matches, which will be inserted into the
48 * entry when they are selected, #GtkEntryCompletion also allows to display
49 * "actions" in the popup window. Their appearance is similar to menuitems,
50 * to differentiate them clearly from completion strings. When an action is
51 * selected, the #GtkEntryCompletion::action-activated signal is emitted.
56 #include "gtkentrycompletion.h"
58 #include "gtkentryprivate.h"
59 #include "gtkcelllayout.h"
60 #include "gtkcellareabox.h"
63 #include "gtkcellrenderertext.h"
65 #include "gtktreeselection.h"
66 #include "gtktreeview.h"
67 #include "gtkscrolledwindow.h"
68 #include "gtksizerequest.h"
70 #include "gtkwindow.h"
72 #include "gtkmainprivate.h"
73 #include "gtkmarshalers.h"
75 #include "gtkprivate.h"
95 PROP_MINIMUM_KEY_LENGTH,
97 PROP_INLINE_COMPLETION,
98 PROP_POPUP_COMPLETION,
100 PROP_POPUP_SINGLE_MATCH,
101 PROP_INLINE_SELECTION,
106 static void gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface);
107 static GtkCellArea* gtk_entry_completion_get_area (GtkCellLayout *cell_layout);
109 static GObject *gtk_entry_completion_constructor (GType type,
110 guint n_construct_properties,
111 GObjectConstructParam *construct_properties);
112 static void gtk_entry_completion_set_property (GObject *object,
116 static void gtk_entry_completion_get_property (GObject *object,
120 static void gtk_entry_completion_finalize (GObject *object);
121 static void gtk_entry_completion_dispose (GObject *object);
123 static gboolean gtk_entry_completion_visible_func (GtkTreeModel *model,
126 static gboolean gtk_entry_completion_popup_key_event (GtkWidget *widget,
129 static gboolean gtk_entry_completion_popup_button_press (GtkWidget *widget,
130 GdkEventButton *event,
132 static gboolean gtk_entry_completion_list_button_press (GtkWidget *widget,
133 GdkEventButton *event,
135 static gboolean gtk_entry_completion_action_button_press (GtkWidget *widget,
136 GdkEventButton *event,
138 static void gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
140 static gboolean gtk_entry_completion_list_enter_notify (GtkWidget *widget,
141 GdkEventCrossing *event,
143 static gboolean gtk_entry_completion_list_motion_notify (GtkWidget *widget,
144 GdkEventMotion *event,
146 static void gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
150 static void gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
151 GtkCellRenderer *cell,
156 static gboolean gtk_entry_completion_match_selected (GtkEntryCompletion *completion,
159 static gboolean gtk_entry_completion_real_insert_prefix (GtkEntryCompletion *completion,
160 const gchar *prefix);
161 static gboolean gtk_entry_completion_cursor_on_match (GtkEntryCompletion *completion,
164 static gboolean gtk_entry_completion_insert_completion (GtkEntryCompletion *completion,
167 static void gtk_entry_completion_insert_completion_text (GtkEntryCompletion *completion,
170 static guint entry_completion_signals[LAST_SIGNAL] = { 0 };
173 static void gtk_entry_completion_buildable_init (GtkBuildableIface *iface);
175 G_DEFINE_TYPE_WITH_CODE (GtkEntryCompletion, gtk_entry_completion, G_TYPE_OBJECT,
176 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
177 gtk_entry_completion_cell_layout_init)
178 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
179 gtk_entry_completion_buildable_init))
183 gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
185 GObjectClass *object_class;
187 object_class = (GObjectClass *)klass;
189 object_class->constructor = gtk_entry_completion_constructor;
190 object_class->set_property = gtk_entry_completion_set_property;
191 object_class->get_property = gtk_entry_completion_get_property;
192 object_class->dispose = gtk_entry_completion_dispose;
193 object_class->finalize = gtk_entry_completion_finalize;
195 klass->match_selected = gtk_entry_completion_match_selected;
196 klass->insert_prefix = gtk_entry_completion_real_insert_prefix;
197 klass->cursor_on_match = gtk_entry_completion_cursor_on_match;
200 * GtkEntryCompletion::insert-prefix:
201 * @widget: the object which received the signal
202 * @prefix: the common prefix of all possible completions
204 * Gets emitted when the inline autocompletion is triggered.
205 * The default behaviour is to make the entry display the
206 * whole prefix and select the newly inserted part.
208 * Applications may connect to this signal in order to insert only a
209 * smaller part of the @prefix into the entry - e.g. the entry used in
210 * the #GtkFileChooser inserts only the part of the prefix up to the
213 * Return value: %TRUE if the signal has been handled
217 entry_completion_signals[INSERT_PREFIX] =
218 g_signal_new (I_("insert-prefix"),
219 G_TYPE_FROM_CLASS (klass),
221 G_STRUCT_OFFSET (GtkEntryCompletionClass, insert_prefix),
222 _gtk_boolean_handled_accumulator, NULL,
223 _gtk_marshal_BOOLEAN__STRING,
228 * GtkEntryCompletion::match-selected:
229 * @widget: the object which received the signal
230 * @model: the #GtkTreeModel containing the matches
231 * @iter: a #GtkTreeIter positioned at the selected match
233 * Gets emitted when a match from the list is selected.
234 * The default behaviour is to replace the contents of the
235 * entry with the contents of the text column in the row
236 * pointed to by @iter.
238 * Return value: %TRUE if the signal has been handled
242 entry_completion_signals[MATCH_SELECTED] =
243 g_signal_new (I_("match-selected"),
244 G_TYPE_FROM_CLASS (klass),
246 G_STRUCT_OFFSET (GtkEntryCompletionClass, match_selected),
247 _gtk_boolean_handled_accumulator, NULL,
248 _gtk_marshal_BOOLEAN__OBJECT_BOXED,
254 * GtkEntryCompletion::cursor-on-match:
255 * @widget: the object which received the signal
256 * @model: the #GtkTreeModel containing the matches
257 * @iter: a #GtkTreeIter positioned at the selected match
259 * Gets emitted when a match from the cursor is on a match
260 * of the list. The default behaviour is to replace the contents
261 * of the entry with the contents of the text column in the row
262 * pointed to by @iter.
264 * Return value: %TRUE if the signal has been handled
268 entry_completion_signals[CURSOR_ON_MATCH] =
269 g_signal_new (I_("cursor-on-match"),
270 G_TYPE_FROM_CLASS (klass),
272 G_STRUCT_OFFSET (GtkEntryCompletionClass, cursor_on_match),
273 _gtk_boolean_handled_accumulator, NULL,
274 _gtk_marshal_BOOLEAN__OBJECT_BOXED,
280 * GtkEntryCompletion::action-activated:
281 * @widget: the object which received the signal
282 * @index: the index of the activated action
284 * Gets emitted when an action is activated.
288 entry_completion_signals[ACTION_ACTIVATED] =
289 g_signal_new (I_("action-activated"),
290 G_TYPE_FROM_CLASS (klass),
292 G_STRUCT_OFFSET (GtkEntryCompletionClass, action_activated),
294 _gtk_marshal_VOID__INT,
298 g_object_class_install_property (object_class,
300 g_param_spec_object ("model",
301 P_("Completion Model"),
302 P_("The model to find matches in"),
304 GTK_PARAM_READWRITE));
305 g_object_class_install_property (object_class,
306 PROP_MINIMUM_KEY_LENGTH,
307 g_param_spec_int ("minimum-key-length",
308 P_("Minimum Key Length"),
309 P_("Minimum length of the search key in order to look up matches"),
313 GTK_PARAM_READWRITE));
315 * GtkEntryCompletion:text-column:
317 * The column of the model containing the strings.
318 * Note that the strings must be UTF-8.
322 g_object_class_install_property (object_class,
324 g_param_spec_int ("text-column",
326 P_("The column of the model containing the strings."),
330 GTK_PARAM_READWRITE));
333 * GtkEntryCompletion:inline-completion:
335 * Determines whether the common prefix of the possible completions
336 * should be inserted automatically in the entry. Note that this
337 * requires text-column to be set, even if you are using a custom
342 g_object_class_install_property (object_class,
343 PROP_INLINE_COMPLETION,
344 g_param_spec_boolean ("inline-completion",
345 P_("Inline completion"),
346 P_("Whether the common prefix should be inserted automatically"),
348 GTK_PARAM_READWRITE));
350 * GtkEntryCompletion:popup-completion:
352 * Determines whether the possible completions should be
353 * shown in a popup window.
357 g_object_class_install_property (object_class,
358 PROP_POPUP_COMPLETION,
359 g_param_spec_boolean ("popup-completion",
360 P_("Popup completion"),
361 P_("Whether the completions should be shown in a popup window"),
363 GTK_PARAM_READWRITE));
366 * GtkEntryCompletion:popup-set-width:
368 * Determines whether the completions popup window will be
369 * resized to the width of the entry.
373 g_object_class_install_property (object_class,
374 PROP_POPUP_SET_WIDTH,
375 g_param_spec_boolean ("popup-set-width",
376 P_("Popup set width"),
377 P_("If TRUE, the popup window will have the same size as the entry"),
379 GTK_PARAM_READWRITE));
382 * GtkEntryCompletion:popup-single-match:
384 * Determines whether the completions popup window will shown
385 * for a single possible completion. You probably want to set
386 * this to %FALSE if you are using
387 * <link linkend="GtkEntryCompletion--inline-completion">inline
392 g_object_class_install_property (object_class,
393 PROP_POPUP_SINGLE_MATCH,
394 g_param_spec_boolean ("popup-single-match",
395 P_("Popup single match"),
396 P_("If TRUE, the popup window will appear for a single match."),
398 GTK_PARAM_READWRITE));
400 * GtkEntryCompletion:inline-selection:
402 * Determines whether the possible completions on the popup
403 * will appear in the entry as you navigate through them.
407 g_object_class_install_property (object_class,
408 PROP_INLINE_SELECTION,
409 g_param_spec_boolean ("inline-selection",
410 P_("Inline selection"),
411 P_("Your description here"),
413 GTK_PARAM_READWRITE));
417 * GtkEntryCompletion:cell-area:
419 * The #GtkCellArea used to layout cell renderers in the treeview column.
423 g_object_class_install_property (object_class,
425 g_param_spec_object ("cell-area",
427 P_("The GtkCellArea used to layout cells"),
429 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
431 g_type_class_add_private (object_class, sizeof (GtkEntryCompletionPrivate));
436 gtk_entry_completion_buildable_custom_tag_end (GtkBuildable *buildable,
439 const gchar *tagname,
442 /* Just ignore the boolean return from here */
443 _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname, data);
447 gtk_entry_completion_buildable_init (GtkBuildableIface *iface)
449 iface->add_child = _gtk_cell_layout_buildable_add_child;
450 iface->custom_tag_start = _gtk_cell_layout_buildable_custom_tag_start;
451 iface->custom_tag_end = gtk_entry_completion_buildable_custom_tag_end;
455 gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface)
457 iface->get_area = gtk_entry_completion_get_area;
461 gtk_entry_completion_init (GtkEntryCompletion *completion)
463 GtkEntryCompletionPrivate *priv;
465 /* yes, also priv, need to keep the code readable */
466 completion->priv = G_TYPE_INSTANCE_GET_PRIVATE (completion,
467 GTK_TYPE_ENTRY_COMPLETION,
468 GtkEntryCompletionPrivate);
469 priv = completion->priv;
471 priv->minimum_key_length = 1;
472 priv->text_column = -1;
473 priv->has_completion = FALSE;
474 priv->inline_completion = FALSE;
475 priv->popup_completion = TRUE;
476 priv->popup_set_width = TRUE;
477 priv->popup_single_match = TRUE;
478 priv->inline_selection = FALSE;
480 priv->filter_model = NULL;
484 gtk_entry_completion_constructor (GType type,
485 guint n_construct_properties,
486 GObjectConstructParam *construct_properties)
488 GtkEntryCompletion *completion;
489 GtkEntryCompletionPrivate *priv;
491 GtkCellRenderer *cell;
492 GtkTreeSelection *sel;
493 GtkWidget *popup_frame;
495 object = G_OBJECT_CLASS (gtk_entry_completion_parent_class)->constructor
496 (type, n_construct_properties, construct_properties);
498 completion = (GtkEntryCompletion *) object;
499 priv = completion->priv;
501 if (!priv->cell_area)
503 priv->cell_area = gtk_cell_area_box_new ();
504 g_object_ref_sink (priv->cell_area);
508 priv->tree_view = gtk_tree_view_new ();
509 g_signal_connect (priv->tree_view, "button-press-event",
510 G_CALLBACK (gtk_entry_completion_list_button_press),
512 g_signal_connect (priv->tree_view, "enter-notify-event",
513 G_CALLBACK (gtk_entry_completion_list_enter_notify),
515 g_signal_connect (priv->tree_view, "motion-notify-event",
516 G_CALLBACK (gtk_entry_completion_list_motion_notify),
519 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
520 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view), TRUE);
522 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
523 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
524 gtk_tree_selection_unselect_all (sel);
525 g_signal_connect (sel, "changed",
526 G_CALLBACK (gtk_entry_completion_selection_changed),
528 priv->first_sel_changed = TRUE;
530 priv->column = gtk_tree_view_column_new_with_area (priv->cell_area);
531 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
533 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
534 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
536 GTK_POLICY_AUTOMATIC);
537 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
540 /* a nasty hack to get the completions treeview to size nicely */
541 gtk_widget_set_size_request (gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (priv->scrolled_window)),
545 priv->actions = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_BOOLEAN);
548 gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->actions));
549 g_object_ref_sink (priv->action_view);
550 g_signal_connect (priv->action_view, "button-press-event",
551 G_CALLBACK (gtk_entry_completion_action_button_press),
553 g_signal_connect (priv->action_view, "enter-notify-event",
554 G_CALLBACK (gtk_entry_completion_list_enter_notify),
556 g_signal_connect (priv->action_view, "motion-notify-event",
557 G_CALLBACK (gtk_entry_completion_list_motion_notify),
559 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->action_view), FALSE);
560 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->action_view), TRUE);
562 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->action_view));
563 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
564 gtk_tree_selection_unselect_all (sel);
566 cell = gtk_cell_renderer_text_new ();
567 gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (priv->action_view),
570 gtk_entry_completion_action_data_func,
575 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
576 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
577 gtk_window_set_type_hint (GTK_WINDOW(priv->popup_window),
578 GDK_WINDOW_TYPE_HINT_COMBO);
579 g_signal_connect (priv->popup_window, "key-press-event",
580 G_CALLBACK (gtk_entry_completion_popup_key_event),
582 g_signal_connect (priv->popup_window, "key-release-event",
583 G_CALLBACK (gtk_entry_completion_popup_key_event),
585 g_signal_connect (priv->popup_window, "button-press-event",
586 G_CALLBACK (gtk_entry_completion_popup_button_press),
589 popup_frame = gtk_frame_new (NULL);
590 gtk_frame_set_shadow_type (GTK_FRAME (popup_frame),
591 GTK_SHADOW_ETCHED_IN);
592 gtk_widget_show (popup_frame);
593 gtk_container_add (GTK_CONTAINER (priv->popup_window), popup_frame);
595 priv->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
596 gtk_container_add (GTK_CONTAINER (popup_frame), priv->vbox);
598 gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->tree_view);
599 gtk_box_pack_start (GTK_BOX (priv->vbox), priv->scrolled_window,
602 /* we don't want to see the action treeview when no actions have
603 * been inserted, so we pack the action treeview after the first
604 * action has been added
612 gtk_entry_completion_set_property (GObject *object,
617 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
618 GtkEntryCompletionPrivate *priv = completion->priv;
624 gtk_entry_completion_set_model (completion,
625 g_value_get_object (value));
628 case PROP_MINIMUM_KEY_LENGTH:
629 gtk_entry_completion_set_minimum_key_length (completion,
630 g_value_get_int (value));
633 case PROP_TEXT_COLUMN:
634 priv->text_column = g_value_get_int (value);
637 case PROP_INLINE_COMPLETION:
638 priv->inline_completion = g_value_get_boolean (value);
641 case PROP_POPUP_COMPLETION:
642 priv->popup_completion = g_value_get_boolean (value);
645 case PROP_POPUP_SET_WIDTH:
646 priv->popup_set_width = g_value_get_boolean (value);
649 case PROP_POPUP_SINGLE_MATCH:
650 priv->popup_single_match = g_value_get_boolean (value);
653 case PROP_INLINE_SELECTION:
654 priv->inline_selection = g_value_get_boolean (value);
658 /* Construct-only, can only be assigned once */
659 area = g_value_get_object (value);
662 priv->cell_area = g_object_ref_sink (area);
666 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
672 gtk_entry_completion_get_property (GObject *object,
677 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
682 g_value_set_object (value,
683 gtk_entry_completion_get_model (completion));
686 case PROP_MINIMUM_KEY_LENGTH:
687 g_value_set_int (value, gtk_entry_completion_get_minimum_key_length (completion));
690 case PROP_TEXT_COLUMN:
691 g_value_set_int (value, gtk_entry_completion_get_text_column (completion));
694 case PROP_INLINE_COMPLETION:
695 g_value_set_boolean (value, gtk_entry_completion_get_inline_completion (completion));
698 case PROP_POPUP_COMPLETION:
699 g_value_set_boolean (value, gtk_entry_completion_get_popup_completion (completion));
702 case PROP_POPUP_SET_WIDTH:
703 g_value_set_boolean (value, gtk_entry_completion_get_popup_set_width (completion));
706 case PROP_POPUP_SINGLE_MATCH:
707 g_value_set_boolean (value, gtk_entry_completion_get_popup_single_match (completion));
710 case PROP_INLINE_SELECTION:
711 g_value_set_boolean (value, gtk_entry_completion_get_inline_selection (completion));
715 g_value_set_object (value, completion->priv->cell_area);
719 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
725 gtk_entry_completion_finalize (GObject *object)
727 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
728 GtkEntryCompletionPrivate *priv = completion->priv;
730 g_free (priv->case_normalized_key);
731 g_free (priv->completion_prefix);
733 if (priv->match_notify)
734 (* priv->match_notify) (priv->match_data);
736 G_OBJECT_CLASS (gtk_entry_completion_parent_class)->finalize (object);
740 gtk_entry_completion_dispose (GObject *object)
742 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
743 GtkEntryCompletionPrivate *priv = completion->priv;
747 gtk_widget_destroy (priv->tree_view);
748 priv->tree_view = NULL;
752 gtk_entry_set_completion (GTK_ENTRY (priv->entry), NULL);
756 g_object_unref (priv->actions);
757 priv->actions = NULL;
760 if (priv->action_view)
762 g_object_unref (priv->action_view);
763 priv->action_view = NULL;
766 if (priv->popup_window)
768 gtk_widget_destroy (priv->popup_window);
769 priv->popup_window = NULL;
774 g_object_unref (priv->cell_area);
775 priv->cell_area = NULL;
778 G_OBJECT_CLASS (gtk_entry_completion_parent_class)->dispose (object);
781 /* implement cell layout interface (only need to return the underlying cell area) */
783 gtk_entry_completion_get_area (GtkCellLayout *cell_layout)
785 GtkEntryCompletionPrivate *priv;
787 priv = GTK_ENTRY_COMPLETION (cell_layout)->priv;
789 return priv->cell_area;
792 /* all those callbacks */
794 gtk_entry_completion_default_completion_func (GtkEntryCompletion *completion,
800 gchar *normalized_string;
801 gchar *case_normalized_string;
803 gboolean ret = FALSE;
807 model = gtk_tree_model_filter_get_model (completion->priv->filter_model);
809 g_return_val_if_fail (gtk_tree_model_get_column_type (model, completion->priv->text_column) == G_TYPE_STRING,
812 gtk_tree_model_get (model, iter,
813 completion->priv->text_column, &item,
818 normalized_string = g_utf8_normalize (item, -1, G_NORMALIZE_ALL);
820 if (normalized_string != NULL)
822 case_normalized_string = g_utf8_casefold (normalized_string, -1);
824 if (!strncmp (key, case_normalized_string, strlen (key)))
827 g_free (case_normalized_string);
829 g_free (normalized_string);
837 gtk_entry_completion_visible_func (GtkTreeModel *model,
841 gboolean ret = FALSE;
843 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
845 if (!completion->priv->case_normalized_key)
848 if (completion->priv->match_func)
849 ret = (* completion->priv->match_func) (completion,
850 completion->priv->case_normalized_key,
852 completion->priv->match_data);
853 else if (completion->priv->text_column >= 0)
854 ret = gtk_entry_completion_default_completion_func (completion,
855 completion->priv->case_normalized_key,
863 gtk_entry_completion_popup_key_event (GtkWidget *widget,
867 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
869 if (!gtk_widget_get_mapped (completion->priv->popup_window))
872 /* propagate event to the entry */
873 gtk_widget_event (completion->priv->entry, (GdkEvent *)event);
879 gtk_entry_completion_popup_button_press (GtkWidget *widget,
880 GdkEventButton *event,
883 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
885 if (!gtk_widget_get_mapped (completion->priv->popup_window))
888 /* if we come here, it's usually time to popdown */
889 _gtk_entry_completion_popdown (completion);
895 gtk_entry_completion_list_button_press (GtkWidget *widget,
896 GdkEventButton *event,
899 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
900 GtkTreePath *path = NULL;
902 if (!gtk_widget_get_mapped (completion->priv->popup_window))
905 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
907 &path, NULL, NULL, NULL))
912 GtkTreeIter child_iter;
914 gtk_tree_model_get_iter (GTK_TREE_MODEL (completion->priv->filter_model),
916 gtk_tree_path_free (path);
917 gtk_tree_model_filter_convert_iter_to_child_iter (completion->priv->filter_model,
920 model = gtk_tree_model_filter_get_model (completion->priv->filter_model);
922 g_signal_handler_block (completion->priv->entry,
923 completion->priv->changed_id);
924 g_signal_emit (completion, entry_completion_signals[MATCH_SELECTED],
925 0, model, &child_iter, &entry_set);
926 g_signal_handler_unblock (completion->priv->entry,
927 completion->priv->changed_id);
929 _gtk_entry_completion_popdown (completion);
938 gtk_entry_completion_action_button_press (GtkWidget *widget,
939 GdkEventButton *event,
942 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
943 GtkTreePath *path = NULL;
945 if (!gtk_widget_get_mapped (completion->priv->popup_window))
948 _gtk_entry_reset_im_context (GTK_ENTRY (completion->priv->entry));
950 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
952 &path, NULL, NULL, NULL))
954 g_signal_emit (completion, entry_completion_signals[ACTION_ACTIVATED],
955 0, gtk_tree_path_get_indices (path)[0]);
956 gtk_tree_path_free (path);
958 _gtk_entry_completion_popdown (completion);
966 gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
967 GtkCellRenderer *cell,
972 gchar *string = NULL;
975 gtk_tree_model_get (model, iter,
998 gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
1001 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1003 if (completion->priv->first_sel_changed)
1005 completion->priv->first_sel_changed = FALSE;
1006 if (gtk_widget_is_focus (completion->priv->tree_view))
1007 gtk_tree_selection_unselect_all (selection);
1014 * gtk_entry_completion_new:
1016 * Creates a new #GtkEntryCompletion object.
1018 * Return value: A newly created #GtkEntryCompletion object
1022 GtkEntryCompletion *
1023 gtk_entry_completion_new (void)
1025 GtkEntryCompletion *completion;
1027 completion = g_object_new (GTK_TYPE_ENTRY_COMPLETION, NULL);
1033 * gtk_entry_completion_new_with_area:
1034 * @area: the #GtkCellArea used to layout cells
1036 * Creates a new #GtkEntryCompletion object using the
1037 * specified @area to layout cells in the underlying
1038 * #GtkTreeViewColumn for the drop-down menu.
1040 * Return value: A newly created #GtkEntryCompletion object
1044 GtkEntryCompletion *
1045 gtk_entry_completion_new_with_area (GtkCellArea *area)
1047 GtkEntryCompletion *completion;
1049 completion = g_object_new (GTK_TYPE_ENTRY_COMPLETION, "cell-area", area, NULL);
1055 * gtk_entry_completion_get_entry:
1056 * @completion: a #GtkEntryCompletion
1058 * Gets the entry @completion has been attached to.
1060 * Return value: (transfer none): The entry @completion has been attached to
1065 gtk_entry_completion_get_entry (GtkEntryCompletion *completion)
1067 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
1069 return completion->priv->entry;
1073 * gtk_entry_completion_set_model:
1074 * @completion: a #GtkEntryCompletion
1075 * @model: (allow-none): the #GtkTreeModel
1077 * Sets the model for a #GtkEntryCompletion. If @completion already has
1078 * a model set, it will remove it before setting the new model.
1079 * If model is %NULL, then it will unset the model.
1084 gtk_entry_completion_set_model (GtkEntryCompletion *completion,
1085 GtkTreeModel *model)
1087 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1088 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
1092 gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
1094 _gtk_entry_completion_popdown (completion);
1095 completion->priv->filter_model = NULL;
1099 /* code will unref the old filter model (if any) */
1100 completion->priv->filter_model =
1101 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
1102 gtk_tree_model_filter_set_visible_func (completion->priv->filter_model,
1103 gtk_entry_completion_visible_func,
1107 gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
1108 GTK_TREE_MODEL (completion->priv->filter_model));
1109 g_object_unref (completion->priv->filter_model);
1111 g_object_notify (G_OBJECT (completion), "model");
1113 if (gtk_widget_get_visible (completion->priv->popup_window))
1114 _gtk_entry_completion_resize_popup (completion);
1118 * gtk_entry_completion_get_model:
1119 * @completion: a #GtkEntryCompletion
1121 * Returns the model the #GtkEntryCompletion is using as data source.
1122 * Returns %NULL if the model is unset.
1124 * Return value: (transfer none): A #GtkTreeModel, or %NULL if none
1125 * is currently being used
1130 gtk_entry_completion_get_model (GtkEntryCompletion *completion)
1132 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
1134 if (!completion->priv->filter_model)
1137 return gtk_tree_model_filter_get_model (completion->priv->filter_model);
1141 * gtk_entry_completion_set_match_func:
1142 * @completion: a #GtkEntryCompletion
1143 * @func: the #GtkEntryCompletionMatchFunc to use
1144 * @func_data: user data for @func
1145 * @func_notify: destroy notify for @func_data.
1147 * Sets the match function for @completion to be @func. The match function
1148 * is used to determine if a row should or should not be in the completion
1154 gtk_entry_completion_set_match_func (GtkEntryCompletion *completion,
1155 GtkEntryCompletionMatchFunc func,
1157 GDestroyNotify func_notify)
1159 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1161 if (completion->priv->match_notify)
1162 (* completion->priv->match_notify) (completion->priv->match_data);
1164 completion->priv->match_func = func;
1165 completion->priv->match_data = func_data;
1166 completion->priv->match_notify = func_notify;
1170 * gtk_entry_completion_set_minimum_key_length:
1171 * @completion: a #GtkEntryCompletion
1172 * @length: the minimum length of the key in order to start completing
1174 * Requires the length of the search key for @completion to be at least
1175 * @length. This is useful for long lists, where completing using a small
1176 * key takes a lot of time and will come up with meaningless results anyway
1177 * (ie, a too large dataset).
1182 gtk_entry_completion_set_minimum_key_length (GtkEntryCompletion *completion,
1185 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1186 g_return_if_fail (length >= 0);
1188 if (completion->priv->minimum_key_length != length)
1190 completion->priv->minimum_key_length = length;
1192 g_object_notify (G_OBJECT (completion), "minimum-key-length");
1197 * gtk_entry_completion_get_minimum_key_length:
1198 * @completion: a #GtkEntryCompletion
1200 * Returns the minimum key length as set for @completion.
1202 * Return value: The currently used minimum key length
1207 gtk_entry_completion_get_minimum_key_length (GtkEntryCompletion *completion)
1209 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), 0);
1211 return completion->priv->minimum_key_length;
1215 * gtk_entry_completion_complete:
1216 * @completion: a #GtkEntryCompletion
1218 * Requests a completion operation, or in other words a refiltering of the
1219 * current list with completions, using the current key. The completion list
1220 * view will be updated accordingly.
1225 gtk_entry_completion_complete (GtkEntryCompletion *completion)
1229 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1231 if (!completion->priv->filter_model)
1234 g_free (completion->priv->case_normalized_key);
1236 tmp = g_utf8_normalize (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)),
1237 -1, G_NORMALIZE_ALL);
1238 completion->priv->case_normalized_key = g_utf8_casefold (tmp, -1);
1241 gtk_tree_model_filter_refilter (completion->priv->filter_model);
1243 if (gtk_widget_get_visible (completion->priv->popup_window))
1244 _gtk_entry_completion_resize_popup (completion);
1248 gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
1250 const gchar *string,
1255 gtk_list_store_insert (completion->priv->actions, &iter, index);
1256 gtk_list_store_set (completion->priv->actions, &iter,
1261 if (!gtk_widget_get_parent (completion->priv->action_view))
1263 GtkTreePath *path = gtk_tree_path_new_from_indices (0, -1);
1265 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
1267 gtk_tree_path_free (path);
1269 gtk_box_pack_start (GTK_BOX (completion->priv->vbox),
1270 completion->priv->action_view, FALSE, FALSE, 0);
1271 gtk_widget_show (completion->priv->action_view);
1276 * gtk_entry_completion_insert_action_text:
1277 * @completion: a #GtkEntryCompletion
1278 * @index_: the index of the item to insert
1279 * @text: text of the item to insert
1281 * Inserts an action in @completion's action item list at position @index_
1282 * with text @text. If you want the action item to have markup, use
1283 * gtk_entry_completion_insert_action_markup().
1288 gtk_entry_completion_insert_action_text (GtkEntryCompletion *completion,
1292 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1293 g_return_if_fail (text != NULL);
1295 gtk_entry_completion_insert_action (completion, index_, text, FALSE);
1299 * gtk_entry_completion_insert_action_markup:
1300 * @completion: a #GtkEntryCompletion
1301 * @index_: the index of the item to insert
1302 * @markup: markup of the item to insert
1304 * Inserts an action in @completion's action item list at position @index_
1305 * with markup @markup.
1310 gtk_entry_completion_insert_action_markup (GtkEntryCompletion *completion,
1312 const gchar *markup)
1314 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1315 g_return_if_fail (markup != NULL);
1317 gtk_entry_completion_insert_action (completion, index_, markup, TRUE);
1321 * gtk_entry_completion_delete_action:
1322 * @completion: a #GtkEntryCompletion
1323 * @index_: the index of the item to delete
1325 * Deletes the action at @index_ from @completion's action list.
1330 gtk_entry_completion_delete_action (GtkEntryCompletion *completion,
1335 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1336 g_return_if_fail (index_ >= 0);
1338 gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (completion->priv->actions),
1339 &iter, NULL, index_);
1340 gtk_list_store_remove (completion->priv->actions, &iter);
1344 * gtk_entry_completion_set_text_column:
1345 * @completion: a #GtkEntryCompletion
1346 * @column: the column in the model of @completion to get strings from
1348 * Convenience function for setting up the most used case of this code: a
1349 * completion list with just strings. This function will set up @completion
1350 * to have a list displaying all (and just) strings in the completion list,
1351 * and to get those strings from @column in the model of @completion.
1353 * This functions creates and adds a #GtkCellRendererText for the selected
1354 * column. If you need to set the text column, but don't want the cell
1355 * renderer, use g_object_set() to set the #GtkEntryCompletion:text-column
1356 * property directly.
1361 gtk_entry_completion_set_text_column (GtkEntryCompletion *completion,
1364 GtkCellRenderer *cell;
1366 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1367 g_return_if_fail (column >= 0);
1369 completion->priv->text_column = column;
1371 cell = gtk_cell_renderer_text_new ();
1372 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion),
1374 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (completion),
1378 g_object_notify (G_OBJECT (completion), "text-column");
1382 * gtk_entry_completion_get_text_column:
1383 * @completion: a #GtkEntryCompletion
1385 * Returns the column in the model of @completion to get strings from.
1387 * Return value: the column containing the strings
1392 gtk_entry_completion_get_text_column (GtkEntryCompletion *completion)
1394 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), -1);
1396 return completion->priv->text_column;
1402 gtk_entry_completion_list_enter_notify (GtkWidget *widget,
1403 GdkEventCrossing *event,
1406 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1408 return completion->priv->ignore_enter;
1412 gtk_entry_completion_list_motion_notify (GtkWidget *widget,
1413 GdkEventMotion *event,
1416 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1418 completion->priv->ignore_enter = FALSE;
1424 /* some nasty size requisition */
1426 _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
1428 GtkAllocation allocation;
1430 gint matches, actions, items, height, x_border, y_border;
1433 gint vertical_separator;
1434 GdkRectangle monitor;
1436 GtkRequisition popup_req;
1437 GtkRequisition entry_req;
1441 GtkTreeViewColumn *action_column;
1444 window = gtk_widget_get_window (completion->priv->entry);
1449 gtk_widget_get_allocation (completion->priv->entry, &allocation);
1450 gtk_widget_get_preferred_size (completion->priv->entry,
1453 gdk_window_get_origin (window, &x, &y);
1455 y += allocation.y + (allocation.height - entry_req.height) / 2;
1457 _gtk_entry_get_borders (GTK_ENTRY (completion->priv->entry), &x_border, &y_border);
1459 matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
1460 actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
1461 action_column = gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0);
1463 gtk_tree_view_column_cell_get_size (completion->priv->column, NULL,
1464 NULL, NULL, NULL, &height);
1465 gtk_tree_view_column_cell_get_size (action_column, NULL,
1466 NULL, NULL, NULL, &action_height);
1468 gtk_widget_style_get (GTK_WIDGET (completion->priv->tree_view),
1469 "vertical-separator", &vertical_separator,
1472 height += vertical_separator;
1474 gtk_widget_realize (completion->priv->tree_view);
1476 screen = gtk_widget_get_screen (GTK_WIDGET (completion->priv->entry));
1477 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
1478 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1482 if (y > monitor.height / 2)
1483 items = MIN (matches, (((monitor.y + y) - (actions * action_height)) / height) - 1);
1485 items = MIN (matches, (((monitor.height - y) - (actions * action_height)) / height) - 1);
1488 gtk_widget_hide (completion->priv->scrolled_window);
1490 gtk_widget_show (completion->priv->scrolled_window);
1492 if (completion->priv->popup_set_width)
1493 width = MIN (allocation.width, monitor.width) - 2 * x_border;
1497 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (completion->priv->tree_view));
1498 gtk_scrolled_window_set_min_content_width (GTK_SCROLLED_WINDOW (completion->priv->scrolled_window), width);
1499 gtk_widget_set_size_request (completion->priv->scrolled_window, width, -1);
1500 gtk_scrolled_window_set_min_content_height (GTK_SCROLLED_WINDOW (completion->priv->scrolled_window), items * height);
1504 gtk_widget_show (completion->priv->action_view);
1505 gtk_widget_set_size_request (completion->priv->action_view, width, -1);
1508 gtk_widget_hide (completion->priv->action_view);
1510 gtk_widget_get_preferred_size (completion->priv->popup_window,
1515 else if (x + popup_req.width > monitor.x + monitor.width)
1516 x = monitor.x + monitor.width - popup_req.width;
1518 if (y + entry_req.height + popup_req.height <= monitor.y + monitor.height ||
1519 y - monitor.y < (monitor.y + monitor.height) - (y + entry_req.height))
1521 y += entry_req.height;
1526 y -= popup_req.height;
1532 path = gtk_tree_path_new_from_indices (above ? matches - 1 : 0, -1);
1533 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (completion->priv->tree_view), path,
1534 NULL, FALSE, 0.0, 0.0);
1535 gtk_tree_path_free (path);
1538 gtk_window_move (GTK_WINDOW (completion->priv->popup_window), x, y);
1544 _gtk_entry_completion_popup (GtkEntryCompletion *completion,
1547 GtkTreeViewColumn *column;
1548 GtkStyleContext *context;
1551 GtkWidget *toplevel;
1553 if (gtk_widget_get_mapped (completion->priv->popup_window))
1556 if (!gtk_widget_get_mapped (completion->priv->entry))
1559 if (!gtk_widget_has_focus (completion->priv->entry))
1562 if (completion->priv->grab_device)
1565 completion->priv->ignore_enter = TRUE;
1567 column = gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0);
1568 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
1570 context = gtk_widget_get_style_context (completion->priv->tree_view);
1571 gtk_style_context_get_background_color (context, 0, &color);
1573 g_object_set (GTK_CELL_RENDERER (renderers->data),
1574 "cell-background-rgba", &color,
1576 g_list_free (renderers);
1578 gtk_widget_show_all (completion->priv->vbox);
1580 /* default on no match */
1581 completion->priv->current_selected = -1;
1583 _gtk_entry_completion_resize_popup (completion);
1585 toplevel = gtk_widget_get_toplevel (completion->priv->entry);
1586 if (GTK_IS_WINDOW (toplevel))
1587 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1588 GTK_WINDOW (completion->priv->popup_window));
1590 /* prevent the first row being focused */
1591 gtk_widget_grab_focus (completion->priv->tree_view);
1593 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
1594 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
1596 gtk_window_set_screen (GTK_WINDOW (completion->priv->popup_window),
1597 gtk_widget_get_screen (completion->priv->entry));
1599 gtk_widget_show (completion->priv->popup_window);
1601 gtk_device_grab_add (completion->priv->popup_window, device, TRUE);
1602 gdk_device_grab (device, gtk_widget_get_window (completion->priv->popup_window),
1603 GDK_OWNERSHIP_WINDOW, TRUE,
1604 GDK_BUTTON_PRESS_MASK |
1605 GDK_BUTTON_RELEASE_MASK |
1606 GDK_POINTER_MOTION_MASK,
1607 NULL, GDK_CURRENT_TIME);
1609 completion->priv->grab_device = device;
1613 _gtk_entry_completion_popdown (GtkEntryCompletion *completion)
1615 if (!gtk_widget_get_mapped (completion->priv->popup_window))
1618 completion->priv->ignore_enter = FALSE;
1620 if (completion->priv->grab_device)
1622 gdk_device_ungrab (completion->priv->grab_device, GDK_CURRENT_TIME);
1623 gtk_device_grab_remove (completion->priv->popup_window,
1624 completion->priv->grab_device);
1625 completion->priv->grab_device = NULL;
1628 gtk_widget_hide (completion->priv->popup_window);
1632 gtk_entry_completion_match_selected (GtkEntryCompletion *completion,
1633 GtkTreeModel *model,
1638 gtk_tree_model_get (model, iter, completion->priv->text_column, &str, -1);
1639 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), str ? str : "");
1641 /* move cursor to the end */
1642 gtk_editable_set_position (GTK_EDITABLE (completion->priv->entry), -1);
1650 gtk_entry_completion_cursor_on_match (GtkEntryCompletion *completion,
1651 GtkTreeModel *model,
1654 gtk_entry_completion_insert_completion (completion, model, iter);
1660 gtk_entry_completion_compute_prefix (GtkEntryCompletion *completion)
1663 gchar *prefix = NULL;
1667 if (completion->priv->text_column < 0)
1670 key = gtk_entry_get_text (GTK_ENTRY (completion->priv->entry));
1672 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (completion->priv->filter_model),
1679 gtk_tree_model_get (GTK_TREE_MODEL (completion->priv->filter_model),
1680 &iter, completion->priv->text_column, &text,
1683 if (text && g_str_has_prefix (text, key))
1686 prefix = g_strdup (text);
1692 while (*p && *p == *q)
1702 /* strip a partial multibyte character */
1703 q = g_utf8_find_prev_char (prefix, p);
1704 switch (g_utf8_get_char_validated (q, p - q))
1716 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (completion->priv->filter_model),
1725 gtk_entry_completion_real_insert_prefix (GtkEntryCompletion *completion,
1726 const gchar *prefix)
1734 prefix_len = g_utf8_strlen (prefix, -1);
1736 key = gtk_entry_get_text (GTK_ENTRY (completion->priv->entry));
1737 key_len = g_utf8_strlen (key, -1);
1739 if (prefix_len > key_len)
1741 gint pos = prefix_len;
1743 gtk_editable_insert_text (GTK_EDITABLE (completion->priv->entry),
1744 prefix + strlen (key), -1, &pos);
1745 gtk_editable_select_region (GTK_EDITABLE (completion->priv->entry),
1746 key_len, prefix_len);
1748 completion->priv->has_completion = TRUE;
1756 * gtk_entry_completion_get_completion_prefix:
1757 * @completion: a #GtkEntryCompletion
1759 * Get the original text entered by the user that triggered
1760 * the completion or %NULL if there's no completion ongoing.
1762 * Returns: the prefix for the current completion
1767 gtk_entry_completion_get_completion_prefix (GtkEntryCompletion *completion)
1769 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
1771 return completion->priv->completion_prefix;
1775 gtk_entry_completion_insert_completion_text (GtkEntryCompletion *completion,
1778 GtkEntryCompletionPrivate *priv = completion->priv;
1781 priv = completion->priv;
1783 if (priv->changed_id > 0)
1784 g_signal_handler_block (priv->entry, priv->changed_id);
1786 if (priv->insert_text_id > 0)
1787 g_signal_handler_block (priv->entry, priv->insert_text_id);
1789 gtk_entry_set_text (GTK_ENTRY (priv->entry), text);
1791 len = strlen (priv->completion_prefix);
1792 gtk_editable_select_region (GTK_EDITABLE (priv->entry), len, -1);
1794 if (priv->changed_id > 0)
1795 g_signal_handler_unblock (priv->entry, priv->changed_id);
1797 if (priv->insert_text_id > 0)
1798 g_signal_handler_unblock (priv->entry, priv->insert_text_id);
1802 gtk_entry_completion_insert_completion (GtkEntryCompletion *completion,
1803 GtkTreeModel *model,
1808 if (completion->priv->text_column < 0)
1811 gtk_tree_model_get (model, iter,
1812 completion->priv->text_column, &str,
1815 gtk_entry_completion_insert_completion_text (completion, str);
1823 * gtk_entry_completion_insert_prefix:
1824 * @completion: a #GtkEntryCompletion
1826 * Requests a prefix insertion.
1831 gtk_entry_completion_insert_prefix (GtkEntryCompletion *completion)
1836 if (completion->priv->insert_text_id > 0)
1837 g_signal_handler_block (completion->priv->entry,
1838 completion->priv->insert_text_id);
1840 prefix = gtk_entry_completion_compute_prefix (completion);
1843 g_signal_emit (completion, entry_completion_signals[INSERT_PREFIX],
1848 if (completion->priv->insert_text_id > 0)
1849 g_signal_handler_unblock (completion->priv->entry,
1850 completion->priv->insert_text_id);
1854 * gtk_entry_completion_set_inline_completion:
1855 * @completion: a #GtkEntryCompletion
1856 * @inline_completion: %TRUE to do inline completion
1858 * Sets whether the common prefix of the possible completions should
1859 * be automatically inserted in the entry.
1864 gtk_entry_completion_set_inline_completion (GtkEntryCompletion *completion,
1865 gboolean inline_completion)
1867 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1869 inline_completion = inline_completion != FALSE;
1871 if (completion->priv->inline_completion != inline_completion)
1873 completion->priv->inline_completion = inline_completion;
1875 g_object_notify (G_OBJECT (completion), "inline-completion");
1880 * gtk_entry_completion_get_inline_completion:
1881 * @completion: a #GtkEntryCompletion
1883 * Returns whether the common prefix of the possible completions should
1884 * be automatically inserted in the entry.
1886 * Return value: %TRUE if inline completion is turned on
1891 gtk_entry_completion_get_inline_completion (GtkEntryCompletion *completion)
1893 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), FALSE);
1895 return completion->priv->inline_completion;
1899 * gtk_entry_completion_set_popup_completion:
1900 * @completion: a #GtkEntryCompletion
1901 * @popup_completion: %TRUE to do popup completion
1903 * Sets whether the completions should be presented in a popup window.
1908 gtk_entry_completion_set_popup_completion (GtkEntryCompletion *completion,
1909 gboolean popup_completion)
1911 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1913 popup_completion = popup_completion != FALSE;
1915 if (completion->priv->popup_completion != popup_completion)
1917 completion->priv->popup_completion = popup_completion;
1919 g_object_notify (G_OBJECT (completion), "popup-completion");
1925 * gtk_entry_completion_get_popup_completion:
1926 * @completion: a #GtkEntryCompletion
1928 * Returns whether the completions should be presented in a popup window.
1930 * Return value: %TRUE if popup completion is turned on
1935 gtk_entry_completion_get_popup_completion (GtkEntryCompletion *completion)
1937 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), TRUE);
1939 return completion->priv->popup_completion;
1943 * gtk_entry_completion_set_popup_set_width:
1944 * @completion: a #GtkEntryCompletion
1945 * @popup_set_width: %TRUE to make the width of the popup the same as the entry
1947 * Sets whether the completion popup window will be resized to be the same
1948 * width as the entry.
1953 gtk_entry_completion_set_popup_set_width (GtkEntryCompletion *completion,
1954 gboolean popup_set_width)
1956 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1958 popup_set_width = popup_set_width != FALSE;
1960 if (completion->priv->popup_set_width != popup_set_width)
1962 completion->priv->popup_set_width = popup_set_width;
1964 g_object_notify (G_OBJECT (completion), "popup-set-width");
1969 * gtk_entry_completion_get_popup_set_width:
1970 * @completion: a #GtkEntryCompletion
1972 * Returns whether the completion popup window will be resized to the
1973 * width of the entry.
1975 * Return value: %TRUE if the popup window will be resized to the width of
1981 gtk_entry_completion_get_popup_set_width (GtkEntryCompletion *completion)
1983 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), TRUE);
1985 return completion->priv->popup_set_width;
1990 * gtk_entry_completion_set_popup_single_match:
1991 * @completion: a #GtkEntryCompletion
1992 * @popup_single_match: %TRUE if the popup should appear even for a single
1995 * Sets whether the completion popup window will appear even if there is
1996 * only a single match. You may want to set this to %FALSE if you
1997 * are using <link linkend="GtkEntryCompletion--inline-completion">inline
1998 * completion</link>.
2003 gtk_entry_completion_set_popup_single_match (GtkEntryCompletion *completion,
2004 gboolean popup_single_match)
2006 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
2008 popup_single_match = popup_single_match != FALSE;
2010 if (completion->priv->popup_single_match != popup_single_match)
2012 completion->priv->popup_single_match = popup_single_match;
2014 g_object_notify (G_OBJECT (completion), "popup-single-match");
2019 * gtk_entry_completion_get_popup_single_match:
2020 * @completion: a #GtkEntryCompletion
2022 * Returns whether the completion popup window will appear even if there is
2023 * only a single match.
2025 * Return value: %TRUE if the popup window will appear regardless of the
2031 gtk_entry_completion_get_popup_single_match (GtkEntryCompletion *completion)
2033 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), TRUE);
2035 return completion->priv->popup_single_match;
2039 * gtk_entry_completion_set_inline_selection:
2040 * @completion: a #GtkEntryCompletion
2041 * @inline_selection: %TRUE to do inline selection
2043 * Sets whether it is possible to cycle through the possible completions
2049 gtk_entry_completion_set_inline_selection (GtkEntryCompletion *completion,
2050 gboolean inline_selection)
2052 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
2054 inline_selection = inline_selection != FALSE;
2056 if (completion->priv->inline_selection != inline_selection)
2058 completion->priv->inline_selection = inline_selection;
2060 g_object_notify (G_OBJECT (completion), "inline-selection");
2065 * gtk_entry_completion_get_inline_selection:
2066 * @completion: a #GtkEntryCompletion
2068 * Returns %TRUE if inline-selection mode is turned on.
2070 * Returns: %TRUE if inline-selection mode is on
2075 gtk_entry_completion_get_inline_selection (GtkEntryCompletion *completion)
2077 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), FALSE);
2079 return completion->priv->inline_selection;