3#include "sdl_select_list.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() =
default;
35int SdlSelectList::run()
38 ssize_t CurrentActiveTextInput = 0;
41 if (!_window || !_renderer)
51 if (!SDL_WaitEvent(&event))
57 case SDL_EVENT_KEY_DOWN:
58 switch (event.key.key)
62 if (CurrentActiveTextInput > 0)
63 CurrentActiveTextInput--;
64 else if (_list.empty())
65 CurrentActiveTextInput = 0;
68 auto s = _list.size();
69 CurrentActiveTextInput =
70 WINPR_ASSERTING_INT_CAST(ssize_t, s) - 1;
75 if ((CurrentActiveTextInput < 0) || _list.empty())
76 CurrentActiveTextInput = 0;
79 auto s = _list.size();
80 CurrentActiveTextInput++;
83 CurrentActiveTextInput =
84 CurrentActiveTextInput %
85 WINPR_ASSERTING_INT_CAST(ssize_t, s);
93 res =
static_cast<int>(CurrentActiveTextInput);
97 res = INPUT_BUTTON_CANCEL;
103 case SDL_EVENT_MOUSE_MOTION:
105 auto TextInputIndex = get_index(event.button);
107 if (TextInputIndex >= 0)
109 auto& cur = _list[WINPR_ASSERTING_INT_CAST(
size_t, TextInputIndex)];
110 if (!cur.mouseover(
true))
114 _buttons.set_mouseover(event.button.x, event.button.y);
117 case SDL_EVENT_MOUSE_BUTTON_DOWN:
119 auto button = _buttons.get_selected(event.button);
123 if (button->id() == INPUT_BUTTON_CANCEL)
124 res = INPUT_BUTTON_CANCEL;
126 res =
static_cast<int>(CurrentActiveTextInput);
130 CurrentActiveTextInput = get_index(event.button);
135 res = INPUT_BUTTON_CANCEL;
141 }
while (SDL_PollEvent(&event));
143 if (CurrentActiveTextInput >= 0)
145 auto& cur = _list[WINPR_ASSERTING_INT_CAST(
size_t, CurrentActiveTextInput)];
146 if (!cur.highlight(
true))
160bool SdlSelectList::updateInternal()
162 for (
auto& cur : _list)
170ssize_t SdlSelectList::get_index(
const SDL_MouseButtonEvent& button)
172 const auto x = button.x;
173 const auto y = button.y;
174 for (
size_t i = 0; i < _list.size(); i++)
176 auto& cur = _list[i];
179 if ((x >= r.x) && (x <= r.x + r.w) && (y >= r.y) && (y <= r.y + r.h))
180 return WINPR_ASSERTING_INT_CAST(ssize_t, i);
185void SdlSelectList::reset_mouseover()
187 for (
auto& cur : _list)
189 cur.mouseover(
false);
193void SdlSelectList::reset_highlight()
195 for (
auto& cur : _list)
197 cur.highlight(
false);