summaryrefslogtreecommitdiff
path: root/LibGUI/GObject.cpp
blob: 881f2203179870682f1edd9204b0f478b5becf64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "GObject.h"
#include "GEvent.h"
#include "GEventLoop.h"
#include <AK/Assertions.h>

GObject::GObject(GObject* parent)
    : m_parent(parent)
{
    if (m_parent)
        m_parent->addChild(*this);
}

GObject::~GObject()
{
    if (m_parent)
        m_parent->removeChild(*this);
    auto childrenToDelete = move(m_children);
    for (auto* child : childrenToDelete)
        delete child;
}

void GObject::event(GEvent& event)
{
    switch (event.type()) {
    case GEvent::Timer:
        return timerEvent(static_cast<GTimerEvent&>(event));
    case GEvent::DeferredDestroy:
        delete this;
        break;
    case GEvent::Invalid:
        ASSERT_NOT_REACHED();
        break;
    default:
        break;
    }
}

void GObject::addChild(GObject& object)
{
    m_children.append(&object);
}

void GObject::removeChild(GObject& object)
{
    for (unsigned i = 0; i < m_children.size(); ++i) {
        if (m_children[i] == &object) {
            m_children.remove(i);
            return;
        }
    }
}

void GObject::timerEvent(GTimerEvent&)
{
}

void GObject::startTimer(int ms)
{
    if (m_timerID) {
        dbgprintf("GObject{%p} already has a timer!\n", this);
        ASSERT_NOT_REACHED();
    }
}

void GObject::stopTimer()
{
    if (!m_timerID)
        return;
    m_timerID = 0;
}

void GObject::delete_later()
{
    GEventLoop::main().post_event(this, make<GEvent>(GEvent::DeferredDestroy));
}