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