ladybird/LibGUI/GObject.cpp

77 lines
1.4 KiB
C++
Raw Normal View History

#include "GObject.h"
#include "GEvent.h"
#include "GEventLoop.h"
2018-10-10 13:12:38 +00:00
#include <AK/Assertions.h>
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
}
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);
auto childrenToDelete = move(m_children);
for (auto* child : childrenToDelete)
2018-10-10 14:49:36 +00:00
delete child;
2018-10-10 13:12:38 +00:00
}
void GObject::event(GEvent& event)
2018-10-10 13:12:38 +00:00
{
switch (event.type()) {
case GEvent::Timer:
return timer_event(static_cast<GTimerEvent&>(event));
case GEvent::DeferredDestroy:
delete this;
break;
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
void GObject::addChild(GObject& object)
2018-10-10 14:49:36 +00:00
{
m_children.append(&object);
}
void GObject::removeChild(GObject& object)
2018-10-10 14:49:36 +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
void GObject::timer_event(GTimerEvent&)
2018-10-12 10:18:59 +00:00
{
}
void GObject::startTimer(int ms)
2018-10-12 10:18:59 +00:00
{
if (m_timerID) {
dbgprintf("GObject{%p} already has a timer!\n", this);
2018-10-12 10:18:59 +00:00
ASSERT_NOT_REACHED();
}
}
void GObject::stopTimer()
2018-10-12 10:18:59 +00:00
{
if (!m_timerID)
return;
m_timerID = 0;
}
void GObject::delete_later()
{
GEventLoop::main().post_event(this, make<GEvent>(GEvent::DeferredDestroy));
}