bump product version to 7.2.5.1
[LibreOffice.git] / vcl / source / image / ImplImage.cxx
blobba8b85802ac912a83a23b14c251da5fdeb438bd0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/log.hxx>
21 #include <vcl/svapp.hxx>
22 #include <vcl/bitmapex.hxx>
23 #include <vcl/settings.hxx>
24 #include <vcl/BitmapFilter.hxx>
25 #include <vcl/ImageTree.hxx>
26 #include <bitmap/BitmapDisabledImageFilter.hxx>
27 #include <comphelper/lok.hxx>
29 #include <image.h>
31 ImplImage::ImplImage(const BitmapEx &rBitmapEx)
32 : maBitmapChecksum(0)
33 , maSizePixel(rBitmapEx.GetSizePixel())
34 , maBitmapEx(rBitmapEx)
38 ImplImage::ImplImage(const OUString &aStockName)
39 : maBitmapChecksum(0)
40 , maSizePixel() // defer size lookup
41 , maStockName(aStockName)
45 bool ImplImage::loadStockAtScale(double fScale, BitmapEx &rBitmapEx)
47 BitmapEx aBitmapEx;
49 ImageLoadFlags eScalingFlags = ImageLoadFlags::NONE;
50 sal_Int32 nScalePercentage = -1;
52 if (comphelper::LibreOfficeKit::isActive()) // scale at the surface
54 nScalePercentage = fScale * 100.0;
55 eScalingFlags = ImageLoadFlags::IgnoreScalingFactor;
58 OUString aIconTheme = Application::GetSettings().GetStyleSettings().DetermineIconTheme();
59 if (!ImageTree::get().loadImage(maStockName, aIconTheme, aBitmapEx, true,
60 nScalePercentage, eScalingFlags))
62 /* If the uno command has parameters, passed in from a toolbar,
63 * recover from failure by removing the parameters from the file name
65 if (maStockName.indexOf("%3f") > 0)
67 sal_Int32 nStart = maStockName.indexOf("%3f");
68 sal_Int32 nEnd = maStockName.lastIndexOf(".");
70 OUString aFileName = maStockName.replaceAt(nStart, nEnd - nStart, "");
71 if (!ImageTree::get().loadImage(aFileName, aIconTheme, aBitmapEx, true,
72 nScalePercentage, eScalingFlags))
74 SAL_WARN("vcl", "Failed to load scaled image from " << maStockName <<
75 " and " << aFileName << " at " << fScale);
76 return false;
79 else
81 SAL_WARN("vcl", "Failed to load scaled image from " << maStockName <<
82 " at " << fScale);
83 return false;
86 rBitmapEx = aBitmapEx;
87 return true;
90 Size ImplImage::getSizePixel()
92 Size aRet;
93 if (!isSizeEmpty())
94 aRet = maSizePixel;
95 else if (isStock())
97 if (loadStockAtScale(1.0, maBitmapEx))
99 assert(maDisabledBitmapEx.IsEmpty());
100 assert(maBitmapChecksum == 0);
101 maSizePixel = maBitmapEx.GetSizePixel();
102 aRet = maSizePixel;
104 else
105 SAL_WARN("vcl", "Failed to load stock icon " << maStockName);
107 return aRet;
110 /// non-HiDPI compatibility method.
111 BitmapEx const & ImplImage::getBitmapEx(bool bDisabled)
113 getSizePixel(); // force load, and at unity scale.
114 if (bDisabled)
116 // Changed since we last generated this.
117 BitmapChecksum aChecksum = maBitmapEx.GetChecksum();
118 if (maBitmapChecksum != aChecksum ||
119 maDisabledBitmapEx.GetSizePixel() != maBitmapEx.GetSizePixel())
121 maDisabledBitmapEx = maBitmapEx;
122 BitmapFilter::Filter(maDisabledBitmapEx, BitmapDisabledImageFilter());
123 maBitmapChecksum = aChecksum;
125 return maDisabledBitmapEx;
128 return maBitmapEx;
131 bool ImplImage::isEqual(const ImplImage &ref) const
133 if (isStock() != ref.isStock())
134 return false;
135 if (isStock())
136 return maStockName == ref.maStockName;
137 else
138 return maBitmapEx == ref.maBitmapEx;
141 BitmapEx const & ImplImage::getBitmapExForHiDPI(bool bDisabled)
143 if (isStock())
144 { // check we have the right bitmap cached.
145 // FIXME: DPI scaling should be tied to the outdev really ...
146 double fScale = comphelper::LibreOfficeKit::getDPIScale();
147 Size aTarget(maSizePixel.Width()*fScale,
148 maSizePixel.Height()*fScale);
149 if (maBitmapEx.GetSizePixel() != aTarget)
150 loadStockAtScale(fScale, maBitmapEx);
152 return getBitmapEx(bDisabled);
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */