2008-09-22 Alejandro Pinheiro <apinheiro@igalia.com>
[hildon] / examples / hildon-pannable-area-example-3.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 "hildon.h"
31
32 GtkWidget *btn;
33
34 static void
35 on_button_clicked (GtkWidget *widget, gpointer data)
36 {
37     g_debug ("Button %d clicked", GPOINTER_TO_INT (data));
38     btn = widget;
39 }
40
41 static void
42 find_button_clicked (GtkButton *button,
43                      gpointer user_data)
44 {
45         HildonPannableArea *panarea;
46
47         panarea = HILDON_PANNABLE_AREA (user_data);
48
49         hildon_pannable_area_scroll_to_child (panarea, btn);
50 }
51
52 int
53 main (int argc, char **args)
54 {
55     int i;
56     HildonProgram *program;
57     GtkWidget *window, *panarea, *button;
58     GtkWidget *hbox, *vbox;
59
60     gtk_init (&argc, &args);
61
62     program = hildon_program_get_instance ();
63
64     /* Create the main window */
65     window = hildon_window_new ();
66     hildon_program_add_window (program, HILDON_WINDOW (window));
67     gtk_container_set_border_width (GTK_CONTAINER (window), 5);
68
69     /* Create a VBox and pack some buttons */
70     vbox = gtk_vbox_new (FALSE, 1);
71     for (i = 0; i < 80; i++) {
72             gchar *label = g_strdup_printf ("Button number %d", i);
73
74             button = gtk_button_new_with_label (label);
75             gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
76             g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (on_button_clicked), GINT_TO_POINTER (i));
77             g_free (label);
78     }
79
80     /* Put everything in a pannable area */
81     panarea = hildon_pannable_area_new ();
82     hildon_pannable_area_add_with_viewport(HILDON_PANNABLE_AREA (panarea), GTK_WIDGET (vbox));
83
84     vbox = gtk_vbox_new (FALSE, 10);
85     hbox = gtk_hbox_new (FALSE, 10);
86
87     button = gtk_button_new_with_label ("Find the latest clicked button");
88     g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (find_button_clicked), panarea);
89     gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
90
91     g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (gtk_main_quit), NULL);
92
93     gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 10);
94     gtk_box_pack_start (GTK_BOX (vbox), panarea, TRUE, TRUE, 0);
95
96     gtk_container_add (GTK_CONTAINER (window), vbox);
97
98     gtk_widget_show_all (GTK_WIDGET (window));
99
100     gtk_main ();
101
102     return 0;
103 }