summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Applications/TextEditor/main.cpp2
-rw-r--r--LibGUI/GTextEditor.cpp33
-rw-r--r--LibGUI/GTextEditor.h3
3 files changed, 33 insertions, 5 deletions
diff --git a/Applications/TextEditor/main.cpp b/Applications/TextEditor/main.cpp
index 938613397a..84b474cf04 100644
--- a/Applications/TextEditor/main.cpp
+++ b/Applications/TextEditor/main.cpp
@@ -83,7 +83,7 @@ int main(int argc, char** argv)
});
auto copy_action = GAction::create("Copy", { Mod_Ctrl, Key_C }, GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/copyfile16.rgb", { 16, 16 }), [&] (const GAction&) {
- dbgprintf("FIXME: Implement Edit/Copy");
+ printf("Copy: \"%s\"\n", text_editor->selected_text().characters());
});
auto paste_action = GAction::create("Paste", { Mod_Ctrl, Key_V }, GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/paste16.rgb", { 16, 16 }), [&] (const GAction&) {
diff --git a/LibGUI/GTextEditor.cpp b/LibGUI/GTextEditor.cpp
index d3bb2e9a4c..6a2652685e 100644
--- a/LibGUI/GTextEditor.cpp
+++ b/LibGUI/GTextEditor.cpp
@@ -3,6 +3,7 @@
#include <LibGUI/GFontDatabase.h>
#include <SharedGraphics/Painter.h>
#include <Kernel/KeyCode.h>
+#include <AK/StringBuilder.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
@@ -272,22 +273,22 @@ void GTextEditor::keydown_event(GKeyEvent& event)
}
return;
}
- if (event.key() == KeyCode::Key_Home) {
+ if (!event.ctrl() && event.key() == KeyCode::Key_Home) {
toggle_selection_if_needed_for_event(event);
set_cursor(m_cursor.line(), 0);
return;
}
- if (event.key() == KeyCode::Key_End) {
+ if (!event.ctrl() && event.key() == KeyCode::Key_End) {
toggle_selection_if_needed_for_event(event);
set_cursor(m_cursor.line(), current_line().length());
return;
}
- if (event.key() == KeyCode::Key_Home) {
+ if (event.ctrl() && event.key() == KeyCode::Key_Home) {
toggle_selection_if_needed_for_event(event);
set_cursor(0, 0);
return;
}
- if (event.key() == KeyCode::Key_End) {
+ if (event.ctrl() && event.key() == KeyCode::Key_End) {
toggle_selection_if_needed_for_event(event);
set_cursor(line_count() - 1, m_lines[line_count() - 1]->length());
return;
@@ -571,3 +572,27 @@ bool GTextEditor::write_to_file(const String& path)
close(fd);
return true;
}
+
+String GTextEditor::selected_text() const
+{
+ if (!has_selection())
+ return { };
+
+ auto normalized_selection_start = m_selection_start;
+ auto normalized_selection_end = m_cursor;
+ if (m_cursor < m_selection_start)
+ swap(normalized_selection_start, normalized_selection_end);
+
+ StringBuilder builder;
+
+ for (int i = normalized_selection_start.line(); i <= normalized_selection_end.line(); ++i) {
+ auto& line = *m_lines[i];
+ int selection_start_column_on_line = normalized_selection_start.line() == i ? normalized_selection_start.column() : 0;
+ int selection_end_column_on_line = normalized_selection_end.line() == i ? normalized_selection_end.column() : line.length();
+ builder.append(line.characters() + selection_start_column_on_line, selection_end_column_on_line - selection_start_column_on_line);
+ if (i != normalized_selection_end.line())
+ builder.append('\n');
+ }
+
+ return builder.to_string();
+}
diff --git a/LibGUI/GTextEditor.h b/LibGUI/GTextEditor.h
index be734515f8..6ccc4b3297 100644
--- a/LibGUI/GTextEditor.h
+++ b/LibGUI/GTextEditor.h
@@ -53,6 +53,9 @@ public:
bool write_to_file(const String& path);
+ bool has_selection() const { return m_selection_start.is_valid(); }
+ String selected_text() const;
+
private:
virtual void paint_event(GPaintEvent&) override;
virtual void resize_event(GResizeEvent&) override;