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