edn/sources/appl/TextPluginHistory.cpp

203 lines
6.6 KiB
C++
Raw Normal View History

2016-05-02 22:01:55 +02:00
/** @file
2013-10-22 22:01:31 +02:00
* @author Edouard DUPIN
* @copyright 2010, Edouard DUPIN, all right reserved
* @license GPL v3 (see license file)
*/
2016-10-03 22:01:55 +02:00
#include <appl/TextPluginHistory.hpp>
#include <appl/Gui/TextViewer.hpp>
#include <appl/TextPluginManager.hpp>
2013-10-22 22:01:31 +02:00
2014-08-29 22:52:21 +02:00
appl::TextPluginHistory::TextPluginHistory() :
m_menuIdTitle(-1),
m_menuIdUndo(-1),
m_menuIdRedo(-1) {
m_activateOnReceiveShortCut = true;
2013-10-22 22:01:31 +02:00
m_activateOnWrite = true;
m_activateOnReplace = true;
m_activateOnRemove = true;
addObjectType("appl::TextPluginHistory");
2013-10-22 22:01:31 +02:00
}
2013-10-22 22:01:31 +02:00
void appl::TextPluginHistory::onPluginEnable(appl::TextViewer& _textDrawer) {
2016-07-19 22:03:39 +02:00
ememory::SharedPtr<ewol::widget::Menu> menu = m_menuInterface.lock();
2018-06-19 22:31:29 +02:00
if (menu != null) {
m_menuIdTitle = menu->addTitle("_T{Edit}");
2014-08-29 22:52:21 +02:00
if (m_menuIdTitle != -1) {
m_menuIdUndo = menu->add(m_menuIdTitle, "_T{Undo}", "THEME:GUI:Undo.svg", "appl::TextPluginHistory::menu:undo");
m_menuIdRedo = menu->add(m_menuIdTitle, "_T{Redo}", "THEME:GUI:Redo.svg", "appl::TextPluginHistory::menu:redo");
2014-08-29 22:52:21 +02:00
}
}
2013-10-22 22:01:31 +02:00
// add event :
_textDrawer.ext_shortCutAdd("ctrl+z", "appl::TextPluginHistory::Undo");
_textDrawer.ext_shortCutAdd("ctrl+shift+z", "appl::TextPluginHistory::Redo");
2013-10-22 22:01:31 +02:00
}
void appl::TextPluginHistory::onPluginDisable(appl::TextViewer& _textDrawer) {
2014-08-29 22:52:21 +02:00
_textDrawer.ext_shortCutRm("appl::TextPluginHistory::Undo");
_textDrawer.ext_shortCutRm("appl::TextPluginHistory::Redo");
2016-07-19 22:03:39 +02:00
ememory::SharedPtr<ewol::widget::Menu> menu = m_menuInterface.lock();
2018-06-19 22:31:29 +02:00
if (menu != null) {
2014-08-29 22:52:21 +02:00
menu->remove(m_menuIdRedo);
menu->remove(m_menuIdUndo);
menu->remove(m_menuIdTitle);
}
m_menuIdTitle = -1;
m_menuIdUndo = -1;
m_menuIdRedo = -1;
2013-10-22 22:01:31 +02:00
}
bool appl::TextPluginHistory::onDataReceiveShortCut(appl::TextViewer& _textDrawer,
2017-08-28 00:09:10 +02:00
const etk::String& _shortCutName,
appl::PluginHistoryData& _data) {
2013-10-22 22:01:31 +02:00
if (isEnable() == false) {
return false;
}
if (_shortCutName == "appl::TextPluginHistory::Redo") {
if (_data.m_redo.size() == 0) {
2013-10-22 22:01:31 +02:00
return true;
}
2018-06-19 22:31:29 +02:00
if (_data.m_redo[_data.m_redo.size()-1] == null) {
2017-08-28 00:09:10 +02:00
_data.m_redo.popBack();
2013-10-22 22:01:31 +02:00
return true;
}
appl::History *tmpElement = _data.m_redo[_data.m_redo.size()-1];
2017-08-28 00:09:10 +02:00
_data.m_redo.popBack();
_data.m_undo.pushBack(tmpElement);
_textDrawer.replaceDirect(tmpElement->m_addedText,
_textDrawer.position(tmpElement->m_posAdded),
_textDrawer.position(tmpElement->m_endPosRemoved) );
2013-10-22 22:01:31 +02:00
return true;
} else if (_shortCutName == "appl::TextPluginHistory::Undo") {
if (_data.m_undo.size() == 0) {
2013-10-22 22:01:31 +02:00
return true;
}
2018-06-19 22:31:29 +02:00
if (_data.m_undo[_data.m_undo.size()-1] == null) {
2017-08-28 00:09:10 +02:00
_data.m_undo.popBack();
2013-10-22 22:01:31 +02:00
return true;
}
appl::History *tmpElement = _data.m_undo[_data.m_undo.size()-1];
2017-08-28 00:09:10 +02:00
_data.m_undo.popBack();
_data.m_redo.pushBack(tmpElement);
_textDrawer.replaceDirect(tmpElement->m_removedText,
_textDrawer.position(tmpElement->m_posAdded),
_textDrawer.position(tmpElement->m_endPosAdded) );
2013-10-22 22:01:31 +02:00
return true;
}
return false;
}
void appl::TextPluginHistory::clearRedo(appl::PluginHistoryData& _data) {
if (_data.m_redo.size() == 0) {
2013-10-22 22:01:31 +02:00
return;
}
for (size_t iii=0; iii<_data.m_redo.size(); ++iii) {
2018-06-19 22:31:29 +02:00
if (_data.m_redo[iii] == null) {
2013-10-22 22:01:31 +02:00
continue;
}
ETK_DELETE(appl::History, _data.m_redo[iii]);
2018-06-19 22:31:29 +02:00
_data.m_redo[iii] = null;
2013-10-22 22:01:31 +02:00
}
_data.m_redo.clear();
2013-10-22 22:01:31 +02:00
}
void appl::TextPluginHistory::clearUndo(appl::PluginHistoryData& _data) {
if (_data.m_undo.size() == 0) {
2013-10-22 22:01:31 +02:00
return;
}
for (size_t iii=0; iii<_data.m_undo.size(); ++iii) {
2018-06-19 22:31:29 +02:00
if (_data.m_undo[iii] == null) {
2013-10-22 22:01:31 +02:00
continue;
}
ETK_DELETE(appl::History, _data.m_undo[iii]);
2018-06-19 22:31:29 +02:00
_data.m_undo[iii] = null;
2013-10-22 22:01:31 +02:00
}
_data.m_undo.clear();
2013-10-22 22:01:31 +02:00
}
2014-08-21 21:00:13 +02:00
bool appl::TextPluginHistory::onDataWrite(appl::TextViewer& _textDrawer,
const appl::Buffer::Iterator& _pos,
2017-08-28 00:09:10 +02:00
const etk::String& _strData,
2014-08-21 21:00:13 +02:00
appl::PluginHistoryData& _data) {
2013-10-22 22:01:31 +02:00
if (isEnable() == false) {
return false;
}
appl::History *tmpElement = ETK_NEW(appl::History);
2018-06-19 22:31:29 +02:00
if (tmpElement != null) {
tmpElement->m_addedText = _strData;
2013-11-23 18:30:52 +01:00
tmpElement->m_posAdded = (int64_t)_pos;
tmpElement->m_endPosRemoved = (int64_t)_pos;
} else {
APPL_ERROR("History allocation error");
2013-10-22 22:01:31 +02:00
}
_textDrawer.writeDirect(_strData, _pos);
2018-06-19 22:31:29 +02:00
if (tmpElement != null) {
tmpElement->m_endPosAdded = (int64_t)_textDrawer.cursor();
clearRedo(_data);
2017-08-28 00:09:10 +02:00
_data.m_undo.pushBack(tmpElement);
2013-10-22 22:01:31 +02:00
}
2016-07-19 22:03:39 +02:00
ememory::SharedPtr<appl::textPluginManager> mng = m_pluginManager.lock();
2018-06-19 22:31:29 +02:00
if (mng!=null) {
mng->onCursorMove(_textDrawer, _textDrawer.cursor());
}
2013-10-22 22:01:31 +02:00
return true;
}
2014-08-21 21:00:13 +02:00
bool appl::TextPluginHistory::onDataReplace(appl::TextViewer& _textDrawer,
const appl::Buffer::Iterator& _pos,
2017-08-28 00:09:10 +02:00
const etk::String& _strData,
2014-08-21 21:00:13 +02:00
const appl::Buffer::Iterator& _posEnd,
appl::PluginHistoryData& _data) {
2013-10-22 22:01:31 +02:00
if (isEnable() == false) {
return false;
}
appl::History *tmpElement = ETK_NEW(appl::History);
2018-06-19 22:31:29 +02:00
if (tmpElement != null) {
2013-11-23 18:30:52 +01:00
tmpElement->m_posAdded = (int64_t)_pos;
tmpElement->m_addedText = _strData;
2013-11-23 18:30:52 +01:00
tmpElement->m_endPosRemoved = (int64_t)_posEnd;
_textDrawer.copy(tmpElement->m_removedText, _pos, _posEnd);
2013-10-22 22:01:31 +02:00
}
_textDrawer.replaceDirect(_strData, _pos, _posEnd);
2018-06-19 22:31:29 +02:00
if (tmpElement != null) {
tmpElement->m_endPosAdded = (int64_t)_textDrawer.cursor();
clearRedo(_data);
2017-08-28 00:09:10 +02:00
_data.m_undo.pushBack(tmpElement);
2013-10-22 22:01:31 +02:00
}
2016-07-19 22:03:39 +02:00
ememory::SharedPtr<appl::textPluginManager> mng = m_pluginManager.lock();
2018-06-19 22:31:29 +02:00
if (mng!=null) {
mng->onCursorMove(_textDrawer, _textDrawer.cursor());
}
2013-10-22 22:01:31 +02:00
return true;
}
2014-08-21 21:00:13 +02:00
bool appl::TextPluginHistory::onDataRemove(appl::TextViewer& _textDrawer,
const appl::Buffer::Iterator& _pos,
const appl::Buffer::Iterator& _posEnd,
appl::PluginHistoryData& _data) {
2013-10-22 22:01:31 +02:00
if (isEnable() == false) {
return false;
}
appl::History *tmpElement = ETK_NEW(appl::History);
2018-06-19 22:31:29 +02:00
if (tmpElement != null) {
2013-10-22 22:01:31 +02:00
tmpElement->m_addedText = "";
2013-11-23 18:30:52 +01:00
tmpElement->m_posAdded = (int64_t)_pos;
2013-10-22 22:01:31 +02:00
tmpElement->m_endPosAdded = tmpElement->m_posAdded;
2013-11-23 18:30:52 +01:00
tmpElement->m_endPosRemoved = (int64_t)_posEnd;
_textDrawer.copy(tmpElement->m_removedText, _pos, _posEnd);
clearRedo(_data);
2017-08-28 00:09:10 +02:00
_data.m_undo.pushBack(tmpElement);
2013-10-22 22:01:31 +02:00
}
_textDrawer.removeDirect();
2016-07-19 22:03:39 +02:00
ememory::SharedPtr<appl::textPluginManager> mng = m_pluginManager.lock();
2018-06-19 22:31:29 +02:00
if (mng!=null) {
mng->onCursorMove(_textDrawer, _textDrawer.cursor());
}
2013-10-22 22:01:31 +02:00
return true;
}