aseprite/src/app/context.h

117 lines
3.1 KiB
C
Raw Normal View History

2015-02-12 12:16:25 -03:00
// Aseprite
// Copyright (C) 2018 Igara Studio S.A.
// 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.
#ifndef APP_CONTEXT_H_INCLUDED
#define APP_CONTEXT_H_INCLUDED
2014-03-29 19:40:17 -03:00
#pragma once
#include "app/commands/params.h"
#include "app/context_flags.h"
#include "app/context_observer.h"
2018-07-07 08:38:04 -03:00
#include "app/docs.h"
#include "app/docs_observer.h"
#include "base/disable_copying.h"
#include "base/exception.h"
#include "obs/observable.h"
#include "obs/signal.h"
#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;
class Transaction;
class CommandPreconditionException : public base::Exception {
public:
CommandPreconditionException() throw()
: base::Exception("Cannot execute the command because its pre-conditions are false.") { }
};
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;
};
class Context : public obs::observable<ContextObserver>,
2018-07-07 08:38:04 -03:00
public DocsObserver {
public:
Context();
virtual ~Context();
2018-07-07 08:38:04 -03:00
const Docs& documents() const { return m_docs; }
Docs& documents() { return m_docs; }
virtual bool isUIAvailable() const { return false; }
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);
Site activeSite() const;
2018-07-07 11:54:44 -03:00
Doc* activeDocument() const;
void setActiveDocument(Doc* document);
bool hasModifiedDocuments() const;
void notifyActiveSiteChanged();
void executeCommand(const char* commandName);
virtual void executeCommand(Command* command, const Params& params = Params());
2018-07-14 23:24:49 -03:00
virtual DocView* getFirstDocView(Doc* document) const {
return nullptr;
}
// Sets active/running transaction.
void setTransaction(Transaction* transaction);
Transaction* transaction() { return m_transaction; }
obs::signal<void (CommandExecutionEvent&)> BeforeCommandExecution;
obs::signal<void (CommandExecutionEvent&)> AfterCommandExecution;
protected:
2018-07-07 08:38:04 -03:00
// DocsObserver impl
2018-07-07 11:54:44 -03:00
void onAddDocument(Doc* doc) override;
void onRemoveDocument(Doc* doc) override;
virtual void onGetActiveSite(Site* site) const;
2018-07-07 11:54:44 -03:00
virtual void onSetActiveDocument(Doc* doc);
2018-07-07 11:54:44 -03:00
Doc* lastSelectedDoc() { return m_lastSelectedDoc; }
private:
2018-07-07 08:38:04 -03:00
Docs m_docs;
ContextFlags m_flags; // Last updated flags.
2018-07-07 11:54:44 -03:00
Doc* m_lastSelectedDoc;
Transaction* m_transaction;
DISABLE_COPYING(Context);
};
} // namespace app
#endif