FifeGUI 0.3.0
A C++ GUI library designed for games.
passwordfield.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
2// SPDX-FileCopyrightText: 2004 - 2008 Olof Naessén and Per Larsson
3// SPDX-FileCopyrightText: 2013 - 2026 Fifengine contributors
4
5// Corresponding header include
6#include <fifechan/widgets/passwordfield.hpp>
7
8// Standard library includes
9#include <cassert>
10#include <string>
11
12// Project headers (subdirs before local)
13#include "fifechan/key.hpp"
14#include "fifechan/text.hpp"
15#include "fifechan/utf8stringeditor.hpp"
16
17namespace fcn
18{
19 PasswordField::PasswordField(std::string const & text) : TextField(text), mActualText(new Text)
20 {
21 assert("mActualText must not be null after allocation" && mActualText != nullptr);
22 setText(text);
23 }
24
25 PasswordField::~PasswordField()
26 {
27 delete mActualText;
28 }
29
31 {
32 Key const key = keyEvent.getKey();
33
34 if (key.getValue() == fcn::Key::LEFT && getCaretPosition() > 0) {
35 mText->setCaretPosition(mText->getCaretPosition() - 1);
36 setActualTextCaretPosition(
37 fcn::UTF8StringEditor::prevChar(getText(), static_cast<int>(getActualTextCaretPosition())));
38 } else if (key.getValue() == fcn::Key::RIGHT && getCaretPosition() < getText().size()) {
39 mText->setCaretPosition(mText->getCaretPosition() + 1);
40 setActualTextCaretPosition(
41 fcn::UTF8StringEditor::nextChar(getText(), static_cast<int>(getActualTextCaretPosition())));
42 } else if (
43 mEditable && key.getValue() == fcn::Key::KEY_DELETE && getCaretPosition() < getText().size() &&
44 mText->getNumberOfRows() > 0) {
45 mText->remove(1);
46 setActualTextCaretPosition(
48 mActualText->getRow(0), static_cast<int>(getActualTextCaretPosition())));
49 } else if (
50 mEditable && key.getValue() == fcn::Key::BACKSPACE && getCaretPosition() > 0 &&
51 mText->getNumberOfRows() > 0) {
52 mText->remove(-1);
53 setActualTextCaretPosition(
55 mActualText->getRow(0), static_cast<int>(getActualTextCaretPosition())));
56 setActualTextCaretPosition(
58 mActualText->getRow(0), static_cast<int>(getActualTextCaretPosition())));
59 } else if (key.getValue() == fcn::Key::KEY_RETURN) {
61 } else if (key.getValue() == fcn::Key::HOME) {
62 mText->setCaretColumn(0);
63 setActualTextCaretPosition(0);
64 } else if (key.getValue() == fcn::Key::END) {
65 mText->setCaretColumn(mText->getNumberOfCharacters(0));
66 setActualTextCaretPosition(getText().size());
67 } else if (
68 // Add character to text, if key is a ASCII character
69 // or is greater than 8bits long and the character is not
70 // the tab key.
71 (key.isCharacter() || (key.getValue() > 255 && mText->getNumberOfRows() > 0)) &&
72 key.getValue() != fcn::Key::TAB) {
73 mText->insert('*');
74 setActualTextCaretPosition(
76 mActualText->getRow(0), getActualTextCaretPosition(), key.getValue()));
77 }
78
79 if (key.getValue() != fcn::Key::TAB) {
80 // consume all characters except TAB which is needed
81 // for traversing through widgets in a container.
82 keyEvent.consume();
83 }
84
85 fixScroll();
86 }
87
88 void PasswordField::setText(std::string const & text)
89 {
90 assert("mText must not be null" && mText != nullptr);
91 assert("mActualText must not be null" && mActualText != nullptr);
92
93 std::string asterisks;
94 asterisks.assign(text.size(), '*');
95
96 mText->setContent(asterisks);
97 mActualText->setContent(text);
98 }
99
100 std::string PasswordField::getText() const
101 {
102 return mActualText->getContent();
103 }
104
105 void PasswordField::setActualTextCaretPosition(unsigned int position)
106 {
107 mActualText->setCaretPosition(position);
108 }
109
110 unsigned int PasswordField::getActualTextCaretPosition() const
111 {
112 return mActualText->getCaretPosition();
113 }
114}; // namespace fcn
void consume()
Marks this event as consumed.
Represents a key event.
Definition keyevent.hpp:26
Key const & getKey() const
Gets the key of the event.
Definition keyevent.cpp:48
std::string getText() const override
Gets the text of the text field.
PasswordField(std::string const &text="")
Constructor.
void keyPressed(KeyEvent &keyEvent) override
Called if a key is pressed when the widget has keyboard focus.
void setText(std::string const &text) override
Sets the text of the text field.
bool mEditable
True if the text field is editable, false otherwise.
unsigned int getCaretPosition() const
Gets the caret position.
void fixScroll()
Ensures the caret remains visible by adjusting horizontal scroll.
Text * mText
Holds the text of the text field.
Helper class for text manipulation within widgets.
Definition text.hpp:32
virtual int getCaretPosition() const
Gets the caret position.
Definition text.cpp:250
virtual void setCaretPosition(int position)
Sets the caret position.
Definition text.cpp:255
static int insertChar(std::string &text, int byteOffset, int ch)
Insert a character at specified byte offset.
static int eraseChar(std::string &text, int byteOffset)
Erase character at specified byte offset.
static int nextChar(std::string const &text, int byteOffset)
Returns byte offset of the next character.
static int prevChar(std::string const &text, int byteOffset)
Returns byte offset of the previous character.
void distributeActionEvent()
Distributes an action event to all action listeners of the widget.
Definition widget.cpp:1291
Used replacement tokens by configure_file():