8bad34e0637cb4c6f599c16df0bc84506ee1155f
[hildon] / examples / hildon-pannable-area-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.h"
30
31 enum { TEXT_COLUMN, N_COLUMNS };
32
33 static void
34 on_button_clicked (GtkWidget *widget, gpointer data)
35 {
36     g_debug ("Button %d clicked", GPOINTER_TO_INT (data));
37 }
38
39
40 static void
41 get_sawtooth_label (gchar **label, guint num)
42 {
43   static gchar *sawtooth = NULL;
44   gchar *label_aux, *sawtooth_aux;
45   
46   if (num % 5 != 0) {
47     sawtooth_aux = g_strconcat (" ", sawtooth, NULL);
48     g_free (sawtooth);
49     
50     sawtooth = sawtooth_aux;
51   } else {
52     if (sawtooth)
53       g_free (sawtooth);
54
55     sawtooth = g_strdup (" ");
56   }
57   
58   label_aux = g_strconcat (sawtooth, *label, NULL);
59   g_free (*label);
60   
61   *label = label_aux;
62 }
63
64 int
65 main (int argc, char **args)
66 {
67     int i;
68     HildonProgram *program;
69     GtkWidget *window, *tv, *panarea;
70     GtkTreeViewColumn *col;
71     GtkCellRenderer *renderer;
72     GtkListStore *store;
73     GtkVBox *vbox;
74
75     gtk_init (&argc, &args);
76
77     program = hildon_program_get_instance ();
78
79     /* Create the main window */
80     window = hildon_window_new ();
81     hildon_program_add_window (program, HILDON_WINDOW (window));
82
83     gtk_container_set_border_width (GTK_CONTAINER (window), 5);
84
85     /* Create a VBox and pack some buttons */
86     vbox = GTK_VBOX (gtk_vbox_new (FALSE, 1));
87     for (i = 0; i < 30; i++) {
88             gchar *label = g_strdup_printf ("Button number %d", i);
89             GtkWidget *but = NULL;
90
91             get_sawtooth_label (&label, i);
92
93             but = gtk_button_new_with_label (label);
94             gtk_box_pack_start (GTK_BOX (vbox), but, TRUE, TRUE, 0);
95             g_signal_connect (G_OBJECT (but), "clicked", G_CALLBACK (on_button_clicked), GINT_TO_POINTER (i));
96             g_free (label);
97     }
98
99     /* Create a treeview */
100     tv = gtk_tree_view_new ();
101     renderer = gtk_cell_renderer_text_new ();
102     col = gtk_tree_view_column_new_with_attributes ("Title", renderer, "text", TEXT_COLUMN, NULL);
103     gtk_tree_view_append_column (GTK_TREE_VIEW(tv), col);
104
105     /* Add some rows to the treeview */
106     store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING);
107     for (i = 0; i < 100; i++) {
108             GtkTreeIter iter;
109             gchar *label = g_strdup_printf ("Row number %d", i);
110             
111             get_sawtooth_label (&label, i);
112
113             gtk_list_store_append (store, &iter);
114             gtk_list_store_set (store, &iter, TEXT_COLUMN, label, -1);
115             g_free (label);
116     }
117     gtk_tree_view_set_model (GTK_TREE_VIEW (tv), GTK_TREE_MODEL (store));
118     g_object_unref (store);
119
120     /* Pack the treeview in the VBox */
121     gtk_box_pack_start (GTK_BOX (vbox), tv, TRUE, TRUE, 0);
122
123     /* Put everything in a pannable area */
124     panarea = hildon_pannable_area_new ();
125     hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA (panarea), GTK_WIDGET (vbox));
126     gtk_container_add (GTK_CONTAINER (window), panarea);
127
128     g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (gtk_main_quit), NULL);
129
130     gtk_widget_show_all (GTK_WIDGET (window));
131
132     gtk_main ();
133
134     return 0;
135 }