2008-07-26 [colin] 3.5.0cvs35
[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 gchar *menu_translate(const gchar *path, gpointer data)
98 {
99         gchar *retval;
100
101         retval = gettext(path);
102
103         return retval;
104 }
105
106 void menu_set_sensitive(GtkItemFactory *ifactory, const gchar *path,
107                         gboolean sensitive)
108 {
109         GtkWidget *widget;
110
111         g_return_if_fail(ifactory != NULL);
112
113         widget = gtk_item_factory_get_item(ifactory, path);
114         g_return_if_fail(widget != NULL);
115
116         gtk_widget_set_sensitive(widget, sensitive);
117 }
118
119 void cm_menu_set_sensitive(gchar *menu, gboolean sensitive)
120 {
121         GtkUIManager *gui_manager = gtkut_ui_manager();
122         GtkWidget *widget;
123         gchar *path = g_strdup_printf("/Menus/%s/", menu);
124
125         widget = gtk_ui_manager_get_widget(gui_manager, path);
126         if( !GTK_IS_WIDGET(widget) ) {
127                 g_message("Blah, '%s' is not a widget.\n", path);
128         }
129
130         if( !GTK_IS_MENU_ITEM(widget) ) {
131                 g_message("Blah, '%s' is not a menu item.\n", path);
132         }
133
134         gtk_widget_set_sensitive(widget, sensitive);
135         g_free(path);
136 }
137
138 void cm_toggle_menu_set_active(gchar *menu, gboolean active)
139 {
140         GtkUIManager *gui_manager = gtkut_ui_manager();
141         GtkWidget *widget;
142         gchar *path = g_strdup_printf("/Menus/%s/", menu);
143
144         widget = gtk_ui_manager_get_widget(gui_manager, path);
145         if( !GTK_IS_WIDGET(widget) ) {
146                 g_message("Blah, '%s' is not a widget.\n", path);
147         }
148
149         if( !GTK_CHECK_MENU_ITEM(widget) ) {
150                 g_message("Blah, '%s' is not a check menu item.\n", path);
151         }
152
153         gtk_check_menu_item_set_active(widget, active);
154         g_free(path);
155 }
156
157 void menu_set_sensitive_all(GtkMenuShell *menu_shell, gboolean sensitive)
158 {
159         GList *cur;
160
161         for (cur = menu_shell->children; cur != NULL; cur = cur->next)
162                 gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
163 }
164
165 void menu_set_active(GtkItemFactory *ifactory, const gchar *path,
166                      gboolean is_active)
167 {
168         GtkWidget *widget;
169
170         g_return_if_fail(ifactory != NULL);
171
172         widget = gtk_item_factory_get_item(ifactory, path);
173         g_return_if_fail(widget != NULL);
174
175         if (!GTK_IS_CHECK_MENU_ITEM(widget)) {
176                 debug_print("%s not check_menu_item\n", path?path:"(null)");
177                 return;
178         }       
179         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), is_active);
180 }
181
182 void menu_button_position(GtkMenu *menu, gint *x, gint *y, gboolean *push_in,
183                           gpointer user_data)
184 {
185         GtkWidget *widget;
186         gint wheight;
187         gint wx, wy;
188         GtkRequisition mreq;
189         GdkScreen *screen;
190         GdkRectangle monitor;
191         gint monitor_num;
192
193         g_return_if_fail(x && y);
194         g_return_if_fail(GTK_IS_BUTTON(user_data));
195
196         widget = GTK_WIDGET(user_data);
197
198         gdk_window_get_origin(widget->window, x, y);
199         wheight = widget->requisition.height;
200         wx = widget->allocation.x;
201         wy = widget->allocation.y;
202         
203         gtk_widget_size_request(GTK_WIDGET(menu), &mreq);
204         screen = gtk_widget_get_screen (widget);
205         monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
206         gdk_screen_get_monitor_geometry (screen, monitor_num, 
207                                          &monitor);
208
209         *x = *x + wx;
210         *y = *y + wy + wheight;
211         
212         if (*y + mreq.height >= monitor.height)
213                 *y -= mreq.height;
214 }
215
216 gint menu_find_option_menu_index(GtkOptionMenu *optmenu, gpointer data,
217                                  GCompareFunc func)
218 {
219         GtkWidget *menu;
220         GtkWidget *menuitem;
221         gpointer menu_data;
222         GList *cur;
223         gint n;
224
225         menu = gtk_option_menu_get_menu(optmenu);
226
227         for (cur = GTK_MENU_SHELL(menu)->children, n = 0;
228              cur != NULL; cur = cur->next, n++) {
229                 menuitem = GTK_WIDGET(cur->data);
230                 menu_data = g_object_get_data(G_OBJECT(menuitem),
231                                               MENU_VAL_ID);
232                 if (func) {
233                         if (func(menu_data, data) == 0)
234                                 return n;
235                 } else if (menu_data == data)
236                         return n;
237         }
238
239         return -1;
240 }
241
242 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2) 
243 {
244 #if 0
245         g_signal_connect_after(G_OBJECT(widget), "add_accelerator", 
246                                G_CALLBACK(menu_item_add_accel), wid2);
247         g_signal_connect_after(G_OBJECT(widget), "remove_accelerator", 
248                                G_CALLBACK(menu_item_remove_accel), wid2);
249 #endif
250 }
251
252 void menu_connect_identical_items(void)
253 {
254         gint n;
255         GtkWidget *item1;
256         GtkWidget *item2;
257
258         static const struct {   
259                 const gchar *path1;
260                 const gchar *path2;
261         } pairs[] = {
262                 {"<Main>/Message/Reply",                        "<SummaryView>/Reply"},
263 #ifndef GENERIC_UMPC
264                 {"<Main>/Message/Reply to/all",                 "<SummaryView>/Reply to/all"},
265                 {"<Main>/Message/Reply to/sender",              "<SummaryView>/Reply to/sender"},
266                 {"<Main>/Message/Reply to/mailing list",        "<SummaryView>/Reply to/mailing list"},
267 #endif
268                 {"<Main>/Message/Forward",                      "<SummaryView>/Forward"},
269 #ifndef GENERIC_UMPC
270                 {"<Main>/Message/Redirect",                     "<SummaryView>/Redirect"},
271 #endif
272                 {"<Main>/Message/Move...",                      "<SummaryView>/Move..."},
273                 {"<Main>/Message/Copy...",                      "<SummaryView>/Copy..."},
274 #ifndef GENERIC_UMPC
275                 {"<Main>/Message/Delete...",                    "<SummaryView>/Delete..."},
276 #endif
277                 {"<Main>/Message/Mark/Mark",                    "<SummaryView>/Mark/Mark"},
278                 {"<Main>/Message/Mark/Unmark",                  "<SummaryView>/Mark/Unmark"},
279                 {"<Main>/Message/Mark/Mark as unread",          "<SummaryView>/Mark/Mark as unread"},
280                 {"<Main>/Message/Mark/Mark as read",            "<SummaryView>/Mark/Mark as read"},
281                 {"<Main>/Message/Mark/Mark all read",           "<SummaryView>/Mark/Mark all read"},
282 #ifndef GENERIC_UMPC
283                 {"<Main>/Tools/Add sender to address book",     "<SummaryView>/Add sender to address book"},
284 #endif
285                 {"<Main>/Tools/Create filter rule/Automatically",       
286                                                                 "<SummaryView>/Create filter rule/Automatically"},
287                 {"<Main>/Tools/Create filter rule/by From",     "<SummaryView>/Create filter rule/by From"},
288                 {"<Main>/Tools/Create filter rule/by To",       "<SummaryView>/Create filter rule/by To"},
289                 {"<Main>/Tools/Create filter rule/by Subject",  "<SummaryView>/Create filter rule/by Subject"},
290 #ifndef GENERIC_UMPC
291                 {"<Main>/Tools/Create processing rule/Automatically",
292                                                                 "<SummaryView>/Create processing rule/Automatically"},
293                 {"<Main>/Tools/Create processing rule/by From", "<SummaryView>/Create processing rule/by From"},
294                 {"<Main>/Tools/Create processing rule/by To",   "<SummaryView>/Create processing rule/by To"},
295                 {"<Main>/Tools/Create processing rule/by Subject",
296                                                                 "<SummaryView>/Create processing rule/by Subject"},
297 #endif
298                 {"<Main>/View/Open in new window",              "<SummaryView>/View/Open in new window"},
299                 {"<Main>/View/Message source",                  "<SummaryView>/View/Message source"},
300 #ifndef GENERIC_UMPC
301                 {"<Main>/View/All headers",                     "<SummaryView>/View/All headers"},
302 #endif
303         };
304
305         const gint numpairs = sizeof pairs / sizeof pairs[0];
306         for (n = 0; n < numpairs; n++) {
307                 /* get widgets from the paths */
308
309                 item1 = gtk_item_factory_get_widget
310                                 (gtk_item_factory_from_path(pairs[n].path1),pairs[n].path1);            
311                 item2 = gtk_item_factory_get_widget
312                                 (gtk_item_factory_from_path(pairs[n].path2),pairs[n].path2);            
313
314                 if (item1 && item2) {
315                         /* connect widgets both ways around */
316                         connect_accel_change_signals(item2,item1);
317                         connect_accel_change_signals(item1,item2);
318                 } else { 
319                         if (!item1) debug_print(" ** Menu item not found: %s\n",pairs[n].path1);
320                         if (!item2) debug_print(" ** Menu item not found: %s\n",pairs[n].path2);
321                 }                               
322         }
323 }