aboutsummaryrefslogtreecommitdiffstats
path: root/src/ephy-tab.c
blob: 8a3a7119a3fe6f72d81a54fba087f497d4e13088 (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Copyright © 2000-2003 Marco Pesenti Gritti
 *  Copyright © 2003, 2004, 2005 Christian Persch
 *  Copyright © 2004 Crispin Flowerday
 *  Copyright © 2004 Adam Hooper
 *
 *  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.
 *
 *  $Id$
 */

#include "config.h"

#include "ephy-tab.h"
#include "ephy-type-builtins.h"
#include "ephy-embed-type-builtins.h"
#include "eel-gconf-extensions.h"
#include "ephy-prefs.h"
#include "ephy-embed-prefs.h"
#include "ephy-debug.h"
#include "ephy-string.h"
#include "ephy-notebook.h"
#include "ephy-file-helpers.h"
#include "ephy-zoom.h"
#include "ephy-favicon-cache.h"
#include "ephy-history.h"
#include "ephy-embed-shell.h"
#include "ephy-embed-single.h"
#include "ephy-shell.h"
#include "ephy-permission-manager.h"
#include "ephy-link.h"

#include <glib/gi18n.h>
#include <gtk/gtklabel.h>
#include <gtk/gtkbutton.h>
#include <gtk/gtkstock.h>
#include <gtk/gtkmisc.h>
#include <gtk/gtkhbox.h>
#include <gtk/gtkimage.h>
#include <gtk/gtkiconfactory.h>
#include <gtk/gtkstyle.h>
#include <gtk/gtkselection.h>
#include <gtk/gtkmain.h>
#include <gtk/gtkmenu.h>
#include <gtk/gtkuimanager.h>
#include <gtk/gtkclipboard.h>
#include <libgnomevfs/gnome-vfs.h>
#include <libgnomevfs/gnome-vfs-result.h>
#include <libgnomevfs/gnome-vfs-monitor.h>
#include <libgnomevfs/gnome-vfs-ops.h>
#include <libgnomevfs/gnome-vfs-uri.h>
#include <string.h>

#ifdef ENABLE_PYTHON
#include "ephy-python.h"
#endif

#define EPHY_TAB_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_TAB, EphyTabPrivate))

struct _EphyTabPrivate
{
    int width;
    int height;
    guint idle_resize_handler;
};

static void ephy_tab_class_init     (EphyTabClass *klass);
static void ephy_tab_init       (EphyTab *tab);
static void ephy_tab_dispose        (GObject *object);
static void ephy_tab_finalize       (GObject *object);

enum
{
    PROP_0,
};

static GObjectClass *parent_class;

/* Class functions */

GType
ephy_tab_get_type (void)
{
        static GType type = 0;

        if (G_UNLIKELY (type == 0))
        {
                const GTypeInfo our_info =
                {
                        sizeof (EphyTabClass),
                        NULL, /* base_init */
                        NULL, /* base_finalize */
                        (GClassInitFunc) ephy_tab_class_init,
                        NULL,
                        NULL, /* class_data */
                        sizeof (EphyTab),
                        0, /* n_preallocs */
                        (GInstanceInitFunc) ephy_tab_init
                };
        const GInterfaceInfo link_info = 
        {
            NULL,
            NULL,
            NULL
        };


                type = g_type_register_static (GTK_TYPE_BIN,
                           "EphyTab",
                           &our_info, 0);

        g_type_add_interface_static (type,
                         EPHY_TYPE_LINK,
                         &link_info);
        }

        return type;
}

static void
ephy_tab_size_request (GtkWidget *widget,
               GtkRequisition *requisition)
{
    GtkWidget *child;

    GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);

    child = GTK_BIN (widget)->child;
    
    if (child && GTK_WIDGET_VISIBLE (child))
    {
        GtkRequisition child_requisition;
        gtk_widget_size_request (GTK_WIDGET (child), &child_requisition);
    }
}

static void
ephy_tab_size_allocate (GtkWidget *widget,
            GtkAllocation *allocation)
{
    GtkWidget *child;
    GtkAllocation invalid = { -1, -1, 1, 1 };

    widget->allocation = *allocation;

    child = GTK_BIN (widget)->child;
    g_return_if_fail (child != NULL);

    /* only resize if we're mapped (bug #128191),
     * or if this is the initial size-allocate (bug #156854).
     */
    if (GTK_WIDGET_MAPPED (child) ||
        memcmp (&child->allocation, &invalid, sizeof (GtkAllocation)) == 0)
    {
        gtk_widget_size_allocate (child, allocation);
    }
}

static void
ephy_tab_map (GtkWidget *widget)
{
    GtkWidget *child;

    g_return_if_fail (GTK_WIDGET_REALIZED (widget));

    child = GTK_BIN (widget)->child;
    g_return_if_fail (child != NULL);

    /* we do this since the window might have been resized while this
     * tab wasn't mapped (i.e. was a non-active tab during the resize).
     * See bug #156854.
     */
    if (memcmp (&widget->allocation, &child->allocation,
        sizeof (GtkAllocation)) != 0)
    {
        gtk_widget_size_allocate (child, &widget->allocation);
    }

    GTK_WIDGET_CLASS (parent_class)->map (widget);  
}

static void
ephy_tab_grab_focus (GtkWidget *widget)
{
    EphyTab *tab = EPHY_TAB (widget);

    gtk_widget_grab_focus (GTK_WIDGET (ephy_tab_get_embed (tab)));
}

