1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:18:11 +00:00

LibCore: Move LibGUI/GObject to LibCore/CObject.

This commit is contained in:
Andreas Kling 2019-04-10 17:01:54 +02:00
parent b8062f69d8
commit 2f1f51b8ab
30 changed files with 144 additions and 75 deletions

View file

@ -13,7 +13,7 @@ class IRCQuery;
class IRCWindowListModel; class IRCWindowListModel;
class GNotifier; class GNotifier;
class IRCClient final : public GObject { class IRCClient final : public CObject {
friend class IRCChannel; friend class IRCChannel;
friend class IRCQuery; friend class IRCQuery;
public: public:

View file

@ -1,7 +1,7 @@
#include <LibCore/CEvent.h> #include <LibCore/CEvent.h>
#include <LibGUI/GObject.h> #include <LibCore/CObject.h>
CChildEvent::CChildEvent(Type type, GObject& child) CChildEvent::CChildEvent(Type type, CObject& child)
: CEvent(type) : CEvent(type)
, m_child(child.make_weak_ptr()) , m_child(child.make_weak_ptr())
{ {

69
LibCore/CEvent.h Normal file
View file

@ -0,0 +1,69 @@
#pragma once
#include <AK/AKString.h>
#include <AK/Types.h>
#include <AK/WeakPtr.h>
#include <AK/Function.h>
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<void(CObject&)> invokee)
: CEvent(CEvent::Type::DeferredInvoke)
, m_invokee(move(invokee))
{
}
private:
Function<void(CObject&)> 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<CObject> m_child;
};

View file

@ -1,17 +1,17 @@
#include "GObject.h" #include <LibCore/CObject.h>
#include "GEvent.h" #include <LibCore/CEvent.h>
#include "GEventLoop.h" #include <LibGUI/GEventLoop.h>
#include <AK/Assertions.h> #include <AK/Assertions.h>
#include <stdio.h> #include <stdio.h>
GObject::GObject(GObject* parent) CObject::CObject(CObject* parent)
: m_parent(parent) : m_parent(parent)
{ {
if (m_parent) if (m_parent)
m_parent->add_child(*this); m_parent->add_child(*this);
} }
GObject::~GObject() CObject::~CObject()
{ {
stop_timer(); stop_timer();
if (m_parent) if (m_parent)
@ -21,7 +21,7 @@ GObject::~GObject()
delete child; delete child;
} }
void GObject::event(CEvent& event) void CObject::event(CEvent& event)
{ {
switch (event.type()) { switch (event.type()) {
case GEvent::Timer: 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); m_children.append(&object);
GEventLoop::current().post_event(*this, make<CChildEvent>(GEvent::ChildAdded, object)); GEventLoop::current().post_event(*this, make<CChildEvent>(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) { for (ssize_t i = 0; i < m_children.size(); ++i) {
if (m_children[i] == &object) { 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) { 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(); ASSERT_NOT_REACHED();
} }
m_timer_id = GEventLoop::register_timer(*this, ms, true); m_timer_id = GEventLoop::register_timer(*this, ms, true);
} }
void GObject::stop_timer() void CObject::stop_timer()
{ {
if (!m_timer_id) if (!m_timer_id)
return; return;
@ -84,12 +84,12 @@ void GObject::stop_timer()
m_timer_id = 0; m_timer_id = 0;
} }
void GObject::delete_later() void CObject::delete_later()
{ {
GEventLoop::current().post_event(*this, make<CEvent>(CEvent::DeferredDestroy)); GEventLoop::current().post_event(*this, make<CEvent>(CEvent::DeferredDestroy));
} }
void GObject::dump_tree(int indent) void CObject::dump_tree(int indent)
{ {
for (int i = 0; i < indent; ++i) { for (int i = 0; i < indent; ++i) {
printf(" "); printf(" ");
@ -101,7 +101,7 @@ void GObject::dump_tree(int indent)
} }
} }
void GObject::deferred_invoke(Function<void(GObject&)> invokee) void CObject::deferred_invoke(Function<void(CObject&)> invokee)
{ {
GEventLoop::current().post_event(*this, make<CDeferredInvocationEvent>(move(invokee))); GEventLoop::current().post_event(*this, make<CDeferredInvocationEvent>(move(invokee)));
} }

View file

@ -8,32 +8,32 @@ class CEvent;
class CChildEvent; class CChildEvent;
class CTimerEvent; class CTimerEvent;
class GObject : public Weakable<GObject> { class CObject : public Weakable<CObject> {
public: public:
GObject(GObject* parent = nullptr); CObject(CObject* parent = nullptr);
virtual ~GObject(); virtual ~CObject();
virtual const char* class_name() const { return "GObject"; } virtual const char* class_name() const { return "CObject"; }
virtual void event(CEvent&); virtual void event(CEvent&);
Vector<GObject*>& children() { return m_children; } Vector<CObject*>& children() { return m_children; }
GObject* parent() { return m_parent; } CObject* parent() { return m_parent; }
const GObject* parent() const { return m_parent; } const CObject* parent() const { return m_parent; }
void start_timer(int ms); void start_timer(int ms);
void stop_timer(); void stop_timer();
bool has_timer() const { return m_timer_id; } bool has_timer() const { return m_timer_id; }
void add_child(GObject&); void add_child(CObject&);
void remove_child(GObject&); void remove_child(CObject&);
void delete_later(); void delete_later();
void dump_tree(int indent = 0); void dump_tree(int indent = 0);
void deferred_invoke(Function<void(GObject&)>); void deferred_invoke(Function<void(CObject&)>);
virtual bool is_widget() const { return false; } virtual bool is_widget() const { return false; }
virtual bool is_window() const { return false; } virtual bool is_window() const { return false; }
@ -43,7 +43,7 @@ protected:
virtual void child_event(CChildEvent&); virtual void child_event(CChildEvent&);
private: private:
GObject* m_parent { nullptr }; CObject* m_parent { nullptr };
int m_timer_id { 0 }; int m_timer_id { 0 };
Vector<GObject*> m_children; Vector<CObject*> m_children;
}; };

View file

@ -1,5 +1,6 @@
OBJS = \ OBJS = \
CElapsedTimer.o \ CElapsedTimer.o \
CObject.o \
CEvent.o CEvent.o
LIBRARY = libcore.a LIBRARY = libcore.a

View file

@ -33,7 +33,7 @@ int GApplication::exec()
{ {
int exit_code = m_event_loop->exec(); int exit_code = m_event_loop->exec();
// NOTE: Maybe it would be cool to return instead of exit()? // 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); exit(exit_code);
return exit_code; return exit_code;
} }

View file

@ -1,7 +1,7 @@
#include <LibGUI/GDialog.h> #include <LibGUI/GDialog.h>
#include <LibGUI/GEventLoop.h> #include <LibGUI/GEventLoop.h>
GDialog::GDialog(GObject* parent) GDialog::GDialog(CObject* parent)
: GWindow(parent) : GWindow(parent)
{ {
set_modal(true); set_modal(true);

View file

@ -15,7 +15,7 @@ public:
void done(int result); void done(int result);
protected: protected:
explicit GDialog(GObject* parent); explicit GDialog(CObject* parent);
private: private:
OwnPtr<GEventLoop> m_event_loop; OwnPtr<GEventLoop> m_event_loop;

View file

@ -6,7 +6,7 @@
#include <Kernel/KeyCode.h> #include <Kernel/KeyCode.h>
#include <LibGUI/GWindowType.h> #include <LibGUI/GWindowType.h>
class GObject; class CObject;
class GEvent : public CEvent{ class GEvent : public CEvent{
public: public:

View file

@ -1,6 +1,6 @@
#include <LibCore/CObject.h>
#include "GEventLoop.h" #include "GEventLoop.h"
#include "GEvent.h" #include "GEvent.h"
#include "GObject.h"
#include "GWindow.h" #include "GWindow.h"
#include <LibGUI/GApplication.h> #include <LibGUI/GApplication.h>
#include <LibGUI/GAction.h> #include <LibGUI/GAction.h>
@ -177,7 +177,7 @@ int GEventLoop::exec()
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
void GEventLoop::post_event(GObject& receiver, OwnPtr<CEvent>&& event) void GEventLoop::post_event(CObject& receiver, OwnPtr<CEvent>&& event)
{ {
#ifdef GEVENTLOOP_DEBUG #ifdef GEVENTLOOP_DEBUG
dbgprintf("GEventLoop::post_event: {%u} << receiver=%p, event=%p\n", m_queued_events.size(), &receiver, event.ptr()); 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); ASSERT(milliseconds >= 0);
auto timer = make<EventLoopTimer>(); auto timer = make<EventLoopTimer>();

View file

@ -9,7 +9,7 @@
#include <LibGUI/GEvent.h> #include <LibGUI/GEvent.h>
class GAction; class GAction;
class GObject; class CObject;
class GNotifier; class GNotifier;
class GWindow; class GWindow;
@ -20,14 +20,14 @@ public:
int exec(); int exec();
void post_event(GObject& receiver, OwnPtr<CEvent>&&); void post_event(CObject& receiver, OwnPtr<CEvent>&&);
static GEventLoop& main(); static GEventLoop& main();
static GEventLoop& current(); static GEventLoop& current();
bool running() const { return m_running; } 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 bool unregister_timer(int timer_id);
static void register_notifier(Badge<GNotifier>, GNotifier&); static void register_notifier(Badge<GNotifier>, GNotifier&);
@ -65,7 +65,7 @@ private:
void connect_to_server(); void connect_to_server();
struct QueuedEvent { struct QueuedEvent {
WeakPtr<GObject> receiver; WeakPtr<CObject> receiver;
OwnPtr<CEvent> event; OwnPtr<CEvent> event;
}; };
Vector<QueuedEvent> m_queued_events; Vector<QueuedEvent> m_queued_events;
@ -84,7 +84,7 @@ private:
int interval { 0 }; int interval { 0 };
timeval fire_time; timeval fire_time;
bool should_reload { false }; bool should_reload { false };
WeakPtr<GObject> owner; WeakPtr<CObject> owner;
void reload(); void reload();
bool has_expired() const; bool has_expired() const;

View file

@ -3,8 +3,8 @@
#include <sys/select.h> #include <sys/select.h>
#include <stdio.h> #include <stdio.h>
GIODevice::GIODevice(GObject* parent) GIODevice::GIODevice(CObject* parent)
: GObject(parent) : CObject(parent)
{ {
} }

View file

@ -1,9 +1,9 @@
#pragma once #pragma once
#include <LibGUI/GObject.h> #include <LibCore/CObject.h>
#include <AK/ByteBuffer.h> #include <AK/ByteBuffer.h>
class GIODevice : public GObject { class GIODevice : public CObject {
public: public:
enum OpenMode { enum OpenMode {
NotOpen = 0, NotOpen = 0,
@ -41,7 +41,7 @@ public:
virtual const char* class_name() const override { return "GIODevice"; } virtual const char* class_name() const override { return "GIODevice"; }
protected: protected:
explicit GIODevice(GObject* parent = nullptr); explicit GIODevice(CObject* parent = nullptr);
void set_fd(int fd) { m_fd = fd; } void set_fd(int fd) { m_fd = fd; }
void set_mode(OpenMode mode) { m_mode = mode; } void set_mode(OpenMode mode) { m_mode = mode; }

View file

@ -5,7 +5,7 @@
#include <LibGUI/GTextEditor.h> #include <LibGUI/GTextEditor.h>
#include <stdio.h> #include <stdio.h>
GInputBox::GInputBox(const String& prompt, const String& title, GObject* parent) GInputBox::GInputBox(const String& prompt, const String& title, CObject* parent)
: GDialog(parent) : GDialog(parent)
, m_prompt(prompt) , m_prompt(prompt)
{ {

View file

@ -7,7 +7,7 @@ class GTextEditor;
class GInputBox : public GDialog { class GInputBox : public GDialog {
public: 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; virtual ~GInputBox() override;
String text_value() const { return m_text_value; } String text_value() const { return m_text_value; }

View file

@ -3,7 +3,7 @@
#include <LibGUI/GLabel.h> #include <LibGUI/GLabel.h>
#include <LibGUI/GButton.h> #include <LibGUI/GButton.h>
GMessageBox::GMessageBox(const String& text, const String& title, GObject* parent) GMessageBox::GMessageBox(const String& text, const String& title, CObject* parent)
: GDialog(parent) : GDialog(parent)
, m_text(text) , m_text(text)
{ {

View file

@ -4,7 +4,7 @@
class GMessageBox : public GDialog { class GMessageBox : public GDialog {
public: 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; virtual ~GMessageBox() override;
private: private:

View file

@ -1,11 +1,11 @@
#pragma once #pragma once
#include <LibGUI/GObject.h> #include <LibCore/CObject.h>
#include <AK/Function.h> #include <AK/Function.h>
class GNetworkResponse; class GNetworkResponse;
class GNetworkJob : public GObject { class GNetworkJob : public CObject {
public: public:
enum class Error { enum class Error {
None, None,

View file

@ -8,7 +8,7 @@
#include <netdb.h> #include <netdb.h>
#include <errno.h> #include <errno.h>
GSocket::GSocket(Type type, GObject* parent) GSocket::GSocket(Type type, CObject* parent)
: GIODevice(parent) : GIODevice(parent)
, m_type(type) , m_type(type)
{ {

View file

@ -29,7 +29,7 @@ public:
virtual const char* class_name() const override { return "GSocket"; } virtual const char* class_name() const override { return "GSocket"; }
protected: protected:
GSocket(Type, GObject* parent); GSocket(Type, CObject* parent);
GSocketAddress m_source_address; GSocketAddress m_source_address;
GSocketAddress m_destination_address; GSocketAddress m_destination_address;

View file

@ -1,7 +1,7 @@
#include <LibGUI/GTCPSocket.h> #include <LibGUI/GTCPSocket.h>
#include <sys/socket.h> #include <sys/socket.h>
GTCPSocket::GTCPSocket(GObject* parent) GTCPSocket::GTCPSocket(CObject* parent)
: GSocket(GSocket::Type::TCP, parent) : GSocket(GSocket::Type::TCP, parent)
{ {
int fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); int fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);

View file

@ -2,7 +2,7 @@
class GTCPSocket final : public GSocket { class GTCPSocket final : public GSocket {
public: public:
explicit GTCPSocket(GObject* parent); explicit GTCPSocket(CObject* parent);
virtual ~GTCPSocket() override; virtual ~GTCPSocket() override;
private: private:

View file

@ -1,7 +1,7 @@
#include <LibGUI/GTimer.h> #include <LibGUI/GTimer.h>
GTimer::GTimer(GObject* parent) GTimer::GTimer(CObject* parent)
: GObject(parent) : CObject(parent)
{ {
} }

View file

@ -1,11 +1,11 @@
#pragma once #pragma once
#include <LibGUI/GObject.h> #include <LibCore/CObject.h>
#include <AK/Function.h> #include <AK/Function.h>
class GTimer final : public GObject { class GTimer final : public CObject {
public: public:
explicit GTimer(GObject* parent = nullptr); explicit GTimer(CObject* parent = nullptr);
virtual ~GTimer() override; virtual ~GTimer() override;
void start(); void start();

View file

@ -11,7 +11,7 @@
#include <unistd.h> #include <unistd.h>
GWidget::GWidget(GWidget* parent) GWidget::GWidget(GWidget* parent)
: GObject(parent) : CObject(parent)
{ {
set_font(nullptr); set_font(nullptr);
m_background_color = Color::LightGray; m_background_color = Color::LightGray;
@ -36,7 +36,7 @@ void GWidget::child_event(CChildEvent& event)
invalidate_layout(); invalidate_layout();
} }
} }
return GObject::child_event(event); return CObject::child_event(event);
} }
void GWidget::set_relative_rect(const Rect& rect) void GWidget::set_relative_rect(const Rect& rect)
@ -84,7 +84,7 @@ void GWidget::event(CEvent& event)
case GEvent::Leave: case GEvent::Leave:
return handle_leave_event(event); return handle_leave_event(event);
default: default:
return GObject::event(event); return CObject::event(event);
} }
} }

View file

@ -2,7 +2,7 @@
#include <LibCore/CElapsedTimer.h> #include <LibCore/CElapsedTimer.h>
#include <LibGUI/GEvent.h> #include <LibGUI/GEvent.h>
#include <LibGUI/GObject.h> #include <LibCore/CObject.h>
#include <SharedGraphics/Rect.h> #include <SharedGraphics/Rect.h>
#include <SharedGraphics/Color.h> #include <SharedGraphics/Color.h>
#include <SharedGraphics/Font.h> #include <SharedGraphics/Font.h>
@ -18,7 +18,7 @@ enum class Orientation { Horizontal, Vertical };
enum class HorizontalDirection { Left, Right }; enum class HorizontalDirection { Left, Right };
enum class VerticalDirection { Up, Down }; enum class VerticalDirection { Up, Down };
class GWidget : public GObject { class GWidget : public CObject {
public: public:
explicit GWidget(GWidget* parent = nullptr); explicit GWidget(GWidget* parent = nullptr);
virtual ~GWidget() override; virtual ~GWidget() override;

View file

@ -28,8 +28,8 @@ GWindow* GWindow::from_window_id(int window_id)
return nullptr; return nullptr;
} }
GWindow::GWindow(GObject* parent) GWindow::GWindow(CObject* parent)
: GObject(parent) : CObject(parent)
{ {
m_rect_when_windowless = { 100, 400, 140, 140 }; m_rect_when_windowless = { 100, 400, 140, 140 };
m_title_when_windowless = "GWindow"; 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) if (event.type() == GEvent::WM_WindowRemoved || event.type() == GEvent::WM_WindowStateChanged)
return wm_event(static_cast<GWMEvent&>(event)); return wm_event(static_cast<GWMEvent&>(event));
GObject::event(event); CObject::event(event);
} }
bool GWindow::is_visible() const bool GWindow::is_visible() const

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <LibGUI/GObject.h> #include <LibCore/CObject.h>
#include <LibGUI/GWindowType.h> #include <LibGUI/GWindowType.h>
#include <SharedGraphics/Rect.h> #include <SharedGraphics/Rect.h>
#include <SharedGraphics/GraphicsBitmap.h> #include <SharedGraphics/GraphicsBitmap.h>
@ -18,9 +18,9 @@ enum class GStandardCursor {
ResizeVertical, ResizeVertical,
}; };
class GWindow : public GObject { class GWindow : public CObject {
public: public:
GWindow(GObject* parent = nullptr); GWindow(CObject* parent = nullptr);
virtual ~GWindow() override; virtual ~GWindow() override;
static GWindow* from_window_id(int); static GWindow* from_window_id(int);

View file

@ -17,7 +17,6 @@ LIBGUI_OBJS = \
GEventLoop.o \ GEventLoop.o \
GLabel.o \ GLabel.o \
GListBox.o \ GListBox.o \
GObject.o \
GNotifier.o \ GNotifier.o \
GTextBox.o \ GTextBox.o \
GScrollBar.o \ GScrollBar.o \