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