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 {
|
|
|
|
|
2023-04-29 17:41:48 +03:00
|
|
|
Label::Label(String 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 });
|
2023-04-14 15:54:12 +03:00
|
|
|
set_min_size({ SpecialDimension::Shrink });
|
2023-04-29 23:47:52 +03:00
|
|
|
set_frame_style(Gfx::FrameStyle::NoFrame);
|
2020-10-23 12:57:00 +03:00
|
|
|
set_foreground_role(Gfx::ColorRole::WindowText);
|
|
|
|
|
2023-04-29 17:41:48 +03:00
|
|
|
REGISTER_STRING_PROPERTY("text", text, set_text);
|
2020-12-20 14:35:10 +03:00
|
|
|
REGISTER_BOOL_PROPERTY("autosize", is_autosize, set_autosize);
|
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();
|
|
|
|
}
|
|
|
|
|
2023-04-29 17:41:48 +03:00
|
|
|
void Label::set_text(String 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-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
|
|
|
|
2023-04-14 15:54:16 +03:00
|
|
|
void Label::did_change_font()
|
|
|
|
{
|
|
|
|
if (m_autosize)
|
|
|
|
size_to_fit();
|
|
|
|
}
|
|
|
|
|
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());
|
2023-04-14 15:54:16 +03:00
|
|
|
set_fixed_height(text_calculated_preferred_height());
|
2023-02-09 17:21:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int Label::text_calculated_preferred_width() const
|
|
|
|
{
|
2023-04-14 15:52:47 +03:00
|
|
|
return font().width_rounded_up(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
|
|
|
{
|
2024-03-08 18:02:20 +03:00
|
|
|
return static_cast<int>(ceilf(font().preferred_line_height()) * m_text.bytes_as_string_view().count_lines());
|
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
|
|
|
|
2023-04-14 15: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 17:07:41 +03:00
|
|
|
}
|