2019-03-07 02:31:06 +03:00
|
|
|
#include <LibGUI/GTextEditor.h>
|
|
|
|
#include <LibGUI/GScrollBar.h>
|
|
|
|
#include <LibGUI/GFontDatabase.h>
|
2019-03-08 16:08:15 +03:00
|
|
|
#include <LibGUI/GClipboard.h>
|
2019-03-07 02:31:06 +03:00
|
|
|
#include <SharedGraphics/Painter.h>
|
|
|
|
#include <Kernel/KeyCode.h>
|
2019-03-08 03:59:59 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2019-03-07 19:06:11 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
2019-03-07 02:31:06 +03:00
|
|
|
|
|
|
|
GTextEditor::GTextEditor(GWidget* parent)
|
|
|
|
: GWidget(parent)
|
|
|
|
{
|
2019-03-07 02:46:29 +03:00
|
|
|
set_font(GFontDatabase::the().get_by_name("Csilla Thin"));
|
2019-03-07 02:31:06 +03:00
|
|
|
|
|
|
|
set_fill_with_background_color(false);
|
|
|
|
|
|
|
|
m_vertical_scrollbar = new GScrollBar(Orientation::Vertical, this);
|
|
|
|
m_vertical_scrollbar->set_step(4);
|
|
|
|
m_vertical_scrollbar->on_change = [this] (int) {
|
|
|
|
update();
|
|
|
|
};
|
|
|
|
|
|
|
|
m_horizontal_scrollbar = new GScrollBar(Orientation::Horizontal, this);
|
|
|
|
m_horizontal_scrollbar->set_step(4);
|
|
|
|
m_horizontal_scrollbar->set_big_step(30);
|
|
|
|
m_horizontal_scrollbar->on_change = [this] (int) {
|
|
|
|
update();
|
|
|
|
};
|
2019-03-07 18:51:47 +03:00
|
|
|
|
2019-03-08 20:58:40 +03:00
|
|
|
m_corner_widget = new GWidget(this);
|
|
|
|
|
2019-03-07 18:51:47 +03:00
|
|
|
m_lines.append(make<Line>());
|
2019-03-07 02:31:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
GTextEditor::~GTextEditor()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::set_text(const String& text)
|
|
|
|
{
|
|
|
|
m_lines.clear();
|
|
|
|
int start_of_current_line = 0;
|
|
|
|
|
|
|
|
auto add_line = [&] (int current_position) {
|
|
|
|
int line_length = current_position - start_of_current_line;
|
2019-03-07 17:52:11 +03:00
|
|
|
auto line = make<Line>();
|
2019-03-07 02:31:06 +03:00
|
|
|
if (line_length)
|
2019-03-07 17:52:11 +03:00
|
|
|
line->set_text(text.substring(start_of_current_line, current_position - start_of_current_line));
|
2019-03-07 02:31:06 +03:00
|
|
|
m_lines.append(move(line));
|
|
|
|
start_of_current_line = current_position + 1;
|
|
|
|
};
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < text.length(); ++i) {
|
|
|
|
if (text[i] == '\n')
|
|
|
|
add_line(i);
|
|
|
|
}
|
|
|
|
add_line(i);
|
2019-03-07 02:46:29 +03:00
|
|
|
set_cursor(0, 0);
|
2019-03-07 02:31:06 +03:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::resize_event(GResizeEvent& event)
|
|
|
|
{
|
|
|
|
update_scrollbar_ranges();
|
|
|
|
m_vertical_scrollbar->set_relative_rect(event.size().width() - m_vertical_scrollbar->preferred_size().width(), 0, m_vertical_scrollbar->preferred_size().width(), event.size().height() - m_horizontal_scrollbar->preferred_size().height());
|
|
|
|
m_horizontal_scrollbar->set_relative_rect(0, event.size().height() - m_horizontal_scrollbar->preferred_size().height(), event.size().width() - m_vertical_scrollbar->preferred_size().width(), m_horizontal_scrollbar->preferred_size().height());
|
2019-03-08 20:58:40 +03:00
|
|
|
m_corner_widget->set_relative_rect(m_horizontal_scrollbar->rect().right() + 1, m_vertical_scrollbar->rect().bottom() + 1, m_horizontal_scrollbar->height(), m_vertical_scrollbar->width());
|
2019-03-07 02:31:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::update_scrollbar_ranges()
|
|
|
|
{
|
|
|
|
int available_height = height() - m_horizontal_scrollbar->height();
|
2019-03-07 15:54:02 +03:00
|
|
|
int excess_height = max(0, (content_height() + padding() * 2) - available_height);
|
2019-03-07 02:31:06 +03:00
|
|
|
m_vertical_scrollbar->set_range(0, excess_height);
|
|
|
|
|
2019-03-07 22:05:05 +03:00
|
|
|
int available_width = width() - m_vertical_scrollbar->width() - ruler_width();
|
2019-03-07 15:54:02 +03:00
|
|
|
int excess_width = max(0, (content_width() + padding() * 2) - available_width);
|
2019-03-07 02:31:06 +03:00
|
|
|
m_horizontal_scrollbar->set_range(0, excess_width);
|
|
|
|
|
|
|
|
m_vertical_scrollbar->set_big_step(visible_content_rect().height());
|
|
|
|
}
|
|
|
|
|
2019-03-07 15:54:02 +03:00
|
|
|
int GTextEditor::content_height() const
|
|
|
|
{
|
|
|
|
return line_count() * line_height();
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:31:06 +03:00
|
|
|
int GTextEditor::content_width() const
|
|
|
|
{
|
2019-03-07 15:13:25 +03:00
|
|
|
// FIXME: Cache this somewhere.
|
2019-03-07 02:31:06 +03:00
|
|
|
int max_width = 0;
|
|
|
|
for (auto& line : m_lines)
|
2019-03-07 17:52:11 +03:00
|
|
|
max_width = max(line->width(font()), max_width);
|
2019-03-07 02:31:06 +03:00
|
|
|
return max_width;
|
|
|
|
}
|
|
|
|
|
2019-03-07 17:03:38 +03:00
|
|
|
GTextPosition GTextEditor::text_position_at(const Point& a_position) const
|
2019-03-07 02:31:06 +03:00
|
|
|
{
|
2019-03-07 17:03:38 +03:00
|
|
|
auto position = a_position;
|
2019-03-07 16:02:10 +03:00
|
|
|
position.move_by(m_horizontal_scrollbar->value(), m_vertical_scrollbar->value());
|
2019-03-07 22:05:05 +03:00
|
|
|
position.move_by(-(padding() + ruler_width()), -padding());
|
2019-03-07 16:02:10 +03:00
|
|
|
int line_index = position.y() / line_height();
|
|
|
|
int column_index = position.x() / glyph_width();
|
2019-03-08 19:13:31 +03:00
|
|
|
line_index = max(0, min(line_index, line_count() - 1));
|
|
|
|
column_index = max(0, min(column_index, m_lines[line_index]->length()));
|
2019-03-07 17:03:38 +03:00
|
|
|
return { line_index, column_index };
|
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::mousedown_event(GMouseEvent& event)
|
|
|
|
{
|
2019-03-08 19:53:02 +03:00
|
|
|
if (event.button() == GMouseButton::Left) {
|
|
|
|
if (event.modifiers() & Mod_Shift) {
|
|
|
|
if (!has_selection())
|
2019-03-08 20:28:24 +03:00
|
|
|
m_selection.set(m_cursor, { });
|
2019-03-08 19:53:02 +03:00
|
|
|
} else {
|
2019-03-08 20:28:24 +03:00
|
|
|
m_selection.clear();
|
2019-03-08 19:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
m_in_drag_select = true;
|
|
|
|
set_global_cursor_tracking(true);
|
|
|
|
|
|
|
|
set_cursor(text_position_at(event.position()));
|
|
|
|
|
|
|
|
if (!(event.modifiers() & Mod_Shift)) {
|
|
|
|
if (!has_selection())
|
2019-03-08 20:28:24 +03:00
|
|
|
m_selection.set(m_cursor, { });
|
2019-03-08 19:53:02 +03:00
|
|
|
}
|
|
|
|
|
2019-03-08 20:28:24 +03:00
|
|
|
if (m_selection.start().is_valid())
|
|
|
|
m_selection.set_end(m_cursor);
|
|
|
|
|
2019-03-08 19:53:02 +03:00
|
|
|
// FIXME: Only update the relevant rects.
|
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::mouseup_event(GMouseEvent& event)
|
|
|
|
{
|
|
|
|
if (event.button() == GMouseButton::Left) {
|
|
|
|
if (m_in_drag_select) {
|
|
|
|
m_in_drag_select = false;
|
|
|
|
set_global_cursor_tracking(false);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::mousemove_event(GMouseEvent& event)
|
|
|
|
{
|
|
|
|
if (m_in_drag_select) {
|
|
|
|
set_cursor(text_position_at(event.position()));
|
2019-03-08 20:28:24 +03:00
|
|
|
m_selection.set_end(m_cursor);
|
2019-03-08 02:49:45 +03:00
|
|
|
update();
|
2019-03-08 19:53:02 +03:00
|
|
|
return;
|
2019-03-08 02:49:45 +03:00
|
|
|
}
|
2019-03-07 02:31:06 +03:00
|
|
|
}
|
|
|
|
|
2019-03-07 22:05:05 +03:00
|
|
|
int GTextEditor::ruler_width() const
|
|
|
|
{
|
|
|
|
// FIXME: Resize based on needed space.
|
|
|
|
return 5 * font().glyph_width('x') + 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect GTextEditor::ruler_content_rect(int line_index) const
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
0 - ruler_width() - padding() + m_horizontal_scrollbar->value(),
|
|
|
|
line_index * line_height(),
|
|
|
|
ruler_width(),
|
|
|
|
line_height()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:31:06 +03:00
|
|
|
void GTextEditor::paint_event(GPaintEvent& event)
|
|
|
|
{
|
|
|
|
Painter painter(*this);
|
2019-03-08 20:58:40 +03:00
|
|
|
Rect item_area_rect { 0, 0, width() - m_vertical_scrollbar->width(), height() - m_horizontal_scrollbar->height() };
|
|
|
|
painter.set_clip_rect(item_area_rect);
|
2019-03-07 02:31:06 +03:00
|
|
|
painter.set_clip_rect(event.rect());
|
|
|
|
painter.fill_rect(event.rect(), Color::White);
|
2019-03-07 22:05:05 +03:00
|
|
|
|
|
|
|
Rect ruler_rect { 0, 0, ruler_width(), height() - m_horizontal_scrollbar->height()};
|
|
|
|
painter.fill_rect(ruler_rect, Color::LightGray);
|
|
|
|
painter.draw_line(ruler_rect.top_right(), ruler_rect.bottom_right(), Color::DarkGray);
|
|
|
|
|
2019-03-09 18:48:43 +03:00
|
|
|
painter.save();
|
|
|
|
|
2019-03-07 02:31:06 +03:00
|
|
|
painter.translate(-m_horizontal_scrollbar->value(), -m_vertical_scrollbar->value());
|
2019-03-07 22:05:05 +03:00
|
|
|
painter.translate(padding() + ruler_width(), padding());
|
2019-03-07 02:31:06 +03:00
|
|
|
int exposed_width = max(content_width(), width());
|
|
|
|
|
2019-03-07 17:03:38 +03:00
|
|
|
int first_visible_line = text_position_at(event.rect().top_left()).line();
|
|
|
|
int last_visible_line = text_position_at(event.rect().bottom_right()).line();
|
|
|
|
|
2019-03-08 20:28:24 +03:00
|
|
|
auto selection = normalized_selection();
|
|
|
|
bool has_selection = selection.is_valid();
|
2019-03-08 02:49:45 +03:00
|
|
|
|
2019-03-07 22:05:05 +03:00
|
|
|
for (int i = first_visible_line; i <= last_visible_line; ++i) {
|
|
|
|
bool is_current_line = i == m_cursor.line();
|
|
|
|
auto ruler_line_rect = ruler_content_rect(i);
|
2019-03-09 23:30:16 +03:00
|
|
|
painter.draw_text(
|
|
|
|
ruler_line_rect.shrunken(2, 0),
|
|
|
|
String::format("%u", i),
|
|
|
|
is_current_line ? Font::default_bold_font() : font(),
|
|
|
|
TextAlignment::CenterRight,
|
|
|
|
is_current_line ? Color::DarkGray : Color::MidGray
|
|
|
|
);
|
2019-03-07 22:05:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
painter.set_clip_rect({ ruler_rect.right() + 1, 0, width() - m_vertical_scrollbar->width() - ruler_width(), height() - m_horizontal_scrollbar->height() });
|
|
|
|
|
2019-03-07 17:03:38 +03:00
|
|
|
for (int i = first_visible_line; i <= last_visible_line; ++i) {
|
2019-03-07 17:52:11 +03:00
|
|
|
auto& line = *m_lines[i];
|
2019-03-07 02:31:06 +03:00
|
|
|
auto line_rect = line_content_rect(i);
|
2019-03-07 04:24:46 +03:00
|
|
|
line_rect.set_width(exposed_width);
|
2019-03-07 22:05:05 +03:00
|
|
|
if (i == m_cursor.line())
|
2019-03-07 04:24:46 +03:00
|
|
|
painter.fill_rect(line_rect, Color(230, 230, 230));
|
2019-03-07 16:35:32 +03:00
|
|
|
painter.draw_text(line_rect, line.characters(), line.length(), TextAlignment::CenterLeft, Color::Black);
|
2019-03-08 20:28:24 +03:00
|
|
|
bool line_has_selection = has_selection && i >= selection.start().line() && i <= selection.end().line();
|
2019-03-08 02:49:45 +03:00
|
|
|
if (line_has_selection) {
|
2019-03-08 20:28:24 +03:00
|
|
|
int selection_start_column_on_line = selection.start().line() == i ? selection.start().column() : 0;
|
|
|
|
int selection_end_column_on_line = selection.end().line() == i ? selection.end().column() : line.length();
|
2019-03-08 02:49:45 +03:00
|
|
|
int selection_left = selection_start_column_on_line * font().glyph_width('x');
|
|
|
|
int selection_right = line_rect.left() + selection_end_column_on_line * font().glyph_width('x');
|
|
|
|
Rect selection_rect { selection_left, line_rect.y(), selection_right - selection_left, line_rect.height() };
|
|
|
|
painter.fill_rect(selection_rect, Color::from_rgb(0x955233));
|
|
|
|
painter.draw_text(selection_rect, line.characters() + selection_start_column_on_line, line.length() - selection_start_column_on_line - (line.length() - selection_end_column_on_line), TextAlignment::CenterLeft, Color::White);
|
|
|
|
}
|
2019-03-07 02:31:06 +03:00
|
|
|
}
|
|
|
|
|
2019-03-07 15:26:02 +03:00
|
|
|
if (is_focused() && m_cursor_state)
|
2019-03-07 03:05:35 +03:00
|
|
|
painter.fill_rect(cursor_content_rect(), Color::Red);
|
2019-03-07 02:31:06 +03:00
|
|
|
|
2019-03-09 18:48:43 +03:00
|
|
|
painter.restore();
|
2019-03-07 02:31:06 +03:00
|
|
|
|
2019-03-08 20:58:40 +03:00
|
|
|
if (is_focused())
|
2019-03-07 02:31:06 +03:00
|
|
|
painter.draw_rect(item_area_rect, Color::from_rgb(0x84351a));
|
|
|
|
}
|
|
|
|
|
2019-03-08 02:49:45 +03:00
|
|
|
void GTextEditor::toggle_selection_if_needed_for_event(const GKeyEvent& event)
|
|
|
|
{
|
2019-03-08 20:28:24 +03:00
|
|
|
if (event.shift() && !m_selection.is_valid()) {
|
|
|
|
m_selection.set(m_cursor, { });
|
2019-03-08 02:49:45 +03:00
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
2019-03-08 20:28:24 +03:00
|
|
|
if (!event.shift() && m_selection.is_valid()) {
|
|
|
|
m_selection.clear();
|
2019-03-08 02:49:45 +03:00
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:31:06 +03:00
|
|
|
void GTextEditor::keydown_event(GKeyEvent& event)
|
|
|
|
{
|
2019-03-08 02:49:45 +03:00
|
|
|
if (event.key() == KeyCode::Key_Up) {
|
2019-03-07 02:31:06 +03:00
|
|
|
if (m_cursor.line() > 0) {
|
2019-03-07 02:46:29 +03:00
|
|
|
int new_line = m_cursor.line() - 1;
|
2019-03-07 17:52:11 +03:00
|
|
|
int new_column = min(m_cursor.column(), m_lines[new_line]->length());
|
2019-03-08 02:49:45 +03:00
|
|
|
toggle_selection_if_needed_for_event(event);
|
2019-03-07 02:46:29 +03:00
|
|
|
set_cursor(new_line, new_column);
|
2019-03-08 20:28:24 +03:00
|
|
|
if (m_selection.start().is_valid())
|
|
|
|
m_selection.set_end(m_cursor);
|
2019-03-07 02:31:06 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-03-08 02:49:45 +03:00
|
|
|
if (event.key() == KeyCode::Key_Down) {
|
2019-03-07 02:31:06 +03:00
|
|
|
if (m_cursor.line() < (m_lines.size() - 1)) {
|
2019-03-07 02:46:29 +03:00
|
|
|
int new_line = m_cursor.line() + 1;
|
2019-03-07 17:52:11 +03:00
|
|
|
int new_column = min(m_cursor.column(), m_lines[new_line]->length());
|
2019-03-08 02:49:45 +03:00
|
|
|
toggle_selection_if_needed_for_event(event);
|
2019-03-07 02:46:29 +03:00
|
|
|
set_cursor(new_line, new_column);
|
2019-03-08 20:28:24 +03:00
|
|
|
if (m_selection.start().is_valid())
|
|
|
|
m_selection.set_end(m_cursor);
|
2019-03-07 02:31:06 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-03-08 02:49:45 +03:00
|
|
|
if (event.key() == KeyCode::Key_PageUp) {
|
2019-03-07 19:11:17 +03:00
|
|
|
if (m_cursor.line() > 0) {
|
|
|
|
int new_line = max(0, m_cursor.line() - visible_content_rect().height() / line_height());
|
|
|
|
int new_column = min(m_cursor.column(), m_lines[new_line]->length());
|
2019-03-08 02:49:45 +03:00
|
|
|
toggle_selection_if_needed_for_event(event);
|
2019-03-07 19:11:17 +03:00
|
|
|
set_cursor(new_line, new_column);
|
2019-03-08 20:28:24 +03:00
|
|
|
if (m_selection.start().is_valid())
|
|
|
|
m_selection.set_end(m_cursor);
|
2019-03-07 19:11:17 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-03-08 02:49:45 +03:00
|
|
|
if (event.key() == KeyCode::Key_PageDown) {
|
2019-03-07 19:11:17 +03:00
|
|
|
if (m_cursor.line() < (m_lines.size() - 1)) {
|
|
|
|
int new_line = min(line_count() - 1, m_cursor.line() + visible_content_rect().height() / line_height());
|
|
|
|
int new_column = min(m_cursor.column(), m_lines[new_line]->length());
|
2019-03-08 02:49:45 +03:00
|
|
|
toggle_selection_if_needed_for_event(event);
|
2019-03-07 19:11:17 +03:00
|
|
|
set_cursor(new_line, new_column);
|
2019-03-08 20:28:24 +03:00
|
|
|
if (m_selection.start().is_valid())
|
|
|
|
m_selection.set_end(m_cursor);
|
2019-03-07 19:11:17 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-03-08 02:49:45 +03:00
|
|
|
if (event.key() == KeyCode::Key_Left) {
|
2019-03-07 02:31:06 +03:00
|
|
|
if (m_cursor.column() > 0) {
|
2019-03-07 02:46:29 +03:00
|
|
|
int new_column = m_cursor.column() - 1;
|
2019-03-08 02:49:45 +03:00
|
|
|
toggle_selection_if_needed_for_event(event);
|
2019-03-07 02:46:29 +03:00
|
|
|
set_cursor(m_cursor.line(), new_column);
|
2019-03-08 20:28:24 +03:00
|
|
|
if (m_selection.start().is_valid())
|
|
|
|
m_selection.set_end(m_cursor);
|
2019-03-10 00:02:35 +03:00
|
|
|
} else if (m_cursor.line() > 0) {
|
|
|
|
int new_line = m_cursor.line() - 1;
|
|
|
|
int new_column = m_lines[new_line]->length();
|
|
|
|
toggle_selection_if_needed_for_event(event);
|
|
|
|
set_cursor(new_line, new_column);
|
|
|
|
if (m_selection.start().is_valid())
|
|
|
|
m_selection.set_end(m_cursor);
|
2019-03-07 02:31:06 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-03-08 02:49:45 +03:00
|
|
|
if (event.key() == KeyCode::Key_Right) {
|
2019-03-07 15:21:51 +03:00
|
|
|
if (m_cursor.column() < current_line().length()) {
|
2019-03-07 02:46:29 +03:00
|
|
|
int new_column = m_cursor.column() + 1;
|
2019-03-08 02:49:45 +03:00
|
|
|
toggle_selection_if_needed_for_event(event);
|
2019-03-07 02:46:29 +03:00
|
|
|
set_cursor(m_cursor.line(), new_column);
|
2019-03-08 20:28:24 +03:00
|
|
|
if (m_selection.start().is_valid())
|
|
|
|
m_selection.set_end(m_cursor);
|
2019-03-10 00:02:35 +03:00
|
|
|
} else if (m_cursor.line() != line_count() - 1) {
|
|
|
|
int new_line = m_cursor.line() + 1;
|
|
|
|
int new_column = 0;
|
|
|
|
toggle_selection_if_needed_for_event(event);
|
|
|
|
set_cursor(new_line, new_column);
|
|
|
|
if (m_selection.start().is_valid())
|
|
|
|
m_selection.set_end(m_cursor);
|
2019-03-07 02:31:06 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-03-08 03:59:59 +03:00
|
|
|
if (!event.ctrl() && event.key() == KeyCode::Key_Home) {
|
2019-03-08 02:49:45 +03:00
|
|
|
toggle_selection_if_needed_for_event(event);
|
2019-03-07 15:21:51 +03:00
|
|
|
set_cursor(m_cursor.line(), 0);
|
2019-03-08 20:28:24 +03:00
|
|
|
if (m_selection.start().is_valid())
|
|
|
|
m_selection.set_end(m_cursor);
|
2019-03-07 15:21:51 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-03-08 03:59:59 +03:00
|
|
|
if (!event.ctrl() && event.key() == KeyCode::Key_End) {
|
2019-03-08 02:49:45 +03:00
|
|
|
toggle_selection_if_needed_for_event(event);
|
2019-03-07 15:21:51 +03:00
|
|
|
set_cursor(m_cursor.line(), current_line().length());
|
2019-03-08 20:28:24 +03:00
|
|
|
if (m_selection.start().is_valid())
|
|
|
|
m_selection.set_end(m_cursor);
|
2019-03-07 15:21:51 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-03-08 03:59:59 +03:00
|
|
|
if (event.ctrl() && event.key() == KeyCode::Key_Home) {
|
2019-03-08 02:49:45 +03:00
|
|
|
toggle_selection_if_needed_for_event(event);
|
2019-03-07 15:21:51 +03:00
|
|
|
set_cursor(0, 0);
|
2019-03-08 20:28:24 +03:00
|
|
|
if (m_selection.start().is_valid())
|
|
|
|
m_selection.set_end(m_cursor);
|
2019-03-07 15:21:51 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-03-08 03:59:59 +03:00
|
|
|
if (event.ctrl() && event.key() == KeyCode::Key_End) {
|
2019-03-08 02:49:45 +03:00
|
|
|
toggle_selection_if_needed_for_event(event);
|
2019-03-07 17:52:11 +03:00
|
|
|
set_cursor(line_count() - 1, m_lines[line_count() - 1]->length());
|
2019-03-08 20:28:24 +03:00
|
|
|
if (m_selection.start().is_valid())
|
|
|
|
m_selection.set_end(m_cursor);
|
2019-03-07 15:21:51 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-03-08 16:12:41 +03:00
|
|
|
if (event.modifiers() == Mod_Ctrl && event.key() == KeyCode::Key_A) {
|
2019-03-08 20:28:24 +03:00
|
|
|
GTextPosition start_of_document { 0, 0 };
|
|
|
|
GTextPosition end_of_document { line_count() - 1, m_lines[line_count() - 1]->length() };
|
|
|
|
m_selection.set(start_of_document, end_of_document);
|
|
|
|
set_cursor(end_of_document);
|
2019-03-08 16:12:41 +03:00
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
2019-03-07 16:35:32 +03:00
|
|
|
|
2019-03-08 20:50:14 +03:00
|
|
|
if (event.key() == KeyCode::Key_Backspace) {
|
2019-03-08 16:10:34 +03:00
|
|
|
if (has_selection()) {
|
|
|
|
delete_selection();
|
|
|
|
return;
|
|
|
|
}
|
2019-03-07 18:04:21 +03:00
|
|
|
if (m_cursor.column() > 0) {
|
2019-03-07 18:15:25 +03:00
|
|
|
// Backspace within line
|
2019-03-07 18:04:21 +03:00
|
|
|
current_line().remove(m_cursor.column() - 1);
|
2019-03-07 18:33:07 +03:00
|
|
|
update_scrollbar_ranges();
|
2019-03-07 18:04:21 +03:00
|
|
|
set_cursor(m_cursor.line(), m_cursor.column() - 1);
|
2019-03-07 19:18:22 +03:00
|
|
|
return;
|
2019-03-07 18:04:21 +03:00
|
|
|
}
|
2019-03-07 18:15:25 +03:00
|
|
|
if (m_cursor.column() == 0 && m_cursor.line() != 0) {
|
2019-03-07 18:33:07 +03:00
|
|
|
// Backspace at column 0; merge with previous line
|
2019-03-07 18:15:25 +03:00
|
|
|
auto& previous_line = *m_lines[m_cursor.line() - 1];
|
|
|
|
int previous_length = previous_line.length();
|
|
|
|
previous_line.append(current_line().characters(), current_line().length());
|
|
|
|
m_lines.remove(m_cursor.line());
|
2019-03-07 18:33:07 +03:00
|
|
|
update_scrollbar_ranges();
|
2019-03-07 18:15:25 +03:00
|
|
|
update();
|
|
|
|
set_cursor(m_cursor.line() - 1, previous_length);
|
2019-03-07 19:18:22 +03:00
|
|
|
return;
|
2019-03-07 18:15:25 +03:00
|
|
|
}
|
2019-03-07 18:04:21 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-08 20:50:14 +03:00
|
|
|
if (event.key() == KeyCode::Key_Delete) {
|
2019-03-08 16:10:34 +03:00
|
|
|
if (has_selection()) {
|
|
|
|
delete_selection();
|
|
|
|
return;
|
|
|
|
}
|
2019-03-07 18:33:07 +03:00
|
|
|
if (m_cursor.column() < current_line().length()) {
|
|
|
|
// Delete within line
|
|
|
|
current_line().remove(m_cursor.column());
|
|
|
|
update_scrollbar_ranges();
|
|
|
|
update_cursor();
|
2019-03-07 19:18:22 +03:00
|
|
|
return;
|
2019-03-07 18:33:07 +03:00
|
|
|
}
|
2019-03-07 22:15:05 +03:00
|
|
|
if (m_cursor.column() == current_line().length() && m_cursor.line() != line_count() - 1) {
|
2019-03-07 18:33:07 +03:00
|
|
|
// Delete at end of line; merge with next line
|
|
|
|
auto& next_line = *m_lines[m_cursor.line() + 1];
|
|
|
|
int previous_length = current_line().length();
|
|
|
|
current_line().append(next_line.characters(), next_line.length());
|
|
|
|
m_lines.remove(m_cursor.line() + 1);
|
|
|
|
update_scrollbar_ranges();
|
|
|
|
update();
|
|
|
|
set_cursor(m_cursor.line(), previous_length);
|
2019-03-07 19:18:22 +03:00
|
|
|
return;
|
2019-03-07 18:33:07 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-08 16:10:34 +03:00
|
|
|
if (!event.ctrl() && !event.alt() && !event.text().is_empty())
|
|
|
|
insert_at_cursor_or_replace_selection(event.text());
|
2019-03-07 16:35:32 +03:00
|
|
|
|
2019-03-07 02:31:06 +03:00
|
|
|
return GWidget::keydown_event(event);
|
|
|
|
}
|
|
|
|
|
2019-03-08 16:08:15 +03:00
|
|
|
void GTextEditor::insert_at_cursor(const String& text)
|
|
|
|
{
|
|
|
|
// FIXME: This should obviously not be implemented this way.
|
|
|
|
for (int i = 0; i < text.length(); ++i) {
|
|
|
|
insert_at_cursor(text[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 16:35:32 +03:00
|
|
|
void GTextEditor::insert_at_cursor(char ch)
|
|
|
|
{
|
2019-03-07 17:52:11 +03:00
|
|
|
bool at_head = m_cursor.column() == 0;
|
|
|
|
bool at_tail = m_cursor.column() == current_line().length();
|
|
|
|
if (ch == '\n') {
|
|
|
|
if (at_tail || at_head) {
|
|
|
|
m_lines.insert(m_cursor.line() + (at_tail ? 1 : 0), make<Line>());
|
|
|
|
update_scrollbar_ranges();
|
|
|
|
update();
|
2019-03-07 18:49:04 +03:00
|
|
|
set_cursor(m_cursor.line() + 1, 0);
|
2019-03-07 17:52:11 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-03-07 18:49:04 +03:00
|
|
|
auto new_line = make<Line>();
|
|
|
|
new_line->append(current_line().characters() + m_cursor.column(), current_line().length() - m_cursor.column());
|
|
|
|
current_line().truncate(m_cursor.column());
|
|
|
|
m_lines.insert(m_cursor.line() + 1, move(new_line));
|
|
|
|
update_scrollbar_ranges();
|
|
|
|
update();
|
|
|
|
set_cursor(m_cursor.line() + 1, 0);
|
2019-03-07 18:04:21 +03:00
|
|
|
return;
|
2019-03-07 17:52:11 +03:00
|
|
|
}
|
2019-03-07 18:04:21 +03:00
|
|
|
current_line().insert(m_cursor.column(), ch);
|
2019-03-07 22:05:05 +03:00
|
|
|
update_scrollbar_ranges();
|
2019-03-07 18:04:21 +03:00
|
|
|
set_cursor(m_cursor.line(), m_cursor.column() + 1);
|
|
|
|
update_cursor();
|
2019-03-07 16:35:32 +03:00
|
|
|
}
|
|
|
|
|
2019-03-07 02:31:06 +03:00
|
|
|
Rect GTextEditor::visible_content_rect() const
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
m_horizontal_scrollbar->value(),
|
|
|
|
m_vertical_scrollbar->value(),
|
2019-03-07 22:05:05 +03:00
|
|
|
width() - m_vertical_scrollbar->width() - padding() * 2 - ruler_width(),
|
2019-03-07 02:31:06 +03:00
|
|
|
height() - m_horizontal_scrollbar->height() - padding() * 2
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect GTextEditor::cursor_content_rect() const
|
|
|
|
{
|
2019-03-07 03:05:35 +03:00
|
|
|
if (!m_cursor.is_valid())
|
|
|
|
return { };
|
2019-03-07 02:31:06 +03:00
|
|
|
ASSERT(!m_lines.is_empty());
|
2019-03-07 16:35:32 +03:00
|
|
|
ASSERT(m_cursor.column() <= (current_line().length() + 1));
|
2019-03-07 16:02:10 +03:00
|
|
|
return { m_cursor.column() * glyph_width(), m_cursor.line() * line_height(), 1, line_height() };
|
2019-03-07 03:05:35 +03:00
|
|
|
}
|
|
|
|
|
2019-03-07 04:24:46 +03:00
|
|
|
Rect GTextEditor::line_widget_rect(int line_index) const
|
|
|
|
{
|
|
|
|
ASSERT(m_horizontal_scrollbar);
|
|
|
|
ASSERT(m_vertical_scrollbar);
|
|
|
|
auto rect = line_content_rect(line_index);
|
2019-03-07 15:13:25 +03:00
|
|
|
rect.move_by(-(m_horizontal_scrollbar->value() - padding()), -(m_vertical_scrollbar->value() - padding()));
|
2019-03-07 15:54:02 +03:00
|
|
|
rect.set_width(rect.width() + 1); // Add 1 pixel for when the cursor is on the end.
|
2019-03-07 15:13:25 +03:00
|
|
|
rect.intersect(this->rect());
|
2019-03-07 18:33:07 +03:00
|
|
|
// This feels rather hackish, but extend the rect to the edge of the content view:
|
|
|
|
rect.set_right(m_vertical_scrollbar->relative_rect().left() - 1);
|
2019-03-07 15:13:25 +03:00
|
|
|
return rect;
|
2019-03-07 04:24:46 +03:00
|
|
|
}
|
|
|
|
|
2019-03-07 15:13:25 +03:00
|
|
|
void GTextEditor::scroll_cursor_into_view()
|
2019-03-07 02:31:06 +03:00
|
|
|
{
|
|
|
|
auto visible_content_rect = this->visible_content_rect();
|
2019-03-07 15:13:25 +03:00
|
|
|
auto rect = cursor_content_rect();
|
|
|
|
|
|
|
|
if (visible_content_rect.is_empty())
|
|
|
|
return;
|
2019-03-07 02:31:06 +03:00
|
|
|
|
|
|
|
if (visible_content_rect.contains(rect))
|
|
|
|
return;
|
|
|
|
|
2019-03-07 15:13:25 +03:00
|
|
|
if (rect.top() < visible_content_rect.top())
|
|
|
|
m_vertical_scrollbar->set_value(rect.top());
|
|
|
|
else if (rect.bottom() > visible_content_rect.bottom())
|
|
|
|
m_vertical_scrollbar->set_value(rect.bottom() - visible_content_rect.height());
|
|
|
|
|
|
|
|
if (rect.left() < visible_content_rect.left())
|
|
|
|
m_horizontal_scrollbar->set_value(rect.left());
|
|
|
|
else if (rect.right() > visible_content_rect.right())
|
|
|
|
m_horizontal_scrollbar->set_value(rect.right() - visible_content_rect.width());
|
|
|
|
|
|
|
|
update();
|
2019-03-07 02:31:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Rect GTextEditor::line_content_rect(int line_index) const
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
0,
|
2019-03-07 03:05:35 +03:00
|
|
|
line_index * line_height(),
|
2019-03-07 15:13:25 +03:00
|
|
|
content_width(),
|
2019-03-07 03:05:35 +03:00
|
|
|
line_height()
|
2019-03-07 02:31:06 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::update_cursor()
|
|
|
|
{
|
2019-03-07 15:23:17 +03:00
|
|
|
update(line_widget_rect(m_cursor.line()));
|
2019-03-07 02:31:06 +03:00
|
|
|
}
|
|
|
|
|
2019-03-07 02:46:29 +03:00
|
|
|
void GTextEditor::set_cursor(int line, int column)
|
|
|
|
{
|
2019-03-07 17:03:38 +03:00
|
|
|
set_cursor({ line, column });
|
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::set_cursor(const GTextPosition& position)
|
|
|
|
{
|
2019-03-08 16:08:15 +03:00
|
|
|
ASSERT(!m_lines.is_empty());
|
2019-03-08 17:55:58 +03:00
|
|
|
ASSERT(position.line() < m_lines.size());
|
|
|
|
ASSERT(position.column() <= m_lines[position.line()]->length());
|
2019-03-08 19:53:02 +03:00
|
|
|
if (m_cursor != position) {
|
|
|
|
auto old_cursor_line_rect = line_widget_rect(m_cursor.line());
|
|
|
|
m_cursor = position;
|
|
|
|
m_cursor_state = true;
|
|
|
|
scroll_cursor_into_view();
|
|
|
|
update(old_cursor_line_rect);
|
|
|
|
update_cursor();
|
|
|
|
}
|
2019-03-07 02:46:29 +03:00
|
|
|
if (on_cursor_change)
|
|
|
|
on_cursor_change(*this);
|
|
|
|
}
|
|
|
|
|
2019-03-07 03:05:35 +03:00
|
|
|
void GTextEditor::focusin_event(GEvent&)
|
|
|
|
{
|
|
|
|
update_cursor();
|
|
|
|
start_timer(500);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::focusout_event(GEvent&)
|
|
|
|
{
|
|
|
|
stop_timer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::timer_event(GTimerEvent&)
|
|
|
|
{
|
|
|
|
m_cursor_state = !m_cursor_state;
|
|
|
|
if (is_focused())
|
|
|
|
update_cursor();
|
|
|
|
}
|
|
|
|
|
2019-03-07 16:35:32 +03:00
|
|
|
GTextEditor::Line::Line()
|
|
|
|
{
|
2019-03-08 17:55:58 +03:00
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::Line::clear()
|
|
|
|
{
|
|
|
|
m_text.clear();
|
2019-03-07 16:35:32 +03:00
|
|
|
m_text.append(0);
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:31:06 +03:00
|
|
|
void GTextEditor::Line::set_text(const String& text)
|
|
|
|
{
|
2019-03-07 16:35:32 +03:00
|
|
|
if (text.length() == length() && !memcmp(text.characters(), characters(), length()))
|
2019-03-07 02:31:06 +03:00
|
|
|
return;
|
2019-03-08 17:55:58 +03:00
|
|
|
if (text.is_empty()) {
|
|
|
|
clear();
|
|
|
|
return;
|
|
|
|
}
|
2019-03-07 16:35:32 +03:00
|
|
|
m_text.resize(text.length() + 1);
|
|
|
|
memcpy(m_text.data(), text.characters(), text.length() + 1);
|
2019-03-07 02:31:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int GTextEditor::Line::width(const Font& font) const
|
|
|
|
{
|
2019-03-07 16:35:32 +03:00
|
|
|
return font.glyph_width('x') * length();
|
2019-03-07 02:31:06 +03:00
|
|
|
}
|
2019-03-07 18:04:21 +03:00
|
|
|
|
2019-03-07 18:15:25 +03:00
|
|
|
void GTextEditor::Line::append(const char* characters, int length)
|
|
|
|
{
|
|
|
|
int old_length = m_text.size() - 1;
|
|
|
|
m_text.resize(m_text.size() + length);
|
|
|
|
memcpy(m_text.data() + old_length, characters, length);
|
|
|
|
m_text.last() = 0;
|
|
|
|
}
|
|
|
|
|
2019-03-07 18:04:21 +03:00
|
|
|
void GTextEditor::Line::append(char ch)
|
|
|
|
{
|
|
|
|
insert(length(), ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::Line::prepend(char ch)
|
|
|
|
{
|
|
|
|
insert(0, ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::Line::insert(int index, char ch)
|
|
|
|
{
|
|
|
|
if (index == length()) {
|
|
|
|
m_text.last() = ch;
|
|
|
|
m_text.append(0);
|
|
|
|
} else {
|
|
|
|
m_text.insert(index, move(ch));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::Line::remove(int index)
|
|
|
|
{
|
|
|
|
if (index == length()) {
|
|
|
|
m_text.take_last();
|
|
|
|
m_text.last() = 0;
|
|
|
|
} else {
|
|
|
|
m_text.remove(index);
|
|
|
|
}
|
|
|
|
}
|
2019-03-07 18:49:04 +03:00
|
|
|
|
|
|
|
void GTextEditor::Line::truncate(int length)
|
|
|
|
{
|
|
|
|
m_text.resize(length + 1);
|
|
|
|
m_text.last() = 0;
|
|
|
|
}
|
2019-03-07 19:06:11 +03:00
|
|
|
|
|
|
|
bool GTextEditor::write_to_file(const String& path)
|
|
|
|
{
|
|
|
|
int fd = open(path.characters(), O_WRONLY | O_CREAT, 0666);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("open");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < m_lines.size(); ++i) {
|
|
|
|
auto& line = *m_lines[i];
|
|
|
|
if (line.length()) {
|
|
|
|
ssize_t nwritten = write(fd, line.characters(), line.length());
|
|
|
|
if (nwritten < 0) {
|
|
|
|
perror("write");
|
|
|
|
close(fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i != m_lines.size() - 1) {
|
|
|
|
char ch = '\n';
|
|
|
|
ssize_t nwritten = write(fd, &ch, 1);
|
|
|
|
if (nwritten != 1) {
|
|
|
|
perror("write");
|
|
|
|
close(fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return true;
|
|
|
|
}
|
2019-03-08 03:59:59 +03:00
|
|
|
|
|
|
|
String GTextEditor::selected_text() const
|
|
|
|
{
|
|
|
|
if (!has_selection())
|
|
|
|
return { };
|
|
|
|
|
2019-03-08 20:28:24 +03:00
|
|
|
auto selection = normalized_selection();
|
2019-03-08 03:59:59 +03:00
|
|
|
StringBuilder builder;
|
2019-03-08 20:28:24 +03:00
|
|
|
for (int i = selection.start().line(); i <= selection.end().line(); ++i) {
|
2019-03-08 03:59:59 +03:00
|
|
|
auto& line = *m_lines[i];
|
2019-03-08 20:28:24 +03:00
|
|
|
int selection_start_column_on_line = selection.start().line() == i ? selection.start().column() : 0;
|
|
|
|
int selection_end_column_on_line = selection.end().line() == i ? selection.end().column() : line.length();
|
2019-03-08 03:59:59 +03:00
|
|
|
builder.append(line.characters() + selection_start_column_on_line, selection_end_column_on_line - selection_start_column_on_line);
|
2019-03-08 20:28:24 +03:00
|
|
|
if (i != selection.end().line())
|
2019-03-08 03:59:59 +03:00
|
|
|
builder.append('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.to_string();
|
|
|
|
}
|
2019-03-08 16:08:15 +03:00
|
|
|
|
|
|
|
void GTextEditor::delete_selection()
|
|
|
|
{
|
|
|
|
if (!has_selection())
|
|
|
|
return;
|
|
|
|
|
2019-03-08 20:28:24 +03:00
|
|
|
auto selection = normalized_selection();
|
2019-03-08 16:08:15 +03:00
|
|
|
|
2019-03-08 17:55:58 +03:00
|
|
|
// First delete all the lines in between the first and last one.
|
2019-03-08 20:28:24 +03:00
|
|
|
for (int i = selection.start().line() + 1; i < selection.end().line();) {
|
2019-03-08 17:55:58 +03:00
|
|
|
m_lines.remove(i);
|
2019-03-08 20:28:24 +03:00
|
|
|
selection.end().set_line(selection.end().line() - 1);
|
2019-03-08 17:55:58 +03:00
|
|
|
}
|
|
|
|
|
2019-03-08 20:28:24 +03:00
|
|
|
if (selection.start().line() == selection.end().line()) {
|
2019-03-08 17:55:58 +03:00
|
|
|
// Delete within same line.
|
2019-03-08 20:28:24 +03:00
|
|
|
auto& line = *m_lines[selection.start().line()];
|
|
|
|
bool whole_line_is_selected = selection.start().column() == 0 && selection.end().column() == line.length();
|
2019-03-08 16:08:15 +03:00
|
|
|
if (whole_line_is_selected) {
|
2019-03-08 17:55:58 +03:00
|
|
|
line.clear();
|
|
|
|
} else {
|
2019-03-08 20:28:24 +03:00
|
|
|
auto before_selection = String(line.characters(), line.length()).substring(0, selection.start().column());
|
|
|
|
auto after_selection = String(line.characters(), line.length()).substring(selection.end().column(), line.length() - selection.end().column());
|
2019-03-08 17:55:58 +03:00
|
|
|
StringBuilder builder(before_selection.length() + after_selection.length());
|
|
|
|
builder.append(before_selection);
|
|
|
|
builder.append(after_selection);
|
|
|
|
line.set_text(builder.to_string());
|
2019-03-08 16:08:15 +03:00
|
|
|
}
|
2019-03-08 17:55:58 +03:00
|
|
|
} else {
|
|
|
|
// Delete across a newline, merging lines.
|
2019-03-08 20:28:24 +03:00
|
|
|
ASSERT(selection.start().line() == selection.end().line() - 1);
|
|
|
|
auto& first_line = *m_lines[selection.start().line()];
|
|
|
|
auto& second_line = *m_lines[selection.end().line()];
|
|
|
|
auto before_selection = String(first_line.characters(), first_line.length()).substring(0, selection.start().column());
|
|
|
|
auto after_selection = String(second_line.characters(), second_line.length()).substring(selection.end().column(), second_line.length() - selection.end().column());
|
2019-03-08 16:08:15 +03:00
|
|
|
StringBuilder builder(before_selection.length() + after_selection.length());
|
|
|
|
builder.append(before_selection);
|
|
|
|
builder.append(after_selection);
|
2019-03-08 17:55:58 +03:00
|
|
|
first_line.set_text(builder.to_string());
|
2019-03-08 20:28:24 +03:00
|
|
|
m_lines.remove(selection.end().line());
|
2019-03-08 16:08:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_lines.is_empty())
|
|
|
|
m_lines.append(make<Line>());
|
|
|
|
|
2019-03-08 20:28:24 +03:00
|
|
|
m_selection.clear();
|
|
|
|
set_cursor(selection.start());
|
2019-03-08 16:08:15 +03:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::insert_at_cursor_or_replace_selection(const String& text)
|
|
|
|
{
|
|
|
|
if (has_selection())
|
|
|
|
delete_selection();
|
|
|
|
insert_at_cursor(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::cut()
|
|
|
|
{
|
|
|
|
auto selected_text = this->selected_text();
|
|
|
|
printf("Cut: \"%s\"\n", selected_text.characters());
|
|
|
|
GClipboard::the().set_data(selected_text);
|
|
|
|
delete_selection();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::copy()
|
|
|
|
{
|
|
|
|
auto selected_text = this->selected_text();
|
|
|
|
printf("Copy: \"%s\"\n", selected_text.characters());
|
|
|
|
GClipboard::the().set_data(selected_text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::paste()
|
|
|
|
{
|
|
|
|
auto paste_text = GClipboard::the().data();
|
|
|
|
printf("Paste: \"%s\"\n", paste_text.characters());
|
|
|
|
insert_at_cursor_or_replace_selection(paste_text);
|
|
|
|
}
|