FreeRDP
Loading...
Searching...
No Matches
sdl_input_widget_pair.cpp
1
20#include "sdl_input_widget_pair.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
34SdlInputWidgetPair::SdlInputWidgetPair(std::shared_ptr<SDL_Renderer>& renderer,
35 const std::string& label, const std::string& initial,
36 Uint32 flags, size_t offset, size_t width, size_t height)
37 : _flags(flags), _label(renderer, { 0, static_cast<float>(offset * (height + _vpadding)),
38 static_cast<float>(width), static_cast<float>(height) }),
39 _input(renderer, { static_cast<float>(width + _hpadding),
40 static_cast<float>(offset * (height + _vpadding)),
41 static_cast<float>(width), static_cast<float>(height) })
42{
43 _label.update_text(label);
44 update_input_text(initial);
45}
46
48
49bool SdlInputWidgetPair::set_mouseover(bool mouseOver)
50{
51 if (readonly())
52 return true;
53 return _input.mouseover(mouseOver);
54}
55
56bool SdlInputWidgetPair::set_highlight(bool highlight)
57{
58 if (readonly())
59 return true;
60 return _input.highlight(highlight);
61}
62
63bool SdlInputWidgetPair::set_str(const std::string& text)
64{
65 if (readonly())
66 return true;
67 return update_input_text(text);
68}
69
70bool SdlInputWidgetPair::remove_str(size_t count)
71{
72 if (readonly())
73 return true;
74
75 auto text = _text;
76 if (text.empty())
77 return true;
78
79 auto newsize = text.size() - std::min<size_t>(text.size(), count);
80 return update_input_text(text.substr(0, newsize));
81}
82
83bool SdlInputWidgetPair::append_str(const std::string& text)
84{
85 if (readonly())
86 return true;
87
88 auto itext = _text;
89 itext.append(text);
90 return update_input_text(itext);
91}
92
93const SDL_FRect& SdlInputWidgetPair::input_rect() const
94{
95 return _input.rect();
96}
97
98std::string SdlInputWidgetPair::value() const
99{
100 return _text;
101}
102
103bool SdlInputWidgetPair::readonly() const
104{
105 return (_flags & SDL_INPUT_READONLY) != 0;
106}
107
108bool SdlInputWidgetPair::update()
109{
110 // TODO: Draw the pair area
111 if (!_label.update())
112 return false;
113 if (!_input.update())
114 return false;
115 return true;
116}
117
118bool SdlInputWidgetPair::update_input_text(const std::string& txt)
119{
120 _text = txt;
121 auto text = txt;
122
123 if (_flags & SDL_INPUT_MASK)
124 {
125 std::fill(text.begin(), text.end(), '*');
126 }
127
128 return _input.update_text(text);
129}
SdlInputWidgetPair(std::shared_ptr< SDL_Renderer > &renderer, const std::string &label, const std::string &initial, Uint32 flags, size_t offset, size_t width, size_t height)