2011-07-20 10:33:24 +02:00
|
|
|
/**
|
|
|
|
*******************************************************************************
|
|
|
|
* @file BufferManager.cpp
|
|
|
|
* @brief Editeur De N'ours : Text Buffer manager (sources)
|
|
|
|
* @author Edouard DUPIN
|
|
|
|
* @date 08/12/2010
|
|
|
|
* @par Project
|
|
|
|
* Edn
|
|
|
|
*
|
|
|
|
* @par Copyright
|
|
|
|
* Copyright 2010 Edouard DUPIN, all right reserved
|
|
|
|
*
|
|
|
|
* This software is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY.
|
|
|
|
*
|
|
|
|
* Licence summary :
|
|
|
|
* You can modify and redistribute the sources code and binaries.
|
|
|
|
* You can send me the bug-fix
|
|
|
|
* You can not earn money with this Software (if the source extract from Edn
|
|
|
|
* represent less than 50% of original Sources)
|
|
|
|
* Term of the licence in in the file licence.txt.
|
|
|
|
*
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2012-01-11 15:26:53 +01:00
|
|
|
#include <tools_debug.h>
|
|
|
|
#include <tools_globals.h>
|
|
|
|
#include <BufferManager.h>
|
2012-01-31 18:26:04 +01:00
|
|
|
#include <ewol/WidgetMessageMultiCast.h>
|
2011-07-20 10:33:24 +02:00
|
|
|
|
|
|
|
#undef __class__
|
|
|
|
#define __class__ "BufferManager"
|
|
|
|
|
|
|
|
// Constructeur
|
|
|
|
/**
|
|
|
|
* @brief
|
|
|
|
*
|
|
|
|
* @param[in,out] ---
|
|
|
|
*
|
|
|
|
* @return ---
|
|
|
|
*
|
|
|
|
*/
|
2012-01-31 18:26:04 +01:00
|
|
|
BufferManager::BufferManager(void)
|
2011-07-20 10:33:24 +02:00
|
|
|
{
|
|
|
|
// nothing to do ...
|
|
|
|
BufferNotExiste = new BufferEmpty();
|
|
|
|
m_idSelected = -1;
|
2012-02-03 18:14:45 +01:00
|
|
|
ewol::widgetMessageMultiCast::Add(GetWidgetId(), ednMsgGuiNew);
|
2012-02-05 22:03:36 +01:00
|
|
|
ewol::widgetMessageMultiCast::Add(GetWidgetId(), ednMsgOpenFile);
|
2012-02-03 18:14:45 +01:00
|
|
|
/*
|
2012-01-31 18:26:04 +01:00
|
|
|
ewol::widgetMessageMultiCast::Add(GetWidgetId(), ednMsgBufferManagerNewFile);
|
|
|
|
ewol::widgetMessageMultiCast::Add(GetWidgetId(), ednMsgBufferManagerSaveAll);
|
|
|
|
ewol::widgetMessageMultiCast::Add(GetWidgetId(), ednMsgBufferManagerCloseAll);
|
|
|
|
ewol::widgetMessageMultiCast::Add(GetWidgetId(), ednMsgBufferManagerClose);
|
|
|
|
ewol::widgetMessageMultiCast::Add(GetWidgetId(), ednMsgBufferManagerSave);
|
2012-02-03 18:14:45 +01:00
|
|
|
*/
|
2011-07-20 10:33:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief
|
|
|
|
*
|
|
|
|
* @param[in,out] ---
|
|
|
|
*
|
|
|
|
* @return ---
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
BufferManager::~BufferManager(void)
|
|
|
|
{
|
|
|
|
//clean All Buffer
|
|
|
|
EDN_INFO("~BufferManager::RemoveAll();");
|
|
|
|
RemoveAll();
|
|
|
|
// clear The list of Buffer
|
2011-08-03 14:39:39 +02:00
|
|
|
EDN_INFO("~BufferManager::listBuffer.Clear();");
|
|
|
|
listBuffer.Clear();
|
2011-07-20 10:33:24 +02:00
|
|
|
EDN_INFO("~BufferManager::delete(BufferNotExiste);");
|
|
|
|
delete(BufferNotExiste);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-05 22:03:36 +01:00
|
|
|
bool BufferManager::OnEventAreaExternal(int32_t widgetID, const char * generateEventId, const char * data, etkFloat_t x, etkFloat_t y)
|
2011-07-20 10:33:24 +02:00
|
|
|
{
|
2012-02-03 18:14:45 +01:00
|
|
|
if (generateEventId == ednMsgGuiNew) {
|
|
|
|
int32_t newOne = Create();
|
|
|
|
if (-1 != newOne) {
|
|
|
|
ewol::widgetMessageMultiCast::Send(GetWidgetId(), ednMsgBufferId, newOne);
|
|
|
|
ewol::widgetMessageMultiCast::Send(GetWidgetId(), ednMsgBufferListChange);
|
|
|
|
}
|
2012-02-05 22:03:36 +01:00
|
|
|
} else if (generateEventId == ednMsgOpenFile) {
|
|
|
|
if (NULL != data) {
|
|
|
|
etk::File myFile(data, etk::FILE_TYPE_DIRECT);
|
|
|
|
int32_t newOne = Open(myFile);
|
|
|
|
if (-1 != newOne) {
|
|
|
|
ewol::widgetMessageMultiCast::Send(GetWidgetId(), ednMsgBufferId, newOne);
|
|
|
|
ewol::widgetMessageMultiCast::Send(GetWidgetId(), ednMsgBufferListChange);
|
|
|
|
}
|
|
|
|
}
|
2012-02-03 18:14:45 +01:00
|
|
|
}
|
2012-01-31 18:26:04 +01:00
|
|
|
/*
|
2011-07-20 10:33:24 +02:00
|
|
|
switch (id)
|
|
|
|
{
|
2011-08-03 14:39:39 +02:00
|
|
|
case EDN_MSG__BUFFER_CHANGE_CURRENT:
|
|
|
|
m_idSelected = dataID;
|
|
|
|
break;
|
2011-07-20 10:33:24 +02:00
|
|
|
case EDN_MSG__NEW:
|
|
|
|
{
|
|
|
|
int32_t newOne = Create();
|
|
|
|
if (-1 != newOne) {
|
|
|
|
SendMessage(EDN_MSG__CURRENT_CHANGE_BUFFER_ID, newOne);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2011-08-03 14:39:39 +02:00
|
|
|
case EDN_MSG__BUFF_ID_CLOSE:
|
|
|
|
// Check buffer existence
|
|
|
|
if(true == Exist(dataID)) {
|
|
|
|
// Get the new display buffer
|
|
|
|
if (m_idSelected == dataID) {
|
|
|
|
// Try previous buffer
|
|
|
|
int32_t destBuffer = -1;
|
|
|
|
for(int32_t ii=dataID-1; ii >= 0; ii--) {
|
|
|
|
if (true == Exist(ii) ) {
|
|
|
|
destBuffer = ii;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//EDN_DEBUG("new buffer selected : ?? " << destBuffer);
|
|
|
|
// try next buffer
|
|
|
|
if (-1 == destBuffer) {
|
|
|
|
for(int32_t ii=dataID+1; ii < listBuffer.Size(); ii++) {
|
|
|
|
if (true == Exist(ii) ) {
|
|
|
|
destBuffer = ii;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//EDN_DEBUG("new buffer selected : ?? " << destBuffer);
|
|
|
|
// set it to the currect display
|
|
|
|
SendMessage(EDN_MSG__CURRENT_CHANGE_BUFFER_ID, destBuffer);
|
|
|
|
}
|
|
|
|
//EDN_DEBUG("Remove : " << dataID);
|
|
|
|
// Remove requested buffer
|
|
|
|
Remove(dataID);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EDN_MSG__BUFF_ID_SAVE:
|
|
|
|
// Check buffer existence
|
|
|
|
if(true == Exist(dataID)) {
|
|
|
|
// If no name ==> request a Gui display ...
|
|
|
|
if (Get(dataID)->HaveName() == false) {
|
|
|
|
SendMessage(EDN_MSG__GUI_SHOW_SAVE_AS, dataID);
|
|
|
|
} else {
|
|
|
|
Get(dataID)->Save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2011-07-20 10:33:24 +02:00
|
|
|
}
|
2012-01-31 18:26:04 +01:00
|
|
|
*/
|
|
|
|
return false;
|
2011-07-20 10:33:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Remove all buffer opened
|
|
|
|
*
|
|
|
|
* @param[in,out] ---
|
|
|
|
*
|
|
|
|
* @return ---
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void BufferManager::RemoveAll(void)
|
|
|
|
{
|
|
|
|
int32_t i;
|
2011-08-03 14:39:39 +02:00
|
|
|
for (i=0; i<listBuffer.Size(); i++) {
|
2011-07-20 10:33:24 +02:00
|
|
|
Remove(i);
|
|
|
|
}
|
2012-01-31 18:26:04 +01:00
|
|
|
//SendMessage(EDN_MSG__BUFFER_REMOVE_ALL);
|
|
|
|
ewol::widgetMessageMultiCast::Send(GetWidgetId(), ednMsgBufferRemoveAll);
|
2011-07-20 10:33:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Create a new buffer with no name and empty
|
|
|
|
*
|
|
|
|
* @param[in,out] ---
|
|
|
|
*
|
|
|
|
* @return The ID of the curent buffer where the file is loaded
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int32_t BufferManager::Create(void)
|
|
|
|
{
|
|
|
|
// allocate a new Buffer
|
|
|
|
Buffer *myBuffer = new BufferText();
|
|
|
|
// Add at the list of element
|
2011-08-03 14:39:39 +02:00
|
|
|
listBuffer.PushBack(myBuffer);
|
|
|
|
int32_t basicID = listBuffer.Size() - 1;
|
2012-01-31 18:26:04 +01:00
|
|
|
//SendMessage(EDN_MSG__BUFFER_ADD, basicID);
|
|
|
|
ewol::widgetMessageMultiCast::Send(GetWidgetId(), ednMsgBufferAdd);
|
2011-07-20 10:33:24 +02:00
|
|
|
return basicID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief open a file with the name set in parameters
|
|
|
|
*
|
|
|
|
* @param[in] filename curent filename
|
|
|
|
*
|
|
|
|
* @return The ID of the curent buffer where the file is loaded
|
|
|
|
*
|
|
|
|
* @todo : check if this file is not curently open and return the old ID
|
|
|
|
*
|
|
|
|
*/
|
2012-01-11 15:26:53 +01:00
|
|
|
int32_t BufferManager::Open(etk::File &myFile)
|
2011-07-20 10:33:24 +02:00
|
|
|
{
|
2011-08-05 14:31:47 +02:00
|
|
|
// TODO : Check here if the file is already open ==> and display it if needed
|
2011-07-20 10:33:24 +02:00
|
|
|
// allocate a new Buffer
|
2011-08-05 14:31:47 +02:00
|
|
|
Buffer *myBuffer = new BufferText(myFile);
|
2011-07-20 10:33:24 +02:00
|
|
|
// Add at the list of element
|
2011-08-03 14:39:39 +02:00
|
|
|
listBuffer.PushBack(myBuffer);
|
2012-02-05 22:03:36 +01:00
|
|
|
return listBuffer.Size() - 1;
|
2011-07-20 10:33:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Buffer * BufferManager::Get(int32_t BufferID)
|
|
|
|
{
|
|
|
|
// possible special case : -1;
|
|
|
|
if (-1 >= BufferID) {
|
|
|
|
return BufferNotExiste;
|
|
|
|
}
|
|
|
|
// check if the Buffer existed
|
2011-08-03 14:39:39 +02:00
|
|
|
if (BufferID < listBuffer.Size()) {
|
2011-07-20 10:33:24 +02:00
|
|
|
// check if the buffer already existed
|
|
|
|
if (NULL != listBuffer[BufferID]) {
|
|
|
|
return listBuffer[BufferID];
|
|
|
|
} else {
|
|
|
|
EDN_ERROR("non existing Buffer " << BufferID);
|
|
|
|
}
|
|
|
|
} else {
|
2011-08-03 14:39:39 +02:00
|
|
|
EDN_ERROR("call an non existing Buffer number too hight : " << BufferID << " > " << listBuffer.Size());
|
2011-07-20 10:33:24 +02:00
|
|
|
}
|
|
|
|
return BufferNotExiste;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool BufferManager::Exist(int32_t BufferID)
|
|
|
|
{
|
|
|
|
if (-1 >= BufferID) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// check if the Buffer existed
|
2011-08-03 14:39:39 +02:00
|
|
|
if (BufferID < listBuffer.Size()) {
|
2011-07-20 10:33:24 +02:00
|
|
|
// check if the buffer already existed
|
|
|
|
if (NULL != listBuffer[BufferID]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-11 15:26:53 +01:00
|
|
|
bool BufferManager::Exist(etk::File &myFile )
|
2011-08-05 14:31:47 +02:00
|
|
|
{
|
|
|
|
if (-1 == GetId(myFile)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-11 15:26:53 +01:00
|
|
|
int32_t BufferManager::GetId(etk::File &myFile)
|
2011-07-20 10:33:24 +02:00
|
|
|
{
|
|
|
|
int32_t iii;
|
|
|
|
// check if the Buffer existed
|
2011-08-03 14:39:39 +02:00
|
|
|
for (iii=0; iii < listBuffer.Size(); iii++) {
|
2011-07-20 10:33:24 +02:00
|
|
|
// check if the buffer already existed
|
|
|
|
if (NULL != listBuffer[iii]) {
|
2011-08-05 14:31:47 +02:00
|
|
|
if ( listBuffer[iii]->GetFileName() == myFile) {
|
2011-07-20 10:33:24 +02:00
|
|
|
return iii;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// return the number of buffer (open in the past) if 5 buffer open and 4 close ==> return 5
|
|
|
|
uint32_t BufferManager::Size(void)
|
|
|
|
{
|
2011-08-03 14:39:39 +02:00
|
|
|
return listBuffer.Size();
|
2011-07-20 10:33:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief
|
|
|
|
*
|
|
|
|
* @param[in,out] ---
|
|
|
|
*
|
|
|
|
* @return ---
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
bool BufferManager::Remove(int32_t BufferID)
|
|
|
|
{
|
|
|
|
if (-1 >= BufferID) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// check if the Buffer existed
|
2011-08-03 14:39:39 +02:00
|
|
|
if (BufferID < listBuffer.Size()) {
|
2011-07-20 10:33:24 +02:00
|
|
|
// check if the buffer already existed
|
|
|
|
if (NULL != listBuffer[BufferID]) {
|
|
|
|
// TODO : Check if it saved...
|
|
|
|
/*
|
|
|
|
if (false == IsSaved(BufferID) ) {
|
|
|
|
EDN_INFO("Buffer " << BufferID << " : Not Saved", BufferID);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
// Delete the Buffer
|
|
|
|
delete( listBuffer[BufferID] );
|
|
|
|
listBuffer[BufferID] = NULL;
|
2012-01-31 18:26:04 +01:00
|
|
|
//SendMessage(EDN_MSG__BUFFER_REMOVE, BufferID);
|
|
|
|
ewol::widgetMessageMultiCast::Send(GetWidgetId(), ednMsgBufferRemove);
|
2011-07-20 10:33:24 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
EDN_INFO("non existing Buffer " << BufferID);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2011-08-03 14:39:39 +02:00
|
|
|
EDN_INFO("call an non existing Buffer number too hight : " << BufferID << " > " << listBuffer.Size());
|
2011-07-20 10:33:24 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief to get the element 14 in the buffer
|
|
|
|
*
|
|
|
|
* @param[in,out] ---
|
|
|
|
*
|
|
|
|
* @return ---
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int32_t BufferManager::WitchBuffer(int32_t iEmeElement)
|
|
|
|
{
|
|
|
|
int32_t i;
|
2011-08-03 14:39:39 +02:00
|
|
|
for (i=0; i<listBuffer.Size(); i++) {
|
2011-07-20 10:33:24 +02:00
|
|
|
if (NULL != listBuffer[i]) {
|
|
|
|
iEmeElement--;
|
|
|
|
// find the element :
|
|
|
|
if (0 == iEmeElement) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|