WebCore:
[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 static void menuMainBackCb(gpointer data)
97 {
98     ASSERT(!data);
99     gFrame->loader()->goBackOrForward(-1);
100 }
101
102 static void menuMainForwardCb(gpointer data)
103 {
104     ASSERT(!data);
105     gFrame->loader()->goBackOrForward(1);
106 }
107
108 static void menuMainQuitCb(gpointer data)
109 {
110     gtk_main_quit();
111 }
112
113 int main(int argc, char* argv[]) 
114 {
115     gtk_init(&argc, &argv);
116
117     const char* url = "http://www.google.com";
118     bool exitAfterLoading = false;
119     bool dumpRenderTree = false;
120     for (int argPos = 1; argPos < argc; ++argPos) {
121         char *currArg = argv[argPos];
122         if (strEq(currArg, "-exit-after-loading"))
123             exitAfterLoading = true;
124         else if (strEq(currArg, "-exitafterloading"))
125             exitAfterLoading = true;
126         else if (strEq(currArg, "-exitafterload"))
127             exitAfterLoading = true;
128         else if (strEq(currArg, "-exit-after-load"))
129             exitAfterLoading = true;
130         else if (strEq(currArg, "-drt"))
131             dumpRenderTree = true;
132         else if (strEq(currArg, "-dump-render-tree"))
133             dumpRenderTree = true;
134         else if (strEq(currArg, "-dumprendertree"))
135             dumpRenderTree = true;
136         else
137             url = currArg;
138     }
139
140     GtkWidget* menuMain = gtk_menu_new();
141     GtkWidget* menuMainBack = gtk_menu_item_new_with_label("Back");
142     gtk_menu_shell_append(GTK_MENU_SHELL(menuMain), menuMainBack);
143     g_signal_connect_swapped(G_OBJECT(menuMainBack), "activate", G_CALLBACK(menuMainBackCb), NULL);
144     gtk_widget_show(menuMainBack);
145
146     GtkWidget* menuMainForward = gtk_menu_item_new_with_label("Forward");
147     gtk_menu_shell_append(GTK_MENU_SHELL(menuMain), menuMainForward);
148     g_signal_connect_swapped(G_OBJECT(menuMainForward), "activate", G_CALLBACK(menuMainForwardCb), NULL);
149     gtk_widget_show(menuMainForward);
150
151     GtkWidget* menuMainQuit = gtk_menu_item_new_with_label("Quit");
152     gtk_menu_shell_append(GTK_MENU_SHELL(menuMain), menuMainQuit);
153     g_signal_connect_swapped(G_OBJECT(menuMainQuit), "activate", G_CALLBACK(menuMainQuitCb), NULL);
154     gtk_widget_show(menuMainQuit);
155
156     GtkWidget* menuMainRoot = gtk_menu_item_new_with_label("Main");
157     gtk_widget_show(menuMainRoot);
158     gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuMainRoot), menuMain);
159
160     GtkWidget* menuBar = gtk_menu_bar_new();
161     gtk_menu_bar_append(GTK_MENU_BAR(menuBar), menuMainRoot);
162
163     GtkWidget* topLevelWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
164     gtk_window_set_default_size(GTK_WINDOW(topLevelWindow), 800, 600);
165     gtk_widget_set_name(topLevelWindow, "GdkLauncher");
166     GtkWidget* vbox = gtk_vbox_new(FALSE, 0);
167     gtk_container_add(GTK_CONTAINER(topLevelWindow), vbox);
168     g_signal_connect(G_OBJECT(topLevelWindow), "destroy", G_CALLBACK(frameDestroyCb), NULL);
169     g_signal_connect(GTK_OBJECT(topLevelWindow), "size-request", G_CALLBACK(frameResizeCb), NULL);
170
171     GtkWidget* hbox = gtk_hbox_new(FALSE, 2);
172     gtk_box_pack_start(GTK_BOX(vbox), menuBar, FALSE, FALSE, 0);
173     gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
174
175     gUrlBarEntry = gtk_entry_new();
176     g_signal_connect(G_OBJECT(gUrlBarEntry), "activate", G_CALLBACK(urlBarEnterCb), (gpointer)gUrlBarEntry);
177     gtk_box_pack_start(GTK_BOX(hbox), gUrlBarEntry, TRUE, TRUE, 0);
178
179     GtkWidget* urlBarSubmitButton = gtk_button_new_with_label("Go");  
180     gtk_box_pack_start(GTK_BOX(hbox), urlBarSubmitButton, FALSE, FALSE, 0);
181     g_signal_connect(G_OBJECT(urlBarSubmitButton), "clicked", G_CALLBACK(goButtonClickedCb), (gpointer)gUrlBarEntry);
182     gtk_widget_show(vbox);
183
184     GtkWidget* frameWindow = gtk_drawing_area_new();
185     registerRenderingAreaEvents(frameWindow); 
186     gtk_box_pack_start(GTK_BOX(vbox), frameWindow, TRUE, TRUE, 0);
187     gtk_widget_show(frameWindow);
188     GTK_WIDGET_SET_FLAGS(frameWindow, GTK_CAN_FOCUS);
189
190     gtk_widget_show_all(topLevelWindow);
191
192     EditorClientGdk* editorClient = new EditorClientGdk;
193     ContextMenuClient* contextMenuClient = new ContextMenuClientGdk;
194     Page* page = new Page(new ChromeClientGdk, contextMenuClient, editorClient, 0);
195     editorClient->setPage(page);
196     FrameLoaderClientGdk* frameLoaderClient = new FrameLoaderClientGdk;
197     gFrame = new FrameGdk(page, 0, frameLoaderClient);
198     gFrame->setExitAfterLoading(exitAfterLoading);
199     gFrame->setDumpRenderTreeAfterLoading(dumpRenderTree);
200
201     FrameView* frameView = new FrameView(gFrame);
202     gFrame->setView(frameView);
203     frameView->ScrollView::setDrawable(frameWindow->window);
204
205     printf("OPENING URL == %s \n", url);
206     gFrame->loader()->load(ResourceRequest(KURL(url)));
207     gtk_main();
208 #if 0 // FIXME: this crashes at the moment. needs to provide DragClient
209     delete page;
210 #endif
211     if (FrameLoader* loader = gFrame->loader())
212         loader->stopAllLoaders();
213     delete gFrame;
214     return 0;
215 }
216