FifeGUI 0.2.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#include <fifechan/widgets/passwordfield.hpp>
6
7#include <fifechan/key.hpp>
8#include <fifechan/text.hpp>
9#include <fifechan/utf8stringeditor.hpp>
10
11#include <string>
12
13namespace fcn
14{
15 PasswordField::PasswordField(std::string const & text) : TextField(text), mActualText(new Text)
16 {
17 setText(text);
18 }
19
20 PasswordField::~PasswordField()
21 {
22 delete mActualText;
23 }
24
26 {
27 Key const key = keyEvent.getKey();
28
29 if (key.getValue() == Key::Left && getCaretPosition() > 0) {
30 mText->setCaretPosition(mText->getCaretPosition() - 1);
31 setActualTextCaretPosition(
32 fcn::UTF8StringEditor::prevChar(getText(), static_cast<int>(getActualTextCaretPosition())));
33 } else if (key.getValue() == Key::Right && getCaretPosition() < getText().size()) {
34 mText->setCaretPosition(mText->getCaretPosition() + 1);
35 setActualTextCaretPosition(
36 fcn::UTF8StringEditor::nextChar(getText(), static_cast<int>(getActualTextCaretPosition())));
37 } else if (
38 mEditable && key.getValue() == Key::Delete && getCaretPosition() < getText().size() &&
39 mText->getNumberOfRows() > 0) {
40 mText->remove(1);
41 setActualTextCaretPosition(
43 mActualText->getRow(0), static_cast<int>(getActualTextCaretPosition())));
44 } else if (
45 mEditable && key.getValue() == Key::Backspace && getCaretPosition() > 0 && mText->getNumberOfRows() > 0) {
46 mText->remove(-1);
47 setActualTextCaretPosition(
49 mActualText->getRow(0), static_cast<int>(getActualTextCaretPosition())));
50 setActualTextCaretPosition(
52 mActualText->getRow(0), static_cast<int>(getActualTextCaretPosition())));
53 } else if (key.getValue() == Key::Enter) {
55 } else if (key.getValue() == Key::Home) {
56 mText->setCaretColumn(0);
57 setActualTextCaretPosition(0);
58 } else if (key.getValue() == Key::End) {
59 mText->setCaretColumn(mText->getNumberOfCharacters(0));
60 setActualTextCaretPosition(getText().size());
61 } else if (
62 // Add character to text, if key is a ASCII character
63 // or is greater than 8bits long and the character is not
64 // the tab key.
65 (key.isCharacter() || (key.getValue() > 255 && mText->getNumberOfRows() > 0)) &&
66 key.getValue() != Key::Tab) {
67 mText->insert('*');
68 setActualTextCaretPosition(
70 mActualText->getRow(0), getActualTextCaretPosition(), key.getValue()));
71 }
72
73 if (key.getValue() != Key::Tab) {
74 // consume all characters except TAB which is needed
75 // for traversing through widgets in a container.
76 keyEvent.consume();
77 }
78
79 fixScroll();
80 }
81
82 void PasswordField::setText(std::string const & text)
83 {
84 std::string asterisks;
85 asterisks.assign(text.size(), '*');
86
87 mText->setContent(asterisks);
88 mActualText->setContent(text);
89 }
90
91 std::string PasswordField::getText() const
92 {
93 return mActualText->getContent();
94 }
95
96 void PasswordField::setActualTextCaretPosition(unsigned int position)
97 {
98 mActualText->setCaretPosition(position);
99 }
100
101 unsigned int PasswordField::getActualTextCaretPosition() const
102 {
103 return mActualText->getCaretPosition();
104 }
105}; // namespace fcn
void consume()
Marks the event as consumed.
Represents a key event.
Definition keyevent.hpp:22
Key const & getKey() const
Gets the key of the event.
Definition keyevent.cpp:39
Represents a keyboard key or character code.
Definition key.hpp:20
int getValue() const
Gets the value of the key.
Definition key.cpp:28
bool isCharacter() const
Checks if a key is a character.
Definition key.cpp:11
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()
Scrolls the text horizontally so that the caret shows if needed.
Text * mText
Holds the text of the text field.
Helper class for text manipulation within widgets.
Definition text.hpp:28
virtual int getCaretPosition() const
Gets the caret position.
Definition text.cpp:220
virtual void setCaretPosition(int position)
Sets the caret position.
Definition text.cpp:225
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:1113