remove all gtk3 conditionals
[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         g_free(path);
98 }
99
100 gchar *cm_menu_item_get_shortcut(GtkUIManager *gui_manager, gchar *menu)
101 {
102         GtkWidget *widget;
103         gchar *path = g_strdup_printf("/%s/", menu);
104         const gchar *accel = NULL;
105         GtkAccelKey key;
106
107         widget = gtk_ui_manager_get_widget(gui_manager, path);
108         if( !GTK_IS_WIDGET(widget) ) {
109                 g_message("Blah, '%s' is not a widget.\n", path);
110         }
111
112         if( !GTK_IS_MENU_ITEM(widget) ) {
113                 g_message("Blah, '%s' is not a menu item.\n", path);
114         }
115
116         g_free(path);
117
118         accel = gtk_menu_item_get_accel_path(GTK_MENU_ITEM(widget));
119
120         if (accel && gtk_accel_map_lookup_entry(accel, &key))
121                 return gtk_accelerator_get_label (key.accel_key, key.accel_mods);
122         else
123                 return g_strdup(_("None"));
124
125 }
126
127 GtkWidget *cm_menu_item_new_label_from_url(gchar *url)
128 {
129         gint len = strlen(url);
130         if (len > MAX_MENU_LABEL_LENGTH) {
131                 g_message("Refusing a %d bytes string as menu label\n", len);
132                 url[64] = '\0', url[63] = url[62] = url[61] = '.', url[60] = ' ';
133                 GtkWidget *newlabel = gtk_menu_item_new_with_label(url);
134                 gtk_widget_set_tooltip_markup(GTK_WIDGET(newlabel),
135                         g_strconcat("<span><b>", _("Warning:"), "</b>",
136                         _("This URL was too long for displaying and\n"
137                         "has been truncated for safety. This message could be\n"
138                         "corrupted, malformed or part of some DoS attempt."),
139                         "</span>", NULL));
140                 return newlabel;
141         }
142         
143         return gtk_menu_item_new_with_label(url);
144 }
145
146 void cm_toggle_menu_set_active_full(GtkUIManager *gui_manager, gchar *menu, gboolean active)
147 {
148         GtkWidget *widget;
149         gchar *path = g_strdup_printf("/%s/", menu);
150
151         widget = gtk_ui_manager_get_widget(gui_manager, path);
152         if( !GTK_IS_WIDGET(widget) ) {
153                 g_message("Blah, '%s' is not a widget.\n", path);
154         }
155
156         if( !GTK_CHECK_MENU_ITEM(widget) ) {
157                 g_message("Blah, '%s' is not a check menu item.\n", path);
158         }
159
160         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), active);
161         g_free(path);
162 }
163
164 void menu_set_sensitive_all(GtkMenuShell *menu_shell, gboolean sensitive)
165 {
166         GList *children = gtk_container_get_children(GTK_CONTAINER(menu_shell));
167         GList *cur;
168
169         for (cur = children; cur != NULL; cur = cur->next)
170                 gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
171
172         g_list_free(children);
173 }
174
175 void menu_button_position(GtkMenu *menu, gint *x, gint *y, gboolean *push_in,
176                           gpointer user_data)
177 {
178         GtkWidget *widget;
179         gint wheight;
180         gint wx, wy;
181         GtkAllocation allocation;
182         GtkRequisition mreq, wreq;
183         GdkScreen *screen;
184         GdkRectangle monitor;
185         gint monitor_num;
186
187         cm_return_if_fail(x && y);
188         cm_return_if_fail(GTK_IS_BUTTON(user_data));
189
190         widget = GTK_WIDGET(user_data);
191
192         gdk_window_get_origin(gtk_widget_get_window(widget), x, y);
193         gtk_widget_get_requisition(widget, &wreq);
194         wheight = wreq.height;
195         gtk_widget_get_allocation(widget, &allocation);
196         wx = allocation.x;
197         wy = allocation.y;
198         
199         gtk_widget_size_request(GTK_WIDGET(menu), &mreq);
200         screen = gtk_widget_get_screen (widget);
201         monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
202         gdk_screen_get_monitor_geometry (screen, monitor_num, 
203                                          &monitor);
204
205         *x = *x + wx;
206         *y = *y + wy + wheight;
207         
208         if (*y + mreq.height >= monitor.height)
209                 *y -= mreq.height;
210 }
211
212 gint menu_find_option_menu_index(GtkCMOptionMenu *optmenu, gpointer data,
213                                  GCompareFunc func)
214 {
215         GtkWidget *menu;
216         GtkWidget *menuitem;
217         gpointer menu_data;
218         GList *children;
219         GList *cur;
220         gint n, found = -1;
221
222         menu = gtk_cmoption_menu_get_menu(optmenu);
223         children = gtk_container_get_children(GTK_CONTAINER(GTK_MENU_SHELL(menu)));
224
225         for (cur = children, n = 0;
226              cur != NULL; cur = cur->next, n++) {
227                 menuitem = GTK_WIDGET(cur->data);
228                 menu_data = g_object_get_data(G_OBJECT(menuitem),
229                                               MENU_VAL_ID);
230                 if (func) {
231                         if (func(menu_data, data) == 0) {
232                                 found = n;
233                                 break;
234                         }
235                 } else if (menu_data == data) {
236                         found = n;
237                         break;
238                 }
239         }
240
241         g_list_free(children);
242
243         return found;
244 }