~aleteoryx/muditaos

ref: 07b243a08489c5da81666959d506fe64ff23a501 muditaos/module-gui/gui/widgets/TextBlockCursor.cpp -rw-r--r-- 6.8 KiB
07b243a0 — Adam Egd-3408 Rich text (#712) 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include "TextBlockCursor.hpp"
#include "TextBlock.hpp"
#include "TextDocument.hpp"
#include "TextParse.hpp"
#include "log/log.hpp"
#include <algorithm>
#include <cassert>
#include <iterator>

static const int last_char_inclusive = 0; // if then -1 / else 0

#define debug_cursor(...)
// #define debug_cursor(...) LOG_DEBUG(__VA_ARGS__)

namespace gui
{
    auto BlockCursor::currentBlock()
    {
        if (block_nr == text::npos) {
            return std::end(document->blocks);
        }
        return std::next(document->blocks.begin(), block_nr);
    }

    auto BlockCursor::blocksEnd()
    {
        return std::end(document->blocks);
    }

    auto BlockCursor::blocksBegin()
    {
        return std::begin(document->blocks);
    }

    BlockCursor::BlockCursor(TextDocument *document, unsigned int pos, unsigned int block_nr, RawFont *default_font)
        : document(document), default_font(default_font)
    {
        this->pos      = text::npos;
        this->block_nr = text::npos;
        if (checkDocument()) {
            this->block_nr = block_nr < document->blocks.size() ? block_nr : document->blocks.size() - 1;
            auto block_len = std::next(document->blocks.begin(), this->block_nr)->length();
            this->pos      = pos < block_len ? pos : block_len + last_char_inclusive;
        }
    }

    [[nodiscard]] auto BlockCursor::checkNpos() const -> bool
    {
        return pos == text::npos || block_nr == text::npos;
    }

    void BlockCursor::resetNpos()
    {
        if (pos == text::npos)
            pos = 0;
        if (block_nr == text::npos)
            block_nr = 0;
    }

    [[nodiscard]] auto BlockCursor::checkDocument() const -> bool
    {
        return document != nullptr && document->blocks.size() > 0;
    }

    auto BlockCursor::atEnd() const -> bool
    {
        if (!checkDocument() || checkNpos()) {
            return false;
        }
        auto lastBlock = document->blocks.back();
        return block_nr == document->blocks.size() - 1 &&
               pos >= lastBlock.length() + (lastBlock.getEnd() != TextBlock::End::Newline ? last_char_inclusive : -1);
        ;
    }

    auto BlockCursor::operator+=(unsigned int val) -> BlockCursor &
    {
        // just use operator ++
        for (unsigned int i = 0; i < val; ++i, ++*this) {}
        return *this;
    }

    BlockCursor &BlockCursor::operator++()
    {
        if (!checkDocument()) {
            return *this;
        }
        else {
        }

        resetNpos();

        auto block = std::next(document->blocks.begin(), block_nr);

        bool end_of_current_block_reached =
            pos >= std::next(document->blocks.begin(), block_nr)->length() +
                       (block->getEnd() != TextBlock::End::Newline ? last_char_inclusive : -1);
        ;
        bool last_document_reached = block_nr + 1 == document->blocks.size();

        if (end_of_current_block_reached && last_document_reached) {
            return *this;
        }

        if (end_of_current_block_reached) {
            block_nr += 1;
            pos = 0;
            return *this;
        }

        pos += 1;
        return *this;
    }

    auto BlockCursor::operator-=(unsigned int val) -> BlockCursor &
    {
        // just use operator --
        for (unsigned int i = 0; i < val; ++i, --*this) {}
        return *this;
    }

    BlockCursor &BlockCursor::operator--()
    {
        if (!checkDocument()) {
            return *this;
        }

        resetNpos();

        if (pos == 0 && block_nr == 0) {
            return *this;
        }

        if (pos == 0) {
            block_nr -= 1;
            auto block = std::next(document->blocks.begin(), block_nr);
            pos        = block->length() + last_char_inclusive;
            if (block->getEnd() == TextBlock::End::Newline) {
                pos -= 1;
            }
            return *this;
        }

        pos -= 1;

        return *this;
    } // namespace gui

    void BlockCursor::addChar(uint32_t utf_val)
    {
        if (document->blocks.size() == 0) {
            if (utf_val == text::newline) {

                /*
                 * Adding new line to empty document means adding new block with only new line and imidiatelly jumps to
                 * another block
                 */
                document->append(TextBlock("", default_font, TextBlock::End::Newline));
                document->append(TextBlock("", default_font, TextBlock::End::None));

                pos      = 0;
                block_nr = 1;

                return;
            }
            document->append(TextBlock("", default_font, TextBlock::End::None));
            block_nr = 0;
            pos      = 0;
        }
        auto block = currentBlock();
        if (block == blocksEnd()) {
            LOG_ERROR("add char to document with no text blocks shouldn't ever happen");
            return;
        }
        if (utf_val == text::newline) {
            document->addNewline(*this, TextBlock::End::Newline);
            return;
        }
        block->addChar(utf_val, pos);
    }

    void BlockCursor::addTextBlock(TextBlock &&textblock)
    {
        if (textblock.length() == 0) {
            return;
        }
        if (document->isEmpty()) {
            block_nr = 0;
            pos      = 0;
        }
        document->append(std::move(textblock));
    }

    bool BlockCursor::removeChar()
    {
        if (checkNpos()) {
            LOG_ERROR("cant remove from not initialized/empty cursor");
            return false;
        }

        auto block     = currentBlock();
        auto prevBlock = block;
        auto nextBlock = block;

        if (block != blocksBegin()) {
            prevBlock--;
        }
        if (block != blocksEnd()) {
            nextBlock++;
        }

        if (block == blocksEnd()) {
            LOG_ERROR("removing char from document with no TextBlocks");
            return false;
        }

        if (nextBlock != blocksEnd() && nextBlock->isEmpty() && block->getEnd() == TextBlock::End::Newline) {
            document->removeBlock(nextBlock);
            block->removeChar(pos);
            return true;
        }

        debug_cursor("From block: [%d], remove pos: [%d] block length [%d]", getBlockNr(), pos, block->length());
        block->removeChar(pos);

        if (block->isEmpty() && block != blocksEnd() && prevBlock->getEnd() != TextBlock::End::Newline) {
            debug_cursor("empty block removed");
            document->removeBlock(block);
        }

        return true;
    }

    const TextBlock &BlockCursor::operator*()
    {
        return *currentBlock();
    }

    auto BlockCursor::begin() -> std::list<TextBlock>::iterator
    {
        return document == nullptr ? document->blocks.end() : document->blocks.begin();
    }

    auto BlockCursor::end() -> std::list<TextBlock>::iterator
    {
        return document->blocks.end();
    }
} // namespace gui