Composition and decomposition text controls are multiline now + they synchronize text selection.

This commit is contained in:
Simon Rozman 2016-02-09 11:11:26 +01:00
parent d25b215a69
commit 03b855fbbb
12 changed files with 210 additions and 28 deletions

View File

@ -196,7 +196,7 @@
<property name="resize">Resizable</property>
<property name="show">1</property>
<property name="size"></property>
<property name="style">wxTE_CENTRE</property>
<property name="style">wxTE_CENTRE|wxTE_MULTILINE</property>
<property name="subclass"></property>
<property name="toolbar_pane">0</property>
<property name="tooltip"></property>
@ -224,7 +224,7 @@
<event name="OnMotion"></event>
<event name="OnMouseEvents"></event>
<event name="OnMouseWheel"></event>
<event name="OnPaint"></event>
<event name="OnPaint">OnDecomposedPaint</event>
<event name="OnRightDClick"></event>
<event name="OnRightDown"></event>
<event name="OnRightUp"></event>
@ -287,7 +287,7 @@
<property name="resize">Resizable</property>
<property name="show">1</property>
<property name="size"></property>
<property name="style">wxTE_CENTRE</property>
<property name="style">wxTE_CENTRE|wxTE_MULTILINE</property>
<property name="subclass"></property>
<property name="toolbar_pane">0</property>
<property name="tooltip"></property>
@ -315,7 +315,7 @@
<event name="OnMotion"></event>
<event name="OnMouseEvents"></event>
<event name="OnMouseWheel"></event>
<event name="OnPaint"></event>
<event name="OnPaint">OnComposedPaint</event>
<event name="OnRightDClick"></event>
<event name="OnRightDown"></event>
<event name="OnRightUp"></event>

View File

@ -24,8 +24,8 @@
#include "zrcolacomppnl.h"
#include "zrcolafrm.h"
#include <zrcola\compose.h>
#include <zrcola\decompose.h>
#include <zrcola/compose.h>
#include <zrcola/decompose.h>
#include <wx/msgdlg.h>
#include <wxex/common.h>

View File

@ -26,6 +26,8 @@
wxZRColaComposerPanel::wxZRColaComposerPanel(wxWindow* parent) :
m_progress(false),
m_selDecomposed(0, 0),
m_selComposed(0, 0),
wxZRColaComposerPanelBase(parent)
{
}
@ -36,36 +38,94 @@ wxZRColaComposerPanel::~wxZRColaComposerPanel()
}
void wxZRColaComposerPanel::OnDecomposedPaint(wxPaintEvent& event)
{
event.Skip();
long from, to;
m_decomposed->GetSelection(&from, &to);
if (m_selDecomposed.first != from || m_selDecomposed.second != to) {
// Save new selection first, to avoid loop.
m_selDecomposed.first = from;
m_selDecomposed.second = to;
m_composed->SetSelection(m_mapping.to_composed(from), m_mapping.to_composed(to));
}
}
void wxZRColaComposerPanel::OnDecomposedText(wxCommandEvent& event)
{
if (m_progress) {
// We are being updated by wxZRColaComposerPanel::OnComposedText()
event.Skip();
} else {
std::wstring composed;
ZRCola::Compose(m_decomposed->GetValue(), (size_t)-1, composed);
#ifdef __WINDOWS__
// Use Windows GetWindowText() function to avoid line ending conversion incompletely imposed by wxWidgets.
WXHWND hWnd = m_decomposed->GetHWND();
std::vector<wchar_t> src((std::vector<wchar_t>::size_type)::GetWindowTextLengthW(hWnd) + 1);
::GetWindowTextW(hWnd, src.data(), src.size());
#else
wxString src(m_decomposed->GetValue());
#endif
std::wstring dst;
ZRCola::Compose(src.data(), src.size(), dst, &m_mapping);
long from, to;
m_decomposed->GetSelection(&from, &to);
// Update composed text.
m_progress = true;
m_composed->SetValue(composed);
m_composed->SetValue(dst);
m_composed->SetSelection(m_mapping.to_composed(from), m_mapping.to_composed(to));
event.Skip();
m_progress = false;
}
}
void wxZRColaComposerPanel::OnComposedPaint(wxPaintEvent& event)
{
event.Skip();
long from, to;
m_composed->GetSelection(&from, &to);
if (m_selComposed.first != from || m_selComposed.second != to) {
// Save new selection first, to avoid loop.
m_selComposed.first = from;
m_selComposed.second = to;
m_decomposed->SetSelection(m_mapping.to_decomposed(from), m_mapping.to_decomposed(to));
}
}
void wxZRColaComposerPanel::OnComposedText(wxCommandEvent& event)
{
if (m_progress) {
// We are being updated by wxZRColaComposerPanel::OnDecomposedText()
event.Skip();
} else {
std::wstring decomposed;
ZRCola::Decompose(m_composed->GetValue(), (size_t)-1, decomposed);
#ifdef __WINDOWS__
// Use Windows GetWindowTextLength() function to avoid line ending conversion incompletely imposed by wxWidgets.
WXHWND hWnd = m_composed->GetHWND();
std::vector<wchar_t> src((std::vector<wchar_t>::size_type)::GetWindowTextLengthW(hWnd) + 1);
::GetWindowTextW(hWnd, src.data(), src.size());
#else
wxString src(m_composed->GetValue());
#endif
std::wstring dst;
ZRCola::Decompose(src.data(), src.size(), dst, &m_mapping);
long from, to;
m_composed->GetSelection(&from, &to);
// Update decomposed text.
m_progress = true;
m_decomposed->SetValue(decomposed);
m_decomposed->SetValue(dst);
m_decomposed->SetSelection(m_mapping.to_decomposed(from), m_mapping.to_decomposed(to));
event.Skip();
m_progress = false;
}

View File

@ -20,6 +20,8 @@
#pragma once
#include "zrcolagui.h"
#include <zrcola/common.h>
#include <utility>
///
@ -35,9 +37,15 @@ public:
friend class wxZRColaFrame;
protected:
virtual void OnDecomposedPaint(wxPaintEvent& event);
virtual void OnDecomposedText(wxCommandEvent& event);
virtual void OnComposedPaint(wxPaintEvent& event);
virtual void OnComposedText(wxCommandEvent& event);
protected:
bool m_progress; ///< A boolean flag to avoid recursive updates of composed and decomposed text controls
bool m_progress; ///< Boolean flag to avoid recursive updates of composed and decomposed text controls
ZRCola::mapping_vector m_mapping; ///< Character index mapping vector between composed and decomposed text
std::pair<long, long>
m_selDecomposed, ///< Character index of selected text in decomposed text control
m_selComposed; ///< Character index of selected text in composed text control
};

