edn/sources/appl/TextPluginData.hpp

126 lines
4.1 KiB
C++
Raw Normal View History

2016-05-02 22:01:55 +02:00
/** @file
* @author Edouard DUPIN
* @copyright 2010, Edouard DUPIN, all right reserved
* @license GPL v3 (see license file)
*/
2016-05-02 22:01:55 +02:00
#pragma once
2016-10-03 22:01:55 +02:00
#include <etk/types.hpp>
#include <ewol/object/Object.hpp>
#include <appl/Gui/TextViewer.hpp>
#include <ewol/compositing/Text.hpp>
#include <appl/TextPlugin.hpp>
namespace appl {
template <typename TYPE> class TextViewerPluginData : public appl::TextViewerPlugin {
protected:
TextViewerPluginData() {
// nothing to do ...
addObjectType("appl::TextViewerPluginData");
}
public:
DECLARE_FACTORY(TextViewerPluginData);
virtual ~TextViewerPluginData() {
for (size_t iii = 0; iii < m_specificData.size() ; ++iii) {
2018-06-19 22:31:29 +02:00
if (m_specificData[iii].second != null) {
remove(*m_specificData[iii].second);
}
}
m_specificData.clear();
}
private:
2017-10-09 10:25:41 +02:00
etk::Vector<etk::Pair<ememory::WeakPtr<appl::Buffer> ,ememory::UniquePtr<TYPE>>> m_specificData;
protected:
TYPE* getDataRef(appl::TextViewer& _textDrawer) {
auto it = m_specificData.begin();
while(it != m_specificData.end()) {
2016-07-19 22:03:39 +02:00
ememory::SharedPtr<appl::Buffer> buf = it->first.lock();
2018-06-19 22:31:29 +02:00
if (buf == null) {
it = m_specificData.erase(it);
continue;
}
if (buf == _textDrawer.internalGetBuffer()) {
return it->second.get();
}
++it;
}
ememory::UniquePtr<TYPE> data(ETK_NEW(TYPE));
2018-06-19 22:31:29 +02:00
if (data == null) {
2017-03-16 22:39:06 +01:00
APPL_ERROR("ALLOCATION plugin data error");
2018-06-19 22:31:29 +02:00
return null;
}
2017-03-16 22:39:06 +01:00
TYPE* copyPocalPointer = data.get();
2017-10-09 10:25:41 +02:00
ememory::WeakPtr<appl::Buffer> tmpBuffer = _textDrawer.internalGetBuffer();
m_specificData.pushBack(etk::makePair(tmpBuffer, etk::move(data)));
// create a new one ...
2017-03-16 22:39:06 +01:00
return copyPocalPointer;
}
protected: // Wrap all element with their internal data: (do not use theses function)
bool onReceiveShortCut(appl::TextViewer& _textDrawer,
2017-08-28 00:09:10 +02:00
const etk::String& _shortCutName) {
TYPE* data = getDataRef(_textDrawer);
2018-06-19 22:31:29 +02:00
if (data == null) {
return false;
}
return onDataReceiveShortCut(_textDrawer, _shortCutName, *data);
}
bool onWrite(appl::TextViewer& _textDrawer,
const appl::Buffer::Iterator& _pos,
2017-08-28 00:09:10 +02:00
const etk::String& _data) {
TYPE* data = getDataRef(_textDrawer);
2018-06-19 22:31:29 +02:00
if (data == null) {
return false;
}
2014-08-21 21:00:13 +02:00
return onDataWrite(_textDrawer, _pos, _data, *data);
}
bool onReplace(appl::TextViewer& _textDrawer,
const appl::Buffer::Iterator& _pos,
2017-08-28 00:09:10 +02:00
const etk::String& _data,
const appl::Buffer::Iterator& _posEnd) {
TYPE* data = getDataRef(_textDrawer);
2018-06-19 22:31:29 +02:00
if (data == null) {
return false;
}
2014-08-21 21:00:13 +02:00
return onDataReplace(_textDrawer, _pos, _data, _posEnd, *data);
}
bool onRemove(appl::TextViewer& _textDrawer,
const appl::Buffer::Iterator& _pos,
const appl::Buffer::Iterator& _posEnd) {
TYPE* data = getDataRef(_textDrawer);
2018-06-19 22:31:29 +02:00
if (data == null) {
return false;
}
2014-08-21 21:00:13 +02:00
return onDataRemove(_textDrawer, _pos, _posEnd, *data);
}
public:
virtual bool onDataReceiveShortCut(appl::TextViewer& _textDrawer,
2017-08-28 00:09:10 +02:00
const etk::String& _shortCutName,
TYPE& _data) {
return false;
}
2014-08-21 21:00:13 +02:00
virtual bool 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
TYPE& _data) {
return false;
}
2014-08-21 21:00:13 +02:00
virtual bool 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,
TYPE& _data) {
return false;
}
2014-08-21 21:00:13 +02:00
virtual bool onDataRemove(appl::TextViewer& _textDrawer,
const appl::Buffer::Iterator& _pos,
const appl::Buffer::Iterator& _posEnd,
TYPE& _data) {
return false;
}
virtual void remove(TYPE& _data) {
return;
};
};
2016-05-02 22:01:55 +02:00
}