aboutsummaryrefslogtreecommitdiffstats
path: root/src/ephy-navigation-action.c
blob: 943126fbfc3d05ff809b5f6ce45e4fa07036a6b6 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Copyright © 2003, 2004 Marco Pesenti Gritti
 *  Copyright © 2003, 2004 Christian Persch
 *  Copyright © 2008 Jan Alonzo
 *  Copyright © 2009 Igalia S.L.
 *
 *  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"
#include "ephy-navigation-action.h"

#include "ephy-debug.h"
#include "ephy-embed-shell.h"
#include "ephy-favicon-cache.h"
#include "ephy-history.h"
#include "ephy-link.h"
#include "ephy-shell.h"
#include "ephy-type-builtins.h"
#include "ephy-window.h"

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

#define EPHY_NAVIGATION_ACTION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_NAVIGATION_ACTION, EphyNavigationActionPrivate))

struct _EphyNavigationActionPrivate
{
    EphyWindow *window;
    char *arrow_tooltip;
    guint statusbar_cid;
};

enum
{
    PROP_0,
    PROP_ARROW_TOOLTIP,
    PROP_WINDOW
};

static void ephy_navigation_action_init       (EphyNavigationAction *action);
static void ephy_navigation_action_class_init (EphyNavigationActionClass *class);

G_DEFINE_TYPE (EphyNavigationAction, ephy_navigation_action, EPHY_TYPE_LINK_ACTION)


#define MAX_LABEL_LENGTH 48

static GtkWidget *
build_dropdown_menu (EphyNavigationAction *action)
{
    EphyNavigationActionClass *class = EPHY_NAVIGATION_ACTION_GET_CLASS (action);

    return class->build_dropdown_menu (action);
}

static void
menu_activated_cb (GtkMenuToolButton *button,
           EphyNavigationAction *action)
{
    GtkWidget *menu = NULL;

    LOG ("menu_activated_cb");

    menu = build_dropdown_menu (action);
    gtk_menu_tool_button_set_menu (button, menu);
}

static void
connect_proxy (GtkAction *gaction,
           GtkWidget *proxy)
{
    LOG ("Connect navigation action proxy");

    if (GTK_IS_MENU_TOOL_BUTTON (proxy))
    {
        EphyNavigationAction *action = EPHY_NAVIGATION_ACTION (gaction);
        EphyNavigationActionPrivate *priv = action->priv;
        GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (proxy);
        GtkWidget *menu;

        /* set dummy menu so the arrow gets sensitive */
        menu = gtk_menu_new ();
        gtk_menu_tool_button_set_menu (button, menu);
        gtk_menu_tool_button_set_arrow_tooltip_text (button, priv->arrow_tooltip);

        g_signal_connect (proxy, "show-menu",
                  G_CALLBACK (menu_activated_cb), gaction);
    }

    GTK_ACTION_CLASS (ephy_navigation_action_parent_class)->connect_proxy (gaction, proxy);
}

static void
ephy_navigation_action_init (EphyNavigationAction *action)
{
    action->priv = EPHY_NAVIGATION_ACTION_GET_PRIVATE (action);
}

static void
ephy_navigation_action_finalize (GObject *object)
{
    EphyNavigationAction *action = EPHY_NAVIGATION_ACTION (object);

    g_free (action->priv->arrow_tooltip);

    G_OBJECT_CLASS (ephy_navigation_action_parent_class)->finalize (object);
}

static void
ephy_navigation_action_set_property (GObject *object,
                     guint prop_id,
                     const GValue *value,
                     GParamSpec *pspec)
{
    EphyNavigationAction *nav = EPHY_NAVIGATION_ACTION (object);

    switch (prop_id)
    {
        case PROP_ARROW_TOOLTIP:
            nav->priv->arrow_tooltip = g_value_dup_string (value);
            g_object_notify (object, "tooltip");
            break;
        case PROP_WINDOW:
            {
                GtkWidget *statusbar;

                nav->priv->window = EPHY_WINDOW (g_value_get_object (value));

                /* statusbar context to display current selected item */
                statusbar = ephy_window_get_statusbar (nav->priv->window);

                nav->priv->statusbar_cid = gtk_statusbar_get_context_id (
                                GTK_STATUSBAR (statusbar), 
                                "navigation_message");
            }
            break;
    }
}

static void
ephy_navigation_action_get_property (GObject *object,
                     guint prop_id,
                     GValue *value,
                     GParamSpec *pspec)
{
    EphyNavigationAction *nav = EPHY_NAVIGATION_ACTION (object);

    switch (prop_id)
    {
        case PROP_ARROW_TOOLTIP:
            g_value_set_string (value, nav->priv->arrow_tooltip);
            break;
        case PROP_WINDOW:
            g_value_set_object (value, nav->priv->window);
            break;
    }
}

static void
ephy_navigation_action_class_init (EphyNavigationActionClass *class)
{
    GObjectClass *object_class = G_OBJECT_CLASS (class);
    GtkActionClass *action_class = GTK_ACTION_CLASS (class);

    object_class->finalize = ephy_navigation_action_finalize;
    object_class->set_property = ephy_navigation_action_set_property;
    object_class->get_property = ephy_navigation_action_get_property;

    action_class->toolbar_item_type = GTK_TYPE_MENU_TOOL_BUTTON;
    action_class->connect_proxy = connect_proxy;

    class->build_dropdown_menu = NULL;

    g_object_class_install_property (object_class,
                     PROP_ARROW_TOOLTIP,
                     g_param_spec_string ("arrow-tooltip", NULL, NULL,
                                  NULL,
                                  G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));

    g_object_class_install_property (object_class,
                     PROP_WINDOW,
                     g_param_spec_object ("window", NULL, NULL,
                                  G_TYPE_OBJECT,
                                  G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));

    g_type_class_add_private (object_class, sizeof (EphyNavigationActionPrivate));
}

EphyWindow *
_ephy_navigation_action_get_window (EphyNavigationAction *action)
{
      g_return_val_if_fail (EPHY_IS_NAVIGATION_ACTION (action), NULL);

      return action->priv->window;
}

guint
_ephy_navigation_action_get_statusbar_context_id (EphyNavigationAction *action)
{
      g_return_val_if_fail (EPHY_IS_NAVIGATION_ACTION (action), 0);

      return action->priv->statusbar_cid;
}

GtkWidget *
_ephy_navigation_action_new_history_menu_item (const char *origtext,
                           const char *address)
{
    EphyFaviconCache *cache;
    EphyHistory *history;
    GtkWidget *item, *image;
    GdkPixbuf *icon = NULL;
    GtkLabel *label;
    const char *icon_address;

    g_return_val_if_fail (address != NULL && origtext != NULL, NULL);

    item = gtk_image_menu_item_new_with_label (origtext);

    label = GTK_LABEL (gtk_bin_get_child (GTK_BIN (item)));
    gtk_label_set_ellipsize (label, PANGO_ELLIPSIZE_END);
    gtk_label_set_max_width_chars (label, MAX_LABEL_LENGTH);

    history = EPHY_HISTORY
        (ephy_embed_shell_get_global_history (embed_shell));
    icon_address = ephy_history_get_icon (history, address);

    cache = EPHY_FAVICON_CACHE
        (ephy_embed_shell_get_favicon_cache (embed_shell));
    icon = ephy_favicon_cache_get (cache, icon_address);

    if (icon != NULL)
    {
        image = gtk_image_new_from_pixbuf (icon);
        gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
        gtk_widget_show (image);
        g_object_unref (icon);
    }

    gtk_widget_show (item);

    return item;
}