]> Pileus Git - ~andy/gtk/blob - gtk/gtkentrycompletion.c
Fixes #136082 and #135265, patch by Morten Welinder.
[~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 "gtktreeselection.h"
28 #include "gtktreeview.h"
29 #include "gtkscrolledwindow.h"
30 #include "gtkvbox.h"
31 #include "gtkwindow.h"
32 #include "gtkentry.h"
33 #include "gtkmain.h"
34 #include "gtksignal.h"
35 #include "gtkmarshalers.h"
36
37 #include <string.h>
38
39
40 /* signals */
41 enum
42 {
43   MATCH_SELECTED,
44   ACTION_ACTIVATED,
45   LAST_SIGNAL
46 };
47
48 /* properties */
49 enum
50 {
51   PROP_0,
52   PROP_MODEL,
53   PROP_MINIMUM_KEY_LENGTH
54 };
55
56 #define GTK_ENTRY_COMPLETION_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ENTRY_COMPLETION, GtkEntryCompletionPrivate))
57
58 static void     gtk_entry_completion_class_init          (GtkEntryCompletionClass *klass);
59 static void     gtk_entry_completion_cell_layout_init    (GtkCellLayoutIface      *iface);
60 static void     gtk_entry_completion_init                (GtkEntryCompletion      *completion);
61 static void     gtk_entry_completion_set_property        (GObject                 *object,
62                                                           guint                    prop_id,
63                                                           const GValue            *value,
64                                                           GParamSpec              *pspec);
65 static void     gtk_entry_completion_get_property        (GObject                 *object,
66                                                           guint                    prop_id,
67                                                           GValue                  *value,
68                                                           GParamSpec              *pspec);
69 static void     gtk_entry_completion_finalize            (GObject                 *object);
70
71 static void     gtk_entry_completion_pack_start          (GtkCellLayout           *cell_layout,
72                                                           GtkCellRenderer         *cell,
73                                                           gboolean                 expand);
74 static void     gtk_entry_completion_pack_end            (GtkCellLayout           *cell_layout,
75                                                           GtkCellRenderer         *cell,
76                                                           gboolean                 expand);
77 static void     gtk_entry_completion_clear               (GtkCellLayout           *cell_layout);
78 static void     gtk_entry_completion_add_attribute       (GtkCellLayout           *cell_layout,
79                                                           GtkCellRenderer         *cell,
80                                                           const char              *attribute,
81                                                           gint                     column);
82 static void     gtk_entry_completion_set_cell_data_func  (GtkCellLayout           *cell_layout,
83                                                           GtkCellRenderer         *cell,
84                                                           GtkCellLayoutDataFunc    func,
85                                                           gpointer                 func_data,
86                                                           GDestroyNotify           destroy);
87 static void     gtk_entry_completion_clear_attributes    (GtkCellLayout           *cell_layout,
88                                                           GtkCellRenderer         *cell);
89 static void     gtk_entry_completion_reorder             (GtkCellLayout           *cell_layout,
90                                                           GtkCellRenderer         *cell,
91                                                           gint                     position);
92
93 static gboolean gtk_entry_completion_visible_func        (GtkTreeModel            *model,
94                                                           GtkTreeIter             *iter,
95                                                           gpointer                 data);
96 static gboolean gtk_entry_completion_popup_key_press     (GtkWidget               *widget,
97                                                           GdkEventKey             *event,
98                                                           gpointer                 user_data);
99 static gboolean gtk_entry_completion_popup_button_press  (GtkWidget               *widget,
100                                                           GdkEventButton          *event,
101                                                           gpointer                 user_data);
102 static gboolean gtk_entry_completion_list_button_press   (GtkWidget               *widget,
103                                                           GdkEventButton          *event,
104                                                           gpointer                 user_data);
105 static gboolean gtk_entry_completion_action_button_press (GtkWidget               *widget,
106                                                           GdkEventButton          *event,
107                                                           gpointer                 user_data);
108 static void     gtk_entry_completion_selection_changed   (GtkTreeSelection        *selection,
109                                                           gpointer                 data);
110
111 static void     gtk_entry_completion_insert_action       (GtkEntryCompletion      *completion,
112                                                           gint                     index,
113                                                           const gchar             *string,
114                                                           gboolean                 markup);
115 static void     gtk_entry_completion_action_data_func    (GtkTreeViewColumn       *tree_column,
116                                                           GtkCellRenderer         *cell,
117                                                           GtkTreeModel            *model,
118                                                           GtkTreeIter             *iter,
119                                                           gpointer                 data);
120
121
122 static GObjectClass *parent_class = NULL;
123 static guint entry_completion_signals[LAST_SIGNAL] = { 0 };
124
125
126 GType
127 gtk_entry_completion_get_type (void)
128 {
129   static GType entry_completion_type = 0;
130
131   if (!entry_completion_type)
132     {
133       static const GTypeInfo entry_completion_info =
134       {
135         sizeof (GtkEntryCompletionClass),
136         NULL,
137         NULL,
138         (GClassInitFunc) gtk_entry_completion_class_init,
139         NULL,
140         NULL,
141         sizeof (GtkEntryCompletion),
142         0,
143         (GInstanceInitFunc) gtk_entry_completion_init
144       };
145
146       static const GInterfaceInfo cell_layout_info =
147       {
148         (GInterfaceInitFunc) gtk_entry_completion_cell_layout_init,
149         NULL,
150         NULL
151       };
152
153       entry_completion_type =
154         g_type_register_static (G_TYPE_OBJECT, "GtkEntryCompletion",
155                                 &entry_completion_info, 0);
156
157       g_type_add_interface_static (entry_completion_type,
158                                    GTK_TYPE_CELL_LAYOUT,
159                                    &cell_layout_info);
160     }
161
162   return entry_completion_type;
163 }
164
165 static void
166 gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
167 {
168   GObjectClass *object_class;
169
170   parent_class = g_type_class_peek_parent (klass);
171   object_class = (GObjectClass *)klass;
172
173   object_class->set_property = gtk_entry_completion_set_property;
174   object_class->get_property = gtk_entry_completion_get_property;
175   object_class->finalize = gtk_entry_completion_finalize;
176
177   entry_completion_signals[MATCH_SELECTED] =
178     g_signal_new ("match_selected",
179                   G_TYPE_FROM_CLASS (klass),
180                   G_SIGNAL_RUN_LAST,
181                   G_STRUCT_OFFSET (GtkEntryCompletionClass, match_selected),
182                   _gtk_boolean_handled_accumulator, NULL,
183                   _gtk_marshal_BOOLEAN__OBJECT_BOXED,
184                   G_TYPE_BOOLEAN, 2,
185                   GTK_TYPE_TREE_MODEL,
186                   GTK_TYPE_TREE_ITER);
187   entry_completion_signals[ACTION_ACTIVATED] =
188     g_signal_new ("action_activated",
189                   G_TYPE_FROM_CLASS (klass),
190                   G_SIGNAL_RUN_LAST,
191                   G_STRUCT_OFFSET (GtkEntryCompletionClass, action_activated),
192                   NULL, NULL,
193                   _gtk_marshal_NONE__INT,
194                   G_TYPE_NONE, 1,
195                   G_TYPE_INT);
196
197   g_object_class_install_property (object_class,
198                                    PROP_MODEL,
199                                    g_param_spec_object ("model",
200                                                         P_("Completion Model"),
201                                                         P_("The model to find matches in"),
202                                                         GTK_TYPE_TREE_MODEL,
203                                                         G_PARAM_READWRITE));
204   g_object_class_install_property (object_class,
205                                    PROP_MINIMUM_KEY_LENGTH,
206                                    g_param_spec_int ("minimum_key_length",
207                                                      P_("Minimum Key Length"),
208                                                      P_("Minimum length of the search key in order to look up matches"),
209                                                      -1,
210                                                      G_MAXINT,
211                                                      1,
212                                                      G_PARAM_READWRITE));
213
214   g_type_class_add_private (object_class, sizeof (GtkEntryCompletionPrivate));
215 }
216
217 static void
218 gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface)
219 {
220   iface->pack_start = gtk_entry_completion_pack_start;
221   iface->pack_end = gtk_entry_completion_pack_end;
222   iface->clear = gtk_entry_completion_clear;
223   iface->add_attribute = gtk_entry_completion_add_attribute;
224   iface->set_cell_data_func = gtk_entry_completion_set_cell_data_func;
225   iface->clear_attributes = gtk_entry_completion_clear_attributes;
226   iface->reorder = gtk_entry_completion_reorder;
227 }
228
229 static void
230 gtk_entry_completion_init (GtkEntryCompletion *completion)
231 {
232   GtkCellRenderer *cell;
233   GtkTreeSelection *sel;
234   GtkEntryCompletionPrivate *priv;
235
236   /* yes, also priv, need to keep the code readable */
237   priv = completion->priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (completion);
238
239   priv->minimum_key_length = 1;
240   priv->text_column = -1;
241
242   /* completions */
243   priv->filter_model = NULL;
244
245   priv->tree_view = gtk_tree_view_new ();
246   g_signal_connect (priv->tree_view, "button_press_event",
247                     G_CALLBACK (gtk_entry_completion_list_button_press),
248                     completion);
249   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
250
251   sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
252   gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
253   gtk_tree_selection_unselect_all (sel);
254   g_signal_connect (sel, "changed",
255                     G_CALLBACK (gtk_entry_completion_selection_changed),
256                     completion);
257   priv->first_sel_changed = TRUE;
258
259   priv->column = gtk_tree_view_column_new ();
260   gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
261
262   priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
263   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
264                                   GTK_POLICY_NEVER,
265                                   GTK_POLICY_AUTOMATIC);
266   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
267                                        GTK_SHADOW_ETCHED_IN);
268
269   /* a nasty hack to get the completions treeview to size nicely */
270   gtk_widget_set_size_request (GTK_SCROLLED_WINDOW (priv->scrolled_window)->vscrollbar, -1, 0);
271
272   /* actions */
273   priv->actions = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_BOOLEAN);
274
275   priv->action_view =
276     gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->actions));
277   g_signal_connect (priv->action_view, "button_press_event",
278                     G_CALLBACK (gtk_entry_completion_action_button_press),
279                     completion);
280   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->action_view), FALSE);
281
282   sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->action_view));
283   gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
284   gtk_tree_selection_unselect_all (sel);
285
286   cell = gtk_cell_renderer_text_new ();
287   g_object_set (cell, "cell_background_gdk",
288                 &priv->tree_view->style->bg[GTK_STATE_NORMAL],
289                 NULL);
290   gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (priv->action_view),
291                                               0, "",
292                                               cell,
293                                               gtk_entry_completion_action_data_func,
294                                               NULL,
295                                               NULL);
296
297   /* pack it all */
298   priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
299   gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
300   g_signal_connect (priv->popup_window, "key_press_event",
301                     G_CALLBACK (gtk_entry_completion_popup_key_press),
302                     completion);
303   g_signal_connect (priv->popup_window, "button_press_event",
304                     G_CALLBACK (gtk_entry_completion_popup_button_press),
305                     completion);
306
307   priv->vbox = gtk_vbox_new (FALSE, 0);
308   gtk_container_add (GTK_CONTAINER (priv->popup_window), priv->vbox);
309
310   gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->tree_view);
311   gtk_box_pack_start (GTK_BOX (priv->vbox), priv->scrolled_window,
312                       TRUE, TRUE, 0);
313
314   /* we don't want to see the action treeview when no actions have
315    * been inserted, so we pack the action treeview after the first
316    * action has been added
317    */
318 }
319
320 static void
321 gtk_entry_completion_set_property (GObject      *object,
322                                    guint         prop_id,
323                                    const GValue *value,
324                                    GParamSpec   *pspec)
325 {
326   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
327
328   switch (prop_id)
329     {
330       case PROP_MODEL:
331         gtk_entry_completion_set_model (completion,
332                                         g_value_get_object (value));
333         break;
334
335       case PROP_MINIMUM_KEY_LENGTH:
336         gtk_entry_completion_set_minimum_key_length (completion,
337                                                      g_value_get_int (value));
338         break;
339
340       default:
341         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
342         break;
343     }
344 }
345
346 static void
347 gtk_entry_completion_get_property (GObject    *object,
348                                    guint       prop_id,
349                                    GValue     *value,
350                                    GParamSpec *pspec)
351 {
352   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
353
354   switch (prop_id)
355     {
356       case PROP_MODEL:
357         g_value_set_object (value,
358                             gtk_entry_completion_get_model (completion));
359         break;
360
361       case PROP_MINIMUM_KEY_LENGTH:
362         g_value_set_int (value, gtk_entry_completion_get_minimum_key_length (completion));
363         break;
364
365       default:
366         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
367         break;
368     }
369 }
370
371 static void
372 gtk_entry_completion_finalize (GObject *object)
373 {
374   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (object);
375
376   if (completion->priv->tree_view)
377     gtk_widget_destroy (completion->priv->tree_view);
378
379   if (completion->priv->entry)
380     gtk_entry_set_completion (GTK_ENTRY (completion->priv->entry), NULL);
381
382   if (completion->priv->actions)
383     g_object_unref (completion->priv->actions);
384
385   if (completion->priv->case_normalized_key)
386     g_free (completion->priv->case_normalized_key);
387
388   if (completion->priv->popup_window)
389     gtk_widget_destroy (completion->priv->popup_window);
390
391   G_OBJECT_CLASS (parent_class)->finalize (object);
392 }
393
394 /* implement cell layout interface */
395 static void
396 gtk_entry_completion_pack_start (GtkCellLayout   *cell_layout,
397                                  GtkCellRenderer *cell,
398                                  gboolean         expand)
399 {
400   GtkEntryCompletionPrivate *priv;
401
402   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
403
404   priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
405
406   gtk_tree_view_column_pack_start (priv->column, cell, expand);
407 }
408
409 static void
410 gtk_entry_completion_pack_end (GtkCellLayout   *cell_layout,
411                                GtkCellRenderer *cell,
412                                gboolean         expand)
413 {
414   GtkEntryCompletionPrivate *priv;
415
416   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
417
418   priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
419
420   gtk_tree_view_column_pack_end (priv->column, cell, expand);
421 }
422
423 static void
424 gtk_entry_completion_clear (GtkCellLayout *cell_layout)
425 {
426   GtkEntryCompletionPrivate *priv;
427
428   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
429
430   priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
431
432   gtk_tree_view_column_clear (priv->column);
433 }
434
435 static void
436 gtk_entry_completion_add_attribute (GtkCellLayout   *cell_layout,
437                                     GtkCellRenderer *cell,
438                                     const gchar     *attribute,
439                                     gint             column)
440 {
441   GtkEntryCompletionPrivate *priv;
442
443   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
444
445   priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
446
447   gtk_tree_view_column_add_attribute (priv->column, cell, attribute, column);
448 }
449
450 static void
451 gtk_entry_completion_set_cell_data_func (GtkCellLayout          *cell_layout,
452                                          GtkCellRenderer        *cell,
453                                          GtkCellLayoutDataFunc   func,
454                                          gpointer                func_data,
455                                          GDestroyNotify          destroy)
456 {
457   GtkEntryCompletionPrivate *priv;
458
459   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
460
461   priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
462
463   gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column),
464                                       cell, func, func_data, destroy);
465 }
466
467 static void
468 gtk_entry_completion_clear_attributes (GtkCellLayout   *cell_layout,
469                                        GtkCellRenderer *cell)
470 {
471   GtkEntryCompletionPrivate *priv;
472
473   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
474
475   priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
476
477   gtk_tree_view_column_clear_attributes (priv->column, cell);
478 }
479
480 static void
481 gtk_entry_completion_reorder (GtkCellLayout   *cell_layout,
482                               GtkCellRenderer *cell,
483                               gint             position)
484 {
485   GtkEntryCompletionPrivate *priv;
486
487   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
488
489   priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
490
491   gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column), cell, position);
492 }
493
494 /* all those callbacks */
495 static gboolean
496 gtk_entry_completion_default_completion_func (GtkEntryCompletion *completion,
497                                               const gchar        *key,
498                                               GtkTreeIter        *iter,
499                                               gpointer            user_data)
500 {
501   gchar *item = NULL;
502   gchar *normalized_string;
503   gchar *case_normalized_string;
504
505   gboolean ret = FALSE;
506
507   GtkTreeModel *model;
508
509   model = gtk_tree_model_filter_get_model (completion->priv->filter_model);
510
511   gtk_tree_model_get (model, iter,
512                       completion->priv->text_column, &item,
513                       -1);
514
515   if (item != NULL)
516     {
517       normalized_string = g_utf8_normalize (item, -1, G_NORMALIZE_ALL);
518       case_normalized_string = g_utf8_casefold (normalized_string, -1);
519       
520       if (!strncmp (key, case_normalized_string, strlen (key)))
521         ret = TRUE;
522       
523       g_free (item);
524       g_free (normalized_string);
525       g_free (case_normalized_string);
526     }
527
528   return ret;
529 }
530
531 static gboolean
532 gtk_entry_completion_visible_func (GtkTreeModel *model,
533                                    GtkTreeIter  *iter,
534                                    gpointer      data)
535 {
536   gboolean ret = FALSE;
537
538   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
539
540   if (!completion->priv->case_normalized_key)
541     return ret;
542
543   if (completion->priv->text_column >= 0)
544     ret = gtk_entry_completion_default_completion_func (completion,
545                                                         completion->priv->case_normalized_key,
546                                                         iter,
547                                                         NULL);
548
549   else if (completion->priv->match_func)
550     ret = (* completion->priv->match_func) (completion,
551                                             completion->priv->case_normalized_key,
552                                             iter,
553                                             completion->priv->match_data);
554
555   return ret;
556 }
557
558 static gboolean
559 gtk_entry_completion_popup_key_press (GtkWidget   *widget,
560                                       GdkEventKey *event,
561                                       gpointer     user_data)
562 {
563   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
564
565   if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
566     return FALSE;
567
568   /* propagate event to the entry */
569   gtk_widget_event (completion->priv->entry, (GdkEvent *)event);
570
571   return TRUE;
572 }
573
574 static gboolean
575 gtk_entry_completion_popup_button_press (GtkWidget      *widget,
576                                          GdkEventButton *event,
577                                          gpointer        user_data)
578 {
579   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
580
581   if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
582     return FALSE;
583
584   /* if we come here, it's usually time to popdown */
585   _gtk_entry_completion_popdown (completion);
586
587   return TRUE;
588 }
589
590 static gboolean
591 gtk_entry_completion_list_button_press (GtkWidget      *widget,
592                                         GdkEventButton *event,
593                                         gpointer        user_data)
594 {
595   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
596   GtkTreePath *path = NULL;
597
598   if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
599     return FALSE;
600
601   if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
602                                      event->x, event->y,
603                                      &path, NULL, NULL, NULL))
604     {
605       gboolean entry_set;
606       GtkTreeIter iter;
607
608       gtk_tree_model_get_iter (GTK_TREE_MODEL (completion->priv->filter_model),
609                                &iter, path);
610       gtk_tree_path_free (path);
611
612       g_signal_emit (completion, entry_completion_signals[MATCH_SELECTED],
613                      0, GTK_TREE_MODEL (completion->priv->filter_model),
614                      &iter, &entry_set);
615
616       if (!entry_set)
617         {
618           gchar *str = NULL;
619
620           gtk_tree_model_get (GTK_TREE_MODEL (completion->priv->filter_model),
621                               &iter,
622                               completion->priv->text_column, &str,
623                               -1);
624
625           g_signal_handler_block (completion->priv->entry,
626                                   completion->priv->changed_id);
627           gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), str);
628           g_signal_handler_unblock (completion->priv->entry,
629                                     completion->priv->changed_id);
630
631           /* move cursor to the end */
632           gtk_editable_set_position (GTK_EDITABLE (completion->priv->entry),
633                                      -1);
634
635           g_free (str);
636         }
637
638       _gtk_entry_completion_popdown (completion);
639       return TRUE;
640     }
641
642   return FALSE;
643 }
644
645 static gboolean
646 gtk_entry_completion_action_button_press (GtkWidget      *widget,
647                                           GdkEventButton *event,
648                                           gpointer        user_data)
649 {
650   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
651   GtkTreePath *path = NULL;
652
653   if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
654     return FALSE;
655
656   if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
657                                      event->x, event->y,
658                                      &path, NULL, NULL, NULL))
659     {
660       g_signal_emit (completion, entry_completion_signals[ACTION_ACTIVATED],
661                      0, gtk_tree_path_get_indices (path)[0]);
662       gtk_tree_path_free (path);
663
664       _gtk_entry_completion_popdown (completion);
665       return TRUE;
666     }
667
668   return FALSE;
669 }
670
671 static void
672 gtk_entry_completion_action_data_func (GtkTreeViewColumn *tree_column,
673                                        GtkCellRenderer   *cell,
674                                        GtkTreeModel      *model,
675                                        GtkTreeIter       *iter,
676                                        gpointer           data)
677 {
678   gchar *string = NULL;
679   gboolean markup;
680
681   gtk_tree_model_get (model, iter,
682                       0, &string,
683                       1, &markup,
684                       -1);
685
686   if (!string)
687     return;
688
689   if (markup)
690     g_object_set (G_OBJECT (cell),
691                   "text", NULL,
692                   "markup", string,
693                   NULL);
694   else
695     g_object_set (G_OBJECT (cell),
696                   "markup", NULL,
697                   "text", string,
698                   NULL);
699
700   g_free (string);
701 }
702
703 static void
704 gtk_entry_completion_selection_changed (GtkTreeSelection *selection,
705                                         gpointer          data)
706 {
707   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
708
709   if (completion->priv->first_sel_changed)
710     {
711       completion->priv->first_sel_changed = FALSE;
712       if (gtk_widget_is_focus (completion->priv->tree_view))
713         gtk_tree_selection_unselect_all (selection);
714     }
715 }
716
717 /* public API */
718
719 /**
720  * gtk_entry_completion_new:
721  *
722  * Creates a new #GtkEntryCompletion object.
723  *
724  * Return value: A newly created #GtkEntryCompletion object.
725  *
726  * Since: 2.4
727  */
728 GtkEntryCompletion *
729 gtk_entry_completion_new (void)
730 {
731   GtkEntryCompletion *completion;
732
733   completion = g_object_new (GTK_TYPE_ENTRY_COMPLETION, NULL);
734
735   return completion;
736 }
737
738 /**
739  * gtk_entry_completion_get_entry:
740  * @completion: A #GtkEntryCompletion.
741  *
742  * Gets the entry @completion has been attached to.
743  *
744  * Return value: The entry @completion has been attached to.
745  *
746  * Since: 2.4
747  */
748 GtkWidget *
749 gtk_entry_completion_get_entry (GtkEntryCompletion *completion)
750 {
751   g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
752
753   return completion->priv->entry;
754 }
755
756 /**
757  * gtk_entry_completion_set_model:
758  * @completion: A #GtkEntryCompletion.
759  * @model: The #GtkTreeModel.
760  *
761  * Sets the model for a #GtkEntryCompletion. If @completion already has
762  * a model set, it will remove it before setting the new model.
763  *
764  * Since: 2.4
765  */
766 void
767 gtk_entry_completion_set_model (GtkEntryCompletion *completion,
768                                 GtkTreeModel       *model)
769 {
770   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
771   g_return_if_fail (GTK_IS_TREE_MODEL (model));
772
773   /* code will unref the old filter model (if any) */
774   completion->priv->filter_model =
775     GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
776   gtk_tree_model_filter_set_visible_func (completion->priv->filter_model,
777                                           gtk_entry_completion_visible_func,
778                                           completion,
779                                           NULL);
780   gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
781                            GTK_TREE_MODEL (completion->priv->filter_model));
782   g_object_unref (G_OBJECT (completion->priv->filter_model));
783 }
784
785 /**
786  * gtk_entry_completion_get_model:
787  * @completion: A #GtkEntryCompletion.
788  *
789  * Returns the model the #GtkEntryCompletion is using as data source.
790  * Returns %NULL if the model is unset.
791  *
792  * Return value: A #GtkTreeModel, or %NULL if none is currently being used.
793  *
794  * Since: 2.4
795  */
796 GtkTreeModel *
797 gtk_entry_completion_get_model (GtkEntryCompletion *completion)
798 {
799   g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
800
801   return gtk_tree_model_filter_get_model (completion->priv->filter_model);
802 }
803
804 /**
805  * gtk_entry_completion_set_match_func:
806  * @completion: A #GtkEntryCompletion.
807  * @func: The #GtkEntryCompletionMatchFunc to use.
808  * @func_data: The user data for @func.
809  * @func_notify: Destroy notifier for @func_data.
810  *
811  * Sets the match function for @completion to be @func. The match function
812  * is used to determine if a row should or should not be in the completion
813  * list.
814  *
815  * Since: 2.4.
816  */
817 void
818 gtk_entry_completion_set_match_func (GtkEntryCompletion          *completion,
819                                      GtkEntryCompletionMatchFunc  func,
820                                      gpointer                     func_data,
821                                      GDestroyNotify               func_notify)
822 {
823   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
824
825   if (completion->priv->match_notify)
826     (* completion->priv->match_notify) (completion->priv->match_data);
827
828   completion->priv->match_func = func;
829   completion->priv->match_data = func_data;
830   completion->priv->match_notify = func_notify;
831 }
832
833 /**
834  * gtk_entry_completion_set_minimum_key_length:
835  * @completion: A #GtkEntryCompletion.
836  * @length: The minimum length of the key in order to start completing.
837  *
838  * Requires the length of the search key for @completion to be at least
839  * @length. This is useful for long lists, where completing using a small
840  * key takes a lot of time and will come up with meaningless results anyway
841  * (ie, a too large dataset).
842  *
843  * Since: 2.4
844  */
845 void
846 gtk_entry_completion_set_minimum_key_length (GtkEntryCompletion *completion,
847                                              gint                length)
848 {
849   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
850   g_return_if_fail (length >= 1);
851
852   completion->priv->minimum_key_length = length;
853 }
854
855 /**
856  * gtk_entry_completion_get_minimum_key_length:
857  * @completion: A #GtkEntryCompletion.
858  *
859  * Returns the minimum key length as set for @completion.
860  *
861  * Return value: The currently used minimum key length.
862  *
863  * Since: 2.4
864  */
865 gint
866 gtk_entry_completion_get_minimum_key_length (GtkEntryCompletion *completion)
867 {
868   g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), 0);
869
870   return completion->priv->minimum_key_length;
871 }
872
873 /**
874  * gtk_entry_completion_complete:
875  * @completion: A #GtkEntryCompletion.
876  *
877  * Requests a completion operation, or in other words a refiltering of the
878  * current list with completions, using the current key. The completion list
879  * view will be updated accordingly.
880  *
881  * Since: 2.4
882  */
883 void
884 gtk_entry_completion_complete (GtkEntryCompletion *completion)
885 {
886   gchar *tmp;
887
888   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
889   g_return_if_fail (completion->priv->filter_model != NULL);
890
891   if (completion->priv->case_normalized_key)
892     g_free (completion->priv->case_normalized_key);
893
894   tmp = g_utf8_normalize (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)),
895                           -1, G_NORMALIZE_ALL);
896   completion->priv->case_normalized_key = g_utf8_casefold (tmp, -1);
897   g_free (tmp);
898
899   gtk_tree_model_filter_refilter (completion->priv->filter_model);
900 }
901
902 static void
903 gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
904                                     gint                index,
905                                     const gchar        *string,
906                                     gboolean            markup)
907 {
908   GtkTreeIter iter;
909
910   gtk_list_store_insert (completion->priv->actions, &iter, index);
911   gtk_list_store_set (completion->priv->actions, &iter,
912                       0, string,
913                       1, markup,
914                       -1);
915
916   if (!completion->priv->action_view->parent)
917     {
918       GtkTreePath *path = gtk_tree_path_new_from_indices (0, -1);
919
920       gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
921                                 path, NULL, FALSE);
922       gtk_tree_path_free (path);
923
924       gtk_box_pack_start (GTK_BOX (completion->priv->vbox),
925                           completion->priv->action_view, FALSE, FALSE, 0);
926       gtk_widget_show (completion->priv->action_view);
927     }
928 }
929
930 /**
931  * gtk_entry_completion_insert_action_text:
932  * @completion: A #GtkEntryCompletion.
933  * @index: The index of the item to insert.
934  * @text: Text of the item to insert.
935  *
936  * Inserts an action in @completion's action item list at position @index
937  * with text @text. If you want the action item to have markup, use
938  * gtk_entry_completion_insert_action_markup().
939  *
940  * Since: 2.4
941  */
942 void
943 gtk_entry_completion_insert_action_text (GtkEntryCompletion *completion,
944                                          gint                index,
945                                          const gchar        *text)
946 {
947   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
948   g_return_if_fail (text != NULL);
949
950   gtk_entry_completion_insert_action (completion, index, text, FALSE);
951 }
952
953 /**
954  * gtk_entry_completion_insert_action_markup:
955  * @completion: A #GtkEntryCompletion.
956  * @index: The index of the item to insert.
957  * @markup: Markup of the item to insert.
958  *
959  * Inserts an action in @completion's action item list at position @index
960  * with markup @markup.
961  *
962  * Since: 2.4
963  */
964 void
965 gtk_entry_completion_insert_action_markup (GtkEntryCompletion *completion,
966                                            gint                index,
967                                            const gchar        *markup)
968 {
969   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
970   g_return_if_fail (markup != NULL);
971
972   gtk_entry_completion_insert_action (completion, index, markup, TRUE);
973 }
974
975 /**
976  * gtk_entry_completion_delete_action:
977  * @completion: A #GtkEntryCompletion.
978  * @index: The index of the item to Delete.
979  *
980  * Deletes the action at @index from @completion's action list.
981  *
982  * Since: 2.4
983  */
984 void
985 gtk_entry_completion_delete_action (GtkEntryCompletion *completion,
986                                     gint                index)
987 {
988   GtkTreeIter iter;
989
990   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
991   g_return_if_fail (index >= 0);
992
993   gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (completion->priv->actions),
994                                  &iter, NULL, index);
995   gtk_list_store_remove (completion->priv->actions, &iter);
996 }
997
998 /**
999  * gtk_entry_completion_set_text_column:
1000  * @completion: A #GtkEntryCompletion.
1001  * @column: The column in the model of @completion to get strings from.
1002  *
1003  * Convenience function for setting up the most used case of this code: a
1004  * completion list with just strings. This function will set up @completion
1005  * to have a list displaying all (and just) strings in the completion list,
1006  * and to get those strings from @column in the model of @completion.
1007  *
1008  * This functions creates and adds a GtkCellRendererText for the selected column.
1009
1010  * Since: 2.4
1011  */
1012 void
1013 gtk_entry_completion_set_text_column (GtkEntryCompletion *completion,
1014                                       gint                column)
1015 {
1016   GtkCellRenderer *cell;
1017
1018   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1019   g_return_if_fail (column >= 0);
1020
1021   completion->priv->text_column = column;
1022
1023   cell = gtk_cell_renderer_text_new ();
1024   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion),
1025                               cell, TRUE);
1026   gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (completion),
1027                                  cell,
1028                                  "text", column);
1029 }
1030
1031 /* private */
1032
1033 /* lame copy from gtkentry.c */
1034 static void
1035 get_borders (GtkEntry *entry,
1036              gint     *xborder,
1037              gint     *yborder)
1038 {
1039   GtkWidget *widget = GTK_WIDGET (entry);
1040   gint focus_width;
1041   gboolean interior_focus;
1042
1043   gtk_widget_style_get (widget,
1044                         "interior-focus", &interior_focus,
1045                         "focus-line-width", &focus_width,
1046                         NULL);
1047
1048   if (entry->has_frame)
1049     {
1050       *xborder = widget->style->xthickness;
1051       *yborder = widget->style->ythickness;
1052     }
1053   else
1054     {
1055       *xborder = 0;
1056       *yborder = 0;
1057     }
1058
1059   if (!interior_focus)
1060     {
1061       *xborder += focus_width;
1062       *yborder += focus_width;
1063     }
1064 }
1065
1066 /* some nasty size requisition */
1067 gint
1068 _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
1069 {
1070   gint items, height, x_border, y_border;
1071
1072   get_borders (GTK_ENTRY (completion->priv->entry), &x_border, &y_border);
1073
1074   items = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
1075
1076   items = MIN (items, 15);
1077
1078   gtk_tree_view_column_cell_get_size (completion->priv->column, NULL,
1079                                       NULL, NULL, NULL, &height);
1080
1081   if (items <= 0)
1082     gtk_widget_hide (completion->priv->scrolled_window);
1083   else
1084     gtk_widget_show (completion->priv->scrolled_window);
1085
1086   gtk_widget_set_size_request (completion->priv->tree_view,
1087                                completion->priv->entry->allocation.width - 2 * x_border,
1088                                items * height);
1089
1090   /* default on no match */
1091   completion->priv->current_selected = -1;
1092
1093   items = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
1094
1095   if (items)
1096     {
1097       gtk_widget_show (completion->priv->action_view);
1098
1099       gtk_tree_view_column_cell_get_size (gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0),
1100                                           NULL, NULL, NULL, NULL,
1101                                           &height);
1102
1103       gtk_widget_set_size_request (completion->priv->action_view,
1104                                    completion->priv->entry->allocation.width - 2 * x_border,
1105                                    items * height);
1106     }
1107   else
1108     gtk_widget_hide (completion->priv->action_view);
1109
1110   return height;
1111 }
1112
1113 void
1114 _gtk_entry_completion_popup (GtkEntryCompletion *completion)
1115 {
1116   gint x, y, x_border, y_border;
1117   gint height;
1118   GdkScreen *screen;
1119   gint monitor_num;
1120   GdkRectangle monitor;
1121   GtkRequisition popup_req;
1122
1123   if (GTK_WIDGET_MAPPED (completion->priv->popup_window))
1124     return;
1125
1126   gtk_widget_show_all (completion->priv->vbox);
1127
1128   gdk_window_get_origin (completion->priv->entry->window, &x, &y);
1129   get_borders (GTK_ENTRY (completion->priv->entry), &x_border, &y_border);
1130
1131   x += x_border;
1132   y += 2 * y_border;
1133
1134   height = _gtk_entry_completion_resize_popup (completion);
1135
1136   gtk_widget_size_request (completion->priv->popup_window, &popup_req);
1137
1138   screen = gtk_widget_get_screen (GTK_WIDGET (completion->priv->entry));
1139   monitor_num = gdk_screen_get_monitor_at_window (screen, 
1140                                                   GTK_WIDGET (completion->priv->entry)->window);
1141   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1142   
1143   if (x < monitor.x)
1144     x = monitor.x;
1145   else if (x + popup_req.width > monitor.x + monitor.width)
1146     x = monitor.x + monitor.width - popup_req.width;
1147   
1148   if (y + height + popup_req.height <= monitor.y + monitor.height)
1149     y += height;
1150   else
1151     y -= popup_req.height;
1152
1153   gtk_window_move (GTK_WINDOW (completion->priv->popup_window), x, y);
1154
1155   gtk_widget_show (completion->priv->popup_window);
1156
1157   gtk_grab_add (completion->priv->popup_window);
1158   gdk_pointer_grab (completion->priv->popup_window->window, TRUE,
1159                     GDK_BUTTON_PRESS_MASK |
1160                     GDK_BUTTON_RELEASE_MASK |
1161                     GDK_POINTER_MOTION_MASK,
1162                     NULL, NULL, GDK_CURRENT_TIME);
1163 }
1164
1165 void
1166 _gtk_entry_completion_popdown (GtkEntryCompletion *completion)
1167 {
1168   gdk_pointer_ungrab (GDK_CURRENT_TIME);
1169   gtk_grab_remove (completion->priv->popup_window);
1170
1171   gtk_widget_hide (completion->priv->popup_window);
1172 }