Label.h 1.8 KB

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