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