ladybird/Widgets/Widget.cpp

165 lines
3.4 KiB
C++
Raw Normal View History

2018-10-10 15:12:38 +02:00
#include "Widget.h"
#include "Event.h"
#include "EventLoop.h"
#include "GraphicsBitmap.h"
2018-10-11 16:52:40 +02:00
#include "WindowManager.h"
2018-10-12 12:18:59 +02:00
#include "Window.h"
2018-10-14 00:21:42 +02:00
#include "Painter.h"
2018-10-10 15:12:38 +02:00
#include <AK/Assertions.h>
Widget::Widget(Widget* parent)
: Object(parent)
{
setFont(nullptr);
2018-10-12 23:02:23 +02:00
m_backgroundColor = Color::White;
m_foregroundColor = Color::Black;
2018-10-10 15:12:38 +02:00
}
Widget::~Widget()
{
}
void Widget::setWindowRelativeRect(const Rect& rect, bool should_update)
2018-10-10 16:49:36 +02:00
{
// FIXME: Make some kind of event loop driven ResizeEvent?
2018-10-12 14:15:14 +02:00
m_relativeRect = rect;
if (should_update)
update();
2018-10-10 16:49:36 +02:00
}
void Widget::repaint(const Rect& rect)
{
// FIXME: Implement.
}
2018-10-10 15:12:38 +02:00
void Widget::event(Event& event)
{
switch (event.type()) {
case Event::Paint:
m_hasPendingPaintEvent = false;
2018-10-12 12:18:59 +02:00
if (auto* win = window()) {
2019-01-16 13:49:44 +01:00
if (win->is_being_dragged())
2018-10-12 12:18:59 +02:00
return;
2019-01-16 13:49:44 +01:00
if (!win->is_visible())
return;
2018-10-12 12:18:59 +02:00
}
return paintEvent(static_cast<PaintEvent&>(event));
2018-10-10 15:12:38 +02:00
case Event::Show:
return showEvent(static_cast<ShowEvent&>(event));
2018-10-10 15:12:38 +02:00
case Event::Hide:
return hideEvent(static_cast<HideEvent&>(event));
2018-10-10 15:12:38 +02:00
case Event::KeyDown:
return keyDownEvent(static_cast<KeyEvent&>(event));
2018-10-10 15:12:38 +02:00
case Event::KeyUp:
return keyUpEvent(static_cast<KeyEvent&>(event));
2018-10-10 15:12:38 +02:00
case Event::MouseMove:
return mouseMoveEvent(static_cast<MouseEvent&>(event));
2018-10-10 15:12:38 +02:00
case Event::MouseDown:
// FIXME: Focus self if needed.
return mouseDownEvent(static_cast<MouseEvent&>(event));
2018-10-10 15:12:38 +02:00
case Event::MouseUp:
return mouseUpEvent(static_cast<MouseEvent&>(event));
2018-10-10 15:12:38 +02:00
default:
return Object::event(event);
}
}
void Widget::paintEvent(PaintEvent& event)
2018-10-10 15:12:38 +02:00
{
//printf("Widget::paintEvent :)\n");
2018-10-14 00:21:42 +02:00
if (fillWithBackgroundColor()) {
Painter painter(*this);
painter.fill_rect(rect(), backgroundColor());
2018-10-14 00:21:42 +02:00
}
2018-10-10 16:49:36 +02:00
for (auto* ch : children()) {
auto* child = (Widget*)ch;
child->event(event);
2018-10-10 16:49:36 +02:00
}
2018-10-10 15:12:38 +02:00
}
void Widget::showEvent(ShowEvent&)
2018-10-10 15:12:38 +02:00
{
}
void Widget::hideEvent(HideEvent&)
2018-10-10 15:12:38 +02:00
{
}
void Widget::keyDownEvent(KeyEvent&)
2018-10-10 15:12:38 +02:00
{
}
void Widget::keyUpEvent(KeyEvent&)
2018-10-10 15:12:38 +02:00
{
}
void Widget::mouseDownEvent(MouseEvent&)
2018-10-10 15:12:38 +02:00
{
}
void Widget::mouseUpEvent(MouseEvent&)
2018-10-10 15:12:38 +02:00
{
}
void Widget::mouseMoveEvent(MouseEvent&)
2018-10-10 15:12:38 +02:00
{
}
void Widget::update()
{
auto* w = window();
if (!w)
return;
if (m_hasPendingPaintEvent)
return;
m_hasPendingPaintEvent = true;
EventLoop::main().postEvent(w, make<PaintEvent>(relativeRect()));
2018-10-10 15:12:38 +02:00
}
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;
2018-10-12 14:15:14 +02:00
if (child->relativeRect().contains(x, y)) {
return child->hitTest(x - child->relativeRect().x(), y - child->relativeRect().y());
2018-10-10 16:49:36 +02:00
}
}
return { this, x, y };
}
void Widget::setWindow(Window* window)
{
if (m_window == window)
return;
m_window = window;
}
2018-10-13 17:52:47 +02:00
bool Widget::isFocused() const
{
// FIXME: Implement.
return false;
2018-10-13 17:52:47 +02:00
}
void Widget::setFocus(bool focus)
{
if (focus == isFocused())
return;
// FIXME: Implement.
2018-10-13 17:52:47 +02:00
}
void Widget::setFont(RetainPtr<Font>&& font)
{
if (!font)
m_font = Font::defaultFont();
else
m_font = move(font);
}
GraphicsBitmap* Widget::backing()
{
if (auto* w = window())
return w->backing();
return nullptr;
}