Label.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. virtual Optional<UISize> calculated_preferred_size() const override;
  31. int text_calculated_preferred_height() const;
  32. Gfx::IntRect text_rect() const;
  33. protected:
  34. explicit Label(String text = {});
  35. virtual void paint_event(PaintEvent&) override;
  36. virtual void did_change_text() { }
  37. private:
  38. void size_to_fit();
  39. String m_text;
  40. RefPtr<Gfx::Bitmap> m_icon;
  41. Gfx::TextAlignment m_text_alignment { Gfx::TextAlignment::Center };
  42. Gfx::TextWrapping m_text_wrapping { Gfx::TextWrapping::Wrap };
  43. bool m_should_stretch_icon { false };
  44. bool m_autosize { false };
  45. size_t m_autosize_padding { 0 };
  46. };
  47. }