23#include <winpr/cast.h>
25#include "sdl_widget_list.hpp"
26#include "sdl_input_widget_pair_list.hpp"
28static const Uint32 vpadding = 5;
30SdlInputWidgetPairList::SdlInputWidgetPairList(
const std::string& title,
31 const std::vector<std::string>& labels,
32 const std::vector<std::string>& initial,
33 const std::vector<Uint32>& flags, ssize_t selected)
35 assert(labels.size() == initial.size());
36 assert(labels.size() == flags.size());
37 const std::vector<int> buttonids = { INPUT_BUTTON_ACCEPT, INPUT_BUTTON_CANCEL };
38 const std::vector<std::string> buttonlabels = {
"accept",
"cancel" };
40 const size_t widget_width = 300;
41 const size_t widget_heigth = 50;
43 const size_t total_width = widget_width + widget_width;
44 const size_t input_height = labels.size() * (widget_heigth + vpadding) + vpadding;
45 const size_t total_height = input_height + widget_heigth;
46 assert(total_width <= INT32_MAX);
47 assert(total_height <= INT32_MAX);
49 if (reset(title, total_width, total_height))
51 for (
size_t x = 0; x < labels.size(); x++)
54 std::make_shared<SdlInputWidgetPair>(_renderer, labels.at(x), initial.at(x),
55 flags.at(x), x, widget_width, widget_heigth);
56 m_list.emplace_back(widget);
59 std::ignore = _buttons.populate(
60 _renderer, buttonlabels, buttonids, total_width,
static_cast<Sint32
>(input_height),
61 static_cast<Sint32
>(widget_width),
static_cast<Sint32
>(widget_heigth));
62 _buttons.set_highlight(0);
63 m_currentActiveTextInput = selected;
67ssize_t SdlInputWidgetPairList::next(ssize_t current)
70 auto val =
static_cast<size_t>(current);
74 if (iteration >= m_list.size())
88 }
while (!valid(
static_cast<ssize_t
>(val)));
89 return static_cast<ssize_t
>(val);
92bool SdlInputWidgetPairList::valid(ssize_t current)
const
96 auto s =
static_cast<size_t>(current);
97 if (s >= m_list.size())
99 return !m_list.at(s)->readonly();
102std::shared_ptr<SdlInputWidgetPair> SdlInputWidgetPairList::get(ssize_t index)
106 auto s =
static_cast<size_t>(index);
107 if (s >= m_list.size())
112SdlInputWidgetPairList::~SdlInputWidgetPairList()
118bool SdlInputWidgetPairList::updateInternal()
120 for (
auto& btn : m_list)
131ssize_t SdlInputWidgetPairList::get_index(
const SDL_MouseButtonEvent& button)
133 const auto x = button.x;
134 const auto y = button.y;
135 for (
size_t i = 0; i < m_list.size(); i++)
137 auto& cur = m_list.at(i);
138 auto r = cur->input_rect();
140 if ((x >= r.x) && (x <= r.x + r.w) && (y >= r.y) && (y <= r.y + r.h))
141 return WINPR_ASSERTING_INT_CAST(ssize_t, i);
146int SdlInputWidgetPairList::run(std::vector<std::string>& result)
149 ssize_t LastActiveTextInput = -1;
150 m_currentActiveTextInput = next(m_currentActiveTextInput);
152 if (!_window || !_renderer)
155 if (!SDL_StartTextInput(_window.get()))
166 SDL_Event
event = {};
167 if (!SDL_WaitEvent(&event))
173 case SDL_EVENT_KEY_UP:
175 switch (event.key.key)
179 auto cur = get(m_currentActiveTextInput);
182 if ((event.key.mod & SDL_KMOD_CTRL) != 0)
184 if (!cur->set_str(
""))
189 if (!cur->remove_str(1))
196 m_currentActiveTextInput = next(m_currentActiveTextInput);
202 res = INPUT_BUTTON_ACCEPT;
206 res = INPUT_BUTTON_CANCEL;
209 if ((event.key.mod & SDL_KMOD_CTRL) != 0)
211 auto cur = get(m_currentActiveTextInput);
214 auto text = SDL_GetClipboardText();
215 if (!cur->set_str(text))
225 case SDL_EVENT_TEXT_INPUT:
227 auto cur = get(m_currentActiveTextInput);
230 if (!cur->append_str(event.text.text))
235 case SDL_EVENT_MOUSE_MOTION:
237 auto TextInputIndex = get_index(event.button);
238 for (
auto& cur : m_list)
240 cur->set_mouseover(
false);
242 if (TextInputIndex >= 0)
244 auto& cur = m_list.at(
static_cast<size_t>(TextInputIndex));
245 cur->set_mouseover(
true);
248 _buttons.set_mouseover(event.button.x, event.button.y);
251 case SDL_EVENT_MOUSE_BUTTON_DOWN:
253 auto val = get_index(event.button);
255 m_currentActiveTextInput = val;
257 auto button = _buttons.get_selected(event.button);
261 if (button->id() == INPUT_BUTTON_CANCEL)
262 res = INPUT_BUTTON_CANCEL;
264 res = INPUT_BUTTON_ACCEPT;
269 res = INPUT_BUTTON_CANCEL;
275 }
while (SDL_PollEvent(&event));
277 if (LastActiveTextInput != m_currentActiveTextInput)
279 LastActiveTextInput = m_currentActiveTextInput;
282 for (
auto& cur : m_list)
284 if (!cur->set_highlight(
false))
287 auto cur = get(m_currentActiveTextInput);
290 if (!cur->set_highlight(
true))
294 auto rc = SDL_RenderPresent(_renderer.get());
297 SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"[%s] SDL_RenderPresent failed with %s",
298 __func__, SDL_GetError());
302 for (
auto& cur : m_list)
303 result.push_back(cur->value());
309 if (!SDL_StopTextInput(_window.get()))