diff --git a/sources/appl/Buffer/Buffer.cpp b/sources/appl/Buffer/Buffer.cpp index 4b74bb6..8d3cfbf 100644 --- a/sources/appl/Buffer/Buffer.cpp +++ b/sources/appl/Buffer/Buffer.cpp @@ -31,12 +31,168 @@ bool appl::Buffer::LoadFile(const etk::UString& _name) } + void appl::Buffer::SetFileName(const etk::UString& _name) { // TODO : ... } -bool appl::Buffer::OnEventEntry(const ewol::EventEntry& _event) + +void appl::Buffer::MoveCursorRight(appl::Buffer::moveMode _mode) +{ + etk::UniChar value; + esize_t nbElement; + switch (_mode) { + default: + case moveLetter: + nbElement = Get(m_cursorPos, value); + if (nbElement>0) { + m_cursorPos += nbElement; + } + break; + case moveWord: + // TODO : ... + break; + case moveEnd: + // TODO : ... + break; + } + +} + +void appl::Buffer::MoveCursorLeft(appl::Buffer::moveMode _mode) +{ + etk::UniChar value; + esize_t nbElement; + switch (_mode) { + default: + case moveLetter: + nbElement = GetBack(m_cursorPos-1, value); + if (nbElement>0) { + m_cursorPos -= nbElement; + } + break; + case moveWord: + // TODO : ... + break; + case moveEnd: + // TODO : ... + break; + } +} + +void appl::Buffer::MoveCursorUp(esize_t _nbLine) +{ + /* + // Find the position of the start of the line. + esize_t lineStartPos = m_EdnBuf.StartOfLine(m_cursorPos); + // check if we can go up ... + if (lineStartPos == 0) { + return; + } + // Decide what column to move to, if there's a preferred column use that + int32_t column = m_cursorPreferredCol; + if (m_cursorPreferredCol < 0) { + column = m_EdnBuf.CountDispChars(lineStartPos, m_cursorPos); + } + // Get the previous line + esize_t prevLineStartPos = m_EdnBuf.CountBackwardNLines(lineStartPos, _nbLine); + //APPL_INFO("Move Line UP result : prevLineStartPos=" << prevLineStartPos); + // Get the display char position + esize_t newPos = m_EdnBuf.CountForwardDispChars(prevLineStartPos, column); + //APPL_INFO("Move to colomn : column=" << column << " newPos=" << newPos); + // move the cursor + SetInsertPosition(newPos); + // if a preferred column wasn't aleady established, establish it + if (m_cursorPreferredCol < 0) { + m_cursorPreferredCol = column; + } + return; + */ +} + +void appl::Buffer::MoveCursorDown(esize_t _nbLine) +{ + /* + // check if we are not at the end of Buffer + if (m_cursorPos == m_EdnBuf.Size() ) { + return; + } + // Find the position of the start of the line. + esize_t lineStartPos = m_EdnBuf.StartOfLine(m_cursorPos); + + int32_t column = m_cursorPreferredCol; + if (m_cursorPreferredCol < 0) { + column = m_EdnBuf.CountDispChars(lineStartPos, m_cursorPos); + } + // get the next line : + esize_t nextLineStartPos = m_EdnBuf.CountForwardNLines(lineStartPos, _nbLine); + //APPL_INFO("Move Line DOWN result : nextLineStartPos=" << nextLineStartPos); + // Get the display char position + esize_t newPos = m_EdnBuf.CountForwardDispChars(nextLineStartPos, column); + //APPL_INFO("Move to colomn : column=" << column << " newPos=" << newPos); + + SetInsertPosition(newPos); + // if a preferred column wasn't aleady established, establish it + if (m_cursorPreferredCol < 0) { + m_cursorPreferredCol = column; + } + return; + */ +} + +esize_t appl::Buffer::StartLine(esize_t _pos) +{ + esize_t startPos; + if (false == SearchBackward(pos, etk::UniChar::Return, &startPos)) { + return 0; + } + return startPos + 1; +} + +esize_t appl::Buffer::EndLine(esize_t _pos) +{ + esize_t endPos; + if (false == SearchForward(pos, etk::UniChar::Return, &endPos)) { + endPos = m_data.Size(); + } + return endPos; +} + +bool appl::Buffer::SearchForward(esize_t _pos, const etk::UniChar& _search, esize_t& _result) +{ + // move in the string + esize_t nbElementBuffer = 0; + etk::UniChar value; + for(esize_t iii=_pos ; iii=0 ; iii-=nbElementBuffer ) { + nbElementBuffer = GetBack(iii, value); + if (value == _search) { + _result = iii-nbElementBuffer; + return true; + } + } + _result = 0; + return false; +} + + +bool appl::Buffer::OnEventEntry(const ewol::EventEntry& _event) // TODO : , vec2 _displaySize) { APPL_DEBUG(" event : " << _event); if (_event.GetType() == ewol::keyEvent::keyboardChar) { @@ -51,26 +207,74 @@ bool appl::Buffer::OnEventEntry(const ewol::EventEntry& _event) m_data.Insert(m_cursorPos, '\n'); m_cursorPos += 1; } else if (_event.GetChar() == etk::UniChar::Suppress ) { - APPL_INFO("keyEvent : pos=" << m_cursorPos); + //APPL_INFO("keyEvent : pos=" << m_cursorPos); + etk::UniChar value; + esize_t nbElement = Get(m_cursorPos, value); + if (nbElement>0) { + m_data.Remove(m_cursorPos, nbElement); + } } else if (_event.GetChar() == etk::UniChar::Delete) { - APPL_INFO("keyEvent : pos=" << m_cursorPos); + //APPL_INFO("keyEvent : pos=" << m_cursorPos); + etk::UniChar value; + esize_t nbElement = GetBack(m_cursorPos-1, value); + if (nbElement>0) { + m_cursorPos -= nbElement; + m_data.Remove(m_cursorPos, nbElement); + } } else { // normal adding char ... char output[5]; int32_t nbElement = _event.GetChar().GetUtf8(output); - etk::Vector values; - for (int32_t iii=0; iiiCursorMove(_event.GetType()); - + bool needUpdatePosition = true; + // check selection event ... + switch(_event.GetType()) { + case ewol::keyEvent::keyboardLeft: + //APPL_INFO("keyEvent : "); + MoveCursorLeft(); + break; + case ewol::keyEvent::keyboardRight: + //APPL_INFO("keyEvent : "); + MoveCursorRight(); + break; + case ewol::keyEvent::keyboardUp: + //APPL_INFO("keyEvent : "); + MoveCursorUp(1); + break; + case ewol::keyEvent::keyboardDown: + //APPL_INFO("keyEvent : "); + MoveCursorDown(1); + break; + case ewol::keyEvent::keyboardPageUp: + //APPL_INFO("keyEvent : "); + //TextDMoveUp(m_displaySize.y()); + break; + case ewol::keyEvent::keyboardPageDown: + //APPL_INFO("keyEvent : "); + //TextDMoveDown(m_displaySize.y()); + break; + case ewol::keyEvent::keyboardStart: + //APPL_INFO("keyEvent : "); + MoveCursorLeft(moveEnd); + break; + case ewol::keyEvent::keyboardEnd: + //APPL_INFO("keyEvent : "); + MoveCursorRight(moveEnd); + break; + default: + break; + } + /* + if ( true == needUpdatePosition) { + RequestUpdateOfThePosition(); + } + */ return true; } return false; @@ -80,13 +284,17 @@ bool appl::Buffer::OnEventEntry(const ewol::EventEntry& _event) esize_t appl::Buffer::Get(esize_t _pos, etk::UniChar& _value, unicode::charset_te _charset) const { _value = '\0'; + if (_pos<0 && _pos tmpVal + && _pos-iii>0) { + --pointerVal; + ++iii; + *pointerVal = m_data[_pos - iii]; + }; + int32_t nbElement = _value.SetUtf8(pointerVal); + return nbElement; + } + _value.Set(m_data[_pos]); + return 1; + +} + + + diff --git a/sources/appl/Buffer/Buffer.h b/sources/appl/Buffer/Buffer.h index 557fe18..a67d25f 100644 --- a/sources/appl/Buffer/Buffer.h +++ b/sources/appl/Buffer/Buffer.h @@ -53,7 +53,70 @@ namespace appl * @param[in] _charset Charset used to parse the current buffer * @return number ofelement read in the buffer (to increment the position) */ - esize_t Get(esize_t _pos, etk::UniChar& _value, unicode::charset_te _charset) const; + esize_t Get(esize_t _pos, etk::UniChar& _value, unicode::charset_te _charset = unicode::EDN_CHARSET_UTF8) const; + /** + * @brief Get the previous element in the buffer. + * @param[in] _pos Position in the buffer (last element of the element) + * @param[out] _value Unicode value read in the buffer + * @param[in] _charset Charset used to parse the current buffer + * @return number of element read in the buffer (to increment the position) + */ + esize_t GetBack(esize_t _pos, etk::UniChar& _value, unicode::charset_te _charset = unicode::EDN_CHARSET_UTF8) const; + private: + enum moveMode { + moveLetter, + moveWord, + moveEnd + } + /** + * Move the cursor right in the line (no stop of a new line) + * @param[in] _mode Moving mode char, word, ... + */ + void MoveCursorRight(moveMode _mode = moveLetter); + /** + * Move the cursor left in the line (no stop of a new line) + * @param[in] _mode Moving mode char, word, ... + */ + void MoveCursorLeft(moveMode _mode = moveLetter); + /** + * @brief Move the cursor at an other position upper. + * @param[in] _nbLine number of up line that might be moved + */ + void MoveCursorUp(esize_t _nbLine); + /** + * @brief Move the cursor at an other position under. + * @param[in] _nbLine number of down line that might be moved + */ + void MoveCursorDown(esize_t _nbLine); + /** + * @brief Get the start of a line with the position in the buffer. + * @param[in] _pos position in the buffer. + * @return The position in the buffer of the start of the line. + */ + esize_t StartLine(esize_t _pos); + /** + * @brief Get the end of a line with the position in the buffer. + * @param[in] _pos position in the buffer. + * @return The position in the buffer of the end of the line. + */ + esize_t EndLine(esize_t _pos); + /** + * @brief Search a character in the buffer. + * @param[in] _pos Position to start the search of the element. + * @param[in] _search Character to search. + * @param[out] _result Research position. + * @return true if pos if fined. + */ + bool SearchForward(esize_t _pos, const etk::UniChar& _search, esize_t& _result); + /** + * @brief Search a character in the buffer in back mode. + * @param[in] _pos Position to start the search of the element. + * @param[in] _search Character to search. + * @param[out] _result Research position. + * @return true if pos if fined. + */ + bool SearchBack(esize_t _pos, const etk::UniChar& _search, esize_t& _result); + }; }; diff --git a/sources/appl/Gui/TextViewer.cpp b/sources/appl/Gui/TextViewer.cpp index c8489ff..2f7108c 100644 --- a/sources/appl/Gui/TextViewer.cpp +++ b/sources/appl/Gui/TextViewer.cpp @@ -240,6 +240,7 @@ bool appl::TextViewer::OnEventEntry(const ewol::EventEntry& _event) if (m_buffer == NULL) { return false; } + // just forward event ==> manage directly in the buffer if (m_buffer->OnEventEntry(_event) == true) { MarkToRedraw(); return true;