2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2023-01-06 13:27:05 +03:00
|
|
|
* Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
|
2022-02-26 20:50:04 +03:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2021-07-26 00:20:11 +03:00
|
|
|
#include <AK/Utf8View.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/Label.h>
|
|
|
|
#include <LibGUI/Painter.h>
|
2020-02-15 01:53:11 +03:00
|
|
|
#include <LibGfx/Bitmap.h>
|
|
|
|
#include <LibGfx/Palette.h>
|
2021-07-27 22:33:03 +03:00
|
|
|
#include <LibGfx/TextWrapping.h>
|
2018-10-10 17:49:36 +03:00
|
|
|
|
2021-01-03 02:30:13 +03:00
|
|
|
REGISTER_WIDGET(GUI, Label)
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
namespace GUI {
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
Label::Label(DeprecatedString text)
|
2020-12-26 15:10:50 +03:00
|
|
|
: m_text(move(text))
|
2019-03-10 15:16:36 +03:00
|
|
|
{
|
2020-12-28 22:44:29 +03:00
|
|
|
REGISTER_TEXT_ALIGNMENT_PROPERTY("text_alignment", text_alignment, set_text_alignment);
|
2021-07-29 23:06:46 +03:00
|
|
|
REGISTER_TEXT_WRAPPING_PROPERTY("text_wrapping", text_wrapping, set_text_wrapping);
|
2020-12-28 22:44:29 +03:00
|
|
|
|
2022-06-29 06:06:56 +03:00
|
|
|
set_preferred_size({ SpecialDimension::OpportunisticGrow });
|
|
|
|
set_min_height(22);
|
2022-02-22 22:25:30 +03:00
|
|
|
|
2020-02-23 13:09:20 +03:00
|
|
|
set_frame_thickness(0);
|
|
|
|
set_frame_shadow(Gfx::FrameShadow::Plain);
|
|
|
|
set_frame_shape(Gfx::FrameShape::NoFrame);
|
2020-09-25 21:41:30 +03:00
|
|
|
|
2020-10-23 12:57:00 +03:00
|
|
|
set_foreground_role(Gfx::ColorRole::WindowText);
|
|
|
|
|
2023-03-10 20:02:14 +03:00
|
|
|
REGISTER_DEPRECATED_STRING_PROPERTY("text", text, set_text);
|
2020-12-20 14:35:10 +03:00
|
|
|
REGISTER_BOOL_PROPERTY("autosize", is_autosize, set_autosize);
|
2022-12-08 19:00:55 +03:00
|
|
|
REGISTER_WRITE_ONLY_STRING_PROPERTY("icon", set_icon_from_path);
|
2019-03-10 15:16:36 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 04:14:36 +03:00
|
|
|
void Label::set_autosize(bool autosize, size_t padding)
|
2020-12-20 14:35:10 +03:00
|
|
|
{
|
2022-04-01 04:14:36 +03:00
|
|
|
if (m_autosize == autosize && m_autosize_padding == padding)
|
2020-12-20 14:35:10 +03:00
|
|
|
return;
|
|
|
|
m_autosize = autosize;
|
2022-04-01 04:14:36 +03:00
|
|
|
m_autosize_padding = padding;
|
2020-12-20 14:35:10 +03:00
|
|
|
if (m_autosize)
|
|
|
|
size_to_fit();
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void Label::set_icon(Gfx::Bitmap const* icon)
|
2019-02-12 17:23:07 +03:00
|
|
|
{
|
2019-08-10 00:48:44 +03:00
|
|
|
if (m_icon == icon)
|
|
|
|
return;
|
2019-07-11 16:52:15 +03:00
|
|
|
m_icon = icon;
|
2019-08-10 00:48:44 +03:00
|
|
|
update();
|
2019-02-12 17:23:07 +03:00
|
|
|
}
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
void Label::set_icon_from_path(DeprecatedString const& path)
|
2022-01-29 10:00:15 +03:00
|
|
|
{
|
2023-01-20 22:06:05 +03:00
|
|
|
auto maybe_bitmap = Gfx::Bitmap::load_from_file(path);
|
2022-01-29 10:00:15 +03:00
|
|
|
if (maybe_bitmap.is_error()) {
|
|
|
|
dbgln("Unable to load bitmap `{}` for label icon", path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
set_icon(maybe_bitmap.release_value());
|
|
|
|
}
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
void Label::set_text(DeprecatedString text)
|
2018-10-10 21:06:58 +03:00
|
|
|
{
|
|
|
|
if (text == m_text)
|
|
|
|
return;
|
2020-12-26 15:10:50 +03:00
|
|
|
m_text = move(text);
|
2021-07-26 00:20:11 +03:00
|
|
|
|
2020-12-20 14:35:10 +03:00
|
|
|
if (m_autosize)
|
|
|
|
size_to_fit();
|
2018-10-10 21:06:58 +03:00
|
|
|
update();
|
2020-12-26 15:10:50 +03:00
|
|
|
did_change_text();
|
2018-10-10 21:06:58 +03:00
|
|
|
}
|
|
|
|
|
2021-07-26 00:20:11 +03:00
|
|
|
Gfx::IntRect Label::text_rect() const
|
2020-12-26 17:34:49 +03:00
|
|
|
{
|
2021-07-28 00:55:42 +03:00
|
|
|
return frame_inner_rect().shrunken(frame_thickness() > 0 ? font().glyph_width('x') : 0, 0);
|
2020-12-26 17:34:49 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Label::paint_event(PaintEvent& event)
|
2018-10-10 17:49:36 +03:00
|
|
|
{
|
2020-02-02 17:07:41 +03:00
|
|
|
Frame::paint_event(event);
|
2019-03-28 17:30:29 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
Painter painter(*this);
|
2019-03-29 17:01:54 +03:00
|
|
|
painter.add_clip_rect(event.rect());
|
2019-03-22 06:20:48 +03:00
|
|
|
|
2019-02-12 17:23:07 +03:00
|
|
|
if (m_icon) {
|
2019-03-22 06:20:48 +03:00
|
|
|
if (m_should_stretch_icon) {
|
2019-03-28 17:30:29 +03:00
|
|
|
painter.draw_scaled_bitmap(frame_inner_rect(), *m_icon, m_icon->rect());
|
2019-03-22 06:20:48 +03:00
|
|
|
} else {
|
2019-03-28 17:30:29 +03:00
|
|
|
auto icon_location = frame_inner_rect().center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2));
|
2019-03-22 06:20:48 +03:00
|
|
|
painter.blit(icon_location, *m_icon, m_icon->rect());
|
|
|
|
}
|
2019-02-12 17:23:07 +03:00
|
|
|
}
|
2021-03-01 22:52:04 +03:00
|
|
|
|
2019-05-25 00:00:07 +03:00
|
|
|
if (text().is_empty())
|
|
|
|
return;
|
|
|
|
|
2021-07-26 00:20:11 +03:00
|
|
|
auto text_rect = this->text_rect();
|
|
|
|
if (is_enabled()) {
|
2021-07-29 23:06:46 +03:00
|
|
|
painter.draw_text(text_rect, text(), text_alignment(), palette().color(foreground_role()), Gfx::TextElision::Right, text_wrapping());
|
2019-05-25 00:00:07 +03:00
|
|
|
} else {
|
2022-03-05 03:28:03 +03: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 17:30:29 +03:00
|
|
|
}
|
2018-10-10 17:49:36 +03:00
|
|
|
}
|
2019-03-21 00:01:02 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Label::size_to_fit()
|
2019-03-21 00:01:02 +03:00
|
|
|
{
|
2023-02-09 17:21:15 +03:00
|
|
|
set_fixed_width(text_calculated_preferred_width());
|
|
|
|
}
|
|
|
|
|
|
|
|
int Label::text_calculated_preferred_width() const
|
|
|
|
{
|
|
|
|
return static_cast<int>(ceilf(font().width(m_text))) + m_autosize_padding * 2;
|
2019-03-21 00:01:02 +03:00
|
|
|
}
|
2020-02-02 17:07:41 +03:00
|
|
|
|
2022-02-22 22:25:30 +03:00
|
|
|
int Label::text_calculated_preferred_height() const
|
2021-03-01 22:52:04 +03:00
|
|
|
{
|
2023-01-06 15:41:51 +03:00
|
|
|
return static_cast<int>(ceilf(font().preferred_line_height()) * (m_text.count("\n"sv) + 1));
|
2021-03-01 22:52:04 +03:00
|
|
|
}
|
2022-02-22 22:25:30 +03:00
|
|
|
|
|
|
|
Optional<UISize> Label::calculated_preferred_size() const
|
|
|
|
{
|
2023-02-09 17:21:15 +03:00
|
|
|
return GUI::UISize(text_calculated_preferred_width(), text_calculated_preferred_height());
|
2022-02-22 22:25:30 +03:00
|
|
|
}
|
2023-02-09 17:21:15 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
}
|