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 gchar *menu_translate(const gchar *path, gpointer data);
40 static void menu_item_add_accel( GtkWidget *widget, guint accel_signal_id, GtkAccelGroup *accel_group,
41 guint accel_key, GdkModifierType accel_mods, GtkAccelFlags accel_flags,
44 static void menu_item_remove_accel(GtkWidget *widget, GtkAccelGroup *accel_group,
45 guint accel_key, GdkModifierType accel_mods,
48 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2) ;
51 GtkWidget *menubar_create(GtkWidget *window, GtkItemFactoryEntry *entries,
52 guint n_entries, const gchar *path, gpointer data)
54 GtkItemFactory *factory;
56 factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, path, NULL);
57 gtk_item_factory_set_translate_func(factory, menu_translate,
59 gtk_item_factory_create_items(factory, n_entries, entries, data);
60 gtk_window_add_accel_group (GTK_WINDOW (window), factory->accel_group);
62 return gtk_item_factory_get_widget(factory, path);
65 GtkWidget *menu_create_items(GtkItemFactoryEntry *entries,
66 guint n_entries, const gchar *path,
67 GtkItemFactory **factory, gpointer data)
69 *factory = gtk_item_factory_new(GTK_TYPE_MENU, path, NULL);
70 gtk_item_factory_set_translate_func(*factory, menu_translate,
72 gtk_item_factory_create_items(*factory, n_entries, entries, data);
74 return gtk_item_factory_get_widget(*factory, path);
77 GtkWidget *popupmenu_create(GtkWidget *window, GtkItemFactoryEntry *entries,
78 guint n_entries, const gchar *path, gpointer data)
80 GtkItemFactory *factory;
81 GtkAccelGroup *accel_group;
83 accel_group = gtk_accel_group_new();
84 factory = gtk_item_factory_new(GTK_TYPE_MENU, path, accel_group);
85 gtk_item_factory_set_translate_func(factory, menu_translate,
87 gtk_item_factory_create_items(factory, n_entries, entries, data);
88 gtk_window_add_accel_group(GTK_WINDOW (window), accel_group);
90 return gtk_item_factory_get_widget(factory, path);
93 static gchar *menu_translate(const gchar *path, gpointer data)
97 retval = gettext(path);
102 static void factory_print_func(gpointer data, gchar *string)
104 GString *out_str = data;
106 g_string_append(out_str, string);
107 g_string_append_c(out_str, '\n');
112 GString *menu_factory_get_rc(const gchar *path)
115 GtkPatternSpec *pspec;
118 pspec = g_new(GtkPatternSpec, 1);
119 g_snprintf(pattern, sizeof(pattern), "%s*", path);
120 gtk_pattern_spec_init(pspec, pattern);
121 string = g_string_new("");
122 gtk_item_factory_dump_items(pspec, FALSE, factory_print_func,
124 gtk_pattern_spec_free_segs(pspec);
129 void menu_factory_clear_rc(const gchar *rc_str)
136 string = g_string_new(rc_str);
137 while ((p = strstr(string->str + pos, "(menu-path \"")) != NULL) {
138 pos = p + 12 - string->str;
139 p = strchr(p + 12, '"');
141 start = strchr(p + 1, '"');
142 if (!start) continue;
143 end = strchr(start + 1, '"');
145 pos = start + 1 - string->str;
147 g_string_erase(string, pos, end - (start + 1));
150 gtk_item_factory_parse_rc_string(string->str);
151 g_string_free(string, TRUE);
154 void menu_factory_copy_rc(const gchar *src_path, const gchar *dest_path)
162 string = menu_factory_get_rc(src_path);
163 src_path_len = strlen(src_path);
164 dest_path_len = strlen(dest_path);
166 while ((p = strstr(string->str + pos, src_path)) != NULL) {
167 pos = p - string->str;
168 g_string_erase(string, pos, src_path_len);
169 g_string_insert(string, pos, dest_path);
170 pos += dest_path_len;
174 while ((p = strchr(string->str + pos, ';')) != NULL) {
175 pos = p - string->str;
176 if (pos == 0 || *(p - 1) == '\n')
177 g_string_erase(string, pos, 1);
180 menu_factory_clear_rc(string->str);
181 gtk_item_factory_parse_rc_string(string->str);
182 g_string_free(string, TRUE);
186 void menu_set_sensitive(GtkItemFactory *ifactory, const gchar *path,
191 g_return_if_fail(ifactory != NULL);
193 widget = gtk_item_factory_get_item(ifactory, path);
195 debug_print("unknown menu entry %s\n", path);
198 gtk_widget_set_sensitive(widget, sensitive);
201 void menu_set_sensitive_all(GtkMenuShell *menu_shell, gboolean sensitive)
205 for (cur = menu_shell->children; cur != NULL; cur = cur->next)
206 gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
209 void menu_set_toggle(GtkItemFactory *ifactory, const gchar *path,
214 g_return_if_fail(ifactory != NULL);
216 widget = gtk_item_factory_get_item(ifactory, path);
217 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(widget), active);
220 void menu_toggle_toggle(GtkItemFactory *ifactory, const gchar *path)
224 g_return_if_fail(ifactory != NULL);
226 widget = gtk_item_factory_get_item(ifactory, path);
227 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), !((GTK_CHECK_MENU_ITEM(widget))->active));
230 void menu_button_position(GtkMenu *menu, gint *x, gint *y, gboolean *push_in,
237 g_return_if_fail(x && y);
238 g_return_if_fail(GTK_IS_BUTTON(user_data));
240 widget = GTK_WIDGET(user_data);
242 gdk_window_get_origin(widget->window, x, y);
243 wheight = widget->requisition.height;
244 wx = widget->allocation.x;
245 wy = widget->allocation.y;
247 *y = *y + wy + wheight;
251 gint menu_find_option_menu_index(GtkOptionMenu *optmenu, gpointer data,
260 menu = gtk_option_menu_get_menu(optmenu);
262 for (cur = GTK_MENU_SHELL(menu)->children, n = 0;
263 cur != NULL; cur = cur->next, n++) {
264 menuitem = GTK_WIDGET(cur->data);
265 menu_data = g_object_get_data(G_OBJECT(menuitem),
268 if (func(menu_data, data) == 0)
270 } else if (menu_data == data)
277 /* call backs for accelerator changes on selected menu items */
278 static void menu_item_add_accel( GtkWidget *widget, guint accel_signal_id, GtkAccelGroup *accel_group,
279 guint accel_key, GdkModifierType accel_mods, GtkAccelFlags accel_flags,
284 GtkWidget *connected = GTK_WIDGET(user_data);
285 if (gtk_signal_n_emissions_by_name(GTK_OBJECT(widget),"add_accelerator") > 1 ) return;
286 gtk_widget_remove_accelerators(connected,"activate",FALSE);
287 /* lock _this_ widget */
288 gtk_accel_group_lock_entry(accel_group,accel_key,accel_mods);
289 /* modify the _other_ widget */
290 gtk_widget_add_accelerator(connected, "activate",
291 gtk_item_factory_from_widget(connected)->accel_group,
292 accel_key, accel_mods,
294 gtk_accel_group_unlock_entry(accel_group,accel_key,accel_mods);
298 static void menu_item_remove_accel(GtkWidget *widget, GtkAccelGroup *accel_group,
299 guint accel_key, GdkModifierType accel_mods,
304 GtkWidget *wid = GTK_WIDGET(user_data);
306 if (gtk_signal_n_emissions_by_name(GTK_OBJECT(widget),
307 "remove_accelerator") > 2 )
309 gtk_widget_remove_accelerators(wid,"activate",FALSE);
313 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2)
315 gtk_signal_connect_after(GTK_OBJECT(widget), "add_accelerator",
316 G_CALLBACK(menu_item_add_accel), wid2);
317 gtk_signal_connect_after(GTK_OBJECT(widget), "remove_accelerator",
318 G_CALLBACK(menu_item_remove_accel), wid2);
321 void menu_connect_identical_items(void)
327 static const struct {
331 {"<Main>/Message/Reply", "<SummaryView>/Reply"},
332 {"<Main>/Message/Reply to/all", "<SummaryView>/Reply to/all"},
333 {"<Main>/Message/Reply to/sender", "<SummaryView>/Reply to/sender"},
334 {"<Main>/Message/Reply to/mailing list", "<SummaryView>/Reply to/mailing list"},
335 {"<Main>/Message/Follow-up and reply to", "<SummaryView>/Follow-up and reply to"},
336 {"<Main>/Message/Forward", "<SummaryView>/Forward"},
337 {"<Main>/Message/Redirect", "<SummaryView>/Redirect"},
338 {"<Main>/Message/Re-edit", "<SummaryView>/Re-edit"},
339 {"<Main>/Message/Move...", "<SummaryView>/Move..."},
340 {"<Main>/Message/Copy...", "<SummaryView>/Copy..."},
341 {"<Main>/Message/Delete", "<SummaryView>/Delete"},
342 {"<Main>/Message/Cancel a news message", "<SummaryView>/Cancel a news message"},
343 {"<Main>/Tools/Execute", "<SummaryView>/Execute"},
344 {"<Main>/Message/Mark/Mark", "<SummaryView>/Mark/Mark"},
345 {"<Main>/Message/Mark/Unmark", "<SummaryView>/Mark/Unmark"},
346 {"<Main>/Message/Mark/Mark as unread", "<SummaryView>/Mark/Mark as unread"},
347 {"<Main>/Message/Mark/Mark as read", "<SummaryView>/Mark/Mark as read"},
348 {"<Main>/Message/Mark/Mark all read", "<SummaryView>/Mark/Mark all read"},
349 {"<Main>/Tools/Add sender to address book", "<SummaryView>/Add sender to address book"},
350 {"<Main>/Tools/Create filter rule/Automatically", "<SummaryView>/Create filter rule/Automatically"},
351 {"<Main>/Tools/Create filter rule/by From", "<SummaryView>/Create filter rule/by From"},
352 {"<Main>/Tools/Create filter rule/by To", "<SummaryView>/Create filter rule/by To"},
353 {"<Main>/Tools/Create filter rule/by Subject", "<SummaryView>/Create filter rule/by Subject"},
354 {"<Main>/View/Open in new window", "<SummaryView>/View/Open in new window"},
355 {"<Main>/View/Message source", "<SummaryView>/View/Source"},
356 {"<Main>/View/Show all headers", "<SummaryView>/View/All header"},
357 {"<Main>/File/Save as...", "<SummaryView>/Save as..."},
358 {"<Main>/File/Print...", "<SummaryView>/Print..."},
359 {"<Main>/Edit/Select all", "<SummaryView>/Select all"},
360 {"<Main>/Edit/Select thread", "<SummaryView>/Select thread"}
363 const gint numpairs = sizeof pairs / sizeof pairs[0];
364 for (n = 0; n < numpairs; n++) {
365 /* get widgets from the paths */
367 item1 = gtk_item_factory_get_widget
368 (gtk_item_factory_from_path(pairs[n].path1),pairs[n].path1);
369 item2 = gtk_item_factory_get_widget
370 (gtk_item_factory_from_path(pairs[n].path2),pairs[n].path2);
372 if (item1 && item2) {
373 /* connect widgets both ways around */
374 connect_accel_change_signals(item2,item1);
375 connect_accel_change_signals(item1,item2);
377 if (!item1) debug_print(" ** Menu item not found: %s\n",pairs[n].path1);
378 if (!item2) debug_print(" ** Menu item not found: %s\n",pairs[n].path2);
383 void menu_select_by_data(GtkMenu *menu, gpointer data)
385 GList *children, *cur;
386 GtkWidget *select_item = NULL;
388 g_return_if_fail(menu != NULL);
390 children = gtk_container_children(GTK_CONTAINER(menu));
392 for (cur = children; cur != NULL; cur = g_list_next(cur)) {
393 GtkObject *child = GTK_OBJECT(cur->data);
395 if (gtk_object_get_user_data(child) == data) {
396 select_item = GTK_WIDGET(child);
399 if (select_item != NULL) {
400 gtk_menu_shell_select_item(GTK_MENU_SHELL(menu), select_item);
401 gtk_menu_shell_activate_item(GTK_MENU_SHELL(menu), select_item, FALSE);
404 g_list_free(children);