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