2019-01-20 06:49:48 +03:00
|
|
|
#include "GWidget.h"
|
|
|
|
#include "GEvent.h"
|
|
|
|
#include "GEventLoop.h"
|
|
|
|
#include "GWindow.h"
|
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <SharedGraphics/GraphicsBitmap.h>
|
|
|
|
#include <SharedGraphics/Painter.h>
|
|
|
|
|
|
|
|
GWidget::GWidget(GWidget* parent)
|
|
|
|
: GObject(parent)
|
|
|
|
{
|
2019-01-21 02:46:08 +03:00
|
|
|
set_font(nullptr);
|
|
|
|
m_background_color = Color::White;
|
|
|
|
m_foreground_color = Color::Black;
|
2019-01-20 06:49:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
GWidget::~GWidget()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-25 01:40:12 +03:00
|
|
|
void GWidget::set_relative_rect(const Rect& rect)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
2019-01-25 01:40:12 +03:00
|
|
|
if (rect == m_relative_rect)
|
|
|
|
return;
|
2019-01-20 06:49:48 +03:00
|
|
|
// FIXME: Make some kind of event loop driven ResizeEvent?
|
2019-01-21 02:46:08 +03:00
|
|
|
m_relative_rect = rect;
|
2019-01-25 01:40:12 +03:00
|
|
|
update();
|
2019-01-20 06:49:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void GWidget::repaint(const Rect& rect)
|
|
|
|
{
|
|
|
|
// FIXME: Implement.
|
|
|
|
}
|
|
|
|
|
|
|
|
void GWidget::event(GEvent& event)
|
|
|
|
{
|
|
|
|
switch (event.type()) {
|
|
|
|
case GEvent::Paint:
|
2019-01-21 02:46:08 +03:00
|
|
|
m_has_pending_paint_event = false;
|
|
|
|
return paint_event(static_cast<GPaintEvent&>(event));
|
2019-01-26 13:24:16 +03:00
|
|
|
case GEvent::FocusIn:
|
|
|
|
case GEvent::FocusOut:
|
|
|
|
return focusin_event(event);
|
2019-01-20 06:49:48 +03:00
|
|
|
case GEvent::Show:
|
2019-01-21 02:46:08 +03:00
|
|
|
return show_event(static_cast<GShowEvent&>(event));
|
2019-01-20 06:49:48 +03:00
|
|
|
case GEvent::Hide:
|
2019-01-21 02:46:08 +03:00
|
|
|
return hide_event(static_cast<GHideEvent&>(event));
|
2019-01-20 06:49:48 +03:00
|
|
|
case GEvent::KeyDown:
|
2019-01-21 02:46:08 +03:00
|
|
|
return keydown_event(static_cast<GKeyEvent&>(event));
|
2019-01-20 06:49:48 +03:00
|
|
|
case GEvent::KeyUp:
|
2019-01-21 02:46:08 +03:00
|
|
|
return keyup_event(static_cast<GKeyEvent&>(event));
|
2019-01-20 06:49:48 +03:00
|
|
|
case GEvent::MouseMove:
|
2019-01-21 02:46:08 +03:00
|
|
|
return mousemove_event(static_cast<GMouseEvent&>(event));
|
2019-01-20 06:49:48 +03:00
|
|
|
case GEvent::MouseDown:
|
2019-01-26 13:24:16 +03:00
|
|
|
if (accepts_focus())
|
|
|
|
set_focus(true);
|
2019-01-21 02:46:08 +03:00
|
|
|
return mousedown_event(static_cast<GMouseEvent&>(event));
|
2019-01-20 06:49:48 +03:00
|
|
|
case GEvent::MouseUp:
|
2019-01-21 02:46:08 +03:00
|
|
|
return mouseup_event(static_cast<GMouseEvent&>(event));
|
2019-01-20 06:49:48 +03:00
|
|
|
default:
|
|
|
|
return GObject::event(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void GWidget::paint_event(GPaintEvent& event)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
2019-01-21 02:46:08 +03:00
|
|
|
if (fill_with_background_color()) {
|
2019-01-20 06:49:48 +03:00
|
|
|
Painter painter(*this);
|
2019-01-21 02:46:08 +03:00
|
|
|
painter.fill_rect(event.rect(), background_color());
|
2019-01-20 06:49:48 +03:00
|
|
|
}
|
|
|
|
for (auto* ch : children()) {
|
|
|
|
auto* child = (GWidget*)ch;
|
|
|
|
child->event(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void GWidget::show_event(GShowEvent&)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void GWidget::hide_event(GHideEvent&)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void GWidget::keydown_event(GKeyEvent&)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void GWidget::keyup_event(GKeyEvent&)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void GWidget::mousedown_event(GMouseEvent&)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void GWidget::mouseup_event(GMouseEvent&)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void GWidget::mousemove_event(GMouseEvent&)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-26 13:24:16 +03:00
|
|
|
void GWidget::focusin_event(GEvent&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void GWidget::focusout_event(GEvent&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
void GWidget::update()
|
|
|
|
{
|
|
|
|
auto* w = window();
|
|
|
|
if (!w)
|
|
|
|
return;
|
2019-01-21 02:46:08 +03:00
|
|
|
if (m_has_pending_paint_event)
|
2019-01-20 06:49:48 +03:00
|
|
|
return;
|
2019-01-21 02:46:08 +03:00
|
|
|
m_has_pending_paint_event = true;
|
2019-01-26 07:20:32 +03:00
|
|
|
w->update(relative_rect());
|
2019-01-20 06:49:48 +03:00
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
GWidget::HitTestResult GWidget::hit_test(int x, int y)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
// FIXME: Care about z-order.
|
|
|
|
for (auto* ch : children()) {
|
|
|
|
auto* child = (GWidget*)ch;
|
2019-01-21 02:46:08 +03:00
|
|
|
if (child->relative_rect().contains(x, y)) {
|
|
|
|
return child->hit_test(x - child->relative_rect().x(), y - child->relative_rect().y());
|
2019-01-20 06:49:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return { this, x, y };
|
|
|
|
}
|
|
|
|
|
2019-01-20 09:03:38 +03:00
|
|
|
void GWidget::set_window(GWindow* window)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
if (m_window == window)
|
|
|
|
return;
|
|
|
|
m_window = window;
|
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
bool GWidget::is_focused() const
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
2019-01-26 13:24:16 +03:00
|
|
|
auto* win = window();
|
|
|
|
if (!win)
|
|
|
|
return false;
|
|
|
|
return win->focused_widget() == this;
|
2019-01-20 06:49:48 +03:00
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void GWidget::set_focus(bool focus)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
2019-01-26 13:24:16 +03:00
|
|
|
auto* win = window();
|
|
|
|
if (!win)
|
2019-01-20 06:49:48 +03:00
|
|
|
return;
|
2019-01-26 13:24:16 +03:00
|
|
|
if (focus) {
|
|
|
|
win->set_focused_widget(this);
|
|
|
|
} else {
|
|
|
|
if (win->focused_widget() == this)
|
|
|
|
win->set_focused_widget(nullptr);
|
|
|
|
}
|
2019-01-20 06:49:48 +03:00
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void GWidget::set_font(RetainPtr<Font>&& font)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
if (!font)
|
|
|
|
m_font = Font::default_font();
|
|
|
|
else
|
|
|
|
m_font = move(font);
|
|
|
|
}
|