2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2023-01-06 11:27:05 +01:00
|
|
|
* Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
|
2022-02-26 10:50:04 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
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-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 {
|
|
|
|
|
|
2023-04-29 10:41:48 -04:00
|
|
|
Label::Label(String text)
|
2020-12-26 13:10:50 +01:00
|
|
|
: 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
|
|
|
|
2022-06-29 05:06:56 +02:00
|
|
|
set_preferred_size({ SpecialDimension::OpportunisticGrow });
|
2023-04-14 08:54:12 -04:00
|
|
|
set_min_size({ SpecialDimension::Shrink });
|
2023-04-29 16:47:52 -04:00
|
|
|
set_frame_style(Gfx::FrameStyle::NoFrame);
|
2020-10-23 11:57:00 +02:00
|
|
|
set_foreground_role(Gfx::ColorRole::WindowText);
|
|
|
|
|
|
2023-04-29 10:41:48 -04:00
|
|
|
REGISTER_STRING_PROPERTY("text", text, set_text);
|
2020-12-20 12:35:10 +01:00
|
|
|
REGISTER_BOOL_PROPERTY("autosize", is_autosize, set_autosize);
|
2019-03-10 13:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
2022-03-31 18:14:36 -07:00
|
|
|
void Label::set_autosize(bool autosize, size_t padding)
|
2020-12-20 12:35:10 +01:00
|
|
|
{
|
2022-03-31 18:14:36 -07:00
|
|
|
if (m_autosize == autosize && m_autosize_padding == padding)
|
2020-12-20 12:35:10 +01:00
|
|
|
return;
|
|
|
|
|
m_autosize = autosize;
|
2022-03-31 18:14:36 -07:00
|
|
|
m_autosize_padding = padding;
|
2020-12-20 12:35:10 +01:00
|
|
|
if (m_autosize)
|
|
|
|
|
size_to_fit();
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-29 10:41:48 -04: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-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
|
|
|
|
2023-04-14 08:54:16 -04:00
|
|
|
void Label::did_change_font()
|
|
|
|
|
{
|
|
|
|
|
if (m_autosize)
|
|
|
|
|
size_to_fit();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Label::size_to_fit()
|
2019-03-20 22:01:02 +01:00
|
|
|
{
|
2023-02-09 15:21:15 +01:00
|
|
|
set_fixed_width(text_calculated_preferred_width());
|
2023-04-14 08:54:16 -04:00
|
|
|
set_fixed_height(text_calculated_preferred_height());
|
2023-02-09 15:21:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Label::text_calculated_preferred_width() const
|
|
|
|
|
{
|
2023-04-14 08:52:47 -04:00
|
|
|
return font().width_rounded_up(m_text) + m_autosize_padding * 2;
|
2019-03-20 22:01:02 +01:00
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
|
2022-02-22 20:25:30 +01:00
|
|
|
int Label::text_calculated_preferred_height() const
|
2021-03-01 14:52:04 -05:00
|
|
|
{
|
2023-01-06 13:41:51 +01:00
|
|
|
return static_cast<int>(ceilf(font().preferred_line_height()) * (m_text.count("\n"sv) + 1));
|
2021-03-01 14:52:04 -05:00
|
|
|
}
|
2022-02-22 20:25:30 +01:00
|
|
|
|
|
|
|
|
Optional<UISize> Label::calculated_preferred_size() const
|
|
|
|
|
{
|
2023-02-09 15:21:15 +01:00
|
|
|
return GUI::UISize(text_calculated_preferred_width(), text_calculated_preferred_height());
|
2022-02-22 20:25:30 +01:00
|
|
|
}
|
2023-02-09 15:21:15 +01:00
|
|
|
|
2023-04-14 08:54:12 -04:00
|
|
|
Optional<UISize> Label::calculated_min_size() const
|
|
|
|
|
{
|
|
|
|
|
int frame = frame_thickness() * 2;
|
|
|
|
|
int width = font().width_rounded_up("..."sv) + frame;
|
|
|
|
|
int height = font().pixel_size_rounded_up() + frame;
|
|
|
|
|
height = max(height, 22);
|
|
|
|
|
|
|
|
|
|
return UISize(width, height);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
}
|