2019-05-28 06:53:16 -03:00
|
|
|
#include <AK/Assertions.h>
|
2019-08-18 15:36:37 -03:00
|
|
|
#include <AK/JsonObject.h>
|
2019-06-22 16:21:57 -03:00
|
|
|
#include <AK/kstdio.h>
|
2019-04-10 12:01:54 -03:00
|
|
|
#include <LibCore/CEvent.h>
|
2019-04-11 19:03:21 -03:00
|
|
|
#include <LibCore/CEventLoop.h>
|
2019-05-28 06:53:16 -03:00
|
|
|
#include <LibCore/CObject.h>
|
2019-03-18 20:01:02 -03:00
|
|
|
#include <stdio.h>
|
2018-10-10 10:12:38 -03:00
|
|
|
|
2019-08-17 06:26:19 -03:00
|
|
|
IntrusiveList<CObject, &CObject::m_all_objects_list_node>& CObject::all_objects()
|
|
|
|
|
{
|
|
|
|
|
static IntrusiveList<CObject, &CObject::m_all_objects_list_node> objects;
|
|
|
|
|
return objects;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-18 17:57:24 -03:00
|
|
|
CObject::CObject(CObject* parent, bool is_widget)
|
2018-10-10 10:12:38 -03:00
|
|
|
: m_parent(parent)
|
2019-04-18 17:57:24 -03:00
|
|
|
, m_widget(is_widget)
|
2018-10-10 10:12:38 -03:00
|
|
|
{
|
2019-08-17 06:26:19 -03:00
|
|
|
all_objects().append(*this);
|
2018-10-10 11:49:36 -03:00
|
|
|
if (m_parent)
|
2019-01-31 14:31:23 -02:00
|
|
|
m_parent->add_child(*this);
|
2018-10-10 10:12:38 -03:00
|
|
|
}
|
|
|
|
|
|
2019-04-10 12:01:54 -03:00
|
|
|
CObject::~CObject()
|
2018-10-10 10:12:38 -03:00
|
|
|
{
|
2019-09-21 19:17:53 -03:00
|
|
|
// NOTE: We move our children out to a stack vector to prevent other
|
|
|
|
|
// code from trying to iterate over them.
|
|
|
|
|
auto children = move(m_children);
|
|
|
|
|
// NOTE: We also unparent the children, so that they won't try to unparent
|
|
|
|
|
// themselves in their own destructors.
|
|
|
|
|
for (auto& child : children)
|
|
|
|
|
child.m_parent = nullptr;
|
|
|
|
|
|
2019-08-17 06:26:19 -03:00
|
|
|
all_objects().remove(*this);
|
2019-02-05 07:31:37 -02:00
|
|
|
stop_timer();
|
2018-10-10 11:49:36 -03:00
|
|
|
if (m_parent)
|
2019-01-31 14:31:23 -02:00
|
|
|
m_parent->remove_child(*this);
|
2018-10-10 10:12:38 -03:00
|
|
|
}
|
|
|
|
|
|
2019-04-10 12:01:54 -03:00
|
|
|
void CObject::event(CEvent& event)
|
2018-10-10 10:12:38 -03:00
|
|
|
{
|
|
|
|
|
switch (event.type()) {
|
2019-04-11 19:03:21 -03:00
|
|
|
case CEvent::Timer:
|
2019-04-10 11:56:55 -03:00
|
|
|
return timer_event(static_cast<CTimerEvent&>(event));
|
2019-04-11 19:03:21 -03:00
|
|
|
case CEvent::ChildAdded:
|
|
|
|
|
case CEvent::ChildRemoved:
|
2019-04-10 11:56:55 -03:00
|
|
|
return child_event(static_cast<CChildEvent&>(event));
|
2019-04-11 19:03:21 -03:00
|
|
|
case CEvent::Invalid:
|
2018-10-10 10:12:38 -03:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
|
break;
|
2019-07-14 05:27:27 -03:00
|
|
|
case CEvent::Custom:
|
|
|
|
|
return custom_event(static_cast<CCustomEvent&>(event));
|
2018-10-10 10:12:38 -03:00
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-10 11:49:36 -03:00
|
|
|
|
2019-04-10 12:01:54 -03:00
|
|
|
void CObject::add_child(CObject& object)
|
2018-10-10 11:49:36 -03:00
|
|
|
{
|
2019-05-04 20:09:49 -03:00
|
|
|
// FIXME: Should we support reparenting objects?
|
|
|
|
|
ASSERT(!object.parent() || object.parent() == this);
|
|
|
|
|
object.m_parent = this;
|
2019-09-21 19:17:53 -03:00
|
|
|
m_children.append(object);
|
2019-04-18 17:57:24 -03:00
|
|
|
event(*make<CChildEvent>(CEvent::ChildAdded, object));
|
2018-10-10 11:49:36 -03:00
|
|
|
}
|
|
|
|
|
|
2019-11-05 16:41:27 -03:00
|
|
|
void CObject::insert_child_before(CObject& new_child, CObject& before_child)
|
|
|
|
|
{
|
|
|
|
|
// FIXME: Should we support reparenting objects?
|
|
|
|
|
ASSERT(!new_child.parent() || new_child.parent() == this);
|
|
|
|
|
new_child.m_parent = this;
|
|
|
|
|
m_children.insert_before_matching(new_child, [&](auto& existing_child) { return existing_child.ptr() == &before_child; });
|
|
|
|
|
event(*make<CChildEvent>(CEvent::ChildAdded, new_child, &before_child));
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-10 12:01:54 -03:00
|
|
|
void CObject::remove_child(CObject& object)
|
2018-10-10 11:49:36 -03:00
|
|
|
{
|
2019-09-21 19:17:53 -03:00
|
|
|
for (int i = 0; i < m_children.size(); ++i) {
|
|
|
|
|
if (m_children.ptr_at(i).ptr() == &object) {
|
|
|
|
|
// NOTE: We protect the child so it survives the handling of ChildRemoved.
|
|
|
|
|
NonnullRefPtr<CObject> protector = object;
|
|
|
|
|
object.m_parent = nullptr;
|
2018-10-12 20:19:25 -03:00
|
|
|
m_children.remove(i);
|
2019-04-18 17:57:24 -03:00
|
|
|
event(*make<CChildEvent>(CEvent::ChildRemoved, object));
|
2018-10-12 20:19:25 -03:00
|
|
|
return;
|
|
|
|
|
}
|
2018-10-10 11:49:36 -03:00
|
|
|
}
|
2019-09-21 19:17:53 -03:00
|
|
|
ASSERT_NOT_REACHED();
|
2018-10-10 11:49:36 -03:00
|
|
|
}
|
2018-10-12 07:18:59 -03:00
|
|
|
|
2019-04-10 12:01:54 -03:00
|
|
|
void CObject::timer_event(CTimerEvent&)
|
2018-10-12 07:18:59 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-10 12:01:54 -03:00
|
|
|
void CObject::child_event(CChildEvent&)
|
2019-03-15 12:12:06 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-14 05:27:27 -03:00
|
|
|
void CObject::custom_event(CCustomEvent&)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-10 12:01:54 -03:00
|
|
|
void CObject::start_timer(int ms)
|
2018-10-12 07:18:59 -03:00
|
|
|
{
|
2019-01-31 14:31:23 -02:00
|
|
|
if (m_timer_id) {
|
2019-04-10 12:01:54 -03:00
|
|
|
dbgprintf("CObject{%p} already has a timer!\n", this);
|
2018-10-12 07:18:59 -03:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
|
}
|
2019-02-01 00:50:06 -02:00
|
|
|
|
2019-04-11 19:03:21 -03:00
|
|
|
m_timer_id = CEventLoop::register_timer(*this, ms, true);
|
2018-10-12 07:18:59 -03:00
|
|
|
}
|
|
|
|
|
|
2019-04-10 12:01:54 -03:00
|
|
|
void CObject::stop_timer()
|
2018-10-12 07:18:59 -03:00
|
|
|
{
|
2019-01-31 14:31:23 -02:00
|
|
|
if (!m_timer_id)
|
2018-10-12 07:18:59 -03:00
|
|
|
return;
|
2019-04-11 19:03:21 -03:00
|
|
|
bool success = CEventLoop::unregister_timer(m_timer_id);
|
2019-02-01 00:50:06 -02:00
|
|
|
ASSERT(success);
|
2019-01-31 14:31:23 -02:00
|
|
|
m_timer_id = 0;
|
2018-10-12 07:18:59 -03:00
|
|
|
}
|
|
|
|
|
|
2019-04-10 12:01:54 -03:00
|
|
|
void CObject::dump_tree(int indent)
|
2019-03-18 20:01:02 -03:00
|
|
|
{
|
|
|
|
|
for (int i = 0; i < indent; ++i) {
|
|
|
|
|
printf(" ");
|
|
|
|
|
}
|
|
|
|
|
printf("%s{%p}\n", class_name(), this);
|
|
|
|
|
|
2019-05-28 06:53:16 -03:00
|
|
|
for_each_child([&](auto& child) {
|
2019-05-26 22:52:19 -03:00
|
|
|
child.dump_tree(indent + 2);
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
2019-04-07 09:36:10 -03:00
|
|
|
}
|
2019-03-18 20:01:02 -03:00
|
|
|
|
2019-04-10 12:01:54 -03:00
|
|
|
void CObject::deferred_invoke(Function<void(CObject&)> invokee)
|
2019-04-07 09:36:10 -03:00
|
|
|
{
|
2019-04-11 19:03:21 -03:00
|
|
|
CEventLoop::current().post_event(*this, make<CDeferredInvocationEvent>(move(invokee)));
|
2019-03-18 20:01:02 -03:00
|
|
|
}
|
2019-08-18 15:36:37 -03:00
|
|
|
|
|
|
|
|
void CObject::save_to(JsonObject& json)
|
|
|
|
|
{
|
|
|
|
|
json.set("class_name", class_name());
|
|
|
|
|
json.set("address", String::format("%p", this));
|
|
|
|
|
json.set("name", name());
|
|
|
|
|
json.set("parent", String::format("%p", parent()));
|
|
|
|
|
}
|
2019-09-20 15:37:31 -03:00
|
|
|
|
|
|
|
|
bool CObject::is_ancestor_of(const CObject& other) const
|
|
|
|
|
{
|
|
|
|
|
if (&other == this)
|
|
|
|
|
return false;
|
|
|
|
|
for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
|
|
|
|
|
if (ancestor == this)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CObject::dispatch_event(CEvent& e, CObject* stay_within)
|
|
|
|
|
{
|
|
|
|
|
ASSERT(!stay_within || stay_within == this || stay_within->is_ancestor_of(*this));
|
|
|
|
|
auto* target = this;
|
|
|
|
|
do {
|
|
|
|
|
target->event(e);
|
|
|
|
|
target = target->parent();
|
|
|
|
|
} while (target && target != stay_within && !e.is_accepted());
|
|
|
|
|
}
|