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