Ensure to show the initial-hint the first time the pannable becomes
[hildon] / examples / hildon-pannable-area-initial-hint-example.c
1 /*
2  * This file is a part of hildon examples
3  *
4  * Copyright (C) 2009 Nokia Corporation, all rights reserved.
5  *
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23
24 #include                                        <stdio.h>
25 #include                                        <stdlib.h>
26 #include                                        <glib.h>
27 #include                                        <gtk/gtk.h>
28 #include                                        <string.h>
29 #include                                        <hildon/hildon.h>
30
31 enum { TEXT_COLUMN, N_COLUMNS };
32
33 static void
34 on_button_clicked (GtkButton *button,
35                    gpointer user_data)
36 {
37   GtkListStore *store = NULL;
38   GtkTreeIter   iter;
39   gint          i;
40
41   store = GTK_LIST_STORE (user_data);
42
43   for (i = 0; i < 5; i++) {
44     gtk_list_store_append (store, &iter);
45     gtk_list_store_set (store, &iter,
46                         TEXT_COLUMN, "Extra row", -1);
47   }
48 }
49
50 static GtkWidget*
51 create_content ()
52 {
53   gint               i;
54   GtkWidget         *tv       = NULL;
55   GtkWidget         *panarea  = NULL;
56   GtkWidget         *button   = NULL;;
57   GtkTreeViewColumn *col      = NULL;
58   GtkCellRenderer   *renderer = NULL;
59   GtkListStore      *store    = NULL;
60   GtkWidget         *vbox     = NULL;
61
62   /* Create a treeview */
63 #ifdef MAEMO_GTK
64   tv = hildon_gtk_tree_view_new (HILDON_UI_MODE_NORMAL);
65 #else
66   tv = GTK_TREE_VIEW (gtk_tree_view_new ());
67 #endif /* MAEMO_GTK */
68
69   renderer = gtk_cell_renderer_text_new ();
70   g_object_set (renderer, "width", 1, "xalign", 0.5, NULL);
71
72   col = gtk_tree_view_column_new ();
73   gtk_tree_view_column_set_title (col, "Title");
74
75   gtk_tree_view_column_pack_start (col, renderer, TRUE);
76   gtk_tree_view_column_set_attributes (col, renderer, "text", TEXT_COLUMN, NULL);
77
78   gtk_tree_view_append_column (GTK_TREE_VIEW (tv), col);
79
80   /* Add some rows to the treeview */
81   store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING);
82
83   for (i = 0; i < 3; i++) {
84     GtkTreeIter iter;
85
86     gtk_list_store_append (store, &iter);
87     gtk_list_store_set (store, &iter,
88                         TEXT_COLUMN, "One row", -1);
89   }
90
91   gtk_tree_view_set_model (GTK_TREE_VIEW (tv), GTK_TREE_MODEL (store));
92   g_object_unref (store);
93
94   /* Put everything in a pannable area */
95   panarea = hildon_pannable_area_new ();
96   gtk_container_add (GTK_CONTAINER (panarea), GTK_WIDGET (tv));
97
98   vbox = gtk_vbox_new (FALSE, 5);
99
100   button = gtk_button_new_with_label ("Add some rows");
101   g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (on_button_clicked), store);
102
103   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
104   gtk_box_pack_start (GTK_BOX (vbox), panarea, TRUE, TRUE, 6);
105
106   return vbox;
107 }
108
109 int
110 main (int argc, char **argv)
111 {
112   HildonProgram *program = NULL;
113   GtkWidget     *window  = NULL;
114   GtkWidget     *content = NULL;
115
116   hildon_gtk_init (&argc, &argv);
117
118   program = hildon_program_get_instance ();
119
120   /* Create the main window */
121   window = hildon_window_new ();
122   hildon_program_add_window (program, HILDON_WINDOW (window));
123   gtk_container_set_border_width (GTK_CONTAINER (window), 5);
124
125   content = create_content ();
126
127   gtk_container_add (GTK_CONTAINER (window), content);
128
129   g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (gtk_main_quit), NULL);
130
131   gtk_widget_show_all (GTK_WIDGET (window));
132
133   gtk_main ();
134
135   return 0;
136 }