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 namespace sfx2 { namespace sidebar { 45 46 Image Tools::GetImage ( 47 const ::rtl::OUString& rsImageURL, 48 const ::rtl::OUString& rsHighContrastImageURL, 49 const Reference<frame::XFrame>& rxFrame) 50 { 51 if (Theme::IsHighContrastMode()) 52 return GetImage(rsHighContrastImageURL, rxFrame); 53 else 54 return GetImage(rsImageURL, rxFrame); 55 } 56 57 Image Tools::GetImage ( 58 const ::rtl::OUString& rsURL, 59 const Reference<frame::XFrame>& rxFrame) 60 { 61 if (rsURL.getLength() > 0) 62 { 63 static const sal_Char* sUnoCommandPrefix = ".uno:"; 64 static const sal_Int32 nUnoCommandPrefixLength = strlen(sUnoCommandPrefix); 65 static const sal_Char* sCommandImagePrefix = "private:commandimage/"; 66 static const sal_Int32 nCommandImagePrefixLength = strlen(sCommandImagePrefix); 67 68 if (rsURL.compareToAscii(sUnoCommandPrefix, nUnoCommandPrefixLength) == 0) 69 { 70 const Image aPanelImage (::GetImage(rxFrame, rsURL, sal_False, Theme::IsHighContrastMode())); 71 return aPanelImage; 72 } 73 else if (rsURL.compareToAscii(sCommandImagePrefix, nCommandImagePrefixLength) == 0) 74 { 75 ::rtl::OUStringBuffer aCommandName; 76 aCommandName.appendAscii(sUnoCommandPrefix); 77 aCommandName.append(rsURL.copy(nCommandImagePrefixLength)); 78 const ::rtl::OUString sCommandName (aCommandName.makeStringAndClear()); 79 80 const Image aPanelImage (::GetImage(rxFrame, sCommandName, sal_False, Theme::IsHighContrastMode())); 81 return aPanelImage; 82 } 83 else 84 { 85 const ::comphelper::ComponentContext aContext (::comphelper::getProcessServiceFactory()); 86 const Reference<graphic::XGraphicProvider> xGraphicProvider ( 87 aContext.createComponent("com.sun.star.graphic.GraphicProvider"), 88 UNO_QUERY); 89 if ( xGraphicProvider.is()) 90 { 91 ::comphelper::NamedValueCollection aMediaProperties; 92 aMediaProperties.put("URL", rsURL); 93 const Reference<graphic::XGraphic> xGraphic ( 94 xGraphicProvider->queryGraphic(aMediaProperties.getPropertyValues()), 95 UNO_QUERY); 96 if (xGraphic.is()) 97 return Image(xGraphic); 98 } 99 } 100 } 101 return Image(); 102 } 103 104 css::awt::Gradient Tools::VclToAwtGradient (const Gradient aVclGradient) 105 { 106 css::awt::Gradient aAwtGradient ( 107 awt::GradientStyle(aVclGradient.GetStyle()), 108 aVclGradient.GetStartColor().GetRGBColor(), 109 aVclGradient.GetEndColor().GetRGBColor(), 110 aVclGradient.GetAngle(), 111 aVclGradient.GetBorder(), 112 aVclGradient.GetOfsX(), 113 aVclGradient.GetOfsY(), 114 aVclGradient.GetStartIntensity(), 115 aVclGradient.GetEndIntensity(), 116 aVclGradient.GetSteps()); 117 return aAwtGradient; 118 } 119 120 Gradient Tools::AwtToVclGradient (const css::awt::Gradient aAwtGradient) 121 { 122 Gradient aVclGradient ( 123 GradientStyle(aAwtGradient.Style), 124 aAwtGradient.StartColor, 125 aAwtGradient.EndColor); 126 aVclGradient.SetAngle(aAwtGradient.Angle); 127 aVclGradient.SetBorder(aAwtGradient.Border); 128 aVclGradient.SetOfsX(aAwtGradient.XOffset); 129 aVclGradient.SetOfsY(aAwtGradient.YOffset); 130 aVclGradient.SetStartIntensity(aAwtGradient.StartIntensity); 131 aVclGradient.SetEndIntensity(aAwtGradient.EndIntensity); 132 aVclGradient.SetSteps(aAwtGradient.StepCount); 133 134 return aVclGradient; 135 } 136 137 SvBorder Tools::RectangleToSvBorder (const Rectangle aBox) 138 { 139 return SvBorder( 140 aBox.Left(), 141 aBox.Top(), 142 aBox.Right(), 143 aBox.Bottom()); 144 } 145 146 util::URL Tools::GetURL (const ::rtl::OUString& rsCommand) 147 { 148 util::URL aURL; 149 aURL.Complete = rsCommand; 150 151 const ::comphelper::ComponentContext aComponentContext (::comphelper::getProcessServiceFactory()); 152 const Reference<util::XURLTransformer> xParser ( 153 aComponentContext.createComponent("com.sun.star.util.URLTransformer"), 154 UNO_QUERY_THROW); 155 xParser->parseStrict(aURL); 156 157 return aURL; 158 } 159 160 Reference<frame::XDispatch> Tools::GetDispatch ( 161 const cssu::Reference<css::frame::XFrame>& rxFrame, 162 const util::URL& rURL) 163 { 164 Reference<frame::XDispatchProvider> xProvider (rxFrame, UNO_QUERY_THROW); 165 Reference<frame::XDispatch> xDispatch (xProvider->queryDispatch(rURL, ::rtl::OUString(), 0)); 166 return xDispatch; 167 } 168 169 ::rtl::OUString Tools::GetModuleName ( 170 const cssu::Reference<css::frame::XFrame>& rxFrame) 171 { 172 if ( ! rxFrame.is() || ! rxFrame->getController().is()) 173 return ::rtl::OUString(); 174 175 try 176 { 177 const ::comphelper::ComponentContext aContext (::comphelper::getProcessServiceFactory()); 178 const Reference<frame::XModuleManager> xModuleManager ( 179 aContext.createComponent("com.sun.star.frame.ModuleManager"), 180 UNO_QUERY_THROW); 181 return xModuleManager->identify(rxFrame); 182 } 183 catch (const Exception&) 184 { 185 // Ignored. 186 } 187 return ::rtl::OUString(); 188 } 189 190 } } // end of namespace sfx2::sidebar 191 192 /* vim: set noet sw=4 ts=4: */ 193