Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "toolkit/awt/animatedimagespeer.hxx"
22 : #include "toolkit/helper/property.hxx"
23 :
24 : #include <com/sun/star/awt/XAnimatedImages.hpp>
25 : #include <com/sun/star/awt/Size.hpp>
26 : #include <com/sun/star/graphic/GraphicProvider.hpp>
27 : #include <com/sun/star/graphic/XGraphicProvider.hpp>
28 : #include <com/sun/star/beans/XPropertySet.hpp>
29 : #include <com/sun/star/graphic/XGraphic.hpp>
30 : #include <com/sun/star/awt/ImageScaleMode.hpp>
31 :
32 : #include <comphelper/namedvaluecollection.hxx>
33 : #include <comphelper/processfactory.hxx>
34 : #include <rtl/ustrbuf.hxx>
35 : #include <tools/diagnose_ex.h>
36 : #include <tools/urlobj.hxx>
37 : #include <vcl/throbber.hxx>
38 : #include <vcl/svapp.hxx>
39 :
40 : #include <limits>
41 :
42 : //......................................................................................................................
43 : namespace toolkit
44 : {
45 : //......................................................................................................................
46 :
47 : using ::com::sun::star::uno::XComponentContext;
48 : using ::com::sun::star::uno::Reference;
49 : using ::com::sun::star::uno::XInterface;
50 : using ::com::sun::star::uno::UNO_QUERY;
51 : using ::com::sun::star::uno::UNO_QUERY_THROW;
52 : using ::com::sun::star::uno::UNO_SET_THROW;
53 : using ::com::sun::star::uno::Exception;
54 : using ::com::sun::star::uno::RuntimeException;
55 : using ::com::sun::star::uno::Any;
56 : using ::com::sun::star::uno::makeAny;
57 : using ::com::sun::star::uno::Sequence;
58 : using ::com::sun::star::uno::Type;
59 : using ::com::sun::star::lang::EventObject;
60 : using ::com::sun::star::container::ContainerEvent;
61 : using ::com::sun::star::awt::XAnimatedImages;
62 : using ::com::sun::star::awt::Size;
63 : using ::com::sun::star::lang::XMultiServiceFactory;
64 : using ::com::sun::star::graphic::XGraphicProvider;
65 : using ::com::sun::star::beans::XPropertySet;
66 : using ::com::sun::star::graphic::XGraphic;
67 :
68 : namespace ImageScaleMode = ::com::sun::star::awt::ImageScaleMode;
69 :
70 : //==================================================================================================================
71 : //= AnimatedImagesPeer_Data
72 : //==================================================================================================================
73 0 : struct CachedImage
74 : {
75 : OUString sImageURL;
76 : mutable Reference< XGraphic > xGraphic;
77 :
78 0 : CachedImage()
79 : :sImageURL()
80 0 : ,xGraphic()
81 : {
82 0 : }
83 :
84 0 : CachedImage( OUString const& i_imageURL )
85 : :sImageURL( i_imageURL )
86 0 : ,xGraphic()
87 : {
88 0 : }
89 : };
90 :
91 0 : struct AnimatedImagesPeer_Data
92 : {
93 : AnimatedImagesPeer& rAntiImpl;
94 : ::std::vector< ::std::vector< CachedImage > > aCachedImageSets;
95 :
96 0 : AnimatedImagesPeer_Data( AnimatedImagesPeer& i_antiImpl )
97 : :rAntiImpl( i_antiImpl )
98 0 : ,aCachedImageSets()
99 : {
100 0 : }
101 : };
102 :
103 : //==================================================================================================================
104 : //= helper
105 : //==================================================================================================================
106 : namespace
107 : {
108 : //--------------------------------------------------------------------------------------------------------------
109 0 : OUString lcl_getHighContrastURL( OUString const& i_imageURL )
110 : {
111 0 : INetURLObject aURL( i_imageURL );
112 0 : if ( aURL.GetProtocol() != INET_PROT_PRIV_SOFFICE )
113 : {
114 0 : OSL_VERIFY( aURL.insertName( OUString( "hicontrast" ), false, 0 ) );
115 0 : return aURL.GetMainURL( INetURLObject::NO_DECODE );
116 : }
117 : // the private: scheme is not considered to be hierarchical by INetURLObject, so manually insert the
118 : // segment
119 0 : const sal_Int32 separatorPos = i_imageURL.indexOf( '/' );
120 0 : ENSURE_OR_RETURN( separatorPos != -1, "lcl_getHighContrastURL: unsipported URL scheme - cannot automatically determine HC version!", i_imageURL );
121 :
122 0 : OUStringBuffer composer;
123 0 : composer.append( i_imageURL.copy( 0, separatorPos ) );
124 0 : composer.appendAscii( "/hicontrast" );
125 0 : composer.append( i_imageURL.copy( separatorPos ) );
126 0 : return composer.makeStringAndClear();
127 : }
128 :
129 : //--------------------------------------------------------------------------------------------------------------
130 0 : bool lcl_ensureImage_throw( Reference< XGraphicProvider > const& i_graphicProvider, const bool i_isHighContrast, const CachedImage& i_cachedImage )
131 : {
132 0 : if ( !i_cachedImage.xGraphic.is() )
133 : {
134 0 : ::comphelper::NamedValueCollection aMediaProperties;
135 0 : if ( i_isHighContrast )
136 : {
137 : // try (to find) the high-contrast version of the graphic first
138 0 : aMediaProperties.put( "URL", lcl_getHighContrastURL( i_cachedImage.sImageURL ) );
139 0 : i_cachedImage.xGraphic.set( i_graphicProvider->queryGraphic( aMediaProperties.getPropertyValues() ), UNO_QUERY );
140 : }
141 0 : if ( !i_cachedImage.xGraphic.is() )
142 : {
143 0 : aMediaProperties.put( "URL", i_cachedImage.sImageURL );
144 0 : i_cachedImage.xGraphic.set( i_graphicProvider->queryGraphic( aMediaProperties.getPropertyValues() ), UNO_QUERY );
145 0 : }
146 : }
147 0 : return i_cachedImage.xGraphic.is();
148 : }
149 :
150 : //--------------------------------------------------------------------------------------------------------------
151 0 : Size lcl_getGraphicSizePixel( Reference< XGraphic > const& i_graphic )
152 : {
153 0 : Size aSizePixel;
154 : try
155 : {
156 0 : if ( i_graphic.is() )
157 : {
158 0 : const Reference< XPropertySet > xGraphicProps( i_graphic, UNO_QUERY_THROW );
159 0 : OSL_VERIFY( xGraphicProps->getPropertyValue("SizePixel") >>= aSizePixel );
160 : }
161 : }
162 0 : catch( const Exception& )
163 : {
164 : DBG_UNHANDLED_EXCEPTION();
165 : }
166 0 : return aSizePixel;
167 : }
168 :
169 : //--------------------------------------------------------------------------------------------------------------
170 0 : void lcl_init( Sequence< OUString > const& i_imageURLs, ::std::vector< CachedImage >& o_images )
171 : {
172 0 : o_images.resize(0);
173 0 : size_t count = size_t( i_imageURLs.getLength() );
174 0 : o_images.reserve( count );
175 0 : for ( size_t i = 0; i < count; ++i )
176 : {
177 0 : o_images.push_back( CachedImage( i_imageURLs[i] ) );
178 : }
179 0 : }
180 :
181 : //--------------------------------------------------------------------------------------------------------------
182 0 : void lcl_updateImageList_nothrow( AnimatedImagesPeer_Data& i_data )
183 : {
184 0 : Throbber* pThrobber = dynamic_cast< Throbber* >( i_data.rAntiImpl.GetWindow() );
185 0 : if ( pThrobber == NULL )
186 0 : return;
187 :
188 : try
189 : {
190 : // collect the image sizes of the different image sets
191 0 : const Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
192 0 : const Reference< XGraphicProvider > xGraphicProvider( com::sun::star::graphic::GraphicProvider::create(xContext) );
193 :
194 0 : const bool isHighContrast = pThrobber->GetSettings().GetStyleSettings().GetHighContrastMode();
195 :
196 0 : sal_Int32 nPreferredSet = -1;
197 0 : const size_t nImageSetCount = i_data.aCachedImageSets.size();
198 0 : if ( nImageSetCount < 2 )
199 : {
200 0 : nPreferredSet = sal_Int32( nImageSetCount ) - 1;
201 : }
202 : else
203 : {
204 0 : ::std::vector< Size > aImageSizes( nImageSetCount );
205 0 : for ( sal_Int32 nImageSet = 0; size_t( nImageSet ) < nImageSetCount; ++nImageSet )
206 : {
207 0 : ::std::vector< CachedImage > const& rImageSet( i_data.aCachedImageSets[ nImageSet ] );
208 0 : if ( ( rImageSet.empty() )
209 0 : || ( !lcl_ensureImage_throw( xGraphicProvider, isHighContrast, rImageSet[0] ) )
210 : )
211 : {
212 0 : aImageSizes[ nImageSet ] = Size( SAL_MAX_INT32, SAL_MAX_INT32 );
213 : }
214 : else
215 : {
216 0 : aImageSizes[ nImageSet ] = lcl_getGraphicSizePixel( rImageSet[0].xGraphic );
217 : }
218 : }
219 :
220 : // find the set with the smallest difference between window size and image size
221 0 : const ::Size aWindowSizePixel = pThrobber->GetSizePixel();
222 0 : long nMinimalDistance = ::std::numeric_limits< long >::max();
223 0 : for ( ::std::vector< Size >::const_iterator check = aImageSizes.begin();
224 0 : check != aImageSizes.end();
225 : ++check
226 : )
227 : {
228 0 : if ( ( check->Width > aWindowSizePixel.Width() )
229 0 : || ( check->Height > aWindowSizePixel.Height() )
230 : )
231 : // do not use an image set which doesn't fit into the window
232 0 : continue;
233 :
234 : const sal_Int64 distance =
235 0 : ( aWindowSizePixel.Width() - check->Width ) * ( aWindowSizePixel.Width() - check->Width )
236 0 : + ( aWindowSizePixel.Height() - check->Height ) * ( aWindowSizePixel.Height() - check->Height );
237 0 : if ( distance < nMinimalDistance )
238 : {
239 0 : nMinimalDistance = distance;
240 0 : nPreferredSet = check - aImageSizes.begin();
241 : }
242 0 : }
243 : }
244 :
245 : // found a set?
246 0 : Sequence< Reference< XGraphic > > aImages;
247 0 : if ( ( nPreferredSet >= 0 ) && ( size_t( nPreferredSet ) < nImageSetCount ) )
248 : {
249 : // => set the images
250 0 : ::std::vector< CachedImage > const& rImageSet( i_data.aCachedImageSets[ nPreferredSet ] );
251 0 : aImages.realloc( rImageSet.size() );
252 0 : sal_Int32 imageIndex = 0;
253 0 : for ( ::std::vector< CachedImage >::const_iterator cachedImage = rImageSet.begin();
254 0 : cachedImage != rImageSet.end();
255 : ++cachedImage, ++imageIndex
256 : )
257 : {
258 0 : lcl_ensureImage_throw( xGraphicProvider, isHighContrast, *cachedImage );
259 0 : aImages[ imageIndex ] = cachedImage->xGraphic;
260 : }
261 : }
262 0 : pThrobber->setImageList( aImages );
263 : }
264 0 : catch( const Exception& )
265 : {
266 : DBG_UNHANDLED_EXCEPTION();
267 : }
268 : }
269 :
270 : //--------------------------------------------------------------------------------------------------------------
271 0 : void lcl_updateImageList_nothrow( AnimatedImagesPeer_Data& i_data, const Reference< XAnimatedImages >& i_images )
272 : {
273 : try
274 : {
275 0 : const sal_Int32 nImageSetCount = i_images->getImageSetCount();
276 0 : i_data.aCachedImageSets.resize(0);
277 0 : for ( sal_Int32 set = 0; set < nImageSetCount; ++set )
278 : {
279 0 : const Sequence< OUString > aImageURLs( i_images->getImageSet( set ) );
280 0 : ::std::vector< CachedImage > aImages;
281 0 : lcl_init( aImageURLs, aImages );
282 0 : i_data.aCachedImageSets.push_back( aImages );
283 0 : }
284 :
285 0 : lcl_updateImageList_nothrow( i_data );
286 : }
287 0 : catch( const Exception& )
288 : {
289 : DBG_UNHANDLED_EXCEPTION();
290 : }
291 0 : }
292 : }
293 :
294 : //==================================================================================================================
295 : //= AnimatedImagesPeer
296 : //==================================================================================================================
297 : //------------------------------------------------------------------------------------------------------------------
298 0 : AnimatedImagesPeer::AnimatedImagesPeer()
299 : :AnimatedImagesPeer_Base()
300 0 : ,m_pData( new AnimatedImagesPeer_Data( *this ) )
301 : {
302 0 : }
303 :
304 : //------------------------------------------------------------------------------------------------------------------
305 0 : AnimatedImagesPeer::~AnimatedImagesPeer()
306 : {
307 0 : }
308 :
309 : //------------------------------------------------------------------------------------------------------------------
310 0 : void SAL_CALL AnimatedImagesPeer::startAnimation( ) throw (RuntimeException)
311 : {
312 0 : SolarMutexGuard aGuard;
313 0 : Throbber* pThrobber( dynamic_cast< Throbber* >( GetWindow() ) );
314 0 : if ( pThrobber != NULL)
315 0 : pThrobber->start();
316 0 : }
317 :
318 : //------------------------------------------------------------------------------------------------------------------
319 0 : void SAL_CALL AnimatedImagesPeer::stopAnimation( ) throw (RuntimeException)
320 : {
321 0 : SolarMutexGuard aGuard;
322 0 : Throbber* pThrobber( dynamic_cast< Throbber* >( GetWindow() ) );
323 0 : if ( pThrobber != NULL)
324 0 : pThrobber->stop();
325 0 : }
326 :
327 : //------------------------------------------------------------------------------------------------------------------
328 0 : ::sal_Bool SAL_CALL AnimatedImagesPeer::isAnimationRunning( ) throw (RuntimeException)
329 : {
330 0 : SolarMutexGuard aGuard;
331 0 : Throbber* pThrobber( dynamic_cast< Throbber* >( GetWindow() ) );
332 0 : if ( pThrobber != NULL)
333 0 : return pThrobber->isRunning();
334 0 : return sal_False;
335 : }
336 :
337 : //------------------------------------------------------------------------------------------------------------------
338 0 : void SAL_CALL AnimatedImagesPeer::setProperty( const OUString& i_propertyName, const Any& i_value ) throw(RuntimeException)
339 : {
340 0 : SolarMutexGuard aGuard;
341 :
342 0 : Throbber* pThrobber( dynamic_cast< Throbber* >( GetWindow() ) );
343 0 : if ( pThrobber == NULL )
344 : {
345 0 : VCLXWindow::setProperty( i_propertyName, i_value );
346 0 : return;
347 : }
348 :
349 0 : const sal_uInt16 nPropertyId = GetPropertyId( i_propertyName );
350 0 : switch ( nPropertyId )
351 : {
352 : case BASEPROPERTY_STEP_TIME:
353 : {
354 0 : sal_Int32 nStepTime( 0 );
355 0 : if ( i_value >>= nStepTime )
356 0 : pThrobber->setStepTime( nStepTime );
357 0 : break;
358 : }
359 : case BASEPROPERTY_AUTO_REPEAT:
360 : {
361 0 : sal_Bool bRepeat( sal_True );
362 0 : if ( i_value >>= bRepeat )
363 0 : pThrobber->setRepeat( bRepeat );
364 0 : break;
365 : }
366 :
367 : case BASEPROPERTY_IMAGE_SCALE_MODE:
368 : {
369 0 : sal_Int16 nScaleMode( ImageScaleMode::ANISOTROPIC );
370 0 : ImageControl* pImageControl = dynamic_cast< ImageControl* >( GetWindow() );
371 0 : if ( pImageControl && ( i_value >>= nScaleMode ) )
372 : {
373 0 : pImageControl->SetScaleMode( nScaleMode );
374 : }
375 : }
376 0 : break;
377 :
378 : default:
379 0 : AnimatedImagesPeer_Base::setProperty( i_propertyName, i_value );
380 0 : break;
381 0 : }
382 : }
383 :
384 : //------------------------------------------------------------------------------------------------------------------
385 0 : Any SAL_CALL AnimatedImagesPeer::getProperty( const OUString& i_propertyName ) throw(RuntimeException)
386 : {
387 0 : SolarMutexGuard aGuard;
388 :
389 0 : Any aReturn;
390 :
391 0 : Throbber* pThrobber( dynamic_cast< Throbber* >( GetWindow() ) );
392 0 : if ( pThrobber == NULL )
393 0 : return VCLXWindow::getProperty( i_propertyName );
394 :
395 0 : const sal_uInt16 nPropertyId = GetPropertyId( i_propertyName );
396 0 : switch ( nPropertyId )
397 : {
398 : case BASEPROPERTY_STEP_TIME:
399 0 : aReturn <<= pThrobber->getStepTime();
400 0 : break;
401 :
402 : case BASEPROPERTY_AUTO_REPEAT:
403 0 : aReturn <<= pThrobber->getRepeat();
404 0 : break;
405 :
406 : case BASEPROPERTY_IMAGE_SCALE_MODE:
407 : {
408 0 : ImageControl const* pImageControl = dynamic_cast< ImageControl* >( GetWindow() );
409 0 : aReturn <<= ( pImageControl ? pImageControl->GetScaleMode() : ImageScaleMode::ANISOTROPIC );
410 : }
411 0 : break;
412 :
413 : default:
414 0 : aReturn = AnimatedImagesPeer_Base::getProperty( i_propertyName );
415 0 : break;
416 : }
417 :
418 0 : return aReturn;
419 : }
420 :
421 : //------------------------------------------------------------------------------------------------------------------
422 0 : void AnimatedImagesPeer::ProcessWindowEvent( const VclWindowEvent& i_windowEvent )
423 : {
424 0 : switch ( i_windowEvent.GetId() )
425 : {
426 : case VCLEVENT_WINDOW_RESIZE:
427 0 : lcl_updateImageList_nothrow( *m_pData );
428 0 : break;
429 : }
430 :
431 0 : AnimatedImagesPeer_Base::ProcessWindowEvent( i_windowEvent );
432 0 : }
433 :
434 : //------------------------------------------------------------------------------------------------------------------
435 0 : void AnimatedImagesPeer::impl_updateImages_nolck( const Reference< XInterface >& i_animatedImages )
436 : {
437 0 : SolarMutexGuard aGuard;
438 :
439 0 : lcl_updateImageList_nothrow( *m_pData, Reference< XAnimatedImages >( i_animatedImages, UNO_QUERY_THROW ) );
440 0 : }
441 :
442 : //------------------------------------------------------------------------------------------------------------------
443 0 : void SAL_CALL AnimatedImagesPeer::elementInserted( const ContainerEvent& i_event ) throw (RuntimeException)
444 : {
445 0 : SolarMutexGuard aGuard;
446 0 : Reference< XAnimatedImages > xAnimatedImages( i_event.Source, UNO_QUERY_THROW );
447 :
448 0 : sal_Int32 nPosition(0);
449 0 : OSL_VERIFY( i_event.Accessor >>= nPosition );
450 0 : size_t position = size_t( nPosition );
451 0 : if ( position > m_pData->aCachedImageSets.size() )
452 : {
453 : OSL_ENSURE( false, "AnimatedImagesPeer::elementInserted: illegal accessor/index!" );
454 0 : lcl_updateImageList_nothrow( *m_pData, xAnimatedImages );
455 : }
456 :
457 0 : Sequence< OUString > aImageURLs;
458 0 : OSL_VERIFY( i_event.Element >>= aImageURLs );
459 0 : ::std::vector< CachedImage > aImages;
460 0 : lcl_init( aImageURLs, aImages );
461 0 : m_pData->aCachedImageSets.insert( m_pData->aCachedImageSets.begin() + position, aImages );
462 0 : lcl_updateImageList_nothrow( *m_pData );
463 0 : }
464 :
465 : //------------------------------------------------------------------------------------------------------------------
466 0 : void SAL_CALL AnimatedImagesPeer::elementRemoved( const ContainerEvent& i_event ) throw (RuntimeException)
467 : {
468 0 : SolarMutexGuard aGuard;
469 0 : Reference< XAnimatedImages > xAnimatedImages( i_event.Source, UNO_QUERY_THROW );
470 :
471 0 : sal_Int32 nPosition(0);
472 0 : OSL_VERIFY( i_event.Accessor >>= nPosition );
473 0 : size_t position = size_t( nPosition );
474 0 : if ( position >= m_pData->aCachedImageSets.size() )
475 : {
476 : OSL_ENSURE( false, "AnimatedImagesPeer::elementRemoved: illegal accessor/index!" );
477 0 : lcl_updateImageList_nothrow( *m_pData, xAnimatedImages );
478 : }
479 :
480 0 : m_pData->aCachedImageSets.erase( m_pData->aCachedImageSets.begin() + position );
481 0 : lcl_updateImageList_nothrow( *m_pData );
482 0 : }
483 :
484 : //------------------------------------------------------------------------------------------------------------------
485 0 : void SAL_CALL AnimatedImagesPeer::elementReplaced( const ContainerEvent& i_event ) throw (RuntimeException)
486 : {
487 0 : SolarMutexGuard aGuard;
488 0 : Reference< XAnimatedImages > xAnimatedImages( i_event.Source, UNO_QUERY_THROW );
489 :
490 0 : sal_Int32 nPosition(0);
491 0 : OSL_VERIFY( i_event.Accessor >>= nPosition );
492 0 : size_t position = size_t( nPosition );
493 0 : if ( position >= m_pData->aCachedImageSets.size() )
494 : {
495 : OSL_ENSURE( false, "AnimatedImagesPeer::elementReplaced: illegal accessor/index!" );
496 0 : lcl_updateImageList_nothrow( *m_pData, xAnimatedImages );
497 : }
498 :
499 0 : Sequence< OUString > aImageURLs;
500 0 : OSL_VERIFY( i_event.Element >>= aImageURLs );
501 0 : ::std::vector< CachedImage > aImages;
502 0 : lcl_init( aImageURLs, aImages );
503 0 : m_pData->aCachedImageSets[ position ] = aImages;
504 0 : lcl_updateImageList_nothrow( *m_pData );
505 0 : }
506 :
507 : //------------------------------------------------------------------------------------------------------------------
508 0 : void SAL_CALL AnimatedImagesPeer::disposing( const EventObject& i_event ) throw (RuntimeException)
509 : {
510 0 : VCLXWindow::disposing( i_event );
511 0 : }
512 :
513 : //------------------------------------------------------------------------------------------------------------------
514 0 : void SAL_CALL AnimatedImagesPeer::modified( const EventObject& i_event ) throw (RuntimeException)
515 : {
516 0 : impl_updateImages_nolck( i_event.Source );
517 0 : }
518 :
519 : //------------------------------------------------------------------------------------------------------------------
520 0 : void SAL_CALL AnimatedImagesPeer::dispose( ) throw(RuntimeException)
521 : {
522 0 : AnimatedImagesPeer_Base::dispose();
523 0 : SolarMutexGuard aGuard;
524 0 : m_pData->aCachedImageSets.resize(0);
525 0 : }
526 :
527 : //......................................................................................................................
528 465 : } // namespace toolkit
529 : //......................................................................................................................
530 :
531 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|