sheet-object-image.c 8.9 KB
Newer Older
Jody Goldberg's avatar
Jody Goldberg committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 * sheet-object-image.c: a wrapper for gdkpixbuf to display images.
 *
 * Author:
 * 	Jody Goldberg (jody@gnome.org)
 */
#include <gnumeric-config.h>
#include "gnumeric.h"
#include "sheet-object-image.h"
#include "sheet-object-impl.h"
#include "sheet-control-gui.h"
#include "gnumeric-canvas.h"

#include <gal/util/e-util.h>
#include <libgnomecanvas/gnome-canvas-pixbuf.h>
#include <libgnomecanvas/gnome-canvas-rect-ellipse.h>

#include <math.h>

Jon K Hellan's avatar
Jon K Hellan committed
22
struct _SheetObjectImage {
Jody Goldberg's avatar
Jody Goldberg committed
23 24 25 26 27
	SheetObject  sheet_object;

	char const   *type;
	guint8       *data;
	guint32	      data_len;
28 29

	gboolean dumped;
Jon K Hellan's avatar
Jon K Hellan committed
30 31 32 33 34
	double   crop_top;
	double   crop_bottom;
	double   crop_left;
	double   crop_right;
};
Jody Goldberg's avatar
Jody Goldberg committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

typedef struct {
	SheetObjectClass parent_class;
} SheetObjectImageClass;

#define SHEET_OBJECT_IMAGE(o)    (G_TYPE_CHECK_INSTANCE_CAST((o), SHEET_OBJECT_IMAGE_TYPE, SheetObjectImage))
#define IS_SHEET_OBJECT_IMAGE(o) (G_TYPE_CHECK_INSTANCE_TYPE((o), SHEET_OBJECT_IMAGE_TYPE))

static SheetObjectClass *sheet_object_image_parent_class;

SheetObject *
sheet_object_image_new (char const   *type,
			guint8       *data,
			guint32	      data_len,
			gboolean      copy_data)
{
	SheetObjectImage *soi;

	soi = g_object_new (SHEET_OBJECT_IMAGE_TYPE, NULL);
	soi->type     = type;
	soi->data_len = data_len;
Jon K Hellan's avatar
Jon K Hellan committed
56 57
	soi->crop_top = soi->crop_bottom = soi->crop_left = soi->crop_right
		= 0.0;
Jody Goldberg's avatar
Jody Goldberg committed
58 59 60 61 62 63 64 65 66
	if (copy_data) {
		soi->data = g_malloc (data_len);
		memcpy (soi->data, data, data_len);
	} else
		soi->data = data;

	return SHEET_OBJECT (soi);
}

Jon K Hellan's avatar
Jon K Hellan committed
67 68 69 70 71 72 73 74 75 76 77 78 79
void
sheet_object_image_set_crop (SheetObjectImage *soi,
			     double crop_left,  double crop_top,
			     double crop_right, double crop_bottom)
{
	g_return_if_fail (IS_SHEET_OBJECT_IMAGE (soi));
	
	soi->crop_left   = crop_left;
	soi->crop_top    = crop_top;
	soi->crop_right  = crop_right;
	soi->crop_bottom = crop_bottom;
}

Jody Goldberg's avatar
Jody Goldberg committed
80 81 82 83 84 85 86 87 88 89 90 91
static void
sheet_object_image_finalize (GObject *object)
{
	SheetObjectImage *soi;

	soi = SHEET_OBJECT_IMAGE (object);
	g_free (soi->data);
	soi->data = NULL;

	G_OBJECT_CLASS (sheet_object_image_parent_class)->finalize (object);
}

Jon K Hellan's avatar
Jon K Hellan committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
static GdkPixbuf *
soi_get_cropped_pixbuf (SheetObjectImage *soi, GdkPixbuf *pixbuf)
{
	int width  = gdk_pixbuf_get_width (pixbuf);
	int height = gdk_pixbuf_get_height (pixbuf);
	int sub_x = rint (soi->crop_left * width);
	int sub_y = rint (soi->crop_top * height);
	int sub_width  = rint (width *
			       (1. - soi->crop_left - soi->crop_right));
	int sub_height = rint (height *
			       (1. - soi->crop_top - soi->crop_bottom));
	GdkPixbuf *sub = gdk_pixbuf_new_subpixbuf (pixbuf, sub_x, sub_y,
						   sub_width, sub_height);
	if (sub) {
		g_object_unref (G_OBJECT (pixbuf));
		pixbuf = sub;
	}
	return pixbuf;
}

112 113
/**
 * be sure to unref the result if it is non-NULL
114 115 116 117
 *
 * TODO : this is really overkill for now.
 * only wmf/emf will require regenerating the pixbug for different scale
 * factors.  And even then we should cache them.
118 119 120
 */
