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/uno/XComponentContext.hpp>
29 :
30 : #include <osl/mutex.hxx>
31 : #include <comphelper/getexpandeduri.hxx>
32 : #include <comphelper/processfactory.hxx>
33 : #include <unotools/ucbstreamhelper.hxx>
34 : #include <vcl/svapp.hxx>
35 : #include <vcl/mnemonic.hxx>
36 : #include <vcl/window.hxx>
37 : #include <vcl/graph.hxx>
38 : #include <vcl/bitmap.hxx>
39 : #include <vcl/graphicfilter.hxx>
40 : #include <vcl/toolbox.hxx>
41 : #include <svtools/miscopt.hxx>
42 : #include <boost/scoped_ptr.hpp>
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 216 : const ::Size aImageSizeSmall( 16, 16 );
53 216 : const ::Size aImageSizeBig( 26, 26 );
54 :
55 : namespace framework
56 : {
57 :
58 0 : static void SubstituteVariables( OUString& aURL )
59 : {
60 0 : aURL = comphelper::getExpandedUri(
61 0 : comphelper::getProcessComponentContext(), aURL);
62 0 : }
63 :
64 0 : ImageButtonToolbarController::ImageButtonToolbarController(
65 : const Reference< XComponentContext >& rxContext,
66 : const Reference< XFrame >& rFrame,
67 : ToolBox* pToolbar,
68 : sal_uInt16 nID,
69 : const OUString& aCommand ) :
70 0 : ComplexToolbarController( rxContext, rFrame, pToolbar, nID, aCommand )
71 : {
72 0 : bool bBigImages( SvtMiscOptions().AreCurrentSymbolsLarge() );
73 :
74 0 : Image aImage = AddonsOptions().GetImageFromURL( aCommand, bBigImages, true );
75 :
76 : // Height will be controlled by scaling according to button height
77 0 : m_pToolbar->SetItemImage( m_nID, aImage );
78 0 : }
79 :
80 0 : ImageButtonToolbarController::~ImageButtonToolbarController()
81 : {
82 0 : }
83 :
84 0 : void SAL_CALL ImageButtonToolbarController::dispose()
85 : throw ( RuntimeException, std::exception )
86 : {
87 0 : SolarMutexGuard aSolarMutexGuard;
88 0 : ComplexToolbarController::dispose();
89 0 : }
90 :
91 0 : void ImageButtonToolbarController::executeControlCommand( const ::com::sun::star::frame::ControlCommand& rControlCommand )
92 : {
93 0 : SolarMutexGuard aSolarMutexGuard;
94 : // i73486 to be downward compatible use old and "wrong" also!
95 0 : if( rControlCommand.Command == "SetImag" ||
96 0 : rControlCommand.Command == "SetImage" )
97 : {
98 0 : for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
99 : {
100 0 : if ( rControlCommand.Arguments[i].Name == "URL" )
101 : {
102 0 : OUString aURL;
103 0 : rControlCommand.Arguments[i].Value >>= aURL;
104 :
105 0 : SubstituteVariables( aURL );
106 :
107 0 : Image aImage;
108 0 : if ( ReadImageFromURL( SvtMiscOptions().AreCurrentSymbolsLarge(),
109 : aURL,
110 0 : aImage ))
111 : {
112 0 : m_pToolbar->SetItemImage( m_nID, aImage );
113 :
114 : // send notification
115 0 : uno::Sequence< beans::NamedValue > aInfo( 1 );
116 0 : aInfo[0].Name = "URL";
117 0 : aInfo[0].Value <<= aURL;
118 : addNotifyInfo( OUString( "ImageChanged" ),
119 : getDispatchFromCommand( m_aCommandURL ),
120 0 : aInfo );
121 0 : break;
122 0 : }
123 : }
124 : }
125 0 : }
126 0 : }
127 :
128 0 : bool ImageButtonToolbarController::ReadImageFromURL( bool bBigImage, const OUString& aImageURL, Image& aImage )
129 : {
130 0 : boost::scoped_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream( aImageURL, STREAM_STD_READ ));
131 0 : if ( pStream && ( pStream->GetErrorCode() == 0 ))
132 : {
133 : // Use graphic class to also support more graphic formats (bmp,png,...)
134 0 : Graphic aGraphic;
135 :
136 0 : GraphicFilter& rGF = GraphicFilter::GetGraphicFilter();
137 0 : rGF.ImportGraphic( aGraphic, OUString(), *pStream, GRFILTER_FORMAT_DONTKNOW );
138 :
139 0 : BitmapEx aBitmapEx = aGraphic.GetBitmapEx();
140 :
141 0 : const ::Size aSize = bBigImage ? aImageSizeBig : aImageSizeSmall; // Sizes used for toolbar images
142 :
143 0 : ::Size aBmpSize = aBitmapEx.GetSizePixel();
144 0 : if ( aBmpSize.Width() > 0 && aBmpSize.Height() > 0 )
145 : {
146 0 : ::Size aNoScaleSize( aBmpSize.Width(), aSize.Height() );
147 0 : if ( aBmpSize != aNoScaleSize )
148 0 : aBitmapEx.Scale( aNoScaleSize, BmpScaleFlag::BestQuality );
149 0 : aImage = Image( aBitmapEx );
150 0 : return true;
151 0 : }
152 : }
153 :
154 0 : return false;
155 : }
156 :
157 648 : } // namespace
158 :
159 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|