summaryrefslogtreecommitdiff
path: root/Libraries/LibGUI/GTextEditor.h
blob: 3caa350924d68f4373bef449a7b73ec00841e696 (plain)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#pragma once

#include <AK/Function.h>
#include <AK/HashMap.h>
#include <AK/NonnullOwnPtrVector.h>
#include <AK/NonnullRefPtrVector.h>
#include <LibDraw/TextAlignment.h>
#include <LibGUI/GScrollableWidget.h>

class GAction;
class GMenu;
class GScrollBar;
class Painter;

enum class ShouldWrapAtEndOfDocument { No = 0, Yes };
enum class ShouldWrapAtStartOfDocument { No = 0, Yes };

class GTextPosition {
public:
    GTextPosition() {}
    GTextPosition(int line, int column)
        : m_line(line)
        , m_column(column)
    {
    }

    bool is_valid() const { return m_line >= 0 && m_column >= 0; }

    int line() const { return m_line; }
    int column() const { return m_column; }

    void set_line(int line) { m_line = line; }
    void set_column(int column) { m_column = column; }

    bool operator==(const GTextPosition& other) const { return m_line == other.m_line && m_column == other.m_column; }
    bool operator!=(const GTextPosition& other) const { return m_line != other.m_line || m_column != other.m_column; }
    bool operator<(const GTextPosition& other) const { return m_line < other.m_line || (m_line == other.m_line && m_column < other.m_column); }

private:
    int m_line { -1 };
    int m_column { -1 };
};

class GTextRange {
public:
    GTextRange() {}
    GTextRange(const GTextPosition& start, const GTextPosition& end)
        : m_start(start)
        , m_end(end)
    {
    }

    bool is_valid() const { return m_start.is_valid() && m_end.is_valid(); }
    void clear()
    {
        m_start = {};
        m_end = {};
    }

    GTextPosition& start() { return m_start; }
    GTextPosition& end() { return m_end; }
    const GTextPosition& start() const { return m_start; }
    const GTextPosition& end() const { return m_end; }

    GTextRange normalized() const { return GTextRange(normalized_start(), normalized_end()); }

    void set_start(const GTextPosition& position) { m_start = position; }
    void set_end(const GTextPosition& position) { m_end = position; }

    void set(const GTextPosition& start, const GTextPosition& end)
    {
        m_start = start;
        m_end = end;
    }

    bool operator==(const GTextRange& other) const
    {
        return m_start == other.m_start && m_end == other.m_end;
    }

private:
    GTextPosition normalized_start() const { return m_start < m_end ? m_start : m_end; }
    GTextPosition normalized_end() const { return m_start < m_end ? m_end : m_start; }

    GTextPosition m_start;
    GTextPosition m_end;
};

class GTextEditor : public GScrollableWidget {
    C_OBJECT(GTextEditor)
public:
    enum Type {
        MultiLine,
        SingleLine
    };
    virtual ~GTextEditor() override;

    bool is_readonly() const { return m_readonly; }
    void set_readonly(bool);

    bool is_automatic_indentation_enabled() const { return m_automatic_indentation_enabled; }
    void set_automatic_indentation_enabled(bool enabled) { m_automatic_indentation_enabled = enabled; }

    bool is_line_wrapping_enabled() const { return m_line_wrapping_enabled; }
    void set_line_wrapping_enabled(bool);

    TextAlignment text_alignment() const { return m_text_alignment; }
    void set_text_alignment(TextAlignment);

    Type type() const { return m_type; }
    bool is_single_line() const { return m_type == SingleLine; }
    bool is_multi_line() const { return m_type == MultiLine; }

    bool is_ruler_visible() const { return m_ruler_visible; }
    void set_ruler_visible(bool b) { m_ruler_visible = b; }

    Function<void()> on_cursor_change;
    Function<void()> on_selection_change;

    void set_text(const StringView&);
    void scroll_cursor_into_view();
    void scroll_position_into_view(const GTextPosition&);
    int line_count() const { return m_lines.size(); }
    int line_spacing() const { return m_line_spacing; }
    int line_height() const { return font().glyph_height() + m_line_spacing; }
    GTextPosition cursor() const { return m_cursor; }
    GTextRange normalized_selection() const { return m_selection.normalized(); }
    // FIXME: This should take glyph spacing into account, no?
    int glyph_width() const { return font().glyph_width('x'); }

    bool write_to_file(const StringView& path);

    GTextRange find_next(const StringView&, const GTextPosition& start = {});
    GTextRange find_prev(const StringView&, const GTextPosition& start = {});

    GTextPosition next_position_after(const GTextPosition&, ShouldWrapAtEndOfDocument = ShouldWrapAtEndOfDocument::Yes);
    GTextPosition prev_position_before(const GTextPosition&, ShouldWrapAtStartOfDocument = ShouldWrapAtStartOfDocument::Yes);

    bool has_selection() const { return m_selection.is_valid(); }
    String selected_text() const;
    void set_selection(const GTextRange&);

    String text() const;

    void clear();

