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