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