1 // Aseprite
2 // Copyright (C) 2001-2018  David Capello
3 //
4 // This program is distributed under the terms of
5 // the End-User License Agreement for Aseprite.
6 
7 #ifndef APP_CONTEXT_H_INCLUDED
8 #define APP_CONTEXT_H_INCLUDED
9 #pragma once
10 
11 #include "app/commands/params.h"
12 #include "app/context_flags.h"
13 #include "app/context_observer.h"
14 #include "app/docs.h"
15 #include "app/docs_observer.h"
16 #include "base/disable_copying.h"
17 #include "base/exception.h"
18 #include "obs/observable.h"
19 #include "obs/signal.h"
20 
21 #include <vector>
22 
23 namespace app {
24   class Command;
25   class Doc;
26   class DocView;
27 
28   class CommandPreconditionException : public base::Exception {
29   public:
CommandPreconditionException()30     CommandPreconditionException() throw()
31     : base::Exception("Cannot execute the command because its pre-conditions are false.") { }
32   };
33 
34   class CommandExecutionEvent {
35   public:
CommandExecutionEvent(Command * command)36     CommandExecutionEvent(Command* command)
37       : m_command(command), m_canceled(false) {
38     }
39 
command()40     Command* command() const { return m_command; }
41 
42     // True if the command was canceled or simulated by an
43     // observer/signal slot.
isCanceled()44     bool isCanceled() const { return m_canceled; }
cancel()45     void cancel() {
46       m_canceled = true;
47     }
48 
49   private:
50     Command* m_command;
51     bool m_canceled;
52   };
53 
54   class Context : public obs::observable<ContextObserver>,
55                   public DocsObserver {
56   public:
57     Context();
58     virtual ~Context();
59 
documents()60     const Docs& documents() const { return m_docs; }
documents()61     Docs& documents() { return m_docs; }
62 
isUIAvailable()63     virtual bool isUIAvailable() const     { return false; }
isRecordingMacro()64     virtual bool isRecordingMacro() const  { return false; }
isExecutingMacro()65     virtual bool isExecutingMacro() const  { return false; }
isExecutingScript()66     virtual bool isExecutingScript() const { return false; }
67 
checkFlags(uint32_t flags)68     bool checkFlags(uint32_t flags) const { return m_flags.check(flags); }
updateFlags()69     void updateFlags() { m_flags.update(this); }
70 
71     void sendDocumentToTop(Doc* document);
72 
73     Site activeSite() const;
74     Doc* activeDocument() const;
75     void setActiveDocument(Doc* document);
76     bool hasModifiedDocuments() const;
77     void notifyActiveSiteChanged();
78 
79     void executeCommand(const char* commandName);
80     virtual void executeCommand(Command* command, const Params& params = Params());
81 
getFirstDocView(Doc * document)82     virtual DocView* getFirstDocView(Doc* document) const {
83       return nullptr;
84     }
85 
86     obs::signal<void (CommandExecutionEvent&)> BeforeCommandExecution;
87     obs::signal<void (CommandExecutionEvent&)> AfterCommandExecution;
88 
89   protected:
90     // DocsObserver impl
91     void onCreateDocument(CreateDocArgs* args) override;
92     void onAddDocument(Doc* doc) override;
93     void onRemoveDocument(Doc* doc) override;
94 
95     virtual void onGetActiveSite(Site* site) const;
96     virtual void onSetActiveDocument(Doc* doc);
97 
lastSelectedDoc()98     Doc* lastSelectedDoc() { return m_lastSelectedDoc; }
99 
100   private:
101     Docs m_docs;
102     ContextFlags m_flags;       // Last updated flags.
103     Doc* m_lastSelectedDoc;
104 
105     DISABLE_COPYING(Context);
106   };
107 
108 } // namespace app
109 
110 #endif
111