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};
108 static void sp_canvas_request_update (SPCanvas *canvas);
110 static void track_latency(GdkEvent const *event);
111 static void sp_canvas_item_class_init (SPCanvasItemClass *klass);
112 static void sp_canvas_item_init (SPCanvasItem *item);
113 static void sp_canvas_item_dispose (GObject *object);
114 static void sp_canvas_item_construct (SPCanvasItem *item, SPCanvasGroup *parent, gchar const *first_arg_name, va_list args);
116 static int emit_event (SPCanvas *canvas, GdkEvent *event);
118 static guint item_signals[ITEM_LAST_SIGNAL] = { 0 };
120 static GtkObjectClass *item_parent_class;
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 /* fixme: Derive from GObject */
155 item_parent_class = (GtkObjectClass*)gtk_type_class (GTK_TYPE_OBJECT);
157 item_signals[ITEM_EVENT] = g_signal_new ("event",
158 G_TYPE_FROM_CLASS (klass),
159 G_SIGNAL_RUN_LAST,
160 ((glong)((guint8*)&(klass->event) - (guint8*)klass)),
161 NULL, NULL,
162 sp_marshal_BOOLEAN__POINTER,
163 G_TYPE_BOOLEAN, 1,
164 GDK_TYPE_EVENT);
166 object_class->dispose = sp_canvas_item_dispose;
167 }
169 /**
170 * Callback for initialization of SPCanvasItem.
171 */
172 static void
173 sp_canvas_item_init (SPCanvasItem *item)
174 {
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 (item_parent_class)->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 /**
481 * Sets visible flag on item and requests a redraw.
482 */
483 void
484 sp_canvas_item_show (SPCanvasItem *item)
485 {
486 g_return_if_fail (item != NULL);
487 g_return_if_fail (SP_IS_CANVAS_ITEM (item));
489 if (item->flags & SP_CANVAS_ITEM_VISIBLE)
490 return;
492 item->flags |= SP_CANVAS_ITEM_VISIBLE;
494 int x0 = (int)(item->x1);
495 int x1 = (int)(item->x2);
496 int y0 = (int)(item->y1);
497 int y1 = (int)(item->y2);
499 if (x0 !=0 || x1 !=0 || y0 !=0 || y1 !=0) {
500 sp_canvas_request_redraw (item->canvas, (int)(item->x1), (int)(item->y1), (int)(item->x2 + 1), (int)(item->y2 + 1));
501 item->canvas->need_repick = TRUE;
502 }
503 }
505 /**
506 * Clears visible flag on item and requests a redraw.
507 */
508 void
509 sp_canvas_item_hide (SPCanvasItem *item)
510 {
511 g_return_if_fail (item != NULL);
512 g_return_if_fail (SP_IS_CANVAS_ITEM (item));
514 if (!(item->flags & SP_CANVAS_ITEM_VISIBLE))
515 return;
517 item->flags &= ~SP_CANVAS_ITEM_VISIBLE;
519 int x0 = (int)(item->x1);
520 int x1 = (int)(item->x2);
521 int y0 = (int)(item->y1);
522 int y1 = (int)(item->y2);
524 if (x0 !=0 || x1 !=0 || y0 !=0 || y1 !=0) {
525 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)(item->x2 + 1), (int)(item->y2 + 1));
526 item->canvas->need_repick = TRUE;
527 }
528 }
530 /**
531 * Grab item under cursor.
532 *
533 * \pre !canvas->grabbed_item && item->flags & SP_CANVAS_ITEM_VISIBLE
534 */
535 int
536 sp_canvas_item_grab (SPCanvasItem *item, guint event_mask, GdkCursor *cursor, guint32 etime)
537 {
538 g_return_val_if_fail (item != NULL, -1);
539 g_return_val_if_fail (SP_IS_CANVAS_ITEM (item), -1);
540 g_return_val_if_fail (GTK_WIDGET_MAPPED (item->canvas), -1);
542 if (item->canvas->grabbed_item)
543 return -1;
545 if (!(item->flags & SP_CANVAS_ITEM_VISIBLE))
546 return -1;
548 if (HAS_BROKEN_MOTION_HINTS) {
549 event_mask &= ~GDK_POINTER_MOTION_HINT_MASK;
550 }
552 /* fixme: Top hack (Lauris) */
553 /* fixme: If we add key masks to event mask, Gdk will abort (Lauris) */
554 /* fixme: But Canvas actualle does get key events, so all we need is routing these here */
555 gdk_pointer_grab (SP_CANVAS_WINDOW (item->canvas), FALSE,
556 (GdkEventMask)(event_mask & (~(GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK))),
557 NULL, cursor, etime);
559 item->canvas->grabbed_item = item;
560 item->canvas->grabbed_event_mask = event_mask;
561 item->canvas->current_item = item; /* So that events go to the grabbed item */
563 return 0;
564 }
566 /**
567 * Ungrabs the item, which must have been grabbed in the canvas, and ungrabs the
568 * mouse.
569 *
570 * \param item A canvas item that holds a grab.
571 * \param etime The timestamp for ungrabbing the mouse.
572 */
573 void
574 sp_canvas_item_ungrab (SPCanvasItem *item, guint32 etime)
575 {
576 g_return_if_fail (item != NULL);
577 g_return_if_fail (SP_IS_CANVAS_ITEM (item));
579 if (item->canvas->grabbed_item != item)
580 return;
582 item->canvas->grabbed_item = NULL;
584 gdk_pointer_ungrab (etime);
585 }
587 /**
588 * Returns the product of all transformation matrices from the root item down
589 * to the item.
590 */
591 Geom::Matrix sp_canvas_item_i2w_affine(SPCanvasItem const *item)
592 {
593 g_assert (SP_IS_CANVAS_ITEM (item)); // should we get this?
595 Geom::Matrix affine = Geom::identity();
597 while (item) {
598 affine *= item->xform;
599 item = item->parent;
600 }
601 return affine;
602 }
604 /**
605 * Helper that returns true iff item is descendant of parent.
606 */
607 static bool is_descendant(SPCanvasItem const *item, SPCanvasItem const *parent)
608 {
609 while (item) {
610 if (item == parent)
611 return true;
612 item = item->parent;
613 }
615 return false;
616 }
618 /**
619 * Focus canvas, and item under cursor if it is not already focussed.
620 */
621 void
622 sp_canvas_item_grab_focus (SPCanvasItem *item)
623 {
624 g_return_if_fail (item != NULL);
625 g_return_if_fail (SP_IS_CANVAS_ITEM (item));
626 g_return_if_fail (GTK_WIDGET_CAN_FOCUS (GTK_WIDGET (item->canvas)));
628 SPCanvasItem *focused_item = item->canvas->focused_item;
630 if (focused_item) {
631 GdkEvent ev;
632 ev.focus_change.type = GDK_FOCUS_CHANGE;
633 ev.focus_change.window = SP_CANVAS_WINDOW (item->canvas);
634 ev.focus_change.send_event = FALSE;
635 ev.focus_change.in = FALSE;
637 emit_event (item->canvas, &ev);
638 }
640 item->canvas->focused_item = item;
641 gtk_widget_grab_focus (GTK_WIDGET (item->canvas));
643 if (focused_item) {
644 GdkEvent ev;
645 ev.focus_change.type = GDK_FOCUS_CHANGE;
646 ev.focus_change.window = SP_CANVAS_WINDOW (item->canvas);
647 ev.focus_change.send_event = FALSE;
648 ev.focus_change.in = TRUE;
650 emit_event (item->canvas, &ev);
651 }
652 }
654 /**
655 * Requests that the canvas queue an update for the specified item.
656 *
657 * To be used only by item implementations.
658 */
659 void
660 sp_canvas_item_request_update (SPCanvasItem *item)
661 {
662 if (item->flags & SP_CANVAS_ITEM_NEED_UPDATE)
663 return;
665 item->flags |= SP_CANVAS_ITEM_NEED_UPDATE;
667 if (item->parent != NULL) {
668 /* Recurse up the tree */
669 sp_canvas_item_request_update (item->parent);
670 } else {
671 /* Have reached the top of the tree, make sure the update call gets scheduled. */
672 sp_canvas_request_update (item->canvas);
673 }
674 }
676 /**
677 * Returns position of item in group.
678 */
679 gint sp_canvas_item_order (SPCanvasItem * item)
680 {
681 return g_list_index (SP_CANVAS_GROUP (item->parent)->items, item);
682 }
684 /* SPCanvasGroup */
686 static void sp_canvas_group_class_init (SPCanvasGroupClass *klass);
687 static void sp_canvas_group_init (SPCanvasGroup *group);
688 static void sp_canvas_group_destroy (GtkObject *object);
690 static void sp_canvas_group_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags);
691 static double sp_canvas_group_point (SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item);
692 static void sp_canvas_group_render (SPCanvasItem *item, SPCanvasBuf *buf);
694 static SPCanvasItemClass *group_parent_class;
696 /**
697 * Registers SPCanvasGroup class with Gtk and returns its type number.
698 */
699 GType sp_canvas_group_get_type(void)
700 {
701 static GType type = 0;
702 if (!type) {
703 GTypeInfo info = {
704 sizeof(SPCanvasGroupClass),
705 0, // base_init
706 0, // base_finalize
707 (GClassInitFunc)sp_canvas_group_class_init,
708 0, // class_finalize
709 0, // class_data
710 sizeof(SPCanvasGroup),
711 0, // n_preallocs
712 (GInstanceInitFunc)sp_canvas_group_init,
713 0 // value_table
714 };
715 type = g_type_register_static(sp_canvas_item_get_type(), "SPCanvasGroup", &info, static_cast<GTypeFlags>(0));
716 }
717 return type;
718 }
720 /**
721 * Class initialization function for SPCanvasGroupClass
722 */
723 static void
724 sp_canvas_group_class_init (SPCanvasGroupClass *klass)
725 {
726 GtkObjectClass *object_class = (GtkObjectClass *) klass;
727 SPCanvasItemClass *item_class = (SPCanvasItemClass *) klass;
729 group_parent_class = (SPCanvasItemClass*)gtk_type_class (sp_canvas_item_get_type ());
731 object_class->destroy = sp_canvas_group_destroy;
733 item_class->update = sp_canvas_group_update;
734 item_class->render = sp_canvas_group_render;
735 item_class->point = sp_canvas_group_point;
736 }
738 /**
739 * Callback. Empty.
740 */
741 static void
742 sp_canvas_group_init (SPCanvasGroup */*group*/)
743 {
744 /* Nothing here */
745 }
747 /**
748 * Callback that destroys all items in group and calls group's virtual
749 * destroy() function.
750 */
751 static void
752 sp_canvas_group_destroy (GtkObject *object)
753 {
754 g_return_if_fail (object != NULL);
755 g_return_if_fail (SP_IS_CANVAS_GROUP (object));
757 SPCanvasGroup const *group = SP_CANVAS_GROUP (object);
759 GList *list = group->items;
760 while (list) {
761 SPCanvasItem *child = (SPCanvasItem *)list->data;
762 list = list->next;
764 gtk_object_destroy (GTK_OBJECT (child));
765 }
767 if (GTK_OBJECT_CLASS (group_parent_class)->destroy)
768 (* GTK_OBJECT_CLASS (group_parent_class)->destroy) (object);
769 }
771 /**
772 * Update handler for canvas groups
773 */
774 static void
775 sp_canvas_group_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
776 {
777 SPCanvasGroup const *group = SP_CANVAS_GROUP (item);
778 Geom::RectHull corners(Geom::Point(0, 0));
779 bool empty=true;
781 for (GList *list = group->items; list; list = list->next) {
782 SPCanvasItem *i = (SPCanvasItem *)list->data;
784 sp_canvas_item_invoke_update (i, affine, flags);
786 if ( i->x2 > i->x1 && i->y2 > i->y1 ) {
787 if (empty) {
788 corners = Geom::RectHull(Geom::Point(i->x1, i->y1));
789 empty = false;
790 } else {
791 corners.add(Geom::Point(i->x1, i->y1));
792 }
793 corners.add(Geom::Point(i->x2, i->y2));
794 }
795 }
797 Geom::OptRect const bounds = corners.bounds();
798 if (bounds) {
799 item->x1 = bounds->min()[Geom::X];
800 item->y1 = bounds->min()[Geom::Y];
801 item->x2 = bounds->max()[Geom::X];
802 item->y2 = bounds->max()[Geom::Y];
803 } else {
804 // FIXME ?
805 item->x1 = item->x2 = item->y1 = item->y2 = 0;
806 }
807 }
809 /**
810 * Point handler for canvas groups.
811 */
812 static double
813 sp_canvas_group_point (SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item)
814 {
815 SPCanvasGroup const *group = SP_CANVAS_GROUP (item);
816 double const x = p[Geom::X];
817 double const y = p[Geom::Y];
818 int x1 = (int)(x - item->canvas->close_enough);
819 int y1 = (int)(y - item->canvas->close_enough);
820 int x2 = (int)(x + item->canvas->close_enough);
821 int y2 = (int)(y + item->canvas->close_enough);
823 double best = 0.0;
824 *actual_item = NULL;
826 double dist = 0.0;
828 for (GList *list = group->items; list; list = list->next) {
829 SPCanvasItem *child = (SPCanvasItem *)list->data;
831 if ((child->x1 <= x2) && (child->y1 <= y2) && (child->x2 >= x1) && (child->y2 >= y1)) {
832 SPCanvasItem *point_item = NULL; /* cater for incomplete item implementations */
834 int has_point;
835 if ((child->flags & SP_CANVAS_ITEM_VISIBLE) && SP_CANVAS_ITEM_GET_CLASS (child)->point) {
836 dist = sp_canvas_item_invoke_point (child, p, &point_item);
837 has_point = TRUE;
838 } else
839 has_point = FALSE;
841 if (has_point && point_item && ((int) (dist + 0.5) <= item->canvas->close_enough)) {
842 best = dist;
843 *actual_item = point_item;
844 }
845 }
846 }
848 return best;
849 }
851 /**
852 * Renders all visible canvas group items in buf rectangle.
853 */
854 static void
855 sp_canvas_group_render (SPCanvasItem *item, SPCanvasBuf *buf)
856 {
857 SPCanvasGroup const *group = SP_CANVAS_GROUP (item);
859 for (GList *list = group->items; list; list = list->next) {
860 SPCanvasItem *child = (SPCanvasItem *)list->data;
861 if (child->flags & SP_CANVAS_ITEM_VISIBLE) {
862 if ((child->x1 < buf->rect.x1) &&
863 (child->y1 < buf->rect.y1) &&
864 (child->x2 > buf->rect.x0) &&
865 (child->y2 > buf->rect.y0)) {
866 if (SP_CANVAS_ITEM_GET_CLASS (child)->render)
867 SP_CANVAS_ITEM_GET_CLASS (child)->render (child, buf);
868 }
869 }
870 }
871 }
873 /**
874 * Adds an item to a canvas group.
875 */
876 static void
877 group_add (SPCanvasGroup *group, SPCanvasItem *item)
878 {
879 gtk_object_ref (GTK_OBJECT (item));
880 gtk_object_sink (GTK_OBJECT (item));
882 if (!group->items) {
883 group->items = g_list_append (group->items, item);
884 group->last = group->items;
885 } else {
886 group->last = g_list_append (group->last, item)->next;
887 }
889 sp_canvas_item_request_update (item);
890 }
892 /**
893 * Removes an item from a canvas group
894 */
895 static void
896 group_remove (SPCanvasGroup *group, SPCanvasItem *item)
897 {
898 g_return_if_fail (group != NULL);
899 g_return_if_fail (SP_IS_CANVAS_GROUP (group));
900 g_return_if_fail (item != NULL);
902 for (GList *children = group->items; children; children = children->next) {
903 if (children->data == item) {
905 /* Unparent the child */
907 item->parent = NULL;
908 gtk_object_unref (GTK_OBJECT (item));
910 /* Remove it from the list */
912 if (children == group->last) group->last = children->prev;
914 group->items = g_list_remove_link (group->items, children);
915 g_list_free (children);
916 break;
917 }
918 }
919 }
921 /* SPCanvas */
923 static void sp_canvas_class_init (SPCanvasClass *klass);
924 static void sp_canvas_init (SPCanvas *canvas);
925 static void sp_canvas_destroy (GtkObject *object);
927 static void sp_canvas_realize (GtkWidget *widget);
928 static void sp_canvas_unrealize (GtkWidget *widget);
930 static void sp_canvas_size_request (GtkWidget *widget, GtkRequisition *req);
931 static void sp_canvas_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
933 static gint sp_canvas_button (GtkWidget *widget, GdkEventButton *event);
934 static gint sp_canvas_scroll (GtkWidget *widget, GdkEventScroll *event);
935 static gint sp_canvas_motion (GtkWidget *widget, GdkEventMotion *event);
936 static gint sp_canvas_expose (GtkWidget *widget, GdkEventExpose *event);
937 static gint sp_canvas_key (GtkWidget *widget, GdkEventKey *event);
938 static gint sp_canvas_crossing (GtkWidget *widget, GdkEventCrossing *event);
939 static gint sp_canvas_focus_in (GtkWidget *widget, GdkEventFocus *event);
940 static gint sp_canvas_focus_out (GtkWidget *widget, GdkEventFocus *event);
942 static GtkWidgetClass *canvas_parent_class;
944 static void sp_canvas_resize_tiles(SPCanvas* canvas, int nl, int nt, int nr, int nb);
945 static void sp_canvas_dirty_rect(SPCanvas* canvas, int nl, int nt, int nr, int nb);
946 static void sp_canvas_mark_rect(SPCanvas* canvas, int nl, int nt, int nr, int nb, uint8_t val);
947 static int do_update (SPCanvas *canvas);
949 static gboolean sp_canvas_snap_watchdog_callback(gpointer data);
950 static void sp_canvas_snap_watchdog_set(SPCanvas *canvas, GdkEventMotion *event);
951 static void sp_canvas_snap_watchdog_kill(SPCanvas *canvas);
953 /**
954 * Registers the SPCanvas class if necessary, and returns the type ID
955 * associated to it.
956 *
957 * \return The type ID of the SPCanvas class.
958 **/
959 GType sp_canvas_get_type(void)
960 {
961 static GType type = 0;
962 if (!type) {
963 GTypeInfo info = {
964 sizeof(SPCanvasClass),
965 0, // base_init
966 0, // base_finalize
967 (GClassInitFunc)sp_canvas_class_init,
968 0, // class_finalize
969 0, // class_data
970 sizeof(SPCanvas),
971 0, // n_preallocs
972 (GInstanceInitFunc)sp_canvas_init,
973 0 // value_table
974 };
975 type = g_type_register_static(GTK_TYPE_WIDGET, "SPCanvas", &info, static_cast<GTypeFlags>(0));
976 }
977 return type;
978 }
980 /**
981 * Class initialization function for SPCanvasClass.
982 */
983 static void
984 sp_canvas_class_init (SPCanvasClass *klass)
985 {
986 GtkObjectClass *object_class = (GtkObjectClass *) klass;
987 GtkWidgetClass *widget_class = (GtkWidgetClass *) klass;
989 canvas_parent_class = (GtkWidgetClass *)gtk_type_class (GTK_TYPE_WIDGET);
991 object_class->destroy = sp_canvas_destroy;
993 widget_class->realize = sp_canvas_realize;
994 widget_class->unrealize = sp_canvas_unrealize;
995 widget_class->size_request = sp_canvas_size_request;
996 widget_class->size_allocate = sp_canvas_size_allocate;
997 widget_class->button_press_event = sp_canvas_button;
998 widget_class->button_release_event = sp_canvas_button;
999 widget_class->motion_notify_event = sp_canvas_motion;
1000 widget_class->scroll_event = sp_canvas_scroll;
1001 widget_class->expose_event = sp_canvas_expose;
1002 widget_class->key_press_event = sp_canvas_key;
1003 widget_class->key_release_event = sp_canvas_key;
1004 widget_class->enter_notify_event = sp_canvas_crossing;
1005 widget_class->leave_notify_event = sp_canvas_crossing;
1006 widget_class->focus_in_event = sp_canvas_focus_in;
1007 widget_class->focus_out_event = sp_canvas_focus_out;
1008 }
1010 /**
1011 * Callback: object initialization for SPCanvas.
1012 */
1013 static void
1014 sp_canvas_init (SPCanvas *canvas)
1015 {
1016 GTK_WIDGET_UNSET_FLAGS (canvas, GTK_NO_WINDOW);
1017 GTK_WIDGET_UNSET_FLAGS (canvas, GTK_DOUBLE_BUFFERED);
1018 GTK_WIDGET_SET_FLAGS (canvas, GTK_CAN_FOCUS);
1020 canvas->pick_event.type = GDK_LEAVE_NOTIFY;
1021 canvas->pick_event.crossing.x = 0;
1022 canvas->pick_event.crossing.y = 0;
1024 /* Create the root item as a special case */
1025 canvas->root = SP_CANVAS_ITEM (gtk_type_new (sp_canvas_group_get_type ()));
1026 canvas->root->canvas = canvas;
1028 gtk_object_ref (GTK_OBJECT (canvas->root));
1029 gtk_object_sink (GTK_OBJECT (canvas->root));
1031 canvas->need_repick = TRUE;
1033 // See comment at in sp-canvas.h.
1034 canvas->gen_all_enter_events = false;
1036 canvas->tiles=NULL;
1037 canvas->tLeft=canvas->tTop=canvas->tRight=canvas->tBottom=0;
1038 canvas->tileH=canvas->tileV=0;
1040 canvas->forced_redraw_count = 0;
1041 canvas->forced_redraw_limit = -1;
1043 #if ENABLE_LCMS
1044 canvas->enable_cms_display_adj = false;
1045 canvas->cms_key = new Glib::ustring("");
1046 #endif // ENABLE_LCMS
1048 canvas->is_scrolling = false;
1050 canvas->watchdog_id = 0;
1051 canvas->watchdog_event = NULL;
1052 canvas->context_snap_delay_active = false;
1053 }
1055 /**
1056 * Convenience function to remove the idle handler of a canvas.
1057 */
1058 static void
1059 remove_idle (SPCanvas *canvas)
1060 {
1061 if (canvas->idle_id) {
1062 gtk_idle_remove (canvas->idle_id);
1063 canvas->idle_id = 0;
1064 }
1065 }
1067 /*
1068 * Removes the transient state of the canvas (idle handler, grabs).
1069 */
1070 static void
1071 shutdown_transients (SPCanvas *canvas)
1072 {
1073 /* We turn off the need_redraw flag, since if the canvas is mapped again
1074 * it will request a redraw anyways. We do not turn off the need_update
1075 * flag, though, because updates are not queued when the canvas remaps
1076 * itself.
1077 */
1078 if (canvas->need_redraw) {
1079 canvas->need_redraw = FALSE;
1080 }
1081 if ( canvas->tiles ) g_free(canvas->tiles);
1082 canvas->tiles=NULL;
1083 canvas->tLeft=canvas->tTop=canvas->tRight=canvas->tBottom=0;
1084 canvas->tileH=canvas->tileV=0;
1086 if (canvas->grabbed_item) {
1087 canvas->grabbed_item = NULL;
1088 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1089 }
1091 remove_idle (canvas);
1092 }
1094 /**
1095 * Destroy handler for SPCanvas.
1096 */
1097 static void
1098 sp_canvas_destroy (GtkObject *object)
1099 {
1100 SPCanvas *canvas = SP_CANVAS (object);
1102 if (canvas->root) {
1103 gtk_object_unref (GTK_OBJECT (canvas->root));
1104 canvas->root = NULL;
1105 }
1107 shutdown_transients (canvas);
1109 if (GTK_OBJECT_CLASS (canvas_parent_class)->destroy)
1110 (* GTK_OBJECT_CLASS (canvas_parent_class)->destroy) (object);
1111 }
1113 static void track_latency(GdkEvent const *event) {
1114 GdkEventLatencyTracker &tracker = GdkEventLatencyTracker::default_tracker();
1115 boost::optional<double> latency = tracker.process(event);
1116 if (latency && *latency > 2.0) {
1117 //g_warning("Event latency reached %f sec (%1.4f)", *latency, tracker.getSkew());
1118 }
1119 }
1121 /**
1122 * Returns new canvas as widget.
1123 */
1124 GtkWidget *
1125 sp_canvas_new_aa (void)
1126 {
1127 SPCanvas *canvas = (SPCanvas *)gtk_type_new (sp_canvas_get_type ());
1129 return (GtkWidget *) canvas;
1130 }
1132 /**
1133 * The canvas widget's realize callback.
1134 */
1135 static void
1136 sp_canvas_realize (GtkWidget *widget)
1137 {
1138 SPCanvas *canvas = SP_CANVAS (widget);
1140 GdkWindowAttr attributes;
1141 attributes.window_type = GDK_WINDOW_CHILD;
1142 attributes.x = widget->allocation.x;
1143 attributes.y = widget->allocation.y;
1144 attributes.width = widget->allocation.width;
1145 attributes.height = widget->allocation.height;
1146 attributes.wclass = GDK_INPUT_OUTPUT;
1147 attributes.visual = gdk_rgb_get_visual ();
1148 attributes.colormap = gdk_rgb_get_cmap ();
1149 attributes.event_mask = (gtk_widget_get_events (widget) |
1150 GDK_EXPOSURE_MASK |
1151 GDK_BUTTON_PRESS_MASK |
1152 GDK_BUTTON_RELEASE_MASK |
1153 GDK_POINTER_MOTION_MASK |
1154 ( HAS_BROKEN_MOTION_HINTS ?
1155 0 : GDK_POINTER_MOTION_HINT_MASK ) |
1156 GDK_PROXIMITY_IN_MASK |
1157 GDK_PROXIMITY_OUT_MASK |
1158 GDK_KEY_PRESS_MASK |
1159 GDK_KEY_RELEASE_MASK |
1160 GDK_ENTER_NOTIFY_MASK |
1161 GDK_LEAVE_NOTIFY_MASK |
1162 GDK_FOCUS_CHANGE_MASK);
1163 gint attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1165 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1166 gdk_window_set_user_data (widget->window, widget);
1168 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1169 if ( prefs->getBool("/options/useextinput/value", true) )
1170 gtk_widget_set_events(widget, attributes.event_mask);
1172 widget->style = gtk_style_attach (widget->style, widget->window);
1174 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1176 canvas->pixmap_gc = gdk_gc_new (SP_CANVAS_WINDOW (canvas));
1177 }
1179 /**
1180 * The canvas widget's unrealize callback.
1181 */
1182 static void
1183 sp_canvas_unrealize (GtkWidget *widget)
1184 {
1185 SPCanvas *canvas = SP_CANVAS (widget);
1187 canvas->current_item = NULL;
1188 canvas->grabbed_item = NULL;
1189 canvas->focused_item = NULL;
1191 shutdown_transients (canvas);
1193 gdk_gc_destroy (canvas->pixmap_gc);
1194 canvas->pixmap_gc = NULL;
1196 if (GTK_WIDGET_CLASS (canvas_parent_class)->unrealize)
1197 (* GTK_WIDGET_CLASS (canvas_parent_class)->unrealize) (widget);
1198 }
1200 /**
1201 * The canvas widget's size_request callback.
1202 */
1203 static void
1204 sp_canvas_size_request (GtkWidget *widget, GtkRequisition *req)
1205 {
1206 static_cast<void>(SP_CANVAS (widget));
1208 req->width = 256;
1209 req->height = 256;
1210 }
1212 /**
1213 * The canvas widget's size_allocate callback.
1214 */
1215 static void
1216 sp_canvas_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
1217 {
1218 SPCanvas *canvas = SP_CANVAS (widget);
1220 /* Schedule redraw of new region */
1221 sp_canvas_resize_tiles(canvas,canvas->x0,canvas->y0,canvas->x0+allocation->width,canvas->y0+allocation->height);
1222 if (allocation->width > widget->allocation.width) {
1223 sp_canvas_request_redraw (canvas,
1224 canvas->x0 + widget->allocation.width,
1225 0,
1226 canvas->x0 + allocation->width,
1227 canvas->y0 + allocation->height);
1228 }
1229 if (allocation->height > widget->allocation.height) {
1230 sp_canvas_request_redraw (canvas,
1231 0,
1232 canvas->y0 + widget->allocation.height,
1233 canvas->x0 + allocation->width,
1234 canvas->y0 + allocation->height);
1235 }
1237 widget->allocation = *allocation;
1239 if (GTK_WIDGET_REALIZED (widget)) {
1240 gdk_window_move_resize (widget->window,
1241 widget->allocation.x, widget->allocation.y,
1242 widget->allocation.width, widget->allocation.height);
1243 }
1244 }
1246 /**
1247 * Helper that emits an event for an item in the canvas, be it the current
1248 * item, grabbed item, or focused item, as appropriate.
1249 */
1250 static int
1251 emit_event (SPCanvas *canvas, GdkEvent *event)
1252 {
1253 guint mask;
1255 if (canvas->grabbed_item) {
1256 switch (event->type) {
1257 case GDK_ENTER_NOTIFY:
1258 mask = GDK_ENTER_NOTIFY_MASK;
1259 break;
1260 case GDK_LEAVE_NOTIFY:
1261 mask = GDK_LEAVE_NOTIFY_MASK;
1262 break;
1263 case GDK_MOTION_NOTIFY:
1264 mask = GDK_POINTER_MOTION_MASK;
1265 break;
1266 case GDK_BUTTON_PRESS:
1267 case GDK_2BUTTON_PRESS:
1268 case GDK_3BUTTON_PRESS:
1269 mask = GDK_BUTTON_PRESS_MASK;
1270 break;
1271 case GDK_BUTTON_RELEASE:
1272 mask = GDK_BUTTON_RELEASE_MASK;
1273 break;
1274 case GDK_KEY_PRESS:
1275 mask = GDK_KEY_PRESS_MASK;
1276 break;
1277 case GDK_KEY_RELEASE:
1278 mask = GDK_KEY_RELEASE_MASK;
1279 break;
1280 case GDK_SCROLL:
1281 mask = GDK_SCROLL;
1282 break;
1283 default:
1284 mask = 0;
1285 break;
1286 }
1288 if (!(mask & canvas->grabbed_event_mask)) return FALSE;
1289 }
1291 /* Convert to world coordinates -- we have two cases because of diferent
1292 * offsets of the fields in the event structures.
1293 */
1295 GdkEvent ev = *event;
1297 switch (ev.type) {
1298 case GDK_ENTER_NOTIFY:
1299 case GDK_LEAVE_NOTIFY:
1300 ev.crossing.x += canvas->x0;
1301 ev.crossing.y += canvas->y0;
1302 break;
1303 case GDK_MOTION_NOTIFY:
1304 case GDK_BUTTON_PRESS:
1305 case GDK_2BUTTON_PRESS:
1306 case GDK_3BUTTON_PRESS:
1307 case GDK_BUTTON_RELEASE:
1308 ev.motion.x += canvas->x0;
1309 ev.motion.y += canvas->y0;
1310 break;
1311 default:
1312 break;
1313 }
1315 /* Choose where we send the event */
1317 /* canvas->current_item becomes NULL in some cases under Win32
1318 ** (e.g. if the pointer leaves the window). So this is a hack that
1319 ** Lauris applied to SP to get around the problem.
1320 */
1321 SPCanvasItem* item = NULL;
1322 if (canvas->grabbed_item && !is_descendant (canvas->current_item, canvas->grabbed_item)) {
1323 item = canvas->grabbed_item;
1324 } else {
1325 item = canvas->current_item;
1326 }
1328 if (canvas->focused_item &&
1329 ((event->type == GDK_KEY_PRESS) ||
1330 (event->type == GDK_KEY_RELEASE) ||
1331 (event->type == GDK_FOCUS_CHANGE))) {
1332 item = canvas->focused_item;
1333 }
1335 /* The event is propagated up the hierarchy (for if someone connected to
1336 * a group instead of a leaf event), and emission is stopped if a
1337 * handler returns TRUE, just like for GtkWidget events.
1338 */
1340 gint finished = FALSE;
1342 while (item && !finished) {
1343 gtk_object_ref (GTK_OBJECT (item));
1344 gtk_signal_emit (GTK_OBJECT (item), item_signals[ITEM_EVENT], &ev, &finished);
1345 SPCanvasItem *parent = item->parent;
1346 gtk_object_unref (GTK_OBJECT (item));
1347 item = parent;
1348 }
1350 return finished;
1351 }
1353 /**
1354 * Helper that re-picks the current item in the canvas, based on the event's
1355 * coordinates and emits enter/leave events for items as appropriate.
1356 */
1357 static int
1358 pick_current_item (SPCanvas *canvas, GdkEvent *event)
1359 {
1360 int button_down = 0;
1361 double x, y;
1363 if (!canvas->root) // canvas may have already be destroyed by closing desktop durring interrupted display!
1364 return FALSE;
1366 int retval = FALSE;
1368 if (canvas->gen_all_enter_events == false) {
1369 // If a button is down, we'll perform enter and leave events on the
1370 // current item, but not enter on any other item. This is more or
1371 // less like X pointer grabbing for canvas items.
1372 //
1373 button_down = canvas->state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1374 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK | GDK_BUTTON5_MASK);
1376 if (!button_down) canvas->left_grabbed_item = FALSE;
1377 }
1379 /* Save the event in the canvas. This is used to synthesize enter and
1380 * leave events in case the current item changes. It is also used to
1381 * re-pick the current item if the current one gets deleted. Also,
1382 * synthesize an enter event.
1383 */
1384 if (event != &canvas->pick_event) {
1385 if ((event->type == GDK_MOTION_NOTIFY) || (event->type == GDK_BUTTON_RELEASE)) {
1386 /* these fields have the same offsets in both types of events */
1388 canvas->pick_event.crossing.type = GDK_ENTER_NOTIFY;
1389 canvas->pick_event.crossing.window = event->motion.window;
1390 canvas->pick_event.crossing.send_event = event->motion.send_event;
1391 canvas->pick_event.crossing.subwindow = NULL;
1392 canvas->pick_event.crossing.x = event->motion.x;
1393 canvas->pick_event.crossing.y = event->motion.y;
1394 canvas->pick_event.crossing.mode = GDK_CROSSING_NORMAL;
1395 canvas->pick_event.crossing.detail = GDK_NOTIFY_NONLINEAR;
1396 canvas->pick_event.crossing.focus = FALSE;
1397 canvas->pick_event.crossing.state = event->motion.state;
1399 /* these fields don't have the same offsets in both types of events */
1401 if (event->type == GDK_MOTION_NOTIFY) {
1402 canvas->pick_event.crossing.x_root = event->motion.x_root;
1403 canvas->pick_event.crossing.y_root = event->motion.y_root;
1404 } else {
1405 canvas->pick_event.crossing.x_root = event->button.x_root;
1406 canvas->pick_event.crossing.y_root = event->button.y_root;
1407 }
1408 } else {
1409 canvas->pick_event = *event;
1410 }
1411 }
1413 /* Don't do anything else if this is a recursive call */
1414 if (canvas->in_repick) return retval;
1416 /* LeaveNotify means that there is no current item, so we don't look for one */
1417 if (canvas->pick_event.type != GDK_LEAVE_NOTIFY) {
1418 /* these fields don't have the same offsets in both types of events */
1420 if (canvas->pick_event.type == GDK_ENTER_NOTIFY) {
1421 x = canvas->pick_event.crossing.x;
1422 y = canvas->pick_event.crossing.y;
1423 } else {
1424 x = canvas->pick_event.motion.x;
1425 y = canvas->pick_event.motion.y;
1426 }
1428 /* world coords */
1429 x += canvas->x0;
1430 y += canvas->y0;
1432 /* find the closest item */
1433 if (canvas->root->flags & SP_CANVAS_ITEM_VISIBLE) {
1434 sp_canvas_item_invoke_point (canvas->root, Geom::Point(x, y), &canvas->new_current_item);
1435 } else {
1436 canvas->new_current_item = NULL;
1437 }
1438 } else {
1439 canvas->new_current_item = NULL;
1440 }
1442 if ((canvas->new_current_item == canvas->current_item) && !canvas->left_grabbed_item) {
1443 return retval; /* current item did not change */
1444 }
1446 /* Synthesize events for old and new current items */
1448 if ((canvas->new_current_item != canvas->current_item)
1449 && (canvas->current_item != NULL)
1450 && !canvas->left_grabbed_item) {
1451 GdkEvent new_event;
1452 SPCanvasItem *item;
1454 item = canvas->current_item;
1456 new_event = canvas->pick_event;
1457 new_event.type = GDK_LEAVE_NOTIFY;
1459 new_event.crossing.detail = GDK_NOTIFY_ANCESTOR;
1460 new_event.crossing.subwindow = NULL;
1461 canvas->in_repick = TRUE;
1462 retval = emit_event (canvas, &new_event);
1463 canvas->in_repick = FALSE;
1464 }
1466 if (canvas->gen_all_enter_events == false) {
1467 // new_current_item may have been set to NULL during the call to
1468 // emit_event() above
1469 if ((canvas->new_current_item != canvas->current_item) && button_down) {
1470 canvas->left_grabbed_item = TRUE;
1471 return retval;
1472 }
1473 }
1475 /* Handle the rest of cases */
1477 canvas->left_grabbed_item = FALSE;
1478 canvas->current_item = canvas->new_current_item;
1480 if (canvas->current_item != NULL) {
1481 GdkEvent new_event;
1483 new_event = canvas->pick_event;
1484 new_event.type = GDK_ENTER_NOTIFY;
1485 new_event.crossing.detail = GDK_NOTIFY_ANCESTOR;
1486 new_event.crossing.subwindow = NULL;
1487 retval = emit_event (canvas, &new_event);
1488 }
1490 return retval;
1491 }
1493 /**
1494 * Button event handler for the canvas.
1495 */
1496 static gint
1497 sp_canvas_button (GtkWidget *widget, GdkEventButton *event)
1498 {
1499 SPCanvas *canvas = SP_CANVAS (widget);
1500 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1502 int retval = FALSE;
1504 /* dispatch normally regardless of the event's window if an item has
1505 has a pointer grab in effect */
1506 if (!canvas->grabbed_item &&
1507 event->window != SP_CANVAS_WINDOW (canvas))
1508 return retval;
1510 int mask;
1511 switch (event->button) {
1512 case 1:
1513 mask = GDK_BUTTON1_MASK;
1514 break;
1515 case 2:
1516 mask = GDK_BUTTON2_MASK;
1517 break;
1518 case 3:
1519 mask = GDK_BUTTON3_MASK;
1520 break;
1521 case 4:
1522 mask = GDK_BUTTON4_MASK;
1523 break;
1524 case 5:
1525 mask = GDK_BUTTON5_MASK;
1526 break;
1527 default:
1528 mask = 0;
1529 }
1531 switch (event->type) {
1532 case GDK_BUTTON_PRESS:
1533 case GDK_2BUTTON_PRESS:
1534 case GDK_3BUTTON_PRESS:
1535 if (dt) {
1536 // Snapping will be on hold if we're moving the mouse at high speeds. When starting
1537 // drawing a new shape we really should snap though.
1538 dt->namedview->snap_manager.snapprefs.setSnapPostponedGlobally(false);
1539 }
1541 /* Pick the current item as if the button were not pressed, and
1542 * then process the event.
1543 */
1544 canvas->state = event->state;
1545 pick_current_item (canvas, (GdkEvent *) event);
1546 canvas->state ^= mask;
1547 retval = emit_event (canvas, (GdkEvent *) event);
1548 break;
1550 case GDK_BUTTON_RELEASE:
1551 sp_canvas_snap_watchdog_callback(canvas); // If we have any pending snapping action, then invoke it now
1553 /* Process the event as if the button were pressed, then repick
1554 * after the button has been released
1555 */
1556 canvas->state = event->state;
1557 retval = emit_event (canvas, (GdkEvent *) event);
1558 event->state ^= mask;
1559 canvas->state = event->state;
1560 pick_current_item (canvas, (GdkEvent *) event);
1561 event->state ^= mask;
1563 break;
1565 default:
1566 g_assert_not_reached ();
1567 }
1569 return retval;
1570 }
1572 /**
1573 * Scroll event handler for the canvas.
1574 *
1575 * \todo FIXME: generate motion events to re-select items.
1576 */
1577 static gint
1578 sp_canvas_scroll (GtkWidget *widget, GdkEventScroll *event)
1579 {
1580 return emit_event (SP_CANVAS (widget), (GdkEvent *) event);
1581 }
1583 static inline void request_motions(GdkWindow *w, GdkEventMotion *event) {
1584 gdk_window_get_pointer(w, NULL, NULL, NULL);
1585 #if HAS_GDK_EVENT_REQUEST_MOTIONS
1586 gdk_event_request_motions(event);
1587 #endif
1588 }
1590 /**
1591 * Motion event handler for the canvas.
1592 */
1593 static int
1594 sp_canvas_motion (GtkWidget *widget, GdkEventMotion *event)
1595 {
1596 static guint32 prev_time;
1597 static boost::optional<Geom::Point> prev_pos;
1599 int status;
1600 SPCanvas *canvas = SP_CANVAS (widget);
1602 track_latency((GdkEvent *)event);
1604 if (event->window != SP_CANVAS_WINDOW (canvas))
1605 return FALSE;
1607 if (canvas->pixmap_gc == NULL) // canvas being deleted
1608 return FALSE;
1610 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1612 // Snapping occurs when dragging with the left mouse button down, or when hovering e.g. in the pen tool with left mouse button up
1613 bool const c1 = event->state & GDK_BUTTON2_MASK; // We shouldn't hold back any events when other mouse buttons have been
1614 bool const c2 = event->state & GDK_BUTTON3_MASK; // pressed, e.g. when scrolling with the middle mouse button; if we do then
1615 // Inkscape will get stuck in an unresponsive state
1617 if (canvas->context_snap_delay_active && !c1 && !c2 && dt && dt->namedview->snap_manager.snapprefs.getSnapEnabledGlobally()) {
1618 // Snap when speed drops below e.g. 0.02 px/msec, or when no motion events have occurred for some period.
1619 // i.e. snap when we're at stand still. A speed threshold enforces snapping for tablets, which might never
1620 // be fully at stand still and might keep spitting out motion events.
1621 dt->namedview->snap_manager.snapprefs.setSnapPostponedGlobally(true); // put snapping on hold
1623 Geom::Point event_pos(event->x, event->y);
1624 guint32 event_t = gdk_event_get_time ( (GdkEvent *) event );
1626 if (prev_pos) {
1627 Geom::Coord dist = Geom::L2(event_pos - *prev_pos);
1628 guint32 delta_t = event_t - prev_time;
1629 gdouble speed = delta_t > 0 ? dist/delta_t : 1000;
1630 if (speed > 0.02) { // Jitter threshold, might be needed for tablets
1631 // We're moving fast, so postpone any snapping until the next GDK_MOTION_NOTIFY event. We
1632 // will keep on postponing the snapping as long as the speed is high.
1633 // We must snap at some point in time though, so set a watchdog timer at some time from
1634 // now, just in case there's no future motion event that drops under the speed limit (when
1635 // stopping abruptly)
1636 sp_canvas_snap_watchdog_kill(canvas);
1637 sp_canvas_snap_watchdog_set(canvas, event); // watchdog is reset, i.e. pushed forward in time
1638 // If the watchdog expires before a new motion event is received, we will snap (as explained
1639 // above). This means however that when the timer is too short, we will always snap and that the
1640 // speed threshold is ineffective. In the extreme case the delay is set to zero, and snapping will
1641 // be immediate, as it used to be in the old days ;-).
1642 } else { // Speed is very low, so we're virtually at stand still
1643 // But if we're really standing still, then we should snap now. We could use some low-pass filtering,
1644 // otherwise snapping occurs for each jitter movement. For this filtering we'll leave the watchdog to expire,
1645 // snap, and set a new watchdog again.
1646 if (canvas->watchdog_id == 0) { // no watchdog has been set
1647 // it might have already expired, so we'll set a new one; the snapping frequency will be limited by this
1648 sp_canvas_snap_watchdog_set(canvas, event);
1649 } // else: watchdog has been set before and we'll wait for it to expire
1650 }
1651 } else {
1652 // This is the first GDK_MOTION_NOTIFY event, so postpone snapping and set the watchdog
1653 sp_canvas_snap_watchdog_set(canvas, event);
1654 }
1656 prev_pos = event_pos;
1657 prev_time = event_t;
1658 }
1660 canvas->state = event->state;
1661 pick_current_item (canvas, (GdkEvent *) event);
1662 status = emit_event (canvas, (GdkEvent *) event);
1663 if (event->is_hint) {
1664 request_motions(widget->window, event);
1665 }
1667 return status;
1668 }
1670 gboolean sp_canvas_snap_watchdog_callback(gpointer data)
1671 {
1672 // Snap NOW! For this the "postponed" flag will be reset and an the last motion event will be repeated
1673 SPCanvas *canvas = reinterpret_cast<SPCanvas *>(data);
1674 if (!canvas->watchdog_event) {
1675 // This might occur when this method is called directly, i.e. not through the timer
1676 return FALSE;
1677 }
1679 SPDesktop *dt = SP_ACTIVE_DESKTOP;
1680 if (dt) {
1681 dt->namedview->snap_manager.snapprefs.setSnapPostponedGlobally(false);
1682 }
1684 ((GdkEventMotion *)canvas->watchdog_event)->time = GDK_CURRENT_TIME;
1685 emit_event(canvas, canvas->watchdog_event);
1686 gdk_event_free(canvas->watchdog_event);
1687 canvas->watchdog_event = NULL;
1688 canvas->watchdog_id = 0;
1690 return FALSE;
1691 }
1693 void sp_canvas_snap_watchdog_set(SPCanvas *canvas, GdkEventMotion *event)
1694 {
1695 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1696 double value = prefs->getDoubleLimited("/options/snapdelay/value", 0, 0, 1000);
1697 g_assert(canvas->watchdog_id == 0);
1698 canvas->watchdog_id = g_timeout_add(value, &sp_canvas_snap_watchdog_callback, canvas);
1699 g_assert(canvas->watchdog_event == NULL);
1700 canvas->watchdog_event = gdk_event_copy( (GdkEvent *) event);
1701 }
1703 void sp_canvas_snap_watchdog_kill(SPCanvas *canvas)
1704 {
1705 if (canvas->watchdog_id) {
1706 g_source_remove(canvas->watchdog_id); // Kill the watchdog
1707 canvas->watchdog_id = 0;
1708 }
1710 if (canvas->watchdog_event) {
1711 gdk_event_free(canvas->watchdog_event);
1712 canvas->watchdog_event = NULL;
1713 }
1714 }
1716 void sp_canvas_set_snap_delay_active(SPCanvas *canvas, bool snapping)
1717 {
1718 // Only when canvas->context_snap_delay_active has been set, Inkscape will know that snapping is active
1719 // and will delay any snapping events (but only when asked to through the preferences)
1721 // When snapping is being delayed, then that will also mean that at some point the last event
1722 // might be re-triggered. This should only occur when Inkscape is still in the same tool or context,
1723 // and even more specifically, the tool should even be in the same state. If for example snapping is being delayed while
1724 // creating a rectangle, then the rect-context will be active and it will be in the "dragging" state
1725 // (see the static boolean variable "dragging" in the sp_rect_context_root_handler). The procedure is
1726 // as follows: call sp_canvas_set_snap_delay_active(*, TRUE) when entering the "dragging" state, which will delay
1727 // snapping from that moment on, and call sp_canvas_set_snap_delay_active(*, FALSE) when leaving the "dragging"
1728 // state. This last call will also make sure that any pending snap events will be canceled.
1730 if (!canvas) {
1731 g_warning("sp_canvas_set_snap_delay_active() has been called without providing a canvas!");
1732 return;
1733 }
1735 if (canvas->context_snap_delay_active == snapping) {
1736 g_warning("Snapping was already allowed or disallowed! This is a bug, please report it.");
1737 }
1739 canvas->context_snap_delay_active = snapping;
1741 if (snapping == false) {
1742 sp_canvas_snap_watchdog_kill(canvas); // kill any pending snapping events
1743 }
1744 }
1746 static void
1747 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)
1748 {
1749 GtkWidget *widget = GTK_WIDGET (canvas);
1751 SPCanvasBuf buf;
1752 if (canvas->rendermode != Inkscape::RENDERMODE_OUTLINE) {
1753 buf.buf = nr_pixelstore_256K_new (FALSE, 0);
1754 } else {
1755 buf.buf = nr_pixelstore_1M_new (FALSE, 0);
1756 }
1758 // Mark the region clean
1759 sp_canvas_mark_rect(canvas, x0, y0, x1, y1, 0);
1761 buf.buf_rowstride = sw * 4;
1762 buf.rect.x0 = x0;
1763 buf.rect.y0 = y0;
1764 buf.rect.x1 = x1;
1765 buf.rect.y1 = y1;
1766 buf.visible_rect.x0 = draw_x1;
1767 buf.visible_rect.y0 = draw_y1;
1768 buf.visible_rect.x1 = draw_x2;
1769 buf.visible_rect.y1 = draw_y2;
1770 GdkColor *color = &widget->style->bg[GTK_STATE_NORMAL];
1771 buf.bg_color = (((color->red & 0xff00) << 8)
1772 | (color->green & 0xff00)
1773 | (color->blue >> 8));
1774 buf.is_empty = true;
1776 buf.ct = nr_create_cairo_context_canvasbuf (&(buf.visible_rect), &buf);
1778 if (canvas->root->flags & SP_CANVAS_ITEM_VISIBLE) {
1779 SP_CANVAS_ITEM_GET_CLASS (canvas->root)->render (canvas->root, &buf);
1780 }
1782 #if ENABLE_LCMS
1783 cmsHTRANSFORM transf = 0;
1784 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1785 bool fromDisplay = prefs->getBool( "/options/displayprofile/from_display");
1786 if ( fromDisplay ) {
1787 transf = Inkscape::colorprofile_get_display_per( canvas->cms_key ? *(canvas->cms_key) : "" );
1788 } else {
1789 transf = Inkscape::colorprofile_get_display_transform();
1790 }
1791 #endif // ENABLE_LCMS
1793 if (buf.is_empty) {
1794 #if ENABLE_LCMS
1795 if ( transf && canvas->enable_cms_display_adj ) {
1796 cmsDoTransform( transf, &buf.bg_color, &buf.bg_color, 1 );
1797 }
1798 #endif // ENABLE_LCMS
1799 gdk_rgb_gc_set_foreground (canvas->pixmap_gc, buf.bg_color);
1800 gdk_draw_rectangle (SP_CANVAS_WINDOW (canvas),
1801 canvas->pixmap_gc,
1802 TRUE,
1803 x0 - canvas->x0, y0 - canvas->y0,
1804 x1 - x0, y1 - y0);
1805 } else {
1807 #if ENABLE_LCMS
1808 if ( transf && canvas->enable_cms_display_adj ) {
1809 for ( gint yy = 0; yy < (y1 - y0); yy++ ) {
1810 guchar* p = buf.buf + (buf.buf_rowstride * yy);
1811 cmsDoTransform( transf, p, p, (x1 - x0) );
1812 }
1813 }
1814 #endif // ENABLE_LCMS
1816 // Now we only need to output the prepared pixmap to the actual screen, and this define chooses one
1817 // of the two ways to do it. The cairo way is direct and straightforward, but unfortunately
1818 // noticeably slower. I asked Carl Worth but he was unable so far to suggest any specific reason
1819 // for this slowness. So, for now we use the oldish method: squeeze out 32bpp buffer to 24bpp and
1820 // use gdk_draw_rgb_image_dithalign, for unfortunately gdk can only handle 24 bpp, which cairo
1821 // cannot handle at all. Still, this way is currently faster even despite the blit with squeeze.
1823 ///#define CANVAS_OUTPUT_VIA_CAIRO
1825 #ifdef CANVAS_OUTPUT_VIA_CAIRO
1827 buf.cst = cairo_image_surface_create_for_data (
1828 buf.buf,
1829 CAIRO_FORMAT_ARGB32, // unpacked, i.e. 32 bits! one byte is unused
1830 x1 - x0, y1 - y0,
1831 buf.buf_rowstride
1832 );
1833 cairo_t *window_ct = gdk_cairo_create(SP_CANVAS_WINDOW (canvas));
1834 cairo_set_source_surface (window_ct, buf.cst, x0 - canvas->x0, y0 - canvas->y0);
1835 cairo_paint (window_ct);
1836 cairo_destroy (window_ct);
1837 cairo_surface_finish (buf.cst);
1838 cairo_surface_destroy (buf.cst);
1840 #else
1842 NRPixBlock b3;
1843 nr_pixblock_setup_fast (&b3, NR_PIXBLOCK_MODE_R8G8B8, x0, y0, x1, y1, TRUE);
1845 NRPixBlock b4;
1846 nr_pixblock_setup_extern (&b4, NR_PIXBLOCK_MODE_R8G8B8A8P, x0, y0, x1, y1,
1847 buf.buf,
1848 buf.buf_rowstride,
1849 FALSE, FALSE);
1851 // this does the 32->24 squishing, using an assembler routine:
1852 nr_blit_pixblock_pixblock (&b3, &b4);
1854 gdk_draw_rgb_image_dithalign (SP_CANVAS_WINDOW (canvas),
1855 canvas->pixmap_gc,
1856 x0 - canvas->x0, y0 - canvas->y0,
1857 x1 - x0, y1 - y0,
1858 GDK_RGB_DITHER_MAX,
1859 NR_PIXBLOCK_PX(&b3),
1860 sw * 3,
1861 x0 - canvas->x0, y0 - canvas->y0);
1863 nr_pixblock_release (&b3);
1864 nr_pixblock_release (&b4);
1865 #endif
1866 }
1868 cairo_surface_t *cst = cairo_get_target(buf.ct);
1869 cairo_destroy (buf.ct);
1870 cairo_surface_finish (cst);
1871 cairo_surface_destroy (cst);
1873 if (canvas->rendermode != Inkscape::RENDERMODE_OUTLINE) {
1874 nr_pixelstore_256K_free (buf.buf);
1875 } else {
1876 nr_pixelstore_1M_free (buf.buf);
1877 }
1878 }
1880 struct PaintRectSetup {
1881 SPCanvas* canvas;
1882 NRRectL big_rect;
1883 GTimeVal start_time;
1884 int max_pixels;
1885 Geom::Point mouse_loc;
1886 };
1888 /**
1889 * Paint the given rect, recursively subdividing the region until it is the size of a single
1890 * buffer.
1891 *
1892 * @return true if the drawing completes
1893 */
1894 static int
1895 sp_canvas_paint_rect_internal (PaintRectSetup const *setup, NRRectL this_rect)
1896 {
1897 GTimeVal now;
1898 g_get_current_time (&now);
1900 glong elapsed = (now.tv_sec - setup->start_time.tv_sec) * 1000000
1901 + (now.tv_usec - setup->start_time.tv_usec);
1903 // Allow only very fast buffers to be run together;
1904 // as soon as the total redraw time exceeds 1ms, cancel;
1905 // this returns control to the idle loop and allows Inkscape to process user input
1906 // (potentially interrupting the redraw); as soon as Inkscape has some more idle time,
1907 // it will get back and finish painting what remains to paint.
1908 if (elapsed > 1000) {
1910 // Interrupting redraw isn't always good.
1911 // For example, when you drag one node of a big path, only the buffer containing
1912 // the mouse cursor will be redrawn again and again, and the rest of the path
1913 // will remain stale because Inkscape never has enough idle time to redraw all
1914 // of the screen. To work around this, such operations set a forced_redraw_limit > 0.
1915 // If this limit is set, and if we have aborted redraw more times than is allowed,
1916 // interrupting is blocked and we're forced to redraw full screen once
1917 // (after which we can again interrupt forced_redraw_limit times).
1918 if (setup->canvas->forced_redraw_limit < 0 ||
1919 setup->canvas->forced_redraw_count < setup->canvas->forced_redraw_limit) {
1921 if (setup->canvas->forced_redraw_limit != -1) {
1922 setup->canvas->forced_redraw_count++;
1923 }
1925 return false;
1926 }
1927 }
1929 // Find the optimal buffer dimensions
1930 int bw = this_rect.x1 - this_rect.x0;
1931 int bh = this_rect.y1 - this_rect.y0;
1932 if ((bw < 1) || (bh < 1))
1933 return 0;
1935 if (bw * bh < setup->max_pixels) {
1936 // We are small enough
1937 sp_canvas_paint_single_buffer (setup->canvas,
1938 this_rect.x0, this_rect.y0,
1939 this_rect.x1, this_rect.y1,
1940 setup->big_rect.x0, setup->big_rect.y0,
1941 setup->big_rect.x1, setup->big_rect.y1, bw);
1942 return 1;
1943 }
1945 NRRectL lo = this_rect;
1946 NRRectL hi = this_rect;
1948 /*
1949 This test determines the redraw strategy:
1951 bw < bh (strips mode) splits across the smaller dimension of the rect and therefore (on
1952 horizontally-stretched windows) results in redrawing in horizontal strips (from cursor point, in
1953 both directions if the cursor is in the middle). This is traditional for Inkscape since old days,
1954 and seems to be faster for drawings with many smaller objects at zoom-out.
1956 bw > bh (chunks mode) splits across the larger dimension of the rect and therefore paints in
1957 almost-square chunks, again from the cursor point. It's sometimes faster for drawings with few slow
1958 (e.g. blurred) objects crossing the entire screen. It also appears to be somewhat psychologically
1959 faster.
1961 The default for now is the strips mode.
1962 */
1963 if (bw < bh || bh < 2 * TILE_SIZE) {
1964 // to correctly calculate the mean of two ints, we need to sum them into a larger int type
1965 int mid = ((long long) this_rect.x0 + (long long) this_rect.x1) / 2;
1966 // Make sure that mid lies on a tile boundary
1967 mid = (mid / TILE_SIZE) * TILE_SIZE;
1969 lo.x1 = mid;
1970 hi.x0 = mid;
1972 if (setup->mouse_loc[Geom::X] < mid) {
1973 // Always paint towards the mouse first
1974 return sp_canvas_paint_rect_internal(setup, lo)
1975 && sp_canvas_paint_rect_internal(setup, hi);
1976 } else {
1977 return sp_canvas_paint_rect_internal(setup, hi)
1978 && sp_canvas_paint_rect_internal(setup, lo);
1979 }
1980 } else {
1981 // to correctly calculate the mean of two ints, we need to sum them into a larger int type
1982 int mid = ((long long) this_rect.y0 + (long long) this_rect.y1) / 2;
1983 // Make sure that mid lies on a tile boundary
1984 mid = (mid / TILE_SIZE) * TILE_SIZE;
1986 lo.y1 = mid;
1987 hi.y0 = mid;
1989 if (setup->mouse_loc[Geom::Y] < mid) {
1990 // Always paint towards the mouse first
1991 return sp_canvas_paint_rect_internal(setup, lo)
1992 && sp_canvas_paint_rect_internal(setup, hi);
1993 } else {
1994 return sp_canvas_paint_rect_internal(setup, hi)
1995 && sp_canvas_paint_rect_internal(setup, lo);
1996 }
1997 }
1998 }
2001 /**
2002 * Helper that draws a specific rectangular part of the canvas.
2003 *
2004 * @return true if the rectangle painting succeeds.
2005 */
2006 static bool
2007 sp_canvas_paint_rect (SPCanvas *canvas, int xx0, int yy0, int xx1, int yy1)
2008 {
2009 g_return_val_if_fail (!canvas->need_update, false);
2011 NRRectL rect;
2012 rect.x0 = xx0;
2013 rect.x1 = xx1;
2014 rect.y0 = yy0;
2015 rect.y1 = yy1;
2017 // Clip rect-to-draw by the current visible area
2018 rect.x0 = MAX (rect.x0, canvas->x0);
2019 rect.y0 = MAX (rect.y0, canvas->y0);
2020 rect.x1 = MIN (rect.x1, canvas->x0/*draw_x1*/ + GTK_WIDGET (canvas)->allocation.width);
2021 rect.y1 = MIN (rect.y1, canvas->y0/*draw_y1*/ + GTK_WIDGET (canvas)->allocation.height);
2023 #ifdef DEBUG_REDRAW
2024 // paint the area to redraw yellow
2025 gdk_rgb_gc_set_foreground (canvas->pixmap_gc, 0xFFFF00);
2026 gdk_draw_rectangle (SP_CANVAS_WINDOW (canvas),
2027 canvas->pixmap_gc,
2028 TRUE,
2029 rect.x0 - canvas->x0, rect.y0 - canvas->y0,
2030 rect.x1 - rect.x0, rect.y1 - rect.y0);
2031 #endif
2033 PaintRectSetup setup;
2035 setup.canvas = canvas;
2036 setup.big_rect = rect;
2038 // Save the mouse location
2039 gint x, y;
2040 gdk_window_get_pointer (GTK_WIDGET(canvas)->window, &x, &y, NULL);
2041 setup.mouse_loc = sp_canvas_window_to_world (canvas, Geom::Point(x,y));
2043 if (canvas->rendermode != Inkscape::RENDERMODE_OUTLINE) {
2044 // use 256K as a compromise to not slow down gradients
2045 // 256K is the cached buffer and we need 4 channels
2046 setup.max_pixels = 65536; // 256K/4
2047 } else {
2048 // paths only, so 1M works faster
2049 // 1M is the cached buffer and we need 4 channels
2050 setup.max_pixels = 262144;
2051 }
2053 // Start the clock
2054 g_get_current_time(&(setup.start_time));
2056 // Go
2057 return sp_canvas_paint_rect_internal(&setup, rect);
2058 }
2060 /**
2061 * Force a full redraw after a specified number of interrupted redraws
2062 */
2063 void
2064 sp_canvas_force_full_redraw_after_interruptions(SPCanvas *canvas, unsigned int count) {
2065 g_return_if_fail(canvas != NULL);
2067 canvas->forced_redraw_limit = count;
2068 canvas->forced_redraw_count = 0;
2069 }
2071 /**
2072 * End forced full redraw requests
2073 */
2074 void
2075 sp_canvas_end_forced_full_redraws(SPCanvas *canvas) {
2076 g_return_if_fail(canvas != NULL);
2078 canvas->forced_redraw_limit = -1;
2079 }
2081 /**
2082 * The canvas widget's expose callback.
2083 */
2084 static gint
2085 sp_canvas_expose (GtkWidget *widget, GdkEventExpose *event)
2086 {
2087 SPCanvas *canvas = SP_CANVAS (widget);
2089 if (!GTK_WIDGET_DRAWABLE (widget) ||
2090 (event->window != SP_CANVAS_WINDOW (canvas)))
2091 return FALSE;
2093 int n_rects;
2094 GdkRectangle *rects;
2095 gdk_region_get_rectangles (event->region, &rects, &n_rects);
2097 for (int i = 0; i < n_rects; i++) {
2098 NRRectL rect;
2100 rect.x0 = rects[i].x + canvas->x0;
2101 rect.y0 = rects[i].y + canvas->y0;
2102 rect.x1 = rect.x0 + rects[i].width;
2103 rect.y1 = rect.y0 + rects[i].height;
2105 sp_canvas_request_redraw (canvas, rect.x0, rect.y0, rect.x1, rect.y1);
2106 }
2108 if (n_rects > 0)
2109 g_free (rects);
2111 return FALSE;
2112 }
2114 /**
2115 * The canvas widget's keypress callback.
2116 */
2117 static gint
2118 sp_canvas_key (GtkWidget *widget, GdkEventKey *event)
2119 {
2120 return emit_event (SP_CANVAS (widget), (GdkEvent *) event);
2121 }
2123 /**
2124 * Crossing event handler for the canvas.
2125 */
2126 static gint
2127 sp_canvas_crossing (GtkWidget *widget, GdkEventCrossing *event)
2128 {
2129 SPCanvas *canvas = SP_CANVAS (widget);
2131 if (event->window != SP_CANVAS_WINDOW (canvas))
2132 return FALSE;
2134 canvas->state = event->state;
2135 return pick_current_item (canvas, (GdkEvent *) event);
2136 }
2138 /**
2139 * Focus in handler for the canvas.
2140 */
2141 static gint
2142 sp_canvas_focus_in (GtkWidget *widget, GdkEventFocus *event)
2143 {
2144 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
2146 SPCanvas *canvas = SP_CANVAS (widget);
2148 if (canvas->focused_item) {
2149 return emit_event (canvas, (GdkEvent *) event);
2150 } else {
2151 return FALSE;
2152 }
2153 }
2155 /**
2156 * Focus out handler for the canvas.
2157 */
2158 static gint
2159 sp_canvas_focus_out (GtkWidget *widget, GdkEventFocus *event)
2160 {
2161 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
2163 SPCanvas *canvas = SP_CANVAS (widget);
2165 if (canvas->focused_item)
2166 return emit_event (canvas, (GdkEvent *) event);
2167 else
2168 return FALSE;
2169 }
2171 /**
2172 * Helper that repaints the areas in the canvas that need it.
2173 *
2174 * @return true if all the dirty parts have been redrawn
2175 */
2176 static int
2177 paint (SPCanvas *canvas)
2178 {
2179 if (canvas->need_update) {
2180 sp_canvas_item_invoke_update (canvas->root, Geom::identity(), 0);
2181 canvas->need_update = FALSE;
2182 }
2184 if (!canvas->need_redraw)
2185 return TRUE;
2187 Gdk::Region to_paint;
2189 for (int j=canvas->tTop; j<canvas->tBottom; j++) {
2190 for (int i=canvas->tLeft; i<canvas->tRight; i++) {
2191 int tile_index = (i - canvas->tLeft) + (j - canvas->tTop)*canvas->tileH;
2193 if ( canvas->tiles[tile_index] ) { // if this tile is dirtied (nonzero)
2194 to_paint.union_with_rect(Gdk::Rectangle(i*TILE_SIZE, j*TILE_SIZE,
2195 TILE_SIZE, TILE_SIZE));
2196 }
2198 }
2199 }
2201 if (!to_paint.empty()) {
2202 Glib::ArrayHandle<Gdk::Rectangle> rect = to_paint.get_rectangles();
2203 typedef Glib::ArrayHandle<Gdk::Rectangle>::const_iterator Iter;
2204 for (Iter i=rect.begin(); i != rect.end(); ++i) {
2205 int x0 = (*i).get_x();
2206 int y0 = (*i).get_y();
2207 int x1 = x0 + (*i).get_width();
2208 int y1 = y0 + (*i).get_height();
2209 if (!sp_canvas_paint_rect(canvas, x0, y0, x1, y1)) {
2210 // Aborted
2211 return FALSE;
2212 };
2213 }
2214 }
2216 canvas->need_redraw = FALSE;
2218 // we've had a full unaborted redraw, reset the full redraw counter
2219 if (canvas->forced_redraw_limit != -1) {
2220 canvas->forced_redraw_count = 0;
2221 }
2223 return TRUE;
2224 }
2226 /**
2227 * Helper that invokes update, paint, and repick on canvas.
2228 */
2229 static int
2230 do_update (SPCanvas *canvas)
2231 {
2232 if (!canvas->root || !canvas->pixmap_gc) // canvas may have already be destroyed by closing desktop durring interrupted display!
2233 return TRUE;
2235 /* Cause the update if necessary */
2236 if (canvas->need_update) {
2237 sp_canvas_item_invoke_update (canvas->root, Geom::identity(), 0);
2238 canvas->need_update = FALSE;
2239 }
2241 /* Paint if able to */
2242 if (GTK_WIDGET_DRAWABLE (canvas)) {
2243 return paint (canvas);
2244 }
2246 /* Pick new current item */
2247 while (canvas->need_repick) {
2248 canvas->need_repick = FALSE;
2249 pick_current_item (canvas, &canvas->pick_event);
2250 }
2252 return TRUE;
2253 }
2255 /**
2256 * Idle handler for the canvas that deals with pending updates and redraws.
2257 */
2258 static gint
2259 idle_handler (gpointer data)
2260 {
2261 GDK_THREADS_ENTER ();
2263 SPCanvas *canvas = SP_CANVAS (data);
2265 int const ret = do_update (canvas);
2267 if (ret) {
2268 /* Reset idle id */
2269 canvas->idle_id = 0;
2270 }
2272 GDK_THREADS_LEAVE ();
2274 return !ret;
2275 }
2277 /**
2278 * Convenience function to add an idle handler to a canvas.
2279 */
2280 static void
2281 add_idle (SPCanvas *canvas)
2282 {
2283 if (canvas->idle_id != 0)
2284 return;
2286 canvas->idle_id = gtk_idle_add_priority (sp_canvas_update_priority, idle_handler, canvas);
2287 }
2289 /**
2290 * Returns the root group of the specified canvas.
2291 */
2292 SPCanvasGroup *
2293 sp_canvas_root (SPCanvas *canvas)
2294 {
2295 g_return_val_if_fail (canvas != NULL, NULL);
2296 g_return_val_if_fail (SP_IS_CANVAS (canvas), NULL);
2298 return SP_CANVAS_GROUP (canvas->root);
2299 }
2301 /**
2302 * Scrolls canvas to specific position (cx and cy are measured in screen pixels)
2303 */
2304 void
2305 sp_canvas_scroll_to (SPCanvas *canvas, double cx, double cy, unsigned int clear, bool is_scrolling)
2306 {
2307 g_return_if_fail (canvas != NULL);
2308 g_return_if_fail (SP_IS_CANVAS (canvas));
2310 int ix = (int) round(cx); // ix and iy are the new canvas coordinates (integer screen pixels)
2311 int iy = (int) round(cy); // cx might be negative, so (int)(cx + 0.5) will not do!
2312 int dx = ix - canvas->x0; // dx and dy specify the displacement (scroll) of the
2313 int dy = iy - canvas->y0; // canvas w.r.t its previous position
2315 canvas->dx0 = cx; // here the 'd' stands for double, not delta!
2316 canvas->dy0 = cy;
2317 canvas->x0 = ix;
2318 canvas->y0 = iy;
2320 sp_canvas_resize_tiles (canvas, canvas->x0, canvas->y0, canvas->x0+canvas->widget.allocation.width, canvas->y0+canvas->widget.allocation.height);
2322 if (!clear) {
2323 // scrolling without zoom; redraw only the newly exposed areas
2324 if ((dx != 0) || (dy != 0)) {
2325 canvas->is_scrolling = is_scrolling;
2326 if (GTK_WIDGET_REALIZED (canvas)) {
2327 gdk_window_scroll (SP_CANVAS_WINDOW (canvas), -dx, -dy);
2328 }
2329 }
2330 } else {
2331 // scrolling as part of zoom; do nothing here - the next do_update will perform full redraw
2332 }
2333 }
2335 /**
2336 * Updates canvas if necessary.
2337 */
2338 void
2339 sp_canvas_update_now (SPCanvas *canvas)
2340 {
2341 g_return_if_fail (canvas != NULL);
2342 g_return_if_fail (SP_IS_CANVAS (canvas));
2344 if (!(canvas->need_update ||
2345 canvas->need_redraw))
2346 return;
2348 do_update (canvas);
2349 }
2351 /**
2352 * Update callback for canvas widget.
2353 */
2354 static void
2355 sp_canvas_request_update (SPCanvas *canvas)
2356 {
2357 canvas->need_update = TRUE;
2358 add_idle (canvas);
2359 }
2361 /**
2362 * Forces redraw of rectangular canvas area.
2363 */
2364 void
2365 sp_canvas_request_redraw (SPCanvas *canvas, int x0, int y0, int x1, int y1)
2366 {
2367 NRRectL bbox;
2368 NRRectL visible;
2369 NRRectL clip;
2371 g_return_if_fail (canvas != NULL);
2372 g_return_if_fail (SP_IS_CANVAS (canvas));
2374 if (!GTK_WIDGET_DRAWABLE (canvas)) return;
2375 if ((x0 >= x1) || (y0 >= y1)) return;
2377 bbox.x0 = x0;
2378 bbox.y0 = y0;
2379 bbox.x1 = x1;
2380 bbox.y1 = y1;
2382 visible.x0 = canvas->x0;
2383 visible.y0 = canvas->y0;
2384 visible.x1 = visible.x0 + GTK_WIDGET (canvas)->allocation.width;
2385 visible.y1 = visible.y0 + GTK_WIDGET (canvas)->allocation.height;
2387 nr_rect_l_intersect (&clip, &bbox, &visible);
2389 sp_canvas_dirty_rect(canvas, clip.x0, clip.y0, clip.x1, clip.y1);
2390 add_idle (canvas);
2391 }
2393 /**
2394 * Sets world coordinates from win and canvas.
2395 */
2396 void sp_canvas_window_to_world(SPCanvas const *canvas, double winx, double winy, double *worldx, double *worldy)
2397 {
2398 g_return_if_fail (canvas != NULL);
2399 g_return_if_fail (SP_IS_CANVAS (canvas));
2401 if (worldx) *worldx = canvas->x0 + winx;
2402 if (worldy) *worldy = canvas->y0 + winy;
2403 }
2405 /**
2406 * Sets win coordinates from world and canvas.
2407 */
2408 void sp_canvas_world_to_window(SPCanvas const *canvas, double worldx, double worldy, double *winx, double *winy)
2409 {
2410 g_return_if_fail (canvas != NULL);
2411 g_return_if_fail (SP_IS_CANVAS (canvas));
2413 if (winx) *winx = worldx - canvas->x0;
2414 if (winy) *winy = worldy - canvas->y0;
2415 }
2417 /**
2418 * Converts point from win to world coordinates.
2419 */
2420 Geom::Point sp_canvas_window_to_world(SPCanvas const *canvas, Geom::Point const win)
2421 {
2422 g_assert (canvas != NULL);
2423 g_assert (SP_IS_CANVAS (canvas));
2425 return Geom::Point(canvas->x0 + win[0], canvas->y0 + win[1]);
2426 }
2428 /**
2429 * Converts point from world to win coordinates.
2430 */
2431 Geom::Point sp_canvas_world_to_window(SPCanvas const *canvas, Geom::Point const world)
2432 {
2433 g_assert (canvas != NULL);
2434 g_assert (SP_IS_CANVAS (canvas));
2436 return Geom::Point(world[0] - canvas->x0, world[1] - canvas->y0);
2437 }
2439 /**
2440 * Returns true if point given in world coordinates is inside window.
2441 */
2442 bool sp_canvas_world_pt_inside_window(SPCanvas const *canvas, Geom::Point const &world)
2443 {
2444 g_assert( canvas != NULL );
2445 g_assert(SP_IS_CANVAS(canvas));
2447 GtkWidget const &w = *GTK_WIDGET(canvas);
2448 return ( ( canvas->x0 <= world[Geom::X] ) &&
2449 ( canvas->y0 <= world[Geom::Y] ) &&
2450 ( world[Geom::X] < canvas->x0 + w.allocation.width ) &&
2451 ( world[Geom::Y] < canvas->y0 + w.allocation.height ) );
2452 }
2454 /**
2455 * Return canvas window coordinates as Geom::Rect.
2456 */
2457 Geom::Rect SPCanvas::getViewbox() const
2458 {
2459 GtkWidget const *w = GTK_WIDGET(this);
2460 return Geom::Rect(Geom::Point(dx0, dy0),
2461 Geom::Point(dx0 + w->allocation.width, dy0 + w->allocation.height));
2462 }
2464 /**
2465 * Return canvas window coordinates as IRect (a rectangle defined by integers).
2466 */
2467 NR::IRect SPCanvas::getViewboxIntegers() const
2468 {
2469 GtkWidget const *w = GTK_WIDGET(this);
2470 return NR::IRect(NR::IPoint(x0, y0),
2471 NR::IPoint(x0 + w->allocation.width, y0 + w->allocation.height));
2472 }
2474 inline int sp_canvas_tile_floor(int x)
2475 {
2476 return (x & (~(TILE_SIZE - 1))) / TILE_SIZE;
2477 }
2479 inline int sp_canvas_tile_ceil(int x)
2480 {
2481 return ((x + (TILE_SIZE - 1)) & (~(TILE_SIZE - 1))) / TILE_SIZE;
2482 }
2484 /**
2485 * Helper that allocates a new tile array for the canvas, copying overlapping tiles from the old array
2486 */
2487 static void sp_canvas_resize_tiles(SPCanvas* canvas, int nl, int nt, int nr, int nb)
2488 {
2489 if ( nl >= nr || nt >= nb ) {
2490 if ( canvas->tiles ) g_free(canvas->tiles);
2491 canvas->tLeft=canvas->tTop=canvas->tRight=canvas->tBottom=0;
2492 canvas->tileH=canvas->tileV=0;
2493 canvas->tiles=NULL;
2494 return;
2495 }
2496 int tl=sp_canvas_tile_floor(nl);
2497 int tt=sp_canvas_tile_floor(nt);
2498 int tr=sp_canvas_tile_ceil(nr);
2499 int tb=sp_canvas_tile_ceil(nb);
2501 int nh = tr-tl, nv = tb-tt;
2502 uint8_t* ntiles = (uint8_t*)g_malloc(nh*nv*sizeof(uint8_t));
2503 for (int i=tl; i<tr; i++) {
2504 for (int j=tt; j<tb; j++) {
2505 int ind = (i-tl) + (j-tt)*nh;
2506 if ( i >= canvas->tLeft && i < canvas->tRight && j >= canvas->tTop && j < canvas->tBottom ) {
2507 ntiles[ind]=canvas->tiles[(i-canvas->tLeft)+(j-canvas->tTop)*canvas->tileH]; // copy from the old tile
2508 } else {
2509 ntiles[ind]=0; // newly exposed areas get 0
2510 }
2511 }
2512 }
2513 if ( canvas->tiles ) g_free(canvas->tiles);
2514 canvas->tiles=ntiles;
2515 canvas->tLeft=tl;
2516 canvas->tTop=tt;
2517 canvas->tRight=tr;
2518 canvas->tBottom=tb;
2519 canvas->tileH=nh;
2520 canvas->tileV=nv;
2521 }
2523 /*
2524 * Helper that queues a canvas rectangle for redraw
2525 */
2526 static void sp_canvas_dirty_rect(SPCanvas* canvas, int nl, int nt, int nr, int nb) {
2527 canvas->need_redraw = TRUE;
2529 sp_canvas_mark_rect(canvas, nl, nt, nr, nb, 1);
2530 }
2532 /**
2533 * Helper that marks specific canvas rectangle as clean (val == 0) or dirty (otherwise)
2534 */
2535 void sp_canvas_mark_rect(SPCanvas* canvas, int nl, int nt, int nr, int nb, uint8_t val)
2536 {
2537 if ( nl >= nr || nt >= nb ) {
2538 return;
2539 }
2540 int tl=sp_canvas_tile_floor(nl);
2541 int tt=sp_canvas_tile_floor(nt);
2542 int tr=sp_canvas_tile_ceil(nr);
2543 int tb=sp_canvas_tile_ceil(nb);
2544 if ( tl >= canvas->tRight || tr <= canvas->tLeft || tt >= canvas->tBottom || tb <= canvas->tTop ) return;
2545 if ( tl < canvas->tLeft ) tl=canvas->tLeft;
2546 if ( tr > canvas->tRight ) tr=canvas->tRight;
2547 if ( tt < canvas->tTop ) tt=canvas->tTop;
2548 if ( tb > canvas->tBottom ) tb=canvas->tBottom;
2550 for (int i=tl; i<tr; i++) {
2551 for (int j=tt; j<tb; j++) {
2552 canvas->tiles[(i-canvas->tLeft)+(j-canvas->tTop)*canvas->tileH] = val;
2553 }
2554 }
2555 }
2558 /*
2559 Local Variables:
2560 mode:c++
2561 c-file-style:"stroustrup"
2562 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2563 indent-tabs-mode:nil
2564 fill-column:99
2565 End:
2566 */
2567 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :