fix bug 605957 'shortcuts in popup-menu'
[claws.git] / src / gtk / menu.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <gtk/gtkwidget.h>
26 #include <gtk/gtkmenu.h>
27 #include <gtk/gtkmenubar.h>
28 #include <gtk/gtkitemfactory.h>
29 #include <gtk/gtkcheckmenuitem.h>
30 #include <gtk/gtkbutton.h>
31
32 #include "intl.h"
33 #include "menu.h"
34 #include "utils.h"
35
36 static gchar *menu_translate(const gchar *path, gpointer data);
37
38 static void menu_item_add_accel( GtkWidget *widget, guint accel_signal_id, GtkAccelGroup *accel_group,
39                                  guint accel_key, GdkModifierType accel_mods, GtkAccelFlags accel_flags,
40                                  gpointer user_data);
41
42 static void menu_item_remove_accel(GtkWidget *widget, GtkAccelGroup *accel_group,
43                                    guint accel_key, GdkModifierType accel_mods,
44                                    gpointer user_data);
45
46 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2) ;
47
48
49 GtkWidget *menubar_create(GtkWidget *window, GtkItemFactoryEntry *entries,
50                           guint n_entries, const gchar *path, gpointer data)
51 {
52         GtkItemFactory *factory;
53         GtkAccelGroup *accel_group;
54
55         accel_group = gtk_accel_group_new();
56         factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, path, accel_group);
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_accel_group_attach(accel_group, GTK_OBJECT(window));
61
62         return gtk_item_factory_get_widget(factory, path);
63 }
64
65 GtkWidget *menu_create_items(GtkItemFactoryEntry *entries,
66                              guint n_entries, const gchar *path,
67                              GtkItemFactory **factory, gpointer data)
68 {
69         *factory = gtk_item_factory_new(GTK_TYPE_MENU, path, NULL);
70         gtk_item_factory_set_translate_func(*factory, menu_translate,
71                                             NULL, NULL);
72         gtk_item_factory_create_items(*factory, n_entries, entries, data);
73
74         return gtk_item_factory_get_widget(*factory, path);
75 }
76
77 GtkWidget *popupmenu_create(GtkWidget *window, GtkItemFactoryEntry *entries,
78                              guint n_entries, const gchar *path, gpointer data)
79 {
80         GtkItemFactory *factory;
81         GtkAccelGroup *accel_group;
82
83         accel_group = gtk_accel_group_new();
84         factory = gtk_item_factory_new(GTK_TYPE_MENU, path, accel_group);
85         gtk_item_factory_set_translate_func(factory, menu_translate,
86                                             NULL, NULL);
87         gtk_item_factory_create_items(factory, n_entries, entries, data);
88         gtk_accel_group_attach(accel_group, GTK_OBJECT(window));
89
90         return gtk_item_factory_get_widget(factory, path);
91 }
92
93 static gchar *menu_translate(const gchar *path, gpointer data)
94 {
95         gchar *retval;
96
97         retval = gettext(path);
98
99         return retval;
100 }
101
102 void menu_set_sensitive(GtkItemFactory *ifactory, const gchar *path,
103                         gboolean sensitive)
104 {
105         GtkWidget *widget;
106
107         g_return_if_fail(ifactory != NULL);
108
109         widget = gtk_item_factory_get_item(ifactory, path);
110         if(widget == NULL) {
111                 debug_print("unknown menu entry %s\n", path);
112                 return;
113         }
114         gtk_widget_set_sensitive(widget, sensitive);
115 }
116
117 void menu_set_sensitive_all(GtkMenuShell *menu_shell, gboolean sensitive)
118 {
119         GList *cur;
120
121         for (cur = menu_shell->children; cur != NULL; cur = cur->next)
122                 gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
123 }
124
125 void menu_set_toggle(GtkItemFactory *ifactory, const gchar *path,
126                         gboolean active)
127 {
128         GtkWidget *widget;
129
130         g_return_if_fail(ifactory != NULL);
131
132         widget = gtk_item_factory_get_item(ifactory, path);
133         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(widget), active);
134 }
135
136 void menu_toggle_toggle(GtkItemFactory *ifactory, const gchar *path)
137 {
138         GtkWidget *widget;
139         
140         g_return_if_fail(ifactory != NULL);
141         
142         widget = gtk_item_factory_get_item(ifactory, path);
143         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), !((GTK_CHECK_MENU_ITEM(widget))->active));
144 }
145
146 void menu_button_position(GtkMenu *menu, gint *x, gint *y, gpointer user_data)
147 {
148         GtkWidget *button;
149         GtkRequisition requisition;
150         gint button_xpos, button_ypos;
151         gint xpos, ypos;
152         gint width, height;
153         gint scr_width, scr_height;
154
155         g_return_if_fail(user_data != NULL);
156         g_return_if_fail(GTK_IS_BUTTON(user_data));
157
158         button = GTK_WIDGET(user_data);
159
160         gtk_widget_get_child_requisition(GTK_WIDGET(menu), &requisition);
161         width = requisition.width;
162         height = requisition.height;
163         gdk_window_get_origin(button->window, &button_xpos, &button_ypos);
164
165         xpos = button_xpos;
166         ypos = button_ypos + button->allocation.height;
167
168         scr_width = gdk_screen_width();
169         scr_height = gdk_screen_height();
170
171         if (xpos + width > scr_width)
172                 xpos -= (xpos + width) - scr_width;
173         if (ypos + height > scr_height)
174                 ypos = button_ypos - height;
175         if (xpos < 0)
176                 xpos = 0;
177         if (ypos < 0)
178                 ypos = 0;
179
180         *x = xpos;
181         *y = ypos;
182 }
183
184 gint menu_find_option_menu_index(GtkOptionMenu *optmenu, gpointer data,
185                                  GCompareFunc func)
186 {
187         GtkWidget *menu;
188         GtkWidget *menuitem;
189         gpointer menu_data;
190         GList *cur;
191         gint n;
192
193         menu = gtk_option_menu_get_menu(optmenu);
194
195         for (cur = GTK_MENU_SHELL(menu)->children, n = 0;
196              cur != NULL; cur = cur->next, n++) {
197                 menuitem = GTK_WIDGET(cur->data);
198                 menu_data = gtk_object_get_user_data(GTK_OBJECT(menuitem));
199                 if (func) {
200                         if (func(menu_data, data) == 0)
201                                 return n;
202                 } else if (menu_data == data)
203                         return n;
204         }
205
206         return -1;
207 }
208
209 /* call backs for accelerator changes on selected menu items */
210 static void menu_item_add_accel( GtkWidget *widget, guint accel_signal_id, GtkAccelGroup *accel_group,
211                                  guint accel_key, GdkModifierType accel_mods, GtkAccelFlags accel_flags,
212                                  gpointer user_data)
213 {
214         GtkWidget *connected = GTK_WIDGET(user_data);   
215         if (gtk_signal_n_emissions_by_name(GTK_OBJECT(widget),"add_accelerator") > 1 ) return;
216         gtk_widget_remove_accelerators(connected,"activate",FALSE);
217         /* lock _this_ widget */
218         gtk_accel_group_lock_entry(accel_group,accel_key,accel_mods);
219         /* modify the _other_ widget */
220         gtk_widget_add_accelerator(connected, "activate",
221                                    gtk_item_factory_from_widget(connected)->accel_group,
222                                    accel_key, accel_mods,
223                                    GTK_ACCEL_VISIBLE );
224         gtk_accel_group_unlock_entry(accel_group,accel_key,accel_mods);                            
225 }
226
227 static void menu_item_remove_accel(GtkWidget *widget, GtkAccelGroup *accel_group,
228                                    guint accel_key, GdkModifierType accel_mods,
229                                    gpointer user_data)
230 {       
231         GtkWidget *wid = GTK_WIDGET(user_data);
232
233         if (gtk_signal_n_emissions_by_name(GTK_OBJECT(widget),
234             "remove_accelerator") > 2 )
235                 return;
236         gtk_widget_remove_accelerators(wid,"activate",FALSE);
237 }
238
239 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2) 
240 {
241         gtk_signal_connect_after(GTK_OBJECT(widget), "add_accelerator", 
242                                  menu_item_add_accel, wid2);
243         gtk_signal_connect_after(GTK_OBJECT(widget), "remove_accelerator", 
244                                  menu_item_remove_accel, wid2);
245 }
246
247 void menu_connect_identical_items(void)
248 {
249         gint n;
250         GtkWidget *item1;
251         GtkWidget *item2;
252
253         static const struct {   
254                 const gchar *path1;
255                 const gchar *path2;
256         } pairs[] = {
257                 {"<Main>/Message/Reply",                        "<SummaryView>/Reply"},
258                 {"<Main>/Message/Reply to/all",                 "<SummaryView>/Reply to/all"},
259                 {"<Main>/Message/Reply to/sender",              "<SummaryView>/Reply to/sender"},
260                 {"<Main>/Message/Reply to/mailing list",        "<SummaryView>/Reply to/mailing list"},
261                 {"<Main>/Message/Follow-up and reply to",       "<SummaryView>/Follow-up and reply to"},
262                 {"<Main>/Message/Forward",                      "<SummaryView>/Forward"},
263                 {"<Main>/Message/Redirect",                     "<SummaryView>/Redirect"},
264                 {"<Main>/Message/Re-edit",                      "<SummaryView>/Re-edit"},
265                 {"<Main>/Message/Move...",                      "<SummaryView>/Move..."},
266                 {"<Main>/Message/Copy...",                      "<SummaryView>/Copy..."},
267                 {"<Main>/Message/Delete",                       "<SummaryView>/Delete"},
268                 {"<Main>/Message/Cancel a news message",        "<SummaryView>/Cancel a news message"},
269                 {"<Main>/Tools/Execute",                        "<SummaryView>/Execute"},
270                 {"<Main>/Message/Mark/Mark",                    "<SummaryView>/Mark/Mark"},
271                 {"<Main>/Message/Mark/Unmark",                  "<SummaryView>/Mark/Unmark"},
272                 {"<Main>/Message/Mark/Mark as unread",          "<SummaryView>/Mark/Mark as unread"},
273                 {"<Main>/Message/Mark/Mark as read",            "<SummaryView>/Mark/Mark as read"},
274                 {"<Main>/Message/Mark/Mark all read",           "<SummaryView>/Mark/Mark all read"},
275                 {"<Main>/Tools/Add sender to address book",     "<SummaryView>/Add sender to address book"},
276                 {"<Main>/Tools/Create filter rule/Automatically",       "<SummaryView>/Create filter rule/Automatically"},
277                 {"<Main>/Tools/Create filter rule/by From",     "<SummaryView>/Create filter rule/by From"},
278                 {"<Main>/Tools/Create filter rule/by To",       "<SummaryView>/Create filter rule/by To"},
279                 {"<Main>/Tools/Create filter rule/by Subject",  "<SummaryView>/Create filter rule/by Subject"},
280                 {"<Main>/View/Open in new window",              "<SummaryView>/View/Open in new window"},
281                 {"<Main>/View/Message source",                  "<SummaryView>/View/Source"},
282                 {"<Main>/View/Show all headers",                "<SummaryView>/View/All header"},
283                 {"<Main>/File/Save as...",                      "<SummaryView>/Save as..."},
284                 {"<Main>/File/Print...",                        "<SummaryView>/Print..."},
285                 {"<Main>/Edit/Select all",                      "<SummaryView>/Select all"},
286                 {"<Main>/Edit/Select thread",                   "<SummaryView>/Select thread"}           
287         };
288
289         const gint numpairs = sizeof pairs / sizeof pairs[0];
290         for (n = 0; n < numpairs; n++) {
291                 /* get widgets from the paths */
292
293                 item1 = gtk_item_factory_get_widget
294                                 (gtk_item_factory_from_path(pairs[n].path1),pairs[n].path1);            
295                 item2 = gtk_item_factory_get_widget
296                                 (gtk_item_factory_from_path(pairs[n].path2),pairs[n].path2);            
297
298                 if (item1 && item2) {
299                         /* connect widgets both ways around */
300                         connect_accel_change_signals(item2,item1);
301                         connect_accel_change_signals(item1,item2);
302                 } else { 
303                         if (!item1) debug_print(" ** Menu item not found: %s\n",pairs[n].path1);
304                         if (!item2) debug_print(" ** Menu item not found: %s\n",pairs[n].path2);
305                 }                               
306         }
307 }
308
309
310
311