aboutsummaryrefslogtreecommitdiffstats
path: root/src/bookmarks/ephy-bookmark-toolitem.c
blob: b4245de29091ae507568f8be7f0d4f7e6f810c66 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 *  Copyright (C) 2003  Christian Persch
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "ephy-bookmark-toolitem.h"
#include "ephy-marshal.h"
#include "ephy-debug.h"

#include <gtk/gtk.h>
#include <gtk/gtkimagemenuitem.h>

static GObjectClass *parent_class = NULL;

static void ephy_bookmark_toolitem_class_init   (EphyBookmarkToolitemClass *klass);
static void ephy_bookmark_toolitem_init     (EphyBookmarkToolitem *item);

enum
{
    ACTIVATE,
    LAST_SIGNAL
};
static guint EphyBookmarkToolitemSignals[LAST_SIGNAL];

#define MENU_ID "ephy-bookmark-toolitem-menu-id"

/**
 * EphyBookmarkToolitem object
 */

GType
ephy_bookmark_toolitem_get_type (void)
{
        static GType ephy_bookmark_toolitem_type = 0;

        if (ephy_bookmark_toolitem_type == 0)
        {
                static const GTypeInfo our_info =
            {
                sizeof (EphyBookmarkToolitemClass),
                NULL, /* base_init */
                NULL, /* base_finalize */
                (GClassInitFunc) ephy_bookmark_toolitem_class_init,
                NULL,
                NULL, /* class_data */
                sizeof (EphyBookmarkToolitem),
                0, /* n_preallocs */
                (GInstanceInitFunc) ephy_bookmark_toolitem_init,
            };

                ephy_bookmark_toolitem_type = g_type_register_static (EGG_TYPE_TOOL_ITEM,
                                      "EphyBookmarkToolitem",
                                       &our_info, 0);
        }

        return ephy_bookmark_toolitem_type;
}

static void
activated_cb (GtkWidget *widget, EggToolItem *item)
{
    char *text = NULL;

    if (GTK_IS_EDITABLE (widget))
    {
        text = gtk_editable_get_chars (GTK_EDITABLE (widget), 0, -1);
    }

    g_signal_emit (item, EphyBookmarkToolitemSignals[ACTIVATE], 0, text);

    g_free (text);
}

static gboolean
ephy_bookmark_toolitem_create_menu_proxy (EggToolItem *item)
{
    GtkWidget *menu_item, *label, *icon, *image;
    GdkPixbuf *pixbuf;
    const char *text;

    LOG ("create menu proxy for %p", item)
    
    icon = g_object_get_data (G_OBJECT (item), "icon");
    pixbuf = gtk_image_get_pixbuf (GTK_IMAGE (icon));
    image = gtk_image_new_from_pixbuf (pixbuf);

    label = g_object_get_data (G_OBJECT (item), "label");
    text = gtk_label_get_label (GTK_LABEL (label));

    menu_item = gtk_image_menu_item_new_with_mnemonic (text);
    gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item), image);

    g_signal_connect (menu_item, "activate", G_CALLBACK (activated_cb), item);

    egg_tool_item_set_proxy_menu_item (item, MENU_ID, menu_item);

    return TRUE;
}

static void
ephy_bookmark_toolitem_init (EphyBookmarkToolitem *item)
{
    GtkWidget *button, *hbox, *label, *icon, *entry;

    LOG ("Initialising bookmark toolitem %p", item)

    hbox = gtk_hbox_new (FALSE, 0);
    gtk_widget_show (hbox);
    gtk_container_add (GTK_CONTAINER (item), hbox);

    button = gtk_button_new ();
    gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
    gtk_widget_show (button);
    gtk_container_add (GTK_CONTAINER (hbox), button);
    g_object_set_data (G_OBJECT (item), "button", button);

    entry = gtk_entry_new ();
    gtk_widget_set_size_request (entry, 120, -1);
    gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
    g_object_set_data (G_OBJECT (item), "entry", entry);

    hbox = gtk_hbox_new (FALSE, 0);
    gtk_widget_show (hbox);
    gtk_container_add (GTK_CONTAINER (button), hbox);

    icon = gtk_image_new ();
    gtk_widget_show (icon);
    gtk_box_pack_start (GTK_BOX (hbox), icon, TRUE, TRUE, 0);
    g_object_set_data (G_OBJECT (item), "icon", icon);

    label = gtk_label_new (NULL);
    gtk_widget_show (label);
    gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
    g_object_set_data (G_OBJECT (item), "label", label);

    g_signal_connect (button, "clicked", G_CALLBACK (activated_cb), item);
    g_signal_connect (entry, "activate", G_CALLBACK (activated_cb), item);
}

static void
ephy_bookmark_toolitem_class_init (EphyBookmarkToolitemClass *klass)
{
    EggToolItemClass *tool_item_class;

    parent_class = g_type_class_peek_parent (klass);

    tool_item_class = (EggToolItemClass *)klass;

    tool_item_class->create_menu_proxy = ephy_bookmark_toolitem_create_menu_proxy;

    EphyBookmarkToolitemSignals[ACTIVATE] = g_signal_new (
            "activate", G_OBJECT_CLASS_TYPE (klass),
            G_SIGNAL_RUN_FIRST,
            G_STRUCT_OFFSET (EphyBookmarkToolitemClass, activated),
            NULL, NULL,
            ephy_marshal_VOID__STRING,
            G_TYPE_NONE,
            1,
            G_TYPE_STRING);
}