2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 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 2 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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 #include <gtk/gtkwindow.h>
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,
42 static void menu_item_remove_accel(GtkWidget *widget, GtkAccelGroup *accel_group,
43 guint accel_key, GdkModifierType accel_mods,
46 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2) ;
49 GtkWidget *menubar_create(GtkWidget *window, GtkItemFactoryEntry *entries,
50 guint n_entries, const gchar *path, gpointer data)
52 GtkItemFactory *factory;
54 factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, path, NULL);
55 gtk_item_factory_set_translate_func(factory, menu_translate,
57 gtk_item_factory_create_items(factory, n_entries, entries, data);
58 gtk_window_add_accel_group (GTK_WINDOW (window), factory->accel_group);
60 return gtk_item_factory_get_widget(factory, path);
63 GtkWidget *menu_create_items(GtkItemFactoryEntry *entries,
64 guint n_entries, const gchar *path,
65 GtkItemFactory **factory, gpointer data)
67 *factory = gtk_item_factory_new(GTK_TYPE_MENU, path, NULL);
68 gtk_item_factory_set_translate_func(*factory, menu_translate,
70 gtk_item_factory_create_items(*factory, n_entries, entries, data);
72 return gtk_item_factory_get_widget(*factory, path);
75 GtkWidget *popupmenu_create(GtkWidget *window, GtkItemFactoryEntry *entries,
76 guint n_entries, const gchar *path, gpointer data)
78 GtkItemFactory *factory;
79 GtkAccelGroup *accel_group;
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,
85 gtk_item_factory_create_items(factory, n_entries, entries, data);
86 gtk_window_add_accel_group(GTK_WINDOW (window), accel_group);
88 return gtk_item_factory_get_widget(factory, path);
91 gchar *menu_translate(const gchar *path, gpointer data)
95 retval = gettext(path);
100 static void factory_print_func(gpointer data, gchar *string)
102 GString *out_str = data;
104 g_string_append(out_str, string);
105 g_string_append_c(out_str, '\n');
110 GString *menu_factory_get_rc(const gchar *path)
113 GtkPatternSpec *pspec;
116 pspec = g_new(GtkPatternSpec, 1);
117 g_snprintf(pattern, sizeof(pattern), "%s*", path);
118 gtk_pattern_spec_init(pspec, pattern);
119 string = g_string_new("");
120 gtk_item_factory_dump_items(pspec, FALSE, factory_print_func,
122 gtk_pattern_spec_free_segs(pspec);
127 void menu_factory_clear_rc(const gchar *rc_str)
134 string = g_string_new(rc_str);
135 while ((p = strstr(string->str + pos, "(menu-path \"")) != NULL) {
136 pos = p + 12 - string->str;
137 p = strchr(p + 12, '"');
139 start = strchr(p + 1, '"');
140 if (!start) continue;
141 end = strchr(start + 1, '"');
143 pos = start + 1 - string->str;
145 g_string_erase(string, pos, end - (start + 1));
148 gtk_item_factory_parse_rc_string(string->str);
149 g_string_free(string, TRUE);
152 void menu_factory_copy_rc(const gchar *src_path, const gchar *dest_path)
160 string = menu_factory_get_rc(src_path);
161 src_path_len = strlen(src_path);
162 dest_path_len = strlen(dest_path);
164 while ((p = strstr(string->str + pos, src_path)) != NULL) {
165 pos = p - string->str;
166 g_string_erase(string, pos, src_path_len);
167 g_string_insert(string, pos, dest_path);
168 pos += dest_path_len;
172 while ((p = strchr(string->str + pos, ';')) != NULL) {
173 pos = p - string->str;
174 if (pos == 0 || *(p - 1) == '\n')
175 g_string_erase(string, pos, 1);
178 menu_factory_clear_rc(string->str);
179 gtk_item_factory_parse_rc_string(string->str);
180 g_string_free(string, TRUE);
184 void menu_set_sensitive(GtkItemFactory *ifactory, const gchar *path,
189 g_return_if_fail(ifactory != NULL);
191 widget = gtk_item_factory_get_item(ifactory, path);
193 debug_print("unknown menu entry %s\n", path);
196 gtk_widget_set_sensitive(widget, sensitive);
199 void menu_set_sensitive_all(GtkMenuShell *menu_shell, gboolean sensitive)
203 for (cur = menu_shell->children; cur != NULL; cur = cur->next)
204 gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
207 void menu_set_toggle(GtkItemFactory *ifactory, const gchar *path,
212 g_return_if_fail(ifactory != NULL);
214 widget = gtk_item_factory_get_item(ifactory, path);
215 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(widget), active);
218 void menu_toggle_toggle(GtkItemFactory *ifactory, const gchar *path)
222 g_return_if_fail(ifactory != NULL);
224 widget = gtk_item_factory_get_item(ifactory, path);
225 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), !((GTK_CHECK_MENU_ITEM(widget))->active));
228 void menu_button_position(GtkMenu *menu, gint *x, gint *y, gboolean *push_in,
235 g_return_if_fail(x && y);
236 g_return_if_fail(GTK_IS_BUTTON(user_data));
238 widget = GTK_WIDGET(user_data);
240 gdk_window_get_origin(widget->window, x, y);
241 wheight = widget->requisition.height;
242 wx = widget->allocation.x;
243 wy = widget->allocation.y;
245 *y = *y + wy + wheight;
249 gint menu_find_option_menu_index(GtkOptionMenu *optmenu, gpointer data,
258 menu = gtk_option_menu_get_menu(optmenu);
260 for (cur = GTK_MENU_SHELL(menu)->children, n = 0;
261 cur != NULL; cur = cur->next, n++) {
262 menuitem = GTK_WIDGET(cur->data);
263 menu_data = g_object_get_data(G_OBJECT(menuitem),
266 if (func(menu_data, data) == 0)
268 } else if (menu_data == data)
275 /* call backs for accelerator changes on selected menu items */
276 static void menu_item_add_accel( GtkWidget *widget, guint accel_signal_id, GtkAccelGroup *accel_group,
277 guint accel_key, GdkModifierType accel_mods, GtkAccelFlags accel_flags,
282 GtkWidget *connected = GTK_WIDGET(user_data);
283 if (gtk_signal_n_emissions_by_name(G_OBJECT(widget),"add_accelerator") > 1 ) return;
284 gtk_widget_remove_accelerators(connected,"activate",FALSE);
285 /* lock _this_ widget */
286 gtk_accel_group_lock_entry(accel_group,accel_key,accel_mods);
287 /* modify the _other_ widget */
288 gtk_widget_add_accelerator(connected, "activate",
289 gtk_item_factory_from_widget(connected)->accel_group,
290 accel_key, accel_mods,
292 gtk_accel_group_unlock_entry(accel_group,accel_key,accel_mods);
296 static void menu_item_remove_accel(GtkWidget *widget, GtkAccelGroup *accel_group,
297 guint accel_key, GdkModifierType accel_mods,
302 GtkWidget *wid = GTK_WIDGET(user_data);
304 if (gtk_signal_n_emissions_by_name(G_OBJECT(widget),
305 "remove_accelerator") > 2 )
307 gtk_widget_remove_accelerators(wid,"activate",FALSE);
311 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2)
313 g_signal_connect_after(G_OBJECT(widget), "add_accelerator",
314 G_CALLBACK(menu_item_add_accel), wid2);
315 g_signal_connect_after(G_OBJECT(widget), "remove_accelerator",
316 G_CALLBACK(menu_item_remove_accel), wid2);
319 void menu_connect_identical_items(void)
325 static const struct {
329 {"<Main>/Message/Reply", "<SummaryView>/Reply"},
330 {"<Main>/Message/Reply to/all", "<SummaryView>/Reply to/all"},
331 {"<Main>/Message/Reply to/sender", "<SummaryView>/Reply to/sender"},
332 {"<Main>/Message/Reply to/mailing list", "<SummaryView>/Reply to/mailing list"},
333 {"<Main>/Message/Follow-up and reply to", "<SummaryView>/Follow-up and reply to"},
334 {"<Main>/Message/Forward", "<SummaryView>/Forward"},
335 {"<Main>/Message/Redirect", "<SummaryView>/Redirect"},
336 {"<Main>/Message/Re-edit", "<SummaryView>/Re-edit"},
337 {"<Main>/Message/Move...", "<SummaryView>/Move..."},
338 {"<Main>/Message/Copy...", "<SummaryView>/Copy..."},
339 {"<Main>/Message/Delete", "<SummaryView>/Delete"},
340 {"<Main>/Message/Cancel a news message", "<SummaryView>/Cancel a news message"},
341 {"<Main>/Message/Mark/Mark", "<SummaryView>/Mark/Mark"},
342 {"<Main>/Message/Mark/Unmark", "<SummaryView>/Mark/Unmark"},
343 {"<Main>/Message/Mark/Mark as unread", "<SummaryView>/Mark/Mark as unread"},
344 {"<Main>/Message/Mark/Mark as read", "<SummaryView>/Mark/Mark as read"},
345 {"<Main>/Message/Mark/Mark all read", "<SummaryView>/Mark/Mark all read"},
346 {"<Main>/Tools/Add sender to address book", "<SummaryView>/Add sender to address book"},
347 {"<Main>/Tools/Create filter rule/Automatically", "<SummaryView>/Create filter rule/Automatically"},
348 {"<Main>/Tools/Create filter rule/by From", "<SummaryView>/Create filter rule/by From"},
349 {"<Main>/Tools/Create filter rule/by To", "<SummaryView>/Create filter rule/by To"},
350 {"<Main>/Tools/Create filter rule/by Subject", "<SummaryView>/Create filter rule/by Subject"},
351 {"<Main>/View/Open in new window", "<SummaryView>/View/Open in new window"},
352 {"<Main>/View/Message source", "<SummaryView>/View/Source"},
353 {"<Main>/View/Show all headers", "<SummaryView>/View/All header"},
356 const gint numpairs = sizeof pairs / sizeof pairs[0];
357 for (n = 0; n < numpairs; n++) {
358 /* get widgets from the paths */
360 item1 = gtk_item_factory_get_widget
361 (gtk_item_factory_from_path(pairs[n].path1),pairs[n].path1);
362 item2 = gtk_item_factory_get_widget
363 (gtk_item_factory_from_path(pairs[n].path2),pairs[n].path2);
365 if (item1 && item2) {
366 /* connect widgets both ways around */
367 connect_accel_change_signals(item2,item1);
368 connect_accel_change_signals(item1,item2);
370 if (!item1) debug_print(" ** Menu item not found: %s\n",pairs[n].path1);
371 if (!item2) debug_print(" ** Menu item not found: %s\n",pairs[n].path2);
376 void menu_select_by_data(GtkMenu *menu, gpointer data)
378 GList *children, *cur;
379 GtkWidget *select_item = NULL;
381 g_return_if_fail(menu != NULL);
383 children = gtk_container_children(GTK_CONTAINER(menu));
385 for (cur = children; cur != NULL; cur = g_list_next(cur)) {
386 GtkObject *child = G_OBJECT(cur->data);
388 if (gtk_object_get_user_data(child) == data) {
389 select_item = GTK_WIDGET(child);
392 if (select_item != NULL) {
393 gtk_menu_shell_select_item(GTK_MENU_SHELL(menu), select_item);
394 gtk_menu_shell_activate_item(GTK_MENU_SHELL(menu), select_item, FALSE);
397 g_list_free(children);