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 .
20 #include <com/sun/star/container/XIdentifierContainer.hpp>
21 #include <com/sun/star/container/XIndexContainer.hpp>
22 #include <com/sun/star/drawing/GluePoint2.hpp>
23 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
25 #include <cppuhelper/implbase.hxx>
26 #include <tools/weakbase.hxx>
28 #include <svx/svdmodel.hxx>
29 #include <svx/svdobj.hxx>
30 #include <svx/svdglue.hxx>
31 #include <svx/svdpage.hxx>
33 #include "gluepts.hxx"
35 using namespace ::com::sun::star
;
36 using namespace ::cppu
;
38 const sal_uInt16 NON_USER_DEFINED_GLUE_POINTS
= 4;
40 class SvxUnoGluePointAccess
: public WeakImplHelper
< container::XIndexContainer
, container::XIdentifierContainer
>
43 tools::WeakReference
<SdrObject
> mpObject
;
46 explicit SvxUnoGluePointAccess( SdrObject
* pObject
) throw();
48 // XIdentifierContainer
49 virtual sal_Int32 SAL_CALL
insert( const uno::Any
& aElement
) override
;
50 virtual void SAL_CALL
removeByIdentifier( sal_Int32 Identifier
) override
;
53 virtual void SAL_CALL
replaceByIdentifer( sal_Int32 Identifier
, const uno::Any
& aElement
) override
;
56 virtual uno::Any SAL_CALL
getByIdentifier( sal_Int32 Identifier
) override
;
57 virtual uno::Sequence
< sal_Int32
> SAL_CALL
getIdentifiers( ) override
;
61 virtual void SAL_CALL
insertByIndex( sal_Int32 Index
, const uno::Any
& Element
) override
;
62 virtual void SAL_CALL
removeByIndex( sal_Int32 Index
) override
;
66 virtual void SAL_CALL
replaceByIndex( sal_Int32 Index
, const uno::Any
& Element
) override
;
70 virtual sal_Int32 SAL_CALL
getCount( ) override
;
71 virtual uno::Any SAL_CALL
getByIndex( sal_Int32 Index
) override
;
74 virtual uno::Type SAL_CALL
getElementType( ) override
;
75 virtual sal_Bool SAL_CALL
hasElements( ) override
;
78 static void convert( const SdrGluePoint
& rSdrGlue
, drawing::GluePoint2
& rUnoGlue
) throw()
80 rUnoGlue
.Position
.X
= rSdrGlue
.GetPos().X();
81 rUnoGlue
.Position
.Y
= rSdrGlue
.GetPos().Y();
82 rUnoGlue
.IsRelative
= rSdrGlue
.IsPercent();
84 SdrAlign eAlign
= rSdrGlue
.GetAlign();
85 if (eAlign
== (SdrAlign::VERT_TOP
|SdrAlign::HORZ_LEFT
))
86 rUnoGlue
.PositionAlignment
= drawing::Alignment_TOP_LEFT
;
87 else if (eAlign
== (SdrAlign::HORZ_CENTER
|SdrAlign::VERT_TOP
))
88 rUnoGlue
.PositionAlignment
= drawing::Alignment_TOP
;
89 else if (eAlign
== (SdrAlign::VERT_TOP
|SdrAlign::HORZ_RIGHT
))
90 rUnoGlue
.PositionAlignment
= drawing::Alignment_TOP_RIGHT
;
91 else if (eAlign
== (SdrAlign::HORZ_CENTER
|SdrAlign::VERT_CENTER
))
92 rUnoGlue
.PositionAlignment
= drawing::Alignment_CENTER
;
93 else if (eAlign
== (SdrAlign::HORZ_RIGHT
|SdrAlign::VERT_CENTER
))
94 rUnoGlue
.PositionAlignment
= drawing::Alignment_RIGHT
;
95 else if (eAlign
== (SdrAlign::HORZ_LEFT
|SdrAlign::VERT_BOTTOM
))
96 rUnoGlue
.PositionAlignment
= drawing::Alignment_BOTTOM_LEFT
;
97 else if (eAlign
== (SdrAlign::HORZ_CENTER
|SdrAlign::VERT_BOTTOM
))
98 rUnoGlue
.PositionAlignment
= drawing::Alignment_BOTTOM
;
99 else if (eAlign
== (SdrAlign::HORZ_RIGHT
|SdrAlign::VERT_BOTTOM
))
100 rUnoGlue
.PositionAlignment
= drawing::Alignment_BOTTOM_RIGHT
;
102 rUnoGlue
.PositionAlignment
= drawing::Alignment_LEFT
;
105 switch( rSdrGlue
.GetEscDir() )
107 case SdrEscapeDirection::LEFT
:
108 rUnoGlue
.Escape
= drawing::EscapeDirection_LEFT
;
110 case SdrEscapeDirection::RIGHT
:
111 rUnoGlue
.Escape
= drawing::EscapeDirection_RIGHT
;
113 case SdrEscapeDirection::TOP
:
114 rUnoGlue
.Escape
= drawing::EscapeDirection_UP
;
116 case SdrEscapeDirection::BOTTOM
:
117 rUnoGlue
.Escape
= drawing::EscapeDirection_DOWN
;
119 case SdrEscapeDirection::HORZ
:
120 rUnoGlue
.Escape
= drawing::EscapeDirection_HORIZONTAL
;
122 case SdrEscapeDirection::VERT
:
123 rUnoGlue
.Escape
= drawing::EscapeDirection_VERTICAL
;
125 // case SdrEscapeDirection::SMART:
127 rUnoGlue
.Escape
= drawing::EscapeDirection_SMART
;
132 static void convert( const drawing::GluePoint2
& rUnoGlue
, SdrGluePoint
& rSdrGlue
) throw()
134 rSdrGlue
.SetPos( Point( rUnoGlue
.Position
.X
, rUnoGlue
.Position
.Y
) );
135 rSdrGlue
.SetPercent( rUnoGlue
.IsRelative
);
137 switch( rUnoGlue
.PositionAlignment
)
139 case drawing::Alignment_TOP_LEFT
:
140 rSdrGlue
.SetAlign( SdrAlign::VERT_TOP
|SdrAlign::HORZ_LEFT
);
142 case drawing::Alignment_TOP
:
143 rSdrGlue
.SetAlign( SdrAlign::HORZ_CENTER
|SdrAlign::VERT_TOP
);
145 case drawing::Alignment_TOP_RIGHT
:
146 rSdrGlue
.SetAlign( SdrAlign::VERT_TOP
|SdrAlign::HORZ_RIGHT
);
148 case drawing::Alignment_CENTER
:
149 rSdrGlue
.SetAlign( SdrAlign::HORZ_CENTER
|SdrAlign::VERT_CENTER
);
151 case drawing::Alignment_RIGHT
:
152 rSdrGlue
.SetAlign( SdrAlign::HORZ_RIGHT
|SdrAlign::VERT_CENTER
);
154 case drawing::Alignment_BOTTOM_LEFT
:
155 rSdrGlue
.SetAlign( SdrAlign::HORZ_LEFT
|SdrAlign::VERT_BOTTOM
);
157 case drawing::Alignment_BOTTOM
:
158 rSdrGlue
.SetAlign( SdrAlign::HORZ_CENTER
|SdrAlign::VERT_BOTTOM
);
160 case drawing::Alignment_BOTTOM_RIGHT
:
161 rSdrGlue
.SetAlign( SdrAlign::HORZ_RIGHT
|SdrAlign::VERT_BOTTOM
);
163 // case SdrAlign::HORZ_LEFT:
165 rSdrGlue
.SetAlign( SdrAlign::HORZ_LEFT
);
168 switch( rUnoGlue
.Escape
)
170 case drawing::EscapeDirection_LEFT
:
171 rSdrGlue
.SetEscDir(SdrEscapeDirection::LEFT
);
173 case drawing::EscapeDirection_RIGHT
:
174 rSdrGlue
.SetEscDir(SdrEscapeDirection::RIGHT
);
176 case drawing::EscapeDirection_UP
:
177 rSdrGlue
.SetEscDir(SdrEscapeDirection::TOP
);
179 case drawing::EscapeDirection_DOWN
:
180 rSdrGlue
.SetEscDir(SdrEscapeDirection::BOTTOM
);
182 case drawing::EscapeDirection_HORIZONTAL
:
183 rSdrGlue
.SetEscDir(SdrEscapeDirection::HORZ
);
185 case drawing::EscapeDirection_VERTICAL
:
186 rSdrGlue
.SetEscDir(SdrEscapeDirection::VERT
);
188 // case drawing::EscapeDirection_SMART:
190 rSdrGlue
.SetEscDir(SdrEscapeDirection::SMART
);
195 SvxUnoGluePointAccess::SvxUnoGluePointAccess( SdrObject
* pObject
) throw()
196 : mpObject( pObject
)
200 // XIdentifierContainer
201 sal_Int32 SAL_CALL
SvxUnoGluePointAccess::insert( const uno::Any
& aElement
)
205 SdrGluePointList
* pList
= mpObject
->ForceGluePointList();
208 // second, insert the new glue point
209 drawing::GluePoint2 aUnoGlue
;
211 if( aElement
>>= aUnoGlue
)
213 SdrGluePoint aSdrGlue
;
214 convert( aUnoGlue
, aSdrGlue
);
215 sal_uInt16 nId
= pList
->Insert( aSdrGlue
);
217 // only repaint, no objectchange
218 mpObject
->ActionChanged();
219 // mpObject->BroadcastObjectChange();
221 return static_cast<sal_Int32
>((*pList
)[nId
].GetId() + NON_USER_DEFINED_GLUE_POINTS
) - 1;
224 throw lang::IllegalArgumentException();
231 void SAL_CALL
SvxUnoGluePointAccess::removeByIdentifier( sal_Int32 Identifier
)
233 if( mpObject
.is() && ( Identifier
>= NON_USER_DEFINED_GLUE_POINTS
))
235 const sal_uInt16 nId
= static_cast<sal_uInt16
>(Identifier
- NON_USER_DEFINED_GLUE_POINTS
) + 1;
237 SdrGluePointList
* pList
= const_cast<SdrGluePointList
*>(mpObject
->GetGluePointList());
238 const sal_uInt16 nCount
= pList
? pList
->GetCount() : 0;
241 for( i
= 0; i
< nCount
; i
++ )
243 if( (*pList
)[i
].GetId() == nId
)
247 // only repaint, no objectchange
248 mpObject
->ActionChanged();
249 // mpObject->BroadcastObjectChange();
256 throw container::NoSuchElementException();
259 // XIdentifierReplace
260 void SAL_CALL
SvxUnoGluePointAccess::replaceByIdentifer( sal_Int32 Identifier
, const uno::Any
& aElement
)
264 struct drawing::GluePoint2 aGluePoint
;
265 if( (Identifier
< NON_USER_DEFINED_GLUE_POINTS
) || !(aElement
>>= aGluePoint
))
266 throw lang::IllegalArgumentException();
268 const sal_uInt16 nId
= static_cast<sal_uInt16
>( Identifier
- NON_USER_DEFINED_GLUE_POINTS
) + 1;
270 SdrGluePointList
* pList
= const_cast< SdrGluePointList
* >( mpObject
->GetGluePointList() );
271 const sal_uInt16 nCount
= pList
? pList
->GetCount() : 0;
273 for( i
= 0; i
< nCount
; i
++ )
275 if( (*pList
)[i
].GetId() == nId
)
277 // change the glue point
278 SdrGluePoint
& rTempPoint
= (*pList
)[i
];
279 convert( aGluePoint
, rTempPoint
);
281 // only repaint, no objectchange
282 mpObject
->ActionChanged();
283 // mpObject->BroadcastObjectChange();
289 throw container::NoSuchElementException();
294 uno::Any SAL_CALL
SvxUnoGluePointAccess::getByIdentifier( sal_Int32 Identifier
)
298 struct drawing::GluePoint2 aGluePoint
;
300 if( Identifier
< NON_USER_DEFINED_GLUE_POINTS
) // default glue point?
302 SdrGluePoint aTempPoint
= mpObject
->GetVertexGluePoint( static_cast<sal_uInt16
>(Identifier
) );
303 aGluePoint
.IsUserDefined
= false;
304 convert( aTempPoint
, aGluePoint
);
305 return uno::makeAny( aGluePoint
);
309 const sal_uInt16 nId
= static_cast<sal_uInt16
>( Identifier
- NON_USER_DEFINED_GLUE_POINTS
) + 1;
311 const SdrGluePointList
* pList
= mpObject
->GetGluePointList();
312 const sal_uInt16 nCount
= pList
? pList
->GetCount() : 0;
313 for( sal_uInt16 i
= 0; i
< nCount
; i
++ )
315 const SdrGluePoint
& rTempPoint
= (*pList
)[i
];
316 if( rTempPoint
.GetId() == nId
)
319 if(rTempPoint
.IsUserDefined())
321 aGluePoint
.IsUserDefined
= true;
324 convert( rTempPoint
, aGluePoint
);
325 return uno::makeAny( aGluePoint
);
331 throw container::NoSuchElementException();
334 uno::Sequence
< sal_Int32
> SAL_CALL
SvxUnoGluePointAccess::getIdentifiers()
338 const SdrGluePointList
* pList
= mpObject
->GetGluePointList();
339 const sal_uInt16 nCount
= pList
? pList
->GetCount() : 0;
343 uno::Sequence
< sal_Int32
> aIdSequence( nCount
+ NON_USER_DEFINED_GLUE_POINTS
);
344 sal_Int32
*pIdentifier
= aIdSequence
.getArray();
346 for( i
= 0; i
< NON_USER_DEFINED_GLUE_POINTS
; i
++ )
347 *pIdentifier
++ = static_cast<sal_Int32
>(i
);
349 for( i
= 0; i
< nCount
; i
++ )
350 *pIdentifier
++ = static_cast<sal_Int32
>( (*pList
)[i
].GetId() + NON_USER_DEFINED_GLUE_POINTS
) - 1;
356 uno::Sequence
< sal_Int32
> aEmpty
;
364 void SAL_CALL
SvxUnoGluePointAccess::insertByIndex( sal_Int32
, const uno::Any
& Element
)
368 SdrGluePointList
* pList
= mpObject
->ForceGluePointList();
371 SdrGluePoint aSdrGlue
;
372 drawing::GluePoint2 aUnoGlue
;
374 if( Element
>>= aUnoGlue
)
376 convert( aUnoGlue
, aSdrGlue
);
377 pList
->Insert( aSdrGlue
);
379 // only repaint, no objectchange
380 mpObject
->ActionChanged();
381 // mpObject->BroadcastObjectChange();
386 throw lang::IllegalArgumentException();
390 throw lang::IndexOutOfBoundsException();
393 void SAL_CALL
SvxUnoGluePointAccess::removeByIndex( sal_Int32 Index
)
397 SdrGluePointList
* pList
= mpObject
->ForceGluePointList();
401 if( Index
>= 0 && Index
< pList
->GetCount() )
403 pList
->Delete( static_cast<sal_uInt16
>(Index
) );
405 // only repaint, no objectchange
406 mpObject
->ActionChanged();
407 // mpObject->BroadcastObjectChange();
414 throw lang::IndexOutOfBoundsException();
418 void SAL_CALL
SvxUnoGluePointAccess::replaceByIndex( sal_Int32 Index
, const uno::Any
& Element
)
420 drawing::GluePoint2 aUnoGlue
;
421 if(!(Element
>>= aUnoGlue
))
422 throw lang::IllegalArgumentException();
425 if( mpObject
.is() && Index
>= 0 )
427 SdrGluePointList
* pList
= const_cast< SdrGluePointList
* >( mpObject
->GetGluePointList() );
428 if( pList
&& Index
< pList
->GetCount() )
430 SdrGluePoint
& rGlue
= (*pList
)[static_cast<sal_uInt16
>(Index
)];
431 convert( aUnoGlue
, rGlue
);
433 // only repaint, no objectchange
434 mpObject
->ActionChanged();
435 // mpObject->BroadcastObjectChange();
439 throw lang::IndexOutOfBoundsException();
443 sal_Int32 SAL_CALL
SvxUnoGluePointAccess::getCount()
445 sal_Int32 nCount
= 0;
448 // each node has a default of 4 glue points
449 // and any number of user defined glue points
452 const SdrGluePointList
* pList
= mpObject
->GetGluePointList();
454 nCount
+= pList
->GetCount();
460 uno::Any SAL_CALL
SvxUnoGluePointAccess::getByIndex( sal_Int32 Index
)
462 if( Index
>= 0 && mpObject
.is() )
464 struct drawing::GluePoint2 aGluePoint
;
466 if( Index
< 4 ) // default glue point?
468 SdrGluePoint aTempPoint
= mpObject
->GetVertexGluePoint( static_cast<sal_uInt16
>(Index
) );
469 aGluePoint
.IsUserDefined
= false;
470 convert( aTempPoint
, aGluePoint
);
471 return uno::Any(aGluePoint
);
476 const SdrGluePointList
* pList
= mpObject
->GetGluePointList();
477 if( pList
&& Index
< pList
->GetCount() )
479 const SdrGluePoint
& rTempPoint
= (*pList
)[static_cast<sal_uInt16
>(Index
)];
480 aGluePoint
.IsUserDefined
= true;
481 convert( rTempPoint
, aGluePoint
);
482 return uno::Any(aGluePoint
);
487 throw lang::IndexOutOfBoundsException();
491 uno::Type SAL_CALL
SvxUnoGluePointAccess::getElementType()
493 return cppu::UnoType
<drawing::GluePoint2
>::get();
496 sal_Bool SAL_CALL
SvxUnoGluePointAccess::hasElements()
498 return mpObject
.is();
502 * Create a SvxUnoGluePointAccess
504 uno::Reference
< uno::XInterface
> SvxUnoGluePointAccess_createInstance( SdrObject
* pObject
)
506 return *new SvxUnoGluePointAccess(pObject
);
509 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */