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 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.
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(PACKAGE, 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 (void)
155 static GType find_toolbar_type = 0;
157 if (! find_toolbar_type) {
158 static const GTypeInfo find_toolbar_info = {
159 sizeof(HildonFindToolbarClass),
160 NULL, /* base_init */
161 NULL, /* base_finalize */
162 (GClassInitFunc) hildon_find_toolbar_class_init,
163 NULL, /* class_finalize */
164 NULL, /* class_data */
165 sizeof(HildonFindToolbar),
167 (GInstanceInitFunc) hildon_find_toolbar_init,
169 find_toolbar_type = g_type_register_static (GTK_TYPE_TOOLBAR,
171 &find_toolbar_info, 0);
174 return find_toolbar_type;
178 hildon_find_toolbar_get_list_model (HildonFindToolbarPrivate *priv)
180 GtkTreeModel *filter_model =
181 gtk_combo_box_get_model (GTK_COMBO_BOX (priv->entry_combo_box));
183 return filter_model == NULL ? NULL :
184 gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (filter_model));
188 hildon_find_toolbar_get_entry (HildonFindToolbarPrivate *priv)
190 return GTK_ENTRY (gtk_bin_get_child (GTK_BIN (priv->entry_combo_box)));
194 hildon_find_toolbar_filter (GtkTreeModel *model,
204 total = gtk_tree_model_iter_n_children (model, NULL);
205 g_object_get (self, "history_limit", &limit, NULL);
206 path = gtk_tree_model_get_path (model, iter);
207 indices = gtk_tree_path_get_indices (path);
209 /* set the row's index, list store has only one level */
211 gtk_tree_path_free (path);
213 /*if the row is among the latest "history_limit" additions of the
214 * model, then we show it */
215 if( (total - limit <= n) && (n < total) )
222 hildon_find_toolbar_apply_filter (HildonFindToolbar *self,
225 GtkTreeModel *filter;
226 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
229 /* Create a filter for the given model. Its only purpose is to hide
230 the oldest entries so only "history_limit" entries are visible. */
231 filter = gtk_tree_model_filter_new (model, NULL);
233 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER(filter),
234 hildon_find_toolbar_filter,
237 gtk_combo_box_set_model (GTK_COMBO_BOX (priv->entry_combo_box), filter);
239 /* ComboBox keeps the only needed reference to the filter */
240 g_object_unref (filter);
244 hildon_find_toolbar_get_property (GObject *object,
249 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (object);
258 string = gtk_label_get_text (GTK_LABEL (priv->label));
259 g_value_set_string (value, string);
263 string = gtk_entry_get_text (hildon_find_toolbar_get_entry(priv));
264 g_value_set_string (value, string);
268 g_value_set_object (value, hildon_find_toolbar_get_list_model(priv));
272 c_n = gtk_combo_box_entry_get_text_column (priv->entry_combo_box);
273 g_value_set_int (value, c_n);
277 max_len = gtk_entry_get_max_length (hildon_find_toolbar_get_entry(priv));
278 g_value_set_int (value, max_len);
281 case PROP_HISTORY_LIMIT:
282 g_value_set_int (value, priv->history_limit);
286 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
292 hildon_find_toolbar_set_property (GObject *object,
297 HildonFindToolbar *self = HILDON_FIND_TOOLBAR(object);
298 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (object);
307 string = g_value_get_string (value);
308 gtk_label_set_text (GTK_LABEL (priv->label), string);
312 string = g_value_get_string (value);
313 gtk_entry_set_text (hildon_find_toolbar_get_entry(priv), string);
317 model = GTK_TREE_MODEL (g_value_get_object(value));
318 hildon_find_toolbar_apply_filter (self, model);
322 gtk_combo_box_entry_set_text_column (priv->entry_combo_box,
323 g_value_get_int (value));
327 gtk_entry_set_max_length (hildon_find_toolbar_get_entry(priv),
328 g_value_get_int (value));
331 case PROP_HISTORY_LIMIT:
332 priv->history_limit = g_value_get_int (value);
334 /* Re-apply the history limit to the model. */
335 model = hildon_find_toolbar_get_list_model (priv);
338 /* Note that refilter function doesn't update the status of the
339 combobox popup arrow, so we'll just recreate the filter. */
340 hildon_find_toolbar_apply_filter (self, model);
342 if (gtk_combo_box_entry_get_text_column (priv->entry_combo_box) == -1)
344 /* FIXME: This is only for backwards compatibility, although
345 probably nothing actually relies on it. The behavior was only
346 an accidental side effect of original code */
347 gtk_combo_box_entry_set_text_column (priv->entry_combo_box, 0);
353 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
359 hildon_find_toolbar_find_string (HildonFindToolbar *self,
364 GtkTreeModel *model = NULL;
366 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
369 model = hildon_find_toolbar_get_list_model (priv);
371 if (gtk_tree_model_get_iter_first (model, iter))
374 gtk_tree_model_get (model, iter, column, &old_string, -1);
375 if (old_string != NULL && strcmp (string, old_string) == 0)
380 } while (gtk_tree_model_iter_next (model, iter));
387 hildon_find_toolbar_history_append (HildonFindToolbar *self,
390 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
395 GtkTreeModel *model = NULL;
396 GtkListStore *list = NULL;
398 gboolean self_create = FALSE;
400 g_object_get (self, "prefix", &string, NULL);
404 /* empty prefix, ignore */
410 /* If list store is set, get it */
411 model = hildon_find_toolbar_get_list_model(priv);
414 list = GTK_LIST_STORE (model);
415 g_object_get(self, "column", &column, NULL);
419 /* Column number is -1 if "column" property hasn't been set but
420 "list" property is. */
425 /* Latest string is always the first one in list. If the string
426 already exists, remove it so there are no duplicates in list. */
427 if (hildon_find_toolbar_find_string (self, &iter, column, string))
428 gtk_list_store_remove (list, &iter);
432 /* No list store set. Create our own. */
433 list = gtk_list_store_new (1, G_TYPE_STRING);
434 model = GTK_TREE_MODEL (list);
438 /* Add the string to first in list */
439 gtk_list_store_append (list, &iter);
440 gtk_list_store_set (list, &iter, column, string, -1);
444 /* Add the created list to ComboBoxEntry */
445 hildon_find_toolbar_apply_filter (self, model);
446 /* ComboBoxEntry keeps the only needed reference to this list */
447 g_object_unref (list);
449 /* Set the column only after ComboBoxEntry's model is set
450 in hildon_find_toolbar_apply_filter() */
451 g_object_set (self, "column", 0, NULL);
455 /* Refilter to get the oldest entry hidden from history */
456 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER(
457 gtk_combo_box_get_model (GTK_COMBO_BOX(priv->entry_combo_box))));
466 hildon_find_toolbar_emit_search (GtkButton *button,
471 /* Clicked search button. Perform search and add search prefix to history */
472 g_signal_emit_by_name(self, "search", NULL);
473 g_signal_emit_by_name(self, "history_append", &rb, NULL);
477 hildon_find_toolbar_emit_close (GtkButton *button,
480 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
483 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (priv->entry_combo_box));
484 if (GTK_WIDGET_HAS_FOCUS (entry))
486 hildon_gtk_im_context_hide (GTK_ENTRY (entry)->im_context);
489 /* Clicked close button */
490 g_signal_emit_by_name (self, "close", NULL);
494 hildon_find_toolbar_emit_invalid_input (GtkEntry *entry,
495 GtkInvalidInputType type,
498 if(type == GTK_INVALID_INPUT_MAX_CHARS_REACHED)
499 g_signal_emit_by_name (self, "invalid_input", NULL);
503 hildon_find_toolbar_entry_activate (GtkWidget *widget,
506 GtkWidget *find_toolbar = GTK_WIDGET (user_data);
509 /* NB#40936 stop focus from moving to next widget */
510 g_signal_stop_emission_by_name (widget, "activate");
512 g_signal_emit_by_name (find_toolbar, "search", NULL);
513 g_signal_emit_by_name (find_toolbar, "history_append", &rb, NULL);
517 hildon_find_toolbar_class_init (HildonFindToolbarClass *klass)
519 GObjectClass *object_class;
521 g_type_class_add_private (klass, sizeof (HildonFindToolbarPrivate));
523 object_class = G_OBJECT_CLASS(klass);
525 object_class->get_property = hildon_find_toolbar_get_property;
526 object_class->set_property = hildon_find_toolbar_set_property;
528 klass->history_append = (gpointer) hildon_find_toolbar_history_append;
530 g_object_class_install_property(object_class, PROP_LABEL,
531 g_param_spec_string("label",
532 "Label", "Displayed name for"
534 _("ecdg_ti_find_toolbar_label"),
538 g_object_class_install_property(object_class, PROP_PREFIX,
539 g_param_spec_string("prefix",
540 "Prefix", "Search string", NULL,
543 g_object_class_install_property(object_class, PROP_LIST,
544 g_param_spec_object("list",
545 "List"," GtkListStore model where "
546 "history list is kept",
550 g_object_class_install_property(object_class, PROP_COLUMN,
551 g_param_spec_int("column",
552 "Column", "Column number in GtkListStore "
553 "where history list strings are kept",
555 0, G_PARAM_READWRITE));
557 g_object_class_install_property(object_class, PROP_MAX,
558 g_param_spec_int("max_characters",
559 "Maximum number of characters",
560 "Maximum number of characters "
563 0, G_PARAM_READWRITE |
566 g_object_class_install_property(object_class, PROP_HISTORY_LIMIT,
567 g_param_spec_int("history_limit",
568 "Maximum number of history items",
569 "Maximum number of history items "
570 "in search combobox",
572 5, G_PARAM_READWRITE |
576 * HildonFindToolbar::search:
577 * @toolbar: the toolbar which received the signal
579 * Gets emitted when the find button is pressed.
581 HildonFindToolbar_signal[SEARCH] =
583 "search", HILDON_TYPE_FIND_TOOLBAR,
584 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
585 (HildonFindToolbarClass, search),
586 NULL, NULL, gtk_marshal_VOID__VOID,
590 * HildonFindToolbar::close:
591 * @toolbar: the toolbar which received the signal
593 * Gets emitted when the close button is pressed.
595 HildonFindToolbar_signal[CLOSE] =
597 "close", HILDON_TYPE_FIND_TOOLBAR,
598 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
599 (HildonFindToolbarClass, close),
600 NULL, NULL, gtk_marshal_VOID__VOID,
604 * HildonFindToolbar::invalid-input:
605 * @toolbar: the toolbar which received the signal
607 * Gets emitted when the maximum search prefix length is reached and
608 * user tries to type more.
610 HildonFindToolbar_signal[INVALID_INPUT] =
612 "invalid_input", HILDON_TYPE_FIND_TOOLBAR,
613 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
614 (HildonFindToolbarClass, invalid_input),
615 NULL, NULL, gtk_marshal_VOID__VOID,
619 * HildonFindToolbar::history-append:
620 * @toolbar: the toolbar which received the signal
622 * Gets emitted when the current search prefix should be added to history.
624 HildonFindToolbar_signal[HISTORY_APPEND] =
626 "history_append", HILDON_TYPE_FIND_TOOLBAR,
627 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
628 (HildonFindToolbarClass, history_append),
629 g_signal_accumulator_true_handled, NULL,
630 gtk_marshal_BOOLEAN__VOID,
635 hildon_find_toolbar_init (HildonFindToolbar *self)
637 GtkToolItem *label_container;
638 GtkToolItem *entry_combo_box_container;
640 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
643 /* Create the label */
644 priv->label = gtk_label_new (_("ecdg_ti_find_toolbar_label"));
646 gtk_misc_set_padding (GTK_MISC (priv->label), FIND_LABEL_XPADDING,
647 FIND_LABEL_YPADDING);
649 label_container = gtk_tool_item_new ();
650 gtk_container_add (GTK_CONTAINER (label_container),
653 gtk_widget_show_all (GTK_WIDGET (label_container));
654 gtk_toolbar_insert (GTK_TOOLBAR (self), label_container, -1);
656 /* ComboBoxEntry for search prefix string / history list */
657 priv->entry_combo_box = GTK_COMBO_BOX_ENTRY (gtk_combo_box_entry_new ());
658 g_signal_connect (hildon_find_toolbar_get_entry(priv),
660 G_CALLBACK(hildon_find_toolbar_emit_invalid_input), self);
662 entry_combo_box_container = gtk_tool_item_new ();
664 gtk_tool_item_set_expand (entry_combo_box_container, TRUE);
665 gtk_container_add (GTK_CONTAINER (entry_combo_box_container),
666 GTK_WIDGET (priv->entry_combo_box));
667 gtk_widget_show_all(GTK_WIDGET (entry_combo_box_container));
668 gtk_toolbar_insert (GTK_TOOLBAR (self), entry_combo_box_container, -1);
669 g_signal_connect (hildon_find_toolbar_get_entry (priv),
671 G_CALLBACK(hildon_find_toolbar_entry_activate), self);
674 priv->find_button = gtk_tool_button_new (
675 gtk_image_new_from_icon_name ("qgn_toolb_browser_gobutton",
676 HILDON_ICON_SIZE_TOOLBAR),
679 g_signal_connect (priv->find_button, "clicked",
680 G_CALLBACK(hildon_find_toolbar_emit_search), self);
681 gtk_widget_show_all( GTK_WIDGET(priv->find_button));
682 gtk_toolbar_insert ( GTK_TOOLBAR(self), priv->find_button, -1);
683 if ( GTK_WIDGET_CAN_FOCUS( GTK_BIN(priv->find_button)->child) )
684 GTK_WIDGET_UNSET_FLAGS(
685 GTK_BIN(priv->find_button)->child, GTK_CAN_FOCUS);
688 priv->separator = gtk_separator_tool_item_new();
689 gtk_widget_show(GTK_WIDGET(priv->separator));
690 gtk_toolbar_insert (GTK_TOOLBAR(self), priv->separator, -1);
693 priv->close_button = gtk_tool_button_new (
694 gtk_image_new_from_icon_name ("qgn_toolb_gene_close",
695 HILDON_ICON_SIZE_TOOLBAR),
697 g_signal_connect(priv->close_button, "clicked",
698 G_CALLBACK(hildon_find_toolbar_emit_close), self);
699 gtk_widget_show_all(GTK_WIDGET(priv->close_button));
700 gtk_toolbar_insert (GTK_TOOLBAR(self), priv->close_button, -1);
701 if ( GTK_WIDGET_CAN_FOCUS( GTK_BIN(priv->close_button)->child) )
702 GTK_WIDGET_UNSET_FLAGS(
703 GTK_BIN(priv->close_button)->child, GTK_CAN_FOCUS);
707 * hildon_find_toolbar_new:
708 * @label: label for the find_toolbar, NULL to set the label to
711 * Returns a new HildonFindToolbar.
713 * Returns: a new HildonFindToolbar
717 hildon_find_toolbar_new (const gchar *label)
719 GtkWidget *findtoolbar;
721 findtoolbar = GTK_WIDGET (g_object_new (HILDON_TYPE_FIND_TOOLBAR, NULL));
724 g_object_set(findtoolbar, "label", label, NULL);
730 * hildon_find_toolbar_new_with_model
731 * @label: label for the find_toolbar, NULL to set the label to
733 * @model: a @GtkListStore
734 * @column: indicating which column the search histry list will
735 * retreive string from
737 * Returns a new HildonFindToolbar, with a model.
739 * Returns: a new #HildonFindToolbar
742 hildon_find_toolbar_new_with_model (const gchar *label,
746 GtkWidget *findtoolbar;
748 findtoolbar = hildon_find_toolbar_new (label);
750 g_object_set (findtoolbar, "list", model, "column", column, NULL);
756 * hildon_find_toolbar_highlight_entry
757 * @ftb: find Toolbar whose entry is to be highlighted
758 * @get_focus: if user passes TRUE to this value, then the text in
759 * the entry will not only get highlighted, but also get focused.
763 hildon_find_toolbar_highlight_entry (HildonFindToolbar *self,
766 GtkEntry *entry = NULL;
767 HildonFindToolbarPrivate *priv;
769 g_return_if_fail (HILDON_IS_FIND_TOOLBAR (self));
770 priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
773 entry = hildon_find_toolbar_get_entry (priv);
775 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
778 gtk_widget_grab_focus (GTK_WIDGET (entry));