24d7a9660af692292ffa7beeb4e3e67cd169d7bd
[hildon] / hildon / hildon-find-toolbar.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 /**
26  * SECTION:hildon-find-toolbar
27  * @short_description: A toolbar with a search field.
28  * @see_also: #HildonWindow
29  *
30  * HildonFindToolbar is a toolbar that contains a search entry and a dropdown
31  * list with previously searched strings. The list is represented using a 
32  * #GtkListStore and can be accesed using a property 'list'. Entries are added
33  * automatically to the list when the search button is pressed.
34  *
35  */
36
37 #ifdef                                          HAVE_CONFIG_H
38 #include                                        <config.h>
39 #endif
40
41 #include                                        <string.h>
42 #include                                        <libintl.h>
43 #include                                        <gdk/gdkkeysyms.h>
44
45 #include                                        "hildon-find-toolbar.h"
46 #include                                        "hildon-defines.h"
47 #include                                        "hildon-find-toolbar-private.h"
48 #include                                        "hildon-marshalers.h"
49
50 #define                                         _(String) \
51                                                 dgettext("hildon-libs", String)
52
53 /* Same define as gtkentry.c as entry will further handle this */
54
55 #define                                         MAX_SIZE G_MAXUSHORT
56
57 #define                                         FIND_LABEL_XPADDING 6
58
59 #define                                         FIND_LABEL_YPADDING 0
60
61 static GtkTreeModel*
62 hildon_find_toolbar_get_list_model              (HildonFindToolbarPrivate *priv);
63
64 static GtkEntry*
65 hildon_find_toolbar_get_entry                   (HildonFindToolbarPrivate *priv);
66
67 static gboolean
68 hildon_find_toolbar_filter                      (GtkTreeModel *model,
69                                                  GtkTreeIter *iter,
70                                                  gpointer self);
71
72 static void
73 hildon_find_toolbar_apply_filter                (HildonFindToolbar *self,  
74                                                  GtkTreeModel *model);
75
76 static void
77 hildon_find_toolbar_get_property                (GObject *object,
78                                                  guint prop_id,
79                                                  GValue *value,
80                                                  GParamSpec *pspec);
81
82 static void
83 hildon_find_toolbar_set_property                (GObject *object,
84                                                  guint prop_id,
85                                                  const GValue *value,
86                                                  GParamSpec *pspec);
87
88 static gboolean
89 hildon_find_toolbar_find_string                 (HildonFindToolbar *self,
90                                                  GtkTreeIter *iter,
91                                                  gint column,
92                                                  const gchar *string);
93
94 static gboolean
95 hildon_find_toolbar_history_append              (HildonFindToolbar *self,
96                                                  gpointer data);
97
98 static void
99 hildon_find_toolbar_emit_close                  (GtkButton *button, 
100                                                  gpointer self);
101
102 #ifdef MAEMO_GTK 
103 static void
104 hildon_find_toolbar_emit_invalid_input          (GtkEntry *entry, 
105                                                  GtkInvalidInputType type, 
106                                                  gpointer self);
107 #endif
108
109 static void
110 hildon_find_toolbar_entry_activate              (GtkWidget *widget,
111                                                  gpointer user_data);
112
113 static void
114 hildon_find_toolbar_class_init                  (HildonFindToolbarClass *klass);
115
116 static void
117 hildon_find_toolbar_init                        (HildonFindToolbar *self);
118
119 enum
120 {
121     SEARCH = 0,
122     CLOSE,
123     INVALID_INPUT,
124     HISTORY_APPEND,
125
126     LAST_SIGNAL
127 };
128
129 enum
130 {
131     PROP_0,
132     PROP_LABEL = 1,
133     PROP_PREFIX,
134     PROP_LIST,
135     PROP_COLUMN,
136     PROP_MAX,
137     PROP_HISTORY_LIMIT
138 };
139
140 static guint                                    HildonFindToolbar_signal [LAST_SIGNAL] = {0};
141
142 /**
143  * hildon_find_toolbar_get_type:
144  *
145  * Initializes and returns the type of a #HildonFindToolbar.
146  *
147  * Returns: GType of #HildonFindToolbar
148  */
149 GType G_GNUC_CONST
150 hildon_find_toolbar_get_type                    (void)
151 {
152     static GType find_toolbar_type = 0;
153
154     if (! find_toolbar_type) {
155         static const GTypeInfo find_toolbar_info = {
156             sizeof(HildonFindToolbarClass),
157             NULL,       /* base_init */
158             NULL,       /* base_finalize */
159             (GClassInitFunc) hildon_find_toolbar_class_init,
160             NULL,       /* class_finalize */
161             NULL,       /* class_data */
162             sizeof(HildonFindToolbar),
163             0,  /* n_preallocs */
164             (GInstanceInitFunc) hildon_find_toolbar_init,
165         };
166         find_toolbar_type = g_type_register_static (GTK_TYPE_TOOLBAR,
167                 "HildonFindToolbar",
168                 &find_toolbar_info, 0);
169     }
170
171     return find_toolbar_type;
172 }
173
174 static GtkTreeModel*
175 hildon_find_toolbar_get_list_model              (HildonFindToolbarPrivate *priv)
176 {
177     GtkTreeModel *filter_model =
178         gtk_combo_box_get_model (GTK_COMBO_BOX (priv->entry_combo_box));
179
180     return filter_model == NULL ? NULL :
181         gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (filter_model));
182 }
183
184 static GtkEntry*
185 hildon_find_toolbar_get_entry                   (HildonFindToolbarPrivate *priv)
186 {
187     return GTK_ENTRY (gtk_bin_get_child (GTK_BIN (priv->entry_combo_box)));
188 }
189
190 static gboolean
191 hildon_find_toolbar_filter                      (GtkTreeModel *model,
192                                                  GtkTreeIter *iter,
193                                                  gpointer self)
194 {
195     GtkTreePath *path;
196     const gint *indices;
197     gint n;
198     gint limit;
199     gint total;
200
201     total = gtk_tree_model_iter_n_children (model, NULL);
202     g_object_get (self, "history_limit", &limit, NULL);
203     path = gtk_tree_model_get_path (model, iter);
204     indices = gtk_tree_path_get_indices (path);
205
206     /* set the row's index, list store has only one level */
207     n = indices [0];
208     gtk_tree_path_free (path);
209
210     /*if the row is among the latest "history_limit" additions of the 
211      * model, then we show it */
212     if( (total - limit <= n) && (n < total) )
213         return TRUE;
214     else
215         return FALSE;
216 }
217
218 static void
219 hildon_find_toolbar_apply_filter                (HildonFindToolbar *self,  
220                                                  GtkTreeModel *model)
221 {
222     GtkTreeModel *filter;
223     HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
224     g_assert (priv);
225
226     /* Create a filter for the given model. Its only purpose is to hide
227        the oldest entries so only "history_limit" entries are visible. */
228     filter = gtk_tree_model_filter_new (model, NULL);
229
230     gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER(filter), 
231             hildon_find_toolbar_filter,
232             self, NULL);
233
234     gtk_combo_box_set_model (GTK_COMBO_BOX (priv->entry_combo_box), filter);
235
236     /* ComboBox keeps the only needed reference to the filter */
237     g_object_unref (filter);
238 }
239
240 static void
241 hildon_find_toolbar_get_property                (GObject *object,
242                                                  guint prop_id,
243                                                  GValue *value,
244                                                  GParamSpec *pspec)
245 {
246     HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (object);
247     g_assert (priv);
248
249     const gchar *string;
250     gint c_n, max_len;
251
252     switch (prop_id)
253     {
254         case PROP_LABEL:
255             string = gtk_label_get_text (GTK_LABEL (priv->label));
256             g_value_set_string (value, string);
257             break;
258
259         case PROP_PREFIX:
260             string = gtk_entry_get_text (hildon_find_toolbar_get_entry(priv));
261             g_value_set_string (value, string);
262             break;
263
264         case PROP_LIST:
265             g_value_set_object (value, hildon_find_toolbar_get_list_model(priv));
266             break;
267
268         case PROP_COLUMN:
269             c_n = gtk_combo_box_entry_get_text_column (priv->entry_combo_box);
270             g_value_set_int (value, c_n);
271             break;
272
273         case PROP_MAX:
274             max_len = gtk_entry_get_max_length (hildon_find_toolbar_get_entry(priv));
275             g_value_set_int (value, max_len);
276             break;
277
278         case PROP_HISTORY_LIMIT:
279             g_value_set_int (value, priv->history_limit);
280             break;
281
282         default:
283             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
284             break;
285     }
286 }
287
288 static void
289 hildon_find_toolbar_set_property                (GObject *object,
290                                                  guint prop_id,
291                                                  const GValue *value,
292                                                  GParamSpec *pspec)
293 {
294     HildonFindToolbar *self = HILDON_FIND_TOOLBAR(object);
295     HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (object);
296     g_assert (priv);
297
298     GtkTreeModel *model;
299     const gchar *string;
300
301     switch (prop_id)
302     {
303         case PROP_LABEL:
304             string = g_value_get_string (value);   
305             gtk_label_set_text (GTK_LABEL (priv->label), string);
306             break;
307
308         case PROP_PREFIX:
309             string = g_value_get_string (value);
310             gtk_entry_set_text (hildon_find_toolbar_get_entry(priv), string);
311             break;
312
313         case PROP_LIST:
314             model = GTK_TREE_MODEL (g_value_get_object(value));
315             hildon_find_toolbar_apply_filter (self, model);
316             break;
317
318         case PROP_COLUMN:
319             gtk_combo_box_entry_set_text_column (priv->entry_combo_box,
320                     g_value_get_int (value));
321             break;
322
323         case PROP_MAX:
324             gtk_entry_set_max_length (hildon_find_toolbar_get_entry(priv),
325                     g_value_get_int (value));
326             break;
327
328         case PROP_HISTORY_LIMIT:
329             priv->history_limit = g_value_get_int (value);
330
331             /* Re-apply the history limit to the model. */
332             model = hildon_find_toolbar_get_list_model (priv);
333             if (model != NULL)
334             {
335                 /* Note that refilter function doesn't update the status of the
336                    combobox popup arrow, so we'll just recreate the filter. */
337                 hildon_find_toolbar_apply_filter (self, model);
338
339                 if (gtk_combo_box_entry_get_text_column (priv->entry_combo_box) == -1)
340                 {
341                     /* FIXME: This is only for backwards compatibility, although
342                        probably nothing actually relies on it. The behavior was only
343                        an accidental side effect of original code */
344                     gtk_combo_box_entry_set_text_column (priv->entry_combo_box, 0);
345                 }
346             }
347             break;
348
349         default:
350             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
351             break;
352     }
353 }
354
355 static gboolean
356 hildon_find_toolbar_find_string                 (HildonFindToolbar *self,
357                                                  GtkTreeIter *iter,
358                                                  gint column,
359                                                  const gchar *string)
360 {
361     GtkTreeModel *model = NULL;
362     gchar *old_string;
363     HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
364     g_assert (priv);
365
366     model = hildon_find_toolbar_get_list_model (priv);
367
368     if (gtk_tree_model_get_iter_first (model, iter))
369     {
370         do {
371             gtk_tree_model_get (model, iter, column, &old_string, -1);
372             if (old_string != NULL && strcmp (string, old_string) == 0)
373             {
374                 /* Found it */
375                 return TRUE;
376             }
377         } while (gtk_tree_model_iter_next (model, iter));
378     }
379
380     return FALSE;
381 }
382
383 static gboolean
384 hildon_find_toolbar_history_append              (HildonFindToolbar *self,
385                                                  gpointer data) 
386 {
387     HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
388     g_assert (priv);
389
390     gchar *string;
391     gint column = 0;
392     GtkTreeModel *model = NULL;
393     GtkListStore *list = NULL;
394     GtkTreeIter iter;
395     gboolean self_create = FALSE;
396
397     g_object_get (self, "prefix", &string, NULL);
398
399     if (*string == '\0')
400     {
401         /* empty prefix, ignore */
402         g_free (string);
403         return TRUE;
404     }
405
406
407     /* If list store is set, get it */
408     model = hildon_find_toolbar_get_list_model(priv);
409     if(model != NULL)
410     {
411         list = GTK_LIST_STORE (model);
412         g_object_get(self, "column", &column, NULL);
413
414         if (column < 0)
415         {
416             /* Column number is -1 if "column" property hasn't been set but
417                "list" property is. */
418             g_free (string);
419             return TRUE;
420         }
421
422         /* Latest string is always the first one in list. If the string
423            already exists, remove it so there are no duplicates in list. */
424         if (hildon_find_toolbar_find_string (self, &iter, column, string))
425             gtk_list_store_remove (list, &iter);
426     }
427     else
428     {
429         /* No list store set. Create our own. */
430         list = gtk_list_store_new (1, G_TYPE_STRING);
431         model = GTK_TREE_MODEL (list);
432         self_create = TRUE;
433     }
434
435     /* Add the string to first in list */
436     gtk_list_store_append (list, &iter);
437     gtk_list_store_set (list, &iter, column, string, -1);
438
439     if(self_create)
440     {
441         /* Add the created list to ComboBoxEntry */
442         hildon_find_toolbar_apply_filter (self, model);
443         /* ComboBoxEntry keeps the only needed reference to this list */
444         g_object_unref (list);
445
446         /* Set the column only after ComboBoxEntry's model is set
447            in hildon_find_toolbar_apply_filter() */
448         g_object_set (self, "column", 0, NULL);
449     }
450     else
451     {
452         /* Refilter to get the oldest entry hidden from history */
453         gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER(
454                     gtk_combo_box_get_model (GTK_COMBO_BOX(priv->entry_combo_box))));
455     }
456
457     g_free (string);
458
459     return FALSE;
460 }
461
462 static void
463 hildon_find_toolbar_emit_close                  (GtkButton *button, 
464                                                  gpointer self)
465 {
466 #ifdef MAEMO_GTK 
467     HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
468     g_assert (priv);
469
470     GtkWidget *entry = gtk_bin_get_child (GTK_BIN (priv->entry_combo_box));
471     if (GTK_WIDGET_HAS_FOCUS (entry))
472     {
473         hildon_gtk_im_context_hide (GTK_ENTRY (entry)->im_context);
474     }
475 #endif
476
477     /* Clicked close button */
478     g_signal_emit (self, HildonFindToolbar_signal [CLOSE], 0);
479 }
480
481 #ifdef MAEMO_GTK 
482 static void
483 hildon_find_toolbar_emit_invalid_input          (GtkEntry *entry, 
484                                                  GtkInvalidInputType type, 
485                                                  gpointer self)
486 {
487     if(type == GTK_INVALID_INPUT_MAX_CHARS_REACHED)
488         g_signal_emit (self, HildonFindToolbar_signal [INVALID_INPUT], 0);
489 }
490 #endif
491
492 static void
493 hildon_find_toolbar_entry_activate              (GtkWidget *widget,
494                                                  gpointer user_data)
495 {
496     GtkWidget *find_toolbar = GTK_WIDGET (user_data);
497     gboolean rb;  
498
499     /* NB#40936 stop focus from moving to next widget */
500     g_signal_stop_emission_by_name (widget, "activate");
501
502     g_signal_emit (find_toolbar, HildonFindToolbar_signal [SEARCH], 0);
503     g_signal_emit (find_toolbar, HildonFindToolbar_signal [HISTORY_APPEND], 0, &rb);
504 }
505
506 static void
507 hildon_find_toolbar_class_init                  (HildonFindToolbarClass *klass)
508 {
509     GObjectClass *object_class;
510
511     g_type_class_add_private (klass, sizeof (HildonFindToolbarPrivate));
512
513     object_class = G_OBJECT_CLASS(klass);
514
515     object_class->get_property = hildon_find_toolbar_get_property;
516     object_class->set_property = hildon_find_toolbar_set_property;
517
518     klass->history_append = (gpointer) hildon_find_toolbar_history_append;
519
520     /**
521      * HildonFindToolbar:label:
522      *
523      * The label to display before the search box.
524      *                      
525      */
526     g_object_class_install_property (object_class, PROP_LABEL, 
527             g_param_spec_string ("label", 
528                 "Label", "Displayed name for"
529                 " find-toolbar",
530                 _("ecdg_ti_find_toolbar_label"),
531                 G_PARAM_READWRITE |
532                 G_PARAM_CONSTRUCT));
533
534     /**
535      * HildonFindToolbar:prefix:
536      *
537      * The search string.
538      *                      
539      */
540     g_object_class_install_property (object_class, PROP_PREFIX, 
541             g_param_spec_string ("prefix", 
542                 "Prefix", "Search string", NULL,
543                 G_PARAM_READWRITE));
544
545     /**
546      * HildonFindToolbar:list:
547      *
548      * A #GtkListStore where the search history is kept.
549      *                      
550      */
551     g_object_class_install_property (object_class, PROP_LIST,
552             g_param_spec_object ("list",
553                 "List"," GtkListStore model where "
554                 "history list is kept",
555                 GTK_TYPE_LIST_STORE,
556                 G_PARAM_READWRITE));
557
558     /**
559      * HildonFindToolbar:column:
560      *
561      * The column number in GtkListStore where strings of
562      * search history are kept.
563      *                      
564      */
565     g_object_class_install_property(object_class, PROP_COLUMN,
566             g_param_spec_int ("column",
567                 "Column", "Column number in GtkListStore "
568                 "where history list strings are kept",
569                 0, G_MAXINT,
570                 0, G_PARAM_READWRITE));
571
572     /**
573      * HildonFindToolbar:max-characters:
574      *
575      * Maximum number of characters in search string.
576      *                      
577      */
578     g_object_class_install_property (object_class, PROP_MAX,
579             g_param_spec_int ("max_characters",
580                 "Maximum number of characters",
581                 "Maximum number of characters "
582                 "in search string",
583                 0, MAX_SIZE,
584                 0, G_PARAM_READWRITE |
585                 G_PARAM_CONSTRUCT));
586
587     /**
588      * HildonFindToolbar:history-limit:
589      *
590      * Maximum number of history items in the combobox.
591      *                      
592      */
593     g_object_class_install_property (object_class, PROP_HISTORY_LIMIT,
594             g_param_spec_int ("history-limit",
595                 "Maximum number of history items",
596                 "Maximum number of history items "
597                 "in search combobox",
598                 0, G_MAXINT,
599                 5, G_PARAM_READWRITE |
600                 G_PARAM_CONSTRUCT));
601
602     /**
603      * HildonFindToolbar::search:
604      * @toolbar: the toolbar which received the signal
605      * 
606      * Gets emitted when the find button is pressed.
607      */ 
608     HildonFindToolbar_signal[SEARCH] = 
609         g_signal_new(
610                 "search", HILDON_TYPE_FIND_TOOLBAR,
611                 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET 
612                 (HildonFindToolbarClass, search),
613                 NULL, NULL, g_cclosure_marshal_VOID__VOID,
614                 G_TYPE_NONE, 0);
615
616     /**
617      * HildonFindToolbar::close:
618      * @toolbar: the toolbar which received the signal
619      * 
620      * Gets emitted when the close button is pressed.
621      */ 
622     HildonFindToolbar_signal[CLOSE] = 
623         g_signal_new(
624                 "close", HILDON_TYPE_FIND_TOOLBAR,
625                 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET 
626                 (HildonFindToolbarClass, close),
627                 NULL, NULL, g_cclosure_marshal_VOID__VOID,
628                 G_TYPE_NONE, 0);
629
630     /**
631      * HildonFindToolbar::invalid-input:
632      * @toolbar: the toolbar which received the signal
633      * 
634      * Gets emitted when the maximum search prefix length is reached and
635      * user tries to type more.
636      */
637     HildonFindToolbar_signal[INVALID_INPUT] = 
638         g_signal_new(
639                 "invalid_input", HILDON_TYPE_FIND_TOOLBAR,
640                 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET 
641                 (HildonFindToolbarClass, invalid_input),
642                 NULL, NULL, g_cclosure_marshal_VOID__VOID,
643                 G_TYPE_NONE, 0);
644
645     /**
646      * HildonFindToolbar::history-append:
647      * @toolbar: the toolbar which received the signal
648      * 
649      * Gets emitted when the current search prefix should be added to history.
650      */ 
651     HildonFindToolbar_signal[HISTORY_APPEND] = 
652         g_signal_new(
653                 "history_append", HILDON_TYPE_FIND_TOOLBAR,
654                 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET 
655                 (HildonFindToolbarClass, history_append),
656                 g_signal_accumulator_true_handled, NULL, 
657                 _hildon_marshal_BOOLEAN__VOID,
658                 G_TYPE_BOOLEAN, 0);
659 }
660
661 static void
662 hildon_find_toolbar_init                        (HildonFindToolbar *self)
663 {
664     GtkToolItem *label_container;
665     GtkToolItem *entry_combo_box_container;
666     GtkAlignment *alignment;
667
668     HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
669     g_assert (priv);
670
671     /* Create the label */
672     priv->label = gtk_label_new (_("ecdg_ti_find_toolbar_label"));
673
674     gtk_misc_set_padding (GTK_MISC (priv->label), FIND_LABEL_XPADDING,
675             FIND_LABEL_YPADDING);
676
677     label_container = gtk_tool_item_new ();
678     gtk_container_add (GTK_CONTAINER (label_container), 
679             priv->label);
680
681     gtk_widget_show_all (GTK_WIDGET (label_container));
682     gtk_toolbar_insert (GTK_TOOLBAR (self), label_container, -1);
683
684     /* ComboBoxEntry for search prefix string / history list */
685     priv->entry_combo_box = GTK_COMBO_BOX_ENTRY (gtk_combo_box_entry_new ());
686
687 #ifdef MAEMO_GTK
688     g_signal_connect (hildon_find_toolbar_get_entry(priv),
689             "invalid_input", 
690             G_CALLBACK(hildon_find_toolbar_emit_invalid_input), self);
691 #endif
692
693     entry_combo_box_container = gtk_tool_item_new ();
694     alignment = GTK_ALIGNMENT (gtk_alignment_new (0, 0.5, 1, 0));
695
696     gtk_tool_item_set_expand (entry_combo_box_container, TRUE);
697     gtk_container_add (GTK_CONTAINER (alignment),
698             GTK_WIDGET (priv->entry_combo_box));
699     gtk_container_add (GTK_CONTAINER (entry_combo_box_container),
700             GTK_WIDGET (alignment));
701     gtk_widget_show_all(GTK_WIDGET (entry_combo_box_container));
702     gtk_toolbar_insert (GTK_TOOLBAR (self), entry_combo_box_container, -1);
703     g_signal_connect (hildon_find_toolbar_get_entry (priv),
704             "activate",
705             G_CALLBACK(hildon_find_toolbar_entry_activate), self);
706
707     /* Separator */
708     priv->separator = gtk_separator_tool_item_new();
709     gtk_widget_set_size_request (GTK_WIDGET (priv->separator), 72, -1);
710     gtk_widget_show(GTK_WIDGET(priv->separator));
711     gtk_toolbar_insert (GTK_TOOLBAR(self), priv->separator, -1);
712
713     /* Close button */
714     priv->close_button = gtk_tool_button_new (
715             gtk_image_new_from_icon_name ("general_close",
716                 HILDON_ICON_SIZE_TOOLBAR),
717             "Close");
718     g_signal_connect(priv->close_button, "clicked",
719             G_CALLBACK(hildon_find_toolbar_emit_close), self);
720     gtk_widget_show_all(GTK_WIDGET(priv->close_button));
721     gtk_toolbar_insert (GTK_TOOLBAR(self), priv->close_button, -1);
722     if ( GTK_WIDGET_CAN_FOCUS( GTK_BIN(priv->close_button)->child) )
723         GTK_WIDGET_UNSET_FLAGS(
724                 GTK_BIN(priv->close_button)->child, GTK_CAN_FOCUS);
725 }
726
727 /**
728  * hildon_find_toolbar_new:
729  * @label: label for the #HildonFindToolbar, %NULL to set the label to
730  *         default "Find".
731  * 
732  * Creates a new #HildonFindToolbar.
733  *
734  * Returns: a new #HildonFindToolbar.
735  */
736 GtkWidget*
737 hildon_find_toolbar_new                         (const gchar *label)
738 {
739     GtkWidget *findtoolbar;
740
741     findtoolbar = GTK_WIDGET (g_object_new (HILDON_TYPE_FIND_TOOLBAR, NULL));
742
743     if (label != NULL)
744         g_object_set(findtoolbar, "label", label, NULL);
745
746     return findtoolbar;
747 }
748
749 /**
750  * hildon_find_toolbar_new_with_model:
751  * @label: label for the #HildonFindToolbar, %NULL to set the label to
752  *         default "Find".
753  * @model: a #GtkListStore.
754  * @column: indicates which column the search history list will
755  *          retrieve the string from.
756  * 
757  * Creates a new #HildonFindToolbar with an initial #GtkTreeModel.
758  *
759  * Returns: a new #HildonFindToolbar.
760  */
761 GtkWidget*
762 hildon_find_toolbar_new_with_model              (const gchar *label,
763                                                  GtkListStore *model,
764                                                  gint column)
765 {
766     GtkWidget *findtoolbar;
767
768     findtoolbar = hildon_find_toolbar_new (label);
769
770     g_object_set (findtoolbar, "list", model, "column", column, NULL);
771
772     return findtoolbar;
773 }
774
775 /**
776  * hildon_find_toolbar_highlight_entry:
777  * @ftb: #HildonFindToolbar whose entry is to be highlighted.
778  * @get_focus: if user passes %TRUE to this value, then the text in
779  * the entry will not only get highlighted, but also get focused.
780  *
781  * Highlights the current entry in the #HildonFindToolbar.
782  * 
783  */
784 void
785 hildon_find_toolbar_highlight_entry             (HildonFindToolbar *self,
786                                                  gboolean get_focus)
787 {
788     GtkEntry *entry = NULL;
789     HildonFindToolbarPrivate *priv;
790
791     g_return_if_fail (HILDON_IS_FIND_TOOLBAR (self));
792     priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
793     g_assert (priv);
794
795     entry = hildon_find_toolbar_get_entry (priv);
796
797     gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
798
799     if (get_focus)
800         gtk_widget_grab_focus (GTK_WIDGET (entry));
801 }
802
803 /**
804  * hildon_find_toolbar_set_active:
805  * @toolbar: A #HildonFindToolbar to operate on.
806  * @index: An index in the model, or -1 to have no active item.
807  *
808  * Sets the active item on the #HildonFindToolbar's #GtkComboBox. Simply calls
809  * gtk_combo_box_set_active() on the #HildonFindToolbar's #GtkComboBox.
810  * 
811  */
812 void
813 hildon_find_toolbar_set_active                  (HildonFindToolbar *toolbar,
814                                                  gint index)
815 {
816     HildonFindToolbarPrivate *priv;
817
818     g_return_if_fail (HILDON_IS_FIND_TOOLBAR (toolbar));
819     priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (toolbar);
820
821     gtk_combo_box_set_active (GTK_COMBO_BOX (priv->entry_combo_box), index);
822 }
823
824 /**
825  * hildon_find_toolbar_get_active:
826  * @toolbar: A #HildonFindToolbar to query.
827  *
828  * Gets the index of the currently active item, or -1 if there's no active item.
829  * Simply calls gtk_combo_box_get_active() on the #HildonFindToolbar's
830  * #GtkComboBox.
831  *
832  * Returns: the index of the currently active item, or -1 if there is no active
833  * item.
834  * 
835  */
836 gint
837 hildon_find_toolbar_get_active                  (HildonFindToolbar *toolbar)
838 {
839     HildonFindToolbarPrivate *priv;
840
841     g_return_val_if_fail (HILDON_IS_FIND_TOOLBAR (toolbar), -1);
842     priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (toolbar);
843
844     return gtk_combo_box_get_active (GTK_COMBO_BOX (priv->entry_combo_box));
845 }
846
847 /**
848  * hildon_find_toolbar_set_active_iter:
849  * @toolbar: A #HildonFindToolbar to operate on.
850  * @iter: A #GtkTreeIter to activate.
851  *
852  * Sets the current active item to be the one referenced by @iter. Simply calls
853  * gtk_combo_box_set_active_iter() on the #HildonFindToolbar's #GtkComboBox.
854  * 
855  */
856 void
857 hildon_find_toolbar_set_active_iter             (HildonFindToolbar *toolbar, 
858                                                  GtkTreeIter *iter)
859 {
860     HildonFindToolbarPrivate *priv;
861
862     g_return_if_fail (HILDON_IS_FIND_TOOLBAR (toolbar));
863     priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (toolbar);
864
865     gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->entry_combo_box), iter);
866 }
867
868 /**
869  * hildon_find_toolbar_get_active_iter:
870  * @toolbar: A #HildonFindToolbar to query.
871  * @iter: The uninitialized #GtkTreeIter.
872  *
873  * Sets @iter to point to the current active item, if it exists. Simply calls
874  * gtk_combo_box_get_active_iter() on the #HildonFindToolbar's #GtkComboBox.
875  * 
876  * Returns: %TRUE, if @iter was set.
877  *
878  */
879 gboolean
880 hildon_find_toolbar_get_active_iter             (HildonFindToolbar *toolbar, 
881                                                  GtkTreeIter *iter)
882 {
883     HildonFindToolbarPrivate *priv;
884
885     g_return_val_if_fail (HILDON_IS_FIND_TOOLBAR (toolbar), FALSE);
886     priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (toolbar);
887
888     return gtk_combo_box_get_active_iter (GTK_COMBO_BOX (priv->entry_combo_box), iter);
889 }
890
891 /**
892  * hildon_find_toolbar_get_last_index
893  * @toolbar: A #HildonFindToolbar to query.
894  *
895  * Returns the index of the last (most recently added) item in the
896  * #HildonFindToolbar. Can be used to set this item active in the
897  * #HildonFindToolbar::history-append signal.
898  *
899  * 
900  * Returns: Index of the most recent entry.
901  *
902  */
903 gint32
904 hildon_find_toolbar_get_last_index              (HildonFindToolbar *toolbar)
905 {
906     HildonFindToolbarPrivate *priv;
907     GtkTreeModel *filter_model;
908     
909     g_return_val_if_fail (HILDON_IS_FIND_TOOLBAR (toolbar), FALSE);
910     priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (toolbar);
911
912     filter_model = gtk_combo_box_get_model (GTK_COMBO_BOX (priv->entry_combo_box));
913
914     if (filter_model == NULL)
915         return 0;
916
917     gint i = 0;
918     GtkTreeIter iter;
919
920     gtk_tree_model_get_iter_first (filter_model, &iter);
921
922     while (gtk_tree_model_iter_next (filter_model, &iter))
923         i++;
924
925     return i;
926 }
927