d1d7221f00cd39608273fe7a2df95023b287b1f2
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 canvas->tiles=NULL;
964 canvas->tLeft=canvas->tTop=canvas->tRight=canvas->tBottom=0;
965 canvas->tileH=canvas->tileV=0;
966 }
968 /**
969 * Convenience function to remove the idle handler of a canvas.
970 */
971 static void
972 remove_idle (SPCanvas *canvas)
973 {
974 if (canvas->idle_id) {
975 gtk_idle_remove (canvas->idle_id);
976 canvas->idle_id = 0;
977 }
978 }
980 /*
981 * Removes the transient state of the canvas (idle handler, grabs).
982 */
983 static void
984 shutdown_transients (SPCanvas *canvas)
985 {
986 /* We turn off the need_redraw flag, since if the canvas is mapped again
987 * it will request a redraw anyways. We do not turn off the need_update
988 * flag, though, because updates are not queued when the canvas remaps
989 * itself.
990 */
991 if (canvas->need_redraw) {
992 canvas->need_redraw = FALSE;
993 }
994 if ( canvas->tiles ) free(canvas->tiles);
995 canvas->tiles=NULL;
996 canvas->tLeft=canvas->tTop=canvas->tRight=canvas->tBottom=0;
997 canvas->tileH=canvas->tileV=0;
999 if (canvas->grabbed_item) {
1000 canvas->grabbed_item = NULL;
1001 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1002 }
1004 remove_idle (canvas);
1005 }
1007 /**
1008 * Destroy handler for SPCanvas.
1009 */
1010 static void
1011 sp_canvas_destroy (GtkObject *object)
1012 {
1013 SPCanvas *canvas = SP_CANVAS (object);
1015 if (canvas->root) {
1016 gtk_object_unref (GTK_OBJECT (canvas->root));
1017 canvas->root = NULL;
1018 }
1020 shutdown_transients (canvas);
1022 if (GTK_OBJECT_CLASS (canvas_parent_class)->destroy)
1023 (* GTK_OBJECT_CLASS (canvas_parent_class)->destroy) (object);
1024 }
1026 /**
1027 * Returns new canvas as widget.
1028 */
1029 GtkWidget *
1030 sp_canvas_new_aa (void)
1031 {
1032 SPCanvas *canvas = (SPCanvas *)gtk_type_new (sp_canvas_get_type ());
1034 return (GtkWidget *) canvas;
1035 }
1037 /**
1038 * The canvas widget's realize callback.
1039 */
1040 static void
1041 sp_canvas_realize (GtkWidget *widget)
1042 {
1043 SPCanvas *canvas = SP_CANVAS (widget);
1045 GdkWindowAttr attributes;
1046 attributes.window_type = GDK_WINDOW_CHILD;
1047 attributes.x = widget->allocation.x;
1048 attributes.y = widget->allocation.y;
1049 attributes.width = widget->allocation.width;
1050 attributes.height = widget->allocation.height;
1051 attributes.wclass = GDK_INPUT_OUTPUT;
1052 attributes.visual = gdk_rgb_get_visual ();
1053 attributes.colormap = gdk_rgb_get_cmap ();
1054 attributes.event_mask = (gtk_widget_get_events (widget) |
1055 GDK_EXPOSURE_MASK |
1056 GDK_BUTTON_PRESS_MASK |
1057 GDK_BUTTON_RELEASE_MASK |
1058 GDK_POINTER_MOTION_MASK |
1059 GDK_PROXIMITY_IN_MASK |
1060 GDK_PROXIMITY_OUT_MASK |
1061 GDK_KEY_PRESS_MASK |
1062 GDK_KEY_RELEASE_MASK |
1063 GDK_ENTER_NOTIFY_MASK |
1064 GDK_LEAVE_NOTIFY_MASK |
1065 GDK_FOCUS_CHANGE_MASK);
1066 gint attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1068 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1069 gdk_window_set_user_data (widget->window, widget);
1070 gtk_widget_set_events(widget, attributes.event_mask);
1072 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1074 canvas->pixmap_gc = gdk_gc_new (SP_CANVAS_WINDOW (canvas));
1075 }
1077 /**
1078 * The canvas widget's unrealize callback.
1079 */
1080 static void
1081 sp_canvas_unrealize (GtkWidget *widget)
1082 {
1083 SPCanvas *canvas = SP_CANVAS (widget);
1085 shutdown_transients (canvas);
1087 gdk_gc_destroy (canvas->pixmap_gc);
1088 canvas->pixmap_gc = NULL;
1090 if (GTK_WIDGET_CLASS (canvas_parent_class)->unrealize)
1091 (* GTK_WIDGET_CLASS (canvas_parent_class)->unrealize) (widget);
1092 }
1094 /**
1095 * The canvas widget's size_request callback.
1096 */
1097 static void
1098 sp_canvas_size_request (GtkWidget *widget, GtkRequisition *req)
1099 {
1100 static_cast<void>(SP_CANVAS (widget));
1102 req->width = 256;
1103 req->height = 256;
1104 }
1106 /**
1107 * The canvas widget's size_allocate callback.
1108 */
1109 static void
1110 sp_canvas_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
1111 {
1112 SPCanvas *canvas = SP_CANVAS (widget);
1114 /* Schedule redraw of new region */
1115 sp_canvas_resize_tiles(canvas,canvas->x0,canvas->y0,canvas->x0+allocation->width,canvas->y0+allocation->height);
1116 if (allocation->width > widget->allocation.width) {
1117 sp_canvas_request_redraw (canvas,
1118 canvas->x0 + widget->allocation.width,
1119 0,
1120 canvas->x0 + allocation->width,
1121 canvas->y0 + allocation->height);
1122 }
1123 if (allocation->height > widget->allocation.height) {
1124 sp_canvas_request_redraw (canvas,
1125 0,
1126 canvas->y0 + widget->allocation.height,
1127 canvas->x0 + allocation->width,
1128 canvas->y0 + allocation->height);
1129 }
1131 widget->allocation = *allocation;
1133 if (GTK_WIDGET_REALIZED (widget)) {
1134 gdk_window_move_resize (widget->window,
1135 widget->allocation.x, widget->allocation.y,
1136 widget->allocation.width, widget->allocation.height);
1137 }
1138 }
1140 /**
1141 * Helper that emits an event for an item in the canvas, be it the current
1142 * item, grabbed item, or focused item, as appropriate.
1143 */
1144 static int
1145 emit_event (SPCanvas *canvas, GdkEvent *event)
1146 {
1147 guint mask;
1149 if (canvas->grabbed_item) {
1150 switch (event->type) {
1151 case GDK_ENTER_NOTIFY:
1152 mask = GDK_ENTER_NOTIFY_MASK;
1153 break;
1154 case GDK_LEAVE_NOTIFY:
1155 mask = GDK_LEAVE_NOTIFY_MASK;
1156 break;
1157 case GDK_MOTION_NOTIFY:
1158 mask = GDK_POINTER_MOTION_MASK;
1159 break;
1160 case GDK_BUTTON_PRESS:
1161 case GDK_2BUTTON_PRESS:
1162 case GDK_3BUTTON_PRESS:
1163 mask = GDK_BUTTON_PRESS_MASK;
1164 break;
1165 case GDK_BUTTON_RELEASE:
1166 mask = GDK_BUTTON_RELEASE_MASK;
1167 break;
1168 case GDK_KEY_PRESS:
1169 mask = GDK_KEY_PRESS_MASK;
1170 break;
1171 case GDK_KEY_RELEASE:
1172 mask = GDK_KEY_RELEASE_MASK;
1173 break;
1174 case GDK_SCROLL:
1175 mask = GDK_SCROLL;
1176 break;
1177 default:
1178 mask = 0;
1179 break;
1180 }
1182 if (!(mask & canvas->grabbed_event_mask)) return FALSE;
1183 }
1185 /* Convert to world coordinates -- we have two cases because of diferent
1186 * offsets of the fields in the event structures.
1187 */
1189 GdkEvent ev = *event;
1191 switch (ev.type) {
1192 case GDK_ENTER_NOTIFY:
1193 case GDK_LEAVE_NOTIFY:
1194 ev.crossing.x += canvas->x0;
1195 ev.crossing.y += canvas->y0;
1196 break;
1197 case GDK_MOTION_NOTIFY:
1198 case GDK_BUTTON_PRESS:
1199 case GDK_2BUTTON_PRESS:
1200 case GDK_3BUTTON_PRESS:
1201 case GDK_BUTTON_RELEASE:
1202 ev.motion.x += canvas->x0;
1203 ev.motion.y += canvas->y0;
1204 break;
1205 default:
1206 break;
1207 }
1209 /* Choose where we send the event */
1211 /* canvas->current_item becomes NULL in some cases under Win32
1212 ** (e.g. if the pointer leaves the window). So this is a hack that
1213 ** Lauris applied to SP to get around the problem.
1214 */
1215 SPCanvasItem* item = NULL;
1216 if (canvas->grabbed_item && !is_descendant (canvas->current_item, canvas->grabbed_item)) {
1217 item = canvas->grabbed_item;
1218 } else {
1219 item = canvas->current_item;
1220 }
1222 if (canvas->focused_item &&
1223 ((event->type == GDK_KEY_PRESS) ||
1224 (event->type == GDK_KEY_RELEASE) ||
1225 (event->type == GDK_FOCUS_CHANGE))) {
1226 item = canvas->focused_item;
1227 }
1229 /* The event is propagated up the hierarchy (for if someone connected to
1230 * a group instead of a leaf event), and emission is stopped if a
1231 * handler returns TRUE, just like for GtkWidget events.
1232 */
1234 gint finished = FALSE;
1236 while (item && !finished) {
1237 gtk_object_ref (GTK_OBJECT (item));
1238 gtk_signal_emit (GTK_OBJECT (item), item_signals[ITEM_EVENT], &ev, &finished);
1239 SPCanvasItem *parent = item->parent;
1240 gtk_object_unref (GTK_OBJECT (item));
1241 item = parent;
1242 }
1244 return finished;
1245 }
1247 /**
1248 * Helper that re-picks the current item in the canvas, based on the event's
1249 * coordinates and emits enter/leave events for items as appropriate.
1250 */
1251 static int
1252 pick_current_item (SPCanvas *canvas, GdkEvent *event)
1253 {
1254 double x, y;
1256 int retval = FALSE;
1258 /* Save the event in the canvas. This is used to synthesize enter and
1259 * leave events in case the current item changes. It is also used to
1260 * re-pick the current item if the current one gets deleted. Also,
1261 * synthesize an enter event.
1262 */
1263 if (event != &canvas->pick_event) {
1264 if ((event->type == GDK_MOTION_NOTIFY) || (event->type == GDK_BUTTON_RELEASE)) {
1265 /* these fields have the same offsets in both types of events */
1267 canvas->pick_event.crossing.type = GDK_ENTER_NOTIFY;
1268 canvas->pick_event.crossing.window = event->motion.window;
1269 canvas->pick_event.crossing.send_event = event->motion.send_event;
1270 canvas->pick_event.crossing.subwindow = NULL;
1271 canvas->pick_event.crossing.x = event->motion.x;
1272 canvas->pick_event.crossing.y = event->motion.y;
1273 canvas->pick_event.crossing.mode = GDK_CROSSING_NORMAL;
1274 canvas->pick_event.crossing.detail = GDK_NOTIFY_NONLINEAR;
1275 canvas->pick_event.crossing.focus = FALSE;
1276 canvas->pick_event.crossing.state = event->motion.state;
1278 /* these fields don't have the same offsets in both types of events */
1280 if (event->type == GDK_MOTION_NOTIFY) {
1281 canvas->pick_event.crossing.x_root = event->motion.x_root;
1282 canvas->pick_event.crossing.y_root = event->motion.y_root;
1283 } else {
1284 canvas->pick_event.crossing.x_root = event->button.x_root;
1285 canvas->pick_event.crossing.y_root = event->button.y_root;
1286 }
1287 } else {
1288 canvas->pick_event = *event;
1289 }
1290 }
1292 /* Don't do anything else if this is a recursive call */
1293 if (canvas->in_repick) return retval;
1295 /* LeaveNotify means that there is no current item, so we don't look for one */
1296 if (canvas->pick_event.type != GDK_LEAVE_NOTIFY) {
1297 /* these fields don't have the same offsets in both types of events */
1299 if (canvas->pick_event.type == GDK_ENTER_NOTIFY) {
1300 x = canvas->pick_event.crossing.x;
1301 y = canvas->pick_event.crossing.y;
1302 } else {
1303 x = canvas->pick_event.motion.x;
1304 y = canvas->pick_event.motion.y;
1305 }
1307 /* world coords */
1308 x += canvas->x0;
1309 y += canvas->y0;
1311 /* find the closest item */
1312 if (canvas->root->flags & SP_CANVAS_ITEM_VISIBLE) {
1313 sp_canvas_item_invoke_point (canvas->root, NR::Point(x, y), &canvas->new_current_item);
1314 } else {
1315 canvas->new_current_item = NULL;
1316 }
1317 } else {
1318 canvas->new_current_item = NULL;
1319 }
1321 if ((canvas->new_current_item == canvas->current_item) && !canvas->left_grabbed_item) {
1322 return retval; /* current item did not change */
1323 }
1325 /* Synthesize events for old and new current items */
1327 if ((canvas->new_current_item != canvas->current_item)
1328 && (canvas->current_item != NULL)
1329 && !canvas->left_grabbed_item) {
1330 GdkEvent new_event;
1331 SPCanvasItem *item;
1333 item = canvas->current_item;
1335 new_event = canvas->pick_event;
1336 new_event.type = GDK_LEAVE_NOTIFY;
1338 new_event.crossing.detail = GDK_NOTIFY_ANCESTOR;
1339 new_event.crossing.subwindow = NULL;
1340 canvas->in_repick = TRUE;
1341 retval = emit_event (canvas, &new_event);
1342 canvas->in_repick = FALSE;
1343 }
1345 /* Handle the rest of cases */
1347 canvas->left_grabbed_item = FALSE;
1348 canvas->current_item = canvas->new_current_item;
1350 if (canvas->current_item != NULL) {
1351 GdkEvent new_event;
1353 new_event = canvas->pick_event;
1354 new_event.type = GDK_ENTER_NOTIFY;
1355 new_event.crossing.detail = GDK_NOTIFY_ANCESTOR;
1356 new_event.crossing.subwindow = NULL;
1357 retval = emit_event (canvas, &new_event);
1358 }
1360 return retval;
1361 }
1363 /**
1364 * Button event handler for the canvas.
1365 */
1366 static gint
1367 sp_canvas_button (GtkWidget *widget, GdkEventButton *event)
1368 {
1369 SPCanvas *canvas = SP_CANVAS (widget);
1371 int retval = FALSE;
1373 /* dispatch normally regardless of the event's window if an item has
1374 has a pointer grab in effect */
1375 if (!canvas->grabbed_item &&
1376 event->window != SP_CANVAS_WINDOW (canvas))
1377 return retval;
1379 int mask;
1380 switch (event->button) {
1381 case 1:
1382 mask = GDK_BUTTON1_MASK;
1383 break;
1384 case 2:
1385 mask = GDK_BUTTON2_MASK;
1386 break;
1387 case 3:
1388 mask = GDK_BUTTON3_MASK;
1389 break;
1390 case 4:
1391 mask = GDK_BUTTON4_MASK;
1392 break;
1393 case 5:
1394 mask = GDK_BUTTON5_MASK;
1395 break;
1396 default:
1397 mask = 0;
1398 }
1400 switch (event->type) {
1401 case GDK_BUTTON_PRESS:
1402 case GDK_2BUTTON_PRESS:
1403 case GDK_3BUTTON_PRESS:
1404 /* Pick the current item as if the button were not pressed, and
1405 * then process the event.
1406 */
1407 canvas->state = event->state;
1408 pick_current_item (canvas, (GdkEvent *) event);
1409 canvas->state ^= mask;
1410 retval = emit_event (canvas, (GdkEvent *) event);
1411 break;
1413 case GDK_BUTTON_RELEASE:
1414 /* Process the event as if the button were pressed, then repick
1415 * after the button has been released
1416 */
1417 canvas->state = event->state;
1418 retval = emit_event (canvas, (GdkEvent *) event);
1419 event->state ^= mask;
1420 canvas->state = event->state;
1421 pick_current_item (canvas, (GdkEvent *) event);
1422 event->state ^= mask;
1423 break;
1425 default:
1426 g_assert_not_reached ();
1427 }
1429 return retval;
1430 }
1432 /**
1433 * Scroll event handler for the canvas.
1434 *
1435 * \todo FIXME: generate motion events to re-select items.
1436 */
1437 static gint
1438 sp_canvas_scroll (GtkWidget *widget, GdkEventScroll *event)
1439 {
1440 return emit_event (SP_CANVAS (widget), (GdkEvent *) event);
1441 }
1443 /**
1444 * Motion event handler for the canvas.
1445 */
1446 static int
1447 sp_canvas_motion (GtkWidget *widget, GdkEventMotion *event)
1448 {
1449 SPCanvas *canvas = SP_CANVAS (widget);
1451 if (event->window != SP_CANVAS_WINDOW (canvas))
1452 return FALSE;
1454 if (canvas->grabbed_event_mask & GDK_POINTER_MOTION_HINT_MASK) {
1455 gint x, y;
1456 gdk_window_get_pointer (widget->window, &x, &y, NULL);
1457 event->x = x;
1458 event->y = y;
1459 }
1461 canvas->state = event->state;
1462 pick_current_item (canvas, (GdkEvent *) event);
1464 return emit_event (canvas, (GdkEvent *) event);
1465 }
1467 /**
1468 * Helper that draws a specific rectangular part of the canvas.
1469 */
1470 static void
1471 sp_canvas_paint_rect (SPCanvas *canvas, int xx0, int yy0, int xx1, int yy1)
1472 {
1473 g_return_if_fail (!canvas->need_update);
1475 GtkWidget *widget = GTK_WIDGET (canvas);
1477 int draw_x1 = MAX (xx0, canvas->x0);
1478 int draw_y1 = MAX (yy0, canvas->y0);
1479 int draw_x2 = MIN (xx1, canvas->x0/*draw_x1*/ + GTK_WIDGET (canvas)->allocation.width);
1480 int draw_y2 = MIN (yy1, canvas->y0/*draw_y1*/ + GTK_WIDGET (canvas)->allocation.height);
1482 int bw = draw_x2 - draw_x1;
1483 int bh = draw_y2 - draw_y1;
1484 if ((bw < 1) || (bh < 1))
1485 return;
1487 int sw, sh;
1488 if (canvas->rendermode != RENDERMODE_OUTLINE) { // use 256K as a compromise to not slow down gradients
1489 /* 256K is the cached buffer and we need 3 channels */
1490 if (bw * bh < 87381) { // 256K/3
1491 // We can go with single buffer
1492 sw = bw;
1493 sh = bh;
1494 } else if (bw <= (16 * 341)) {
1495 // Go with row buffer
1496 sw = bw;
1497 sh = 87381 / bw;
1498 } else if (bh <= (16 * 256)) {
1499 // Go with column buffer
1500 sw = 87381 / bh;
1501 sh = bh;
1502 } else {
1503 sw = 341;
1504 sh = 256;
1505 }
1506 } else { // paths only, so 1M works faster
1507 /* 1M is the cached buffer and we need 3 channels */
1508 if (bw * bh < 349525) { // 1M/3
1509 // We can go with single buffer
1510 sw = bw;
1511 sh = bh;
1512 } else if (bw <= (16 * 682)) {
1513 // Go with row buffer
1514 sw = bw;
1515 sh = 349525 / bw;
1516 } else if (bh <= (16 * 512)) {
1517 // Go with column buffer
1518 sw = 349525 / bh;
1519 sh = bh;
1520 } else {
1521 sw = 682;
1522 sh = 512;
1523 }
1524 }
1526 // As we can come from expose, we have to tile here
1527 for (int y0 = draw_y1; y0 < draw_y2; y0 += sh) {
1528 int y1 = MIN (y0 + sh, draw_y2);
1529 for (int x0 = draw_x1; x0 < draw_x2; x0 += sw) {
1530 int x1 = MIN (x0 + sw, draw_x2);
1532 SPCanvasBuf buf;
1533 if (canvas->rendermode != RENDERMODE_OUTLINE) {
1534 buf.buf = nr_pixelstore_256K_new (FALSE, 0);
1535 } else {
1536 buf.buf = nr_pixelstore_1M_new (FALSE, 0);
1537 }
1539 buf.buf_rowstride = sw * 3;
1540 buf.rect.x0 = x0;
1541 buf.rect.y0 = y0;
1542 buf.rect.x1 = x1;
1543 buf.rect.y1 = y1;
1544 GdkColor *color = &widget->style->bg[GTK_STATE_NORMAL];
1545 buf.bg_color = (((color->red & 0xff00) << 8)
1546 | (color->green & 0xff00)
1547 | (color->blue >> 8));
1548 buf.is_empty = true;
1550 if (canvas->root->flags & SP_CANVAS_ITEM_VISIBLE) {
1551 SP_CANVAS_ITEM_GET_CLASS (canvas->root)->render (canvas->root, &buf);
1552 }
1554 if (buf.is_empty) {
1555 gdk_rgb_gc_set_foreground (canvas->pixmap_gc, buf.bg_color);
1556 gdk_draw_rectangle (SP_CANVAS_WINDOW (canvas),
1557 canvas->pixmap_gc,
1558 TRUE,
1559 x0 - canvas->x0, y0 - canvas->y0,
1560 x1 - x0, y1 - y0);
1561 } else {
1562 gdk_draw_rgb_image_dithalign (SP_CANVAS_WINDOW (canvas),
1563 canvas->pixmap_gc,
1564 x0 - canvas->x0, y0 - canvas->y0,
1565 x1 - x0, y1 - y0,
1566 GDK_RGB_DITHER_MAX,
1567 buf.buf,
1568 sw * 3,
1569 x0 - canvas->x0, y0 - canvas->y0);
1570 }
1572 if (canvas->rendermode != RENDERMODE_OUTLINE) {
1573 nr_pixelstore_256K_free (buf.buf);
1574 } else {
1575 nr_pixelstore_1M_free (buf.buf);
1576 }
1578 }
1579 }
1580 }
1582 /**
1583 * The canvas widget's expose callback.
1584 */
1585 static gint
1586 sp_canvas_expose (GtkWidget *widget, GdkEventExpose *event)
1587 {
1588 SPCanvas *canvas = SP_CANVAS (widget);
1590 if (!GTK_WIDGET_DRAWABLE (widget) ||
1591 (event->window != SP_CANVAS_WINDOW (canvas)))
1592 return FALSE;
1594 int n_rects;
1595 GdkRectangle *rects;
1596 gdk_region_get_rectangles (event->region, &rects, &n_rects);
1598 for (int i = 0; i < n_rects; i++) {
1599 NRRectL rect;
1601 rect.x0 = rects[i].x + canvas->x0;
1602 rect.y0 = rects[i].y + canvas->y0;
1603 rect.x1 = rect.x0 + rects[i].width;
1604 rect.y1 = rect.y0 + rects[i].height;
1606 if (canvas->need_update || canvas->need_redraw) {
1607 sp_canvas_request_redraw (canvas, rect.x0, rect.y0, rect.x1, rect.y1);
1608 } else {
1609 /* No pending updates, draw exposed area immediately */
1610 sp_canvas_paint_rect (canvas, rect.x0, rect.y0, rect.x1, rect.y1);
1611 }
1612 }
1614 if (n_rects > 0)
1615 g_free (rects);
1617 return FALSE;
1618 }
1620 /**
1621 * The canvas widget's keypress callback.
1622 */
1623 static gint
1624 sp_canvas_key (GtkWidget *widget, GdkEventKey *event)
1625 {
1626 return emit_event (SP_CANVAS (widget), (GdkEvent *) event);
1627 }
1629 /**
1630 * Crossing event handler for the canvas.
1631 */
1632 static gint
1633 sp_canvas_crossing (GtkWidget *widget, GdkEventCrossing *event)
1634 {
1635 SPCanvas *canvas = SP_CANVAS (widget);
1637 if (event->window != SP_CANVAS_WINDOW (canvas))
1638 return FALSE;
1640 canvas->state = event->state;
1641 return pick_current_item (canvas, (GdkEvent *) event);
1642 }
1644 /**
1645 * Focus in handler for the canvas.
1646 */
1647 static gint
1648 sp_canvas_focus_in (GtkWidget *widget, GdkEventFocus *event)
1649 {
1650 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
1652 SPCanvas *canvas = SP_CANVAS (widget);
1654 if (canvas->focused_item) {
1655 return emit_event (canvas, (GdkEvent *) event);
1656 } else {
1657 return FALSE;
1658 }
1659 }
1661 /**
1662 * Focus out handler for the canvas.
1663 */
1664 static gint
1665 sp_canvas_focus_out (GtkWidget *widget, GdkEventFocus *event)
1666 {
1667 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
1669 SPCanvas *canvas = SP_CANVAS (widget);
1671 if (canvas->focused_item)
1672 return emit_event (canvas, (GdkEvent *) event);
1673 else
1674 return FALSE;
1675 }
1677 /**
1678 * Helper that repaints the areas in the canvas that need it.
1679 */
1680 static int
1681 paint (SPCanvas *canvas)
1682 {
1683 if (canvas->need_update) {
1684 sp_canvas_item_invoke_update (canvas->root, NR::identity(), 0);
1685 canvas->need_update = FALSE;
1686 }
1688 if (!canvas->need_redraw)
1689 return TRUE;
1691 GtkWidget const *widget = GTK_WIDGET(canvas);
1692 int const canvas_x1 = canvas->x0 + widget->allocation.width;
1693 int const canvas_y1 = canvas->y0 + widget->allocation.height;
1695 NRRectL topaint;
1696 topaint.x0 = topaint.y0 = topaint.x1 = topaint.y1 = 0;
1698 for (int j=canvas->tTop&(~3);j<canvas->tBottom;j+=4) {
1699 for (int i=canvas->tLeft&(~3);i<canvas->tRight;i+=4) {
1700 int mode=0;
1702 int pl=i+1,pr=i,pt=j+4,pb=j;
1703 for (int l=MAX(j,canvas->tTop);l<MIN(j+4,canvas->tBottom);l++) {
1704 for (int k=MAX(i,canvas->tLeft);k<MIN(i+4,canvas->tRight);k++) {
1705 if ( canvas->tiles[(k-canvas->tLeft)+(l-canvas->tTop)*canvas->tileH] ) {
1706 mode|=1<<((k-i)+(l-j)*4);
1707 if ( k < pl ) pl=k;
1708 if ( k+1 > pr ) pr=k+1;
1709 if ( l < pt ) pt=l;
1710 if ( l+1 > pb ) pb=l+1;
1711 }
1712 canvas->tiles[(k-canvas->tLeft)+(l-canvas->tTop)*canvas->tileH]=0;
1713 }
1714 }
1716 if ( mode ) {
1717 NRRectL tile;
1718 tile.x0 = MAX (pl*32, canvas->x0);
1719 tile.y0 = MAX (pt*32, canvas->y0);
1720 tile.x1 = MIN (pr*32, canvas_x1);
1721 tile.y1 = MIN (pb*32, canvas_y1);
1722 if ((tile.x0 < tile.x1) && (tile.y0 < tile.y1)) {
1723 nr_rect_l_union (&topaint, &topaint, &tile);
1724 }
1726 }
1727 }
1728 }
1730 sp_canvas_paint_rect (canvas, topaint.x0, topaint.y0, topaint.x1, topaint.y1);
1732 canvas->need_redraw = FALSE;
1733 return TRUE;
1734 }
1736 /**
1737 * Helper that invokes update, paint, and repick on canvas.
1738 */
1739 static int
1740 do_update (SPCanvas *canvas)
1741 {
1742 /* Cause the update if necessary */
1743 if (canvas->need_update) {
1744 sp_canvas_item_invoke_update (canvas->root, NR::identity(), 0);
1745 canvas->need_update = FALSE;
1746 }
1748 /* Paint if able to */
1749 if (GTK_WIDGET_DRAWABLE (canvas)) {
1750 return paint (canvas);
1751 }
1753 /* Pick new current item */
1754 while (canvas->need_repick) {
1755 canvas->need_repick = FALSE;
1756 pick_current_item (canvas, &canvas->pick_event);
1757 }
1759 return TRUE;
1760 }
1762 /**
1763 * Idle handler for the canvas that deals with pending updates and redraws.
1764 */
1765 static gint
1766 idle_handler (gpointer data)
1767 {
1768 GDK_THREADS_ENTER ();
1770 SPCanvas *canvas = SP_CANVAS (data);
1772 const int ret = do_update (canvas);
1774 if (ret) {
1775 /* Reset idle id */
1776 canvas->idle_id = 0;
1777 }
1779 GDK_THREADS_LEAVE ();
1781 return !ret;
1782 }
1784 /**
1785 * Convenience function to add an idle handler to a canvas.
1786 */
1787 static void
1788 add_idle (SPCanvas *canvas)
1789 {
1790 if (canvas->idle_id != 0)
1791 return;
1793 canvas->idle_id = gtk_idle_add_priority (sp_canvas_update_priority, idle_handler, canvas);
1794 }
1796 /**
1797 * Returns the root group of the specified canvas.
1798 */
1799 SPCanvasGroup *
1800 sp_canvas_root (SPCanvas *canvas)
1801 {
1802 g_return_val_if_fail (canvas != NULL, NULL);
1803 g_return_val_if_fail (SP_IS_CANVAS (canvas), NULL);
1805 return SP_CANVAS_GROUP (canvas->root);
1806 }
1808 /**
1809 * Scrolls canvas to specific position.
1810 */
1811 void
1812 sp_canvas_scroll_to (SPCanvas *canvas, double cx, double cy, unsigned int clear)
1813 {
1814 g_return_if_fail (canvas != NULL);
1815 g_return_if_fail (SP_IS_CANVAS (canvas));
1817 int ix = (int) (cx + 0.5);
1818 int iy = (int) (cy + 0.5);
1819 int dx = ix - canvas->x0;
1820 int dy = iy - canvas->y0;
1822 canvas->dx0 = cx;
1823 canvas->dy0 = cy;
1824 canvas->x0 = ix;
1825 canvas->y0 = iy;
1827 sp_canvas_resize_tiles(canvas,canvas->x0,canvas->y0,canvas->x0+canvas->widget.allocation.width,canvas->y0+canvas->widget.allocation.height);
1829 if (!clear) {
1830 // scrolling without zoom; redraw only the newly exposed areas
1831 if ((dx != 0) || (dy != 0)) {
1832 int width, height;
1833 width = canvas->widget.allocation.width;
1834 height = canvas->widget.allocation.height;
1835 if (GTK_WIDGET_REALIZED (canvas)) {
1836 gdk_window_scroll (SP_CANVAS_WINDOW (canvas), -dx, -dy);
1837 gdk_window_process_updates (SP_CANVAS_WINDOW (canvas), TRUE);
1838 }
1839 if (dx < 0) {
1840 sp_canvas_request_redraw (canvas, ix + 0, iy + 0, ix - dx, iy + height);
1841 } else if (dx > 0) {
1842 sp_canvas_request_redraw (canvas, ix + width - dx, iy + 0, ix + width, iy + height);
1843 }
1844 if (dy < 0) {
1845 sp_canvas_request_redraw (canvas, ix + 0, iy + 0, ix + width, iy - dy);
1846 } else if (dy > 0) {
1847 sp_canvas_request_redraw (canvas, ix + 0, iy + height - dy, ix + width, iy + height);
1848 }
1849 }
1850 } else {
1851 // scrolling as part of zoom; do nothing here - the next do_update will perform full redraw
1852 }
1853 }
1855 /**
1856 * Updates canvas if necessary.
1857 */
1858 void
1859 sp_canvas_update_now (SPCanvas *canvas)
1860 {
1861 g_return_if_fail (canvas != NULL);
1862 g_return_if_fail (SP_IS_CANVAS (canvas));
1864 if (!(canvas->need_update ||
1865 canvas->need_redraw))
1866 return;
1868 remove_idle (canvas);
1869 do_update (canvas);
1870 }
1872 /**
1873 * Update callback for canvas widget.
1874 */
1875 static void
1876 sp_canvas_request_update (SPCanvas *canvas)
1877 {
1878 canvas->need_update = TRUE;
1879 add_idle (canvas);
1880 }
1882 /**
1883 * Forces redraw of rectangular canvas area.
1884 */
1885 void
1886 sp_canvas_request_redraw (SPCanvas *canvas, int x0, int y0, int x1, int y1)
1887 {
1888 NRRectL bbox;
1889 NRRectL visible;
1890 NRRectL clip;
1892 g_return_if_fail (canvas != NULL);
1893 g_return_if_fail (SP_IS_CANVAS (canvas));
1895 if (!GTK_WIDGET_DRAWABLE (canvas)) return;
1896 if ((x0 >= x1) || (y0 >= y1)) return;
1898 bbox.x0 = x0;
1899 bbox.y0 = y0;
1900 bbox.x1 = x1;
1901 bbox.y1 = y1;
1903 visible.x0 = canvas->x0;
1904 visible.y0 = canvas->y0;
1905 visible.x1 = visible.x0 + GTK_WIDGET (canvas)->allocation.width;
1906 visible.y1 = visible.y0 + GTK_WIDGET (canvas)->allocation.height;
1908 nr_rect_l_intersect (&clip, &bbox, &visible);
1910 sp_canvas_dirty_rect(canvas,x0,y0,x1,y1);
1911 add_idle (canvas);
1912 }
1914 /**
1915 * Sets world coordinates from win and canvas.
1916 */
1917 void sp_canvas_window_to_world(SPCanvas const *canvas, double winx, double winy, double *worldx, double *worldy)
1918 {
1919 g_return_if_fail (canvas != NULL);
1920 g_return_if_fail (SP_IS_CANVAS (canvas));
1922 if (worldx) *worldx = canvas->x0 + winx;
1923 if (worldy) *worldy = canvas->y0 + winy;
1924 }
1926 /**
1927 * Sets win coordinates from world and canvas.
1928 */
1929 void sp_canvas_world_to_window(SPCanvas const *canvas, double worldx, double worldy, double *winx, double *winy)
1930 {
1931 g_return_if_fail (canvas != NULL);
1932 g_return_if_fail (SP_IS_CANVAS (canvas));
1934 if (winx) *winx = worldx - canvas->x0;
1935 if (winy) *winy = worldy - canvas->y0;
1936 }
1938 /**
1939 * Converts point from win to world coordinates.
1940 */
1941 NR::Point sp_canvas_window_to_world(SPCanvas const *canvas, NR::Point const win)
1942 {
1943 g_assert (canvas != NULL);
1944 g_assert (SP_IS_CANVAS (canvas));
1946 return NR::Point(canvas->x0 + win[0], canvas->y0 + win[1]);
1947 }
1949 /**
1950 * Converts point from world to win coordinates.
1951 */
1952 NR::Point sp_canvas_world_to_window(SPCanvas const *canvas, NR::Point const world)
1953 {
1954 g_assert (canvas != NULL);
1955 g_assert (SP_IS_CANVAS (canvas));
1957 return NR::Point(world[0] - canvas->x0, world[1] - canvas->y0);
1958 }
1960 /**
1961 * Returns true if point given in world coordinates is inside window.
1962 */
1963 bool sp_canvas_world_pt_inside_window(SPCanvas const *canvas, NR::Point const &world)
1964 {
1965 g_assert( canvas != NULL );
1966 g_assert(SP_IS_CANVAS(canvas));
1968 using NR::X;
1969 using NR::Y;
1970 GtkWidget const &w = *GTK_WIDGET(canvas);
1971 return ( ( canvas->x0 <= world[X] ) &&
1972 ( canvas->y0 <= world[Y] ) &&
1973 ( world[X] < canvas->x0 + w.allocation.width ) &&
1974 ( world[Y] < canvas->y0 + w.allocation.height ) );
1975 }
1977 /**
1978 * Return canvas window coordinates as NRRect.
1979 */
1980 NR::Rect SPCanvas::getViewbox() const
1981 {
1982 GtkWidget const *w = GTK_WIDGET(this);
1984 return NR::Rect(NR::Point(dx0, dy0),
1985 NR::Point(dx0 + w->allocation.width, dy0 + w->allocation.height));
1986 }
1988 inline int sp_canvas_tile_floor(int x)
1989 {
1990 return (x&(~31))/32;
1991 }
1993 inline int sp_canvas_tile_ceil(int x)
1994 {
1995 return ((x+31)&(~31))/32;
1996 }
1998 /**
1999 * Helper that changes tile size for canvas redraw.
2000 */
2001 void sp_canvas_resize_tiles(SPCanvas* canvas,int nl,int nt,int nr,int nb)
2002 {
2003 if ( nl >= nr || nt >= nb ) {
2004 if ( canvas->tiles ) free(canvas->tiles);
2005 canvas->tLeft=canvas->tTop=canvas->tRight=canvas->tBottom=0;
2006 canvas->tileH=canvas->tileV=0;
2007 canvas->tiles=NULL;
2008 return;
2009 }
2010 int tl=sp_canvas_tile_floor(nl);
2011 int tt=sp_canvas_tile_floor(nt);
2012 int tr=sp_canvas_tile_ceil(nr);
2013 int tb=sp_canvas_tile_ceil(nb);
2015 int nh=tr-tl,nv=tb-tt;
2016 uint8_t* ntiles=(uint8_t*)malloc(nh*nv*sizeof(uint8_t));
2017 for (int i=tl;i<tr;i++) {
2018 for (int j=tt;j<tb;j++) {
2019 int ind=(i-tl)+(j-tt)*nh;
2020 if ( i >= canvas->tLeft && i < canvas->tRight && j >= canvas->tTop && j < canvas->tBottom ) {
2021 ntiles[ind]=canvas->tiles[(i-canvas->tLeft)+(j-canvas->tTop)*canvas->tileH];
2022 } else {
2023 ntiles[ind]=0;
2024 }
2025 }
2026 }
2027 if ( canvas->tiles ) free(canvas->tiles);
2028 canvas->tiles=ntiles;
2029 canvas->tLeft=tl;
2030 canvas->tTop=tt;
2031 canvas->tRight=tr;
2032 canvas->tBottom=tb;
2033 canvas->tileH=nh;
2034 canvas->tileV=nv;
2035 }
2037 /**
2038 * Helper that marks specific canvas rectangle for redraw.
2039 */
2040 void sp_canvas_dirty_rect(SPCanvas* canvas,int nl,int nt,int nr,int nb)
2041 {
2042 if ( nl >= nr || nt >= nb ) {
2043 return;
2044 }
2045 int tl=sp_canvas_tile_floor(nl);
2046 int tt=sp_canvas_tile_floor(nt);
2047 int tr=sp_canvas_tile_ceil(nr);
2048 int tb=sp_canvas_tile_ceil(nb);
2049 if ( tl >= canvas->tRight || tr <= canvas->tLeft || tt >= canvas->tBottom || tb <= canvas->tTop ) return;
2050 if ( tl < canvas->tLeft ) tl=canvas->tLeft;
2051 if ( tr > canvas->tRight ) tr=canvas->tRight;
2052 if ( tt < canvas->tTop ) tt=canvas->tTop;
2053 if ( tb > canvas->tBottom ) tb=canvas->tBottom;
2055 canvas->need_redraw = TRUE;
2057 for (int i=tl;i<tr;i++) {
2058 for (int j=tt;j<tb;j++) {
2059 canvas->tiles[(i-canvas->tLeft)+(j-canvas->tTop)*canvas->tileH]=1;
2060 }
2061 }
2062 }
2065 /*
2066 Local Variables:
2067 mode:c++
2068 c-file-style:"stroustrup"
2069 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2070 indent-tabs-mode:nil
2071 fill-column:99
2072 End:
2073 */
2074 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :