4 #include <winpr/cast.h>
6 #include "sdl_input_widgets.hpp"
8 static const Uint32 vpadding = 5;
10 SdlInputWidgetList::SdlInputWidgetList(
const std::string& title,
11 const std::vector<std::string>& labels,
12 const std::vector<std::string>& initial,
13 const std::vector<Uint32>& flags)
14 : _window(nullptr), _renderer(nullptr)
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" };
21 const size_t widget_width = 300;
22 const size_t widget_heigth = 50;
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 assert(total_width <= INT32_MAX);
28 assert(total_height <= INT32_MAX);
29 auto rc = SDL_CreateWindowAndRenderer(
30 title.c_str(), total_width,
static_cast<int>(total_height),
31 SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_MOUSE_FOCUS | SDL_WINDOW_INPUT_FOCUS, &_window,
34 widget_log_error(rc,
"SDL_CreateWindowAndRenderer");
37 for (
size_t x = 0; x < labels.size(); x++)
38 _list.emplace_back(_renderer, labels[x], initial[x], flags[x], x, widget_width,
41 _buttons.populate(_renderer, buttonlabels, buttonids, total_width,
42 static_cast<Sint32
>(input_height),
static_cast<Sint32
>(widget_width),
43 static_cast<Sint32
>(widget_heigth));
44 _buttons.set_highlight(0);
48 ssize_t SdlInputWidgetList::next(ssize_t current)
51 auto val =
static_cast<size_t>(current);
55 if (iteration >= _list.size())
69 }
while (!valid(
static_cast<ssize_t
>(val)));
70 return static_cast<ssize_t
>(val);
73 bool SdlInputWidgetList::valid(ssize_t current)
const
77 auto s =
static_cast<size_t>(current);
78 if (s >= _list.size())
80 return !_list[s].readonly();
87 auto s =
static_cast<size_t>(index);
88 if (s >= _list.size())
93 SdlInputWidgetList::~SdlInputWidgetList()
97 SDL_DestroyRenderer(_renderer);
98 SDL_DestroyWindow(_window);
101 bool SdlInputWidgetList::update(SDL_Renderer* renderer)
103 for (
auto& btn : _list)
105 if (!btn.update_label(renderer))
107 if (!btn.update_input(renderer))
111 return _buttons.update(renderer);
114 ssize_t SdlInputWidgetList::get_index(
const SDL_MouseButtonEvent& button)
116 const auto x = button.x;
117 const auto y = button.y;
118 for (
size_t i = 0; i < _list.size(); i++)
120 auto& cur = _list[i];
121 auto r = cur.input_rect();
123 if ((x >= r.x) && (x <= r.x + r.w) && (y >= r.y) && (y <= r.y + r.h))
124 return WINPR_ASSERTING_INT_CAST(ssize_t, i);
129 int SdlInputWidgetList::run(std::vector<std::string>& result)
132 ssize_t LastActiveTextInput = -1;
133 ssize_t CurrentActiveTextInput = next(-1);
135 if (!_window || !_renderer)
141 std::vector<SDL_Keycode> pressed;
144 if (!clear_window(_renderer))
147 if (!update(_renderer))
150 if (!_buttons.update(_renderer))
153 SDL_Event
event = {};
154 SDL_WaitEvent(&event);
157 case SDL_EVENT_KEY_UP:
159 auto it = std::remove(pressed.begin(), pressed.end(), event.key.key);
160 pressed.erase(it, pressed.end());
162 switch (event.key.key)
166 auto cur = get(CurrentActiveTextInput);
169 if (!cur->remove_str(_renderer, 1))
175 CurrentActiveTextInput = next(CurrentActiveTextInput);
181 res = INPUT_BUTTON_ACCEPT;
185 res = INPUT_BUTTON_CANCEL;
188 if (pressed.size() == 2)
190 if ((pressed[0] == SDLK_LCTRL) || (pressed[0] == SDLK_RCTRL))
192 auto cur = get(CurrentActiveTextInput);
195 auto text = SDL_GetClipboardText();
196 cur->set_str(_renderer, text);
206 case SDL_EVENT_KEY_DOWN:
207 pressed.push_back(event.key.key);
209 case SDL_EVENT_TEXT_INPUT:
211 auto cur = get(CurrentActiveTextInput);
214 if (!cur->append_str(_renderer, event.text.text))
219 case SDL_EVENT_MOUSE_MOTION:
221 auto TextInputIndex = get_index(event.button);
222 for (
auto& cur : _list)
224 if (!cur.set_mouseover(_renderer,
false))
227 if (TextInputIndex >= 0)
229 auto& cur = _list[
static_cast<size_t>(TextInputIndex)];
230 if (!cur.set_mouseover(_renderer,
true))
234 _buttons.set_mouseover(event.button.x, event.button.y);
237 case SDL_EVENT_MOUSE_BUTTON_DOWN:
239 auto val = get_index(event.button);
241 CurrentActiveTextInput = val;
243 auto button = _buttons.get_selected(event.button);
247 if (button->id() == INPUT_BUTTON_CANCEL)
248 res = INPUT_BUTTON_CANCEL;
250 res = INPUT_BUTTON_ACCEPT;
255 res = INPUT_BUTTON_CANCEL;
262 if (LastActiveTextInput != CurrentActiveTextInput)
264 if (CurrentActiveTextInput < 0)
265 SDL_StopTextInput(_window);
267 SDL_StartTextInput(_window);
268 LastActiveTextInput = CurrentActiveTextInput;
271 for (
auto& cur : _list)
273 if (!cur.set_highlight(_renderer,
false))
276 auto cur = get(CurrentActiveTextInput);
279 if (!cur->set_highlight(_renderer,
true))
283 SDL_RenderPresent(_renderer);
286 for (
auto& cur : _list)
287 result.push_back(cur.value());