mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 15:28:11 +00:00
LibGUI: Make GTextEditor inherit from GScrollableWidget.
This is quite nice, now we can share a ton of logic. :^)
This commit is contained in:
parent
6fbabac460
commit
3854e752cb
4 changed files with 94 additions and 127 deletions
|
@ -34,19 +34,19 @@ void GScrollableWidget::resize_event(GResizeEvent& event)
|
||||||
|
|
||||||
m_corner_widget->set_visible(m_vertical_scrollbar->is_visible() && m_horizontal_scrollbar->is_visible());
|
m_corner_widget->set_visible(m_vertical_scrollbar->is_visible() && m_horizontal_scrollbar->is_visible());
|
||||||
if (m_corner_widget->is_visible()) {
|
if (m_corner_widget->is_visible()) {
|
||||||
Rect corner_rect { m_horizontal_scrollbar->rect().right() + 1, m_vertical_scrollbar->rect().bottom() + 1, m_horizontal_scrollbar->height(), m_vertical_scrollbar->width() };
|
Rect corner_rect { m_horizontal_scrollbar->rect().right() + 1, m_vertical_scrollbar->rect().bottom() + 1, height_occupied_by_horizontal_scrollbar(), width_occupied_by_vertical_scrollbar() };
|
||||||
m_corner_widget->set_relative_rect(corner_rect);
|
m_corner_widget->set_relative_rect(corner_rect);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GScrollableWidget::update_scrollbar_ranges()
|
void GScrollableWidget::update_scrollbar_ranges()
|
||||||
{
|
{
|
||||||
int available_height = height() - m_size_occupied_by_fixed_elements.height() - m_horizontal_scrollbar->height();
|
int available_height = height() - m_size_occupied_by_fixed_elements.height() - height_occupied_by_horizontal_scrollbar();
|
||||||
int excess_height = max(0, m_content_size.height() - available_height);
|
int excess_height = max(0, (m_content_size.height() + m_padding.height() * 2) - available_height);
|
||||||
m_vertical_scrollbar->set_range(0, excess_height);
|
m_vertical_scrollbar->set_range(0, excess_height);
|
||||||
|
|
||||||
int available_width = width() - m_size_occupied_by_fixed_elements.width() - m_vertical_scrollbar->width();
|
int available_width = width() - m_size_occupied_by_fixed_elements.width() - width_occupied_by_vertical_scrollbar();
|
||||||
int excess_width = max(0, m_content_size.width() - available_width);
|
int excess_width = max(0, (m_content_size.width() + m_padding.height() * 2) - available_width);
|
||||||
m_horizontal_scrollbar->set_range(0, excess_width);
|
m_horizontal_scrollbar->set_range(0, excess_width);
|
||||||
|
|
||||||
m_vertical_scrollbar->set_big_step(visible_content_rect().height() - m_vertical_scrollbar->step());
|
m_vertical_scrollbar->set_big_step(visible_content_rect().height() - m_vertical_scrollbar->step());
|
||||||
|
@ -68,31 +68,67 @@ void GScrollableWidget::set_size_occupied_by_fixed_elements(const Size& size)
|
||||||
update_scrollbar_ranges();
|
update_scrollbar_ranges();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GScrollableWidget::set_padding(const Size& size)
|
||||||
|
{
|
||||||
|
if (m_padding == size)
|
||||||
|
return;
|
||||||
|
m_padding = size;
|
||||||
|
update_scrollbar_ranges();
|
||||||
|
}
|
||||||
|
|
||||||
|
int GScrollableWidget::height_occupied_by_horizontal_scrollbar() const
|
||||||
|
{
|
||||||
|
return m_horizontal_scrollbar->is_visible() ? m_horizontal_scrollbar->height() : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int GScrollableWidget::width_occupied_by_vertical_scrollbar() const
|
||||||
|
{
|
||||||
|
return m_vertical_scrollbar->is_visible() ? m_vertical_scrollbar->width() : 0;
|
||||||
|
}
|
||||||
|
|
||||||
Rect GScrollableWidget::visible_content_rect() const
|
Rect GScrollableWidget::visible_content_rect() const
|
||||||
{
|
{
|
||||||
return {
|
return {
|
||||||
m_horizontal_scrollbar->value(),
|
m_horizontal_scrollbar->value(),
|
||||||
m_vertical_scrollbar->value(),
|
m_vertical_scrollbar->value(),
|
||||||
width() - m_vertical_scrollbar->width(),
|
width() - width_occupied_by_vertical_scrollbar() - padding().width() * 2 - m_size_occupied_by_fixed_elements.width(),
|
||||||
height() - m_size_occupied_by_fixed_elements.height() - m_horizontal_scrollbar->height()
|
height() - height_occupied_by_horizontal_scrollbar() - padding().height() * 2 - m_size_occupied_by_fixed_elements.height()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
void GScrollableWidget::scroll_into_view(const Rect& rect, Orientation orientation)
|
void GScrollableWidget::scroll_into_view(const Rect& rect, Orientation orientation)
|
||||||
|
{
|
||||||
|
if (orientation == Orientation::Vertical)
|
||||||
|
return scroll_into_view(rect, false, true);
|
||||||
|
return scroll_into_view(rect, true, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GScrollableWidget::scroll_into_view(const Rect& rect, bool scroll_horizontally, bool scroll_vertically)
|
||||||
{
|
{
|
||||||
auto visible_content_rect = this->visible_content_rect();
|
auto visible_content_rect = this->visible_content_rect();
|
||||||
if (visible_content_rect.contains(rect))
|
if (visible_content_rect.contains(rect))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (orientation == Orientation::Vertical) {
|
if (scroll_vertically) {
|
||||||
if (rect.top() < visible_content_rect.top())
|
if (rect.top() < visible_content_rect.top())
|
||||||
m_vertical_scrollbar->set_value(rect.top());
|
m_vertical_scrollbar->set_value(rect.top());
|
||||||
else if (rect.bottom() > visible_content_rect.bottom())
|
else if (rect.bottom() > visible_content_rect.bottom())
|
||||||
m_vertical_scrollbar->set_value(rect.bottom() - visible_content_rect.height());
|
m_vertical_scrollbar->set_value(rect.bottom() - visible_content_rect.height());
|
||||||
} else {
|
}
|
||||||
|
if (scroll_horizontally) {
|
||||||
if (rect.left() < visible_content_rect.left())
|
if (rect.left() < visible_content_rect.left())
|
||||||
m_horizontal_scrollbar->set_value(rect.left());
|
m_horizontal_scrollbar->set_value(rect.left());
|
||||||
else if (rect.right() > visible_content_rect.right())
|
else if (rect.right() > visible_content_rect.right())
|
||||||
m_horizontal_scrollbar->set_value(rect.right() - visible_content_rect.width());
|
m_horizontal_scrollbar->set_value(rect.right() - visible_content_rect.width());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GScrollableWidget::set_scrollbars_enabled(bool scrollbars_enabled)
|
||||||
|
{
|
||||||
|
if (m_scrollbars_enabled == scrollbars_enabled)
|
||||||
|
return;
|
||||||
|
m_scrollbars_enabled = scrollbars_enabled;
|
||||||
|
m_vertical_scrollbar->set_visible(m_scrollbars_enabled);
|
||||||
|
m_horizontal_scrollbar->set_visible(m_scrollbars_enabled);
|
||||||
|
m_corner_widget->set_visible(m_scrollbars_enabled);
|
||||||
|
}
|
||||||
|
|
|
@ -11,21 +11,35 @@ public:
|
||||||
virtual const char* class_name() const override { return "GScrollableWidget"; }
|
virtual const char* class_name() const override { return "GScrollableWidget"; }
|
||||||
|
|
||||||
Size content_size() const { return m_content_size; }
|
Size content_size() const { return m_content_size; }
|
||||||
|
int content_width() const { return m_content_size.width(); }
|
||||||
|
int content_height() const { return m_content_size.height(); }
|
||||||
|
|
||||||
|
Size padding() const { return m_padding; }
|
||||||
|
|
||||||
Rect visible_content_rect() const;
|
Rect visible_content_rect() const;
|
||||||
|
|
||||||
void scroll_into_view(const Rect&, Orientation);
|
void scroll_into_view(const Rect&, Orientation);
|
||||||
|
void scroll_into_view(const Rect&, bool scroll_horizontally, bool scroll_vertically);
|
||||||
|
|
||||||
GScrollBar& vertical_scrollbar() { return *m_vertical_scrollbar; }
|
GScrollBar& vertical_scrollbar() { return *m_vertical_scrollbar; }
|
||||||
const GScrollBar& vertical_scrollbar() const { return *m_vertical_scrollbar; }
|
const GScrollBar& vertical_scrollbar() const { return *m_vertical_scrollbar; }
|
||||||
GScrollBar& horizontal_scrollbar() { return *m_horizontal_scrollbar; }
|
GScrollBar& horizontal_scrollbar() { return *m_horizontal_scrollbar; }
|
||||||
const GScrollBar& horizontal_scrollbar() const { return *m_horizontal_scrollbar; }
|
const GScrollBar& horizontal_scrollbar() const { return *m_horizontal_scrollbar; }
|
||||||
|
GWidget& corner_widget() { return *m_corner_widget; }
|
||||||
|
const GWidget& corner_widget() const { return *m_corner_widget; }
|
||||||
|
|
||||||
|
void set_scrollbars_enabled(bool);
|
||||||
|
bool is_scrollbars_enabled() const { return m_scrollbars_enabled; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit GScrollableWidget(GWidget* parent);
|
explicit GScrollableWidget(GWidget* parent);
|
||||||
virtual void resize_event(GResizeEvent&) override;
|
virtual void resize_event(GResizeEvent&) override;
|
||||||
void set_content_size(const Size&);
|
void set_content_size(const Size&);
|
||||||
void set_size_occupied_by_fixed_elements(const Size&);
|
void set_size_occupied_by_fixed_elements(const Size&);
|
||||||
|
void set_padding(const Size&);
|
||||||
|
|
||||||
|
int width_occupied_by_vertical_scrollbar() const;
|
||||||
|
int height_occupied_by_horizontal_scrollbar() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void update_scrollbar_ranges();
|
void update_scrollbar_ranges();
|
||||||
|
@ -35,4 +49,6 @@ private:
|
||||||
GWidget* m_corner_widget { nullptr };
|
GWidget* m_corner_widget { nullptr };
|
||||||
Size m_content_size;
|
Size m_content_size;
|
||||||
Size m_size_occupied_by_fixed_elements;
|
Size m_size_occupied_by_fixed_elements;
|
||||||
|
Size m_padding;
|
||||||
|
bool m_scrollbars_enabled { true };
|
||||||
};
|
};
|
||||||
|
|
|
@ -10,32 +10,13 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
GTextEditor::GTextEditor(Type type, GWidget* parent)
|
GTextEditor::GTextEditor(Type type, GWidget* parent)
|
||||||
: GWidget(parent)
|
: GScrollableWidget(parent)
|
||||||
, m_type(type)
|
, m_type(type)
|
||||||
{
|
{
|
||||||
m_ruler_visible = m_type == MultiLine;
|
set_padding({ 3, 3 });
|
||||||
|
set_scrollbars_enabled(is_multi_line());
|
||||||
|
m_ruler_visible = is_multi_line();
|
||||||
set_font(GFontDatabase::the().get_by_name("Csilla Thin"));
|
set_font(GFontDatabase::the().get_by_name("Csilla Thin"));
|
||||||
|
|
||||||
m_vertical_scrollbar = new GScrollBar(Orientation::Vertical, this);
|
|
||||||
m_vertical_scrollbar->set_step(4);
|
|
||||||
m_vertical_scrollbar->set_visible(is_multi_line());
|
|
||||||
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->set_visible(is_multi_line());
|
|
||||||
m_horizontal_scrollbar->on_change = [this] (int) {
|
|
||||||
update();
|
|
||||||
};
|
|
||||||
|
|
||||||
m_corner_widget = new GWidget(this);
|
|
||||||
m_corner_widget->set_fill_with_background_color(true);
|
|
||||||
m_corner_widget->set_visible(is_multi_line());
|
|
||||||
|
|
||||||
m_lines.append(make<Line>());
|
m_lines.append(make<Line>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,52 +43,26 @@ void GTextEditor::set_text(const String& text)
|
||||||
add_line(i);
|
add_line(i);
|
||||||
}
|
}
|
||||||
add_line(i);
|
add_line(i);
|
||||||
|
update_content_size();
|
||||||
set_cursor(0, 0);
|
set_cursor(0, 0);
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
void GTextEditor::resize_event(GResizeEvent& event)
|
void GTextEditor::update_content_size()
|
||||||
{
|
{
|
||||||
update_scrollbar_ranges();
|
int content_width = 0;
|
||||||
if (is_multi_line()) {
|
|
||||||
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());
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void GTextEditor::update_scrollbar_ranges()
|
|
||||||
{
|
|
||||||
int available_height = height() - m_horizontal_scrollbar->height();
|
|
||||||
int excess_height = max(0, (content_height() + padding() * 2) - available_height);
|
|
||||||
m_vertical_scrollbar->set_range(0, excess_height);
|
|
||||||
|
|
||||||
int available_width = width() - m_vertical_scrollbar->width() - ruler_width();
|
|
||||||
int excess_width = max(0, (content_width() + padding() * 2) - available_width);
|
|
||||||
m_horizontal_scrollbar->set_range(0, excess_width);
|
|
||||||
|
|
||||||
m_vertical_scrollbar->set_big_step(visible_content_rect().height());
|
|
||||||
}
|
|
||||||
|
|
||||||
int GTextEditor::content_height() const
|
|
||||||
{
|
|
||||||
return line_count() * line_height();
|
|
||||||
}
|
|
||||||
|
|
||||||
int GTextEditor::content_width() const
|
|
||||||
{
|
|
||||||
// FIXME: Cache this somewhere.
|
|
||||||
int max_width = 0;
|
|
||||||
for (auto& line : m_lines)
|
for (auto& line : m_lines)
|
||||||
max_width = max(line->width(font()), max_width);
|
content_width = max(line->width(font()), content_width);
|
||||||
return max_width;
|
int content_height = line_count() * line_height();
|
||||||
|
set_content_size({ content_width, content_height });
|
||||||
|
set_size_occupied_by_fixed_elements({ ruler_width(), 0 });
|
||||||
}
|
}
|
||||||
|
|
||||||
GTextPosition GTextEditor::text_position_at(const Point& a_position) const
|
GTextPosition GTextEditor::text_position_at(const Point& a_position) const
|
||||||
{
|
{
|
||||||
auto position = a_position;
|
auto position = a_position;
|
||||||
position.move_by(m_horizontal_scrollbar->value(), m_vertical_scrollbar->value());
|
position.move_by(horizontal_scrollbar().value(), vertical_scrollbar().value());
|
||||||
position.move_by(-(padding() + ruler_width()), -padding());
|
position.move_by(-(padding().width() + ruler_width()), -padding().height());
|
||||||
int line_index = position.y() / line_height();
|
int line_index = position.y() / line_height();
|
||||||
int column_index = position.x() / glyph_width();
|
int column_index = position.x() / glyph_width();
|
||||||
line_index = max(0, min(line_index, line_count() - 1));
|
line_index = max(0, min(line_index, line_count() - 1));
|
||||||
|
@ -178,7 +133,7 @@ Rect GTextEditor::ruler_content_rect(int line_index) const
|
||||||
if (!m_ruler_visible)
|
if (!m_ruler_visible)
|
||||||
return { };
|
return { };
|
||||||
return {
|
return {
|
||||||
0 - ruler_width() - padding() + m_horizontal_scrollbar->value(),
|
0 - ruler_width() - padding().width() + horizontal_scrollbar().value(),
|
||||||
line_index * line_height(),
|
line_index * line_height(),
|
||||||
ruler_width(),
|
ruler_width(),
|
||||||
line_height()
|
line_height()
|
||||||
|
@ -188,12 +143,12 @@ Rect GTextEditor::ruler_content_rect(int line_index) const
|
||||||
void GTextEditor::paint_event(GPaintEvent& event)
|
void GTextEditor::paint_event(GPaintEvent& event)
|
||||||
{
|
{
|
||||||
Painter painter(*this);
|
Painter painter(*this);
|
||||||
Rect item_area_rect { 0, 0, width() - m_vertical_scrollbar->width(), height() - m_horizontal_scrollbar->height() };
|
Rect item_area_rect { 0, 0, width() - width_occupied_by_vertical_scrollbar(), height() - height_occupied_by_horizontal_scrollbar() };
|
||||||
painter.set_clip_rect(item_area_rect);
|
painter.set_clip_rect(item_area_rect);
|
||||||
painter.set_clip_rect(event.rect());
|
painter.set_clip_rect(event.rect());
|
||||||
painter.fill_rect(event.rect(), Color::White);
|
painter.fill_rect(event.rect(), Color::White);
|
||||||
|
|
||||||
Rect ruler_rect { 0, 0, ruler_width(), height() - m_horizontal_scrollbar->height()};
|
Rect ruler_rect { 0, 0, ruler_width(), height() - height_occupied_by_horizontal_scrollbar()};
|
||||||
|
|
||||||
if (m_ruler_visible) {
|
if (m_ruler_visible) {
|
||||||
painter.fill_rect(ruler_rect, Color::LightGray);
|
painter.fill_rect(ruler_rect, Color::LightGray);
|
||||||
|
@ -202,8 +157,8 @@ void GTextEditor::paint_event(GPaintEvent& event)
|
||||||
|
|
||||||
painter.save();
|
painter.save();
|
||||||
|
|
||||||
painter.translate(-m_horizontal_scrollbar->value(), -m_vertical_scrollbar->value());
|
painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
|
||||||
painter.translate(padding() + ruler_width(), padding());
|
painter.translate(padding().width() + ruler_width(), padding().height());
|
||||||
int exposed_width = max(content_width(), width());
|
int exposed_width = max(content_width(), width());
|
||||||
|
|
||||||
int first_visible_line = text_position_at(event.rect().top_left()).line();
|
int first_visible_line = text_position_at(event.rect().top_left()).line();
|
||||||
|
@ -226,7 +181,7 @@ void GTextEditor::paint_event(GPaintEvent& event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
painter.set_clip_rect({ ruler_rect.right() + 1, 0, width() - m_vertical_scrollbar->width() - ruler_width(), height() - m_horizontal_scrollbar->height() });
|
painter.set_clip_rect({ ruler_rect.right() + 1, 0, width() - width_occupied_by_vertical_scrollbar() - ruler_width(), height() - height_occupied_by_horizontal_scrollbar() });
|
||||||
|
|
||||||
for (int i = first_visible_line; i <= last_visible_line; ++i) {
|
for (int i = first_visible_line; i <= last_visible_line; ++i) {
|
||||||
auto& line = *m_lines[i];
|
auto& line = *m_lines[i];
|
||||||
|
@ -395,7 +350,7 @@ void GTextEditor::keydown_event(GKeyEvent& event)
|
||||||
if (m_cursor.column() > 0) {
|
if (m_cursor.column() > 0) {
|
||||||
// Backspace within line
|
// Backspace within line
|
||||||
current_line().remove(m_cursor.column() - 1);
|
current_line().remove(m_cursor.column() - 1);
|
||||||
update_scrollbar_ranges();
|
update_content_size();
|
||||||
set_cursor(m_cursor.line(), m_cursor.column() - 1);
|
set_cursor(m_cursor.line(), m_cursor.column() - 1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -405,7 +360,7 @@ void GTextEditor::keydown_event(GKeyEvent& event)
|
||||||
int previous_length = previous_line.length();
|
int previous_length = previous_line.length();
|
||||||
previous_line.append(current_line().characters(), current_line().length());
|
previous_line.append(current_line().characters(), current_line().length());
|
||||||
m_lines.remove(m_cursor.line());
|
m_lines.remove(m_cursor.line());
|
||||||
update_scrollbar_ranges();
|
update_content_size();
|
||||||
update();
|
update();
|
||||||
set_cursor(m_cursor.line() - 1, previous_length);
|
set_cursor(m_cursor.line() - 1, previous_length);
|
||||||
return;
|
return;
|
||||||
|
@ -421,7 +376,7 @@ void GTextEditor::keydown_event(GKeyEvent& event)
|
||||||
if (m_cursor.column() < current_line().length()) {
|
if (m_cursor.column() < current_line().length()) {
|
||||||
// Delete within line
|
// Delete within line
|
||||||
current_line().remove(m_cursor.column());
|
current_line().remove(m_cursor.column());
|
||||||
update_scrollbar_ranges();
|
update_content_size();
|
||||||
update_cursor();
|
update_cursor();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -431,7 +386,7 @@ void GTextEditor::keydown_event(GKeyEvent& event)
|
||||||
int previous_length = current_line().length();
|
int previous_length = current_line().length();
|
||||||
current_line().append(next_line.characters(), next_line.length());
|
current_line().append(next_line.characters(), next_line.length());
|
||||||
m_lines.remove(m_cursor.line() + 1);
|
m_lines.remove(m_cursor.line() + 1);
|
||||||
update_scrollbar_ranges();
|
update_content_size();
|
||||||
update();
|
update();
|
||||||
set_cursor(m_cursor.line(), previous_length);
|
set_cursor(m_cursor.line(), previous_length);
|
||||||
return;
|
return;
|
||||||
|
@ -465,7 +420,7 @@ void GTextEditor::insert_at_cursor(char ch)
|
||||||
}
|
}
|
||||||
if (at_tail || at_head) {
|
if (at_tail || at_head) {
|
||||||
m_lines.insert(m_cursor.line() + (at_tail ? 1 : 0), make<Line>());
|
m_lines.insert(m_cursor.line() + (at_tail ? 1 : 0), make<Line>());
|
||||||
update_scrollbar_ranges();
|
update_content_size();
|
||||||
update();
|
update();
|
||||||
set_cursor(m_cursor.line() + 1, 0);
|
set_cursor(m_cursor.line() + 1, 0);
|
||||||
return;
|
return;
|
||||||
|
@ -474,7 +429,7 @@ void GTextEditor::insert_at_cursor(char ch)
|
||||||
new_line->append(current_line().characters() + m_cursor.column(), current_line().length() - m_cursor.column());
|
new_line->append(current_line().characters() + m_cursor.column(), current_line().length() - m_cursor.column());
|
||||||
current_line().truncate(m_cursor.column());
|
current_line().truncate(m_cursor.column());
|
||||||
m_lines.insert(m_cursor.line() + 1, move(new_line));
|
m_lines.insert(m_cursor.line() + 1, move(new_line));
|
||||||
update_scrollbar_ranges();
|
update_content_size();
|
||||||
update();
|
update();
|
||||||
set_cursor(m_cursor.line() + 1, 0);
|
set_cursor(m_cursor.line() + 1, 0);
|
||||||
return;
|
return;
|
||||||
|
@ -485,27 +440,17 @@ void GTextEditor::insert_at_cursor(char ch)
|
||||||
for (int i = 0; i < spaces_to_insert; ++i) {
|
for (int i = 0; i < spaces_to_insert; ++i) {
|
||||||
current_line().insert(m_cursor.column(), ' ');
|
current_line().insert(m_cursor.column(), ' ');
|
||||||
}
|
}
|
||||||
update_scrollbar_ranges();
|
update_content_size();
|
||||||
set_cursor(m_cursor.line(), next_soft_tab_stop);
|
set_cursor(m_cursor.line(), next_soft_tab_stop);
|
||||||
update_cursor();
|
update_cursor();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
current_line().insert(m_cursor.column(), ch);
|
current_line().insert(m_cursor.column(), ch);
|
||||||
update_scrollbar_ranges();
|
update_content_size();
|
||||||
set_cursor(m_cursor.line(), m_cursor.column() + 1);
|
set_cursor(m_cursor.line(), m_cursor.column() + 1);
|
||||||
update_cursor();
|
update_cursor();
|
||||||
}
|
}
|
||||||
|
|
||||||
Rect GTextEditor::visible_content_rect() const
|
|
||||||
{
|
|
||||||
return {
|
|
||||||
m_horizontal_scrollbar->value(),
|
|
||||||
m_vertical_scrollbar->value(),
|
|
||||||
width() - m_vertical_scrollbar->width() - padding() * 2 - ruler_width(),
|
|
||||||
height() - m_horizontal_scrollbar->height() - padding() * 2
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
Rect GTextEditor::cursor_content_rect() const
|
Rect GTextEditor::cursor_content_rect() const
|
||||||
{
|
{
|
||||||
if (!m_cursor.is_valid())
|
if (!m_cursor.is_valid())
|
||||||
|
@ -517,39 +462,18 @@ Rect GTextEditor::cursor_content_rect() const
|
||||||
|
|
||||||
Rect GTextEditor::line_widget_rect(int line_index) const
|
Rect GTextEditor::line_widget_rect(int line_index) const
|
||||||
{
|
{
|
||||||
ASSERT(m_horizontal_scrollbar);
|
|
||||||
ASSERT(m_vertical_scrollbar);
|
|
||||||
auto rect = line_content_rect(line_index);
|
auto rect = line_content_rect(line_index);
|
||||||
rect.move_by(-(m_horizontal_scrollbar->value() - padding()), -(m_vertical_scrollbar->value() - padding()));
|
rect.move_by(-(horizontal_scrollbar().value() - padding().width()), -(vertical_scrollbar().value() - padding().height()));
|
||||||
rect.set_width(rect.width() + 1); // Add 1 pixel for when the cursor is on the end.
|
rect.set_width(rect.width() + 1); // Add 1 pixel for when the cursor is on the end.
|
||||||
rect.intersect(this->rect());
|
rect.intersect(this->rect());
|
||||||
// This feels rather hackish, but extend the rect to the edge of the content view:
|
// 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);
|
rect.set_right(vertical_scrollbar().relative_rect().left() - 1);
|
||||||
return rect;
|
return rect;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GTextEditor::scroll_cursor_into_view()
|
void GTextEditor::scroll_cursor_into_view()
|
||||||
{
|
{
|
||||||
auto visible_content_rect = this->visible_content_rect();
|
scroll_into_view(cursor_content_rect(), true, true);
|
||||||
auto rect = cursor_content_rect();
|
|
||||||
|
|
||||||
if (visible_content_rect.is_empty())
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (visible_content_rect.contains(rect))
|
|
||||||
return;
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Rect GTextEditor::line_content_rect(int line_index) const
|
Rect GTextEditor::line_content_rect(int line_index) const
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <LibGUI/GWidget.h>
|
#include <LibGUI/GScrollableWidget.h>
|
||||||
#include <AK/Function.h>
|
#include <AK/Function.h>
|
||||||
#include <AK/HashMap.h>
|
#include <AK/HashMap.h>
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ private:
|
||||||
GTextPosition m_end;
|
GTextPosition m_end;
|
||||||
};
|
};
|
||||||
|
|
||||||
class GTextEditor : public GWidget {
|
class GTextEditor : public GScrollableWidget {
|
||||||
public:
|
public:
|
||||||
enum Type { MultiLine, SingleLine };
|
enum Type { MultiLine, SingleLine };
|
||||||
GTextEditor(Type, GWidget* parent);
|
GTextEditor(Type, GWidget* parent);
|
||||||
|
@ -74,14 +74,10 @@ public:
|
||||||
Function<void(GTextEditor&)> on_cursor_change;
|
Function<void(GTextEditor&)> on_cursor_change;
|
||||||
|
|
||||||
void set_text(const String&);
|
void set_text(const String&);
|
||||||
int content_width() const;
|
|
||||||
int content_height() const;
|
|
||||||
Rect visible_content_rect() const;
|
|
||||||
void scroll_cursor_into_view();
|
void scroll_cursor_into_view();
|
||||||
int line_count() const { return m_lines.size(); }
|
int line_count() const { return m_lines.size(); }
|
||||||
int line_spacing() const { return m_line_spacing; }
|
int line_spacing() const { return m_line_spacing; }
|
||||||
int line_height() const { return font().glyph_height() + m_line_spacing; }
|
int line_height() const { return font().glyph_height() + m_line_spacing; }
|
||||||
int padding() const { return 3; }
|
|
||||||
GTextPosition cursor() const { return m_cursor; }
|
GTextPosition cursor() const { return m_cursor; }
|
||||||
GTextRange normalized_selection() const { return m_selection.normalized(); }
|
GTextRange normalized_selection() const { return m_selection.normalized(); }
|
||||||
int glyph_width() const { return font().glyph_width('x'); }
|
int glyph_width() const { return font().glyph_width('x'); }
|
||||||
|
@ -104,7 +100,6 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void paint_event(GPaintEvent&) override;
|
virtual void paint_event(GPaintEvent&) override;
|
||||||
virtual void resize_event(GResizeEvent&) override;
|
|
||||||
virtual void mousedown_event(GMouseEvent&) override;
|
virtual void mousedown_event(GMouseEvent&) override;
|
||||||
virtual void mouseup_event(GMouseEvent&) override;
|
virtual void mouseup_event(GMouseEvent&) override;
|
||||||
virtual void mousemove_event(GMouseEvent&) override;
|
virtual void mousemove_event(GMouseEvent&) override;
|
||||||
|
@ -114,6 +109,7 @@ private:
|
||||||
virtual void timer_event(GTimerEvent&) override;
|
virtual void timer_event(GTimerEvent&) override;
|
||||||
virtual bool accepts_focus() const override { return true; }
|
virtual bool accepts_focus() const override { return true; }
|
||||||
void paint_ruler(Painter&);
|
void paint_ruler(Painter&);
|
||||||
|
void update_content_size();
|
||||||
|
|
||||||
class Line {
|
class Line {
|
||||||
friend class GTextEditor;
|
friend class GTextEditor;
|
||||||
|
@ -137,7 +133,6 @@ private:
|
||||||
Vector<char> m_text;
|
Vector<char> m_text;
|
||||||
};
|
};
|
||||||
|
|
||||||
void update_scrollbar_ranges();
|
|
||||||
Rect line_content_rect(int item_index) const;
|
Rect line_content_rect(int item_index) const;
|
||||||
Rect line_widget_rect(int line_index) const;
|
Rect line_widget_rect(int line_index) const;
|
||||||
Rect cursor_content_rect() const;
|
Rect cursor_content_rect() const;
|
||||||
|
@ -157,10 +152,6 @@ private:
|
||||||
|
|
||||||
Type m_type { MultiLine };
|
Type m_type { MultiLine };
|
||||||
|
|
||||||
GScrollBar* m_vertical_scrollbar { nullptr };
|
|
||||||
GScrollBar* m_horizontal_scrollbar { nullptr };
|
|
||||||
GWidget* m_corner_widget { nullptr };
|
|
||||||
|
|
||||||
Vector<OwnPtr<Line>> m_lines;
|
Vector<OwnPtr<Line>> m_lines;
|
||||||
GTextPosition m_cursor;
|
GTextPosition m_cursor;
|
||||||
bool m_cursor_state { true };
|
bool m_cursor_state { true };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue