Label.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibGUI/Frame.h>
  8. #include <LibGfx/TextAlignment.h>
  9. #include <LibGfx/TextWrapping.h>
  10. namespace GUI {
  11. class Label : public Frame {
  12. C_OBJECT(Label);
  13. public:
  14. virtual ~Label() override;
  15. String text() const { return m_text; }
  16. void set_text(String);
  17. void set_icon(const Gfx::Bitmap*);
  18. const Gfx::Bitmap* icon() const { return m_icon.ptr(); }
  19. Gfx::Bitmap* icon() { return m_icon.ptr(); }
  20. Gfx::TextAlignment text_alignment() const { return m_text_alignment; }
  21. void set_text_alignment(Gfx::TextAlignment text_alignment) { m_text_alignment = text_alignment; }
  22. Gfx::TextWrapping text_wrapping() const { return m_text_wrapping; }
  23. void set_text_wrapping(Gfx::TextWrapping text_wrapping) { m_text_wrapping = text_wrapping; }
  24. bool should_stretch_icon() const { return m_should_stretch_icon; }
  25. void set_should_stretch_icon(bool b) { m_should_stretch_icon = b; }
  26. bool is_autosize() const { return m_autosize; }
  27. void set_autosize(bool);
  28. int preferred_height() const;
  29. Gfx::IntRect text_rect() const;
  30. protected:
  31. explicit Label(String text = {});
  32. virtual void paint_event(PaintEvent&) override;
  33. virtual void did_change_text() { }
  34. private:
  35. void size_to_fit();
  36. String m_text;
  37. RefPtr<Gfx::Bitmap> m_icon;
  38. Gfx::TextAlignment m_text_alignment { Gfx::TextAlignment::Center };
  39. Gfx::TextWrapping m_text_wrapping { Gfx::TextWrapping::Wrap };
  40. bool m_should_stretch_icon { false };
  41. bool m_autosize { false };
  42. };
  43. }