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