Serenity Operating System
at master 65 lines 2.0 kB view raw
1/* 2 * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org> 3 * Copyright (c) 2022, the SerenityOS developers. 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#pragma once 9 10#include <LibGUI/Frame.h> 11#include <LibGfx/TextAlignment.h> 12#include <LibGfx/TextWrapping.h> 13 14namespace GUI { 15 16class Label : public Frame { 17 C_OBJECT(Label); 18 19public: 20 virtual ~Label() override = default; 21 22 DeprecatedString text() const { return m_text; } 23 void set_text(DeprecatedString); 24 25 void set_icon(Gfx::Bitmap const*); 26 void set_icon_from_path(DeprecatedString const&); 27 Gfx::Bitmap const* icon() const { return m_icon.ptr(); } 28 29 Gfx::TextAlignment text_alignment() const { return m_text_alignment; } 30 void set_text_alignment(Gfx::TextAlignment text_alignment) { m_text_alignment = text_alignment; } 31 32 Gfx::TextWrapping text_wrapping() const { return m_text_wrapping; } 33 void set_text_wrapping(Gfx::TextWrapping text_wrapping) { m_text_wrapping = text_wrapping; } 34 35 bool should_stretch_icon() const { return m_should_stretch_icon; } 36 void set_should_stretch_icon(bool b) { m_should_stretch_icon = b; } 37 38 bool is_autosize() const { return m_autosize; } 39 void set_autosize(bool, size_t padding = 0); 40 41 virtual Optional<UISize> calculated_preferred_size() const override; 42 int text_calculated_preferred_height() const; 43 int text_calculated_preferred_width() const; 44 45 Gfx::IntRect text_rect() const; 46 47protected: 48 explicit Label(DeprecatedString text = {}); 49 50 virtual void paint_event(PaintEvent&) override; 51 virtual void did_change_text() { } 52 53private: 54 void size_to_fit(); 55 56 DeprecatedString m_text; 57 RefPtr<Gfx::Bitmap const> m_icon; 58 Gfx::TextAlignment m_text_alignment { Gfx::TextAlignment::Center }; 59 Gfx::TextWrapping m_text_wrapping { Gfx::TextWrapping::Wrap }; 60 bool m_should_stretch_icon { false }; 61 bool m_autosize { false }; 62 size_t m_autosize_padding { 0 }; 63}; 64 65}