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