2005-07-14 [paul] 1.9.12cvs47
[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 <glib/gi18n.h>
26 #include <gtk/gtk.h>
27 #include <gtk/gtkwidget.h>
28 #include <gtk/gtkmenu.h>
29 #include <gtk/gtkmenubar.h>
30 #include <gtk/gtkcheckmenuitem.h>
31 #include <gtk/gtkitemfactory.h>
32 #include <gtk/gtkbutton.h>
33 #include <gtk/gtkwindow.h>
34
35 #include "menu.h"
36 #include "utils.h"
37
38 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2) ;
39
40
41 GtkWidget *menubar_create(GtkWidget *window, GtkItemFactoryEntry *entries,
42                           guint n_entries, const gchar *path, gpointer data)
43 {
44         GtkItemFactory *factory;
45
46         factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, path, NULL);
47         gtk_item_factory_set_translate_func(factory, menu_translate,
48                                             NULL, NULL);
49         gtk_item_factory_create_items(factory, n_entries, entries, data);
50         gtk_window_add_accel_group (GTK_WINDOW (window), factory->accel_group);
51
52         return gtk_item_factory_get_widget(factory, path);
53 }
54
55 GtkWidget *menu_create_items(GtkItemFactoryEntry *entries,
56                              guint n_entries, const gchar *path,
57                              GtkItemFactory **factory, gpointer data)
58 {
59         *factory = gtk_item_factory_new(GTK_TYPE_MENU, path, NULL);
60         gtk_item_factory_set_translate_func(*factory, menu_translate,
61                                             NULL, NULL);
62         gtk_item_factory_create_items(*factory, n_entries, entries, data);
63
64         return gtk_item_factory_get_widget(*factory, path);
65 }
66
67 GtkWidget *popupmenu_create(GtkWidget *window, GtkItemFactoryEntry *entries,
68                              guint n_entries, const gchar *path, gpointer data)
69 {
70         GtkItemFactory *factory;
71         GtkAccelGroup *accel_group;
72
73         accel_group = gtk_accel_group_new();
74         factory = gtk_item_factory_new(GTK_TYPE_MENU, path, accel_group);
75         gtk_item_factory_set_translate_func(factory, menu_translate,
76                                             NULL, NULL);
77         gtk_item_factory_create_items(factory, n_entries, entries, data);
78         gtk_window_add_accel_group(GTK_WINDOW (window), accel_group);
79
80         return gtk_item_factory_get_widget(factory, path);
81 }
82
83 gchar *menu_translate(const gchar *path, gpointer data)
84 {
85         gchar *retval;
86
87         retval = gettext(path);
88
89         return retval;
90 }
91
92 void menu_set_sensitive(GtkItemFactory *ifactory, const gchar *path,
93                         gboolean sensitive)
94 {
95         GtkWidget *widget;
96
97         g_return_if_fail(ifactory != NULL);
98
99         widget = gtk_item_factory_get_item(ifactory, path);
100         g_return_if_fail(widget != NULL);
101
102         gtk_widget_set_sensitive(widget, sensitive);
103 }
104
105 void menu_set_sensitive_all(GtkMenuShell *menu_shell, gboolean sensitive)
106 {
107         GList *cur;
108
109         for (cur = menu_shell->children; cur != NULL; cur = cur->next)
110                 gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
111 }
112
113 void menu_set_active(GtkItemFactory *ifactory, const gchar *path,
114                      gboolean is_active)
115 {
116         GtkWidget *widget;
117
118         g_return_if_fail(ifactory != NULL);
119
120         widget = gtk_item_factory_get_item(ifactory, path);
121         g_return_if_fail(widget != NULL);
122
123         if (!GTK_IS_CHECK_MENU_ITEM(widget)) {
124                 debug_print("%s not check_menu_item\n", path);
125                 return;
126         }       
127         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), is_active);
128 }
129
130 void menu_button_position(GtkMenu *menu, gint *x, gint *y, gboolean *push_in,
131                           gpointer user_data)
132 {
133         GtkWidget *widget;
134         gint wheight;
135         gint wx, wy;
136         GtkRequisition mreq;
137         GdkScreen *screen;
138         GdkRectangle monitor;
139         gint monitor_num;
140
141         g_return_if_fail(x && y);
142         g_return_if_fail(GTK_IS_BUTTON(user_data));
143
144         widget = GTK_WIDGET(user_data);
145
146         gdk_window_get_origin(widget->window, x, y);
147         wheight = widget->requisition.height;
148         wx = widget->allocation.x;
149         wy = widget->allocation.y;
150         
151         gtk_widget_size_request(GTK_WIDGET(menu), &mreq);
152         screen = gtk_widget_get_screen (widget);
153         monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
154         gdk_screen_get_monitor_geometry (screen, monitor_num, 
155                                          &monitor);
156
157         *x = *x + wx;
158         *y = *y + wy + wheight;
159         
160         if (*y + mreq.height >= monitor.height)
161                 *y -= mreq.height;
162 }
163
164 gint menu_find_option_menu_index(GtkOptionMenu *optmenu, gpointer data,
165                                  GCompareFunc func)
166 {
167         GtkWidget *menu;
168         GtkWidget *menuitem;
169         gpointer menu_data;
170         GList *cur;
171         gint n;
172
173         menu = gtk_option_menu_get_menu(optmenu);
174
175         for (cur = GTK_MENU_SHELL(menu)->children, n = 0;
176              cur != NULL; cur = cur->next, n++) {
177                 menuitem = GTK_WIDGET(cur->data);
178                 menu_data = g_object_get_data(G_OBJECT(menuitem),
179                                               MENU_VAL_ID);
180                 if (func) {
181                         if (func(menu_data, data) == 0)
182                                 return n;
183                 } else if (menu_data == data)
184                         return n;
185         }
186
187         return -1;
188 }
189
190 gpointer menu_get_option_menu_active_user_data(GtkOptionMenu *optmenu)
191 {
192         GtkWidget *menu;
193         GtkWidget *menuitem;
194
195         menu = gtk_option_menu_get_menu(optmenu);
196         menuitem = gtk_menu_get_active(GTK_MENU(menu));
197
198         return g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID);
199 }
200
201 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2) 
202 {
203 #if 0
204         g_signal_connect_after(G_OBJECT(widget), "add_accelerator", 
205                                G_CALLBACK(menu_item_add_accel), wid2);
206         g_signal_connect_after(G_OBJECT(widget), "remove_accelerator", 
207                                G_CALLBACK(menu_item_remove_accel), wid2);
208 #endif
209 }
210
211 void menu_connect_identical_items(void)
212 {
213         gint n;
214         GtkWidget *item1;
215         GtkWidget *item2;
216
217         static const struct {   
218                 const gchar *path1;
219                 const gchar *path2;
220         } pairs[] = {
221                 {"<Main>/Message/Reply",                        "<SummaryView>/Reply"},
222                 {"<Main>/Message/Reply to/all",                 "<SummaryView>/Reply to/all"},
223                 {"<Main>/Message/Reply to/sender",              "<SummaryView>/Reply to/sender"},
224                 {"<Main>/Message/Reply to/mailing list",        "<SummaryView>/Reply to/mailing list"},
225                 {"<Main>/Message/Follow-up and reply to",       "<SummaryView>/Follow-up and reply to"},
226                 {"<Main>/Message/Forward",                      "<SummaryView>/Forward"},
227                 {"<Main>/Message/Redirect",                     "<SummaryView>/Redirect"},
228                 {"<Main>/Message/Re-edit",                      "<SummaryView>/Re-edit"},
229                 {"<Main>/Message/Move...",                      "<SummaryView>/Move..."},
230                 {"<Main>/Message/Copy...",                      "<SummaryView>/Copy..."},
231                 {"<Main>/Message/Delete...",                    "<SummaryView>/Delete..."},
232                 {"<Main>/Message/Cancel a news message",        "<SummaryView>/Cancel a news message"},
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                 {"<Main>/Tools/Add sender to address book",     "<SummaryView>/Add sender to address book"},
239                 {"<Main>/Tools/Create filter rule/Automatically",       
240                                                                 "<SummaryView>/Create filter rule/Automatically"},
241                 {"<Main>/Tools/Create filter rule/by From",     "<SummaryView>/Create filter rule/by From"},
242                 {"<Main>/Tools/Create filter rule/by To",       "<SummaryView>/Create filter rule/by To"},
243                 {"<Main>/Tools/Create filter rule/by Subject",  "<SummaryView>/Create filter rule/by Subject"},
244                 {"<Main>/Tools/Create processing rule/Automatically",
245                                                                 "<SummaryView>/Create processing rule/Automatically"},
246                 {"<Main>/Tools/Create processing rule/by From", "<SummaryView>/Create processing rule/by From"},
247                 {"<Main>/Tools/Create processing rule/by To",   "<SummaryView>/Create processing rule/by To"},
248                 {"<Main>/Tools/Create processing rule/by Subject",
249                                                                 "<SummaryView>/Create processing rule/by Subject"},
250                 {"<Main>/View/Open in new window",              "<SummaryView>/View/Open in new window"},
251                 {"<Main>/View/Message source",                  "<SummaryView>/View/Source"},
252                 {"<Main>/View/Show all headers",                "<SummaryView>/View/All header"},
253         };
254
255         const gint numpairs = sizeof pairs / sizeof pairs[0];
256         for (n = 0; n < numpairs; n++) {
257                 /* get widgets from the paths */
258
259                 item1 = gtk_item_factory_get_widget
260                                 (gtk_item_factory_from_path(pairs[n].path1),pairs[n].path1);            
261                 item2 = gtk_item_factory_get_widget
262                                 (gtk_item_factory_from_path(pairs[n].path2),pairs[n].path2);            
263
264                 if (item1 && item2) {
265                         /* connect widgets both ways around */
266                         connect_accel_change_signals(item2,item1);
267                         connect_accel_change_signals(item1,item2);
268                 } else { 
269                         if (!item1) debug_print(" ** Menu item not found: %s\n",pairs[n].path1);
270                         if (!item2) debug_print(" ** Menu item not found: %s\n",pairs[n].path2);
271                 }                               
272         }
273 }
274
275 void menu_select_by_data(GtkMenu *menu, gpointer data)
276 {
277         GList *children, *cur;
278         GtkWidget *select_item = NULL;
279         
280         g_return_if_fail(menu != NULL);
281
282         children = gtk_container_get_children(GTK_CONTAINER(menu));
283
284         for (cur = children; cur != NULL; cur = g_list_next(cur)) {
285                 GObject *child = G_OBJECT(cur->data);
286
287                 if (g_object_get_data(child, MENU_VAL_ID) == data) {
288                         select_item = GTK_WIDGET(child);
289                 }
290         }
291         if (select_item != NULL) {
292                 gtk_menu_shell_select_item(GTK_MENU_SHELL(menu), select_item);
293                 gtk_menu_shell_activate_item(GTK_MENU_SHELL(menu), select_item, FALSE);
294         }
295
296         g_list_free(children);
297 }