ea1546ba7a10a441722be711b0ef06bef0d93553
[hildon] / examples / hildon-pannable-area-example-2.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.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 **args)
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   gtk_init (&argc, &args);
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_cell_renderer_set_fixed_size (renderer, -1, 66);
143   gtk_tree_view_column_pack_start (col, renderer, FALSE);
144   gtk_tree_view_column_set_attributes (col, renderer, "text", TEXT_COLUMN, NULL);
145   gtk_tree_view_append_column (GTK_TREE_VIEW(tv), col);
146
147   /* Add some rows to the treeview */
148   stocks = gtk_stock_list_ids ();
149   item = stocks;
150   store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING);
151   for (i = 0; i < 100; i++) {
152     GtkTreeIter iter;
153     GtkStockItem stock_item;
154     gchar *stock_id;
155
156     stock_id = (gchar *)item->data;
157     gtk_stock_lookup (stock_id, &stock_item);
158     gtk_list_store_append (store, &iter);
159     gtk_list_store_set (store, &iter, PIXBUF_COLUMN, stock_id, TEXT_COLUMN, stock_item.label, -1);
160
161     item = item->next? item->next : stocks;
162   }
163   gtk_tree_view_set_model (GTK_TREE_VIEW (tv), GTK_TREE_MODEL (store));
164   g_object_unref (store);
165
166   g_slist_foreach (stocks, (GFunc)g_free, NULL);
167   g_slist_free (stocks);
168
169   /* Put everything in a pannable area */
170   panarea = hildon_pannable_area_new ();
171   gtk_container_add (GTK_CONTAINER (panarea), GTK_WIDGET (tv));
172
173   ctx = g_new0 (SearchContext, 1);
174
175   vbox = gtk_vbox_new (FALSE, 5);
176   hbox = gtk_hbox_new (FALSE, 5);
177   button = gtk_button_new_from_stock (GTK_STOCK_FIND);
178
179   entry = gtk_entry_new ();
180   gtk_entry_set_text (GTK_ENTRY (entry), "Enter some text to scroll");
181
182   g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (search_button_clicked), &scroll);
183
184   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
185   gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
186   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 10);
187
188   ctx->scroll_entry = entry;
189
190   hbox = gtk_hbox_new (FALSE, 5);
191   button = gtk_button_new_from_stock (GTK_STOCK_FIND);
192
193   entry = gtk_entry_new ();
194   gtk_entry_set_text (GTK_ENTRY (entry), "Enter some text to jump");
195
196   g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (search_button_clicked), &jump);
197
198   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
199   gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
200   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 10);
201
202   ctx->jump_entry = entry;
203   ctx->treeview = tv;
204   ctx->panarea = panarea;
205
206   g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (gtk_main_quit), NULL);
207
208   gtk_box_pack_start (GTK_BOX (vbox), panarea, TRUE, TRUE, 0);
209
210   gtk_container_add (GTK_CONTAINER (window), vbox);
211
212   gtk_widget_show_all (GTK_WIDGET (window));
213
214   gtk_main ();
215
216   return 0;
217 }