19f62ea84SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
39f62ea84SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
49f62ea84SAndrew Rist * or more contributor license agreements. See the NOTICE file
59f62ea84SAndrew Rist * distributed with this work for additional information
69f62ea84SAndrew Rist * regarding copyright ownership. The ASF licenses this file
79f62ea84SAndrew Rist * to you under the Apache License, Version 2.0 (the
89f62ea84SAndrew Rist * "License"); you may not use this file except in compliance
99f62ea84SAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
119f62ea84SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
139f62ea84SAndrew Rist * Unless required by applicable law or agreed to in writing,
149f62ea84SAndrew Rist * software distributed under the License is distributed on an
159f62ea84SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169f62ea84SAndrew Rist * KIND, either express or implied. See the License for the
179f62ea84SAndrew Rist * specific language governing permissions and limitations
189f62ea84SAndrew Rist * under the License.
19cdf0e10cSrcweir *
209f62ea84SAndrew Rist *************************************************************/
219f62ea84SAndrew Rist
22cdf0e10cSrcweir #include "precompiled_vcl.hxx"
23cdf0e10cSrcweir
24cdf0e10cSrcweir #include "vcl/throbber.hxx"
25cdf0e10cSrcweir #include "vcl/svapp.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <com/sun/star/graphic/XGraphicProvider.hpp>
28cdf0e10cSrcweir #include <com/sun/star/awt/ImageScaleMode.hpp>
29cdf0e10cSrcweir
30cdf0e10cSrcweir #include <comphelper/componentcontext.hxx>
31cdf0e10cSrcweir #include <comphelper/namedvaluecollection.hxx>
32cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
33cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
34cdf0e10cSrcweir #include <tools/diagnose_ex.h>
35cdf0e10cSrcweir #include <tools/urlobj.hxx>
36cdf0e10cSrcweir
37cdf0e10cSrcweir #include <limits>
38cdf0e10cSrcweir
39cdf0e10cSrcweir using ::com::sun::star::uno::Sequence;
40cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
41cdf0e10cSrcweir using ::com::sun::star::graphic::XGraphic;
42cdf0e10cSrcweir using ::com::sun::star::graphic::XGraphicProvider;
43cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY_THROW;
44cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY;
45cdf0e10cSrcweir using ::com::sun::star::uno::Exception;
46cdf0e10cSrcweir namespace ImageScaleMode = ::com::sun::star::awt::ImageScaleMode;
47cdf0e10cSrcweir
48cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
Throbber(Window * i_parentWindow,WinBits i_style,const ImageSet i_imageSet)49cdf0e10cSrcweir Throbber::Throbber( Window* i_parentWindow, WinBits i_style, const ImageSet i_imageSet )
50cdf0e10cSrcweir :ImageControl( i_parentWindow, i_style )
51cdf0e10cSrcweir ,mbRepeat( sal_True )
52cdf0e10cSrcweir ,mnStepTime( 100 )
53cdf0e10cSrcweir ,mnCurStep( 0 )
54cdf0e10cSrcweir ,mnStepCount( 0 )
55cdf0e10cSrcweir ,meImageSet( i_imageSet )
56cdf0e10cSrcweir {
57cdf0e10cSrcweir maWaitTimer.SetTimeout( mnStepTime );
58cdf0e10cSrcweir maWaitTimer.SetTimeoutHdl( LINK( this, Throbber, TimeOutHdl ) );
59cdf0e10cSrcweir
60b6dc695eSAriel Constenla-Haile SetScaleMode( ImageScaleMode::NONE );
61cdf0e10cSrcweir initImages();
62cdf0e10cSrcweir }
63cdf0e10cSrcweir
64cdf0e10cSrcweir //--------------------------------------------------------------------
Throbber(Window * i_parentWindow,const ResId & i_resId,const ImageSet i_imageSet)65cdf0e10cSrcweir Throbber::Throbber( Window* i_parentWindow, const ResId& i_resId, const ImageSet i_imageSet )
66cdf0e10cSrcweir :ImageControl( i_parentWindow, i_resId )
67cdf0e10cSrcweir ,mbRepeat( sal_True )
68cdf0e10cSrcweir ,mnStepTime( 100 )
69cdf0e10cSrcweir ,mnCurStep( 0 )
70cdf0e10cSrcweir ,mnStepCount( 0 )
71cdf0e10cSrcweir ,meImageSet( i_imageSet )
72cdf0e10cSrcweir {
73cdf0e10cSrcweir maWaitTimer.SetTimeout( mnStepTime );
74cdf0e10cSrcweir maWaitTimer.SetTimeoutHdl( LINK( this, Throbber, TimeOutHdl ) );
75cdf0e10cSrcweir
76b6dc695eSAriel Constenla-Haile SetScaleMode( ImageScaleMode::NONE );
77cdf0e10cSrcweir initImages();
78cdf0e10cSrcweir }
79cdf0e10cSrcweir
80cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
~Throbber()81cdf0e10cSrcweir Throbber::~Throbber()
82cdf0e10cSrcweir {
83cdf0e10cSrcweir maWaitTimer.Stop();
84cdf0e10cSrcweir }
85cdf0e10cSrcweir
86cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
87cdf0e10cSrcweir namespace
88cdf0e10cSrcweir {
lcl_loadImageSet(const Throbber::ImageSet i_imageSet,const bool i_isHiContrast)89cdf0e10cSrcweir ::std::vector< Image > lcl_loadImageSet( const Throbber::ImageSet i_imageSet, const bool i_isHiContrast )
90cdf0e10cSrcweir {
91cdf0e10cSrcweir ::std::vector< Image > aImages;
92cdf0e10cSrcweir ENSURE_OR_RETURN( i_imageSet != Throbber::IMAGES_NONE, "lcl_loadImageSet: illegal image set", aImages );
93cdf0e10cSrcweir
94cdf0e10cSrcweir const ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() );
95cdf0e10cSrcweir const Reference< XGraphicProvider > xGraphicProvider( aContext.createComponent( "com.sun.star.graphic.GraphicProvider" ), UNO_QUERY_THROW );
96cdf0e10cSrcweir
97cdf0e10cSrcweir ::std::vector< ::rtl::OUString > aImageURLs( Throbber::getDefaultImageURLs( i_imageSet ) );
98cdf0e10cSrcweir aImages.reserve( aImageURLs.size() );
99cdf0e10cSrcweir
100cdf0e10cSrcweir ::comphelper::NamedValueCollection aMediaProperties;
101cdf0e10cSrcweir for ( ::std::vector< ::rtl::OUString >::const_iterator imageURL = aImageURLs.begin();
102cdf0e10cSrcweir imageURL != aImageURLs.end();
103cdf0e10cSrcweir ++imageURL
104cdf0e10cSrcweir )
105cdf0e10cSrcweir {
106cdf0e10cSrcweir Reference< XGraphic > xGraphic;
107cdf0e10cSrcweir if ( i_isHiContrast )
108cdf0e10cSrcweir {
109aeee3b8fSAriel Constenla-Haile INetURLObject aURL( *imageURL );
110aeee3b8fSAriel Constenla-Haile if ( aURL.GetProtocol() != INET_PROT_PRIV_SOFFICE )
111aeee3b8fSAriel Constenla-Haile {
112aeee3b8fSAriel Constenla-Haile const sal_Int32 separatorPos = imageURL->lastIndexOf( '/' );
113aeee3b8fSAriel Constenla-Haile if ( separatorPos != -1 )
114aeee3b8fSAriel Constenla-Haile {
115aeee3b8fSAriel Constenla-Haile ::rtl::OUStringBuffer composer;
116aeee3b8fSAriel Constenla-Haile composer.append( imageURL->copy( 0, separatorPos ) );
117aeee3b8fSAriel Constenla-Haile composer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "/hicontrast" ) );
118aeee3b8fSAriel Constenla-Haile composer.append( imageURL->copy( separatorPos ) );
119aeee3b8fSAriel Constenla-Haile
120aeee3b8fSAriel Constenla-Haile aMediaProperties.put( "URL", composer.makeStringAndClear() );
121cdf0e10cSrcweir xGraphic.set( xGraphicProvider->queryGraphic( aMediaProperties.getPropertyValues() ), UNO_QUERY );
122cdf0e10cSrcweir }
123aeee3b8fSAriel Constenla-Haile }
124aeee3b8fSAriel Constenla-Haile }
125cdf0e10cSrcweir if ( !xGraphic.is() )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir aMediaProperties.put( "URL", *imageURL );
128cdf0e10cSrcweir xGraphic.set( xGraphicProvider->queryGraphic( aMediaProperties.getPropertyValues() ), UNO_QUERY );
129cdf0e10cSrcweir }
130cdf0e10cSrcweir aImages.push_back( Image( xGraphic ) );
131cdf0e10cSrcweir }
132cdf0e10cSrcweir
133cdf0e10cSrcweir return aImages;
134cdf0e10cSrcweir }
135cdf0e10cSrcweir }
136cdf0e10cSrcweir
137cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
Resize()138cdf0e10cSrcweir void Throbber::Resize()
139cdf0e10cSrcweir {
140cdf0e10cSrcweir ImageControl::Resize();
141cdf0e10cSrcweir
142cdf0e10cSrcweir if ( meImageSet == IMAGES_AUTO )
143cdf0e10cSrcweir initImages();
144cdf0e10cSrcweir }
145cdf0e10cSrcweir
146cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
initImages()147cdf0e10cSrcweir void Throbber::initImages()
148cdf0e10cSrcweir {
149cdf0e10cSrcweir if ( meImageSet == IMAGES_NONE )
150cdf0e10cSrcweir return;
151cdf0e10cSrcweir
152cdf0e10cSrcweir try
153cdf0e10cSrcweir {
154cdf0e10cSrcweir ::std::vector< ::std::vector< Image > > aImageSets;
155cdf0e10cSrcweir const bool isHiContrast = GetSettings().GetStyleSettings().GetHighContrastMode();
156cdf0e10cSrcweir if ( meImageSet == IMAGES_AUTO )
157cdf0e10cSrcweir {
158cdf0e10cSrcweir aImageSets.push_back( lcl_loadImageSet( IMAGES_16_PX, isHiContrast ) );
159cdf0e10cSrcweir aImageSets.push_back( lcl_loadImageSet( IMAGES_32_PX, isHiContrast ) );
160cdf0e10cSrcweir aImageSets.push_back( lcl_loadImageSet( IMAGES_64_PX, isHiContrast ) );
161cdf0e10cSrcweir }
162cdf0e10cSrcweir else
163cdf0e10cSrcweir {
164cdf0e10cSrcweir aImageSets.push_back( lcl_loadImageSet( meImageSet, isHiContrast ) );
165cdf0e10cSrcweir }
166cdf0e10cSrcweir
167cdf0e10cSrcweir // find the best matching image set (size-wise)
168cdf0e10cSrcweir const ::Size aWindowSizePixel = GetSizePixel();
169cdf0e10cSrcweir size_t nPreferredSet = 0;
170cdf0e10cSrcweir if ( aImageSets.size() > 1 )
171cdf0e10cSrcweir {
172cdf0e10cSrcweir long nMinimalDistance = ::std::numeric_limits< long >::max();
173cdf0e10cSrcweir for ( ::std::vector< ::std::vector< Image > >::const_iterator check = aImageSets.begin();
174cdf0e10cSrcweir check != aImageSets.end();
175cdf0e10cSrcweir ++check
176cdf0e10cSrcweir )
177cdf0e10cSrcweir {
178cdf0e10cSrcweir ENSURE_OR_CONTINUE( !check->empty(), "Throbber::initImages: illegal image!" );
179cdf0e10cSrcweir const Size aImageSize = (*check)[0].GetSizePixel();
180cdf0e10cSrcweir
181cdf0e10cSrcweir if ( ( aImageSize.Width() > aWindowSizePixel.Width() )
182cdf0e10cSrcweir || ( aImageSize.Height() > aWindowSizePixel.Height() )
183cdf0e10cSrcweir )
184cdf0e10cSrcweir // do not use an image set which doesn't fit into the window
185cdf0e10cSrcweir continue;
186cdf0e10cSrcweir
187cdf0e10cSrcweir const sal_Int64 distance =
188cdf0e10cSrcweir ( aWindowSizePixel.Width() - aImageSize.Width() ) * ( aWindowSizePixel.Width() - aImageSize.Width() )
189cdf0e10cSrcweir + ( aWindowSizePixel.Height() - aImageSize.Height() ) * ( aWindowSizePixel.Height() - aImageSize.Height() );
190cdf0e10cSrcweir if ( distance < nMinimalDistance )
191cdf0e10cSrcweir {
192cdf0e10cSrcweir nMinimalDistance = distance;
193cdf0e10cSrcweir nPreferredSet = check - aImageSets.begin();
194cdf0e10cSrcweir }
195cdf0e10cSrcweir }
196cdf0e10cSrcweir }
197cdf0e10cSrcweir
198cdf0e10cSrcweir if ( nPreferredSet < aImageSets.size() )
199cdf0e10cSrcweir setImageList( aImageSets[nPreferredSet] );
200cdf0e10cSrcweir }
201cdf0e10cSrcweir catch( const Exception& )
202cdf0e10cSrcweir {
203cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION();
204cdf0e10cSrcweir }
205cdf0e10cSrcweir }
206cdf0e10cSrcweir
207cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
start()208cdf0e10cSrcweir void Throbber::start()
209cdf0e10cSrcweir {
210cdf0e10cSrcweir maWaitTimer.Start();
211cdf0e10cSrcweir }
212cdf0e10cSrcweir
213cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
stop()214cdf0e10cSrcweir void Throbber::stop()
215cdf0e10cSrcweir {
216cdf0e10cSrcweir maWaitTimer.Stop();
217cdf0e10cSrcweir }
218cdf0e10cSrcweir
219cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
isRunning() const220cdf0e10cSrcweir bool Throbber::isRunning() const
221cdf0e10cSrcweir {
222cdf0e10cSrcweir return maWaitTimer.IsActive();
223cdf0e10cSrcweir }
224cdf0e10cSrcweir
225cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
setImageList(::std::vector<Image> const & i_images)226cdf0e10cSrcweir void Throbber::setImageList( ::std::vector< Image > const& i_images )
227cdf0e10cSrcweir {
228cdf0e10cSrcweir maImageList = i_images;
229cdf0e10cSrcweir
230cdf0e10cSrcweir mnStepCount = maImageList.size();
231cdf0e10cSrcweir const Image aInitialImage( mnStepCount ? maImageList[ 0 ] : Image() );
232cdf0e10cSrcweir SetImage( aInitialImage );
233cdf0e10cSrcweir }
234cdf0e10cSrcweir
235cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
setImageList(const Sequence<Reference<XGraphic>> & rImageList)236cdf0e10cSrcweir void Throbber::setImageList( const Sequence< Reference< XGraphic > >& rImageList )
237cdf0e10cSrcweir {
238cdf0e10cSrcweir ::std::vector< Image > aImages( rImageList.getLength() );
239cdf0e10cSrcweir ::std::copy(
240cdf0e10cSrcweir rImageList.getConstArray(),
241cdf0e10cSrcweir rImageList.getConstArray() + rImageList.getLength(),
242cdf0e10cSrcweir aImages.begin()
243cdf0e10cSrcweir );
244cdf0e10cSrcweir setImageList( aImages );
245cdf0e10cSrcweir }
246cdf0e10cSrcweir
247cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
getDefaultImageURLs(const ImageSet i_imageSet)248cdf0e10cSrcweir ::std::vector< ::rtl::OUString > Throbber::getDefaultImageURLs( const ImageSet i_imageSet )
249cdf0e10cSrcweir {
250cdf0e10cSrcweir ::std::vector< ::rtl::OUString > aImageURLs;
251cdf0e10cSrcweir
252cdf0e10cSrcweir sal_Char const* const pResolutions[] = { "16", "32", "64" };
253*2dce64d2Smseidel size_t const nImageCounts[] = { 12, 12, 12 };
254cdf0e10cSrcweir
255cdf0e10cSrcweir size_t index = 0;
256cdf0e10cSrcweir switch ( i_imageSet )
257cdf0e10cSrcweir {
258cdf0e10cSrcweir case IMAGES_16_PX: index = 0; break;
259cdf0e10cSrcweir case IMAGES_32_PX: index = 1; break;
260cdf0e10cSrcweir case IMAGES_64_PX: index = 2; break;
261cdf0e10cSrcweir case IMAGES_NONE:
262cdf0e10cSrcweir case IMAGES_AUTO:
263cdf0e10cSrcweir OSL_ENSURE( false, "Throbber::getDefaultImageURLs: illegal image set!" );
264cdf0e10cSrcweir return aImageURLs;
265cdf0e10cSrcweir }
266cdf0e10cSrcweir
267cdf0e10cSrcweir aImageURLs.reserve( nImageCounts[index] );
268cdf0e10cSrcweir for ( size_t i=0; i<nImageCounts[index]; ++i )
269cdf0e10cSrcweir {
270cdf0e10cSrcweir ::rtl::OUStringBuffer aURL;
271aeee3b8fSAriel Constenla-Haile aURL.appendAscii( "private:graphicrepository/vcl/res/spinner-" );
272cdf0e10cSrcweir aURL.appendAscii( pResolutions[index] );
273cdf0e10cSrcweir aURL.appendAscii( "-" );
274cdf0e10cSrcweir if ( i < 9 )
275cdf0e10cSrcweir aURL.appendAscii( "0" );
276cdf0e10cSrcweir aURL.append ( sal_Int32( i + 1 ) );
277cdf0e10cSrcweir aURL.appendAscii( ".png" );
278cdf0e10cSrcweir
279cdf0e10cSrcweir aImageURLs.push_back( aURL.makeStringAndClear() );
280cdf0e10cSrcweir }
281cdf0e10cSrcweir
282cdf0e10cSrcweir return aImageURLs;
283cdf0e10cSrcweir }
284cdf0e10cSrcweir
285cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
IMPL_LINK(Throbber,TimeOutHdl,void *,EMPTYARG)286cdf0e10cSrcweir IMPL_LINK( Throbber, TimeOutHdl, void*, EMPTYARG )
287cdf0e10cSrcweir {
288cdf0e10cSrcweir ::vos::OGuard aGuard( Application::GetSolarMutex() );
289cdf0e10cSrcweir if ( maImageList.empty() )
290cdf0e10cSrcweir return 0;
291cdf0e10cSrcweir
292cdf0e10cSrcweir if ( mnCurStep < mnStepCount - 1 )
293cdf0e10cSrcweir mnCurStep += 1;
294cdf0e10cSrcweir else
295cdf0e10cSrcweir {
296cdf0e10cSrcweir if ( mbRepeat )
297cdf0e10cSrcweir {
298cdf0e10cSrcweir // start over
299cdf0e10cSrcweir mnCurStep = 0;
300cdf0e10cSrcweir }
301cdf0e10cSrcweir else
302cdf0e10cSrcweir {
303cdf0e10cSrcweir stop();
304cdf0e10cSrcweir }
305cdf0e10cSrcweir }
306cdf0e10cSrcweir
307cdf0e10cSrcweir SetImage( maImageList[ mnCurStep ] );
308cdf0e10cSrcweir
309cdf0e10cSrcweir return 0;
310cdf0e10cSrcweir }
311*2dce64d2Smseidel
312*2dce64d2Smseidel /* vim: set noet sw=4 ts=4: */
313