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 #include "gtkentrycompletion.h"
22 #include "gtkentryprivate.h"
23 #include "gtkcelllayout.h"
26 #include "gtkcellrenderertext.h"
28 #include "gtktreeselection.h"
29 #include "gtktreeview.h"
30 #include "gtkscrolledwindow.h"
32 #include "gtkwindow.h"
35 #include "gtksignal.h"
36 #include "gtkmarshalers.h"
54 PROP_MINIMUM_KEY_LENGTH,
58 #define GTK_ENTRY_COMPLETION_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ENTRY_COMPLETION, GtkEntryCompletionPrivate))
60 static void gtk_entry_completion_class_init (GtkEntryCompletionClass *klass);
61 static void gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface);
62 static void gtk_entry_completion_init (GtkEntryCompletion *completion);
63 static void gtk_entry_completion_set_property (GObject *object,
67 static void gtk_entry_completion_get_property (GObject *object,
71 static void gtk_entry_completion_finalize (GObject *object);
73 static void gtk_entry_completion_pack_start (GtkCellLayout *cell_layout,
74 GtkCellRenderer *cell,
76 static void gtk_entry_completion_pack_end (GtkCellLayout *cell_layout,
77 GtkCellRenderer *cell,
79 static void gtk_entry_completion_clear (GtkCellLayout *cell_layout);
80 static void gtk_entry_completion_add_attribute (GtkCellLayout *cell_layout,
81 GtkCellRenderer *cell,
82 const char *attribute,
84 static void gtk_entry_completion_set_cell_data_func (GtkCellLayout *cell_layout,
85 GtkCellRenderer *cell,
86 GtkCellLayoutDataFunc func,
88 GDestroyNotify destroy);
89 static void gtk_entry_completion_clear_attributes (GtkCellLayout *cell_layout,
90 GtkCellRenderer *cell);
91 static void gtk_entry_completion_reorder (GtkCellLayout *cell_layout,
92 GtkCellRenderer *cell,
95 static gboolean gtk_entry_completion_visible_func (GtkTreeModel *model,
98 static gboolean gtk_entry_completion_popup_key_press (GtkWidget *widget,
101 static gboolean gtk_entry_completion_popup_button_press (GtkWidget *widget,
102 GdkEventButton *event,
104 static gboolean gtk_entry_completion_list_button_press (GtkWidget *widget,
105 GdkEventButton *event,
107 static gboolean gtk_entry_completion_action_button_press (GtkWidget *widget,
108 GdkEventButton *event,
110 static void gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
113 static void gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
117 static void gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
118 GtkCellRenderer *cell,
123 static gboolean gtk_entry_completion_match_selected (GtkEntryCompletion *completion,
127 static GObjectClass *parent_class = NULL;
128 static guint entry_completion_signals[LAST_SIGNAL] = { 0 };
132 gtk_entry_completion_get_type (void)
134 static GType entry_completion_type = 0;
136 if (!entry_completion_type)
138 static const GTypeInfo entry_completion_info =
140 sizeof (GtkEntryCompletionClass),
143 (GClassInitFunc) gtk_entry_completion_class_init,
146 sizeof (GtkEntryCompletion),
148 (GInstanceInitFunc) gtk_entry_completion_init
151 static const GInterfaceInfo cell_layout_info =
153 (GInterfaceInitFunc) gtk_entry_completion_cell_layout_init,
158 entry_completion_type =
159 g_type_register_static (G_TYPE_OBJECT, "GtkEntryCompletion",
160 &entry_completion_info, 0);
162 g_type_add_interface_static (entry_completion_type,
163 GTK_TYPE_CELL_LAYOUT,
167 return entry_completion_type;
171 gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
173 GObjectClass *object_class;
175 parent_class = g_type_class_peek_parent (klass);
176 object_class = (GObjectClass *)klass;
178 object_class->set_property = gtk_entry_completion_set_property;
179 object_class->get_property = gtk_entry_completion_get_property;
180 object_class->finalize = gtk_entry_completion_finalize;
182 klass->match_selected = gtk_entry_completion_match_selected;
185 * GtkEntryCompletion::match-selected:
186 * @widget: the object which received the signal
187 * @model: the #GtkTreeModel containing the matches
188 * @iter: a #GtkTreeIter positioned at the selected match
190 * The ::match-selected signal is emitted when a match from the list
191 * is selected. The default behaviour is to replace the contents of the
192 * entry with the contents of the text column in the row pointed to by
195 * Return value: %TRUE if the signal has been handled
197 entry_completion_signals[MATCH_SELECTED] =
198 g_signal_new ("match_selected",
199 G_TYPE_FROM_CLASS (klass),
201 G_STRUCT_OFFSET (GtkEntryCompletionClass, match_selected),
202 _gtk_boolean_handled_accumulator, NULL,
203 _gtk_marshal_BOOLEAN__OBJECT_BOXED,
209 * GtkEntryCompletion::action-activated:
210 * @widget: the object which received the signal
211 * @index: the index of the activated action
213 * The ::action-activated signal is emitted when an action
216 entry_completion_signals[ACTION_ACTIVATED] =
217 g_signal_new ("action_activated",
218 G_TYPE_FROM_CLASS (klass),
220 G_STRUCT_OFFSET (GtkEntryCompletionClass, action_activated),
222 _gtk_marshal_NONE__INT,
226 g_object_class_install_property (object_class,
228 g_param_spec_object ("model",
229 P_("Completion Model"),
230 P_("The model to find matches in"),
233 g_object_class_install_property (object_class,
234 PROP_MINIMUM_KEY_LENGTH,
235 g_param_spec_int ("minimum_key_length",
236 P_("Minimum Key Length"),
237 P_("Minimum length of the search key in order to look up matches"),
243 * GtkEntryCompletion::text-column:
245 * The column of the model containing the strings.
249 g_object_class_install_property (object_class,
251 g_param_spec_int ("text_column",
253 P_("The column of the model containing the strings."),
259 g_type_class_add_private (object_class, sizeof (GtkEntryCompletionPrivate));
263 gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface)
265 iface->pack_start = gtk_entry_completion_pack_start;
266 iface->pack_end = gtk_entry_completion_pack_end;
267 iface->clear = gtk_entry_completion_clear;
268 iface->add_attribute = gtk_entry_completion_add_attribute;
269 iface->set_cell_data_func = gtk_entry_completion_set_cell_data_func;
270 iface->clear_attributes = gtk_entry_completion_clear_attributes;
271 iface->reorder = gtk_entry_completion_reorder;
275 gtk_entry_completion_init (GtkEntryCompletion *completion)
277 GtkCellRenderer *cell;
278 GtkTreeSelection *sel;
279 GtkEntryCompletionPrivate *priv;
280 GtkWidget *popup_frame;
282 /* yes, also priv, need to keep the code readable */
283 priv = completion->priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (completion);
285 priv->minimum_key_length = 1;
286 priv->text_column = -1;
289 priv->filter_model = NULL;
291 priv->tree_view = gtk_tree_view_new ();
292 g_signal_connect (priv->tree_view, "button_press_event",
293 G_CALLBACK (gtk_entry_completion_list_button_press),
295 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
296 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view), TRUE);
298 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
299 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
300 gtk_tree_selection_unselect_all (sel);
301 g_signal_connect (sel, "changed",
302 G_CALLBACK (gtk_entry_completion_selection_changed),
304 priv->first_sel_changed = TRUE;
306 priv->column = gtk_tree_view_column_new ();
307 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
309 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
310 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
312 GTK_POLICY_AUTOMATIC);
313 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
316 /* a nasty hack to get the completions treeview to size nicely */
317 gtk_widget_set_size_request (GTK_SCROLLED_WINDOW (priv->scrolled_window)->vscrollbar, -1, 0);
320 priv->actions = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_BOOLEAN);
323 gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->actions));
324 g_signal_connect (priv->action_view, "button_press_event",
325 G_CALLBACK (gtk_entry_completion_action_button_press),
327 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->action_view), FALSE);
328 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->action_view), TRUE);
330 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->action_view));
331 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
332 gtk_tree_selection_unselect_all (sel);
334 cell = gtk_cell_renderer_text_new ();
335 gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (priv->action_view),
338 gtk_entry_completion_action_data_func,
343 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
344 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
345 g_signal_connect (priv->popup_window, "key_press_event",
346 G_CALLBACK (gtk_entry_completion_popup_key_press),
348 g_signal_connect (priv->popup_window, "button_press_event",
349 G_CALLBACK (gtk_entry_completion_popup_button_press),
352 popup_frame = gtk_frame_new (NULL);
353 gtk_frame_set_shadow_type (GTK_FRAME (popup_frame),
354 GTK_SHADOW_ETCHED_IN);
355 gtk_widget_show (popup_frame);
356 gtk_container_add (GTK_CONTAINER (priv->popup_window), popup_frame);
358 priv->vbox = gtk_vbox_new (FALSE, 0);
359 gtk_container_add (GTK_CONTAINER (popup_frame), priv->vbox);
361 gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->tree_view);
362 gtk_box_pack_start (GTK_BOX (priv->vbox), priv->scrolled_window,
365 /* we don't want to see the action treeview when no actions have
366 * been inserted, so we pack the action treeview after the first
367 * action has been added
372 gtk_entry_completion_set_property (GObject *object,
377 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
378 GtkEntryCompletionPrivate *priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (completion);
383 gtk_entry_completion_set_model (completion,
384 g_value_get_object (value));
387 case PROP_MINIMUM_KEY_LENGTH:
388 gtk_entry_completion_set_minimum_key_length (completion,
389 g_value_get_int (value));
392 case PROP_TEXT_COLUMN:
393 priv->text_column = g_value_get_int (value);
397 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
403 gtk_entry_completion_get_property (GObject *object,
408 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
413 g_value_set_object (value,
414 gtk_entry_completion_get_model (completion));
417 case PROP_MINIMUM_KEY_LENGTH:
418 g_value_set_int (value, gtk_entry_completion_get_minimum_key_length (completion));
421 case PROP_TEXT_COLUMN:
422 g_value_set_int (value, gtk_entry_completion_get_text_column (completion));
426 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
432 gtk_entry_completion_finalize (GObject *object)
434 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
436 if (completion->priv->tree_view)
437 gtk_widget_destroy (completion->priv->tree_view);
439 if (completion->priv->entry)
440 gtk_entry_set_completion (GTK_ENTRY (completion->priv->entry), NULL);
442 if (completion->priv->actions)
443 g_object_unref (completion->priv->actions);
445 if (completion->priv->case_normalized_key)
446 g_free (completion->priv->case_normalized_key);
448 if (completion->priv->popup_window)
449 gtk_widget_destroy (completion->priv->popup_window);
451 G_OBJECT_CLASS (parent_class)->finalize (object);
454 /* implement cell layout interface */
456 gtk_entry_completion_pack_start (GtkCellLayout *cell_layout,
457 GtkCellRenderer *cell,
460 GtkEntryCompletionPrivate *priv;
462 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
464 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
466 gtk_tree_view_column_pack_start (priv->column, cell, expand);
470 gtk_entry_completion_pack_end (GtkCellLayout *cell_layout,
471 GtkCellRenderer *cell,
474 GtkEntryCompletionPrivate *priv;
476 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
478 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
480 gtk_tree_view_column_pack_end (priv->column, cell, expand);
484 gtk_entry_completion_clear (GtkCellLayout *cell_layout)
486 GtkEntryCompletionPrivate *priv;
488 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
490 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
492 gtk_tree_view_column_clear (priv->column);
496 gtk_entry_completion_add_attribute (GtkCellLayout *cell_layout,
497 GtkCellRenderer *cell,
498 const gchar *attribute,
501 GtkEntryCompletionPrivate *priv;
503 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
505 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
507 gtk_tree_view_column_add_attribute (priv->column, cell, attribute, column);
511 gtk_entry_completion_set_cell_data_func (GtkCellLayout *cell_layout,
512 GtkCellRenderer *cell,
513 GtkCellLayoutDataFunc func,
515 GDestroyNotify destroy)
517 GtkEntryCompletionPrivate *priv;
519 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
521 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
523 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column),
524 cell, func, func_data, destroy);
528 gtk_entry_completion_clear_attributes (GtkCellLayout *cell_layout,
529 GtkCellRenderer *cell)
531 GtkEntryCompletionPrivate *priv;
533 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
535 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
537 gtk_tree_view_column_clear_attributes (priv->column, cell);
541 gtk_entry_completion_reorder (GtkCellLayout *cell_layout,
542 GtkCellRenderer *cell,
545 GtkEntryCompletionPrivate *priv;
547 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
549 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
551 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column), cell, position);
554 /* all those callbacks */
556 gtk_entry_completion_default_completion_func (GtkEntryCompletion *completion,
562 gchar *normalized_string;
563 gchar *case_normalized_string;
565 gboolean ret = FALSE;
569 model = gtk_tree_model_filter_get_model (completion->priv->filter_model);
571 gtk_tree_model_get (model, iter,
572 completion->priv->text_column, &item,
577 normalized_string = g_utf8_normalize (item, -1, G_NORMALIZE_ALL);
578 case_normalized_string = g_utf8_casefold (normalized_string, -1);
580 if (!strncmp (key, case_normalized_string, strlen (key)))
584 g_free (normalized_string);
585 g_free (case_normalized_string);
592 gtk_entry_completion_visible_func (GtkTreeModel *model,
596 gboolean ret = FALSE;
598 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
600 if (!completion->priv->case_normalized_key)
603 if (completion->priv->match_func)
604 ret = (* completion->priv->match_func) (completion,
605 completion->priv->case_normalized_key,
607 completion->priv->match_data);
608 else if (completion->priv->text_column >= 0)
609 ret = gtk_entry_completion_default_completion_func (completion,
610 completion->priv->case_normalized_key,
618 gtk_entry_completion_popup_key_press (GtkWidget *widget,
622 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
624 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
627 /* propagate event to the entry */
628 gtk_widget_event (completion->priv->entry, (GdkEvent *)event);
634 gtk_entry_completion_popup_button_press (GtkWidget *widget,
635 GdkEventButton *event,
638 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
640 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
643 /* if we come here, it's usually time to popdown */
644 _gtk_entry_completion_popdown (completion);
650 gtk_entry_completion_list_button_press (GtkWidget *widget,
651 GdkEventButton *event,
654 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
655 GtkTreePath *path = NULL;
657 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
660 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
662 &path, NULL, NULL, NULL))
667 gtk_tree_model_get_iter (GTK_TREE_MODEL (completion->priv->filter_model),
669 gtk_tree_path_free (path);
671 g_signal_handler_block (completion->priv->entry,
672 completion->priv->changed_id);
673 g_signal_emit (completion, entry_completion_signals[MATCH_SELECTED],
674 0, GTK_TREE_MODEL (completion->priv->filter_model),
676 g_signal_handler_unblock (completion->priv->entry,
677 completion->priv->changed_id);
679 _gtk_entry_completion_popdown (completion);
688 gtk_entry_completion_action_button_press (GtkWidget *widget,
689 GdkEventButton *event,
692 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
693 GtkTreePath *path = NULL;
695 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
698 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
700 &path, NULL, NULL, NULL))
702 g_signal_emit (completion, entry_completion_signals[ACTION_ACTIVATED],
703 0, gtk_tree_path_get_indices (path)[0]);
704 gtk_tree_path_free (path);
706 _gtk_entry_completion_popdown (completion);
714 gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
715 GtkCellRenderer *cell,
720 gchar *string = NULL;
723 gtk_tree_model_get (model, iter,
732 g_object_set (G_OBJECT (cell),
737 g_object_set (G_OBJECT (cell),
746 gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
749 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
751 if (completion->priv->first_sel_changed)
753 completion->priv->first_sel_changed = FALSE;
754 if (gtk_widget_is_focus (completion->priv->tree_view))
755 gtk_tree_selection_unselect_all (selection);
762 * gtk_entry_completion_new:
764 * Creates a new #GtkEntryCompletion object.
766 * Return value: A newly created #GtkEntryCompletion object.
771 gtk_entry_completion_new (void)
773 GtkEntryCompletion *completion;
775 completion = g_object_new (GTK_TYPE_ENTRY_COMPLETION, NULL);
781 * gtk_entry_completion_get_entry:
782 * @completion: A #GtkEntryCompletion.
784 * Gets the entry @completion has been attached to.
786 * Return value: The entry @completion has been attached to.
791 gtk_entry_completion_get_entry (GtkEntryCompletion *completion)
793 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
795 return completion->priv->entry;
799 * gtk_entry_completion_set_model:
800 * @completion: A #GtkEntryCompletion.
801 * @model: The #GtkTreeModel.
803 * Sets the model for a #GtkEntryCompletion. If @completion already has
804 * a model set, it will remove it before setting the new model.
809 gtk_entry_completion_set_model (GtkEntryCompletion *completion,
812 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
813 g_return_if_fail (GTK_IS_TREE_MODEL (model));
815 /* code will unref the old filter model (if any) */
816 completion->priv->filter_model =
817 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
818 gtk_tree_model_filter_set_visible_func (completion->priv->filter_model,
819 gtk_entry_completion_visible_func,
822 gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
823 GTK_TREE_MODEL (completion->priv->filter_model));
824 g_object_unref (G_OBJECT (completion->priv->filter_model));
828 * gtk_entry_completion_get_model:
829 * @completion: A #GtkEntryCompletion.
831 * Returns the model the #GtkEntryCompletion is using as data source.
832 * Returns %NULL if the model is unset.
834 * Return value: A #GtkTreeModel, or %NULL if none is currently being used.
839 gtk_entry_completion_get_model (GtkEntryCompletion *completion)
841 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
843 return gtk_tree_model_filter_get_model (completion->priv->filter_model);
847 * gtk_entry_completion_set_match_func:
848 * @completion: A #GtkEntryCompletion.
849 * @func: The #GtkEntryCompletionMatchFunc to use.
850 * @func_data: The user data for @func.
851 * @func_notify: Destroy notifier for @func_data.
853 * Sets the match function for @completion to be @func. The match function
854 * is used to determine if a row should or should not be in the completion
860 gtk_entry_completion_set_match_func (GtkEntryCompletion *completion,
861 GtkEntryCompletionMatchFunc func,
863 GDestroyNotify func_notify)
865 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
867 if (completion->priv->match_notify)
868 (* completion->priv->match_notify) (completion->priv->match_data);
870 completion->priv->match_func = func;
871 completion->priv->match_data = func_data;
872 completion->priv->match_notify = func_notify;
876 * gtk_entry_completion_set_minimum_key_length:
877 * @completion: A #GtkEntryCompletion.
878 * @length: The minimum length of the key in order to start completing.
880 * Requires the length of the search key for @completion to be at least
881 * @length. This is useful for long lists, where completing using a small
882 * key takes a lot of time and will come up with meaningless results anyway
883 * (ie, a too large dataset).
888 gtk_entry_completion_set_minimum_key_length (GtkEntryCompletion *completion,
891 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
892 g_return_if_fail (length >= 1);
894 completion->priv->minimum_key_length = length;
898 * gtk_entry_completion_get_minimum_key_length:
899 * @completion: A #GtkEntryCompletion.
901 * Returns the minimum key length as set for @completion.
903 * Return value: The currently used minimum key length.
908 gtk_entry_completion_get_minimum_key_length (GtkEntryCompletion *completion)
910 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), 0);
912 return completion->priv->minimum_key_length;
916 * gtk_entry_completion_complete:
917 * @completion: A #GtkEntryCompletion.
919 * Requests a completion operation, or in other words a refiltering of the
920 * current list with completions, using the current key. The completion list
921 * view will be updated accordingly.
926 gtk_entry_completion_complete (GtkEntryCompletion *completion)
930 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
931 g_return_if_fail (completion->priv->filter_model != NULL);
933 if (completion->priv->case_normalized_key)
934 g_free (completion->priv->case_normalized_key);
936 tmp = g_utf8_normalize (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)),
937 -1, G_NORMALIZE_ALL);
938 completion->priv->case_normalized_key = g_utf8_casefold (tmp, -1);
941 gtk_tree_model_filter_refilter (completion->priv->filter_model);
945 gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
952 gtk_list_store_insert (completion->priv->actions, &iter, index);
953 gtk_list_store_set (completion->priv->actions, &iter,
958 if (!completion->priv->action_view->parent)
960 GtkTreePath *path = gtk_tree_path_new_from_indices (0, -1);
962 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
964 gtk_tree_path_free (path);
966 gtk_box_pack_start (GTK_BOX (completion->priv->vbox),
967 completion->priv->action_view, FALSE, FALSE, 0);
968 gtk_widget_show (completion->priv->action_view);
973 * gtk_entry_completion_insert_action_text:
974 * @completion: A #GtkEntryCompletion.
975 * @index: The index of the item to insert.
976 * @text: Text of the item to insert.
978 * Inserts an action in @completion's action item list at position @index
979 * with text @text. If you want the action item to have markup, use
980 * gtk_entry_completion_insert_action_markup().
985 gtk_entry_completion_insert_action_text (GtkEntryCompletion *completion,
989 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
990 g_return_if_fail (text != NULL);
992 gtk_entry_completion_insert_action (completion, index, text, FALSE);
996 * gtk_entry_completion_insert_action_markup:
997 * @completion: A #GtkEntryCompletion.
998 * @index: The index of the item to insert.
999 * @markup: Markup of the item to insert.
1001 * Inserts an action in @completion's action item list at position @index
1002 * with markup @markup.
1007 gtk_entry_completion_insert_action_markup (GtkEntryCompletion *completion,
1009 const gchar *markup)
1011 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1012 g_return_if_fail (markup != NULL);
1014 gtk_entry_completion_insert_action (completion, index, markup, TRUE);
1018 * gtk_entry_completion_delete_action:
1019 * @completion: A #GtkEntryCompletion.
1020 * @index: The index of the item to Delete.
1022 * Deletes the action at @index from @completion's action list.
1027 gtk_entry_completion_delete_action (GtkEntryCompletion *completion,
1032 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1033 g_return_if_fail (index >= 0);
1035 gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (completion->priv->actions),
1036 &iter, NULL, index);
1037 gtk_list_store_remove (completion->priv->actions, &iter);
1041 * gtk_entry_completion_set_text_column:
1042 * @completion: A #GtkEntryCompletion.
1043 * @column: The column in the model of @completion to get strings from.
1045 * Convenience function for setting up the most used case of this code: a
1046 * completion list with just strings. This function will set up @completion
1047 * to have a list displaying all (and just) strings in the completion list,
1048 * and to get those strings from @column in the model of @completion.
1050 * This functions creates and adds a #GtkCellRendererText for the selected
1056 gtk_entry_completion_set_text_column (GtkEntryCompletion *completion,
1059 GtkCellRenderer *cell;
1061 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1062 g_return_if_fail (column >= 0);
1064 completion->priv->text_column = column;
1066 cell = gtk_cell_renderer_text_new ();
1067 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion),
1069 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (completion),
1073 g_object_notify (completion, "text_column");
1077 * gtk_entry_completion_get_text_column:
1078 * @completion: a #GtkEntryCompletion
1080 * Returns the column in the model of @completion to get strings from.
1082 * Return value: the column containing the strings
1087 gtk_entry_completion_get_text_column (GtkEntryCompletion *completion)
1089 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1091 return completion->priv->text_column;
1096 /* lame copy from gtkentry.c */
1098 get_borders (GtkEntry *entry,
1102 GtkWidget *widget = GTK_WIDGET (entry);
1104 gboolean interior_focus;
1106 gtk_widget_style_get (widget,
1107 "interior-focus", &interior_focus,
1108 "focus-line-width", &focus_width,
1111 if (entry->has_frame)
1113 *xborder = widget->style->xthickness;
1114 *yborder = widget->style->ythickness;
1122 if (!interior_focus)
1124 *xborder += focus_width;
1125 *yborder += focus_width;
1129 /* some nasty size requisition */
1131 _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
1134 gint matches, items, height, x_border, y_border;
1137 GdkRectangle monitor;
1138 GtkRequisition popup_req;
1143 gdk_window_get_origin (completion->priv->entry->window, &x, &y);
1144 get_borders (GTK_ENTRY (completion->priv->entry), &x_border, &y_border);
1149 matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
1151 items = MIN (matches, 15);
1153 gtk_tree_view_column_cell_get_size (completion->priv->column, NULL,
1154 NULL, NULL, NULL, &height);
1157 gtk_widget_hide (completion->priv->scrolled_window);
1159 gtk_widget_show (completion->priv->scrolled_window);
1161 screen = gtk_widget_get_screen (GTK_WIDGET (completion->priv->entry));
1162 monitor_num = gdk_screen_get_monitor_at_window (screen,
1163 GTK_WIDGET (completion->priv->entry)->window);
1164 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1166 width = MIN (completion->priv->entry->allocation.width, monitor.width);
1167 gtk_widget_set_size_request (completion->priv->tree_view,
1168 width - 2 * x_border, items * height);
1170 /* default on no match */
1171 completion->priv->current_selected = -1;
1173 items = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
1177 gtk_widget_show (completion->priv->action_view);
1179 gtk_tree_view_column_cell_get_size (gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0),
1180 NULL, NULL, NULL, NULL,
1183 gtk_widget_set_size_request (completion->priv->action_view,
1184 width - 2 * x_border, items * height);
1187 gtk_widget_hide (completion->priv->action_view);
1189 gtk_widget_size_request (completion->priv->popup_window, &popup_req);
1193 else if (x + popup_req.width > monitor.x + monitor.width)
1194 x = monitor.x + monitor.width - popup_req.width;
1196 if (y + height + popup_req.height <= monitor.y + monitor.height)
1203 y -= popup_req.height;
1209 path = gtk_tree_path_new_from_indices (above ? matches - 1 : 0, -1);
1210 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (completion->priv->tree_view), path,
1211 NULL, FALSE, 0.0, 0.0);
1212 gtk_tree_path_free (path);
1215 gtk_window_move (GTK_WINDOW (completion->priv->popup_window), x, y);
1221 _gtk_entry_completion_popup (GtkEntryCompletion *completion)
1223 GtkTreeViewColumn *column;
1226 if (GTK_WIDGET_MAPPED (completion->priv->popup_window))
1229 completion->priv->may_wrap = TRUE;
1231 column = gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0);
1232 renderers = gtk_tree_view_column_get_cell_renderers (column);
1233 gtk_widget_ensure_style (completion->priv->tree_view);
1234 g_object_set (GTK_CELL_RENDERER (renderers->data), "cell_background_gdk",
1235 &completion->priv->tree_view->style->bg[GTK_STATE_NORMAL],
1237 g_list_free (renderers);
1239 gtk_widget_show_all (completion->priv->vbox);
1241 _gtk_entry_completion_resize_popup (completion);
1243 gtk_widget_show (completion->priv->popup_window);
1245 gtk_grab_add (completion->priv->popup_window);
1246 gdk_pointer_grab (completion->priv->popup_window->window, TRUE,
1247 GDK_BUTTON_PRESS_MASK |
1248 GDK_BUTTON_RELEASE_MASK |
1249 GDK_POINTER_MOTION_MASK,
1250 NULL, NULL, GDK_CURRENT_TIME);
1254 _gtk_entry_completion_popdown (GtkEntryCompletion *completion)
1256 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1257 gtk_grab_remove (completion->priv->popup_window);
1259 gtk_widget_hide (completion->priv->popup_window);
1263 gtk_entry_completion_match_selected (GtkEntryCompletion *completion,
1264 GtkTreeModel *model,
1269 gtk_tree_model_get (model, iter, completion->priv->text_column, &str, -1);
1270 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), str);
1272 /* move cursor to the end */
1273 gtk_editable_set_position (GTK_EDITABLE (completion->priv->entry), -1);