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/NoSuchElementException.hpp>
21 #include <com/sun/star/container/XIdentifierContainer.hpp>
22 #include <com/sun/star/container/XIndexContainer.hpp>
23 #include <com/sun/star/drawing/GluePoint2.hpp>
24 #include <com/sun/star/lang/IllegalArgumentException.hpp>
25 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
27 #include <cppuhelper/implbase.hxx>
28 #include <unotools/weakref.hxx>
30 #include <svx/svdobj.hxx>
31 #include <svx/svdglue.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;
42 class SvxUnoGluePointAccess
: public WeakImplHelper
< container::XIndexContainer
, container::XIdentifierContainer
>
45 unotools::WeakReference
<SdrObject
> mpObject
;
48 explicit SvxUnoGluePointAccess( SdrObject
* pObject
) noexcept
;
50 // XIdentifierContainer
51 virtual sal_Int32 SAL_CALL
insert( const uno::Any
& aElement
) override
;
52 virtual void SAL_CALL
removeByIdentifier( sal_Int32 Identifier
) override
;
55 virtual void SAL_CALL
replaceByIdentifer( sal_Int32 Identifier
, const uno::Any
& aElement
) override
;
58 virtual uno::Any SAL_CALL
getByIdentifier( sal_Int32 Identifier
) override
;
59 virtual uno::Sequence
< sal_Int32
> SAL_CALL
getIdentifiers( ) override
;
63 virtual void SAL_CALL
insertByIndex( sal_Int32 Index
, const uno::Any
& Element
) override
;
64 virtual void SAL_CALL
removeByIndex( sal_Int32 Index
) override
;
68 virtual void SAL_CALL
replaceByIndex( sal_Int32 Index
, const uno::Any
& Element
) override
;
72 virtual sal_Int32 SAL_CALL
getCount( ) override
;
73 virtual uno::Any SAL_CALL
getByIndex( sal_Int32 Index
) override
;
76 virtual uno::Type SAL_CALL
getElementType( ) override
;
77 virtual sal_Bool SAL_CALL
hasElements( ) override
;
82 static void convert( const SdrGluePoint
& rSdrGlue
, drawing::GluePoint2
& rUnoGlue
) noexcept
84 rUnoGlue
.Position
.X
= rSdrGlue
.GetPos().X();
85 rUnoGlue
.Position
.Y
= rSdrGlue
.GetPos().Y();
86 rUnoGlue
.IsRelative
= rSdrGlue
.IsPercent();
88 SdrAlign eAlign
= rSdrGlue
.GetAlign();
89 if (eAlign
== (SdrAlign::VERT_TOP
|SdrAlign::HORZ_LEFT
))
90 rUnoGlue
.PositionAlignment
= drawing::Alignment_TOP_LEFT
;
91 else if (eAlign
== (SdrAlign::HORZ_CENTER
|SdrAlign::VERT_TOP
))
92 rUnoGlue
.PositionAlignment
= drawing::Alignment_TOP
;
93 else if (eAlign
== (SdrAlign::VERT_TOP
|SdrAlign::HORZ_RIGHT
))
94 rUnoGlue
.PositionAlignment
= drawing::Alignment_TOP_RIGHT
;
95 else if (eAlign
== (SdrAlign::HORZ_CENTER
|SdrAlign::VERT_CENTER
))
96 rUnoGlue
.PositionAlignment
= drawing::Alignment_CENTER
;
97 else if (eAlign
== (SdrAlign::HORZ_RIGHT
|SdrAlign::VERT_CENTER
))
98 rUnoGlue
.PositionAlignment
= drawing::Alignment_RIGHT
;
99 else if (eAlign
== (SdrAlign::HORZ_LEFT
|SdrAlign::VERT_BOTTOM
))
100 rUnoGlue
.PositionAlignment
= drawing::Alignment_BOTTOM_LEFT
;
101 else if (eAlign
== (SdrAlign::HORZ_CENTER
|SdrAlign::VERT_BOTTOM
))
102 rUnoGlue
.PositionAlignment
= drawing::Alignment_BOTTOM
;
103 else if (eAlign
== (SdrAlign::HORZ_RIGHT
|SdrAlign::VERT_BOTTOM
))
104 rUnoGlue
.PositionAlignment
= drawing::Alignment_BOTTOM_RIGHT
;
106 rUnoGlue
.PositionAlignment
= drawing::Alignment_LEFT
;
109 switch( rSdrGlue
.GetEscDir() )
111 case SdrEscapeDirection::LEFT
:
112 rUnoGlue
.Escape
= drawing::EscapeDirection_LEFT
;
114 case SdrEscapeDirection::RIGHT
:
115 rUnoGlue
.Escape
= drawing::EscapeDirection_RIGHT
;
117 case SdrEscapeDirection::TOP
:
118 rUnoGlue
.Escape
= drawing::EscapeDirection_UP
;
120 case SdrEscapeDirection::BOTTOM
:
121 rUnoGlue
.Escape
= drawing::EscapeDirection_DOWN
;
123 case SdrEscapeDirection::HORZ
:
124 rUnoGlue
.Escape
= drawing::EscapeDirection_HORIZONTAL
;
126 case SdrEscapeDirection::VERT
:
127 rUnoGlue
.Escape
= drawing::EscapeDirection_VERTICAL
;
129 // case SdrEscapeDirection::SMART:
131 rUnoGlue
.Escape
= drawing::EscapeDirection_SMART
;
136 static void convert( const drawing::GluePoint2
& rUnoGlue
, SdrGluePoint
& rSdrGlue
) noexcept
138 rSdrGlue
.SetPos( Point( rUnoGlue
.Position
.X
, rUnoGlue
.Position
.Y
) );
139 rSdrGlue
.SetPercent( rUnoGlue
.IsRelative
);
141 switch( rUnoGlue
.PositionAlignment
)
143 case drawing::Alignment_TOP_LEFT
:
144 rSdrGlue
.SetAlign( SdrAlign::VERT_TOP
|SdrAlign::HORZ_LEFT
);
146 case drawing::Alignment_TOP
:
147 rSdrGlue
.SetAlign( SdrAlign::HORZ_CENTER
|SdrAlign::VERT_TOP
);
149 case drawing::Alignment_TOP_RIGHT
:
150 rSdrGlue
.SetAlign( SdrAlign::VERT_TOP
|SdrAlign::HORZ_RIGHT
);
152 case drawing::Alignment_CENTER
:
153 rSdrGlue
.SetAlign( SdrAlign::HORZ_CENTER
|SdrAlign::VERT_CENTER
);
155 case drawing::Alignment_RIGHT
:
156 rSdrGlue
.SetAlign( SdrAlign::HORZ_RIGHT
|SdrAlign::VERT_CENTER
);
158 case drawing::Alignment_BOTTOM_LEFT
:
159 rSdrGlue
.SetAlign( SdrAlign::HORZ_LEFT
|SdrAlign::VERT_BOTTOM
);
161 case drawing::Alignment_BOTTOM
:
162 rSdrGlue
.SetAlign( SdrAlign::HORZ_CENTER
|SdrAlign::VERT_BOTTOM
);
164 case drawing::Alignment_BOTTOM_RIGHT
:
165 rSdrGlue
.SetAlign( SdrAlign::HORZ_RIGHT
|SdrAlign::VERT_BOTTOM
);
167 // case SdrAlign::HORZ_LEFT:
169 rSdrGlue
.SetAlign( SdrAlign::HORZ_LEFT
);
172 switch( rUnoGlue
.Escape
)
174 case drawing::EscapeDirection_LEFT
:
175 rSdrGlue
.SetEscDir(SdrEscapeDirection::LEFT
);
177 case drawing::EscapeDirection_RIGHT
:
178 rSdrGlue
.SetEscDir(SdrEscapeDirection::RIGHT
);
180 case drawing::EscapeDirection_UP
:
181 rSdrGlue
.SetEscDir(SdrEscapeDirection::TOP
);
183 case drawing::EscapeDirection_DOWN
:
184 rSdrGlue
.SetEscDir(SdrEscapeDirection::BOTTOM
);
186 case drawing::EscapeDirection_HORIZONTAL
:
187 rSdrGlue
.SetEscDir(SdrEscapeDirection::HORZ
);
189 case drawing::EscapeDirection_VERTICAL
:
190 rSdrGlue
.SetEscDir(SdrEscapeDirection::VERT
);
192 // case drawing::EscapeDirection_SMART:
194 rSdrGlue
.SetEscDir(SdrEscapeDirection::SMART
);
199 SvxUnoGluePointAccess::SvxUnoGluePointAccess( SdrObject
* pObject
) noexcept
200 : mpObject( pObject
)
204 // XIdentifierContainer
205 sal_Int32 SAL_CALL
SvxUnoGluePointAccess::insert( const uno::Any
& aElement
)
207 if( auto pObject
= mpObject
.get() )
209 SdrGluePointList
* pList
= pObject
->ForceGluePointList();
212 // second, insert the new gluepoint
213 drawing::GluePoint2 aUnoGlue
;
215 if( aElement
>>= aUnoGlue
)
217 SdrGluePoint aSdrGlue
;
218 convert( aUnoGlue
, aSdrGlue
);
219 sal_uInt16 nId
= pList
->Insert( aSdrGlue
);
221 // only repaint, no objectchange
222 pObject
->ActionChanged();
223 // mpObject->BroadcastObjectChange();
225 return static_cast<sal_Int32
>((*pList
)[nId
].GetId() + NON_USER_DEFINED_GLUE_POINTS
) - 1;
228 throw lang::IllegalArgumentException();
235 void SAL_CALL
SvxUnoGluePointAccess::removeByIdentifier( sal_Int32 Identifier
)
237 auto pObject
= mpObject
.get();
238 if( pObject
&& ( Identifier
>= NON_USER_DEFINED_GLUE_POINTS
))
240 const sal_uInt16 nId
= static_cast<sal_uInt16
>(Identifier
- NON_USER_DEFINED_GLUE_POINTS
) + 1;
242 SdrGluePointList
* pList
= const_cast<SdrGluePointList
*>(pObject
->GetGluePointList());
243 const sal_uInt16 nCount
= pList
? pList
->GetCount() : 0;
246 for( i
= 0; i
< nCount
; i
++ )
248 if( (*pList
)[i
].GetId() == nId
)
252 // only repaint, no objectchange
253 pObject
->ActionChanged();
254 // pObject->BroadcastObjectChange();
261 throw container::NoSuchElementException();
264 // XIdentifierReplace
265 void SAL_CALL
SvxUnoGluePointAccess::replaceByIdentifer( sal_Int32 Identifier
, const uno::Any
& aElement
)
267 auto pObject
= mpObject
.get();
271 struct drawing::GluePoint2 aGluePoint
;
272 if( (Identifier
< NON_USER_DEFINED_GLUE_POINTS
) || !(aElement
>>= aGluePoint
))
273 throw lang::IllegalArgumentException();
275 const sal_uInt16 nId
= static_cast<sal_uInt16
>( Identifier
- NON_USER_DEFINED_GLUE_POINTS
) + 1;
277 SdrGluePointList
* pList
= const_cast< SdrGluePointList
* >( pObject
->GetGluePointList() );
278 const sal_uInt16 nCount
= pList
? pList
->GetCount() : 0;
280 for( i
= 0; i
< nCount
; i
++ )
282 if( (*pList
)[i
].GetId() == nId
)
284 // change the gluepoint
285 SdrGluePoint
& rTempPoint
= (*pList
)[i
];
286 convert( aGluePoint
, rTempPoint
);
288 // only repaint, no objectchange
289 pObject
->ActionChanged();
290 // pObject->BroadcastObjectChange();
296 throw container::NoSuchElementException();
300 uno::Any SAL_CALL
SvxUnoGluePointAccess::getByIdentifier( sal_Int32 Identifier
)
302 auto pObject
= mpObject
.get();
305 struct drawing::GluePoint2 aGluePoint
;
307 if( Identifier
< NON_USER_DEFINED_GLUE_POINTS
) // default gluepoint?
309 SdrGluePoint aTempPoint
= pObject
->GetVertexGluePoint( static_cast<sal_uInt16
>(Identifier
) );
310 aGluePoint
.IsUserDefined
= false;
311 convert( aTempPoint
, aGluePoint
);
312 return uno::Any( aGluePoint
);
316 const sal_uInt16 nId
= static_cast<sal_uInt16
>( Identifier
- NON_USER_DEFINED_GLUE_POINTS
) + 1;
318 const SdrGluePointList
* pList
= pObject
->GetGluePointList();
319 const sal_uInt16 nCount
= pList
? pList
->GetCount() : 0;
320 for( sal_uInt16 i
= 0; i
< nCount
; i
++ )
322 const SdrGluePoint
& rTempPoint
= (*pList
)[i
];
323 if( rTempPoint
.GetId() == nId
)
326 if(rTempPoint
.IsUserDefined())
328 aGluePoint
.IsUserDefined
= true;
331 convert( rTempPoint
, aGluePoint
);
332 return uno::Any( aGluePoint
);
338 throw container::NoSuchElementException();
341 uno::Sequence
< sal_Int32
> SAL_CALL
SvxUnoGluePointAccess::getIdentifiers()
343 auto pObject
= mpObject
.get();
346 const SdrGluePointList
* pList
= pObject
->GetGluePointList();
347 const sal_uInt16 nCount
= pList
? pList
->GetCount() : 0;
351 uno::Sequence
< sal_Int32
> aIdSequence( nCount
+ NON_USER_DEFINED_GLUE_POINTS
);
352 sal_Int32
*pIdentifier
= aIdSequence
.getArray();
354 for( i
= 0; i
< NON_USER_DEFINED_GLUE_POINTS
; i
++ )
355 *pIdentifier
++ = static_cast<sal_Int32
>(i
);
357 for( i
= 0; i
< nCount
; i
++ )
358 *pIdentifier
++ = static_cast<sal_Int32
>( (*pList
)[i
].GetId() + NON_USER_DEFINED_GLUE_POINTS
) - 1;
364 uno::Sequence
< sal_Int32
> aEmpty
;
372 void SAL_CALL
SvxUnoGluePointAccess::insertByIndex( sal_Int32
, const uno::Any
& Element
)
374 auto pObject
= mpObject
.get();
377 SdrGluePointList
* pList
= pObject
->ForceGluePointList();
380 drawing::GluePoint2 aUnoGlue
;
382 if( Element
>>= aUnoGlue
)
384 SdrGluePoint aSdrGlue
;
385 convert( aUnoGlue
, aSdrGlue
);
386 pList
->Insert( aSdrGlue
);
388 // only repaint, no objectchange
389 pObject
->ActionChanged();
390 // pObject->BroadcastObjectChange();
395 throw lang::IllegalArgumentException();
399 throw lang::IndexOutOfBoundsException();
402 void SAL_CALL
SvxUnoGluePointAccess::removeByIndex( sal_Int32 Index
)
404 auto pObject
= mpObject
.get();
407 SdrGluePointList
* pList
= pObject
->ForceGluePointList();
411 if( Index
>= 0 && Index
< pList
->GetCount() )
413 pList
->Delete( static_cast<sal_uInt16
>(Index
) );
415 // only repaint, no objectchange
416 pObject
->ActionChanged();
417 // pObject->BroadcastObjectChange();
424 throw lang::IndexOutOfBoundsException();
428 void SAL_CALL
SvxUnoGluePointAccess::replaceByIndex( sal_Int32 Index
, const uno::Any
& Element
)
430 drawing::GluePoint2 aUnoGlue
;
431 if(!(Element
>>= aUnoGlue
))
432 throw lang::IllegalArgumentException();
434 auto pObject
= mpObject
.get();
436 if( pObject
&& Index
>= 0 )
438 SdrGluePointList
* pList
= const_cast< SdrGluePointList
* >( pObject
->GetGluePointList() );
439 if( pList
&& Index
< pList
->GetCount() )
441 SdrGluePoint
& rGlue
= (*pList
)[static_cast<sal_uInt16
>(Index
)];
442 convert( aUnoGlue
, rGlue
);
444 // only repaint, no objectchange
445 pObject
->ActionChanged();
446 // pObject->BroadcastObjectChange();
450 throw lang::IndexOutOfBoundsException();
454 sal_Int32 SAL_CALL
SvxUnoGluePointAccess::getCount()
456 auto pObject
= mpObject
.get();
457 sal_Int32 nCount
= 0;
460 // each node has a default of 4 gluepoints
461 // and any number of user defined gluepoints
464 const SdrGluePointList
* pList
= pObject
->GetGluePointList();
466 nCount
+= pList
->GetCount();
472 uno::Any SAL_CALL
SvxUnoGluePointAccess::getByIndex( sal_Int32 Index
)
474 auto pObject
= mpObject
.get();
475 if( Index
>= 0 && pObject
)
477 struct drawing::GluePoint2 aGluePoint
;
479 if( Index
< 4 ) // default gluepoint?
481 SdrGluePoint aTempPoint
= pObject
->GetVertexGluePoint( static_cast<sal_uInt16
>(Index
) );
482 aGluePoint
.IsUserDefined
= false;
483 convert( aTempPoint
, aGluePoint
);
484 return uno::Any(aGluePoint
);
489 const SdrGluePointList
* pList
= pObject
->GetGluePointList();
490 if( pList
&& Index
< pList
->GetCount() )
492 const SdrGluePoint
& rTempPoint
= (*pList
)[static_cast<sal_uInt16
>(Index
)];
493 aGluePoint
.IsUserDefined
= true;
494 convert( rTempPoint
, aGluePoint
);
495 return uno::Any(aGluePoint
);
500 throw lang::IndexOutOfBoundsException();
504 uno::Type SAL_CALL
SvxUnoGluePointAccess::getElementType()
506 return cppu::UnoType
<drawing::GluePoint2
>::get();
509 sal_Bool SAL_CALL
SvxUnoGluePointAccess::hasElements()
511 return bool(mpObject
.get());
515 * Create a SvxUnoGluePointAccess
517 uno::Reference
< uno::XInterface
> SvxUnoGluePointAccess_createInstance( SdrObject
* pObject
)
519 return *new SvxUnoGluePointAccess(pObject
);
522 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */