2007-10-03 Alp Toker <alp@atoker.com>
[WebKit-https.git] / WebKitTools / GtkLauncher / main.cpp
1 #include "webkitgtkpage.h"
2 #include "webkitgtkglobal.h"
3
4 #include <gtk/gtk.h>
5
6 #include <string.h>
7
8 static GtkWidget* gURLBarEntry;
9 static GtkWidget* gTopLevelWindow;
10 static WebKitPage* gPage;
11 static gchar* gTitle;
12 static gint gProgress;
13
14 static bool stringIsEqual(const char* str1, const char* str2)
15 {
16     return 0 == strcmp(str1, str2);
17 }
18
19 static gchar* autocorrectURL(const gchar* url)
20 {
21     if (strncmp("http://", url, 7) != 0 && strncmp("https://", url, 8) != 0 && strncmp("file://", url, 7) != 0 && strncmp("ftp://", url, 6) != 0) {
22         GString* string = g_string_new("http://");
23         g_string_append(string, url);
24         return g_string_free(string, FALSE);
25     }
26     
27     return g_strdup(url);
28 }
29
30 static void goToURLBarText(GtkWidget* urlBarEntry)
31 {
32     const gchar* url = gtk_entry_get_text(GTK_ENTRY(urlBarEntry));
33     if (!url || strlen(url) == 0)
34         return;
35
36     gchar* parsedURL = autocorrectURL(url);
37     webkit_page_open(gPage, parsedURL);
38     g_free(parsedURL);
39 }
40
41 static void goButtonClickedCallback(GtkWidget*, GtkWidget* entry)
42 {
43     goToURLBarText(entry);
44 }
45
46 static void urlBarEnterCallback(GtkWidget*, GtkWidget* entry)
47 {
48     goToURLBarText(entry);
49 }
50
51 static void updateWindowTitle()
52 {
53     GString* string = g_string_new(NULL);
54     g_string_printf(string, "GtkLauncher %s  (%d/100)", gTitle, gProgress);
55     gchar* title = g_string_free(string, FALSE);
56     gtk_window_set_title(GTK_WINDOW(gTopLevelWindow), title);
57     g_free(title);
58 }
59
60 static void titleChanged(WebKitPage*, const gchar* title, const gchar* url, WebKitPage*)
61 {
62     gtk_entry_set_text(GTK_ENTRY(gURLBarEntry), url);
63
64     if (gTitle)
65         g_free(gTitle);
66     gTitle = g_strdup(title);
67     updateWindowTitle();
68 }
69
70 static void progressChanged(WebKitPage*, gint progress, WebKitPage*)
71 {
72     gProgress = progress;
73     updateWindowTitle();
74 }
75
76 static void frameDestroyCallback(GtkWidget*, gpointer)
77 {
78     gtk_main_quit();
79 }
80
81 static void menuMainBackCallback(gpointer data)
82 {
83     g_assert(!data);
84     webkit_page_go_backward(gPage);
85 }
86
87 static void menuMainForwardCallback(gpointer data)
88 {
89     g_assert(!data);
90     webkit_page_go_forward(gPage);
91 }
92
93 static void menuMainQuitCallback(gpointer)
94 {
95     gtk_main_quit();
96 }
97
98 int main(int argc, char* argv[]) 
99 {
100     gtk_init(&argc, &argv);
101     webkit_init();
102
103     gchar* url = "http://www.google.com";
104     bool exitAfterLoading = false;
105     bool dumpRenderTree = false;
106     for (int argPos = 1; argPos < argc; ++argPos) {
107         char *currArg = argv[argPos];
108         if (stringIsEqual(currArg, "-exit-after-loading"))
109             exitAfterLoading = true;
110         else if (stringIsEqual(currArg, "-exitafterloading"))
111             exitAfterLoading = true;
112         else if (stringIsEqual(currArg, "-exitafterload"))
113             exitAfterLoading = true;
114         else if (stringIsEqual(currArg, "-exit-after-load"))
115             exitAfterLoading = true;
116         else if (stringIsEqual(currArg, "-drt"))
117             dumpRenderTree = true;
118         else if (stringIsEqual(currArg, "-dump-render-tree"))
119             dumpRenderTree = true;
120         else if (stringIsEqual(currArg, "-dumprendertree"))
121             dumpRenderTree = true;
122         else
123             url = autocorrectURL(currArg);
124     }
125
126     GtkWidget* menuMain = gtk_menu_new();
127     GtkWidget* menuMainBack = gtk_menu_item_new_with_label("Back");
128     gtk_menu_shell_append(GTK_MENU_SHELL(menuMain), menuMainBack);
129     g_signal_connect_swapped(G_OBJECT(menuMainBack), "activate", G_CALLBACK(menuMainBackCallback), NULL);
130     gtk_widget_show(menuMainBack);
131
132     GtkWidget* menuMainForward = gtk_menu_item_new_with_label("Forward");
133     gtk_menu_shell_append(GTK_MENU_SHELL(menuMain), menuMainForward);
134     g_signal_connect_swapped(G_OBJECT(menuMainForward), "activate", G_CALLBACK(menuMainForwardCallback), NULL);
135     gtk_widget_show(menuMainForward);
136
137     GtkWidget* menuMainQuit = gtk_menu_item_new_with_label("Quit");
138     gtk_menu_shell_append(GTK_MENU_SHELL(menuMain), menuMainQuit);
139     g_signal_connect_swapped(G_OBJECT(menuMainQuit), "activate", G_CALLBACK(menuMainQuitCallback), NULL);
140     gtk_widget_show(menuMainQuit);
141
142     GtkWidget* menuMainRoot = gtk_menu_item_new_with_label("Main");
143     gtk_widget_show(menuMainRoot);
144     gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuMainRoot), menuMain);
145
146     GtkWidget* menuBar = gtk_menu_bar_new();
147     gtk_menu_shell_append(GTK_MENU_SHELL(menuBar), menuMainRoot);
148
149     gTopLevelWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
150     gtk_window_set_default_size(GTK_WINDOW(gTopLevelWindow), 800, 600);
151     gtk_widget_set_name(gTopLevelWindow, "GtkLauncher");
152     GtkWidget* vbox = gtk_vbox_new(FALSE, 0);
153     gtk_container_add(GTK_CONTAINER(gTopLevelWindow), vbox);
154     g_signal_connect(G_OBJECT(gTopLevelWindow), "destroy", G_CALLBACK(frameDestroyCallback), NULL);
155
156     GtkWidget* hbox = gtk_hbox_new(FALSE, 2);
157     gtk_box_pack_start(GTK_BOX(vbox), menuBar, FALSE, FALSE, 0);
158     gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
159
160     gURLBarEntry = gtk_entry_new();
161     g_signal_connect(G_OBJECT(gURLBarEntry), "activate", G_CALLBACK(urlBarEnterCallback), (gpointer)gURLBarEntry);
162     gtk_box_pack_start(GTK_BOX(hbox), gURLBarEntry, TRUE, TRUE, 0);
163
164     GtkWidget* urlBarSubmitButton = gtk_button_new_with_label("Go");  
165     gtk_box_pack_start(GTK_BOX(hbox), urlBarSubmitButton, FALSE, FALSE, 0);
166     g_signal_connect(G_OBJECT(urlBarSubmitButton), "clicked", G_CALLBACK(goButtonClickedCallback), (gpointer)gURLBarEntry);
167     gtk_widget_show(vbox);
168
169     GtkWidget* scrolledWindow = gtk_scrolled_window_new(NULL,NULL);
170     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledWindow),
171                                    GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
172     gPage = WEBKIT_PAGE(webkit_page_new());
173     gtk_container_add(GTK_CONTAINER(scrolledWindow), GTK_WIDGET(gPage));
174     gtk_box_pack_start(GTK_BOX(vbox), scrolledWindow, TRUE, TRUE, 0);
175     
176     g_signal_connect(gPage, "title-changed", G_CALLBACK(titleChanged), gPage);
177     g_signal_connect(gPage, "load-progress-changed", G_CALLBACK(progressChanged), gPage);
178     webkit_page_open(gPage, url);
179
180     gtk_widget_show_all(gTopLevelWindow);
181     gtk_main();
182     return 0;
183 }
184