diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fdada3209..a19d44ec2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -73,6 +73,7 @@ add_library(aseprite-library check_args.cpp console.cpp context.cpp + documents.cpp gfxmode.cpp gui_xml.cpp job.cpp diff --git a/src/context.cpp b/src/context.cpp index 16c1dcbbc..b5affb61c 100644 --- a/src/context.cpp +++ b/src/context.cpp @@ -18,13 +18,13 @@ #include "config.h" -#include - #include "console.h" #include "context.h" #include "commands/command.h" #include "raster/sprite.h" +#include + Context::Context(ISettings* settings) { m_currentSprite = NULL; @@ -33,13 +33,6 @@ Context::Context(ISettings* settings) Context::~Context() { - for (Documents::iterator - it = m_documents.begin(), end = m_documents.end(); it != end; ++it) { - Sprite* sprite = *it; - delete sprite; - } - m_documents.clear(); - delete m_settings; } @@ -51,7 +44,7 @@ const Documents& Context::getDocuments() const Sprite* Context::getFirstSprite() const { if (!m_documents.empty()) - return m_documents.front(); + return m_documents.getByIndex(0); else return NULL; } @@ -74,7 +67,7 @@ void Context::addSprite(Sprite* sprite) { ASSERT(sprite != NULL); - m_documents.push_front(sprite); + m_documents.addDocument(sprite); // Generate onAddSprite event onAddSprite(sprite); @@ -84,11 +77,8 @@ void Context::removeSprite(Sprite* sprite) { ASSERT(sprite != NULL); - Documents::iterator it = std::find(m_documents.begin(), m_documents.end(), sprite); - ASSERT(it != m_documents.end()); - - // remove the item from the sprites list - m_documents.erase(it); + // Remove the item from the sprites list. + m_documents.removeDocument(sprite); // generate on_remove_sprite event onRemoveSprite(sprite); @@ -100,16 +90,9 @@ void Context::removeSprite(Sprite* sprite) void Context::sendSpriteToTop(Sprite* sprite) { - ASSERT(sprite); + ASSERT(sprite != NULL); - Documents::iterator it = std::find(m_documents.begin(), m_documents.end(), sprite); - ASSERT(it != m_documents.end()); - - // remove the item from the sprites list - m_documents.erase(it); - - // add it again - m_documents.push_front(sprite); + m_documents.moveDocument(sprite, 0); } Sprite* Context::getCurrentSprite() const diff --git a/src/context.h b/src/context.h index c612c521c..8d96eaeb6 100644 --- a/src/context.h +++ b/src/context.h @@ -19,9 +19,9 @@ #ifndef CONTEXT_H_INCLUDED #define CONTEXT_H_INCLUDED -#include #include "base/disable_copying.h" #include "base/exception.h" +#include "documents.h" #include "settings/settings.h" class Sprite; @@ -29,8 +29,6 @@ class SpriteReader; class Command; class Params; -typedef std::list Documents; - class CommandPreconditionException : public base::Exception { public: diff --git a/src/documents.cpp b/src/documents.cpp new file mode 100644 index 000000000..d3e9a603f --- /dev/null +++ b/src/documents.cpp @@ -0,0 +1,65 @@ +/* ASE - Allegro Sprite Editor + * Copyright (C) 2001-2011 David Capello + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#include "documents.h" +#include "raster/sprite.h" + +#include + +Documents::Documents() +{ +} + +Documents::~Documents() +{ + deleteAll(); +} + +void Documents::addDocument(Sprite* sprite) +{ + ASSERT(sprite != NULL); + + m_documents.insert(begin(), sprite); +} + +void Documents::removeDocument(Sprite* sprite) +{ + iterator it = std::find(begin(), end(), sprite); + ASSERT(it != end()); + + if (it != end()) + m_documents.erase(it); +} + +void Documents::moveDocument(Sprite* sprite, int index) +{ + removeDocument(sprite); + + m_documents.insert(begin()+index, sprite); +} + +void Documents::deleteAll() +{ + for (iterator it = begin(), end = this->end(); it != end; ++it) { + Sprite* sprite = *it; + delete sprite; + } + m_documents.clear(); +} diff --git a/src/documents.h b/src/documents.h new file mode 100644 index 000000000..a47a9b58e --- /dev/null +++ b/src/documents.h @@ -0,0 +1,69 @@ +/* ASE - Allegro Sprite Editor + * Copyright (C) 2001-2011 David Capello + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef DOCUMENTS_H_INCLUDED +#define DOCUMENTS_H_INCLUDED + +#include "base/disable_copying.h" + +#include + +class Sprite; + +class Documents +{ +public: + typedef std::vector::iterator iterator; + typedef std::vector::const_iterator const_iterator; + + Documents(); + ~Documents(); + + iterator begin() { return m_documents.begin(); } + iterator end() { return m_documents.end(); } + const_iterator begin() const { return m_documents.begin(); } + const_iterator end() const { return m_documents.end(); } + + int size() const { return m_documents.size(); } + bool empty() const { return m_documents.empty(); } + + // Adds a new document to the list. If the destructor is called the + // document is deleted automatically. + void addDocument(Sprite* sprite); + + // Removes a document from the list without deleting it. You must to + // delete the document after removing it. + void removeDocument(Sprite* sprite); + + // Moves the document to the given location in the same + // list. E.g. It is used to reorder documents when they are selected + // as active. + void moveDocument(Sprite* sprite, int index); + + // Deletes all documents in the list (calling "delete" operation). + void deleteAll(); + + Sprite* getByIndex(int index) const { return m_documents[index]; } + +private: + std::vector m_documents; + + DISABLE_COPYING(Documents) +}; + +#endif