bump product version to 4.1.6.2
[LibreOffice.git] / sfx2 / source / sidebar / Tools.cxx
blob62c244cd6045409e8968fe0a1e7585a6e6625961
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #include "sfx2/sidebar/Tools.hxx"
21 #include "sfx2/sidebar/Theme.hxx"
23 #include "sfx2/imagemgr.hxx"
24 #include <comphelper/processfactory.hxx>
25 #include <comphelper/componentcontext.hxx>
26 #include <comphelper/namedvaluecollection.hxx>
27 #include <vcl/gradient.hxx>
29 #include <com/sun/star/frame/XDispatchProvider.hpp>
30 #include <com/sun/star/graphic/XGraphicProvider.hpp>
31 #include <com/sun/star/util/XURLTransformer.hpp>
32 #include <com/sun/star/frame/XModuleManager.hpp>
34 #include <cstring>
36 using namespace css;
37 using namespace cssu;
40 namespace sfx2 { namespace sidebar {
42 Image Tools::GetImage (
43 const ::rtl::OUString& rsImageURL,
44 const ::rtl::OUString& rsHighContrastImageURL,
45 const Reference<frame::XFrame>& rxFrame)
47 if (Theme::IsHighContrastMode())
48 return GetImage(rsHighContrastImageURL, rxFrame);
49 else
50 return GetImage(rsImageURL, rxFrame);
56 Image Tools::GetImage (
57 const ::rtl::OUString& rsURL,
58 const Reference<frame::XFrame>& rxFrame)
60 if (rsURL.getLength() > 0)
62 const sal_Char sUnoCommandPrefix[] = ".uno:";
63 const sal_Char sCommandImagePrefix[] = "private:commandimage/";
64 const sal_Int32 nCommandImagePrefixLength = strlen(sCommandImagePrefix);
66 if (rsURL.startsWith(sUnoCommandPrefix))
68 const Image aPanelImage (::GetImage(rxFrame, rsURL, sal_False));
69 return aPanelImage;
71 else if (rsURL.startsWith(sCommandImagePrefix))
73 ::rtl::OUStringBuffer aCommandName;
74 aCommandName.appendAscii(sUnoCommandPrefix);
75 aCommandName.append(rsURL.copy(nCommandImagePrefixLength));
76 const ::rtl::OUString sCommandName (aCommandName.makeStringAndClear());
78 const Image aPanelImage (::GetImage(rxFrame, sCommandName, sal_False));
79 return aPanelImage;
81 else
83 const ::comphelper::ComponentContext aContext (::comphelper::getProcessServiceFactory());
84 const Reference<graphic::XGraphicProvider> xGraphicProvider (
85 aContext.createComponent("com.sun.star.graphic.GraphicProvider"),
86 UNO_QUERY);
87 if ( xGraphicProvider.is())
89 ::comphelper::NamedValueCollection aMediaProperties;
90 aMediaProperties.put("URL", rsURL);
91 const Reference<graphic::XGraphic> xGraphic (
92 xGraphicProvider->queryGraphic(aMediaProperties.getPropertyValues()),
93 UNO_QUERY);
94 if (xGraphic.is())
95 return Image(xGraphic);
99 return Image();
105 css::awt::Gradient Tools::VclToAwtGradient (const Gradient aVclGradient)
107 css::awt::Gradient aAwtGradient (
108 awt::GradientStyle(aVclGradient.GetStyle()),
109 aVclGradient.GetStartColor().GetRGBColor(),
110 aVclGradient.GetEndColor().GetRGBColor(),
111 aVclGradient.GetAngle(),
112 aVclGradient.GetBorder(),
113 aVclGradient.GetOfsX(),
114 aVclGradient.GetOfsY(),
115 aVclGradient.GetStartIntensity(),
116 aVclGradient.GetEndIntensity(),
117 aVclGradient.GetSteps());
118 return aAwtGradient;
124 Gradient Tools::AwtToVclGradient (const css::awt::Gradient aAwtGradient)
126 Gradient aVclGradient (
127 GradientStyle(aAwtGradient.Style),
128 aAwtGradient.StartColor,
129 aAwtGradient.EndColor);
130 aVclGradient.SetAngle(aAwtGradient.Angle);
131 aVclGradient.SetBorder(aAwtGradient.Border);
132 aVclGradient.SetOfsX(aAwtGradient.XOffset);
133 aVclGradient.SetOfsY(aAwtGradient.YOffset);
134 aVclGradient.SetStartIntensity(aAwtGradient.StartIntensity);
135 aVclGradient.SetEndIntensity(aAwtGradient.EndIntensity);
136 aVclGradient.SetSteps(aAwtGradient.StepCount);
138 return aVclGradient;
144 SvBorder Tools::RectangleToSvBorder (const Rectangle aBox)
146 return SvBorder(
147 aBox.Left(),
148 aBox.Top(),
149 aBox.Right(),
150 aBox.Bottom());
156 util::URL Tools::GetURL (const ::rtl::OUString& rsCommand)
158 util::URL aURL;
159 aURL.Complete = rsCommand;
161 const ::comphelper::ComponentContext aComponentContext (::comphelper::getProcessServiceFactory());
162 const Reference<util::XURLTransformer> xParser (
163 aComponentContext.createComponent("com.sun.star.util.URLTransformer"),
164 UNO_QUERY_THROW);
165 xParser->parseStrict(aURL);
167 return aURL;
173 Reference<frame::XDispatch> Tools::GetDispatch (
174 const cssu::Reference<css::frame::XFrame>& rxFrame,
175 const util::URL& rURL)
177 Reference<frame::XDispatchProvider> xProvider (rxFrame, UNO_QUERY_THROW);
178 Reference<frame::XDispatch> xDispatch (xProvider->queryDispatch(rURL, ::rtl::OUString(), 0));
179 return xDispatch;
185 ::rtl::OUString Tools::GetModuleName (
186 const cssu::Reference<css::frame::XFrame>& rxFrame)
188 if ( ! rxFrame.is() || ! rxFrame->getController().is())
189 return ::rtl::OUString();
193 const ::comphelper::ComponentContext aContext (::comphelper::getProcessServiceFactory());
194 const Reference<frame::XModuleManager> xModuleManager (
195 aContext.createComponent("com.sun.star.frame.ModuleManager"),
196 UNO_QUERY_THROW);
197 return xModuleManager->identify(rxFrame);
199 catch (const Exception&)
201 // Ignored.
203 return ::rtl::OUString();
207 } } // end of namespace sfx2::sidebar