1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <BitmapDisabledImageFilter.hxx>
27 #include <comphelper/lok.hxx>
31 ImplImage::ImplImage(const BitmapEx
&rBitmapEx
)
33 , maSizePixel(rBitmapEx
.GetSizePixel())
34 , maPreferedSizePixel()
35 , maBitmapEx(rBitmapEx
)
39 ImplImage::ImplImage(const OUString
&aStockName
, Size
const & rPreferedSize
)
41 , maSizePixel() // defer size lookup
42 , maPreferedSizePixel(rPreferedSize
)
43 , maStockName(aStockName
)
47 bool ImplImage::loadStockAtScale(double fScale
, BitmapEx
&rBitmapEx
)
51 ImageLoadFlags eScalingFlags
= ImageLoadFlags::NONE
;
52 sal_Int32 nScalePercentage
= -1;
54 if (comphelper::LibreOfficeKit::isActive()) // scale at the surface
56 nScalePercentage
= fScale
* 100.0;
57 eScalingFlags
= ImageLoadFlags::IgnoreScalingFactor
;
60 OUString aIconTheme
= Application::GetSettings().GetStyleSettings().DetermineIconTheme();
61 if (!ImageTree::get().loadImage(maStockName
, aIconTheme
, aBitmapEx
, true,
62 nScalePercentage
, eScalingFlags
))
64 /* If the uno command has parameters, passed in from a toolbar,
65 * recover from failure by removing the parameters from the file name
67 if (maStockName
.indexOf("%3f") > 0)
69 sal_Int32 nStart
= maStockName
.indexOf("%3f");
70 sal_Int32 nEnd
= maStockName
.lastIndexOf(".");
72 OUString aFileName
= maStockName
.replaceAt(nStart
, nEnd
- nStart
, "");
73 if (!ImageTree::get().loadImage(aFileName
, aIconTheme
, aBitmapEx
, true,
74 nScalePercentage
, eScalingFlags
))
76 SAL_WARN("vcl", "Failed to load scaled image from " << maStockName
<<
77 " and " << aFileName
<< " at " << fScale
);
82 if (maPreferedSizePixel
!= Size())
84 Size
aScaleSize(maPreferedSizePixel
.Width() * fScale
, maPreferedSizePixel
.Height() * fScale
);
85 aBitmapEx
.Scale(aScaleSize
);
87 rBitmapEx
= aBitmapEx
;
91 Size
ImplImage::getSizePixel()
98 if (loadStockAtScale(1.0, maBitmapEx
))
100 assert(!maDisabledBitmapEx
);
101 assert(maBitmapChecksum
== 0);
102 maSizePixel
= maBitmapEx
.GetSizePixel();
106 SAL_WARN("vcl", "Failed to load stock icon " << maStockName
);
111 /// non-HiDPI compatibility method.
112 BitmapEx
const & ImplImage::getBitmapEx(bool bDisabled
)
114 getSizePixel(); // force load, and at unity scale.
117 // Changed since we last generated this.
118 BitmapChecksum aChecksum
= maBitmapEx
.GetChecksum();
119 if (maBitmapChecksum
!= aChecksum
||
120 maDisabledBitmapEx
.GetSizePixel() != maBitmapEx
.GetSizePixel())
122 maDisabledBitmapEx
= maBitmapEx
;
123 BitmapFilter::Filter(maDisabledBitmapEx
, BitmapDisabledImageFilter());
124 maBitmapChecksum
= aChecksum
;
126 return maDisabledBitmapEx
;
132 bool ImplImage::isEqual(const ImplImage
&ref
) const
134 if (isStock() != ref
.isStock())
137 return maStockName
== ref
.maStockName
;
139 return maBitmapEx
== ref
.maBitmapEx
;
142 BitmapEx
const & ImplImage::getBitmapExForHiDPI(bool bDisabled
)
145 { // check we have the right bitmap cached.
146 // FIXME: DPI scaling should be tied to the outdev really ...
147 double fScale
= comphelper::LibreOfficeKit::getDPIScale();
148 Size
aTarget(maSizePixel
.Width()*fScale
,
149 maSizePixel
.Height()*fScale
);
150 if (maBitmapEx
.GetSizePixel() != aTarget
)
151 loadStockAtScale(fScale
, maBitmapEx
);
153 return getBitmapEx(bDisabled
);
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */