2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2004 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/gtkcheckmenuitem.h>
30 #include <gtk/gtkitemfactory.h>
31 #include <gtk/gtkbutton.h>
37 static void menu_item_add_accel( GtkWidget *widget, guint accel_signal_id, GtkAccelGroup *accel_group,
38 guint accel_key, GdkModifierType accel_mods, GtkAccelFlags accel_flags,
41 static void menu_item_remove_accel(GtkWidget *widget, GtkAccelGroup *accel_group,
42 guint accel_key, GdkModifierType accel_mods,
45 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2) ;
48 GtkWidget *menubar_create(GtkWidget *window, GtkItemFactoryEntry *entries,
49 guint n_entries, const gchar *path, gpointer data)
51 GtkItemFactory *factory;
53 factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, path, NULL);
54 gtk_item_factory_set_translate_func(factory, menu_translate,
56 gtk_item_factory_create_items(factory, n_entries, entries, data);
57 gtk_accel_group_attach(factory->accel_group, GTK_OBJECT(window));
59 return gtk_item_factory_get_widget(factory, path);
62 GtkWidget *menu_create_items(GtkItemFactoryEntry *entries,
63 guint n_entries, const gchar *path,
64 GtkItemFactory **factory, gpointer data)
66 *factory = gtk_item_factory_new(GTK_TYPE_MENU, path, NULL);
67 gtk_item_factory_set_translate_func(*factory, menu_translate,
69 gtk_item_factory_create_items(*factory, n_entries, entries, data);
71 return gtk_item_factory_get_widget(*factory, path);
74 GtkWidget *popupmenu_create(GtkWidget *window, GtkItemFactoryEntry *entries,
75 guint n_entries, const gchar *path, gpointer data)
77 GtkItemFactory *factory;
78 GtkAccelGroup *accel_group;
80 accel_group = gtk_accel_group_new();
81 factory = gtk_item_factory_new(GTK_TYPE_MENU, path, accel_group);
82 gtk_item_factory_set_translate_func(factory, menu_translate,
84 gtk_item_factory_create_items(factory, n_entries, entries, data);
85 gtk_accel_group_attach(accel_group, GTK_OBJECT(window));
87 return gtk_item_factory_get_widget(factory, path);
90 gchar *menu_translate(const gchar *path, gpointer data)
94 retval = gettext(path);
99 static void factory_print_func(gpointer data, gchar *string)
101 GString *out_str = data;
103 g_string_append(out_str, string);
104 g_string_append_c(out_str, '\n');
107 GString *menu_factory_get_rc(const gchar *path)
110 GtkPatternSpec *pspec;
113 pspec = g_new(GtkPatternSpec, 1);
114 g_snprintf(pattern, sizeof(pattern), "%s*", path);
115 gtk_pattern_spec_init(pspec, pattern);
116 string = g_string_new("");
117 gtk_item_factory_dump_items(pspec, FALSE, factory_print_func,
119 gtk_pattern_spec_free_segs(pspec);
124 void menu_factory_clear_rc(const gchar *rc_str)
131 string = g_string_new(rc_str);
132 while ((p = strstr(string->str + pos, "(menu-path \"")) != NULL) {
133 pos = p + 12 - string->str;
134 p = strchr(p + 12, '"');
136 start = strchr(p + 1, '"');
137 if (!start) continue;
138 end = strchr(start + 1, '"');
140 pos = start + 1 - string->str;
142 g_string_erase(string, pos, end - (start + 1));
145 gtk_item_factory_parse_rc_string(string->str);
146 g_string_free(string, TRUE);
149 void menu_factory_copy_rc(const gchar *src_path, const gchar *dest_path)
157 string = menu_factory_get_rc(src_path);
158 src_path_len = strlen(src_path);
159 dest_path_len = strlen(dest_path);
161 while ((p = strstr(string->str + pos, src_path)) != NULL) {
162 pos = p - string->str;
163 g_string_erase(string, pos, src_path_len);
164 g_string_insert(string, pos, dest_path);
165 pos += dest_path_len;
169 while ((p = strchr(string->str + pos, ';')) != NULL) {
170 pos = p - string->str;
171 if (pos == 0 || *(p - 1) == '\n')
172 g_string_erase(string, pos, 1);
175 menu_factory_clear_rc(string->str);
176 gtk_item_factory_parse_rc_string(string->str);
177 g_string_free(string, TRUE);
180 void menu_set_sensitive(GtkItemFactory *ifactory, const gchar *path,
185 g_return_if_fail(ifactory != NULL);
187 widget = gtk_item_factory_get_item(ifactory, path);
189 debug_print("unknown menu entry %s\n", path);
192 gtk_widget_set_sensitive(widget, sensitive);
195 void menu_set_sensitive_all(GtkMenuShell *menu_shell, gboolean sensitive)
199 for (cur = menu_shell->children; cur != NULL; cur = cur->next)
200 gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
203 void menu_set_active(GtkItemFactory *ifactory, const gchar *path,
208 g_return_if_fail(ifactory != NULL);
210 widget = gtk_item_factory_get_item(ifactory, path);
211 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), is_active);
214 void menu_button_position(GtkMenu *menu, gint *x, gint *y, gpointer user_data)
217 GtkRequisition requisition;
218 gint button_xpos, button_ypos;
221 gint scr_width, scr_height;
223 g_return_if_fail(user_data != NULL);
224 g_return_if_fail(GTK_IS_BUTTON(user_data));
226 button = GTK_WIDGET(user_data);
228 gtk_widget_get_child_requisition(GTK_WIDGET(menu), &requisition);
229 width = requisition.width;
230 height = requisition.height;
231 gdk_window_get_origin(button->window, &button_xpos, &button_ypos);
234 ypos = button_ypos + button->allocation.height;
236 scr_width = gdk_screen_width();
237 scr_height = gdk_screen_height();
239 if (xpos + width > scr_width)
240 xpos -= (xpos + width) - scr_width;
241 if (ypos + height > scr_height)
242 ypos = button_ypos - height;
252 gint menu_find_option_menu_index(GtkOptionMenu *optmenu, gpointer data,
261 menu = gtk_option_menu_get_menu(optmenu);
263 for (cur = GTK_MENU_SHELL(menu)->children, n = 0;
264 cur != NULL; cur = cur->next, n++) {
265 menuitem = GTK_WIDGET(cur->data);
266 menu_data = gtk_object_get_user_data(GTK_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,
282 GtkWidget *connected = GTK_WIDGET(user_data);
283 if (gtk_signal_n_emissions_by_name(GTK_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);
295 static void menu_item_remove_accel(GtkWidget *widget, GtkAccelGroup *accel_group,
296 guint accel_key, GdkModifierType accel_mods,
299 GtkWidget *wid = GTK_WIDGET(user_data);
301 if (gtk_signal_n_emissions_by_name(GTK_OBJECT(widget),
302 "remove_accelerator") > 2 )
304 gtk_widget_remove_accelerators(wid,"activate",FALSE);
307 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2)
309 gtk_signal_connect_after(GTK_OBJECT(widget), "add_accelerator",
310 menu_item_add_accel, wid2);
311 gtk_signal_connect_after(GTK_OBJECT(widget), "remove_accelerator",
312 menu_item_remove_accel, wid2);
315 void menu_connect_identical_items(void)
321 static const struct {
325 {"<Main>/Message/Reply", "<SummaryView>/Reply"},
326 {"<Main>/Message/Reply to/all", "<SummaryView>/Reply to/all"},
327 {"<Main>/Message/Reply to/sender", "<SummaryView>/Reply to/sender"},
328 {"<Main>/Message/Reply to/mailing list", "<SummaryView>/Reply to/mailing list"},
329 {"<Main>/Message/Follow-up and reply to", "<SummaryView>/Follow-up and reply to"},
330 {"<Main>/Message/Forward", "<SummaryView>/Forward"},
331 {"<Main>/Message/Redirect", "<SummaryView>/Redirect"},
332 {"<Main>/Message/Re-edit", "<SummaryView>/Re-edit"},
333 {"<Main>/Message/Move...", "<SummaryView>/Move..."},
334 {"<Main>/Message/Copy...", "<SummaryView>/Copy..."},
335 {"<Main>/Message/Delete", "<SummaryView>/Delete"},
336 {"<Main>/Message/Cancel a news message", "<SummaryView>/Cancel a news message"},
337 {"<Main>/Message/Mark/Mark", "<SummaryView>/Mark/Mark"},
338 {"<Main>/Message/Mark/Unmark", "<SummaryView>/Mark/Unmark"},
339 {"<Main>/Message/Mark/Mark as unread", "<SummaryView>/Mark/Mark as unread"},
340 {"<Main>/Message/Mark/Mark as read", "<SummaryView>/Mark/Mark as read"},
341 {"<Main>/Message/Mark/Mark all read", "<SummaryView>/Mark/Mark all read"},
342 {"<Main>/Tools/Add sender to address book", "<SummaryView>/Add sender to address book"},
343 {"<Main>/Tools/Create filter rule/Automatically", "<SummaryView>/Create filter rule/Automatically"},
344 {"<Main>/Tools/Create filter rule/by From", "<SummaryView>/Create filter rule/by From"},
345 {"<Main>/Tools/Create filter rule/by To", "<SummaryView>/Create filter rule/by To"},
346 {"<Main>/Tools/Create filter rule/by Subject", "<SummaryView>/Create filter rule/by Subject"},
347 {"<Main>/View/Open in new window", "<SummaryView>/View/Open in new window"},
348 {"<Main>/View/Message source", "<SummaryView>/View/Source"},
349 {"<Main>/View/Show all headers", "<SummaryView>/View/All header"},
352 const gint numpairs = sizeof pairs / sizeof pairs[0];
353 for (n = 0; n < numpairs; n++) {
354 /* get widgets from the paths */
356 item1 = gtk_item_factory_get_widget
357 (gtk_item_factory_from_path(pairs[n].path1),pairs[n].path1);
358 item2 = gtk_item_factory_get_widget
359 (gtk_item_factory_from_path(pairs[n].path2),pairs[n].path2);
361 if (item1 && item2) {
362 /* connect widgets both ways around */
363 connect_accel_change_signals(item2,item1);
364 connect_accel_change_signals(item1,item2);
366 if (!item1) debug_print(" ** Menu item not found: %s\n",pairs[n].path1);
367 if (!item2) debug_print(" ** Menu item not found: %s\n",pairs[n].path2);
372 void menu_select_by_data(GtkMenu *menu, gpointer data)
374 GList *children, *cur;
375 GtkWidget *select_item = NULL;
377 g_return_if_fail(menu != NULL);
379 children = gtk_container_children(GTK_CONTAINER(menu));
381 for (cur = children; cur != NULL; cur = g_list_next(cur)) {
382 GtkObject *child = GTK_OBJECT(cur->data);
384 if (gtk_object_get_user_data(child) == data) {
385 select_item = GTK_WIDGET(child);
388 if (select_item != NULL) {
389 gtk_menu_shell_select_item(GTK_MENU_SHELL(menu), select_item);
390 gtk_menu_shell_activate_item(GTK_MENU_SHELL(menu), select_item, FALSE);
393 g_list_free(children);