2008-08-06 Claudio Saavedra <csaavedra@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  * 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 #include                                        <X11/X.h>
32 #include                                        <X11/Xlib.h>
33 #include                                        <X11/Xatom.h>
34
35 static void
36 add_window                                      (GtkWidget* w);
37
38 static void
39 detach_window                                   (GtkWidget* w)
40 {
41     HildonProgram *program = hildon_program_get_instance ();
42     hildon_program_remove_window (program, HILDON_WINDOW (w));
43 }
44
45 static GtkWidget*
46 new_window                                      (gboolean ismain)
47 {
48     GtkWidget *window, *hbbox, *add;
49     static int count = 0;
50     gchar* title;
51
52     window = hildon_stackable_window_new ();
53
54     if (count == 0)
55         title = g_strdup ("main window");
56     else
57         title = g_strdup_printf ("win%d", count);
58
59     gtk_window_set_title (GTK_WINDOW (window), title);
60     g_free (title);
61
62     count++;
63
64     gtk_container_set_border_width (GTK_CONTAINER (window), 6);
65
66     hbbox = gtk_hbutton_box_new ();
67     gtk_container_add (GTK_CONTAINER (window), hbbox);
68
69     add = gtk_button_new_with_label ("Add a window");
70     gtk_box_pack_start (GTK_BOX (hbbox), add, FALSE, FALSE, 0);
71
72     g_signal_connect (G_OBJECT (add), "clicked", G_CALLBACK (add_window), NULL);
73
74     if (!ismain)
75     {
76         GtkWidget *detach, *back;
77         detach = GTK_WIDGET (gtk_button_new_with_label ("Detach"));
78         gtk_box_pack_end (GTK_BOX (hbbox), detach, FALSE, FALSE, 0);
79
80         g_signal_connect_swapped (G_OBJECT (detach), "clicked",
81                                   G_CALLBACK (detach_window),
82                                   HILDON_STACKABLE_WINDOW (window));
83
84         back = GTK_WIDGET (gtk_button_new_with_label ("Back to root"));
85         gtk_box_pack_end (GTK_BOX (hbbox), back, FALSE, FALSE, 0);
86
87         g_signal_connect_swapped (G_OBJECT (back), "clicked",
88                                   G_CALLBACK (hildon_program_go_to_root_window),
89                                   hildon_program_get_instance ());
90     }
91
92     return window;
93 }
94
95 static void
96 add_window                                      (GtkWidget *w)
97 {
98     GtkWidget *window = new_window (FALSE);
99     HildonProgram *program = hildon_program_get_instance ();
100
101     hildon_program_add_window (program, HILDON_WINDOW (window));
102
103     gtk_widget_show_all (window);
104
105     return;
106 }
107
108 int
109 main                                            (int argc,
110                                                  char **args)
111 {
112     HildonProgram *program;
113     GtkWidget *window;
114
115     gtk_init (&argc, &args);
116
117     g_set_application_name ("stack");
118
119     program = hildon_program_get_instance ();
120     window = new_window (TRUE);
121     hildon_program_add_window (program, HILDON_WINDOW (window));
122
123     g_signal_connect (G_OBJECT (window), "delete_event",
124                       G_CALLBACK (gtk_main_quit), NULL);
125
126     gtk_widget_show_all (GTK_WIDGET (window));
127
128     gtk_main ();
129
130     return 0;
131 }