Remove some cruft from ScintillaTextCtrl and make SetUnicodeStyling significantly faster

Originally committed to SVN as r5570.
This commit is contained in:
Thomas Goyne 2011-08-27 06:52:35 +00:00
parent ccf9f64818
commit b8a1674c27
3 changed files with 18 additions and 83 deletions

View file

@ -34,62 +34,27 @@
/// @ingroup custom_control /// @ingroup custom_control
/// ///
////////////
// Includes
#include "config.h" #include "config.h"
#include "scintilla_text_ctrl.h" #include "scintilla_text_ctrl.h"
#include "utils.h" #include "utils.h"
ScintillaTextCtrl::ScintillaTextCtrl(wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size)
/// @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)
: wxStyledTextCtrl(parent, id, pos, size, 0, value) : wxStyledTextCtrl(parent, id, pos, size, 0, value)
{ {
//SetWindowStyle(style);
} }
/// @brief Destructor
///
ScintillaTextCtrl::~ScintillaTextCtrl() {
}
/// @brief Get unicode-compatible position /// @brief Get unicode-compatible position
/// @param pos
/// @return
///
int ScintillaTextCtrl::GetUnicodePosition(int pos) { int ScintillaTextCtrl::GetUnicodePosition(int pos) {
wxString string = GetText().Left(pos); return GetText().Left(pos).utf8_str().length();
wxCharBuffer buffer = string.mb_str(wxConvUTF8);
return strlen(buffer);
} }
/// @brief Reverse unicode-compatible position /// @brief Reverse unicode-compatible position
/// @param pos
/// @return
///
int ScintillaTextCtrl::GetReverseUnicodePosition(int pos) { int ScintillaTextCtrl::GetReverseUnicodePosition(int pos) {
// Get UTF8 wxCharBuffer buffer = GetText().utf8_str();
wxCharBuffer buffer = GetText().mb_str(wxConvUTF8);
// Limit position to it // Limit position to it
if (pos > (signed)strlen(buffer)) pos = strlen(buffer); if (pos > (signed)buffer.length()) pos = buffer.length();
// Get UTF8 substring // Get UTF8 substring
char *buf2 = new char[pos+1]; char *buf2 = new char[pos+1];
@ -102,77 +67,47 @@ int ScintillaTextCtrl::GetReverseUnicodePosition(int pos) {
return buf3.Length(); return buf3.Length();
} }
/// @brief Start unicode-safe styling /// @brief Start unicode-safe styling
/// @param start
/// @param mask
///
void ScintillaTextCtrl::StartUnicodeStyling(int start,int mask) { void ScintillaTextCtrl::StartUnicodeStyling(int start,int mask) {
StartStyling(GetUnicodePosition(start),mask); StartStyling(GetUnicodePosition(start),mask);
// Cache the text for styling as GetText is hideously slow
text = GetText();
} }
/// @brief Unicode-safe styling /// @brief Unicode-safe styling
/// @param start
/// @param length
/// @param style
///
void ScintillaTextCtrl::SetUnicodeStyling(int start,int length,int style) { void ScintillaTextCtrl::SetUnicodeStyling(int start,int length,int style) {
// Get the real length // Get the real length
wxString string = GetText().Mid(start,length); int len = text.Mid(start, length).utf8_str().length();
wxCharBuffer buffer = string.mb_str(wxConvUTF8);
int len = strlen(buffer);
// Set styling // Set styling
SetStyling(len,style); SetStyling(len,style);
} }
/// @brief Get boundaries of word at position /// @brief Get boundaries of word at position
/// @param pos void ScintillaTextCtrl::GetBoundsOfWordAtPosition(int pos,int &start,int &end) {
/// @param _start
/// @param _end
/// @return
///
void ScintillaTextCtrl::GetBoundsOfWordAtPosition(int pos,int &_start,int &_end) {
// Results // Results
IntPairVector results; IntPairVector results;
GetWordBoundaries(GetText(),results); GetWordBoundaries(GetText(), results);
// Get boundaries // Get boundaries
int count = results.size(); int count = results.size();
for (int i=0;i<count;i++) { for (int i = 0; i < count; i++) {
if (results[i].first <= pos && results[i].second >= pos) { if (results[i].first <= pos && results[i].second >= pos) {
_start = results[i].first; start = results[i].first;
_end = results[i].second-1; end = results[i].second - 1;
return; return;
} }
} }
} }
/// @brief Get word at specified position /// @brief Get word at specified position
/// @param pos
/// @return
///
wxString ScintillaTextCtrl::GetWordAtPosition(int pos) { wxString ScintillaTextCtrl::GetWordAtPosition(int pos) {
int start,end; int start,end;
GetBoundsOfWordAtPosition(pos,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 /// @brief Set selection, unicode-aware
/// @param start
/// @param end
///
void ScintillaTextCtrl::SetSelectionU(int start, int end) { void ScintillaTextCtrl::SetSelectionU(int start, int end) {
SetSelection(GetUnicodePosition(start),GetUnicodePosition(end)); SetSelection(GetUnicodePosition(start),GetUnicodePosition(end));
} }

View file

@ -44,6 +44,7 @@
/// ///
/// DOCME /// DOCME
class ScintillaTextCtrl : public wxStyledTextCtrl { class ScintillaTextCtrl : public wxStyledTextCtrl {
wxString text;
public: public:
wxString GetWordAtPosition(int pos); wxString GetWordAtPosition(int pos);
void GetBoundsOfWordAtPosition(int pos,int &start,int &end); void GetBoundsOfWordAtPosition(int pos,int &start,int &end);
@ -54,6 +55,5 @@ public:
void SetUnicodeStyling(int start,int length,int style); void SetUnicodeStyling(int start,int length,int style);
void SetSelectionU(int start,int end); 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); ScintillaTextCtrl(wxWindow* parent, wxWindowID id, const wxString& value = "", const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize);
virtual ~ScintillaTextCtrl();
}; };

View file

@ -88,7 +88,7 @@ enum {
/// @return /// @return
/// ///
SubsTextEditCtrl::SubsTextEditCtrl(wxWindow* parent, wxSize wsize, long style, SubtitlesGrid *grid) SubsTextEditCtrl::SubsTextEditCtrl(wxWindow* parent, wxSize wsize, long style, SubtitlesGrid *grid)
: ScintillaTextCtrl(parent, wxID_ANY, "", wxDefaultPosition, wsize, style) : ScintillaTextCtrl(parent, wxID_ANY)
, spellchecker(SpellCheckerFactory::GetSpellChecker()) , spellchecker(SpellCheckerFactory::GetSpellChecker())
, thesaurus(Thesaurus::GetThesaurus()) , thesaurus(Thesaurus::GetThesaurus())
, grid(grid) , grid(grid)
@ -279,8 +279,9 @@ void SubsTextEditCtrl::SetStyles() {
} }
void SubsTextEditCtrl::UpdateStyle(int start, int _length) { void SubsTextEditCtrl::UpdateStyle(int start, int _length) {
StartUnicodeStyling(0,255);
if (!OPT_GET("Subtitle/Highlight/Syntax")->GetBool()) { if (!OPT_GET("Subtitle/Highlight/Syntax")->GetBool()) {
StartStyling(0,255);
SetUnicodeStyling(start, _length, 0); SetUnicodeStyling(start, _length, 0);
return; return;
} }
@ -311,7 +312,6 @@ void SubsTextEditCtrl::UpdateStyle(int start, int _length) {
bool drawingMode = false; // for \p1 -> \p0 stuff bool drawingMode = false; // for \p1 -> \p0 stuff
// Begin styling // Begin styling
StartStyling(0,255);
int ran = 0; // length of current range int ran = 0; // length of current range
int depth = 0; // brace nesting depth int depth = 0; // brace nesting depth
int curStyle = 0; // style to apply to current range int curStyle = 0; // style to apply to current range