diff options
author | Andreas Kling <awesomekling@gmail.com> | 2018-10-12 20:05:11 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2018-10-12 20:05:11 +0200 |
commit | fb4ae12bc246771b8ca0d46641017fddc6ee12b8 (patch) | |
tree | f8716d9475f78ed8967eee6b5e7aa54808585048 /Widgets | |
parent | 20a1795218569857354dd9ac8f28287e6d141725 (diff) | |
download | serenity-fb4ae12bc246771b8ca0d46641017fddc6ee12b8.zip |
Add a visual bell to TerminalWidget. Also backspace.
Diffstat (limited to 'Widgets')
-rw-r--r-- | Widgets/Color.h | 9 | ||||
-rw-r--r-- | Widgets/ColorSDL.cpp | 21 | ||||
-rw-r--r-- | Widgets/TerminalWidget.cpp | 61 | ||||
-rw-r--r-- | Widgets/TerminalWidget.h | 5 |
4 files changed, 82 insertions, 14 deletions
diff --git a/Widgets/Color.h b/Widgets/Color.h index 162261723e..9a18e78ed4 100644 --- a/Widgets/Color.h +++ b/Widgets/Color.h @@ -4,7 +4,16 @@ class Color { public: + enum NamedColor { + Black, + White, + Red, + Green, + Blue, + }; + Color() { } + Color(NamedColor); Color(byte r, byte g, byte b); dword value() const { return m_value; } diff --git a/Widgets/ColorSDL.cpp b/Widgets/ColorSDL.cpp index ec6f94bf89..50228fb8fc 100644 --- a/Widgets/ColorSDL.cpp +++ b/Widgets/ColorSDL.cpp @@ -5,3 +5,24 @@ Color::Color(byte r, byte g, byte b) { m_value = SDL_MapRGB(FrameBufferSDL::the().surface()->format, r, g, b); } + +Color::Color(NamedColor named) +{ + struct { + byte r; + byte g; + byte b; + } rgb; + + switch (named) { + case Black: rgb = { 0, 0, 0 }; break; + case White: rgb = { 255, 255, 255 }; break; + case Red: rgb = { 255, 0, 0}; break; + case Green: rgb = { 0, 255, 0}; break; + case Blue: rgb = { 0, 0, 255}; break; + default: ASSERT_NOT_REACHED(); break; + } + + m_value = SDL_MapRGB(FrameBufferSDL::the().surface()->format, rgb.r, rgb.g, rgb.g); + +} diff --git a/Widgets/TerminalWidget.cpp b/Widgets/TerminalWidget.cpp index 570bfd2c01..94306ecce2 100644 --- a/Widgets/TerminalWidget.cpp +++ b/Widgets/TerminalWidget.cpp @@ -63,7 +63,7 @@ CharacterWithAttributes& TerminalWidget::at(unsigned row, unsigned column) void TerminalWidget::onPaint(PaintEvent&) { Painter painter(*this); - painter.fillRect({ 0, 0, width(), height() }, Color(0, 0, 0)); + painter.fillRect(rect(), Color(0, 0, 0)); auto& font = Font::defaultFont(); @@ -76,6 +76,9 @@ void TerminalWidget::onPaint(PaintEvent&) painter.drawText({ x + 2, y + 2, width(), font.glyphHeight() }, buf, Painter::TextAlignment::TopLeft, Color(0xa0, 0xa0, 0xa0)); } } + + if (m_belling) + painter.drawRect(rect(), Color::Red); } void TerminalWidget::onReceive(const ByteBuffer& buffer) @@ -105,20 +108,35 @@ void TerminalWidget::onReceive(byte ch) } }; - if (ch == '\n') { - if (m_cursorRow < (m_rows - 1)) { - ++m_cursorRow; - } else { - scrollScreen(); - } - } else if (ch == '\r') { - m_cursorColumn = 0; - } else if (ch == '\t') { - while ((m_cursorColumn % 8) != 0 && m_cursorColumn < m_columns) { - addChar(' '); + switch (ch) { + case '\n': + if (m_cursorRow < (m_rows - 1)) { + ++m_cursorRow; + } else { + scrollScreen(); + } + break; + case '\r': + m_cursorColumn = 0; + break; + case '\t': + // FIXME: Respect terminal tab stops. + while ((m_cursorColumn % 8) != 0 && m_cursorColumn < m_columns) { + addChar(' '); + break; + case '\a': + bell(); + break; + case 8: + if (m_cursorColumn > 0) { + --m_cursorColumn; + at(m_cursorRow, m_cursorColumn).character = ' '; + } + break; + default: + addChar(ch); + break; } - } else { - addChar(ch); } update(); } @@ -136,3 +154,18 @@ void TerminalWidget::onKeyUp(KeyEvent& event) return Widget::onKeyUp(event); } +void TerminalWidget::bell() +{ + if (m_belling) + stopTimer(); + startTimer(250); + m_belling = true; + update(); +} + +void TerminalWidget::onTimer(TimerEvent&) +{ + m_belling = false; + stopTimer(); + update(); +} diff --git a/Widgets/TerminalWidget.h b/Widgets/TerminalWidget.h index 2cd651b870..d8f25d41b7 100644 --- a/Widgets/TerminalWidget.h +++ b/Widgets/TerminalWidget.h @@ -25,6 +25,9 @@ private: virtual void onPaint(PaintEvent&) override; virtual void onKeyDown(KeyEvent&) override; virtual void onKeyUp(KeyEvent&) override; + virtual void onTimer(TimerEvent&) override; + + void bell(); unsigned m_columns { 80 }; unsigned m_rows { 25 }; @@ -33,4 +36,6 @@ private: unsigned m_cursorColumn { 0 }; CharacterWithAttributes* m_screen { nullptr }; + + bool m_belling { false }; }; |