Kill ScintillaTextCtrl and just always use the UTF-8 methods

Trivially improves performance and reduces the opportunities for wxSTC
to fuck up.
This commit is contained in:
Thomas Goyne 2014-05-30 12:39:39 -07:00
parent e6b495dc1b
commit 060536824b
10 changed files with 27 additions and 164 deletions

View File

@ -184,7 +184,6 @@
<ClInclude Include="$(SrcDir)preferences_base.h" /> <ClInclude Include="$(SrcDir)preferences_base.h" />
<ClInclude Include="$(SrcDir)project.h" /> <ClInclude Include="$(SrcDir)project.h" />
<ClInclude Include="$(SrcDir)resolution_resampler.h" /> <ClInclude Include="$(SrcDir)resolution_resampler.h" />
<ClInclude Include="$(SrcDir)scintilla_text_ctrl.h" />
<ClInclude Include="$(SrcDir)search_replace_engine.h" /> <ClInclude Include="$(SrcDir)search_replace_engine.h" />
<ClInclude Include="$(SrcDir)selection_controller.h" /> <ClInclude Include="$(SrcDir)selection_controller.h" />
<ClInclude Include="$(SrcDir)spellchecker_hunspell.h" /> <ClInclude Include="$(SrcDir)spellchecker_hunspell.h" />
@ -376,7 +375,6 @@
<ClCompile Include="$(SrcDir)preferences_base.cpp" /> <ClCompile Include="$(SrcDir)preferences_base.cpp" />
<ClCompile Include="$(SrcDir)project.cpp" /> <ClCompile Include="$(SrcDir)project.cpp" />
<ClCompile Include="$(SrcDir)resolution_resampler.cpp" /> <ClCompile Include="$(SrcDir)resolution_resampler.cpp" />
<ClCompile Include="$(SrcDir)scintilla_text_ctrl.cpp" />
<ClCompile Include="$(SrcDir)search_replace_engine.cpp" /> <ClCompile Include="$(SrcDir)search_replace_engine.cpp" />
<ClCompile Include="$(SrcDir)selection_controller.cpp" /> <ClCompile Include="$(SrcDir)selection_controller.cpp" />
<ClCompile Include="$(SrcDir)spellchecker.cpp" /> <ClCompile Include="$(SrcDir)spellchecker.cpp" />

View File

@ -390,9 +390,6 @@
<ClInclude Include="$(SrcDir)base_grid.h"> <ClInclude Include="$(SrcDir)base_grid.h">
<Filter>Main UI\Grid</Filter> <Filter>Main UI\Grid</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="$(SrcDir)scintilla_text_ctrl.h">
<Filter>Main UI\Edit box</Filter>
</ClInclude>
<ClInclude Include="$(SrcDir)selection_controller.h"> <ClInclude Include="$(SrcDir)selection_controller.h">
<Filter>Main UI\Grid</Filter> <Filter>Main UI\Grid</Filter>
</ClInclude> </ClInclude>
@ -926,9 +923,6 @@
<ClCompile Include="$(SrcDir)selection_controller.cpp"> <ClCompile Include="$(SrcDir)selection_controller.cpp">
<Filter>Main UI\Grid</Filter> <Filter>Main UI\Grid</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="$(SrcDir)scintilla_text_ctrl.cpp">
<Filter>Main UI\Edit box</Filter>
</ClCompile>
<ClCompile Include="$(SrcDir)subs_edit_box.cpp"> <ClCompile Include="$(SrcDir)subs_edit_box.cpp">
<Filter>Main UI\Edit box</Filter> <Filter>Main UI\Edit box</Filter>
</ClCompile> </ClCompile>

View File

@ -208,7 +208,6 @@ SRC += \
preferences_base.cpp \ preferences_base.cpp \
project.cpp \ project.cpp \
resolution_resampler.cpp \ resolution_resampler.cpp \
scintilla_text_ctrl.cpp \
search_replace_engine.cpp \ search_replace_engine.cpp \
selection_controller.cpp \ selection_controller.cpp \
spellchecker.cpp \ spellchecker.cpp \

View File

