Serenity Operating System
at portability 87 lines 2.9 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 <AK/Assertions.h> 28#include <AK/StringBuilder.h> 29#include <LibGUI/Painter.h> 30#include <LibGUI/ProgressBar.h> 31#include <LibGfx/Palette.h> 32 33namespace GUI { 34 35ProgressBar::ProgressBar() 36{ 37} 38 39ProgressBar::~ProgressBar() 40{ 41} 42 43void ProgressBar::set_value(int value) 44{ 45 if (m_value == value) 46 return; 47 m_value = value; 48 update(); 49} 50 51void ProgressBar::set_range(int min, int max) 52{ 53 ASSERT(min < max); 54 m_min = min; 55 m_max = max; 56 m_value = clamp(m_value, m_min, m_max); 57} 58 59void ProgressBar::paint_event(PaintEvent& event) 60{ 61 Frame::paint_event(event); 62 63 Painter painter(*this); 64 auto rect = frame_inner_rect(); 65 painter.add_clip_rect(rect); 66 painter.add_clip_rect(event.rect()); 67 68 String progress_text; 69 if (m_format != Format::NoText) { 70 // Then we draw the progress text over the gradient. 71 // We draw it twice, once offset (1, 1) for a drop shadow look. 72 StringBuilder builder; 73 builder.append(m_caption); 74 if (m_format == Format::Percentage) { 75 float range_size = m_max - m_min; 76 float progress = (m_value - m_min) / range_size; 77 builder.appendf("%d%%", (int)(progress * 100)); 78 } else if (m_format == Format::ValueSlashMax) { 79 builder.appendf("%d/%d", m_value, m_max); 80 } 81 progress_text = builder.to_string(); 82 } 83 84 Gfx::StylePainter::paint_progress_bar(painter, rect, palette(), m_min, m_max, m_value, progress_text); 85} 86 87}