summaryrefslogtreecommitdiff
path: root/Libraries/LibGUI/GStackWidget.cpp
blob: d74003d45ae1eed30d7023d71797ba9dbe11f1e3 (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
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GStackWidget.h>

GStackWidget::GStackWidget(GWidget* parent)
    : GWidget(parent)
{
}

GStackWidget::~GStackWidget()
{
}

void GStackWidget::set_active_widget(GWidget* widget)
{
    if (widget == m_active_widget)
        return;

    if (m_active_widget)
        m_active_widget->set_visible(false);
    m_active_widget = widget;
    if (m_active_widget) {
        m_active_widget->set_relative_rect(rect());
        m_active_widget->set_visible(true);
    }
    if (on_active_widget_change)
        on_active_widget_change(m_active_widget);
}

void GStackWidget::resize_event(GResizeEvent& event)
{
    if (!m_active_widget)
        return;
    m_active_widget->set_relative_rect({ {}, event.size() });
}

void GStackWidget::child_event(CChildEvent& event)
{
    if (!event.child() || !is<GWidget>(*event.child()))
        return GWidget::child_event(event);
    auto& child = to<GWidget>(*event.child());
    if (event.type() == GEvent::ChildAdded) {
        if (!m_active_widget)
            set_active_widget(&child);
        else if (m_active_widget != &child)
            child.set_visible(false);
    } else if (event.type() == GEvent::ChildRemoved) {
        if (m_active_widget == &child) {
            GWidget* new_active_widget = nullptr;
            for_each_child_widget([&](auto& new_child) {
                new_active_widget = &new_child;
                return IterationDecision::Break;
            });
            set_active_widget(new_active_widget);
        }
    }
    GWidget::child_event(event);
}