mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 02:27:35 +00:00
GTextEditor: Disable the ruler in single-line mode.
Also make it possible to hide the ruler in multi-line mode, if you should want to do that. :^)
This commit is contained in:
parent
1fc283ed7d
commit
7e3673f710
2 changed files with 27 additions and 13 deletions
|
@ -13,6 +13,8 @@ GTextEditor::GTextEditor(Type type, GWidget* parent)
|
||||||
: GWidget(parent)
|
: GWidget(parent)
|
||||||
, m_type(type)
|
, m_type(type)
|
||||||
{
|
{
|
||||||
|
m_ruler_visible = m_type == MultiLine;
|
||||||
|
|
||||||
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 = new GScrollBar(Orientation::Vertical, this);
|
||||||
|
@ -165,12 +167,16 @@ void GTextEditor::mousemove_event(GMouseEvent& event)
|
||||||
|
|
||||||
int GTextEditor::ruler_width() const
|
int GTextEditor::ruler_width() const
|
||||||
{
|
{
|
||||||
|
if (!m_ruler_visible)
|
||||||
|
return 0;
|
||||||
// FIXME: Resize based on needed space.
|
// FIXME: Resize based on needed space.
|
||||||
return 5 * font().glyph_width('x') + 4;
|
return 5 * font().glyph_width('x') + 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
Rect GTextEditor::ruler_content_rect(int line_index) const
|
Rect GTextEditor::ruler_content_rect(int line_index) const
|
||||||
{
|
{
|
||||||
|
if (!m_ruler_visible)
|
||||||
|
return { };
|
||||||
return {
|
return {
|
||||||
0 - ruler_width() - padding() + m_horizontal_scrollbar->value(),
|
0 - ruler_width() - padding() + m_horizontal_scrollbar->value(),
|
||||||
line_index * line_height(),
|
line_index * line_height(),
|
||||||
|
@ -188,8 +194,11 @@ void GTextEditor::paint_event(GPaintEvent& event)
|
||||||
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() - m_horizontal_scrollbar->height()};
|
||||||
painter.fill_rect(ruler_rect, Color::LightGray);
|
|
||||||
painter.draw_line(ruler_rect.top_right(), ruler_rect.bottom_right(), Color::DarkGray);
|
if (m_ruler_visible) {
|
||||||
|
painter.fill_rect(ruler_rect, Color::LightGray);
|
||||||
|
painter.draw_line(ruler_rect.top_right(), ruler_rect.bottom_right(), Color::DarkGray);
|
||||||
|
}
|
||||||
|
|
||||||
painter.save();
|
painter.save();
|
||||||
|
|
||||||
|
@ -203,16 +212,18 @@ void GTextEditor::paint_event(GPaintEvent& event)
|
||||||
auto selection = normalized_selection();
|
auto selection = normalized_selection();
|
||||||
bool has_selection = selection.is_valid();
|
bool has_selection = selection.is_valid();
|
||||||
|
|
||||||
for (int i = first_visible_line; i <= last_visible_line; ++i) {
|
if (m_ruler_visible) {
|
||||||
bool is_current_line = i == m_cursor.line();
|
for (int i = first_visible_line; i <= last_visible_line; ++i) {
|
||||||
auto ruler_line_rect = ruler_content_rect(i);
|
bool is_current_line = i == m_cursor.line();
|
||||||
painter.draw_text(
|
auto ruler_line_rect = ruler_content_rect(i);
|
||||||
ruler_line_rect.shrunken(2, 0),
|
painter.draw_text(
|
||||||
String::format("%u", i),
|
ruler_line_rect.shrunken(2, 0),
|
||||||
is_current_line ? Font::default_bold_font() : font(),
|
String::format("%u", i),
|
||||||
TextAlignment::CenterRight,
|
is_current_line ? Font::default_bold_font() : font(),
|
||||||
is_current_line ? Color::DarkGray : Color::MidGray
|
TextAlignment::CenterRight,
|
||||||
);
|
is_current_line ? Color::DarkGray : Color::MidGray
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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() - m_vertical_scrollbar->width() - ruler_width(), height() - m_horizontal_scrollbar->height() });
|
||||||
|
@ -221,7 +232,7 @@ void GTextEditor::paint_event(GPaintEvent& event)
|
||||||
auto& line = *m_lines[i];
|
auto& line = *m_lines[i];
|
||||||
auto line_rect = line_content_rect(i);
|
auto line_rect = line_content_rect(i);
|
||||||
line_rect.set_width(exposed_width);
|
line_rect.set_width(exposed_width);
|
||||||
if (i == m_cursor.line())
|
if (is_multi_line() && i == m_cursor.line())
|
||||||
painter.fill_rect(line_rect, Color(230, 230, 230));
|
painter.fill_rect(line_rect, Color(230, 230, 230));
|
||||||
painter.draw_text(line_rect, line.characters(), line.length(), TextAlignment::CenterLeft, Color::Black);
|
painter.draw_text(line_rect, line.characters(), line.length(), TextAlignment::CenterLeft, Color::Black);
|
||||||
bool line_has_selection = has_selection && i >= selection.start().line() && i <= selection.end().line();
|
bool line_has_selection = has_selection && i >= selection.start().line() && i <= selection.end().line();
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include <AK/HashMap.h>
|
#include <AK/HashMap.h>
|
||||||
|
|
||||||
class GScrollBar;
|
class GScrollBar;
|
||||||
|
class Painter;
|
||||||
|
|
||||||
class GTextPosition {
|
class GTextPosition {
|
||||||
public:
|
public:
|
||||||
|
@ -110,6 +111,7 @@ private:
|
||||||
virtual void focusout_event(GEvent&) override;
|
virtual void focusout_event(GEvent&) override;
|
||||||
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&);
|
||||||
|
|
||||||
class Line {
|
class Line {
|
||||||
friend class GTextEditor;
|
friend class GTextEditor;
|
||||||
|
@ -161,6 +163,7 @@ private:
|
||||||
GTextPosition m_cursor;
|
GTextPosition m_cursor;
|
||||||
bool m_cursor_state { true };
|
bool m_cursor_state { true };
|
||||||
bool m_in_drag_select { false };
|
bool m_in_drag_select { false };
|
||||||
|
bool m_ruler_visible { true };
|
||||||
int m_line_spacing { 2 };
|
int m_line_spacing { 2 };
|
||||||
int m_soft_tab_width { 4 };
|
int m_soft_tab_width { 4 };
|
||||||
GTextRange m_selection;
|
GTextRange m_selection;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue