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#include <algorithm>
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(std::shared_ptr<SDL_Renderer>& renderer, std::string label,
44 std::string initial, Uint32 flags, size_t offset, size_t width,
45 size_t height)
46 : _flags(flags), _text(std::move(initial)), _text_label(std::move(label)),
47 _label(renderer,
48 { 0, static_cast<float>(offset * (height + vpadding)), static_cast<float>(width),
49 static_cast<float>(height) },
50 false),
51 _input(renderer,
52 { static_cast<float>(width + hpadding),
53 static_cast<float>(offset * (height + vpadding)), static_cast<float>(width),
54 static_cast<float>(height) },
55 true),
56 _highlight(false), _mouseover(false)
57{
58}
59
60SdlInputWidget::SdlInputWidget(SdlInputWidget&& other) noexcept
61 : _flags(other._flags), _text(std::move(other._text)),
62 _text_label(std::move(other._text_label)), _label(std::move(other._label)),
63 _input(std::move(other._input)), _highlight(other._highlight), _mouseover(other._mouseover)
64{
65}
66
67bool SdlInputWidget::fill_label(std::shared_ptr<SDL_Renderer>& renderer, SDL_Color color)
68{
69 if (!_label.fill(renderer, color))
70 return false;
71 return _label.update_text(renderer, _text_label, labelfontcolor);
72}
73
74bool SdlInputWidget::update_label(std::shared_ptr<SDL_Renderer>& renderer)
75{
76 return _label.update_text(renderer, _text_label, labelfontcolor, labelbackgroundcolor);
77}
78
79bool SdlInputWidget::set_mouseover(std::shared_ptr<SDL_Renderer>& renderer, bool mouseOver)
80{
81 if (readonly())
82 return true;
83 _mouseover = mouseOver;
84 return update_input(renderer);
85}
86
87bool SdlInputWidget::set_highlight(std::shared_ptr<SDL_Renderer>& renderer, bool highlight)
88{
89 if (readonly())
90 return true;
91 _highlight = highlight;
92 return update_input(renderer);
93}
94
95bool SdlInputWidget::update_input(std::shared_ptr<SDL_Renderer>& renderer) const
96{
97 std::vector<SDL_Color> colors = { inputbackgroundcolor };
98 if (_highlight)
99 colors.push_back(inputhighlightcolor);
100 if (_mouseover)
101 colors.push_back(inputmouseovercolor);
102
103 if (!_input.fill(renderer, colors))
104 return false;
105 return update_input(renderer, inputfontcolor);
106}
107
108bool SdlInputWidget::set_str(std::shared_ptr<SDL_Renderer>& renderer, const std::string& text)
109{
110 if (readonly())
111 return true;
112 _text = text;
113 return update_input(renderer);
114}
115
116bool SdlInputWidget::remove_str(std::shared_ptr<SDL_Renderer>& renderer, size_t count)
117{
118 if (readonly())
119 return true;
120
121 assert(renderer);
122 if (_text.empty())
123 return true;
124
125 auto newsize = _text.size() - std::min<size_t>(_text.size(), count);
126 _text = _text.substr(0, newsize);
127 return update_input(renderer);
128}
129
130bool SdlInputWidget::append_str(std::shared_ptr<SDL_Renderer>& renderer, const std::string& text)
131{
132 assert(renderer);
133 if (readonly())
134 return true;
135
136 _text.append(text);
137 return update_input(renderer);
138}
139
140const SDL_FRect& SdlInputWidget::input_rect() const
141{
142 return _input.rect();
143}
144
145std::string SdlInputWidget::value() const
146{
147 return _text;
148}
149
150bool SdlInputWidget::readonly() const
151{
152 return (_flags & SDL_INPUT_READONLY) != 0;
153}
154
155bool SdlInputWidget::update_input(std::shared_ptr<SDL_Renderer>& renderer, SDL_Color fgcolor) const
156{
157 std::string text = _text;
158 if (!text.empty())
159 {
160 if (_flags & SDL_INPUT_MASK)
161 {
162 for (char& x : text)
163 x = '*';
164 }
165 }
166
167 return _input.update_text(renderer, text, fgcolor);
168}