Label.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2018-2023, 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/TextWrapping.h>
  13. REGISTER_WIDGET(GUI, Label)
  14. namespace GUI {
  15. Label::Label(DeprecatedString text)
  16. : m_text(move(text))
  17. {
  18. REGISTER_TEXT_ALIGNMENT_PROPERTY("text_alignment", text_alignment, set_text_alignment);
  19. REGISTER_TEXT_WRAPPING_PROPERTY("text_wrapping", text_wrapping, set_text_wrapping);
  20. set_preferred_size({ SpecialDimension::OpportunisticGrow });
  21. set_min_height(22);
  22. set_frame_thickness(0);
  23. set_frame_shadow(Gfx::FrameShadow::Plain);
  24. set_frame_shape(Gfx::FrameShape::NoFrame);
  25. set_foreground_role(Gfx::ColorRole::WindowText);
  26. REGISTER_DEPRECATED_STRING_PROPERTY("text", text, set_text);
  27. REGISTER_BOOL_PROPERTY("autosize", is_autosize, set_autosize);
  28. REGISTER_WRITE_ONLY_STRING_PROPERTY("icon", set_icon_from_path);
  29. }
  30. void Label::set_autosize(bool autosize, size_t padding)
  31. {
  32. if (m_autosize == autosize && m_autosize_padding == padding)
  33. return;
  34. m_autosize = autosize;
  35. m_autosize_padding = padding;
  36. if (m_autosize)
  37. size_to_fit();
  38. }
  39. void Label::set_icon(Gfx::Bitmap const* icon)
  40. {
  41. if (m_icon == icon)
  42. return;
  43. m_icon = icon;
  44. update();
  45. }
  46. void Label::set_icon_from_path(DeprecatedString const& path)
  47. {
  48. auto maybe_bitmap = Gfx::Bitmap::load_from_file(path);
  49. if (maybe_bitmap.is_error()) {
  50. dbgln("Unable to load bitmap `{}` for label icon", path);
  51. return;
  52. }
  53. set_icon(maybe_bitmap.release_value());
  54. }
  55. void Label::set_text(DeprecatedString text)
  56. {
  57. if (text == m_text)
  58. return;
  59. m_text = move(text);
  60. if (m_autosize)
  61. size_to_fit();
  62. update();
  63. did_change_text();
  64. }
  65. Gfx::IntRect Label::text_rect() const
  66. {
  67. return frame_inner_rect().shrunken(frame_thickness() > 0 ? font().glyph_width('x') : 0, 0);
  68. }
  69. void Label::paint_event(PaintEvent& event)
  70. {
  71. Frame::paint_event(event);
  72. Painter painter(*this);
  73. painter.add_clip_rect(event.rect());
  74. if (m_icon) {
  75. if (m_should_stretch_icon) {
  76. painter.draw_scaled_bitmap(frame_inner_rect(), *m_icon, m_icon->rect());
  77. } else {
  78. auto icon_location = frame_inner_rect().center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2));
  79. painter.blit(icon_location, *m_icon, m_icon->rect());
  80. }
  81. }
  82. if (text().is_empty())
  83. return;
  84. auto text_rect = this->text_rect();
  85. if (is_enabled()) {
  86. painter.draw_text(text_rect, text(), text_alignment(), palette().color(foreground_role()), Gfx::TextElision::Right, text_wrapping());
  87. } else {
  88. painter.draw_text(text_rect.translated(1, 1), text(), font(), text_alignment(), palette().disabled_text_back(), Gfx::TextElision::Right, text_wrapping());
  89. painter.draw_text(text_rect, text(), font(), text_alignment(), palette().disabled_text_front(), Gfx::TextElision::Right, text_wrapping());
  90. }
  91. }
  92. void Label::size_to_fit()
  93. {
  94. set_fixed_width(text_calculated_preferred_width());
  95. }
  96. int Label::text_calculated_preferred_width() const
  97. {
  98. return static_cast<int>(ceilf(font().width(m_text))) + m_autosize_padding * 2;
  99. }
  100. int Label::text_calculated_preferred_height() const
  101. {
  102. return static_cast<int>(ceilf(font().preferred_line_height()) * (m_text.count("\n"sv) + 1));
  103. }
  104. Optional<UISize> Label::calculated_preferred_size() const
  105. {
  106. return GUI::UISize(text_calculated_preferred_width(), text_calculated_preferred_height());
  107. }
  108. }