~aleteoryx/muditaos

ref: 782980b261457bc9a54235bd1a0fc90a6bd1484e muditaos/module-gui/gui/widgets/Lines.hpp -rw-r--r-- 2.7 KiB
782980b2 — PrzeBrudny [EGD-4372] Text addition boundaries added with tests. Removed old bounds checking. Text blocks on newlines split issue fixed. Text widgets cleanups. (#1020) 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <memory>
#include <vector>

#include "Alignment.hpp"
#include "InputEvent.hpp"
#include "TextConstants.hpp"
#include "utf8/UTF8.hpp"

#include "InputMode.hpp"
#include "TextLine.hpp"
#include "Translator.hpp"

namespace gui
{
    class Text;
    class TextLineCursor;

    class Lines
    {
        Text *text = nullptr;
        std::list<TextLine> lines;

        uint32_t max_lines_count = 4;
        uint32_t scroll_position = 0;

        bool underLine            = false;
        Position underLinePadding = 0;

      public:
        Lines(Text *text) : text(text)
        {}

        ~Lines() = default;

        void erase()
        {
            if (text != nullptr) {
                for (auto &line : lines) {
                    line.erase();
                }
            }
            lines.clear();
        }

        void emplace(TextLine &&line)
        {
            lines.emplace_back(std::move(line));
        }

        const auto &get()
        {
            return lines;
        }

        auto &last()
        {
            return lines.back();
        }

        auto size()
        {
            return lines.size();
        }

        [[nodiscard]] auto empty() const noexcept -> bool
        {
            return lines.empty();
        }

        auto maxWidth()
        {
            unsigned int w = 0;
            // could be std::max_element
            for (auto &el : lines) {
                w = el.width() > w ? el.width() : w;
            }
            return w;
        }

        auto linesHeight()
        {
            unsigned int h = 0;
            for (auto &el : lines) {
                h += el.height();
            }
            return h;
        }

        auto setUnderLine(bool val) -> void
        {
            underLine = val;
        }

        auto getUnderLine() -> bool
        {
            return underLine;
        }

        auto setUnderLinePadding(Position val) -> void
        {
            underLinePadding = val;
        }

        auto getUnderLinePadding() -> Position
        {
            return underLinePadding;
        }

        auto draw(BlockCursor &drawCursor, Length w, Length h, Position lineYPosition, Position lineXPosition) -> void;
        auto draw(BlockCursor &drawCursor,
                  Length w,
                  Length h,
                  Position lineYPosition,
                  Position lineXPosition,
                  unsigned int linesCount) -> void;

        auto linesHAlign(Length parentSize) -> void;
        auto linesVAlign(Length parentSize) -> void;

      protected:
        TextLine *getTextLine(uint32_t line);
    };

} // namespace gui