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