3#include "sdl_selectlist.hpp"
4#include "../sdl_utils.hpp"
6static const Uint32 vpadding = 5;
8SdlSelectList::SdlSelectList(
const std::string& title,
const std::vector<std::string>& labels)
10 const size_t widget_height = 50;
11 const size_t widget_width = 600;
13 const size_t total_height = labels.size() * (widget_height + vpadding) + vpadding;
14 const size_t height = total_height + widget_height;
15 if (reset(title, widget_width, height))
17 SDL_FRect rect = { 0, 0, widget_width, widget_height };
18 for (
auto& label : labels)
20 _list.emplace_back(_renderer, label, rect);
21 rect.y += widget_height + vpadding;
24 const std::vector<int> buttonids = { INPUT_BUTTON_ACCEPT, INPUT_BUTTON_CANCEL };
25 const std::vector<std::string> buttonlabels = {
"accept",
"cancel" };
26 _buttons.populate(_renderer, buttonlabels, buttonids, widget_width,
27 static_cast<Sint32
>(total_height),
static_cast<Sint32
>(widget_width / 2),
28 static_cast<Sint32
>(widget_height));
29 _buttons.set_highlight(0);
33SdlSelectList::~SdlSelectList()
39int SdlSelectList::run()
42 ssize_t CurrentActiveTextInput = 0;
45 if (!_window || !_renderer)
51 if (!SdlWidget::clear_window(_renderer))
57 if (!_buttons.update(_renderer))
61 if (!SDL_WaitEvent(&event))
67 case SDL_EVENT_KEY_DOWN:
68 switch (event.key.key)
72 if (CurrentActiveTextInput > 0)
73 CurrentActiveTextInput--;
74 else if (_list.empty())
75 CurrentActiveTextInput = 0;
78 auto s = _list.size();
79 CurrentActiveTextInput =
80 WINPR_ASSERTING_INT_CAST(ssize_t, s) - 1;
85 if ((CurrentActiveTextInput < 0) || _list.empty())
86 CurrentActiveTextInput = 0;
89 auto s = _list.size();
90 CurrentActiveTextInput++;
93 CurrentActiveTextInput =
94 CurrentActiveTextInput %
95 WINPR_ASSERTING_INT_CAST(ssize_t, s);
103 res =
static_cast<int>(CurrentActiveTextInput);
107 res = INPUT_BUTTON_CANCEL;
113 case SDL_EVENT_MOUSE_MOTION:
115 auto TextInputIndex = get_index(event.button);
117 if (TextInputIndex >= 0)
119 auto& cur = _list[WINPR_ASSERTING_INT_CAST(
size_t, TextInputIndex)];
120 if (!cur.set_mouseover(_renderer,
true))
124 _buttons.set_mouseover(event.button.x, event.button.y);
127 case SDL_EVENT_MOUSE_BUTTON_DOWN:
129 auto button = _buttons.get_selected(event.button);
133 if (button->id() == INPUT_BUTTON_CANCEL)
134 res = INPUT_BUTTON_CANCEL;
136 res =
static_cast<int>(CurrentActiveTextInput);
140 CurrentActiveTextInput = get_index(event.button);
145 res = INPUT_BUTTON_CANCEL;
151 }
while (SDL_PollEvent(&event));
153 if (CurrentActiveTextInput >= 0)
155 auto& cur = _list[WINPR_ASSERTING_INT_CAST(
size_t, CurrentActiveTextInput)];
156 if (!cur.set_highlight(_renderer,
true))
160 auto rc = SDL_RenderPresent(_renderer.get());
163 SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"SDL_RenderPresent failed with %s",
175ssize_t SdlSelectList::get_index(
const SDL_MouseButtonEvent& button)
177 const auto x = button.x;
178 const auto y = button.y;
179 for (
size_t i = 0; i < _list.size(); i++)
181 auto& cur = _list[i];
184 if ((x >= r.x) && (x <= r.x + r.w) && (y >= r.y) && (y <= r.y + r.h))
185 return WINPR_ASSERTING_INT_CAST(ssize_t, i);
190bool SdlSelectList::update_text()
192 for (
auto& cur : _list)
194 if (!cur.update_text(_renderer))
201void SdlSelectList::reset_mouseover()
203 for (
auto& cur : _list)
205 cur.set_mouseover(_renderer,
false);
209void SdlSelectList::reset_highlight()
211 for (
auto& cur : _list)
213 cur.set_highlight(_renderer,
false);