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