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