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
= css::awt::ImageScaleMode
;
42 Throbber::Throbber( vcl::Window
* i_parentWindow
, WinBits i_style
)
43 :ImageControl( i_parentWindow
, i_style
)
47 ,maWaitTimer("Throbber maWaitTimer")
49 maWaitTimer
.SetTimeout( mnStepTime
);
50 maWaitTimer
.SetInvokeHandler( LINK( this, Throbber
, TimeOutHdl
) );
52 SetScaleMode( ImageScaleMode::NONE
);
61 void Throbber::dispose()
64 ImageControl::dispose();
69 ::std::vector
< Image
> lcl_loadImageSet( const Throbber::ImageSet i_imageSet
)
71 ::std::vector
< Image
> aImages
;
73 const Reference
< css::uno::XComponentContext
>& aContext( ::comphelper::getProcessComponentContext() );
74 const Reference
< XGraphicProvider
> xGraphicProvider( css::graphic::GraphicProvider::create(aContext
) );
76 ::std::vector
< OUString
> aImageURLs( Throbber::getDefaultImageURLs( i_imageSet
) );
77 aImages
.reserve( aImageURLs
.size() );
79 ::comphelper::NamedValueCollection aMediaProperties
;
80 for ( const auto& rImageURL
: aImageURLs
)
82 Reference
< XGraphic
> xGraphic
;
83 aMediaProperties
.put( u
"URL"_ustr
, rImageURL
);
84 xGraphic
= xGraphicProvider
->queryGraphic( aMediaProperties
.getPropertyValues() );
85 aImages
.emplace_back( xGraphic
);
92 void Throbber::Resize()
94 ImageControl::Resize();
98 void Throbber::initImages()
102 ::std::vector
< ::std::vector
< Image
> > aImageSets
104 lcl_loadImageSet( ImageSet::N16px
),
105 lcl_loadImageSet( ImageSet::N32px
),
106 lcl_loadImageSet( ImageSet::N64px
)
109 // find the best matching image set (size-wise)
110 const ::Size aWindowSizePixel
= GetSizePixel();
111 size_t nPreferredSet
= 0;
112 if ( aImageSets
.size() > 1 )
114 tools::Long nMinimalDistance
= ::std::numeric_limits
< tools::Long
>::max();
115 for ( ::std::vector
< ::std::vector
< Image
> >::const_iterator check
= aImageSets
.begin();
116 check
!= aImageSets
.end();
120 if ( check
->empty() )
122 SAL_WARN( "vcl.control", "Throbber::initImages: illegal image!" );
126 const Size aImageSize
= (*check
)[0].GetSizePixel();
128 if ( ( aImageSize
.Width() > aWindowSizePixel
.Width() )
129 || ( aImageSize
.Height() > aWindowSizePixel
.Height() )
131 // do not use an image set which doesn't fit into the window
134 const sal_Int64 distance
=
135 ( aWindowSizePixel
.Width() - aImageSize
.Width() ) * ( aWindowSizePixel
.Width() - aImageSize
.Width() )
136 + ( aWindowSizePixel
.Height() - aImageSize
.Height() ) * ( aWindowSizePixel
.Height() - aImageSize
.Height() );
137 if ( distance
< nMinimalDistance
)
139 nMinimalDistance
= distance
;
140 nPreferredSet
= check
- aImageSets
.begin();
145 if ( nPreferredSet
< aImageSets
.size() )
146 setImageList( std::vector(aImageSets
[nPreferredSet
]) );
148 catch( const Exception
& )
153 void Throbber::start()
155 maWaitTimer
.SetTimeout(mnStepTime
);
159 void Throbber::stop()
164 bool Throbber::isRunning() const
166 return maWaitTimer
.IsActive();
169 void Throbber::setImageList( ::std::vector
< Image
> && i_images
)
171 SAL_WARN_IF( i_images
.size()>=SAL_MAX_INT32
, "vcl.control", "Throbber::setImageList: too many images!" );
173 maImageList
= std::move(i_images
);
175 const Image
aInitialImage( !maImageList
.empty() ? maImageList
[ 0 ] : Image() );
176 SetImage( aInitialImage
);
179 ::std::vector
< OUString
> Throbber::getDefaultImageURLs( const ImageSet i_imageSet
)
181 ::std::vector
< OUString
> aImageURLs
;
183 sal_Unicode
const* const pResolutions
[] = { u
"16", u
"32", u
"64" };
184 size_t const nImageCounts
[] = { 6, 12, 12 };
187 switch ( i_imageSet
)
189 case ImageSet::N16px
: index
= 0; break;
190 case ImageSet::N32px
: index
= 1; break;
191 case ImageSet::N64px
: index
= 2; break;
194 aImageURLs
.reserve( nImageCounts
[index
] );
195 for ( size_t i
=0; i
<nImageCounts
[index
]; ++i
)
197 OUStringBuffer
aURL( OUString::Concat("private:graphicrepository/vcl/res/spinner-")
198 + pResolutions
[index
]
202 aURL
.append( OUString::number( sal_Int32( i
+ 1 ) ) + ".png" );
204 aImageURLs
.push_back( aURL
.makeStringAndClear() );
210 IMPL_LINK_NOARG(Throbber
, TimeOutHdl
, Timer
*, void)
212 SolarMutexGuard aGuard
;
213 if ( maImageList
.empty() )
216 if ( mnCurStep
< static_cast<sal_Int32
>(maImageList
.size()-1) )
231 SetImage( maImageList
[ mnCurStep
] );
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */