0684b6fa753a0dc028d45c42bbb680c301ef9f8a
[claws.git] / src / gtk / menu.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 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
36 #ifdef MAEMO
37 #ifdef CHINOOK
38 #include <hildon/hildon-program.h>
39 #else
40 #include <hildon-widgets/hildon-program.h>
41 #endif
42 #endif
43
44 GtkActionGroup *cm_menu_create_action_group(const gchar *name, GtkActionEntry *entries,
45                                             gint num_entries, gpointer data)
46 {
47         GtkActionGroup *group = gtk_action_group_new(name);
48         gtk_action_group_set_translate_func(group, menu_translate, NULL, NULL);
49         gtk_action_group_add_actions(group, entries, num_entries, data);
50         gtk_ui_manager_insert_action_group(gtkut_ui_manager(), group, 0);
51         return group;
52 }
53
54 GtkActionGroup *cm_menu_create_action_group_full(GtkUIManager *manager, const gchar *name, GtkActionEntry *entries,
55                                             gint num_entries, gpointer data)
56 {
57         GtkActionGroup *group = gtk_action_group_new(name);
58         gtk_action_group_set_translate_func(group, menu_translate, NULL, NULL);
59         gtk_action_group_add_actions(group, entries, num_entries, data);
60         gtk_ui_manager_insert_action_group(manager, group, 0);
61         return group;
62 }
63
64 gchar *menu_translate(const gchar *path, gpointer data)
65 {
66         gchar *retval;
67
68         retval = gettext(path);
69
70         return retval;
71 }
72
73 void cm_menu_set_sensitive(gchar *menu, gboolean sensitive)
74 {
75         GtkUIManager *gui_manager = gtkut_ui_manager();
76         gchar *path = g_strdup_printf("Menus/%s", menu);
77
78         cm_menu_set_sensitive_full(gui_manager, path, sensitive);
79         g_free(path);
80 }
81
82 void cm_toggle_menu_set_active(gchar *menu, gboolean active)
83 {
84         GtkUIManager *gui_manager = gtkut_ui_manager();
85         gchar *path = g_strdup_printf("Menus/%s", menu);
86
87         cm_toggle_menu_set_active_full(gui_manager, path, active);
88         g_free(path);
89 }
90
91 void cm_menu_set_sensitive_full(GtkUIManager *gui_manager, gchar *menu, gboolean sensitive)
92 {
93         GtkWidget *widget;
94         gchar *path = g_strdup_printf("/%s/", menu);
95
96         widget = gtk_ui_manager_get_widget(gui_manager, path);
97         if( !GTK_IS_WIDGET(widget) ) {
98                 g_message("Blah, '%s' is not a widget.\n", path);
99         }
100
101         if( !GTK_IS_MENU_ITEM(widget) ) {
102                 g_message("Blah, '%s' is not a menu item.\n", path);
103         }
104
105         gtk_widget_set_sensitive(widget, sensitive);
106         g_free(path);
107 }
108
109 gchar *cm_menu_item_get_shortcut(GtkUIManager *gui_manager, gchar *menu)
110 {
111         GtkWidget *widget;
112         gchar *path = g_strdup_printf("/%s/", menu);
113         const gchar *accel = NULL;
114         GtkAccelKey key;
115
116         widget = gtk_ui_manager_get_widget(gui_manager, path);
117         if( !GTK_IS_WIDGET(widget) ) {
118                 g_message("Blah, '%s' is not a widget.\n", path);
119         }
120
121         if( !GTK_IS_MENU_ITEM(widget) ) {
122                 g_message("Blah, '%s' is not a menu item.\n", path);
123         }
124
125         g_free(path);
126
127         accel = gtk_menu_item_get_accel_path(GTK_MENU_ITEM(widget));
128
129         if (accel && gtk_accel_map_lookup_entry(accel, &key))
130                 return gtk_accelerator_get_label (key.accel_key, key.accel_mods);
131         else
132                 return g_strdup(_("None"));
133
134 }
135
136 void cm_toggle_menu_set_active_full(GtkUIManager *gui_manager, gchar *menu, gboolean active)
137 {
138         GtkWidget *widget;
139         gchar *path = g_strdup_printf("/%s/", menu);
140
141         widget = gtk_ui_manager_get_widget(gui_manager, path);
142         if( !GTK_IS_WIDGET(widget) ) {
143                 g_message("Blah, '%s' is not a widget.\n", path);
144         }
145
146         if( !GTK_CHECK_MENU_ITEM(widget) ) {
147                 g_message("Blah, '%s' is not a check menu item.\n", path);
148         }
149
150         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), active);
151         g_free(path);
152 }
153
154 void menu_set_sensitive_all(GtkMenuShell *menu_shell, gboolean sensitive)
155 {
156         GList *children = gtk_container_get_children(GTK_CONTAINER(menu_shell));
157         GList *cur;
158
159         for (cur = children; cur != NULL; cur = cur->next)
160                 gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
161
162         g_list_free(children);
163 }
164
165 void menu_button_position(GtkMenu *menu, gint *x, gint *y, gboolean *push_in,
166                           gpointer user_data)
167 {
168         GtkWidget *widget;
169         gint wheight;
170         gint wx, wy;
171         GtkAllocation allocation;
172         GtkRequisition mreq, wreq;
173         GdkScreen *screen;
174         GdkRectangle monitor;
175         gint monitor_num;
176
177         cm_return_if_fail(x && y);
178         cm_return_if_fail(GTK_IS_BUTTON(user_data));
179
180         widget = GTK_WIDGET(user_data);
181
182         gdk_window_get_origin(gtk_widget_get_window(widget), x, y);
183         gtk_widget_get_requisition(widget, &wreq);
184         wheight = wreq.height;
185         gtk_widget_get_allocation(widget, &allocation);
186         wx = allocation.x;
187         wy = allocation.y;
188         
189         gtk_widget_size_request(GTK_WIDGET(menu), &mreq);
190         screen = gtk_widget_get_screen (widget);
191         monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
192         gdk_screen_get_monitor_geometry (screen, monitor_num, 
193                                          &monitor);
194
195         *x = *x + wx;
196         *y = *y + wy + wheight;
197         
198         if (*y + mreq.height >= monitor.height)
199                 *y -= mreq.height;
200 }
201
202 #if !GTK_CHECK_VERSION(3, 0, 0)
203 gint menu_find_option_menu_index(GtkCMOptionMenu *optmenu, gpointer data,
204                                  GCompareFunc func)
205 {
206         GtkWidget *menu;
207         GtkWidget *menuitem;
208         gpointer menu_data;
209         GList *children;
210         GList *cur;
211         gint n;
212
213         menu = gtk_cmoption_menu_get_menu(optmenu);
214         children = gtk_container_get_children(GTK_CONTAINER(GTK_MENU_SHELL(menu)));
215
216         for (cur = children, n = 0;
217              cur != NULL; cur = cur->next, n++) {
218                 menuitem = GTK_WIDGET(cur->data);
219                 menu_data = g_object_get_data(G_OBJECT(menuitem),
220                                               MENU_VAL_ID);
221                 if (func) {
222                         if (func(menu_data, data) == 0)
223                                 return n;
224                 } else if (menu_data == data)
225                         return n;
226         }
227
228         g_list_free(children);
229
230         return -1;
231 }
232 #endif