2019-01-20 03:49:48 +00:00
|
|
|
#include "GObject.h"
|
|
|
|
#include "GEvent.h"
|
|
|
|
#include "GEventLoop.h"
|
2018-10-10 13:12:38 +00:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
|
2019-01-20 03:49:48 +00:00
|
|
|
GObject::GObject(GObject* parent)
|
2018-10-10 13:12:38 +00:00
|
|
|
: m_parent(parent)
|
|
|
|
{
|
2018-10-10 14:49:36 +00:00
|
|
|
if (m_parent)
|
|
|
|
m_parent->addChild(*this);
|
2018-10-10 13:12:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-20 03:49:48 +00:00
|
|
|
GObject::~GObject()
|
2018-10-10 13:12:38 +00:00
|
|
|
{
|
2018-10-10 14:49:36 +00:00
|
|
|
if (m_parent)
|
|
|
|
m_parent->removeChild(*this);
|
2019-01-10 22:19:29 +00:00
|
|
|
auto childrenToDelete = move(m_children);
|
2018-10-10 23:00:15 +00:00
|
|
|
for (auto* child : childrenToDelete)
|
2018-10-10 14:49:36 +00:00
|
|
|
delete child;
|
2018-10-10 13:12:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-20 03:49:48 +00:00
|
|
|
void GObject::event(GEvent& event)
|
2018-10-10 13:12:38 +00:00
|
|
|
{
|
|
|
|
switch (event.type()) {
|
2019-01-20 03:49:48 +00:00
|
|
|
case GEvent::Timer:
|
2019-01-31 15:37:43 +00:00
|
|
|
return timer_event(static_cast<GTimerEvent&>(event));
|
2019-01-20 03:49:48 +00:00
|
|
|
case GEvent::DeferredDestroy:
|
2018-10-13 23:23:01 +00:00
|
|
|
delete this;
|
|
|
|
break;
|
2019-01-20 03:49:48 +00:00
|
|
|
case GEvent::Invalid:
|
2018-10-10 13:12:38 +00:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-10-10 14:49:36 +00:00
|
|
|
|
2019-01-20 03:49:48 +00:00
|
|
|
void GObject::addChild(GObject& object)
|
2018-10-10 14:49:36 +00:00
|
|
|
{
|
|
|
|
m_children.append(&object);
|
|
|
|
}
|
|
|
|
|
2019-01-20 03:49:48 +00:00
|
|
|
void GObject::removeChild(GObject& object)
|
2018-10-10 14:49:36 +00:00
|
|
|
{
|
2018-10-12 23:19:25 +00:00
|
|
|
for (unsigned i = 0; i < m_children.size(); ++i) {
|
|
|
|
if (m_children[i] == &object) {
|
|
|
|
m_children.remove(i);
|
|
|
|
return;
|
|
|
|
}
|
2018-10-10 14:49:36 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-12 10:18:59 +00:00
|
|
|
|
2019-01-31 15:37:43 +00:00
|
|
|
void GObject::timer_event(GTimerEvent&)
|
2018-10-12 10:18:59 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-20 03:49:48 +00:00
|
|
|
void GObject::startTimer(int ms)
|
2018-10-12 10:18:59 +00:00
|
|
|
{
|
|
|
|
if (m_timerID) {
|
2019-01-20 03:49:48 +00:00
|
|
|
dbgprintf("GObject{%p} already has a timer!\n", this);
|
2018-10-12 10:18:59 +00:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-20 03:49:48 +00:00
|
|
|
void GObject::stopTimer()
|
2018-10-12 10:18:59 +00:00
|
|
|
{
|
|
|
|
if (!m_timerID)
|
|
|
|
return;
|
|
|
|
m_timerID = 0;
|
|
|
|
}
|
|
|
|
|
2019-01-30 19:03:52 +00:00
|
|
|
void GObject::delete_later()
|
2018-10-13 23:23:01 +00:00
|
|
|
{
|
2019-01-20 04:48:43 +00:00
|
|
|
GEventLoop::main().post_event(this, make<GEvent>(GEvent::DeferredDestroy));
|
2018-10-13 23:23:01 +00:00
|
|
|
}
|
|
|
|
|