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.
822 * If model is %NULL, then it will unset the model.
827 gtk_entry_completion_set_model (GtkEntryCompletion *completion,
830 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
831 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
833 /* code will unref the old filter model (if any) */
834 completion->priv->filter_model =
835 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
836 gtk_tree_model_filter_set_visible_func (completion->priv->filter_model,
837 gtk_entry_completion_visible_func,
840 gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
841 GTK_TREE_MODEL (completion->priv->filter_model));
842 g_object_unref (G_OBJECT (completion->priv->filter_model));
846 * gtk_entry_completion_get_model:
847 * @completion: A #GtkEntryCompletion.
849 * Returns the model the #GtkEntryCompletion is using as data source.
850 * Returns %NULL if the model is unset.
852 * Return value: A #GtkTreeModel, or %NULL if none is currently being used.
857 gtk_entry_completion_get_model (GtkEntryCompletion *completion)
859 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
861 return gtk_tree_model_filter_get_model (completion->priv->filter_model);
865 * gtk_entry_completion_set_match_func:
866 * @completion: A #GtkEntryCompletion.
867 * @func: The #GtkEntryCompletionMatchFunc to use.
868 * @func_data: The user data for @func.
869 * @func_notify: Destroy notifier for @func_data.
871 * Sets the match function for @completion to be @func. The match function
872 * is used to determine if a row should or should not be in the completion
878 gtk_entry_completion_set_match_func (GtkEntryCompletion *completion,
879 GtkEntryCompletionMatchFunc func,
881 GDestroyNotify func_notify)
883 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
885 if (completion->priv->match_notify)
886 (* completion->priv->match_notify) (completion->priv->match_data);
888 completion->priv->match_func = func;
889 completion->priv->match_data = func_data;
890 completion->priv->match_notify = func_notify;
894 * gtk_entry_completion_set_minimum_key_length:
895 * @completion: A #GtkEntryCompletion.
896 * @length: The minimum length of the key in order to start completing.
898 * Requires the length of the search key for @completion to be at least
899 * @length. This is useful for long lists, where completing using a small
900 * key takes a lot of time and will come up with meaningless results anyway
901 * (ie, a too large dataset).
906 gtk_entry_completion_set_minimum_key_length (GtkEntryCompletion *completion,
909 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
910 g_return_if_fail (length >= 1);
912 completion->priv->minimum_key_length = length;
916 * gtk_entry_completion_get_minimum_key_length:
917 * @completion: A #GtkEntryCompletion.
919 * Returns the minimum key length as set for @completion.
921 * Return value: The currently used minimum key length.
926 gtk_entry_completion_get_minimum_key_length (GtkEntryCompletion *completion)
928 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), 0);
930 return completion->priv->minimum_key_length;
934 * gtk_entry_completion_complete:
935 * @completion: A #GtkEntryCompletion.
937 * Requests a completion operation, or in other words a refiltering of the
938 * current list with completions, using the current key. The completion list
939 * view will be updated accordingly.
944 gtk_entry_completion_complete (GtkEntryCompletion *completion)
948 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
949 g_return_if_fail (completion->priv->filter_model != NULL);
951 if (completion->priv->case_normalized_key)
952 g_free (completion->priv->case_normalized_key);
954 tmp = g_utf8_normalize (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)),
955 -1, G_NORMALIZE_ALL);
956 completion->priv->case_normalized_key = g_utf8_casefold (tmp, -1);
959 gtk_tree_model_filter_refilter (completion->priv->filter_model);
963 gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
970 gtk_list_store_insert (completion->priv->actions, &iter, index);
971 gtk_list_store_set (completion->priv->actions, &iter,
976 if (!completion->priv->action_view->parent)
978 GtkTreePath *path = gtk_tree_path_new_from_indices (0, -1);
980 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
982 gtk_tree_path_free (path);
984 gtk_box_pack_start (GTK_BOX (completion->priv->vbox),
985 completion->priv->action_view, FALSE, FALSE, 0);
986 gtk_widget_show (completion->priv->action_view);
991 * gtk_entry_completion_insert_action_text:
992 * @completion: A #GtkEntryCompletion.
993 * @index_: The index of the item to insert.
994 * @text: Text of the item to insert.
996 * Inserts an action in @completion's action item list at position @index_
997 * with text @text. If you want the action item to have markup, use
998 * gtk_entry_completion_insert_action_markup().
1003 gtk_entry_completion_insert_action_text (GtkEntryCompletion *completion,
1007 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1008 g_return_if_fail (text != NULL);
1010 gtk_entry_completion_insert_action (completion, index_, text, FALSE);
1014 * gtk_entry_completion_insert_action_markup:
1015 * @completion: A #GtkEntryCompletion.
1016 * @index_: The index of the item to insert.
1017 * @markup: Markup of the item to insert.
1019 * Inserts an action in @completion's action item list at position @index_
1020 * with markup @markup.
1025 gtk_entry_completion_insert_action_markup (GtkEntryCompletion *completion,
1027 const gchar *markup)
1029 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1030 g_return_if_fail (markup != NULL);
1032 gtk_entry_completion_insert_action (completion, index_, markup, TRUE);
1036 * gtk_entry_completion_delete_action:
1037 * @completion: A #GtkEntryCompletion.
1038 * @index_: The index of the item to Delete.
1040 * Deletes the action at @index_ from @completion's action list.
1045 gtk_entry_completion_delete_action (GtkEntryCompletion *completion,
1050 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1051 g_return_if_fail (index_ >= 0);
1053 gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (completion->priv->actions),
1054 &iter, NULL, index_);
1055 gtk_list_store_remove (completion->priv->actions, &iter);
1059 * gtk_entry_completion_set_text_column:
1060 * @completion: A #GtkEntryCompletion.
1061 * @column: The column in the model of @completion to get strings from.
1063 * Convenience function for setting up the most used case of this code: a
1064 * completion list with just strings. This function will set up @completion
1065 * to have a list displaying all (and just) strings in the completion list,
1066 * and to get those strings from @column in the model of @completion.
1068 * This functions creates and adds a #GtkCellRendererText for the selected
1069 * column. If you need to set the text column, but don't want the cell
1070 * renderer, use g_object_set() to set the ::text_column property directly.
1075 gtk_entry_completion_set_text_column (GtkEntryCompletion *completion,
1078 GtkCellRenderer *cell;
1080 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1081 g_return_if_fail (column >= 0);
1083 completion->priv->text_column = column;
1085 cell = gtk_cell_renderer_text_new ();
1086 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion),
1088 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (completion),
1092 g_object_notify (completion, "text_column");
1096 * gtk_entry_completion_get_text_column:
1097 * @completion: a #GtkEntryCompletion
1099 * Returns the column in the model of @completion to get strings from.
1101 * Return value: the column containing the strings
1106 gtk_entry_completion_get_text_column (GtkEntryCompletion *completion)
1108 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1110 return completion->priv->text_column;
1115 /* lame copy from gtkentry.c */
1117 get_borders (GtkEntry *entry,
1121 GtkWidget *widget = GTK_WIDGET (entry);
1123 gboolean interior_focus;
1125 gtk_widget_style_get (widget,
1126 "interior-focus", &interior_focus,
1127 "focus-line-width", &focus_width,
1130 if (entry->has_frame)
1132 *xborder = widget->style->xthickness;
1133 *yborder = widget->style->ythickness;
1141 if (!interior_focus)
1143 *xborder += focus_width;
1144 *yborder += focus_width;
1149 gtk_entry_completion_list_enter_notify (GtkWidget *widget,
1150 GdkEventCrossing *event,
1153 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1155 return completion->priv->ignore_enter;
1159 gtk_entry_completion_list_motion_notify (GtkWidget *widget,
1160 GdkEventMotion *event,
1163 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1165 completion->priv->ignore_enter = FALSE;
1171 /* some nasty size requisition */
1173 _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
1176 gint matches, items, height, x_border, y_border;
1179 GdkRectangle monitor;
1180 GtkRequisition popup_req;
1185 gdk_window_get_origin (completion->priv->entry->window, &x, &y);
1186 get_borders (GTK_ENTRY (completion->priv->entry), &x_border, &y_border);
1191 matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
1193 items = MIN (matches, 15);
1195 gtk_tree_view_column_cell_get_size (completion->priv->column, NULL,
1196 NULL, NULL, NULL, &height);
1199 gtk_widget_hide (completion->priv->scrolled_window);
1201 gtk_widget_show (completion->priv->scrolled_window);
1203 screen = gtk_widget_get_screen (GTK_WIDGET (completion->priv->entry));
1204 monitor_num = gdk_screen_get_monitor_at_window (screen,
1205 GTK_WIDGET (completion->priv->entry)->window);
1206 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1208 width = MIN (completion->priv->entry->allocation.width, monitor.width);
1209 gtk_widget_set_size_request (completion->priv->tree_view,
1210 width - 2 * x_border, items * height);
1212 /* default on no match */
1213 completion->priv->current_selected = -1;
1215 items = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
1219 gtk_widget_show (completion->priv->action_view);
1221 gtk_tree_view_column_cell_get_size (gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0),
1222 NULL, NULL, NULL, NULL,
1225 gtk_widget_set_size_request (completion->priv->action_view,
1226 width - 2 * x_border, items * height);
1229 gtk_widget_hide (completion->priv->action_view);
1231 gtk_widget_size_request (completion->priv->popup_window, &popup_req);
1235 else if (x + popup_req.width > monitor.x + monitor.width)
1236 x = monitor.x + monitor.width - popup_req.width;
1238 if (y + height + popup_req.height <= monitor.y + monitor.height)
1245 y -= popup_req.height;
1251 path = gtk_tree_path_new_from_indices (above ? matches - 1 : 0, -1);
1252 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (completion->priv->tree_view), path,
1253 NULL, FALSE, 0.0, 0.0);
1254 gtk_tree_path_free (path);
1257 gtk_window_move (GTK_WINDOW (completion->priv->popup_window), x, y);
1263 _gtk_entry_completion_popup (GtkEntryCompletion *completion)
1265 GtkTreeViewColumn *column;
1268 if (GTK_WIDGET_MAPPED (completion->priv->popup_window))
1271 completion->priv->ignore_enter = TRUE;
1273 column = gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0);
1274 renderers = gtk_tree_view_column_get_cell_renderers (column);
1275 gtk_widget_ensure_style (completion->priv->tree_view);
1276 g_object_set (GTK_CELL_RENDERER (renderers->data), "cell_background_gdk",
1277 &completion->priv->tree_view->style->bg[GTK_STATE_NORMAL],
1279 g_list_free (renderers);
1281 gtk_widget_show_all (completion->priv->vbox);
1283 _gtk_entry_completion_resize_popup (completion);
1285 gtk_widget_show (completion->priv->popup_window);
1287 gtk_grab_add (completion->priv->popup_window);
1288 gdk_pointer_grab (completion->priv->popup_window->window, TRUE,
1289 GDK_BUTTON_PRESS_MASK |
1290 GDK_BUTTON_RELEASE_MASK |
1291 GDK_POINTER_MOTION_MASK,
1292 NULL, NULL, GDK_CURRENT_TIME);
1296 _gtk_entry_completion_popdown (GtkEntryCompletion *completion)
1298 completion->priv->ignore_enter = FALSE;
1300 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1301 gtk_grab_remove (completion->priv->popup_window);
1303 gtk_widget_hide (completion->priv->popup_window);
1307 gtk_entry_completion_match_selected (GtkEntryCompletion *completion,
1308 GtkTreeModel *model,
1313 gtk_tree_model_get (model, iter, completion->priv->text_column, &str, -1);
1314 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), str);
1316 /* move cursor to the end */
1317 gtk_editable_set_position (GTK_EDITABLE (completion->priv->entry), -1);