xref: /AOO41X/main/vcl/source/control/throbber.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "precompiled_vcl.hxx"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include "vcl/throbber.hxx"
31*cdf0e10cSrcweir #include "vcl/svapp.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <com/sun/star/graphic/XGraphicProvider.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/awt/ImageScaleMode.hpp>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <comphelper/componentcontext.hxx>
37*cdf0e10cSrcweir #include <comphelper/namedvaluecollection.hxx>
38*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
39*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
40*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
41*cdf0e10cSrcweir #include <tools/urlobj.hxx>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include <limits>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir using ::com::sun::star::uno::Sequence;
46*cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
47*cdf0e10cSrcweir using ::com::sun::star::graphic::XGraphic;
48*cdf0e10cSrcweir using ::com::sun::star::graphic::XGraphicProvider;
49*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY_THROW;
50*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY;
51*cdf0e10cSrcweir using ::com::sun::star::uno::Exception;
52*cdf0e10cSrcweir namespace ImageScaleMode = ::com::sun::star::awt::ImageScaleMode;
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
55*cdf0e10cSrcweir Throbber::Throbber( Window* i_parentWindow, WinBits i_style, const ImageSet i_imageSet )
56*cdf0e10cSrcweir     :ImageControl( i_parentWindow, i_style )
57*cdf0e10cSrcweir     ,mbRepeat( sal_True )
58*cdf0e10cSrcweir     ,mnStepTime( 100 )
59*cdf0e10cSrcweir     ,mnCurStep( 0 )
60*cdf0e10cSrcweir     ,mnStepCount( 0 )
61*cdf0e10cSrcweir     ,meImageSet( i_imageSet )
62*cdf0e10cSrcweir {
63*cdf0e10cSrcweir     maWaitTimer.SetTimeout( mnStepTime );
64*cdf0e10cSrcweir     maWaitTimer.SetTimeoutHdl( LINK( this, Throbber, TimeOutHdl ) );
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir     SetScaleMode( ImageScaleMode::None );
67*cdf0e10cSrcweir     initImages();
68*cdf0e10cSrcweir }
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir //--------------------------------------------------------------------
71*cdf0e10cSrcweir Throbber::Throbber( Window* i_parentWindow, const ResId& i_resId, const ImageSet i_imageSet )
72*cdf0e10cSrcweir     :ImageControl( i_parentWindow, i_resId )
73*cdf0e10cSrcweir     ,mbRepeat( sal_True )
74*cdf0e10cSrcweir     ,mnStepTime( 100 )
75*cdf0e10cSrcweir     ,mnCurStep( 0 )
76*cdf0e10cSrcweir     ,mnStepCount( 0 )
77*cdf0e10cSrcweir     ,meImageSet( i_imageSet )
78*cdf0e10cSrcweir {
79*cdf0e10cSrcweir     maWaitTimer.SetTimeout( mnStepTime );
80*cdf0e10cSrcweir     maWaitTimer.SetTimeoutHdl( LINK( this, Throbber, TimeOutHdl ) );
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir     SetScaleMode( ImageScaleMode::None );
83*cdf0e10cSrcweir     initImages();
84*cdf0e10cSrcweir }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
87*cdf0e10cSrcweir Throbber::~Throbber()
88*cdf0e10cSrcweir {
89*cdf0e10cSrcweir     maWaitTimer.Stop();
90*cdf0e10cSrcweir }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
93*cdf0e10cSrcweir namespace
94*cdf0e10cSrcweir {
95*cdf0e10cSrcweir     //..................................................................................................................
96*cdf0e10cSrcweir     ::rtl::OUString lcl_getHighContrastURL( ::rtl::OUString const& i_imageURL )
97*cdf0e10cSrcweir     {
98*cdf0e10cSrcweir         INetURLObject aURL( i_imageURL );
99*cdf0e10cSrcweir         if ( aURL.GetProtocol() != INET_PROT_PRIV_SOFFICE )
100*cdf0e10cSrcweir         {
101*cdf0e10cSrcweir             OSL_VERIFY( aURL.insertName( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "hicontrast" ) ), false, 0 ) );
102*cdf0e10cSrcweir             return aURL.GetMainURL( INetURLObject::NO_DECODE );
103*cdf0e10cSrcweir         }
104*cdf0e10cSrcweir         // the private: scheme is not considered to be hierarchical by INetURLObject, so manually insert the
105*cdf0e10cSrcweir         // segment
106*cdf0e10cSrcweir         const sal_Int32 separatorPos = i_imageURL.indexOf( '/' );
107*cdf0e10cSrcweir         ENSURE_OR_RETURN( separatorPos != -1, "lcl_getHighContrastURL: unsipported URL scheme - cannot automatically determine HC version!", i_imageURL );
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir         ::rtl::OUStringBuffer composer;
110*cdf0e10cSrcweir         composer.append( i_imageURL.copy( 0, separatorPos ) );
111*cdf0e10cSrcweir         composer.appendAscii( "/hicontrast" );
112*cdf0e10cSrcweir         composer.append( i_imageURL.copy( separatorPos ) );
113*cdf0e10cSrcweir         return composer.makeStringAndClear();
114*cdf0e10cSrcweir     }
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir     //..................................................................................................................
117*cdf0e10cSrcweir     ::std::vector< Image > lcl_loadImageSet( const Throbber::ImageSet i_imageSet, const bool i_isHiContrast )
118*cdf0e10cSrcweir     {
119*cdf0e10cSrcweir         ::std::vector< Image > aImages;
120*cdf0e10cSrcweir         ENSURE_OR_RETURN( i_imageSet != Throbber::IMAGES_NONE, "lcl_loadImageSet: illegal image set", aImages );
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir         const ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() );
123*cdf0e10cSrcweir         const Reference< XGraphicProvider > xGraphicProvider( aContext.createComponent( "com.sun.star.graphic.GraphicProvider" ), UNO_QUERY_THROW );
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir         ::std::vector< ::rtl::OUString > aImageURLs( Throbber::getDefaultImageURLs( i_imageSet ) );
126*cdf0e10cSrcweir         aImages.reserve( aImageURLs.size() );
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir         ::comphelper::NamedValueCollection aMediaProperties;
129*cdf0e10cSrcweir         for (   ::std::vector< ::rtl::OUString >::const_iterator imageURL = aImageURLs.begin();
130*cdf0e10cSrcweir                 imageURL != aImageURLs.end();
131*cdf0e10cSrcweir                 ++imageURL
132*cdf0e10cSrcweir             )
133*cdf0e10cSrcweir         {
134*cdf0e10cSrcweir             Reference< XGraphic > xGraphic;
135*cdf0e10cSrcweir             if ( i_isHiContrast )
136*cdf0e10cSrcweir             {
137*cdf0e10cSrcweir                 aMediaProperties.put( "URL", lcl_getHighContrastURL( *imageURL ) );
138*cdf0e10cSrcweir                 xGraphic.set( xGraphicProvider->queryGraphic( aMediaProperties.getPropertyValues() ), UNO_QUERY );
139*cdf0e10cSrcweir             }
140*cdf0e10cSrcweir             if ( !xGraphic.is() )
141*cdf0e10cSrcweir             {
142*cdf0e10cSrcweir                 aMediaProperties.put( "URL", *imageURL );
143*cdf0e10cSrcweir                 xGraphic.set( xGraphicProvider->queryGraphic( aMediaProperties.getPropertyValues() ), UNO_QUERY );
144*cdf0e10cSrcweir             }
145*cdf0e10cSrcweir             aImages.push_back( Image( xGraphic ) );
146*cdf0e10cSrcweir         }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir         return aImages;
149*cdf0e10cSrcweir     }
150*cdf0e10cSrcweir }
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
153*cdf0e10cSrcweir void Throbber::Resize()
154*cdf0e10cSrcweir {
155*cdf0e10cSrcweir     ImageControl::Resize();
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir     if ( meImageSet == IMAGES_AUTO )
158*cdf0e10cSrcweir         initImages();
159*cdf0e10cSrcweir }
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
162*cdf0e10cSrcweir void Throbber::initImages()
163*cdf0e10cSrcweir {
164*cdf0e10cSrcweir     if ( meImageSet == IMAGES_NONE )
165*cdf0e10cSrcweir         return;
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir     try
168*cdf0e10cSrcweir     {
169*cdf0e10cSrcweir         ::std::vector< ::std::vector< Image > > aImageSets;
170*cdf0e10cSrcweir         const bool isHiContrast = GetSettings().GetStyleSettings().GetHighContrastMode();
171*cdf0e10cSrcweir         if ( meImageSet == IMAGES_AUTO )
172*cdf0e10cSrcweir         {
173*cdf0e10cSrcweir             aImageSets.push_back( lcl_loadImageSet( IMAGES_16_PX, isHiContrast ) );
174*cdf0e10cSrcweir             aImageSets.push_back( lcl_loadImageSet( IMAGES_32_PX, isHiContrast ) );
175*cdf0e10cSrcweir             aImageSets.push_back( lcl_loadImageSet( IMAGES_64_PX, isHiContrast ) );
176*cdf0e10cSrcweir         }
177*cdf0e10cSrcweir         else
178*cdf0e10cSrcweir         {
179*cdf0e10cSrcweir             aImageSets.push_back( lcl_loadImageSet( meImageSet, isHiContrast ) );
180*cdf0e10cSrcweir         }
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir         // find the best matching image set (size-wise)
183*cdf0e10cSrcweir         const ::Size aWindowSizePixel = GetSizePixel();
184*cdf0e10cSrcweir         size_t nPreferredSet = 0;
185*cdf0e10cSrcweir         if ( aImageSets.size() > 1 )
186*cdf0e10cSrcweir         {
187*cdf0e10cSrcweir             long nMinimalDistance = ::std::numeric_limits< long >::max();
188*cdf0e10cSrcweir             for (   ::std::vector< ::std::vector< Image > >::const_iterator check = aImageSets.begin();
189*cdf0e10cSrcweir                     check != aImageSets.end();
190*cdf0e10cSrcweir                     ++check
191*cdf0e10cSrcweir                 )
192*cdf0e10cSrcweir             {
193*cdf0e10cSrcweir                 ENSURE_OR_CONTINUE( !check->empty(), "Throbber::initImages: illegal image!" );
194*cdf0e10cSrcweir                 const Size aImageSize = (*check)[0].GetSizePixel();
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir                 if  (   ( aImageSize.Width() > aWindowSizePixel.Width() )
197*cdf0e10cSrcweir                     ||  ( aImageSize.Height() > aWindowSizePixel.Height() )
198*cdf0e10cSrcweir                     )
199*cdf0e10cSrcweir                     // do not use an image set which doesn't fit into the window
200*cdf0e10cSrcweir                     continue;
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir                 const sal_Int64 distance =
203*cdf0e10cSrcweir                         ( aWindowSizePixel.Width() - aImageSize.Width() ) * ( aWindowSizePixel.Width() - aImageSize.Width() )
204*cdf0e10cSrcweir                     +   ( aWindowSizePixel.Height() - aImageSize.Height() ) * ( aWindowSizePixel.Height() - aImageSize.Height() );
205*cdf0e10cSrcweir                 if ( distance < nMinimalDistance )
206*cdf0e10cSrcweir                 {
207*cdf0e10cSrcweir                     nMinimalDistance = distance;
208*cdf0e10cSrcweir                     nPreferredSet = check - aImageSets.begin();
209*cdf0e10cSrcweir                 }
210*cdf0e10cSrcweir             }
211*cdf0e10cSrcweir         }
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir         if ( nPreferredSet < aImageSets.size() )
214*cdf0e10cSrcweir             setImageList( aImageSets[nPreferredSet] );
215*cdf0e10cSrcweir     }
216*cdf0e10cSrcweir     catch( const Exception& )
217*cdf0e10cSrcweir     {
218*cdf0e10cSrcweir     	DBG_UNHANDLED_EXCEPTION();
219*cdf0e10cSrcweir     }
220*cdf0e10cSrcweir }
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
223*cdf0e10cSrcweir void Throbber::start()
224*cdf0e10cSrcweir {
225*cdf0e10cSrcweir     maWaitTimer.Start();
226*cdf0e10cSrcweir }
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
229*cdf0e10cSrcweir void Throbber::stop()
230*cdf0e10cSrcweir {
231*cdf0e10cSrcweir     maWaitTimer.Stop();
232*cdf0e10cSrcweir }
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
235*cdf0e10cSrcweir bool Throbber::isRunning() const
236*cdf0e10cSrcweir {
237*cdf0e10cSrcweir     return maWaitTimer.IsActive();
238*cdf0e10cSrcweir }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
241*cdf0e10cSrcweir void Throbber::setImageList( ::std::vector< Image > const& i_images )
242*cdf0e10cSrcweir {
243*cdf0e10cSrcweir     maImageList = i_images;
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir     mnStepCount = maImageList.size();
246*cdf0e10cSrcweir     const Image aInitialImage( mnStepCount ? maImageList[ 0 ] : Image() );
247*cdf0e10cSrcweir     SetImage( aInitialImage );
248*cdf0e10cSrcweir }
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
251*cdf0e10cSrcweir void Throbber::setImageList( const Sequence< Reference< XGraphic > >& rImageList )
252*cdf0e10cSrcweir {
253*cdf0e10cSrcweir     ::std::vector< Image > aImages( rImageList.getLength() );
254*cdf0e10cSrcweir     ::std::copy(
255*cdf0e10cSrcweir         rImageList.getConstArray(),
256*cdf0e10cSrcweir         rImageList.getConstArray() + rImageList.getLength(),
257*cdf0e10cSrcweir         aImages.begin()
258*cdf0e10cSrcweir     );
259*cdf0e10cSrcweir     setImageList( aImages );
260*cdf0e10cSrcweir }
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
263*cdf0e10cSrcweir ::std::vector< ::rtl::OUString > Throbber::getDefaultImageURLs( const ImageSet i_imageSet )
264*cdf0e10cSrcweir {
265*cdf0e10cSrcweir     ::std::vector< ::rtl::OUString > aImageURLs;
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir     sal_Char const* const pResolutions[] = { "16", "32", "64" };
268*cdf0e10cSrcweir     size_t const nImageCounts[] = { 6, 12, 12 };
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir     size_t index = 0;
271*cdf0e10cSrcweir     switch ( i_imageSet )
272*cdf0e10cSrcweir     {
273*cdf0e10cSrcweir     case IMAGES_16_PX:  index = 0;  break;
274*cdf0e10cSrcweir     case IMAGES_32_PX:  index = 1;  break;
275*cdf0e10cSrcweir     case IMAGES_64_PX:  index = 2;  break;
276*cdf0e10cSrcweir     case IMAGES_NONE:
277*cdf0e10cSrcweir     case IMAGES_AUTO:
278*cdf0e10cSrcweir         OSL_ENSURE( false, "Throbber::getDefaultImageURLs: illegal image set!" );
279*cdf0e10cSrcweir         return aImageURLs;
280*cdf0e10cSrcweir     }
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir     aImageURLs.reserve( nImageCounts[index] );
283*cdf0e10cSrcweir     for ( size_t i=0; i<nImageCounts[index]; ++i )
284*cdf0e10cSrcweir     {
285*cdf0e10cSrcweir         ::rtl::OUStringBuffer aURL;
286*cdf0e10cSrcweir         aURL.appendAscii( "private:graphicrepository/shared/spinner-" );
287*cdf0e10cSrcweir         aURL.appendAscii( pResolutions[index] );
288*cdf0e10cSrcweir         aURL.appendAscii( "-" );
289*cdf0e10cSrcweir         if ( i < 9 )
290*cdf0e10cSrcweir             aURL.appendAscii( "0" );
291*cdf0e10cSrcweir         aURL.append     ( sal_Int32( i + 1 ) );
292*cdf0e10cSrcweir         aURL.appendAscii( ".png" );
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir         aImageURLs.push_back( aURL.makeStringAndClear() );
295*cdf0e10cSrcweir     }
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir     return aImageURLs;
298*cdf0e10cSrcweir }
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
301*cdf0e10cSrcweir IMPL_LINK( Throbber, TimeOutHdl, void*, EMPTYARG )
302*cdf0e10cSrcweir {
303*cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
304*cdf0e10cSrcweir     if ( maImageList.empty() )
305*cdf0e10cSrcweir         return 0;
306*cdf0e10cSrcweir 
307*cdf0e10cSrcweir     if ( mnCurStep < mnStepCount - 1 )
308*cdf0e10cSrcweir         mnCurStep += 1;
309*cdf0e10cSrcweir     else
310*cdf0e10cSrcweir     {
311*cdf0e10cSrcweir         if ( mbRepeat )
312*cdf0e10cSrcweir         {
313*cdf0e10cSrcweir             // start over
314*cdf0e10cSrcweir             mnCurStep = 0;
315*cdf0e10cSrcweir         }
316*cdf0e10cSrcweir         else
317*cdf0e10cSrcweir         {
318*cdf0e10cSrcweir             stop();
319*cdf0e10cSrcweir         }
320*cdf0e10cSrcweir     }
321*cdf0e10cSrcweir 
322*cdf0e10cSrcweir     SetImage( maImageList[ mnCurStep ] );
323*cdf0e10cSrcweir 
324*cdf0e10cSrcweir     return 0;
325*cdf0e10cSrcweir }
326