From 9eb5ab4e25428c1c71283d83a6a1d1cacbb72888 Mon Sep 17 00:00:00 2001 From: Niels Martin Hansen Date: Fri, 6 Jul 2007 14:26:04 +0000 Subject: [PATCH] Can now set the grid selected from a Lua macro. Should still work in Ruby too but can't test that. Don't blame me if it breaks. Originally committed to SVN as r1384. --- aegisub/auto4_base.h | 2 +- aegisub/auto4_lua.cpp | 24 ++++++++++++++++--- aegisub/auto4_lua.h | 2 +- aegisub/auto4_ruby.cpp | 11 ++++----- aegisub/auto4_ruby.h | 2 +- aegisub/frame_main_events.cpp | 1 + automation/tests/selection-set-test.lua | 32 +++++++++++++++++++++++++ 7 files changed, 62 insertions(+), 12 deletions(-) create mode 100644 automation/tests/selection-set-test.lua diff --git a/aegisub/auto4_base.h b/aegisub/auto4_base.h index e8d9f975f..010c54460 100644 --- a/aegisub/auto4_base.h +++ b/aegisub/auto4_base.h @@ -110,7 +110,7 @@ namespace Automation4 { const wxString& GetDescription() const; virtual bool Validate(AssFile *subs, const std::vector &selected, int active) = 0; - virtual void Process(AssFile *subs, const std::vector &selected, int active, wxWindow * const progress_parent) = 0; + virtual void Process(AssFile *subs, std::vector &selected, int active, wxWindow * const progress_parent) = 0; }; diff --git a/aegisub/auto4_lua.cpp b/aegisub/auto4_lua.cpp index 27dae2411..d1bc5985d 100644 --- a/aegisub/auto4_lua.cpp +++ b/aegisub/auto4_lua.cpp @@ -561,7 +561,7 @@ namespace Automation4 { return result; } - void LuaFeatureMacro::Process(AssFile *subs, const std::vector &selected, int active, wxWindow * const progress_parent) + void LuaFeatureMacro::Process(AssFile *subs, std::vector &selected, int active, wxWindow * const progress_parent) { GetFeatureFunction(1); // 1 = processing function @@ -575,13 +575,31 @@ namespace Automation4 { ps->SetTitle(GetName()); // do call - LuaThreadedCall call(L, 3, 0); + // 3 args: subtitles, selected lines, active line + // 1 result: new selected lines + LuaThreadedCall call(L, 3, 1); ps->ShowModal(); wxThread::ExitCode code = call.Wait(); - (void) code; + (void) code; // ignore //if (code) ThrowError(); + // top of stack will be selected lines array, if any was returned + if (lua_istable(L, -1)) { + selected.clear(); + selected.reserve(lua_objlen(L, -1)); + lua_pushnil(L); + while (lua_next(L, -2)) { + if (lua_isnumber(L, -1)) { + selected.push_back(lua_tointeger(L, -1)); + } + lua_pop(L, 1); + } + std::sort(selected.begin(), selected.end()); + } + // either way, there will be something on the stack + lua_pop(L, 1); + delete ps; } diff --git a/aegisub/auto4_lua.h b/aegisub/auto4_lua.h index dbaf3c1f0..d3da43f04 100644 --- a/aegisub/auto4_lua.h +++ b/aegisub/auto4_lua.h @@ -222,7 +222,7 @@ namespace Automation4 { virtual ~LuaFeatureMacro() { } virtual bool Validate(AssFile *subs, const std::vector &selected, int active); - virtual void Process(AssFile *subs, const std::vector &selected, int active, wxWindow * const progress_parent); + virtual void Process(AssFile *subs, std::vector &selected, int active, wxWindow * const progress_parent); }; diff --git a/aegisub/auto4_ruby.cpp b/aegisub/auto4_ruby.cpp index 8f8eaf61f..7a3165832 100644 --- a/aegisub/auto4_ruby.cpp +++ b/aegisub/auto4_ruby.cpp @@ -334,7 +334,7 @@ namespace Automation4 { return false; } - void RubyFeatureMacro::Process(AssFile *subs, const std::vector &selected, int active, wxWindow * const progress_parent) + void RubyFeatureMacro::Process(AssFile *subs, std::vector &selected, int active, wxWindow * const progress_parent) { delete RubyProgressSink::inst; RubyProgressSink::inst = new RubyProgressSink(progress_parent, false); @@ -375,14 +375,13 @@ namespace Automation4 { break; case T_FIXNUM: // array of ints = selection + // i hope this works, can't test it -jfs int num = RARRAY(p)->len; - std::vector sel(num); + selected.clear(); + selected.reserve(num); for(int i = 0; i < num; ++i) { - sel[i] = FIX2INT(RARRAY(p)->ptr[i]); + selected.push_back(FIX2INT(RARRAY(p)->ptr[i])); } - FrameMain *frame = AegisubApp::Get()->frame; - frame->SubsBox->LoadFromAss(AssFile::top, true, true); - frame->SubsBox->SetSelectionFromAbsolute(sel); break; } } diff --git a/aegisub/auto4_ruby.h b/aegisub/auto4_ruby.h index fd641369a..8538b9352 100644 --- a/aegisub/auto4_ruby.h +++ b/aegisub/auto4_ruby.h @@ -195,7 +195,7 @@ namespace Automation4 { virtual ~RubyFeatureMacro() { } virtual bool Validate(AssFile *subs, const std::vector &selected, int active); - virtual void Process(AssFile *subs, const std::vector &selected, int active, wxWindow * const progress_parent); + virtual void Process(AssFile *subs, std::vector &selected, int active, wxWindow * const progress_parent); }; diff --git a/aegisub/frame_main_events.cpp b/aegisub/frame_main_events.cpp index f0413125e..23be37cc6 100644 --- a/aegisub/frame_main_events.cpp +++ b/aegisub/frame_main_events.cpp @@ -999,6 +999,7 @@ void FrameMain::OnAutomationMacro (wxCommandEvent &event) { activeMacroItems[event.GetId()-Menu_Automation_Macro]->Process(SubsBox->ass, selected_lines, first_sel, this); // Have the grid update its maps, this properly refreshes it to reflect the changed subs SubsBox->UpdateMaps(); + SubsBox->SetSelectionFromAbsolute(selected_lines); SubsBox->EndBatch(); SubsBox->CommitChanges(true, false); } diff --git a/automation/tests/selection-set-test.lua b/automation/tests/selection-set-test.lua new file mode 100644 index 000000000..60a576475 --- /dev/null +++ b/automation/tests/selection-set-test.lua @@ -0,0 +1,32 @@ +-- Automation 4 test file +-- Create a Macro feature, that displays some text + +script_name = "Automation 4 set-selection test" +script_description = "Test setting the grid selection" +script_author = "Niels Martin Hansen" +script_version = "1" + + +function selecttest(subtitles, selected_lines, active_line) + -- get line-id of first selected line + local lid = selected_lines[1] + -- insert a copy of line 'lid' before itself + subtitles[-lid] = subtitles[lid] + -- append a copy of the copy of the copied line + subtitles[0] = subtitles[lid] + -- grab the copied line + local l = subtitles[lid] + -- modify it + l.text = "A4 was here!" + -- and store it back + subtitles[lid] = l + -- select some new lines + selected_lines = { lid-1, lid, lid+1 } + -- and set undo point (never forget!) + aegisub.set_undo_point("Insert+select Stuff") + -- return the new selection + return selected_lines +end + + +aegisub.register_macro("Insert+select stuff", "Inserts some lines near the active line and selects the new lines", selecttest, nil)