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