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/fmglob.hxx>
26 #include <svx/fmpage.hxx>
27 #include <editeng/editeng.hxx>
28 #include <svx/svdovirt.hxx>
29 #include <svx/fmmodel.hxx>
31 #include <com/sun/star/awt/XDevice.hpp>
32 #include <com/sun/star/awt/XControlContainer.hpp>
33 #include <com/sun/star/beans/XPropertySet.hpp>
34 #include <com/sun/star/form/Forms.hpp>
35 #include <com/sun/star/io/XPersistObject.hpp>
36 #include <com/sun/star/script/XEventAttacherManager.hpp>
37 #include <com/sun/star/util/XCloneable.hpp>
38 #include <svx/fmtools.hxx>
40 #include <comphelper/property.hxx>
41 #include <comphelper/processfactory.hxx>
42 #include <toolkit/awt/vclxdevice.hxx>
43 #include <vcl/svapp.hxx>
44 #include <tools/debug.hxx>
45 #include <tools/diagnose_ex.h>
47 using namespace ::com::sun::star::io
;
48 using namespace ::com::sun::star::uno
;
49 using namespace ::com::sun::star::awt
;
50 using namespace ::com::sun::star::lang
;
51 using namespace ::com::sun::star::util
;
52 using namespace ::com::sun::star::form
;
53 using namespace ::com::sun::star::beans
;
54 using namespace ::com::sun::star::script
;
55 using namespace ::com::sun::star::container
;
56 using namespace ::svxform
;
61 const OUString
& rModelName
)
62 : SdrUnoObj(rSdrModel
, rModelName
)
64 ,m_pLastKnownRefDevice(nullptr)
66 // normally, this is done in SetUnoControlModel, but if the call happened in the base class ctor,
67 // then our incarnation of it was not called (since we were not constructed at this time).
68 impl_checkRefDevice_nothrow( true );
71 FmFormObj::FmFormObj(SdrModel
& rSdrModel
)
72 : SdrUnoObj(rSdrModel
, "")
74 ,m_pLastKnownRefDevice(nullptr)
76 // Stuff that old SetModel also did:
77 impl_checkRefDevice_nothrow();
80 FmFormObj::~FmFormObj()
83 if (m_xEnvironmentHistory
.is())
84 m_xEnvironmentHistory
->dispose();
86 m_xEnvironmentHistory
= nullptr;
87 m_aEventsHistory
.realloc(0);
91 void FmFormObj::SetObjEnv(const Reference
< XIndexContainer
> & xForm
, const sal_Int32 nIdx
,
92 const Sequence
< ScriptEventDescriptor
>& rEvts
)
100 void FmFormObj::ClearObjEnv()
108 void FmFormObj::impl_checkRefDevice_nothrow( bool _force
)
110 const FmFormModel
* pFormModel
= dynamic_cast<FmFormModel
*>(&getSdrModelFromSdrObject());
111 if ( !pFormModel
|| !pFormModel
->ControlsUseRefDevice() )
114 OutputDevice
* pCurrentRefDevice
= pFormModel
->GetRefDevice();
115 if ( ( m_pLastKnownRefDevice
.get() == pCurrentRefDevice
) && !_force
)
118 Reference
< XControlModel
> xControlModel( GetUnoControlModel() );
119 if ( !xControlModel
.is() )
122 m_pLastKnownRefDevice
= pCurrentRefDevice
;
123 if ( !m_pLastKnownRefDevice
)
128 Reference
< XPropertySet
> xModelProps( GetUnoControlModel(), UNO_QUERY_THROW
);
129 Reference
< XPropertySetInfo
> xPropertyInfo( xModelProps
->getPropertySetInfo(), UNO_SET_THROW
);
131 static const char sRefDevicePropName
[] = "ReferenceDevice";
132 if ( xPropertyInfo
->hasPropertyByName( sRefDevicePropName
) )
134 VCLXDevice
* pUnoRefDevice
= new VCLXDevice
;
135 pUnoRefDevice
->SetOutputDevice( m_pLastKnownRefDevice
);
136 Reference
< XDevice
> xRefDevice( pUnoRefDevice
);
137 xModelProps
->setPropertyValue( sRefDevicePropName
, makeAny( xRefDevice
) );
140 catch( const Exception
& )
142 DBG_UNHANDLED_EXCEPTION("svx");
147 void FmFormObj::impl_isolateControlModel_nothrow()
151 Reference
< XChild
> xControlModel( GetUnoControlModel(), UNO_QUERY
);
152 if ( xControlModel
.is() )
154 Reference
< XIndexContainer
> xParent( xControlModel
->getParent(), UNO_QUERY
);
157 sal_Int32 nPos
= getElementPos( xParent
.get(), xControlModel
);
158 xParent
->removeByIndex( nPos
);
162 catch( const Exception
& )
164 DBG_UNHANDLED_EXCEPTION("svx");
169 void FmFormObj::handlePageChange(SdrPage
* pOldPage
, SdrPage
* pNewPage
)
171 FmFormPage
* pOldFormPage(dynamic_cast< FmFormPage
* >(getSdrPageFromSdrObject()));
173 pOldFormPage
->GetImpl().formObjectRemoved( *this );
175 FmFormPage
* pNewFormPage
= dynamic_cast<FmFormPage
*>( pNewPage
);
178 // 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,
179 // me may want to remember our place within the old page. For this we could create a new m_xEnvironmentHistory to store it.
180 // So the next SetPage with a valid new page would restore that environment within the new page.
181 // But for the original Bug (#57300#) we don't need that, so I omit it here. Maybe this will be implemented later.
182 impl_isolateControlModel_nothrow();
183 SdrUnoObj::handlePageChange(pOldPage
, pNewPage
);
187 Reference
< css::form::XForms
> xNewPageForms
= pNewFormPage
->GetForms();
188 Reference
< XIndexContainer
> xNewParent
;
189 Sequence
< ScriptEventDescriptor
> aNewEvents
;
191 // calc the new parent for my model (within the new page's forms hierarchy)
192 // do we have a history ? (from :Clone)
193 if ( m_xEnvironmentHistory
.is() )
195 // the element in m_xEnvironmentHistory which is equivalent to my new parent (which (perhaps) has to be created within pNewPage->GetForms)
196 // is the right-most element in the tree.
197 Reference
< XIndexContainer
> xRightMostLeaf( m_xEnvironmentHistory
, UNO_QUERY_THROW
);
200 while ( xRightMostLeaf
->getCount() )
203 xRightMostLeaf
->getByIndex( xRightMostLeaf
->getCount() - 1 ),
208 xNewParent
.set( ensureModelEnv( xRightMostLeaf
, xNewPageForms
), UNO_QUERY_THROW
);
210 // we successfully cloned the environment in m_xEnvironmentHistory, so we can use m_aEventsHistory
211 // (which describes the events of our model at the moment m_xEnvironmentHistory was created)
212 aNewEvents
= m_aEventsHistory
;
214 catch( const Exception
& )
216 DBG_UNHANDLED_EXCEPTION("svx");
220 if ( !xNewParent
.is() )
222 // are we a valid part of our current page forms ?
223 Reference
< XIndexContainer
> xOldForms
;
225 xOldForms
.set( pOldFormPage
->GetForms(), UNO_QUERY_THROW
);
227 if ( xOldForms
.is() )
229 // search (upward from our model) for xOldForms
230 Reference
< XChild
> xSearch( GetUnoControlModel(), UNO_QUERY
);
233 if ( xSearch
== xOldForms
)
235 xSearch
.set( xSearch
->getParent(), UNO_QUERY
);
237 if ( xSearch
.is() ) // implies xSearch == xOldForms, which means we're a valid part of our current page forms hierarchy
239 Reference
< XChild
> xMeAsChild( GetUnoControlModel(), UNO_QUERY
);
240 xNewParent
.set( ensureModelEnv( xMeAsChild
->getParent(), xNewPageForms
), UNO_QUERY
);
242 if ( xNewParent
.is() )
246 // transfer the events from our (model's) parent to the new (model's) parent, too
247 Reference
< XEventAttacherManager
> xEventManager(xMeAsChild
->getParent(), UNO_QUERY
);
248 Reference
< XIndexAccess
> xManagerAsIndex(xEventManager
, UNO_QUERY
);
249 if (xManagerAsIndex
.is())
251 sal_Int32 nPos
= getElementPos(xManagerAsIndex
, xMeAsChild
);
253 aNewEvents
= xEventManager
->getScriptEvents(nPos
);
258 catch( const Exception
& )
260 DBG_UNHANDLED_EXCEPTION("svx");
268 SdrUnoObj::handlePageChange(pOldPage
, pNewPage
);
270 // place my model within the new parent container
273 Reference
< XFormComponent
> xMeAsFormComp(GetUnoControlModel(), UNO_QUERY
);
274 if (xMeAsFormComp
.is())
276 // check if I have another parent (and remove me, if necessary)
277 Reference
< XIndexContainer
> xOldParent(xMeAsFormComp
->getParent(), UNO_QUERY
);
280 sal_Int32 nPos
= getElementPos(xOldParent
, xMeAsFormComp
);
282 xOldParent
->removeByIndex(nPos
);
285 // and insert into the new container
286 xNewParent
->insertByIndex(xNewParent
->getCount(), makeAny(xMeAsFormComp
));
288 // transfer the events
289 if (aNewEvents
.hasElements())
293 Reference
< XEventAttacherManager
> xEventManager(xNewParent
, UNO_QUERY
);
294 Reference
< XIndexAccess
> xManagerAsIndex(xEventManager
, UNO_QUERY
);
295 if (xManagerAsIndex
.is())
297 sal_Int32 nPos
= getElementPos(xManagerAsIndex
, xMeAsFormComp
);
298 DBG_ASSERT(nPos
>= 0, "FmFormObj::SetPage : inserted but not present ?");
299 xEventManager
->registerScriptEvents(nPos
, aNewEvents
);
302 catch( const Exception
& )
304 DBG_UNHANDLED_EXCEPTION("svx");
312 if (m_xEnvironmentHistory
.is())
313 m_xEnvironmentHistory
->dispose();
315 m_xEnvironmentHistory
= nullptr;
316 m_aEventsHistory
.realloc(0);
318 pNewFormPage
->GetImpl().formObjectInserted( *this );
322 SdrInventor
FmFormObj::GetObjInventor() const
324 return SdrInventor::FmForm
;
328 sal_uInt16
FmFormObj::GetObjIdentifier() const
334 void FmFormObj::clonedFrom(const FmFormObj
* _pSource
)
336 DBG_ASSERT(_pSource
!= nullptr, "FmFormObj::clonedFrom : invalid source !");
337 if (m_xEnvironmentHistory
.is())
338 m_xEnvironmentHistory
->dispose();
340 m_xEnvironmentHistory
= nullptr;
341 m_aEventsHistory
.realloc(0);
343 Reference
< XChild
> xSourceAsChild(_pSource
->GetUnoControlModel(), UNO_QUERY
);
344 if (!xSourceAsChild
.is())
347 Reference
< XInterface
> xSourceContainer
= xSourceAsChild
->getParent();
349 m_xEnvironmentHistory
= css::form::Forms::create( comphelper::getProcessComponentContext() );
351 ensureModelEnv(xSourceContainer
, m_xEnvironmentHistory
);
352 m_aEventsHistory
= aEvts
;
353 // if we were clone there was a call to operator=, so aEvts are exactly the events we need here...
357 FmFormObj
* FmFormObj::CloneSdrObject(SdrModel
& rTargetModel
) const
359 FmFormObj
* pFormObject
= CloneHelper
< FmFormObj
>(rTargetModel
);
360 DBG_ASSERT(pFormObject
!= nullptr, "FmFormObj::Clone : invalid clone !");
362 pFormObject
->clonedFrom(this);
368 FmFormObj
& FmFormObj::operator= (const FmFormObj
& rObj
)
372 SdrUnoObj::operator= (rObj
);
374 // If UnoControlModel is part of an event environment,
375 // events may assigned to it.
376 Reference
< XFormComponent
> xContent(rObj
.xUnoControlModel
, UNO_QUERY
);
379 Reference
< XEventAttacherManager
> xManager(xContent
->getParent(), UNO_QUERY
);
380 Reference
< XIndexAccess
> xManagerAsIndex(xManager
, UNO_QUERY
);
381 if (xManagerAsIndex
.is())
383 sal_Int32 nPos
= getElementPos( xManagerAsIndex
, xContent
);
385 aEvts
= xManager
->getScriptEvents( nPos
);
394 void FmFormObj::NbcReformatText()
396 impl_checkRefDevice_nothrow();
397 SdrUnoObj::NbcReformatText();
403 OUString
lcl_getFormComponentAccessPath(const Reference
< XInterface
>& _xElement
, Reference
< XInterface
>& _rTopLevelElement
)
405 Reference
< css::form::XFormComponent
> xChild(_xElement
, UNO_QUERY
);
406 Reference
< css::container::XIndexAccess
> xParent
;
408 xParent
.set(xChild
->getParent(), UNO_QUERY
);
410 // while the current content is a form
414 // get the content's relative pos within its parent container
415 sal_Int32 nPos
= getElementPos(xParent
, xChild
);
417 // prepend this current relative pos
418 OUString sCurrentIndex
= OUString::number(nPos
);
419 if (!sReturn
.isEmpty())
421 sCurrentIndex
+= "\\" + sReturn
;
424 sReturn
= sCurrentIndex
;
427 xChild
.set(xParent
, css::uno::UNO_QUERY
);
429 xParent
.set(xChild
->getParent(), UNO_QUERY
);
432 _rTopLevelElement
= xParent
;
438 Reference
< XInterface
> FmFormObj::ensureModelEnv(const Reference
< XInterface
> & _rSourceContainer
, const Reference
<css::form::XForms
>& _rTopLevelDestContainer
)
440 Reference
< XInterface
> xTopLevelSource
;
441 OUString sAccessPath
= lcl_getFormComponentAccessPath(_rSourceContainer
, xTopLevelSource
);
442 if (!xTopLevelSource
.is())
443 // something went wrong, maybe _rSourceContainer isn't part of a valid forms hierarchy
444 return Reference
< XInterface
> ();
446 Reference
< XIndexContainer
> xDestContainer(_rTopLevelDestContainer
, UNO_QUERY_THROW
);
447 Reference
< XIndexContainer
> xSourceContainer(xTopLevelSource
, UNO_QUERY
);
448 DBG_ASSERT(xSourceContainer
.is(), "FmFormObj::ensureModelEnv : the top level source is invalid !");
450 sal_Int32 nTokIndex
= 0;
453 OUString aToken
= sAccessPath
.getToken( 0, '\\', nTokIndex
);
454 sal_uInt16 nIndex
= static_cast<sal_uInt16
>(aToken
.toInt32());
456 // get the DSS of the source form (we have to find an equivalent for)
457 DBG_ASSERT(nIndex
<xSourceContainer
->getCount(), "FmFormObj::ensureModelEnv : invalid access path !");
458 Reference
< XPropertySet
> xSourceForm
;
459 xSourceContainer
->getByIndex(nIndex
) >>= xSourceForm
;
460 DBG_ASSERT(xSourceForm
.is(), "FmFormObj::ensureModelEnv : invalid source form !");
462 Any aSrcCursorSource
, aSrcCursorSourceType
, aSrcDataSource
;
463 DBG_ASSERT(::comphelper::hasProperty(FM_PROP_COMMAND
, xSourceForm
) && ::comphelper::hasProperty(FM_PROP_COMMANDTYPE
, xSourceForm
)
464 && ::comphelper::hasProperty(FM_PROP_DATASOURCE
, xSourceForm
), "FmFormObj::ensureModelEnv : invalid access path or invalid form (missing props) !");
465 // the parent access path should refer to a row set
468 aSrcCursorSource
= xSourceForm
->getPropertyValue(FM_PROP_COMMAND
);
469 aSrcCursorSourceType
= xSourceForm
->getPropertyValue(FM_PROP_COMMANDTYPE
);
470 aSrcDataSource
= xSourceForm
->getPropertyValue(FM_PROP_DATASOURCE
);
474 OSL_FAIL("FmFormObj::ensureModelEnv : could not retrieve a source DSS !");
478 // calc the number of (source) form siblings with the same DSS
479 Reference
< XPropertySet
> xCurrentSourceForm
, xCurrentDestForm
;
480 sal_Int16 nCurrentSourceIndex
= 0;
481 sal_Int32 nCurrentDestIndex
= 0;
482 while (nCurrentSourceIndex
<= nIndex
)
484 bool bEqualDSS
= false;
485 while (!bEqualDSS
) // (we don't have to check nCurrentSourceIndex here : it's bound by nIndex)
487 xSourceContainer
->getByIndex(nCurrentSourceIndex
) >>= xCurrentSourceForm
;
488 DBG_ASSERT(xCurrentSourceForm
.is(), "FmFormObj::ensureModelEnv : invalid form ancestor (2) !");
490 if (::comphelper::hasProperty(FM_PROP_DATASOURCE
, xCurrentSourceForm
))
494 if ( xCurrentSourceForm
->getPropertyValue(FM_PROP_COMMAND
) == aSrcCursorSource
495 && xCurrentSourceForm
->getPropertyValue(FM_PROP_COMMANDTYPE
) == aSrcCursorSourceType
496 && xCurrentSourceForm
->getPropertyValue(FM_PROP_DATASOURCE
) == aSrcDataSource
504 OSL_FAIL("FmFormObj::ensureModelEnv : exception while getting a sibling's DSS !");
508 ++nCurrentSourceIndex
;
511 DBG_ASSERT(bEqualDSS
, "FmFormObj::ensureModelEnv : found no source form !");
512 // ??? at least the nIndex-th one should have been found ???
514 // now search the next one with the given DSS (within the destination container)
516 while (!bEqualDSS
&& (nCurrentDestIndex
< xDestContainer
->getCount()))
518 xDestContainer
->getByIndex(nCurrentDestIndex
) >>= xCurrentDestForm
;
519 DBG_ASSERT(xCurrentDestForm
.is(), "FmFormObj::ensureModelEnv : invalid destination form !");
521 if (::comphelper::hasProperty(FM_PROP_DATASOURCE
, xCurrentDestForm
))
525 if ( xCurrentDestForm
->getPropertyValue(FM_PROP_COMMAND
) == aSrcCursorSource
526 && xCurrentDestForm
->getPropertyValue(FM_PROP_COMMANDTYPE
) == aSrcCursorSourceType
527 && xCurrentDestForm
->getPropertyValue(FM_PROP_DATASOURCE
) == aSrcDataSource
535 OSL_FAIL("FmFormObj::ensureModelEnv : exception while getting a destination DSS !");
543 { // There is at least one more source form with the given DSS than destination forms are.
547 // create and insert (into the destination) a copy of the form
548 xCurrentDestForm
.set(
549 ::comphelper::getProcessServiceFactory()->createInstance("com.sun.star.form.component.DataForm"),
551 ::comphelper::copyProperties( xCurrentSourceForm
, xCurrentDestForm
);
553 DBG_ASSERT(nCurrentDestIndex
== xDestContainer
->getCount(), "FmFormObj::ensureModelEnv : something went wrong with the numbers !");
554 xDestContainer
->insertByIndex(nCurrentDestIndex
, makeAny(xCurrentDestForm
));
557 // like nCurrentSourceIndex, nCurrentDestIndex now points 'behind' the form it actually means
561 OSL_FAIL("FmFormObj::ensureModelEnv : something went seriously wrong while creating a new form !");
562 // no more options anymore ...
563 return Reference
< XInterface
> ();
569 // now xCurrentDestForm is a form equivalent to xSourceForm (which means they have the same DSS and the same number
570 // of left siblings with the same DSS, which counts for all their ancestors, too)
573 xDestContainer
.set(xCurrentDestForm
, UNO_QUERY
);
574 xSourceContainer
.set(xSourceForm
, UNO_QUERY
);
575 DBG_ASSERT(xDestContainer
.is() && xSourceContainer
.is(), "FmFormObj::ensureModelEnv : invalid container !");
577 while ( nTokIndex
>= 0 );
579 return Reference
<XInterface
>( xDestContainer
, UNO_QUERY
);
582 FmFormObj
* FmFormObj::GetFormObject( SdrObject
* _pSdrObject
)
584 FmFormObj
* pFormObject
= dynamic_cast< FmFormObj
* >( _pSdrObject
);
587 SdrVirtObj
* pVirtualObject
= dynamic_cast< SdrVirtObj
* >( _pSdrObject
);
588 if ( pVirtualObject
)
589 pFormObject
= dynamic_cast< FmFormObj
* >( &pVirtualObject
->ReferencedObj() );
595 const FmFormObj
* FmFormObj::GetFormObject( const SdrObject
* _pSdrObject
)
597 const FmFormObj
* pFormObject
= dynamic_cast< const FmFormObj
* >( _pSdrObject
);
600 const SdrVirtObj
* pVirtualObject
= dynamic_cast< const SdrVirtObj
* >( _pSdrObject
);
601 if ( pVirtualObject
)
602 pFormObject
= dynamic_cast< const FmFormObj
* >( &pVirtualObject
->GetReferencedObj() );
608 void FmFormObj::SetUnoControlModel( const Reference
< css::awt::XControlModel
>& _rxModel
)
610 SdrUnoObj::SetUnoControlModel( _rxModel
);
612 FmFormPage
* pFormPage(dynamic_cast< FmFormPage
* >(getSdrPageFromSdrObject()));
614 pFormPage
->GetImpl().formModelAssigned( *this );
616 impl_checkRefDevice_nothrow( true );
620 bool FmFormObj::EndCreate( SdrDragStat
& rStat
, SdrCreateCmd eCmd
)
622 bool bResult
= SdrUnoObj::EndCreate(rStat
, eCmd
);
623 if ( bResult
&& SdrCreateCmd::ForceEnd
== eCmd
&& rStat
.GetView() )
625 FmFormPage
* pFormPage(dynamic_cast< FmFormPage
* >(getSdrPageFromSdrObject()));
627 if (nullptr != pFormPage
)
631 Reference
< XFormComponent
> xContent( xUnoControlModel
, UNO_QUERY_THROW
);
632 Reference
< XForm
> xParentForm( xContent
->getParent(), UNO_QUERY
);
634 Reference
< XIndexContainer
> xFormToInsertInto
;
636 if ( !xParentForm
.is() )
637 { // model is not yet part of a form component hierarchy
638 xParentForm
.set( pFormPage
->GetImpl().findPlaceInFormComponentHierarchy( xContent
), UNO_SET_THROW
);
639 xFormToInsertInto
.set( xParentForm
, UNO_QUERY_THROW
);
642 FmFormPageImpl::setUniqueName( xContent
, xParentForm
);
644 if ( xFormToInsertInto
.is() )
645 xFormToInsertInto
->insertByIndex( xFormToInsertInto
->getCount(), makeAny( xContent
) );
647 catch( const Exception
& )
649 DBG_UNHANDLED_EXCEPTION("svx");
653 FmFormView
* pView( dynamic_cast< FmFormView
* >( rStat
.GetView() ) );
654 FmXFormView
* pViewImpl
= pView
? pView
->GetImpl() : nullptr;
655 OSL_ENSURE( pViewImpl
, "FmFormObj::EndCreate: no view!?" );
657 pViewImpl
->onCreatedFormObject( *this );
663 void FmFormObj::BrkCreate( SdrDragStat
& rStat
)
665 SdrUnoObj::BrkCreate( rStat
);
666 impl_isolateControlModel_nothrow();
668 FmFormView
* pView( dynamic_cast< FmFormView
* >( rStat
.GetView() ) );
669 FmXFormView
* pViewImpl
= pView
? pView
->GetImpl() : nullptr;
670 OSL_ENSURE( pViewImpl
, "FmFormObj::EndCreate: no view!?" );
672 pViewImpl
->breakCreateFormObject();
676 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */