Update ooo320-m1
[ooovba.git] / svx / source / gallery2 / galobj.cxx
bloba4b9cc0d396e2c2cb3a2834b48c506819ab77795
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: galobj.cxx,v $
10 * $Revision: 1.30 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 #define ENABLE_BYTESTRING_STREAM_OPERATORS
36 #include <com/sun/star/lang/XUnoTunnel.hpp>
37 #include <sfx2/objsh.hxx>
38 #include <sfx2/docfac.hxx>
40 #include <comphelper/classids.hxx>
41 #include <svtools/pathoptions.hxx>
43 #include <tools/rcid.h>
44 #include <tools/vcompat.hxx>
45 #include <vcl/virdev.hxx>
46 #include <svtools/itempool.hxx>
47 #include <svx/fmmodel.hxx>
48 #include <svx/fmview.hxx>
49 #include <svx/fmpage.hxx>
50 #include "gallery.hrc"
51 #include "galmisc.hxx"
52 #include "galobj.hxx"
53 #include <vcl/salbtype.hxx> // FRound
54 #include <vcl/svapp.hxx>
56 #include "gallerydrawmodel.hxx"
58 using namespace ::com::sun::star;
60 // -------------
61 // - SgaObject -
62 // -------------
64 SgaObject::SgaObject() :
65 bIsValid ( FALSE ),
66 bIsThumbBmp ( TRUE )
70 // ------------------------------------------------------------------------
72 BOOL SgaObject::CreateThumb( const Graphic& rGraphic )
74 BOOL bRet = FALSE;
76 if( rGraphic.GetType() == GRAPHIC_BITMAP )
78 BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
79 Size aBmpSize( aBmpEx.GetSizePixel() );
81 if( aBmpSize.Width() && aBmpSize.Height() )
83 const Color aWhite( COL_WHITE );
85 if( aBmpEx.GetPrefMapMode().GetMapUnit() != MAP_PIXEL &&
86 aBmpEx.GetPrefSize().Width() > 0 &&
87 aBmpEx.GetPrefSize().Height() > 0 )
89 Size aLogSize( OutputDevice::LogicToLogic( aBmpEx.GetPrefSize(), aBmpEx.GetPrefMapMode(), MAP_100TH_MM ) );
91 if( aLogSize.Width() > 0 && aLogSize.Height() > 0 )
93 double fFactorLog = static_cast< double >( aLogSize.Width() ) / aLogSize.Height();
94 double fFactorPix = static_cast< double >( aBmpSize.Width() ) / aBmpSize.Height();
96 if( fFactorPix > fFactorLog )
97 aBmpSize.Width() = FRound( aBmpSize.Height() * fFactorLog );
98 else
99 aBmpSize.Height() = FRound( aBmpSize.Width() / fFactorLog );
101 aBmpEx.SetSizePixel( aBmpSize );
105 aThumbBmp = aBmpEx.GetBitmap( &aWhite );
107 if( ( aBmpSize.Width() <= S_THUMB ) && ( aBmpSize.Height() <= S_THUMB ) )
109 aThumbBmp.Convert( BMP_CONVERSION_8BIT_COLORS );
110 bRet = TRUE;
112 else
114 const float fFactor = (float) aBmpSize.Width() / aBmpSize.Height();
115 const Size aNewSize( Max( (long) (fFactor < 1. ? S_THUMB * fFactor : S_THUMB), 8L ),
116 Max( (long) (fFactor < 1. ? S_THUMB : S_THUMB / fFactor), 8L ) );
118 if( aThumbBmp.Scale( (double) aNewSize.Width() / aBmpSize.Width(),
119 (double) aNewSize.Height() / aBmpSize.Height(), BMP_SCALE_INTERPOLATE ) )
121 aThumbBmp.Convert( BMP_CONVERSION_8BIT_COLORS );
122 bRet = TRUE;
127 else if( rGraphic.GetType() == GRAPHIC_GDIMETAFILE )
129 const Size aPrefSize( rGraphic.GetPrefSize() );
130 const double fFactor = (double)aPrefSize.Width() / (double)aPrefSize.Height();
131 Size aSize( S_THUMB, S_THUMB );
132 if ( fFactor < 1.0 )
133 aSize.Width() = (sal_Int32)( S_THUMB * fFactor );
134 else
135 aSize.Height() = (sal_Int32)( S_THUMB / fFactor );
137 const GraphicConversionParameters aParameters(aSize);
138 aThumbBmp = rGraphic.GetBitmap(aParameters);
140 if( !aThumbBmp.IsEmpty() )
142 aThumbBmp.Convert( BMP_CONVERSION_8BIT_COLORS );
143 bRet = TRUE;
147 return bRet;
150 // ------------------------------------------------------------------------
152 void SgaObject::WriteData( SvStream& rOut, const String& rDestDir ) const
154 static const UINT32 nInventor = COMPAT_FORMAT( 'S', 'G', 'A', '3' );
156 rOut << nInventor << (UINT16) 0x0004 << GetVersion() << (UINT16) GetObjKind();
157 rOut << bIsThumbBmp;
159 if( bIsThumbBmp )
161 const USHORT nOldCompressMode = rOut.GetCompressMode();
162 const ULONG nOldVersion = rOut.GetVersion();
164 rOut.SetCompressMode( COMPRESSMODE_ZBITMAP );
165 rOut.SetVersion( SOFFICE_FILEFORMAT_50 );
167 rOut << aThumbBmp;
169 rOut.SetVersion( nOldVersion );
170 rOut.SetCompressMode( nOldCompressMode );
172 else
173 rOut << aThumbMtf;
175 String aURLWithoutDestDir = String(aURL.GetMainURL( INetURLObject::NO_DECODE ));
176 aURLWithoutDestDir.SearchAndReplace(rDestDir, String());
177 rOut << ByteString( aURLWithoutDestDir, RTL_TEXTENCODING_UTF8 );
180 // ------------------------------------------------------------------------
182 void SgaObject::ReadData(SvStream& rIn, UINT16& rReadVersion )
184 ByteString aTmpStr;
185 UINT32 nTmp32;
186 UINT16 nTmp16;
188 rIn >> nTmp32 >> nTmp16 >> rReadVersion >> nTmp16 >> bIsThumbBmp;
190 if( bIsThumbBmp )
191 rIn >> aThumbBmp;
192 else
193 rIn >> aThumbMtf;
195 rIn >> aTmpStr; aURL = INetURLObject( String( aTmpStr.GetBuffer(), RTL_TEXTENCODING_UTF8 ) );
198 // ------------------------------------------------------------------------
200 const String SgaObject::GetTitle() const
202 String aReturnValue( aTitle );
203 if ( !getenv( "GALLERY_SHOW_PRIVATE_TITLE" ) )
205 if ( aReturnValue.GetTokenCount( ':' ) == 3 )
207 String aPrivateInd ( aReturnValue.GetToken( 0, ':' ) );
208 String aResourceName( aReturnValue.GetToken( 1, ':' ) );
209 sal_Int32 nResId ( aReturnValue.GetToken( 2, ':' ).ToInt32() );
210 if ( aReturnValue.GetToken( 0, ':' ).EqualsAscii( "private" ) &&
211 aResourceName.Len() && ( nResId > 0 ) && ( nResId < 0x10000 ) )
213 ByteString aMgrName( aResourceName, RTL_TEXTENCODING_UTF8 );
214 ResMgr* pResMgr = ResMgr::CreateResMgr( aMgrName.GetBuffer(),
215 Application::GetSettings().GetUILocale() );
216 if ( pResMgr )
218 ResId aResId( (sal_uInt16)nResId, *pResMgr );
219 aResId.SetRT( RSC_STRING );
220 if ( pResMgr->IsAvailable( aResId ) )
222 aReturnValue = String( aResId );
224 delete pResMgr;
229 return aReturnValue;
232 // ------------------------------------------------------------------------
234 void SgaObject::SetTitle( const String& rTitle )
236 aTitle = rTitle;
239 // ------------------------------------------------------------------------
241 SvStream& operator<<( SvStream& rOut, const SgaObject& rObj )
243 rObj.WriteData( rOut, String() );
244 return rOut;
247 // ------------------------------------------------------------------------
249 SvStream& operator>>( SvStream& rIn, SgaObject& rObj )
251 UINT16 nReadVersion;
253 rObj.ReadData( rIn, nReadVersion );
254 rObj.bIsValid = ( rIn.GetError() == ERRCODE_NONE );
256 return rIn;
259 // ----------------
260 // - SgaObjectBmp -
261 // ----------------
263 SgaObjectBmp::SgaObjectBmp()
267 // ------------------------------------------------------------------------
269 SgaObjectBmp::SgaObjectBmp( const INetURLObject& rURL )
271 Graphic aGraphic;
272 String aFilter;
274 if ( SGA_IMPORT_NONE != GalleryGraphicImport( rURL, aGraphic, aFilter ) )
275 Init( aGraphic, rURL );
278 // ------------------------------------------------------------------------
280 SgaObjectBmp::SgaObjectBmp( const Graphic& rGraphic, const INetURLObject& rURL, const String& )
282 if( FileExists( rURL ) )
283 Init( rGraphic, rURL );
286 // ------------------------------------------------------------------------
288 void SgaObjectBmp::Init( const Graphic& rGraphic, const INetURLObject& rURL )
290 aURL = rURL;
291 bIsValid = CreateThumb( rGraphic );
294 // ------------------------------------------------------------------------
296 void SgaObjectBmp::WriteData( SvStream& rOut, const String& rDestDir ) const
298 String aDummyStr;
299 char aDummy[ 10 ];
301 // Version setzen
302 SgaObject::WriteData( rOut, rDestDir );
303 rOut.Write( aDummy, 10 );
304 rOut << ByteString( aDummyStr, RTL_TEXTENCODING_UTF8 ) << ByteString( aTitle, RTL_TEXTENCODING_UTF8 );
307 // ------------------------------------------------------------------------
309 void SgaObjectBmp::ReadData( SvStream& rIn, UINT16& rReadVersion )
311 ByteString aTmpStr;
313 SgaObject::ReadData( rIn, rReadVersion );
314 rIn.SeekRel( 10 ); // 16, 16, 32, 16
315 rIn >> aTmpStr; // dummy
317 if( rReadVersion >= 5 )
319 rIn >> aTmpStr; aTitle = String( aTmpStr.GetBuffer(), RTL_TEXTENCODING_UTF8 );
323 // ------------------
324 // - SgaObjectSound -
325 // ------------------
327 SgaObjectSound::SgaObjectSound() :
328 eSoundType( SOUND_STANDARD )
332 // ------------------------------------------------------------------------
334 SgaObjectSound::SgaObjectSound( const INetURLObject& rURL ) :
335 eSoundType( SOUND_STANDARD )
337 if( FileExists( rURL ) )
339 aURL = rURL;
340 aThumbBmp = Bitmap( Size( 1, 1 ), 1 );
341 bIsValid = TRUE;
343 else
344 bIsValid = FALSE;
347 // ------------------------------------------------------------------------
349 SgaObjectSound::~SgaObjectSound()
353 // ------------------------------------------------------------------------
355 Bitmap SgaObjectSound::GetThumbBmp() const
357 USHORT nId;
359 switch( eSoundType )
361 case( SOUND_COMPUTER ): nId = RID_SVXBMP_GALLERY_SOUND_1; break;
362 case( SOUND_MISC ): nId = RID_SVXBMP_GALLERY_SOUND_2; break;
363 case( SOUND_MUSIC ): nId = RID_SVXBMP_GALLERY_SOUND_3; break;
364 case( SOUND_NATURE ): nId = RID_SVXBMP_GALLERY_SOUND_4; break;
365 case( SOUND_SPEECH ): nId = RID_SVXBMP_GALLERY_SOUND_5; break;
366 case( SOUND_TECHNIC ): nId = RID_SVXBMP_GALLERY_SOUND_6; break;
367 case( SOUND_ANIMAL ): nId = RID_SVXBMP_GALLERY_SOUND_7; break;
369 // standard
370 default:
371 nId = RID_SVXBMP_GALLERY_MEDIA;
372 break;
375 const BitmapEx aBmpEx( GAL_RESID( nId ) );
376 const Color aTransColor( COL_WHITE );
378 return aBmpEx.GetBitmap( &aTransColor );
381 // ------------------------------------------------------------------------
383 void SgaObjectSound::WriteData( SvStream& rOut, const String& rDestDir ) const
385 SgaObject::WriteData( rOut, rDestDir );
386 rOut << (UINT16) eSoundType << ByteString( aTitle, RTL_TEXTENCODING_UTF8 );
389 // ------------------------------------------------------------------------
391 void SgaObjectSound::ReadData( SvStream& rIn, UINT16& rReadVersion )
393 SgaObject::ReadData( rIn, rReadVersion );
395 if( rReadVersion >= 5 )
397 ByteString aTmpStr;
398 UINT16 nTmp16;
400 rIn >> nTmp16; eSoundType = (GalSoundType) nTmp16;
402 if( rReadVersion >= 6 )
404 rIn >> aTmpStr; aTitle = String( aTmpStr.GetBuffer(), RTL_TEXTENCODING_UTF8 );
409 // -----------------
410 // - SgaObjectAnim -
411 // -----------------
413 SgaObjectAnim::SgaObjectAnim()
417 // ------------------------------------------------------------------------
419 SgaObjectAnim::SgaObjectAnim( const Graphic& rGraphic,
420 const INetURLObject& rURL,
421 const String& )
423 aURL = rURL;
424 bIsValid = CreateThumb( rGraphic );
427 // -----------------
428 // - SgaObjectINet -
429 // -----------------
431 SgaObjectINet::SgaObjectINet()
435 // ------------------------------------------------------------------------
437 SgaObjectINet::SgaObjectINet( const Graphic& rGraphic, const INetURLObject& rURL, const String& rFormatName ) :
438 SgaObjectAnim ( rGraphic, rURL, rFormatName )
442 // -------------------
443 // - SgaObjectSvDraw -
444 // -------------------
446 SgaObjectSvDraw::SgaObjectSvDraw()
450 // ------------------------------------------------------------------------
452 SgaObjectSvDraw::SgaObjectSvDraw( const FmFormModel& rModel, const INetURLObject& rURL )
454 aURL = rURL;
455 bIsValid = CreateThumb( rModel );
458 // ------------------------------------------------------------------------
460 SvxGalleryDrawModel::SvxGalleryDrawModel()
461 : mpFormModel( 0 )
463 const String sFactoryURL(RTL_CONSTASCII_USTRINGPARAM("sdraw"));
465 mxDoc = SfxObjectShell::CreateObjectByFactoryName( sFactoryURL );
467 if( mxDoc.Is() )
469 mxDoc->DoInitNew(0);
471 uno::Reference< lang::XUnoTunnel > xTunnel( mxDoc->GetModel(), uno::UNO_QUERY );
472 if( xTunnel.is() )
474 mpFormModel = dynamic_cast< FmFormModel* >(
475 reinterpret_cast<SdrModel*>(xTunnel->getSomething(SdrModel::getUnoTunnelImplementationId())));
476 if( mpFormModel )
478 mpFormModel->InsertPage( mpFormModel->AllocPage( false ) );
484 // ------------------------------------------------------------------------
486 SvxGalleryDrawModel::~SvxGalleryDrawModel()
488 if( mxDoc.Is() )
489 mxDoc->DoClose();
492 // ------------------------------------------------------------------------
494 SgaObjectSvDraw::SgaObjectSvDraw( SvStream& rIStm, const INetURLObject& rURL )
496 SvxGalleryDrawModel aModel;
498 if( aModel.GetModel() )
500 if( GallerySvDrawImport( rIStm, *aModel.GetModel() ) )
502 aURL = rURL;
503 bIsValid = CreateThumb( *aModel.GetModel() );
508 // ------------------------------------------------------------------------
510 BOOL SgaObjectSvDraw::CreateThumb( const FmFormModel& rModel )
512 Graphic aGraphic;
513 ImageMap aImageMap;
514 BOOL bRet = FALSE;
516 if ( CreateIMapGraphic( rModel, aGraphic, aImageMap ) )
517 bRet = SgaObject::CreateThumb( aGraphic );
518 else
520 VirtualDevice aVDev;
522 aVDev.SetOutputSizePixel( Size( S_THUMB*2, S_THUMB*2 ) );
524 bRet = DrawCentered( &aVDev, rModel );
525 if( bRet )
527 aThumbBmp = aVDev.GetBitmap( Point(), aVDev.GetOutputSizePixel() );
529 Size aMS( 2, 2 );
530 BmpFilterParam aParam( aMS );
531 aThumbBmp.Filter( BMP_FILTER_MOSAIC, &aParam );
532 aThumbBmp.Scale( Size( S_THUMB, S_THUMB ) );
534 aThumbBmp.Convert( BMP_CONVERSION_8BIT_COLORS );
538 return bRet;
541 // ------------------------------------------------------------------------
543 BOOL SgaObjectSvDraw::DrawCentered( OutputDevice* pOut, const FmFormModel& rModel )
545 const FmFormPage* pPage = static_cast< const FmFormPage* >( rModel.GetPage( 0 ) );
546 BOOL bRet = FALSE;
548 if( pOut && pPage )
550 const Rectangle aObjRect( pPage->GetAllObjBoundRect() );
551 const Size aOutSizePix( pOut->GetOutputSizePixel() );
553 if( aObjRect.GetWidth() && aObjRect.GetHeight() && aOutSizePix.Width() > 2 && aOutSizePix.Height() > 2 )
555 FmFormView aView( const_cast< FmFormModel* >( &rModel ), pOut );
556 MapMode aMap( rModel.GetScaleUnit() );
557 Rectangle aDrawRectPix( Point( 1, 1 ), Size( aOutSizePix.Width() - 2, aOutSizePix.Height() - 2 ) );
558 const double fFactor = (double) aObjRect.GetWidth() / aObjRect.GetHeight();
559 Fraction aFrac( FRound( fFactor < 1. ? aDrawRectPix.GetWidth() * fFactor : aDrawRectPix.GetWidth() ),
560 pOut->LogicToPixel( aObjRect.GetSize(), aMap ).Width() );
562 aMap.SetScaleX( aFrac );
563 aMap.SetScaleY( aFrac );
565 const Size aDrawSize( pOut->PixelToLogic( aDrawRectPix.GetSize(), aMap ) );
566 Point aOrigin( pOut->PixelToLogic( aDrawRectPix.TopLeft(), aMap ) );
568 aOrigin.X() += ( ( aDrawSize.Width() - aObjRect.GetWidth() ) >> 1 ) - aObjRect.Left();
569 aOrigin.Y() += ( ( aDrawSize.Height() - aObjRect.GetHeight() ) >> 1 ) - aObjRect.Top();
570 aMap.SetOrigin( aOrigin );
572 aView.SetPageVisible( FALSE );
573 aView.SetBordVisible( FALSE );
574 aView.SetGridVisible( FALSE );
575 aView.SetHlplVisible( FALSE );
576 aView.SetGlueVisible( FALSE );
578 pOut->Push();
579 pOut->SetMapMode( aMap );
580 aView.ShowSdrPage( const_cast< FmFormPage* >( pPage ));
581 aView.CompleteRedraw( pOut, Rectangle( pOut->PixelToLogic( Point() ), pOut->GetOutputSize() ) );
582 pOut->Pop();
584 bRet = TRUE;
588 return bRet;
591 // ------------------------------------------------------------------------
593 void SgaObjectSvDraw::WriteData( SvStream& rOut, const String& rDestDir ) const
595 SgaObject::WriteData( rOut, rDestDir );
596 rOut << ByteString( aTitle, RTL_TEXTENCODING_UTF8 );
599 // ------------------------------------------------------------------------
601 void SgaObjectSvDraw::ReadData( SvStream& rIn, UINT16& rReadVersion )
603 SgaObject::ReadData( rIn, rReadVersion );
605 if( rReadVersion >= 5 )
607 ByteString aTmpStr;
608 rIn >> aTmpStr; aTitle = String( aTmpStr.GetBuffer(), RTL_TEXTENCODING_UTF8 );