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