Code

interruptible redraw
[inkscape.git] / src / display / sp-canvas.cpp
1 #define __SP_CANVAS_C__
3 /** \file
4  * Port of GnomeCanvas for Inkscape needs
5  *
6  * Authors:
7  *   Federico Mena <federico@nuclecu.unam.mx>
8  *   Raph Levien <raph@gimp.org>
9  *   Lauris Kaplinski <lauris@kaplinski.com>
10  *   fred
11  *   bbyak
12  *
13  * Copyright (C) 1998 The Free Software Foundation
14  * Copyright (C) 2002-2006 authors
15  *
16  * Released under GNU GPL, read the file 'COPYING' for more information
17  */
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
23 #include <libnr/nr-pixblock.h>
25 #include <gtk/gtkmain.h>
26 #include <gtk/gtksignal.h>
28 #include <gtkmm.h>
30 #include <helper/sp-marshal.h>
31 #include <display/sp-canvas.h>
32 #include "display-forward.h"
33 #include <libnr/nr-matrix-fns.h>
34 #include <libnr/nr-matrix-ops.h>
35 #include <libnr/nr-convex-hull.h>
37 enum {
38         RENDERMODE_NORMAL,
39         RENDERMODE_NOAA,
40         RENDERMODE_OUTLINE
41 };
43 const gint sp_canvas_update_priority = G_PRIORITY_HIGH_IDLE;
45 #define SP_CANVAS_WINDOW(c) (((GtkWidget *) (c))->window)
47 enum {
48     SP_CANVAS_ITEM_VISIBLE = 1 << 7,
49     SP_CANVAS_ITEM_NEED_UPDATE = 1 << 8,
50     SP_CANVAS_ITEM_NEED_AFFINE = 1 << 9
51 };
53 /**
54  * A group of Items.
55  */
56 struct SPCanvasGroup {
57     SPCanvasItem item;
59     GList *items, *last;
60 };
62 /**
63  * The SPCanvasGroup vtable.
64  */
65 struct SPCanvasGroupClass {
66     SPCanvasItemClass parent_class;
67 };
69 /**
70  * The SPCanvas vtable.
71  */
72 struct SPCanvasClass {
73     GtkWidgetClass parent_class;
74 };
76 static void group_add (SPCanvasGroup *group, SPCanvasItem *item);
77 static void group_remove (SPCanvasGroup *group, SPCanvasItem *item);
79 /* SPCanvasItem */
81 enum {ITEM_EVENT, ITEM_LAST_SIGNAL};
84 static void sp_canvas_request_update (SPCanvas *canvas);
86 static void sp_canvas_item_class_init (SPCanvasItemClass *klass);
87 static void sp_canvas_item_init (SPCanvasItem *item);
88 static void sp_canvas_item_dispose (GObject *object);
89 static void sp_canvas_item_construct (SPCanvasItem *item, SPCanvasGroup *parent, const gchar *first_arg_name, va_list args);
91 static int emit_event (SPCanvas *canvas, GdkEvent *event);
93 static guint item_signals[ITEM_LAST_SIGNAL] = { 0 };
95 static GtkObjectClass *item_parent_class;
97 /**
98  * Registers the SPCanvasItem class with Glib and returns its type number.
99  */
100 GType
101 sp_canvas_item_get_type (void)
103     static GType type = 0;
104     if (!type) {
105         static const GTypeInfo info = {
106             sizeof (SPCanvasItemClass),
107             NULL, NULL,
108             (GClassInitFunc) sp_canvas_item_class_init,
109             NULL, NULL,
110             sizeof (SPCanvasItem),
111             0,
112             (GInstanceInitFunc) sp_canvas_item_init,
113             NULL
114         };
115         type = g_type_register_static (GTK_TYPE_OBJECT, "SPCanvasItem", &info, (GTypeFlags)0);
116     }
118     return type;
121 /**
122  * Initializes the SPCanvasItem vtable and the "event" signal.
123  */
124 static void
125 sp_canvas_item_class_init (SPCanvasItemClass *klass)
127     GObjectClass *object_class = (GObjectClass *) klass;
129     /* fixme: Derive from GObject */
130     item_parent_class = (GtkObjectClass*)gtk_type_class (GTK_TYPE_OBJECT);
132     item_signals[ITEM_EVENT] = g_signal_new ("event",
133                                              G_TYPE_FROM_CLASS (klass),
134                                              G_SIGNAL_RUN_LAST,
135                                              G_STRUCT_OFFSET (SPCanvasItemClass, event),
136                                              NULL, NULL,
137                                              sp_marshal_BOOLEAN__POINTER,
138                                              G_TYPE_BOOLEAN, 1,
139                                              GDK_TYPE_EVENT);
141     object_class->dispose = sp_canvas_item_dispose;
144 /**
145  * Callback for initialization of SPCanvasItem.
146  */
147 static void
148 sp_canvas_item_init (SPCanvasItem *item)
150     item->flags |= SP_CANVAS_ITEM_VISIBLE;
151     item->xform = NR::Matrix(NR::identity());
154 /**
155  * Constructs new SPCanvasItem on SPCanvasGroup.
156  */
157 SPCanvasItem *
158 sp_canvas_item_new (SPCanvasGroup *parent, GtkType type, const gchar *first_arg_name, ...)
160     va_list args;
162     g_return_val_if_fail (parent != NULL, NULL);
163     g_return_val_if_fail (SP_IS_CANVAS_GROUP (parent), NULL);
164     g_return_val_if_fail (gtk_type_is_a (type, sp_canvas_item_get_type ()), NULL);
166     SPCanvasItem *item = SP_CANVAS_ITEM (gtk_type_new (type));
168     va_start (args, first_arg_name);
169     sp_canvas_item_construct (item, parent, first_arg_name, args);
170     va_end (args);
172     return item;
175 /**
176  * Sets up the newly created SPCanvasItem.
177  *
178  * We make it static for encapsulation reasons since it was nowhere used.
179  */
180 static void
181 sp_canvas_item_construct (SPCanvasItem *item, SPCanvasGroup *parent, const gchar *first_arg_name, va_list args)
183     g_return_if_fail (SP_IS_CANVAS_GROUP (parent));
184     g_return_if_fail (SP_IS_CANVAS_ITEM (item));
186     item->parent = SP_CANVAS_ITEM (parent);
187     item->canvas = item->parent->canvas;
189     g_object_set_valist (G_OBJECT (item), first_arg_name, args);
191     group_add (SP_CANVAS_GROUP (item->parent), item);
193     sp_canvas_item_request_update (item);
194     sp_canvas_request_redraw (item->canvas, (int)(item->x1), (int)(item->y1), (int)(item->x2 + 1), (int)(item->y2 + 1));
195     item->canvas->need_repick = TRUE;
198 /**
199  * Helper function that requests redraw only if item's visible flag is set.
200  */
201 static void
202 redraw_if_visible (SPCanvasItem *item)
204     if (item->flags & SP_CANVAS_ITEM_VISIBLE) {
205         sp_canvas_request_redraw (item->canvas, (int)(item->x1), (int)(item->y1), (int)(item->x2 + 1), (int)(item->y2 + 1));
206     }
209 /**
210  * Callback that removes item from all referers and destroys it.
211  */
212 static void
213 sp_canvas_item_dispose (GObject *object)
215     SPCanvasItem *item = SP_CANVAS_ITEM (object);
217     redraw_if_visible (item);
218     item->flags &= ~SP_CANVAS_ITEM_VISIBLE;
220     if (item == item->canvas->current_item) {
221         item->canvas->current_item = NULL;
222         item->canvas->need_repick = TRUE;
223     }
225     if (item == item->canvas->new_current_item) {
226         item->canvas->new_current_item = NULL;
227         item->canvas->need_repick = TRUE;
228     }
230     if (item == item->canvas->grabbed_item) {
231         item->canvas->grabbed_item = NULL;
232         gdk_pointer_ungrab (GDK_CURRENT_TIME);
233     }
235     if (item == item->canvas->focused_item)
236         item->canvas->focused_item = NULL;
238     if (item->parent) {
239         group_remove (SP_CANVAS_GROUP (item->parent), item);
240     }
242     G_OBJECT_CLASS (item_parent_class)->dispose (object);
245 /**
246  * Helper function to update item and its children.
247  *
248  * NB! affine is parent2canvas.
249  */
250 static void
251 sp_canvas_item_invoke_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
253     /* Apply the child item's transform */
254     NR::Matrix child_affine = item->xform * affine;
256     /* apply object flags to child flags */
257     int child_flags = flags & ~SP_CANVAS_UPDATE_REQUESTED;
259     if (item->flags & SP_CANVAS_ITEM_NEED_UPDATE)
260         child_flags |= SP_CANVAS_UPDATE_REQUESTED;
262     if (item->flags & SP_CANVAS_ITEM_NEED_AFFINE)
263         child_flags |= SP_CANVAS_UPDATE_AFFINE;
265     if (child_flags & (SP_CANVAS_UPDATE_REQUESTED | SP_CANVAS_UPDATE_AFFINE)) {
266         if (SP_CANVAS_ITEM_GET_CLASS (item)->update)
267             SP_CANVAS_ITEM_GET_CLASS (item)->update (item, child_affine, child_flags);
268     }
270     GTK_OBJECT_UNSET_FLAGS (item, SP_CANVAS_ITEM_NEED_UPDATE);
271     GTK_OBJECT_UNSET_FLAGS (item, SP_CANVAS_ITEM_NEED_AFFINE);
274 /** 
275  * Helper function to invoke the point method of the item.  
276  *
277  * The argument x, y should be in the parent's item-relative coordinate 
278  * system.  This routine applies the inverse of the item's transform, 
279  * maintaining the affine invariant.
280  */
281 static double
282 sp_canvas_item_invoke_point (SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item)
284     if (SP_CANVAS_ITEM_GET_CLASS (item)->point)
285         return SP_CANVAS_ITEM_GET_CLASS (item)->point (item, p, actual_item);
287     return NR_HUGE;
290 /**
291  * Makes the item's affine transformation matrix be equal to the specified
292  * matrix.
293  * 
294  * @item: A canvas item.
295  * @affine: An affine transformation matrix.
296  */
297 void
298 sp_canvas_item_affine_absolute (SPCanvasItem *item, NR::Matrix const& affine)
300     item->xform = affine;
302     if (!(item->flags & SP_CANVAS_ITEM_NEED_AFFINE)) {
303         item->flags |= SP_CANVAS_ITEM_NEED_AFFINE;
304         if (item->parent != NULL) {
305             sp_canvas_item_request_update (item->parent);
306         } else {
307             sp_canvas_request_update (item->canvas);
308         }
309     }
311     item->canvas->need_repick = TRUE;
314 /**
315  * Convenience function to reorder items in a group's child list.  
316  *
317  * This puts the specified link after the "before" link.
318  */
319 static void
320 put_item_after (GList *link, GList *before)
322     if (link == before)
323         return;
325     SPCanvasGroup *parent = SP_CANVAS_GROUP (SP_CANVAS_ITEM (link->data)->parent);
327     if (before == NULL) {
328         if (link == parent->items) return;
330         link->prev->next = link->next;
332         if (link->next) {
333             link->next->prev = link->prev;
334         } else {
335             parent->last = link->prev;
336         }
338         link->prev = before;
339         link->next = parent->items;
340         link->next->prev = link;
341         parent->items = link;
342     } else {
343         if ((link == parent->last) && (before == parent->last->prev))
344             return;
346         if (link->next)
347             link->next->prev = link->prev;
349         if (link->prev)
350             link->prev->next = link->next;
351         else {
352             parent->items = link->next;
353             parent->items->prev = NULL;
354         }
356         link->prev = before;
357         link->next = before->next;
359         link->prev->next = link;
361         if (link->next)
362             link->next->prev = link;
363         else
364             parent->last = link;
365     }
369 /**
370  * Raises the item in its parent's stack by the specified number of positions.
371  * 
372  * \param item A canvas item.
373  * \param positions Number of steps to raise the item.
374  *
375  * If the number of positions is greater than the distance to the top of the
376  * stack, then the item is put at the top.
377  */
378 void
379 sp_canvas_item_raise (SPCanvasItem *item, int positions)
381     g_return_if_fail (item != NULL);
382     g_return_if_fail (SP_IS_CANVAS_ITEM (item));
383     g_return_if_fail (positions >= 0);
385     if (!item->parent || positions == 0)
386         return;
388     SPCanvasGroup *parent = SP_CANVAS_GROUP (item->parent);
389     GList *link = g_list_find (parent->items, item);
390     g_assert (link != NULL);
392     GList *before;
393     for (before = link; positions && before; positions--)
394         before = before->next;
396     if (!before)
397         before = parent->last;
399     put_item_after (link, before);
401     redraw_if_visible (item);
402     item->canvas->need_repick = TRUE;
406 /**
407  * Lowers the item in its parent's stack by the specified number of positions.
408  *
409  * \param item A canvas item.
410  * \param positions Number of steps to lower the item.
411  *
412  * If the number of positions is greater than the distance to the bottom of the
413  * stack, then the item is put at the bottom.
414  **/
415 void
416 sp_canvas_item_lower (SPCanvasItem *item, int positions)
418     g_return_if_fail (item != NULL);
419     g_return_if_fail (SP_IS_CANVAS_ITEM (item));
420     g_return_if_fail (positions >= 1);
422     if (!item->parent || positions == 0)
423         return;
425     SPCanvasGroup *parent = SP_CANVAS_GROUP (item->parent);
426     GList *link = g_list_find (parent->items, item);
427     g_assert (link != NULL);
429     GList *before;
430     if (link->prev)
431         for (before = link->prev; positions && before; positions--)
432             before = before->prev;
433     else
434         before = NULL;
436     put_item_after (link, before);
438     redraw_if_visible (item);
439     item->canvas->need_repick = TRUE;
442 /**
443  * Sets visible flag on item and requests a redraw.
444  */
445 void
446 sp_canvas_item_show (SPCanvasItem *item)
448     g_return_if_fail (item != NULL);
449     g_return_if_fail (SP_IS_CANVAS_ITEM (item));
451     if (item->flags & SP_CANVAS_ITEM_VISIBLE)
452         return;
454     item->flags |= SP_CANVAS_ITEM_VISIBLE;
456     sp_canvas_request_redraw (item->canvas, (int)(item->x1), (int)(item->y1), (int)(item->x2 + 1), (int)(item->y2 + 1));
457     item->canvas->need_repick = TRUE;
460 /**
461  * Clears visible flag on item and requests a redraw.
462  */
463 void
464 sp_canvas_item_hide (SPCanvasItem *item)
466     g_return_if_fail (item != NULL);
467     g_return_if_fail (SP_IS_CANVAS_ITEM (item));
469     if (!(item->flags & SP_CANVAS_ITEM_VISIBLE))
470         return;
472     item->flags &= ~SP_CANVAS_ITEM_VISIBLE;
474     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)(item->x2 + 1), (int)(item->y2 + 1));
475     item->canvas->need_repick = TRUE;
478 /**
479  * Grab item under cursor.
480  * 
481  * \pre !canvas->grabbed_item && item->flags & SP_CANVAS_ITEM_VISIBLE
482  */
483 int
484 sp_canvas_item_grab (SPCanvasItem *item, guint event_mask, GdkCursor *cursor, guint32 etime)
486     g_return_val_if_fail (item != NULL, -1);
487     g_return_val_if_fail (SP_IS_CANVAS_ITEM (item), -1);
488     g_return_val_if_fail (GTK_WIDGET_MAPPED (item->canvas), -1);
490     if (item->canvas->grabbed_item)
491         return -1;
493     if (!(item->flags & SP_CANVAS_ITEM_VISIBLE))
494         return -1;
496     /* fixme: Top hack (Lauris) */
497     /* fixme: If we add key masks to event mask, Gdk will abort (Lauris) */
498     /* fixme: But Canvas actualle does get key events, so all we need is routing these here */
499     gdk_pointer_grab (SP_CANVAS_WINDOW (item->canvas), FALSE,
500                       (GdkEventMask)(event_mask & (~(GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK))),
501                       NULL, cursor, etime);
503     item->canvas->grabbed_item = item;
504     item->canvas->grabbed_event_mask = event_mask;
505     item->canvas->current_item = item; /* So that events go to the grabbed item */
507     return 0;
510 /**
511  * Ungrabs the item, which must have been grabbed in the canvas, and ungrabs the
512  * mouse.
513  * 
514  * \param item A canvas item that holds a grab.
515  * \param etime The timestamp for ungrabbing the mouse.
516  */
517 void
518 sp_canvas_item_ungrab (SPCanvasItem *item, guint32 etime)
520     g_return_if_fail (item != NULL);
521     g_return_if_fail (SP_IS_CANVAS_ITEM (item));
523     if (item->canvas->grabbed_item != item)
524         return;
526     item->canvas->grabbed_item = NULL;
528     gdk_pointer_ungrab (etime);
531 /**
532  * Returns the product of all transformation matrices from the root item down
533  * to the item.
534  */
535 NR::Matrix sp_canvas_item_i2w_affine(SPCanvasItem const *item)
537     g_assert (SP_IS_CANVAS_ITEM (item)); // should we get this?
539     NR::Matrix affine = NR::identity();
541     while (item) {
542         affine *= item->xform;
543         item = item->parent;
544     }
545     return affine;
548 /**
549  * Helper that returns true iff item is descendant of parent.
550  */
551 static bool is_descendant(SPCanvasItem const *item, SPCanvasItem const *parent)
553     while (item) {
554         if (item == parent)
555             return true;
556         item = item->parent;
557     }
559     return false;
562 /**
563  * Focus canvas, and item under cursor if it is not already focussed.
564  */
565 void
566 sp_canvas_item_grab_focus (SPCanvasItem *item)
568     g_return_if_fail (item != NULL);
569     g_return_if_fail (SP_IS_CANVAS_ITEM (item));
570     g_return_if_fail (GTK_WIDGET_CAN_FOCUS (GTK_WIDGET (item->canvas)));
572     SPCanvasItem *focused_item = item->canvas->focused_item;
574     if (focused_item) {
575         GdkEvent ev;
576         ev.focus_change.type = GDK_FOCUS_CHANGE;
577         ev.focus_change.window = SP_CANVAS_WINDOW (item->canvas);
578         ev.focus_change.send_event = FALSE;
579         ev.focus_change.in = FALSE;
581         emit_event (item->canvas, &ev);
582     }
584     item->canvas->focused_item = item;
585     gtk_widget_grab_focus (GTK_WIDGET (item->canvas));
587     if (focused_item) {
588         GdkEvent ev;
589         ev.focus_change.type = GDK_FOCUS_CHANGE;
590         ev.focus_change.window = SP_CANVAS_WINDOW (item->canvas);
591         ev.focus_change.send_event = FALSE;
592         ev.focus_change.in = TRUE;
594         emit_event (item->canvas, &ev);
595     }
598 /**
599  * Requests that the canvas queue an update for the specified item.
600  * 
601  * To be used only by item implementations.
602  */
603 void
604 sp_canvas_item_request_update (SPCanvasItem *item)
606     if (item->flags & SP_CANVAS_ITEM_NEED_UPDATE)
607         return;
609     item->flags |= SP_CANVAS_ITEM_NEED_UPDATE;
611     if (item->parent != NULL) {
612         /* Recurse up the tree */
613         sp_canvas_item_request_update (item->parent);
614     } else {
615         /* Have reached the top of the tree, make sure the update call gets scheduled. */
616         sp_canvas_request_update (item->canvas);
617     }
620 /**
621  * Returns position of item in group.
622  */
623 gint sp_canvas_item_order (SPCanvasItem * item)
625     return g_list_index (SP_CANVAS_GROUP (item->parent)->items, item);
628 /* SPCanvasGroup */
630 static void sp_canvas_group_class_init (SPCanvasGroupClass *klass);
631 static void sp_canvas_group_init (SPCanvasGroup *group);
632 static void sp_canvas_group_destroy (GtkObject *object);
634 static void sp_canvas_group_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags);
635 static double sp_canvas_group_point (SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item);
636 static void sp_canvas_group_render (SPCanvasItem *item, SPCanvasBuf *buf);
638 static SPCanvasItemClass *group_parent_class;
640 /**
641  * Registers SPCanvasGroup class with Gtk and returns its type number.
642  */
643 GtkType
644 sp_canvas_group_get_type (void)
646     static GtkType group_type = 0;
648     if (!group_type) {
649         static const GtkTypeInfo group_info = {
650             "SPCanvasGroup",
651             sizeof (SPCanvasGroup),
652             sizeof (SPCanvasGroupClass),
653             (GtkClassInitFunc) sp_canvas_group_class_init,
654             (GtkObjectInitFunc) sp_canvas_group_init,
655             NULL, NULL, NULL
656         };
658         group_type = gtk_type_unique (sp_canvas_item_get_type (), &group_info);
659     }
661     return group_type;
664 /**
665  * Class initialization function for SPCanvasGroupClass
666  */
667 static void
668 sp_canvas_group_class_init (SPCanvasGroupClass *klass)
670     GtkObjectClass *object_class = (GtkObjectClass *) klass;
671     SPCanvasItemClass *item_class = (SPCanvasItemClass *) klass;
673     group_parent_class = (SPCanvasItemClass*)gtk_type_class (sp_canvas_item_get_type ());
675     object_class->destroy = sp_canvas_group_destroy;
677     item_class->update = sp_canvas_group_update;
678     item_class->render = sp_canvas_group_render;
679     item_class->point = sp_canvas_group_point;
682 /**
683  * Callback. Empty.
684  */
685 static void
686 sp_canvas_group_init (SPCanvasGroup */*group*/)
688     /* Nothing here */
691 /**
692  * Callback that destroys all items in group and calls group's virtual 
693  * destroy() function.
694  */
695 static void
696 sp_canvas_group_destroy (GtkObject *object)
698     g_return_if_fail (object != NULL);
699     g_return_if_fail (SP_IS_CANVAS_GROUP (object));
701     const SPCanvasGroup *group = SP_CANVAS_GROUP (object);
703     GList *list = group->items;
704     while (list) {
705         SPCanvasItem *child = (SPCanvasItem *)list->data;
706         list = list->next;
708         gtk_object_destroy (GTK_OBJECT (child));
709     }
711     if (GTK_OBJECT_CLASS (group_parent_class)->destroy)
712         (* GTK_OBJECT_CLASS (group_parent_class)->destroy) (object);
715 /**
716  * Update handler for canvas groups
717  */
718 static void
719 sp_canvas_group_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
721     const SPCanvasGroup *group = SP_CANVAS_GROUP (item);
722     NR::ConvexHull corners(NR::Point(0, 0));
723     bool empty=true;
725     for (GList *list = group->items; list; list = list->next) {
726         SPCanvasItem *i = (SPCanvasItem *)list->data;
728         sp_canvas_item_invoke_update (i, affine, flags);
730         if ( i->x2 > i->x1 && i->y2 > i->y1 ) {
731             if (empty) {
732                 corners = NR::ConvexHull(NR::Point(i->x1, i->y1));
733                 empty = false;
734             } else {
735                 corners.add(NR::Point(i->x1, i->y1));
736             }
737             corners.add(NR::Point(i->x2, i->y2));
738         }
739     }
741     NR::Rect const &bounds = corners.bounds();
742     item->x1 = bounds.min()[NR::X];
743     item->y1 = bounds.min()[NR::Y];
744     item->x2 = bounds.max()[NR::X];
745     item->y2 = bounds.max()[NR::Y];
748 /**
749  * Point handler for canvas groups.
750  */
751 static double
752 sp_canvas_group_point (SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item)
754     const SPCanvasGroup *group = SP_CANVAS_GROUP (item);
755     const double x = p[NR::X];
756     const double y = p[NR::Y];
757     int x1 = (int)(x - item->canvas->close_enough);
758     int y1 = (int)(y - item->canvas->close_enough);
759     int x2 = (int)(x + item->canvas->close_enough);
760     int y2 = (int)(y + item->canvas->close_enough);
762     double best = 0.0;
763     *actual_item = NULL;
765     double dist = 0.0;
767     for (GList *list = group->items; list; list = list->next) {
768         SPCanvasItem *child = (SPCanvasItem *)list->data;
770         if ((child->x1 <= x2) && (child->y1 <= y2) && (child->x2 >= x1) && (child->y2 >= y1)) {
771             SPCanvasItem *point_item = NULL; /* cater for incomplete item implementations */
773             int has_point;
774             if ((child->flags & SP_CANVAS_ITEM_VISIBLE) && SP_CANVAS_ITEM_GET_CLASS (child)->point) {
775                 dist = sp_canvas_item_invoke_point (child, p, &point_item);
776                 has_point = TRUE;
777             } else
778                 has_point = FALSE;
780             if (has_point && point_item && ((int) (dist + 0.5) <= item->canvas->close_enough)) {
781                 best = dist;
782                 *actual_item = point_item;
783             }
784         }
785     }
787     return best;
790 /**
791  * Renders all visible canvas group items in buf rectangle.
792  */
793 static void
794 sp_canvas_group_render (SPCanvasItem *item, SPCanvasBuf *buf)
796     const SPCanvasGroup *group = SP_CANVAS_GROUP (item);
798     for (GList *list = group->items; list; list = list->next) {
799         SPCanvasItem *child = (SPCanvasItem *)list->data;
800         if (child->flags & SP_CANVAS_ITEM_VISIBLE) {
801             if ((child->x1 < buf->rect.x1) &&
802                 (child->y1 < buf->rect.y1) &&
803                 (child->x2 > buf->rect.x0) &&
804                 (child->y2 > buf->rect.y0)) {
805                 if (SP_CANVAS_ITEM_GET_CLASS (child)->render)
806                     SP_CANVAS_ITEM_GET_CLASS (child)->render (child, buf);
807             }
808         }
809     }
812 /**
813  * Adds an item to a canvas group.
814  */
815 static void
816 group_add (SPCanvasGroup *group, SPCanvasItem *item)
818     gtk_object_ref (GTK_OBJECT (item));
819     gtk_object_sink (GTK_OBJECT (item));
821     if (!group->items) {
822         group->items = g_list_append (group->items, item);
823         group->last = group->items;
824     } else {
825         group->last = g_list_append (group->last, item)->next;
826     }
828     sp_canvas_item_request_update (item);
831 /** 
832  * Removes an item from a canvas group
833  */
834 static void
835 group_remove (SPCanvasGroup *group, SPCanvasItem *item)
837     g_return_if_fail (group != NULL);
838     g_return_if_fail (SP_IS_CANVAS_GROUP (group));
839     g_return_if_fail (item != NULL);
841     for (GList *children = group->items; children; children = children->next) {
842         if (children->data == item) {
844             /* Unparent the child */
846             item->parent = NULL;
847             gtk_object_unref (GTK_OBJECT (item));
849             /* Remove it from the list */
851             if (children == group->last) group->last = children->prev;
853             group->items = g_list_remove_link (group->items, children);
854             g_list_free (children);
855             break;
856         }
857     }
860 /* SPCanvas */
862 static void sp_canvas_class_init (SPCanvasClass *klass);
863 static void sp_canvas_init (SPCanvas *canvas);
864 static void sp_canvas_destroy (GtkObject *object);
866 static void sp_canvas_realize (GtkWidget *widget);
867 static void sp_canvas_unrealize (GtkWidget *widget);
869 static void sp_canvas_size_request (GtkWidget *widget, GtkRequisition *req);
870 static void sp_canvas_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
872 static gint sp_canvas_button (GtkWidget *widget, GdkEventButton *event);
873 static gint sp_canvas_scroll (GtkWidget *widget, GdkEventScroll *event);
874 static gint sp_canvas_motion (GtkWidget *widget, GdkEventMotion *event);
875 static gint sp_canvas_expose (GtkWidget *widget, GdkEventExpose *event);
876 static gint sp_canvas_key (GtkWidget *widget, GdkEventKey *event);
877 static gint sp_canvas_crossing (GtkWidget *widget, GdkEventCrossing *event);
878 static gint sp_canvas_focus_in (GtkWidget *widget, GdkEventFocus *event);
879 static gint sp_canvas_focus_out (GtkWidget *widget, GdkEventFocus *event);
881 static GtkWidgetClass *canvas_parent_class;
883 void sp_canvas_resize_tiles(SPCanvas* canvas,int nl,int nt,int nr,int nb);
884 void sp_canvas_dirty_rect(SPCanvas* canvas,int nl,int nt,int nr,int nb);
885 static int do_update (SPCanvas *canvas);
887 /**
888  * Registers the SPCanvas class if necessary, and returns the type ID
889  * associated to it.
890  *
891  * \return The type ID of the SPCanvas class.
892  **/
893 GtkType
894 sp_canvas_get_type (void)
896     static GtkType canvas_type = 0;
898     if (!canvas_type) {
899         static const GtkTypeInfo canvas_info = {
900             "SPCanvas",
901             sizeof (SPCanvas),
902             sizeof (SPCanvasClass),
903             (GtkClassInitFunc) sp_canvas_class_init,
904             (GtkObjectInitFunc) sp_canvas_init,
905             NULL, NULL, NULL
906         };
908         canvas_type = gtk_type_unique (GTK_TYPE_WIDGET, &canvas_info);
909     }
911     return canvas_type;
914 /**
915  * Class initialization function for SPCanvasClass.
916  */
917 static void
918 sp_canvas_class_init (SPCanvasClass *klass)
920     GtkObjectClass *object_class = (GtkObjectClass *) klass;
921     GtkWidgetClass *widget_class = (GtkWidgetClass *) klass;
923     canvas_parent_class = (GtkWidgetClass *)gtk_type_class (GTK_TYPE_WIDGET);
925     object_class->destroy = sp_canvas_destroy;
927     widget_class->realize = sp_canvas_realize;
928     widget_class->unrealize = sp_canvas_unrealize;
929     widget_class->size_request = sp_canvas_size_request;
930     widget_class->size_allocate = sp_canvas_size_allocate;
931     widget_class->button_press_event = sp_canvas_button;
932     widget_class->button_release_event = sp_canvas_button;
933     widget_class->motion_notify_event = sp_canvas_motion;
934     widget_class->scroll_event = sp_canvas_scroll;
935     widget_class->expose_event = sp_canvas_expose;
936     widget_class->key_press_event = sp_canvas_key;
937     widget_class->key_release_event = sp_canvas_key;
938     widget_class->enter_notify_event = sp_canvas_crossing;
939     widget_class->leave_notify_event = sp_canvas_crossing;
940     widget_class->focus_in_event = sp_canvas_focus_in;
941     widget_class->focus_out_event = sp_canvas_focus_out;
944 /** 
945  * Callback: object initialization for SPCanvas.
946  */
947 static void
948 sp_canvas_init (SPCanvas *canvas)
950     GTK_WIDGET_UNSET_FLAGS (canvas, GTK_NO_WINDOW);
951     GTK_WIDGET_UNSET_FLAGS (canvas, GTK_DOUBLE_BUFFERED);
952     GTK_WIDGET_SET_FLAGS (canvas, GTK_CAN_FOCUS);
954     canvas->pick_event.type = GDK_LEAVE_NOTIFY;
955     canvas->pick_event.crossing.x = 0;
956     canvas->pick_event.crossing.y = 0;
958     /* Create the root item as a special case */
959     canvas->root = SP_CANVAS_ITEM (gtk_type_new (sp_canvas_group_get_type ()));
960     canvas->root->canvas = canvas;
962     gtk_object_ref (GTK_OBJECT (canvas->root));
963     gtk_object_sink (GTK_OBJECT (canvas->root));
965     canvas->need_repick = TRUE;
967     // See comment at in sp-canvas.h.
968     canvas->gen_all_enter_events = false;
970     canvas->tiles=NULL;
971     canvas->tLeft=canvas->tTop=canvas->tRight=canvas->tBottom=0;
972     canvas->tileH=canvas->tileV=0;
974     canvas->redraw_aborted.x0 = NR_HUGE_L;
975     canvas->redraw_aborted.x1 = -NR_HUGE_L;
976     canvas->redraw_aborted.y0 = NR_HUGE_L;
977     canvas->redraw_aborted.y1 = -NR_HUGE_L;
978     
979     canvas->redraw_count = 0;
981     canvas->slowest_buffer = 0;
984 /**
985  * Convenience function to remove the idle handler of a canvas.
986  */
987 static void
988 remove_idle (SPCanvas *canvas)
990     if (canvas->idle_id) {
991         gtk_idle_remove (canvas->idle_id);
992         canvas->idle_id = 0;
993     }
996 /*
997  * Removes the transient state of the canvas (idle handler, grabs).
998  */
999 static void
1000 shutdown_transients (SPCanvas *canvas)
1002     /* We turn off the need_redraw flag, since if the canvas is mapped again
1003      * it will request a redraw anyways.  We do not turn off the need_update
1004      * flag, though, because updates are not queued when the canvas remaps
1005      * itself.
1006      */
1007     if (canvas->need_redraw) {
1008         canvas->need_redraw = FALSE;
1009     }
1010     if ( canvas->tiles ) g_free(canvas->tiles);
1011     canvas->tiles=NULL;
1012     canvas->tLeft=canvas->tTop=canvas->tRight=canvas->tBottom=0;
1013     canvas->tileH=canvas->tileV=0;
1015     if (canvas->grabbed_item) {
1016         canvas->grabbed_item = NULL;
1017         gdk_pointer_ungrab (GDK_CURRENT_TIME);
1018     }
1020     remove_idle (canvas);
1023 /**
1024  * Destroy handler for SPCanvas.
1025  */
1026 static void
1027 sp_canvas_destroy (GtkObject *object)
1029     SPCanvas *canvas = SP_CANVAS (object);
1031     if (canvas->root) {
1032         gtk_object_unref (GTK_OBJECT (canvas->root));
1033         canvas->root = NULL;
1034     }
1036     shutdown_transients (canvas);
1038     if (GTK_OBJECT_CLASS (canvas_parent_class)->destroy)
1039         (* GTK_OBJECT_CLASS (canvas_parent_class)->destroy) (object);
1042 /**
1043  * Returns new canvas as widget.
1044  */
1045 GtkWidget *
1046 sp_canvas_new_aa (void)
1048     SPCanvas *canvas = (SPCanvas *)gtk_type_new (sp_canvas_get_type ());
1050     return (GtkWidget *) canvas;
1053 /**
1054  * The canvas widget's realize callback.
1055  */
1056 static void
1057 sp_canvas_realize (GtkWidget *widget)
1059     SPCanvas *canvas = SP_CANVAS (widget);
1061     GdkWindowAttr attributes;
1062     attributes.window_type = GDK_WINDOW_CHILD;
1063     attributes.x = widget->allocation.x;
1064     attributes.y = widget->allocation.y;
1065     attributes.width = widget->allocation.width;
1066     attributes.height = widget->allocation.height;
1067     attributes.wclass = GDK_INPUT_OUTPUT;
1068     attributes.visual = gdk_rgb_get_visual ();
1069     attributes.colormap = gdk_rgb_get_cmap ();
1070     attributes.event_mask = (gtk_widget_get_events (widget) |
1071                              GDK_EXPOSURE_MASK |
1072                              GDK_BUTTON_PRESS_MASK |
1073                              GDK_BUTTON_RELEASE_MASK |
1074                              GDK_POINTER_MOTION_MASK |
1075                              GDK_PROXIMITY_IN_MASK |
1076                              GDK_PROXIMITY_OUT_MASK |
1077                              GDK_KEY_PRESS_MASK |
1078                              GDK_KEY_RELEASE_MASK |
1079                              GDK_ENTER_NOTIFY_MASK |
1080                              GDK_LEAVE_NOTIFY_MASK |
1081                              GDK_FOCUS_CHANGE_MASK);
1082     gint attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1084     widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1085     gdk_window_set_user_data (widget->window, widget);
1086     gtk_widget_set_events(widget, attributes.event_mask);
1088     GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1090     canvas->pixmap_gc = gdk_gc_new (SP_CANVAS_WINDOW (canvas));
1093 /**
1094  * The canvas widget's unrealize callback.
1095  */
1096 static void
1097 sp_canvas_unrealize (GtkWidget *widget)
1099     SPCanvas *canvas = SP_CANVAS (widget);
1101     shutdown_transients (canvas);
1103     gdk_gc_destroy (canvas->pixmap_gc);
1104     canvas->pixmap_gc = NULL;
1106     if (GTK_WIDGET_CLASS (canvas_parent_class)->unrealize)
1107         (* GTK_WIDGET_CLASS (canvas_parent_class)->unrealize) (widget);
1110 /**
1111  * The canvas widget's size_request callback.
1112  */
1113 static void
1114 sp_canvas_size_request (GtkWidget *widget, GtkRequisition *req)
1116     static_cast<void>(SP_CANVAS (widget));
1118     req->width = 256;
1119     req->height = 256;
1122 /**
1123  * The canvas widget's size_allocate callback.
1124  */
1125 static void
1126 sp_canvas_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
1128     SPCanvas *canvas = SP_CANVAS (widget);
1130     /* Schedule redraw of new region */
1131     sp_canvas_resize_tiles(canvas,canvas->x0,canvas->y0,canvas->x0+allocation->width,canvas->y0+allocation->height);
1132     if (allocation->width > widget->allocation.width) {
1133         sp_canvas_request_redraw (canvas,
1134                                   canvas->x0 + widget->allocation.width,
1135                                   0,
1136                                   canvas->x0 + allocation->width,
1137                                   canvas->y0 + allocation->height);
1138     }
1139     if (allocation->height > widget->allocation.height) {
1140         sp_canvas_request_redraw (canvas,
1141                                   0,
1142                                   canvas->y0 + widget->allocation.height,
1143                                   canvas->x0 + allocation->width,
1144                                   canvas->y0 + allocation->height);
1145     }
1147     widget->allocation = *allocation;
1149     if (GTK_WIDGET_REALIZED (widget)) {
1150         gdk_window_move_resize (widget->window,
1151                                 widget->allocation.x, widget->allocation.y,
1152                                 widget->allocation.width, widget->allocation.height);
1153     }
1156 /**
1157  * Helper that emits an event for an item in the canvas, be it the current 
1158  * item, grabbed item, or focused item, as appropriate.
1159  */
1160 static int
1161 emit_event (SPCanvas *canvas, GdkEvent *event)
1163     guint mask;
1165     if (canvas->grabbed_item) {
1166         switch (event->type) {
1167         case GDK_ENTER_NOTIFY:
1168             mask = GDK_ENTER_NOTIFY_MASK;
1169             break;
1170         case GDK_LEAVE_NOTIFY:
1171             mask = GDK_LEAVE_NOTIFY_MASK;
1172             break;
1173         case GDK_MOTION_NOTIFY:
1174             mask = GDK_POINTER_MOTION_MASK;
1175             break;
1176         case GDK_BUTTON_PRESS:
1177         case GDK_2BUTTON_PRESS:
1178         case GDK_3BUTTON_PRESS:
1179             mask = GDK_BUTTON_PRESS_MASK;
1180             break;
1181         case GDK_BUTTON_RELEASE:
1182             mask = GDK_BUTTON_RELEASE_MASK;
1183             break;
1184         case GDK_KEY_PRESS:
1185             mask = GDK_KEY_PRESS_MASK;
1186             break;
1187         case GDK_KEY_RELEASE:
1188             mask = GDK_KEY_RELEASE_MASK;
1189             break;
1190         case GDK_SCROLL:
1191             mask = GDK_SCROLL;
1192             break;
1193         default:
1194             mask = 0;
1195             break;
1196         }
1198         if (!(mask & canvas->grabbed_event_mask)) return FALSE;
1199     }
1201     /* Convert to world coordinates -- we have two cases because of diferent
1202      * offsets of the fields in the event structures.
1203      */
1205     GdkEvent ev = *event;
1207     switch (ev.type) {
1208     case GDK_ENTER_NOTIFY:
1209     case GDK_LEAVE_NOTIFY:
1210         ev.crossing.x += canvas->x0;
1211         ev.crossing.y += canvas->y0;
1212         break;
1213     case GDK_MOTION_NOTIFY:
1214     case GDK_BUTTON_PRESS:
1215     case GDK_2BUTTON_PRESS:
1216     case GDK_3BUTTON_PRESS:
1217     case GDK_BUTTON_RELEASE:
1218         ev.motion.x += canvas->x0;
1219         ev.motion.y += canvas->y0;
1220         break;
1221     default:
1222         break;
1223     }
1225     /* Choose where we send the event */
1227     /* canvas->current_item becomes NULL in some cases under Win32
1228     ** (e.g. if the pointer leaves the window).  So this is a hack that
1229     ** Lauris applied to SP to get around the problem.
1230     */
1231     SPCanvasItem* item = NULL;
1232     if (canvas->grabbed_item && !is_descendant (canvas->current_item, canvas->grabbed_item)) {
1233         item = canvas->grabbed_item;
1234     } else {
1235         item = canvas->current_item;
1236     }
1238     if (canvas->focused_item &&
1239         ((event->type == GDK_KEY_PRESS) ||
1240          (event->type == GDK_KEY_RELEASE) ||
1241          (event->type == GDK_FOCUS_CHANGE))) {
1242         item = canvas->focused_item;
1243     }
1245     /* The event is propagated up the hierarchy (for if someone connected to
1246      * a group instead of a leaf event), and emission is stopped if a
1247      * handler returns TRUE, just like for GtkWidget events.
1248      */
1250     gint finished = FALSE;
1252     while (item && !finished) {
1253         gtk_object_ref (GTK_OBJECT (item));
1254         gtk_signal_emit (GTK_OBJECT (item), item_signals[ITEM_EVENT], &ev, &finished);
1255         SPCanvasItem *parent = item->parent;
1256         gtk_object_unref (GTK_OBJECT (item));
1257         item = parent;
1258     }
1260     return finished;
1263 /**
1264  * Helper that re-picks the current item in the canvas, based on the event's 
1265  * coordinates and emits enter/leave events for items as appropriate.
1266  */
1267 static int
1268 pick_current_item (SPCanvas *canvas, GdkEvent *event)
1270     int button_down = 0;
1271     double x, y;
1273     int retval = FALSE;
1275     if (canvas->gen_all_enter_events == false) {
1276         // If a button is down, we'll perform enter and leave events on the
1277         // current item, but not enter on any other item.  This is more or
1278         // less like X pointer grabbing for canvas items.
1279         //
1280         button_down = canvas->state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1281                 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK | GDK_BUTTON5_MASK);
1283         if (!button_down) canvas->left_grabbed_item = FALSE;
1284     }
1286     /* Save the event in the canvas.  This is used to synthesize enter and
1287      * leave events in case the current item changes.  It is also used to
1288      * re-pick the current item if the current one gets deleted.  Also,
1289      * synthesize an enter event.
1290      */
1291     if (event != &canvas->pick_event) {
1292         if ((event->type == GDK_MOTION_NOTIFY) || (event->type == GDK_BUTTON_RELEASE)) {
1293             /* these fields have the same offsets in both types of events */
1295             canvas->pick_event.crossing.type       = GDK_ENTER_NOTIFY;
1296             canvas->pick_event.crossing.window     = event->motion.window;
1297             canvas->pick_event.crossing.send_event = event->motion.send_event;
1298             canvas->pick_event.crossing.subwindow  = NULL;
1299             canvas->pick_event.crossing.x          = event->motion.x;
1300             canvas->pick_event.crossing.y          = event->motion.y;
1301             canvas->pick_event.crossing.mode       = GDK_CROSSING_NORMAL;
1302             canvas->pick_event.crossing.detail     = GDK_NOTIFY_NONLINEAR;
1303             canvas->pick_event.crossing.focus      = FALSE;
1304             canvas->pick_event.crossing.state      = event->motion.state;
1306             /* these fields don't have the same offsets in both types of events */
1308             if (event->type == GDK_MOTION_NOTIFY) {
1309                 canvas->pick_event.crossing.x_root = event->motion.x_root;
1310                 canvas->pick_event.crossing.y_root = event->motion.y_root;
1311             } else {
1312                 canvas->pick_event.crossing.x_root = event->button.x_root;
1313                 canvas->pick_event.crossing.y_root = event->button.y_root;
1314             }
1315         } else {
1316             canvas->pick_event = *event;
1317         }
1318     }
1320     /* Don't do anything else if this is a recursive call */
1321     if (canvas->in_repick) return retval;
1323     /* LeaveNotify means that there is no current item, so we don't look for one */
1324     if (canvas->pick_event.type != GDK_LEAVE_NOTIFY) {
1325         /* these fields don't have the same offsets in both types of events */
1327         if (canvas->pick_event.type == GDK_ENTER_NOTIFY) {
1328             x = canvas->pick_event.crossing.x;
1329             y = canvas->pick_event.crossing.y;
1330         } else {
1331             x = canvas->pick_event.motion.x;
1332             y = canvas->pick_event.motion.y;
1333         }
1335         /* world coords */
1336         x += canvas->x0;
1337         y += canvas->y0;
1339         /* find the closest item */
1340         if (canvas->root->flags & SP_CANVAS_ITEM_VISIBLE) {
1341             sp_canvas_item_invoke_point (canvas->root, NR::Point(x, y), &canvas->new_current_item);
1342         } else {
1343             canvas->new_current_item = NULL;
1344         }
1345     } else {
1346         canvas->new_current_item = NULL;
1347     }
1349     if ((canvas->new_current_item == canvas->current_item) && !canvas->left_grabbed_item) {
1350         return retval; /* current item did not change */
1351     }
1353     /* Synthesize events for old and new current items */
1355     if ((canvas->new_current_item != canvas->current_item)
1356         && (canvas->current_item != NULL)
1357         && !canvas->left_grabbed_item) {
1358         GdkEvent new_event;
1359         SPCanvasItem *item;
1361         item = canvas->current_item;
1363         new_event = canvas->pick_event;
1364         new_event.type = GDK_LEAVE_NOTIFY;
1366         new_event.crossing.detail = GDK_NOTIFY_ANCESTOR;
1367         new_event.crossing.subwindow = NULL;
1368         canvas->in_repick = TRUE;
1369         retval = emit_event (canvas, &new_event);
1370         canvas->in_repick = FALSE;
1371     }
1373     if (canvas->gen_all_enter_events == false) {
1374         // new_current_item may have been set to NULL during the call to
1375         // emit_event() above
1376         if ((canvas->new_current_item != canvas->current_item) && button_down) {
1377             canvas->left_grabbed_item = TRUE;
1378             return retval;
1379         }
1380     }
1382     /* Handle the rest of cases */
1384     canvas->left_grabbed_item = FALSE;
1385     canvas->current_item = canvas->new_current_item;
1387     if (canvas->current_item != NULL) {
1388         GdkEvent new_event;
1390         new_event = canvas->pick_event;
1391         new_event.type = GDK_ENTER_NOTIFY;
1392         new_event.crossing.detail = GDK_NOTIFY_ANCESTOR;
1393         new_event.crossing.subwindow = NULL;
1394         retval = emit_event (canvas, &new_event);
1395     }
1397     return retval;
1400 /**
1401  * Button event handler for the canvas.
1402  */
1403 static gint
1404 sp_canvas_button (GtkWidget *widget, GdkEventButton *event)
1406     SPCanvas *canvas = SP_CANVAS (widget);
1408     int retval = FALSE;
1410     /* dispatch normally regardless of the event's window if an item has
1411        has a pointer grab in effect */
1412     if (!canvas->grabbed_item && 
1413         event->window != SP_CANVAS_WINDOW (canvas))
1414         return retval;
1416     int mask;
1417     switch (event->button) {
1418     case 1:
1419         mask = GDK_BUTTON1_MASK;
1420         break;
1421     case 2:
1422         mask = GDK_BUTTON2_MASK;
1423         break;
1424     case 3:
1425         mask = GDK_BUTTON3_MASK;
1426         break;
1427     case 4:
1428         mask = GDK_BUTTON4_MASK;
1429         break;
1430     case 5:
1431         mask = GDK_BUTTON5_MASK;
1432         break;
1433     default:
1434         mask = 0;
1435     }
1437     switch (event->type) {
1438     case GDK_BUTTON_PRESS:
1439     case GDK_2BUTTON_PRESS:
1440     case GDK_3BUTTON_PRESS:
1441         /* Pick the current item as if the button were not pressed, and
1442          * then process the event.
1443          */
1444         canvas->state = event->state;
1445         pick_current_item (canvas, (GdkEvent *) event);
1446         canvas->state ^= mask;
1447         retval = emit_event (canvas, (GdkEvent *) event);
1448         break;
1450     case GDK_BUTTON_RELEASE:
1451         /* Process the event as if the button were pressed, then repick
1452          * after the button has been released
1453          */
1454         canvas->state = event->state;
1455         retval = emit_event (canvas, (GdkEvent *) event);
1456         event->state ^= mask;
1457         canvas->state = event->state;
1458         pick_current_item (canvas, (GdkEvent *) event);
1459         event->state ^= mask;
1460         break;
1462     default:
1463         g_assert_not_reached ();
1464     }
1466     return retval;
1469 /**
1470  * Scroll event handler for the canvas.
1471  *
1472  * \todo FIXME: generate motion events to re-select items.
1473  */
1474 static gint
1475 sp_canvas_scroll (GtkWidget *widget, GdkEventScroll *event)
1477     return emit_event (SP_CANVAS (widget), (GdkEvent *) event);
1480 /**
1481  * Motion event handler for the canvas.
1482  */
1483 static int
1484 sp_canvas_motion (GtkWidget *widget, GdkEventMotion *event)
1486     SPCanvas *canvas = SP_CANVAS (widget);
1488     if (event->window != SP_CANVAS_WINDOW (canvas))
1489         return FALSE;
1491     if (canvas->grabbed_event_mask & GDK_POINTER_MOTION_HINT_MASK) {
1492         gint x, y;
1493         gdk_window_get_pointer (widget->window, &x, &y, NULL);
1494         event->x = x;
1495         event->y = y;
1496     }
1498     canvas->state = event->state;
1499     pick_current_item (canvas, (GdkEvent *) event);
1501     return emit_event (canvas, (GdkEvent *) event);
1504 static void
1505 sp_canvas_paint_single_buffer (SPCanvas *canvas, int x0, int y0, int x1, int y1, int draw_x1, int draw_y1, int draw_x2, int draw_y2, int sw)
1507     GtkWidget *widget = GTK_WIDGET (canvas);
1509     SPCanvasBuf buf;
1510     if (canvas->rendermode != RENDERMODE_OUTLINE) {
1511         buf.buf = nr_pixelstore_256K_new (FALSE, 0);
1512     } else {
1513         buf.buf = nr_pixelstore_1M_new (FALSE, 0);
1514     }
1516     buf.buf_rowstride = sw * 3;
1517     buf.rect.x0 = x0;
1518     buf.rect.y0 = y0;
1519     buf.rect.x1 = x1;
1520     buf.rect.y1 = y1;
1521     buf.visible_rect.x0 = draw_x1;
1522     buf.visible_rect.y0 = draw_y1;
1523     buf.visible_rect.x1 = draw_x2;
1524     buf.visible_rect.y1 = draw_y2;
1525     GdkColor *color = &widget->style->bg[GTK_STATE_NORMAL];
1526     buf.bg_color = (((color->red & 0xff00) << 8)
1527                     | (color->green & 0xff00)
1528                     | (color->blue >> 8));
1529     buf.is_empty = true;
1530       
1531     if (canvas->root->flags & SP_CANVAS_ITEM_VISIBLE) {
1532         SP_CANVAS_ITEM_GET_CLASS (canvas->root)->render (canvas->root, &buf);
1533     }
1534       
1535     if (buf.is_empty) {
1536         gdk_rgb_gc_set_foreground (canvas->pixmap_gc, buf.bg_color);
1537         gdk_draw_rectangle (SP_CANVAS_WINDOW (canvas),
1538                             canvas->pixmap_gc,
1539                             TRUE,
1540                             x0 - canvas->x0, y0 - canvas->y0,
1541                             x1 - x0, y1 - y0);
1542     } else {
1543         gdk_draw_rgb_image_dithalign (SP_CANVAS_WINDOW (canvas),
1544                                       canvas->pixmap_gc,
1545                                       x0 - canvas->x0, y0 - canvas->y0,
1546                                       x1 - x0, y1 - y0,
1547                                       GDK_RGB_DITHER_MAX,
1548                                       buf.buf,
1549                                       sw * 3,
1550                                       x0 - canvas->x0, y0 - canvas->y0);
1551     }
1553     if (canvas->rendermode != RENDERMODE_OUTLINE) {
1554         nr_pixelstore_256K_free (buf.buf);
1555     } else {
1556         nr_pixelstore_1M_free (buf.buf);
1557     }
1560 /**
1561  * Helper that draws a specific rectangular part of the canvas.
1562  */
1563 static void
1564 sp_canvas_paint_rect (SPCanvas *canvas, int xx0, int yy0, int xx1, int yy1)
1566     g_return_if_fail (!canvas->need_update);
1567  
1568     // Monotonously increment the canvas-global counter on each paint. This will let us find out
1569     // when a new paint happened in event processing during this paint, so we can abort it.
1570     canvas->redraw_count++;
1572     NRRectL rect;
1573     rect.x0 = xx0;
1574     rect.x1 = xx1;
1575     rect.y0 = yy0;
1576     rect.y1 = yy1;
1578     if (canvas->redraw_aborted.x0 < canvas->redraw_aborted.x1 || canvas->redraw_aborted.y0 < canvas->redraw_aborted.y1) {
1579         // There was an aborted redraw last time, now we need to redraw BOTH it and the new rect.
1580         
1581         // OPTIMIZATION IDEA:
1582         //   if (area(rect) + area(redraw_aborted)) * 1.2 > area (union)
1583         //   then paint their union (as done below)
1584         //   else
1585         //   two_rects = true; paint new rect and aborted rect SEPARATELY without unioning.
1586         // Without this, when you scroll down and quickly up, the entire screen has to be redrawn,
1587         // because the union of the aborted strip at top and the newly exposed strip at bottom
1588         // covers the whole screen.
1589     
1590         // For now, just always do a union.
1591         rect.x0 = MIN(rect.x0, canvas->redraw_aborted.x0);
1592         rect.x1 = MAX(rect.x1, canvas->redraw_aborted.x1);
1593         rect.y0 = MIN(rect.y0, canvas->redraw_aborted.y0);
1594         rect.y1 = MAX(rect.y1, canvas->redraw_aborted.y1);
1595     }
1597     // Clip drawable rect by the visible area
1598     int draw_x1 = MAX (rect.x0, canvas->x0);
1599     int draw_y1 = MAX (rect.y0, canvas->y0);
1600     int draw_x2 = MIN (rect.x1, canvas->x0/*draw_x1*/ + GTK_WIDGET (canvas)->allocation.width);
1601     int draw_y2 = MIN (rect.y1, canvas->y0/*draw_y1*/ + GTK_WIDGET (canvas)->allocation.height);
1603     // Here we'll store the time it took to draw the slowest buffer of this paint. 
1604     glong slowest_buffer = 0;
1606     // Initially, the rect to redraw later (in case we're aborted) is the same as the one we're going to draw now.
1607     canvas->redraw_aborted.x0 = draw_x1;
1608     canvas->redraw_aborted.x1 = draw_x2;
1609     canvas->redraw_aborted.y0 = draw_y1;
1610     canvas->redraw_aborted.y1 = draw_y2;
1612     // Find the optimal buffer dimensions
1613     int bw = draw_x2 - draw_x1;
1614     int bh = draw_y2 - draw_y1;
1615     if ((bw < 1) || (bh < 1))
1616         return;
1617     int sw, sh;
1618     if (canvas->rendermode != RENDERMODE_OUTLINE) { // use 256K as a compromise to not slow down gradients
1619         /* 256K is the cached buffer and we need 3 channels */
1620         if (bw * bh <  87381) { // 256K/3
1621             // We can go with single buffer 
1622             sw = bw;
1623             sh = bh;
1624         } else if (bw <= (16 * 341)) {
1625             // Go with row buffer 
1626             sw = bw;
1627             sh =  87381 / bw;
1628         } else if (bh <= (16 * 256)) {
1629             // Go with column buffer 
1630             sw = 87381 / bh;
1631             sh = bh;
1632         } else {
1633             sw = 341;
1634             sh = 256;
1635         }
1636     } else {  // paths only, so 1M works faster
1637         /* 1M is the cached buffer and we need 3 channels */
1638         if (bw * bh <  349525) { // 1M/3
1639             // We can go with single buffer 
1640             sw = bw;
1641             sh = bh;
1642         } else if (bw <= (16 * 682)) {
1643             // Go with row buffer 
1644             sw = bw;
1645             sh =  349525 / bw;
1646         } else if (bh <= (16 * 512)) {
1647             // Go with column buffer 
1648             sw = 349525 / bh;
1649             sh = bh;
1650         } else {
1651             sw = 682;
1652             sh = 512;
1653         }
1654     }
1655     
1656     // Will this paint require more than one buffer?
1657     bool multiple_buffers = (((draw_y2 - draw_y1) > sh) || ((draw_x2 - draw_x1) > sw)); // or two_rects
1659     // remember the counter during this paint
1660     long this_count = canvas->redraw_count;
1662     // Time values to measure each buffer's paint time
1663     GTimeVal tstart, tfinish;
1665     // This is the main loop which corresponds to the visible left-to-right, top-to-bottom drawing
1666     // of screen blocks (buffers).
1667     for (int y0 = draw_y1; y0 < draw_y2; y0 += sh) {
1668         int y1 = MIN (y0 + sh, draw_y2);
1669         for (int x0 = draw_x1; x0 < draw_x2; x0 += sw) {
1670             int x1 = MIN (x0 + sw, draw_x2);
1672             // OPTIMIZATION IDEA: if drawing is really slow (as measured by canvas->slowest
1673             // buffer), process some events even BEFORE we do any buffers?
1674             
1675             // Paint one buffer; measure how long it takes.
1676             g_get_current_time (&tstart);
1677             sp_canvas_paint_single_buffer (canvas, x0, y0, x1, y1, draw_x1, draw_y1, draw_x2, draw_y2, sw);
1678             g_get_current_time (&tfinish);
1680             // Remember the slowest_buffer of this paint.
1681             glong this_buffer = (tfinish.tv_sec - tstart.tv_sec) * 1000000 + (tfinish.tv_usec - tstart.tv_usec);
1682             if (this_buffer > slowest_buffer) 
1683                 slowest_buffer = this_buffer;
1685             // After each successful buffer, reduce the rect remaining to redraw by what is already redrawn
1686             if (x1 >= draw_x2 && canvas->redraw_aborted.y0 < y1)
1687                 canvas->redraw_aborted.y0 = y1;
1688             if (y1 >= draw_y2 && canvas->redraw_aborted.x0 < x1)
1689                 canvas->redraw_aborted.x0 = x1;
1691             // INTERRUPTIBLE DISPLAY:
1692             // Process events that may have arrived while we were busy drawing;
1693             // only if we're drawing multiple buffers, and only if this one was not very fast
1694             if (multiple_buffers && this_buffer > 25000) {
1696                 // Run at most max_iterations of the main loop; we cannot process ALL events
1697                 // here because some things (e.g. rubberband) flood with dirtying events but will
1698                 // not redraw themselves
1699                 int max_iterations = 10;
1700                 int iterations = 0;
1701                 while (Gtk::Main::events_pending() && iterations++ < max_iterations) {
1702                     Gtk::Main::iteration(false);
1703                     // If one of the iterations has redrawn by itself, abort
1704                     if (this_count != canvas->redraw_count) {
1705                         canvas->slowest_buffer = slowest_buffer;
1706                         return;
1707                     }
1708                 }
1709    
1710                 // If not aborted so far, check if the events set redraw or update flags; 
1711                 // if so, force update and abort
1712                 if (canvas->need_redraw || canvas->need_update) {
1713                     canvas->slowest_buffer = slowest_buffer;
1714                     do_update (canvas);
1715                     return;
1716                 }
1717             }
1718         }
1719     }
1721     // Remember the slowest buffer of this paint in canvas
1722     canvas->slowest_buffer = slowest_buffer;
1725 /**
1726  * The canvas widget's expose callback.
1727  */
1728 static gint
1729 sp_canvas_expose (GtkWidget *widget, GdkEventExpose *event)
1731     SPCanvas *canvas = SP_CANVAS (widget);
1733     if (!GTK_WIDGET_DRAWABLE (widget) || 
1734         (event->window != SP_CANVAS_WINDOW (canvas)))
1735         return FALSE;
1737     int n_rects;
1738     GdkRectangle *rects;
1739     gdk_region_get_rectangles (event->region, &rects, &n_rects);
1741     for (int i = 0; i < n_rects; i++) {
1742         NRRectL rect;
1743                 
1744         rect.x0 = rects[i].x + canvas->x0;
1745         rect.y0 = rects[i].y + canvas->y0;
1746         rect.x1 = rect.x0 + rects[i].width;
1747         rect.y1 = rect.y0 + rects[i].height;
1749         sp_canvas_request_redraw (canvas, rect.x0, rect.y0, rect.x1, rect.y1);
1750     }
1752     if (n_rects > 0)
1753         g_free (rects);
1755     return FALSE;
1758 /**
1759  * The canvas widget's keypress callback.
1760  */
1761 static gint
1762 sp_canvas_key (GtkWidget *widget, GdkEventKey *event)
1764     return emit_event (SP_CANVAS (widget), (GdkEvent *) event);
1767 /**
1768  * Crossing event handler for the canvas.
1769  */
1770 static gint
1771 sp_canvas_crossing (GtkWidget *widget, GdkEventCrossing *event)
1773     SPCanvas *canvas = SP_CANVAS (widget);
1775     if (event->window != SP_CANVAS_WINDOW (canvas))
1776         return FALSE;
1778     canvas->state = event->state;
1779     return pick_current_item (canvas, (GdkEvent *) event);
1782 /**
1783  * Focus in handler for the canvas.
1784  */
1785 static gint
1786 sp_canvas_focus_in (GtkWidget *widget, GdkEventFocus *event)
1788     GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
1790     SPCanvas *canvas = SP_CANVAS (widget);
1792     if (canvas->focused_item) {
1793         return emit_event (canvas, (GdkEvent *) event);
1794     } else {
1795         return FALSE;
1796     }
1799 /**
1800  * Focus out handler for the canvas.
1801  */
1802 static gint
1803 sp_canvas_focus_out (GtkWidget *widget, GdkEventFocus *event)
1805     GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
1807     SPCanvas *canvas = SP_CANVAS (widget);
1809     if (canvas->focused_item)
1810         return emit_event (canvas, (GdkEvent *) event);
1811     else
1812         return FALSE;
1815 /**
1816  * Helper that repaints the areas in the canvas that need it.
1817  */
1818 static int
1819 paint (SPCanvas *canvas)
1821     if (canvas->need_update) {
1822         sp_canvas_item_invoke_update (canvas->root, NR::identity(), 0);
1823         canvas->need_update = FALSE;
1824     }
1826     if (!canvas->need_redraw)
1827         return TRUE;
1829     GtkWidget const *widget = GTK_WIDGET(canvas);
1830     int const canvas_x1 = canvas->x0 + widget->allocation.width;
1831     int const canvas_y1 = canvas->y0 + widget->allocation.height;
1833     NRRectL topaint;
1834     topaint.x0 = topaint.y0 = topaint.x1 = topaint.y1 = 0;
1836     for (int j=canvas->tTop&(~3);j<canvas->tBottom;j+=4) {
1837         for (int i=canvas->tLeft&(~3);i<canvas->tRight;i+=4) {
1838             int  mode=0;
1839       
1840             int pl=i+1,pr=i,pt=j+4,pb=j;
1841             for (int l=MAX(j,canvas->tTop);l<MIN(j+4,canvas->tBottom);l++) {
1842                 for (int k=MAX(i,canvas->tLeft);k<MIN(i+4,canvas->tRight);k++) {
1843                     if ( canvas->tiles[(k-canvas->tLeft)+(l-canvas->tTop)*canvas->tileH] ) {
1844                         mode|=1<<((k-i)+(l-j)*4);
1845                         if ( k < pl ) pl=k;
1846                         if ( k+1 > pr ) pr=k+1;
1847                         if ( l < pt ) pt=l;
1848                         if ( l+1 > pb ) pb=l+1;
1849                     }
1850                     canvas->tiles[(k-canvas->tLeft)+(l-canvas->tTop)*canvas->tileH]=0;
1851                 }
1852             }
1853       
1854             if ( mode ) {
1855                 NRRectL tile;
1856                 tile.x0 = MAX (pl*32, canvas->x0);
1857                 tile.y0 = MAX (pt*32, canvas->y0);
1858                 tile.x1 = MIN (pr*32, canvas_x1);
1859                 tile.y1 = MIN (pb*32, canvas_y1);
1860                 if ((tile.x0 < tile.x1) && (tile.y0 < tile.y1)) {
1861                     nr_rect_l_union (&topaint, &topaint, &tile);
1862                 }
1864             }
1865         }
1866     }
1868     canvas->need_redraw = FALSE;
1869     sp_canvas_paint_rect (canvas, topaint.x0, topaint.y0, topaint.x1, topaint.y1);
1871     return TRUE;
1874 /**
1875  * Helper that invokes update, paint, and repick on canvas.
1876  */
1877 static int
1878 do_update (SPCanvas *canvas)
1880     if (!canvas->root) // canvas may have already be destroyed by closing desktop durring interrupted display!
1881         return TRUE;
1883     /* Cause the update if necessary */
1884     if (canvas->need_update) {
1885         sp_canvas_item_invoke_update (canvas->root, NR::identity(), 0);
1886         canvas->need_update = FALSE;
1887     }
1889     /* Paint if able to */
1890     if (GTK_WIDGET_DRAWABLE (canvas)) {
1891             return paint (canvas);
1892     }
1894     /* Pick new current item */
1895     while (canvas->need_repick) {
1896         canvas->need_repick = FALSE;
1897         pick_current_item (canvas, &canvas->pick_event);
1898     }
1900     return TRUE;
1903 /**
1904  * Idle handler for the canvas that deals with pending updates and redraws.
1905  */
1906 static gint
1907 idle_handler (gpointer data)
1909     GDK_THREADS_ENTER ();
1911     SPCanvas *canvas = SP_CANVAS (data);
1913     const int ret = do_update (canvas);
1915     if (ret) {
1916         /* Reset idle id */
1917         canvas->idle_id = 0;
1918     }
1920     GDK_THREADS_LEAVE ();
1922     return !ret;
1925 /**
1926  * Convenience function to add an idle handler to a canvas.
1927  */
1928 static void
1929 add_idle (SPCanvas *canvas)
1931     if (canvas->idle_id != 0)
1932         return;
1934     canvas->idle_id = gtk_idle_add_priority (sp_canvas_update_priority, idle_handler, canvas);
1937 /**
1938  * Returns the root group of the specified canvas.
1939  */
1940 SPCanvasGroup *
1941 sp_canvas_root (SPCanvas *canvas)
1943     g_return_val_if_fail (canvas != NULL, NULL);
1944     g_return_val_if_fail (SP_IS_CANVAS (canvas), NULL);
1946     return SP_CANVAS_GROUP (canvas->root);
1949 /**
1950  * Scrolls canvas to specific position.
1951  */
1952 void
1953 sp_canvas_scroll_to (SPCanvas *canvas, double cx, double cy, unsigned int clear)
1955     g_return_if_fail (canvas != NULL);
1956     g_return_if_fail (SP_IS_CANVAS (canvas));
1958     int ix = (int) (cx + 0.5);
1959     int iy = (int) (cy + 0.5);
1960     int dx = ix - canvas->x0;
1961     int dy = iy - canvas->y0;
1963     canvas->dx0 = cx;
1964     canvas->dy0 = cy;
1965     canvas->x0 = ix;
1966     canvas->y0 = iy;
1968     sp_canvas_resize_tiles(canvas,canvas->x0,canvas->y0,canvas->x0+canvas->widget.allocation.width,canvas->y0+canvas->widget.allocation.height);
1970     if (!clear) {
1971         // scrolling without zoom; redraw only the newly exposed areas
1972         if ((dx != 0) || (dy != 0)) {
1973             int width, height;
1974             width = canvas->widget.allocation.width;
1975             height = canvas->widget.allocation.height;
1976             if (GTK_WIDGET_REALIZED (canvas)) {
1977                 gdk_window_scroll (SP_CANVAS_WINDOW (canvas), -dx, -dy);
1978                 gdk_window_process_updates (SP_CANVAS_WINDOW (canvas), TRUE);
1979             }
1980             if (dx < 0) {
1981                 sp_canvas_request_redraw (canvas, ix + 0, iy + 0, ix - dx, iy + height);
1982             } else if (dx > 0) {
1983                 sp_canvas_request_redraw (canvas, ix + width - dx, iy + 0, ix + width, iy + height);
1984             }
1985             if (dy < 0) {
1986                 sp_canvas_request_redraw (canvas, ix + 0, iy + 0, ix + width, iy - dy);
1987             } else if (dy > 0) {
1988                 sp_canvas_request_redraw (canvas, ix + 0, iy + height - dy, ix + width, iy + height);
1989             }
1990         }
1991     } else {
1992         // scrolling as part of zoom; do nothing here - the next do_update will perform full redraw
1993     }
1996 /** 
1997  * Updates canvas if necessary.
1998  */
1999 void
2000 sp_canvas_update_now (SPCanvas *canvas)
2002     g_return_if_fail (canvas != NULL);
2003     g_return_if_fail (SP_IS_CANVAS (canvas));
2005     if (!(canvas->need_update ||
2006           canvas->need_redraw))
2007         return;
2009     remove_idle (canvas);
2010     do_update (canvas);
2013 /**
2014  * Update callback for canvas widget.
2015  */
2016 static void
2017 sp_canvas_request_update (SPCanvas *canvas)
2019     canvas->need_update = TRUE;
2020     add_idle (canvas);
2023 /**
2024  * Forces redraw of rectangular canvas area.
2025  */
2026 void
2027 sp_canvas_request_redraw (SPCanvas *canvas, int x0, int y0, int x1, int y1)
2029     NRRectL bbox;
2030     NRRectL visible;
2031     NRRectL clip;
2033     g_return_if_fail (canvas != NULL);
2034     g_return_if_fail (SP_IS_CANVAS (canvas));
2036     if (!GTK_WIDGET_DRAWABLE (canvas)) return;
2037     if ((x0 >= x1) || (y0 >= y1)) return;
2039     bbox.x0 = x0;
2040     bbox.y0 = y0;
2041     bbox.x1 = x1;
2042     bbox.y1 = y1;
2044     visible.x0 = canvas->x0;
2045     visible.y0 = canvas->y0;
2046     visible.x1 = visible.x0 + GTK_WIDGET (canvas)->allocation.width;
2047     visible.y1 = visible.y0 + GTK_WIDGET (canvas)->allocation.height;
2049     nr_rect_l_intersect (&clip, &bbox, &visible);
2051     sp_canvas_dirty_rect(canvas, clip.x0, clip.y0, clip.x1, clip.y1);
2052     add_idle (canvas);
2055 /**
2056  * Sets world coordinates from win and canvas.
2057  */
2058 void sp_canvas_window_to_world(SPCanvas const *canvas, double winx, double winy, double *worldx, double *worldy)
2060     g_return_if_fail (canvas != NULL);
2061     g_return_if_fail (SP_IS_CANVAS (canvas));
2063     if (worldx) *worldx = canvas->x0 + winx;
2064     if (worldy) *worldy = canvas->y0 + winy;
2067 /**
2068  * Sets win coordinates from world and canvas.
2069  */
2070 void sp_canvas_world_to_window(SPCanvas const *canvas, double worldx, double worldy, double *winx, double *winy)
2072     g_return_if_fail (canvas != NULL);
2073     g_return_if_fail (SP_IS_CANVAS (canvas));
2075     if (winx) *winx = worldx - canvas->x0;
2076     if (winy) *winy = worldy - canvas->y0;
2079 /**
2080  * Converts point from win to world coordinates.
2081  */
2082 NR::Point sp_canvas_window_to_world(SPCanvas const *canvas, NR::Point const win)
2084     g_assert (canvas != NULL);
2085     g_assert (SP_IS_CANVAS (canvas));
2087     return NR::Point(canvas->x0 + win[0], canvas->y0 + win[1]);
2090 /**
2091  * Converts point from world to win coordinates.
2092  */
2093 NR::Point sp_canvas_world_to_window(SPCanvas const *canvas, NR::Point const world)
2095     g_assert (canvas != NULL);
2096     g_assert (SP_IS_CANVAS (canvas));
2098     return NR::Point(world[0] - canvas->x0, world[1] - canvas->y0);
2101 /**
2102  * Returns true if point given in world coordinates is inside window.
2103  */
2104 bool sp_canvas_world_pt_inside_window(SPCanvas const *canvas, NR::Point const &world)
2106     g_assert( canvas != NULL );
2107     g_assert(SP_IS_CANVAS(canvas));
2109     using NR::X;
2110     using NR::Y;
2111     GtkWidget const &w = *GTK_WIDGET(canvas);
2112     return ( ( canvas->x0 <= world[X] )  &&
2113              ( canvas->y0 <= world[Y] )  &&
2114              ( world[X] < canvas->x0 + w.allocation.width )  &&
2115              ( world[Y] < canvas->y0 + w.allocation.height ) );
2118 /**
2119  * Return canvas window coordinates as NRRect.
2120  */
2121 NR::Rect SPCanvas::getViewbox() const
2123     GtkWidget const *w = GTK_WIDGET(this);
2125     return NR::Rect(NR::Point(dx0, dy0),
2126                     NR::Point(dx0 + w->allocation.width, dy0 + w->allocation.height));
2129 inline int sp_canvas_tile_floor(int x)
2131     return (x&(~31))/32;
2134 inline int sp_canvas_tile_ceil(int x)
2136     return ((x+31)&(~31))/32;
2139 /**
2140  * Helper that changes tile size for canvas redraw.
2141  */
2142 void sp_canvas_resize_tiles(SPCanvas* canvas,int nl,int nt,int nr,int nb)
2144     if ( nl >= nr || nt >= nb ) {
2145         if ( canvas->tiles ) g_free(canvas->tiles);
2146         canvas->tLeft=canvas->tTop=canvas->tRight=canvas->tBottom=0;
2147         canvas->tileH=canvas->tileV=0;
2148         canvas->tiles=NULL;
2149         return;
2150     }
2151     int tl=sp_canvas_tile_floor(nl);
2152     int tt=sp_canvas_tile_floor(nt);
2153     int tr=sp_canvas_tile_ceil(nr);
2154     int tb=sp_canvas_tile_ceil(nb);
2156     int nh=tr-tl,nv=tb-tt;
2157     uint8_t* ntiles=(uint8_t*)g_malloc(nh*nv*sizeof(uint8_t));
2158     for (int i=tl;i<tr;i++) {
2159         for (int j=tt;j<tb;j++) {
2160             int ind=(i-tl)+(j-tt)*nh;
2161             if ( i >= canvas->tLeft && i < canvas->tRight && j >= canvas->tTop && j < canvas->tBottom ) {
2162                 ntiles[ind]=canvas->tiles[(i-canvas->tLeft)+(j-canvas->tTop)*canvas->tileH];
2163             } else {
2164                 ntiles[ind]=0;
2165             }
2166         }
2167     }
2168     if ( canvas->tiles ) g_free(canvas->tiles);
2169     canvas->tiles=ntiles;
2170     canvas->tLeft=tl;
2171     canvas->tTop=tt;
2172     canvas->tRight=tr;
2173     canvas->tBottom=tb;
2174     canvas->tileH=nh;
2175     canvas->tileV=nv;
2178 /**
2179  * Helper that marks specific canvas rectangle for redraw.
2180  */
2181 void sp_canvas_dirty_rect(SPCanvas* canvas,int nl,int nt,int nr,int nb)
2183     if ( nl >= nr || nt >= nb ) {
2184         return;
2185     }
2186     int tl=sp_canvas_tile_floor(nl);
2187     int tt=sp_canvas_tile_floor(nt);
2188     int tr=sp_canvas_tile_ceil(nr);
2189     int tb=sp_canvas_tile_ceil(nb);
2190     if ( tl >= canvas->tRight || tr <= canvas->tLeft || tt >= canvas->tBottom || tb <= canvas->tTop ) return;
2191     if ( tl < canvas->tLeft ) tl=canvas->tLeft;
2192     if ( tr > canvas->tRight ) tr=canvas->tRight;
2193     if ( tt < canvas->tTop ) tt=canvas->tTop;
2194     if ( tb > canvas->tBottom ) tb=canvas->tBottom;
2196     canvas->need_redraw = TRUE;
2198     for (int i=tl;i<tr;i++) {
2199         for (int j=tt;j<tb;j++) {
2200             canvas->tiles[(i-canvas->tLeft)+(j-canvas->tTop)*canvas->tileH]=1;
2201         }
2202     }
2206 /*
2207   Local Variables:
2208   mode:c++
2209   c-file-style:"stroustrup"
2210   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2211   indent-tabs-mode:nil
2212   fill-column:99
2213   End:
2214 */
2215 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :