Code

Remove 'default scale origin' from selector tool's preferences
[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         nr_rect_l_enlarge (&carea, item->filter->get_enlarge (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             /* This pointer wouldn't be valid outside this block, so clear it */
543             item->background_pb = NULL;
545         } else {
546             if (item->render_opacity) { // opacity was already rendered in, just copy to dpb here
547                 nr_blit_pixblock_pixblock(dpb, &ipb);
548             } else { // copy while multiplying by opacity
549                 nr_blit_pixblock_pixblock_alpha (dpb, &ipb, item->opacity);
550             }
551         }
552         nr_pixblock_release (&ipb);
553         dpb->empty = FALSE;
554     } else {
555         /* Just render */
556         unsigned int state = NR_ARENA_ITEM_VIRTUAL (item, render) (ct, item, &carea, dpb, flags);
557         if (state & NR_ARENA_ITEM_STATE_INVALID) {
558             /* Clean up and return error */
559             if (dpb != pb)
560                 nr_pixblock_release (dpb);
561             item->state |= NR_ARENA_ITEM_STATE_INVALID;
562             return item->state;
563         }
564         dpb->empty = FALSE;
565     }
567     if (dpb != pb) {
568         /* Have to blit from cache */
569         nr_blit_pixblock_pixblock (pb, dpb);
570         nr_pixblock_release (dpb);
571         pb->empty = FALSE;
572         item->state |= NR_ARENA_ITEM_STATE_IMAGE;
573     }
575     return item->state | NR_ARENA_ITEM_STATE_RENDER;
578 unsigned int
579 nr_arena_item_invoke_clip (NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
581     nr_return_val_if_fail (item != NULL, NR_ARENA_ITEM_STATE_INVALID);
582     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item),
583                            NR_ARENA_ITEM_STATE_INVALID);
584     /* we originally short-circuited if the object state included
585      * NR_ARENA_ITEM_STATE_CLIP (and showed a warning on the console);
586      * anyone know why we stopped doing so?
587      */
588     nr_return_val_if_fail ((pb->area.x1 - pb->area.x0) >=
589                            (area->x1 - area->x0),
590                            NR_ARENA_ITEM_STATE_INVALID);
591     nr_return_val_if_fail ((pb->area.y1 - pb->area.y0) >=
592                            (area->y1 - area->y0),
593                            NR_ARENA_ITEM_STATE_INVALID);
595 #ifdef NR_ARENA_ITEM_VERBOSE
596     printf ("Invoke clip by %p: %d %d - %d %d, item bbox %d %d - %d %d\n",
597             item, area->x0, area->y0, area->x1, area->y1, (&item->bbox)->x0,
598             (&item->bbox)->y0, (&item->bbox)->x1, (&item->bbox)->y1);
599 #endif
601     if (item->visible && nr_rect_l_test_intersect (area, &item->bbox)) {
602         /* Need render that item */
603         if (((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->clip) {
604             return ((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->
605                 clip (item, area, pb);
606         }
607     }
609     return item->state;
612 NRArenaItem *
613 nr_arena_item_invoke_pick (NRArenaItem *item, NR::Point p, double delta,
614                            unsigned int sticky)
616     nr_return_val_if_fail (item != NULL, NULL);
617     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item), NULL);
619     // Sometimes there's no BBOX in item->state, reason unknown (bug 992817); I made this not an assert to remove the warning
620     if (!(item->state & NR_ARENA_ITEM_STATE_BBOX)
621         || !(item->state & NR_ARENA_ITEM_STATE_PICK))
622         return NULL;
624     if (!sticky && !(item->visible && item->sensitive))
625         return NULL;
627     // TODO: rewrite using NR::Rect
628     const double x = p[NR::X];
629     const double y = p[NR::Y];
631     if (((x + delta) >= item->bbox.x0) &&
632         ((x - delta) < item->bbox.x1) &&
633         ((y + delta) >= item->bbox.y0) && ((y - delta) < item->bbox.y1)) {
634         if (((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->pick)
635             return ((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->
636                 pick (item, p, delta, sticky);
637     }
639     return NULL;
642 void
643 nr_arena_item_request_update (NRArenaItem *item, unsigned int reset,
644                               unsigned int propagate)
646     nr_return_if_fail (item != NULL);
647     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
648     nr_return_if_fail (!(reset & NR_ARENA_ITEM_STATE_INVALID));
650     if (propagate && !item->propagate)
651         item->propagate = TRUE;
653     if (item->state & reset) {
654         item->state &= ~reset;
655         if (item->parent) {
656             nr_arena_item_request_update (item->parent, reset, FALSE);
657         } else {
658             nr_arena_request_update (item->arena, item);
659         }
660     }
663 void
664 nr_arena_item_request_render (NRArenaItem *item)
666     nr_return_if_fail (item != NULL);
667     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
669     nr_arena_request_render_rect (item->arena, &item->bbox);
672 /* Public */
674 NRArenaItem *
675 nr_arena_item_unparent (NRArenaItem *item)
677     nr_return_val_if_fail (item != NULL, NULL);
678     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item), NULL);
680     nr_arena_item_request_render (item);
682     if (item->parent) {
683         nr_arena_item_remove_child (item->parent, item);
684     }
686     return NULL;
689 void
690 nr_arena_item_append_child (NRArenaItem *parent, NRArenaItem *child)
692     nr_return_if_fail (parent != NULL);
693     nr_return_if_fail (NR_IS_ARENA_ITEM (parent));
694     nr_return_if_fail (child != NULL);
695     nr_return_if_fail (NR_IS_ARENA_ITEM (child));
696     nr_return_if_fail (parent->arena == child->arena);
697     nr_return_if_fail (child->parent == NULL);
698     nr_return_if_fail (child->prev == NULL);
699     nr_return_if_fail (child->next == NULL);
701     nr_arena_item_add_child (parent, child, nr_arena_item_last_child (parent));
704 void
705 nr_arena_item_set_transform (NRArenaItem *item, NR::Matrix const &transform)
707     NRMatrix const t (transform);
708     nr_arena_item_set_transform (item, &t);
711 void
712 nr_arena_item_set_transform (NRArenaItem *item, NRMatrix const *transform)
714     nr_return_if_fail (item != NULL);
715     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
717     if (!transform && !item->transform)
718         return;
720     const NRMatrix *md = (item->transform) ? item->transform : &NR_MATRIX_IDENTITY;
721     const NRMatrix *ms = (transform) ? transform : &NR_MATRIX_IDENTITY;
723     if (!NR_MATRIX_DF_TEST_CLOSE (md, ms, NR_EPSILON)) {
724         nr_arena_item_request_render (item);
725         if (!transform || nr_matrix_test_identity (transform, NR_EPSILON)) {
726             /* Set to identity affine */
727             item->transform = NULL;
728         } else {
729             if (!item->transform)
730                 item->transform = new (GC::ATOMIC) NRMatrix ();
731             *item->transform = *transform;
732         }
733         nr_arena_item_request_update (item, NR_ARENA_ITEM_STATE_ALL, TRUE);
734     }
737 void
738 nr_arena_item_set_opacity (NRArenaItem *item, double opacity)
740     nr_return_if_fail (item != NULL);
741     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
743     nr_arena_item_request_render (item);
745     item->opacity = (unsigned int) (opacity * 255.9999);
748 void
749 nr_arena_item_set_sensitive (NRArenaItem *item, unsigned int sensitive)
751     nr_return_if_fail (item != NULL);
752     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
754     /* fixme: mess with pick/repick... */
756     item->sensitive = sensitive;
759 void
760 nr_arena_item_set_visible (NRArenaItem *item, unsigned int visible)
762     nr_return_if_fail (item != NULL);
763     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
765     item->visible = visible;
767     nr_arena_item_request_render (item);
770 void
771 nr_arena_item_set_clip (NRArenaItem *item, NRArenaItem *clip)
773     nr_return_if_fail (item != NULL);
774     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
775     nr_return_if_fail (!clip || NR_IS_ARENA_ITEM (clip));
777     if (clip != item->clip) {
778         nr_arena_item_request_render (item);
779         if (item->clip)
780             item->clip = nr_arena_item_detach (item, item->clip);
781         if (clip)
782             item->clip = nr_arena_item_attach (item, clip, NULL, NULL);
783         nr_arena_item_request_update (item, NR_ARENA_ITEM_STATE_ALL, TRUE);
784     }
787 void
788 nr_arena_item_set_mask (NRArenaItem *item, NRArenaItem *mask)
790     nr_return_if_fail (item != NULL);
791     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
792     nr_return_if_fail (!mask || NR_IS_ARENA_ITEM (mask));
794     if (mask != item->mask) {
795         nr_arena_item_request_render (item);
796         if (item->mask)
797             item->mask = nr_arena_item_detach (item, item->mask);
798         if (mask)
799             item->mask = nr_arena_item_attach (item, mask, NULL, NULL);
800         nr_arena_item_request_update (item, NR_ARENA_ITEM_STATE_ALL, TRUE);
801     }
804 void
805 nr_arena_item_set_order (NRArenaItem *item, int order)
807     nr_return_if_fail (item != NULL);
808     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
810     if (!item->parent)
811         return;
813     NRArenaItem *children = nr_arena_item_children (item->parent);
815     NRArenaItem *ref = NULL;
816     int pos = 0;
817     for (NRArenaItem *child = children; child != NULL; child = child->next) {
818         if (pos >= order)
819             break;
820         if (child != item) {
821             ref = child;
822             pos += 1;
823         }
824     }
826     nr_arena_item_set_child_position (item->parent, item, ref);
829 /** Returns a background image for use with filter effects. */
830 NRPixBlock *
831 nr_arena_item_get_background (NRArenaItem const *item, int depth)
833     NRPixBlock *pb;
834     if (!item->background_pb)
835         return NULL;
836     if (item->background_new) {
837         pb = new NRPixBlock ();
838         nr_pixblock_setup_fast (pb, item->background_pb->mode,
839                                 item->background_pb->area.x0,
840                                 item->background_pb->area.y0,
841                                 item->background_pb->area.x1,
842                                 item->background_pb->area.y1, true);
843         if (pb->size != NR_PIXBLOCK_SIZE_TINY && pb->data.px == NULL) // allocation failed
844             return NULL;
845     } else if (item->parent) {
846         pb = nr_arena_item_get_background (item->parent, depth + 1);
847     } else
848         return NULL;
850     if (depth > 0)
851         nr_blit_pixblock_pixblock (pb, item->background_pb);
853     return pb;
856 /* Helpers */
858 NRArenaItem *
859 nr_arena_item_attach (NRArenaItem *parent, NRArenaItem *child,
860                       NRArenaItem *prev, NRArenaItem *next)
862     nr_return_val_if_fail (parent != NULL, NULL);
863     nr_return_val_if_fail (NR_IS_ARENA_ITEM (parent), NULL);
864     nr_return_val_if_fail (child != NULL, NULL);
865     nr_return_val_if_fail (NR_IS_ARENA_ITEM (child), NULL);
866     nr_return_val_if_fail (child->parent == NULL, NULL);
867     nr_return_val_if_fail (child->prev == NULL, NULL);
868     nr_return_val_if_fail (child->next == NULL, NULL);
869     nr_return_val_if_fail (!prev || NR_IS_ARENA_ITEM (prev), NULL);
870     nr_return_val_if_fail (!prev || (prev->parent == parent), NULL);
871     nr_return_val_if_fail (!prev || (prev->next == next), NULL);
872     nr_return_val_if_fail (!next || NR_IS_ARENA_ITEM (next), NULL);
873     nr_return_val_if_fail (!next || (next->parent == parent), NULL);
874     nr_return_val_if_fail (!next || (next->prev == prev), NULL);
876     child->parent = parent;
877     child->prev = prev;
878     child->next = next;
880     if (prev)
881         prev->next = child;
882     if (next)
883         next->prev = child;
885     return child;
888 NRArenaItem *
889 nr_arena_item_detach (NRArenaItem *parent, NRArenaItem *child)
891     nr_return_val_if_fail (parent != NULL, NULL);
892     nr_return_val_if_fail (NR_IS_ARENA_ITEM (parent), NULL);
893     nr_return_val_if_fail (child != NULL, NULL);
894     nr_return_val_if_fail (NR_IS_ARENA_ITEM (child), NULL);
895     nr_return_val_if_fail (child->parent == parent, NULL);
897     NRArenaItem *prev = child->prev;
898     NRArenaItem *next = child->next;
900     child->parent = NULL;
901     child->prev = NULL;
902     child->next = NULL;
904     if (prev)
905         prev->next = next;
906     if (next)
907         next->prev = prev;
909     return next;
912 /*
913   Local Variables:
914   mode:c++
915   c-file-style:"stroustrup"
916   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
917   indent-tabs-mode:nil
918   fill-column:99
919   End:
920 */
921 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :