From 8f97caed92298ea9bd821544d1680480bb621ef5 Mon Sep 17 00:00:00 2001 From: Richard Bailey Date: Fri, 13 Aug 2021 16:48:35 +0100 Subject: [PATCH] get UI text by ID rather than magic strings --- .../src/reaper_adm/CMakeLists.txt | 2 + .../src/reaper_adm/pluginmain.cpp | 64 +++++++++++++------ .../src/reaper_adm/ui_text.cpp | 23 +++++++ reaper-adm-extension/src/reaper_adm/ui_text.h | 25 ++++++++ 4 files changed, 96 insertions(+), 18 deletions(-) create mode 100644 reaper-adm-extension/src/reaper_adm/ui_text.cpp create mode 100644 reaper-adm-extension/src/reaper_adm/ui_text.h diff --git a/reaper-adm-extension/src/reaper_adm/CMakeLists.txt b/reaper-adm-extension/src/reaper_adm/CMakeLists.txt index 7dadf3eb7..21934fc5f 100644 --- a/reaper-adm-extension/src/reaper_adm/CMakeLists.txt +++ b/reaper-adm-extension/src/reaper_adm/CMakeLists.txt @@ -67,6 +67,7 @@ set(EXTENSION_SOURCES reaperguid.cpp reaperhost.cpp track.cpp + ui_text.cpp coordinate_conversion/coord_conv.cpp progress/importdialog.cpp progress/importlistener.cpp @@ -142,6 +143,7 @@ set(EXTENSION_HEADERS reaper_plugin_functions.h resource.h track.h + ui_text.h win_mem_debug.h win_nonblock_msg.h coordinate_conversion/coord_conv.cpp diff --git a/reaper-adm-extension/src/reaper_adm/pluginmain.cpp b/reaper-adm-extension/src/reaper_adm/pluginmain.cpp index be3bf69d4..b5c1df40a 100644 --- a/reaper-adm-extension/src/reaper_adm/pluginmain.cpp +++ b/reaper-adm-extension/src/reaper_adm/pluginmain.cpp @@ -12,6 +12,8 @@ #include "exportaction.h" #include "pluginsuite.h" #include "pluginregistry.h" +#include "ui_text.h" +#include #ifdef WIN32 #include "win_nonblock_msg.h" @@ -70,6 +72,9 @@ const std::map defaultMenuPositions = { } #endif +using eps::TextID; +using eps::uiText; + extern "C" { int REAPER_PLUGIN_DLL_EXPORT REAPER_PLUGIN_ENTRYPOINT(REAPER_PLUGIN_HINSTANCE hInstance, reaper_plugin_info_t *rec) { @@ -77,11 +82,14 @@ extern "C" { std::unique_ptr reaper; auto nonBlockingMessage = [rec](const char* text) { + auto title = uiText(TextID::EXTENSION_ERROR_TITLE); #ifdef WIN32 - // Windows version of Reaper locks up if you try show a message box during splash - winhelpers::NonBlockingMessageBox(text, "ADM Extension Error", MB_ICONEXCLAMATION); + // Windows version of Reaper locks up if you try show a message box during + // splash + winhelpers::NonBlockingMessageBox(text, title.c_str(), + MB_ICONEXCLAMATION); #else - MessageBox(rec->hwnd_main, text, "ADM Extension Error", MB_OK); + MessageBox(rec->hwnd_main, text, title.c_str(), MB_OK); #endif }; @@ -108,7 +116,8 @@ extern "C" { // Item right-click menu - auto admContextMenu = std::make_unique("ADM"); + auto admContextMenu = + std::make_unique(uiText(TextID::CONTEXT_MENU)); auto admContextMenuUpdateCallback = [api](MenuItem& item) { int numMediaItems = api->CountSelectedMediaItems(0); if (numMediaItems == 1) { // For now, lets just deal with one selection @@ -125,7 +134,7 @@ extern "C" { admContextMenu->updateCallback = admContextMenuUpdateCallback; for (auto& pluginSuite : *pluginRegistry->getPluginSuites()) { - std::string actionName("Explode using "); + auto actionName = uiText(TextID::EXPLODE_ACTION_PREFIX); actionName += pluginSuite.first; std::string actionSID("ADM_EXPLODE_"); actionSID += std::to_string(actionCounter++); @@ -140,7 +149,11 @@ extern "C" { importer.import(mediaItem, api); } else { - api.ShowMessageBox("Please select a source before running this action.", "ADM: Explode to Takes", 0); + api.ShowMessageBox( + uiText(TextID::EXPLODE_ERROR_DESCRIPTION) + .c_str(), + uiText(TextID::EXPLODE_ERROR_TITLE).c_str(), + 0); } }); explodeAction->setEnabled(pluginSuite.second->pluginSuiteUsable(*api)); @@ -156,13 +169,14 @@ extern "C" { defaultMenuPositions.at("Group"), *api)); // File menu - auto admFileMenu = std::make_unique("Create project from ADM file"); - auto admFileMenuUpdateCallback = [api](MenuItem& item) {}; + auto admFileMenu = + std::make_unique(uiText(TextID::CREATE_PROJECT_MENU)); + auto admFileMenuUpdateCallback = [api](MenuItem& item) {}; admFileMenu->updateCallback = admFileMenuUpdateCallback; for (auto& pluginSuite : *pluginRegistry->getPluginSuites()) { - std::string actionName("Create from ADM using "); - actionName += pluginSuite.first; + std::string actionName(uiText(TextID::CREATE_PROJECT_ACTION_PREFIX)); + actionName += pluginSuite.first; std::string actionSID("ADM_CREATE_PROJECT_"); actionSID += std::to_string(actionCounter++); @@ -180,12 +194,21 @@ extern "C" { auto filenameStr = std::string(filename); filenameStr += "/.wav"; memcpy(filename, filenameStr.data(), filenameStr.length() + 1); - if(api.GetUserFileNameForRead(filename, "ADM File to Open", "wav")) { - filenameStr = std::string(filename); + if (api.GetUserFileNameForRead( + filename, + uiText(TextID::CREATE_PROJECT_FILE_PROMPT) + .c_str(), + "wav")) { + filenameStr = std::string(filename); if(ImportAction::canMediaExplode_QuickCheck(api, filenameStr)) { importer.import(filenameStr, api); } else { - api.ShowMessageBox("Error: This file can not be imported.", "ADM Open", 0); + api.ShowMessageBox( + uiText(TextID::CREATE_PROJECT_ERROR_DESCRIPTION) + .c_str(), + uiText(TextID::CREATE_PROJECT_ERROR_TITLE) + .c_str(), + 0); } } } @@ -208,12 +231,13 @@ extern "C" { // Insert menu - auto admInsertMenu = std::make_unique("Import ADM file in to current project"); + auto admInsertMenu = + std::make_unique(uiText(TextID::IMPORT_MENU)); auto admInsertMenuUpdateCallback = [api](MenuItem& item) {}; admInsertMenu->updateCallback = admInsertMenuUpdateCallback; for (auto& pluginSuite : *pluginRegistry->getPluginSuites()) { - std::string actionName("Import ADM file using "); + std::string actionName(uiText(TextID::IMPORT_ACTION_PREFIX)); actionName += pluginSuite.first; std::string actionSID("ADM_IMPORT_"); actionSID += std::to_string(actionCounter++); @@ -229,12 +253,16 @@ extern "C" { auto filenameStr = std::string(filename); filenameStr += "/.wav"; memcpy(filename, filenameStr.data(), filenameStr.length() + 1); - if (api.GetUserFileNameForRead(filename, "ADM File to Import", "wav")) { - filenameStr = std::string(filename); + if (api.GetUserFileNameForRead( + filename, uiText(TextID::IMPORT_FILE_PROMPT).c_str(), + "wav")) { + filenameStr = std::string(filename); if(ImportAction::canMediaExplode_QuickCheck(api, filenameStr)) { importer.import(filenameStr, api); } else { - api.ShowMessageBox("Error: This file can not be imported.", "ADM Import", 0); + api.ShowMessageBox( + uiText(TextID::IMPORT_ERROR_DESCRIPTION).c_str(), + uiText(TextID::IMPORT_ERROR_TITLE).c_str(), 0); } } }); diff --git a/reaper-adm-extension/src/reaper_adm/ui_text.cpp b/reaper-adm-extension/src/reaper_adm/ui_text.cpp new file mode 100644 index 000000000..3bb6efd2a --- /dev/null +++ b/reaper-adm-extension/src/reaper_adm/ui_text.cpp @@ -0,0 +1,23 @@ +#include "ui_text.h" +#include + +std::string eps::uiText(eps::TextID item) { + static std::unordered_map dictionary { + {TextID::EXTENSION_ERROR_TITLE, "ADM Extension Error"}, + {TextID::CONTEXT_MENU, "ADM"}, + {TextID::EXPLODE_ACTION_PREFIX, "Explode using "}, + {TextID::EXPLODE_ERROR_TITLE, "ADM: Explode to Takes"}, + {TextID::EXPLODE_ERROR_DESCRIPTION, "Please select a source before running this action."}, + {TextID::CREATE_PROJECT_MENU, "Create project from ADM file"}, + {TextID::CREATE_PROJECT_ACTION_PREFIX, "Create from ADM using "}, + {TextID::CREATE_PROJECT_FILE_PROMPT, "ADM File to Open"}, + {TextID::CREATE_PROJECT_ERROR_TITLE, "Import ADM file in to current project"}, + {TextID::CREATE_PROJECT_ERROR_DESCRIPTION, "Error: This file can not be imported."}, + {TextID::IMPORT_MENU, "ADM File to Import"}, + {TextID::IMPORT_ACTION_PREFIX, "Import ADM file using "}, + {TextID::IMPORT_ERROR_TITLE, "Error: This file can not be imported."}, + {TextID::IMPORT_ERROR_DESCRIPTION, "ADM Import"} + }; + assert(dictionary.find(item) != dictionary.end()); + return(dictionary.find(item) == dictionary.end()) ? "" : dictionary[item]; +} diff --git a/reaper-adm-extension/src/reaper_adm/ui_text.h b/reaper-adm-extension/src/reaper_adm/ui_text.h new file mode 100644 index 000000000..09954b49a --- /dev/null +++ b/reaper-adm-extension/src/reaper_adm/ui_text.h @@ -0,0 +1,25 @@ +#pragma once +#include + +namespace eps { + +enum class TextID { + EXTENSION_ERROR_TITLE, + CONTEXT_MENU, + EXPLODE_ACTION_PREFIX, + EXPLODE_ERROR_TITLE, + EXPLODE_ERROR_DESCRIPTION, + CREATE_PROJECT_ACTION_PREFIX, + CREATE_PROJECT_MENU, + CREATE_PROJECT_FILE_PROMPT, + CREATE_PROJECT_ERROR_TITLE, + CREATE_PROJECT_ERROR_DESCRIPTION, + IMPORT_MENU, + IMPORT_ACTION_PREFIX, + IMPORT_FILE_PROMPT, + IMPORT_ERROR_TITLE, + IMPORT_ERROR_DESCRIPTION +}; + +std::string uiText(TextID item); +}