tazweb view src/main.c @ rev 66

Uniformize gchar* and small fix
author Christophe Lincoln <pankso@slitaz.org>
date Fri Apr 22 09:39:16 2011 +0200 (2011-04-22)
parents 0b5ed969f4f2
children 72220b3cf20b
line source
1 /*
2 * TazWeb is a radically simple web browser providing a single window
3 * with a single toolbar with buttons, an URL entry and search as well
4 * as a contextual menu, but no menu bar or tabs.
5 *
6 * Copyright (C) 2011 SliTaz GNU/Linux - BSD License
7 * See AUTHORS and LICENSE for detailed information
8 *
9 */
11 #include <gtk/gtk.h>
12 #include <webkit/webkit.h>
14 #define CONFIG g_strdup_printf ("%s/.config/tazweb", g_get_home_dir ())
15 #define START "file:///usr/share/webhome/index.html"
17 /* Loader color - #d66018 #7b705c */
18 static gchar *loader_color = "#351a0a";
20 static GtkWidget *main_window, *scrolled, *loader, *toolbar;
21 static GtkWidget *uri_entry, *search_entry;
22 static WebKitWebView* web_view;
23 static WebKitWebFrame* frame;
24 static gdouble load_progress;
25 static guint status_context_id;
26 static gchar* main_title;
27 static gchar* title;
28 static gint progress;
29 static gint loader_width;
30 const gchar* uri;
32 /* Create an icon */
33 static GdkPixbuf*
34 create_pixbuf (const gchar* image)
35 {
36 GdkPixbuf *pixbuf;
37 pixbuf = gdk_pixbuf_new_from_file (image, NULL);
38 return pixbuf;
39 }
41 /* Can be: http://hg.slitaz.org or hg.slitaz.org */
42 static void
43 check_requested_uri ()
44 {
45 uri = g_strrstr (uri, "://") ? g_strdup (uri)
46 : g_strdup_printf ("http://%s", uri);
47 }
49 /* Loader area */
50 static void
51 draw_loader ()
52 {
53 GdkGC* gc = gdk_gc_new (loader->window);
54 GdkColor fg;
56 uri = webkit_web_view_get_uri (web_view);
57 loader_width = progress * loader->allocation.width / 100;
59 gdk_color_parse (loader_color, &fg);
60 gdk_gc_set_rgb_fg_color (gc, &fg);
61 gdk_draw_rectangle (loader->window,
62 loader->style->bg_gc [GTK_WIDGET_STATE (loader)],
63 TRUE, 0, 0, loader->allocation.width, loader->allocation.height);
64 gdk_draw_rectangle (loader->window, gc, TRUE, 0, 0, loader_width,
65 loader->allocation.height);
66 g_object_unref (gc);
67 }
69 /* Loader progress */
70 static gboolean
71 expose_loader_cb (GtkWidget *loader, GdkEventExpose *event, gpointer data)
72 {
73 draw_loader ();
74 return TRUE;
75 }
77 /* Update title and loader */
78 static void
79 update ()
80 {
81 title = g_strdup (main_title);
82 if (! main_title)
83 title = g_strdup_printf ("Unknow - TazWeb", main_title);
84 draw_loader ();
85 gtk_window_set_title (GTK_WINDOW (main_window), title);
86 g_free (title);
87 }
89 /* Get the page title */
90 static void
91 notify_title_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
92 {
93 main_title = g_strdup (webkit_web_view_get_title (web_view));
94 update ();
95 }
97 /* Request progress in window title */
98 static void
99 notify_progress_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
100 {
101 progress = webkit_web_view_get_progress (web_view) * 100;
102 update ();
103 }
105 /* Notify loader and url entry */
106 static void
107 notify_load_status_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
108 {
109 switch (webkit_web_view_get_load_status (web_view))
110 {
111 case WEBKIT_LOAD_COMMITTED:
112 break;
113 case WEBKIT_LOAD_FINISHED:
114 progress = 0;
115 update ();
116 break;
117 }
118 frame = webkit_web_view_get_main_frame (web_view);
119 uri = webkit_web_frame_get_uri (frame);
120 if (uri)
121 gtk_entry_set_text (GTK_ENTRY (uri_entry), uri);
122 }
124 static void
125 destroy_cb (GtkWidget* widget, gpointer data)
126 {
127 gtk_main_quit ();
128 }
130 /* Show page source */
131 static void
132 view_source_cb ()
133 {
134 gboolean source;
136 frame = webkit_web_view_get_main_frame (web_view);
137 uri = webkit_web_frame_get_uri (frame);
138 source = webkit_web_view_get_view_source_mode (web_view);
140 webkit_web_view_set_view_source_mode(web_view, !source);
141 webkit_web_view_load_uri (web_view, uri);
142 }
144 /* URL entry callback function */
145 static void
146 uri_entry_cb (GtkWidget* entry, gpointer data)
147 {
148 uri = gtk_entry_get_text (GTK_ENTRY (entry));
149 g_assert (uri);
150 check_requested_uri ();
151 webkit_web_view_load_uri (web_view, uri);
152 }
154 /* Search entry callback function */
155 static void
156 search_entry_cb (GtkWidget* entry, gpointer data)
157 {
158 uri = g_strdup_printf ("http://www.google.com/search?q=%s",
159 gtk_entry_get_text (GTK_ENTRY (entry)));
160 g_assert (uri);
161 webkit_web_view_load_uri (web_view, uri);
162 }
164 /* Home button callback function */
165 static void
166 go_home_cb (GtkWidget* widget, gpointer data)
167 {
168 uri = g_strdup_printf ("file://%s/home.html", CONFIG);
169 g_assert (uri);
170 webkit_web_view_load_uri (web_view, uri);
171 }
173 /* Fullscreen and unfullscreen callback function */
174 static void
175 fullscreen_cb (GtkWindow* window, gpointer data)
176 {
177 GdkWindowState state;
178 state = gdk_window_get_state (gtk_widget_get_window (GTK_WIDGET (main_window)));
180 if (state & GDK_WINDOW_STATE_FULLSCREEN)
181 gtk_window_unfullscreen (GTK_WINDOW (main_window));
182 else
183 gtk_window_fullscreen (GTK_WINDOW (main_window));
184 }
186 /* TazWeb doc callback function */
187 static void
188 tazweb_doc_cb (GtkWidget* widget, gpointer data)
189 {
190 uri = ("file:///usr/share/doc/tazweb/tazweb.html");
191 g_assert (uri);
192 webkit_web_view_load_uri (web_view, uri);
193 }
195 /* Download function */
196 static gboolean
197 download_requested_cb (WebKitWebView *web_view, WebKitDownload *download,
198 gpointer user_data)
199 {
200 uri = webkit_download_get_uri (download);
201 const gchar* buffer;
202 asprintf (&buffer, "tazbox dl-out %s", uri);
203 system (buffer);
204 }
206 /* Zoom ou and in callback function */
207 static void
208 zoom_out_cb (GtkWidget *main_window)
209 {
210 webkit_web_view_zoom_out (web_view);
211 }
213 static void
214 zoom_in_cb (GtkWidget *main_window)
215 {
216 webkit_web_view_zoom_in (web_view);
217 }
219 /* Add items to WebKit contextual menu */
220 static void
221 populate_menu_cb (WebKitWebView *web_view, GtkMenu *menu, gpointer data)
222 {
223 GtkWidget* item;
225 /* Separator */
226 item = gtk_separator_menu_item_new ();
227 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
229 /* Zoom in */
230 item = gtk_image_menu_item_new_with_label ("Zoom in");
231 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
232 gtk_image_new_from_stock (GTK_STOCK_ZOOM_IN, GTK_ICON_SIZE_MENU));
233 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
234 g_signal_connect (item, "activate", G_CALLBACK (zoom_in_cb), NULL);
236 /* Zoom out */
237 item = gtk_image_menu_item_new_with_label ("Zoom out");
238 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
239 gtk_image_new_from_stock (GTK_STOCK_ZOOM_OUT, GTK_ICON_SIZE_MENU));
240 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
241 g_signal_connect (item, "activate", G_CALLBACK (zoom_out_cb), NULL);
243 /* Separator */
244 item = gtk_separator_menu_item_new ();
245 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
247 /* TazWeb documentation */
248 item = gtk_image_menu_item_new_with_label ("TazWeb manual");
249 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
250 gtk_image_new_from_stock (GTK_STOCK_INFO, GTK_ICON_SIZE_MENU));
251 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
252 g_signal_connect (item, "activate", G_CALLBACK (tazweb_doc_cb), NULL);
254 /* View source mode */
255 item = gtk_image_menu_item_new_with_label ("View source mode");
256 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
257 gtk_image_new_from_stock (GTK_STOCK_PROPERTIES, GTK_ICON_SIZE_MENU));
258 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
259 g_signal_connect (item, "activate", G_CALLBACK (view_source_cb), NULL);
261 gtk_widget_show_all (GTK_WIDGET (menu));
262 }
264 /* Scrolled window for the web_view */
265 static GtkWidget*
266 create_browser ()
267 {
268 scrolled = gtk_scrolled_window_new (NULL, NULL);
269 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
270 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
272 web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
273 gtk_container_add (GTK_CONTAINER (scrolled), GTK_WIDGET (web_view));
275 /* Connect events */
276 g_signal_connect (web_view, "notify::title",
277 G_CALLBACK (notify_title_cb), web_view);
278 g_signal_connect (web_view, "notify::progress",
279 G_CALLBACK (notify_progress_cb), web_view);
280 g_signal_connect (web_view, "notify::load-status",
281 G_CALLBACK (notify_load_status_cb), web_view);
282 g_signal_connect (web_view, "download-requested",
283 G_CALLBACK (download_requested_cb), NULL);
285 /* Connect WebKit contextual menu items */
286 g_object_connect (G_OBJECT (web_view), "signal::populate-popup",
287 G_CALLBACK (populate_menu_cb), web_view, NULL);
289 return scrolled;
290 }
292 /* Loader area */
293 static GtkWidget*
294 create_loader ()
295 {
296 loader = gtk_drawing_area_new ();
297 gtk_widget_set_size_request (loader, 0, 1);
298 g_signal_connect (G_OBJECT (loader), "expose_event",
299 G_CALLBACK (expose_loader_cb), NULL);
301 return loader;
302 }
304 static GtkWidget*
305 create_toolbar ()
306 {
307 GtkToolItem* item;
309 toolbar = gtk_toolbar_new ();
310 gtk_widget_set_size_request (toolbar, 0, 33);
311 gtk_toolbar_set_orientation (GTK_TOOLBAR (toolbar),
312 GTK_ORIENTATION_HORIZONTAL);
313 gtk_toolbar_set_style (GTK_TOOLBAR (toolbar),
314 GTK_TOOLBAR_BOTH_HORIZ);
316 /* Home button */
317 item = gtk_tool_button_new_from_stock (GTK_STOCK_HOME);
318 g_signal_connect (G_OBJECT (item), "clicked",
319 G_CALLBACK (go_home_cb), NULL);
320 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
322 /* URL entry */
323 item = gtk_tool_item_new ();
324 gtk_tool_item_set_expand (item, TRUE);
325 uri_entry = gtk_entry_new ();
326 gtk_container_add (GTK_CONTAINER (item), uri_entry);
327 g_signal_connect (G_OBJECT (uri_entry), "activate",
328 G_CALLBACK (uri_entry_cb), NULL);
329 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
331 /* Separator */
332 item = gtk_separator_tool_item_new ();
333 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
335 /* Search entry */
336 item = gtk_tool_item_new ();
337 search_entry = gtk_entry_new ();
338 gtk_container_add (GTK_CONTAINER (item), search_entry);
339 g_signal_connect (G_OBJECT (search_entry), "activate",
340 G_CALLBACK (search_entry_cb), NULL);
341 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
343 /* The Fullscreen button */
344 item = gtk_tool_button_new_from_stock (GTK_STOCK_FULLSCREEN);
345 g_signal_connect (G_OBJECT (item), "clicked",
346 G_CALLBACK (fullscreen_cb), NULL);
347 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
349 return toolbar;
350 }
352 /* Main window */
353 static GtkWidget*
354 create_window ()
355 {
356 GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
357 GtkWidget* vbox = gtk_vbox_new (FALSE, 0);
359 /* Default TazWeb window size ratio to 3/4 --> 720, 540*/
360 gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
361 gtk_window_set_icon (GTK_WINDOW (window),
362 create_pixbuf ("/usr/share/pixmaps/tazweb.png"));
363 gtk_widget_set_name (window, "TazWeb");
364 g_signal_connect (window, "destroy", G_CALLBACK (destroy_cb), NULL);
366 /* Pack box and container */
367 gtk_box_pack_start (GTK_BOX (vbox), create_browser (), TRUE, TRUE, 0);
368 gtk_container_add (GTK_CONTAINER (vbox), create_loader());
369 gtk_box_set_child_packing (GTK_BOX (vbox), loader,
370 FALSE, FALSE, 0, GTK_PACK_START);
371 gtk_box_pack_start (GTK_BOX (vbox), create_toolbar (), FALSE, FALSE, 0);
372 gtk_container_add (GTK_CONTAINER (window), vbox);
374 main_window = window;
375 gtk_widget_show_all (main_window);
376 }
378 int
379 main (int argc, char* argv[])
380 {
381 gtk_init (&argc, &argv);
382 if (!g_thread_supported ())
383 g_thread_init (NULL);
385 /* Get a default home.html if missing */
386 if (! g_file_test (CONFIG, G_FILE_TEST_EXISTS))
387 system ("cp -r /usr/share/tazweb $HOME/.config/tazweb");
389 /* Load the start page file or the url in argument */
390 uri = (char*) (argc > 1 ? argv[1] : START);
391 if (argv[1])
392 check_requested_uri ();
395 create_window ();
396 webkit_web_view_load_uri (web_view, uri);
397 gtk_widget_grab_focus (GTK_WIDGET (web_view));
398 gtk_main ();
400 return 0;
401 }