Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/hulahop-browser.cpp
blob: 40d656255d771a46426ee6cfe5cfb31212a13c33 (plain)
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
/*
 * Copyright (C) 2007, Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <nsIDocShellTreeItem.h>
#include <nsComponentManagerUtils.h>
#include <nsCOMPtr.h>
#include <nsIWebBrowser.h>
#include <nsILocalFile.h>
#include <nsIBaseWindow.h>
#include <nsXULAppAPI.h>
#include <PyXPCOM.h>

#include "hulahop-browser.h"
#include "HulahopDirectoryProvider.h"

struct _HulahopBrowser {
	GtkBin base_instance;
	nsCOMPtr<nsIWebBrowser>  browser;
	nsCOMPtr<nsIBaseWindow>  base_window;
};

struct _HulahopBrowserClass {
	GtkBinClass base_class;
};

G_DEFINE_TYPE(HulahopBrowser, hulahop_browser, GTK_TYPE_BIN)

static GObjectClass *parent_class = NULL;

static const HulahopDirectoryProvider kDirectoryProvider;

gboolean
hulahop_startup()
{
    nsresult rv;

    nsCOMPtr<nsILocalFile> greDir;
    rv = NS_NewNativeLocalFile(nsCString(MOZILLA_HOME), PR_TRUE,
                               getter_AddRefs(greDir));
    NS_ENSURE_SUCCESS(rv, FALSE);

    nsCOMPtr<nsILocalFile> binDir;
    rv = NS_NewNativeLocalFile(nsCString(MOZILLA_HOME"/components"), PR_TRUE,
                               getter_AddRefs(binDir));
    NS_ENSURE_SUCCESS(rv, FALSE);

    rv = XRE_InitEmbedding(greDir, binDir,
                           NS_CONST_CAST(HulahopDirectoryProvider *,
                                    &kDirectoryProvider), nsnull, 0);
    NS_ENSURE_SUCCESS(rv, FALSE);
    
    return TRUE;
}

static void
hulahop_browser_realize(GtkWidget *widget)
{
    HulahopBrowser *browser = HULAHOP_BROWSER(widget);
    GdkWindowAttr attributes;
    gint attributes_mask;

    GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED);

    attributes.window_type = GDK_WINDOW_CHILD;
    attributes.x = widget->allocation.x;
    attributes.y = widget->allocation.y;
    attributes.width = widget->allocation.width;
    attributes.height = widget->allocation.height;
    attributes.wclass = GDK_INPUT_OUTPUT;
    attributes.visual = gtk_widget_get_visual(widget);
    attributes.colormap = gtk_widget_get_colormap(widget);
    attributes.event_mask = gtk_widget_get_events(widget) | GDK_EXPOSURE_MASK;

    attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;

    widget->window = gdk_window_new(gtk_widget_get_parent_window(widget),
                                    &attributes, attributes_mask);
    gdk_window_set_user_data(widget->window, widget);

    widget->style = gtk_style_attach (widget->style, widget->window);
    gtk_style_set_background(widget->style, widget->window, GTK_STATE_NORMAL);

    nsresult rv;
    
    browser->browser = do_CreateInstance
            ("@mozilla.org/embedding/browser/nsWebBrowser;1");
    g_assert(browser->browser);

    nsCOMPtr<nsIDocShellTreeItem> item = do_QueryInterface(browser->browser);
    item->SetItemType(nsIDocShellTreeItem::typeContentWrapper);

    browser->base_window = do_QueryInterface(browser->browser);
        
    rv = browser->base_window->InitWindow(widget, nsnull, 0, 0,
                                          widget->allocation.width,
                                          widget->allocation.height);
    g_assert(NS_SUCCEEDED(rv));

    rv = browser->base_window->Create();
    g_assert(NS_SUCCEEDED(rv));
}

static void
hulahop_browser_map(GtkWidget *widget)
{
    HulahopBrowser *browser = HULAHOP_BROWSER(widget);

    GTK_WIDGET_SET_FLAGS(widget, GTK_MAPPED);

    nsCOMPtr<nsIBaseWindow> baseWindow = do_QueryInterface(browser->browser);

    // XXX hack around problem. probably widget/gtk2 window initialization.
    baseWindow->SetVisibility(PR_FALSE);
    baseWindow->SetVisibility(PR_TRUE);

    gdk_window_show(widget->window);
}

static void
hulahop_browser_class_init(HulahopBrowserClass *browser_class)
{
    GtkWidgetClass  *widget_class = GTK_WIDGET_CLASS(browser_class);

    parent_class = (GObjectClass *) g_type_class_peek_parent(browser_class);

    widget_class->realize = hulahop_browser_realize;
    widget_class->map = hulahop_browser_map;
}

static void
hulahop_browser_init(HulahopBrowser *browser)
{
    GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(browser), GTK_NO_WINDOW);
}

PyObject *
hulahop_browser_get_browser (HulahopBrowser *browser)
{
    return PyObject_FromNSInterface(browser->browser,
                                    NS_GET_IID(nsIWebBrowser), PR_FALSE);
}