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) {
|
2014-08-27 22:58:21 +02:00
|
|
|
m_activateOnReceiveShortCut = true;
|
2013-10-22 22:01:31 +02:00
|
|
|
m_activateOnWrite = true;
|
|
|
|
m_activateOnReplace = true;
|
|
|
|
m_activateOnRemove = true;
|
2014-05-26 21:42:51 +02:00
|
|
|
addObjectType("appl::TextPluginHistory");
|
2013-10-22 22:01:31 +02:00
|
|
|
}
|
|
|
|
|
2014-08-07 23:41:48 +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) {
|
2017-03-16 21:14:14 +01:00
|
|
|
m_menuIdTitle = menu->addTitle("_T{Edit}");
|
2014-08-29 22:52:21 +02:00
|
|
|
if (m_menuIdTitle != -1) {
|
2017-03-16 21:14:14 +01:00
|
|
|
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 :
|
2014-08-27 22:58:21 +02:00
|
|
|
_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
|
|
|
}
|
|
|
|
|
2014-08-27 22:58:21 +02:00
|
|
|
bool appl::TextPluginHistory::onDataReceiveShortCut(appl::TextViewer& _textDrawer,
|
2017-08-28 00:09:10 +02:00
|
|
|
const etk::String& _shortCutName,
|
2014-08-27 22:58:21 +02:00
|
|
|
appl::PluginHistoryData& _data) {
|
2013-10-22 22:01:31 +02:00
|
|
|
if (isEnable() == false) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-27 22:58:21 +02:00
|
|
|
if (_shortCutName == "appl::TextPluginHistory::Redo") {
|
2013-11-25 22:18:06 +01:00
|
|
|
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;
|
|
|
|
}
|
2013-11-25 22:18:06 +01:00
|
|
|
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);
|
2013-11-24 16:07:43 +01:00
|
|
|
_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;
|
2014-08-27 22:58:21 +02:00
|
|
|
} else if (_shortCutName == "appl::TextPluginHistory::Undo") {
|
2013-11-25 22:18:06 +01:00
|
|
|
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;
|
|
|
|
}
|
2013-11-25 22:18:06 +01:00
|
|
|
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);
|
2013-11-24 16:07:43 +01:00
|
|
|
_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;
|
|
|
|
}
|
|
|
|
|
2013-11-25 22:18:06 +01:00
|
|
|
void appl::TextPluginHistory::clearRedo(appl::PluginHistoryData& _data) {
|
|
|
|
if (_data.m_redo.size() == 0) {
|
2013-10-22 22:01:31 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-11-25 22:18:06 +01:00
|
|
|
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;
|
|
|
|
}
|
2017-10-21 19:07:42 +02:00
|
|
|
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
|
|
|
}
|
2013-11-25 22:18:06 +01:00
|
|
|
_data.m_redo.clear();
|
2013-10-22 22:01:31 +02:00
|
|
|
}
|
|
|
|
|
2013-11-25 22:18:06 +01:00
|
|
|
void appl::TextPluginHistory::clearUndo(appl::PluginHistoryData& _data) {
|
|
|
|
if (_data.m_undo.size() == 0) {
|
2013-10-22 22:01:31 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-11-25 22:18:06 +01:00
|
|
|
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;
|
|
|
|
}
|
2017-10-21 19:07:42 +02:00
|
|
|
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
|
|
|
}
|
2013-11-25 22:18:06 +01: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;
|
|
|
|
}
|
2017-10-21 19:07:42 +02:00
|
|
|
appl::History *tmpElement = ETK_NEW(appl::History);
|
2018-06-19 22:31:29 +02:00
|
|
|
if (tmpElement != null) {
|
2013-11-25 22:18:06 +01:00
|
|
|
tmpElement->m_addedText = _strData;
|
2013-11-23 18:30:52 +01:00
|
|
|
tmpElement->m_posAdded = (int64_t)_pos;
|
|
|
|
tmpElement->m_endPosRemoved = (int64_t)_pos;
|
2017-03-16 21:14:14 +01:00
|
|
|
} else {
|
|
|
|
APPL_ERROR("History allocation error");
|
2013-10-22 22:01:31 +02:00
|
|
|
}
|
2013-11-25 22:18:06 +01:00
|
|
|
_textDrawer.writeDirect(_strData, _pos);
|
2018-06-19 22:31:29 +02:00
|
|
|
if (tmpElement != null) {
|
2013-11-24 16:07:43 +01:00
|
|
|
tmpElement->m_endPosAdded = (int64_t)_textDrawer.cursor();
|
2013-11-25 22:18:06 +01:00
|
|
|
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) {
|
2014-09-18 22:27:54 +02:00
|
|
|
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;
|
|
|
|
}
|
2017-10-21 19:07:42 +02:00
|
|
|
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;
|
2013-11-25 22:18:06 +01:00
|
|
|
tmpElement->m_addedText = _strData;
|
2013-11-23 18:30:52 +01:00
|
|
|
tmpElement->m_endPosRemoved = (int64_t)_posEnd;
|
2013-11-24 16:07:43 +01:00
|
|
|
_textDrawer.copy(tmpElement->m_removedText, _pos, _posEnd);
|
2013-10-22 22:01:31 +02:00
|
|
|
}
|
2013-11-25 22:18:06 +01:00
|
|
|
_textDrawer.replaceDirect(_strData, _pos, _posEnd);
|
2018-06-19 22:31:29 +02:00
|
|
|
if (tmpElement != null) {
|
2013-11-24 16:07:43 +01:00
|
|
|
tmpElement->m_endPosAdded = (int64_t)_textDrawer.cursor();
|
2013-11-25 22:18:06 +01:00
|
|
|
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) {
|
2014-09-18 22:27:54 +02:00
|
|
|
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;
|
|
|
|
}
|
2017-10-21 19:07:42 +02:00
|
|
|
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;
|
2013-11-24 16:07:43 +01:00
|
|
|
_textDrawer.copy(tmpElement->m_removedText, _pos, _posEnd);
|
2013-11-25 22:18:06 +01:00
|
|
|
clearRedo(_data);
|
2017-08-28 00:09:10 +02:00
|
|
|
_data.m_undo.pushBack(tmpElement);
|
2013-10-22 22:01:31 +02:00
|
|
|
}
|
2013-11-24 16:07:43 +01: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) {
|
2014-09-18 22:27:54 +02:00
|
|
|
mng->onCursorMove(_textDrawer, _textDrawer.cursor());
|
|
|
|
}
|
2013-10-22 22:01:31 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|