[DEV] add copy/cut/paste in std clipboard

This commit is contained in:
Edouard DUPIN 2013-10-18 21:01:11 +02:00
parent 0b022d62fc
commit 16f78eaef2
3 changed files with 114 additions and 7 deletions

View File

@ -237,8 +237,8 @@ bool appl::Buffer::onEventEntry(const ewol::EventEntry& _event, ewol::Text& _tes
} else if (localValue == etk::UniChar::Suppress ) {
//APPL_INFO("keyEvent : <suppr> pos=" << m_cursorPos);
if (hasTextSelected()) {
esize_t startPos = etk_min(m_cursorPos, m_cursorSelectPos);
esize_t endPos = etk_max(m_cursorPos, m_cursorSelectPos);
esize_t startPos = getStartSelectionPos();
esize_t endPos = getStopSelectionPos();
m_data.remove(startPos, endPos-startPos);
m_selectMode = false;
moveCursor(startPos);
@ -253,8 +253,8 @@ bool appl::Buffer::onEventEntry(const ewol::EventEntry& _event, ewol::Text& _tes
} else if (localValue == etk::UniChar::Delete) {
//APPL_INFO("keyEvent : <del> pos=" << m_cursorPos);
if (hasTextSelected()) {
esize_t startPos = etk_min(m_cursorPos, m_cursorSelectPos);
esize_t endPos = etk_max(m_cursorPos, m_cursorSelectPos);
esize_t startPos = getStartSelectionPos();
esize_t endPos = getStopSelectionPos();
m_data.remove(startPos, endPos-startPos);
m_selectMode = false;
moveCursor(startPos);
@ -274,8 +274,8 @@ bool appl::Buffer::onEventEntry(const ewol::EventEntry& _event, ewol::Text& _tes
char output[5];
int32_t nbElement = localValue.getUtf8(output);
if (hasTextSelected()) {
esize_t startPos = etk_min(m_cursorPos, m_cursorSelectPos);
esize_t endPos = etk_max(m_cursorPos, m_cursorSelectPos);
esize_t startPos = getStartSelectionPos();
esize_t endPos = getStopSelectionPos();
m_data.replace(startPos, endPos-startPos, (int8_t*)output, nbElement);
moveCursor(startPos+nbElement);
} else {
@ -829,3 +829,49 @@ esize_t appl::Buffer::countBackwardNLines(esize_t _startPos, int32_t _nLines) {
//APPL_INFO(" == > (2) at position=0");
return 0;
}
bool appl::Buffer::copy(etk::UString& _data) {
_data.clear();
if (hasTextSelected() == true) {
esize_t startPos = getStartSelectionPos();
esize_t endPos = getStopSelectionPos();
esize_t bufferElementSize;
etk::UniChar value;
for(int32_t iii = startPos; iii < endPos ; iii+=bufferElementSize ) {
// get the element value:
bufferElementSize = get(iii, value);
_data += value;
if (bufferElementSize <= 0) {
bufferElementSize = 1;
}
}
return true;
}
return false;
}
bool appl::Buffer::paste(const etk::UString& _data) {
etk::Char output = _data.c_str();
if (hasTextSelected() == true) {
esize_t startPos = getStartSelectionPos();
esize_t endPos = getStopSelectionPos();
m_data.replace(m_cursorPos, endPos-startPos, (int8_t*)((void*)output), output.size());
} else {
m_data.insert(m_cursorPos, (int8_t*)((void*)output), output.size());
}
m_selectMode = false;
moveCursor(m_cursorPos+output.size());
return false;
}
void appl::Buffer::removeSelection(void) {
if (hasTextSelected() == true) {
esize_t startPos = getStartSelectionPos();
esize_t endPos = getStopSelectionPos();
m_data.remove(startPos, endPos-startPos);
m_selectMode = false;
moveCursor(startPos);
}
}

View File

@ -57,9 +57,27 @@ namespace appl {
void moveCursor(esize_t _pos);
void mouseEventDouble(void);
void mouseEventTriple(void);
/**
* @brief Get the status of selection.
* @return true if we have a curent selection, false otherwise.
*/
bool hasTextSelected(void) {
return m_cursorSelectPos >= 0;
}
/**
* @brief Get the Start position of the selection.
* @return position of the start selection.
*/
esize_t getStartSelectionPos(void) {
return etk_min(m_cursorPos, m_cursorSelectPos);
}
/**
* @brief Get the Stop position of the selection.
* @return position of the stop selection.
*/
esize_t getStopSelectionPos(void) {
return etk_max(m_cursorPos, m_cursorSelectPos);
}
bool selectAround(int32_t _startPos, int32_t &_beginPos, int32_t &_endPos);
/**
* @brief Get the position in the buffer of a display distance from the start of the line
@ -191,6 +209,23 @@ namespace appl {
* @return position of the starting the line
*/
esize_t countBackwardNLines(esize_t _startPos, int32_t _nLines);
public:
/**
* @brief copy data in the _data ref value.
* @param[out] _data Output stream to copy.
* @return true of no error occured.
*/
bool copy(etk::UString& _data);
/**
* @brief past data from the _data ref value.
* @param[in] _data Input stream to copy.
* @return true of no error occured.
*/
bool paste(const etk::UString& _data);
/**
* @brief Remove the selection of the buffer. (do nothing if no secection)
*/
void removeSelection(void);
};
};

View File

@ -217,13 +217,39 @@ bool appl::TextViewer::onEventInput(const ewol::EventInput& _event) {
void appl::TextViewer::onEventClipboard(ewol::clipBoard::clipboardListe_te _clipboardID) {
if (m_buffer != NULL) {
//tmpBuffer->Paste(_clipboardID);
etk::UString data = ewol::clipBoard::get(_clipboardID);
m_buffer->paste(data);
}
markToRedraw();
}
void appl::TextViewer::onReceiveMessage(const ewol::EMessage& _msg) {
// force redraw of the widget
if ( _msg.getMessage() == ednMsgGuiCopy
|| _msg.getMessage() == ednMsgGuiCut) {
if (m_buffer != NULL) {
etk::UString value;
m_buffer->copy(value);
if (value.size() != 0) {
ewol::clipBoard::set(ewol::clipBoard::clipboardStd, value);
}
}
if (_msg.getMessage() == ednMsgGuiCut) {
m_buffer->removeSelection();
}
} else if (_msg.getMessage() == ednMsgGuiPaste) {
if (m_buffer != NULL) {
ewol::clipBoard::request(ewol::clipBoard::clipboardStd);
}
} else if (_msg.getMessage() == ednMsgGuiUndo) {
if (m_buffer != NULL) {
//m_buffer->undo();
}
} else if (_msg.getMessage() == ednMsgGuiRedo) {
if (m_buffer != NULL) {
//m_buffer->redo();
}
}
markToRedraw();
}