1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <com/sun/star/lang/XUnoTunnel.hpp>
22 #include <sfx2/objsh.hxx>
23 #include <sfx2/docfac.hxx>
25 #include <comphelper/classids.hxx>
26 #include <comphelper/string.hxx>
27 #include <unotools/pathoptions.hxx>
29 #include <tools/rcid.h>
30 #include <tools/vcompat.hxx>
31 #include <tools/helpers.hxx>
32 #include <vcl/virdev.hxx>
33 #include <svl/itempool.hxx>
34 #include <svx/fmmodel.hxx>
35 #include <svx/fmview.hxx>
36 #include <svx/fmpage.hxx>
37 #include "gallery.hrc"
38 #include "svx/galmisc.hxx"
40 #include <vcl/svapp.hxx>
42 #include "gallerydrawmodel.hxx"
44 using namespace ::com::sun::star
;
50 SgaObject::SgaObject() :
51 bIsValid ( sal_False
),
52 bIsThumbBmp ( sal_True
)
56 // ------------------------------------------------------------------------
58 sal_Bool
SgaObject::CreateThumb( const Graphic
& rGraphic
)
60 sal_Bool bRet
= sal_False
;
62 if( rGraphic
.GetType() == GRAPHIC_BITMAP
)
64 BitmapEx
aBmpEx( rGraphic
.GetBitmapEx() );
65 Size
aBmpSize( aBmpEx
.GetSizePixel() );
67 if( aBmpSize
.Width() && aBmpSize
.Height() )
69 if( aBmpEx
.GetPrefMapMode().GetMapUnit() != MAP_PIXEL
&&
70 aBmpEx
.GetPrefSize().Width() > 0 &&
71 aBmpEx
.GetPrefSize().Height() > 0 )
73 Size
aLogSize( OutputDevice::LogicToLogic( aBmpEx
.GetPrefSize(), aBmpEx
.GetPrefMapMode(), MAP_100TH_MM
) );
75 if( aLogSize
.Width() > 0 && aLogSize
.Height() > 0 )
77 double fFactorLog
= static_cast< double >( aLogSize
.Width() ) / aLogSize
.Height();
78 double fFactorPix
= static_cast< double >( aBmpSize
.Width() ) / aBmpSize
.Height();
80 if( fFactorPix
> fFactorLog
)
81 aBmpSize
.Width() = FRound( aBmpSize
.Height() * fFactorLog
);
83 aBmpSize
.Height() = FRound( aBmpSize
.Width() / fFactorLog
);
85 aBmpEx
.SetSizePixel( aBmpSize
, BMP_SCALE_BESTQUALITY
);
92 if( ( aBmpSize
.Width() <= S_THUMB
) && ( aBmpSize
.Height() <= S_THUMB
) )
94 aThumbBmp
.Convert( BMP_CONVERSION_8BIT_COLORS
);
99 const float fFactor
= (float) aBmpSize
.Width() / aBmpSize
.Height();
100 const Size
aNewSize( std::max( (long) (fFactor
< 1. ? S_THUMB
* fFactor
: S_THUMB
), 8L ),
101 std::max( (long) (fFactor
< 1. ? S_THUMB
: S_THUMB
/ fFactor
), 8L ) );
103 (double) aNewSize
.Width() / aBmpSize
.Width(),
104 (double) aNewSize
.Height() / aBmpSize
.Height(),
105 BMP_SCALE_BESTQUALITY
) )
107 aThumbBmp
.Convert( BMP_CONVERSION_8BIT_COLORS
);
113 else if( rGraphic
.GetType() == GRAPHIC_GDIMETAFILE
)
115 const Size
aPrefSize( rGraphic
.GetPrefSize() );
116 const double fFactor
= (double)aPrefSize
.Width() / (double)aPrefSize
.Height();
117 Size
aSize( S_THUMB
, S_THUMB
);
119 aSize
.Width() = (sal_Int32
)( S_THUMB
* fFactor
);
121 aSize
.Height() = (sal_Int32
)( S_THUMB
/ fFactor
);
123 const GraphicConversionParameters
aParameters(aSize
, false, true, true /*TODO: extra ", true" post-#i121194#*/);
124 aThumbBmp
= rGraphic
.GetBitmapEx(aParameters
);
126 if( !aThumbBmp
.IsEmpty() )
128 aThumbBmp
.Convert( BMP_CONVERSION_8BIT_COLORS
);
136 // ------------------------------------------------------------------------
138 void SgaObject::WriteData( SvStream
& rOut
, const String
& rDestDir
) const
140 static const sal_uInt32 nInventor
= COMPAT_FORMAT( 'S', 'G', 'A', '3' );
142 rOut
<< nInventor
<< (sal_uInt16
) 0x0004 << GetVersion() << (sal_uInt16
) GetObjKind();
147 const sal_uInt16 nOldCompressMode
= rOut
.GetCompressMode();
148 const sal_uIntPtr nOldVersion
= rOut
.GetVersion();
150 rOut
.SetCompressMode( COMPRESSMODE_ZBITMAP
);
151 rOut
.SetVersion( SOFFICE_FILEFORMAT_50
);
155 rOut
.SetVersion( nOldVersion
);
156 rOut
.SetCompressMode( nOldCompressMode
);
161 String aURLWithoutDestDir
= String(aURL
.GetMainURL( INetURLObject::NO_DECODE
));
162 aURLWithoutDestDir
.SearchAndReplace(rDestDir
, String());
163 write_lenPrefixed_uInt8s_FromOUString
<sal_uInt16
>(rOut
, aURLWithoutDestDir
, RTL_TEXTENCODING_UTF8
);
166 // ------------------------------------------------------------------------
168 void SgaObject::ReadData(SvStream
& rIn
, sal_uInt16
& rReadVersion
)
173 rIn
>> nTmp32
>> nTmp16
>> rReadVersion
>> nTmp16
>> bIsThumbBmp
;
180 OUString aTmpStr
= read_lenPrefixed_uInt8s_ToOUString
<sal_uInt16
>(rIn
, RTL_TEXTENCODING_UTF8
);
181 aURL
= INetURLObject(aTmpStr
);
184 // ------------------------------------------------------------------------
186 const String
SgaObject::GetTitle() const
188 String
aReturnValue( aTitle
);
189 if ( !getenv( "GALLERY_SHOW_PRIVATE_TITLE" ) )
191 if ( comphelper::string::getTokenCount(aReturnValue
, ':') == 3 )
193 String
aPrivateInd ( aReturnValue
.GetToken( 0, ':' ) );
194 String
aResourceName( aReturnValue
.GetToken( 1, ':' ) );
195 sal_Int32
nResId ( aReturnValue
.GetToken( 2, ':' ).ToInt32() );
196 if ( aPrivateInd
.EqualsAscii( "private" ) &&
197 aResourceName
.Len() && ( nResId
> 0 ) && ( nResId
< 0x10000 ) )
199 OString
aMgrName(OUStringToOString(aResourceName
, RTL_TEXTENCODING_UTF8
));
200 ResMgr
* pResMgr
= ResMgr::CreateResMgr( aMgrName
.getStr(),
201 Application::GetSettings().GetUILanguageTag() );
204 ResId
aResId( (sal_uInt16
)nResId
, *pResMgr
);
205 aResId
.SetRT( RSC_STRING
);
206 if ( pResMgr
->IsAvailable( aResId
) )
208 aReturnValue
= aResId
.toString();
218 // ------------------------------------------------------------------------
220 void SgaObject::SetTitle( const String
& rTitle
)
225 // ------------------------------------------------------------------------
227 SvStream
& operator<<( SvStream
& rOut
, const SgaObject
& rObj
)
229 rObj
.WriteData( rOut
, String() );
233 // ------------------------------------------------------------------------
235 SvStream
& operator>>( SvStream
& rIn
, SgaObject
& rObj
)
237 sal_uInt16 nReadVersion
;
239 rObj
.ReadData( rIn
, nReadVersion
);
240 rObj
.bIsValid
= ( rIn
.GetError() == ERRCODE_NONE
);
249 SgaObjectBmp::SgaObjectBmp()
253 // ------------------------------------------------------------------------
255 SgaObjectBmp::SgaObjectBmp( const INetURLObject
& rURL
)
260 if ( SGA_IMPORT_NONE
!= GalleryGraphicImport( rURL
, aGraphic
, aFilter
) )
261 Init( aGraphic
, rURL
);
264 // ------------------------------------------------------------------------
266 SgaObjectBmp::SgaObjectBmp( const Graphic
& rGraphic
, const INetURLObject
& rURL
, const String
& )
268 if( FileExists( rURL
) )
269 Init( rGraphic
, rURL
);
272 // ------------------------------------------------------------------------
274 void SgaObjectBmp::Init( const Graphic
& rGraphic
, const INetURLObject
& rURL
)
277 bIsValid
= CreateThumb( rGraphic
);
280 // ------------------------------------------------------------------------
282 void SgaObjectBmp::WriteData( SvStream
& rOut
, const String
& rDestDir
) const
285 SgaObject::WriteData( rOut
, rDestDir
);
287 rOut
.Write( aDummy
, 10 );
288 write_lenPrefixed_uInt8s_FromOString
<sal_uInt16
>(rOut
, OString()); //dummy
289 write_lenPrefixed_uInt8s_FromOUString
<sal_uInt16
>(rOut
, aTitle
, RTL_TEXTENCODING_UTF8
);
292 // ------------------------------------------------------------------------
294 void SgaObjectBmp::ReadData( SvStream
& rIn
, sal_uInt16
& rReadVersion
)
297 SgaObject::ReadData( rIn
, rReadVersion
);
298 rIn
.SeekRel( 10 ); // 16, 16, 32, 16
299 read_lenPrefixed_uInt8s_ToOString
<sal_uInt16
>(rIn
); //dummy
301 if( rReadVersion
>= 5 )
302 aTitle
= read_lenPrefixed_uInt8s_ToOUString
<sal_uInt16
>(rIn
, RTL_TEXTENCODING_UTF8
);
305 // ------------------
306 // - SgaObjectSound -
307 // ------------------
308 DBG_NAME(SgaObjectSound
)
310 SgaObjectSound::SgaObjectSound() :
311 eSoundType( SOUND_STANDARD
)
313 DBG_CTOR(SgaObjectSound
,NULL
);
316 // ------------------------------------------------------------------------
318 SgaObjectSound::SgaObjectSound( const INetURLObject
& rURL
) :
319 eSoundType( SOUND_STANDARD
)
321 DBG_CTOR(SgaObjectSound
,NULL
);
323 if( FileExists( rURL
) )
326 aThumbBmp
= Bitmap( Size( 1, 1 ), 1 );
330 bIsValid
= sal_False
;
333 // ------------------------------------------------------------------------
335 SgaObjectSound::~SgaObjectSound()
337 DBG_DTOR(SgaObjectSound
,NULL
);
340 // ------------------------------------------------------------------------
342 BitmapEx
SgaObjectSound::GetThumbBmp() const
348 case( SOUND_COMPUTER
): nId
= RID_SVXBMP_GALLERY_SOUND_1
; break;
349 case( SOUND_MISC
): nId
= RID_SVXBMP_GALLERY_SOUND_2
; break;
350 case( SOUND_MUSIC
): nId
= RID_SVXBMP_GALLERY_SOUND_3
; break;
351 case( SOUND_NATURE
): nId
= RID_SVXBMP_GALLERY_SOUND_4
; break;
352 case( SOUND_SPEECH
): nId
= RID_SVXBMP_GALLERY_SOUND_5
; break;
353 case( SOUND_TECHNIC
): nId
= RID_SVXBMP_GALLERY_SOUND_6
; break;
354 case( SOUND_ANIMAL
): nId
= RID_SVXBMP_GALLERY_SOUND_7
; break;
358 nId
= RID_SVXBMP_GALLERY_MEDIA
;
362 const BitmapEx
aBmpEx( GAL_RES( nId
) );
367 // ------------------------------------------------------------------------
369 void SgaObjectSound::WriteData( SvStream
& rOut
, const String
& rDestDir
) const
371 SgaObject::WriteData( rOut
, rDestDir
);
372 rOut
<< (sal_uInt16
) eSoundType
;
373 write_lenPrefixed_uInt8s_FromOUString
<sal_uInt16
>(rOut
, aTitle
, RTL_TEXTENCODING_UTF8
);
376 // ------------------------------------------------------------------------
378 void SgaObjectSound::ReadData( SvStream
& rIn
, sal_uInt16
& rReadVersion
)
380 SgaObject::ReadData( rIn
, rReadVersion
);
382 if( rReadVersion
>= 5 )
386 rIn
>> nTmp16
; eSoundType
= (GalSoundType
) nTmp16
;
388 if( rReadVersion
>= 6 )
389 aTitle
= read_lenPrefixed_uInt8s_ToOUString
<sal_uInt16
>(rIn
, RTL_TEXTENCODING_UTF8
);
397 SgaObjectAnim::SgaObjectAnim()
401 // ------------------------------------------------------------------------
403 SgaObjectAnim::SgaObjectAnim( const Graphic
& rGraphic
,
404 const INetURLObject
& rURL
,
408 bIsValid
= CreateThumb( rGraphic
);
415 SgaObjectINet::SgaObjectINet()
419 // ------------------------------------------------------------------------
421 SgaObjectINet::SgaObjectINet( const Graphic
& rGraphic
, const INetURLObject
& rURL
, const String
& rFormatName
) :
422 SgaObjectAnim ( rGraphic
, rURL
, rFormatName
)
426 // -------------------
427 // - SgaObjectSvDraw -
428 // -------------------
430 SgaObjectSvDraw::SgaObjectSvDraw()
434 // ------------------------------------------------------------------------
436 SgaObjectSvDraw::SgaObjectSvDraw( const FmFormModel
& rModel
, const INetURLObject
& rURL
)
439 bIsValid
= CreateThumb( rModel
);
442 // ------------------------------------------------------------------------
443 DBG_NAME(SvxGalleryDrawModel
)
445 SvxGalleryDrawModel::SvxGalleryDrawModel()
448 DBG_CTOR(SvxGalleryDrawModel
,NULL
);
450 const String
sFactoryURL(RTL_CONSTASCII_USTRINGPARAM("sdraw"));
452 mxDoc
= SfxObjectShell::CreateObjectByFactoryName( sFactoryURL
);
458 uno::Reference
< lang::XUnoTunnel
> xTunnel( mxDoc
->GetModel(), uno::UNO_QUERY
);
461 mpFormModel
= dynamic_cast< FmFormModel
* >(
462 reinterpret_cast<SdrModel
*>(xTunnel
->getSomething(SdrModel::getUnoTunnelImplementationId())));
465 mpFormModel
->InsertPage( mpFormModel
->AllocPage( false ) );
471 // ------------------------------------------------------------------------
473 SvxGalleryDrawModel::~SvxGalleryDrawModel()
478 DBG_DTOR(SvxGalleryDrawModel
,NULL
);
481 // ------------------------------------------------------------------------
483 SgaObjectSvDraw::SgaObjectSvDraw( SvStream
& rIStm
, const INetURLObject
& rURL
)
485 SvxGalleryDrawModel aModel
;
487 if( aModel
.GetModel() )
489 if( GallerySvDrawImport( rIStm
, *aModel
.GetModel() ) )
492 bIsValid
= CreateThumb( *aModel
.GetModel() );
497 // ------------------------------------------------------------------------
499 sal_Bool
SgaObjectSvDraw::CreateThumb( const FmFormModel
& rModel
)
503 sal_Bool bRet
= sal_False
;
505 if ( CreateIMapGraphic( rModel
, aGraphic
, aImageMap
) )
507 bRet
= SgaObject::CreateThumb( aGraphic
);
511 const FmFormPage
* pPage
= static_cast< const FmFormPage
* >(rModel
.GetPage(0));
515 const Rectangle
aObjRect(pPage
->GetAllObjBoundRect());
517 if(aObjRect
.GetWidth() && aObjRect
.GetHeight())
520 FmFormView
aView(const_cast< FmFormModel
* >(&rModel
), &aVDev
);
522 aView
.ShowSdrPage(const_cast< FmFormPage
* >(pPage
));
524 aThumbBmp
= aView
.GetMarkedObjBitmapEx();
526 const Size
aDiscreteSize(aThumbBmp
.GetSizePixel());
528 if(aDiscreteSize
.Width() && aDiscreteSize
.Height())
530 sal_uInt32
nTargetSizeX(S_THUMB
);
531 sal_uInt32
nTargetSizeY(S_THUMB
);
533 if(aDiscreteSize
.Width() > aDiscreteSize
.Height())
535 nTargetSizeY
= (aDiscreteSize
.Height() * nTargetSizeX
) / aDiscreteSize
.Width();
539 nTargetSizeX
= (aDiscreteSize
.Width() * nTargetSizeY
) / aDiscreteSize
.Height();
544 aThumbBmp
.Scale(Size(nTargetSizeX
, nTargetSizeY
), BMP_SCALE_BESTQUALITY
);
545 aThumbBmp
.Convert(BMP_CONVERSION_8BIT_COLORS
);
556 // ------------------------------------------------------------------------
558 void SgaObjectSvDraw::WriteData( SvStream
& rOut
, const String
& rDestDir
) const
560 SgaObject::WriteData( rOut
, rDestDir
);
561 write_lenPrefixed_uInt8s_FromOUString
<sal_uInt16
>(rOut
, aTitle
, RTL_TEXTENCODING_UTF8
);
564 // ------------------------------------------------------------------------
566 void SgaObjectSvDraw::ReadData( SvStream
& rIn
, sal_uInt16
& rReadVersion
)
568 SgaObject::ReadData( rIn
, rReadVersion
);
570 if( rReadVersion
>= 5 )
571 aTitle
= read_lenPrefixed_uInt8s_ToOUString
<sal_uInt16
>(rIn
, RTL_TEXTENCODING_UTF8
);
574 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */