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 special toolbar to be used with HildonWindow.
28 * @see_also: #HildonWindow
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.
44 #include "hildon-find-toolbar.h"
45 #include "hildon-defines.h"
46 #include <gdk/gdkkeysyms.h>
47 #include <gtk/gtklabel.h>
48 #include <gtk/gtkentry.h>
49 #include <gtk/gtkbutton.h>
50 #include <gtk/gtktoolbutton.h>
51 #include <gtk/gtktoolitem.h>
52 #include <gtk/gtkcomboboxentry.h>
53 #include <gtk/gtkseparatortoolitem.h>
56 #include "hildon-find-toolbar-private.h"
59 dgettext("hildon-libs", String)
61 /* Same define as gtkentry.c as entry will further handle this */
63 #define MAX_SIZE G_MAXUSHORT
65 #define FIND_LABEL_XPADDING 6
67 #define FIND_LABEL_YPADDING 0
70 hildon_find_toolbar_get_list_model (HildonFindToolbarPrivate *priv);
73 hildon_find_toolbar_get_entry (HildonFindToolbarPrivate *priv);
76 hildon_find_toolbar_filter (GtkTreeModel *model,
81 hildon_find_toolbar_apply_filter (HildonFindToolbar *self,
85 hildon_find_toolbar_get_property (GObject *object,
91 hildon_find_toolbar_set_property (GObject *object,
97 hildon_find_toolbar_find_string (HildonFindToolbar *self,
100 const gchar *string);
103 hildon_find_toolbar_history_append (HildonFindToolbar *self,
107 hildon_find_toolbar_emit_search (GtkButton *button,
111 hildon_find_toolbar_emit_close (GtkButton *button,
115 hildon_find_toolbar_emit_invalid_input (GtkEntry *entry,
116 GtkInvalidInputType type,
120 hildon_find_toolbar_entry_activate (GtkWidget *widget,
124 hildon_find_toolbar_class_init (HildonFindToolbarClass *klass);
127 hildon_find_toolbar_init (HildonFindToolbar *self);
150 static guint HildonFindToolbar_signal [LAST_SIGNAL] = {0};
153 * hildon_find_toolbar_get_type:
155 * Initializes and returns the type of a hildon fond toolbar.
157 * @Returns: GType of #HildonFindToolbar
160 hildon_find_toolbar_get_type (void)
162 static GType find_toolbar_type = 0;
164 if (! find_toolbar_type) {
165 static const GTypeInfo find_toolbar_info = {
166 sizeof(HildonFindToolbarClass),
167 NULL, /* base_init */
168 NULL, /* base_finalize */
169 (GClassInitFunc) hildon_find_toolbar_class_init,
170 NULL, /* class_finalize */
171 NULL, /* class_data */
172 sizeof(HildonFindToolbar),
174 (GInstanceInitFunc) hildon_find_toolbar_init,
176 find_toolbar_type = g_type_register_static (GTK_TYPE_TOOLBAR,
178 &find_toolbar_info, 0);
181 return find_toolbar_type;
185 hildon_find_toolbar_get_list_model (HildonFindToolbarPrivate *priv)
187 GtkTreeModel *filter_model =
188 gtk_combo_box_get_model (GTK_COMBO_BOX (priv->entry_combo_box));
190 return filter_model == NULL ? NULL :
191 gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (filter_model));
195 hildon_find_toolbar_get_entry (HildonFindToolbarPrivate *priv)
197 return GTK_ENTRY (gtk_bin_get_child (GTK_BIN (priv->entry_combo_box)));
201 hildon_find_toolbar_filter (GtkTreeModel *model,
211 total = gtk_tree_model_iter_n_children (model, NULL);
212 g_object_get (self, "history_limit", &limit, NULL);
213 path = gtk_tree_model_get_path (model, iter);
214 indices = gtk_tree_path_get_indices (path);
216 /* set the row's index, list store has only one level */
218 gtk_tree_path_free (path);
220 /*if the row is among the latest "history_limit" additions of the
221 * model, then we show it */
222 if( (total - limit <= n) && (n < total) )
229 hildon_find_toolbar_apply_filter (HildonFindToolbar *self,
232 GtkTreeModel *filter;
233 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
236 /* Create a filter for the given model. Its only purpose is to hide
237 the oldest entries so only "history_limit" entries are visible. */
238 filter = gtk_tree_model_filter_new (model, NULL);
240 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER(filter),
241 hildon_find_toolbar_filter,
244 gtk_combo_box_set_model (GTK_COMBO_BOX (priv->entry_combo_box), filter);
246 /* ComboBox keeps the only needed reference to the filter */
247 g_object_unref (filter);
251 hildon_find_toolbar_get_property (GObject *object,
256 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (object);
265 string = gtk_label_get_text (GTK_LABEL (priv->label));
266 g_value_set_string (value, string);
270 string = gtk_entry_get_text (hildon_find_toolbar_get_entry(priv));
271 g_value_set_string (value, string);
275 g_value_set_object (value, hildon_find_toolbar_get_list_model(priv));
279 c_n = gtk_combo_box_entry_get_text_column (priv->entry_combo_box);
280 g_value_set_int (value, c_n);
284 max_len = gtk_entry_get_max_length (hildon_find_toolbar_get_entry(priv));
285 g_value_set_int (value, max_len);
288 case PROP_HISTORY_LIMIT:
289 g_value_set_int (value, priv->history_limit);
293 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
299 hildon_find_toolbar_set_property (GObject *object,
304 HildonFindToolbar *self = HILDON_FIND_TOOLBAR(object);
305 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (object);
314 string = g_value_get_string (value);
315 gtk_label_set_text (GTK_LABEL (priv->label), string);
319 string = g_value_get_string (value);
320 gtk_entry_set_text (hildon_find_toolbar_get_entry(priv), string);
324 model = GTK_TREE_MODEL (g_value_get_object(value));
325 hildon_find_toolbar_apply_filter (self, model);
329 gtk_combo_box_entry_set_text_column (priv->entry_combo_box,
330 g_value_get_int (value));
334 gtk_entry_set_max_length (hildon_find_toolbar_get_entry(priv),
335 g_value_get_int (value));
338 case PROP_HISTORY_LIMIT:
339 priv->history_limit = g_value_get_int (value);
341 /* Re-apply the history limit to the model. */
342 model = hildon_find_toolbar_get_list_model (priv);
345 /* Note that refilter function doesn't update the status of the
346 combobox popup arrow, so we'll just recreate the filter. */
347 hildon_find_toolbar_apply_filter (self, model);
349 if (gtk_combo_box_entry_get_text_column (priv->entry_combo_box) == -1)
351 /* FIXME: This is only for backwards compatibility, although
352 probably nothing actually relies on it. The behavior was only
353 an accidental side effect of original code */
354 gtk_combo_box_entry_set_text_column (priv->entry_combo_box, 0);
360 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
366 hildon_find_toolbar_find_string (HildonFindToolbar *self,
371 GtkTreeModel *model = NULL;
373 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
376 model = hildon_find_toolbar_get_list_model (priv);
378 if (gtk_tree_model_get_iter_first (model, iter))
381 gtk_tree_model_get (model, iter, column, &old_string, -1);
382 if (old_string != NULL && strcmp (string, old_string) == 0)
387 } while (gtk_tree_model_iter_next (model, iter));
394 hildon_find_toolbar_history_append (HildonFindToolbar *self,
397 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
402 GtkTreeModel *model = NULL;
403 GtkListStore *list = NULL;
405 gboolean self_create = FALSE;
407 g_object_get (self, "prefix", &string, NULL);
411 /* empty prefix, ignore */
417 /* If list store is set, get it */
418 model = hildon_find_toolbar_get_list_model(priv);
421 list = GTK_LIST_STORE (model);
422 g_object_get(self, "column", &column, NULL);
426 /* Column number is -1 if "column" property hasn't been set but
427 "list" property is. */
432 /* Latest string is always the first one in list. If the string
433 already exists, remove it so there are no duplicates in list. */
434 if (hildon_find_toolbar_find_string (self, &iter, column, string))
435 gtk_list_store_remove (list, &iter);
439 /* No list store set. Create our own. */
440 list = gtk_list_store_new (1, G_TYPE_STRING);
441 model = GTK_TREE_MODEL (list);
445 /* Add the string to first in list */
446 gtk_list_store_append (list, &iter);
447 gtk_list_store_set (list, &iter, column, string, -1);
451 /* Add the created list to ComboBoxEntry */
452 hildon_find_toolbar_apply_filter (self, model);
453 /* ComboBoxEntry keeps the only needed reference to this list */
454 g_object_unref (list);
456 /* Set the column only after ComboBoxEntry's model is set
457 in hildon_find_toolbar_apply_filter() */
458 g_object_set (self, "column", 0, NULL);
462 /* Refilter to get the oldest entry hidden from history */
463 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER(
464 gtk_combo_box_get_model (GTK_COMBO_BOX(priv->entry_combo_box))));
473 hildon_find_toolbar_emit_search (GtkButton *button,
478 /* Clicked search button. Perform search and add search prefix to history */
479 g_signal_emit_by_name(self, "search", NULL);
480 g_signal_emit_by_name(self, "history_append", &rb, NULL);
484 hildon_find_toolbar_emit_close (GtkButton *button,
487 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
490 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (priv->entry_combo_box));
491 if (GTK_WIDGET_HAS_FOCUS (entry))
493 hildon_gtk_im_context_hide (GTK_ENTRY (entry)->im_context);
496 /* Clicked close button */
497 g_signal_emit_by_name (self, "close", NULL);
501 hildon_find_toolbar_emit_invalid_input (GtkEntry *entry,
502 GtkInvalidInputType type,
505 if(type == GTK_INVALID_INPUT_MAX_CHARS_REACHED)
506 g_signal_emit_by_name (self, "invalid_input", NULL);
510 hildon_find_toolbar_entry_activate (GtkWidget *widget,
513 GtkWidget *find_toolbar = GTK_WIDGET (user_data);
516 /* NB#40936 stop focus from moving to next widget */
517 g_signal_stop_emission_by_name (widget, "activate");
519 g_signal_emit_by_name (find_toolbar, "search", NULL);
520 g_signal_emit_by_name (find_toolbar, "history_append", &rb, NULL);
524 hildon_find_toolbar_class_init (HildonFindToolbarClass *klass)
526 GObjectClass *object_class;
528 g_type_class_add_private (klass, sizeof (HildonFindToolbarPrivate));
530 object_class = G_OBJECT_CLASS(klass);
532 object_class->get_property = hildon_find_toolbar_get_property;
533 object_class->set_property = hildon_find_toolbar_set_property;
535 klass->history_append = (gpointer) hildon_find_toolbar_history_append;
538 * HildonFindToolbar:label:
540 * The label to display before the search box.
543 g_object_class_install_property (object_class, PROP_LABEL,
544 g_param_spec_string ("label",
545 "Label", "Displayed name for"
547 _("ecdg_ti_find_toolbar_label"),
552 * HildonFindToolbar:label:
554 * The label to display before the search box.
557 g_object_class_install_property (object_class, PROP_PREFIX,
558 g_param_spec_string ("prefix",
559 "Prefix", "Search string", NULL,
563 * HildonFindToolbar:list:
565 * A #GtkListStore where the search history is kept.
568 g_object_class_install_property (object_class, PROP_LIST,
569 g_param_spec_object ("list",
570 "List"," GtkListStore model where "
571 "history list is kept",
576 * HildonFindToolbar:column:
578 * The column number in GtkListStore where strings of
579 * search history are kept.
582 g_object_class_install_property(object_class, PROP_COLUMN,
583 g_param_spec_int ("column",
584 "Column", "Column number in GtkListStore "
585 "where history list strings are kept",
587 0, G_PARAM_READWRITE));
590 * HildonFindToolbar:label:
592 * The label to display before the search box.
595 g_object_class_install_property (object_class, PROP_MAX,
596 g_param_spec_int ("max_characters",
597 "Maximum number of characters",
598 "Maximum number of characters "
601 0, G_PARAM_READWRITE |
605 * HildonFindToolbar:history-limit:
607 * Maximum number of history items in the combobox.
610 g_object_class_install_property (object_class, PROP_HISTORY_LIMIT,
611 g_param_spec_int ("history-limit",
612 "Maximum number of history items",
613 "Maximum number of history items "
614 "in search combobox",
616 5, G_PARAM_READWRITE |
620 * HildonFindToolbar::search:
621 * @toolbar: the toolbar which received the signal
623 * Gets emitted when the find button is pressed.
625 HildonFindToolbar_signal[SEARCH] =
627 "search", HILDON_TYPE_FIND_TOOLBAR,
628 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
629 (HildonFindToolbarClass, search),
630 NULL, NULL, gtk_marshal_VOID__VOID,
634 * HildonFindToolbar::close:
635 * @toolbar: the toolbar which received the signal
637 * Gets emitted when the close button is pressed.
639 HildonFindToolbar_signal[CLOSE] =
641 "close", HILDON_TYPE_FIND_TOOLBAR,
642 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
643 (HildonFindToolbarClass, close),
644 NULL, NULL, gtk_marshal_VOID__VOID,
648 * HildonFindToolbar::invalid-input:
649 * @toolbar: the toolbar which received the signal
651 * Gets emitted when the maximum search prefix length is reached and
652 * user tries to type more.
654 HildonFindToolbar_signal[INVALID_INPUT] =
656 "invalid_input", HILDON_TYPE_FIND_TOOLBAR,
657 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
658 (HildonFindToolbarClass, invalid_input),
659 NULL, NULL, gtk_marshal_VOID__VOID,
663 * HildonFindToolbar::history-append:
664 * @toolbar: the toolbar which received the signal
666 * Gets emitted when the current search prefix should be added to history.
668 HildonFindToolbar_signal[HISTORY_APPEND] =
670 "history_append", HILDON_TYPE_FIND_TOOLBAR,
671 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
672 (HildonFindToolbarClass, history_append),
673 g_signal_accumulator_true_handled, NULL,
674 gtk_marshal_BOOLEAN__VOID,
679 hildon_find_toolbar_init (HildonFindToolbar *self)
681 GtkToolItem *label_container;
682 GtkToolItem *entry_combo_box_container;
684 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
687 /* Create the label */
688 priv->label = gtk_label_new (_("ecdg_ti_find_toolbar_label"));
690 gtk_misc_set_padding (GTK_MISC (priv->label), FIND_LABEL_XPADDING,
691 FIND_LABEL_YPADDING);
693 label_container = gtk_tool_item_new ();
694 gtk_container_add (GTK_CONTAINER (label_container),
697 gtk_widget_show_all (GTK_WIDGET (label_container));
698 gtk_toolbar_insert (GTK_TOOLBAR (self), label_container, -1);
700 /* ComboBoxEntry for search prefix string / history list */
701 priv->entry_combo_box = GTK_COMBO_BOX_ENTRY (gtk_combo_box_entry_new ());
702 g_signal_connect (hildon_find_toolbar_get_entry(priv),
704 G_CALLBACK(hildon_find_toolbar_emit_invalid_input), self);
706 entry_combo_box_container = gtk_tool_item_new ();
708 gtk_tool_item_set_expand (entry_combo_box_container, TRUE);
709 gtk_container_add (GTK_CONTAINER (entry_combo_box_container),
710 GTK_WIDGET (priv->entry_combo_box));
711 gtk_widget_show_all(GTK_WIDGET (entry_combo_box_container));
712 gtk_toolbar_insert (GTK_TOOLBAR (self), entry_combo_box_container, -1);
713 g_signal_connect (hildon_find_toolbar_get_entry (priv),
715 G_CALLBACK(hildon_find_toolbar_entry_activate), self);
718 priv->find_button = gtk_tool_button_new (
719 gtk_image_new_from_icon_name ("qgn_toolb_browser_gobutton",
720 HILDON_ICON_SIZE_TOOLBAR),
723 g_signal_connect (priv->find_button, "clicked",
724 G_CALLBACK(hildon_find_toolbar_emit_search), self);
725 gtk_widget_show_all( GTK_WIDGET(priv->find_button));
726 gtk_toolbar_insert ( GTK_TOOLBAR(self), priv->find_button, -1);
727 if ( GTK_WIDGET_CAN_FOCUS( GTK_BIN(priv->find_button)->child) )
728 GTK_WIDGET_UNSET_FLAGS(
729 GTK_BIN(priv->find_button)->child, GTK_CAN_FOCUS);
732 priv->separator = gtk_separator_tool_item_new();
733 gtk_widget_show(GTK_WIDGET(priv->separator));
734 gtk_toolbar_insert (GTK_TOOLBAR(self), priv->separator, -1);
737 priv->close_button = gtk_tool_button_new (
738 gtk_image_new_from_icon_name ("qgn_toolb_gene_close",
739 HILDON_ICON_SIZE_TOOLBAR),
741 g_signal_connect(priv->close_button, "clicked",
742 G_CALLBACK(hildon_find_toolbar_emit_close), self);
743 gtk_widget_show_all(GTK_WIDGET(priv->close_button));
744 gtk_toolbar_insert (GTK_TOOLBAR(self), priv->close_button, -1);
745 if ( GTK_WIDGET_CAN_FOCUS( GTK_BIN(priv->close_button)->child) )
746 GTK_WIDGET_UNSET_FLAGS(
747 GTK_BIN(priv->close_button)->child, GTK_CAN_FOCUS);
751 * hildon_find_toolbar_new:
752 * @label: label for the find_toolbar, NULL to set the label to
755 * Creates a new HildonFindToolbar.
757 * Returns: a new HildonFindToolbar
760 hildon_find_toolbar_new (const gchar *label)
762 GtkWidget *findtoolbar;
764 findtoolbar = GTK_WIDGET (g_object_new (HILDON_TYPE_FIND_TOOLBAR, NULL));
767 g_object_set(findtoolbar, "label", label, NULL);
773 * hildon_find_toolbar_new_with_model:
774 * @label: label for the find_toolbar, NULL to set the label to
776 * @model: a @GtkListStore
777 * @column: indicating which column the search histry list will
778 * retreive string from
780 * Creates a new HildonFindToolbar with a model.
782 * Returns: a new #HildonFindToolbar
785 hildon_find_toolbar_new_with_model (const gchar *label,
789 GtkWidget *findtoolbar;
791 findtoolbar = hildon_find_toolbar_new (label);
793 g_object_set (findtoolbar, "list", model, "column", column, NULL);
799 * hildon_find_toolbar_highlight_entry:
800 * @ftb: find Toolbar whose entry is to be highlighted
801 * @get_focus: if user passes TRUE to this value, then the text in
802 * the entry will not only get highlighted, but also get focused.
804 * Highlights the current entry in the find toolbar.
808 hildon_find_toolbar_highlight_entry (HildonFindToolbar *self,
811 GtkEntry *entry = NULL;
812 HildonFindToolbarPrivate *priv;
814 g_return_if_fail (HILDON_IS_FIND_TOOLBAR (self));
815 priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
818 entry = hildon_find_toolbar_get_entry (priv);
820 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
823 gtk_widget_grab_focus (GTK_WIDGET (entry));