Bump version to 6.4.7.2.M8
[LibreOffice.git] / svx / source / unodraw / gluepts.cxx
blob2b1ba019292e39f6bfbab01f69105034095d82f9
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>
23 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
25 #include <cppuhelper/implbase.hxx>
26 #include <tools/weakbase.hxx>
28 #include <svx/svdmodel.hxx>
29 #include <svx/svdobj.hxx>
30 #include <svx/svdglue.hxx>
31 #include <svx/svdpage.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;
40 class SvxUnoGluePointAccess : public WeakImplHelper< container::XIndexContainer, container::XIdentifierContainer >
42 private:
43 tools::WeakReference<SdrObject> mpObject;
45 public:
46 explicit SvxUnoGluePointAccess( SdrObject* pObject ) throw();
48 // XIdentifierContainer
49 virtual sal_Int32 SAL_CALL insert( const uno::Any& aElement ) override;
50 virtual void SAL_CALL removeByIdentifier( sal_Int32 Identifier ) override;
52 // XIdentifierReplace
53 virtual void SAL_CALL replaceByIdentifer( sal_Int32 Identifier, const uno::Any& aElement ) override;
55 // XIdentifierReplace
56 virtual uno::Any SAL_CALL getByIdentifier( sal_Int32 Identifier ) override;
57 virtual uno::Sequence< sal_Int32 > SAL_CALL getIdentifiers( ) override;
59 /* deprecated */
60 // XIndexContainer
61 virtual void SAL_CALL insertByIndex( sal_Int32 Index, const uno::Any& Element ) override;
62 virtual void SAL_CALL removeByIndex( sal_Int32 Index ) override;
64 /* deprecated */
65 // XIndexReplace
66 virtual void SAL_CALL replaceByIndex( sal_Int32 Index, const uno::Any& Element ) override;
68 /* deprecated */
69 // XIndexAccess
70 virtual sal_Int32 SAL_CALL getCount( ) override;
71 virtual uno::Any SAL_CALL getByIndex( sal_Int32 Index ) override;
73 // XElementAccess
74 virtual uno::Type SAL_CALL getElementType( ) override;
75 virtual sal_Bool SAL_CALL hasElements( ) override;
78 static void convert( const SdrGluePoint& rSdrGlue, drawing::GluePoint2& rUnoGlue ) throw()
80 rUnoGlue.Position.X = rSdrGlue.GetPos().X();
81 rUnoGlue.Position.Y = rSdrGlue.GetPos().Y();
82 rUnoGlue.IsRelative = rSdrGlue.IsPercent();
84 SdrAlign eAlign = rSdrGlue.GetAlign();
85 if (eAlign == (SdrAlign::VERT_TOP|SdrAlign::HORZ_LEFT))
86 rUnoGlue.PositionAlignment = drawing::Alignment_TOP_LEFT;
87 else if (eAlign == (SdrAlign::HORZ_CENTER|SdrAlign::VERT_TOP))
88 rUnoGlue.PositionAlignment = drawing::Alignment_TOP;
89 else if (eAlign == (SdrAlign::VERT_TOP|SdrAlign::HORZ_RIGHT))
90 rUnoGlue.PositionAlignment = drawing::Alignment_TOP_RIGHT;
91 else if (eAlign == (SdrAlign::HORZ_CENTER|SdrAlign::VERT_CENTER))
92 rUnoGlue.PositionAlignment = drawing::Alignment_CENTER;
93 else if (eAlign == (SdrAlign::HORZ_RIGHT|SdrAlign::VERT_CENTER))
94 rUnoGlue.PositionAlignment = drawing::Alignment_RIGHT;
95 else if (eAlign == (SdrAlign::HORZ_LEFT|SdrAlign::VERT_BOTTOM))
96 rUnoGlue.PositionAlignment = drawing::Alignment_BOTTOM_LEFT;
97 else if (eAlign == (SdrAlign::HORZ_CENTER|SdrAlign::VERT_BOTTOM))
98 rUnoGlue.PositionAlignment = drawing::Alignment_BOTTOM;
99 else if (eAlign == (SdrAlign::HORZ_RIGHT|SdrAlign::VERT_BOTTOM))
100 rUnoGlue.PositionAlignment = drawing::Alignment_BOTTOM_RIGHT;
101 else {
102 rUnoGlue.PositionAlignment = drawing::Alignment_LEFT;
105 switch( rSdrGlue.GetEscDir() )
107 case SdrEscapeDirection::LEFT:
108 rUnoGlue.Escape = drawing::EscapeDirection_LEFT;
109 break;
110 case SdrEscapeDirection::RIGHT:
111 rUnoGlue.Escape = drawing::EscapeDirection_RIGHT;
112 break;
113 case SdrEscapeDirection::TOP:
114 rUnoGlue.Escape = drawing::EscapeDirection_UP;
115 break;
116 case SdrEscapeDirection::BOTTOM:
117 rUnoGlue.Escape = drawing::EscapeDirection_DOWN;
118 break;
119 case SdrEscapeDirection::HORZ:
120 rUnoGlue.Escape = drawing::EscapeDirection_HORIZONTAL;
121 break;
122 case SdrEscapeDirection::VERT:
123 rUnoGlue.Escape = drawing::EscapeDirection_VERTICAL;
124 break;
125 // case SdrEscapeDirection::SMART:
126 default:
127 rUnoGlue.Escape = drawing::EscapeDirection_SMART;
128 break;
132 static void convert( const drawing::GluePoint2& rUnoGlue, SdrGluePoint& rSdrGlue ) throw()
134 rSdrGlue.SetPos( Point( rUnoGlue.Position.X, rUnoGlue.Position.Y ) );
135 rSdrGlue.SetPercent( rUnoGlue.IsRelative );
137 switch( rUnoGlue.PositionAlignment )
139 case drawing::Alignment_TOP_LEFT:
140 rSdrGlue.SetAlign( SdrAlign::VERT_TOP|SdrAlign::HORZ_LEFT );
141 break;
142 case drawing::Alignment_TOP:
143 rSdrGlue.SetAlign( SdrAlign::HORZ_CENTER|SdrAlign::VERT_TOP );
144 break;
145 case drawing::Alignment_TOP_RIGHT:
146 rSdrGlue.SetAlign( SdrAlign::VERT_TOP|SdrAlign::HORZ_RIGHT );
147 break;
148 case drawing::Alignment_CENTER:
149 rSdrGlue.SetAlign( SdrAlign::HORZ_CENTER|SdrAlign::VERT_CENTER );
150 break;
151 case drawing::Alignment_RIGHT:
152 rSdrGlue.SetAlign( SdrAlign::HORZ_RIGHT|SdrAlign::VERT_CENTER );
153 break;
154 case drawing::Alignment_BOTTOM_LEFT:
155 rSdrGlue.SetAlign( SdrAlign::HORZ_LEFT|SdrAlign::VERT_BOTTOM );
156 break;
157 case drawing::Alignment_BOTTOM:
158 rSdrGlue.SetAlign( SdrAlign::HORZ_CENTER|SdrAlign::VERT_BOTTOM );
159 break;
160 case drawing::Alignment_BOTTOM_RIGHT:
161 rSdrGlue.SetAlign( SdrAlign::HORZ_RIGHT|SdrAlign::VERT_BOTTOM );
162 break;
163 // case SdrAlign::HORZ_LEFT:
164 default:
165 rSdrGlue.SetAlign( SdrAlign::HORZ_LEFT );
166 break;
168 switch( rUnoGlue.Escape )
170 case drawing::EscapeDirection_LEFT:
171 rSdrGlue.SetEscDir(SdrEscapeDirection::LEFT);
172 break;
173 case drawing::EscapeDirection_RIGHT:
174 rSdrGlue.SetEscDir(SdrEscapeDirection::RIGHT);
175 break;
176 case drawing::EscapeDirection_UP:
177 rSdrGlue.SetEscDir(SdrEscapeDirection::TOP);
178 break;
179 case drawing::EscapeDirection_DOWN:
180 rSdrGlue.SetEscDir(SdrEscapeDirection::BOTTOM);
181 break;
182 case drawing::EscapeDirection_HORIZONTAL:
183 rSdrGlue.SetEscDir(SdrEscapeDirection::HORZ);
184 break;
185 case drawing::EscapeDirection_VERTICAL:
186 rSdrGlue.SetEscDir(SdrEscapeDirection::VERT);
187 break;
188 // case drawing::EscapeDirection_SMART:
189 default:
190 rSdrGlue.SetEscDir(SdrEscapeDirection::SMART);
191 break;
195 SvxUnoGluePointAccess::SvxUnoGluePointAccess( SdrObject* pObject ) throw()
196 : mpObject( pObject )
200 // XIdentifierContainer
201 sal_Int32 SAL_CALL SvxUnoGluePointAccess::insert( const uno::Any& aElement )
203 if( mpObject.is() )
205 SdrGluePointList* pList = mpObject->ForceGluePointList();
206 if( pList )
208 // second, insert the new glue point
209 drawing::GluePoint2 aUnoGlue;
211 if( aElement >>= aUnoGlue )
213 SdrGluePoint aSdrGlue;
214 convert( aUnoGlue, aSdrGlue );
215 sal_uInt16 nId = pList->Insert( aSdrGlue );
217 // only repaint, no objectchange
218 mpObject->ActionChanged();
219 // mpObject->BroadcastObjectChange();
221 return static_cast<sal_Int32>((*pList)[nId].GetId() + NON_USER_DEFINED_GLUE_POINTS) - 1;
224 throw lang::IllegalArgumentException();
228 return -1;
231 void SAL_CALL SvxUnoGluePointAccess::removeByIdentifier( sal_Int32 Identifier )
233 if( mpObject.is() && ( Identifier >= NON_USER_DEFINED_GLUE_POINTS ))
235 const sal_uInt16 nId = static_cast<sal_uInt16>(Identifier - NON_USER_DEFINED_GLUE_POINTS) + 1;
237 SdrGluePointList* pList = const_cast<SdrGluePointList*>(mpObject->GetGluePointList());
238 const sal_uInt16 nCount = pList ? pList->GetCount() : 0;
239 sal_uInt16 i;
241 for( i = 0; i < nCount; i++ )
243 if( (*pList)[i].GetId() == nId )
245 pList->Delete( i );
247 // only repaint, no objectchange
248 mpObject->ActionChanged();
249 // mpObject->BroadcastObjectChange();
251 return;
256 throw container::NoSuchElementException();
259 // XIdentifierReplace
260 void SAL_CALL SvxUnoGluePointAccess::replaceByIdentifer( sal_Int32 Identifier, const uno::Any& aElement )
262 if( mpObject.is() )
264 struct drawing::GluePoint2 aGluePoint;
265 if( (Identifier < NON_USER_DEFINED_GLUE_POINTS) || !(aElement >>= aGluePoint))
266 throw lang::IllegalArgumentException();
268 const sal_uInt16 nId = static_cast<sal_uInt16>( Identifier - NON_USER_DEFINED_GLUE_POINTS ) + 1;
270 SdrGluePointList* pList = const_cast< SdrGluePointList* >( mpObject->GetGluePointList() );
271 const sal_uInt16 nCount = pList ? pList->GetCount() : 0;
272 sal_uInt16 i;
273 for( i = 0; i < nCount; i++ )
275 if( (*pList)[i].GetId() == nId )
277 // change the glue point
278 SdrGluePoint& rTempPoint = (*pList)[i];
279 convert( aGluePoint, rTempPoint );
281 // only repaint, no objectchange
282 mpObject->ActionChanged();
283 // mpObject->BroadcastObjectChange();
285 return;
289 throw container::NoSuchElementException();
293 // XIdentifierAccess
294 uno::Any SAL_CALL SvxUnoGluePointAccess::getByIdentifier( sal_Int32 Identifier )
296 if( mpObject.is() )
298 struct drawing::GluePoint2 aGluePoint;
300 if( Identifier < NON_USER_DEFINED_GLUE_POINTS ) // default glue point?
302 SdrGluePoint aTempPoint = mpObject->GetVertexGluePoint( static_cast<sal_uInt16>(Identifier) );
303 aGluePoint.IsUserDefined = false;
304 convert( aTempPoint, aGluePoint );
305 return uno::makeAny( aGluePoint );
307 else
309 const sal_uInt16 nId = static_cast<sal_uInt16>( Identifier - NON_USER_DEFINED_GLUE_POINTS ) + 1;
311 const SdrGluePointList* pList = mpObject->GetGluePointList();
312 const sal_uInt16 nCount = pList ? pList->GetCount() : 0;
313 for( sal_uInt16 i = 0; i < nCount; i++ )
315 const SdrGluePoint& rTempPoint = (*pList)[i];
316 if( rTempPoint.GetId() == nId )
318 // #i38892#
319 if(rTempPoint.IsUserDefined())
321 aGluePoint.IsUserDefined = true;
324 convert( rTempPoint, aGluePoint );
325 return uno::makeAny( aGluePoint );
331 throw container::NoSuchElementException();
334 uno::Sequence< sal_Int32 > SAL_CALL SvxUnoGluePointAccess::getIdentifiers()
336 if( mpObject.is() )
338 const SdrGluePointList* pList = mpObject->GetGluePointList();
339 const sal_uInt16 nCount = pList ? pList->GetCount() : 0;
341 sal_uInt16 i;
343 uno::Sequence< sal_Int32 > aIdSequence( nCount + NON_USER_DEFINED_GLUE_POINTS );
344 sal_Int32 *pIdentifier = aIdSequence.getArray();
346 for( i = 0; i < NON_USER_DEFINED_GLUE_POINTS; i++ )
347 *pIdentifier++ = static_cast<sal_Int32>(i);
349 for( i = 0; i < nCount; i++ )
350 *pIdentifier++ = static_cast<sal_Int32>( (*pList)[i].GetId() + NON_USER_DEFINED_GLUE_POINTS ) - 1;
352 return aIdSequence;
354 else
356 uno::Sequence< sal_Int32 > aEmpty;
357 return aEmpty;
361 /* deprecated */
363 // XIndexContainer
364 void SAL_CALL SvxUnoGluePointAccess::insertByIndex( sal_Int32, const uno::Any& Element )
366 if( mpObject.is() )
368 SdrGluePointList* pList = mpObject->ForceGluePointList();
369 if( pList )
371 SdrGluePoint aSdrGlue;
372 drawing::GluePoint2 aUnoGlue;
374 if( Element >>= aUnoGlue )
376 convert( aUnoGlue, aSdrGlue );
377 pList->Insert( aSdrGlue );
379 // only repaint, no objectchange
380 mpObject->ActionChanged();
381 // mpObject->BroadcastObjectChange();
383 return;
386 throw lang::IllegalArgumentException();
390 throw lang::IndexOutOfBoundsException();
393 void SAL_CALL SvxUnoGluePointAccess::removeByIndex( sal_Int32 Index )
395 if( mpObject.is() )
397 SdrGluePointList* pList = mpObject->ForceGluePointList();
398 if( pList )
400 Index -= 4;
401 if( Index >= 0 && Index < pList->GetCount() )
403 pList->Delete( static_cast<sal_uInt16>(Index) );
405 // only repaint, no objectchange
406 mpObject->ActionChanged();
407 // mpObject->BroadcastObjectChange();
409 return;
414 throw lang::IndexOutOfBoundsException();
417 // XIndexReplace
418 void SAL_CALL SvxUnoGluePointAccess::replaceByIndex( sal_Int32 Index, const uno::Any& Element )
420 drawing::GluePoint2 aUnoGlue;
421 if(!(Element >>= aUnoGlue))
422 throw lang::IllegalArgumentException();
424 Index -= 4;
425 if( mpObject.is() && Index >= 0 )
427 SdrGluePointList* pList = const_cast< SdrGluePointList* >( mpObject->GetGluePointList() );
428 if( pList && Index < pList->GetCount() )
430 SdrGluePoint& rGlue = (*pList)[static_cast<sal_uInt16>(Index)];
431 convert( aUnoGlue, rGlue );
433 // only repaint, no objectchange
434 mpObject->ActionChanged();
435 // mpObject->BroadcastObjectChange();
439 throw lang::IndexOutOfBoundsException();
442 // XIndexAccess
443 sal_Int32 SAL_CALL SvxUnoGluePointAccess::getCount()
445 sal_Int32 nCount = 0;
446 if( mpObject.is() )
448 // each node has a default of 4 glue points
449 // and any number of user defined glue points
450 nCount += 4;
452 const SdrGluePointList* pList = mpObject->GetGluePointList();
453 if( pList )
454 nCount += pList->GetCount();
457 return nCount;
460 uno::Any SAL_CALL SvxUnoGluePointAccess::getByIndex( sal_Int32 Index )
462 if( Index >= 0 && mpObject.is() )
464 struct drawing::GluePoint2 aGluePoint;
466 if( Index < 4 ) // default glue point?
468 SdrGluePoint aTempPoint = mpObject->GetVertexGluePoint( static_cast<sal_uInt16>(Index) );
469 aGluePoint.IsUserDefined = false;
470 convert( aTempPoint, aGluePoint );
471 return uno::Any(aGluePoint);
473 else
475 Index -= 4;
476 const SdrGluePointList* pList = mpObject->GetGluePointList();
477 if( pList && Index < pList->GetCount() )
479 const SdrGluePoint& rTempPoint = (*pList)[static_cast<sal_uInt16>(Index)];
480 aGluePoint.IsUserDefined = true;
481 convert( rTempPoint, aGluePoint );
482 return uno::Any(aGluePoint);
487 throw lang::IndexOutOfBoundsException();
490 // XElementAccess
491 uno::Type SAL_CALL SvxUnoGluePointAccess::getElementType()
493 return cppu::UnoType<drawing::GluePoint2>::get();
496 sal_Bool SAL_CALL SvxUnoGluePointAccess::hasElements()
498 return mpObject.is();
502 * Create a SvxUnoGluePointAccess
504 uno::Reference< uno::XInterface > SvxUnoGluePointAccess_createInstance( SdrObject* pObject )
506 return *new SvxUnoGluePointAccess(pObject);
509 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */