diff options
author | quou <quou@disroot.org> | 2025-01-24 00:37:53 +1100 |
---|---|---|
committer | quou <quou@disroot.org> | 2025-01-24 00:37:53 +1100 |
commit | c0dc2dbcd26ca307bd7985c8626a2a1425a961fa (patch) | |
tree | 97c5466d22b79ade317d409ba19f1ca6e2e54a81 /ui.cpp | |
parent | 3afcec1798977d3309a5359b56b9534d87e70b6b (diff) |
UI checkbox
Diffstat (limited to 'ui.cpp')
-rw-r--r-- | ui.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -895,6 +895,46 @@ void UI::Button::on_render() { ); } +UI::Checkbox::Checkbox( + UI* ui, + Element* parent, + const char* text +): Element(ui, parent) { + btn = ui->create_element<Button>(this, " "); + btn->handler = [](UI::Element* e, const Message& m) { + if (m.type == UI::Message::Type::click) { + UI::Checkbox* ch = (UI::Checkbox*)e->parent; + ch->set_val(!ch->val); + } + return 0; + }; + label = ui->create_element<Label>(this, text); + val = false; +} + +UI::Rect UI::Checkbox::layout(const Rect& avail) { + int w; + Rect r = avail; + Rect br = btn->layout(avail); + w = br.w + ui_padding; + r.x += w; + r.w -= w; + r.clip(avail); + label->layout(r); + bound = Rect(br.x, br.y, br.w + ui_padding * 2, br.h); + clip = bound; + clip.clip(ui->area); + return bound; +} + +void UI::Checkbox::set_val(bool v) { + Message msg{}; + btn->set_text(v? "x": " "); + val = v; + msg.type = Message::Type::checkbox_changed; + message(msg); +} + UI::Input::Input(UI* ui, Element* parent, Input_Filter f): Element(ui, parent), filter(f), |