tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / framework / source / uielement / imagebuttontoolbarcontroller.cxx
blob206942d8d8de75041be0f94b28ff14c5cb7ad541
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::uno;
38 using namespace ::com::sun::star::beans;
39 using namespace ::com::sun::star::frame;
41 const ::Size aImageSizeSmall( 16, 16 );
42 const ::Size aImageSizeBig( 26, 26 );
44 namespace framework
47 static void SubstituteVariables( OUString& aURL )
49 aURL = comphelper::getExpandedUri(
50 comphelper::getProcessComponentContext(), aURL);
53 ImageButtonToolbarController::ImageButtonToolbarController(
54 const Reference< XComponentContext >& rxContext,
55 const Reference< XFrame >& rFrame,
56 ToolBox* pToolbar,
57 ToolBoxItemId nID,
58 const OUString& aCommand ) :
59 ComplexToolbarController( rxContext, rFrame, pToolbar, nID, aCommand )
61 bool bBigImages( SvtMiscOptions::AreCurrentSymbolsLarge() );
63 Image aImage(AddonsOptions().GetImageFromURL(aCommand, bBigImages, true));
65 // Height will be controlled by scaling according to button height
66 m_xToolbar->SetItemImage( m_nID, aImage );
69 ImageButtonToolbarController::~ImageButtonToolbarController()
73 void SAL_CALL ImageButtonToolbarController::dispose()
75 SolarMutexGuard aSolarMutexGuard;
76 ComplexToolbarController::dispose();
79 void ImageButtonToolbarController::executeControlCommand( const css::frame::ControlCommand& rControlCommand )
81 SolarMutexGuard aSolarMutexGuard;
82 // i73486 to be downward compatible use old and "wrong" also!
83 if( rControlCommand.Command != "SetImag" &&
84 rControlCommand.Command != "SetImage" )
85 return;
87 for ( const NamedValue& rArg : rControlCommand.Arguments )
89 if ( rArg.Name == "URL" )
91 OUString aURL;
92 rArg.Value >>= aURL;
94 SubstituteVariables( aURL );
96 Image aImage;
97 if ( ReadImageFromURL( SvtMiscOptions::AreCurrentSymbolsLarge(),
98 aURL,
99 aImage ))
101 m_xToolbar->SetItemImage( m_nID, aImage );
103 // send notification
104 uno::Sequence< beans::NamedValue > aInfo { { u"URL"_ustr, css::uno::Any(aURL) } };
105 addNotifyInfo( u"ImageChanged"_ustr,
106 getDispatchFromCommand( m_aCommandURL ),
107 aInfo );
108 break;
114 // static
115 bool ImageButtonToolbarController::ReadImageFromURL( bool bBigImage, const OUString& aImageURL, Image& aImage )
117 std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream( aImageURL, StreamMode::STD_READ ));
118 if ( !pStream || ( pStream->GetErrorCode() != ERRCODE_NONE ))
119 return false;
121 // Use graphic class to also support more graphic formats (bmp,png,...)
122 Graphic aGraphic;
124 GraphicFilter& rGF = GraphicFilter::GetGraphicFilter();
125 rGF.ImportGraphic( aGraphic, u"", *pStream );
127 BitmapEx aBitmapEx = aGraphic.GetBitmapEx();
129 const ::Size aSize = bBigImage ? aImageSizeBig : aImageSizeSmall; // Sizes used for toolbar images
131 ::Size aBmpSize = aBitmapEx.GetSizePixel();
132 if ( !aBmpSize.IsEmpty() )
134 ::Size aNoScaleSize( aBmpSize.Width(), aSize.Height() );
135 if ( aBmpSize != aNoScaleSize )
136 aBitmapEx.Scale( aNoScaleSize, BmpScaleFlag::BestQuality );
137 aImage = Image( aBitmapEx );
138 return true;
141 return false;
144 } // namespace
146 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */