tazweb rev 81
Make new webview definitly work! We can now open new window from menu. By the way, removed loader (buggy with new webviw) and bloated... remove colors (use gtk theme)
author | Christophe Lincoln <pankso@slitaz.org> |
---|---|
date | Mon Apr 25 01:17:19 2011 +0200 (2011-04-25) |
parents | 32ceefc08f61 |
children | 5b1045c7842b |
files | README src/main.c |
line diff
1.1 --- a/README Sun Apr 24 12:30:41 2011 +0200 1.2 +++ b/README Mon Apr 25 01:17:19 2011 +0200 1.3 @@ -36,27 +36,30 @@ 1.4 1.5 Install with 'make install' (PREFIX and DESTDIR are supported for packaging) 1.6 1.7 - 1.8 Coding notes 1.9 ------------ 1.10 1.11 - * To get the filename of a WebKitDownload 1.12 + * To get the filename of a WebKitDownload: 1.13 file = webkit_download_get_suggested_filename (download); 1.14 1.15 * Check mime-type before download (needs a callback function) 1.16 g_signal_connect (web_view, "mime-type-policy-decision-requested", 1.17 - G_CALLBACK (mime_type_decision_cb), webview); 1.18 + G_CALLBACK (mime_type_decision_cb), webview); 1.19 1.20 - * Get user config directory path in $HOME 1.21 + * Get user config directory path in $HOME: 1.22 config = g_strdup_printf ("%s/.config/tazweb", g_get_home_dir ()); 1.23 1.24 - * This code changes the browser user-agent 1.25 - static gchar *useragent = "TazWeb/1.0 (X11; Linux; U; en_US) AppleWebKit/531.2+"; 1.26 + * Change font of a widget: 1.27 + PangoFontDescription *font; 1.28 + font = pango_font_description_from_string("8"); 1.29 + gtk_widget_modify_font(urientry, font); 1.30 + pango_font_description_free(font); 1.31 + 1.32 + * Change size of a widget: 1.33 + gtk_widget_set_size_request(toolbar, 0, 28); 1.34 + 1.35 + * Append a string 1.36 + g_string_append(string, " - TazWeb"); 1.37 1.38 - /* User agent */ 1.39 - WebKitWebSettings *settings; 1.40 - settings = webkit_web_view_get_settings (webview); 1.41 - g_object_set (G_OBJECT (settings), "user-agent", useragent, NULL); 1.42 - 1.43 1.44 ================================================================================
2.1 --- a/src/main.c Sun Apr 24 12:30:41 2011 +0200 2.2 +++ b/src/main.c Mon Apr 25 01:17:19 2011 +0200 2.3 @@ -15,22 +15,16 @@ 2.4 #define CONFIG g_strdup_printf("%s/.config/tazweb", g_get_home_dir()) 2.5 #define START "file:///usr/share/webhome/index.html" 2.6 2.7 -/* Colors(Loader: #d66018 #7b705c) */ 2.8 -static gchar *loadfg = "#351a0a"; 2.9 -static gchar *toolbarbg = "#f1efeb"; 2.10 - 2.11 /* Needs AppleWebKit/531.2+ to handle all sites ? */ 2.12 static gchar *useragent = "TazWeb (X11; SliTaz GNU/Linux; U; en_US)"; 2.13 2.14 -static gchar* pagetitle; 2.15 -static GtkWidget *mainwindow, *browser, *loader, *toolbar; 2.16 +static GtkWidget* create_window(WebKitWebView** newwebview); 2.17 +static GtkWidget *mainwindow, *vbox, *browser, *toolbar; 2.18 static GtkWidget *urientry, *search; 2.19 static WebKitWebView* webview; 2.20 static WebKitWebFrame* frame; 2.21 -static gint progress; 2.22 static gint count = 0; 2.23 const gchar* uri; 2.24 -static GtkWidget* create_window(WebKitWebView** newwebview); 2.25 2.26 /* Create an icon */ 2.27 static GdkPixbuf* 2.28 @@ -49,43 +43,20 @@ 2.29 : g_strdup_printf("http://%s", uri); 2.30 } 2.31 2.32 -/* Loader area */ 2.33 -static void 2.34 -update_loader() 2.35 -{ 2.36 - GdkGC* gc; 2.37 - GdkColor fg; 2.38 - gint width; 2.39 - 2.40 - gc = gdk_gc_new(loader->window); 2.41 - width = progress * loader->allocation.width / 100; 2.42 - 2.43 - gdk_color_parse(loadfg, &fg); 2.44 - gdk_gc_set_rgb_fg_color(gc, &fg); 2.45 - gdk_draw_rectangle(loader->window, 2.46 - loader->style->bg_gc [GTK_WIDGET_STATE(loader)], 2.47 - TRUE, 0, 0, loader->allocation.width, loader->allocation.height); 2.48 - gdk_draw_rectangle(loader->window, gc, TRUE, 0, 0, width, 2.49 - loader->allocation.height); 2.50 - g_object_unref(gc); 2.51 -} 2.52 - 2.53 -/* Loader progress */ 2.54 -static gboolean 2.55 -expose_loader_cb(GtkWidget *loader, GdkEventExpose *event, gpointer data) 2.56 -{ 2.57 - update_loader(); 2.58 - return TRUE; 2.59 -} 2.60 - 2.61 /* Update title */ 2.62 static void 2.63 update_title(GtkWindow* window, WebKitWebView* webview) 2.64 { 2.65 - GString *string = g_string_new(webkit_web_view_get_title(webview)); 2.66 - gdouble loadprogress = webkit_web_view_get_progress(webview) * 100; 2.67 - //g_string_append(string, " - TazWeb"); 2.68 - gchar *title = g_string_free(string, FALSE); 2.69 + GString *string; 2.70 + gdouble progress; 2.71 + gchar *title; 2.72 + 2.73 + string = g_string_new(webkit_web_view_get_title(webview)); 2.74 + progress = webkit_web_view_get_progress(webview) * 100; 2.75 + if (progress < 100) 2.76 + g_string_append_printf(string, " [ %f%% ] ", progress); 2.77 + 2.78 + title = g_string_free(string, FALSE); 2.79 gtk_window_set_title(window, title); 2.80 g_free(title); 2.81 } 2.82 @@ -94,37 +65,26 @@ 2.83 static void 2.84 notify_title_cb(WebKitWebView* webview, GParamSpec* pspec, GtkWidget* window) 2.85 { 2.86 - pagetitle = g_strdup(webkit_web_view_get_title(webview)); 2.87 update_title(GTK_WINDOW(window), webview); 2.88 - update_loader(); 2.89 } 2.90 2.91 /* Request progress in window title */ 2.92 static void 2.93 notify_progress_cb(WebKitWebView* webview, GParamSpec* pspec, GtkWidget* window) 2.94 { 2.95 - progress = webkit_web_view_get_progress(webview) * 100; 2.96 update_title(GTK_WINDOW(window), webview); 2.97 - update_loader(); 2.98 } 2.99 2.100 /* Notify loader and url entry */ 2.101 static void 2.102 notify_load_status_cb(WebKitWebView* webview, GParamSpec* pspec, gpointer data) 2.103 { 2.104 - switch(webkit_web_view_get_load_status(webview)) 2.105 - { 2.106 - case WEBKIT_LOAD_COMMITTED: 2.107 - break; 2.108 - case WEBKIT_LOAD_FINISHED: 2.109 - progress = 0; 2.110 - update_loader(); 2.111 - break; 2.112 - } 2.113 - frame = webkit_web_view_get_main_frame(webview); 2.114 - uri = webkit_web_frame_get_uri(frame); 2.115 + if (webkit_web_view_get_load_status(webview) == WEBKIT_LOAD_COMMITTED) { 2.116 + frame = webkit_web_view_get_main_frame(webview); 2.117 + uri = webkit_web_frame_get_uri(frame); 2.118 if (uri) 2.119 gtk_entry_set_text(GTK_ENTRY(urientry), uri); 2.120 + } 2.121 } 2.122 2.123 /* Destroy the window */ 2.124 @@ -146,7 +106,7 @@ 2.125 source = webkit_web_view_get_view_source_mode(webview); 2.126 2.127 webkit_web_view_set_view_source_mode(webview, !source); 2.128 - webkit_web_view_load_uri(webview, uri); 2.129 + webkit_web_view_reload(webview); 2.130 } 2.131 2.132 /* URL entry callback function */ 2.133 @@ -221,8 +181,8 @@ 2.134 uri = webkit_download_get_uri(download); 2.135 const gchar* buffer; 2.136 asprintf(&buffer, 2.137 - "xterm -T 'Download' -geom 72x10+0-24 -e 'cd $HOME/Downloads && \ 2.138 - wget -c %s; sleep 2' &", uri); 2.139 + "xterm -T 'Download' -geom 72x10+0-24 -e \ 2.140 + 'cd $HOME/Downloads && wget -c %s; sleep 2' &", uri); 2.141 system(buffer); 2.142 } 2.143 2.144 @@ -319,7 +279,6 @@ 2.145 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(browser), 2.146 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); 2.147 2.148 - webview = WEBKIT_WEB_VIEW(webkit_web_view_new()); 2.149 gtk_container_add(GTK_CONTAINER(browser), GTK_WIDGET(webview)); 2.150 2.151 /* User agent */ 2.152 @@ -349,32 +308,16 @@ 2.153 return browser; 2.154 } 2.155 2.156 -/* Loader area */ 2.157 static GtkWidget* 2.158 -create_loader() 2.159 -{ 2.160 - loader = gtk_drawing_area_new(); 2.161 - gtk_widget_set_size_request(loader, 0, 2); 2.162 - g_signal_connect(G_OBJECT(loader), "expose_event", 2.163 - G_CALLBACK(expose_loader_cb), NULL); 2.164 - 2.165 - return loader; 2.166 -} 2.167 - 2.168 -static GtkWidget* 2.169 -create_toolbar() 2.170 +create_toolbar(GtkWidget* urientry, GtkWidget* search, WebKitWebView* webview) 2.171 { 2.172 GtkToolItem* item; 2.173 - GdkColor bg; 2.174 2.175 toolbar = gtk_toolbar_new(); 2.176 - //gtk_widget_set_size_request(toolbar, 0, 24); 2.177 gtk_toolbar_set_orientation(GTK_TOOLBAR(toolbar), 2.178 GTK_ORIENTATION_HORIZONTAL); 2.179 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), 2.180 GTK_TOOLBAR_BOTH_HORIZ); 2.181 - gdk_color_parse(toolbarbg, &bg); 2.182 - gtk_widget_modify_bg(toolbar, GTK_STATE_NORMAL, &bg); 2.183 2.184 /* The back button */ 2.185 item = gtk_tool_button_new_from_stock(GTK_STOCK_GO_BACK); 2.186 @@ -395,25 +338,18 @@ 2.187 /* URL entry */ 2.188 item = gtk_tool_item_new(); 2.189 gtk_tool_item_set_expand(item, TRUE); 2.190 - 2.191 - urientry = gtk_entry_new(); 2.192 - gtk_widget_modify_base( GTK_WIDGET(urientry), 2.193 - GTK_STATE_NORMAL, &bg); 2.194 - gtk_entry_set_inner_border(GTK_ENTRY(urientry), NULL); 2.195 - gtk_entry_set_has_frame(GTK_ENTRY(urientry), FALSE); 2.196 - 2.197 + gtk_widget_set_size_request(urientry, 0, 20); 2.198 gtk_container_add(GTK_CONTAINER(item), urientry); 2.199 g_signal_connect(G_OBJECT(urientry), "activate", 2.200 G_CALLBACK(uri_entry_cb), NULL); 2.201 gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1); 2.202 2.203 /* Separator */ 2.204 - //item = gtk_separator_tool_item_new(); 2.205 - //gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1); 2.206 + item = gtk_separator_tool_item_new(); 2.207 + gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1); 2.208 2.209 /* Search entry */ 2.210 item = gtk_tool_item_new(); 2.211 - search = gtk_entry_new(); 2.212 gtk_widget_set_size_request(search, 150, 20); 2.213 gtk_container_add(GTK_CONTAINER(item), search); 2.214 g_signal_connect(G_OBJECT(search), "activate", 2.215 @@ -433,26 +369,30 @@ 2.216 static GtkWidget* 2.217 create_window(WebKitWebView** newwebview) 2.218 { 2.219 + GtkWidget* window; 2.220 g_atomic_int_inc(&count); 2.221 - 2.222 - GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 2.223 - GtkWidget* vbox = gtk_vbox_new(FALSE, 0); 2.224 2.225 - /* Default TazWeb window size ratio to 3/4 --> 720, 540 */ 2.226 + /* Default TazWeb window */ 2.227 + window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 2.228 gtk_window_set_default_size(GTK_WINDOW(window), 800, 600); 2.229 gtk_window_set_icon(GTK_WINDOW(window), 2.230 create_pixbuf("/usr/share/pixmaps/tazweb.png")); 2.231 gtk_widget_set_name(window, "TazWeb"); 2.232 g_signal_connect(window, "destroy", G_CALLBACK(destroy_cb), NULL); 2.233 2.234 + /* Webview and widgets */ 2.235 + webview = WEBKIT_WEB_VIEW(webkit_web_view_new()); 2.236 + urientry = gtk_entry_new(); 2.237 + search = gtk_entry_new(); 2.238 + vbox = gtk_vbox_new(FALSE, 0); 2.239 + 2.240 /* Pack box and container */ 2.241 - gtk_box_pack_start(GTK_BOX(vbox), create_browser(window), TRUE, TRUE, 0); 2.242 - gtk_container_add(GTK_CONTAINER(vbox), create_loader()); 2.243 - gtk_box_set_child_packing(GTK_BOX(vbox), loader, 2.244 - FALSE, FALSE, 0, GTK_PACK_START); 2.245 - gtk_box_pack_start(GTK_BOX(vbox), create_toolbar(), FALSE, FALSE, 0); 2.246 + gtk_box_pack_start(GTK_BOX(vbox), 2.247 + create_browser(window), TRUE, TRUE, 0); 2.248 + gtk_box_pack_start(GTK_BOX(vbox), 2.249 + create_toolbar(urientry, search, webview), FALSE, FALSE, 0); 2.250 gtk_container_add(GTK_CONTAINER(window), vbox); 2.251 - 2.252 + 2.253 if (newwebview) 2.254 *newwebview = webview; 2.255 2.256 @@ -462,7 +402,7 @@ 2.257 int 2.258 main(int argc, char* argv[]) 2.259 { 2.260 - gtk_init(&argc, &argv); 2.261 + gtk_init(NULL, NULL); 2.262 if (!g_thread_supported()) 2.263 g_thread_init(NULL); 2.264 2.265 @@ -476,11 +416,10 @@ 2.266 check_requested_uri(); 2.267 2.268 mainwindow = create_window(&webview); 2.269 + 2.270 gtk_widget_show_all(mainwindow); 2.271 - 2.272 webkit_web_view_load_uri(webview, uri); 2.273 gtk_widget_grab_focus(GTK_WIDGET(webview)); 2.274 - 2.275 gtk_main(); 2.276 2.277 return 0;