From b6b823aa9c2e4e2ef4c95a3124b501d6119df8ed Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Tue, 30 Aug 2011 11:25:08 -0300 Subject: [PATCH] hide passwords in line edits --- modules/core_ui/styles/lineedits.otui | 2 ++ modules/mainmenu/ui/entergamewindow.otui | 2 +- src/framework/ui/uilineedit.cpp | 28 +++++++++++++++++++----- src/framework/ui/uilineedit.h | 3 +++ 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/modules/core_ui/styles/lineedits.otui b/modules/core_ui/styles/lineedits.otui index 4c484b2b..813e0a9b 100644 --- a/modules/core_ui/styles/lineedits.otui +++ b/modules/core_ui/styles/lineedits.otui @@ -6,3 +6,5 @@ LineEdit < UILineEdit source: /core_ui/images/panel_flat.png border: 1 +PasswordLineEdit < LineEdit + text hidden: true diff --git a/modules/mainmenu/ui/entergamewindow.otui b/modules/mainmenu/ui/entergamewindow.otui index 89d45748..144f79f5 100644 --- a/modules/mainmenu/ui/entergamewindow.otui +++ b/modules/mainmenu/ui/entergamewindow.otui @@ -27,7 +27,7 @@ MainWindow anchors.top: prev.bottom margin.top: 8 - LineEdit + PasswordLineEdit id: accountPasswordLineEdit text: 123456 anchors.left: parent.left diff --git a/src/framework/ui/uilineedit.cpp b/src/framework/ui/uilineedit.cpp index 89cb0cba..ad2b523f 100644 --- a/src/framework/ui/uilineedit.cpp +++ b/src/framework/ui/uilineedit.cpp @@ -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& glyphsPositions = m_font->calculateGlyphsPositions(m_text, m_align, &textBoxSize); + const std::vector& 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()); } } } diff --git a/src/framework/ui/uilineedit.h b/src/framework/ui/uilineedit.h index c2bf9c53..b1c42d18 100644 --- a/src/framework/ui/uilineedit.h +++ b/src/framework/ui/uilineedit.h @@ -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 m_glyphsCoords; std::vector m_glyphsTexCoords;