FreeRDP
Loading...
Searching...
No Matches
SdlInputWidgetList Class Reference

Public Member Functions

 SdlInputWidgetList (const std::string &title, const std::vector< std::string > &labels, const std::vector< std::string > &initial, const std::vector< Uint32 > &flags)
 
 SdlInputWidgetList (const SdlInputWidgetList &other)=delete
 
 SdlInputWidgetList (SdlInputWidgetList &&other)=delete
 
SdlInputWidgetListoperator= (const SdlInputWidgetList &other)=delete
 
SdlInputWidgetListoperator= (SdlInputWidgetList &&other)=delete
 
int run (std::vector< std::string > &result)
 
 SdlInputWidgetList (const std::string &title, const std::vector< std::string > &labels, const std::vector< std::string > &initial, const std::vector< Uint32 > &flags)
 
 SdlInputWidgetList (const SdlInputWidgetList &other)=delete
 
 SdlInputWidgetList (SdlInputWidgetList &&other)=delete
 
SdlInputWidgetListoperator= (const SdlInputWidgetList &other)=delete
 
SdlInputWidgetListoperator= (SdlInputWidgetList &&other)=delete
 
int run (std::vector< std::string > &result)
 

Protected Member Functions

bool update (SDL_Renderer *renderer)
 
ssize_t get_index (const SDL_MouseButtonEvent &button)
 
bool update (SDL_Renderer *renderer)
 
ssize_t get_index (const SDL_MouseButtonEvent &button)
 

Detailed Description

Definition at line 10 of file SDL2/dialogs/sdl_input_widgets.hpp.

Constructor & Destructor Documentation

◆ SdlInputWidgetList()

SdlInputWidgetList::SdlInputWidgetList ( const std::string &  title,
const std::vector< std::string > &  labels,
const std::vector< std::string > &  initial,
const std::vector< Uint32 > &  flags 
)

Definition at line 10 of file SDL2/dialogs/sdl_input_widgets.cpp.

14 : _window(nullptr), _renderer(nullptr)
15{
16 assert(labels.size() == initial.size());
17 assert(labels.size() == flags.size());
18 const std::vector<int> buttonids = { INPUT_BUTTON_ACCEPT, INPUT_BUTTON_CANCEL };
19 const std::vector<std::string> buttonlabels = { "accept", "cancel" };
20
21 const size_t widget_width = 300;
22 const size_t widget_heigth = 50;
23
24 const size_t total_width = widget_width + widget_width;
25 const size_t input_height = labels.size() * (widget_heigth + vpadding) + vpadding;
26 const size_t total_height = input_height + widget_heigth;
27
28 assert(total_width <= INT32_MAX);
29 assert(total_height <= INT32_MAX);
30 Uint32 wflags = SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_MOUSE_FOCUS | SDL_WINDOW_INPUT_FOCUS;
31 auto rc =
32 SDL_CreateWindowAndRenderer(static_cast<int>(total_width), static_cast<int>(total_height),
33 wflags, &_window, &_renderer);
34 if (rc != 0)
35 widget_log_error(rc, "SDL_CreateWindowAndRenderer");
36 else
37 {
38 SDL_SetWindowTitle(_window, title.c_str());
39 for (size_t x = 0; x < labels.size(); x++)
40 _list.emplace_back(_renderer, labels[x], initial[x], flags[x], x, widget_width,
41 widget_heigth);
42
43 _buttons.populate(_renderer, buttonlabels, buttonids, total_width,
44 static_cast<Sint32>(input_height), static_cast<Sint32>(widget_width),
45 static_cast<Sint32>(widget_heigth));
46 _buttons.set_highlight(0);
47 }
48}

◆ ~SdlInputWidgetList()

SdlInputWidgetList::~SdlInputWidgetList ( )
virtual

Definition at line 95 of file SDL2/dialogs/sdl_input_widgets.cpp.

96{
97 _list.clear();
98 _buttons.clear();
99 SDL_DestroyRenderer(_renderer);
100 SDL_DestroyWindow(_window);
101}

Member Function Documentation

◆ get_index()

ssize_t SdlInputWidgetList::get_index ( const SDL_MouseButtonEvent &  button)
protected

Definition at line 116 of file SDL2/dialogs/sdl_input_widgets.cpp.

117{
118 const Sint32 x = button.x;
119 const Sint32 y = button.y;
120
121 assert(_list.size() <= std::numeric_limits<ssize_t>::max());
122 for (size_t i = 0; i < _list.size(); i++)
123 {
124 auto& cur = _list[i];
125 auto r = cur.input_rect();
126
127 if ((x >= r.x) && (x <= r.x + r.w) && (y >= r.y) && (y <= r.y + r.h))
128 return static_cast<ssize_t>(i);
129 }
130 return -1;
131}

◆ run()

int SdlInputWidgetList::run ( std::vector< std::string > &  result)

