2019-05-28 09:53:16 +00:00
|
|
|
#include <AK/Assertions.h>
|
2019-06-22 19:21:57 +00:00
|
|
|
#include <AK/kstdio.h>
|
2019-04-10 15:01:54 +00:00
|
|
|
#include <LibCore/CEvent.h>
|
2019-04-11 22:03:21 +00:00
|
|
|
#include <LibCore/CEventLoop.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <LibCore/CObject.h>
|
2019-03-18 23:01:02 +00:00
|
|
|
#include <stdio.h>
|
2018-10-10 13:12:38 +00:00
|
|
|
|
2019-04-18 20:57:24 +00:00
|
|
|
CObject::CObject(CObject* parent, bool is_widget)
|
2018-10-10 13:12:38 +00:00
|
|
|
: m_parent(parent)
|
2019-04-18 20:57:24 +00:00
|
|
|
, m_widget(is_widget)
|
2018-10-10 13:12:38 +00:00
|
|
|
{
|
2018-10-10 14:49:36 +00:00
|
|
|
if (m_parent)
|
2019-01-31 16:31:23 +00:00
|
|
|
m_parent->add_child(*this);
|
2018-10-10 13:12:38 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
CObject::~CObject()
|
2018-10-10 13:12:38 +00:00
|
|
|
{
|
2019-02-05 09:31:37 +00:00
|
|
|
stop_timer();
|
2018-10-10 14:49:36 +00:00
|
|
|
if (m_parent)
|
2019-01-31 16:31:23 +00:00
|
|
|
m_parent->remove_child(*this);
|
|
|
|
auto children_to_delete = move(m_children);
|
|
|
|
for (auto* child : children_to_delete)
|
2018-10-10 14:49:36 +00:00
|
|
|
delete child;
|
2018-10-10 13:12:38 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
void CObject::event(CEvent& event)
|
2018-10-10 13:12:38 +00:00
|
|
|
{
|
|
|
|
switch (event.type()) {
|
2019-04-11 22:03:21 +00:00
|
|
|
case CEvent::Timer:
|
2019-04-10 14:56:55 +00:00
|
|
|
return timer_event(static_cast<CTimerEvent&>(event));
|
2019-04-11 22:03:21 +00:00
|
|
|
case CEvent::DeferredDestroy:
|
2018-10-13 23:23:01 +00:00
|
|
|
delete this;
|
|
|
|
break;
|
2019-04-11 22:03:21 +00:00
|
|
|
case CEvent::ChildAdded:
|
|
|
|
case CEvent::ChildRemoved:
|
2019-04-10 14:56:55 +00:00
|
|
|
return child_event(static_cast<CChildEvent&>(event));
|
2019-04-11 22:03:21 +00:00
|
|
|
case CEvent::Invalid:
|
2018-10-10 13:12:38 +00:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-10-10 14:49:36 +00:00
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
void CObject::add_child(CObject& object)
|
2018-10-10 14:49:36 +00:00
|
|
|
{
|
2019-05-04 23:09:49 +00:00
|
|
|
// FIXME: Should we support reparenting objects?
|
|
|
|
ASSERT(!object.parent() || object.parent() == this);
|
|
|
|
object.m_parent = this;
|
2018-10-10 14:49:36 +00:00
|
|
|
m_children.append(&object);
|
2019-04-18 20:57:24 +00:00
|
|
|
event(*make<CChildEvent>(CEvent::ChildAdded, object));
|
2018-10-10 14:49:36 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
void CObject::remove_child(CObject& object)
|
2018-10-10 14:49:36 +00:00
|
|
|
{
|
2019-02-25 21:06:55 +00:00
|
|
|
for (ssize_t i = 0; i < m_children.size(); ++i) {
|
2018-10-12 23:19:25 +00:00
|
|
|
if (m_children[i] == &object) {
|
|
|
|
m_children.remove(i);
|
2019-04-18 20:57:24 +00:00
|
|
|
event(*make<CChildEvent>(CEvent::ChildRemoved, object));
|
2018-10-12 23:19:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-10-10 14:49:36 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-12 10:18:59 +00:00
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
void CObject::timer_event(CTimerEvent&)
|
2018-10-12 10:18:59 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
void CObject::child_event(CChildEvent&)
|
2019-03-15 15:12:06 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
void CObject::start_timer(int ms)
|
2018-10-12 10:18:59 +00:00
|
|
|
{
|
2019-01-31 16:31:23 +00:00
|
|
|
if (m_timer_id) {
|
2019-04-10 15:01:54 +00:00
|
|
|
dbgprintf("CObject{%p} already has a timer!\n", this);
|
2018-10-12 10:18:59 +00:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
2019-02-01 02:50:06 +00:00
|
|
|
|
2019-04-11 22:03:21 +00:00
|
|
|
m_timer_id = CEventLoop::register_timer(*this, ms, true);
|
2018-10-12 10:18:59 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
void CObject::stop_timer()
|
2018-10-12 10:18:59 +00:00
|
|
|
{
|
2019-01-31 16:31:23 +00:00
|
|
|
if (!m_timer_id)
|
2018-10-12 10:18:59 +00:00
|
|
|
return;
|
2019-04-11 22:03:21 +00:00
|
|
|
bool success = CEventLoop::unregister_timer(m_timer_id);
|
2019-02-01 02:50:06 +00:00
|
|
|
ASSERT(success);
|
2019-01-31 16:31:23 +00:00
|
|
|
m_timer_id = 0;
|
2018-10-12 10:18:59 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
void CObject::delete_later()
|
2018-10-13 23:23:01 +00:00
|
|
|
{
|
2019-04-11 22:03:21 +00:00
|
|
|
CEventLoop::current().post_event(*this, make<CEvent>(CEvent::DeferredDestroy));
|
2018-10-13 23:23:01 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
void CObject::dump_tree(int indent)
|
2019-03-18 23:01:02 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < indent; ++i) {
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
printf("%s{%p}\n", class_name(), this);
|
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
for_each_child([&](auto& child) {
|
2019-05-27 01:52:19 +00:00
|
|
|
child.dump_tree(indent + 2);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
2019-04-07 12:36:10 +00:00
|
|
|
}
|
2019-03-18 23:01:02 +00:00
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
void CObject::deferred_invoke(Function<void(CObject&)> invokee)
|
2019-04-07 12:36:10 +00:00
|
|
|
{
|
2019-04-11 22:03:21 +00:00
|
|
|
CEventLoop::current().post_event(*this, make<CDeferredInvocationEvent>(move(invokee)));
|
2019-03-18 23:01:02 +00:00
|
|
|
}
|