aboutsummaryrefslogtreecommitdiffstats
path: root/libgnomecanvas/gnome-canvas-pixbuf.c
blob: 32e83bbe1dd36d77e3393d70de70d0386e09b5a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* GNOME libraries - GdkPixbuf item for the GNOME canvas
 *
 * Copyright (C) 1999 The Free Software Foundation
 *
 * Author: Federico Mena-Quintero <federico@gimp.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include <math.h>
#include <libgnomecanvas/gnome-canvas.h>
#include <libgnomecanvas/gnome-canvas-util.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include "gnome-canvas-pixbuf.h"

/* Private part of the GnomeCanvasPixbuf structure */
struct _GnomeCanvasPixbufPrivate {
    /* Our gdk-pixbuf */
    GdkPixbuf *pixbuf;
};

/* Object argument IDs */
enum {
    PROP_0,
    PROP_PIXBUF
};

static void gnome_canvas_pixbuf_dispose (GnomeCanvasItem *object);
static void gnome_canvas_pixbuf_set_property (GObject *object,
                          guint param_id,
                          const GValue *value,
                          GParamSpec *pspec);
static void gnome_canvas_pixbuf_get_property (GObject *object,
                          guint param_id,
                          GValue *value,
                          GParamSpec *pspec);

static void gnome_canvas_pixbuf_update (GnomeCanvasItem *item, const cairo_matrix_t *i2c, gint flags);
static void gnome_canvas_pixbuf_draw (GnomeCanvasItem *item, GdkDrawable *drawable,
                      gint x, gint y, gint width, gint height);
static GnomeCanvasItem *gnome_canvas_pixbuf_point (GnomeCanvasItem *item,
                                                   gdouble x,
                                                   gdouble y,
                                                   gint cx,
                                                   gint cy);
static void gnome_canvas_pixbuf_bounds (GnomeCanvasItem *item,
                    gdouble *x1, gdouble *y1, gdouble *x2, gdouble *y2);

G_DEFINE_TYPE (GnomeCanvasPixbuf, gnome_canvas_pixbuf, GNOME_TYPE_CANVAS_ITEM)



/* Class initialization function for the pixbuf canvas item */
static void
gnome_canvas_pixbuf_class_init (GnomeCanvasPixbufClass *class)
{
        GObjectClass *gobject_class;
    GnomeCanvasItemClass *item_class;

        gobject_class = (GObjectClass *) class;
    item_class = (GnomeCanvasItemClass *) class;

    gobject_class->set_property = gnome_canvas_pixbuf_set_property;
    gobject_class->get_property = gnome_canvas_pixbuf_get_property;

        g_object_class_install_property
                (gobject_class,
                 PROP_PIXBUF,
                 g_param_spec_object ("pixbuf", NULL, NULL,
                                      GDK_TYPE_PIXBUF,
                                      (G_PARAM_READABLE | G_PARAM_WRITABLE)));

    item_class->dispose = gnome_canvas_pixbuf_dispose;
    item_class->update = gnome_canvas_pixbuf_update;
    item_class->draw = gnome_canvas_pixbuf_draw;
    item_class->point = gnome_canvas_pixbuf_point;
    item_class->bounds = gnome_canvas_pixbuf_bounds;

        g_type_class_add_private (class, sizeof (GnomeCanvasPixbufPrivate));
}

/* Object initialization function for the pixbuf canvas item */
static void
gnome_canvas_pixbuf_init (GnomeCanvasPixbuf *gcp)
{
    GnomeCanvasPixbufPrivate *priv;

    priv = gcp->priv =  G_TYPE_INSTANCE_GET_PRIVATE (gcp,
                                                         GNOME_TYPE_CANVAS_PIXBUF,
                                                         GnomeCanvasPixbufPrivate);
}

