summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/Statusbar.cpp
blob: cc66795e818a78775b387149f3269efa26ab768d (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibGUI/BoxLayout.h>
#include <LibGUI/Label.h>
#include <LibGUI/Painter.h>
#include <LibGUI/ResizeCorner.h>
#include <LibGUI/Statusbar.h>
#include <LibGUI/Window.h>
#include <LibGfx/Palette.h>
#include <LibGfx/StylePainter.h>

REGISTER_WIDGET(GUI, Statusbar)

namespace GUI {

Statusbar::Statusbar(int label_count)
{
    set_fixed_height(18);
    set_layout<HorizontalBoxLayout>();
    layout()->set_margins(0);
    layout()->set_spacing(2);

    m_corner = add<ResizeCorner>();
    set_label_count(label_count);

    REGISTER_STRING_PROPERTY("text", text, set_text);
    REGISTER_INT_PROPERTY("label_count", label_count, set_label_count);
}

Statusbar::~Statusbar()
{
}

NonnullRefPtr<Label> Statusbar::create_label()
{
    auto label = Label::construct();
    insert_child_before(*label, *m_corner);
    label->set_frame_shadow(Gfx::FrameShadow::Sunken);
    label->set_frame_shape(Gfx::FrameShape::Panel);
    label->set_frame_thickness(1);
    label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
    label->set_text_wrapping(Gfx::TextWrapping::DontWrap);
    return label;
}

void Statusbar::set_text(String text)
{
    set_text(0, move(text));
}

String Statusbar::text() const
{
    return text(0);
}

void Statusbar::set_text(size_t index, String text)
{
    m_segments.at(index).text = move(text);
    update_label(index);
}

void Statusbar::set_label_count(size_t label_count)
{
    if (label_count <= 1)
        label_count = 1;

    for (auto i = m_segments.size(); i < label_count; i++) {
        m_segments.append(Segment {
            .label = create_label(),
            .text = {},
            .override_text = {},
        });
    }
}

void Statusbar::update_label(size_t index)
{
    auto& segment = m_segments.at(index);

    if (segment.override_text.is_null()) {
        segment.label->set_frame_shadow(Gfx::FrameShadow::Sunken);
        segment.label->set_frame_shape(Gfx::FrameShape::Panel);
        segment.label->set_text(segment.text);
    } else {
        segment.label->set_frame_shadow(Gfx::FrameShadow::Plain);
        segment.label->set_frame_shape(Gfx::FrameShape::NoFrame);
        segment.label->set_text(segment.override_text);
    }
}

String Statusbar::text(size_t index) const
{
    return m_segments.at(index).label->text();
}

void Statusbar::set_override_text(String override_text)
{
    set_override_text(0, move(override_text));
}

void Statusbar::set_override_text(size_t index, String override_text)
{
    m_segments.at(index).override_text = move(override_text);
    update_label(index);
}

void Statusbar::paint_event(PaintEvent& event)
{
    Painter painter(*this);
    painter.add_clip_rect(event.rect());
    painter.fill_rect(rect(), palette().button());
}

void Statusbar::resize_event(ResizeEvent& event)
{
    if (auto* window = this->window()) {
        m_corner->set_visible(window->is_resizable() && !window->is_maximized());
    }

    Widget::resize_event(event);
}
}