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