Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / framework / source / uielement / imagebuttontoolbarcontroller.cxx
bloba91b0123a597272a85379f79b11d383f5386eb7a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <uielement/imagebuttontoolbarcontroller.hxx>
22 #include <framework/addonsoptions.hxx>
24 #include <com/sun/star/uno/XComponentContext.hpp>
26 #include <comphelper/getexpandeduri.hxx>
27 #include <comphelper/processfactory.hxx>
28 #include <unotools/ucbstreamhelper.hxx>
29 #include <vcl/svapp.hxx>
30 #include <vcl/graph.hxx>
31 #include <vcl/graphicfilter.hxx>
32 #include <vcl/toolbox.hxx>
33 #include <svtools/miscopt.hxx>
34 #include <memory>
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::awt;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::beans;
40 using namespace ::com::sun::star::lang;
41 using namespace ::com::sun::star::frame;
42 using namespace ::com::sun::star::util;
44 const ::Size aImageSizeSmall( 16, 16 );
45 const ::Size aImageSizeBig( 26, 26 );
47 namespace framework
50 static void SubstituteVariables( OUString& aURL )
52 aURL = comphelper::getExpandedUri(
53 comphelper::getProcessComponentContext(), aURL);
56 ImageButtonToolbarController::ImageButtonToolbarController(
57 const Reference< XComponentContext >& rxContext,
58 const Reference< XFrame >& rFrame,
59 ToolBox* pToolbar,
60 ToolBoxItemId nID,
61 const OUString& aCommand ) :
62 ComplexToolbarController( rxContext, rFrame, pToolbar, nID, aCommand )
64 bool bBigImages( SvtMiscOptions::AreCurrentSymbolsLarge() );
66 Image aImage(AddonsOptions().GetImageFromURL(aCommand, bBigImages, true));
68 // Height will be controlled by scaling according to button height
69 m_xToolbar->SetItemImage( m_nID, aImage );
72 ImageButtonToolbarController::~ImageButtonToolbarController()
76 void SAL_CALL ImageButtonToolbarController::dispose()
78 SolarMutexGuard aSolarMutexGuard;
79 ComplexToolbarController::dispose();
82 void ImageButtonToolbarController::executeControlCommand( const css::frame::ControlCommand& rControlCommand )
84 SolarMutexGuard aSolarMutexGuard;
85 // i73486 to be downward compatible use old and "wrong" also!
86 if( rControlCommand.Command != "SetImag" &&
87 rControlCommand.Command != "SetImage" )
88 return;
90 for ( const NamedValue& rArg : rControlCommand.Arguments )
92 if ( rArg.Name == "URL" )
94 OUString aURL;
95 rArg.Value >>= aURL;
97 SubstituteVariables( aURL );
99 Image aImage;
100 if ( ReadImageFromURL( SvtMiscOptions::AreCurrentSymbolsLarge(),
101 aURL,
102 aImage ))
104 m_xToolbar->SetItemImage( m_nID, aImage );
106 // send notification
107 uno::Sequence< beans::NamedValue > aInfo { { "URL", css::uno::Any(aURL) } };
108 addNotifyInfo( "ImageChanged",
109 getDispatchFromCommand( m_aCommandURL ),
110 aInfo );
111 break;
117 bool ImageButtonToolbarController::ReadImageFromURL( bool bBigImage, const OUString& aImageURL, Image& aImage )
119 std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream( aImageURL, StreamMode::STD_READ ));
120 if ( !pStream || ( pStream->GetErrorCode() != ERRCODE_NONE ))
121 return false;
123 // Use graphic class to also support more graphic formats (bmp,png,...)
124 Graphic aGraphic;
126 GraphicFilter& rGF = GraphicFilter::GetGraphicFilter();
127 rGF.ImportGraphic( aGraphic, u"", *pStream );
129 BitmapEx aBitmapEx = aGraphic.GetBitmapEx();
131 const ::Size aSize = bBigImage ? aImageSizeBig : aImageSizeSmall; // Sizes used for toolbar images
133 ::Size aBmpSize = aBitmapEx.GetSizePixel();
134 if ( !aBmpSize.IsEmpty() )
136 ::Size aNoScaleSize( aBmpSize.Width(), aSize.Height() );
137 if ( aBmpSize != aNoScaleSize )
138 aBitmapEx.Scale( aNoScaleSize, BmpScaleFlag::BestQuality );
139 aImage = Image( aBitmapEx );
140 return true;
143 return false;
146 } // namespace
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */