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>
24 #include <cppuhelper/implbase2.hxx>
26 #include <svx/svdmodel.hxx>
27 #include <svx/svdobj.hxx>
28 #include <svx/svdglue.hxx>
29 #include <svx/svdpage.hxx>
31 using namespace ::com::sun::star
;
32 using namespace ::rtl
;
33 using namespace ::cppu
;
35 const sal_uInt16 NON_USER_DEFINED_GLUE_POINTS
= 4;
37 class SvxUnoGluePointAccess
: public WeakImplHelper2
< container::XIndexContainer
, container::XIdentifierContainer
>
40 SdrObjectWeakRef mpObject
;
43 SvxUnoGluePointAccess( SdrObject
* pObject
) throw();
44 virtual ~SvxUnoGluePointAccess() throw();
46 // XIdentifierContainer
47 virtual sal_Int32 SAL_CALL
insert( const uno::Any
& aElement
) throw (lang::IllegalArgumentException
, lang::WrappedTargetException
, uno::RuntimeException
);
48 virtual void SAL_CALL
removeByIdentifier( sal_Int32 Identifier
) throw (container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
);
51 virtual void SAL_CALL
replaceByIdentifer( sal_Int32 Identifier
, const uno::Any
& aElement
) throw (lang::IllegalArgumentException
, container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
);
54 virtual uno::Any SAL_CALL
getByIdentifier( sal_Int32 Identifier
) throw (container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
);
55 virtual uno::Sequence
< sal_Int32
> SAL_CALL
getIdentifiers( ) throw (uno::RuntimeException
);
59 virtual void SAL_CALL
insertByIndex( sal_Int32 Index
, const uno::Any
& Element
) throw(lang::IllegalArgumentException
, lang::IndexOutOfBoundsException
, lang::WrappedTargetException
, uno::RuntimeException
);
60 virtual void SAL_CALL
removeByIndex( sal_Int32 Index
) throw(lang::IndexOutOfBoundsException
, lang::WrappedTargetException
, uno::RuntimeException
);
64 virtual void SAL_CALL
replaceByIndex( sal_Int32 Index
, const uno::Any
& Element
) throw(lang::IllegalArgumentException
, lang::IndexOutOfBoundsException
, lang::WrappedTargetException
, uno::RuntimeException
);
68 virtual sal_Int32 SAL_CALL
getCount( ) throw(uno::RuntimeException
);
69 virtual uno::Any SAL_CALL
getByIndex( sal_Int32 Index
) throw(lang::IndexOutOfBoundsException
, lang::WrappedTargetException
, uno::RuntimeException
);
72 virtual uno::Type SAL_CALL
getElementType( ) throw( uno::RuntimeException
);
73 virtual sal_Bool SAL_CALL
hasElements( ) throw( uno::RuntimeException
);
76 static void convert( const SdrGluePoint
& rSdrGlue
, drawing::GluePoint2
& rUnoGlue
) throw()
78 rUnoGlue
.Position
.X
= rSdrGlue
.GetPos().X();
79 rUnoGlue
.Position
.Y
= rSdrGlue
.GetPos().Y();
80 rUnoGlue
.IsRelative
= rSdrGlue
.IsPercent();
82 switch( rSdrGlue
.GetAlign() )
84 case SDRVERTALIGN_TOP
|SDRHORZALIGN_LEFT
:
85 rUnoGlue
.PositionAlignment
= drawing::Alignment_TOP_LEFT
;
87 case SDRHORZALIGN_CENTER
|SDRVERTALIGN_TOP
:
88 rUnoGlue
.PositionAlignment
= drawing::Alignment_TOP
;
90 case SDRVERTALIGN_TOP
|SDRHORZALIGN_RIGHT
:
91 rUnoGlue
.PositionAlignment
= drawing::Alignment_TOP_RIGHT
;
93 case SDRHORZALIGN_CENTER
|SDRVERTALIGN_CENTER
:
94 rUnoGlue
.PositionAlignment
= drawing::Alignment_CENTER
;
96 case SDRHORZALIGN_RIGHT
|SDRVERTALIGN_CENTER
:
97 rUnoGlue
.PositionAlignment
= drawing::Alignment_RIGHT
;
99 case SDRHORZALIGN_LEFT
|SDRVERTALIGN_BOTTOM
:
100 rUnoGlue
.PositionAlignment
= drawing::Alignment_BOTTOM_LEFT
;
102 case SDRHORZALIGN_CENTER
|SDRVERTALIGN_BOTTOM
:
103 rUnoGlue
.PositionAlignment
= drawing::Alignment_BOTTOM
;
105 case SDRHORZALIGN_RIGHT
|SDRVERTALIGN_BOTTOM
:
106 rUnoGlue
.PositionAlignment
= drawing::Alignment_BOTTOM_RIGHT
;
108 // case SDRHORZALIGN_LEFT:
110 rUnoGlue
.PositionAlignment
= drawing::Alignment_LEFT
;
114 switch( rSdrGlue
.GetEscDir() )
117 rUnoGlue
.Escape
= drawing::EscapeDirection_LEFT
;
120 rUnoGlue
.Escape
= drawing::EscapeDirection_RIGHT
;
123 rUnoGlue
.Escape
= drawing::EscapeDirection_UP
;
126 rUnoGlue
.Escape
= drawing::EscapeDirection_DOWN
;
129 rUnoGlue
.Escape
= drawing::EscapeDirection_HORIZONTAL
;
132 rUnoGlue
.Escape
= drawing::EscapeDirection_VERTICAL
;
134 // case SDRESC_SMART:
136 rUnoGlue
.Escape
= drawing::EscapeDirection_SMART
;
141 static void convert( const drawing::GluePoint2
& rUnoGlue
, SdrGluePoint
& rSdrGlue
) throw()
143 rSdrGlue
.SetPos( Point( rUnoGlue
.Position
.X
, rUnoGlue
.Position
.Y
) );
144 rSdrGlue
.SetPercent( rUnoGlue
.IsRelative
);
146 switch( rUnoGlue
.PositionAlignment
)
148 case drawing::Alignment_TOP_LEFT
:
149 rSdrGlue
.SetAlign( SDRVERTALIGN_TOP
|SDRHORZALIGN_LEFT
);
151 case drawing::Alignment_TOP
:
152 rSdrGlue
.SetAlign( SDRHORZALIGN_CENTER
|SDRVERTALIGN_TOP
);
154 case drawing::Alignment_TOP_RIGHT
:
155 rSdrGlue
.SetAlign( SDRVERTALIGN_TOP
|SDRHORZALIGN_RIGHT
);
157 case drawing::Alignment_CENTER
:
158 rSdrGlue
.SetAlign( SDRHORZALIGN_CENTER
|SDRVERTALIGN_CENTER
);
160 case drawing::Alignment_RIGHT
:
161 rSdrGlue
.SetAlign( SDRHORZALIGN_RIGHT
|SDRVERTALIGN_CENTER
);
163 case drawing::Alignment_BOTTOM_LEFT
:
164 rSdrGlue
.SetAlign( SDRHORZALIGN_LEFT
|SDRVERTALIGN_BOTTOM
);
166 case drawing::Alignment_BOTTOM
:
167 rSdrGlue
.SetAlign( SDRHORZALIGN_CENTER
|SDRVERTALIGN_BOTTOM
);
169 case drawing::Alignment_BOTTOM_RIGHT
:
170 rSdrGlue
.SetAlign( SDRHORZALIGN_RIGHT
|SDRVERTALIGN_BOTTOM
);
172 // case SDRHORZALIGN_LEFT:
174 rSdrGlue
.SetAlign( SDRHORZALIGN_LEFT
);
177 switch( rUnoGlue
.Escape
)
179 case drawing::EscapeDirection_LEFT
:
180 rSdrGlue
.SetEscDir(SDRESC_LEFT
);
182 case drawing::EscapeDirection_RIGHT
:
183 rSdrGlue
.SetEscDir(SDRESC_RIGHT
);
185 case drawing::EscapeDirection_UP
:
186 rSdrGlue
.SetEscDir(SDRESC_TOP
);
188 case drawing::EscapeDirection_DOWN
:
189 rSdrGlue
.SetEscDir(SDRESC_BOTTOM
);
191 case drawing::EscapeDirection_HORIZONTAL
:
192 rSdrGlue
.SetEscDir(SDRESC_HORZ
);
194 case drawing::EscapeDirection_VERTICAL
:
195 rSdrGlue
.SetEscDir(SDRESC_VERT
);
197 // case drawing::EscapeDirection_SMART:
199 rSdrGlue
.SetEscDir(SDRESC_SMART
);
204 SvxUnoGluePointAccess::SvxUnoGluePointAccess( SdrObject
* pObject
) throw()
205 : mpObject( pObject
)
209 SvxUnoGluePointAccess::~SvxUnoGluePointAccess() throw()
213 // XIdentifierContainer
214 sal_Int32 SAL_CALL
SvxUnoGluePointAccess::insert( const uno::Any
& aElement
) throw (lang::IllegalArgumentException
, lang::WrappedTargetException
, uno::RuntimeException
)
218 SdrGluePointList
* pList
= mpObject
->ForceGluePointList();
221 // second, insert the new glue point
222 drawing::GluePoint2 aUnoGlue
;
224 if( aElement
>>= aUnoGlue
)
226 SdrGluePoint aSdrGlue
;
227 convert( aUnoGlue
, aSdrGlue
);
228 sal_uInt16 nId
= pList
->Insert( aSdrGlue
);
230 // only repaint, no objectchange
231 mpObject
->ActionChanged();
232 // mpObject->BroadcastObjectChange();
234 return (sal_Int32
)((*pList
)[nId
].GetId() + NON_USER_DEFINED_GLUE_POINTS
) - 1;
237 throw lang::IllegalArgumentException();
244 void SAL_CALL
SvxUnoGluePointAccess::removeByIdentifier( sal_Int32 Identifier
) throw (container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
)
246 if( mpObject
.is() && ( Identifier
>= NON_USER_DEFINED_GLUE_POINTS
))
248 const sal_uInt16 nId
= (sal_uInt16
)(Identifier
- NON_USER_DEFINED_GLUE_POINTS
) + 1;
250 SdrGluePointList
* pList
= const_cast<SdrGluePointList
*>(mpObject
->GetGluePointList());
251 const sal_uInt16 nCount
= pList
? pList
->GetCount() : 0;
254 for( i
= 0; i
< nCount
; i
++ )
256 if( (*pList
)[i
].GetId() == nId
)
260 // only repaint, no objectchange
261 mpObject
->ActionChanged();
262 // mpObject->BroadcastObjectChange();
269 throw container::NoSuchElementException();
272 // XIdentifierReplace
273 void SAL_CALL
SvxUnoGluePointAccess::replaceByIdentifer( sal_Int32 Identifier
, const uno::Any
& aElement
) throw (lang::IllegalArgumentException
, container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
)
275 if( mpObject
.is() && mpObject
->IsNode() )
277 struct drawing::GluePoint2 aGluePoint
;
278 if( (Identifier
< NON_USER_DEFINED_GLUE_POINTS
) || !(aElement
>>= aGluePoint
))
279 throw lang::IllegalArgumentException();
281 const sal_uInt16 nId
= (sal_uInt16
)( Identifier
- NON_USER_DEFINED_GLUE_POINTS
) + 1;
283 SdrGluePointList
* pList
= const_cast< SdrGluePointList
* >( mpObject
->GetGluePointList() );
284 const sal_uInt16 nCount
= pList
? pList
->GetCount() : 0;
286 for( i
= 0; i
< nCount
; i
++ )
288 if( (*pList
)[i
].GetId() == nId
)
290 // change the glue point
291 SdrGluePoint
& rTempPoint
= (*pList
)[i
];
292 convert( aGluePoint
, rTempPoint
);
294 // only repaint, no objectchange
295 mpObject
->ActionChanged();
296 // mpObject->BroadcastObjectChange();
302 throw container::NoSuchElementException();
307 uno::Any SAL_CALL
SvxUnoGluePointAccess::getByIdentifier( sal_Int32 Identifier
) throw (container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
)
309 if( mpObject
.is() && mpObject
->IsNode() )
311 struct drawing::GluePoint2 aGluePoint
;
313 if( Identifier
< NON_USER_DEFINED_GLUE_POINTS
) // default glue point?
315 SdrGluePoint aTempPoint
= mpObject
->GetVertexGluePoint( (sal_uInt16
)Identifier
);
316 aGluePoint
.IsUserDefined
= sal_False
;
317 convert( aTempPoint
, aGluePoint
);
318 return uno::makeAny( aGluePoint
);
322 const sal_uInt16 nId
= (sal_uInt16
)( Identifier
- NON_USER_DEFINED_GLUE_POINTS
) + 1;
324 const SdrGluePointList
* pList
= mpObject
->GetGluePointList();
325 const sal_uInt16 nCount
= pList
? pList
->GetCount() : 0;
326 for( sal_uInt16 i
= 0; i
< nCount
; i
++ )
328 const SdrGluePoint
& rTempPoint
= (*pList
)[i
];
329 if( rTempPoint
.GetId() == nId
)
332 if(rTempPoint
.IsUserDefined())
334 aGluePoint
.IsUserDefined
= sal_True
;
337 convert( rTempPoint
, aGluePoint
);
338 return uno::makeAny( aGluePoint
);
344 throw lang::IndexOutOfBoundsException();
347 uno::Sequence
< sal_Int32
> SAL_CALL
SvxUnoGluePointAccess::getIdentifiers() throw (uno::RuntimeException
)
351 const SdrGluePointList
* pList
= mpObject
->GetGluePointList();
352 const sal_uInt16 nCount
= pList
? pList
->GetCount() : 0;
356 uno::Sequence
< sal_Int32
> aIdSequence( nCount
+ NON_USER_DEFINED_GLUE_POINTS
);
357 sal_Int32
*pIdentifier
= aIdSequence
.getArray();
359 for( i
= 0; i
< NON_USER_DEFINED_GLUE_POINTS
; i
++ )
360 *pIdentifier
++ = (sal_Int32
)i
;
362 for( i
= 0; i
< nCount
; i
++ )
363 *pIdentifier
++ = (sal_Int32
) ( (*pList
)[i
].GetId() + NON_USER_DEFINED_GLUE_POINTS
) - 1;
369 uno::Sequence
< sal_Int32
> aEmpty
;
377 void SAL_CALL
SvxUnoGluePointAccess::insertByIndex( sal_Int32
, const uno::Any
& Element
)
378 throw(lang::IllegalArgumentException
, lang::IndexOutOfBoundsException
,
379 lang::WrappedTargetException
, uno::RuntimeException
)
383 SdrGluePointList
* pList
= mpObject
->ForceGluePointList();
386 SdrGluePoint aSdrGlue
;
387 drawing::GluePoint2 aUnoGlue
;
389 if( Element
>>= aUnoGlue
)
391 convert( aUnoGlue
, aSdrGlue
);
392 pList
->Insert( aSdrGlue
);
394 // only repaint, no objectchange
395 mpObject
->ActionChanged();
396 // mpObject->BroadcastObjectChange();
401 throw lang::IllegalArgumentException();
405 throw lang::IndexOutOfBoundsException();
408 void SAL_CALL
SvxUnoGluePointAccess::removeByIndex( sal_Int32 Index
)
409 throw(lang::IndexOutOfBoundsException
, lang::WrappedTargetException
, uno::RuntimeException
)
413 SdrGluePointList
* pList
= mpObject
->ForceGluePointList();
417 if( Index
>= 0 && Index
< pList
->GetCount() )
419 pList
->Delete( (sal_uInt16
)Index
);
421 // only repaint, no objectchange
422 mpObject
->ActionChanged();
423 // mpObject->BroadcastObjectChange();
430 throw lang::IndexOutOfBoundsException();
434 void SAL_CALL
SvxUnoGluePointAccess::replaceByIndex( sal_Int32 Index
, const uno::Any
& Element
)
435 throw(lang::IllegalArgumentException
, lang::IndexOutOfBoundsException
, lang::WrappedTargetException
,
436 uno::RuntimeException
)
438 drawing::GluePoint2 aUnoGlue
;
439 if(!(Element
>>= aUnoGlue
))
440 throw lang::IllegalArgumentException();
443 if( mpObject
.is() && Index
>= 0 )
445 SdrGluePointList
* pList
= const_cast< SdrGluePointList
* >( mpObject
->GetGluePointList() );
446 if( pList
&& Index
< pList
->GetCount() )
448 SdrGluePoint
& rGlue
= (*pList
)[(sal_uInt16
)Index
];
449 convert( aUnoGlue
, rGlue
);
451 // only repaint, no objectchange
452 mpObject
->ActionChanged();
453 // mpObject->BroadcastObjectChange();
457 throw lang::IndexOutOfBoundsException();
461 sal_Int32 SAL_CALL
SvxUnoGluePointAccess::getCount()
462 throw(uno::RuntimeException
)
464 sal_Int32 nCount
= 0;
467 // each node has a default of 4 glue points
468 // and any number of user defined glue points
469 if( mpObject
->IsNode() )
473 const SdrGluePointList
* pList
= mpObject
->GetGluePointList();
475 nCount
+= pList
->GetCount();
482 uno::Any SAL_CALL
SvxUnoGluePointAccess::getByIndex( sal_Int32 Index
)
483 throw(lang::IndexOutOfBoundsException
, lang::WrappedTargetException
, uno::RuntimeException
)
485 if( Index
>= 0 && mpObject
.is() && mpObject
->IsNode() )
487 struct drawing::GluePoint2 aGluePoint
;
489 if( Index
< 4 ) // default glue point?
491 SdrGluePoint aTempPoint
= mpObject
->GetVertexGluePoint( (sal_uInt16
)Index
);
492 aGluePoint
.IsUserDefined
= sal_False
;
493 convert( aTempPoint
, aGluePoint
);
501 const SdrGluePointList
* pList
= mpObject
->GetGluePointList();
502 if( pList
&& Index
< pList
->GetCount() )
504 const SdrGluePoint
& rTempPoint
= (*pList
)[(sal_uInt16
)Index
];
505 aGluePoint
.IsUserDefined
= sal_True
;
506 convert( rTempPoint
, aGluePoint
);
514 throw lang::IndexOutOfBoundsException();
518 uno::Type SAL_CALL
SvxUnoGluePointAccess::getElementType()
519 throw( uno::RuntimeException
)
521 return ::getCppuType((const struct drawing::GluePoint2
*)0);
524 sal_Bool SAL_CALL
SvxUnoGluePointAccess::hasElements()
525 throw( uno::RuntimeException
)
527 return mpObject
.is() && mpObject
->IsNode();
531 * Create a SvxUnoGluePointAccess
533 uno::Reference
< uno::XInterface
> SAL_CALL
SvxUnoGluePointAccess_createInstance( SdrObject
* pObject
)
535 return *new SvxUnoGluePointAccess(pObject
);
538 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */