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