@ -24,7 +24,6 @@
#include "include/aegisub/context.h" #include "include/aegisub/context.h"
#include "libresrc/libresrc.h" #include "libresrc/libresrc.h"
#include "options.h" #include "options.h"
#include "scintilla_text_ctrl.h"
#include "utils.h" #include "utils.h"
#include <libaegisub/dispatch.h> #include <libaegisub/dispatch.h>
@ -52,7 +51,7 @@ namespace {
class DialogFontsCollector final : public wxDialog { class DialogFontsCollector final : public wxDialog {
AssFile *subs; AssFile *subs;
ScintillaTextCtrl *collection_log; wxStyledTextCtrl *collection_log;
wxButton *close_btn; wxButton *close_btn;
wxButton *dest_browse_button; wxButton *dest_browse_button;
wxButton *start_btn; wxButton *start_btn;
@ -250,7 +249,7 @@ DialogFontsCollector::DialogFontsCollector(agi::Context *c)
destination_box->Add(dest_browse_sizer, wxSizerFlags().Expand()); destination_box->Add(dest_browse_sizer, wxSizerFlags().Expand());
wxStaticBoxSizer *log_box = new wxStaticBoxSizer(wxVERTICAL, this, _("Log")); wxStaticBoxSizer *log_box = new wxStaticBoxSizer(wxVERTICAL, this, _("Log"));
collection_log = new ScintillaTextCtrl(this, -1, "", wxDefaultPosition, wxSize(600, 300)); collection_log = new wxStyledTextCtrl(this, -1, wxDefaultPosition, wxSize(600, 300));
collection_log->SetWrapMode(wxSTC_WRAP_WORD); collection_log->SetWrapMode(wxSTC_WRAP_WORD);
collection_log->SetMarginWidth(1, 0); collection_log->SetMarginWidth(1, 0);
collection_log->SetReadOnly(true); collection_log->SetReadOnly(true);
@ -384,13 +383,14 @@ void DialogFontsCollector::OnRadio(wxCommandEvent &) {
void DialogFontsCollector::OnAddText(wxThreadEvent &event) { void DialogFontsCollector::OnAddText(wxThreadEvent &event) {
std::pair<int, wxString> str = event.GetPayload<std::pair<int, wxString>>(); std::pair<int, wxString> str = event.GetPayload<std::pair<int, wxString>>();
collection_log->SetReadOnly(false); collection_log->SetReadOnly(false);
int pos = collection_log->GetReverseUnicodePosition(collection_log->GetLength()); int pos = collection_log->GetLength();
collection_log->AppendText(str.second); auto const& utf8 = str.second.utf8_str();
collection_log->AppendTextRaw(utf8.data(), utf8.length());
if (str.first) { if (str.first) {
collection_log->StartUnicodeStyling(pos, 31); collection_log->StartStyling(pos, 31);
collection_log->SetUnicodeStyling(pos, str.second.size(), str.first); collection_log->SetStyling(utf8.length(), str.first);
} }
collection_log->GotoPos(pos); collection_log->GotoPos(pos + utf8.length());
collection_log->SetReadOnly(true); collection_log->SetReadOnly(true);
} }

View File

@ -41,12 +41,14 @@
#include <boost/algorithm/string/classification.hpp> #include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/predicate.hpp> #include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <wx/checkbox.h> #include <wx/checkbox.h>
#include <wx/msgdlg.h> #include <wx/msgdlg.h>
#include <wx/sizer.h> #include <wx/sizer.h>
#include <wx/settings.h> #include <wx/settings.h>
#include <wx/stattext.h> #include <wx/stattext.h>
#include <wx/stc/stc.h>
static void add_hotkey(wxSizer *sizer, wxWindow *parent, const char *command, wxString const& text) { static void add_hotkey(wxSizer *sizer, wxWindow *parent, const char *command, wxString const& text) {
sizer->Add(new wxStaticText(parent, -1, text)); sizer->Add(new wxStaticText(parent, -1, text));
@ -77,7 +79,7 @@ DialogTranslation::DialogTranslation(agi::Context *c)
line_number_display = new wxStaticText(this, -1, ""); line_number_display = new wxStaticText(this, -1, "");
original_box->Add(line_number_display, 0, wxBOTTOM, 5); original_box->Add(line_number_display, 0, wxBOTTOM, 5);
original_text = new ScintillaTextCtrl(this, -1, "", wxDefaultPosition, wxSize(320, 80)); original_text = new wxStyledTextCtrl(this, -1, wxDefaultPosition, wxSize(320, 80));
original_text->SetWrapMode(wxSTC_WRAP_WORD); original_text->SetWrapMode(wxSTC_WRAP_WORD);
original_text->SetMarginWidth(1, 0); original_text->SetMarginWidth(1, 0);
original_text->StyleSetForeground(1, wxColour(10, 60, 200)); original_text->StyleSetForeground(1, wxColour(10, 60, 200));
@ -163,8 +165,7 @@ DialogTranslation::DialogTranslation(agi::Context *c)
UpdateDisplay(); UpdateDisplay();
} }
DialogTranslation::~DialogTranslation() { DialogTranslation::~DialogTranslation() { }
}
void DialogTranslation::OnActiveLineChanged(AssDialogue *new_line) { void DialogTranslation::OnActiveLineChanged(AssDialogue *new_line) {
if (switching_lines) return; if (switching_lines) return;
@ -243,9 +244,8 @@ void DialogTranslation::UpdateDisplay() {
int initial_pos = original_text->GetLength(); int initial_pos = original_text->GetLength();
original_text->AppendTextRaw(block->GetText().c_str()); original_text->AppendTextRaw(block->GetText().c_str());
if (i == cur_block) { if (i == cur_block) {
int cur_size = original_text->GetReverseUnicodePosition(initial_pos); original_text->StartStyling(initial_pos, 31);
original_text->StartUnicodeStyling(cur_size); original_text->SetStyling(block->GetText().size(), 1);
original_text->SetUnicodeStyling(cur_size, block->GetText().size(), 1);
} }
} }
else else
@ -262,11 +262,11 @@ void DialogTranslation::UpdateDisplay() {
} }
void DialogTranslation::Commit(bool next) { void DialogTranslation::Commit(bool next) {
wxString new_value = translated_text->GetValue(); std::string new_value = translated_text->GetTextRaw().data();
new_value.Replace("\r\n", "\\N"); boost::replace_all(new_value, "\r\n", "\\N");
new_value.Replace("\r", "\\N"); boost::replace_all(new_value, "\r", "\\N");
new_value.Replace("\n", "\\N"); boost::replace_all(new_value, "\n", "\\N");
*blocks[cur_block] = AssDialogueBlockPlain(from_wx(new_value)); *blocks[cur_block] = AssDialogueBlockPlain(new_value);
active_line->UpdateText(blocks); active_line->UpdateText(blocks);
file_change_connection.Block(); file_change_connection.Block();
@ -285,7 +285,8 @@ void DialogTranslation::Commit(bool next) {
} }
void DialogTranslation::InsertOriginal() { void DialogTranslation::InsertOriginal() {
translated_text->AddText(to_wx(blocks[cur_block]->GetText())); auto const& text = blocks[cur_block]->GetText();
translated_text->AddTextRaw(text.data(), text.size());
} }
void DialogTranslation::OnKeyDown(wxKeyEvent &evt) { void DialogTranslation::OnKeyDown(wxKeyEvent &evt) {

View File

@ -14,10 +14,6 @@
// //
// Aegisub Project http://www.aegisub.org/ // Aegisub Project http://www.aegisub.org/
/// @file dialog_translation.h
/// @see dialog_translation.cpp
/// @ingroup tools_ui
#include <libaegisub/exception.h> #include <libaegisub/exception.h>
#include <libaegisub/signal.h> #include <libaegisub/signal.h>
@ -29,10 +25,10 @@ namespace agi { struct Context; }
class AssDialogue; class AssDialogue;
class AssDialogueBlock; class AssDialogueBlock;
class PersistLocation; class PersistLocation;
class ScintillaTextCtrl;
class SubsTextEditCtrl; class SubsTextEditCtrl;
class wxStaticText;
class wxCheckBox; class wxCheckBox;
class wxStaticText;
class wxStyledTextCtrl;
/// Assistant for translating subtitles in one language to another language /// Assistant for translating subtitles in one language to another language
class DialogTranslation final : public wxDialog { class DialogTranslation final : public wxDialog {
@ -55,7 +51,7 @@ class DialogTranslation final : public wxDialog {
bool switching_lines = false; bool switching_lines = false;
wxStaticText *line_number_display; wxStaticText *line_number_display;
ScintillaTextCtrl *original_text; wxStyledTextCtrl *original_text;
SubsTextEditCtrl *translated_text; SubsTextEditCtrl *translated_text;
wxCheckBox *seek_video; wxCheckBox *seek_video;

View File

@ -1,75 +0,0 @@
// Copyright (c) 2007, Rodrigo Braz Monteiro
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of the Aegisub Group nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Aegisub Project http://www.aegisub.org/
/// @file scintilla_text_ctrl.cpp
/// @brief Customised version of wxStyledTextControl used for main edit box
/// @ingroup custom_control
///
#include "scintilla_text_ctrl.h"
#include "utils.h"
ScintillaTextCtrl::ScintillaTextCtrl(wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, long style)
: wxStyledTextCtrl(parent, id, pos, size, style, value)
{
Bind(wxEVT_MOUSEWHEEL, &ScintillaTextCtrl::OnMouseWheel, this);
}
/// @brief Get unicode-compatible position
int ScintillaTextCtrl::GetUnicodePosition(int pos) {
return GetText().Left(pos).utf8_str().length();
}
/// @brief Reverse unicode-compatible position
int ScintillaTextCtrl::GetReverseUnicodePosition(int pos) {
wxCharBuffer buffer = GetTextRaw();
return wxString::FromUTF8(buffer.data(), std::min<int>(pos, buffer.length())).length();
}
/// @brief Start unicode-safe styling
void ScintillaTextCtrl::StartUnicodeStyling(int start,int mask) {
StartStyling(GetUnicodePosition(start),mask);
// Cache the text for styling as GetText is hideously slow
text = GetText();
}
/// @brief Unicode-safe styling
void ScintillaTextCtrl::SetUnicodeStyling(int start,int length,int style) {
// Get the real length
int len = text.Mid(start, length).utf8_str().length();
SetStyling(len,style);
}
void ScintillaTextCtrl::OnMouseWheel(wxMouseEvent& evt) {
if (ForwardMouseWheelEvent(this, evt)) {
// Skip the event so that wxSTC's default mouse wheel handler is hit
evt.Skip();
}
}

View File

@ -1,44 +0,0 @@
// Copyright (c) 2007, Rodrigo Braz Monteiro
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of the Aegisub Group nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Aegisub Project http://www.aegisub.org/
#include <wx/stc/stc.h>
class ScintillaTextCtrl : public wxStyledTextCtrl {
wxString text;
void OnMouseWheel(wxMouseEvent& evt);
public:
int GetUnicodePosition(int pos);
int GetReverseUnicodePosition(int pos);
void StartUnicodeStyling(int start,int mask=31);
void SetUnicodeStyling(int start,int length,int style);
ScintillaTextCtrl(wxWindow* parent, wxWindowID id, const wxString& value = wxString(), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = 0);
};

View File

@ -27,11 +27,6 @@
// //
// Aegisub Project http://www.aegisub.org/ // Aegisub Project http://www.aegisub.org/
/// @file subs_edit_ctrl.cpp
/// @brief Main subtitle editing text control
/// @ingroup main_ui
///
#include "subs_edit_ctrl.h" #include "subs_edit_ctrl.h"
#include "ass_dialogue.h" #include "ass_dialogue.h"
@ -82,7 +77,7 @@ enum {
}; };
SubsTextEditCtrl::SubsTextEditCtrl(wxWindow* parent, wxSize wsize, long style, agi::Context *context) SubsTextEditCtrl::SubsTextEditCtrl(wxWindow* parent, wxSize wsize, long style, agi::Context *context)
: ScintillaTextCtrl(parent, -1, "", wxDefaultPosition, wsize, style) : wxStyledTextCtrl(parent, -1, wxDefaultPosition, wsize, style)
, spellchecker(SpellCheckerFactory::GetSpellChecker()) , spellchecker(SpellCheckerFactory::GetSpellChecker())
, thesaurus(agi::make_unique<Thesaurus>()) , thesaurus(agi::make_unique<Thesaurus>())
, context(context) , context(context)

View File

@ -27,11 +27,10 @@
// //
// Aegisub Project http://www.aegisub.org/ // Aegisub Project http://www.aegisub.org/
#include "scintilla_text_ctrl.h"
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include <wx/stc/stc.h>
class Thesaurus; class Thesaurus;
namespace agi { namespace agi {
@ -43,7 +42,7 @@ namespace agi {
/// @class SubsTextEditCtrl /// @class SubsTextEditCtrl
/// @brief A Scintilla control with spell checking and syntax highlighting /// @brief A Scintilla control with spell checking and syntax highlighting
class SubsTextEditCtrl final : public ScintillaTextCtrl { class SubsTextEditCtrl final : public wxStyledTextCtrl {
/// Backend spellchecker to use /// Backend spellchecker to use
std::unique_ptr<agi::SpellChecker> spellchecker; std::unique_ptr<agi::SpellChecker> spellchecker;