2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2013 Hiroyuki Yamamoto and the Claws Mail team
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.
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.
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/>.
22 #include "claws-features.h"
26 #include <glib/gi18n.h>
29 #include "gtkcmoptionmenu.h"
35 GtkActionGroup *cm_menu_create_action_group(const gchar *name, GtkActionEntry *entries,
36 gint num_entries, gpointer data)
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);
45 GtkActionGroup *cm_menu_create_action_group_full(GtkUIManager *manager, const gchar *name, GtkActionEntry *entries,
46 gint num_entries, gpointer data)
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);
55 gchar *menu_translate(const gchar *path, gpointer data)
59 retval = gettext(path);
64 void cm_menu_set_sensitive(gchar *menu, gboolean sensitive)
66 GtkUIManager *gui_manager = gtkut_ui_manager();
67 gchar *path = g_strdup_printf("Menus/%s", menu);
69 cm_menu_set_sensitive_full(gui_manager, path, sensitive);
73 void cm_toggle_menu_set_active(gchar *menu, gboolean active)
75 GtkUIManager *gui_manager = gtkut_ui_manager();
76 gchar *path = g_strdup_printf("Menus/%s", menu);
78 cm_toggle_menu_set_active_full(gui_manager, path, active);
82 void cm_menu_set_sensitive_full(GtkUIManager *gui_manager, const gchar *menu, gboolean sensitive)
85 gchar *path = g_strdup_printf("/%s/", menu);
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);
92 if( !GTK_IS_MENU_ITEM(widget) ) {
93 g_message("Blah, '%s' is not a menu item.\n", path);
96 gtk_widget_set_sensitive(widget, sensitive);
98 if (strcmp(menu, "Menus/SummaryViewPopup/Reedit") == 0)
99 (sensitive)? gtk_widget_show(widget) : gtk_widget_hide(widget);
104 gchar *cm_menu_item_get_shortcut(GtkUIManager *gui_manager, gchar *menu)
107 gchar *path = g_strdup_printf("/%s/", menu);
108 const gchar *accel = NULL;
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);
116 if( !GTK_IS_MENU_ITEM(widget) ) {
117 g_message("Blah, '%s' is not a menu item.\n", path);
122 accel = gtk_menu_item_get_accel_path(GTK_MENU_ITEM(widget));
124 if (accel && gtk_accel_map_lookup_entry(accel, &key))
125 return gtk_accelerator_get_label (key.accel_key, key.accel_mods);
127 return g_strdup(_("None"));
131 GtkWidget *cm_menu_item_new_label_from_url(gchar *url)
133 gint len = strlen(url);
134 if (len > MAX_MENU_LABEL_LENGTH) {
135 g_message("Refusing a %d bytes string as menu label\n", len);
136 url[64] = '\0', url[63] = url[62] = url[61] = '.', url[60] = ' ';
137 GtkWidget *newlabel = gtk_menu_item_new_with_label(url);
138 gtk_widget_set_tooltip_markup(GTK_WIDGET(newlabel),
139 g_strconcat("<span><b>", _("Warning:"), "</b>",
140 _("This URL was too long for displaying and\n"
141 "has been truncated for safety. This message could be\n"
142 "corrupted, malformed or part of some DoS attempt."),
147 return gtk_menu_item_new_with_label(url);
150 void cm_toggle_menu_set_active_full(GtkUIManager *gui_manager, gchar *menu, gboolean active)
153 gchar *path = g_strdup_printf("/%s/", menu);
155 widget = gtk_ui_manager_get_widget(gui_manager, path);
156 if( !GTK_IS_WIDGET(widget) ) {
157 g_message("Blah, '%s' is not a widget.\n", path);
160 if( !GTK_CHECK_MENU_ITEM(widget) ) {
161 g_message("Blah, '%s' is not a check menu item.\n", path);
164 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), active);
168 void menu_set_sensitive_all(GtkMenuShell *menu_shell, gboolean sensitive)
170 GList *children = gtk_container_get_children(GTK_CONTAINER(menu_shell));
173 for (cur = children; cur != NULL; cur = cur->next)
174 gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
176 g_list_free(children);
179 void menu_button_position(GtkMenu *menu, gint *x, gint *y, gboolean *push_in,
185 GtkAllocation allocation;
186 GtkRequisition mreq, wreq;
188 GdkRectangle monitor;
191 cm_return_if_fail(x && y);
192 cm_return_if_fail(GTK_IS_BUTTON(user_data));
194 widget = GTK_WIDGET(user_data);
196 gdk_window_get_origin(gtk_widget_get_window(widget), x, y);
197 gtk_widget_get_requisition(widget, &wreq);
198 wheight = wreq.height;
199 gtk_widget_get_allocation(widget, &allocation);
203 gtk_widget_size_request(GTK_WIDGET(menu), &mreq);
204 screen = gtk_widget_get_screen (widget);
205 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
206 gdk_screen_get_monitor_geometry (screen, monitor_num,
210 *y = *y + wy + wheight;
212 if (*y + mreq.height >= monitor.height)
216 gint menu_find_option_menu_index(GtkCMOptionMenu *optmenu, gpointer data,
226 menu = gtk_cmoption_menu_get_menu(optmenu);
227 children = gtk_container_get_children(GTK_CONTAINER(GTK_MENU_SHELL(menu)));
229 for (cur = children, n = 0;
230 cur != NULL; cur = cur->next, n++) {
231 menuitem = GTK_WIDGET(cur->data);
232 menu_data = g_object_get_data(G_OBJECT(menuitem),
235 if (func(menu_data, data) == 0) {
239 } else if (menu_data == data) {
245 g_list_free(children);