From f4741bf2975b9a3930897a704fc3ac73fd0e7d9e Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Wed, 27 Jul 2016 08:36:54 +0300 Subject: [PATCH] close #282 --- examples/example1/src/example1.d | 1 + src/dlangui/widgets/editors.d | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/examples/example1/src/example1.d b/examples/example1/src/example1.d index d285c214..8139a0f4 100644 --- a/examples/example1/src/example1.d +++ b/examples/example1/src/example1.d @@ -433,6 +433,7 @@ extern (C) int UIAppMain(string[] args) { hlayout.addChild((new TextWidget()).text("in horizontal layout")); hlayout.addChild((new ImageWidget()).drawableId("exit").padding(Rect(5,5,5,5)).alignment(Align.Center)); hlayout.addChild((new EditLine("editline", "Some text to edit"d)).popupMenu(editPopupItem).alignment(Align.Center).layoutWidth(FILL_PARENT)); + hlayout.addChild((new EditLine("passwd", "Password"d)).passwordChar('*').popupMenu(editPopupItem).alignment(Align.Center).layoutWidth(FILL_PARENT)); //hlayout.addChild((new Button()).text(">>")); //.textColor(0x40FF4000) hlayout.backgroundColor = 0x8080C0; layout.addChild(hlayout); diff --git a/src/dlangui/widgets/editors.d b/src/dlangui/widgets/editors.d index bd8c08cf..80837517 100644 --- a/src/dlangui/widgets/editors.d +++ b/src/dlangui/widgets/editors.d @@ -1832,6 +1832,17 @@ class EditLine : EditWidgetBase { protected int[] _measuredTextWidths; protected Point _measuredTextSize; + protected dchar _passwordChar = 0; + /// password character - 0 for normal editor, some character, e.g. '*' to hide text by replacing all characters with this char + @property dchar passwordChar() { return _passwordChar; } + @property EditLine passwordChar(dchar ch) { + if (_passwordChar != ch) { + _passwordChar = ch; + requestLayout(); + } + return this; + } + override protected Rect textPosToClient(TextPosition p) { Rect res; res.bottom = _clientRect.height; @@ -1879,10 +1890,19 @@ class EditLine : EditWidgetBase { updateScrollBars(); } + protected dstring applyPasswordChar(dstring s) { + if (!_passwordChar || s.length == 0) + return s; + dchar[] ss = s.dup; + foreach(ref ch; ss) + ch = _passwordChar; + return cast(dstring)ss; + } + override protected Point measureVisibleText() { FontRef font = font(); //Point sz = font.textSize(text); - _measuredText = text; + _measuredText = applyPasswordChar(text); _measuredTextWidths.length = _measuredText.length; int charsMeasured = font.measureText(_measuredText, _measuredTextWidths, int.max, tabSize); _measuredTextSize.x = charsMeasured > 0 ? _measuredTextWidths[charsMeasured - 1]: 0; @@ -1985,7 +2005,7 @@ class EditLine : EditWidgetBase { applyPadding(rc); auto saver = ClipRectSaver(buf, rc, alpha); FontRef font = font(); - dstring txt = text; + dstring txt = applyPasswordChar(text); Point sz = font.textSize(txt); //applyAlign(rc, sz); Rect lineRect = _clientRect;