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