From 8e3a41d796830f8c38a860823a08774bf20173e4 Mon Sep 17 00:00:00 2001 From: Bertrand Songis Date: Tue, 31 Mar 2020 15:52:12 +0200 Subject: [PATCH] Choice can now accept an array of const char * as values argument --- radio/src/thirdparty/libopenui/src/choice.cpp | 63 ++++++++++++++++--- radio/src/thirdparty/libopenui/src/choice.h | 9 ++- 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/radio/src/thirdparty/libopenui/src/choice.cpp b/radio/src/thirdparty/libopenui/src/choice.cpp index 919f1941d..01fd41a80 100644 --- a/radio/src/thirdparty/libopenui/src/choice.cpp +++ b/radio/src/thirdparty/libopenui/src/choice.cpp @@ -21,10 +21,9 @@ #include "menu.h" #include "theme.h" -Choice::Choice(FormGroup * parent, const rect_t & rect, const char * values, int16_t vmin, int16_t vmax, - std::function getValue, std::function setValue, WindowFlags windowFlags) : +Choice::Choice(FormGroup * parent, const rect_t & rect, int16_t vmin, int16_t vmax, + std::function getValue, std::function setValue, WindowFlags windowFlags) : ChoiceBase(parent, rect, CHOICE_TYPE_DROPOWN, windowFlags), - values(values), vmin(vmin), vmax(vmax), getValue(std::move(getValue)), @@ -32,10 +31,56 @@ Choice::Choice(FormGroup * parent, const rect_t & rect, const char * values, int { } +Choice::Choice(FormGroup * parent, const rect_t & rect, const char * values[], int16_t vmin, int16_t vmax, + std::function getValue, std::function setValue, WindowFlags windowFlags) : + ChoiceBase(parent, rect, CHOICE_TYPE_DROPOWN, windowFlags), + vmin(vmin), + vmax(vmax), + getValue(std::move(getValue)), + setValue(std::move(setValue)) +{ + const char ** value = &values[0]; + for (int i = vmin; i <= vmax; i++) { + this->values.emplace_back(*value++); + } +} + +Choice::Choice(FormGroup * parent, const rect_t & rect, const char * values, int16_t vmin, int16_t vmax, + std::function getValue, std::function setValue, WindowFlags windowFlags) : + ChoiceBase(parent, rect, CHOICE_TYPE_DROPOWN, windowFlags), + vmin(vmin), + vmax(vmax), + getValue(std::move(getValue)), + setValue(std::move(setValue)) +{ + if (values) { + uint8_t len = values[0]; + const char * value = &values[1]; + for (int i = vmin; i <= vmax; i++) { + this->values.emplace_back(std::string(value, min(len, strlen(value)))); + value += len; + } + } +} + +void Choice::addItem(const char * item) +{ + values.emplace_back(item); + vmax += 1; +} + +void Choice::addItems(const char * items[], uint8_t count) +{ + values.reserve(values.size() + count); + for (uint8_t i = 0; i < count; i++) + values.emplace_back(items[i]); + vmax += count; +} + void Choice::paint(BitmapBuffer * dc) { FormField::paint(dc); - theme->drawChoice(dc, this, textHandler ? textHandler(getValue()).c_str() : TEXT_AT_INDEX(values, getValue() - vmin).c_str()); + theme->drawChoice(dc, this, textHandler ? textHandler(getValue()).c_str() : values[getValue() - vmin].c_str()); } #if defined(HARDWARE_KEYS) @@ -67,12 +112,12 @@ void Choice::openMenu() continue; if (textHandler) { menu->addLine(textHandler(i), [=]() { - setValue(i); + setValue(i); }); } else { - menu->addLine(TEXT_AT_INDEX(values, i - vmin), [=]() { - setValue(i); + menu->addLine(values[i - vmin], [=]() { + setValue(i); }); } if (value == i) { @@ -86,8 +131,8 @@ void Choice::openMenu() } menu->setCloseHandler([=]() { - editMode = false; - setFocus(); + editMode = false; + setFocus(); }); setEditMode(true); diff --git a/radio/src/thirdparty/libopenui/src/choice.h b/radio/src/thirdparty/libopenui/src/choice.h index 8b0d1092b..fdca595be 100644 --- a/radio/src/thirdparty/libopenui/src/choice.h +++ b/radio/src/thirdparty/libopenui/src/choice.h @@ -20,6 +20,7 @@ #ifndef _CHOICE_H_ #define _CHOICE_H_ +#include #include "form.h" enum ChoiceType { @@ -46,8 +47,14 @@ class ChoiceBase : public FormField { class Choice : public ChoiceBase { public: + Choice(FormGroup * parent, const rect_t & rect, int16_t vmin, int16_t vmax, std::function getValue, std::function setValue = nullptr, WindowFlags windowFlags = 0); + Choice(FormGroup * parent, const rect_t & rect, const char * values[], int16_t vmin, int16_t vmax, std::function getValue, std::function setValue = nullptr, WindowFlags windowFlags = 0); Choice(FormGroup * parent, const rect_t & rect, const char * values, int16_t vmin, int16_t vmax, std::function getValue, std::function setValue = nullptr, WindowFlags windowFlags = 0); + void addItem(const char * item); + + void addItems(const char * items[], uint8_t count); + #if defined(DEBUG_WINDOWS) std::string getName() override { @@ -86,7 +93,7 @@ class Choice : public ChoiceBase { } protected: - const char * values = nullptr; + std::vector values; int16_t vmin = 0; int16_t vmax = 0; std::string menuTitle;