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