tazweb view src/main.c @ rev 59

Move TazWeb documentation button to a popup menu item
author Christophe Lincoln <pankso@slitaz.org>
date Fri Apr 22 03:49:58 2011 +0200 (2011-04-22)
parents ae341ad49c1b
children 246560e6885c
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 */
10 #include <gtk/gtk.h>
11 #include <webkit/webkit.h>
13 static GtkWidget *main_window, *scrolled, *toolbar, *uri_entry, *search_entry;
14 static WebKitWebView* web_view;
15 static WebKitWebFrame* frame;
16 static gdouble load_progress;
17 static guint status_context_id;
18 static gchar* main_title;
20 const gchar* config;
21 const gchar* uri;
23 /* Create an icon */
24 static GdkPixbuf*
25 create_pixbuf (const gchar * image)
26 {
27 GdkPixbuf *pixbuf;
28 pixbuf = gdk_pixbuf_new_from_file (image, NULL);
29 return pixbuf;
30 }
32 /* Get a default page.html if missing */
33 static void
34 get_config ()
35 {
36 config = g_strdup_printf ("%s/.config/tazweb", g_get_home_dir ());
37 if (! g_file_test(config, G_FILE_TEST_EXISTS)) {
38 g_mkdir (config, 0700);
39 system ("cp /usr/share/tazweb/* $HOME/.config/tazweb");
40 }
41 }
43 /* Page title to window title */
44 static void
45 update_title (GtkWindow* window)
46 {
47 GString* string = g_string_new (main_title);
48 /* g_string_append (string, " - TazWeb"); */
49 if (load_progress < 100)
50 g_string_append_printf (string, " [ %f%% ] ", load_progress);
51 gchar* title = g_string_free (string, FALSE);
52 gtk_window_set_title (window, title);
53 g_free (title);
54 }
56 static void
57 notify_title_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
58 {
59 if (main_title)
60 g_free (main_title);
61 main_title = g_strdup (webkit_web_view_get_title(web_view));
62 update_title (GTK_WINDOW (main_window));
63 }
65 /* Request progress in window title */
66 static void
67 notify_progress_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
68 {
69 load_progress = webkit_web_view_get_progress (web_view) * 100;
70 update_title (GTK_WINDOW (main_window));
71 }
73 static void
74 notify_load_status_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
75 {
76 if (webkit_web_view_get_load_status (web_view) == WEBKIT_LOAD_COMMITTED) {
77 frame = webkit_web_view_get_main_frame (web_view);
78 uri = webkit_web_frame_get_uri (frame);
79 if (uri)
80 gtk_entry_set_text (GTK_ENTRY (uri_entry), uri);
81 }
82 }
84 static void
85 destroy_cb (GtkWidget* widget, gpointer data)
86 {
87 gtk_main_quit ();
88 }
90 /* Show page source */
91 static void
92 view_source_cb ()
93 {
94 gboolean source;
96 frame = webkit_web_view_get_main_frame (web_view);
97 uri = webkit_web_frame_get_uri (frame);
98 source = webkit_web_view_get_view_source_mode (web_view);
100 webkit_web_view_set_view_source_mode(web_view, !source);
101 webkit_web_view_load_uri (web_view, uri);
102 }
104 /* URL entry callback function */
105 static void
106 uri_entry_cb (GtkWidget* entry, gpointer data)
107 {
108 uri = gtk_entry_get_text (GTK_ENTRY (entry));
109 g_assert (uri);
110 webkit_web_view_load_uri (web_view, uri);
111 }
113 /* Search entry callback function */
114 static void
115 search_entry_cb (GtkWidget* entry, gpointer data)
116 {
117 uri = g_strdup_printf ("http://www.google.com/search?q=%s",
118 gtk_entry_get_text (GTK_ENTRY (entry)));
119 g_assert (uri);
120 webkit_web_view_load_uri (web_view, uri);
121 }
123 /* Home button callback function */
124 static void
125 go_home_cb (GtkWidget* widget, gpointer data)
126 {
127 uri = g_strdup_printf ("file://%s/.config/tazweb/home.html",
128 g_get_home_dir ());
129 g_assert (uri);
130 webkit_web_view_load_uri (web_view, uri);
131 }
133 /* Fullscreen and unfullscreen callback function */
134 static void
135 fullscreen_cb (GtkWindow* window, gpointer data)
136 {
137 GdkWindowState state;
138 state = gdk_window_get_state (gtk_widget_get_window (GTK_WIDGET (main_window)));
140 if (state & GDK_WINDOW_STATE_FULLSCREEN)
141 gtk_window_unfullscreen (GTK_WINDOW (main_window));
142 else
143 gtk_window_fullscreen (GTK_WINDOW (main_window));
144 }
146 /* TazWeb doc callback function */
147 static void
148 tazweb_doc_cb (GtkWidget* widget, gpointer data)
149 {
150 uri = ("file:///usr/share/doc/tazweb/tazweb.html");
151 g_assert (uri);
152 webkit_web_view_load_uri (web_view, uri);
153 }
155 /* Download function */
156 static gboolean
157 download_requested_cb (WebKitWebView *web_view, WebKitDownload *download,
158 gpointer user_data)
159 {
160 uri = webkit_download_get_uri (download);
161 gchar *buffer;
162 asprintf (&buffer, "tazbox dl-out %s", uri);
163 system (buffer);
164 }
166 /* Add items to WebKit contextual menu */
167 static void
168 populate_menu_cb (WebKitWebView *web_view, GtkMenu *menu, gpointer data)
169 {
170 GtkWidget *item;
172 /* separator */
173 item = gtk_separator_menu_item_new ();
174 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
176 /* TazWeb documentation */
177 item = gtk_image_menu_item_new_with_label ("TazWeb manual");
178 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
179 gtk_image_new_from_stock (GTK_STOCK_INFO, GTK_ICON_SIZE_MENU));
180 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
181 g_signal_connect (item, "activate", G_CALLBACK (tazweb_doc_cb), NULL);
183 /* View source mode */
184 item = gtk_image_menu_item_new_with_label ("View source mode");
185 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
186 gtk_image_new_from_stock (GTK_STOCK_PROPERTIES, GTK_ICON_SIZE_MENU));
187 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
188 g_signal_connect (item, "activate", G_CALLBACK (view_source_cb), NULL);
190 gtk_widget_show_all (GTK_WIDGET (menu));
191 }
193 /* Scrolled window for the web_view */
194 static GtkWidget*
195 create_browser ()
196 {
197 scrolled = gtk_scrolled_window_new (NULL, NULL);
198 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
199 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
201 web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
202 gtk_container_add (GTK_CONTAINER (scrolled), GTK_WIDGET (web_view));
204 /* Connect events */
205 g_signal_connect (web_view, "notify::title",
206 G_CALLBACK (notify_title_cb), web_view);
207 g_signal_connect (web_view, "notify::progress",
208 G_CALLBACK (notify_progress_cb), web_view);
209 g_signal_connect (web_view, "notify::load-status",
210 G_CALLBACK (notify_load_status_cb), web_view);
211 g_signal_connect (web_view, "download-requested",
212 G_CALLBACK (download_requested_cb), NULL);
214 /* Connect WebKit contextual menu items */
215 g_object_connect (G_OBJECT (web_view), "signal::populate-popup",
216 G_CALLBACK (populate_menu_cb), web_view, NULL);
218 return scrolled;
219 }
221 static GtkWidget*
222 create_toolbar ()
223 {
224 GtkToolItem* item;
226 toolbar = gtk_toolbar_new ();
227 gtk_widget_set_size_request (toolbar, 0, 31);
228 gtk_toolbar_set_orientation (GTK_TOOLBAR (toolbar),
229 GTK_ORIENTATION_HORIZONTAL);
230 gtk_toolbar_set_style (GTK_TOOLBAR (toolbar),
231 GTK_TOOLBAR_BOTH_HORIZ);
233 /* Home button */
234 item = gtk_tool_button_new_from_stock (GTK_STOCK_HOME);
235 g_signal_connect (G_OBJECT (item), "clicked",
236 G_CALLBACK (go_home_cb), NULL);
237 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
239 /* URL entry */
240 item = gtk_tool_item_new ();
241 gtk_tool_item_set_expand (item, TRUE);
242 uri_entry = gtk_entry_new ();
243 gtk_container_add (GTK_CONTAINER (item), uri_entry);
244 g_signal_connect (G_OBJECT (uri_entry), "activate",
245 G_CALLBACK (uri_entry_cb), NULL);
246 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
248 /* Separator */
249 item = gtk_separator_tool_item_new ();
250 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
252 /* Search entry */
253 item = gtk_tool_item_new ();
254 search_entry = gtk_entry_new ();
255 gtk_container_add (GTK_CONTAINER (item), search_entry);
256 g_signal_connect (G_OBJECT (search_entry), "activate",
257 G_CALLBACK (search_entry_cb), NULL);
258 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
260 /* The Fullscreen button */
261 item = gtk_tool_button_new_from_stock (GTK_STOCK_FULLSCREEN);
262 g_signal_connect (G_OBJECT (item), "clicked",
263 G_CALLBACK (fullscreen_cb), NULL);
264 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
266 return toolbar;
267 }
269 /* Main window */
270 static GtkWidget*
271 create_window ()
272 {
273 GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
274 GtkWidget* vbox = gtk_vbox_new (FALSE, 0);
276 gtk_box_pack_start (GTK_BOX (vbox), create_browser (), TRUE, TRUE, 0);
277 gtk_box_pack_start (GTK_BOX (vbox), create_toolbar (), FALSE, FALSE, 0);
279 /* Default TazWeb window size ratio to 3/4 --> 720, 540*/
280 gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
281 gtk_window_set_icon (GTK_WINDOW (window),
282 create_pixbuf ("/usr/share/pixmaps/tazweb.png"));
283 gtk_widget_set_name (window, "TazWeb");
284 g_signal_connect (window, "destroy", G_CALLBACK (destroy_cb), NULL);
285 gtk_container_add (GTK_CONTAINER (window), vbox);
287 main_window = window;
288 gtk_widget_show_all (main_window);
289 }
291 int
292 main (int argc, char* argv[])
293 {
294 gtk_init (&argc, &argv);
295 if (!g_thread_supported ())
296 g_thread_init (NULL);
298 get_config ();
299 create_window ();
301 /* Start page url or file */
302 uri = (gchar*) (argc > 1 ? argv[1] :
303 "file:///usr/share/webhome/index.html");
305 webkit_web_view_load_uri (web_view, uri);
306 gtk_widget_grab_focus (GTK_WIDGET (web_view));
308 gtk_main ();
309 return 0;
310 }