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