char array should be null terminated
[LibreOffice.git] / svx / source / gallery2 / galobj.cxx
blobd86553ace8c88266e1130c3d9c1c7d0289ac625f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_svx.hxx"
32 #define ENABLE_BYTESTRING_STREAM_OPERATORS
34 #include <com/sun/star/lang/XUnoTunnel.hpp>
35 #include <sfx2/objsh.hxx>
36 #include <sfx2/docfac.hxx>
38 #include <comphelper/classids.hxx>
39 #include <unotools/pathoptions.hxx>
41 #include <tools/rcid.h>
42 #include <tools/vcompat.hxx>
43 #include <vcl/virdev.hxx>
44 #include <svl/itempool.hxx>
45 #include <svx/fmmodel.hxx>
46 #include <svx/fmview.hxx>
47 #include <svx/fmpage.hxx>
48 #include "gallery.hrc"
49 #include "svx/galmisc.hxx"
50 #include "galobj.hxx"
51 #include <vcl/salbtype.hxx> // FRound
52 #include <vcl/svapp.hxx>
54 #include "gallerydrawmodel.hxx"
56 using namespace ::com::sun::star;
58 // -------------
59 // - SgaObject -
60 // -------------
62 SgaObject::SgaObject() :
63 bIsValid ( sal_False ),
64 bIsThumbBmp ( sal_True )
68 // ------------------------------------------------------------------------
70 sal_Bool SgaObject::CreateThumb( const Graphic& rGraphic )
72 sal_Bool bRet = sal_False;
74 if( rGraphic.GetType() == GRAPHIC_BITMAP )
76 BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
77 Size aBmpSize( aBmpEx.GetSizePixel() );
79 if( aBmpSize.Width() && aBmpSize.Height() )
81 const Color aWhite( COL_WHITE );
83 if( aBmpEx.GetPrefMapMode().GetMapUnit() != MAP_PIXEL &&
84 aBmpEx.GetPrefSize().Width() > 0 &&
85 aBmpEx.GetPrefSize().Height() > 0 )
87 Size aLogSize( OutputDevice::LogicToLogic( aBmpEx.GetPrefSize(), aBmpEx.GetPrefMapMode(), MAP_100TH_MM ) );
89 if( aLogSize.Width() > 0 && aLogSize.Height() > 0 )
91 double fFactorLog = static_cast< double >( aLogSize.Width() ) / aLogSize.Height();
92 double fFactorPix = static_cast< double >( aBmpSize.Width() ) / aBmpSize.Height();
94 if( fFactorPix > fFactorLog )
95 aBmpSize.Width() = FRound( aBmpSize.Height() * fFactorLog );
96 else
97 aBmpSize.Height() = FRound( aBmpSize.Width() / fFactorLog );
99 aBmpEx.SetSizePixel( aBmpSize );
103 aThumbBmp = aBmpEx.GetBitmap( &aWhite );
105 if( ( aBmpSize.Width() <= S_THUMB ) && ( aBmpSize.Height() <= S_THUMB ) )
107 aThumbBmp.Convert( BMP_CONVERSION_8BIT_COLORS );
108 bRet = sal_True;
110 else
112 const float fFactor = (float) aBmpSize.Width() / aBmpSize.Height();
113 const Size aNewSize( Max( (long) (fFactor < 1. ? S_THUMB * fFactor : S_THUMB), 8L ),
114 Max( (long) (fFactor < 1. ? S_THUMB : S_THUMB / fFactor), 8L ) );
116 if( aThumbBmp.Scale( (double) aNewSize.Width() / aBmpSize.Width(),
117 (double) aNewSize.Height() / aBmpSize.Height(), BMP_SCALE_INTERPOLATE ) )
119 aThumbBmp.Convert( BMP_CONVERSION_8BIT_COLORS );
120 bRet = sal_True;
125 else if( rGraphic.GetType() == GRAPHIC_GDIMETAFILE )
127 const Size aPrefSize( rGraphic.GetPrefSize() );
128 const double fFactor = (double)aPrefSize.Width() / (double)aPrefSize.Height();
129 Size aSize( S_THUMB, S_THUMB );
130 if ( fFactor < 1.0 )
131 aSize.Width() = (sal_Int32)( S_THUMB * fFactor );
132 else
133 aSize.Height() = (sal_Int32)( S_THUMB / fFactor );
135 const GraphicConversionParameters aParameters(aSize);
136 aThumbBmp = rGraphic.GetBitmap(aParameters);
138 if( !aThumbBmp.IsEmpty() )
140 aThumbBmp.Convert( BMP_CONVERSION_8BIT_COLORS );
141 bRet = sal_True;
145 return bRet;
148 // ------------------------------------------------------------------------
150 void SgaObject::WriteData( SvStream& rOut, const String& rDestDir ) const
152 static const sal_uInt32 nInventor = COMPAT_FORMAT( 'S', 'G', 'A', '3' );
154 rOut << nInventor << (sal_uInt16) 0x0004 << GetVersion() << (sal_uInt16) GetObjKind();
155 rOut << bIsThumbBmp;
157 if( bIsThumbBmp )
159 const sal_uInt16 nOldCompressMode = rOut.GetCompressMode();
160 const sal_uIntPtr nOldVersion = rOut.GetVersion();
162 rOut.SetCompressMode( COMPRESSMODE_ZBITMAP );
163 rOut.SetVersion( SOFFICE_FILEFORMAT_50 );
165 rOut << aThumbBmp;
167 rOut.SetVersion( nOldVersion );
168 rOut.SetCompressMode( nOldCompressMode );
170 else
171 rOut << aThumbMtf;
173 String aURLWithoutDestDir = String(aURL.GetMainURL( INetURLObject::NO_DECODE ));
174 aURLWithoutDestDir.SearchAndReplace(rDestDir, String());
175 rOut << ByteString( aURLWithoutDestDir, RTL_TEXTENCODING_UTF8 );
178 // ------------------------------------------------------------------------
180 void SgaObject::ReadData(SvStream& rIn, sal_uInt16& rReadVersion )
182 ByteString aTmpStr;
183 sal_uInt32 nTmp32;
184 sal_uInt16 nTmp16;
186 rIn >> nTmp32 >> nTmp16 >> rReadVersion >> nTmp16 >> bIsThumbBmp;
188 if( bIsThumbBmp )
189 rIn >> aThumbBmp;
190 else
191 rIn >> aThumbMtf;
193 rIn >> aTmpStr; aURL = INetURLObject( String( aTmpStr.GetBuffer(), RTL_TEXTENCODING_UTF8 ) );
196 // ------------------------------------------------------------------------
198 const String SgaObject::GetTitle() const
200 String aReturnValue( aTitle );
201 if ( !getenv( "GALLERY_SHOW_PRIVATE_TITLE" ) )
203 if ( aReturnValue.GetTokenCount( ':' ) == 3 )
205 String aPrivateInd ( aReturnValue.GetToken( 0, ':' ) );
206 String aResourceName( aReturnValue.GetToken( 1, ':' ) );
207 sal_Int32 nResId ( aReturnValue.GetToken( 2, ':' ).ToInt32() );
208 if ( aReturnValue.GetToken( 0, ':' ).EqualsAscii( "private" ) &&
209 aResourceName.Len() && ( nResId > 0 ) && ( nResId < 0x10000 ) )
211 ByteString aMgrName( aResourceName, RTL_TEXTENCODING_UTF8 );
212 ResMgr* pResMgr = ResMgr::CreateResMgr( aMgrName.GetBuffer(),
213 Application::GetSettings().GetUILocale() );
214 if ( pResMgr )
216 ResId aResId( (sal_uInt16)nResId, *pResMgr );
217 aResId.SetRT( RSC_STRING );
218 if ( pResMgr->IsAvailable( aResId ) )
220 aReturnValue = String( aResId );
222 delete pResMgr;
227 return aReturnValue;
230 // ------------------------------------------------------------------------
232 void SgaObject::SetTitle( const String& rTitle )
234 aTitle = rTitle;
237 // ------------------------------------------------------------------------
239 SvStream& operator<<( SvStream& rOut, const SgaObject& rObj )
241 rObj.WriteData( rOut, String() );
242 return rOut;
245 // ------------------------------------------------------------------------
247 SvStream& operator>>( SvStream& rIn, SgaObject& rObj )
249 sal_uInt16 nReadVersion;
251 rObj.ReadData( rIn, nReadVersion );
252 rObj.bIsValid = ( rIn.GetError() == ERRCODE_NONE );
254 return rIn;
257 // ----------------
258 // - SgaObjectBmp -
259 // ----------------
261 SgaObjectBmp::SgaObjectBmp()
265 // ------------------------------------------------------------------------
267 SgaObjectBmp::SgaObjectBmp( const INetURLObject& rURL )
269 Graphic aGraphic;
270 String aFilter;
272 if ( SGA_IMPORT_NONE != GalleryGraphicImport( rURL, aGraphic, aFilter ) )
273 Init( aGraphic, rURL );
276 // ------------------------------------------------------------------------
278 SgaObjectBmp::SgaObjectBmp( const Graphic& rGraphic, const INetURLObject& rURL, const String& )
280 if( FileExists( rURL ) )
281 Init( rGraphic, rURL );
284 // ------------------------------------------------------------------------
286 void SgaObjectBmp::Init( const Graphic& rGraphic, const INetURLObject& rURL )
288 aURL = rURL;
289 bIsValid = CreateThumb( rGraphic );
292 // ------------------------------------------------------------------------
294 void SgaObjectBmp::WriteData( SvStream& rOut, const String& rDestDir ) const
296 String aDummyStr;
297 char aDummy[ 10 ];
299 // Version setzen
300 SgaObject::WriteData( rOut, rDestDir );
301 rOut.Write( aDummy, 10 );
302 rOut << ByteString( aDummyStr, RTL_TEXTENCODING_UTF8 ) << ByteString( aTitle, RTL_TEXTENCODING_UTF8 );
305 // ------------------------------------------------------------------------
307 void SgaObjectBmp::ReadData( SvStream& rIn, sal_uInt16& rReadVersion )
309 ByteString aTmpStr;
311 SgaObject::ReadData( rIn, rReadVersion );
312 rIn.SeekRel( 10 ); // 16, 16, 32, 16
313 rIn >> aTmpStr; // dummy
315 if( rReadVersion >= 5 )
317 rIn >> aTmpStr; aTitle = String( aTmpStr.GetBuffer(), RTL_TEXTENCODING_UTF8 );
321 // ------------------
322 // - SgaObjectSound -
323 // ------------------
325 SgaObjectSound::SgaObjectSound() :
326 eSoundType( SOUND_STANDARD )
330 // ------------------------------------------------------------------------
332 SgaObjectSound::SgaObjectSound( const INetURLObject& rURL ) :
333 eSoundType( SOUND_STANDARD )
335 if( FileExists( rURL ) )
337 aURL = rURL;
338 aThumbBmp = Bitmap( Size( 1, 1 ), 1 );
339 bIsValid = sal_True;
341 else
342 bIsValid = sal_False;
345 // ------------------------------------------------------------------------
347 SgaObjectSound::~SgaObjectSound()
351 // ------------------------------------------------------------------------
353 Bitmap SgaObjectSound::GetThumbBmp() const
355 sal_uInt16 nId;
357 switch( eSoundType )
359 case( SOUND_COMPUTER ): nId = RID_SVXBMP_GALLERY_SOUND_1; break;
360 case( SOUND_MISC ): nId = RID_SVXBMP_GALLERY_SOUND_2; break;
361 case( SOUND_MUSIC ): nId = RID_SVXBMP_GALLERY_SOUND_3; break;
362 case( SOUND_NATURE ): nId = RID_SVXBMP_GALLERY_SOUND_4; break;
363 case( SOUND_SPEECH ): nId = RID_SVXBMP_GALLERY_SOUND_5; break;
364 case( SOUND_TECHNIC ): nId = RID_SVXBMP_GALLERY_SOUND_6; break;
365 case( SOUND_ANIMAL ): nId = RID_SVXBMP_GALLERY_SOUND_7; break;
367 // standard
368 default:
369 nId = RID_SVXBMP_GALLERY_MEDIA;
370 break;
373 const BitmapEx aBmpEx( GAL_RESID( nId ) );
374 const Color aTransColor( COL_WHITE );
376 return aBmpEx.GetBitmap( &aTransColor );
379 // ------------------------------------------------------------------------
381 void SgaObjectSound::WriteData( SvStream& rOut, const String& rDestDir ) const
383 SgaObject::WriteData( rOut, rDestDir );
384 rOut << (sal_uInt16) eSoundType << ByteString( aTitle, RTL_TEXTENCODING_UTF8 );
387 // ------------------------------------------------------------------------
389 void SgaObjectSound::ReadData( SvStream& rIn, sal_uInt16& rReadVersion )
391 SgaObject::ReadData( rIn, rReadVersion );
393 if( rReadVersion >= 5 )
395 ByteString aTmpStr;
396 sal_uInt16 nTmp16;
398 rIn >> nTmp16; eSoundType = (GalSoundType) nTmp16;
400 if( rReadVersion >= 6 )
402 rIn >> aTmpStr; aTitle = String( aTmpStr.GetBuffer(), RTL_TEXTENCODING_UTF8 );
407 // -----------------
408 // - SgaObjectAnim -
409 // -----------------
411 SgaObjectAnim::SgaObjectAnim()
415 // ------------------------------------------------------------------------
417 SgaObjectAnim::SgaObjectAnim( const Graphic& rGraphic,
418 const INetURLObject& rURL,
419 const String& )
421 aURL = rURL;
422 bIsValid = CreateThumb( rGraphic );
425 // -----------------
426 // - SgaObjectINet -
427 // -----------------
429 SgaObjectINet::SgaObjectINet()
433 // ------------------------------------------------------------------------
435 SgaObjectINet::SgaObjectINet( const Graphic& rGraphic, const INetURLObject& rURL, const String& rFormatName ) :
436 SgaObjectAnim ( rGraphic, rURL, rFormatName )
440 // -------------------
441 // - SgaObjectSvDraw -
442 // -------------------
444 SgaObjectSvDraw::SgaObjectSvDraw()
448 // ------------------------------------------------------------------------
450 SgaObjectSvDraw::SgaObjectSvDraw( const FmFormModel& rModel, const INetURLObject& rURL )
452 aURL = rURL;
453 bIsValid = CreateThumb( rModel );
456 // ------------------------------------------------------------------------
458 SvxGalleryDrawModel::SvxGalleryDrawModel()
459 : mpFormModel( 0 )
461 const String sFactoryURL(RTL_CONSTASCII_USTRINGPARAM("sdraw"));
463 mxDoc = SfxObjectShell::CreateObjectByFactoryName( sFactoryURL );
465 if( mxDoc.Is() )
467 mxDoc->DoInitNew(0);
469 uno::Reference< lang::XUnoTunnel > xTunnel( mxDoc->GetModel(), uno::UNO_QUERY );
470 if( xTunnel.is() )
472 mpFormModel = dynamic_cast< FmFormModel* >(
473 reinterpret_cast<SdrModel*>(xTunnel->getSomething(SdrModel::getUnoTunnelImplementationId())));
474 if( mpFormModel )
476 mpFormModel->InsertPage( mpFormModel->AllocPage( false ) );
482 // ------------------------------------------------------------------------
484 SvxGalleryDrawModel::~SvxGalleryDrawModel()
486 if( mxDoc.Is() )
487 mxDoc->DoClose();
490 // ------------------------------------------------------------------------
492 SgaObjectSvDraw::SgaObjectSvDraw( SvStream& rIStm, const INetURLObject& rURL )
494 SvxGalleryDrawModel aModel;
496 if( aModel.GetModel() )
498 if( GallerySvDrawImport( rIStm, *aModel.GetModel() ) )
500 aURL = rURL;
501 bIsValid = CreateThumb( *aModel.GetModel() );
506 // ------------------------------------------------------------------------
508 sal_Bool SgaObjectSvDraw::CreateThumb( const FmFormModel& rModel )
510 Graphic aGraphic;
511 ImageMap aImageMap;
512 sal_Bool bRet = sal_False;
514 if ( CreateIMapGraphic( rModel, aGraphic, aImageMap ) )
515 bRet = SgaObject::CreateThumb( aGraphic );
516 else
518 VirtualDevice aVDev;
520 aVDev.SetOutputSizePixel( Size( S_THUMB*2, S_THUMB*2 ) );
522 bRet = DrawCentered( &aVDev, rModel );
523 if( bRet )
525 aThumbBmp = aVDev.GetBitmap( Point(), aVDev.GetOutputSizePixel() );
527 Size aMS( 2, 2 );
528 BmpFilterParam aParam( aMS );
529 aThumbBmp.Filter( BMP_FILTER_MOSAIC, &aParam );
530 aThumbBmp.Scale( Size( S_THUMB, S_THUMB ) );
532 aThumbBmp.Convert( BMP_CONVERSION_8BIT_COLORS );
536 return bRet;
539 // ------------------------------------------------------------------------
541 sal_Bool SgaObjectSvDraw::DrawCentered( OutputDevice* pOut, const FmFormModel& rModel )
543 const FmFormPage* pPage = static_cast< const FmFormPage* >( rModel.GetPage( 0 ) );
544 sal_Bool bRet = sal_False;
546 if( pOut && pPage )
548 const Rectangle aObjRect( pPage->GetAllObjBoundRect() );
549 const Size aOutSizePix( pOut->GetOutputSizePixel() );
551 if( aObjRect.GetWidth() && aObjRect.GetHeight() && aOutSizePix.Width() > 2 && aOutSizePix.Height() > 2 )
553 FmFormView aView( const_cast< FmFormModel* >( &rModel ), pOut );
554 MapMode aMap( rModel.GetScaleUnit() );
555 Rectangle aDrawRectPix( Point( 1, 1 ), Size( aOutSizePix.Width() - 2, aOutSizePix.Height() - 2 ) );
556 const double fFactor = (double) aObjRect.GetWidth() / aObjRect.GetHeight();
557 Fraction aFrac( FRound( fFactor < 1. ? aDrawRectPix.GetWidth() * fFactor : aDrawRectPix.GetWidth() ),
558 pOut->LogicToPixel( aObjRect.GetSize(), aMap ).Width() );
560 aMap.SetScaleX( aFrac );
561 aMap.SetScaleY( aFrac );
563 const Size aDrawSize( pOut->PixelToLogic( aDrawRectPix.GetSize(), aMap ) );
564 Point aOrigin( pOut->PixelToLogic( aDrawRectPix.TopLeft(), aMap ) );
566 aOrigin.X() += ( ( aDrawSize.Width() - aObjRect.GetWidth() ) >> 1 ) - aObjRect.Left();
567 aOrigin.Y() += ( ( aDrawSize.Height() - aObjRect.GetHeight() ) >> 1 ) - aObjRect.Top();
568 aMap.SetOrigin( aOrigin );
570 aView.SetPageVisible( sal_False );
571 aView.SetBordVisible( sal_False );
572 aView.SetGridVisible( sal_False );
573 aView.SetHlplVisible( sal_False );
574 aView.SetGlueVisible( sal_False );
576 pOut->Push();
577 pOut->SetMapMode( aMap );
578 aView.ShowSdrPage( const_cast< FmFormPage* >( pPage ));
579 aView.CompleteRedraw( pOut, Rectangle( pOut->PixelToLogic( Point() ), pOut->GetOutputSize() ) );
580 pOut->Pop();
582 bRet = sal_True;
586 return bRet;
589 // ------------------------------------------------------------------------
591 void SgaObjectSvDraw::WriteData( SvStream& rOut, const String& rDestDir ) const
593 SgaObject::WriteData( rOut, rDestDir );
594 rOut << ByteString( aTitle, RTL_TEXTENCODING_UTF8 );
597 // ------------------------------------------------------------------------
599 void SgaObjectSvDraw::ReadData( SvStream& rIn, sal_uInt16& rReadVersion )
601 SgaObject::ReadData( rIn, rReadVersion );
603 if( rReadVersion >= 5 )
605 ByteString aTmpStr;
606 rIn >> aTmpStr; aTitle = String( aTmpStr.GetBuffer(), RTL_TEXTENCODING_UTF8 );
610 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */