2019-01-20 03:49:48 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-07 12:36:10 +00:00
|
|
|
#include <AK/Function.h>
|
2019-01-20 03:49:48 +00:00
|
|
|
#include <AK/Vector.h>
|
|
|
|
#include <AK/Weakable.h>
|
|
|
|
|
2019-04-10 14:56:55 +00:00
|
|
|
class CEvent;
|
|
|
|
class CChildEvent;
|
|
|
|
class CTimerEvent;
|
2019-01-20 03:49:48 +00:00
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
class CObject : public Weakable<CObject> {
|
2019-01-20 03:49:48 +00:00
|
|
|
public:
|
2019-04-18 20:57:24 +00:00
|
|
|
CObject(CObject* parent = nullptr, bool is_widget = false);
|
2019-04-10 15:01:54 +00:00
|
|
|
virtual ~CObject();
|
2019-01-20 03:49:48 +00:00
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
virtual const char* class_name() const { return "CObject"; }
|
2019-01-20 03:49:48 +00:00
|
|
|
|
2019-04-10 14:56:55 +00:00
|
|
|
virtual void event(CEvent&);
|
2019-01-20 03:49:48 +00:00
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
Vector<CObject*>& children() { return m_children; }
|
2019-04-16 01:47:55 +00:00
|
|
|
const Vector<CObject*>& children() const { return m_children; }
|
2019-01-20 03:49:48 +00:00
|
|
|
|
2019-05-27 01:52:19 +00:00
|
|
|
template<typename Callback>
|
|
|
|
void for_each_child(Callback callback)
|
|
|
|
{
|
|
|
|
for (auto* child : m_children) {
|
|
|
|
if (callback(*child) == IterationDecision::Abort)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
template<typename T, typename Callback>
|
|
|
|
void for_each_child_of_type(Callback callback);
|
2019-05-27 02:18:24 +00:00
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
CObject* parent() { return m_parent; }
|
|
|
|
const CObject* parent() const { return m_parent; }
|
2019-01-20 03:49:48 +00:00
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
void start_timer(int ms);
|
|
|
|
void stop_timer();
|
|
|
|
bool has_timer() const { return m_timer_id; }
|
2019-01-20 03:49:48 +00:00
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
void add_child(CObject&);
|
|
|
|
void remove_child(CObject&);
|
2019-01-20 03:49:48 +00:00
|
|
|
|
2019-01-30 19:03:52 +00:00
|
|
|
void delete_later();
|
2019-01-20 03:49:48 +00:00
|
|
|
|
2019-03-18 23:01:02 +00:00
|
|
|
void dump_tree(int indent = 0);
|
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
void deferred_invoke(Function<void(CObject&)>);
|
2019-04-07 12:36:10 +00:00
|
|
|
|
2019-04-18 20:57:24 +00:00
|
|
|
bool is_widget() const { return m_widget; }
|
2019-03-19 01:20:00 +00:00
|
|
|
virtual bool is_window() const { return false; }
|
2019-03-15 15:12:06 +00:00
|
|
|
|
|
|
|
protected:
|
2019-04-10 14:56:55 +00:00
|
|
|
virtual void timer_event(CTimerEvent&);
|
|
|
|
virtual void child_event(CChildEvent&);
|
2019-01-20 03:49:48 +00:00
|
|
|
|
2019-03-15 15:12:06 +00:00
|
|
|
private:
|
2019-04-10 15:01:54 +00:00
|
|
|
CObject* m_parent { nullptr };
|
2019-01-31 16:31:23 +00:00
|
|
|
int m_timer_id { 0 };
|
2019-04-18 20:57:24 +00:00
|
|
|
bool m_widget { false };
|
2019-04-10 15:01:54 +00:00
|
|
|
Vector<CObject*> m_children;
|
2019-01-20 03:49:48 +00:00
|
|
|
};
|
2019-05-27 02:06:01 +00:00
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
template<typename T>
|
|
|
|
inline bool is(const CObject&) { return true; }
|
2019-05-27 02:06:01 +00:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline T& to(CObject& object)
|
|
|
|
{
|
|
|
|
ASSERT(is<T>(object));
|
|
|
|
return static_cast<T&>(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline const T& to(const CObject& object)
|
|
|
|
{
|
|
|
|
ASSERT(is<T>(object));
|
|
|
|
return static_cast<const T&>(object);
|
|
|
|
}
|
2019-05-27 02:18:24 +00:00
|
|
|
|
|
|
|
template<typename T, typename Callback>
|
|
|
|
inline void CObject::for_each_child_of_type(Callback callback)
|
|
|
|
{
|
2019-05-28 09:53:16 +00:00
|
|
|
for_each_child([&](auto& child) {
|
2019-05-27 02:18:24 +00:00
|
|
|
if (is<T>(child))
|
|
|
|
return callback(to<T>(child));
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
}
|