FreeRDP
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
SDL3/dialogs/sdl_input.cpp
1
20#include "sdl_input.hpp"
21
22#include <cassert>
23
24#include <string>
25#include <utility>
26
27#include <SDL3/SDL.h>
28#include <SDL3_ttf/SDL_ttf.h>
29
30#include "sdl_widget.hpp"
31#include "sdl_button.hpp"
32#include "sdl_buttons.hpp"
33
34static const SDL_Color inputbackgroundcolor = { 0x56, 0x56, 0x56, 0xff };
35static const SDL_Color inputhighlightcolor = { 0x80, 0, 0, 0x60 };
36static const SDL_Color inputmouseovercolor = { 0, 0x80, 0, 0x60 };
37static const SDL_Color inputfontcolor = { 0xd1, 0xcf, 0xcd, 0xff };
38static const SDL_Color labelbackgroundcolor = { 0x56, 0x56, 0x56, 0xff };
39static const SDL_Color labelfontcolor = { 0xd1, 0xcf, 0xcd, 0xff };
40static const Uint32 vpadding = 5;
41static const Uint32 hpadding = 10;
42
43SdlInputWidget::SdlInputWidget(SDL_Renderer* renderer, std::string label, std::string initial,
44 Uint32 flags, size_t offset, size_t width, size_t height)
45 : _flags(flags), _text(std::move(initial)), _text_label(std::move(label)),
46 _label(renderer,
47 { 0, static_cast<float>(offset * (height + vpadding)), static_cast<float>(width),
48 static_cast<float>(height) },
49 false),
50 _input(renderer,
51 { static_cast<float>(width + hpadding),
52 static_cast<float>(offset * (height + vpadding)), static_cast<float>(width),
53 static_cast<float>(height) },
54 true),
55 _highlight(false), _mouseover(false)
56{
57}
58
59SdlInputWidget::SdlInputWidget(SdlInputWidget&& other) noexcept
60 : _flags(other._flags), _text(std::move(other._text)),
61 _text_label(std::move(other._text_label)), _label(std::move(other._label)),
62 _input(std::move(other._input)), _highlight(other._highlight), _mouseover(other._mouseover)
63{
64}
65
66bool SdlInputWidget::fill_label(SDL_Renderer* renderer, SDL_Color color)
67{
68 if (!_label.fill(renderer, color))
69 return false;
70 return _label.update_text(renderer, _text_label, labelfontcolor);
71}
72
73bool SdlInputWidget::update_label(SDL_Renderer* renderer)
74{
75 return _label.update_text(renderer, _text_label, labelfontcolor, labelbackgroundcolor);
76}
77
78bool SdlInputWidget::set_mouseover(SDL_Renderer* renderer, bool mouseOver)
79{
80 if (readonly())
81 return true;
82 _mouseover = mouseOver;
83 return update_input(renderer);
84}
85
86bool SdlInputWidget::set_highlight(SDL_Renderer* renderer, bool highlight)
87{
88 if (readonly())
89 return true;
90 _highlight = highlight;
91 return update_input(renderer);
92}
93
94bool SdlInputWidget::update_input(SDL_Renderer* renderer)
95{
96 std::vector<SDL_Color> colors = { inputbackgroundcolor };
97 if (_highlight)
98 colors.push_back(inputhighlightcolor);
99 if (_mouseover)
100 colors.push_back(inputmouseovercolor);
101
102 if (!_input.fill(renderer, colors))
103 return false;
104 return update_input(renderer, inputfontcolor);
105}
106
107bool SdlInputWidget::resize_input(size_t size)
108{
109 _text.resize(size);
110
111 return true;
112}
113
114bool SdlInputWidget::set_str(SDL_Renderer* renderer, const std::string& text)
115{
116 if (readonly())
117 return true;
118 _text = text;
119 if (!resize_input(_text.size()))
120 return false;
121 return update_input(renderer);
122}
123
124bool SdlInputWidget::remove_str(SDL_Renderer* renderer, size_t count)
125{
126 if (readonly())
127 return true;
128
129 assert(renderer);
130 if (_text.empty())
131 return true;
132
133 if (!resize_input(_text.size() - count))
134 return false;
135 return update_input(renderer);
136}
137
138bool SdlInputWidget::append_str(SDL_Renderer* renderer, const std::string& text)
139{
140 assert(renderer);
141 if (readonly())
142 return true;
143
144 _text.append(text);
145 if (!resize_input(_text.size()))
146 return false;
147 return update_input(renderer);
148}
149
150const SDL_FRect& SdlInputWidget::input_rect() const
151{
152 return _input.rect();
153}
154
155std::string SdlInputWidget::value() const
156{
157 return _text;
158}
159
160bool SdlInputWidget::readonly() const
161{
162 return (_flags & SDL_INPUT_READONLY) != 0;
163}
164
165bool SdlInputWidget::update_input(SDL_Renderer* renderer, SDL_Color fgcolor)
166{
167 std::string text = _text;
168 if (!text.empty())
169 {
170 if (_flags & SDL_INPUT_MASK)
171 {
172 for (char& x : text)
173 x = '*';
174 }
175 }
176
177 return _input.update_text(renderer, text, fgcolor);
178}