| 1 | // Aseprite |
|---|---|
| 2 | // Copyright (C) 2020 Igara Studio S.A. |
| 3 | // Copyright (C) 2001-2017 David Capello |
| 4 | // |
| 5 | // This program is distributed under the terms of |
| 6 | // the End-User License Agreement for Aseprite. |
| 7 | |
| 8 | #ifdef HAVE_CONFIG_H |
| 9 | #include "config.h" |
| 10 | #endif |
| 11 | |
| 12 | #include "app/commands/command.h" |
| 13 | #include "app/commands/params.h" |
| 14 | #include "app/console.h" |
| 15 | #include "app/i18n/strings.h" |
| 16 | |
| 17 | namespace app { |
| 18 | |
| 19 | Command::Command(const char* id, CommandFlags flags) |
| 20 | : m_id(id) |
| 21 | , m_flags(flags) |
| 22 | { |
| 23 | generateFriendlyName(); |
| 24 | } |
| 25 | |
| 26 | Command::~Command() |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | std::string Command::friendlyName() const |
| 31 | { |
| 32 | return onGetFriendlyName(); |
| 33 | } |
| 34 | |
| 35 | bool Command::needsParams() const |
| 36 | { |
| 37 | return onNeedsParams(); |
| 38 | } |
| 39 | |
| 40 | void Command::loadParams(const Params& params) |
| 41 | { |
| 42 | onLoadParams(params); |
| 43 | } |
| 44 | |
| 45 | bool Command::isEnabled(Context* context) |
| 46 | { |
| 47 | try { |
| 48 | return onEnabled(context); |
| 49 | } |
| 50 | catch (...) { |
| 51 | // TODO add a status-bar item |
| 52 | return false; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | bool Command::isChecked(Context* context) |
| 57 | { |
| 58 | try { |
| 59 | return onChecked(context); |
| 60 | } |
| 61 | catch (...) { |
| 62 | // TODO add a status-bar item... |
| 63 | return false; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void Command::generateFriendlyName() |
| 68 | { |
| 69 | std::string strId = "commands."+ this->id(); |
| 70 | if (auto s = Strings::instance()) |
| 71 | m_friendlyName = s->translate(strId.c_str()); |
| 72 | else |
| 73 | m_friendlyName = strId; |
| 74 | } |
| 75 | |
| 76 | void Command::execute(Context* context) |
| 77 | { |
| 78 | onExecute(context); |
| 79 | } |
| 80 | |
| 81 | bool Command::onNeedsParams() const |
| 82 | { |
| 83 | // By default a command can be called without params |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | // Converts specified parameters to class members. |
| 88 | void Command::onLoadParams(const Params& params) |
| 89 | { |
| 90 | // do nothing |
| 91 | } |
| 92 | |
| 93 | // Preconditions to execute the command |
| 94 | bool Command::onEnabled(Context* context) |
| 95 | { |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | // Should the menu-item be checked? |
| 100 | bool Command::onChecked(Context* context) |
| 101 | { |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | // Execute the command (after checking the preconditions). |
| 106 | void Command::onExecute(Context* context) |
| 107 | { |
| 108 | // Do nothing |
| 109 | } |
| 110 | |
| 111 | std::string Command::onGetFriendlyName() const |
| 112 | { |
| 113 | return m_friendlyName; |
| 114 | } |
| 115 | |
| 116 | } // namespace app |
| 117 |