7039f939f0e0a2b052409906fe20ea27c3df99b5
[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
35 #include "menu.h"
36 #include "utils.h"
37
38 #ifdef MAEMO
39 #ifdef CHINOOK
40 #include <hildon/hildon-program.h>
41 #else
42 #include <hildon-widgets/hildon-program.h>
43 #endif
44 #include <gtk/gtkmain.h>
45 #endif
46
47 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2) ;
48
49
50 GtkWidget *menubar_create(GtkWidget *window, GtkItemFactoryEntry *entries,
51                           guint n_entries, const gchar *path, gpointer data)
52 {
53         GtkItemFactory *factory;
54         GtkWidget *menubar;
55         
56 #ifdef MAEMO
57         factory = gtk_item_factory_new(GTK_TYPE_MENU, path, NULL);
58 #else
59         factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, path, NULL);
60 #endif
61         gtk_item_factory_set_translate_func(factory, menu_translate,
62                                             NULL, NULL);
63         gtk_item_factory_create_items(factory, n_entries, entries, data);
64         gtk_window_add_accel_group (GTK_WINDOW (window), factory->accel_group);
65
66         menubar  = gtk_item_factory_get_widget(factory, path);
67 #ifdef MAEMO
68         hildon_window_set_menu(HILDON_WINDOW(window), GTK_MENU(menubar));
69 #endif
70         return menubar;
71 }
72
73 GtkWidget *menu_create_items(GtkItemFactoryEntry *entries,
74                              guint n_entries, const gchar *path,
75                              GtkItemFactory **factory, gpointer data)
76 {
77         *factory = gtk_item_factory_new(GTK_TYPE_MENU, path, NULL);
78         gtk_item_factory_set_translate_func(*factory, menu_translate,
79                                             NULL, NULL);
80         gtk_item_factory_create_items(*factory, n_entries, entries, data);
81
82         return gtk_item_factory_get_widget(*factory, path);
83 }
84
85 gchar *menu_translate(const gchar *path, gpointer data)
86 {
87         gchar *retval;
88
89         retval = gettext(path);
90
91         return retval;
92 }
93
94 void menu_set_sensitive(GtkItemFactory *ifactory, const gchar *path,
95                         gboolean sensitive)
96 {
97         GtkWidget *widget;
98
99         g_return_if_fail(ifactory != NULL);
100
101         widget = gtk_item_factory_get_item(ifactory, path);
102         g_return_if_fail(widget != NULL);
103
104         gtk_widget_set_sensitive(widget, sensitive);
105 }
106
107 void menu_set_sensitive_all(GtkMenuShell *menu_shell, gboolean sensitive)
108 {
109         GList *cur;
110
111         for (cur = menu_shell->children; cur != NULL; cur = cur->next)
112                 gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
113 }
114
115 void menu_set_active(GtkItemFactory *ifactory, const gchar *path,
116                      gboolean is_active)
117 {
118         GtkWidget *widget;
119
120         g_return_if_fail(ifactory != NULL);
121
122         widget = gtk_item_factory_get_item(ifactory, path);
123         g_return_if_fail(widget != NULL);
124
125         if (!GTK_IS_CHECK_MENU_ITEM(widget)) {
126                 debug_print("%s not check_menu_item\n", path?path:"(null)");
127                 return;
128         }       
129         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), is_active);
130 }
131
132 void menu_button_position(GtkMenu *menu, gint *x, gint *y, gboolean *push_in,
133                           gpointer user_data)
134 {
135         GtkWidget *widget;
136         gint wheight;
137         gint wx, wy;
138         GtkRequisition mreq;
139         GdkScreen *screen;
140         GdkRectangle monitor;
141         gint monitor_num;
142
143         g_return_if_fail(x && y);
144         g_return_if_fail(GTK_IS_BUTTON(user_data));
145
146         widget = GTK_WIDGET(user_data);
147
148         gdk_window_get_origin(widget->window, x, y);
149         wheight = widget->requisition.height;
150         wx = widget->allocation.x;
151         wy = widget->allocation.y;
152         
153         gtk_widget_size_request(GTK_WIDGET(menu), &mreq);
154         screen = gtk_widget_get_screen (widget);
155         monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
156         gdk_screen_get_monitor_geometry (screen, monitor_num, 
157                                          &monitor);
158
159         *x = *x + wx;
160         *y = *y + wy + wheight;
161         
162         if (*y + mreq.height >= monitor.height)
163                 *y -= mreq.height;
164 }
165
166 gint menu_find_option_menu_index(GtkOptionMenu *optmenu, gpointer data,
167                                  GCompareFunc func)
168 {
169         GtkWidget *menu;
170         GtkWidget *menuitem;
171         gpointer menu_data;
172         GList *cur;
173         gint n;
174
175         menu = gtk_option_menu_get_menu(optmenu);
176
177         for (cur = GTK_MENU_SHELL(menu)->children, n = 0;
178              cur != NULL; cur = cur->next, n++) {
179                 menuitem = GTK_WIDGET(cur->data);
180                 menu_data = g_object_get_data(G_OBJECT(menuitem),
181                                               MENU_VAL_ID);
182                 if (func) {
183                         if (func(menu_data, data) == 0)
184                                 return n;
185                 } else if (menu_data == data)
186                         return n;
187         }
188
189         return -1;
190 }
191
192 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2) 
193 {
194 #if 0
195         g_signal_connect_after(G_OBJECT(widget), "add_accelerator", 
196                                G_CALLBACK(menu_item_add_accel), wid2);
197         g_signal_connect_after(G_OBJECT(widget), "remove_accelerator", 
198                                G_CALLBACK(menu_item_remove_accel), wid2);
199 #endif
200 }
201
202 void menu_connect_identical_items(void)
203 {
204         gint n;
205         GtkWidget *item1;
206         GtkWidget *item2;
207
208         static const struct {   
209                 const gchar *path1;
210                 const gchar *path2;
211         } pairs[] = {
212                 {"<Main>/Message/Reply",                        "<SummaryView>/Reply"},
213 #ifndef GENERIC_UMPC
214                 {"<Main>/Message/Reply to/all",                 "<SummaryView>/Reply to/all"},
215                 {"<Main>/Message/Reply to/sender",              "<SummaryView>/Reply to/sender"},
216                 {"<Main>/Message/Reply to/mailing list",        "<SummaryView>/Reply to/mailing list"},
217 #endif
218                 {"<Main>/Message/Forward",                      "<SummaryView>/Forward"},
219 #ifndef GENERIC_UMPC
220                 {"<Main>/Message/Redirect",                     "<SummaryView>/Redirect"},
221 #endif
222                 {"<Main>/Message/Move...",                      "<SummaryView>/Move..."},
223                 {"<Main>/Message/Copy...",                      "<SummaryView>/Copy..."},
224 #ifndef GENERIC_UMPC
225                 {"<Main>/Message/Delete...",                    "<SummaryView>/Delete..."},
226 #endif
227                 {"<Main>/Message/Mark/Mark",                    "<SummaryView>/Mark/Mark"},
228                 {"<Main>/Message/Mark/Unmark",                  "<SummaryView>/Mark/Unmark"},
229                 {"<Main>/Message/Mark/Mark as unread",          "<SummaryView>/Mark/Mark as unread"},
230                 {"<Main>/Message/Mark/Mark as read",            "<SummaryView>/Mark/Mark as read"},
231                 {"<Main>/Message/Mark/Mark all read",           "<SummaryView>/Mark/Mark all read"},
232 #ifndef GENERIC_UMPC
233                 {"<Main>/Tools/Add sender to address book",     "<SummaryView>/Add sender to address book"},
234 #endif
235                 {"<Main>/Tools/Create filter rule/Automatically",       
236                                                                 "<SummaryView>/Create filter rule/Automatically"},
237                 {"<Main>/Tools/Create filter rule/by From",     "<SummaryView>/Create filter rule/by From"},
238                 {"<Main>/Tools/Create filter rule/by To",       "<SummaryView>/Create filter rule/by To"},
239                 {"<Main>/Tools/Create filter rule/by Subject",  "<SummaryView>/Create filter rule/by Subject"},
240 #ifndef GENERIC_UMPC
241                 {"<Main>/Tools/Create processing rule/Automatically",
242                                                                 "<SummaryView>/Create processing rule/Automatically"},
243                 {"<Main>/Tools/Create processing rule/by From", "<SummaryView>/Create processing rule/by From"},
244                 {"<Main>/Tools/Create processing rule/by To",   "<SummaryView>/Create processing rule/by To"},
245                 {"<Main>/Tools/Create processing rule/by Subject",
246                                                                 "<SummaryView>/Create processing rule/by Subject"},
247 #endif
248                 {"<Main>/View/Open in new window",              "<SummaryView>/View/Open in new window"},
249                 {"<Main>/View/Message source",                  "<SummaryView>/View/Message source"},
250 #ifndef GENERIC_UMPC
251                 {"<Main>/View/All headers",                     "<SummaryView>/View/All headers"},
252 #endif
253         };
254
255         const gint numpairs = sizeof pairs / sizeof pairs[0];
256         for (n = 0; n < numpairs; n++) {
257                 /* get widgets from the paths */
258
259                 item1 = gtk_item_factory_get_widget
260                                 (gtk_item_factory_from_path(pairs[n].path1),pairs[n].path1);            
261                 item2 = gtk_item_factory_get_widget
262                                 (gtk_item_factory_from_path(pairs[n].path2),pairs[n].path2);            
263
264                 if (item1 && item2) {
265                         /* connect widgets both ways around */
266                         connect_accel_change_signals(item2,item1);
267                         connect_accel_change_signals(item1,item2);
268                 } else { 
269                         if (!item1) debug_print(" ** Menu item not found: %s\n",pairs[n].path1);
270                         if (!item2) debug_print(" ** Menu item not found: %s\n",pairs[n].path2);
271                 }                               
272         }
273 }