Skip to content

Commit

Permalink
get UI text by ID rather than magic strings
Browse files Browse the repository at this point in the history
  • Loading branch information
rsjbailey committed Aug 13, 2021
1 parent 7ad2918 commit 8f97cae
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 18 deletions.
2 changes: 2 additions & 0 deletions reaper-adm-extension/src/reaper_adm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
64 changes: 46 additions & 18 deletions reaper-adm-extension/src/reaper_adm/pluginmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "exportaction.h"
#include "pluginsuite.h"
#include "pluginregistry.h"
#include "ui_text.h"
#include <unordered_map>

#ifdef WIN32
#include "win_nonblock_msg.h"
Expand Down Expand Up @@ -70,18 +72,24 @@ const std::map<const std::string, const int> 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)
{
using namespace admplug;
std::unique_ptr<ReaperHost> 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
};

Expand All @@ -108,7 +116,8 @@ extern "C" {

// Item right-click menu

auto admContextMenu = std::make_unique<SubMenu>("ADM");
auto admContextMenu =
std::make_unique<SubMenu>(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
Expand All @@ -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++);
Expand All @@ -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));
Expand All @@ -156,13 +169,14 @@ extern "C" {
defaultMenuPositions.at("Group"), *api));
// File menu

auto admFileMenu = std::make_unique<SubMenu>("Create project from ADM file");
auto admFileMenuUpdateCallback = [api](MenuItem& item) {};
auto admFileMenu =
std::make_unique<SubMenu>(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++);

Expand All @@ -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);
}
}
}
Expand All @@ -208,12 +231,13 @@ extern "C" {

// Insert menu

auto admInsertMenu = std::make_unique<SubMenu>("Import ADM file in to current project");
auto admInsertMenu =
std::make_unique<SubMenu>(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++);
Expand All @@ -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);
}
}
});
Expand Down
23 changes: 23 additions & 0 deletions reaper-adm-extension/src/reaper_adm/ui_text.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "ui_text.h"
#include <unordered_map>

std::string eps::uiText(eps::TextID item) {
static std::unordered_map<TextID, std::string> 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];
}
25 changes: 25 additions & 0 deletions reaper-adm-extension/src/reaper_adm/ui_text.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once
#include <string>

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);
}

0 comments on commit 8f97cae

Please sign in to comment.