* modest-header-view.[ch]:
[modest] / src / widgets / modest-toolbar.c
1 /* Copyright (c) 2006, 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 <glib/gi18n.h>
31 #include "modest-toolbar.h"
32
33 /* 'private'/'protected' functions */
34 static void modest_toolbar_class_init (ModestToolbarClass *klass);
35 static void modest_toolbar_init       (ModestToolbar *obj);
36 static void modest_toolbar_finalize   (GObject *obj);
37
38 static void on_toolbutton_clicked (GtkToolButton *button, ModestToolbar *self);
39
40 /* list my signals  */
41 enum {
42         BUTTON_CLICKED_SIGNAL,
43         LAST_SIGNAL
44 };
45
46 typedef struct _ModestToolbarPrivate ModestToolbarPrivate;
47 struct _ModestToolbarPrivate {
48         GtkTooltips     *tooltips;
49
50 };
51 #define MODEST_TOOLBAR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
52                                             MODEST_TYPE_TOOLBAR, \
53                                             ModestToolbarPrivate))
54 /* globals */
55 static GtkToolbarClass *parent_class = NULL;
56
57 static guint signals[LAST_SIGNAL] = {0};
58
59 GType
60 modest_toolbar_get_type (void)
61 {
62         static GType my_type = 0;
63         if (!my_type) {
64                 static const GTypeInfo my_info = {
65                         sizeof(ModestToolbarClass),
66                         NULL,           /* base init */
67                         NULL,           /* base finalize */
68                         (GClassInitFunc) modest_toolbar_class_init,
69                         NULL,           /* class finalize */
70                         NULL,           /* class data */
71                         sizeof(ModestToolbar),
72                         1,              /* n_preallocs */
73                         (GInstanceInitFunc) modest_toolbar_init,
74                         NULL
75                 };
76                 my_type = g_type_register_static (GTK_TYPE_TOOLBAR,
77                                                   "ModestToolbar",
78                                                   &my_info, 0);
79         }
80         return my_type;
81 }
82
83 static void
84 modest_toolbar_class_init (ModestToolbarClass *klass)
85 {
86         GObjectClass *gobject_class;
87         gobject_class = (GObjectClass*) klass;
88
89         parent_class            = g_type_class_peek_parent (klass);
90         gobject_class->finalize = modest_toolbar_finalize;
91
92         g_type_class_add_private (gobject_class, sizeof(ModestToolbarPrivate));
93
94         signals[BUTTON_CLICKED_SIGNAL] = 
95                 g_signal_new ("button_clicked",
96                               G_TYPE_FROM_CLASS (gobject_class),
97                               G_SIGNAL_RUN_FIRST,
98                               G_STRUCT_OFFSET (ModestToolbarClass, button_clicked),
99                               NULL, NULL,
100                               g_cclosure_marshal_VOID__INT,
101                               G_TYPE_NONE, 1, G_TYPE_INT);
102 }
103
104 static void
105 modest_toolbar_init (ModestToolbar *obj)
106 {
107         ModestToolbarPrivate *priv;
108         priv = MODEST_TOOLBAR_GET_PRIVATE(obj);
109
110         priv->tooltips = NULL;
111         
112 }
113
114 static void
115 modest_toolbar_finalize (GObject *obj)
116 {
117         ModestToolbarPrivate *priv;
118         priv = MODEST_TOOLBAR_GET_PRIVATE(obj);
119
120         if (priv->tooltips) {
121                 g_object_ref_sink (G_OBJECT(priv->tooltips));
122                 priv->tooltips = NULL;
123         }
124
125         G_OBJECT_CLASS(parent_class)->finalize (obj);
126 }
127
128
129 /* don't free icon_name/label/tooltip, they're static */ 
130 static gboolean
131 data_for_button_id (ModestToolbarButton button_id,
132                     gchar **icon_name, gchar **label, gchar **tooltip)
133 {
134         switch (button_id) {
135         case MODEST_TOOLBAR_BUTTON_MAIL_SEND:
136                 *label     = _("Send");
137                 *tooltip   = _("Send the current email message");
138                 *icon_name = MODEST_TOOLBAR_ICON_MAIL_SEND;
139                 break;
140         case MODEST_TOOLBAR_BUTTON_NEW_MAIL:
141                 *label     = _("New mail");
142                 *tooltip   = _("Compose a new email message");
143                 *icon_name = MODEST_TOOLBAR_ICON_NEW_MAIL;
144                 break;
145         case MODEST_TOOLBAR_BUTTON_SEND_RECEIVE:
146                 *label     = _("Send/Receive");
147                 *tooltip   = _("Send and receive messages");
148                 *icon_name = MODEST_TOOLBAR_ICON_SEND_RECEIVE;
149                 break;
150         case MODEST_TOOLBAR_BUTTON_REPLY:
151                 *label     = _("Reply");
152                 *tooltip   = _("Reply to the selected email message");
153                 *icon_name = MODEST_TOOLBAR_ICON_REPLY;
154                 break;
155         case MODEST_TOOLBAR_BUTTON_REPLY_ALL:
156                 *label    = _("Reply all");
157                 *tooltip   = _("Reply to all people the selected email was sent to");
158                 *icon_name = MODEST_TOOLBAR_ICON_REPLY_ALL;
159                 break;
160         case MODEST_TOOLBAR_BUTTON_FORWARD:
161                 *label     = _("Forward");
162                 *tooltip   = _("Forward the selected email");
163                 *icon_name = MODEST_TOOLBAR_ICON_FORWARD;
164                 break;
165         case MODEST_TOOLBAR_BUTTON_DELETE:
166                 *label     = _("Delete");
167                 *tooltip   = _("Delete the selected email message(s)");
168                 *icon_name = MODEST_TOOLBAR_ICON_DELETE;
169                 break;
170         case MODEST_TOOLBAR_BUTTON_NEXT:
171                 *label     = _("Next");
172                 *tooltip   = _("Move to the next message");
173                 *icon_name = MODEST_TOOLBAR_ICON_NEXT;
174                 break;
175         case MODEST_TOOLBAR_BUTTON_PREV:
176                 *label    = _("Previous");
177                 *tooltip   = _("Move to the previous message");
178                 *icon_name = MODEST_TOOLBAR_ICON_PREV;  
179                 break;
180         case MODEST_TOOLBAR_BUTTON_STOP:
181                 *label    = _("Stop");
182                 *tooltip  = _("Stop whatever"); 
183                 *icon_name = MODEST_TOOLBAR_ICON_STOP;
184                 break;
185         default:
186                 g_printerr ("modest: not a valid button id: %d\n", 
187                             button_id);
188                 return FALSE;
189         }
190         return TRUE;
191 }
192
193
194 static gboolean 
195 modest_toolbar_set_buttons (ModestToolbar *self, const GSList *buttons)
196 {
197         const GSList *cursor;
198         ModestToolbarPrivate *priv;
199
200         priv = MODEST_TOOLBAR_GET_PRIVATE(self);
201         
202         priv->tooltips = gtk_tooltips_new ();
203         gtk_tooltips_enable (priv->tooltips);
204         gtk_toolbar_set_tooltips (GTK_TOOLBAR(self), TRUE);
205
206         cursor = buttons;
207         while (cursor) {
208                 ModestToolbarButton button_id =
209                         (ModestToolbarButton) GPOINTER_TO_INT(cursor->data);
210                 
211                 if (button_id == MODEST_TOOLBAR_SEPARATOR)
212                         gtk_toolbar_insert (GTK_TOOLBAR(self), 
213                                             gtk_separator_tool_item_new(), -1);
214                 else {
215                         gchar *icon_name, *label, *tooltip; /* don't free these */
216                         if (!data_for_button_id (button_id, &icon_name, &label, &tooltip))
217                                 g_printerr ("modest: error getting data for toolbar button %d\n",
218                                             button_id);
219                         else {
220                                 GtkWidget *icon = NULL;
221                                 GtkToolItem *button = NULL;
222                                 GdkPixbuf *pixbuf = NULL;
223                                 
224                                 pixbuf = modest_icon_factory_get_icon_at_size (icon_name, 24, 24);
225                                 if (pixbuf)
226                                         icon   = gtk_image_new_from_pixbuf ((GdkPixbuf*)pixbuf);
227                                 button = gtk_tool_button_new (icon, label);
228                                 g_object_set_data (G_OBJECT(button), "button_id",
229                                                    GINT_TO_POINTER(button_id));
230                                 g_signal_connect (G_OBJECT(button), "clicked", 
231                                                   G_CALLBACK(on_toolbutton_clicked), self);
232
233                                 gtk_tooltips_set_tip (priv->tooltips, GTK_WIDGET(button),
234                                                       tooltip, NULL);
235                                 gtk_widget_show_all (GTK_WIDGET(button));
236                                 gtk_toolbar_insert (GTK_TOOLBAR(self), button, -1);
237                         }
238                 }
239                 cursor = cursor->next;
240         }
241         
242         return TRUE; /* FIXME */   
243 }       
244
245
246 ModestToolbar*
247 modest_toolbar_new (const GSList *buttons)
248 {
249         GObject *obj;
250
251         obj = g_object_new(MODEST_TYPE_TOOLBAR, NULL);
252         modest_toolbar_set_buttons (MODEST_TOOLBAR(obj), buttons);
253         
254         return MODEST_TOOLBAR(obj);
255 }
256
257 static void
258 on_toolbutton_clicked (GtkToolButton *button, ModestToolbar *self)
259 {
260         gint button_id = GPOINTER_TO_INT(
261                 g_object_get_data(G_OBJECT(button), "button_id"));
262
263         g_signal_emit (G_OBJECT(self), signals[BUTTON_CLICKED_SIGNAL],
264                        0, button_id);
265 }
266
267