tazweb view src/main.c @ rev 49

Use gtk_widget_set_size_request for toolbar size and remove border
author Christophe Lincoln <pankso@slitaz.org>
date Thu Apr 21 00:59:01 2011 +0200 (2011-04-21)
parents fe087b478df1
children 22991c13b29b
line source
1 /*
2 * TazWeb is a radically simple web browser providing a single window
3 * with a single toolbar with buttons and an URL entry, but no menu or
4 * tabs.
5 *
6 * Copyright (C) 2011 SliTaz GNU/Linux <devel@slitaz.org>
7 *
8 *
9 */
11 #include <gtk/gtk.h>
12 #include <webkit/webkit.h>
14 static GtkWidget* main_window;
15 static WebKitWebView* web_view;
16 static GtkWidget* uri_entry;
17 static gchar* main_title;
18 static gdouble load_progress;
19 static guint status_context_id;
21 /* Page title to window title */
22 static void
23 update_title (GtkWindow* window)
24 {
25 GString* string = g_string_new (main_title);
26 /* g_string_append (string, " - TazWeb"); */
27 if (load_progress < 100)
28 g_string_append_printf (string, " (%f%%)", load_progress);
29 gchar* title = g_string_free (string, FALSE);
30 gtk_window_set_title (window, title);
31 g_free (title);
32 }
34 static void
35 notify_title_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
36 {
37 if (main_title)
38 g_free (main_title);
39 main_title = g_strdup (webkit_web_view_get_title(web_view));
40 update_title (GTK_WINDOW (main_window));
41 }
43 /* Request progress in window title */
44 static void
45 notify_progress_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
46 {
47 load_progress = webkit_web_view_get_progress (web_view) * 100;
48 update_title (GTK_WINDOW (main_window));
49 }
51 static void
52 notify_load_status_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
53 {
54 if (webkit_web_view_get_load_status (web_view) == WEBKIT_LOAD_COMMITTED) {
55 WebKitWebFrame* frame = webkit_web_view_get_main_frame (web_view);
56 const gchar* uri = webkit_web_frame_get_uri (frame);
57 if (uri)
58 gtk_entry_set_text (GTK_ENTRY (uri_entry), uri);
59 }
60 }
62 /* URL entry callback function */
63 static void
64 activate_uri_entry_cb (GtkWidget* entry, gpointer data)
65 {
66 const gchar* uri = gtk_entry_get_text (GTK_ENTRY (entry));
67 g_assert (uri);
68 webkit_web_view_load_uri (web_view, uri);
69 }
71 static void
72 destroy_cb (GtkWidget* widget, gpointer data)
73 {
74 gtk_main_quit ();
75 }
77 /* Home button function */
78 static void
79 go_home_cb (GtkWidget* widget, gpointer data)
80 {
81 const gchar* uri = ("file:///usr/share/webhome/index.html");
82 g_assert (uri);
83 webkit_web_view_load_uri (web_view, uri);
84 }
86 /* My page button function */
87 static void
88 my_page_cb (GtkWidget* widget, gpointer data)
89 {
90 const gchar* uri = g_strdup_printf ("file://%s/.config/tazweb/page.html",
91 g_get_home_dir ());
92 g_assert (uri);
93 webkit_web_view_load_uri (web_view, uri);
94 }
96 /* Fullscreen and unfullscreen function */
97 static void
98 fullscreen_cb (GtkWindow* window, gpointer data)
99 {
100 GdkWindowState state;
101 state = gdk_window_get_state (gtk_widget_get_window (GTK_WIDGET (main_window)));
103 if (state & GDK_WINDOW_STATE_FULLSCREEN)
104 gtk_window_unfullscreen (GTK_WINDOW (main_window));
105 else
106 gtk_window_fullscreen (GTK_WINDOW (main_window));
107 }
109 /* TazWeb doc function */
110 static void
111 tazweb_doc_cb (GtkWidget* widget, gpointer data)
112 {
113 const gchar* uri = ("file:///usr/share/doc/tazweb/tazweb.html");
114 g_assert (uri);
115 webkit_web_view_load_uri (web_view, uri);
116 }
118 /* Download function */
119 static gboolean
120 download_requested_cb (WebKitWebView *web_view, WebKitDownload *download,
121 gpointer user_data)
122 {
123 const gchar* uri = webkit_download_get_uri (download);
124 gchar *buffer;
125 asprintf (&buffer, "tazbox dl-out %s", uri);
126 system (buffer);
127 }
129 static GtkWidget*
130 create_browser ()
131 {
132 GtkWidget* scrolled_window = gtk_scrolled_window_new (NULL, NULL);
133 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
134 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
136 web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
137 gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (web_view));
139 g_signal_connect (web_view, "notify::title",
140 G_CALLBACK (notify_title_cb), web_view);
141 g_signal_connect (web_view, "notify::progress",
142 G_CALLBACK (notify_progress_cb), web_view);
143 g_signal_connect (web_view, "notify::load-status",
144 G_CALLBACK (notify_load_status_cb), web_view);
145 g_signal_connect (web_view, "download-requested",
146 G_CALLBACK (download_requested_cb), NULL);
148 return scrolled_window;
149 }
151 /* Create an icon */
152 static GdkPixbuf*
153 create_pixbuf (const gchar * image)
154 {
155 GdkPixbuf *pixbuf;
156 pixbuf = gdk_pixbuf_new_from_file (image, NULL);
158 return pixbuf;
159 }
161 static GtkWidget*
162 create_toolbar ()
163 {
164 GtkToolItem* item;
166 GtkWidget* toolbar = gtk_toolbar_new ();
167 gtk_widget_set_size_request (toolbar, 0, 31);
168 gtk_toolbar_set_orientation (GTK_TOOLBAR (toolbar), GTK_ORIENTATION_HORIZONTAL);
169 gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_BOTH_HORIZ);
171 /* The Home button */
172 item = gtk_tool_button_new_from_stock (GTK_STOCK_HOME);
173 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (go_home_cb), NULL);
174 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
176 /* The personal page button */
177 item = gtk_tool_button_new_from_stock (GTK_STOCK_PREFERENCES);
178 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (my_page_cb), NULL);
179 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
181 /* The URL entry */
182 item = gtk_tool_item_new ();
183 gtk_tool_item_set_expand (item, TRUE);
184 uri_entry = gtk_entry_new ();
185 gtk_container_add (GTK_CONTAINER (item), uri_entry);
186 g_signal_connect (G_OBJECT (uri_entry), "activate",
187 G_CALLBACK (activate_uri_entry_cb), NULL);
188 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
190 /* The TazWeb doc button */
191 item = gtk_tool_button_new_from_stock (GTK_STOCK_INFO);
192 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (tazweb_doc_cb), NULL);
193 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
195 /* The Fullscreen button */
196 item = gtk_tool_button_new_from_stock (GTK_STOCK_FULLSCREEN);
197 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (fullscreen_cb), NULL);
198 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
200 return toolbar;
201 }
203 static GtkWidget*
204 create_window ()
205 {
206 GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
208 /* Default tazweb window size ratio to 3/4 --> 720, 540*/
209 gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
210 gtk_window_set_icon (GTK_WINDOW (window),
211 create_pixbuf ("/usr/share/pixmaps/tazweb.png"));
212 gtk_widget_set_name (window, "TazWeb");
213 g_signal_connect (window, "destroy", G_CALLBACK (destroy_cb), NULL);
215 return window;
216 }
218 int
219 main (int argc, char* argv[])
220 {
221 gtk_init (&argc, &argv);
222 if (!g_thread_supported ())
223 g_thread_init (NULL);
225 /* Get a default page.html if missing */
226 const gchar* config = g_strdup_printf ("%s/.config/tazweb", g_get_home_dir ());
227 if (!g_file_test(config, G_FILE_TEST_EXISTS)) {
228 g_mkdir (config, 0700);
229 system ("cp /usr/share/tazweb/* $HOME/.config/tazweb");
230 }
232 GtkWidget* vbox = gtk_vbox_new (FALSE, 0);
233 gtk_box_pack_start (GTK_BOX (vbox), create_browser (), TRUE, TRUE, 0);
234 gtk_box_pack_start (GTK_BOX (vbox), create_toolbar (), FALSE, FALSE, 0);
236 main_window = create_window ();
237 gtk_container_add (GTK_CONTAINER (main_window), vbox);
239 /* Home page url or file */
240 gchar* uri = (gchar*) (argc > 1 ? argv[1] :
241 "file:///usr/share/webhome/index.html");
242 webkit_web_view_load_uri (web_view, uri);
244 gtk_widget_grab_focus (GTK_WIDGET (web_view));
245 gtk_widget_show_all (main_window);
246 gtk_main ();
248 return 0;
249 }