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