bump product version to 5.0.4.1
[LibreOffice.git] / svx / source / unodraw / gluepts.cxx
blob08108b2c8d8b44afbb017086bed39bcd032696d0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #include <gluepts.hxx>
33 using namespace ::com::sun::star;
34 using namespace ::cppu;
36 const sal_uInt16 NON_USER_DEFINED_GLUE_POINTS = 4;
38 class SvxUnoGluePointAccess : public WeakImplHelper2< container::XIndexContainer, container::XIdentifierContainer >
40 private:
41 SdrObjectWeakRef mpObject;
43 public:
44 SvxUnoGluePointAccess( SdrObject* pObject ) throw();
45 virtual ~SvxUnoGluePointAccess() throw();
47 // XIdentifierContainer
48 virtual sal_Int32 SAL_CALL insert( const uno::Any& aElement ) throw (lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
49 virtual void SAL_CALL removeByIdentifier( sal_Int32 Identifier ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
51 // XIdentifierReplace
52 virtual void SAL_CALL replaceByIdentifer( sal_Int32 Identifier, const uno::Any& aElement ) throw (lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
54 // XIdentifierReplace
55 virtual uno::Any SAL_CALL getByIdentifier( sal_Int32 Identifier ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
56 virtual uno::Sequence< sal_Int32 > SAL_CALL getIdentifiers( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
58 /* deprecated */
59 // XIndexContainer
60 virtual void SAL_CALL insertByIndex( sal_Int32 Index, const uno::Any& Element ) throw(lang::IllegalArgumentException, lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
61 virtual void SAL_CALL removeByIndex( sal_Int32 Index ) throw(lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
63 /* deprecated */
64 // XIndexReplace
65 virtual void SAL_CALL replaceByIndex( sal_Int32 Index, const uno::Any& Element ) throw(lang::IllegalArgumentException, lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
67 /* deprecated */
68 // XIndexAccess
69 virtual sal_Int32 SAL_CALL getCount( ) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE;
70 virtual uno::Any SAL_CALL getByIndex( sal_Int32 Index ) throw(lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
72 // XElementAccess
73 virtual uno::Type SAL_CALL getElementType( ) throw( uno::RuntimeException, std::exception) SAL_OVERRIDE;
74 virtual sal_Bool SAL_CALL hasElements( ) throw( uno::RuntimeException, std::exception) SAL_OVERRIDE;
77 static void convert( const SdrGluePoint& rSdrGlue, drawing::GluePoint2& rUnoGlue ) throw()
79 rUnoGlue.Position.X = rSdrGlue.GetPos().X();
80 rUnoGlue.Position.Y = rSdrGlue.GetPos().Y();
81 rUnoGlue.IsRelative = rSdrGlue.IsPercent();
83 SdrAlign eAlign = rSdrGlue.GetAlign();
84 if (eAlign == (SdrAlign::VERT_TOP|SdrAlign::HORZ_LEFT))
85 rUnoGlue.PositionAlignment = drawing::Alignment_TOP_LEFT;
86 else if (eAlign == (SdrAlign::HORZ_CENTER|SdrAlign::VERT_TOP))
87 rUnoGlue.PositionAlignment = drawing::Alignment_TOP;
88 else if (eAlign == (SdrAlign::VERT_TOP|SdrAlign::HORZ_RIGHT))
89 rUnoGlue.PositionAlignment = drawing::Alignment_TOP_RIGHT;
90 else if (eAlign == (SdrAlign::HORZ_CENTER|SdrAlign::VERT_CENTER))
91 rUnoGlue.PositionAlignment = drawing::Alignment_CENTER;
92 else if (eAlign == (SdrAlign::HORZ_RIGHT|SdrAlign::VERT_CENTER))
93 rUnoGlue.PositionAlignment = drawing::Alignment_RIGHT;
94 else if (eAlign == (SdrAlign::HORZ_LEFT|SdrAlign::VERT_BOTTOM))
95 rUnoGlue.PositionAlignment = drawing::Alignment_BOTTOM_LEFT;
96 else if (eAlign == (SdrAlign::HORZ_CENTER|SdrAlign::VERT_BOTTOM))
97 rUnoGlue.PositionAlignment = drawing::Alignment_BOTTOM;
98 else if (eAlign == (SdrAlign::HORZ_RIGHT|SdrAlign::VERT_BOTTOM))
99 rUnoGlue.PositionAlignment = drawing::Alignment_BOTTOM_RIGHT;
100 else {
101 rUnoGlue.PositionAlignment = drawing::Alignment_LEFT;
104 switch( rSdrGlue.GetEscDir() )
106 case SdrEscapeDirection::LEFT:
107 rUnoGlue.Escape = drawing::EscapeDirection_LEFT;
108 break;
109 case SdrEscapeDirection::RIGHT:
110 rUnoGlue.Escape = drawing::EscapeDirection_RIGHT;
111 break;
112 case SdrEscapeDirection::TOP:
113 rUnoGlue.Escape = drawing::EscapeDirection_UP;
114 break;
115 case SdrEscapeDirection::BOTTOM:
116 rUnoGlue.Escape = drawing::EscapeDirection_DOWN;
117 break;
118 case SdrEscapeDirection::HORZ:
119 rUnoGlue.Escape = drawing::EscapeDirection_HORIZONTAL;
120 break;
121 case SdrEscapeDirection::VERT:
122 rUnoGlue.Escape = drawing::EscapeDirection_VERTICAL;
123 break;
124 // case SdrEscapeDirection::SMART:
125 default:
126 rUnoGlue.Escape = drawing::EscapeDirection_SMART;
127 break;
131 static void convert( const drawing::GluePoint2& rUnoGlue, SdrGluePoint& rSdrGlue ) throw()
133 rSdrGlue.SetPos( Point( rUnoGlue.Position.X, rUnoGlue.Position.Y ) );
134 rSdrGlue.SetPercent( rUnoGlue.IsRelative );
136 switch( rUnoGlue.PositionAlignment )
138 case drawing::Alignment_TOP_LEFT:
139 rSdrGlue.SetAlign( SdrAlign::VERT_TOP|SdrAlign::HORZ_LEFT );
140 break;
141 case drawing::Alignment_TOP:
142 rSdrGlue.SetAlign( SdrAlign::HORZ_CENTER|SdrAlign::VERT_TOP );
143 break;
144 case drawing::Alignment_TOP_RIGHT:
145 rSdrGlue.SetAlign( SdrAlign::VERT_TOP|SdrAlign::HORZ_RIGHT );
146 break;
147 case drawing::Alignment_CENTER:
148 rSdrGlue.SetAlign( SdrAlign::HORZ_CENTER|SdrAlign::VERT_CENTER );
149 break;
150 case drawing::Alignment_RIGHT:
151 rSdrGlue.SetAlign( SdrAlign::HORZ_RIGHT|SdrAlign::VERT_CENTER );
152 break;
153 case drawing::Alignment_BOTTOM_LEFT:
154 rSdrGlue.SetAlign( SdrAlign::HORZ_LEFT|SdrAlign::VERT_BOTTOM );
155 break;
156 case drawing::Alignment_BOTTOM:
157 rSdrGlue.SetAlign( SdrAlign::HORZ_CENTER|SdrAlign::VERT_BOTTOM );
158 break;
159 case drawing::Alignment_BOTTOM_RIGHT:
160 rSdrGlue.SetAlign( SdrAlign::HORZ_RIGHT|SdrAlign::VERT_BOTTOM );
161 break;
162 // case SdrAlign::HORZ_LEFT:
163 default:
164 rSdrGlue.SetAlign( SdrAlign::HORZ_LEFT );
165 break;
167 switch( rUnoGlue.Escape )
169 case drawing::EscapeDirection_LEFT:
170 rSdrGlue.SetEscDir(SdrEscapeDirection::LEFT);
171 break;
172 case drawing::EscapeDirection_RIGHT:
173 rSdrGlue.SetEscDir(SdrEscapeDirection::RIGHT);
174 break;
175 case drawing::EscapeDirection_UP:
176 rSdrGlue.SetEscDir(SdrEscapeDirection::TOP);
177 break;
178 case drawing::EscapeDirection_DOWN:
179 rSdrGlue.SetEscDir(SdrEscapeDirection::BOTTOM);
180 break;
181 case drawing::EscapeDirection_HORIZONTAL:
182 rSdrGlue.SetEscDir(SdrEscapeDirection::HORZ);
183 break;
184 case drawing::EscapeDirection_VERTICAL:
185 rSdrGlue.SetEscDir(SdrEscapeDirection::VERT);
186 break;
187 // case drawing::EscapeDirection_SMART:
188 default:
189 rSdrGlue.SetEscDir(SdrEscapeDirection::SMART);
190 break;
194 SvxUnoGluePointAccess::SvxUnoGluePointAccess( SdrObject* pObject ) throw()
195 : mpObject( pObject )
199 SvxUnoGluePointAccess::~SvxUnoGluePointAccess() throw()
203 // XIdentifierContainer
204 sal_Int32 SAL_CALL SvxUnoGluePointAccess::insert( const uno::Any& aElement ) throw (lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
206 if( mpObject.is() )
208 SdrGluePointList* pList = mpObject->ForceGluePointList();
209 if( pList )
211 // second, insert the new glue point
212 drawing::GluePoint2 aUnoGlue;
214 if( aElement >>= aUnoGlue )
216 SdrGluePoint aSdrGlue;
217 convert( aUnoGlue, aSdrGlue );
218 sal_uInt16 nId = pList->Insert( aSdrGlue );
220 // only repaint, no objectchange
221 mpObject->ActionChanged();
222 // mpObject->BroadcastObjectChange();
224 return (sal_Int32)((*pList)[nId].GetId() + NON_USER_DEFINED_GLUE_POINTS) - 1;
227 throw lang::IllegalArgumentException();
231 return -1;
234 void SAL_CALL SvxUnoGluePointAccess::removeByIdentifier( sal_Int32 Identifier ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
236 if( mpObject.is() && ( Identifier >= NON_USER_DEFINED_GLUE_POINTS ))
238 const sal_uInt16 nId = (sal_uInt16)(Identifier - NON_USER_DEFINED_GLUE_POINTS) + 1;
240 SdrGluePointList* pList = const_cast<SdrGluePointList*>(mpObject->GetGluePointList());
241 const sal_uInt16 nCount = pList ? pList->GetCount() : 0;
242 sal_uInt16 i;
244 for( i = 0; i < nCount; i++ )
246 if( (*pList)[i].GetId() == nId )
248 pList->Delete( i );
250 // only repaint, no objectchange
251 mpObject->ActionChanged();
252 // mpObject->BroadcastObjectChange();
254 return;
259 throw container::NoSuchElementException();
262 // XIdentifierReplace
263 void SAL_CALL SvxUnoGluePointAccess::replaceByIdentifer( sal_Int32 Identifier, const uno::Any& aElement ) throw (lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
265 if( mpObject.is() && mpObject->IsNode() )
267 struct drawing::GluePoint2 aGluePoint;
268 if( (Identifier < NON_USER_DEFINED_GLUE_POINTS) || !(aElement >>= aGluePoint))
269 throw lang::IllegalArgumentException();
271 const sal_uInt16 nId = (sal_uInt16)( Identifier - NON_USER_DEFINED_GLUE_POINTS ) + 1;
273 SdrGluePointList* pList = const_cast< SdrGluePointList* >( mpObject->GetGluePointList() );
274 const sal_uInt16 nCount = pList ? pList->GetCount() : 0;
275 sal_uInt16 i;
276 for( i = 0; i < nCount; i++ )
278 if( (*pList)[i].GetId() == nId )
280 // change the glue point
281 SdrGluePoint& rTempPoint = (*pList)[i];
282 convert( aGluePoint, rTempPoint );
284 // only repaint, no objectchange
285 mpObject->ActionChanged();
286 // mpObject->BroadcastObjectChange();
288 return;
292 throw container::NoSuchElementException();
296 // XIdentifierAccess
297 uno::Any SAL_CALL SvxUnoGluePointAccess::getByIdentifier( sal_Int32 Identifier ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
299 if( mpObject.is() && mpObject->IsNode() )
301 struct drawing::GluePoint2 aGluePoint;
303 if( Identifier < NON_USER_DEFINED_GLUE_POINTS ) // default glue point?
305 SdrGluePoint aTempPoint = mpObject->GetVertexGluePoint( (sal_uInt16)Identifier );
306 aGluePoint.IsUserDefined = sal_False;
307 convert( aTempPoint, aGluePoint );
308 return uno::makeAny( aGluePoint );
310 else
312 const sal_uInt16 nId = (sal_uInt16)( Identifier - NON_USER_DEFINED_GLUE_POINTS ) + 1;
314 const SdrGluePointList* pList = mpObject->GetGluePointList();
315 const sal_uInt16 nCount = pList ? pList->GetCount() : 0;
316 for( sal_uInt16 i = 0; i < nCount; i++ )
318 const SdrGluePoint& rTempPoint = (*pList)[i];
319 if( rTempPoint.GetId() == nId )
321 // #i38892#
322 if(rTempPoint.IsUserDefined())
324 aGluePoint.IsUserDefined = sal_True;
327 convert( rTempPoint, aGluePoint );
328 return uno::makeAny( aGluePoint );
334 throw container::NoSuchElementException();
337 uno::Sequence< sal_Int32 > SAL_CALL SvxUnoGluePointAccess::getIdentifiers() throw (uno::RuntimeException, std::exception)
339 if( mpObject.is() )
341 const SdrGluePointList* pList = mpObject->GetGluePointList();
342 const sal_uInt16 nCount = pList ? pList->GetCount() : 0;
344 sal_uInt16 i;
346 uno::Sequence< sal_Int32 > aIdSequence( nCount + NON_USER_DEFINED_GLUE_POINTS );
347 sal_Int32 *pIdentifier = aIdSequence.getArray();
349 for( i = 0; i < NON_USER_DEFINED_GLUE_POINTS; i++ )
350 *pIdentifier++ = (sal_Int32)i;
352 for( i = 0; i < nCount; i++ )
353 *pIdentifier++ = (sal_Int32) ( (*pList)[i].GetId() + NON_USER_DEFINED_GLUE_POINTS ) - 1;
355 return aIdSequence;
357 else
359 uno::Sequence< sal_Int32 > aEmpty;
360 return aEmpty;
364 /* deprecated */
366 // XIndexContainer
367 void SAL_CALL SvxUnoGluePointAccess::insertByIndex( sal_Int32, const uno::Any& Element )
368 throw(lang::IllegalArgumentException, lang::IndexOutOfBoundsException,
369 lang::WrappedTargetException, uno::RuntimeException, std::exception)
371 if( mpObject.is() )
373 SdrGluePointList* pList = mpObject->ForceGluePointList();
374 if( pList )
376 SdrGluePoint aSdrGlue;
377 drawing::GluePoint2 aUnoGlue;
379 if( Element >>= aUnoGlue )
381 convert( aUnoGlue, aSdrGlue );
382 pList->Insert( aSdrGlue );
384 // only repaint, no objectchange
385 mpObject->ActionChanged();
386 // mpObject->BroadcastObjectChange();
388 return;
391 throw lang::IllegalArgumentException();
395 throw lang::IndexOutOfBoundsException();
398 void SAL_CALL SvxUnoGluePointAccess::removeByIndex( sal_Int32 Index )
399 throw(lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
401 if( mpObject.is() )
403 SdrGluePointList* pList = mpObject->ForceGluePointList();
404 if( pList )
406 Index -= 4;
407 if( Index >= 0 && Index < pList->GetCount() )
409 pList->Delete( (sal_uInt16)Index );
411 // only repaint, no objectchange
412 mpObject->ActionChanged();
413 // mpObject->BroadcastObjectChange();
415 return;
420 throw lang::IndexOutOfBoundsException();
423 // XIndexReplace
424 void SAL_CALL SvxUnoGluePointAccess::replaceByIndex( sal_Int32 Index, const uno::Any& Element )
425 throw(lang::IllegalArgumentException, lang::IndexOutOfBoundsException, lang::WrappedTargetException,
426 uno::RuntimeException, std::exception)
428 drawing::GluePoint2 aUnoGlue;
429 if(!(Element >>= aUnoGlue))
430 throw lang::IllegalArgumentException();
432 Index -= 4;
433 if( mpObject.is() && Index >= 0 )
435 SdrGluePointList* pList = const_cast< SdrGluePointList* >( mpObject->GetGluePointList() );
436 if( pList && Index < pList->GetCount() )
438 SdrGluePoint& rGlue = (*pList)[(sal_uInt16)Index];
439 convert( aUnoGlue, rGlue );
441 // only repaint, no objectchange
442 mpObject->ActionChanged();
443 // mpObject->BroadcastObjectChange();
447 throw lang::IndexOutOfBoundsException();
450 // XIndexAccess
451 sal_Int32 SAL_CALL SvxUnoGluePointAccess::getCount()
452 throw(uno::RuntimeException, std::exception)
454 sal_Int32 nCount = 0;
455 if( mpObject.is() )
457 // each node has a default of 4 glue points
458 // and any number of user defined glue points
459 if( mpObject->IsNode() )
461 nCount += 4;
463 const SdrGluePointList* pList = mpObject->GetGluePointList();
464 if( pList )
465 nCount += pList->GetCount();
469 return nCount;
472 uno::Any SAL_CALL SvxUnoGluePointAccess::getByIndex( sal_Int32 Index )
473 throw(lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
475 if( Index >= 0 && mpObject.is() && mpObject->IsNode() )
477 struct drawing::GluePoint2 aGluePoint;
479 if( Index < 4 ) // default glue point?
481 SdrGluePoint aTempPoint = mpObject->GetVertexGluePoint( (sal_uInt16)Index );
482 aGluePoint.IsUserDefined = sal_False;
483 convert( aTempPoint, aGluePoint );
484 uno::Any aAny;
485 aAny <<= aGluePoint;
486 return aAny;
488 else
490 Index -= 4;
491 const SdrGluePointList* pList = mpObject->GetGluePointList();
492 if( pList && Index < pList->GetCount() )
494 const SdrGluePoint& rTempPoint = (*pList)[(sal_uInt16)Index];
495 aGluePoint.IsUserDefined = sal_True;
496 convert( rTempPoint, aGluePoint );
497 uno::Any aAny;
498 aAny <<= aGluePoint;
499 return aAny;
504 throw lang::IndexOutOfBoundsException();
507 // XElementAccess
508 uno::Type SAL_CALL SvxUnoGluePointAccess::getElementType()
509 throw( uno::RuntimeException, std::exception)
511 return cppu::UnoType<drawing::GluePoint2>::get();
514 sal_Bool SAL_CALL SvxUnoGluePointAccess::hasElements()
515 throw( uno::RuntimeException, std::exception)
517 return mpObject.is() && mpObject->IsNode();
521 * Create a SvxUnoGluePointAccess
523 uno::Reference< uno::XInterface > SAL_CALL SvxUnoGluePointAccess_createInstance( SdrObject* pObject )
525 return *new SvxUnoGluePointAccess(pObject);
528 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */