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
11 * the License or any later version.
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.
40 #include "hildon-find-toolbar.h"
41 #include "hildon-defines.h"
42 #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>
57 #define _(String) dgettext(PACKAGE, String)
59 /*same define as gtkentry.c as entry will further handle this*/
60 #define MAX_SIZE G_MAXUSHORT
61 #define FIND_LABEL_XPADDING 6
62 #define FIND_LABEL_YPADDING 0
84 struct _HildonFindToolbarPrivate
87 GtkComboBoxEntry* entry_combo_box;
88 GtkToolItem* find_button;
89 GtkToolItem* separator;
90 GtkToolItem* close_button;
94 static guint HildonFindToolbar_signal[LAST_SIGNAL] = {0};
96 G_DEFINE_TYPE(HildonFindToolbar, hildon_find_toolbar, GTK_TYPE_TOOLBAR)
99 hildon_find_toolbar_get_list_model(HildonFindToolbarPrivate *priv)
101 GtkTreeModel *filter_model =
102 gtk_combo_box_get_model(GTK_COMBO_BOX(priv->entry_combo_box));
104 return filter_model == NULL ? NULL :
105 gtk_tree_model_filter_get_model(GTK_TREE_MODEL_FILTER(filter_model));
109 hildon_find_toolbar_get_entry(HildonFindToolbarPrivate *priv)
111 return GTK_ENTRY(gtk_bin_get_child(GTK_BIN(priv->entry_combo_box)));
115 hildon_find_toolbar_filter(GtkTreeModel *model,
125 total = gtk_tree_model_iter_n_children(model, NULL);
126 g_object_get(self, "history_limit", &limit, NULL);
127 path = gtk_tree_model_get_path(model, iter);
128 indices = gtk_tree_path_get_indices (path);
130 /* set the row's index, list store has only one level */
132 gtk_tree_path_free(path);
134 /*if the row is among the latest "history_limit" additions of the
135 * model, then we show it */
136 if( (total - limit <= n) && (n < total) )
143 hildon_find_toolbar_apply_filter(HildonFindToolbar *self, GtkTreeModel *model)
145 GtkTreeModel *filter;
146 HildonFindToolbarPrivate *priv = self->priv;
148 /* Create a filter for the given model. Its only purpose is to hide
149 the oldest entries so only "history_limit" entries are visible. */
150 filter = gtk_tree_model_filter_new(model, NULL);
152 gtk_tree_model_filter_set_visible_func(GTK_TREE_MODEL_FILTER(filter),
153 hildon_find_toolbar_filter,
155 gtk_combo_box_set_model(GTK_COMBO_BOX(priv->entry_combo_box), filter);
157 /* ComboBox keeps the only needed reference to the filter */
158 g_object_unref(filter);
162 hildon_find_toolbar_get_property(GObject *object,
167 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR(object)->priv;
174 string = gtk_label_get_text(GTK_LABEL(priv->label));
175 g_value_set_string(value, string);
178 string = gtk_entry_get_text(hildon_find_toolbar_get_entry(priv));
179 g_value_set_string(value, string);
182 g_value_set_object(value, hildon_find_toolbar_get_list_model(priv));
185 c_n = gtk_combo_box_entry_get_text_column(priv->entry_combo_box);
186 g_value_set_int(value, c_n);
189 max_len = gtk_entry_get_max_length(hildon_find_toolbar_get_entry(priv));
190 g_value_set_int(value, max_len);
192 case PROP_HISTORY_LIMIT:
193 g_value_set_int(value, priv->history_limit);
196 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
202 hildon_find_toolbar_set_property(GObject *object,
207 HildonFindToolbar *self = HILDON_FIND_TOOLBAR(object);
208 HildonFindToolbarPrivate *priv = self->priv;
215 string = g_value_get_string(value);
216 gtk_label_set_text(GTK_LABEL(priv->label), string);
219 string = g_value_get_string(value);
220 gtk_entry_set_text(hildon_find_toolbar_get_entry(priv), string);
223 model = GTK_TREE_MODEL(g_value_get_object(value));
224 hildon_find_toolbar_apply_filter(self, model);
227 gtk_combo_box_entry_set_text_column(priv->entry_combo_box,
228 g_value_get_int(value));
231 gtk_entry_set_max_length(hildon_find_toolbar_get_entry(priv),
232 g_value_get_int(value));
234 case PROP_HISTORY_LIMIT:
235 priv->history_limit = g_value_get_int(value);
237 /* Re-apply the history limit to the model. */
238 model = hildon_find_toolbar_get_list_model(priv);
241 /* Note that refilter function doesn't update the status of the
242 combobox popup arrow, so we'll just recreate the filter. */
243 hildon_find_toolbar_apply_filter(self, model);
245 if (gtk_combo_box_entry_get_text_column(priv->entry_combo_box) == -1)
247 /* FIXME: This is only for backwards compatibility, although
248 probably nothing actually relies on it. The behavior was only
249 an accidental side effect of original code */
250 gtk_combo_box_entry_set_text_column(priv->entry_combo_box, 0);
255 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
261 hildon_find_toolbar_find_string(HildonFindToolbar *self,
266 GtkTreeModel *model = NULL;
269 model = hildon_find_toolbar_get_list_model(self->priv);
271 if (gtk_tree_model_get_iter_first(model, iter))
274 gtk_tree_model_get(model, iter, column, &old_string, -1);
275 if (old_string != NULL && strcmp(string, old_string) == 0)
280 } while (gtk_tree_model_iter_next(model, iter));
287 hildon_find_toolbar_history_append(HildonFindToolbar *self,
290 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR(self)->priv;
293 GtkTreeModel *model = NULL;
294 GtkListStore *list = NULL;
296 gboolean self_create = FALSE;
298 g_object_get(self, "prefix", &string, NULL);
302 /* empty prefix, ignore */
308 /* If list store is set, get it */
309 model = hildon_find_toolbar_get_list_model(priv);
312 list = GTK_LIST_STORE(model);
313 g_object_get(self, "column", &column, NULL);
317 /* Column number is -1 if "column" property hasn't been set but
318 "list" property is. */
323 /* Latest string is always the first one in list. If the string
324 already exists, remove it so there are no duplicates in list. */
325 if (hildon_find_toolbar_find_string(self, &iter, column, string))
326 gtk_list_store_remove(list, &iter);
330 /* No list store set. Create our own. */
331 list = gtk_list_store_new(1, G_TYPE_STRING);
332 model = GTK_TREE_MODEL(list);
336 /* Add the string to first in list */
337 gtk_list_store_append(list, &iter);
338 gtk_list_store_set(list, &iter, column, string, -1);
342 /* Add the created list to ComboBoxEntry */
343 hildon_find_toolbar_apply_filter(self, model);
344 /* ComboBoxEntry keeps the only needed reference to this list */
345 g_object_unref(list);
347 /* Set the column only after ComboBoxEntry's model is set
348 in hildon_find_toolbar_apply_filter() */
349 g_object_set(self, "column", 0, NULL);
353 /* Refilter to get the oldest entry hidden from history */
354 gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(
355 gtk_combo_box_get_model(GTK_COMBO_BOX(priv->entry_combo_box))));
364 hildon_find_toolbar_emit_search(GtkButton *button, gpointer self)
368 /* Clicked search button. Perform search and add search prefix to history */
369 g_signal_emit_by_name(self, "search", NULL);
370 g_signal_emit_by_name(self, "history_append", &rb, NULL);
374 hildon_find_toolbar_emit_close(GtkButton *button, gpointer self)
376 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (self));
377 gtk_widget_grab_focus (parent);
378 /* Clicked close button */
379 g_signal_emit_by_name(self, "close", NULL);
383 hildon_find_toolbar_emit_invalid_input(GtkEntry *entry,
384 GtkInvalidInputType type,
387 if(type == GTK_INVALID_INPUT_MAX_CHARS_REACHED)
388 g_signal_emit_by_name(self, "invalid_input", NULL);
392 hildon_find_toolbar_entry_activate (GtkWidget *widget,
395 GtkWidget *find_toolbar = GTK_WIDGET(user_data);
398 /* NB#40936 stop focus from moving to next widget */
399 g_signal_stop_emission_by_name (widget, "activate");
401 g_signal_emit_by_name(find_toolbar, "search", NULL);
402 g_signal_emit_by_name(find_toolbar, "history_append", &rb, NULL);
406 hildon_find_toolbar_class_init(HildonFindToolbarClass *klass)
408 GObjectClass *object_class;
410 g_type_class_add_private(klass, sizeof(HildonFindToolbarPrivate));
412 object_class = G_OBJECT_CLASS(klass);
414 object_class->get_property = hildon_find_toolbar_get_property;
415 object_class->set_property = hildon_find_toolbar_set_property;
417 klass->history_append = hildon_find_toolbar_history_append;
419 g_object_class_install_property(object_class, PROP_LABEL,
420 g_param_spec_string("label",
421 "Label", "Displayed name for"
423 _("ecdg_ti_find_toolbar_label"),
427 g_object_class_install_property(object_class, PROP_PREFIX,
428 g_param_spec_string("prefix",
429 "Prefix", "Search string", NULL,
432 g_object_class_install_property(object_class, PROP_LIST,
433 g_param_spec_object("list",
434 "List"," GtkListStore model where "
435 "history list is kept",
439 g_object_class_install_property(object_class, PROP_COLUMN,
440 g_param_spec_int("column",
441 "Column", "Column number in GtkListStore "
442 "where history list strings are kept",
444 0, G_PARAM_READWRITE));
446 g_object_class_install_property(object_class, PROP_MAX,
447 g_param_spec_int("max_characters",
448 "Maximum number of characters",
449 "Maximum number of characters "
452 0, G_PARAM_READWRITE |
455 g_object_class_install_property(object_class, PROP_HISTORY_LIMIT,
456 g_param_spec_int("history_limit",
457 "Maximum number of history items",
458 "Maximum number of history items "
459 "in search combobox",
461 5, G_PARAM_READWRITE |
465 * HildonFindToolbar::search:
466 * @toolbar: the toolbar which received the signal
468 * Gets emitted when the find button is pressed.
470 HildonFindToolbar_signal[SEARCH] =
472 "search", HILDON_TYPE_FIND_TOOLBAR,
473 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
474 (HildonFindToolbarClass, search),
475 NULL, NULL, gtk_marshal_VOID__VOID,
479 * HildonFindToolbar::close:
480 * @toolbar: the toolbar which received the signal
482 * Gets emitted when the close button is pressed.
484 HildonFindToolbar_signal[CLOSE] =
486 "close", HILDON_TYPE_FIND_TOOLBAR,
487 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
488 (HildonFindToolbarClass, close),
489 NULL, NULL, gtk_marshal_VOID__VOID,
493 * HildonFindToolbar::invalid-input:
494 * @toolbar: the toolbar which received the signal
496 * Gets emitted when the maximum search prefix length is reached and
497 * user tries to type more.
499 HildonFindToolbar_signal[INVALID_INPUT] =
501 "invalid_input", HILDON_TYPE_FIND_TOOLBAR,
502 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
503 (HildonFindToolbarClass, invalid_input),
504 NULL, NULL, gtk_marshal_VOID__VOID,
508 * HildonFindToolbar::history-append:
509 * @toolbar: the toolbar which received the signal
511 * Gets emitted when the current search prefix should be added to history.
513 HildonFindToolbar_signal[HISTORY_APPEND] =
515 "history_append", HILDON_TYPE_FIND_TOOLBAR,
516 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
517 (HildonFindToolbarClass, history_append),
518 g_signal_accumulator_true_handled, NULL,
519 gtk_marshal_BOOLEAN__VOID,
524 hildon_find_toolbar_init(HildonFindToolbar *self)
526 GtkToolItem *label_container;
527 GtkToolItem *entry_combo_box_container;
529 self->priv = G_TYPE_INSTANCE_GET_PRIVATE(self,
530 HILDON_TYPE_FIND_TOOLBAR,
531 HildonFindToolbarPrivate);
533 /* Create the label */
534 self->priv->label = gtk_label_new(_("ecdg_ti_find_toolbar_label"));
536 gtk_misc_set_padding (GTK_MISC(self->priv->label), FIND_LABEL_XPADDING,
537 FIND_LABEL_YPADDING);
539 label_container = gtk_tool_item_new();
540 gtk_container_add(GTK_CONTAINER(label_container),
542 gtk_widget_show_all(GTK_WIDGET(label_container));
543 gtk_toolbar_insert (GTK_TOOLBAR(self), label_container, -1);
545 /* ComboBoxEntry for search prefix string / history list */
546 self->priv->entry_combo_box = GTK_COMBO_BOX_ENTRY(gtk_combo_box_entry_new());
547 g_signal_connect(hildon_find_toolbar_get_entry(self->priv),
549 G_CALLBACK(hildon_find_toolbar_emit_invalid_input), self);
550 entry_combo_box_container = gtk_tool_item_new();
551 gtk_tool_item_set_expand(entry_combo_box_container, TRUE);
552 gtk_container_add(GTK_CONTAINER(entry_combo_box_container),
553 GTK_WIDGET(self->priv->entry_combo_box));
554 gtk_widget_show_all(GTK_WIDGET(entry_combo_box_container));
555 gtk_toolbar_insert (GTK_TOOLBAR(self), entry_combo_box_container, -1);
556 g_signal_connect(hildon_find_toolbar_get_entry(self->priv),
558 G_CALLBACK(hildon_find_toolbar_entry_activate), self);
561 self->priv->find_button = gtk_tool_button_new (
562 gtk_image_new_from_icon_name ("qgn_toolb_browser_gobutton",
563 HILDON_ICON_SIZE_TOOLBAR),
565 g_signal_connect(self->priv->find_button, "clicked",
566 G_CALLBACK(hildon_find_toolbar_emit_search), self);
567 gtk_widget_show_all(GTK_WIDGET(self->priv->find_button));
568 gtk_toolbar_insert (GTK_TOOLBAR(self), self->priv->find_button, -1);
569 if ( GTK_WIDGET_CAN_FOCUS( GTK_BIN(self->priv->find_button)->child) )
570 GTK_WIDGET_UNSET_FLAGS(
571 GTK_BIN(self->priv->find_button)->child, GTK_CAN_FOCUS);
574 self->priv->separator = gtk_separator_tool_item_new();
575 gtk_widget_show(GTK_WIDGET(self->priv->separator));
576 gtk_toolbar_insert (GTK_TOOLBAR(self), self->priv->separator, -1);
579 self->priv->close_button = gtk_tool_button_new (
580 gtk_image_new_from_icon_name ("qgn_toolb_gene_close",
581 HILDON_ICON_SIZE_TOOLBAR),
583 g_signal_connect(self->priv->close_button, "clicked",
584 G_CALLBACK(hildon_find_toolbar_emit_close), self);
585 gtk_widget_show_all(GTK_WIDGET(self->priv->close_button));
586 gtk_toolbar_insert (GTK_TOOLBAR(self), self->priv->close_button, -1);
587 if ( GTK_WIDGET_CAN_FOCUS( GTK_BIN(self->priv->close_button)->child) )
588 GTK_WIDGET_UNSET_FLAGS(
589 GTK_BIN(self->priv->close_button)->child, GTK_CAN_FOCUS);
595 * hildon_find_toolbar_new:
596 * @label: label for the find_toolbar, NULL to set the label to
599 * Returns a new HildonFindToolbar.
601 * Returns: a new HildonFindToolbar
605 hildon_find_toolbar_new(const gchar *label)
607 GtkWidget *findtoolbar;
609 findtoolbar = GTK_WIDGET(g_object_new(HILDON_TYPE_FIND_TOOLBAR, NULL));
611 g_object_set(findtoolbar, "label", label, NULL);
617 * hildon_find_toolbar_new_with_model
618 * @label: label for the find_toolbar, NULL to set the label to
620 * @model: a @GtkListStore
621 * @column: indicating which column the search histry list will
622 * retreive string from
624 * Returns a new HildonFindToolbar, with a model.
626 * Returns: a new #HildonFindToolbar
629 hildon_find_toolbar_new_with_model(const gchar *label,
633 GtkWidget *findtoolbar;
635 findtoolbar = hildon_find_toolbar_new(label);
636 g_object_set(findtoolbar, "list", model,
637 "column", column, NULL);
643 * hildon_find_toolbar_highlight_entry
644 * @ftb: find Toolbar whose entry is to be highlighted
645 * @get_focus: if user passes TRUE to this value, then the text in
646 * the entry will not only get highlighted, but also get focused.
650 hildon_find_toolbar_highlight_entry(HildonFindToolbar *ftb,
653 GtkEntry *entry = NULL;
655 g_return_if_fail(HILDON_IS_FIND_TOOLBAR(ftb));
657 entry = hildon_find_toolbar_get_entry(ftb->priv);
659 gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
662 gtk_widget_grab_focus(GTK_WIDGET(entry));