merge the formfield patch from ooo-build
[ooovba.git] / svx / source / unodraw / gluepts.cxx
blobca41fa9fe519183500a374d68ac284e134871a43
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: gluepts.cxx,v $
10 * $Revision: 1.18 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
33 #include <com/sun/star/container/XIdentifierContainer.hpp>
34 #include <com/sun/star/container/XIndexContainer.hpp>
35 #ifndef _COM_SUN_STAR_DRAWING_GLUEPOINT2_HDL_
36 #include <com/sun/star/drawing/GluePoint2.hpp>
37 #endif
39 #include <cppuhelper/implbase2.hxx>
41 #include <svx/svdmodel.hxx>
42 #include <svx/svdobj.hxx>
43 #include <svx/svdglue.hxx>
44 #include <svx/svdpage.hxx>
46 using namespace ::com::sun::star;
47 using namespace ::rtl;
48 using namespace ::cppu;
50 const USHORT NON_USER_DEFINED_GLUE_POINTS = 4;
52 class SvxUnoGluePointAccess : public WeakImplHelper2< container::XIndexContainer, container::XIdentifierContainer >
54 private:
55 SdrObjectWeakRef mpObject;
57 public:
58 SvxUnoGluePointAccess( SdrObject* pObject ) throw();
59 virtual ~SvxUnoGluePointAccess() throw();
61 // XIdentifierContainer
62 virtual sal_Int32 SAL_CALL insert( const uno::Any& aElement ) throw (lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException);
63 virtual void SAL_CALL removeByIdentifier( sal_Int32 Identifier ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException);
65 // XIdentifierReplace
66 virtual void SAL_CALL replaceByIdentifer( sal_Int32 Identifier, const uno::Any& aElement ) throw (lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException);
68 // XIdentifierReplace
69 virtual uno::Any SAL_CALL getByIdentifier( sal_Int32 Identifier ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException);
70 virtual uno::Sequence< sal_Int32 > SAL_CALL getIdentifiers( ) throw (uno::RuntimeException);
72 /* deprecated */
73 // XIndexContainer
74 virtual void SAL_CALL insertByIndex( sal_Int32 Index, const uno::Any& Element ) throw(lang::IllegalArgumentException, lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException);
75 virtual void SAL_CALL removeByIndex( sal_Int32 Index ) throw(lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException);
77 /* deprecated */
78 // XIndexReplace
79 virtual void SAL_CALL replaceByIndex( sal_Int32 Index, const uno::Any& Element ) throw(lang::IllegalArgumentException, lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException);
81 /* deprecated */
82 // XIndexAccess
83 virtual sal_Int32 SAL_CALL getCount( ) throw(uno::RuntimeException);
84 virtual uno::Any SAL_CALL getByIndex( sal_Int32 Index ) throw(lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException);
86 // XElementAccess
87 virtual uno::Type SAL_CALL getElementType( ) throw( uno::RuntimeException);
88 virtual sal_Bool SAL_CALL hasElements( ) throw( uno::RuntimeException);
91 static void convert( const SdrGluePoint& rSdrGlue, drawing::GluePoint2& rUnoGlue ) throw()
93 rUnoGlue.Position.X = rSdrGlue.GetPos().X();
94 rUnoGlue.Position.Y = rSdrGlue.GetPos().Y();
95 rUnoGlue.IsRelative = rSdrGlue.IsPercent();
97 switch( rSdrGlue.GetAlign() )
99 case SDRVERTALIGN_TOP|SDRHORZALIGN_LEFT:
100 rUnoGlue.PositionAlignment = drawing::Alignment_TOP_LEFT;
101 break;
102 case SDRHORZALIGN_CENTER|SDRVERTALIGN_TOP:
103 rUnoGlue.PositionAlignment = drawing::Alignment_TOP;
104 break;
105 case SDRVERTALIGN_TOP|SDRHORZALIGN_RIGHT:
106 rUnoGlue.PositionAlignment = drawing::Alignment_TOP_RIGHT;
107 break;
108 case SDRHORZALIGN_CENTER|SDRVERTALIGN_CENTER:
109 rUnoGlue.PositionAlignment = drawing::Alignment_CENTER;
110 break;
111 case SDRHORZALIGN_RIGHT|SDRVERTALIGN_CENTER:
112 rUnoGlue.PositionAlignment = drawing::Alignment_RIGHT;
113 break;
114 case SDRHORZALIGN_LEFT|SDRVERTALIGN_BOTTOM:
115 rUnoGlue.PositionAlignment = drawing::Alignment_BOTTOM_LEFT;
116 break;
117 case SDRHORZALIGN_CENTER|SDRVERTALIGN_BOTTOM:
118 rUnoGlue.PositionAlignment = drawing::Alignment_BOTTOM;
119 break;
120 case SDRHORZALIGN_RIGHT|SDRVERTALIGN_BOTTOM:
121 rUnoGlue.PositionAlignment = drawing::Alignment_BOTTOM_RIGHT;
122 break;
123 // case SDRHORZALIGN_LEFT:
124 default:
125 rUnoGlue.PositionAlignment = drawing::Alignment_LEFT;
126 break;
129 switch( rSdrGlue.GetEscDir() )
131 case SDRESC_LEFT:
132 rUnoGlue.Escape = drawing::EscapeDirection_LEFT;
133 break;
134 case SDRESC_RIGHT:
135 rUnoGlue.Escape = drawing::EscapeDirection_RIGHT;
136 break;
137 case SDRESC_TOP:
138 rUnoGlue.Escape = drawing::EscapeDirection_UP;
139 break;
140 case SDRESC_BOTTOM:
141 rUnoGlue.Escape = drawing::EscapeDirection_DOWN;
142 break;
143 case SDRESC_HORZ:
144 rUnoGlue.Escape = drawing::EscapeDirection_HORIZONTAL;
145 break;
146 case SDRESC_VERT:
147 rUnoGlue.Escape = drawing::EscapeDirection_VERTICAL;
148 break;
149 // case SDRESC_SMART:
150 default:
151 rUnoGlue.Escape = drawing::EscapeDirection_SMART;
152 break;
156 static void convert( const drawing::GluePoint2& rUnoGlue, SdrGluePoint& rSdrGlue ) throw()
158 rSdrGlue.SetPos( Point( rUnoGlue.Position.X, rUnoGlue.Position.Y ) );
159 rSdrGlue.SetPercent( rUnoGlue.IsRelative );
161 switch( rUnoGlue.PositionAlignment )
163 case drawing::Alignment_TOP_LEFT:
164 rSdrGlue.SetAlign( SDRVERTALIGN_TOP|SDRHORZALIGN_LEFT );
165 break;
166 case drawing::Alignment_TOP:
167 rSdrGlue.SetAlign( SDRHORZALIGN_CENTER|SDRVERTALIGN_TOP );
168 break;
169 case drawing::Alignment_TOP_RIGHT:
170 rSdrGlue.SetAlign( SDRVERTALIGN_TOP|SDRHORZALIGN_RIGHT );
171 break;
172 case drawing::Alignment_CENTER:
173 rSdrGlue.SetAlign( SDRHORZALIGN_CENTER|SDRVERTALIGN_CENTER );
174 break;
175 case drawing::Alignment_RIGHT:
176 rSdrGlue.SetAlign( SDRHORZALIGN_RIGHT|SDRVERTALIGN_CENTER );
177 break;
178 case drawing::Alignment_BOTTOM_LEFT:
179 rSdrGlue.SetAlign( SDRHORZALIGN_LEFT|SDRVERTALIGN_BOTTOM );
180 break;
181 case drawing::Alignment_BOTTOM:
182 rSdrGlue.SetAlign( SDRHORZALIGN_CENTER|SDRVERTALIGN_BOTTOM );
183 break;
184 case drawing::Alignment_BOTTOM_RIGHT:
185 rSdrGlue.SetAlign( SDRHORZALIGN_RIGHT|SDRVERTALIGN_BOTTOM );
186 break;
187 // case SDRHORZALIGN_LEFT:
188 default:
189 rSdrGlue.SetAlign( SDRHORZALIGN_LEFT );
190 break;
192 switch( rUnoGlue.Escape )
194 case drawing::EscapeDirection_LEFT:
195 rSdrGlue.SetEscDir(SDRESC_LEFT);
196 break;
197 case drawing::EscapeDirection_RIGHT:
198 rSdrGlue.SetEscDir(SDRESC_RIGHT);
199 break;
200 case drawing::EscapeDirection_UP:
201 rSdrGlue.SetEscDir(SDRESC_TOP);
202 break;
203 case drawing::EscapeDirection_DOWN:
204 rSdrGlue.SetEscDir(SDRESC_BOTTOM);
205 break;
206 case drawing::EscapeDirection_HORIZONTAL:
207 rSdrGlue.SetEscDir(SDRESC_HORZ);
208 break;
209 case drawing::EscapeDirection_VERTICAL:
210 rSdrGlue.SetEscDir(SDRESC_VERT);
211 break;
212 // case drawing::EscapeDirection_SMART:
213 default:
214 rSdrGlue.SetEscDir(SDRESC_SMART);
215 break;
219 SvxUnoGluePointAccess::SvxUnoGluePointAccess( SdrObject* pObject ) throw()
220 : mpObject( pObject )
224 SvxUnoGluePointAccess::~SvxUnoGluePointAccess() throw()
228 // XIdentifierContainer
229 sal_Int32 SAL_CALL SvxUnoGluePointAccess::insert( const uno::Any& aElement ) throw (lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
231 if( mpObject.is() )
233 SdrGluePointList* pList = mpObject->ForceGluePointList();
234 if( pList )
236 // second, insert the new glue point
237 drawing::GluePoint2 aUnoGlue;
239 if( aElement >>= aUnoGlue )
241 SdrGluePoint aSdrGlue;
242 convert( aUnoGlue, aSdrGlue );
243 USHORT nId = pList->Insert( aSdrGlue );
245 // only repaint, no objectchange
246 mpObject->ActionChanged();
247 // mpObject->BroadcastObjectChange();
249 return (sal_Int32)((*pList)[nId].GetId() + NON_USER_DEFINED_GLUE_POINTS) - 1;
252 throw lang::IllegalArgumentException();
256 return -1;
259 void SAL_CALL SvxUnoGluePointAccess::removeByIdentifier( sal_Int32 Identifier ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
261 if( mpObject.is() && ( Identifier >= NON_USER_DEFINED_GLUE_POINTS ))
263 const USHORT nId = (USHORT)(Identifier - NON_USER_DEFINED_GLUE_POINTS) + 1;
265 SdrGluePointList* pList = const_cast<SdrGluePointList*>(mpObject->GetGluePointList());
266 const USHORT nCount = pList ? pList->GetCount() : 0;
267 USHORT i;
269 for( i = 0; i < nCount; i++ )
271 if( (*pList)[i].GetId() == nId )
273 pList->Delete( i );
275 // only repaint, no objectchange
276 mpObject->ActionChanged();
277 // mpObject->BroadcastObjectChange();
279 return;
284 throw container::NoSuchElementException();
287 // XIdentifierReplace
288 void SAL_CALL SvxUnoGluePointAccess::replaceByIdentifer( sal_Int32 Identifier, const uno::Any& aElement ) throw (lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
290 if( mpObject.is() && mpObject->IsNode() )
292 struct drawing::GluePoint2 aGluePoint;
293 if( (Identifier < NON_USER_DEFINED_GLUE_POINTS) || !(aElement >>= aGluePoint))
294 throw lang::IllegalArgumentException();
296 const USHORT nId = (USHORT)( Identifier - NON_USER_DEFINED_GLUE_POINTS ) + 1;
298 SdrGluePointList* pList = const_cast< SdrGluePointList* >( mpObject->GetGluePointList() );
299 const USHORT nCount = pList ? pList->GetCount() : 0;
300 USHORT i;
301 for( i = 0; i < nCount; i++ )
303 if( (*pList)[i].GetId() == nId )
305 // change the glue point
306 SdrGluePoint& rTempPoint = (*pList)[i];
307 convert( aGluePoint, rTempPoint );
309 // only repaint, no objectchange
310 mpObject->ActionChanged();
311 // mpObject->BroadcastObjectChange();
313 return;
317 throw container::NoSuchElementException();
321 // XIdentifierAccess
322 uno::Any SAL_CALL SvxUnoGluePointAccess::getByIdentifier( sal_Int32 Identifier ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
324 if( mpObject.is() && mpObject->IsNode() )
326 struct drawing::GluePoint2 aGluePoint;
328 if( Identifier < NON_USER_DEFINED_GLUE_POINTS ) // default glue point?
330 SdrGluePoint aTempPoint = mpObject->GetVertexGluePoint( (USHORT)Identifier );
331 aGluePoint.IsUserDefined = sal_False;
332 convert( aTempPoint, aGluePoint );
333 return uno::makeAny( aGluePoint );
335 else
337 const USHORT nId = (USHORT)( Identifier - NON_USER_DEFINED_GLUE_POINTS ) + 1;
339 const SdrGluePointList* pList = mpObject->GetGluePointList();
340 const USHORT nCount = pList ? pList->GetCount() : 0;
341 for( USHORT i = 0; i < nCount; i++ )
343 const SdrGluePoint& rTempPoint = (*pList)[i];
344 if( rTempPoint.GetId() == nId )
346 // #i38892#
347 if(rTempPoint.IsUserDefined())
349 aGluePoint.IsUserDefined = sal_True;
352 convert( rTempPoint, aGluePoint );
353 return uno::makeAny( aGluePoint );
359 throw lang::IndexOutOfBoundsException();
362 uno::Sequence< sal_Int32 > SAL_CALL SvxUnoGluePointAccess::getIdentifiers() throw (uno::RuntimeException)
364 if( mpObject.is() )
366 const SdrGluePointList* pList = mpObject->GetGluePointList();
367 const USHORT nCount = pList ? pList->GetCount() : 0;
369 USHORT i;
371 uno::Sequence< sal_Int32 > aIdSequence( nCount + NON_USER_DEFINED_GLUE_POINTS );
372 sal_Int32 *pIdentifier = aIdSequence.getArray();
374 for( i = 0; i < NON_USER_DEFINED_GLUE_POINTS; i++ )
375 *pIdentifier++ = (sal_Int32)i;
377 for( i = 0; i < nCount; i++ )
378 *pIdentifier++ = (sal_Int32) ( (*pList)[i].GetId() + NON_USER_DEFINED_GLUE_POINTS ) - 1;
380 return aIdSequence;
382 else
384 uno::Sequence< sal_Int32 > aEmpty;
385 return aEmpty;
389 /* deprecated */
391 // XIndexContainer
392 void SAL_CALL SvxUnoGluePointAccess::insertByIndex( sal_Int32, const uno::Any& Element )
393 throw(lang::IllegalArgumentException, lang::IndexOutOfBoundsException,
394 lang::WrappedTargetException, uno::RuntimeException)
396 if( mpObject.is() )
398 SdrGluePointList* pList = mpObject->ForceGluePointList();
399 if( pList )
401 SdrGluePoint aSdrGlue;
402 drawing::GluePoint2 aUnoGlue;
404 if( Element >>= aUnoGlue )
406 convert( aUnoGlue, aSdrGlue );
407 pList->Insert( aSdrGlue );
409 // only repaint, no objectchange
410 mpObject->ActionChanged();
411 // mpObject->BroadcastObjectChange();
413 return;
416 throw lang::IllegalArgumentException();
420 throw lang::IndexOutOfBoundsException();
423 void SAL_CALL SvxUnoGluePointAccess::removeByIndex( sal_Int32 Index )
424 throw(lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
426 if( mpObject.is() )
428 SdrGluePointList* pList = mpObject->ForceGluePointList();
429 if( pList )
431 Index -= 4;
432 if( Index >= 0 && Index < pList->GetCount() )
434 pList->Delete( (USHORT)Index );
436 // only repaint, no objectchange
437 mpObject->ActionChanged();
438 // mpObject->BroadcastObjectChange();
440 return;
445 throw lang::IndexOutOfBoundsException();
448 // XIndexReplace
449 void SAL_CALL SvxUnoGluePointAccess::replaceByIndex( sal_Int32 Index, const uno::Any& Element )
450 throw(lang::IllegalArgumentException, lang::IndexOutOfBoundsException, lang::WrappedTargetException,
451 uno::RuntimeException)
453 drawing::GluePoint2 aUnoGlue;
454 if(!(Element >>= aUnoGlue))
455 throw lang::IllegalArgumentException();
457 Index -= 4;
458 if( mpObject.is() && Index >= 0 )
460 SdrGluePointList* pList = const_cast< SdrGluePointList* >( mpObject->GetGluePointList() );
461 if( pList && Index < pList->GetCount() )
463 SdrGluePoint& rGlue = (*pList)[(USHORT)Index];
464 convert( aUnoGlue, rGlue );
466 // only repaint, no objectchange
467 mpObject->ActionChanged();
468 // mpObject->BroadcastObjectChange();
472 throw lang::IndexOutOfBoundsException();
475 // XIndexAccess
476 sal_Int32 SAL_CALL SvxUnoGluePointAccess::getCount()
477 throw(uno::RuntimeException)
479 sal_Int32 nCount = 0;
480 if( mpObject.is() )
482 // each node has a default of 4 glue points
483 // and any number of user defined glue points
484 if( mpObject->IsNode() )
486 nCount += 4;
488 const SdrGluePointList* pList = mpObject->GetGluePointList();
489 if( pList )
490 nCount += pList->GetCount();
494 return nCount;
497 uno::Any SAL_CALL SvxUnoGluePointAccess::getByIndex( sal_Int32 Index )
498 throw(lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
500 if( Index >= 0 && mpObject.is() && mpObject->IsNode() )
502 struct drawing::GluePoint2 aGluePoint;
504 if( Index < 4 ) // default glue point?
506 SdrGluePoint aTempPoint = mpObject->GetVertexGluePoint( (USHORT)Index );
507 aGluePoint.IsUserDefined = sal_False;
508 convert( aTempPoint, aGluePoint );
509 uno::Any aAny;
510 aAny <<= aGluePoint;
511 return aAny;
513 else
515 Index -= 4;
516 const SdrGluePointList* pList = mpObject->GetGluePointList();
517 if( pList && Index < pList->GetCount() )
519 const SdrGluePoint& rTempPoint = (*pList)[(USHORT)Index];
520 aGluePoint.IsUserDefined = sal_True;
521 convert( rTempPoint, aGluePoint );
522 uno::Any aAny;
523 aAny <<= aGluePoint;
524 return aAny;
529 throw lang::IndexOutOfBoundsException();
532 // XElementAccess
533 uno::Type SAL_CALL SvxUnoGluePointAccess::getElementType()
534 throw( uno::RuntimeException)
536 return ::getCppuType((const struct drawing::GluePoint2*)0);
539 sal_Bool SAL_CALL SvxUnoGluePointAccess::hasElements()
540 throw( uno::RuntimeException)
542 return mpObject.is() && mpObject->IsNode();
546 * Create a SvxUnoGluePointAccess
548 uno::Reference< uno::XInterface > SAL_CALL SvxUnoGluePointAccess_createInstance( SdrObject* pObject )
550 return *new SvxUnoGluePointAccess(pObject);