1 #include "value_control.hpp" 2 3 #include <cctype> 4 #include <string> 5 6 #include <cppurses/painter/color.hpp> 7 8 using namespace cppurses; 9 10 namespace palette { 11 namespace detail { 12 Buffered_edit_box()13Buffered_edit_box::Buffered_edit_box() { 14 this->height_policy.fixed(1); 15 this->box.set_validator([](char c) { return std::isdigit(c); }); 16 this->box.set_ghost_color(Color::White); 17 for (auto& child : this->children.get()) { 18 child->brush.set_background(Color::Blue); 19 } 20 } 21 } // namespace detail 22 23 // Set up value_edit_ and slider_ to be consistent with each other. Value_control(const Glyph_string & label)24Value_control::Value_control(const Glyph_string& label) 25 : label_{this->make_child<Label>(label)} { 26 this->width_policy.maximum(5); 27 auto& slider = this->slider_.slider; 28 auto& edit_box = this->value_edit_.box; 29 30 value_edit_.box.edit_finished.connect( __anonb29e77960202(std::string value_str) 31 [&slider, &edit_box](std::string value_str) { 32 if (value_str.empty()) { 33 slider.set_value(0); 34 } else { 35 slider.set_value(std::stoi(value_str)); 36 } 37 edit_box.set_contents(std::to_string(slider.value())); 38 edit_box.set_cursor(edit_box.contents().size()); 39 }); 40 __anonb29e77960302(int value) 41 slider.value_changed.connect([&edit_box](int value) { 42 edit_box.set_contents(std::to_string(value)); 43 edit_box.set_cursor(edit_box.contents().size()); 44 }); 45 46 label_.set_alignment(Alignment::Center); 47 } 48 } // namespace palette 49