static void
ephy_tab_class_init (EphyTabClass *class)
{
        GObjectClass *object_class = G_OBJECT_CLASS (class);
    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(class);

        parent_class = g_type_class_peek_parent (class);

    object_class->dispose = ephy_tab_dispose;
    object_class->finalize = ephy_tab_finalize;

    widget_class->size_allocate = ephy_tab_size_allocate;
    widget_class->size_request = ephy_tab_size_request;
    widget_class->map = ephy_tab_map;
    widget_class->grab_focus = ephy_tab_grab_focus;

    g_type_class_add_private (object_class, sizeof (EphyTabPrivate));
}

static void
ephy_tab_dispose (GObject *object)
{
    EphyTab *tab = EPHY_TAB (object);
    EphyTabPrivate *priv = tab->priv;

    if (priv->idle_resize_handler != 0)
    {
        g_source_remove (priv->idle_resize_handler);
        priv->idle_resize_handler = 0;
    }

    G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
ephy_tab_finalize (GObject *object)
{
    EphyTab *tab = EPHY_TAB (object);

    G_OBJECT_CLASS (parent_class)->finalize (object);

#ifdef ENABLE_PYTHON
    ephy_python_schedule_gc ();
#endif

    LOG ("EphyTab finalized %p", tab);
}

static gboolean
address_has_web_scheme (const char *address)
{
    gboolean has_web_scheme;

    if (address == NULL) return FALSE;

    has_web_scheme = (g_str_has_prefix (address, "http:") ||
              g_str_has_prefix (address, "https:") ||
              g_str_has_prefix (address, "ftp:") ||
              g_str_has_prefix (address, "file:") ||
              g_str_has_prefix (address, "data:") ||
              g_str_has_prefix (address, "about:") ||
              g_str_has_prefix (address, "gopher:"));

    return has_web_scheme;
}

static gboolean
let_me_resize_hack (EphyTab *tab)
{
    EphyTabPrivate *priv = tab->priv;

    gtk_widget_set_size_request (GTK_WIDGET (tab), -1, -1);

    priv->idle_resize_handler = 0;  
    return FALSE;
}

/* Public functions */

/**
 * ephy_tab_new:
 *
 * Equivalent to g_object_new(), but returns an #EphyTab so you don't have to
 * cast it.
 *
 * Returns: a new #EphyTab
 **/
EphyTab *
ephy_tab_new (void)
{
    return EPHY_TAB (g_object_new (EPHY_TYPE_TAB, NULL));
}

/**
 * ephy_tab_get_embed:
 * @tab: an #EphyTab
 *
 * Returns @tab's #EphyEmbed.
 *
 * Return value: @tab's #EphyEmbed
 **/
EphyEmbed *
ephy_tab_get_embed (EphyTab *tab)
{
    g_return_val_if_fail (EPHY_IS_TAB (tab), NULL);

    return EPHY_EMBED (gtk_bin_get_child (GTK_BIN (tab)));
}

/**
 * ephy_tab_for_embed
 * @embed: an #EphyEmbed
 *
 * Returns the #EphyTab which holds @embed.
 *
 * Return value: the #EphyTab which holds @embed
 **/
EphyTab *
ephy_tab_for_embed (EphyEmbed *embed)
{
    GtkWidget *parent;

    g_return_val_if_fail (EPHY_IS_EMBED (embed), NULL);

    parent = GTK_WIDGET (embed)->parent;
    g_return_val_if_fail (parent != NULL, NULL);

    return EPHY_TAB (parent);
}

/**
 * ephy_tab_get_size:
 * @tab: an #EphyTab
 * @width: return location for width, or %NULL
 * @height: return location for height, or %NULL
 *
 * Obtains the size of @tab. This is not guaranteed to be the actual number of
 * pixels occupied by the #EphyTab.
 **/
void
ephy_tab_get_size (EphyTab *tab, int *width, int *height)
{
    g_return_if_fail (EPHY_IS_TAB (tab));

    if (width != NULL)
    {
        *width = tab->priv->width;
    }
    if (height != NULL)
    {
        *height = tab->priv->height;
    }
}

/**
 * ephy_tab_set_size:
 * @tab: an #EphyTab
 * @width:
 * @height:
 *
 * Sets the size of @tab. This is not guaranteed to actually resize the tab.
 **/
void
ephy_tab_set_size (EphyTab *tab,
           int width,
           int height)
{
    EphyTabPrivate *priv = tab->priv;
    GtkWidget *widget = GTK_WIDGET (tab);
    GtkAllocation allocation;

    priv->width = width;
    priv->height = height;

    gtk_widget_set_size_request (widget, width, height);

    /* HACK: When the web site changes both width and height,
     * we will first get a width change, then a height change,
     * without actually resizing the window in between (since
     * that happens only on idle).
     * If we don't set the allocation, GtkMozEmbed doesn't tell
     * mozilla the new width, so the height change sets the width
     * back to the old value!
     */
    allocation.x = widget->allocation.x;
    allocation.y = widget->allocation.y;
    allocation.width = width;
    allocation.height = height;
    gtk_widget_size_allocate (widget, &allocation);

    /* HACK: reset widget requisition after the container
     * has been resized. It appears to be the only way
     * to have the window sized according to embed
     * size correctly.
     */
    if (priv->idle_resize_handler == 0)
    {
        priv->idle_resize_handler =
            g_idle_add ((GSourceFunc) let_me_resize_hack, tab);
    }
}

/* Private callbacks for embed signals */

static void
ephy_tab_init (EphyTab *tab)
{
    EphyTabPrivate *priv;
    GObject *embed;

    LOG ("EphyTab initialising %p", tab);

    priv = tab->priv = EPHY_TAB_GET_PRIVATE (tab);

    tab->priv->width = -1;
    tab->priv->height = -1;

    embed = ephy_embed_factory_new_object (EPHY_TYPE_EMBED);
    g_assert (embed != NULL);

    gtk_container_add (GTK_CONTAINER (tab), GTK_WIDGET (embed));
    gtk_widget_show (GTK_WIDGET (embed));
}