From b8a1674c27dc607db7125764971ee99c255987b0 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Sat, 27 Aug 2011 06:52:35 +0000 Subject: [PATCH] Remove some cruft from ScintillaTextCtrl and make SetUnicodeStyling significantly faster Originally committed to SVN as r5570. --- aegisub/src/scintilla_text_ctrl.cpp | 91 +++++------------------------ aegisub/src/scintilla_text_ctrl.h | 4 +- aegisub/src/subs_edit_ctrl.cpp | 6 +- 3 files changed, 18 insertions(+), 83 deletions(-) diff --git a/aegisub/src/scintilla_text_ctrl.cpp b/aegisub/src/scintilla_text_ctrl.cpp index 3824da2dc..bd04f926f 100644 --- a/aegisub/src/scintilla_text_ctrl.cpp +++ b/aegisub/src/scintilla_text_ctrl.cpp @@ -34,62 +34,27 @@ /// @ingroup custom_control /// - -//////////// -// Includes #include "config.h" #include "scintilla_text_ctrl.h" #include "utils.h" - -/// @brief Constructor -/// @param parent -/// @param id -/// @param value -/// @param pos -/// @param size -/// @param style -/// @param validator -/// @param name -/// -ScintillaTextCtrl::ScintillaTextCtrl(wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator, const wxString& name) +ScintillaTextCtrl::ScintillaTextCtrl(wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size) : wxStyledTextCtrl(parent, id, pos, size, 0, value) { - //SetWindowStyle(style); } - - -/// @brief Destructor -/// -ScintillaTextCtrl::~ScintillaTextCtrl() { -} - - - /// @brief Get unicode-compatible position -/// @param pos -/// @return -/// int ScintillaTextCtrl::GetUnicodePosition(int pos) { - wxString string = GetText().Left(pos); - wxCharBuffer buffer = string.mb_str(wxConvUTF8); - return strlen(buffer); + return GetText().Left(pos).utf8_str().length(); } - - /// @brief Reverse unicode-compatible position -/// @param pos -/// @return -/// int ScintillaTextCtrl::GetReverseUnicodePosition(int pos) { - // Get UTF8 - wxCharBuffer buffer = GetText().mb_str(wxConvUTF8); + wxCharBuffer buffer = GetText().utf8_str(); // Limit position to it - if (pos > (signed)strlen(buffer)) pos = strlen(buffer); + if (pos > (signed)buffer.length()) pos = buffer.length(); // Get UTF8 substring char *buf2 = new char[pos+1]; @@ -102,77 +67,47 @@ int ScintillaTextCtrl::GetReverseUnicodePosition(int pos) { return buf3.Length(); } - - /// @brief Start unicode-safe styling -/// @param start -/// @param mask -/// 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 -/// @param start -/// @param length -/// @param style -/// void ScintillaTextCtrl::SetUnicodeStyling(int start,int length,int style) { // Get the real length - wxString string = GetText().Mid(start,length); - wxCharBuffer buffer = string.mb_str(wxConvUTF8); - int len = strlen(buffer); + int len = text.Mid(start, length).utf8_str().length(); // Set styling SetStyling(len,style); } - - /// @brief Get boundaries of word at position -/// @param pos -/// @param _start -/// @param _end -/// @return -/// -void ScintillaTextCtrl::GetBoundsOfWordAtPosition(int pos,int &_start,int &_end) { +void ScintillaTextCtrl::GetBoundsOfWordAtPosition(int pos,int &start,int &end) { // Results IntPairVector results; - GetWordBoundaries(GetText(),results); + GetWordBoundaries(GetText(), results); // Get boundaries int count = results.size(); - for (int i=0;i= pos) { - _start = results[i].first; - _end = results[i].second-1; + start = results[i].first; + end = results[i].second - 1; return; } } } - - /// @brief Get word at specified position -/// @param pos -/// @return -/// wxString ScintillaTextCtrl::GetWordAtPosition(int pos) { int start,end; GetBoundsOfWordAtPosition(pos,start,end); - return GetText().Mid(start,end-start+1); + return GetText().Mid(start, end - start + 1); } - - /// @brief Set selection, unicode-aware -/// @param start -/// @param end -/// void ScintillaTextCtrl::SetSelectionU(int start, int end) { SetSelection(GetUnicodePosition(start),GetUnicodePosition(end)); } - - diff --git a/aegisub/src/scintilla_text_ctrl.h b/aegisub/src/scintilla_text_ctrl.h index dabc5fbb9..b37c4f87b 100644 --- a/aegisub/src/scintilla_text_ctrl.h +++ b/aegisub/src/scintilla_text_ctrl.h @@ -44,6 +44,7 @@ /// /// DOCME class ScintillaTextCtrl : public wxStyledTextCtrl { + wxString text; public: wxString GetWordAtPosition(int pos); void GetBoundsOfWordAtPosition(int pos,int &start,int &end); @@ -54,6 +55,5 @@ public: void SetUnicodeStyling(int start,int length,int style); void SetSelectionU(int start,int end); - ScintillaTextCtrl(wxWindow* parent, wxWindowID id, const wxString& value = _T(""), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = 0, const wxValidator& validator = wxDefaultValidator, const wxString& name = wxTextCtrlNameStr); - virtual ~ScintillaTextCtrl(); + ScintillaTextCtrl(wxWindow* parent, wxWindowID id, const wxString& value = "", const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize); }; diff --git a/aegisub/src/subs_edit_ctrl.cpp b/aegisub/src/subs_edit_ctrl.cpp index 23da32acd..3a6684dd7 100644 --- a/aegisub/src/subs_edit_ctrl.cpp +++ b/aegisub/src/subs_edit_ctrl.cpp @@ -88,7 +88,7 @@ enum { /// @return /// SubsTextEditCtrl::SubsTextEditCtrl(wxWindow* parent, wxSize wsize, long style, SubtitlesGrid *grid) -: ScintillaTextCtrl(parent, wxID_ANY, "", wxDefaultPosition, wsize, style) +: ScintillaTextCtrl(parent, wxID_ANY) , spellchecker(SpellCheckerFactory::GetSpellChecker()) , thesaurus(Thesaurus::GetThesaurus()) , grid(grid) @@ -279,8 +279,9 @@ void SubsTextEditCtrl::SetStyles() { } void SubsTextEditCtrl::UpdateStyle(int start, int _length) { + StartUnicodeStyling(0,255); + if (!OPT_GET("Subtitle/Highlight/Syntax")->GetBool()) { - StartStyling(0,255); SetUnicodeStyling(start, _length, 0); return; } @@ -311,7 +312,6 @@ void SubsTextEditCtrl::UpdateStyle(int start, int _length) { bool drawingMode = false; // for \p1 -> \p0 stuff // Begin styling - StartStyling(0,255); int ran = 0; // length of current range int depth = 0; // brace nesting depth int curStyle = 0; // style to apply to current range