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