Label.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. void Label::paint_event(PaintEvent& event)
  71. {
  72. Frame::paint_event(event);
  73. Painter painter(*this);
  74. painter.add_clip_rect(event.rect());
  75. if (m_icon) {
  76. if (m_should_stretch_icon) {
  77. painter.draw_scaled_bitmap(frame_inner_rect(), *m_icon, m_icon->rect());
  78. } else {
  79. auto icon_location = frame_inner_rect().center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2));
  80. painter.blit(icon_location, *m_icon, m_icon->rect());
  81. }
  82. }
  83. if (text().is_empty())
  84. return;
  85. int indent = 0;
  86. if (frame_thickness() > 0)
  87. indent = font().glyph_width('x') / 2;
  88. auto text_rect = frame_inner_rect();
  89. text_rect.move_by(indent, 0);
  90. text_rect.set_width(text_rect.width() - indent * 2);
  91. if (is_enabled()) {
  92. painter.draw_text(text_rect, text(), m_text_alignment, palette().color(foreground_role()), Gfx::TextElision::Right);
  93. } else {
  94. painter.draw_text(text_rect.translated(1, 1), text(), font(), text_alignment(), Color::White, Gfx::TextElision::Right);
  95. painter.draw_text(text_rect, text(), font(), text_alignment(), Color::from_rgb(0x808080), Gfx::TextElision::Right);
  96. }
  97. }
  98. void Label::size_to_fit()
  99. {
  100. set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  101. set_preferred_size(font().width(m_text), 0);
  102. }
  103. }