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 * Gets emitted when the inline autocompletion is triggered.
202 * 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 * Gets emitted when a match from the list is selected.
231 * The default behaviour is to replace the contents of the
232 * entry with the contents of the text column in the row
233 * pointed to by @iter.
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 * Gets emitted when an action is activated.
259 entry_completion_signals[ACTION_ACTIVATED] =
260 g_signal_new ("action_activated",
261 G_TYPE_FROM_CLASS (klass),
263 G_STRUCT_OFFSET (GtkEntryCompletionClass, action_activated),
265 _gtk_marshal_NONE__INT,
269 g_object_class_install_property (object_class,
271 g_param_spec_object ("model",
272 P_("Completion Model"),
273 P_("The model to find matches in"),
276 g_object_class_install_property (object_class,
277 PROP_MINIMUM_KEY_LENGTH,
278 g_param_spec_int ("minimum_key_length",
279 P_("Minimum Key Length"),
280 P_("Minimum length of the search key in order to look up matches"),
286 * GtkEntryCompletion:text-column:
288 * The column of the model containing the strings.
292 g_object_class_install_property (object_class,
294 g_param_spec_int ("text_column",
296 P_("The column of the model containing the strings."),
303 * GtkEntryCompletion:inline-completion:
305 * Determines whether the common prefix of the possible completions
306 * should be inserted automatically in the entry.
310 g_object_class_install_property (object_class,
311 PROP_INLINE_COMPLETION,
312 g_param_spec_boolean ("inline_completion",
313 P_("Inline completion"),
314 P_("Whether the common prefix should be inserted automatically"),
318 * GtkEntryCompletion:popup-completion:
320 * Determines whether the possible completions should be
321 * shown in a popup window.
325 g_object_class_install_property (object_class,
326 PROP_POPUP_COMPLETION,
327 g_param_spec_boolean ("popup_completion",
328 P_("Popup completion"),
329 P_("Whether the completions should be shown in a popup window"),
333 g_type_class_add_private (object_class, sizeof (GtkEntryCompletionPrivate));
337 gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface)
339 iface->pack_start = gtk_entry_completion_pack_start;
340 iface->pack_end = gtk_entry_completion_pack_end;
341 iface->clear = gtk_entry_completion_clear;
342 iface->add_attribute = gtk_entry_completion_add_attribute;
343 iface->set_cell_data_func = gtk_entry_completion_set_cell_data_func;
344 iface->clear_attributes = gtk_entry_completion_clear_attributes;
345 iface->reorder = gtk_entry_completion_reorder;
349 gtk_entry_completion_init (GtkEntryCompletion *completion)
351 GtkCellRenderer *cell;
352 GtkTreeSelection *sel;
353 GtkEntryCompletionPrivate *priv;
354 GtkWidget *popup_frame;
356 /* yes, also priv, need to keep the code readable */
357 priv = completion->priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (completion);
359 priv->minimum_key_length = 1;
360 priv->text_column = -1;
361 priv->has_completion = FALSE;
362 priv->inline_completion = FALSE;
363 priv->popup_completion = TRUE;
366 priv->filter_model = NULL;
368 priv->tree_view = gtk_tree_view_new ();
369 g_signal_connect (priv->tree_view, "button_press_event",
370 G_CALLBACK (gtk_entry_completion_list_button_press),
372 g_signal_connect (priv->tree_view, "enter_notify_event",
373 G_CALLBACK (gtk_entry_completion_list_enter_notify),
375 g_signal_connect (priv->tree_view, "motion_notify_event",
376 G_CALLBACK (gtk_entry_completion_list_motion_notify),
378 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
379 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view), TRUE);
381 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
382 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
383 gtk_tree_selection_unselect_all (sel);
384 g_signal_connect (sel, "changed",
385 G_CALLBACK (gtk_entry_completion_selection_changed),
387 priv->first_sel_changed = TRUE;
389 priv->column = gtk_tree_view_column_new ();
390 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
392 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
393 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
395 GTK_POLICY_AUTOMATIC);
396 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
399 /* a nasty hack to get the completions treeview to size nicely */
400 gtk_widget_set_size_request (GTK_SCROLLED_WINDOW (priv->scrolled_window)->vscrollbar, -1, 0);
403 priv->actions = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_BOOLEAN);
406 gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->actions));
407 g_signal_connect (priv->action_view, "button_press_event",
408 G_CALLBACK (gtk_entry_completion_action_button_press),
410 g_signal_connect (priv->action_view, "enter_notify_event",
411 G_CALLBACK (gtk_entry_completion_list_enter_notify),
413 g_signal_connect (priv->action_view, "motion_notify_event",
414 G_CALLBACK (gtk_entry_completion_list_motion_notify),
416 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->action_view), FALSE);
417 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->action_view), TRUE);
419 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->action_view));
420 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
421 gtk_tree_selection_unselect_all (sel);
423 cell = gtk_cell_renderer_text_new ();
424 gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (priv->action_view),
427 gtk_entry_completion_action_data_func,
432 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
433 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
434 g_signal_connect (priv->popup_window, "key_press_event",
435 G_CALLBACK (gtk_entry_completion_popup_key_press),
437 g_signal_connect (priv->popup_window, "button_press_event",
438 G_CALLBACK (gtk_entry_completion_popup_button_press),
441 popup_frame = gtk_frame_new (NULL);
442 gtk_frame_set_shadow_type (GTK_FRAME (popup_frame),
443 GTK_SHADOW_ETCHED_IN);
444 gtk_widget_show (popup_frame);
445 gtk_container_add (GTK_CONTAINER (priv->popup_window), popup_frame);
447 priv->vbox = gtk_vbox_new (FALSE, 0);
448 gtk_container_add (GTK_CONTAINER (popup_frame), priv->vbox);
450 gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->tree_view);
451 gtk_box_pack_start (GTK_BOX (priv->vbox), priv->scrolled_window,
454 /* we don't want to see the action treeview when no actions have
455 * been inserted, so we pack the action treeview after the first
456 * action has been added
461 gtk_entry_completion_set_property (GObject *object,
466 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
467 GtkEntryCompletionPrivate *priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (completion);
472 gtk_entry_completion_set_model (completion,
473 g_value_get_object (value));
476 case PROP_MINIMUM_KEY_LENGTH:
477 gtk_entry_completion_set_minimum_key_length (completion,
478 g_value_get_int (value));
481 case PROP_TEXT_COLUMN:
482 priv->text_column = g_value_get_int (value);
485 case PROP_INLINE_COMPLETION:
486 priv->inline_completion = g_value_get_boolean (value);
489 case PROP_POPUP_COMPLETION:
490 priv->popup_completion = g_value_get_boolean (value);
494 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
500 gtk_entry_completion_get_property (GObject *object,
505 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
510 g_value_set_object (value,
511 gtk_entry_completion_get_model (completion));
514 case PROP_MINIMUM_KEY_LENGTH:
515 g_value_set_int (value, gtk_entry_completion_get_minimum_key_length (completion));
518 case PROP_TEXT_COLUMN:
519 g_value_set_int (value, gtk_entry_completion_get_text_column (completion));
522 case PROP_INLINE_COMPLETION:
523 g_value_set_boolean (value, gtk_entry_completion_get_inline_completion (completion));
526 case PROP_POPUP_COMPLETION:
527 g_value_set_boolean (value, gtk_entry_completion_get_popup_completion (completion));
531 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
537 gtk_entry_completion_finalize (GObject *object)
539 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
541 if (completion->priv->tree_view)
542 gtk_widget_destroy (completion->priv->tree_view);
544 if (completion->priv->entry)
545 gtk_entry_set_completion (GTK_ENTRY (completion->priv->entry), NULL);
547 if (completion->priv->actions)
548 g_object_unref (completion->priv->actions);
550 if (completion->priv->case_normalized_key)
551 g_free (completion->priv->case_normalized_key);
553 if (completion->priv->popup_window)
554 gtk_widget_destroy (completion->priv->popup_window);
556 G_OBJECT_CLASS (parent_class)->finalize (object);
559 /* implement cell layout interface */
561 gtk_entry_completion_pack_start (GtkCellLayout *cell_layout,
562 GtkCellRenderer *cell,
565 GtkEntryCompletionPrivate *priv;
567 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
569 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
571 gtk_tree_view_column_pack_start (priv->column, cell, expand);
575 gtk_entry_completion_pack_end (GtkCellLayout *cell_layout,
576 GtkCellRenderer *cell,
579 GtkEntryCompletionPrivate *priv;
581 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
583 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
585 gtk_tree_view_column_pack_end (priv->column, cell, expand);
589 gtk_entry_completion_clear (GtkCellLayout *cell_layout)
591 GtkEntryCompletionPrivate *priv;
593 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
595 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
597 gtk_tree_view_column_clear (priv->column);
601 gtk_entry_completion_add_attribute (GtkCellLayout *cell_layout,
602 GtkCellRenderer *cell,
603 const gchar *attribute,
606 GtkEntryCompletionPrivate *priv;
608 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
610 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
612 gtk_tree_view_column_add_attribute (priv->column, cell, attribute, column);
616 gtk_entry_completion_set_cell_data_func (GtkCellLayout *cell_layout,
617 GtkCellRenderer *cell,
618 GtkCellLayoutDataFunc func,
620 GDestroyNotify destroy)
622 GtkEntryCompletionPrivate *priv;
624 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
626 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
628 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column),
629 cell, func, func_data, destroy);
633 gtk_entry_completion_clear_attributes (GtkCellLayout *cell_layout,
634 GtkCellRenderer *cell)
636 GtkEntryCompletionPrivate *priv;
638 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
640 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
642 gtk_tree_view_column_clear_attributes (priv->column, cell);
646 gtk_entry_completion_reorder (GtkCellLayout *cell_layout,
647 GtkCellRenderer *cell,
650 GtkEntryCompletionPrivate *priv;
652 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
654 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
656 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column), cell, position);
659 /* all those callbacks */
661 gtk_entry_completion_default_completion_func (GtkEntryCompletion *completion,
667 gchar *normalized_string;
668 gchar *case_normalized_string;
670 gboolean ret = FALSE;
674 model = gtk_tree_model_filter_get_model (completion->priv->filter_model);
676 g_return_val_if_fail (gtk_tree_model_get_column_type (model, completion->priv->text_column) == G_TYPE_STRING,
679 gtk_tree_model_get (model, iter,
680 completion->priv->text_column, &item,
685 normalized_string = g_utf8_normalize (item, -1, G_NORMALIZE_ALL);
686 case_normalized_string = g_utf8_casefold (normalized_string, -1);
688 if (!strncmp (key, case_normalized_string, strlen (key)))
692 g_free (normalized_string);
693 g_free (case_normalized_string);
700 gtk_entry_completion_visible_func (GtkTreeModel *model,
704 gboolean ret = FALSE;
706 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
708 if (!completion->priv->case_normalized_key)
711 if (completion->priv->match_func)
712 ret = (* completion->priv->match_func) (completion,
713 completion->priv->case_normalized_key,
715 completion->priv->match_data);
716 else if (completion->priv->text_column >= 0)
717 ret = gtk_entry_completion_default_completion_func (completion,
718 completion->priv->case_normalized_key,
726 gtk_entry_completion_popup_key_press (GtkWidget *widget,
730 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
732 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
735 /* propagate event to the entry */
736 gtk_widget_event (completion->priv->entry, (GdkEvent *)event);
742 gtk_entry_completion_popup_button_press (GtkWidget *widget,
743 GdkEventButton *event,
746 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
748 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
751 /* if we come here, it's usually time to popdown */
752 _gtk_entry_completion_popdown (completion);
758 gtk_entry_completion_list_button_press (GtkWidget *widget,
759 GdkEventButton *event,
762 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
763 GtkTreePath *path = NULL;
765 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
768 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
770 &path, NULL, NULL, NULL))
775 gtk_tree_model_get_iter (GTK_TREE_MODEL (completion->priv->filter_model),
777 gtk_tree_path_free (path);
779 g_signal_handler_block (completion->priv->entry,
780 completion->priv->changed_id);
781 g_signal_emit (completion, entry_completion_signals[MATCH_SELECTED],
782 0, GTK_TREE_MODEL (completion->priv->filter_model),
784 g_signal_handler_unblock (completion->priv->entry,
785 completion->priv->changed_id);
787 _gtk_entry_completion_popdown (completion);
796 gtk_entry_completion_action_button_press (GtkWidget *widget,
797 GdkEventButton *event,
800 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
801 GtkTreePath *path = NULL;
803 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
806 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
808 &path, NULL, NULL, NULL))
810 g_signal_emit (completion, entry_completion_signals[ACTION_ACTIVATED],
811 0, gtk_tree_path_get_indices (path)[0]);
812 gtk_tree_path_free (path);
814 _gtk_entry_completion_popdown (completion);
822 gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
823 GtkCellRenderer *cell,
828 gchar *string = NULL;
831 gtk_tree_model_get (model, iter,
840 g_object_set (G_OBJECT (cell),
845 g_object_set (G_OBJECT (cell),
854 gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
857 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
859 if (completion->priv->first_sel_changed)
861 completion->priv->first_sel_changed = FALSE;
862 if (gtk_widget_is_focus (completion->priv->tree_view))
863 gtk_tree_selection_unselect_all (selection);
870 * gtk_entry_completion_new:
872 * Creates a new #GtkEntryCompletion object.
874 * Return value: A newly created #GtkEntryCompletion object.
879 gtk_entry_completion_new (void)
881 GtkEntryCompletion *completion;
883 completion = g_object_new (GTK_TYPE_ENTRY_COMPLETION, NULL);
889 * gtk_entry_completion_get_entry:
890 * @completion: A #GtkEntryCompletion.
892 * Gets the entry @completion has been attached to.
894 * Return value: The entry @completion has been attached to.
899 gtk_entry_completion_get_entry (GtkEntryCompletion *completion)
901 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
903 return completion->priv->entry;
907 filter_model_changed_cb (GtkTreeModel *model,
912 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
914 if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
915 _gtk_entry_completion_resize_popup (completion);
919 * gtk_entry_completion_set_model:
920 * @completion: A #GtkEntryCompletion.
921 * @model: The #GtkTreeModel.
923 * Sets the model for a #GtkEntryCompletion. If @completion already has
924 * a model set, it will remove it before setting the new model.
925 * If model is %NULL, then it will unset the model.
930 gtk_entry_completion_set_model (GtkEntryCompletion *completion,
933 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
934 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
938 gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
940 _gtk_entry_completion_popdown (completion);
941 completion->priv->filter_model = NULL;
945 /* code will unref the old filter model (if any) */
946 completion->priv->filter_model =
947 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
948 gtk_tree_model_filter_set_visible_func (completion->priv->filter_model,
949 gtk_entry_completion_visible_func,
953 gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
954 GTK_TREE_MODEL (completion->priv->filter_model));
955 g_object_unref (G_OBJECT (completion->priv->filter_model));
957 if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
958 _gtk_entry_completion_resize_popup (completion);
962 * gtk_entry_completion_get_model:
963 * @completion: A #GtkEntryCompletion.
965 * Returns the model the #GtkEntryCompletion is using as data source.
966 * Returns %NULL if the model is unset.
968 * Return value: A #GtkTreeModel, or %NULL if none is currently being used.
973 gtk_entry_completion_get_model (GtkEntryCompletion *completion)
975 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
977 if (!completion->priv->filter_model)
980 return gtk_tree_model_filter_get_model (completion->priv->filter_model);
984 * gtk_entry_completion_set_match_func:
985 * @completion: A #GtkEntryCompletion.
986 * @func: The #GtkEntryCompletionMatchFunc to use.
987 * @func_data: The user data for @func.
988 * @func_notify: Destroy notifier for @func_data.
990 * Sets the match function for @completion to be @func. The match function
991 * is used to determine if a row should or should not be in the completion
997 gtk_entry_completion_set_match_func (GtkEntryCompletion *completion,
998 GtkEntryCompletionMatchFunc func,
1000 GDestroyNotify func_notify)
1002 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1004 if (completion->priv->match_notify)
1005 (* completion->priv->match_notify) (completion->priv->match_data);
1007 completion->priv->match_func = func;
1008 completion->priv->match_data = func_data;
1009 completion->priv->match_notify = func_notify;
1013 * gtk_entry_completion_set_minimum_key_length:
1014 * @completion: A #GtkEntryCompletion.
1015 * @length: The minimum length of the key in order to start completing.
1017 * Requires the length of the search key for @completion to be at least
1018 * @length. This is useful for long lists, where completing using a small
1019 * key takes a lot of time and will come up with meaningless results anyway
1020 * (ie, a too large dataset).
1025 gtk_entry_completion_set_minimum_key_length (GtkEntryCompletion *completion,
1028 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1029 g_return_if_fail (length >= 1);
1031 completion->priv->minimum_key_length = length;
1035 * gtk_entry_completion_get_minimum_key_length:
1036 * @completion: A #GtkEntryCompletion.
1038 * Returns the minimum key length as set for @completion.
1040 * Return value: The currently used minimum key length.
1045 gtk_entry_completion_get_minimum_key_length (GtkEntryCompletion *completion)
1047 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), 0);
1049 return completion->priv->minimum_key_length;
1053 * gtk_entry_completion_complete:
1054 * @completion: A #GtkEntryCompletion.
1056 * Requests a completion operation, or in other words a refiltering of the
1057 * current list with completions, using the current key. The completion list
1058 * view will be updated accordingly.
1063 gtk_entry_completion_complete (GtkEntryCompletion *completion)
1067 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1069 if (!completion->priv->filter_model)
1072 if (completion->priv->case_normalized_key)
1073 g_free (completion->priv->case_normalized_key);
1075 tmp = g_utf8_normalize (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)),
1076 -1, G_NORMALIZE_ALL);
1077 completion->priv->case_normalized_key = g_utf8_casefold (tmp, -1);
1080 gtk_tree_model_filter_refilter (completion->priv->filter_model);
1084 gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
1086 const gchar *string,
1091 gtk_list_store_insert (completion->priv->actions, &iter, index);
1092 gtk_list_store_set (completion->priv->actions, &iter,
1097 if (!completion->priv->action_view->parent)
1099 GtkTreePath *path = gtk_tree_path_new_from_indices (0, -1);
1101 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
1103 gtk_tree_path_free (path);
1105 gtk_box_pack_start (GTK_BOX (completion->priv->vbox),
1106 completion->priv->action_view, FALSE, FALSE, 0);
1107 gtk_widget_show (completion->priv->action_view);
1112 * gtk_entry_completion_insert_action_text:
1113 * @completion: A #GtkEntryCompletion.
1114 * @index_: The index of the item to insert.
1115 * @text: Text of the item to insert.
1117 * Inserts an action in @completion's action item list at position @index_
1118 * with text @text. If you want the action item to have markup, use
1119 * gtk_entry_completion_insert_action_markup().
1124 gtk_entry_completion_insert_action_text (GtkEntryCompletion *completion,
1128 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1129 g_return_if_fail (text != NULL);
1131 gtk_entry_completion_insert_action (completion, index_, text, FALSE);
1135 * gtk_entry_completion_insert_action_markup:
1136 * @completion: A #GtkEntryCompletion.
1137 * @index_: The index of the item to insert.
1138 * @markup: Markup of the item to insert.
1140 * Inserts an action in @completion's action item list at position @index_
1141 * with markup @markup.
1146 gtk_entry_completion_insert_action_markup (GtkEntryCompletion *completion,
1148 const gchar *markup)
1150 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1151 g_return_if_fail (markup != NULL);
1153 gtk_entry_completion_insert_action (completion, index_, markup, TRUE);
1157 * gtk_entry_completion_delete_action:
1158 * @completion: A #GtkEntryCompletion.
1159 * @index_: The index of the item to Delete.
1161 * Deletes the action at @index_ from @completion's action list.
1166 gtk_entry_completion_delete_action (GtkEntryCompletion *completion,
1171 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1172 g_return_if_fail (index_ >= 0);
1174 gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (completion->priv->actions),
1175 &iter, NULL, index_);
1176 gtk_list_store_remove (completion->priv->actions, &iter);
1180 * gtk_entry_completion_set_text_column:
1181 * @completion: A #GtkEntryCompletion.
1182 * @column: The column in the model of @completion to get strings from.
1184 * Convenience function for setting up the most used case of this code: a
1185 * completion list with just strings. This function will set up @completion
1186 * to have a list displaying all (and just) strings in the completion list,
1187 * and to get those strings from @column in the model of @completion.
1189 * This functions creates and adds a #GtkCellRendererText for the selected
1190 * column. If you need to set the text column, but don't want the cell
1191 * renderer, use g_object_set() to set the ::text_column property directly.
1196 gtk_entry_completion_set_text_column (GtkEntryCompletion *completion,
1199 GtkCellRenderer *cell;
1201 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1202 g_return_if_fail (column >= 0);
1204 completion->priv->text_column = column;
1206 cell = gtk_cell_renderer_text_new ();
1207 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion),
1209 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (completion),
1213 g_object_notify (G_OBJECT (completion), "text_column");
1217 * gtk_entry_completion_get_text_column:
1218 * @completion: a #GtkEntryCompletion
1220 * Returns the column in the model of @completion to get strings from.
1222 * Return value: the column containing the strings
1227 gtk_entry_completion_get_text_column (GtkEntryCompletion *completion)
1229 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), -1);
1231 return completion->priv->text_column;
1237 gtk_entry_completion_list_enter_notify (GtkWidget *widget,
1238 GdkEventCrossing *event,
1241 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1243 return completion->priv->ignore_enter;
1247 gtk_entry_completion_list_motion_notify (GtkWidget *widget,
1248 GdkEventMotion *event,
1251 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1253 completion->priv->ignore_enter = FALSE;
1259 /* some nasty size requisition */
1261 _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
1264 gint matches, items, height, x_border, y_border;
1267 GdkRectangle monitor;
1268 GtkRequisition popup_req;
1269 GtkRequisition entry_req;
1274 if (!completion->priv->entry->window)
1277 gdk_window_get_origin (completion->priv->entry->window, &x, &y);
1278 _gtk_entry_get_borders (GTK_ENTRY (completion->priv->entry), &x_border, &y_border);
1280 matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
1282 items = MIN (matches, 15);
1284 gtk_tree_view_column_cell_get_size (completion->priv->column, NULL,
1285 NULL, NULL, NULL, &height);
1288 gtk_widget_hide (completion->priv->scrolled_window);
1290 gtk_widget_show (completion->priv->scrolled_window);
1292 screen = gtk_widget_get_screen (GTK_WIDGET (completion->priv->entry));
1293 monitor_num = gdk_screen_get_monitor_at_window (screen,
1294 GTK_WIDGET (completion->priv->entry)->window);
1295 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1297 width = MIN (completion->priv->entry->allocation.width, monitor.width) - 2 * x_border;
1298 gtk_widget_set_size_request (completion->priv->tree_view, width, items * height);
1300 /* default on no match */
1301 completion->priv->current_selected = -1;
1303 items = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
1307 gtk_widget_show (completion->priv->action_view);
1308 gtk_widget_set_size_request (completion->priv->action_view, width, -1);
1311 gtk_widget_hide (completion->priv->action_view);
1313 gtk_widget_size_request (completion->priv->popup_window, &popup_req);
1314 gtk_widget_size_request (completion->priv->entry, &entry_req);
1318 else if (x + popup_req.width > monitor.x + monitor.width)
1319 x = monitor.x + monitor.width - popup_req.width;
1321 if (y + entry_req.height + popup_req.height <= monitor.y + monitor.height)
1323 y += entry_req.height;
1328 y -= popup_req.height;
1334 path = gtk_tree_path_new_from_indices (above ? matches - 1 : 0, -1);
1335 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (completion->priv->tree_view), path,
1336 NULL, FALSE, 0.0, 0.0);
1337 gtk_tree_path_free (path);
1340 gtk_window_move (GTK_WINDOW (completion->priv->popup_window), x, y);
1346 _gtk_entry_completion_popup (GtkEntryCompletion *completion)
1348 GtkTreeViewColumn *column;
1351 if (GTK_WIDGET_MAPPED (completion->priv->popup_window))
1354 if (!GTK_WIDGET_MAPPED (completion->priv->entry))
1357 completion->priv->ignore_enter = TRUE;
1359 column = gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0);
1360 renderers = gtk_tree_view_column_get_cell_renderers (column);
1361 gtk_widget_ensure_style (completion->priv->tree_view);
1362 g_object_set (GTK_CELL_RENDERER (renderers->data), "cell_background_gdk",
1363 &completion->priv->tree_view->style->bg[GTK_STATE_NORMAL],
1365 g_list_free (renderers);
1367 gtk_widget_show_all (completion->priv->vbox);
1369 _gtk_entry_completion_resize_popup (completion);
1371 gtk_widget_show (completion->priv->popup_window);
1373 gtk_grab_add (completion->priv->popup_window);
1374 gdk_pointer_grab (completion->priv->popup_window->window, TRUE,
1375 GDK_BUTTON_PRESS_MASK |
1376 GDK_BUTTON_RELEASE_MASK |
1377 GDK_POINTER_MOTION_MASK,
1378 NULL, NULL, GDK_CURRENT_TIME);
1382 _gtk_entry_completion_popdown (GtkEntryCompletion *completion)
1384 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
1387 completion->priv->ignore_enter = FALSE;
1389 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1390 gtk_grab_remove (completion->priv->popup_window);
1392 gtk_widget_hide (completion->priv->popup_window);
1396 gtk_entry_completion_match_selected (GtkEntryCompletion *completion,
1397 GtkTreeModel *model,
1402 gtk_tree_model_get (model, iter, completion->priv->text_column, &str, -1);
1403 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), str);
1405 /* move cursor to the end */
1406 gtk_editable_set_position (GTK_EDITABLE (completion->priv->entry), -1);
1415 gtk_entry_completion_compute_prefix (GtkEntryCompletion *completion)
1418 gchar *prefix = NULL;
1421 const gchar *key = gtk_entry_get_text (GTK_ENTRY (completion->priv->entry));
1423 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (completion->priv->filter_model),
1430 gtk_tree_model_get (GTK_TREE_MODEL (completion->priv->filter_model),
1431 &iter, completion->priv->text_column, &text,
1434 if (g_str_has_prefix (text, key))
1437 prefix = g_strdup (text);
1441 const gchar *q = text;
1443 while (*p && *p == *q)
1454 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (completion->priv->filter_model),
1463 gtk_entry_completion_real_insert_prefix (GtkEntryCompletion *completion,
1473 prefix_len = g_utf8_strlen (prefix, -1);
1475 key = gtk_entry_get_text (GTK_ENTRY (completion->priv->entry));
1476 key_len = g_utf8_strlen (key, -1);
1478 if (prefix_len > key_len)
1480 gtk_editable_insert_text (GTK_EDITABLE (completion->priv->entry),
1481 prefix + key_len, -1, &pos);
1482 gtk_editable_select_region (GTK_EDITABLE (completion->priv->entry),
1483 key_len, prefix_len);
1485 completion->priv->has_completion = TRUE;
1493 * gtk_entry_completion_insert_prefix:
1494 * @completion: a #GtkEntryCompletion
1496 * Requests a prefix insertion.
1501 gtk_entry_completion_insert_prefix (GtkEntryCompletion *completion)
1506 g_signal_handler_block (completion->priv->entry,
1507 completion->priv->insert_text_id);
1508 prefix = gtk_entry_completion_compute_prefix (completion);
1511 g_signal_emit (completion, entry_completion_signals[INSERT_PREFIX],
1515 g_signal_handler_unblock (completion->priv->entry,
1516 completion->priv->insert_text_id);
1520 * gtk_entry_completion_set_inline_completion:
1521 * @completion: a #GtkEntryCompletion
1522 * @inline_completion: %TRUE to do inline completion
1524 * Sets whether the common prefix of the possible completions should
1525 * be automatically inserted in the entry.
1530 gtk_entry_completion_set_inline_completion (GtkEntryCompletion *completion,
1531 gboolean inline_completion)
1533 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1535 inline_completion = inline_completion != FALSE;
1537 if (completion->priv->inline_completion != inline_completion)
1539 completion->priv->inline_completion = inline_completion;
1541 g_object_notify (G_OBJECT (completion), "inline_completion");
1547 * gtk_entry_completion_get_inline_completion:
1548 * @completion: a #GtkEntryCompletion
1550 * Returns whether the common prefix of the possible completions should
1551 * be automatically inserted in the entry.
1553 * Return value: %TRUE if inline completion is turned on
1558 gtk_entry_completion_get_inline_completion (GtkEntryCompletion *completion)
1560 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), FALSE);
1562 return completion->priv->inline_completion;
1566 * gtk_entry_completion_set_popup_completion:
1567 * @completion: a #GtkEntryCompletion
1568 * @popup_completion: %TRUE to do popup completion
1570 * Sets whether the completions should be presented in a popup window.
1575 gtk_entry_completion_set_popup_completion (GtkEntryCompletion *completion,
1576 gboolean popup_completion)
1578 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1580 popup_completion = popup_completion != FALSE;
1582 if (completion->priv->popup_completion != popup_completion)
1584 completion->priv->popup_completion = popup_completion;
1586 g_object_notify (G_OBJECT (completion), "popup_completion");
1592 * gtk_entry_completion_get_popup_completion:
1593 * @completion: a #GtkEntryCompletion
1595 * Returns whether the completions should be presented in a popup window.
1597 * Return value: %TRUE if popup completion is turned on
1602 gtk_entry_completion_get_popup_completion (GtkEntryCompletion *completion)
1604 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), TRUE);
1606 return completion->priv->popup_completion;