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