2 * This file is part of hildon-libs
4 * Copyright (C) 2005 Nokia Corporation.
6 * Contact: Luc Pionchon <luc.pionchon@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; either version 2.1 of
11 * the License, or (at your option) 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
25 #include "hildon-find-toolbar.h"
26 #include "hildon-defines.h"
27 #include <gdk/gdkkeysyms.h>
29 #include <gtk/gtklabel.h>
30 #include <gtk/gtkentry.h>
31 #include <gtk/gtkbutton.h>
32 #include <gtk/gtktoolbutton.h>
33 #include <gtk/gtktoolitem.h>
34 #include <gtk/gtkcomboboxentry.h>
35 #include <gtk/gtkseparatortoolitem.h>
42 #define _(String) dgettext(PACKAGE, String)
44 /*same define as gtkentry.c as entry will further handle this*/
45 #define MAX_SIZE G_MAXUSHORT
46 #define FIND_LABEL_XPADDING 6
47 #define FIND_LABEL_YPADDING 0
69 struct _HildonFindToolbarPrivate
72 GtkComboBoxEntry* entry_combo_box;
73 GtkToolItem* find_button;
74 GtkToolItem* separator;
75 GtkToolItem* close_button;
79 static guint HildonFindToolbar_signal[LAST_SIGNAL] = {0};
81 G_DEFINE_TYPE(HildonFindToolbar, hildon_find_toolbar, GTK_TYPE_TOOLBAR)
84 hildon_find_toolbar_get_list_model(HildonFindToolbarPrivate *priv)
86 GtkTreeModel *filter_model =
87 gtk_combo_box_get_model(GTK_COMBO_BOX(priv->entry_combo_box));
89 return filter_model == NULL ? NULL :
90 gtk_tree_model_filter_get_model(GTK_TREE_MODEL_FILTER(filter_model));
94 hildon_find_toolbar_get_entry(HildonFindToolbarPrivate *priv)
96 return GTK_ENTRY(gtk_bin_get_child(GTK_BIN(priv->entry_combo_box)));
100 hildon_find_toolbar_filter(GtkTreeModel *model,
110 total = gtk_tree_model_iter_n_children(model, NULL);
111 g_object_get(self, "history_limit", &limit, NULL);
112 path = gtk_tree_model_get_path(model, iter);
113 indices = gtk_tree_path_get_indices (path);
115 /* set the row's index, list store has only one level */
117 gtk_tree_path_free(path);
119 /*if the row is among the latest "history_limit" additions of the
120 * model, then we show it */
121 if( (total - limit <= n) && (n < total) )
128 hildon_find_toolbar_apply_filter(HildonFindToolbar *self, GtkTreeModel *model)
130 GtkTreeModel *filter;
131 HildonFindToolbarPrivate *priv = self->priv;
133 /* Create a filter for the given model. Its only purpose is to hide
134 the oldest entries so only "history_limit" entries are visible. */
135 filter = gtk_tree_model_filter_new(model, NULL);
137 gtk_tree_model_filter_set_visible_func(GTK_TREE_MODEL_FILTER(filter),
138 hildon_find_toolbar_filter,
140 gtk_combo_box_set_model(GTK_COMBO_BOX(priv->entry_combo_box), filter);
142 /* ComboBox keeps the only needed reference to the filter */
143 g_object_unref(filter);
147 hildon_find_toolbar_get_property(GObject *object,
152 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR(object)->priv;
159 string = gtk_label_get_text(GTK_LABEL(priv->label));
160 g_value_set_string(value, string);
163 string = gtk_entry_get_text(hildon_find_toolbar_get_entry(priv));
164 g_value_set_string(value, string);
167 g_value_set_object(value, hildon_find_toolbar_get_list_model(priv));
170 c_n = gtk_combo_box_entry_get_text_column(priv->entry_combo_box);
171 g_value_set_int(value, c_n);
174 max_len = gtk_entry_get_max_length(hildon_find_toolbar_get_entry(priv));
175 g_value_set_int(value, max_len);
177 case PROP_HISTORY_LIMIT:
178 g_value_set_int(value, priv->history_limit);
181 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
187 hildon_find_toolbar_set_property(GObject *object,
192 HildonFindToolbar *self = HILDON_FIND_TOOLBAR(object);
193 HildonFindToolbarPrivate *priv = self->priv;
200 string = g_value_get_string(value);
201 gtk_label_set_text(GTK_LABEL(priv->label), string);
204 string = g_value_get_string(value);
205 gtk_entry_set_text(hildon_find_toolbar_get_entry(priv), string);
208 model = GTK_TREE_MODEL(g_value_get_object(value));
209 hildon_find_toolbar_apply_filter(self, model);
212 gtk_combo_box_entry_set_text_column(priv->entry_combo_box,
213 g_value_get_int(value));
216 gtk_entry_set_max_length(hildon_find_toolbar_get_entry(priv),
217 g_value_get_int(value));
219 case PROP_HISTORY_LIMIT:
220 priv->history_limit = g_value_get_int(value);
222 /* Re-apply the history limit to the model. */
223 model = hildon_find_toolbar_get_list_model(priv);
226 /* Note that refilter function doesn't update the status of the
227 combobox popup arrow, so we'll just recreate the filter. */
228 hildon_find_toolbar_apply_filter(self, model);
230 if (gtk_combo_box_entry_get_text_column(priv->entry_combo_box) == -1)
232 /* FIXME: This is only for backwards compatibility, although
233 probably nothing actually relies on it. The behavior was only
234 an accidental side effect of original code */
235 gtk_combo_box_entry_set_text_column(priv->entry_combo_box, 0);
240 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
246 hildon_find_toolbar_find_string(HildonFindToolbar *self,
251 GtkTreeModel *model = NULL;
254 model = hildon_find_toolbar_get_list_model(self->priv);
256 if (gtk_tree_model_get_iter_first(model, iter))
259 gtk_tree_model_get(model, iter, column, &old_string, -1);
260 if (old_string != NULL && strcmp(string, old_string) == 0)
265 } while (gtk_tree_model_iter_next(model, iter));
272 hildon_find_toolbar_history_append(HildonFindToolbar *self,
275 HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR(self)->priv;
278 GtkTreeModel *model = NULL;
279 GtkListStore *list = NULL;
281 gboolean self_create = FALSE;
283 g_object_get(self, "prefix", &string, NULL);
287 /* empty prefix, ignore */
293 /* If list store is set, get it */
294 model = hildon_find_toolbar_get_list_model(priv);
297 list = GTK_LIST_STORE(model);
298 g_object_get(self, "column", &column, NULL);
302 /* Column number is -1 if "column" property hasn't been set but
303 "list" property is. */
308 /* Latest string is always the first one in list. If the string
309 already exists, remove it so there are no duplicates in list. */
310 if (hildon_find_toolbar_find_string(self, &iter, column, string))
311 gtk_list_store_remove(list, &iter);
315 /* No list store set. Create our own. */
316 list = gtk_list_store_new(1, G_TYPE_STRING);
317 model = GTK_TREE_MODEL(list);
321 /* Add the string to first in list */
322 gtk_list_store_append(list, &iter);
323 gtk_list_store_set(list, &iter, column, string, -1);
327 /* Add the created list to ComboBoxEntry */
328 hildon_find_toolbar_apply_filter(self, model);
329 /* ComboBoxEntry keeps the only needed reference to this list */
330 g_object_unref(list);
332 /* Set the column only after ComboBoxEntry's model is set
333 in hildon_find_toolbar_apply_filter() */
334 g_object_set(self, "column", 0, NULL);
338 /* Refilter to get the oldest entry hidden from history */
339 gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(
340 gtk_combo_box_get_model(GTK_COMBO_BOX(priv->entry_combo_box))));
349 hildon_find_toolbar_emit_search(GtkButton *button, gpointer self)
353 /* Clicked search button. Perform search and add search prefix to history */
354 g_signal_emit_by_name(self, "search", NULL);
355 g_signal_emit_by_name(self, "history_append", &rb, NULL);
359 hildon_find_toolbar_emit_close(GtkButton *button, gpointer self)
361 /* Clicked close button */
362 g_signal_emit_by_name(self, "close", NULL);
366 hildon_find_toolbar_emit_invalid_input(GtkEntry *entry,
367 GtkInvalidInputType type,
370 if(type == GTK_INVALID_INPUT_MAX_CHARS_REACHED)
371 g_signal_emit_by_name(self, "invalid_input", NULL);
375 hildon_find_toolbar_entry_key_press (GtkWidget *widget,
379 GtkWidget *find_toolbar = GTK_WIDGET(user_data);
381 /* on enter we emit search and history_append signals and keep
382 * focus by returning true */
383 if (event->keyval == GDK_KP_Enter)
386 g_signal_emit_by_name(find_toolbar, "search", NULL);
387 g_signal_emit_by_name(find_toolbar, "history_append", &rb, NULL);
396 hildon_find_toolbar_class_init(HildonFindToolbarClass *klass)
398 GObjectClass *object_class;
400 g_type_class_add_private(klass, sizeof(HildonFindToolbarPrivate));
402 object_class = G_OBJECT_CLASS(klass);
404 object_class->get_property = hildon_find_toolbar_get_property;
405 object_class->set_property = hildon_find_toolbar_set_property;
407 klass->history_append = hildon_find_toolbar_history_append;
409 g_object_class_install_property(object_class, PROP_LABEL,
410 g_param_spec_string("label",
411 "Label", "Displayed name for"
413 _("Ecdg_ti_find_toolbar_label"),
417 g_object_class_install_property(object_class, PROP_PREFIX,
418 g_param_spec_string("prefix",
419 "Prefix", "Search string", NULL,
422 g_object_class_install_property(object_class, PROP_LIST,
423 g_param_spec_object("list",
424 "List"," GtkListStore model where "
425 "history list is kept",
429 g_object_class_install_property(object_class, PROP_COLUMN,
430 g_param_spec_int("column",
431 "Column", "Column number in GtkListStore "
432 "where history list strings are kept",
434 0, G_PARAM_READWRITE));
436 g_object_class_install_property(object_class, PROP_MAX,
437 g_param_spec_int("max_characters",
438 "Maximum number of characters",
439 "Maximum number of characters "
442 0, G_PARAM_READWRITE |
445 g_object_class_install_property(object_class, PROP_HISTORY_LIMIT,
446 g_param_spec_int("history_limit",
447 "Maximum number of history items",
448 "Maximum number of history items "
449 "in search combobox",
451 5, G_PARAM_READWRITE |
455 * HildonFindToolbar::search:
456 * @toolbar: the toolbar which received the signal
458 * Gets emitted when the find button is pressed.
460 HildonFindToolbar_signal[SEARCH] =
462 "search", HILDON_TYPE_FIND_TOOLBAR,
463 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
464 (HildonFindToolbarClass, search),
465 NULL, NULL, gtk_marshal_VOID__VOID,
469 * HildonFindToolbar::close:
470 * @toolbar: the toolbar which received the signal
472 * Gets emitted when the close button is pressed.
474 HildonFindToolbar_signal[CLOSE] =
476 "close", HILDON_TYPE_FIND_TOOLBAR,
477 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
478 (HildonFindToolbarClass, close),
479 NULL, NULL, gtk_marshal_VOID__VOID,
483 * HildonFindToolbar::invalid-input:
484 * @toolbar: the toolbar which received the signal
486 * Gets emitted when the maximum search prefix length is reached and
487 * user tries to type more.
489 HildonFindToolbar_signal[INVALID_INPUT] =
491 "invalid_input", HILDON_TYPE_FIND_TOOLBAR,
492 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
493 (HildonFindToolbarClass, invalid_input),
494 NULL, NULL, gtk_marshal_VOID__VOID,
498 * HildonFindToolbar::history-append:
499 * @toolbar: the toolbar which received the signal
501 * Gets emitted when the current search prefix should be added to history.
503 HildonFindToolbar_signal[HISTORY_APPEND] =
505 "history_append", HILDON_TYPE_FIND_TOOLBAR,
506 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET
507 (HildonFindToolbarClass, history_append),
508 g_signal_accumulator_true_handled, NULL,
509 gtk_marshal_BOOLEAN__VOID,
514 hildon_find_toolbar_init(HildonFindToolbar *self)
516 GtkToolItem *label_container;
517 GtkToolItem *entry_combo_box_container;
519 self->priv = G_TYPE_INSTANCE_GET_PRIVATE(self,
520 HILDON_TYPE_FIND_TOOLBAR,
521 HildonFindToolbarPrivate);
523 /* Create the label */
524 self->priv->label = gtk_label_new(_("Ecdg_ti_find_toolbar_label"));
526 gtk_misc_set_padding (GTK_MISC(self->priv->label), FIND_LABEL_XPADDING,
527 FIND_LABEL_YPADDING);
529 label_container = gtk_tool_item_new();
530 gtk_container_add(GTK_CONTAINER(label_container),
532 gtk_widget_show_all(GTK_WIDGET(label_container));
533 gtk_toolbar_insert (GTK_TOOLBAR(self), label_container, -1);
535 /* ComboBoxEntry for search prefix string / history list */
536 self->priv->entry_combo_box = GTK_COMBO_BOX_ENTRY(gtk_combo_box_entry_new());
537 g_signal_connect(hildon_find_toolbar_get_entry(self->priv),
539 G_CALLBACK(hildon_find_toolbar_emit_invalid_input), self);
540 entry_combo_box_container = gtk_tool_item_new();
541 gtk_tool_item_set_expand(entry_combo_box_container, TRUE);
542 gtk_container_add(GTK_CONTAINER(entry_combo_box_container),
543 GTK_WIDGET(self->priv->entry_combo_box));
544 gtk_widget_show_all(GTK_WIDGET(entry_combo_box_container));
545 gtk_toolbar_insert (GTK_TOOLBAR(self), entry_combo_box_container, -1);
546 g_signal_connect(hildon_find_toolbar_get_entry(self->priv),
548 G_CALLBACK(hildon_find_toolbar_entry_key_press), self);
551 self->priv->find_button = gtk_tool_button_new (
552 gtk_image_new_from_icon_name ("qgn_toolb_browser_gobutton",
553 HILDON_ICON_SIZE_TOOLBAR),
555 g_signal_connect(self->priv->find_button, "clicked",
556 G_CALLBACK(hildon_find_toolbar_emit_search), self);
557 gtk_widget_show_all(GTK_WIDGET(self->priv->find_button));
558 gtk_toolbar_insert (GTK_TOOLBAR(self), self->priv->find_button, -1);
559 if ( GTK_WIDGET_CAN_FOCUS( GTK_BIN(self->priv->find_button)->child) )
560 GTK_WIDGET_UNSET_FLAGS(
561 GTK_BIN(self->priv->find_button)->child, GTK_CAN_FOCUS);
564 self->priv->separator = gtk_separator_tool_item_new();
565 gtk_widget_show(GTK_WIDGET(self->priv->separator));
566 gtk_toolbar_insert (GTK_TOOLBAR(self), self->priv->separator, -1);
569 self->priv->close_button = gtk_tool_button_new (
570 gtk_image_new_from_icon_name ("qgn_toolb_gene_close",
571 HILDON_ICON_SIZE_TOOLBAR),
573 g_signal_connect(self->priv->close_button, "clicked",
574 G_CALLBACK(hildon_find_toolbar_emit_close), self);
575 gtk_widget_show_all(GTK_WIDGET(self->priv->close_button));
576 gtk_toolbar_insert (GTK_TOOLBAR(self), self->priv->close_button, -1);
577 if ( GTK_WIDGET_CAN_FOCUS( GTK_BIN(self->priv->close_button)->child) )
578 GTK_WIDGET_UNSET_FLAGS(
579 GTK_BIN(self->priv->close_button)->child, GTK_CAN_FOCUS);
585 * hildon_find_toolbar_new:
586 * @label: label for the find_toolbar, NULL to set the label to
589 * Returns a new HildonFindToolbar.
594 hildon_find_toolbar_new(const gchar *label)
596 GtkWidget *findtoolbar;
598 findtoolbar = GTK_WIDGET(g_object_new(HILDON_TYPE_FIND_TOOLBAR, NULL));
600 g_object_set(findtoolbar, "label", label, NULL);
606 * hildon_find_toolbar_new_with_model
607 * @label: label for the find_toolbar, NULL to set the label to
609 * @model: A @GtkListStore.
610 * @column: Indicating which column the search histry list will
611 * retreive string from.
613 * Returns a new HildonFindToolbar, with a model.
617 hildon_find_toolbar_new_with_model(const gchar *label,
621 GtkWidget *findtoolbar;
623 findtoolbar = hildon_find_toolbar_new(label);
624 g_object_set(findtoolbar, "list", model,
625 "column", column, NULL);
631 * hildon_find_toolbar_highlight_entry
632 * @HildonFindToolbar: Find Toolbar whose entry is to be highlighted.
633 * @get_focus: if user passes TRUE to this value, then the text in
634 * the entry will not only get highlighted, but also get focused.
638 hildon_find_toolbar_highlight_entry(HildonFindToolbar *ftb,
641 GtkEntry *entry = NULL;
643 g_return_if_fail(HILDON_IS_FIND_TOOLBAR(ftb));
645 entry = hildon_find_toolbar_get_entry(ftb->priv);
647 gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
650 gtk_widget_grab_focus(GTK_WIDGET(entry));