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