Grabbed the focus in the HildonTextView button press callback
[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/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     /* Marked up window title */
66     text = g_strdup_printf ("Stack number <i>%d</i> - window <i>%d</i>", stack_number, win_number);
67     hildon_window_set_markup (HILDON_WINDOW (window), text);
68     g_free (text);
69
70     /* Main label */
71     text = g_strdup_printf ("Stack number %d\nWindow number %d", stack_number, win_number);
72     label = gtk_label_new (text);
73     g_free (text);
74
75     hbbox = gtk_hbutton_box_new ();
76
77     /* Button to push a window to the current stack */
78     add = hildon_gtk_button_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
79     gtk_button_set_label (GTK_BUTTON (add), "Add a window to this stack");
80     gtk_box_pack_start (GTK_BOX (hbbox), add, FALSE, FALSE, 0);
81     g_signal_connect (G_OBJECT (add), "clicked", G_CALLBACK (add_window), window);
82
83     /* Button to create a new stack */
84     new = hildon_gtk_button_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
85     gtk_button_set_label (GTK_BUTTON (new), "Add a window to a new stack");
86     gtk_box_pack_start (GTK_BOX (hbbox), new, FALSE, FALSE, 0);
87     g_signal_connect (G_OBJECT (new), "clicked", G_CALLBACK (add_window), NULL);
88
89     /* Spinbox and button to push many windows */
90     spin1hbox = gtk_hbox_new (FALSE, 0);
91     spin1label1 = gtk_label_new ("Push");
92     spin1 = gtk_spin_button_new (GTK_ADJUSTMENT (gtk_adjustment_new (2, 2, 5, 1, 1, 1)), 1, 0);
93     gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spin1), TRUE);
94     spin1label2 = gtk_label_new ("windows");
95     pushbtn = hildon_gtk_button_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
96     gtk_button_set_label (GTK_BUTTON (pushbtn), "Push windows");
97     gtk_box_pack_start (GTK_BOX (spin1hbox), spin1label1, FALSE, FALSE, 0);
98     gtk_box_pack_start (GTK_BOX (spin1hbox), spin1, FALSE, FALSE, 0);
99     gtk_box_pack_start (GTK_BOX (spin1hbox), spin1label2, FALSE, FALSE, 0);
100     gtk_box_pack_start (GTK_BOX (spin1hbox), pushbtn, FALSE, FALSE, 10);
101     align1 = gtk_alignment_new (0.5, 0.5, 0, 0);
102     gtk_container_add (GTK_CONTAINER (align1), spin1hbox);
103     g_signal_connect (G_OBJECT (pushbtn), "clicked", G_CALLBACK (push_windows), spin1);
104
105     /* Spinbox and button to pop many windows */
106     spin2hbox = gtk_hbox_new (FALSE, 0);
107     spin2label1 = gtk_label_new ("Pop");
108     spin2 = gtk_spin_button_new (GTK_ADJUSTMENT (gtk_adjustment_new (2, 2, 5, 1, 1, 1)), 1, 0);
109     gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spin2), TRUE);
110     spin2label2 = gtk_label_new ("windows");
111     popbtn = hildon_gtk_button_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
112     gtk_button_set_label (GTK_BUTTON (popbtn), "Pop windows");
113     gtk_box_pack_start (GTK_BOX (spin2hbox), spin2label1, FALSE, FALSE, 0);
114     gtk_box_pack_start (GTK_BOX (spin2hbox), spin2, FALSE, FALSE, 0);
115     gtk_box_pack_start (GTK_BOX (spin2hbox), spin2label2, FALSE, FALSE, 0);
116     gtk_box_pack_start (GTK_BOX (spin2hbox), popbtn, FALSE, FALSE, 10);
117     align2 = gtk_alignment_new (0.5, 0.5, 0, 0);
118     gtk_container_add (GTK_CONTAINER (align2), spin2hbox);
119     g_signal_connect (G_OBJECT (popbtn), "clicked", G_CALLBACK (pop_windows), spin2);
120
121     vbox = gtk_vbox_new (FALSE, 0);
122     gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
123     gtk_box_pack_start (GTK_BOX (vbox), hbbox, FALSE, FALSE, 10);
124     gtk_box_pack_start (GTK_BOX (vbox), align1, FALSE, FALSE, 10);
125     gtk_box_pack_start (GTK_BOX (vbox), align2, FALSE, FALSE, 10);
126
127     gtk_container_set_border_width (GTK_CONTAINER (window), 6);
128     gtk_container_add (GTK_CONTAINER (window), vbox);
129     gtk_widget_show_all (vbox);
130
131     return window;
132 }
133
134 static void
135 add_window                                      (GtkWidget             *button,
136                                                  HildonStackableWindow *parent)
137 {
138     HildonWindowStack *stack = NULL;
139     GtkWidget *window;
140
141     if (parent) {
142         stack = hildon_stackable_window_get_stack (parent);
143     } else {
144         stack = hildon_window_stack_new ();
145     }
146
147     window = new_window (parent);
148
149     if (!stack) {
150         stack = hildon_window_stack_get_default ();
151     }
152     hildon_window_stack_push_1 (stack, HILDON_STACKABLE_WINDOW (window));
153 }
154
155 static void
156 push_windows                                    (GtkWidget     *button,
157                                                  GtkSpinButton *spin)
158 {
159     GList *l = NULL;
160     HildonWindowStack *stack = NULL;
161     HildonStackableWindow *parent;
162     gint nwindows = gtk_spin_button_get_value_as_int (spin);
163
164     parent = HILDON_STACKABLE_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (spin)));
165     stack = hildon_stackable_window_get_stack (parent);
166
167     while (nwindows > 0) {
168         parent = HILDON_STACKABLE_WINDOW (new_window (parent));
169         l = g_list_append (l, parent);
170         nwindows--;
171     }
172     hildon_window_stack_push_list (stack, l);
173     g_list_free (l);
174 }
175
176 static void
177 pop_windows                                     (GtkWidget     *button,
178                                                  GtkSpinButton *spin)
179 {
180     HildonWindowStack *stack = NULL;
181     HildonStackableWindow *win;
182     gint nwindows = gtk_spin_button_get_value_as_int (spin);
183
184     win = HILDON_STACKABLE_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (spin)));
185     stack = hildon_stackable_window_get_stack (win);
186
187     hildon_window_stack_pop (stack, nwindows, NULL);
188 }
189
190 int
191 main                                            (int    argc,
192                                                  char **argv)
193 {
194     GtkWidget *window;
195
196     hildon_gtk_init (&argc, &argv);
197
198     g_set_application_name ("hildon-stackable-window-example");
199
200     window = new_window (NULL);
201
202     g_signal_connect (G_OBJECT (window), "destroy",
203                       G_CALLBACK (gtk_main_quit), NULL);
204
205     gtk_widget_show (window);
206
207     gtk_main ();
208
209     return 0;
210 }