diff options
Diffstat (limited to 'src/display/sp-canvas.cpp')
| -rw-r--r-- | src/display/sp-canvas.cpp | 212 |
1 files changed, 78 insertions, 134 deletions
diff --git a/src/display/sp-canvas.cpp b/src/display/sp-canvas.cpp index 5efc4ce86..d17271752 100644 --- a/src/display/sp-canvas.cpp +++ b/src/display/sp-canvas.cpp @@ -58,7 +58,7 @@ struct SPCanvasGroupClass { }; /** - * A group of Items. + * A group of items. */ struct SPCanvasGroup { /** @@ -109,8 +109,8 @@ struct SPCanvasGroup { SPCanvasItem item; - GList *items; - GList *last; + std::list<SPCanvasItem *> items; + }; /** @@ -167,13 +167,6 @@ static guint object_signals[LAST_SIGNAL] = { 0 }; void sp_canvas_item_construct(SPCanvasItem *item, SPCanvasGroup *parent, gchar const *first_arg_name, va_list args); /** - * Convenience function to reorder items in a group's child list. - * - * This puts the specified link after the "before" link. - */ -void put_item_after(GList *link, GList *before); - -/** * Helper that returns true iff item is descendant of parent. */ bool is_descendant(SPCanvasItem const *item, SPCanvasItem const *parent); @@ -426,7 +419,7 @@ static void redraw_if_visible(SPCanvasItem *item) int y1 = (int)(item->y2); if (x0 !=0 || x1 !=0 || y0 !=0 || y1 !=0) { - item->canvas->requestRedraw((int)(item->x1), (int)(item->y1), (int)(item->x2 + 1), (int)(item->y2 + 1)); + item->canvas->requestRedraw((int)(item->x1 - 1), (int)(item->y1 -1), (int)(item->x2 + 1), (int)(item->y2 + 1)); } } } @@ -590,64 +583,6 @@ void sp_canvas_item_affine_absolute(SPCanvasItem *item, Geom::Affine const &affi item->canvas->need_repick = TRUE; } -namespace { - -void put_item_after(GList *link, GList *before) -{ - if (link == before) { - return; - } - - SPCanvasGroup *parent = SP_CANVAS_GROUP (SP_CANVAS_ITEM (link->data)->parent); - - if (before == NULL) { - if (link == parent->items) { - return; - } - - link->prev->next = link->next; - - if (link->next) { - link->next->prev = link->prev; - } else { - parent->last = link->prev; - } - - link->prev = before; - link->next = parent->items; - link->next->prev = link; - parent->items = link; - } else { - if ((link == parent->last) && (before == parent->last->prev)) { - return; - } - - if (link->next) { - link->next->prev = link->prev; - } - - if (link->prev) { - link->prev->next = link->next; - } else { - parent->items = link->next; - parent->items->prev = NULL; - } - - link->prev = before; - link->next = before->next; - - link->prev->next = link; - - if (link->next) { - link->next->prev = link; - } else { - parent->last = link; - } - } -} - -} // namespace - /** * Raises the item in its parent's stack by the specified number of positions. * @@ -668,24 +603,34 @@ void sp_canvas_item_raise(SPCanvasItem *item, int positions) } SPCanvasGroup *parent = SP_CANVAS_GROUP (item->parent); - GList *link = g_list_find (parent->items, item); - g_assert (link != NULL); + std::list<SPCanvasItem *>::iterator l = std::find(parent->items.begin(),parent->items.end(), item); + g_assert (l != parent->items.end()); - GList *before; - for (before = link; positions && before; positions--) - before = before->next; + for (int i=0; i<=positions && l != parent->items.end(); ++i) + ++l; - if (!before) { - before = parent->last; - } + parent->items.remove(item); + parent->items.insert(l, item); - put_item_after (link, before); + redraw_if_visible (item); + item->canvas->need_repick = TRUE; +} +void sp_canvas_item_raise_to_top(SPCanvasItem *item) +{ + g_return_if_fail (item != NULL); + g_return_if_fail (SP_IS_CANVAS_ITEM (item)); + if (!item->parent) + return; + SPCanvasGroup *parent = SP_CANVAS_GROUP (item->parent); + parent->items.remove(item); + parent->items.push_back(item); redraw_if_visible (item); item->canvas->need_repick = TRUE; } + /** * Lowers the item in its parent's stack by the specified number of positions. * @@ -701,32 +646,41 @@ void sp_canvas_item_lower(SPCanvasItem *item, int positions) g_return_if_fail (SP_IS_CANVAS_ITEM (item)); g_return_if_fail (positions >= 1); - if (!item->parent || positions == 0) { + SPCanvasGroup *parent = SP_CANVAS_GROUP(item->parent); + + if (!parent || positions == 0 || item == parent->items.front() ) { return; } - SPCanvasGroup *parent = SP_CANVAS_GROUP (item->parent); - GList *link = g_list_find (parent->items, item); - g_assert (link != NULL); + std::list<SPCanvasItem *>::iterator l = std::find(parent->items.begin(), parent->items.end(), item); + g_assert (l != parent->items.end()); - GList *before; - if (link->prev) { - for (before = link->prev; positions && before; positions--) { - before = before->prev; - } - } else { - before = NULL; - } - - put_item_after (link, before); + for (int i=0; i<positions && l != parent->items.begin(); ++i) + --l; + + parent->items.remove(item); + parent->items.insert(l, item); redraw_if_visible (item); item->canvas->need_repick = TRUE; } +void sp_canvas_item_lower_to_bottom(SPCanvasItem *item) +{ + g_return_if_fail (item != NULL); + g_return_if_fail (SP_IS_CANVAS_ITEM (item)); + if (!item->parent) + return; + SPCanvasGroup *parent = SP_CANVAS_GROUP (item->parent); + parent->items.remove(item); + parent->items.push_front(item); + redraw_if_visible (item); + item->canvas->need_repick = TRUE; +} + bool sp_canvas_item_is_visible(SPCanvasItem *item) { - return item->visible; + return item->visible; } /** @@ -919,7 +873,15 @@ void sp_canvas_item_request_update(SPCanvasItem *item) */ gint sp_canvas_item_order (SPCanvasItem * item) { - return g_list_index (SP_CANVAS_GROUP (item->parent)->items, item); + SPCanvasGroup * p = SP_CANVAS_GROUP(item->parent); + size_t index = 0; + for (std::list<SPCanvasItem*>::const_iterator it = p->items.begin(); it != p->items.end(); ++it, ++index) { + if ((*it) == item) { + return index; + } + } + + return -1; } // SPCanvasGroup @@ -936,9 +898,9 @@ static void sp_canvas_group_class_init(SPCanvasGroupClass *klass) item_class->viewbox_changed = SPCanvasGroup::viewboxChanged; } -static void sp_canvas_group_init(SPCanvasGroup * /*group*/) +static void sp_canvas_group_init(SPCanvasGroup * group) { - // Nothing here + new (&group->items) std::list<SPCanvasItem *>; } void SPCanvasGroup::destroy(SPCanvasItem *object) @@ -946,16 +908,15 @@ void SPCanvasGroup::destroy(SPCanvasItem *object) g_return_if_fail(object != NULL); g_return_if_fail(SP_IS_CANVAS_GROUP(object)); - SPCanvasGroup const *group = SP_CANVAS_GROUP(object); - - GList *list = group->items; - while (list) { - SPCanvasItem *child = reinterpret_cast<SPCanvasItem *>(list->data); - list = list->next; + SPCanvasGroup *group = SP_CANVAS_GROUP(object); - sp_canvas_item_destroy(child); + for (std::list<SPCanvasItem *>::iterator it = group->items.begin(); it != group->items.end(); ++it) { + sp_canvas_item_destroy(*it); } + group->items.clear(); + group->items.~list(); // invoke manually + if (SP_CANVAS_ITEM_CLASS(sp_canvas_group_parent_class)->destroy) { (* SP_CANVAS_ITEM_CLASS(sp_canvas_group_parent_class)->destroy)(object); } @@ -966,8 +927,8 @@ void SPCanvasGroup::update(SPCanvasItem *item, Geom::Affine const &affine, unsig SPCanvasGroup const *group = SP_CANVAS_GROUP(item); Geom::OptRect bounds; - for (GList *list = group->items; list; list = list->next) { - SPCanvasItem *i = SP_CANVAS_ITEM(list->data); + for (std::list<SPCanvasItem *>::const_iterator it = group->items.begin(); it != group->items.end(); ++it) { + SPCanvasItem *i = *it; sp_canvas_item_invoke_update (i, affine, flags); @@ -1002,9 +963,8 @@ double SPCanvasGroup::point(SPCanvasItem *item, Geom::Point p, SPCanvasItem **ac *actual_item = NULL; double dist = 0.0; - - for (GList *list = group->items; list; list = list->next) { - SPCanvasItem *child = SP_CANVAS_ITEM(list->data); + for (std::list<SPCanvasItem *>::const_iterator it = group->items.begin(); it != group->items.end(); ++it) { + SPCanvasItem *child = *it; if ((child->x1 <= x2) && (child->y1 <= y2) && (child->x2 >= x1) && (child->y2 >= y1)) { SPCanvasItem *point_item = NULL; // cater for incomplete item implementations @@ -1037,8 +997,8 @@ void SPCanvasGroup::render(SPCanvasItem *item, SPCanvasBuf *buf) { SPCanvasGroup const *group = SP_CANVAS_GROUP(item); - for (GList *list = group->items; list; list = list->next) { - SPCanvasItem *child = SP_CANVAS_ITEM(list->data); + for (std::list<SPCanvasItem *>::const_iterator it = group->items.begin(); it != group->items.end(); ++it) { + SPCanvasItem *child = *it; if (child->visible) { if ((child->x1 < buf->rect.right()) && (child->y1 < buf->rect.bottom()) && @@ -1056,8 +1016,8 @@ void SPCanvasGroup::viewboxChanged(SPCanvasItem *item, Geom::IntRect const &new_ { SPCanvasGroup *group = SP_CANVAS_GROUP(item); - for (GList *list = group->items; list; list = list->next) { - SPCanvasItem *child = SP_CANVAS_ITEM(list->data); + for (std::list<SPCanvasItem *>::const_iterator it = group->items.begin(); it != group->items.end(); ++it) { + SPCanvasItem *child = *it; if (child->visible) { if (SP_CANVAS_ITEM_GET_CLASS(child)->viewbox_changed) { SP_CANVAS_ITEM_GET_CLASS(child)->viewbox_changed(child, new_area); @@ -1071,37 +1031,21 @@ void SPCanvasGroup::add(SPCanvasItem *item) g_object_ref(item); g_object_ref_sink(item); - if (!items) { - items = g_list_append(items, item); - last = items; - } else { - last = g_list_append(last, item)->next; - } + items.push_back(item); sp_canvas_item_request_update(item); } void SPCanvasGroup::remove(SPCanvasItem *item) { + g_return_if_fail(item != NULL); + items.remove(item); - for (GList *children = items; children; children = children->next) { - if (children->data == item) { + // Unparent the child + item->parent = NULL; + g_object_unref(item); - // Unparent the child - item->parent = NULL; - g_object_unref(item); - - // Remove it from the list - if (children == last) { - last = children->prev; - } - - items = g_list_remove_link(items, children); - g_list_free(children); - break; - } - } } static void sp_canvas_dispose (GObject *object); |
