using http://trac.webkit.org/browser/trunk/WebKitTools/GtkLauncher/main.c as starting...
[uzbl-mobile] / uzbl.c
1 /*
2  * Copyright (C) 2006, 2007 Apple Inc.
3  * Copyright (C) 2007 Alp Toker <alp@atoker.com>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <gtk/gtk.h>
28 #include <webkit/webkit.h>
29
30 static GtkWidget* main_window;
31 static GtkWidget* uri_entry;
32 static GtkStatusbar* main_statusbar;
33 static WebKitWebView* web_view;
34 static gchar* main_title;
35 static gint load_progress;
36 static guint status_context_id;
37
38 static void
39 activate_uri_entry_cb (GtkWidget* entry, gpointer data)
40 {
41     const gchar* uri = gtk_entry_get_text (GTK_ENTRY (entry));
42     g_assert (uri);
43     webkit_web_view_load_uri (web_view, uri);
44 }
45
46 static void
47 update_title (GtkWindow* window)
48 {
49     GString* string = g_string_new (main_title);
50     g_string_append (string, " - WebKit Launcher");
51     if (load_progress < 100)
52         g_string_append_printf (string, " (%d%%)", load_progress);
53     gchar* title = g_string_free (string, FALSE);
54     gtk_window_set_title (window, title);
55     g_free (title);
56 }
57
58 static void
59 link_hover_cb (WebKitWebView* page, const gchar* title, const gchar* link, gpointer data)
60 {
61     /* underflow is allowed */
62     gtk_statusbar_pop (main_statusbar, status_context_id);
63     if (link)
64         gtk_statusbar_push (main_statusbar, status_context_id, link);
65 }
66
67 static void
68 title_change_cb (WebKitWebView* web_view, WebKitWebFrame* web_frame, const gchar* title, gpointer data)
69 {
70     if (main_title)
71         g_free (main_title);
72     main_title = g_strdup (title);
73     update_title (GTK_WINDOW (main_window));
74 }
75
76 static void
77 progress_change_cb (WebKitWebView* page, gint progress, gpointer data)
78 {
79     load_progress = progress;
80     update_title (GTK_WINDOW (main_window));
81 }
82
83 static void
84 load_commit_cb (WebKitWebView* page, WebKitWebFrame* frame, gpointer data)
85 {
86     const gchar* uri = webkit_web_frame_get_uri(frame);
87     if (uri)
88         gtk_entry_set_text (GTK_ENTRY (uri_entry), uri);
89 }
90
91 static void
92 destroy_cb (GtkWidget* widget, gpointer data)
93 {
94     gtk_main_quit ();
95 }
96
97 static void
98 go_back_cb (GtkWidget* widget, gpointer data)
99 {
100     webkit_web_view_go_back (web_view);
101 }
102
103 static void
104 go_forward_cb (GtkWidget* widget, gpointer data)
105 {
106     webkit_web_view_go_forward (web_view);
107 }
108
109 static GtkWidget*
110 create_browser ()
111 {
112     GtkWidget* scrolled_window = gtk_scrolled_window_new (NULL, NULL);
113     gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
114
115     web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
116     gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (web_view));
117
118     g_signal_connect (G_OBJECT (web_view), "title-changed", G_CALLBACK (title_change_cb), web_view);
119     g_signal_connect (G_OBJECT (web_view), "load-progress-changed", G_CALLBACK (progress_change_cb), web_view);
120     g_signal_connect (G_OBJECT (web_view), "load-committed", G_CALLBACK (load_commit_cb), web_view);
121     g_signal_connect (G_OBJECT (web_view), "hovering-over-link", G_CALLBACK (link_hover_cb), web_view);
122
123     return scrolled_window;
124 }
125
126 static GtkWidget*
127 create_statusbar ()
128 {
129     main_statusbar = GTK_STATUSBAR (gtk_statusbar_new ());
130     status_context_id = gtk_statusbar_get_context_id (main_statusbar, "Link Hover");
131
132     return (GtkWidget*)main_statusbar;
133 }
134
135 static GtkWidget*
136 create_toolbar ()
137 {
138     GtkWidget* toolbar = gtk_toolbar_new ();
139
140     gtk_toolbar_set_orientation (GTK_TOOLBAR (toolbar), GTK_ORIENTATION_HORIZONTAL);
141     gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_BOTH_HORIZ);
142
143     GtkToolItem* item;
144
145     /* the back button */
146     item = gtk_tool_button_new_from_stock (GTK_STOCK_GO_BACK);
147     g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (go_back_cb), NULL);
148     gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
149
150     /* The forward button */
151     item = gtk_tool_button_new_from_stock (GTK_STOCK_GO_FORWARD);
152     g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (go_forward_cb), NULL);
153     gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
154
155     /* The URL entry */
156     item = gtk_tool_item_new ();
157     gtk_tool_item_set_expand (item, TRUE);
158     uri_entry = gtk_entry_new ();
159     gtk_container_add (GTK_CONTAINER (item), uri_entry);
160     g_signal_connect (G_OBJECT (uri_entry), "activate", G_CALLBACK (activate_uri_entry_cb), NULL);
161     gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
162
163     /* The go button */
164     item = gtk_tool_button_new_from_stock (GTK_STOCK_OK);
165     g_signal_connect_swapped (G_OBJECT (item), "clicked", G_CALLBACK (activate_uri_entry_cb), (gpointer)uri_entry);
166     gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
167
168     return toolbar;
169 }
170
171 static GtkWidget*
172 create_window ()
173 {
174     GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
175     gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
176     gtk_widget_set_name (window, "GtkLauncher");
177     g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (destroy_cb), NULL);
178
179     return window;
180 }
181
182 int
183 main (int argc, char* argv[])
184 {
185     gtk_init (&argc, &argv);
186     if (!g_thread_supported ())
187         g_thread_init (NULL);
188
189     GtkWidget* vbox = gtk_vbox_new (FALSE, 0);
190     gtk_box_pack_start (GTK_BOX (vbox), create_toolbar (), FALSE, FALSE, 0);
191     gtk_box_pack_start (GTK_BOX (vbox), create_browser (), TRUE, TRUE, 0);
192     gtk_box_pack_start (GTK_BOX (vbox), create_statusbar (), FALSE, FALSE, 0);
193
194     main_window = create_window ();
195     gtk_container_add (GTK_CONTAINER (main_window), vbox);
196
197     gchar* uri = (gchar*) (argc > 1 ? argv[1] : "http://www.google.com/");
198     webkit_web_view_load_uri (web_view, uri);
199
200     gtk_widget_grab_focus (GTK_WIDGET (web_view));
201     gtk_widget_show_all (main_window);
202     gtk_main ();
203
204     return 0;
205 }