xref: /trunk/main/svx/source/gallery2/galobj.cxx (revision 45fd3b9a)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svx.hxx"
26 
27 #define ENABLE_BYTESTRING_STREAM_OPERATORS
28 
29 #include <com/sun/star/lang/XUnoTunnel.hpp>
30 #include <sfx2/objsh.hxx>
31 #include <sfx2/docfac.hxx>
32 #include <comphelper/classids.hxx>
33 #include <unotools/pathoptions.hxx>
34 #include <tools/rcid.h>
35 #include <tools/vcompat.hxx>
36 #include <vcl/virdev.hxx>
37 #include <svl/itempool.hxx>
38 #include <svx/fmmodel.hxx>
39 #include <svx/fmview.hxx>
40 #include <svx/fmpage.hxx>
41 #include "gallery.hrc"
42 #include "svx/galmisc.hxx"
43 #include "galobj.hxx"
44 #include <vcl/salbtype.hxx>		// FRound
45 #include <vcl/svapp.hxx>
46 #include <vcl/dibtools.hxx>
47 #include "gallerydrawmodel.hxx"
48 
49 using namespace ::com::sun::star;
50 
51 // -------------
52 // - SgaObject -
53 // -------------
54 
55 SgaObject::SgaObject() :
56 		bIsValid    ( sal_False ),
57 		bIsThumbBmp	( sal_True )
58 {
59 }
60 
61 // ------------------------------------------------------------------------
62 
63 sal_Bool SgaObject::CreateThumb( const Graphic& rGraphic )
64 {
65 	sal_Bool bRet = sal_False;
66 
67 	if( rGraphic.GetType() == GRAPHIC_BITMAP )
68 	{
69 		BitmapEx    aBmpEx( rGraphic.GetBitmapEx() );
70 		Size        aBmpSize( aBmpEx.GetSizePixel() );
71 
72 		if( aBmpSize.Width() && aBmpSize.Height() )
73 		{
74 			const Color	aWhite( COL_WHITE );
75 
76 		    if( aBmpEx.GetPrefMapMode().GetMapUnit() != MAP_PIXEL &&
77 		        aBmpEx.GetPrefSize().Width() > 0 &&
78 		        aBmpEx.GetPrefSize().Height() > 0 )
79 		    {
80 		        Size aLogSize( OutputDevice::LogicToLogic( aBmpEx.GetPrefSize(), aBmpEx.GetPrefMapMode(), MAP_100TH_MM ) );
81 
82 		        if( aLogSize.Width() > 0 && aLogSize.Height() > 0 )
83 		        {
84 		            double  fFactorLog = static_cast< double >( aLogSize.Width() ) / aLogSize.Height();
85 		            double  fFactorPix = static_cast< double >( aBmpSize.Width() ) / aBmpSize.Height();
86 
87 		            if( fFactorPix > fFactorLog )
88 		                aBmpSize.Width() = FRound( aBmpSize.Height() * fFactorLog );
89 		            else
90 		                aBmpSize.Height() = FRound( aBmpSize.Width() / fFactorLog );
91 
92 		            aBmpEx.SetSizePixel( aBmpSize, BMP_SCALE_BESTQUALITY );
93 		        }
94 		    }
95 
96             // take over BitmapEx
97 			aThumbBmp = aBmpEx;
98 
99 			if( ( aBmpSize.Width() <= S_THUMB ) && ( aBmpSize.Height() <= S_THUMB ) )
100 			{
101 				aThumbBmp.Convert( BMP_CONVERSION_8BIT_COLORS );
102 				bRet = sal_True;
103 			}
104 			else
105 			{
106 				const float	fFactor  = (float) aBmpSize.Width() / aBmpSize.Height();
107 				const Size	aNewSize( Max( (long) (fFactor < 1. ? S_THUMB * fFactor : S_THUMB), 8L ),
108 									  Max( (long) (fFactor < 1. ? S_THUMB : S_THUMB / fFactor), 8L ) );
109 
110 				if(aThumbBmp.Scale(
111                     (double) aNewSize.Width() / aBmpSize.Width(),
112                     (double) aNewSize.Height() / aBmpSize.Height(),
113                     BMP_SCALE_BESTQUALITY ) )
114 				{
115 					aThumbBmp.Convert( BMP_CONVERSION_8BIT_COLORS );
116 					bRet = sal_True;
117 				}
118 			}
119 		}
120 	}
121 	else if( rGraphic.GetType() == GRAPHIC_GDIMETAFILE )
122 	{
123 		const Size aPrefSize( rGraphic.GetPrefSize() );
124 		const double fFactor  = (double)aPrefSize.Width() / (double)aPrefSize.Height();
125         Size aSize( S_THUMB, S_THUMB );
126 		if ( fFactor < 1.0 )
127 			aSize.Width() = (sal_Int32)( S_THUMB * fFactor );
128 		else
129 			aSize.Height() = (sal_Int32)( S_THUMB / fFactor );
130 
131         const GraphicConversionParameters aParameters(aSize, false, true, true, true);
132         aThumbBmp = rGraphic.GetBitmapEx(aParameters);
133 
134 		if( !aThumbBmp.IsEmpty() )
135 		{
136 			aThumbBmp.Convert( BMP_CONVERSION_8BIT_COLORS );
137 			bRet = sal_True;
138 		}
139 	}
140 
141 	return bRet;
142 }
143 
144 // ------------------------------------------------------------------------
145 
146 void SgaObject::WriteData( SvStream& rOut, const String& rDestDir ) const
147 {
148 	static const sal_uInt32 nInventor = COMPAT_FORMAT( 'S', 'G', 'A', '3' );
149 
150 	rOut << nInventor << (sal_uInt16) 0x0004 << GetVersion() << (sal_uInt16) GetObjKind();
151 	rOut << bIsThumbBmp;
152 
153 	if( bIsThumbBmp )
154 	{
155 		const sal_uInt16	nOldCompressMode = rOut.GetCompressMode();
156 		const sal_uIntPtr		nOldVersion = rOut.GetVersion();
157 
158 		rOut.SetCompressMode( COMPRESSMODE_ZBITMAP );
159 		rOut.SetVersion( SOFFICE_FILEFORMAT_50 );
160 
161         WriteDIBBitmapEx(aThumbBmp, rOut);
162 
163 		rOut.SetVersion( nOldVersion );
164 		rOut.SetCompressMode( nOldCompressMode );
165 	}
166 	else
167 		rOut << aThumbMtf;
168 
169 	String aURLWithoutDestDir = String(aURL.GetMainURL( INetURLObject::NO_DECODE ));
170 	aURLWithoutDestDir.SearchAndReplace(rDestDir, String());
171 	rOut << ByteString( aURLWithoutDestDir, RTL_TEXTENCODING_UTF8 );
172 }
173 
174 // ------------------------------------------------------------------------
175 
176 void SgaObject::ReadData(SvStream& rIn, sal_uInt16& rReadVersion )
177 {
178 	ByteString	aTmpStr;
179 	sal_uInt32		nTmp32;
180 	sal_uInt16		nTmp16;
181 
182 	rIn >> nTmp32 >> nTmp16 >> rReadVersion >> nTmp16 >> bIsThumbBmp;
183 
184 	if( bIsThumbBmp )
185     {
186         ReadDIBBitmapEx(aThumbBmp, rIn);
187     }
188 	else
189     {
190 		rIn >> aThumbMtf;
191     }
192 
193 	rIn >> aTmpStr; aURL = INetURLObject( String( aTmpStr.GetBuffer(), RTL_TEXTENCODING_UTF8 ) );
194 }
195 
196 // ------------------------------------------------------------------------
197 
198 const String SgaObject::GetTitle() const
199 {
200 	String aReturnValue( aTitle );
201 	if ( !getenv( "GALLERY_SHOW_PRIVATE_TITLE" ) )
202 	{
203 		if ( aReturnValue.GetTokenCount( ':' ) == 3 )
204 		{
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 ) )
210 			{
211 				ByteString aMgrName( aResourceName, RTL_TEXTENCODING_UTF8 );
212 				ResMgr* pResMgr = ResMgr::CreateResMgr( aMgrName.GetBuffer(),
213 							Application::GetSettings().GetUILocale() );
214 				if ( pResMgr )
215 				{
216 					ResId aResId( (sal_uInt16)nResId, *pResMgr );
217 					aResId.SetRT( RSC_STRING );
218 					if ( pResMgr->IsAvailable( aResId ) )
219 					{
220 						aReturnValue = String( aResId );
221 					}
222 					delete pResMgr;
223 				}
224 			}
225 		}
226 	}
227 	return aReturnValue;
228 }
229 
230 // ------------------------------------------------------------------------
231 
232 void SgaObject::SetTitle( const String& rTitle )
233 {
234 	aTitle = rTitle;
235 }
236 
237 // ------------------------------------------------------------------------
238 
239 SvStream& operator<<( SvStream& rOut, const SgaObject& rObj )
240 {
241 	rObj.WriteData( rOut, String() );
242 	return rOut;
243 }
244 
245 // ------------------------------------------------------------------------
246 
247 SvStream& operator>>( SvStream& rIn, SgaObject& rObj )
248 {
249 	sal_uInt16 nReadVersion;
250 
251 	rObj.ReadData( rIn, nReadVersion );
252 	rObj.bIsValid = ( rIn.GetError() == ERRCODE_NONE );
253 
254 	return rIn;
255 }
256 
257 // ----------------
258 // - SgaObjectBmp -
259 // ----------------
260 
261 SgaObjectBmp::SgaObjectBmp()
262 {
263 }
264 
265 // ------------------------------------------------------------------------
266 
267 SgaObjectBmp::SgaObjectBmp( const INetURLObject& rURL )
268 {
269 	Graphic aGraphic;
270 	String	aFilter;
271 
272 	if ( SGA_IMPORT_NONE != GalleryGraphicImport( rURL, aGraphic, aFilter ) )
273 		Init( aGraphic, rURL );
274 }
275 
276 // ------------------------------------------------------------------------
277 
278 SgaObjectBmp::SgaObjectBmp( const Graphic& rGraphic, const INetURLObject& rURL, const String& )
279 {
280 	if( FileExists( rURL ) )
281 		Init( rGraphic, rURL );
282 }
283 
284 // ------------------------------------------------------------------------
285 
286 void SgaObjectBmp::Init( const Graphic& rGraphic, const INetURLObject& rURL )
287 {
288 	aURL = rURL;
289 	bIsValid = CreateThumb( rGraphic );
290 }
291 
292 // ------------------------------------------------------------------------
293 
294 void SgaObjectBmp::WriteData( SvStream& rOut, const String& rDestDir ) const
295 {
296 	String	aDummyStr;
297 	char	aDummy[ 10 ];
298 
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 );
303 }
304 
305 // ------------------------------------------------------------------------
306 
307 void SgaObjectBmp::ReadData( SvStream& rIn, sal_uInt16& rReadVersion )
308 {
309 	ByteString aTmpStr;
310 
311 	SgaObject::ReadData( rIn, rReadVersion );
312 	rIn.SeekRel( 10 ); // 16, 16, 32, 16
313 	rIn >> aTmpStr; // dummy
314 
315 	if( rReadVersion >= 5 )
316 	{
317 		rIn >> aTmpStr; aTitle = String( aTmpStr.GetBuffer(), RTL_TEXTENCODING_UTF8 );
318 	}
319 }
320 
321 // ------------------
322 // - SgaObjectSound -
323 // ------------------
324 DBG_NAME(SgaObjectSound)
325 
326 SgaObjectSound::SgaObjectSound() :
327 	eSoundType( SOUND_STANDARD )
328 {
329     DBG_CTOR(SgaObjectSound,NULL);
330 
331 }
332 
333 // ------------------------------------------------------------------------
334 
335 SgaObjectSound::SgaObjectSound( const INetURLObject& rURL ) :
336 	eSoundType( SOUND_STANDARD )
337 {
338     DBG_CTOR(SgaObjectSound,NULL);
339 
340 	if( FileExists( rURL ) )
341 	{
342 		aURL = rURL;
343 		aThumbBmp = Bitmap( Size( 1, 1 ), 1 );
344 		bIsValid = sal_True;
345 	}
346 	else
347 		bIsValid = sal_False;
348 }
349 
350 // ------------------------------------------------------------------------
351 
352 SgaObjectSound::~SgaObjectSound()
353 {
354 
355     DBG_DTOR(SgaObjectSound,NULL);
356 }
357 
358 // ------------------------------------------------------------------------
359 
360 BitmapEx SgaObjectSound::GetThumbBmp() const
361 {
362 	sal_uInt16 nId;
363 
364 	switch( eSoundType )
365 	{
366 		case( SOUND_COMPUTER ): nId = RID_SVXBMP_GALLERY_SOUND_1; break;
367 		case( SOUND_MISC ): nId = RID_SVXBMP_GALLERY_SOUND_2; break;
368 		case( SOUND_MUSIC ): nId = RID_SVXBMP_GALLERY_SOUND_3; break;
369 		case( SOUND_NATURE ): nId = RID_SVXBMP_GALLERY_SOUND_4; break;
370 		case( SOUND_SPEECH ): nId = RID_SVXBMP_GALLERY_SOUND_5; break;
371 		case( SOUND_TECHNIC ): nId = RID_SVXBMP_GALLERY_SOUND_6; break;
372 		case( SOUND_ANIMAL ): nId = RID_SVXBMP_GALLERY_SOUND_7; break;
373 
374 		// standard
375 		default:
376 			 nId = RID_SVXBMP_GALLERY_MEDIA;
377 		break;
378 	}
379 
380 	const BitmapEx  aBmpEx( GAL_RESID( nId ) );
381 
382     return aBmpEx;
383 }
384 
385 // ------------------------------------------------------------------------
386 
387 void SgaObjectSound::WriteData( SvStream& rOut, const String& rDestDir ) const
388 {
389 	SgaObject::WriteData( rOut, rDestDir );
390 	rOut << (sal_uInt16) eSoundType << ByteString( aTitle, RTL_TEXTENCODING_UTF8 );
391 }
392 
393 // ------------------------------------------------------------------------
394 
395 void SgaObjectSound::ReadData( SvStream& rIn, sal_uInt16& rReadVersion )
396 {
397 	SgaObject::ReadData( rIn, rReadVersion );
398 
399 	if( rReadVersion >= 5 )
400 	{
401 		ByteString	aTmpStr;
402 		sal_uInt16		nTmp16;
403 
404 		rIn >> nTmp16; eSoundType = (GalSoundType) nTmp16;
405 
406 		if( rReadVersion >= 6 )
407 		{
408 			rIn >> aTmpStr; aTitle = String( aTmpStr.GetBuffer(), RTL_TEXTENCODING_UTF8 );
409 		}
410 	}
411 }
412 
413 // -----------------
414 // - SgaObjectAnim -
415 // -----------------
416 
417 SgaObjectAnim::SgaObjectAnim()
418 {
419 }
420 
421 // ------------------------------------------------------------------------
422 
423 SgaObjectAnim::SgaObjectAnim( const Graphic& rGraphic,
424 							  const INetURLObject& rURL,
425 							  const String& )
426 {
427 	aURL = rURL;
428 	bIsValid = CreateThumb( rGraphic );
429 }
430 
431 // -----------------
432 // - SgaObjectINet -
433 // -----------------
434 
435 SgaObjectINet::SgaObjectINet()
436 {
437 }
438 
439 // ------------------------------------------------------------------------
440 
441 SgaObjectINet::SgaObjectINet( const Graphic& rGraphic, const INetURLObject& rURL, const String& rFormatName ) :
442 			SgaObjectAnim	( rGraphic, rURL, rFormatName )
443 {
444 }
445 
446 // -------------------
447 // - SgaObjectSvDraw -
448 // -------------------
449 
450 SgaObjectSvDraw::SgaObjectSvDraw()
451 {
452 }
453 
454 // ------------------------------------------------------------------------
455 
456 SgaObjectSvDraw::SgaObjectSvDraw( const FmFormModel& rModel, const INetURLObject& rURL )
457 {
458 	aURL = rURL;
459 	bIsValid = CreateThumb( rModel );
460 }
461 
462 // ------------------------------------------------------------------------
463 DBG_NAME(SvxGalleryDrawModel)
464 
465 SvxGalleryDrawModel::SvxGalleryDrawModel()
466 : mpFormModel( 0 )
467 {
468     DBG_CTOR(SvxGalleryDrawModel,NULL);
469 
470     const String sFactoryURL(RTL_CONSTASCII_USTRINGPARAM("sdraw"));
471 
472     mxDoc = SfxObjectShell::CreateObjectByFactoryName( sFactoryURL );
473 
474     if( mxDoc.Is() )
475     {
476         mxDoc->DoInitNew(0);
477 
478         uno::Reference< lang::XUnoTunnel > xTunnel( mxDoc->GetModel(), uno::UNO_QUERY );
479         if( xTunnel.is() )
480         {
481             mpFormModel = dynamic_cast< FmFormModel* >(
482                 reinterpret_cast<SdrModel*>(xTunnel->getSomething(SdrModel::getUnoTunnelImplementationId())));
483             if( mpFormModel )
484             {
485                 mpFormModel->InsertPage( mpFormModel->AllocPage( false ) );
486             }
487         }
488     }
489 }
490 
491 // ------------------------------------------------------------------------
492 
493 SvxGalleryDrawModel::~SvxGalleryDrawModel()
494 {
495     if( mxDoc.Is() )
496         mxDoc->DoClose();
497 
498     DBG_DTOR(SvxGalleryDrawModel,NULL);
499 }
500 
501 // ------------------------------------------------------------------------
502 
503 SgaObjectSvDraw::SgaObjectSvDraw( SvStream& rIStm, const INetURLObject& rURL )
504 {
505     SvxGalleryDrawModel aModel;
506 
507     if( aModel.GetModel() )
508     {
509         if( GallerySvDrawImport( rIStm, *aModel.GetModel()  ) )
510         {
511             aURL = rURL;
512             bIsValid = CreateThumb( *aModel.GetModel()  );
513         }
514     }
515 }
516 
517 // ------------------------------------------------------------------------
518 
519 sal_Bool SgaObjectSvDraw::CreateThumb( const FmFormModel& rModel )
520 {
521 	Graphic		aGraphic;
522 	ImageMap	aImageMap;
523 	sal_Bool		bRet = sal_False;
524 
525 	if ( CreateIMapGraphic( rModel, aGraphic, aImageMap ) )
526     {
527 		bRet = SgaObject::CreateThumb( aGraphic );
528     }
529 	else
530 	{
531         const FmFormPage* pPage = static_cast< const FmFormPage* >(rModel.GetPage(0));
532 
533         if(pPage)
534         {
535             const Rectangle aObjRect(pPage->GetAllObjBoundRect());
536 
537             if(aObjRect.GetWidth() && aObjRect.GetHeight())
538             {
539                 VirtualDevice aVDev;
540                 FmFormView aView(const_cast< FmFormModel* >(&rModel), &aVDev);
541 
542                 aView.ShowSdrPage(const_cast< FmFormPage* >(pPage));
543                 aView.MarkAllObj();
544                 aThumbBmp = aView.GetMarkedObjBitmapEx();
545 
546                 const Size aDiscreteSize(aThumbBmp.GetSizePixel());
547 
548                 if(aDiscreteSize.Width() && aDiscreteSize.Height())
549                 {
550                     sal_uInt32 nTargetSizeX(S_THUMB);
551                     sal_uInt32 nTargetSizeY(S_THUMB);
552 
553                     if(aDiscreteSize.Width() > aDiscreteSize.Height())
554                     {
555                         nTargetSizeY = (aDiscreteSize.Height() * nTargetSizeX) / aDiscreteSize.Width();
556                     }
557                     else
558                     {
559                         nTargetSizeX = (aDiscreteSize.Width() * nTargetSizeY) / aDiscreteSize.Height();
560                     }
561 
562                     if(!!aThumbBmp)
563                     {
564                         aThumbBmp.Scale(Size(nTargetSizeX, nTargetSizeY), BMP_SCALE_BESTQUALITY);
565                         aThumbBmp.Convert(BMP_CONVERSION_8BIT_COLORS);
566                         bRet = true;
567                     }
568                 }
569             }
570         }
571 	}
572 
573 	return bRet;
574 }
575 
576 // ------------------------------------------------------------------------
577 
578 void SgaObjectSvDraw::WriteData( SvStream& rOut, const String& rDestDir ) const
579 {
580 	SgaObject::WriteData( rOut, rDestDir );
581 	rOut << ByteString( aTitle, RTL_TEXTENCODING_UTF8 );
582 }
583 
584 // ------------------------------------------------------------------------
585 
586 void SgaObjectSvDraw::ReadData( SvStream& rIn, sal_uInt16& rReadVersion )
587 {
588 	SgaObject::ReadData( rIn, rReadVersion );
589 
590 	if( rReadVersion >= 5 )
591 	{
592 		ByteString aTmpStr;
593 		rIn >> aTmpStr; aTitle = String( aTmpStr.GetBuffer(), RTL_TEXTENCODING_UTF8 );
594 	}
595 }
596 
597 // eof
598