Make sure that all timeouts in HildonBanner are removed
[hildon] / examples / hildon-pannable-area-scroll-jump-example.c
1 /*
2  * This file is a part of hildon examples
3  *
4  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
5  *
6  * Based in hildon-pannable-area-example.c
7  * by Karl Lattimer <karl.lattimer@nokia.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; version 2.1 of
12  * the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22  * 02110-1301 USA
23  *
24  */
25
26 #include                                        <stdio.h>
27 #include                                        <stdlib.h>
28 #include                                        <glib.h>
29 #include                                        <gtk/gtk.h>
30 #include                                        <string.h>
31 #include                                        <hildon/hildon.h>
32
33 enum { PIXBUF_COLUMN, TEXT_COLUMN, N_COLUMNS };
34
35 typedef struct {
36   GtkWidget *scroll_entry;
37   GtkWidget *jump_entry;
38   GtkWidget *panarea;
39   GtkWidget *treeview;
40 } SearchContext;
41
42 SearchContext *ctx;
43
44 static void
45 search_button_clicked (GtkButton *button,
46                        gpointer user_data)
47 {
48   GtkTreeModel *model;
49   const gchar *s1;
50   gchar *s2;
51   gboolean found;
52   GtkTreeIter iter;
53   GtkTreePath *path;
54   GdkRectangle rect;
55   gint y;
56   gboolean jump_or_scroll;
57
58   jump_or_scroll = *((gboolean *) user_data);
59
60   if (jump_or_scroll)
61     {
62       s1 = gtk_entry_get_text (GTK_ENTRY (ctx->scroll_entry));
63     }
64   else
65     {
66       s1 = gtk_entry_get_text (GTK_ENTRY (ctx->jump_entry));
67     }
68
69   model = gtk_tree_view_get_model (GTK_TREE_VIEW (ctx->treeview));
70
71   gtk_tree_model_get_iter_first (model, &iter);
72
73   do {
74     gtk_tree_model_get (model, &iter, TEXT_COLUMN, &s2, -1);
75     found = (strcmp (s1, s2) == 0);
76     g_free (s2);
77   } while (found != TRUE && gtk_tree_model_iter_next (model, &iter));
78
79   if (found) {
80     GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW (ctx->treeview));
81
82     path = gtk_tree_model_get_path (model, &iter);
83
84     gtk_tree_view_get_background_area (GTK_TREE_VIEW (ctx->treeview),
85                                        path, NULL, &rect);
86     gtk_tree_view_convert_bin_window_to_tree_coords (GTK_TREE_VIEW (ctx->treeview),
87                                                      0, rect.y, NULL, &y);
88     g_print ("text found in (0, %d)\n", y);
89
90     gtk_tree_selection_select_path (selection, path);
91     if (jump_or_scroll)
92       {
93         hildon_pannable_area_scroll_to (HILDON_PANNABLE_AREA (ctx->panarea),
94                                         -1, y);
95       }
96     else
97       {
98         hildon_pannable_area_jump_to (HILDON_PANNABLE_AREA (ctx->panarea),
99                                       -1, y);
100       }
101
102     gtk_tree_path_free (path);
103   }
104 }
105
106 int
107 main (int argc, char **argv)
108 {
109   int i;
110   HildonProgram *program;
111   GtkWidget *window, *tv, *panarea, *button;
112   GtkTreeViewColumn *col;
113   GtkCellRenderer *renderer;
114   GtkListStore *store;
115   GtkWidget *hbox, *vbox;
116   GtkWidget *entry;
117   gboolean scroll = TRUE;
118   gboolean jump = FALSE;
119   GSList *stocks, *item;
120
121   hildon_gtk_init (&argc, &argv);
122
123   program = hildon_program_get_instance ();
124
125   /* Create the main window */
126   window = hildon_window_new ();
127   hildon_program_add_window (program, HILDON_WINDOW (window));
128   gtk_container_set_border_width (GTK_CONTAINER (window), 5);
129
130   /* Create a treeview */
131   tv = gtk_tree_view_new ();
132
133   col = gtk_tree_view_column_new ();
134   gtk_tree_view_column_set_title (col, "Title");
135
136   renderer = gtk_cell_renderer_pixbuf_new ();
137   gtk_cell_renderer_set_fixed_size (renderer, 48, 48);
138   gtk_tree_view_column_pack_start (col, renderer, FALSE);
139   gtk_tree_view_column_set_attributes (col, renderer, "stock-id", PIXBUF_COLUMN, NULL);
140
141   renderer = gtk_cell_renderer_text_new ();
142   gtk_tree_view_column_pack_start (col, renderer, FALSE);
143   gtk_tree_view_column_set_attributes (col, renderer, "text", TEXT_COLUMN, NULL);
144   gtk_tree_view_append_column (GTK_TREE_VIEW(tv), col);
145
146   /* Add some rows to the treeview */
147   stocks = gtk_stock_list_ids ();
148   item = stocks;
149   store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING);
150   for (i = 0; i < 100; i++) {
151     GtkTreeIter iter;
152     GtkStockItem stock_item;
153     gchar *stock_id;
154
155     stock_id = (gchar *)item->data;
156     gtk_stock_lookup (stock_id, &stock_item);
157     gtk_list_store_append (store, &iter);
158     gtk_list_store_set (store, &iter, PIXBUF_COLUMN, stock_id, TEXT_COLUMN, stock_item.label, -1);
159
160     item = item->next? item->next : stocks;
161   }
162   gtk_tree_view_set_model (GTK_TREE_VIEW (tv), GTK_TREE_MODEL (store));
163   g_object_unref (store);
164
165   g_slist_foreach (stocks, (GFunc)g_free, NULL);
166   g_slist_free (stocks);
167
168   /* Put everything in a pannable area */
169   panarea = hildon_pannable_area_new ();
170   gtk_container_add (GTK_CONTAINER (panarea), GTK_WIDGET (tv));
171
172   ctx = g_new0 (SearchContext, 1);
173
174   vbox = gtk_vbox_new (FALSE, 5);
175   hbox = gtk_hbox_new (FALSE, 5);
176   button = gtk_button_new_from_stock (GTK_STOCK_FIND);
177
178   entry = gtk_entry_new ();
179   gtk_entry_set_text (GTK_ENTRY (entry), "Enter some text to scroll");
180
181   g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (search_button_clicked), &scroll);
182
183   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
184   gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
185   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 10);
186
187   ctx->scroll_entry = entry;
188
189   hbox = gtk_hbox_new (FALSE, 5);
190   button = gtk_button_new_from_stock (GTK_STOCK_FIND);
191
192   entry = gtk_entry_new ();
193   gtk_entry_set_text (GTK_ENTRY (entry), "Enter some text to jump");
194
195   g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (search_button_clicked), &jump);
196
197   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
198   gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
199   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 10);
200
201   ctx->jump_entry = entry;
202   ctx->treeview = tv;
203   ctx->panarea = panarea;
204
205   g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (gtk_main_quit), NULL);
206
207   gtk_box_pack_start (GTK_BOX (vbox), panarea, TRUE, TRUE, 0);
208
209   gtk_container_add (GTK_CONTAINER (window), vbox);
210
211   gtk_widget_show_all (GTK_WIDGET (window));
212
213   gtk_main ();
214
215   return 0;
216 }