b8d4f422f0e2e4fe73301babc5e4058e8704783e
[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 GtkWidget *popupmenu_create(GtkWidget *window, GtkItemFactoryEntry *entries,
82                              guint n_entries, const gchar *path, gpointer data)
83 {
84         GtkItemFactory *factory;
85         GtkAccelGroup *accel_group;
86
87         accel_group = gtk_accel_group_new();
88         factory = gtk_item_factory_new(GTK_TYPE_MENU, path, accel_group);
89         gtk_item_factory_set_translate_func(factory, menu_translate,
90                                             NULL, NULL);
91         gtk_item_factory_create_items(factory, n_entries, entries, data);
92         gtk_window_add_accel_group(GTK_WINDOW (window), accel_group);
93
94         return gtk_item_factory_get_widget(factory, path);
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 menu_set_sensitive_all(GtkMenuShell *menu_shell, gboolean sensitive)
120 {
121         GList *cur;
122
123         for (cur = menu_shell->children; cur != NULL; cur = cur->next)
124                 gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
125 }
126
127 void menu_set_active(GtkItemFactory *ifactory, const gchar *path,
128                      gboolean is_active)
129 {
130         GtkWidget *widget;
131
132         g_return_if_fail(ifactory != NULL);
133
134         widget = gtk_item_factory_get_item(ifactory, path);
135         g_return_if_fail(widget != NULL);
136
137         if (!GTK_IS_CHECK_MENU_ITEM(widget)) {
138                 debug_print("%s not check_menu_item\n", path?path:"(null)");
139                 return;
140         }       
141         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), is_active);
142 }
143
144 void menu_button_position(GtkMenu *menu, gint *x, gint *y, gboolean *push_in,
145                           gpointer user_data)
146 {
147         GtkWidget *widget;
148         gint wheight;
149         gint wx, wy;
150         GtkRequisition mreq;
151         GdkScreen *screen;
152         GdkRectangle monitor;
153         gint monitor_num;
154
155         g_return_if_fail(x && y);
156         g_return_if_fail(GTK_IS_BUTTON(user_data));
157
158         widget = GTK_WIDGET(user_data);
159
160         gdk_window_get_origin(widget->window, x, y);
161         wheight = widget->requisition.height;
162         wx = widget->allocation.x;
163         wy = widget->allocation.y;
164         
165         gtk_widget_size_request(GTK_WIDGET(menu), &mreq);
166         screen = gtk_widget_get_screen (widget);
167         monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
168         gdk_screen_get_monitor_geometry (screen, monitor_num, 
169                                          &monitor);
170
171         *x = *x + wx;
172         *y = *y + wy + wheight;
173         
174         if (*y + mreq.height >= monitor.height)
175                 *y -= mreq.height;
176 }
177
178 gint menu_find_option_menu_index(GtkOptionMenu *optmenu, gpointer data,
179                                  GCompareFunc func)
180 {
181         GtkWidget *menu;
182         GtkWidget *menuitem;
183         gpointer menu_data;
184         GList *cur;
185         gint n;
186
187         menu = gtk_option_menu_get_menu(optmenu);
188
189         for (cur = GTK_MENU_SHELL(menu)->children, n = 0;
190              cur != NULL; cur = cur->next, n++) {
191                 menuitem = GTK_WIDGET(cur->data);
192                 menu_data = g_object_get_data(G_OBJECT(menuitem),
193                                               MENU_VAL_ID);
194                 if (func) {
195                         if (func(menu_data, data) == 0)
196                                 return n;
197                 } else if (menu_data == data)
198                         return n;
199         }
200
201         return -1;
202 }
203
204 gpointer menu_get_option_menu_active_user_data(GtkOptionMenu *optmenu)
205 {
206         GtkWidget *menu;
207         GtkWidget *menuitem;
208
209         menu = gtk_option_menu_get_menu(optmenu);
210         menuitem = gtk_menu_get_active(GTK_MENU(menu));
211
212         return g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID);
213 }
214
215 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2) 
216 {
217 #if 0
218         g_signal_connect_after(G_OBJECT(widget), "add_accelerator", 
219                                G_CALLBACK(menu_item_add_accel), wid2);
220         g_signal_connect_after(G_OBJECT(widget), "remove_accelerator", 
221                                G_CALLBACK(menu_item_remove_accel), wid2);
222 #endif
223 }
224
225 void menu_connect_identical_items(void)
226 {
227         gint n;
228         GtkWidget *item1;
229         GtkWidget *item2;
230
231         static const struct {   
232                 const gchar *path1;
233                 const gchar *path2;
234         } pairs[] = {
235                 {"<Main>/Message/Reply",                        "<SummaryView>/Reply"},
236 #ifndef MAEMO
237                 {"<Main>/Message/Reply to/all",                 "<SummaryView>/Reply to/all"},
238                 {"<Main>/Message/Reply to/sender",              "<SummaryView>/Reply to/sender"},
239                 {"<Main>/Message/Reply to/mailing list",        "<SummaryView>/Reply to/mailing list"},
240 #endif
241                 {"<Main>/Message/Forward",                      "<SummaryView>/Forward"},
242 #ifndef MAEMO
243                 {"<Main>/Message/Redirect",                     "<SummaryView>/Redirect"},
244 #endif
245                 {"<Main>/Message/Move...",                      "<SummaryView>/Move..."},
246                 {"<Main>/Message/Copy...",                      "<SummaryView>/Copy..."},
247 #ifndef MAEMO
248                 {"<Main>/Message/Delete...",                    "<SummaryView>/Delete..."},
249 #endif
250                 {"<Main>/Message/Mark/Mark",                    "<SummaryView>/Mark/Mark"},
251                 {"<Main>/Message/Mark/Unmark",                  "<SummaryView>/Mark/Unmark"},
252                 {"<Main>/Message/Mark/Mark as unread",          "<SummaryView>/Mark/Mark as unread"},
253                 {"<Main>/Message/Mark/Mark as read",            "<SummaryView>/Mark/Mark as read"},
254                 {"<Main>/Message/Mark/Mark all read",           "<SummaryView>/Mark/Mark all read"},
255 #ifndef MAEMO
256                 {"<Main>/Tools/Add sender to address book",     "<SummaryView>/Add sender to address book"},
257 #endif
258                 {"<Main>/Tools/Create filter rule/Automatically",       
259                                                                 "<SummaryView>/Create filter rule/Automatically"},
260                 {"<Main>/Tools/Create filter rule/by From",     "<SummaryView>/Create filter rule/by From"},
261                 {"<Main>/Tools/Create filter rule/by To",       "<SummaryView>/Create filter rule/by To"},
262                 {"<Main>/Tools/Create filter rule/by Subject",  "<SummaryView>/Create filter rule/by Subject"},
263 #ifndef MAEMO
264                 {"<Main>/Tools/Create processing rule/Automatically",
265                                                                 "<SummaryView>/Create processing rule/Automatically"},
266                 {"<Main>/Tools/Create processing rule/by From", "<SummaryView>/Create processing rule/by From"},
267                 {"<Main>/Tools/Create processing rule/by To",   "<SummaryView>/Create processing rule/by To"},
268                 {"<Main>/Tools/Create processing rule/by Subject",
269                                                                 "<SummaryView>/Create processing rule/by Subject"},
270 #endif
271                 {"<Main>/View/Open in new window",              "<SummaryView>/View/Open in new window"},
272                 {"<Main>/View/Message source",                  "<SummaryView>/View/Message source"},
273 #ifndef MAEMO
274                 {"<Main>/View/All headers",                     "<SummaryView>/View/All headers"},
275 #endif
276         };
277
278         const gint numpairs = sizeof pairs / sizeof pairs[0];
279         for (n = 0; n < numpairs; n++) {
280                 /* get widgets from the paths */
281
282                 item1 = gtk_item_factory_get_widget
283                                 (gtk_item_factory_from_path(pairs[n].path1),pairs[n].path1);            
284                 item2 = gtk_item_factory_get_widget
285                                 (gtk_item_factory_from_path(pairs[n].path2),pairs[n].path2);            
286
287                 if (item1 && item2) {
288                         /* connect widgets both ways around */
289                         connect_accel_change_signals(item2,item1);
290                         connect_accel_change_signals(item1,item2);
291                 } else { 
292                         if (!item1) debug_print(" ** Menu item not found: %s\n",pairs[n].path1);
293                         if (!item2) debug_print(" ** Menu item not found: %s\n",pairs[n].path2);
294                 }                               
295         }
296 }
297
298 void menu_select_by_data(GtkMenu *menu, gpointer data)
299 {
300         GList *children, *cur;
301         GtkWidget *select_item = NULL;
302         
303         g_return_if_fail(menu != NULL);
304
305         children = gtk_container_get_children(GTK_CONTAINER(menu));
306
307         for (cur = children; cur != NULL; cur = g_list_next(cur)) {
308                 GObject *child = G_OBJECT(cur->data);
309
310                 if (g_object_get_data(child, MENU_VAL_ID) == data) {
311                         select_item = GTK_WIDGET(child);
312                 }
313         }
314         if (select_item != NULL) {
315                 gtk_menu_shell_select_item(GTK_MENU_SHELL(menu), select_item);
316                 gtk_menu_shell_activate_item(GTK_MENU_SHELL(menu), select_item, FALSE);
317         }
318
319         g_list_free(children);
320 }