[DEV] add missing history plugin
This commit is contained in:
196
sources/appl/Buffer/TextPluginHistory.cpp
Normal file
196
sources/appl/Buffer/TextPluginHistory.cpp
Normal file
@@ -0,0 +1,196 @@
|
|||||||
|
/**
|
||||||
|
* @author Edouard DUPIN
|
||||||
|
*
|
||||||
|
* @copyright 2010, Edouard DUPIN, all right reserved
|
||||||
|
*
|
||||||
|
* @license GPL v3 (see license file)
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <appl/Buffer/TextPluginHistory.h>
|
||||||
|
#include <ewol/clipBoard.h>
|
||||||
|
#include <appl/Gui/TextViewer.h>
|
||||||
|
#include <appl/Buffer/TextPluginManager.h>
|
||||||
|
|
||||||
|
#undef __class__
|
||||||
|
#define __class__ "TextPluginHistory"
|
||||||
|
|
||||||
|
appl::TextPluginHistory::TextPluginHistory(void) {
|
||||||
|
m_activateOnReceiveMessage = true;
|
||||||
|
m_activateOnWrite = true;
|
||||||
|
m_activateOnReplace = true;
|
||||||
|
m_activateOnRemove = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
appl::TextPluginHistory::~TextPluginHistory(void) {
|
||||||
|
clearUndo();
|
||||||
|
clearRedo();
|
||||||
|
};
|
||||||
|
|
||||||
|
void appl::TextPluginHistory::onPluginEnable(appl::TextViewer& _textDrawer) {
|
||||||
|
// add event :
|
||||||
|
_textDrawer.registerMultiCast(ednMsgGuiRedo);
|
||||||
|
_textDrawer.registerMultiCast(ednMsgGuiUndo);
|
||||||
|
_textDrawer.shortCutAdd("ctrl+z", ednMsgGuiUndo);
|
||||||
|
_textDrawer.shortCutAdd("ctrl+shift+z", ednMsgGuiRedo);
|
||||||
|
}
|
||||||
|
|
||||||
|
void appl::TextPluginHistory::onPluginDisable(appl::TextViewer& _textDrawer) {
|
||||||
|
// TODO : unknow function ...
|
||||||
|
}
|
||||||
|
|
||||||
|
bool appl::TextPluginHistory::onReceiveMessage(appl::TextViewer& _textDrawer,
|
||||||
|
const ewol::EMessage& _msg) {
|
||||||
|
if (isEnable() == false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (_msg.getMessage() == ednMsgGuiRedo) {
|
||||||
|
if (m_redo.size() == 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (m_redo[m_redo.size()-1] == NULL) {
|
||||||
|
m_redo.popBack();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
appl::History *tmpElement = m_redo[m_redo.size()-1];
|
||||||
|
m_redo.popBack();
|
||||||
|
m_undo.pushBack(tmpElement);
|
||||||
|
_textDrawer.m_buffer->replace(tmpElement->m_addedText,
|
||||||
|
_textDrawer.m_buffer->position(tmpElement->m_posAdded),
|
||||||
|
_textDrawer.m_buffer->position(tmpElement->m_endPosRemoved) );
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} else if (_msg.getMessage() == ednMsgGuiUndo) {
|
||||||
|
if (m_undo.size() == 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (m_undo[m_undo.size()-1] == NULL) {
|
||||||
|
m_undo.popBack();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
if (m_history[m_positionHistory]->m_removedText.size() == 0) {
|
||||||
|
// just insert mode
|
||||||
|
} else if (m_history[m_positionHistory]->m_posAdded == m_history[m_positionHistory]->m_endPosAdded) {
|
||||||
|
// just remove mode
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// just replace mode
|
||||||
|
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
appl::History *tmpElement = m_undo[m_undo.size()-1];
|
||||||
|
m_undo.popBack();
|
||||||
|
m_redo.pushBack(tmpElement);
|
||||||
|
_textDrawer.m_buffer->replace(tmpElement->m_removedText,
|
||||||
|
_textDrawer.m_buffer->position(tmpElement->m_posAdded),
|
||||||
|
_textDrawer.m_buffer->position(tmpElement->m_endPosAdded) );
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void appl::TextPluginHistory::clearRedo(void) {
|
||||||
|
if (m_redo.size() == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (esize_t iii=0; iii<m_redo.size(); ++iii) {
|
||||||
|
if (m_redo[iii] == NULL) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
delete(m_redo[iii]);
|
||||||
|
m_redo[iii] = NULL;
|
||||||
|
}
|
||||||
|
m_redo.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void appl::TextPluginHistory::clearUndo(void) {
|
||||||
|
if (m_undo.size() == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (esize_t iii=0; iii<m_undo.size(); ++iii) {
|
||||||
|
if (m_undo[iii] == NULL) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
delete(m_undo[iii]);
|
||||||
|
m_undo[iii] = NULL;
|
||||||
|
}
|
||||||
|
m_undo.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool appl::TextPluginHistory::onWrite(appl::TextViewer& _textDrawer,
|
||||||
|
const appl::Buffer::Iterator& _pos,
|
||||||
|
const etk::UString& _data) {
|
||||||
|
if (isEnable() == false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
appl::History *tmpElement = new appl::History();
|
||||||
|
if (tmpElement != NULL) {
|
||||||
|
tmpElement->m_addedText = _data;
|
||||||
|
tmpElement->m_posAdded = (esize_t)_pos;
|
||||||
|
tmpElement->m_endPosRemoved = (esize_t)_pos;
|
||||||
|
}
|
||||||
|
_textDrawer.m_buffer->write(_data, _pos);
|
||||||
|
if (tmpElement != NULL) {
|
||||||
|
tmpElement->m_endPosAdded = (esize_t)_textDrawer.m_buffer->cursor();
|
||||||
|
clearRedo();
|
||||||
|
m_undo.pushBack(tmpElement);
|
||||||
|
}
|
||||||
|
appl::textPluginManager::onCursorMove(_textDrawer, _textDrawer.m_buffer->cursor());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool appl::TextPluginHistory::onReplace(appl::TextViewer& _textDrawer,
|
||||||
|
const appl::Buffer::Iterator& _pos,
|
||||||
|
const etk::UString& _data,
|
||||||
|
const appl::Buffer::Iterator& _posEnd) {
|
||||||
|
if (isEnable() == false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
appl::History *tmpElement = new appl::History();
|
||||||
|
if (tmpElement != NULL) {
|
||||||
|
tmpElement->m_posAdded = (esize_t)_pos;
|
||||||
|
tmpElement->m_addedText = _data;
|
||||||
|
tmpElement->m_endPosRemoved = (esize_t)_posEnd;
|
||||||
|
_textDrawer.m_buffer->copy(tmpElement->m_removedText, _pos, _posEnd);
|
||||||
|
}
|
||||||
|
_textDrawer.m_buffer->replace(_data, _pos, _posEnd);
|
||||||
|
if (tmpElement != NULL) {
|
||||||
|
tmpElement->m_endPosAdded = (esize_t)_textDrawer.m_buffer->cursor();
|
||||||
|
clearRedo();
|
||||||
|
m_undo.pushBack(tmpElement);
|
||||||
|
}
|
||||||
|
appl::textPluginManager::onCursorMove(_textDrawer, _textDrawer.m_buffer->cursor());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool appl::TextPluginHistory::onRemove(appl::TextViewer& _textDrawer,
|
||||||
|
const appl::Buffer::Iterator& _pos,
|
||||||
|
const appl::Buffer::Iterator& _posEnd) {
|
||||||
|
if (isEnable() == false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
appl::History *tmpElement = new appl::History();
|
||||||
|
if (tmpElement != NULL) {
|
||||||
|
tmpElement->m_addedText = "";
|
||||||
|
tmpElement->m_posAdded = (esize_t)_pos;
|
||||||
|
tmpElement->m_endPosAdded = tmpElement->m_posAdded;
|
||||||
|
tmpElement->m_endPosRemoved = (esize_t)_posEnd;
|
||||||
|
_textDrawer.m_buffer->copy(tmpElement->m_removedText, _pos, _posEnd);
|
||||||
|
clearRedo();
|
||||||
|
m_undo.pushBack(tmpElement);
|
||||||
|
}
|
||||||
|
_textDrawer.m_buffer->removeSelection();
|
||||||
|
appl::textPluginManager::onCursorMove(_textDrawer, _textDrawer.m_buffer->cursor());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void appl::TextPluginHistory::onObjectRemove(ewol::EObject* _removeObject) {
|
||||||
|
// TODO : Dependence with buffer removing ...
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
64
sources/appl/Buffer/TextPluginHistory.h
Normal file
64
sources/appl/Buffer/TextPluginHistory.h
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
/**
|
||||||
|
* @author Edouard DUPIN
|
||||||
|
*
|
||||||
|
* @copyright 2010, Edouard DUPIN, all right reserved
|
||||||
|
*
|
||||||
|
* @license GPL v3 (see license file)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __APPL_TEXT_PLUGIN_HISTORY_H__
|
||||||
|
#define __APPL_TEXT_PLUGIN_HISTORY_H__
|
||||||
|
|
||||||
|
#include <etk/types.h>
|
||||||
|
#include <ewol/renderer/EObject.h>
|
||||||
|
#include <appl/Gui/TextViewer.h>
|
||||||
|
#include <ewol/compositing/Text.h>
|
||||||
|
#include <appl/Buffer/TextPlugin.h>
|
||||||
|
|
||||||
|
namespace appl {
|
||||||
|
class History {
|
||||||
|
public:
|
||||||
|
History(void) :
|
||||||
|
m_posAdded(0),
|
||||||
|
m_endPosAdded(0),
|
||||||
|
m_endPosRemoved(0) {
|
||||||
|
|
||||||
|
};
|
||||||
|
etk::UString m_addedText;
|
||||||
|
etk::UString m_removedText;
|
||||||
|
esize_t m_posAdded;
|
||||||
|
esize_t m_endPosAdded;
|
||||||
|
esize_t m_endPosRemoved;
|
||||||
|
};
|
||||||
|
class TextPluginHistory : public appl::TextViewerPlugin {
|
||||||
|
public:
|
||||||
|
TextPluginHistory(void);
|
||||||
|
~TextPluginHistory(void);
|
||||||
|
private:
|
||||||
|
etk::Vector<History*> m_undo; //!< History storing data
|
||||||
|
etk::Vector<History*> m_redo; //!< History storing data
|
||||||
|
public:
|
||||||
|
virtual void onPluginEnable(appl::TextViewer& _textDrawer);
|
||||||
|
virtual void onPluginDisable(appl::TextViewer& _textDrawer);
|
||||||
|
virtual bool onReceiveMessage(appl::TextViewer& _textDrawer,
|
||||||
|
const ewol::EMessage& _msg);
|
||||||
|
virtual bool onWrite(appl::TextViewer& _textDrawer,
|
||||||
|
const appl::Buffer::Iterator& _pos,
|
||||||
|
const etk::UString& _data);
|
||||||
|
virtual bool onReplace(appl::TextViewer& _textDrawer,
|
||||||
|
const appl::Buffer::Iterator& _pos,
|
||||||
|
const etk::UString& _data,
|
||||||
|
const appl::Buffer::Iterator& _posEnd);
|
||||||
|
virtual bool onRemove(appl::TextViewer& _textDrawer,
|
||||||
|
const appl::Buffer::Iterator& _pos,
|
||||||
|
const appl::Buffer::Iterator& _posEnd);
|
||||||
|
private:
|
||||||
|
void clearRedo(void);
|
||||||
|
void clearUndo(void);
|
||||||
|
public:
|
||||||
|
virtual void onObjectRemove(ewol::EObject* _removeObject);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
Reference in New Issue
Block a user