TermOx
cycle_box.hpp
1 #ifndef TERMOX_WIDGET_WIDGETS_CYCLE_BOX_HPP
2 #define TERMOX_WIDGET_WIDGETS_CYCLE_BOX_HPP
3 #include <cstddef>
4 #include <memory>
5 #include <string>
6 #include <vector>
7 
8 #include <signals_light/signal.hpp>
9 
10 #include <termox/painter/glyph.hpp>
11 #include <termox/painter/glyph_string.hpp>
12 #include <termox/system/key.hpp>
13 #include <termox/system/mouse.hpp>
14 #include <termox/widget/align.hpp>
15 #include <termox/widget/growth.hpp>
16 #include <termox/widget/tuple.hpp>
17 #include <termox/widget/widgets/label.hpp>
18 #include <termox/widget/widgets/tile.hpp>
19 
20 // TODO Indicate in focus with color or trait change.
21 
22 namespace ox {
23 
25 
27 class Cycle_box : public HLabel {
28  public:
29  struct Parameters {
30  Align alignment = Align::Center;
31  int extra_left = 0;
32  int extra_right = 0;
33  Growth growth_strategy = Growth::Static;
34  };
35 
36  public:
38  sl::Signal<void(std::string)> option_changed;
39 
40  public:
42  explicit Cycle_box(Align alignment = Align::Center,
43  int extra_left = 0,
44  int extra_right = 0,
45  Growth growth_strategy = Growth::Static);
46 
47  explicit Cycle_box(Parameters p);
48 
49  public:
51 
53  auto add_option(Glyph_string label) -> sl::Signal<void()>&;
54 
56 
58  void remove_option(Glyph_string const& label);
59 
61  void next();
62 
64  void previous();
65 
67 
68  void set_current_option(std::string const& label);
69 
71 
73  void set_current_option(std::size_t index);
74 
76  [[nodiscard]] auto size() const -> std::size_t;
77 
78  protected:
79  auto mouse_press_event(Mouse const& m) -> bool override;
80 
81  auto mouse_wheel_event(Mouse const& m) -> bool override;
82 
83  auto key_press_event(Key k) -> bool override;
84 
85  private:
87  struct Option {
89  sl::Signal<void()> enabled = {};
90  };
91 
92  private:
93  std::vector<Option> options_;
94  std::size_t index_ = 0;
95 
96  private:
98  [[nodiscard]] auto current_option_label() const -> Glyph_string;
99 
101  [[nodiscard]] auto find(std::string const& label)
102  -> decltype(options_)::iterator;
103 
105  void emit_signals();
106 };
107 
109 [[nodiscard]] auto cycle_box(Cycle_box::Parameters = {})
110  -> std::unique_ptr<Cycle_box>;
111 
113 class Labeled_cycle_box : public HTuple<HLabel, Tile, Cycle_box> {
114  public:
115  struct Parameters {
116  Glyph_string label = U"";
117  };
118 
119  public:
120  HLabel& label = this->get<0>();
121  Tile& div = this->get<1>();
122  Cycle_box& cycle_box = this->get<2>();
123 
124  public:
125  explicit Labeled_cycle_box(Glyph_string title = U"");
126 
127  explicit Labeled_cycle_box(Parameters p);
128 };
129 
131 [[nodiscard]] auto labeled_cycle_box(Glyph_string label = U"")
132  -> std::unique_ptr<Labeled_cycle_box>;
133 
135 [[nodiscard]] auto labeled_cycle_box(Labeled_cycle_box::Parameters p)
136  -> std::unique_ptr<Labeled_cycle_box>;
137 
138 } // namespace ox
139 
140 namespace ox::slot {
141 
142 [[nodiscard]] auto add_option(Cycle_box& cb) -> sl::Slot<void(Glyph_string)>;
143 
144 [[nodiscard]] auto add_option(Cycle_box& cb, Glyph_string const& label)
145  -> sl::Slot<void()>;
146 
147 [[nodiscard]] auto remove_option(Cycle_box& cb)
148  -> sl::Slot<void(std::string const&)>;
149 
150 [[nodiscard]] auto remove_option(Cycle_box& cb, std::string const& label)
151  -> sl::Slot<void()>;
152 
153 [[nodiscard]] auto next(Cycle_box& cb) -> sl::Slot<void()>;
154 
155 [[nodiscard]] auto previous(Cycle_box& cb) -> sl::Slot<void()>;
156 
157 } // namespace ox::slot
158 #endif // TERMOX_WIDGET_WIDGETS_CYCLE_BOX_HPP
A rotating set of labels, emitting a Signal when the label is changed.
Definition: cycle_box.hpp:27
sl::Signal< void(std::string)> option_changed
Emitted when the option is changed, sends the new option's string rep.
Definition: cycle_box.hpp:38
Cycle_box(Align alignment=Align::Center, int extra_left=0, int extra_right=0, Growth growth_strategy=Growth::Static)
Given Label parameters are applied to each option's display.
Definition: cycle_box.cpp:42
void set_current_option(std::string const &label)
Set the current option to the option with label identifier.
Definition: cycle_box.cpp:89
auto mouse_wheel_event(Mouse const &m) -> bool override
Handles Mouse_wheel_event objects.
Definition: cycle_box.cpp:114
auto size() const -> std::size_t
Return the number of options in the Cycle_box.
Definition: cycle_box.cpp:102
auto mouse_press_event(Mouse const &m) -> bool override
Handles Mouse_press_event objects.
Definition: cycle_box.cpp:104
auto add_option(Glyph_string label) -> sl::Signal< void()> &
Add an option/label to the Cycle_box using Label constructor parameters.
Definition: cycle_box.cpp:55
auto key_press_event(Key k) -> bool override
Handles Key_press_event objects.
Definition: cycle_box.cpp:124
void next()
Move forward one option.
Definition: cycle_box.cpp:73
void previous()
Move backward one option.
Definition: cycle_box.cpp:81
void remove_option(Glyph_string const &label)
Remove an option/label identified by its text.
Definition: cycle_box.cpp:63
Holds a collection of Glyphs with a similar interface to std::string.
Definition: glyph_string.hpp:19
A single line of text with alignment, non-editable.
Definition: label.hpp:22
auto alignment() const noexcept -> Align
Return the Align given to set_alignment().
Definition: label.cpp:111
auto extra_left() const noexcept -> int
Return the amount given to set_extra_left().
Definition: label.cpp:124
auto extra_right() const noexcept -> int
Return the amount given to set_extra_right().
Definition: label.cpp:137
auto growth_strategy() const noexcept -> Growth
Return the value given to set_growth_strategy().
Definition: label.cpp:155
A label on the left and a Cycle_box on the right.
Definition: cycle_box.hpp:113
A unit width/height Widget that can display a single Glyph.
Definition: tile.hpp:12
Heterogeneous collection of Widgets within a Layout_t.
Definition: tuple.hpp:17
auto name() const -> std::string const &
Return the name of the Widget.
Definition: widget.cpp:77
Definition: cycle_box.hpp:29
Definition: cycle_box.hpp:115