2004-09-02 [colin] 0.9.12cvs90
[claws.git] / src / gtk / menu.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <gtk/gtk.h>
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>
32
33 #include "intl.h"
34 #include "menu.h"
35 #include "utils.h"
36
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,
39                                  gpointer user_data);
40
41 static void menu_item_remove_accel(GtkWidget *widget, GtkAccelGroup *accel_group,
42                                    guint accel_key, GdkModifierType accel_mods,
43                                    gpointer user_data);
44
45 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2) ;
46
47
48 GtkWidget *menubar_create(GtkWidget *window, GtkItemFactoryEntry *entries,
49                           guint n_entries, const gchar *path, gpointer data)
50 {
51         GtkItemFactory *factory;
52
53         factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, path, NULL);
54         gtk_item_factory_set_translate_func(factory, menu_translate,
55                                             NULL, NULL);
56         gtk_item_factory_create_items(factory, n_entries, entries, data);
57         gtk_accel_group_attach(factory->accel_group, GTK_OBJECT(window));
58
59         return gtk_item_factory_get_widget(factory, path);
60 }
61
62 GtkWidget *menu_create_items(GtkItemFactoryEntry *entries,
63                              guint n_entries, const gchar *path,
64                              GtkItemFactory **factory, gpointer data)
65 {
66         *factory = gtk_item_factory_new(GTK_TYPE_MENU, path, NULL);
67         gtk_item_factory_set_translate_func(*factory, menu_translate,
68                                             NULL, NULL);
69         gtk_item_factory_create_items(*factory, n_entries, entries, data);
70
71         return gtk_item_factory_get_widget(*factory, path);
72 }
73
74 GtkWidget *popupmenu_create(GtkWidget *window, GtkItemFactoryEntry *entries,
75                              guint n_entries, const gchar *path, gpointer data)
76 {
77         GtkItemFactory *factory;
78         GtkAccelGroup *accel_group;
79
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,
83                                             NULL, NULL);
84         gtk_item_factory_create_items(factory, n_entries, entries, data);
85         gtk_accel_group_attach(accel_group, GTK_OBJECT(window));
86
87         return gtk_item_factory_get_widget(factory, path);
88 }
89
90 gchar *menu_translate(const gchar *path, gpointer data)
91 {
92         gchar *retval;
93
94         retval = gettext(path);
95
96         return retval;
97 }
98
99 static void factory_print_func(gpointer data, gchar *string)
100 {
101         GString *out_str = data;
102
103         g_string_append(out_str, string);
104         g_string_append_c(out_str, '\n');
105 }
106
107 GString *menu_factory_get_rc(const gchar *path)
108 {
109         GString *string;
110         GtkPatternSpec *pspec;
111         gchar pattern[256];
112
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,
118                                     string);
119         gtk_pattern_spec_free_segs(pspec);
120
121         return string;
122 }
123
124 void menu_factory_clear_rc(const gchar *rc_str)
125 {
126         GString *string;
127         gchar *p;
128         gchar *start, *end;
129         guint pos = 0;
130
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, '"');
135                 if (!p) continue;
136                 start = strchr(p + 1, '"');
137                 if (!start) continue;
138                 end = strchr(start + 1, '"');
139                 if (!end) continue;
140                 pos = start + 1 - string->str;
141                 if (end > start + 1)
142                         g_string_erase(string, pos, end - (start + 1));
143         }
144
145         gtk_item_factory_parse_rc_string(string->str);
146         g_string_free(string, TRUE);
147 }
148
149 void menu_factory_copy_rc(const gchar *src_path, const gchar *dest_path)
150 {
151         GString *string;
152         gint src_path_len;
153         gint dest_path_len;
154         gchar *p;
155         guint pos = 0;
156
157         string = menu_factory_get_rc(src_path);
158         src_path_len = strlen(src_path);
159         dest_path_len = strlen(dest_path);
160
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;
166         }
167
168         pos = 0;
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);
173         }
174
175         menu_factory_clear_rc(string->str);
176         gtk_item_factory_parse_rc_string(string->str);
177         g_string_free(string, TRUE);
178 }
179
180 void menu_set_sensitive(GtkItemFactory *ifactory, const gchar *path,
181                         gboolean sensitive)
182 {
183         GtkWidget *widget;
184
185         g_return_if_fail(ifactory != NULL);
186
187         widget = gtk_item_factory_get_item(ifactory, path);
188         if(widget == NULL) {
189                 debug_print("unknown menu entry %s\n", path);
190                 return;
191         }
192         gtk_widget_set_sensitive(widget, sensitive);
193 }
194
195 void menu_set_sensitive_all(GtkMenuShell *menu_shell, gboolean sensitive)
196 {
197         GList *cur;
198
199         for (cur = menu_shell->children; cur != NULL; cur = cur->next)
200                 gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
201 }
202
203 void menu_set_active(GtkItemFactory *ifactory, const gchar *path,
204                      gboolean is_active)
205 {
206         GtkWidget *widget;
207
208         g_return_if_fail(ifactory != NULL);
209
210         widget = gtk_item_factory_get_item(ifactory, path);
211         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), is_active);
212 }
213
214 void menu_button_position(GtkMenu *menu, gint *x, gint *y, gpointer user_data)
215 {
216         GtkWidget *button;
217         GtkRequisition requisition;
218         gint button_xpos, button_ypos;
219         gint xpos, ypos;
220         gint width, height;
221         gint scr_width, scr_height;
222
223         g_return_if_fail(user_data != NULL);
224         g_return_if_fail(GTK_IS_BUTTON(user_data));
225
226         button = GTK_WIDGET(user_data);
227
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);
232
233         xpos = button_xpos;
234         ypos = button_ypos + button->allocation.height;
235
236         scr_width = gdk_screen_width();
237         scr_height = gdk_screen_height();
238
239         if (xpos + width > scr_width)
240                 xpos -= (xpos + width) - scr_width;
241         if (ypos + height > scr_height)
242                 ypos = button_ypos - height;
243         if (xpos < 0)
244                 xpos = 0;
245         if (ypos < 0)
246                 ypos = 0;
247
248         *x = xpos;
249         *y = ypos;
250 }
251
252 gint menu_find_option_menu_index(GtkOptionMenu *optmenu, gpointer data,
253                                  GCompareFunc func)
254 {
255         GtkWidget *menu;
256         GtkWidget *menuitem;
257         gpointer menu_data;
258         GList *cur;
259         gint n;
260
261         menu = gtk_option_menu_get_menu(optmenu);
262
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));
267                 if (func) {
268                         if (func(menu_data, data) == 0)
269                                 return n;
270                 } else if (menu_data == data)
271                         return n;
272         }
273
274         return -1;
275 }
276
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,
280                                  gpointer user_data)
281 {
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,
291                                    GTK_ACCEL_VISIBLE );
292         gtk_accel_group_unlock_entry(accel_group,accel_key,accel_mods);                            
293 }
294
295 static void menu_item_remove_accel(GtkWidget *widget, GtkAccelGroup *accel_group,
296                                    guint accel_key, GdkModifierType accel_mods,
297                                    gpointer user_data)
298 {       
299         GtkWidget *wid = GTK_WIDGET(user_data);
300
301         if (gtk_signal_n_emissions_by_name(GTK_OBJECT(widget),
302             "remove_accelerator") > 2 )
303                 return;
304         gtk_widget_remove_accelerators(wid,"activate",FALSE);
305 }
306
307 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2) 
308 {
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);
313 }
314
315 void menu_connect_identical_items(void)
316 {
317         gint n;
318         GtkWidget *item1;
319         GtkWidget *item2;
320
321         static const struct {   
322                 const gchar *path1;
323                 const gchar *path2;
324         } pairs[] = {
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"},
350         };
351
352         const gint numpairs = sizeof pairs / sizeof pairs[0];
353         for (n = 0; n < numpairs; n++) {
354                 /* get widgets from the paths */
355
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);            
360
361                 if (item1 && item2) {
362                         /* connect widgets both ways around */
363                         connect_accel_change_signals(item2,item1);
364                         connect_accel_change_signals(item1,item2);
365                 } else { 
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);
368                 }                               
369         }
370 }
371
372 void menu_select_by_data(GtkMenu *menu, gpointer data)
373 {
374         GList *children, *cur;
375         GtkWidget *select_item = NULL;
376         
377         g_return_if_fail(menu != NULL);
378
379         children = gtk_container_children(GTK_CONTAINER(menu));
380
381         for (cur = children; cur != NULL; cur = g_list_next(cur)) {
382                 GtkObject *child = GTK_OBJECT(cur->data);
383
384                 if (gtk_object_get_user_data(child) == data) {
385                         select_item = GTK_WIDGET(child);
386                 }
387         }
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);
391         }
392
393         g_list_free(children);
394 }