89220b3d870c8200fe887e198a9b993f4aba03fc
[claws.git] / src / gtk / menu.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27 #include <gtk/gtkwidget.h>
28 #include <gtk/gtkmenu.h>
29 #include <gtk/gtkmenubar.h>
30 #include <gtk/gtkcheckmenuitem.h>
31 #include <gtk/gtkitemfactory.h>
32 #include <gtk/gtkbutton.h>
33 #include <gtk/gtkwindow.h>
34 #include <gtk/gtkutils.h>
35
36 #include "menu.h"
37 #include "utils.h"
38 #include "gtkutils.h"
39
40 #ifdef MAEMO
41 #ifdef CHINOOK
42 #include <hildon/hildon-program.h>
43 #else
44 #include <hildon-widgets/hildon-program.h>
45 #endif
46 #include <gtk/gtkmain.h>
47 #endif
48
49 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2) ;
50
51
52 GtkWidget *menubar_create(GtkWidget *window, GtkItemFactoryEntry *entries,
53                           guint n_entries, const gchar *path, gpointer data)
54 {
55         GtkItemFactory *factory;
56         GtkWidget *menubar;
57         
58 #ifdef MAEMO
59         factory = gtk_item_factory_new(GTK_TYPE_MENU, path, NULL);
60 #else
61         factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, path, NULL);
62 #endif
63         gtk_item_factory_set_translate_func(factory, menu_translate,
64                                             NULL, NULL);
65         gtk_item_factory_create_items(factory, n_entries, entries, data);
66         gtk_window_add_accel_group (GTK_WINDOW (window), factory->accel_group);
67
68         menubar  = gtk_item_factory_get_widget(factory, path);
69 #ifdef MAEMO
70         hildon_window_set_menu(HILDON_WINDOW(window), GTK_MENU(menubar));
71 #endif
72         return menubar;
73 }
74
75 GtkWidget *menu_create_items(GtkItemFactoryEntry *entries,
76                              guint n_entries, const gchar *path,
77                              GtkItemFactory **factory, gpointer data)
78 {
79         *factory = gtk_item_factory_new(GTK_TYPE_MENU, path, NULL);
80         gtk_item_factory_set_translate_func(*factory, menu_translate,
81                                             NULL, NULL);
82         gtk_item_factory_create_items(*factory, n_entries, entries, data);
83
84         return gtk_item_factory_get_widget(*factory, path);
85 }
86
87 GtkActionGroup *cm_menu_create_action_group(const gchar *name, GtkActionEntry *entries,
88                                             gint num_entries, gpointer data)
89 {
90         GtkActionGroup *group = gtk_action_group_new(name);
91         gtk_action_group_set_translate_func(group, menu_translate, NULL, NULL);
92         gtk_action_group_add_actions(group, entries, num_entries, data);
93         gtk_ui_manager_insert_action_group(gtkut_ui_manager(), group, 0);
94         return group;
95 }
96
97 GtkActionGroup *cm_menu_create_action_group_full(GtkUIManager *manager, const gchar *name, GtkActionEntry *entries,
98                                             gint num_entries, gpointer data)
99 {
100         GtkActionGroup *group = gtk_action_group_new(name);
101         gtk_action_group_set_translate_func(group, menu_translate, NULL, NULL);
102         gtk_action_group_add_actions(group, entries, num_entries, data);
103         gtk_ui_manager_insert_action_group(manager, group, 0);
104         return group;
105 }
106
107 gchar *menu_translate(const gchar *path, gpointer data)
108 {
109         gchar *retval;
110
111         retval = gettext(path);
112
113         return retval;
114 }
115
116 void menu_set_sensitive(GtkItemFactory *ifactory, const gchar *path,
117                         gboolean sensitive)
118 {
119         GtkWidget *widget;
120
121         g_return_if_fail(ifactory != NULL);
122
123         widget = gtk_item_factory_get_item(ifactory, path);
124         g_return_if_fail(widget != NULL);
125
126         gtk_widget_set_sensitive(widget, sensitive);
127 }
128
129 void cm_menu_set_sensitive(gchar *menu, gboolean sensitive)
130 {
131         GtkUIManager *gui_manager = gtkut_ui_manager();
132         GtkWidget *widget;
133         gchar *path = g_strdup_printf("Menus/%s", menu);
134
135         cm_menu_set_sensitive_full(gui_manager, path, sensitive);
136         g_free(path);
137 }
138
139 void cm_toggle_menu_set_active(gchar *menu, gboolean active)
140 {
141         GtkUIManager *gui_manager = gtkut_ui_manager();
142         GtkWidget *widget;
143         gchar *path = g_strdup_printf("Menus/%s", menu);
144
145         cm_toggle_menu_set_active_full(gui_manager, path, active);
146         g_free(path);
147 }
148
149 void cm_menu_set_sensitive_full(GtkUIManager *gui_manager, gchar *menu, gboolean sensitive)
150 {
151         GtkWidget *widget;
152         gchar *path = g_strdup_printf("/%s/", menu);
153
154         widget = gtk_ui_manager_get_widget(gui_manager, path);
155         if( !GTK_IS_WIDGET(widget) ) {
156                 g_message("Blah, '%s' is not a widget.\n", path);
157         }
158
159         if( !GTK_IS_MENU_ITEM(widget) ) {
160                 g_message("Blah, '%s' is not a menu item.\n", path);
161         }
162
163         gtk_widget_set_sensitive(widget, sensitive);
164         g_free(path);
165 }
166
167 void cm_toggle_menu_set_active_full(GtkUIManager *gui_manager, gchar *menu, gboolean active)
168 {
169         GtkWidget *widget;
170         gchar *path = g_strdup_printf("/%s/", menu);
171
172         widget = gtk_ui_manager_get_widget(gui_manager, path);
173         if( !GTK_IS_WIDGET(widget) ) {
174                 g_message("Blah, '%s' is not a widget.\n", path);
175         }
176
177         if( !GTK_CHECK_MENU_ITEM(widget) ) {
178                 g_message("Blah, '%s' is not a check menu item.\n", path);
179         }
180
181         gtk_check_menu_item_set_active(widget, active);
182         g_free(path);
183 }
184
185 void menu_set_sensitive_all(GtkMenuShell *menu_shell, gboolean sensitive)
186 {
187         GList *cur;
188
189         for (cur = menu_shell->children; cur != NULL; cur = cur->next)
190                 gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
191 }
192
193 void menu_set_active(GtkItemFactory *ifactory, const gchar *path,
194                      gboolean is_active)
195 {
196         GtkWidget *widget;
197
198         g_return_if_fail(ifactory != NULL);
199
200         widget = gtk_item_factory_get_item(ifactory, path);
201         g_return_if_fail(widget != NULL);
202
203         if (!GTK_IS_CHECK_MENU_ITEM(widget)) {
204                 debug_print("%s not check_menu_item\n", path?path:"(null)");
205                 return;
206         }       
207         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), is_active);
208 }
209
210 void menu_button_position(GtkMenu *menu, gint *x, gint *y, gboolean *push_in,
211                           gpointer user_data)
212 {
213         GtkWidget *widget;
214         gint wheight;
215         gint wx, wy;
216         GtkRequisition mreq;
217         GdkScreen *screen;
218         GdkRectangle monitor;
219         gint monitor_num;
220
221         g_return_if_fail(x && y);
222         g_return_if_fail(GTK_IS_BUTTON(user_data));
223
224         widget = GTK_WIDGET(user_data);
225
226         gdk_window_get_origin(widget->window, x, y);
227         wheight = widget->requisition.height;
228         wx = widget->allocation.x;
229         wy = widget->allocation.y;
230         
231         gtk_widget_size_request(GTK_WIDGET(menu), &mreq);
232         screen = gtk_widget_get_screen (widget);
233         monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
234         gdk_screen_get_monitor_geometry (screen, monitor_num, 
235                                          &monitor);
236
237         *x = *x + wx;
238         *y = *y + wy + wheight;
239         
240         if (*y + mreq.height >= monitor.height)
241                 *y -= mreq.height;
242 }
243
244 gint menu_find_option_menu_index(GtkOptionMenu *optmenu, gpointer data,
245                                  GCompareFunc func)
246 {
247         GtkWidget *menu;
248         GtkWidget *menuitem;
249         gpointer menu_data;
250         GList *cur;
251         gint n;
252
253         menu = gtk_option_menu_get_menu(optmenu);
254
255         for (cur = GTK_MENU_SHELL(menu)->children, n = 0;
256              cur != NULL; cur = cur->next, n++) {
257                 menuitem = GTK_WIDGET(cur->data);
258                 menu_data = g_object_get_data(G_OBJECT(menuitem),
259                                               MENU_VAL_ID);
260                 if (func) {
261                         if (func(menu_data, data) == 0)
262                                 return n;
263                 } else if (menu_data == data)
264                         return n;
265         }
266
267         return -1;
268 }
269
270 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2) 
271 {
272 #if 0
273         g_signal_connect_after(G_OBJECT(widget), "add_accelerator", 
274                                G_CALLBACK(menu_item_add_accel), wid2);
275         g_signal_connect_after(G_OBJECT(widget), "remove_accelerator", 
276                                G_CALLBACK(menu_item_remove_accel), wid2);
277 #endif
278 }
279
280 void menu_connect_identical_items(void)
281 {
282         gint n;
283         GtkWidget *item1;
284         GtkWidget *item2;
285
286         static const struct {   
287                 const gchar *path1;
288                 const gchar *path2;
289         } pairs[] = {
290                 {"<Main>/Message/Reply",                        "<SummaryView>/Reply"},
291 #ifndef GENERIC_UMPC
292                 {"<Main>/Message/Reply to/all",                 "<SummaryView>/Reply to/all"},
293                 {"<Main>/Message/Reply to/sender",              "<SummaryView>/Reply to/sender"},
294                 {"<Main>/Message/Reply to/mailing list",        "<SummaryView>/Reply to/mailing list"},
295 #endif
296                 {"<Main>/Message/Forward",                      "<SummaryView>/Forward"},
297 #ifndef GENERIC_UMPC
298                 {"<Main>/Message/Redirect",                     "<SummaryView>/Redirect"},
299 #endif
300                 {"<Main>/Message/Move...",                      "<SummaryView>/Move..."},
301                 {"<Main>/Message/Copy...",                      "<SummaryView>/Copy..."},
302 #ifndef GENERIC_UMPC
303                 {"<Main>/Message/Delete...",                    "<SummaryView>/Delete..."},
304 #endif
305                 {"<Main>/Message/Mark/Mark",                    "<SummaryView>/Mark/Mark"},
306                 {"<Main>/Message/Mark/Unmark",                  "<SummaryView>/Mark/Unmark"},
307                 {"<Main>/Message/Mark/Mark as unread",          "<SummaryView>/Mark/Mark as unread"},
308                 {"<Main>/Message/Mark/Mark as read",            "<SummaryView>/Mark/Mark as read"},
309                 {"<Main>/Message/Mark/Mark all read",           "<SummaryView>/Mark/Mark all read"},
310 #ifndef GENERIC_UMPC
311                 {"<Main>/Tools/Add sender to address book",     "<SummaryView>/Add sender to address book"},
312 #endif
313                 {"<Main>/Tools/Create filter rule/Automatically",       
314                                                                 "<SummaryView>/Create filter rule/Automatically"},
315                 {"<Main>/Tools/Create filter rule/by From",     "<SummaryView>/Create filter rule/by From"},
316                 {"<Main>/Tools/Create filter rule/by To",       "<SummaryView>/Create filter rule/by To"},
317                 {"<Main>/Tools/Create filter rule/by Subject",  "<SummaryView>/Create filter rule/by Subject"},
318 #ifndef GENERIC_UMPC
319                 {"<Main>/Tools/Create processing rule/Automatically",
320                                                                 "<SummaryView>/Create processing rule/Automatically"},
321                 {"<Main>/Tools/Create processing rule/by From", "<SummaryView>/Create processing rule/by From"},
322                 {"<Main>/Tools/Create processing rule/by To",   "<SummaryView>/Create processing rule/by To"},
323                 {"<Main>/Tools/Create processing rule/by Subject",
324                                                                 "<SummaryView>/Create processing rule/by Subject"},
325 #endif
326                 {"<Main>/View/Open in new window",              "<SummaryView>/View/Open in new window"},
327                 {"<Main>/View/Message source",                  "<SummaryView>/View/Message source"},
328 #ifndef GENERIC_UMPC
329                 {"<Main>/View/All headers",                     "<SummaryView>/View/All headers"},
330 #endif
331         };
332
333         const gint numpairs = sizeof pairs / sizeof pairs[0];
334         for (n = 0; n < numpairs; n++) {
335                 /* get widgets from the paths */
336
337                 item1 = gtk_item_factory_get_widget
338                                 (gtk_item_factory_from_path(pairs[n].path1),pairs[n].path1);            
339                 item2 = gtk_item_factory_get_widget
340                                 (gtk_item_factory_from_path(pairs[n].path2),pairs[n].path2);            
341
342                 if (item1 && item2) {
343                         /* connect widgets both ways around */
344                         connect_accel_change_signals(item2,item1);
345                         connect_accel_change_signals(item1,item2);
346                 } else { 
347                         if (!item1) debug_print(" ** Menu item not found: %s\n",pairs[n].path1);
348                         if (!item2) debug_print(" ** Menu item not found: %s\n",pairs[n].path2);
349                 }                               
350         }
351 }