Label.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. REGISTER_WIDGET(GUI, Label)
  32. namespace GUI {
  33. Label::Label(String text)
  34. : m_text(move(text))
  35. {
  36. REGISTER_TEXT_ALIGNMENT_PROPERTY("text_alignment", text_alignment, set_text_alignment);
  37. set_frame_thickness(0);
  38. set_frame_shadow(Gfx::FrameShadow::Plain);
  39. set_frame_shape(Gfx::FrameShape::NoFrame);
  40. set_foreground_role(Gfx::ColorRole::WindowText);
  41. REGISTER_STRING_PROPERTY("text", text, set_text);
  42. REGISTER_BOOL_PROPERTY("autosize", is_autosize, set_autosize);
  43. }
  44. Label::~Label()
  45. {
  46. }
  47. void Label::set_autosize(bool autosize)
  48. {
  49. if (m_autosize == autosize)
  50. return;
  51. m_autosize = autosize;
  52. if (m_autosize)
  53. size_to_fit();
  54. }
  55. void Label::set_icon(const Gfx::Bitmap* icon)
  56. {
  57. if (m_icon == icon)
  58. return;
  59. m_icon = icon;
  60. update();
  61. }
  62. void Label::set_text(String text)
  63. {
  64. if (text == m_text)
  65. return;
  66. m_text = move(text);
  67. if (m_autosize)
  68. size_to_fit();
  69. update();
  70. did_change_text();
  71. }
  72. Gfx::IntRect Label::text_rect() const
  73. {
  74. int indent = 0;
  75. if (frame_thickness() > 0)
  76. indent = font().glyph_width('x') / 2;
  77. auto rect = frame_inner_rect();
  78. rect.move_by(indent, 0);
  79. rect.set_width(rect.width() - indent * 2);
  80. return rect;
  81. }
  82. void Label::paint_event(PaintEvent& event)
  83. {
  84. Frame::paint_event(event);
  85. Painter painter(*this);
  86. painter.add_clip_rect(event.rect());
  87. if (m_icon) {
  88. if (m_should_stretch_icon) {
  89. painter.draw_scaled_bitmap(frame_inner_rect(), *m_icon, m_icon->rect());
  90. } else {
  91. auto icon_location = frame_inner_rect().center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2));
  92. painter.blit(icon_location, *m_icon, m_icon->rect());
  93. }
  94. }
  95. if (text().is_empty())
  96. return;
  97. auto text_rect = this->text_rect();
  98. if (is_enabled()) {
  99. painter.draw_text(text_rect, text(), m_text_alignment, palette().color(foreground_role()), Gfx::TextElision::Right);
  100. } else {
  101. painter.draw_text(text_rect.translated(1, 1), text(), font(), text_alignment(), Color::White, Gfx::TextElision::Right);
  102. painter.draw_text(text_rect, text(), font(), text_alignment(), Color::from_rgb(0x808080), Gfx::TextElision::Right);
  103. }
  104. }
  105. void Label::size_to_fit()
  106. {
  107. set_fixed_width(font().width(m_text));
  108. }
  109. }