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