Code

Add ability to force canvas to perform a full, non-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;
980     
981     canvas->forced_full_redraw_count = 0;
983     canvas->slowest_buffer = 0;
986 /**
987  * Convenience function to remove the idle handler of a canvas.
988  */
989 static void
990 remove_idle (SPCanvas *canvas)
992     if (canvas->idle_id) {
993         gtk_idle_remove (canvas->idle_id);
994         canvas->idle_id = 0;
995     }
998 /*
999  * Removes the transient state of the canvas (idle handler, grabs).
1000  */
1001 static void
1002 shutdown_transients (SPCanvas *canvas)
1004     /* We turn off the need_redraw flag, since if the canvas is mapped again
1005      * it will request a redraw anyways.  We do not turn off the need_update
1006      * flag, though, because updates are not queued when the canvas remaps
1007      * itself.
1008      */
1009     if (canvas->need_redraw) {
1010         canvas->need_redraw = FALSE;
1011     }
1012     if ( canvas->tiles ) g_free(canvas->tiles);
1013     canvas->tiles=NULL;
1014     canvas->tLeft=canvas->tTop=canvas->tRight=canvas->tBottom=0;
1015     canvas->tileH=canvas->tileV=0;
1017     if (canvas->grabbed_item) {
1018         canvas->grabbed_item = NULL;
1019         gdk_pointer_ungrab (GDK_CURRENT_TIME);
1020     }
1022     remove_idle (canvas);
1025 /**
1026  * Destroy handler for SPCanvas.
1027  */
1028 static void
1029 sp_canvas_destroy (GtkObject *object)
1031     SPCanvas *canvas = SP_CANVAS (object);
1033     if (canvas->root) {
1034         gtk_object_unref (GTK_OBJECT (canvas->root));
1035         canvas->root = NULL;
1036     }
1038     shutdown_transients (canvas);
1040     if (GTK_OBJECT_CLASS (canvas_parent_class)->destroy)
1041         (* GTK_OBJECT_CLASS (canvas_parent_class)->destroy) (object);
1044 /**
1045  * Returns new canvas as widget.
1046  */
1047 GtkWidget *
1048 sp_canvas_new_aa (void)
1050     SPCanvas *canvas = (SPCanvas *)gtk_type_new (sp_canvas_get_type ());
1052     return (GtkWidget *) canvas;
1055 /**
1056  * The canvas widget's realize callback.
1057  */
1058 static void
1059 sp_canvas_realize (GtkWidget *widget)
1061     SPCanvas *canvas = SP_CANVAS (widget);
1063     GdkWindowAttr attributes;
1064     attributes.window_type = GDK_WINDOW_CHILD;
1065     attributes.x = widget->allocation.x;
1066     attributes.y = widget->allocation.y;
1067     attributes.width = widget->allocation.width;
1068     attributes.height = widget->allocation.height;
1069     attributes.wclass = GDK_INPUT_OUTPUT;
1070     attributes.visual = gdk_rgb_get_visual ();
1071     attributes.colormap = gdk_rgb_get_cmap ();
1072     attributes.event_mask = (gtk_widget_get_events (widget) |
1073                              GDK_EXPOSURE_MASK |
1074                              GDK_BUTTON_PRESS_MASK |
1075                              GDK_BUTTON_RELEASE_MASK |
1076                              GDK_POINTER_MOTION_MASK |
1077                              GDK_PROXIMITY_IN_MASK |
1078                              GDK_PROXIMITY_OUT_MASK |
1079                              GDK_KEY_PRESS_MASK |
1080                              GDK_KEY_RELEASE_MASK |
1081                              GDK_ENTER_NOTIFY_MASK |
1082                              GDK_LEAVE_NOTIFY_MASK |
1083                              GDK_FOCUS_CHANGE_MASK);
1084     gint attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1086     widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1087     gdk_window_set_user_data (widget->window, widget);
1088     gtk_widget_set_events(widget, attributes.event_mask);
1090     GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1092     canvas->pixmap_gc = gdk_gc_new (SP_CANVAS_WINDOW (canvas));
1095 /**
1096  * The canvas widget's unrealize callback.
1097  */
1098 static void
1099 sp_canvas_unrealize (GtkWidget *widget)
1101     SPCanvas *canvas = SP_CANVAS (widget);
1103     shutdown_transients (canvas);
1105     gdk_gc_destroy (canvas->pixmap_gc);
1106     canvas->pixmap_gc = NULL;
1108     if (GTK_WIDGET_CLASS (canvas_parent_class)->unrealize)
1109         (* GTK_WIDGET_CLASS (canvas_parent_class)->unrealize) (widget);
1112 /**
1113  * The canvas widget's size_request callback.
1114  */
1115 static void
1116 sp_canvas_size_request (GtkWidget *widget, GtkRequisition *req)
1118     static_cast<void>(SP_CANVAS (widget));
1120     req->width = 256;
1121     req->height = 256;
1124 /**
1125  * The canvas widget's size_allocate callback.
1126  */
1127 static void
1128 sp_canvas_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
1130     SPCanvas *canvas = SP_CANVAS (widget);
1132     /* Schedule redraw of new region */
1133     sp_canvas_resize_tiles(canvas,canvas->x0,canvas->y0,canvas->x0+allocation->width,canvas->y0+allocation->height);
1134     if (allocation->width > widget->allocation.width) {
1135         sp_canvas_request_redraw (canvas,
1136                                   canvas->x0 + widget->allocation.width,
1137                                   0,
1138                                   canvas->x0 + allocation->width,
1139                                   canvas->y0 + allocation->height);
1140     }
1141     if (allocation->height > widget->allocation.height) {
1142         sp_canvas_request_redraw (canvas,
1143                                   0,
1144                                   canvas->y0 + widget->allocation.height,
1145                                   canvas->x0 + allocation->width,
1146                                   canvas->y0 + allocation->height);
1147     }
1149     widget->allocation = *allocation;
1151     if (GTK_WIDGET_REALIZED (widget)) {
1152         gdk_window_move_resize (widget->window,
1153                                 widget->allocation.x, widget->allocation.y,
1154                                 widget->allocation.width, widget->allocation.height);
1155     }
1158 /**
1159  * Helper that emits an event for an item in the canvas, be it the current 
1160  * item, grabbed item, or focused item, as appropriate.
1161  */
1162 static int
1163 emit_event (SPCanvas *canvas, GdkEvent *event)
1165     guint mask;
1167     if (canvas->grabbed_item) {
1168         switch (event->type) {
1169         case GDK_ENTER_NOTIFY:
1170             mask = GDK_ENTER_NOTIFY_MASK;
1171             break;
1172         case GDK_LEAVE_NOTIFY:
1173             mask = GDK_LEAVE_NOTIFY_MASK;
1174             break;
1175         case GDK_MOTION_NOTIFY:
1176             mask = GDK_POINTER_MOTION_MASK;
1177             break;
1178         case GDK_BUTTON_PRESS:
1179         case GDK_2BUTTON_PRESS:
1180         case GDK_3BUTTON_PRESS:
1181             mask = GDK_BUTTON_PRESS_MASK;
1182             break;
1183         case GDK_BUTTON_RELEASE:
1184             mask = GDK_BUTTON_RELEASE_MASK;
1185             break;
1186         case GDK_KEY_PRESS:
1187             mask = GDK_KEY_PRESS_MASK;
1188             break;
1189         case GDK_KEY_RELEASE:
1190             mask = GDK_KEY_RELEASE_MASK;
1191             break;
1192         case GDK_SCROLL:
1193             mask = GDK_SCROLL;
1194             break;
1195         default:
1196             mask = 0;
1197             break;
1198         }
1200         if (!(mask & canvas->grabbed_event_mask)) return FALSE;
1201     }
1203     /* Convert to world coordinates -- we have two cases because of diferent
1204      * offsets of the fields in the event structures.
1205      */
1207     GdkEvent ev = *event;
1209     switch (ev.type) {
1210     case GDK_ENTER_NOTIFY:
1211     case GDK_LEAVE_NOTIFY:
1212         ev.crossing.x += canvas->x0;
1213         ev.crossing.y += canvas->y0;
1214         break;
1215     case GDK_MOTION_NOTIFY:
1216     case GDK_BUTTON_PRESS:
1217     case GDK_2BUTTON_PRESS:
1218     case GDK_3BUTTON_PRESS:
1219     case GDK_BUTTON_RELEASE:
1220         ev.motion.x += canvas->x0;
1221         ev.motion.y += canvas->y0;
1222         break;
1223     default:
1224         break;
1225     }
1227     /* Choose where we send the event */
1229     /* canvas->current_item becomes NULL in some cases under Win32
1230     ** (e.g. if the pointer leaves the window).  So this is a hack that
1231     ** Lauris applied to SP to get around the problem.
1232     */
1233     SPCanvasItem* item = NULL;
1234     if (canvas->grabbed_item && !is_descendant (canvas->current_item, canvas->grabbed_item)) {
1235         item = canvas->grabbed_item;
1236     } else {
1237         item = canvas->current_item;
1238     }
1240     if (canvas->focused_item &&
1241         ((event->type == GDK_KEY_PRESS) ||
1242          (event->type == GDK_KEY_RELEASE) ||
1243          (event->type == GDK_FOCUS_CHANGE))) {
1244         item = canvas->focused_item;
1245     }
1247     /* The event is propagated up the hierarchy (for if someone connected to
1248      * a group instead of a leaf event), and emission is stopped if a
1249      * handler returns TRUE, just like for GtkWidget events.
1250      */
1252     gint finished = FALSE;
1254     while (item && !finished) {
1255         gtk_object_ref (GTK_OBJECT (item));
1256         gtk_signal_emit (GTK_OBJECT (item), item_signals[ITEM_EVENT], &ev, &finished);
1257         SPCanvasItem *parent = item->parent;
1258         gtk_object_unref (GTK_OBJECT (item));
1259         item = parent;
1260     }
1262     return finished;
1265 /**
1266  * Helper that re-picks the current item in the canvas, based on the event's 
1267  * coordinates and emits enter/leave events for items as appropriate.
1268  */
1269 static int
1270 pick_current_item (SPCanvas *canvas, GdkEvent *event)
1272     int button_down = 0;
1273     double x, y;
1275     int retval = FALSE;
1277     if (canvas->gen_all_enter_events == false) {
1278         // If a button is down, we'll perform enter and leave events on the
1279         // current item, but not enter on any other item.  This is more or
1280         // less like X pointer grabbing for canvas items.
1281         //
1282         button_down = canvas->state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1283                 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK | GDK_BUTTON5_MASK);
1285         if (!button_down) canvas->left_grabbed_item = FALSE;
1286     }
1288     /* Save the event in the canvas.  This is used to synthesize enter and
1289      * leave events in case the current item changes.  It is also used to
1290      * re-pick the current item if the current one gets deleted.  Also,
1291      * synthesize an enter event.
1292      */
1293     if (event != &canvas->pick_event) {
1294         if ((event->type == GDK_MOTION_NOTIFY) || (event->type == GDK_BUTTON_RELEASE)) {
1295             /* these fields have the same offsets in both types of events */
1297             canvas->pick_event.crossing.type       = GDK_ENTER_NOTIFY;
1298             canvas->pick_event.crossing.window     = event->motion.window;
1299             canvas->pick_event.crossing.send_event = event->motion.send_event;
1300             canvas->pick_event.crossing.subwindow  = NULL;
1301             canvas->pick_event.crossing.x          = event->motion.x;
1302             canvas->pick_event.crossing.y          = event->motion.y;
1303             canvas->pick_event.crossing.mode       = GDK_CROSSING_NORMAL;
1304             canvas->pick_event.crossing.detail     = GDK_NOTIFY_NONLINEAR;
1305             canvas->pick_event.crossing.focus      = FALSE;
1306             canvas->pick_event.crossing.state      = event->motion.state;
1308             /* these fields don't have the same offsets in both types of events */
1310             if (event->type == GDK_MOTION_NOTIFY) {
1311                 canvas->pick_event.crossing.x_root = event->motion.x_root;
1312                 canvas->pick_event.crossing.y_root = event->motion.y_root;
1313             } else {
1314                 canvas->pick_event.crossing.x_root = event->button.x_root;
1315                 canvas->pick_event.crossing.y_root = event->button.y_root;
1316             }
1317         } else {
1318             canvas->pick_event = *event;
1319         }
1320     }
1322     /* Don't do anything else if this is a recursive call */
1323     if (canvas->in_repick) return retval;
1325     /* LeaveNotify means that there is no current item, so we don't look for one */
1326     if (canvas->pick_event.type != GDK_LEAVE_NOTIFY) {
1327         /* these fields don't have the same offsets in both types of events */
1329         if (canvas->pick_event.type == GDK_ENTER_NOTIFY) {
1330             x = canvas->pick_event.crossing.x;
1331             y = canvas->pick_event.crossing.y;
1332         } else {
1333             x = canvas->pick_event.motion.x;
1334             y = canvas->pick_event.motion.y;
1335         }
1337         /* world coords */
1338         x += canvas->x0;
1339         y += canvas->y0;
1341         /* find the closest item */
1342         if (canvas->root->flags & SP_CANVAS_ITEM_VISIBLE) {
1343             sp_canvas_item_invoke_point (canvas->root, NR::Point(x, y), &canvas->new_current_item);
1344         } else {
1345             canvas->new_current_item = NULL;
1346         }
1347     } else {
1348         canvas->new_current_item = NULL;
1349     }
1351     if ((canvas->new_current_item == canvas->current_item) && !canvas->left_grabbed_item) {
1352         return retval; /* current item did not change */
1353     }
1355     /* Synthesize events for old and new current items */
1357     if ((canvas->new_current_item != canvas->current_item)
1358         && (canvas->current_item != NULL)
1359         && !canvas->left_grabbed_item) {
1360         GdkEvent new_event;
1361         SPCanvasItem *item;
1363         item = canvas->current_item;
1365         new_event = canvas->pick_event;
1366         new_event.type = GDK_LEAVE_NOTIFY;
1368         new_event.crossing.detail = GDK_NOTIFY_ANCESTOR;
1369         new_event.crossing.subwindow = NULL;
1370         canvas->in_repick = TRUE;
1371         retval = emit_event (canvas, &new_event);
1372         canvas->in_repick = FALSE;
1373     }
1375     if (canvas->gen_all_enter_events == false) {
1376         // new_current_item may have been set to NULL during the call to
1377         // emit_event() above
1378         if ((canvas->new_current_item != canvas->current_item) && button_down) {
1379             canvas->left_grabbed_item = TRUE;
1380             return retval;
1381         }
1382     }
1384     /* Handle the rest of cases */
1386     canvas->left_grabbed_item = FALSE;
1387     canvas->current_item = canvas->new_current_item;
1389     if (canvas->current_item != NULL) {
1390         GdkEvent new_event;
1392         new_event = canvas->pick_event;
1393         new_event.type = GDK_ENTER_NOTIFY;
1394         new_event.crossing.detail = GDK_NOTIFY_ANCESTOR;
1395         new_event.crossing.subwindow = NULL;
1396         retval = emit_event (canvas, &new_event);
1397     }
1399     return retval;
1402 /**
1403  * Button event handler for the canvas.
1404  */
1405 static gint
1406 sp_canvas_button (GtkWidget *widget, GdkEventButton *event)
1408     SPCanvas *canvas = SP_CANVAS (widget);
1410     int retval = FALSE;
1412     /* dispatch normally regardless of the event's window if an item has
1413        has a pointer grab in effect */
1414     if (!canvas->grabbed_item && 
1415         event->window != SP_CANVAS_WINDOW (canvas))
1416         return retval;
1418     int mask;
1419     switch (event->button) {
1420     case 1:
1421         mask = GDK_BUTTON1_MASK;
1422         break;
1423     case 2:
1424         mask = GDK_BUTTON2_MASK;
1425         break;
1426     case 3:
1427         mask = GDK_BUTTON3_MASK;
1428         break;
1429     case 4:
1430         mask = GDK_BUTTON4_MASK;
1431         break;
1432     case 5:
1433         mask = GDK_BUTTON5_MASK;
1434         break;
1435     default:
1436         mask = 0;
1437     }
1439     switch (event->type) {
1440     case GDK_BUTTON_PRESS:
1441     case GDK_2BUTTON_PRESS:
1442     case GDK_3BUTTON_PRESS:
1443         /* Pick the current item as if the button were not pressed, and
1444          * then process the event.
1445          */
1446         canvas->state = event->state;
1447         pick_current_item (canvas, (GdkEvent *) event);
1448         canvas->state ^= mask;
1449         retval = emit_event (canvas, (GdkEvent *) event);
1450         break;
1452     case GDK_BUTTON_RELEASE:
1453         /* Process the event as if the button were pressed, then repick
1454          * after the button has been released
1455          */
1456         canvas->state = event->state;
1457         retval = emit_event (canvas, (GdkEvent *) event);
1458         event->state ^= mask;
1459         canvas->state = event->state;
1460         pick_current_item (canvas, (GdkEvent *) event);
1461         event->state ^= mask;
1462         break;
1464     default:
1465         g_assert_not_reached ();
1466     }
1468     return retval;
1471 /**
1472  * Scroll event handler for the canvas.
1473  *
1474  * \todo FIXME: generate motion events to re-select items.
1475  */
1476 static gint
1477 sp_canvas_scroll (GtkWidget *widget, GdkEventScroll *event)
1479     return emit_event (SP_CANVAS (widget), (GdkEvent *) event);
1482 /**
1483  * Motion event handler for the canvas.
1484  */
1485 static int
1486 sp_canvas_motion (GtkWidget *widget, GdkEventMotion *event)
1488     SPCanvas *canvas = SP_CANVAS (widget);
1490     if (event->window != SP_CANVAS_WINDOW (canvas))
1491         return FALSE;
1493     if (canvas->grabbed_event_mask & GDK_POINTER_MOTION_HINT_MASK) {
1494         gint x, y;
1495         gdk_window_get_pointer (widget->window, &x, &y, NULL);
1496         event->x = x;
1497         event->y = y;
1498     }
1500     canvas->state = event->state;
1501     pick_current_item (canvas, (GdkEvent *) event);
1503     return emit_event (canvas, (GdkEvent *) event);
1506 static void
1507 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)
1509     GtkWidget *widget = GTK_WIDGET (canvas);
1511     SPCanvasBuf buf;
1512     if (canvas->rendermode != RENDERMODE_OUTLINE) {
1513         buf.buf = nr_pixelstore_256K_new (FALSE, 0);
1514     } else {
1515         buf.buf = nr_pixelstore_1M_new (FALSE, 0);
1516     }
1518     buf.buf_rowstride = sw * 3;
1519     buf.rect.x0 = x0;
1520     buf.rect.y0 = y0;
1521     buf.rect.x1 = x1;
1522     buf.rect.y1 = y1;
1523     buf.visible_rect.x0 = draw_x1;
1524     buf.visible_rect.y0 = draw_y1;
1525     buf.visible_rect.x1 = draw_x2;
1526     buf.visible_rect.y1 = draw_y2;
1527     GdkColor *color = &widget->style->bg[GTK_STATE_NORMAL];
1528     buf.bg_color = (((color->red & 0xff00) << 8)
1529                     | (color->green & 0xff00)
1530                     | (color->blue >> 8));
1531     buf.is_empty = true;
1532       
1533     if (canvas->root->flags & SP_CANVAS_ITEM_VISIBLE) {
1534         SP_CANVAS_ITEM_GET_CLASS (canvas->root)->render (canvas->root, &buf);
1535     }
1536       
1537     if (buf.is_empty) {
1538         gdk_rgb_gc_set_foreground (canvas->pixmap_gc, buf.bg_color);
1539         gdk_draw_rectangle (SP_CANVAS_WINDOW (canvas),
1540                             canvas->pixmap_gc,
1541                             TRUE,
1542                             x0 - canvas->x0, y0 - canvas->y0,
1543                             x1 - x0, y1 - y0);
1544     } else {
1545         gdk_draw_rgb_image_dithalign (SP_CANVAS_WINDOW (canvas),
1546                                       canvas->pixmap_gc,
1547                                       x0 - canvas->x0, y0 - canvas->y0,
1548                                       x1 - x0, y1 - y0,
1549                                       GDK_RGB_DITHER_MAX,
1550                                       buf.buf,
1551                                       sw * 3,
1552                                       x0 - canvas->x0, y0 - canvas->y0);
1553     }
1555     if (canvas->rendermode != RENDERMODE_OUTLINE) {
1556         nr_pixelstore_256K_free (buf.buf);
1557     } else {
1558         nr_pixelstore_1M_free (buf.buf);
1559     }
1562 /**
1563  * Helper that draws a specific rectangular part of the canvas.
1564  */
1565 static void
1566 sp_canvas_paint_rect (SPCanvas *canvas, int xx0, int yy0, int xx1, int yy1)
1568     g_return_if_fail (!canvas->need_update);
1569  
1570     // Monotonously increment the canvas-global counter on each paint. This will let us find out
1571     // when a new paint happened in event processing during this paint, so we can abort it.
1572     canvas->redraw_count++;
1574     NRRectL rect;
1575     rect.x0 = xx0;
1576     rect.x1 = xx1;
1577     rect.y0 = yy0;
1578     rect.y1 = yy1;
1580     if (canvas->redraw_aborted.x0 < canvas->redraw_aborted.x1 || canvas->redraw_aborted.y0 < canvas->redraw_aborted.y1) {
1581         // There was an aborted redraw last time, now we need to redraw BOTH it and the new rect.
1582         
1583         // OPTIMIZATION IDEA:
1584         //   if (area(rect) + area(redraw_aborted)) * 1.2 > area (union)
1585         //   then paint their union (as done below)
1586         //   else
1587         //   two_rects = true; paint new rect and aborted rect SEPARATELY without unioning.
1588         // Without this, when you scroll down and quickly up, the entire screen has to be redrawn,
1589         // because the union of the aborted strip at top and the newly exposed strip at bottom
1590         // covers the whole screen.
1591     
1592         // For now, just always do a union.
1593         rect.x0 = MIN(rect.x0, canvas->redraw_aborted.x0);
1594         rect.x1 = MAX(rect.x1, canvas->redraw_aborted.x1);
1595         rect.y0 = MIN(rect.y0, canvas->redraw_aborted.y0);
1596         rect.y1 = MAX(rect.y1, canvas->redraw_aborted.y1);
1597     }
1599     // Clip drawable rect by the visible area
1600     int draw_x1 = MAX (rect.x0, canvas->x0);
1601     int draw_y1 = MAX (rect.y0, canvas->y0);
1602     int draw_x2 = MIN (rect.x1, canvas->x0/*draw_x1*/ + GTK_WIDGET (canvas)->allocation.width);
1603     int draw_y2 = MIN (rect.y1, canvas->y0/*draw_y1*/ + GTK_WIDGET (canvas)->allocation.height);
1605     // Here we'll store the time it took to draw the slowest buffer of this paint. 
1606     glong slowest_buffer = 0;
1608     // Initially, the rect to redraw later (in case we're aborted) is the same as the one we're going to draw now.
1609     canvas->redraw_aborted.x0 = draw_x1;
1610     canvas->redraw_aborted.x1 = draw_x2;
1611     canvas->redraw_aborted.y0 = draw_y1;
1612     canvas->redraw_aborted.y1 = draw_y2;
1614     // Find the optimal buffer dimensions
1615     int bw = draw_x2 - draw_x1;
1616     int bh = draw_y2 - draw_y1;
1617     if ((bw < 1) || (bh < 1))
1618         return;
1619     int sw, sh;
1620     if (canvas->rendermode != RENDERMODE_OUTLINE) { // use 256K as a compromise to not slow down gradients
1621         /* 256K is the cached buffer and we need 3 channels */
1622         if (bw * bh <  87381) { // 256K/3
1623             // We can go with single buffer 
1624             sw = bw;
1625             sh = bh;
1626         } else if (bw <= (16 * 341)) {
1627             // Go with row buffer 
1628             sw = bw;
1629             sh =  87381 / bw;
1630         } else if (bh <= (16 * 256)) {
1631             // Go with column buffer 
1632             sw = 87381 / bh;
1633             sh = bh;
1634         } else {
1635             sw = 341;
1636             sh = 256;
1637         }
1638     } else {  // paths only, so 1M works faster
1639         /* 1M is the cached buffer and we need 3 channels */
1640         if (bw * bh <  349525) { // 1M/3
1641             // We can go with single buffer 
1642             sw = bw;
1643             sh = bh;
1644         } else if (bw <= (16 * 682)) {
1645             // Go with row buffer 
1646             sw = bw;
1647             sh =  349525 / bw;
1648         } else if (bh <= (16 * 512)) {
1649             // Go with column buffer 
1650             sw = 349525 / bh;
1651             sh = bh;
1652         } else {
1653             sw = 682;
1654             sh = 512;
1655         }
1656     }
1657     
1658     // Will this paint require more than one buffer?
1659     bool multiple_buffers = (((draw_y2 - draw_y1) > sh) || ((draw_x2 - draw_x1) > sw)); // or two_rects
1661     // remember the counter during this paint
1662     long this_count = canvas->redraw_count;
1664     // Time values to measure each buffer's paint time
1665     GTimeVal tstart, tfinish;
1667     // This is the main loop which corresponds to the visible left-to-right, top-to-bottom drawing
1668     // of screen blocks (buffers).
1669     for (int y0 = draw_y1; y0 < draw_y2; y0 += sh) {
1670         int y1 = MIN (y0 + sh, draw_y2);
1671         for (int x0 = draw_x1; x0 < draw_x2; x0 += sw) {
1672             int x1 = MIN (x0 + sw, draw_x2);
1674             // OPTIMIZATION IDEA: if drawing is really slow (as measured by canvas->slowest
1675             // buffer), process some events even BEFORE we do any buffers?
1676             
1677             // Paint one buffer; measure how long it takes.
1678             g_get_current_time (&tstart);
1679             sp_canvas_paint_single_buffer (canvas, x0, y0, x1, y1, draw_x1, draw_y1, draw_x2, draw_y2, sw);
1680             g_get_current_time (&tfinish);
1682             // Remember the slowest_buffer of this paint.
1683             glong this_buffer = (tfinish.tv_sec - tstart.tv_sec) * 1000000 + (tfinish.tv_usec - tstart.tv_usec);
1684             if (this_buffer > slowest_buffer) 
1685                 slowest_buffer = this_buffer;
1687             // After each successful buffer, reduce the rect remaining to redraw by what is already redrawn
1688             if (x1 >= draw_x2 && canvas->redraw_aborted.y0 < y1)
1689                 canvas->redraw_aborted.y0 = y1;
1690             if (y1 >= draw_y2 && canvas->redraw_aborted.x0 < x1)
1691                 canvas->redraw_aborted.x0 = x1;
1693             // INTERRUPTIBLE DISPLAY:
1694             // Process events that may have arrived while we were busy drawing;
1695             // only if we're drawing multiple buffers, and only if this one was not very fast,
1696             // and only if we're allowed to interrupt this redraw
1697             if (multiple_buffers && this_buffer > 25000 && !canvas->forced_full_redraw_count) {
1699                 // Run at most max_iterations of the main loop; we cannot process ALL events
1700                 // here because some things (e.g. rubberband) flood with dirtying events but will
1701                 // not redraw themselves
1702                 int max_iterations = 10;
1703                 int iterations = 0;
1704                 while (Gtk::Main::events_pending() && iterations++ < max_iterations) {
1705                     Gtk::Main::iteration(false);
1706                     // If one of the iterations has redrawn by itself, abort
1707                     if (this_count != canvas->redraw_count) {
1708                         canvas->slowest_buffer = slowest_buffer;
1709                         return;
1710                     }
1711                 }
1712    
1713                 // If not aborted so far, check if the events set redraw or update flags; 
1714                 // if so, force update and abort
1715                 if (canvas->need_redraw || canvas->need_update) {
1716                     canvas->slowest_buffer = slowest_buffer;
1717                     do_update (canvas);
1718                     return;
1719                 }
1720             }
1721         }
1722     }
1724     // We've finished a redraw; decrement the full redraw counter
1725     if (canvas->forced_full_redraw_count > 0) {
1726       canvas->forced_full_redraw_count--;
1727     }
1729     // Remember the slowest buffer of this paint in canvas
1730     canvas->slowest_buffer = slowest_buffer;
1733 /**
1734  * Force the next several screen redraws to not be interruptible.
1735  * Used when having an accurate representation of the drawing canvas
1736  * is more important than speed.
1737  *
1738  * In the future, this should be a toggle switch to be enabled/disabled
1739  * when precise object drawing is necessary.
1740  */
1741 void
1742 sp_canvas_force_full_redraws(SPCanvas *canvas, unsigned int count) {
1743   canvas->forced_full_redraw_count += count;
1746 /**
1747  * Flush all forced full redraw requests.
1748  * Used when accurate representation of the drawing canvas is no
1749  * longer needed.
1750  */
1751 void
1752 sp_canvas_clear_forced_full_redraws(SPCanvas *canvas) {
1753   canvas->forced_full_redraw_count = 0;
1756 /**
1757  * The canvas widget's expose callback.
1758  */
1759 static gint
1760 sp_canvas_expose (GtkWidget *widget, GdkEventExpose *event)
1762     SPCanvas *canvas = SP_CANVAS (widget);
1764     if (!GTK_WIDGET_DRAWABLE (widget) || 
1765         (event->window != SP_CANVAS_WINDOW (canvas)))
1766         return FALSE;
1768     int n_rects;
1769     GdkRectangle *rects;
1770     gdk_region_get_rectangles (event->region, &rects, &n_rects);
1772     for (int i = 0; i < n_rects; i++) {
1773         NRRectL rect;
1774                 
1775         rect.x0 = rects[i].x + canvas->x0;
1776         rect.y0 = rects[i].y + canvas->y0;
1777         rect.x1 = rect.x0 + rects[i].width;
1778         rect.y1 = rect.y0 + rects[i].height;
1780         sp_canvas_request_redraw (canvas, rect.x0, rect.y0, rect.x1, rect.y1);
1781     }
1783     if (n_rects > 0)
1784         g_free (rects);
1786     return FALSE;
1789 /**
1790  * The canvas widget's keypress callback.
1791  */
1792 static gint
1793 sp_canvas_key (GtkWidget *widget, GdkEventKey *event)
1795     return emit_event (SP_CANVAS (widget), (GdkEvent *) event);
1798 /**
1799  * Crossing event handler for the canvas.
1800  */
1801 static gint
1802 sp_canvas_crossing (GtkWidget *widget, GdkEventCrossing *event)
1804     SPCanvas *canvas = SP_CANVAS (widget);
1806     if (event->window != SP_CANVAS_WINDOW (canvas))
1807         return FALSE;
1809     canvas->state = event->state;
1810     return pick_current_item (canvas, (GdkEvent *) event);
1813 /**
1814  * Focus in handler for the canvas.
1815  */
1816 static gint
1817 sp_canvas_focus_in (GtkWidget *widget, GdkEventFocus *event)
1819     GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
1821     SPCanvas *canvas = SP_CANVAS (widget);
1823     if (canvas->focused_item) {
1824         return emit_event (canvas, (GdkEvent *) event);
1825     } else {
1826         return FALSE;
1827     }
1830 /**
1831  * Focus out handler for the canvas.
1832  */
1833 static gint
1834 sp_canvas_focus_out (GtkWidget *widget, GdkEventFocus *event)
1836     GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
1838     SPCanvas *canvas = SP_CANVAS (widget);
1840     if (canvas->focused_item)
1841         return emit_event (canvas, (GdkEvent *) event);
1842     else
1843         return FALSE;
1846 /**
1847  * Helper that repaints the areas in the canvas that need it.
1848  */
1849 static int
1850 paint (SPCanvas *canvas)
1852     if (canvas->need_update) {
1853         sp_canvas_item_invoke_update (canvas->root, NR::identity(), 0);
1854         canvas->need_update = FALSE;
1855     }
1857     if (!canvas->need_redraw)
1858         return TRUE;
1860     GtkWidget const *widget = GTK_WIDGET(canvas);
1861     int const canvas_x1 = canvas->x0 + widget->allocation.width;
1862     int const canvas_y1 = canvas->y0 + widget->allocation.height;
1864     NRRectL topaint;
1865     topaint.x0 = topaint.y0 = topaint.x1 = topaint.y1 = 0;
1867     for (int j=canvas->tTop&(~3);j<canvas->tBottom;j+=4) {
1868         for (int i=canvas->tLeft&(~3);i<canvas->tRight;i+=4) {
1869             int  mode=0;
1870       
1871             int pl=i+1,pr=i,pt=j+4,pb=j;
1872             for (int l=MAX(j,canvas->tTop);l<MIN(j+4,canvas->tBottom);l++) {
1873                 for (int k=MAX(i,canvas->tLeft);k<MIN(i+4,canvas->tRight);k++) {
1874                     if ( canvas->tiles[(k-canvas->tLeft)+(l-canvas->tTop)*canvas->tileH] ) {
1875                         mode|=1<<((k-i)+(l-j)*4);
1876                         if ( k < pl ) pl=k;
1877                         if ( k+1 > pr ) pr=k+1;
1878                         if ( l < pt ) pt=l;
1879                         if ( l+1 > pb ) pb=l+1;
1880                     }
1881                     canvas->tiles[(k-canvas->tLeft)+(l-canvas->tTop)*canvas->tileH]=0;
1882                 }
1883             }
1884       
1885             if ( mode ) {
1886                 NRRectL tile;
1887                 tile.x0 = MAX (pl*32, canvas->x0);
1888                 tile.y0 = MAX (pt*32, canvas->y0);
1889                 tile.x1 = MIN (pr*32, canvas_x1);
1890                 tile.y1 = MIN (pb*32, canvas_y1);
1891                 if ((tile.x0 < tile.x1) && (tile.y0 < tile.y1)) {
1892                     nr_rect_l_union (&topaint, &topaint, &tile);
1893                 }
1895             }
1896         }
1897     }
1899     canvas->need_redraw = FALSE;
1900     sp_canvas_paint_rect (canvas, topaint.x0, topaint.y0, topaint.x1, topaint.y1);
1902     return TRUE;
1905 /**
1906  * Helper that invokes update, paint, and repick on canvas.
1907  */
1908 static int
1909 do_update (SPCanvas *canvas)
1911     if (!canvas->root) // canvas may have already be destroyed by closing desktop durring interrupted display!
1912         return TRUE;
1914     /* Cause the update if necessary */
1915     if (canvas->need_update) {
1916         sp_canvas_item_invoke_update (canvas->root, NR::identity(), 0);
1917         canvas->need_update = FALSE;
1918     }
1920     /* Paint if able to */
1921     if (GTK_WIDGET_DRAWABLE (canvas)) {
1922             return paint (canvas);
1923     }
1925     /* Pick new current item */
1926     while (canvas->need_repick) {
1927         canvas->need_repick = FALSE;
1928         pick_current_item (canvas, &canvas->pick_event);
1929     }
1931     return TRUE;
1934 /**
1935  * Idle handler for the canvas that deals with pending updates and redraws.
1936  */
1937 static gint
1938 idle_handler (gpointer data)
1940     GDK_THREADS_ENTER ();
1942     SPCanvas *canvas = SP_CANVAS (data);
1944     const int ret = do_update (canvas);
1946     if (ret) {
1947         /* Reset idle id */
1948         canvas->idle_id = 0;
1949     }
1951     GDK_THREADS_LEAVE ();
1953     return !ret;
1956 /**
1957  * Convenience function to add an idle handler to a canvas.
1958  */
1959 static void
1960 add_idle (SPCanvas *canvas)
1962     if (canvas->idle_id != 0)
1963         return;
1965     canvas->idle_id = gtk_idle_add_priority (sp_canvas_update_priority, idle_handler, canvas);
1968 /**
1969  * Returns the root group of the specified canvas.
1970  */
1971 SPCanvasGroup *
1972 sp_canvas_root (SPCanvas *canvas)
1974     g_return_val_if_fail (canvas != NULL, NULL);
1975     g_return_val_if_fail (SP_IS_CANVAS (canvas), NULL);
1977     return SP_CANVAS_GROUP (canvas->root);
1980 /**
1981  * Scrolls canvas to specific position.
1982  */
1983 void
1984 sp_canvas_scroll_to (SPCanvas *canvas, double cx, double cy, unsigned int clear)
1986     g_return_if_fail (canvas != NULL);
1987     g_return_if_fail (SP_IS_CANVAS (canvas));
1989     int ix = (int) (cx + 0.5);
1990     int iy = (int) (cy + 0.5);
1991     int dx = ix - canvas->x0;
1992     int dy = iy - canvas->y0;
1994     canvas->dx0 = cx;
1995     canvas->dy0 = cy;
1996     canvas->x0 = ix;
1997     canvas->y0 = iy;
1999     sp_canvas_resize_tiles(canvas,canvas->x0,canvas->y0,canvas->x0+canvas->widget.allocation.width,canvas->y0+canvas->widget.allocation.height);
2001     if (!clear) {
2002         // scrolling without zoom; redraw only the newly exposed areas
2003         if ((dx != 0) || (dy != 0)) {
2004             int width, height;
2005             width = canvas->widget.allocation.width;
2006             height = canvas->widget.allocation.height;
2007             if (GTK_WIDGET_REALIZED (canvas)) {
2008                 gdk_window_scroll (SP_CANVAS_WINDOW (canvas), -dx, -dy);
2009                 gdk_window_process_updates (SP_CANVAS_WINDOW (canvas), TRUE);
2010             }
2011             if (dx < 0) {
2012                 sp_canvas_request_redraw (canvas, ix + 0, iy + 0, ix - dx, iy + height);
2013             } else if (dx > 0) {
2014                 sp_canvas_request_redraw (canvas, ix + width - dx, iy + 0, ix + width, iy + height);
2015             }
2016             if (dy < 0) {
2017                 sp_canvas_request_redraw (canvas, ix + 0, iy + 0, ix + width, iy - dy);
2018             } else if (dy > 0) {
2019                 sp_canvas_request_redraw (canvas, ix + 0, iy + height - dy, ix + width, iy + height);
2020             }
2021         }
2022     } else {
2023         // scrolling as part of zoom; do nothing here - the next do_update will perform full redraw
2024     }
2027 /** 
2028  * Updates canvas if necessary.
2029  */
2030 void
2031 sp_canvas_update_now (SPCanvas *canvas)
2033     g_return_if_fail (canvas != NULL);
2034     g_return_if_fail (SP_IS_CANVAS (canvas));
2036     if (!(canvas->need_update ||
2037           canvas->need_redraw))
2038         return;
2040     remove_idle (canvas);
2041     do_update (canvas);
2044 /**
2045  * Update callback for canvas widget.
2046  */
2047 static void
2048 sp_canvas_request_update (SPCanvas *canvas)
2050     canvas->need_update = TRUE;
2051     add_idle (canvas);
2054 /**
2055  * Forces redraw of rectangular canvas area.
2056  */
2057 void
2058 sp_canvas_request_redraw (SPCanvas *canvas, int x0, int y0, int x1, int y1)
2060     NRRectL bbox;
2061     NRRectL visible;
2062     NRRectL clip;
2064     g_return_if_fail (canvas != NULL);
2065     g_return_if_fail (SP_IS_CANVAS (canvas));
2067     if (!GTK_WIDGET_DRAWABLE (canvas)) return;
2068     if ((x0 >= x1) || (y0 >= y1)) return;
2070     bbox.x0 = x0;
2071     bbox.y0 = y0;
2072     bbox.x1 = x1;
2073     bbox.y1 = y1;
2075     visible.x0 = canvas->x0;
2076     visible.y0 = canvas->y0;
2077     visible.x1 = visible.x0 + GTK_WIDGET (canvas)->allocation.width;
2078     visible.y1 = visible.y0 + GTK_WIDGET (canvas)->allocation.height;
2080     nr_rect_l_intersect (&clip, &bbox, &visible);
2082     sp_canvas_dirty_rect(canvas, clip.x0, clip.y0, clip.x1, clip.y1);
2083     add_idle (canvas);
2086 /**
2087  * Sets world coordinates from win and canvas.
2088  */
2089 void sp_canvas_window_to_world(SPCanvas const *canvas, double winx, double winy, double *worldx, double *worldy)
2091     g_return_if_fail (canvas != NULL);
2092     g_return_if_fail (SP_IS_CANVAS (canvas));
2094     if (worldx) *worldx = canvas->x0 + winx;
2095     if (worldy) *worldy = canvas->y0 + winy;
2098 /**
2099  * Sets win coordinates from world and canvas.
2100  */
2101 void sp_canvas_world_to_window(SPCanvas const *canvas, double worldx, double worldy, double *winx, double *winy)
2103     g_return_if_fail (canvas != NULL);
2104     g_return_if_fail (SP_IS_CANVAS (canvas));
2106     if (winx) *winx = worldx - canvas->x0;
2107     if (winy) *winy = worldy - canvas->y0;
2110 /**
2111  * Converts point from win to world coordinates.
2112  */
2113 NR::Point sp_canvas_window_to_world(SPCanvas const *canvas, NR::Point const win)
2115     g_assert (canvas != NULL);
2116     g_assert (SP_IS_CANVAS (canvas));
2118     return NR::Point(canvas->x0 + win[0], canvas->y0 + win[1]);
2121 /**
2122  * Converts point from world to win coordinates.
2123  */
2124 NR::Point sp_canvas_world_to_window(SPCanvas const *canvas, NR::Point const world)
2126     g_assert (canvas != NULL);
2127     g_assert (SP_IS_CANVAS (canvas));
2129     return NR::Point(world[0] - canvas->x0, world[1] - canvas->y0);
2132 /**
2133  * Returns true if point given in world coordinates is inside window.
2134  */
2135 bool sp_canvas_world_pt_inside_window(SPCanvas const *canvas, NR::Point const &world)
2137     g_assert( canvas != NULL );
2138     g_assert(SP_IS_CANVAS(canvas));
2140     using NR::X;
2141     using NR::Y;
2142     GtkWidget const &w = *GTK_WIDGET(canvas);
2143     return ( ( canvas->x0 <= world[X] )  &&
2144              ( canvas->y0 <= world[Y] )  &&
2145              ( world[X] < canvas->x0 + w.allocation.width )  &&
2146              ( world[Y] < canvas->y0 + w.allocation.height ) );
2149 /**
2150  * Return canvas window coordinates as NRRect.
2151  */
2152 NR::Rect SPCanvas::getViewbox() const
2154     GtkWidget const *w = GTK_WIDGET(this);
2156     return NR::Rect(NR::Point(dx0, dy0),
2157                     NR::Point(dx0 + w->allocation.width, dy0 + w->allocation.height));
2160 inline int sp_canvas_tile_floor(int x)
2162     return (x&(~31))/32;
2165 inline int sp_canvas_tile_ceil(int x)
2167     return ((x+31)&(~31))/32;
2170 /**
2171  * Helper that changes tile size for canvas redraw.
2172  */
2173 void sp_canvas_resize_tiles(SPCanvas* canvas,int nl,int nt,int nr,int nb)
2175     if ( nl >= nr || nt >= nb ) {
2176         if ( canvas->tiles ) g_free(canvas->tiles);
2177         canvas->tLeft=canvas->tTop=canvas->tRight=canvas->tBottom=0;
2178         canvas->tileH=canvas->tileV=0;
2179         canvas->tiles=NULL;
2180         return;
2181     }
2182     int tl=sp_canvas_tile_floor(nl);
2183     int tt=sp_canvas_tile_floor(nt);
2184     int tr=sp_canvas_tile_ceil(nr);
2185     int tb=sp_canvas_tile_ceil(nb);
2187     int nh=tr-tl,nv=tb-tt;
2188     uint8_t* ntiles=(uint8_t*)g_malloc(nh*nv*sizeof(uint8_t));
2189     for (int i=tl;i<tr;i++) {
2190         for (int j=tt;j<tb;j++) {
2191             int ind=(i-tl)+(j-tt)*nh;
2192             if ( i >= canvas->tLeft && i < canvas->tRight && j >= canvas->tTop && j < canvas->tBottom ) {
2193                 ntiles[ind]=canvas->tiles[(i-canvas->tLeft)+(j-canvas->tTop)*canvas->tileH];
2194             } else {
2195                 ntiles[ind]=0;
2196             }
2197         }
2198     }
2199     if ( canvas->tiles ) g_free(canvas->tiles);
2200     canvas->tiles=ntiles;
2201     canvas->tLeft=tl;
2202     canvas->tTop=tt;
2203     canvas->tRight=tr;
2204     canvas->tBottom=tb;
2205     canvas->tileH=nh;
2206     canvas->tileV=nv;
2209 /**
2210  * Helper that marks specific canvas rectangle for redraw.
2211  */
2212 void sp_canvas_dirty_rect(SPCanvas* canvas,int nl,int nt,int nr,int nb)
2214     if ( nl >= nr || nt >= nb ) {
2215         return;
2216     }
2217     int tl=sp_canvas_tile_floor(nl);
2218     int tt=sp_canvas_tile_floor(nt);
2219     int tr=sp_canvas_tile_ceil(nr);
2220     int tb=sp_canvas_tile_ceil(nb);
2221     if ( tl >= canvas->tRight || tr <= canvas->tLeft || tt >= canvas->tBottom || tb <= canvas->tTop ) return;
2222     if ( tl < canvas->tLeft ) tl=canvas->tLeft;
2223     if ( tr > canvas->tRight ) tr=canvas->tRight;
2224     if ( tt < canvas->tTop ) tt=canvas->tTop;
2225     if ( tb > canvas->tBottom ) tb=canvas->tBottom;
2227     canvas->need_redraw = TRUE;
2229     for (int i=tl;i<tr;i++) {
2230         for (int j=tt;j<tb;j++) {
2231             canvas->tiles[(i-canvas->tLeft)+(j-canvas->tTop)*canvas->tileH]=1;
2232         }
2233     }
2237 /*
2238   Local Variables:
2239   mode:c++
2240   c-file-style:"stroustrup"
2241   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2242   indent-tabs-mode:nil
2243   fill-column:99
2244   End:
2245 */
2246 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :