3f5859e3e08756003f421a6e18b1eba906106586
[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 #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_active(GtkItemFactory *ifactory, const gchar *path,
208                      gboolean is_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), is_active);
216 }
217
218 void menu_button_position(GtkMenu *menu, gint *x, gint *y, gboolean *push_in,
219                           gpointer user_data)
220 {
221         GtkWidget *widget;
222         gint wheight;
223         gint wx, wy;
224
225         g_return_if_fail(x && y);
226         g_return_if_fail(GTK_IS_BUTTON(user_data));
227
228         widget = GTK_WIDGET(user_data);
229
230         gdk_window_get_origin(widget->window, x, y);
231         wheight = widget->requisition.height;
232         wx = widget->allocation.x;
233         wy = widget->allocation.y;
234          
235         *y = *y + wy + wheight;
236         *x = *x + wx;
237 }
238
239 gint menu_find_option_menu_index(GtkOptionMenu *optmenu, gpointer data,
240                                  GCompareFunc func)
241 {
242         GtkWidget *menu;
243         GtkWidget *menuitem;
244         gpointer menu_data;
245         GList *cur;
246         gint n;
247
248         menu = gtk_option_menu_get_menu(optmenu);
249
250         for (cur = GTK_MENU_SHELL(menu)->children, n = 0;
251              cur != NULL; cur = cur->next, n++) {
252                 menuitem = GTK_WIDGET(cur->data);
253                 menu_data = g_object_get_data(G_OBJECT(menuitem),
254                                               MENU_VAL_ID);
255                 if (func) {
256                         if (func(menu_data, data) == 0)
257                                 return n;
258                 } else if (menu_data == data)
259                         return n;
260         }
261
262         return -1;
263 }
264
265 /* call backs for accelerator changes on selected menu items */
266 static void menu_item_add_accel( GtkWidget *widget, guint accel_signal_id, GtkAccelGroup *accel_group,
267                                  guint accel_key, GdkModifierType accel_mods, GtkAccelFlags accel_flags,
268                                  gpointer user_data)
269 {
270 #warning FIXME_GTK2
271 #if 0
272         GtkWidget *connected = GTK_WIDGET(user_data);   
273         if (gtk_signal_n_emissions_by_name(G_OBJECT(widget),"add_accelerator") > 1 ) return;
274         gtk_widget_remove_accelerators(connected,"activate",FALSE);
275         /* lock _this_ widget */
276         gtk_accel_group_lock_entry(accel_group,accel_key,accel_mods);
277         /* modify the _other_ widget */
278         gtk_widget_add_accelerator(connected, "activate",
279                                    gtk_item_factory_from_widget(connected)->accel_group,
280                                    accel_key, accel_mods,
281                                    GTK_ACCEL_VISIBLE );
282         gtk_accel_group_unlock_entry(accel_group,accel_key,accel_mods);                            
283 #endif
284 }
285
286 static void menu_item_remove_accel(GtkWidget *widget, GtkAccelGroup *accel_group,
287                                    guint accel_key, GdkModifierType accel_mods,
288                                    gpointer user_data)
289 {       
290 #warning FIXME_GTK2
291 #if 0
292         GtkWidget *wid = GTK_WIDGET(user_data);
293
294         if (gtk_signal_n_emissions_by_name(G_OBJECT(widget),
295             "remove_accelerator") > 2 )
296                 return;
297         gtk_widget_remove_accelerators(wid,"activate",FALSE);
298 #endif
299 }
300
301 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2) 
302 {
303         g_signal_connect_after(G_OBJECT(widget), "add_accelerator", 
304                                G_CALLBACK(menu_item_add_accel), wid2);
305         g_signal_connect_after(G_OBJECT(widget), "remove_accelerator", 
306                                G_CALLBACK(menu_item_remove_accel), wid2);
307 }
308
309 void menu_connect_identical_items(void)
310 {
311         gint n;
312         GtkWidget *item1;
313         GtkWidget *item2;
314
315         static const struct {   
316                 const gchar *path1;
317                 const gchar *path2;
318         } pairs[] = {
319                 {"<Main>/Message/Reply",                        "<SummaryView>/Reply"},
320                 {"<Main>/Message/Reply to/all",                 "<SummaryView>/Reply to/all"},
321                 {"<Main>/Message/Reply to/sender",              "<SummaryView>/Reply to/sender"},
322                 {"<Main>/Message/Reply to/mailing list",        "<SummaryView>/Reply to/mailing list"},
323                 {"<Main>/Message/Follow-up and reply to",       "<SummaryView>/Follow-up and reply to"},
324                 {"<Main>/Message/Forward",                      "<SummaryView>/Forward"},
325                 {"<Main>/Message/Redirect",                     "<SummaryView>/Redirect"},
326                 {"<Main>/Message/Re-edit",                      "<SummaryView>/Re-edit"},
327                 {"<Main>/Message/Move...",                      "<SummaryView>/Move..."},
328                 {"<Main>/Message/Copy...",                      "<SummaryView>/Copy..."},
329                 {"<Main>/Message/Delete",                       "<SummaryView>/Delete"},
330                 {"<Main>/Message/Cancel a news message",        "<SummaryView>/Cancel a news message"},
331                 {"<Main>/Message/Mark/Mark",                    "<SummaryView>/Mark/Mark"},
332                 {"<Main>/Message/Mark/Unmark",                  "<SummaryView>/Mark/Unmark"},
333                 {"<Main>/Message/Mark/Mark as unread",          "<SummaryView>/Mark/Mark as unread"},
334                 {"<Main>/Message/Mark/Mark as read",            "<SummaryView>/Mark/Mark as read"},
335                 {"<Main>/Message/Mark/Mark all read",           "<SummaryView>/Mark/Mark all read"},
336                 {"<Main>/Tools/Add sender to address book",     "<SummaryView>/Add sender to address book"},
337                 {"<Main>/Tools/Create filter rule/Automatically",       "<SummaryView>/Create filter rule/Automatically"},
338                 {"<Main>/Tools/Create filter rule/by From",     "<SummaryView>/Create filter rule/by From"},
339                 {"<Main>/Tools/Create filter rule/by To",       "<SummaryView>/Create filter rule/by To"},
340                 {"<Main>/Tools/Create filter rule/by Subject",  "<SummaryView>/Create filter rule/by Subject"},
341                 {"<Main>/View/Open in new window",              "<SummaryView>/View/Open in new window"},
342                 {"<Main>/View/Message source",                  "<SummaryView>/View/Source"},
343                 {"<Main>/View/Show all headers",                "<SummaryView>/View/All header"},
344         };
345
346         const gint numpairs = sizeof pairs / sizeof pairs[0];
347         for (n = 0; n < numpairs; n++) {
348                 /* get widgets from the paths */
349
350                 item1 = gtk_item_factory_get_widget
351                                 (gtk_item_factory_from_path(pairs[n].path1),pairs[n].path1);            
352                 item2 = gtk_item_factory_get_widget
353                                 (gtk_item_factory_from_path(pairs[n].path2),pairs[n].path2);            
354
355                 if (item1 && item2) {
356                         /* connect widgets both ways around */
357                         connect_accel_change_signals(item2,item1);
358                         connect_accel_change_signals(item1,item2);
359                 } else { 
360                         if (!item1) debug_print(" ** Menu item not found: %s\n",pairs[n].path1);
361                         if (!item2) debug_print(" ** Menu item not found: %s\n",pairs[n].path2);
362                 }                               
363         }
364 }
365
366 void menu_select_by_data(GtkMenu *menu, gpointer data)
367 {
368         GList *children, *cur;
369         GtkWidget *select_item = NULL;
370         
371         g_return_if_fail(menu != NULL);
372
373         children = gtk_container_children(GTK_CONTAINER(menu));
374
375         for (cur = children; cur != NULL; cur = g_list_next(cur)) {
376                 GtkObject *child = G_OBJECT(cur->data);
377
378                 if (gtk_object_get_user_data(child) == data) {
379                         select_item = GTK_WIDGET(child);
380                 }
381         }
382         if (select_item != NULL) {
383                 gtk_menu_shell_select_item(GTK_MENU_SHELL(menu), select_item);
384                 gtk_menu_shell_activate_item(GTK_MENU_SHELL(menu), select_item, FALSE);
385         }
386
387         g_list_free(children);
388 }