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>
37 static gchar *menu_translate(const gchar *path, gpointer data);
39 static void menu_item_add_accel( GtkWidget *widget, guint accel_signal_id, GtkAccelGroup *accel_group,
40 guint accel_key, GdkModifierType accel_mods, GtkAccelFlags accel_flags,
43 static void menu_item_remove_accel(GtkWidget *widget, GtkAccelGroup *accel_group,
44 guint accel_key, GdkModifierType accel_mods,
47 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2) ;
50 GtkWidget *menubar_create(GtkWidget *window, GtkItemFactoryEntry *entries,
51 guint n_entries, const gchar *path, gpointer data)
53 GtkItemFactory *factory;
55 factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, path, NULL);
56 gtk_item_factory_set_translate_func(factory, menu_translate,
58 gtk_item_factory_create_items(factory, n_entries, entries, data);
59 gtk_accel_group_attach(factory->accel_group, GTK_OBJECT(window));
61 return gtk_item_factory_get_widget(factory, path);
64 GtkWidget *menu_create_items(GtkItemFactoryEntry *entries,
65 guint n_entries, const gchar *path,
66 GtkItemFactory **factory, gpointer data)
68 *factory = gtk_item_factory_new(GTK_TYPE_MENU, path, NULL);
69 gtk_item_factory_set_translate_func(*factory, menu_translate,
71 gtk_item_factory_create_items(*factory, n_entries, entries, data);
73 return gtk_item_factory_get_widget(*factory, path);
76 GtkWidget *popupmenu_create(GtkWidget *window, GtkItemFactoryEntry *entries,
77 guint n_entries, const gchar *path, gpointer data)
79 GtkItemFactory *factory;
80 GtkAccelGroup *accel_group;
82 accel_group = gtk_accel_group_new();
83 factory = gtk_item_factory_new(GTK_TYPE_MENU, path, accel_group);
84 gtk_item_factory_set_translate_func(factory, menu_translate,
86 gtk_item_factory_create_items(factory, n_entries, entries, data);
87 gtk_accel_group_attach(accel_group, GTK_OBJECT(window));
89 return gtk_item_factory_get_widget(factory, path);
92 static gchar *menu_translate(const gchar *path, gpointer data)
96 retval = gettext(path);
101 static void factory_print_func(gpointer data, gchar *string)
103 GString *out_str = data;
105 g_string_append(out_str, string);
106 g_string_append_c(out_str, '\n');
109 GString *menu_factory_get_rc(const gchar *path)
112 GtkPatternSpec *pspec;
115 pspec = g_new(GtkPatternSpec, 1);
116 g_snprintf(pattern, sizeof(pattern), "%s*", path);
117 gtk_pattern_spec_init(pspec, pattern);
118 string = g_string_new("");
119 gtk_item_factory_dump_items(pspec, FALSE, factory_print_func,
121 gtk_pattern_spec_free_segs(pspec);
126 void menu_factory_clear_rc(const gchar *rc_str)
133 string = g_string_new(rc_str);
134 while ((p = strstr(string->str + pos, "(menu-path \"")) != NULL) {
135 pos = p + 12 - string->str;
136 p = strchr(p + 12, '"');
138 start = strchr(p + 1, '"');
139 if (!start) continue;
140 end = strchr(start + 1, '"');
142 pos = start + 1 - string->str;
144 g_string_erase(string, pos, end - (start + 1));
147 gtk_item_factory_parse_rc_string(string->str);
148 g_string_free(string, TRUE);
151 void menu_factory_copy_rc(const gchar *src_path, const gchar *dest_path)
159 string = menu_factory_get_rc(src_path);
160 src_path_len = strlen(src_path);
161 dest_path_len = strlen(dest_path);
163 while ((p = strstr(string->str + pos, src_path)) != NULL) {
164 pos = p - string->str;
165 g_string_erase(string, pos, src_path_len);
166 g_string_insert(string, pos, dest_path);
167 pos += dest_path_len;
171 while ((p = strchr(string->str + pos, ';')) != NULL) {
172 pos = p - string->str;
173 if (pos == 0 || *(p - 1) == '\n')
174 g_string_erase(string, pos, 1);
177 menu_factory_clear_rc(string->str);
178 gtk_item_factory_parse_rc_string(string->str);
179 g_string_free(string, TRUE);
182 void menu_set_sensitive(GtkItemFactory *ifactory, const gchar *path,
187 g_return_if_fail(ifactory != NULL);
189 widget = gtk_item_factory_get_item(ifactory, path);
191 debug_print("unknown menu entry %s\n", path);
194 gtk_widget_set_sensitive(widget, sensitive);
197 void menu_set_sensitive_all(GtkMenuShell *menu_shell, gboolean sensitive)
201 for (cur = menu_shell->children; cur != NULL; cur = cur->next)
202 gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
205 void menu_set_toggle(GtkItemFactory *ifactory, const gchar *path,
210 g_return_if_fail(ifactory != NULL);
212 widget = gtk_item_factory_get_item(ifactory, path);
213 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(widget), active);
216 void menu_toggle_toggle(GtkItemFactory *ifactory, const gchar *path)
220 g_return_if_fail(ifactory != NULL);
222 widget = gtk_item_factory_get_item(ifactory, path);
223 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), !((GTK_CHECK_MENU_ITEM(widget))->active));
226 void menu_button_position(GtkMenu *menu, gint *x, gint *y, gpointer user_data)
229 GtkRequisition requisition;
230 gint button_xpos, button_ypos;
233 gint scr_width, scr_height;
235 g_return_if_fail(user_data != NULL);
236 g_return_if_fail(GTK_IS_BUTTON(user_data));
238 button = GTK_WIDGET(user_data);
240 gtk_widget_get_child_requisition(GTK_WIDGET(menu), &requisition);
241 width = requisition.width;
242 height = requisition.height;
243 gdk_window_get_origin(button->window, &button_xpos, &button_ypos);
246 ypos = button_ypos + button->allocation.height;
248 scr_width = gdk_screen_width();
249 scr_height = gdk_screen_height();
251 if (xpos + width > scr_width)
252 xpos -= (xpos + width) - scr_width;
253 if (ypos + height > scr_height)
254 ypos = button_ypos - height;
264 gint menu_find_option_menu_index(GtkOptionMenu *optmenu, gpointer data,
273 menu = gtk_option_menu_get_menu(optmenu);
275 for (cur = GTK_MENU_SHELL(menu)->children, n = 0;
276 cur != NULL; cur = cur->next, n++) {
277 menuitem = GTK_WIDGET(cur->data);
278 menu_data = gtk_object_get_user_data(GTK_OBJECT(menuitem));
280 if (func(menu_data, data) == 0)
282 } else if (menu_data == data)
289 /* call backs for accelerator changes on selected menu items */
290 static void menu_item_add_accel( GtkWidget *widget, guint accel_signal_id, GtkAccelGroup *accel_group,
291 guint accel_key, GdkModifierType accel_mods, GtkAccelFlags accel_flags,
294 GtkWidget *connected = GTK_WIDGET(user_data);
295 if (gtk_signal_n_emissions_by_name(GTK_OBJECT(widget),"add_accelerator") > 1 ) return;
296 gtk_widget_remove_accelerators(connected,"activate",FALSE);
297 /* lock _this_ widget */
298 gtk_accel_group_lock_entry(accel_group,accel_key,accel_mods);
299 /* modify the _other_ widget */
300 gtk_widget_add_accelerator(connected, "activate",
301 gtk_item_factory_from_widget(connected)->accel_group,
302 accel_key, accel_mods,
304 gtk_accel_group_unlock_entry(accel_group,accel_key,accel_mods);
307 static void menu_item_remove_accel(GtkWidget *widget, GtkAccelGroup *accel_group,
308 guint accel_key, GdkModifierType accel_mods,
311 GtkWidget *wid = GTK_WIDGET(user_data);
313 if (gtk_signal_n_emissions_by_name(GTK_OBJECT(widget),
314 "remove_accelerator") > 2 )
316 gtk_widget_remove_accelerators(wid,"activate",FALSE);
319 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2)
321 gtk_signal_connect_after(GTK_OBJECT(widget), "add_accelerator",
322 menu_item_add_accel, wid2);
323 gtk_signal_connect_after(GTK_OBJECT(widget), "remove_accelerator",
324 menu_item_remove_accel, wid2);
327 void menu_connect_identical_items(void)
333 static const struct {
337 {"<Main>/Message/Reply", "<SummaryView>/Reply"},
338 {"<Main>/Message/Reply to/all", "<SummaryView>/Reply to/all"},
339 {"<Main>/Message/Reply to/sender", "<SummaryView>/Reply to/sender"},
340 {"<Main>/Message/Reply to/mailing list", "<SummaryView>/Reply to/mailing list"},
341 {"<Main>/Message/Follow-up and reply to", "<SummaryView>/Follow-up and reply to"},
342 {"<Main>/Message/Forward", "<SummaryView>/Forward"},
343 {"<Main>/Message/Redirect", "<SummaryView>/Redirect"},
344 {"<Main>/Message/Re-edit", "<SummaryView>/Re-edit"},
345 {"<Main>/Message/Move...", "<SummaryView>/Move..."},
346 {"<Main>/Message/Copy...", "<SummaryView>/Copy..."},
347 {"<Main>/Message/Delete", "<SummaryView>/Delete"},
348 {"<Main>/Message/Cancel a news message", "<SummaryView>/Cancel a news message"},
349 {"<Main>/Tools/Execute", "<SummaryView>/Execute"},
350 {"<Main>/Message/Mark/Mark", "<SummaryView>/Mark/Mark"},
351 {"<Main>/Message/Mark/Unmark", "<SummaryView>/Mark/Unmark"},
352 {"<Main>/Message/Mark/Mark as unread", "<SummaryView>/Mark/Mark as unread"},
353 {"<Main>/Message/Mark/Mark as read", "<SummaryView>/Mark/Mark as read"},
354 {"<Main>/Message/Mark/Mark all read", "<SummaryView>/Mark/Mark all read"},
355 {"<Main>/Tools/Add sender to address book", "<SummaryView>/Add sender to address book"},
356 {"<Main>/Tools/Create filter rule/Automatically", "<SummaryView>/Create filter rule/Automatically"},
357 {"<Main>/Tools/Create filter rule/by From", "<SummaryView>/Create filter rule/by From"},
358 {"<Main>/Tools/Create filter rule/by To", "<SummaryView>/Create filter rule/by To"},
359 {"<Main>/Tools/Create filter rule/by Subject", "<SummaryView>/Create filter rule/by Subject"},
360 {"<Main>/View/Open in new window", "<SummaryView>/View/Open in new window"},
361 {"<Main>/View/Message source", "<SummaryView>/View/Source"},
362 {"<Main>/View/Show all headers", "<SummaryView>/View/All header"},
363 {"<Main>/File/Save as...", "<SummaryView>/Save as..."},
364 {"<Main>/File/Print...", "<SummaryView>/Print..."},
365 {"<Main>/Edit/Select all", "<SummaryView>/Select all"},
366 {"<Main>/Edit/Select thread", "<SummaryView>/Select thread"}
369 const gint numpairs = sizeof pairs / sizeof pairs[0];
370 for (n = 0; n < numpairs; n++) {
371 /* get widgets from the paths */
373 item1 = gtk_item_factory_get_widget
374 (gtk_item_factory_from_path(pairs[n].path1),pairs[n].path1);
375 item2 = gtk_item_factory_get_widget
376 (gtk_item_factory_from_path(pairs[n].path2),pairs[n].path2);
378 if (item1 && item2) {
379 /* connect widgets both ways around */
380 connect_accel_change_signals(item2,item1);
381 connect_accel_change_signals(item1,item2);
383 if (!item1) debug_print(" ** Menu item not found: %s\n",pairs[n].path1);
384 if (!item2) debug_print(" ** Menu item not found: %s\n",pairs[n].path2);
389 void menu_select_by_data(GtkMenu *menu, gpointer data)
391 GList *children, *cur;
392 GtkWidget *select_item = NULL;
394 g_return_if_fail(menu != NULL);
396 children = gtk_container_children(GTK_CONTAINER(menu));
398 for (cur = children; cur != NULL; cur = g_list_next(cur)) {
399 GtkObject *child = GTK_OBJECT(cur->data);
401 if (gtk_object_get_user_data(child) == data) {
402 select_item = GTK_WIDGET(child);
405 if (select_item != NULL) {
406 gtk_menu_shell_select_item(GTK_MENU_SHELL(menu), select_item);
407 gtk_menu_shell_activate_item(GTK_MENU_SHELL(menu), select_item, FALSE);
410 g_list_free(children);