2 * This file is a part of hildon
4 * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
6 * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
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
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.
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
26 * SECTION:hildon-find-toolbar
27 * @short_description: A toolbar with a search field
28 * @see_also: #HildonWindow
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.
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>
53 #include "hildon-find-toolbar-private.h"
56 dgettext("hildon-libs", String)
58 /* Same define as gtkentry.c as entry will further handle this */
60 #define MAX_SIZE G_MAXUSHORT
62 #define FIND_LABEL_XPADDING 6
64 #define FIND_LABEL_YPADDING 0
67 hildon_find_toolbar_get_list_model (HildonFindToolbarPrivate *priv);
70 hildon_find_toolbar_get_entry (HildonFindToolbarPrivate *priv);
73 hildon_find_toolbar_filter (GtkTreeModel *model,
78 hildon_find_toolbar_apply_filter (HildonFindToolbar *self,
82 hildon_find_toolbar_get_property (GObject *object,
88 hildon_find_toolbar_set_property (GObject *object,
94 hildon_find_toolbar_find_string (HildonFindToolbar *self,
100 hildon_find_toolbar_history_append (HildonFindToolbar *self,
104 hildon_find_toolbar_emit_search (GtkButton *button,
108 hildon_find_toolbar_emit_close (GtkButton *button,
112 hildon_find_toolbar_emit_invalid_input (GtkEntry *entry,
113 GtkInvalidInputType type,
117 hildon_find_toolbar_entry_activate (GtkWidget *widget,
121 hildon_find_toolbar_class_init (HildonFindToolbarClass *klass);
124 hildon_find_toolbar_init (HildonFindToolbar *self);
147 static guint HildonFindToolbar_signal [LAST_SIGNAL] = {0};
150 * hildon_find_toolbar_get_type:
152 * Initializes and returns the type of a hildon fond toolbar.
154 * @Returns: GType of #HildonFindToolbar
157 hildon_find_toolbar_get_type (void)
159 static GType find_toolbar_type = 0;
161 if (! find_toolbar_type) {
162 static const GTypeInfo find_toolbar_info = {
163 sizeof(HildonFindToolbarClass),
164 NULL, /* base_init */
165 NULL, /* base_finalize */
166 (GClassInitFunc) hildon_find_toolbar_class_init,
167 NULL, /* class_finalize */
168 NULL, /* class_data */
169 sizeof(HildonFindToolbar),
171 (GInstanceInitFunc) hildon_find_toolbar_init,
173 find_toolbar_type = g_type_register_static (GTK_TYPE_TOOLBAR,
175 &find_toolbar_info, 0);
178 return find_toolbar_type;
182 hildon_find_toolbar_get_list_model (HildonFindToolbarPrivate *priv)
184 GtkTreeModel *filter_model =
185 gtk_combo_box_get_model (GTK_COMBO_BOX (priv->entry_combo_box));
187 return filter_model == NULL ? NULL :
188 gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (filter_model));
192 hildon_find_toolbar_get_entry (HildonFindToolbarPrivate *priv)
194 return GTK_ENTRY (gtk_bin_get_child (GTK_BIN (priv->entry_combo_box)));
198 hildon_find_toolbar_filter (GtkTreeModel *model,
208 total = gtk_tree_model_iter_n_children (model, NULL);
209 g_object_get (self, "history_limit", &limit, NULL);
210 path = gtk_tree_model_get_path (model, iter);
211 indices = gtk_tree_path_get_indices (path);
213 /* set the row's index, list store has only one level */
215 gtk_tree_path_free (path);
217 /*if the row is among the latest "history_limit" additions of the
218 * model, then we show it */
219 if( (total - limit <= n) && (n < total) )
226 hildon_find_toolbar_apply_filter (HildonFindToolbar *self,
229 GtkTreeModel *filter;
230 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
233 /* Create a filter for the given model. Its only purpose is to hide
234 the oldest entries so only "history_limit" entries are visible. */
235 filter = gtk_tree_model_filter_new (model, NULL);
237 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER(filter),
238 hildon_find_toolbar_filter,
241 gtk_combo_box_set_model (GTK_COMBO_BOX (priv->entry_combo_box), filter);
243 /* ComboBox keeps the only needed reference to the filter */
244 g_object_unref (filter);
248 hildon_find_toolbar_get_property (GObject *object,
253 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (object);
262 string = gtk_label_get_text (GTK_LABEL (priv->label));
263 g_value_set_string (value, string);
267 string = gtk_entry_get_text (hildon_find_toolbar_get_entry(priv));
268 g_value_set_string (value, string);
272 g_value_set_object (value, hildon_find_toolbar_get_list_model(priv));
276 c_n = gtk_combo_box_entry_get_text_column (priv->entry_combo_box);
277 g_value_set_int (value, c_n);
281 max_len = gtk_entry_get_max_length (hildon_find_toolbar_get_entry(priv));
282 g_value_set_int (value, max_len);
285 case PROP_HISTORY_LIMIT:
286 g_value_set_int (value, priv->history_limit);
290 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
296 hildon_find_toolbar_set_property (GObject *object,
301 HildonFindToolbar *self = HILDON_FIND_TOOLBAR(object);
302 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (object);
311 string = g_value_get_string (value);
312 gtk_label_set_text (GTK_LABEL (priv->label), string);
316 string = g_value_get_string (value);
317 gtk_entry_set_text (hildon_find_toolbar_get_entry(priv), string);
321 model = GTK_TREE_MODEL (g_value_get_object(value));
322 hildon_find_toolbar_apply_filter (self, model);
326 gtk_combo_box_entry_set_text_column (priv->entry_combo_box,
327 g_value_get_int (value));
331 gtk_entry_set_max_length (hildon_find_toolbar_get_entry(priv),
332 g_value_get_int (value));
335 case PROP_HISTORY_LIMIT:
336 priv->history_limit = g_value_get_int (value);
338 /* Re-apply the history limit to the model. */
339 model = hildon_find_toolbar_get_list_model (priv);
342 /* Note that refilter function doesn't update the status of the
343 combobox popup arrow, so we'll just recreate the filter. */
344 hildon_find_toolbar_apply_filter (self, model);
346 if (gtk_combo_box_entry_get_text_column (priv->entry_combo_box) == -1)
348 /* FIXME: This is only for backwards compatibility, although
349 probably nothing actually relies on it. The behavior was only
350 an accidental side effect of original code */
351 gtk_combo_box_entry_set_text_column (priv->entry_combo_box, 0);
357 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
363 hildon_find_toolbar_find_string (HildonFindToolbar *self,
368 GtkTreeModel *model = NULL;
370 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
373 model = hildon_find_toolbar_get_list_model (priv);
375 if (gtk_tree_model_get_iter_first (model, iter))
378 gtk_tree_model_get (model, iter, column, &old_string, -1);
379 if (old_string != NULL && strcmp (string, old_string) == 0)
384 } while (gtk_tree_model_iter_next (model, iter));
391 hildon_find_toolbar_history_append (HildonFindToolbar *self,
394 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
399 GtkTreeModel *model = NULL;
400 GtkListStore *list = NULL;
402 gboolean self_create = FALSE;
404 g_object_get (self, "prefix", &string, NULL);
408 /* empty prefix, ignore */
414 /* If list store is set, get it */
415 model = hildon_find_toolbar_get_list_model(priv);
418 list = GTK_LIST_STORE (model);
419 g_object_get(self, "column", &column, NULL);
423 /* Column number is -1 if "column" property hasn't been set but
424 "list" property is. */
429 /* Latest string is always the first one in list. If the string
430 already exists, remove it so there are no duplicates in list. */
431 if (hildon_find_toolbar_find_string (self, &iter, column, string))
432 gtk_list_store_remove (list, &iter);
436 /* No list store set. Create our own. */
437 list = gtk_list_store_new (1, G_TYPE_STRING);
438 model = GTK_TREE_MODEL (list);
442 /* Add the string to first in list */
443 gtk_list_store_append (list, &iter);
444 gtk_list_store_set (list, &iter, column, string, -1);
448 /* Add the created list to ComboBoxEntry */
449 hildon_find_toolbar_apply_filter (self, model);
450 /* ComboBoxEntry keeps the only needed reference to this list */
451 g_object_unref (list);
453 /* Set the column only after ComboBoxEntry's model is set
454 in hildon_find_toolbar_apply_filter() */
455 g_object_set (self, "column", 0, NULL);
459 /* Refilter to get the oldest entry hidden from history */
460 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER(
461 gtk_combo_box_get_model (GTK_COMBO_BOX(priv->entry_combo_box))));
470 hildon_find_toolbar_emit_search (GtkButton *button,
475 /* Clicked search button. Perform search and add search prefix to history */
476 g_signal_emit_by_name(self, "search", NULL);
477 g_signal_emit_by_name(self, "history_append", &rb, NULL);
481 hildon_find_toolbar_emit_close (GtkButton *button,
484 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
487 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (priv->entry_combo_box));
488 if (GTK_WIDGET_HAS_FOCUS (entry))
490 hildon_gtk_im_context_hide (GTK_ENTRY (entry)->im_context);
493 /* Clicked close button */
494 g_signal_emit_by_name (self, "close", NULL);
498 hildon_find_toolbar_emit_invalid_input (GtkEntry *entry,
499 GtkInvalidInputType type,
502 if(type == GTK_INVALID_INPUT_MAX_CHARS_REACHED)
503 g_signal_emit_by_name (self, "invalid_input", NULL);
507 hildon_find_toolbar_entry_activate (GtkWidget *widget,
510 GtkWidget *find_toolbar = GTK_WIDGET (user_data);
513 /* NB#40936 stop focus from moving to next widget */
514 g_signal_stop_emission_by_name (widget, "activate");
516 g_signal_emit_by_name (find_toolbar, "search", NULL);
517 g_signal_emit_by_name (find_toolbar, "history_append", &rb, NULL);
521 hildon_find_toolbar_class_init (HildonFindToolbarClass *klass)
523 GObjectClass *object_class;
525 g_type_class_add_private (klass, sizeof (HildonFindToolbarPrivate));
527 object_class = G_OBJECT_CLASS(klass);
529 object_class->get_property = hildon_find_toolbar_get_property;
530 object_class->set_property = hildon_find_toolbar_set_property;
532 klass->history_append = (gpointer) hildon_find_toolbar_history_append;
535 * HildonFindToolbar:label:
537 * The label to display before the search box.
540 g_object_class_install_property (object_class, PROP_LABEL,
541 g_param_spec_string ("label",
542 "Label", "Displayed name for"
544 _("ecdg_ti_find_toolbar_label"),
549 * HildonFindToolbar:label:
551 * The label to display before the search box.
554 g_object_class_install_property (object_class, PROP_PREFIX,
555 g_param_spec_string ("prefix",
556 "Prefix", "Search string", NULL,
560 * HildonFindToolbar:list:
562 * A #GtkListStore where the search history is kept.
565 g_object_class_install_property (object_class, PROP_LIST,
566 g_param_spec_object ("list",
567 "List"," GtkListStore model where "
568 "history list is kept",
573 * HildonFindToolbar:column:
575 * The column number in GtkListStore where strings of
576 * search history are kept.
579 g_object_class_install_property(object_class, PROP_COLUMN,
580 g_param_spec_int ("column",
581 "Column", "Column number in GtkListStore "
582 "where history list strings are kept",
584 0, G_PARAM_READWRITE));
587 * HildonFindToolbar:label:
589 * The label to display before the search box.
592 g_object_class_install_property (object_class, PROP_MAX,
593 g_param_spec_int ("max_characters",
594 "Maximum number of characters",
595 "Maximum number of characters "
598 0, G_PARAM_READWRITE |
602 * HildonFindToolbar:history-limit:
604 * Maximum number of history items in the combobox.
607 g_object_class_install_property (object_class, PROP_HISTORY_LIMIT,
608 g_param_spec_int ("history-limit",
609 "Maximum number of history items",
610 "Maximum number of history items "
611 "in search combobox",
613 5, G_PARAM_READWRITE |
617 * HildonFindToolbar::search:
618 * @toolbar: the toolbar which received the signal
620 * Gets emitted when the find button is pressed.
622 HildonFindToolbar_signal[SEARCH] =
624 "search", HILDON_TYPE_FIND_TOOLBAR,
625 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
626 (HildonFindToolbarClass, search),
627 NULL, NULL, gtk_marshal_VOID__VOID,
631 * HildonFindToolbar::close:
632 * @toolbar: the toolbar which received the signal
634 * Gets emitted when the close button is pressed.
636 HildonFindToolbar_signal[CLOSE] =
638 "close", HILDON_TYPE_FIND_TOOLBAR,
639 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
640 (HildonFindToolbarClass, close),
641 NULL, NULL, gtk_marshal_VOID__VOID,
645 * HildonFindToolbar::invalid-input:
646 * @toolbar: the toolbar which received the signal
648 * Gets emitted when the maximum search prefix length is reached and
649 * user tries to type more.
651 HildonFindToolbar_signal[INVALID_INPUT] =
653 "invalid_input", HILDON_TYPE_FIND_TOOLBAR,
654 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
655 (HildonFindToolbarClass, invalid_input),
656 NULL, NULL, gtk_marshal_VOID__VOID,
660 * HildonFindToolbar::history-append:
661 * @toolbar: the toolbar which received the signal
663 * Gets emitted when the current search prefix should be added to history.
665 HildonFindToolbar_signal[HISTORY_APPEND] =
667 "history_append", HILDON_TYPE_FIND_TOOLBAR,
668 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
669 (HildonFindToolbarClass, history_append),
670 g_signal_accumulator_true_handled, NULL,
671 gtk_marshal_BOOLEAN__VOID,
676 hildon_find_toolbar_init (HildonFindToolbar *self)
678 GtkToolItem *label_container;
679 GtkToolItem *entry_combo_box_container;
681 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
684 /* Create the label */
685 priv->label = gtk_label_new (_("ecdg_ti_find_toolbar_label"));
687 gtk_misc_set_padding (GTK_MISC (priv->label), FIND_LABEL_XPADDING,
688 FIND_LABEL_YPADDING);
690 label_container = gtk_tool_item_new ();
691 gtk_container_add (GTK_CONTAINER (label_container),
694 gtk_widget_show_all (GTK_WIDGET (label_container));
695 gtk_toolbar_insert (GTK_TOOLBAR (self), label_container, -1);
697 /* ComboBoxEntry for search prefix string / history list */
698 priv->entry_combo_box = GTK_COMBO_BOX_ENTRY (gtk_combo_box_entry_new ());
699 g_signal_connect (hildon_find_toolbar_get_entry(priv),
701 G_CALLBACK(hildon_find_toolbar_emit_invalid_input), self);
703 entry_combo_box_container = gtk_tool_item_new ();
705 gtk_tool_item_set_expand (entry_combo_box_container, TRUE);
706 gtk_container_add (GTK_CONTAINER (entry_combo_box_container),
707 GTK_WIDGET (priv->entry_combo_box));
708 gtk_widget_show_all(GTK_WIDGET (entry_combo_box_container));
709 gtk_toolbar_insert (GTK_TOOLBAR (self), entry_combo_box_container, -1);
710 g_signal_connect (hildon_find_toolbar_get_entry (priv),
712 G_CALLBACK(hildon_find_toolbar_entry_activate), self);
715 priv->find_button = gtk_tool_button_new (
716 gtk_image_new_from_icon_name ("qgn_toolb_browser_gobutton",
717 HILDON_ICON_SIZE_TOOLBAR),
720 g_signal_connect (priv->find_button, "clicked",
721 G_CALLBACK(hildon_find_toolbar_emit_search), self);
722 gtk_widget_show_all( GTK_WIDGET(priv->find_button));
723 gtk_toolbar_insert ( GTK_TOOLBAR(self), priv->find_button, -1);
724 if ( GTK_WIDGET_CAN_FOCUS( GTK_BIN(priv->find_button)->child) )
725 GTK_WIDGET_UNSET_FLAGS(
726 GTK_BIN(priv->find_button)->child, GTK_CAN_FOCUS);
729 priv->separator = gtk_separator_tool_item_new();
730 gtk_widget_show(GTK_WIDGET(priv->separator));
731 gtk_toolbar_insert (GTK_TOOLBAR(self), priv->separator, -1);
734 priv->close_button = gtk_tool_button_new (
735 gtk_image_new_from_icon_name ("qgn_toolb_gene_close",
736 HILDON_ICON_SIZE_TOOLBAR),
738 g_signal_connect(priv->close_button, "clicked",
739 G_CALLBACK(hildon_find_toolbar_emit_close), self);
740 gtk_widget_show_all(GTK_WIDGET(priv->close_button));
741 gtk_toolbar_insert (GTK_TOOLBAR(self), priv->close_button, -1);
742 if ( GTK_WIDGET_CAN_FOCUS( GTK_BIN(priv->close_button)->child) )
743 GTK_WIDGET_UNSET_FLAGS(
744 GTK_BIN(priv->close_button)->child, GTK_CAN_FOCUS);
748 * hildon_find_toolbar_new:
749 * @label: label for the find_toolbar, NULL to set the label to
752 * Creates a new HildonFindToolbar.
754 * Returns: a new HildonFindToolbar
757 hildon_find_toolbar_new (const gchar *label)
759 GtkWidget *findtoolbar;
761 findtoolbar = GTK_WIDGET (g_object_new (HILDON_TYPE_FIND_TOOLBAR, NULL));
764 g_object_set(findtoolbar, "label", label, NULL);
770 * hildon_find_toolbar_new_with_model:
771 * @label: label for the find_toolbar, NULL to set the label to
773 * @model: a @GtkListStore
774 * @column: indicating which column the search histry list will
775 * retreive string from
777 * Creates a new HildonFindToolbar with a model.
779 * Returns: a new #HildonFindToolbar
782 hildon_find_toolbar_new_with_model (const gchar *label,
786 GtkWidget *findtoolbar;
788 findtoolbar = hildon_find_toolbar_new (label);
790 g_object_set (findtoolbar, "list", model, "column", column, NULL);
796 * hildon_find_toolbar_highlight_entry:
797 * @ftb: find Toolbar whose entry is to be highlighted
798 * @get_focus: if user passes TRUE to this value, then the text in
799 * the entry will not only get highlighted, but also get focused.
801 * Highlights the current entry in the find toolbar.
805 hildon_find_toolbar_highlight_entry (HildonFindToolbar *self,
808 GtkEntry *entry = NULL;
809 HildonFindToolbarPrivate *priv;
811 g_return_if_fail (HILDON_IS_FIND_TOOLBAR (self));
812 priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
815 entry = hildon_find_toolbar_get_entry (priv);
817 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
820 gtk_widget_grab_focus (GTK_WIDGET (entry));