edn/sources/appl/Gui/BufferView.cpp

282 lines
8.0 KiB
C++
Raw Normal View History

/**
* @author Edouard DUPIN
2012-11-25 11:55:06 +01:00
*
* @copyright 2010, Edouard DUPIN, all right reserved
*
* @license GPL v3 (see license file)
*/
2013-10-25 20:49:26 +02:00
#include <appl/debug.h>
2012-04-24 09:42:14 +02:00
#include <appl/global.h>
2013-11-23 18:30:52 +01:00
#include <appl/Gui/BufferView.h>
#include <appl/BufferManager.h>
2013-10-25 22:12:34 +02:00
//#include <ColorizeManager.h>
2013-11-23 18:30:52 +01:00
#include <appl/Gui/MainWindows.h>
2013-12-13 21:50:40 +01:00
#include <ewol/object/Object.h>
#undef __class__
2013-10-09 22:00:24 +02:00
#define __class__ "BufferView"
2013-11-14 21:57:10 +01:00
static void SortElementList(std::vector<appl::dataBufferStruct*>& _list) {
std::vector<appl::dataBufferStruct *> tmpList = _list;
2013-10-09 22:00:24 +02:00
_list.clear();
2014-06-02 21:04:35 +02:00
for(size_t iii=0; iii<tmpList.size(); iii++) {
2014-06-05 22:01:24 +02:00
if (nullptr == tmpList[iii]) {
2013-10-09 22:00:24 +02:00
continue;
}
2014-06-02 21:04:35 +02:00
size_t findPos = 0;
for(size_t jjj=0; jjj<_list.size(); jjj++) {
2013-10-09 22:00:24 +02:00
//EWOL_DEBUG("compare : \""<<*tmpList[iii] << "\" and \"" << *m_listDirectory[jjj] << "\"");
2014-06-05 22:01:24 +02:00
if (_list[jjj] == nullptr) {
2013-10-09 22:00:24 +02:00
continue;
}
if (tmpList[iii]->m_bufferName.getNameFile() > _list[jjj]->m_bufferName.getNameFile()) {
findPos = jjj+1;
2012-10-18 11:09:19 +02:00
}
}
2013-10-09 22:00:24 +02:00
//EWOL_DEBUG("position="<<findPos);
2013-11-14 21:57:10 +01:00
_list.insert(_list.begin()+findPos, tmpList[iii]);
2012-10-18 11:09:19 +02:00
}
}
BufferView::BufferView() :
2013-11-25 21:03:21 +01:00
m_openOrderMode(false) {
addObjectType("appl::BufferView");
2013-10-07 22:04:21 +02:00
setCanHaveFocus(true);
m_selectedID = -1;
m_selectedIdRequested = -1;
// load buffer manager:
m_bufferManager = appl::BufferManager::create();
// load color properties
m_paintingProperties = appl::GlyphPainting::create("THEME:COLOR:bufferList.json");
// get all id properties ...
m_colorBackground1 = m_paintingProperties->request("backgroung1");
m_colorBackground2 = m_paintingProperties->request("backgroung2");
m_colorBackgroundSelect = m_paintingProperties->request("backgroungSelected");
m_colorTextNormal = m_paintingProperties->request("textNormal");
m_colorTextModify = m_paintingProperties->request("textModify");
}
void BufferView::init() {
ewol::widget::List::init();
registerMultiCast(ednMsgBufferListChange);
registerMultiCast(ednMsgBufferState);
registerMultiCast(ednMsgBufferId);
registerMultiCast(appl::MsgSelectNewFile);
registerMultiCast(appl::MsgSelectChange);
registerMultiCast(appl::MsgNameChange);
}
BufferView::~BufferView() {
2013-10-07 22:04:21 +02:00
removeAllElement();
}
2012-02-29 18:06:08 +01:00
void BufferView::removeAllElement() {
2014-06-02 21:04:35 +02:00
for(auto &it : m_list) {
delete(it);
2014-06-05 22:01:24 +02:00
it = nullptr;
2012-10-18 11:09:19 +02:00
}
2013-10-07 22:04:21 +02:00
m_list.clear();
2012-10-18 11:09:19 +02:00
}
2012-02-29 18:06:08 +01:00
2013-11-25 21:03:21 +01:00
void BufferView::insertAlphabetic(appl::dataBufferStruct* _dataStruct, bool _selectNewPosition) {
2014-06-05 22:01:24 +02:00
if (_dataStruct == nullptr) {
2013-11-25 21:03:21 +01:00
return;
}
// alphabetical order:
for (size_t iii = 0; iii < m_list.size(); ++iii) {
2014-06-05 22:01:24 +02:00
if (m_list[iii] == nullptr) {
2013-11-25 21:03:21 +01:00
continue;
}
2014-08-13 22:30:47 +02:00
if (etk::tolower(m_list[iii]->m_bufferName.getNameFile()) > etk::tolower(_dataStruct->m_bufferName.getNameFile())) {
2013-11-25 21:03:21 +01:00
m_list.insert(m_list.begin() + iii, _dataStruct);
2014-06-05 22:01:24 +02:00
_dataStruct = nullptr;
2013-11-25 21:03:21 +01:00
if (_selectNewPosition == true) {
m_selectedID = iii;
}
break;
}
}
2014-06-05 22:01:24 +02:00
if (_dataStruct != nullptr) {
2013-11-25 21:03:21 +01:00
m_list.push_back(_dataStruct);
if (_selectNewPosition == true) {
m_selectedID = m_list.size()-1;
}
}
}
2013-12-13 21:50:40 +01:00
void BufferView::onReceiveMessage(const ewol::object::Message& _msg) {
2014-08-17 23:30:37 +02:00
APPL_VERBOSE("message : " << _msg);
2013-12-13 21:50:40 +01:00
ewol::widget::List::onReceiveMessage(_msg);
if (_msg.getMessage() == appl::MsgSelectNewFile) {
std::shared_ptr<appl::Buffer> buffer = m_bufferManager->get(_msg.getData());
2014-06-05 22:01:24 +02:00
if (buffer == nullptr) {
APPL_ERROR("event on element nor exist : " << _msg.getData());
return;
}
buffer->signalIsSave.bind(shared_from_this(), &BufferView::onCallbackIsSave);
buffer->signalIsModify.bind(shared_from_this(), &BufferView::onCallbackIsModify);
buffer->signalChangeName.bind(shared_from_this(), &BufferView::onCallbackChangeName);
appl::dataBufferStruct* tmp = new appl::dataBufferStruct(_msg.getData(), buffer);
2014-06-05 22:01:24 +02:00
if (tmp == nullptr) {
APPL_ERROR("Allocation error of the tmp buffer list element");
return;
}
2013-11-25 21:03:21 +01:00
if (m_openOrderMode == true) {
m_list.push_back(tmp);
} else {
insertAlphabetic(tmp);
}
markToRedraw();
2013-11-07 21:08:57 +01:00
return;
}
if (_msg.getMessage() == appl::MsgSelectChange) {
m_selectedID = -1;
std::shared_ptr<appl::Buffer> tmpBuffer;
2014-06-05 22:01:24 +02:00
if (m_bufferManager != nullptr) {
2013-11-07 21:08:57 +01:00
tmpBuffer = m_bufferManager->getBufferSelected();
}
2014-06-05 22:01:24 +02:00
if (tmpBuffer != nullptr) {
2014-06-02 21:04:35 +02:00
for (size_t iii=0; iii<m_list.size(); iii++) {
2014-06-05 22:01:24 +02:00
if (m_list[iii] == nullptr) {
2013-11-07 21:08:57 +01:00
continue;
}
if (m_list[iii]->m_buffer != tmpBuffer) {
continue;
}
m_selectedID = iii;
break;
}
}
markToRedraw();
return;
}
2013-10-07 22:04:21 +02:00
if (_msg.getMessage() == ednMsgBufferListChange) {
2012-10-18 11:09:19 +02:00
// clean The list
2013-10-07 22:04:21 +02:00
removeAllElement();
// get all the buffer name and properties:
2014-06-02 21:04:35 +02:00
size_t nbBufferOpen = 0; // BufferManager::size();
for (size_t iii=0; iii<nbBufferOpen; iii++) {
2013-10-29 21:13:45 +01:00
/*
2013-10-09 22:00:24 +02:00
if (BufferManager::exist(iii)) {
2013-10-07 22:04:21 +02:00
BufferText* tmpBuffer = BufferManager::get(iii);
2014-06-05 22:01:24 +02:00
if (nullptr != tmpBuffer) {
2013-10-07 22:04:21 +02:00
bool isModify = tmpBuffer->isModify();
etk::FSNode name = tmpBuffer->getFileName();
appl::dataBufferStruct* tmpElement = new appl::dataBufferStruct(name, iii, isModify);
2014-06-05 22:01:24 +02:00
if (nullptr != tmpElement) {
2013-11-14 21:57:10 +01:00
m_list.push_back(tmpElement);
} else {
APPL_ERROR("Allocation error of the tmp buffer list element");
}
2012-10-18 11:09:19 +02:00
}
}
2013-10-29 21:13:45 +01:00
*/
2012-10-18 11:09:19 +02:00
}
if (true == globals::OrderTheBufferList() ) {
SortElementList(m_list);
}
2013-10-07 22:04:21 +02:00
markToRedraw();
}else if (_msg.getMessage() == ednMsgBufferId) {
2013-10-29 21:13:45 +01:00
m_selectedIdRequested = 0; //BufferManager::getSelected();
2013-10-07 22:04:21 +02:00
markToRedraw();
}else if (_msg.getMessage() == ednMsgBufferState) {
// update list of modify section ...
2014-06-02 21:04:35 +02:00
for (auto &it : m_list) {
if (it != nullptr) {
//it->m_isModify = BufferManager::get(it->m_bufferID)->isModify();
2012-10-18 11:09:19 +02:00
}
}
2013-10-07 22:04:21 +02:00
markToRedraw();
}
}
void BufferView::onCallbackChangeName() {
for (size_t iii = 0; iii < m_list.size(); ++iii) {
if (m_list[iii] == nullptr) {
continue;
}
if (m_list[iii]->m_bufferName != m_list[iii]->m_buffer->getFileName()) {
m_list[iii]->m_bufferName = m_list[iii]->m_buffer->getFileName();
if (m_openOrderMode == false) {
// re-order the fine in the correct position
appl::dataBufferStruct* tmp = m_list[iii];
m_list[iii] = nullptr;
m_list.erase(m_list.begin() + iii);
insertAlphabetic(tmp, ((int64_t)iii == m_selectedID));
break;
}
}
}
markToRedraw();
}
void BufferView::onCallbackIsSave() {
markToRedraw();
}
void BufferView::onCallbackIsModify() {
markToRedraw();
}
etk::Color<> BufferView::getBasicBG() {
return (*m_paintingProperties)[m_colorBackground1].getForeground();
}
uint32_t BufferView::getNuberOfColomn() {
return 1;
}
2013-11-14 21:57:10 +01:00
bool BufferView::getTitle(int32_t _colomn, std::string &_myTitle, etk::Color<> &_fg, etk::Color<> &_bg) {
2013-10-09 22:00:24 +02:00
_myTitle = "Buffers : ";
return true;
}
uint32_t BufferView::getNuberOfRaw() {
2013-10-07 22:04:21 +02:00
return m_list.size();
}
2013-11-14 21:57:10 +01:00
bool BufferView::getElement(int32_t _colomn, int32_t _raw, std::string& _myTextToWrite, etk::Color<>& _fg, etk::Color<>& _bg) {
if( _raw >= 0
2014-06-02 21:04:35 +02:00
&& _raw<(int64_t)m_list.size()
&& m_list[_raw] != nullptr) {
_myTextToWrite = m_list[_raw]->m_bufferName.getNameFile();
2013-11-07 21:08:57 +01:00
2014-06-05 22:01:24 +02:00
if ( m_list[_raw]->m_buffer != nullptr
2013-11-07 21:08:57 +01:00
&& m_list[_raw]->m_buffer->isModify() == false) {
_fg = (*m_paintingProperties)[m_colorTextNormal].getForeground();
} else {
_fg = (*m_paintingProperties)[m_colorTextModify].getForeground();
}
if (_raw%2 == 0) {
_bg = (*m_paintingProperties)[m_colorBackground1].getForeground();
} else {
_bg = (*m_paintingProperties)[m_colorBackground2].getForeground();
}
// the buffer change of selection ...
if (m_selectedID == _raw) {
_bg = (*m_paintingProperties)[m_colorBackgroundSelect].getForeground();
}
} else {
_myTextToWrite = "ERROR";
}
return true;
}
2013-12-13 21:50:40 +01:00
bool BufferView::onItemEvent(int32_t _IdInput, enum ewol::key::status _typeEvent, int32_t _colomn, int32_t _raw, float _x, float _y)
{
2013-12-13 21:50:40 +01:00
if (1 == _IdInput && _typeEvent == ewol::key::statusSingle) {
2013-10-09 22:00:24 +02:00
APPL_INFO("Event on List : IdInput=" << _IdInput << " colomn=" << _colomn << " raw=" << _raw );
if( _raw >= 0
2014-06-02 21:04:35 +02:00
&& _raw<(int64_t)m_list.size()
2014-06-05 22:01:24 +02:00
&& nullptr != m_list[_raw]) {
if (m_list[_raw]->m_buffer != nullptr) {
2013-11-07 21:08:57 +01:00
sendMultiCast(appl::MsgSelectNewFile, m_list[_raw]->m_buffer->getFileName());
m_selectedID = _raw;
markToRedraw();
return true;
}
}
}
return false;
}