2018-10-10 13:12:38 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Event.h"
|
|
|
|
#include "Object.h"
|
2018-10-10 14:49:36 +00:00
|
|
|
#include "Rect.h"
|
2018-10-10 23:48:09 +00:00
|
|
|
#include "Color.h"
|
2018-10-14 11:06:05 +00:00
|
|
|
#include "Font.h"
|
2018-10-11 14:52:40 +00:00
|
|
|
#include <AK/String.h>
|
2018-10-13 21:01:06 +00:00
|
|
|
#include <functional>
|
2018-10-10 13:12:38 +00:00
|
|
|
|
2018-10-11 23:03:22 +00:00
|
|
|
class Window;
|
|
|
|
|
2018-10-10 13:12:38 +00:00
|
|
|
class Widget : public Object {
|
|
|
|
public:
|
|
|
|
explicit Widget(Widget* parent = nullptr);
|
|
|
|
virtual ~Widget();
|
|
|
|
|
|
|
|
virtual void event(Event&);
|
2018-10-13 20:51:50 +00:00
|
|
|
virtual void paintEvent(PaintEvent&);
|
|
|
|
virtual void showEvent(ShowEvent&);
|
|
|
|
virtual void hideEvent(HideEvent&);
|
|
|
|
virtual void keyDownEvent(KeyEvent&);
|
|
|
|
virtual void keyUpEvent(KeyEvent&);
|
|
|
|
virtual void mouseMoveEvent(MouseEvent&);
|
|
|
|
virtual void mouseDownEvent(MouseEvent&);
|
|
|
|
virtual void mouseUpEvent(MouseEvent&);
|
2018-10-10 13:12:38 +00:00
|
|
|
|
2018-10-12 12:15:14 +00:00
|
|
|
Rect relativeRect() const { return m_relativeRect; }
|
|
|
|
Point relativePosition() const { return m_relativeRect.location(); }
|
2018-10-12 08:06:50 +00:00
|
|
|
|
2018-10-12 12:15:14 +00:00
|
|
|
int x() const { return m_relativeRect.x(); }
|
|
|
|
int y() const { return m_relativeRect.y(); }
|
|
|
|
int width() const { return m_relativeRect.width(); }
|
|
|
|
int height() const { return m_relativeRect.height(); }
|
|
|
|
|
|
|
|
Rect rect() const { return { 0, 0, width(), height() }; }
|
2018-10-10 14:49:36 +00:00
|
|
|
|
2018-10-10 13:12:38 +00:00
|
|
|
void update();
|
2018-10-12 17:39:48 +00:00
|
|
|
void repaint(const Rect&);
|
2018-10-10 13:12:38 +00:00
|
|
|
|
2018-10-13 15:52:47 +00:00
|
|
|
bool isFocused() const;
|
|
|
|
void setFocus(bool);
|
|
|
|
|
2018-10-10 14:49:36 +00:00
|
|
|
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"; }
|
|
|
|
|
2018-10-12 12:15:14 +00:00
|
|
|
void setWindowRelativeRect(const Rect&);
|
2018-10-10 14:49:36 +00:00
|
|
|
|
2018-10-10 23:48:09 +00:00
|
|
|
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; }
|
|
|
|
|
2018-10-11 23:03:22 +00:00
|
|
|
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;
|
|
|
|
}
|
2018-10-11 14:52:40 +00:00
|
|
|
|
2018-10-12 00:41:27 +00:00
|
|
|
void setWindow(Window*);
|
|
|
|
|
2018-10-11 23:03:22 +00:00
|
|
|
Widget* parentWidget() { return static_cast<Widget*>(parent()); }
|
|
|
|
const Widget* parentWidget() const { return static_cast<const Widget*>(parent()); }
|
2018-10-11 14:52:40 +00:00
|
|
|
|
2018-10-13 22:21:42 +00:00
|
|
|
void setFillWithBackgroundColor(bool b) { m_fillWithBackgroundColor = b; }
|
|
|
|
bool fillWithBackgroundColor() const { return m_fillWithBackgroundColor; }
|
|
|
|
|
2018-10-14 11:06:05 +00:00
|
|
|
const Font& font() const { return *m_font; }
|
|
|
|
void setFont(RetainPtr<Font>&&);
|
|
|
|
|
2018-10-10 13:12:38 +00:00
|
|
|
private:
|
2018-10-11 23:03:22 +00:00
|
|
|
Window* m_window { nullptr };
|
|
|
|
|
2018-10-12 12:15:14 +00:00
|
|
|
Rect m_relativeRect;
|
2018-10-10 23:48:09 +00:00
|
|
|
Color m_backgroundColor;
|
|
|
|
Color m_foregroundColor;
|
2018-10-14 11:06:05 +00:00
|
|
|
RetainPtr<Font> m_font;
|
2018-10-11 10:33:03 +00:00
|
|
|
|
|
|
|
bool m_hasPendingPaintEvent { false };
|
2018-10-13 22:21:42 +00:00
|
|
|
bool m_fillWithBackgroundColor { false };
|
2018-10-10 13:12:38 +00:00
|
|
|
};
|