From d6ee96775dfdf0ee8bb2f00537f11a335c9b5a18 Mon Sep 17 00:00:00 2001 From: mathias Date: Fri, 6 May 2005 10:02:03 +0000 Subject: more keyactions for textbox-patch from Vadim --- ChangeLog | 7 +++++ src/FbTk/TextBox.cc | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++--- src/FbTk/TextBox.hh | 5 +++ 3 files changed, 98 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index d5072d6..a9198ca 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ (Format: Year/Month/Day) Changes for 0.9.13 +*05/05/06: + * Added more KeyActions to TextBox (thanx to Vadim + Control + LeftArrow -> Moves cursor to the left direction, up to next word. + Control + RightArrow -> to the right direction. + Control + BackSpace -> Removes everything from the cursor left side, up to next left word. + Control + Delete -> like above but removes to the right direction. + FbTk/TextBox.cc/hh *05/05/05: * Fix #1160244, #1099704, #1094107 Mutiple keyboard layout (Mathias + thanx to Vadim) diff --git a/src/FbTk/TextBox.cc b/src/FbTk/TextBox.cc index 2ebd6c0..8e54ec7 100644 --- a/src/FbTk/TextBox.cc +++ b/src/FbTk/TextBox.cc @@ -35,6 +35,8 @@ #include #include +#include + namespace FbTk { TextBox::TextBox(int screen_num, @@ -141,11 +143,8 @@ void TextBox::insertText(const std::string &val) { m_text.insert(m_start_pos + cursorPosition(), val); m_cursor_pos += val.size(); m_end_pos += val.size(); - - if (m_start_pos + cursorPosition() < m_end_pos) - adjustEndPos(); - else - adjustStartPos(); + + adjustPos(); } void TextBox::killToEnd() { @@ -247,6 +246,47 @@ void TextBox::keyPressEvent(XKeyEvent &event) { m_cursor_pos = 0; m_end_pos = 0; break; + case XK_Left: + if (m_cursor_pos && m_text.size()){ + m_cursor_pos = findEmptySpaceLeft(); + adjustPos(); + } + break; + case XK_Right: + if (m_text.size() && m_cursor_pos < m_text.size()){ + m_cursor_pos = findEmptySpaceRight(); + adjustPos(); + } + break; + + case XK_BackSpace: { + if (!m_cursor_pos || !m_text.size()) + break; + + int pos = findEmptySpaceLeft(); + + m_text.erase(pos, m_cursor_pos - pos); + m_start_pos = 0; + m_cursor_pos = pos; + m_end_pos = m_text.size(); + adjustPos(); + } + break; + case XK_Delete: { + + if (!m_text.size() || m_cursor_pos >= m_text.size()) + break; + + int pos = findEmptySpaceRight(); + + m_text.erase(m_cursor_pos, pos - m_cursor_pos); + m_start_pos = 0; + m_cursor_pos = m_cursor_pos; + m_end_pos = m_text.size(); + + adjustPos(); + } + break; } } else if ((event.state & ShiftMask)== ShiftMask || (event.state & 0x80) == 0x80) { // shif and altgr @@ -356,6 +396,47 @@ void TextBox::adjustStartPos() { m_start_pos = start_pos; } +int TextBox::findEmptySpaceLeft(){ + // found the first left space symbol + int pos = m_text.rfind(' ', m_cursor_pos - 1); + // do we have one more space symbol near? + int next_pos = -1; + while ( pos > 0 && (next_pos = m_text.rfind(' ', pos - 1)) > -1 ){ + if (next_pos + 1 < pos) + break; + pos = next_pos; + } + if (pos < 0) + pos = 0; + + return pos; + +} +int TextBox::findEmptySpaceRight(){ + + // found the first right space symbol + int pos = m_text.find(' ', m_cursor_pos); + + // do we have one more space symbol near? + int next_pos = -1; + while (pos > -1 && pos < m_text.size() && (next_pos = m_text.find(' ', pos + 1)) > -1 ){ + if (next_pos - 1 > pos) + break; + pos = next_pos; + } + if (pos < 0) + pos = m_text.size(); + + return pos + 1; // (+1) - sets cursor at the right. + +} +void TextBox::adjustPos(){ + if (m_start_pos + cursorPosition() < m_end_pos) + adjustEndPos(); + else + adjustStartPos(); + +} }; // end namespace FbTk diff --git a/src/FbTk/TextBox.hh b/src/FbTk/TextBox.hh index ea3d00c..6b68f28 100644 --- a/src/FbTk/TextBox.hh +++ b/src/FbTk/TextBox.hh @@ -69,10 +69,15 @@ public: GC gc() const { return m_gc; } int cursorPosition() const { return m_cursor_pos; } + int findEmptySpaceLeft(); + int findEmptySpaceRight(); + private: void adjustEndPos(); void adjustStartPos(); + void adjustPos(); + const FbTk::Font *m_font; std::string m_text; GC m_gc; -- cgit v0.11.2