Code

* src/display/sp-canvas.cpp, display/sp-canvas.h, connector-context.cpp:
[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  *
12  * Copyright (C) 1998 The Free Software Foundation
13  * Copyright (C) 2002 Lauris Kaplinski
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
22 #include <libnr/nr-pixblock.h>
24 #include <gtk/gtkmain.h>
25 #include <gtk/gtksignal.h>
27 #include <helper/sp-marshal.h>
28 #include <display/sp-canvas.h>
29 #include "display-forward.h"
30 #include <libnr/nr-matrix-fns.h>
31 #include <libnr/nr-matrix-ops.h>
32 #include <libnr/nr-convex-hull.h>
34 enum {
35         RENDERMODE_NORMAL,
36         RENDERMODE_NOAA,
37         RENDERMODE_OUTLINE
38 };
40 const gint sp_canvas_update_priority = G_PRIORITY_HIGH_IDLE;
42 #define SP_CANVAS_WINDOW(c) (((GtkWidget *) (c))->window)
44 enum {
45     SP_CANVAS_ITEM_VISIBLE = 1 << 7,
46     SP_CANVAS_ITEM_NEED_UPDATE = 1 << 8,
47     SP_CANVAS_ITEM_NEED_AFFINE = 1 << 9
48 };
50 /**
51  * A group of Items.
52  */
53 struct SPCanvasGroup {
54     SPCanvasItem item;
56     GList *items, *last;
57 };
59 /**
60  * The SPCanvasGroup vtable.
61  */
62 struct SPCanvasGroupClass {
63     SPCanvasItemClass parent_class;
64 };
66 /**
67  * The SPCanvas vtable.
68  */
69 struct SPCanvasClass {
70     GtkWidgetClass parent_class;
71 };
73 static void group_add (SPCanvasGroup *group, SPCanvasItem *item);
74 static void group_remove (SPCanvasGroup *group, SPCanvasItem *item);
76 /* SPCanvasItem */
78 enum {ITEM_EVENT, ITEM_LAST_SIGNAL};
81 static void sp_canvas_request_update (SPCanvas *canvas);
83 static void sp_canvas_item_class_init (SPCanvasItemClass *klass);
84 static void sp_canvas_item_init (SPCanvasItem *item);
85 static void sp_canvas_item_dispose (GObject *object);
86 static void sp_canvas_item_construct (SPCanvasItem *item, SPCanvasGroup *parent, const gchar *first_arg_name, va_list args);
88 static int emit_event (SPCanvas *canvas, GdkEvent *event);
90 static guint item_signals[ITEM_LAST_SIGNAL] = { 0 };
92 static GtkObjectClass *item_parent_class;
94 /**
95  * Registers the SPCanvasItem class with Glib and returns its type number.
96  */
97 GType
98 sp_canvas_item_get_type (void)
99 {
100     static GType type = 0;
101     if (!type) {
102         static const GTypeInfo info = {
103             sizeof (SPCanvasItemClass),
104             NULL, NULL,
105             (GClassInitFunc) sp_canvas_item_class_init,
106             NULL, NULL,
107             sizeof (SPCanvasItem),
108             0,
109             (GInstanceInitFunc) sp_canvas_item_init,
110             NULL
111         };
112         type = g_type_register_static (GTK_TYPE_OBJECT, "SPCanvasItem", &info, (GTypeFlags)0);
113     }
115     return type;
118 /**
119  * Initializes the SPCanvasItem vtable and the "event" signal.
120  */
121 static void
122 sp_canvas_item_class_init (SPCanvasItemClass *klass)
124     GObjectClass *object_class = (GObjectClass *) klass;
126     /* fixme: Derive from GObject */
127     item_parent_class = (GtkObjectClass*)gtk_type_class (GTK_TYPE_OBJECT);
129     item_signals[ITEM_EVENT] = g_signal_new ("event",
130                                              G_TYPE_FROM_CLASS (klass),
131                                              G_SIGNAL_RUN_LAST,
132                                              G_STRUCT_OFFSET (SPCanvasItemClass, event),
133                                              NULL, NULL,
134                                              sp_marshal_BOOLEAN__POINTER,
135                                              G_TYPE_BOOLEAN, 1,
136                                              GDK_TYPE_EVENT);
138     object_class->dispose = sp_canvas_item_dispose;
141 /**
142  * Callback for initialization of SPCanvasItem.
143  */
144 static void
145 sp_canvas_item_init (SPCanvasItem *item)
147     item->flags |= SP_CANVAS_ITEM_VISIBLE;
148     item->xform = NR::Matrix(NR::identity());
151 /**
152  * Constructs new SPCanvasItem on SPCanvasGroup.
153  */
154 SPCanvasItem *
155 sp_canvas_item_new (SPCanvasGroup *parent, GtkType type, const gchar *first_arg_name, ...)
157     va_list args;
159     g_return_val_if_fail (parent != NULL, NULL);
160     g_return_val_if_fail (SP_IS_CANVAS_GROUP (parent), NULL);
161     g_return_val_if_fail (gtk_type_is_a (type, sp_canvas_item_get_type ()), NULL);
163     SPCanvasItem *item = SP_CANVAS_ITEM (gtk_type_new (type));
165     va_start (args, first_arg_name);
166     sp_canvas_item_construct (item, parent, first_arg_name, args);
167     va_end (args);
169     return item;
172 /**
173  * Sets up the newly created SPCanvasItem.
174  *
175  * We make it static for encapsulation reasons since it was nowhere used.
176  */
177 static void
178 sp_canvas_item_construct (SPCanvasItem *item, SPCanvasGroup *parent, const gchar *first_arg_name, va_list args)
180     g_return_if_fail (SP_IS_CANVAS_GROUP (parent));
181     g_return_if_fail (SP_IS_CANVAS_ITEM (item));
183     item->parent = SP_CANVAS_ITEM (parent);
184     item->canvas = item->parent->canvas;
186     g_object_set_valist (G_OBJECT (item), first_arg_name, args);
188     group_add (SP_CANVAS_GROUP (item->parent), item);
190     sp_canvas_item_request_update (item);
191     sp_canvas_request_redraw (item->canvas, (int)(item->x1), (int)(item->y1), (int)(item->x2 + 1), (int)(item->y2 + 1));
192     item->canvas->need_repick = TRUE;
195 /**
196  * Helper function that requests redraw only if item's visible flag is set.
197  */
198 static void
199 redraw_if_visible (SPCanvasItem *item)
201     if (item->flags & SP_CANVAS_ITEM_VISIBLE) {
202         sp_canvas_request_redraw (item->canvas, (int)(item->x1), (int)(item->y1), (int)(item->x2 + 1), (int)(item->y2 + 1));
203     }
206 /**
207  * Callback that removes item from all referers and destroys it.
208  */
209 static void
210 sp_canvas_item_dispose (GObject *object)
212     SPCanvasItem *item = SP_CANVAS_ITEM (object);
214     redraw_if_visible (item);
215     item->flags &= ~SP_CANVAS_ITEM_VISIBLE;
217     if (item == item->canvas->current_item) {
218         item->canvas->current_item = NULL;
219         item->canvas->need_repick = TRUE;
220     }
222     if (item == item->canvas->new_current_item) {
223         item->canvas->new_current_item = NULL;
224         item->canvas->need_repick = TRUE;
225     }
227     if (item == item->canvas->grabbed_item) {
228         item->canvas->grabbed_item = NULL;
229         gdk_pointer_ungrab (GDK_CURRENT_TIME);
230     }
232     if (item == item->canvas->focused_item)
233         item->canvas->focused_item = NULL;
235     if (item->parent) {
236         group_remove (SP_CANVAS_GROUP (item->parent), item);
237     }
239     G_OBJECT_CLASS (item_parent_class)->dispose (object);
242 /**
243  * Helper function to update item and its children.
244  *
245  * NB! affine is parent2canvas.
246  */
247 static void
248 sp_canvas_item_invoke_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
250     /* Apply the child item's transform */
251     NR::Matrix child_affine = item->xform * affine;
253     /* apply object flags to child flags */
254     int child_flags = flags & ~SP_CANVAS_UPDATE_REQUESTED;
256     if (item->flags & SP_CANVAS_ITEM_NEED_UPDATE)
257         child_flags |= SP_CANVAS_UPDATE_REQUESTED;
259     if (item->flags & SP_CANVAS_ITEM_NEED_AFFINE)
260         child_flags |= SP_CANVAS_UPDATE_AFFINE;
262     if (child_flags & (SP_CANVAS_UPDATE_REQUESTED | SP_CANVAS_UPDATE_AFFINE)) {
263         if (SP_CANVAS_ITEM_GET_CLASS (item)->update)
264             SP_CANVAS_ITEM_GET_CLASS (item)->update (item, child_affine, child_flags);
265     }
267     GTK_OBJECT_UNSET_FLAGS (item, SP_CANVAS_ITEM_NEED_UPDATE);
268     GTK_OBJECT_UNSET_FLAGS (item, SP_CANVAS_ITEM_NEED_AFFINE);
271 /** 
272  * Helper function to invoke the point method of the item.  
273  *
274  * The argument x, y should be in the parent's item-relative coordinate 
275  * system.  This routine applies the inverse of the item's transform, 
276  * maintaining the affine invariant.
277  */
278 static double
279 sp_canvas_item_invoke_point (SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item)
281     if (SP_CANVAS_ITEM_GET_CLASS (item)->point)
282         return SP_CANVAS_ITEM_GET_CLASS (item)->point (item, p, actual_item);
284     return NR_HUGE;
287 /**
288  * Makes the item's affine transformation matrix be equal to the specified
289  * matrix.
290  * 
291  * @item: A canvas item.
292  * @affine: An affine transformation matrix.
293  */
294 void
295 sp_canvas_item_affine_absolute (SPCanvasItem *item, NR::Matrix const& affine)
297     item->xform = affine;
299     if (!(item->flags & SP_CANVAS_ITEM_NEED_AFFINE)) {
300         item->flags |= SP_CANVAS_ITEM_NEED_AFFINE;
301         if (item->parent != NULL) {
302             sp_canvas_item_request_update (item->parent);
303         } else {
304             sp_canvas_request_update (item->canvas);
305         }
306     }
308     item->canvas->need_repick = TRUE;
311 /**
312  * Convenience function to reorder items in a group's child list.  
313  *
314  * This puts the specified link after the "before" link.
315  */
316 static void
317 put_item_after (GList *link, GList *before)
319     if (link == before)
320         return;
322     SPCanvasGroup *parent = SP_CANVAS_GROUP (SP_CANVAS_ITEM (link->data)->parent);
324     if (before == NULL) {
325         if (link == parent->items) return;
327         link->prev->next = link->next;
329         if (link->next) {
330             link->next->prev = link->prev;
331         } else {
332             parent->last = link->prev;
333         }
335         link->prev = before;
336         link->next = parent->items;
337         link->next->prev = link;
338         parent->items = link;
339     } else {
340         if ((link == parent->last) && (before == parent->last->prev))
341             return;
343         if (link->next)
344             link->next->prev = link->prev;
346         if (link->prev)
347             link->prev->next = link->next;
348         else {
349             parent->items = link->next;
350             parent->items->prev = NULL;
351         }
353         link->prev = before;
354         link->next = before->next;
356         link->prev->next = link;
358         if (link->next)
359             link->next->prev = link;
360         else
361             parent->last = link;
362     }
366 /**
367  * Raises the item in its parent's stack by the specified number of positions.
368  * 
369  * \param item A canvas item.
370  * \param positions Number of steps to raise the item.
371  *
372  * If the number of positions is greater than the distance to the top of the
373  * stack, then the item is put at the top.
374  */
375 void
376 sp_canvas_item_raise (SPCanvasItem *item, int positions)
378     g_return_if_fail (item != NULL);
379     g_return_if_fail (SP_IS_CANVAS_ITEM (item));
380     g_return_if_fail (positions >= 0);
382     if (!item->parent || positions == 0)
383         return;
385     SPCanvasGroup *parent = SP_CANVAS_GROUP (item->parent);
386     GList *link = g_list_find (parent->items, item);
387     g_assert (link != NULL);
389     GList *before;
390     for (before = link; positions && before; positions--)
391         before = before->next;
393     if (!before)
394         before = parent->last;
396     put_item_after (link, before);
398     redraw_if_visible (item);
399     item->canvas->need_repick = TRUE;
403 /**
404  * Lowers the item in its parent's stack by the specified number of positions.
405  *
406  * \param item A canvas item.
407  * \param positions Number of steps to lower the item.
408  *
409  * If the number of positions is greater than the distance to the bottom of the
410  * stack, then the item is put at the bottom.
411  **/
412 void
413 sp_canvas_item_lower (SPCanvasItem *item, int positions)
415     g_return_if_fail (item != NULL);
416     g_return_if_fail (SP_IS_CANVAS_ITEM (item));
417     g_return_if_fail (positions >= 1);
419     if (!item->parent || positions == 0)
420         return;
422     SPCanvasGroup *parent = SP_CANVAS_GROUP (item->parent);
423     GList *link = g_list_find (parent->items, item);
424     g_assert (link != NULL);
426     GList *before;
427     if (link->prev)
428         for (before = link->prev; positions && before; positions--)
429             before = before->prev;
430     else
431         before = NULL;
433     put_item_after (link, before);
435     redraw_if_visible (item);
436     item->canvas->need_repick = TRUE;
439 /**
440  * Sets visible flag on item and requests a redraw.
441  */
442 void
443 sp_canvas_item_show (SPCanvasItem *item)
445     g_return_if_fail (item != NULL);
446     g_return_if_fail (SP_IS_CANVAS_ITEM (item));
448     if (item->flags & SP_CANVAS_ITEM_VISIBLE)
449         return;
451     item->flags |= SP_CANVAS_ITEM_VISIBLE;
453     sp_canvas_request_redraw (item->canvas, (int)(item->x1), (int)(item->y1), (int)(item->x2 + 1), (int)(item->y2 + 1));
454     item->canvas->need_repick = TRUE;
457 /**
458  * Clears visible flag on item and requests a redraw.
459  */
460 void
461 sp_canvas_item_hide (SPCanvasItem *item)
463     g_return_if_fail (item != NULL);
464     g_return_if_fail (SP_IS_CANVAS_ITEM (item));
466     if (!(item->flags & SP_CANVAS_ITEM_VISIBLE))
467         return;
469     item->flags &= ~SP_CANVAS_ITEM_VISIBLE;
471     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)(item->x2 + 1), (int)(item->y2 + 1));
472     item->canvas->need_repick = TRUE;
475 /**
476  * Grab item under cursor.
477  * 
478  * \pre !canvas->grabbed_item && item->flags & SP_CANVAS_ITEM_VISIBLE
479  */
480 int
481 sp_canvas_item_grab (SPCanvasItem *item, guint event_mask, GdkCursor *cursor, guint32 etime)
483     g_return_val_if_fail (item != NULL, -1);
484     g_return_val_if_fail (SP_IS_CANVAS_ITEM (item), -1);
485     g_return_val_if_fail (GTK_WIDGET_MAPPED (item->canvas), -1);
487     if (item->canvas->grabbed_item)
488         return -1;
490     if (!(item->flags & SP_CANVAS_ITEM_VISIBLE))
491         return -1;
493     /* fixme: Top hack (Lauris) */
494     /* fixme: If we add key masks to event mask, Gdk will abort (Lauris) */
495     /* fixme: But Canvas actualle does get key events, so all we need is routing these here */
496     gdk_pointer_grab (SP_CANVAS_WINDOW (item->canvas), FALSE,
497                       (GdkEventMask)(event_mask & (~(GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK))),
498                       NULL, cursor, etime);
500     item->canvas->grabbed_item = item;
501     item->canvas->grabbed_event_mask = event_mask;
502     item->canvas->current_item = item; /* So that events go to the grabbed item */
504     return 0;
507 /**
508  * Ungrabs the item, which must have been grabbed in the canvas, and ungrabs the
509  * mouse.
510  * 
511  * \param item A canvas item that holds a grab.
512  * \param etime The timestamp for ungrabbing the mouse.
513  */
514 void
515 sp_canvas_item_ungrab (SPCanvasItem *item, guint32 etime)
517     g_return_if_fail (item != NULL);
518     g_return_if_fail (SP_IS_CANVAS_ITEM (item));
520     if (item->canvas->grabbed_item != item)
521         return;
523     item->canvas->grabbed_item = NULL;
525     gdk_pointer_ungrab (etime);
528 /**
529  * Returns the product of all transformation matrices from the root item down
530  * to the item.
531  */
532 NR::Matrix sp_canvas_item_i2w_affine(SPCanvasItem const *item)
534     g_assert (SP_IS_CANVAS_ITEM (item)); // should we get this?
536     NR::Matrix affine = NR::identity();
538     while (item) {
539         affine *= item->xform;
540         item = item->parent;
541     }
542     return affine;
545 /**
546  * Helper that returns true iff item is descendant of parent.
547  */
548 static bool is_descendant(SPCanvasItem const *item, SPCanvasItem const *parent)
550     while (item) {
551         if (item == parent)
552             return true;
553         item = item->parent;
554     }
556     return false;
559 /**
560  * Focus canvas, and item under cursor if it is not already focussed.
561  */
562 void
563 sp_canvas_item_grab_focus (SPCanvasItem *item)
565     g_return_if_fail (item != NULL);
566     g_return_if_fail (SP_IS_CANVAS_ITEM (item));
567     g_return_if_fail (GTK_WIDGET_CAN_FOCUS (GTK_WIDGET (item->canvas)));
569     SPCanvasItem *focused_item = item->canvas->focused_item;
571     if (focused_item) {
572         GdkEvent ev;
573         ev.focus_change.type = GDK_FOCUS_CHANGE;
574         ev.focus_change.window = SP_CANVAS_WINDOW (item->canvas);
575         ev.focus_change.send_event = FALSE;
576         ev.focus_change.in = FALSE;
578         emit_event (item->canvas, &ev);
579     }
581     item->canvas->focused_item = item;
582     gtk_widget_grab_focus (GTK_WIDGET (item->canvas));
584     if (focused_item) {
585         GdkEvent ev;
586         ev.focus_change.type = GDK_FOCUS_CHANGE;
587         ev.focus_change.window = SP_CANVAS_WINDOW (item->canvas);
588         ev.focus_change.send_event = FALSE;
589         ev.focus_change.in = TRUE;
591         emit_event (item->canvas, &ev);
592     }
595 /**
596  * Requests that the canvas queue an update for the specified item.
597  * 
598  * To be used only by item implementations.
599  */
600 void
601 sp_canvas_item_request_update (SPCanvasItem *item)
603     if (item->flags & SP_CANVAS_ITEM_NEED_UPDATE)
604         return;
606     item->flags |= SP_CANVAS_ITEM_NEED_UPDATE;
608     if (item->parent != NULL) {
609         /* Recurse up the tree */
610         sp_canvas_item_request_update (item->parent);
611     } else {
612         /* Have reached the top of the tree, make sure the update call gets scheduled. */
613         sp_canvas_request_update (item->canvas);
614     }
617 /**
618  * Returns position of item in group.
619  */
620 gint sp_canvas_item_order (SPCanvasItem * item)
622     return g_list_index (SP_CANVAS_GROUP (item->parent)->items, item);
625 /* SPCanvasGroup */
627 static void sp_canvas_group_class_init (SPCanvasGroupClass *klass);
628 static void sp_canvas_group_init (SPCanvasGroup *group);
629 static void sp_canvas_group_destroy (GtkObject *object);
631 static void sp_canvas_group_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags);
632 static double sp_canvas_group_point (SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item);
633 static void sp_canvas_group_render (SPCanvasItem *item, SPCanvasBuf *buf);
635 static SPCanvasItemClass *group_parent_class;
637 /**
638  * Registers SPCanvasGroup class with Gtk and returns its type number.
639  */
640 GtkType
641 sp_canvas_group_get_type (void)
643     static GtkType group_type = 0;
645     if (!group_type) {
646         static const GtkTypeInfo group_info = {
647             "SPCanvasGroup",
648             sizeof (SPCanvasGroup),
649             sizeof (SPCanvasGroupClass),
650             (GtkClassInitFunc) sp_canvas_group_class_init,
651             (GtkObjectInitFunc) sp_canvas_group_init,
652             NULL, NULL, NULL
653         };
655         group_type = gtk_type_unique (sp_canvas_item_get_type (), &group_info);
656     }
658     return group_type;
661 /**
662  * Class initialization function for SPCanvasGroupClass
663  */
664 static void
665 sp_canvas_group_class_init (SPCanvasGroupClass *klass)
667     GtkObjectClass *object_class = (GtkObjectClass *) klass;
668     SPCanvasItemClass *item_class = (SPCanvasItemClass *) klass;
670     group_parent_class = (SPCanvasItemClass*)gtk_type_class (sp_canvas_item_get_type ());
672     object_class->destroy = sp_canvas_group_destroy;
674     item_class->update = sp_canvas_group_update;
675     item_class->render = sp_canvas_group_render;
676     item_class->point = sp_canvas_group_point;
679 /**
680  * Callback. Empty.
681  */
682 static void
683 sp_canvas_group_init (SPCanvasGroup */*group*/)
685     /* Nothing here */
688 /**
689  * Callback that destroys all items in group and calls group's virtual 
690  * destroy() function.
691  */
692 static void
693 sp_canvas_group_destroy (GtkObject *object)
695     g_return_if_fail (object != NULL);
696     g_return_if_fail (SP_IS_CANVAS_GROUP (object));
698     const SPCanvasGroup *group = SP_CANVAS_GROUP (object);
700     GList *list = group->items;
701     while (list) {
702         SPCanvasItem *child = (SPCanvasItem *)list->data;
703         list = list->next;
705         gtk_object_destroy (GTK_OBJECT (child));
706     }
708     if (GTK_OBJECT_CLASS (group_parent_class)->destroy)
709         (* GTK_OBJECT_CLASS (group_parent_class)->destroy) (object);
712 /**
713  * Update handler for canvas groups
714  */
715 static void
716 sp_canvas_group_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
718     const SPCanvasGroup *group = SP_CANVAS_GROUP (item);
719     NR::ConvexHull corners(NR::Point(0, 0));
720     bool empty=true;
722     for (GList *list = group->items; list; list = list->next) {
723         SPCanvasItem *i = (SPCanvasItem *)list->data;
725         sp_canvas_item_invoke_update (i, affine, flags);
727         if ( i->x2 > i->x1 && i->y2 > i->y1 ) {
728             if (empty) {
729                 corners = NR::ConvexHull(NR::Point(i->x1, i->y1));
730                 empty = false;
731             } else {
732                 corners.add(NR::Point(i->x1, i->y1));
733             }
734             corners.add(NR::Point(i->x2, i->y2));
735         }
736     }
738     NR::Rect const &bounds = corners.bounds();
739     item->x1 = bounds.min()[NR::X];
740     item->y1 = bounds.min()[NR::Y];
741     item->x2 = bounds.max()[NR::X];
742     item->y2 = bounds.max()[NR::Y];
745 /**
746  * Point handler for canvas groups.
747  */
748 static double
749 sp_canvas_group_point (SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item)
751     const SPCanvasGroup *group = SP_CANVAS_GROUP (item);
752     const double x = p[NR::X];
753     const double y = p[NR::Y];
754     int x1 = (int)(x - item->canvas->close_enough);
755     int y1 = (int)(y - item->canvas->close_enough);
756     int x2 = (int)(x + item->canvas->close_enough);
757     int y2 = (int)(y + item->canvas->close_enough);
759     double best = 0.0;
760     *actual_item = NULL;
762     double dist = 0.0;
764     for (GList *list = group->items; list; list = list->next) {
765         SPCanvasItem *child = (SPCanvasItem *)list->data;
767         if ((child->x1 <= x2) && (child->y1 <= y2) && (child->x2 >= x1) && (child->y2 >= y1)) {
768             SPCanvasItem *point_item = NULL; /* cater for incomplete item implementations */
770             int has_point;
771             if ((child->flags & SP_CANVAS_ITEM_VISIBLE) && SP_CANVAS_ITEM_GET_CLASS (child)->point) {
772                 dist = sp_canvas_item_invoke_point (child, p, &point_item);
773                 has_point = TRUE;
774             } else
775                 has_point = FALSE;
777             if (has_point && point_item && ((int) (dist + 0.5) <= item->canvas->close_enough)) {
778                 best = dist;
779                 *actual_item = point_item;
780             }
781         }
782     }
784     return best;
787 /**
788  * Renders all visible canvas group items in buf rectangle.
789  */
790 static void
791 sp_canvas_group_render (SPCanvasItem *item, SPCanvasBuf *buf)
793     const SPCanvasGroup *group = SP_CANVAS_GROUP (item);
795     for (GList *list = group->items; list; list = list->next) {
796         SPCanvasItem *child = (SPCanvasItem *)list->data;
797         if (child->flags & SP_CANVAS_ITEM_VISIBLE) {
798             if ((child->x1 < buf->rect.x1) &&
799                 (child->y1 < buf->rect.y1) &&
800                 (child->x2 > buf->rect.x0) &&
801                 (child->y2 > buf->rect.y0)) {
802                 if (SP_CANVAS_ITEM_GET_CLASS (child)->render)
803                     SP_CANVAS_ITEM_GET_CLASS (child)->render (child, buf);
804             }
805         }
806     }
809 /**
810  * Adds an item to a canvas group.
811  */
812 static void
813 group_add (SPCanvasGroup *group, SPCanvasItem *item)
815     gtk_object_ref (GTK_OBJECT (item));
816     gtk_object_sink (GTK_OBJECT (item));
818     if (!group->items) {
819         group->items = g_list_append (group->items, item);
820         group->last = group->items;
821     } else {
822         group->last = g_list_append (group->last, item)->next;
823     }
825     sp_canvas_item_request_update (item);
828 /** 
829  * Removes an item from a canvas group
830  */
831 static void
832 group_remove (SPCanvasGroup *group, SPCanvasItem *item)
834     g_return_if_fail (group != NULL);
835     g_return_if_fail (SP_IS_CANVAS_GROUP (group));
836     g_return_if_fail (item != NULL);
838     for (GList *children = group->items; children; children = children->next) {
839         if (children->data == item) {
841             /* Unparent the child */
843             item->parent = NULL;
844             gtk_object_unref (GTK_OBJECT (item));
846             /* Remove it from the list */
848             if (children == group->last) group->last = children->prev;
850             group->items = g_list_remove_link (group->items, children);
851             g_list_free (children);
852             break;
853         }
854     }
857 /* SPCanvas */
859 static void sp_canvas_class_init (SPCanvasClass *klass);
860 static void sp_canvas_init (SPCanvas *canvas);
861 static void sp_canvas_destroy (GtkObject *object);
863 static void sp_canvas_realize (GtkWidget *widget);
864 static void sp_canvas_unrealize (GtkWidget *widget);
866 static void sp_canvas_size_request (GtkWidget *widget, GtkRequisition *req);
867 static void sp_canvas_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
869 static gint sp_canvas_button (GtkWidget *widget, GdkEventButton *event);
870 static gint sp_canvas_scroll (GtkWidget *widget, GdkEventScroll *event);
871 static gint sp_canvas_motion (GtkWidget *widget, GdkEventMotion *event);
872 static gint sp_canvas_expose (GtkWidget *widget, GdkEventExpose *event);
873 static gint sp_canvas_key (GtkWidget *widget, GdkEventKey *event);
874 static gint sp_canvas_crossing (GtkWidget *widget, GdkEventCrossing *event);
875 static gint sp_canvas_focus_in (GtkWidget *widget, GdkEventFocus *event);
876 static gint sp_canvas_focus_out (GtkWidget *widget, GdkEventFocus *event);
878 static GtkWidgetClass *canvas_parent_class;
880 void sp_canvas_resize_tiles(SPCanvas* canvas,int nl,int nt,int nr,int nb);
881 void sp_canvas_dirty_rect(SPCanvas* canvas,int nl,int nt,int nr,int nb);
883 /**
884  * Registers the SPCanvas class if necessary, and returns the type ID
885  * associated to it.
886  *
887  * \return The type ID of the SPCanvas class.
888  **/
889 GtkType
890 sp_canvas_get_type (void)
892     static GtkType canvas_type = 0;
894     if (!canvas_type) {
895         static const GtkTypeInfo canvas_info = {
896             "SPCanvas",
897             sizeof (SPCanvas),
898             sizeof (SPCanvasClass),
899             (GtkClassInitFunc) sp_canvas_class_init,
900             (GtkObjectInitFunc) sp_canvas_init,
901             NULL, NULL, NULL
902         };
904         canvas_type = gtk_type_unique (GTK_TYPE_WIDGET, &canvas_info);
905     }
907     return canvas_type;
910 /**
911  * Class initialization function for SPCanvasClass.
912  */
913 static void
914 sp_canvas_class_init (SPCanvasClass *klass)
916     GtkObjectClass *object_class = (GtkObjectClass *) klass;
917     GtkWidgetClass *widget_class = (GtkWidgetClass *) klass;
919     canvas_parent_class = (GtkWidgetClass *)gtk_type_class (GTK_TYPE_WIDGET);
921     object_class->destroy = sp_canvas_destroy;
923     widget_class->realize = sp_canvas_realize;
924     widget_class->unrealize = sp_canvas_unrealize;
925     widget_class->size_request = sp_canvas_size_request;
926     widget_class->size_allocate = sp_canvas_size_allocate;
927     widget_class->button_press_event = sp_canvas_button;
928     widget_class->button_release_event = sp_canvas_button;
929     widget_class->motion_notify_event = sp_canvas_motion;
930     widget_class->scroll_event = sp_canvas_scroll;
931     widget_class->expose_event = sp_canvas_expose;
932     widget_class->key_press_event = sp_canvas_key;
933     widget_class->key_release_event = sp_canvas_key;
934     widget_class->enter_notify_event = sp_canvas_crossing;
935     widget_class->leave_notify_event = sp_canvas_crossing;
936     widget_class->focus_in_event = sp_canvas_focus_in;
937     widget_class->focus_out_event = sp_canvas_focus_out;
940 /** 
941  * Callback: object initialization for SPCanvas.
942  */
943 static void
944 sp_canvas_init (SPCanvas *canvas)
946     GTK_WIDGET_UNSET_FLAGS (canvas, GTK_NO_WINDOW);
947     GTK_WIDGET_UNSET_FLAGS (canvas, GTK_DOUBLE_BUFFERED);
948     GTK_WIDGET_SET_FLAGS (canvas, GTK_CAN_FOCUS);
950     canvas->pick_event.type = GDK_LEAVE_NOTIFY;
951     canvas->pick_event.crossing.x = 0;
952     canvas->pick_event.crossing.y = 0;
954     /* Create the root item as a special case */
955     canvas->root = SP_CANVAS_ITEM (gtk_type_new (sp_canvas_group_get_type ()));
956     canvas->root->canvas = canvas;
958     gtk_object_ref (GTK_OBJECT (canvas->root));
959     gtk_object_sink (GTK_OBJECT (canvas->root));
961     canvas->need_repick = TRUE;
963     // See comment at in sp-canvas.h.
964     canvas->gen_all_enter_events = false;
966     canvas->tiles=NULL;
967     canvas->tLeft=canvas->tTop=canvas->tRight=canvas->tBottom=0;
968     canvas->tileH=canvas->tileV=0;
971 /**
972  * Convenience function to remove the idle handler of a canvas.
973  */
974 static void
975 remove_idle (SPCanvas *canvas)
977     if (canvas->idle_id) {
978         gtk_idle_remove (canvas->idle_id);
979         canvas->idle_id = 0;
980     }
983 /*
984  * Removes the transient state of the canvas (idle handler, grabs).
985  */
986 static void
987 shutdown_transients (SPCanvas *canvas)
989     /* We turn off the need_redraw flag, since if the canvas is mapped again
990      * it will request a redraw anyways.  We do not turn off the need_update
991      * flag, though, because updates are not queued when the canvas remaps
992      * itself.
993      */
994     if (canvas->need_redraw) {
995         canvas->need_redraw = FALSE;
996     }
997     if ( canvas->tiles ) free(canvas->tiles);
998     canvas->tiles=NULL;
999     canvas->tLeft=canvas->tTop=canvas->tRight=canvas->tBottom=0;
1000     canvas->tileH=canvas->tileV=0;
1002     if (canvas->grabbed_item) {
1003         canvas->grabbed_item = NULL;
1004         gdk_pointer_ungrab (GDK_CURRENT_TIME);
1005     }
1007     remove_idle (canvas);
1010 /**
1011  * Destroy handler for SPCanvas.
1012  */
1013 static void
1014 sp_canvas_destroy (GtkObject *object)
1016     SPCanvas *canvas = SP_CANVAS (object);
1018     if (canvas->root) {
1019         gtk_object_unref (GTK_OBJECT (canvas->root));
1020         canvas->root = NULL;
1021     }
1023     shutdown_transients (canvas);
1025     if (GTK_OBJECT_CLASS (canvas_parent_class)->destroy)
1026         (* GTK_OBJECT_CLASS (canvas_parent_class)->destroy) (object);
1029 /**
1030  * Returns new canvas as widget.
1031  */
1032 GtkWidget *
1033 sp_canvas_new_aa (void)
1035     SPCanvas *canvas = (SPCanvas *)gtk_type_new (sp_canvas_get_type ());
1037     return (GtkWidget *) canvas;
1040 /**
1041  * The canvas widget's realize callback.
1042  */
1043 static void
1044 sp_canvas_realize (GtkWidget *widget)
1046     SPCanvas *canvas = SP_CANVAS (widget);
1048     GdkWindowAttr attributes;
1049     attributes.window_type = GDK_WINDOW_CHILD;
1050     attributes.x = widget->allocation.x;
1051     attributes.y = widget->allocation.y;
1052     attributes.width = widget->allocation.width;
1053     attributes.height = widget->allocation.height;
1054     attributes.wclass = GDK_INPUT_OUTPUT;
1055     attributes.visual = gdk_rgb_get_visual ();
1056     attributes.colormap = gdk_rgb_get_cmap ();
1057     attributes.event_mask = (gtk_widget_get_events (widget) |
1058                              GDK_EXPOSURE_MASK |
1059                              GDK_BUTTON_PRESS_MASK |
1060                              GDK_BUTTON_RELEASE_MASK |
1061                              GDK_POINTER_MOTION_MASK |
1062                              GDK_PROXIMITY_IN_MASK |
1063                              GDK_PROXIMITY_OUT_MASK |
1064                              GDK_KEY_PRESS_MASK |
1065                              GDK_KEY_RELEASE_MASK |
1066                              GDK_ENTER_NOTIFY_MASK |
1067                              GDK_LEAVE_NOTIFY_MASK |
1068                              GDK_FOCUS_CHANGE_MASK);
1069     gint attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1071     widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1072     gdk_window_set_user_data (widget->window, widget);
1073     gtk_widget_set_events(widget, attributes.event_mask);
1075     GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1077     canvas->pixmap_gc = gdk_gc_new (SP_CANVAS_WINDOW (canvas));
1080 /**
1081  * The canvas widget's unrealize callback.
1082  */
1083 static void
1084 sp_canvas_unrealize (GtkWidget *widget)
1086     SPCanvas *canvas = SP_CANVAS (widget);
1088     shutdown_transients (canvas);
1090     gdk_gc_destroy (canvas->pixmap_gc);
1091     canvas->pixmap_gc = NULL;
1093     if (GTK_WIDGET_CLASS (canvas_parent_class)->unrealize)
1094         (* GTK_WIDGET_CLASS (canvas_parent_class)->unrealize) (widget);
1097 /**
1098  * The canvas widget's size_request callback.
1099  */
1100 static void
1101 sp_canvas_size_request (GtkWidget *widget, GtkRequisition *req)
1103     static_cast<void>(SP_CANVAS (widget));
1105     req->width = 256;
1106     req->height = 256;
1109 /**
1110  * The canvas widget's size_allocate callback.
1111  */
1112 static void
1113 sp_canvas_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
1115     SPCanvas *canvas = SP_CANVAS (widget);
1117     /* Schedule redraw of new region */
1118     sp_canvas_resize_tiles(canvas,canvas->x0,canvas->y0,canvas->x0+allocation->width,canvas->y0+allocation->height);
1119     if (allocation->width > widget->allocation.width) {
1120         sp_canvas_request_redraw (canvas,
1121                                   canvas->x0 + widget->allocation.width,
1122                                   0,
1123                                   canvas->x0 + allocation->width,
1124                                   canvas->y0 + allocation->height);
1125     }
1126     if (allocation->height > widget->allocation.height) {
1127         sp_canvas_request_redraw (canvas,
1128                                   0,
1129                                   canvas->y0 + widget->allocation.height,
1130                                   canvas->x0 + allocation->width,
1131                                   canvas->y0 + allocation->height);
1132     }
1134     widget->allocation = *allocation;
1136     if (GTK_WIDGET_REALIZED (widget)) {
1137         gdk_window_move_resize (widget->window,
1138                                 widget->allocation.x, widget->allocation.y,
1139                                 widget->allocation.width, widget->allocation.height);
1140     }
1143 /**
1144  * Helper that emits an event for an item in the canvas, be it the current 
1145  * item, grabbed item, or focused item, as appropriate.
1146  */
1147 static int
1148 emit_event (SPCanvas *canvas, GdkEvent *event)
1150     guint mask;
1152     if (canvas->grabbed_item) {
1153         switch (event->type) {
1154         case GDK_ENTER_NOTIFY:
1155             mask = GDK_ENTER_NOTIFY_MASK;
1156             break;
1157         case GDK_LEAVE_NOTIFY:
1158             mask = GDK_LEAVE_NOTIFY_MASK;
1159             break;
1160         case GDK_MOTION_NOTIFY:
1161             mask = GDK_POINTER_MOTION_MASK;
1162             break;
1163         case GDK_BUTTON_PRESS:
1164         case GDK_2BUTTON_PRESS:
1165         case GDK_3BUTTON_PRESS:
1166             mask = GDK_BUTTON_PRESS_MASK;
1167             break;
1168         case GDK_BUTTON_RELEASE:
1169             mask = GDK_BUTTON_RELEASE_MASK;
1170             break;
1171         case GDK_KEY_PRESS:
1172             mask = GDK_KEY_PRESS_MASK;
1173             break;
1174         case GDK_KEY_RELEASE:
1175             mask = GDK_KEY_RELEASE_MASK;
1176             break;
1177         case GDK_SCROLL:
1178             mask = GDK_SCROLL;
1179             break;
1180         default:
1181             mask = 0;
1182             break;
1183         }
1185         if (!(mask & canvas->grabbed_event_mask)) return FALSE;
1186     }
1188     /* Convert to world coordinates -- we have two cases because of diferent
1189      * offsets of the fields in the event structures.
1190      */
1192     GdkEvent ev = *event;
1194     switch (ev.type) {
1195     case GDK_ENTER_NOTIFY:
1196     case GDK_LEAVE_NOTIFY:
1197         ev.crossing.x += canvas->x0;
1198         ev.crossing.y += canvas->y0;
1199         break;
1200     case GDK_MOTION_NOTIFY:
1201     case GDK_BUTTON_PRESS:
1202     case GDK_2BUTTON_PRESS:
1203     case GDK_3BUTTON_PRESS:
1204     case GDK_BUTTON_RELEASE:
1205         ev.motion.x += canvas->x0;
1206         ev.motion.y += canvas->y0;
1207         break;
1208     default:
1209         break;
1210     }
1212     /* Choose where we send the event */
1214     /* canvas->current_item becomes NULL in some cases under Win32
1215     ** (e.g. if the pointer leaves the window).  So this is a hack that
1216     ** Lauris applied to SP to get around the problem.
1217     */
1218     SPCanvasItem* item = NULL;
1219     if (canvas->grabbed_item && !is_descendant (canvas->current_item, canvas->grabbed_item)) {
1220         item = canvas->grabbed_item;
1221     } else {
1222         item = canvas->current_item;
1223     }
1225     if (canvas->focused_item &&
1226         ((event->type == GDK_KEY_PRESS) ||
1227          (event->type == GDK_KEY_RELEASE) ||
1228          (event->type == GDK_FOCUS_CHANGE))) {
1229         item = canvas->focused_item;
1230     }
1232     /* The event is propagated up the hierarchy (for if someone connected to
1233      * a group instead of a leaf event), and emission is stopped if a
1234      * handler returns TRUE, just like for GtkWidget events.
1235      */
1237     gint finished = FALSE;
1239     while (item && !finished) {
1240         gtk_object_ref (GTK_OBJECT (item));
1241         gtk_signal_emit (GTK_OBJECT (item), item_signals[ITEM_EVENT], &ev, &finished);
1242         SPCanvasItem *parent = item->parent;
1243         gtk_object_unref (GTK_OBJECT (item));
1244         item = parent;
1245     }
1247     return finished;
1250 /**
1251  * Helper that re-picks the current item in the canvas, based on the event's 
1252  * coordinates and emits enter/leave events for items as appropriate.
1253  */
1254 static int
1255 pick_current_item (SPCanvas *canvas, GdkEvent *event)
1257     int button_down;
1258     double x, y;
1260     int retval = FALSE;
1262     if (canvas->gen_all_enter_events == false) {
1263         // If a button is down, we'll perform enter and leave events on the
1264         // current item, but not enter on any other item.  This is more or
1265         // less like X pointer grabbing for canvas items.
1266         //
1267         button_down = canvas->state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1268                 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK | GDK_BUTTON5_MASK);
1270         if (!button_down) canvas->left_grabbed_item = FALSE;
1271     }
1273     /* Save the event in the canvas.  This is used to synthesize enter and
1274      * leave events in case the current item changes.  It is also used to
1275      * re-pick the current item if the current one gets deleted.  Also,
1276      * synthesize an enter event.
1277      */
1278     if (event != &canvas->pick_event) {
1279         if ((event->type == GDK_MOTION_NOTIFY) || (event->type == GDK_BUTTON_RELEASE)) {
1280             /* these fields have the same offsets in both types of events */
1282             canvas->pick_event.crossing.type       = GDK_ENTER_NOTIFY;
1283             canvas->pick_event.crossing.window     = event->motion.window;
1284             canvas->pick_event.crossing.send_event = event->motion.send_event;
1285             canvas->pick_event.crossing.subwindow  = NULL;
1286             canvas->pick_event.crossing.x          = event->motion.x;
1287             canvas->pick_event.crossing.y          = event->motion.y;
1288             canvas->pick_event.crossing.mode       = GDK_CROSSING_NORMAL;
1289             canvas->pick_event.crossing.detail     = GDK_NOTIFY_NONLINEAR;
1290             canvas->pick_event.crossing.focus      = FALSE;
1291             canvas->pick_event.crossing.state      = event->motion.state;
1293             /* these fields don't have the same offsets in both types of events */
1295             if (event->type == GDK_MOTION_NOTIFY) {
1296                 canvas->pick_event.crossing.x_root = event->motion.x_root;
1297                 canvas->pick_event.crossing.y_root = event->motion.y_root;
1298             } else {
1299                 canvas->pick_event.crossing.x_root = event->button.x_root;
1300                 canvas->pick_event.crossing.y_root = event->button.y_root;
1301             }
1302         } else {
1303             canvas->pick_event = *event;
1304         }
1305     }
1307     /* Don't do anything else if this is a recursive call */
1308     if (canvas->in_repick) return retval;
1310     /* LeaveNotify means that there is no current item, so we don't look for one */
1311     if (canvas->pick_event.type != GDK_LEAVE_NOTIFY) {
1312         /* these fields don't have the same offsets in both types of events */
1314         if (canvas->pick_event.type == GDK_ENTER_NOTIFY) {
1315             x = canvas->pick_event.crossing.x;
1316             y = canvas->pick_event.crossing.y;
1317         } else {
1318             x = canvas->pick_event.motion.x;
1319             y = canvas->pick_event.motion.y;
1320         }
1322         /* world coords */
1323         x += canvas->x0;
1324         y += canvas->y0;
1326         /* find the closest item */
1327         if (canvas->root->flags & SP_CANVAS_ITEM_VISIBLE) {
1328             sp_canvas_item_invoke_point (canvas->root, NR::Point(x, y), &canvas->new_current_item);
1329         } else {
1330             canvas->new_current_item = NULL;
1331         }
1332     } else {
1333         canvas->new_current_item = NULL;
1334     }
1336     if ((canvas->new_current_item == canvas->current_item) && !canvas->left_grabbed_item) {
1337         return retval; /* current item did not change */
1338     }
1340     /* Synthesize events for old and new current items */
1342     if ((canvas->new_current_item != canvas->current_item)
1343         && (canvas->current_item != NULL)
1344         && !canvas->left_grabbed_item) {
1345         GdkEvent new_event;
1346         SPCanvasItem *item;
1348         item = canvas->current_item;
1350         new_event = canvas->pick_event;
1351         new_event.type = GDK_LEAVE_NOTIFY;
1353         new_event.crossing.detail = GDK_NOTIFY_ANCESTOR;
1354         new_event.crossing.subwindow = NULL;
1355         canvas->in_repick = TRUE;
1356         retval = emit_event (canvas, &new_event);
1357         canvas->in_repick = FALSE;
1358     }
1360     if (canvas->gen_all_enter_events == false) {
1361         // new_current_item may have been set to NULL during the call to
1362         // emit_event() above
1363         if ((canvas->new_current_item != canvas->current_item) && button_down) {
1364             canvas->left_grabbed_item = TRUE;
1365             return retval;
1366         }
1367     }
1369     /* Handle the rest of cases */
1371     canvas->left_grabbed_item = FALSE;
1372     canvas->current_item = canvas->new_current_item;
1374     if (canvas->current_item != NULL) {
1375         GdkEvent new_event;
1377         new_event = canvas->pick_event;
1378         new_event.type = GDK_ENTER_NOTIFY;
1379         new_event.crossing.detail = GDK_NOTIFY_ANCESTOR;
1380         new_event.crossing.subwindow = NULL;
1381         retval = emit_event (canvas, &new_event);
1382     }
1384     return retval;
1387 /**
1388  * Button event handler for the canvas.
1389  */
1390 static gint
1391 sp_canvas_button (GtkWidget *widget, GdkEventButton *event)
1393     SPCanvas *canvas = SP_CANVAS (widget);
1395     int retval = FALSE;
1397     /* dispatch normally regardless of the event's window if an item has
1398        has a pointer grab in effect */
1399     if (!canvas->grabbed_item && 
1400         event->window != SP_CANVAS_WINDOW (canvas))
1401         return retval;
1403     int mask;
1404     switch (event->button) {
1405     case 1:
1406         mask = GDK_BUTTON1_MASK;
1407         break;
1408     case 2:
1409         mask = GDK_BUTTON2_MASK;
1410         break;
1411     case 3:
1412         mask = GDK_BUTTON3_MASK;
1413         break;
1414     case 4:
1415         mask = GDK_BUTTON4_MASK;
1416         break;
1417     case 5:
1418         mask = GDK_BUTTON5_MASK;
1419         break;
1420     default:
1421         mask = 0;
1422     }
1424     switch (event->type) {
1425     case GDK_BUTTON_PRESS:
1426     case GDK_2BUTTON_PRESS:
1427     case GDK_3BUTTON_PRESS:
1428         /* Pick the current item as if the button were not pressed, and
1429          * then process the event.
1430          */
1431         canvas->state = event->state;
1432         pick_current_item (canvas, (GdkEvent *) event);
1433         canvas->state ^= mask;
1434         retval = emit_event (canvas, (GdkEvent *) event);
1435         break;
1437     case GDK_BUTTON_RELEASE:
1438         /* Process the event as if the button were pressed, then repick
1439          * after the button has been released
1440          */
1441         canvas->state = event->state;
1442         retval = emit_event (canvas, (GdkEvent *) event);
1443         event->state ^= mask;
1444         canvas->state = event->state;
1445         pick_current_item (canvas, (GdkEvent *) event);
1446         event->state ^= mask;
1447         break;
1449     default:
1450         g_assert_not_reached ();
1451     }
1453     return retval;
1456 /**
1457  * Scroll event handler for the canvas.
1458  *
1459  * \todo FIXME: generate motion events to re-select items.
1460  */
1461 static gint
1462 sp_canvas_scroll (GtkWidget *widget, GdkEventScroll *event)
1464     return emit_event (SP_CANVAS (widget), (GdkEvent *) event);
1467 /**
1468  * Motion event handler for the canvas.
1469  */
1470 static int
1471 sp_canvas_motion (GtkWidget *widget, GdkEventMotion *event)
1473     SPCanvas *canvas = SP_CANVAS (widget);
1475     if (event->window != SP_CANVAS_WINDOW (canvas))
1476         return FALSE;
1478     if (canvas->grabbed_event_mask & GDK_POINTER_MOTION_HINT_MASK) {
1479         gint x, y;
1480         gdk_window_get_pointer (widget->window, &x, &y, NULL);
1481         event->x = x;
1482         event->y = y;
1483     }
1485     canvas->state = event->state;
1486     pick_current_item (canvas, (GdkEvent *) event);
1488     return emit_event (canvas, (GdkEvent *) event);
1491 /**
1492  * Helper that draws a specific rectangular part of the canvas.
1493  */
1494 static void
1495 sp_canvas_paint_rect (SPCanvas *canvas, int xx0, int yy0, int xx1, int yy1)
1497     g_return_if_fail (!canvas->need_update);
1499     GtkWidget *widget = GTK_WIDGET (canvas);
1501     int draw_x1 = MAX (xx0, canvas->x0);
1502     int draw_y1 = MAX (yy0, canvas->y0);
1503     int draw_x2 = MIN (xx1, canvas->x0/*draw_x1*/ + GTK_WIDGET (canvas)->allocation.width);
1504     int draw_y2 = MIN (yy1, canvas->y0/*draw_y1*/ + GTK_WIDGET (canvas)->allocation.height);
1506     int bw = draw_x2 - draw_x1;
1507     int bh = draw_y2 - draw_y1;
1508     if ((bw < 1) || (bh < 1))
1509         return;
1511     int sw, sh;
1512     if (canvas->rendermode != RENDERMODE_OUTLINE) { // use 256K as a compromise to not slow down gradients
1513         /* 256K is the cached buffer and we need 3 channels */
1514         if (bw * bh <  87381) { // 256K/3
1515             // We can go with single buffer 
1516             sw = bw;
1517             sh = bh;
1518         } else if (bw <= (16 * 341)) {
1519             // Go with row buffer 
1520             sw = bw;
1521             sh =  87381 / bw;
1522         } else if (bh <= (16 * 256)) {
1523             // Go with column buffer 
1524             sw = 87381 / bh;
1525             sh = bh;
1526         } else {
1527             sw = 341;
1528             sh = 256;
1529         }
1530     } else {  // paths only, so 1M works faster
1531         /* 1M is the cached buffer and we need 3 channels */
1532         if (bw * bh <  349525) { // 1M/3
1533             // We can go with single buffer 
1534             sw = bw;
1535             sh = bh;
1536         } else if (bw <= (16 * 682)) {
1537             // Go with row buffer 
1538             sw = bw;
1539             sh =  349525 / bw;
1540         } else if (bh <= (16 * 512)) {
1541             // Go with column buffer 
1542             sw = 349525 / bh;
1543             sh = bh;
1544         } else {
1545             sw = 682;
1546             sh = 512;
1547         }
1548     }
1550     // As we can come from expose, we have to tile here 
1551     for (int y0 = draw_y1; y0 < draw_y2; y0 += sh) {
1552         int y1 = MIN (y0 + sh, draw_y2);
1553         for (int x0 = draw_x1; x0 < draw_x2; x0 += sw) {
1554             int x1 = MIN (x0 + sw, draw_x2);
1556             SPCanvasBuf buf;
1557         if (canvas->rendermode != RENDERMODE_OUTLINE) {
1558             buf.buf = nr_pixelstore_256K_new (FALSE, 0);
1559   } else {
1560             buf.buf = nr_pixelstore_1M_new (FALSE, 0);
1561   }
1563             buf.buf_rowstride = sw * 3;
1564             buf.rect.x0 = x0;
1565             buf.rect.y0 = y0;
1566             buf.rect.x1 = x1;
1567             buf.rect.y1 = y1;
1568             GdkColor *color = &widget->style->bg[GTK_STATE_NORMAL];
1569             buf.bg_color = (((color->red & 0xff00) << 8)
1570                             | (color->green & 0xff00)
1571                             | (color->blue >> 8));
1572             buf.is_empty = true;
1573       
1574             if (canvas->root->flags & SP_CANVAS_ITEM_VISIBLE) {
1575                 SP_CANVAS_ITEM_GET_CLASS (canvas->root)->render (canvas->root, &buf);
1576             }
1577       
1578             if (buf.is_empty) {
1579                 gdk_rgb_gc_set_foreground (canvas->pixmap_gc, buf.bg_color);
1580                 gdk_draw_rectangle (SP_CANVAS_WINDOW (canvas),
1581                                     canvas->pixmap_gc,
1582                                     TRUE,
1583                                     x0 - canvas->x0, y0 - canvas->y0,
1584                                     x1 - x0, y1 - y0);
1585             } else {
1586                 gdk_draw_rgb_image_dithalign (SP_CANVAS_WINDOW (canvas),
1587                                               canvas->pixmap_gc,
1588                                               x0 - canvas->x0, y0 - canvas->y0,
1589                                               x1 - x0, y1 - y0,
1590                                               GDK_RGB_DITHER_MAX,
1591                                               buf.buf,
1592                                               sw * 3,
1593                                               x0 - canvas->x0, y0 - canvas->y0);
1594             }
1596         if (canvas->rendermode != RENDERMODE_OUTLINE) {
1597             nr_pixelstore_256K_free (buf.buf);
1598   } else {
1599             nr_pixelstore_1M_free (buf.buf);
1600   }
1602         }
1603     }
1606 /**
1607  * The canvas widget's expose callback.
1608  */
1609 static gint
1610 sp_canvas_expose (GtkWidget *widget, GdkEventExpose *event)
1612     SPCanvas *canvas = SP_CANVAS (widget);
1614     if (!GTK_WIDGET_DRAWABLE (widget) || 
1615         (event->window != SP_CANVAS_WINDOW (canvas)))
1616         return FALSE;
1618     int n_rects;
1619     GdkRectangle *rects;
1620     gdk_region_get_rectangles (event->region, &rects, &n_rects);
1622     for (int i = 0; i < n_rects; i++) {
1623         NRRectL rect;
1624                 
1625         rect.x0 = rects[i].x + canvas->x0;
1626         rect.y0 = rects[i].y + canvas->y0;
1627         rect.x1 = rect.x0 + rects[i].width;
1628         rect.y1 = rect.y0 + rects[i].height;
1630         if (canvas->need_update || canvas->need_redraw) {
1631             sp_canvas_request_redraw (canvas, rect.x0, rect.y0, rect.x1, rect.y1);
1632         } else {
1633             /* No pending updates, draw exposed area immediately */
1634             sp_canvas_paint_rect (canvas, rect.x0, rect.y0, rect.x1, rect.y1);
1635         }
1636     }
1638     if (n_rects > 0)
1639         g_free (rects);
1641     return FALSE;
1644 /**
1645  * The canvas widget's keypress callback.
1646  */
1647 static gint
1648 sp_canvas_key (GtkWidget *widget, GdkEventKey *event)
1650     return emit_event (SP_CANVAS (widget), (GdkEvent *) event);
1653 /**
1654  * Crossing event handler for the canvas.
1655  */
1656 static gint
1657 sp_canvas_crossing (GtkWidget *widget, GdkEventCrossing *event)
1659     SPCanvas *canvas = SP_CANVAS (widget);
1661     if (event->window != SP_CANVAS_WINDOW (canvas))
1662         return FALSE;
1664     canvas->state = event->state;
1665     return pick_current_item (canvas, (GdkEvent *) event);
1668 /**
1669  * Focus in handler for the canvas.
1670  */
1671 static gint
1672 sp_canvas_focus_in (GtkWidget *widget, GdkEventFocus *event)
1674     GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
1676     SPCanvas *canvas = SP_CANVAS (widget);
1678     if (canvas->focused_item) {
1679         return emit_event (canvas, (GdkEvent *) event);
1680     } else {
1681         return FALSE;
1682     }
1685 /**
1686  * Focus out handler for the canvas.
1687  */
1688 static gint
1689 sp_canvas_focus_out (GtkWidget *widget, GdkEventFocus *event)
1691     GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
1693     SPCanvas *canvas = SP_CANVAS (widget);
1695     if (canvas->focused_item)
1696         return emit_event (canvas, (GdkEvent *) event);
1697     else
1698         return FALSE;
1701 /**
1702  * Helper that repaints the areas in the canvas that need it.
1703  */
1704 static int
1705 paint (SPCanvas *canvas)
1707     if (canvas->need_update) {
1708         sp_canvas_item_invoke_update (canvas->root, NR::identity(), 0);
1709         canvas->need_update = FALSE;
1710     }
1712     if (!canvas->need_redraw)
1713         return TRUE;
1715     GtkWidget const *widget = GTK_WIDGET(canvas);
1716     int const canvas_x1 = canvas->x0 + widget->allocation.width;
1717     int const canvas_y1 = canvas->y0 + widget->allocation.height;
1719     NRRectL topaint;
1720     topaint.x0 = topaint.y0 = topaint.x1 = topaint.y1 = 0;
1722     for (int j=canvas->tTop&(~3);j<canvas->tBottom;j+=4) {
1723         for (int i=canvas->tLeft&(~3);i<canvas->tRight;i+=4) {
1724             int  mode=0;
1725       
1726             int pl=i+1,pr=i,pt=j+4,pb=j;
1727             for (int l=MAX(j,canvas->tTop);l<MIN(j+4,canvas->tBottom);l++) {
1728                 for (int k=MAX(i,canvas->tLeft);k<MIN(i+4,canvas->tRight);k++) {
1729                     if ( canvas->tiles[(k-canvas->tLeft)+(l-canvas->tTop)*canvas->tileH] ) {
1730                         mode|=1<<((k-i)+(l-j)*4);
1731                         if ( k < pl ) pl=k;
1732                         if ( k+1 > pr ) pr=k+1;
1733                         if ( l < pt ) pt=l;
1734                         if ( l+1 > pb ) pb=l+1;
1735                     }
1736                     canvas->tiles[(k-canvas->tLeft)+(l-canvas->tTop)*canvas->tileH]=0;
1737                 }
1738             }
1739       
1740             if ( mode ) {
1741                 NRRectL tile;
1742                 tile.x0 = MAX (pl*32, canvas->x0);
1743                 tile.y0 = MAX (pt*32, canvas->y0);
1744                 tile.x1 = MIN (pr*32, canvas_x1);
1745                 tile.y1 = MIN (pb*32, canvas_y1);
1746                 if ((tile.x0 < tile.x1) && (tile.y0 < tile.y1)) {
1747                     nr_rect_l_union (&topaint, &topaint, &tile);
1748                 }
1750             }
1751         }
1752     }
1754     sp_canvas_paint_rect (canvas, topaint.x0, topaint.y0, topaint.x1, topaint.y1);
1756     canvas->need_redraw = FALSE;
1757     return TRUE;
1760 /**
1761  * Helper that invokes update, paint, and repick on canvas.
1762  */
1763 static int
1764 do_update (SPCanvas *canvas)
1766     /* Cause the update if necessary */
1767     if (canvas->need_update) {
1768         sp_canvas_item_invoke_update (canvas->root, NR::identity(), 0);
1769         canvas->need_update = FALSE;
1770     }
1772     /* Paint if able to */
1773     if (GTK_WIDGET_DRAWABLE (canvas)) {
1774             return paint (canvas);
1775     }
1777     /* Pick new current item */
1778     while (canvas->need_repick) {
1779         canvas->need_repick = FALSE;
1780         pick_current_item (canvas, &canvas->pick_event);
1781     }
1783     return TRUE;
1786 /**
1787  * Idle handler for the canvas that deals with pending updates and redraws.
1788  */
1789 static gint
1790 idle_handler (gpointer data)
1792     GDK_THREADS_ENTER ();
1794     SPCanvas *canvas = SP_CANVAS (data);
1796     const int ret = do_update (canvas);
1798     if (ret) {
1799         /* Reset idle id */
1800         canvas->idle_id = 0;
1801     }
1803     GDK_THREADS_LEAVE ();
1805     return !ret;
1808 /**
1809  * Convenience function to add an idle handler to a canvas.
1810  */
1811 static void
1812 add_idle (SPCanvas *canvas)
1814     if (canvas->idle_id != 0)
1815         return;
1817     canvas->idle_id = gtk_idle_add_priority (sp_canvas_update_priority, idle_handler, canvas);
1820 /**
1821  * Returns the root group of the specified canvas.
1822  */
1823 SPCanvasGroup *
1824 sp_canvas_root (SPCanvas *canvas)
1826     g_return_val_if_fail (canvas != NULL, NULL);
1827     g_return_val_if_fail (SP_IS_CANVAS (canvas), NULL);
1829     return SP_CANVAS_GROUP (canvas->root);
1832 /**
1833  * Scrolls canvas to specific position.
1834  */
1835 void
1836 sp_canvas_scroll_to (SPCanvas *canvas, double cx, double cy, unsigned int clear)
1838     g_return_if_fail (canvas != NULL);
1839     g_return_if_fail (SP_IS_CANVAS (canvas));
1841     int ix = (int) (cx + 0.5);
1842     int iy = (int) (cy + 0.5);
1843     int dx = ix - canvas->x0;
1844     int dy = iy - canvas->y0;
1846     canvas->dx0 = cx;
1847     canvas->dy0 = cy;
1848     canvas->x0 = ix;
1849     canvas->y0 = iy;
1851     sp_canvas_resize_tiles(canvas,canvas->x0,canvas->y0,canvas->x0+canvas->widget.allocation.width,canvas->y0+canvas->widget.allocation.height);
1853     if (!clear) {
1854         // scrolling without zoom; redraw only the newly exposed areas
1855         if ((dx != 0) || (dy != 0)) {
1856             int width, height;
1857             width = canvas->widget.allocation.width;
1858             height = canvas->widget.allocation.height;
1859             if (GTK_WIDGET_REALIZED (canvas)) {
1860                 gdk_window_scroll (SP_CANVAS_WINDOW (canvas), -dx, -dy);
1861                 gdk_window_process_updates (SP_CANVAS_WINDOW (canvas), TRUE);
1862             }
1863             if (dx < 0) {
1864                 sp_canvas_request_redraw (canvas, ix + 0, iy + 0, ix - dx, iy + height);
1865             } else if (dx > 0) {
1866                 sp_canvas_request_redraw (canvas, ix + width - dx, iy + 0, ix + width, iy + height);
1867             }
1868             if (dy < 0) {
1869                 sp_canvas_request_redraw (canvas, ix + 0, iy + 0, ix + width, iy - dy);
1870             } else if (dy > 0) {
1871                 sp_canvas_request_redraw (canvas, ix + 0, iy + height - dy, ix + width, iy + height);
1872             }
1873         }
1874     } else {
1875         // scrolling as part of zoom; do nothing here - the next do_update will perform full redraw
1876     }
1879 /** 
1880  * Updates canvas if necessary.
1881  */
1882 void
1883 sp_canvas_update_now (SPCanvas *canvas)
1885     g_return_if_fail (canvas != NULL);
1886     g_return_if_fail (SP_IS_CANVAS (canvas));
1888     if (!(canvas->need_update ||
1889           canvas->need_redraw))
1890         return;
1892     remove_idle (canvas);
1893     do_update (canvas);
1896 /**
1897  * Update callback for canvas widget.
1898  */
1899 static void
1900 sp_canvas_request_update (SPCanvas *canvas)
1902     canvas->need_update = TRUE;
1903     add_idle (canvas);
1906 /**
1907  * Forces redraw of rectangular canvas area.
1908  */
1909 void
1910 sp_canvas_request_redraw (SPCanvas *canvas, int x0, int y0, int x1, int y1)
1912     NRRectL bbox;
1913     NRRectL visible;
1914     NRRectL clip;
1916     g_return_if_fail (canvas != NULL);
1917     g_return_if_fail (SP_IS_CANVAS (canvas));
1919     if (!GTK_WIDGET_DRAWABLE (canvas)) return;
1920     if ((x0 >= x1) || (y0 >= y1)) return;
1922     bbox.x0 = x0;
1923     bbox.y0 = y0;
1924     bbox.x1 = x1;
1925     bbox.y1 = y1;
1927     visible.x0 = canvas->x0;
1928     visible.y0 = canvas->y0;
1929     visible.x1 = visible.x0 + GTK_WIDGET (canvas)->allocation.width;
1930     visible.y1 = visible.y0 + GTK_WIDGET (canvas)->allocation.height;
1932     nr_rect_l_intersect (&clip, &bbox, &visible);
1934     sp_canvas_dirty_rect(canvas,x0,y0,x1,y1);
1935     add_idle (canvas);
1938 /**
1939  * Sets world coordinates from win and canvas.
1940  */
1941 void sp_canvas_window_to_world(SPCanvas const *canvas, double winx, double winy, double *worldx, double *worldy)
1943     g_return_if_fail (canvas != NULL);
1944     g_return_if_fail (SP_IS_CANVAS (canvas));
1946     if (worldx) *worldx = canvas->x0 + winx;
1947     if (worldy) *worldy = canvas->y0 + winy;
1950 /**
1951  * Sets win coordinates from world and canvas.
1952  */
1953 void sp_canvas_world_to_window(SPCanvas const *canvas, double worldx, double worldy, double *winx, double *winy)
1955     g_return_if_fail (canvas != NULL);
1956     g_return_if_fail (SP_IS_CANVAS (canvas));
1958     if (winx) *winx = worldx - canvas->x0;
1959     if (winy) *winy = worldy - canvas->y0;
1962 /**
1963  * Converts point from win to world coordinates.
1964  */
1965 NR::Point sp_canvas_window_to_world(SPCanvas const *canvas, NR::Point const win)
1967     g_assert (canvas != NULL);
1968     g_assert (SP_IS_CANVAS (canvas));
1970     return NR::Point(canvas->x0 + win[0], canvas->y0 + win[1]);
1973 /**
1974  * Converts point from world to win coordinates.
1975  */
1976 NR::Point sp_canvas_world_to_window(SPCanvas const *canvas, NR::Point const world)
1978     g_assert (canvas != NULL);
1979     g_assert (SP_IS_CANVAS (canvas));
1981     return NR::Point(world[0] - canvas->x0, world[1] - canvas->y0);
1984 /**
1985  * Returns true if point given in world coordinates is inside window.
1986  */
1987 bool sp_canvas_world_pt_inside_window(SPCanvas const *canvas, NR::Point const &world)
1989     g_assert( canvas != NULL );
1990     g_assert(SP_IS_CANVAS(canvas));
1992     using NR::X;
1993     using NR::Y;
1994     GtkWidget const &w = *GTK_WIDGET(canvas);
1995     return ( ( canvas->x0 <= world[X] )  &&
1996              ( canvas->y0 <= world[Y] )  &&
1997              ( world[X] < canvas->x0 + w.allocation.width )  &&
1998              ( world[Y] < canvas->y0 + w.allocation.height ) );
2001 /**
2002  * Return canvas window coordinates as NRRect.
2003  */
2004 NR::Rect SPCanvas::getViewbox() const
2006     GtkWidget const *w = GTK_WIDGET(this);
2008     return NR::Rect(NR::Point(dx0, dy0),
2009                     NR::Point(dx0 + w->allocation.width, dy0 + w->allocation.height));
2012 inline int sp_canvas_tile_floor(int x)
2014     return (x&(~31))/32;
2017 inline int sp_canvas_tile_ceil(int x)
2019     return ((x+31)&(~31))/32;
2022 /**
2023  * Helper that changes tile size for canvas redraw.
2024  */
2025 void sp_canvas_resize_tiles(SPCanvas* canvas,int nl,int nt,int nr,int nb)
2027     if ( nl >= nr || nt >= nb ) {
2028         if ( canvas->tiles ) free(canvas->tiles);
2029         canvas->tLeft=canvas->tTop=canvas->tRight=canvas->tBottom=0;
2030         canvas->tileH=canvas->tileV=0;
2031         canvas->tiles=NULL;
2032         return;
2033     }
2034     int tl=sp_canvas_tile_floor(nl);
2035     int tt=sp_canvas_tile_floor(nt);
2036     int tr=sp_canvas_tile_ceil(nr);
2037     int tb=sp_canvas_tile_ceil(nb);
2039     int nh=tr-tl,nv=tb-tt;
2040     uint8_t* ntiles=(uint8_t*)malloc(nh*nv*sizeof(uint8_t));
2041     for (int i=tl;i<tr;i++) {
2042         for (int j=tt;j<tb;j++) {
2043             int ind=(i-tl)+(j-tt)*nh;
2044             if ( i >= canvas->tLeft && i < canvas->tRight && j >= canvas->tTop && j < canvas->tBottom ) {
2045                 ntiles[ind]=canvas->tiles[(i-canvas->tLeft)+(j-canvas->tTop)*canvas->tileH];
2046             } else {
2047                 ntiles[ind]=0;
2048             }
2049         }
2050     }
2051     if ( canvas->tiles ) free(canvas->tiles);
2052     canvas->tiles=ntiles;
2053     canvas->tLeft=tl;
2054     canvas->tTop=tt;
2055     canvas->tRight=tr;
2056     canvas->tBottom=tb;
2057     canvas->tileH=nh;
2058     canvas->tileV=nv;
2061 /**
2062  * Helper that marks specific canvas rectangle for redraw.
2063  */
2064 void sp_canvas_dirty_rect(SPCanvas* canvas,int nl,int nt,int nr,int nb)
2066     if ( nl >= nr || nt >= nb ) {
2067         return;
2068     }
2069     int tl=sp_canvas_tile_floor(nl);
2070     int tt=sp_canvas_tile_floor(nt);
2071     int tr=sp_canvas_tile_ceil(nr);
2072     int tb=sp_canvas_tile_ceil(nb);
2073     if ( tl >= canvas->tRight || tr <= canvas->tLeft || tt >= canvas->tBottom || tb <= canvas->tTop ) return;
2074     if ( tl < canvas->tLeft ) tl=canvas->tLeft;
2075     if ( tr > canvas->tRight ) tr=canvas->tRight;
2076     if ( tt < canvas->tTop ) tt=canvas->tTop;
2077     if ( tb > canvas->tBottom ) tb=canvas->tBottom;
2079     canvas->need_redraw = TRUE;
2081     for (int i=tl;i<tr;i++) {
2082         for (int j=tt;j<tb;j++) {
2083             canvas->tiles[(i-canvas->tLeft)+(j-canvas->tTop)*canvas->tileH]=1;
2084         }
2085     }
2089 /*
2090   Local Variables:
2091   mode:c++
2092   c-file-style:"stroustrup"
2093   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2094   indent-tabs-mode:nil
2095   fill-column:99
2096   End:
2097 */
2098 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :