From 2b8fc2a2d563cc307a987563b0e2a8690a00af14 Mon Sep 17 00:00:00 2001 From: Rodrigo Braz Monteiro Date: Fri, 22 Jun 2007 23:43:21 +0000 Subject: [PATCH] Moved a lot of the subs_edit_ctrl functionality into a class of its own, so it can be reused elsewhere. Originally committed to SVN as r1289. --- aegisub/Makefile.am | 1 + aegisub/scintilla_text_ctrl.cpp | 132 ++++++++++++++++++++++++++++++++ aegisub/scintilla_text_ctrl.h | 59 ++++++++++++++ aegisub/subs_edit_ctrl.cpp | 80 +------------------ aegisub/subs_edit_ctrl.h | 13 +--- 5 files changed, 196 insertions(+), 89 deletions(-) create mode 100644 aegisub/scintilla_text_ctrl.cpp create mode 100644 aegisub/scintilla_text_ctrl.h diff --git a/aegisub/Makefile.am b/aegisub/Makefile.am index d368fb5d5..8c86464fd 100644 --- a/aegisub/Makefile.am +++ b/aegisub/Makefile.am @@ -174,6 +174,7 @@ aegisub_SOURCES = \ mkv_wrap.cpp \ mythes.cxx \ options.cpp \ + scintilla_text_ctrl.cpp \ spellchecker.cpp \ standard_paths.cpp \ static_bmp.cpp \ diff --git a/aegisub/scintilla_text_ctrl.cpp b/aegisub/scintilla_text_ctrl.cpp new file mode 100644 index 000000000..836889008 --- /dev/null +++ b/aegisub/scintilla_text_ctrl.cpp @@ -0,0 +1,132 @@ +// 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 +// +// Website: http://aegisub.cellosoft.com +// Contact: mailto:zeratul@cellosoft.com +// + + +//////////// +// Includes +#include "scintilla_text_ctrl.h" +#include "utils.h" + + +/////////////// +// Constructor +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) +{ +} + + +////////////// +// Destructor +ScintillaTextCtrl::~ScintillaTextCtrl() { +} + + +/////////////////////////////////// +// Get unicode-compatible position +int ScintillaTextCtrl::GetUnicodePosition(int pos) { + wxString string = GetText().Left(pos); + wxCharBuffer buffer = string.mb_str(wxConvUTF8); + return strlen(buffer); +} + + +/////////////////////////////////////// +// Reverse unicode-compatible position +int ScintillaTextCtrl::GetReverseUnicodePosition(int pos) { + // Get UTF8 + wxCharBuffer buffer = GetText().mb_str(wxConvUTF8); + + // Limit position to it + if (pos > (signed)strlen(buffer)) pos = strlen(buffer); + + // Get UTF8 substring + char *buf2 = new char[pos+1]; + memcpy(buf2,buffer,pos); + buf2[pos] = 0; + + // Convert back and return its length + wxString buf3(buf2,wxConvUTF8); + delete[] buf2; + return buf3.Length(); +} + + +//////////////////////// +// Unicode-safe styling +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); + + // Set styling + SetStyling(len,style); +} + + +////////////////////////////////////// +// Get boundaries of word at position +void ScintillaTextCtrl::GetBoundsOfWordAtPosition(int pos,int &_start,int &_end) { + // Results + IntPairVector 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; + return; + } + } +} + + +////////////////////////////////// +// Get word at specified position +wxString ScintillaTextCtrl::GetWordAtPosition(int pos) { + int start,end; + GetBoundsOfWordAtPosition(pos,start,end); + return GetText().Mid(start,end-start+1); +} + + +//////////////////////////////// +// Set selection, unicode-aware +void ScintillaTextCtrl::SetSelectionU(int start, int end) { + SetSelection(GetUnicodePosition(start),GetUnicodePosition(end)); +} diff --git a/aegisub/scintilla_text_ctrl.h b/aegisub/scintilla_text_ctrl.h new file mode 100644 index 000000000..2b8cbfdd4 --- /dev/null +++ b/aegisub/scintilla_text_ctrl.h @@ -0,0 +1,59 @@ +// 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 +// +// Website: http://aegisub.cellosoft.com +// Contact: mailto:zeratul@cellosoft.com +// + + +#pragma once + + +//////////// +// Includes +#include +#include + + +///////// +// Class +class ScintillaTextCtrl : public wxStyledTextCtrl { +public: + wxString GetWordAtPosition(int pos); + void GetBoundsOfWordAtPosition(int pos,int &start,int &end); + void SetUnicodeStyling(int start,int length,int style); + int GetUnicodePosition(int pos); + int GetReverseUnicodePosition(int pos); + 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(); +}; diff --git a/aegisub/subs_edit_ctrl.cpp b/aegisub/subs_edit_ctrl.cpp index 8284f3b73..cfff3cd1a 100644 --- a/aegisub/subs_edit_ctrl.cpp +++ b/aegisub/subs_edit_ctrl.cpp @@ -48,7 +48,7 @@ //////////////////////// // Edit box constructor SubsTextEditCtrl::SubsTextEditCtrl(wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& wsize, long style, const wxValidator& validator, const wxString& name) -: wxStyledTextCtrl(parent, id, pos, wsize, 0, value) +: ScintillaTextCtrl(parent, id, value, pos, wsize, style, validator, name) { // Set properties SetWrapMode(wxSTC_WRAP_WORD); @@ -604,49 +604,6 @@ void SubsTextEditCtrl::UpdateCallTip() { } -/////////////////////////////////// -// Get unicode-compatible position -int SubsTextEditCtrl::GetUnicodePosition(int pos) { - wxString string = GetText().Left(pos); - wxCharBuffer buffer = string.mb_str(wxConvUTF8); - return strlen(buffer); -} - - -/////////////////////////////////////// -// Reverse unicode-compatible position -int SubsTextEditCtrl::GetReverseUnicodePosition(int pos) { - // Get UTF8 - wxCharBuffer buffer = GetText().mb_str(wxConvUTF8); - - // Limit position to it - if (pos > (signed)strlen(buffer)) pos = strlen(buffer); - - // Get UTF8 substring - char *buf2 = new char[pos+1]; - memcpy(buf2,buffer,pos); - buf2[pos] = 0; - - // Convert back and return its length - wxString buf3(buf2,wxConvUTF8); - delete[] buf2; - return buf3.Length(); -} - - -//////////////////////// -// Unicode-safe styling -void SubsTextEditCtrl::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); - - // Set styling - SetStyling(len,style); -} - - /////////////// // Spell check void SubsTextEditCtrl::StyleSpellCheck(int start, int len) { @@ -679,25 +636,6 @@ void SubsTextEditCtrl::StyleSpellCheck(int start, int len) { } -////////////////////////////////////// -// Get boundaries of word at position -void SubsTextEditCtrl::GetBoundsOfWordAtPosition(int pos,int &_start,int &_end) { - // Results - IntPairVector 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; - return; - } - } -} - - /////////////////////////// // Set text to a new value void SubsTextEditCtrl::SetTextTo(const wxString _text) { @@ -942,15 +880,6 @@ void SubsTextEditCtrl::ShowPopupMenu(int activePos) { } -////////////////////////////////// -// Get word at specified position -wxString SubsTextEditCtrl::GetWordAtPosition(int pos) { - int start,end; - GetBoundsOfWordAtPosition(pos,start,end); - return GetText().Mid(start,end-start+1); -} - - /////////////////////////////// // Split line preserving times void SubsTextEditCtrl::OnSplitLinePreserve (wxCommandEvent &event) { @@ -1099,10 +1028,3 @@ void SubsTextEditCtrl::OnSetThesLanguage(wxCommandEvent &event) { // Update styling UpdateStyle(); } - - -//////////////////////////////// -// Set selection, unicode-aware -void SubsTextEditCtrl::SetSelectionU(int start, int end) { - SetSelection(GetUnicodePosition(start),GetUnicodePosition(end)); -} diff --git a/aegisub/subs_edit_ctrl.h b/aegisub/subs_edit_ctrl.h index 5628b3580..2f5acc96f 100644 --- a/aegisub/subs_edit_ctrl.h +++ b/aegisub/subs_edit_ctrl.h @@ -40,9 +40,9 @@ //////////// // Includes #include -#include #include "spellchecker.h" #include "thesaurus.h" +#include "scintilla_text_ctrl.h" ////////////// @@ -52,7 +52,7 @@ class SubsEditBox; //////////////////// // SubsTextEditCtrl -class SubsTextEditCtrl : public wxStyledTextCtrl { +class SubsTextEditCtrl : public ScintillaTextCtrl { private: SpellChecker *spellchecker; Thesaurus *thesaurus; @@ -65,13 +65,9 @@ private: wxArrayString proto; int tipProtoN; - void OnMouseEvent(wxMouseEvent &event); - - wxString GetWordAtPosition(int pos); - void GetBoundsOfWordAtPosition(int pos,int &start,int &end); - void SetUnicodeStyling(int start,int length,int style); void ShowPopupMenu(int activePos=-1); + void OnMouseEvent(wxMouseEvent &event); void OnSplitLinePreserve(wxCommandEvent &event); void OnSplitLineEstimate(wxCommandEvent &event); void OnCut(wxCommandEvent &event); @@ -92,9 +88,6 @@ public: SubsTextEditCtrl(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); ~SubsTextEditCtrl(); - int GetUnicodePosition(int pos); - int GetReverseUnicodePosition(int pos); - void SetSelectionU(int start,int end); void SetTextTo(const wxString text); void UpdateStyle(int start=0,int length=-1); void StyleSpellCheck(int start=0,int length=-1);