File indexing completed on 2024-04-21 05:46:58

0001 /*****************************************************************************
0002  *   Copyright 2003 - 2010 Craig Drummond <craig.p.drummond@gmail.com>       *
0003  *   Copyright 2013 - 2015 Yichao Yu <yyc1992@gmail.com>                     *
0004  *                                                                           *
0005  *   This program is free software; you can redistribute it and/or modify    *
0006  *   it under the terms of the GNU Lesser General Public License as          *
0007  *   published by the Free Software Foundation; either version 2.1 of the    *
0008  *   License, or (at your option) version 3, or any later version accepted   *
0009  *   by the membership of KDE e.V. (or its successor approved by the         *
0010  *   membership of KDE e.V.), which shall act as a proxy defined in          *
0011  *   Section 6 of version 3 of the license.                                  *
0012  *                                                                           *
0013  *   This program is distributed in the hope that it will be useful,         *
0014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
0015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       *
0016  *   Lesser General Public License for more details.                         *
0017  *                                                                           *
0018  *   You should have received a copy of the GNU Lesser General Public        *
0019  *   License along with this library. If not,                                *
0020  *   see <http://www.gnu.org/licenses/>.                                     *
0021  *****************************************************************************/
0022 
0023 #include "pixcache.h"
0024 #include "check_on-png.h"
0025 #include "check_x_on-png.h"
0026 #include "blank16x16-png.h"
0027 #include "qt_settings.h"
0028 
0029 #include <qtcurve-utils/gtkutils.h>
0030 
0031 #include <unordered_map>
0032 
0033 namespace QtCurve {
0034 
0035 struct PixKey {
0036     GdkColor col;
0037     double shade;
0038 };
0039 
0040 struct PixHash {
0041     size_t
0042     operator()(const PixKey &key) const
0043     {
0044         const GdkColor &col = key.col;
0045         return (std::hash<int>()(col.red) ^
0046                 (std::hash<int>()(col.green) << 1) ^
0047                 (std::hash<int>()(col.blue) << 2) ^
0048                 (std::hash<double>()(key.shade) << 3));
0049     }
0050 };
0051 
0052 struct PixEqual {
0053     bool
0054     operator()(const PixKey &lhs, const PixKey &rhs) const
0055     {
0056         return memcmp(&lhs, &rhs, sizeof(PixKey)) == 0;
0057     }
0058 };
0059 
0060 static std::unordered_map<PixKey, GObjPtr<GdkPixbuf>,
0061                           PixHash, PixEqual> pixbufMap;
0062 #pragma GCC diagnostic push
0063 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
0064 // Replacement isn't available until the version it is deprecated
0065 // (gdk-pixbuf-2.0 2.32)
0066 static GObjPtr<GdkPixbuf> blankPixbuf =
0067     gdk_pixbuf_new_from_inline(-1, blank16x16, true, nullptr);
0068 #pragma GCC diagnostic pop
0069 
0070 static GdkPixbuf*
0071 pixbufCacheValueNew(const PixKey &key)
0072 {
0073 #pragma GCC diagnostic push
0074 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
0075     // Replacement isn't available until the version it is deprecated
0076     // (gdk-pixbuf-2.0 2.32)
0077     GdkPixbuf *res = gdk_pixbuf_new_from_inline(-1, opts.xCheck ? check_x_on :
0078                                                 check_on, true, nullptr);
0079 #pragma GCC diagnostic pop
0080     qtcAdjustPix(gdk_pixbuf_get_pixels(res), gdk_pixbuf_get_n_channels(res),
0081                  gdk_pixbuf_get_width(res), gdk_pixbuf_get_height(res),
0082                  gdk_pixbuf_get_rowstride(res),
0083                  key.col.red >> 8, key.col.green >> 8,
0084                  key.col.blue >> 8, key.shade, QTC_PIXEL_GDK);
0085     return res;
0086 }
0087 
0088 GdkPixbuf*
0089 getPixbuf(GdkColor *widgetColor, EPixmap p, double shade)
0090 {
0091     if (p != PIX_CHECK) {
0092         return blankPixbuf.get();
0093     }
0094     const PixKey key = {*widgetColor, shade};
0095     auto &pixbuf = pixbufMap[key];
0096     if (pixbuf.get() == nullptr) {
0097         pixbuf = pixbufCacheValueNew(key);
0098     }
0099     return pixbuf.get();
0100 }
0101 
0102 }