Label.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/Label.h>
  27. #include <LibGUI/Painter.h>
  28. #include <LibGfx/Bitmap.h>
  29. #include <LibGfx/Font.h>
  30. #include <LibGfx/Palette.h>
  31. namespace GUI {
  32. Label::Label(String text)
  33. : m_text(move(text))
  34. {
  35. set_frame_thickness(0);
  36. set_frame_shadow(Gfx::FrameShadow::Plain);
  37. set_frame_shape(Gfx::FrameShape::NoFrame);
  38. set_foreground_role(Gfx::ColorRole::WindowText);
  39. REGISTER_STRING_PROPERTY("text", text, set_text);
  40. REGISTER_BOOL_PROPERTY("autosize", is_autosize, set_autosize);
  41. }
  42. Label::~Label()
  43. {
  44. }
  45. void Label::set_autosize(bool autosize)
  46. {
  47. if (m_autosize == autosize)
  48. return;
  49. m_autosize = autosize;
  50. if (m_autosize)
  51. size_to_fit();
  52. }
  53. void Label::set_icon(const Gfx::Bitmap* icon)
  54. {
  55. if (m_icon == icon)
  56. return;
  57. m_icon = icon;
  58. update();
  59. }
  60. void Label::set_text(String text)
  61. {
  62. if (text == m_text)
  63. return;
  64. m_text = move(text);
  65. if (m_autosize)
  66. size_to_fit();
  67. update();
  68. did_change_text();
  69. }
  70. Gfx::IntRect Label::text_rect() const
  71. {
  72. int indent = 0;
  73. if (frame_thickness() > 0)
  74. indent = font().glyph_width('x') / 2;
  75. auto rect = frame_inner_rect();
  76. rect.move_by(indent, 0);
  77. rect.set_width(rect.width() - indent * 2);
  78. return rect;
  79. }
  80. void Label::paint_event(PaintEvent& event)
  81. {
  82. Frame::paint_event(event);
  83. Painter painter(*this);
  84. painter.add_clip_rect(event.rect());
  85. if (m_icon) {
  86. if (m_should_stretch_icon) {
  87. painter.draw_scaled_bitmap(frame_inner_rect(), *m_icon, m_icon->rect());
  88. } else {
  89. auto icon_location = frame_inner_rect().center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2));
  90. painter.blit(icon_location, *m_icon, m_icon->rect());
  91. }
  92. }
  93. if (text().is_empty())
  94. return;
  95. auto text_rect = this->text_rect();
  96. if (is_enabled()) {
  97. painter.draw_text(text_rect, text(), m_text_alignment, palette().color(foreground_role()), Gfx::TextElision::Right);
  98. } else {
  99. painter.draw_text(text_rect.translated(1, 1), text(), font(), text_alignment(), Color::White, Gfx::TextElision::Right);
  100. painter.draw_text(text_rect, text(), font(), text_alignment(), Color::from_rgb(0x808080), Gfx::TextElision::Right);
  101. }
  102. }
  103. void Label::size_to_fit()
  104. {
  105. set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  106. set_preferred_size(font().width(m_text), 0);
  107. }
  108. }