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
57 #define GTK_ENTRY_COMPLETION_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ENTRY_COMPLETION, GtkEntryCompletionPrivate))
59 static void gtk_entry_completion_class_init (GtkEntryCompletionClass *klass);
60 static void gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface);
61 static void gtk_entry_completion_init (GtkEntryCompletion *completion);
62 static void gtk_entry_completion_set_property (GObject *object,
66 static void gtk_entry_completion_get_property (GObject *object,
70 static void gtk_entry_completion_finalize (GObject *object);
72 static void gtk_entry_completion_pack_start (GtkCellLayout *cell_layout,
73 GtkCellRenderer *cell,
75 static void gtk_entry_completion_pack_end (GtkCellLayout *cell_layout,
76 GtkCellRenderer *cell,
78 static void gtk_entry_completion_clear (GtkCellLayout *cell_layout);
79 static void gtk_entry_completion_add_attribute (GtkCellLayout *cell_layout,
80 GtkCellRenderer *cell,
81 const char *attribute,
83 static void gtk_entry_completion_set_cell_data_func (GtkCellLayout *cell_layout,
84 GtkCellRenderer *cell,
85 GtkCellLayoutDataFunc func,
87 GDestroyNotify destroy);
88 static void gtk_entry_completion_clear_attributes (GtkCellLayout *cell_layout,
89 GtkCellRenderer *cell);
90 static void gtk_entry_completion_reorder (GtkCellLayout *cell_layout,
91 GtkCellRenderer *cell,
94 static gboolean gtk_entry_completion_visible_func (GtkTreeModel *model,
97 static gboolean gtk_entry_completion_popup_key_press (GtkWidget *widget,
100 static gboolean gtk_entry_completion_popup_button_press (GtkWidget *widget,
101 GdkEventButton *event,
103 static gboolean gtk_entry_completion_list_button_press (GtkWidget *widget,
104 GdkEventButton *event,
106 static gboolean gtk_entry_completion_action_button_press (GtkWidget *widget,
107 GdkEventButton *event,
109 static void gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
112 static void gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
116 static void gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
117 GtkCellRenderer *cell,
123 static GObjectClass *parent_class = NULL;
124 static guint entry_completion_signals[LAST_SIGNAL] = { 0 };
128 gtk_entry_completion_get_type (void)
130 static GType entry_completion_type = 0;
132 if (!entry_completion_type)
134 static const GTypeInfo entry_completion_info =
136 sizeof (GtkEntryCompletionClass),
139 (GClassInitFunc) gtk_entry_completion_class_init,
142 sizeof (GtkEntryCompletion),
144 (GInstanceInitFunc) gtk_entry_completion_init
147 static const GInterfaceInfo cell_layout_info =
149 (GInterfaceInitFunc) gtk_entry_completion_cell_layout_init,
154 entry_completion_type =
155 g_type_register_static (G_TYPE_OBJECT, "GtkEntryCompletion",
156 &entry_completion_info, 0);
158 g_type_add_interface_static (entry_completion_type,
159 GTK_TYPE_CELL_LAYOUT,
163 return entry_completion_type;
167 gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
169 GObjectClass *object_class;
171 parent_class = g_type_class_peek_parent (klass);
172 object_class = (GObjectClass *)klass;
174 object_class->set_property = gtk_entry_completion_set_property;
175 object_class->get_property = gtk_entry_completion_get_property;
176 object_class->finalize = gtk_entry_completion_finalize;
179 * GtkEntryCompletion::match-selected:
180 * @widget: the object which received the signal
181 * @model: the #GtkTreeModel containing the matches
182 * @iter: a #GtkTreeIter positioned at the selected match
184 * The ::match-selected signal is emitted when a match from the list
185 * is selected. The default behaviour is to replace the contents of the
186 * entry with the contents of the text column in the row pointed to by
189 * Return value: %TRUE if the signal has been handled
191 entry_completion_signals[MATCH_SELECTED] =
192 g_signal_new ("match_selected",
193 G_TYPE_FROM_CLASS (klass),
195 G_STRUCT_OFFSET (GtkEntryCompletionClass, match_selected),
196 _gtk_boolean_handled_accumulator, NULL,
197 _gtk_marshal_BOOLEAN__OBJECT_BOXED,
203 * GtkEntryCompletion::action-activated:
204 * @widget: the object which received the signal
205 * @index: the index of the activated action
207 * The ::action-activated signal is emitted when an action
210 entry_completion_signals[ACTION_ACTIVATED] =
211 g_signal_new ("action_activated",
212 G_TYPE_FROM_CLASS (klass),
214 G_STRUCT_OFFSET (GtkEntryCompletionClass, action_activated),
216 _gtk_marshal_NONE__INT,
220 g_object_class_install_property (object_class,
222 g_param_spec_object ("model",
223 P_("Completion Model"),
224 P_("The model to find matches in"),
227 g_object_class_install_property (object_class,
228 PROP_MINIMUM_KEY_LENGTH,
229 g_param_spec_int ("minimum_key_length",
230 P_("Minimum Key Length"),
231 P_("Minimum length of the search key in order to look up matches"),
237 g_type_class_add_private (object_class, sizeof (GtkEntryCompletionPrivate));
241 gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface)
243 iface->pack_start = gtk_entry_completion_pack_start;
244 iface->pack_end = gtk_entry_completion_pack_end;
245 iface->clear = gtk_entry_completion_clear;
246 iface->add_attribute = gtk_entry_completion_add_attribute;
247 iface->set_cell_data_func = gtk_entry_completion_set_cell_data_func;
248 iface->clear_attributes = gtk_entry_completion_clear_attributes;
249 iface->reorder = gtk_entry_completion_reorder;
253 gtk_entry_completion_init (GtkEntryCompletion *completion)
255 GtkCellRenderer *cell;
256 GtkTreeSelection *sel;
257 GtkEntryCompletionPrivate *priv;
258 GtkWidget *popup_frame;
260 /* yes, also priv, need to keep the code readable */
261 priv = completion->priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (completion);
263 priv->minimum_key_length = 1;
264 priv->text_column = -1;
267 priv->filter_model = NULL;
269 priv->tree_view = gtk_tree_view_new ();
270 g_signal_connect (priv->tree_view, "button_press_event",
271 G_CALLBACK (gtk_entry_completion_list_button_press),
273 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
275 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
276 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
277 gtk_tree_selection_unselect_all (sel);
278 g_signal_connect (sel, "changed",
279 G_CALLBACK (gtk_entry_completion_selection_changed),
281 priv->first_sel_changed = TRUE;
283 priv->column = gtk_tree_view_column_new ();
284 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
286 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
287 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
289 GTK_POLICY_AUTOMATIC);
290 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
293 /* a nasty hack to get the completions treeview to size nicely */
294 gtk_widget_set_size_request (GTK_SCROLLED_WINDOW (priv->scrolled_window)->vscrollbar, -1, 0);
297 priv->actions = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_BOOLEAN);
300 gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->actions));
301 g_signal_connect (priv->action_view, "button_press_event",
302 G_CALLBACK (gtk_entry_completion_action_button_press),
304 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->action_view), FALSE);
306 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->action_view));
307 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
308 gtk_tree_selection_unselect_all (sel);
310 cell = gtk_cell_renderer_text_new ();
311 gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (priv->action_view),
314 gtk_entry_completion_action_data_func,
319 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
320 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
321 g_signal_connect (priv->popup_window, "key_press_event",
322 G_CALLBACK (gtk_entry_completion_popup_key_press),
324 g_signal_connect (priv->popup_window, "button_press_event",
325 G_CALLBACK (gtk_entry_completion_popup_button_press),
328 popup_frame = gtk_frame_new (NULL);
329 gtk_frame_set_shadow_type (GTK_FRAME (popup_frame),
330 GTK_SHADOW_ETCHED_IN);
331 gtk_widget_show (popup_frame);
332 gtk_container_add (GTK_CONTAINER (priv->popup_window), popup_frame);
334 priv->vbox = gtk_vbox_new (FALSE, 0);
335 gtk_container_add (GTK_CONTAINER (popup_frame), priv->vbox);
337 gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->tree_view);
338 gtk_box_pack_start (GTK_BOX (priv->vbox), priv->scrolled_window,
341 /* we don't want to see the action treeview when no actions have
342 * been inserted, so we pack the action treeview after the first
343 * action has been added
348 gtk_entry_completion_set_property (GObject *object,
353 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
358 gtk_entry_completion_set_model (completion,
359 g_value_get_object (value));
362 case PROP_MINIMUM_KEY_LENGTH:
363 gtk_entry_completion_set_minimum_key_length (completion,
364 g_value_get_int (value));
368 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
374 gtk_entry_completion_get_property (GObject *object,
379 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
384 g_value_set_object (value,
385 gtk_entry_completion_get_model (completion));
388 case PROP_MINIMUM_KEY_LENGTH:
389 g_value_set_int (value, gtk_entry_completion_get_minimum_key_length (completion));
393 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
399 gtk_entry_completion_finalize (GObject *object)
401 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
403 if (completion->priv->tree_view)
404 gtk_widget_destroy (completion->priv->tree_view);
406 if (completion->priv->entry)
407 gtk_entry_set_completion (GTK_ENTRY (completion->priv->entry), NULL);
409 if (completion->priv->actions)
410 g_object_unref (completion->priv->actions);
412 if (completion->priv->case_normalized_key)
413 g_free (completion->priv->case_normalized_key);
415 if (completion->priv->popup_window)
416 gtk_widget_destroy (completion->priv->popup_window);
418 G_OBJECT_CLASS (parent_class)->finalize (object);
421 /* implement cell layout interface */
423 gtk_entry_completion_pack_start (GtkCellLayout *cell_layout,
424 GtkCellRenderer *cell,
427 GtkEntryCompletionPrivate *priv;
429 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
431 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
433 gtk_tree_view_column_pack_start (priv->column, cell, expand);
437 gtk_entry_completion_pack_end (GtkCellLayout *cell_layout,
438 GtkCellRenderer *cell,
441 GtkEntryCompletionPrivate *priv;
443 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
445 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
447 gtk_tree_view_column_pack_end (priv->column, cell, expand);
451 gtk_entry_completion_clear (GtkCellLayout *cell_layout)
453 GtkEntryCompletionPrivate *priv;
455 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
457 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
459 gtk_tree_view_column_clear (priv->column);
463 gtk_entry_completion_add_attribute (GtkCellLayout *cell_layout,
464 GtkCellRenderer *cell,
465 const gchar *attribute,
468 GtkEntryCompletionPrivate *priv;
470 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
472 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
474 gtk_tree_view_column_add_attribute (priv->column, cell, attribute, column);
478 gtk_entry_completion_set_cell_data_func (GtkCellLayout *cell_layout,
479 GtkCellRenderer *cell,
480 GtkCellLayoutDataFunc func,
482 GDestroyNotify destroy)
484 GtkEntryCompletionPrivate *priv;
486 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
488 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
490 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column),
491 cell, func, func_data, destroy);
495 gtk_entry_completion_clear_attributes (GtkCellLayout *cell_layout,
496 GtkCellRenderer *cell)
498 GtkEntryCompletionPrivate *priv;
500 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
502 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
504 gtk_tree_view_column_clear_attributes (priv->column, cell);
508 gtk_entry_completion_reorder (GtkCellLayout *cell_layout,
509 GtkCellRenderer *cell,
512 GtkEntryCompletionPrivate *priv;
514 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
516 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
518 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column), cell, position);
521 /* all those callbacks */
523 gtk_entry_completion_default_completion_func (GtkEntryCompletion *completion,
529 gchar *normalized_string;
530 gchar *case_normalized_string;
532 gboolean ret = FALSE;
536 model = gtk_tree_model_filter_get_model (completion->priv->filter_model);
538 gtk_tree_model_get (model, iter,
539 completion->priv->text_column, &item,
544 normalized_string = g_utf8_normalize (item, -1, G_NORMALIZE_ALL);
545 case_normalized_string = g_utf8_casefold (normalized_string, -1);
547 if (!strncmp (key, case_normalized_string, strlen (key)))
551 g_free (normalized_string);
552 g_free (case_normalized_string);
559 gtk_entry_completion_visible_func (GtkTreeModel *model,
563 gboolean ret = FALSE;
565 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
567 if (!completion->priv->case_normalized_key)
570 if (completion->priv->match_func)
571 ret = (* completion->priv->match_func) (completion,
572 completion->priv->case_normalized_key,
574 completion->priv->match_data);
575 else if (completion->priv->text_column >= 0)
576 ret = gtk_entry_completion_default_completion_func (completion,
577 completion->priv->case_normalized_key,
585 gtk_entry_completion_popup_key_press (GtkWidget *widget,
589 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
591 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
594 /* propagate event to the entry */
595 gtk_widget_event (completion->priv->entry, (GdkEvent *)event);
601 gtk_entry_completion_popup_button_press (GtkWidget *widget,
602 GdkEventButton *event,
605 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
607 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
610 /* if we come here, it's usually time to popdown */
611 _gtk_entry_completion_popdown (completion);
617 gtk_entry_completion_list_button_press (GtkWidget *widget,
618 GdkEventButton *event,
621 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
622 GtkTreePath *path = NULL;
624 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
627 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
629 &path, NULL, NULL, NULL))
634 gtk_tree_model_get_iter (GTK_TREE_MODEL (completion->priv->filter_model),
636 gtk_tree_path_free (path);
638 g_signal_handler_block (completion->priv->entry,
639 completion->priv->changed_id);
640 g_signal_emit (completion, entry_completion_signals[MATCH_SELECTED],
641 0, GTK_TREE_MODEL (completion->priv->filter_model),
643 g_signal_handler_unblock (completion->priv->entry,
644 completion->priv->changed_id);
650 gtk_tree_model_get (GTK_TREE_MODEL (completion->priv->filter_model),
652 completion->priv->text_column, &str,
655 g_signal_handler_block (completion->priv->entry,
656 completion->priv->changed_id);
657 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), str);
658 g_signal_handler_unblock (completion->priv->entry,
659 completion->priv->changed_id);
661 /* move cursor to the end */
662 gtk_editable_set_position (GTK_EDITABLE (completion->priv->entry),
668 _gtk_entry_completion_popdown (completion);
676 gtk_entry_completion_action_button_press (GtkWidget *widget,
677 GdkEventButton *event,
680 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
681 GtkTreePath *path = NULL;
683 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
686 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
688 &path, NULL, NULL, NULL))
690 g_signal_emit (completion, entry_completion_signals[ACTION_ACTIVATED],
691 0, gtk_tree_path_get_indices (path)[0]);
692 gtk_tree_path_free (path);
694 _gtk_entry_completion_popdown (completion);
702 gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
703 GtkCellRenderer *cell,
708 gchar *string = NULL;
711 gtk_tree_model_get (model, iter,
720 g_object_set (G_OBJECT (cell),
725 g_object_set (G_OBJECT (cell),
734 gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
737 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
739 if (completion->priv->first_sel_changed)
741 completion->priv->first_sel_changed = FALSE;
742 if (gtk_widget_is_focus (completion->priv->tree_view))
743 gtk_tree_selection_unselect_all (selection);
750 * gtk_entry_completion_new:
752 * Creates a new #GtkEntryCompletion object.
754 * Return value: A newly created #GtkEntryCompletion object.
759 gtk_entry_completion_new (void)
761 GtkEntryCompletion *completion;
763 completion = g_object_new (GTK_TYPE_ENTRY_COMPLETION, NULL);
769 * gtk_entry_completion_get_entry:
770 * @completion: A #GtkEntryCompletion.
772 * Gets the entry @completion has been attached to.
774 * Return value: The entry @completion has been attached to.
779 gtk_entry_completion_get_entry (GtkEntryCompletion *completion)
781 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
783 return completion->priv->entry;
787 * gtk_entry_completion_set_model:
788 * @completion: A #GtkEntryCompletion.
789 * @model: The #GtkTreeModel.
791 * Sets the model for a #GtkEntryCompletion. If @completion already has
792 * a model set, it will remove it before setting the new model.
797 gtk_entry_completion_set_model (GtkEntryCompletion *completion,
800 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
801 g_return_if_fail (GTK_IS_TREE_MODEL (model));
803 /* code will unref the old filter model (if any) */
804 completion->priv->filter_model =
805 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
806 gtk_tree_model_filter_set_visible_func (completion->priv->filter_model,
807 gtk_entry_completion_visible_func,
810 gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
811 GTK_TREE_MODEL (completion->priv->filter_model));
812 g_object_unref (G_OBJECT (completion->priv->filter_model));
816 * gtk_entry_completion_get_model:
817 * @completion: A #GtkEntryCompletion.
819 * Returns the model the #GtkEntryCompletion is using as data source.
820 * Returns %NULL if the model is unset.
822 * Return value: A #GtkTreeModel, or %NULL if none is currently being used.
827 gtk_entry_completion_get_model (GtkEntryCompletion *completion)
829 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
831 return gtk_tree_model_filter_get_model (completion->priv->filter_model);
835 * gtk_entry_completion_set_match_func:
836 * @completion: A #GtkEntryCompletion.
837 * @func: The #GtkEntryCompletionMatchFunc to use.
838 * @func_data: The user data for @func.
839 * @func_notify: Destroy notifier for @func_data.
841 * Sets the match function for @completion to be @func. The match function
842 * is used to determine if a row should or should not be in the completion
848 gtk_entry_completion_set_match_func (GtkEntryCompletion *completion,
849 GtkEntryCompletionMatchFunc func,
851 GDestroyNotify func_notify)
853 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
855 if (completion->priv->match_notify)
856 (* completion->priv->match_notify) (completion->priv->match_data);
858 completion->priv->match_func = func;
859 completion->priv->match_data = func_data;
860 completion->priv->match_notify = func_notify;
864 * gtk_entry_completion_set_minimum_key_length:
865 * @completion: A #GtkEntryCompletion.
866 * @length: The minimum length of the key in order to start completing.
868 * Requires the length of the search key for @completion to be at least
869 * @length. This is useful for long lists, where completing using a small
870 * key takes a lot of time and will come up with meaningless results anyway
871 * (ie, a too large dataset).
876 gtk_entry_completion_set_minimum_key_length (GtkEntryCompletion *completion,
879 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
880 g_return_if_fail (length >= 1);
882 completion->priv->minimum_key_length = length;
886 * gtk_entry_completion_get_minimum_key_length:
887 * @completion: A #GtkEntryCompletion.
889 * Returns the minimum key length as set for @completion.
891 * Return value: The currently used minimum key length.
896 gtk_entry_completion_get_minimum_key_length (GtkEntryCompletion *completion)
898 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), 0);
900 return completion->priv->minimum_key_length;
904 * gtk_entry_completion_complete:
905 * @completion: A #GtkEntryCompletion.
907 * Requests a completion operation, or in other words a refiltering of the
908 * current list with completions, using the current key. The completion list
909 * view will be updated accordingly.
914 gtk_entry_completion_complete (GtkEntryCompletion *completion)
918 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
919 g_return_if_fail (completion->priv->filter_model != NULL);
921 if (completion->priv->case_normalized_key)
922 g_free (completion->priv->case_normalized_key);
924 tmp = g_utf8_normalize (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)),
925 -1, G_NORMALIZE_ALL);
926 completion->priv->case_normalized_key = g_utf8_casefold (tmp, -1);
929 gtk_tree_model_filter_refilter (completion->priv->filter_model);
933 gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
940 gtk_list_store_insert (completion->priv->actions, &iter, index);
941 gtk_list_store_set (completion->priv->actions, &iter,
946 if (!completion->priv->action_view->parent)
948 GtkTreePath *path = gtk_tree_path_new_from_indices (0, -1);
950 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
952 gtk_tree_path_free (path);
954 gtk_box_pack_start (GTK_BOX (completion->priv->vbox),
955 completion->priv->action_view, FALSE, FALSE, 0);
956 gtk_widget_show (completion->priv->action_view);
961 * gtk_entry_completion_insert_action_text:
962 * @completion: A #GtkEntryCompletion.
963 * @index: The index of the item to insert.
964 * @text: Text of the item to insert.
966 * Inserts an action in @completion's action item list at position @index
967 * with text @text. If you want the action item to have markup, use
968 * gtk_entry_completion_insert_action_markup().
973 gtk_entry_completion_insert_action_text (GtkEntryCompletion *completion,
977 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
978 g_return_if_fail (text != NULL);
980 gtk_entry_completion_insert_action (completion, index, text, FALSE);
984 * gtk_entry_completion_insert_action_markup:
985 * @completion: A #GtkEntryCompletion.
986 * @index: The index of the item to insert.
987 * @markup: Markup of the item to insert.
989 * Inserts an action in @completion's action item list at position @index
990 * with markup @markup.
995 gtk_entry_completion_insert_action_markup (GtkEntryCompletion *completion,
999 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1000 g_return_if_fail (markup != NULL);
1002 gtk_entry_completion_insert_action (completion, index, markup, TRUE);
1006 * gtk_entry_completion_delete_action:
1007 * @completion: A #GtkEntryCompletion.
1008 * @index: The index of the item to Delete.
1010 * Deletes the action at @index from @completion's action list.
1015 gtk_entry_completion_delete_action (GtkEntryCompletion *completion,
1020 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1021 g_return_if_fail (index >= 0);
1023 gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (completion->priv->actions),
1024 &iter, NULL, index);
1025 gtk_list_store_remove (completion->priv->actions, &iter);
1029 * gtk_entry_completion_set_text_column:
1030 * @completion: A #GtkEntryCompletion.
1031 * @column: The column in the model of @completion to get strings from.
1033 * Convenience function for setting up the most used case of this code: a
1034 * completion list with just strings. This function will set up @completion
1035 * to have a list displaying all (and just) strings in the completion list,
1036 * and to get those strings from @column in the model of @completion.
1038 * This functions creates and adds a GtkCellRendererText for the selected column.
1043 gtk_entry_completion_set_text_column (GtkEntryCompletion *completion,
1046 GtkCellRenderer *cell;
1048 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1049 g_return_if_fail (column >= 0);
1051 completion->priv->text_column = column;
1053 cell = gtk_cell_renderer_text_new ();
1054 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion),
1056 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (completion),
1063 /* lame copy from gtkentry.c */
1065 get_borders (GtkEntry *entry,
1069 GtkWidget *widget = GTK_WIDGET (entry);
1071 gboolean interior_focus;
1073 gtk_widget_style_get (widget,
1074 "interior-focus", &interior_focus,
1075 "focus-line-width", &focus_width,
1078 if (entry->has_frame)
1080 *xborder = widget->style->xthickness;
1081 *yborder = widget->style->ythickness;
1089 if (!interior_focus)
1091 *xborder += focus_width;
1092 *yborder += focus_width;
1096 /* some nasty size requisition */
1098 _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
1101 gint items, height, x_border, y_border;
1104 GdkRectangle monitor;
1105 GtkRequisition popup_req;
1109 gdk_window_get_origin (completion->priv->entry->window, &x, &y);
1110 get_borders (GTK_ENTRY (completion->priv->entry), &x_border, &y_border);
1115 items = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
1117 items = MIN (items, 15);
1119 gtk_tree_view_column_cell_get_size (completion->priv->column, NULL,
1120 NULL, NULL, NULL, &height);
1123 gtk_widget_hide (completion->priv->scrolled_window);
1125 gtk_widget_show (completion->priv->scrolled_window);
1127 gtk_widget_set_size_request (completion->priv->tree_view,
1128 completion->priv->entry->allocation.width - 2 * x_border,
1131 /* default on no match */
1132 completion->priv->current_selected = -1;
1134 items = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
1138 gtk_widget_show (completion->priv->action_view);
1140 gtk_tree_view_column_cell_get_size (gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0),
1141 NULL, NULL, NULL, NULL,
1144 gtk_widget_set_size_request (completion->priv->action_view,
1145 completion->priv->entry->allocation.width - 2 * x_border,
1149 gtk_widget_hide (completion->priv->action_view);
1151 gtk_widget_size_request (completion->priv->popup_window, &popup_req);
1153 screen = gtk_widget_get_screen (GTK_WIDGET (completion->priv->entry));
1154 monitor_num = gdk_screen_get_monitor_at_window (screen,
1155 GTK_WIDGET (completion->priv->entry)->window);
1156 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1160 else if (x + popup_req.width > monitor.x + monitor.width)
1161 x = monitor.x + monitor.width - popup_req.width;
1163 if (y + height + popup_req.height <= monitor.y + monitor.height)
1167 path = gtk_tree_path_new_from_indices (0, -1);
1171 y -= popup_req.height;
1173 path = gtk_tree_path_new_from_indices (gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL) - 1, -1);
1176 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (completion->priv->tree_view), path, NULL, FALSE, 0.0, 0.0);
1177 gtk_tree_path_free (path);
1179 gtk_window_move (GTK_WINDOW (completion->priv->popup_window), x, y);
1185 _gtk_entry_completion_popup (GtkEntryCompletion *completion)
1187 GtkTreeViewColumn *column;
1190 if (GTK_WIDGET_MAPPED (completion->priv->popup_window))
1193 completion->priv->may_wrap = TRUE;
1195 column = gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0);
1196 renderers = gtk_tree_view_column_get_cell_renderers (column);
1197 gtk_widget_ensure_style (completion->priv->tree_view);
1198 g_object_set (GTK_CELL_RENDERER (renderers->data), "cell_background_gdk",
1199 &completion->priv->tree_view->style->bg[GTK_STATE_NORMAL],
1201 g_list_free (renderers);
1203 gtk_widget_show_all (completion->priv->vbox);
1205 _gtk_entry_completion_resize_popup (completion);
1207 gtk_widget_show (completion->priv->popup_window);
1209 gtk_grab_add (completion->priv->popup_window);
1210 gdk_pointer_grab (completion->priv->popup_window->window, TRUE,
1211 GDK_BUTTON_PRESS_MASK |
1212 GDK_BUTTON_RELEASE_MASK |
1213 GDK_POINTER_MOTION_MASK,
1214 NULL, NULL, GDK_CURRENT_TIME);
1218 _gtk_entry_completion_popdown (GtkEntryCompletion *completion)
1220 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1221 gtk_grab_remove (completion->priv->popup_window);
1223 gtk_widget_hide (completion->priv->popup_window);