GTextEditor: Add selected_text() function.
I don't have a clipboard to actually put the copied selection onto yet, so just print the selected text to stdout so we can see what it is.
This commit is contained in:
parent
77359a5360
commit
6576ac8332
Notes:
sideshowbarker
2024-07-19 15:07:36 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/6576ac83320
3 changed files with 33 additions and 5 deletions
|
@ -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&) {
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue