14#include <egt/canvas.h>
15#include <egt/detail/meta.h>
19#include <egt/slider.h>
20#include <egt/textwidget.h>
57 const cairo_text_extents_t& te,
59 : m_text(std::move(
text)),
61 m_text_rect_flags(
flags),
66 const std::string&
text(
void)
const {
return m_text; }
67 const Rect&
rect(
void)
const {
return m_rect; }
71 uint32_t
behave(
void)
const {
return m_behave; }
72 const cairo_text_extents_t&
text_extents(
void)
const {
return m_te; }
82 return ((m_text_rect_flags == r.m_text_rect_flags) &&
83 (m_rect.
y() == r.m_rect.y()) &&
84 (m_rect.
height() == r.m_rect.height()) &&
85 ((m_rect.
x() + m_rect.
width()) == r.m_rect.x()) &&
105 uint32_t m_behave{0};
108 cairo_text_extents_t m_te;
141 fit_to_width = detail::bit(0),
144 multiline = detail::bit(1),
147 word_wrap = detail::bit(2),
150 no_virt_keyboard = detail::bit(3),
153 horizontal_scrollable = detail::bit(4),
156 vertical_scrollable = detail::bit(5),
178 explicit TextBox(
const std::string& text = {},
179 const AlignFlags& text_align = default_text_align(),
180 const TextFlags& flags = {})
noexcept;
189 const AlignFlags& text_align = default_text_align()) noexcept;
199 const
AlignFlags& text_align = default_text_align(),
209 const std::string& text = {},
210 const AlignFlags& text_align = default_text_align(),
211 const TextFlags& flags = {})
noexcept;
221 const std::string& text,
223 const AlignFlags& text_align = default_text_align(),
251 using TextWidget::text;
253 void text(
const std::string& str)
override;
257 using TextWidget::min_size_hint;
280 if (detail::change_if_diff<>(m_text_flags, text_flags))
405 return m_select_start;
470 bool internal_drag()
const override {
return true; }
479 using TextWidget::text_size;
519 size_t up(
size_t cursor_pos)
const;
522 size_t up()
const {
return up(m_cursor_pos); }
525 size_t down(
size_t cursor_pos)
const;
567 TextRects::iterator& pos,
579 TextRects::iterator& prev_pos,
581 TextRects::iterator& next_pos);
585 TextRects::iterator& prev_pos,
587 TextRects::iterator& next_pos);
591 TextRects::iterator& prev_pos,
593 TextRects::iterator& next_pos);
597 TextRects::iterator& prev_pos,
599 TextRects::iterator& next_pos);
606 TextRects::const_iterator& pos,
611 TextRects::const_iterator& prev_pos,
613 TextRects::const_iterator& next_pos);
666 damage(Rect::intersection(rect, text_area()));
673 damage(Rect::intersection(m_cursor_rect, box()));
683 size_t m_cursor_pos{0};
689 size_t m_saved_column{0};
695 size_t m_select_start{0};
698 size_t m_select_len{0};
701 size_t m_select_origin{0};
704 size_t m_select_drag_start{0};
707 bool m_validate_input{
false};
717 mutable bool m_text_rect_valid{
false};
722 cairo_t* m_cr{
nullptr};
740 explicit State(
const Font* font,
743 const TextFlags& text_flags) noexcept
746 m_text_align(text_align),
747 m_text_flags(text_flags)
750 State& operator=(
const State& rhs)
noexcept
753 m_flags = rhs.m_flags;
754 m_text_align = rhs.m_text_align;
755 m_text_flags = rhs.m_text_flags;
759 State& operator=(State&& rhs)
noexcept
762 m_flags = std::move(rhs.m_flags);
763 m_text_align = std::move(rhs.m_text_align);
764 m_text_flags = std::move(rhs.m_text_flags);
768 bool operator==(
const State& rhs)
const noexcept
770 return m_font == rhs.m_font &&
771 m_flags == rhs.m_flags &&
772 m_text_align == rhs.m_text_align &&
773 m_text_flags == rhs.m_text_flags;
776 bool operator!=(
const State& rhs)
const noexcept
782 const Font* m_font{
nullptr};
783 Widget::Flags m_flags;
784 AlignFlags m_text_align;
785 TextFlags m_text_flags;
789 void cursor_timeout();
792 bool m_cursor_state{
false};
795 TextFlags m_text_flags{};
812 Signal<>::RegisterHandle m_gain_focus_reg{};
815 Signal<>::RegisterHandle m_lost_focus_reg{};
817 void initialize(
bool init_inherited_properties =
true);
819 void deserialize(Serializer::Properties& props);
825EGT_API
void draw_text(Painter& painter,
827 const std::string& text,
830 const AlignFlags& text_align,
832 const Pattern& text_color,
833 const std::function<
void(
const Point& offset,
size_t height)>& draw_cursor =
nullptr,
834 size_t cursor_pos = 0,
835 const Pattern& highlight_color = {},
836 size_t select_start = 0,
837 size_t select_len = 0);
840EGT_API
void draw_text(Painter& painter,
842 const std::string& text,
845 const AlignFlags& text_align,
847 const Pattern& text_color,
848 const AlignFlags& image_align,
850 const std::function<
void(
const Point& offset,
size_t height)>& draw_cursor =
nullptr,
851 size_t cursor_pos = 0,
852 const Pattern& highlight_color = {},
853 size_t select_start = 0,
854 size_t select_len = 0);
Alignment flags.
Definition widgetflags.h:379
Manages a unique drawing surface and context.
Definition canvas.h:41
EGT_NODISCARD constexpr bool is_set(const T flag) const noexcept
Test if the specified flag is set.
Definition flagsbase.h:64
Utility class for managing a set of flags with the ability to observe changes to the flags.
Definition flags.h:40
bool clear(T flag) noexcept
Clear a single flag.
Definition flags.h:125
bool set(T flag) noexcept
Set a single flag.
Definition flags.h:107
Manages a font and properties of a font.
Definition font.h:35
A Frame is a Widget that has children widgets.
Definition frame.h:45
Drawing interface for 2D graphics.
Definition painter.h:45
Periodic timer.
Definition timer.h:281
EGT_NODISCARD constexpr Dim width() const noexcept
Get the width value.
Definition geometry.h:913
EGT_NODISCARD constexpr Dim x() const noexcept
Get the x value.
Definition geometry.h:903
EGT_NODISCARD constexpr const PointType< Dim, DimCompat > & point() const noexcept
Get the PointType of the rectangle.
Definition geometry.h:722
EGT_NODISCARD constexpr Dim height() const noexcept
Get the height value.
Definition geometry.h:915
EGT_NODISCARD constexpr Dim y() const noexcept
Get the y value.
Definition geometry.h:905
Abstract base serializer class.
Definition serialize.h:34
std::list< std::tuple< std::string, std::string, Serializer::Attributes > > Properties
Definition serialize.h:47
Input text box.
Definition text.h:134
void selection_move(size_t count, bool save_column=true)
Move the selection cursor to count character(s):
TextBox(Serializer::Properties &props, bool is_derived) noexcept
size_t insert(const std::string &str)
Insert text at the cursor.
size_t up(size_t cursor_pos) const
Get the position in the line befor cursor_pos at the same column, if possible.
void move_vslider()
Move the vertical slider, if needed, to track the cursor.
void damage_cursor()
Damage the cursor but only if visible.
Definition text.h:670
EGT_NODISCARD Size min_size_hint() const override
Get a minimum size hint for the Widget.
void tokenize(TextRects &rects)
Split the text into atomic tokens that fill the TextRects parameter.
void update_hslider()
Update the horizontal slider.
void show_cursor()
Show/enable the visibility of the cursor.
void tag_text(TextRects &prev, TextRects &next)
Damage the differences between two texts.
void draw_sliders(Painter &painter, const Rect &rect)
Draw sliders.
void selection_clear()
Clear any selection.
void clear_selection(TextRects &rects)
Clear the TextRectFlag::selected flag from all TextRects in rects.
void invalidate_text_rect()
Invalidate the cache used by text_rect()
void selection_damage()
Compute damage rectangles for selection updates.
void tag_default_aligned_line(TextRects &prev, TextRects::iterator &prev_pos, TextRects &next, TextRects::iterator &next_pos)
Damage the merged rectangle of two text lines if they are not equal.
void change_cursor(size_t pos, bool save_column=true)
Change the value of m_cursor_pos.
void draw(Painter &painter, const Rect &rect) override
Draw the widget.
EGT_NODISCARD std::string selected_text() const
Get, or copy, the selected text.
EGT_NODISCARD size_t selection_cursor()
Get the position of the moving end of the selection, as opposed to its origin (the fixed end of the s...
static std::string longest_prefix(const std::string &s1, const std::string &s2)
Compute the longest common prefix between two strings.
void selection(size_t pos, size_t length)
Set the selection of text.
void init_sliders()
Init sliders.
ValidatorCallbackArray m_validator_callbacks
Callbacks invoked to validate the input.
Definition text.h:710
Rect m_text_rect
The cache for text_rect().
Definition text.h:716
void hide_cursor()
Hide/disable the visibility of the cursor.
EGT_NODISCARD Point text_offset() const
Get the position offset of the drawn text as compared to the whole text.
static void get_line(const TextRects &rects, TextRects::iterator &pos, std::string &line, Rect &rect)
Get the std::string and Rect from pos to the first follwing TextRect in rects with a different height...
void update_sliders()
Update the horizontal and vertical sliders as needed.
TextBox & operator=(const TextBox &)=delete
void cursor_forward(size_t count=1)
Move the cursor forward by the specified count from the current position.
void prepare_text(TextRects &rects)
Tokenize and compute the layout of a text; fill TextRects accordingly.
void tag_line(TextRects &prev, TextRects::iterator &prev_pos, TextRects &next, TextRects::iterator &next_pos)
Damage the differences between two text lines, based on the text alignment.
egt::Flags< TextFlag > TextFlags
Text flags.
Definition text.h:160
EGT_NODISCARD Rect text_rect() const
Get the rectangle of a text.
void tag_line_selection(const TextRects &prev, TextRects::const_iterator &prev_pos, const TextRects &next, TextRects::const_iterator &next_pos)
Damage the differences between two selected text lines.
static AlignFlags default_text_align()
Change text align.
size_t up() const
Get the position in the previous line at the same column, if possible.
Definition text.h:522
static std::string longest_suffix(const std::string &s1, const std::string &s2)
Compute the longest common suffix between two strings.
EGT_NODISCARD size_t selection_length() const
Get the length of the selection.
Definition text.h:411
TextBox(const std::string &text={}, const AlignFlags &text_align=default_text_align(), const TextFlags &flags={}) noexcept
void text_flags(const TextFlags &text_flags)
Set the text flags.
Definition text.h:278
void resize(const Size &size) override
Resize the widget.
void cursor_begin()
Move the cursor to the beginning.
void set_selection(TextRects &rects)
Add the TextRectFlag::selected flag to TextRects based on the m_select_start and m_select_len values.
void cursor_backward(size_t count=1)
Move the cursor backward by the specified count from the current position.
void move_hslider()
Move the horizontal slider, if needed, to track the cursor.
Rect m_cursor_rect
Definition text.h:725
size_t down(size_t cursor_pos) const
Get the position in the line after cursor_pos at the same column, if possible.
TextRects m_rects
Definition text.h:724
size_t append(const std::string &str)
Move the cursor to the end and insert.
EGT_NODISCARD size_t selection_start() const
Get the start position of the selection.
Definition text.h:403
EGT_NODISCARD size_t width_to_len(const std::string &str) const
Given text, return the number of UTF8 characters that will fit on a single line inside of the widget.
void consolidate(TextRects &rects)
Merge adjacent TextRect items, when possible.
void tag_left_aligned_line(TextRects &prev, TextRects::iterator &prev_pos, TextRects &next, TextRects::iterator &next_pos)
Damage the merge rectangle of two text lines, excluding their common prefix.
void serialize(Serializer &serializer) const override
Serialize the widget to the specified serializer.
TextBox(Frame &parent, const std::string &text={}, const AlignFlags &text_align=default_text_align(), const TextFlags &flags={}) noexcept
void draw_text(Painter &painter, const Rect &rect)
Draw the text based on the m_rects TextRects.
static void default_text_align(const AlignFlags &align)
Change default text align.
TextBox & operator=(TextBox &&)=default
void text_offset(const Point &p)
Set the position offset of the drawn text as compared to the whole text.
EGT_NODISCARD Rect text_boundaries() const
Get the rectangle of the text boundaries.
std::vector< ValidatorCallback > ValidatorCallbackArray
Type array used for validator callbacks.
Definition text.h:486
void update_vslider()
Update the vertical slider.
size_t down() const
Get the position in the next line at the same column, if possible.
Definition text.h:528
TextFlag
Text flags.
Definition text.h:139
EGT_NODISCARD size_t max_length() const
Get the max length of allowed text.
Definition text.h:271
TextBox(const std::string &text, const TextFlags &flags, const AlignFlags &text_align=default_text_align()) noexcept
void selection_backward(size_t count=1)
Extend the selection to the left if the cursor is at its end, otherwise reduce the selection from the...
void add_validator_function(ValidatorCallback callback)
Add a callback to be invoked to validate the input.
static void get_line_selection(const TextRects &rects, TextRects::const_iterator &pos, Rect &rect)
Compute the merged Rect for the selected TextRects on the same line as pos.
EGT_NODISCARD size_t cursor() const
Get the cursor position.
cairo_t * context() const
Get the cairo context to compute text extents, hence the text layout.
Definition text.h:540
TextBox(Serializer::Properties &props) noexcept
Definition text.h:229
void cursor_set(size_t pos)
Set the cursor to the specific position.
virtual void handle_key(const Key &key)
Process key events.
void handle(Event &event) override
Handle an event.
TextBox(const TextBox &)=delete
EGT_NODISCARD Rect text_area() const
Return the rectangle boundaries where the text can be drawn.
size_t end_of_line(size_t cursor_pos) const
Get the position of the end of the line at cursor_pos.
size_t end_of_line() const
Get the position of the end of the current line.
Definition text.h:516
size_t beginning_of_line() const
Get the position of the beginning of the current line.
Definition text.h:510
void selection_forward(size_t count=1)
Extend the selection to the right if the cursor is at its beginning, otherwise reduce the selection f...
void input_validation_enabled(bool enabled)
Enable or disable input validation.
void compute_layout(TextRects &rects)
Compute the text layout from the tokens contained in the TextRects.
void tag_text_selection(const TextRects &prev, const TextRects &next)
Damage the differences between two selected texts.
void tag_right_aligned_line(TextRects &prev, TextRects::iterator &prev_pos, TextRects &next, TextRects::iterator &next_pos)
Damage the merge rectangle of two text lines, excluding their common suffix.
void get_cursor_rect()
Update m_cursor_rect based on the current position of the cursor.
void cursor_set(size_t pos, bool save_column)
Set the cursor position.
TextBox(Frame &parent, const std::string &text, const Rect &rect, const AlignFlags &text_align=default_text_align(), const TextFlags &flags={}) noexcept
void damage_text(const Rect &rect)
Damage the text but only if visible.
Definition text.h:664
void selection_all()
Select all of the text.
void selection_delete()
Delete the selected text.
void resize_sliders()
Resize the sliders whenever the widget size changes.
size_t point2pos(const Point &p) const
Convert point coordinates to position in text for cursor or selection.
void move_sliders()
Move sliders, if needed, to track the cursor.
~TextBox() noexcept override
void refresh_text_area()
Compute the new text layout and cursor rectangle.
PeriodicTimer m_timer
Timer for blinking the cursor.
Definition text.h:677
void continue_show_cursor()
If the cursor is shown, keep is shown some more.
bool validate_input(const std::string &str)
Validate the input against the validator pattern.
TextBox(TextBox &&)=default
std::function< bool(const std::string &)> ValidatorCallback
Validator callback type.
Definition text.h:166
void clear() override
Clear the text value.
void max_length(size_t len)
Set the maximum allowed length of the text.
size_t beginning_of_line(size_t cursor_pos) const
Get the position of the beginning of the line at cursor_pos.
void cursor_end()
Move the cursor to the end.
static DefaultDim half_screens(DefaultDim size, DefaultDim screen_size)
Return the lowest multiple of 'screen_size' / 2, greater than 'size'.
EGT_NODISCARD const TextFlags & text_flags() const
Get a const ref of the flags.
Definition text.h:290
EGT_NODISCARD Size text_size() const
void text(const std::string &str) override
Set the text.
Canvas m_canvas
The canvas that provides a cairo context to compute the text layout.
Definition text.h:713
TextRect split(size_t pos, cairo_t *cr) noexcept
void deselect(void)
Definition text.h:74
TextRectFlag
TextRect flags.
Definition text.h:40
@ eonel
End of non-empty line.
@ selected
Text is selected.
bool end_of_non_empty_line(void) const
Definition text.h:76
TextRect & consolidate(const TextRect &r, cairo_t *cr) noexcept
void point(const Point &p)
Definition text.h:69
const TextRectFlags & flags(void) const
Definition text.h:70
uint32_t behave(void) const
Definition text.h:71
void mark_beginning_of_line(void)
Definition text.h:77
const Rect & rect(void) const
Definition text.h:67
const std::string & text(void) const
Definition text.h:66
bool is_selected(void) const
Definition text.h:75
const cairo_text_extents_t & text_extents(void) const
Definition text.h:72
TextRect(uint32_t behave, const Rect &rect, std::string text, const cairo_text_extents_t &te, const TextRectFlags &flags={}) noexcept
Definition text.h:54
size_t length(void) const noexcept
void rect(const Rect &r)
Definition text.h:68
void select(void)
Definition text.h:73
bool beginning_of_line(void) const
Definition text.h:78
bool can_consolidate(const TextRect &r) const noexcept
Definition text.h:80
A widget with text and text related properties.
Definition textwidget.h:33
RectType< DefaultDim, detail::Compatible::normal > Rect
Helper type alias.
Definition geometry.h:1023
PointType< DefaultDim, detail::Compatible::normal > Point
Helper type alias.
Definition geometry.h:314
constexpr T bit(T n)
Utility to create a bit mask for the specified bit.
Definition meta.h:306
constexpr bool operator!=(const Color &lhs, const Color &rhs)
Color operator.
Definition color.h:509
Justification
Generic justification of children flags.
Definition widgetflags.h:667
std::list< TextRect > TextRects
Definition text.h:111
constexpr bool operator==(const Color &lhs, const Color &rhs)
Color operator.
Definition color.h:500
int DefaultDim
Define the default dimension type used for geometry.
Definition geometry.h:34
EGT framework namespace.
Definition animation.h:24
A single event that has information about the event and state for the event.
Definition event.h:255
Keyboard event data.
Definition event.h:165