2019-06-07 12:46:02 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
#include <Kernel/KeyCode.h>
|
|
|
|
#include <LibGUI/GAction.h>
|
2019-03-08 16:08:15 +03:00
|
|
|
#include <LibGUI/GClipboard.h>
|
2019-06-07 12:46:02 +03:00
|
|
|
#include <LibGUI/GFontDatabase.h>
|
|
|
|
#include <LibGUI/GMenu.h>
|
2019-03-28 19:19:56 +03:00
|
|
|
#include <LibGUI/GPainter.h>
|
2019-06-07 12:46:02 +03:00
|
|
|
#include <LibGUI/GScrollBar.h>
|
|
|
|
#include <LibGUI/GTextEditor.h>
|
2019-04-01 00:52:02 +03:00
|
|
|
#include <LibGUI/GWindow.h>
|
2019-06-07 12:46:02 +03:00
|
|
|
#include <ctype.h>
|
2019-03-07 19:06:11 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
2019-06-07 12:46:02 +03:00
|
|
|
#include <unistd.h>
|
2019-03-07 02:31:06 +03:00
|
|
|
|
2019-09-16 21:57:32 +03:00
|
|
|
//#define DEBUG_GTEXTEDITOR
|
|
|
|
|
2019-03-15 19:37:13 +03:00
|
|
|
GTextEditor::GTextEditor(Type type, GWidget* parent)
|
2019-03-16 18:54:51 +03:00
|
|
|
: GScrollableWidget(parent)
|
2019-03-15 19:37:13 +03:00
|
|
|
, m_type(type)
|
2019-03-07 02:31:06 +03:00
|
|
|
{
|
2019-04-10 04:43:46 +03:00
|
|
|
set_frame_shape(FrameShape::Container);
|
|
|
|
set_frame_shadow(FrameShadow::Sunken);
|
2019-03-28 22:15:13 +03:00
|
|
|
set_frame_thickness(2);
|
2019-03-16 18:54:51 +03:00
|
|
|
set_scrollbars_enabled(is_multi_line());
|
2019-03-07 02:46:29 +03:00
|
|
|
set_font(GFontDatabase::the().get_by_name("Csilla Thin"));
|
2019-06-07 11:42:43 +03:00
|
|
|
// FIXME: Recompute vertical scrollbar step size on font change.
|
|
|
|
vertical_scrollbar().set_step(line_height());
|
2019-08-21 20:32:39 +03:00
|
|
|
m_lines.append(make<Line>(*this));
|
2019-03-19 03:41:00 +03:00
|
|
|
m_cursor = { 0, 0 };
|
2019-04-18 13:25:00 +03:00
|
|
|
create_actions();
|
2019-03-07 02:31:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
GTextEditor::~GTextEditor()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-18 13:25:00 +03:00
|
|
|
void GTextEditor::create_actions()
|
|
|
|
{
|
2019-09-14 23:23:49 +03:00
|
|
|
m_undo_action = GCommonActions::make_undo_action([&](auto&) {
|
|
|
|
// FIXME: Undo
|
|
|
|
});
|
|
|
|
m_redo_action = GCommonActions::make_redo_action([&](auto&) {
|
|
|
|
// FIXME: Undo
|
|
|
|
});
|
2019-09-14 23:10:44 +03:00
|
|
|
m_cut_action = GCommonActions::make_cut_action([&](auto&) { cut(); }, this);
|
|
|
|
m_copy_action = GCommonActions::make_copy_action([&](auto&) { copy(); }, this);
|
|
|
|
m_paste_action = GCommonActions::make_paste_action([&](auto&) { paste(); }, this);
|
|
|
|
m_delete_action = GCommonActions::make_delete_action([&](auto&) { do_delete(); }, this);
|
2019-04-18 13:25:00 +03:00
|
|
|
}
|
|
|
|
|
2019-06-02 15:58:02 +03:00
|
|
|
void GTextEditor::set_text(const StringView& text)
|
2019-03-07 02:31:06 +03:00
|
|
|
{
|
2019-07-24 10:12:23 +03:00
|
|
|
if (is_single_line() && text.length() == m_lines[0].length() && !memcmp(text.characters_without_null_termination(), m_lines[0].characters(), text.length()))
|
2019-03-20 20:12:56 +03:00
|
|
|
return;
|
|
|
|
|
2019-04-09 17:20:36 +03:00
|
|
|
m_selection.clear();
|
2019-03-07 02:31:06 +03:00
|
|
|
m_lines.clear();
|
|
|
|
int start_of_current_line = 0;
|
|
|
|
|
2019-06-07 12:46:02 +03:00
|
|
|
auto add_line = [&](int current_position) {
|
2019-03-07 02:31:06 +03:00
|
|
|
int line_length = current_position - start_of_current_line;
|
2019-08-21 20:32:39 +03:00
|
|
|
auto line = make<Line>(*this);
|
2019-03-07 02:31:06 +03:00
|
|
|
if (line_length)
|
2019-06-02 15:58:02 +03:00
|
|
|
line->set_text(text.substring_view(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-16 18:54:51 +03:00
|
|
|
update_content_size();
|
2019-08-25 09:43:01 +03:00
|
|
|
recompute_all_visual_lines();
|
2019-03-20 20:12:56 +03:00
|
|
|
if (is_single_line())
|
2019-07-24 10:12:23 +03:00
|
|
|
set_cursor(0, m_lines[0].length());
|
2019-03-20 20:12:56 +03:00
|
|
|
else
|
|
|
|
set_cursor(0, 0);
|
2019-04-12 03:52:34 +03:00
|
|
|
did_update_selection();
|
2019-03-07 02:31:06 +03:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2019-03-16 18:54:51 +03:00
|
|
|
void GTextEditor::update_content_size()
|
2019-03-07 15:54:02 +03:00
|
|
|
{
|
2019-03-16 18:54:51 +03:00
|
|
|
int content_width = 0;
|
2019-08-29 07:26:24 +03:00
|
|
|
int content_height = 0;
|
|
|
|
for (auto& line : m_lines) {
|
2019-09-06 03:03:53 +03:00
|
|
|
content_width = max(line.m_visual_rect.width(), content_width);
|
|
|
|
content_height += line.m_visual_rect.height();
|
2019-08-29 07:26:24 +03:00
|
|
|
}
|
2019-03-17 01:16:37 +03:00
|
|
|
content_width += m_horizontal_content_padding * 2;
|
2019-04-25 00:42:49 +03:00
|
|
|
if (is_right_text_alignment(m_text_alignment))
|
|
|
|
content_width = max(frame_inner_rect().width(), content_width);
|
2019-08-29 07:26:24 +03:00
|
|
|
|
2019-03-16 18:54:51 +03:00
|
|
|
set_content_size({ content_width, content_height });
|
|
|
|
set_size_occupied_by_fixed_elements({ ruler_width(), 0 });
|
2019-03-07 02:31:06 +03:00
|
|
|
}
|
|
|
|
|
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-16 18:54:51 +03:00
|
|
|
position.move_by(horizontal_scrollbar().value(), vertical_scrollbar().value());
|
2019-03-17 01:16:37 +03:00
|
|
|
position.move_by(-(m_horizontal_content_padding + ruler_width()), 0);
|
2019-04-25 03:15:45 +03:00
|
|
|
position.move_by(-frame_thickness(), -frame_thickness());
|
|
|
|
|
2019-08-25 09:43:01 +03:00
|
|
|
int line_index = -1;
|
|
|
|
|
|
|
|
if (is_line_wrapping_enabled()) {
|
|
|
|
for (int i = 0; i < m_lines.size(); ++i) {
|
|
|
|
auto& rect = m_lines[i].m_visual_rect;
|
|
|
|
if (position.y() >= rect.top() && position.y() <= rect.bottom()) {
|
|
|
|
line_index = i;
|
|
|
|
break;
|
2019-09-01 13:34:14 +03:00
|
|
|
} else if (position.y() > rect.bottom())
|
|
|
|
line_index = m_lines.size() - 1;
|
2019-08-25 09:43:01 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
line_index = position.y() / line_height();
|
|
|
|
}
|
|
|
|
|
2019-03-08 19:13:31 +03:00
|
|
|
line_index = max(0, min(line_index, line_count() - 1));
|
2019-04-24 23:24:16 +03:00
|
|
|
|
2019-08-25 09:43:01 +03:00
|
|
|
auto& line = m_lines[line_index];
|
|
|
|
|
2019-04-24 23:24:16 +03:00
|
|
|
int column_index;
|
|
|
|
switch (m_text_alignment) {
|
|
|
|
case TextAlignment::CenterLeft:
|
2019-04-25 03:15:45 +03:00
|
|
|
column_index = (position.x() + glyph_width() / 2) / glyph_width();
|
2019-08-25 09:43:01 +03:00
|
|
|
if (is_line_wrapping_enabled()) {
|
|
|
|
line.for_each_visual_line([&](const Rect& rect, const StringView&, int start_of_line) {
|
2019-09-01 18:30:23 +03:00
|
|
|
if (rect.contains_vertically(position.y())) {
|
2019-08-25 09:43:01 +03:00
|
|
|
column_index += start_of_line;
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
}
|
2019-04-24 23:24:16 +03:00
|
|
|
break;
|
|
|
|
case TextAlignment::CenterRight:
|
2019-08-25 09:43:01 +03:00
|
|
|
// FIXME: Support right-aligned line wrapping, I guess.
|
|
|
|
ASSERT(!is_line_wrapping_enabled());
|
2019-04-25 03:15:45 +03:00
|
|
|
column_index = (position.x() - content_x_for_position({ line_index, 0 }) + glyph_width() / 2) / glyph_width();
|
2019-04-24 23:24:16 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2019-07-24 10:12:23 +03:00
|
|
|
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 };
|
|
|
|
}
|
|
|
|
|
2019-04-25 23:29:25 +03:00
|
|
|
void GTextEditor::doubleclick_event(GMouseEvent& event)
|
|
|
|
{
|
|
|
|
if (event.button() != GMouseButton::Left)
|
|
|
|
return;
|
|
|
|
|
2019-05-16 01:40:55 +03:00
|
|
|
m_triple_click_timer.start();
|
2019-04-25 23:29:25 +03:00
|
|
|
m_in_drag_select = false;
|
|
|
|
|
|
|
|
auto start = text_position_at(event.position());
|
|
|
|
auto end = start;
|
2019-07-24 10:12:23 +03:00
|
|
|
auto& line = m_lines[start.line()];
|
2019-04-25 23:29:25 +03:00
|
|
|
while (start.column() > 0) {
|
|
|
|
if (isspace(line.characters()[start.column() - 1]))
|
|
|
|
break;
|
|
|
|
start.set_column(start.column() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (end.column() < line.length()) {
|
|
|
|
if (isspace(line.characters()[end.column()]))
|
|
|
|
break;
|
|
|
|
end.set_column(end.column() + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_selection.set(start, end);
|
|
|
|
set_cursor(end);
|
|
|
|
update();
|
|
|
|
did_update_selection();
|
|
|
|
}
|
|
|
|
|
2019-03-07 17:03:38 +03:00
|
|
|
void GTextEditor::mousedown_event(GMouseEvent& event)
|
|
|
|
{
|
2019-05-16 01:15:58 +03:00
|
|
|
if (event.button() != GMouseButton::Left) {
|
|
|
|
return;
|
|
|
|
}
|
2019-03-08 19:53:02 +03:00
|
|
|
|
2019-05-16 01:40:55 +03:00
|
|
|
if (m_triple_click_timer.is_valid() && m_triple_click_timer.elapsed() < 250) {
|
|
|
|
m_triple_click_timer = CElapsedTimer();
|
|
|
|
|
|
|
|
GTextPosition start;
|
|
|
|
GTextPosition end;
|
|
|
|
|
|
|
|
if (is_multi_line()) {
|
|
|
|
// select *current* line
|
|
|
|
start = GTextPosition(m_cursor.line(), 0);
|
2019-07-24 10:12:23 +03:00
|
|
|
end = GTextPosition(m_cursor.line(), m_lines[m_cursor.line()].length());
|
2019-05-16 01:40:55 +03:00
|
|
|
} else {
|
|
|
|
// select *whole* line
|
|
|
|
start = GTextPosition(0, 0);
|
2019-07-24 10:12:23 +03:00
|
|
|
end = GTextPosition(line_count() - 1, m_lines[line_count() - 1].length());
|
2019-05-16 01:40:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
m_selection.set(start, end);
|
|
|
|
set_cursor(end);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-16 01:15:58 +03:00
|
|
|
if (event.modifiers() & Mod_Shift) {
|
|
|
|
if (!has_selection())
|
2019-06-07 12:46:02 +03:00
|
|
|
m_selection.set(m_cursor, {});
|
2019-05-16 01:15:58 +03:00
|
|
|
} else {
|
|
|
|
m_selection.clear();
|
|
|
|
}
|
2019-03-08 19:53:02 +03:00
|
|
|
|
2019-05-16 01:15:58 +03:00
|
|
|
m_in_drag_select = true;
|
2019-03-08 19:53:02 +03:00
|
|
|
|
2019-05-16 01:15:58 +03:00
|
|
|
set_cursor(text_position_at(event.position()));
|
2019-03-08 20:28:24 +03:00
|
|
|
|
2019-05-16 01:15:58 +03:00
|
|
|
if (!(event.modifiers() & Mod_Shift)) {
|
|
|
|
if (!has_selection())
|
2019-06-07 12:46:02 +03:00
|
|
|
m_selection.set(m_cursor, {});
|
2019-03-08 19:53:02 +03:00
|
|
|
}
|
2019-05-16 01:15:58 +03:00
|
|
|
|
|
|
|
if (m_selection.start().is_valid() && m_selection.start() != m_cursor)
|
|
|
|
m_selection.set_end(m_cursor);
|
|
|
|
|
|
|
|
// FIXME: Only update the relevant rects.
|
|
|
|
update();
|
|
|
|
did_update_selection();
|
2019-03-08 19:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::mouseup_event(GMouseEvent& event)
|
|
|
|
{
|
|
|
|
if (event.button() == GMouseButton::Left) {
|
|
|
|
if (m_in_drag_select) {
|
|
|
|
m_in_drag_select = 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-04-12 03:52:34 +03:00
|
|
|
did_update_selection();
|
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
|
|
|
|
{
|
2019-03-15 19:54:05 +03:00
|
|
|
if (!m_ruler_visible)
|
|
|
|
return 0;
|
2019-03-07 22:05:05 +03:00
|
|
|
// FIXME: Resize based on needed space.
|
|
|
|
return 5 * font().glyph_width('x') + 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect GTextEditor::ruler_content_rect(int line_index) const
|
|
|
|
{
|
2019-03-15 19:54:05 +03:00
|
|
|
if (!m_ruler_visible)
|
2019-06-07 12:46:02 +03:00
|
|
|
return {};
|
2019-03-07 22:05:05 +03:00
|
|
|
return {
|
2019-03-17 01:16:37 +03:00
|
|
|
0 - ruler_width() + horizontal_scrollbar().value(),
|
2019-08-25 08:14:02 +03:00
|
|
|
line_content_rect(line_index).y(),
|
2019-03-07 22:05:05 +03:00
|
|
|
ruler_width(),
|
2019-08-25 08:14:02 +03:00
|
|
|
line_content_rect(line_index).height()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect GTextEditor::ruler_rect_in_inner_coordinates() const
|
|
|
|
{
|
|
|
|
return { 0, 0, ruler_width(), height() - height_occupied_by_horizontal_scrollbar() };
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect GTextEditor::visible_text_rect_in_inner_coordinates() const
|
|
|
|
{
|
|
|
|
return {
|
2019-09-01 21:04:25 +03:00
|
|
|
m_horizontal_content_padding + (m_ruler_visible ? (ruler_rect_in_inner_coordinates().right() + 1) : 0),
|
2019-08-25 08:14:02 +03:00
|
|
|
0,
|
2019-09-01 21:04:25 +03:00
|
|
|
frame_inner_rect().width() - (m_horizontal_content_padding * 2) - width_occupied_by_vertical_scrollbar() - ruler_width(),
|
|
|
|
frame_inner_rect().height() - height_occupied_by_horizontal_scrollbar()
|
2019-03-07 22:05:05 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:31:06 +03:00
|
|
|
void GTextEditor::paint_event(GPaintEvent& event)
|
|
|
|
{
|
2019-03-28 18:14:26 +03:00
|
|
|
GFrame::paint_event(event);
|
|
|
|
|
2019-03-28 19:19:56 +03:00
|
|
|
GPainter painter(*this);
|
2019-03-29 17:01:54 +03:00
|
|
|
painter.add_clip_rect(widget_inner_rect());
|
|
|
|
painter.add_clip_rect(event.rect());
|
2019-03-07 02:31:06 +03:00
|
|
|
painter.fill_rect(event.rect(), Color::White);
|
2019-03-07 22:05:05 +03:00
|
|
|
|
2019-04-25 02:09:44 +03:00
|
|
|
painter.translate(frame_thickness(), frame_thickness());
|
|
|
|
|
2019-08-25 08:14:02 +03:00
|
|
|
auto ruler_rect = ruler_rect_in_inner_coordinates();
|
2019-03-15 19:54:05 +03:00
|
|
|
|
|
|
|
if (m_ruler_visible) {
|
2019-06-30 10:23:16 +03:00
|
|
|
painter.fill_rect(ruler_rect, Color::WarmGray);
|
2019-03-15 19:54:05 +03:00
|
|
|
painter.draw_line(ruler_rect.top_right(), ruler_rect.bottom_right(), Color::DarkGray);
|
|
|
|
}
|
2019-03-07 22:05:05 +03:00
|
|
|
|
2019-03-16 18:54:51 +03:00
|
|
|
painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
|
2019-03-19 04:45:23 +03:00
|
|
|
if (m_ruler_visible)
|
|
|
|
painter.translate(ruler_width(), 0);
|
2019-03-07 02:31:06 +03:00
|
|
|
|
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-15 19:54:05 +03:00
|
|
|
if (m_ruler_visible) {
|
|
|
|
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);
|
|
|
|
painter.draw_text(
|
2019-09-06 20:24:16 +03:00
|
|
|
ruler_line_rect.shrunken(2, 0).translated(0, m_line_spacing / 2),
|
2019-07-27 22:20:38 +03:00
|
|
|
String::number(i + 1),
|
2019-03-15 19:54:05 +03:00
|
|
|
is_current_line ? Font::default_bold_font() : font(),
|
2019-09-06 20:24:16 +03:00
|
|
|
TextAlignment::TopRight,
|
2019-06-07 12:46:02 +03:00
|
|
|
is_current_line ? Color::DarkGray : Color::MidGray);
|
2019-03-15 19:54:05 +03:00
|
|
|
}
|
2019-03-07 22:05:05 +03:00
|
|
|
}
|
|
|
|
|
2019-08-25 08:14:02 +03:00
|
|
|
Rect text_clip_rect {
|
|
|
|
(m_ruler_visible ? (ruler_rect_in_inner_coordinates().right() + frame_thickness() + 1) : frame_thickness()),
|
|
|
|
frame_thickness(),
|
|
|
|
width() - width_occupied_by_vertical_scrollbar() - ruler_width(),
|
|
|
|
height() - height_occupied_by_horizontal_scrollbar()
|
|
|
|
};
|
|
|
|
painter.add_clip_rect(text_clip_rect);
|
2019-03-07 22:05:05 +03:00
|
|
|
|
2019-08-25 11:23:11 +03:00
|
|
|
for (int line_index = first_visible_line; line_index <= last_visible_line; ++line_index) {
|
|
|
|
auto& line = m_lines[line_index];
|
|
|
|
|
|
|
|
bool physical_line_has_selection = has_selection && line_index >= selection.start().line() && line_index <= selection.end().line();
|
|
|
|
int first_visual_line_with_selection = -1;
|
|
|
|
int last_visual_line_with_selection = -1;
|
|
|
|
if (physical_line_has_selection) {
|
2019-08-25 15:04:46 +03:00
|
|
|
if (selection.start().line() < line_index)
|
2019-08-25 11:23:11 +03:00
|
|
|
first_visual_line_with_selection = 0;
|
2019-08-25 15:04:46 +03:00
|
|
|
else
|
|
|
|
first_visual_line_with_selection = line.visual_line_containing(selection.start().column());
|
|
|
|
|
|
|
|
if (selection.end().line() > line_index)
|
2019-08-25 11:23:11 +03:00
|
|
|
last_visual_line_with_selection = line.m_visual_line_breaks.size();
|
2019-08-25 15:04:46 +03:00
|
|
|
else
|
|
|
|
last_visual_line_with_selection = line.visual_line_containing(selection.end().column());
|
2019-08-25 11:23:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int selection_start_column_within_line = selection.start().line() == line_index ? selection.start().column() : 0;
|
|
|
|
int selection_end_column_within_line = selection.end().line() == line_index ? selection.end().column() : line.length();
|
|
|
|
|
|
|
|
int visual_line_index = 0;
|
|
|
|
line.for_each_visual_line([&](const Rect& visual_line_rect, const StringView& visual_line_text, int start_of_visual_line) {
|
|
|
|
if (is_multi_line() && line_index == m_cursor.line())
|
|
|
|
painter.fill_rect(visual_line_rect, Color(230, 230, 230));
|
2019-09-16 21:57:32 +03:00
|
|
|
#ifdef DEBUG_GTEXTEDITOR
|
|
|
|
painter.draw_rect(visual_line_rect, Color::Cyan);
|
|
|
|
#endif
|
2019-10-25 22:05:06 +03:00
|
|
|
if (m_spans.is_empty()) {
|
|
|
|
// Fast-path for plain text
|
|
|
|
painter.draw_text(visual_line_rect, visual_line_text, m_text_alignment, Color::Black);
|
|
|
|
} else {
|
|
|
|
int advance = font().glyph_width(' ') + font().glyph_spacing();
|
|
|
|
Rect character_rect = { visual_line_rect.location(), { font().glyph_width(' '), line_height() } };
|
|
|
|
for (int i = 0; i < visual_line_text.length(); ++i) {
|
2019-10-26 01:13:07 +03:00
|
|
|
const Font* font = &this->font();
|
2019-10-25 22:05:06 +03:00
|
|
|
Color color;
|
|
|
|
int physical_line = line_index;
|
|
|
|
int physical_column = start_of_visual_line + i;
|
|
|
|
// FIXME: This is *horribly* inefficient.
|
|
|
|
for (auto& span : m_spans) {
|
|
|
|
if (!span.contains(GTextPosition(physical_line, physical_column)))
|
|
|
|
continue;
|
|
|
|
color = span.color;
|
2019-10-26 01:13:07 +03:00
|
|
|
if (span.font)
|
|
|
|
font = span.font;
|
2019-10-25 22:05:06 +03:00
|
|
|
break;
|
|
|
|
}
|
2019-10-26 01:13:07 +03:00
|
|
|
painter.draw_text(character_rect, visual_line_text.substring_view(i, 1), *font, m_text_alignment, color);
|
2019-10-25 22:05:06 +03:00
|
|
|
character_rect.move_by(advance, 0);
|
|
|
|
}
|
|
|
|
}
|
2019-08-25 11:23:11 +03:00
|
|
|
bool physical_line_has_selection = has_selection && line_index >= selection.start().line() && line_index <= selection.end().line();
|
|
|
|
if (physical_line_has_selection) {
|
|
|
|
|
|
|
|
bool current_visual_line_has_selection = (line_index != selection.start().line() && line_index != selection.end().line())
|
|
|
|
|| (visual_line_index >= first_visual_line_with_selection && visual_line_index <= last_visual_line_with_selection);
|
|
|
|
if (current_visual_line_has_selection) {
|
|
|
|
bool selection_begins_on_current_visual_line = visual_line_index == first_visual_line_with_selection;
|
|
|
|
bool selection_ends_on_current_visual_line = visual_line_index == last_visual_line_with_selection;
|
|
|
|
|
|
|
|
int selection_left = selection_begins_on_current_visual_line
|
|
|
|
? content_x_for_position({ line_index, selection_start_column_within_line })
|
|
|
|
: m_horizontal_content_padding;
|
|
|
|
|
|
|
|
int selection_right = selection_ends_on_current_visual_line
|
|
|
|
? content_x_for_position({ line_index, selection_end_column_within_line })
|
2019-09-01 18:30:23 +03:00
|
|
|
: visual_line_rect.right() + 1;
|
2019-08-25 11:23:11 +03:00
|
|
|
|
|
|
|
Rect selection_rect {
|
|
|
|
selection_left,
|
|
|
|
visual_line_rect.y(),
|
|
|
|
selection_right - selection_left,
|
|
|
|
visual_line_rect.height()
|
|
|
|
};
|
|
|
|
|
|
|
|
painter.fill_rect(selection_rect, Color::from_rgb(0x955233));
|
|
|
|
|
|
|
|
int start_of_selection_within_visual_line = max(0, selection_start_column_within_line - start_of_visual_line);
|
|
|
|
int end_of_selection_within_visual_line = selection_end_column_within_line - start_of_visual_line;
|
|
|
|
|
|
|
|
StringView visual_selected_text {
|
|
|
|
visual_line_text.characters_without_null_termination() + start_of_selection_within_visual_line,
|
|
|
|
end_of_selection_within_visual_line - start_of_selection_within_visual_line
|
|
|
|
};
|
|
|
|
|
|
|
|
painter.draw_text(selection_rect, visual_selected_text, TextAlignment::CenterLeft, Color::White);
|
|
|
|
}
|
2019-08-25 09:43:01 +03:00
|
|
|
}
|
2019-08-25 11:23:11 +03:00
|
|
|
++visual_line_index;
|
2019-08-25 09:43:01 +03:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
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-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()) {
|
2019-06-07 12:46:02 +03:00
|
|
|
m_selection.set(m_cursor, {});
|
2019-04-12 03:52:34 +03:00
|
|
|
did_update_selection();
|
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-04-12 03:52:34 +03:00
|
|
|
did_update_selection();
|
2019-03-08 02:49:45 +03:00
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 11:38:38 +03:00
|
|
|
void GTextEditor::select_all()
|
|
|
|
{
|
|
|
|
GTextPosition start_of_document { 0, 0 };
|
2019-07-24 10:12:23 +03:00
|
|
|
GTextPosition end_of_document { line_count() - 1, m_lines[line_count() - 1].length() };
|
2019-06-22 11:38:38 +03:00
|
|
|
m_selection.set(start_of_document, end_of_document);
|
|
|
|
did_update_selection();
|
|
|
|
set_cursor(end_of_document);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:31:06 +03:00
|
|
|
void GTextEditor::keydown_event(GKeyEvent& event)
|
|
|
|
{
|
2019-05-15 03:39:58 +03:00
|
|
|
if (is_single_line() && event.key() == KeyCode::Key_Tab)
|
|
|
|
return GWidget::keydown_event(event);
|
|
|
|
|
2019-06-23 08:53:58 +03:00
|
|
|
if (is_single_line() && event.key() == KeyCode::Key_Return) {
|
|
|
|
if (on_return_pressed)
|
|
|
|
on_return_pressed();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-19 03:41:00 +03:00
|
|
|
if (event.key() == KeyCode::Key_Escape) {
|
|
|
|
if (on_escape_pressed)
|
2019-04-10 04:08:29 +03:00
|
|
|
on_escape_pressed();
|
2019-03-19 03:41:00 +03:00
|
|
|
return;
|
|
|
|
}
|
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-07-24 10:12:23 +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-10-26 13:57:05 +03:00
|
|
|
if (event.shift() && m_selection.start().is_valid()) {
|
2019-03-08 20:28:24 +03:00
|
|
|
m_selection.set_end(m_cursor);
|
2019-04-12 03:52:34 +03:00
|
|
|
did_update_selection();
|
|
|
|
}
|
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-07-24 10:12:23 +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-10-26 13:57:05 +03:00
|
|
|
if (event.shift() && m_selection.start().is_valid()) {
|
2019-03-08 20:28:24 +03:00
|
|
|
m_selection.set_end(m_cursor);
|
2019-04-12 03:52:34 +03:00
|
|
|
did_update_selection();
|
|
|
|
}
|
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());
|
2019-07-24 10:12:23 +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 19:11:17 +03:00
|
|
|
set_cursor(new_line, new_column);
|
2019-10-26 13:57:05 +03:00
|
|
|
if (event.shift() && m_selection.start().is_valid()) {
|
2019-03-08 20:28:24 +03:00
|
|
|
m_selection.set_end(m_cursor);
|
2019-04-12 03:52:34 +03:00
|
|
|
did_update_selection();
|
|
|
|
}
|
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());
|
2019-07-24 10:12:23 +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 19:11:17 +03:00
|
|
|
set_cursor(new_line, new_column);
|
2019-10-26 13:57:05 +03:00
|
|
|
if (event.shift() && m_selection.start().is_valid()) {
|
2019-03-08 20:28:24 +03:00
|
|
|
m_selection.set_end(m_cursor);
|
2019-04-12 03:52:34 +03:00
|
|
|
did_update_selection();
|
|
|
|
}
|
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-10-26 13:57:05 +03:00
|
|
|
if (event.shift() && m_selection.start().is_valid()) {
|
2019-03-08 20:28:24 +03:00
|
|
|
m_selection.set_end(m_cursor);
|
2019-04-12 03:52:34 +03:00
|
|
|
did_update_selection();
|
|
|
|
}
|
2019-03-10 00:02:35 +03:00
|
|
|
} else if (m_cursor.line() > 0) {
|
|
|
|
int new_line = m_cursor.line() - 1;
|
2019-07-24 10:12:23 +03:00
|
|
|
int new_column = m_lines[new_line].length();
|
2019-03-10 00:02:35 +03:00
|
|
|
toggle_selection_if_needed_for_event(event);
|
|
|
|
set_cursor(new_line, new_column);
|
2019-10-26 13:57:05 +03:00
|
|
|
if (event.shift() && m_selection.start().is_valid()) {
|
2019-03-10 00:02:35 +03:00
|
|
|
m_selection.set_end(m_cursor);
|
2019-04-12 03:52:34 +03:00
|
|
|
did_update_selection();
|
|
|
|
}
|
2019-03-07 02:31:06 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-03-08 02:49:45 +03:00
|
|
|
if (event.key() == KeyCode::Key_Right) {
|
2019-08-26 07:06:19 +03:00
|
|
|
int new_line = m_cursor.line();
|
|
|
|
int new_column = m_cursor.column();
|
2019-03-07 15:21:51 +03:00
|
|
|
if (m_cursor.column() < current_line().length()) {
|
2019-08-26 07:06:19 +03:00
|
|
|
new_line = m_cursor.line();
|
|
|
|
new_column = m_cursor.column() + 1;
|
2019-03-10 00:02:35 +03:00
|
|
|
} else if (m_cursor.line() != line_count() - 1) {
|
2019-08-26 07:06:19 +03:00
|
|
|
new_line = m_cursor.line() + 1;
|
|
|
|
new_column = 0;
|
2019-09-14 23:10:44 +03:00
|
|
|
}
|
2019-08-26 07:06:19 +03:00
|
|
|
toggle_selection_if_needed_for_event(event);
|
|
|
|
set_cursor(new_line, new_column);
|
2019-10-26 13:57:05 +03:00
|
|
|
if (event.shift() && m_selection.start().is_valid()) {
|
2019-08-26 07:06:19 +03:00
|
|
|
m_selection.set_end(m_cursor);
|
|
|
|
did_update_selection();
|
2019-09-14 23:10:44 +03:00
|
|
|
}
|
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-10-26 13:57:05 +03:00
|
|
|
if (event.shift() && m_selection.start().is_valid()) {
|
2019-03-08 20:28:24 +03:00
|
|
|
m_selection.set_end(m_cursor);
|
2019-04-12 03:52:34 +03:00
|
|
|
did_update_selection();
|
|
|
|
}
|
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-10-26 13:57:05 +03:00
|
|
|
if (event.shift() && m_selection.start().is_valid()) {
|
2019-03-08 20:28:24 +03:00
|
|
|
m_selection.set_end(m_cursor);
|
2019-04-12 03:52:34 +03:00
|
|
|
did_update_selection();
|
|
|
|
}
|
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-10-26 13:57:05 +03:00
|
|
|
if (event.shift() && m_selection.start().is_valid()) {
|
2019-03-08 20:28:24 +03:00
|
|
|
m_selection.set_end(m_cursor);
|
2019-04-12 03:52:34 +03:00
|
|
|
did_update_selection();
|
|
|
|
}
|
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-07-24 10:12:23 +03:00
|
|
|
set_cursor(line_count() - 1, m_lines[line_count() - 1].length());
|
2019-10-26 13:57:05 +03:00
|
|
|
if (event.shift() && m_selection.start().is_valid()) {
|
2019-03-08 20:28:24 +03:00
|
|
|
m_selection.set_end(m_cursor);
|
2019-04-12 03:52:34 +03:00
|
|
|
did_update_selection();
|
|
|
|
}
|
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-06-22 11:38:38 +03:00
|
|
|
select_all();
|
2019-03-08 16:12:41 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-03-07 16:35:32 +03:00
|
|
|
|
2019-03-08 20:50:14 +03:00
|
|
|
if (event.key() == KeyCode::Key_Backspace) {
|
2019-05-08 06:00:28 +03:00
|
|
|
if (is_readonly())
|
|
|
|
return;
|
2019-03-08 16:10:34 +03:00
|
|
|
if (has_selection()) {
|
|
|
|
delete_selection();
|
2019-04-12 03:52:34 +03:00
|
|
|
did_update_selection();
|
2019-03-08 16:10:34 +03:00
|
|
|
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-16 18:54:51 +03:00
|
|
|
update_content_size();
|
2019-03-07 18:04:21 +03:00
|
|
|
set_cursor(m_cursor.line(), m_cursor.column() - 1);
|
2019-08-27 21:16:52 +03:00
|
|
|
did_change();
|
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-07-24 10:12:23 +03:00
|
|
|
auto& previous_line = m_lines[m_cursor.line() - 1];
|
2019-03-07 18:15:25 +03:00
|
|
|
int previous_length = previous_line.length();
|
|
|
|
previous_line.append(current_line().characters(), current_line().length());
|
|
|
|
m_lines.remove(m_cursor.line());
|
2019-03-16 18:54:51 +03:00
|
|
|
update_content_size();
|
2019-03-07 18:15:25 +03:00
|
|
|
update();
|
|
|
|
set_cursor(m_cursor.line() - 1, previous_length);
|
2019-08-27 21:16:52 +03:00
|
|
|
did_change();
|
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-25 15:13:46 +03:00
|
|
|
if (event.modifiers() == Mod_Shift && event.key() == KeyCode::Key_Delete) {
|
2019-05-08 06:00:28 +03:00
|
|
|
if (is_readonly())
|
|
|
|
return;
|
2019-03-25 15:13:46 +03:00
|
|
|
delete_current_line();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-08 20:50:14 +03:00
|
|
|
if (event.key() == KeyCode::Key_Delete) {
|
2019-05-08 06:00:28 +03:00
|
|
|
if (is_readonly())
|
|
|
|
return;
|
2019-03-21 01:11:00 +03:00
|
|
|
do_delete();
|
2019-03-07 18:33:07 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-08 06:00:28 +03:00
|
|
|
if (!is_readonly() && !event.ctrl() && !event.alt() && !event.text().is_empty())
|
2019-03-08 16:10:34 +03:00
|
|
|
insert_at_cursor_or_replace_selection(event.text());
|
2019-03-07 02:31:06 +03:00
|
|
|
}
|
|
|
|
|
2019-03-25 15:13:46 +03:00
|
|
|
void GTextEditor::delete_current_line()
|
|
|
|
{
|
|
|
|
if (has_selection())
|
|
|
|
return delete_selection();
|
|
|
|
|
|
|
|
m_lines.remove(m_cursor.line());
|
|
|
|
if (m_lines.is_empty())
|
2019-08-21 20:32:39 +03:00
|
|
|
m_lines.append(make<Line>(*this));
|
2019-03-25 15:13:46 +03:00
|
|
|
|
|
|
|
update_content_size();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2019-03-21 01:11:00 +03:00
|
|
|
void GTextEditor::do_delete()
|
|
|
|
{
|
2019-05-08 06:00:28 +03:00
|
|
|
if (is_readonly())
|
|
|
|
return;
|
|
|
|
|
2019-03-25 15:13:46 +03:00
|
|
|
if (has_selection())
|
|
|
|
return delete_selection();
|
|
|
|
|
2019-03-21 01:11:00 +03:00
|
|
|
if (m_cursor.column() < current_line().length()) {
|
|
|
|
// Delete within line
|
|
|
|
current_line().remove(m_cursor.column());
|
2019-04-09 17:20:36 +03:00
|
|
|
did_change();
|
2019-04-25 02:28:17 +03:00
|
|
|
update_cursor();
|
2019-03-21 01:11:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (m_cursor.column() == current_line().length() && m_cursor.line() != line_count() - 1) {
|
|
|
|
// Delete at end of line; merge with next line
|
2019-07-24 10:12:23 +03:00
|
|
|
auto& next_line = m_lines[m_cursor.line() + 1];
|
2019-03-21 01:11:00 +03:00
|
|
|
int previous_length = current_line().length();
|
|
|
|
current_line().append(next_line.characters(), next_line.length());
|
|
|
|
m_lines.remove(m_cursor.line() + 1);
|
|
|
|
update();
|
2019-04-09 17:20:36 +03:00
|
|
|
did_change();
|
2019-04-25 02:28:17 +03:00
|
|
|
set_cursor(m_cursor.line(), previous_length);
|
2019-03-21 01:11:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-02 15:58:02 +03:00
|
|
|
void GTextEditor::insert_at_cursor(const StringView& text)
|
2019-03-08 16:08:15 +03:00
|
|
|
{
|
|
|
|
// 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) {
|
2019-04-25 23:56:09 +03:00
|
|
|
String new_line_contents;
|
|
|
|
if (m_automatic_indentation_enabled && at_tail) {
|
|
|
|
int leading_spaces = 0;
|
2019-07-24 10:12:23 +03:00
|
|
|
auto& old_line = m_lines[m_cursor.line()];
|
2019-04-25 23:56:09 +03:00
|
|
|
for (int i = 0; i < old_line.length(); ++i) {
|
|
|
|
if (old_line.characters()[i] == ' ')
|
|
|
|
++leading_spaces;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (leading_spaces)
|
|
|
|
new_line_contents = String::repeated(' ', leading_spaces);
|
|
|
|
}
|
2019-08-21 20:32:39 +03:00
|
|
|
m_lines.insert(m_cursor.line() + (at_tail ? 1 : 0), make<Line>(*this, new_line_contents));
|
2019-03-07 17:52:11 +03:00
|
|
|
update();
|
2019-04-09 17:20:36 +03:00
|
|
|
did_change();
|
2019-07-24 10:12:23 +03:00
|
|
|
set_cursor(m_cursor.line() + 1, m_lines[m_cursor.line() + 1].length());
|
2019-03-07 17:52:11 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-08-21 20:32:39 +03:00
|
|
|
auto new_line = make<Line>(*this);
|
2019-03-07 18:49:04 +03:00
|
|
|
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();
|
2019-04-09 17:20:36 +03:00
|
|
|
did_change();
|
2019-04-25 02:28:17 +03:00
|
|
|
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-10 13:08:36 +03:00
|
|
|
if (ch == '\t') {
|
|
|
|
int next_soft_tab_stop = ((m_cursor.column() + m_soft_tab_width) / m_soft_tab_width) * m_soft_tab_width;
|
|
|
|
int spaces_to_insert = next_soft_tab_stop - m_cursor.column();
|
|
|
|
for (int i = 0; i < spaces_to_insert; ++i) {
|
|
|
|
current_line().insert(m_cursor.column(), ' ');
|
|
|
|
}
|
2019-04-09 17:20:36 +03:00
|
|
|
did_change();
|
2019-04-25 02:28:17 +03:00
|
|
|
set_cursor(m_cursor.line(), next_soft_tab_stop);
|
2019-03-10 13:08:36 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-03-07 18:04:21 +03:00
|
|
|
current_line().insert(m_cursor.column(), ch);
|
2019-04-09 17:20:36 +03:00
|
|
|
did_change();
|
2019-04-25 02:28:17 +03:00
|
|
|
set_cursor(m_cursor.line(), m_cursor.column() + 1);
|
2019-03-07 16:35:32 +03:00
|
|
|
}
|
|
|
|
|
2019-04-24 23:24:16 +03:00
|
|
|
int GTextEditor::content_x_for_position(const GTextPosition& position) const
|
|
|
|
{
|
2019-07-24 10:12:23 +03:00
|
|
|
auto& line = m_lines[position.line()];
|
2019-08-25 10:06:54 +03:00
|
|
|
int x_offset = -1;
|
2019-04-24 23:24:16 +03:00
|
|
|
switch (m_text_alignment) {
|
|
|
|
case TextAlignment::CenterLeft:
|
2019-08-25 10:06:54 +03:00
|
|
|
line.for_each_visual_line([&](const Rect&, const StringView& view, int start_of_visual_line) {
|
2019-08-25 11:23:11 +03:00
|
|
|
if (position.column() >= start_of_visual_line && ((position.column() - start_of_visual_line) <= view.length())) {
|
2019-08-25 10:06:54 +03:00
|
|
|
x_offset = (position.column() - start_of_visual_line) * glyph_width();
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
return m_horizontal_content_padding + x_offset;
|
2019-04-24 23:24:16 +03:00
|
|
|
case TextAlignment::CenterRight:
|
2019-08-25 10:06:54 +03:00
|
|
|
// FIXME
|
|
|
|
ASSERT(!is_line_wrapping_enabled());
|
2019-04-25 00:42:49 +03:00
|
|
|
return content_width() - m_horizontal_content_padding - (line.length() * glyph_width()) + (position.column() * glyph_width());
|
2019-04-24 23:24:16 +03:00
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 22:23:17 +03:00
|
|
|
Rect GTextEditor::content_rect_for_position(const GTextPosition& position) const
|
2019-03-07 02:31:06 +03:00
|
|
|
{
|
2019-08-21 22:23:17 +03:00
|
|
|
if (!position.is_valid())
|
2019-06-07 12:46:02 +03:00
|
|
|
return {};
|
2019-03-07 02:31:06 +03:00
|
|
|
ASSERT(!m_lines.is_empty());
|
2019-08-21 22:23:17 +03:00
|
|
|
ASSERT(position.column() <= (current_line().length() + 1));
|
2019-04-24 23:24:16 +03:00
|
|
|
|
2019-08-21 22:23:17 +03:00
|
|
|
int x = content_x_for_position(position);
|
2019-04-24 23:24:16 +03:00
|
|
|
|
2019-03-28 17:30:29 +03:00
|
|
|
if (is_single_line()) {
|
2019-08-21 22:23:17 +03:00
|
|
|
Rect rect { x, 0, 1, font().glyph_height() + 2 };
|
|
|
|
rect.center_vertically_within({ {}, frame_inner_rect().size() });
|
|
|
|
return rect;
|
2019-03-28 17:30:29 +03:00
|
|
|
}
|
2019-08-25 10:06:54 +03:00
|
|
|
|
|
|
|
auto& line = m_lines[position.line()];
|
|
|
|
Rect rect;
|
|
|
|
line.for_each_visual_line([&](const Rect& visual_line_rect, const StringView& view, int start_of_visual_line) {
|
2019-08-25 11:23:11 +03:00
|
|
|
if (position.column() >= start_of_visual_line && ((position.column() - start_of_visual_line) <= view.length())) {
|
2019-08-25 10:06:54 +03:00
|
|
|
// NOTE: We have to subtract the horizontal padding here since it's part of the visual line rect
|
|
|
|
// *and* included in what we get from content_x_for_position().
|
|
|
|
rect = {
|
|
|
|
visual_line_rect.x() + x - (m_horizontal_content_padding),
|
|
|
|
visual_line_rect.y(),
|
|
|
|
1,
|
|
|
|
line_height()
|
|
|
|
};
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
return rect;
|
2019-08-21 22:23:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Rect GTextEditor::cursor_content_rect() const
|
|
|
|
{
|
|
|
|
return content_rect_for_position(m_cursor);
|
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
|
|
|
|
{
|
|
|
|
auto rect = line_content_rect(line_index);
|
2019-04-25 00:15:15 +03:00
|
|
|
rect.set_x(frame_thickness());
|
|
|
|
rect.set_width(frame_inner_rect().width());
|
|
|
|
rect.move_by(0, -(vertical_scrollbar().value()));
|
2019-04-25 02:09:44 +03:00
|
|
|
rect.move_by(0, frame_thickness());
|
|
|
|
rect.intersect(frame_inner_rect());
|
2019-03-07 15:13:25 +03:00
|
|
|
return rect;
|
2019-03-07 04:24:46 +03:00
|
|
|
}
|
|
|
|
|
2019-08-21 22:23:17 +03:00
|
|
|
void GTextEditor::scroll_position_into_view(const GTextPosition& position)
|
2019-03-07 02:31:06 +03:00
|
|
|
{
|
2019-08-21 22:23:17 +03:00
|
|
|
auto rect = content_rect_for_position(position);
|
|
|
|
if (position.column() == 0)
|
|
|
|
rect.set_x(content_x_for_position({ position.line(), 0 }) - 2);
|
|
|
|
else if (position.column() == m_lines[position.line()].length())
|
|
|
|
rect.set_x(content_x_for_position({ position.line(), m_lines[position.line()].length() }) + 2);
|
2019-04-25 02:33:59 +03:00
|
|
|
scroll_into_view(rect, true, true);
|
2019-03-07 02:31:06 +03:00
|
|
|
}
|
|
|
|
|
2019-08-21 22:23:17 +03:00
|
|
|
void GTextEditor::scroll_cursor_into_view()
|
|
|
|
{
|
|
|
|
scroll_position_into_view(m_cursor);
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:31:06 +03:00
|
|
|
Rect GTextEditor::line_content_rect(int line_index) const
|
|
|
|
{
|
2019-07-24 10:12:23 +03:00
|
|
|
auto& line = m_lines[line_index];
|
2019-03-28 17:30:29 +03:00
|
|
|
if (is_single_line()) {
|
2019-04-24 23:46:27 +03:00
|
|
|
Rect line_rect = { content_x_for_position({ line_index, 0 }), 0, line.length() * glyph_width(), font().glyph_height() + 2 };
|
2019-06-07 12:46:02 +03:00
|
|
|
line_rect.center_vertically_within({ {}, frame_inner_rect().size() });
|
2019-03-28 17:30:29 +03:00
|
|
|
return line_rect;
|
|
|
|
}
|
2019-08-25 09:43:01 +03:00
|
|
|
if (is_line_wrapping_enabled())
|
|
|
|
return line.m_visual_rect;
|
2019-03-07 02:31:06 +03:00
|
|
|
return {
|
2019-04-24 23:46:27 +03:00
|
|
|
content_x_for_position({ line_index, 0 }),
|
2019-03-07 03:05:35 +03:00
|
|
|
line_index * line_height(),
|
2019-04-24 23:46:27 +03:00
|
|
|
line.length() * glyph_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 });
|
|
|
|
}
|
|
|
|
|
2019-10-21 19:58:27 +03:00
|
|
|
void GTextEditor::set_cursor(const GTextPosition& a_position)
|
2019-03-07 17:03:38 +03:00
|
|
|
{
|
2019-03-08 16:08:15 +03:00
|
|
|
ASSERT(!m_lines.is_empty());
|
2019-10-21 19:58:27 +03:00
|
|
|
|
|
|
|
GTextPosition position = a_position;
|
|
|
|
|
|
|
|
if (position.line() >= m_lines.size())
|
|
|
|
position.set_line(m_lines.size() - 1);
|
|
|
|
|
|
|
|
if (position.column() > m_lines[position.line()].length())
|
|
|
|
position.set_column(m_lines[position.line()].length());
|
|
|
|
|
2019-03-08 19:53:02 +03:00
|
|
|
if (m_cursor != position) {
|
2019-05-06 23:04:53 +03:00
|
|
|
// NOTE: If the old cursor is no longer valid, repaint everything just in case.
|
|
|
|
auto old_cursor_line_rect = m_cursor.line() < m_lines.size()
|
|
|
|
? line_widget_rect(m_cursor.line())
|
|
|
|
: rect();
|
2019-03-08 19:53:02 +03:00
|
|
|
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)
|
2019-04-10 04:08:29 +03:00
|
|
|
on_cursor_change();
|
2019-03-07 02:46:29 +03:00
|
|
|
}
|
|
|
|
|
2019-04-10 17:56:55 +03:00
|
|
|
void GTextEditor::focusin_event(CEvent&)
|
2019-03-07 03:05:35 +03:00
|
|
|
{
|
|
|
|
update_cursor();
|
|
|
|
start_timer(500);
|
|
|
|
}
|
|
|
|
|
2019-04-10 17:56:55 +03:00
|
|
|
void GTextEditor::focusout_event(CEvent&)
|
2019-03-07 03:05:35 +03:00
|
|
|
{
|
|
|
|
stop_timer();
|
|
|
|
}
|
|
|
|
|
2019-04-10 17:56:55 +03:00
|
|
|
void GTextEditor::timer_event(CTimerEvent&)
|
2019-03-07 03:05:35 +03:00
|
|
|
{
|
|
|
|
m_cursor_state = !m_cursor_state;
|
|
|
|
if (is_focused())
|
|
|
|
update_cursor();
|
|
|
|
}
|
|
|
|
|
2019-08-21 20:32:39 +03:00
|
|
|
GTextEditor::Line::Line(GTextEditor& editor)
|
|
|
|
: m_editor(editor)
|
2019-03-07 16:35:32 +03:00
|
|
|
{
|
2019-03-08 17:55:58 +03:00
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2019-08-21 20:32:39 +03:00
|
|
|
GTextEditor::Line::Line(GTextEditor& editor, const StringView& text)
|
|
|
|
: m_editor(editor)
|
2019-04-25 23:56:09 +03:00
|
|
|
{
|
|
|
|
set_text(text);
|
|
|
|
}
|
|
|
|
|
2019-03-08 17:55:58 +03:00
|
|
|
void GTextEditor::Line::clear()
|
|
|
|
{
|
|
|
|
m_text.clear();
|
2019-03-07 16:35:32 +03:00
|
|
|
m_text.append(0);
|
|
|
|
}
|
|
|
|
|
2019-06-02 15:58:02 +03:00
|
|
|
void GTextEditor::Line::set_text(const StringView& text)
|
2019-03-07 02:31:06 +03:00
|
|
|
{
|
2019-07-08 16:38:44 +03:00
|
|
|
if (text.length() == length() && !memcmp(text.characters_without_null_termination(), 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);
|
2019-07-08 16:38:44 +03:00
|
|
|
memcpy(m_text.data(), text.characters_without_null_termination(), text.length() + 1);
|
2019-03-07 02:31:06 +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
|
|
|
|
2019-06-02 15:58:02 +03:00
|
|
|
bool GTextEditor::write_to_file(const StringView& path)
|
2019-03-07 19:06:11 +03:00
|
|
|
{
|
2019-07-08 21:01:49 +03:00
|
|
|
int fd = open_with_path_length(path.characters_without_null_termination(), path.length(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
2019-03-07 19:06:11 +03:00
|
|
|
if (fd < 0) {
|
|
|
|
perror("open");
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-28 20:32:45 +03:00
|
|
|
|
|
|
|
// Compute the final file size and ftruncate() to make writing fast.
|
|
|
|
// FIXME: Remove this once the kernel is smart enough to do this instead.
|
|
|
|
off_t file_size = 0;
|
|
|
|
for (int i = 0; i < m_lines.size(); ++i)
|
|
|
|
file_size += m_lines[i].length();
|
|
|
|
file_size += m_lines.size() - 1;
|
|
|
|
|
|
|
|
int rc = ftruncate(fd, file_size);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("ftruncate");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-07 19:06:11 +03:00
|
|
|
for (int i = 0; i < m_lines.size(); ++i) {
|
2019-07-24 10:12:23 +03:00
|
|
|
auto& line = m_lines[i];
|
2019-03-07 19:06:11 +03:00
|
|
|
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
|
|
|
|
2019-03-15 19:37:13 +03:00
|
|
|
String GTextEditor::text() const
|
|
|
|
{
|
|
|
|
StringBuilder builder;
|
|
|
|
for (int i = 0; i < line_count(); ++i) {
|
2019-07-24 10:12:23 +03:00
|
|
|
auto& line = m_lines[i];
|
2019-03-15 19:37:13 +03:00
|
|
|
builder.append(line.characters(), line.length());
|
|
|
|
if (i != line_count() - 1)
|
|
|
|
builder.append('\n');
|
|
|
|
}
|
|
|
|
return builder.to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::clear()
|
|
|
|
{
|
|
|
|
m_lines.clear();
|
2019-08-21 20:32:39 +03:00
|
|
|
m_lines.append(make<Line>(*this));
|
2019-03-15 19:37:13 +03:00
|
|
|
m_selection.clear();
|
2019-04-12 03:52:34 +03:00
|
|
|
did_update_selection();
|
2019-03-15 19:37:13 +03:00
|
|
|
set_cursor(0, 0);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2019-03-08 03:59:59 +03:00
|
|
|
String GTextEditor::selected_text() const
|
|
|
|
{
|
|
|
|
if (!has_selection())
|
2019-06-07 12:46:02 +03:00
|
|
|
return {};
|
2019-03-08 03:59:59 +03:00
|
|
|
|
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-07-24 10:12:23 +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-07-24 10:12:23 +03:00
|
|
|
auto& line = m_lines[selection.start().line()];
|
2019-03-08 20:28:24 +03:00
|
|
|
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);
|
2019-07-24 10:12:23 +03:00
|
|
|
auto& first_line = m_lines[selection.start().line()];
|
|
|
|
auto& second_line = m_lines[selection.end().line()];
|
2019-03-08 20:28:24 +03:00
|
|
|
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())
|
2019-08-21 20:32:39 +03:00
|
|
|
m_lines.append(make<Line>(*this));
|
2019-03-08 16:08:15 +03:00
|
|
|
|
2019-03-08 20:28:24 +03:00
|
|
|
m_selection.clear();
|
2019-04-12 03:52:34 +03:00
|
|
|
did_update_selection();
|
2019-04-25 02:28:17 +03:00
|
|
|
did_change();
|
2019-03-08 20:28:24 +03:00
|
|
|
set_cursor(selection.start());
|
2019-03-08 16:08:15 +03:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2019-06-02 15:58:02 +03:00
|
|
|
void GTextEditor::insert_at_cursor_or_replace_selection(const StringView& text)
|
2019-03-08 16:08:15 +03:00
|
|
|
{
|
2019-05-08 06:00:28 +03:00
|
|
|
ASSERT(!is_readonly());
|
2019-03-08 16:08:15 +03:00
|
|
|
if (has_selection())
|
|
|
|
delete_selection();
|
|
|
|
insert_at_cursor(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::cut()
|
|
|
|
{
|
2019-05-08 06:00:28 +03:00
|
|
|
if (is_readonly())
|
|
|
|
return;
|
2019-03-08 16:08:15 +03:00
|
|
|
auto selected_text = this->selected_text();
|
|
|
|
printf("Cut: \"%s\"\n", selected_text.characters());
|
2019-04-05 06:10:18 +03:00
|
|
|
GClipboard::the().set_data(selected_text);
|
2019-03-08 16:08:15 +03:00
|
|
|
delete_selection();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::copy()
|
|
|
|
{
|
|
|
|
auto selected_text = this->selected_text();
|
|
|
|
printf("Copy: \"%s\"\n", selected_text.characters());
|
2019-04-05 06:10:18 +03:00
|
|
|
GClipboard::the().set_data(selected_text);
|
2019-03-08 16:08:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::paste()
|
|
|
|
{
|
2019-05-08 06:00:28 +03:00
|
|
|
if (is_readonly())
|
|
|
|
return;
|
2019-04-05 06:10:18 +03:00
|
|
|
auto paste_text = GClipboard::the().data();
|
2019-03-08 16:08:15 +03:00
|
|
|
printf("Paste: \"%s\"\n", paste_text.characters());
|
|
|
|
insert_at_cursor_or_replace_selection(paste_text);
|
|
|
|
}
|
2019-04-01 00:52:02 +03:00
|
|
|
|
2019-04-10 17:56:55 +03:00
|
|
|
void GTextEditor::enter_event(CEvent&)
|
2019-04-01 00:52:02 +03:00
|
|
|
{
|
|
|
|
ASSERT(window());
|
|
|
|
window()->set_override_cursor(GStandardCursor::IBeam);
|
|
|
|
}
|
|
|
|
|
2019-04-10 17:56:55 +03:00
|
|
|
void GTextEditor::leave_event(CEvent&)
|
2019-04-01 00:52:02 +03:00
|
|
|
{
|
|
|
|
ASSERT(window());
|
|
|
|
window()->set_override_cursor(GStandardCursor::None);
|
|
|
|
}
|
2019-04-09 17:20:36 +03:00
|
|
|
|
|
|
|
void GTextEditor::did_change()
|
|
|
|
{
|
2019-06-23 09:23:37 +03:00
|
|
|
ASSERT(!is_readonly());
|
2019-04-09 17:20:36 +03:00
|
|
|
update_content_size();
|
2019-08-25 09:43:01 +03:00
|
|
|
recompute_all_visual_lines();
|
2019-04-09 17:20:36 +03:00
|
|
|
if (!m_have_pending_change_notification) {
|
|
|
|
m_have_pending_change_notification = true;
|
2019-06-07 12:46:02 +03:00
|
|
|
deferred_invoke([this](auto&) {
|
2019-04-09 17:20:36 +03:00
|
|
|
if (on_change)
|
|
|
|
on_change();
|
|
|
|
m_have_pending_change_notification = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-04-12 03:52:34 +03:00
|
|
|
|
2019-05-08 06:00:28 +03:00
|
|
|
void GTextEditor::set_readonly(bool readonly)
|
|
|
|
{
|
|
|
|
if (m_readonly == readonly)
|
|
|
|
return;
|
|
|
|
m_readonly = readonly;
|
|
|
|
m_cut_action->set_enabled(!is_readonly() && has_selection());
|
|
|
|
m_delete_action->set_enabled(!is_readonly());
|
|
|
|
m_paste_action->set_enabled(!is_readonly());
|
|
|
|
}
|
|
|
|
|
2019-04-12 03:52:34 +03:00
|
|
|
void GTextEditor::did_update_selection()
|
|
|
|
{
|
2019-05-08 06:00:28 +03:00
|
|
|
m_cut_action->set_enabled(!is_readonly() && has_selection());
|
2019-04-18 13:25:00 +03:00
|
|
|
m_copy_action->set_enabled(has_selection());
|
2019-04-12 03:52:34 +03:00
|
|
|
if (on_selection_change)
|
|
|
|
on_selection_change();
|
2019-08-25 11:23:11 +03:00
|
|
|
if (is_line_wrapping_enabled()) {
|
|
|
|
// FIXME: Try to repaint less.
|
|
|
|
update();
|
|
|
|
}
|
2019-04-12 03:52:34 +03:00
|
|
|
}
|
2019-04-18 13:25:00 +03:00
|
|
|
|
|
|
|
void GTextEditor::context_menu_event(GContextMenuEvent& event)
|
|
|
|
{
|
|
|
|
if (!m_context_menu) {
|
2019-09-13 23:14:07 +03:00
|
|
|
m_context_menu = make<GMenu>();
|
2019-04-18 13:25:00 +03:00
|
|
|
m_context_menu->add_action(undo_action());
|
|
|
|
m_context_menu->add_action(redo_action());
|
|
|
|
m_context_menu->add_separator();
|
|
|
|
m_context_menu->add_action(cut_action());
|
|
|
|
m_context_menu->add_action(copy_action());
|
|
|
|
m_context_menu->add_action(paste_action());
|
|
|
|
m_context_menu->add_action(delete_action());
|
2019-08-25 22:33:08 +03:00
|
|
|
if (!m_custom_context_menu_actions.is_empty()) {
|
|
|
|
m_context_menu->add_separator();
|
|
|
|
for (auto& action : m_custom_context_menu_actions) {
|
|
|
|
m_context_menu->add_action(action);
|
|
|
|
}
|
|
|
|
}
|
2019-04-18 13:25:00 +03:00
|
|
|
}
|
|
|
|
m_context_menu->popup(event.screen_position());
|
|
|
|
}
|
2019-04-24 23:24:16 +03:00
|
|
|
|
|
|
|
void GTextEditor::set_text_alignment(TextAlignment alignment)
|
|
|
|
{
|
|
|
|
if (m_text_alignment == alignment)
|
|
|
|
return;
|
|
|
|
m_text_alignment = alignment;
|
|
|
|
update();
|
|
|
|
}
|
2019-04-25 00:42:49 +03:00
|
|
|
|
|
|
|
void GTextEditor::resize_event(GResizeEvent& event)
|
|
|
|
{
|
|
|
|
GScrollableWidget::resize_event(event);
|
|
|
|
update_content_size();
|
2019-08-25 09:43:01 +03:00
|
|
|
recompute_all_visual_lines();
|
2019-04-25 00:42:49 +03:00
|
|
|
}
|
2019-08-21 22:23:17 +03:00
|
|
|
|
|
|
|
GTextPosition GTextEditor::next_position_after(const GTextPosition& position, ShouldWrapAtEndOfDocument should_wrap)
|
|
|
|
{
|
|
|
|
auto& line = m_lines[position.line()];
|
|
|
|
if (position.column() == line.length()) {
|
|
|
|
if (position.line() == line_count() - 1) {
|
|
|
|
if (should_wrap == ShouldWrapAtEndOfDocument::Yes)
|
|
|
|
return { 0, 0 };
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return { position.line() + 1, 0 };
|
|
|
|
}
|
|
|
|
return { position.line(), position.column() + 1 };
|
|
|
|
}
|
|
|
|
|
2019-08-24 21:09:35 +03:00
|
|
|
GTextPosition GTextEditor::prev_position_before(const GTextPosition& position, ShouldWrapAtStartOfDocument should_wrap)
|
|
|
|
{
|
2019-08-25 08:14:02 +03:00
|
|
|
if (position.column() == 0) {
|
2019-08-24 21:09:35 +03:00
|
|
|
if (position.line() == 0) {
|
|
|
|
if (should_wrap == ShouldWrapAtStartOfDocument::Yes) {
|
2019-08-25 08:14:02 +03:00
|
|
|
auto& last_line = m_lines[line_count() - 1];
|
2019-08-24 21:09:35 +03:00
|
|
|
return { line_count() - 1, last_line.length() };
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
auto& prev_line = m_lines[position.line() - 1];
|
|
|
|
return { position.line() - 1, prev_line.length() };
|
|
|
|
}
|
|
|
|
return { position.line(), position.column() - 1 };
|
|
|
|
}
|
|
|
|
|
|
|
|
GTextRange GTextEditor::find_next(const StringView& needle, const GTextPosition& start)
|
2019-08-21 22:23:17 +03:00
|
|
|
{
|
|
|
|
if (needle.is_empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
GTextPosition position = start.is_valid() ? start : GTextPosition(0, 0);
|
|
|
|
GTextPosition original_position = position;
|
|
|
|
|
|
|
|
GTextPosition start_of_potential_match;
|
|
|
|
int needle_index = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
auto ch = character_at(position);
|
|
|
|
if (ch == needle[needle_index]) {
|
|
|
|
if (needle_index == 0)
|
|
|
|
start_of_potential_match = position;
|
|
|
|
++needle_index;
|
|
|
|
if (needle_index >= needle.length())
|
|
|
|
return { start_of_potential_match, next_position_after(position) };
|
|
|
|
} else {
|
2019-08-23 14:54:25 +03:00
|
|
|
if (needle_index > 0)
|
|
|
|
position = start_of_potential_match;
|
2019-08-21 22:23:17 +03:00
|
|
|
needle_index = 0;
|
|
|
|
}
|
|
|
|
position = next_position_after(position);
|
2019-08-25 08:14:02 +03:00
|
|
|
} while (position.is_valid() && position != original_position);
|
2019-08-21 22:23:17 +03:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-08-24 21:09:35 +03:00
|
|
|
GTextRange GTextEditor::find_prev(const StringView& needle, const GTextPosition& start)
|
|
|
|
{
|
|
|
|
if (needle.is_empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
GTextPosition position = start.is_valid() ? start : GTextPosition(0, 0);
|
2019-08-26 03:23:35 +03:00
|
|
|
position = prev_position_before(position);
|
2019-08-24 21:09:35 +03:00
|
|
|
GTextPosition original_position = position;
|
|
|
|
|
|
|
|
GTextPosition end_of_potential_match;
|
|
|
|
int needle_index = needle.length() - 1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
auto ch = character_at(position);
|
|
|
|
if (ch == needle[needle_index]) {
|
|
|
|
if (needle_index == needle.length() - 1)
|
|
|
|
end_of_potential_match = position;
|
|
|
|
--needle_index;
|
|
|
|
if (needle_index < 0)
|
|
|
|
return { position, next_position_after(end_of_potential_match) };
|
|
|
|
} else {
|
|
|
|
if (needle_index < needle.length() - 1)
|
|
|
|
position = end_of_potential_match;
|
|
|
|
needle_index = needle.length() - 1;
|
|
|
|
}
|
|
|
|
position = prev_position_before(position);
|
2019-08-25 08:14:02 +03:00
|
|
|
} while (position.is_valid() && position != original_position);
|
2019-08-24 21:09:35 +03:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-08-21 22:23:17 +03:00
|
|
|
void GTextEditor::set_selection(const GTextRange& selection)
|
|
|
|
{
|
|
|
|
if (m_selection == selection)
|
|
|
|
return;
|
|
|
|
m_selection = selection;
|
|
|
|
set_cursor(m_selection.end());
|
|
|
|
scroll_position_into_view(normalized_selection().start());
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
char GTextEditor::character_at(const GTextPosition& position) const
|
|
|
|
{
|
|
|
|
ASSERT(position.line() < line_count());
|
|
|
|
auto& line = m_lines[position.line()];
|
|
|
|
if (position.column() == line.length())
|
|
|
|
return '\n';
|
|
|
|
return line.characters()[position.column()];
|
|
|
|
}
|
2019-08-25 09:43:01 +03:00
|
|
|
|
|
|
|
void GTextEditor::recompute_all_visual_lines()
|
|
|
|
{
|
|
|
|
int y_offset = 0;
|
|
|
|
for (auto& line : m_lines) {
|
|
|
|
line.recompute_visual_lines();
|
|
|
|
line.m_visual_rect.set_y(y_offset);
|
|
|
|
y_offset += line.m_visual_rect.height();
|
|
|
|
}
|
2019-08-29 07:26:24 +03:00
|
|
|
|
|
|
|
update_content_size();
|
2019-08-25 09:43:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void GTextEditor::Line::recompute_visual_lines()
|
|
|
|
{
|
|
|
|
m_visual_line_breaks.clear_with_capacity();
|
|
|
|
|
|
|
|
int available_width = m_editor.visible_text_rect_in_inner_coordinates().width();
|
|
|
|
|
|
|
|
if (m_editor.is_line_wrapping_enabled()) {
|
|
|
|
int line_width_so_far = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < length(); ++i) {
|
|
|
|
auto ch = characters()[i];
|
|
|
|
auto glyph_width = m_editor.font().glyph_width(ch);
|
|
|
|
if ((line_width_so_far + glyph_width) > available_width) {
|
|
|
|
m_visual_line_breaks.append(i);
|
2019-09-01 21:04:25 +03:00
|
|
|
line_width_so_far = glyph_width;
|
2019-08-25 09:43:01 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
line_width_so_far += glyph_width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_visual_line_breaks.append(length());
|
|
|
|
|
|
|
|
if (m_editor.is_line_wrapping_enabled())
|
2019-08-25 09:46:48 +03:00
|
|
|
m_visual_rect = { m_editor.m_horizontal_content_padding, 0, available_width, m_visual_line_breaks.size() * m_editor.line_height() };
|
2019-08-25 09:43:01 +03:00
|
|
|
else
|
2019-08-25 09:46:48 +03:00
|
|
|
m_visual_rect = { m_editor.m_horizontal_content_padding, 0, m_editor.font().width(view()), m_editor.line_height() };
|
2019-08-25 09:43:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
void GTextEditor::Line::for_each_visual_line(Callback callback) const
|
|
|
|
{
|
2019-09-16 21:57:32 +03:00
|
|
|
auto editor_visible_text_rect = m_editor.visible_text_rect_in_inner_coordinates();
|
2019-08-25 09:43:01 +03:00
|
|
|
int start_of_line = 0;
|
|
|
|
int line_index = 0;
|
|
|
|
for (auto visual_line_break : m_visual_line_breaks) {
|
|
|
|
auto visual_line_view = StringView(characters() + start_of_line, visual_line_break - start_of_line);
|
|
|
|
Rect visual_line_rect {
|
|
|
|
m_visual_rect.x(),
|
|
|
|
m_visual_rect.y() + (line_index * m_editor.line_height()),
|
2019-09-01 18:30:23 +03:00
|
|
|
m_editor.font().width(visual_line_view),
|
2019-08-25 09:43:01 +03:00
|
|
|
m_editor.line_height()
|
|
|
|
};
|
2019-09-16 21:57:32 +03:00
|
|
|
if (is_right_text_alignment(m_editor.text_alignment()))
|
|
|
|
visual_line_rect.set_right_without_resize(editor_visible_text_rect.right());
|
|
|
|
if (!m_editor.is_multi_line())
|
|
|
|
visual_line_rect.center_vertically_within(editor_visible_text_rect);
|
2019-08-25 09:43:01 +03:00
|
|
|
if (callback(visual_line_rect, visual_line_view, start_of_line) == IterationDecision::Break)
|
|
|
|
break;
|
|
|
|
start_of_line = visual_line_break;
|
|
|
|
++line_index;
|
|
|
|
}
|
|
|
|
}
|
2019-08-25 13:23:14 +03:00
|
|
|
|
|
|
|
void GTextEditor::set_line_wrapping_enabled(bool enabled)
|
|
|
|
{
|
|
|
|
if (m_line_wrapping_enabled == enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_line_wrapping_enabled = enabled;
|
2019-09-01 21:34:50 +03:00
|
|
|
horizontal_scrollbar().set_visible(!m_line_wrapping_enabled);
|
2019-08-25 13:23:14 +03:00
|
|
|
update_content_size();
|
|
|
|
recompute_all_visual_lines();
|
|
|
|
update();
|
|
|
|
}
|
2019-08-25 15:04:46 +03:00
|
|
|
|
|
|
|
int GTextEditor::Line::visual_line_containing(int column) const
|
|
|
|
{
|
|
|
|
int visual_line_index = 0;
|
|
|
|
for_each_visual_line([&](const Rect&, const StringView& view, int start_of_visual_line) {
|
|
|
|
if (column >= start_of_visual_line && ((column - start_of_visual_line) < view.length()))
|
|
|
|
return IterationDecision::Break;
|
|
|
|
++visual_line_index;
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
return visual_line_index;
|
|
|
|
}
|
2019-08-25 22:33:08 +03:00
|
|
|
|
|
|
|
void GTextEditor::add_custom_context_menu_action(GAction& action)
|
|
|
|
{
|
|
|
|
m_custom_context_menu_actions.append(action);
|
|
|
|
}
|
2019-09-01 13:26:35 +03:00
|
|
|
|
|
|
|
void GTextEditor::did_change_font()
|
|
|
|
{
|
|
|
|
vertical_scrollbar().set_step(line_height());
|
|
|
|
GWidget::did_change_font();
|
|
|
|
}
|