contributed and reviewed by: Claudio Saavedra <csaavedra@igalia.com>
[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 { 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
120   gtk_init (&argc, &args);
121
122   program = hildon_program_get_instance ();
123
124   /* Create the main window */
125   window = hildon_window_new ();
126   hildon_program_add_window (program, HILDON_WINDOW (window));
127   gtk_container_set_border_width (GTK_CONTAINER (window), 5);
128
129   /* Create a treeview */
130   tv = gtk_tree_view_new ();
131   renderer = gtk_cell_renderer_text_new ();
132   col = gtk_tree_view_column_new_with_attributes ("Title", renderer, "text", TEXT_COLUMN, NULL);
133   gtk_tree_view_append_column (GTK_TREE_VIEW(tv), col);
134
135   /* Add some rows to the treeview */
136   store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING);
137   for (i = 0; i < 100; i++) {
138     GtkTreeIter iter;
139     gchar *label = g_strdup_printf ("Row %d", i);
140     gtk_list_store_append (store, &iter);
141     gtk_list_store_set (store, &iter, TEXT_COLUMN, label, -1);
142     g_free (label);
143   }
144   gtk_tree_view_set_model (GTK_TREE_VIEW (tv), GTK_TREE_MODEL (store));
145   g_object_unref (store);
146
147   /* Put everything in a pannable area */
148   panarea = hildon_pannable_area_new ();
149   gtk_container_add (GTK_CONTAINER (panarea), GTK_WIDGET (tv));
150
151   ctx = g_new0 (SearchContext, 1);
152
153   vbox = gtk_vbox_new (FALSE, 5);
154   hbox = gtk_hbox_new (FALSE, 5);
155   button = gtk_button_new_from_stock (GTK_STOCK_FIND);
156
157   entry = gtk_entry_new ();
158   gtk_entry_set_text (GTK_ENTRY (entry), "Enter some text to scroll");
159
160   g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (search_button_clicked), &scroll);
161
162   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
163   gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
164   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 10);
165
166   ctx->scroll_entry = entry;
167
168   hbox = gtk_hbox_new (FALSE, 5);
169   button = gtk_button_new_from_stock (GTK_STOCK_FIND);
170
171   entry = gtk_entry_new ();
172   gtk_entry_set_text (GTK_ENTRY (entry), "Enter some text to jump");
173
174   g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (search_button_clicked), &jump);
175
176   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
177   gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
178   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 10);
179
180   ctx->jump_entry = entry;
181   ctx->treeview = tv;
182   ctx->panarea = panarea;
183
184   g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (gtk_main_quit), NULL);
185
186   gtk_box_pack_start (GTK_BOX (vbox), panarea, TRUE, TRUE, 0);
187
188   gtk_container_add (GTK_CONTAINER (window), vbox);
189
190   gtk_widget_show_all (GTK_WIDGET (window));
191
192   gtk_main ();
193
194   return 0;
195 }