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