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