~aleteoryx/muditaos

ref: 1d2f5cf7a49cf1fb9463d289daa1492a2bec2d1d muditaos/module-gui/gui/widgets/TextCursor.hpp -rw-r--r-- 2.8 KiB
1d2f5cf7 — Piotr Tański [EGD-7754] Dates bumped in disclaimers 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
// 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 "Common.hpp"
#include "Rect.hpp"
#include "TextConstants.hpp"
#include "TextDocument.hpp"
#include <memory>

namespace gui
{
    class Text;
    class TextDocument;

    /// TextCursor - element joining Visible cursor element on screen ( via Rect )
    /// movement on TextDocument ( via BlockCursor )
    /// and position in gui::Text::Lines shown on screen
    class TextCursor : public Rect, public BlockCursor
    {
      protected:
        Text *text                              = nullptr;
        CursorStartPosition cursorStartPosition = CursorStartPosition::DocumentEnd;
        unsigned int onScreenPosition           = 0;

      public:
        static const unsigned int defaultWidth;
        enum class Move
        {
            Start, /// we are text `0`
            End,   /// we hit end of document
            Up,    /// we moved up a line
            Down,  /// we moved down a line
            Left,  /// we moved left inline
            Right, /// we moved right inline

            Error, /// error - now not implemented
        };

        explicit TextCursor(gui::Text *parent, unsigned int pos = text::npos, unsigned int block = text::npos);
        TextCursor() = delete;

        /// Up Down - end of line movement like in vi
        /// - moves on TextBlock ( TextDocument really... ) cursor to know where we are
        /// - moves gui::Text relative to TextDocument `pos_on_screen` value
        /// - informs that we changed line when needed - TODO think about it better... ( and if it's needed...?)
        /// - with_update - updates position in parent ( if false not - means we handled it already with i.e. addChar or
        /// removeChar)
        virtual Move moveCursor(NavigationDirection direction);
        virtual Move moveCursor(NavigationDirection direction, unsigned int n);

        // TODO note to self - here should be too UTF8 char handling, not in document...
        // cursor can pass processing char directly to TextBlock we are interested in...
        // so this should be in BlockCursor in reality
        auto getSelectedLine() -> std::tuple<const TextLine *, unsigned int, unsigned int>;
        void updateView();

        void addChar(uint32_t utf_val) override;
        TextCursor &operator<<(const UTF8 &);
        TextCursor &operator<<(const TextBlock &);
        bool removeChar() override;

        auto setCursorStartPosition(CursorStartPosition val) -> void;

        [[nodiscard]] auto getOnScreenPosition() const
        {
            return onScreenPosition;
        }
        [[nodiscard]] auto getAbsolutePosition() const -> unsigned int;
    };
} // namespace gui

const char *c_str(enum gui::TextCursor::Move);