Author: vince
Date: Thu Dec 29 11:12:21 2011
New Revision: 13352
URL:
http://source.netsurf-browser.org?rev=13352&view=rev
Log:
fix icon setting
Added:
branches/vince/netsurf-cairo/gtk/gdk.c
branches/vince/netsurf-cairo/gtk/gdk.h
Modified:
branches/vince/netsurf-cairo/gtk/Makefile.target
branches/vince/netsurf-cairo/gtk/scaffolding.c
Modified: branches/vince/netsurf-cairo/gtk/Makefile.target
URL:
http://source.netsurf-browser.org/branches/vince/netsurf-cairo/gtk/Makefi...
==============================================================================
--- branches/vince/netsurf-cairo/gtk/Makefile.target (original)
+++ branches/vince/netsurf-cairo/gtk/Makefile.target Thu Dec 29 11:12:21 2011
@@ -71,7 +71,7 @@
# S_GTK are sources purely for the GTK build
S_GTK := font_pango.c bitmap.c gui.c schedule.c thumbnail.c plotters.c \
- treeview.c scaffolding.c completion.c login.c throbber.c \
+ treeview.c scaffolding.c gdk.c completion.c login.c throbber.c \
selection.c history.c window.c filetype.c download.c menu.c \
print.c save.c search.c tabs.c theme.c toolbar.c \
sexy_icon_entry.c compat.c cookies.c hotlist.c system_colour.c \
Added: branches/vince/netsurf-cairo/gtk/gdk.c
URL:
http://source.netsurf-browser.org/branches/vince/netsurf-cairo/gtk/gdk.c?...
==============================================================================
--- branches/vince/netsurf-cairo/gtk/gdk.c (added)
+++ branches/vince/netsurf-cairo/gtk/gdk.c Thu Dec 29 11:12:21 2011
@@ -1,0 +1,124 @@
+/*
+ * Copyright 2011 Vincent Sanders <vince(a)netsurf-browser.org>
+ *
+ * This file is part of NetSurf,
http://www.netsurf-browser.org/
+ *
+ * NetSurf is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * NetSurf is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <
http://www.gnu.org/licenses/>.
+ */
+
+#include <string.h>
+
+#include "utils/log.h"
+
+#include "gtk/gdk.h"
+
+static void
+convert_alpha(guchar *dest_data,
+ int dest_stride,
+ guchar *src_data,
+ int src_stride,
+ int width,
+ int height)
+{
+ int x, y;
+
+ for (y = 0; y < height; y++) {
+ guint32 *src = (guint32 *) src_data;
+
+ for (x = 0; x < width; x++) {
+ guint alpha = src[x] >> 24;
+
+ if (alpha == 0) {
+ dest_data[x * 4 + 0] = 0;
+ dest_data[x * 4 + 1] = 0;
+ dest_data[x * 4 + 2] = 0;
+ } else {
+ dest_data[x * 4 + 0] = (((src[x] & 0xff0000) >> 16) * 255 + alpha / 2) /
alpha;
+ dest_data[x * 4 + 1] = (((src[x] & 0x00ff00) >> 8) * 255 + alpha / 2) /
alpha;
+ dest_data[x * 4 + 2] = (((src[x] & 0x0000ff) >> 0) * 255 + alpha / 2) /
alpha;
+ }
+ dest_data[x * 4 + 3] = alpha;
+ }
+
+ src_data += src_stride;
+ dest_data += dest_stride;
+ }
+}
+
+
+GdkPixbuf *
+nsgdk_pixbuf_get_from_surface(cairo_surface_t *surface, int scwidth, int scheight)
+{
+ int width, height; /* source width and height */
+ cairo_surface_t *scsurface; /* scaled surface */
+ cairo_t *cr; /* cairo context for scaled surface */
+ GdkPixbuf *pixbuf; /* The result pixel buffer */
+
+ /* create pixmap */
+ pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, true, 8, scwidth, scheight);
+ if (pixbuf == NULL) {
+ return NULL;
+ }
+
+ memset(gdk_pixbuf_get_pixels(pixbuf),
+ 0xff,
+ gdk_pixbuf_get_rowstride(pixbuf) * scheight);
+
+ /* scale cairo surface into new surface the target size */
+ cairo_surface_flush(surface); /* ensure source surface is ready */
+
+ /* get source surface dimensions */
+ width = cairo_image_surface_get_width(surface);
+ height = cairo_image_surface_get_height(surface);
+
+ /* scaled surface always has an alpha chanel for ease */
+ scsurface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, scwidth, scheight);
+ if (cairo_surface_status(scsurface) != CAIRO_STATUS_SUCCESS) {
+ cairo_surface_destroy(scsurface);
+ g_object_unref(pixbuf);
+ LOG(("Surface creation failed"));
+ return NULL;
+ }
+
+ cr = cairo_create(scsurface);
+
+ /* Scale *before* setting the source surface */
+ cairo_scale(cr, (double)scwidth / width, (double)scheight / height);
+ cairo_set_source_surface(cr, surface, 0, 0);
+
+ /* To avoid getting the edge pixels blended with 0
+ * alpha, which would occur with the default
+ * EXTEND_NONE. Use EXTEND_PAD for 1.2 or newer
+ */
+ cairo_pattern_set_extend(cairo_get_source(cr), CAIRO_EXTEND_REFLECT);
+
+ /* Replace the destination with the source instead of overlaying */
+ cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
+
+ /* Do the actual drawing */
+ cairo_paint(cr);
+
+ cairo_destroy(cr);
+
+ /* copy data from surface into pixmap */
+ convert_alpha(gdk_pixbuf_get_pixels(pixbuf),
+ gdk_pixbuf_get_rowstride(pixbuf),
+ cairo_image_surface_get_data(scsurface),
+ cairo_image_surface_get_stride(scsurface),
+ scwidth, scheight);
+
+ cairo_surface_destroy(scsurface);
+
+ return pixbuf;
+}
+
Added: branches/vince/netsurf-cairo/gtk/gdk.h
URL:
http://source.netsurf-browser.org/branches/vince/netsurf-cairo/gtk/gdk.h?...
==============================================================================
--- branches/vince/netsurf-cairo/gtk/gdk.h (added)
+++ branches/vince/netsurf-cairo/gtk/gdk.h Thu Dec 29 11:12:21 2011
@@ -1,0 +1,35 @@
+/*
+ * Copyright 2011 Vincent Sanders <vince(a)netsurf-browser.org>
+ *
+ * This file is part of NetSurf,
http://www.netsurf-browser.org/
+ *
+ * NetSurf is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * NetSurf is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <
http://www.gnu.org/licenses/>.
+ */
+
+/** \file
+ * GDK support functions for missing interfaces
+ */
+
+#ifndef NETSURF_GTK_GDK_H_
+#define NETSURF_GTK_GDK_H_
+
+#include <gtk/gtk.h>
+
+/** obtain a pixbuf of the specified size from a cairo surface.
+ *
+ * This is the same as the GTK+ 3 gdk_pixbuf_get_from_surface but
+ * actually works and is available on gtk 2
+ */
+GdkPixbuf *nsgdk_pixbuf_get_from_surface(cairo_surface_t *surface, int width, int
height);
+
+#endif /* NETSURF_GTK_GDK_H */
Modified: branches/vince/netsurf-cairo/gtk/scaffolding.c
URL:
http://source.netsurf-browser.org/branches/vince/netsurf-cairo/gtk/scaffo...
==============================================================================
--- branches/vince/netsurf-cairo/gtk/scaffolding.c (original)
+++ branches/vince/netsurf-cairo/gtk/scaffolding.c Thu Dec 29 11:12:21 2011
@@ -76,6 +76,7 @@
#include "gtk/options.h"
#include "gtk/sexy_icon_entry.h"
#include "gtk/compat.h"
+#include "gtk/gdk.h"
#include "image/ico.h"
#include "render/box.h"
#include "render/font.h"
@@ -1992,57 +1993,21 @@
gtk_image_set_from_pixbuf(g->throbber, nsgtk_throbber->framedata[0]);
}
-static GdkPixmap *
-pixmap_from_cairo_surface(cairo_surface_t *bmsurface,
- GdkDrawable *drawable,
- int width,
- int height)
-{
- int bmwidth, bmheight;
- GdkPixmap *pixmap;
- cairo_t *cr;
-
- cairo_surface_flush(bmsurface);
-
- bmwidth = cairo_image_surface_get_width(bmsurface);
- bmheight = cairo_image_surface_get_height(bmsurface);
-
- pixmap = gdk_pixmap_new(drawable, width, height, -1);
-
- cr = gdk_cairo_create(pixmap);
-
- if (cairo_status(cr) == CAIRO_STATUS_SUCCESS) {
- cairo_scale(cr,
- (double)width / bmwidth,
- (double)height / bmheight);
-
- cairo_set_source_surface(cr, bmsurface, 0, 0);
-
- /* To avoid getting the edge pixels blended with 0
- * alpha, which would occur with the default
- * EXTEND_NONE. Use EXTEND_PAD for 1.2 or newer (2)
- */
- cairo_pattern_set_extend(cairo_get_source(cr),
- CAIRO_EXTEND_REFLECT);
-
- /* Replace the destination with the source instead of
- * overlaying
- */
- cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
-
- /* Do the actual drawing */
- cairo_paint(cr);
-
- cairo_surface_flush(cairo_get_target(cr));
- } else {
- LOG(("%s", cairo_status_to_string(cairo_status(cr))));
- g_object_unref(pixmap);
- pixmap = NULL;
- }
-
- cairo_destroy(cr);
-
- return pixmap;
+static GtkImage *
+nsgtk_image_new_from_surface(cairo_surface_t *surface, int w, int h)
+{
+ GdkPixbuf *pixbuf;
+ GtkImage *image = NULL;
+
+ pixbuf = nsgdk_pixbuf_get_from_surface(surface, w, h);
+
+ if (pixbuf != NULL) {
+ image = GTK_IMAGE(gtk_image_new_from_pixbuf(pixbuf));
+ }
+
+ g_object_unref(pixbuf);
+
+ return image;
}
/**
@@ -2060,12 +2025,7 @@
icon_bitmap = (icon != NULL) ? content_get_bitmap(icon) : NULL;
if (icon_bitmap != NULL) {
- GdkPixmap *pixmap;
- pixmap = pixmap_from_cairo_surface(icon_bitmap->surface, ((GtkWidget
*)g->window)->window, 16, 16);
-
- iconImage = GTK_IMAGE(gtk_image_new_from_pixmap(pixmap, NULL));
- LOG(("set to %p",iconImage));
- g_object_unref(pixmap);
+ iconImage = nsgtk_image_new_from_surface(icon_bitmap->surface, 16, 16);
}
if (iconImage == NULL) {