]> Pileus Git - ~andy/gtk/blob - gtk/gtkentrycompletion.c
Make this function public API. (#307099, Todd Berman)
[~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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <config.h>
21 #include "gtkentrycompletion.h"
22 #include "gtkentryprivate.h"
23 #include "gtkcelllayout.h"
24
25 #include "gtkintl.h"
26 #include "gtkcellrenderertext.h"
27 #include "gtkframe.h"
28 #include "gtktreeselection.h"
29 #include "gtktreeview.h"
30 #include "gtkscrolledwindow.h"
31 #include "gtkvbox.h"
32 #include "gtkwindow.h"
33 #include "gtkentry.h"
34 #include "gtkmain.h"
35 #include "gtksignal.h"
36 #include "gtkmarshalers.h"
37
38 #include "gtkprivate.h"
39 #include "gtkalias.h"
40
41 #include <string.h>
42
43
44 /* signals */
45 enum
46 {
47   INSERT_PREFIX,
48   MATCH_SELECTED,
49   ACTION_ACTIVATED,
50   LAST_SIGNAL
51 };
52
53 /* properties */
54 enum
55 {
56   PROP_0,
57   PROP_MODEL,
58   PROP_MINIMUM_KEY_LENGTH,
59   PROP_TEXT_COLUMN,
60   PROP_INLINE_COMPLETION,
61   PROP_POPUP_COMPLETION,
62   PROP_POPUP_SET_WIDTH,
63   PROP_POPUP_SINGLE_MATCH
64 };
65
66 #define GTK_ENTRY_COMPLETION_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ENTRY_COMPLETION, GtkEntryCompletionPrivate))
67
68 static void     gtk_entry_completion_class_init          (GtkEntryCompletionClass *klass);
69 static void     gtk_entry_completion_cell_layout_init    (GtkCellLayoutIface      *iface);
70 static void     gtk_entry_completion_init                (GtkEntryCompletion      *completion);
71 static void     gtk_entry_completion_set_property        (GObject                 *object,
72                                                           guint                    prop_id,
73                                                           const GValue            *value,
74                                                           GParamSpec              *pspec);
75 static void     gtk_entry_completion_get_property        (GObject                 *object,
76                                                           guint                    prop_id,
77                                                           GValue                  *value,
78                                                           GParamSpec              *pspec);
79 static void     gtk_entry_completion_finalize            (GObject                 *object);
80
81 static void     gtk_entry_completion_pack_start          (GtkCellLayout           *cell_layout,
82                                                           GtkCellRenderer         *cell,
83                                                           gboolean                 expand);
84 static void     gtk_entry_completion_pack_end            (GtkCellLayout           *cell_layout,
85                                                           GtkCellRenderer         *cell,
86                                                           gboolean                 expand);
87 static void     gtk_entry_completion_clear               (GtkCellLayout           *cell_layout);
88 static void     gtk_entry_completion_add_attribute       (GtkCellLayout           *cell_layout,
89                                                           GtkCellRenderer         *cell,
90                                                           const char              *attribute,
91                                                           gint                     column);
92 static void     gtk_entry_completion_set_cell_data_func  (GtkCellLayout           *cell_layout,
93                                                           GtkCellRenderer         *cell,
94                                                           GtkCellLayoutDataFunc    func,
95                                                           gpointer                 func_data,
96                                                           GDestroyNotify           destroy);
97 static void     gtk_entry_completion_clear_attributes    (GtkCellLayout           *cell_layout,
98                                                           GtkCellRenderer         *cell);
99 static void     gtk_entry_completion_reorder             (GtkCellLayout           *cell_layout,
100                                                           GtkCellRenderer         *cell,
101                                                           gint                     position);
102
103 static gboolean gtk_entry_completion_visible_func        (GtkTreeModel            *model,
104                                                           GtkTreeIter             *iter,
105                                                           gpointer                 data);
106 static gboolean gtk_entry_completion_popup_key_press     (GtkWidget               *widget,
107                                                           GdkEventKey             *event,
108                                                           gpointer                 user_data);
109 static gboolean gtk_entry_completion_popup_button_press  (GtkWidget               *widget,
110                                                           GdkEventButton          *event,
111                                                           gpointer                 user_data);
112 static gboolean gtk_entry_completion_list_button_press   (GtkWidget               *widget,
113                                                           GdkEventButton          *event,
114                                                           gpointer                 user_data);
115 static gboolean gtk_entry_completion_action_button_press (GtkWidget               *widget,
116                                                           GdkEventButton          *event,
117                                                           gpointer                 user_data);
118 static void     gtk_entry_completion_selection_changed   (GtkTreeSelection        *selection,
119                                                           gpointer                 data);
120 static gboolean gtk_entry_completion_list_enter_notify   (GtkWidget               *widget,
121                                                           GdkEventCrossing        *event,
122                                                           gpointer                 data);
123 static gboolean gtk_entry_completion_list_motion_notify  (GtkWidget               *widget,
124                                                           GdkEventMotion          *event,
125                                                           gpointer                 data);
126 static void     gtk_entry_completion_insert_action       (GtkEntryCompletion      *completion,
127                                                           gint                     index,
128                                                           const gchar             *string,
129                                                           gboolean                 markup);
130 static void     gtk_entry_completion_action_data_func    (GtkTreeViewColumn       *tree_column,
131                                                           GtkCellRenderer         *cell,
132                                                           GtkTreeModel            *model,
133                                                           GtkTreeIter             *iter,
134                                                           gpointer                 data);
135
136 static gboolean gtk_entry_completion_match_selected      (GtkEntryCompletion *completion,
137                                                           GtkTreeModel       *model,
138                                                           GtkTreeIter        *iter);
139 static gboolean gtk_entry_completion_real_insert_prefix  (GtkEntryCompletion *completion,
140                                                           const gchar        *prefix);
141
142 static GObjectClass *parent_class = NULL;
143 static guint entry_completion_signals[LAST_SIGNAL] = { 0 };
144
145
146 GType
147 gtk_entry_completion_get_type (void)
148 {
149   static GType entry_completion_type = 0;
150
151   if (!entry_completion_type)
152     {
153       static const GTypeInfo entry_completion_info =
154       {
155         sizeof (GtkEntryCompletionClass),
156         NULL,
157         NULL,
158         (GClassInitFunc) gtk_entry_completion_class_init,
159         NULL,
160         NULL,
161         sizeof (GtkEntryCompletion),
162         0,
163         (GInstanceInitFunc) gtk_entry_completion_init
164       };
165
166       static const GInterfaceInfo cell_layout_info =
167       {
168         (GInterfaceInitFunc) gtk_entry_completion_cell_layout_init,
169         NULL,
170         NULL
171       };
172
173       entry_completion_type =
174         g_type_register_static (G_TYPE_OBJECT, I_("GtkEntryCompletion"),
175                                 &entry_completion_info, 0);
176
177       g_type_add_interface_static (entry_completion_type,
178                                    GTK_TYPE_CELL_LAYOUT,
179                                    &cell_layout_info);
180     }
181
182   return entry_completion_type;
183 }
184
185 static void
186 gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
187 {
188   GObjectClass *object_class;
189
190   parent_class = g_type_class_peek_parent (klass);
191   object_class = (GObjectClass *)klass;
192
193   object_class->set_property = gtk_entry_completion_set_property;
194   object_class->get_property = gtk_entry_completion_get_property;
195   object_class->finalize = gtk_entry_completion_finalize;
196
197   klass->match_selected = gtk_entry_completion_match_selected;
198   klass->insert_prefix = gtk_entry_completion_real_insert_prefix;
199
200   /**
201    * GtkEntryCompletion::insert-prefix:
202    * @widget: the object which received the signal
203    * @prefix: the common prefix of all possible completions
204    * 
205    * Gets emitted when the inline autocompletion is triggered. 
206    * The default behaviour is to make the entry display the 
207    * whole prefix and select the newly inserted part.
208    *
209    * Applications may connect to this signal in order to insert only a
210    * smaller part of the @prefix into the entry - e.g. the entry used in
211    * the #GtkFileChooser inserts only the part of the prefix up to the 
212    * next '/'.
213    *
214    * Return value: %TRUE if the signal has been handled
215    * 
216    * Since: 2.6
217    */ 
218   entry_completion_signals[INSERT_PREFIX] =
219     g_signal_new (I_("insert_prefix"),
220                   G_TYPE_FROM_CLASS (klass),
221                   G_SIGNAL_RUN_LAST,
222                   G_STRUCT_OFFSET (GtkEntryCompletionClass, insert_prefix),
223                   _gtk_boolean_handled_accumulator, NULL,
224                   _gtk_marshal_BOOLEAN__STRING,
225                   G_TYPE_BOOLEAN, 1,
226                   G_TYPE_STRING);
227
228   /**
229    * GtkEntryCompletion::match-selected:
230    * @widget: the object which received the signal
231    * @model: the #GtkTreeModel containing the matches
232    * @iter: a #GtkTreeIter positioned at the selected match
233    * 
234    * Gets emitted when a match from the list is selected. 
235    * The default behaviour is to replace the contents of the 
236    * entry with the contents of the text column in the row 
237    * pointed to by @iter.
238    *
239    * Return value: %TRUE if the signal has been handled
240    * 
241    * Since: 2.4
242    */ 
243   entry_completion_signals[MATCH_SELECTED] =
244     g_signal_new (I_("match_selected"),
245                   G_TYPE_FROM_CLASS (klass),
246                   G_SIGNAL_RUN_LAST,
247                   G_STRUCT_OFFSET (GtkEntryCompletionClass, match_selected),
248                   _gtk_boolean_handled_accumulator, NULL,
249                   _gtk_marshal_BOOLEAN__OBJECT_BOXED,
250                   G_TYPE_BOOLEAN, 2,
251                   GTK_TYPE_TREE_MODEL,
252                   GTK_TYPE_TREE_ITER);
253                   
254   /**
255    * GtkEntryCompletion::action-activated:
256    * @widget: the object which received the signal
257    * @index: the index of the activated action
258    *
259    * Gets emitted when an action is activated.
260    * 
261    * Since: 2.4
262    */
263   entry_completion_signals[ACTION_ACTIVATED] =
264     g_signal_new (I_("action_activated"),
265                   G_TYPE_FROM_CLASS (klass),
266                   G_SIGNAL_RUN_LAST,
267                   G_STRUCT_OFFSET (GtkEntryCompletionClass, action_activated),
268                   NULL, NULL,
269                   _gtk_marshal_NONE__INT,
270                   G_TYPE_NONE, 1,
271                   G_TYPE_INT);
272
273   g_object_class_install_property (object_class,
274                                    PROP_MODEL,
275                                    g_param_spec_object ("model",
276                                                         P_("Completion Model"),
277                                                         P_("The model to find matches in"),
278                                                         GTK_TYPE_TREE_MODEL,
279                                                         GTK_PARAM_READWRITE));
280   g_object_class_install_property (object_class,
281                                    PROP_MINIMUM_KEY_LENGTH,
282                                    g_param_spec_int ("minimum-key-length",
283                                                      P_("Minimum Key Length"),
284                                                      P_("Minimum length of the search key in order to look up matches"),
285                                                      0,
286                                                      G_MAXINT,
287                                                      1,
288                                                      GTK_PARAM_READWRITE));
289   /**
290    * GtkEntryCompletion:text-column:
291    *
292    * The column of the model containing the strings.
293    *
294    * Since: 2.6
295    */
296   g_object_class_install_property (object_class,
297                                    PROP_TEXT_COLUMN,
298                                    g_param_spec_int ("text-column",
299                                                      P_("Text column"),
300                                                      P_("The column of the model containing the strings."),
301                                                      -1,
302                                                      G_MAXINT,
303                                                      -1,
304                                                      GTK_PARAM_READWRITE));
305
306   /**
307    * GtkEntryCompletion:inline-completion:
308    * 
309    * Determines whether the common prefix of the possible completions 
310    * should be inserted automatically in the entry. Note that this
311    * requires text-column to be set, even if you are using a custom
312    * match function.
313    *
314    * Since: 2.6
315    **/
316   g_object_class_install_property (object_class,
317                                    PROP_INLINE_COMPLETION,
318                                    g_param_spec_boolean ("inline-completion",
319                                                          P_("Inline completion"),
320                                                          P_("Whether the common prefix should be inserted automatically"),
321                                                          FALSE,
322                                                          GTK_PARAM_READWRITE));
323   /**
324    * GtkEntryCompletion:popup-completion:
325    * 
326    * Determines whether the possible completions should be 
327    * shown in a popup window. 
328    *
329    * Since: 2.6
330    **/
331   g_object_class_install_property (object_class,
332                                    PROP_POPUP_COMPLETION,
333                                    g_param_spec_boolean ("popup-completion",
334                                                          P_("Popup completion"),
335                                                          P_("Whether the completions should be shown in a popup window"),
336                                                          TRUE,
337                                                          GTK_PARAM_READWRITE));
338
339   /**
340    * GtkEntryCompletion:popup-set-width:
341    * 
342    * Determines whether the completions popup window will be
343    * resized to the width of the entry.
344    *
345    * Since: 2.8
346    */
347   g_object_class_install_property (object_class,
348                                    PROP_POPUP_SET_WIDTH,
349                                    g_param_spec_boolean ("popup-set-width",
350                                                          P_("Popup set width"),
351                                                          P_("If TRUE, the popup window will have the same size as the entry"),
352                                                          TRUE,
353                                                          GTK_PARAM_READWRITE));
354
355   /**
356    * GtkEntryCompletion:popup-single-match:
357    * 
358    * Determines whether the completions popup window will shown
359    * for a single possible completion. You probably want to set
360    * this to %FALSE if you are using 
361    * <link linkend="GtkEntryCompletion--inline-completion">inline 
362    * completion</link>.
363    *
364    * Since: 2.8
365    */
366   g_object_class_install_property (object_class,
367                                    PROP_POPUP_SINGLE_MATCH,
368                                    g_param_spec_boolean ("popup-single-match",
369                                                          P_("Popup single match"),
370                                                          P_("If TRUE, the popup window will appear for a single match."),
371                                                          TRUE,
372                                                          GTK_PARAM_READWRITE));
373
374   g_type_class_add_private (object_class, sizeof (GtkEntryCompletionPrivate));
375 }
376
377 static void
378 gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface)
379 {
380   iface->pack_start = gtk_entry_completion_pack_start;
381   iface->pack_end = gtk_entry_completion_pack_end;
382   iface->clear = gtk_entry_completion_clear;
383   iface->add_attribute = gtk_entry_completion_add_attribute;
384   iface->set_cell_data_func = gtk_entry_completion_set_cell_data_func;
385   iface->clear_attributes = gtk_entry_completion_clear_attributes;
386   iface->reorder = gtk_entry_completion_reorder;
387 }
388
389 static void
390 gtk_entry_completion_init (GtkEntryCompletion *completion)
391 {
392   GtkCellRenderer *cell;
393   GtkTreeSelection *sel;
394   GtkEntryCompletionPrivate *priv;
395   GtkWidget *popup_frame;
396
397   /* yes, also priv, need to keep the code readable */
398   priv = completion->priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (completion);
399
400   priv->minimum_key_length = 1;
401   priv->text_column = -1;
402   priv->has_completion = FALSE;
403   priv->inline_completion = FALSE;
404   priv->popup_completion = TRUE;
405   priv->popup_set_width = TRUE;
406   priv->popup_single_match = TRUE;
407
408   /* completions */
409   priv->filter_model = NULL;
410
411   priv->tree_view = gtk_tree_view_new ();
412   g_signal_connect (priv->tree_view, "button_press_event",
413                     G_CALLBACK (gtk_entry_completion_list_button_press),
414                     completion);
415   g_signal_connect (priv->tree_view, "enter_notify_event",
416                     G_CALLBACK (gtk_entry_completion_list_enter_notify),
417                     completion);
418   g_signal_connect (priv->tree_view, "motion_notify_event",
419                     G_CALLBACK (gtk_entry_completion_list_motion_notify),
420                     completion);
421   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
422   gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view), TRUE);
423
424   sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
425   gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
426   gtk_tree_selection_unselect_all (sel);
427   g_signal_connect (sel, "changed",
428                     G_CALLBACK (gtk_entry_completion_selection_changed),
429                     completion);
430   priv->first_sel_changed = TRUE;
431
432   priv->column = gtk_tree_view_column_new ();
433   gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
434
435   priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
436   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
437                                   GTK_POLICY_NEVER,
438                                   GTK_POLICY_AUTOMATIC);
439   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
440                                        GTK_SHADOW_NONE);
441
442   /* a nasty hack to get the completions treeview to size nicely */
443   gtk_widget_set_size_request (GTK_SCROLLED_WINDOW (priv->scrolled_window)->vscrollbar, -1, 0);
444
445   /* actions */
446   priv->actions = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_BOOLEAN);
447
448   priv->action_view =
449     gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->actions));
450   g_signal_connect (priv->action_view, "button_press_event",
451                     G_CALLBACK (gtk_entry_completion_action_button_press),
452                     completion);
453   g_signal_connect (priv->action_view, "enter_notify_event",
454                     G_CALLBACK (gtk_entry_completion_list_enter_notify),
455                     completion);
456   g_signal_connect (priv->action_view, "motion_notify_event",
457                     G_CALLBACK (gtk_entry_completion_list_motion_notify),
458                     completion);
459   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->action_view), FALSE);
460   gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->action_view), TRUE);
461
462   sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->action_view));
463   gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
464   gtk_tree_selection_unselect_all (sel);
465
466   cell = gtk_cell_renderer_text_new ();
467   gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (priv->action_view),
468                                               0, "",
469                                               cell,
470                                               gtk_entry_completion_action_data_func,
471                                               NULL,
472                                               NULL);
473
474   /* pack it all */
475   priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
476   gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
477   g_signal_connect (priv->popup_window, "key_press_event",
478                     G_CALLBACK (gtk_entry_completion_popup_key_press),
479                     completion);
480   g_signal_connect (priv->popup_window, "button_press_event",
481                     G_CALLBACK (gtk_entry_completion_popup_button_press),
482                     completion);
483
484   popup_frame = gtk_frame_new (NULL);
485   gtk_frame_set_shadow_type (GTK_FRAME (popup_frame),
486                              GTK_SHADOW_ETCHED_IN);
487   gtk_widget_show (popup_frame);
488   gtk_container_add (GTK_CONTAINER (priv->popup_window), popup_frame);
489   
490   priv->vbox = gtk_vbox_new (FALSE, 0);
491   gtk_container_add (GTK_CONTAINER (popup_frame), priv->vbox);
492
493   gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->tree_view);
494   gtk_box_pack_start (GTK_BOX (priv->vbox), priv->scrolled_window,
495                       TRUE, TRUE, 0);
496
497   /* we don't want to see the action treeview when no actions have
498    * been inserted, so we pack the action treeview after the first
499    * action has been added
500    */
501 }
502
503 static void
504 gtk_entry_completion_set_property (GObject      *object,
505                                    guint         prop_id,
506                                    const GValue *value,
507                                    GParamSpec   *pspec)
508 {
509   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
510   GtkEntryCompletionPrivate *priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (completion);
511
512   switch (prop_id)
513     {
514       case PROP_MODEL:
515         gtk_entry_completion_set_model (completion,
516                                         g_value_get_object (value));
517         break;
518
519       case PROP_MINIMUM_KEY_LENGTH:
520         gtk_entry_completion_set_minimum_key_length (completion,
521                                                      g_value_get_int (value));
522         break;
523
524       case PROP_TEXT_COLUMN:
525         priv->text_column = g_value_get_int (value);
526         break;
527
528       case PROP_INLINE_COMPLETION:
529         priv->inline_completion = g_value_get_boolean (value);
530         break;
531
532       case PROP_POPUP_COMPLETION:
533         priv->popup_completion = g_value_get_boolean (value);
534         break;
535
536       case PROP_POPUP_SET_WIDTH:
537         priv->popup_set_width = g_value_get_boolean (value);
538         break;
539
540       case PROP_POPUP_SINGLE_MATCH:
541         priv->popup_single_match = g_value_get_boolean (value);
542         break;
543
544       default:
545         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
546         break;
547     }
548 }
549
550 static void
551 gtk_entry_completion_get_property (GObject    *object,
552                                    guint       prop_id,
553                                    GValue     *value,
554                                    GParamSpec *pspec)
555 {
556   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
557
558   switch (prop_id)
559     {
560       case PROP_MODEL:
561         g_value_set_object (value,
562                             gtk_entry_completion_get_model (completion));
563         break;
564
565       case PROP_MINIMUM_KEY_LENGTH:
566         g_value_set_int (value, gtk_entry_completion_get_minimum_key_length (completion));
567         break;
568
569       case PROP_TEXT_COLUMN:
570         g_value_set_int (value, gtk_entry_completion_get_text_column (completion));
571         break;
572
573       case PROP_INLINE_COMPLETION:
574         g_value_set_boolean (value, gtk_entry_completion_get_inline_completion (completion));
575         break;
576
577       case PROP_POPUP_COMPLETION:
578         g_value_set_boolean (value, gtk_entry_completion_get_popup_completion (completion));
579         break;
580
581       case PROP_POPUP_SET_WIDTH:
582         g_value_set_boolean (value, gtk_entry_completion_get_popup_set_width (completion));
583         break;
584
585       case PROP_POPUP_SINGLE_MATCH:
586         g_value_set_boolean (value, gtk_entry_completion_get_popup_single_match (completion));
587         break;
588
589       default:
590         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
591         break;
592     }
593 }
594
595 static void
596 gtk_entry_completion_finalize (GObject *object)
597 {
598   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
599
600   if (completion->priv->tree_view)
601     gtk_widget_destroy (completion->priv->tree_view);
602
603   if (completion->priv->entry)
604     gtk_entry_set_completion (GTK_ENTRY (completion->priv->entry), NULL);
605
606   if (completion->priv->actions)
607     g_object_unref (completion->priv->actions);
608
609   if (completion->priv->case_normalized_key)
610     g_free (completion->priv->case_normalized_key);
611
612   if (completion->priv->popup_window)
613     gtk_widget_destroy (completion->priv->popup_window);
614
615   G_OBJECT_CLASS (parent_class)->finalize (object);
616 }
617
618 /* implement cell layout interface */
619 static void
620 gtk_entry_completion_pack_start (GtkCellLayout   *cell_layout,
621                                  GtkCellRenderer *cell,
622                                  gboolean         expand)
623 {
624   GtkEntryCompletionPrivate *priv;
625
626   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
627
628   priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
629
630   gtk_tree_view_column_pack_start (priv->column, cell, expand);
631 }
632
633 static void
634 gtk_entry_completion_pack_end (GtkCellLayout   *cell_layout,
635                                GtkCellRenderer *cell,
636                                gboolean         expand)
637 {
638   GtkEntryCompletionPrivate *priv;
639
640   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
641
642   priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
643
644   gtk_tree_view_column_pack_end (priv->column, cell, expand);
645 }
646
647 static void
648 gtk_entry_completion_clear (GtkCellLayout *cell_layout)
649 {
650   GtkEntryCompletionPrivate *priv;
651
652   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
653
654   priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
655
656   gtk_tree_view_column_clear (priv->column);
657 }
658
659 static void
660 gtk_entry_completion_add_attribute (GtkCellLayout   *cell_layout,
661                                     GtkCellRenderer *cell,
662                                     const gchar     *attribute,
663                                     gint             column)
664 {
665   GtkEntryCompletionPrivate *priv;
666
667   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
668
669   priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
670
671   gtk_tree_view_column_add_attribute (priv->column, cell, attribute, column);
672 }
673
674 static void
675 gtk_entry_completion_set_cell_data_func (GtkCellLayout          *cell_layout,
676                                          GtkCellRenderer        *cell,
677                                          GtkCellLayoutDataFunc   func,
678                                          gpointer                func_data,
679                                          GDestroyNotify          destroy)
680 {
681   GtkEntryCompletionPrivate *priv;
682
683   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
684
685   priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
686
687   gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column),
688                                       cell, func, func_data, destroy);
689 }
690
691 static void
692 gtk_entry_completion_clear_attributes (GtkCellLayout   *cell_layout,
693                                        GtkCellRenderer *cell)
694 {
695   GtkEntryCompletionPrivate *priv;
696
697   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
698
699   priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
700
701   gtk_tree_view_column_clear_attributes (priv->column, cell);
702 }
703
704 static void
705 gtk_entry_completion_reorder (GtkCellLayout   *cell_layout,
706                               GtkCellRenderer *cell,
707                               gint             position)
708 {
709   GtkEntryCompletionPrivate *priv;
710
711   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
712
713   priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
714
715   gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column), cell, position);
716 }
717
718 /* all those callbacks */
719 static gboolean
720 gtk_entry_completion_default_completion_func (GtkEntryCompletion *completion,
721                                               const gchar        *key,
722                                               GtkTreeIter        *iter,
723                                               gpointer            user_data)
724 {
725   gchar *item = NULL;
726   gchar *normalized_string;
727   gchar *case_normalized_string;
728
729   gboolean ret = FALSE;
730
731   GtkTreeModel *model;
732
733   model = gtk_tree_model_filter_get_model (completion->priv->filter_model);
734
735   g_return_val_if_fail (gtk_tree_model_get_column_type (model, completion->priv->text_column) == G_TYPE_STRING, 
736                         FALSE);
737
738   gtk_tree_model_get (model, iter,
739                       completion->priv->text_column, &item,
740                       -1);
741
742   if (item != NULL)
743     {
744       normalized_string = g_utf8_normalize (item, -1, G_NORMALIZE_ALL);
745       case_normalized_string = g_utf8_casefold (normalized_string, -1);
746       
747       if (!strncmp (key, case_normalized_string, strlen (key)))
748         ret = TRUE;
749       
750       g_free (item);
751       g_free (normalized_string);
752       g_free (case_normalized_string);
753     }
754
755   return ret;
756 }
757
758 static gboolean
759 gtk_entry_completion_visible_func (GtkTreeModel *model,
760                                    GtkTreeIter  *iter,
761                                    gpointer      data)
762 {
763   gboolean ret = FALSE;
764
765   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
766
767   if (!completion->priv->case_normalized_key)
768     return ret;
769
770   if (completion->priv->match_func)
771     ret = (* completion->priv->match_func) (completion,
772                                             completion->priv->case_normalized_key,
773                                             iter,
774                                             completion->priv->match_data);
775   else if (completion->priv->text_column >= 0)
776     ret = gtk_entry_completion_default_completion_func (completion,
777                                                         completion->priv->case_normalized_key,
778                                                         iter,
779                                                         NULL);
780
781   return ret;
782 }
783
784 static gboolean
785 gtk_entry_completion_popup_key_press (GtkWidget   *widget,
786                                       GdkEventKey *event,
787                                       gpointer     user_data)
788 {
789   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
790
791   if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
792     return FALSE;
793
794   /* propagate event to the entry */
795   gtk_widget_event (completion->priv->entry, (GdkEvent *)event);
796
797   return TRUE;
798 }
799
800 static gboolean
801 gtk_entry_completion_popup_button_press (GtkWidget      *widget,
802                                          GdkEventButton *event,
803                                          gpointer        user_data)
804 {
805   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
806
807   if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
808     return FALSE;
809
810   /* if we come here, it's usually time to popdown */
811   _gtk_entry_completion_popdown (completion);
812
813   return TRUE;
814 }
815
816 static gboolean
817 gtk_entry_completion_list_button_press (GtkWidget      *widget,
818                                         GdkEventButton *event,
819                                         gpointer        user_data)
820 {
821   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
822   GtkTreePath *path = NULL;
823
824   if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
825     return FALSE;
826
827   if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
828                                      event->x, event->y,
829                                      &path, NULL, NULL, NULL))
830     {
831       GtkTreeIter iter;
832       gboolean entry_set;
833
834       gtk_tree_model_get_iter (GTK_TREE_MODEL (completion->priv->filter_model),
835                                &iter, path);
836       gtk_tree_path_free (path);
837
838       g_signal_handler_block (completion->priv->entry,
839                               completion->priv->changed_id);
840       g_signal_emit (completion, entry_completion_signals[MATCH_SELECTED],
841                      0, GTK_TREE_MODEL (completion->priv->filter_model),
842                      &iter, &entry_set);
843       g_signal_handler_unblock (completion->priv->entry,
844                                 completion->priv->changed_id);
845
846       _gtk_entry_completion_popdown (completion);
847
848       return TRUE;
849     }
850
851   return FALSE;
852 }
853
854 static gboolean
855 gtk_entry_completion_action_button_press (GtkWidget      *widget,
856                                           GdkEventButton *event,
857                                           gpointer        user_data)
858 {
859   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
860   GtkTreePath *path = NULL;
861
862   if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
863     return FALSE;
864
865   if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
866                                      event->x, event->y,
867                                      &path, NULL, NULL, NULL))
868     {
869       g_signal_emit (completion, entry_completion_signals[ACTION_ACTIVATED],
870                      0, gtk_tree_path_get_indices (path)[0]);
871       gtk_tree_path_free (path);
872
873       _gtk_entry_completion_popdown (completion);
874       return TRUE;
875     }
876
877   return FALSE;
878 }
879
880 static void
881 gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
882                                        GtkCellRenderer   *cell,
883                                        GtkTreeModel      *model,
884                                        GtkTreeIter       *iter,
885                                        gpointer           data)
886 {
887   gchar *string = NULL;
888   gboolean markup;
889
890   gtk_tree_model_get (model, iter,
891                       0, &string,
892                       1, &markup,
893                       -1);
894
895   if (!string)
896     return;
897
898   if (markup)
899     g_object_set (cell,
900                   "text", NULL,
901                   "markup", string,
902                   NULL);
903   else
904     g_object_set (cell,
905                   "markup", NULL,
906                   "text", string,
907                   NULL);
908
909   g_free (string);
910 }
911
912 static void
913 gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
914                                         gpointer          data)
915 {
916   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
917
918   if (completion->priv->first_sel_changed)
919     {
920       completion->priv->first_sel_changed = FALSE;
921       if (gtk_widget_is_focus (completion->priv->tree_view))
922         gtk_tree_selection_unselect_all (selection);
923     }
924 }
925
926 /* public API */
927
928 /**
929  * gtk_entry_completion_new:
930  *
931  * Creates a new #GtkEntryCompletion object.
932  *
933  * Return value: A newly created #GtkEntryCompletion object.
934  *
935  * Since: 2.4
936  */
937 GtkEntryCompletion *
938 gtk_entry_completion_new (void)
939 {
940   GtkEntryCompletion *completion;
941
942   completion = g_object_new (GTK_TYPE_ENTRY_COMPLETION, NULL);
943
944   return completion;
945 }
946
947 /**
948  * gtk_entry_completion_get_entry:
949  * @completion: A #GtkEntryCompletion.
950  *
951  * Gets the entry @completion has been attached to.
952  *
953  * Return value: The entry @completion has been attached to.
954  *
955  * Since: 2.4
956  */
957 GtkWidget *
958 gtk_entry_completion_get_entry (GtkEntryCompletion *completion)
959 {
960   g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
961
962   return completion->priv->entry;
963 }
964
965 /**
966  * gtk_entry_completion_set_model:
967  * @completion: A #GtkEntryCompletion.
968  * @model: The #GtkTreeModel.
969  *
970  * Sets the model for a #GtkEntryCompletion. If @completion already has
971  * a model set, it will remove it before setting the new model.
972  * If model is %NULL, then it will unset the model.
973  *
974  * Since: 2.4
975  */
976 void
977 gtk_entry_completion_set_model (GtkEntryCompletion *completion,
978                                 GtkTreeModel       *model)
979 {
980   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
981   g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
982
983   if (!model)
984     {
985       gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
986                                NULL);
987       _gtk_entry_completion_popdown (completion);
988       completion->priv->filter_model = NULL;
989       return;
990     }
991      
992   /* code will unref the old filter model (if any) */
993   completion->priv->filter_model =
994     GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
995   gtk_tree_model_filter_set_visible_func (completion->priv->filter_model,
996                                           gtk_entry_completion_visible_func,
997                                           completion,
998                                           NULL);
999
1000   gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
1001                            GTK_TREE_MODEL (completion->priv->filter_model));
1002   g_object_unref (completion->priv->filter_model);
1003
1004   g_object_notify (G_OBJECT (completion), "model");
1005
1006   if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
1007     _gtk_entry_completion_resize_popup (completion);
1008 }
1009
1010 /**
1011  * gtk_entry_completion_get_model:
1012  * @completion: A #GtkEntryCompletion.
1013  *
1014  * Returns the model the #GtkEntryCompletion is using as data source.
1015  * Returns %NULL if the model is unset.
1016  *
1017  * Return value: A #GtkTreeModel, or %NULL if none is currently being used.
1018  *
1019  * Since: 2.4
1020  */
1021 GtkTreeModel *
1022 gtk_entry_completion_get_model (GtkEntryCompletion *completion)
1023 {
1024   g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
1025
1026   if (!completion->priv->filter_model)
1027     return NULL;
1028   
1029   return gtk_tree_model_filter_get_model (completion->priv->filter_model);
1030 }
1031
1032 /**
1033  * gtk_entry_completion_set_match_func:
1034  * @completion: A #GtkEntryCompletion.
1035  * @func: The #GtkEntryCompletionMatchFunc to use.
1036  * @func_data: The user data for @func.
1037  * @func_notify: Destroy notifier for @func_data.
1038  *
1039  * Sets the match function for @completion to be @func. The match function
1040  * is used to determine if a row should or should not be in the completion
1041  * list.
1042  *
1043  * Since: 2.4.
1044  */
1045 void
1046 gtk_entry_completion_set_match_func (GtkEntryCompletion          *completion,
1047                                      GtkEntryCompletionMatchFunc  func,
1048                                      gpointer                     func_data,
1049                                      GDestroyNotify               func_notify)
1050 {
1051   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1052
1053   if (completion->priv->match_notify)
1054     (* completion->priv->match_notify) (completion->priv->match_data);
1055
1056   completion->priv->match_func = func;
1057   completion->priv->match_data = func_data;
1058   completion->priv->match_notify = func_notify;
1059 }
1060
1061 /**
1062  * gtk_entry_completion_set_minimum_key_length:
1063  * @completion: A #GtkEntryCompletion.
1064  * @length: The minimum length of the key in order to start completing.
1065  *
1066  * Requires the length of the search key for @completion to be at least
1067  * @length. This is useful for long lists, where completing using a small
1068  * key takes a lot of time and will come up with meaningless results anyway
1069  * (ie, a too large dataset).
1070  *
1071  * Since: 2.4
1072  */
1073 void
1074 gtk_entry_completion_set_minimum_key_length (GtkEntryCompletion *completion,
1075                                              gint                length)
1076 {
1077   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1078   g_return_if_fail (length >= 0);
1079
1080   if (completion->priv->minimum_key_length != length)
1081     {
1082       completion->priv->minimum_key_length = length;
1083      
1084       g_object_notify (G_OBJECT (completion), "minimum-key-length");
1085     }
1086 }
1087
1088 /**
1089  * gtk_entry_completion_get_minimum_key_length:
1090  * @completion: A #GtkEntryCompletion.
1091  *
1092  * Returns the minimum key length as set for @completion.
1093  *
1094  * Return value: The currently used minimum key length.
1095  *
1096  * Since: 2.4
1097  */
1098 gint
1099 gtk_entry_completion_get_minimum_key_length (GtkEntryCompletion *completion)
1100 {
1101   g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), 0);
1102
1103   return completion->priv->minimum_key_length;
1104 }
1105
1106 /**
1107  * gtk_entry_completion_complete:
1108  * @completion: A #GtkEntryCompletion.
1109  *
1110  * Requests a completion operation, or in other words a refiltering of the
1111  * current list with completions, using the current key. The completion list
1112  * view will be updated accordingly.
1113  *
1114  * Since: 2.4
1115  */
1116 void
1117 gtk_entry_completion_complete (GtkEntryCompletion *completion)
1118 {
1119   gchar *tmp;
1120
1121   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1122
1123   if (!completion->priv->filter_model)
1124     return;
1125   
1126   if (completion->priv->case_normalized_key)
1127     g_free (completion->priv->case_normalized_key);
1128
1129   tmp = g_utf8_normalize (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)),
1130                           -1, G_NORMALIZE_ALL);
1131   completion->priv->case_normalized_key = g_utf8_casefold (tmp, -1);
1132   g_free (tmp);
1133
1134   gtk_tree_model_filter_refilter (completion->priv->filter_model);
1135
1136   if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
1137     _gtk_entry_completion_resize_popup (completion);
1138 }
1139
1140 static void
1141 gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
1142                                     gint                index,
1143                                     const gchar        *string,
1144                                     gboolean            markup)
1145 {
1146   GtkTreeIter iter;
1147
1148   gtk_list_store_insert (completion->priv->actions, &iter, index);
1149   gtk_list_store_set (completion->priv->actions, &iter,
1150                       0, string,
1151                       1, markup,
1152                       -1);
1153
1154   if (!completion->priv->action_view->parent)
1155     {
1156       GtkTreePath *path = gtk_tree_path_new_from_indices (0, -1);
1157
1158       gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
1159                                 path, NULL, FALSE);
1160       gtk_tree_path_free (path);
1161
1162       gtk_box_pack_start (GTK_BOX (completion->priv->vbox),
1163                           completion->priv->action_view, FALSE, FALSE, 0);
1164       gtk_widget_show (completion->priv->action_view);
1165     }
1166 }
1167
1168 /**
1169  * gtk_entry_completion_insert_action_text:
1170  * @completion: A #GtkEntryCompletion.
1171  * @index_: The index of the item to insert.
1172  * @text: Text of the item to insert.
1173  *
1174  * Inserts an action in @completion's action item list at position @index_
1175  * with text @text. If you want the action item to have markup, use
1176  * gtk_entry_completion_insert_action_markup().
1177  *
1178  * Since: 2.4
1179  */
1180 void
1181 gtk_entry_completion_insert_action_text (GtkEntryCompletion *completion,
1182                                          gint                index_,
1183                                          const gchar        *text)
1184 {
1185   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1186   g_return_if_fail (text != NULL);
1187
1188   gtk_entry_completion_insert_action (completion, index_, text, FALSE);
1189 }
1190
1191 /**
1192  * gtk_entry_completion_insert_action_markup:
1193  * @completion: A #GtkEntryCompletion.
1194  * @index_: The index of the item to insert.
1195  * @markup: Markup of the item to insert.
1196  *
1197  * Inserts an action in @completion's action item list at position @index_
1198  * with markup @markup.
1199  *
1200  * Since: 2.4
1201  */
1202 void
1203 gtk_entry_completion_insert_action_markup (GtkEntryCompletion *completion,
1204                                            gint                index_,
1205                                            const gchar        *markup)
1206 {
1207   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1208   g_return_if_fail (markup != NULL);
1209
1210   gtk_entry_completion_insert_action (completion, index_, markup, TRUE);
1211 }
1212
1213 /**
1214  * gtk_entry_completion_delete_action:
1215  * @completion: A #GtkEntryCompletion.
1216  * @index_: The index of the item to Delete.
1217  *
1218  * Deletes the action at @index_ from @completion's action list.
1219  *
1220  * Since: 2.4
1221  */
1222 void
1223 gtk_entry_completion_delete_action (GtkEntryCompletion *completion,
1224                                     gint                index_)
1225 {
1226   GtkTreeIter iter;
1227
1228   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1229   g_return_if_fail (index_ >= 0);
1230
1231   gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (completion->priv->actions),
1232                                  &iter, NULL, index_);
1233   gtk_list_store_remove (completion->priv->actions, &iter);
1234 }
1235
1236 /**
1237  * gtk_entry_completion_set_text_column:
1238  * @completion: A #GtkEntryCompletion.
1239  * @column: The column in the model of @completion to get strings from.
1240  *
1241  * Convenience function for setting up the most used case of this code: a
1242  * completion list with just strings. This function will set up @completion
1243  * to have a list displaying all (and just) strings in the completion list,
1244  * and to get those strings from @column in the model of @completion.
1245  *
1246  * This functions creates and adds a #GtkCellRendererText for the selected 
1247  * column. If you need to set the text column, but don't want the cell 
1248  * renderer, use g_object_set() to set the ::text_column property directly.
1249  * 
1250  * Since: 2.4
1251  */
1252 void
1253 gtk_entry_completion_set_text_column (GtkEntryCompletion *completion,
1254                                       gint                column)
1255 {
1256   GtkCellRenderer *cell;
1257
1258   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1259   g_return_if_fail (column >= 0);
1260
1261   completion->priv->text_column = column;
1262
1263   cell = gtk_cell_renderer_text_new ();
1264   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion),
1265                               cell, TRUE);
1266   gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (completion),
1267                                  cell,
1268                                  "text", column);
1269
1270   g_object_notify (G_OBJECT (completion), "text-column");
1271 }
1272
1273 /**
1274  * gtk_entry_completion_get_text_column:
1275  * @completion: a #GtkEntryCompletion
1276  * 
1277  * Returns the column in the model of @completion to get strings from.
1278  * 
1279  * Return value: the column containing the strings
1280  *
1281  * Since: 2.6
1282  **/
1283 gint
1284 gtk_entry_completion_get_text_column (GtkEntryCompletion *completion)
1285 {
1286   g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), -1);
1287
1288   return completion->priv->text_column;  
1289 }
1290
1291 /* private */
1292
1293 static gboolean
1294 gtk_entry_completion_list_enter_notify (GtkWidget        *widget,
1295                                         GdkEventCrossing *event,
1296                                         gpointer          data)
1297 {
1298   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1299   
1300   return completion->priv->ignore_enter;
1301 }
1302
1303 static gboolean
1304 gtk_entry_completion_list_motion_notify (GtkWidget      *widget,
1305                                          GdkEventMotion *event,
1306                                          gpointer        data)
1307 {
1308   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1309
1310   completion->priv->ignore_enter = FALSE; 
1311   
1312   return FALSE;
1313 }
1314
1315
1316 /* some nasty size requisition */
1317 gboolean
1318 _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
1319 {
1320   gint x, y;
1321   gint matches, items, height, x_border, y_border;
1322   GdkScreen *screen;
1323   gint monitor_num;
1324   gint vertical_separator;
1325   GdkRectangle monitor;
1326   GtkRequisition popup_req;
1327   GtkRequisition entry_req;
1328   GtkTreePath *path;
1329   gboolean above;
1330   gint width;
1331
1332   if (!completion->priv->entry->window)
1333     return FALSE;
1334
1335   gdk_window_get_origin (completion->priv->entry->window, &x, &y);
1336   _gtk_entry_get_borders (GTK_ENTRY (completion->priv->entry), &x_border, &y_border);
1337
1338   matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
1339
1340   items = MIN (matches, 15);
1341
1342   gtk_tree_view_column_cell_get_size (completion->priv->column, NULL,
1343                                       NULL, NULL, NULL, &height);
1344
1345   gtk_widget_style_get (GTK_WIDGET (completion->priv->tree_view),
1346                         "vertical-separator", &vertical_separator,
1347                         NULL);
1348
1349   height += vertical_separator;
1350
1351   if (items <= 0)
1352     gtk_widget_hide (completion->priv->scrolled_window);
1353   else
1354     gtk_widget_show (completion->priv->scrolled_window);
1355
1356   screen = gtk_widget_get_screen (GTK_WIDGET (completion->priv->entry));
1357   monitor_num = gdk_screen_get_monitor_at_window (screen, 
1358                                                   GTK_WIDGET (completion->priv->entry)->window);
1359   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1360
1361   if (completion->priv->popup_set_width)
1362     width = MIN (completion->priv->entry->allocation.width, monitor.width) - 2 * x_border;
1363   else
1364     width = -1;
1365
1366   gtk_tree_view_columns_autosize (GTK_TREE_VIEW (completion->priv->tree_view));
1367   gtk_widget_set_size_request (completion->priv->tree_view, width, items * height);
1368
1369   /* default on no match */
1370   completion->priv->current_selected = -1;
1371
1372   items = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
1373
1374   if (items)
1375     {
1376       gtk_widget_show (completion->priv->action_view);
1377       gtk_widget_set_size_request (completion->priv->action_view, width, -1);
1378     }
1379   else
1380     gtk_widget_hide (completion->priv->action_view);
1381
1382   gtk_widget_size_request (completion->priv->popup_window, &popup_req);
1383   gtk_widget_size_request (completion->priv->entry, &entry_req);
1384   
1385   if (x < monitor.x)
1386     x = monitor.x;
1387   else if (x + popup_req.width > monitor.x + monitor.width)
1388     x = monitor.x + monitor.width - popup_req.width;
1389   
1390   if (y + entry_req.height + popup_req.height <= monitor.y + monitor.height ||
1391       y - monitor.y < (monitor.y + monitor.height) - (y + entry_req.height))
1392     {
1393       y += entry_req.height;
1394       above = FALSE;
1395     }
1396   else
1397     {
1398       y -= popup_req.height;
1399       above = TRUE;
1400     }
1401   
1402   if (matches > 0) 
1403     {
1404       path = gtk_tree_path_new_from_indices (above ? matches - 1 : 0, -1);
1405       gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (completion->priv->tree_view), path, 
1406                                     NULL, FALSE, 0.0, 0.0);
1407       gtk_tree_path_free (path);
1408     }
1409
1410   gtk_window_move (GTK_WINDOW (completion->priv->popup_window), x, y);
1411
1412   return above;
1413 }
1414
1415 void
1416 _gtk_entry_completion_popup (GtkEntryCompletion *completion)
1417 {
1418   GtkTreeViewColumn *column;
1419   GList *renderers;
1420   GtkWidget *toplevel;
1421
1422   if (GTK_WIDGET_MAPPED (completion->priv->popup_window))
1423     return;
1424
1425   if (!GTK_WIDGET_MAPPED (completion->priv->entry))
1426     return;
1427
1428   if (!GTK_WIDGET_HAS_FOCUS (completion->priv->entry))
1429     return;
1430
1431   completion->priv->ignore_enter = TRUE;
1432     
1433   column = gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0);
1434   renderers = gtk_tree_view_column_get_cell_renderers (column);
1435   gtk_widget_ensure_style (completion->priv->tree_view);
1436   g_object_set (GTK_CELL_RENDERER (renderers->data), "cell-background-gdk",
1437                 &completion->priv->tree_view->style->bg[GTK_STATE_NORMAL],
1438                 NULL);
1439   g_list_free (renderers);
1440
1441   gtk_widget_show_all (completion->priv->vbox);
1442
1443   _gtk_entry_completion_resize_popup (completion);
1444
1445   toplevel = gtk_widget_get_toplevel (completion->priv->entry);
1446   if (GTK_IS_WINDOW (toplevel))
1447     gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)), 
1448                                  GTK_WINDOW (completion->priv->popup_window));
1449
1450   /* prevent the first row being focused */
1451   gtk_widget_grab_focus (completion->priv->tree_view);
1452
1453   gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
1454   gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
1455   
1456
1457   gtk_widget_show (completion->priv->popup_window);
1458     
1459   gtk_grab_add (completion->priv->popup_window);
1460   gdk_pointer_grab (completion->priv->popup_window->window, TRUE,
1461                     GDK_BUTTON_PRESS_MASK |
1462                     GDK_BUTTON_RELEASE_MASK |
1463                     GDK_POINTER_MOTION_MASK,
1464                     NULL, NULL, GDK_CURRENT_TIME);
1465 }
1466
1467 void
1468 _gtk_entry_completion_popdown (GtkEntryCompletion *completion)
1469 {
1470   if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
1471     return;
1472
1473   completion->priv->ignore_enter = FALSE;
1474   
1475   gdk_pointer_ungrab (GDK_CURRENT_TIME);
1476   gtk_grab_remove (completion->priv->popup_window);
1477
1478   gtk_widget_hide (completion->priv->popup_window);
1479 }
1480
1481 static gboolean 
1482 gtk_entry_completion_match_selected (GtkEntryCompletion *completion,
1483                                      GtkTreeModel       *model,
1484                                      GtkTreeIter        *iter)
1485 {
1486   gchar *str = NULL;
1487
1488   gtk_tree_model_get (model, iter, completion->priv->text_column, &str, -1);
1489   gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), str);
1490   
1491   /* move cursor to the end */
1492   gtk_editable_set_position (GTK_EDITABLE (completion->priv->entry), -1);
1493   
1494   g_free (str);
1495
1496   return TRUE;
1497 }
1498
1499
1500 static gchar *
1501 gtk_entry_completion_compute_prefix (GtkEntryCompletion *completion)
1502 {
1503   GtkTreeIter iter;
1504   gchar *prefix = NULL;
1505   gboolean valid;
1506   const gchar *key;
1507
1508   if (completion->priv->text_column < 0)
1509     return NULL;
1510
1511   key = gtk_entry_get_text (GTK_ENTRY (completion->priv->entry));
1512
1513   valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (completion->priv->filter_model),
1514                                          &iter);
1515   
1516   while (valid)
1517     {
1518       gchar *text;
1519       
1520       gtk_tree_model_get (GTK_TREE_MODEL (completion->priv->filter_model),
1521                           &iter, completion->priv->text_column, &text,
1522                           -1);
1523
1524       if (g_str_has_prefix (text, key))
1525         {
1526           if (!prefix)
1527             prefix = g_strdup (text);
1528           else
1529             {
1530               gchar *p = prefix;
1531               const gchar *q = text;
1532               
1533               while (*p && *p == *q)
1534                 {
1535                   p++;
1536                   q++;
1537                 }
1538               
1539               *p = '\0';
1540             }
1541         }
1542       
1543       g_free (text);
1544       valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (completion->priv->filter_model),
1545                                         &iter);
1546     }
1547
1548   return prefix;
1549 }
1550
1551
1552 static gboolean
1553 gtk_entry_completion_real_insert_prefix (GtkEntryCompletion *completion,
1554                                          const gchar        *prefix)
1555 {
1556   if (prefix)
1557     {
1558       gint key_len;
1559       gint prefix_len;
1560       const gchar *key;
1561
1562       prefix_len = g_utf8_strlen (prefix, -1);
1563
1564       key = gtk_entry_get_text (GTK_ENTRY (completion->priv->entry));
1565       key_len = g_utf8_strlen (key, -1);
1566
1567       if (prefix_len > key_len)
1568         {
1569           gint pos = prefix_len;
1570
1571           gtk_editable_insert_text (GTK_EDITABLE (completion->priv->entry),
1572                                     prefix + strlen (key), -1, &pos);
1573           gtk_editable_select_region (GTK_EDITABLE (completion->priv->entry),
1574                                       key_len, prefix_len);
1575
1576           completion->priv->has_completion = TRUE;
1577         }
1578     }
1579
1580   return TRUE;
1581 }
1582
1583 /**
1584  * gtk_entry_completion_insert_prefix:
1585  * @completion: a #GtkEntryCompletion
1586  * 
1587  * Requests a prefix insertion. 
1588  * 
1589  * Since: 2.6
1590  **/
1591 void
1592 gtk_entry_completion_insert_prefix (GtkEntryCompletion *completion)
1593 {
1594   gboolean done;
1595   gchar *prefix;
1596
1597   g_signal_handler_block (completion->priv->entry,
1598                           completion->priv->insert_text_id);
1599   prefix = gtk_entry_completion_compute_prefix (completion);
1600   if (prefix)
1601     {
1602       g_signal_emit (completion, entry_completion_signals[INSERT_PREFIX],
1603                      0, prefix, &done);
1604       g_free (prefix);
1605     }
1606   g_signal_handler_unblock (completion->priv->entry,
1607                             completion->priv->insert_text_id);
1608 }
1609
1610 /**
1611  * gtk_entry_completion_set_inline_completion:
1612  * @completion: a #GtkEntryCompletion
1613  * @inline_completion: %TRUE to do inline completion
1614  * 
1615  * Sets whether the common prefix of the possible completions should
1616  * be automatically inserted in the entry.
1617  * 
1618  * Since: 2.6
1619  **/
1620 void 
1621 gtk_entry_completion_set_inline_completion (GtkEntryCompletion *completion,
1622                                             gboolean            inline_completion)
1623 {
1624   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1625   
1626   inline_completion = inline_completion != FALSE;
1627
1628   if (completion->priv->inline_completion != inline_completion)
1629     {
1630       completion->priv->inline_completion = inline_completion;
1631
1632       g_object_notify (G_OBJECT (completion), "inline-completion");
1633     }
1634 }
1635
1636
1637 /**
1638  * gtk_entry_completion_get_inline_completion:
1639  * @completion: a #GtkEntryCompletion
1640  * 
1641  * Returns whether the common prefix of the possible completions should
1642  * be automatically inserted in the entry.
1643  * 
1644  * Return value: %TRUE if inline completion is turned on
1645  * 
1646  * Since: 2.6
1647  **/
1648 gboolean
1649 gtk_entry_completion_get_inline_completion (GtkEntryCompletion *completion)
1650 {
1651   g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), FALSE);
1652   
1653   return completion->priv->inline_completion;
1654 }
1655
1656 /**
1657  * gtk_entry_completion_set_popup_completion:
1658  * @completion: a #GtkEntryCompletion
1659  * @popup_completion: %TRUE to do popup completion
1660  * 
1661  * Sets whether the completions should be presented in a popup window.
1662  * 
1663  * Since: 2.6
1664  **/
1665 void 
1666 gtk_entry_completion_set_popup_completion (GtkEntryCompletion *completion,
1667                                            gboolean            popup_completion)
1668 {
1669   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1670   
1671   popup_completion = popup_completion != FALSE;
1672
1673   if (completion->priv->popup_completion != popup_completion)
1674     {
1675       completion->priv->popup_completion = popup_completion;
1676
1677       g_object_notify (G_OBJECT (completion), "popup-completion");
1678     }
1679 }
1680
1681
1682 /**
1683  * gtk_entry_completion_get_popup_completion:
1684  * @completion: a #GtkEntryCompletion
1685  * 
1686  * Returns whether the completions should be presented in a popup window.
1687  * 
1688  * Return value: %TRUE if popup completion is turned on
1689  * 
1690  * Since: 2.6
1691  **/
1692 gboolean
1693 gtk_entry_completion_get_popup_completion (GtkEntryCompletion *completion)
1694 {
1695   g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), TRUE);
1696   
1697   return completion->priv->popup_completion;
1698 }
1699
1700 /**
1701  * gtk_entry_completion_set_popup_set_width:
1702  * @completion: a #GtkEntryCompletion
1703  * @popup_set_width: %TRUE to make the width of the popup the same as the entry
1704  *
1705  * Sets whether the completion popup window will be resized to be the same
1706  * width as the entry.
1707  *
1708  * Since: 2.8
1709  */
1710 void 
1711 gtk_entry_completion_set_popup_set_width (GtkEntryCompletion *completion,
1712                                           gboolean            popup_set_width)
1713 {
1714   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1715   
1716   popup_set_width = popup_set_width != FALSE;
1717
1718   if (completion->priv->popup_set_width != popup_set_width)
1719     {
1720       completion->priv->popup_set_width = popup_set_width;
1721
1722       g_object_notify (G_OBJECT (completion), "popup-set-width");
1723     }
1724 }
1725
1726 /**
1727  * gtk_entry_completion_get_popup_set_width:
1728  * @completion: a #GtkEntryCompletion
1729  * 
1730  * Returns whether the  completion popup window will be resized to the 
1731  * width of the entry.
1732  * 
1733  * Return value: %TRUE if the popup window will be resized to the width of 
1734  *   the entry
1735  * 
1736  * Since: 2.8
1737  **/
1738 gboolean
1739 gtk_entry_completion_get_popup_set_width (GtkEntryCompletion *completion)
1740 {
1741   g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), TRUE);
1742   
1743   return completion->priv->popup_set_width;
1744 }
1745
1746
1747 /**
1748  * gtk_entry_completion_set_popup_single_match:
1749  * @completion: a #GtkEntryCompletion
1750  * @popup_single_match: %TRUE if the popup should appear even for a single
1751  *   match
1752  *
1753  * Sets whether the completion popup window will appear even if there is
1754  * only a single match. You may want to set this to %FALSE if you
1755  * are using <link linkend="GtkEntryCompletion--inline-completion">inline
1756  * completion</link>.
1757  *
1758  * Since: 2.8
1759  */
1760 void 
1761 gtk_entry_completion_set_popup_single_match (GtkEntryCompletion *completion,
1762                                              gboolean            popup_single_match)
1763 {
1764   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1765   
1766   popup_single_match = popup_single_match != FALSE;
1767
1768   if (completion->priv->popup_single_match != popup_single_match)
1769     {
1770       completion->priv->popup_single_match = popup_single_match;
1771
1772       g_object_notify (G_OBJECT (completion), "popup-single-match");
1773     }
1774 }
1775
1776 /**
1777  * gtk_entry_completion_get_popup_single_match:
1778  * @completion: a #GtkEntryCompletion
1779  * 
1780  * Returns whether the completion popup window will appear even if there is
1781  * only a single match. 
1782  * 
1783  * Return value: %TRUE if the popup window will appear regardless of the
1784  *    number of matches.
1785  * 
1786  * Since: 2.8
1787  **/
1788 gboolean
1789 gtk_entry_completion_get_popup_single_match (GtkEntryCompletion *completion)
1790 {
1791   g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), TRUE);
1792   
1793   return completion->priv->popup_single_match;
1794 }
1795
1796
1797 #define __GTK_ENTRY_COMPLETION_C__
1798 #include "gtkaliasdef.c"