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