bump product version to 4.1.6.2
[LibreOffice.git] / svx / source / unodraw / gluepts.cxx
blobff44e982e78e13cf33b76f310b8fc1a8b7abe3b2
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 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 >
39 private:
40 SdrObjectWeakRef mpObject;
42 public:
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);
50 // XIdentifierReplace
51 virtual void SAL_CALL replaceByIdentifer( sal_Int32 Identifier, const uno::Any& aElement ) throw (lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException);
53 // XIdentifierReplace
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);
57 /* deprecated */
58 // XIndexContainer
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);
62 /* deprecated */
63 // XIndexReplace
64 virtual void SAL_CALL replaceByIndex( sal_Int32 Index, const uno::Any& Element ) throw(lang::IllegalArgumentException, lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException);
66 /* deprecated */
67 // XIndexAccess
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);
71 // XElementAccess
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;
86 break;
87 case SDRHORZALIGN_CENTER|SDRVERTALIGN_TOP:
88 rUnoGlue.PositionAlignment = drawing::Alignment_TOP;
89 break;
90 case SDRVERTALIGN_TOP|SDRHORZALIGN_RIGHT:
91 rUnoGlue.PositionAlignment = drawing::Alignment_TOP_RIGHT;
92 break;
93 case SDRHORZALIGN_CENTER|SDRVERTALIGN_CENTER:
94 rUnoGlue.PositionAlignment = drawing::Alignment_CENTER;
95 break;
96 case SDRHORZALIGN_RIGHT|SDRVERTALIGN_CENTER:
97 rUnoGlue.PositionAlignment = drawing::Alignment_RIGHT;
98 break;
99 case SDRHORZALIGN_LEFT|SDRVERTALIGN_BOTTOM:
100 rUnoGlue.PositionAlignment = drawing::Alignment_BOTTOM_LEFT;
101 break;
102 case SDRHORZALIGN_CENTER|SDRVERTALIGN_BOTTOM:
103 rUnoGlue.PositionAlignment = drawing::Alignment_BOTTOM;
104 break;
105 case SDRHORZALIGN_RIGHT|SDRVERTALIGN_BOTTOM:
106 rUnoGlue.PositionAlignment = drawing::Alignment_BOTTOM_RIGHT;
107 break;
108 // case SDRHORZALIGN_LEFT:
109 default:
110 rUnoGlue.PositionAlignment = drawing::Alignment_LEFT;
111 break;
114 switch( rSdrGlue.GetEscDir() )
116 case SDRESC_LEFT:
117 rUnoGlue.Escape = drawing::EscapeDirection_LEFT;
118 break;
119 case SDRESC_RIGHT:
120 rUnoGlue.Escape = drawing::EscapeDirection_RIGHT;
121 break;
122 case SDRESC_TOP:
123 rUnoGlue.Escape = drawing::EscapeDirection_UP;
124 break;
125 case SDRESC_BOTTOM:
126 rUnoGlue.Escape = drawing::EscapeDirection_DOWN;
127 break;
128 case SDRESC_HORZ:
129 rUnoGlue.Escape = drawing::EscapeDirection_HORIZONTAL;
130 break;
131 case SDRESC_VERT:
132 rUnoGlue.Escape = drawing::EscapeDirection_VERTICAL;
133 break;
134 // case SDRESC_SMART:
135 default:
136 rUnoGlue.Escape = drawing::EscapeDirection_SMART;
137 break;
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 );
150 break;
151 case drawing::Alignment_TOP:
152 rSdrGlue.SetAlign( SDRHORZALIGN_CENTER|SDRVERTALIGN_TOP );
153 break;
154 case drawing::Alignment_TOP_RIGHT:
155 rSdrGlue.SetAlign( SDRVERTALIGN_TOP|SDRHORZALIGN_RIGHT );
156 break;
157 case drawing::Alignment_CENTER:
158 rSdrGlue.SetAlign( SDRHORZALIGN_CENTER|SDRVERTALIGN_CENTER );
159 break;
160 case drawing::Alignment_RIGHT:
161 rSdrGlue.SetAlign( SDRHORZALIGN_RIGHT|SDRVERTALIGN_CENTER );
162 break;
163 case drawing::Alignment_BOTTOM_LEFT:
164 rSdrGlue.SetAlign( SDRHORZALIGN_LEFT|SDRVERTALIGN_BOTTOM );
165 break;
166 case drawing::Alignment_BOTTOM:
167 rSdrGlue.SetAlign( SDRHORZALIGN_CENTER|SDRVERTALIGN_BOTTOM );
168 break;
169 case drawing::Alignment_BOTTOM_RIGHT:
170 rSdrGlue.SetAlign( SDRHORZALIGN_RIGHT|SDRVERTALIGN_BOTTOM );
171 break;
172 // case SDRHORZALIGN_LEFT:
173 default:
174 rSdrGlue.SetAlign( SDRHORZALIGN_LEFT );
175 break;
177 switch( rUnoGlue.Escape )
179 case drawing::EscapeDirection_LEFT:
180 rSdrGlue.SetEscDir(SDRESC_LEFT);
181 break;
182 case drawing::EscapeDirection_RIGHT:
183 rSdrGlue.SetEscDir(SDRESC_RIGHT);
184 break;
185 case drawing::EscapeDirection_UP:
186 rSdrGlue.SetEscDir(SDRESC_TOP);
187 break;
188 case drawing::EscapeDirection_DOWN:
189 rSdrGlue.SetEscDir(SDRESC_BOTTOM);
190 break;
191 case drawing::EscapeDirection_HORIZONTAL:
192 rSdrGlue.SetEscDir(SDRESC_HORZ);
193 break;
194 case drawing::EscapeDirection_VERTICAL:
195 rSdrGlue.SetEscDir(SDRESC_VERT);
196 break;
197 // case drawing::EscapeDirection_SMART:
198 default:
199 rSdrGlue.SetEscDir(SDRESC_SMART);
200 break;
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)
216 if( mpObject.is() )
218 SdrGluePointList* pList = mpObject->ForceGluePointList();
219 if( pList )
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();
241 return -1;
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;
252 sal_uInt16 i;
254 for( i = 0; i < nCount; i++ )
256 if( (*pList)[i].GetId() == nId )
258 pList->Delete( i );
260 // only repaint, no objectchange
261 mpObject->ActionChanged();
262 // mpObject->BroadcastObjectChange();
264 return;
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;
285 sal_uInt16 i;
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();
298 return;
302 throw container::NoSuchElementException();
306 // XIdentifierAccess
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 );
320 else
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 )
331 // #i38892#
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)
349 if( mpObject.is() )
351 const SdrGluePointList* pList = mpObject->GetGluePointList();
352 const sal_uInt16 nCount = pList ? pList->GetCount() : 0;
354 sal_uInt16 i;
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;
365 return aIdSequence;
367 else
369 uno::Sequence< sal_Int32 > aEmpty;
370 return aEmpty;
374 /* deprecated */
376 // XIndexContainer
377 void SAL_CALL SvxUnoGluePointAccess::insertByIndex( sal_Int32, const uno::Any& Element )
378 throw(lang::IllegalArgumentException, lang::IndexOutOfBoundsException,
379 lang::WrappedTargetException, uno::RuntimeException)
381 if( mpObject.is() )
383 SdrGluePointList* pList = mpObject->ForceGluePointList();
384 if( pList )
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();
398 return;
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)
411 if( mpObject.is() )
413 SdrGluePointList* pList = mpObject->ForceGluePointList();
414 if( pList )
416 Index -= 4;
417 if( Index >= 0 && Index < pList->GetCount() )
419 pList->Delete( (sal_uInt16)Index );
421 // only repaint, no objectchange
422 mpObject->ActionChanged();
423 // mpObject->BroadcastObjectChange();
425 return;
430 throw lang::IndexOutOfBoundsException();
433 // XIndexReplace
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();
442 Index -= 4;
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();
460 // XIndexAccess
461 sal_Int32 SAL_CALL SvxUnoGluePointAccess::getCount()
462 throw(uno::RuntimeException)
464 sal_Int32 nCount = 0;
465 if( mpObject.is() )
467 // each node has a default of 4 glue points
468 // and any number of user defined glue points
469 if( mpObject->IsNode() )
471 nCount += 4;
473 const SdrGluePointList* pList = mpObject->GetGluePointList();
474 if( pList )
475 nCount += pList->GetCount();
479 return nCount;
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 );
494 uno::Any aAny;
495 aAny <<= aGluePoint;
496 return aAny;
498 else
500 Index -= 4;
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 );
507 uno::Any aAny;
508 aAny <<= aGluePoint;
509 return aAny;
514 throw lang::IndexOutOfBoundsException();
517 // XElementAccess
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: */