aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/misc/e-popup-menu.c
blob: da28926677542924f7c50eb2328f8353e931dc4b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * e-popup-menu.c: popup menu display
nnn *
 * Authors:
 *   Miguel de Icaza (miguel@kernel.org)
 *   Jody Goldberg (jgoldberg@home.com)
 *   Jeffrey Stedfast <fejj@helixcode.com>
 *
 * (C) 2000 Helix Code, Inc.
 */
#include <config.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtkaccellabel.h>
#include <gtk/gtklabel.h>
#include <gtk/gtkmenuitem.h>
#include <gtk/gtksignal.h>
#include <libgnomeui/gtkpixmapmenuitem.h>
#include <libgnomeui/gnome-stock.h>

#include "e-popup-menu.h"
#include "e-gui-utils.h"

#include <libgnome/gnome-i18n.h>

#ifndef GNOME_APP_HELPER_H
/* Copied this i18n function to use for the same purpose */

#ifdef ENABLE_NLS
#define L_(x) gnome_app_helper_gettext(x)

static gchar *
gnome_app_helper_gettext (const gchar *str)
{
    char *s;

        s = gettext (str);
    if ( s == str )
            s = dgettext (PACKAGE, str);

    return s;
}

#else
#define L_(x) x
#endif

#endif

/*
 * Creates an item with an optional icon
 */
static GtkWidget *
make_item (GtkMenu *menu, const char *name, const char *pixname)
{
    GtkWidget *label, *item;
    guint label_accel;

    if (*name == '\0')
        return gtk_menu_item_new ();
    
    /*
     * Ugh.  This needs to go into Gtk+
     */
    label = gtk_accel_label_new ("");
    label_accel = gtk_label_parse_uline (GTK_LABEL (label), name);
    gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
    gtk_widget_show (label);
    
    item = pixname ? gtk_pixmap_menu_item_new () : gtk_menu_item_new ();
    gtk_container_add (GTK_CONTAINER (item), label);
    
    if (label_accel != GDK_VoidSymbol){
        gtk_widget_add_accelerator (
            item,
            "activate_item",
            gtk_menu_ensure_uline_accel_group (GTK_MENU (menu)),
            label_accel, 0,
            GTK_ACCEL_LOCKED);
    }

    if (pixname){
        GtkWidget *pixmap = gnome_stock_pixmap_widget (item, pixname);

        gtk_widget_show (pixmap);
        gtk_pixmap_menu_item_set_pixmap (
            GTK_PIXMAP_MENU_ITEM (item), pixmap);
    }

    return item;
}

GtkMenu *
e_popup_menu_create (EPopupMenu *menu_list, guint32 disable_mask, guint32 hide_mask, void *closure)
{
    GtkMenu *menu = GTK_MENU (gtk_menu_new ());
    gboolean last_item_seperator = TRUE;
    gint last_non_seperator = -1;
    gint i;
    
    for (i = 0; menu_list[i].name; i++) {
        if (strcmp ("", menu_list[i].name) && !(menu_list [i].disable_mask & hide_mask)) {
            last_non_seperator = i;
        }
    }
    
    for (i = 0; i <= last_non_seperator; i++) {
        gboolean seperator;
        
        seperator = !strcmp ("", menu_list[i].name);
        
        if ((!(seperator && last_item_seperator)) && !(menu_list [i].disable_mask & hide_mask)) {
            GtkWidget *item;
            
            item = make_item (menu, seperator ? "" : L_(menu_list[i].name), menu_list[i].pixname);
            gtk_menu_append (menu, item);
            
            if (!menu_list[i].submenu) {
                if (menu_list[i].fn)
                    gtk_signal_connect (GTK_OBJECT (item), "activate",
                                GTK_SIGNAL_FUNC (menu_list[i].fn),
                                closure);
            } else {
                /* submenu */
                GtkMenu *submenu;
                
                submenu = e_popup_menu_create (menu_list[i].submenu, disable_mask, hide_mask, closure);
                
                gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), GTK_WIDGET (submenu));
            }
            
            if (menu_list[i].disable_mask & disable_mask)
                gtk_widget_set_sensitive (item, FALSE);
            
            gtk_widget_show (item);
        }
        
        last_item_seperator = seperator;
    }
    
    return menu;
}

void
e_popup_menu_run (EPopupMenu *menu_list, GdkEvent *event, guint32 disable_mask, guint32 hide_mask, void *closure)
{
    GtkMenu *menu;
    
    g_return_if_fail (menu_list != NULL);
    g_return_if_fail (event != NULL);
    
    menu = e_popup_menu_create (menu_list, disable_mask, hide_mask, closure);
    
    e_popup_menu (menu, event);
}