new file: README
[freeto.git] / main.c
blob8cdf2a51fae5d887cc7407a4023afaa5e40ded1a
1 /*
2 * Copyright (C) 2006, 2007 Apple Inc.
3 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
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.
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.
27 #include <gtk/gtk.h>
28 #include <webkit/webkit.h>
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 gdouble load_progress;
36 static guint status_context_id;
38 static void
39 activate_uri_entry_cb (GtkWidget* entry, gpointer data)
41 const gchar* uri = gtk_entry_get_text (GTK_ENTRY (entry));
42 g_assert (uri);
43 webkit_web_view_load_uri (web_view, uri);
46 static void
47 update_title (GtkWindow* window)
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, " (%f%%)", load_progress);
53 gchar* title = g_string_free (string, FALSE);
54 gtk_window_set_title (window, title);
55 g_free (title);
58 static void
59 link_hover_cb (WebKitWebView* page, const gchar* title, const gchar* link, gpointer data)
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);
67 static void
68 title_change_cb (WebKitWebView* web_view, WebKitWebFrame* web_frame, const gchar* title, gpointer data)
70 if (main_title)
71 g_free (main_title);
72 main_title = g_strdup (title);
73 update_title (GTK_WINDOW (main_window));
76 static void
77 notify_load_status_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
79 if (webkit_web_view_get_load_status (web_view) == WEBKIT_LOAD_COMMITTED) {
80 WebKitWebFrame* frame = webkit_web_view_get_main_frame (web_view);
81 const gchar* uri = webkit_web_frame_get_uri (frame);
82 if (uri)
83 gtk_entry_set_text (GTK_ENTRY (uri_entry), uri);
87 static void
88 notify_progress_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
90 load_progress = webkit_web_view_get_progress (web_view) * 100;
91 update_title (GTK_WINDOW (main_window));
94 static void
95 destroy_cb (GtkWidget* widget, gpointer data)
97 gtk_main_quit ();
100 static void
101 go_back_cb (GtkWidget* widget, gpointer data)
103 webkit_web_view_go_back (web_view);
106 static void
107 go_forward_cb (GtkWidget* widget, gpointer data)
109 webkit_web_view_go_forward (web_view);
112 static GtkWidget*
113 create_browser ()
115 GtkWidget* scrolled_window = gtk_scrolled_window_new (NULL, NULL);
116 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
118 web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
119 gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (web_view));
121 g_signal_connect (web_view, "title-changed", G_CALLBACK (title_change_cb), web_view);
122 g_signal_connect (web_view, "notify::load-status", G_CALLBACK (notify_load_status_cb), web_view);
123 g_signal_connect (web_view, "notify::progress", G_CALLBACK (notify_progress_cb), web_view);
124 g_signal_connect (web_view, "hovering-over-link", G_CALLBACK (link_hover_cb), web_view);
126 return scrolled_window;
129 static GtkWidget*
130 create_statusbar ()
132 main_statusbar = GTK_STATUSBAR (gtk_statusbar_new ());
133 status_context_id = gtk_statusbar_get_context_id (main_statusbar, "Link Hover");
135 return (GtkWidget*)main_statusbar;
138 static GtkWidget*
139 create_toolbar ()
141 GtkWidget* toolbar = gtk_toolbar_new ();
144 gtk_orientable_set_orientation (GTK_ORIENTABLE (toolbar), GTK_ORIENTATION_HORIZONTAL);
146 gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_BOTH_HORIZ);
148 GtkToolItem* item;
150 /* the back button */
151 item = gtk_tool_button_new_from_stock (GTK_STOCK_GO_BACK);
152 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (go_back_cb), NULL);
153 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
155 /* The forward button */
156 item = gtk_tool_button_new_from_stock (GTK_STOCK_GO_FORWARD);
157 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (go_forward_cb), NULL);
158 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
160 /* The URL entry */
161 item = gtk_tool_item_new ();
162 gtk_tool_item_set_expand (item, TRUE);
163 uri_entry = gtk_entry_new ();
164 gtk_container_add (GTK_CONTAINER (item), uri_entry);
165 g_signal_connect (G_OBJECT (uri_entry), "activate", G_CALLBACK (activate_uri_entry_cb), NULL);
166 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
168 /* The go button */
169 item = gtk_tool_button_new_from_stock (GTK_STOCK_OK);
170 g_signal_connect_swapped (G_OBJECT (item), "clicked", G_CALLBACK (activate_uri_entry_cb), (gpointer)uri_entry);
171 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
173 return toolbar;
176 static GtkWidget*
177 create_window ()
179 GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
180 gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
181 gtk_widget_set_name (window, "GtkLauncher");
182 g_signal_connect (window, "destroy", G_CALLBACK (destroy_cb), NULL);
184 return window;
188 main (int argc, char* argv[])
190 gtk_init (&argc, &argv);
191 if (!g_thread_supported ())
192 g_thread_init (NULL);
194 GtkWidget* vbox = gtk_vbox_new (FALSE, 0);
195 gtk_box_pack_start (GTK_BOX (vbox), create_toolbar (), FALSE, FALSE, 0);
196 gtk_box_pack_start (GTK_BOX (vbox), create_browser (), TRUE, TRUE, 0);
197 gtk_box_pack_start (GTK_BOX (vbox), create_statusbar (), FALSE, FALSE, 0);
199 main_window = create_window ();
200 gtk_container_add (GTK_CONTAINER (main_window), vbox);
202 gchar* uri = (gchar*) (argc > 1 ? argv[1] : "http://www.google.com/");
203 webkit_web_view_load_uri (web_view, uri);
205 gtk_widget_grab_focus (GTK_WIDGET (web_view));
206 gtk_widget_show_all (main_window);
207 gtk_main ();
209 return 0;