tazweb view src/main.c @ rev 14

main.c: create a toolbar, 2 button and pack it at bottom window
author Christophe Lincoln <pankso@slitaz.org>
date Mon Apr 04 19:25:11 2011 +0200 (2011-04-04)
parents 2d3ce36d0c3d
children 8a95a53b519a
line source
1 /*
2 * TazWeb is a radicaly simple web browser providing a single window
3 * with no toolbar, menu or tabs.
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 /* Page title to window title */
22 static void
23 update_title (GtkWindow* window)
24 {
25 GString* string = g_string_new (main_title);
26 g_string_append (string, " - TazWeb");
27 if (load_progress < 100)
28 g_string_append_printf (string, " (%f%%)", load_progress);
29 gchar* title = g_string_free (string, FALSE);
30 gtk_window_set_title (window, title);
31 g_free (title);
32 }
34 static void
35 notify_title_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
36 {
37 if (main_title)
38 g_free (main_title);
39 main_title = g_strdup (webkit_web_view_get_title(web_view));
40 update_title (GTK_WINDOW (main_window));
41 }
43 /* Request progress in window title */
44 static void
45 notify_progress_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
46 {
47 load_progress = webkit_web_view_get_progress (web_view) * 100;
48 update_title (GTK_WINDOW (main_window));
49 }
51 static void
52 destroy_cb (GtkWidget* widget, gpointer data)
53 {
54 gtk_main_quit ();
55 }
57 /* Home button function */
58 static void
59 go_home (GtkWidget* widget, gpointer data)
60 {
61 const gchar* uri = ("file:///usr/share/webhome/index.html");
62 g_assert (uri);
63 webkit_web_view_load_uri (web_view, uri);
64 }
66 /* TazWeb doc function */
67 static void
68 tazweb_doc (GtkWidget* widget, gpointer data)
69 {
70 const gchar* uri = ("file:///usr/share/doc/slitaz/tazweb.html");
71 g_assert (uri);
72 webkit_web_view_load_uri (web_view, uri);
73 }
75 static GtkWidget*
76 create_browser ()
77 {
78 GtkWidget* scrolled_window = gtk_scrolled_window_new (NULL, NULL);
79 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
81 web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
82 gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (web_view));
84 g_signal_connect (web_view, "notify::title", G_CALLBACK (notify_title_cb), web_view);
85 g_signal_connect (web_view, "notify::progress", G_CALLBACK (notify_progress_cb), web_view);
87 return scrolled_window;
88 }
90 static GtkWidget*
91 create_toolbar ()
92 {
93 GtkWidget* toolbar = gtk_toolbar_new ();
95 gtk_toolbar_set_orientation (GTK_TOOLBAR (toolbar), GTK_ORIENTATION_HORIZONTAL);
96 gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_BOTH_HORIZ);
98 GtkToolItem* item;
100 /* The Home button */
101 item = gtk_tool_button_new_from_stock (GTK_STOCK_HOME);
102 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (go_home), NULL);
103 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
105 /* Expand to have help icon on the right */
106 item = gtk_tool_item_new ();
107 gtk_tool_item_set_expand (item, TRUE);
108 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
110 /* The TazWeb doc button */
111 item = gtk_tool_button_new_from_stock (GTK_STOCK_INFO);
112 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (tazweb_doc), NULL);
113 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
115 return toolbar;
116 }
118 /* Create an icon */
119 static GdkPixbuf*
120 create_pixbuf (const gchar * image)
121 {
122 GdkPixbuf *pixbuf;
123 pixbuf = gdk_pixbuf_new_from_file (image, NULL);
125 return pixbuf;
126 }
128 static GtkWidget*
129 create_window ()
130 {
131 GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
132 /* Default tazweb window size ratio to 3/4 ?? --> 720, 540*/
133 gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
134 gtk_window_set_icon (GTK_WINDOW (window),
135 create_pixbuf ("/usr/share/pixmaps/tazweb.png"));
136 gtk_widget_set_name (window, "TazWeb");
137 g_signal_connect (window, "destroy", G_CALLBACK (destroy_cb), NULL);
139 return window;
140 }
142 int
143 main (int argc, char* argv[])
144 {
145 gtk_init (&argc, &argv);
146 if (!g_thread_supported ())
147 g_thread_init (NULL);
149 GtkWidget* vbox = gtk_vbox_new (FALSE, 0);
150 gtk_box_pack_start (GTK_BOX (vbox), create_browser (), TRUE, TRUE, 0);
151 gtk_box_pack_start (GTK_BOX (vbox), create_toolbar (), FALSE, FALSE, 0);
153 main_window = create_window ();
154 gtk_container_add (GTK_CONTAINER (main_window), vbox);
156 /* Home page url or file */
157 gchar* uri = (gchar*) (argc > 1 ? argv[1] : "file:///usr/share/webhome/index.html");
158 webkit_web_view_load_uri (web_view, uri);
160 gtk_widget_grab_focus (GTK_WIDGET (web_view));
161 gtk_widget_show_all (main_window);
162 gtk_main ();
164 return 0;
165 }