f0c484f6201087513559a47612d966140ee3847b
[hildon] / hildon-widgets / hildon-find-toolbar.c
1 /*
2  * This file is part of hildon-libs
3  *
4  * Copyright (C) 2005 Nokia Corporation.
5  *
6  * Contact: Luc Pionchon <luc.pionchon@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; either 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 #include "hildon-find-toolbar.h"
26 #include "hildon-defines.h"
27 #include <gdk/gdkkeysyms.h>
28
29 #include <gtk/gtklabel.h>
30 #include <gtk/gtkentry.h>
31 #include <gtk/gtkbutton.h>
32 #include <gtk/gtktoolbutton.h>
33 #include <gtk/gtktoolitem.h>
34 #include <gtk/gtkcomboboxentry.h>
35 #include <gtk/gtkseparatortoolitem.h>
36 #include <string.h>
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41 #include <libintl.h>
42 #define _(String) dgettext(PACKAGE, String)
43
44 /*same define as gtkentry.c as entry will further handle this*/
45 #define MAX_SIZE G_MAXUSHORT
46 #define FIND_LABEL_XPADDING 6
47 #define FIND_LABEL_YPADDING 0
48
49 enum
50 {
51   SEARCH = 0,
52   CLOSE,
53   INVALID_INPUT,
54   HISTORY_APPEND,
55
56   LAST_SIGNAL
57 };
58
59 enum
60 {
61   PROP_LABEL = 1,
62   PROP_PREFIX,
63   PROP_LIST,
64   PROP_COLUMN,
65   PROP_MAX,
66   PROP_HISTORY_LIMIT
67 };
68
69 struct _HildonFindToolbarPrivate
70 {
71   GtkWidget*            label;
72   GtkComboBoxEntry*     entry_combo_box;
73   GtkToolItem*          find_button;
74   GtkToolItem*          separator;
75   GtkToolItem*          close_button;
76
77   gint                  history_limit;
78 };
79 static guint HildonFindToolbar_signal[LAST_SIGNAL] = {0};
80
81 G_DEFINE_TYPE(HildonFindToolbar, hildon_find_toolbar, GTK_TYPE_TOOLBAR)
82
83 static GtkTreeModel *
84 hildon_find_toolbar_get_list_model(HildonFindToolbarPrivate *priv)
85 {
86   GtkTreeModel *filter_model =
87     gtk_combo_box_get_model(GTK_COMBO_BOX(priv->entry_combo_box));
88
89   return filter_model == NULL ? NULL :
90     gtk_tree_model_filter_get_model(GTK_TREE_MODEL_FILTER(filter_model));
91 }
92
93 static GtkEntry *
94 hildon_find_toolbar_get_entry(HildonFindToolbarPrivate *priv)
95 {
96   return GTK_ENTRY(gtk_bin_get_child(GTK_BIN(priv->entry_combo_box)));
97 }
98
99 static gboolean
100 hildon_find_toolbar_filter(GtkTreeModel *model,
101                            GtkTreeIter *iter,
102                            gpointer self)
103 {
104   GtkTreePath *path;
105   const gint *indices;
106   gint n;
107   gint limit;
108   gint total;
109
110   total = gtk_tree_model_iter_n_children(model, NULL);
111   g_object_get(self, "history_limit", &limit, NULL);
112   path = gtk_tree_model_get_path(model, iter);
113   indices = gtk_tree_path_get_indices (path);
114
115   /* set the row's index, list store has only one level */
116   n = indices[0];
117   gtk_tree_path_free(path);
118   
119   /*if the row is among the latest "history_limit" additions of the 
120    * model, then we show it */
121   if( (total - limit <= n) && (n < total) )
122     return TRUE;
123   else
124     return FALSE;
125 }
126
127 static void
128 hildon_find_toolbar_apply_filter(HildonFindToolbar *self,  GtkTreeModel *model)
129 {
130   GtkTreeModel *filter;
131   HildonFindToolbarPrivate *priv = self->priv;
132
133   /* Create a filter for the given model. Its only purpose is to hide
134      the oldest entries so only "history_limit" entries are visible. */
135   filter = gtk_tree_model_filter_new(model, NULL);
136
137   gtk_tree_model_filter_set_visible_func(GTK_TREE_MODEL_FILTER(filter), 
138                                          hildon_find_toolbar_filter,
139                                          self, NULL);
140   gtk_combo_box_set_model(GTK_COMBO_BOX(priv->entry_combo_box), filter);
141
142   /* ComboBox keeps the only needed reference to the filter */
143   g_object_unref(filter);
144 }
145
146 static void
147 hildon_find_toolbar_get_property(GObject     *object,
148                                  guint        prop_id,
149                                  GValue      *value,
150                                  GParamSpec  *pspec)
151 {
152   HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR(object)->priv;
153   const gchar *string;
154   gint c_n, max_len;
155
156   switch (prop_id)
157     {
158     case PROP_LABEL:
159       string = gtk_label_get_text(GTK_LABEL(priv->label));
160       g_value_set_string(value, string);
161       break;
162     case PROP_PREFIX:
163       string = gtk_entry_get_text(hildon_find_toolbar_get_entry(priv));
164       g_value_set_string(value, string);
165       break;
166     case PROP_LIST:
167       g_value_set_object(value, hildon_find_toolbar_get_list_model(priv));
168       break;
169     case PROP_COLUMN:
170       c_n = gtk_combo_box_entry_get_text_column(priv->entry_combo_box);
171       g_value_set_int(value, c_n);
172       break;
173     case PROP_MAX:
174       max_len = gtk_entry_get_max_length(hildon_find_toolbar_get_entry(priv));
175       g_value_set_int(value, max_len);
176       break;
177     case PROP_HISTORY_LIMIT:
178       g_value_set_int(value, priv->history_limit);
179       break;
180     default:
181       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
182       break;
183     }
184 }
185
186 static void
187 hildon_find_toolbar_set_property(GObject      *object,
188                                  guint         prop_id,
189                                  const GValue *value,
190                                  GParamSpec   *pspec)
191 {
192   HildonFindToolbar *self = HILDON_FIND_TOOLBAR(object);
193   HildonFindToolbarPrivate *priv = self->priv;
194   GtkTreeModel *model;
195   const gchar *string;
196   
197   switch (prop_id)
198     {
199     case PROP_LABEL:
200       string = g_value_get_string(value);       
201       gtk_label_set_text(GTK_LABEL(priv->label), string);
202       break;
203     case PROP_PREFIX:
204       string = g_value_get_string(value);
205       gtk_entry_set_text(hildon_find_toolbar_get_entry(priv), string);
206       break;
207     case PROP_LIST:
208       model = GTK_TREE_MODEL(g_value_get_object(value));
209       hildon_find_toolbar_apply_filter(self, model);
210       break;
211     case PROP_COLUMN:
212       gtk_combo_box_entry_set_text_column(priv->entry_combo_box,
213                                           g_value_get_int(value));
214       break;
215     case PROP_MAX:
216       gtk_entry_set_max_length(hildon_find_toolbar_get_entry(priv),
217                                g_value_get_int(value));
218       break;
219     case PROP_HISTORY_LIMIT:
220       priv->history_limit = g_value_get_int(value);
221
222       /* Re-apply the history limit to the model. */
223       model = hildon_find_toolbar_get_list_model(priv);
224       if (model != NULL)
225         {
226           /* Note that refilter function doesn't update the status of the
227              combobox popup arrow, so we'll just recreate the filter. */
228           hildon_find_toolbar_apply_filter(self, model);
229
230           if (gtk_combo_box_entry_get_text_column(priv->entry_combo_box) == -1)
231             {
232               /* FIXME: This is only for backwards compatibility, although
233                  probably nothing actually relies on it. The behavior was only
234                  an accidental side effect of original code */
235               gtk_combo_box_entry_set_text_column(priv->entry_combo_box, 0);
236             }
237         }
238       break;
239     default:
240       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241       break;
242     }
243 }
244
245 static gboolean
246 hildon_find_toolbar_find_string(HildonFindToolbar *self,
247                                 GtkTreeIter       *iter,
248                                 gint               column,
249                                 const gchar       *string)
250 {
251   GtkTreeModel *model = NULL;
252   gchar *old_string;
253
254   model = hildon_find_toolbar_get_list_model(self->priv);
255
256   if (gtk_tree_model_get_iter_first(model, iter))
257     {
258       do {
259         gtk_tree_model_get(model, iter, column, &old_string, -1);
260         if (old_string != NULL && strcmp(string, old_string) == 0)
261           {
262             /* Found it */
263             return TRUE;
264           }
265       } while (gtk_tree_model_iter_next(model, iter));
266     }
267
268   return FALSE;
269 }
270
271 static gboolean
272 hildon_find_toolbar_history_append(HildonFindToolbar *self,
273                                    gpointer data) 
274 {
275   HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR(self)->priv;
276   gchar *string;
277   gint column = 0;
278   GtkTreeModel *model = NULL;
279   GtkListStore *list = NULL;
280   GtkTreeIter iter;
281   gboolean self_create = FALSE;
282   
283   g_object_get(self, "prefix", &string, NULL);
284   
285   if (*string == '\0')
286     {
287       /* empty prefix, ignore */
288       g_free(string);
289       return TRUE;
290     }
291
292
293   /* If list store is set, get it */
294   model = hildon_find_toolbar_get_list_model(priv);
295   if(model != NULL)
296     {
297       list = GTK_LIST_STORE(model);
298       g_object_get(self, "column", &column, NULL);
299
300       if (column < 0)
301         {
302           /* Column number is -1 if "column" property hasn't been set but
303              "list" property is. */
304           g_free(string);
305           return TRUE;
306         }
307
308       /* Latest string is always the first one in list. If the string
309          already exists, remove it so there are no duplicates in list. */
310       if (hildon_find_toolbar_find_string(self, &iter, column, string))
311           gtk_list_store_remove(list, &iter);
312     }
313   else
314     {
315       /* No list store set. Create our own. */
316       list = gtk_list_store_new(1, G_TYPE_STRING);
317       model = GTK_TREE_MODEL(list);
318       self_create = TRUE;
319     }
320
321   /* Add the string to first in list */
322   gtk_list_store_append(list, &iter);
323   gtk_list_store_set(list, &iter, column, string, -1);
324
325   if(self_create)
326     {
327       /* Add the created list to ComboBoxEntry */
328       hildon_find_toolbar_apply_filter(self, model);
329       /* ComboBoxEntry keeps the only needed reference to this list */
330       g_object_unref(list);
331
332       /* Set the column only after ComboBoxEntry's model is set
333          in hildon_find_toolbar_apply_filter() */
334       g_object_set(self, "column", 0, NULL);
335     }
336   else
337     {
338       /* Refilter to get the oldest entry hidden from history */
339       gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(
340         gtk_combo_box_get_model(GTK_COMBO_BOX(priv->entry_combo_box))));
341     }
342
343   g_free(string);
344
345   return TRUE;
346 }
347
348 static void
349 hildon_find_toolbar_emit_search(GtkButton *button, gpointer self)
350 {
351   gboolean rb;
352
353   /* Clicked search button. Perform search and add search prefix to history */
354   g_signal_emit_by_name(self, "search", NULL);
355   g_signal_emit_by_name(self, "history_append", &rb, NULL);
356 }
357
358 static void
359 hildon_find_toolbar_emit_close(GtkButton *button, gpointer self)
360 {
361   /* Clicked close button */
362   g_signal_emit_by_name(self, "close", NULL);
363 }
364
365 static void
366 hildon_find_toolbar_emit_invalid_input(GtkEntry *entry, 
367                                        GtkInvalidInputType type, 
368                                        gpointer self)
369 {
370   if(type == GTK_INVALID_INPUT_MAX_CHARS_REACHED)
371     g_signal_emit_by_name(self, "invalid_input", NULL);
372 }
373
374 static gboolean
375 hildon_find_toolbar_entry_key_press (GtkWidget *widget,
376                                     GdkEventKey *event,
377                                     gpointer user_data)
378 {
379   GtkWidget *find_toolbar = GTK_WIDGET(user_data);
380   
381   /* on enter we emit search and history_append signals and keep
382    * focus by returning true */
383   if (event->keyval == GDK_KP_Enter)
384     {
385       gboolean rb;  
386       g_signal_emit_by_name(find_toolbar, "search", NULL);
387       g_signal_emit_by_name(find_toolbar, "history_append", &rb, NULL);
388
389       return TRUE;
390     }
391
392   return FALSE;      
393 }
394
395 static void
396 hildon_find_toolbar_class_init(HildonFindToolbarClass *klass)
397 {
398   GObjectClass *object_class;
399
400   g_type_class_add_private(klass, sizeof(HildonFindToolbarPrivate));
401
402   object_class = G_OBJECT_CLASS(klass);
403
404   object_class->get_property = hildon_find_toolbar_get_property;
405   object_class->set_property = hildon_find_toolbar_set_property;
406
407   klass->history_append = hildon_find_toolbar_history_append;
408   
409   g_object_class_install_property(object_class, PROP_LABEL, 
410                                   g_param_spec_string("label", 
411                                   "Label", "Displayed name for"
412                                   " find-toolbar",
413                                   _("Ecdg_ti_find_toolbar_label"),
414                                   G_PARAM_READWRITE |
415                                   G_PARAM_CONSTRUCT));
416   
417   g_object_class_install_property(object_class, PROP_PREFIX, 
418                                   g_param_spec_string("prefix", 
419                                   "Prefix", "Search string", NULL,
420                                   G_PARAM_READWRITE));
421   
422   g_object_class_install_property(object_class, PROP_LIST,
423                                   g_param_spec_object("list",
424                                   "List"," GtkListStore model where "
425                                   "history list is kept",
426                                   GTK_TYPE_LIST_STORE,
427                                   G_PARAM_READWRITE));
428
429   g_object_class_install_property(object_class, PROP_COLUMN,
430                                   g_param_spec_int("column",
431                                   "Column", "Column number in GtkListStore "
432                                   "where history list strings are kept",
433                                   0, G_MAXINT,
434                                   0, G_PARAM_READWRITE));
435
436   g_object_class_install_property(object_class, PROP_MAX,
437                                   g_param_spec_int("max_characters",
438                                   "Maximum number of characters",
439                                   "Maximum number of characters "
440                                   "in search string",
441                                   0, MAX_SIZE,
442                                   0, G_PARAM_READWRITE |
443                                   G_PARAM_CONSTRUCT));
444   
445   g_object_class_install_property(object_class, PROP_HISTORY_LIMIT,
446                                   g_param_spec_int("history_limit",
447                                   "Maximum number of history items",
448                                   "Maximum number of history items "
449                                   "in search combobox",
450                                   0, G_MAXINT,
451                                   5, G_PARAM_READWRITE |
452                                   G_PARAM_CONSTRUCT));
453
454   /**
455    * HildonFindToolbar::search:
456    * @toolbar: the toolbar which received the signal
457    * 
458    * Gets emitted when the find button is pressed.
459    */ 
460   HildonFindToolbar_signal[SEARCH] = 
461                               g_signal_new(
462                               "search", HILDON_TYPE_FIND_TOOLBAR,
463                               G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET 
464                               (HildonFindToolbarClass, search),
465                               NULL, NULL, gtk_marshal_VOID__VOID,
466                               G_TYPE_NONE, 0);
467   
468   /**
469    * HildonFindToolbar::close:
470    * @toolbar: the toolbar which received the signal
471    * 
472    * Gets emitted when the close button is pressed.
473    */ 
474   HildonFindToolbar_signal[CLOSE] = 
475                              g_signal_new(
476                              "close", HILDON_TYPE_FIND_TOOLBAR,
477                              G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET 
478                              (HildonFindToolbarClass, close),
479                              NULL, NULL, gtk_marshal_VOID__VOID,
480                              G_TYPE_NONE, 0);
481   
482   /**
483    * HildonFindToolbar::invalid-input:
484    * @toolbar: the toolbar which received the signal
485    * 
486    * Gets emitted when the maximum search prefix length is reached and
487    * user tries to type more.
488    */ 
489   HildonFindToolbar_signal[INVALID_INPUT] = 
490                              g_signal_new(
491                              "invalid_input", HILDON_TYPE_FIND_TOOLBAR,
492                              G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET 
493                              (HildonFindToolbarClass, invalid_input),
494                              NULL, NULL, gtk_marshal_VOID__VOID,
495                              G_TYPE_NONE, 0);
496   
497   /**
498    * HildonFindToolbar::history-append:
499    * @toolbar: the toolbar which received the signal
500    * 
501    * Gets emitted when the current search prefix should be added to history.
502    */ 
503   HildonFindToolbar_signal[HISTORY_APPEND] = 
504                              g_signal_new(
505                              "history_append", HILDON_TYPE_FIND_TOOLBAR,
506                              G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET 
507                              (HildonFindToolbarClass, history_append),
508                              g_signal_accumulator_true_handled, NULL, 
509                              gtk_marshal_BOOLEAN__VOID,
510                              G_TYPE_BOOLEAN, 0);
511 }
512
513 static void
514 hildon_find_toolbar_init(HildonFindToolbar *self)
515 {
516   GtkToolItem *label_container;
517   GtkToolItem *entry_combo_box_container;
518   
519   self->priv = G_TYPE_INSTANCE_GET_PRIVATE(self,
520                                            HILDON_TYPE_FIND_TOOLBAR, 
521                                            HildonFindToolbarPrivate);
522
523   /* Create the label */
524   self->priv->label = gtk_label_new(_("Ecdg_ti_find_toolbar_label"));
525   
526   gtk_misc_set_padding (GTK_MISC(self->priv->label), FIND_LABEL_XPADDING,
527                         FIND_LABEL_YPADDING);
528
529   label_container = gtk_tool_item_new();
530   gtk_container_add(GTK_CONTAINER(label_container), 
531                     self->priv->label);
532   gtk_widget_show_all(GTK_WIDGET(label_container));
533   gtk_toolbar_insert (GTK_TOOLBAR(self), label_container, -1);
534   
535   /* ComboBoxEntry for search prefix string / history list */
536   self->priv->entry_combo_box = GTK_COMBO_BOX_ENTRY(gtk_combo_box_entry_new());
537   g_signal_connect(hildon_find_toolbar_get_entry(self->priv),
538                    "invalid_input", 
539                    G_CALLBACK(hildon_find_toolbar_emit_invalid_input), self);
540   entry_combo_box_container = gtk_tool_item_new();
541   gtk_tool_item_set_expand(entry_combo_box_container, TRUE);
542   gtk_container_add(GTK_CONTAINER(entry_combo_box_container),
543                     GTK_WIDGET(self->priv->entry_combo_box));
544   gtk_widget_show_all(GTK_WIDGET(entry_combo_box_container));
545   gtk_toolbar_insert (GTK_TOOLBAR(self), entry_combo_box_container, -1);
546   g_signal_connect(hildon_find_toolbar_get_entry(self->priv),
547                     "key-press-event",
548                     G_CALLBACK(hildon_find_toolbar_entry_key_press), self);
549
550   /* Find button */
551   self->priv->find_button = gtk_tool_button_new (
552                               gtk_image_new_from_icon_name ("qgn_toolb_browser_gobutton",
553                                                             HILDON_ICON_SIZE_TOOLBAR),
554                               "Find");
555   g_signal_connect(self->priv->find_button, "clicked",
556                    G_CALLBACK(hildon_find_toolbar_emit_search), self);
557   gtk_widget_show_all(GTK_WIDGET(self->priv->find_button));
558   gtk_toolbar_insert (GTK_TOOLBAR(self), self->priv->find_button, -1);
559   if ( GTK_WIDGET_CAN_FOCUS( GTK_BIN(self->priv->find_button)->child) )
560       GTK_WIDGET_UNSET_FLAGS(
561               GTK_BIN(self->priv->find_button)->child, GTK_CAN_FOCUS);
562   
563   /* Separator */
564   self->priv->separator = gtk_separator_tool_item_new();
565   gtk_widget_show(GTK_WIDGET(self->priv->separator));
566   gtk_toolbar_insert (GTK_TOOLBAR(self), self->priv->separator, -1);
567   
568   /* Close button */
569   self->priv->close_button = gtk_tool_button_new (
570                                gtk_image_new_from_icon_name ("qgn_toolb_gene_close",
571                                                              HILDON_ICON_SIZE_TOOLBAR),
572                                "Close");
573   g_signal_connect(self->priv->close_button, "clicked",
574                    G_CALLBACK(hildon_find_toolbar_emit_close), self);
575   gtk_widget_show_all(GTK_WIDGET(self->priv->close_button));
576   gtk_toolbar_insert (GTK_TOOLBAR(self), self->priv->close_button, -1);
577   if ( GTK_WIDGET_CAN_FOCUS( GTK_BIN(self->priv->close_button)->child) )
578       GTK_WIDGET_UNSET_FLAGS(
579               GTK_BIN(self->priv->close_button)->child, GTK_CAN_FOCUS);
580 }
581
582 /*Public functions*/
583
584 /**
585  * hildon_find_toolbar_new:
586  * @label: label for the find_toolbar, NULL to set the label to 
587  *         default "Find".
588  * 
589  * Returns a new HildonFindToolbar.
590  *
591  **/
592
593 GtkWidget *
594 hildon_find_toolbar_new(const gchar *label)
595 {
596   GtkWidget *findtoolbar;
597   
598   findtoolbar = GTK_WIDGET(g_object_new(HILDON_TYPE_FIND_TOOLBAR, NULL));
599   if(label != NULL)
600     g_object_set(findtoolbar, "label", label, NULL);
601
602   return findtoolbar;
603 }
604
605 /**
606  * hildon_find_toolbar_new_with_model
607  * @label: label for the find_toolbar, NULL to set the label to 
608  *         default "Find".
609  * @model: A @GtkListStore.
610  * @column: Indicating which column the search histry list will 
611  *          retreive string from.
612  * 
613  * Returns a new HildonFindToolbar, with a model.
614  *
615  **/
616 GtkWidget *
617 hildon_find_toolbar_new_with_model(const gchar *label,
618                                    GtkListStore *model,
619                                    gint column)
620 {
621   GtkWidget *findtoolbar;
622
623   findtoolbar = hildon_find_toolbar_new(label);
624   g_object_set(findtoolbar, "list", model,
625                "column", column, NULL);
626
627   return findtoolbar;
628 }
629
630 /**
631  * hildon_find_toolbar_highlight_entry
632  * @HildonFindToolbar: Find Toolbar whose entry is to be highlighted.
633  * @get_focus: if user passes TRUE to this value, then the text in
634  * the entry will not only get highlighted, but also get focused.
635  * 
636  * */
637 void
638 hildon_find_toolbar_highlight_entry(HildonFindToolbar *ftb,
639                                     gboolean get_focus)
640 {
641   GtkEntry *entry = NULL;
642   
643   g_return_if_fail(HILDON_IS_FIND_TOOLBAR(ftb));
644   
645   entry = hildon_find_toolbar_get_entry(ftb->priv);
646   
647   gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
648   
649   if(get_focus)
650     gtk_widget_grab_focus(GTK_WIDGET(entry));
651 }