diff --git a/Applications/IRCClient/IRCClient.h b/Applications/IRCClient/IRCClient.h index 0eacbcf301..91522b729b 100644 --- a/Applications/IRCClient/IRCClient.h +++ b/Applications/IRCClient/IRCClient.h @@ -13,7 +13,7 @@ class IRCQuery; class IRCWindowListModel; class GNotifier; -class IRCClient final : public GObject { +class IRCClient final : public CObject { friend class IRCChannel; friend class IRCQuery; public: diff --git a/LibCore/CEvent.cpp b/LibCore/CEvent.cpp index 5b5d143721..eacce964c2 100644 --- a/LibCore/CEvent.cpp +++ b/LibCore/CEvent.cpp @@ -1,7 +1,7 @@ #include -#include +#include -CChildEvent::CChildEvent(Type type, GObject& child) +CChildEvent::CChildEvent(Type type, CObject& child) : CEvent(type) , m_child(child.make_weak_ptr()) { diff --git a/LibCore/CEvent.h b/LibCore/CEvent.h new file mode 100644 index 0000000000..d9fbb74d70 --- /dev/null +++ b/LibCore/CEvent.h @@ -0,0 +1,69 @@ +#pragma once + +#include +#include +#include +#include + +class CObject; + +class CEvent { +public: + enum Type { + Invalid = 0, + Quit, + Timer, + DeferredDestroy, + DeferredInvoke, + ChildAdded, + ChildRemoved, + }; + + CEvent() { } + explicit CEvent(unsigned type) : m_type(type) { } + virtual ~CEvent() { } + + unsigned type() const { return m_type; } + +private: + unsigned m_type { Type::Invalid }; +}; + +class CDeferredInvocationEvent : public CEvent { + friend class GEventLoop; +public: + CDeferredInvocationEvent(Function invokee) + : CEvent(CEvent::Type::DeferredInvoke) + , m_invokee(move(invokee)) + { + } + +private: + Function m_invokee; +}; + +class CTimerEvent final : public CEvent { +public: + explicit CTimerEvent(int timer_id) + : CEvent(CEvent::Timer), m_timer_id(timer_id) + { + } + ~CTimerEvent() { } + + int timer_id() const { return m_timer_id; } + +private: + int m_timer_id; +}; + +class CChildEvent final : public CEvent { +public: + CChildEvent(Type, CObject& child); + ~CChildEvent(); + + CObject* child() { return m_child.ptr(); } + const CObject* child() const { return m_child.ptr(); } + +private: + WeakPtr m_child; +}; diff --git a/LibGUI/GObject.cpp b/LibCore/CObject.cpp similarity index 75% rename from LibGUI/GObject.cpp rename to LibCore/CObject.cpp index eda781bfbd..59ecfea62a 100644 --- a/LibGUI/GObject.cpp +++ b/LibCore/CObject.cpp @@ -1,17 +1,17 @@ -#include "GObject.h" -#include "GEvent.h" -#include "GEventLoop.h" +#include +#include +#include #include #include -GObject::GObject(GObject* parent) +CObject::CObject(CObject* parent) : m_parent(parent) { if (m_parent) m_parent->add_child(*this); } -GObject::~GObject() +CObject::~CObject() { stop_timer(); if (m_parent) @@ -21,7 +21,7 @@ GObject::~GObject() delete child; } -void GObject::event(CEvent& event) +void CObject::event(CEvent& event) { switch (event.type()) { case GEvent::Timer: @@ -40,13 +40,13 @@ void GObject::event(CEvent& event) } } -void GObject::add_child(GObject& object) +void CObject::add_child(CObject& object) { m_children.append(&object); GEventLoop::current().post_event(*this, make(GEvent::ChildAdded, object)); } -void GObject::remove_child(GObject& object) +void CObject::remove_child(CObject& object) { for (ssize_t i = 0; i < m_children.size(); ++i) { if (m_children[i] == &object) { @@ -57,25 +57,25 @@ void GObject::remove_child(GObject& object) } } -void GObject::timer_event(CTimerEvent&) +void CObject::timer_event(CTimerEvent&) { } -void GObject::child_event(CChildEvent&) +void CObject::child_event(CChildEvent&) { } -void GObject::start_timer(int ms) +void CObject::start_timer(int ms) { if (m_timer_id) { - dbgprintf("GObject{%p} already has a timer!\n", this); + dbgprintf("CObject{%p} already has a timer!\n", this); ASSERT_NOT_REACHED(); } m_timer_id = GEventLoop::register_timer(*this, ms, true); } -void GObject::stop_timer() +void CObject::stop_timer() { if (!m_timer_id) return; @@ -84,12 +84,12 @@ void GObject::stop_timer() m_timer_id = 0; } -void GObject::delete_later() +void CObject::delete_later() { GEventLoop::current().post_event(*this, make(CEvent::DeferredDestroy)); } -void GObject::dump_tree(int indent) +void CObject::dump_tree(int indent) { for (int i = 0; i < indent; ++i) { printf(" "); @@ -101,7 +101,7 @@ void GObject::dump_tree(int indent) } } -void GObject::deferred_invoke(Function invokee) +void CObject::deferred_invoke(Function invokee) { GEventLoop::current().post_event(*this, make(move(invokee))); } diff --git a/LibGUI/GObject.h b/LibCore/CObject.h similarity index 54% rename from LibGUI/GObject.h rename to LibCore/CObject.h index 428a70ab82..c94ed2f167 100644 --- a/LibGUI/GObject.h +++ b/LibCore/CObject.h @@ -8,32 +8,32 @@ class CEvent; class CChildEvent; class CTimerEvent; -class GObject : public Weakable { +class CObject : public Weakable { public: - GObject(GObject* parent = nullptr); - virtual ~GObject(); + CObject(CObject* parent = nullptr); + virtual ~CObject(); - virtual const char* class_name() const { return "GObject"; } + virtual const char* class_name() const { return "CObject"; } virtual void event(CEvent&); - Vector& children() { return m_children; } + Vector& children() { return m_children; } - GObject* parent() { return m_parent; } - const GObject* parent() const { return m_parent; } + CObject* parent() { return m_parent; } + const CObject* parent() const { return m_parent; } void start_timer(int ms); void stop_timer(); bool has_timer() const { return m_timer_id; } - void add_child(GObject&); - void remove_child(GObject&); + void add_child(CObject&); + void remove_child(CObject&); void delete_later(); void dump_tree(int indent = 0); - void deferred_invoke(Function); + void deferred_invoke(Function); virtual bool is_widget() const { return false; } virtual bool is_window() const { return false; } @@ -43,7 +43,7 @@ protected: virtual void child_event(CChildEvent&); private: - GObject* m_parent { nullptr }; + CObject* m_parent { nullptr }; int m_timer_id { 0 }; - Vector m_children; + Vector m_children; }; diff --git a/LibCore/Makefile b/LibCore/Makefile index 7815597924..1a7d41a678 100644 --- a/LibCore/Makefile +++ b/LibCore/Makefile @@ -1,5 +1,6 @@ OBJS = \ CElapsedTimer.o \ + CObject.o \ CEvent.o LIBRARY = libcore.a diff --git a/LibGUI/GApplication.cpp b/LibGUI/GApplication.cpp index 6c9f759998..bdf68cf692 100644 --- a/LibGUI/GApplication.cpp +++ b/LibGUI/GApplication.cpp @@ -33,7 +33,7 @@ int GApplication::exec() { int exit_code = m_event_loop->exec(); // NOTE: Maybe it would be cool to return instead of exit()? - // This would require cleaning up all the GObjects on the heap. + // This would require cleaning up all the CObjects on the heap. exit(exit_code); return exit_code; } diff --git a/LibGUI/GDialog.cpp b/LibGUI/GDialog.cpp index 3832d2b32c..78f7df2184 100644 --- a/LibGUI/GDialog.cpp +++ b/LibGUI/GDialog.cpp @@ -1,7 +1,7 @@ #include #include -GDialog::GDialog(GObject* parent) +GDialog::GDialog(CObject* parent) : GWindow(parent) { set_modal(true); diff --git a/LibGUI/GDialog.h b/LibGUI/GDialog.h index e6cefbce98..85227797f2 100644 --- a/LibGUI/GDialog.h +++ b/LibGUI/GDialog.h @@ -15,7 +15,7 @@ public: void done(int result); protected: - explicit GDialog(GObject* parent); + explicit GDialog(CObject* parent); private: OwnPtr m_event_loop; diff --git a/LibGUI/GEvent.h b/LibGUI/GEvent.h index df386c113f..d9413b2f5d 100644 --- a/LibGUI/GEvent.h +++ b/LibGUI/GEvent.h @@ -6,7 +6,7 @@ #include #include -class GObject; +class CObject; class GEvent : public CEvent{ public: diff --git a/LibGUI/GEventLoop.cpp b/LibGUI/GEventLoop.cpp index da031ceb18..dd234b6397 100644 --- a/LibGUI/GEventLoop.cpp +++ b/LibGUI/GEventLoop.cpp @@ -1,6 +1,6 @@ +#include #include "GEventLoop.h" #include "GEvent.h" -#include "GObject.h" #include "GWindow.h" #include #include @@ -177,7 +177,7 @@ int GEventLoop::exec() ASSERT_NOT_REACHED(); } -void GEventLoop::post_event(GObject& receiver, OwnPtr&& event) +void GEventLoop::post_event(CObject& receiver, OwnPtr&& event) { #ifdef GEVENTLOOP_DEBUG dbgprintf("GEventLoop::post_event: {%u} << receiver=%p, event=%p\n", m_queued_events.size(), &receiver, event.ptr()); @@ -517,7 +517,7 @@ void GEventLoop::get_next_timer_expiration(timeval& soonest) } } -int GEventLoop::register_timer(GObject& object, int milliseconds, bool should_reload) +int GEventLoop::register_timer(CObject& object, int milliseconds, bool should_reload) { ASSERT(milliseconds >= 0); auto timer = make(); diff --git a/LibGUI/GEventLoop.h b/LibGUI/GEventLoop.h index a1ebcde5f5..e26bc7ff5b 100644 --- a/LibGUI/GEventLoop.h +++ b/LibGUI/GEventLoop.h @@ -9,7 +9,7 @@ #include class GAction; -class GObject; +class CObject; class GNotifier; class GWindow; @@ -20,14 +20,14 @@ public: int exec(); - void post_event(GObject& receiver, OwnPtr&&); + void post_event(CObject& receiver, OwnPtr&&); static GEventLoop& main(); static GEventLoop& current(); bool running() const { return m_running; } - static int register_timer(GObject&, int milliseconds, bool should_reload); + static int register_timer(CObject&, int milliseconds, bool should_reload); static bool unregister_timer(int timer_id); static void register_notifier(Badge, GNotifier&); @@ -65,7 +65,7 @@ private: void connect_to_server(); struct QueuedEvent { - WeakPtr receiver; + WeakPtr receiver; OwnPtr event; }; Vector m_queued_events; @@ -84,7 +84,7 @@ private: int interval { 0 }; timeval fire_time; bool should_reload { false }; - WeakPtr owner; + WeakPtr owner; void reload(); bool has_expired() const; diff --git a/LibGUI/GIODevice.cpp b/LibGUI/GIODevice.cpp index 863c0e780d..5766de65b9 100644 --- a/LibGUI/GIODevice.cpp +++ b/LibGUI/GIODevice.cpp @@ -3,8 +3,8 @@ #include #include -GIODevice::GIODevice(GObject* parent) - : GObject(parent) +GIODevice::GIODevice(CObject* parent) + : CObject(parent) { } diff --git a/LibGUI/GIODevice.h b/LibGUI/GIODevice.h index d2aca72b06..4b2150ef01 100644 --- a/LibGUI/GIODevice.h +++ b/LibGUI/GIODevice.h @@ -1,9 +1,9 @@ #pragma once -#include +#include #include -class GIODevice : public GObject { +class GIODevice : public CObject { public: enum OpenMode { NotOpen = 0, @@ -41,7 +41,7 @@ public: virtual const char* class_name() const override { return "GIODevice"; } protected: - explicit GIODevice(GObject* parent = nullptr); + explicit GIODevice(CObject* parent = nullptr); void set_fd(int fd) { m_fd = fd; } void set_mode(OpenMode mode) { m_mode = mode; } diff --git a/LibGUI/GInputBox.cpp b/LibGUI/GInputBox.cpp index 3950f7abb8..cc08772f1b 100644 --- a/LibGUI/GInputBox.cpp +++ b/LibGUI/GInputBox.cpp @@ -5,7 +5,7 @@ #include #include -GInputBox::GInputBox(const String& prompt, const String& title, GObject* parent) +GInputBox::GInputBox(const String& prompt, const String& title, CObject* parent) : GDialog(parent) , m_prompt(prompt) { diff --git a/LibGUI/GInputBox.h b/LibGUI/GInputBox.h index 1885adaa8e..e5b70c1591 100644 --- a/LibGUI/GInputBox.h +++ b/LibGUI/GInputBox.h @@ -7,7 +7,7 @@ class GTextEditor; class GInputBox : public GDialog { public: - explicit GInputBox(const String& prompt, const String& title, GObject* parent = nullptr); + explicit GInputBox(const String& prompt, const String& title, CObject* parent = nullptr); virtual ~GInputBox() override; String text_value() const { return m_text_value; } diff --git a/LibGUI/GMessageBox.cpp b/LibGUI/GMessageBox.cpp index 74997d0980..dddc8414c6 100644 --- a/LibGUI/GMessageBox.cpp +++ b/LibGUI/GMessageBox.cpp @@ -3,7 +3,7 @@ #include #include -GMessageBox::GMessageBox(const String& text, const String& title, GObject* parent) +GMessageBox::GMessageBox(const String& text, const String& title, CObject* parent) : GDialog(parent) , m_text(text) { diff --git a/LibGUI/GMessageBox.h b/LibGUI/GMessageBox.h index f0b7a6d84b..3e5acff024 100644 --- a/LibGUI/GMessageBox.h +++ b/LibGUI/GMessageBox.h @@ -4,7 +4,7 @@ class GMessageBox : public GDialog { public: - explicit GMessageBox(const String& text, const String& title, GObject* parent = nullptr); + explicit GMessageBox(const String& text, const String& title, CObject* parent = nullptr); virtual ~GMessageBox() override; private: diff --git a/LibGUI/GNetworkJob.h b/LibGUI/GNetworkJob.h index fcabcd34be..fd87244cd2 100644 --- a/LibGUI/GNetworkJob.h +++ b/LibGUI/GNetworkJob.h @@ -1,11 +1,11 @@ #pragma once -#include +#include #include class GNetworkResponse; -class GNetworkJob : public GObject { +class GNetworkJob : public CObject { public: enum class Error { None, diff --git a/LibGUI/GSocket.cpp b/LibGUI/GSocket.cpp index 1b8cee1fec..b1a1f4797d 100644 --- a/LibGUI/GSocket.cpp +++ b/LibGUI/GSocket.cpp @@ -8,7 +8,7 @@ #include #include -GSocket::GSocket(Type type, GObject* parent) +GSocket::GSocket(Type type, CObject* parent) : GIODevice(parent) , m_type(type) { diff --git a/LibGUI/GSocket.h b/LibGUI/GSocket.h index a6a0d7c3e9..f9501eab3f 100644 --- a/LibGUI/GSocket.h +++ b/LibGUI/GSocket.h @@ -29,7 +29,7 @@ public: virtual const char* class_name() const override { return "GSocket"; } protected: - GSocket(Type, GObject* parent); + GSocket(Type, CObject* parent); GSocketAddress m_source_address; GSocketAddress m_destination_address; diff --git a/LibGUI/GTCPSocket.cpp b/LibGUI/GTCPSocket.cpp index ad53027674..22fa01f84c 100644 --- a/LibGUI/GTCPSocket.cpp +++ b/LibGUI/GTCPSocket.cpp @@ -1,7 +1,7 @@ #include #include -GTCPSocket::GTCPSocket(GObject* parent) +GTCPSocket::GTCPSocket(CObject* parent) : GSocket(GSocket::Type::TCP, parent) { int fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); diff --git a/LibGUI/GTCPSocket.h b/LibGUI/GTCPSocket.h index 691da1731e..676e8def35 100644 --- a/LibGUI/GTCPSocket.h +++ b/LibGUI/GTCPSocket.h @@ -2,7 +2,7 @@ class GTCPSocket final : public GSocket { public: - explicit GTCPSocket(GObject* parent); + explicit GTCPSocket(CObject* parent); virtual ~GTCPSocket() override; private: diff --git a/LibGUI/GTimer.cpp b/LibGUI/GTimer.cpp index dc61314f1c..18c092f1b0 100644 --- a/LibGUI/GTimer.cpp +++ b/LibGUI/GTimer.cpp @@ -1,7 +1,7 @@ #include -GTimer::GTimer(GObject* parent) - : GObject(parent) +GTimer::GTimer(CObject* parent) + : CObject(parent) { } diff --git a/LibGUI/GTimer.h b/LibGUI/GTimer.h index 1aa37b4f58..68707748c8 100644 --- a/LibGUI/GTimer.h +++ b/LibGUI/GTimer.h @@ -1,11 +1,11 @@ #pragma once -#include +#include #include -class GTimer final : public GObject { +class GTimer final : public CObject { public: - explicit GTimer(GObject* parent = nullptr); + explicit GTimer(CObject* parent = nullptr); virtual ~GTimer() override; void start(); diff --git a/LibGUI/GWidget.cpp b/LibGUI/GWidget.cpp index 0d92e52580..c52ca48acf 100644 --- a/LibGUI/GWidget.cpp +++ b/LibGUI/GWidget.cpp @@ -11,7 +11,7 @@ #include GWidget::GWidget(GWidget* parent) - : GObject(parent) + : CObject(parent) { set_font(nullptr); m_background_color = Color::LightGray; @@ -36,7 +36,7 @@ void GWidget::child_event(CChildEvent& event) invalidate_layout(); } } - return GObject::child_event(event); + return CObject::child_event(event); } void GWidget::set_relative_rect(const Rect& rect) @@ -84,7 +84,7 @@ void GWidget::event(CEvent& event) case GEvent::Leave: return handle_leave_event(event); default: - return GObject::event(event); + return CObject::event(event); } } diff --git a/LibGUI/GWidget.h b/LibGUI/GWidget.h index 2b6c652f25..0d322e4ab0 100644 --- a/LibGUI/GWidget.h +++ b/LibGUI/GWidget.h @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include #include @@ -18,7 +18,7 @@ enum class Orientation { Horizontal, Vertical }; enum class HorizontalDirection { Left, Right }; enum class VerticalDirection { Up, Down }; -class GWidget : public GObject { +class GWidget : public CObject { public: explicit GWidget(GWidget* parent = nullptr); virtual ~GWidget() override; diff --git a/LibGUI/GWindow.cpp b/LibGUI/GWindow.cpp index 49da256a8d..204e4c09fc 100644 --- a/LibGUI/GWindow.cpp +++ b/LibGUI/GWindow.cpp @@ -28,8 +28,8 @@ GWindow* GWindow::from_window_id(int window_id) return nullptr; } -GWindow::GWindow(GObject* parent) - : GObject(parent) +GWindow::GWindow(CObject* parent) + : CObject(parent) { m_rect_when_windowless = { 100, 400, 140, 140 }; m_title_when_windowless = "GWindow"; @@ -280,7 +280,7 @@ void GWindow::event(CEvent& event) if (event.type() == GEvent::WM_WindowRemoved || event.type() == GEvent::WM_WindowStateChanged) return wm_event(static_cast(event)); - GObject::event(event); + CObject::event(event); } bool GWindow::is_visible() const diff --git a/LibGUI/GWindow.h b/LibGUI/GWindow.h index f36a97146d..eb9d9a384c 100644 --- a/LibGUI/GWindow.h +++ b/LibGUI/GWindow.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include #include @@ -18,9 +18,9 @@ enum class GStandardCursor { ResizeVertical, }; -class GWindow : public GObject { +class GWindow : public CObject { public: - GWindow(GObject* parent = nullptr); + GWindow(CObject* parent = nullptr); virtual ~GWindow() override; static GWindow* from_window_id(int); diff --git a/LibGUI/Makefile b/LibGUI/Makefile index e13c965040..37b21c6e62 100644 --- a/LibGUI/Makefile +++ b/LibGUI/Makefile @@ -17,7 +17,6 @@ LIBGUI_OBJS = \ GEventLoop.o \ GLabel.o \ GListBox.o \ - GObject.o \ GNotifier.o \ GTextBox.o \ GScrollBar.o \