add 'Re-edit' to the message context menu in the Drafts folder
[claws.git] / src / gtk / menu.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2013 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <gtk/gtk.h>
28
29 #include "gtkcmoptionmenu.h"
30 #include "menu.h"
31 #include "utils.h"
32 #include "gtkutils.h"
33 #include "defs.h"
34
35 GtkActionGroup *cm_menu_create_action_group(const gchar *name, GtkActionEntry *entries,
36                                             gint num_entries, gpointer data)
37 {
38         GtkActionGroup *group = gtk_action_group_new(name);
39         gtk_action_group_set_translate_func(group, menu_translate, NULL, NULL);
40         gtk_action_group_add_actions(group, entries, num_entries, data);
41         gtk_ui_manager_insert_action_group(gtkut_ui_manager(), group, 0);
42         return group;
43 }
44
45 GtkActionGroup *cm_menu_create_action_group_full(GtkUIManager *manager, const gchar *name, GtkActionEntry *entries,
46                                             gint num_entries, gpointer data)
47 {
48         GtkActionGroup *group = gtk_action_group_new(name);
49         gtk_action_group_set_translate_func(group, menu_translate, NULL, NULL);
50         gtk_action_group_add_actions(group, entries, num_entries, data);
51         gtk_ui_manager_insert_action_group(manager, group, 0);
52         return group;
53 }
54
55 gchar *menu_translate(const gchar *path, gpointer data)
56 {
57         gchar *retval;
58
59         retval = gettext(path);
60
61         return retval;
62 }
63
64 void cm_menu_set_sensitive(gchar *menu, gboolean sensitive)
65 {
66         GtkUIManager *gui_manager = gtkut_ui_manager();
67         gchar *path = g_strdup_printf("Menus/%s", menu);
68
69         cm_menu_set_sensitive_full(gui_manager, path, sensitive);
70         g_free(path);
71 }
72
73 void cm_toggle_menu_set_active(gchar *menu, gboolean active)
74 {
75         GtkUIManager *gui_manager = gtkut_ui_manager();
76         gchar *path = g_strdup_printf("Menus/%s", menu);
77
78         cm_toggle_menu_set_active_full(gui_manager, path, active);
79         g_free(path);
80 }
81
82 void cm_menu_set_sensitive_full(GtkUIManager *gui_manager, const gchar *menu, gboolean sensitive)
83 {
84         GtkWidget *widget;
85         gchar *path = g_strdup_printf("/%s/", menu);
86
87         widget = gtk_ui_manager_get_widget(gui_manager, path);
88         if( !GTK_IS_WIDGET(widget) ) {
89                 g_message("Blah, '%s' is not a widget.\n", path);
90         }
91
92         if( !GTK_IS_MENU_ITEM(widget) ) {
93                 g_message("Blah, '%s' is not a menu item.\n", path);
94         }
95
96         gtk_widget_set_sensitive(widget, sensitive);
97
98         if (strcmp(menu, "Menus/SummaryViewPopup/Reedit") == 0)
99                 (sensitive)? gtk_widget_show(widget) : gtk_widget_hide(widget);
100
101         g_free(path);
102 }
103
104 gchar *cm_menu_item_get_shortcut(GtkUIManager *gui_manager, gchar *menu)
105 {
106         GtkWidget *widget;
107         gchar *path = g_strdup_printf("/%s/", menu);
108         const gchar *accel = NULL;
109         GtkAccelKey key;
110
111         widget = gtk_ui_manager_get_widget(gui_manager, path);
112         if( !GTK_IS_WIDGET(widget) ) {
113                 g_message("Blah, '%s' is not a widget.\n", path);
114         }
115
116         if( !GTK_IS_MENU_ITEM(widget) ) {
117                 g_message("Blah, '%s' is not a menu item.\n", path);
118         }
119
120         g_free(path);
121
122         accel = gtk_menu_item_get_accel_path(GTK_MENU_ITEM(widget));
123
124         if (accel && gtk_accel_map_lookup_entry(accel, &key))
125                 return gtk_accelerator_get_label (key.accel_key, key.accel_mods);
126         else
127                 return g_strdup(_("None"));
128
129 }
130
131 GtkWidget *cm_menu_item_new_label_from_url(gchar *url)
132 {
133         gint len = strlen(url);
134         if (len > MAX_MENU_LABEL_LENGTH) {
135                 g_message("Refusing a %d bytes string as menu label\n", len);
136                 url[64] = '\0', url[63] = url[62] = url[61] = '.', url[60] = ' ';
137                 GtkWidget *newlabel = gtk_menu_item_new_with_label(url);
138                 gtk_widget_set_tooltip_markup(GTK_WIDGET(newlabel),
139                         g_strconcat("<span><b>", _("Warning:"), "</b>",
140                         _("This URL was too long for displaying and\n"
141                         "has been truncated for safety. This message could be\n"
142                         "corrupted, malformed or part of some DoS attempt."),
143                         "</span>", NULL));
144                 return newlabel;
145         }
146         
147         return gtk_menu_item_new_with_label(url);
148 }
149
150 void cm_toggle_menu_set_active_full(GtkUIManager *gui_manager, gchar *menu, gboolean active)
151 {
152         GtkWidget *widget;
153         gchar *path = g_strdup_printf("/%s/", menu);
154
155         widget = gtk_ui_manager_get_widget(gui_manager, path);
156         if( !GTK_IS_WIDGET(widget) ) {
157                 g_message("Blah, '%s' is not a widget.\n", path);
158         }
159
160         if( !GTK_CHECK_MENU_ITEM(widget) ) {
161                 g_message("Blah, '%s' is not a check menu item.\n", path);
162         }
163
164         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), active);
165         g_free(path);
166 }
167
168 void menu_set_sensitive_all(GtkMenuShell *menu_shell, gboolean sensitive)
169 {
170         GList *children = gtk_container_get_children(GTK_CONTAINER(menu_shell));
171         GList *cur;
172
173         for (cur = children; cur != NULL; cur = cur->next)
174                 gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
175
176         g_list_free(children);
177 }
178
179 void menu_button_position(GtkMenu *menu, gint *x, gint *y, gboolean *push_in,
180                           gpointer user_data)
181 {
182         GtkWidget *widget;
183         gint wheight;
184         gint wx, wy;
185         GtkAllocation allocation;
186         GtkRequisition mreq, wreq;
187         GdkScreen *screen;
188         GdkRectangle monitor;
189         gint monitor_num;
190
191         cm_return_if_fail(x && y);
192         cm_return_if_fail(GTK_IS_BUTTON(user_data));
193
194         widget = GTK_WIDGET(user_data);
195
196         gdk_window_get_origin(gtk_widget_get_window(widget), x, y);
197         gtk_widget_get_requisition(widget, &wreq);
198         wheight = wreq.height;
199         gtk_widget_get_allocation(widget, &allocation);
200         wx = allocation.x;
201         wy = allocation.y;
202         
203         gtk_widget_size_request(GTK_WIDGET(menu), &mreq);
204         screen = gtk_widget_get_screen (widget);
205         monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
206         gdk_screen_get_monitor_geometry (screen, monitor_num, 
207                                          &monitor);
208
209         *x = *x + wx;
210         *y = *y + wy + wheight;
211         
212         if (*y + mreq.height >= monitor.height)
213                 *y -= mreq.height;
214 }
215
216 gint menu_find_option_menu_index(GtkCMOptionMenu *optmenu, gpointer data,
217                                  GCompareFunc func)
218 {
219         GtkWidget *menu;
220         GtkWidget *menuitem;
221         gpointer menu_data;
222         GList *children;
223         GList *cur;
224         gint n, found = -1;
225
226         menu = gtk_cmoption_menu_get_menu(optmenu);
227         children = gtk_container_get_children(GTK_CONTAINER(GTK_MENU_SHELL(menu)));
228
229         for (cur = children, n = 0;
230              cur != NULL; cur = cur->next, n++) {
231                 menuitem = GTK_WIDGET(cur->data);
232                 menu_data = g_object_get_data(G_OBJECT(menuitem),
233                                               MENU_VAL_ID);
234                 if (func) {
235                         if (func(menu_data, data) == 0) {
236                                 found = n;
237                                 break;
238                         }
239                 } else if (menu_data == data) {
240                         found = n;
241                         break;
242                 }
243         }
244
245         g_list_free(children);
246
247         return found;
248 }