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