Serenity Operating System
at hosted 102 lines 3.2 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/String.h> 28#include <LibGUI/Icon.h> 29#include <LibGfx/Bitmap.h> 30 31namespace GUI { 32 33Icon::Icon() 34 : m_impl(IconImpl::create()) 35{ 36} 37 38Icon::Icon(const IconImpl& impl) 39 : m_impl(const_cast<IconImpl&>(impl)) 40{ 41} 42 43Icon::Icon(const Icon& other) 44 : m_impl(other.m_impl) 45{ 46} 47 48Icon::Icon(RefPtr<Gfx::Bitmap>&& bitmap) 49 : Icon() 50{ 51 if (bitmap) { 52 ASSERT(bitmap->width() == bitmap->height()); 53 int size = bitmap->width(); 54 set_bitmap_for_size(size, move(bitmap)); 55 } 56} 57 58Icon::Icon(RefPtr<Gfx::Bitmap>&& bitmap1, RefPtr<Gfx::Bitmap>&& bitmap2) 59 : Icon(move(bitmap1)) 60{ 61 if (bitmap2) { 62 ASSERT(bitmap2->width() == bitmap2->height()); 63 int size = bitmap2->width(); 64 set_bitmap_for_size(size, move(bitmap2)); 65 } 66} 67 68const Gfx::Bitmap* IconImpl::bitmap_for_size(int size) const 69{ 70 auto it = m_bitmaps.find(size); 71 if (it != m_bitmaps.end()) 72 return it->value.ptr(); 73 74 int best_diff_so_far = INT32_MAX; 75 const Gfx::Bitmap* best_fit = nullptr; 76 for (auto& it : m_bitmaps) { 77 int abs_diff = abs(it.key - size); 78 if (abs_diff < best_diff_so_far) { 79 best_diff_so_far = abs_diff; 80 best_fit = it.value.ptr(); 81 } 82 } 83 return best_fit; 84} 85 86void IconImpl::set_bitmap_for_size(int size, RefPtr<Gfx::Bitmap>&& bitmap) 87{ 88 if (!bitmap) { 89 m_bitmaps.remove(size); 90 return; 91 } 92 m_bitmaps.set(size, move(bitmap)); 93} 94 95Icon Icon::default_icon(const StringView& name) 96{ 97 auto bitmap16 = Gfx::Bitmap::load_from_file(String::format("/res/icons/16x16/%s.png", String(name).characters())); 98 auto bitmap32 = Gfx::Bitmap::load_from_file(String::format("/res/icons/32x32/%s.png", String(name).characters())); 99 return Icon(move(bitmap16), move(bitmap32)); 100} 101 102}