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