tazweb rev 63
Remove loading progress from window title and use gtk_drawing_area_new to have a small progress bar
author | Christophe Lincoln <pankso@slitaz.org> |
---|---|
date | Fri Apr 22 08:48:23 2011 +0200 (2011-04-22) |
parents | 08c04b52a6c4 |
children | 79e6f575defb |
files | src/main.c |
line diff
1.1 --- a/src/main.c Fri Apr 22 07:04:49 2011 +0200 1.2 +++ b/src/main.c Fri Apr 22 08:48:23 2011 +0200 1.3 @@ -3,7 +3,7 @@ 1.4 * with a single toolbar with buttons, an URL entry and search as well 1.5 * as a contextual menu, but no menu bar or tabs. 1.6 * 1.7 - * Copyright (C) 2011 SliTaz GNU/Linux <devel@slitaz.org> 1.8 + * Copyright (C) 2011 SliTaz GNU/Linux - BSD License 1.9 * See AUTHORS and LICENSE for detailed information 1.10 * 1.11 */ 1.12 @@ -13,13 +13,18 @@ 1.13 1.14 #define CONFIG g_strdup_printf ("%s/.config/tazweb", g_get_home_dir ()) 1.15 1.16 -static GtkWidget *main_window, *scrolled, *toolbar; 1.17 +/* Loader color - #d66018 #7b705c */ 1.18 +static gchar *loader_color = "#351a0a"; 1.19 + 1.20 +static GtkWidget *main_window, *scrolled, *loader, *toolbar; 1.21 static GtkWidget *uri_entry, *search_entry; 1.22 static WebKitWebView* web_view; 1.23 static WebKitWebFrame* frame; 1.24 static gdouble load_progress; 1.25 static guint status_context_id; 1.26 static gchar* main_title; 1.27 +static gchar *title; 1.28 +static gint progress; 1.29 const gchar* uri; 1.30 1.31 /* Create an icon */ 1.32 @@ -35,50 +40,83 @@ 1.33 static void 1.34 get_config () 1.35 { 1.36 - if (! g_file_test (CONFIG, G_FILE_TEST_EXISTS)) { 1.37 + if (! g_file_test (CONFIG, G_FILE_TEST_EXISTS)) 1.38 system ("cp -r /usr/share/tazweb $HOME/.config/tazweb"); 1.39 - } 1.40 } 1.41 1.42 -/* Page title to window title */ 1.43 +/* Loader area */ 1.44 static void 1.45 -update_title (GtkWindow* window) 1.46 +draw_loader_cb () 1.47 { 1.48 - GString* string = g_string_new (main_title); 1.49 - /* g_string_append (string, " - TazWeb"); */ 1.50 - if (load_progress < 100) 1.51 - g_string_append_printf (string, " [ %f%% ] ", load_progress); 1.52 - gchar* title = g_string_free (string, FALSE); 1.53 - gtk_window_set_title (window, title); 1.54 + GdkGC *gc = gdk_gc_new (loader->window); 1.55 + GdkColor fg; 1.56 + 1.57 + uri = webkit_web_view_get_uri (web_view); 1.58 + const gint width = progress * loader->allocation.width / 100; 1.59 + 1.60 + gdk_color_parse (loader_color, &fg); 1.61 + gdk_gc_set_rgb_fg_color (gc, &fg); 1.62 + gdk_draw_rectangle (loader->window, 1.63 + loader->style->bg_gc [GTK_WIDGET_STATE (loader)], 1.64 + TRUE, 0, 0, loader->allocation.width, loader->allocation.height); 1.65 + gdk_draw_rectangle (loader->window, gc, TRUE, 0, 0, width, 1.66 + loader->allocation.height); 1.67 + g_object_unref (gc); 1.68 +} 1.69 + 1.70 +/* Update title and loader */ 1.71 +static void 1.72 +update () 1.73 +{ 1.74 + title = g_strdup (main_title); 1.75 + if (! main_title) 1.76 + title = g_strdup_printf ("Unknow - TazWeb", main_title); 1.77 + draw_loader_cb (); 1.78 + gtk_window_set_title (GTK_WINDOW (main_window), title); 1.79 g_free (title); 1.80 } 1.81 1.82 +/* Get the page title */ 1.83 static void 1.84 -notify_title_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data) 1.85 +notify_title_cb (WebKitWebView *web_view, WebKitWebFrame *frame) 1.86 { 1.87 - if (main_title) 1.88 - g_free (main_title); 1.89 - main_title = g_strdup (webkit_web_view_get_title(web_view)); 1.90 - update_title (GTK_WINDOW (main_window)); 1.91 + main_title = g_strdup (webkit_web_view_get_title (web_view)); 1.92 + update (); 1.93 } 1.94 1.95 /* Request progress in window title */ 1.96 static void 1.97 notify_progress_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data) 1.98 { 1.99 - load_progress = webkit_web_view_get_progress (web_view) * 100; 1.100 - update_title (GTK_WINDOW (main_window)); 1.101 + progress = webkit_web_view_get_progress (web_view) * 100; 1.102 + update (); 1.103 } 1.104 1.105 +/* Loader progress */ 1.106 +static gboolean 1.107 +expose_loader_cb (GtkWidget *area, GdkEventExpose *event, gpointer data) 1.108 +{ 1.109 + draw_loader_cb(); 1.110 + return TRUE; 1.111 +} 1.112 + 1.113 +/* Notify loader and url entry */ 1.114 static void 1.115 notify_load_status_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data) 1.116 { 1.117 - if (webkit_web_view_get_load_status (web_view) == WEBKIT_LOAD_COMMITTED) { 1.118 - frame = webkit_web_view_get_main_frame (web_view); 1.119 - uri = webkit_web_frame_get_uri (frame); 1.120 + switch (webkit_web_view_get_load_status (web_view)) 1.121 + { 1.122 + case WEBKIT_LOAD_COMMITTED: 1.123 + break; 1.124 + case WEBKIT_LOAD_FINISHED: 1.125 + progress = 0; 1.126 + update (); 1.127 + break; 1.128 + } 1.129 + frame = webkit_web_view_get_main_frame (web_view); 1.130 + uri = webkit_web_frame_get_uri (frame); 1.131 if (uri) 1.132 gtk_entry_set_text (GTK_ENTRY (uri_entry), uri); 1.133 - } 1.134 } 1.135 1.136 static void 1.137 @@ -157,7 +195,7 @@ 1.138 gpointer user_data) 1.139 { 1.140 uri = webkit_download_get_uri (download); 1.141 - gchar *buffer; 1.142 + const gchar *buffer; 1.143 asprintf (&buffer, "tazbox dl-out %s", uri); 1.144 system (buffer); 1.145 } 1.146 @@ -248,13 +286,25 @@ 1.147 return scrolled; 1.148 } 1.149 1.150 +/* Loader area */ 1.151 +static GtkWidget* 1.152 +create_loader () 1.153 +{ 1.154 + loader = gtk_drawing_area_new (); 1.155 + gtk_widget_set_size_request (loader, 0, 1); 1.156 + g_signal_connect (G_OBJECT (loader), "expose_event", 1.157 + G_CALLBACK (expose_loader_cb), NULL); 1.158 + 1.159 + return loader; 1.160 +} 1.161 + 1.162 static GtkWidget* 1.163 create_toolbar () 1.164 { 1.165 GtkToolItem* item; 1.166 1.167 toolbar = gtk_toolbar_new (); 1.168 - gtk_widget_set_size_request (toolbar, 0, 31); 1.169 + gtk_widget_set_size_request (toolbar, 0, 33); 1.170 gtk_toolbar_set_orientation (GTK_TOOLBAR (toolbar), 1.171 GTK_ORIENTATION_HORIZONTAL); 1.172 gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), 1.173 @@ -302,16 +352,20 @@ 1.174 { 1.175 GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL); 1.176 GtkWidget* vbox = gtk_vbox_new (FALSE, 0); 1.177 - 1.178 - gtk_box_pack_start (GTK_BOX (vbox), create_browser (), TRUE, TRUE, 0); 1.179 - gtk_box_pack_start (GTK_BOX (vbox), create_toolbar (), FALSE, FALSE, 0); 1.180 - 1.181 + 1.182 /* Default TazWeb window size ratio to 3/4 --> 720, 540*/ 1.183 gtk_window_set_default_size (GTK_WINDOW (window), 800, 600); 1.184 gtk_window_set_icon (GTK_WINDOW (window), 1.185 create_pixbuf ("/usr/share/pixmaps/tazweb.png")); 1.186 gtk_widget_set_name (window, "TazWeb"); 1.187 g_signal_connect (window, "destroy", G_CALLBACK (destroy_cb), NULL); 1.188 + 1.189 + /* Pack box and caontainer */ 1.190 + gtk_box_pack_start (GTK_BOX (vbox), create_browser (), TRUE, TRUE, 0); 1.191 + gtk_container_add (GTK_CONTAINER (vbox), create_loader()); 1.192 + gtk_box_set_child_packing (GTK_BOX (vbox), loader, 1.193 + FALSE, FALSE, 0, GTK_PACK_START); 1.194 + gtk_box_pack_start (GTK_BOX (vbox), create_toolbar (), FALSE, FALSE, 0); 1.195 gtk_container_add (GTK_CONTAINER (window), vbox); 1.196 1.197 main_window = window;