Label.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Utf8View.h>
  8. #include <LibGUI/Label.h>
  9. #include <LibGUI/Painter.h>
  10. #include <LibGfx/Bitmap.h>
  11. #include <LibGfx/Palette.h>
  12. #include <LibGfx/TextLayout.h>
  13. #include <LibGfx/TextWrapping.h>
  14. REGISTER_WIDGET(GUI, Label)
  15. namespace GUI {
  16. Label::Label(String text)
  17. : m_text(move(text))
  18. {
  19. REGISTER_TEXT_ALIGNMENT_PROPERTY("text_alignment", text_alignment, set_text_alignment);
  20. REGISTER_TEXT_WRAPPING_PROPERTY("text_wrapping", text_wrapping, set_text_wrapping);
  21. set_preferred_size({ SpecialDimension::OpportunisticGrow });
  22. set_min_height(22);
  23. set_frame_thickness(0);
  24. set_frame_shadow(Gfx::FrameShadow::Plain);
  25. set_frame_shape(Gfx::FrameShape::NoFrame);
  26. set_foreground_role(Gfx::ColorRole::WindowText);
  27. REGISTER_STRING_PROPERTY("text", text, set_text);
  28. REGISTER_BOOL_PROPERTY("autosize", is_autosize, set_autosize);
  29. REGISTER_STRING_PROPERTY("icon", icon, set_icon_from_path);
  30. }
  31. void Label::set_autosize(bool autosize, size_t padding)
  32. {
  33. if (m_autosize == autosize && m_autosize_padding == padding)
  34. return;
  35. m_autosize = autosize;
  36. m_autosize_padding = padding;
  37. if (m_autosize)
  38. size_to_fit();
  39. }
  40. void Label::set_icon(Gfx::Bitmap const* icon)
  41. {
  42. if (m_icon == icon)
  43. return;
  44. m_icon = icon;
  45. update();
  46. }
  47. void Label::set_icon_from_path(String const& path)
  48. {
  49. auto maybe_bitmap = Gfx::Bitmap::try_load_from_file(path);
  50. if (maybe_bitmap.is_error()) {
  51. dbgln("Unable to load bitmap `{}` for label icon", path);
  52. return;
  53. }
  54. set_icon(maybe_bitmap.release_value());
  55. }
  56. void Label::set_text(String text)
  57. {
  58. if (text == m_text)
  59. return;
  60. m_text = move(text);
  61. if (m_autosize)
  62. size_to_fit();
  63. update();
  64. did_change_text();
  65. }
  66. Gfx::IntRect Label::text_rect() const
  67. {
  68. return frame_inner_rect().shrunken(frame_thickness() > 0 ? font().glyph_width('x') : 0, 0);
  69. }
  70. void Label::paint_event(PaintEvent& event)
  71. {
  72. Frame::paint_event(event);
  73. Painter painter(*this);
  74. painter.add_clip_rect(event.rect());
  75. if (m_icon) {
  76. if (m_should_stretch_icon) {
  77. painter.draw_scaled_bitmap(frame_inner_rect(), *m_icon, m_icon->rect());
  78. } else {
  79. auto icon_location = frame_inner_rect().center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2));
  80. painter.blit(icon_location, *m_icon, m_icon->rect());
  81. }
  82. }
  83. if (text().is_empty())
  84. return;
  85. auto text_rect = this->text_rect();
  86. if (is_enabled()) {
  87. painter.draw_text(text_rect, text(), text_alignment(), palette().color(foreground_role()), Gfx::TextElision::Right, text_wrapping());
  88. } else {
  89. painter.draw_text(text_rect.translated(1, 1), text(), font(), text_alignment(), palette().disabled_text_back(), Gfx::TextElision::Right, text_wrapping());
  90. painter.draw_text(text_rect, text(), font(), text_alignment(), palette().disabled_text_front(), Gfx::TextElision::Right, text_wrapping());
  91. }
  92. }
  93. void Label::size_to_fit()
  94. {
  95. set_fixed_width(font().width(m_text) + m_autosize_padding * 2);
  96. }
  97. int Label::text_calculated_preferred_height() const
  98. {
  99. // FIXME: The 4 is taken from Gfx::Painter and should be available as
  100. // a constant instead.
  101. return Gfx::TextLayout(&font(), Utf8View { m_text }, text_rect()).bounding_rect(Gfx::TextWrapping::Wrap, 4).height();
  102. }
  103. Optional<UISize> Label::calculated_preferred_size() const
  104. {
  105. return GUI::UISize(SpecialDimension::Grow, text_calculated_preferred_height());
  106. }
  107. }