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