Render stock:// as pixbufs and pass as data URIs

Stock icons do not match filenames in many icon themes and the
appropriate sizes may not be available. Thus we now always
render the icon through the theme engine and encode it as a
data URI with BASE64/ PNG.

As a side effect, we use stock sizes now instead of pixel sizes,
where 1 means menu size, 4 means button size and 6 dialog size;
the value 16 is translated to 4 to keep existing files working.
This commit is contained in:
Christian Dywan 2010-05-21 01:17:43 +02:00
parent 62cafb87b2
commit 6c4d94942f
2 changed files with 32 additions and 15 deletions

View file

@ -119,7 +119,7 @@
margin-bottom: -17px;
margin-left: 180px;
margin-top: 2px;
background: url({stock}/16/gtk-close) 98% 70% no-repeat;
background: url({stock}/1/gtk-close) 98% 70% no-repeat;
cursor: pointer;
z-index: -4;
opacity: 0.6;
@ -131,7 +131,7 @@
.activated p {
cursor: text;
background: url({stock}/16/gtk-edit) 98% 70% no-repeat;
background: url({stock}/1/gtk-edit) 98% 70% no-repeat;
opacity: 0.6;
color: rgba(0,0,0,1);
}

View file

@ -1117,13 +1117,13 @@ midori_view_web_view_resource_request_cb (WebKitWebView* web_view,
}
else if (g_str_has_prefix (uri, "stock://"))
{
GtkIconTheme* icon_theme = gtk_icon_theme_get_default ();
GdkPixbuf* pixbuf;
const gchar* icon_name = &uri[8] ? &uri[8] : "";
gint icon_size = 22;
GtkIconInfo* info;
gint icon_size = GTK_ICON_SIZE_MENU;
if (g_ascii_isalpha (icon_name[0]))
icon_size = strstr (icon_name, "dialog") ? 48 : 22;
icon_size = strstr (icon_name, "dialog") ?
GTK_ICON_SIZE_DIALOG : GTK_ICON_SIZE_BUTTON;
else if (g_ascii_isdigit (icon_name[0]))
{
guint i = 0;
@ -1132,21 +1132,38 @@ midori_view_web_view_resource_request_cb (WebKitWebView* web_view,
{
gchar* size = g_strndup (icon_name, i - 1);
icon_size = atoi (size);
/* Compatibility: map pixel to symbolic size */
if (icon_size == 16)
icon_size = GTK_ICON_SIZE_MENU;
g_free (size);
icon_name = &icon_name[i];
}
}
if ((info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, icon_size, 0)))
pixbuf = gtk_widget_render_icon (GTK_WIDGET (view), icon_name, icon_size, NULL);
if (!pixbuf)
pixbuf = gtk_widget_render_icon (GTK_WIDGET (view),
GTK_STOCK_MISSING_IMAGE, icon_size, NULL);
if (pixbuf)
{
const gchar* filename = gtk_icon_info_get_filename (info);
if (filename)
{
gchar* file_uri = g_filename_to_uri (filename, NULL, NULL);
webkit_network_request_set_uri (request, file_uri);
g_free (file_uri);
}
gtk_icon_info_free (info);
gboolean success;
gchar* buffer;
gsize buffer_size;
gchar* encoded;
gchar* data_uri;
success = gdk_pixbuf_save_to_buffer (pixbuf, &buffer, &buffer_size, "png", NULL, NULL);
g_object_unref (pixbuf);
if (!success)
return;
encoded = g_base64_encode ((guchar*)buffer, buffer_size);
g_free (buffer);
data_uri = g_strconcat ("data:image/png;base64,", encoded, NULL);
g_free (encoded);
webkit_network_request_set_uri (request, data_uri);
g_free (data_uri);
return;
}
}
}