2013-08-09 08:01:20 +08:00
|
|
|
/* Aseprite
|
2013-01-27 23:13:13 +08:00
|
|
|
* Copyright (C) 2001-2013 David Capello
|
2012-01-06 06:45:03 +08:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2012-01-06 06:45:03 +08:00
|
|
|
#include "config.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "app/app.h"
|
|
|
|
#include "app/commands/command.h"
|
|
|
|
#include "app/commands/commands.h"
|
|
|
|
#include "app/context_access.h"
|
|
|
|
#include "app/document_access.h"
|
|
|
|
#include "app/modules/editors.h"
|
|
|
|
#include "app/ui/document_view.h"
|
|
|
|
#include "app/ui/main_window.h"
|
|
|
|
#include "app/ui/status_bar.h"
|
|
|
|
#include "app/ui/workspace.h"
|
|
|
|
#include "app/ui_context.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
#include "raster/sprite.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "ui/ui.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2012-08-11 21:42:51 +08:00
|
|
|
#include <memory>
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
namespace app {
|
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
using namespace ui;
|
|
|
|
|
2012-01-06 06:45:03 +08:00
|
|
|
static bool close_active_document(Context* context);
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
class CloseFileCommand : public Command {
|
2012-01-06 06:45:03 +08:00
|
|
|
public:
|
|
|
|
CloseFileCommand()
|
|
|
|
: Command("CloseFile",
|
|
|
|
"Close File",
|
|
|
|
CmdUIOnlyFlag)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-03-30 04:08:40 +08:00
|
|
|
Command* clone() const OVERRIDE { return new CloseFileCommand(*this); }
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
bool onEnabled(Context* context)
|
|
|
|
{
|
2013-03-12 07:29:45 +08:00
|
|
|
const ContextReader reader(context);
|
|
|
|
const Sprite* sprite(reader.sprite());
|
2012-01-06 06:45:03 +08:00
|
|
|
return sprite != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onExecute(Context* context)
|
|
|
|
{
|
2013-01-21 05:40:37 +08:00
|
|
|
Workspace* workspace = App::instance()->getMainWindow()->getWorkspace();
|
|
|
|
|
2014-07-29 11:53:24 +08:00
|
|
|
if (workspace->activeView() == NULL)
|
2013-01-21 05:40:37 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (DocumentView* docView =
|
2014-07-29 11:53:24 +08:00
|
|
|
dynamic_cast<DocumentView*>(workspace->activeView())) {
|
2013-01-21 05:40:37 +08:00
|
|
|
Document* document = docView->getDocument();
|
|
|
|
if (static_cast<UIContext*>(context)->countViewsOf(document) == 1) {
|
|
|
|
// If we have only one view for this document, close the file.
|
|
|
|
close_active_document(context);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the active view.
|
2014-07-29 11:53:24 +08:00
|
|
|
WorkspaceView* view = workspace->activeView();
|
2013-03-28 08:19:35 +08:00
|
|
|
workspace->removeView(view);
|
|
|
|
delete view;
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static char* read_authors_txt(const char *filename);
|
|
|
|
};
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
class CloseAllFilesCommand : public Command {
|
2012-01-06 06:45:03 +08:00
|
|
|
public:
|
|
|
|
CloseAllFilesCommand()
|
|
|
|
: Command("CloseAllFiles",
|
|
|
|
"Close All Files",
|
|
|
|
CmdRecordableFlag)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-03-30 04:08:40 +08:00
|
|
|
Command* clone() const OVERRIDE { return new CloseAllFilesCommand(*this); }
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
bool onEnabled(Context* context)
|
|
|
|
{
|
2014-07-29 11:53:24 +08:00
|
|
|
return !context->documents().empty();
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void onExecute(Context* context)
|
|
|
|
{
|
|
|
|
while (true) {
|
2014-07-29 11:53:24 +08:00
|
|
|
if (context->activeDocument() != NULL) {
|
2012-01-06 06:45:03 +08:00
|
|
|
if (!close_active_document(context))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-03-12 07:29:45 +08:00
|
|
|
// Closes the active document, asking to the user to save it if it is
|
|
|
|
// modified.
|
2012-01-06 06:45:03 +08:00
|
|
|
static bool close_active_document(Context* context)
|
|
|
|
{
|
2013-03-12 07:29:45 +08:00
|
|
|
Document* closedDocument = NULL;
|
2012-01-06 06:45:03 +08:00
|
|
|
bool save_it;
|
|
|
|
bool try_again = true;
|
|
|
|
|
|
|
|
while (try_again) {
|
|
|
|
// This flag indicates if we have to sabe the sprite before to destroy it
|
|
|
|
save_it = false;
|
|
|
|
{
|
|
|
|
// The sprite is locked as reader temporaly
|
2013-03-12 07:29:45 +08:00
|
|
|
const ContextReader reader(context);
|
|
|
|
const Document* document = reader.document();
|
|
|
|
closedDocument = const_cast<Document*>(document);
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
// see if the sprite has changes
|
|
|
|
while (document->isModified()) {
|
|
|
|
// ask what want to do the user with the changes in the sprite
|
|
|
|
int ret = Alert::show("Warning<<Saving changes in:<<%s||&Save||Do&n't Save||&Cancel",
|
2014-07-29 11:53:24 +08:00
|
|
|
document->name().c_str());
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
if (ret == 1) {
|
|
|
|
// "save": save the changes
|
|
|
|
save_it = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (ret != 2) {
|
|
|
|
// "cancel" or "ESC" */
|
|
|
|
return false; // we back doing nothing
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// "discard"
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Does we need to save the sprite?
|
|
|
|
if (save_it) {
|
|
|
|
Command* save_command =
|
|
|
|
CommandsModule::instance()->getCommandByName(CommandId::SaveFile);
|
|
|
|
context->executeCommand(save_command);
|
|
|
|
|
|
|
|
try_again = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
try_again = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy the sprite (locking it as writer)
|
|
|
|
{
|
2013-03-12 07:29:45 +08:00
|
|
|
DocumentDestroyer document(context, closedDocument);
|
2012-07-10 00:20:58 +08:00
|
|
|
StatusBar::instance()
|
2012-01-06 06:45:03 +08:00
|
|
|
->setStatusText(0, "Sprite '%s' closed.",
|
2014-07-29 11:53:24 +08:00
|
|
|
document->name().c_str());
|
2013-03-12 07:29:45 +08:00
|
|
|
document.destroyDocument();
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
2013-01-21 05:40:37 +08:00
|
|
|
|
2012-01-06 06:45:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Command* CommandFactory::createCloseFileCommand()
|
|
|
|
{
|
|
|
|
return new CloseFileCommand;
|
|
|
|
}
|
|
|
|
|
|
|
|
Command* CommandFactory::createCloseAllFilesCommand()
|
|
|
|
{
|
|
|
|
return new CloseAllFilesCommand;
|
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
} // namespace app
|