From 4731dae6e4b28fa2a253560723982e89b1e7e20a Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Thu, 7 Nov 2013 21:08:57 +0100 Subject: [PATCH] [DEV] add store --- sources/appl/Buffer.cpp | 36 +++++++++-- sources/appl/Buffer.h | 29 +++++++++ sources/appl/BufferManager.cpp | 10 ++- sources/appl/BufferManager.h | 34 ++++++++-- sources/appl/Gui/BufferView.cpp | 103 ++++++++++++++----------------- sources/appl/Gui/MainWindows.cpp | 84 ++++++++++++++++++++++--- sources/appl/Gui/MainWindows.h | 3 + sources/appl/Gui/TextViewer.cpp | 54 ++++++++++++---- sources/appl/Gui/TextViewer.h | 11 ++++ sources/appl/globalMsg.cpp | 1 + sources/appl/globalMsg.h | 1 + 11 files changed, 278 insertions(+), 88 deletions(-) diff --git a/sources/appl/Buffer.cpp b/sources/appl/Buffer.cpp index baf9f07..da176c6 100644 --- a/sources/appl/Buffer.cpp +++ b/sources/appl/Buffer.cpp @@ -122,6 +122,7 @@ appl::Buffer::~Buffer(void) { bool appl::Buffer::loadFile(const etk::UString& _name) { APPL_DEBUG("Load file : '" << _name << "'"); m_fileName = _name; + m_isModify = true; setHighlightType(""); etk::FSNode file(m_fileName); if (file.exist() == false) { @@ -132,13 +133,40 @@ bool appl::Buffer::loadFile(const etk::UString& _name) { if (true == m_data.dumpFrom(file) ) { countNumberofLine(); tryFindHighlightType(); + m_isModify = false; return true; } return false; } void appl::Buffer::setFileName(const etk::UString& _name) { - // TODO : ... + if (m_fileName == _name) { + return; + } + m_fileName = _name; + setModification(true); +} + +bool appl::Buffer::storeFile(void) { + etk::FSNode file(m_fileName); + if (true == m_data.dumpIn(file) ) { + APPL_INFO("saving file : " << file); + setModification(false); + return true; + } + return false; +} + +void appl::Buffer::setModification(bool _status) { + if (m_isModify == _status) { + return; + } + m_isModify = _status; + if (m_isModify == true) { + generateEventId(eventIsModify); + } else { + generateEventId(eventIsSave); + } } @@ -449,7 +477,7 @@ bool appl::Buffer::write(const etk::UString& _data, const appl::Buffer::Iterator m_selectMode = false; moveCursor((esize_t)_pos+output.size()); countNumberofLine(); // TODO : use more intelligent counter - generateEventId(eventIsModify); + setModification(true); return true; } @@ -460,7 +488,7 @@ bool appl::Buffer::replace(const etk::UString& _data, const appl::Buffer::Iterat m_selectMode = false; moveCursor((esize_t)_pos+output.size()); countNumberofLine(); // TODO : use more intelligent counter - generateEventId(eventIsModify); + setModification(true); return true; } @@ -473,7 +501,7 @@ void appl::Buffer::removeSelection(void) { m_selectMode = false; moveCursor(startPos); countNumberofLine(); // TODO : use more intelligent counter - generateEventId(eventIsModify); + setModification(true); } } diff --git a/sources/appl/Buffer.h b/sources/appl/Buffer.h index 434d7f9..6bc9ca0 100644 --- a/sources/appl/Buffer.h +++ b/sources/appl/Buffer.h @@ -276,9 +276,38 @@ namespace appl { const etk::UString& getFileName(void) { return m_fileName; } + /** + * @brief Load A specific file in this buffer. + * @param[in] _name name of the file. + * @return true if file corectly opened. + */ bool loadFile(const etk::UString& _name); + /** + * @brief Set a file name at this buffer (no saving ...) + * @param[in] _name name of the file. + */ void setFileName(const etk::UString& _name); + /** + * @brief save the file in the specify path. + * @return true is saving well done + */ + bool storeFile(void); + protected: bool m_isModify; //!< true if the file is modify + public: + /** + * @breif get the status of file modification. + * @return true if file is modify. + */ + bool isModify(void) { + return m_isModify; + } + /** + * @brief Set the file modification status. + * @param[in] _status New modification status. + */ + void setModification(bool _status); + protected: etk::Buffer m_data; //!< copy of the file buffer public: etk::Buffer& getData(void) { diff --git a/sources/appl/BufferManager.cpp b/sources/appl/BufferManager.cpp index 98f904a..2b688c4 100644 --- a/sources/appl/BufferManager.cpp +++ b/sources/appl/BufferManager.cpp @@ -16,7 +16,8 @@ #define __class__ "BufferManager" appl::BufferManager::BufferManager(void) : - ewol::Resource("???BufferManager???") { + ewol::Resource("???BufferManager???"), + m_bufferSelected(NULL) { } @@ -57,8 +58,15 @@ appl::Buffer* appl::BufferManager::get(const etk::UString& _fileName, bool _crea } return NULL; } +void appl::BufferManager::setBufferSelected(appl::Buffer* _bufferSelected) { + m_bufferSelected = _bufferSelected; + sendMultiCast(appl::MsgSelectChange, ""); +} void appl::BufferManager::onObjectRemove(ewol::EObject * _removeObject) { + if (m_bufferSelected == _removeObject) { + setBufferSelected(NULL); + } for (esize_t iii = 0; iii < m_list.size(); ++iii) { if (m_list[iii] != _removeObject) { continue; diff --git a/sources/appl/BufferManager.h b/sources/appl/BufferManager.h index 67ee4cb..ac61272 100644 --- a/sources/appl/BufferManager.h +++ b/sources/appl/BufferManager.h @@ -40,10 +40,36 @@ namespace appl { * @return true if the buffer is already open. */ bool exist(const etk::UString& _fileName); - /* - appl::Buffer* get(esize_t _bufferID); - esize_t size(void); - */ + /** + * @brief Get count of all buffer availlable. + * @return Number of buffer + */ + esize_t size(void) const { + return m_list.size(); + } + /** + * @brief Get a pointer on a buffer Id (never remember this ID!!!). + * @param[in] _id Number of buffer + * @return pointer on the buffer + */ + appl::Buffer* get(esize_t _id) { + return m_list[_id]; + } + private: + appl::Buffer* m_bufferSelected; + public: + /** + * @brief Set the current buffer selected + * @param[in] _bufferSelected Pointer on the buffer selected + */ + void setBufferSelected(appl::Buffer* _bufferSelected); + /** + * @brief Get the current buffer selected + * @return Pointer on the buffer selected + */ + appl::Buffer* getBufferSelected(void) { + return m_bufferSelected; + }; public: // herited function void onReceiveMessage(const ewol::EMessage& _msg); void onObjectRemove(ewol::EObject * _removeObject); diff --git a/sources/appl/Gui/BufferView.cpp b/sources/appl/Gui/BufferView.cpp index d14b779..585c6e1 100644 --- a/sources/appl/Gui/BufferView.cpp +++ b/sources/appl/Gui/BufferView.cpp @@ -45,6 +45,7 @@ BufferView::BufferView(void) { registerMultiCast(ednMsgBufferState); registerMultiCast(ednMsgBufferId); registerMultiCast(appl::MsgSelectNewFile); + registerMultiCast(appl::MsgSelectChange); m_selectedID = -1; m_selectedIdRequested = -1; // load buffer manager: @@ -84,6 +85,8 @@ void BufferView::onReceiveMessage(const ewol::EMessage& _msg) { APPL_ERROR("event on element nor exist : " << _msg.getData()); return; } + buffer->registerOnEvent(this, appl::Buffer::eventIsSave); + buffer->registerOnEvent(this, appl::Buffer::eventIsModify); appl::dataBufferStruct* tmp = new appl::dataBufferStruct(_msg.getData(), buffer); if (tmp == NULL) { APPL_ERROR("Allocation error of the tmp buffer list element"); @@ -91,6 +94,37 @@ void BufferView::onReceiveMessage(const ewol::EMessage& _msg) { } m_list.pushBack(tmp); markToRedraw(); + return; + } + if (_msg.getMessage() == appl::Buffer::eventIsSave) { + markToRedraw(); + return; + } + if (_msg.getMessage() == appl::Buffer::eventIsModify) { + markToRedraw(); + return; + } + APPL_DEBUG("message : " << _msg); + if (_msg.getMessage() == appl::MsgSelectChange) { + m_selectedID = -1; + appl::Buffer* tmpBuffer = NULL; + if (m_bufferManager != NULL) { + tmpBuffer = m_bufferManager->getBufferSelected(); + } + if (tmpBuffer != NULL) { + for (esize_t iii=0; iiim_buffer != tmpBuffer) { + continue; + } + m_selectedID = iii; + break; + } + } + markToRedraw(); + return; } if (_msg.getMessage() == ednMsgBufferListChange) { // clean The list @@ -170,10 +204,11 @@ bool BufferView::getElement(int32_t _colomn, int32_t _raw, etk::UString& _myText && _rawm_bufferName.getNameFile(); - /* - if (true == m_list[_raw]->m_isModify) { - _fg = (*m_paintingProperties)[m_colorTextModify].getForeground(); - } else */ { + + if ( m_list[_raw]->m_buffer != NULL + && m_list[_raw]->m_buffer->isModify() == false) { + _fg = (*m_paintingProperties)[m_colorTextNormal].getForeground(); + } else { _fg = (*m_paintingProperties)[m_colorTextModify].getForeground(); } if (_raw%2 == 0) { @@ -182,63 +217,12 @@ bool BufferView::getElement(int32_t _colomn, int32_t _raw, etk::UString& _myText _bg = (*m_paintingProperties)[m_colorBackground2].getForeground(); } // the buffer change of selection ... - /* - if (m_selectedIdRequested == m_list[_raw]->m_bufferID) { - m_selectedID = _raw; - // stop searching - m_selectedIdRequested = -1; - // set the raw visible : - setRawVisible(m_selectedID); - } - */ - /* if (m_selectedID == _raw) { _bg = (*m_paintingProperties)[m_colorBackgroundSelect].getForeground(); } - */ } else { _myTextToWrite = "ERROR"; } - /* - bool isModify; - basicColor_te selectFG = COLOR_LIST_TEXT_NORMAL; - basicColor_te selectBG = COLOR_LIST_BG_1; - // when requested a new display selection == > reset the previous one ... - if (m_selectedIdRequested != -1) { - m_selectedID = -1; - } - if( _raw >= 0 - && _rawm_bufferName.getNameFile(); - - if (true == m_list[_raw]->m_isModify) { - selectFG = COLOR_LIST_TEXT_MODIFY; - } else { - selectFG = COLOR_LIST_TEXT_NORMAL; - } - if (_raw%2 == 0) { - selectBG = COLOR_LIST_BG_1; - } else { - selectBG = COLOR_LIST_BG_2; - } - // the buffer change of selection ... - if (m_selectedIdRequested == m_list[_raw]->m_bufferID) { - m_selectedID = _raw; - // stop searching - m_selectedIdRequested = -1; - // set the raw visible : - setRawVisible(m_selectedID); - } - if (m_selectedID == _raw) { - selectBG = COLOR_LIST_BG_SELECTED; - } - } else { - _myTextToWrite = "ERROR"; - } - _fg = ColorizeManager::get(selectFG); - _bg = ColorizeManager::get(selectBG); - */ return true; } @@ -249,11 +233,14 @@ bool BufferView::onItemEvent(int32_t _IdInput, ewol::keyEvent::status_te _typeEv if( _raw >= 0 && _rawm_buffer; - //sendMultiCast(ednMsgBufferId, m_list[_raw]->m_buffer); + if (m_list[_raw]->m_buffer != NULL) { + sendMultiCast(appl::MsgSelectNewFile, m_list[_raw]->m_buffer->getFileName()); + m_selectedID = _raw; + markToRedraw(); + return true; + } } } - markToRedraw(); return false; } diff --git a/sources/appl/Gui/MainWindows.cpp b/sources/appl/Gui/MainWindows.cpp index 5c0dd5e..c4fe432 100644 --- a/sources/appl/Gui/MainWindows.cpp +++ b/sources/appl/Gui/MainWindows.cpp @@ -122,6 +122,9 @@ MainWindows::MainWindows(void) { BufferView * myBufferView = NULL; widget::Menu * myMenu = NULL; + // load buffer manager: + m_bufferManager = appl::BufferManager::keep(); + mySizerVert = new widget::Sizer(widget::Sizer::modeVert); setSubWidget(mySizerVert); @@ -270,7 +273,9 @@ MainWindows::MainWindows(void) { MainWindows::~MainWindows(void) { - + if (m_bufferManager != NULL) { + appl::BufferManager::release(m_bufferManager); + } } @@ -285,22 +290,80 @@ void MainWindows::onReceiveMessage(const ewol::EMessage& _msg) { // open file Section ... if (_msg.getMessage() == ednMsgGuiOpen) { widget::FileChooser* tmpWidget = new widget::FileChooser(); + if (tmpWidget == NULL) { + APPL_ERROR("Can not open File chooser !!! "); + return; + } tmpWidget->setTitle("Open files ..."); tmpWidget->setValidateLabel("Open"); - /* - if (BufferManager::getSelected()!=-1) { - BufferText * myBuffer = BufferManager::get(BufferManager::getSelected()); - if (NULL!=myBuffer) { - etk::FSNode tmpFile = myBuffer->getFileName(); - tmpWidget->setFolder(tmpFile.getNameFolder()); - } + if (m_bufferManager == NULL) { + APPL_ERROR("can not call unexistant buffer manager ... "); + return; } - */ + // Get a ref on the buffer selected (if null, no buffer was selected ...) + appl::Buffer* tmpBuffer = m_bufferManager->getBufferSelected(); + if (tmpBuffer != NULL) { + etk::FSNode tmpFile = tmpBuffer->getFileName(); + tmpWidget->setFolder(tmpFile.getNameFolder()); + } + // apply widget pop-up ... popUpWidgetPush(tmpWidget); tmpWidget->registerOnEvent(this, ewolEventFileChooserValidate, ednEventPopUpFileSelected); } else if (_msg.getMessage() == ednEventPopUpFileSelected) { APPL_DEBUG("Request opening the file : " << _msg.getData()); - sendMultiCast(ednMsgOpenFile, _msg.getData()); + if (m_bufferManager == NULL) { + APPL_ERROR("can not call unexistant buffer manager ... "); + return; + } + m_bufferManager->open(_msg.getData()); + } else if (_msg.getMessage() == ednMsgGuiSave) { + APPL_DEBUG("Request saving the file : " << _msg.getData()); + if (m_bufferManager == NULL) { + APPL_ERROR("can not call unexistant buffer manager ... "); + return; + } + if (_msg.getData().toLower() == "current") { + appl::Buffer* tmpBuffer = m_bufferManager->getBufferSelected(); + if (tmpBuffer == NULL) { + APPL_WARNING("No buffer selected !!! "); + createPopUpMessage(widget::Windows::messageTypeError, "No buffer selected !!!"); + return; + } + // Note : for direct saving, we do not chack the saving status ==> all time saving ... + if (tmpBuffer->getFileName() == "") { + // TODO : Has no name ==> must generate a a save AS !!! + APPL_TODO("Has no name ==> must generate a a save AS"); + return; + } + if (tmpBuffer->storeFile() == false) { + // TODO : Generate a pop-up to inform error... + APPL_ERROR("can not save the file !!! '" << tmpBuffer->getFileName() << "'"); + } + return; + } else if (_msg.getData().toLower() == "all") { + APPL_TODO("Need to save all the buffers ... "); + for (esize_t iii=0; iii < m_bufferManager->size(); ++iii) { + appl::Buffer* tmpBuffer = m_bufferManager->get(iii); + if (tmpBuffer == NULL) { + continue; + } + if (tmpBuffer->isModify() == false) { + continue; + } + if (tmpBuffer->getFileName() == "") { + // TODO : Has no name ==> must generate a a save AS !!! + APPL_TODO("Has no name ==> must generate a a save AS"); + continue; + } + if (tmpBuffer->storeFile() == false) { + // TODO : Generate a pop-up to inform error... + APPL_ERROR("can not save the file !!! '" << tmpBuffer->getFileName() << "'"); + } + } + return; + } else { + APPL_ERROR("UNKNOW request : " << _msg); + } } else if (_msg.getMessage() == ednMsgGuiSaveAs) { if (_msg.getData() == "") { APPL_ERROR("Null data for Save As file ... "); @@ -311,6 +374,7 @@ void MainWindows::onReceiveMessage(const ewol::EMessage& _msg) { } else { sscanf(_msg.getData().c_str(), "%d", &m_currentSavingAsIdBuffer); } + /* if (false == BufferManager::exist(m_currentSavingAsIdBuffer)) { APPL_ERROR("Request saveAs on non existant Buffer ID=" << m_currentSavingAsIdBuffer); diff --git a/sources/appl/Gui/MainWindows.h b/sources/appl/Gui/MainWindows.h index d9e2c21..7e3e96d 100644 --- a/sources/appl/Gui/MainWindows.h +++ b/sources/appl/Gui/MainWindows.h @@ -15,6 +15,7 @@ #include #include #include +#include class MainWindows : public ewol::Windows { private: @@ -24,6 +25,8 @@ class MainWindows : public ewol::Windows { // Constructeur MainWindows(void); ~MainWindows(void); + private: + appl::BufferManager* m_bufferManager; //!< handle on the buffer manager public: // Derived function virtual const char * const getObjectType(void) { return "MainWindows"; }; virtual void onReceiveMessage(const ewol::EMessage& _msg); diff --git a/sources/appl/Gui/TextViewer.cpp b/sources/appl/Gui/TextViewer.cpp index b08fb8d..15b5552 100644 --- a/sources/appl/Gui/TextViewer.cpp +++ b/sources/appl/Gui/TextViewer.cpp @@ -65,6 +65,8 @@ appl::TextViewer::TextViewer(const etk::UString& _fontName, int32_t _fontSize) : m_buffer->loadFile("./example.txt"); */ appl::textPluginManager::connect(*this); + // last created has focus ... + setCurrentSelect(); } appl::TextViewer::~TextViewer(void) { @@ -390,10 +392,12 @@ bool appl::TextViewer::onEventEntry(const ewol::EventEntry& _event) { } bool appl::TextViewer::onEventInput(const ewol::EventInput& _event) { + if (_event.getId() != 0) { + keepFocus(); + } if (m_buffer == NULL) { return false; } - keepFocus(); // First call the scrolling widget : if (WidgetScrooled::onEventInput(_event) == true) { markToRedraw(); @@ -542,6 +546,19 @@ void appl::TextViewer::onReceiveMessage(const ewol::EMessage& _msg) { markToRedraw(); return; } + // event needed even if selection of buffer is not done ... + if (_msg.getMessage() == appl::Buffer::eventIsModify) { + markToRedraw(); + return; + } + if (_msg.getMessage() == appl::Buffer::eventSelectChange) { + markToRedraw(); + return; + } + // If not the last buffer selected, then no event parsing ... + if (isSelectedLast() == false) { + return; + } if (_msg.getMessage() == appl::MsgSelectNewFile) { if (m_buffer != NULL) { m_buffer->unRegisterOnEvent(this); @@ -551,14 +568,9 @@ void appl::TextViewer::onReceiveMessage(const ewol::EMessage& _msg) { m_buffer->registerOnEvent(this, appl::Buffer::eventIsModify); m_buffer->registerOnEvent(this, appl::Buffer::eventSelectChange); } - markToRedraw(); - return; - } - if (_msg.getMessage() == appl::Buffer::eventIsModify) { - markToRedraw(); - return; - } - if (_msg.getMessage() == appl::Buffer::eventSelectChange) { + if (m_bufferManager != NULL) { + m_bufferManager->setBufferSelected(m_buffer); + } markToRedraw(); return; } @@ -575,11 +587,14 @@ void appl::TextViewer::onObjectRemove(ewol::EObject* _removeObject) { void appl::TextViewer::onGetFocus(void) { showKeyboard(); APPL_INFO("Focus - In"); + setCurrentSelect(); + markToRedraw(); } void appl::TextViewer::onLostFocus(void) { hideKeyboard(); APPL_INFO("Focus - out"); + markToRedraw(); } void appl::TextViewer::setFontSize(int32_t _size) { @@ -591,8 +606,6 @@ void appl::TextViewer::setFontName(const etk::UString& _fontName) { m_displayText.setFontName(_fontName); } - - bool appl::TextViewer::moveCursor(const appl::Buffer::Iterator& _pos) { if (m_buffer == NULL) { return false; @@ -826,3 +839,22 @@ float appl::TextViewer::getScreenSize(const appl::Buffer::Iterator& _startLinePo } return ret; } + +appl::TextViewer* appl::TextViewer::m_currentBufferSelect = NULL; + +void appl::TextViewer::setCurrentSelect(void) { + if (this == m_currentBufferSelect) { + return; + } + m_currentBufferSelect = this; + if (m_bufferManager != NULL) { + m_bufferManager->setBufferSelected(m_buffer); + } +} + +bool appl::TextViewer::isSelectedLast(void) { + if (this == m_currentBufferSelect) { + return true; + } + return false; +} diff --git a/sources/appl/Gui/TextViewer.h b/sources/appl/Gui/TextViewer.h index 0bae825..bd59009 100644 --- a/sources/appl/Gui/TextViewer.h +++ b/sources/appl/Gui/TextViewer.h @@ -112,6 +112,17 @@ namespace appl { appl::Buffer::Iterator getPosSize(const appl::Buffer::Iterator& _startLinePos, float _distance); float getScreenSize(const appl::Buffer::Iterator& _startLinePos, const appl::Buffer::Iterator& _stopPos); + private: + static TextViewer* m_currentBufferSelect; //!< to know which buffer is currently last selected + /** + * @brief Set the current buffer selected + */ + void setCurrentSelect(void); + /** + * @brief Check if the current buffer is last selected + * @return true if selected last + */ + bool isSelectedLast(void); }; }; diff --git a/sources/appl/globalMsg.cpp b/sources/appl/globalMsg.cpp index c128d3d..fa9fd5b 100644 --- a/sources/appl/globalMsg.cpp +++ b/sources/appl/globalMsg.cpp @@ -59,5 +59,6 @@ extern const char* const ednMsgBufferColor = "edn-Msg-Buffer-Color"; extern const char* const appl::MsgSelectNewFile = "edn-msg-select-new-file"; +extern const char* const appl::MsgSelectChange = "edn-msg-select-change"; diff --git a/sources/appl/globalMsg.h b/sources/appl/globalMsg.h index ce84737..145b92e 100644 --- a/sources/appl/globalMsg.h +++ b/sources/appl/globalMsg.h @@ -59,6 +59,7 @@ namespace appl { extern const char* const MsgSelectNewFile; // data : "buffer/name" + extern const char* const MsgSelectChange; // data : "" }; #endif