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