LibreOffice Module svx (master) 1
gluepts.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <com/sun/star/container/NoSuchElementException.hpp>
21#include <com/sun/star/container/XIdentifierContainer.hpp>
22#include <com/sun/star/container/XIndexContainer.hpp>
23#include <com/sun/star/drawing/GluePoint2.hpp>
24#include <com/sun/star/lang/IllegalArgumentException.hpp>
25#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
26
28#include <tools/weakbase.hxx>
29
30#include <svx/svdobj.hxx>
31#include <svx/svdglue.hxx>
32
33#include "gluepts.hxx"
34
35using namespace ::com::sun::star;
36using namespace ::cppu;
37
38const sal_uInt16 NON_USER_DEFINED_GLUE_POINTS = 4;
39
40namespace {
41
42class SvxUnoGluePointAccess : public WeakImplHelper< container::XIndexContainer, container::XIdentifierContainer >
43{
44private:
46
47public:
48 explicit SvxUnoGluePointAccess( SdrObject* pObject ) noexcept;
49
50 // XIdentifierContainer
51 virtual sal_Int32 SAL_CALL insert( const uno::Any& aElement ) override;
52 virtual void SAL_CALL removeByIdentifier( sal_Int32 Identifier ) override;
53
54 // XIdentifierReplace
55 virtual void SAL_CALL replaceByIdentifer( sal_Int32 Identifier, const uno::Any& aElement ) override;
56
57 // XIdentifierReplace
58 virtual uno::Any SAL_CALL getByIdentifier( sal_Int32 Identifier ) override;
59 virtual uno::Sequence< sal_Int32 > SAL_CALL getIdentifiers( ) override;
60
61 /* deprecated */
62 // XIndexContainer
63 virtual void SAL_CALL insertByIndex( sal_Int32 Index, const uno::Any& Element ) override;
64 virtual void SAL_CALL removeByIndex( sal_Int32 Index ) override;
65
66 /* deprecated */
67 // XIndexReplace
68 virtual void SAL_CALL replaceByIndex( sal_Int32 Index, const uno::Any& Element ) override;
69
70 /* deprecated */
71 // XIndexAccess
72 virtual sal_Int32 SAL_CALL getCount( ) override;
73 virtual uno::Any SAL_CALL getByIndex( sal_Int32 Index ) override;
74
75 // XElementAccess
76 virtual uno::Type SAL_CALL getElementType( ) override;
77 virtual sal_Bool SAL_CALL hasElements( ) override;
78};
79
80}
81
82static void convert( const SdrGluePoint& rSdrGlue, drawing::GluePoint2& rUnoGlue ) noexcept
83{
84 rUnoGlue.Position.X = rSdrGlue.GetPos().X();
85 rUnoGlue.Position.Y = rSdrGlue.GetPos().Y();
86 rUnoGlue.IsRelative = rSdrGlue.IsPercent();
87
88 SdrAlign eAlign = rSdrGlue.GetAlign();
90 rUnoGlue.PositionAlignment = drawing::Alignment_TOP_LEFT;
91 else if (eAlign == (SdrAlign::HORZ_CENTER|SdrAlign::VERT_TOP))
92 rUnoGlue.PositionAlignment = drawing::Alignment_TOP;
93 else if (eAlign == (SdrAlign::VERT_TOP|SdrAlign::HORZ_RIGHT))
94 rUnoGlue.PositionAlignment = drawing::Alignment_TOP_RIGHT;
96 rUnoGlue.PositionAlignment = drawing::Alignment_CENTER;
98 rUnoGlue.PositionAlignment = drawing::Alignment_RIGHT;
99 else if (eAlign == (SdrAlign::HORZ_LEFT|SdrAlign::VERT_BOTTOM))
100 rUnoGlue.PositionAlignment = drawing::Alignment_BOTTOM_LEFT;
101 else if (eAlign == (SdrAlign::HORZ_CENTER|SdrAlign::VERT_BOTTOM))
102 rUnoGlue.PositionAlignment = drawing::Alignment_BOTTOM;
103 else if (eAlign == (SdrAlign::HORZ_RIGHT|SdrAlign::VERT_BOTTOM))
104 rUnoGlue.PositionAlignment = drawing::Alignment_BOTTOM_RIGHT;
105 else {
106 rUnoGlue.PositionAlignment = drawing::Alignment_LEFT;
107 }
108
109 switch( rSdrGlue.GetEscDir() )
110 {
112 rUnoGlue.Escape = drawing::EscapeDirection_LEFT;
113 break;
115 rUnoGlue.Escape = drawing::EscapeDirection_RIGHT;
116 break;
118 rUnoGlue.Escape = drawing::EscapeDirection_UP;
119 break;
121 rUnoGlue.Escape = drawing::EscapeDirection_DOWN;
122 break;
124 rUnoGlue.Escape = drawing::EscapeDirection_HORIZONTAL;
125 break;
127 rUnoGlue.Escape = drawing::EscapeDirection_VERTICAL;
128 break;
129// case SdrEscapeDirection::SMART:
130 default:
131 rUnoGlue.Escape = drawing::EscapeDirection_SMART;
132 break;
133 }
134}
135
136static void convert( const drawing::GluePoint2& rUnoGlue, SdrGluePoint& rSdrGlue ) noexcept
137{
138 rSdrGlue.SetPos( Point( rUnoGlue.Position.X, rUnoGlue.Position.Y ) );
139 rSdrGlue.SetPercent( rUnoGlue.IsRelative );
140
141 switch( rUnoGlue.PositionAlignment )
142 {
143 case drawing::Alignment_TOP_LEFT:
144 rSdrGlue.SetAlign( SdrAlign::VERT_TOP|SdrAlign::HORZ_LEFT );
145 break;
146 case drawing::Alignment_TOP:
147 rSdrGlue.SetAlign( SdrAlign::HORZ_CENTER|SdrAlign::VERT_TOP );
148 break;
149 case drawing::Alignment_TOP_RIGHT:
150 rSdrGlue.SetAlign( SdrAlign::VERT_TOP|SdrAlign::HORZ_RIGHT );
151 break;
152 case drawing::Alignment_CENTER:
153 rSdrGlue.SetAlign( SdrAlign::HORZ_CENTER|SdrAlign::VERT_CENTER );
154 break;
155 case drawing::Alignment_RIGHT:
156 rSdrGlue.SetAlign( SdrAlign::HORZ_RIGHT|SdrAlign::VERT_CENTER );
157 break;
158 case drawing::Alignment_BOTTOM_LEFT:
159 rSdrGlue.SetAlign( SdrAlign::HORZ_LEFT|SdrAlign::VERT_BOTTOM );
160 break;
161 case drawing::Alignment_BOTTOM:
162 rSdrGlue.SetAlign( SdrAlign::HORZ_CENTER|SdrAlign::VERT_BOTTOM );
163 break;
164 case drawing::Alignment_BOTTOM_RIGHT:
165 rSdrGlue.SetAlign( SdrAlign::HORZ_RIGHT|SdrAlign::VERT_BOTTOM );
166 break;
167// case SdrAlign::HORZ_LEFT:
168 default:
169 rSdrGlue.SetAlign( SdrAlign::HORZ_LEFT );
170 break;
171 }
172 switch( rUnoGlue.Escape )
173 {
174 case drawing::EscapeDirection_LEFT:
175 rSdrGlue.SetEscDir(SdrEscapeDirection::LEFT);
176 break;
177 case drawing::EscapeDirection_RIGHT:
178 rSdrGlue.SetEscDir(SdrEscapeDirection::RIGHT);
179 break;
180 case drawing::EscapeDirection_UP:
181 rSdrGlue.SetEscDir(SdrEscapeDirection::TOP);
182 break;
183 case drawing::EscapeDirection_DOWN:
184 rSdrGlue.SetEscDir(SdrEscapeDirection::BOTTOM);
185 break;
186 case drawing::EscapeDirection_HORIZONTAL:
187 rSdrGlue.SetEscDir(SdrEscapeDirection::HORZ);
188 break;
189 case drawing::EscapeDirection_VERTICAL:
190 rSdrGlue.SetEscDir(SdrEscapeDirection::VERT);
191 break;
192// case drawing::EscapeDirection_SMART:
193 default:
194 rSdrGlue.SetEscDir(SdrEscapeDirection::SMART);
195 break;
196 }
197}
198
199SvxUnoGluePointAccess::SvxUnoGluePointAccess( SdrObject* pObject ) noexcept
200: mpObject( pObject )
201{
202}
203
204// XIdentifierContainer
205sal_Int32 SAL_CALL SvxUnoGluePointAccess::insert( const uno::Any& aElement )
206{
207 if( auto pObject = mpObject.get() )
208 {
209 SdrGluePointList* pList = pObject->ForceGluePointList();
210 if( pList )
211 {
212 // second, insert the new gluepoint
213 drawing::GluePoint2 aUnoGlue;
214
215 if( aElement >>= aUnoGlue )
216 {
217 SdrGluePoint aSdrGlue;
218 convert( aUnoGlue, aSdrGlue );
219 sal_uInt16 nId = pList->Insert( aSdrGlue );
220
221 // only repaint, no objectchange
222 pObject->ActionChanged();
223 // mpObject->BroadcastObjectChange();
224
225 return static_cast<sal_Int32>((*pList)[nId].GetId() + NON_USER_DEFINED_GLUE_POINTS) - 1;
226 }
227
228 throw lang::IllegalArgumentException();
229 }
230 }
231
232 return -1;
233}
234
235void SAL_CALL SvxUnoGluePointAccess::removeByIdentifier( sal_Int32 Identifier )
236{
237 auto pObject = mpObject.get();
238 if( pObject && ( Identifier >= NON_USER_DEFINED_GLUE_POINTS ))
239 {
240 const sal_uInt16 nId = static_cast<sal_uInt16>(Identifier - NON_USER_DEFINED_GLUE_POINTS) + 1;
241
242 SdrGluePointList* pList = const_cast<SdrGluePointList*>(pObject->GetGluePointList());
243 const sal_uInt16 nCount = pList ? pList->GetCount() : 0;
244 sal_uInt16 i;
245
246 for( i = 0; i < nCount; i++ )
247 {
248 if( (*pList)[i].GetId() == nId )
249 {
250 pList->Delete( i );
251
252 // only repaint, no objectchange
253 pObject->ActionChanged();
254 // pObject->BroadcastObjectChange();
255
256 return;
257 }
258 }
259 }
260
261 throw container::NoSuchElementException();
262}
263
264// XIdentifierReplace
265void SAL_CALL SvxUnoGluePointAccess::replaceByIdentifer( sal_Int32 Identifier, const uno::Any& aElement )
266{
267 auto pObject = mpObject.get();
268 if( !pObject )
269 return;
270
271 struct drawing::GluePoint2 aGluePoint;
272 if( (Identifier < NON_USER_DEFINED_GLUE_POINTS) || !(aElement >>= aGluePoint))
273 throw lang::IllegalArgumentException();
274
275 const sal_uInt16 nId = static_cast<sal_uInt16>( Identifier - NON_USER_DEFINED_GLUE_POINTS ) + 1;
276
277 SdrGluePointList* pList = const_cast< SdrGluePointList* >( pObject->GetGluePointList() );
278 const sal_uInt16 nCount = pList ? pList->GetCount() : 0;
279 sal_uInt16 i;
280 for( i = 0; i < nCount; i++ )
281 {
282 if( (*pList)[i].GetId() == nId )
283 {
284 // change the gluepoint
285 SdrGluePoint& rTempPoint = (*pList)[i];
286 convert( aGluePoint, rTempPoint );
287
288 // only repaint, no objectchange
289 pObject->ActionChanged();
290 // pObject->BroadcastObjectChange();
291
292 return;
293 }
294 }
295
296 throw container::NoSuchElementException();
297}
298
299// XIdentifierAccess
300uno::Any SAL_CALL SvxUnoGluePointAccess::getByIdentifier( sal_Int32 Identifier )
301{
302 auto pObject = mpObject.get();
303 if( pObject )
304 {
305 struct drawing::GluePoint2 aGluePoint;
306
307 if( Identifier < NON_USER_DEFINED_GLUE_POINTS ) // default gluepoint?
308 {
309 SdrGluePoint aTempPoint = pObject->GetVertexGluePoint( static_cast<sal_uInt16>(Identifier) );
310 aGluePoint.IsUserDefined = false;
311 convert( aTempPoint, aGluePoint );
312 return uno::Any( aGluePoint );
313 }
314 else
315 {
316 const sal_uInt16 nId = static_cast<sal_uInt16>( Identifier - NON_USER_DEFINED_GLUE_POINTS ) + 1;
317
318 const SdrGluePointList* pList = pObject->GetGluePointList();
319 const sal_uInt16 nCount = pList ? pList->GetCount() : 0;
320 for( sal_uInt16 i = 0; i < nCount; i++ )
321 {
322 const SdrGluePoint& rTempPoint = (*pList)[i];
323 if( rTempPoint.GetId() == nId )
324 {
325 // #i38892#
326 if(rTempPoint.IsUserDefined())
327 {
328 aGluePoint.IsUserDefined = true;
329 }
330
331 convert( rTempPoint, aGluePoint );
332 return uno::Any( aGluePoint );
333 }
334 }
335 }
336 }
337
338 throw container::NoSuchElementException();
339}
340
341uno::Sequence< sal_Int32 > SAL_CALL SvxUnoGluePointAccess::getIdentifiers()
342{
343 auto pObject = mpObject.get();
344 if( pObject )
345 {
346 const SdrGluePointList* pList = pObject->GetGluePointList();
347 const sal_uInt16 nCount = pList ? pList->GetCount() : 0;
348
349 sal_uInt16 i;
350
351 uno::Sequence< sal_Int32 > aIdSequence( nCount + NON_USER_DEFINED_GLUE_POINTS );
352 sal_Int32 *pIdentifier = aIdSequence.getArray();
353
354 for( i = 0; i < NON_USER_DEFINED_GLUE_POINTS; i++ )
355 *pIdentifier++ = static_cast<sal_Int32>(i);
356
357 for( i = 0; i < nCount; i++ )
358 *pIdentifier++ = static_cast<sal_Int32>( (*pList)[i].GetId() + NON_USER_DEFINED_GLUE_POINTS ) - 1;
359
360 return aIdSequence;
361 }
362 else
363 {
364 uno::Sequence< sal_Int32 > aEmpty;
365 return aEmpty;
366 }
367}
368
369/* deprecated */
370
371// XIndexContainer
372void SAL_CALL SvxUnoGluePointAccess::insertByIndex( sal_Int32, const uno::Any& Element )
373{
374 auto pObject = mpObject.get();
375 if( pObject )
376 {
377 SdrGluePointList* pList = pObject->ForceGluePointList();
378 if( pList )
379 {
380 drawing::GluePoint2 aUnoGlue;
381
382 if( Element >>= aUnoGlue )
383 {
384 SdrGluePoint aSdrGlue;
385 convert( aUnoGlue, aSdrGlue );
386 pList->Insert( aSdrGlue );
387
388 // only repaint, no objectchange
389 pObject->ActionChanged();
390 // pObject->BroadcastObjectChange();
391
392 return;
393 }
394
395 throw lang::IllegalArgumentException();
396 }
397 }
398
399 throw lang::IndexOutOfBoundsException();
400}
401
402void SAL_CALL SvxUnoGluePointAccess::removeByIndex( sal_Int32 Index )
403{
404 auto pObject = mpObject.get();
405 if( pObject )
406 {
407 SdrGluePointList* pList = pObject->ForceGluePointList();
408 if( pList )
409 {
410 Index -= 4;
411 if( Index >= 0 && Index < pList->GetCount() )
412 {
413 pList->Delete( static_cast<sal_uInt16>(Index) );
414
415 // only repaint, no objectchange
416 pObject->ActionChanged();
417 // pObject->BroadcastObjectChange();
418
419 return;
420 }
421 }
422 }
423
424 throw lang::IndexOutOfBoundsException();
425}
426
427// XIndexReplace
428void SAL_CALL SvxUnoGluePointAccess::replaceByIndex( sal_Int32 Index, const uno::Any& Element )
429{
430 drawing::GluePoint2 aUnoGlue;
431 if(!(Element >>= aUnoGlue))
432 throw lang::IllegalArgumentException();
433
434 auto pObject = mpObject.get();
435 Index -= 4;
436 if( pObject && Index >= 0 )
437 {
438 SdrGluePointList* pList = const_cast< SdrGluePointList* >( pObject->GetGluePointList() );
439 if( pList && Index < pList->GetCount() )
440 {
441 SdrGluePoint& rGlue = (*pList)[static_cast<sal_uInt16>(Index)];
442 convert( aUnoGlue, rGlue );
443
444 // only repaint, no objectchange
445 pObject->ActionChanged();
446 // pObject->BroadcastObjectChange();
447 }
448 }
449
450 throw lang::IndexOutOfBoundsException();
451}
452
453// XIndexAccess
454sal_Int32 SAL_CALL SvxUnoGluePointAccess::getCount()
455{
456 auto pObject = mpObject.get();
457 sal_Int32 nCount = 0;
458 if( pObject )
459 {
460 // each node has a default of 4 gluepoints
461 // and any number of user defined gluepoints
462 nCount += 4;
463
464 const SdrGluePointList* pList = pObject->GetGluePointList();
465 if( pList )
466 nCount += pList->GetCount();
467 }
468
469 return nCount;
470}
471
472uno::Any SAL_CALL SvxUnoGluePointAccess::getByIndex( sal_Int32 Index )
473{
474 auto pObject = mpObject.get();
475 if( Index >= 0 && pObject )
476 {
477 struct drawing::GluePoint2 aGluePoint;
478
479 if( Index < 4 ) // default gluepoint?
480 {
481 SdrGluePoint aTempPoint = pObject->GetVertexGluePoint( static_cast<sal_uInt16>(Index) );
482 aGluePoint.IsUserDefined = false;
483 convert( aTempPoint, aGluePoint );
484 return uno::Any(aGluePoint);
485 }
486 else
487 {
488 Index -= 4;
489 const SdrGluePointList* pList = pObject->GetGluePointList();
490 if( pList && Index < pList->GetCount() )
491 {
492 const SdrGluePoint& rTempPoint = (*pList)[static_cast<sal_uInt16>(Index)];
493 aGluePoint.IsUserDefined = true;
494 convert( rTempPoint, aGluePoint );
495 return uno::Any(aGluePoint);
496 }
497 }
498 }
499
500 throw lang::IndexOutOfBoundsException();
501}
502
503// XElementAccess
504uno::Type SAL_CALL SvxUnoGluePointAccess::getElementType()
505{
507}
508
509sal_Bool SAL_CALL SvxUnoGluePointAccess::hasElements()
510{
511 return bool(mpObject.get());
512}
513
517uno::Reference< uno::XInterface > SvxUnoGluePointAccess_createInstance( SdrObject* pObject )
518{
519 return *new SvxUnoGluePointAccess(pObject);
520}
521
522/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOIndex Index
SbxObjectRef mpObject
sal_uInt16 Insert(const SdrGluePoint &rGP)
Definition: svdglue.cxx:295
void Delete(sal_uInt16 nPos)
Definition: svdglue.hxx:201
sal_uInt16 GetCount() const
Definition: svdglue.hxx:194
sal_uInt16 GetId() const
Definition: svdglue.hxx:112
bool IsUserDefined() const
Definition: svdglue.hxx:132
Abstract DrawObject.
Definition: svdobj.hxx:261
css::uno::Type const & get()
T * get() const
int nCount
virtual sal_uInt32 GetId() const override
EmbeddedObjectRef * pObject
const sal_uInt16 NON_USER_DEFINED_GLUE_POINTS
Definition: gluepts.cxx:38
uno::Reference< uno::XInterface > SvxUnoGluePointAccess_createInstance(SdrObject *pObject)
Create a SvxUnoGluePointAccess.
Definition: gluepts.cxx:517
static void convert(const SdrGluePoint &rSdrGlue, drawing::GluePoint2 &rUnoGlue) noexcept
Definition: gluepts.cxx:82
int i
sal_Int16 nId
SdrAlign
Definition: svdglue.hxx:51
unsigned char sal_Bool