xref: /aoo41x/main/sfx2/source/sidebar/Tools.cxx (revision d46a1e42)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #include "precompiled_sfx2.hxx"
23 
24 #include "sfx2/sidebar/Tools.hxx"
25 
26 #include "sfx2/sidebar/Theme.hxx"
27 
28 #include "sfx2/imagemgr.hxx"
29 #include <comphelper/processfactory.hxx>
30 #include <comphelper/componentcontext.hxx>
31 #include <comphelper/namedvaluecollection.hxx>
32 #include <vcl/gradient.hxx>
33 
34 #include <com/sun/star/frame/XDispatchProvider.hpp>
35 #include <com/sun/star/graphic/XGraphicProvider.hpp>
36 #include <com/sun/star/util/XURLTransformer.hpp>
37 #include <com/sun/star/frame/XModuleManager.hpp>
38 
39 #include <cstring>
40 
41 using namespace css;
42 using namespace cssu;
43 
44 
45 namespace sfx2 { namespace sidebar {
46 
GetImage(const::rtl::OUString & rsImageURL,const::rtl::OUString & rsHighContrastImageURL,const Reference<frame::XFrame> & rxFrame)47 Image Tools::GetImage (
48     const ::rtl::OUString& rsImageURL,
49     const ::rtl::OUString& rsHighContrastImageURL,
50     const Reference<frame::XFrame>& rxFrame)
51 {
52     if (Theme::IsHighContrastMode())
53         return GetImage(rsHighContrastImageURL, rxFrame);
54     else
55         return GetImage(rsImageURL, rxFrame);
56 }
57 
58 
59 
60 
GetImage(const::rtl::OUString & rsURL,const Reference<frame::XFrame> & rxFrame)61 Image Tools::GetImage (
62     const ::rtl::OUString& rsURL,
63     const Reference<frame::XFrame>& rxFrame)
64 {
65     if (rsURL.getLength() > 0)
66     {
67         static const sal_Char* sUnoCommandPrefix = ".uno:";
68         static const sal_Int32 nUnoCommandPrefixLength = strlen(sUnoCommandPrefix);
69         static const sal_Char* sCommandImagePrefix = "private:commandimage/";
70         static const sal_Int32 nCommandImagePrefixLength = strlen(sCommandImagePrefix);
71 
72         if (rsURL.compareToAscii(sUnoCommandPrefix, nUnoCommandPrefixLength) == 0)
73         {
74             const Image aPanelImage (::GetImage(rxFrame, rsURL, sal_False, Theme::IsHighContrastMode()));
75             return aPanelImage;
76         }
77         else if (rsURL.compareToAscii(sCommandImagePrefix, nCommandImagePrefixLength) == 0)
78         {
79             ::rtl::OUStringBuffer aCommandName;
80             aCommandName.appendAscii(sUnoCommandPrefix);
81             aCommandName.append(rsURL.copy(nCommandImagePrefixLength));
82             const ::rtl::OUString sCommandName (aCommandName.makeStringAndClear());
83 
84             const Image aPanelImage (::GetImage(rxFrame, sCommandName, sal_False, Theme::IsHighContrastMode()));
85             return aPanelImage;
86         }
87         else
88         {
89             const ::comphelper::ComponentContext aContext (::comphelper::getProcessServiceFactory());
90             const Reference<graphic::XGraphicProvider> xGraphicProvider (
91                 aContext.createComponent("com.sun.star.graphic.GraphicProvider"),
92                 UNO_QUERY);
93             if ( xGraphicProvider.is())
94             {
95                 ::comphelper::NamedValueCollection aMediaProperties;
96                 aMediaProperties.put("URL", rsURL);
97                 const Reference<graphic::XGraphic> xGraphic (
98                     xGraphicProvider->queryGraphic(aMediaProperties.getPropertyValues()),
99                     UNO_QUERY);
100                 if (xGraphic.is())
101                     return Image(xGraphic);
102             }
103         }
104     }
105     return Image();
106 }
107 
108 
109 
110 
VclToAwtGradient(const Gradient aVclGradient)111 css::awt::Gradient Tools::VclToAwtGradient (const Gradient aVclGradient)
112 {
113     css::awt::Gradient aAwtGradient (
114         awt::GradientStyle(aVclGradient.GetStyle()),
115         aVclGradient.GetStartColor().GetRGBColor(),
116         aVclGradient.GetEndColor().GetRGBColor(),
117         aVclGradient.GetAngle(),
118         aVclGradient.GetBorder(),
119         aVclGradient.GetOfsX(),
120         aVclGradient.GetOfsY(),
121         aVclGradient.GetStartIntensity(),
122         aVclGradient.GetEndIntensity(),
123         aVclGradient.GetSteps());
124     return aAwtGradient;
125 }
126 
127 
128 
129 
AwtToVclGradient(const css::awt::Gradient aAwtGradient)130 Gradient Tools::AwtToVclGradient (const css::awt::Gradient aAwtGradient)
131 {
132     Gradient aVclGradient (
133         GradientStyle(aAwtGradient.Style),
134         aAwtGradient.StartColor,
135         aAwtGradient.EndColor);
136     aVclGradient.SetAngle(aAwtGradient.Angle);
137     aVclGradient.SetBorder(aAwtGradient.Border);
138     aVclGradient.SetOfsX(aAwtGradient.XOffset);
139     aVclGradient.SetOfsY(aAwtGradient.YOffset);
140     aVclGradient.SetStartIntensity(aAwtGradient.StartIntensity);
141     aVclGradient.SetEndIntensity(aAwtGradient.EndIntensity);
142     aVclGradient.SetSteps(aAwtGradient.StepCount);
143 
144     return aVclGradient;
145 }
146 
147 
148 
149 
RectangleToSvBorder(const Rectangle aBox)150 SvBorder Tools::RectangleToSvBorder (const Rectangle aBox)
151 {
152     return SvBorder(
153         aBox.Left(),
154         aBox.Top(),
155         aBox.Right(),
156         aBox.Bottom());
157 }
158 
159 
160 
161 
GetURL(const::rtl::OUString & rsCommand)162 util::URL Tools::GetURL (const ::rtl::OUString& rsCommand)
163 {
164     util::URL aURL;
165     aURL.Complete = rsCommand;
166 
167     const ::comphelper::ComponentContext aComponentContext (::comphelper::getProcessServiceFactory());
168     const Reference<util::XURLTransformer> xParser (
169         aComponentContext.createComponent("com.sun.star.util.URLTransformer"),
170             UNO_QUERY_THROW);
171     xParser->parseStrict(aURL);
172 
173     return aURL;
174 }
175 
176 
177 
178 
GetDispatch(const cssu::Reference<css::frame::XFrame> & rxFrame,const util::URL & rURL)179 Reference<frame::XDispatch> Tools::GetDispatch (
180     const cssu::Reference<css::frame::XFrame>& rxFrame,
181     const util::URL& rURL)
182 {
183     Reference<frame::XDispatchProvider> xProvider (rxFrame, UNO_QUERY_THROW);
184     Reference<frame::XDispatch> xDispatch (xProvider->queryDispatch(rURL, ::rtl::OUString(), 0));
185     return xDispatch;
186 }
187 
188 
189 
190 
GetModuleName(const cssu::Reference<css::frame::XFrame> & rxFrame)191 ::rtl::OUString Tools::GetModuleName (
192     const cssu::Reference<css::frame::XFrame>& rxFrame)
193 {
194     if ( ! rxFrame.is() || ! rxFrame->getController().is())
195         return ::rtl::OUString();
196 
197     try
198     {
199         const ::comphelper::ComponentContext aContext (::comphelper::getProcessServiceFactory());
200         const Reference<frame::XModuleManager> xModuleManager (
201             aContext.createComponent("com.sun.star.frame.ModuleManager"),
202             UNO_QUERY_THROW);
203         return xModuleManager->identify(rxFrame);
204     }
205     catch (const Exception&)
206     {
207         // Ignored.
208     }
209     return ::rtl::OUString();
210 }
211 
212 
213 } } // end of namespace sfx2::sidebar
214