1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include <fmvwimp.hxx>
23 #include <fmpgeimp.hxx>
24 #include <svx/fmview.hxx>
25 #include <svx/fmpage.hxx>
26 #include <svx/svdovirt.hxx>
27 #include <svx/fmmodel.hxx>
29 #include <com/sun/star/awt/XDevice.hpp>
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 #include <com/sun/star/form/Forms.hpp>
32 #include <com/sun/star/script/XEventAttacherManager.hpp>
33 #include <svx/fmtools.hxx>
35 #include <comphelper/property.hxx>
36 #include <comphelper/processfactory.hxx>
37 #include <toolkit/awt/vclxdevice.hxx>
38 #include <tools/debug.hxx>
39 #include <tools/diagnose_ex.h>
41 using namespace ::com::sun::star::io
;
42 using namespace ::com::sun::star::uno
;
43 using namespace ::com::sun::star::awt
;
44 using namespace ::com::sun::star::lang
;
45 using namespace ::com::sun::star::util
;
46 using namespace ::com::sun::star::form
;
47 using namespace ::com::sun::star::beans
;
48 using namespace ::com::sun::star::script
;
49 using namespace ::com::sun::star::container
;
50 using namespace ::svxform
;
55 const OUString
& rModelName
)
56 : SdrUnoObj(rSdrModel
, rModelName
)
58 ,m_pLastKnownRefDevice(nullptr)
60 // normally, this is done in SetUnoControlModel, but if the call happened in the base class ctor,
61 // then our incarnation of it was not called (since we were not constructed at this time).
62 impl_checkRefDevice_nothrow( true );
65 FmFormObj::FmFormObj(SdrModel
& rSdrModel
)
66 : SdrUnoObj(rSdrModel
, "")
68 ,m_pLastKnownRefDevice(nullptr)
70 // Stuff that old SetModel also did:
71 impl_checkRefDevice_nothrow();
74 FmFormObj::FmFormObj(SdrModel
& rSdrModel
, FmFormObj
const & rSource
)
75 : SdrUnoObj(rSdrModel
, rSource
)
77 ,m_pLastKnownRefDevice(nullptr)
79 // Stuff that old SetModel also did:
80 impl_checkRefDevice_nothrow();
82 // If UnoControlModel is part of an event environment,
83 // events may assigned to it.
84 Reference
< XFormComponent
> xContent(rSource
.xUnoControlModel
, UNO_QUERY
);
87 Reference
< XEventAttacherManager
> xManager(xContent
->getParent(), UNO_QUERY
);
88 Reference
< XIndexAccess
> xManagerAsIndex(xManager
, UNO_QUERY
);
89 if (xManagerAsIndex
.is())
91 sal_Int32 nPos
= getElementPos( xManagerAsIndex
, xContent
);
93 aEvts
= xManager
->getScriptEvents( nPos
);
97 aEvts
= rSource
.aEvts
;
99 Reference
< XChild
> xSourceAsChild(rSource
.GetUnoControlModel(), UNO_QUERY
);
100 if (!xSourceAsChild
.is())
103 Reference
< XInterface
> xSourceContainer
= xSourceAsChild
->getParent();
105 m_xEnvironmentHistory
= css::form::Forms::create( comphelper::getProcessComponentContext() );
107 ensureModelEnv(xSourceContainer
, m_xEnvironmentHistory
);
108 m_aEventsHistory
= aEvts
;
109 // if we were clone there was a call to operator=, so aEvts are exactly the events we need here...
112 FmFormObj::~FmFormObj()
115 if (m_xEnvironmentHistory
.is())
116 m_xEnvironmentHistory
->dispose();
118 m_xEnvironmentHistory
= nullptr;
119 m_aEventsHistory
.realloc(0);
123 void FmFormObj::SetObjEnv(const Reference
< XIndexContainer
> & xForm
, const sal_Int32 nIdx
,
124 const Sequence
< ScriptEventDescriptor
>& rEvts
)
132 void FmFormObj::ClearObjEnv()
140 void FmFormObj::impl_checkRefDevice_nothrow( bool _force
)
142 const FmFormModel
* pFormModel
= dynamic_cast<FmFormModel
*>(&getSdrModelFromSdrObject());
143 if ( !pFormModel
|| !pFormModel
->ControlsUseRefDevice() )
146 OutputDevice
* pCurrentRefDevice
= pFormModel
->GetRefDevice();
147 if ( ( m_pLastKnownRefDevice
.get() == pCurrentRefDevice
) && !_force
)
150 Reference
< XControlModel
> xControlModel( GetUnoControlModel() );
151 if ( !xControlModel
.is() )
154 m_pLastKnownRefDevice
= pCurrentRefDevice
;
155 if ( !m_pLastKnownRefDevice
)
160 Reference
< XPropertySet
> xModelProps( GetUnoControlModel(), UNO_QUERY_THROW
);
161 Reference
< XPropertySetInfo
> xPropertyInfo( xModelProps
->getPropertySetInfo(), UNO_SET_THROW
);
163 static constexpr OUStringLiteral sRefDevicePropName
= u
"ReferenceDevice";
164 if ( xPropertyInfo
->hasPropertyByName( sRefDevicePropName
) )
166 rtl::Reference
<VCLXDevice
> pUnoRefDevice
= new VCLXDevice
;
167 pUnoRefDevice
->SetOutputDevice( m_pLastKnownRefDevice
);
168 Reference
< XDevice
> xRefDevice( pUnoRefDevice
);
169 xModelProps
->setPropertyValue( sRefDevicePropName
, makeAny( xRefDevice
) );
172 catch( const Exception
& )
174 DBG_UNHANDLED_EXCEPTION("svx");
179 void FmFormObj::impl_isolateControlModel_nothrow()
183 Reference
< XChild
> xControlModel( GetUnoControlModel(), UNO_QUERY
);
184 if ( xControlModel
.is() )
186 Reference
< XIndexContainer
> xParent( xControlModel
->getParent(), UNO_QUERY
);
189 sal_Int32 nPos
= getElementPos( xParent
, xControlModel
);
190 xParent
->removeByIndex( nPos
);
194 catch( const Exception
& )
196 DBG_UNHANDLED_EXCEPTION("svx");
201 void FmFormObj::handlePageChange(SdrPage
* pOldPage
, SdrPage
* pNewPage
)
203 FmFormPage
* pOldFormPage(dynamic_cast< FmFormPage
* >(getSdrPageFromSdrObject()));
205 pOldFormPage
->GetImpl().formObjectRemoved( *this );
207 FmFormPage
* pNewFormPage
= dynamic_cast<FmFormPage
*>( pNewPage
);
210 // Maybe it makes sense to create an environment history here : if somebody set's our page to NULL, and we have a valid page before,
211 // me may want to remember our place within the old page. For this we could create a new m_xEnvironmentHistory to store it.
212 // So the next SetPage with a valid new page would restore that environment within the new page.
213 // But for the original Bug (#57300#) we don't need that, so I omit it here. Maybe this will be implemented later.
214 impl_isolateControlModel_nothrow();
215 SdrUnoObj::handlePageChange(pOldPage
, pNewPage
);
219 Reference
< css::form::XForms
> xNewPageForms
= pNewFormPage
->GetForms();
220 Reference
< XIndexContainer
> xNewParent
;
221 Sequence
< ScriptEventDescriptor
> aNewEvents
;
223 // calc the new parent for my model (within the new page's forms hierarchy)
224 // do we have a history ? (from :Clone)
225 if ( m_xEnvironmentHistory
.is() )
227 // the element in m_xEnvironmentHistory which is equivalent to my new parent (which (perhaps) has to be created within pNewPage->GetForms)
228 // is the right-most element in the tree.
229 Reference
< XIndexContainer
> xRightMostLeaf( m_xEnvironmentHistory
, UNO_QUERY_THROW
);
232 while ( xRightMostLeaf
->getCount() )
235 xRightMostLeaf
->getByIndex( xRightMostLeaf
->getCount() - 1 ),
240 xNewParent
.set( ensureModelEnv( xRightMostLeaf
, xNewPageForms
), UNO_QUERY_THROW
);
242 // we successfully cloned the environment in m_xEnvironmentHistory, so we can use m_aEventsHistory
243 // (which describes the events of our model at the moment m_xEnvironmentHistory was created)
244 aNewEvents
= m_aEventsHistory
;
246 catch( const Exception
& )
248 DBG_UNHANDLED_EXCEPTION("svx");
252 if ( !xNewParent
.is() )
254 // are we a valid part of our current page forms ?
255 Reference
< XIndexContainer
> xOldForms
;
257 xOldForms
.set( pOldFormPage
->GetForms(), UNO_QUERY_THROW
);
259 if ( xOldForms
.is() )
261 // search (upward from our model) for xOldForms
262 Reference
< XChild
> xSearch( GetUnoControlModel(), UNO_QUERY
);
265 if ( xSearch
== xOldForms
)
267 xSearch
.set( xSearch
->getParent(), UNO_QUERY
);
269 if ( xSearch
.is() ) // implies xSearch == xOldForms, which means we're a valid part of our current page forms hierarchy
271 Reference
< XChild
> xMeAsChild( GetUnoControlModel(), UNO_QUERY
);
272 xNewParent
.set( ensureModelEnv( xMeAsChild
->getParent(), xNewPageForms
), UNO_QUERY
);
274 if ( xNewParent
.is() )
278 // transfer the events from our (model's) parent to the new (model's) parent, too
279 Reference
< XEventAttacherManager
> xEventManager(xMeAsChild
->getParent(), UNO_QUERY
);
280 Reference
< XIndexAccess
> xManagerAsIndex(xEventManager
, UNO_QUERY
);
281 if (xManagerAsIndex
.is())
283 sal_Int32 nPos
= getElementPos(xManagerAsIndex
, xMeAsChild
);
285 aNewEvents
= xEventManager
->getScriptEvents(nPos
);
290 catch( const Exception
& )
292 DBG_UNHANDLED_EXCEPTION("svx");
300 SdrUnoObj::handlePageChange(pOldPage
, pNewPage
);
302 // place my model within the new parent container
305 Reference
< XFormComponent
> xMeAsFormComp(GetUnoControlModel(), UNO_QUERY
);
306 if (xMeAsFormComp
.is())
308 // check if I have another parent (and remove me, if necessary)
309 Reference
< XIndexContainer
> xOldParent(xMeAsFormComp
->getParent(), UNO_QUERY
);
312 sal_Int32 nPos
= getElementPos(xOldParent
, xMeAsFormComp
);
314 xOldParent
->removeByIndex(nPos
);
317 // and insert into the new container
318 xNewParent
->insertByIndex(xNewParent
->getCount(), makeAny(xMeAsFormComp
));
320 // transfer the events
321 if (aNewEvents
.hasElements())
325 Reference
< XEventAttacherManager
> xEventManager(xNewParent
, UNO_QUERY
);
326 Reference
< XIndexAccess
> xManagerAsIndex(xEventManager
, UNO_QUERY
);
327 if (xManagerAsIndex
.is())
329 sal_Int32 nPos
= getElementPos(xManagerAsIndex
, xMeAsFormComp
);
330 DBG_ASSERT(nPos
>= 0, "FmFormObj::SetPage : inserted but not present ?");
331 xEventManager
->registerScriptEvents(nPos
, aNewEvents
);
334 catch( const Exception
& )
336 DBG_UNHANDLED_EXCEPTION("svx");
344 if (m_xEnvironmentHistory
.is())
345 m_xEnvironmentHistory
->dispose();
347 m_xEnvironmentHistory
= nullptr;
348 m_aEventsHistory
.realloc(0);
350 pNewFormPage
->GetImpl().formObjectInserted( *this );
353 SdrInventor
FmFormObj::GetObjInventor() const
355 return SdrInventor::FmForm
;
358 SdrObjKind
FmFormObj::GetObjIdentifier() const
363 FmFormObj
* FmFormObj::CloneSdrObject(SdrModel
& rTargetModel
) const
365 return new FmFormObj(rTargetModel
, *this);
368 void FmFormObj::NbcReformatText()
370 impl_checkRefDevice_nothrow();
371 SdrUnoObj::NbcReformatText();
377 OUString
lcl_getFormComponentAccessPath(const Reference
< XInterface
>& _xElement
, Reference
< XInterface
>& _rTopLevelElement
)
379 Reference
< css::form::XFormComponent
> xChild(_xElement
, UNO_QUERY
);
380 Reference
< css::container::XIndexAccess
> xParent
;
382 xParent
.set(xChild
->getParent(), UNO_QUERY
);
384 // while the current content is a form
388 // get the content's relative pos within its parent container
389 sal_Int32 nPos
= getElementPos(xParent
, xChild
);
391 // prepend this current relative pos
392 OUString sCurrentIndex
= OUString::number(nPos
);
393 if (!sReturn
.isEmpty())
395 sCurrentIndex
+= "\\" + sReturn
;
398 sReturn
= sCurrentIndex
;
401 xChild
.set(xParent
, css::uno::UNO_QUERY
);
403 xParent
.set(xChild
->getParent(), UNO_QUERY
);
406 _rTopLevelElement
= xParent
;
412 Reference
< XInterface
> FmFormObj::ensureModelEnv(const Reference
< XInterface
> & _rSourceContainer
, const Reference
<css::form::XForms
>& _rTopLevelDestContainer
)
414 Reference
< XInterface
> xTopLevelSource
;
415 OUString sAccessPath
= lcl_getFormComponentAccessPath(_rSourceContainer
, xTopLevelSource
);
416 if (!xTopLevelSource
.is())
417 // something went wrong, maybe _rSourceContainer isn't part of a valid forms hierarchy
418 return Reference
< XInterface
> ();
420 Reference
< XIndexContainer
> xDestContainer(_rTopLevelDestContainer
, UNO_QUERY_THROW
);
421 Reference
< XIndexContainer
> xSourceContainer(xTopLevelSource
, UNO_QUERY
);
422 DBG_ASSERT(xSourceContainer
.is(), "FmFormObj::ensureModelEnv : the top level source is invalid !");
424 sal_Int32 nTokIndex
= 0;
427 OUString aToken
= sAccessPath
.getToken( 0, '\\', nTokIndex
);
428 sal_uInt16 nIndex
= static_cast<sal_uInt16
>(aToken
.toInt32());
430 // get the DSS of the source form (we have to find an equivalent for)
431 DBG_ASSERT(nIndex
<xSourceContainer
->getCount(), "FmFormObj::ensureModelEnv : invalid access path !");
432 Reference
< XPropertySet
> xSourceForm
;
433 xSourceContainer
->getByIndex(nIndex
) >>= xSourceForm
;
434 DBG_ASSERT(xSourceForm
.is(), "FmFormObj::ensureModelEnv : invalid source form !");
436 Any aSrcCursorSource
, aSrcCursorSourceType
, aSrcDataSource
;
437 DBG_ASSERT(::comphelper::hasProperty(FM_PROP_COMMAND
, xSourceForm
) && ::comphelper::hasProperty(FM_PROP_COMMANDTYPE
, xSourceForm
)
438 && ::comphelper::hasProperty(FM_PROP_DATASOURCE
, xSourceForm
), "FmFormObj::ensureModelEnv : invalid access path or invalid form (missing props) !");
439 // the parent access path should refer to a row set
442 aSrcCursorSource
= xSourceForm
->getPropertyValue(FM_PROP_COMMAND
);
443 aSrcCursorSourceType
= xSourceForm
->getPropertyValue(FM_PROP_COMMANDTYPE
);
444 aSrcDataSource
= xSourceForm
->getPropertyValue(FM_PROP_DATASOURCE
);
448 OSL_FAIL("FmFormObj::ensureModelEnv : could not retrieve a source DSS !");
452 // calc the number of (source) form siblings with the same DSS
453 Reference
< XPropertySet
> xCurrentSourceForm
, xCurrentDestForm
;
454 sal_Int16 nCurrentSourceIndex
= 0;
455 sal_Int32 nCurrentDestIndex
= 0;
456 while (nCurrentSourceIndex
<= nIndex
)
458 bool bEqualDSS
= false;
459 while (!bEqualDSS
) // (we don't have to check nCurrentSourceIndex here : it's bound by nIndex)
461 xSourceContainer
->getByIndex(nCurrentSourceIndex
) >>= xCurrentSourceForm
;
462 DBG_ASSERT(xCurrentSourceForm
.is(), "FmFormObj::ensureModelEnv : invalid form ancestor (2) !");
464 if (::comphelper::hasProperty(FM_PROP_DATASOURCE
, xCurrentSourceForm
))
468 if ( xCurrentSourceForm
->getPropertyValue(FM_PROP_COMMAND
) == aSrcCursorSource
469 && xCurrentSourceForm
->getPropertyValue(FM_PROP_COMMANDTYPE
) == aSrcCursorSourceType
470 && xCurrentSourceForm
->getPropertyValue(FM_PROP_DATASOURCE
) == aSrcDataSource
478 TOOLS_WARN_EXCEPTION("svx.form",
479 "exception while getting a sibling's DSS !");
483 ++nCurrentSourceIndex
;
486 DBG_ASSERT(bEqualDSS
, "FmFormObj::ensureModelEnv : found no source form !");
487 // ??? at least the nIndex-th one should have been found ???
489 // now search the next one with the given DSS (within the destination container)
491 while (!bEqualDSS
&& (nCurrentDestIndex
< xDestContainer
->getCount()))
493 xDestContainer
->getByIndex(nCurrentDestIndex
) >>= xCurrentDestForm
;
494 DBG_ASSERT(xCurrentDestForm
.is(), "FmFormObj::ensureModelEnv : invalid destination form !");
496 if (::comphelper::hasProperty(FM_PROP_DATASOURCE
, xCurrentDestForm
))
500 if ( xCurrentDestForm
->getPropertyValue(FM_PROP_COMMAND
) == aSrcCursorSource
501 && xCurrentDestForm
->getPropertyValue(FM_PROP_COMMANDTYPE
) == aSrcCursorSourceType
502 && xCurrentDestForm
->getPropertyValue(FM_PROP_DATASOURCE
) == aSrcDataSource
510 TOOLS_WARN_EXCEPTION("svx.form",
511 "exception while getting a destination DSS !");
519 { // There is at least one more source form with the given DSS than destination forms are.
523 // create and insert (into the destination) a copy of the form
524 xCurrentDestForm
.set(
525 ::comphelper::getProcessServiceFactory()->createInstance("com.sun.star.form.component.DataForm"),
527 ::comphelper::copyProperties( xCurrentSourceForm
, xCurrentDestForm
);
529 DBG_ASSERT(nCurrentDestIndex
== xDestContainer
->getCount(), "FmFormObj::ensureModelEnv : something went wrong with the numbers !");
530 xDestContainer
->insertByIndex(nCurrentDestIndex
, makeAny(xCurrentDestForm
));
533 // like nCurrentSourceIndex, nCurrentDestIndex now points 'behind' the form it actually means
537 OSL_FAIL("FmFormObj::ensureModelEnv : something went seriously wrong while creating a new form !");
538 // no more options anymore ...
539 return Reference
< XInterface
> ();
545 // now xCurrentDestForm is a form equivalent to xSourceForm (which means they have the same DSS and the same number
546 // of left siblings with the same DSS, which counts for all their ancestors, too)
549 xDestContainer
.set(xCurrentDestForm
, UNO_QUERY
);
550 xSourceContainer
.set(xSourceForm
, UNO_QUERY
);
551 DBG_ASSERT(xDestContainer
.is() && xSourceContainer
.is(), "FmFormObj::ensureModelEnv : invalid container !");
553 while ( nTokIndex
>= 0 );
555 return Reference
<XInterface
>( xDestContainer
, UNO_QUERY
);
558 FmFormObj
* FmFormObj::GetFormObject( SdrObject
* _pSdrObject
)
560 FmFormObj
* pFormObject
= dynamic_cast< FmFormObj
* >( _pSdrObject
);
563 SdrVirtObj
* pVirtualObject
= dynamic_cast< SdrVirtObj
* >( _pSdrObject
);
564 if ( pVirtualObject
)
565 pFormObject
= dynamic_cast< FmFormObj
* >( &pVirtualObject
->ReferencedObj() );
571 const FmFormObj
* FmFormObj::GetFormObject( const SdrObject
* _pSdrObject
)
573 const FmFormObj
* pFormObject
= dynamic_cast< const FmFormObj
* >( _pSdrObject
);
576 const SdrVirtObj
* pVirtualObject
= dynamic_cast< const SdrVirtObj
* >( _pSdrObject
);
577 if ( pVirtualObject
)
578 pFormObject
= dynamic_cast< const FmFormObj
* >( &pVirtualObject
->GetReferencedObj() );
584 void FmFormObj::SetUnoControlModel( const Reference
< css::awt::XControlModel
>& _rxModel
)
586 SdrUnoObj::SetUnoControlModel( _rxModel
);
588 FmFormPage
* pFormPage(dynamic_cast< FmFormPage
* >(getSdrPageFromSdrObject()));
590 pFormPage
->GetImpl().formModelAssigned( *this );
592 impl_checkRefDevice_nothrow( true );
596 bool FmFormObj::EndCreate( SdrDragStat
& rStat
, SdrCreateCmd eCmd
)
598 bool bResult
= SdrUnoObj::EndCreate(rStat
, eCmd
);
599 if ( bResult
&& SdrCreateCmd::ForceEnd
== eCmd
&& rStat
.GetView() )
601 FmFormPage
* pFormPage(dynamic_cast< FmFormPage
* >(getSdrPageFromSdrObject()));
603 if (nullptr != pFormPage
)
607 Reference
< XFormComponent
> xContent( xUnoControlModel
, UNO_QUERY_THROW
);
608 Reference
< XForm
> xParentForm( xContent
->getParent(), UNO_QUERY
);
610 Reference
< XIndexContainer
> xFormToInsertInto
;
612 if ( !xParentForm
.is() )
613 { // model is not yet part of a form component hierarchy
614 xParentForm
.set( pFormPage
->GetImpl().findPlaceInFormComponentHierarchy( xContent
), UNO_SET_THROW
);
615 xFormToInsertInto
.set( xParentForm
, UNO_QUERY_THROW
);
618 FmFormPageImpl::setUniqueName( xContent
, xParentForm
);
620 if ( xFormToInsertInto
.is() )
621 xFormToInsertInto
->insertByIndex( xFormToInsertInto
->getCount(), makeAny( xContent
) );
623 catch( const Exception
& )
625 DBG_UNHANDLED_EXCEPTION("svx");
629 FmFormView
* pView( dynamic_cast< FmFormView
* >( rStat
.GetView() ) );
630 FmXFormView
* pViewImpl
= pView
? pView
->GetImpl() : nullptr;
631 OSL_ENSURE( pViewImpl
, "FmFormObj::EndCreate: no view!?" );
633 pViewImpl
->onCreatedFormObject( *this );
639 void FmFormObj::BrkCreate( SdrDragStat
& rStat
)
641 SdrUnoObj::BrkCreate( rStat
);
642 impl_isolateControlModel_nothrow();
644 FmFormView
* pView( dynamic_cast< FmFormView
* >( rStat
.GetView() ) );
645 FmXFormView
* pViewImpl
= pView
? pView
->GetImpl() : nullptr;
646 OSL_ENSURE( pViewImpl
, "FmFormObj::EndCreate: no view!?" );
648 pViewImpl
->breakCreateFormObject();
652 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */