~aleteoryx/muditaos

ref: 7fbaf735ed99f5b0d26adb686cfa6ed2bb4267d5 muditaos/module-gui/gui/widgets/TextBlockCursor.hpp -rw-r--r-- 5.3 KiB
7fbaf735 — Przemyslaw Brudny [EGD-7813] Option Window titles localizations fix 4 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include "utf8/UTF8.hpp"
#include "TextConstants.hpp"
#include "TextBlock.hpp"
#include <cstdio>
#include <stdint.h>
#include <string>
#include <list>

namespace gui
{

    class TextDocument;
    class TextBlock;
    class RawFont;

    /// element to:
    /// 1. get position in TextDocument and
    /// 2. work as iterator on TextDocument
    ///
    /// pos - points to curent character position pointed via cursor
    /// to add character we insert element in cursor position
    /// to remove character we remove element pointed via cursor
    class BlockCursor
    {
      protected:
        TextDocument *document = nullptr;
        [[nodiscard]] auto currentBlock() const -> std::list<TextBlock>::iterator;
        [[nodiscard]] auto blocksEnd() const -> std::list<TextBlock>::iterator;
        [[nodiscard]] auto blocksBegin() const -> std::list<TextBlock>::iterator;
        RawFont *default_font = nullptr;

      private:
        bool emptyNewLineAdded          = false;
        bool blockChanged               = false;
        unsigned int pos                = text::npos;
        unsigned int currentBlockNumber = text::npos;

      protected:
        [[nodiscard]] auto checkNpos() const -> bool;
        void resetNpos();
        [[nodiscard]] auto checkDocument() const -> bool;

      public:
        /// gets cursor pointing in position in block
        /// @note it does not select next block - to do so add another ctor based on operator+
        /// and check if this one is needed
        BlockCursor(TextDocument *document, unsigned int pos, unsigned int block_nr, RawFont *default_font);
        BlockCursor()          = default; /// bad cursor
        virtual ~BlockCursor() = default;

        [[nodiscard]] auto getPosition() const -> unsigned int
        {
            return pos;
        }

        [[nodiscard]] auto getDocument() const -> TextDocument *
        {
            return document;
        }

        [[nodiscard]] auto getBlockNumber() const -> unsigned int
        {
            return currentBlockNumber;
        }

        [[nodiscard]] auto checkPreviousBlockNewLine() const -> bool
        {
            if (currentBlockNumber != text::npos) {
                if (currentBlockNumber > 0) {
                    return (--currentBlock())->getEnd() == TextBlock::End::Newline;
                }
                else {
                    return false;
                }
            }
            else {
                return false;
            }
        }

        [[nodiscard]] auto checkAndInvalidateBlockChanged() -> bool
        {
            if (blockChanged) {
                blockChanged = false;
                return true;
            }
            return false;
        }

        [[nodiscard]] auto checkLastNewLine() -> bool
        {
            if (!emptyNewLineAdded && checkPreviousBlockNewLine() &&
                currentBlock()->length() + (currentBlock()->getEnd() != TextBlock::End::Newline ? 0 : -1) == 0) {

                emptyNewLineAdded = true;
                return true;
            }
            return false;
        }

        [[nodiscard]] auto checkPreviousBlockNoNewLine() const -> bool
        {
            if (currentBlockNumber != text::npos) {
                if (currentBlockNumber > 0) {
                    return (--currentBlock())->getEnd() == TextBlock::End::None;
                }
                else {
                    return false;
                }
            }
            else {
                return false;
            }
        }

        [[nodiscard]] auto checkCurrentBlockNoNewLine() const -> bool
        {
            if (currentBlockNumber != text::npos && currentBlock() != blocksEnd()) {
                return (currentBlock())->getEnd() == TextBlock::End::None;
            }
            else {
                return false;
            }
        }

        [[nodiscard]] auto atBegin() const -> bool
        {
            if (document == nullptr || checkNpos()) {
                return false;
            }
            return pos == 0 && currentBlockNumber == 0;
        }

        [[nodiscard]] auto atEnd() const -> bool;

        operator bool() const
        {
            return !checkNpos();
        }

        auto goToNextBlock() -> BlockCursor &;
        auto goToPreviousBlock() -> BlockCursor &;

        auto checkIfBlockFirstAndEmpty(std::list<TextBlock>::iterator block) -> bool;
        auto checkIfNextBlockEmpty(std::list<TextBlock>::iterator nextBlock) -> bool;

        auto operator+=(unsigned int) -> BlockCursor &;
        auto operator++() -> BlockCursor &;
        auto operator-=(unsigned int) -> BlockCursor &;
        auto operator--() -> BlockCursor &;

        // return if handled ( this is not i.e. at begin/end)
        virtual auto removeChar() -> bool;
        auto operator*() -> const TextBlock &;
        auto operator->() -> const TextBlock *;

        virtual void addChar(uint32_t utf_val);
        void addTextBlock(TextBlock &&);

        [[nodiscard]] auto getText() -> std::string;
        auto getUTF8Text() -> UTF8;

        /// iterable
        /// {
        auto begin() -> std::list<TextBlock>::iterator;
        auto end() -> std::list<TextBlock>::iterator;
        /// }
    };
} // namespace gui