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