Let macros update their help string from the validate function

If the second return value from the validate function exists and is a
non-empty string, replace the macro's help text with that string.

Revert r6327, as this is a better solution to the same problem.

Closes #1413.

Originally committed to SVN as r6352.
This commit is contained in:
Thomas Goyne 2012-01-25 00:21:37 +00:00
parent 3140d902da
commit a24f1692be
3 changed files with 18 additions and 31 deletions

View file

@ -596,16 +596,12 @@ namespace Automation4 {
LuaCommand::LuaCommand(lua_State *L) LuaCommand::LuaCommand(lua_State *L)
: LuaFeature(L) : LuaFeature(L)
, display(check_wxstring(L, 1)) , display(check_wxstring(L, 1))
, help(get_wxstring(L, 2))
, cmd_type(cmd::COMMAND_NORMAL) , cmd_type(cmd::COMMAND_NORMAL)
{ {
lua_getfield(L, LUA_REGISTRYINDEX, "filename"); lua_getfield(L, LUA_REGISTRYINDEX, "filename");
cmd_name = STD_STR(wxString::Format("automation/lua/%s/%s", get_wxstring(L, -1), check_wxstring(L, 1))); cmd_name = STD_STR(wxString::Format("automation/lua/%s/%s", get_wxstring(L, -1), check_wxstring(L, 1)));
if (lua_isstring(L, 2))
help = get_wxstring(L, 2);
else if (lua_isfunction(L, 2))
cmd_type |= cmd::COMMAND_DYNAMIC_HELP;
if (!lua_isfunction(L, 3)) if (!lua_isfunction(L, 3))
luaL_error(L, "The macro processing function must be a function"); luaL_error(L, "The macro processing function must be a function");
@ -618,11 +614,6 @@ namespace Automation4 {
// new table for containing the functions for this feature // new table for containing the functions for this feature
lua_newtable(L); lua_newtable(L);
// store help string function
lua_pushstring(L, "help");
lua_pushvalue(L, 2);
lua_rawset(L, -3);
// store processing function // store processing function
lua_pushstring(L, "run"); lua_pushstring(L, "run");
lua_pushvalue(L, 3); lua_pushvalue(L, 3);
@ -650,17 +641,6 @@ namespace Automation4 {
LuaScript::GetScriptObject(L)->UnregisterCommand(this); LuaScript::GetScriptObject(L)->UnregisterCommand(this);
} }
wxString LuaCommand::StrHelp() const
{
if (!(cmd_type & cmd::COMMAND_DYNAMIC_HELP)) return help;
GetFeatureFunction("help");
lua_pcall(L, 0, 1, 0);
wxString result = get_wxstring(L, -1);
lua_pop(L, 1);
return result;
}
static int transform_selection(lua_State *L, const agi::Context *c) static int transform_selection(lua_State *L, const agi::Context *c)
{ {
std::set<AssDialogue*> sel = c->selectionController->GetSelectedSet(); std::set<AssDialogue*> sel = c->selectionController->GetSelectedSet();
@ -695,18 +675,25 @@ namespace Automation4 {
LuaAssFile *subsobj = new LuaAssFile(L, c->ass); LuaAssFile *subsobj = new LuaAssFile(L, c->ass);
lua_pushinteger(L, transform_selection(L, c)); lua_pushinteger(L, transform_selection(L, c));
int err = lua_pcall(L, 3, 1, 0); int err = lua_pcall(L, 3, 2, 0);
subsobj->ProcessingComplete(); subsobj->ProcessingComplete();
bool result = false; if (err) {
if (err)
wxLogWarning("Runtime error in Lua macro validation function:\n%s", get_wxstring(L, -1)); wxLogWarning("Runtime error in Lua macro validation function:\n%s", get_wxstring(L, -1));
else
result = !!lua_toboolean(L, -1);
// clean up stack (result or error message)
lua_pop(L, 1); lua_pop(L, 1);
return false;
}
bool result = !!lua_toboolean(L, -2);
wxString new_help_string(get_wxstring(L, -1));
if (new_help_string.size()) {
help = new_help_string;
cmd_type |= cmd::COMMAND_DYNAMIC_HELP;
}
lua_pop(L, 2);
return result; return result;
} }

View file

@ -259,7 +259,7 @@ namespace Automation4 {
const char* name() const { return cmd_name.c_str(); } const char* name() const { return cmd_name.c_str(); }
wxString StrMenu(const agi::Context *) const { return display; } wxString StrMenu(const agi::Context *) const { return display; }
wxString StrDisplay(const agi::Context *) const { return display; } wxString StrDisplay(const agi::Context *) const { return display; }
wxString StrHelp() const; wxString StrHelp() const { return help; }
int Type() const { return cmd_type; } int Type() const { return cmd_type; }

View file

@ -146,12 +146,12 @@ class CommandManager {
void UpdateItem(std::pair<std::string, wxMenuItem*> const& item) { void UpdateItem(std::pair<std::string, wxMenuItem*> const& item) {
cmd::Command *c = cmd::get(item.first); cmd::Command *c = cmd::get(item.first);
int flags = c->Type(); int flags = c->Type();
if (flags & cmd::COMMAND_VALIDATE)
item.second->Enable(c->Validate(context));
if (flags & cmd::COMMAND_DYNAMIC_NAME) if (flags & cmd::COMMAND_DYNAMIC_NAME)
UpdateItemName(item); UpdateItemName(item);
if (flags & cmd::COMMAND_DYNAMIC_HELP) if (flags & cmd::COMMAND_DYNAMIC_HELP)
item.second->SetHelp(c->StrHelp()); item.second->SetHelp(c->StrHelp());
if (flags & cmd::COMMAND_VALIDATE)
item.second->Enable(c->Validate(context));
if (flags & cmd::COMMAND_RADIO || flags & cmd::COMMAND_TOGGLE) { if (flags & cmd::COMMAND_RADIO || flags & cmd::COMMAND_TOGGLE) {
bool check = c->IsActive(context); bool check = c->IsActive(context);
// Don't call Check(false) on radio items as this causes wxGtk to // Don't call Check(false) on radio items as this causes wxGtk to