2000-08-28 22:22:52 +00:00
|
|
|
|
2000-07-20 01:12:25 +00:00
|
|
|
//
|
|
|
|
// What follows is a hacked version the the sample hello
|
|
|
|
// world gtk app found: http://www.gtk.org/tutorial/gtk_tut-2.html
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2001-02-26 00:42:17 +00:00
|
|
|
#include "gtkEmbed.h"
|
|
|
|
|
2000-08-28 22:22:52 +00:00
|
|
|
#include "nsCOMPtr.h"
|
2000-07-20 01:12:25 +00:00
|
|
|
#include "nsEmbedAPI.h"
|
2000-08-28 22:22:52 +00:00
|
|
|
#include "WebBrowserChrome.h"
|
2001-02-26 00:42:17 +00:00
|
|
|
#include "WindowCreator.h"
|
2000-07-20 01:12:25 +00:00
|
|
|
|
|
|
|
#include "nsIEventQueueService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
2001-02-26 00:42:17 +00:00
|
|
|
#include "nsIWindowWatcher.h"
|
2000-07-20 01:12:25 +00:00
|
|
|
|
2000-12-18 21:40:32 +00:00
|
|
|
#ifdef NS_TRACE_MALLOC
|
|
|
|
#include "nsTraceMalloc.h"
|
|
|
|
#endif
|
|
|
|
|
2000-07-20 01:12:25 +00:00
|
|
|
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
|
|
|
|
|
|
|
static gint io_identifier = 0;
|
2001-02-26 00:42:17 +00:00
|
|
|
static char *sWatcherContractID = "@mozilla.org/embedcomp/window-watcher;1";
|
2000-07-20 01:12:25 +00:00
|
|
|
|
2000-09-01 19:36:43 +00:00
|
|
|
typedef struct
|
2000-08-28 22:22:52 +00:00
|
|
|
{
|
2000-09-01 19:36:43 +00:00
|
|
|
GtkWidget *window;
|
|
|
|
GtkWidget *urlEntry;
|
|
|
|
GtkWidget *box;
|
|
|
|
GtkWidget *view;
|
2000-09-27 22:15:08 +00:00
|
|
|
GtkWidget *memUsageDumpBtn;
|
2000-09-01 19:36:43 +00:00
|
|
|
nsIWebBrowserChrome *chrome;
|
|
|
|
} MyBrowser;
|
|
|
|
|
|
|
|
|
|
|
|
void destroy( GtkWidget *widget, MyBrowser* data )
|
|
|
|
{
|
2001-02-26 00:42:17 +00:00
|
|
|
PRBool dontQuit;
|
|
|
|
|
2000-09-01 19:36:43 +00:00
|
|
|
printf("Destroying created widgets\n");
|
|
|
|
gtk_widget_destroy( data->window );
|
|
|
|
gtk_widget_destroy( data->urlEntry );
|
|
|
|
gtk_widget_destroy( data->box );
|
2000-09-27 22:15:08 +00:00
|
|
|
gtk_widget_destroy( data->memUsageDumpBtn );
|
2000-09-01 19:36:43 +00:00
|
|
|
// gtk_widget_destroy( data->view );
|
|
|
|
free (data);
|
2001-02-26 00:42:17 +00:00
|
|
|
|
|
|
|
dontQuit = PR_FALSE;
|
|
|
|
nsCOMPtr<nsIWindowWatcher> wwatch(do_GetService(sWatcherContractID));
|
|
|
|
if (wwatch) {
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> list;
|
|
|
|
wwatch->GetWindowEnumerator(getter_AddRefs(list));
|
|
|
|
if (list)
|
|
|
|
list->HasMoreElements(&dontQuit);
|
|
|
|
}
|
|
|
|
if (!dontQuit)
|
|
|
|
gtk_main_quit();
|
2000-08-28 22:22:52 +00:00
|
|
|
}
|
|
|
|
|
2000-09-01 19:36:43 +00:00
|
|
|
gint delete_event( GtkWidget *widget, GdkEvent *event, MyBrowser* data )
|
|
|
|
{
|
|
|
|
printf("Releasing chrome...");
|
|
|
|
NS_RELEASE(data->chrome);
|
|
|
|
printf("done.\n");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-27 22:15:08 +00:00
|
|
|
void
|
|
|
|
dump_mem_clicked_cb(GtkButton *button, nsIWebBrowserChrome *browser)
|
|
|
|
{
|
|
|
|
g_print("dumping memory usage:\n");
|
|
|
|
system("ps -eo \"%c %z\" | grep gtkEmbed");
|
2000-12-18 21:40:32 +00:00
|
|
|
|
|
|
|
#ifdef NS_TRACE_MALLOC
|
|
|
|
// dump detailed information, too
|
|
|
|
NS_TraceMallocDumpAllocations("allocations.log");
|
|
|
|
#endif
|
2000-09-27 22:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-30 03:51:50 +00:00
|
|
|
void
|
|
|
|
url_activate_cb( GtkEditable *widget, nsIWebBrowserChrome *browser)
|
|
|
|
{
|
|
|
|
gchar *text = gtk_editable_get_chars(widget, 0, -1);
|
|
|
|
g_print("loading url %s\n", text);
|
|
|
|
if (browser)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIWebBrowser> webBrowser;
|
|
|
|
browser->GetWebBrowser(getter_AddRefs(webBrowser));
|
|
|
|
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(webBrowser));
|
|
|
|
if (webNav)
|
2000-09-20 12:23:53 +00:00
|
|
|
webNav->LoadURI(NS_ConvertASCIItoUCS2(text).GetUnicode(), nsIWebNavigation::LOAD_FLAGS_NONE);
|
2000-08-30 03:51:50 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
g_free(text);
|
|
|
|
}
|
|
|
|
|
2000-07-20 01:12:25 +00:00
|
|
|
static void
|
|
|
|
handle_event_queue(gpointer data, gint source, GdkInputCondition condition)
|
|
|
|
{
|
|
|
|
nsIEventQueue *eventQueue = (nsIEventQueue *)data;
|
|
|
|
eventQueue->ProcessPendingEvents();
|
|
|
|
}
|
|
|
|
|
2000-08-28 22:22:52 +00:00
|
|
|
|
2001-02-26 00:42:17 +00:00
|
|
|
/* InitializeWindowCreator creates and hands off an object with a callback
|
|
|
|
to a window creation function. This will be used by Gecko C++ code
|
|
|
|
(never JS) to create new windows when no previous window is handy
|
|
|
|
to begin with. This is done in a few exceptional cases, like PSM code.
|
|
|
|
Failure to set this callback will only disable the ability to create
|
|
|
|
new windows under these circumstances. */
|
|
|
|
nsresult InitializeWindowCreator()
|
|
|
|
{
|
|
|
|
// create an nsWindowCreator and give it to the WindowWatcher service
|
|
|
|
WindowCreator *creatorCallback = new WindowCreator();
|
|
|
|
if (creatorCallback) {
|
|
|
|
nsCOMPtr<nsIWindowCreator> windowCreator(dont_QueryInterface(NS_STATIC_CAST(nsIWindowCreator *, creatorCallback)));
|
|
|
|
if (windowCreator) {
|
|
|
|
nsCOMPtr<nsIWindowWatcher> wwatch(do_GetService(sWatcherContractID));
|
|
|
|
if (wwatch) {
|
|
|
|
wwatch->SetWindowCreator(windowCreator);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CreateBrowserWindow(PRUint32 aChromeFlags,
|
|
|
|
nsIWebBrowserChrome *aParent,
|
|
|
|
nsIWebBrowserChrome **aNewWindow)
|
2000-08-28 22:22:52 +00:00
|
|
|
{
|
|
|
|
WebBrowserChrome * chrome = new WebBrowserChrome();
|
|
|
|
if (!chrome)
|
|
|
|
return NS_ERROR_FAILURE;
|
2000-07-20 01:12:25 +00:00
|
|
|
|
2001-02-26 00:42:17 +00:00
|
|
|
CallQueryInterface(NS_STATIC_CAST(nsIWebBrowserChrome *, chrome), aNewWindow);
|
|
|
|
|
|
|
|
// chrome->SetChromeFlags(aChromeFlags);
|
|
|
|
|
2000-08-28 22:22:52 +00:00
|
|
|
nsCOMPtr<nsIWebBrowser> newBrowser;
|
2001-02-26 00:42:17 +00:00
|
|
|
chrome->CreateBrowser(-1, -1, -1, -1, getter_AddRefs(newBrowser));
|
2000-08-28 22:22:52 +00:00
|
|
|
if (!newBrowser)
|
|
|
|
return NS_ERROR_FAILURE;
|
2000-07-20 01:12:25 +00:00
|
|
|
|
2000-08-28 22:22:52 +00:00
|
|
|
nsCOMPtr<nsIBaseWindow> baseWindow = do_QueryInterface(newBrowser);
|
|
|
|
|
|
|
|
baseWindow->SetPositionAndSize(0,
|
|
|
|
0,
|
2000-08-30 03:51:50 +00:00
|
|
|
430,
|
|
|
|
430,
|
2000-08-28 22:22:52 +00:00
|
|
|
PR_TRUE);
|
2000-07-20 01:12:25 +00:00
|
|
|
|
2000-08-28 22:22:52 +00:00
|
|
|
baseWindow->SetVisibility(PR_TRUE);
|
2000-07-20 01:12:25 +00:00
|
|
|
|
2000-08-28 22:22:52 +00:00
|
|
|
GtkWidget *widget;
|
|
|
|
baseWindow->GetParentNativeWindow((void**)&widget);
|
2000-07-20 01:12:25 +00:00
|
|
|
|
2000-08-28 22:22:52 +00:00
|
|
|
gtk_widget_show (widget); // should this function do this?
|
2001-02-26 00:42:17 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-07-20 01:12:25 +00:00
|
|
|
|
2000-08-28 22:22:52 +00:00
|
|
|
|
2001-02-26 00:42:17 +00:00
|
|
|
nsresult OpenWebPage(char* url)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIWebBrowserChrome> chrome;
|
|
|
|
|
|
|
|
rv = CreateBrowserWindow(nsIWebBrowserChrome::CHROME_ALL, nsnull,
|
|
|
|
getter_AddRefs(chrome));
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// Start loading a page
|
|
|
|
nsCOMPtr<nsIWebBrowser> newBrowser;
|
|
|
|
chrome->GetWebBrowser(getter_AddRefs(newBrowser));
|
|
|
|
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(newBrowser));
|
|
|
|
return webNav->LoadURI(NS_ConvertASCIItoUCS2(url).GetUnicode(), nsIWebNavigation::LOAD_FLAGS_NONE);
|
|
|
|
}
|
|
|
|
return rv;
|
2000-08-28 22:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nativeWindow CreateNativeWindow(nsIWebBrowserChrome* chrome)
|
|
|
|
{
|
2000-09-01 19:36:43 +00:00
|
|
|
MyBrowser* browser = (MyBrowser*) malloc (sizeof(MyBrowser) );
|
|
|
|
if (!browser)
|
2000-08-30 03:51:50 +00:00
|
|
|
return nsnull;
|
2000-07-20 01:12:25 +00:00
|
|
|
|
2000-09-01 19:36:43 +00:00
|
|
|
browser->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
|
|
|
browser->urlEntry = gtk_entry_new();
|
|
|
|
browser->box = gtk_vbox_new(FALSE, 0);
|
|
|
|
browser->view = gtk_event_box_new();
|
2000-09-27 22:15:08 +00:00
|
|
|
browser->memUsageDumpBtn = gtk_button_new_with_label("Dump Memory Usage");
|
2000-09-01 19:36:43 +00:00
|
|
|
browser->chrome = chrome; // take ownership!
|
2001-02-26 00:42:17 +00:00
|
|
|
NS_ADDREF(chrome); // no, really take it! (released in delete_event)
|
2000-09-01 19:36:43 +00:00
|
|
|
|
2000-09-27 22:15:08 +00:00
|
|
|
if (!browser->window || !browser->urlEntry || !browser->box ||
|
|
|
|
!browser->view || !browser->memUsageDumpBtn)
|
2000-09-01 19:36:43 +00:00
|
|
|
return nsnull;
|
|
|
|
|
2000-08-30 03:51:50 +00:00
|
|
|
/* Put the box into the main window. */
|
2000-09-01 19:36:43 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (browser->window),browser->box);
|
2000-07-20 01:12:25 +00:00
|
|
|
|
2000-09-01 19:36:43 +00:00
|
|
|
gtk_box_pack_start(GTK_BOX(browser->box), browser->urlEntry, FALSE, FALSE, 5);
|
|
|
|
gtk_box_pack_start(GTK_BOX(browser->box), browser->view, FALSE, FALSE, 5);
|
2000-09-27 22:15:08 +00:00
|
|
|
gtk_box_pack_start(GTK_BOX(browser->box), browser->memUsageDumpBtn, FALSE, FALSE, 5);
|
2000-07-20 01:12:25 +00:00
|
|
|
|
2000-09-01 19:36:43 +00:00
|
|
|
gtk_signal_connect (GTK_OBJECT (browser->window),
|
|
|
|
"destroy",
|
|
|
|
GTK_SIGNAL_FUNC (destroy),
|
|
|
|
browser);
|
|
|
|
|
|
|
|
gtk_signal_connect (GTK_OBJECT (browser->window),
|
2000-08-30 03:51:50 +00:00
|
|
|
"delete_event",
|
2000-09-01 19:36:43 +00:00
|
|
|
GTK_SIGNAL_FUNC (delete_event),
|
|
|
|
browser);
|
2000-07-20 01:12:25 +00:00
|
|
|
|
2000-09-01 19:36:43 +00:00
|
|
|
gtk_signal_connect(GTK_OBJECT(browser->urlEntry),
|
|
|
|
"activate",
|
|
|
|
GTK_SIGNAL_FUNC(url_activate_cb),
|
|
|
|
chrome);
|
2000-08-28 22:22:52 +00:00
|
|
|
|
2000-09-27 22:15:08 +00:00
|
|
|
gtk_signal_connect(GTK_OBJECT(browser->memUsageDumpBtn),
|
|
|
|
"clicked",
|
|
|
|
GTK_SIGNAL_FUNC(dump_mem_clicked_cb),
|
|
|
|
chrome);
|
|
|
|
|
2000-09-01 19:36:43 +00:00
|
|
|
gtk_widget_set_usize(browser->view, 430, 430);
|
2000-08-28 22:22:52 +00:00
|
|
|
|
2000-09-01 19:36:43 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (browser->window), "Embedding is Fun!");
|
|
|
|
gtk_window_set_default_size (GTK_WINDOW(browser->window), 450, 450);
|
2000-08-28 22:22:52 +00:00
|
|
|
|
2000-09-01 19:36:43 +00:00
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (browser->window), 10);
|
2000-08-28 22:22:52 +00:00
|
|
|
|
2000-09-01 19:36:43 +00:00
|
|
|
gtk_widget_realize (browser->window);
|
2000-08-30 03:51:50 +00:00
|
|
|
|
2000-09-27 22:15:08 +00:00
|
|
|
gtk_widget_show (browser->memUsageDumpBtn);
|
2000-09-01 19:36:43 +00:00
|
|
|
gtk_widget_show (browser->urlEntry);
|
|
|
|
gtk_widget_show (browser->box);
|
|
|
|
gtk_widget_show (browser->view);
|
|
|
|
gtk_widget_show (browser->window);
|
2001-02-26 00:42:17 +00:00
|
|
|
|
2000-09-01 19:36:43 +00:00
|
|
|
return browser->view;
|
2000-08-28 22:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-09-01 19:36:43 +00:00
|
|
|
int main( int argc, char *argv[] )
|
2000-08-28 22:22:52 +00:00
|
|
|
{
|
2000-12-18 21:40:32 +00:00
|
|
|
#ifdef NS_TRACE_MALLOC
|
|
|
|
argc = NS_TraceMallocStartupArgs(argc, argv);
|
|
|
|
#endif
|
|
|
|
|
2000-09-01 19:36:43 +00:00
|
|
|
char *loadURLStr;
|
|
|
|
if (argc > 1)
|
|
|
|
loadURLStr = argv[1];
|
|
|
|
else
|
2000-10-24 21:57:35 +00:00
|
|
|
loadURLStr = "http://www.mozilla.org/projects/embedding";
|
2000-09-01 19:36:43 +00:00
|
|
|
|
2000-08-28 22:22:52 +00:00
|
|
|
NS_InitEmbedding(nsnull, nsnull);
|
|
|
|
|
2001-02-26 00:42:17 +00:00
|
|
|
InitializeWindowCreator();
|
|
|
|
|
2000-08-28 22:22:52 +00:00
|
|
|
// set up the thread event queue
|
2000-09-01 19:36:43 +00:00
|
|
|
nsCOMPtr<nsIEventQueueService> eventQService = do_GetService(kEventQueueServiceCID);
|
|
|
|
if (!eventQService) return (-1);
|
2000-08-28 22:22:52 +00:00
|
|
|
|
2000-09-01 19:36:43 +00:00
|
|
|
nsCOMPtr< nsIEventQueue> eventQueue;
|
|
|
|
eventQService->GetThreadEventQueue( NS_CURRENT_THREAD, getter_AddRefs(eventQueue));
|
|
|
|
if (!eventQueue) return (-1);
|
|
|
|
|
|
|
|
io_identifier = gdk_input_add( eventQueue->GetEventQueueSelectFD(),
|
|
|
|
GDK_INPUT_READ,
|
|
|
|
handle_event_queue,
|
|
|
|
eventQueue);
|
|
|
|
|
2000-08-28 22:22:52 +00:00
|
|
|
/* This is called in all GTK applications. Arguments are parsed
|
|
|
|
* from the command line and are returned to the application. */
|
|
|
|
gtk_init(&argc, &argv);
|
|
|
|
|
2000-09-01 19:36:43 +00:00
|
|
|
|
|
|
|
if ( NS_FAILED( OpenWebPage(loadURLStr) ))
|
2000-08-28 22:22:52 +00:00
|
|
|
return (-1);
|
|
|
|
|
2000-07-20 01:12:25 +00:00
|
|
|
|
2000-08-28 22:22:52 +00:00
|
|
|
/* All GTK applications must have a gtk_main(). Control ends here
|
|
|
|
* and waits for an event to occur (like a key press or
|
|
|
|
* mouse event). */
|
|
|
|
gtk_main ();
|
2000-09-01 19:36:43 +00:00
|
|
|
|
|
|
|
gtk_input_remove(io_identifier);
|
|
|
|
|
|
|
|
printf("Cleaning up embedding\n");
|
|
|
|
NS_TermEmbedding();
|
2000-12-18 21:40:32 +00:00
|
|
|
|
|
|
|
#ifdef NS_TRACE_MALLOC
|
|
|
|
NS_TraceMallocShutdown();
|
|
|
|
#endif
|
|
|
|
|
2000-08-28 22:22:52 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|