Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include "uielement/imagebuttontoolbarcontroller.hxx"
22 :
23 : #include <framework/addonsoptions.hxx>
24 : #include "uielement/toolbar.hxx"
25 :
26 : #include <com/sun/star/util/XURLTransformer.hpp>
27 : #include <com/sun/star/frame/XDispatchProvider.hpp>
28 : #include <com/sun/star/beans/PropertyValue.hpp>
29 : #include <com/sun/star/frame/XControlNotificationListener.hpp>
30 : #include "com/sun/star/util/XMacroExpander.hpp"
31 : #include "com/sun/star/uno/XComponentContext.hpp"
32 :
33 : #include <rtl/uri.hxx>
34 : #include <osl/mutex.hxx>
35 : #include <comphelper/processfactory.hxx>
36 : #include <unotools/ucbstreamhelper.hxx>
37 : #include <vcl/svapp.hxx>
38 : #include <vcl/mnemonic.hxx>
39 : #include <vcl/window.hxx>
40 : #include <vcl/graph.hxx>
41 : #include <vcl/bitmap.hxx>
42 : #include <svtools/filter.hxx>
43 : #include <svtools/miscopt.hxx>
44 :
45 : using namespace ::com::sun::star;
46 : using namespace ::com::sun::star::awt;
47 : using namespace ::com::sun::star::uno;
48 : using namespace ::com::sun::star::beans;
49 : using namespace ::com::sun::star::lang;
50 : using namespace ::com::sun::star::frame;
51 : using namespace ::com::sun::star::util;
52 :
53 : #define EXPAND_PROTOCOL "vnd.sun.star.expand:"
54 :
55 19 : const ::Size aImageSizeSmall( 16, 16 );
56 19 : const ::Size aImageSizeBig( 26, 26 );
57 :
58 : namespace framework
59 : {
60 :
61 19 : static uno::WeakReference< util::XMacroExpander > m_xMacroExpander;
62 :
63 : // ------------------------------------------------------------------
64 :
65 0 : uno::Reference< util::XMacroExpander > GetMacroExpander()
66 : {
67 0 : uno::Reference< util::XMacroExpander > xMacroExpander( m_xMacroExpander );
68 0 : if ( !xMacroExpander.is() )
69 : {
70 0 : SolarMutexGuard aSolarMutexGuard;
71 :
72 0 : if ( !xMacroExpander.is() )
73 : {
74 : uno::Reference< uno::XComponentContext > xContext(
75 0 : comphelper::getProcessComponentContext() );
76 0 : m_xMacroExpander = Reference< com::sun::star::util::XMacroExpander >( xContext->getValueByName(
77 0 : ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/singletons/com.sun.star.util.theMacroExpander"))),
78 0 : UNO_QUERY );
79 0 : xMacroExpander = m_xMacroExpander;
80 0 : }
81 : }
82 :
83 0 : return xMacroExpander;
84 : }
85 :
86 0 : static void SubstituteVariables( ::rtl::OUString& aURL )
87 : {
88 0 : if ( aURL.compareToAscii( RTL_CONSTASCII_STRINGPARAM( EXPAND_PROTOCOL )) == 0 )
89 : {
90 0 : uno::Reference< util::XMacroExpander > xMacroExpander = GetMacroExpander();
91 :
92 : // cut protocol
93 0 : rtl::OUString aMacro( aURL.copy( sizeof ( EXPAND_PROTOCOL ) -1 ) );
94 : // decode uric class chars
95 0 : aMacro = ::rtl::Uri::decode( aMacro, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
96 : // expand macro string
97 0 : aURL = xMacroExpander->expandMacros( aMacro );
98 : }
99 0 : }
100 :
101 : // ------------------------------------------------------------------
102 :
103 0 : ImageButtonToolbarController::ImageButtonToolbarController(
104 : const Reference< XMultiServiceFactory >& rServiceManager,
105 : const Reference< XFrame >& rFrame,
106 : ToolBox* pToolbar,
107 : sal_uInt16 nID,
108 : const ::rtl::OUString& aCommand ) :
109 0 : ComplexToolbarController( rServiceManager, rFrame, pToolbar, nID, aCommand )
110 : {
111 0 : sal_Bool bBigImages( SvtMiscOptions().AreCurrentSymbolsLarge() );
112 :
113 0 : Image aImage = AddonsOptions().GetImageFromURL( aCommand, bBigImages, sal_True );
114 :
115 : // Height will be controlled by scaling according to button height
116 0 : m_pToolbar->SetItemImage( m_nID, aImage );
117 0 : }
118 :
119 : // ------------------------------------------------------------------
120 :
121 0 : ImageButtonToolbarController::~ImageButtonToolbarController()
122 : {
123 0 : }
124 :
125 : // ------------------------------------------------------------------
126 :
127 0 : void SAL_CALL ImageButtonToolbarController::dispose()
128 : throw ( RuntimeException )
129 : {
130 0 : SolarMutexGuard aSolarMutexGuard;
131 0 : ComplexToolbarController::dispose();
132 0 : }
133 :
134 : // ------------------------------------------------------------------
135 :
136 0 : void ImageButtonToolbarController::executeControlCommand( const ::com::sun::star::frame::ControlCommand& rControlCommand )
137 : {
138 0 : SolarMutexGuard aSolarMutexGuard;
139 : // i73486 to be downward compatible use old and "wrong" also!
140 0 : if (( rControlCommand.Command.equalsAsciiL( "SetImag", 7 )) ||
141 0 : ( rControlCommand.Command.equalsAsciiL( "SetImage", 8 )) )
142 : {
143 0 : for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
144 : {
145 0 : if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "URL", 3 ))
146 : {
147 0 : rtl::OUString aURL;
148 0 : rControlCommand.Arguments[i].Value >>= aURL;
149 :
150 0 : SubstituteVariables( aURL );
151 :
152 0 : Image aImage;
153 0 : if ( ReadImageFromURL( SvtMiscOptions().AreCurrentSymbolsLarge(),
154 : aURL,
155 0 : aImage ))
156 : {
157 0 : m_pToolbar->SetItemImage( m_nID, aImage );
158 :
159 : // send notification
160 0 : uno::Sequence< beans::NamedValue > aInfo( 1 );
161 0 : aInfo[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "URL" ));
162 0 : aInfo[0].Value <<= aURL;
163 : addNotifyInfo( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ImageChanged" )),
164 : getDispatchFromCommand( m_aCommandURL ),
165 0 : aInfo );
166 0 : break;
167 0 : }
168 : }
169 : }
170 0 : }
171 0 : }
172 :
173 0 : sal_Bool ImageButtonToolbarController::ReadImageFromURL( sal_Bool bBigImage, const ::rtl::OUString& aImageURL, Image& aImage )
174 : {
175 0 : SvStream* pStream = utl::UcbStreamHelper::CreateStream( aImageURL, STREAM_STD_READ );
176 0 : if ( pStream && ( pStream->GetErrorCode() == 0 ))
177 : {
178 : // Use graphic class to also support more graphic formats (bmp,png,...)
179 0 : Graphic aGraphic;
180 :
181 0 : GraphicFilter& rGF = GraphicFilter::GetGraphicFilter();
182 0 : rGF.ImportGraphic( aGraphic, String(), *pStream, GRFILTER_FORMAT_DONTKNOW );
183 :
184 0 : BitmapEx aBitmapEx = aGraphic.GetBitmapEx();
185 :
186 0 : const ::Size aSize = bBigImage ? aImageSizeBig : aImageSizeSmall; // Sizes used for toolbar images
187 :
188 0 : ::Size aBmpSize = aBitmapEx.GetSizePixel();
189 0 : if ( aBmpSize.Width() > 0 && aBmpSize.Height() > 0 )
190 : {
191 0 : ::Size aNoScaleSize( aBmpSize.Width(), aSize.Height() );
192 0 : if ( aBmpSize != aNoScaleSize )
193 0 : aBitmapEx.Scale( aNoScaleSize, BMP_SCALE_BEST );
194 0 : aImage = Image( aBitmapEx );
195 0 : return sal_True;
196 0 : }
197 : }
198 :
199 0 : delete pStream;
200 0 : return sal_False;
201 : }
202 :
203 57 : } // namespace
204 :
205 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|