/* Dispose handler for the pixbuf canvas item */
static void
gnome_canvas_pixbuf_dispose (GnomeCanvasItem *object)
{
    GnomeCanvasItem *item;
    GnomeCanvasPixbuf *gcp;
    GnomeCanvasPixbufPrivate *priv;

    g_return_if_fail (object != NULL);
    g_return_if_fail (GNOME_IS_CANVAS_PIXBUF (object));

    item = GNOME_CANVAS_ITEM (object);
    gcp = GNOME_CANVAS_PIXBUF (object);
    priv = gcp->priv;

    if (priv->pixbuf != NULL) {
        g_object_unref (priv->pixbuf);
        priv->pixbuf = NULL;
    }

    if (GNOME_CANVAS_ITEM_CLASS (gnome_canvas_pixbuf_parent_class)->dispose)
        GNOME_CANVAS_ITEM_CLASS (gnome_canvas_pixbuf_parent_class)->dispose (object);
}



/* Set_property handler for the pixbuf canvas item */
static void
gnome_canvas_pixbuf_set_property (GObject            *object,
                  guint               param_id,
                  const GValue       *value,
                  GParamSpec         *pspec)
{
    GnomeCanvasItem *item;
    GnomeCanvasPixbuf *gcp;
    GnomeCanvasPixbufPrivate *priv;
    GdkPixbuf *pixbuf;

    g_return_if_fail (object != NULL);
    g_return_if_fail (GNOME_IS_CANVAS_PIXBUF (object));

    item = GNOME_CANVAS_ITEM (object);
    gcp = GNOME_CANVAS_PIXBUF (object);
    priv = gcp->priv;

    switch (param_id) {
    case PROP_PIXBUF:
        pixbuf = g_value_get_object (value);
        if (pixbuf != priv->pixbuf) {
            if (priv->pixbuf)
                g_object_unref (priv->pixbuf);

            priv->pixbuf = g_object_ref (pixbuf);
        }

        gnome_canvas_item_request_update (item);
        break;

    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
        break;
    }
}

/* Get_property handler for the pixbuf canvasi item */
static void
gnome_canvas_pixbuf_get_property (GObject            *object,
                  guint               param_id,
                  GValue             *value,
                  GParamSpec         *pspec)
{
    GnomeCanvasPixbuf *gcp;
    GnomeCanvasPixbufPrivate *priv;

    g_return_if_fail (object != NULL);
    g_return_if_fail (GNOME_IS_CANVAS_PIXBUF (object));

    gcp = GNOME_CANVAS_PIXBUF (object);
    priv = gcp->priv;

    switch (param_id) {
    case PROP_PIXBUF:
        g_value_set_object (value, priv->pixbuf);
        break;

    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
        break;
    }
}



/* Bounds and utilities */

/* Recomputes the bounding box of a pixbuf canvas item.  The horizontal and
 * vertical dimensions may be specified in units or pixels, separately, so we
 * have to compute the components individually for each dimension.
 */
static void
recompute_bounding_box (GnomeCanvasPixbuf *gcp)
{
    GnomeCanvasItem *item;
    GnomeCanvasPixbufPrivate *priv;
    cairo_matrix_t i2c;
        double x1, x2, y1, y2;

    item = GNOME_CANVAS_ITEM (gcp);
    priv = gcp->priv;

    if (!priv->pixbuf) {
        item->x1 = item->y1 = item->x2 = item->y2 = 0.0;
        return;
    }

    x1 = 0.0;
    x2 = gdk_pixbuf_get_width (priv->pixbuf);
    y1 = 0.0;
    y2 = gdk_pixbuf_get_height (priv->pixbuf);

    gnome_canvas_item_i2c_matrix (item, &i2c);
    gnome_canvas_matrix_transform_rect (&i2c, &x1, &y1, &x2, &y2);

    item->x1 = floor (x1);
    item->y1 = floor (y1);
    item->x2 = ceil (x2);
    item->y2 = ceil (y2);
}



/* Update sequence */