View File

@ -39,12 +39,12 @@ wxZRColaComposerPanelBase::wxZRColaComposerPanelBase( wxWindow* parent, wxWindow
wxBoxSizer* bSizerEditor;
bSizerEditor = new wxBoxSizer( wxVERTICAL );
m_decomposed = new wxTextCtrl( this, wxID_DECOMPOSED, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_CENTRE );
m_decomposed = new wxTextCtrl( this, wxID_DECOMPOSED, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_CENTRE|wxTE_MULTILINE );
m_decomposed->SetFont( wxFont( 20, 70, 90, 90, false, wxT("00 ZRCola") ) );
bSizerEditor->Add( m_decomposed, 50, wxALL|wxEXPAND, 5 );
m_composed = new wxTextCtrl( this, wxID_COMPOSED, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_CENTRE );
m_composed = new wxTextCtrl( this, wxID_COMPOSED, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_CENTRE|wxTE_MULTILINE );
m_composed->SetFont( wxFont( 20, 70, 90, 90, false, wxT("00 ZRCola") ) );
bSizerEditor->Add( m_composed, 50, wxALL|wxEXPAND, 5 );
@ -55,14 +55,18 @@ wxZRColaComposerPanelBase::wxZRColaComposerPanelBase( wxWindow* parent, wxWindow
bSizerEditor->Fit( this );
// Connect Events
m_decomposed->Connect( wxEVT_PAINT, wxPaintEventHandler( wxZRColaComposerPanelBase::OnDecomposedPaint ), NULL, this );
m_decomposed->Connect( wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler( wxZRColaComposerPanelBase::OnDecomposedText ), NULL, this );
m_composed->Connect( wxEVT_PAINT, wxPaintEventHandler( wxZRColaComposerPanelBase::OnComposedPaint ), NULL, this );
m_composed->Connect( wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler( wxZRColaComposerPanelBase::OnComposedText ), NULL, this );
}
wxZRColaComposerPanelBase::~wxZRColaComposerPanelBase()
{
// Disconnect Events
m_decomposed->Disconnect( wxEVT_PAINT, wxPaintEventHandler( wxZRColaComposerPanelBase::OnDecomposedPaint ), NULL, this );
m_decomposed->Disconnect( wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler( wxZRColaComposerPanelBase::OnDecomposedText ), NULL, this );
m_composed->Disconnect( wxEVT_PAINT, wxPaintEventHandler( wxZRColaComposerPanelBase::OnComposedPaint ), NULL, this );
m_composed->Disconnect( wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler( wxZRColaComposerPanelBase::OnComposedText ), NULL, this );
}

View File

@ -59,7 +59,9 @@ class wxZRColaComposerPanelBase : public wxPanel
wxTextCtrl* m_composed;
// Virtual event handlers, overide them in your derived class
virtual void OnDecomposedPaint( wxPaintEvent& event ) { event.Skip(); }
virtual void OnDecomposedText( wxCommandEvent& event ) { event.Skip(); }
virtual void OnComposedPaint( wxPaintEvent& event ) { event.Skip(); }
virtual void OnComposedText( wxCommandEvent& event ) { event.Skip(); }

View File

@ -22,6 +22,7 @@
<ClCompile Include="..\src\compose.cpp" />
<ClCompile Include="..\src\data.cpp" />
<ClCompile Include="..\src\decompose.cpp" />
<ClCompile Include="..\src\mapping.cpp" />
<ClCompile Include="..\src\stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>

View File

@ -27,6 +27,9 @@
<ClCompile Include="..\src\data.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\mapping.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\src\stdafx.h">

View File

@ -19,22 +19,59 @@
#pragma once
#include <vector>
///
/// Public function calling convention
///
#ifdef LIBZRCOLA
#define ZRCOLA_API __declspec(dllexport)
#else
#define ZRCOLA_API __declspec(dllimport)
#endif
#define ZRCOLA_NOVTABLE __declspec(novtable)
#pragma warning(push)
#pragma warning(disable: 4251)
namespace ZRCola {
///
/// Source-destination index mapping
/// Composed-decomposed index transformation mapping
///
class mapping {
class ZRCOLA_NOVTABLE mapping {
public:
size_t src; ///< Source index
size_t dst; ///< Destination index
size_t cmp; ///< Character index in composed string
size_t decmp; ///< Character index in decomposed string
inline mapping() {};
inline mapping(size_t s, size_t d) : src(s), dst(d) {}
inline mapping(_In_ size_t c, _In_ size_t d) : cmp(c), decmp(d) {}
};
///
/// A vector for composed-decomposed index transformation mapping
///
class ZRCOLA_API mapping_vector : public std::vector<mapping> {
public:
///
/// Transforms character index of decomposed to composed string
///
/// \param[in] decmp Character index in decomposed string
///
/// \returns Character index in composed string
///
size_t to_composed(_In_ size_t decmp) const;
///
/// Transforms destination index to source index
///
/// \param[in] cmp Character index in composed string
///
/// \returns Character index in decomposed string
///
size_t to_decomposed(_In_ size_t cmp) const;
};
};
#pragma warning(pop)

View File

@ -31,6 +31,8 @@ void ZRCOLA_API ZRCola::Decompose(_In_z_count_(inputMax) const wchar_t *input, _
// Since decomposition expands the string, let's keep our fingers crossed to avoid reallocation later.
output.clear();
output.reserve(inputMax * 2);
if (map)
map->clear();
for (size_t i = 0; i < inputMax;) {
// Find whether the character can be decomposed.
@ -58,9 +60,4 @@ void ZRCOLA_API ZRCola::Decompose(_In_z_count_(inputMax) const wchar_t *input, _
}
}
}
if (map) {
// Add final mapping.
map->push_back(mapping(inputMax, output.length()));
}
}

View File

@ -0,0 +1,70 @@
/*
Copyright 2015-2016 Amebis
This file is part of ZRCola.
ZRCola is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ZRCola is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ZRCola. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
size_t ZRCola::mapping_vector::to_composed(_In_ size_t decmp) const
{
for (size_type l = 0, r = size();;) {
if (l < r) {
size_type m = (l + r) / 2;
const mapping &el = (*this)[m];
if (decmp < el.decmp) r = m;
else if (el.decmp < decmp) l = m + 1;
else {
// An exact match found.
return el.cmp;
}
} else if (l) {
// We found a map interval.
const mapping &el = (*this)[l - 1];
return el.cmp + (decmp - el.decmp);
} else {
// The decomposed character index is far left.
return decmp;
}
}
}
size_t ZRCola::mapping_vector::to_decomposed(_In_ size_t cmp) const
{
for (size_type l = 0, r = size();;) {
if (l < r) {
size_type m = (l + r) / 2;
const mapping &el = (*this)[m];
if (cmp < el.cmp) r = m;
else if (el.cmp < cmp) l = m + 1;
else {
// An exact match found.
return el.decmp;
}
} else if (l) {
// We found a map interval.
const mapping &el = (*this)[l - 1];
return el.decmp + (cmp - el.cmp);
} else {
// The composed character index is far left.
return cmp;
}
}
}

@ -1 +1 @@
Subproject commit 27edfa1a59f121f1d143f6b0135bc8a315508a1f
Subproject commit 6914baf5b2f0c9eaef036a0aaa347460652b96ea