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.
22 #include "gtkentrycompletion.h"
23 #include "gtkentryprivate.h"
24 #include "gtkcelllayout.h"
27 #include "gtkcellrenderertext.h"
29 #include "gtktreeselection.h"
30 #include "gtktreeview.h"
31 #include "gtkscrolledwindow.h"
33 #include "gtkwindow.h"
36 #include "gtksignal.h"
37 #include "gtkmarshalers.h"
56 PROP_MINIMUM_KEY_LENGTH,
58 PROP_INLINE_COMPLETION,
62 #define GTK_ENTRY_COMPLETION_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ENTRY_COMPLETION, GtkEntryCompletionPrivate))
64 static void gtk_entry_completion_class_init (GtkEntryCompletionClass *klass);
65 static void gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface);
66 static void gtk_entry_completion_init (GtkEntryCompletion *completion);
67 static void gtk_entry_completion_set_property (GObject *object,
71 static void gtk_entry_completion_get_property (GObject *object,
75 static void gtk_entry_completion_finalize (GObject *object);
77 static void gtk_entry_completion_pack_start (GtkCellLayout *cell_layout,
78 GtkCellRenderer *cell,
80 static void gtk_entry_completion_pack_end (GtkCellLayout *cell_layout,
81 GtkCellRenderer *cell,
83 static void gtk_entry_completion_clear (GtkCellLayout *cell_layout);
84 static void gtk_entry_completion_add_attribute (GtkCellLayout *cell_layout,
85 GtkCellRenderer *cell,
86 const char *attribute,
88 static void gtk_entry_completion_set_cell_data_func (GtkCellLayout *cell_layout,
89 GtkCellRenderer *cell,
90 GtkCellLayoutDataFunc func,
92 GDestroyNotify destroy);
93 static void gtk_entry_completion_clear_attributes (GtkCellLayout *cell_layout,
94 GtkCellRenderer *cell);
95 static void gtk_entry_completion_reorder (GtkCellLayout *cell_layout,
96 GtkCellRenderer *cell,
99 static gboolean gtk_entry_completion_visible_func (GtkTreeModel *model,
102 static gboolean gtk_entry_completion_popup_key_press (GtkWidget *widget,
105 static gboolean gtk_entry_completion_popup_button_press (GtkWidget *widget,
106 GdkEventButton *event,
108 static gboolean gtk_entry_completion_list_button_press (GtkWidget *widget,
109 GdkEventButton *event,
111 static gboolean gtk_entry_completion_action_button_press (GtkWidget *widget,
112 GdkEventButton *event,
114 static void gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
116 static gboolean gtk_entry_completion_list_enter_notify (GtkWidget *widget,
117 GdkEventCrossing *event,
119 static gboolean gtk_entry_completion_list_motion_notify (GtkWidget *widget,
120 GdkEventMotion *event,
122 static void gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
126 static void gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
127 GtkCellRenderer *cell,
132 static gboolean gtk_entry_completion_match_selected (GtkEntryCompletion *completion,
135 static gboolean gtk_entry_completion_real_insert_prefix (GtkEntryCompletion *completion,
138 static GObjectClass *parent_class = NULL;
139 static guint entry_completion_signals[LAST_SIGNAL] = { 0 };
143 gtk_entry_completion_get_type (void)
145 static GType entry_completion_type = 0;
147 if (!entry_completion_type)
149 static const GTypeInfo entry_completion_info =
151 sizeof (GtkEntryCompletionClass),
154 (GClassInitFunc) gtk_entry_completion_class_init,
157 sizeof (GtkEntryCompletion),
159 (GInstanceInitFunc) gtk_entry_completion_init
162 static const GInterfaceInfo cell_layout_info =
164 (GInterfaceInitFunc) gtk_entry_completion_cell_layout_init,
169 entry_completion_type =
170 g_type_register_static (G_TYPE_OBJECT, "GtkEntryCompletion",
171 &entry_completion_info, 0);
173 g_type_add_interface_static (entry_completion_type,
174 GTK_TYPE_CELL_LAYOUT,
178 return entry_completion_type;
182 gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
184 GObjectClass *object_class;
186 parent_class = g_type_class_peek_parent (klass);
187 object_class = (GObjectClass *)klass;
189 object_class->set_property = gtk_entry_completion_set_property;
190 object_class->get_property = gtk_entry_completion_get_property;
191 object_class->finalize = gtk_entry_completion_finalize;
193 klass->match_selected = gtk_entry_completion_match_selected;
194 klass->insert_prefix = gtk_entry_completion_real_insert_prefix;
197 * GtkEntryCompletion::insert-prefix:
198 * @widget: the object which received the signal
199 * @prefix: the common prefix of all possible completions
201 * The ::insert-prefix signal is emitted when the inline autocompletion
202 * is triggered. The default behaviour is to make the entry display the
203 * whole prefix and select the newly inserted part.
205 * Applications may connect to this signal in order to insert only a
206 * smaller part of the @prefix into the entry - e.g. the entry used in
207 * the #GtkFileChooser inserts only the part of the prefix up to the
210 * Return value: %TRUE if the signal has been handled
214 entry_completion_signals[INSERT_PREFIX] =
215 g_signal_new ("insert_prefix",
216 G_TYPE_FROM_CLASS (klass),
218 G_STRUCT_OFFSET (GtkEntryCompletionClass, insert_prefix),
219 _gtk_boolean_handled_accumulator, NULL,
220 _gtk_marshal_BOOLEAN__STRING,
225 * GtkEntryCompletion::match-selected:
226 * @widget: the object which received the signal
227 * @model: the #GtkTreeModel containing the matches
228 * @iter: a #GtkTreeIter positioned at the selected match
230 * The ::match-selected signal is emitted when a match from the list
231 * is selected. The default behaviour is to replace the contents of the
232 * entry with the contents of the text column in the row pointed to by
235 * Return value: %TRUE if the signal has been handled
239 entry_completion_signals[MATCH_SELECTED] =
240 g_signal_new ("match_selected",
241 G_TYPE_FROM_CLASS (klass),
243 G_STRUCT_OFFSET (GtkEntryCompletionClass, match_selected),
244 _gtk_boolean_handled_accumulator, NULL,
245 _gtk_marshal_BOOLEAN__OBJECT_BOXED,
251 * GtkEntryCompletion::action-activated:
252 * @widget: the object which received the signal
253 * @index: the index of the activated action
255 * The ::action-activated signal is emitted when an action
260 entry_completion_signals[ACTION_ACTIVATED] =
261 g_signal_new ("action_activated",
262 G_TYPE_FROM_CLASS (klass),
264 G_STRUCT_OFFSET (GtkEntryCompletionClass, action_activated),
266 _gtk_marshal_NONE__INT,
270 g_object_class_install_property (object_class,
272 g_param_spec_object ("model",
273 P_("Completion Model"),
274 P_("The model to find matches in"),
277 g_object_class_install_property (object_class,
278 PROP_MINIMUM_KEY_LENGTH,
279 g_param_spec_int ("minimum_key_length",
280 P_("Minimum Key Length"),
281 P_("Minimum length of the search key in order to look up matches"),
287 * GtkEntryCompletion:text-column:
289 * The column of the model containing the strings.
293 g_object_class_install_property (object_class,
295 g_param_spec_int ("text_column",
297 P_("The column of the model containing the strings."),
304 * GtkEntryCompletion:inline_completion:
306 * The boolean :inline_completion property determines whether the
307 * common prefix of the possible completions should be inserted
308 * automatically in the entry.
312 g_object_class_install_property (object_class,
313 PROP_INLINE_COMPLETION,
314 g_param_spec_boolean ("inline_completion",
315 P_("Inline completion"),
316 P_("Whether the common prefix should be inserted automatically"),
320 * GtkEntryCompletion:popup_completion:
322 * The boolean :popup_completion property determines whether the
323 * possible completions should be shown in a popup window.
327 g_object_class_install_property (object_class,
328 PROP_POPUP_COMPLETION,
329 g_param_spec_boolean ("popup_completion",
330 P_("Popup completion"),
331 P_("Whether the completions should be shown in a popup window"),
335 g_type_class_add_private (object_class, sizeof (GtkEntryCompletionPrivate));
339 gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface)
341 iface->pack_start = gtk_entry_completion_pack_start;
342 iface->pack_end = gtk_entry_completion_pack_end;
343 iface->clear = gtk_entry_completion_clear;
344 iface->add_attribute = gtk_entry_completion_add_attribute;
345 iface->set_cell_data_func = gtk_entry_completion_set_cell_data_func;
346 iface->clear_attributes = gtk_entry_completion_clear_attributes;
347 iface->reorder = gtk_entry_completion_reorder;
351 gtk_entry_completion_init (GtkEntryCompletion *completion)
353 GtkCellRenderer *cell;
354 GtkTreeSelection *sel;
355 GtkEntryCompletionPrivate *priv;
356 GtkWidget *popup_frame;
358 /* yes, also priv, need to keep the code readable */
359 priv = completion->priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (completion);
361 priv->minimum_key_length = 1;
362 priv->text_column = -1;
363 priv->has_completion = FALSE;
364 priv->inline_completion = FALSE;
365 priv->popup_completion = TRUE;
368 priv->filter_model = NULL;
370 priv->tree_view = gtk_tree_view_new ();
371 g_signal_connect (priv->tree_view, "button_press_event",
372 G_CALLBACK (gtk_entry_completion_list_button_press),
374 g_signal_connect (priv->tree_view, "enter_notify_event",
375 G_CALLBACK (gtk_entry_completion_list_enter_notify),
377 g_signal_connect (priv->tree_view, "motion_notify_event",
378 G_CALLBACK (gtk_entry_completion_list_motion_notify),
380 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
381 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view), TRUE);
383 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
384 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
385 gtk_tree_selection_unselect_all (sel);
386 g_signal_connect (sel, "changed",
387 G_CALLBACK (gtk_entry_completion_selection_changed),
389 priv->first_sel_changed = TRUE;
391 priv->column = gtk_tree_view_column_new ();
392 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
394 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
395 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
397 GTK_POLICY_AUTOMATIC);
398 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
401 /* a nasty hack to get the completions treeview to size nicely */
402 gtk_widget_set_size_request (GTK_SCROLLED_WINDOW (priv->scrolled_window)->vscrollbar, -1, 0);
405 priv->actions = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_BOOLEAN);
408 gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->actions));
409 g_signal_connect (priv->action_view, "button_press_event",
410 G_CALLBACK (gtk_entry_completion_action_button_press),
412 g_signal_connect (priv->action_view, "enter_notify_event",
413 G_CALLBACK (gtk_entry_completion_list_enter_notify),
415 g_signal_connect (priv->action_view, "motion_notify_event",
416 G_CALLBACK (gtk_entry_completion_list_motion_notify),
418 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->action_view), FALSE);
419 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->action_view), TRUE);
421 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->action_view));
422 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
423 gtk_tree_selection_unselect_all (sel);
425 cell = gtk_cell_renderer_text_new ();
426 gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (priv->action_view),
429 gtk_entry_completion_action_data_func,
434 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
435 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
436 g_signal_connect (priv->popup_window, "key_press_event",
437 G_CALLBACK (gtk_entry_completion_popup_key_press),
439 g_signal_connect (priv->popup_window, "button_press_event",
440 G_CALLBACK (gtk_entry_completion_popup_button_press),
443 popup_frame = gtk_frame_new (NULL);
444 gtk_frame_set_shadow_type (GTK_FRAME (popup_frame),
445 GTK_SHADOW_ETCHED_IN);
446 gtk_widget_show (popup_frame);
447 gtk_container_add (GTK_CONTAINER (priv->popup_window), popup_frame);
449 priv->vbox = gtk_vbox_new (FALSE, 0);
450 gtk_container_add (GTK_CONTAINER (popup_frame), priv->vbox);
452 gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->tree_view);
453 gtk_box_pack_start (GTK_BOX (priv->vbox), priv->scrolled_window,
456 /* we don't want to see the action treeview when no actions have
457 * been inserted, so we pack the action treeview after the first
458 * action has been added
463 gtk_entry_completion_set_property (GObject *object,
468 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
469 GtkEntryCompletionPrivate *priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (completion);
474 gtk_entry_completion_set_model (completion,
475 g_value_get_object (value));
478 case PROP_MINIMUM_KEY_LENGTH:
479 gtk_entry_completion_set_minimum_key_length (completion,
480 g_value_get_int (value));
483 case PROP_TEXT_COLUMN:
484 priv->text_column = g_value_get_int (value);
487 case PROP_INLINE_COMPLETION:
488 priv->inline_completion = g_value_get_boolean (value);
491 case PROP_POPUP_COMPLETION:
492 priv->popup_completion = g_value_get_boolean (value);
496 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
502 gtk_entry_completion_get_property (GObject *object,
507 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
512 g_value_set_object (value,
513 gtk_entry_completion_get_model (completion));
516 case PROP_MINIMUM_KEY_LENGTH:
517 g_value_set_int (value, gtk_entry_completion_get_minimum_key_length (completion));
520 case PROP_TEXT_COLUMN:
521 g_value_set_int (value, gtk_entry_completion_get_text_column (completion));
524 case PROP_INLINE_COMPLETION:
525 g_value_set_boolean (value, gtk_entry_completion_get_inline_completion (completion));
528 case PROP_POPUP_COMPLETION:
529 g_value_set_boolean (value, gtk_entry_completion_get_popup_completion (completion));
533 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
539 gtk_entry_completion_finalize (GObject *object)
541 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
543 if (completion->priv->tree_view)
544 gtk_widget_destroy (completion->priv->tree_view);
546 if (completion->priv->entry)
547 gtk_entry_set_completion (GTK_ENTRY (completion->priv->entry), NULL);
549 if (completion->priv->actions)
550 g_object_unref (completion->priv->actions);
552 if (completion->priv->case_normalized_key)
553 g_free (completion->priv->case_normalized_key);
555 if (completion->priv->popup_window)
556 gtk_widget_destroy (completion->priv->popup_window);
558 G_OBJECT_CLASS (parent_class)->finalize (object);
561 /* implement cell layout interface */
563 gtk_entry_completion_pack_start (GtkCellLayout *cell_layout,
564 GtkCellRenderer *cell,
567 GtkEntryCompletionPrivate *priv;
569 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
571 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
573 gtk_tree_view_column_pack_start (priv->column, cell, expand);
577 gtk_entry_completion_pack_end (GtkCellLayout *cell_layout,
578 GtkCellRenderer *cell,
581 GtkEntryCompletionPrivate *priv;
583 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
585 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
587 gtk_tree_view_column_pack_end (priv->column, cell, expand);
591 gtk_entry_completion_clear (GtkCellLayout *cell_layout)
593 GtkEntryCompletionPrivate *priv;
595 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
597 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
599 gtk_tree_view_column_clear (priv->column);
603 gtk_entry_completion_add_attribute (GtkCellLayout *cell_layout,
604 GtkCellRenderer *cell,
605 const gchar *attribute,
608 GtkEntryCompletionPrivate *priv;
610 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
612 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
614 gtk_tree_view_column_add_attribute (priv->column, cell, attribute, column);
618 gtk_entry_completion_set_cell_data_func (GtkCellLayout *cell_layout,
619 GtkCellRenderer *cell,
620 GtkCellLayoutDataFunc func,
622 GDestroyNotify destroy)
624 GtkEntryCompletionPrivate *priv;
626 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
628 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
630 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column),
631 cell, func, func_data, destroy);
635 gtk_entry_completion_clear_attributes (GtkCellLayout *cell_layout,
636 GtkCellRenderer *cell)
638 GtkEntryCompletionPrivate *priv;
640 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
642 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
644 gtk_tree_view_column_clear_attributes (priv->column, cell);
648 gtk_entry_completion_reorder (GtkCellLayout *cell_layout,
649 GtkCellRenderer *cell,
652 GtkEntryCompletionPrivate *priv;
654 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
656 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
658 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column), cell, position);
661 /* all those callbacks */
663 gtk_entry_completion_default_completion_func (GtkEntryCompletion *completion,
669 gchar *normalized_string;
670 gchar *case_normalized_string;
672 gboolean ret = FALSE;
676 model = gtk_tree_model_filter_get_model (completion->priv->filter_model);
678 g_return_val_if_fail (gtk_tree_model_get_column_type (model, completion->priv->text_column) == G_TYPE_STRING,
681 gtk_tree_model_get (model, iter,
682 completion->priv->text_column, &item,
687 normalized_string = g_utf8_normalize (item, -1, G_NORMALIZE_ALL);
688 case_normalized_string = g_utf8_casefold (normalized_string, -1);
690 if (!strncmp (key, case_normalized_string, strlen (key)))
694 g_free (normalized_string);
695 g_free (case_normalized_string);
702 gtk_entry_completion_visible_func (GtkTreeModel *model,
706 gboolean ret = FALSE;
708 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
710 if (!completion->priv->case_normalized_key)
713 if (completion->priv->match_func)
714 ret = (* completion->priv->match_func) (completion,
715 completion->priv->case_normalized_key,
717 completion->priv->match_data);
718 else if (completion->priv->text_column >= 0)
719 ret = gtk_entry_completion_default_completion_func (completion,
720 completion->priv->case_normalized_key,
728 gtk_entry_completion_popup_key_press (GtkWidget *widget,
732 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
734 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
737 /* propagate event to the entry */
738 gtk_widget_event (completion->priv->entry, (GdkEvent *)event);
744 gtk_entry_completion_popup_button_press (GtkWidget *widget,
745 GdkEventButton *event,
748 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
750 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
753 /* if we come here, it's usually time to popdown */
754 _gtk_entry_completion_popdown (completion);
760 gtk_entry_completion_list_button_press (GtkWidget *widget,
761 GdkEventButton *event,
764 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
765 GtkTreePath *path = NULL;
767 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
770 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
772 &path, NULL, NULL, NULL))
777 gtk_tree_model_get_iter (GTK_TREE_MODEL (completion->priv->filter_model),
779 gtk_tree_path_free (path);
781 g_signal_handler_block (completion->priv->entry,
782 completion->priv->changed_id);
783 g_signal_emit (completion, entry_completion_signals[MATCH_SELECTED],
784 0, GTK_TREE_MODEL (completion->priv->filter_model),
786 g_signal_handler_unblock (completion->priv->entry,
787 completion->priv->changed_id);
789 _gtk_entry_completion_popdown (completion);
798 gtk_entry_completion_action_button_press (GtkWidget *widget,
799 GdkEventButton *event,
802 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
803 GtkTreePath *path = NULL;
805 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
808 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
810 &path, NULL, NULL, NULL))
812 g_signal_emit (completion, entry_completion_signals[ACTION_ACTIVATED],
813 0, gtk_tree_path_get_indices (path)[0]);
814 gtk_tree_path_free (path);
816 _gtk_entry_completion_popdown (completion);
824 gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
825 GtkCellRenderer *cell,
830 gchar *string = NULL;
833 gtk_tree_model_get (model, iter,
842 g_object_set (G_OBJECT (cell),
847 g_object_set (G_OBJECT (cell),
856 gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
859 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
861 if (completion->priv->first_sel_changed)
863 completion->priv->first_sel_changed = FALSE;
864 if (gtk_widget_is_focus (completion->priv->tree_view))
865 gtk_tree_selection_unselect_all (selection);
872 * gtk_entry_completion_new:
874 * Creates a new #GtkEntryCompletion object.
876 * Return value: A newly created #GtkEntryCompletion object.
881 gtk_entry_completion_new (void)
883 GtkEntryCompletion *completion;
885 completion = g_object_new (GTK_TYPE_ENTRY_COMPLETION, NULL);
891 * gtk_entry_completion_get_entry:
892 * @completion: A #GtkEntryCompletion.
894 * Gets the entry @completion has been attached to.
896 * Return value: The entry @completion has been attached to.
901 gtk_entry_completion_get_entry (GtkEntryCompletion *completion)
903 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
905 return completion->priv->entry;
909 * gtk_entry_completion_set_model:
910 * @completion: A #GtkEntryCompletion.
911 * @model: The #GtkTreeModel.
913 * Sets the model for a #GtkEntryCompletion. If @completion already has
914 * a model set, it will remove it before setting the new model.
915 * If model is %NULL, then it will unset the model.
920 gtk_entry_completion_set_model (GtkEntryCompletion *completion,
923 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
924 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
926 /* code will unref the old filter model (if any) */
927 completion->priv->filter_model =
928 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
929 gtk_tree_model_filter_set_visible_func (completion->priv->filter_model,
930 gtk_entry_completion_visible_func,
933 gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
934 GTK_TREE_MODEL (completion->priv->filter_model));
935 g_object_unref (G_OBJECT (completion->priv->filter_model));
939 * gtk_entry_completion_get_model:
940 * @completion: A #GtkEntryCompletion.
942 * Returns the model the #GtkEntryCompletion is using as data source.
943 * Returns %NULL if the model is unset.
945 * Return value: A #GtkTreeModel, or %NULL if none is currently being used.
950 gtk_entry_completion_get_model (GtkEntryCompletion *completion)
952 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
954 return gtk_tree_model_filter_get_model (completion->priv->filter_model);
958 * gtk_entry_completion_set_match_func:
959 * @completion: A #GtkEntryCompletion.
960 * @func: The #GtkEntryCompletionMatchFunc to use.
961 * @func_data: The user data for @func.
962 * @func_notify: Destroy notifier for @func_data.
964 * Sets the match function for @completion to be @func. The match function
965 * is used to determine if a row should or should not be in the completion
971 gtk_entry_completion_set_match_func (GtkEntryCompletion *completion,
972 GtkEntryCompletionMatchFunc func,
974 GDestroyNotify func_notify)
976 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
978 if (completion->priv->match_notify)
979 (* completion->priv->match_notify) (completion->priv->match_data);
981 completion->priv->match_func = func;
982 completion->priv->match_data = func_data;
983 completion->priv->match_notify = func_notify;
987 * gtk_entry_completion_set_minimum_key_length:
988 * @completion: A #GtkEntryCompletion.
989 * @length: The minimum length of the key in order to start completing.
991 * Requires the length of the search key for @completion to be at least
992 * @length. This is useful for long lists, where completing using a small
993 * key takes a lot of time and will come up with meaningless results anyway
994 * (ie, a too large dataset).
999 gtk_entry_completion_set_minimum_key_length (GtkEntryCompletion *completion,
1002 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1003 g_return_if_fail (length >= 1);
1005 completion->priv->minimum_key_length = length;
1009 * gtk_entry_completion_get_minimum_key_length:
1010 * @completion: A #GtkEntryCompletion.
1012 * Returns the minimum key length as set for @completion.
1014 * Return value: The currently used minimum key length.
1019 gtk_entry_completion_get_minimum_key_length (GtkEntryCompletion *completion)
1021 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), 0);
1023 return completion->priv->minimum_key_length;
1027 * gtk_entry_completion_complete:
1028 * @completion: A #GtkEntryCompletion.
1030 * Requests a completion operation, or in other words a refiltering of the
1031 * current list with completions, using the current key. The completion list
1032 * view will be updated accordingly.
1037 gtk_entry_completion_complete (GtkEntryCompletion *completion)
1041 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1042 g_return_if_fail (completion->priv->filter_model != NULL);
1044 if (completion->priv->case_normalized_key)
1045 g_free (completion->priv->case_normalized_key);
1047 tmp = g_utf8_normalize (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)),
1048 -1, G_NORMALIZE_ALL);
1049 completion->priv->case_normalized_key = g_utf8_casefold (tmp, -1);
1052 gtk_tree_model_filter_refilter (completion->priv->filter_model);
1056 gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
1058 const gchar *string,
1063 gtk_list_store_insert (completion->priv->actions, &iter, index);
1064 gtk_list_store_set (completion->priv->actions, &iter,
1069 if (!completion->priv->action_view->parent)
1071 GtkTreePath *path = gtk_tree_path_new_from_indices (0, -1);
1073 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
1075 gtk_tree_path_free (path);
1077 gtk_box_pack_start (GTK_BOX (completion->priv->vbox),
1078 completion->priv->action_view, FALSE, FALSE, 0);
1079 gtk_widget_show (completion->priv->action_view);
1084 * gtk_entry_completion_insert_action_text:
1085 * @completion: A #GtkEntryCompletion.
1086 * @index_: The index of the item to insert.
1087 * @text: Text of the item to insert.
1089 * Inserts an action in @completion's action item list at position @index_
1090 * with text @text. If you want the action item to have markup, use
1091 * gtk_entry_completion_insert_action_markup().
1096 gtk_entry_completion_insert_action_text (GtkEntryCompletion *completion,
1100 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1101 g_return_if_fail (text != NULL);
1103 gtk_entry_completion_insert_action (completion, index_, text, FALSE);
1107 * gtk_entry_completion_insert_action_markup:
1108 * @completion: A #GtkEntryCompletion.
1109 * @index_: The index of the item to insert.
1110 * @markup: Markup of the item to insert.
1112 * Inserts an action in @completion's action item list at position @index_
1113 * with markup @markup.
1118 gtk_entry_completion_insert_action_markup (GtkEntryCompletion *completion,
1120 const gchar *markup)
1122 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1123 g_return_if_fail (markup != NULL);
1125 gtk_entry_completion_insert_action (completion, index_, markup, TRUE);
1129 * gtk_entry_completion_delete_action:
1130 * @completion: A #GtkEntryCompletion.
1131 * @index_: The index of the item to Delete.
1133 * Deletes the action at @index_ from @completion's action list.
1138 gtk_entry_completion_delete_action (GtkEntryCompletion *completion,
1143 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1144 g_return_if_fail (index_ >= 0);
1146 gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (completion->priv->actions),
1147 &iter, NULL, index_);
1148 gtk_list_store_remove (completion->priv->actions, &iter);
1152 * gtk_entry_completion_set_text_column:
1153 * @completion: A #GtkEntryCompletion.
1154 * @column: The column in the model of @completion to get strings from.
1156 * Convenience function for setting up the most used case of this code: a
1157 * completion list with just strings. This function will set up @completion
1158 * to have a list displaying all (and just) strings in the completion list,
1159 * and to get those strings from @column in the model of @completion.
1161 * This functions creates and adds a #GtkCellRendererText for the selected
1162 * column. If you need to set the text column, but don't want the cell
1163 * renderer, use g_object_set() to set the ::text_column property directly.
1168 gtk_entry_completion_set_text_column (GtkEntryCompletion *completion,
1171 GtkCellRenderer *cell;
1173 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1174 g_return_if_fail (column >= 0);
1176 completion->priv->text_column = column;
1178 cell = gtk_cell_renderer_text_new ();
1179 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion),
1181 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (completion),
1185 g_object_notify (G_OBJECT (completion), "text_column");
1189 * gtk_entry_completion_get_text_column:
1190 * @completion: a #GtkEntryCompletion
1192 * Returns the column in the model of @completion to get strings from.
1194 * Return value: the column containing the strings
1199 gtk_entry_completion_get_text_column (GtkEntryCompletion *completion)
1201 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), -1);
1203 return completion->priv->text_column;
1208 /* lame copy from gtkentry.c */
1210 get_borders (GtkEntry *entry,
1214 GtkWidget *widget = GTK_WIDGET (entry);
1216 gboolean interior_focus;
1218 gtk_widget_style_get (widget,
1219 "interior-focus", &interior_focus,
1220 "focus-line-width", &focus_width,
1223 if (entry->has_frame)
1225 *xborder = widget->style->xthickness;
1226 *yborder = widget->style->ythickness;
1234 if (!interior_focus)
1236 *xborder += focus_width;
1237 *yborder += focus_width;
1242 gtk_entry_completion_list_enter_notify (GtkWidget *widget,
1243 GdkEventCrossing *event,
1246 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1248 return completion->priv->ignore_enter;
1252 gtk_entry_completion_list_motion_notify (GtkWidget *widget,
1253 GdkEventMotion *event,
1256 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1258 completion->priv->ignore_enter = FALSE;
1264 /* some nasty size requisition */
1266 _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
1269 gint matches, items, height, x_border, y_border;
1272 GdkRectangle monitor;
1273 GtkRequisition popup_req;
1274 GtkRequisition entry_req;
1279 gdk_window_get_origin (completion->priv->entry->window, &x, &y);
1280 get_borders (GTK_ENTRY (completion->priv->entry), &x_border, &y_border);
1285 matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
1287 items = MIN (matches, 15);
1289 gtk_tree_view_column_cell_get_size (completion->priv->column, NULL,
1290 NULL, NULL, NULL, &height);
1293 gtk_widget_hide (completion->priv->scrolled_window);
1295 gtk_widget_show (completion->priv->scrolled_window);
1297 screen = gtk_widget_get_screen (GTK_WIDGET (completion->priv->entry));
1298 monitor_num = gdk_screen_get_monitor_at_window (screen,
1299 GTK_WIDGET (completion->priv->entry)->window);
1300 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1302 width = MIN (completion->priv->entry->allocation.width, monitor.width);
1303 gtk_widget_set_size_request (completion->priv->tree_view,
1304 width - 2 * x_border, items * height);
1306 /* default on no match */
1307 completion->priv->current_selected = -1;
1309 items = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
1313 gtk_widget_show (completion->priv->action_view);
1315 gtk_tree_view_column_cell_get_size (gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0),
1316 NULL, NULL, NULL, NULL,
1319 gtk_widget_set_size_request (completion->priv->action_view,
1320 width - 2 * x_border, items * height);
1323 gtk_widget_hide (completion->priv->action_view);
1325 gtk_widget_size_request (completion->priv->popup_window, &popup_req);
1326 gtk_widget_size_request (completion->priv->entry, &entry_req);
1330 else if (x + popup_req.width > monitor.x + monitor.width)
1331 x = monitor.x + monitor.width - popup_req.width;
1333 if (y + entry_req.height + popup_req.height <= monitor.y + monitor.height)
1335 y += entry_req.height;
1340 y -= popup_req.height;
1346 path = gtk_tree_path_new_from_indices (above ? matches - 1 : 0, -1);
1347 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (completion->priv->tree_view), path,
1348 NULL, FALSE, 0.0, 0.0);
1349 gtk_tree_path_free (path);
1352 gtk_window_move (GTK_WINDOW (completion->priv->popup_window), x, y);
1358 _gtk_entry_completion_popup (GtkEntryCompletion *completion)
1360 GtkTreeViewColumn *column;
1363 if (GTK_WIDGET_MAPPED (completion->priv->popup_window))
1366 completion->priv->ignore_enter = TRUE;
1368 column = gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0);
1369 renderers = gtk_tree_view_column_get_cell_renderers (column);
1370 gtk_widget_ensure_style (completion->priv->tree_view);
1371 g_object_set (GTK_CELL_RENDERER (renderers->data), "cell_background_gdk",
1372 &completion->priv->tree_view->style->bg[GTK_STATE_NORMAL],
1374 g_list_free (renderers);
1376 gtk_widget_show_all (completion->priv->vbox);
1378 _gtk_entry_completion_resize_popup (completion);
1380 gtk_widget_show (completion->priv->popup_window);
1382 gtk_grab_add (completion->priv->popup_window);
1383 gdk_pointer_grab (completion->priv->popup_window->window, TRUE,
1384 GDK_BUTTON_PRESS_MASK |
1385 GDK_BUTTON_RELEASE_MASK |
1386 GDK_POINTER_MOTION_MASK,
1387 NULL, NULL, GDK_CURRENT_TIME);
1391 _gtk_entry_completion_popdown (GtkEntryCompletion *completion)
1393 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
1396 completion->priv->ignore_enter = FALSE;
1398 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1399 gtk_grab_remove (completion->priv->popup_window);
1401 gtk_widget_hide (completion->priv->popup_window);
1405 gtk_entry_completion_match_selected (GtkEntryCompletion *completion,
1406 GtkTreeModel *model,
1411 gtk_tree_model_get (model, iter, completion->priv->text_column, &str, -1);
1412 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), str);
1414 /* move cursor to the end */
1415 gtk_editable_set_position (GTK_EDITABLE (completion->priv->entry), -1);
1424 gtk_entry_completion_compute_prefix (GtkEntryCompletion *completion)
1427 gchar *prefix = NULL;
1430 const gchar *key = gtk_entry_get_text (GTK_ENTRY (completion->priv->entry));
1432 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (completion->priv->filter_model),
1439 gtk_tree_model_get (GTK_TREE_MODEL (completion->priv->filter_model),
1440 &iter, completion->priv->text_column, &text,
1443 if (g_str_has_prefix (text, key))
1446 prefix = g_strdup (text);
1450 const gchar *q = text;
1452 while (*p && *p == *q)
1463 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (completion->priv->filter_model),
1472 gtk_entry_completion_real_insert_prefix (GtkEntryCompletion *completion,
1482 prefix_len = g_utf8_strlen (prefix, -1);
1484 key = gtk_entry_get_text (GTK_ENTRY (completion->priv->entry));
1485 key_len = g_utf8_strlen (key, -1);
1487 if (prefix_len > key_len)
1489 gtk_editable_insert_text (GTK_EDITABLE (completion->priv->entry),
1490 prefix + key_len, -1, &pos);
1491 gtk_editable_select_region (GTK_EDITABLE (completion->priv->entry),
1492 key_len, prefix_len);
1494 completion->priv->has_completion = TRUE;
1502 * gtk_entry_completion_insert_prefix:
1503 * @completion: a #GtkEntryCompletion
1505 * Requests a prefix insertion.
1510 gtk_entry_completion_insert_prefix (GtkEntryCompletion *completion)
1515 g_signal_handler_block (completion->priv->entry,
1516 completion->priv->insert_text_id);
1517 prefix = gtk_entry_completion_compute_prefix (completion);
1520 g_signal_emit (completion, entry_completion_signals[INSERT_PREFIX],
1524 g_signal_handler_unblock (completion->priv->entry,
1525 completion->priv->insert_text_id);
1529 * gtk_entry_completion_set_inline_completion:
1530 * @completion: a #GtkEntryCompletion
1531 * @inline_completion: %TRUE to do inline completion
1533 * Sets whether the common prefix of the possible completions should
1534 * be automatically inserted in the entry.
1539 gtk_entry_completion_set_inline_completion (GtkEntryCompletion *completion,
1540 gboolean inline_completion)
1542 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1544 inline_completion = inline_completion != FALSE;
1546 if (completion->priv->inline_completion != inline_completion)
1548 completion->priv->inline_completion = inline_completion;
1550 g_object_notify (G_OBJECT (completion), "inline_completion");
1556 * gtk_entry_completion_get_inline_completion:
1557 * @completion: a #GtkEntryCompletion
1559 * Returns whether the common prefix of the possible completions should
1560 * be automatically inserted in the entry.
1562 * Return value: %TRUE if inline completion is turned on
1567 gtk_entry_completion_get_inline_completion (GtkEntryCompletion *completion)
1569 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), FALSE);
1571 return completion->priv->inline_completion;
1575 * gtk_entry_completion_set_popup_completion:
1576 * @completion: a #GtkEntryCompletion
1577 * @popup_completion: %TRUE to do popup completion
1579 * Sets whether the completions should be presented in a popup window.
1584 gtk_entry_completion_set_popup_completion (GtkEntryCompletion *completion,
1585 gboolean popup_completion)
1587 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1589 popup_completion = popup_completion != FALSE;
1591 if (completion->priv->popup_completion != popup_completion)
1593 completion->priv->popup_completion = popup_completion;
1595 g_object_notify (G_OBJECT (completion), "popup_completion");
1601 * gtk_entry_completion_get_popup_completion:
1602 * @completion: a #GtkEntryCompletion
1604 * Returns whether the completions should be presented in a popup window.
1606 * Return value: %TRUE if popup completion is turned on
1611 gtk_entry_completion_get_popup_completion (GtkEntryCompletion *completion)
1613 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), TRUE);
1615 return completion->priv->popup_completion;