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,
112 static gboolean gtk_entry_completion_list_enter_notify (GtkWidget *widget,
113 GdkEventCrossing *event,
115 static gboolean gtk_entry_completion_list_motion_notify (GtkWidget *widget,
116 GdkEventMotion *event,
118 static void gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
122 static void gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
123 GtkCellRenderer *cell,
128 static gboolean gtk_entry_completion_match_selected (GtkEntryCompletion *completion,
132 static GObjectClass *parent_class = NULL;
133 static guint entry_completion_signals[LAST_SIGNAL] = { 0 };
137 gtk_entry_completion_get_type (void)
139 static GType entry_completion_type = 0;
141 if (!entry_completion_type)
143 static const GTypeInfo entry_completion_info =
145 sizeof (GtkEntryCompletionClass),
148 (GClassInitFunc) gtk_entry_completion_class_init,
151 sizeof (GtkEntryCompletion),
153 (GInstanceInitFunc) gtk_entry_completion_init
156 static const GInterfaceInfo cell_layout_info =
158 (GInterfaceInitFunc) gtk_entry_completion_cell_layout_init,
163 entry_completion_type =
164 g_type_register_static (G_TYPE_OBJECT, "GtkEntryCompletion",
165 &entry_completion_info, 0);
167 g_type_add_interface_static (entry_completion_type,
168 GTK_TYPE_CELL_LAYOUT,
172 return entry_completion_type;
176 gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
178 GObjectClass *object_class;
180 parent_class = g_type_class_peek_parent (klass);
181 object_class = (GObjectClass *)klass;
183 object_class->set_property = gtk_entry_completion_set_property;
184 object_class->get_property = gtk_entry_completion_get_property;
185 object_class->finalize = gtk_entry_completion_finalize;
187 klass->match_selected = gtk_entry_completion_match_selected;
190 * GtkEntryCompletion::match-selected:
191 * @widget: the object which received the signal
192 * @model: the #GtkTreeModel containing the matches
193 * @iter: a #GtkTreeIter positioned at the selected match
195 * The ::match-selected signal is emitted when a match from the list
196 * is selected. The default behaviour is to replace the contents of the
197 * entry with the contents of the text column in the row pointed to by
200 * Return value: %TRUE if the signal has been handled
202 entry_completion_signals[MATCH_SELECTED] =
203 g_signal_new ("match_selected",
204 G_TYPE_FROM_CLASS (klass),
206 G_STRUCT_OFFSET (GtkEntryCompletionClass, match_selected),
207 _gtk_boolean_handled_accumulator, NULL,
208 _gtk_marshal_BOOLEAN__OBJECT_BOXED,
214 * GtkEntryCompletion::action-activated:
215 * @widget: the object which received the signal
216 * @index: the index of the activated action
218 * The ::action-activated signal is emitted when an action
221 entry_completion_signals[ACTION_ACTIVATED] =
222 g_signal_new ("action_activated",
223 G_TYPE_FROM_CLASS (klass),
225 G_STRUCT_OFFSET (GtkEntryCompletionClass, action_activated),
227 _gtk_marshal_NONE__INT,
231 g_object_class_install_property (object_class,
233 g_param_spec_object ("model",
234 P_("Completion Model"),
235 P_("The model to find matches in"),
238 g_object_class_install_property (object_class,
239 PROP_MINIMUM_KEY_LENGTH,
240 g_param_spec_int ("minimum_key_length",
241 P_("Minimum Key Length"),
242 P_("Minimum length of the search key in order to look up matches"),
248 * GtkEntryCompletion::text-column:
250 * The column of the model containing the strings.
254 g_object_class_install_property (object_class,
256 g_param_spec_int ("text_column",
258 P_("The column of the model containing the strings."),
264 g_type_class_add_private (object_class, sizeof (GtkEntryCompletionPrivate));
268 gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface)
270 iface->pack_start = gtk_entry_completion_pack_start;
271 iface->pack_end = gtk_entry_completion_pack_end;
272 iface->clear = gtk_entry_completion_clear;
273 iface->add_attribute = gtk_entry_completion_add_attribute;
274 iface->set_cell_data_func = gtk_entry_completion_set_cell_data_func;
275 iface->clear_attributes = gtk_entry_completion_clear_attributes;
276 iface->reorder = gtk_entry_completion_reorder;
280 gtk_entry_completion_init (GtkEntryCompletion *completion)
282 GtkCellRenderer *cell;
283 GtkTreeSelection *sel;
284 GtkEntryCompletionPrivate *priv;
285 GtkWidget *popup_frame;
287 /* yes, also priv, need to keep the code readable */
288 priv = completion->priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (completion);
290 priv->minimum_key_length = 1;
291 priv->text_column = -1;
294 priv->filter_model = NULL;
296 priv->tree_view = gtk_tree_view_new ();
297 g_signal_connect (priv->tree_view, "button_press_event",
298 G_CALLBACK (gtk_entry_completion_list_button_press),
300 g_signal_connect (priv->tree_view, "enter_notify_event",
301 G_CALLBACK (gtk_entry_completion_list_enter_notify),
303 g_signal_connect (priv->tree_view, "motion_notify_event",
304 G_CALLBACK (gtk_entry_completion_list_motion_notify),
306 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
307 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view), TRUE);
309 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
310 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
311 gtk_tree_selection_unselect_all (sel);
312 g_signal_connect (sel, "changed",
313 G_CALLBACK (gtk_entry_completion_selection_changed),
315 priv->first_sel_changed = TRUE;
317 priv->column = gtk_tree_view_column_new ();
318 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
320 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
321 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
323 GTK_POLICY_AUTOMATIC);
324 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
327 /* a nasty hack to get the completions treeview to size nicely */
328 gtk_widget_set_size_request (GTK_SCROLLED_WINDOW (priv->scrolled_window)->vscrollbar, -1, 0);
331 priv->actions = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_BOOLEAN);
334 gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->actions));
335 g_signal_connect (priv->action_view, "button_press_event",
336 G_CALLBACK (gtk_entry_completion_action_button_press),
338 g_signal_connect (priv->action_view, "enter_notify_event",
339 G_CALLBACK (gtk_entry_completion_list_enter_notify),
341 g_signal_connect (priv->action_view, "motion_notify_event",
342 G_CALLBACK (gtk_entry_completion_list_motion_notify),
344 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->action_view), FALSE);
345 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->action_view), TRUE);
347 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->action_view));
348 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
349 gtk_tree_selection_unselect_all (sel);
351 cell = gtk_cell_renderer_text_new ();
352 gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (priv->action_view),
355 gtk_entry_completion_action_data_func,
360 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
361 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
362 g_signal_connect (priv->popup_window, "key_press_event",
363 G_CALLBACK (gtk_entry_completion_popup_key_press),
365 g_signal_connect (priv->popup_window, "button_press_event",
366 G_CALLBACK (gtk_entry_completion_popup_button_press),
369 popup_frame = gtk_frame_new (NULL);
370 gtk_frame_set_shadow_type (GTK_FRAME (popup_frame),
371 GTK_SHADOW_ETCHED_IN);
372 gtk_widget_show (popup_frame);
373 gtk_container_add (GTK_CONTAINER (priv->popup_window), popup_frame);
375 priv->vbox = gtk_vbox_new (FALSE, 0);
376 gtk_container_add (GTK_CONTAINER (popup_frame), priv->vbox);
378 gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->tree_view);
379 gtk_box_pack_start (GTK_BOX (priv->vbox), priv->scrolled_window,
382 /* we don't want to see the action treeview when no actions have
383 * been inserted, so we pack the action treeview after the first
384 * action has been added
389 gtk_entry_completion_set_property (GObject *object,
394 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
395 GtkEntryCompletionPrivate *priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (completion);
400 gtk_entry_completion_set_model (completion,
401 g_value_get_object (value));
404 case PROP_MINIMUM_KEY_LENGTH:
405 gtk_entry_completion_set_minimum_key_length (completion,
406 g_value_get_int (value));
409 case PROP_TEXT_COLUMN:
410 priv->text_column = g_value_get_int (value);
414 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
420 gtk_entry_completion_get_property (GObject *object,
425 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
430 g_value_set_object (value,
431 gtk_entry_completion_get_model (completion));
434 case PROP_MINIMUM_KEY_LENGTH:
435 g_value_set_int (value, gtk_entry_completion_get_minimum_key_length (completion));
438 case PROP_TEXT_COLUMN:
439 g_value_set_int (value, gtk_entry_completion_get_text_column (completion));
443 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
449 gtk_entry_completion_finalize (GObject *object)
451 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
453 if (completion->priv->tree_view)
454 gtk_widget_destroy (completion->priv->tree_view);
456 if (completion->priv->entry)
457 gtk_entry_set_completion (GTK_ENTRY (completion->priv->entry), NULL);
459 if (completion->priv->actions)
460 g_object_unref (completion->priv->actions);
462 if (completion->priv->case_normalized_key)
463 g_free (completion->priv->case_normalized_key);
465 if (completion->priv->popup_window)
466 gtk_widget_destroy (completion->priv->popup_window);
468 G_OBJECT_CLASS (parent_class)->finalize (object);
471 /* implement cell layout interface */
473 gtk_entry_completion_pack_start (GtkCellLayout *cell_layout,
474 GtkCellRenderer *cell,
477 GtkEntryCompletionPrivate *priv;
479 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
481 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
483 gtk_tree_view_column_pack_start (priv->column, cell, expand);
487 gtk_entry_completion_pack_end (GtkCellLayout *cell_layout,
488 GtkCellRenderer *cell,
491 GtkEntryCompletionPrivate *priv;
493 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
495 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
497 gtk_tree_view_column_pack_end (priv->column, cell, expand);
501 gtk_entry_completion_clear (GtkCellLayout *cell_layout)
503 GtkEntryCompletionPrivate *priv;
505 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
507 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
509 gtk_tree_view_column_clear (priv->column);
513 gtk_entry_completion_add_attribute (GtkCellLayout *cell_layout,
514 GtkCellRenderer *cell,
515 const gchar *attribute,
518 GtkEntryCompletionPrivate *priv;
520 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
522 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
524 gtk_tree_view_column_add_attribute (priv->column, cell, attribute, column);
528 gtk_entry_completion_set_cell_data_func (GtkCellLayout *cell_layout,
529 GtkCellRenderer *cell,
530 GtkCellLayoutDataFunc func,
532 GDestroyNotify destroy)
534 GtkEntryCompletionPrivate *priv;
536 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
538 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
540 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column),
541 cell, func, func_data, destroy);
545 gtk_entry_completion_clear_attributes (GtkCellLayout *cell_layout,
546 GtkCellRenderer *cell)
548 GtkEntryCompletionPrivate *priv;
550 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
552 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
554 gtk_tree_view_column_clear_attributes (priv->column, cell);
558 gtk_entry_completion_reorder (GtkCellLayout *cell_layout,
559 GtkCellRenderer *cell,
562 GtkEntryCompletionPrivate *priv;
564 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
566 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
568 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column), cell, position);
571 /* all those callbacks */
573 gtk_entry_completion_default_completion_func (GtkEntryCompletion *completion,
579 gchar *normalized_string;
580 gchar *case_normalized_string;
582 gboolean ret = FALSE;
586 model = gtk_tree_model_filter_get_model (completion->priv->filter_model);
588 gtk_tree_model_get (model, iter,
589 completion->priv->text_column, &item,
594 normalized_string = g_utf8_normalize (item, -1, G_NORMALIZE_ALL);
595 case_normalized_string = g_utf8_casefold (normalized_string, -1);
597 if (!strncmp (key, case_normalized_string, strlen (key)))
601 g_free (normalized_string);
602 g_free (case_normalized_string);
609 gtk_entry_completion_visible_func (GtkTreeModel *model,
613 gboolean ret = FALSE;
615 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
617 if (!completion->priv->case_normalized_key)
620 if (completion->priv->match_func)
621 ret = (* completion->priv->match_func) (completion,
622 completion->priv->case_normalized_key,
624 completion->priv->match_data);
625 else if (completion->priv->text_column >= 0)
626 ret = gtk_entry_completion_default_completion_func (completion,
627 completion->priv->case_normalized_key,
635 gtk_entry_completion_popup_key_press (GtkWidget *widget,
639 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
641 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
644 /* propagate event to the entry */
645 gtk_widget_event (completion->priv->entry, (GdkEvent *)event);
651 gtk_entry_completion_popup_button_press (GtkWidget *widget,
652 GdkEventButton *event,
655 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
657 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
660 /* if we come here, it's usually time to popdown */
661 _gtk_entry_completion_popdown (completion);
667 gtk_entry_completion_list_button_press (GtkWidget *widget,
668 GdkEventButton *event,
671 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
672 GtkTreePath *path = NULL;
674 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
677 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
679 &path, NULL, NULL, NULL))
684 gtk_tree_model_get_iter (GTK_TREE_MODEL (completion->priv->filter_model),
686 gtk_tree_path_free (path);
688 g_signal_handler_block (completion->priv->entry,
689 completion->priv->changed_id);
690 g_signal_emit (completion, entry_completion_signals[MATCH_SELECTED],
691 0, GTK_TREE_MODEL (completion->priv->filter_model),
693 g_signal_handler_unblock (completion->priv->entry,
694 completion->priv->changed_id);
696 _gtk_entry_completion_popdown (completion);
705 gtk_entry_completion_action_button_press (GtkWidget *widget,
706 GdkEventButton *event,
709 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
710 GtkTreePath *path = NULL;
712 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
715 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
717 &path, NULL, NULL, NULL))
719 g_signal_emit (completion, entry_completion_signals[ACTION_ACTIVATED],
720 0, gtk_tree_path_get_indices (path)[0]);
721 gtk_tree_path_free (path);
723 _gtk_entry_completion_popdown (completion);
731 gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
732 GtkCellRenderer *cell,
737 gchar *string = NULL;
740 gtk_tree_model_get (model, iter,
749 g_object_set (G_OBJECT (cell),
754 g_object_set (G_OBJECT (cell),
763 gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
766 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
768 if (completion->priv->first_sel_changed)
770 completion->priv->first_sel_changed = FALSE;
771 if (gtk_widget_is_focus (completion->priv->tree_view))
772 gtk_tree_selection_unselect_all (selection);
779 * gtk_entry_completion_new:
781 * Creates a new #GtkEntryCompletion object.
783 * Return value: A newly created #GtkEntryCompletion object.
788 gtk_entry_completion_new (void)
790 GtkEntryCompletion *completion;
792 completion = g_object_new (GTK_TYPE_ENTRY_COMPLETION, NULL);
798 * gtk_entry_completion_get_entry:
799 * @completion: A #GtkEntryCompletion.
801 * Gets the entry @completion has been attached to.
803 * Return value: The entry @completion has been attached to.
808 gtk_entry_completion_get_entry (GtkEntryCompletion *completion)
810 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
812 return completion->priv->entry;
816 * gtk_entry_completion_set_model:
817 * @completion: A #GtkEntryCompletion.
818 * @model: The #GtkTreeModel.
820 * Sets the model for a #GtkEntryCompletion. If @completion already has
821 * a model set, it will remove it before setting the new model.
826 gtk_entry_completion_set_model (GtkEntryCompletion *completion,
829 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
830 g_return_if_fail (GTK_IS_TREE_MODEL (model));
832 /* code will unref the old filter model (if any) */
833 completion->priv->filter_model =
834 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
835 gtk_tree_model_filter_set_visible_func (completion->priv->filter_model,
836 gtk_entry_completion_visible_func,
839 gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
840 GTK_TREE_MODEL (completion->priv->filter_model));
841 g_object_unref (G_OBJECT (completion->priv->filter_model));
845 * gtk_entry_completion_get_model:
846 * @completion: A #GtkEntryCompletion.
848 * Returns the model the #GtkEntryCompletion is using as data source.
849 * Returns %NULL if the model is unset.
851 * Return value: A #GtkTreeModel, or %NULL if none is currently being used.
856 gtk_entry_completion_get_model (GtkEntryCompletion *completion)
858 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
860 return gtk_tree_model_filter_get_model (completion->priv->filter_model);
864 * gtk_entry_completion_set_match_func:
865 * @completion: A #GtkEntryCompletion.
866 * @func: The #GtkEntryCompletionMatchFunc to use.
867 * @func_data: The user data for @func.
868 * @func_notify: Destroy notifier for @func_data.
870 * Sets the match function for @completion to be @func. The match function
871 * is used to determine if a row should or should not be in the completion
877 gtk_entry_completion_set_match_func (GtkEntryCompletion *completion,
878 GtkEntryCompletionMatchFunc func,
880 GDestroyNotify func_notify)
882 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
884 if (completion->priv->match_notify)
885 (* completion->priv->match_notify) (completion->priv->match_data);
887 completion->priv->match_func = func;
888 completion->priv->match_data = func_data;
889 completion->priv->match_notify = func_notify;
893 * gtk_entry_completion_set_minimum_key_length:
894 * @completion: A #GtkEntryCompletion.
895 * @length: The minimum length of the key in order to start completing.
897 * Requires the length of the search key for @completion to be at least
898 * @length. This is useful for long lists, where completing using a small
899 * key takes a lot of time and will come up with meaningless results anyway
900 * (ie, a too large dataset).
905 gtk_entry_completion_set_minimum_key_length (GtkEntryCompletion *completion,
908 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
909 g_return_if_fail (length >= 1);
911 completion->priv->minimum_key_length = length;
915 * gtk_entry_completion_get_minimum_key_length:
916 * @completion: A #GtkEntryCompletion.
918 * Returns the minimum key length as set for @completion.
920 * Return value: The currently used minimum key length.
925 gtk_entry_completion_get_minimum_key_length (GtkEntryCompletion *completion)
927 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), 0);
929 return completion->priv->minimum_key_length;
933 * gtk_entry_completion_complete:
934 * @completion: A #GtkEntryCompletion.
936 * Requests a completion operation, or in other words a refiltering of the
937 * current list with completions, using the current key. The completion list
938 * view will be updated accordingly.
943 gtk_entry_completion_complete (GtkEntryCompletion *completion)
947 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
948 g_return_if_fail (completion->priv->filter_model != NULL);
950 if (completion->priv->case_normalized_key)
951 g_free (completion->priv->case_normalized_key);
953 tmp = g_utf8_normalize (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)),
954 -1, G_NORMALIZE_ALL);
955 completion->priv->case_normalized_key = g_utf8_casefold (tmp, -1);
958 gtk_tree_model_filter_refilter (completion->priv->filter_model);
962 gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
969 gtk_list_store_insert (completion->priv->actions, &iter, index);
970 gtk_list_store_set (completion->priv->actions, &iter,
975 if (!completion->priv->action_view->parent)
977 GtkTreePath *path = gtk_tree_path_new_from_indices (0, -1);
979 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
981 gtk_tree_path_free (path);
983 gtk_box_pack_start (GTK_BOX (completion->priv->vbox),
984 completion->priv->action_view, FALSE, FALSE, 0);
985 gtk_widget_show (completion->priv->action_view);
990 * gtk_entry_completion_insert_action_text:
991 * @completion: A #GtkEntryCompletion.
992 * @index_: The index of the item to insert.
993 * @text: Text of the item to insert.
995 * Inserts an action in @completion's action item list at position @index_
996 * with text @text. If you want the action item to have markup, use
997 * gtk_entry_completion_insert_action_markup().
1002 gtk_entry_completion_insert_action_text (GtkEntryCompletion *completion,
1006 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1007 g_return_if_fail (text != NULL);
1009 gtk_entry_completion_insert_action (completion, index_, text, FALSE);
1013 * gtk_entry_completion_insert_action_markup:
1014 * @completion: A #GtkEntryCompletion.
1015 * @index_: The index of the item to insert.
1016 * @markup: Markup of the item to insert.
1018 * Inserts an action in @completion's action item list at position @index_
1019 * with markup @markup.
1024 gtk_entry_completion_insert_action_markup (GtkEntryCompletion *completion,
1026 const gchar *markup)
1028 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1029 g_return_if_fail (markup != NULL);
1031 gtk_entry_completion_insert_action (completion, index_, markup, TRUE);
1035 * gtk_entry_completion_delete_action:
1036 * @completion: A #GtkEntryCompletion.
1037 * @index_: The index of the item to Delete.
1039 * Deletes the action at @index_ from @completion's action list.
1044 gtk_entry_completion_delete_action (GtkEntryCompletion *completion,
1049 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1050 g_return_if_fail (index_ >= 0);
1052 gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (completion->priv->actions),
1053 &iter, NULL, index_);
1054 gtk_list_store_remove (completion->priv->actions, &iter);
1058 * gtk_entry_completion_set_text_column:
1059 * @completion: A #GtkEntryCompletion.
1060 * @column: The column in the model of @completion to get strings from.
1062 * Convenience function for setting up the most used case of this code: a
1063 * completion list with just strings. This function will set up @completion
1064 * to have a list displaying all (and just) strings in the completion list,
1065 * and to get those strings from @column in the model of @completion.
1067 * This functions creates and adds a #GtkCellRendererText for the selected
1068 * column. If you need to set the text column, but don't want the cell
1069 * renderer, use g_object_set() to set the ::text_column property directly.
1074 gtk_entry_completion_set_text_column (GtkEntryCompletion *completion,
1077 GtkCellRenderer *cell;
1079 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1080 g_return_if_fail (column >= 0);
1082 completion->priv->text_column = column;
1084 cell = gtk_cell_renderer_text_new ();
1085 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion),
1087 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (completion),
1091 g_object_notify (completion, "text_column");
1095 * gtk_entry_completion_get_text_column:
1096 * @completion: a #GtkEntryCompletion
1098 * Returns the column in the model of @completion to get strings from.
1100 * Return value: the column containing the strings
1105 gtk_entry_completion_get_text_column (GtkEntryCompletion *completion)
1107 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1109 return completion->priv->text_column;
1114 /* lame copy from gtkentry.c */
1116 get_borders (GtkEntry *entry,
1120 GtkWidget *widget = GTK_WIDGET (entry);
1122 gboolean interior_focus;
1124 gtk_widget_style_get (widget,
1125 "interior-focus", &interior_focus,
1126 "focus-line-width", &focus_width,
1129 if (entry->has_frame)
1131 *xborder = widget->style->xthickness;
1132 *yborder = widget->style->ythickness;
1140 if (!interior_focus)
1142 *xborder += focus_width;
1143 *yborder += focus_width;
1148 gtk_entry_completion_list_enter_notify (GtkWidget *widget,
1149 GdkEventCrossing *event,
1152 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1154 return completion->priv->ignore_enter;
1158 gtk_entry_completion_list_motion_notify (GtkWidget *widget,
1159 GdkEventMotion *event,
1162 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1164 completion->priv->ignore_enter = FALSE;
1170 /* some nasty size requisition */
1172 _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
1175 gint matches, items, height, x_border, y_border;
1178 GdkRectangle monitor;
1179 GtkRequisition popup_req;
1184 gdk_window_get_origin (completion->priv->entry->window, &x, &y);
1185 get_borders (GTK_ENTRY (completion->priv->entry), &x_border, &y_border);
1190 matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
1192 items = MIN (matches, 15);
1194 gtk_tree_view_column_cell_get_size (completion->priv->column, NULL,
1195 NULL, NULL, NULL, &height);
1198 gtk_widget_hide (completion->priv->scrolled_window);
1200 gtk_widget_show (completion->priv->scrolled_window);
1202 screen = gtk_widget_get_screen (GTK_WIDGET (completion->priv->entry));
1203 monitor_num = gdk_screen_get_monitor_at_window (screen,
1204 GTK_WIDGET (completion->priv->entry)->window);
1205 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1207 width = MIN (completion->priv->entry->allocation.width, monitor.width);
1208 gtk_widget_set_size_request (completion->priv->tree_view,
1209 width - 2 * x_border, items * height);
1211 /* default on no match */
1212 completion->priv->current_selected = -1;
1214 items = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
1218 gtk_widget_show (completion->priv->action_view);
1220 gtk_tree_view_column_cell_get_size (gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0),
1221 NULL, NULL, NULL, NULL,
1224 gtk_widget_set_size_request (completion->priv->action_view,
1225 width - 2 * x_border, items * height);
1228 gtk_widget_hide (completion->priv->action_view);
1230 gtk_widget_size_request (completion->priv->popup_window, &popup_req);
1234 else if (x + popup_req.width > monitor.x + monitor.width)
1235 x = monitor.x + monitor.width - popup_req.width;
1237 if (y + height + popup_req.height <= monitor.y + monitor.height)
1244 y -= popup_req.height;
1250 path = gtk_tree_path_new_from_indices (above ? matches - 1 : 0, -1);
1251 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (completion->priv->tree_view), path,
1252 NULL, FALSE, 0.0, 0.0);
1253 gtk_tree_path_free (path);
1256 gtk_window_move (GTK_WINDOW (completion->priv->popup_window), x, y);
1262 _gtk_entry_completion_popup (GtkEntryCompletion *completion)
1264 GtkTreeViewColumn *column;
1267 if (GTK_WIDGET_MAPPED (completion->priv->popup_window))
1270 completion->priv->ignore_enter = TRUE;
1272 column = gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0);
1273 renderers = gtk_tree_view_column_get_cell_renderers (column);
1274 gtk_widget_ensure_style (completion->priv->tree_view);
1275 g_object_set (GTK_CELL_RENDERER (renderers->data), "cell_background_gdk",
1276 &completion->priv->tree_view->style->bg[GTK_STATE_NORMAL],
1278 g_list_free (renderers);
1280 gtk_widget_show_all (completion->priv->vbox);
1282 _gtk_entry_completion_resize_popup (completion);
1284 gtk_widget_show (completion->priv->popup_window);
1286 gtk_grab_add (completion->priv->popup_window);
1287 gdk_pointer_grab (completion->priv->popup_window->window, TRUE,
1288 GDK_BUTTON_PRESS_MASK |
1289 GDK_BUTTON_RELEASE_MASK |
1290 GDK_POINTER_MOTION_MASK,
1291 NULL, NULL, GDK_CURRENT_TIME);
1295 _gtk_entry_completion_popdown (GtkEntryCompletion *completion)
1297 completion->priv->ignore_enter = FALSE;
1299 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1300 gtk_grab_remove (completion->priv->popup_window);
1302 gtk_widget_hide (completion->priv->popup_window);
1306 gtk_entry_completion_match_selected (GtkEntryCompletion *completion,
1307 GtkTreeModel *model,
1312 gtk_tree_model_get (model, iter, completion->priv->text_column, &str, -1);
1313 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), str);
1315 /* move cursor to the end */
1316 gtk_editable_set_position (GTK_EDITABLE (completion->priv->entry), -1);