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