serenity/Widgets/Widget.h
2018-10-12 02:41:27 +02:00

79 lines
2 KiB
C++

#pragma once
#include "Event.h"
#include "Object.h"
#include "Rect.h"
#include "Color.h"
#include <AK/String.h>
class Window;
class Widget : public Object {
public:
explicit Widget(Widget* parent = nullptr);
virtual ~Widget();
virtual void event(Event&);
virtual void onPaint(PaintEvent&);
virtual void onShow(ShowEvent&);
virtual void onHide(HideEvent&);
virtual void onKeyDown(KeyEvent&);
virtual void onKeyUp(KeyEvent&);
virtual void onMouseMove(MouseEvent&);
virtual void onMouseDown(MouseEvent&);
virtual void onMouseUp(MouseEvent&);
Rect rect() const { return m_rect; }
int x() const { return rect().x(); }
int y() const { return rect().y(); }
int width() const { return rect().width(); }
int height() const { return rect().height(); }
void update();
struct HitTestResult {
Widget* widget { nullptr };
int localX { 0 };
int localY { 0 };
};
HitTestResult hitTest(int x, int y);
virtual const char* className() const override { return "Widget"; }
void setRect(const Rect&);
Color backgroundColor() const { return m_backgroundColor; }
Color foregroundColor() const { return m_foregroundColor; }
void setBackgroundColor(Color color) { m_backgroundColor = color; }
void setForegroundColor(Color color) { m_foregroundColor = color; }
Window* window()
{
if (auto* pw = parentWidget())
return pw->window();
return m_window;
}
const Window* window() const
{
if (auto* pw = parentWidget())
return pw->window();
return m_window;
}
void setWindow(Window*);
Widget* parentWidget() { return static_cast<Widget*>(parent()); }
const Widget* parentWidget() const { return static_cast<const Widget*>(parent()); }
private:
Window* m_window { nullptr };
Rect m_rect;
Color m_backgroundColor;
Color m_foregroundColor;
bool m_hasPendingPaintEvent { false };
};