hide passwords in line edits

This commit is contained in:
Eduardo Bart 2011-08-30 11:25:08 -03:00
parent 9bae47a40c
commit b6b823aa9c
4 changed files with 29 additions and 6 deletions

View File

@ -6,3 +6,5 @@ LineEdit < UILineEdit
source: /core_ui/images/panel_flat.png
border: 1
PasswordLineEdit < LineEdit
text hidden: true

View File

@ -27,7 +27,7 @@ MainWindow
anchors.top: prev.bottom
margin.top: 8
LineEdit
PasswordLineEdit
id: accountPasswordLineEdit
text: 123456
anchors.left: parent.left

View File

@ -32,6 +32,7 @@ UILineEdit::UILineEdit()
m_cursorPos = 0;
m_startRenderPos = 0;
m_textHorizontalMargin = 3;
m_textHidden = false;
blinkCursor();
}
@ -69,7 +70,8 @@ void UILineEdit::render()
void UILineEdit::update()
{
int textLength = m_text.length();
std::string text = getDisplayedText();
int textLength = text.length();
// prevent glitches
if(m_rect.isEmpty())
@ -77,7 +79,7 @@ void UILineEdit::update()
// map glyphs positions
Size textBoxSize;
const std::vector<Point>& glyphsPositions = m_font->calculateGlyphsPositions(m_text, m_align, &textBoxSize);
const std::vector<Point>& glyphsPositions = m_font->calculateGlyphsPositions(text, m_align, &textBoxSize);
const Rect *glyphsTextureCoords = m_font->getGlyphsTextureCoords();
const Size *glyphsSize = m_font->getGlyphsSize();
int glyph;
@ -101,7 +103,7 @@ void UILineEdit::update()
{
Rect virtualRect(m_startInternalPos, m_rect.size() - Size(2*m_textHorizontalMargin, 0) ); // previous rendered virtual rect
int pos = m_cursorPos - 1; // element before cursor
glyph = (uchar)m_text[pos]; // glyph of the element before cursor
glyph = (uchar)text[pos]; // glyph of the element before cursor
Rect glyphRect(glyphsPositions[pos], glyphsSize[glyph]);
// if the cursor is not on the previous rendered virtual rect we need to update it
@ -113,7 +115,7 @@ void UILineEdit::update()
// find that glyph
for(pos = 0; pos < textLength; ++pos) {
glyph = (uchar)m_text[pos];
glyph = (uchar)text[pos];
glyphRect = Rect(glyphsPositions[pos], glyphsSize[glyph]);
glyphRect.setTop(std::max(glyphRect.top() - m_font->getTopMargin() - m_font->getGlyphSpacing().height(), 0));
glyphRect.setLeft(std::max(glyphRect.left() - m_font->getGlyphSpacing().width(), 0));
@ -153,7 +155,7 @@ void UILineEdit::update()
}
for(int i = 0; i < textLength; ++i) {
glyph = (uchar)m_text[i];
glyph = (uchar)text[i];
m_glyphsCoords[i].clear();
// skip invalid glyphs
@ -239,6 +241,12 @@ void UILineEdit::setText(const std::string& text)
}
}
void UILineEdit::setTextHidden(bool hidden)
{
m_textHidden = true;
update();
}
void UILineEdit::setAlign(Fw::AlignmentFlag align)
{
if(m_align != align) {
@ -355,6 +363,14 @@ int UILineEdit::getTextPos(Point pos)
return candidatePos;
}
std::string UILineEdit::getDisplayedText()
{
if(m_textHidden)
return std::string(m_text.length(), '*');
else
return m_text;
}
void UILineEdit::onStyleApply(const OTMLNodePtr& styleNode)
{
UIWidget::onStyleApply(styleNode);
@ -363,6 +379,8 @@ void UILineEdit::onStyleApply(const OTMLNodePtr& styleNode)
if(node->tag() == "text") {
setText(node->value());
setCursorPos(m_text.length());
} else if(node->tag() == "text hidden") {
setTextHidden(node->value<bool>());
}
}
}

View File

@ -35,6 +35,7 @@ public:
void update();
void setText(const std::string& text);
void setTextHidden(bool hidden);
void setAlign(Fw::AlignmentFlag align);
void setCursorPos(int pos);
void setCursorEnabled(bool enable = true);
@ -47,6 +48,7 @@ public:
void setFont(const FontPtr& font);
std::string getText() const { return m_text; }
std::string getDisplayedText();
int getTextPos(Point pos);
protected:
@ -67,6 +69,7 @@ private:
int m_startRenderPos;
int m_cursorTicks;
int m_textHorizontalMargin;
bool m_textHidden;
std::vector<Rect> m_glyphsCoords;
std::vector<Rect> m_glyphsTexCoords;