static GdkPixbuf *
soi_get_pixbuf (SheetObjectImage *soi, double scale)
Jody Goldberg's avatar
Jody Goldberg committed
121 122 123 124 125
{
	GError *err = NULL;
	guint8 *data;
	guint32 data_len;
	GdkPixbufLoader *loader;
126
	GdkPixbuf	*res = NULL;
Jody Goldberg's avatar
Jody Goldberg committed
127

128
	g_return_val_if_fail (IS_SHEET_OBJECT_IMAGE (soi), NULL);
Jody Goldberg's avatar
Jody Goldberg committed
129 130 131 132 133 134 135

	data     = soi->data;
	data_len = soi->data_len;

#warning Add optional use of libwmf here to handle wmf
	loader = gdk_pixbuf_loader_new ();

136 137 138 139
	if (!gdk_pixbuf_loader_write (loader, soi->data, soi->data_len, &err)) {

		if (!soi->dumped) {
			static int count = 0;
140
			char *filename = g_strdup_printf ("unknown%d.%s",  count++, soi->type);
141 142 143 144 145 146 147 148 149 150 151 152 153
			FILE *file = fopen (filename, "w");
			if (file != NULL) {
				fwrite (soi->data, soi->data_len, 1, file);
				fclose (file);
			}
			g_free (filename);

			if (err != NULL) {
				g_warning (err-> message);
				g_error_free (err);
				err = NULL;
			}
			soi->dumped = TRUE;
Jody Goldberg's avatar
Jody Goldberg committed
154
		}
155 156 157
	} else {
		res = gdk_pixbuf_loader_get_pixbuf (loader),
		g_object_ref (G_OBJECT (res));
Jon K Hellan's avatar
Jon K Hellan committed
158 159 160 161
		if (soi->crop_top != 0.0  || soi->crop_bottom != 0.0 ||
		    soi->crop_left != 0.0 || soi->crop_right != 0.0) {
			res = soi_get_cropped_pixbuf (soi, res);
		}
Jody Goldberg's avatar
Jody Goldberg committed
162 163 164 165 166 167 168 169 170
	}

	gdk_pixbuf_loader_close (loader, &err);
	if (err != NULL) {
		g_warning (err->message);
		g_error_free (err);
		err = NULL;
	}
	g_object_unref (G_OBJECT (loader));
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
	return res;
}

static GObject *
sheet_object_image_new_view (SheetObject *so, SheetControlGUI *scg)
{
	/* FIXME : this is bogus */
	GnumericCanvas *gcanvas = scg_pane (scg, 0);
	SheetObjectImage *soi = SHEET_OBJECT_IMAGE (so);
	GnomeCanvasItem *item = NULL;
	GdkPixbuf	*pixbuf;

	g_return_val_if_fail (IS_SHEET_OBJECT_IMAGE (so), NULL);
	g_return_val_if_fail (IS_SHEET_CONTROL_GUI (scg), NULL);

	pixbuf = soi_get_pixbuf (soi, 1.);
	if (pixbuf != NULL)
		item = gnome_canvas_item_new (gcanvas->object_group,
			GNOME_TYPE_CANVAS_PIXBUF,
			"pixbuf", pixbuf,
			NULL);
	else
		item = gnome_canvas_item_new (gcanvas->object_group,
			GNOME_TYPE_CANVAS_RECT,
			"fill_color",		"white",
			"outline_color",	"black",
			"width_units",		1.,
			NULL);
Jody Goldberg's avatar
Jody Goldberg committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278

	scg_object_register (so, item);
	return G_OBJECT (item);
}

static void
sheet_object_image_update_bounds (SheetObject *so, GObject *view,
				  SheetControlGUI *scg)
{
	double coords [4];

	scg_object_view_position (scg, so, coords);

	/* handle place holders */
	if (GNOME_IS_CANVAS_PIXBUF (view))
		gnome_canvas_item_set (GNOME_CANVAS_ITEM (view),
			"x",	  MIN (coords [0], coords [2]),
			"y",	  MIN (coords [1], coords [3]),
			"width",  fabs (coords [2] - coords [0]),
			"height", fabs (coords [3] - coords [1]),
			"width_set",  TRUE,
			"height_set", TRUE,
			NULL);
	else
		gnome_canvas_item_set (GNOME_CANVAS_ITEM (view),
			"x1", MIN (coords [0], coords [2]),
			"y1", MIN (coords [1], coords [3]),
			"x2", MAX (coords [0], coords [2]),
			"y2", MAX (coords [1], coords [3]),
			NULL);

	if (so->is_visible)
		gnome_canvas_item_show (GNOME_CANVAS_ITEM (view));
	else
		gnome_canvas_item_hide (GNOME_CANVAS_ITEM (view));
}

