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