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