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