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 : explicit 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 : explicit 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() != INetProtocol::PrivSoffice )
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 : VclPtr<Throbber> pThrobber = i_data.rAntiImpl.GetAsDynamic<Throbber>();
186 0 : if ( !pThrobber )
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 ( size_t nImageSet = 0; 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 0 : }
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_xData( 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 : VclPtr<Throbber> pThrobber = GetAsDynamic<Throbber>();
315 0 : if (pThrobber)
316 0 : pThrobber->start();
317 0 : }
318 :
319 0 : void SAL_CALL AnimatedImagesPeer::stopAnimation() throw (RuntimeException, std::exception)
320 : {
321 0 : SolarMutexGuard aGuard;
322 0 : VclPtr<Throbber> pThrobber = GetAsDynamic<Throbber>();
323 0 : if (pThrobber)
324 0 : pThrobber->stop();
325 0 : }
326 :
327 0 : sal_Bool SAL_CALL AnimatedImagesPeer::isAnimationRunning() throw (RuntimeException, std::exception)
328 : {
329 0 : SolarMutexGuard aGuard;
330 0 : VclPtr<Throbber> pThrobber = GetAsDynamic<Throbber>();
331 0 : if (pThrobber)
332 0 : return pThrobber->isRunning();
333 0 : return sal_False;
334 : }
335 :
336 0 : void SAL_CALL AnimatedImagesPeer::setProperty( const OUString& i_propertyName, const Any& i_value ) throw(RuntimeException, std::exception)
337 : {
338 0 : SolarMutexGuard aGuard;
339 :
340 0 : VclPtr<Throbber> pThrobber = GetAsDynamic<Throbber>();
341 0 : if ( pThrobber )
342 : {
343 0 : VCLXWindow::setProperty( i_propertyName, i_value );
344 0 : return;
345 : }
346 :
347 0 : const sal_uInt16 nPropertyId = GetPropertyId( i_propertyName );
348 0 : switch ( nPropertyId )
349 : {
350 : case BASEPROPERTY_STEP_TIME:
351 : {
352 0 : sal_Int32 nStepTime( 0 );
353 0 : if ( i_value >>= nStepTime )
354 0 : pThrobber->setStepTime( nStepTime );
355 0 : break;
356 : }
357 : case BASEPROPERTY_AUTO_REPEAT:
358 : {
359 0 : bool bRepeat( true );
360 0 : if ( i_value >>= bRepeat )
361 0 : pThrobber->setRepeat( bRepeat );
362 0 : break;
363 : }
364 :
365 : case BASEPROPERTY_IMAGE_SCALE_MODE:
366 : {
367 0 : sal_Int16 nScaleMode( ImageScaleMode::ANISOTROPIC );
368 0 : VclPtr<ImageControl> pImageControl = GetAsDynamic< ImageControl >();
369 0 : if ( pImageControl && ( i_value >>= nScaleMode ) )
370 0 : pImageControl->SetScaleMode( nScaleMode );
371 : }
372 0 : break;
373 :
374 : default:
375 0 : AnimatedImagesPeer_Base::setProperty( i_propertyName, i_value );
376 0 : break;
377 0 : }
378 : }
379 :
380 :
381 0 : Any SAL_CALL AnimatedImagesPeer::getProperty( const OUString& i_propertyName ) throw(RuntimeException, std::exception)
382 : {
383 0 : SolarMutexGuard aGuard;
384 :
385 0 : Any aReturn;
386 :
387 0 : VclPtr<Throbber> pThrobber = GetAsDynamic<Throbber>();
388 0 : if ( !pThrobber )
389 0 : return VCLXWindow::getProperty( i_propertyName );
390 :
391 0 : const sal_uInt16 nPropertyId = GetPropertyId( i_propertyName );
392 0 : switch ( nPropertyId )
393 : {
394 : case BASEPROPERTY_STEP_TIME:
395 0 : aReturn <<= pThrobber->getStepTime();
396 0 : break;
397 :
398 : case BASEPROPERTY_AUTO_REPEAT:
399 0 : aReturn <<= pThrobber->getRepeat();
400 0 : break;
401 :
402 : case BASEPROPERTY_IMAGE_SCALE_MODE:
403 : {
404 0 : VclPtr<ImageControl> pImageControl = GetAsDynamic<ImageControl>();
405 0 : aReturn <<= ( pImageControl ? pImageControl->GetScaleMode() : ImageScaleMode::ANISOTROPIC );
406 : }
407 0 : break;
408 :
409 : default:
410 0 : aReturn = AnimatedImagesPeer_Base::getProperty( i_propertyName );
411 0 : break;
412 : }
413 :
414 0 : return aReturn;
415 : }
416 :
417 :
418 0 : void AnimatedImagesPeer::ProcessWindowEvent( const VclWindowEvent& i_windowEvent )
419 : {
420 0 : switch ( i_windowEvent.GetId() )
421 : {
422 : case VCLEVENT_WINDOW_RESIZE:
423 0 : lcl_updateImageList_nothrow( *m_xData );
424 0 : break;
425 : }
426 :
427 0 : AnimatedImagesPeer_Base::ProcessWindowEvent( i_windowEvent );
428 0 : }
429 :
430 :
431 0 : void AnimatedImagesPeer::impl_updateImages_nolck( const Reference< XInterface >& i_animatedImages )
432 : {
433 0 : SolarMutexGuard aGuard;
434 :
435 0 : lcl_updateImageList_nothrow( *m_xData, Reference< XAnimatedImages >( i_animatedImages, UNO_QUERY_THROW ) );
436 0 : }
437 :
438 :
439 0 : void SAL_CALL AnimatedImagesPeer::elementInserted( const ContainerEvent& i_event ) throw (RuntimeException, std::exception)
440 : {
441 0 : SolarMutexGuard aGuard;
442 0 : Reference< XAnimatedImages > xAnimatedImages( i_event.Source, UNO_QUERY_THROW );
443 :
444 0 : sal_Int32 nPosition(0);
445 0 : OSL_VERIFY( i_event.Accessor >>= nPosition );
446 0 : size_t position = size_t( nPosition );
447 0 : if ( position > m_xData->aCachedImageSets.size() )
448 : {
449 : OSL_ENSURE( false, "AnimatedImagesPeer::elementInserted: illegal accessor/index!" );
450 0 : lcl_updateImageList_nothrow( *m_xData, xAnimatedImages );
451 : }
452 :
453 0 : Sequence< OUString > aImageURLs;
454 0 : OSL_VERIFY( i_event.Element >>= aImageURLs );
455 0 : ::std::vector< CachedImage > aImages;
456 0 : lcl_init( aImageURLs, aImages );
457 0 : m_xData->aCachedImageSets.insert( m_xData->aCachedImageSets.begin() + position, aImages );
458 0 : lcl_updateImageList_nothrow( *m_xData );
459 0 : }
460 :
461 :
462 0 : void SAL_CALL AnimatedImagesPeer::elementRemoved( const ContainerEvent& i_event ) throw (RuntimeException, std::exception)
463 : {
464 0 : SolarMutexGuard aGuard;
465 0 : Reference< XAnimatedImages > xAnimatedImages( i_event.Source, UNO_QUERY_THROW );
466 :
467 0 : sal_Int32 nPosition(0);
468 0 : OSL_VERIFY( i_event.Accessor >>= nPosition );
469 0 : size_t position = size_t( nPosition );
470 0 : if ( position >= m_xData->aCachedImageSets.size() )
471 : {
472 : OSL_ENSURE( false, "AnimatedImagesPeer::elementRemoved: illegal accessor/index!" );
473 0 : lcl_updateImageList_nothrow( *m_xData, xAnimatedImages );
474 : }
475 :
476 0 : m_xData->aCachedImageSets.erase( m_xData->aCachedImageSets.begin() + position );
477 0 : lcl_updateImageList_nothrow( *m_xData );
478 0 : }
479 :
480 :
481 0 : void SAL_CALL AnimatedImagesPeer::elementReplaced( const ContainerEvent& i_event ) throw (RuntimeException, std::exception)
482 : {
483 0 : SolarMutexGuard aGuard;
484 0 : Reference< XAnimatedImages > xAnimatedImages( i_event.Source, UNO_QUERY_THROW );
485 :
486 0 : sal_Int32 nPosition(0);
487 0 : OSL_VERIFY( i_event.Accessor >>= nPosition );
488 0 : size_t position = size_t( nPosition );
489 0 : if ( position >= m_xData->aCachedImageSets.size() )
490 : {
491 : OSL_ENSURE( false, "AnimatedImagesPeer::elementReplaced: illegal accessor/index!" );
492 0 : lcl_updateImageList_nothrow( *m_xData, xAnimatedImages );
493 : }
494 :
495 0 : Sequence< OUString > aImageURLs;
496 0 : OSL_VERIFY( i_event.Element >>= aImageURLs );
497 0 : ::std::vector< CachedImage > aImages;
498 0 : lcl_init( aImageURLs, aImages );
499 0 : m_xData->aCachedImageSets[ position ] = aImages;
500 0 : lcl_updateImageList_nothrow( *m_xData );
501 0 : }
502 :
503 :
504 0 : void SAL_CALL AnimatedImagesPeer::disposing( const EventObject& i_event ) throw (RuntimeException, std::exception)
505 : {
506 0 : VCLXWindow::disposing( i_event );
507 0 : }
508 :
509 :
510 0 : void SAL_CALL AnimatedImagesPeer::modified( const EventObject& i_event ) throw (RuntimeException, std::exception)
511 : {
512 0 : impl_updateImages_nolck( i_event.Source );
513 0 : }
514 :
515 :
516 0 : void SAL_CALL AnimatedImagesPeer::dispose( ) throw(RuntimeException, std::exception)
517 : {
518 0 : AnimatedImagesPeer_Base::dispose();
519 0 : SolarMutexGuard aGuard;
520 0 : m_xData->aCachedImageSets.resize(0);
521 0 : }
522 :
523 :
524 798 : } // namespace toolkit
525 :
526 :
527 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|