| 
									
										
										
										
											2015-02-12 23:16:25 +08:00
										 |  |  | // Aseprite
 | 
					
						
							| 
									
										
										
										
											2021-10-08 05:56:39 +08:00
										 |  |  | // Copyright (C) 2018-2021  Igara Studio S.A.
 | 
					
						
							| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											2019-04-14 03:25:21 +08:00
										 |  |  | #include "doc/frame.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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-14 03:25:21 +08:00
										 |  |  | #include <memory>
 | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | #include <vector>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-14 03:25:21 +08:00
										 |  |  | namespace doc { | 
					
						
							|  |  |  |   class Layer; | 
					
						
							| 
									
										
										
										
											2019-08-11 01:37:18 +08:00
										 |  |  |   class PalettePicks; | 
					
						
							| 
									
										
										
										
											2019-04-14 03:25:21 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | namespace app { | 
					
						
							| 
									
										
										
										
											2019-04-14 03:25:21 +08:00
										 |  |  |   class ActiveSiteHandler; | 
					
						
							| 
									
										
										
										
											2020-09-25 22:13:52 +08:00
										 |  |  |   class Clipboard; | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  |   class Command; | 
					
						
							| 
									
										
										
										
											2018-07-07 22:54:44 +08:00
										 |  |  |   class Doc; | 
					
						
							| 
									
										
										
										
											2020-04-17 06:19:18 +08:00
										 |  |  |   class DocRange; | 
					
						
							| 
									
										
										
										
											2018-07-15 10:24:49 +08:00
										 |  |  |   class DocView; | 
					
						
							| 
									
										
										
										
											2019-12-11 23:27:56 +08:00
										 |  |  |   class Preferences; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-11 23:27:56 +08:00
										 |  |  |     Preferences& preferences() const; | 
					
						
							| 
									
										
										
										
											2020-09-25 22:13:52 +08:00
										 |  |  |     Clipboard* clipboard() const; | 
					
						
							| 
									
										
										
										
											2019-12-11 23:27:56 +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); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 00:51:41 +08:00
										 |  |  |     void sendDocumentToTop(Doc* doc); | 
					
						
							|  |  |  |     void closeDocument(Doc* doc); | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-07 13:47:42 +08:00
										 |  |  |     Site activeSite() const; | 
					
						
							| 
									
										
										
										
											2018-07-07 22:54:44 +08:00
										 |  |  |     Doc* activeDocument() const; | 
					
						
							|  |  |  |     void setActiveDocument(Doc* document); | 
					
						
							| 
									
										
										
										
											2019-04-14 03:25:21 +08:00
										 |  |  |     void setActiveLayer(doc::Layer* layer); | 
					
						
							|  |  |  |     void setActiveFrame(doc::frame_t frame); | 
					
						
							| 
									
										
										
										
											2020-04-17 06:19:18 +08:00
										 |  |  |     void setRange(const DocRange& range); | 
					
						
							| 
									
										
										
										
											2019-08-11 01:37:18 +08:00
										 |  |  |     void setSelectedColors(const doc::PalettePicks& picks); | 
					
						
							| 
									
										
										
										
											2019-11-16 23:09:33 +08:00
										 |  |  |     void setSelectedTiles(const doc::PalettePicks& picks); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-04 09:57:20 +08:00
										 |  |  |     void executeCommandFromMenuOrShortcut(Command* command, const Params& params = Params()); | 
					
						
							| 
									
										
										
										
											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-15 10:24:49 +08:00
										 |  |  |     virtual DocView* getFirstDocView(Doc* 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 22:54:44 +08:00
										 |  |  |     void onAddDocument(Doc* doc) override; | 
					
						
							|  |  |  |     void onRemoveDocument(Doc* doc) override; | 
					
						
							| 
									
										
										
										
											2018-07-07 13:47:42 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     virtual void onGetActiveSite(Site* site) const; | 
					
						
							| 
									
										
										
										
											2021-10-08 05:56:39 +08:00
										 |  |  |     virtual void onSetActiveDocument(Doc* doc, bool notify); | 
					
						
							| 
									
										
										
										
											2019-04-14 03:25:21 +08:00
										 |  |  |     virtual void onSetActiveLayer(doc::Layer* layer); | 
					
						
							|  |  |  |     virtual void onSetActiveFrame(const doc::frame_t frame); | 
					
						
							| 
									
										
										
										
											2020-04-17 06:19:18 +08:00
										 |  |  |     virtual void onSetRange(const DocRange& range); | 
					
						
							| 
									
										
										
										
											2019-08-11 01:37:18 +08:00
										 |  |  |     virtual void onSetSelectedColors(const doc::PalettePicks& picks); | 
					
						
							| 
									
										
										
										
											2019-11-16 23:09:33 +08:00
										 |  |  |     virtual void onSetSelectedTiles(const doc::PalettePicks& picks); | 
					
						
							| 
									
										
										
										
											2019-05-28 00:51:41 +08:00
										 |  |  |     virtual void onCloseDocument(Doc* doc); | 
					
						
							| 
									
										
										
										
											2018-05-07 11:11:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-07 22:54:44 +08:00
										 |  |  |     Doc* lastSelectedDoc() { return m_lastSelectedDoc; } | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   private: | 
					
						
							| 
									
										
										
										
											2019-04-14 03:25:21 +08:00
										 |  |  |     ActiveSiteHandler* activeSiteHandler() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-08 05:56:39 +08:00
										 |  |  |     // This must be defined before m_docs because ActiveSiteHandler
 | 
					
						
							|  |  |  |     // will be an observer of all documents.
 | 
					
						
							|  |  |  |     mutable std::unique_ptr<ActiveSiteHandler> m_activeSiteHandler; | 
					
						
							| 
									
										
										
										
											2019-12-11 23:27:56 +08:00
										 |  |  |     mutable Docs m_docs; | 
					
						
							| 
									
										
										
										
											2018-07-07 13:47:42 +08:00
										 |  |  |     ContextFlags m_flags;       // Last updated flags.
 | 
					
						
							| 
									
										
										
										
											2018-07-07 22:54:44 +08:00
										 |  |  |     Doc* m_lastSelectedDoc; | 
					
						
							| 
									
										
										
										
											2019-12-11 23:27:56 +08:00
										 |  |  |     mutable std::unique_ptr<Preferences> m_preferences; | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     DISABLE_COPYING(Context); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // namespace app
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 |