123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- #include <LibGUI/GTextEditor.h>
- #include <LibGUI/GScrollBar.h>
- #include <LibGUI/GFontDatabase.h>
- #include <SharedGraphics/Painter.h>
- #include <Kernel/KeyCode.h>
- GTextEditor::GTextEditor(GWidget* parent)
- : GWidget(parent)
- {
- set_font(GFontDatabase::the().get_by_name("Csilla Thin"));
- set_fill_with_background_color(false);
- m_vertical_scrollbar = new GScrollBar(Orientation::Vertical, this);
- m_vertical_scrollbar->set_step(4);
- m_vertical_scrollbar->on_change = [this] (int) {
- update();
- };
- m_horizontal_scrollbar = new GScrollBar(Orientation::Horizontal, this);
- m_horizontal_scrollbar->set_step(4);
- m_horizontal_scrollbar->set_big_step(30);
- m_horizontal_scrollbar->on_change = [this] (int) {
- update();
- };
- }
- GTextEditor::~GTextEditor()
- {
- }
- void GTextEditor::set_text(const String& text)
- {
- m_lines.clear();
- int start_of_current_line = 0;
- auto add_line = [&] (int current_position) {
- int line_length = current_position - start_of_current_line;
- Line line;
- if (line_length)
- line.set_text(text.substring(start_of_current_line, current_position - start_of_current_line));
- 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);
- set_cursor(0, 0);
- update();
- }
- void GTextEditor::resize_event(GResizeEvent& event)
- {
- update_scrollbar_ranges();
- m_vertical_scrollbar->set_relative_rect(event.size().width() - m_vertical_scrollbar->preferred_size().width(), 0, m_vertical_scrollbar->preferred_size().width(), event.size().height() - m_horizontal_scrollbar->preferred_size().height());
- m_horizontal_scrollbar->set_relative_rect(0, event.size().height() - m_horizontal_scrollbar->preferred_size().height(), event.size().width() - m_vertical_scrollbar->preferred_size().width(), m_horizontal_scrollbar->preferred_size().height());
- }
- 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();
- 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)
- max_width = max(line.width(font()), max_width);
- return max_width;
- }
- GTextPosition GTextEditor::text_position_at(const Point& a_position) const
- {
- auto position = a_position;
- position.move_by(m_horizontal_scrollbar->value(), m_vertical_scrollbar->value());
- position.move_by(-padding(), -padding());
- int line_index = position.y() / line_height();
- int column_index = position.x() / glyph_width();
- line_index = min(line_index, line_count() - 1);
- column_index = min(column_index, m_lines[line_index].length());
- return { line_index, column_index };
- }
- void GTextEditor::mousedown_event(GMouseEvent& event)
- {
- set_cursor(text_position_at(event.position()));
- }
- void GTextEditor::paint_event(GPaintEvent& event)
- {
- Painter painter(*this);
- painter.set_clip_rect(event.rect());
- painter.fill_rect(event.rect(), Color::White);
- painter.translate(-m_horizontal_scrollbar->value(), -m_vertical_scrollbar->value());
- painter.translate(padding(), padding());
- int exposed_width = max(content_width(), width());
- int first_visible_line = text_position_at(event.rect().top_left()).line();
- int last_visible_line = text_position_at(event.rect().bottom_right()).line();
- for (int i = first_visible_line; i <= last_visible_line; ++i) {
- auto& line = m_lines[i];
- auto line_rect = line_content_rect(i);
- line_rect.set_width(exposed_width);
- if (i == m_cursor.line() && is_focused())
- painter.fill_rect(line_rect, Color(230, 230, 230));
- painter.draw_text(line_rect, line.characters(), line.length(), TextAlignment::CenterLeft, Color::Black);
- }
- if (is_focused() && m_cursor_state)
- painter.fill_rect(cursor_content_rect(), Color::Red);
- painter.translate(-padding(), -padding());
- painter.translate(m_horizontal_scrollbar->value(), m_vertical_scrollbar->value());
- painter.fill_rect({ m_horizontal_scrollbar->relative_rect().top_right().translated(1, 0), { m_vertical_scrollbar->preferred_size().width(), m_horizontal_scrollbar->preferred_size().height() } }, Color::LightGray);
- if (is_focused()) {
- Rect item_area_rect { 0, 0, width() - m_vertical_scrollbar->width(), height() - m_horizontal_scrollbar->height() };
- painter.draw_rect(item_area_rect, Color::from_rgb(0x84351a));
- };
- }
- void GTextEditor::keydown_event(GKeyEvent& event)
- {
- if (!event.modifiers() && event.key() == KeyCode::Key_Up) {
- if (m_cursor.line() > 0) {
- int new_line = m_cursor.line() - 1;
- int new_column = min(m_cursor.column(), m_lines[new_line].length());
- set_cursor(new_line, new_column);
- }
- return;
- }
- if (!event.modifiers() && event.key() == KeyCode::Key_Down) {
- if (m_cursor.line() < (m_lines.size() - 1)) {
- int new_line = m_cursor.line() + 1;
- int new_column = min(m_cursor.column(), m_lines[new_line].length());
- set_cursor(new_line, new_column);
- }
- return;
- }
- if (!event.modifiers() && event.key() == KeyCode::Key_Left) {
- if (m_cursor.column() > 0) {
- int new_column = m_cursor.column() - 1;
- set_cursor(m_cursor.line(), new_column);
- }
- return;
- }
- if (!event.modifiers() && event.key() == KeyCode::Key_Right) {
- if (m_cursor.column() < current_line().length()) {
- int new_column = m_cursor.column() + 1;
- set_cursor(m_cursor.line(), new_column);
- }
- return;
- }
- if (!event.modifiers() && event.key() == KeyCode::Key_Home) {
- set_cursor(m_cursor.line(), 0);
- return;
- }
- if (!event.modifiers() && event.key() == KeyCode::Key_End) {
- set_cursor(m_cursor.line(), current_line().length());
- return;
- }
- if (event.ctrl() && event.key() == KeyCode::Key_Home) {
- set_cursor(0, 0);
- return;
- }
- if (event.ctrl() && event.key() == KeyCode::Key_End) {
- set_cursor(line_count() - 1, m_lines[line_count() - 1].length());
- return;
- }
- if (!event.text().is_empty())
- insert_at_cursor(event.text()[0]);
- return GWidget::keydown_event(event);
- }
- void GTextEditor::insert_at_cursor(char ch)
- {
- }
- Rect GTextEditor::visible_content_rect() const
- {
- return {
- m_horizontal_scrollbar->value(),
- m_vertical_scrollbar->value(),
- width() - m_vertical_scrollbar->width() - padding() * 2,
- height() - m_horizontal_scrollbar->height() - padding() * 2
- };
- }
- Rect GTextEditor::cursor_content_rect() const
- {
- if (!m_cursor.is_valid())
- return { };
- ASSERT(!m_lines.is_empty());
- ASSERT(m_cursor.column() <= (current_line().length() + 1));
- return { m_cursor.column() * glyph_width(), m_cursor.line() * line_height(), 1, line_height() };
- }
- Rect GTextEditor::line_widget_rect(int line_index) const
- {
- ASSERT(m_horizontal_scrollbar);
- ASSERT(m_vertical_scrollbar);
- auto rect = line_content_rect(line_index);
- rect.move_by(-(m_horizontal_scrollbar->value() - padding()), -(m_vertical_scrollbar->value() - padding()));
- rect.set_width(rect.width() + 1); // Add 1 pixel for when the cursor is on the end.
- rect.intersect(this->rect());
- return rect;
- }
- void GTextEditor::scroll_cursor_into_view()
- {
- auto visible_content_rect = this->visible_content_rect();
- 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
- {
- return {
- 0,
- line_index * line_height(),
- content_width(),
- line_height()
- };
- }
- void GTextEditor::update_cursor()
- {
- update(line_widget_rect(m_cursor.line()));
- }
- void GTextEditor::set_cursor(int line, int column)
- {
- set_cursor({ line, column });
- }
- void GTextEditor::set_cursor(const GTextPosition& position)
- {
- if (m_cursor == position)
- return;
- auto old_cursor_line_rect = line_widget_rect(m_cursor.line());
- m_cursor = position;
- m_cursor_state = true;
- scroll_cursor_into_view();
- update(old_cursor_line_rect);
- update_cursor();
- if (on_cursor_change)
- on_cursor_change(*this);
- }
- void GTextEditor::focusin_event(GEvent&)
- {
- update_cursor();
- start_timer(500);
- }
- void GTextEditor::focusout_event(GEvent&)
- {
- stop_timer();
- }
- void GTextEditor::timer_event(GTimerEvent&)
- {
- m_cursor_state = !m_cursor_state;
- if (is_focused())
- update_cursor();
- }
- GTextEditor::Line::Line()
- {
- m_text.append(0);
- }
- void GTextEditor::Line::set_text(const String& text)
- {
- if (text.length() == length() && !memcmp(text.characters(), characters(), length()))
- return;
- m_text.resize(text.length() + 1);
- memcpy(m_text.data(), text.characters(), text.length() + 1);
- }
- int GTextEditor::Line::width(const Font& font) const
- {
- return font.glyph_width('x') * length();
- }
|