eec7bb4b3f3fd99783c85fc459b379cb33c03597
[claws.git] / src / gtk / menu.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 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/gtkitemfactory.h>
30 #include <gtk/gtkcheckmenuitem.h>
31 #include <gtk/gtkbutton.h>
32
33 #include "intl.h"
34 #include "menu.h"
35 #include "utils.h"
36
37 static gchar *menu_translate(const gchar *path, gpointer data);
38
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,
41                                  gpointer user_data);
42
43 static void menu_item_remove_accel(GtkWidget *widget, GtkAccelGroup *accel_group,
44                                    guint accel_key, GdkModifierType accel_mods,
45                                    gpointer user_data);
46
47 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2) ;
48
49
50 GtkWidget *menubar_create(GtkWidget *window, GtkItemFactoryEntry *entries,
51                           guint n_entries, const gchar *path, gpointer data)
52 {
53         GtkItemFactory *factory;
54
55         factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, path, NULL);
56         gtk_item_factory_set_translate_func(factory, menu_translate,
57                                             NULL, NULL);
58         gtk_item_factory_create_items(factory, n_entries, entries, data);
59         gtk_accel_group_attach(factory->accel_group, GTK_OBJECT(window));
60
61         return gtk_item_factory_get_widget(factory, path);
62 }
63
64 GtkWidget *menu_create_items(GtkItemFactoryEntry *entries,
65                              guint n_entries, const gchar *path,
66                              GtkItemFactory **factory, gpointer data)
67 {
68         *factory = gtk_item_factory_new(GTK_TYPE_MENU, path, NULL);
69         gtk_item_factory_set_translate_func(*factory, menu_translate,
70                                             NULL, NULL);
71         gtk_item_factory_create_items(*factory, n_entries, entries, data);
72
73         return gtk_item_factory_get_widget(*factory, path);
74 }
75
76 GtkWidget *popupmenu_create(GtkWidget *window, GtkItemFactoryEntry *entries,
77                              guint n_entries, const gchar *path, gpointer data)
78 {
79         GtkItemFactory *factory;
80         GtkAccelGroup *accel_group;
81
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,
85                                             NULL, NULL);
86         gtk_item_factory_create_items(factory, n_entries, entries, data);
87         gtk_accel_group_attach(accel_group, GTK_OBJECT(window));
88
89         return gtk_item_factory_get_widget(factory, path);
90 }
91
92 static gchar *menu_translate(const gchar *path, gpointer data)
93 {
94         gchar *retval;
95
96         retval = gettext(path);
97
98         return retval;
99 }
100
101 static void factory_print_func(gpointer data, gchar *string)
102 {
103         GString *out_str = data;
104
105         g_string_append(out_str, string);
106         g_string_append_c(out_str, '\n');
107 }
108
109 GString *menu_factory_get_rc(const gchar *path)
110 {
111         GString *string;
112         GtkPatternSpec *pspec;
113         gchar pattern[256];
114
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,
120                                     string);
121         gtk_pattern_spec_free_segs(pspec);
122
123         return string;
124 }
125
126 void menu_factory_clear_rc(const gchar *rc_str)
127 {
128         GString *string;
129         gchar *p;
130         gchar *start, *end;
131         guint pos = 0;
132
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, '"');
137                 if (!p) continue;
138                 start = strchr(p + 1, '"');
139                 if (!start) continue;
140                 end = strchr(start + 1, '"');
141                 if (!end) continue;
142                 pos = start + 1 - string->str;
143                 if (end > start + 1)
144                         g_string_erase(string, pos, end - (start + 1));
145         }
146
147         gtk_item_factory_parse_rc_string(string->str);
148         g_string_free(string, TRUE);
149 }
150
151 void menu_factory_copy_rc(const gchar *src_path, const gchar *dest_path)
152 {
153         GString *string;
154         gint src_path_len;
155         gint dest_path_len;
156         gchar *p;
157         guint pos = 0;
158
159         string = menu_factory_get_rc(src_path);
160         src_path_len = strlen(src_path);
161         dest_path_len = strlen(dest_path);
162
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;
168         }
169
170         pos = 0;
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);
175         }
176
177         menu_factory_clear_rc(string->str);
178         gtk_item_factory_parse_rc_string(string->str);
179         g_string_free(string, TRUE);
180 }
181
182 void menu_set_sensitive(GtkItemFactory *ifactory, const gchar *path,
183                         gboolean sensitive)
184 {
185         GtkWidget *widget;
186
187         g_return_if_fail(ifactory != NULL);
188
189         widget = gtk_item_factory_get_item(ifactory, path);
190         if(widget == NULL) {
191                 debug_print("unknown menu entry %s\n", path);
192                 return;
193         }
194         gtk_widget_set_sensitive(widget, sensitive);
195 }
196
197 void menu_set_sensitive_all(GtkMenuShell *menu_shell, gboolean sensitive)
198 {
199         GList *cur;
200
201         for (cur = menu_shell->children; cur != NULL; cur = cur->next)
202                 gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
203 }
204
205 void menu_set_toggle(GtkItemFactory *ifactory, const gchar *path,
206                         gboolean active)
207 {
208         GtkWidget *widget;
209
210         g_return_if_fail(ifactory != NULL);
211
212         widget = gtk_item_factory_get_item(ifactory, path);
213         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(widget), active);
214 }
215
216 void menu_toggle_toggle(GtkItemFactory *ifactory, const gchar *path)
217 {
218         GtkWidget *widget;
219         
220         g_return_if_fail(ifactory != NULL);
221         
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));
224 }
225
226 void menu_button_position(GtkMenu *menu, gint *x, gint *y, gpointer user_data)
227 {
228         GtkWidget *button;
229         GtkRequisition requisition;
230         gint button_xpos, button_ypos;
231         gint xpos, ypos;
232         gint width, height;
233         gint scr_width, scr_height;
234
235         g_return_if_fail(user_data != NULL);
236         g_return_if_fail(GTK_IS_BUTTON(user_data));
237
238         button = GTK_WIDGET(user_data);
239
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);
244
245         xpos = button_xpos;
246         ypos = button_ypos + button->allocation.height;
247
248         scr_width = gdk_screen_width();
249         scr_height = gdk_screen_height();
250
251         if (xpos + width > scr_width)
252                 xpos -= (xpos + width) - scr_width;
253         if (ypos + height > scr_height)
254                 ypos = button_ypos - height;
255         if (xpos < 0)
256                 xpos = 0;
257         if (ypos < 0)
258                 ypos = 0;
259
260         *x = xpos;
261         *y = ypos;
262 }
263
264 gint menu_find_option_menu_index(GtkOptionMenu *optmenu, gpointer data,
265                                  GCompareFunc func)
266 {
267         GtkWidget *menu;
268         GtkWidget *menuitem;
269         gpointer menu_data;
270         GList *cur;
271         gint n;
272
273         menu = gtk_option_menu_get_menu(optmenu);
274
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));
279                 if (func) {
280                         if (func(menu_data, data) == 0)
281                                 return n;
282                 } else if (menu_data == data)
283                         return n;
284         }
285
286         return -1;
287 }
288
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,
292                                  gpointer user_data)
293 {
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,
303                                    GTK_ACCEL_VISIBLE );
304         gtk_accel_group_unlock_entry(accel_group,accel_key,accel_mods);                            
305 }
306
307 static void menu_item_remove_accel(GtkWidget *widget, GtkAccelGroup *accel_group,
308                                    guint accel_key, GdkModifierType accel_mods,
309                                    gpointer user_data)
310 {       
311         GtkWidget *wid = GTK_WIDGET(user_data);
312
313         if (gtk_signal_n_emissions_by_name(GTK_OBJECT(widget),
314             "remove_accelerator") > 2 )
315                 return;
316         gtk_widget_remove_accelerators(wid,"activate",FALSE);
317 }
318
319 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2) 
320 {
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);
325 }
326
327 void menu_connect_identical_items(void)
328 {
329         gint n;
330         GtkWidget *item1;
331         GtkWidget *item2;
332
333         static const struct {   
334                 const gchar *path1;
335                 const gchar *path2;
336         } pairs[] = {
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"}           
367         };
368
369         const gint numpairs = sizeof pairs / sizeof pairs[0];
370         for (n = 0; n < numpairs; n++) {
371                 /* get widgets from the paths */
372
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);            
377
378                 if (item1 && item2) {
379                         /* connect widgets both ways around */
380                         connect_accel_change_signals(item2,item1);
381                         connect_accel_change_signals(item1,item2);
382                 } else { 
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);
385                 }                               
386         }
387 }
388
389 void menu_select_by_data(GtkMenu *menu, gpointer data)
390 {
391         GList *children, *cur;
392         GtkWidget *select_item = NULL;
393         
394         g_return_if_fail(menu != NULL);
395
396         children = gtk_container_children(GTK_CONTAINER(menu));
397
398         for (cur = children; cur != NULL; cur = g_list_next(cur)) {
399                 GtkObject *child = GTK_OBJECT(cur->data);
400
401                 if (gtk_object_get_user_data(child) == data) {
402                         select_item = GTK_WIDGET(child);
403                 }
404         }
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);
408         }
409
410         g_list_free(children);
411 }