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 $
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"
38 //_________________________________________________________________________________________________________________
40 //_________________________________________________________________________________________________________________
41 #include <classes/addonsoptions.hxx>
42 #ifndef __FRAMEWORK_TOOLBAR_HXX_
43 #include "uielement/toolbar.hxx"
46 //_________________________________________________________________________________________________________________
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 //_________________________________________________________________________________________________________________
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 );
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
;
109 m_xMacroExpander
= Reference
< com::sun::star::util::XMacroExpander
>( xContext
->getValueByName(
110 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/singletons/com.sun.star.util.theMacroExpander"))),
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();
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
,
142 const ::rtl::OUString
& aCommand
) :
143 ComplexToolbarController( rServiceManager
, rFrame
, pToolbar
, nID
, aCommand
)
145 sal_Bool
bBigImages( SvtMiscOptions().AreCurrentSymbolsLarge() );
146 sal_Bool
bHiContrast( pToolbar
->GetSettings().GetStyleSettings().GetHighContrastMode() );
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 ))
183 rControlCommand
.Arguments
[i
].Value
>>= aURL
;
185 SubstituteVariables( aURL
);
188 if ( ReadImageFromURL( SvtMiscOptions().AreCurrentSymbolsLarge(),
192 m_pToolbar
->SetItemImage( m_nID
, aImage
);
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
),
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,...)
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
);