03856a724e90749f33e8b4379c9193c0e6049e55
[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 #include <gtk/gtkwindow.h>
33
34 #include "intl.h"
35 #include "menu.h"
36 #include "utils.h"
37
38 static void menu_item_add_accel( GtkWidget *widget, guint accel_signal_id, GtkAccelGroup *accel_group,
39                                  guint accel_key, GdkModifierType accel_mods, GtkAccelFlags accel_flags,
40                                  gpointer user_data);
41
42 static void menu_item_remove_accel(GtkWidget *widget, GtkAccelGroup *accel_group,
43                                    guint accel_key, GdkModifierType accel_mods,
44                                    gpointer user_data);
45
46 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2) ;
47
48
49 GtkWidget *menubar_create(GtkWidget *window, GtkItemFactoryEntry *entries,
50                           guint n_entries, const gchar *path, gpointer data)
51 {
52         GtkItemFactory *factory;
53
54         factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, path, NULL);
55         gtk_item_factory_set_translate_func(factory, menu_translate,
56                                             NULL, NULL);
57         gtk_item_factory_create_items(factory, n_entries, entries, data);
58         gtk_window_add_accel_group (GTK_WINDOW (window), factory->accel_group);
59
60         return gtk_item_factory_get_widget(factory, path);
61 }
62
63 GtkWidget *menu_create_items(GtkItemFactoryEntry *entries,
64                              guint n_entries, const gchar *path,
65                              GtkItemFactory **factory, gpointer data)
66 {
67         *factory = gtk_item_factory_new(GTK_TYPE_MENU, path, NULL);
68         gtk_item_factory_set_translate_func(*factory, menu_translate,
69                                             NULL, NULL);
70         gtk_item_factory_create_items(*factory, n_entries, entries, data);
71
72         return gtk_item_factory_get_widget(*factory, path);
73 }
74
75 GtkWidget *popupmenu_create(GtkWidget *window, GtkItemFactoryEntry *entries,
76                              guint n_entries, const gchar *path, gpointer data)
77 {
78         GtkItemFactory *factory;
79         GtkAccelGroup *accel_group;
80
81         accel_group = gtk_accel_group_new();
82         factory = gtk_item_factory_new(GTK_TYPE_MENU, path, accel_group);
83         gtk_item_factory_set_translate_func(factory, menu_translate,
84                                             NULL, NULL);
85         gtk_item_factory_create_items(factory, n_entries, entries, data);
86         gtk_window_add_accel_group(GTK_WINDOW (window), accel_group);
87
88         return gtk_item_factory_get_widget(factory, path);
89 }
90
91 gchar *menu_translate(const gchar *path, gpointer data)
92 {
93         gchar *retval;
94
95         retval = gettext(path);
96
97         return retval;
98 }
99
100 static void factory_print_func(gpointer data, gchar *string)
101 {
102         GString *out_str = data;
103
104         g_string_append(out_str, string);
105         g_string_append_c(out_str, '\n');
106 }
107
108 #warning FIXME_GTK2
109 #if 0
110 GString *menu_factory_get_rc(const gchar *path)
111 {
112         GString *string;
113         GtkPatternSpec *pspec;
114         gchar pattern[256];
115
116         pspec = g_new(GtkPatternSpec, 1);
117         g_snprintf(pattern, sizeof(pattern), "%s*", path);
118         gtk_pattern_spec_init(pspec, pattern);
119         string = g_string_new("");
120         gtk_item_factory_dump_items(pspec, FALSE, factory_print_func,
121                                     string);
122         gtk_pattern_spec_free_segs(pspec);
123
124         return string;
125 }
126
127 void menu_factory_clear_rc(const gchar *rc_str)
128 {
129         GString *string;
130         gchar *p;
131         gchar *start, *end;
132         guint pos = 0;
133
134         string = g_string_new(rc_str);
135         while ((p = strstr(string->str + pos, "(menu-path \"")) != NULL) {
136                 pos = p + 12 - string->str;
137                 p = strchr(p + 12, '"');
138                 if (!p) continue;
139                 start = strchr(p + 1, '"');
140                 if (!start) continue;
141                 end = strchr(start + 1, '"');
142                 if (!end) continue;
143                 pos = start + 1 - string->str;
144                 if (end > start + 1)
145                         g_string_erase(string, pos, end - (start + 1));
146         }
147
148         gtk_item_factory_parse_rc_string(string->str);
149         g_string_free(string, TRUE);
150 }
151
152 void menu_factory_copy_rc(const gchar *src_path, const gchar *dest_path)
153 {
154         GString *string;
155         gint src_path_len;
156         gint dest_path_len;
157         gchar *p;
158         guint pos = 0;
159
160         string = menu_factory_get_rc(src_path);
161         src_path_len = strlen(src_path);
162         dest_path_len = strlen(dest_path);
163
164         while ((p = strstr(string->str + pos, src_path)) != NULL) {
165                 pos = p - string->str;
166                 g_string_erase(string, pos, src_path_len);
167                 g_string_insert(string, pos, dest_path);
168                 pos += dest_path_len;
169         }
170
171         pos = 0;
172         while ((p = strchr(string->str + pos, ';')) != NULL) {
173                 pos = p - string->str;
174                 if (pos == 0 || *(p - 1) == '\n')
175                         g_string_erase(string, pos, 1);
176         }
177
178         menu_factory_clear_rc(string->str);
179         gtk_item_factory_parse_rc_string(string->str);
180         g_string_free(string, TRUE);
181 }
182 #endif
183
184 void menu_set_sensitive(GtkItemFactory *ifactory, const gchar *path,
185                         gboolean sensitive)
186 {
187         GtkWidget *widget;
188
189         g_return_if_fail(ifactory != NULL);
190
191         widget = gtk_item_factory_get_item(ifactory, path);
192         if(widget == NULL) {
193                 debug_print("unknown menu entry %s\n", path);
194                 return;
195         }
196         gtk_widget_set_sensitive(widget, sensitive);
197 }
198
199 void menu_set_sensitive_all(GtkMenuShell *menu_shell, gboolean sensitive)
200 {
201         GList *cur;
202
203         for (cur = menu_shell->children; cur != NULL; cur = cur->next)
204                 gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
205 }
206
207 void menu_set_toggle(GtkItemFactory *ifactory, const gchar *path,
208                         gboolean active)
209 {
210         GtkWidget *widget;
211
212         g_return_if_fail(ifactory != NULL);
213
214         widget = gtk_item_factory_get_item(ifactory, path);
215         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(widget), active);
216 }
217
218 void menu_toggle_toggle(GtkItemFactory *ifactory, const gchar *path)
219 {
220         GtkWidget *widget;
221         
222         g_return_if_fail(ifactory != NULL);
223         
224         widget = gtk_item_factory_get_item(ifactory, path);
225         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), !((GTK_CHECK_MENU_ITEM(widget))->active));
226 }
227
228 void menu_button_position(GtkMenu *menu, gint *x, gint *y, gboolean *push_in,
229                           gpointer user_data)
230 {
231         GtkWidget *widget;
232         gint wheight;
233         gint wx, wy;
234
235         g_return_if_fail(x && y);
236         g_return_if_fail(GTK_IS_BUTTON(user_data));
237
238         widget = GTK_WIDGET(user_data);
239
240         gdk_window_get_origin(widget->window, x, y);
241         wheight = widget->requisition.height;
242         wx = widget->allocation.x;
243         wy = widget->allocation.y;
244          
245         *y = *y + wy + wheight;
246         *x = *x + wx;
247 }
248
249 gint menu_find_option_menu_index(GtkOptionMenu *optmenu, gpointer data,
250                                  GCompareFunc func)
251 {
252         GtkWidget *menu;
253         GtkWidget *menuitem;
254         gpointer menu_data;
255         GList *cur;
256         gint n;
257
258         menu = gtk_option_menu_get_menu(optmenu);
259
260         for (cur = GTK_MENU_SHELL(menu)->children, n = 0;
261              cur != NULL; cur = cur->next, n++) {
262                 menuitem = GTK_WIDGET(cur->data);
263                 menu_data = g_object_get_data(G_OBJECT(menuitem),
264                                               MENU_VAL_ID);
265                 if (func) {
266                         if (func(menu_data, data) == 0)
267                                 return n;
268                 } else if (menu_data == data)
269                         return n;
270         }
271
272         return -1;
273 }
274
275 /* call backs for accelerator changes on selected menu items */
276 static void menu_item_add_accel( GtkWidget *widget, guint accel_signal_id, GtkAccelGroup *accel_group,
277                                  guint accel_key, GdkModifierType accel_mods, GtkAccelFlags accel_flags,
278                                  gpointer user_data)
279 {
280 #warning FIXME_GTK2
281 #if 0
282         GtkWidget *connected = GTK_WIDGET(user_data);   
283         if (gtk_signal_n_emissions_by_name(G_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 #endif
294 }
295
296 static void menu_item_remove_accel(GtkWidget *widget, GtkAccelGroup *accel_group,
297                                    guint accel_key, GdkModifierType accel_mods,
298                                    gpointer user_data)
299 {       
300 #warning FIXME_GTK2
301 #if 0
302         GtkWidget *wid = GTK_WIDGET(user_data);
303
304         if (gtk_signal_n_emissions_by_name(G_OBJECT(widget),
305             "remove_accelerator") > 2 )
306                 return;
307         gtk_widget_remove_accelerators(wid,"activate",FALSE);
308 #endif
309 }
310
311 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2) 
312 {
313         g_signal_connect_after(G_OBJECT(widget), "add_accelerator", 
314                                G_CALLBACK(menu_item_add_accel), wid2);
315         g_signal_connect_after(G_OBJECT(widget), "remove_accelerator", 
316                                G_CALLBACK(menu_item_remove_accel), wid2);
317 }
318
319 void menu_connect_identical_items(void)
320 {
321         gint n;
322         GtkWidget *item1;
323         GtkWidget *item2;
324
325         static const struct {   
326                 const gchar *path1;
327                 const gchar *path2;
328         } pairs[] = {
329                 {"<Main>/Message/Reply",                        "<SummaryView>/Reply"},
330                 {"<Main>/Message/Reply to/all",                 "<SummaryView>/Reply to/all"},
331                 {"<Main>/Message/Reply to/sender",              "<SummaryView>/Reply to/sender"},
332                 {"<Main>/Message/Reply to/mailing list",        "<SummaryView>/Reply to/mailing list"},
333                 {"<Main>/Message/Follow-up and reply to",       "<SummaryView>/Follow-up and reply to"},
334                 {"<Main>/Message/Forward",                      "<SummaryView>/Forward"},
335                 {"<Main>/Message/Redirect",                     "<SummaryView>/Redirect"},
336                 {"<Main>/Message/Re-edit",                      "<SummaryView>/Re-edit"},
337                 {"<Main>/Message/Move...",                      "<SummaryView>/Move..."},
338                 {"<Main>/Message/Copy...",                      "<SummaryView>/Copy..."},
339                 {"<Main>/Message/Delete",                       "<SummaryView>/Delete"},
340                 {"<Main>/Message/Cancel a news message",        "<SummaryView>/Cancel a news message"},
341                 {"<Main>/Message/Mark/Mark",                    "<SummaryView>/Mark/Mark"},
342                 {"<Main>/Message/Mark/Unmark",                  "<SummaryView>/Mark/Unmark"},
343                 {"<Main>/Message/Mark/Mark as unread",          "<SummaryView>/Mark/Mark as unread"},
344                 {"<Main>/Message/Mark/Mark as read",            "<SummaryView>/Mark/Mark as read"},
345                 {"<Main>/Message/Mark/Mark all read",           "<SummaryView>/Mark/Mark all read"},
346                 {"<Main>/Tools/Add sender to address book",     "<SummaryView>/Add sender to address book"},
347                 {"<Main>/Tools/Create filter rule/Automatically",       "<SummaryView>/Create filter rule/Automatically"},
348                 {"<Main>/Tools/Create filter rule/by From",     "<SummaryView>/Create filter rule/by From"},
349                 {"<Main>/Tools/Create filter rule/by To",       "<SummaryView>/Create filter rule/by To"},
350                 {"<Main>/Tools/Create filter rule/by Subject",  "<SummaryView>/Create filter rule/by Subject"},
351                 {"<Main>/View/Open in new window",              "<SummaryView>/View/Open in new window"},
352                 {"<Main>/View/Message source",                  "<SummaryView>/View/Source"},
353                 {"<Main>/View/Show all headers",                "<SummaryView>/View/All header"},
354         };
355
356         const gint numpairs = sizeof pairs / sizeof pairs[0];
357         for (n = 0; n < numpairs; n++) {
358                 /* get widgets from the paths */
359
360                 item1 = gtk_item_factory_get_widget
361                                 (gtk_item_factory_from_path(pairs[n].path1),pairs[n].path1);            
362                 item2 = gtk_item_factory_get_widget
363                                 (gtk_item_factory_from_path(pairs[n].path2),pairs[n].path2);            
364
365                 if (item1 && item2) {
366                         /* connect widgets both ways around */
367                         connect_accel_change_signals(item2,item1);
368                         connect_accel_change_signals(item1,item2);
369                 } else { 
370                         if (!item1) debug_print(" ** Menu item not found: %s\n",pairs[n].path1);
371                         if (!item2) debug_print(" ** Menu item not found: %s\n",pairs[n].path2);
372                 }                               
373         }
374 }
375
376 void menu_select_by_data(GtkMenu *menu, gpointer data)
377 {
378         GList *children, *cur;
379         GtkWidget *select_item = NULL;
380         
381         g_return_if_fail(menu != NULL);
382
383         children = gtk_container_children(GTK_CONTAINER(menu));
384
385         for (cur = children; cur != NULL; cur = g_list_next(cur)) {
386                 GtkObject *child = G_OBJECT(cur->data);
387
388                 if (gtk_object_get_user_data(child) == data) {
389                         select_item = GTK_WIDGET(child);
390                 }
391         }
392         if (select_item != NULL) {
393                 gtk_menu_shell_select_item(GTK_MENU_SHELL(menu), select_item);
394                 gtk_menu_shell_activate_item(GTK_MENU_SHELL(menu), select_item, FALSE);
395         }
396
397         g_list_free(children);
398 }