Widgets/[Rich]TextAreaWidget: Add copy/cut/paste support

This commit is contained in:
Jérôme Leclercq
2021-11-28 20:21:07 +01:00
parent a29c0b0e63
commit bb93209713
6 changed files with 108 additions and 2 deletions

View File

@@ -165,6 +165,59 @@ namespace Nz
switch (key.virtualKey)
{
// Copy (Ctrl+C)
case Keyboard::VKey::C:
{
if (!key.control)
break;
bool ignoreDefaultAction = (m_echoMode != EchoMode::Normal);
OnTextAreaKeyCopy(this, &ignoreDefaultAction);
if (ignoreDefaultAction || !HasSelection())
return true;
CopySelectionToClipboard(m_cursorPositionBegin, m_cursorPositionEnd);
return true;
}
// Paste (Ctrl+V)
case Keyboard::VKey::V:
{
if (!key.control)
break;
bool ignoreDefaultAction = false;
OnTextAreaKeyPaste(this, &ignoreDefaultAction);
if (ignoreDefaultAction)
return true;
if (HasSelection())
EraseSelection();
PasteFromClipboard(m_cursorPositionBegin);
return true;
}
// Cut (Ctrl+X)
case Keyboard::VKey::X:
{
if (!key.control)
break;
bool ignoreDefaultAction = (m_echoMode != EchoMode::Normal);
OnTextAreaKeyCut(this, &ignoreDefaultAction);
if (ignoreDefaultAction || !HasSelection())
return true;
CopySelectionToClipboard(m_cursorPositionBegin, m_cursorPositionEnd);
EraseSelection();
return true;
}
case Keyboard::VKey::Backspace:
{
bool ignoreDefaultAction = false;

View File

@@ -154,6 +154,11 @@ namespace Nz
UpdateDisplayText();
}
void RichTextAreaWidget::CopySelectionToClipboard(const Vector2ui& selectionBegin, const Vector2ui& selectionEnd)
{
// TODO
}
AbstractTextDrawer& RichTextAreaWidget::GetTextDrawer()
{
return m_drawer;
@@ -176,6 +181,11 @@ namespace Nz
{
}
void RichTextAreaWidget::PasteFromClipboard(const Vector2ui& targetPosition)
{
// TODO
}
void RichTextAreaWidget::UpdateDisplayText()
{
/*m_drawer.Clear();

View File

@@ -5,6 +5,7 @@
#include <Nazara/Widgets/TextAreaWidget.hpp>
#include <Nazara/Core/Unicode.hpp>
#include <Nazara/Graphics/Components/GraphicsComponent.hpp>
#include <Nazara/Platform/Clipboard.hpp>
#include <Nazara/Utility/Font.hpp>
#include <Nazara/Utility/Components/NodeComponent.hpp>
#include <Nazara/Widgets/Debug.hpp>
@@ -110,6 +111,17 @@ namespace Nz
}
}
void TextAreaWidget::CopySelectionToClipboard(const Vector2ui& selectionBegin, const Vector2ui& selectionEnd)
{
std::size_t glyphCount = ComputeCharacterCount(m_text);
assert(glyphCount > 0);
std::size_t startIndex = GetCharacterPosition(m_text, GetGlyphIndex(selectionBegin));
std::size_t endIndex = GetCharacterPosition(m_text, std::min(GetGlyphIndex(selectionEnd), glyphCount - 1));
Clipboard::SetString(m_text.substr(startIndex, endIndex - startIndex));
}
AbstractTextDrawer& TextAreaWidget::GetTextDrawer()
{
return m_drawer;
@@ -216,6 +228,21 @@ namespace Nz
}
}
void TextAreaWidget::PasteFromClipboard(const Vector2ui& targetPosition)
{
std::size_t glyphCount = ComputeCharacterCount(m_text);
std::size_t targetIndex = GetCharacterPosition(m_text, std::min(GetGlyphIndex(targetPosition), glyphCount));
std::string clipboardString = Clipboard::GetString();
if (clipboardString.empty())
return;
m_text.insert(targetIndex, clipboardString);
UpdateDisplayText();
SetCursorPosition(targetIndex + ComputeCharacterCount(clipboardString));
}
void TextAreaWidget::UpdateDisplayText()
{
switch (m_echoMode)