2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto
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.
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.
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/>.
25 #include <glib/gi18n.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>
42 #include <hildon/hildon-program.h>
44 #include <hildon-widgets/hildon-program.h>
46 #include <gtk/gtkmain.h>
49 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2) ;
52 GtkWidget *menubar_create(GtkWidget *window, GtkItemFactoryEntry *entries,
53 guint n_entries, const gchar *path, gpointer data)
55 GtkItemFactory *factory;
59 factory = gtk_item_factory_new(GTK_TYPE_MENU, path, NULL);
61 factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, path, NULL);
63 gtk_item_factory_set_translate_func(factory, menu_translate,
65 gtk_item_factory_create_items(factory, n_entries, entries, data);
66 gtk_window_add_accel_group (GTK_WINDOW (window), factory->accel_group);
68 menubar = gtk_item_factory_get_widget(factory, path);
70 hildon_window_set_menu(HILDON_WINDOW(window), GTK_MENU(menubar));
75 GtkWidget *menu_create_items(GtkItemFactoryEntry *entries,
76 guint n_entries, const gchar *path,
77 GtkItemFactory **factory, gpointer data)
79 *factory = gtk_item_factory_new(GTK_TYPE_MENU, path, NULL);
80 gtk_item_factory_set_translate_func(*factory, menu_translate,
82 gtk_item_factory_create_items(*factory, n_entries, entries, data);
84 return gtk_item_factory_get_widget(*factory, path);
87 GtkActionGroup *cm_menu_create_action_group(const gchar *name, GtkActionEntry **entries,
88 gint num_entries, gpointer data)
90 GtkActionGroup *group = gtk_action_group_new(name);
91 gtk_action_group_set_translate_func(group, menu_translate, NULL, NULL);
92 gtk_action_group_add_actions(group, entries, num_entries, data);
93 gtk_ui_manager_insert_action_group(gtkut_ui_manager(), group, 0);
97 gchar *menu_translate(const gchar *path, gpointer data)
101 retval = gettext(path);
106 void menu_set_sensitive(GtkItemFactory *ifactory, const gchar *path,
111 g_return_if_fail(ifactory != NULL);
113 widget = gtk_item_factory_get_item(ifactory, path);
114 g_return_if_fail(widget != NULL);
116 gtk_widget_set_sensitive(widget, sensitive);
119 void cm_menu_set_sensitive(gchar *menu, gboolean sensitive)
121 GtkUIManager *gui_manager = gtkut_ui_manager();
123 gchar *path = g_strdup_printf("/Menus/%s/", menu);
125 widget = gtk_ui_manager_get_widget(gui_manager, path);
126 if( !GTK_IS_WIDGET(widget) ) {
127 g_message("Blah, '%s' is not a widget.\n", path);
130 if( !GTK_IS_MENU_ITEM(widget) ) {
131 g_message("Blah, '%s' is not a menu item.\n", path);
134 gtk_widget_set_sensitive(widget, sensitive);
138 void cm_toggle_menu_set_active(gchar *menu, gboolean active)
140 GtkUIManager *gui_manager = gtkut_ui_manager();
142 gchar *path = g_strdup_printf("/Menus/%s/", menu);
144 widget = gtk_ui_manager_get_widget(gui_manager, path);
145 if( !GTK_IS_WIDGET(widget) ) {
146 g_message("Blah, '%s' is not a widget.\n", path);
149 if( !GTK_CHECK_MENU_ITEM(widget) ) {
150 g_message("Blah, '%s' is not a check menu item.\n", path);
153 gtk_check_menu_item_set_active(widget, active);
157 void menu_set_sensitive_all(GtkMenuShell *menu_shell, gboolean sensitive)
161 for (cur = menu_shell->children; cur != NULL; cur = cur->next)
162 gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
165 void menu_set_active(GtkItemFactory *ifactory, const gchar *path,
170 g_return_if_fail(ifactory != NULL);
172 widget = gtk_item_factory_get_item(ifactory, path);
173 g_return_if_fail(widget != NULL);
175 if (!GTK_IS_CHECK_MENU_ITEM(widget)) {
176 debug_print("%s not check_menu_item\n", path?path:"(null)");
179 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), is_active);
182 void menu_button_position(GtkMenu *menu, gint *x, gint *y, gboolean *push_in,
190 GdkRectangle monitor;
193 g_return_if_fail(x && y);
194 g_return_if_fail(GTK_IS_BUTTON(user_data));
196 widget = GTK_WIDGET(user_data);
198 gdk_window_get_origin(widget->window, x, y);
199 wheight = widget->requisition.height;
200 wx = widget->allocation.x;
201 wy = widget->allocation.y;
203 gtk_widget_size_request(GTK_WIDGET(menu), &mreq);
204 screen = gtk_widget_get_screen (widget);
205 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
206 gdk_screen_get_monitor_geometry (screen, monitor_num,
210 *y = *y + wy + wheight;
212 if (*y + mreq.height >= monitor.height)
216 gint menu_find_option_menu_index(GtkOptionMenu *optmenu, gpointer data,
225 menu = gtk_option_menu_get_menu(optmenu);
227 for (cur = GTK_MENU_SHELL(menu)->children, n = 0;
228 cur != NULL; cur = cur->next, n++) {
229 menuitem = GTK_WIDGET(cur->data);
230 menu_data = g_object_get_data(G_OBJECT(menuitem),
233 if (func(menu_data, data) == 0)
235 } else if (menu_data == data)
242 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2)
245 g_signal_connect_after(G_OBJECT(widget), "add_accelerator",
246 G_CALLBACK(menu_item_add_accel), wid2);
247 g_signal_connect_after(G_OBJECT(widget), "remove_accelerator",
248 G_CALLBACK(menu_item_remove_accel), wid2);
252 void menu_connect_identical_items(void)
258 static const struct {
262 {"<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"},
268 {"<Main>/Message/Forward", "<SummaryView>/Forward"},
270 {"<Main>/Message/Redirect", "<SummaryView>/Redirect"},
272 {"<Main>/Message/Move...", "<SummaryView>/Move..."},
273 {"<Main>/Message/Copy...", "<SummaryView>/Copy..."},
275 {"<Main>/Message/Delete...", "<SummaryView>/Delete..."},
277 {"<Main>/Message/Mark/Mark", "<SummaryView>/Mark/Mark"},
278 {"<Main>/Message/Mark/Unmark", "<SummaryView>/Mark/Unmark"},
279 {"<Main>/Message/Mark/Mark as unread", "<SummaryView>/Mark/Mark as unread"},
280 {"<Main>/Message/Mark/Mark as read", "<SummaryView>/Mark/Mark as read"},
281 {"<Main>/Message/Mark/Mark all read", "<SummaryView>/Mark/Mark all read"},
283 {"<Main>/Tools/Add sender to address book", "<SummaryView>/Add sender to address book"},
285 {"<Main>/Tools/Create filter rule/Automatically",
286 "<SummaryView>/Create filter rule/Automatically"},
287 {"<Main>/Tools/Create filter rule/by From", "<SummaryView>/Create filter rule/by From"},
288 {"<Main>/Tools/Create filter rule/by To", "<SummaryView>/Create filter rule/by To"},
289 {"<Main>/Tools/Create filter rule/by Subject", "<SummaryView>/Create filter rule/by Subject"},
291 {"<Main>/Tools/Create processing rule/Automatically",
292 "<SummaryView>/Create processing rule/Automatically"},
293 {"<Main>/Tools/Create processing rule/by From", "<SummaryView>/Create processing rule/by From"},
294 {"<Main>/Tools/Create processing rule/by To", "<SummaryView>/Create processing rule/by To"},
295 {"<Main>/Tools/Create processing rule/by Subject",
296 "<SummaryView>/Create processing rule/by Subject"},
298 {"<Main>/View/Open in new window", "<SummaryView>/View/Open in new window"},
299 {"<Main>/View/Message source", "<SummaryView>/View/Message source"},
301 {"<Main>/View/All headers", "<SummaryView>/View/All headers"},
305 const gint numpairs = sizeof pairs / sizeof pairs[0];
306 for (n = 0; n < numpairs; n++) {
307 /* get widgets from the paths */
309 item1 = gtk_item_factory_get_widget
310 (gtk_item_factory_from_path(pairs[n].path1),pairs[n].path1);
311 item2 = gtk_item_factory_get_widget
312 (gtk_item_factory_from_path(pairs[n].path2),pairs[n].path2);
314 if (item1 && item2) {
315 /* connect widgets both ways around */
316 connect_accel_change_signals(item2,item1);
317 connect_accel_change_signals(item1,item2);
319 if (!item1) debug_print(" ** Menu item not found: %s\n",pairs[n].path1);
320 if (!item2) debug_print(" ** Menu item not found: %s\n",pairs[n].path2);