Statistics
| Revision:

root / src / menu.c @ 1

History | View | Annotate | Download (6.65 KB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 1999-2004 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/gtkcheckmenuitem.h>
29
#include <gtk/gtkitemfactory.h>
30
#include <gtk/gtkbutton.h>
31
#include <gtk/gtkwindow.h>
32

    
33
#include "intl.h"
34
#include "menu.h"
35
#include "utils.h"
36

    
37
static gchar *menu_translate(const gchar *path, gpointer data);
38

    
39
GtkWidget *menubar_create(GtkWidget *window, GtkItemFactoryEntry *entries,
40
                          guint n_entries, const gchar *path, gpointer data)
41
{
42
        GtkItemFactory *factory;
43

    
44
        factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, path, NULL);
45
        gtk_item_factory_set_translate_func(factory, menu_translate,
46
                                            NULL, NULL);
47
        gtk_item_factory_create_items(factory, n_entries, entries, data);
48
        gtk_window_add_accel_group(GTK_WINDOW(window), factory->accel_group);
49

    
50
        return gtk_item_factory_get_widget(factory, path);
51
}
52

    
53
GtkWidget *menu_create_items(GtkItemFactoryEntry *entries,
54
                             guint n_entries, const gchar *path,
55
                             GtkItemFactory **factory, gpointer data)
56
{
57
        *factory = gtk_item_factory_new(GTK_TYPE_MENU, path, NULL);
58
        gtk_item_factory_set_translate_func(*factory, menu_translate,
59
                                            NULL, NULL);
60
        gtk_item_factory_create_items(*factory, n_entries, entries, data);
61

    
62
        return gtk_item_factory_get_widget(*factory, path);
63
}
64

    
65
static gchar *menu_translate(const gchar *path, gpointer data)
66
{
67
        gchar *retval;
68

    
69
        retval = gettext(path);
70

    
71
        return retval;
72
}
73

    
74
#warning FIXME_GTK2
75
#if 0
76
static void factory_print_func(gpointer data, gchar *string)
77
{
78
        GString *out_str = data;
79

80
        g_string_append(out_str, string);
81
        g_string_append_c(out_str, '\n');
82
}
83

84
GString *menu_factory_get_rc(const gchar *path)
85
{
86
        GString *string;
87
        GtkPatternSpec *pspec;
88
        gchar pattern[256];
89

90
        pspec = g_new(GtkPatternSpec, 1);
91
        g_snprintf(pattern, sizeof(pattern), "%s*", path);
92
        gtk_pattern_spec_init(pspec, pattern);
93
        string = g_string_new("");
94
        gtk_item_factory_dump_items(pspec, FALSE, factory_print_func,
95
                                    string);
96
        gtk_pattern_spec_free_segs(pspec);
97

98
        return string;
99
}
100

101
void menu_factory_clear_rc(const gchar *rc_str)
102
{
103
        GString *string;
104
        gchar *p;
105
        gchar *start, *end;
106
        guint pos = 0;
107

108
        string = g_string_new(rc_str);
109
        while ((p = strstr(string->str + pos, "(menu-path \"")) != NULL) {
110
                pos = p + 12 - string->str;
111
                p = strchr(p + 12, '"');
112
                if (!p) continue;
113
                start = strchr(p + 1, '"');
114
                if (!start) continue;
115
                end = strchr(start + 1, '"');
116
                if (!end) continue;
117
                pos = start + 1 - string->str;
118
                if (end > start + 1)
119
                        g_string_erase(string, pos, end - (start + 1));
120
        }
121

122
        gtk_item_factory_parse_rc_string(string->str);
123
        g_string_free(string, TRUE);
124
}
125

