summaryrefslogtreecommitdiff
path: root/Libraries/LibGUI/TextEditor.h
blob: 175361487d915c8cd327dfd1fe0b7f60591df678 (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#pragma once

#include <AK/Function.h>
#include <AK/NonnullOwnPtrVector.h>
#include <AK/NonnullRefPtrVector.h>
#include <LibCore/ElapsedTimer.h>
#include <LibGUI/ScrollableWidget.h>
#include <LibGUI/TextDocument.h>
#include <LibGUI/TextRange.h>
#include <LibGfx/TextAlignment.h>

namespace GUI {

class TextEditor
    : public ScrollableWidget
    , public TextDocument::Client {
    C_OBJECT(TextEditor)
public:
    enum Type {
        MultiLine,
        SingleLine
    };

    enum Mode {
        Editable,
        ReadOnly,
        DisplayOnly
    };

    virtual ~TextEditor() override;

    const TextDocument& document() const { return *m_document; }
    TextDocument& document() { return *m_document; }

    virtual void set_document(TextDocument&);

    const String& placeholder() const { return m_placeholder; }
    void set_placeholder(const StringView& placeholder) { m_placeholder = placeholder; }

    void set_visualize_trailing_whitespace(bool);
    bool visualize_trailing_whitespace() const { return m_visualize_trailing_whitespace; }

    bool has_visible_list() const { return m_has_visible_list; }
    void set_has_visible_list(bool);
    bool has_open_button() const { return m_has_open_button; }
    void set_has_open_button(bool);

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

    virtual int soft_tab_width() const final { return m_soft_tab_width; }

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

    Gfx::TextAlignment text_alignment() const { return m_text_alignment; }
    void set_text_alignment(Gfx::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; }

    Mode mode() const { return m_mode; }
    bool is_editable() const { return m_mode == Editable; }
    bool is_readonly() const { return m_mode == ReadOnly; }
    bool is_displayonly() const { return m_mode == DisplayOnly; }
    void set_mode(const Mode);

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

    void set_icon(const Gfx::Bitmap*);
    const Gfx::Bitmap* icon() const { return m_icon; }

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

    void set_text(const StringView&);
    void scroll_cursor_into_view();
    void scroll_position_into_view(const TextPosition&);
    size_t line_count() const { return document().line_count(); }
    int line_spacing() const { return m_line_spacing; }
    int line_height() const;
    TextPosition cursor() const { return m_cursor; }
    TextRange normalized_selection() const { return m_selection.normalized(); }

    void insert_at_cursor_or_replace_selection(const StringView&);
    bool write_to_file(const StringView& path);
    bool has_selection() const { return m_selection.is_valid(); }
    String selected_text() const;
    void set_selection(const TextRange&);
    void clear_selection();
    bool can_undo() const { return document().can_undo(); }
    bool can_redo() const { return document().can_redo(); }

    String text() const;

    void clear();

    void cut();
    void copy();
    void paste();
    void do_delete();
    void delete_current_line();
    void select_all();
    void undo() { document().undo(); }
    void redo() { document().redo(); }

    Function<void()> on_change;
    Function<void()> on_mousedown;
    Function<void()> on_return_pressed;
    Function<void()> on_escape_pressed;
    Function<void()> on_up_pressed;
    Function<void()> on_down_pressed;
    Function<void()> on_pageup_pressed;
    Function<void()> on_pagedown_pressed;

    Action& undo_action() { return *m_undo_action; }
    Action& redo_action() { return *m_redo_action; }
    Action& cut_action() { return *m_cut_action; }
    Action& copy_action() { return *m_copy_action; }
    Action& paste_action() { return *m_paste_action; }
    Action& delete_action() { return *m_delete_action; }
    Action& go_to_line_action() { return *m_go_to_line_action; }
    Action& select_all_action() { return *m_select_all_action; }

    void add_custom_context_menu_action(Action&);

    void set_cursor(size_t line, size_t column);
    void set_cursor(const TextPosition&);

    const SyntaxHighlighter* syntax_highlighter() const;
    void set_syntax_highlighter(OwnPtr<SyntaxHighlighter>);

    bool is_in_drag_select() const { return m_in_drag_select; }

protected:
    explicit TextEditor(Type = Type::MultiLine);

    virtual void did_change_font() override;
    virtual void paint_event(PaintEvent&) override;
    virtual void mousedown_event(MouseEvent&) override;
    virtual void mouseup_event(MouseEvent&) override;
    virtual void mousemove_event(MouseEvent&) override;
    virtual void doubleclick_event(MouseEvent&) override;
    virtual void keydown_event(KeyEvent&) override;
    virtual void focusin_event(FocusEvent&) override;
    virtual void focusout_event(FocusEvent&) override;
    virtual void timer_event(Core::TimerEvent&) override;
    virtual bool accepts_focus() const override { return true; }
    virtual void enter_event(Core::Event&) override;
    virtual void leave_event(Core::Event&) override;
    virtual void context_menu_event(ContextMenuEvent&) override;
    virtual void resize_event(ResizeEvent&) override;
    virtual void theme_change_event(ThemeChangeEvent&) override;
    virtual void cursor_did_change() {}
    Gfx::IntRect ruler_content_rect(size_t line) const;

    TextPosition text_position_at(const Gfx::IntPoint&) const;
    bool ruler_visible() const { return m_ruler_visible; }
    Gfx::IntRect content_rect_for_position(const TextPosition&) const;
    int ruler_width() const;

private:
    friend class TextDocumentLine;

    // ^TextDocument::Client
    virtual void document_did_append_line() override;
    virtual void document_did_insert_line(size_t) override;
    virtual void document_did_remove_line(size_t) override;
    virtual void document_did_remove_all_lines() override;
    virtual void document_did_change() override;
    virtual void document_did_set_text() override;
    virtual void document_did_set_cursor(const TextPosition&) override;

    void create_actions();
    void paint_ruler(Painter&);
    void update_content_size();
    void did_change();
    int fixed_glyph_width() const;

    void defer_reflow();
    void undefer_reflow();

    int icon_size() const { return 16; }
    int icon_padding() const { return 2; }
    int button_padding() const { return m_has_open_button ? 17 : 2; }

    class ReflowDeferrer {
    public:
        ReflowDeferrer(TextEditor& editor)
            : m_editor(editor)
        {
            m_editor.defer_reflow();
        }
        ~ReflowDeferrer()
        {
            m_editor.undefer_reflow();
        }

    private:
        TextEditor& m_editor;
    };

    Gfx::IntRect line_content_rect(size_t item_index) const;
    Gfx::IntRect line_widget_rect(size_t line_index) const;
    Gfx::IntRect cursor_content_rect() const;
    void update_cursor();
    const NonnullOwnPtrVector<TextDocumentLine>& lines() const { return document().lines(); }
    NonnullOwnPtrVector<TextDocumentLine>& lines() { return document().lines(); }
    TextDocumentLine& line(size_t index) { return document().line(index); }
    const TextDocumentLine& line(size_t index) const { return document().line(index); }
    TextDocumentLine& current_line() { return line(m_cursor.line()); }
    const TextDocumentLine& current_line() const { return line(m_cursor.line()); }
    void toggle_selection_if_needed_for_event(const KeyEvent&);
    void delete_selection();
    void did_update_selection();
    int content_x_for_position(const TextPosition&) const;
    Gfx::IntRect ruler_rect_in_inner_coordinates() const;
    Gfx::IntRect visible_text_rect_in_inner_coordinates() const;
    void recompute_all_visual_lines();
    void ensure_cursor_is_valid();
    void flush_pending_change_notification_if_needed();
    void get_selection_line_boundaries(size_t& first_line, size_t& last_line);
    void move_selected_lines_up();
    void move_selected_lines_down();
    void sort_selected_lines();

    size_t visual_line_containing(size_t line_index, size_t column) const;
    void recompute_visual_lines(size_t line_index);

    void automatic_selection_scroll_timer_fired();

    template<class T, class... Args>
    inline void execute(Args&&... args)
    {
        auto command = make<T>(*m_document, forward<Args>(args)...);
        command->execute_from(*this);
        m_document->add_to_undo_stack(move(command));
    }

    Type m_type { MultiLine };
    Mode m_mode { Editable };

    TextPosition m_cursor;
    Gfx::TextAlignment m_text_alignment { Gfx::TextAlignment::CenterLeft };
    bool m_cursor_state { true };
    bool m_in_drag_select { false };
    bool m_ruler_visible { false };
    bool m_has_pending_change_notification { false };
    bool m_automatic_indentation_enabled { false };
    bool m_line_wrapping_enabled { false };
    bool m_has_visible_list { false };
    bool m_has_open_button { false };
    bool m_visualize_trailing_whitespace { true };
    int m_line_spacing { 4 };
    size_t m_soft_tab_width { 4 };
    int m_horizontal_content_padding { 3 };
    TextRange m_selection;
    RefPtr<Menu> m_context_menu;
    RefPtr<Action> m_undo_action;
    RefPtr<Action> m_redo_action;
    RefPtr<Action> m_cut_action;
    RefPtr<Action> m_copy_action;
    RefPtr<Action> m_paste_action;
    RefPtr<Action> m_delete_action;
    RefPtr<Action> m_go_to_line_action;
    RefPtr<Action> m_select_all_action;
    Core::ElapsedTimer m_triple_click_timer;
    NonnullRefPtrVector<Action> m_custom_context_menu_actions;

    size_t m_reflow_deferred { 0 };
    bool m_reflow_requested { false };

    bool is_visual_data_up_to_date() const { return !m_reflow_requested; }

    RefPtr<TextDocument> m_document;

    String m_placeholder { "" };

    template<typename Callback>
    void for_each_visual_line(size_t line_index, Callback) const;

    struct LineVisualData {
        Vector<size_t, 1> visual_line_breaks;
        Gfx::IntRect visual_rect;
    };

    NonnullOwnPtrVector<LineVisualData> m_line_visual_data;

    OwnPtr<SyntaxHighlighter> m_highlighter;

    RefPtr<Core::Timer> m_automatic_selection_scroll_timer;
    Gfx::IntPoint m_last_mousemove_position;

    RefPtr<Gfx::Bitmap> m_icon;
};

}