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>
27 #include <gtk/gtkversion.h>
29 #include <gtkmm.h>
31 #include "helper/sp-marshal.h"
32 #include <helper/recthull.h>
33 #include <display/sp-canvas.h>
34 #include "display-forward.h"
35 #include <2geom/matrix.h>
36 #include <libnr/nr-convex-hull.h>
37 #include "preferences.h"
38 #include "inkscape.h"
39 #include "sodipodi-ctrlrect.h"
40 #if ENABLE_LCMS
41 #include "color-profile-fns.h"
42 #endif // ENABLE_LCMS
43 #include "display/rendermode.h"
44 #include "libnr/nr-blit.h"
45 #include "display/inkscape-cairo.h"
46 #include "debug/gdk-event-latency-tracker.h"
47 #include "desktop.h"
48 #include "sp-namedview.h"
50 using Inkscape::Debug::GdkEventLatencyTracker;
52 // GTK_CHECK_VERSION returns false on failure
53 #define HAS_GDK_EVENT_REQUEST_MOTIONS GTK_CHECK_VERSION(2, 12, 0)
55 // gtk_check_version returns non-NULL on failure
56 static bool const HAS_BROKEN_MOTION_HINTS =
57 true || gtk_check_version(2, 12, 0) != NULL || !HAS_GDK_EVENT_REQUEST_MOTIONS;
59 // Define this to visualize the regions to be redrawn
60 //#define DEBUG_REDRAW 1;
62 // Tiles are a way to minimize the number of redraws, eliminating too small redraws.
63 // The canvas stores a 2D array of ints, each representing a TILE_SIZExTILE_SIZE pixels tile.
64 // If any part of it is dirtied, the entire tile is dirtied (its int is nonzero) and repainted.
65 #define TILE_SIZE 16
67 static gint const sp_canvas_update_priority = G_PRIORITY_HIGH_IDLE;
69 #define SP_CANVAS_WINDOW(c) (((GtkWidget *) (c))->window)
71 enum {
72 SP_CANVAS_ITEM_VISIBLE = 1 << 7,
73 SP_CANVAS_ITEM_NEED_UPDATE = 1 << 8,
74 SP_CANVAS_ITEM_NEED_AFFINE = 1 << 9
75 };
77 /**
78 * A group of Items.
79 */
80 struct SPCanvasGroup {
81 SPCanvasItem item;
83 GList *items, *last;
84 };
86 /**
87 * The SPCanvasGroup vtable.
88 */
89 struct SPCanvasGroupClass {
90 SPCanvasItemClass parent_class;
91 };
93 /**
94 * The SPCanvas vtable.
95 */
96 struct SPCanvasClass {
97 GtkWidgetClass parent_class;
98 };
100 static void group_add (SPCanvasGroup *group, SPCanvasItem *item);
101 static void group_remove (SPCanvasGroup *group, SPCanvasItem *item);
103 /* SPCanvasItem */
105 enum {ITEM_EVENT, ITEM_LAST_SIGNAL};
106 enum {PROP_0, PROP_VISIBLE};
109 static void sp_canvas_request_update (SPCanvas *canvas);
111 static void track_latency(GdkEvent const *event);
112 static void sp_canvas_item_class_init (SPCanvasItemClass *klass);
113 static void sp_canvas_item_init (SPCanvasItem *item);
114 static void sp_canvas_item_dispose (GObject *object);
115 static void sp_canvas_item_construct (SPCanvasItem *item, SPCanvasGroup *parent, gchar const *first_arg_name, va_list args);
118 static int emit_event (SPCanvas *canvas, GdkEvent *event);
120 static guint item_signals[ITEM_LAST_SIGNAL] = { 0 };
122 /**
123 * Registers the SPCanvasItem class with Glib and returns its type number.
124 */
125 GType
126 sp_canvas_item_get_type (void)
127 {
128 static GType type = 0;
129 if (!type) {
130 static GTypeInfo const info = {
131 sizeof (SPCanvasItemClass),
132 NULL, NULL,
133 (GClassInitFunc) sp_canvas_item_class_init,
134 NULL, NULL,
135 sizeof (SPCanvasItem),
136 0,
137 (GInstanceInitFunc) sp_canvas_item_init,
138 NULL
139 };
140 type = g_type_register_static (GTK_TYPE_OBJECT, "SPCanvasItem", &info, (GTypeFlags)0);
141 }
143 return type;
144 }
146 /**
147 * Initializes the SPCanvasItem vtable and the "event" signal.
148 */
149 static void
150 sp_canvas_item_class_init (SPCanvasItemClass *klass)
151 {
152 GObjectClass *object_class = (GObjectClass *) klass;
154 item_signals[ITEM_EVENT] = g_signal_new ("event",
155 G_TYPE_FROM_CLASS (klass),
156 G_SIGNAL_RUN_LAST,
157 ((glong)((guint8*)&(klass->event) - (guint8*)klass)),
158 NULL, NULL,
159 sp_marshal_BOOLEAN__POINTER,
160 G_TYPE_BOOLEAN, 1,
161 GDK_TYPE_EVENT);
163 object_class->dispose = sp_canvas_item_dispose;
164 }
166 /**
167 * Callback for initialization of SPCanvasItem.
168 */
169 static void
170 sp_canvas_item_init (SPCanvasItem *item)
171 {
172 // TODO items should not be visible on creation - this causes kludges with items
173 // that should be initially invisible; examples of such items: node handles, the CtrlRect
174 // used for rubberbanding, path outline, etc.
175 item->flags |= SP_CANVAS_ITEM_VISIBLE;
176 item->xform = Geom::Matrix(Geom::identity());
177 }
179 /**
180 * Constructs new SPCanvasItem on SPCanvasGroup.
181 */
182 SPCanvasItem *
183 sp_canvas_item_new (SPCanvasGroup *parent, GtkType type, gchar const *first_arg_name, ...)
184 {
185 va_list args;
187 g_return_val_if_fail (parent != NULL, NULL);
188 g_return_val_if_fail (SP_IS_CANVAS_GROUP (parent), NULL);
189 g_return_val_if_fail (gtk_type_is_a (type, sp_canvas_item_get_type ()), NULL);
191 SPCanvasItem *item = SP_CANVAS_ITEM (gtk_type_new (type));
193 va_start (args, first_arg_name);
194 sp_canvas_item_construct (item, parent, first_arg_name, args);
195 va_end (args);
197 return item;
198 }
200 /**
201 * Sets up the newly created SPCanvasItem.
202 *
203 * We make it static for encapsulation reasons since it was nowhere used.
204 */
205 static void
206 sp_canvas_item_construct (SPCanvasItem *item, SPCanvasGroup *parent, gchar const *first_arg_name, va_list args)
207 {
208 g_return_if_fail (SP_IS_CANVAS_GROUP (parent));
209 g_return_if_fail (SP_IS_CANVAS_ITEM (item));
211 item->parent = SP_CANVAS_ITEM (parent);
212 item->canvas = item->parent->canvas;
214 g_object_set_valist (G_OBJECT (item), first_arg_name, args);
216 group_add (SP_CANVAS_GROUP (item->parent), item);
218 sp_canvas_item_request_update (item);
219 }
221 /**
222 * Helper function that requests redraw only if item's visible flag is set.
223 */
224 static void
225 redraw_if_visible (SPCanvasItem *item)
226 {
227 if (item->flags & SP_CANVAS_ITEM_VISIBLE) {
228 int x0 = (int)(item->x1);
229 int x1 = (int)(item->x2);
230 int y0 = (int)(item->y1);
231 int y1 = (int)(item->y2);
233 if (x0 !=0 || x1 !=0 || y0 !=0 || y1 !=0) {
234 sp_canvas_request_redraw (item->canvas, (int)(item->x1), (int)(item->y1), (int)(item->x2 + 1), (int)(item->y2 + 1));
235 }
236 }
237 }
239 /**
240 * Callback that removes item from all referers and destroys it.
241 */
242 static void
243 sp_canvas_item_dispose (GObject *object)
244 {
245 SPCanvasItem *item = SP_CANVAS_ITEM (object);
247 // Hack: if this is a ctrlrect, move it to 0,0;
248 // this redraws only the stroke of the rect to be deleted,
249 // avoiding redraw of the entire area
250 if (SP_IS_CTRLRECT(item)) {
251 SP_CTRLRECT(object)->setRectangle(Geom::Rect(Geom::Point(0,0),Geom::Point(0,0)));
252 SP_CTRLRECT(object)->update(item->xform, 0);
253 } else {
254 redraw_if_visible (item);
255 }
256 item->flags &= ~SP_CANVAS_ITEM_VISIBLE;
258 if (item == item->canvas->current_item) {
259 item->canvas->current_item = NULL;
260 item->canvas->need_repick = TRUE;
261 }
263 if (item == item->canvas->new_current_item) {
264 item->canvas->new_current_item = NULL;
265 item->canvas->need_repick = TRUE;
266 }
268 if (item == item->canvas->grabbed_item) {
269 item->canvas->grabbed_item = NULL;
270 gdk_pointer_ungrab (GDK_CURRENT_TIME);
271 }
273 if (item == item->canvas->focused_item)
274 item->canvas->focused_item = NULL;
276 if (item->parent) {
277 group_remove (SP_CANVAS_GROUP (item->parent), item);
278 }
280 G_OBJECT_CLASS (g_type_class_peek(g_type_parent(sp_canvas_item_get_type())))->dispose (object);
281 }
283 /**
284 * Helper function to update item and its children.
285 *
286 * NB! affine is parent2canvas.
287 */
288 static void
289 sp_canvas_item_invoke_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
290 {
291 /* Apply the child item's transform */
292 Geom::Matrix child_affine = item->xform * affine;
294 /* apply object flags to child flags */
295 int child_flags = flags & ~SP_CANVAS_UPDATE_REQUESTED;
297 if (item->flags & SP_CANVAS_ITEM_NEED_UPDATE)
298 child_flags |= SP_CANVAS_UPDATE_REQUESTED;
300 if (item->flags & SP_CANVAS_ITEM_NEED_AFFINE)
301 child_flags |= SP_CANVAS_UPDATE_AFFINE;
303 if (child_flags & (SP_CANVAS_UPDATE_REQUESTED | SP_CANVAS_UPDATE_AFFINE)) {
304 if (SP_CANVAS_ITEM_GET_CLASS (item)->update)
305 SP_CANVAS_ITEM_GET_CLASS (item)->update (item, child_affine, child_flags);
306 }
308 GTK_OBJECT_UNSET_FLAGS (item, SP_CANVAS_ITEM_NEED_UPDATE);
309 GTK_OBJECT_UNSET_FLAGS (item, SP_CANVAS_ITEM_NEED_AFFINE);
310 }
312 /**
313 * Helper function to invoke the point method of the item.
314 *
315 * The argument x, y should be in the parent's item-relative coordinate
316 * system. This routine applies the inverse of the item's transform,
317 * maintaining the affine invariant.
318 */
319 static double
320 sp_canvas_item_invoke_point (SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item)
321 {
322 if (SP_CANVAS_ITEM_GET_CLASS (item)->point)
323 return SP_CANVAS_ITEM_GET_CLASS (item)->point (item, p, actual_item);
325 return NR_HUGE;
326 }
328 /**
329 * Makes the item's affine transformation matrix be equal to the specified
330 * matrix.
331 *
332 * @item: A canvas item.
333 * @affine: An affine transformation matrix.
334 */
335 void
336 sp_canvas_item_affine_absolute (SPCanvasItem *item, Geom::Matrix const &affine)
337 {
338 item->xform = affine;
340 if (!(item->flags & SP_CANVAS_ITEM_NEED_AFFINE)) {
341 item->flags |= SP_CANVAS_ITEM_NEED_AFFINE;
342 if (item->parent != NULL) {
343 sp_canvas_item_request_update (item->parent);
344 } else {
345 sp_canvas_request_update (item->canvas);
346 }
347 }
349 item->canvas->need_repick = TRUE;
350 }
352 /**
353 * Convenience function to reorder items in a group's child list.
354 *
355 * This puts the specified link after the "before" link.
356 */
357 static void
358 put_item_after (GList *link, GList *before)
359 {
360 if (link == before)
361 return;
363 SPCanvasGroup *parent = SP_CANVAS_GROUP (SP_CANVAS_ITEM (link->data)->parent);
365 if (before == NULL) {
366 if (link == parent->items) return;
368 link->prev->next = link->next;
370 if (link->next) {
371 link->next->prev = link->prev;
372 } else {
373 parent->last = link->prev;
374 }
376 link->prev = before;
377 link->next = parent->items;
378 link->next->prev = link;
379 parent->items = link;
380 } else {
381 if ((link == parent->last) && (before == parent->last->prev))
382 return;
384 if (link->next)
385 link->next->prev = link->prev;
387 if (link->prev)
388 link->prev->next = link->next;
389 else {
390 parent->items = link->next;
391 parent->items->prev = NULL;
392 }
394 link->prev = before;
395 link->next = before->next;
397 link->prev->next = link;
399 if (link->next)
400 link->next->prev = link;
401 else
402 parent->last = link;
403 }
404 }
407 /**
408 * Raises the item in its parent's stack by the specified number of positions.
409 *
410 * \param item A canvas item.
411 * \param positions Number of steps to raise the item.
412 *
413 * If the number of positions is greater than the distance to the top of the
414 * stack, then the item is put at the top.
415 */
416 void
417 sp_canvas_item_raise (SPCanvasItem *item, int positions)
418 {
419 g_return_if_fail (item != NULL);
420 g_return_if_fail (SP_IS_CANVAS_ITEM (item));
421 g_return_if_fail (positions >= 0);
423 if (!item->parent || positions == 0)
424 return;
426 SPCanvasGroup *parent = SP_CANVAS_GROUP (item->parent);
427 GList *link = g_list_find (parent->items, item);
428 g_assert (link != NULL);
430 GList *before;
431 for (before = link; positions && before; positions--)
432 before = before->next;
434 if (!before)
435 before = parent->last;
437 put_item_after (link, before);
439 redraw_if_visible (item);
440 item->canvas->need_repick = TRUE;
441 }
444 /**
445 * Lowers the item in its parent's stack by the specified number of positions.
446 *
447 * \param item A canvas item.
448 * \param positions Number of steps to lower the item.
449 *
450 * If the number of positions is greater than the distance to the bottom of the
451 * stack, then the item is put at the bottom.
452 **/
453 void
454 sp_canvas_item_lower (SPCanvasItem *item, int positions)
455 {
456 g_return_if_fail (item != NULL);
457 g_return_if_fail (SP_IS_CANVAS_ITEM (item));
458 g_return_if_fail (positions >= 1);
460 if (!item->parent || positions == 0)
461 return;
463 SPCanvasGroup *parent = SP_CANVAS_GROUP (item->parent);
464 GList *link = g_list_find (parent->items, item);
465 g_assert (link != NULL);
467 GList *before;
468 if (link->prev)
469 for (before = link->prev; positions && before; positions--)
470 before = before->prev;
471 else
472 before = NULL;
474 put_item_after (link, before);
476 redraw_if_visible (item);
477 item->canvas->need_repick = TRUE;
478 }
480 bool
481 sp_canvas_item_is_visible (SPCanvasItem *item)
482 {
483 return item->flags & SP_CANVAS_ITEM_VISIBLE;
484 }
487 /**
488 * Sets visible flag on item and requests a redraw.
489 */
490 void
491 sp_canvas_item_show (SPCanvasItem *item)
492 {
493 g_return_if_fail (item != NULL);
494 g_return_if_fail (SP_IS_CANVAS_ITEM (item));
496 if (item->flags & SP_CANVAS_ITEM_VISIBLE)
497 return;
499 item->flags |= SP_CANVAS_ITEM_VISIBLE;
501 int x0 = (int)(item->x1);
502 int x1 = (int)(item->x2);
503 int y0 = (int)(item->y1);
504 int y1 = (int)(item->y2);
506 if (x0 !=0 || x1 !=0 || y0 !=0 || y1 !=0) {
507 sp_canvas_request_redraw (item->canvas, (int)(item->x1), (int)(item->y1), (int)(item->x2 + 1), (int)(item->y2 + 1));
508 item->canvas->need_repick = TRUE;
509 }
510 }
512 /**
513 * Clears visible flag on item and requests a redraw.
514 */
515 void
516 sp_canvas_item_hide (SPCanvasItem *item)
517 {
518 g_return_if_fail (item != NULL);
519 g_return_if_fail (SP_IS_CANVAS_ITEM (item));
521 if (!(item->flags & SP_CANVAS_ITEM_VISIBLE))
522 return;
524 item->flags &= ~SP_CANVAS_ITEM_VISIBLE;
526 int x0 = (int)(item->x1);
527 int x1 = (int)(item->x2);
528 int y0 = (int)(item->y1);
529 int y1 = (int)(item->y2);
531 if (x0 !=0 || x1 !=0 || y0 !=0 || y1 !=0) {
532 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)(item->x2 + 1), (int)(item->y2 + 1));
533 item->canvas->need_repick = TRUE;
534 }
535 }
537 /**
538 * Grab item under cursor.
539 *
540 * \pre !canvas->grabbed_item && item->flags & SP_CANVAS_ITEM_VISIBLE
541 */
542 int
543 sp_canvas_item_grab (SPCanvasItem *item, guint event_mask, GdkCursor *cursor, guint32 etime)
544 {
545 g_return_val_if_fail (item != NULL, -1);
546 g_return_val_if_fail (SP_IS_CANVAS_ITEM (item), -1);
547 g_return_val_if_fail (GTK_WIDGET_MAPPED (item->canvas), -1);
549 if (item->canvas->grabbed_item)
550 return -1;
552 // This test disallows grabbing events by an invisible item, which may be useful
553 // sometimes. An example is the hidden control point used for the selector component,
554 // where it is used for object selection and rubberbanding. There seems to be nothing
555 // preventing this except this test, so I removed it.
556 // -- Krzysztof Kosiński, 2009.08.12
557 //if (!(item->flags & SP_CANVAS_ITEM_VISIBLE))
558 // return -1;
560 if (HAS_BROKEN_MOTION_HINTS) {
561 event_mask &= ~GDK_POINTER_MOTION_HINT_MASK;
562 }
564 /* fixme: Top hack (Lauris) */
565 /* fixme: If we add key masks to event mask, Gdk will abort (Lauris) */
566 /* fixme: But Canvas actualle does get key events, so all we need is routing these here */
567 gdk_pointer_grab (SP_CANVAS_WINDOW (item->canvas), FALSE,
568 (GdkEventMask)(event_mask & (~(GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK))),
569 NULL, cursor, etime);
571 item->canvas->grabbed_item = item;
572 item->canvas->grabbed_event_mask = event_mask;
573 item->canvas->current_item = item; /* So that events go to the grabbed item */
575 return 0;
576 }
578 /**
579 * Ungrabs the item, which must have been grabbed in the canvas, and ungrabs the
580 * mouse.
581 *
582 * \param item A canvas item that holds a grab.
583 * \param etime The timestamp for ungrabbing the mouse.
584 */
585 void
586 sp_canvas_item_ungrab (SPCanvasItem *item, guint32 etime)
587 {
588 g_return_if_fail (item != NULL);
589 g_return_if_fail (SP_IS_CANVAS_ITEM (item));
591 if (item->canvas->grabbed_item != item)
592 return;
594 item->canvas->grabbed_item = NULL;
596 gdk_pointer_ungrab (etime);
597 }
599 /**
600 * Returns the product of all transformation matrices from the root item down
601 * to the item.
602 */
603 Geom::Matrix sp_canvas_item_i2w_affine(SPCanvasItem const *item)
604 {
605 g_assert (SP_IS_CANVAS_ITEM (item)); // should we get this?
607 Geom::Matrix affine = Geom::identity();
609 while (item) {
610 affine *= item->xform;
611 item = item->parent;
612 }
613 return affine;
614 }
616 /**
617 * Helper that returns true iff item is descendant of parent.
618 */
619 static bool is_descendant(SPCanvasItem const *item, SPCanvasItem const *parent)
620 {
621 while (item) {
622 if (item == parent)
623 return true;
624 item = item->parent;
625 }
627 return false;
628 }
630 /**
631 * Focus canvas, and item under cursor if it is not already focussed.
632 */
633 void
634 sp_canvas_item_grab_focus (SPCanvasItem *item)
635 {
636 g_return_if_fail (item != NULL);
637 g_return_if_fail (SP_IS_CANVAS_ITEM (item));
638 g_return_if_fail (GTK_WIDGET_CAN_FOCUS (GTK_WIDGET (item->canvas)));
640 SPCanvasItem *focused_item = item->canvas->focused_item;
642 if (focused_item) {
643 GdkEvent ev;
644 ev.focus_change.type = GDK_FOCUS_CHANGE;
645 ev.focus_change.window = SP_CANVAS_WINDOW (item->canvas);
646 ev.focus_change.send_event = FALSE;
647 ev.focus_change.in = FALSE;
649 emit_event (item->canvas, &ev);
650 }
652 item->canvas->focused_item = item;
653 gtk_widget_grab_focus (GTK_WIDGET (item->canvas));
655 if (focused_item) {
656 GdkEvent ev;
657 ev.focus_change.type = GDK_FOCUS_CHANGE;
658 ev.focus_change.window = SP_CANVAS_WINDOW (item->canvas);
659 ev.focus_change.send_event = FALSE;
660 ev.focus_change.in = TRUE;
662 emit_event (item->canvas, &ev);
663 }
664 }
666 /**
667 * Requests that the canvas queue an update for the specified item.
668 *
669 * To be used only by item implementations.
670 */
671 void
672 sp_canvas_item_request_update (SPCanvasItem *item)
673 {
674 if (item->flags & SP_CANVAS_ITEM_NEED_UPDATE)
675 return;
677 item->flags |= SP_CANVAS_ITEM_NEED_UPDATE;
679 if (item->parent != NULL) {
680 /* Recurse up the tree */
681 sp_canvas_item_request_update (item->parent);
682 } else {
683 /* Have reached the top of the tree, make sure the update call gets scheduled. */
684 sp_canvas_request_update (item->canvas);
685 }
686 }
688 /**
689 * Returns position of item in group.
690 */
691 gint sp_canvas_item_order (SPCanvasItem * item)
692 {
693 return g_list_index (SP_CANVAS_GROUP (item->parent)->items, item);
694 }
696 /* SPCanvasGroup */
698 static void sp_canvas_group_class_init (SPCanvasGroupClass *klass);
699 static void sp_canvas_group_init (SPCanvasGroup *group);
700 static void sp_canvas_group_destroy (GtkObject *object);
702 static void sp_canvas_group_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags);
703 static double sp_canvas_group_point (SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item);
704 static void sp_canvas_group_render (SPCanvasItem *item, SPCanvasBuf *buf);
706 static SPCanvasItemClass *group_parent_class;
708 /**
709 * Registers SPCanvasGroup class with Gtk and returns its type number.
710 */
711 GType sp_canvas_group_get_type(void)
712 {
713 static GType type = 0;
714 if (!type) {
715 GTypeInfo info = {
716 sizeof(SPCanvasGroupClass),
717 0, // base_init
718 0, // base_finalize
719 (GClassInitFunc)sp_canvas_group_class_init,
720 0, // class_finalize
721 0, // class_data
722 sizeof(SPCanvasGroup),
723 0, // n_preallocs
724 (GInstanceInitFunc)sp_canvas_group_init,
725 0 // value_table
726 };
727 type = g_type_register_static(sp_canvas_item_get_type(), "SPCanvasGroup", &info, static_cast<GTypeFlags>(0));
728 }
729 return type;
730 }
732 /**
733 * Class initialization function for SPCanvasGroupClass
734 */
735 static void
736 sp_canvas_group_class_init (SPCanvasGroupClass *klass)
737 {
738 GtkObjectClass *object_class = (GtkObjectClass *) klass;
739 SPCanvasItemClass *item_class = (SPCanvasItemClass *) klass;
741 group_parent_class = (SPCanvasItemClass*)gtk_type_class (sp_canvas_item_get_type ());
743 object_class->destroy = sp_canvas_group_destroy;
745 item_class->update = sp_canvas_group_update;
746 item_class->render = sp_canvas_group_render;
747 item_class->point = sp_canvas_group_point;
748 }
750 /**
751 * Callback. Empty.
752 */
753 static void
754 sp_canvas_group_init (SPCanvasGroup */*group*/)
755 {
756 /* Nothing here */
757 }
759 /**
760 * Callback that destroys all items in group and calls group's virtual
761 * destroy() function.
762 */
763 static void
764 sp_canvas_group_destroy (GtkObject *object)
765 {
766 g_return_if_fail (object != NULL);
767 g_return_if_fail (SP_IS_CANVAS_GROUP (object));
769 SPCanvasGroup const *group = SP_CANVAS_GROUP (object);
771 GList *list = group->items;
772 while (list) {
773 SPCanvasItem *child = (SPCanvasItem *)list->data;
774 list = list->next;
776 gtk_object_destroy (GTK_OBJECT (child));
777 }
779 if (GTK_OBJECT_CLASS (group_parent_class)->destroy)
780 (* GTK_OBJECT_CLASS (group_parent_class)->destroy) (object);
781 }
783 /**
784 * Update handler for canvas groups
785 */
786 static void
787 sp_canvas_group_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
788 {
789 SPCanvasGroup const *group = SP_CANVAS_GROUP (item);
790 Geom::RectHull corners(Geom::Point(0, 0));
791 bool empty=true;
793 for (GList *list = group->items; list; list = list->next) {
794 SPCanvasItem *i = (SPCanvasItem *)list->data;
796 sp_canvas_item_invoke_update (i, affine, flags);
798 if ( i->x2 > i->x1 && i->y2 > i->y1 ) {
799 if (empty) {
800 corners = Geom::RectHull(Geom::Point(i->x1, i->y1));
801 empty = false;
802 } else {
803 corners.add(Geom::Point(i->x1, i->y1));
804 }
805 corners.add(Geom::Point(i->x2, i->y2));
806 }
807 }
809 Geom::OptRect const bounds = corners.bounds();
810 if (bounds) {
811 item->x1 = bounds->min()[Geom::X];
812 item->y1 = bounds->min()[Geom::Y];
813 item->x2 = bounds->max()[Geom::X];
814 item->y2 = bounds->max()[Geom::Y];
815 } else {
816 // FIXME ?
817 item->x1 = item->x2 = item->y1 = item->y2 = 0;
818 }
819 }
821 /**
822 * Point handler for canvas groups.
823 */
824 static double
825 sp_canvas_group_point (SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item)
826 {
827 SPCanvasGroup const *group = SP_CANVAS_GROUP (item);
828 double const x = p[Geom::X];
829 double const y = p[Geom::Y];
830 int x1 = (int)(x - item->canvas->close_enough);
831 int y1 = (int)(y - item->canvas->close_enough);
832 int x2 = (int)(x + item->canvas->close_enough);
833 int y2 = (int)(y + item->canvas->close_enough);
835 double best = 0.0;
836 *actual_item = NULL;
838 double dist = 0.0;
840 for (GList *list = group->items; list; list = list->next) {
841 SPCanvasItem *child = (SPCanvasItem *)list->data;
843 if ((child->x1 <= x2) && (child->y1 <= y2) && (child->x2 >= x1) && (child->y2 >= y1)) {
844 SPCanvasItem *point_item = NULL; /* cater for incomplete item implementations */
846 int has_point;
847 if ((child->flags & SP_CANVAS_ITEM_VISIBLE) && SP_CANVAS_ITEM_GET_CLASS (child)->point) {
848 dist = sp_canvas_item_invoke_point (child, p, &point_item);
849 has_point = TRUE;
850 } else
851 has_point = FALSE;
853 if (has_point && point_item && ((int) (dist + 0.5) <= item->canvas->close_enough)) {
854 best = dist;
855 *actual_item = point_item;
856 }
857 }
858 }
860 return best;
861 }
863 /**
864 * Renders all visible canvas group items in buf rectangle.
865 */
866 static void
867 sp_canvas_group_render (SPCanvasItem *item, SPCanvasBuf *buf)
868 {
869 SPCanvasGroup const *group = SP_CANVAS_GROUP (item);
871 for (GList *list = group->items; list; list = list->next) {
872 SPCanvasItem *child = (SPCanvasItem *)list->data;
873 if (child->flags & SP_CANVAS_ITEM_VISIBLE) {
874 if ((child->x1 < buf->rect.x1) &&
875 (child->y1 < buf->rect.y1) &&
876 (child->x2 > buf->rect.x0) &&
877 (child->y2 > buf->rect.y0)) {
878 if (SP_CANVAS_ITEM_GET_CLASS (child)->render)
879 SP_CANVAS_ITEM_GET_CLASS (child)->render (child, buf);
880 }
881 }
882 }
883 }
885 /**
886 * Adds an item to a canvas group.
887 */
888 static void
889 group_add (SPCanvasGroup *group, SPCanvasItem *item)
890 {
891 gtk_object_ref (GTK_OBJECT (item));
892 gtk_object_sink (GTK_OBJECT (item));
894 if (!group->items) {
895 group->items = g_list_append (group->items, item);
896 group->last = group->items;
897 } else {
898 group->last = g_list_append (group->last, item)->next;
899 }
901 sp_canvas_item_request_update (item);
902 }
904 /**
905 * Removes an item from a canvas group
906 */
907 static void
908 group_remove (SPCanvasGroup *group, SPCanvasItem *item)
909 {
910 g_return_if_fail (group != NULL);
911 g_return_if_fail (SP_IS_CANVAS_GROUP (group));
912 g_return_if_fail (item != NULL);
914 for (GList *children = group->items; children; children = children->next) {
915 if (children->data == item) {
917 /* Unparent the child */
919 item->parent = NULL;
920 gtk_object_unref (GTK_OBJECT (item));
922 /* Remove it from the list */
924 if (children == group->last) group->last = children->prev;
926 group->items = g_list_remove_link (group->items, children);
927 g_list_free (children);
928 break;
929 }
930 }
931 }
933 /* SPCanvas */
935 static void sp_canvas_class_init (SPCanvasClass *klass);
936 static void sp_canvas_init (SPCanvas *canvas);
937 static void sp_canvas_destroy (GtkObject *object);
939 static void sp_canvas_realize (GtkWidget *widget);
940 static void sp_canvas_unrealize (GtkWidget *widget);
942 static void sp_canvas_size_request (GtkWidget *widget, GtkRequisition *req);
943 static void sp_canvas_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
945 static gint sp_canvas_button (GtkWidget *widget, GdkEventButton *event);
946 static gint sp_canvas_scroll (GtkWidget *widget, GdkEventScroll *event);
947 static gint sp_canvas_motion (GtkWidget *widget, GdkEventMotion *event);
948 static gint sp_canvas_expose (GtkWidget *widget, GdkEventExpose *event);
949 static gint sp_canvas_key (GtkWidget *widget, GdkEventKey *event);
950 static gint sp_canvas_crossing (GtkWidget *widget, GdkEventCrossing *event);
951 static gint sp_canvas_focus_in (GtkWidget *widget, GdkEventFocus *event);
952 static gint sp_canvas_focus_out (GtkWidget *widget, GdkEventFocus *event);
954 static GtkWidgetClass *canvas_parent_class;
956 static void sp_canvas_resize_tiles(SPCanvas* canvas, int nl, int nt, int nr, int nb);
957 static void sp_canvas_dirty_rect(SPCanvas* canvas, int nl, int nt, int nr, int nb);
958 static void sp_canvas_mark_rect(SPCanvas* canvas, int nl, int nt, int nr, int nb, uint8_t val);
959 static int do_update (SPCanvas *canvas);
961 /**
962 * Registers the SPCanvas class if necessary, and returns the type ID
963 * associated to it.
964 *
965 * \return The type ID of the SPCanvas class.
966 **/
967 GType sp_canvas_get_type(void)
968 {
969 static GType type = 0;
970 if (!type) {
971 GTypeInfo info = {
972 sizeof(SPCanvasClass),
973 0, // base_init
974 0, // base_finalize
975 (GClassInitFunc)sp_canvas_class_init,
976 0, // class_finalize
977 0, // class_data
978 sizeof(SPCanvas),
979 0, // n_preallocs
980 (GInstanceInitFunc)sp_canvas_init,
981 0 // value_table
982 };
983 type = g_type_register_static(GTK_TYPE_WIDGET, "SPCanvas", &info, static_cast<GTypeFlags>(0));
984 }
985 return type;
986 }
988 /**
989 * Class initialization function for SPCanvasClass.
990 */
991 static void
992 sp_canvas_class_init (SPCanvasClass *klass)
993 {
994 GtkObjectClass *object_class = (GtkObjectClass *) klass;
995 GtkWidgetClass *widget_class = (GtkWidgetClass *) klass;
997 canvas_parent_class = (GtkWidgetClass *)gtk_type_class (GTK_TYPE_WIDGET);
999 object_class->destroy = sp_canvas_destroy;
1001 widget_class->realize = sp_canvas_realize;
1002 widget_class->unrealize = sp_canvas_unrealize;
1003 widget_class->size_request = sp_canvas_size_request;
1004 widget_class->size_allocate = sp_canvas_size_allocate;
1005 widget_class->button_press_event = sp_canvas_button;
1006 widget_class->button_release_event = sp_canvas_button;
1007 widget_class->motion_notify_event = sp_canvas_motion;
1008 widget_class->scroll_event = sp_canvas_scroll;
1009 widget_class->expose_event = sp_canvas_expose;
1010 widget_class->key_press_event = sp_canvas_key;
1011 widget_class->key_release_event = sp_canvas_key;
1012 widget_class->enter_notify_event = sp_canvas_crossing;
1013 widget_class->leave_notify_event = sp_canvas_crossing;
1014 widget_class->focus_in_event = sp_canvas_focus_in;
1015 widget_class->focus_out_event = sp_canvas_focus_out;
1016 }
1018 /**
1019 * Callback: object initialization for SPCanvas.
1020 */
1021 static void
1022 sp_canvas_init (SPCanvas *canvas)
1023 {
1024 GTK_WIDGET_UNSET_FLAGS (canvas, GTK_NO_WINDOW);
1025 GTK_WIDGET_UNSET_FLAGS (canvas, GTK_DOUBLE_BUFFERED);
1026 GTK_WIDGET_SET_FLAGS (canvas, GTK_CAN_FOCUS);
1028 canvas->pick_event.type = GDK_LEAVE_NOTIFY;
1029 canvas->pick_event.crossing.x = 0;
1030 canvas->pick_event.crossing.y = 0;
1032 /* Create the root item as a special case */
1033 canvas->root = SP_CANVAS_ITEM (gtk_type_new (sp_canvas_group_get_type ()));
1034 canvas->root->canvas = canvas;
1036 gtk_object_ref (GTK_OBJECT (canvas->root));
1037 gtk_object_sink (GTK_OBJECT (canvas->root));
1039 canvas->need_repick = TRUE;
1041 // See comment at in sp-canvas.h.
1042 canvas->gen_all_enter_events = false;
1044 canvas->tiles=NULL;
1045 canvas->tLeft=canvas->tTop=canvas->tRight=canvas->tBottom=0;
1046 canvas->tileH=canvas->tileV=0;
1048 canvas->forced_redraw_count = 0;
1049 canvas->forced_redraw_limit = -1;
1051 #if ENABLE_LCMS
1052 canvas->enable_cms_display_adj = false;
1053 canvas->cms_key = new Glib::ustring("");
1054 #endif // ENABLE_LCMS
1056 canvas->is_scrolling = false;
1057 }
1059 /**
1060 * Convenience function to remove the idle handler of a canvas.
1061 */
1062 static void
1063 remove_idle (SPCanvas *canvas)
1064 {
1065 if (canvas->idle_id) {
1066 gtk_idle_remove (canvas->idle_id);
1067 canvas->idle_id = 0;
1068 }
1069 }
1071 /*
1072 * Removes the transient state of the canvas (idle handler, grabs).
1073 */
1074 static void
1075 shutdown_transients (SPCanvas *canvas)
1076 {
1077 /* We turn off the need_redraw flag, since if the canvas is mapped again
1078 * it will request a redraw anyways. We do not turn off the need_update
1079 * flag, though, because updates are not queued when the canvas remaps
1080 * itself.
1081 */
1082 if (canvas->need_redraw) {
1083 canvas->need_redraw = FALSE;
1084 }
1085 if ( canvas->tiles ) g_free(canvas->tiles);
1086 canvas->tiles=NULL;
1087 canvas->tLeft=canvas->tTop=canvas->tRight=canvas->tBottom=0;
1088 canvas->tileH=canvas->tileV=0;
1090 if (canvas->grabbed_item) {
1091 canvas->grabbed_item = NULL;
1092 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1093 }
1095 remove_idle (canvas);
1096 }
1098 /**
1099 * Destroy handler for SPCanvas.
1100 */
1101 static void
1102 sp_canvas_destroy (GtkObject *object)
1103 {
1104 SPCanvas *canvas = SP_CANVAS (object);
1106 if (canvas->root) {
1107 gtk_object_unref (GTK_OBJECT (canvas->root));
1108 canvas->root = NULL;
1109 }
1111 shutdown_transients (canvas);
1113 if (GTK_OBJECT_CLASS (canvas_parent_class)->destroy)
1114 (* GTK_OBJECT_CLASS (canvas_parent_class)->destroy) (object);
1115 }
1117 static void track_latency(GdkEvent const *event) {
1118 GdkEventLatencyTracker &tracker = GdkEventLatencyTracker::default_tracker();
1119 boost::optional<double> latency = tracker.process(event);
1120 if (latency && *latency > 2.0) {
1121 //g_warning("Event latency reached %f sec (%1.4f)", *latency, tracker.getSkew());
1122 }
1123 }
1125 /**
1126 * Returns new canvas as widget.
1127 */
1128 GtkWidget *
1129 sp_canvas_new_aa (void)
1130 {
1131 SPCanvas *canvas = (SPCanvas *)gtk_type_new (sp_canvas_get_type ());
1133 return (GtkWidget *) canvas;
1134 }
1136 /**
1137 * The canvas widget's realize callback.
1138 */
1139 static void
1140 sp_canvas_realize (GtkWidget *widget)
1141 {
1142 SPCanvas *canvas = SP_CANVAS (widget);
1144 GdkWindowAttr attributes;
1145 attributes.window_type = GDK_WINDOW_CHILD;
1146 attributes.x = widget->allocation.x;
1147 attributes.y = widget->allocation.y;
1148 attributes.width = widget->allocation.width;
1149 attributes.height = widget->allocation.height;
1150 attributes.wclass = GDK_INPUT_OUTPUT;
1151 attributes.visual = gdk_rgb_get_visual ();
1152 attributes.colormap = gdk_rgb_get_cmap ();
1153 attributes.event_mask = (gtk_widget_get_events (widget) |
1154 GDK_EXPOSURE_MASK |
1155 GDK_BUTTON_PRESS_MASK |
1156 GDK_BUTTON_RELEASE_MASK |
1157 GDK_POINTER_MOTION_MASK |
1158 ( HAS_BROKEN_MOTION_HINTS ?
1159 0 : GDK_POINTER_MOTION_HINT_MASK ) |
1160 GDK_PROXIMITY_IN_MASK |
1161 GDK_PROXIMITY_OUT_MASK |
1162 GDK_KEY_PRESS_MASK |
1163 GDK_KEY_RELEASE_MASK |
1164 GDK_ENTER_NOTIFY_MASK |
1165 GDK_LEAVE_NOTIFY_MASK |
1166 GDK_FOCUS_CHANGE_MASK);
1167 gint attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1169 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1170 gdk_window_set_user_data (widget->window, widget);
1172 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1173 if ( prefs->getBool("/options/useextinput/value", true) )
1174 gtk_widget_set_events(widget, attributes.event_mask);
1176 widget->style = gtk_style_attach (widget->style, widget->window);
1178 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1180 canvas->pixmap_gc = gdk_gc_new (SP_CANVAS_WINDOW (canvas));
1181 }
1183 /**
1184 * The canvas widget's unrealize callback.
1185 */
1186 static void
1187 sp_canvas_unrealize (GtkWidget *widget)
1188 {
1189 SPCanvas *canvas = SP_CANVAS (widget);
1191 canvas->current_item = NULL;
1192 canvas->grabbed_item = NULL;
1193 canvas->focused_item = NULL;
1195 shutdown_transients (canvas);
1197 gdk_gc_destroy (canvas->pixmap_gc);
1198 canvas->pixmap_gc = NULL;
1200 if (GTK_WIDGET_CLASS (canvas_parent_class)->unrealize)
1201 (* GTK_WIDGET_CLASS (canvas_parent_class)->unrealize) (widget);
1202 }
1204 /**
1205 * The canvas widget's size_request callback.
1206 */
1207 static void
1208 sp_canvas_size_request (GtkWidget *widget, GtkRequisition *req)
1209 {
1210 static_cast<void>(SP_CANVAS (widget));
1212 req->width = 256;
1213 req->height = 256;
1214 }
1216 /**
1217 * The canvas widget's size_allocate callback.
1218 */
1219 static void
1220 sp_canvas_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
1221 {
1222 SPCanvas *canvas = SP_CANVAS (widget);
1224 /* Schedule redraw of new region */
1225 sp_canvas_resize_tiles(canvas,canvas->x0,canvas->y0,canvas->x0+allocation->width,canvas->y0+allocation->height);
1226 if (allocation->width > widget->allocation.width) {
1227 sp_canvas_request_redraw (canvas,
1228 canvas->x0 + widget->allocation.width,
1229 0,
1230 canvas->x0 + allocation->width,
1231 canvas->y0 + allocation->height);
1232 }
1233 if (allocation->height > widget->allocation.height) {
1234 sp_canvas_request_redraw (canvas,
1235 0,
1236 canvas->y0 + widget->allocation.height,
1237 canvas->x0 + allocation->width,
1238 canvas->y0 + allocation->height);
1239 }
1241 widget->allocation = *allocation;
1243 if (GTK_WIDGET_REALIZED (widget)) {
1244 gdk_window_move_resize (widget->window,
1245 widget->allocation.x, widget->allocation.y,
1246 widget->allocation.width, widget->allocation.height);
1247 }
1248 }
1250 /**
1251 * Helper that emits an event for an item in the canvas, be it the current
1252 * item, grabbed item, or focused item, as appropriate.
1253 */
1254 static int
1255 emit_event (SPCanvas *canvas, GdkEvent *event)
1256 {
1257 guint mask;
1259 if (canvas->grabbed_item) {
1260 switch (event->type) {
1261 case GDK_ENTER_NOTIFY:
1262 mask = GDK_ENTER_NOTIFY_MASK;
1263 break;
1264 case GDK_LEAVE_NOTIFY:
1265 mask = GDK_LEAVE_NOTIFY_MASK;
1266 break;
1267 case GDK_MOTION_NOTIFY:
1268 mask = GDK_POINTER_MOTION_MASK;
1269 break;
1270 case GDK_BUTTON_PRESS:
1271 case GDK_2BUTTON_PRESS:
1272 case GDK_3BUTTON_PRESS:
1273 mask = GDK_BUTTON_PRESS_MASK;
1274 break;
1275 case GDK_BUTTON_RELEASE:
1276 mask = GDK_BUTTON_RELEASE_MASK;
1277 break;
1278 case GDK_KEY_PRESS:
1279 mask = GDK_KEY_PRESS_MASK;
1280 break;
1281 case GDK_KEY_RELEASE:
1282 mask = GDK_KEY_RELEASE_MASK;
1283 break;
1284 case GDK_SCROLL:
1285 mask = GDK_SCROLL;
1286 break;
1287 default:
1288 mask = 0;
1289 break;
1290 }
1292 if (!(mask & canvas->grabbed_event_mask)) return FALSE;
1293 }
1295 /* Convert to world coordinates -- we have two cases because of different
1296 * offsets of the fields in the event structures.
1297 */
1299 GdkEvent ev = *event;
1301 switch (ev.type) {
1302 case GDK_ENTER_NOTIFY:
1303 case GDK_LEAVE_NOTIFY:
1304 ev.crossing.x += canvas->x0;
1305 ev.crossing.y += canvas->y0;
1306 break;
1307 case GDK_MOTION_NOTIFY:
1308 case GDK_BUTTON_PRESS:
1309 case GDK_2BUTTON_PRESS:
1310 case GDK_3BUTTON_PRESS:
1311 case GDK_BUTTON_RELEASE:
1312 ev.motion.x += canvas->x0;
1313 ev.motion.y += canvas->y0;
1314 break;
1315 default:
1316 break;
1317 }
1319 /* Choose where we send the event */
1321 /* canvas->current_item becomes NULL in some cases under Win32
1322 ** (e.g. if the pointer leaves the window). So this is a hack that
1323 ** Lauris applied to SP to get around the problem.
1324 */
1325 SPCanvasItem* item = NULL;
1326 if (canvas->grabbed_item && !is_descendant (canvas->current_item, canvas->grabbed_item)) {
1327 item = canvas->grabbed_item;
1328 } else {
1329 item = canvas->current_item;
1330 }
1332 if (canvas->focused_item &&
1333 ((event->type == GDK_KEY_PRESS) ||
1334 (event->type == GDK_KEY_RELEASE) ||
1335 (event->type == GDK_FOCUS_CHANGE))) {
1336 item = canvas->focused_item;
1337 }
1339 /* The event is propagated up the hierarchy (for if someone connected to
1340 * a group instead of a leaf event), and emission is stopped if a
1341 * handler returns TRUE, just like for GtkWidget events.
1342 */
1344 gint finished = FALSE;
1346 while (item && !finished) {
1347 gtk_object_ref (GTK_OBJECT (item));
1348 gtk_signal_emit (GTK_OBJECT (item), item_signals[ITEM_EVENT], &ev, &finished);
1349 SPCanvasItem *parent = item->parent;
1350 gtk_object_unref (GTK_OBJECT (item));
1351 item = parent;
1352 }
1354 return finished;
1355 }
1357 /**
1358 * Helper that re-picks the current item in the canvas, based on the event's
1359 * coordinates and emits enter/leave events for items as appropriate.
1360 */
1361 static int
1362 pick_current_item (SPCanvas *canvas, GdkEvent *event)
1363 {
1364 int button_down = 0;
1365 double x, y;
1367 if (!canvas->root) // canvas may have already be destroyed by closing desktop durring interrupted display!
1368 return FALSE;
1370 int retval = FALSE;
1372 if (canvas->gen_all_enter_events == false) {
1373 // If a button is down, we'll perform enter and leave events on the
1374 // current item, but not enter on any other item. This is more or
1375 // less like X pointer grabbing for canvas items.
1376 //
1377 button_down = canvas->state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1378 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK | GDK_BUTTON5_MASK);
1380 if (!button_down) canvas->left_grabbed_item = FALSE;
1381 }
1383 /* Save the event in the canvas. This is used to synthesize enter and
1384 * leave events in case the current item changes. It is also used to
1385 * re-pick the current item if the current one gets deleted. Also,
1386 * synthesize an enter event.
1387 */
1388 if (event != &canvas->pick_event) {
1389 if ((event->type == GDK_MOTION_NOTIFY) || (event->type == GDK_BUTTON_RELEASE)) {
1390 /* these fields have the same offsets in both types of events */
1392 canvas->pick_event.crossing.type = GDK_ENTER_NOTIFY;
1393 canvas->pick_event.crossing.window = event->motion.window;
1394 canvas->pick_event.crossing.send_event = event->motion.send_event;
1395 canvas->pick_event.crossing.subwindow = NULL;
1396 canvas->pick_event.crossing.x = event->motion.x;
1397 canvas->pick_event.crossing.y = event->motion.y;
1398 canvas->pick_event.crossing.mode = GDK_CROSSING_NORMAL;
1399 canvas->pick_event.crossing.detail = GDK_NOTIFY_NONLINEAR;
1400 canvas->pick_event.crossing.focus = FALSE;
1401 canvas->pick_event.crossing.state = event->motion.state;
1403 /* these fields don't have the same offsets in both types of events */
1405 if (event->type == GDK_MOTION_NOTIFY) {
1406 canvas->pick_event.crossing.x_root = event->motion.x_root;
1407 canvas->pick_event.crossing.y_root = event->motion.y_root;
1408 } else {
1409 canvas->pick_event.crossing.x_root = event->button.x_root;
1410 canvas->pick_event.crossing.y_root = event->button.y_root;
1411 }
1412 } else {
1413 canvas->pick_event = *event;
1414 }
1415 }
1417 /* Don't do anything else if this is a recursive call */
1418 if (canvas->in_repick) return retval;
1420 /* LeaveNotify means that there is no current item, so we don't look for one */
1421 if (canvas->pick_event.type != GDK_LEAVE_NOTIFY) {
1422 /* these fields don't have the same offsets in both types of events */
1424 if (canvas->pick_event.type == GDK_ENTER_NOTIFY) {
1425 x = canvas->pick_event.crossing.x;
1426 y = canvas->pick_event.crossing.y;
1427 } else {
1428 x = canvas->pick_event.motion.x;
1429 y = canvas->pick_event.motion.y;
1430 }
1432 /* world coords */
1433 x += canvas->x0;
1434 y += canvas->y0;
1436 /* find the closest item */
1437 if (canvas->root->flags & SP_CANVAS_ITEM_VISIBLE) {
1438 sp_canvas_item_invoke_point (canvas->root, Geom::Point(x, y), &canvas->new_current_item);
1439 } else {
1440 canvas->new_current_item = NULL;
1441 }
1442 } else {
1443 canvas->new_current_item = NULL;
1444 }
1446 if ((canvas->new_current_item == canvas->current_item) && !canvas->left_grabbed_item) {
1447 return retval; /* current item did not change */
1448 }
1450 /* Synthesize events for old and new current items */
1452 if ((canvas->new_current_item != canvas->current_item)
1453 && (canvas->current_item != NULL)
1454 && !canvas->left_grabbed_item) {
1455 GdkEvent new_event;
1456 SPCanvasItem *item;
1458 item = canvas->current_item;
1460 new_event = canvas->pick_event;
1461 new_event.type = GDK_LEAVE_NOTIFY;
1463 new_event.crossing.detail = GDK_NOTIFY_ANCESTOR;
1464 new_event.crossing.subwindow = NULL;
1465 canvas->in_repick = TRUE;
1466 retval = emit_event (canvas, &new_event);
1467 canvas->in_repick = FALSE;
1468 }
1470 if (canvas->gen_all_enter_events == false) {
1471 // new_current_item may have been set to NULL during the call to
1472 // emit_event() above
1473 if ((canvas->new_current_item != canvas->current_item) && button_down) {
1474 canvas->left_grabbed_item = TRUE;
1475 return retval;
1476 }
1477 }
1479 /* Handle the rest of cases */
1481 canvas->left_grabbed_item = FALSE;
1482 canvas->current_item = canvas->new_current_item;
1484 if (canvas->current_item != NULL) {
1485 GdkEvent new_event;
1487 new_event = canvas->pick_event;
1488 new_event.type = GDK_ENTER_NOTIFY;
1489 new_event.crossing.detail = GDK_NOTIFY_ANCESTOR;
1490 new_event.crossing.subwindow = NULL;
1491 retval = emit_event (canvas, &new_event);
1492 }
1494 return retval;
1495 }
1497 /**
1498 * Button event handler for the canvas.
1499 */
1500 static gint
1501 sp_canvas_button (GtkWidget *widget, GdkEventButton *event)
1502 {
1503 SPCanvas *canvas = SP_CANVAS (widget);
1505 int retval = FALSE;
1507 /* dispatch normally regardless of the event's window if an item
1508 has a pointer grab in effect */
1509 if (!canvas->grabbed_item &&
1510 event->window != SP_CANVAS_WINDOW (canvas))
1511 return retval;
1513 int mask;
1514 switch (event->button) {
1515 case 1:
1516 mask = GDK_BUTTON1_MASK;
1517 break;
1518 case 2:
1519 mask = GDK_BUTTON2_MASK;
1520 break;
1521 case 3:
1522 mask = GDK_BUTTON3_MASK;
1523 break;
1524 case 4:
1525 mask = GDK_BUTTON4_MASK;
1526 break;
1527 case 5:
1528 mask = GDK_BUTTON5_MASK;
1529 break;
1530 default:
1531 mask = 0;
1532 }
1534 switch (event->type) {
1535 case GDK_BUTTON_PRESS:
1536 case GDK_2BUTTON_PRESS:
1537 case GDK_3BUTTON_PRESS:
1538 /* Pick the current item as if the button were not pressed, and
1539 * then process the event.
1540 */
1541 canvas->state = event->state;
1542 pick_current_item (canvas, (GdkEvent *) event);
1543 canvas->state ^= mask;
1544 retval = emit_event (canvas, (GdkEvent *) event);
1545 break;
1547 case GDK_BUTTON_RELEASE:
1548 /* Process the event as if the button were pressed, then repick
1549 * after the button has been released
1550 */
1551 canvas->state = event->state;
1552 retval = emit_event (canvas, (GdkEvent *) event);
1553 event->state ^= mask;
1554 canvas->state = event->state;
1555 pick_current_item (canvas, (GdkEvent *) event);
1556 event->state ^= mask;
1558 break;
1560 default:
1561 g_assert_not_reached ();
1562 }
1564 return retval;
1565 }
1567 /**
1568 * Scroll event handler for the canvas.
1569 *
1570 * \todo FIXME: generate motion events to re-select items.
1571 */
1572 static gint
1573 sp_canvas_scroll (GtkWidget *widget, GdkEventScroll *event)
1574 {
1575 return emit_event (SP_CANVAS (widget), (GdkEvent *) event);
1576 }
1578 static inline void request_motions(GdkWindow *w, GdkEventMotion *event) {
1579 gdk_window_get_pointer(w, NULL, NULL, NULL);
1580 #if HAS_GDK_EVENT_REQUEST_MOTIONS
1581 gdk_event_request_motions(event);
1582 #endif
1583 }
1585 /**
1586 * Motion event handler for the canvas.
1587 */
1588 static int
1589 sp_canvas_motion (GtkWidget *widget, GdkEventMotion *event)
1590 {
1591 int status;
1592 SPCanvas *canvas = SP_CANVAS (widget);
1594 track_latency((GdkEvent *)event);
1596 if (event->window != SP_CANVAS_WINDOW (canvas))
1597 return FALSE;
1599 if (canvas->pixmap_gc == NULL) // canvas being deleted
1600 return FALSE;
1602 canvas->state = event->state;
1603 pick_current_item (canvas, (GdkEvent *) event);
1604 status = emit_event (canvas, (GdkEvent *) event);
1605 if (event->is_hint) {
1606 request_motions(widget->window, event);
1607 }
1609 return status;
1610 }
1612 static void
1613 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)
1614 {
1615 GtkWidget *widget = GTK_WIDGET (canvas);
1617 SPCanvasBuf buf;
1618 if (canvas->rendermode != Inkscape::RENDERMODE_OUTLINE) {
1619 buf.buf = nr_pixelstore_256K_new (FALSE, 0);
1620 } else {
1621 buf.buf = nr_pixelstore_1M_new (FALSE, 0);
1622 }
1624 // Mark the region clean
1625 sp_canvas_mark_rect(canvas, x0, y0, x1, y1, 0);
1627 buf.buf_rowstride = sw * 4;
1628 buf.rect.x0 = x0;
1629 buf.rect.y0 = y0;
1630 buf.rect.x1 = x1;
1631 buf.rect.y1 = y1;
1632 buf.visible_rect.x0 = draw_x1;
1633 buf.visible_rect.y0 = draw_y1;
1634 buf.visible_rect.x1 = draw_x2;
1635 buf.visible_rect.y1 = draw_y2;
1636 GdkColor *color = &widget->style->bg[GTK_STATE_NORMAL];
1637 buf.bg_color = (((color->red & 0xff00) << 8)
1638 | (color->green & 0xff00)
1639 | (color->blue >> 8));
1640 buf.is_empty = true;
1642 buf.ct = nr_create_cairo_context_canvasbuf (&(buf.visible_rect), &buf);
1644 if (canvas->root->flags & SP_CANVAS_ITEM_VISIBLE) {
1645 SP_CANVAS_ITEM_GET_CLASS (canvas->root)->render (canvas->root, &buf);
1646 }
1648 #if ENABLE_LCMS
1649 cmsHTRANSFORM transf = 0;
1650 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1651 bool fromDisplay = prefs->getBool( "/options/displayprofile/from_display");
1652 if ( fromDisplay ) {
1653 transf = Inkscape::colorprofile_get_display_per( canvas->cms_key ? *(canvas->cms_key) : "" );
1654 } else {
1655 transf = Inkscape::colorprofile_get_display_transform();
1656 }
1657 #endif // ENABLE_LCMS
1659 if (buf.is_empty) {
1660 #if ENABLE_LCMS
1661 if ( transf && canvas->enable_cms_display_adj ) {
1662 cmsDoTransform( transf, &buf.bg_color, &buf.bg_color, 1 );
1663 }
1664 #endif // ENABLE_LCMS
1665 gdk_rgb_gc_set_foreground (canvas->pixmap_gc, buf.bg_color);
1666 gdk_draw_rectangle (SP_CANVAS_WINDOW (canvas),
1667 canvas->pixmap_gc,
1668 TRUE,
1669 x0 - canvas->x0, y0 - canvas->y0,
1670 x1 - x0, y1 - y0);
1671 } else {
1673 #if ENABLE_LCMS
1674 if ( transf && canvas->enable_cms_display_adj ) {
1675 for ( gint yy = 0; yy < (y1 - y0); yy++ ) {
1676 guchar* p = buf.buf + (buf.buf_rowstride * yy);
1677 cmsDoTransform( transf, p, p, (x1 - x0) );
1678 }
1679 }
1680 #endif // ENABLE_LCMS
1682 // Now we only need to output the prepared pixmap to the actual screen, and this define chooses one
1683 // of the two ways to do it. The cairo way is direct and straightforward, but unfortunately
1684 // noticeably slower. I asked Carl Worth but he was unable so far to suggest any specific reason
1685 // for this slowness. So, for now we use the oldish method: squeeze out 32bpp buffer to 24bpp and
1686 // use gdk_draw_rgb_image_dithalign, for unfortunately gdk can only handle 24 bpp, which cairo
1687 // cannot handle at all. Still, this way is currently faster even despite the blit with squeeze.
1689 ///#define CANVAS_OUTPUT_VIA_CAIRO
1691 #ifdef CANVAS_OUTPUT_VIA_CAIRO
1693 buf.cst = cairo_image_surface_create_for_data (
1694 buf.buf,
1695 CAIRO_FORMAT_ARGB32, // unpacked, i.e. 32 bits! one byte is unused
1696 x1 - x0, y1 - y0,
1697 buf.buf_rowstride
1698 );
1699 cairo_t *window_ct = gdk_cairo_create(SP_CANVAS_WINDOW (canvas));
1700 cairo_set_source_surface (window_ct, buf.cst, x0 - canvas->x0, y0 - canvas->y0);
1701 cairo_paint (window_ct);
1702 cairo_destroy (window_ct);
1703 cairo_surface_finish (buf.cst);
1704 cairo_surface_destroy (buf.cst);
1706 #else
1708 NRPixBlock b3;
1709 nr_pixblock_setup_fast (&b3, NR_PIXBLOCK_MODE_R8G8B8, x0, y0, x1, y1, TRUE);
1711 NRPixBlock b4;
1712 nr_pixblock_setup_extern (&b4, NR_PIXBLOCK_MODE_R8G8B8A8P, x0, y0, x1, y1,
1713 buf.buf,
1714 buf.buf_rowstride,
1715 FALSE, FALSE);
1717 // this does the 32->24 squishing, using an assembler routine:
1718 nr_blit_pixblock_pixblock (&b3, &b4);
1720 gdk_draw_rgb_image_dithalign (SP_CANVAS_WINDOW (canvas),
1721 canvas->pixmap_gc,
1722 x0 - canvas->x0, y0 - canvas->y0,
1723 x1 - x0, y1 - y0,
1724 GDK_RGB_DITHER_MAX,
1725 NR_PIXBLOCK_PX(&b3),
1726 sw * 3,
1727 x0 - canvas->x0, y0 - canvas->y0);
1729 nr_pixblock_release (&b3);
1730 nr_pixblock_release (&b4);
1731 #endif
1732 }
1734 cairo_surface_t *cst = cairo_get_target(buf.ct);
1735 cairo_destroy (buf.ct);
1736 cairo_surface_finish (cst);
1737 cairo_surface_destroy (cst);
1739 if (canvas->rendermode != Inkscape::RENDERMODE_OUTLINE) {
1740 nr_pixelstore_256K_free (buf.buf);
1741 } else {
1742 nr_pixelstore_1M_free (buf.buf);
1743 }
1744 }
1746 struct PaintRectSetup {
1747 SPCanvas* canvas;
1748 NRRectL big_rect;
1749 GTimeVal start_time;
1750 int max_pixels;
1751 Geom::Point mouse_loc;
1752 };
1754 /**
1755 * Paint the given rect, recursively subdividing the region until it is the size of a single
1756 * buffer.
1757 *
1758 * @return true if the drawing completes
1759 */
1760 static int
1761 sp_canvas_paint_rect_internal (PaintRectSetup const *setup, NRRectL this_rect)
1762 {
1763 GTimeVal now;
1764 g_get_current_time (&now);
1766 glong elapsed = (now.tv_sec - setup->start_time.tv_sec) * 1000000
1767 + (now.tv_usec - setup->start_time.tv_usec);
1769 // Allow only very fast buffers to be run together;
1770 // as soon as the total redraw time exceeds 1ms, cancel;
1771 // this returns control to the idle loop and allows Inkscape to process user input
1772 // (potentially interrupting the redraw); as soon as Inkscape has some more idle time,
1773 // it will get back and finish painting what remains to paint.
1774 if (elapsed > 1000) {
1776 // Interrupting redraw isn't always good.
1777 // For example, when you drag one node of a big path, only the buffer containing
1778 // the mouse cursor will be redrawn again and again, and the rest of the path
1779 // will remain stale because Inkscape never has enough idle time to redraw all
1780 // of the screen. To work around this, such operations set a forced_redraw_limit > 0.
1781 // If this limit is set, and if we have aborted redraw more times than is allowed,
1782 // interrupting is blocked and we're forced to redraw full screen once
1783 // (after which we can again interrupt forced_redraw_limit times).
1784 if (setup->canvas->forced_redraw_limit < 0 ||
1785 setup->canvas->forced_redraw_count < setup->canvas->forced_redraw_limit) {
1787 if (setup->canvas->forced_redraw_limit != -1) {
1788 setup->canvas->forced_redraw_count++;
1789 }
1791 return false;
1792 }
1793 }
1795 // Find the optimal buffer dimensions
1796 int bw = this_rect.x1 - this_rect.x0;
1797 int bh = this_rect.y1 - this_rect.y0;
1798 if ((bw < 1) || (bh < 1))
1799 return 0;
1801 if (bw * bh < setup->max_pixels) {
1802 // We are small enough
1803 sp_canvas_paint_single_buffer (setup->canvas,
1804 this_rect.x0, this_rect.y0,
1805 this_rect.x1, this_rect.y1,
1806 setup->big_rect.x0, setup->big_rect.y0,
1807 setup->big_rect.x1, setup->big_rect.y1, bw);
1808 return 1;
1809 }
1811 NRRectL lo = this_rect;
1812 NRRectL hi = this_rect;
1814 /*
1815 This test determines the redraw strategy:
1817 bw < bh (strips mode) splits across the smaller dimension of the rect and therefore (on
1818 horizontally-stretched windows) results in redrawing in horizontal strips (from cursor point, in
1819 both directions if the cursor is in the middle). This is traditional for Inkscape since old days,
1820 and seems to be faster for drawings with many smaller objects at zoom-out.
1822 bw > bh (chunks mode) splits across the larger dimension of the rect and therefore paints in
1823 almost-square chunks, again from the cursor point. It's sometimes faster for drawings with few slow
1824 (e.g. blurred) objects crossing the entire screen. It also appears to be somewhat psychologically
1825 faster.
1827 The default for now is the strips mode.
1828 */
1829 if (bw < bh || bh < 2 * TILE_SIZE) {
1830 // to correctly calculate the mean of two ints, we need to sum them into a larger int type
1831 int mid = ((long long) this_rect.x0 + (long long) this_rect.x1) / 2;
1832 // Make sure that mid lies on a tile boundary
1833 mid = (mid / TILE_SIZE) * TILE_SIZE;
1835 lo.x1 = mid;
1836 hi.x0 = mid;
1838 if (setup->mouse_loc[Geom::X] < mid) {
1839 // Always paint towards the mouse first
1840 return sp_canvas_paint_rect_internal(setup, lo)
1841 && sp_canvas_paint_rect_internal(setup, hi);
1842 } else {
1843 return sp_canvas_paint_rect_internal(setup, hi)
1844 && sp_canvas_paint_rect_internal(setup, lo);
1845 }
1846 } else {
1847 // to correctly calculate the mean of two ints, we need to sum them into a larger int type
1848 int mid = ((long long) this_rect.y0 + (long long) this_rect.y1) / 2;
1849 // Make sure that mid lies on a tile boundary
1850 mid = (mid / TILE_SIZE) * TILE_SIZE;
1852 lo.y1 = mid;
1853 hi.y0 = mid;
1855 if (setup->mouse_loc[Geom::Y] < mid) {
1856 // Always paint towards the mouse first
1857 return sp_canvas_paint_rect_internal(setup, lo)
1858 && sp_canvas_paint_rect_internal(setup, hi);
1859 } else {
1860 return sp_canvas_paint_rect_internal(setup, hi)
1861 && sp_canvas_paint_rect_internal(setup, lo);
1862 }
1863 }
1864 }
1867 /**
1868 * Helper that draws a specific rectangular part of the canvas.
1869 *
1870 * @return true if the rectangle painting succeeds.
1871 */
1872 static bool
1873 sp_canvas_paint_rect (SPCanvas *canvas, int xx0, int yy0, int xx1, int yy1)
1874 {
1875 g_return_val_if_fail (!canvas->need_update, false);
1877 NRRectL rect;
1878 rect.x0 = xx0;
1879 rect.x1 = xx1;
1880 rect.y0 = yy0;
1881 rect.y1 = yy1;
1883 // Clip rect-to-draw by the current visible area
1884 rect.x0 = MAX (rect.x0, canvas->x0);
1885 rect.y0 = MAX (rect.y0, canvas->y0);
1886 rect.x1 = MIN (rect.x1, canvas->x0/*draw_x1*/ + GTK_WIDGET (canvas)->allocation.width);
1887 rect.y1 = MIN (rect.y1, canvas->y0/*draw_y1*/ + GTK_WIDGET (canvas)->allocation.height);
1889 #ifdef DEBUG_REDRAW
1890 // paint the area to redraw yellow
1891 gdk_rgb_gc_set_foreground (canvas->pixmap_gc, 0xFFFF00);
1892 gdk_draw_rectangle (SP_CANVAS_WINDOW (canvas),
1893 canvas->pixmap_gc,
1894 TRUE,
1895 rect.x0 - canvas->x0, rect.y0 - canvas->y0,
1896 rect.x1 - rect.x0, rect.y1 - rect.y0);
1897 #endif
1899 PaintRectSetup setup;
1901 setup.canvas = canvas;
1902 setup.big_rect = rect;
1904 // Save the mouse location
1905 gint x, y;
1906 gdk_window_get_pointer (GTK_WIDGET(canvas)->window, &x, &y, NULL);
1907 setup.mouse_loc = sp_canvas_window_to_world (canvas, Geom::Point(x,y));
1909 if (canvas->rendermode != Inkscape::RENDERMODE_OUTLINE) {
1910 // use 256K as a compromise to not slow down gradients
1911 // 256K is the cached buffer and we need 4 channels
1912 setup.max_pixels = 65536; // 256K/4
1913 } else {
1914 // paths only, so 1M works faster
1915 // 1M is the cached buffer and we need 4 channels
1916 setup.max_pixels = 262144;
1917 }
1919 // Start the clock
1920 g_get_current_time(&(setup.start_time));
1922 // Go
1923 return sp_canvas_paint_rect_internal(&setup, rect);
1924 }
1926 /**
1927 * Force a full redraw after a specified number of interrupted redraws
1928 */
1929 void
1930 sp_canvas_force_full_redraw_after_interruptions(SPCanvas *canvas, unsigned int count) {
1931 g_return_if_fail(canvas != NULL);
1933 canvas->forced_redraw_limit = count;
1934 canvas->forced_redraw_count = 0;
1935 }
1937 /**
1938 * End forced full redraw requests
1939 */
1940 void
1941 sp_canvas_end_forced_full_redraws(SPCanvas *canvas) {
1942 g_return_if_fail(canvas != NULL);
1944 canvas->forced_redraw_limit = -1;
1945 }
1947 /**
1948 * The canvas widget's expose callback.
1949 */
1950 static gint
1951 sp_canvas_expose (GtkWidget *widget, GdkEventExpose *event)
1952 {
1953 SPCanvas *canvas = SP_CANVAS (widget);
1955 if (!GTK_WIDGET_DRAWABLE (widget) ||
1956 (event->window != SP_CANVAS_WINDOW (canvas)))
1957 return FALSE;
1959 int n_rects;
1960 GdkRectangle *rects;
1961 gdk_region_get_rectangles (event->region, &rects, &n_rects);
1963 for (int i = 0; i < n_rects; i++) {
1964 NRRectL rect;
1966 rect.x0 = rects[i].x + canvas->x0;
1967 rect.y0 = rects[i].y + canvas->y0;
1968 rect.x1 = rect.x0 + rects[i].width;
1969 rect.y1 = rect.y0 + rects[i].height;
1971 sp_canvas_request_redraw (canvas, rect.x0, rect.y0, rect.x1, rect.y1);
1972 }
1974 if (n_rects > 0)
1975 g_free (rects);
1977 return FALSE;
1978 }
1980 /**
1981 * The canvas widget's keypress callback.
1982 */
1983 static gint
1984 sp_canvas_key (GtkWidget *widget, GdkEventKey *event)
1985 {
1986 return emit_event (SP_CANVAS (widget), (GdkEvent *) event);
1987 }
1989 /**
1990 * Crossing event handler for the canvas.
1991 */
1992 static gint
1993 sp_canvas_crossing (GtkWidget *widget, GdkEventCrossing *event)
1994 {
1995 SPCanvas *canvas = SP_CANVAS (widget);
1997 if (event->window != SP_CANVAS_WINDOW (canvas))
1998 return FALSE;
2000 canvas->state = event->state;
2001 return pick_current_item (canvas, (GdkEvent *) event);
2002 }
2004 /**
2005 * Focus in handler for the canvas.
2006 */
2007 static gint
2008 sp_canvas_focus_in (GtkWidget *widget, GdkEventFocus *event)
2009 {
2010 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
2012 SPCanvas *canvas = SP_CANVAS (widget);
2014 if (canvas->focused_item) {
2015 return emit_event (canvas, (GdkEvent *) event);
2016 } else {
2017 return FALSE;
2018 }
2019 }
2021 /**
2022 * Focus out handler for the canvas.
2023 */
2024 static gint
2025 sp_canvas_focus_out (GtkWidget *widget, GdkEventFocus *event)
2026 {
2027 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
2029 SPCanvas *canvas = SP_CANVAS (widget);
2031 if (canvas->focused_item)
2032 return emit_event (canvas, (GdkEvent *) event);
2033 else
2034 return FALSE;
2035 }
2037 /**
2038 * Helper that repaints the areas in the canvas that need it.
2039 *
2040 * @return true if all the dirty parts have been redrawn
2041 */
2042 static int
2043 paint (SPCanvas *canvas)
2044 {
2045 if (canvas->need_update) {
2046 sp_canvas_item_invoke_update (canvas->root, Geom::identity(), 0);
2047 canvas->need_update = FALSE;
2048 }
2050 if (!canvas->need_redraw)
2051 return TRUE;
2053 Gdk::Region to_paint;
2055 for (int j=canvas->tTop; j<canvas->tBottom; j++) {
2056 for (int i=canvas->tLeft; i<canvas->tRight; i++) {
2057 int tile_index = (i - canvas->tLeft) + (j - canvas->tTop)*canvas->tileH;
2059 if ( canvas->tiles[tile_index] ) { // if this tile is dirtied (nonzero)
2060 to_paint.union_with_rect(Gdk::Rectangle(i*TILE_SIZE, j*TILE_SIZE,
2061 TILE_SIZE, TILE_SIZE));
2062 }
2064 }
2065 }
2067 if (!to_paint.empty()) {
2068 Glib::ArrayHandle<Gdk::Rectangle> rect = to_paint.get_rectangles();
2069 typedef Glib::ArrayHandle<Gdk::Rectangle>::const_iterator Iter;
2070 for (Iter i=rect.begin(); i != rect.end(); ++i) {
2071 int x0 = (*i).get_x();
2072 int y0 = (*i).get_y();
2073 int x1 = x0 + (*i).get_width();
2074 int y1 = y0 + (*i).get_height();
2075 if (!sp_canvas_paint_rect(canvas, x0, y0, x1, y1)) {
2076 // Aborted
2077 return FALSE;
2078 };
2079 }
2080 }
2082 canvas->need_redraw = FALSE;
2084 // we've had a full unaborted redraw, reset the full redraw counter
2085 if (canvas->forced_redraw_limit != -1) {
2086 canvas->forced_redraw_count = 0;
2087 }
2089 return TRUE;
2090 }
2092 /**
2093 * Helper that invokes update, paint, and repick on canvas.
2094 */
2095 static int
2096 do_update (SPCanvas *canvas)
2097 {
2098 if (!canvas->root || !canvas->pixmap_gc) // canvas may have already be destroyed by closing desktop durring interrupted display!
2099 return TRUE;
2101 /* Cause the update if necessary */
2102 if (canvas->need_update) {
2103 sp_canvas_item_invoke_update (canvas->root, Geom::identity(), 0);
2104 canvas->need_update = FALSE;
2105 }
2107 /* Paint if able to */
2108 if (GTK_WIDGET_DRAWABLE (canvas)) {
2109 return paint (canvas);
2110 }
2112 /* Pick new current item */
2113 while (canvas->need_repick) {
2114 canvas->need_repick = FALSE;
2115 pick_current_item (canvas, &canvas->pick_event);
2116 }
2118 return TRUE;
2119 }
2121 /**
2122 * Idle handler for the canvas that deals with pending updates and redraws.
2123 */
2124 static gint
2125 idle_handler (gpointer data)
2126 {
2127 GDK_THREADS_ENTER ();
2129 SPCanvas *canvas = SP_CANVAS (data);
2131 int const ret = do_update (canvas);
2133 if (ret) {
2134 /* Reset idle id */
2135 canvas->idle_id = 0;
2136 }
2138 GDK_THREADS_LEAVE ();
2140 return !ret;
2141 }
2143 /**
2144 * Convenience function to add an idle handler to a canvas.
2145 */
2146 static void
2147 add_idle (SPCanvas *canvas)
2148 {
2149 if (canvas->idle_id != 0)
2150 return;
2152 canvas->idle_id = gtk_idle_add_priority (sp_canvas_update_priority, idle_handler, canvas);
2153 }
2155 /**
2156 * Returns the root group of the specified canvas.
2157 */
2158 SPCanvasGroup *
2159 sp_canvas_root (SPCanvas *canvas)
2160 {
2161 g_return_val_if_fail (canvas != NULL, NULL);
2162 g_return_val_if_fail (SP_IS_CANVAS (canvas), NULL);
2164 return SP_CANVAS_GROUP (canvas->root);
2165 }
2167 /**
2168 * Scrolls canvas to specific position (cx and cy are measured in screen pixels)
2169 */
2170 void
2171 sp_canvas_scroll_to (SPCanvas *canvas, double cx, double cy, unsigned int clear, bool is_scrolling)
2172 {
2173 g_return_if_fail (canvas != NULL);
2174 g_return_if_fail (SP_IS_CANVAS (canvas));
2176 int ix = (int) round(cx); // ix and iy are the new canvas coordinates (integer screen pixels)
2177 int iy = (int) round(cy); // cx might be negative, so (int)(cx + 0.5) will not do!
2178 int dx = ix - canvas->x0; // dx and dy specify the displacement (scroll) of the
2179 int dy = iy - canvas->y0; // canvas w.r.t its previous position
2181 canvas->dx0 = cx; // here the 'd' stands for double, not delta!
2182 canvas->dy0 = cy;
2183 canvas->x0 = ix;
2184 canvas->y0 = iy;
2186 sp_canvas_resize_tiles (canvas, canvas->x0, canvas->y0, canvas->x0+canvas->widget.allocation.width, canvas->y0+canvas->widget.allocation.height);
2188 if (!clear) {
2189 // scrolling without zoom; redraw only the newly exposed areas
2190 if ((dx != 0) || (dy != 0)) {
2191 canvas->is_scrolling = is_scrolling;
2192 if (GTK_WIDGET_REALIZED (canvas)) {
2193 gdk_window_scroll (SP_CANVAS_WINDOW (canvas), -dx, -dy);
2194 }
2195 }
2196 } else {
2197 // scrolling as part of zoom; do nothing here - the next do_update will perform full redraw
2198 }
2199 }
2201 /**
2202 * Updates canvas if necessary.
2203 */
2204 void
2205 sp_canvas_update_now (SPCanvas *canvas)
2206 {
2207 g_return_if_fail (canvas != NULL);
2208 g_return_if_fail (SP_IS_CANVAS (canvas));
2210 if (!(canvas->need_update ||
2211 canvas->need_redraw))
2212 return;
2214 do_update (canvas);
2215 }
2217 /**
2218 * Update callback for canvas widget.
2219 */
2220 static void
2221 sp_canvas_request_update (SPCanvas *canvas)
2222 {
2223 canvas->need_update = TRUE;
2224 add_idle (canvas);
2225 }
2227 /**
2228 * Forces redraw of rectangular canvas area.
2229 */
2230 void
2231 sp_canvas_request_redraw (SPCanvas *canvas, int x0, int y0, int x1, int y1)
2232 {
2233 NRRectL bbox;
2234 NRRectL visible;
2235 NRRectL clip;
2237 g_return_if_fail (canvas != NULL);
2238 g_return_if_fail (SP_IS_CANVAS (canvas));
2240 if (!GTK_WIDGET_DRAWABLE (canvas)) return;
2241 if ((x0 >= x1) || (y0 >= y1)) return;
2243 bbox.x0 = x0;
2244 bbox.y0 = y0;
2245 bbox.x1 = x1;
2246 bbox.y1 = y1;
2248 visible.x0 = canvas->x0;
2249 visible.y0 = canvas->y0;
2250 visible.x1 = visible.x0 + GTK_WIDGET (canvas)->allocation.width;
2251 visible.y1 = visible.y0 + GTK_WIDGET (canvas)->allocation.height;
2253 nr_rect_l_intersect (&clip, &bbox, &visible);
2255 sp_canvas_dirty_rect(canvas, clip.x0, clip.y0, clip.x1, clip.y1);
2256 add_idle (canvas);
2257 }
2259 /**
2260 * Sets world coordinates from win and canvas.
2261 */
2262 void sp_canvas_window_to_world(SPCanvas const *canvas, double winx, double winy, double *worldx, double *worldy)
2263 {
2264 g_return_if_fail (canvas != NULL);
2265 g_return_if_fail (SP_IS_CANVAS (canvas));
2267 if (worldx) *worldx = canvas->x0 + winx;
2268 if (worldy) *worldy = canvas->y0 + winy;
2269 }
2271 /**
2272 * Sets win coordinates from world and canvas.
2273 */
2274 void sp_canvas_world_to_window(SPCanvas const *canvas, double worldx, double worldy, double *winx, double *winy)
2275 {
2276 g_return_if_fail (canvas != NULL);
2277 g_return_if_fail (SP_IS_CANVAS (canvas));
2279 if (winx) *winx = worldx - canvas->x0;
2280 if (winy) *winy = worldy - canvas->y0;
2281 }
2283 /**
2284 * Converts point from win to world coordinates.
2285 */
2286 Geom::Point sp_canvas_window_to_world(SPCanvas const *canvas, Geom::Point const win)
2287 {
2288 g_assert (canvas != NULL);
2289 g_assert (SP_IS_CANVAS (canvas));
2291 return Geom::Point(canvas->x0 + win[0], canvas->y0 + win[1]);
2292 }
2294 /**
2295 * Converts point from world to win coordinates.
2296 */
2297 Geom::Point sp_canvas_world_to_window(SPCanvas const *canvas, Geom::Point const world)
2298 {
2299 g_assert (canvas != NULL);
2300 g_assert (SP_IS_CANVAS (canvas));
2302 return Geom::Point(world[0] - canvas->x0, world[1] - canvas->y0);
2303 }
2305 /**
2306 * Returns true if point given in world coordinates is inside window.
2307 */
2308 bool sp_canvas_world_pt_inside_window(SPCanvas const *canvas, Geom::Point const &world)
2309 {
2310 g_assert( canvas != NULL );
2311 g_assert(SP_IS_CANVAS(canvas));
2313 GtkWidget const &w = *GTK_WIDGET(canvas);
2314 return ( ( canvas->x0 <= world[Geom::X] ) &&
2315 ( canvas->y0 <= world[Geom::Y] ) &&
2316 ( world[Geom::X] < canvas->x0 + w.allocation.width ) &&
2317 ( world[Geom::Y] < canvas->y0 + w.allocation.height ) );
2318 }
2320 /**
2321 * Return canvas window coordinates as Geom::Rect.
2322 */
2323 Geom::Rect SPCanvas::getViewbox() const
2324 {
2325 GtkWidget const *w = GTK_WIDGET(this);
2326 return Geom::Rect(Geom::Point(dx0, dy0),
2327 Geom::Point(dx0 + w->allocation.width, dy0 + w->allocation.height));
2328 }
2330 /**
2331 * Return canvas window coordinates as IRect (a rectangle defined by integers).
2332 */
2333 NR::IRect SPCanvas::getViewboxIntegers() const
2334 {
2335 GtkWidget const *w = GTK_WIDGET(this);
2336 return NR::IRect(NR::IPoint(x0, y0),
2337 NR::IPoint(x0 + w->allocation.width, y0 + w->allocation.height));
2338 }
2340 inline int sp_canvas_tile_floor(int x)
2341 {
2342 return (x & (~(TILE_SIZE - 1))) / TILE_SIZE;
2343 }
2345 inline int sp_canvas_tile_ceil(int x)
2346 {
2347 return ((x + (TILE_SIZE - 1)) & (~(TILE_SIZE - 1))) / TILE_SIZE;
2348 }
2350 /**
2351 * Helper that allocates a new tile array for the canvas, copying overlapping tiles from the old array
2352 */
2353 static void sp_canvas_resize_tiles(SPCanvas* canvas, int nl, int nt, int nr, int nb)
2354 {
2355 if ( nl >= nr || nt >= nb ) {
2356 if ( canvas->tiles ) g_free(canvas->tiles);
2357 canvas->tLeft=canvas->tTop=canvas->tRight=canvas->tBottom=0;
2358 canvas->tileH=canvas->tileV=0;
2359 canvas->tiles=NULL;
2360 return;
2361 }
2362 int tl=sp_canvas_tile_floor(nl);
2363 int tt=sp_canvas_tile_floor(nt);
2364 int tr=sp_canvas_tile_ceil(nr);
2365 int tb=sp_canvas_tile_ceil(nb);
2367 int nh = tr-tl, nv = tb-tt;
2368 uint8_t* ntiles = (uint8_t*)g_malloc(nh*nv*sizeof(uint8_t));
2369 for (int i=tl; i<tr; i++) {
2370 for (int j=tt; j<tb; j++) {
2371 int ind = (i-tl) + (j-tt)*nh;
2372 if ( i >= canvas->tLeft && i < canvas->tRight && j >= canvas->tTop && j < canvas->tBottom ) {
2373 ntiles[ind]=canvas->tiles[(i-canvas->tLeft)+(j-canvas->tTop)*canvas->tileH]; // copy from the old tile
2374 } else {
2375 ntiles[ind]=0; // newly exposed areas get 0
2376 }
2377 }
2378 }
2379 if ( canvas->tiles ) g_free(canvas->tiles);
2380 canvas->tiles=ntiles;
2381 canvas->tLeft=tl;
2382 canvas->tTop=tt;
2383 canvas->tRight=tr;
2384 canvas->tBottom=tb;
2385 canvas->tileH=nh;
2386 canvas->tileV=nv;
2387 }
2389 /*
2390 * Helper that queues a canvas rectangle for redraw
2391 */
2392 static void sp_canvas_dirty_rect(SPCanvas* canvas, int nl, int nt, int nr, int nb) {
2393 canvas->need_redraw = TRUE;
2395 sp_canvas_mark_rect(canvas, nl, nt, nr, nb, 1);
2396 }
2398 /**
2399 * Helper that marks specific canvas rectangle as clean (val == 0) or dirty (otherwise)
2400 */
2401 void sp_canvas_mark_rect(SPCanvas* canvas, int nl, int nt, int nr, int nb, uint8_t val)
2402 {
2403 if ( nl >= nr || nt >= nb ) {
2404 return;
2405 }
2406 int tl=sp_canvas_tile_floor(nl);
2407 int tt=sp_canvas_tile_floor(nt);
2408 int tr=sp_canvas_tile_ceil(nr);
2409 int tb=sp_canvas_tile_ceil(nb);
2410 if ( tl >= canvas->tRight || tr <= canvas->tLeft || tt >= canvas->tBottom || tb <= canvas->tTop ) return;
2411 if ( tl < canvas->tLeft ) tl=canvas->tLeft;
2412 if ( tr > canvas->tRight ) tr=canvas->tRight;
2413 if ( tt < canvas->tTop ) tt=canvas->tTop;
2414 if ( tb > canvas->tBottom ) tb=canvas->tBottom;
2416 for (int i=tl; i<tr; i++) {
2417 for (int j=tt; j<tb; j++) {
2418 canvas->tiles[(i-canvas->tLeft)+(j-canvas->tTop)*canvas->tileH] = val;
2419 }
2420 }
2421 }
2424 /*
2425 Local Variables:
2426 mode:c++
2427 c-file-style:"stroustrup"
2428 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2429 indent-tabs-mode:nil
2430 fill-column:99
2431 End:
2432 */
2433 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :