From a94892485011b2e390bfd740d13885a207da4cec Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Mon, 8 Oct 2012 07:32:51 -0700 Subject: [PATCH] Add a TextSelectionController interface to avoid exposing the wxSTC edit directly in the context --- .../aegisub_vs2008/aegisub_vs2008.vcproj | 12 +++++ aegisub/src/Makefile | 1 + aegisub/src/dialog_search_replace.cpp | 5 +- aegisub/src/dialog_spellchecker.cpp | 9 ++-- aegisub/src/frame_main.cpp | 3 -- aegisub/src/include/aegisub/context.h | 3 +- .../scintilla_text_selection_controller.cpp | 46 +++++++++++++++++++ .../src/scintilla_text_selection_controller.h | 33 +++++++++++++ aegisub/src/subs_edit_box.cpp | 5 ++ aegisub/src/subs_edit_box.h | 8 +++- aegisub/src/text_selection_controller.h | 29 ++++++++++++ 11 files changed, 142 insertions(+), 12 deletions(-) create mode 100644 aegisub/src/scintilla_text_selection_controller.cpp create mode 100644 aegisub/src/scintilla_text_selection_controller.h create mode 100644 aegisub/src/text_selection_controller.h diff --git a/aegisub/build/aegisub_vs2008/aegisub_vs2008.vcproj b/aegisub/build/aegisub_vs2008/aegisub_vs2008.vcproj index ddee7a176..69371fed2 100644 --- a/aegisub/build/aegisub_vs2008/aegisub_vs2008.vcproj +++ b/aegisub/build/aegisub_vs2008/aegisub_vs2008.vcproj @@ -1831,10 +1831,22 @@ RelativePath="..\..\src\audio_timing.h" > + + + + + + subsGrid->SelectRow(curLine,false); context->subsGrid->MakeCellVisible(curLine,0); if (field == 0) { - context->subsGrid->SetActiveLine(context->subsGrid->GetDialogue(curLine)); - context->editBox->SetSelectionU(pos,pos+replaceLen); + context->selectionController->SetActiveLine(context->subsGrid->GetDialogue(curLine)); + context->textSelectionController->SetSelection(pos, pos + replaceLen); } // Update video diff --git a/aegisub/src/dialog_spellchecker.cpp b/aegisub/src/dialog_spellchecker.cpp index 3e03d4226..bcd00fa6c 100644 --- a/aegisub/src/dialog_spellchecker.cpp +++ b/aegisub/src/dialog_spellchecker.cpp @@ -45,6 +45,7 @@ #include "libresrc/libresrc.h" #include "main.h" #include "selection_controller.h" +#include "text_selection_controller.h" #include "subs_edit_ctrl.h" #include "utils.h" @@ -208,7 +209,7 @@ bool DialogSpellChecker::FindNext() { start_line = active_line; } - int start_pos = context->editBox->GetReverseUnicodePosition(context->editBox->GetCurrentPos()); + int start_pos = context->textSelectionController->GetInsertionPoint(); int commit_id = -1; if (CheckLine(active_line, start_pos, &commit_id)) @@ -288,7 +289,7 @@ void DialogSpellChecker::Replace() { if (active_line->Text.Mid(word_start, word_end - word_start) == orig_word->GetValue()) { active_line->Text = active_line->Text.Left(word_start) + replace_word->GetValue() + active_line->Text.Mid(word_end); context->ass->Commit(_("spell check replace"), AssFile::COMMIT_DIAG_TEXT); - context->editBox->SetCurrentPos(context->editBox->GetUnicodePosition(word_start + replace_word->GetValue().size())); + context->textSelectionController->SetInsertionPoint(word_start + replace_word->GetValue().size()); } } @@ -300,8 +301,8 @@ void DialogSpellChecker::SetWord(wxString const& word) { suggest_list->Clear(); suggest_list->Append(suggestions); - context->editBox->SetSelectionU(word_start, word_end); - context->editBox->SetCurrentPos(context->editBox->GetUnicodePosition(word_end)); + context->textSelectionController->SetSelection(word_start, word_end); + context->textSelectionController->SetInsertionPoint(word_end); add_button->Enable(spellchecker->CanAddWord(word)); } diff --git a/aegisub/src/frame_main.cpp b/aegisub/src/frame_main.cpp index 9e585c754..8c5569f5d 100644 --- a/aegisub/src/frame_main.cpp +++ b/aegisub/src/frame_main.cpp @@ -389,7 +389,6 @@ void FrameMain::InitContents() { StartupLog("Create subtitle editing box"); SubsEditBox *EditBox = new SubsEditBox(Panel, context.get()); - context->editBox = EditBox->TextEdit; StartupLog("Arrange main sizers"); ToolsSizer = new wxBoxSizer(wxVERTICAL); @@ -406,8 +405,6 @@ void FrameMain::InitContents() { StartupLog("Perform layout"); Layout(); - StartupLog("Set focus to edting box"); - EditBox->TextEdit->SetFocus(); StartupLog("Leaving InitContents"); } diff --git a/aegisub/src/include/aegisub/context.h b/aegisub/src/include/aegisub/context.h index 3288192e4..e38957a8e 100644 --- a/aegisub/src/include/aegisub/context.h +++ b/aegisub/src/include/aegisub/context.h @@ -7,6 +7,7 @@ class DialogManager; template class SelectionController; class SubsTextEditCtrl; class SubtitlesGrid; +class TextSelectionController; class VideoContext; class VideoDisplay; class wxWindow; @@ -22,6 +23,7 @@ struct Context { // Controllers AudioController *audioController; SelectionController *selectionController; + TextSelectionController *textSelectionController; VideoContext *videoController; // Things that should probably be in some sort of UI-context-model @@ -33,7 +35,6 @@ struct Context { AudioBox *audioBox; AudioKaraoke *karaoke; DialogManager *dialog; - SubsTextEditCtrl *editBox; SubtitlesGrid *subsGrid; VideoDisplay *videoDisplay; }; diff --git a/aegisub/src/scintilla_text_selection_controller.cpp b/aegisub/src/scintilla_text_selection_controller.cpp new file mode 100644 index 000000000..b5c679503 --- /dev/null +++ b/aegisub/src/scintilla_text_selection_controller.cpp @@ -0,0 +1,46 @@ +// Copyright (c) 2012, Thomas Goyne +// +// Permission to use, copy, modify, and distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +// +// Aegisub Project http://www.aegisub.org/ + +#include "config.h" + +#include "scintilla_text_selection_controller.h" + +#include "scintilla_text_ctrl.h" + +ScintillaTextSelectionController::ScintillaTextSelectionController(ScintillaTextCtrl *ctrl) +: ctrl(ctrl) +{ +} + +void ScintillaTextSelectionController::SetInsertionPoint(int position) { + ctrl->SetInsertionPoint(ctrl->GetUnicodePosition(position)); +} + +int ScintillaTextSelectionController::GetInsertionPoint() const { + return ctrl->GetReverseUnicodePosition(ctrl->GetInsertionPoint()); +} + +void ScintillaTextSelectionController::SetSelection(int start, int end) { + ctrl->SetSelectionU(start, end); +} + +int ScintillaTextSelectionController::GetSelectionStart() const { + return ctrl->GetReverseUnicodePosition(ctrl->GetSelectionStart()); +} + +int ScintillaTextSelectionController::GetSelectionEnd() const { + return ctrl->GetReverseUnicodePosition(ctrl->GetSelectionEnd()); +} diff --git a/aegisub/src/scintilla_text_selection_controller.h b/aegisub/src/scintilla_text_selection_controller.h new file mode 100644 index 000000000..af08a0e46 --- /dev/null +++ b/aegisub/src/scintilla_text_selection_controller.h @@ -0,0 +1,33 @@ +// Copyright (c) 2012, Thomas Goyne +// +// Permission to use, copy, modify, and distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +// +// Aegisub Project http://www.aegisub.org/ + +#include "text_selection_controller.h" + +class ScintillaTextCtrl; + +class ScintillaTextSelectionController : public TextSelectionController { + ScintillaTextCtrl *ctrl; + +public: + void SetSelection(int start, int end); + void SetInsertionPoint(int point); + + int GetSelectionStart() const; + int GetSelectionEnd() const; + int GetInsertionPoint() const; + + ScintillaTextSelectionController(ScintillaTextCtrl *ctrl); +}; diff --git a/aegisub/src/subs_edit_box.cpp b/aegisub/src/subs_edit_box.cpp index a566a583e..122568f3e 100644 --- a/aegisub/src/subs_edit_box.cpp +++ b/aegisub/src/subs_edit_box.cpp @@ -64,6 +64,7 @@ #include "libresrc/libresrc.h" #include "main.h" #include "placeholder_ctrl.h" +#include "scintilla_text_selection_controller.h" #include "subs_edit_ctrl.h" #include "subs_grid.h" #include "timeedit_ctrl.h" @@ -250,6 +251,10 @@ SubsEditBox::SubsEditBox(wxWindow *parent, agi::Context *context) connections.push_back(context->videoController->AddTimecodesListener(&SubsEditBox::UpdateFrameTiming, this)); connections.push_back(context->selectionController->AddActiveLineListener(&SubsEditBox::OnActiveLineChanged, this)); connections.push_back(context->selectionController->AddSelectionListener(&SubsEditBox::OnSelectedSetChanged, this)); + + textSelectionController.reset(new ScintillaTextSelectionController(TextEdit)); + context->textSelectionController = textSelectionController.get(); + TextEdit->SetFocus(); } SubsEditBox::~SubsEditBox() { diff --git a/aegisub/src/subs_edit_box.h b/aegisub/src/subs_edit_box.h index c5b540018..3743ceae9 100644 --- a/aegisub/src/subs_edit_box.h +++ b/aegisub/src/subs_edit_box.h @@ -43,6 +43,7 @@ #include #endif +#include #include #include "selection_controller.h" @@ -54,6 +55,7 @@ class AssDialogue; class AssStyle; class AssTime; class SubsTextEditCtrl; +class TextSelectionController; class TimeEdit; class wxButton; class wxCheckBox; @@ -216,9 +218,11 @@ class SubsEditBox : public wxPanel { /// @brief Enable or disable frame timing mode void UpdateFrameTiming(agi::vfr::Framerate const& fps); -public: - SubsTextEditCtrl *TextEdit; + SubsTextEditCtrl *TextEdit; + agi::scoped_ptr textSelectionController; + +public: /// @brief Constructor /// @param parent Parent window SubsEditBox(wxWindow *parent, agi::Context *context); diff --git a/aegisub/src/text_selection_controller.h b/aegisub/src/text_selection_controller.h new file mode 100644 index 000000000..cc24c61c7 --- /dev/null +++ b/aegisub/src/text_selection_controller.h @@ -0,0 +1,29 @@ +// Copyright (c) 2012, Thomas Goyne +// +// Permission to use, copy, modify, and distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +// +// Aegisub Project http://www.aegisub.org/ + +#pragma once + +class TextSelectionController { +public: + virtual ~TextSelectionController() { } + + virtual void SetSelection(int start, int end) = 0; + virtual void SetInsertionPoint(int point) = 0; + + virtual int GetSelectionStart() const = 0; + virtual int GetSelectionEnd() const = 0; + virtual int GetInsertionPoint() const = 0; +};