tazweb view src/main.c @ rev 68

Connect create-web-view (right click and open in new window)but still dont do the job
author Christophe Lincoln <pankso@slitaz.org>
date Fri Apr 22 19:29:12 2011 +0200 (2011-04-22)
parents 72220b3cf20b
children 8961264a6475
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, GtkWindow* window)
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,
203 "xterm -T 'Download' -geom 72x10+0-24 -e 'cd $HOME/Downloads && \
204 wget -c %s; sleep 2' &", uri);
205 system (buffer);
206 }
208 /* Zoom ou and in callback function */
209 static void
210 zoom_out_cb (GtkWidget *main_window)
211 {
212 webkit_web_view_zoom_out (web_view);
213 }
215 static void
216 zoom_in_cb (GtkWidget *main_window)
217 {
218 webkit_web_view_zoom_in (web_view);
219 }
221 /* Add items to WebKit contextual menu */
222 static void
223 populate_menu_cb (WebKitWebView *web_view, GtkMenu *menu, gpointer data)
224 {
225 GtkWidget* item;
227 /* Separator */
228 item = gtk_separator_menu_item_new ();
229 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
231 /* Zoom in */
232 item = gtk_image_menu_item_new_with_label ("Zoom in");
233 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
234 gtk_image_new_from_stock (GTK_STOCK_ZOOM_IN, GTK_ICON_SIZE_MENU));
235 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
236 g_signal_connect (item, "activate", G_CALLBACK (zoom_in_cb), NULL);
238 /* Zoom out */
239 item = gtk_image_menu_item_new_with_label ("Zoom out");
240 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
241 gtk_image_new_from_stock (GTK_STOCK_ZOOM_OUT, GTK_ICON_SIZE_MENU));
242 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
243 g_signal_connect (item, "activate", G_CALLBACK (zoom_out_cb), NULL);
245 /* Separator */
246 item = gtk_separator_menu_item_new ();
247 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
249 /* TazWeb documentation */
250 item = gtk_image_menu_item_new_with_label ("TazWeb manual");
251 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
252 gtk_image_new_from_stock (GTK_STOCK_INFO, GTK_ICON_SIZE_MENU));
253 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
254 g_signal_connect (item, "activate", G_CALLBACK (tazweb_doc_cb), NULL);
256 /* View source mode */
257 item = gtk_image_menu_item_new_with_label ("View source mode");
258 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
259 gtk_image_new_from_stock (GTK_STOCK_PROPERTIES, GTK_ICON_SIZE_MENU));
260 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
261 g_signal_connect (item, "activate", G_CALLBACK (view_source_cb), NULL);
263 gtk_widget_show_all (GTK_WIDGET (menu));
264 }
266 /* Open in a new window from menu */
267 static WebKitWebView*
268 create_web_view_cb (WebKitWebView* web_view, GtkWidget* window)
269 {
270 return WEBKIT_WEB_VIEW (web_view);
271 }
273 /* Scrolled window for the web_view */
274 static GtkWidget*
275 create_browser ()
276 {
277 scrolled = gtk_scrolled_window_new (NULL, NULL);
278 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
279 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
281 web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
282 gtk_container_add (GTK_CONTAINER (scrolled), GTK_WIDGET (web_view));
284 /* Connect events */
285 g_signal_connect (web_view, "notify::title",
286 G_CALLBACK (notify_title_cb), web_view);
287 g_signal_connect (web_view, "notify::progress",
288 G_CALLBACK (notify_progress_cb), web_view);
289 g_signal_connect (web_view, "notify::load-status",
290 G_CALLBACK (notify_load_status_cb), web_view);
291 g_signal_connect (web_view, "download-requested",
292 G_CALLBACK (download_requested_cb), NULL);
293 g_signal_connect (web_view, "create-web-view",
294 G_CALLBACK (create_web_view_cb), web_view);
296 /* Connect WebKit contextual menu items */
297 g_object_connect (G_OBJECT (web_view), "signal::populate-popup",
298 G_CALLBACK (populate_menu_cb), web_view, NULL);
300 return scrolled;
301 }
303 /* Loader area */
304 static GtkWidget*
305 create_loader ()
306 {
307 loader = gtk_drawing_area_new ();
308 gtk_widget_set_size_request (loader, 0, 2);
309 g_signal_connect (G_OBJECT (loader), "expose_event",
310 G_CALLBACK (expose_loader_cb), NULL);
312 return loader;
313 }
315 static GtkWidget*
316 create_toolbar ()
317 {
318 GtkToolItem* item;
320 toolbar = gtk_toolbar_new ();
321 gtk_widget_set_size_request (toolbar, 0, 33);
322 gtk_toolbar_set_orientation (GTK_TOOLBAR (toolbar),
323 GTK_ORIENTATION_HORIZONTAL);
324 gtk_toolbar_set_style (GTK_TOOLBAR (toolbar),
325 GTK_TOOLBAR_BOTH_HORIZ);
327 /* Home button */
328 item = gtk_tool_button_new_from_stock (GTK_STOCK_HOME);
329 g_signal_connect (G_OBJECT (item), "clicked",
330 G_CALLBACK (go_home_cb), NULL);
331 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
333 /* URL entry */
334 item = gtk_tool_item_new ();
335 gtk_tool_item_set_expand (item, TRUE);
336 uri_entry = gtk_entry_new ();
337 gtk_container_add (GTK_CONTAINER (item), uri_entry);
338 g_signal_connect (G_OBJECT (uri_entry), "activate",
339 G_CALLBACK (uri_entry_cb), NULL);
340 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
342 /* Separator */
343 item = gtk_separator_tool_item_new ();
344 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
346 /* Search entry */
347 item = gtk_tool_item_new ();
348 search_entry = gtk_entry_new ();
349 gtk_container_add (GTK_CONTAINER (item), search_entry);
350 g_signal_connect (G_OBJECT (search_entry), "activate",
351 G_CALLBACK (search_entry_cb), NULL);
352 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
354 /* The Fullscreen button */
355 item = gtk_tool_button_new_from_stock (GTK_STOCK_FULLSCREEN);
356 g_signal_connect (G_OBJECT (item), "clicked",
357 G_CALLBACK (fullscreen_cb), NULL);
358 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
360 return toolbar;
361 }
363 /* Main window */
364 static GtkWidget*
365 create_window ()
366 {
367 GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
368 GtkWidget* vbox = gtk_vbox_new (FALSE, 0);
370 /* Default TazWeb window size ratio to 3/4 --> 720, 540*/
371 gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
372 gtk_window_set_icon (GTK_WINDOW (window),
373 create_pixbuf ("/usr/share/pixmaps/tazweb.png"));
374 gtk_widget_set_name (window, "TazWeb");
375 g_signal_connect (window, "destroy", G_CALLBACK (destroy_cb), NULL);
377 /* Pack box and container */
378 gtk_box_pack_start (GTK_BOX (vbox), create_browser (), TRUE, TRUE, 0);
379 gtk_container_add (GTK_CONTAINER (vbox), create_loader());
380 gtk_box_set_child_packing (GTK_BOX (vbox), loader,
381 FALSE, FALSE, 0, GTK_PACK_START);
382 gtk_box_pack_start (GTK_BOX (vbox), create_toolbar (), FALSE, FALSE, 0);
383 gtk_container_add (GTK_CONTAINER (window), vbox);
385 return window;
386 }
388 int
389 main (int argc, char* argv[])
390 {
391 gtk_init (&argc, &argv);
392 if (!g_thread_supported ())
393 g_thread_init (NULL);
395 /* Get a default home.html if missing */
396 if (! g_file_test (CONFIG, G_FILE_TEST_EXISTS))
397 system ("cp -r /usr/share/tazweb $HOME/.config/tazweb");
399 /* Load the start page file or the url in argument */
400 uri = (char*) (argc > 1 ? argv[1] : START);
401 if (argv[1])
402 check_requested_uri ();
404 main_window = create_window ();
405 gtk_widget_show_all (main_window);
406 webkit_web_view_load_uri (web_view, uri);
407 gtk_widget_grab_focus (GTK_WIDGET (web_view));
408 gtk_main ();
409 return 0;
410 }