xref: /trunk/main/svtools/source/uno/unoimap.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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_svtools.hxx"
26 #include <com/sun/star/container/XNameContainer.hpp>
27 #include <com/sun/star/container/XIndexContainer.hpp>
28 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <com/sun/star/document/XEventsSupplier.hpp>
30 #include <com/sun/star/lang/XUnoTunnel.hpp>
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <com/sun/star/awt/Rectangle.hpp>
33 #include <com/sun/star/awt/Point.hpp>
34 #include <com/sun/star/drawing/PointSequence.hpp>
35 #include <comphelper/servicehelper.hxx>
36 #include <comphelper/propertysethelper.hxx>
37 #include <comphelper/propertysetinfo.hxx>
38 #include <cppuhelper/weakagg.hxx>
39 #include <cppuhelper/implbase3.hxx>
40 #include <list>
41 #include <rtl/uuid.h>
42 #include <vos/mutex.hxx>
43 #include <vcl/svapp.hxx>
44 #include <svtools/unoevent.hxx>
45 #include <svtools/unoimap.hxx>
46 #include <svtools/imap.hxx>
47 #include <svtools/imapcirc.hxx>
48 #include <svtools/imaprect.hxx>
49 #include <svtools/imappoly.hxx>
50 
51 #ifndef SEQTYPE
52  #if defined(__SUNPRO_CC) && (__SUNPRO_CC == 0x500)
53   #define SEQTYPE(x) (new ::com::sun::star::uno::Type( x ))
54  #else
55   #define SEQTYPE(x) &(x)
56  #endif
57 #endif
58 
59 #define MAP_LEN(x) x, sizeof(x)-1
60 
61 
62 using namespace comphelper;
63 using namespace cppu;
64 using namespace com::sun::star;
65 using namespace com::sun::star::uno;
66 using namespace com::sun::star::lang;
67 using namespace com::sun::star::container;
68 using namespace com::sun::star::beans;
69 using namespace com::sun::star::document;
70 using namespace com::sun::star::drawing;
71 
72 const sal_Int32 HANDLE_URL = 1;
73 const sal_Int32 HANDLE_DESCRIPTION = 2;
74 const sal_Int32 HANDLE_TARGET = 3;
75 const sal_Int32 HANDLE_NAME = 4;
76 const sal_Int32 HANDLE_ISACTIVE = 5;
77 const sal_Int32 HANDLE_POLYGON = 6;
78 const sal_Int32 HANDLE_CENTER = 7;
79 const sal_Int32 HANDLE_RADIUS = 8;
80 const sal_Int32 HANDLE_BOUNDARY = 9;
81 const sal_Int32 HANDLE_TITLE = 10;
82 
83 class SvUnoImageMapObject : public OWeakAggObject,
84                             public XEventsSupplier,
85                             public XServiceInfo,
86                             public PropertySetHelper,
87                             public XTypeProvider,
88                             public XUnoTunnel
89 {
90 public:
91     SvUnoImageMapObject( sal_uInt16 nType, const SvEventDescription* pSupportedMacroItems );
92     SvUnoImageMapObject( const IMapObject& rMapObject, const SvEventDescription* pSupportedMacroItems );
93     virtual ~SvUnoImageMapObject() throw();
94 
95     UNO3_GETIMPLEMENTATION_DECL( SvUnoImageMapObject )
96 
97     IMapObject* createIMapObject() const;
98 
99     SvMacroTableEventDescriptor* mpEvents;
100 
101     // overiden helpers from PropertySetHelper
102     virtual void _setPropertyValues( const PropertyMapEntry** ppEntries, const Any* pValues );
103     virtual void _getPropertyValues( const PropertyMapEntry** ppEntries, Any* pValue );
104 
105     // XInterface
106     virtual Any SAL_CALL queryAggregation( const Type & rType );
107     virtual Any SAL_CALL queryInterface( const Type & rType );
108     virtual void SAL_CALL acquire() throw();
109     virtual void SAL_CALL release() throw();
110 
111     // XTypeProvider
112     virtual Sequence< Type > SAL_CALL getTypes(  );
113     virtual Sequence< sal_Int8 > SAL_CALL getImplementationId(  );
114 
115     // XEventsSupplier
116     virtual Reference< ::com::sun::star::container::XNameReplace > SAL_CALL getEvents(  );
117 
118     // XServiceInfo
119     virtual ::rtl::OUString SAL_CALL getImplementationName(  );
120     virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName );
121     virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  );
122 
123 private:
124     static PropertySetInfo* createPropertySetInfo( sal_uInt16 nType );
125 
126 
127     sal_uInt16 mnType;
128 
129     ::rtl::OUString maURL;
130     ::rtl::OUString maAltText;
131     ::rtl::OUString maDesc;
132     ::rtl::OUString maTarget;
133     ::rtl::OUString maName;
134     sal_Bool mbIsActive;
135     awt::Rectangle maBoundary;
136     awt::Point maCenter;
137     sal_Int32 mnRadius;
138     PointSequence maPolygon;
139 };
140 
141 UNO3_GETIMPLEMENTATION_IMPL( SvUnoImageMapObject );
142 
createPropertySetInfo(sal_uInt16 nType)143 PropertySetInfo* SvUnoImageMapObject::createPropertySetInfo( sal_uInt16 nType )
144 {
145     switch( nType )
146     {
147     case IMAP_OBJ_POLYGON:
148         {
149             static PropertyMapEntry aPolygonObj_Impl[] =
150             {
151                 { MAP_LEN( "URL" ),         HANDLE_URL,         &::getCppuType((const ::rtl::OUString*)0),     0, 0 },
152                 { MAP_LEN( "Title" ),       HANDLE_TITLE,       &::getCppuType((const ::rtl::OUString*)0),     0, 0 },
153                 { MAP_LEN( "Description" ), HANDLE_DESCRIPTION, &::getCppuType((const ::rtl::OUString*)0),     0, 0 },
154                 { MAP_LEN( "Target" ),      HANDLE_TARGET,      &::getCppuType((const ::rtl::OUString*)0),     0, 0 },
155                 { MAP_LEN( "Name" ),        HANDLE_NAME,        &::getCppuType((const ::rtl::OUString*)0),     0, 0 },
156                 { MAP_LEN( "IsActive" ),    HANDLE_ISACTIVE,    &::getBooleanCppuType(),                0, 0 },
157                 { MAP_LEN( "Polygon" ),     HANDLE_POLYGON,     SEQTYPE(::getCppuType((const PointSequence*)0)),    0, 0 },
158                 {0,0,0,0,0,0}
159             };
160 
161             return new PropertySetInfo( aPolygonObj_Impl );
162         }
163     case IMAP_OBJ_CIRCLE:
164         {
165             static PropertyMapEntry aCircleObj_Impl[] =
166             {
167                 { MAP_LEN( "URL" ),         HANDLE_URL,         &::getCppuType((const ::rtl::OUString*)0),     0, 0 },
168                 { MAP_LEN( "Title" ),       HANDLE_TITLE,       &::getCppuType((const ::rtl::OUString*)0),     0, 0 },
169                 { MAP_LEN( "Description" ), HANDLE_DESCRIPTION, &::getCppuType((const ::rtl::OUString*)0),     0, 0 },
170                 { MAP_LEN( "Target" ),      HANDLE_TARGET,      &::getCppuType((const ::rtl::OUString*)0),     0, 0 },
171                 { MAP_LEN( "Name" ),            HANDLE_NAME,        &::getCppuType((const ::rtl::OUString*)0),     0, 0 },
172                 { MAP_LEN( "IsActive" ),        HANDLE_ISACTIVE,    &::getBooleanCppuType(),                0, 0 },
173                 { MAP_LEN( "Center" ),      HANDLE_CENTER,      &::getCppuType((const awt::Point*)0),   0, 0 },
174                 { MAP_LEN( "Radius" ),      HANDLE_RADIUS,      &::getCppuType((const sal_Int32*)0),    0, 0 },
175                 {0,0,0,0,0,0}
176             };
177 
178             return new PropertySetInfo( aCircleObj_Impl );
179         }
180     case IMAP_OBJ_RECTANGLE:
181     default:
182         {
183             static PropertyMapEntry aRectangleObj_Impl[] =
184             {
185                 { MAP_LEN( "URL" ),         HANDLE_URL,         &::getCppuType((const ::rtl::OUString*)0), 0, 0 },
186                 { MAP_LEN( "Title" ),       HANDLE_TITLE,       &::getCppuType((const ::rtl::OUString*)0),     0, 0 },
187                 { MAP_LEN( "Description" ), HANDLE_DESCRIPTION, &::getCppuType((const ::rtl::OUString*)0), 0, 0 },
188                 { MAP_LEN( "Target" ),      HANDLE_TARGET,      &::getCppuType((const ::rtl::OUString*)0), 0, 0 },
189                 { MAP_LEN( "Name" ),        HANDLE_NAME,        &::getCppuType((const ::rtl::OUString*)0), 0, 0 },
190                 { MAP_LEN( "IsActive" ),    HANDLE_ISACTIVE,    &::getBooleanCppuType(),            0, 0 },
191                 { MAP_LEN( "Boundary" ),    HANDLE_BOUNDARY,    &::getCppuType((const awt::Rectangle*)0),   0, 0 },
192                 {0,0,0,0,0,0}
193             };
194 
195             return new PropertySetInfo( aRectangleObj_Impl );
196         }
197     }
198 }
199 
SvUnoImageMapObject(sal_uInt16 nType,const SvEventDescription * pSupportedMacroItems)200 SvUnoImageMapObject::SvUnoImageMapObject( sal_uInt16 nType, const SvEventDescription* pSupportedMacroItems )
201 :   PropertySetHelper( createPropertySetInfo( nType ) ),
202     mnType( nType )
203 ,   mbIsActive( true )
204 ,   mnRadius( 0 )
205 {
206     mpEvents = new SvMacroTableEventDescriptor( pSupportedMacroItems );
207     mpEvents->acquire();
208 }
209 
SvUnoImageMapObject(const IMapObject & rMapObject,const SvEventDescription * pSupportedMacroItems)210 SvUnoImageMapObject::SvUnoImageMapObject( const IMapObject& rMapObject, const SvEventDescription* pSupportedMacroItems )
211 :   PropertySetHelper( createPropertySetInfo( rMapObject.GetType() ) ),
212     mnType( rMapObject.GetType() )
213 ,   mbIsActive( true )
214 ,   mnRadius( 0 )
215 {
216     maURL = rMapObject.GetURL();
217     maAltText = rMapObject.GetAltText();
218     maDesc = rMapObject.GetDesc();
219     maTarget = rMapObject.GetTarget();
220     maName = rMapObject.GetName();
221     mbIsActive = rMapObject.IsActive();
222 
223     switch( mnType )
224     {
225     case IMAP_OBJ_RECTANGLE:
226         {
227             const Rectangle aRect( ((IMapRectangleObject*)&rMapObject)->GetRectangle(sal_False) );
228             maBoundary.X = aRect.Left();
229             maBoundary.Y = aRect.Top();
230             maBoundary.Width = aRect.GetWidth();
231             maBoundary.Height = aRect.GetHeight();
232         }
233         break;
234     case IMAP_OBJ_CIRCLE:
235         {
236             mnRadius = (sal_Int32)((IMapCircleObject*)&rMapObject)->GetRadius(sal_False);
237             const Point aPoint( ((IMapCircleObject*)&rMapObject)->GetCenter(sal_False) );
238 
239             maCenter.X = aPoint.X();
240             maCenter.Y = aPoint.Y();
241         }
242         break;
243     case IMAP_OBJ_POLYGON:
244     default:
245         {
246             const Polygon aPoly( ((IMapPolygonObject*)&rMapObject)->GetPolygon(sal_False) );
247 
248             const sal_uInt16 nCount = aPoly.GetSize();
249             maPolygon.realloc( nCount );
250             awt::Point* pPoints = maPolygon.getArray();
251 
252             for( sal_uInt16 nPoint = 0; nPoint < nCount; nPoint++ )
253             {
254                 const Point& rPoint = aPoly.GetPoint( nPoint );
255                 pPoints->X = rPoint.X();
256                 pPoints->Y = rPoint.Y();
257 
258                 pPoints++;
259             }
260         }
261     }
262 
263     mpEvents = new SvMacroTableEventDescriptor( rMapObject.GetMacroTable(), pSupportedMacroItems );
264     mpEvents->acquire();
265 }
266 
~SvUnoImageMapObject()267 SvUnoImageMapObject::~SvUnoImageMapObject() throw()
268 {
269     mpEvents->release();
270 }
271 
createIMapObject() const272 IMapObject* SvUnoImageMapObject::createIMapObject() const
273 {
274     const String aURL( maURL );
275     const String aAltText( maAltText );
276     const String aDesc( maDesc );
277     const String aTarget( maTarget );
278     const String aName( maName );
279 
280     IMapObject* pNewIMapObject;
281 
282     switch( mnType )
283     {
284     case IMAP_OBJ_RECTANGLE:
285         {
286             const Rectangle aRect( maBoundary.X, maBoundary.Y, maBoundary.X + maBoundary.Width - 1, maBoundary.Y + maBoundary.Height - 1 );
287             pNewIMapObject = new IMapRectangleObject( aRect, aURL, aAltText, aDesc, aTarget, aName, mbIsActive, sal_False );
288         }
289         break;
290 
291     case IMAP_OBJ_CIRCLE:
292         {
293             const Point aCenter( maCenter.X, maCenter.Y );
294             pNewIMapObject = new IMapCircleObject( aCenter, mnRadius, aURL, aAltText, aDesc, aTarget, aName, mbIsActive, sal_False );
295         }
296         break;
297 
298     case IMAP_OBJ_POLYGON:
299     default:
300         {
301             const sal_uInt16 nCount = (sal_uInt16)maPolygon.getLength();
302 
303             Polygon aPoly( nCount );
304             for( sal_uInt16 nPoint = 0; nPoint < nCount; nPoint++ )
305             {
306                 Point aPoint( maPolygon[nPoint].X, maPolygon[nPoint].Y );
307                 aPoly.SetPoint( aPoint, nPoint );
308             }
309 
310             aPoly.Optimize( POLY_OPTIMIZE_CLOSE );
311             pNewIMapObject = new IMapPolygonObject( aPoly, aURL, aAltText, aDesc, aTarget, aName, mbIsActive, sal_False );
312         }
313         break;
314     }
315 
316     SvxMacroTableDtor aMacroTable;
317     mpEvents->copyMacrosIntoTable(aMacroTable);
318     pNewIMapObject->SetMacroTable( aMacroTable );
319 
320     return pNewIMapObject;
321 }
322 
323 // XInterface
324 
queryInterface(const Type & rType)325 Any SAL_CALL SvUnoImageMapObject::queryInterface( const Type & rType )
326 {
327     return OWeakAggObject::queryInterface( rType );
328 }
329 
queryAggregation(const Type & rType)330 Any SAL_CALL SvUnoImageMapObject::queryAggregation( const Type & rType )
331 {
332     Any aAny;
333 
334     if( rType == ::getCppuType((const Reference< XServiceInfo >*)0) )
335         aAny <<= Reference< XServiceInfo >(this);
336     else if( rType == ::getCppuType((const Reference< XTypeProvider >*)0) )
337         aAny <<= Reference< XTypeProvider >(this);
338     else if( rType == ::getCppuType((const Reference< XPropertySet >*)0) )
339         aAny <<= Reference< XPropertySet >(this);
340     else if( rType == ::getCppuType((const Reference< XEventsSupplier >*)0) )
341         aAny <<= Reference< XEventsSupplier >(this);
342     else if( rType == ::getCppuType((const Reference< XMultiPropertySet >*)0) )
343         aAny <<= Reference< XMultiPropertySet >(this);
344     else if( rType == ::getCppuType((const Reference< XUnoTunnel >*)0) )
345         aAny <<= Reference< XUnoTunnel >(this);
346     else
347         aAny <<= OWeakAggObject::queryAggregation( rType );
348 
349     return aAny;
350 }
351 
acquire()352 void SAL_CALL SvUnoImageMapObject::acquire() throw()
353 {
354     OWeakAggObject::acquire();
355 }
356 
release()357 void SAL_CALL SvUnoImageMapObject::release() throw()
358 {
359     OWeakAggObject::release();
360 }
361 
getTypes()362 uno::Sequence< uno::Type > SAL_CALL SvUnoImageMapObject::getTypes()
363 {
364     uno::Sequence< uno::Type > aTypes( 7 );
365     uno::Type* pTypes = aTypes.getArray();
366 
367     *pTypes++ = ::getCppuType((const uno::Reference< XAggregation>*)0);
368     *pTypes++ = ::getCppuType((const uno::Reference< XEventsSupplier>*)0);
369     *pTypes++ = ::getCppuType((const uno::Reference< XServiceInfo>*)0);
370     *pTypes++ = ::getCppuType((const uno::Reference< XPropertySet>*)0);
371     *pTypes++ = ::getCppuType((const uno::Reference< XMultiPropertySet>*)0);
372     *pTypes++ = ::getCppuType((const uno::Reference< XTypeProvider>*)0);
373     *pTypes++ = ::getCppuType((const uno::Reference< XUnoTunnel>*)0);
374 
375     return aTypes;
376 }
377 
getImplementationId()378 uno::Sequence< sal_Int8 > SAL_CALL SvUnoImageMapObject::getImplementationId()
379 {
380     vos::OGuard aGuard( Application::GetSolarMutex() );
381 
382     static uno::Sequence< sal_Int8 > aId;
383     if( aId.getLength() == 0 )
384     {
385         aId.realloc( 16 );
386         rtl_createUuid( (sal_uInt8 *)aId.getArray(), 0, sal_True );
387     }
388     return aId;
389 }
390 
391 // XServiceInfo
392 
supportsService(const::rtl::OUString & ServiceName)393 sal_Bool SAL_CALL SvUnoImageMapObject::supportsService( const  ::rtl::OUString& ServiceName )
394 {
395     const Sequence< ::rtl::OUString > aSNL( getSupportedServiceNames() );
396     const ::rtl::OUString * pArray = aSNL.getConstArray();
397 
398     const sal_Int32 nCount = aSNL.getLength();
399     for( sal_Int32 i = 0; i < nCount; i++ )
400         if( pArray[i] == ServiceName )
401             return sal_True;
402 
403     return sal_False;
404 }
405 
getSupportedServiceNames()406 Sequence< ::rtl::OUString > SAL_CALL SvUnoImageMapObject::getSupportedServiceNames()
407 {
408     Sequence< ::rtl::OUString > aSNS( 2 );
409     aSNS.getArray()[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.image.ImageMapObject" ));
410     switch( mnType )
411     {
412     case IMAP_OBJ_POLYGON:
413     default:
414         aSNS.getArray()[1] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.image.ImageMapPolygonObject" ));
415         break;
416     case IMAP_OBJ_RECTANGLE:
417         aSNS.getArray()[1] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.image.ImageMapRectangleObject" ));
418         break;
419     case IMAP_OBJ_CIRCLE:
420         aSNS.getArray()[1] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.image.ImageMapCircleObject" ));
421         break;
422     }
423     return aSNS;
424 }
425 
getImplementationName()426 ::rtl::OUString SAL_CALL SvUnoImageMapObject::getImplementationName()
427 {
428     switch( mnType )
429     {
430     case IMAP_OBJ_POLYGON:
431     default:
432         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("org.openoffice.comp.svt.ImageMapPolygonObject") );
433     case IMAP_OBJ_CIRCLE:
434         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("org.openoffice.comp.svt.ImageMapCircleObject") );
435     case IMAP_OBJ_RECTANGLE:
436         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("org.openoffice.comp.svt.ImageMapRectangleObject") );
437     }
438 }
439 
440 // overiden helpers from PropertySetHelper
_setPropertyValues(const PropertyMapEntry ** ppEntries,const Any * pValues)441 void SvUnoImageMapObject::_setPropertyValues( const PropertyMapEntry** ppEntries, const Any* pValues )
442 {
443     sal_Bool bOk = sal_False;
444 
445     while( *ppEntries )
446     {
447         switch( (*ppEntries)->mnHandle )
448         {
449         case HANDLE_URL:
450             bOk = *pValues >>= maURL;
451             break;
452         case HANDLE_TITLE:
453             bOk = *pValues >>= maAltText;
454             break;
455         case HANDLE_DESCRIPTION:
456             bOk = *pValues >>= maDesc;
457             break;
458         case HANDLE_TARGET:
459             bOk = *pValues >>= maTarget;
460             break;
461         case HANDLE_NAME:
462             bOk = *pValues >>= maName;
463             break;
464         case HANDLE_ISACTIVE:
465             bOk = *pValues >>= mbIsActive;
466             break;
467         case HANDLE_BOUNDARY:
468             bOk = *pValues >>= maBoundary;
469             break;
470         case HANDLE_CENTER:
471             bOk = *pValues >>= maCenter;
472             break;
473         case HANDLE_RADIUS:
474             bOk = *pValues >>= mnRadius;
475             break;
476         case HANDLE_POLYGON:
477             bOk = *pValues >>= maPolygon;
478             break;
479         default:
480             DBG_ERROR( "SvUnoImageMapObject::_setPropertyValues: unexpected property handle" );
481             break;
482         }
483 
484         if( !bOk )
485             throw IllegalArgumentException();
486 
487         ppEntries++;
488         pValues++;
489     }
490 }
491 
_getPropertyValues(const PropertyMapEntry ** ppEntries,Any * pValues)492 void SvUnoImageMapObject::_getPropertyValues( const PropertyMapEntry** ppEntries, Any* pValues )
493 {
494     while( *ppEntries )
495     {
496         switch( (*ppEntries)->mnHandle )
497         {
498         case HANDLE_URL:
499             *pValues <<= maURL;
500             break;
501         case HANDLE_TITLE:
502             *pValues <<= maAltText;
503             break;
504         case HANDLE_DESCRIPTION:
505             *pValues <<= maDesc;
506             break;
507         case HANDLE_TARGET:
508             *pValues <<= maTarget;
509             break;
510         case HANDLE_NAME:
511             *pValues <<= maName;
512             break;
513         case HANDLE_ISACTIVE:
514             *pValues <<= mbIsActive;
515             break;
516         case HANDLE_BOUNDARY:
517             *pValues <<= maBoundary;
518             break;
519         case HANDLE_CENTER:
520             *pValues <<= maCenter;
521             break;
522         case HANDLE_RADIUS:
523             *pValues <<= mnRadius;
524             break;
525         case HANDLE_POLYGON:
526             *pValues <<= maPolygon;
527             break;
528         default:
529             DBG_ERROR( "SvUnoImageMapObject::_getPropertyValues: unexpected property handle" );
530             break;
531         }
532 
533         ppEntries++;
534         pValues++;
535     }
536 }
537 
538 
getEvents()539 Reference< XNameReplace > SAL_CALL SvUnoImageMapObject::getEvents()
540 {
541     // try weak reference first
542     Reference< XNameReplace > xEvents( mpEvents );
543     return xEvents;
544 }
545 
546 ///////////////////////////////////////////////////////////////////////
547 
548 class SvUnoImageMap : public WeakImplHelper3< XIndexContainer, XServiceInfo, XUnoTunnel >
549 {
550 public:
551     SvUnoImageMap( const SvEventDescription* pSupportedMacroItems );
552     SvUnoImageMap( const ImageMap& rMap, const SvEventDescription* pSupportedMacroItems );
553     virtual ~SvUnoImageMap();
554 
555     sal_Bool fillImageMap( ImageMap& rMap ) const;
556     SvUnoImageMapObject* getObject( const Any& aElement ) const;
557 
558     UNO3_GETIMPLEMENTATION_DECL( SvUnoImageMap )
559 
560     // XIndexContainer
561     virtual void SAL_CALL insertByIndex( sal_Int32 Index, const Any& Element );
562     virtual void SAL_CALL removeByIndex( sal_Int32 Index );
563 
564     // XIndexReplace
565     virtual void SAL_CALL replaceByIndex( sal_Int32 Index, const Any& Element );
566 
567     // XIndexAccess
568     virtual sal_Int32 SAL_CALL getCount(  );
569     virtual Any SAL_CALL getByIndex( sal_Int32 Index );
570 
571     // XElementAccess
572     virtual Type SAL_CALL getElementType(  );
573     virtual sal_Bool SAL_CALL hasElements(  );
574 
575     // XSerivceInfo
576     virtual ::rtl::OUString SAL_CALL getImplementationName(  );
577     virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName );
578     virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  );
579 
580 private:
581     ::rtl::OUString maName;
582 
583     std::list< SvUnoImageMapObject* > maObjectList;
584 };
585 
586 UNO3_GETIMPLEMENTATION_IMPL( SvUnoImageMap );
587 
SvUnoImageMap(const SvEventDescription *)588 SvUnoImageMap::SvUnoImageMap( const SvEventDescription* )
589 {
590 }
591 
SvUnoImageMap(const ImageMap & rMap,const SvEventDescription * pSupportedMacroItems)592 SvUnoImageMap::SvUnoImageMap( const ImageMap& rMap, const SvEventDescription* pSupportedMacroItems )
593 {
594     maName = rMap.GetName();
595 
596     const sal_uInt16 nCount = rMap.GetIMapObjectCount();
597     for( sal_uInt16 nPos = 0; nPos < nCount; nPos++ )
598     {
599         IMapObject* pMapObject = rMap.GetIMapObject( nPos );
600         SvUnoImageMapObject* pUnoObj = new SvUnoImageMapObject( *pMapObject, pSupportedMacroItems );
601         pUnoObj->acquire();
602         maObjectList.push_back( pUnoObj );
603     }
604 }
605 
~SvUnoImageMap()606 SvUnoImageMap::~SvUnoImageMap()
607 {
608     std::list< SvUnoImageMapObject* >::iterator aIter = maObjectList.begin();
609     const std::list< SvUnoImageMapObject* >::iterator aEnd = maObjectList.end();
610     while( aIter != aEnd )
611     {
612         (*aIter++)->release();
613     }
614 }
615 
getObject(const Any & aElement) const616 SvUnoImageMapObject* SvUnoImageMap::getObject( const Any& aElement ) const
617 {
618     Reference< XInterface > xObject;
619     aElement >>= xObject;
620 
621     SvUnoImageMapObject* pObject = SvUnoImageMapObject::getImplementation( xObject );
622     if( NULL == pObject )
623         throw IllegalArgumentException();
624 
625     return pObject;
626 }
627 
628 // XIndexContainer
insertByIndex(sal_Int32 Index,const Any & Element)629 void SAL_CALL SvUnoImageMap::insertByIndex( sal_Int32 Index, const Any& Element )
630 {
631     SvUnoImageMapObject* pObject = getObject( Element );
632     const sal_Int32 nCount = maObjectList.size();
633     if( NULL == pObject || Index > nCount )
634         throw IndexOutOfBoundsException();
635 
636     pObject->acquire();
637 
638     if( Index == nCount )
639         maObjectList.push_back( pObject );
640     else
641     {
642         std::list< SvUnoImageMapObject* >::iterator aIter = maObjectList.begin();
643         for( sal_Int32 n = 0; n < Index; n++ )
644             aIter++;
645 
646         maObjectList.insert( aIter, pObject );
647     }
648 }
649 
removeByIndex(sal_Int32 Index)650 void SAL_CALL SvUnoImageMap::removeByIndex( sal_Int32 Index )
651 {
652     const sal_Int32 nCount = maObjectList.size();
653     if( Index >= nCount )
654         throw IndexOutOfBoundsException();
655 
656     if( nCount - 1 == Index )
657     {
658         maObjectList.back()->release();
659         maObjectList.pop_back();
660     }
661     else
662     {
663         std::list< SvUnoImageMapObject* >::iterator aIter = maObjectList.begin();
664         for( sal_Int32 n = 0; n < Index; n++ )
665             aIter++;
666 
667         (*aIter)->release();
668         maObjectList.erase( aIter );
669     }
670 }
671 
672 // XIndexReplace
replaceByIndex(sal_Int32 Index,const Any & Element)673 void SAL_CALL SvUnoImageMap::replaceByIndex( sal_Int32 Index, const Any& Element )
674 {
675     SvUnoImageMapObject* pObject = getObject( Element );
676     const sal_Int32 nCount = maObjectList.size();
677     if( NULL == pObject || Index >= nCount )
678         throw IndexOutOfBoundsException();
679 
680     std::list< SvUnoImageMapObject* >::iterator aIter = maObjectList.begin();
681     for( sal_Int32 n = 0; n < Index; n++ )
682         aIter++;
683 
684     (*aIter)->release();
685     *aIter = pObject;
686     pObject->acquire();
687 }
688 
689 // XIndexAccess
getCount()690 sal_Int32 SAL_CALL SvUnoImageMap::getCount(  )
691 {
692     return maObjectList.size();
693 }
694 
getByIndex(sal_Int32 Index)695 Any SAL_CALL SvUnoImageMap::getByIndex( sal_Int32 Index )
696 {
697     const sal_Int32 nCount = maObjectList.size();
698     if( Index >= nCount )
699         throw IndexOutOfBoundsException();
700 
701     std::list< SvUnoImageMapObject* >::iterator aIter = maObjectList.begin();
702     for( sal_Int32 n = 0; n < Index; n++ )
703         aIter++;
704 
705     Reference< XPropertySet > xObj( *aIter );
706     return makeAny( xObj );
707 }
708 
709 // XElementAccess
getElementType()710 Type SAL_CALL SvUnoImageMap::getElementType(  )
711 {
712     return ::getCppuType((const Reference< XPropertySet >*)0);
713 }
714 
hasElements()715 sal_Bool SAL_CALL SvUnoImageMap::hasElements(  )
716 {
717     return maObjectList.size() != 0;
718 }
719 
720 // XSerivceInfo
getImplementationName()721 ::rtl::OUString SAL_CALL SvUnoImageMap::getImplementationName(  )
722 {
723     return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.comp.svt.SvUnoImageMap" ) );
724 }
725 
supportsService(const::rtl::OUString & ServiceName)726 sal_Bool SAL_CALL SvUnoImageMap::supportsService( const ::rtl::OUString& ServiceName )
727 {
728     const Sequence< ::rtl::OUString > aSNL( getSupportedServiceNames() );
729     const ::rtl::OUString * pArray = aSNL.getConstArray();
730 
731     const sal_Int32 nCount = aSNL.getLength();
732     for( sal_Int32 i = 0; i < nCount; i++ )
733         if( pArray[i] == ServiceName )
734             return sal_True;
735 
736     return sal_False;
737 }
738 
getSupportedServiceNames()739 Sequence< ::rtl::OUString > SAL_CALL SvUnoImageMap::getSupportedServiceNames(  )
740 {
741     const ::rtl::OUString aSN( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.image.ImageMap" ) );
742     return Sequence< ::rtl::OUString >( &aSN, 1 );
743 }
744 
fillImageMap(ImageMap & rMap) const745 sal_Bool SvUnoImageMap::fillImageMap( ImageMap& rMap ) const
746 {
747     rMap.ClearImageMap();
748 
749     rMap.SetName( maName );
750 
751     std::list< SvUnoImageMapObject* >::const_iterator aIter = maObjectList.begin();
752     const std::list< SvUnoImageMapObject* >::const_iterator aEnd = maObjectList.end();
753     while( aIter != aEnd )
754     {
755         IMapObject* pNewMapObject = (*aIter)->createIMapObject();
756         rMap.InsertIMapObject( *pNewMapObject );
757         delete pNewMapObject;
758 
759         aIter++;
760     }
761 
762     return sal_True;
763 }
764 
765 // -------------------------------------------------------------------
766 // factory helper methods
767 // -------------------------------------------------------------------
768 
SvUnoImageMapRectangleObject_createInstance(const SvEventDescription * pSupportedMacroItems)769 Reference< XInterface > SvUnoImageMapRectangleObject_createInstance( const SvEventDescription* pSupportedMacroItems )
770 {
771     return (XWeak*)new SvUnoImageMapObject( IMAP_OBJ_RECTANGLE, pSupportedMacroItems );
772 }
773 
SvUnoImageMapCircleObject_createInstance(const SvEventDescription * pSupportedMacroItems)774 Reference< XInterface > SvUnoImageMapCircleObject_createInstance( const SvEventDescription* pSupportedMacroItems )
775 {
776     return (XWeak*)new SvUnoImageMapObject( IMAP_OBJ_CIRCLE, pSupportedMacroItems );
777 }
778 
SvUnoImageMapPolygonObject_createInstance(const SvEventDescription * pSupportedMacroItems)779 Reference< XInterface > SvUnoImageMapPolygonObject_createInstance( const SvEventDescription* pSupportedMacroItems )
780 {
781     return (XWeak*)new SvUnoImageMapObject( IMAP_OBJ_POLYGON, pSupportedMacroItems );
782 }
783 
SvUnoImageMap_createInstance(const SvEventDescription * pSupportedMacroItems)784 Reference< XInterface > SvUnoImageMap_createInstance( const SvEventDescription* pSupportedMacroItems )
785 {
786     return (XWeak*)new SvUnoImageMap( pSupportedMacroItems );
787 }
788 
SvUnoImageMap_createInstance(const ImageMap & rMap,const SvEventDescription * pSupportedMacroItems)789 Reference< XInterface > SvUnoImageMap_createInstance( const ImageMap& rMap, const SvEventDescription* pSupportedMacroItems )
790 {
791     return (XWeak*)new SvUnoImageMap( rMap, pSupportedMacroItems );
792 }
793 
SvUnoImageMap_fillImageMap(Reference<XInterface> xImageMap,ImageMap & rMap)794 sal_Bool SvUnoImageMap_fillImageMap( Reference< XInterface > xImageMap, ImageMap& rMap )
795 {
796     SvUnoImageMap* pUnoImageMap = SvUnoImageMap::getImplementation( xImageMap );
797     if( NULL == pUnoImageMap )
798         return sal_False;
799 
800     return pUnoImageMap->fillImageMap( rMap );
801 }
802