0.8.6claws96
[claws.git] / src / menu.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <gtk/gtkwidget.h>
26 #include <gtk/gtkmenu.h>
27 #include <gtk/gtkmenubar.h>
28 #include <gtk/gtkitemfactory.h>
29 #include <gtk/gtkcheckmenuitem.h>
30 #include <gtk/gtkbutton.h>
31
32 #include "intl.h"
33 #include "menu.h"
34 #include "utils.h"
35
36 static gchar *menu_translate(const gchar *path, gpointer data);
37
38 GtkWidget *menubar_create(GtkWidget *window, GtkItemFactoryEntry *entries,
39                           guint n_entries, const gchar *path, gpointer data)
40 {
41         GtkItemFactory *factory;
42         GtkAccelGroup *accel_group;
43
44         accel_group = gtk_accel_group_new();
45         factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, path, accel_group);
46         gtk_item_factory_set_translate_func(factory, menu_translate,
47                                             NULL, NULL);
48         gtk_item_factory_create_items(factory, n_entries, entries, data);
49         gtk_accel_group_attach(accel_group, GTK_OBJECT(window));
50
51         return gtk_item_factory_get_widget(factory, path);
52 }
53
54 GtkWidget *menu_create_items(GtkItemFactoryEntry *entries,
55                              guint n_entries, const gchar *path,
56                              GtkItemFactory **factory, gpointer data)
57 {
58         *factory = gtk_item_factory_new(GTK_TYPE_MENU, path, NULL);
59         gtk_item_factory_set_translate_func(*factory, menu_translate,
60                                             NULL, NULL);
61         gtk_item_factory_create_items(*factory, n_entries, entries, data);
62
63         return gtk_item_factory_get_widget(*factory, path);
64 }
65
66 GtkWidget *popupmenu_create(GtkWidget *window, GtkItemFactoryEntry *entries,
67                              guint n_entries, const gchar *path, gpointer data)
68 {
69         GtkItemFactory *factory;
70         GtkAccelGroup *accel_group;
71
72         accel_group = gtk_accel_group_new();
73         factory = gtk_item_factory_new(GTK_TYPE_MENU, path, accel_group);
74         gtk_item_factory_set_translate_func(factory, menu_translate,
75                                             NULL, NULL);
76         gtk_item_factory_create_items(factory, n_entries, entries, data);
77         gtk_accel_group_attach(accel_group, GTK_OBJECT(window));
78
79         return gtk_item_factory_get_widget(factory, path);
80 }
81
82 static gchar *menu_translate(const gchar *path, gpointer data)
83 {
84         gchar *retval;
85
86         retval = gettext(path);
87
88         return retval;
89 }
90
91 void menu_set_sensitive(GtkItemFactory *ifactory, const gchar *path,
92                         gboolean sensitive)
93 {
94         GtkWidget *widget;
95
96         g_return_if_fail(ifactory != NULL);
97
98         widget = gtk_item_factory_get_item(ifactory, path);
99         if(widget == NULL) {
100                 debug_print("unknown menu entry %s\n", path);
101                 return;
102         }
103         gtk_widget_set_sensitive(widget, sensitive);
104 }
105
106 void menu_set_sensitive_all(GtkMenuShell *menu_shell, gboolean sensitive)
107 {
108         GList *cur;
109
110         for (cur = menu_shell->children; cur != NULL; cur = cur->next)
111                 gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
112 }
113
114 void menu_set_toggle(GtkItemFactory *ifactory, const gchar *path,
115                         gboolean active)
116 {
117         GtkWidget *widget;
118
119         g_return_if_fail(ifactory != NULL);
120
121         widget = gtk_item_factory_get_item(ifactory, path);
122         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(widget), active);
123 }
124
125 void menu_toggle_toggle(GtkItemFactory *ifactory, const gchar *path)
126 {
127         GtkWidget *widget;
128         
129         g_return_if_fail(ifactory != NULL);
130         
131         widget = gtk_item_factory_get_item(ifactory, path);
132         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), !((GTK_CHECK_MENU_ITEM(widget))->active));
133 }
134
135 void menu_button_position(GtkMenu *menu, gint *x, gint *y, gpointer user_data)
136 {
137         GtkWidget *button;
138         GtkRequisition requisition;
139         gint button_xpos, button_ypos;
140         gint xpos, ypos;
141         gint width, height;
142         gint scr_width, scr_height;
143
144         g_return_if_fail(user_data != NULL);
145         g_return_if_fail(GTK_IS_BUTTON(user_data));
146
147         button = GTK_WIDGET(user_data);
148
149         gtk_widget_get_child_requisition(GTK_WIDGET(menu), &requisition);
150         width = requisition.width;
151         height = requisition.height;
152         gdk_window_get_origin(button->window, &button_xpos, &button_ypos);
153
154         xpos = button_xpos;
155         ypos = button_ypos + button->allocation.height;
156
157         scr_width = gdk_screen_width();
158         scr_height = gdk_screen_height();
159
160         if (xpos + width > scr_width)
161                 xpos -= (xpos + width) - scr_width;
162         if (ypos + height > scr_height)
163                 ypos = button_ypos - height;
164         if (xpos < 0)
165                 xpos = 0;
166         if (ypos < 0)
167                 ypos = 0;
168
169         *x = xpos;
170         *y = ypos;
171 }
172
173 gint menu_find_option_menu_index(GtkOptionMenu *optmenu, gpointer data,
174                                  GCompareFunc func)
175 {
176         GtkWidget *menu;
177         GtkWidget *menuitem;
178         gpointer menu_data;
179         GList *cur;
180         gint n;
181
182         menu = gtk_option_menu_get_menu(optmenu);
183
184         for (cur = GTK_MENU_SHELL(menu)->children, n = 0;
185              cur != NULL; cur = cur->next, n++) {
186                 menuitem = GTK_WIDGET(cur->data);
187                 menu_data = gtk_object_get_user_data(GTK_OBJECT(menuitem));
188                 if (func) {
189                         if (func(menu_data, data) == 0)
190                                 return n;
191                 } else if (menu_data == data)
192                         return n;
193         }
194
195         return -1;
196 }
197