edn/sources/appl/TextPluginMultiLineTab.cpp

97 lines
2.7 KiB
C++
Raw Normal View History

/**
* @author Edouard DUPIN
*
* @copyright 2010, Edouard DUPIN, all right reserved
*
* @license GPL v3 (see license file)
*/
#include <appl/TextPluginMultiLineTab.h>
#include <appl/Gui/TextViewer.h>
2013-10-22 21:34:13 +02:00
#undef __class__
#define __class__ "TextPluginMultiLineTab"
appl::TextPluginMultiLineTab::TextPluginMultiLineTab() {
m_activateOnEventEntry = true;
addObjectType("appl::TextPluginMultiLineTab");
}
bool appl::TextPluginMultiLineTab::onEventEntry(appl::TextViewer& _textDrawer,
2013-12-13 21:50:40 +01:00
const ewol::event::Entry& _event) {
2013-10-22 21:34:13 +02:00
if (isEnable() == false) {
return false;
}
if (_event.getType() != gale::key::keyboard_char) {
return false;
}
//APPL_DEBUG("KB EVENT : \"" << UTF8_data << "\" size=" << strlen(UTF8_data) << "type=" << (int32_t)typeEvent);
if (_event.getStatus() != gale::key::status_down) {
return false;
}
2013-11-14 21:57:10 +01:00
char32_t localValue = _event.getChar();
2013-12-28 14:23:25 +01:00
if (localValue != u32char::Tabulation) {
return false;
}
if (_textDrawer.hasTextSelected() == false) {
return false;
}
appl::Buffer::Iterator itStart = _textDrawer.selectStart();
appl::Buffer::Iterator itStop = _textDrawer.selectStop();
// get the compleate section of the buffer :
itStart = _textDrawer.getStartLine(itStart);
itStop = _textDrawer.getEndLine(itStop);
// copy the curent data in a classicle string:
2013-11-14 21:57:10 +01:00
std::string data;
_textDrawer.copy(data, itStart, itStop);
// TODO : Change this ...
bool m_useTabs = true;
2014-06-02 21:04:35 +02:00
size_t m_tabDist = 4;
2013-12-13 21:50:40 +01:00
if (true == _event.getSpecialKey().getShift() ) {
// un-indent
2013-12-28 14:23:25 +01:00
data.insert(0, 1, u32char::Return);
2014-06-02 21:04:35 +02:00
for (size_t iii=1; iii<data.size(); ++iii) {
if ((char32_t)data[iii-1] != u32char::Return) {
continue;
}
2014-06-02 21:04:35 +02:00
if((char32_t)data[iii] == u32char::Tabulation) {
data.erase(iii, 1);
2014-06-02 21:04:35 +02:00
} else if((char32_t)data[iii] == u32char::Space) {
for (size_t jjj=0; jjj<m_tabDist && jjj+iii<data.size() ; jjj++) {
if((char32_t)data[iii] == u32char::Space) {
data.erase(iii, 1);
2014-06-02 21:04:35 +02:00
} else if((char32_t)data[iii] == u32char::Tabulation) {
data.erase(iii, 1);
break;
} else {
break;
}
}
}
}
data.erase(0, 1);
} else {
// indent
2013-12-28 14:23:25 +01:00
data.insert(0, 1, u32char::Return);
2014-06-02 21:04:35 +02:00
for (size_t iii=1; iii<data.size(); iii++) {
if ((char32_t)data[iii-1] != u32char::Return) {
continue;
}
2013-12-13 21:50:40 +01:00
if (true == _event.getSpecialKey().getCtrl() ) {
2013-12-28 14:23:25 +01:00
data.insert(iii, 1, u32char::Space);
} else if (true == m_useTabs) {
2013-12-28 14:23:25 +01:00
data.insert(iii, 1, u32char::Tabulation);
} else {
2013-12-28 14:23:25 +01:00
data.insert(iii, m_tabDist, u32char::Space);
}
}
data.erase(0, 1);
}
// Real replace of DATA :
_textDrawer.replace(data, itStart, itStop);
_textDrawer.select(itStart, itStart+data.size());
return true;
}