Serenity Operating System
1/*
2 * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2022, the SerenityOS developers.
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#include <LibGUI/GroupBox.h>
9#include <LibGUI/Painter.h>
10#include <LibGfx/Font/Font.h>
11#include <LibGfx/Palette.h>
12#include <LibGfx/StylePainter.h>
13
14REGISTER_WIDGET(GUI, GroupBox)
15
16namespace GUI {
17
18GroupBox::GroupBox(StringView title)
19 : m_title(title)
20{
21 REGISTER_STRING_PROPERTY("title", title, set_title);
22}
23
24Margins GroupBox::content_margins() const
25{
26 return {
27 (!m_title.is_empty() ? font().pixel_size_rounded_up() + 1 /*room for the focus rect*/ : 2),
28 2,
29 2,
30 2
31 };
32}
33
34void GroupBox::paint_event(PaintEvent& event)
35{
36 Painter painter(*this);
37 painter.add_clip_rect(event.rect());
38
39 Gfx::IntRect frame_rect {
40 0, (!m_title.is_empty() ? font().pixel_size_rounded_up() / 2 : 0),
41 width(), height() - (!m_title.is_empty() ? font().pixel_size_rounded_up() / 2 : 0)
42 };
43 Gfx::StylePainter::paint_frame(painter, frame_rect, palette(), Gfx::FrameShape::Box, Gfx::FrameShadow::Sunken, 2);
44
45 if (!m_title.is_empty()) {
46 Gfx::IntRect text_rect { 6, 1, static_cast<int>(ceilf(font().width(m_title) + 6)), font().pixel_size_rounded_up() };
47 painter.fill_rect(text_rect, palette().button());
48 painter.draw_text(text_rect, m_title, Gfx::TextAlignment::CenterLeft, palette().button_text());
49 }
50}
51
52void GroupBox::fonts_change_event(FontsChangeEvent& event)
53{
54 Widget::fonts_change_event(event);
55 layout_relevant_change_occurred();
56}
57
58void GroupBox::set_title(StringView title)
59{
60 if (m_title == title)
61 return;
62 m_title = title;
63 update();
64}
65
66}