    void cut();
    void copy();
    void paste();
    void do_delete();
    void delete_current_line();
    void select_all();

    Function<void()> on_change;
    Function<void()> on_return_pressed;
    Function<void()> on_escape_pressed;

    GAction& undo_action() { return *m_undo_action; }
    GAction& redo_action() { return *m_redo_action; }
    GAction& cut_action() { return *m_cut_action; }
    GAction& copy_action() { return *m_copy_action; }
    GAction& paste_action() { return *m_paste_action; }
    GAction& delete_action() { return *m_delete_action; }

    void add_custom_context_menu_action(GAction&);

protected:
    GTextEditor(Type, GWidget* parent);

    virtual void did_change_font() override;
    virtual void paint_event(GPaintEvent&) override;
    virtual void mousedown_event(GMouseEvent&) override;
    virtual void mouseup_event(GMouseEvent&) override;
    virtual void mousemove_event(GMouseEvent&) override;
    virtual void doubleclick_event(GMouseEvent&) override;
    virtual void keydown_event(GKeyEvent&) override;
    virtual void focusin_event(CEvent&) override;
    virtual void focusout_event(CEvent&) override;
    virtual void timer_event(CTimerEvent&) override;
    virtual bool accepts_focus() const override { return true; }
    virtual void enter_event(CEvent&) override;
    virtual void leave_event(CEvent&) override;
    virtual void context_menu_event(GContextMenuEvent&) override;
    virtual void resize_event(GResizeEvent&) override;

private:

    void create_actions();
    void paint_ruler(Painter&);
    void update_content_size();
    void did_change();

    class Line {
        friend class GTextEditor;

    public:
        explicit Line(GTextEditor&);
        Line(GTextEditor&, const StringView&);

        StringView view() const { return { characters(), length() }; }
        const char* characters() const { return m_text.data(); }
        int length() const { return m_text.size() - 1; }
        void set_text(const StringView&);
        void append(char);
        void prepend(char);
        void insert(int index, char);
        void remove(int index);
        void append(const char*, int);
        void truncate(int length);
        void clear();
        void recompute_visual_lines();
        int visual_line_containing(int column) const;

        template<typename Callback>
        void for_each_visual_line(Callback) const;

    private:
        GTextEditor& m_editor;

        // NOTE: This vector is null terminated.
        Vector<char> m_text;

        Vector<int, 1> m_visual_line_breaks;
        Rect m_visual_rect;
    };

    Rect line_content_rect(int item_index) const;
    Rect line_widget_rect(int line_index) const;
    Rect cursor_content_rect() const;
    Rect content_rect_for_position(const GTextPosition&) const;
    void update_cursor();
    void set_cursor(int line, int column);
    void set_cursor(const GTextPosition&);
    Line& current_line() { return m_lines[m_cursor.line()]; }
    const Line& current_line() const { return m_lines[m_cursor.line()]; }
    GTextPosition text_position_at(const Point&) const;
    void insert_at_cursor(char);
    void insert_at_cursor(const StringView&);
    int ruler_width() const;
    Rect ruler_content_rect(int line) const;
    void toggle_selection_if_needed_for_event(const GKeyEvent&);
    void insert_at_cursor_or_replace_selection(const StringView&);
    void delete_selection();
    void did_update_selection();
    int content_x_for_position(const GTextPosition&) const;
    char character_at(const GTextPosition&) const;
    Rect ruler_rect_in_inner_coordinates() const;
    Rect visible_text_rect_in_inner_coordinates() const;
    void recompute_all_visual_lines();

    Type m_type { MultiLine };

    NonnullOwnPtrVector<Line> m_lines;
    GTextPosition m_cursor;
    TextAlignment m_text_alignment { TextAlignment::CenterLeft };
    bool m_cursor_state { true };
    bool m_in_drag_select { false };
    bool m_ruler_visible { false };
    bool m_have_pending_change_notification { false };
    bool m_automatic_indentation_enabled { false };
    bool m_line_wrapping_enabled { false };
    bool m_readonly { false };
    int m_line_spacing { 4 };
    int m_soft_tab_width { 4 };
    int m_horizontal_content_padding { 2 };
    GTextRange m_selection;
    OwnPtr<GMenu> m_context_menu;
    RefPtr<GAction> m_undo_action;
    RefPtr<GAction> m_redo_action;
    RefPtr<GAction> m_cut_action;
    RefPtr<GAction> m_copy_action;
    RefPtr<GAction> m_paste_action;
    RefPtr<GAction> m_delete_action;
    CElapsedTimer m_triple_click_timer;
    NonnullRefPtrVector<GAction> m_custom_context_menu_actions;
};

inline const LogStream& operator<<(const LogStream& stream, const GTextPosition& value)
{
    if (!value.is_valid())
        return stream << "GTextPosition(Invalid)";
    return stream << String::format("(%d,%d)", value.line(), value.column());
}

inline const LogStream& operator<<(const LogStream& stream, const GTextRange& value)
{
    if (!value.is_valid())
        return stream << "GTextRange(Invalid)";
    return stream << value.start() << '-' << value.end();
}