diff --git a/Widgets/Painter.cpp b/Widgets/Painter.cpp index 2d481a7f49..5257c04286 100644 --- a/Widgets/Painter.cpp +++ b/Widgets/Painter.cpp @@ -2,6 +2,7 @@ #include "FrameBufferSDL.h" #include "Widget.h" #include "Font.h" +#include "Window.h" #include #include @@ -9,6 +10,14 @@ Painter::Painter(Widget& widget) : m_widget(widget) , m_font(Font::defaultFont()) { + if (auto* window = widget.window()) { + printf("window :: %s\n", window->title().characters()); + m_translation.setX(window->x()); + m_translation.setY(window->y()); + } else { + m_translation.setX(widget.x()); + m_translation.setY(widget.y()); + } } Painter::~Painter() @@ -26,7 +35,7 @@ static dword* scanline(int y) void Painter::fillRect(const Rect& rect, Color color) { Rect r = rect; - r.moveBy(m_widget.x(), m_widget.y()); + r.moveBy(m_translation); for (int y = r.top(); y < r.bottom(); ++y) { dword* bits = scanline(y); @@ -39,7 +48,7 @@ void Painter::fillRect(const Rect& rect, Color color) void Painter::drawRect(const Rect& rect, Color color) { Rect r = rect; - r.moveBy(m_widget.x(), m_widget.y()); + r.moveBy(m_translation); for (int y = r.top(); y < r.bottom(); ++y) { dword* bits = scanline(y); @@ -57,7 +66,7 @@ void Painter::drawRect(const Rect& rect, Color color) void Painter::xorRect(const Rect& rect, Color color) { Rect r = rect; - r.moveBy(m_widget.x(), m_widget.y()); + r.moveBy(m_translation); for (int y = r.top(); y < r.bottom(); ++y) { dword* bits = scanline(y); @@ -79,12 +88,12 @@ void Painter::drawText(const Rect& rect, const String& text, TextAlignment align if (alignment == TextAlignment::TopLeft) { point = rect.location(); - point.moveBy(m_widget.x(), m_widget.y()); + point.moveBy(m_translation); } else if (alignment == TextAlignment::Center) { int textWidth = text.length() * m_font.glyphWidth(); point = rect.center(); point.moveBy(-(textWidth / 2), -(m_font.glyphWidth() / 2)); - point.moveBy(m_widget.x(), m_widget.y()); + point.moveBy(m_translation); } else { ASSERT_NOT_REACHED(); } diff --git a/Widgets/Painter.h b/Widgets/Painter.h index 624ae4baac..c01f093eea 100644 --- a/Widgets/Painter.h +++ b/Widgets/Painter.h @@ -24,4 +24,5 @@ public: private: Widget& m_widget; Font& m_font; + Point m_translation; }; diff --git a/Widgets/Point.h b/Widgets/Point.h index 0230d83c20..31a686448e 100644 --- a/Widgets/Point.h +++ b/Widgets/Point.h @@ -17,6 +17,11 @@ public: m_y += dy; } + void moveBy(const Point& delta) + { + moveBy(delta.x(), delta.y()); + } + bool operator==(const Point& other) const { return m_x == other.m_x diff --git a/Widgets/Rect.h b/Widgets/Rect.h index 93d960434a..a9e25c3760 100644 --- a/Widgets/Rect.h +++ b/Widgets/Rect.h @@ -22,6 +22,11 @@ public: m_location.moveBy(dx, dy); } + void moveBy(const Point& delta) + { + m_location.moveBy(delta); + } + Point center() const { return { x() + width() / 2, y() + height() / 2 }; diff --git a/Widgets/TerminalWidget.cpp b/Widgets/TerminalWidget.cpp index f3be6415b0..577f86b19d 100644 --- a/Widgets/TerminalWidget.cpp +++ b/Widgets/TerminalWidget.cpp @@ -16,7 +16,7 @@ TerminalWidget::TerminalWidget(Widget* parent) auto& font = Font::defaultFont(); - setRect({ 100, 300, (columns() * font.glyphWidth()) + 4, (rows() * font.glyphHeight()) + 4 }); + setRect({ 0, 0, (columns() * font.glyphWidth()) + 4, (rows() * font.glyphHeight()) + 4 }); printf("rekt: %d x %d\n", width(), height()); m_screen = new CharacterWithAttributes[rows() * columns()]; diff --git a/Widgets/Widget.cpp b/Widgets/Widget.cpp index 7c073042d8..ecaf60328d 100644 --- a/Widgets/Widget.cpp +++ b/Widgets/Widget.cpp @@ -105,3 +105,9 @@ Widget::HitTestResult Widget::hitTest(int x, int y) return { this, x, y }; } +void Widget::setWindow(Window* window) +{ + if (m_window == window) + return; + m_window = window; +} diff --git a/Widgets/Widget.h b/Widgets/Widget.h index d7a698b3f9..230267fc16 100644 --- a/Widgets/Widget.h +++ b/Widgets/Widget.h @@ -62,6 +62,8 @@ public: return m_window; } + void setWindow(Window*); + Widget* parentWidget() { return static_cast(parent()); } const Widget* parentWidget() const { return static_cast(parent()); } diff --git a/Widgets/Window.cpp b/Widgets/Window.cpp index 81fc780db7..230150bfb5 100644 --- a/Widgets/Window.cpp +++ b/Widgets/Window.cpp @@ -1,6 +1,7 @@ #include "Window.h" #include "WindowManager.h" #include "Event.h" +#include "Widget.h" Window::Window(Object* parent) : Object(parent) @@ -18,6 +19,7 @@ void Window::setMainWidget(Widget* widget) return; m_mainWidget = widget; + widget->setWindow(this); } void Window::setTitle(String&& title) @@ -47,5 +49,12 @@ void Window::event(Event& event) return Object::event(event); } + if (event.isPaintEvent()) { + if (m_mainWidget) { + printf("forward to main widget\n"); + return m_mainWidget->event(event); + } + } + return Object::event(event); } diff --git a/Widgets/WindowManager.cpp b/Widgets/WindowManager.cpp index 889676159a..03e5a6cb2e 100644 --- a/Widgets/WindowManager.cpp +++ b/Widgets/WindowManager.cpp @@ -185,6 +185,10 @@ void WindowManager::handlePaintEvent(PaintEvent& event) //printf("[WM] paint event\n"); m_rootWidget->event(event); paintWindowFrames(); + + for (auto* window : m_windows) { + window->event(event); + } } void WindowManager::event(Event& event) diff --git a/Widgets/test.cpp b/Widgets/test.cpp index 46f588fd72..e010d69e9c 100644 --- a/Widgets/test.cpp +++ b/Widgets/test.cpp @@ -42,7 +42,7 @@ int main(int c, char** v) win->setTitle("Console"); win->setRect({100, 300, 644, 254}); - auto* t = new TerminalWidget(&w); + auto* t = new TerminalWidget(nullptr); win->setMainWidget(t); return loop.exec();