Make sure that all timeouts in HildonBanner are removed
[hildon] / examples / hildon-pannable-area-gesture-signals-example.c
1 /*
2  * This file is a part of hildon examples
3  *
4  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
5  *
6  * Author: Karl Lattimer <karl.lattimer@nokia.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                                        <stdio.h>
26 #include                                        <stdlib.h>
27 #include                                        <glib.h>
28 #include                                        <gtk/gtk.h>
29 #include                                        <hildon/hildon.h>
30
31 enum { TEXT_COLUMN, OPTIONAL_COLUMN, N_COLUMNS };
32
33 typedef struct {
34   GtkWidget *treeview;
35 } HiddenColContext;
36
37 HiddenColContext *ctx;
38
39 static void
40 horizontal_movement (HildonPannableArea *area,
41                      HildonMovementDirection direction,
42                      gdouble x, gdouble y, gpointer user_data)
43 {
44   GtkTreePath *path;
45   GdkRectangle rect;
46   gint col_x;
47   GtkTreeViewColumn *col = GTK_TREE_VIEW_COLUMN (user_data);
48   GtkWidget *child = hildon_pannable_get_child_widget_at (area, x, y);
49
50   g_print ("widget %p treeview %p\n", child, ctx->treeview);
51
52   if (child == ctx->treeview) {
53     if (direction == HILDON_MOVEMENT_LEFT){
54
55       path = gtk_tree_path_new_first ();
56
57       gtk_tree_view_get_background_area (GTK_TREE_VIEW (ctx->treeview),
58                                          path, col, &rect);
59
60       gtk_tree_view_convert_bin_window_to_tree_coords (GTK_TREE_VIEW (ctx->treeview),
61                                                        rect.x, 0, &col_x, NULL);
62
63       gtk_tree_path_free (path);
64
65       hildon_pannable_area_scroll_to (area, col_x, -1);
66     }
67     else {
68       hildon_pannable_area_scroll_to (area, 0, -1);
69     }
70   }
71
72   g_print ("horizontal_movement %lf, %lf\n", x, y);
73 }
74
75 static void
76 vertical_movement (HildonPannableArea *area,
77                    HildonMovementDirection direction,
78                    gdouble x, gdouble y,
79                    gpointer user_data)
80 {
81   g_print ("vertical_movement: %lf, %lf\n", x, y);
82 }
83
84 static void
85 get_sawtooth_label (gchar **label, guint num)
86 {
87   static gchar *sawtooth = NULL;
88   gchar *label_aux, *sawtooth_aux;
89
90   if (num % 5 != 0) {
91     sawtooth_aux = g_strconcat (" ", sawtooth, NULL);
92     g_free (sawtooth);
93
94     sawtooth = sawtooth_aux;
95   } else {
96     sawtooth = g_strdup (" ");
97   }
98
99   label_aux = g_strconcat (sawtooth, *label, NULL);
100   g_free (*label);
101
102   *label = label_aux;
103 }
104
105 int
106 main (int argc, char **argv)
107 {
108     int i;
109     HildonProgram *program;
110     GtkWidget *window, *tv, *panarea;
111     GtkTreeViewColumn *col;
112     GtkCellRenderer *renderer;
113     GtkListStore *store;
114
115     hildon_gtk_init (&argc, &argv);
116
117     program = hildon_program_get_instance ();
118
119     ctx = g_new0 (HiddenColContext, 1);
120
121     /* Create the main window */
122     window = hildon_window_new ();
123     hildon_program_add_window (program, HILDON_WINDOW (window));
124
125     gtk_container_set_border_width (GTK_CONTAINER (window), 5);
126
127     /* Create a treeview */
128     tv = gtk_tree_view_new ();
129     ctx->treeview = tv;
130
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_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
134
135     gtk_tree_view_column_set_fixed_width (col, 700);
136
137     gtk_tree_view_append_column (GTK_TREE_VIEW(tv), col);
138
139     col = gtk_tree_view_column_new_with_attributes ("Title", renderer, "text", OPTIONAL_COLUMN, NULL);
140     gtk_tree_view_append_column (GTK_TREE_VIEW(tv), col);
141
142     /* Add some rows to the treeview */
143     store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING);
144     for (i = 0; i < 100; i++) {
145             GtkTreeIter iter;
146             gchar *label = g_strdup_printf ("Row number %d", i);
147             gchar *label_optional = g_strdup_printf ("< >");
148
149             get_sawtooth_label (&label, i);
150
151             gtk_list_store_append (store, &iter);
152             gtk_list_store_set (store, &iter, TEXT_COLUMN, label, -1);
153             gtk_list_store_set (store, &iter, OPTIONAL_COLUMN, label_optional, -1);
154             g_free (label);
155             g_free (label_optional);
156     }
157     gtk_tree_view_set_model (GTK_TREE_VIEW (tv), GTK_TREE_MODEL (store));
158     g_object_unref (store);
159
160     /* Put everything in a pannable area */
161     panarea = hildon_pannable_area_new ();
162     g_object_set (panarea, "mov_mode", HILDON_MOVEMENT_MODE_VERT,
163                   "hovershoot_max", 0,
164                   "hscrollbar_policy", GTK_POLICY_NEVER, NULL);
165
166     gtk_container_add (GTK_CONTAINER (panarea), GTK_WIDGET (tv));
167     gtk_container_add (GTK_CONTAINER (window), panarea);
168
169     g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (gtk_main_quit), NULL);
170
171     g_signal_connect (G_OBJECT (panarea), "horizontal_movement", G_CALLBACK (horizontal_movement), col);
172     g_signal_connect (G_OBJECT (panarea), "vertical_movement", G_CALLBACK (vertical_movement), NULL);
173
174     gtk_widget_show_all (GTK_WIDGET (window));
175
176     gtk_main ();
177
178     return 0;
179 }