tazweb view src/main.c @ rev 57

Add populate_menu_cb and connection to have custom contextual menu items
author Christophe Lincoln <pankso@slitaz.org>
date Fri Apr 22 01:43:51 2011 +0200 (2011-04-22)
parents 2710f66a57e9
children ae341ad49c1b
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, *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 activate_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 activate_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 /* View source mode */
177 item = gtk_image_menu_item_new_with_label ("View source");
178 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
179 gtk_image_new_from_stock (GTK_STOCK_PROPERTIES, GTK_ICON_SIZE_MENU));
180 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
181 g_signal_connect (item, "activate", G_CALLBACK (view_source_cb), NULL);
183 gtk_widget_show_all (GTK_WIDGET (menu));
184 }
186 static GtkWidget*
187 create_browser ()
188 {
189 GtkWidget* scrolled_window = gtk_scrolled_window_new (NULL, NULL);
190 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
191 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
193 web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
194 gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (web_view));
196 /* Connect events */
197 g_signal_connect (web_view, "notify::title",
198 G_CALLBACK (notify_title_cb), web_view);
199 g_signal_connect (web_view, "notify::progress",
200 G_CALLBACK (notify_progress_cb), web_view);
201 g_signal_connect (web_view, "notify::load-status",
202 G_CALLBACK (notify_load_status_cb), web_view);
203 g_signal_connect (web_view, "download-requested",
204 G_CALLBACK (download_requested_cb), NULL);
206 /* Connect WebKit contextual menu items */
207 g_object_connect (G_OBJECT (web_view), "signal::populate-popup",
208 G_CALLBACK (populate_menu_cb), web_view, NULL);
210 return scrolled_window;
211 }
213 static GtkWidget*
214 create_toolbar ()
215 {
216 GtkToolItem* item;
218 GtkWidget* toolbar = gtk_toolbar_new ();
219 gtk_widget_set_size_request (toolbar, 0, 31);
220 gtk_toolbar_set_orientation (GTK_TOOLBAR (toolbar),
221 GTK_ORIENTATION_HORIZONTAL);
222 gtk_toolbar_set_style (GTK_TOOLBAR (toolbar),
223 GTK_TOOLBAR_BOTH_HORIZ);
225 /* Home button */
226 item = gtk_tool_button_new_from_stock (GTK_STOCK_HOME);
227 g_signal_connect (G_OBJECT (item), "clicked",
228 G_CALLBACK (go_home_cb), NULL);
229 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
231 /* URL entry */
232 item = gtk_tool_item_new ();
233 gtk_tool_item_set_expand (item, TRUE);
234 uri_entry = gtk_entry_new ();
235 gtk_container_add (GTK_CONTAINER (item), uri_entry);
236 g_signal_connect (G_OBJECT (uri_entry), "activate",
237 G_CALLBACK (activate_uri_entry_cb), NULL);
238 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
240 /* Separator */
241 item = gtk_separator_tool_item_new ();
242 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
244 /* Search entry */
245 item = gtk_tool_item_new ();
246 search_entry = gtk_entry_new ();
247 gtk_container_add (GTK_CONTAINER (item), search_entry);
248 g_signal_connect (G_OBJECT (search_entry), "activate",
249 G_CALLBACK (activate_search_entry_cb), NULL);
250 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
252 /* TazWeb doc button */
253 item = gtk_tool_button_new_from_stock (GTK_STOCK_INFO);
254 g_signal_connect (G_OBJECT (item), "clicked",
255 G_CALLBACK (tazweb_doc_cb), NULL);
256 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
258 /* The Fullscreen button */
259 item = gtk_tool_button_new_from_stock (GTK_STOCK_FULLSCREEN);
260 g_signal_connect (G_OBJECT (item), "clicked",
261 G_CALLBACK (fullscreen_cb), NULL);
262 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
264 return toolbar;
265 }
267 static GtkWidget*
268 create_window ()
269 {
270 GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
272 /* Default tazweb window size ratio to 3/4 --> 720, 540*/
273 gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
274 gtk_window_set_icon (GTK_WINDOW (window),
275 create_pixbuf ("/usr/share/pixmaps/tazweb.png"));
276 gtk_widget_set_name (window, "TazWeb");
277 g_signal_connect (window, "destroy", G_CALLBACK (destroy_cb), NULL);
279 return window;
280 }
282 int
283 main (int argc, char* argv[])
284 {
285 gtk_init (&argc, &argv);
286 if (!g_thread_supported ())
287 g_thread_init (NULL);
289 get_config ();
291 GtkWidget* vbox = gtk_vbox_new (FALSE, 0);
292 gtk_box_pack_start (GTK_BOX (vbox), create_browser (), TRUE, TRUE, 0);
293 gtk_box_pack_start (GTK_BOX (vbox), create_toolbar (), FALSE, FALSE, 0);
295 main_window = create_window ();
296 gtk_container_add (GTK_CONTAINER (main_window), vbox);
298 /* Start page url or file */
299 uri = (gchar*) (argc > 1 ? argv[1] :
300 "file:///usr/share/webhome/index.html");
302 webkit_web_view_load_uri (web_view, uri);
303 gtk_widget_grab_focus (GTK_WIDGET (web_view));
304 gtk_widget_show_all (main_window);
305 gtk_main ();
307 return 0;
308 }