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