19f62ea84SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 39f62ea84SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 49f62ea84SAndrew Rist * or more contributor license agreements. See the NOTICE file 59f62ea84SAndrew Rist * distributed with this work for additional information 69f62ea84SAndrew Rist * regarding copyright ownership. The ASF licenses this file 79f62ea84SAndrew Rist * to you under the Apache License, Version 2.0 (the 89f62ea84SAndrew Rist * "License"); you may not use this file except in compliance 99f62ea84SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 119f62ea84SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 139f62ea84SAndrew Rist * Unless required by applicable law or agreed to in writing, 149f62ea84SAndrew Rist * software distributed under the License is distributed on an 159f62ea84SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 169f62ea84SAndrew Rist * KIND, either express or implied. See the License for the 179f62ea84SAndrew Rist * specific language governing permissions and limitations 189f62ea84SAndrew Rist * under the License. 19cdf0e10cSrcweir * 209f62ea84SAndrew Rist *************************************************************/ 219f62ea84SAndrew Rist 229f62ea84SAndrew 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 62*b6dc695eSAriel Constenla-Haile 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 78*b6dc695eSAriel Constenla-Haile SetScaleMode( ImageScaleMode::NONE ); 79cdf0e10cSrcweir initImages(); 80cdf0e10cSrcweir } 81cdf0e10cSrcweir 82cdf0e10cSrcweir //---------------------------------------------------------------------------------------------------------------------- 83cdf0e10cSrcweir Throbber::~Throbber() 84cdf0e10cSrcweir { 85cdf0e10cSrcweir maWaitTimer.Stop(); 86cdf0e10cSrcweir } 87cdf0e10cSrcweir 88cdf0e10cSrcweir //---------------------------------------------------------------------------------------------------------------------- 89cdf0e10cSrcweir namespace 90cdf0e10cSrcweir { 91cdf0e10cSrcweir ::std::vector< Image > lcl_loadImageSet( const Throbber::ImageSet i_imageSet, const bool i_isHiContrast ) 92cdf0e10cSrcweir { 93cdf0e10cSrcweir ::std::vector< Image > aImages; 94cdf0e10cSrcweir ENSURE_OR_RETURN( i_imageSet != Throbber::IMAGES_NONE, "lcl_loadImageSet: illegal image set", aImages ); 95cdf0e10cSrcweir 96cdf0e10cSrcweir const ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() ); 97cdf0e10cSrcweir const Reference< XGraphicProvider > xGraphicProvider( aContext.createComponent( "com.sun.star.graphic.GraphicProvider" ), UNO_QUERY_THROW ); 98cdf0e10cSrcweir 99cdf0e10cSrcweir ::std::vector< ::rtl::OUString > aImageURLs( Throbber::getDefaultImageURLs( i_imageSet ) ); 100cdf0e10cSrcweir aImages.reserve( aImageURLs.size() ); 101cdf0e10cSrcweir 102cdf0e10cSrcweir ::comphelper::NamedValueCollection aMediaProperties; 103cdf0e10cSrcweir for ( ::std::vector< ::rtl::OUString >::const_iterator imageURL = aImageURLs.begin(); 104cdf0e10cSrcweir imageURL != aImageURLs.end(); 105cdf0e10cSrcweir ++imageURL 106cdf0e10cSrcweir ) 107cdf0e10cSrcweir { 108cdf0e10cSrcweir Reference< XGraphic > xGraphic; 109cdf0e10cSrcweir if ( i_isHiContrast ) 110cdf0e10cSrcweir { 111aeee3b8fSAriel Constenla-Haile INetURLObject aURL( *imageURL ); 112aeee3b8fSAriel Constenla-Haile if ( aURL.GetProtocol() != INET_PROT_PRIV_SOFFICE ) 113aeee3b8fSAriel Constenla-Haile { 114aeee3b8fSAriel Constenla-Haile const sal_Int32 separatorPos = imageURL->lastIndexOf( '/' ); 115aeee3b8fSAriel Constenla-Haile if ( separatorPos != -1 ) 116aeee3b8fSAriel Constenla-Haile { 117aeee3b8fSAriel Constenla-Haile ::rtl::OUStringBuffer composer; 118aeee3b8fSAriel Constenla-Haile composer.append( imageURL->copy( 0, separatorPos ) ); 119aeee3b8fSAriel Constenla-Haile composer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "/hicontrast" ) ); 120aeee3b8fSAriel Constenla-Haile composer.append( imageURL->copy( separatorPos ) ); 121aeee3b8fSAriel Constenla-Haile 122aeee3b8fSAriel Constenla-Haile aMediaProperties.put( "URL", composer.makeStringAndClear() ); 123cdf0e10cSrcweir xGraphic.set( xGraphicProvider->queryGraphic( aMediaProperties.getPropertyValues() ), UNO_QUERY ); 124cdf0e10cSrcweir } 125aeee3b8fSAriel Constenla-Haile } 126aeee3b8fSAriel Constenla-Haile } 127cdf0e10cSrcweir if ( !xGraphic.is() ) 128cdf0e10cSrcweir { 129cdf0e10cSrcweir aMediaProperties.put( "URL", *imageURL ); 130cdf0e10cSrcweir xGraphic.set( xGraphicProvider->queryGraphic( aMediaProperties.getPropertyValues() ), UNO_QUERY ); 131cdf0e10cSrcweir } 132cdf0e10cSrcweir aImages.push_back( Image( xGraphic ) ); 133cdf0e10cSrcweir } 134cdf0e10cSrcweir 135cdf0e10cSrcweir return aImages; 136cdf0e10cSrcweir } 137cdf0e10cSrcweir } 138cdf0e10cSrcweir 139cdf0e10cSrcweir //---------------------------------------------------------------------------------------------------------------------- 140cdf0e10cSrcweir void Throbber::Resize() 141cdf0e10cSrcweir { 142cdf0e10cSrcweir ImageControl::Resize(); 143cdf0e10cSrcweir 144cdf0e10cSrcweir if ( meImageSet == IMAGES_AUTO ) 145cdf0e10cSrcweir initImages(); 146cdf0e10cSrcweir } 147cdf0e10cSrcweir 148cdf0e10cSrcweir //---------------------------------------------------------------------------------------------------------------------- 149cdf0e10cSrcweir void Throbber::initImages() 150cdf0e10cSrcweir { 151cdf0e10cSrcweir if ( meImageSet == IMAGES_NONE ) 152cdf0e10cSrcweir return; 153cdf0e10cSrcweir 154cdf0e10cSrcweir try 155cdf0e10cSrcweir { 156cdf0e10cSrcweir ::std::vector< ::std::vector< Image > > aImageSets; 157cdf0e10cSrcweir const bool isHiContrast = GetSettings().GetStyleSettings().GetHighContrastMode(); 158cdf0e10cSrcweir if ( meImageSet == IMAGES_AUTO ) 159cdf0e10cSrcweir { 160cdf0e10cSrcweir aImageSets.push_back( lcl_loadImageSet( IMAGES_16_PX, isHiContrast ) ); 161cdf0e10cSrcweir aImageSets.push_back( lcl_loadImageSet( IMAGES_32_PX, isHiContrast ) ); 162cdf0e10cSrcweir aImageSets.push_back( lcl_loadImageSet( IMAGES_64_PX, isHiContrast ) ); 163cdf0e10cSrcweir } 164cdf0e10cSrcweir else 165cdf0e10cSrcweir { 166cdf0e10cSrcweir aImageSets.push_back( lcl_loadImageSet( meImageSet, isHiContrast ) ); 167cdf0e10cSrcweir } 168cdf0e10cSrcweir 169cdf0e10cSrcweir // find the best matching image set (size-wise) 170cdf0e10cSrcweir const ::Size aWindowSizePixel = GetSizePixel(); 171cdf0e10cSrcweir size_t nPreferredSet = 0; 172cdf0e10cSrcweir if ( aImageSets.size() > 1 ) 173cdf0e10cSrcweir { 174cdf0e10cSrcweir long nMinimalDistance = ::std::numeric_limits< long >::max(); 175cdf0e10cSrcweir for ( ::std::vector< ::std::vector< Image > >::const_iterator check = aImageSets.begin(); 176cdf0e10cSrcweir check != aImageSets.end(); 177cdf0e10cSrcweir ++check 178cdf0e10cSrcweir ) 179cdf0e10cSrcweir { 180cdf0e10cSrcweir ENSURE_OR_CONTINUE( !check->empty(), "Throbber::initImages: illegal image!" ); 181cdf0e10cSrcweir const Size aImageSize = (*check)[0].GetSizePixel(); 182cdf0e10cSrcweir 183cdf0e10cSrcweir if ( ( aImageSize.Width() > aWindowSizePixel.Width() ) 184cdf0e10cSrcweir || ( aImageSize.Height() > aWindowSizePixel.Height() ) 185cdf0e10cSrcweir ) 186cdf0e10cSrcweir // do not use an image set which doesn't fit into the window 187cdf0e10cSrcweir continue; 188cdf0e10cSrcweir 189cdf0e10cSrcweir const sal_Int64 distance = 190cdf0e10cSrcweir ( aWindowSizePixel.Width() - aImageSize.Width() ) * ( aWindowSizePixel.Width() - aImageSize.Width() ) 191cdf0e10cSrcweir + ( aWindowSizePixel.Height() - aImageSize.Height() ) * ( aWindowSizePixel.Height() - aImageSize.Height() ); 192cdf0e10cSrcweir if ( distance < nMinimalDistance ) 193cdf0e10cSrcweir { 194cdf0e10cSrcweir nMinimalDistance = distance; 195cdf0e10cSrcweir nPreferredSet = check - aImageSets.begin(); 196cdf0e10cSrcweir } 197cdf0e10cSrcweir } 198cdf0e10cSrcweir } 199cdf0e10cSrcweir 200cdf0e10cSrcweir if ( nPreferredSet < aImageSets.size() ) 201cdf0e10cSrcweir setImageList( aImageSets[nPreferredSet] ); 202cdf0e10cSrcweir } 203cdf0e10cSrcweir catch( const Exception& ) 204cdf0e10cSrcweir { 205cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 206cdf0e10cSrcweir } 207cdf0e10cSrcweir } 208cdf0e10cSrcweir 209cdf0e10cSrcweir //---------------------------------------------------------------------------------------------------------------------- 210cdf0e10cSrcweir void Throbber::start() 211cdf0e10cSrcweir { 212cdf0e10cSrcweir maWaitTimer.Start(); 213cdf0e10cSrcweir } 214cdf0e10cSrcweir 215cdf0e10cSrcweir //---------------------------------------------------------------------------------------------------------------------- 216cdf0e10cSrcweir void Throbber::stop() 217cdf0e10cSrcweir { 218cdf0e10cSrcweir maWaitTimer.Stop(); 219cdf0e10cSrcweir } 220cdf0e10cSrcweir 221cdf0e10cSrcweir //---------------------------------------------------------------------------------------------------------------------- 222cdf0e10cSrcweir bool Throbber::isRunning() const 223cdf0e10cSrcweir { 224cdf0e10cSrcweir return maWaitTimer.IsActive(); 225cdf0e10cSrcweir } 226cdf0e10cSrcweir 227cdf0e10cSrcweir //---------------------------------------------------------------------------------------------------------------------- 228cdf0e10cSrcweir void Throbber::setImageList( ::std::vector< Image > const& i_images ) 229cdf0e10cSrcweir { 230cdf0e10cSrcweir maImageList = i_images; 231cdf0e10cSrcweir 232cdf0e10cSrcweir mnStepCount = maImageList.size(); 233cdf0e10cSrcweir const Image aInitialImage( mnStepCount ? maImageList[ 0 ] : Image() ); 234cdf0e10cSrcweir SetImage( aInitialImage ); 235cdf0e10cSrcweir } 236cdf0e10cSrcweir 237cdf0e10cSrcweir //---------------------------------------------------------------------------------------------------------------------- 238cdf0e10cSrcweir void Throbber::setImageList( const Sequence< Reference< XGraphic > >& rImageList ) 239cdf0e10cSrcweir { 240cdf0e10cSrcweir ::std::vector< Image > aImages( rImageList.getLength() ); 241cdf0e10cSrcweir ::std::copy( 242cdf0e10cSrcweir rImageList.getConstArray(), 243cdf0e10cSrcweir rImageList.getConstArray() + rImageList.getLength(), 244cdf0e10cSrcweir aImages.begin() 245cdf0e10cSrcweir ); 246cdf0e10cSrcweir setImageList( aImages ); 247cdf0e10cSrcweir } 248cdf0e10cSrcweir 249cdf0e10cSrcweir //---------------------------------------------------------------------------------------------------------------------- 250cdf0e10cSrcweir ::std::vector< ::rtl::OUString > Throbber::getDefaultImageURLs( const ImageSet i_imageSet ) 251cdf0e10cSrcweir { 252cdf0e10cSrcweir ::std::vector< ::rtl::OUString > aImageURLs; 253cdf0e10cSrcweir 254cdf0e10cSrcweir sal_Char const* const pResolutions[] = { "16", "32", "64" }; 255cdf0e10cSrcweir size_t const nImageCounts[] = { 6, 12, 12 }; 256cdf0e10cSrcweir 257cdf0e10cSrcweir size_t index = 0; 258cdf0e10cSrcweir switch ( i_imageSet ) 259cdf0e10cSrcweir { 260cdf0e10cSrcweir case IMAGES_16_PX: index = 0; break; 261cdf0e10cSrcweir case IMAGES_32_PX: index = 1; break; 262cdf0e10cSrcweir case IMAGES_64_PX: index = 2; break; 263cdf0e10cSrcweir case IMAGES_NONE: 264cdf0e10cSrcweir case IMAGES_AUTO: 265cdf0e10cSrcweir OSL_ENSURE( false, "Throbber::getDefaultImageURLs: illegal image set!" ); 266cdf0e10cSrcweir return aImageURLs; 267cdf0e10cSrcweir } 268cdf0e10cSrcweir 269cdf0e10cSrcweir aImageURLs.reserve( nImageCounts[index] ); 270cdf0e10cSrcweir for ( size_t i=0; i<nImageCounts[index]; ++i ) 271cdf0e10cSrcweir { 272cdf0e10cSrcweir ::rtl::OUStringBuffer aURL; 273aeee3b8fSAriel Constenla-Haile aURL.appendAscii( "private:graphicrepository/vcl/res/spinner-" ); 274cdf0e10cSrcweir aURL.appendAscii( pResolutions[index] ); 275cdf0e10cSrcweir aURL.appendAscii( "-" ); 276cdf0e10cSrcweir if ( i < 9 ) 277cdf0e10cSrcweir aURL.appendAscii( "0" ); 278cdf0e10cSrcweir aURL.append ( sal_Int32( i + 1 ) ); 279cdf0e10cSrcweir aURL.appendAscii( ".png" ); 280cdf0e10cSrcweir 281cdf0e10cSrcweir aImageURLs.push_back( aURL.makeStringAndClear() ); 282cdf0e10cSrcweir } 283cdf0e10cSrcweir 284cdf0e10cSrcweir return aImageURLs; 285cdf0e10cSrcweir } 286cdf0e10cSrcweir 287cdf0e10cSrcweir //---------------------------------------------------------------------------------------------------------------------- 288cdf0e10cSrcweir IMPL_LINK( Throbber, TimeOutHdl, void*, EMPTYARG ) 289cdf0e10cSrcweir { 290cdf0e10cSrcweir ::vos::OGuard aGuard( Application::GetSolarMutex() ); 291cdf0e10cSrcweir if ( maImageList.empty() ) 292cdf0e10cSrcweir return 0; 293cdf0e10cSrcweir 294cdf0e10cSrcweir if ( mnCurStep < mnStepCount - 1 ) 295cdf0e10cSrcweir mnCurStep += 1; 296cdf0e10cSrcweir else 297cdf0e10cSrcweir { 298cdf0e10cSrcweir if ( mbRepeat ) 299cdf0e10cSrcweir { 300cdf0e10cSrcweir // start over 301cdf0e10cSrcweir mnCurStep = 0; 302cdf0e10cSrcweir } 303cdf0e10cSrcweir else 304cdf0e10cSrcweir { 305cdf0e10cSrcweir stop(); 306cdf0e10cSrcweir } 307cdf0e10cSrcweir } 308cdf0e10cSrcweir 309cdf0e10cSrcweir SetImage( maImageList[ mnCurStep ] ); 310cdf0e10cSrcweir 311cdf0e10cSrcweir return 0; 312cdf0e10cSrcweir } 313