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 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_framework.hxx"
26 
27 #ifndef __FRAMEWORK_UIELEMENT_IMAGEBUTTONTOOLBARCONTROLLER_HXX
28 #include "uielement/imagebuttontoolbarcontroller.hxx"
29 #endif
30 
31 //_________________________________________________________________________________________________________________
32 //	my own includes
33 //_________________________________________________________________________________________________________________
34 #include <framework/addonsoptions.hxx>
35 #ifndef __FRAMEWORK_TOOLBAR_HXX_
36 #include "uielement/toolbar.hxx"
37 #endif
38 
39 //_________________________________________________________________________________________________________________
40 //	interface includes
41 //_________________________________________________________________________________________________________________
42 #include <com/sun/star/util/XURLTransformer.hpp>
43 #include <com/sun/star/frame/XDispatchProvider.hpp>
44 #include <com/sun/star/beans/PropertyValue.hpp>
45 #include <com/sun/star/frame/XControlNotificationListener.hpp>
46 #include "com/sun/star/util/XMacroExpander.hpp"
47 #include "com/sun/star/uno/XComponentContext.hpp"
48 #include "com/sun/star/beans/XPropertySet.hpp"
49 
50 //_________________________________________________________________________________________________________________
51 //	other includes
52 //_________________________________________________________________________________________________________________
53 
54 #include <rtl/uri.hxx>
55 #include <vos/mutex.hxx>
56 #include <comphelper/processfactory.hxx>
57 #include <unotools/ucbstreamhelper.hxx>
58 #include <tools/urlobj.hxx>
59 #include <vcl/svapp.hxx>
60 #include <vcl/mnemonic.hxx>
61 #include <vcl/window.hxx>
62 #include <vcl/graph.hxx>
63 #include <vcl/bitmap.hxx>
64 #include <svtools/filter.hxx>
65 #include <svtools/miscopt.hxx>
66 
67 using namespace ::com::sun::star;
68 using namespace ::com::sun::star::awt;
69 using namespace ::com::sun::star::uno;
70 using namespace ::com::sun::star::beans;
71 using namespace ::com::sun::star::lang;
72 using namespace ::com::sun::star::frame;
73 using namespace ::com::sun::star::util;
74 
75 #define EXPAND_PROTOCOL "vnd.sun.star.expand:"
76 
77 const ::Size  aImageSizeSmall( 16, 16 );
78 const ::Size  aImageSizeBig( 26, 26 );
79 
80 namespace framework
81 {
82 
83 static uno::WeakReference< util::XMacroExpander > m_xMacroExpander;
84 
85 // ------------------------------------------------------------------
86 
GetMacroExpander()87 uno::Reference< util::XMacroExpander > GetMacroExpander()
88 {
89     uno::Reference< util::XMacroExpander > xMacroExpander( m_xMacroExpander );
90     if ( !xMacroExpander.is() )
91     {
92         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
93 
94         if ( !xMacroExpander.is() )
95         {
96             uno::Reference< uno::XComponentContext > xContext;
97             uno::Reference< beans::XPropertySet > xProps( ::comphelper::getProcessServiceFactory(), UNO_QUERY );
98             xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ))) >>= xContext;
99             if ( xContext.is() )
100             {
101                 m_xMacroExpander =  Reference< com::sun::star::util::XMacroExpander >( xContext->getValueByName(
102                                         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/singletons/com.sun.star.util.theMacroExpander"))),
103                                         UNO_QUERY );
104                 xMacroExpander = m_xMacroExpander;
105             }
106         }
107     }
108 
109     return xMacroExpander;
110 }
111 
SubstituteVariables(::rtl::OUString & aURL)112 static void SubstituteVariables( ::rtl::OUString& aURL )
113 {
114     if ( aURL.compareToAscii( RTL_CONSTASCII_STRINGPARAM( EXPAND_PROTOCOL )) == 0 )
115     {
116         uno::Reference< util::XMacroExpander > xMacroExpander = GetMacroExpander();
117 
118         // cut protocol
119         rtl::OUString aMacro( aURL.copy( sizeof ( EXPAND_PROTOCOL ) -1 ) );
120         // decode uric class chars
121         aMacro = ::rtl::Uri::decode( aMacro, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
122         // expand macro string
123         aURL = xMacroExpander->expandMacros( aMacro );
124     }
125 }
126 
127 // ------------------------------------------------------------------
128 
ImageButtonToolbarController(const Reference<XMultiServiceFactory> & rServiceManager,const Reference<XFrame> & rFrame,ToolBox * pToolbar,sal_uInt16 nID,const::rtl::OUString & aCommand)129 ImageButtonToolbarController::ImageButtonToolbarController(
130     const Reference< XMultiServiceFactory >& rServiceManager,
131     const Reference< XFrame >&               rFrame,
132     ToolBox*                                 pToolbar,
133     sal_uInt16                                   nID,
134     const ::rtl::OUString&                          aCommand ) :
135     ComplexToolbarController( rServiceManager, rFrame, pToolbar, nID, aCommand )
136 {
137     sal_Bool bBigImages( SvtMiscOptions().AreCurrentSymbolsLarge() );
138     sal_Bool bHiContrast( pToolbar->GetSettings().GetStyleSettings().GetHighContrastMode() );
139 
140     Image aImage = AddonsOptions().GetImageFromURL( aCommand, bBigImages, bHiContrast, sal_True );
141 
142     // Height will be controlled by scaling according to button height
143     m_pToolbar->SetItemImage( m_nID, aImage );
144 }
145 
146 // ------------------------------------------------------------------
147 
~ImageButtonToolbarController()148 ImageButtonToolbarController::~ImageButtonToolbarController()
149 {
150 }
151 
152 // ------------------------------------------------------------------
153 
dispose()154 void SAL_CALL ImageButtonToolbarController::dispose()
155 throw ( RuntimeException )
156 {
157     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
158     ComplexToolbarController::dispose();
159 }
160 
161 // ------------------------------------------------------------------
162 
executeControlCommand(const::com::sun::star::frame::ControlCommand & rControlCommand)163 void ImageButtonToolbarController::executeControlCommand( const ::com::sun::star::frame::ControlCommand& rControlCommand )
164 {
165     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
166     // i73486 to be downward compatible use old and "wrong" also!
167     if (( rControlCommand.Command.equalsAsciiL( "SetImag", 7 )) ||
168         ( rControlCommand.Command.equalsAsciiL( "SetImage", 8 )) )
169     {
170         for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
171         {
172             if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "URL", 3 ))
173             {
174                 rtl::OUString aURL;
175                 rControlCommand.Arguments[i].Value >>= aURL;
176 
177                 SubstituteVariables( aURL );
178 
179                 Image aImage;
180                 if ( ReadImageFromURL( SvtMiscOptions().AreCurrentSymbolsLarge(),
181                                        aURL,
182                                        aImage ))
183                 {
184                     m_pToolbar->SetItemImage( m_nID, aImage );
185 
186                     // send notification
187                     uno::Sequence< beans::NamedValue > aInfo( 1 );
188                     aInfo[0].Name  = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "URL" ));
189                     aInfo[0].Value <<= aURL;
190                     addNotifyInfo( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ImageChanged" )),
191                                 getDispatchFromCommand( m_aCommandURL ),
192                                 aInfo );
193                     break;
194                 }
195             }
196         }
197     }
198 }
199 
ReadImageFromURL(sal_Bool bBigImage,const::rtl::OUString & aImageURL,Image & aImage)200 sal_Bool ImageButtonToolbarController::ReadImageFromURL( sal_Bool bBigImage, const ::rtl::OUString& aImageURL, Image& aImage )
201 {
202     SvStream* pStream = utl::UcbStreamHelper::CreateStream( aImageURL, STREAM_STD_READ );
203     if ( pStream && ( pStream->GetErrorCode() == 0 ))
204     {
205         // Use graphic class to also support more graphic formats (bmp,png,...)
206         Graphic aGraphic;
207 
208         GraphicFilter* pGF = GraphicFilter::GetGraphicFilter();
209         pGF->ImportGraphic( aGraphic, String(), *pStream, GRFILTER_FORMAT_DONTKNOW );
210 
211         BitmapEx aBitmapEx = aGraphic.GetBitmapEx();
212 
213         const ::Size aSize = bBigImage ? aImageSizeBig : aImageSizeSmall; // Sizes used for toolbar images
214 
215         ::Size aBmpSize = aBitmapEx.GetSizePixel();
216 	    if ( aBmpSize.Width() > 0 && aBmpSize.Height() > 0 )
217         {
218             ::Size aNoScaleSize( aBmpSize.Width(), aSize.Height() );
219             if ( aBmpSize != aNoScaleSize )
220                 aBitmapEx.Scale( aNoScaleSize, BMP_SCALE_INTERPOLATE );
221             aImage = Image( aBitmapEx );
222             return sal_True;
223         }
224     }
225 
226     delete pStream;
227     return sal_False;
228 }
229 
230 } // namespace
231 
232