Definition at line 133 of file SDL2/dialogs/sdl_input_widgets.cpp.

134{
135 int res = -1;
136 ssize_t LastActiveTextInput = -1;
137 ssize_t CurrentActiveTextInput = next(-1);
138
139 if (!_window || !_renderer)
140 return -2;
141
142 try
143 {
144 bool running = true;
145 std::vector<SDL_Keycode> pressed;
146 while (running)
147 {
148 if (!clear_window(_renderer))
149 throw;
150
151 if (!update(_renderer))
152 throw;
153
154 if (!_buttons.update(_renderer))
155 throw;
156
157 SDL_Event event = {};
158 SDL_WaitEvent(&event);
159 switch (event.type)
160 {
161 case SDL_KEYUP:
162 {
163 auto it = std::remove(pressed.begin(), pressed.end(), event.key.keysym.sym);
164 pressed.erase(it, pressed.end());
165
166 switch (event.key.keysym.sym)
167 {
168 case SDLK_BACKSPACE:
169 {
170 auto cur = get(CurrentActiveTextInput);
171 if (cur)
172 {
173 if (!cur->remove_str(_renderer, 1))
174 throw;
175 }
176 }
177 break;
178 case SDLK_TAB:
179 CurrentActiveTextInput = next(CurrentActiveTextInput);
180 break;
181 case SDLK_RETURN:
182 case SDLK_RETURN2:
183 case SDLK_KP_ENTER:
184 running = false;
185 res = INPUT_BUTTON_ACCEPT;
186 break;
187 case SDLK_ESCAPE:
188 running = false;
189 res = INPUT_BUTTON_CANCEL;
190 break;
191 case SDLK_v:
192 if (pressed.size() == 2)
193 {
194 if ((pressed[0] == SDLK_LCTRL) || (pressed[0] == SDLK_RCTRL))
195 {
196 auto cur = get(CurrentActiveTextInput);
197 if (cur)
198 {
199 auto text = SDL_GetClipboardText();
200 cur->set_str(_renderer, text);
201 }
202 }
203 }
204 break;
205 default:
206 break;
207 }
208 }
209 break;
210 case SDL_KEYDOWN:
211 pressed.push_back(event.key.keysym.sym);
212 break;
213 case SDL_TEXTINPUT:
214 {
215 auto cur = get(CurrentActiveTextInput);
216 if (cur)
217 {
218 if (!cur->append_str(_renderer, event.text.text))
219 throw;
220 }
221 }
222 break;
223 case SDL_MOUSEMOTION:
224 {
225 auto TextInputIndex = get_index(event.button);
226 for (auto& cur : _list)
227 {
228 if (!cur.set_mouseover(_renderer, false))
229 throw;
230 }
231 if (TextInputIndex >= 0)
232 {
233 auto& cur = _list[static_cast<size_t>(TextInputIndex)];
234 if (!cur.set_mouseover(_renderer, true))
235 throw;
236 }
237
238 _buttons.set_mouseover(event.button.x, event.button.y);
239 }
240 break;
241 case SDL_MOUSEBUTTONDOWN:
242 {
243 auto val = get_index(event.button);
244 if (valid(val))
245 CurrentActiveTextInput = val;
246
247 auto button = _buttons.get_selected(event.button);
248 if (button)
249 {
250 running = false;
251 if (button->id() == INPUT_BUTTON_CANCEL)
252 res = INPUT_BUTTON_CANCEL;
253 else
254 res = INPUT_BUTTON_ACCEPT;
255 }
256 }
257 break;
258 case SDL_QUIT:
259 res = INPUT_BUTTON_CANCEL;
260 running = false;
261 break;
262 default:
263 break;
264 }
265
266 if (LastActiveTextInput != CurrentActiveTextInput)
267 {
268 if (CurrentActiveTextInput < 0)
269 SDL_StopTextInput();
270 else
271 SDL_StartTextInput();
272 LastActiveTextInput = CurrentActiveTextInput;
273 }
274
275 for (auto& cur : _list)
276 {
277 if (!cur.set_highlight(_renderer, false))
278 throw;
279 }
280 auto cur = get(CurrentActiveTextInput);
281 if (cur)
282 {
283 if (!cur->set_highlight(_renderer, true))
284 throw;
285 }
286
287 SDL_RenderPresent(_renderer);
288 }
289
290 for (auto& cur : _list)
291 result.push_back(cur.value());
292 }
293 catch (...)
294 {
295 res = -2;
296 }
297
298 return res;
299}

◆ update()

bool SdlInputWidgetList::update ( SDL_Renderer *  renderer)
protected

Definition at line 103 of file SDL2/dialogs/sdl_input_widgets.cpp.

104{
105 for (auto& btn : _list)
106 {
107 if (!btn.update_label(renderer))
108 return false;
109 if (!btn.update_input(renderer))
110 return false;
111 }
112
113 return _buttons.update(renderer);
114}

The documentation for this class was generated from the following files: