Code

Filter effects dialog:
[inkscape.git] / src / display / nr-arena-item.cpp
1 #define __NR_ARENA_ITEM_C__
3 /*
4  * RGBA display list system for inkscape
5  *
6  * Author:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *
9  * Copyright (C) 2001-2002 Lauris Kaplinski
10  * Copyright (C) 2001 Ximian, Inc.
11  *
12  * Released under GNU GPL, read the file 'COPYING' for more information
13  */
15 #define noNR_ARENA_ITEM_VERBOSE
16 #define noNR_ARENA_ITEM_DEBUG_CASCADE
19 #include <libnr/nr-blit.h>
20 #include <libnr/nr-pixops.h>
21 #include "nr-arena.h"
22 #include "nr-arena-item.h"
23 #include "gc-core.h"
25 #include "nr-filter.h"
26 #include "libnr/nr-rect.h"
27 #include "nr-arena-group.h"
28 #include "prefs-utils.h"
30 namespace GC = Inkscape::GC;
32 static void nr_arena_item_class_init (NRArenaItemClass *klass);
33 static void nr_arena_item_init (NRArenaItem *item);
34 static void nr_arena_item_private_finalize (NRObject *object);
36 static NRObjectClass *parent_class;
38 NRType 
39 nr_arena_item_get_type (void)
40 {
41     static NRType type = 0;
42     if (!type) {
43         type = nr_object_register_type (NR_TYPE_OBJECT,
44                                         "NRArenaItem",
45                                         sizeof (NRArenaItemClass),
46                                         sizeof (NRArenaItem),
47                                         (void (*)(NRObjectClass *))
48                                         nr_arena_item_class_init,
49                                         (void (*)(NRObject *))
50                                         nr_arena_item_init);
51     }
52     return type;
53 }
55 static void
56 nr_arena_item_class_init (NRArenaItemClass *klass)
57 {
58     NRObjectClass *object_class;
60     object_class = (NRObjectClass *) klass;
62     parent_class = ((NRObjectClass *) klass)->parent;
64     object_class->finalize = nr_arena_item_private_finalize;
65     object_class->cpp_ctor = NRObject::invoke_ctor < NRArenaItem >;
66 }
68 static void
69 nr_arena_item_init (NRArenaItem *item)
70 {
71     item->arena = NULL;
72     item->parent = NULL;
73     item->next = item->prev = NULL;
75     item->key = 0;
77     item->state = 0;
78     item->sensitive = TRUE;
79     item->visible = TRUE;
81     memset (&item->bbox, 0, sizeof (item->bbox));
82     item->transform = NULL;
83     item->opacity = 255;
84     item->render_opacity = FALSE;
86     item->transform = NULL;
87     item->clip = NULL;
88     item->mask = NULL;
89     item->px = NULL;
90     item->data = NULL;
91     item->filter = NULL;
92     item->background_pb = NULL;
93     item->background_new = false;
94 }
96 static void
97 nr_arena_item_private_finalize (NRObject *object)
98 {
99     NRArenaItem *item = static_cast < NRArenaItem * >(object);
101     item->px = NULL;
102     item->transform = NULL;
104     ((NRObjectClass *) (parent_class))->finalize (object);
107 NRArenaItem *
108 nr_arena_item_children (NRArenaItem *item)
110     nr_return_val_if_fail (item != NULL, NULL);
111     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item), NULL);
113     if (NR_ARENA_ITEM_VIRTUAL (item, children))
114         return NR_ARENA_ITEM_VIRTUAL (item, children) (item);
116     return NULL;
119 NRArenaItem *
120 nr_arena_item_last_child (NRArenaItem *item)
122     nr_return_val_if_fail (item != NULL, NULL);
123     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item), NULL);
125     if (NR_ARENA_ITEM_VIRTUAL (item, last_child)) {
126         return NR_ARENA_ITEM_VIRTUAL (item, last_child) (item);
127     } else {
128         NRArenaItem *ref = nr_arena_item_children (item);
129         if (ref)
130             while (ref->next)
131                 ref = ref->next;
132         return ref;
133     }
136 void
137 nr_arena_item_add_child (NRArenaItem *item, NRArenaItem *child,
138                          NRArenaItem *ref)
140     nr_return_if_fail (item != NULL);
141     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
142     nr_return_if_fail (child != NULL);
143     nr_return_if_fail (NR_IS_ARENA_ITEM (child));
144     nr_return_if_fail (child->parent == NULL);
145     nr_return_if_fail (child->prev == NULL);
146     nr_return_if_fail (child->next == NULL);
147     nr_return_if_fail (child->arena == item->arena);
148     nr_return_if_fail (child != ref);
149     nr_return_if_fail (!ref || NR_IS_ARENA_ITEM (ref));
150     nr_return_if_fail (!ref || (ref->parent == item));
152     if (NR_ARENA_ITEM_VIRTUAL (item, add_child))
153         NR_ARENA_ITEM_VIRTUAL (item, add_child) (item, child, ref);
156 void
157 nr_arena_item_remove_child (NRArenaItem *item, NRArenaItem *child)
159     nr_return_if_fail (item != NULL);
160     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
161     nr_return_if_fail (child != NULL);
162     nr_return_if_fail (NR_IS_ARENA_ITEM (child));
163     nr_return_if_fail (child->parent == item);
165     if (NR_ARENA_ITEM_VIRTUAL (item, remove_child))
166         NR_ARENA_ITEM_VIRTUAL (item, remove_child) (item, child);
169 void
170 nr_arena_item_set_child_position (NRArenaItem *item, NRArenaItem *child,
171                                   NRArenaItem *ref)
173     nr_return_if_fail (item != NULL);
174     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
175     nr_return_if_fail (child != NULL);
176     nr_return_if_fail (NR_IS_ARENA_ITEM (child));
177     nr_return_if_fail (child->parent == item);
178     nr_return_if_fail (!ref || NR_IS_ARENA_ITEM (ref));
179     nr_return_if_fail (!ref || (ref->parent == item));
181     if (NR_ARENA_ITEM_VIRTUAL (item, set_child_position))
182         NR_ARENA_ITEM_VIRTUAL (item, set_child_position) (item, child, ref);
185 NRArenaItem *
186 nr_arena_item_ref (NRArenaItem *item)
188     nr_object_ref ((NRObject *) item);
190     return item;
193 NRArenaItem *
194 nr_arena_item_unref (NRArenaItem *item)
196     nr_object_unref ((NRObject *) item);
198     return NULL;
201 unsigned int
202 nr_arena_item_invoke_update (NRArenaItem *item, NRRectL *area, NRGC *gc,
203                              unsigned int state, unsigned int reset)
205     NRGC childgc (gc);
207     nr_return_val_if_fail (item != NULL, NR_ARENA_ITEM_STATE_INVALID);
208     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item),
209                            NR_ARENA_ITEM_STATE_INVALID);
210     nr_return_val_if_fail (!(state & NR_ARENA_ITEM_STATE_INVALID),
211                            NR_ARENA_ITEM_STATE_INVALID);
213 #ifdef NR_ARENA_ITEM_DEBUG_CASCADE
214     printf ("Update %s:%p %x %x %x\n",
215             nr_type_name_from_instance ((GTypeInstance *) item), item, state,
216             item->state, reset);
217 #endif
219     /* return if in error */
220     if (item->state & NR_ARENA_ITEM_STATE_INVALID)
221         return item->state;
222     /* Set reset flags according to propagation status */
223     if (item->propagate) {
224         reset |= ~item->state;
225         item->propagate = FALSE;
226     }
227     /* Reset our state */
228     item->state &= ~reset;
229     /* Return if NOP */
230     if (!(~item->state & state))
231         return item->state;
232     /* Test whether to return immediately */
233     if (area && (item->state & NR_ARENA_ITEM_STATE_BBOX)) {
234         if (!nr_rect_l_test_intersect (area, &item->bbox))
235             return item->state;
236     }
238     /* Reset image cache, if not to be kept */
239     if (!(item->state & NR_ARENA_ITEM_STATE_IMAGE) && (item->px)) {
240         item->px = NULL;
241     }
243     /* Set up local gc */
244     childgc = *gc;
245     if (item->transform) {
246         nr_matrix_multiply (&childgc.transform, item->transform,
247                             &childgc.transform);
248     }
249     /* Remember the transformation matrix */
250     item->ctm = childgc.transform;
252     /* Invoke the real method */
253     item->state =
254         NR_ARENA_ITEM_VIRTUAL (item, update) (item, area, &childgc, state,
255                                               reset);
256     if (item->state & NR_ARENA_ITEM_STATE_INVALID)
257         return item->state;
258     /* Enlarge the bounding box to contain filter effects */
259     if (item->filter) {
260         item->filter->bbox_enlarge (item->bbox);
261     }
262     // fixme: to fix the display glitches, in outline mode bbox must be a combination of 
263     // full item bbox and its clip and mask (after we have the API to get these)
265     /* Clipping */
266     if (item->clip) {
267         // FIXME: since here we only need bbox, consider passing 
268         // ((state & !(NR_ARENA_ITEM_STATE_RENDER)) | NR_ARENA_ITEM_STATE_BBOX)
269         // instead of state, so it does not have to create rendering structures in nr_arena_shape_update
270         unsigned int newstate = nr_arena_item_invoke_update (item->clip, area, &childgc, state, reset); 
271         if (newstate & NR_ARENA_ITEM_STATE_INVALID) {
272             item->state |= NR_ARENA_ITEM_STATE_INVALID;
273             return item->state;
274         }
275         nr_rect_l_intersect (&item->bbox, &item->bbox, &item->clip->bbox);
276     }
277     /* Masking */
278     if (item->mask) {
279         unsigned int newstate = nr_arena_item_invoke_update (item->mask, area, &childgc, state, reset);
280         if (newstate & NR_ARENA_ITEM_STATE_INVALID) {
281             item->state |= NR_ARENA_ITEM_STATE_INVALID;
282             return item->state;
283         }
284         nr_rect_l_intersect (&item->bbox, &item->bbox, &item->mask->bbox);
285     }
287     return item->state;
290 /**
291  *    Render item to pixblock.
292  *
293  *    \return Has NR_ARENA_ITEM_STATE_RENDER set on success.
294  */
296 unsigned int
297 nr_arena_item_invoke_render (cairo_t *ct, NRArenaItem *item, NRRectL const *area,
298                              NRPixBlock *pb, unsigned int flags)
300    bool outline = (item->arena->rendermode == RENDERMODE_OUTLINE);
302     nr_return_val_if_fail (item != NULL, NR_ARENA_ITEM_STATE_INVALID);
303     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item),
304                            NR_ARENA_ITEM_STATE_INVALID);
305     nr_return_val_if_fail (item->state & NR_ARENA_ITEM_STATE_BBOX,
306                            item->state);
308 #ifdef NR_ARENA_ITEM_VERBOSE
309     printf ("Invoke render %p: %d %d - %d %d\n", item, area->x0, area->y0,
310             area->x1, area->y1);
311 #endif
313     /* If we are outside bbox just return successfully */
314     if (!item->visible)
315         return item->state | NR_ARENA_ITEM_STATE_RENDER;
317     NRRectL carea;
318     nr_rect_l_intersect (&carea, area, &item->bbox);
319     if (nr_rect_l_test_empty (&carea))
320         return item->state | NR_ARENA_ITEM_STATE_RENDER;
321     if (item->filter && !outline) {
322         item->filter->area_enlarge (carea, item->ctm);
323         nr_rect_l_intersect (&carea, &carea, &item->bbox);
324     }
326     if (outline) {
327     // No caching in outline mode for now; investigate if it really gives any advantage with cairo.
328     // Also no attempts to clip anything; just render everything: item, clip, mask   
329             // First, render the object itself 
330             unsigned int state = NR_ARENA_ITEM_VIRTUAL (item, render) (ct, item, &carea, pb, flags);
331             if (state & NR_ARENA_ITEM_STATE_INVALID) {
332                 /* Clean up and return error */
333                 item->state |= NR_ARENA_ITEM_STATE_INVALID;
334                 return item->state;
335             }
337             // render clip and mask, if any
338             guint32 saved_rgba = item->arena->outlinecolor; // save current outline color
339             // render clippath as an object, using a different color
340             if (item->clip) {
341                 item->arena->outlinecolor = prefs_get_int_attribute("options.wireframecolors", "clips", 0x00ff00ff); // green clips
342                 NR_ARENA_ITEM_VIRTUAL (item->clip, render) (ct, item->clip, &carea, pb, flags);
343             } 
344             // render mask as an object, using a different color
345             if (item->mask) {
346                 item->arena->outlinecolor = prefs_get_int_attribute("options.wireframecolors", "masks", 0x0000ffff); // blue masks
347                 NR_ARENA_ITEM_VIRTUAL (item->mask, render) (ct, item->mask, &carea, pb, flags);
348             }
349             item->arena->outlinecolor = saved_rgba; // restore outline color
351             return item->state | NR_ARENA_ITEM_STATE_RENDER;
352     }
354     NRPixBlock cpb;
355     if (item->px) {
356         /* Has cache pixblock, render this and return */
357         nr_pixblock_setup_extern (&cpb, NR_PIXBLOCK_MODE_R8G8B8A8P,
358                                   /* fixme: This probably cannot overflow, because we render only if visible */
359                                   /* fixme: and pixel cache is there only for small items */
360                                   /* fixme: But this still needs extra check (Lauris) */
361                                   item->bbox.x0, item->bbox.y0,
362                                   item->bbox.x1, item->bbox.y1,
363                                   item->px,
364                                   4 * (item->bbox.x1 - item->bbox.x0), FALSE,
365                                   FALSE);
366         nr_blit_pixblock_pixblock (pb, &cpb);
367         nr_pixblock_release (&cpb);
368         pb->empty = FALSE;
369         return item->state | NR_ARENA_ITEM_STATE_RENDER;
370     }
372     NRPixBlock *dpb = pb;
374     /* Setup cache if we can */
375     if ((!(flags & NR_ARENA_ITEM_RENDER_NO_CACHE)) &&
376         (carea.x0 <= item->bbox.x0) && (carea.y0 <= item->bbox.y0) &&
377         (carea.x1 >= item->bbox.x1) && (carea.y1 >= item->bbox.y1) &&
378         (((item->bbox.x1 - item->bbox.x0) * (item->bbox.y1 -
379                                              item->bbox.y0)) <= 4096)) {
380         // Item bbox is fully in renderable area and size is acceptable
381         carea.x0 = item->bbox.x0;
382         carea.y0 = item->bbox.y0;
383         carea.x1 = item->bbox.x1;
384         carea.y1 = item->bbox.y1;
385         item->px =
386             new (GC::ATOMIC) unsigned char[4 * (carea.x1 - carea.x0) *
387                                            (carea.y1 - carea.y0)];
388         nr_pixblock_setup_extern (&cpb, NR_PIXBLOCK_MODE_R8G8B8A8P, carea.x0,
389                                   carea.y0, carea.x1, carea.y1, item->px,
390                                   4 * (carea.x1 - carea.x0), TRUE, TRUE);
391         cpb.visible_area = pb->visible_area;
392         dpb = &cpb;
393         // Set nocache flag for downstream rendering
394         flags |= NR_ARENA_ITEM_RENDER_NO_CACHE;
395     } 
397     /* Determine, whether we need temporary buffer */
398     if (item->clip || item->mask
399         || ((item->opacity != 255) && !item->render_opacity)
400         || (item->filter) || item->background_new
401         || (item->parent && item->parent->background_pb)) {
403         /* Setup and render item buffer */
404         NRPixBlock ipb;
405         nr_pixblock_setup_fast (&ipb, NR_PIXBLOCK_MODE_R8G8B8A8P,
406                                 carea.x0, carea.y0, carea.x1, carea.y1,
407                                 TRUE);
409         //  if memory allocation failed, abort render
410         if (ipb.size != NR_PIXBLOCK_SIZE_TINY && ipb.data.px == NULL) {
411             nr_pixblock_release (&ipb);
412             return (item->state);
413         }
415         /* If background access is used, save the pixblock address.
416          * This address is set to NULL at the end of this block */
417         if (item->background_new ||
418             (item->parent && item->parent->background_pb)) {
419             item->background_pb = &ipb;
420         }
421         ipb.visible_area = pb->visible_area;
422         unsigned int state = NR_ARENA_ITEM_VIRTUAL (item, render) (ct, item, &carea, &ipb, flags);
423         if (state & NR_ARENA_ITEM_STATE_INVALID) {
424             /* Clean up and return error */
425             nr_pixblock_release (&ipb);
426             if (dpb != pb)
427                 nr_pixblock_release (dpb);
428             item->state |= NR_ARENA_ITEM_STATE_INVALID;
429             return item->state;
430         }
431         ipb.empty = FALSE;
433         /* Run filtering, if a filter is set for this object */
434         if (item->filter) {
435             item->filter->render (item, &ipb);
436         }
438         if (item->clip || item->mask) {
439             /* Setup mask pixblock */
440             NRPixBlock mpb;
441             nr_pixblock_setup_fast (&mpb, NR_PIXBLOCK_MODE_A8, carea.x0,
442                                     carea.y0, carea.x1, carea.y1, TRUE);
444             if (mpb.data.px != NULL) { // if memory allocation was successful
446                 mpb.visible_area = pb->visible_area;
447                 /* Do clip if needed */
448                 if (item->clip) {
449                     state = nr_arena_item_invoke_clip (item->clip, &carea, &mpb);
450                     if (state & NR_ARENA_ITEM_STATE_INVALID) {
451                         /* Clean up and return error */
452                         nr_pixblock_release (&mpb);
453                         nr_pixblock_release (&ipb);
454                         if (dpb != pb)
455                             nr_pixblock_release (dpb);
456                         item->state |= NR_ARENA_ITEM_STATE_INVALID;
457                         return item->state;
458                     }
459                     mpb.empty = FALSE;
460                 }
461                 /* Do mask if needed */
462                 if (item->mask) {
463                     NRPixBlock tpb;
464                     /* Set up yet another temporary pixblock */
465                     nr_pixblock_setup_fast (&tpb, NR_PIXBLOCK_MODE_R8G8B8A8N,
466                                             carea.x0, carea.y0, carea.x1,
467                                             carea.y1, TRUE);
469                     if (tpb.data.px != NULL) { // if memory allocation was successful
471                         tpb.visible_area = pb->visible_area;
472                         unsigned int state = NR_ARENA_ITEM_VIRTUAL (item->mask, render) (ct, item->mask, &carea, &tpb, flags);
473                         if (state & NR_ARENA_ITEM_STATE_INVALID) {
474                             /* Clean up and return error */
475                             nr_pixblock_release (&tpb);
476                             nr_pixblock_release (&mpb);
477                             nr_pixblock_release (&ipb);
478                             if (dpb != pb)
479                                 nr_pixblock_release (dpb);
480                             item->state |= NR_ARENA_ITEM_STATE_INVALID;
481                             return item->state;
482                         }
483                         /* Composite with clip */
484                         if (item->clip) {
485                             int x, y;
486                             for (y = carea.y0; y < carea.y1; y++) {
487                                 unsigned char *s, *d;
488                                 s = NR_PIXBLOCK_PX (&tpb) + (y -
489                                                              carea.y0) * tpb.rs;
490                                 d = NR_PIXBLOCK_PX (&mpb) + (y -
491                                                              carea.y0) * mpb.rs;
492                                 for (x = carea.x0; x < carea.x1; x++) {
493                                     unsigned int m;
494                                     m = NR_PREMUL_112 (s[0] + s[1] + s[2], s[3]);
495                                     d[0] =
496                                         FAST_DIV_ROUND < 3 * 255 * 255 >
497                                         (NR_PREMUL_123 (d[0], m));
498                                     s += 4;
499                                     d += 1;
500                                 }
501                             }
502                         } else {
503                             int x, y;
504                             for (y = carea.y0; y < carea.y1; y++) {
505                                 unsigned char *s, *d;
506                                 s = NR_PIXBLOCK_PX (&tpb) + (y -
507                                                              carea.y0) * tpb.rs;
508                                 d = NR_PIXBLOCK_PX (&mpb) + (y -
509                                                              carea.y0) * mpb.rs;
510                                 for (x = carea.x0; x < carea.x1; x++) {
511                                     unsigned int m;
512                                     m = NR_PREMUL_112 (s[0] + s[1] + s[2], s[3]);
513                                     d[0] = FAST_DIV_ROUND < 3 * 255 > (m);
514                                     s += 4;
515                                     d += 1;
516                                 }
517                             }
518                             mpb.empty = FALSE;
519                         }
520                     }
521                     nr_pixblock_release (&tpb);
522                 }
523                 /* Multiply with opacity if needed */
524                 if ((item->opacity != 255) && !item->render_opacity
525                     ) {
526                     int x, y;
527                     unsigned int a;
528                     a = item->opacity;
529                     for (y = carea.y0; y < carea.y1; y++) {
530                         unsigned char *d;
531                         d = NR_PIXBLOCK_PX (&mpb) + (y - carea.y0) * mpb.rs;
532                         for (x = carea.x0; x < carea.x1; x++) {
533                             d[0] = NR_PREMUL_111 (d[0], a);
534                             d += 1;
535                         }
536                     }
537                 }
538                 /* Compose rendering pixblock int destination */
539                 nr_blit_pixblock_pixblock_mask (dpb, &ipb, &mpb);
540             }
541             nr_pixblock_release (&mpb);
542         } else {
543             if (item->render_opacity) { // opacity was already rendered in, just copy to dpb here
544                 nr_blit_pixblock_pixblock(dpb, &ipb);
545             } else { // copy while multiplying by opacity
546                 nr_blit_pixblock_pixblock_alpha (dpb, &ipb, item->opacity);
547             }
548         }
549         nr_pixblock_release (&ipb);
550         dpb->empty = FALSE;
551         /* This pointer wouldn't be valid outside this block, so clear it */
552         item->background_pb = NULL;
553     } else {
554         /* Just render */
555         unsigned int state = NR_ARENA_ITEM_VIRTUAL (item, render) (ct, item, &carea, dpb, flags);
556         if (state & NR_ARENA_ITEM_STATE_INVALID) {
557             /* Clean up and return error */
558             if (dpb != pb)
559                 nr_pixblock_release (dpb);
560             item->state |= NR_ARENA_ITEM_STATE_INVALID;
561             return item->state;
562         }
563         dpb->empty = FALSE;
564     }
566     if (dpb != pb) {
567         /* Have to blit from cache */
568         nr_blit_pixblock_pixblock (pb, dpb);
569         nr_pixblock_release (dpb);
570         pb->empty = FALSE;
571         item->state |= NR_ARENA_ITEM_STATE_IMAGE;
572     }
574     return item->state | NR_ARENA_ITEM_STATE_RENDER;
577 unsigned int
578 nr_arena_item_invoke_clip (NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
580     nr_return_val_if_fail (item != NULL, NR_ARENA_ITEM_STATE_INVALID);
581     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item),
582                            NR_ARENA_ITEM_STATE_INVALID);
583     /* we originally short-circuited if the object state included
584      * NR_ARENA_ITEM_STATE_CLIP (and showed a warning on the console);
585      * anyone know why we stopped doing so?
586      */
587     nr_return_val_if_fail ((pb->area.x1 - pb->area.x0) >=
588                            (area->x1 - area->x0),
589                            NR_ARENA_ITEM_STATE_INVALID);
590     nr_return_val_if_fail ((pb->area.y1 - pb->area.y0) >=
591                            (area->y1 - area->y0),
592                            NR_ARENA_ITEM_STATE_INVALID);
594 #ifdef NR_ARENA_ITEM_VERBOSE
595     printf ("Invoke clip by %p: %d %d - %d %d, item bbox %d %d - %d %d\n",
596             item, area->x0, area->y0, area->x1, area->y1, (&item->bbox)->x0,
597             (&item->bbox)->y0, (&item->bbox)->x1, (&item->bbox)->y1);
598 #endif
600     if (item->visible && nr_rect_l_test_intersect (area, &item->bbox)) {
601         /* Need render that item */
602         if (((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->clip) {
603             return ((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->
604                 clip (item, area, pb);
605         }
606     }
608     return item->state;
611 NRArenaItem *
612 nr_arena_item_invoke_pick (NRArenaItem *item, NR::Point p, double delta,
613                            unsigned int sticky)
615     nr_return_val_if_fail (item != NULL, NULL);
616     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item), NULL);
618     // Sometimes there's no BBOX in item->state, reason unknown (bug 992817); I made this not an assert to remove the warning
619     if (!(item->state & NR_ARENA_ITEM_STATE_BBOX)
620         || !(item->state & NR_ARENA_ITEM_STATE_PICK))
621         return NULL;
623     if (!sticky && !(item->visible && item->sensitive))
624         return NULL;
626     // TODO: rewrite using NR::Rect
627     const double x = p[NR::X];
628     const double y = p[NR::Y];
630     if (((x + delta) >= item->bbox.x0) &&
631         ((x - delta) < item->bbox.x1) &&
632         ((y + delta) >= item->bbox.y0) && ((y - delta) < item->bbox.y1)) {
633         if (((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->pick)
634             return ((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->
635                 pick (item, p, delta, sticky);
636     }
638     return NULL;
641 void
642 nr_arena_item_request_update (NRArenaItem *item, unsigned int reset,
643                               unsigned int propagate)
645     nr_return_if_fail (item != NULL);
646     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
647     nr_return_if_fail (!(reset & NR_ARENA_ITEM_STATE_INVALID));
649     if (propagate && !item->propagate)
650         item->propagate = TRUE;
652     if (item->state & reset) {
653         item->state &= ~reset;
654         if (item->parent) {
655             nr_arena_item_request_update (item->parent, reset, FALSE);
656         } else {
657             nr_arena_request_update (item->arena, item);
658         }
659     }
662 void
663 nr_arena_item_request_render (NRArenaItem *item)
665     nr_return_if_fail (item != NULL);
666     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
668     nr_arena_request_render_rect (item->arena, &item->bbox);
671 /* Public */
673 NRArenaItem *
674 nr_arena_item_unparent (NRArenaItem *item)
676     nr_return_val_if_fail (item != NULL, NULL);
677     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item), NULL);
679     nr_arena_item_request_render (item);
681     if (item->parent) {
682         nr_arena_item_remove_child (item->parent, item);
683     }
685     return NULL;
688 void
689 nr_arena_item_append_child (NRArenaItem *parent, NRArenaItem *child)
691     nr_return_if_fail (parent != NULL);
692     nr_return_if_fail (NR_IS_ARENA_ITEM (parent));
693     nr_return_if_fail (child != NULL);
694     nr_return_if_fail (NR_IS_ARENA_ITEM (child));
695     nr_return_if_fail (parent->arena == child->arena);
696     nr_return_if_fail (child->parent == NULL);
697     nr_return_if_fail (child->prev == NULL);
698     nr_return_if_fail (child->next == NULL);
700     nr_arena_item_add_child (parent, child, nr_arena_item_last_child (parent));
703 void
704 nr_arena_item_set_transform (NRArenaItem *item, NR::Matrix const &transform)
706     NRMatrix const t (transform);
707     nr_arena_item_set_transform (item, &t);
710 void
711 nr_arena_item_set_transform (NRArenaItem *item, NRMatrix const *transform)
713     nr_return_if_fail (item != NULL);
714     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
716     if (!transform && !item->transform)
717         return;
719     const NRMatrix *md = (item->transform) ? item->transform : &NR_MATRIX_IDENTITY;
720     const NRMatrix *ms = (transform) ? transform : &NR_MATRIX_IDENTITY;
722     if (!NR_MATRIX_DF_TEST_CLOSE (md, ms, NR_EPSILON)) {
723         nr_arena_item_request_render (item);
724         if (!transform || nr_matrix_test_identity (transform, NR_EPSILON)) {
725             /* Set to identity affine */
726             item->transform = NULL;
727         } else {
728             if (!item->transform)
729                 item->transform = new (GC::ATOMIC) NRMatrix ();
730             *item->transform = *transform;
731         }
732         nr_arena_item_request_update (item, NR_ARENA_ITEM_STATE_ALL, TRUE);
733     }
736 void
737 nr_arena_item_set_opacity (NRArenaItem *item, double opacity)
739     nr_return_if_fail (item != NULL);
740     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
742     nr_arena_item_request_render (item);
744     item->opacity = (unsigned int) (opacity * 255.9999);
747 void
748 nr_arena_item_set_sensitive (NRArenaItem *item, unsigned int sensitive)
750     nr_return_if_fail (item != NULL);
751     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
753     /* fixme: mess with pick/repick... */
755     item->sensitive = sensitive;
758 void
759 nr_arena_item_set_visible (NRArenaItem *item, unsigned int visible)
761     nr_return_if_fail (item != NULL);
762     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
764     item->visible = visible;
766     nr_arena_item_request_render (item);
769 void
770 nr_arena_item_set_clip (NRArenaItem *item, NRArenaItem *clip)
772     nr_return_if_fail (item != NULL);
773     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
774     nr_return_if_fail (!clip || NR_IS_ARENA_ITEM (clip));
776     if (clip != item->clip) {
777         nr_arena_item_request_render (item);
778         if (item->clip)
779             item->clip = nr_arena_item_detach (item, item->clip);
780         if (clip)
781             item->clip = nr_arena_item_attach (item, clip, NULL, NULL);
782         nr_arena_item_request_update (item, NR_ARENA_ITEM_STATE_ALL, TRUE);
783     }
786 void
787 nr_arena_item_set_mask (NRArenaItem *item, NRArenaItem *mask)
789     nr_return_if_fail (item != NULL);
790     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
791     nr_return_if_fail (!mask || NR_IS_ARENA_ITEM (mask));
793     if (mask != item->mask) {
794         nr_arena_item_request_render (item);
795         if (item->mask)
796             item->mask = nr_arena_item_detach (item, item->mask);
797         if (mask)
798             item->mask = nr_arena_item_attach (item, mask, NULL, NULL);
799         nr_arena_item_request_update (item, NR_ARENA_ITEM_STATE_ALL, TRUE);
800     }
803 void
804 nr_arena_item_set_order (NRArenaItem *item, int order)
806     nr_return_if_fail (item != NULL);
807     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
809     if (!item->parent)
810         return;
812     NRArenaItem *children = nr_arena_item_children (item->parent);
814     NRArenaItem *ref = NULL;
815     int pos = 0;
816     for (NRArenaItem *child = children; child != NULL; child = child->next) {
817         if (pos >= order)
818             break;
819         if (child != item) {
820             ref = child;
821             pos += 1;
822         }
823     }
825     nr_arena_item_set_child_position (item->parent, item, ref);
828 /** Returns a background image for use with filter effects. */
829 NRPixBlock *
830 nr_arena_item_get_background (NRArenaItem const *item, int depth)
832     NRPixBlock *pb;
833     if (!item->background_pb)
834         return NULL;
835     if (item->background_new) {
836         pb = new NRPixBlock ();
837         nr_pixblock_setup_fast (pb, item->background_pb->mode,
838                                 item->background_pb->area.x0,
839                                 item->background_pb->area.y0,
840                                 item->background_pb->area.x1,
841                                 item->background_pb->area.y1, true);
842         if (pb->size != NR_PIXBLOCK_SIZE_TINY && pb->data.px == NULL) // allocation failed
843             return NULL;
844     } else if (item->parent) {
845         pb = nr_arena_item_get_background (item->parent, depth + 1);
846     } else
847         return NULL;
849     if (depth > 0)
850         nr_blit_pixblock_pixblock (pb, item->background_pb);
852     return pb;
855 /* Helpers */
857 NRArenaItem *
858 nr_arena_item_attach (NRArenaItem *parent, NRArenaItem *child,
859                       NRArenaItem *prev, NRArenaItem *next)
861     nr_return_val_if_fail (parent != NULL, NULL);
862     nr_return_val_if_fail (NR_IS_ARENA_ITEM (parent), NULL);
863     nr_return_val_if_fail (child != NULL, NULL);
864     nr_return_val_if_fail (NR_IS_ARENA_ITEM (child), NULL);
865     nr_return_val_if_fail (child->parent == NULL, NULL);
866     nr_return_val_if_fail (child->prev == NULL, NULL);
867     nr_return_val_if_fail (child->next == NULL, NULL);
868     nr_return_val_if_fail (!prev || NR_IS_ARENA_ITEM (prev), NULL);
869     nr_return_val_if_fail (!prev || (prev->parent == parent), NULL);
870     nr_return_val_if_fail (!prev || (prev->next == next), NULL);
871     nr_return_val_if_fail (!next || NR_IS_ARENA_ITEM (next), NULL);
872     nr_return_val_if_fail (!next || (next->parent == parent), NULL);
873     nr_return_val_if_fail (!next || (next->prev == prev), NULL);
875     child->parent = parent;
876     child->prev = prev;
877     child->next = next;
879     if (prev)
880         prev->next = child;
881     if (next)
882         next->prev = child;
884     return child;
887 NRArenaItem *
888 nr_arena_item_detach (NRArenaItem *parent, NRArenaItem *child)
890     nr_return_val_if_fail (parent != NULL, NULL);
891     nr_return_val_if_fail (NR_IS_ARENA_ITEM (parent), NULL);
892     nr_return_val_if_fail (child != NULL, NULL);
893     nr_return_val_if_fail (NR_IS_ARENA_ITEM (child), NULL);
894     nr_return_val_if_fail (child->parent == parent, NULL);
896     NRArenaItem *prev = child->prev;
897     NRArenaItem *next = child->next;
899     child->parent = NULL;
900     child->prev = NULL;
901     child->next = NULL;
903     if (prev)
904         prev->next = next;
905     if (next)
906         next->prev = prev;
908     return next;
911 /*
912   Local Variables:
913   mode:c++
914   c-file-style:"stroustrup"
915   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
916   indent-tabs-mode:nil
917   fill-column:99
918   End:
919 */
920 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :