]> Pileus Git - ~andy/gtk/blob - gtk/gtkentrycompletion.c
Add an ignore_enter flag and use it as in the menu code to avoid the
[~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  *
823  * Since: 2.4
824  */
825 void
826 gtk_entry_completion_set_model (GtkEntryCompletion *completion,
827                                 GtkTreeModel       *model)
828 {
829   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
830   g_return_if_fail (GTK_IS_TREE_MODEL (model));
831
832   /* code will unref the old filter model (if any) */
833   completion->priv->filter_model =
834     GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
835   gtk_tree_model_filter_set_visible_func (completion->priv->filter_model,
836                                           gtk_entry_completion_visible_func,
837                                           completion,
838                                           NULL);
839   gtk_tree_view_set_model (GTK_TREE_VIEW (completion->priv->tree_view),
840                            GTK_TREE_MODEL (completion->priv->filter_model));
841   g_object_unref (G_OBJECT (completion->priv->filter_model));
842 }
843
844 /**
845  * gtk_entry_completion_get_model:
846  * @completion: A #GtkEntryCompletion.
847  *
848  * Returns the model the #GtkEntryCompletion is using as data source.
849  * Returns %NULL if the model is unset.
850  *
851  * Return value: A #GtkTreeModel, or %NULL if none is currently being used.
852  *
853  * Since: 2.4
854  */
855 GtkTreeModel *
856 gtk_entry_completion_get_model (GtkEntryCompletion *completion)
857 {
858   g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), NULL);
859
860   return gtk_tree_model_filter_get_model (completion->priv->filter_model);
861 }
862
863 /**
864  * gtk_entry_completion_set_match_func:
865  * @completion: A #GtkEntryCompletion.
866  * @func: The #GtkEntryCompletionMatchFunc to use.
867  * @func_data: The user data for @func.
868  * @func_notify: Destroy notifier for @func_data.
869  *
870  * Sets the match function for @completion to be @func. The match function
871  * is used to determine if a row should or should not be in the completion
872  * list.
873  *
874  * Since: 2.4.
875  */
876 void
877 gtk_entry_completion_set_match_func (GtkEntryCompletion          *completion,
878                                      GtkEntryCompletionMatchFunc  func,
879                                      gpointer                     func_data,
880                                      GDestroyNotify               func_notify)
881 {
882   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
883
884   if (completion->priv->match_notify)
885     (* completion->priv->match_notify) (completion->priv->match_data);
886
887   completion->priv->match_func = func;
888   completion->priv->match_data = func_data;
889   completion->priv->match_notify = func_notify;
890 }
891
892 /**
893  * gtk_entry_completion_set_minimum_key_length:
894  * @completion: A #GtkEntryCompletion.
895  * @length: The minimum length of the key in order to start completing.
896  *
897  * Requires the length of the search key for @completion to be at least
898  * @length. This is useful for long lists, where completing using a small
899  * key takes a lot of time and will come up with meaningless results anyway
900  * (ie, a too large dataset).
901  *
902  * Since: 2.4
903  */
904 void
905 gtk_entry_completion_set_minimum_key_length (GtkEntryCompletion *completion,
906                                              gint                length)
907 {
908   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
909   g_return_if_fail (length >= 1);
910
911   completion->priv->minimum_key_length = length;
912 }
913
914 /**
915  * gtk_entry_completion_get_minimum_key_length:
916  * @completion: A #GtkEntryCompletion.
917  *
918  * Returns the minimum key length as set for @completion.
919  *
920  * Return value: The currently used minimum key length.
921  *
922  * Since: 2.4
923  */
924 gint
925 gtk_entry_completion_get_minimum_key_length (GtkEntryCompletion *completion)
926 {
927   g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), 0);
928
929   return completion->priv->minimum_key_length;
930 }
931
932 /**
933  * gtk_entry_completion_complete:
934  * @completion: A #GtkEntryCompletion.
935  *
936  * Requests a completion operation, or in other words a refiltering of the
937  * current list with completions, using the current key. The completion list
938  * view will be updated accordingly.
939  *
940  * Since: 2.4
941  */
942 void
943 gtk_entry_completion_complete (GtkEntryCompletion *completion)
944 {
945   gchar *tmp;
946
947   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
948   g_return_if_fail (completion->priv->filter_model != NULL);
949
950   if (completion->priv->case_normalized_key)
951     g_free (completion->priv->case_normalized_key);
952
953   tmp = g_utf8_normalize (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)),
954                           -1, G_NORMALIZE_ALL);
955   completion->priv->case_normalized_key = g_utf8_casefold (tmp, -1);
956   g_free (tmp);
957
958   gtk_tree_model_filter_refilter (completion->priv->filter_model);
959 }
960
961 static void
962 gtk_entry_completion_insert_action (GtkEntryCompletion *completion,
963                                     gint                index,
964                                     const gchar        *string,
965                                     gboolean            markup)
966 {
967   GtkTreeIter iter;
968
969   gtk_list_store_insert (completion->priv->actions, &iter, index);
970   gtk_list_store_set (completion->priv->actions, &iter,
971                       0, string,
972                       1, markup,
973                       -1);
974
975   if (!completion->priv->action_view->parent)
976     {
977       GtkTreePath *path = gtk_tree_path_new_from_indices (0, -1);
978
979       gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
980                                 path, NULL, FALSE);
981       gtk_tree_path_free (path);
982
983       gtk_box_pack_start (GTK_BOX (completion->priv->vbox),
984                           completion->priv->action_view, FALSE, FALSE, 0);
985       gtk_widget_show (completion->priv->action_view);
986     }
987 }
988
989 /**
990  * gtk_entry_completion_insert_action_text:
991  * @completion: A #GtkEntryCompletion.
992  * @index_: The index of the item to insert.
993  * @text: Text of the item to insert.
994  *
995  * Inserts an action in @completion's action item list at position @index_
996  * with text @text. If you want the action item to have markup, use
997  * gtk_entry_completion_insert_action_markup().
998  *
999  * Since: 2.4
1000  */
1001 void
1002 gtk_entry_completion_insert_action_text (GtkEntryCompletion *completion,
1003                                          gint                index_,
1004                                          const gchar        *text)
1005 {
1006   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1007   g_return_if_fail (text != NULL);
1008
1009   gtk_entry_completion_insert_action (completion, index_, text, FALSE);
1010 }
1011
1012 /**
1013  * gtk_entry_completion_insert_action_markup:
1014  * @completion: A #GtkEntryCompletion.
1015  * @index_: The index of the item to insert.
1016  * @markup: Markup of the item to insert.
1017  *
1018  * Inserts an action in @completion's action item list at position @index_
1019  * with markup @markup.
1020  *
1021  * Since: 2.4
1022  */
1023 void
1024 gtk_entry_completion_insert_action_markup (GtkEntryCompletion *completion,
1025                                            gint                index_,
1026                                            const gchar        *markup)
1027 {
1028   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1029   g_return_if_fail (markup != NULL);
1030
1031   gtk_entry_completion_insert_action (completion, index_, markup, TRUE);
1032 }
1033
1034 /**
1035  * gtk_entry_completion_delete_action:
1036  * @completion: A #GtkEntryCompletion.
1037  * @index_: The index of the item to Delete.
1038  *
1039  * Deletes the action at @index_ from @completion's action list.
1040  *
1041  * Since: 2.4
1042  */
1043 void
1044 gtk_entry_completion_delete_action (GtkEntryCompletion *completion,
1045                                     gint                index_)
1046 {
1047   GtkTreeIter iter;
1048
1049   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1050   g_return_if_fail (index_ >= 0);
1051
1052   gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (completion->priv->actions),
1053                                  &iter, NULL, index_);
1054   gtk_list_store_remove (completion->priv->actions, &iter);
1055 }
1056
1057 /**
1058  * gtk_entry_completion_set_text_column:
1059  * @completion: A #GtkEntryCompletion.
1060  * @column: The column in the model of @completion to get strings from.
1061  *
1062  * Convenience function for setting up the most used case of this code: a
1063  * completion list with just strings. This function will set up @completion
1064  * to have a list displaying all (and just) strings in the completion list,
1065  * and to get those strings from @column in the model of @completion.
1066  *
1067  * This functions creates and adds a #GtkCellRendererText for the selected 
1068  * column. If you need to set the text column, but don't want the cell 
1069  * renderer, use g_object_set() to set the ::text_column property directly.
1070  * 
1071  * Since: 2.4
1072  */
1073 void
1074 gtk_entry_completion_set_text_column (GtkEntryCompletion *completion,
1075                                       gint                column)
1076 {
1077   GtkCellRenderer *cell;
1078
1079   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1080   g_return_if_fail (column >= 0);
1081
1082   completion->priv->text_column = column;
1083
1084   cell = gtk_cell_renderer_text_new ();
1085   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion),
1086                               cell, TRUE);
1087   gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (completion),
1088                                  cell,
1089                                  "text", column);
1090
1091   g_object_notify (completion, "text_column");
1092 }
1093
1094 /**
1095  * gtk_entry_completion_get_text_column:
1096  * @completion: a #GtkEntryCompletion
1097  * 
1098  * Returns the column in the model of @completion to get strings from.
1099  * 
1100  * Return value: the column containing the strings
1101  *
1102  * Since: 2.6
1103  **/
1104 gint
1105 gtk_entry_completion_get_text_column (GtkEntryCompletion *completion)
1106 {
1107   g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
1108
1109   return completion->priv->text_column;  
1110 }
1111
1112 /* private */
1113
1114 /* lame copy from gtkentry.c */
1115 static void
1116 get_borders (GtkEntry *entry,
1117              gint     *xborder,
1118              gint     *yborder)
1119 {
1120   GtkWidget *widget = GTK_WIDGET (entry);
1121   gint focus_width;
1122   gboolean interior_focus;
1123
1124   gtk_widget_style_get (widget,
1125                         "interior-focus", &interior_focus,
1126                         "focus-line-width", &focus_width,
1127                         NULL);
1128
1129   if (entry->has_frame)
1130     {
1131       *xborder = widget->style->xthickness;
1132       *yborder = widget->style->ythickness;
1133     }
1134   else
1135     {
1136       *xborder = 0;
1137       *yborder = 0;
1138     }
1139
1140   if (!interior_focus)
1141     {
1142       *xborder += focus_width;
1143       *yborder += focus_width;
1144     }
1145 }
1146
1147 static gboolean
1148 gtk_entry_completion_list_enter_notify (GtkWidget        *widget,
1149                                         GdkEventCrossing *event,
1150                                         gpointer          data)
1151 {
1152   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1153   
1154   return completion->priv->ignore_enter;
1155 }
1156
1157 static gboolean
1158 gtk_entry_completion_list_motion_notify (GtkWidget      *widget,
1159                                          GdkEventMotion *event,
1160                                          gpointer        data)
1161 {
1162   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
1163
1164   completion->priv->ignore_enter = FALSE; 
1165   
1166   return FALSE;
1167 }
1168
1169
1170 /* some nasty size requisition */
1171 gboolean
1172 _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
1173 {
1174   gint x, y;
1175   gint matches, items, height, x_border, y_border;
1176   GdkScreen *screen;
1177   gint monitor_num;
1178   GdkRectangle monitor;
1179   GtkRequisition popup_req;
1180   GtkTreePath *path;
1181   gboolean above;
1182   gint width;
1183   
1184   gdk_window_get_origin (completion->priv->entry->window, &x, &y);
1185   get_borders (GTK_ENTRY (completion->priv->entry), &x_border, &y_border);
1186
1187   x += x_border;
1188   y += 2 * y_border;
1189
1190   matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
1191
1192   items = MIN (matches, 15);
1193
1194   gtk_tree_view_column_cell_get_size (completion->priv->column, NULL,
1195                                       NULL, NULL, NULL, &height);
1196
1197   if (items <= 0)
1198     gtk_widget_hide (completion->priv->scrolled_window);
1199   else
1200     gtk_widget_show (completion->priv->scrolled_window);
1201
1202   screen = gtk_widget_get_screen (GTK_WIDGET (completion->priv->entry));
1203   monitor_num = gdk_screen_get_monitor_at_window (screen, 
1204                                                   GTK_WIDGET (completion->priv->entry)->window);
1205   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1206
1207   width = MIN (completion->priv->entry->allocation.width, monitor.width);
1208   gtk_widget_set_size_request (completion->priv->tree_view,
1209                                width - 2 * x_border, items * height);
1210
1211   /* default on no match */
1212   completion->priv->current_selected = -1;
1213
1214   items = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
1215
1216   if (items)
1217     {
1218       gtk_widget_show (completion->priv->action_view);
1219
1220       gtk_tree_view_column_cell_get_size (gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0),
1221                                           NULL, NULL, NULL, NULL,
1222                                           &height);
1223
1224       gtk_widget_set_size_request (completion->priv->action_view,
1225                                    width - 2 * x_border, items * height);
1226     }
1227   else
1228     gtk_widget_hide (completion->priv->action_view);
1229
1230   gtk_widget_size_request (completion->priv->popup_window, &popup_req);
1231   
1232   if (x < monitor.x)
1233     x = monitor.x;
1234   else if (x + popup_req.width > monitor.x + monitor.width)
1235     x = monitor.x + monitor.width - popup_req.width;
1236   
1237   if (y + height + popup_req.height <= monitor.y + monitor.height)
1238     {
1239       y += height;
1240       above = FALSE;
1241     }
1242   else
1243     {
1244       y -= popup_req.height;
1245       above = TRUE;
1246     }
1247   
1248   if (matches > 0) 
1249     {
1250       path = gtk_tree_path_new_from_indices (above ? matches - 1 : 0, -1);
1251       gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (completion->priv->tree_view), path, 
1252                                     NULL, FALSE, 0.0, 0.0);
1253       gtk_tree_path_free (path);
1254     }
1255
1256   gtk_window_move (GTK_WINDOW (completion->priv->popup_window), x, y);
1257
1258   return above;
1259 }
1260
1261 void
1262 _gtk_entry_completion_popup (GtkEntryCompletion *completion)
1263 {
1264   GtkTreeViewColumn *column;
1265   GList *renderers;
1266
1267   if (GTK_WIDGET_MAPPED (completion->priv->popup_window))
1268     return;
1269
1270   completion->priv->ignore_enter = TRUE;
1271     
1272   column = gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0);
1273   renderers = gtk_tree_view_column_get_cell_renderers (column);
1274   gtk_widget_ensure_style (completion->priv->tree_view);
1275   g_object_set (GTK_CELL_RENDERER (renderers->data), "cell_background_gdk",
1276                 &completion->priv->tree_view->style->bg[GTK_STATE_NORMAL],
1277                 NULL);
1278   g_list_free (renderers);
1279
1280   gtk_widget_show_all (completion->priv->vbox);
1281
1282   _gtk_entry_completion_resize_popup (completion);
1283
1284   gtk_widget_show (completion->priv->popup_window);
1285
1286   gtk_grab_add (completion->priv->popup_window);
1287   gdk_pointer_grab (completion->priv->popup_window->window, TRUE,
1288                     GDK_BUTTON_PRESS_MASK |
1289                     GDK_BUTTON_RELEASE_MASK |
1290                     GDK_POINTER_MOTION_MASK,
1291                     NULL, NULL, GDK_CURRENT_TIME);
1292 }
1293
1294 void
1295 _gtk_entry_completion_popdown (GtkEntryCompletion *completion)
1296 {
1297   completion->priv->ignore_enter = FALSE;
1298   
1299   gdk_pointer_ungrab (GDK_CURRENT_TIME);
1300   gtk_grab_remove (completion->priv->popup_window);
1301
1302   gtk_widget_hide (completion->priv->popup_window);
1303 }
1304
1305 static gboolean 
1306 gtk_entry_completion_match_selected (GtkEntryCompletion *completion,
1307                                      GtkTreeModel       *model,
1308                                      GtkTreeIter        *iter)
1309 {
1310   gchar *str = NULL;
1311
1312   gtk_tree_model_get (model, iter, completion->priv->text_column, &str, -1);
1313   gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), str);
1314   
1315   /* move cursor to the end */
1316   gtk_editable_set_position (GTK_EDITABLE (completion->priv->entry), -1);
1317   
1318   g_free (str);
1319
1320   return TRUE;
1321 }