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.
This commit is contained in:
Rodrigo Braz Monteiro 2007-06-22 23:43:21 +00:00
parent 3818649af7
commit 2b8fc2a2d5
5 changed files with 196 additions and 89 deletions

View file

@ -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 \

View file

@ -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<count;i++) {
if (results[i].first <= pos && results[i].second >= 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));
}

View file

@ -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 <wx/wxprec.h>
#include <wx/stc/stc.h>
/////////
// 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();
};

View file

@ -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<count;i++) {
if (results[i].first <= pos && results[i].second >= 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));
}

View file

@ -40,9 +40,9 @@
////////////
// Includes
#include <wx/wxprec.h>
#include <wx/stc/stc.h>
#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);