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
|
|
|
*/
|
|
|
|
|
|
2021-07-25 21:20:11 +00:00
|
|
|
#include <AK/Utf8View.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Label.h>
|
|
|
|
|
#include <LibGUI/Painter.h>
|
2020-02-14 23:53:11 +01:00
|
|
|
#include <LibGfx/Bitmap.h>
|
|
|
|
|
#include <LibGfx/Palette.h>
|
2021-07-25 21:20:11 +00:00
|
|
|
#include <LibGfx/TextLayout.h>
|
2021-07-27 19:33:03 +00:00
|
|
|
#include <LibGfx/TextWrapping.h>
|
2018-10-10 16:49:36 +02:00
|
|
|
|
2021-01-02 16:30:13 -07:00
|
|
|
REGISTER_WIDGET(GUI, Label)
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
|
2020-12-26 13:10:50 +01:00
|
|
|
Label::Label(String text)
|
|
|
|
|
: m_text(move(text))
|
2019-03-10 13:16:36 +01:00
|
|
|
{
|
2020-12-28 20:44:29 +01:00
|
|
|
REGISTER_TEXT_ALIGNMENT_PROPERTY("text_alignment", text_alignment, set_text_alignment);
|
2021-07-29 20:06:46 +00:00
|
|
|
REGISTER_TEXT_WRAPPING_PROPERTY("text_wrapping", text_wrapping, set_text_wrapping);
|
2020-12-28 20:44:29 +01:00
|
|
|
|
2020-02-23 11:09:20 +01:00
|
|
|
set_frame_thickness(0);
|
|
|
|
|
set_frame_shadow(Gfx::FrameShadow::Plain);
|
|
|
|
|
set_frame_shape(Gfx::FrameShape::NoFrame);
|
2020-09-25 20:41:30 +02:00
|
|
|
|
2020-10-23 11:57:00 +02:00
|
|
|
set_foreground_role(Gfx::ColorRole::WindowText);
|
|
|
|
|
|
2020-09-25 20:41:30 +02:00
|
|
|
REGISTER_STRING_PROPERTY("text", text, set_text);
|
2020-12-20 12:35:10 +01:00
|
|
|
REGISTER_BOOL_PROPERTY("autosize", is_autosize, set_autosize);
|
2022-01-28 23:00:15 -08:00
|
|
|
REGISTER_STRING_PROPERTY("icon", icon, set_icon_from_path);
|
2019-03-10 13:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
Label::~Label()
|
2018-10-10 16:49:36 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-20 12:35:10 +01:00
|
|
|
void Label::set_autosize(bool autosize)
|
|
|
|
|
{
|
|
|
|
|
if (m_autosize == autosize)
|
|
|
|
|
return;
|
|
|
|
|
m_autosize = autosize;
|
|
|
|
|
if (m_autosize)
|
|
|
|
|
size_to_fit();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-29 18:41:34 +02:00
|
|
|
void Label::set_icon(const Gfx::Bitmap* icon)
|
2019-02-12 15:23:07 +01:00
|
|
|
{
|
2019-08-09 23:48:44 +02:00
|
|
|
if (m_icon == icon)
|
|
|
|
|
return;
|
2019-07-11 15:52:15 +02:00
|
|
|
m_icon = icon;
|
2019-08-09 23:48:44 +02:00
|
|
|
update();
|
2019-02-12 15:23:07 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-28 23:00:15 -08:00
|
|
|
void Label::set_icon_from_path(String const& path)
|
|
|
|
|
{
|
|
|
|
|
auto maybe_bitmap = Gfx::Bitmap::try_load_from_file(path);
|
|
|
|
|
if (maybe_bitmap.is_error()) {
|
|
|
|
|
dbgln("Unable to load bitmap `{}` for label icon", path);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
set_icon(maybe_bitmap.release_value());
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-26 13:10:50 +01:00
|
|
|
void Label::set_text(String text)
|
2018-10-10 20:06:58 +02:00
|
|
|
{
|
|
|
|
|
if (text == m_text)
|
|
|
|
|
return;
|
2020-12-26 13:10:50 +01:00
|
|
|
m_text = move(text);
|
2021-07-25 21:20:11 +00:00
|
|
|
|
2020-12-20 12:35:10 +01:00
|
|
|
if (m_autosize)
|
|
|
|
|
size_to_fit();
|
2018-10-10 20:06:58 +02:00
|
|
|
update();
|
2020-12-26 13:10:50 +01:00
|
|
|
did_change_text();
|
2018-10-10 20:06:58 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-25 21:20:11 +00:00
|
|
|
Gfx::IntRect Label::text_rect() const
|
2020-12-26 15:34:49 +01:00
|
|
|
{
|
2021-07-27 23:55:42 +02:00
|
|
|
return frame_inner_rect().shrunken(frame_thickness() > 0 ? font().glyph_width('x') : 0, 0);
|
2020-12-26 15:34:49 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Label::paint_event(PaintEvent& event)
|
2018-10-10 16:49:36 +02:00
|
|
|
{
|
2020-02-02 15:07:41 +01:00
|
|
|
Frame::paint_event(event);
|
2019-03-28 15:30:29 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
Painter painter(*this);
|
2019-03-29 15:01:54 +01:00
|
|
|
painter.add_clip_rect(event.rect());
|
2019-03-22 04:20:48 +01:00
|
|
|
|
2019-02-12 15:23:07 +01:00
|
|
|
if (m_icon) {
|
2019-03-22 04:20:48 +01:00
|
|
|
if (m_should_stretch_icon) {
|
2019-03-28 15:30:29 +01:00
|
|
|
painter.draw_scaled_bitmap(frame_inner_rect(), *m_icon, m_icon->rect());
|
2019-03-22 04:20:48 +01:00
|
|
|
} else {
|
2019-03-28 15:30:29 +01:00
|
|
|
auto icon_location = frame_inner_rect().center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2));
|
2019-03-22 04:20:48 +01:00
|
|
|
painter.blit(icon_location, *m_icon, m_icon->rect());
|
|
|
|
|
}
|
2019-02-12 15:23:07 +01:00
|
|
|
}
|
2021-03-01 14:52:04 -05:00
|
|
|
|
2019-05-24 23:00:07 +02:00
|
|
|
if (text().is_empty())
|
|
|
|
|
return;
|
|
|
|
|
|
2021-07-25 21:20:11 +00:00
|
|
|
auto text_rect = this->text_rect();
|
|
|
|
|
if (is_enabled()) {
|
2021-07-29 20:06:46 +00:00
|
|
|
painter.draw_text(text_rect, text(), text_alignment(), palette().color(foreground_role()), Gfx::TextElision::Right, text_wrapping());
|
2019-05-24 23:00:07 +02:00
|
|
|
} else {
|
2022-03-05 01:28:03 +01:00
|
|
|
painter.draw_text(text_rect.translated(1, 1), text(), font(), text_alignment(), palette().disabled_text_back(), Gfx::TextElision::Right, text_wrapping());
|
|
|
|
|
painter.draw_text(text_rect, text(), font(), text_alignment(), palette().disabled_text_front(), Gfx::TextElision::Right, text_wrapping());
|
2019-03-28 15:30:29 +01:00
|
|
|
}
|
2018-10-10 16:49:36 +02:00
|
|
|
}
|
2019-03-20 22:01:02 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Label::size_to_fit()
|
2019-03-20 22:01:02 +01:00
|
|
|
{
|
2020-12-30 01:23:32 +01:00
|
|
|
set_fixed_width(font().width(m_text));
|
2019-03-20 22:01:02 +01:00
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
|
2021-07-25 21:20:11 +00:00
|
|
|
int Label::preferred_height() const
|
2021-03-01 14:52:04 -05:00
|
|
|
{
|
2021-07-25 21:20:11 +00:00
|
|
|
// FIXME: The 4 is taken from Gfx::Painter and should be available as
|
|
|
|
|
// a constant instead.
|
|
|
|
|
return Gfx::TextLayout(&font(), Utf8View { m_text }, text_rect()).bounding_rect(Gfx::TextWrapping::Wrap, 4).height();
|
2021-03-01 14:52:04 -05:00
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
}
|