Branch libreoffice-5-0-4
[LibreOffice.git] / sfx2 / source / sidebar / Tools.cxx
blobfa9b256f6e0a9e95bcc59d588b2c2a57f0bd9114
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 <sfx2/sidebar/Tools.hxx>
22 #include <sfx2/sidebar/Theme.hxx>
24 #include <sfx2/imagemgr.hxx>
25 #include <comphelper/processfactory.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/GraphicProvider.hpp>
31 #include <com/sun/star/util/URLTransformer.hpp>
32 #include <com/sun/star/frame/ModuleManager.hpp>
34 #include <cstring>
36 using namespace css;
37 using namespace css::uno;
39 namespace sfx2 { namespace sidebar {
41 Image Tools::GetImage (
42 const ::rtl::OUString& rsImageURL,
43 const ::rtl::OUString& rsHighContrastImageURL,
44 const Reference<frame::XFrame>& rxFrame)
46 if (Theme::IsHighContrastMode())
47 return GetImage(rsHighContrastImageURL, rxFrame);
48 else
49 return GetImage(rsImageURL, rxFrame);
52 Image Tools::GetImage (
53 const ::rtl::OUString& rsURL,
54 const Reference<frame::XFrame>& rxFrame)
56 if (rsURL.getLength() > 0)
58 const sal_Char sUnoCommandPrefix[] = ".uno:";
59 const sal_Char sCommandImagePrefix[] = "private:commandimage/";
60 const sal_Int32 nCommandImagePrefixLength = strlen(sCommandImagePrefix);
62 if (rsURL.startsWith(sUnoCommandPrefix))
64 const Image aPanelImage (::GetImage(rxFrame, rsURL, false));
65 return aPanelImage;
67 else if (rsURL.startsWith(sCommandImagePrefix))
69 ::rtl::OUStringBuffer aCommandName;
70 aCommandName.appendAscii(sUnoCommandPrefix);
71 aCommandName.append(rsURL.copy(nCommandImagePrefixLength));
72 const ::rtl::OUString sCommandName (aCommandName.makeStringAndClear());
74 const Image aPanelImage (::GetImage(rxFrame, sCommandName, false));
75 return aPanelImage;
77 else
79 const Reference<XComponentContext> xContext (::comphelper::getProcessComponentContext());
80 const Reference<graphic::XGraphicProvider> xGraphicProvider =
81 graphic::GraphicProvider::create( xContext );
82 ::comphelper::NamedValueCollection aMediaProperties;
83 aMediaProperties.put("URL", rsURL);
84 const Reference<graphic::XGraphic> xGraphic (
85 xGraphicProvider->queryGraphic(aMediaProperties.getPropertyValues()),
86 UNO_QUERY);
87 if (xGraphic.is())
88 return Image(xGraphic);
91 return Image();
94 css::awt::Gradient Tools::VclToAwtGradient (const Gradient& rVclGradient)
96 css::awt::Gradient aAwtGradient (
97 awt::GradientStyle(rVclGradient.GetStyle()),
98 rVclGradient.GetStartColor().GetRGBColor(),
99 rVclGradient.GetEndColor().GetRGBColor(),
100 rVclGradient.GetAngle(),
101 rVclGradient.GetBorder(),
102 rVclGradient.GetOfsX(),
103 rVclGradient.GetOfsY(),
104 rVclGradient.GetStartIntensity(),
105 rVclGradient.GetEndIntensity(),
106 rVclGradient.GetSteps());
107 return aAwtGradient;
110 Gradient Tools::AwtToVclGradient (const css::awt::Gradient& rAwtGradient)
112 Gradient aVclGradient (
113 GradientStyle(rAwtGradient.Style),
114 rAwtGradient.StartColor,
115 rAwtGradient.EndColor);
116 aVclGradient.SetAngle(rAwtGradient.Angle);
117 aVclGradient.SetBorder(rAwtGradient.Border);
118 aVclGradient.SetOfsX(rAwtGradient.XOffset);
119 aVclGradient.SetOfsY(rAwtGradient.YOffset);
120 aVclGradient.SetStartIntensity(rAwtGradient.StartIntensity);
121 aVclGradient.SetEndIntensity(rAwtGradient.EndIntensity);
122 aVclGradient.SetSteps(rAwtGradient.StepCount);
124 return aVclGradient;
127 util::URL Tools::GetURL (const ::rtl::OUString& rsCommand)
129 util::URL aURL;
130 aURL.Complete = rsCommand;
132 const Reference<XComponentContext> xComponentContext (::comphelper::getProcessComponentContext());
133 const Reference<util::XURLTransformer> xParser = util::URLTransformer::create( xComponentContext );
134 xParser->parseStrict(aURL);
136 return aURL;
139 Reference<frame::XDispatch> Tools::GetDispatch (
140 const css::uno::Reference<css::frame::XFrame>& rxFrame,
141 const util::URL& rURL)
143 Reference<frame::XDispatchProvider> xProvider (rxFrame, UNO_QUERY_THROW);
144 Reference<frame::XDispatch> xDispatch (xProvider->queryDispatch(rURL, ::rtl::OUString(), 0));
145 return xDispatch;
148 ::rtl::OUString Tools::GetModuleName (
149 const css::uno::Reference<css::frame::XFrame>& rxFrame)
151 if ( ! rxFrame.is() || ! rxFrame->getController().is())
152 return ::rtl::OUString();
156 const Reference<XComponentContext> xComponentContext (::comphelper::getProcessComponentContext());
157 const Reference<frame::XModuleManager> xModuleManager = frame::ModuleManager::create( xComponentContext );
158 return xModuleManager->identify(rxFrame);
160 catch (const Exception&)
162 // Ignored.
164 return ::rtl::OUString();
167 } } // end of namespace sfx2::sidebar
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */