edn/sources/appl/Gui/TextViewer.cpp

275 lines
7.9 KiB
C++
Raw Normal View History

2013-09-19 22:23:31 +02:00
/**
* @author Edouard DUPIN
*
* @copyright 2010, Edouard DUPIN, all right reserved
*
* @license GPL v3 (see license file)
*/
#include <appl/Debug.h>
#include <appl/global.h>
#include <TextViewer.h>
#include <BufferManager.h>
#include <ColorizeManager.h>
#include <ewol/clipBoard.h>
#include <SearchData.h>
#include <ewol/widget/WidgetManager.h>
#include <ewol/renderer/EObject.h>
#undef __class__
2013-10-09 22:00:24 +02:00
#define __class__ "TextViewer"
2013-09-19 22:23:31 +02:00
appl::TextViewer::TextViewer(const etk::UString& _fontName, int32_t _fontSize) :
2013-10-09 22:00:24 +02:00
m_buffer(NULL),
m_displayText(_fontName, _fontSize),
m_insertMode(false) {
2013-10-07 22:04:21 +02:00
setCanHaveFocus(true);
2013-10-09 22:00:24 +02:00
registerMultiCast(ednMsgBufferId);
registerMultiCast(ednMsgGuiCopy);
registerMultiCast(ednMsgGuiPaste);
registerMultiCast(ednMsgGuiCut);
registerMultiCast(ednMsgGuiRedo);
registerMultiCast(ednMsgGuiUndo);
registerMultiCast(ednMsgGuiRm);
registerMultiCast(ednMsgGuiSelect);
registerMultiCast(ednMsgGuiChangeCharset);
registerMultiCast(ednMsgGuiFind);
registerMultiCast(ednMsgGuiReplace);
registerMultiCast(ednMsgGuiGotoLine);
2013-10-07 22:04:21 +02:00
setLimitScrolling(0.2);
2013-09-19 22:23:31 +02:00
2013-10-09 22:00:24 +02:00
shortCutAdd("ctrl+w", ednMsgGuiRm, "Line");
shortCutAdd("ctrl+shift+w", ednMsgGuiRm, "Paragraph");
shortCutAdd("ctrl+x", ednMsgGuiCut, "STD");
shortCutAdd("ctrl+c", ednMsgGuiCopy, "STD");
shortCutAdd("ctrl+v", ednMsgGuiPaste, "STD");
shortCutAdd("ctrl+a", ednMsgGuiSelect, "ALL");
shortCutAdd("ctrl+shift+a", ednMsgGuiSelect, "NONE");
2013-09-19 22:23:31 +02:00
// by default we load an example object:
m_buffer = new appl::Buffer();
if (m_buffer == NULL) {
APPL_ERROR("can not create buffer ... ");
return;
}
2013-10-07 22:04:21 +02:00
m_buffer->loadFile("./example.txt");
2013-09-19 22:23:31 +02:00
}
2013-10-09 22:00:24 +02:00
appl::TextViewer::~TextViewer(void) {
2013-09-19 22:23:31 +02:00
}
2013-10-09 22:00:24 +02:00
bool appl::TextViewer::calculateMinSize(void) {
2013-09-19 22:23:31 +02:00
m_minSize.setValue(50,50);
return true;
}
2013-10-09 22:00:24 +02:00
void appl::TextViewer::calculateMaxSize(void) {
2013-09-19 22:23:31 +02:00
m_maxSize.setX(256);
m_maxSize.setY(256);
}
2013-10-09 22:00:24 +02:00
void appl::TextViewer::onDraw(void) {
2013-10-07 22:04:21 +02:00
m_displayDrawing.draw();
m_displayText.draw();
WidgetScrooled::onDraw();
2013-09-19 22:23:31 +02:00
}
2013-10-09 22:00:24 +02:00
void appl::TextViewer::onRegenerateDisplay(void) {
2013-10-07 22:04:21 +02:00
if (false == needRedraw()) {
2013-09-26 22:15:39 +02:00
return;
}
// For the scrooling windows
2013-10-07 22:04:21 +02:00
calculateMaxSize();
m_displayDrawing.clear();
m_displayText.clear();
2013-09-26 22:15:39 +02:00
2013-10-07 22:04:21 +02:00
// reset the background :
m_displayDrawing.setPos(vec3(0, 0, 0));
m_displayDrawing.setColor(etk::Color<>(220, 220, 220, 256));
m_displayDrawing.rectangleWidth(m_size);
2013-09-26 22:15:39 +02:00
if (m_buffer == NULL) {
2013-10-07 22:04:21 +02:00
m_displayText.setTextAlignement(10, m_size.x()-20, ewol::Text::alignLeft);
m_displayText.setRelPos(vec3(10, 0, 0));
2013-09-26 22:15:39 +02:00
etk::UString tmpString("<br/>\n"
"<font color=\"red\">\n"
" <b>\n"
" edn - Editeur De N'ours\n"
" </b>\n"
"</font>\n"
"<br/>\n"
"<br/>\n"
"<font color=\"indigo\">\n"
" <i>\n"
" No Buffer Availlable to display\n"
" </i>\n"
"</font>\n");
2013-10-07 22:04:21 +02:00
m_displayText.setPos(vec3(0.0f, m_size.y(), 0.0f) );
m_displayText.forceLineReturn();
m_displayText.printDecorated(tmpString);
2013-09-26 22:15:39 +02:00
// call the herited class...
2013-10-07 22:04:21 +02:00
WidgetScrooled::onRegenerateDisplay();
2013-09-26 22:15:39 +02:00
return;
}
// normal displa of the buffer :
vec3 tmpCursorPosition(0, 0, -1);
// real display ...
2013-10-07 22:04:21 +02:00
etk::Buffer& buf = m_buffer->getData();
m_displayText.setColor(etk::Color<>(0, 0, 0, 256));
2013-09-26 22:15:39 +02:00
float countNbLine = 1;
esize_t countColomn = 0;
vec3 tmpLetterSize = m_displayText.calculateSize((etk::UChar)'A');
2013-09-26 22:15:39 +02:00
vec3 positionCurentDisplay(0.0f, m_size.y()-tmpLetterSize.y(), 0.0f);
2013-10-07 22:04:21 +02:00
m_displayText.setPos(positionCurentDisplay);
2013-09-26 22:15:39 +02:00
// the siplay string :
etk::UString stringToDisplay;
esize_t bufferElementSize = 0;
etk::UChar currentValue;
bool isSelect = false;
int32_t selectPosStart = etk_min(m_buffer->m_cursorPos, m_buffer->m_cursorSelectPos);
int32_t selectPosStop = etk_max(m_buffer->m_cursorPos, m_buffer->m_cursorSelectPos);
if (m_buffer->m_cursorSelectPos<0) {
selectPosStart = -1;
selectPosStop = -1;
}
2013-10-07 22:04:21 +02:00
for (int32_t iii=0; iii<buf.size(); iii+=bufferElementSize) {
if (iii == m_buffer->m_cursorPos) {
2013-09-26 22:15:39 +02:00
// need to display the cursor :
tmpCursorPosition = positionCurentDisplay;
}
if (iii >= selectPosStart && iii < selectPosStop) {
isSelect = true;
} else {
isSelect = false;
}
2013-10-07 22:04:21 +02:00
bufferElementSize = m_buffer->get(iii, currentValue);
2013-09-26 22:15:39 +02:00
//APPL_DEBUG(" element size : " << iii << " : " << bufferElementSize);
if (currentValue == etk::UChar::Return) {
2013-09-26 22:15:39 +02:00
countNbLine += 1;
countColomn = 0;
2013-10-07 22:04:21 +02:00
if (bufferElementSize == 0) {
2013-09-26 22:15:39 +02:00
bufferElementSize = 1;
2013-09-19 22:23:31 +02:00
}
2013-09-26 22:15:39 +02:00
positionCurentDisplay.setX(0);
positionCurentDisplay.setY(positionCurentDisplay.y()-tmpLetterSize.y());
2013-10-07 22:04:21 +02:00
m_displayText.setPos(positionCurentDisplay);
2013-09-26 22:15:39 +02:00
continue;
2013-09-19 22:23:31 +02:00
}
2013-10-09 22:00:24 +02:00
m_buffer->expand(countColomn, currentValue, stringToDisplay);
if (isSelect == true) {
m_displayText.setColorBg(etk::Color<>(0x00FF00FF));
} else {
m_displayText.setColorBg(etk::Color<>(0x00000000));
}
2013-10-07 22:04:21 +02:00
//APPL_DEBUG("display : '" << currentValue << "' == > '" << stringToDisplay << "'");
//m_displayText.setPos(positionCurentDisplay);
m_displayText.print(stringToDisplay);
2013-10-07 22:04:21 +02:00
positionCurentDisplay = m_displayText.getPos();
countColomn += stringToDisplay.size();
2013-09-26 22:15:39 +02:00
2013-10-07 22:04:21 +02:00
if (bufferElementSize == 0) {
2013-09-26 22:15:39 +02:00
bufferElementSize = 1;
2013-09-19 22:23:31 +02:00
}
}
2013-09-26 22:15:39 +02:00
if (tmpCursorPosition.z()!=-1) {
// display the cursor:
//APPL_DEBUG("display cursor at position : " << tmpCursorPosition);
2013-10-07 22:04:21 +02:00
m_displayText.setPos(tmpCursorPosition);
m_displayText.setColor(etk::Color<>(0xFF0000FF));
m_displayText.setColorBg(etk::Color<>(0xFF0000FF));
m_displayText.printCursor(m_insertMode);
2013-09-26 22:15:39 +02:00
}
// call the herited class...
2013-10-07 22:04:21 +02:00
WidgetScrooled::onRegenerateDisplay();
2013-09-19 22:23:31 +02:00
}
2013-10-09 22:00:24 +02:00
bool appl::TextViewer::onEventEntry(const ewol::EventEntry& _event) {
2013-09-26 22:15:39 +02:00
if (m_buffer == NULL) {
return false;
2013-09-19 22:23:31 +02:00
}
2013-10-07 22:04:21 +02:00
// just forward event == > manage directly in the buffer
2013-10-09 22:00:24 +02:00
if (m_buffer->onEventEntry(_event, m_displayText) == true) {
2013-10-07 22:04:21 +02:00
markToRedraw();
2013-09-26 22:15:39 +02:00
return true;
}
return false;
2013-09-19 22:23:31 +02:00
}
2013-10-09 22:00:24 +02:00
bool appl::TextViewer::onEventInput(const ewol::EventInput& _event) {
2013-10-16 21:55:45 +02:00
if (m_buffer == NULL) {
return false;
2013-09-19 22:23:31 +02:00
}
2013-10-07 22:04:21 +02:00
keepFocus();
2013-10-16 21:55:45 +02:00
vec2 relativePos = relativePosition(_event.getPos());
// invert for the buffer event ...
relativePos.setY(m_size.y()-relativePos.y());
// just forward event == > manage directly in the buffer
if (m_buffer->onEventInput(_event, m_displayText, relativePos) == true) {
markToRedraw();
return true;
}
2013-09-19 22:23:31 +02:00
return true;
}
2013-10-09 22:00:24 +02:00
void appl::TextViewer::onEventClipboard(ewol::clipBoard::clipboardListe_te _clipboardID) {
2013-09-26 22:15:39 +02:00
if (m_buffer != NULL) {
etk::UString data = ewol::clipBoard::get(_clipboardID);
m_buffer->paste(data);
2013-09-26 22:15:39 +02:00
}
2013-10-07 22:04:21 +02:00
markToRedraw();
2013-09-26 22:15:39 +02:00
}
2013-10-09 22:00:24 +02:00
void appl::TextViewer::onReceiveMessage(const ewol::EMessage& _msg) {
2013-10-07 22:04:21 +02:00
// force redraw of the widget
if ( _msg.getMessage() == ednMsgGuiCopy
|| _msg.getMessage() == ednMsgGuiCut) {
if (m_buffer != NULL) {
etk::UString value;
m_buffer->copy(value);
if (value.size() != 0) {
ewol::clipBoard::set(ewol::clipBoard::clipboardStd, value);
}
}
if (_msg.getMessage() == ednMsgGuiCut) {
m_buffer->removeSelection();
}
} else if (_msg.getMessage() == ednMsgGuiPaste) {
if (m_buffer != NULL) {
ewol::clipBoard::request(ewol::clipBoard::clipboardStd);
}
} else if (_msg.getMessage() == ednMsgGuiUndo) {
if (m_buffer != NULL) {
//m_buffer->undo();
}
} else if (_msg.getMessage() == ednMsgGuiRedo) {
if (m_buffer != NULL) {
//m_buffer->redo();
}
}
2013-10-07 22:04:21 +02:00
markToRedraw();
2013-09-19 22:23:31 +02:00
}
2013-10-09 22:00:24 +02:00
void appl::TextViewer::onGetFocus(void) {
showKeyboard();
2013-09-19 22:23:31 +02:00
APPL_INFO("Focus - In");
}
2013-10-09 22:00:24 +02:00
void appl::TextViewer::onLostFocus(void) {
hideKeyboard();
2013-09-19 22:23:31 +02:00
APPL_INFO("Focus - out");
}
2013-10-09 22:00:24 +02:00
void appl::TextViewer::setFontSize(int32_t _size) {
2013-10-07 22:04:21 +02:00
m_displayText.setFontSize(_size);
setScrollingSize(_size*3.0*1.46); // 1.46 is a magic number ...
2013-09-19 22:23:31 +02:00
}
2013-10-09 22:00:24 +02:00
void appl::TextViewer::setFontName(const etk::UString& _fontName) {
2013-10-07 22:04:21 +02:00
m_displayText.setFontName(_fontName);
2013-09-19 22:23:31 +02:00
}