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