summaryrefslogtreecommitdiff
path: root/LibGUI/GTextEditor.h
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-03-07 00:31:06 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-03-07 00:31:06 +0100
commit9158de6c418b03e4c1ab5dfee8ed48a400afb443 (patch)
treec8efa987962d41043e144140c9a8459ff2f5cbfa /LibGUI/GTextEditor.h
parent67ee579113edc2f5963372e04703848c87c89a94 (diff)
downloadserenity-9158de6c418b03e4c1ab5dfee8ed48a400afb443.zip
Begin working on a graphical TextEditor.
It's gonna be a wrapper around a new GTextEditor widget so I can easily reuse the functionality anywhere I need it. :^)
Diffstat (limited to 'LibGUI/GTextEditor.h')
-rw-r--r--LibGUI/GTextEditor.h77
1 files changed, 77 insertions, 0 deletions
diff --git a/LibGUI/GTextEditor.h b/LibGUI/GTextEditor.h
new file mode 100644
index 0000000000..ed0b8facde
--- /dev/null
+++ b/LibGUI/GTextEditor.h
@@ -0,0 +1,77 @@
+#pragma once
+
+#include <LibGUI/GWidget.h>
+#include <AK/Function.h>
+#include <AK/HashMap.h>
+
+class GScrollBar;
+
+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; }
+
+private:
+ int m_line { -1 };
+ int m_column { -1 };
+};
+
+class GTextEditor : public GWidget {
+public:
+ explicit GTextEditor(GWidget* parent);
+ virtual ~GTextEditor() override;
+
+ void set_text(const String&);
+
+ int content_width() const;
+
+ Rect visible_content_rect() const;
+ void scroll_into_view(const GTextPosition&, Orientation);
+
+ int line_count() const { return m_lines.size(); }
+
+ int padding() const { return 2; }
+
+private:
+ virtual void paint_event(GPaintEvent&) override;
+ virtual void resize_event(GResizeEvent&) override;
+ virtual void mousedown_event(GMouseEvent&) override;
+ virtual void keydown_event(GKeyEvent&) override;
+ virtual bool accepts_focus() const override { return true; }
+
+ void update_scrollbar_ranges();
+ Rect line_content_rect(int item_index) const;
+ Rect cursor_content_rect() const;
+ void update_cursor();
+
+ GScrollBar* m_vertical_scrollbar { nullptr };
+ GScrollBar* m_horizontal_scrollbar { nullptr };
+
+ class Line {
+ public:
+ Line() { }
+
+ String text() const { return m_text; }
+ int length() const { return m_text.length(); }
+ int width(const Font&) const;
+ void set_text(const String&);
+
+ private:
+ String m_text;
+ mutable int m_cached_width { -1 };
+ };
+ Vector<Line> m_lines;
+ GTextPosition m_cursor;
+};