Label.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. namespace GUI {
  10. class Label : public Frame {
  11. C_OBJECT(Label);
  12. public:
  13. virtual ~Label() override;
  14. String text() const { return m_text; }
  15. void set_text(String);
  16. void set_icon(const Gfx::Bitmap*);
  17. const Gfx::Bitmap* icon() const { return m_icon.ptr(); }
  18. Gfx::Bitmap* icon() { return m_icon.ptr(); }
  19. Gfx::TextAlignment text_alignment() const { return m_text_alignment; }
  20. void set_text_alignment(Gfx::TextAlignment text_alignment) { m_text_alignment = text_alignment; }
  21. bool should_stretch_icon() const { return m_should_stretch_icon; }
  22. void set_should_stretch_icon(bool b) { m_should_stretch_icon = b; }
  23. bool is_autosize() const { return m_autosize; }
  24. void set_autosize(bool);
  25. bool is_word_wrap() const { return m_word_wrap; }
  26. void set_word_wrap(bool);
  27. Gfx::IntRect text_rect(size_t line = 0) const;
  28. protected:
  29. explicit Label(String text = {});
  30. virtual void paint_event(PaintEvent&) override;
  31. virtual void did_change_text() { }
  32. private:
  33. void size_to_fit();
  34. void wrap_text();
  35. String m_text;
  36. RefPtr<Gfx::Bitmap> m_icon;
  37. Gfx::TextAlignment m_text_alignment { Gfx::TextAlignment::Center };
  38. bool m_should_stretch_icon { false };
  39. bool m_autosize { false };
  40. bool m_word_wrap { false };
  41. Vector<String> m_lines;
  42. };
  43. }