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"
27 #include "gtktreeselection.h"
28 #include "gtktreeview.h"
29 #include "gtkscrolledwindow.h"
31 #include "gtkwindow.h"
34 #include "gtksignal.h"
35 #include "gtkmarshalers.h"
53 PROP_MINIMUM_KEY_LENGTH
56 #define GTK_ENTRY_COMPLETION_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ENTRY_COMPLETION, GtkEntryCompletionPrivate))
58 static void gtk_entry_completion_class_init (GtkEntryCompletionClass *klass);
59 static void gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface);
60 static void gtk_entry_completion_init (GtkEntryCompletion *completion);
61 static void gtk_entry_completion_set_property (GObject *object,
65 static void gtk_entry_completion_get_property (GObject *object,
69 static void gtk_entry_completion_finalize (GObject *object);
71 static void gtk_entry_completion_pack_start (GtkCellLayout *cell_layout,
72 GtkCellRenderer *cell,
74 static void gtk_entry_completion_pack_end (GtkCellLayout *cell_layout,
75 GtkCellRenderer *cell,
77 static void gtk_entry_completion_clear (GtkCellLayout *cell_layout);
78 static void gtk_entry_completion_add_attribute (GtkCellLayout *cell_layout,
79 GtkCellRenderer *cell,
80 const char *attribute,
82 static void gtk_entry_completion_set_cell_data_func (GtkCellLayout *cell_layout,
83 GtkCellRenderer *cell,
84 GtkCellLayoutDataFunc func,
86 GDestroyNotify destroy);
87 static void gtk_entry_completion_clear_attributes (GtkCellLayout *cell_layout,
88 GtkCellRenderer *cell);
89 static void gtk_entry_completion_reorder (GtkCellLayout *cell_layout,
90 GtkCellRenderer *cell,
93 static gboolean gtk_entry_completion_visible_func (GtkTreeModel *model,
96 static gboolean gtk_entry_completion_popup_key_press (GtkWidget *widget,
99 static gboolean gtk_entry_completion_popup_button_press (GtkWidget *widget,
100 GdkEventButton *event,
102 static gboolean gtk_entry_completion_list_button_press (GtkWidget *widget,
103 GdkEventButton *event,
105 static gboolean gtk_entry_completion_action_button_press (GtkWidget *widget,
106 GdkEventButton *event,
108 static void gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
111 static void gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
115 static void gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
116 GtkCellRenderer *cell,
122 static GObjectClass *parent_class = NULL;
123 static guint entry_completion_signals[LAST_SIGNAL] = { 0 };
127 gtk_entry_completion_get_type (void)
129 static GType entry_completion_type = 0;
131 if (!entry_completion_type)
133 static const GTypeInfo entry_completion_info =
135 sizeof (GtkEntryCompletionClass),
138 (GClassInitFunc) gtk_entry_completion_class_init,
141 sizeof (GtkEntryCompletion),
143 (GInstanceInitFunc) gtk_entry_completion_init
146 static const GInterfaceInfo cell_layout_info =
148 (GInterfaceInitFunc) gtk_entry_completion_cell_layout_init,
153 entry_completion_type =
154 g_type_register_static (G_TYPE_OBJECT, "GtkEntryCompletion",
155 &entry_completion_info, 0);
157 g_type_add_interface_static (entry_completion_type,
158 GTK_TYPE_CELL_LAYOUT,
162 return entry_completion_type;
166 gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
168 GObjectClass *object_class;
170 parent_class = g_type_class_peek_parent (klass);
171 object_class = (GObjectClass *)klass;
173 object_class->set_property = gtk_entry_completion_set_property;
174 object_class->get_property = gtk_entry_completion_get_property;
175 object_class->finalize = gtk_entry_completion_finalize;
177 entry_completion_signals[MATCH_SELECTED] =
178 g_signal_new ("match_selected",
179 G_TYPE_FROM_CLASS (klass),
181 G_STRUCT_OFFSET (GtkEntryCompletionClass, match_selected),
182 _gtk_boolean_handled_accumulator, NULL,
183 _gtk_marshal_BOOLEAN__OBJECT_BOXED,
187 entry_completion_signals[ACTION_ACTIVATED] =
188 g_signal_new ("action_activated",
189 G_TYPE_FROM_CLASS (klass),
191 G_STRUCT_OFFSET (GtkEntryCompletionClass, action_activated),
193 _gtk_marshal_NONE__INT,
197 g_object_class_install_property (object_class,
199 g_param_spec_object ("model",
200 P_("Completion Model"),
201 P_("The model to find matches in"),
204 g_object_class_install_property (object_class,
205 PROP_MINIMUM_KEY_LENGTH,
206 g_param_spec_int ("minimum_key_length",
207 P_("Minimum Key Length"),
208 P_("Minimum length of the search key in order to look up matches"),
214 g_type_class_add_private (object_class, sizeof (GtkEntryCompletionPrivate));
218 gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface)
220 iface->pack_start = gtk_entry_completion_pack_start;
221 iface->pack_end = gtk_entry_completion_pack_end;
222 iface->clear = gtk_entry_completion_clear;
223 iface->add_attribute = gtk_entry_completion_add_attribute;
224 iface->set_cell_data_func = gtk_entry_completion_set_cell_data_func;
225 iface->clear_attributes = gtk_entry_completion_clear_attributes;
226 iface->reorder = gtk_entry_completion_reorder;
230 gtk_entry_completion_init (GtkEntryCompletion *completion)
232 GtkCellRenderer *cell;
233 GtkTreeSelection *sel;
234 GtkEntryCompletionPrivate *priv;
236 /* yes, also priv, need to keep the code readable */
237 priv = completion->priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (completion);
239 priv->minimum_key_length = 1;
240 priv->text_column = -1;
243 priv->filter_model = NULL;
245 priv->tree_view = gtk_tree_view_new ();
246 g_signal_connect (priv->tree_view, "button_press_event",
247 G_CALLBACK (gtk_entry_completion_list_button_press),
249 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
251 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
252 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
253 gtk_tree_selection_unselect_all (sel);
254 g_signal_connect (sel, "changed",
255 G_CALLBACK (gtk_entry_completion_selection_changed),
257 priv->first_sel_changed = TRUE;
259 priv->column = gtk_tree_view_column_new ();
260 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
262 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
263 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
265 GTK_POLICY_AUTOMATIC);
266 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
267 GTK_SHADOW_ETCHED_IN);
269 /* a nasty hack to get the completions treeview to size nicely */
270 gtk_widget_set_size_request (GTK_SCROLLED_WINDOW (priv->scrolled_window)->vscrollbar, -1, 0);
273 priv->actions = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_BOOLEAN);
276 gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->actions));
277 g_signal_connect (priv->action_view, "button_press_event",
278 G_CALLBACK (gtk_entry_completion_action_button_press),
280 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->action_view), FALSE);
282 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->action_view));
283 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
284 gtk_tree_selection_unselect_all (sel);
286 cell = gtk_cell_renderer_text_new ();
287 g_object_set (cell, "cell_background_gdk",
288 &priv->tree_view->style->bg[GTK_STATE_NORMAL],
290 gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (priv->action_view),
293 gtk_entry_completion_action_data_func,
298 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
299 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
300 g_signal_connect (priv->popup_window, "key_press_event",
301 G_CALLBACK (gtk_entry_completion_popup_key_press),
303 g_signal_connect (priv->popup_window, "button_press_event",
304 G_CALLBACK (gtk_entry_completion_popup_button_press),
307 priv->vbox = gtk_vbox_new (FALSE, 0);
308 gtk_container_add (GTK_CONTAINER (priv->popup_window), priv->vbox);
310 gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->tree_view);
311 gtk_box_pack_start (GTK_BOX (priv->vbox), priv->scrolled_window,
314 /* we don't want to see the action treeview when no actions have
315 * been inserted, so we pack the action treeview after the first
316 * action has been added
321 gtk_entry_completion_set_property (GObject *object,
326 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
331 gtk_entry_completion_set_model (completion,
332 g_value_get_object (value));
335 case PROP_MINIMUM_KEY_LENGTH:
336 gtk_entry_completion_set_minimum_key_length (completion,
337 g_value_get_int (value));
341 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
347 gtk_entry_completion_get_property (GObject *object,
352 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
357 g_value_set_object (value,
358 gtk_entry_completion_get_model (completion));
361 case PROP_MINIMUM_KEY_LENGTH:
362 g_value_set_int (value, gtk_entry_completion_get_minimum_key_length (completion));
366 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
372 gtk_entry_completion_finalize (GObject *object)
374 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
376 if (completion->priv->tree_view)
377 gtk_widget_destroy (completion->priv->tree_view);
379 if (completion->priv->entry)
380 gtk_entry_set_completion (GTK_ENTRY (completion->priv->entry), NULL);
382 if (completion->priv->actions)
383 g_object_unref (completion->priv->actions);
385 if (completion->priv->case_normalized_key)
386 g_free (completion->priv->case_normalized_key);
388 if (completion->priv->popup_window)
389 gtk_widget_destroy (completion->priv->popup_window);
391 G_OBJECT_CLASS (parent_class)->finalize (object);
394 /* implement cell layout interface */
396 gtk_entry_completion_pack_start (GtkCellLayout *cell_layout,
397 GtkCellRenderer *cell,
400 GtkEntryCompletionPrivate *priv;
402 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
404 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
406 gtk_tree_view_column_pack_start (priv->column, cell, expand);
410 gtk_entry_completion_pack_end (GtkCellLayout *cell_layout,
411 GtkCellRenderer *cell,
414 GtkEntryCompletionPrivate *priv;
416 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
418 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
420 gtk_tree_view_column_pack_end (priv->column, cell, expand);
424 gtk_entry_completion_clear (GtkCellLayout *cell_layout)
426 GtkEntryCompletionPrivate *priv;
428 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
430 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
432 gtk_tree_view_column_clear (priv->column);
436 gtk_entry_completion_add_attribute (GtkCellLayout *cell_layout,
437 GtkCellRenderer *cell,
438 const gchar *attribute,
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_add_attribute (priv->column, cell, attribute, column);
451 gtk_entry_completion_set_cell_data_func (GtkCellLayout *cell_layout,
452 GtkCellRenderer *cell,
453 GtkCellLayoutDataFunc func,
455 GDestroyNotify destroy)
457 GtkEntryCompletionPrivate *priv;
459 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
461 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
463 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column),
464 cell, func, func_data, destroy);
468 gtk_entry_completion_clear_attributes (GtkCellLayout *cell_layout,
469 GtkCellRenderer *cell)
471 GtkEntryCompletionPrivate *priv;
473 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
475 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
477 gtk_tree_view_column_clear_attributes (priv->column, cell);
481 gtk_entry_completion_reorder (GtkCellLayout *cell_layout,
482 GtkCellRenderer *cell,
485 GtkEntryCompletionPrivate *priv;
487 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
489 priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
491 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column), cell, position);
494 /* all those callbacks */
496 gtk_entry_completion_default_completion_func (GtkEntryCompletion *completion,
502 gchar *normalized_string;
503 gchar *case_normalized_string;
505 gboolean ret = FALSE;
509 model = gtk_tree_model_filter_get_model (completion->priv->filter_model);
511 gtk_tree_model_get (model, iter,
512 completion->priv->text_column, &item,
517 normalized_string = g_utf8_normalize (item, -1, G_NORMALIZE_ALL);
518 case_normalized_string = g_utf8_casefold (normalized_string, -1);
520 if (!strncmp (key, case_normalized_string, strlen (key)))
524 g_free (normalized_string);
525 g_free (case_normalized_string);
532 gtk_entry_completion_visible_func (GtkTreeModel *model,
536 gboolean ret = FALSE;
538 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
540 if (!completion->priv->case_normalized_key)
543 if (completion->priv->text_column >= 0)
544 ret = gtk_entry_completion_default_completion_func (completion,
545 completion->priv->case_normalized_key,
549 else if (completion->priv->match_func)
550 ret = (* completion->priv->match_func) (completion,
551 completion->priv->case_normalized_key,
553 completion->priv->match_data);
559 gtk_entry_completion_popup_key_press (GtkWidget *widget,
563 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
565 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
568 /* propagate event to the entry */
569 gtk_widget_event (completion->priv->entry, (GdkEvent *)event);
575 gtk_entry_completion_popup_button_press (GtkWidget *widget,
576 GdkEventButton *event,
579 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
581 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
584 /* if we come here, it's usually time to popdown */
585 _gtk_entry_completion_popdown (completion);
591 gtk_entry_completion_list_button_press (GtkWidget *widget,
592 GdkEventButton *event,
595 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
596 GtkTreePath *path = NULL;
598 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
601 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
603 &path, NULL, NULL, NULL))
608 gtk_tree_model_get_iter (GTK_TREE_MODEL (completion->priv->filter_model),
610 gtk_tree_path_free (path);
612 g_signal_emit (completion, entry_completion_signals[MATCH_SELECTED],
613 0, GTK_TREE_MODEL (completion->priv->filter_model),
620 gtk_tree_model_get (GTK_TREE_MODEL (completion->priv->filter_model),
622 completion->priv->text_column, &str,
625 g_signal_handler_block (completion->priv->entry,
626 completion->priv->changed_id);
627 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), str);
628 g_signal_handler_unblock (completion->priv->entry,
629 completion->priv->changed_id);
631 /* move cursor to the end */
632 gtk_editable_set_position (GTK_EDITABLE (completion->priv->entry),
638 _gtk_entry_completion_popdown (completion);
646 gtk_entry_completion_action_button_press (GtkWidget *widget,
647 GdkEventButton *event,
650 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
651 GtkTreePath *path = NULL;
653 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
656 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
658 &path, NULL, NULL, NULL))
660 g_signal_emit (completion, entry_completion_signals[ACTION_ACTIVATED],
661 0, gtk_tree_path_get_indices (path)[0]);
662 gtk_tree_path_free (path);
664 _gtk_entry_completion_popdown (completion);
672 gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
673 GtkCellRenderer *cell,
678 gchar *string = NULL;
681 gtk_tree_model_get (model, iter,
690 g_object_set (G_OBJECT (cell),
695 g_object_set (G_OBJECT (cell),
704 gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
707 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
709 if (completion->priv->first_sel_changed)
711 completion->priv->first_sel_changed = FALSE;
712 if (gtk_widget_is_focus (completion->priv->tree_view))
713 gtk_tree_selection_unselect_all (selection);
720 * gtk_entry_completion_new:
722 * Creates a new #GtkEntryCompletion object.
724 * Return value: A newly created #GtkEntryCompletion object.
729 gtk_entry_completion_new (void)
731 GtkEntryCompletion *completion;
733 completion = g_object_new (GTK_TYPE_ENTRY_COMPLETION, NULL);
739 * gtk_entry_completion_get_entry:
740 * @completion: A #GtkEntryCompletion.
742 * Gets the entry @completion has been attached to.
744 * Return value: The entry @completion has been attached to.
749 gtk_entry_completion_get_entry (GtkEntryCompletion *completion)
751 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
753 return completion->priv->entry;
757 * gtk_entry_completion_set_model:
758 * @completion: A #GtkEntryCompletion.
759 * @model: The #GtkTreeModel.
761 * Sets the model for a #GtkEntryCompletion. If @completion already has
762 * a model set, it will remove it before setting the new model.
767 gtk_entry_completion_set_model (GtkEntryCompletion *completion,
770 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
771 g_return_if_fail (GTK_IS_TREE_MODEL (model));
773 /* code will unref the old filter model (if any) */
774 completion->priv->filter_model =
775 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
776 gtk_tree_model_filter_set_visible_func (completion->priv->filter_model,
777 gtk_entry_completion_visible_func,
780 gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
781 GTK_TREE_MODEL (completion->priv->filter_model));
782 g_object_unref (G_OBJECT (completion->priv->filter_model));
786 * gtk_entry_completion_get_model:
787 * @completion: A #GtkEntryCompletion.
789 * Returns the model the #GtkEntryCompletion is using as data source.
790 * Returns %NULL if the model is unset.
792 * Return value: A #GtkTreeModel, or %NULL if none is currently being used.
797 gtk_entry_completion_get_model (GtkEntryCompletion *completion)
799 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
801 return gtk_tree_model_filter_get_model (completion->priv->filter_model);
805 * gtk_entry_completion_set_match_func:
806 * @completion: A #GtkEntryCompletion.
807 * @func: The #GtkEntryCompletionMatchFunc to use.
808 * @func_data: The user data for @func.
809 * @func_notify: Destroy notifier for @func_data.
811 * Sets the match function for @completion to be @func. The match function
812 * is used to determine if a row should or should not be in the completion
818 gtk_entry_completion_set_match_func (GtkEntryCompletion *completion,
819 GtkEntryCompletionMatchFunc func,
821 GDestroyNotify func_notify)
823 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
825 if (completion->priv->match_notify)
826 (* completion->priv->match_notify) (completion->priv->match_data);
828 completion->priv->match_func = func;
829 completion->priv->match_data = func_data;
830 completion->priv->match_notify = func_notify;
834 * gtk_entry_completion_set_minimum_key_length:
835 * @completion: A #GtkEntryCompletion.
836 * @length: The minimum length of the key in order to start completing.
838 * Requires the length of the search key for @completion to be at least
839 * @length. This is useful for long lists, where completing using a small
840 * key takes a lot of time and will come up with meaningless results anyway
841 * (ie, a too large dataset).
846 gtk_entry_completion_set_minimum_key_length (GtkEntryCompletion *completion,
849 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
850 g_return_if_fail (length >= 1);
852 completion->priv->minimum_key_length = length;
856 * gtk_entry_completion_get_minimum_key_length:
857 * @completion: A #GtkEntryCompletion.
859 * Returns the minimum key length as set for @completion.
861 * Return value: The currently used minimum key length.
866 gtk_entry_completion_get_minimum_key_length (GtkEntryCompletion *completion)
868 g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), 0);
870 return completion->priv->minimum_key_length;
874 * gtk_entry_completion_complete:
875 * @completion: A #GtkEntryCompletion.
877 * Requests a completion operation, or in other words a refiltering of the
878 * current list with completions, using the current key. The completion list
879 * view will be updated accordingly.
884 gtk_entry_completion_complete (GtkEntryCompletion *completion)
888 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
889 g_return_if_fail (completion->priv->filter_model != NULL);
891 if (completion->priv->case_normalized_key)
892 g_free (completion->priv->case_normalized_key);
894 tmp = g_utf8_normalize (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)),
895 -1, G_NORMALIZE_ALL);
896 completion->priv->case_normalized_key = g_utf8_casefold (tmp, -1);
899 gtk_tree_model_filter_refilter (completion->priv->filter_model);
903 gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
910 gtk_list_store_insert (completion->priv->actions, &iter, index);
911 gtk_list_store_set (completion->priv->actions, &iter,
916 if (!completion->priv->action_view->parent)
918 GtkTreePath *path = gtk_tree_path_new_from_indices (0, -1);
920 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
922 gtk_tree_path_free (path);
924 gtk_box_pack_start (GTK_BOX (completion->priv->vbox),
925 completion->priv->action_view, FALSE, FALSE, 0);
926 gtk_widget_show (completion->priv->action_view);
931 * gtk_entry_completion_insert_action_text:
932 * @completion: A #GtkEntryCompletion.
933 * @index: The index of the item to insert.
934 * @text: Text of the item to insert.
936 * Inserts an action in @completion's action item list at position @index
937 * with text @text. If you want the action item to have markup, use
938 * gtk_entry_completion_insert_action_markup().
943 gtk_entry_completion_insert_action_text (GtkEntryCompletion *completion,
947 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
948 g_return_if_fail (text != NULL);
950 gtk_entry_completion_insert_action (completion, index, text, FALSE);
954 * gtk_entry_completion_insert_action_markup:
955 * @completion: A #GtkEntryCompletion.
956 * @index: The index of the item to insert.
957 * @markup: Markup of the item to insert.
959 * Inserts an action in @completion's action item list at position @index
960 * with markup @markup.
965 gtk_entry_completion_insert_action_markup (GtkEntryCompletion *completion,
969 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
970 g_return_if_fail (markup != NULL);
972 gtk_entry_completion_insert_action (completion, index, markup, TRUE);
976 * gtk_entry_completion_delete_action:
977 * @completion: A #GtkEntryCompletion.
978 * @index: The index of the item to Delete.
980 * Deletes the action at @index from @completion's action list.
985 gtk_entry_completion_delete_action (GtkEntryCompletion *completion,
990 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
991 g_return_if_fail (index >= 0);
993 gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (completion->priv->actions),
995 gtk_list_store_remove (completion->priv->actions, &iter);
999 * gtk_entry_completion_set_text_column:
1000 * @completion: A #GtkEntryCompletion.
1001 * @column: The column in the model of @completion to get strings from.
1003 * Convenience function for setting up the most used case of this code: a
1004 * completion list with just strings. This function will set up @completion
1005 * to have a list displaying all (and just) strings in the completion list,
1006 * and to get those strings from @column in the model of @completion.
1008 * This functions creates and adds a GtkCellRendererText for the selected column.
1013 gtk_entry_completion_set_text_column (GtkEntryCompletion *completion,
1016 GtkCellRenderer *cell;
1018 g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1019 g_return_if_fail (column >= 0);
1021 completion->priv->text_column = column;
1023 cell = gtk_cell_renderer_text_new ();
1024 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion),
1026 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (completion),
1033 /* lame copy from gtkentry.c */
1035 get_borders (GtkEntry *entry,
1039 GtkWidget *widget = GTK_WIDGET (entry);
1041 gboolean interior_focus;
1043 gtk_widget_style_get (widget,
1044 "interior-focus", &interior_focus,
1045 "focus-line-width", &focus_width,
1048 if (entry->has_frame)
1050 *xborder = widget->style->xthickness;
1051 *yborder = widget->style->ythickness;
1059 if (!interior_focus)
1061 *xborder += focus_width;
1062 *yborder += focus_width;
1066 /* some nasty size requisition */
1068 _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
1070 gint items, height, x_border, y_border;
1072 get_borders (GTK_ENTRY (completion->priv->entry), &x_border, &y_border);
1074 items = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
1076 items = MIN (items, 15);
1078 gtk_tree_view_column_cell_get_size (completion->priv->column, NULL,
1079 NULL, NULL, NULL, &height);
1082 gtk_widget_hide (completion->priv->scrolled_window);
1084 gtk_widget_show (completion->priv->scrolled_window);
1086 gtk_widget_set_size_request (completion->priv->tree_view,
1087 completion->priv->entry->allocation.width - 2 * x_border,
1090 /* default on no match */
1091 completion->priv->current_selected = -1;
1093 items = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
1097 gtk_widget_show (completion->priv->action_view);
1099 gtk_tree_view_column_cell_get_size (gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0),
1100 NULL, NULL, NULL, NULL,
1103 gtk_widget_set_size_request (completion->priv->action_view,
1104 completion->priv->entry->allocation.width - 2 * x_border,
1108 gtk_widget_hide (completion->priv->action_view);
1114 _gtk_entry_completion_popup (GtkEntryCompletion *completion)
1116 gint x, y, x_border, y_border;
1120 GdkRectangle monitor;
1121 GtkRequisition popup_req;
1123 if (GTK_WIDGET_MAPPED (completion->priv->popup_window))
1126 gtk_widget_show_all (completion->priv->vbox);
1128 gdk_window_get_origin (completion->priv->entry->window, &x, &y);
1129 get_borders (GTK_ENTRY (completion->priv->entry), &x_border, &y_border);
1134 height = _gtk_entry_completion_resize_popup (completion);
1136 gtk_widget_size_request (completion->priv->popup_window, &popup_req);
1138 screen = gtk_widget_get_screen (GTK_WIDGET (completion->priv->entry));
1139 monitor_num = gdk_screen_get_monitor_at_window (screen,
1140 GTK_WIDGET (completion->priv->entry)->window);
1141 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1145 else if (x + popup_req.width > monitor.x + monitor.width)
1146 x = monitor.x + monitor.width - popup_req.width;
1148 if (y + height + popup_req.height <= monitor.y + monitor.height)
1151 y -= popup_req.height;
1153 gtk_window_move (GTK_WINDOW (completion->priv->popup_window), x, y);
1155 gtk_widget_show (completion->priv->popup_window);
1157 gtk_grab_add (completion->priv->popup_window);
1158 gdk_pointer_grab (completion->priv->popup_window->window, TRUE,
1159 GDK_BUTTON_PRESS_MASK |
1160 GDK_BUTTON_RELEASE_MASK |
1161 GDK_POINTER_MOTION_MASK,
1162 NULL, NULL, GDK_CURRENT_TIME);
1166 _gtk_entry_completion_popdown (GtkEntryCompletion *completion)
1168 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1169 gtk_grab_remove (completion->priv->popup_window);
1171 gtk_widget_hide (completion->priv->popup_window);