LibreOffice Module svx (master)  1
svdouno.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 
22 #include <com/sun/star/container/XChild.hpp>
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <com/sun/star/util/XCloneable.hpp>
25 #include <com/sun/star/uno/XComponentContext.hpp>
27 #include <svx/svdouno.hxx>
28 #include <svx/svdpagv.hxx>
29 #include <svx/svdmodel.hxx>
30 #include <svx/dialmgr.hxx>
31 #include <svx/strings.hrc>
32 #include <svx/svdview.hxx>
33 #include <svx/svdorect.hxx>
34 #include <svx/svdviter.hxx>
35 #include <rtl/ref.hxx>
36 #include <svx/sdrpagewindow.hxx>
37 #include <tools/diagnose_ex.h>
38 #include <tools/debug.hxx>
39 #include <o3tl/sorted_vector.hxx>
40 
41 using namespace ::com::sun::star;
42 using namespace sdr::contact;
43 
44 
45 // Defines
46 
47 
48 // Helper class SdrControlEventListenerImpl
49 
50 #include <com/sun/star/lang/XEventListener.hpp>
51 
52 #include <cppuhelper/implbase.hxx>
53 
54 
55 class SdrControlEventListenerImpl : public ::cppu::WeakImplHelper< css::lang::XEventListener >
56 {
57 protected:
59 
60 public:
62  : pObj(_pObj)
63  {}
64 
65  // XEventListener
66  virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
67 
68  void StopListening(const uno::Reference< lang::XComponent >& xComp);
69  void StartListening(const uno::Reference< lang::XComponent >& xComp);
70 };
71 
72 // XEventListener
73 void SAL_CALL SdrControlEventListenerImpl::disposing( const css::lang::EventObject& /*Source*/)
74 {
75  if (pObj)
76  {
77  pObj->xUnoControlModel = nullptr;
78  }
79 }
80 
81 void SdrControlEventListenerImpl::StopListening(const uno::Reference< lang::XComponent >& xComp)
82 {
83  if (xComp.is())
84  xComp->removeEventListener(this);
85 }
86 
87 void SdrControlEventListenerImpl::StartListening(const uno::Reference< lang::XComponent >& xComp)
88 {
89  if (xComp.is())
90  xComp->addEventListener(this);
91 }
92 
93 
95 {
96  mutable ::rtl::Reference< SdrControlEventListenerImpl >
98 };
99 
100 
101 namespace
102 {
103  void lcl_ensureControlVisibility( SdrView const * _pView, const SdrUnoObj* _pObject, bool _bVisible )
104  {
105  OSL_PRECOND( _pObject, "lcl_ensureControlVisibility: no object -> no survival!" );
106 
107  SdrPageView* pPageView = _pView ? _pView->GetSdrPageView() : nullptr;
108  DBG_ASSERT( pPageView, "lcl_ensureControlVisibility: no view found!" );
109  if ( !pPageView )
110  return;
111 
112  ViewContact& rUnoControlContact( _pObject->GetViewContact() );
113 
114  for ( sal_uInt32 i = 0; i < pPageView->PageWindowCount(); ++i )
115  {
116  SdrPageWindow* pPageWindow = pPageView->GetPageWindow( i );
117  DBG_ASSERT( pPageWindow, "lcl_ensureControlVisibility: invalid PageViewWindow!" );
118  if ( !pPageWindow )
119  continue;
120 
121  if ( !pPageWindow->HasObjectContact() )
122  continue;
123 
124  ObjectContact& rPageViewContact( pPageWindow->GetObjectContact() );
125  const ViewObjectContact& rViewObjectContact( rUnoControlContact.GetViewObjectContact( rPageViewContact ) );
126  const ViewObjectContactOfUnoControl* pUnoControlContact = dynamic_cast< const ViewObjectContactOfUnoControl* >( &rViewObjectContact );
127  DBG_ASSERT( pUnoControlContact, "lcl_ensureControlVisibility: wrong ViewObjectContact type!" );
128  if ( !pUnoControlContact )
129  continue;
130 
131  pUnoControlContact->ensureControlVisibility( _bVisible );
132  }
133  }
134 }
135 
137  SdrModel& rSdrModel,
138  const OUString& rModelName)
139 : SdrRectObj(rSdrModel),
141 {
142  m_bIsUnoObj = true;
143 
144  m_pImpl->pEventListener = new SdrControlEventListenerImpl(this);
145 
146  // only an owner may create independently
147  if (!rModelName.isEmpty())
148  CreateUnoControlModel(rModelName);
149 }
150 
151 SdrUnoObj::SdrUnoObj( SdrModel& rSdrModel, SdrUnoObj const & rSource)
152 : SdrRectObj(rSdrModel, rSource),
154 {
155  m_bIsUnoObj = true;
156 
157  m_pImpl->pEventListener = new SdrControlEventListenerImpl(this);
158 
161 
162  // copy the uno control model
163  const uno::Reference< awt::XControlModel > xSourceControlModel = rSource.GetUnoControlModel();
164  if ( xSourceControlModel.is() )
165  {
166  try
167  {
168  uno::Reference< util::XCloneable > xClone( xSourceControlModel, uno::UNO_QUERY_THROW );
169  xUnoControlModel.set( xClone->createClone(), uno::UNO_QUERY_THROW );
170  }
171  catch( const uno::Exception& )
172  {
174  }
175  }
176 
177  // get service name of the control from the control model
178  uno::Reference< beans::XPropertySet > xSet(xUnoControlModel, uno::UNO_QUERY);
179  if (xSet.is())
180  {
181  uno::Any aValue( xSet->getPropertyValue("DefaultControl") );
182  OUString aStr;
183 
184  if( aValue >>= aStr )
185  aUnoControlTypeName = aStr;
186  }
187 
188  uno::Reference< lang::XComponent > xComp(xUnoControlModel, uno::UNO_QUERY);
189  if (xComp.is())
190  m_pImpl->pEventListener->StartListening(xComp);
191 }
192 
194  SdrModel& rSdrModel,
195  const OUString& rModelName,
196  const uno::Reference< lang::XMultiServiceFactory >& rxSFac)
197 : SdrRectObj(rSdrModel),
199 {
200  m_bIsUnoObj = true;
201 
202  m_pImpl->pEventListener = new SdrControlEventListenerImpl(this);
203 
204  // only an owner may create independently
205  if (!rModelName.isEmpty())
206  CreateUnoControlModel(rModelName,rxSFac);
207 }
208 
210 {
211  try
212  {
213  // clean up the control model
214  uno::Reference< lang::XComponent > xComp(xUnoControlModel, uno::UNO_QUERY);
215  if (xComp.is())
216  {
217  // is the control model owned by its environment?
218  uno::Reference< container::XChild > xContent(xUnoControlModel, uno::UNO_QUERY);
219  if (xContent.is() && !xContent->getParent().is())
220  xComp->dispose();
221  else
222  m_pImpl->pEventListener->StopListening(xComp);
223  }
224  }
225  catch( const uno::Exception& )
226  {
227  TOOLS_WARN_EXCEPTION( "svx", "SdrUnoObj::~SdrUnoObj" );
228  }
229 }
230 
232 {
233  rInfo.bRotateFreeAllowed = false;
234  rInfo.bRotate90Allowed = false;
235  rInfo.bMirrorFreeAllowed = false;
236  rInfo.bMirror45Allowed = false;
237  rInfo.bMirror90Allowed = false;
238  rInfo.bTransparenceAllowed = false;
239  rInfo.bShearAllowed = false;
240  rInfo.bEdgeRadiusAllowed = false;
241  rInfo.bNoOrthoDesired = false;
242  rInfo.bCanConvToPath = false;
243  rInfo.bCanConvToPoly = false;
244  rInfo.bCanConvToPathLineToArea = false;
245  rInfo.bCanConvToPolyLineToArea = false;
246  rInfo.bCanConvToContour = false;
247 }
248 
250 {
251  return SdrObjKind::UNO;
252 }
253 
254 void SdrUnoObj::SetContextWritingMode( const sal_Int16 _nContextWritingMode )
255 {
256  try
257  {
258  uno::Reference< beans::XPropertySet > xModelProperties( GetUnoControlModel(), uno::UNO_QUERY_THROW );
259  xModelProperties->setPropertyValue( "ContextWritingMode", uno::Any( _nContextWritingMode ) );
260  }
261  catch( const uno::Exception& )
262  {
264  }
265 }
266 
268 {
269  OUString sName(SvxResId(STR_ObjNameSingulUno));
270 
271  OUString aName(GetName());
272  if (!aName.isEmpty())
273  sName += " '" + aName + "'";
274 
275  return sName;
276 }
277 
279 {
280  return SvxResId(STR_ObjNamePluralUno);
281 }
282 
284 {
285  return new SdrUnoObj(rTargetModel, *this);
286 }
287 
288 void SdrUnoObj::NbcResize(const Point& rRef, const Fraction& xFact, const Fraction& yFact)
289 {
290  SdrRectObj::NbcResize(rRef,xFact,yFact);
291 
292  if (maGeo.nShearAngle==0_deg100 && maGeo.nRotationAngle==0_deg100)
293  return;
294 
295  // small correctors
296  if (maGeo.nRotationAngle>=9000_deg100 && maGeo.nRotationAngle<27000_deg100)
297  {
299  }
300 
301  maGeo.nRotationAngle = 0_deg100;
302  maGeo.nShearAngle = 0_deg100;
305  maGeo.mfTanShearAngle = 0.0;
307 }
308 
309 
311 {
312  // no special drag; we have no rounding rect and
313  // do want frame handles
314  return false;
315 }
316 
318 {
319  if ( GetLayer() == _nLayer )
320  { // redundant call -> not interested in doing anything here
321  SdrRectObj::NbcSetLayer( _nLayer );
322  return;
323  }
324 
325  // we need some special handling here in case we're moved from an invisible layer
326  // to a visible one, or vice versa
327  // (relative to a layer. Remember that the visibility of a layer is a view attribute
328  // - the same layer can be visible in one view, and invisible in another view, at the
329  // same time)
330 
331  // collect all views in which our old layer is visible
332  o3tl::sorted_vector< SdrView* > aPreviouslyVisible;
333 
334  {
335  SdrViewIter aIter( this );
336  for ( SdrView* pView = aIter.FirstView(); pView; pView = aIter.NextView() )
337  aPreviouslyVisible.insert( pView );
338  }
339 
340  SdrRectObj::NbcSetLayer( _nLayer );
341 
342  // collect all views in which our new layer is visible
343  o3tl::sorted_vector< SdrView* > aNewlyVisible;
344 
345  {
346  SdrViewIter aIter( this );
347  for ( SdrView* pView = aIter.FirstView(); pView; pView = aIter.NextView() )
348  {
349  if ( aPreviouslyVisible.erase(pView) == 0 )
350  {
351  // in pView, we were visible _before_ the layer change, and are
352  // _not_ visible after the layer change
353  // => remember this view, as our visibility there changed
354  aNewlyVisible.insert( pView );
355  }
356  }
357  }
358 
359  // now aPreviouslyVisible contains all views where we became invisible
360  for (const auto& rpView : aPreviouslyVisible)
361  {
362  lcl_ensureControlVisibility( rpView, this, false );
363  }
364 
365  // and aNewlyVisible all views where we became visible
366  for (const auto& rpView : aNewlyVisible)
367  {
368  lcl_ensureControlVisibility( rpView, this, true );
369  }
370 }
371 
372 void SdrUnoObj::CreateUnoControlModel(const OUString& rModelName)
373 {
374  DBG_ASSERT(!xUnoControlModel.is(), "model already exists");
375 
376  aUnoControlModelTypeName = rModelName;
377 
378  uno::Reference< awt::XControlModel > xModel;
379  uno::Reference< uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
380  if (!aUnoControlModelTypeName.isEmpty() )
381  {
382  xModel.set(xContext->getServiceManager()->createInstanceWithContext(
383  aUnoControlModelTypeName, xContext), uno::UNO_QUERY);
384 
385  if (xModel.is())
386  SetChanged();
387  }
388 
389  SetUnoControlModel(xModel);
390 }
391 
392 void SdrUnoObj::CreateUnoControlModel(const OUString& rModelName,
393  const uno::Reference< lang::XMultiServiceFactory >& rxSFac)
394 {
395  DBG_ASSERT(!xUnoControlModel.is(), "model already exists");
396 
397  aUnoControlModelTypeName = rModelName;
398 
399  uno::Reference< awt::XControlModel > xModel;
400  if (!aUnoControlModelTypeName.isEmpty() && rxSFac.is() )
401  {
402  xModel.set(rxSFac->createInstance(aUnoControlModelTypeName), uno::UNO_QUERY);
403 
404  if (xModel.is())
405  SetChanged();
406  }
407 
408  SetUnoControlModel(xModel);
409 }
410 
411 void SdrUnoObj::SetUnoControlModel( const uno::Reference< awt::XControlModel >& xModel)
412 {
413  if (xUnoControlModel.is())
414  {
415  uno::Reference< lang::XComponent > xComp(xUnoControlModel, uno::UNO_QUERY);
416  if (xComp.is())
417  m_pImpl->pEventListener->StopListening(xComp);
418  }
419 
420  xUnoControlModel = xModel;
421 
422  // control model has to contain service name of the control
423  if (xUnoControlModel.is())
424  {
425  uno::Reference< beans::XPropertySet > xSet(xUnoControlModel, uno::UNO_QUERY);
426  if (xSet.is())
427  {
428  uno::Any aValue( xSet->getPropertyValue("DefaultControl") );
429  OUString aStr;
430  if( aValue >>= aStr )
431  aUnoControlTypeName = aStr;
432  }
433 
434  uno::Reference< lang::XComponent > xComp(xUnoControlModel, uno::UNO_QUERY);
435  if (xComp.is())
436  m_pImpl->pEventListener->StartListening(xComp);
437  }
438 
439  // invalidate all ViewObject contacts
440  ViewContactOfUnoControl* pVC = nullptr;
441  if ( impl_getViewContact( pVC ) )
442  {
443  // flushViewObjectContacts() removes all existing VOCs for the local DrawHierarchy. This
444  // is always allowed since they will be re-created on demand (and with the changed model)
446  }
447 }
448 
449 
450 uno::Reference< awt::XControl > SdrUnoObj::GetUnoControl(const SdrView& _rView, const OutputDevice& _rOut) const
451 {
452  uno::Reference< awt::XControl > xControl;
453 
454  SdrPageView* pPageView = _rView.GetSdrPageView();
455  OSL_ENSURE( pPageView && getSdrPageFromSdrObject() == pPageView->GetPage(), "SdrUnoObj::GetUnoControl: This object is not displayed in that particular view!" );
456  if ( !pPageView || getSdrPageFromSdrObject() != pPageView->GetPage() )
457  return nullptr;
458 
459  SdrPageWindow* pPageWindow = pPageView->FindPageWindow( _rOut );
460  OSL_ENSURE( pPageWindow, "SdrUnoObj::GetUnoControl: did not find my SdrPageWindow!" );
461  if ( !pPageWindow )
462  return nullptr;
463 
464  ViewObjectContact& rViewObjectContact( GetViewContact().GetViewObjectContact( pPageWindow->GetObjectContact() ) );
465  ViewObjectContactOfUnoControl* pUnoContact = dynamic_cast< ViewObjectContactOfUnoControl* >( &rViewObjectContact );
466  OSL_ENSURE( pUnoContact, "SdrUnoObj::GetUnoControl: wrong contact type!" );
467  if ( pUnoContact )
468  xControl = pUnoContact->getControl();
469 
470  return xControl;
471 }
472 
473 
474 uno::Reference< awt::XControl > SdrUnoObj::GetTemporaryControlForWindow(
475  const vcl::Window& _rWindow, uno::Reference< awt::XControlContainer >& _inout_ControlContainer ) const
476 {
477  uno::Reference< awt::XControl > xControl;
478 
479  ViewContactOfUnoControl* pVC = nullptr;
480  if ( impl_getViewContact( pVC ) )
481  xControl = pVC->getTemporaryControlForWindow( _rWindow, _inout_ControlContainer );
482 
483  return xControl;
484 }
485 
486 
488 {
489  ViewContact& rViewContact( GetViewContact() );
490  _out_rpContact = dynamic_cast< ViewContactOfUnoControl* >( &rViewContact );
491  DBG_ASSERT( _out_rpContact, "SdrUnoObj::impl_getViewContact: could not find my ViewContact!" );
492  return ( _out_rpContact != nullptr );
493 }
494 
495 
496 std::unique_ptr<sdr::contact::ViewContact> SdrUnoObj::CreateObjectSpecificViewContact()
497 {
498  return std::make_unique<sdr::contact::ViewContactOfUnoControl>( *this );
499 }
500 
501 
502 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SVX_DLLPRIVATE bool impl_getViewContact(sdr::contact::ViewContactOfUnoControl *&_out_rpContact) const
Retrieves the typed ViewContact for the object.
Definition: svdouno.cxx:487
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
Definition: svdouno.cxx:73
css::uno::Reference< css::awt::XControl > getControl()
returns the ->XControl instance belonging to the instance, creates it if necessary ...
void StartListening(const uno::Reference< lang::XComponent > &xComp)
Definition: svdouno.cxx:87
virtual SdrObjKind GetObjIdentifier() const override
Definition: svdouno.cxx:249
constexpr tools::Long Left() const
virtual void TakeObjInfo(SdrObjTransformInfoRec &rInfo) const override
Definition: svdouno.cxx:231
css::uno::Reference< css::awt::XControl > getTemporaryControlForWindow(const vcl::Window &_rWindow, css::uno::Reference< css::awt::XControlContainer > &_inout_ControlContainer) const
retrieves a temporary XControl instance, whose parent is the given window SdrUnoObj::GetTemporaryCon...
double mfSinRotationAngle
Definition: svdtrans.hxx:206
continuously activated OLE (PlugIn-Frame or similar)
virtual void SetContextWritingMode(const sal_Int16 _nContextWritingMode) override
Definition: svdouno.cxx:254
SdrUnoObj(SdrModel &rSdrModel, const OUString &rModelName)
Definition: svdouno.cxx:136
SdrPageWindow * FindPageWindow(const SdrPaintWindow &rPaintWindow) const
Definition: svdpagv.cxx:43
sdr::contact::ViewContact & GetViewContact() const
Definition: svdobj.cxx:251
SdrObjKind
Definition: svdobjkind.hxx:24
Rectangle objects (rectangle, circle, ...)
Definition: svdorect.hxx:38
SdrView * NextView()
Definition: svdviter.cxx:158
Provides information about various ZObject properties.
Definition: svdobj.hxx:195
virtual void NbcSetLayer(SdrLayerID nLayer) override
Definition: svdouno.cxx:317
SdrPageWindow * GetPageWindow(sal_uInt32 nIndex) const
Definition: svdpagv.cxx:83
SdrPage * getSdrPageFromSdrObject() const
Definition: svdobj.cxx:269
bool m_bIsUnoObj
Definition: svdobj.hxx:905
SVX_DLLPRIVATE void CreateUnoControlModel(const OUString &rModelName)
Definition: svdouno.cxx:372
OUString SvxResId(TranslateId aId)
Definition: dialmgr.cxx:24
virtual std::unique_ptr< sdr::contact::ViewContact > CreateObjectSpecificViewContact() override
Definition: svdouno.cxx:496
tools::Rectangle maRect
Definition: svdotext.hxx:170
virtual ~SdrUnoObj() override
Definition: svdouno.cxx:209
void StopListening(const uno::Reference< lang::XComponent > &xComp)
Definition: svdouno.cxx:81
virtual void NbcResize(const Point &rRef, const Fraction &xFact, const Fraction &yFact) override
Definition: svdouno.cxx:288
virtual OUString TakeObjNamePlural() const override
Definition: svdouno.cxx:278
double mfTanShearAngle
Definition: svdtrans.hxx:205
virtual void SetBoundAndSnapRectsDirty(bool bNotMyself=false, bool bRecursive=true)
Definition: svdobj.cxx:510
OUString aUnoControlTypeName
Definition: svdouno.hxx:51
#define DBG_UNHANDLED_EXCEPTION(...)
#define TOOLS_WARN_EXCEPTION(area, stream)
#define DBG_ASSERT(sCon, aError)
int i
OUString sName
friend class SdrControlEventListenerImpl
Definition: svdouno.hxx:46
css::uno::Reference< css::awt::XControl > GetTemporaryControlForWindow(const vcl::Window &_rWindow, css::uno::Reference< css::awt::XControlContainer > &_inout_ControlContainer) const
Retrieves a temporary XControl instance for a given output device.
Definition: svdouno.cxx:474
constexpr tools::Long Right() const
virtual void SetUnoControlModel(const css::uno::Reference< css::awt::XControlModel > &xModel)
Definition: svdouno.cxx:411
constexpr tools::Long Top() const
void flushViewObjectContacts(bool bWithHierarchy=true)
const sdr::contact::ObjectContact & GetObjectContact() const
virtual SdrLayerID GetLayer() const
Definition: svdobj.cxx:668
virtual bool hasSpecialDrag() const override
The standard transformations (Move,Resize,Rotate,Mirror,Shear) are taken over by the View (TakeXorPol...
Definition: svdouno.cxx:310
constexpr tools::Long Bottom() const
SdrControlEventListenerImpl(SdrUnoObj *_pObj)
Definition: svdouno.cxx:61
mutable::rtl::Reference< SdrControlEventListenerImpl > pEventListener
Definition: svdouno.cxx:97
std::unique_ptr< SdrUnoObjDataHolder > m_pImpl
Definition: svdouno.hxx:48
SdrPage * GetPage() const
Definition: svdpagv.hxx:166
virtual void NbcResize(const Point &rRef, const Fraction &xFact, const Fraction &yFact) override
Definition: svdorect.cxx:451
GeoStat maGeo
Definition: svdotext.hxx:173
Degree100 nRotationAngle
Definition: svdtrans.hxx:203
bool HasObjectContact() const
determines whether there already exists an ObjectContact
OUString aName
double mfCosRotationAngle
Definition: svdtrans.hxx:207
Reference< XComponentContext > getProcessComponentContext()
virtual OUString TakeObjNameSingul() const override
Definition: svdouno.cxx:267
css::uno::Reference< css::awt::XControl > GetUnoControl(const SdrView &_rView, const OutputDevice &_rOut) const
Definition: svdouno.cxx:450
SdrPageView * GetSdrPageView() const
Definition: svdpntv.hxx:299
const css::uno::Reference< css::awt::XControlModel > & GetUnoControlModel() const
Definition: svdouno.hxx:91
::rtl::Reference< OContentHelper > xContent
void Move(tools::Long nHorzMoveDelta, tools::Long nVertMoveDelta)
Reference< XModel > xModel
css::uno::Reference< css::awt::XControlModel > xUnoControlModel
Definition: svdouno.hxx:54
::std::unique_ptr< XmlIdRegistry_Impl > m_pImpl
std::pair< const_iterator, bool > insert(Value &&x)
virtual void SetChanged()
Definition: svdobj.cxx:1025
virtual SdrUnoObj * CloneSdrObject(SdrModel &rTargetModel) const override
Definition: svdouno.cxx:283
SdrView * FirstView()
Definition: svdviter.cxx:152
const OUString & GetName() const
Definition: svdobj.cxx:792
aStr
Degree100 nShearAngle
Definition: svdtrans.hxx:204
OUString aUnoControlModelTypeName
Definition: svdouno.hxx:50
size_type erase(const Value &x)
virtual void NbcSetLayer(SdrLayerID nLayer)
Definition: svdobj.cxx:685
sal_uInt32 PageWindowCount() const
Definition: svdpagv.hxx:89