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 : #include "uielement/imagebuttontoolbarcontroller.hxx"
21 :
22 : #include <framework/addonsoptions.hxx>
23 :
24 : #include <com/sun/star/util/XURLTransformer.hpp>
25 : #include <com/sun/star/frame/XDispatchProvider.hpp>
26 : #include <com/sun/star/beans/PropertyValue.hpp>
27 : #include <com/sun/star/frame/XControlNotificationListener.hpp>
28 : #include <com/sun/star/util/theMacroExpander.hpp>
29 : #include <com/sun/star/uno/XComponentContext.hpp>
30 :
31 : #include <rtl/uri.hxx>
32 : #include <osl/mutex.hxx>
33 : #include <comphelper/processfactory.hxx>
34 : #include <unotools/ucbstreamhelper.hxx>
35 : #include <vcl/svapp.hxx>
36 : #include <vcl/mnemonic.hxx>
37 : #include <vcl/window.hxx>
38 : #include <vcl/graph.hxx>
39 : #include <vcl/bitmap.hxx>
40 : #include <vcl/graphicfilter.hxx>
41 : #include <vcl/toolbox.hxx>
42 : #include <svtools/miscopt.hxx>
43 : #include <boost/scoped_ptr.hpp>
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 317 : const ::Size aImageSizeSmall( 16, 16 );
56 317 : const ::Size aImageSizeBig( 26, 26 );
57 :
58 : namespace framework
59 : {
60 :
61 317 : static uno::WeakReference< util::XMacroExpander > m_xMacroExpander;
62 :
63 0 : uno::Reference< util::XMacroExpander > GetMacroExpander()
64 : {
65 0 : uno::Reference< util::XMacroExpander > xMacroExpander( m_xMacroExpander );
66 0 : if ( !xMacroExpander.is() )
67 : {
68 0 : SolarMutexGuard aSolarMutexGuard;
69 :
70 0 : if ( !xMacroExpander.is() )
71 : {
72 : uno::Reference< uno::XComponentContext > xContext(
73 0 : comphelper::getProcessComponentContext() );
74 0 : m_xMacroExpander = util::theMacroExpander::get(xContext);
75 0 : xMacroExpander = m_xMacroExpander;
76 0 : }
77 : }
78 :
79 0 : return xMacroExpander;
80 : }
81 :
82 0 : static void SubstituteVariables( OUString& aURL )
83 : {
84 0 : if ( aURL.startsWith( EXPAND_PROTOCOL ) )
85 : {
86 0 : uno::Reference< util::XMacroExpander > xMacroExpander = GetMacroExpander();
87 :
88 : // cut protocol
89 0 : OUString aMacro( aURL.copy( sizeof ( EXPAND_PROTOCOL ) -1 ) );
90 : // decode uric class chars
91 0 : aMacro = ::rtl::Uri::decode( aMacro, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
92 : // expand macro string
93 0 : aURL = xMacroExpander->expandMacros( aMacro );
94 : }
95 0 : }
96 :
97 0 : ImageButtonToolbarController::ImageButtonToolbarController(
98 : const Reference< XComponentContext >& rxContext,
99 : const Reference< XFrame >& rFrame,
100 : ToolBox* pToolbar,
101 : sal_uInt16 nID,
102 : const OUString& aCommand ) :
103 0 : ComplexToolbarController( rxContext, rFrame, pToolbar, nID, aCommand )
104 : {
105 0 : bool bBigImages( SvtMiscOptions().AreCurrentSymbolsLarge() );
106 :
107 0 : Image aImage = AddonsOptions().GetImageFromURL( aCommand, bBigImages, true );
108 :
109 : // Height will be controlled by scaling according to button height
110 0 : m_pToolbar->SetItemImage( m_nID, aImage );
111 0 : }
112 :
113 0 : ImageButtonToolbarController::~ImageButtonToolbarController()
114 : {
115 0 : }
116 :
117 0 : void SAL_CALL ImageButtonToolbarController::dispose()
118 : throw ( RuntimeException, std::exception )
119 : {
120 0 : SolarMutexGuard aSolarMutexGuard;
121 0 : ComplexToolbarController::dispose();
122 0 : }
123 :
124 0 : void ImageButtonToolbarController::executeControlCommand( const ::com::sun::star::frame::ControlCommand& rControlCommand )
125 : {
126 0 : SolarMutexGuard aSolarMutexGuard;
127 : // i73486 to be downward compatible use old and "wrong" also!
128 0 : if( rControlCommand.Command == "SetImag" ||
129 0 : rControlCommand.Command == "SetImage" )
130 : {
131 0 : for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
132 : {
133 0 : if ( rControlCommand.Arguments[i].Name == "URL" )
134 : {
135 0 : OUString aURL;
136 0 : rControlCommand.Arguments[i].Value >>= aURL;
137 :
138 0 : SubstituteVariables( aURL );
139 :
140 0 : Image aImage;
141 0 : if ( ReadImageFromURL( SvtMiscOptions().AreCurrentSymbolsLarge(),
142 : aURL,
143 0 : aImage ))
144 : {
145 0 : m_pToolbar->SetItemImage( m_nID, aImage );
146 :
147 : // send notification
148 0 : uno::Sequence< beans::NamedValue > aInfo( 1 );
149 0 : aInfo[0].Name = "URL";
150 0 : aInfo[0].Value <<= aURL;
151 : addNotifyInfo( OUString( "ImageChanged" ),
152 : getDispatchFromCommand( m_aCommandURL ),
153 0 : aInfo );
154 0 : break;
155 0 : }
156 : }
157 : }
158 0 : }
159 0 : }
160 :
161 0 : bool ImageButtonToolbarController::ReadImageFromURL( bool bBigImage, const OUString& aImageURL, Image& aImage )
162 : {
163 0 : boost::scoped_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream( aImageURL, STREAM_STD_READ ));
164 0 : if ( pStream && ( pStream->GetErrorCode() == 0 ))
165 : {
166 : // Use graphic class to also support more graphic formats (bmp,png,...)
167 0 : Graphic aGraphic;
168 :
169 0 : GraphicFilter& rGF = GraphicFilter::GetGraphicFilter();
170 0 : rGF.ImportGraphic( aGraphic, OUString(), *pStream, GRFILTER_FORMAT_DONTKNOW );
171 :
172 0 : BitmapEx aBitmapEx = aGraphic.GetBitmapEx();
173 :
174 0 : const ::Size aSize = bBigImage ? aImageSizeBig : aImageSizeSmall; // Sizes used for toolbar images
175 :
176 0 : ::Size aBmpSize = aBitmapEx.GetSizePixel();
177 0 : if ( aBmpSize.Width() > 0 && aBmpSize.Height() > 0 )
178 : {
179 0 : ::Size aNoScaleSize( aBmpSize.Width(), aSize.Height() );
180 0 : if ( aBmpSize != aNoScaleSize )
181 0 : aBitmapEx.Scale( aNoScaleSize, BMP_SCALE_BESTQUALITY );
182 0 : aImage = Image( aBitmapEx );
183 0 : return true;
184 0 : }
185 : }
186 :
187 0 : return false;
188 : }
189 :
190 951 : } // namespace
191 :
192 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|