126
void menu_factory_copy_rc(const gchar *src_path, const gchar *dest_path)
127
{
128
        GString *string;
129
        gint src_path_len;
130
        gint dest_path_len;
131
        gchar *p;
132
        guint pos = 0;
133

134
        string = menu_factory_get_rc(src_path);
135
        src_path_len = strlen(src_path);
136
        dest_path_len = strlen(dest_path);
137

138
        while ((p = strstr(string->str + pos, src_path)) != NULL) {
139
                pos = p - string->str;
140
                g_string_erase(string, pos, src_path_len);
141
                g_string_insert(string, pos, dest_path);
142
                pos += dest_path_len;
143
        }
144

145
        pos = 0;
146
        while ((p = strchr(string->str + pos, ';')) != NULL) {
147
                pos = p - string->str;
148
                if (pos == 0 || *(p - 1) == '\n')
149
                        g_string_erase(string, pos, 1);
150
        }
151

152
        menu_factory_clear_rc(string->str);
153
        gtk_item_factory_parse_rc_string(string->str);
154
        g_string_free(string, TRUE);
155
}
156
#endif
157

    
158
void menu_set_sensitive(GtkItemFactory *ifactory, const gchar *path,
159
                        gboolean sensitive)
160
{
161
        GtkWidget *widget;
162

    
163
        g_return_if_fail(ifactory != NULL);
164

    
165
        widget = gtk_item_factory_get_item(ifactory, path);
166
        gtk_widget_set_sensitive(widget, sensitive);
167
}
168

    
169
void menu_set_sensitive_all(GtkMenuShell *menu_shell, gboolean sensitive)
170
{
171
        GList *cur;
172

    
173
        for (cur = menu_shell->children; cur != NULL; cur = cur->next)
174
                gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
175
}
176

    
177
void menu_set_active(GtkItemFactory *ifactory, const gchar *path,
178
                     gboolean is_active)
179
{
180
        GtkWidget *widget;
181

    
182
        g_return_if_fail(ifactory != NULL);
183

    
184
        widget = gtk_item_factory_get_item(ifactory, path);
185
        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), is_active);
186
}
187

    
188
void menu_button_position(GtkMenu *menu, gint *x, gint *y, gboolean *push_in,
189
                          gpointer user_data)
190
{
191
        GtkWidget *button;
192
        GtkRequisition requisition;
193
        gint button_xpos, button_ypos;
194
        gint xpos, ypos;
195
        gint width, height;
196
        gint scr_width, scr_height;
197

    
198
        g_return_if_fail(x != NULL && y != NULL);
199
        g_return_if_fail(GTK_IS_BUTTON(user_data));
200

    
201
        button = GTK_WIDGET(user_data);
202

    
203
        gtk_widget_get_child_requisition(GTK_WIDGET(menu), &requisition);
204
        width = requisition.width;
205
        height = requisition.height;
206
        gdk_window_get_origin(button->window, &button_xpos, &button_ypos);
207

    
208
        xpos = button_xpos + button->allocation.x;
209
        ypos = button_ypos + button->allocation.y + button->requisition.height;
210

    
211
        scr_width = gdk_screen_width();
212
        scr_height = gdk_screen_height();
213

    
214
        if (xpos + width > scr_width)
215
                xpos -= (xpos + width) - scr_width;
216
        if (ypos + height > scr_height)
217
                ypos = button->requisition.height + height;
218
        if (xpos < 0)
219
                xpos = 0;
220
        if (ypos < 0)
221
                ypos = 0;
222

    
223
        *x = xpos;
224
        *y = ypos;
225
}
226

    
227
gint menu_find_option_menu_index(GtkOptionMenu *optmenu, gpointer data,
228
                                 GCompareFunc func)
229
{
230
        GtkWidget *menu;
231
        GtkWidget *menuitem;
232
        gpointer menu_data;
233
        GList *cur;
234
        gint n;
235

    
236
        menu = gtk_option_menu_get_menu(optmenu);
237

    
238
        for (cur = GTK_MENU_SHELL(menu)->children, n = 0;
239
             cur != NULL; cur = cur->next, n++) {
240
                menuitem = GTK_WIDGET(cur->data);
241
                menu_data = g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID);
242
                if (func) {
243
                        if (func(menu_data, data) == 0)
244
                                return n;
245
                } else if (menu_data == data)
246
                        return n;
247
        }
248

    
249
        return -1;
250
}
251

    
252
gint menu_get_option_menu_active_index(GtkOptionMenu *optmenu)
253
{
254
        GtkWidget *menu;
255
        GtkWidget *menuitem;
256

    
257
        menu = gtk_option_menu_get_menu(optmenu);
258
        menuitem = gtk_menu_get_active(GTK_MENU(menu));
259

    
260
        return GPOINTER_TO_INT
261
                (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
262
}