2015-02-12 12:16:25 -03:00
|
|
|
// Aseprite
|
2018-05-07 00:11:50 -03:00
|
|
|
// Copyright (C) 2001-2018 David Capello
|
2015-02-12 12:16:25 -03:00
|
|
|
//
|
2016-08-26 17:02:58 -03:00
|
|
|
// This program is distributed under the terms of
|
|
|
|
// the End-User License Agreement for Aseprite.
|
2013-08-05 21:20:19 -03:00
|
|
|
|
|
|
|
#ifndef APP_CONTEXT_H_INCLUDED
|
|
|
|
#define APP_CONTEXT_H_INCLUDED
|
2014-03-29 19:40:17 -03:00
|
|
|
#pragma once
|
2013-08-05 21:20:19 -03:00
|
|
|
|
2015-03-11 15:40:22 -03:00
|
|
|
#include "app/commands/params.h"
|
2013-08-05 21:20:19 -03:00
|
|
|
#include "app/context_flags.h"
|
2018-07-07 02:47:42 -03:00
|
|
|
#include "app/context_observer.h"
|
2018-07-07 08:38:04 -03:00
|
|
|
#include "app/docs.h"
|
|
|
|
#include "app/docs_observer.h"
|
2013-08-05 21:20:19 -03:00
|
|
|
#include "base/disable_copying.h"
|
|
|
|
#include "base/exception.h"
|
2018-07-07 02:47:42 -03:00
|
|
|
#include "obs/observable.h"
|
2016-09-13 15:02:00 -03:00
|
|
|
#include "obs/signal.h"
|
2013-08-05 21:20:19 -03:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace app {
|
|
|
|
class Command;
|
2018-07-07 11:54:44 -03:00
|
|
|
class Doc;
|
2018-07-14 23:24:49 -03:00
|
|
|
class DocView;
|
2018-08-20 14:20:27 -03:00
|
|
|
class Transaction;
|
2013-08-05 21:20:19 -03:00
|
|
|
|
|
|
|
class CommandPreconditionException : public base::Exception {
|
|
|
|
public:
|
|
|
|
CommandPreconditionException() throw()
|
|
|
|
: base::Exception("Cannot execute the command because its pre-conditions are false.") { }
|
|
|
|
};
|
|
|
|
|
2015-08-13 17:25:39 -03:00
|
|
|
class CommandExecutionEvent {
|
|
|
|
public:
|
|
|
|
CommandExecutionEvent(Command* command)
|
|
|
|
: m_command(command), m_canceled(false) {
|
|
|
|
}
|
|
|
|
|
|
|
|
Command* command() const { return m_command; }
|
|
|
|
|
|
|
|
// True if the command was canceled or simulated by an
|
|
|
|
// observer/signal slot.
|
|
|
|
bool isCanceled() const { return m_canceled; }
|
|
|
|
void cancel() {
|
|
|
|
m_canceled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Command* m_command;
|
|
|
|
bool m_canceled;
|
|
|
|
};
|
|
|
|
|
2018-07-07 02:47:42 -03:00
|
|
|
class Context : public obs::observable<ContextObserver>,
|
2018-07-07 08:38:04 -03:00
|
|
|
public DocsObserver {
|
2013-08-05 21:20:19 -03:00
|
|
|
public:
|
2014-07-19 22:01:39 -03:00
|
|
|
Context();
|
2018-07-07 02:47:42 -03:00
|
|
|
virtual ~Context();
|
|
|
|
|
2018-07-07 08:38:04 -03:00
|
|
|
const Docs& documents() const { return m_docs; }
|
|
|
|
Docs& documents() { return m_docs; }
|
2013-08-05 21:20:19 -03:00
|
|
|
|
2015-05-18 17:04:31 -03:00
|
|
|
virtual bool isUIAvailable() const { return false; }
|
2013-08-05 21:20:19 -03:00
|
|
|
virtual bool isRecordingMacro() const { return false; }
|
|
|
|
virtual bool isExecutingMacro() const { return false; }
|
|
|
|
virtual bool isExecutingScript() const { return false; }
|
|
|
|
|
|
|
|
bool checkFlags(uint32_t flags) const { return m_flags.check(flags); }
|
|
|
|
void updateFlags() { m_flags.update(this); }
|
|
|
|
|
2018-07-07 11:54:44 -03:00
|
|
|
void sendDocumentToTop(Doc* document);
|
2013-08-05 21:20:19 -03:00
|
|
|
|
2018-07-07 02:47:42 -03:00
|
|
|
Site activeSite() const;
|
2018-07-07 11:54:44 -03:00
|
|
|
Doc* activeDocument() const;
|
|
|
|
void setActiveDocument(Doc* document);
|
2016-06-08 13:27:36 -03:00
|
|
|
bool hasModifiedDocuments() const;
|
2018-07-07 02:47:42 -03:00
|
|
|
void notifyActiveSiteChanged();
|
2013-08-05 21:20:19 -03:00
|
|
|
|
2014-11-08 19:57:46 -03:00
|
|
|
void executeCommand(const char* commandName);
|
2015-03-11 15:40:22 -03:00
|
|
|
virtual void executeCommand(Command* command, const Params& params = Params());
|
2013-08-05 21:20:19 -03:00
|
|
|
|
2018-07-14 23:24:49 -03:00
|
|
|
virtual DocView* getFirstDocView(Doc* document) const {
|
2018-05-07 00:11:50 -03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-08-20 14:20:27 -03:00
|
|
|
// Sets active/running transaction.
|
|
|
|
void setTransaction(Transaction* transaction);
|
|
|
|
Transaction* transaction() { return m_transaction; }
|
|
|
|
|
2016-09-13 15:02:00 -03:00
|
|
|
obs::signal<void (CommandExecutionEvent&)> BeforeCommandExecution;
|
|
|
|
obs::signal<void (CommandExecutionEvent&)> AfterCommandExecution;
|
2013-08-05 21:20:19 -03:00
|
|
|
|
|
|
|
protected:
|
2018-07-07 08:38:04 -03:00
|
|
|
// DocsObserver impl
|
2018-07-07 14:05:06 -03:00
|
|
|
void onCreateDocument(CreateDocArgs* args) override;
|
2018-07-07 11:54:44 -03:00
|
|
|
void onAddDocument(Doc* doc) override;
|
|
|
|
void onRemoveDocument(Doc* doc) override;
|
2018-07-07 02:47:42 -03:00
|
|
|
|
|
|
|
virtual void onGetActiveSite(Site* site) const;
|
2018-07-07 11:54:44 -03:00
|
|
|
virtual void onSetActiveDocument(Doc* doc);
|
2018-05-07 00:11:50 -03:00
|
|
|
|
2018-07-07 11:54:44 -03:00
|
|
|
Doc* lastSelectedDoc() { return m_lastSelectedDoc; }
|
2013-08-05 21:20:19 -03:00
|
|
|
|
|
|
|
private:
|
2018-07-07 08:38:04 -03:00
|
|
|
Docs m_docs;
|
2018-07-07 02:47:42 -03:00
|
|
|
ContextFlags m_flags; // Last updated flags.
|
2018-07-07 11:54:44 -03:00
|
|
|
Doc* m_lastSelectedDoc;
|
2018-08-20 14:20:27 -03:00
|
|
|
Transaction* m_transaction;
|
2013-08-05 21:20:19 -03:00
|
|
|
|
|
|
|
DISABLE_COPYING(Context);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace app
|
|
|
|
|
|
|
|
#endif
|