232
244
GooCanvasImage *image = (GooCanvasImage*) object;
237
g_value_set_boxed (value, image->pattern);
240
g_value_set_double (value, image->x);
243
g_value_set_double (value, image->y);
246
g_value_set_double (value, image->width);
249
g_value_set_double (value, image->height);
252
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
246
goo_canvas_image_get_common_property (object, image->image_data, prop_id,
260
goo_canvas_image_set_property (GObject *object,
252
goo_canvas_image_set_common_property (GObject *object,
253
GooCanvasImageData *image_data,
265
GooCanvasImage *image = (GooCanvasImage*) object;
266
258
GdkPixbuf *pixbuf;
270
262
case PROP_PATTERN:
271
cairo_pattern_destroy (image->pattern);
272
image->pattern = g_value_get_boxed (value);
273
cairo_pattern_reference (image->pattern);
263
cairo_pattern_destroy (image_data->pattern);
264
image_data->pattern = g_value_get_boxed (value);
265
cairo_pattern_reference (image_data->pattern);
276
image->x = g_value_get_double (value);
268
image_data->x = g_value_get_double (value);
279
image->y = g_value_get_double (value);
271
image_data->y = g_value_get_double (value);
282
image->width = g_value_get_double (value);
274
image_data->width = g_value_get_double (value);
284
276
case PROP_HEIGHT:
285
image->height = g_value_get_double (value);
277
image_data->height = g_value_get_double (value);
287
279
case PROP_PIXBUF:
288
cairo_pattern_destroy (image->pattern);
280
cairo_pattern_destroy (image_data->pattern);
289
281
pixbuf = g_value_get_object (value);
290
image->pattern = goo_canvas_cairo_pattern_from_pixbuf (pixbuf);
291
image->width = gdk_pixbuf_get_width (pixbuf);
292
image->height = gdk_pixbuf_get_height (pixbuf);
282
image_data->pattern = goo_canvas_cairo_pattern_from_pixbuf (pixbuf);
283
image_data->width = gdk_pixbuf_get_width (pixbuf);
284
image_data->height = gdk_pixbuf_get_height (pixbuf);
295
287
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
299
g_signal_emit_by_name (image, "changed", TRUE);
294
goo_canvas_image_set_property (GObject *object,
299
GooCanvasItemSimple *simple = (GooCanvasItemSimple*) object;
300
GooCanvasImage *image = (GooCanvasImage*) object;
304
g_warning ("Can't set property of a canvas item with a model - set the model property instead");
308
goo_canvas_image_set_common_property (object, image->image_data, prop_id,
310
goo_canvas_item_simple_changed (simple, TRUE);
314
static GooCanvasItem*
315
goo_canvas_image_get_item_at (GooCanvasItemSimple *simple,
319
gboolean is_pointer_event)
321
GooCanvasImage *image = (GooCanvasImage*) simple;
322
GooCanvasImageData *image_data = image->image_data;
324
if (x < image_data->x || (x > image_data->x + image_data->width)
325
|| y < image_data->y || (y > image_data->y + image_data->height))
328
return (GooCanvasItem*) simple;
333
goo_canvas_image_update (GooCanvasItemSimple *simple,
336
GooCanvasImage *image = (GooCanvasImage*) simple;
337
GooCanvasImageData *image_data = image->image_data;
339
/* Compute the new bounds. */
340
simple->bounds.x1 = image_data->x;
341
simple->bounds.y1 = image_data->y;
342
simple->bounds.x2 = image_data->x + image_data->width;
343
simple->bounds.y2 = image_data->y + image_data->height;
348
goo_canvas_image_paint (GooCanvasItemSimple *simple,
350
GooCanvasBounds *bounds)
352
GooCanvasImage *image = (GooCanvasImage*) simple;
353
GooCanvasImageData *image_data = image->image_data;
354
cairo_matrix_t matrix;
356
if (!image_data->pattern)
359
goo_canvas_style_set_fill_options (simple->simple_data->style, cr);
360
cairo_set_source (cr, image_data->pattern);
361
cairo_matrix_init_translate (&matrix, -image_data->x, -image_data->y);
362
cairo_pattern_set_matrix (image_data->pattern, &matrix);
363
cairo_rectangle (cr, image_data->x, image_data->y,
364
image_data->width, image_data->height);
367
/* Using cairo_paint() is much slower, so I guess we shouldn't. */
368
/*cairo_paint (cr);*/
373
goo_canvas_image_set_model (GooCanvasItem *item,
374
GooCanvasItemModel *model)
376
GooCanvasItemSimple *simple = (GooCanvasItemSimple*) item;
377
GooCanvasImage *image = (GooCanvasImage*) item;
378
GooCanvasImageModel *imodel = (GooCanvasImageModel*) model;
380
/* If our data was allocated, free it. */
383
cairo_pattern_destroy (image->image_data->pattern);
384
g_slice_free (GooCanvasImageData, image->image_data);
387
/* Now use the new model's data instead. */
388
image->image_data = &imodel->image_data;
390
/* Let the parent GooCanvasItemSimple code do the rest. */
391
goo_canvas_item_simple_set_model (simple, model);
396
canvas_item_interface_init (GooCanvasItemIface *iface)
398
iface->set_model = goo_canvas_image_set_model;
403
goo_canvas_image_class_init (GooCanvasImageClass *klass)
405
GObjectClass *gobject_class = (GObjectClass*) klass;
406
GooCanvasItemSimpleClass *simple_class = (GooCanvasItemSimpleClass*) klass;
408
gobject_class->finalize = goo_canvas_image_finalize;
410
gobject_class->get_property = goo_canvas_image_get_property;
411
gobject_class->set_property = goo_canvas_image_set_property;
413
simple_class->simple_update = goo_canvas_image_update;
414
simple_class->simple_paint = goo_canvas_image_paint;
415
simple_class->simple_get_item_at = goo_canvas_image_get_item_at;
417
goo_canvas_image_install_common_properties (gobject_class);
423
* SECTION:goocanvasimagemodel
424
* @Title: GooCanvasImageModel
425
* @Short_Description: a model for image items.
427
* GooCanvasImageModel represent a model for image items.
429
* It is a subclass of #GooCanvasItemModelSimple and so inherits all of the
430
* style properties such as "operator" and "pointer-events".
432
* It also implements the #GooCanvasItemModel interface, so you can use the
433
* #GooCanvasItemModel functions such as goo_canvas_item_model_raise() and
434
* goo_canvas_item_model_rotate().
436
* To create a #GooCanvasImageModel use goo_canvas_image_model_new().
438
* To get or set the properties of an existing #GooCanvasImageModel, use
439
* g_object_get() and g_object_set().
441
* To respond to events such as mouse clicks on the image you must connect
442
* to the signal handlers of the corresponding #GooCanvasImage objects.
443
* (See goo_canvas_get_item() and #GooCanvas::item-created.)
446
static void item_model_interface_init (GooCanvasItemModelIface *iface);
447
static void goo_canvas_image_model_finalize (GObject *object);
448
static void goo_canvas_image_model_get_property (GObject *object,
452
static void goo_canvas_image_model_set_property (GObject *object,
457
G_DEFINE_TYPE_WITH_CODE (GooCanvasImageModel, goo_canvas_image_model,
458
GOO_TYPE_CANVAS_ITEM_MODEL_SIMPLE,
459
G_IMPLEMENT_INTERFACE (GOO_TYPE_CANVAS_ITEM_MODEL,
460
item_model_interface_init))
464
goo_canvas_image_model_class_init (GooCanvasImageModelClass *klass)
466
GObjectClass *gobject_class = (GObjectClass*) klass;
468
gobject_class->finalize = goo_canvas_image_model_finalize;
470
gobject_class->get_property = goo_canvas_image_model_get_property;
471
gobject_class->set_property = goo_canvas_image_model_set_property;
473
goo_canvas_image_install_common_properties (gobject_class);
478
goo_canvas_image_model_init (GooCanvasImageModel *emodel)
485
* goo_canvas_image_model_new:
486
* @parent: the parent model, or %NULL. If a parent is specified, it will
487
* assume ownership of the item, and the item will automatically be freed when
488
* it is removed from the parent. Otherwise call g_object_unref() to free it.
489
* @pixbuf: the #GdkPixbuf containing the image data, or %NULL.
490
* @x: the x coordinate of the image.
491
* @y: the y coordinate of the image.
492
* @...: optional pairs of property names and values, and a terminating %NULL.
494
* Creates a new image model.
498
* Here's an example showing how to create an image at (100.0, 100.0), using
499
* the given pixbuf at its natural width and height:
501
* <informalexample><programlisting>
502
* GooCanvasItemModel *image = goo_canvas_image_model_new (mygroup, pixbuf, 100.0, 100.0,
504
* </programlisting></informalexample>
506
* Returns: a new image model.
509
goo_canvas_image_model_new (GooCanvasItemModel *parent,
515
GooCanvasItemModel *model;
516
GooCanvasImageModel *imodel;
517
GooCanvasImageData *image_data;
518
const char *first_property;
521
model = g_object_new (GOO_TYPE_CANVAS_IMAGE_MODEL, NULL);
522
imodel = (GooCanvasImageModel*) model;
524
image_data = &imodel->image_data;
530
image_data->pattern = goo_canvas_cairo_pattern_from_pixbuf (pixbuf);
531
image_data->width = gdk_pixbuf_get_width (pixbuf);
532
image_data->height = gdk_pixbuf_get_height (pixbuf);
535
va_start (var_args, y);
536
first_property = va_arg (var_args, char*);
538
g_object_set_valist ((GObject*) model, first_property, var_args);
543
goo_canvas_item_model_add_child (parent, model, -1);
544
g_object_unref (model);
552
goo_canvas_image_model_finalize (GObject *object)
554
GooCanvasImageModel *imodel = (GooCanvasImageModel*) object;
556
cairo_pattern_destroy (imodel->image_data.pattern);
558
G_OBJECT_CLASS (goo_canvas_image_model_parent_class)->finalize (object);
563
goo_canvas_image_model_get_property (GObject *object,
568
GooCanvasImageModel *imodel = (GooCanvasImageModel*) object;
570
goo_canvas_image_get_common_property (object, &imodel->image_data, prop_id,
576
goo_canvas_image_model_set_property (GObject *object,
581
GooCanvasImageModel *imodel = (GooCanvasImageModel*) object;
583
goo_canvas_image_set_common_property (object, &imodel->image_data, prop_id,
585
g_signal_emit_by_name (imodel, "changed", TRUE);
589
static GooCanvasItem*
590
goo_canvas_image_model_create_item (GooCanvasItemModel *model,
595
item = g_object_new (GOO_TYPE_CANVAS_IMAGE, NULL);
596
goo_canvas_item_set_model (item, model);
603
item_model_interface_init (GooCanvasItemModelIface *iface)
605
iface->create_item = goo_canvas_image_model_create_item;