/* Update handler for the pixbuf canvas item */
static void
gnome_canvas_pixbuf_update (GnomeCanvasItem *item,
                            const cairo_matrix_t *i2c,
                            gint flags)
{
    GnomeCanvasPixbuf *gcp;
    GnomeCanvasPixbufPrivate *priv;

    gcp = GNOME_CANVAS_PIXBUF (item);
    priv = gcp->priv;

    if (GNOME_CANVAS_ITEM_CLASS (gnome_canvas_pixbuf_parent_class)->update)
        GNOME_CANVAS_ITEM_CLASS (gnome_canvas_pixbuf_parent_class)->update (item, i2c, flags);

    /* ordinary update logic */
        gnome_canvas_request_redraw (
        item->canvas, item->x1, item->y1, item->x2, item->y2);
        recompute_bounding_box (gcp);
        gnome_canvas_request_redraw (
        item->canvas, item->x1, item->y1, item->x2, item->y2);
}



/* Draw handler for the pixbuf canvas item */
static void
gnome_canvas_pixbuf_draw (GnomeCanvasItem *item, GdkDrawable *drawable,
              gint x, gint y, gint width, gint height)
{
    GnomeCanvasPixbuf *gcp;
    GnomeCanvasPixbufPrivate *priv;
        cairo_matrix_t matrix;
        cairo_t *cr;

    gcp = GNOME_CANVAS_PIXBUF (item);
    priv = gcp->priv;

    if (!priv->pixbuf)
        return;

        cr = gdk_cairo_create (drawable);
        gnome_canvas_item_i2c_matrix (item, &matrix);
        if (cairo_matrix_invert (&matrix))
                return;
        cairo_transform (cr, &matrix);
        
        gdk_cairo_set_source_pixbuf (cr, priv->pixbuf, 0, 0);
        cairo_paint (cr);

        cairo_destroy (cr);
}



/* Point handler for the pixbuf canvas item */
static GnomeCanvasItem *
gnome_canvas_pixbuf_point (GnomeCanvasItem *item,
                           gdouble x,
                           gdouble y,
                           gint cx,
                           gint cy)
{
    GnomeCanvasPixbuf *gcp;
    GnomeCanvasPixbufPrivate *priv;
        GdkPixbuf *pixbuf;
        int px, py;
        guchar *src;

    gcp = GNOME_CANVAS_PIXBUF (item);
    priv = gcp->priv;
    pixbuf = priv->pixbuf;

    if (!priv->pixbuf)
        return NULL;

        px = x;
        py = y;

    if (px < 0 || px >= gdk_pixbuf_get_width (pixbuf) ||
        py < 0 || py >= gdk_pixbuf_get_height (pixbuf))
        return NULL;

    if (!gdk_pixbuf_get_has_alpha (pixbuf))
        return item;

    src = gdk_pixbuf_get_pixels (pixbuf) +
        py * gdk_pixbuf_get_rowstride (pixbuf) +
        px * gdk_pixbuf_get_n_channels (pixbuf);

    if (src[3] < 128)
        return NULL;
    else
        return item;
}



/* Bounds handler for the pixbuf canvas item */
static void
gnome_canvas_pixbuf_bounds (GnomeCanvasItem *item,
                            gdouble *x1,
                            gdouble *y1,
                            gdouble *x2,
                            gdouble *y2)
{
    GnomeCanvasPixbuf *gcp;
    GnomeCanvasPixbufPrivate *priv;

    gcp = GNOME_CANVAS_PIXBUF (item);
    priv = gcp->priv;

    if (!priv->pixbuf) {
        *x1 = *y1 = *x2 = *y2 = 0.0;
        return;
    }

    *x1 = 0;
    *y1 = 0;
    *x2 = gdk_pixbuf_get_width (priv->pixbuf);
    *y2 = gdk_pixbuf_get_height (priv->pixbuf);
}