FreeRDP
Loading...
Searching...
No Matches
sdl_input_widget_pair_list.cpp
1
20#include <cassert>
21#include <algorithm>
22
23#include <winpr/cast.h>
24
25#include "../sdl_utils.hpp"
26#include "sdl_widget_list.hpp"
27#include "sdl_input_widget_pair_list.hpp"
28
29static const Uint32 vpadding = 5;
30
31SdlInputWidgetPairList::SdlInputWidgetPairList(const std::string& title,
32 const std::vector<std::string>& labels,
33 const std::vector<std::string>& initial,
34 const std::vector<Uint32>& flags, ssize_t selected)
35{
36 assert(labels.size() == initial.size());
37 assert(labels.size() == flags.size());
38 const std::vector<int> buttonids = { INPUT_BUTTON_ACCEPT, INPUT_BUTTON_CANCEL };
39 const std::vector<std::string> buttonlabels = { "accept", "cancel" };
40
41 const size_t widget_width = 300;
42 const size_t widget_heigth = 50;
43
44 const size_t total_width = widget_width + widget_width;
45 const size_t input_height = labels.size() * (widget_heigth + vpadding) + vpadding;
46 const size_t total_height = input_height + widget_heigth;
47 assert(total_width <= INT32_MAX);
48 assert(total_height <= INT32_MAX);
49
50 if (reset(title, total_width, total_height))
51 {
52 for (size_t x = 0; x < labels.size(); x++)
53 {
54 auto widget =
55 std::make_shared<SdlInputWidgetPair>(_renderer, labels.at(x), initial.at(x),
56 flags.at(x), x, widget_width, widget_heigth);
57 m_list.emplace_back(widget);
58 }
59
60 std::ignore = _buttons.populate(
61 _renderer, buttonlabels, buttonids, total_width, static_cast<Sint32>(input_height),
62 static_cast<Sint32>(widget_width), static_cast<Sint32>(widget_heigth));
63 _buttons.set_highlight(0);
64 m_currentActiveTextInput = selected;
65 }
66}
67
68ssize_t SdlInputWidgetPairList::next(ssize_t current)
69{
70 size_t iteration = 0;
71 auto val = static_cast<size_t>(current);
72
73 do
74 {
75 if (iteration >= m_list.size())
76 return -1;
77
78 if (iteration == 0)
79 {
80 if (current < 0)
81 val = 0;
82 else
83 val++;
84 }
85 else
86 val++;
87 iteration++;
88 val %= m_list.size();
89 } while (!valid(static_cast<ssize_t>(val)));
90 return static_cast<ssize_t>(val);
91}
92
93bool SdlInputWidgetPairList::valid(ssize_t current) const
94{
95 if (current < 0)
96 return false;
97 auto s = static_cast<size_t>(current);
98 if (s >= m_list.size())
99 return false;
100 return !m_list.at(s)->readonly();
101}
102
103std::shared_ptr<SdlInputWidgetPair> SdlInputWidgetPairList::get(ssize_t index)
104{
105 if (index < 0)
106 return nullptr;
107 auto s = static_cast<size_t>(index);
108 if (s >= m_list.size())
109 return nullptr;
110 return m_list.at(s);
111}
112
113SdlInputWidgetPairList::~SdlInputWidgetPairList()
114{
115 m_list.clear();
116 _buttons.clear();
117}
118
119bool SdlInputWidgetPairList::updateInternal()
120{
121 for (auto& btn : m_list)
122 {
123 if (!btn->update())
124 return false;
125 if (!btn->update())
126 return false;
127 }
128
129 return true;
130}
131
132ssize_t SdlInputWidgetPairList::get_index(const SDL_MouseButtonEvent& button)
133{
134 const auto x = button.x;
135 const auto y = button.y;
136 for (size_t i = 0; i < m_list.size(); i++)
137 {
138 auto& cur = m_list.at(i);
139 auto r = cur->input_rect();
140
141 if ((x >= r.x) && (x <= r.x + r.w) && (y >= r.y) && (y <= r.y + r.h))
142 return WINPR_ASSERTING_INT_CAST(ssize_t, i);
143 }
144 return -1;
145}
146
147int SdlInputWidgetPairList::run(std::vector<std::string>& result)
148{
149 int res = -1;
150 ssize_t LastActiveTextInput = -1;
151 m_currentActiveTextInput = next(m_currentActiveTextInput);
152
153 if (!_window || !_renderer)
154 return -2;
155
156 if (!SDL_StartTextInput(_window.get()))
157 return -3;
158
159 try
160 {
161 bool running = true;
162 while (running)
163 {
164 if (!update())
165 throw;
166
167 SDL_Event event = {};
168 if (!SDL_WaitEventTimeout(&event, 30))
169 continue;
170 do
171 {
172 switch (event.type)
173 {
174 case SDL_EVENT_KEY_UP:
175 {
176 switch (event.key.key)
177 {
178 case SDLK_BACKSPACE:
179 {
180 auto cur = get(m_currentActiveTextInput);
181 if (cur)
182 {
183 if ((event.key.mod & SDL_KMOD_CTRL) != 0)
184 {
185 if (!cur->set_str(""))
186 throw;
187 }
188 else
189 {
190 if (!cur->remove_str(1))
191 throw;
192 }
193 }
194 }
195 break;
196 case SDLK_TAB:
197 m_currentActiveTextInput = next(m_currentActiveTextInput);
198 break;
199 case SDLK_RETURN:
200 case SDLK_RETURN2:
201 case SDLK_KP_ENTER:
202 running = false;
203 res = INPUT_BUTTON_ACCEPT;
204 break;
205 case SDLK_ESCAPE:
206 running = false;
207 res = INPUT_BUTTON_CANCEL;
208 break;
209 case SDLK_V:
210 if ((event.key.mod & SDL_KMOD_CTRL) != 0)
211 {
212 auto cur = get(m_currentActiveTextInput);
213 if (cur)
214 {
215 auto text = SDL_GetClipboardText();
216 if (!cur->set_str(text))
217 throw;
218 }
219 }
220 break;
221 default:
222 break;
223 }
224 }
225 break;
226 case SDL_EVENT_TEXT_INPUT:
227 {
228 auto cur = get(m_currentActiveTextInput);
229 if (cur)
230 {
231 if (!cur->append_str(event.text.text))
232 throw;
233 }
234 }
235 break;
236 case SDL_EVENT_MOUSE_MOTION:
237 {
238 auto TextInputIndex = get_index(event.button);
239 for (auto& cur : m_list)
240 {
241 cur->set_mouseover(false);
242 }
243 if (TextInputIndex >= 0)
244 {
245 auto& cur = m_list.at(static_cast<size_t>(TextInputIndex));
246 cur->set_mouseover(true);
247 }
248
249 _buttons.set_mouseover(event.button.x, event.button.y);
250 }
251 break;
252 case SDL_EVENT_MOUSE_BUTTON_DOWN:
253 {
254 auto val = get_index(event.button);
255 if (valid(val))
256 m_currentActiveTextInput = val;
257
258 auto button = _buttons.get_selected(event.button);
259 if (button)
260 {
261 running = false;
262 if (button->id() == INPUT_BUTTON_CANCEL)
263 res = INPUT_BUTTON_CANCEL;
264 else
265 res = INPUT_BUTTON_ACCEPT;
266 }
267 }
268 break;
269 case SDL_EVENT_WINDOW_OCCLUDED:
270 case SDL_EVENT_WINDOW_MINIMIZED:
271 case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
272 case SDL_EVENT_TERMINATING:
273 case SDL_EVENT_WINDOW_DESTROYED:
274 case SDL_EVENT_QUIT:
275 res = INPUT_BUTTON_CANCEL;
276 running = false;
277 break;
278 default:
279 break;
280 }
281 } while (running && SDL_PollEvent(&event));
282
283 if (LastActiveTextInput != m_currentActiveTextInput)
284 {
285 LastActiveTextInput = m_currentActiveTextInput;
286 }
287
288 for (auto& cur : m_list)
289 {
290 if (!cur->set_highlight(false))
291 throw;
292 }
293 auto cur = get(m_currentActiveTextInput);
294 if (cur)
295 {
296 if (!cur->set_highlight(true))
297 throw;
298 }
299
300 auto rc = SDL_RenderPresent(_renderer.get());
301 if (!rc)
302 {
303 SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "[%s] SDL_RenderPresent failed with %s",
304 __func__, SDL_GetError());
305 }
306 }
307
308 for (auto& cur : m_list)
309 result.push_back(cur->value());
310 }
311 catch (...)
312 {
313 res = -2;
314 }
315
316 SDL_PumpEvents();
317 SDL_FlushEvents(SDL_EVENT_FIRST, SDL_EVENT_USER);
318
319 if (!SDL_StopTextInput(_window.get()))
320 return -4;
321
322 return res;
323}
324
325void SdlInputWidgetPairList::parent(SDL_Window* parent)
326{
327 if (!parent)
328 return;
329 SDL_SetWindowParent(_window.get(), parent);
330 SDL_SetWindowModal(_window.get(), true);
331}