1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <vcl/toolkit/throbber.hxx>
21 #include <vcl/svapp.hxx>
23 #include <com/sun/star/uno/XComponentContext.hpp>
24 #include <com/sun/star/graphic/GraphicProvider.hpp>
25 #include <com/sun/star/graphic/XGraphicProvider.hpp>
26 #include <com/sun/star/awt/ImageScaleMode.hpp>
28 #include <comphelper/namedvaluecollection.hxx>
29 #include <comphelper/processfactory.hxx>
30 #include <rtl/ustrbuf.hxx>
31 #include <sal/log.hxx>
32 #include <tools/urlobj.hxx>
36 using ::com::sun::star::uno::Reference
;
37 using ::com::sun::star::graphic::XGraphic
;
38 using ::com::sun::star::graphic::XGraphicProvider
;
39 using ::com::sun::star::uno::Exception
;
40 namespace ImageScaleMode
= ::com::sun::star::awt::ImageScaleMode
;
42 Throbber::Throbber( vcl::Window
* i_parentWindow
, WinBits i_style
)
43 :ImageControl( i_parentWindow
, i_style
)
48 maWaitTimer
.SetTimeout( mnStepTime
);
49 maWaitTimer
.SetInvokeHandler( LINK( this, Throbber
, TimeOutHdl
) );
51 SetScaleMode( ImageScaleMode::NONE
);
60 void Throbber::dispose()
63 ImageControl::dispose();
68 ::std::vector
< Image
> lcl_loadImageSet( const Throbber::ImageSet i_imageSet
)
70 ::std::vector
< Image
> aImages
;
72 const Reference
< css::uno::XComponentContext
> aContext( ::comphelper::getProcessComponentContext() );
73 const Reference
< XGraphicProvider
> xGraphicProvider( css::graphic::GraphicProvider::create(aContext
) );
75 ::std::vector
< OUString
> aImageURLs( Throbber::getDefaultImageURLs( i_imageSet
) );
76 aImages
.reserve( aImageURLs
.size() );
78 ::comphelper::NamedValueCollection aMediaProperties
;
79 for ( const auto& rImageURL
: aImageURLs
)
81 Reference
< XGraphic
> xGraphic
;
82 aMediaProperties
.put( "URL", rImageURL
);
83 xGraphic
= xGraphicProvider
->queryGraphic( aMediaProperties
.getPropertyValues() );
84 aImages
.emplace_back( xGraphic
);
91 void Throbber::Resize()
93 ImageControl::Resize();
97 void Throbber::initImages()
101 ::std::vector
< ::std::vector
< Image
> > aImageSets
;
102 aImageSets
.push_back( lcl_loadImageSet( ImageSet::N16px
) );
103 aImageSets
.push_back( lcl_loadImageSet( ImageSet::N32px
) );
104 aImageSets
.push_back( lcl_loadImageSet( ImageSet::N64px
) );
106 // find the best matching image set (size-wise)
107 const ::Size aWindowSizePixel
= GetSizePixel();
108 size_t nPreferredSet
= 0;
109 if ( aImageSets
.size() > 1 )
111 tools::Long nMinimalDistance
= ::std::numeric_limits
< tools::Long
>::max();
112 for ( ::std::vector
< ::std::vector
< Image
> >::const_iterator check
= aImageSets
.begin();
113 check
!= aImageSets
.end();
117 if ( check
->empty() )
119 SAL_WARN( "vcl.control", "Throbber::initImages: illegal image!" );
123 const Size aImageSize
= (*check
)[0].GetSizePixel();
125 if ( ( aImageSize
.Width() > aWindowSizePixel
.Width() )
126 || ( aImageSize
.Height() > aWindowSizePixel
.Height() )
128 // do not use an image set which doesn't fit into the window
131 const sal_Int64 distance
=
132 ( aWindowSizePixel
.Width() - aImageSize
.Width() ) * ( aWindowSizePixel
.Width() - aImageSize
.Width() )
133 + ( aWindowSizePixel
.Height() - aImageSize
.Height() ) * ( aWindowSizePixel
.Height() - aImageSize
.Height() );
134 if ( distance
< nMinimalDistance
)
136 nMinimalDistance
= distance
;
137 nPreferredSet
= check
- aImageSets
.begin();
142 if ( nPreferredSet
< aImageSets
.size() )
143 setImageList( aImageSets
[nPreferredSet
] );
145 catch( const Exception
& )
150 void Throbber::start()
155 void Throbber::stop()
160 bool Throbber::isRunning() const
162 return maWaitTimer
.IsActive();
165 void Throbber::setImageList( ::std::vector
< Image
> const& i_images
)
167 SAL_WARN_IF( i_images
.size()>=SAL_MAX_INT32
, "vcl.control", "Throbber::setImageList: too many images!" );
169 maImageList
= i_images
;
171 const Image
aInitialImage( !maImageList
.empty() ? maImageList
[ 0 ] : Image() );
172 SetImage( aInitialImage
);
175 ::std::vector
< OUString
> Throbber::getDefaultImageURLs( const ImageSet i_imageSet
)
177 ::std::vector
< OUString
> aImageURLs
;
179 char const* const pResolutions
[] = { "16", "32", "64" };
180 size_t const nImageCounts
[] = { 6, 12, 12 };
183 switch ( i_imageSet
)
185 case ImageSet::N16px
: index
= 0; break;
186 case ImageSet::N32px
: index
= 1; break;
187 case ImageSet::N64px
: index
= 2; break;
190 aImageURLs
.reserve( nImageCounts
[index
] );
191 for ( size_t i
=0; i
<nImageCounts
[index
]; ++i
)
194 aURL
.append( "private:graphicrepository/vcl/res/spinner-" );
195 aURL
.appendAscii( pResolutions
[index
] );
199 aURL
.append ( sal_Int32( i
+ 1 ) );
200 aURL
.append( ".png" );
202 aImageURLs
.push_back( aURL
.makeStringAndClear() );
208 IMPL_LINK_NOARG(Throbber
, TimeOutHdl
, Timer
*, void)
210 SolarMutexGuard aGuard
;
211 if ( maImageList
.empty() )
214 if ( mnCurStep
< static_cast<sal_Int32
>(maImageList
.size()-1) )
229 SetImage( maImageList
[ mnCurStep
] );
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */