Label.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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(Widget* parent)
  33. : Frame(parent)
  34. {
  35. }
  36. Label::Label(const StringView& text, Widget* parent)
  37. : Frame(parent)
  38. , m_text(text)
  39. {
  40. }
  41. Label::~Label()
  42. {
  43. }
  44. void Label::set_icon(Gfx::Bitmap* icon)
  45. {
  46. if (m_icon == icon)
  47. return;
  48. m_icon = icon;
  49. update();
  50. }
  51. void Label::set_text(const StringView& text)
  52. {
  53. if (text == m_text)
  54. return;
  55. m_text = text;
  56. update();
  57. }
  58. void Label::paint_event(PaintEvent& event)
  59. {
  60. Frame::paint_event(event);
  61. Painter painter(*this);
  62. painter.add_clip_rect(event.rect());
  63. if (m_icon) {
  64. if (m_should_stretch_icon) {
  65. painter.draw_scaled_bitmap(frame_inner_rect(), *m_icon, m_icon->rect());
  66. } else {
  67. auto icon_location = frame_inner_rect().center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2));
  68. painter.blit(icon_location, *m_icon, m_icon->rect());
  69. }
  70. }
  71. if (text().is_empty())
  72. return;
  73. int indent = 0;
  74. if (frame_thickness() > 0)
  75. indent = font().glyph_width('x') / 2;
  76. auto text_rect = frame_inner_rect();
  77. text_rect.move_by(indent, 0);
  78. text_rect.set_width(text_rect.width() - indent * 2);
  79. if (is_enabled()) {
  80. painter.draw_text(text_rect, text(), m_text_alignment, palette().window_text(), Gfx::TextElision::Right);
  81. } else {
  82. painter.draw_text(text_rect.translated(1, 1), text(), font(), text_alignment(), Color::White, Gfx::TextElision::Right);
  83. painter.draw_text(text_rect, text(), font(), text_alignment(), Color::from_rgb(0x808080), Gfx::TextElision::Right);
  84. }
  85. }
  86. void Label::size_to_fit()
  87. {
  88. set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  89. set_preferred_size(font().width(m_text), 0);
  90. }
  91. }