Label.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_frame_thickness(0);
  22. set_frame_shadow(Gfx::FrameShadow::Plain);
  23. set_frame_shape(Gfx::FrameShape::NoFrame);
  24. set_foreground_role(Gfx::ColorRole::WindowText);
  25. REGISTER_STRING_PROPERTY("text", text, set_text);
  26. REGISTER_BOOL_PROPERTY("autosize", is_autosize, set_autosize);
  27. REGISTER_STRING_PROPERTY("icon", icon, set_icon_from_path);
  28. }
  29. void Label::set_autosize(bool autosize)
  30. {
  31. if (m_autosize == autosize)
  32. return;
  33. m_autosize = autosize;
  34. if (m_autosize)
  35. size_to_fit();
  36. }
  37. void Label::set_icon(const Gfx::Bitmap* icon)
  38. {
  39. if (m_icon == icon)
  40. return;
  41. m_icon = icon;
  42. update();
  43. }
  44. void Label::set_icon_from_path(String const& path)
  45. {
  46. auto maybe_bitmap = Gfx::Bitmap::try_load_from_file(path);
  47. if (maybe_bitmap.is_error()) {
  48. dbgln("Unable to load bitmap `{}` for label icon", path);
  49. return;
  50. }
  51. set_icon(maybe_bitmap.release_value());
  52. }
  53. void Label::set_text(String text)
  54. {
  55. if (text == m_text)
  56. return;
  57. m_text = move(text);
  58. if (m_autosize)
  59. size_to_fit();
  60. update();
  61. did_change_text();
  62. }
  63. Gfx::IntRect Label::text_rect() const
  64. {
  65. return frame_inner_rect().shrunken(frame_thickness() > 0 ? font().glyph_width('x') : 0, 0);
  66. }
  67. void Label::paint_event(PaintEvent& event)
  68. {
  69. Frame::paint_event(event);
  70. Painter painter(*this);
  71. painter.add_clip_rect(event.rect());
  72. if (m_icon) {
  73. if (m_should_stretch_icon) {
  74. painter.draw_scaled_bitmap(frame_inner_rect(), *m_icon, m_icon->rect());
  75. } else {
  76. auto icon_location = frame_inner_rect().center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2));
  77. painter.blit(icon_location, *m_icon, m_icon->rect());
  78. }
  79. }
  80. if (text().is_empty())
  81. return;
  82. auto text_rect = this->text_rect();
  83. if (is_enabled()) {
  84. painter.draw_text(text_rect, text(), text_alignment(), palette().color(foreground_role()), Gfx::TextElision::Right, text_wrapping());
  85. } else {
  86. painter.draw_text(text_rect.translated(1, 1), text(), font(), text_alignment(), palette().disabled_text_back(), Gfx::TextElision::Right, text_wrapping());
  87. painter.draw_text(text_rect, text(), font(), text_alignment(), palette().disabled_text_front(), Gfx::TextElision::Right, text_wrapping());
  88. }
  89. }
  90. void Label::size_to_fit()
  91. {
  92. set_fixed_width(font().width(m_text));
  93. }
  94. int Label::preferred_height() const
  95. {
  96. // FIXME: The 4 is taken from Gfx::Painter and should be available as
  97. // a constant instead.
  98. return Gfx::TextLayout(&font(), Utf8View { m_text }, text_rect()).bounding_rect(Gfx::TextWrapping::Wrap, 4).height();
  99. }
  100. }