2 * This file is part of hildon-libs
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 special toolbar to be used with HildonAppView
28 * @see_also: #HildonAppView
30 * HildonFindToolbar is a predefined toolbar for text searching purpose.
31 * It contains a GtkListStore which has the text items that the user has
32 * searched. But once the application is terminated, or HildonFindToolbar
33 * is trashed. Programmer is responsible for getting the GtkListStore through
34 * property "list", if he/she wants to use the information in the future.
35 * And through the same property, programmer is able to set the GtkListStore.
36 * Note, once the search button is pressed, string in the GtkComboxEntry is
37 * automatically added to the existing model, unless it is empty.
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(PACKAGE, 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 (void)
152 static GType find_toolbar_type = 0;
154 if (! find_toolbar_type) {
155 static const GTypeInfo find_toolbar_info = {
156 sizeof(HildonFindToolbarClass),
157 NULL, /* base_init */
158 NULL, /* base_finalize */
159 (GClassInitFunc) hildon_find_toolbar_class_init,
160 NULL, /* class_finalize */
161 NULL, /* class_data */
162 sizeof(HildonFindToolbar),
164 (GInstanceInitFunc) hildon_find_toolbar_init,
166 find_toolbar_type = g_type_register_static (GTK_TYPE_TOOLBAR,
168 &find_toolbar_info, 0);
171 return find_toolbar_type;
175 hildon_find_toolbar_get_list_model (HildonFindToolbarPrivate *priv)
177 GtkTreeModel *filter_model =
178 gtk_combo_box_get_model (GTK_COMBO_BOX (priv->entry_combo_box));
180 return filter_model == NULL ? NULL :
181 gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (filter_model));
185 hildon_find_toolbar_get_entry (HildonFindToolbarPrivate *priv)
187 return GTK_ENTRY (gtk_bin_get_child (GTK_BIN (priv->entry_combo_box)));
191 hildon_find_toolbar_filter (GtkTreeModel *model,
201 total = gtk_tree_model_iter_n_children (model, NULL);
202 g_object_get (self, "history_limit", &limit, NULL);
203 path = gtk_tree_model_get_path (model, iter);
204 indices = gtk_tree_path_get_indices (path);
206 /* set the row's index, list store has only one level */
208 gtk_tree_path_free (path);
210 /*if the row is among the latest "history_limit" additions of the
211 * model, then we show it */
212 if( (total - limit <= n) && (n < total) )
219 hildon_find_toolbar_apply_filter (HildonFindToolbar *self,
222 GtkTreeModel *filter;
223 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
226 /* Create a filter for the given model. Its only purpose is to hide
227 the oldest entries so only "history_limit" entries are visible. */
228 filter = gtk_tree_model_filter_new (model, NULL);
230 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER(filter),
231 hildon_find_toolbar_filter,
234 gtk_combo_box_set_model (GTK_COMBO_BOX (priv->entry_combo_box), filter);
236 /* ComboBox keeps the only needed reference to the filter */
237 g_object_unref (filter);
241 hildon_find_toolbar_get_property (GObject *object,
246 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (object);
255 string = gtk_label_get_text (GTK_LABEL (priv->label));
256 g_value_set_string (value, string);
260 string = gtk_entry_get_text (hildon_find_toolbar_get_entry(priv));
261 g_value_set_string (value, string);
265 g_value_set_object (value, hildon_find_toolbar_get_list_model(priv));
269 c_n = gtk_combo_box_entry_get_text_column (priv->entry_combo_box);
270 g_value_set_int (value, c_n);
274 max_len = gtk_entry_get_max_length (hildon_find_toolbar_get_entry(priv));
275 g_value_set_int (value, max_len);
278 case PROP_HISTORY_LIMIT:
279 g_value_set_int (value, priv->history_limit);
283 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
289 hildon_find_toolbar_set_property (GObject *object,
294 HildonFindToolbar *self = HILDON_FIND_TOOLBAR(object);
295 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (object);
304 string = g_value_get_string (value);
305 gtk_label_set_text (GTK_LABEL (priv->label), string);
309 string = g_value_get_string (value);
310 gtk_entry_set_text (hildon_find_toolbar_get_entry(priv), string);
314 model = GTK_TREE_MODEL (g_value_get_object(value));
315 hildon_find_toolbar_apply_filter (self, model);
319 gtk_combo_box_entry_set_text_column (priv->entry_combo_box,
320 g_value_get_int (value));
324 gtk_entry_set_max_length (hildon_find_toolbar_get_entry(priv),
325 g_value_get_int (value));
328 case PROP_HISTORY_LIMIT:
329 priv->history_limit = g_value_get_int (value);
331 /* Re-apply the history limit to the model. */
332 model = hildon_find_toolbar_get_list_model (priv);
335 /* Note that refilter function doesn't update the status of the
336 combobox popup arrow, so we'll just recreate the filter. */
337 hildon_find_toolbar_apply_filter (self, model);
339 if (gtk_combo_box_entry_get_text_column (priv->entry_combo_box) == -1)
341 /* FIXME: This is only for backwards compatibility, although
342 probably nothing actually relies on it. The behavior was only
343 an accidental side effect of original code */
344 gtk_combo_box_entry_set_text_column (priv->entry_combo_box, 0);
350 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
356 hildon_find_toolbar_find_string (HildonFindToolbar *self,
361 GtkTreeModel *model = NULL;
363 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
366 model = hildon_find_toolbar_get_list_model (priv);
368 if (gtk_tree_model_get_iter_first (model, iter))
371 gtk_tree_model_get (model, iter, column, &old_string, -1);
372 if (old_string != NULL && strcmp (string, old_string) == 0)
377 } while (gtk_tree_model_iter_next (model, iter));
384 hildon_find_toolbar_history_append (HildonFindToolbar *self,
387 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
392 GtkTreeModel *model = NULL;
393 GtkListStore *list = NULL;
395 gboolean self_create = FALSE;
397 g_object_get (self, "prefix", &string, NULL);
401 /* empty prefix, ignore */
407 /* If list store is set, get it */
408 model = hildon_find_toolbar_get_list_model(priv);
411 list = GTK_LIST_STORE (model);
412 g_object_get(self, "column", &column, NULL);
416 /* Column number is -1 if "column" property hasn't been set but
417 "list" property is. */
422 /* Latest string is always the first one in list. If the string
423 already exists, remove it so there are no duplicates in list. */
424 if (hildon_find_toolbar_find_string (self, &iter, column, string))
425 gtk_list_store_remove (list, &iter);
429 /* No list store set. Create our own. */
430 list = gtk_list_store_new (1, G_TYPE_STRING);
431 model = GTK_TREE_MODEL (list);
435 /* Add the string to first in list */
436 gtk_list_store_append (list, &iter);
437 gtk_list_store_set (list, &iter, column, string, -1);
441 /* Add the created list to ComboBoxEntry */
442 hildon_find_toolbar_apply_filter (self, model);
443 /* ComboBoxEntry keeps the only needed reference to this list */
444 g_object_unref (list);
446 /* Set the column only after ComboBoxEntry's model is set
447 in hildon_find_toolbar_apply_filter() */
448 g_object_set (self, "column", 0, NULL);
452 /* Refilter to get the oldest entry hidden from history */
453 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER(
454 gtk_combo_box_get_model (GTK_COMBO_BOX(priv->entry_combo_box))));
463 hildon_find_toolbar_emit_search (GtkButton *button,
468 /* Clicked search button. Perform search and add search prefix to history */
469 g_signal_emit_by_name(self, "search", NULL);
470 g_signal_emit_by_name(self, "history_append", &rb, NULL);
474 hildon_find_toolbar_emit_close (GtkButton *button,
477 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
480 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (priv->entry_combo_box));
481 if (GTK_WIDGET_HAS_FOCUS (entry))
483 hildon_gtk_im_context_hide (GTK_ENTRY (entry)->im_context);
486 /* Clicked close button */
487 g_signal_emit_by_name (self, "close", NULL);
491 hildon_find_toolbar_emit_invalid_input (GtkEntry *entry,
492 GtkInvalidInputType type,
495 if(type == GTK_INVALID_INPUT_MAX_CHARS_REACHED)
496 g_signal_emit_by_name (self, "invalid_input", NULL);
500 hildon_find_toolbar_entry_activate (GtkWidget *widget,
503 GtkWidget *find_toolbar = GTK_WIDGET (user_data);
506 /* NB#40936 stop focus from moving to next widget */
507 g_signal_stop_emission_by_name (widget, "activate");
509 g_signal_emit_by_name (find_toolbar, "search", NULL);
510 g_signal_emit_by_name (find_toolbar, "history_append", &rb, NULL);
514 hildon_find_toolbar_class_init (HildonFindToolbarClass *klass)
516 GObjectClass *object_class;
518 g_type_class_add_private (klass, sizeof(HildonFindToolbarPrivate));
520 object_class = G_OBJECT_CLASS(klass);
522 object_class->get_property = hildon_find_toolbar_get_property;
523 object_class->set_property = hildon_find_toolbar_set_property;
525 klass->history_append = (gpointer) hildon_find_toolbar_history_append;
527 g_object_class_install_property(object_class, PROP_LABEL,
528 g_param_spec_string("label",
529 "Label", "Displayed name for"
531 _("ecdg_ti_find_toolbar_label"),
535 g_object_class_install_property(object_class, PROP_PREFIX,
536 g_param_spec_string("prefix",
537 "Prefix", "Search string", NULL,
540 g_object_class_install_property(object_class, PROP_LIST,
541 g_param_spec_object("list",
542 "List"," GtkListStore model where "
543 "history list is kept",
547 g_object_class_install_property(object_class, PROP_COLUMN,
548 g_param_spec_int("column",
549 "Column", "Column number in GtkListStore "
550 "where history list strings are kept",
552 0, G_PARAM_READWRITE));
554 g_object_class_install_property(object_class, PROP_MAX,
555 g_param_spec_int("max_characters",
556 "Maximum number of characters",
557 "Maximum number of characters "
560 0, G_PARAM_READWRITE |
563 g_object_class_install_property(object_class, PROP_HISTORY_LIMIT,
564 g_param_spec_int("history_limit",
565 "Maximum number of history items",
566 "Maximum number of history items "
567 "in search combobox",
569 5, G_PARAM_READWRITE |
573 * HildonFindToolbar::search:
574 * @toolbar: the toolbar which received the signal
576 * Gets emitted when the find button is pressed.
578 HildonFindToolbar_signal[SEARCH] =
580 "search", HILDON_TYPE_FIND_TOOLBAR,
581 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
582 (HildonFindToolbarClass, search),
583 NULL, NULL, gtk_marshal_VOID__VOID,
587 * HildonFindToolbar::close:
588 * @toolbar: the toolbar which received the signal
590 * Gets emitted when the close button is pressed.
592 HildonFindToolbar_signal[CLOSE] =
594 "close", HILDON_TYPE_FIND_TOOLBAR,
595 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
596 (HildonFindToolbarClass, close),
597 NULL, NULL, gtk_marshal_VOID__VOID,
601 * HildonFindToolbar::invalid-input:
602 * @toolbar: the toolbar which received the signal
604 * Gets emitted when the maximum search prefix length is reached and
605 * user tries to type more.
607 HildonFindToolbar_signal[INVALID_INPUT] =
609 "invalid_input", HILDON_TYPE_FIND_TOOLBAR,
610 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
611 (HildonFindToolbarClass, invalid_input),
612 NULL, NULL, gtk_marshal_VOID__VOID,
616 * HildonFindToolbar::history-append:
617 * @toolbar: the toolbar which received the signal
619 * Gets emitted when the current search prefix should be added to history.
621 HildonFindToolbar_signal[HISTORY_APPEND] =
623 "history_append", HILDON_TYPE_FIND_TOOLBAR,
624 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
625 (HildonFindToolbarClass, history_append),
626 g_signal_accumulator_true_handled, NULL,
627 gtk_marshal_BOOLEAN__VOID,
632 hildon_find_toolbar_init (HildonFindToolbar *self)
634 GtkToolItem *label_container;
635 GtkToolItem *entry_combo_box_container;
637 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
640 /* Create the label */
641 priv->label = gtk_label_new (_("ecdg_ti_find_toolbar_label"));
643 gtk_misc_set_padding (GTK_MISC (priv->label), FIND_LABEL_XPADDING,
644 FIND_LABEL_YPADDING);
646 label_container = gtk_tool_item_new ();
647 gtk_container_add (GTK_CONTAINER (label_container),
650 gtk_widget_show_all (GTK_WIDGET (label_container));
651 gtk_toolbar_insert (GTK_TOOLBAR (self), label_container, -1);
653 /* ComboBoxEntry for search prefix string / history list */
654 priv->entry_combo_box = GTK_COMBO_BOX_ENTRY (gtk_combo_box_entry_new ());
655 g_signal_connect (hildon_find_toolbar_get_entry(priv),
657 G_CALLBACK(hildon_find_toolbar_emit_invalid_input), self);
659 entry_combo_box_container = gtk_tool_item_new ();
661 gtk_tool_item_set_expand (entry_combo_box_container, TRUE);
662 gtk_container_add (GTK_CONTAINER (entry_combo_box_container),
663 GTK_WIDGET (priv->entry_combo_box));
664 gtk_widget_show_all(GTK_WIDGET (entry_combo_box_container));
665 gtk_toolbar_insert (GTK_TOOLBAR (self), entry_combo_box_container, -1);
666 g_signal_connect (hildon_find_toolbar_get_entry (priv),
668 G_CALLBACK(hildon_find_toolbar_entry_activate), self);
671 priv->find_button = gtk_tool_button_new (
672 gtk_image_new_from_icon_name ("qgn_toolb_browser_gobutton",
673 HILDON_ICON_SIZE_TOOLBAR),
676 g_signal_connect (priv->find_button, "clicked",
677 G_CALLBACK(hildon_find_toolbar_emit_search), self);
678 gtk_widget_show_all( GTK_WIDGET(priv->find_button));
679 gtk_toolbar_insert ( GTK_TOOLBAR(self), priv->find_button, -1);
680 if ( GTK_WIDGET_CAN_FOCUS( GTK_BIN(priv->find_button)->child) )
681 GTK_WIDGET_UNSET_FLAGS(
682 GTK_BIN(priv->find_button)->child, GTK_CAN_FOCUS);
685 priv->separator = gtk_separator_tool_item_new();
686 gtk_widget_show(GTK_WIDGET(priv->separator));
687 gtk_toolbar_insert (GTK_TOOLBAR(self), priv->separator, -1);
690 priv->close_button = gtk_tool_button_new (
691 gtk_image_new_from_icon_name ("qgn_toolb_gene_close",
692 HILDON_ICON_SIZE_TOOLBAR),
694 g_signal_connect(priv->close_button, "clicked",
695 G_CALLBACK(hildon_find_toolbar_emit_close), self);
696 gtk_widget_show_all(GTK_WIDGET(priv->close_button));
697 gtk_toolbar_insert (GTK_TOOLBAR(self), priv->close_button, -1);
698 if ( GTK_WIDGET_CAN_FOCUS( GTK_BIN(priv->close_button)->child) )
699 GTK_WIDGET_UNSET_FLAGS(
700 GTK_BIN(priv->close_button)->child, GTK_CAN_FOCUS);
704 * hildon_find_toolbar_new:
705 * @label: label for the find_toolbar, NULL to set the label to
708 * Returns a new HildonFindToolbar.
710 * Returns: a new HildonFindToolbar
714 hildon_find_toolbar_new (const gchar *label)
716 GtkWidget *findtoolbar;
718 findtoolbar = GTK_WIDGET (g_object_new (HILDON_TYPE_FIND_TOOLBAR, NULL));
721 g_object_set(findtoolbar, "label", label, NULL);
727 * hildon_find_toolbar_new_with_model
728 * @label: label for the find_toolbar, NULL to set the label to
730 * @model: a @GtkListStore
731 * @column: indicating which column the search histry list will
732 * retreive string from
734 * Returns a new HildonFindToolbar, with a model.
736 * Returns: a new #HildonFindToolbar
739 hildon_find_toolbar_new_with_model (const gchar *label,
743 GtkWidget *findtoolbar;
745 findtoolbar = hildon_find_toolbar_new (label);
747 g_object_set (findtoolbar, "list", model, "column", column, NULL);
753 * hildon_find_toolbar_highlight_entry
754 * @ftb: find Toolbar whose entry is to be highlighted
755 * @get_focus: if user passes TRUE to this value, then the text in
756 * the entry will not only get highlighted, but also get focused.
760 hildon_find_toolbar_highlight_entry (HildonFindToolbar *self,
763 GtkEntry *entry = NULL;
764 HildonFindToolbarPrivate *priv;
766 g_return_if_fail (HILDON_IS_FIND_TOOLBAR (self));
767 priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
770 entry = hildon_find_toolbar_get_entry (priv);
772 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
775 gtk_widget_grab_focus (GTK_WIDGET (entry));