Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

a minimal workable embedding example (gtk)

49 views
Skip to first unread message

mr.ch...@gmail.com

unread,
Mar 12, 2008, 4:11:50 AM3/12/08
to
//thanks a lot to Torisugari, Benjamin Smedberg, Emil Wikström & all
mozilla dev docs, thank you all

#define XPCOM_GLUE
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <gtk/gtk.h>
#include "xpcom-config.h"
#include "nsXPCOMGlue.h"
#include "nsDebug.h"
#include "nsCOMPtr.h"
#include "widget/nsIBaseWindow.h"
#include "nsILocalFile.h"
#include "nsIWebBrowser.h"
#include "docshell/nsIWebNavigation.h"
#include "nsEmbedCID.h"
#include "nsEmbedString.h"
#include "xulapp/nsXULAppAPI.h"
#include "nsComponentManagerUtils.h"

XRE_InitEmbeddingType XRE_InitEmbedding;
XRE_TermEmbeddingType XRE_TermEmbedding;

static gboolean delete_event( GtkWidget *widget, GdkEvent *event,
gpointer data ) {
gtk_main_quit();
return FALSE;
}

int main(int argc, char** argv) {
GtkWidget *window;
nsCOMPtr<nsIBaseWindow> baseWindow;
nsCOMPtr<nsIWebBrowser> webBrowser;
nsCOMPtr<nsILocalFile> libxul;
nsCOMPtr<nsIWebNavigation> webNavigation;
nsDynamicFunctionLoad nsFuncs[] = {
{"XRE_InitEmbedding", (NSFuncPtr*)&XRE_InitEmbedding},
{"XRE_TermEmbedding", (NSFuncPtr*)
&XRE_TermEmbedding},
{0, 0}
};
nsresult rv;
gtk_init (&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_show(window);
rv = XPCOMGlueStartup("/opt/xulrunner/libxpcom.so");
if (NS_FAILED(rv)) {
printf("XPCOMGlueStartup\n");
}
rv = XPCOMGlueLoadXULFunctions(nsFuncs);
if (NS_FAILED(rv)) {
printf("XPCOMGlueLoadXULFunctions\n");
}
rv = NS_NewNativeLocalFile(nsEmbedCString("/opt/xulrunner"),
PR_FALSE, getter_AddRefs(libxul));
if (NS_FAILED(rv)) {
printf("NS_NewNativeLocalFile\n");
}
rv = XRE_InitEmbedding(libxul, 0, 0, 0, 0);
if (NS_FAILED(rv)) {
printf("XRE_InitEmbedding\n");
}
webBrowser = do_CreateInstance(NS_WEBBROWSER_CONTRACTID, &rv);
if (NS_FAILED(rv)) {
printf("do_CreateInstance webBrowser\n");
}
baseWindow = do_QueryInterface(webBrowser);
rv = baseWindow->InitWindow(window, 0, 0, 0, 300, 400);
if (NS_FAILED(rv)) {
printf("InitWindow\n");
}
rv = baseWindow->Create();
if (NS_FAILED(rv)) {
printf("Create\n");
}
rv =baseWindow->SetVisibility(PR_TRUE);
if (NS_FAILED(rv)) {
printf("SetVisibility\n");
}
webNavigation = do_QueryInterface(webBrowser);
rv = webNavigation->LoadURI(NS_ConvertASCIItoUTF16("http://
www.google.com").get(),
nsIWebNavigation::LOAD_FLAGS_NONE, 0, 0, 0);
if (NS_FAILED(rv)) {
printf("LoadURI\n");
}
g_signal_connect(G_OBJECT(window), "delete_event",
G_CALLBACK(delete_event), 0);
gtk_main();
printf("exit...\n");
XRE_TermEmbedding();
XPCOMGlueShutdown();
return 0;
}

yong.z...@gmail.com

unread,
Apr 11, 2008, 5:22:05 AM4/11/08
to
>         rv = webNavigation->LoadURI(NS_ConvertASCIItoUTF16("http://www.google.com").get(),

> nsIWebNavigation::LOAD_FLAGS_NONE, 0, 0, 0);
>         if (NS_FAILED(rv)) {
>                 printf("LoadURI\n");
>         }
>         g_signal_connect(G_OBJECT(window), "delete_event",
> G_CALLBACK(delete_event), 0);
>         gtk_main();
>         printf("exit...\n");
>         XRE_TermEmbedding();
>         XPCOMGlueShutdown();
>         return 0;
>
>
>
> }- 隐藏被引用文字 -
>
> - 显示引用的文字 -

hello
i try your code in fedora8, but it compiled wrong

vin...@gmail.com

unread,
Apr 16, 2008, 8:22:32 AM4/16/08
to
>         rv = webNavigation->LoadURI(NS_ConvertASCIItoUTF16("http://www.google.com").get(),

> nsIWebNavigation::LOAD_FLAGS_NONE, 0, 0, 0);
>         if (NS_FAILED(rv)) {
>                 printf("LoadURI\n");
>         }
>         g_signal_connect(G_OBJECT(window), "delete_event",
> G_CALLBACK(delete_event), 0);
>         gtk_main();
>         printf("exit...\n");
>         XRE_TermEmbedding();
>         XPCOMGlueShutdown();
>         return 0;
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -

Hi!
I could not compile the above piece of code.
maybe I didn't use the correct options. Can you please let me know how
you compiled the code?

Regards,
Vinod

mr.ch...@gmail.com

unread,
Apr 23, 2008, 1:48:54 AM4/23/08
to

g++ -Wall -Wno-non-virtual-dtor -I/home/user/xulrunner-sdk/include -I/
home/user/xulrunner-sdk/sdk/include `pkg-config --cflags gtk+-2.0` -c
-o embed_xul.o embed_xul.cpp

cc embed_xul.o -L/home/user/xulrunner-sdk/lib -lxpcomglue -lstdc++
`pkg-config --libs gtk+-2.0` -o embed_xul

Danick

unread,
May 7, 2008, 1:01:12 PM5/7/08
to
>         rv = webNavigation->LoadURI(NS_ConvertASCIItoUTF16("http://www.google.com").get(),

> nsIWebNavigation::LOAD_FLAGS_NONE, 0, 0, 0);
>         if (NS_FAILED(rv)) {
>                 printf("LoadURI\n");
>         }
>         g_signal_connect(G_OBJECT(window), "delete_event",
> G_CALLBACK(delete_event), 0);
>         gtk_main();
>         printf("exit...\n");
>         XRE_TermEmbedding();
>         XPCOMGlueShutdown();
>         return 0;
>
> }

