upd jpilot, fix editldap_basedn
[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 static gchar *menu_translate(const gchar *path, gpointer data)
67 {
68         gchar *retval;
69
70         retval = gettext(path);
71
72         return retval;
73 }
74
75 void menu_set_sensitive(GtkItemFactory *ifactory, const gchar *path,
76                         gboolean sensitive)
77 {
78         GtkWidget *widget;
79
80         g_return_if_fail(ifactory != NULL);
81
82         widget = gtk_item_factory_get_item(ifactory, path);
83         if(widget == NULL) {
84                 debug_print(_("unknown menu entry %s\n"), path);
85                 return;
86         }
87         gtk_widget_set_sensitive(widget, sensitive);
88 }
89
90 void menu_set_insensitive_all(GtkMenuShell *menu_shell)
91 {
92         GList *cur;
93
94         for (cur = menu_shell->children; cur != NULL; cur = cur->next)
95                 gtk_widget_set_sensitive(GTK_WIDGET(cur->data), FALSE);
96 }
97
98 void menu_set_toggle(GtkItemFactory *ifactory, const gchar *path,
99                         gboolean active)
100 {
101         GtkWidget *widget;
102
103         g_return_if_fail(ifactory != NULL);
104
105         widget = gtk_item_factory_get_item(ifactory, path);
106         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(widget), active);
107 }
108
109 void menu_button_position(GtkMenu *menu, gint *x, gint *y, gpointer user_data)
110 {
111         GtkWidget *button;
112         GtkRequisition requisition;
113         gint button_xpos, button_ypos;
114         gint xpos, ypos;
115         gint width, height;
116         gint scr_width, scr_height;
117
118         g_return_if_fail(user_data != NULL);
119         g_return_if_fail(GTK_IS_BUTTON(user_data));
120
121         button = GTK_WIDGET(user_data);
122
123         gtk_widget_get_child_requisition(GTK_WIDGET(menu), &requisition);
124         width = requisition.width;
125         height = requisition.height;
126         gdk_window_get_origin(button->window, &button_xpos, &button_ypos);
127
128         xpos = button_xpos;
129         ypos = button_ypos + button->allocation.height;
130
131         scr_width = gdk_screen_width();
132         scr_height = gdk_screen_height();
133
134         if (xpos + width > scr_width)
135                 xpos -= (xpos + width) - scr_width;
136         if (ypos + height > scr_height)
137                 ypos = button_ypos - height;
138         if (xpos < 0)
139                 xpos = 0;
140         if (ypos < 0)
141                 ypos = 0;
142
143         *x = xpos;
144         *y = ypos;
145 }