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