Serenity Operating System
at portability 138 lines 4.5 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 "TextWidget.h" 28#include <AK/Optional.h> 29#include <AK/String.h> 30#include <AK/StringBuilder.h> 31#include <AK/Vector.h> 32#include <LibGUI/Painter.h> 33#include <LibGfx/Font.h> 34#include <LibGfx/Palette.h> 35 36TextWidget::TextWidget(const StringView& text) 37 : m_text(text) 38{ 39} 40 41TextWidget::~TextWidget() 42{ 43} 44 45void TextWidget::set_text(const StringView& text) 46{ 47 if (text == m_text) 48 return; 49 m_text = text; 50 wrap_and_set_height(); 51 update(); 52} 53 54void TextWidget::paint_event(GUI::PaintEvent& event) 55{ 56 GUI::Frame::paint_event(event); 57 58 GUI::Painter painter(*this); 59 painter.add_clip_rect(event.rect()); 60 61 int indent = 0; 62 if (frame_thickness() > 0) 63 indent = font().glyph_width('x') / 2; 64 65 for (size_t i = 0; i < m_lines.size(); i++) { 66 auto& line = m_lines[i]; 67 68 auto text_rect = frame_inner_rect(); 69 text_rect.move_by(indent, i * m_line_height); 70 if (!line.is_empty()) 71 text_rect.set_width(text_rect.width() - indent * 2); 72 73 if (is_enabled()) { 74 painter.draw_text(text_rect, line, m_text_alignment, palette().color(foreground_role()), Gfx::TextElision::None); 75 } else { 76 painter.draw_text(text_rect.translated(1, 1), line, font(), text_alignment(), Color::White, Gfx::TextElision::Right); 77 painter.draw_text(text_rect, line, font(), text_alignment(), Color::from_rgb(0x808080), Gfx::TextElision::Right); 78 } 79 } 80} 81 82void TextWidget::resize_event(GUI::ResizeEvent& event) 83{ 84 wrap_and_set_height(); 85 GUI::Widget::resize_event(event); 86} 87 88void TextWidget::wrap_and_set_height() 89{ 90 Vector<String> words; 91 Optional<size_t> start; 92 for (size_t i = 0; i < m_text.length(); i++) { 93 auto ch = m_text[i]; 94 95 if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') { 96 if (start.has_value()) 97 words.append(m_text.substring(start.value(), i - start.value())); 98 start.clear(); 99 } else if (!start.has_value()) { 100 start = i; 101 } 102 } 103 if (start.has_value()) 104 words.append(m_text.substring(start.value(), m_text.length() - start.value())); 105 106 auto rect = frame_inner_rect(); 107 if (frame_thickness() > 0) 108 rect.set_width(rect.width() - font().glyph_width('x')); 109 110 StringBuilder builder; 111 Vector<String> lines; 112 int line_width = 0; 113 for (auto& word : words) { 114 int word_width = font().width(word); 115 if (line_width != 0) 116 word_width += font().glyph_width('x'); 117 118 if (line_width + word_width > rect.width()) { 119 lines.append(builder.to_string()); 120 builder.clear(); 121 line_width = 0; 122 } 123 124 if (line_width != 0) 125 builder.append(' '); 126 builder.append(word); 127 line_width += word_width; 128 } 129 auto last_line = builder.to_string(); 130 if (!last_line.is_empty()) { 131 lines.append(last_line); 132 } 133 134 m_lines = lines; 135 136 set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); 137 set_preferred_size(0, m_lines.size() * m_line_height + frame_thickness() * 2); 138}