Rubberstamped by Kevin Decker
[WebKit-https.git] / WebKitTools / GdkLauncher / main.cpp
1 #include "config.h"
2 #include "ChromeClientGdk.h"
3 #include "ContextMenuClientGdk.h"
4 #include "Document.h"
5 #include "DragClient.h"
6 #include "EditorClientGdk.h"
7 #include "FrameGdk.h"
8 #include "FrameLoader.h"
9 #include "FrameLoaderClientGdk.h"
10 #include "FrameView.h"
11 #include "KURL.h"
12 #include "Page.h"
13 #include "PlatformString.h"
14 #include "ResourceHandleManager.h"
15
16 #if SVG_SUPPORT
17 #include "SVGNames.h"
18 #include "XLinkNames.h"
19 #include "SVGDocumentExtensions.h"
20 #endif
21
22 #include <gdk/gdk.h>
23 #include <gtk/gtk.h>
24
25 using namespace WebCore;
26
27 static GtkWidget* gUrlBarEntry;
28 static FrameGdk*  gFrame;
29
30 static bool strEmpty(const char* str)
31 {
32     return !str || !*str;
33 }
34
35 static bool strEq(const char* str1, const char* str2)
36 {
37     return 0 == strcmp(str1, str2);
38 }
39
40 static void handleGdkEvent(GtkWidget* widget, GdkEvent* event)
41 {
42     gFrame->handleGdkEvent(event);
43 }
44
45 static void goToUrlBarText(GtkWidget* urlBarEntry)
46 {
47     const gchar* url = gtk_entry_get_text(GTK_ENTRY(urlBarEntry));
48     if (strEmpty(url))
49         return;
50     // FIXME: append "http://" if doesn't have a scheme
51     gFrame->loader()->load(url, 0);
52 }
53
54 static void goButtonClickedCb(GtkWidget* widget, GtkWidget* entry)
55 {
56     goToUrlBarText(entry);
57 }
58
59 static void urlBarEnterCb(GtkWidget* widget, GtkWidget* entry)
60 {
61     goToUrlBarText(entry);
62 }
63
64 static void registerRenderingAreaEvents(GtkWidget* win)
65 {
66     gtk_widget_set_events(win, GDK_EXPOSURE_MASK
67                          | GDK_BUTTON_PRESS_MASK
68                          | GDK_BUTTON_RELEASE_MASK
69                          | GDK_POINTER_MOTION_MASK
70                          | GDK_KEY_PRESS_MASK
71                          | GDK_KEY_RELEASE_MASK
72                          | GDK_BUTTON_MOTION_MASK
73                          | GDK_BUTTON1_MOTION_MASK
74                          | GDK_BUTTON2_MOTION_MASK
75                          | GDK_BUTTON3_MOTION_MASK);
76
77     g_signal_connect(GTK_OBJECT(win), "expose-event", G_CALLBACK(handleGdkEvent), NULL);
78     g_signal_connect(GTK_OBJECT(win), "key-press-event", G_CALLBACK(handleGdkEvent), NULL);
79     g_signal_connect(GTK_OBJECT(win), "key-release-event", G_CALLBACK(handleGdkEvent), NULL);
80     g_signal_connect(GTK_OBJECT(win), "button-press-event", G_CALLBACK(handleGdkEvent), NULL);
81     g_signal_connect(GTK_OBJECT(win), "button-release-event", G_CALLBACK(handleGdkEvent), NULL);
82     g_signal_connect(GTK_OBJECT(win), "motion-notify-event", G_CALLBACK(handleGdkEvent), NULL);
83     g_signal_connect(GTK_OBJECT(win), "scroll-event", G_CALLBACK(handleGdkEvent), NULL);
84 }
85
86 static void frameResizeCb(GtkWidget* widget, gpointer data)
87 {
88     // FIXME: resize the area?
89 }
90
91 static void frameDestroyCb(GtkWidget* widget, gpointer data)
92 {
93     gtk_main_quit();
94 }
95
96 int main(int argc, char* argv[]) 
97 {
98     gtk_init(&argc, &argv);
99
100     const char* url = "http://www.google.com";
101     bool exitAfterLoading = false;
102     bool dumpRenderTree = false;
103     for (int argPos = 1; argPos < argc; ++argPos) {
104         char *currArg = argv[argPos];
105         if (strEq(currArg, "-exit-after-loading"))
106             exitAfterLoading = true;
107         else if (strEq(currArg, "-exitafterloading"))
108             exitAfterLoading = true;
109         else if (strEq(currArg, "-exitafterload"))
110             exitAfterLoading = true;
111         else if (strEq(currArg, "-exit-after-load"))
112             exitAfterLoading = true;
113         else if (strEq(currArg, "-drt"))
114             dumpRenderTree = true;
115         else if (strEq(currArg, "-dump-render-tree"))
116             dumpRenderTree = true;
117         else if (strEq(currArg, "-dumprendertree"))
118             dumpRenderTree = true;
119         else
120             url = currArg;
121     }
122
123     GtkWidget* topLevelWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
124     gtk_window_set_default_size(GTK_WINDOW(topLevelWindow), 800, 600);
125     gtk_widget_set_name(topLevelWindow, "GdkLauncher");
126     GtkWidget* vbox = gtk_vbox_new(FALSE, 0);
127     gtk_container_add(GTK_CONTAINER(topLevelWindow), vbox);
128     g_signal_connect(G_OBJECT(topLevelWindow), "destroy", G_CALLBACK(frameDestroyCb), NULL);
129     g_signal_connect(GTK_OBJECT(topLevelWindow), "size-request", G_CALLBACK(frameResizeCb), NULL);
130
131     GtkWidget* hbox = gtk_hbox_new(FALSE, 2);
132     gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
133
134     gUrlBarEntry = gtk_entry_new();
135     g_signal_connect(G_OBJECT(gUrlBarEntry), "activate", G_CALLBACK(urlBarEnterCb), (gpointer)gUrlBarEntry);
136     gtk_box_pack_start(GTK_BOX(hbox), gUrlBarEntry, TRUE, TRUE, 0);
137
138     GtkWidget* urlBarSubmitButton = gtk_button_new_with_label("Go");  
139     gtk_box_pack_start(GTK_BOX(hbox), urlBarSubmitButton, FALSE, FALSE, 0);
140     g_signal_connect(G_OBJECT(urlBarSubmitButton), "clicked", G_CALLBACK(goButtonClickedCb), (gpointer)gUrlBarEntry);
141     gtk_widget_show(vbox);
142
143     GtkWidget* frameWindow = gtk_drawing_area_new();
144     registerRenderingAreaEvents(frameWindow); 
145     gtk_box_pack_start(GTK_BOX(vbox), frameWindow, TRUE, TRUE, 0);
146     gtk_widget_show(frameWindow);
147     GTK_WIDGET_SET_FLAGS(frameWindow, GTK_CAN_FOCUS);
148
149     gtk_widget_show_all(topLevelWindow);
150
151     EditorClientGdk* editorClient = new EditorClientGdk;
152     ContextMenuClient* contextMenuClient = new ContextMenuClientGdk;
153     Page* page = new Page(new ChromeClientGdk, contextMenuClient, editorClient, 0);
154     editorClient->setPage(page);
155     FrameLoaderClientGdk* frameLoaderClient = new FrameLoaderClientGdk;
156     gFrame = new FrameGdk(page, 0, frameLoaderClient);
157     gFrame->setExitAfterLoading(exitAfterLoading);
158     gFrame->setDumpRenderTreeAfterLoading(dumpRenderTree);
159
160     FrameView* frameView = new FrameView(gFrame);
161     gFrame->setView(frameView);
162     frameView->ScrollView::setDrawable(frameWindow->window);
163
164     printf("OPENING URL == %s \n", url);
165     gFrame->loader()->load(ResourceRequest(KURL(url)));
166     gtk_main();
167 #if 0 // FIXME: this crashes at the moment. needs to provide DragClient
168     delete page;
169 #endif
170     if (FrameLoader* loader = gFrame->loader())
171         loader->stopAllLoaders();
172     delete gFrame;
173     return 0;
174 }
175