serenity/Widgets/Widget.cpp

114 lines
2.3 KiB
C++
Raw Normal View History

2018-10-10 15:12:38 +02:00
#include "Widget.h"
#include "Event.h"
#include "EventLoop.h"
2018-10-11 16:52:40 +02:00
#include "WindowManager.h"
2018-10-10 15:12:38 +02:00
#include <AK/Assertions.h>
Widget::Widget(Widget* parent)
: Object(parent)
{
2018-10-11 01:48:09 +02:00
m_backgroundColor = Color(255, 255, 255);
m_foregroundColor = Color(0, 0, 0);
2018-10-10 15:12:38 +02:00
}
Widget::~Widget()
{
}
2018-10-10 16:49:36 +02:00
void Widget::setRect(const Rect& rect)
{
// FIXME: Make some kind of event loop driven ResizeEvent?
m_rect = rect;
update();
}
2018-10-10 15:12:38 +02:00
void Widget::event(Event& event)
{
switch (event.type()) {
case Event::Paint:
m_hasPendingPaintEvent = false;
2018-10-10 15:12:38 +02:00
return onPaint(static_cast<PaintEvent&>(event));
case Event::Show:
return onShow(static_cast<ShowEvent&>(event));
case Event::Hide:
return onHide(static_cast<HideEvent&>(event));
case Event::KeyDown:
return onKeyDown(static_cast<KeyEvent&>(event));
case Event::KeyUp:
return onKeyUp(static_cast<KeyEvent&>(event));
case Event::MouseMove:
return onMouseMove(static_cast<MouseEvent&>(event));
case Event::MouseDown:
return onMouseDown(static_cast<MouseEvent&>(event));
case Event::MouseUp:
return onMouseUp(static_cast<MouseEvent&>(event));
default:
return Object::event(event);
}
}
2018-10-10 16:49:36 +02:00
void Widget::onPaint(PaintEvent& event)
2018-10-10 15:12:38 +02:00
{
//printf("Widget::onPaint :)\n");
2018-10-10 16:49:36 +02:00
for (auto* ch : children()) {
auto* child = (Widget*)ch;
child->onPaint(event);
}
2018-10-10 15:12:38 +02:00
}
void Widget::onShow(ShowEvent&)
{
update();
}
void Widget::onHide(HideEvent&)
{
}
void Widget::onKeyDown(KeyEvent&)
{
}
void Widget::onKeyUp(KeyEvent&)
{
}
void Widget::onMouseDown(MouseEvent&)
{
}
void Widget::onMouseUp(MouseEvent&)
{
}
void Widget::onMouseMove(MouseEvent&)
{
}
void Widget::update()
{
if (m_hasPendingPaintEvent)
return;
m_hasPendingPaintEvent = true;
2018-10-10 15:12:38 +02:00
EventLoop::main().postEvent(this, make<PaintEvent>());
}
2018-10-10 16:49:36 +02:00
Widget::HitTestResult Widget::hitTest(int x, int y)
{
// FIXME: Care about z-order.
for (auto* ch : children()) {
auto* child = (Widget*)ch;
if (child->rect().contains(x, y)) {
return child->hitTest(x - child->rect().x(), y - child->rect().y());
}
}
return { this, x, y };
}
void Widget::setWindow(Window* window)
{
if (m_window == window)
return;
m_window = window;
}