xref: /AOO41X/main/sfx2/source/sidebar/Tools.cxx (revision 3ce09a58b0d6873449cda31e55c66dba2dbc8f7f)
1b9e67834SAndre Fischer /**************************************************************
2b9e67834SAndre Fischer  *
3b9e67834SAndre Fischer  * Licensed to the Apache Software Foundation (ASF) under one
4b9e67834SAndre Fischer  * or more contributor license agreements.  See the NOTICE file
5b9e67834SAndre Fischer  * distributed with this work for additional information
6b9e67834SAndre Fischer  * regarding copyright ownership.  The ASF licenses this file
7b9e67834SAndre Fischer  * to you under the Apache License, Version 2.0 (the
8b9e67834SAndre Fischer  * "License"); you may not use this file except in compliance
9b9e67834SAndre Fischer  * with the License.  You may obtain a copy of the License at
10b9e67834SAndre Fischer  *
11b9e67834SAndre Fischer  *   http://www.apache.org/licenses/LICENSE-2.0
12b9e67834SAndre Fischer  *
13b9e67834SAndre Fischer  * Unless required by applicable law or agreed to in writing,
14b9e67834SAndre Fischer  * software distributed under the License is distributed on an
15b9e67834SAndre Fischer  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16b9e67834SAndre Fischer  * KIND, either express or implied.  See the License for the
17b9e67834SAndre Fischer  * specific language governing permissions and limitations
18b9e67834SAndre Fischer  * under the License.
19b9e67834SAndre Fischer  *
20b9e67834SAndre Fischer  *************************************************************/
21b9e67834SAndre Fischer 
22b9e67834SAndre Fischer #include "precompiled_sfx2.hxx"
23b9e67834SAndre Fischer 
24f35c6d02SAndre Fischer #include "sfx2/sidebar/Tools.hxx"
25b9e67834SAndre Fischer 
26b9e67834SAndre Fischer #include "sfx2/sidebar/Theme.hxx"
27b9e67834SAndre Fischer 
28b9e67834SAndre Fischer #include "sfx2/imagemgr.hxx"
29b9e67834SAndre Fischer #include <comphelper/processfactory.hxx>
30b9e67834SAndre Fischer #include <comphelper/componentcontext.hxx>
31b9e67834SAndre Fischer #include <comphelper/namedvaluecollection.hxx>
3295a18594SAndre Fischer #include <vcl/gradient.hxx>
33b9e67834SAndre Fischer 
34f35c6d02SAndre Fischer #include <com/sun/star/frame/XDispatchProvider.hpp>
3595a18594SAndre Fischer #include <com/sun/star/graphic/XGraphicProvider.hpp>
36f35c6d02SAndre Fischer #include <com/sun/star/util/XURLTransformer.hpp>
37*d46a1e42SAndre Fischer #include <com/sun/star/frame/XModuleManager.hpp>
3895a18594SAndre Fischer 
3995a18594SAndre Fischer #include <cstring>
40b9e67834SAndre Fischer 
41b9e67834SAndre Fischer using namespace css;
42b9e67834SAndre Fischer using namespace cssu;
43b9e67834SAndre Fischer 
44b9e67834SAndre Fischer 
45b9e67834SAndre Fischer namespace sfx2 { namespace sidebar {
46b9e67834SAndre Fischer 
GetImage(const::rtl::OUString & rsImageURL,const::rtl::OUString & rsHighContrastImageURL,const Reference<frame::XFrame> & rxFrame)47b9e67834SAndre Fischer Image Tools::GetImage (
48b9e67834SAndre Fischer     const ::rtl::OUString& rsImageURL,
49b9e67834SAndre Fischer     const ::rtl::OUString& rsHighContrastImageURL,
50b9e67834SAndre Fischer     const Reference<frame::XFrame>& rxFrame)
51b9e67834SAndre Fischer {
52b9e67834SAndre Fischer     if (Theme::IsHighContrastMode())
53b9e67834SAndre Fischer         return GetImage(rsHighContrastImageURL, rxFrame);
54b9e67834SAndre Fischer     else
55b9e67834SAndre Fischer         return GetImage(rsImageURL, rxFrame);
56b9e67834SAndre Fischer }
57b9e67834SAndre Fischer 
58b9e67834SAndre Fischer 
59b9e67834SAndre Fischer 
60b9e67834SAndre Fischer 
GetImage(const::rtl::OUString & rsURL,const Reference<frame::XFrame> & rxFrame)61b9e67834SAndre Fischer Image Tools::GetImage (
62b9e67834SAndre Fischer     const ::rtl::OUString& rsURL,
63b9e67834SAndre Fischer     const Reference<frame::XFrame>& rxFrame)
64b9e67834SAndre Fischer {
65b9e67834SAndre Fischer     if (rsURL.getLength() > 0)
66b9e67834SAndre Fischer     {
67b9e67834SAndre Fischer         static const sal_Char* sUnoCommandPrefix = ".uno:";
68b9e67834SAndre Fischer         static const sal_Int32 nUnoCommandPrefixLength = strlen(sUnoCommandPrefix);
69b9e67834SAndre Fischer         static const sal_Char* sCommandImagePrefix = "private:commandimage/";
70b9e67834SAndre Fischer         static const sal_Int32 nCommandImagePrefixLength = strlen(sCommandImagePrefix);
71b9e67834SAndre Fischer 
72b9e67834SAndre Fischer         if (rsURL.compareToAscii(sUnoCommandPrefix, nUnoCommandPrefixLength) == 0)
73b9e67834SAndre Fischer         {
74b9e67834SAndre Fischer             const Image aPanelImage (::GetImage(rxFrame, rsURL, sal_False, Theme::IsHighContrastMode()));
75b9e67834SAndre Fischer             return aPanelImage;
76b9e67834SAndre Fischer         }
77b9e67834SAndre Fischer         else if (rsURL.compareToAscii(sCommandImagePrefix, nCommandImagePrefixLength) == 0)
78b9e67834SAndre Fischer         {
79b9e67834SAndre Fischer             ::rtl::OUStringBuffer aCommandName;
80b9e67834SAndre Fischer             aCommandName.appendAscii(sUnoCommandPrefix);
81b9e67834SAndre Fischer             aCommandName.append(rsURL.copy(nCommandImagePrefixLength));
82b9e67834SAndre Fischer             const ::rtl::OUString sCommandName (aCommandName.makeStringAndClear());
83b9e67834SAndre Fischer 
84b9e67834SAndre Fischer             const Image aPanelImage (::GetImage(rxFrame, sCommandName, sal_False, Theme::IsHighContrastMode()));
85b9e67834SAndre Fischer             return aPanelImage;
86b9e67834SAndre Fischer         }
87b9e67834SAndre Fischer         else
88b9e67834SAndre Fischer         {
89b9e67834SAndre Fischer             const ::comphelper::ComponentContext aContext (::comphelper::getProcessServiceFactory());
90b9e67834SAndre Fischer             const Reference<graphic::XGraphicProvider> xGraphicProvider (
91b9e67834SAndre Fischer                 aContext.createComponent("com.sun.star.graphic.GraphicProvider"),
92b9e67834SAndre Fischer                 UNO_QUERY);
93b9e67834SAndre Fischer             if ( xGraphicProvider.is())
94b9e67834SAndre Fischer             {
95b9e67834SAndre Fischer                 ::comphelper::NamedValueCollection aMediaProperties;
96b9e67834SAndre Fischer                 aMediaProperties.put("URL", rsURL);
97b9e67834SAndre Fischer                 const Reference<graphic::XGraphic> xGraphic (
98b9e67834SAndre Fischer                     xGraphicProvider->queryGraphic(aMediaProperties.getPropertyValues()),
99b9e67834SAndre Fischer                     UNO_QUERY);
100b9e67834SAndre Fischer                 if (xGraphic.is())
101b9e67834SAndre Fischer                     return Image(xGraphic);
102b9e67834SAndre Fischer             }
103b9e67834SAndre Fischer         }
104b9e67834SAndre Fischer     }
105b9e67834SAndre Fischer     return Image();
106b9e67834SAndre Fischer }
107b9e67834SAndre Fischer 
108b9e67834SAndre Fischer 
10995a18594SAndre Fischer 
11095a18594SAndre Fischer 
VclToAwtGradient(const Gradient aVclGradient)11195a18594SAndre Fischer css::awt::Gradient Tools::VclToAwtGradient (const Gradient aVclGradient)
11295a18594SAndre Fischer {
11395a18594SAndre Fischer     css::awt::Gradient aAwtGradient (
11495a18594SAndre Fischer         awt::GradientStyle(aVclGradient.GetStyle()),
11595a18594SAndre Fischer         aVclGradient.GetStartColor().GetRGBColor(),
11695a18594SAndre Fischer         aVclGradient.GetEndColor().GetRGBColor(),
11795a18594SAndre Fischer         aVclGradient.GetAngle(),
11895a18594SAndre Fischer         aVclGradient.GetBorder(),
11995a18594SAndre Fischer         aVclGradient.GetOfsX(),
12095a18594SAndre Fischer         aVclGradient.GetOfsY(),
12195a18594SAndre Fischer         aVclGradient.GetStartIntensity(),
12295a18594SAndre Fischer         aVclGradient.GetEndIntensity(),
12395a18594SAndre Fischer         aVclGradient.GetSteps());
12495a18594SAndre Fischer     return aAwtGradient;
12595a18594SAndre Fischer }
12695a18594SAndre Fischer 
12795a18594SAndre Fischer 
12895a18594SAndre Fischer 
12995a18594SAndre Fischer 
AwtToVclGradient(const css::awt::Gradient aAwtGradient)13095a18594SAndre Fischer Gradient Tools::AwtToVclGradient (const css::awt::Gradient aAwtGradient)
13195a18594SAndre Fischer {
13295a18594SAndre Fischer     Gradient aVclGradient (
13395a18594SAndre Fischer         GradientStyle(aAwtGradient.Style),
13495a18594SAndre Fischer         aAwtGradient.StartColor,
13595a18594SAndre Fischer         aAwtGradient.EndColor);
13695a18594SAndre Fischer     aVclGradient.SetAngle(aAwtGradient.Angle);
13795a18594SAndre Fischer     aVclGradient.SetBorder(aAwtGradient.Border);
13895a18594SAndre Fischer     aVclGradient.SetOfsX(aAwtGradient.XOffset);
13995a18594SAndre Fischer     aVclGradient.SetOfsY(aAwtGradient.YOffset);
14095a18594SAndre Fischer     aVclGradient.SetStartIntensity(aAwtGradient.StartIntensity);
14195a18594SAndre Fischer     aVclGradient.SetEndIntensity(aAwtGradient.EndIntensity);
14295a18594SAndre Fischer     aVclGradient.SetSteps(aAwtGradient.StepCount);
14395a18594SAndre Fischer 
14495a18594SAndre Fischer     return aVclGradient;
14595a18594SAndre Fischer }
14695a18594SAndre Fischer 
14795a18594SAndre Fischer 
14895a18594SAndre Fischer 
14995a18594SAndre Fischer 
RectangleToSvBorder(const Rectangle aBox)15095a18594SAndre Fischer SvBorder Tools::RectangleToSvBorder (const Rectangle aBox)
15195a18594SAndre Fischer {
15295a18594SAndre Fischer     return SvBorder(
15395a18594SAndre Fischer         aBox.Left(),
15495a18594SAndre Fischer         aBox.Top(),
15595a18594SAndre Fischer         aBox.Right(),
15695a18594SAndre Fischer         aBox.Bottom());
15795a18594SAndre Fischer }
15895a18594SAndre Fischer 
159f35c6d02SAndre Fischer 
160f35c6d02SAndre Fischer 
161f35c6d02SAndre Fischer 
GetURL(const::rtl::OUString & rsCommand)162f35c6d02SAndre Fischer util::URL Tools::GetURL (const ::rtl::OUString& rsCommand)
163f35c6d02SAndre Fischer {
164f35c6d02SAndre Fischer     util::URL aURL;
165f35c6d02SAndre Fischer     aURL.Complete = rsCommand;
166f35c6d02SAndre Fischer 
167f35c6d02SAndre Fischer     const ::comphelper::ComponentContext aComponentContext (::comphelper::getProcessServiceFactory());
168f35c6d02SAndre Fischer     const Reference<util::XURLTransformer> xParser (
169f35c6d02SAndre Fischer         aComponentContext.createComponent("com.sun.star.util.URLTransformer"),
170f35c6d02SAndre Fischer             UNO_QUERY_THROW);
171f35c6d02SAndre Fischer     xParser->parseStrict(aURL);
172f35c6d02SAndre Fischer 
173f35c6d02SAndre Fischer     return aURL;
174f35c6d02SAndre Fischer }
175f35c6d02SAndre Fischer 
176f35c6d02SAndre Fischer 
177f35c6d02SAndre Fischer 
178f35c6d02SAndre Fischer 
GetDispatch(const cssu::Reference<css::frame::XFrame> & rxFrame,const util::URL & rURL)179f35c6d02SAndre Fischer Reference<frame::XDispatch> Tools::GetDispatch (
180f35c6d02SAndre Fischer     const cssu::Reference<css::frame::XFrame>& rxFrame,
181f35c6d02SAndre Fischer     const util::URL& rURL)
182f35c6d02SAndre Fischer {
183f35c6d02SAndre Fischer     Reference<frame::XDispatchProvider> xProvider (rxFrame, UNO_QUERY_THROW);
184f35c6d02SAndre Fischer     Reference<frame::XDispatch> xDispatch (xProvider->queryDispatch(rURL, ::rtl::OUString(), 0));
185f35c6d02SAndre Fischer     return xDispatch;
186f35c6d02SAndre Fischer }
187f35c6d02SAndre Fischer 
188f35c6d02SAndre Fischer 
189*d46a1e42SAndre Fischer 
190*d46a1e42SAndre Fischer 
GetModuleName(const cssu::Reference<css::frame::XFrame> & rxFrame)191*d46a1e42SAndre Fischer ::rtl::OUString Tools::GetModuleName (
192*d46a1e42SAndre Fischer     const cssu::Reference<css::frame::XFrame>& rxFrame)
193*d46a1e42SAndre Fischer {
194*d46a1e42SAndre Fischer     if ( ! rxFrame.is() || ! rxFrame->getController().is())
195*d46a1e42SAndre Fischer         return ::rtl::OUString();
196*d46a1e42SAndre Fischer 
197*d46a1e42SAndre Fischer     try
198*d46a1e42SAndre Fischer     {
199*d46a1e42SAndre Fischer         const ::comphelper::ComponentContext aContext (::comphelper::getProcessServiceFactory());
200*d46a1e42SAndre Fischer         const Reference<frame::XModuleManager> xModuleManager (
201*d46a1e42SAndre Fischer             aContext.createComponent("com.sun.star.frame.ModuleManager"),
202*d46a1e42SAndre Fischer             UNO_QUERY_THROW);
203*d46a1e42SAndre Fischer         return xModuleManager->identify(rxFrame);
204*d46a1e42SAndre Fischer     }
205*d46a1e42SAndre Fischer     catch (const Exception&)
206*d46a1e42SAndre Fischer     {
207*d46a1e42SAndre Fischer         // Ignored.
208*d46a1e42SAndre Fischer     }
209*d46a1e42SAndre Fischer     return ::rtl::OUString();
210*d46a1e42SAndre Fischer }
211*d46a1e42SAndre Fischer 
212*d46a1e42SAndre Fischer 
213b9e67834SAndre Fischer } } // end of namespace sfx2::sidebar
214