implement part 1 of #267 - tab position marks in editors

This commit is contained in:
Vadim Lopatin 2016-06-08 16:24:29 +03:00
parent 76a631ebff
commit 2f5a481d2e
1 changed files with 46 additions and 27 deletions

View File

@ -273,7 +273,7 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction
@property bool copyCurrentLineWhenNoSelection() { return _copyCurrentLineWhenNoSelection; } @property bool copyCurrentLineWhenNoSelection() { return _copyCurrentLineWhenNoSelection; }
@property EditWidgetBase copyCurrentLineWhenNoSelection(bool flg) { _copyCurrentLineWhenNoSelection = flg; return this; } @property EditWidgetBase copyCurrentLineWhenNoSelection(bool flg) { _copyCurrentLineWhenNoSelection = flg; return this; }
protected bool _showTabPositionMarks = true; protected bool _showTabPositionMarks = false;
/// when true shows mark on tab positions in beginning of line /// when true shows mark on tab positions in beginning of line
@property bool showTabPositionMarks() { return _showTabPositionMarks; } @property bool showTabPositionMarks() { return _showTabPositionMarks; }
@property EditWidgetBase showTabPositionMarks(bool flg) { @property EditWidgetBase showTabPositionMarks(bool flg) {
@ -2681,6 +2681,8 @@ class EditBox : EditWidgetBase {
} }
void drawWhiteSpaceMarks(DrawBuf buf, ref FontRef font, dstring txt, int tabSize, Rect lineRect, Rect visibleRect) { void drawWhiteSpaceMarks(DrawBuf buf, ref FontRef font, dstring txt, int tabSize, Rect lineRect, Rect visibleRect) {
// _showTabPositionMarks
// _showWhiteSpaceMarks
int firstNonSpace = -1; int firstNonSpace = -1;
int lastNonSpace = -1; int lastNonSpace = -1;
bool hasTabs = false; bool hasTabs = false;
@ -2698,7 +2700,12 @@ class EditBox : EditWidgetBase {
uint color = addAlpha(textColor, 0xC0); uint color = addAlpha(textColor, 0xC0);
static int[] textSizeBuffer; static int[] textSizeBuffer;
int charsMeasured = font.measureText(txt, textSizeBuffer, MAX_WIDTH_UNSPECIFIED, tabSize, 0, 0); int charsMeasured = font.measureText(txt, textSizeBuffer, MAX_WIDTH_UNSPECIFIED, tabSize, 0, 0);
int ts = tabSize;
if (ts < 1)
ts = 1;
if (ts > 8)
ts = 8;
int spaceIndex = 0;
for (int i = 0; i < txt.length && i < charsMeasured; i++) { for (int i = 0; i < txt.length && i < charsMeasured; i++) {
dchar ch = txt[i]; dchar ch = txt[i];
bool outsideText = (i < firstNonSpace || i >= lastNonSpace); bool outsideText = (i < firstNonSpace || i >= lastNonSpace);
@ -2708,30 +2715,42 @@ class EditBox : EditWidgetBase {
rc.right = lineRect.left + textSizeBuffer[i]; rc.right = lineRect.left + textSizeBuffer[i];
int h = rc.height; int h = rc.height;
if (rc.intersects(visibleRect)) { if (rc.intersects(visibleRect)) {
// draw space mark if (_showTabPositionMarks && i < firstNonSpace) {
if (ch == ' ') { if (spaceIndex % ts == 0) {
// space // draw mark
int sz = h / 6; buf.fillRect(Rect(rc.left, rc.top, rc.left + 1, rc.bottom), color);
if (sz < 1) }
sz = 1; if (ch == ' ')
rc.top += h / 2 - sz / 2; spaceIndex++;
rc.bottom = rc.top + sz; else if (ch == '\t')
rc.left += rc.width / 2 - sz / 2; spaceIndex = (spaceIndex + ts) / ts * ts;
rc.right = rc.left + sz; }
buf.fillRect(rc, color); if (_showWhiteSpaceMarks) {
} else if (ch == '\t') { // draw space mark
// tab if (ch == ' ') {
Point p1 = Point(rc.left + 1, rc.top + h / 2); // space
Point p2 = p1; int sz = h / 6;
p2.x = rc.right - 1; if (sz < 1)
int sz = h / 4; sz = 1;
if (sz < 2) rc.top += h / 2 - sz / 2;
sz = 2; rc.bottom = rc.top + sz;
if (sz > p2.x - p1.x) rc.left += rc.width / 2 - sz / 2;
sz = p2.x - p1.x; rc.right = rc.left + sz;
buf.drawLine(p1, p2, color); buf.fillRect(rc, color);
buf.drawLine(p2, Point(p2.x - sz, p2.y - sz), color); } else if (ch == '\t') {
buf.drawLine(p2, Point(p2.x - sz, p2.y + sz), color); // tab
Point p1 = Point(rc.left + 1, rc.top + h / 2);
Point p2 = p1;
p2.x = rc.right - 1;
int sz = h / 4;
if (sz < 2)
sz = 2;
if (sz > p2.x - p1.x)
sz = p2.x - p1.x;
buf.drawLine(p1, p2, color);
buf.drawLine(p2, Point(p2.x - sz, p2.y - sz), color);
buf.drawLine(p2, Point(p2.x - sz, p2.y + sz), color);
}
} }
} }
} }
@ -2761,7 +2780,7 @@ class EditBox : EditWidgetBase {
drawLineBackground(buf, _firstVisibleLine + i, lineRect, visibleRect); drawLineBackground(buf, _firstVisibleLine + i, lineRect, visibleRect);
if (!txt.length) if (!txt.length)
continue; continue;
if (_showWhiteSpaceMarks) if (_showWhiteSpaceMarks || _showTabPositionMarks)
drawWhiteSpaceMarks(buf, font, txt, tabSize, lineRect, visibleRect); drawWhiteSpaceMarks(buf, font, txt, tabSize, lineRect, visibleRect);
if (_leftPaneWidth > 0) { if (_leftPaneWidth > 0) {
Rect leftPaneRect = visibleRect; Rect leftPaneRect = visibleRect;