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