| 1 |
/* |
| 2 |
* This file is a part of hildon examples |
| 3 |
* |
| 4 |
* Copyright (C) 2009 Nokia Corporation, all rights reserved. |
| 5 |
* |
| 6 |
* Author: Alberto Garcia <agarcia@igalia.com> |
| 7 |
* |
| 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 (at your option) any later version. |
| 12 |
* |
| 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. |
| 17 |
* |
| 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 |
| 21 |
* 02110-1301 USA |
| 22 |
* |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <hildon/hildon.h> |
| 26 |
|
| 27 |
static const char *countries[] = { |
| 28 |
"Austria", "Belgium", "Bulgaria", "Cyprus", "Czech Republic", |
| 29 |
"Denmark", "Estonia", "Finland", "France", "Germany", "Greece", |
| 30 |
"Hungary", "Ireland", "Italy", "Latvia", "Lithuania", "Luxembourg", |
| 31 |
"Malta", "Netherlands", "Poland", "Portugal", "Romania", "Slovakia", |
| 32 |
"Slovenia", "Spain", "Sweden", "United Kingdom" |
| 33 |
}; |
| 34 |
|
| 35 |
static gboolean |
| 36 |
filter_func (GtkTreeModel *model, |
| 37 |
GtkTreeIter *iter, |
| 38 |
gchar *text, |
| 39 |
gpointer data) |
| 40 |
{ |
| 41 |
gboolean retvalue; |
| 42 |
gchar *rowtext = NULL; |
| 43 |
|
| 44 |
gtk_tree_model_get (model, iter, 0, &rowtext, -1); |
| 45 |
retvalue = g_str_has_prefix (rowtext, text); |
| 46 |
g_free (rowtext); |
| 47 |
|
| 48 |
return retvalue; |
| 49 |
} |
| 50 |
|
| 51 |
static HildonLiveSearch * |
| 52 |
create_live_search (GtkTreeModelFilter *filter) |
| 53 |
{ |
| 54 |
HildonLiveSearch *live; |
| 55 |
|
| 56 |
live = HILDON_LIVE_SEARCH (hildon_live_search_new ()); |
| 57 |
hildon_live_search_set_filter (live, filter); |
| 58 |
hildon_live_search_set_visible_func (live, filter_func, NULL, NULL); |
| 59 |
|
| 60 |
/* Instead of hildon_live_search_set_filter_func(), we could have used |
| 61 |
* hildon_live_search_set_text_column (live, 0); */ |
| 62 |
|
| 63 |
return live; |
| 64 |
} |
| 65 |
|
| 66 |
static GtkTreeModel * |
| 67 |
create_model (void) |
| 68 |
{ |
| 69 |
int i; |
| 70 |
GtkListStore *store; |
| 71 |
GtkTreeModel *filter; |
| 72 |
|
| 73 |
store = gtk_list_store_new (1, G_TYPE_STRING); |
| 74 |
|
| 75 |
for (i = 0; i < G_N_ELEMENTS (countries); i++) { |
| 76 |
gtk_list_store_insert_with_values (store, NULL, i, 0, countries[i], -1); |
| 77 |
} |
| 78 |
|
| 79 |
filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (store), NULL); |
| 80 |
g_object_unref (store); |
| 81 |
|
| 82 |
return filter; |
| 83 |
} |
| 84 |
|
| 85 |
static GtkWidget * |
| 86 |
create_tree_view (void) |
| 87 |
{ |
| 88 |
GtkWidget *tree_view; |
| 89 |
GtkCellRenderer *renderer; |
| 90 |
GtkTreeModel *model; |
| 91 |
|
| 92 |
tree_view = hildon_gtk_tree_view_new (HILDON_UI_MODE_NORMAL); |
| 93 |
gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (tree_view), TRUE); |
| 94 |
|
| 95 |
model = create_model (); |
| 96 |
gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), model); |
| 97 |
g_object_unref (model); |
| 98 |
|
| 99 |
renderer = gtk_cell_renderer_text_new (); |
| 100 |
g_object_set (renderer, "xalign", 0.5, "weight", PANGO_WEIGHT_BOLD, NULL); |
| 101 |
|
| 102 |
gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view), 0, |
| 103 |
"Column 0", renderer, "text", 0, NULL); |
| 104 |
|
| 105 |
return tree_view; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
int |
| 110 |
main (int argc, |
| 111 |
char **argv) |
| 112 |
{ |
| 113 |
GtkWidget *window; |
| 114 |
GtkWidget *panarea; |
| 115 |
GtkWidget *vbox; |
| 116 |
GtkWidget *treeview; |
| 117 |
HildonLiveSearch *live; |
| 118 |
GtkTreeModelFilter *filter; |
| 119 |
|
| 120 |
hildon_gtk_init (&argc, &argv); |
| 121 |
|
| 122 |
window = hildon_window_new (); |
| 123 |
vbox = gtk_vbox_new (FALSE, 0); |
| 124 |
panarea = hildon_pannable_area_new (); |
| 125 |
|
| 126 |
treeview = create_tree_view (); |
| 127 |
filter = GTK_TREE_MODEL_FILTER (gtk_tree_view_get_model (GTK_TREE_VIEW (treeview))); |
| 128 |
|
| 129 |
live = create_live_search (filter); |
| 130 |
hildon_live_search_widget_hook (live, window, treeview); |
| 131 |
|
| 132 |
gtk_container_add (GTK_CONTAINER (panarea), treeview); |
| 133 |
gtk_box_pack_start (GTK_BOX (vbox), panarea, TRUE, TRUE, 0); |
| 134 |
gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (live), FALSE, FALSE, 0); |
| 135 |
gtk_container_add (GTK_CONTAINER (window), vbox); |
| 136 |
|
| 137 |
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); |
| 138 |
|
| 139 |
gtk_widget_show_all (window); |
| 140 |
|
| 141 |
gtk_main (); |
| 142 |
|
| 143 |
return 0; |
| 144 |
} |