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
Notes: sideshowbarker 2024-07-19 14:46:14 +09:00
30 changed files with 144 additions and 75 deletions

View file

@ -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:

View file

@ -1,7 +1,7 @@
#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)
, 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 "GEvent.h"
#include "GEventLoop.h"
#include <LibCore/CObject.h>
#include <LibCore/CEvent.h>
#include <LibGUI/GEventLoop.h>
#include <AK/Assertions.h>
#include <stdio.h>
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<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) {
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>(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<void(GObject&)> invokee)
void CObject::deferred_invoke(Function<void(CObject&)> invokee)
{
GEventLoop::current().post_event(*this, make<CDeferredInvocationEvent>(move(invokee)));
}

View file

@ -8,32 +8,32 @@ class CEvent;
class CChildEvent;
class CTimerEvent;
class GObject : public Weakable<GObject> {
class CObject : public Weakable<CObject> {
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<GObject*>& children() { return m_children; }
Vector<CObject*>& 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(GObject&)>);
void deferred_invoke(Function<void(CObject&)>);
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<GObject*> m_children;
Vector<CObject*> m_children;
};

View file

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

View file

@ -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;
}

View file

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

View file

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

View file

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

View file

@ -1,6 +1,6 @@
#include <LibCore/CObject.h>
#include "GEventLoop.h"
#include "GEvent.h"
#include "GObject.h"
#include "GWindow.h"
#include <LibGUI/GApplication.h>
#include <LibGUI/GAction.h>
@ -177,7 +177,7 @@ int GEventLoop::exec()
ASSERT_NOT_REACHED();
}
void GEventLoop::post_event(GObject& receiver, OwnPtr<CEvent>&& event)
void GEventLoop::post_event(CObject& receiver, OwnPtr<CEvent>&& 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<EventLoopTimer>();

View file

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

View file

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

View file

@ -1,9 +1,9 @@
#pragma once
#include <LibGUI/GObject.h>
#include <LibCore/CObject.h>
#include <AK/ByteBuffer.h>
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; }

View file

@ -5,7 +5,7 @@
#include <LibGUI/GTextEditor.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)
, m_prompt(prompt)
{

View file

@ -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; }

View file

@ -3,7 +3,7 @@
#include <LibGUI/GLabel.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)
, m_text(text)
{

View file

@ -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:

View file

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

View file

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

View file

@ -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;

View file

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

View file

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

View file

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

View file

@ -1,11 +1,11 @@
#pragma once
#include <LibGUI/GObject.h>
#include <LibCore/CObject.h>
#include <AK/Function.h>
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();

View file

@ -11,7 +11,7 @@
#include <unistd.h>
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);
}
}

View file

@ -2,7 +2,7 @@
#include <LibCore/CElapsedTimer.h>
#include <LibGUI/GEvent.h>
#include <LibGUI/GObject.h>
#include <LibCore/CObject.h>
#include <SharedGraphics/Rect.h>
#include <SharedGraphics/Color.h>
#include <SharedGraphics/Font.h>
@ -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;

View file

@ -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<GWMEvent&>(event));
GObject::event(event);
CObject::event(event);
}
bool GWindow::is_visible() const

View file

@ -1,6 +1,6 @@
#pragma once
#include <LibGUI/GObject.h>
#include <LibCore/CObject.h>
#include <LibGUI/GWindowType.h>
#include <SharedGraphics/Rect.h>
#include <SharedGraphics/GraphicsBitmap.h>
@ -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);

View file

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