From 035445e93a26303047714873796dfa96f262a604 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Wed, 19 Oct 2011 03:24:10 +0000 Subject: [PATCH] When the cursor is in a comment block, insert override tags at the beginning of the block rather than inside the comment Originally committed to SVN as r5757. --- aegisub/src/subs_edit_box.cpp | 62 ++++++++++++++++++++++------------- aegisub/src/subs_edit_box.h | 2 +- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/aegisub/src/subs_edit_box.cpp b/aegisub/src/subs_edit_box.cpp index bd7512361..d436e036f 100644 --- a/aegisub/src/subs_edit_box.cpp +++ b/aegisub/src/subs_edit_box.cpp @@ -662,14 +662,14 @@ void SubsEditBox::OnCommentChange(wxCommandEvent &event) { SetSelectedRows(&AssDialogue::Comment, CommentBox->GetValue(), _("comment change"), AssFile::COMMIT_DIAG_META); } -int SubsEditBox::BlockAtPos(int pos) const { - int n=0; - wxString text = TextEdit->GetText();; - int max = text.Length()-1; - - for (int i=0;i<=pos && i<=max;i++) { - if (i > 0 && text[i] == '{') n++; - if (text[i] == '}' && i != max && i != pos && i != pos -1 && (i+1 == max || text[i+1] != '{')) n++; +int SubsEditBox::BlockAtPos(wxString const& text, int pos) const { + int n = 0; + int max = text.size() - 1; + for (int i = 0; i <= pos && i <= max; ++i) { + if (i > 0 && text[i] == '{') + n++; + if (text[i] == '}' && i != max && i != pos && i != pos -1 && (i+1 == max || text[i+1] != '{')) + n++; } return n; @@ -684,27 +684,43 @@ void SubsEditBox::SetTag(wxString tag, wxString value, bool atEnd) { int selstart, selend; get_selection(TextEdit, selstart, selend); int start = atEnd ? selend : selstart; - int blockn = BlockAtPos(start); + int blockn = BlockAtPos(line->Text, start); - AssDialogueBlock *block = line->Blocks[blockn]; - AssDialogueBlockPlain *plain = dynamic_cast(block); - AssDialogueBlockOverride *ovr = dynamic_cast(block); - - // Drawings should always be preceded by an override block (with the \pX) - if (dynamic_cast(block)) { - assert(blockn > 0); - ovr = dynamic_cast(line->Blocks[blockn - 1]); - assert(ovr); + AssDialogueBlockPlain *plain; + AssDialogueBlockOverride *ovr; + while (blockn >= 0) { + AssDialogueBlock *block = line->Blocks[blockn]; + if (dynamic_cast(block)) + --blockn; + else if (plain = dynamic_cast(block)) { + // Cursor is in a comment block, so try the previous block instead + if (plain->GetText().StartsWith("{")) { + --blockn; + start = line->Text.rfind('{', start); + } + else + break; + } + else { + ovr = dynamic_cast(block); + assert(ovr); + break; + } } + // If we didn't hit a suitable block for inserting the override just put + // it at the beginning of the line + if (blockn < 0) + start = 0; + wxString insert = tag + value; int shift = insert.size(); - if (plain) { + if (plain || blockn < 0) { line->Text = line->Text.Left(start) + "{" + insert + "}" + line->Text.Mid(start); shift += 2; line->ParseASSTags(); } - else if (ovr) { + else { wxString alt; if (tag == "\\c") alt = "\\1c"; // Remove old of same @@ -772,7 +788,7 @@ void SubsEditBox::OnFlagButton(wxCommandEvent &evt) { line->ParseASSTags(); int selstart, selend; get_selection(TextEdit, selstart, selend); - int blockn = BlockAtPos(selstart); + int blockn = BlockAtPos(line->Text, selstart); state = get_value(*line, blockn, state, tagname); @@ -790,7 +806,7 @@ void SubsEditBox::OnFontButton(wxCommandEvent &) { get_selection(TextEdit, selstart, selend); line->ParseASSTags(); - int blockn = BlockAtPos(selstart); + int blockn = BlockAtPos(line->Text, selstart); wxFont startfont; AssStyle *style = c->ass->GetStyle(line->Style); @@ -863,7 +879,7 @@ void SubsEditBox::OnColorButton(wxCommandEvent &evt) { line->ParseASSTags(); int selstart, selend; get_selection(TextEdit, selstart, selend); - int blockn = BlockAtPos(selstart); + int blockn = BlockAtPos(line->Text, selstart); color = get_value(*line, blockn, color, colorTag, alt); wxString initialText = line->Text; diff --git a/aegisub/src/subs_edit_box.h b/aegisub/src/subs_edit_box.h index aa0759f4b..8603c9ddb 100644 --- a/aegisub/src/subs_edit_box.h +++ b/aegisub/src/subs_edit_box.h @@ -122,7 +122,7 @@ class SubsEditBox : public wxPanel, protected SelectionListener { void CommitText(wxString desc); /// Get block number at text position - int BlockAtPos(int pos) const; + int BlockAtPos(wxString const& text, int pos) const; /// @brief Move to the next line, creating it if needed void NextLine();