From 5fc7825c82ed555f4f4ada97eb33843d2efff1ae Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Tue, 13 Sep 2016 15:08:10 +0300 Subject: [PATCH] cursor type support in console apps --- src/dlangui/graphics/resources.d | 2 + src/dlangui/platforms/common/platform.d | 6 +++ src/dlangui/platforms/console/consoleapp.d | 10 +++- src/dlangui/platforms/console/dconsole.d | 54 ++++++++++++++++++++++ src/dlangui/widgets/editors.d | 3 +- 5 files changed, 72 insertions(+), 3 deletions(-) diff --git a/src/dlangui/graphics/resources.d b/src/dlangui/graphics/resources.d index 165d202d..a7a18310 100644 --- a/src/dlangui/graphics/resources.d +++ b/src/dlangui/graphics/resources.d @@ -1437,6 +1437,8 @@ class DrawableCache { foreach(string path; _resourcePaths) { string fn; fn = checkFileName(path, id, ".xml"); + if (fn is null && BACKEND_CONSOLE) + fn = checkFileName(path, id, ".tim"); if (fn is null) fn = checkFileName(path, id, ".png"); if (fn is null) diff --git a/src/dlangui/platforms/common/platform.d b/src/dlangui/platforms/common/platform.d index f33756cc..42959d4e 100644 --- a/src/dlangui/platforms/common/platform.d +++ b/src/dlangui/platforms/common/platform.d @@ -214,9 +214,15 @@ class Window : CustomEventTarget { } protected Rect _caretRect; + /// blinking caret position (empty rect if no blinking caret) @property void caretRect(Rect rc) { _caretRect = rc; } @property Rect caretRect() { return _caretRect; } + protected bool _caretReplace; + /// blinking caret is in Replace mode if true, insert mode if false + @property void caretReplace(bool flg) { _caretReplace = flg; } + @property bool caretReplace() { return _caretReplace; } + // Abstract methods : override in platform implementatino /// show window diff --git a/src/dlangui/platforms/console/consoleapp.d b/src/dlangui/platforms/console/consoleapp.d index e9be2c54..76497cb1 100644 --- a/src/dlangui/platforms/console/consoleapp.d +++ b/src/dlangui/platforms/console/consoleapp.d @@ -142,8 +142,14 @@ class ConsolePlatform : Platform { _drawBuf.fillRect(Rect(0, 0, w.width, w.height), w.backgroundColor); w.onDraw(_drawBuf); auto caretRect = w.caretRect; - if ((w is activeWindow) && !caretRect.empty) - _drawBuf.console.setCursor(caretRect.left, caretRect.top); + if ((w is activeWindow) && !caretRect.empty) { + if (!caretRect.empty) { + _drawBuf.console.setCursor(caretRect.left, caretRect.top); + _drawBuf.console.setCursorType(w.caretReplace ? ConsoleCursorType.Replace : ConsoleCursorType.Insert); + } else { + _drawBuf.console.setCursorType(ConsoleCursorType.Invisible); + } + } } } _needRedraw = false; diff --git a/src/dlangui/platforms/console/dconsole.d b/src/dlangui/platforms/console/dconsole.d index 213da25a..9544f095 100644 --- a/src/dlangui/platforms/console/dconsole.d +++ b/src/dlangui/platforms/console/dconsole.d @@ -15,6 +15,13 @@ version(Windows) { import dlangui.core.signals; import dlangui.core.events; +/// console cursor type +enum ConsoleCursorType { + Invisible, /// hidden + Insert, /// insert (usually underscore) + Replace, /// replace (usually square) +} + enum TextColor : ubyte { BLACK, // 0 BLUE, @@ -441,6 +448,7 @@ class Console { _cursorX = _batchBuf.cursorX; _cursorY = _batchBuf.cursorY; rawSetCursor(_cursorX, _cursorY); + rawSetCursorType(_cursorType); } _batchBuf.clear(ConsoleChar.init); } @@ -477,6 +485,52 @@ class Console { } } + private ConsoleCursorType _rawCursorType = ConsoleCursorType.Insert; + protected void rawSetCursorType(ConsoleCursorType type) { + if (_rawCursorType == type) + return; + version(Windows) { + CONSOLE_CURSOR_INFO ci; + switch(type) { + default: + case ConsoleCursorType.Insert: + ci.dwSize = 10; + ci.bVisible = TRUE; + break; + case ConsoleCursorType.Replace: + ci.dwSize = 100; + ci.bVisible = TRUE; + break; + case ConsoleCursorType.Invisible: + ci.dwSize = 10; + ci.bVisible = FALSE; + break; + } + SetConsoleCursorInfo(_hstdout, &ci); + } else { + switch(type) { + default: + case ConsoleCursorType.Insert: + rawWrite("\x1b[?25h"); + break; + case ConsoleCursorType.Replace: + rawWrite("\x1b[?25h"); + break; + case ConsoleCursorType.Invisible: + rawWrite("\x1b[?25l"); + break; + } + } + _rawCursorType = type; + } + + private ConsoleCursorType _cursorType = ConsoleCursorType.Insert; + void setCursorType(ConsoleCursorType type) { + _cursorType = type; + if (!_batchMode) + rawSetCursorType(_cursorType); + } + protected void rawWriteTextAt(int x, int y, uint attr, dstring str) { if (!str.length) return; diff --git a/src/dlangui/widgets/editors.d b/src/dlangui/widgets/editors.d index e7b28c2d..a8eb80b3 100644 --- a/src/dlangui/widgets/editors.d +++ b/src/dlangui/widgets/editors.d @@ -944,6 +944,7 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction if (window) { static if (BACKEND_CONSOLE) { window.caretRect = caretRect; + window.caretReplace = _replaceMode; } else { long ts = currentTimeMillis; if (_caretTimerId) { @@ -1058,7 +1059,7 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction Rect caretRc = caretRect(); if (caretRc.intersects(_clientRect)) { //caretRc.left++; - if (_replaceMode) + if (_replaceMode && BACKEND_GUI) buf.fillRect(caretRc, _caretColorReplace); buf.drawLine(Point(caretRc.left, caretRc.bottom), Point(caretRc.left, caretRc.top), _caretColor); }