Label.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. void set_icon_from_path(String const&);
  19. const Gfx::Bitmap* icon() const { return m_icon.ptr(); }
  20. Gfx::Bitmap* icon() { return m_icon.ptr(); }
  21. Gfx::TextAlignment text_alignment() const { return m_text_alignment; }
  22. void set_text_alignment(Gfx::TextAlignment text_alignment) { m_text_alignment = text_alignment; }
  23. Gfx::TextWrapping text_wrapping() const { return m_text_wrapping; }
  24. void set_text_wrapping(Gfx::TextWrapping text_wrapping) { m_text_wrapping = text_wrapping; }
  25. bool should_stretch_icon() const { return m_should_stretch_icon; }
  26. void set_should_stretch_icon(bool b) { m_should_stretch_icon = b; }
  27. bool is_autosize() const { return m_autosize; }
  28. void set_autosize(bool);
  29. int preferred_height() const;
  30. Gfx::IntRect text_rect() const;
  31. protected:
  32. explicit Label(String text = {});
  33. virtual void paint_event(PaintEvent&) override;
  34. virtual void did_change_text() { }
  35. private:
  36. void size_to_fit();
  37. String m_text;
  38. RefPtr<Gfx::Bitmap> m_icon;
  39. Gfx::TextAlignment m_text_alignment { Gfx::TextAlignment::Center };
  40. Gfx::TextWrapping m_text_wrapping { Gfx::TextWrapping::Wrap };
  41. bool m_should_stretch_icon { false };
  42. bool m_autosize { false };
  43. };
  44. }