]> Pileus Git - ~andy/gtk/blob - gtk/gtkentrycompletion.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkentrycompletion.c
1 /* gtkentrycompletion.c
2  * Copyright (C) 2003  Kristian Rietveld  <kris@gtk.org>
3  *
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.
8  *
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.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /**
19  * SECTION:gtkentrycompletion
20  * @Short_description: Completion functionality for GtkEntry
21  * @Title: GtkEntryCompletion
22  *
23  * #GtkEntryCompletion is an auxiliary object to be used in conjunction with
24  * #GtkEntry to provide the completion functionality. It implements the
25  * #GtkCellLayout interface, to allow the user to add extra cells to the
26  * #GtkTreeView with completion matches.
27  *
28  * "Completion functionality" means that when the user modifies the text
29  * in the entry, #GtkEntryCompletion checks which rows in the model match
30  * the current content of the entry, and displays a list of matches.
31  * By default, the matching is done by comparing the entry text
32  * case-insensitively against the text column of the model (see
33  * gtk_entry_completion_set_text_column()), but this can be overridden
34  * with a custom match function (see gtk_entry_completion_set_match_func()).
35  *
36  * When the user selects a completion, the content of the entry is
37  * updated. By default, the content of the entry is replaced by the
38  * text column of the model, but this can be overridden by connecting
39  * to the #GtkEntryCompletion::match-selected signal and updating the
40  * entry in the signal handler. Note that you should return %TRUE from
41  * the signal handler to suppress the default behaviour.
42  *
43  * To add completion functionality to an entry, use gtk_entry_set_completion().
44  *
45  * In addition to regular completion matches, which will be inserted into the
46  * entry when they are selected, #GtkEntryCompletion also allows to display
47  * "actions" in the popup window. Their appearance is similar to menuitems,
48  * to differentiate them clearly from completion strings. When an action is
49  * selected, the #GtkEntryCompletion::action-activated signal is emitted.
50  *
51  * GtkEntryCompletion uses a #GtkTreeModelFilter model to represent the
52  * subset of the entire model that is currently matching. While the
53  * GtkEntryCompletion signals #GtkEntryCompletion::match-selected and
54  * #GtkEntryCompletion::cursor-on-match take the original model and an
55  * iter pointing to that model as arguments, other callbacks and signals
56  * (such as #GtkCellLayoutDataFuncs or #GtkCellArea::apply-attributes)
57  * will generally take the filter model as argument. As long as you are
58  * only calling gtk_tree_model_get(), this will make no difference to
59  * you. If for some reason, you need the original model, use
60  * gtk_tree_model_filter_get_model(). Don't forget to use
61  * gtk_tree_model_filter_convert_iter_to_child_iter() to obtain a
62  * matching iter.
63  */
64
65 #include "config.h"
66
67 #include "gtkentrycompletion.h"
68
69 #include "gtkentryprivate.h"
70 #include "gtkcelllayout.h"
71 #include "gtkcellareabox.h"
72
73 #include "gtkintl.h"
74 #include "gtkcellrenderertext.h"
75 #include "gtkframe.h"
76 #include "gtktreeselection.h"
77 #include "gtktreeview.h"
78 #include "gtkscrolledwindow.h"
79 #include "gtksizerequest.h"
80 #include "gtkbox.h"
81 #include "gtkwindow.h"
82 #include "gtkentry.h"
83 #include "gtkmain.h"
84 #include "gtkmarshalers.h"
85
86 #include "gtkprivate.h"
87
88 #include <string.h>
89
90 #define PAGE_STEP 14
91 #define COMPLETION_TIMEOUT 300
92
93 /* signals */
94 enum
95 {
96   INSERT_PREFIX,
97   MATCH_SELECTED,
98   ACTION_ACTIVATED,
99   CURSOR_ON_MATCH,
100   LAST_SIGNAL
101 };
102
103 /* properties */
104 enum
105 {
106   PROP_0,
107   PROP_MODEL,
108   PROP_MINIMUM_KEY_LENGTH,
109   PROP_TEXT_COLUMN,
110   PROP_INLINE_COMPLETION,
111   PROP_POPUP_COMPLETION,
112   PROP_POPUP_SET_WIDTH,
113   PROP_POPUP_SINGLE_MATCH,
114   PROP_INLINE_SELECTION,
115   PROP_CELL_AREA
116 };
117
118
119 static void     gtk_entry_completion_cell_layout_init    (GtkCellLayoutIface      *iface);
120 static GtkCellArea* gtk_entry_completion_get_area        (GtkCellLayout           *cell_layout);
121
122 static GObject *gtk_entry_completion_constructor         (GType                    type,
123                                                           guint                    n_construct_properties,
124                                                           GObjectConstructParam   *construct_properties);
125 static void     gtk_entry_completion_set_property        (GObject      *object,
126                                                           guint         prop_id,
127                                                           const GValue *value,
128                                                           GParamSpec   *pspec);
129 static void     gtk_entry_completion_get_property        (GObject      *object,
130                                                           guint         prop_id,
131                                                           GValue       *value,
132                                                           GParamSpec   *pspec);
133 static void     gtk_entry_completion_finalize            (GObject      *object);
134 static void     gtk_entry_completion_dispose             (GObject      *object);
135
136 static gboolean gtk_entry_completion_visible_func        (GtkTreeModel       *model,
137                                                           GtkTreeIter        *iter,
138                                                           gpointer            data);
139 static gboolean gtk_entry_completion_popup_key_event     (GtkWidget          *widget,
140                                                           GdkEventKey        *event,
141                                                           gpointer            user_data);
142 static gboolean gtk_entry_completion_popup_button_press  (GtkWidget          *widget,
143                                                           GdkEventButton     *event,
144                                                           gpointer            user_data);
145 static gboolean gtk_entry_completion_list_button_press   (GtkWidget          *widget,
146                                                           GdkEventButton     *event,
147                                                           gpointer            user_data);
148 static gboolean gtk_entry_completion_action_button_press (GtkWidget          *widget,
149                                                           GdkEventButton     *event,
150                                                           gpointer            user_data);
151 static void     gtk_entry_completion_selection_changed   (GtkTreeSelection   *selection,
152                                                           gpointer            data);
153 static gboolean gtk_entry_completion_list_enter_notify   (GtkWidget          *widget,
154                                                           GdkEventCrossing   *event,
155                                                           gpointer            data);
156 static gboolean gtk_entry_completion_list_motion_notify  (GtkWidget          *widget,
157                                                           GdkEventMotion     *event,
158                                                           gpointer            data);
159 static void     gtk_entry_completion_insert_action       (GtkEntryCompletion *completion,
160                                                           gint                index,
161                                                           const gchar        *string,
162                                                           gboolean            markup);
163 static void     gtk_entry_completion_action_data_func    (GtkTreeViewColumn  *tree_column,
164                                                           GtkCellRenderer    *cell,
165                                                           GtkTreeModel       *model,
166                                                           GtkTreeIter        *iter,
167                                                           gpointer            data);
168
169 static gboolean gtk_entry_completion_match_selected      (GtkEntryCompletion *completion,
170                                                           GtkTreeModel       *model,
171                                                           GtkTreeIter        *iter);
172 static gboolean gtk_entry_completion_real_insert_prefix  (GtkEntryCompletion *completion,
173                                                           const gchar        *prefix);
174 static gboolean gtk_entry_completion_cursor_on_match     (GtkEntryCompletion *completion,
175                                                           GtkTreeModel       *model,
176                                                           GtkTreeIter        *iter);
177 static gboolean gtk_entry_completion_insert_completion   (GtkEntryCompletion *completion,
178                                                           GtkTreeModel       *model,
179                                                           GtkTreeIter        *iter);
180 static void     gtk_entry_completion_insert_completion_text (GtkEntryCompletion *completion,
181                                                              const gchar *text);
182 static void     connect_completion_signals                  (GtkEntryCompletion *completion);
183 static void     disconnect_completion_signals               (GtkEntryCompletion *completion);
184
185
186 static guint entry_completion_signals[LAST_SIGNAL] = { 0 };
187
188 /* GtkBuildable */
189 static void     gtk_entry_completion_buildable_init      (GtkBuildableIface  *iface);
190
191 G_DEFINE_TYPE_WITH_CODE (GtkEntryCompletion, gtk_entry_completion, G_TYPE_OBJECT,
192                          G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
193                                                 gtk_entry_completion_cell_layout_init)
194                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
195                                                 gtk_entry_completion_buildable_init))
196
197
198 static void
199 gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
200 {
201   GObjectClass *object_class;
202
203   object_class = (GObjectClass *)klass;
204
205   object_class->constructor = gtk_entry_completion_constructor;
206   object_class->set_property = gtk_entry_completion_set_property;
207   object_class->get_property = gtk_entry_completion_get_property;
208   object_class->dispose = gtk_entry_completion_dispose;
209   object_class->finalize = gtk_entry_completion_finalize;
210
211   klass->match_selected = gtk_entry_completion_match_selected;
212   klass->insert_prefix = gtk_entry_completion_real_insert_prefix;
213   klass->cursor_on_match = gtk_entry_completion_cursor_on_match;
214
215   /**
216    * GtkEntryCompletion::insert-prefix:
217    * @widget: the object which received the signal
218    * @prefix: the common prefix of all possible completions
219    *
220    * Gets emitted when the inline autocompletion is triggered.
221    * The default behaviour is to make the entry display the
222    * whole prefix and select the newly inserted part.
223    *
224    * Applications may connect to this signal in order to insert only a
225    * smaller part of the @prefix into the entry - e.g. the entry used in
226    * the #GtkFileChooser inserts only the part of the prefix up to the
227    * next '/'.
228    *
229    * Return value: %TRUE if the signal has been handled
230    *
231    * Since: 2.6
232    */
233   entry_completion_signals[INSERT_PREFIX] =
234     g_signal_new (I_("insert-prefix"),
235                   G_TYPE_FROM_CLASS (klass),
236                   G_SIGNAL_RUN_LAST,
237                   G_STRUCT_OFFSET (GtkEntryCompletionClass, insert_prefix),
238                   _gtk_boolean_handled_accumulator, NULL,
239                   _gtk_marshal_BOOLEAN__STRING,
240                   G_TYPE_BOOLEAN, 1,
241                   G_TYPE_STRING);
242
243   /**
244    * GtkEntryCompletion::match-selected:
245    * @widget: the object which received the signal
246    * @model: the #GtkTreeModel containing the matches
247    * @iter: a #GtkTreeIter positioned at the selected match
248    *
249    * Gets emitted when a match from the list is selected.
250    * The default behaviour is to replace the contents of the
251    * entry with the contents of the text column in the row
252    * pointed to by @iter.
253    *
254    * Note that @model is the model that was passed to
255    * gtk_entry_completion_set_model().
256    *
257    * Return value: %TRUE if the signal has been handled
258    *
259    * Since: 2.4
260    */
261   entry_completion_signals[MATCH_SELECTED] =
262     g_signal_new (I_("match-selected"),
263                   G_TYPE_FROM_CLASS (klass),
264                   G_SIGNAL_RUN_LAST,
265                   G_STRUCT_OFFSET (GtkEntryCompletionClass, match_selected),
266                   _gtk_boolean_handled_accumulator, NULL,
267                   _gtk_marshal_BOOLEAN__OBJECT_BOXED,
268                   G_TYPE_BOOLEAN, 2,
269                   GTK_TYPE_TREE_MODEL,
270                   GTK_TYPE_TREE_ITER);
271
272   /**
273    * GtkEntryCompletion::cursor-on-match:
274    * @widget: the object which received the signal
275    * @model: the #GtkTreeModel containing the matches
276    * @iter: a #GtkTreeIter positioned at the selected match
277    *
278    * Gets emitted when a match from the cursor is on a match
279    * of the list. The default behaviour is to replace the contents
280    * of the entry with the contents of the text column in the row
281    * pointed to by @iter.
282    *
283    * Note that @model is the model that was passed to
284    * gtk_entry_completion_set_model().
285    *
286    * Return value: %TRUE if the signal has been handled
287    *
288    * Since: 2.12
289    */
290   entry_completion_signals[CURSOR_ON_MATCH] =
291     g_signal_new (I_("cursor-on-match"),
292                   G_TYPE_FROM_CLASS (klass),
293                   G_SIGNAL_RUN_LAST,
294                   G_STRUCT_OFFSET (GtkEntryCompletionClass, cursor_on_match),
295                   _gtk_boolean_handled_accumulator, NULL,
296                   _gtk_marshal_BOOLEAN__OBJECT_BOXED,
297                   G_TYPE_BOOLEAN, 2,
298                   GTK_TYPE_TREE_MODEL,
299                   GTK_TYPE_TREE_ITER);
300
301   /**
302    * GtkEntryCompletion::action-activated:
303    * @widget: the object which received the signal
304    * @index: the index of the activated action
305    *
306    * Gets emitted when an action is activated.
307    *
308    * Since: 2.4
309    */
310   entry_completion_signals[ACTION_ACTIVATED] =
311     g_signal_new (I_("action-activated"),
312                   G_TYPE_FROM_CLASS (klass),
313                   G_SIGNAL_RUN_LAST,
314                   G_STRUCT_OFFSET (GtkEntryCompletionClass, action_activated),
315                   NULL, NULL,
316                   _gtk_marshal_VOID__INT,
317                   G_TYPE_NONE, 1,
318                   G_TYPE_INT);
319
320   g_object_class_install_property (object_class,
321                                    PROP_MODEL,
322                                    g_param_spec_object ("model",
323                                                         P_("Completion Model"),
324                                                         P_("The model to find matches in"),
325                                                         GTK_TYPE_TREE_MODEL,
326                                                         GTK_PARAM_READWRITE));
327   g_object_class_install_property (object_class,
328                                    PROP_MINIMUM_KEY_LENGTH,
329                                    g_param_spec_int ("minimum-key-length",
330                                                      P_("Minimum Key Length"),
331                                                      P_("Minimum length of the search key in order to look up matches"),
332                                                      0,
333                                                      G_MAXINT,
334                                                      1,
335                                                      GTK_PARAM_READWRITE));
336   /**
337    * GtkEntryCompletion:text-column:
338    *
339    * The column of the model containing the strings.
340    * Note that the strings must be UTF-8.
341    *
342    * Since: 2.6
343    */
344   g_object_class_install_property (object_class,
345                                    PROP_TEXT_COLUMN,
346                                    g_param_spec_int ("text-column",
347                                                      P_("Text column"),
348                                                      P_("The column of the model containing the strings."),
349                                                      -1,
350                                                      G_MAXINT,
351                                                      -1,
352                                                      GTK_PARAM_READWRITE));
353
354   /**
355    * GtkEntryCompletion:inline-completion:
356    *
357    * Determines whether the common prefix of the possible completions
358    * should be inserted automatically in the entry. Note that this
359    * requires text-column to be set, even if you are using a custom
360    * match function.
361    *
362    * Since: 2.6
363    **/
364   g_object_class_install_property (object_class,
365                                    PROP_INLINE_COMPLETION,
366                                    g_param_spec_boolean ("inline-completion",
367                                                          P_("Inline completion"),
368                                                          P_("Whether the common prefix should be inserted automatically"),
369                                                          FALSE,
370                                                          GTK_PARAM_READWRITE));
371   /**
372    * GtkEntryCompletion:popup-completion:
373    *
374    * Determines whether the possible completions should be
375    * shown in a popup window.
376    *
377    * Since: 2.6
378    **/
379   g_object_class_install_property (object_class,
380                                    PROP_POPUP_COMPLETION,
381                                    g_param_spec_boolean ("popup-completion",
382                                                          P_("Popup completion"),
383                                                          P_("Whether the completions should be shown in a popup window"),
384                                                          TRUE,
385                                                          GTK_PARAM_READWRITE));
386
387   /**
388    * GtkEntryCompletion:popup-set-width:
389    *
390    * Determines whether the completions popup window will be
391    * resized to the width of the entry.
392    *
393    * Since: 2.8
394    */
395   g_object_class_install_property (object_class,
396                                    PROP_POPUP_SET_WIDTH,
397                                    g_param_spec_boolean ("popup-set-width",
398                                                          P_("Popup set width"),
399                                                          P_("If TRUE, the popup window will have the same size as the entry"),
400                                                          TRUE,
401                                                          GTK_PARAM_READWRITE));
402
403   /**
404    * GtkEntryCompletion:popup-single-match:
405    *
406    * Determines whether the completions popup window will shown
407    * for a single possible completion. You probably want to set
408    * this to %FALSE if you are using
409    * <link linkend="GtkEntryCompletion--inline-completion">inline
410    * completion</link>.
411    *
412    * Since: 2.8
413    */
414   g_object_class_install_property (object_class,
415                                    PROP_POPUP_SINGLE_MATCH,
416                                    g_param_spec_boolean ("popup-single-match",
417                                                          P_("Popup single match"),
418                                                          P_("If TRUE, the popup window will appear for a single match."),
419                                                          TRUE,
420                                                          GTK_PARAM_READWRITE));
421   /**
422    * GtkEntryCompletion:inline-selection:
423    *
424    * Determines whether the possible completions on the popup
425    * will appear in the entry as you navigate through them.
426    *
427    * Since: 2.12
428    */
429   g_object_class_install_property (object_class,
430                                    PROP_INLINE_SELECTION,
431                                    g_param_spec_boolean ("inline-selection",
432                                                          P_("Inline selection"),
433                                                          P_("Your description here"),
434                                                          FALSE,
435                                                          GTK_PARAM_READWRITE));
436
437
438   /**
439    * GtkEntryCompletion:cell-area:
440    *
441    * The #GtkCellArea used to layout cell renderers in the treeview column.
442    *
443    * If no area is specified when creating the entry completion with gtk_entry_completion_new_with_area() 
444    * a horizontally oriented #GtkCellAreaBox will be used.
445    *
446    * Since: 3.0
447    */
448   g_object_class_install_property (object_class,
449                                    PROP_CELL_AREA,
450                                    g_param_spec_object ("cell-area",
451                                                         P_("Cell Area"),
452                                                         P_("The GtkCellArea used to layout cells"),
453                                                         GTK_TYPE_CELL_AREA,
454                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
455
456   g_type_class_add_private (object_class, sizeof (GtkEntryCompletionPrivate));
457 }
458
459
460 static void
461 gtk_entry_completion_buildable_custom_tag_end (GtkBuildable *buildable,
462                                                 GtkBuilder   *builder,
463                                                 GObject      *child,
464                                                 const gchar  *tagname,
465                                                 gpointer     *data)
466 {
467   /* Just ignore the boolean return from here */
468   _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname, data);
469 }
470
471 static void
472 gtk_entry_completion_buildable_init (GtkBuildableIface *iface)
473 {
474   iface->add_child = _gtk_cell_layout_buildable_add_child;
475   iface->custom_tag_start = _gtk_cell_layout_buildable_custom_tag_start;
476   iface->custom_tag_end = gtk_entry_completion_buildable_custom_tag_end;
477 }
478
479 static void
480 gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface)
481 {
482   iface->get_area = gtk_entry_completion_get_area;
483 }
484
485 static void
486 gtk_entry_completion_init (GtkEntryCompletion *completion)
487 {
488   GtkEntryCompletionPrivate *priv;
489
490   /* yes, also priv, need to keep the code readable */
491   completion->priv = G_TYPE_INSTANCE_GET_PRIVATE (completion,
492                                                   GTK_TYPE_ENTRY_COMPLETION,
493                                                   GtkEntryCompletionPrivate);
494   priv = completion->priv;
495
496   priv->minimum_key_length = 1;
497   priv->text_column = -1;
498   priv->has_completion = FALSE;
499   priv->inline_completion = FALSE;
500   priv->popup_completion = TRUE;
501   priv->popup_set_width = TRUE;
502   priv->popup_single_match = TRUE;
503   priv->inline_selection = FALSE;
504
505   priv->filter_model = NULL;
506 }
507
508 static GObject *
509 gtk_entry_completion_constructor (GType                  type,
510                                   guint                  n_construct_properties,
511                                   GObjectConstructParam *construct_properties)
512 {
513   GtkEntryCompletion        *completion;
514   GtkEntryCompletionPrivate *priv;
515   GObject                   *object;
516   GtkCellRenderer           *cell;
517   GtkTreeSelection          *sel;
518   GtkWidget                 *popup_frame;
519
520   object = G_OBJECT_CLASS (gtk_entry_completion_parent_class)->constructor
521     (type, n_construct_properties, construct_properties);
522
523   completion = (GtkEntryCompletion *) object;
524   priv       = completion->priv;
525
526   if (!priv->cell_area)
527     {
528       priv->cell_area = gtk_cell_area_box_new ();
529       g_object_ref_sink (priv->cell_area);
530     }
531
532   /* completions */
533   priv->tree_view = gtk_tree_view_new ();
534   g_signal_connect (priv->tree_view, "button-press-event",
535                     G_CALLBACK (gtk_entry_completion_list_button_press),
536                     completion);
537   g_signal_connect (priv->tree_view, "enter-notify-event",
538                     G_CALLBACK (gtk_entry_completion_list_enter_notify),
539                     completion);
540   g_signal_connect (priv->tree_view, "motion-notify-event",
541                     G_CALLBACK (gtk_entry_completion_list_motion_notify),
542                     completion);
543
544   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
545   gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view), TRUE);
546
547   sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
548   gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
549   gtk_tree_selection_unselect_all (sel);
550   g_signal_connect (sel, "changed",
551                     G_CALLBACK (gtk_entry_completion_selection_changed),
552                     completion);
553   priv->first_sel_changed = TRUE;
554
555   priv->column = gtk_tree_view_column_new_with_area (priv->cell_area);
556   gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
557
558   priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
559   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
560                                   GTK_POLICY_NEVER,
561                                   GTK_POLICY_AUTOMATIC);
562   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
563                                        GTK_SHADOW_NONE);
564
565   /* a nasty hack to get the completions treeview to size nicely */
566   gtk_widget_set_size_request (gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (priv->scrolled_window)),
567                                -1, 0);
568
569   /* actions */
570   priv->actions = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_BOOLEAN);
571
572   priv->action_view =
573     gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->actions));
574   g_object_ref_sink (priv->action_view);
575   g_signal_connect (priv->action_view, "button-press-event",
576                     G_CALLBACK (gtk_entry_completion_action_button_press),
577                     completion);
578   g_signal_connect (priv->action_view, "enter-notify-event",
579                     G_CALLBACK (gtk_entry_completion_list_enter_notify),
580                     completion);
581   g_signal_connect (priv->action_view, "motion-notify-event",
582                     G_CALLBACK (gtk_entry_completion_list_motion_notify),
583                     completion);
584   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->action_view), FALSE);
585   gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->action_view), TRUE);
586
587   sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->action_view));
588   gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
589   gtk_tree_selection_unselect_all (sel);
590
591   cell = gtk_cell_renderer_text_new ();
592   gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (priv->action_view),
593                                               0, "",
594                                               cell,
595                                               gtk_entry_completion_action_data_func,
596                                               NULL,
597                                               NULL);
598
599   /* pack it all */
600   priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
601   gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
602   gtk_window_set_type_hint (GTK_WINDOW(priv->popup_window),
603                             GDK_WINDOW_TYPE_HINT_COMBO);
604   g_signal_connect (priv->popup_window, "key-press-event",
605                     G_CALLBACK (gtk_entry_completion_popup_key_event),
606                     completion);
607   g_signal_connect (priv->popup_window, "key-release-event",
608                     G_CALLBACK (gtk_entry_completion_popup_key_event),
609                     completion);
610   g_signal_connect (priv->popup_window, "button-press-event",
611                     G_CALLBACK (gtk_entry_completion_popup_button_press),
612                     completion);
613
614   popup_frame = gtk_frame_new (NULL);
615   gtk_frame_set_shadow_type (GTK_FRAME (popup_frame),
616                              GTK_SHADOW_ETCHED_IN);
617   gtk_widget_show (popup_frame);
618   gtk_container_add (GTK_CONTAINER (priv->popup_window), popup_frame);
619
620   priv->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
621   gtk_container_add (GTK_CONTAINER (popup_frame), priv->vbox);
622
623   gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->tree_view);
624   gtk_box_pack_start (GTK_BOX (priv->vbox), priv->scrolled_window,
625                       TRUE, TRUE, 0);
626
627   /* we don't want to see the action treeview when no actions have
628    * been inserted, so we pack the action treeview after the first
629    * action has been added
630    */
631
632   return object;
633 }
634
635
636 static void
637 gtk_entry_completion_set_property (GObject      *object,
638                                    guint         prop_id,
639                                    const GValue *value,
640                                    GParamSpec   *pspec)
641 {
642   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
643   GtkEntryCompletionPrivate *priv = completion->priv;
644   GtkCellArea *area;
645
646   switch (prop_id)
647     {
648       case PROP_MODEL:
649         gtk_entry_completion_set_model (completion,
650                                         g_value_get_object (value));
651         break;
652
653       case PROP_MINIMUM_KEY_LENGTH:
654         gtk_entry_completion_set_minimum_key_length (completion,
655                                                      g_value_get_int (value));
656         break;
657
658       case PROP_TEXT_COLUMN:
659         priv->text_column = g_value_get_int (value);
660         break;
661
662       case PROP_INLINE_COMPLETION:
663         gtk_entry_completion_set_inline_completion (completion,
664                                                     g_value_get_boolean (value));
665         break;
666
667       case PROP_POPUP_COMPLETION:
668         gtk_entry_completion_set_popup_completion (completion,
669                                                    g_value_get_boolean (value));
670         break;
671
672       case PROP_POPUP_SET_WIDTH:
673         gtk_entry_completion_set_popup_set_width (completion,
674                                                   g_value_get_boolean (value));
675         break;
676
677       case PROP_POPUP_SINGLE_MATCH:
678         gtk_entry_completion_set_popup_single_match (completion,
679                                                      g_value_get_boolean (value));
680         break;
681
682       case PROP_INLINE_SELECTION:
683         gtk_entry_completion_set_inline_selection (completion,
684                                                    g_value_get_boolean (value));
685         break;
686
687       case PROP_CELL_AREA:
688         /* Construct-only, can only be assigned once */
689         area = g_value_get_object (value);
690         if (area)
691           {
692             if (priv->cell_area != NULL)
693               {
694                 g_warning ("cell-area has already been set, ignoring construct property");
695                 g_object_ref_sink (area);
696                 g_object_unref (area);
697               }
698             else
699               priv->cell_area = g_object_ref_sink (area);
700           }
701         break;
702
703       default:
704         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
705         break;
706     }
707 }
708
709 static void
710 gtk_entry_completion_get_property (GObject    *object,
711                                    guint       prop_id,
712                                    GValue     *value,
713                                    GParamSpec *pspec)
714 {
715   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
716
717   switch (prop_id)
718     {
719       case PROP_MODEL:
720         g_value_set_object (value,
721                             gtk_entry_completion_get_model (completion));
722         break;
723
724       case PROP_MINIMUM_KEY_LENGTH:
725         g_value_set_int (value, gtk_entry_completion_get_minimum_key_length (completion));
726         break;
727
728       case PROP_TEXT_COLUMN:
729         g_value_set_int (value, gtk_entry_completion_get_text_column (completion));
730         break;
731
732       case PROP_INLINE_COMPLETION:
733         g_value_set_boolean (value, gtk_entry_completion_get_inline_completion (completion));
734         break;
735
736       case PROP_POPUP_COMPLETION:
737         g_value_set_boolean (value, gtk_entry_completion_get_popup_completion (completion));
738         break;
739
740       case PROP_POPUP_SET_WIDTH:
741         g_value_set_boolean (value, gtk_entry_completion_get_popup_set_width (completion));
742         break;
743
744       case PROP_POPUP_SINGLE_MATCH:
745         g_value_set_boolean (value, gtk_entry_completion_get_popup_single_match (completion));
746         break;
747
748       case PROP_INLINE_SELECTION:
749         g_value_set_boolean (value, gtk_entry_completion_get_inline_selection (completion));
750         break;
751
752       case PROP_CELL_AREA:
753         g_value_set_object (value, completion->priv->cell_area);
754         break;
755
756       default:
757         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
758         break;
759     }
760 }
761
762 static void
763 gtk_entry_completion_finalize (GObject *object)
764 {
765   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
766   GtkEntryCompletionPrivate *priv = completion->priv;
767
768   g_free (priv->case_normalized_key);
769   g_free (priv->completion_prefix);
770
771   if (priv->match_notify)
772     (* priv->match_notify) (priv->match_data);
773
774   G_OBJECT_CLASS (gtk_entry_completion_parent_class)->finalize (object);
775 }
776
777 static void
778 gtk_entry_completion_dispose (GObject *object)
779 {
780   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
781   GtkEntryCompletionPrivate *priv = completion->priv;
782
783   if (priv->tree_view)
784     {
785       gtk_widget_destroy (priv->tree_view);
786       priv->tree_view = NULL;
787     }
788
789   if (priv->entry)
790     gtk_entry_set_completion (GTK_ENTRY (priv->entry), NULL);
791
792   if (priv->actions)
793     {
794       g_object_unref (priv->actions);
795       priv->actions = NULL;
796     }
797
798   if (priv->action_view)
799     {
800       g_object_unref (priv->action_view);
801       priv->action_view = NULL;
802     }
803
804   if (priv->popup_window)
805     {
806       gtk_widget_destroy (priv->popup_window);
807       priv->popup_window = NULL;
808     }
809
810   if (priv->cell_area)
811     {
812       g_object_unref (priv->cell_area);
813       priv->cell_area = NULL;
814     }
815
816   G_OBJECT_CLASS (gtk_entry_completion_parent_class)->dispose (object);
817 }
818
819 /* implement cell layout interface (only need to return the underlying cell area) */
820 static GtkCellArea*
821 gtk_entry_completion_get_area (GtkCellLayout *cell_layout)
822 {
823   GtkEntryCompletionPrivate *priv;
824
825   priv = GTK_ENTRY_COMPLETION (cell_layout)->priv;
826
827   if (G_UNLIKELY (!priv->cell_area))
828     {
829       priv->cell_area = gtk_cell_area_box_new ();
830       g_object_ref_sink (priv->cell_area);
831     }
832
833   return priv->cell_area;
834 }
835
836 /* all those callbacks */
837 static gboolean
838 gtk_entry_completion_default_completion_func (GtkEntryCompletion *completion,
839                                               const gchar        *key,
840                                               GtkTreeIter        *iter,
841                                               gpointer            user_data)
842 {
843   gchar *item = NULL;
844   gchar *normalized_string;
845   gchar *case_normalized_string;
846
847   gboolean ret = FALSE;
848
849   GtkTreeModel *model;
850
851   model = gtk_tree_model_filter_get_model (completion->priv->filter_model);
852
853   g_return_val_if_fail (gtk_tree_model_get_column_type (model, completion->priv->text_column) == G_TYPE_STRING,
854                         FALSE);
855
856   gtk_tree_model_get (model, iter,
857                       completion->priv->text_column, &item,
858                       -1);
859
860   if (item != NULL)
861     {
862       normalized_string = g_utf8_normalize (item, -1, G_NORMALIZE_ALL);
863
864       if (normalized_string != NULL)
865         {
866           case_normalized_string = g_utf8_casefold (normalized_string, -1);
867
868           if (!strncmp (key, case_normalized_string, strlen (key)))
869             ret = TRUE;
870
871           g_free (case_normalized_string);
872         }
873       g_free (normalized_string);
874     }
875   g_free (item);
876
877   return ret;
878 }
879
880 static gboolean
881 gtk_entry_completion_visible_func (GtkTreeModel *model,
882                                    GtkTreeIter  *iter,
883                                    gpointer      data)
884 {
885   gboolean ret = FALSE;
886
887   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
888
889   if (!completion->priv->case_normalized_key)
890     return ret;
891
892   if (completion->priv->match_func)
893     ret = (* completion->priv->match_func) (completion,
894                                             completion->priv->case_normalized_key,
895                                             iter,
896                                             completion->priv->match_data);
897   else if (completion->priv->text_column >= 0)
898     ret = gtk_entry_completion_default_completion_func (completion,
899                                                         completion->priv->case_normalized_key,
900                                                         iter,
901                                                         NULL);
902
903   return ret;
904 }
905
906 static gboolean
907 gtk_entry_completion_popup_key_event (GtkWidget   *widget,
908                                       GdkEventKey *event,
909                                       gpointer     user_data)
910 {
911   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
912
913   if (!gtk_widget_get_mapped (completion->priv->popup_window))
914     return FALSE;
915
916   /* propagate event to the entry */
917   gtk_widget_event (completion->priv->entry, (GdkEvent *)event);
918
919   return TRUE;
920 }
921
922 static gboolean
923 gtk_entry_completion_popup_button_press (GtkWidget      *widget,
924                                          GdkEventButton *event,
925                                          gpointer        user_data)
926 {
927   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
928
929   if (!gtk_widget_get_mapped (completion->priv->popup_window))
930     return FALSE;
931
932   /* if we come here, it's usually time to popdown */
933   _gtk_entry_completion_popdown (completion);
934
935   return TRUE;
936 }
937
938 static gboolean
939 gtk_entry_completion_list_button_press (GtkWidget      *widget,
940                                         GdkEventButton *event,
941                                         gpointer        user_data)
942 {
943   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
944   GtkTreePath *path = NULL;
945
946   if (!gtk_widget_get_mapped (completion->priv->popup_window))
947     return FALSE;
948
949   if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
950                                      event->x, event->y,
951                                      &path, NULL, NULL, NULL))
952     {
953       GtkTreeIter iter;
954       gboolean entry_set;
955       GtkTreeModel *model;
956       GtkTreeIter child_iter;
957
958       gtk_tree_model_get_iter (GTK_TREE_MODEL (completion->priv->filter_model),
959                                &iter, path);
960       gtk_tree_path_free (path);
961       gtk_tree_model_filter_convert_iter_to_child_iter (completion->priv->filter_model,
962                                                         &child_iter,
963                                                         &iter);
964       model = gtk_tree_model_filter_get_model (completion->priv->filter_model);
965
966       g_signal_handler_block (completion->priv->entry,
967                               completion->priv->changed_id);
968       g_signal_emit (completion, entry_completion_signals[MATCH_SELECTED],
969                      0, model, &child_iter, &entry_set);
970       g_signal_handler_unblock (completion->priv->entry,
971                                 completion->priv->changed_id);
972
973       _gtk_entry_completion_popdown (completion);
974
975       return TRUE;
976     }
977
978   return FALSE;
979 }
980
981 static gboolean
982 gtk_entry_completion_action_button_press (GtkWidget      *widget,
983                                           GdkEventButton *event,
984                                           gpointer        user_data)
985 {
986   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
987   GtkTreePath *path = NULL;
988
989   if (!gtk_widget_get_mapped (completion->priv->popup_window))
990     return FALSE;
991
992   gtk_entry_reset_im_context (GTK_ENTRY (completion->priv->entry));
993
994   if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
995                                      event->x, event->y,
996                                      &path, NULL, NULL, NULL))
997     {
998       g_signal_emit (completion, entry_completion_signals[ACTION_ACTIVATED],
999                      0, gtk_tree_path_get_indices (path)[0]);
1000       gtk_tree_path_free (path);
1001
1002       _gtk_entry_completion_popdown (completion);
1003       return TRUE;
1004     }
1005
1006   return FALSE;
1007 }
1008
1009 static void
1010 gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
1011                                        GtkCellRenderer   *cell,
1012                                        GtkTreeModel      *model,
1013                                        GtkTreeIter       *iter,
1014                                        gpointer           data)
1015 {
1016   gchar *string = NULL;
1017   gboolean markup;
1018
1019   gtk_tree_model_get (model, iter,
1020                       0, &string,
1021                       1, &markup,
1022                       -1);
1023
1024   if (!string)
1025     return;
1026
1027   if (markup)
1028     g_object_set (cell,
1029                   "text", NULL,
1030                   "markup", string,
1031                   NULL);
1032   else
1033     g_object_set (cell,
1034                   "markup", NULL,
1035                   "text", string,
1036                   NULL);
1037
1038   g_free (string);
1039 }
1040
1041 static void
1042 gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
1043                                         gpointer          data)
1044 {
1045   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1046
1047   if (completion->priv->first_sel_changed)
1048     {
1049       completion->priv->first_sel_changed = FALSE;
1050       if (gtk_widget_is_focus (completion->priv->tree_view))
1051         gtk_tree_selection_unselect_all (selection);
1052     }
1053 }
1054
1055 /* public API */
1056
1057 /**
1058  * gtk_entry_completion_new:
1059  *
1060  * Creates a new #GtkEntryCompletion object.
1061  *
1062  * Return value: A newly created #GtkEntryCompletion object
1063  *
1064  * Since: 2.4
1065  */
1066 GtkEntryCompletion *
1067 gtk_entry_completion_new (void)
1068 {
1069   GtkEntryCompletion *completion;
1070
1071   completion = g_object_new (GTK_TYPE_ENTRY_COMPLETION, NULL);
1072
1073   return completion;
1074 }
1075
1076 /**
1077  * gtk_entry_completion_new_with_area:
1078  * @area: the #GtkCellArea used to layout cells
1079  *
1080  * Creates a new #GtkEntryCompletion object using the
1081  * specified @area to layout cells in the underlying
1082  * #GtkTreeViewColumn for the drop-down menu.
1083  *
1084  * Return value: A newly created #GtkEntryCompletion object
1085  *
1086  * Since: 3.0
1087  */
1088 GtkEntryCompletion *
1089 gtk_entry_completion_new_with_area (GtkCellArea *area)
1090 {
1091   GtkEntryCompletion *completion;
1092
1093   completion = g_object_new (GTK_TYPE_ENTRY_COMPLETION, "cell-area", area, NULL);
1094
1095   return completion;
1096 }
1097
1098 /**
1099  * gtk_entry_completion_get_entry:
1100  * @completion: a #GtkEntryCompletion
1101  *
1102  * Gets the entry @completion has been attached to.
1103  *
1104  * Return value: (transfer none): The entry @completion has been attached to
1105  *
1106  * Since: 2.4
1107  */
1108 GtkWidget *
1109 gtk_entry_completion_get_entry (GtkEntryCompletion *completion)
1110 {
1111   g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
1112
1113   return completion->priv->entry;
1114 }
1115
1116 /**
1117  * gtk_entry_completion_set_model:
1118  * @completion: a #GtkEntryCompletion
1119  * @model: (allow-none): the #GtkTreeModel
1120  *
1121  * Sets the model for a #GtkEntryCompletion. If @completion already has
1122  * a model set, it will remove it before setting the new model.
1123  * If model is %NULL, then it will unset the model.
1124  *
1125  * Since: 2.4
1126  */
1127 void
1128 gtk_entry_completion_set_model (GtkEntryCompletion *completion,
1129                                 GtkTreeModel       *model)
1130 {
1131   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1132   g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
1133
1134   if (!model)
1135     {
1136       gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
1137                                NULL);
1138       _gtk_entry_completion_popdown (completion);
1139       completion->priv->filter_model = NULL;
1140       return;
1141     }
1142
1143   /* code will unref the old filter model (if any) */
1144   completion->priv->filter_model =
1145     GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
1146   gtk_tree_model_filter_set_visible_func (completion->priv->filter_model,
1147                                           gtk_entry_completion_visible_func,
1148                                           completion,
1149                                           NULL);
1150
1151   gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
1152                            GTK_TREE_MODEL (completion->priv->filter_model));
1153   g_object_unref (completion->priv->filter_model);
1154
1155   g_object_notify (G_OBJECT (completion), "model");
1156
1157   if (gtk_widget_get_visible (completion->priv->popup_window))
1158     _gtk_entry_completion_resize_popup (completion);
1159 }
1160
1161 /**
1162  * gtk_entry_completion_get_model:
1163  * @completion: a #GtkEntryCompletion
1164  *
1165  * Returns the model the #GtkEntryCompletion is using as data source.
1166  * Returns %NULL if the model is unset.
1167  *
1168  * Return value: (transfer none): A #GtkTreeModel, or %NULL if none
1169  *     is currently being used
1170  *
1171  * Since: 2.4
1172  */
1173 GtkTreeModel *
1174 gtk_entry_completion_get_model (GtkEntryCompletion *completion)
1175 {
1176   g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
1177
1178   if (!completion->priv->filter_model)
1179     return NULL;
1180
1181   return gtk_tree_model_filter_get_model (completion->priv->filter_model);
1182 }
1183
1184 /**
1185  * gtk_entry_completion_set_match_func:
1186  * @completion: a #GtkEntryCompletion
1187  * @func: the #GtkEntryCompletionMatchFunc to use
1188  * @func_data: user data for @func
1189  * @func_notify: destroy notify for @func_data.
1190  *
1191  * Sets the match function for @completion to be @func. The match function
1192  * is used to determine if a row should or should not be in the completion
1193  * list.
1194  *
1195  * Since: 2.4
1196  */
1197 void
1198 gtk_entry_completion_set_match_func (GtkEntryCompletion          *completion,
1199                                      GtkEntryCompletionMatchFunc  func,
1200                                      gpointer                     func_data,
1201                                      GDestroyNotify               func_notify)
1202 {
1203   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1204
1205   if (completion->priv->match_notify)
1206     (* completion->priv->match_notify) (completion->priv->match_data);
1207
1208   completion->priv->match_func = func;
1209   completion->priv->match_data = func_data;
1210   completion->priv->match_notify = func_notify;
1211 }
1212
1213 /**
1214  * gtk_entry_completion_set_minimum_key_length:
1215  * @completion: a #GtkEntryCompletion
1216  * @length: the minimum length of the key in order to start completing
1217  *
1218  * Requires the length of the search key for @completion to be at least
1219  * @length. This is useful for long lists, where completing using a small
1220  * key takes a lot of time and will come up with meaningless results anyway
1221  * (ie, a too large dataset).
1222  *
1223  * Since: 2.4
1224  */
1225 void
1226 gtk_entry_completion_set_minimum_key_length (GtkEntryCompletion *completion,
1227                                              gint                length)
1228 {
1229   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1230   g_return_if_fail (length >= 0);
1231
1232   if (completion->priv->minimum_key_length != length)
1233     {
1234       completion->priv->minimum_key_length = length;
1235
1236       g_object_notify (G_OBJECT (completion), "minimum-key-length");
1237     }
1238 }
1239
1240 /**
1241  * gtk_entry_completion_get_minimum_key_length:
1242  * @completion: a #GtkEntryCompletion
1243  *
1244  * Returns the minimum key length as set for @completion.
1245  *
1246  * Return value: The currently used minimum key length
1247  *
1248  * Since: 2.4
1249  */
1250 gint
1251 gtk_entry_completion_get_minimum_key_length (GtkEntryCompletion *completion)
1252 {
1253   g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), 0);
1254
1255   return completion->priv->minimum_key_length;
1256 }
1257
1258 /**
1259  * gtk_entry_completion_complete:
1260  * @completion: a #GtkEntryCompletion
1261  *
1262  * Requests a completion operation, or in other words a refiltering of the
1263  * current list with completions, using the current key. The completion list
1264  * view will be updated accordingly.
1265  *
1266  * Since: 2.4
1267  */
1268 void
1269 gtk_entry_completion_complete (GtkEntryCompletion *completion)
1270 {
1271   gchar *tmp;
1272
1273   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1274   g_return_if_fail (GTK_IS_ENTRY (completion->priv->entry));
1275
1276   if (!completion->priv->filter_model)
1277     return;
1278
1279   g_free (completion->priv->case_normalized_key);
1280
1281   tmp = g_utf8_normalize (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)),
1282                           -1, G_NORMALIZE_ALL);
1283   completion->priv->case_normalized_key = g_utf8_casefold (tmp, -1);
1284   g_free (tmp);
1285
1286   gtk_tree_model_filter_refilter (completion->priv->filter_model);
1287
1288   if (gtk_widget_get_visible (completion->priv->popup_window))
1289     _gtk_entry_completion_resize_popup (completion);
1290 }
1291
1292 static void
1293 gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
1294                                     gint                index,
1295                                     const gchar        *string,
1296                                     gboolean            markup)
1297 {
1298   GtkTreeIter iter;
1299
1300   gtk_list_store_insert (completion->priv->actions, &iter, index);
1301   gtk_list_store_set (completion->priv->actions, &iter,
1302                       0, string,
1303                       1, markup,
1304                       -1);
1305
1306   if (!gtk_widget_get_parent (completion->priv->action_view))
1307     {
1308       GtkTreePath *path = gtk_tree_path_new_from_indices (0, -1);
1309
1310       gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
1311                                 path, NULL, FALSE);
1312       gtk_tree_path_free (path);
1313
1314       gtk_box_pack_start (GTK_BOX (completion->priv->vbox),
1315                           completion->priv->action_view, FALSE, FALSE, 0);
1316       gtk_widget_show (completion->priv->action_view);
1317     }
1318 }
1319
1320 /**
1321  * gtk_entry_completion_insert_action_text:
1322  * @completion: a #GtkEntryCompletion
1323  * @index_: the index of the item to insert
1324  * @text: text of the item to insert
1325  *
1326  * Inserts an action in @completion's action item list at position @index_
1327  * with text @text. If you want the action item to have markup, use
1328  * gtk_entry_completion_insert_action_markup().
1329  *
1330  * Since: 2.4
1331  */
1332 void
1333 gtk_entry_completion_insert_action_text (GtkEntryCompletion *completion,
1334                                          gint                index_,
1335                                          const gchar        *text)
1336 {
1337   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1338   g_return_if_fail (text != NULL);
1339
1340   gtk_entry_completion_insert_action (completion, index_, text, FALSE);
1341 }
1342
1343 /**
1344  * gtk_entry_completion_insert_action_markup:
1345  * @completion: a #GtkEntryCompletion
1346  * @index_: the index of the item to insert
1347  * @markup: markup of the item to insert
1348  *
1349  * Inserts an action in @completion's action item list at position @index_
1350  * with markup @markup.
1351  *
1352  * Since: 2.4
1353  */
1354 void
1355 gtk_entry_completion_insert_action_markup (GtkEntryCompletion *completion,
1356                                            gint                index_,
1357                                            const gchar        *markup)
1358 {
1359   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1360   g_return_if_fail (markup != NULL);
1361
1362   gtk_entry_completion_insert_action (completion, index_, markup, TRUE);
1363 }
1364
1365 /**
1366  * gtk_entry_completion_delete_action:
1367  * @completion: a #GtkEntryCompletion
1368  * @index_: the index of the item to delete
1369  *
1370  * Deletes the action at @index_ from @completion's action list.
1371  *
1372  * Since: 2.4
1373  */
1374 void
1375 gtk_entry_completion_delete_action (GtkEntryCompletion *completion,
1376                                     gint                index_)
1377 {
1378   GtkTreeIter iter;
1379
1380   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1381   g_return_if_fail (index_ >= 0);
1382
1383   gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (completion->priv->actions),
1384                                  &iter, NULL, index_);
1385   gtk_list_store_remove (completion->priv->actions, &iter);
1386 }
1387
1388 /**
1389  * gtk_entry_completion_set_text_column:
1390  * @completion: a #GtkEntryCompletion
1391  * @column: the column in the model of @completion to get strings from
1392  *
1393  * Convenience function for setting up the most used case of this code: a
1394  * completion list with just strings. This function will set up @completion
1395  * to have a list displaying all (and just) strings in the completion list,
1396  * and to get those strings from @column in the model of @completion.
1397  *
1398  * This functions creates and adds a #GtkCellRendererText for the selected
1399  * column. If you need to set the text column, but don't want the cell
1400  * renderer, use g_object_set() to set the #GtkEntryCompletion:text-column
1401  * property directly.
1402  *
1403  * Since: 2.4
1404  */
1405 void
1406 gtk_entry_completion_set_text_column (GtkEntryCompletion *completion,
1407                                       gint                column)
1408 {
1409   GtkCellRenderer *cell;
1410
1411   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1412   g_return_if_fail (column >= 0);
1413
1414   completion->priv->text_column = column;
1415
1416   cell = gtk_cell_renderer_text_new ();
1417   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion),
1418                               cell, TRUE);
1419   gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (completion),
1420                                  cell,
1421                                  "text", column);
1422
1423   g_object_notify (G_OBJECT (completion), "text-column");
1424 }
1425
1426 /**
1427  * gtk_entry_completion_get_text_column:
1428  * @completion: a #GtkEntryCompletion
1429  *
1430  * Returns the column in the model of @completion to get strings from.
1431  *
1432  * Return value: the column containing the strings
1433  *
1434  * Since: 2.6
1435  */
1436 gint
1437 gtk_entry_completion_get_text_column (GtkEntryCompletion *completion)
1438 {
1439   g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), -1);
1440
1441   return completion->priv->text_column;
1442 }
1443
1444 /* private */
1445
1446 static gboolean
1447 gtk_entry_completion_list_enter_notify (GtkWidget        *widget,
1448                                         GdkEventCrossing *event,
1449                                         gpointer          data)
1450 {
1451   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1452
1453   return completion->priv->ignore_enter;
1454 }
1455
1456 static gboolean
1457 gtk_entry_completion_list_motion_notify (GtkWidget      *widget,
1458                                          GdkEventMotion *event,
1459                                          gpointer        data)
1460 {
1461   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1462
1463   completion->priv->ignore_enter = FALSE;
1464
1465   return FALSE;
1466 }
1467
1468
1469 /* some nasty size requisition */
1470 gboolean
1471 _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
1472 {
1473   GtkAllocation allocation;
1474   gint x, y;
1475   gint matches, actions, items, height;
1476   GdkScreen *screen;
1477   gint monitor_num;
1478   gint vertical_separator;
1479   GdkRectangle monitor;
1480   GdkWindow *window;
1481   GtkRequisition popup_req;
1482   GtkRequisition entry_req;
1483   GtkTreePath *path;
1484   gboolean above;
1485   gint width;
1486   GtkTreeViewColumn *action_column;
1487   gint action_height;
1488
1489   window = gtk_widget_get_window (completion->priv->entry);
1490
1491   if (!window)
1492     return FALSE;
1493
1494   if (!completion->priv->filter_model)
1495     return FALSE;
1496
1497   gtk_widget_get_allocation (completion->priv->entry, &allocation);
1498   gtk_widget_get_preferred_size (completion->priv->entry,
1499                                  &entry_req, NULL);
1500
1501   gdk_window_get_origin (window, &x, &y);
1502   x += allocation.x;
1503   y += allocation.y + (allocation.height - entry_req.height) / 2;
1504
1505   matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
1506   actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
1507   action_column  = gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0);
1508
1509   gtk_tree_view_column_cell_get_size (completion->priv->column, NULL,
1510                                       NULL, NULL, NULL, &height);
1511   gtk_tree_view_column_cell_get_size (action_column, NULL,
1512                                       NULL, NULL, NULL, &action_height);
1513
1514   gtk_widget_style_get (GTK_WIDGET (completion->priv->tree_view),
1515                         "vertical-separator", &vertical_separator,
1516                         NULL);
1517
1518   height += vertical_separator;
1519
1520   gtk_widget_realize (completion->priv->tree_view);
1521
1522   screen = gtk_widget_get_screen (GTK_WIDGET (completion->priv->entry));
1523   monitor_num = gdk_screen_get_monitor_at_window (screen, window);
1524   gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
1525
1526   if (height == 0)
1527     items = 0;
1528   else if (y > monitor.height / 2)
1529     items = MIN (matches, (((monitor.y + y) - (actions * action_height)) / height) - 1);
1530   else
1531     items = MIN (matches, (((monitor.height - y) - (actions * action_height)) / height) - 1);
1532
1533   if (items <= 0)
1534     gtk_widget_hide (completion->priv->scrolled_window);
1535   else
1536     gtk_widget_show (completion->priv->scrolled_window);
1537
1538   if (completion->priv->popup_set_width)
1539     width = MIN (allocation.width, monitor.width);
1540   else
1541     width = -1;
1542
1543   gtk_tree_view_columns_autosize (GTK_TREE_VIEW (completion->priv->tree_view));
1544   gtk_scrolled_window_set_min_content_width (GTK_SCROLLED_WINDOW (completion->priv->scrolled_window), width);
1545   gtk_widget_set_size_request (completion->priv->popup_window, width, -1);
1546   gtk_scrolled_window_set_min_content_height (GTK_SCROLLED_WINDOW (completion->priv->scrolled_window), items * height);
1547
1548   if (actions)
1549     gtk_widget_show (completion->priv->action_view);
1550   else
1551     gtk_widget_hide (completion->priv->action_view);
1552
1553   gtk_widget_get_preferred_size (completion->priv->popup_window,
1554                                  &popup_req, NULL);
1555
1556   if (x < monitor.x)
1557     x = monitor.x;
1558   else if (x + popup_req.width > monitor.x + monitor.width)
1559     x = monitor.x + monitor.width - popup_req.width;
1560
1561   if (y + entry_req.height + popup_req.height <= monitor.y + monitor.height ||
1562       y - monitor.y < (monitor.y + monitor.height) - (y + entry_req.height))
1563     {
1564       y += entry_req.height;
1565       above = FALSE;
1566     }
1567   else
1568     {
1569       y -= popup_req.height;
1570       above = TRUE;
1571     }
1572
1573   if (matches > 0)
1574     {
1575       path = gtk_tree_path_new_from_indices (above ? matches - 1 : 0, -1);
1576       gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (completion->priv->tree_view), path,
1577                                     NULL, FALSE, 0.0, 0.0);
1578       gtk_tree_path_free (path);
1579     }
1580
1581   gtk_window_move (GTK_WINDOW (completion->priv->popup_window), x, y);
1582
1583   return above;
1584 }
1585
1586 static void
1587 gtk_entry_completion_popup (GtkEntryCompletion *completion)
1588 {
1589   GtkTreeViewColumn *column;
1590   GtkStyleContext *context;
1591   GdkRGBA color;
1592   GList *renderers;
1593   GtkWidget *toplevel;
1594
1595   if (gtk_widget_get_mapped (completion->priv->popup_window))
1596     return;
1597
1598   if (!gtk_widget_get_mapped (completion->priv->entry))
1599     return;
1600
1601   if (!gtk_widget_has_focus (completion->priv->entry))
1602     return;
1603
1604   if (completion->priv->has_grab)
1605     return;
1606
1607   completion->priv->ignore_enter = TRUE;
1608
1609   column = gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0);
1610   renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
1611
1612   context = gtk_widget_get_style_context (completion->priv->tree_view);
1613   gtk_style_context_get_background_color (context, 0, &color);
1614
1615   g_object_set (GTK_CELL_RENDERER (renderers->data),
1616                 "cell-background-rgba", &color,
1617                 NULL);
1618   g_list_free (renderers);
1619
1620   gtk_widget_show_all (completion->priv->vbox);
1621
1622   /* default on no match */
1623   completion->priv->current_selected = -1;
1624
1625   _gtk_entry_completion_resize_popup (completion);
1626
1627   toplevel = gtk_widget_get_toplevel (completion->priv->entry);
1628   if (GTK_IS_WINDOW (toplevel))
1629     gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1630                                  GTK_WINDOW (completion->priv->popup_window));
1631
1632   /* prevent the first row being focused */
1633   gtk_widget_grab_focus (completion->priv->tree_view);
1634
1635   gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
1636   gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
1637
1638   gtk_window_set_screen (GTK_WINDOW (completion->priv->popup_window),
1639                          gtk_widget_get_screen (completion->priv->entry));
1640
1641   gtk_widget_show (completion->priv->popup_window);
1642
1643   gtk_device_grab_add (completion->priv->popup_window, completion->priv->device, TRUE);
1644   gdk_device_grab (completion->priv->device, gtk_widget_get_window (completion->priv->popup_window),
1645                    GDK_OWNERSHIP_WINDOW, TRUE,
1646                    GDK_BUTTON_PRESS_MASK |
1647                    GDK_BUTTON_RELEASE_MASK |
1648                    GDK_POINTER_MOTION_MASK,
1649                    NULL, GDK_CURRENT_TIME);
1650
1651   completion->priv->has_grab = TRUE;
1652 }
1653
1654 void
1655 _gtk_entry_completion_popdown (GtkEntryCompletion *completion)
1656 {
1657   if (!gtk_widget_get_mapped (completion->priv->popup_window))
1658     return;
1659
1660   completion->priv->ignore_enter = FALSE;
1661
1662   if (completion->priv->has_grab)
1663     {
1664       gdk_device_ungrab (completion->priv->device, GDK_CURRENT_TIME);
1665       gtk_device_grab_remove (completion->priv->popup_window,
1666                               completion->priv->device);
1667       completion->priv->has_grab = FALSE;
1668     }
1669
1670   gtk_widget_hide (completion->priv->popup_window);
1671 }
1672
1673 static gboolean
1674 gtk_entry_completion_match_selected (GtkEntryCompletion *completion,
1675                                      GtkTreeModel       *model,
1676                                      GtkTreeIter        *iter)
1677 {
1678   gchar *str = NULL;
1679
1680   gtk_tree_model_get (model, iter, completion->priv->text_column, &str, -1);
1681   gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), str ? str : "");
1682
1683   /* move cursor to the end */
1684   gtk_editable_set_position (GTK_EDITABLE (completion->priv->entry), -1);
1685
1686   g_free (str);
1687
1688   return TRUE;
1689 }
1690
1691 static gboolean
1692 gtk_entry_completion_cursor_on_match (GtkEntryCompletion *completion,
1693                                       GtkTreeModel       *model,
1694                                       GtkTreeIter        *iter)
1695 {
1696   gtk_entry_completion_insert_completion (completion, model, iter);
1697
1698   return TRUE;
1699 }
1700
1701 /**
1702  * gtk_entry_completion_compute_prefix:
1703  * @completion: the entry completion
1704  * @key: The text to complete for
1705  *
1706  * Computes the common prefix that is shared by all rows in @completion
1707  * that start with @key. If no row matches @key, %NULL will be returned.
1708  * Note that a text column must have been set for this function to work,
1709  * see gtk_entry_completion_set_text_column() for details. 
1710  *
1711  * Returns: (transfer full): The common prefix all rows starting with @key
1712  *   or %NULL if no row matches @key.
1713  *
1714  * Since: 3.4
1715  **/
1716 gchar *
1717 gtk_entry_completion_compute_prefix (GtkEntryCompletion *completion,
1718                                      const char         *key)
1719 {
1720   GtkTreeIter iter;
1721   gchar *prefix = NULL;
1722   gboolean valid;
1723
1724   if (completion->priv->text_column < 0)
1725     return NULL;
1726
1727   valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (completion->priv->filter_model),
1728                                          &iter);
1729
1730   while (valid)
1731     {
1732       gchar *text;
1733
1734       gtk_tree_model_get (GTK_TREE_MODEL (completion->priv->filter_model),
1735                           &iter, completion->priv->text_column, &text,
1736                           -1);
1737
1738       if (text && g_str_has_prefix (text, key))
1739         {
1740           if (!prefix)
1741             prefix = g_strdup (text);
1742           else
1743             {
1744               gchar *p = prefix;
1745               gchar *q = text;
1746
1747               while (*p && *p == *q)
1748                 {
1749                   p++;
1750                   q++;
1751                 }
1752
1753               *p = '\0';
1754
1755               if (p > prefix)
1756                 {
1757                   /* strip a partial multibyte character */
1758                   q = g_utf8_find_prev_char (prefix, p);
1759                   switch (g_utf8_get_char_validated (q, p - q))
1760                     {
1761                     case (gunichar)-2:
1762                     case (gunichar)-1:
1763                       *q = 0;
1764                     default: ;
1765                     }
1766                 }
1767             }
1768         }
1769
1770       g_free (text);
1771       valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (completion->priv->filter_model),
1772                                         &iter);
1773     }
1774
1775   return prefix;
1776 }
1777
1778
1779 static gboolean
1780 gtk_entry_completion_real_insert_prefix (GtkEntryCompletion *completion,
1781                                          const gchar        *prefix)
1782 {
1783   if (prefix)
1784     {
1785       gint key_len;
1786       gint prefix_len;
1787       const gchar *key;
1788
1789       prefix_len = g_utf8_strlen (prefix, -1);
1790
1791       key = gtk_entry_get_text (GTK_ENTRY (completion->priv->entry));
1792       key_len = g_utf8_strlen (key, -1);
1793
1794       if (prefix_len > key_len)
1795         {
1796           gint pos = prefix_len;
1797
1798           gtk_editable_insert_text (GTK_EDITABLE (completion->priv->entry),
1799                                     prefix + strlen (key), -1, &pos);
1800           gtk_editable_select_region (GTK_EDITABLE (completion->priv->entry),
1801                                       key_len, prefix_len);
1802
1803           completion->priv->has_completion = TRUE;
1804         }
1805     }
1806
1807   return TRUE;
1808 }
1809
1810 /**
1811  * gtk_entry_completion_get_completion_prefix:
1812  * @completion: a #GtkEntryCompletion
1813  *
1814  * Get the original text entered by the user that triggered
1815  * the completion or %NULL if there's no completion ongoing.
1816  *
1817  * Returns: the prefix for the current completion
1818  *
1819  * Since: 2.12
1820  */
1821 const gchar*
1822 gtk_entry_completion_get_completion_prefix (GtkEntryCompletion *completion)
1823 {
1824   g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
1825
1826   return completion->priv->completion_prefix;
1827 }
1828
1829 static void
1830 gtk_entry_completion_insert_completion_text (GtkEntryCompletion *completion,
1831                                              const gchar *text)
1832 {
1833   GtkEntryCompletionPrivate *priv = completion->priv;
1834   gint len;
1835
1836   priv = completion->priv;
1837
1838   if (priv->changed_id > 0)
1839     g_signal_handler_block (priv->entry, priv->changed_id);
1840
1841   if (priv->insert_text_id > 0)
1842     g_signal_handler_block (priv->entry, priv->insert_text_id);
1843
1844   gtk_entry_set_text (GTK_ENTRY (priv->entry), text);
1845
1846   len = strlen (priv->completion_prefix);
1847   gtk_editable_select_region (GTK_EDITABLE (priv->entry), len, -1);
1848
1849   if (priv->changed_id > 0)
1850     g_signal_handler_unblock (priv->entry, priv->changed_id);
1851
1852   if (priv->insert_text_id > 0)
1853     g_signal_handler_unblock (priv->entry, priv->insert_text_id);
1854 }
1855
1856 static gboolean
1857 gtk_entry_completion_insert_completion (GtkEntryCompletion *completion,
1858                                         GtkTreeModel       *model,
1859                                         GtkTreeIter        *iter)
1860 {
1861   gchar *str = NULL;
1862
1863   if (completion->priv->text_column < 0)
1864     return FALSE;
1865
1866   gtk_tree_model_get (model, iter,
1867                       completion->priv->text_column, &str,
1868                       -1);
1869
1870   gtk_entry_completion_insert_completion_text (completion, str);
1871
1872   g_free (str);
1873
1874   return TRUE;
1875 }
1876
1877 /**
1878  * gtk_entry_completion_insert_prefix:
1879  * @completion: a #GtkEntryCompletion
1880  *
1881  * Requests a prefix insertion.
1882  *
1883  * Since: 2.6
1884  */
1885 void
1886 gtk_entry_completion_insert_prefix (GtkEntryCompletion *completion)
1887 {
1888   gboolean done;
1889   gchar *prefix;
1890
1891   if (completion->priv->insert_text_id > 0)
1892     g_signal_handler_block (completion->priv->entry,
1893                             completion->priv->insert_text_id);
1894
1895   prefix = gtk_entry_completion_compute_prefix (completion,
1896                                                 gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)));
1897
1898   if (prefix)
1899     {
1900       g_signal_emit (completion, entry_completion_signals[INSERT_PREFIX],
1901                      0, prefix, &done);
1902       g_free (prefix);
1903     }
1904
1905   if (completion->priv->insert_text_id > 0)
1906     g_signal_handler_unblock (completion->priv->entry,
1907                               completion->priv->insert_text_id);
1908 }
1909
1910 /**
1911  * gtk_entry_completion_set_inline_completion:
1912  * @completion: a #GtkEntryCompletion
1913  * @inline_completion: %TRUE to do inline completion
1914  *
1915  * Sets whether the common prefix of the possible completions should
1916  * be automatically inserted in the entry.
1917  *
1918  * Since: 2.6
1919  */
1920 void
1921 gtk_entry_completion_set_inline_completion (GtkEntryCompletion *completion,
1922                                             gboolean            inline_completion)
1923 {
1924   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1925
1926   inline_completion = inline_completion != FALSE;
1927
1928   if (completion->priv->inline_completion != inline_completion)
1929     {
1930       completion->priv->inline_completion = inline_completion;
1931
1932       g_object_notify (G_OBJECT (completion), "inline-completion");
1933     }
1934 }
1935
1936 /**
1937  * gtk_entry_completion_get_inline_completion:
1938  * @completion: a #GtkEntryCompletion
1939  *
1940  * Returns whether the common prefix of the possible completions should
1941  * be automatically inserted in the entry.
1942  *
1943  * Return value: %TRUE if inline completion is turned on
1944  *
1945  * Since: 2.6
1946  */
1947 gboolean
1948 gtk_entry_completion_get_inline_completion (GtkEntryCompletion *completion)
1949 {
1950   g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), FALSE);
1951
1952   return completion->priv->inline_completion;
1953 }
1954
1955 /**
1956  * gtk_entry_completion_set_popup_completion:
1957  * @completion: a #GtkEntryCompletion
1958  * @popup_completion: %TRUE to do popup completion
1959  *
1960  * Sets whether the completions should be presented in a popup window.
1961  *
1962  * Since: 2.6
1963  */
1964 void
1965 gtk_entry_completion_set_popup_completion (GtkEntryCompletion *completion,
1966                                            gboolean            popup_completion)
1967 {
1968   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1969
1970   popup_completion = popup_completion != FALSE;
1971
1972   if (completion->priv->popup_completion != popup_completion)
1973     {
1974       completion->priv->popup_completion = popup_completion;
1975
1976       g_object_notify (G_OBJECT (completion), "popup-completion");
1977     }
1978 }
1979
1980
1981 /**
1982  * gtk_entry_completion_get_popup_completion:
1983  * @completion: a #GtkEntryCompletion
1984  *
1985  * Returns whether the completions should be presented in a popup window.
1986  *
1987  * Return value: %TRUE if popup completion is turned on
1988  *
1989  * Since: 2.6
1990  */
1991 gboolean
1992 gtk_entry_completion_get_popup_completion (GtkEntryCompletion *completion)
1993 {
1994   g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), TRUE);
1995
1996   return completion->priv->popup_completion;
1997 }
1998
1999 /**
2000  * gtk_entry_completion_set_popup_set_width:
2001  * @completion: a #GtkEntryCompletion
2002  * @popup_set_width: %TRUE to make the width of the popup the same as the entry
2003  *
2004  * Sets whether the completion popup window will be resized to be the same
2005  * width as the entry.
2006  *
2007  * Since: 2.8
2008  */
2009 void
2010 gtk_entry_completion_set_popup_set_width (GtkEntryCompletion *completion,
2011                                           gboolean            popup_set_width)
2012 {
2013   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
2014
2015   popup_set_width = popup_set_width != FALSE;
2016
2017   if (completion->priv->popup_set_width != popup_set_width)
2018     {
2019       completion->priv->popup_set_width = popup_set_width;
2020
2021       g_object_notify (G_OBJECT (completion), "popup-set-width");
2022     }
2023 }
2024
2025 /**
2026  * gtk_entry_completion_get_popup_set_width:
2027  * @completion: a #GtkEntryCompletion
2028  *
2029  * Returns whether the  completion popup window will be resized to the
2030  * width of the entry.
2031  *
2032  * Return value: %TRUE if the popup window will be resized to the width of
2033  *   the entry
2034  *
2035  * Since: 2.8
2036  */
2037 gboolean
2038 gtk_entry_completion_get_popup_set_width (GtkEntryCompletion *completion)
2039 {
2040   g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), TRUE);
2041
2042   return completion->priv->popup_set_width;
2043 }
2044
2045
2046 /**
2047  * gtk_entry_completion_set_popup_single_match:
2048  * @completion: a #GtkEntryCompletion
2049  * @popup_single_match: %TRUE if the popup should appear even for a single
2050  *     match
2051  *
2052  * Sets whether the completion popup window will appear even if there is
2053  * only a single match. You may want to set this to %FALSE if you
2054  * are using <link linkend="GtkEntryCompletion--inline-completion">inline
2055  * completion</link>.
2056  *
2057  * Since: 2.8
2058  */
2059 void
2060 gtk_entry_completion_set_popup_single_match (GtkEntryCompletion *completion,
2061                                              gboolean            popup_single_match)
2062 {
2063   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
2064
2065   popup_single_match = popup_single_match != FALSE;
2066
2067   if (completion->priv->popup_single_match != popup_single_match)
2068     {
2069       completion->priv->popup_single_match = popup_single_match;
2070
2071       g_object_notify (G_OBJECT (completion), "popup-single-match");
2072     }
2073 }
2074
2075 /**
2076  * gtk_entry_completion_get_popup_single_match:
2077  * @completion: a #GtkEntryCompletion
2078  *
2079  * Returns whether the completion popup window will appear even if there is
2080  * only a single match.
2081  *
2082  * Return value: %TRUE if the popup window will appear regardless of the
2083  *    number of matches
2084  *
2085  * Since: 2.8
2086  */
2087 gboolean
2088 gtk_entry_completion_get_popup_single_match (GtkEntryCompletion *completion)
2089 {
2090   g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), TRUE);
2091
2092   return completion->priv->popup_single_match;
2093 }
2094
2095 /**
2096  * gtk_entry_completion_set_inline_selection:
2097  * @completion: a #GtkEntryCompletion
2098  * @inline_selection: %TRUE to do inline selection
2099  *
2100  * Sets whether it is possible to cycle through the possible completions
2101  * inside the entry.
2102  *
2103  * Since: 2.12
2104  */
2105 void
2106 gtk_entry_completion_set_inline_selection (GtkEntryCompletion *completion,
2107                                            gboolean inline_selection)
2108 {
2109   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
2110
2111   inline_selection = inline_selection != FALSE;
2112
2113   if (completion->priv->inline_selection != inline_selection)
2114     {
2115       completion->priv->inline_selection = inline_selection;
2116
2117       g_object_notify (G_OBJECT (completion), "inline-selection");
2118     }
2119 }
2120
2121 /**
2122  * gtk_entry_completion_get_inline_selection:
2123  * @completion: a #GtkEntryCompletion
2124  *
2125  * Returns %TRUE if inline-selection mode is turned on.
2126  *
2127  * Returns: %TRUE if inline-selection mode is on
2128  *
2129  * Since: 2.12
2130  */
2131 gboolean
2132 gtk_entry_completion_get_inline_selection (GtkEntryCompletion *completion)
2133 {
2134   g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), FALSE);
2135
2136   return completion->priv->inline_selection;
2137 }
2138
2139
2140 static gint
2141 gtk_entry_completion_timeout (gpointer data)
2142 {
2143   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
2144
2145   completion->priv->completion_timeout = 0;
2146
2147   if (completion->priv->filter_model &&
2148       g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)), -1)
2149       >= completion->priv->minimum_key_length)
2150     {
2151       gint matches;
2152       gint actions;
2153       GtkTreeSelection *s;
2154       gboolean popup_single;
2155
2156       gtk_entry_completion_complete (completion);
2157       matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
2158       gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
2159
2160       s = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
2161
2162       gtk_tree_selection_unselect_all (s);
2163
2164       actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
2165
2166       g_object_get (completion, "popup-single-match", &popup_single, NULL);
2167       if ((matches > (popup_single ? 0: 1)) || actions > 0)
2168         {
2169           if (gtk_widget_get_visible (completion->priv->popup_window))
2170             _gtk_entry_completion_resize_popup (completion);
2171           else
2172             gtk_entry_completion_popup (completion);
2173         }
2174       else
2175         _gtk_entry_completion_popdown (completion);
2176     }
2177   else if (gtk_widget_get_visible (completion->priv->popup_window))
2178     _gtk_entry_completion_popdown (completion);
2179
2180   return FALSE;
2181 }
2182
2183 static inline gboolean
2184 keyval_is_cursor_move (guint keyval)
2185 {
2186   if (keyval == GDK_KEY_Up || keyval == GDK_KEY_KP_Up)
2187     return TRUE;
2188
2189   if (keyval == GDK_KEY_Down || keyval == GDK_KEY_KP_Down)
2190     return TRUE;
2191
2192   if (keyval == GDK_KEY_Page_Up)
2193     return TRUE;
2194
2195   if (keyval == GDK_KEY_Page_Down)
2196     return TRUE;
2197
2198   return FALSE;
2199 }
2200
2201 static gboolean
2202 gtk_entry_completion_key_press (GtkWidget   *widget,
2203                                 GdkEventKey *event,
2204                                 gpointer     user_data)
2205 {
2206   gint matches, actions = 0;
2207   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
2208
2209   if (!completion->priv->popup_completion)
2210     return FALSE;
2211
2212   if (event->keyval == GDK_KEY_Return ||
2213       event->keyval == GDK_KEY_KP_Enter ||
2214       event->keyval == GDK_KEY_ISO_Enter ||
2215       event->keyval == GDK_KEY_Escape)
2216     {
2217       if (completion && completion->priv->completion_timeout)
2218         {
2219           g_source_remove (completion->priv->completion_timeout);
2220           completion->priv->completion_timeout = 0;
2221         }
2222     }
2223
2224   if (!gtk_widget_get_mapped (completion->priv->popup_window))
2225     return FALSE;
2226
2227   matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
2228
2229   if (completion->priv->actions)
2230     actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
2231
2232   if (keyval_is_cursor_move (event->keyval))
2233     {
2234       GtkTreePath *path = NULL;
2235
2236       if (event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_KP_Up)
2237         {
2238           if (completion->priv->current_selected < 0)
2239             completion->priv->current_selected = matches + actions - 1;
2240           else
2241             completion->priv->current_selected--;
2242         }
2243       else if (event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_KP_Down)
2244         {
2245           if (completion->priv->current_selected < matches + actions - 1)
2246             completion->priv->current_selected++;
2247           else
2248             completion->priv->current_selected = -1;
2249         }
2250       else if (event->keyval == GDK_KEY_Page_Up)
2251         {
2252           if (completion->priv->current_selected < 0)
2253             completion->priv->current_selected = matches + actions - 1;
2254           else if (completion->priv->current_selected == 0)
2255             completion->priv->current_selected = -1;
2256           else if (completion->priv->current_selected < matches)
2257             {
2258               completion->priv->current_selected -= PAGE_STEP;
2259               if (completion->priv->current_selected < 0)
2260                 completion->priv->current_selected = 0;
2261             }
2262           else
2263             {
2264               completion->priv->current_selected -= PAGE_STEP;
2265               if (completion->priv->current_selected < matches - 1)
2266                 completion->priv->current_selected = matches - 1;
2267             }
2268         }
2269       else if (event->keyval == GDK_KEY_Page_Down)
2270         {
2271           if (completion->priv->current_selected < 0)
2272             completion->priv->current_selected = 0;
2273           else if (completion->priv->current_selected < matches - 1)
2274             {
2275               completion->priv->current_selected += PAGE_STEP;
2276               if (completion->priv->current_selected > matches - 1)
2277                 completion->priv->current_selected = matches - 1;
2278             }
2279           else if (completion->priv->current_selected == matches + actions - 1)
2280             {
2281               completion->priv->current_selected = -1;
2282             }
2283           else
2284             {
2285               completion->priv->current_selected += PAGE_STEP;
2286               if (completion->priv->current_selected > matches + actions - 1)
2287                 completion->priv->current_selected = matches + actions - 1;
2288             }
2289         }
2290
2291       if (completion->priv->current_selected < 0)
2292         {
2293           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
2294           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
2295
2296           if (completion->priv->inline_selection &&
2297               completion->priv->completion_prefix)
2298             {
2299               gtk_entry_set_text (GTK_ENTRY (completion->priv->entry),
2300                                   completion->priv->completion_prefix);
2301               gtk_editable_set_position (GTK_EDITABLE (widget), -1);
2302             }
2303         }
2304       else if (completion->priv->current_selected < matches)
2305         {
2306           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
2307
2308           path = gtk_tree_path_new_from_indices (completion->priv->current_selected, -1);
2309           gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->tree_view),
2310                                     path, NULL, FALSE);
2311
2312           if (completion->priv->inline_selection)
2313             {
2314
2315               GtkTreeIter iter;
2316               GtkTreeIter child_iter;
2317               GtkTreeModel *model = NULL;
2318               GtkTreeSelection *sel;
2319               gboolean entry_set;
2320
2321               sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
2322               if (!gtk_tree_selection_get_selected (sel, &model, &iter))
2323                 return FALSE;
2324              gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_iter, &iter);
2325               model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
2326
2327               if (completion->priv->completion_prefix == NULL)
2328                 completion->priv->completion_prefix = g_strdup (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)));
2329
2330               g_signal_emit_by_name (completion, "cursor-on-match", model,
2331                                      &child_iter, &entry_set);
2332             }
2333         }
2334       else if (completion->priv->current_selected - matches >= 0)
2335         {
2336           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
2337
2338           path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
2339           gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
2340                                     path, NULL, FALSE);
2341
2342           if (completion->priv->inline_selection &&
2343               completion->priv->completion_prefix)
2344             {
2345               gtk_entry_set_text (GTK_ENTRY (completion->priv->entry),
2346                                   completion->priv->completion_prefix);
2347               gtk_editable_set_position (GTK_EDITABLE (widget), -1);
2348             }
2349         }
2350
2351       gtk_tree_path_free (path);
2352
2353       return TRUE;
2354     }
2355   else if (event->keyval == GDK_KEY_Escape ||
2356            event->keyval == GDK_KEY_Left ||
2357            event->keyval == GDK_KEY_KP_Left ||
2358            event->keyval == GDK_KEY_Right ||
2359            event->keyval == GDK_KEY_KP_Right)
2360     {
2361       gboolean retval = TRUE;
2362
2363       gtk_entry_reset_im_context (GTK_ENTRY (widget));
2364       _gtk_entry_completion_popdown (completion);
2365
2366       if (completion->priv->current_selected < 0)
2367         {
2368           retval = FALSE;
2369           goto keypress_completion_out;
2370         }
2371       else if (completion->priv->inline_selection)
2372         {
2373           /* Escape rejects the tentative completion */
2374           if (event->keyval == GDK_KEY_Escape)
2375             {
2376               if (completion->priv->completion_prefix)
2377                 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry),
2378                                     completion->priv->completion_prefix);
2379               else
2380                 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), "");
2381             }
2382
2383           /* Move the cursor to the end for Right/Esc */
2384           if (event->keyval == GDK_KEY_Right ||
2385               event->keyval == GDK_KEY_KP_Right ||
2386               event->keyval == GDK_KEY_Escape)
2387             gtk_editable_set_position (GTK_EDITABLE (widget), -1);
2388           /* Let the default keybindings run for Left, i.e. either move to the
2389  *            * previous character or select word if a modifier is used */
2390           else
2391             retval = FALSE;
2392         }
2393
2394 keypress_completion_out:
2395       if (completion->priv->inline_selection)
2396         {
2397           g_free (completion->priv->completion_prefix);
2398           completion->priv->completion_prefix = NULL;
2399         }
2400
2401       return retval;
2402     }
2403   else if (event->keyval == GDK_KEY_Tab ||
2404            event->keyval == GDK_KEY_KP_Tab ||
2405            event->keyval == GDK_KEY_ISO_Left_Tab)
2406     {
2407       gtk_entry_reset_im_context (GTK_ENTRY (widget));
2408       _gtk_entry_completion_popdown (completion);
2409
2410       g_free (completion->priv->completion_prefix);
2411       completion->priv->completion_prefix = NULL;
2412
2413       return FALSE;
2414     }
2415   else if (event->keyval == GDK_KEY_ISO_Enter ||
2416            event->keyval == GDK_KEY_KP_Enter ||
2417            event->keyval == GDK_KEY_Return)
2418     {
2419       GtkTreeIter iter;
2420       GtkTreeModel *model = NULL;
2421       GtkTreeModel *child_model;
2422       GtkTreeIter child_iter;
2423       GtkTreeSelection *sel;
2424       gboolean retval = TRUE;
2425
2426       gtk_entry_reset_im_context (GTK_ENTRY (widget));
2427       _gtk_entry_completion_popdown (completion);
2428
2429       if (completion->priv->current_selected < matches)
2430         {
2431           gboolean entry_set;
2432
2433           sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
2434           if (gtk_tree_selection_get_selected (sel, &model, &iter))
2435             {
2436               gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_iter, &iter);
2437               child_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
2438               g_signal_handler_block (widget, completion->priv->changed_id);
2439               g_signal_emit_by_name (completion, "match-selected",
2440                                      child_model, &child_iter, &entry_set);
2441               g_signal_handler_unblock (widget, completion->priv->changed_id);
2442
2443               if (!entry_set)
2444                 {
2445                   gchar *str = NULL;
2446
2447                   gtk_tree_model_get (model, &iter,
2448                                       completion->priv->text_column, &str,
2449                                       -1);
2450
2451                   gtk_entry_set_text (GTK_ENTRY (widget), str);
2452
2453                   /* move the cursor to the end */
2454                   gtk_editable_set_position (GTK_EDITABLE (widget), -1);
2455                   g_free (str);
2456                 }
2457             }
2458           else
2459             retval = FALSE;
2460         }
2461       else if (completion->priv->current_selected - matches >= 0)
2462         {
2463           sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
2464           if (gtk_tree_selection_get_selected (sel, &model, &iter))
2465             {
2466               GtkTreePath *path;
2467
2468               path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
2469               g_signal_emit_by_name (completion, "action-activated",
2470                                      gtk_tree_path_get_indices (path)[0]);
2471               gtk_tree_path_free (path);
2472             }
2473           else
2474             retval = FALSE;
2475         }
2476
2477       g_free (completion->priv->completion_prefix);
2478       completion->priv->completion_prefix = NULL;
2479
2480       return retval;
2481     }
2482
2483   return FALSE;
2484 }
2485
2486 static void
2487 gtk_entry_completion_changed (GtkWidget *widget,
2488                               gpointer   user_data)
2489 {
2490   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
2491   GtkEntry *entry = GTK_ENTRY (widget);
2492   GdkDevice *device;
2493
2494   if (!completion->priv->popup_completion)
2495     return;
2496
2497   /* (re)install completion timeout */
2498   if (completion->priv->completion_timeout)
2499     g_source_remove (completion->priv->completion_timeout);
2500
2501   if (!gtk_entry_get_text (entry))
2502     return;
2503
2504   /* no need to normalize for this test */
2505   if (completion->priv->minimum_key_length > 0 &&
2506       strcmp ("", gtk_entry_get_text (entry)) == 0)
2507     {
2508       if (gtk_widget_get_visible (completion->priv->popup_window))
2509         _gtk_entry_completion_popdown (completion);
2510       return;
2511     }
2512
2513   device = gtk_get_current_event_device ();
2514
2515   if (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
2516     device = gdk_device_get_associated_device (device);
2517
2518   if (device)
2519     completion->priv->device = device;
2520
2521   completion->priv->completion_timeout =
2522     gdk_threads_add_timeout (COMPLETION_TIMEOUT,
2523                    gtk_entry_completion_timeout,
2524                    completion);
2525 }
2526
2527 static gboolean
2528 check_completion_callback (GtkEntryCompletion *completion)
2529 {
2530   completion->priv->check_completion_idle = NULL;
2531   
2532   gtk_entry_completion_complete (completion);
2533   gtk_entry_completion_insert_prefix (completion);
2534
2535   return FALSE;
2536 }
2537
2538 static void
2539 clear_completion_callback (GtkEntry   *entry,
2540                            GParamSpec *pspec)
2541 {
2542   GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
2543       
2544   if (!completion->priv->inline_completion)
2545     return;
2546
2547   if (pspec->name == I_("cursor-position") ||
2548       pspec->name == I_("selection-bound"))
2549     completion->priv->has_completion = FALSE;
2550 }
2551
2552 static gboolean
2553 accept_completion_callback (GtkEntry *entry)
2554 {
2555   GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
2556
2557   if (!completion->priv->inline_completion)
2558     return FALSE;
2559
2560   if (completion->priv->has_completion)
2561     gtk_editable_set_position (GTK_EDITABLE (entry),
2562                                gtk_entry_buffer_get_length (gtk_entry_get_buffer (entry)));
2563
2564   return FALSE;
2565 }
2566
2567 static void
2568 completion_insert_text_callback (GtkEntry           *entry,
2569                                  const gchar        *text,
2570                                  gint                length,
2571                                  gint                position,
2572                                  GtkEntryCompletion *completion)
2573 {
2574   if (!completion->priv->inline_completion)
2575     return;
2576
2577   /* idle to update the selection based on the file list */
2578   if (completion->priv->check_completion_idle == NULL)
2579     {
2580       completion->priv->check_completion_idle = g_idle_source_new ();
2581       g_source_set_priority (completion->priv->check_completion_idle, G_PRIORITY_HIGH);
2582       g_source_set_closure (completion->priv->check_completion_idle,
2583                             g_cclosure_new_object (G_CALLBACK (check_completion_callback),
2584                                                    G_OBJECT (completion)));
2585       g_source_attach (completion->priv->check_completion_idle, NULL);
2586     }
2587 }
2588
2589 static void
2590 connect_completion_signals (GtkEntryCompletion *completion)
2591 {
2592   completion->priv->changed_id =
2593     g_signal_connect (completion->priv->entry, "changed",
2594                       G_CALLBACK (gtk_entry_completion_changed), completion);
2595   g_signal_connect (completion->priv->entry, "key-press-event",
2596                     G_CALLBACK (gtk_entry_completion_key_press), completion);
2597
2598     completion->priv->insert_text_id =
2599       g_signal_connect (completion->priv->entry, "insert-text",
2600                         G_CALLBACK (completion_insert_text_callback), completion);
2601     g_signal_connect (completion->priv->entry, "notify",
2602                       G_CALLBACK (clear_completion_callback), completion);
2603     g_signal_connect (completion->priv->entry, "activate",
2604                       G_CALLBACK (accept_completion_callback), completion);
2605     g_signal_connect (completion->priv->entry, "focus-out-event",
2606                       G_CALLBACK (accept_completion_callback), completion);
2607 }
2608
2609 static void
2610 set_accessible_relation (GtkWidget *window,
2611                          GtkWidget *entry)
2612 {
2613   AtkObject *window_accessible;
2614   AtkObject *entry_accessible;
2615
2616   window_accessible = gtk_widget_get_accessible (window);
2617   entry_accessible = gtk_widget_get_accessible (entry);
2618
2619   atk_object_add_relationship (window_accessible,
2620                                ATK_RELATION_POPUP_FOR,
2621                                entry_accessible);
2622 }
2623
2624 static void
2625 unset_accessible_relation (GtkWidget *window,
2626                            GtkWidget *entry)
2627 {
2628   AtkObject *window_accessible;
2629   AtkObject *entry_accessible;
2630
2631   window_accessible = gtk_widget_get_accessible (window);
2632   entry_accessible = gtk_widget_get_accessible (entry);
2633
2634   atk_object_remove_relationship (window_accessible,
2635                                   ATK_RELATION_POPUP_FOR,
2636                                   entry_accessible);
2637 }
2638
2639 static void
2640 disconnect_completion_signals (GtkEntryCompletion *completion)
2641 {
2642   if (completion->priv->changed_id > 0 &&
2643       g_signal_handler_is_connected (completion->priv->entry,
2644                                      completion->priv->changed_id))
2645     {
2646       g_signal_handler_disconnect (completion->priv->entry,
2647                                    completion->priv->changed_id);
2648       completion->priv->changed_id = 0;
2649     }
2650   g_signal_handlers_disconnect_by_func (completion->priv->entry,
2651                                         G_CALLBACK (gtk_entry_completion_key_press), completion);
2652   if (completion->priv->insert_text_id > 0 &&
2653       g_signal_handler_is_connected (completion->priv->entry,
2654                                      completion->priv->insert_text_id))
2655     {
2656       g_signal_handler_disconnect (completion->priv->entry,
2657                                    completion->priv->insert_text_id);
2658       completion->priv->insert_text_id = 0;
2659     }
2660   g_signal_handlers_disconnect_by_func (completion->priv->entry,
2661                                         G_CALLBACK (completion_insert_text_callback), completion);
2662   g_signal_handlers_disconnect_by_func (completion->priv->entry,
2663                                         G_CALLBACK (clear_completion_callback), completion);
2664   g_signal_handlers_disconnect_by_func (completion->priv->entry,
2665                                         G_CALLBACK (accept_completion_callback), completion);
2666 }
2667
2668 void
2669 _gtk_entry_completion_disconnect (GtkEntryCompletion *completion)
2670 {
2671   if (completion->priv->completion_timeout)
2672     {
2673       g_source_remove (completion->priv->completion_timeout);
2674       completion->priv->completion_timeout = 0;
2675     }
2676   if (completion->priv->check_completion_idle)
2677     {
2678       g_source_destroy (completion->priv->check_completion_idle);
2679       completion->priv->check_completion_idle = NULL;
2680     }
2681
2682   if (gtk_widget_get_mapped (completion->priv->popup_window))
2683     _gtk_entry_completion_popdown (completion);
2684
2685   disconnect_completion_signals (completion);
2686
2687   unset_accessible_relation (completion->priv->popup_window,
2688                              completion->priv->entry);
2689
2690   completion->priv->entry = NULL;
2691 }
2692
2693 void
2694 _gtk_entry_completion_connect (GtkEntryCompletion *completion,
2695                                GtkEntry           *entry)
2696 {
2697   completion->priv->entry = GTK_WIDGET (entry);
2698
2699   set_accessible_relation (completion->priv->popup_window,
2700                            completion->priv->entry);
2701
2702   connect_completion_signals (completion);
2703 }