2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/GroupBox.h>
|
|
|
|
|
#include <LibGUI/Painter.h>
|
2020-02-14 23:53:11 +01:00
|
|
|
#include <LibGfx/Font.h>
|
|
|
|
|
#include <LibGfx/Palette.h>
|
|
|
|
|
#include <LibGfx/StylePainter.h>
|
2019-04-10 05:52:15 +02:00
|
|
|
|
2021-01-02 16:30:13 -07:00
|
|
|
REGISTER_WIDGET(GUI, GroupBox)
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
|
2020-02-23 12:07:13 +01:00
|
|
|
GroupBox::GroupBox(const StringView& title)
|
|
|
|
|
: m_title(title)
|
2019-04-10 05:52:15 +02:00
|
|
|
{
|
2020-12-30 13:58:38 +03:30
|
|
|
REGISTER_STRING_PROPERTY("title", title, set_title);
|
2019-04-10 05:52:15 +02:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
GroupBox::~GroupBox()
|
2019-04-10 05:52:15 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 15:42:25 +02:00
|
|
|
Margins GroupBox::content_margins() const
|
|
|
|
|
{
|
|
|
|
|
return {
|
|
|
|
|
(!m_title.is_empty() ? font().glyph_height() + 1 /*room for the focus rect*/ : 2),
|
|
|
|
|
2,
|
|
|
|
|
2,
|
|
|
|
|
2
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void GroupBox::paint_event(PaintEvent& event)
|
2019-04-10 05:52:15 +02:00
|
|
|
{
|
2020-02-02 15:07:41 +01:00
|
|
|
Painter painter(*this);
|
2019-04-10 05:52:15 +02:00
|
|
|
painter.add_clip_rect(event.rect());
|
|
|
|
|
|
2020-06-10 10:57:59 +02:00
|
|
|
Gfx::IntRect frame_rect {
|
2021-02-21 19:08:23 -05:00
|
|
|
0, (!m_title.is_empty() ? font().glyph_height() / 2 : 0),
|
|
|
|
|
width(), height() - (!m_title.is_empty() ? font().glyph_height() / 2 : 0)
|
2019-04-10 05:52:15 +02:00
|
|
|
};
|
2020-02-06 11:56:38 +01:00
|
|
|
Gfx::StylePainter::paint_frame(painter, frame_rect, palette(), Gfx::FrameShape::Box, Gfx::FrameShadow::Sunken, 2);
|
2019-04-10 05:52:15 +02:00
|
|
|
|
2020-07-14 11:50:34 -04:00
|
|
|
if (!m_title.is_empty()) {
|
2021-07-20 20:39:25 +02:00
|
|
|
Gfx::IntRect text_rect { 6, 1, font().width(m_title) + 6, font().glyph_height() };
|
2020-07-14 11:50:34 -04:00
|
|
|
painter.fill_rect(text_rect, palette().button());
|
|
|
|
|
painter.draw_text(text_rect, m_title, Gfx::TextAlignment::Center, palette().button_text());
|
|
|
|
|
}
|
2019-04-10 05:52:15 +02:00
|
|
|
}
|
2019-04-19 01:40:25 +02:00
|
|
|
|
2021-09-28 15:59:07 +02:00
|
|
|
void GroupBox::fonts_change_event(FontsChangeEvent& event)
|
|
|
|
|
{
|
|
|
|
|
Widget::fonts_change_event(event);
|
|
|
|
|
invalidate_layout();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void GroupBox::set_title(const StringView& title)
|
2019-04-19 01:40:25 +02:00
|
|
|
{
|
2019-05-08 14:32:46 +02:00
|
|
|
if (m_title == title)
|
2019-04-19 01:40:25 +02:00
|
|
|
return;
|
2019-05-08 14:32:46 +02:00
|
|
|
m_title = title;
|
2019-04-19 01:40:25 +02:00
|
|
|
update();
|
|
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
|
|
|
|
|
}
|