Label.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Utf8View.h>
  7. #include <LibGUI/Label.h>
  8. #include <LibGUI/Painter.h>
  9. #include <LibGfx/Bitmap.h>
  10. #include <LibGfx/Font.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. set_frame_thickness(0);
  21. set_frame_shadow(Gfx::FrameShadow::Plain);
  22. set_frame_shape(Gfx::FrameShape::NoFrame);
  23. set_foreground_role(Gfx::ColorRole::WindowText);
  24. REGISTER_STRING_PROPERTY("text", text, set_text);
  25. REGISTER_BOOL_PROPERTY("autosize", is_autosize, set_autosize);
  26. }
  27. Label::~Label()
  28. {
  29. }
  30. void Label::set_autosize(bool autosize)
  31. {
  32. if (m_autosize == autosize)
  33. return;
  34. m_autosize = autosize;
  35. if (m_autosize)
  36. size_to_fit();
  37. }
  38. void Label::set_icon(const Gfx::Bitmap* icon)
  39. {
  40. if (m_icon == icon)
  41. return;
  42. m_icon = icon;
  43. update();
  44. }
  45. void Label::set_text(String text)
  46. {
  47. if (text == m_text)
  48. return;
  49. m_text = move(text);
  50. if (m_autosize)
  51. size_to_fit();
  52. update();
  53. did_change_text();
  54. }
  55. Gfx::IntRect Label::text_rect() const
  56. {
  57. int indent = 0;
  58. if (frame_thickness() > 0)
  59. indent = font().glyph_width('x') / 2;
  60. auto rect = frame_inner_rect();
  61. rect.set_width(rect.width() - indent * 2);
  62. return rect;
  63. }
  64. void Label::paint_event(PaintEvent& event)
  65. {
  66. Frame::paint_event(event);
  67. Painter painter(*this);
  68. painter.add_clip_rect(event.rect());
  69. if (m_icon) {
  70. if (m_should_stretch_icon) {
  71. painter.draw_scaled_bitmap(frame_inner_rect(), *m_icon, m_icon->rect());
  72. } else {
  73. auto icon_location = frame_inner_rect().center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2));
  74. painter.blit(icon_location, *m_icon, m_icon->rect());
  75. }
  76. }
  77. if (text().is_empty())
  78. return;
  79. auto text_rect = this->text_rect();
  80. if (is_enabled()) {
  81. painter.draw_text(text_rect, text(), m_text_alignment, palette().color(foreground_role()), Gfx::TextElision::Right, Gfx::TextWrapping::Wrap);
  82. } else {
  83. painter.draw_text(text_rect.translated(1, 1), text(), font(), text_alignment(), Color::White, Gfx::TextElision::Right, Gfx::TextWrapping::Wrap);
  84. painter.draw_text(text_rect, text(), font(), text_alignment(), Color::from_rgb(0x808080), Gfx::TextElision::Right, Gfx::TextWrapping::Wrap);
  85. }
  86. }
  87. void Label::size_to_fit()
  88. {
  89. set_fixed_width(font().width(m_text));
  90. }
  91. int Label::preferred_height() const
  92. {
  93. // FIXME: The 4 is taken from Gfx::Painter and should be available as
  94. // a constant instead.
  95. return Gfx::TextLayout(&font(), Utf8View { m_text }, text_rect()).bounding_rect(Gfx::TextWrapping::Wrap, 4).height();
  96. }
  97. }