6b365e170526bad255abdcd0ddda071346e5540d
[hildon] / examples / hildon-pannable-area-example-4.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.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                      HildonPannableAreaMovDirection direction,
42                      GtkWidget *widget, gdouble x, gdouble y,
43                      gpointer user_data)
44 {
45   GtkTreePath *path;
46   GdkRectangle rect;
47   gint col_x;
48   GtkTreeViewColumn *col = GTK_TREE_VIEW_COLUMN (user_data);
49
50   g_print ("widget %p treeview %p\n", widget, ctx->treeview);
51
52   if (direction == HILDON_PANNABLE_AREA_MOV_LEFT) {
53
54     path = gtk_tree_path_new_first ();
55
56     gtk_tree_view_get_background_area (GTK_TREE_VIEW (ctx->treeview),
57                                        path, col, &rect);
58
59     gtk_tree_view_convert_bin_window_to_tree_coords (GTK_TREE_VIEW (ctx->treeview),
60                                                      rect.x, 0, &col_x, NULL);
61
62     gtk_tree_path_free (path);
63
64     hildon_pannable_area_scroll_to (area, col_x, -1);
65   }
66   else {
67     hildon_pannable_area_scroll_to (area, 0, -1);
68   }
69
70   g_print ("horizontal_movement %lf, %lf\n", x, y);
71 }
72
73 static void
74 vertical_movement (HildonPannableArea *area,
75                    HildonPannableAreaMovDirection direction,
76                    gdouble x, gdouble y,
77                    gpointer user_data)
78 {
79   g_print ("vertical_movement: %lf, %lf\n", x, y);
80 }
81
82 static void
83 get_sawtooth_label (gchar **label, guint num)
84 {
85   static gchar *sawtooth = NULL;
86   gchar *label_aux, *sawtooth_aux;
87
88   if (num % 5 != 0) {
89     sawtooth_aux = g_strconcat (" ", sawtooth, NULL);
90     g_free (sawtooth);
91
92     sawtooth = sawtooth_aux;
93   } else {
94     sawtooth = g_strdup (" ");
95   }
96
97   label_aux = g_strconcat (sawtooth, *label, NULL);
98   g_free (*label);
99
100   *label = label_aux;
101 }
102
103 int
104 main (int argc, char **args)
105 {
106     int i;
107     HildonProgram *program;
108     GtkWidget *window, *tv, *panarea;
109     GtkTreeViewColumn *col;
110     GtkCellRenderer *renderer;
111     GtkListStore *store;
112     GtkVBox *vbox;
113
114     gtk_init (&argc, &args);
115
116     program = hildon_program_get_instance ();
117
118     ctx = g_new0 (HiddenColContext, 1);
119
120     /* Create the main window */
121     window = hildon_window_new ();
122     hildon_program_add_window (program, HILDON_WINDOW (window));
123
124     gtk_container_set_border_width (GTK_CONTAINER (window), 5);
125
126     /* Create a VBox and pack some buttons */
127     vbox = GTK_VBOX (gtk_vbox_new (FALSE, 1));
128
129     /* Create a treeview */
130     tv = gtk_tree_view_new ();
131     ctx->treeview = tv;
132
133     renderer = gtk_cell_renderer_text_new ();
134     col = gtk_tree_view_column_new_with_attributes ("Title", renderer, "text", TEXT_COLUMN, NULL);
135     gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
136
137     gtk_tree_view_column_set_fixed_width (col, 700);
138
139     gtk_tree_view_append_column (GTK_TREE_VIEW(tv), col);
140
141     col = gtk_tree_view_column_new_with_attributes ("Title", renderer, "text", OPTIONAL_COLUMN, NULL);
142     gtk_tree_view_append_column (GTK_TREE_VIEW(tv), col);
143
144     /* Add some rows to the treeview */
145     store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING);
146     for (i = 0; i < 100; i++) {
147             GtkTreeIter iter;
148             gchar *label = g_strdup_printf ("Row number %d", i);
149             gchar *label_optional = g_strdup_printf ("< >");
150
151             get_sawtooth_label (&label, i);
152
153             gtk_list_store_append (store, &iter);
154             gtk_list_store_set (store, &iter, TEXT_COLUMN, label, -1);
155             gtk_list_store_set (store, &iter, OPTIONAL_COLUMN, label_optional, -1);
156             g_free (label);
157             g_free (label_optional);
158     }
159     gtk_tree_view_set_model (GTK_TREE_VIEW (tv), GTK_TREE_MODEL (store));
160     g_object_unref (store);
161
162     /* Pack the treeview in the VBox */
163     gtk_box_pack_start (GTK_BOX (vbox), tv, TRUE, TRUE, 0);
164
165     /* Put everything in a pannable area */
166     panarea = hildon_pannable_area_new ();
167     g_object_set (panarea, "mov_mode", HILDON_PANNABLE_AREA_MOV_MODE_VERT,
168                   "hovershoot_max", 0,
169                   "hindicator_mode", HILDON_PANNABLE_AREA_INDICATOR_MODE_HIDE, NULL);
170
171     hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA (panarea), GTK_WIDGET (vbox));
172     gtk_container_add (GTK_CONTAINER (window), panarea);
173
174     g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (gtk_main_quit), NULL);
175
176     g_signal_connect (G_OBJECT (panarea), "horizontal_movement", G_CALLBACK (horizontal_movement), col);
177     g_signal_connect (G_OBJECT (panarea), "vertical_movement", G_CALLBACK (vertical_movement), NULL);
178
179     gtk_widget_show_all (GTK_WIDGET (window));
180
181     gtk_main ();
182
183     return 0;
184 }