static gboolean
sheet_object_image_read_xml (SheetObject *so,
			       XmlParseContext const *ctxt, xmlNodePtr tree)
{
	SheetObjectImage *soi;

	g_return_val_if_fail (IS_SHEET_OBJECT_IMAGE (so), TRUE);
	soi = SHEET_OBJECT_IMAGE (so);

	return FALSE;
}

static gboolean
sheet_object_image_write_xml (SheetObject const *so,
				XmlParseContext const *ctxt, xmlNodePtr tree)
{
	SheetObjectImage *soi;

	g_return_val_if_fail (IS_SHEET_OBJECT_IMAGE (so), TRUE);
	soi = SHEET_OBJECT_IMAGE (so);

	return FALSE;
}

static SheetObject *
sheet_object_image_clone (SheetObject const *so, Sheet *sheet)
{
	SheetObjectImage *soi;
	SheetObjectImage *new_soi;

	g_return_val_if_fail (IS_SHEET_OBJECT_IMAGE (so), NULL);
	soi = SHEET_OBJECT_IMAGE (so);

	new_soi = g_object_new (GTK_OBJECT_TYPE (soi), NULL);

	return SHEET_OBJECT (new_soi);
}

static void
sheet_object_image_print (SheetObject const *so, GnomePrintContext *ctx,
			  double base_x, double base_y)
{
	SheetObjectImage *soi;
279
	GdkPixbuf	 *pixbuf;
Jody Goldberg's avatar
Jody Goldberg committed
280 281 282 283 284 285
	double coords [4];

	g_return_if_fail (IS_SHEET_OBJECT_IMAGE (so));
	g_return_if_fail (GNOME_IS_PRINT_CONTEXT (ctx));
	soi = SHEET_OBJECT_IMAGE (so);

286 287 288
	pixbuf = soi_get_pixbuf (soi, 1.);
	if (pixbuf == NULL)
		return;
Jody Goldberg's avatar
Jody Goldberg committed
289

290
	sheet_object_position_pts_get (so, coords);
Jody Goldberg's avatar
Jody Goldberg committed
291 292 293 294 295 296 297 298 299 300 301 302 303
	gnome_print_gsave (ctx);
	if (gdk_pixbuf_get_has_alpha (pixbuf))
		gnome_print_rgbaimage  (ctx,
					gdk_pixbuf_get_pixels    (pixbuf),
					gdk_pixbuf_get_width     (pixbuf),
					gdk_pixbuf_get_height    (pixbuf),
					gdk_pixbuf_get_rowstride (pixbuf));
	else
		gnome_print_rgbimage  (ctx,
				       gdk_pixbuf_get_pixels    (pixbuf),
				       gdk_pixbuf_get_width     (pixbuf),
				       gdk_pixbuf_get_height    (pixbuf),
				       gdk_pixbuf_get_rowstride (pixbuf));
304
	g_object_unref (G_OBJECT (pixbuf));
Jody Goldberg's avatar
Jody Goldberg committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
	gnome_print_grestore (ctx);
}

static void
sheet_object_image_class_init (GObjectClass *object_class)
{
	SheetObjectClass *sheet_object_class;

	sheet_object_image_parent_class = gtk_type_class (SHEET_OBJECT_TYPE);

	/* Object class method overrides */
	object_class->finalize = sheet_object_image_finalize;

	/* SheetObject class method overrides */
	sheet_object_class = SHEET_OBJECT_CLASS (object_class);
	sheet_object_class->new_view	  = sheet_object_image_new_view;
	sheet_object_class->update_bounds = sheet_object_image_update_bounds;
	sheet_object_class->read_xml	  = sheet_object_image_read_xml;
	sheet_object_class->write_xml	  = sheet_object_image_write_xml;
	sheet_object_class->clone         = sheet_object_image_clone;
	sheet_object_class->user_config   = NULL;
	sheet_object_class->print         = sheet_object_image_print;
	sheet_object_class->rubber_band_directly = TRUE;
}

static void
sheet_object_image_init (GObject *obj)
{
	SheetObjectImage *soi;
	SheetObject *so;

	soi = SHEET_OBJECT_IMAGE (obj);
337
	soi->dumped = FALSE;
Jon K Hellan's avatar
Jon K Hellan committed
338 339
	soi->crop_top = soi->crop_bottom = soi->crop_left = soi->crop_right
		= 0.0;
Jody Goldberg's avatar
Jody Goldberg committed
340 341 342 343 344 345 346 347

	so = SHEET_OBJECT (obj);
	so->anchor.direction = SO_DIR_NONE_MASK;
}

E_MAKE_TYPE (sheet_object_image, "SheetObjectImage", SheetObjectImage,
	     sheet_object_image_class_init, sheet_object_image_init,
	     SHEET_OBJECT_TYPE);