Serenity Operating System
at hosted 101 lines 3.3 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <LibGUI/Label.h> 28#include <LibGUI/Painter.h> 29#include <LibGfx/Bitmap.h> 30#include <LibGfx/Font.h> 31#include <LibGfx/Palette.h> 32 33namespace GUI { 34 35Label::Label(const StringView& text) 36 : m_text(text) 37{ 38 set_frame_thickness(0); 39 set_frame_shadow(Gfx::FrameShadow::Plain); 40 set_frame_shape(Gfx::FrameShape::NoFrame); 41} 42 43Label::~Label() 44{ 45} 46 47void Label::set_icon(const Gfx::Bitmap* icon) 48{ 49 if (m_icon == icon) 50 return; 51 m_icon = icon; 52 update(); 53} 54 55void Label::set_text(const StringView& text) 56{ 57 if (text == m_text) 58 return; 59 m_text = text; 60 update(); 61} 62 63void Label::paint_event(PaintEvent& event) 64{ 65 Frame::paint_event(event); 66 67 Painter painter(*this); 68 painter.add_clip_rect(event.rect()); 69 70 if (m_icon) { 71 if (m_should_stretch_icon) { 72 painter.draw_scaled_bitmap(frame_inner_rect(), *m_icon, m_icon->rect()); 73 } else { 74 auto icon_location = frame_inner_rect().center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2)); 75 painter.blit(icon_location, *m_icon, m_icon->rect()); 76 } 77 } 78 if (text().is_empty()) 79 return; 80 int indent = 0; 81 if (frame_thickness() > 0) 82 indent = font().glyph_width('x') / 2; 83 auto text_rect = frame_inner_rect(); 84 text_rect.move_by(indent, 0); 85 text_rect.set_width(text_rect.width() - indent * 2); 86 87 if (is_enabled()) { 88 painter.draw_text(text_rect, text(), m_text_alignment, palette().window_text(), Gfx::TextElision::Right); 89 } else { 90 painter.draw_text(text_rect.translated(1, 1), text(), font(), text_alignment(), Color::White, Gfx::TextElision::Right); 91 painter.draw_text(text_rect, text(), font(), text_alignment(), Color::from_rgb(0x808080), Gfx::TextElision::Right); 92 } 93} 94 95void Label::size_to_fit() 96{ 97 set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); 98 set_preferred_size(font().width(m_text), 0); 99} 100 101}