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