GLabel.cpp 3.2 KB

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