65783123e6253be5dd8c11260c96c0c511bd8795
[modest] / src / gtk / modest-shell.c
1 /* Copyright (c) 2009, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <string.h>
31 #include <modest-shell.h>
32
33 /* 'private'/'protected' functions */
34 static void modest_shell_class_init (ModestShellClass *klass);
35 static void modest_shell_instance_init (ModestShell *obj);
36 static void modest_shell_finalize   (GObject *obj);
37
38
39 typedef struct _ModestShellPrivate ModestShellPrivate;
40 struct _ModestShellPrivate {
41         GtkWidget *main_vbox;
42         GtkWidget *notebook;
43 };
44 #define MODEST_SHELL_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
45                                                                       MODEST_TYPE_SHELL, \
46                                                                       ModestShellPrivate))
47 /* globals */
48 static GObjectClass *parent_class = NULL;
49
50 GType
51 modest_shell_get_type (void)
52 {
53         static GType my_type = 0;
54         if (!my_type) {
55                 static const GTypeInfo my_info = {
56                         sizeof(ModestShellClass),
57                         NULL,           /* base init */
58                         NULL,           /* base finalize */
59                         (GClassInitFunc) modest_shell_class_init,
60                         NULL,           /* class finalize */
61                         NULL,           /* class data */
62                         sizeof(ModestShell),
63                         1,              /* n_preallocs */
64                         (GInstanceInitFunc) modest_shell_instance_init,
65                         NULL
66                 };
67                 my_type = g_type_register_static (GTK_TYPE_WINDOW,
68                                                   "ModestShell",
69                                                   &my_info, 0);
70         }
71         return my_type;
72 }
73
74 static void
75 modest_shell_class_init (ModestShellClass *klass)
76 {
77         GObjectClass *gobject_class;
78
79         gobject_class = (GObjectClass*) klass;
80
81         parent_class            = g_type_class_peek_parent (klass);
82         gobject_class->finalize = modest_shell_finalize;
83
84         g_type_class_add_private (gobject_class, sizeof(ModestShellPrivate));
85
86 }
87
88 static void
89 modest_shell_instance_init (ModestShell *obj)
90 {
91         ModestShellPrivate *priv;
92
93         priv = MODEST_SHELL_GET_PRIVATE(obj);
94
95         priv->main_vbox = gtk_vbox_new (FALSE, 0);
96         gtk_widget_show (priv->main_vbox);
97
98         priv->notebook = gtk_notebook_new ();
99         gtk_notebook_set_show_tabs (GTK_NOTEBOOK (priv->notebook), FALSE);
100         gtk_widget_show (priv->notebook);
101         gtk_box_pack_start (GTK_BOX (priv->main_vbox), priv->notebook, TRUE, TRUE, 0);
102
103 }
104
105 static void
106 modest_shell_finalize (GObject *obj)
107 {
108         G_OBJECT_CLASS(parent_class)->finalize (obj);
109 }
110
111 GtkWidget*
112 modest_shell_new (void)
113 {
114         return (GtkWidget *) g_object_new(MODEST_TYPE_SHELL, NULL);
115 }
116
117 ModestWindow *
118 modest_shell_peek_window (ModestShell *shell)
119 {
120         ModestShellPrivate *priv;
121         gint count;
122
123         priv = MODEST_SHELL_GET_PRIVATE (shell);
124         count = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
125
126         if (count > 0) {
127                 return (ModestWindow *) gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), count - 1);
128         } else {
129                 return NULL;
130         }
131 }
132
133 gboolean
134 modest_shell_delete_window (ModestShell *shell, ModestWindow *window)
135 {
136         ModestShellPrivate *priv;
137         gboolean ret_value;
138
139         priv = MODEST_SHELL_GET_PRIVATE (shell);
140         g_signal_emit_by_name (G_OBJECT (window), "delete-event", NULL, &ret_value);
141         if (ret_value == FALSE) {
142                 gint page_num;
143                 
144                 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook), GTK_WIDGET (window));
145                 if (page_num != -1) {
146                         gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), page_num);
147                 }
148         }
149
150         return ret_value;
151 }
152
153 void
154 modest_shell_add_window (ModestShell *shell, ModestWindow *window)
155 {
156         ModestShellPrivate *priv;
157
158         priv = MODEST_SHELL_GET_PRIVATE (shell);
159         gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook), GTK_WIDGET (window), NULL);
160 }
161
162 gint
163 modest_shell_count_windows (ModestShell *shell)
164 {
165         ModestShellPrivate *priv;
166
167         priv = MODEST_SHELL_GET_PRIVATE (shell);
168
169         return gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
170 }