| 
									
										
										
										
											2015-02-12 23:16:25 +08:00
										 |  |  | // Aseprite
 | 
					
						
							| 
									
										
										
										
											2020-08-05 05:11:18 +08:00
										 |  |  | // Copyright (C) 2020  Igara Studio S.A.
 | 
					
						
							| 
									
										
										
										
											2017-12-01 01:51:13 +08:00
										 |  |  | // Copyright (C) 2001-2017  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.
 | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | #ifdef HAVE_CONFIG_H
 | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  | #include "config.h"
 | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | #include "app/commands/command.h"
 | 
					
						
							|  |  |  | #include "app/commands/params.h"
 | 
					
						
							|  |  |  | #include "app/console.h"
 | 
					
						
							| 
									
										
										
										
											2017-12-01 10:41:45 +08:00
										 |  |  | #include "app/i18n/strings.h"
 | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace app { | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-01 10:41:45 +08:00
										 |  |  | Command::Command(const char* id, CommandFlags flags) | 
					
						
							| 
									
										
										
										
											2015-05-02 06:41:18 +08:00
										 |  |  |   : m_id(id) | 
					
						
							|  |  |  |   , m_flags(flags) | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-12-01 10:41:45 +08:00
										 |  |  |   std::string strId = "commands."; | 
					
						
							|  |  |  |   strId += this->id(); | 
					
						
							| 
									
										
										
										
											2020-08-05 05:11:18 +08:00
										 |  |  |   if (auto s = Strings::instance()) | 
					
						
							|  |  |  |     m_friendlyName = s->translate(strId.c_str()); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     m_friendlyName = strId; | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Command::~Command() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-29 22:58:03 +08:00
										 |  |  | std::string Command::friendlyName() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return onGetFriendlyName(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-01 01:51:13 +08:00
										 |  |  | bool Command::needsParams() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return onNeedsParams(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-12 02:40:22 +08:00
										 |  |  | void Command::loadParams(const Params& params) | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  | { | 
					
						
							|  |  |  |   onLoadParams(params); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool Command::isEnabled(Context* context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   try { | 
					
						
							|  |  |  |     return onEnabled(context); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   catch (...) { | 
					
						
							|  |  |  |     // TODO add a status-bar item
 | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool Command::isChecked(Context* context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   try { | 
					
						
							|  |  |  |     return onChecked(context); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   catch (...) { | 
					
						
							|  |  |  |     // TODO add a status-bar item...
 | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Command::execute(Context* context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   onExecute(context); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-01 01:51:13 +08:00
										 |  |  | bool Command::onNeedsParams() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   // By default a command can be called without params
 | 
					
						
							|  |  |  |   return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Converts specified parameters to class members.
 | 
					
						
							| 
									
										
										
										
											2015-03-12 02:40:22 +08:00
										 |  |  | void Command::onLoadParams(const Params& params) | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  | { | 
					
						
							|  |  |  |   // do nothing
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-01 01:51:13 +08:00
										 |  |  | // Preconditions to execute the command
 | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  | bool Command::onEnabled(Context* context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-01 01:51:13 +08:00
										 |  |  | // Should the menu-item be checked?
 | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  | bool Command::onChecked(Context* context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-01 01:51:13 +08:00
										 |  |  | // Execute the command (after checking the preconditions).
 | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  | void Command::onExecute(Context* context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   // Do nothing
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-29 22:58:03 +08:00
										 |  |  | std::string Command::onGetFriendlyName() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-05-02 06:41:18 +08:00
										 |  |  |   return m_friendlyName; | 
					
						
							| 
									
										
										
										
											2014-10-29 22:58:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | } // namespace app
 |