2009-02-05 Alberto Garcia <agarcia@igalia.com>
[hildon] / examples / hildon-stackable-window-example.c
1 /*
2  * This file is a part of hildon examples
3  *
4  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19  * 02110-1301 USA
20  *
21  */
22
23 #include                                        "hildon.h"
24
25 static gint                                     global_stack_count = 1;
26
27 static void
28 add_window                                      (GtkWidget             *button,
29                                                  HildonStackableWindow *parent);
30
31 static void
32 push_windows                                    (GtkWidget     *button,
33                                                  GtkSpinButton *spin);
34
35 static void
36 pop_windows                                     (GtkWidget     *button,
37                                                  GtkSpinButton *spin);
38
39 static GtkWidget*
40 new_window                                      (HildonStackableWindow *parent)
41 {
42     GtkWidget *window, *hbbox, *vbox, *label, *add, *new;
43     GtkWidget *spin1hbox, *spin1label1, *spin1, *spin1label2, *pushbtn, *align1;
44     GtkWidget *spin2hbox, *spin2label1, *spin2, *spin2label2, *popbtn, *align2;
45     gint stack_number, win_number;
46     gchar *text;
47
48     window = hildon_stackable_window_new ();
49
50     if (parent) {
51         stack_number = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (parent), "stack-number"));
52         win_number = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (parent), "win-number")) + 1;
53     } else {
54         stack_number = global_stack_count++;
55         win_number = 1;
56     }
57     g_object_set_data (G_OBJECT (window), "stack-number", GINT_TO_POINTER (stack_number));
58     g_object_set_data (G_OBJECT (window), "win-number", GINT_TO_POINTER (win_number));
59
60     /* Window title */
61     text = g_strdup_printf ("Stack number %d - window %d", stack_number, win_number);
62     gtk_window_set_title (GTK_WINDOW (window), text);
63     g_free (text);
64
65     /* Main label */
66     text = g_strdup_printf ("Stack number %d\nWindow number %d", stack_number, win_number);
67     label = gtk_label_new (text);
68     g_free (text);
69
70     hbbox = gtk_hbutton_box_new ();
71
72     /* Button to push a window to the current stack */
73     add = hildon_gtk_button_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
74     gtk_button_set_label (GTK_BUTTON (add), "Add a window to this stack");
75     gtk_box_pack_start (GTK_BOX (hbbox), add, FALSE, FALSE, 0);
76     g_signal_connect (G_OBJECT (add), "clicked", G_CALLBACK (add_window), window);
77
78     /* Button to create a new stack */
79     new = hildon_gtk_button_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
80     gtk_button_set_label (GTK_BUTTON (new), "Add a window to a new stack");
81     gtk_box_pack_start (GTK_BOX (hbbox), new, FALSE, FALSE, 0);
82     g_signal_connect (G_OBJECT (new), "clicked", G_CALLBACK (add_window), NULL);
83
84     /* Spinbox and button to push many windows */
85     spin1hbox = gtk_hbox_new (FALSE, 0);
86     spin1label1 = gtk_label_new ("Push");
87     spin1 = gtk_spin_button_new (GTK_ADJUSTMENT (gtk_adjustment_new (2, 2, 5, 1, 1, 1)), 1, 0);
88     gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spin1), TRUE);
89     spin1label2 = gtk_label_new ("windows");
90     pushbtn = hildon_gtk_button_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
91     gtk_button_set_label (GTK_BUTTON (pushbtn), "Push windows");
92     gtk_box_pack_start (GTK_BOX (spin1hbox), spin1label1, FALSE, FALSE, 0);
93     gtk_box_pack_start (GTK_BOX (spin1hbox), spin1, FALSE, FALSE, 0);
94     gtk_box_pack_start (GTK_BOX (spin1hbox), spin1label2, FALSE, FALSE, 0);
95     gtk_box_pack_start (GTK_BOX (spin1hbox), pushbtn, FALSE, FALSE, 10);
96     align1 = gtk_alignment_new (0.5, 0.5, 0, 0);
97     gtk_container_add (GTK_CONTAINER (align1), spin1hbox);
98     g_signal_connect (G_OBJECT (pushbtn), "clicked", G_CALLBACK (push_windows), spin1);
99
100     /* Spinbox and button to pop many windows */
101     spin2hbox = gtk_hbox_new (FALSE, 0);
102     spin2label1 = gtk_label_new ("Pop");
103     spin2 = gtk_spin_button_new (GTK_ADJUSTMENT (gtk_adjustment_new (2, 2, 5, 1, 1, 1)), 1, 0);
104     gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spin2), TRUE);
105     spin2label2 = gtk_label_new ("windows");
106     popbtn = hildon_gtk_button_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
107     gtk_button_set_label (GTK_BUTTON (popbtn), "Pop windows");
108     gtk_box_pack_start (GTK_BOX (spin2hbox), spin2label1, FALSE, FALSE, 0);
109     gtk_box_pack_start (GTK_BOX (spin2hbox), spin2, FALSE, FALSE, 0);
110     gtk_box_pack_start (GTK_BOX (spin2hbox), spin2label2, FALSE, FALSE, 0);
111     gtk_box_pack_start (GTK_BOX (spin2hbox), popbtn, FALSE, FALSE, 10);
112     align2 = gtk_alignment_new (0.5, 0.5, 0, 0);
113     gtk_container_add (GTK_CONTAINER (align2), spin2hbox);
114     g_signal_connect (G_OBJECT (popbtn), "clicked", G_CALLBACK (pop_windows), spin2);
115
116     vbox = gtk_vbox_new (FALSE, 0);
117     gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
118     gtk_box_pack_start (GTK_BOX (vbox), hbbox, FALSE, FALSE, 10);
119     gtk_box_pack_start (GTK_BOX (vbox), align1, FALSE, FALSE, 10);
120     gtk_box_pack_start (GTK_BOX (vbox), align2, FALSE, FALSE, 10);
121
122     gtk_container_set_border_width (GTK_CONTAINER (window), 6);
123     gtk_container_add (GTK_CONTAINER (window), vbox);
124     gtk_widget_show_all (vbox);
125
126     return window;
127 }
128
129 static void
130 add_window                                      (GtkWidget             *button,
131                                                  HildonStackableWindow *parent)
132 {
133     HildonWindowStack *stack = NULL;
134     GtkWidget *window;
135
136     if (parent) {
137         stack = hildon_stackable_window_get_stack (parent);
138     } else {
139         stack = hildon_window_stack_new ();
140     }
141
142     window = new_window (parent);
143
144     if (!stack) {
145         stack = hildon_window_stack_get_default ();
146     }
147     hildon_window_stack_push_1 (stack, HILDON_STACKABLE_WINDOW (window));
148 }
149
150 static void
151 push_windows                                    (GtkWidget     *button,
152                                                  GtkSpinButton *spin)
153 {
154     GList *l = NULL;
155     HildonWindowStack *stack = NULL;
156     HildonStackableWindow *parent;
157     gint nwindows = gtk_spin_button_get_value_as_int (spin);
158
159     parent = HILDON_STACKABLE_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (spin)));
160     stack = hildon_stackable_window_get_stack (parent);
161
162     while (nwindows > 0) {
163         parent = HILDON_STACKABLE_WINDOW (new_window (parent));
164         l = g_list_append (l, parent);
165         nwindows--;
166     }
167     hildon_window_stack_push_list (stack, l);
168     g_list_free (l);
169 }
170
171 static void
172 pop_windows                                     (GtkWidget     *button,
173                                                  GtkSpinButton *spin)
174 {
175     HildonWindowStack *stack = NULL;
176     HildonStackableWindow *win;
177     gint nwindows = gtk_spin_button_get_value_as_int (spin);
178
179     win = HILDON_STACKABLE_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (spin)));
180     stack = hildon_stackable_window_get_stack (win);
181
182     hildon_window_stack_pop (stack, nwindows, NULL);
183 }
184
185 int
186 main                                            (int    argc,
187                                                  char **argv)
188 {
189     GtkWidget *window;
190
191     hildon_gtk_init (&argc, &argv);
192
193     g_set_application_name ("hildon-stackable-window-example");
194
195     window = new_window (NULL);
196
197     g_signal_connect (G_OBJECT (window), "destroy",
198                       G_CALLBACK (gtk_main_quit), NULL);
199
200     gtk_widget_show (window);
201
202     gtk_main ();
203
204     return 0;
205 }