ladybird/LibGUI/GWidget.h

98 lines
2.8 KiB
C
Raw Normal View History

2018-10-10 15:12:38 +02:00
#pragma once
#include "GEvent.h"
#include "GObject.h"
2019-01-19 23:49:56 +01:00
#include <SharedGraphics/Rect.h>
#include <SharedGraphics/Color.h>
#include <SharedGraphics/Font.h>
#include <AK/AKString.h>
2018-10-10 15:12:38 +02:00
class GraphicsBitmap;
class GWindow;
2018-10-12 01:03:22 +02:00
class GWidget : public GObject {
2018-10-10 15:12:38 +02:00
public:
explicit GWidget(GWidget* parent = nullptr);
2019-01-21 00:46:08 +01:00
virtual ~GWidget() override;
virtual void event(GEvent&) override;
2019-01-21 00:46:08 +01:00
virtual void paint_event(GPaintEvent&);
virtual void show_event(GShowEvent&);
virtual void hide_event(GHideEvent&);
virtual void keydown_event(GKeyEvent&);
virtual void keyup_event(GKeyEvent&);
virtual void mousemove_event(GMouseEvent&);
virtual void mousedown_event(GMouseEvent&);
virtual void mouseup_event(GMouseEvent&);
Rect relative_rect() const { return m_relative_rect; }
Point relative_position() const { return m_relative_rect.location(); }
int x() const { return m_relative_rect.x(); }
int y() const { return m_relative_rect.y(); }
int width() const { return m_relative_rect.width(); }
int height() const { return m_relative_rect.height(); }
2018-10-12 14:15:14 +02:00
Rect rect() const { return { 0, 0, width(), height() }; }
2019-01-21 00:46:08 +01:00
Size size() const { return m_relative_rect.size(); }
2018-10-10 16:49:36 +02:00
2018-10-10 15:12:38 +02:00
void update();
void repaint(const Rect&);
2018-10-10 15:12:38 +02:00
2019-01-21 00:46:08 +01:00
bool is_focused() const;
void set_focus(bool);
2018-10-13 17:52:47 +02:00
2018-10-10 16:49:36 +02:00
struct HitTestResult {
GWidget* widget { nullptr };
2018-10-10 16:49:36 +02:00
int localX { 0 };
int localY { 0 };
};
2019-01-21 00:46:08 +01:00
HitTestResult hit_test(int x, int y);
2018-10-10 16:49:36 +02:00
virtual const char* class_name() const override { return "GWidget"; }
2018-10-10 16:49:36 +02:00
void set_relative_rect(const Rect&);
2018-10-10 16:49:36 +02:00
2019-01-21 00:46:08 +01:00
Color background_color() const { return m_background_color; }
Color foreground_color() const { return m_foreground_color; }
2018-10-11 01:48:09 +02:00
2019-01-21 00:46:08 +01:00
void set_background_color(Color color) { m_background_color = color; }
void set_foreground_color(Color color) { m_foreground_color = color; }
2018-10-11 01:48:09 +02:00
GWindow* window()
2018-10-12 01:03:22 +02:00
{
2019-01-21 00:46:08 +01:00
if (auto* pw = parent_widget())
2018-10-12 01:03:22 +02:00
return pw->window();
return m_window;
}
const GWindow* window() const
2018-10-12 01:03:22 +02:00
{
2019-01-21 00:46:08 +01:00
if (auto* pw = parent_widget())
2018-10-12 01:03:22 +02:00
return pw->window();
return m_window;
}
2018-10-11 16:52:40 +02:00
void set_window(GWindow*);
2019-01-21 00:46:08 +01:00
GWidget* parent_widget() { return static_cast<GWidget*>(parent()); }
const GWidget* parent_widget() const { return static_cast<const GWidget*>(parent()); }
2018-10-11 16:52:40 +02:00
2019-01-21 00:46:08 +01:00
void set_fill_with_background_color(bool b) { m_fill_with_background_color = b; }
bool fill_with_background_color() const { return m_fill_with_background_color; }
2018-10-14 00:21:42 +02:00
const Font& font() const { return *m_font; }
2019-01-21 00:46:08 +01:00
void set_font(RetainPtr<Font>&&);
2018-10-10 15:12:38 +02:00
private:
GWindow* m_window { nullptr };
2018-10-12 01:03:22 +02:00
2019-01-21 00:46:08 +01:00
Rect m_relative_rect;
Color m_background_color { 0xffffff };
Color m_foreground_color { 0x000000 };
RetainPtr<Font> m_font;
2019-01-21 00:46:08 +01:00
bool m_has_pending_paint_event { false };
bool m_fill_with_background_color { true };
2018-10-10 15:12:38 +02:00
};