#warning in src/gtk
[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 void menu_set_sensitive(GtkItemFactory *ifactory, const gchar *path,
109                         gboolean sensitive)
110 {
111         GtkWidget *widget;
112
113         g_return_if_fail(ifactory != NULL);
114
115         widget = gtk_item_factory_get_item(ifactory, path);
116         if(widget == NULL) {
117                 debug_print("unknown menu entry %s\n", path);
118                 return;
119         }
120         gtk_widget_set_sensitive(widget, sensitive);
121 }
122
123 void menu_set_sensitive_all(GtkMenuShell *menu_shell, gboolean sensitive)
124 {
125         GList *cur;
126
127         for (cur = menu_shell->children; cur != NULL; cur = cur->next)
128                 gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
129 }
130
131 void menu_set_active(GtkItemFactory *ifactory, const gchar *path,
132                      gboolean is_active)
133 {
134         GtkWidget *widget;
135
136         g_return_if_fail(ifactory != NULL);
137
138         widget = gtk_item_factory_get_item(ifactory, path);
139         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), is_active);
140 }
141
142 void menu_button_position(GtkMenu *menu, gint *x, gint *y, gboolean *push_in,
143                           gpointer user_data)
144 {
145         GtkWidget *widget;
146         gint wheight;
147         gint wx, wy;
148
149         g_return_if_fail(x && y);
150         g_return_if_fail(GTK_IS_BUTTON(user_data));
151
152         widget = GTK_WIDGET(user_data);
153
154         gdk_window_get_origin(widget->window, x, y);
155         wheight = widget->requisition.height;
156         wx = widget->allocation.x;
157         wy = widget->allocation.y;
158          
159         *y = *y + wy + wheight;
160         *x = *x + wx;
161 }
162
163 gint menu_find_option_menu_index(GtkOptionMenu *optmenu, gpointer data,
164                                  GCompareFunc func)
165 {
166         GtkWidget *menu;
167         GtkWidget *menuitem;
168         gpointer menu_data;
169         GList *cur;
170         gint n;
171
172         menu = gtk_option_menu_get_menu(optmenu);
173
174         for (cur = GTK_MENU_SHELL(menu)->children, n = 0;
175              cur != NULL; cur = cur->next, n++) {
176                 menuitem = GTK_WIDGET(cur->data);
177                 menu_data = g_object_get_data(G_OBJECT(menuitem),
178                                               MENU_VAL_ID);
179                 if (func) {
180                         if (func(menu_data, data) == 0)
181                                 return n;
182                 } else if (menu_data == data)
183                         return n;
184         }
185
186         return -1;
187 }
188
189 /* call backs for accelerator changes on selected menu items */
190 static void menu_item_add_accel( GtkWidget *widget, guint accel_signal_id, GtkAccelGroup *accel_group,
191                                  guint accel_key, GdkModifierType accel_mods, GtkAccelFlags accel_flags,
192                                  gpointer user_data)
193 {
194 #warning FIXME_GTK2
195 #if 0
196         GtkWidget *connected = GTK_WIDGET(user_data);   
197         if (gtk_signal_n_emissions_by_name(G_OBJECT(widget),"add_accelerator") > 1 ) return;
198         gtk_widget_remove_accelerators(connected,"activate",FALSE);
199         /* lock _this_ widget */
200         gtk_accel_group_lock_entry(accel_group,accel_key,accel_mods);
201         /* modify the _other_ widget */
202         gtk_widget_add_accelerator(connected, "activate",
203                                    gtk_item_factory_from_widget(connected)->accel_group,
204                                    accel_key, accel_mods,
205                                    GTK_ACCEL_VISIBLE );
206         gtk_accel_group_unlock_entry(accel_group,accel_key,accel_mods);                            
207 #endif
208 }
209
210 static void menu_item_remove_accel(GtkWidget *widget, GtkAccelGroup *accel_group,
211                                    guint accel_key, GdkModifierType accel_mods,
212                                    gpointer user_data)
213 {       
214 #warning FIXME_GTK2
215 #if 0
216         GtkWidget *wid = GTK_WIDGET(user_data);
217
218         if (gtk_signal_n_emissions_by_name(G_OBJECT(widget),
219             "remove_accelerator") > 2 )
220                 return;
221         gtk_widget_remove_accelerators(wid,"activate",FALSE);
222 #endif
223 }
224
225 static void connect_accel_change_signals(GtkWidget* widget, GtkWidget *wid2) 
226 {
227         g_signal_connect_after(G_OBJECT(widget), "add_accelerator", 
228                                G_CALLBACK(menu_item_add_accel), wid2);
229         g_signal_connect_after(G_OBJECT(widget), "remove_accelerator", 
230                                G_CALLBACK(menu_item_remove_accel), wid2);
231 }
232
233 void menu_connect_identical_items(void)
234 {
235         gint n;
236         GtkWidget *item1;
237         GtkWidget *item2;
238
239         static const struct {   
240                 const gchar *path1;
241                 const gchar *path2;
242         } pairs[] = {
243                 {"<Main>/Message/Reply",                        "<SummaryView>/Reply"},
244                 {"<Main>/Message/Reply to/all",                 "<SummaryView>/Reply to/all"},
245                 {"<Main>/Message/Reply to/sender",              "<SummaryView>/Reply to/sender"},
246                 {"<Main>/Message/Reply to/mailing list",        "<SummaryView>/Reply to/mailing list"},
247                 {"<Main>/Message/Follow-up and reply to",       "<SummaryView>/Follow-up and reply to"},
248                 {"<Main>/Message/Forward",                      "<SummaryView>/Forward"},
249                 {"<Main>/Message/Redirect",                     "<SummaryView>/Redirect"},
250                 {"<Main>/Message/Re-edit",                      "<SummaryView>/Re-edit"},
251                 {"<Main>/Message/Move...",                      "<SummaryView>/Move..."},
252                 {"<Main>/Message/Copy...",                      "<SummaryView>/Copy..."},
253                 {"<Main>/Message/Delete",                       "<SummaryView>/Delete"},
254                 {"<Main>/Message/Cancel a news message",        "<SummaryView>/Cancel a news message"},
255                 {"<Main>/Message/Mark/Mark",                    "<SummaryView>/Mark/Mark"},
256                 {"<Main>/Message/Mark/Unmark",                  "<SummaryView>/Mark/Unmark"},
257                 {"<Main>/Message/Mark/Mark as unread",          "<SummaryView>/Mark/Mark as unread"},
258                 {"<Main>/Message/Mark/Mark as read",            "<SummaryView>/Mark/Mark as read"},
259                 {"<Main>/Message/Mark/Mark all read",           "<SummaryView>/Mark/Mark all read"},
260                 {"<Main>/Tools/Add sender to address book",     "<SummaryView>/Add sender to address book"},
261                 {"<Main>/Tools/Create filter rule/Automatically",       "<SummaryView>/Create filter rule/Automatically"},
262                 {"<Main>/Tools/Create filter rule/by From",     "<SummaryView>/Create filter rule/by From"},
263                 {"<Main>/Tools/Create filter rule/by To",       "<SummaryView>/Create filter rule/by To"},
264                 {"<Main>/Tools/Create filter rule/by Subject",  "<SummaryView>/Create filter rule/by Subject"},
265                 {"<Main>/View/Open in new window",              "<SummaryView>/View/Open in new window"},
266                 {"<Main>/View/Message source",                  "<SummaryView>/View/Source"},
267                 {"<Main>/View/Show all headers",                "<SummaryView>/View/All header"},
268         };
269
270         const gint numpairs = sizeof pairs / sizeof pairs[0];
271         for (n = 0; n < numpairs; n++) {
272                 /* get widgets from the paths */
273
274                 item1 = gtk_item_factory_get_widget
275                                 (gtk_item_factory_from_path(pairs[n].path1),pairs[n].path1);            
276                 item2 = gtk_item_factory_get_widget
277                                 (gtk_item_factory_from_path(pairs[n].path2),pairs[n].path2);            
278
279                 if (item1 && item2) {
280                         /* connect widgets both ways around */
281                         connect_accel_change_signals(item2,item1);
282                         connect_accel_change_signals(item1,item2);
283                 } else { 
284                         if (!item1) debug_print(" ** Menu item not found: %s\n",pairs[n].path1);
285                         if (!item2) debug_print(" ** Menu item not found: %s\n",pairs[n].path2);
286                 }                               
287         }
288 }
289
290 void menu_select_by_data(GtkMenu *menu, gpointer data)
291 {
292         GList *children, *cur;
293         GtkWidget *select_item = NULL;
294         
295         g_return_if_fail(menu != NULL);
296
297         children = gtk_container_children(GTK_CONTAINER(menu));
298
299         for (cur = children; cur != NULL; cur = g_list_next(cur)) {
300                 GtkObject *child = G_OBJECT(cur->data);
301
302                 if (gtk_object_get_user_data(child) == data) {
303                         select_item = GTK_WIDGET(child);
304                 }
305         }
306         if (select_item != NULL) {
307                 gtk_menu_shell_select_item(GTK_MENU_SHELL(menu), select_item);
308                 gtk_menu_shell_activate_item(GTK_MENU_SHELL(menu), select_item, FALSE);
309         }
310
311         g_list_free(children);
312 }