From e5afaf8a455698841b633c265c8ba6d93b5e3b5a Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Mon, 16 Sep 2013 11:02:20 -0700 Subject: [PATCH] Handle subtitle lines pasted into the edit box If the text being pasted can be parsed as ASS dialogue events, then do so even if the edit box is focused since it's rather unlikely that the user actually wants ASS subtitle events in their subs. --- aegisub/src/command/edit.cpp | 37 ++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/aegisub/src/command/edit.cpp b/aegisub/src/command/edit.cpp index 8c55349c8..8bbe7e0de 100644 --- a/aegisub/src/command/edit.cpp +++ b/aegisub/src/command/edit.cpp @@ -736,6 +736,37 @@ struct edit_line_join_keep_first : public validate_sel_multiple { } }; +static bool try_paste_lines(agi::Context *c) { + std::string data = GetClipboard(); + boost::trim_left(data); + if (!boost::starts_with(data, "Dialogue:")) return false; + + EntryList parsed; + boost::char_separator sep("\r\n"); + for (auto curdata : boost::tokenizer>(data, sep)) { + boost::trim(curdata); + try { + parsed.push_back(*new AssDialogue(curdata)); + } + catch (...) { + parsed.clear_and_dispose([](AssEntry *e) { delete e; }); + return false; + } + } + + AssDialogue *new_active = static_cast(&*parsed.begin()); + SubtitleSelection new_selection; + for (auto& line : parsed) + new_selection.insert(static_cast(&line)); + + auto pos = c->ass->Line.iterator_to(*c->selectionController->GetActiveLine()); + c->ass->Line.splice(pos, parsed, parsed.begin(), parsed.end()); + c->ass->Commit(_("paste"), AssFile::COMMIT_DIAG_ADDREM); + c->selectionController->SetSelectionAndActive(new_selection, new_active); + + return true; +} + /// Paste subtitles. struct edit_line_paste : public Command { CMD_NAME("edit/line/paste") @@ -754,8 +785,10 @@ struct edit_line_paste : public Command { } void operator()(agi::Context *c) { - if (wxTextEntryBase *ctrl = dynamic_cast(c->parent->FindFocus())) - ctrl->Paste(); + if (wxTextEntryBase *ctrl = dynamic_cast(c->parent->FindFocus())) { + if (!try_paste_lines(c)) + ctrl->Paste(); + } else { auto pos = c->ass->Line.iterator_to(*c->selectionController->GetActiveLine()); paste_lines(c, false, [=](AssDialogue *new_line) -> AssDialogue * {