I have been trying to compile and run the example but I am running
into some difficulties.

The following lines:

XRE_InitEmbeddingType XRE_InitEmbedding;
XRE_TermEmbeddingType XRE_TermEmbedding;

Give me the following errors when compiling:

embed_xul.cpp:27: error: expected initializer before
‘XRE_InitEmbeddingType’
embed_xul.cpp:28: error: ‘XRE_TermEmbeddingType’ does not name a type

If I remove these definitions I am able to compile the application but
I run into some linking issues as follows:

embed_xul.cpp:(.text+0x3e): undefined reference to `XRE_InitEmbedding'
embed_xul.cpp:(.text+0x4d): undefined reference to `XRE_TermEmbedding'
embed_xul.cpp:(.text+0x1ac): undefined reference to
`XRE_InitEmbedding'
embed_xul.cpp:(.text+0x40a): undefined reference to
`XRE_TermEmbedding'

I have tried liking with libembed_base_s.a found under the /embedding/
base directory of the objects folder after building firefox. This
fixes the linking issues but now causes a segmentation fault when
XPCOMGlueLoadXULFunctions is called.

Any help would be greatly appreciated.

Thanks in advance

Danick

unread,
May 7, 2008, 1:14:20 PM5/7/08
to

Sorry I needed to add -lxul not libembed_base_s.a to remove the
linking problem.

Vlad

unread,
Jul 15, 2008, 5:21:53 AM7/15/08
to mr.ch...@gmail.com, dev-em...@lists.mozilla.org
On Mar 12, 11:11 am, mr.chob...@gmail.com wrote:
> //thanks a lot to Torisugari, Benjamin Smedberg, Emil Wikström & all
> mozilla dev docs, thank you all
>

Hello, I managed to perfectly compile your example with

CFLAGS=`pkg-config --cflags gtk+-2.0 gthread-2.0 libxul-embedding-
unstable` -g -Wall
LD_ADD=`pkg-config --libs gtk+-2.0 gthread-2.0 libxul-embedding-
unstable`

minim: minim.cc
g++ -Wno-non-virtual-dtor $(CFLAGS) minim.cc -o minim $(LD_ADD)

on Ubuntu 8.04, with xulrunner-1.9-dev installed, and after changing
the " to < in the include directives

However the app crashes on any browser link clicked.
I do not have the debugging symbols for xulrunner, for a meaningful
backtrace, but are you having the same problem?
If so, what would be the cause? (I saw you're building mozilla from
the source.)

Thanks,

Vlad

0 new messages