2 * This file is part of hildon-libs
4 * Copyright (C) 2005, 2006 Nokia Corporation.
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.
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 /* Clicked close button */
377 g_signal_emit_by_name(self, "close", NULL);
381 hildon_find_toolbar_emit_invalid_input(GtkEntry *entry,
382 GtkInvalidInputType type,
385 if(type == GTK_INVALID_INPUT_MAX_CHARS_REACHED)
386 g_signal_emit_by_name(self, "invalid_input", NULL);
390 hildon_find_toolbar_entry_key_press (GtkWidget *widget,
394 GtkWidget *find_toolbar = GTK_WIDGET(user_data);
396 /* on enter we emit search and history_append signals and keep
397 * focus by returning true */
398 if (event->keyval == GDK_KP_Enter)
401 g_signal_emit_by_name(find_toolbar, "search", NULL);
402 g_signal_emit_by_name(find_toolbar, "history_append", &rb, NULL);
411 hildon_find_toolbar_class_init(HildonFindToolbarClass *klass)
413 GObjectClass *object_class;
415 g_type_class_add_private(klass, sizeof(HildonFindToolbarPrivate));
417 object_class = G_OBJECT_CLASS(klass);
419 object_class->get_property = hildon_find_toolbar_get_property;
420 object_class->set_property = hildon_find_toolbar_set_property;
422 klass->history_append = hildon_find_toolbar_history_append;
424 g_object_class_install_property(object_class, PROP_LABEL,
425 g_param_spec_string("label",
426 "Label", "Displayed name for"
428 _("ecdg_ti_find_toolbar_label"),
432 g_object_class_install_property(object_class, PROP_PREFIX,
433 g_param_spec_string("prefix",
434 "Prefix", "Search string", NULL,
437 g_object_class_install_property(object_class, PROP_LIST,
438 g_param_spec_object("list",
439 "List"," GtkListStore model where "
440 "history list is kept",
444 g_object_class_install_property(object_class, PROP_COLUMN,
445 g_param_spec_int("column",
446 "Column", "Column number in GtkListStore "
447 "where history list strings are kept",
449 0, G_PARAM_READWRITE));
451 g_object_class_install_property(object_class, PROP_MAX,
452 g_param_spec_int("max_characters",
453 "Maximum number of characters",
454 "Maximum number of characters "
457 0, G_PARAM_READWRITE |
460 g_object_class_install_property(object_class, PROP_HISTORY_LIMIT,
461 g_param_spec_int("history_limit",
462 "Maximum number of history items",
463 "Maximum number of history items "
464 "in search combobox",
466 5, G_PARAM_READWRITE |
470 * HildonFindToolbar::search:
471 * @toolbar: the toolbar which received the signal
473 * Gets emitted when the find button is pressed.
475 HildonFindToolbar_signal[SEARCH] =
477 "search", HILDON_TYPE_FIND_TOOLBAR,
478 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
479 (HildonFindToolbarClass, search),
480 NULL, NULL, gtk_marshal_VOID__VOID,
484 * HildonFindToolbar::close:
485 * @toolbar: the toolbar which received the signal
487 * Gets emitted when the close button is pressed.
489 HildonFindToolbar_signal[CLOSE] =
491 "close", HILDON_TYPE_FIND_TOOLBAR,
492 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
493 (HildonFindToolbarClass, close),
494 NULL, NULL, gtk_marshal_VOID__VOID,
498 * HildonFindToolbar::invalid-input:
499 * @toolbar: the toolbar which received the signal
501 * Gets emitted when the maximum search prefix length is reached and
502 * user tries to type more.
504 HildonFindToolbar_signal[INVALID_INPUT] =
506 "invalid_input", HILDON_TYPE_FIND_TOOLBAR,
507 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
508 (HildonFindToolbarClass, invalid_input),
509 NULL, NULL, gtk_marshal_VOID__VOID,
513 * HildonFindToolbar::history-append:
514 * @toolbar: the toolbar which received the signal
516 * Gets emitted when the current search prefix should be added to history.
518 HildonFindToolbar_signal[HISTORY_APPEND] =
520 "history_append", HILDON_TYPE_FIND_TOOLBAR,
521 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
522 (HildonFindToolbarClass, history_append),
523 g_signal_accumulator_true_handled, NULL,
524 gtk_marshal_BOOLEAN__VOID,
529 hildon_find_toolbar_init(HildonFindToolbar *self)
531 GtkToolItem *label_container;
532 GtkToolItem *entry_combo_box_container;
534 self->priv = G_TYPE_INSTANCE_GET_PRIVATE(self,
535 HILDON_TYPE_FIND_TOOLBAR,
536 HildonFindToolbarPrivate);
538 /* Create the label */
539 self->priv->label = gtk_label_new(_("ecdg_ti_find_toolbar_label"));
541 gtk_misc_set_padding (GTK_MISC(self->priv->label), FIND_LABEL_XPADDING,
542 FIND_LABEL_YPADDING);
544 label_container = gtk_tool_item_new();
545 gtk_container_add(GTK_CONTAINER(label_container),
547 gtk_widget_show_all(GTK_WIDGET(label_container));
548 gtk_toolbar_insert (GTK_TOOLBAR(self), label_container, -1);
550 /* ComboBoxEntry for search prefix string / history list */
551 self->priv->entry_combo_box = GTK_COMBO_BOX_ENTRY(gtk_combo_box_entry_new());
552 g_signal_connect(hildon_find_toolbar_get_entry(self->priv),
554 G_CALLBACK(hildon_find_toolbar_emit_invalid_input), self);
555 entry_combo_box_container = gtk_tool_item_new();
556 gtk_tool_item_set_expand(entry_combo_box_container, TRUE);
557 gtk_container_add(GTK_CONTAINER(entry_combo_box_container),
558 GTK_WIDGET(self->priv->entry_combo_box));
559 gtk_widget_show_all(GTK_WIDGET(entry_combo_box_container));
560 gtk_toolbar_insert (GTK_TOOLBAR(self), entry_combo_box_container, -1);
561 g_signal_connect(hildon_find_toolbar_get_entry(self->priv),
563 G_CALLBACK(hildon_find_toolbar_entry_key_press), self);
566 self->priv->find_button = gtk_tool_button_new (
567 gtk_image_new_from_icon_name ("qgn_toolb_browser_gobutton",
568 HILDON_ICON_SIZE_TOOLBAR),
570 g_signal_connect(self->priv->find_button, "clicked",
571 G_CALLBACK(hildon_find_toolbar_emit_search), self);
572 gtk_widget_show_all(GTK_WIDGET(self->priv->find_button));
573 gtk_toolbar_insert (GTK_TOOLBAR(self), self->priv->find_button, -1);
574 if ( GTK_WIDGET_CAN_FOCUS( GTK_BIN(self->priv->find_button)->child) )
575 GTK_WIDGET_UNSET_FLAGS(
576 GTK_BIN(self->priv->find_button)->child, GTK_CAN_FOCUS);
579 self->priv->separator = gtk_separator_tool_item_new();
580 gtk_widget_show(GTK_WIDGET(self->priv->separator));
581 gtk_toolbar_insert (GTK_TOOLBAR(self), self->priv->separator, -1);
584 self->priv->close_button = gtk_tool_button_new (
585 gtk_image_new_from_icon_name ("qgn_toolb_gene_close",
586 HILDON_ICON_SIZE_TOOLBAR),
588 g_signal_connect(self->priv->close_button, "clicked",
589 G_CALLBACK(hildon_find_toolbar_emit_close), self);
590 gtk_widget_show_all(GTK_WIDGET(self->priv->close_button));
591 gtk_toolbar_insert (GTK_TOOLBAR(self), self->priv->close_button, -1);
592 if ( GTK_WIDGET_CAN_FOCUS( GTK_BIN(self->priv->close_button)->child) )
593 GTK_WIDGET_UNSET_FLAGS(
594 GTK_BIN(self->priv->close_button)->child, GTK_CAN_FOCUS);
600 * hildon_find_toolbar_new:
601 * @label: label for the find_toolbar, NULL to set the label to
604 * Returns a new HildonFindToolbar.
606 * Returns: a new HildonFindToolbar
610 hildon_find_toolbar_new(const gchar *label)
612 GtkWidget *findtoolbar;
614 findtoolbar = GTK_WIDGET(g_object_new(HILDON_TYPE_FIND_TOOLBAR, NULL));
616 g_object_set(findtoolbar, "label", label, NULL);
622 * hildon_find_toolbar_new_with_model
623 * @label: label for the find_toolbar, NULL to set the label to
625 * @model: a @GtkListStore
626 * @column: indicating which column the search histry list will
627 * retreive string from
629 * Returns a new HildonFindToolbar, with a model.
631 * Returns: a new #HildonFindToolbar
634 hildon_find_toolbar_new_with_model(const gchar *label,
638 GtkWidget *findtoolbar;
640 findtoolbar = hildon_find_toolbar_new(label);
641 g_object_set(findtoolbar, "list", model,
642 "column", column, NULL);
648 * hildon_find_toolbar_highlight_entry
649 * @ftb: find Toolbar whose entry is to be highlighted
650 * @get_focus: if user passes TRUE to this value, then the text in
651 * the entry will not only get highlighted, but also get focused.
655 hildon_find_toolbar_highlight_entry(HildonFindToolbar *ftb,
658 GtkEntry *entry = NULL;
660 g_return_if_fail(HILDON_IS_FIND_TOOLBAR(ftb));
662 entry = hildon_find_toolbar_get_entry(ftb->priv);
664 gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
667 gtk_widget_grab_focus(GTK_WIDGET(entry));