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