2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
2018-05-07 11:11:50 +08:00
|
|
|
// Copyright (C) 2001-2018 David Capello
|
2015-02-12 23:16:25 +08:00
|
|
|
//
|
2016-08-27 04:02:58 +08:00
|
|
|
// This program is distributed under the terms of
|
|
|
|
|
// the End-User License Agreement for Aseprite.
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
|
#ifndef APP_CONTEXT_H_INCLUDED
|
|
|
|
|
#define APP_CONTEXT_H_INCLUDED
|
2014-03-30 06:40:17 +08:00
|
|
|
#pragma once
|
2013-08-06 08:20:19 +08:00
|
|
|
|
2015-03-12 02:40:22 +08:00
|
|
|
#include "app/commands/params.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/context_flags.h"
|
2018-07-07 13:47:42 +08:00
|
|
|
#include "app/context_observer.h"
|
2018-07-07 19:38:04 +08:00
|
|
|
#include "app/docs.h"
|
|
|
|
|
#include "app/docs_observer.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "base/disable_copying.h"
|
|
|
|
|
#include "base/exception.h"
|
2018-07-07 13:47:42 +08:00
|
|
|
#include "obs/observable.h"
|
2016-09-14 02:02:00 +08:00
|
|
|
#include "obs/signal.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
namespace app {
|
|
|
|
|
class Command;
|
|
|
|
|
class Document;
|
2018-05-07 11:11:50 +08:00
|
|
|
class DocumentView;
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
|
class CommandPreconditionException : public base::Exception {
|
|
|
|
|
public:
|
|
|
|
|
CommandPreconditionException() throw()
|
|
|
|
|
: base::Exception("Cannot execute the command because its pre-conditions are false.") { }
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-14 04:25:39 +08: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 13:47:42 +08:00
|
|
|
class Context : public obs::observable<ContextObserver>,
|
2018-07-07 19:38:04 +08:00
|
|
|
public DocsObserver {
|
2013-08-06 08:20:19 +08:00
|
|
|
public:
|
2014-07-20 09:01:39 +08:00
|
|
|
Context();
|
2018-07-07 13:47:42 +08:00
|
|
|
virtual ~Context();
|
|
|
|
|
|
2018-07-07 19:38:04 +08:00
|
|
|
const Docs& documents() const { return m_docs; }
|
|
|
|
|
Docs& documents() { return m_docs; }
|
2013-08-06 08:20:19 +08:00
|
|
|
|
2015-05-19 04:04:31 +08:00
|
|
|
virtual bool isUIAvailable() const { return false; }
|
2013-08-06 08:20:19 +08: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 13:47:42 +08:00
|
|
|
void sendDocumentToTop(Document* document);
|
2013-08-06 08:20:19 +08:00
|
|
|
|
2018-07-07 13:47:42 +08:00
|
|
|
Site activeSite() const;
|
|
|
|
|
Document* activeDocument() const;
|
|
|
|
|
void setActiveDocument(Document* document);
|
2016-06-09 00:27:36 +08:00
|
|
|
bool hasModifiedDocuments() const;
|
2018-07-07 13:47:42 +08:00
|
|
|
void notifyActiveSiteChanged();
|
2013-08-06 08:20:19 +08:00
|
|
|
|
2014-11-09 06:57:46 +08:00
|
|
|
void executeCommand(const char* commandName);
|
2015-03-12 02:40:22 +08:00
|
|
|
virtual void executeCommand(Command* command, const Params& params = Params());
|
2013-08-06 08:20:19 +08:00
|
|
|
|
2018-07-07 13:47:42 +08:00
|
|
|
virtual DocumentView* getFirstDocumentView(Document* document) const {
|
2018-05-07 11:11:50 +08:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-14 02:02:00 +08:00
|
|
|
obs::signal<void (CommandExecutionEvent&)> BeforeCommandExecution;
|
|
|
|
|
obs::signal<void (CommandExecutionEvent&)> AfterCommandExecution;
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
|
protected:
|
2018-07-07 19:38:04 +08:00
|
|
|
// DocsObserver impl
|
2018-07-07 13:47:42 +08:00
|
|
|
void onCreateDocument(CreateDocumentArgs* args) override;
|
|
|
|
|
void onAddDocument(Document* doc) override;
|
|
|
|
|
void onRemoveDocument(Document* doc) override;
|
|
|
|
|
|
|
|
|
|
virtual void onGetActiveSite(Site* site) const;
|
|
|
|
|
virtual void onSetActiveDocument(Document* doc);
|
2018-05-07 11:11:50 +08:00
|
|
|
|
|
|
|
|
Document* lastSelectedDoc() { return m_lastSelectedDoc; }
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
|
private:
|
2018-07-07 19:38:04 +08:00
|
|
|
Docs m_docs;
|
2018-07-07 13:47:42 +08:00
|
|
|
ContextFlags m_flags; // Last updated flags.
|
2018-05-07 11:11:50 +08:00
|
|
|
Document* m_lastSelectedDoc;
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
|
DISABLE_COPYING(Context);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace app
|
|
|
|
|
|
|
|
|
|
#endif
|