tazweb view src/with-toolbar.c @ rev 12

Small code clean
author Christophe Lincoln <pankso@slitaz.org>
date Mon Apr 04 17:31:19 2011 +0200 (2011-04-04)
parents 181b77ec9d9f
children
line source
1 /*
2 * This is a version of TazWeb with a simple toolbar with home,
3 * go-back, go-next and URL entry
4 *
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 GtkWidget* uri_entry;
16 static WebKitWebView* web_view;
17 static gchar* main_title;
18 static gdouble load_progress;
19 static guint status_context_id;
21 static void
22 activate_uri_entry_cb (GtkWidget* entry, gpointer data)
23 {
24 const gchar* uri = gtk_entry_get_text (GTK_ENTRY (entry));
25 g_assert (uri);
26 webkit_web_view_load_uri (web_view, uri);
27 }
29 /* Page title to window title */
30 static void
31 update_title (GtkWindow* window)
32 {
33 GString* string = g_string_new (main_title);
34 g_string_append (string, " - TazWeb");
35 if (load_progress < 100)
36 g_string_append_printf (string, " (%f%%)", load_progress);
37 gchar* title = g_string_free (string, FALSE);
38 gtk_window_set_title (window, title);
39 g_free (title);
40 }
42 static void
43 notify_title_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
44 {
45 if (main_title)
46 g_free (main_title);
47 main_title = g_strdup (webkit_web_view_get_title(web_view));
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 /* Request progress in window title */
63 static void
64 notify_progress_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
65 {
66 load_progress = webkit_web_view_get_progress (web_view) * 100;
67 update_title (GTK_WINDOW (main_window));
68 }
70 static void
71 destroy_cb (GtkWidget* widget, gpointer data)
72 {
73 gtk_main_quit ();
74 }
76 /* Go Home function */
77 static void
78 go_home (GtkWidget* widget, gpointer data)
79 {
80 const gchar* uri = ("file:///usr/share/webhome/index.html");
81 g_assert (uri);
82 webkit_web_view_load_uri (web_view, uri);
83 }
85 /* TazPanel function */
86 static void
87 slitaz_panel (GtkWidget* widget, gpointer data)
88 {
89 const gchar* uri = ("http://tazpanel:8080/");
90 g_assert (uri);
91 webkit_web_view_load_uri (web_view, uri);
92 }
94 /* TazWeb doc function */
95 static void
96 tazweb_doc (GtkWidget* widget, gpointer data)
97 {
98 const gchar* uri = ("file:///usr/share/doc/slitaz/tazweb.html");
99 g_assert (uri);
100 webkit_web_view_load_uri (web_view, uri);
101 }
103 static void
104 go_back_cb (GtkWidget* widget, gpointer data)
105 {
106 webkit_web_view_go_back (web_view);
107 }
109 static void
110 go_forward_cb (GtkWidget* widget, gpointer data)
111 {
112 webkit_web_view_go_forward (web_view);
113 }
115 static GtkWidget*
116 create_browser ()
117 {
118 GtkWidget* scrolled_window = gtk_scrolled_window_new (NULL, NULL);
119 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
121 web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
122 gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (web_view));
124 g_signal_connect (web_view, "notify::title", G_CALLBACK (notify_title_cb), web_view);
125 g_signal_connect (web_view, "notify::load-status", G_CALLBACK (notify_load_status_cb), web_view);
126 g_signal_connect (web_view, "notify::progress", G_CALLBACK (notify_progress_cb), web_view);
128 return scrolled_window;
129 }
131 static GtkWidget*
132 create_toolbar ()
133 {
134 GtkWidget* toolbar = gtk_toolbar_new ();
136 gtk_toolbar_set_orientation (GTK_TOOLBAR (toolbar), GTK_ORIENTATION_HORIZONTAL);
137 gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_BOTH_HORIZ);
139 GtkToolItem* item;
141 /* The Home button */
142 item = gtk_tool_button_new_from_stock (GTK_STOCK_HOME);
143 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (go_home), NULL);
144 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
146 /* The TazPanel button */
147 item = gtk_tool_button_new_from_stock (GTK_STOCK_PREFERENCES);
148 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (slitaz_panel), NULL);
149 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
151 /* The back button */
152 /*
153 * item = gtk_tool_button_new_from_stock (GTK_STOCK_GO_BACK);
154 * g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (go_back_cb), NULL);
155 * gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
156 */
158 /* The forward button */
159 /*
160 * item = gtk_tool_button_new_from_stock (GTK_STOCK_GO_FORWARD);
161 * g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (go_forward_cb), NULL);
162 * gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
163 */
165 /* The URL entry */
166 item = gtk_tool_item_new ();
167 gtk_tool_item_set_expand (item, TRUE);
168 uri_entry = gtk_entry_new ();
169 gtk_container_add (GTK_CONTAINER (item), uri_entry);
170 g_signal_connect (G_OBJECT (uri_entry), "activate", G_CALLBACK (activate_uri_entry_cb), NULL);
171 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
173 /* The TazWeb doc button */
174 item = gtk_tool_button_new_from_stock (GTK_STOCK_INFO);
175 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (tazweb_doc), NULL);
176 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
178 return toolbar;
179 }
181 /* Create an icon */
182 static GdkPixbuf*
183 create_pixbuf (const gchar * image)
184 {
185 GdkPixbuf *pixbuf;
186 pixbuf = gdk_pixbuf_new_from_file (image, NULL);
188 return pixbuf;
189 }
191 static GtkWidget*
192 create_window ()
193 {
194 GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
195 /* Default tazweb window size ratio to 3/4 --> 720, 540 */
196 gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
197 gtk_window_set_icon (GTK_WINDOW (window),
198 create_pixbuf ("/usr/share/pixmaps/tazweb.png"));
199 gtk_widget_set_name (window, "TazWeb");
200 g_signal_connect (window, "destroy", G_CALLBACK (destroy_cb), NULL);
202 return window;
203 }
205 int
206 main (int argc, char* argv[])
207 {
208 gtk_init (&argc, &argv);
209 if (!g_thread_supported ())
210 g_thread_init (NULL);
212 GtkWidget* vbox = gtk_vbox_new (FALSE, 0);
213 gtk_box_pack_start (GTK_BOX (vbox), create_toolbar (), FALSE, FALSE, 0);
214 gtk_box_pack_start (GTK_BOX (vbox), create_browser (), TRUE, TRUE, 0);
216 main_window = create_window ();
217 gtk_container_add (GTK_CONTAINER (main_window), vbox);
219 gchar* uri = (gchar*) (argc > 1 ? argv[1] : "file:///usr/share/webhome/index.html");
220 webkit_web_view_load_uri (web_view, uri);
222 gtk_widget_grab_focus (GTK_WIDGET (web_view));
223 gtk_widget_show_all (main_window);
224 gtk_main ();
226 return 0;
227 }