GLabel.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <LibDraw/GraphicsBitmap.h>
  2. #include <LibDraw/Palette.h>
  3. #include <LibGUI/GLabel.h>
  4. #include <LibGUI/GPainter.h>
  5. GLabel::GLabel(GWidget* parent)
  6. : GFrame(parent)
  7. {
  8. }
  9. GLabel::GLabel(const StringView& text, GWidget* parent)
  10. : GFrame(parent)
  11. , m_text(text)
  12. {
  13. }
  14. GLabel::~GLabel()
  15. {
  16. }
  17. void GLabel::set_icon(GraphicsBitmap* icon)
  18. {
  19. if (m_icon == icon)
  20. return;
  21. m_icon = icon;
  22. update();
  23. }
  24. void GLabel::set_text(const StringView& text)
  25. {
  26. if (text == m_text)
  27. return;
  28. m_text = text;
  29. update();
  30. }
  31. void GLabel::paint_event(GPaintEvent& event)
  32. {
  33. GFrame::paint_event(event);
  34. GPainter painter(*this);
  35. painter.add_clip_rect(event.rect());
  36. if (m_icon) {
  37. if (m_should_stretch_icon) {
  38. painter.draw_scaled_bitmap(frame_inner_rect(), *m_icon, m_icon->rect());
  39. } else {
  40. auto icon_location = frame_inner_rect().center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2));
  41. painter.blit(icon_location, *m_icon, m_icon->rect());
  42. }
  43. }
  44. if (text().is_empty())
  45. return;
  46. int indent = 0;
  47. if (frame_thickness() > 0)
  48. indent = font().glyph_width('x') / 2;
  49. auto text_rect = frame_inner_rect();
  50. text_rect.move_by(indent, 0);
  51. text_rect.set_width(text_rect.width() - indent * 2);
  52. if (is_enabled()) {
  53. painter.draw_text(text_rect, text(), m_text_alignment, palette().window_text(), TextElision::Right);
  54. } else {
  55. painter.draw_text(text_rect.translated(1, 1), text(), font(), text_alignment(), Color::White, TextElision::Right);
  56. painter.draw_text(text_rect, text(), font(), text_alignment(), Color::from_rgb(0x808080), TextElision::Right);
  57. }
  58. }
  59. void GLabel::size_to_fit()
  60. {
  61. set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  62. set_preferred_size(font().width(m_text), 0);
  63. }