1 #ifndef TERMOX_WIDGET_WIDGETS_LABEL_HPP
2 #define TERMOX_WIDGET_WIDGETS_LABEL_HPP
6 #include <termox/painter/glyph_string.hpp>
7 #include <termox/painter/painter.hpp>
8 #include <termox/widget/align.hpp>
9 #include <termox/widget/area.hpp>
10 #include <termox/widget/growth.hpp>
11 #include <termox/widget/layouts/horizontal.hpp>
12 #include <termox/widget/layouts/opposite.hpp>
13 #include <termox/widget/layouts/vertical.hpp>
14 #include <termox/widget/pipe.hpp>
15 #include <termox/widget/tuple.hpp>
21 template <
template <
typename>
typename Layout_t>
23 static_assert(layout::is_vertical_v<Layout_t<Widget>> ||
24 layout::is_horizontal_v<Layout_t<Widget>>);
29 Align alignment = Align::Left;
32 Growth growth_strategy = Growth::Static;
63 [[nodiscard]] auto
alignment() const noexcept -> Align;
69 [[nodiscard]] auto
extra_left() const noexcept ->
int;
75 [[nodiscard]] auto
extra_right() const noexcept ->
int;
86 auto
resize_event(Area new_size, Area old_size) ->
bool override;
89 inline static auto constexpr is_vertical =
90 layout::is_vertical_v<Layout_t<
Widget>>;
96 Growth growth_strategy_;
102 void update_offset();
104 void paint_vertical(
Painter& p);
106 void paint_horizontal(
Painter& p);
109 [[nodiscard]] auto find_offset(
int text_length,
116 template <template <typename> typename Layout_t>
122 -> std::unique_ptr<
Label<Layout_t>>;
125 template <template <typename> typename Layout_t>
127 -> std::unique_ptr<
Label<Layout_t>>;
138 -> std::unique_ptr<
HLabel>;
152 -> std::unique_ptr<
VLabel>;
161 namespace ox::detail {
166 template <
template <
typename>
typename Label_layout,
169 typename Wrapper_layout,
183 Align
alignment = label_last ? Align::Left : Align::Right;
191 Widget& padding = this->
template make_child() | padding_policy();
208 template <
typename... Args>
212 {std::move(p.text), p.alignment, p.extra_left,
213 p.extra_right, p.growth_strategy},
217 this->
template make_child<Widget_t>(std::forward<Args>(args)...)}
219 if constexpr (layout::is_vertical_v<Wrapper_layout<Widget>>)
224 *this | pipe::forward_focus(wrapped);
226 if constexpr (label_last)
227 this->swap_children(0, 2);
231 [[nodiscard]] auto padding_policy()
233 if constexpr (layout::is_vertical_v<Wrapper_layout<Widget>>)
234 return pipe::fixed_height(0);
236 return pipe::fixed_width(1);
247 template <
template <
typename>
typename Label_layout_t,
typename Widget_t>
249 detail::Label_wrapper<Label_layout_t, Widget_t, layout::Horizontal, false>;
252 template <
template <
typename>
typename Layout_t,
255 [[nodiscard]]
auto label_left(
typename Label<Layout_t>::Parameters p,
257 -> std::unique_ptr<Label_left<Layout_t, Widget_t>>
259 return std::make_unique<Label_left<Layout_t, Widget_t>>(
260 std::move(p), std::forward<Args>(args)...);
266 template <
template <
typename>
typename Layout_t,
typename Widget_t>
267 [[nodiscard]]
auto label_left(Glyph_string
text = U
"",
272 -> std::unique_ptr<Label_left<Layout_t, Widget_t>>
274 return label_left<Layout_t, Widget_t>(
typename Label<Layout_t>::Parameters{
279 template <
typename W
idget_t>
280 using HLabel_left = Label_left<layout::Horizontal, Widget_t>;
283 template <
typename Widget_t,
typename... Args>
284 [[nodiscard]]
auto hlabel_left(HLabel::Parameters p, Args&&... args)
285 -> std::unique_ptr<HLabel_left<Widget_t>>
287 return std::make_unique<HLabel_left<Widget_t>>(std::move(p),
288 std::forward<Args>(args)...);
294 template <
typename W
idget_t>
295 [[nodiscard]]
auto hlabel_left(Glyph_string
text = U
"",
300 -> std::unique_ptr<HLabel_left<Widget_t>>
302 return hlabel_left<Widget_t>(HLabel::Parameters{
307 template <
typename W
idget_t>
308 using VLabel_left = Label_left<layout::Vertical, Widget_t>;
311 template <
typename Widget_t,
typename... Args>
312 [[nodiscard]]
auto vlabel_left(VLabel::Parameters p, Args&&... args)
313 -> std::unique_ptr<VLabel_left<Widget_t>>
315 return std::make_unique<VLabel_left<Widget_t>>(std::move(p),
316 std::forward<Args>(args)...);
322 template <
typename W
idget_t>
323 [[nodiscard]]
auto vlabel_left(Glyph_string
text = U
"",
328 -> std::unique_ptr<VLabel_left<Widget_t>>
330 return vlabel_left<Widget_t>(VLabel::Parameters{
335 template <
template <
typename>
typename Label_layout_t,
typename Widget_t>
337 detail::Label_wrapper<Label_layout_t, Widget_t, layout::Horizontal, true>;
340 template <
template <
typename>
typename Layout_t,
343 [[nodiscard]]
auto label_right(
typename Label<Layout_t>::Parameters p,
345 -> std::unique_ptr<Label_right<Layout_t, Widget_t>>
347 return std::make_unique<Label_right<Layout_t, Widget_t>>(
348 std::move(p), std::forward<Args>(args)...);
354 template <
template <
typename>
typename Layout_t,
typename Widget_t>
355 [[nodiscard]]
auto label_right(Glyph_string
text = U
"",
360 -> std::unique_ptr<Label_left<Layout_t, Widget_t>>
362 return label_right(
typename Label<Layout_t>::Parameters{
367 template <
typename W
idget_t>
368 using HLabel_right = Label_right<layout::Horizontal, Widget_t>;
371 template <
typename Widget_t,
typename... Args>
372 [[nodiscard]]
auto hlabel_right(HLabel::Parameters p, Args&&... args)
373 -> std::unique_ptr<HLabel_right<Widget_t>>
375 return std::make_unique<HLabel_right<Widget_t>>(
376 std::move(p), std::forward<Args>(args)...);
382 template <
typename W
idget_t>
383 [[nodiscard]]
auto hlabel_right(Glyph_string
text = U
"",
388 -> std::unique_ptr<HLabel_right<Widget_t>>
390 return hlabel_right<Widget_t>(HLabel::Parameters{
395 template <
typename W
idget_t>
396 using VLabel_right = Label_right<layout::Vertical, Widget_t>;
399 template <
typename Widget_t,
typename... Args>
400 [[nodiscard]]
auto vlabel_right(VLabel::Parameters p, Args&&... args)
401 -> std::unique_ptr<VLabel_right<Widget_t>>
403 return std::make_unique<VLabel_right<Widget_t>>(
404 std::move(p), std::forward<Args>(args)...);
410 template <
typename W
idget_t>
411 [[nodiscard]]
auto vlabel_right(Glyph_string
text = U
"",
416 -> std::unique_ptr<VLabel_right<Widget_t>>
418 return vlabel_right<Widget_t>(VLabel::Parameters{
423 template <
template <
typename>
typename Label_layout_t,
typename Widget_t>
425 detail::Label_wrapper<Label_layout_t, Widget_t, layout::Vertical, false>;
428 template <
template <
typename>
typename Layout_t,
431 [[nodiscard]]
auto label_top(
typename Label<Layout_t>::Parameters p,
433 -> std::unique_ptr<Label_top<Layout_t, Widget_t>>
435 return std::make_unique<Label_top<Layout_t, Widget_t>>(
436 std::move(p), std::forward<Args>(args)...);
442 template <
template <
typename>
typename Layout_t,
typename Widget_t>
443 [[nodiscard]]
auto label_top(Glyph_string
text = U
"",
446 int extra_bottom = 0,
448 -> std::unique_ptr<Label_left<Layout_t, Widget_t>>
450 return label_top(
typename Label<Layout_t>::Parameters{
455 template <
typename W
idget_t>
456 using VLabel_top = Label_top<layout::Vertical, Widget_t>;
459 template <
typename Widget_t,
typename... Args>
460 [[nodiscard]]
auto vlabel_top(VLabel::Parameters p, Args&&... args)
461 -> std::unique_ptr<VLabel_top<Widget_t>>
463 return std::make_unique<VLabel_top<Widget_t>>(std::move(p),
464 std::forward<Args>(args)...);
470 template <
typename W
idget_t>
471 [[nodiscard]]
auto vlabel_top(Glyph_string
text = U
"",
476 -> std::unique_ptr<VLabel_top<Widget_t>>
478 return vlabel_top<Widget_t>(VLabel::Parameters{
483 template <
typename W
idget_t>
484 using HLabel_top = Label_top<layout::Horizontal, Widget_t>;
487 template <
typename Widget_t,
typename... Args>
488 [[nodiscard]]
auto hlabel_top(HLabel::Parameters p, Args&&... args)
489 -> std::unique_ptr<HLabel_top<Widget_t>>
491 return std::make_unique<HLabel_top<Widget_t>>(std::move(p),
492 std::forward<Args>(args)...);
498 template <
typename W
idget_t>
499 [[nodiscard]]
auto hlabel_top(Glyph_string
text = U
"",
504 -> std::unique_ptr<HLabel_top<Widget_t>>
506 return hlabel_top<Widget_t>(HLabel::Parameters{
511 template <
template <
typename>
typename Label_layout_t,
typename Widget_t>
513 detail::Label_wrapper<Label_layout_t, Widget_t, layout::Vertical, true>;
516 template <
template <
typename>
typename Layout_t,
519 [[nodiscard]]
auto label_bottom(
typename Label<Layout_t>::Parameters p,
521 -> std::unique_ptr<Label_bottom<Layout_t, Widget_t>>
523 return std::make_unique<Label_bottom<Layout_t, Widget_t>>(
524 std::move(p), std::forward<Args>(args)...);
530 template <
template <
typename>
typename Layout_t,
typename Widget_t>
531 [[nodiscard]]
auto label_bottom(Glyph_string
text = U
"",
534 int extra_bottom = 0,
536 -> std::unique_ptr<Label_left<Layout_t, Widget_t>>
538 return label_bottom(
typename Label<Layout_t>::Parameters{
543 template <
typename W
idget_t>
544 using VLabel_bottom = Label_bottom<layout::Vertical, Widget_t>;
547 template <
typename Widget_t,
typename... Args>
548 [[nodiscard]]
auto vlabel_bottom(VLabel::Parameters p, Args&&... args)
549 -> std::unique_ptr<VLabel_bottom<Widget_t>>
551 return std::make_unique<VLabel_bottom<Widget_t>>(
552 std::move(p), std::forward<Args>(args)...);
558 template <
typename W
idget_t>
559 [[nodiscard]]
auto vlabel_bottom(Glyph_string
text = U
"",
564 -> std::unique_ptr<VLabel_bottom<Widget_t>>
566 return vlabel_bottom<Widget_t>(VLabel::Parameters{
571 template <
typename W
idget_t>
572 using HLabel_bottom = Label_bottom<layout::Horizontal, Widget_t>;
575 template <
typename Widget_t,
typename... Args>
576 [[nodiscard]]
auto hlabel_bottom(HLabel::Parameters p, Args&&... args)
577 -> std::unique_ptr<HLabel_bottom<Widget_t>>
579 return std::make_unique<HLabel_bottom<Widget_t>>(
580 std::move(p), std::forward<Args>(args)...);
586 template <
typename W
idget_t>
587 [[nodiscard]]
auto hlabel_bottom(Glyph_string
text = U
"",
592 -> std::unique_ptr<HLabel_bottom<Widget_t>>
594 return hlabel_bottom<Widget_t>(HLabel::Parameters{
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 text() const noexcept -> Glyph_string const &
Return the text given to set_text().
Definition: label.cpp:98
void set_extra_left(int x)
Inform Label about space to left of Label for centered text offset.
Definition: label.cpp:117
Label(Glyph_string text=U"", Align alignment=Align::Left, int extra_left=0, int extra_right=0, Growth growth_strategy=Growth::Static)
Create a new Label Widget.
Definition: label.cpp:47
auto extra_left() const noexcept -> int
Return the amount given to set_extra_left().
Definition: label.cpp:124
void set_alignment(Align x)
Set text alignment of Label and update display.
Definition: label.cpp:104
void set_text(Glyph_string text)
Set text contents of Label and update display.
Definition: label.cpp:81
auto extra_right() const noexcept -> int
Return the amount given to set_extra_right().
Definition: label.cpp:137
void set_extra_right(int x)
Inform Label about space to right of Label for centered text offset.
Definition: label.cpp:130
void set_growth_strategy(Growth type)
Enable/Disable Dynamic size, where the Label's size is the text length.
Definition: label.cpp:143
auto resize_event(Area new_size, Area old_size) -> bool override
Handles Resize_event objects.
Definition: label.cpp:171
auto growth_strategy() const noexcept -> Growth
Return the value given to set_growth_strategy().
Definition: label.cpp:155
auto paint_event(Painter &p) -> bool override
Handles Paint_event objects.
Definition: label.cpp:161
Contains functions to paint Glyphs to a Widget's screen area.
Definition: painter.hpp:21
Heterogeneous collection of Widgets within a Layout_t.
Definition: tuple.hpp:17
Wraps a Widget_t object with a label.
Definition: label.hpp:171
Label_wrapper(Glyph_string text=U"", Align alignment=Align::Left, int extra_left=0, int extra_right=0, Growth growth_strategy=Growth::Static)
Construct a new Label and wrapped Widget_t.
Definition: label.hpp:198
Label_wrapper(Parameters p, Args &&... args)
Constructs Label with given parameters, and Widget_t with args...
Definition: label.hpp:209
Definition: label.hpp:181