2007-05-18 Holger Hans Peter Freyther <zecke@selfish.org>
[WebKit-https.git] / WebKitTools / GdkLauncher / main.cpp
1 #include "config.h"
2 #include "Platform.h"
3 #include "ChromeClientGdk.h"
4 #include "ContextMenuClientGdk.h"
5 #include "Document.h"
6 #include "DragClient.h"
7 #include "EditorClientGdk.h"
8 #include "FrameGdk.h"
9 #include "FrameLoader.h"
10 #include "FrameLoaderClientGdk.h"
11 #include "FrameView.h"
12 #include "KURL.h"
13 #include "Page.h"
14 #include "PlatformString.h"
15 #include "ResourceHandleManager.h"
16
17 #if SVG_SUPPORT
18 #include "SVGNames.h"
19 #include "XLinkNames.h"
20 #include "SVGDocumentExtensions.h"
21 #endif
22
23 #include <gdk/gdk.h>
24 #include <gtk/gtk.h>
25
26 using namespace WebCore;
27
28 static GtkWidget* gURLBarEntry;
29 static FrameGdk* gFrame = 0;
30
31 static bool stringIsEqual(const char* str1, const char* str2)
32 {
33     return 0 == strcmp(str1, str2);
34 }
35
36 static void handleGdkEvent(GtkWidget* widget, GdkEvent* event)
37 {
38     gFrame->handleGdkEvent(event);
39 }
40
41 static String autocorrectURL(const String& url)
42 {
43     String parsedURL = url;
44     if (!url.startsWith("http://") && !url.startsWith("https://")
45         && !url.startsWith("file://") && !url.startsWith("ftp://"))
46         parsedURL = String("http://") + url;
47     return parsedURL;
48 }
49
50 static void goToURLBarText(GtkWidget* urlBarEntry)
51 {
52     String url(gtk_entry_get_text(GTK_ENTRY(urlBarEntry)));
53     if (url.isEmpty())
54         return;
55
56     String parsedURL = autocorrectURL(url);
57     gFrame->loader()->load(ResourceRequest(parsedURL));
58 }
59
60 static void goButtonClickedCallback(GtkWidget* widget, GtkWidget* entry)
61 {
62     goToURLBarText(entry);
63 }
64
65 static void urlBarEnterCallback(GtkWidget* widget, GtkWidget* entry)
66 {
67     goToURLBarText(entry);
68 }
69
70 static void registerRenderingAreaEvents(GtkWidget* win)
71 {
72     gtk_widget_set_events(win, GDK_EXPOSURE_MASK
73                          | GDK_BUTTON_PRESS_MASK
74                          | GDK_BUTTON_RELEASE_MASK
75                          | GDK_POINTER_MOTION_MASK
76                          | GDK_KEY_PRESS_MASK
77                          | GDK_KEY_RELEASE_MASK
78                          | GDK_BUTTON_MOTION_MASK
79                          | GDK_BUTTON1_MOTION_MASK
80                          | GDK_BUTTON2_MOTION_MASK
81                          | GDK_BUTTON3_MOTION_MASK);
82
83     g_signal_connect(GTK_OBJECT(win), "expose-event", G_CALLBACK(handleGdkEvent), NULL);
84     g_signal_connect(GTK_OBJECT(win), "key-press-event", G_CALLBACK(handleGdkEvent), NULL);
85     g_signal_connect(GTK_OBJECT(win), "key-release-event", G_CALLBACK(handleGdkEvent), NULL);
86     g_signal_connect(GTK_OBJECT(win), "button-press-event", G_CALLBACK(handleGdkEvent), NULL);
87     g_signal_connect(GTK_OBJECT(win), "button-release-event", G_CALLBACK(handleGdkEvent), NULL);
88     g_signal_connect(GTK_OBJECT(win), "motion-notify-event", G_CALLBACK(handleGdkEvent), NULL);
89     g_signal_connect(GTK_OBJECT(win), "scroll-event", G_CALLBACK(handleGdkEvent), NULL);
90 }
91
92 static void frameResizeCallback(GtkWidget* widget, GtkAllocation* allocation, gpointer data)
93 {
94     if (!gFrame)
95         return;
96
97     gFrame->view()->setFrameGeometry(IntRect(allocation->x,allocation->y,allocation->width,allocation->height));
98     gFrame->forceLayout();
99     gFrame->sendResizeEvent();
100 }
101
102 static void frameDestroyCallback(GtkWidget* widget, gpointer data)
103 {
104     gtk_main_quit();
105 }
106
107 static void menuMainBackCallback(gpointer data)
108 {
109     ASSERT(!data);
110     gFrame->loader()->goBackOrForward(-1);
111 }
112
113 static void menuMainForwardCallback(gpointer data)
114 {
115     ASSERT(!data);
116     gFrame->loader()->goBackOrForward(1);
117 }
118
119 static void menuMainQuitCallback(gpointer data)
120 {
121     gtk_main_quit();
122 }
123
124 int main(int argc, char* argv[]) 
125 {
126     gtk_init(&argc, &argv);
127
128     String url("http://www.google.com");
129     bool exitAfterLoading = false;
130     bool dumpRenderTree = false;
131     for (int argPos = 1; argPos < argc; ++argPos) {
132         char *currArg = argv[argPos];
133         if (stringIsEqual(currArg, "-exit-after-loading"))
134             exitAfterLoading = true;
135         else if (stringIsEqual(currArg, "-exitafterloading"))
136             exitAfterLoading = true;
137         else if (stringIsEqual(currArg, "-exitafterload"))
138             exitAfterLoading = true;
139         else if (stringIsEqual(currArg, "-exit-after-load"))
140             exitAfterLoading = true;
141         else if (stringIsEqual(currArg, "-drt"))
142             dumpRenderTree = true;
143         else if (stringIsEqual(currArg, "-dump-render-tree"))
144             dumpRenderTree = true;
145         else if (stringIsEqual(currArg, "-dumprendertree"))
146             dumpRenderTree = true;
147         else
148             url = autocorrectURL(currArg);
149     }
150
151     GtkWidget* menuMain = gtk_menu_new();
152     GtkWidget* menuMainBack = gtk_menu_item_new_with_label("Back");
153     gtk_menu_shell_append(GTK_MENU_SHELL(menuMain), menuMainBack);
154     g_signal_connect_swapped(G_OBJECT(menuMainBack), "activate", G_CALLBACK(menuMainBackCallback), NULL);
155     gtk_widget_show(menuMainBack);
156
157     GtkWidget* menuMainForward = gtk_menu_item_new_with_label("Forward");
158     gtk_menu_shell_append(GTK_MENU_SHELL(menuMain), menuMainForward);
159     g_signal_connect_swapped(G_OBJECT(menuMainForward), "activate", G_CALLBACK(menuMainForwardCallback), NULL);
160     gtk_widget_show(menuMainForward);
161
162     GtkWidget* menuMainQuit = gtk_menu_item_new_with_label("Quit");
163     gtk_menu_shell_append(GTK_MENU_SHELL(menuMain), menuMainQuit);
164     g_signal_connect_swapped(G_OBJECT(menuMainQuit), "activate", G_CALLBACK(menuMainQuitCallback), NULL);
165     gtk_widget_show(menuMainQuit);
166
167     GtkWidget* menuMainRoot = gtk_menu_item_new_with_label("Main");
168     gtk_widget_show(menuMainRoot);
169     gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuMainRoot), menuMain);
170
171     GtkWidget* menuBar = gtk_menu_bar_new();
172     gtk_menu_shell_append(GTK_MENU_SHELL(menuBar), menuMainRoot);
173
174     GtkWidget* topLevelWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
175     gtk_window_set_default_size(GTK_WINDOW(topLevelWindow), 800, 600);
176     gtk_widget_set_name(topLevelWindow, "GdkLauncher");
177     GtkWidget* vbox = gtk_vbox_new(FALSE, 0);
178     gtk_container_add(GTK_CONTAINER(topLevelWindow), vbox);
179     g_signal_connect(G_OBJECT(topLevelWindow), "destroy", G_CALLBACK(frameDestroyCallback), NULL);
180
181     GtkWidget* hbox = gtk_hbox_new(FALSE, 2);
182     gtk_box_pack_start(GTK_BOX(vbox), menuBar, FALSE, FALSE, 0);
183     gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
184
185     gURLBarEntry = gtk_entry_new();
186     g_signal_connect(G_OBJECT(gURLBarEntry), "activate", G_CALLBACK(urlBarEnterCallback), (gpointer)gURLBarEntry);
187     gtk_box_pack_start(GTK_BOX(hbox), gURLBarEntry, TRUE, TRUE, 0);
188
189     GtkWidget* urlBarSubmitButton = gtk_button_new_with_label("Go");  
190     gtk_box_pack_start(GTK_BOX(hbox), urlBarSubmitButton, FALSE, FALSE, 0);
191     g_signal_connect(G_OBJECT(urlBarSubmitButton), "clicked", G_CALLBACK(goButtonClickedCallback), (gpointer)gURLBarEntry);
192     gtk_widget_show(vbox);
193
194     GtkWidget* frameWindow = gtk_drawing_area_new();
195     registerRenderingAreaEvents(frameWindow); 
196     gtk_box_pack_start(GTK_BOX(vbox), frameWindow, TRUE, TRUE, 0);
197     g_signal_connect(GTK_OBJECT(frameWindow), "size-allocate", G_CALLBACK(frameResizeCallback), NULL);
198     gtk_widget_show(frameWindow);
199     GTK_WIDGET_SET_FLAGS(frameWindow, GTK_CAN_FOCUS);
200
201     gtk_widget_show_all(topLevelWindow);
202
203     EditorClientGdk* editorClient = new EditorClientGdk;
204     ContextMenuClient* contextMenuClient = new ContextMenuClientGdk;
205     Page* page = new Page(new ChromeClientGdk, contextMenuClient, editorClient, 0);
206     editorClient->setPage(page);
207     FrameLoaderClientGdk* frameLoaderClient = new FrameLoaderClientGdk;
208     gFrame = new FrameGdk(page, 0, frameLoaderClient);
209     gFrame->setExitAfterLoading(exitAfterLoading);
210     gFrame->setDumpRenderTreeAfterLoading(dumpRenderTree);
211
212     FrameView* frameView = new FrameView(gFrame);
213     gFrame->setView(frameView);
214     frameView->ScrollView::setDrawable(frameWindow->window);
215
216     gFrame->init();
217     gFrame->loader()->load(ResourceRequest(url));
218     gtk_main();
219 #if 0 // FIXME: this crashes at the moment. needs to provide DragClient
220     delete page;
221 #endif
222     if (FrameLoader* loader = gFrame->loader())
223         loader->stopAllLoaders();
224     delete gFrame;
225     return 0;
226 }
227