2019-03-07 02:31:06 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Function.h>
|
|
|
|
#include <AK/HashMap.h>
|
2019-07-24 10:12:23 +03:00
|
|
|
#include <AK/NonnullOwnPtrVector.h>
|
2019-08-25 22:33:08 +03:00
|
|
|
#include <AK/NonnullRefPtrVector.h>
|
2019-11-06 01:37:47 +03:00
|
|
|
#include <LibCore/CTimer.h>
|
2019-07-18 11:15:00 +03:00
|
|
|
#include <LibDraw/TextAlignment.h>
|
2019-07-24 10:12:23 +03:00
|
|
|
#include <LibGUI/GScrollableWidget.h>
|
2019-10-27 18:10:07 +03:00
|
|
|
#include <LibGUI/GTextDocument.h>
|
2019-10-27 13:23:53 +03:00
|
|
|
#include <LibGUI/GTextRange.h>
|
2019-03-07 02:31:06 +03:00
|
|
|
|
2019-04-18 13:25:00 +03:00
|
|
|
class GAction;
|
|
|
|
class GMenu;
|
2019-03-07 02:31:06 +03:00
|
|
|
class GScrollBar;
|
2019-03-15 19:54:05 +03:00
|
|
|
class Painter;
|
2019-03-07 02:31:06 +03:00
|
|
|
|
2019-10-27 20:00:07 +03:00
|
|
|
class GTextEditor
|
|
|
|
: public GScrollableWidget
|
|
|
|
, public GTextDocument::Client {
|
2019-07-25 20:49:28 +03:00
|
|
|
C_OBJECT(GTextEditor)
|
2019-03-07 02:31:06 +03:00
|
|
|
public:
|
2019-06-07 18:13:23 +03:00
|
|
|
enum Type {
|
2019-05-28 12:53:16 +03:00
|
|
|
MultiLine,
|
|
|
|
SingleLine
|
|
|
|
};
|
2019-03-07 02:31:06 +03:00
|
|
|
virtual ~GTextEditor() override;
|
|
|
|
|
2019-10-27 18:10:07 +03:00
|
|
|
const GTextDocument& document() const { return *m_document; }
|
|
|
|
GTextDocument& document() { return *m_document; }
|
|
|
|
|
2019-10-27 21:36:59 +03:00
|
|
|
void set_document(GTextDocument&);
|
|
|
|
|
2019-05-08 06:00:28 +03:00
|
|
|
bool is_readonly() const { return m_readonly; }
|
|
|
|
void set_readonly(bool);
|
|
|
|
|
2019-08-21 20:33:54 +03:00
|
|
|
bool is_automatic_indentation_enabled() const { return m_automatic_indentation_enabled; }
|
2019-04-25 23:56:09 +03:00
|
|
|
void set_automatic_indentation_enabled(bool enabled) { m_automatic_indentation_enabled = enabled; }
|
|
|
|
|
2019-08-25 09:43:01 +03:00
|
|
|
bool is_line_wrapping_enabled() const { return m_line_wrapping_enabled; }
|
2019-08-25 13:23:14 +03:00
|
|
|
void set_line_wrapping_enabled(bool);
|
2019-08-25 09:43:01 +03:00
|
|
|
|
2019-04-24 23:24:16 +03:00
|
|
|
TextAlignment text_alignment() const { return m_text_alignment; }
|
|
|
|
void set_text_alignment(TextAlignment);
|
|
|
|
|
2019-03-15 19:37:13 +03:00
|
|
|
Type type() const { return m_type; }
|
|
|
|
bool is_single_line() const { return m_type == SingleLine; }
|
|
|
|
bool is_multi_line() const { return m_type == MultiLine; }
|
|
|
|
|
2019-04-11 05:01:17 +03:00
|
|
|
bool is_ruler_visible() const { return m_ruler_visible; }
|
|
|
|
void set_ruler_visible(bool b) { m_ruler_visible = b; }
|
|
|
|
|
2019-04-10 04:08:29 +03:00
|
|
|
Function<void()> on_cursor_change;
|
2019-04-12 03:52:34 +03:00
|
|
|
Function<void()> on_selection_change;
|
2019-03-07 02:31:06 +03:00
|
|
|
|
2019-06-02 15:58:02 +03:00
|
|
|
void set_text(const StringView&);
|
2019-03-07 15:13:25 +03:00
|
|
|
void scroll_cursor_into_view();
|
2019-08-21 22:23:17 +03:00
|
|
|
void scroll_position_into_view(const GTextPosition&);
|
2019-10-27 18:10:07 +03:00
|
|
|
int line_count() const { return document().line_count(); }
|
2019-03-07 03:05:35 +03:00
|
|
|
int line_spacing() const { return m_line_spacing; }
|
|
|
|
int line_height() const { return font().glyph_height() + m_line_spacing; }
|
2019-03-07 02:46:29 +03:00
|
|
|
GTextPosition cursor() const { return m_cursor; }
|
2019-03-08 20:28:24 +03:00
|
|
|
GTextRange normalized_selection() const { return m_selection.normalized(); }
|
2019-04-24 23:46:27 +03:00
|
|
|
// FIXME: This should take glyph spacing into account, no?
|
2019-03-07 16:02:10 +03:00
|
|
|
int glyph_width() const { return font().glyph_width('x'); }
|
2019-03-07 02:31:06 +03:00
|
|
|
|
2019-06-02 15:58:02 +03:00
|
|
|
bool write_to_file(const StringView& path);
|
2019-03-07 19:06:11 +03:00
|
|
|
|
2019-03-08 20:28:24 +03:00
|
|
|
bool has_selection() const { return m_selection.is_valid(); }
|
2019-03-08 03:59:59 +03:00
|
|
|
String selected_text() const;
|
2019-08-21 22:23:17 +03:00
|
|
|
void set_selection(const GTextRange&);
|
2019-11-09 10:50:39 +03:00
|
|
|
bool can_undo() const { return m_undo_stack_index < m_undo_stack.size() && !m_undo_stack.is_empty(); }
|
|
|
|
bool can_redo() const { return m_undo_stack_index > 0 && m_undo_stack[m_undo_stack_index - 1].m_undo_vector.size() > 0 && !m_undo_stack.is_empty(); }
|
2019-08-21 22:23:17 +03:00
|
|
|
|
2019-03-15 19:37:13 +03:00
|
|
|
String text() const;
|
|
|
|
|
|
|
|
void clear();
|
2019-03-08 03:59:59 +03:00
|
|
|
|
2019-03-08 16:08:15 +03:00
|
|
|
void cut();
|
|
|
|
void copy();
|
|
|
|
void paste();
|
2019-03-21 01:11:00 +03:00
|
|
|
void do_delete();
|
2019-03-25 15:13:46 +03:00
|
|
|
void delete_current_line();
|
2019-06-22 11:38:38 +03:00
|
|
|
void select_all();
|
2019-11-02 22:22:49 +03:00
|
|
|
void undo();
|
|
|
|
void redo();
|
2019-03-08 16:08:15 +03:00
|
|
|
|
2019-04-09 17:20:36 +03:00
|
|
|
Function<void()> on_change;
|
2019-04-10 04:08:29 +03:00
|
|
|
Function<void()> on_return_pressed;
|
|
|
|
Function<void()> on_escape_pressed;
|
2019-03-15 19:37:13 +03:00
|
|
|
|
2019-04-18 13:25:00 +03:00
|
|
|
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; }
|
|
|
|
|
2019-08-25 22:33:08 +03:00
|
|
|
void add_custom_context_menu_action(GAction&);
|
|
|
|
|
2019-10-21 19:58:27 +03:00
|
|
|
void set_cursor(int line, int column);
|
|
|
|
void set_cursor(const GTextPosition&);
|
|
|
|
|
2019-09-01 13:26:35 +03:00
|
|
|
protected:
|
2019-11-10 12:58:03 +03:00
|
|
|
explicit GTextEditor(GWidget* parent);
|
|
|
|
explicit GTextEditor(Type, GWidget* parent);
|
2019-09-01 13:26:35 +03:00
|
|
|
|
2019-09-21 16:43:52 +03:00
|
|
|
virtual void did_change_font() override;
|
2019-03-07 02:31:06 +03:00
|
|
|
virtual void paint_event(GPaintEvent&) override;
|
|
|
|
virtual void mousedown_event(GMouseEvent&) override;
|
2019-03-08 19:53:02 +03:00
|
|
|
virtual void mouseup_event(GMouseEvent&) override;
|
|
|
|
virtual void mousemove_event(GMouseEvent&) override;
|
2019-04-25 23:29:25 +03:00
|
|
|
virtual void doubleclick_event(GMouseEvent&) override;
|
2019-03-07 02:31:06 +03:00
|
|
|
virtual void keydown_event(GKeyEvent&) override;
|
2019-04-10 17:56:55 +03:00
|
|
|
virtual void focusin_event(CEvent&) override;
|
|
|
|
virtual void focusout_event(CEvent&) override;
|
|
|
|
virtual void timer_event(CTimerEvent&) override;
|
2019-03-07 02:31:06 +03:00
|
|
|
virtual bool accepts_focus() const override { return true; }
|
2019-04-10 17:56:55 +03:00
|
|
|
virtual void enter_event(CEvent&) override;
|
|
|
|
virtual void leave_event(CEvent&) override;
|
2019-04-18 13:25:00 +03:00
|
|
|
virtual void context_menu_event(GContextMenuEvent&) override;
|
2019-04-25 00:42:49 +03:00
|
|
|
virtual void resize_event(GResizeEvent&) override;
|
2019-11-18 21:10:06 +03:00
|
|
|
virtual void cursor_did_change() {}
|
2019-04-01 00:52:02 +03:00
|
|
|
|
2019-10-29 23:37:46 +03:00
|
|
|
GTextPosition text_position_at(const Point&) const;
|
|
|
|
|
2019-09-21 16:43:52 +03:00
|
|
|
private:
|
2019-10-27 18:10:07 +03:00
|
|
|
friend class GTextDocumentLine;
|
|
|
|
|
2019-10-27 21:36:59 +03:00
|
|
|
// ^GTextDocument::Client
|
2019-10-27 20:00:07 +03:00
|
|
|
virtual void document_did_append_line() override;
|
|
|
|
virtual void document_did_insert_line(int) override;
|
|
|
|
virtual void document_did_remove_line(int) override;
|
|
|
|
virtual void document_did_remove_all_lines() override;
|
2019-10-27 21:36:59 +03:00
|
|
|
virtual void document_did_change() override;
|
2019-10-27 20:00:07 +03:00
|
|
|
|
2019-04-18 13:25:00 +03:00
|
|
|
void create_actions();
|
2019-03-15 19:54:05 +03:00
|
|
|
void paint_ruler(Painter&);
|
2019-03-16 18:54:51 +03:00
|
|
|
void update_content_size();
|
2019-04-09 17:20:36 +03:00
|
|
|
void did_change();
|
2019-03-07 02:31:06 +03:00
|
|
|
|
2019-03-07 15:21:51 +03:00
|
|
|
Rect line_content_rect(int item_index) const;
|
|
|
|
Rect line_widget_rect(int line_index) const;
|
|
|
|
Rect cursor_content_rect() const;
|
2019-08-21 22:23:17 +03:00
|
|
|
Rect content_rect_for_position(const GTextPosition&) const;
|
2019-03-07 15:21:51 +03:00
|
|
|
void update_cursor();
|
2019-11-02 22:22:49 +03:00
|
|
|
void update_undo_timer();
|
2019-10-27 18:10:07 +03:00
|
|
|
const NonnullOwnPtrVector<GTextDocumentLine>& lines() const { return document().lines(); }
|
|
|
|
NonnullOwnPtrVector<GTextDocumentLine>& lines() { return document().lines(); }
|
|
|
|
GTextDocumentLine& line(int index) { return document().line(index); }
|
|
|
|
const GTextDocumentLine& line(int index) const { return document().line(index); }
|
|
|
|
GTextDocumentLine& current_line() { return line(m_cursor.line()); }
|
|
|
|
const GTextDocumentLine& current_line() const { return line(m_cursor.line()); }
|
2019-03-07 17:03:38 +03:00
|
|
|
void insert_at_cursor(char);
|
2019-06-02 15:58:02 +03:00
|
|
|
void insert_at_cursor(const StringView&);
|
2019-03-07 22:05:05 +03:00
|
|
|
int ruler_width() const;
|
|
|
|
Rect ruler_content_rect(int line) const;
|
2019-03-08 02:49:45 +03:00
|
|
|
void toggle_selection_if_needed_for_event(const GKeyEvent&);
|
2019-06-02 15:58:02 +03:00
|
|
|
void insert_at_cursor_or_replace_selection(const StringView&);
|
2019-03-21 01:11:00 +03:00
|
|
|
void delete_selection();
|
2019-04-12 03:52:34 +03:00
|
|
|
void did_update_selection();
|
2019-04-24 23:24:16 +03:00
|
|
|
int content_x_for_position(const GTextPosition&) const;
|
2019-08-25 08:14:02 +03:00
|
|
|
Rect ruler_rect_in_inner_coordinates() const;
|
|
|
|
Rect visible_text_rect_in_inner_coordinates() const;
|
2019-08-25 09:43:01 +03:00
|
|
|
void recompute_all_visual_lines();
|
2019-11-06 01:37:47 +03:00
|
|
|
void ensure_cursor_is_valid();
|
2019-11-08 21:49:08 +03:00
|
|
|
void flush_pending_change_notification_if_needed();
|
2019-11-08 22:14:42 +03:00
|
|
|
void get_selection_line_boundaries(int& first_line, int& last_line);
|
|
|
|
void move_selected_lines_up();
|
|
|
|
void move_selected_lines_down();
|
2019-11-15 22:45:27 +03:00
|
|
|
void sort_selected_lines();
|
2019-03-07 15:21:51 +03:00
|
|
|
|
2019-11-02 22:22:49 +03:00
|
|
|
class UndoCommand {
|
|
|
|
|
|
|
|
public:
|
|
|
|
UndoCommand(GTextEditor& text_editor);
|
|
|
|
virtual ~UndoCommand();
|
|
|
|
virtual void undo();
|
|
|
|
virtual void redo();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
GTextEditor& m_text_editor;
|
|
|
|
};
|
|
|
|
|
|
|
|
class InsertCharacterCommand : public UndoCommand {
|
|
|
|
public:
|
|
|
|
InsertCharacterCommand(GTextEditor& text_editor, char ch, GTextPosition text_position);
|
|
|
|
virtual void undo() override;
|
|
|
|
virtual void redo() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
char m_character;
|
|
|
|
GTextPosition m_text_position;
|
|
|
|
};
|
|
|
|
|
|
|
|
class RemoveCharacterCommand : public UndoCommand {
|
|
|
|
public:
|
|
|
|
RemoveCharacterCommand(GTextEditor& text_editor, char ch, GTextPosition text_position);
|
|
|
|
virtual void undo() override;
|
|
|
|
virtual void redo() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
char m_character;
|
|
|
|
GTextPosition m_text_position;
|
|
|
|
};
|
|
|
|
|
|
|
|
class RemoveLineCommand : public UndoCommand {
|
|
|
|
public:
|
|
|
|
RemoveLineCommand(GTextEditor& text_editor, String, GTextPosition text_position, bool has_merged_content);
|
|
|
|
virtual void undo() override;
|
|
|
|
virtual void redo() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
String m_line_content;
|
|
|
|
GTextPosition m_text_position;
|
|
|
|
bool m_has_merged_content;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CreateLineCommand : public UndoCommand {
|
|
|
|
public:
|
|
|
|
CreateLineCommand(GTextEditor& text_editor, Vector<char> line_content, GTextPosition text_position);
|
|
|
|
virtual void undo() override;
|
|
|
|
virtual void redo() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Vector<char> m_line_content;
|
|
|
|
GTextPosition m_text_position;
|
|
|
|
};
|
|
|
|
|
2019-11-06 01:37:47 +03:00
|
|
|
struct UndoCommandsContainer {
|
|
|
|
NonnullOwnPtrVector<UndoCommand> m_undo_vector;
|
|
|
|
};
|
|
|
|
|
2019-11-02 22:22:49 +03:00
|
|
|
void add_to_undo_stack(NonnullOwnPtr<UndoCommand> undo_command);
|
2019-10-27 20:00:07 +03:00
|
|
|
int visual_line_containing(int line_index, int column) const;
|
|
|
|
void recompute_visual_lines(int line_index);
|
|
|
|
|
2019-03-15 19:37:13 +03:00
|
|
|
Type m_type { MultiLine };
|
|
|
|
|
2019-03-07 02:31:06 +03:00
|
|
|
GTextPosition m_cursor;
|
2019-04-24 23:24:16 +03:00
|
|
|
TextAlignment m_text_alignment { TextAlignment::CenterLeft };
|
2019-03-07 03:05:35 +03:00
|
|
|
bool m_cursor_state { true };
|
2019-03-08 19:53:02 +03:00
|
|
|
bool m_in_drag_select { false };
|
2019-04-25 00:06:44 +03:00
|
|
|
bool m_ruler_visible { false };
|
2019-11-08 21:49:08 +03:00
|
|
|
bool m_has_pending_change_notification { false };
|
2019-04-25 23:56:09 +03:00
|
|
|
bool m_automatic_indentation_enabled { false };
|
2019-08-25 09:43:01 +03:00
|
|
|
bool m_line_wrapping_enabled { false };
|
2019-05-08 06:00:28 +03:00
|
|
|
bool m_readonly { false };
|
2019-03-17 01:16:37 +03:00
|
|
|
int m_line_spacing { 4 };
|
2019-03-10 13:08:36 +03:00
|
|
|
int m_soft_tab_width { 4 };
|
2019-03-17 01:16:37 +03:00
|
|
|
int m_horizontal_content_padding { 2 };
|
2019-03-08 20:28:24 +03:00
|
|
|
GTextRange m_selection;
|
2019-04-18 13:25:00 +03:00
|
|
|
OwnPtr<GMenu> m_context_menu;
|
2019-06-21 19:37:47 +03:00
|
|
|
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;
|
2019-05-16 01:40:55 +03:00
|
|
|
CElapsedTimer m_triple_click_timer;
|
2019-08-25 22:33:08 +03:00
|
|
|
NonnullRefPtrVector<GAction> m_custom_context_menu_actions;
|
2019-11-06 01:37:47 +03:00
|
|
|
NonnullOwnPtrVector<UndoCommandsContainer> m_undo_stack;
|
|
|
|
int m_undo_stack_index = 0;
|
|
|
|
RefPtr<CTimer> m_undo_timer;
|
|
|
|
int m_last_updated_undo_vector_size = 0;
|
2019-10-25 22:05:06 +03:00
|
|
|
|
2019-10-27 18:10:07 +03:00
|
|
|
RefPtr<GTextDocument> m_document;
|
2019-10-27 20:00:07 +03:00
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
void for_each_visual_line(int line_index, Callback) const;
|
|
|
|
|
|
|
|
struct LineVisualData {
|
|
|
|
Vector<int, 1> visual_line_breaks;
|
|
|
|
Rect visual_rect;
|
|
|
|
};
|
|
|
|
|
|
|
|
NonnullOwnPtrVector<LineVisualData> m_line_visual_data;
|
2019-03-07 02:31:06 +03:00
|
|
|
};
|