xref: /AOO41X/main/svx/source/form/fmpgeimp.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_svx.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "svx/svxerr.hxx"
32*cdf0e10cSrcweir #include "fmpgeimp.hxx"
33*cdf0e10cSrcweir #include "fmundo.hxx"
34*cdf0e10cSrcweir #include "svx/fmtools.hxx"
35*cdf0e10cSrcweir #include "fmprop.hrc"
36*cdf0e10cSrcweir #include "fmservs.hxx"
37*cdf0e10cSrcweir #include "fmobj.hxx"
38*cdf0e10cSrcweir #include "formcontrolfactory.hxx"
39*cdf0e10cSrcweir #include "svx/svditer.hxx"
40*cdf0e10cSrcweir #include "svx/fmresids.hrc"
41*cdf0e10cSrcweir #include "svx/dbtoolsclient.hxx"
42*cdf0e10cSrcweir #include "treevisitor.hxx"
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #include <com/sun/star/sdb/CommandType.hpp>
45*cdf0e10cSrcweir #include <com/sun/star/util/XCloneable.hpp>
46*cdf0e10cSrcweir #include <com/sun/star/container/EnumerableMap.hpp>
47*cdf0e10cSrcweir #include <com/sun/star/drawing/XControlShape.hpp>
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #include <sfx2/objsh.hxx>
50*cdf0e10cSrcweir #include <svx/fmglob.hxx>
51*cdf0e10cSrcweir #include <svx/fmpage.hxx>
52*cdf0e10cSrcweir #include <svx/fmmodel.hxx>
53*cdf0e10cSrcweir #include <tools/resid.hxx>
54*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
55*cdf0e10cSrcweir #include <tools/shl.hxx>
56*cdf0e10cSrcweir #include <vcl/stdtext.hxx>
57*cdf0e10cSrcweir #include <svx/dialmgr.hxx>
58*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
59*cdf0e10cSrcweir #include <comphelper/componentcontext.hxx>
60*cdf0e10cSrcweir #include <comphelper/uno3.hxx>
61*cdf0e10cSrcweir #include <comphelper/types.hxx>
62*cdf0e10cSrcweir #include <unotools/streamwrap.hxx>
63*cdf0e10cSrcweir #include <rtl/logfile.hxx>
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
66*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
67*cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
68*cdf0e10cSrcweir using namespace ::com::sun::star::sdb;
69*cdf0e10cSrcweir using namespace ::com::sun::star::container;
70*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
71*cdf0e10cSrcweir using namespace ::com::sun::star::form;
72*cdf0e10cSrcweir using ::com::sun::star::util::XCloneable;
73*cdf0e10cSrcweir using ::com::sun::star::awt::XControlModel;
74*cdf0e10cSrcweir using ::com::sun::star::container::XMap;
75*cdf0e10cSrcweir using ::com::sun::star::container::EnumerableMap;
76*cdf0e10cSrcweir using ::com::sun::star::drawing::XControlShape;
77*cdf0e10cSrcweir using namespace ::svxform;
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir DBG_NAME(FmFormPageImpl)
80*cdf0e10cSrcweir //------------------------------------------------------------------------------
81*cdf0e10cSrcweir FmFormPageImpl::FmFormPageImpl( FmFormPage& _rPage )
82*cdf0e10cSrcweir                :m_rPage( _rPage )
83*cdf0e10cSrcweir 			   ,m_bFirstActivation( sal_True )
84*cdf0e10cSrcweir                ,m_bAttemptedFormCreation( false )
85*cdf0e10cSrcweir                ,m_bInFind( false )
86*cdf0e10cSrcweir {
87*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "svx", "Ocke.Janssen@sun.com", "FmFormPageImpl::FmFormPageImpl" );
88*cdf0e10cSrcweir     DBG_CTOR(FmFormPageImpl,NULL);
89*cdf0e10cSrcweir }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir //------------------------------------------------------------------------------
92*cdf0e10cSrcweir namespace
93*cdf0e10cSrcweir {
94*cdf0e10cSrcweir     typedef Reference< XInterface > FormComponent;
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir     class FormComponentInfo
97*cdf0e10cSrcweir     {
98*cdf0e10cSrcweir     public:
99*cdf0e10cSrcweir         size_t childCount( const FormComponent& _component ) const
100*cdf0e10cSrcweir         {
101*cdf0e10cSrcweir             Reference< XIndexAccess > xContainer( _component, UNO_QUERY );
102*cdf0e10cSrcweir             if ( xContainer.is() )
103*cdf0e10cSrcweir                 return xContainer->getCount();
104*cdf0e10cSrcweir             return 0;
105*cdf0e10cSrcweir         }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir         FormComponent getChild( const FormComponent& _component, size_t _index ) const
108*cdf0e10cSrcweir         {
109*cdf0e10cSrcweir             Reference< XIndexAccess > xContainer( _component, UNO_QUERY_THROW );
110*cdf0e10cSrcweir             return FormComponent( xContainer->getByIndex( _index ), UNO_QUERY );
111*cdf0e10cSrcweir         }
112*cdf0e10cSrcweir     };
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir     typedef ::std::pair< FormComponent, FormComponent > FormComponentPair;
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir     class FormHierarchyComparator
117*cdf0e10cSrcweir     {
118*cdf0e10cSrcweir     public:
119*cdf0e10cSrcweir         FormHierarchyComparator()
120*cdf0e10cSrcweir         {
121*cdf0e10cSrcweir         }
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir         size_t childCount( const FormComponentPair& _components ) const
124*cdf0e10cSrcweir         {
125*cdf0e10cSrcweir             size_t lhsCount = m_aComponentInfo.childCount( _components.first );
126*cdf0e10cSrcweir             size_t rhsCount = m_aComponentInfo.childCount( _components.second );
127*cdf0e10cSrcweir             if  ( lhsCount != rhsCount )
128*cdf0e10cSrcweir                 throw RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Found inconsistent form component hierarchies (1)!" ) ), NULL );
129*cdf0e10cSrcweir             return lhsCount;
130*cdf0e10cSrcweir         }
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir         FormComponentPair getChild( const FormComponentPair& _components, size_t _index ) const
133*cdf0e10cSrcweir         {
134*cdf0e10cSrcweir             return FormComponentPair(
135*cdf0e10cSrcweir                 m_aComponentInfo.getChild( _components.first, _index ),
136*cdf0e10cSrcweir                 m_aComponentInfo.getChild( _components.second, _index )
137*cdf0e10cSrcweir             );
138*cdf0e10cSrcweir         }
139*cdf0e10cSrcweir     private:
140*cdf0e10cSrcweir         FormComponentInfo   m_aComponentInfo;
141*cdf0e10cSrcweir     };
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir     typedef ::std::map< Reference< XControlModel >, Reference< XControlModel >, ::comphelper::OInterfaceCompare< XControlModel > > MapControlModels;
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir     class FormComponentAssignment
146*cdf0e10cSrcweir     {
147*cdf0e10cSrcweir     public:
148*cdf0e10cSrcweir         FormComponentAssignment( MapControlModels& _out_controlModelMap )
149*cdf0e10cSrcweir             :m_rControlModelMap( _out_controlModelMap )
150*cdf0e10cSrcweir         {
151*cdf0e10cSrcweir         }
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir         void    process( const FormComponentPair& _component )
154*cdf0e10cSrcweir         {
155*cdf0e10cSrcweir             Reference< XControlModel > lhsControlModel( _component.first, UNO_QUERY );
156*cdf0e10cSrcweir             Reference< XControlModel > rhsControlModel( _component.second, UNO_QUERY );
157*cdf0e10cSrcweir             if ( lhsControlModel.is() != rhsControlModel.is() )
158*cdf0e10cSrcweir                 throw RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Found inconsistent form component hierarchies (2)!" ) ), NULL );
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir             if ( lhsControlModel.is() )
161*cdf0e10cSrcweir                 m_rControlModelMap[ lhsControlModel ] = rhsControlModel;
162*cdf0e10cSrcweir         }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir     private:
165*cdf0e10cSrcweir         MapControlModels&   m_rControlModelMap;
166*cdf0e10cSrcweir     };
167*cdf0e10cSrcweir }
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir //------------------------------------------------------------------------------
170*cdf0e10cSrcweir void FmFormPageImpl::initFrom( FmFormPageImpl& i_foreignImpl )
171*cdf0e10cSrcweir {
172*cdf0e10cSrcweir     // clone the Forms collection
173*cdf0e10cSrcweir     const Reference< XNameContainer > xForeignForms( const_cast< FmFormPageImpl& >( i_foreignImpl ).getForms( false ) );
174*cdf0e10cSrcweir     const Reference< XCloneable > xCloneable( xForeignForms, UNO_QUERY );
175*cdf0e10cSrcweir     if ( !xCloneable.is() )
176*cdf0e10cSrcweir     {
177*cdf0e10cSrcweir         // great, nothing to do
178*cdf0e10cSrcweir         OSL_ENSURE( !xForeignForms.is(), "FmFormPageImpl::FmFormPageImpl: a non-cloneable forms container!?" );
179*cdf0e10cSrcweir         return;
180*cdf0e10cSrcweir     }
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir     try
183*cdf0e10cSrcweir     {
184*cdf0e10cSrcweir         m_xForms.set( xCloneable->createClone(), UNO_QUERY_THROW );
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir         // create a mapping between the original control models and their clones
187*cdf0e10cSrcweir         MapControlModels aModelAssignment;
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir         typedef TreeVisitor< FormComponentPair, FormHierarchyComparator, FormComponentAssignment >   FormComponentVisitor;
190*cdf0e10cSrcweir         FormComponentVisitor aVisitor = FormComponentVisitor( FormHierarchyComparator() );
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir         FormComponentAssignment aAssignmentProcessor( aModelAssignment );
193*cdf0e10cSrcweir         aVisitor.process( FormComponentPair( xCloneable, m_xForms ), aAssignmentProcessor );
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir         // assign the cloned models to their SdrObjects
196*cdf0e10cSrcweir         SdrObjListIter aForeignIter( i_foreignImpl.m_rPage );
197*cdf0e10cSrcweir         SdrObjListIter aOwnIter( m_rPage );
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir         OSL_ENSURE( aForeignIter.IsMore() == aOwnIter.IsMore(), "FmFormPageImpl::FmFormPageImpl: inconsistent number of objects (1)!" );
200*cdf0e10cSrcweir         while ( aForeignIter.IsMore() && aOwnIter.IsMore() )
201*cdf0e10cSrcweir         {
202*cdf0e10cSrcweir             FmFormObj* pForeignObj = dynamic_cast< FmFormObj* >( aForeignIter.Next() );
203*cdf0e10cSrcweir             FmFormObj* pOwnObj = dynamic_cast< FmFormObj* >( aOwnIter.Next() );
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir             bool bForeignIsForm = pForeignObj && ( pForeignObj->GetObjInventor() == FmFormInventor );
206*cdf0e10cSrcweir             bool bOwnIsForm = pOwnObj && ( pOwnObj->GetObjInventor() == FmFormInventor );
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir             ENSURE_OR_BREAK( bForeignIsForm == bOwnIsForm, "FmFormPageImpl::FmFormPageImpl: inconsistent ordering of objects!" );
209*cdf0e10cSrcweir                 // if this fires, don't attempt to do further assignments, something's completely messed up
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir             if ( !bForeignIsForm )
212*cdf0e10cSrcweir                 // no form control -> next round
213*cdf0e10cSrcweir                 continue;
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir             Reference< XControlModel > xForeignModel( pForeignObj->GetUnoControlModel() );
216*cdf0e10cSrcweir             ENSURE_OR_CONTINUE( xForeignModel.is(), "FmFormPageImpl::FmFormPageImpl: control shape without control!" );
217*cdf0e10cSrcweir                 // if this fires, the SdrObject does not have a UNO Control Model. This is pathological, but well ...
218*cdf0e10cSrcweir                 // So the cloned SdrObject will also not have a UNO Control Model.
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir             MapControlModels::const_iterator assignment = aModelAssignment.find( xForeignModel );
221*cdf0e10cSrcweir             ENSURE_OR_CONTINUE( assignment != aModelAssignment.end(), "FmFormPageImpl::FmFormPageImpl: no clone found for this model!" );
222*cdf0e10cSrcweir                 // if this fires, the source SdrObject has a model, but it is not part of the model hierarchy in
223*cdf0e10cSrcweir                 // i_foreignImpl.getForms().
224*cdf0e10cSrcweir                 // Pathological, too ...
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir             pOwnObj->SetUnoControlModel( assignment->second );
227*cdf0e10cSrcweir         }
228*cdf0e10cSrcweir         OSL_ENSURE( aForeignIter.IsMore() == aOwnIter.IsMore(), "FmFormPageImpl::FmFormPageImpl: inconsistent number of objects (2)!" );
229*cdf0e10cSrcweir     }
230*cdf0e10cSrcweir     catch( const Exception& )
231*cdf0e10cSrcweir     {
232*cdf0e10cSrcweir     	DBG_UNHANDLED_EXCEPTION();
233*cdf0e10cSrcweir     }
234*cdf0e10cSrcweir }
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir //------------------------------------------------------------------------------
237*cdf0e10cSrcweir Reference< XMap > FmFormPageImpl::getControlToShapeMap()
238*cdf0e10cSrcweir {
239*cdf0e10cSrcweir     Reference< XMap > xControlShapeMap( m_aControlShapeMap.get(), UNO_QUERY );
240*cdf0e10cSrcweir     if ( xControlShapeMap.is() )
241*cdf0e10cSrcweir         return xControlShapeMap;
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir     xControlShapeMap = impl_createControlShapeMap_nothrow();
244*cdf0e10cSrcweir     m_aControlShapeMap = xControlShapeMap;
245*cdf0e10cSrcweir     return xControlShapeMap;
246*cdf0e10cSrcweir }
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir //------------------------------------------------------------------------------
249*cdf0e10cSrcweir namespace
250*cdf0e10cSrcweir {
251*cdf0e10cSrcweir     static void lcl_insertFormObject_throw( const FmFormObj& _object, const Reference< XMap >& _map )
252*cdf0e10cSrcweir     {
253*cdf0e10cSrcweir         // the control model
254*cdf0e10cSrcweir         Reference< XControlModel > xControlModel( _object.GetUnoControlModel(), UNO_QUERY );
255*cdf0e10cSrcweir         OSL_ENSURE( xControlModel.is(), "lcl_insertFormObject_throw: suspicious: no control model!" );
256*cdf0e10cSrcweir         if ( !xControlModel.is() )
257*cdf0e10cSrcweir             return;
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir         Reference< XControlShape > xControlShape( const_cast< FmFormObj& >( _object ).getUnoShape(), UNO_QUERY );
260*cdf0e10cSrcweir         OSL_ENSURE( xControlShape.is(), "lcl_insertFormObject_throw: suspicious: no control shape!" );
261*cdf0e10cSrcweir         if ( !xControlShape.is() )
262*cdf0e10cSrcweir             return;
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir         _map->put( makeAny( xControlModel ), makeAny( xControlShape ) );
265*cdf0e10cSrcweir     }
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir     static void lcl_removeFormObject_throw( const FmFormObj& _object, const Reference< XMap >& _map, bool i_ignoreNonExistence = false )
268*cdf0e10cSrcweir     {
269*cdf0e10cSrcweir         // the control model
270*cdf0e10cSrcweir         Reference< XControlModel > xControlModel( _object.GetUnoControlModel(), UNO_QUERY );
271*cdf0e10cSrcweir         OSL_ENSURE( xControlModel.is(), "lcl_removeFormObject: suspicious: no control model!" );
272*cdf0e10cSrcweir         if ( !xControlModel.is() )
273*cdf0e10cSrcweir             return;
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir     #if OSL_DEBUG_LEVEL > 0
276*cdf0e10cSrcweir         Any aOldAssignment =
277*cdf0e10cSrcweir     #endif
278*cdf0e10cSrcweir             _map->remove( makeAny( xControlModel ) );
279*cdf0e10cSrcweir     #if OSL_DEBUG_LEVEL > 0
280*cdf0e10cSrcweir         (void)aOldAssignment;
281*cdf0e10cSrcweir     #endif
282*cdf0e10cSrcweir         OSL_ENSURE( !i_ignoreNonExistence ||
283*cdf0e10cSrcweir             ( aOldAssignment == makeAny( Reference< XControlShape >( const_cast< FmFormObj& >( _object ).getUnoShape(), UNO_QUERY ) ) ),
284*cdf0e10cSrcweir                 "lcl_removeFormObject: map was inconsistent!" );
285*cdf0e10cSrcweir         (void)i_ignoreNonExistence;
286*cdf0e10cSrcweir     }
287*cdf0e10cSrcweir }
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir //------------------------------------------------------------------------------
290*cdf0e10cSrcweir Reference< XMap > FmFormPageImpl::impl_createControlShapeMap_nothrow()
291*cdf0e10cSrcweir {
292*cdf0e10cSrcweir     Reference< XMap > xMap;
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir     try
295*cdf0e10cSrcweir     {
296*cdf0e10cSrcweir         ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() );
297*cdf0e10cSrcweir         xMap.set( EnumerableMap::create( aContext.getUNOContext(),
298*cdf0e10cSrcweir             ::cppu::UnoType< XControlModel >::get(),
299*cdf0e10cSrcweir             ::cppu::UnoType< XControlShape >::get()
300*cdf0e10cSrcweir         ).get(), UNO_SET_THROW );
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir         SdrObjListIter aPageIter( m_rPage );
303*cdf0e10cSrcweir         while ( aPageIter.IsMore() )
304*cdf0e10cSrcweir         {
305*cdf0e10cSrcweir             // only FmFormObjs are what we're interested in
306*cdf0e10cSrcweir             FmFormObj* pCurrent = FmFormObj::GetFormObject( aPageIter.Next() );
307*cdf0e10cSrcweir             if ( !pCurrent )
308*cdf0e10cSrcweir                 continue;
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir             lcl_insertFormObject_throw( *pCurrent, xMap );
311*cdf0e10cSrcweir         }
312*cdf0e10cSrcweir     }
313*cdf0e10cSrcweir     catch( const Exception& )
314*cdf0e10cSrcweir     {
315*cdf0e10cSrcweir     	DBG_UNHANDLED_EXCEPTION();
316*cdf0e10cSrcweir     }
317*cdf0e10cSrcweir     return xMap;
318*cdf0e10cSrcweir }
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir //------------------------------------------------------------------------------
321*cdf0e10cSrcweir const Reference< XNameContainer >& FmFormPageImpl::getForms( bool _bForceCreate )
322*cdf0e10cSrcweir {
323*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "svx", "Ocke.Janssen@sun.com", "FmFormPageImpl::getForms" );
324*cdf0e10cSrcweir     if ( m_xForms.is() || !_bForceCreate )
325*cdf0e10cSrcweir         return m_xForms;
326*cdf0e10cSrcweir 
327*cdf0e10cSrcweir     if ( !m_bAttemptedFormCreation )
328*cdf0e10cSrcweir     {
329*cdf0e10cSrcweir         m_bAttemptedFormCreation = true;
330*cdf0e10cSrcweir 
331*cdf0e10cSrcweir         const ::rtl::OUString sFormsCollectionServiceName = ::rtl::OUString::createFromAscii("com.sun.star.form.Forms");
332*cdf0e10cSrcweir         m_xForms = Reference< XNameContainer > (
333*cdf0e10cSrcweir             ::comphelper::getProcessServiceFactory()->createInstance( sFormsCollectionServiceName ),
334*cdf0e10cSrcweir             UNO_QUERY
335*cdf0e10cSrcweir         );
336*cdf0e10cSrcweir         DBG_ASSERT( m_xForms.is(), "FmFormPageImpl::getForms: could not create a forms collection!" );
337*cdf0e10cSrcweir 
338*cdf0e10cSrcweir         if ( m_aFormsCreationHdl.IsSet() )
339*cdf0e10cSrcweir         {
340*cdf0e10cSrcweir             m_aFormsCreationHdl.Call( this );
341*cdf0e10cSrcweir         }
342*cdf0e10cSrcweir 
343*cdf0e10cSrcweir         FmFormModel* pFormsModel = PTR_CAST( FmFormModel, m_rPage.GetModel() );
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir         // give the newly created collection a place in the universe
346*cdf0e10cSrcweir         Reference< XChild > xAsChild( m_xForms, UNO_QUERY );
347*cdf0e10cSrcweir         if ( xAsChild.is() )
348*cdf0e10cSrcweir         {
349*cdf0e10cSrcweir             SfxObjectShell* pObjShell = pFormsModel ? pFormsModel->GetObjectShell() : NULL;
350*cdf0e10cSrcweir             if ( pObjShell )
351*cdf0e10cSrcweir                 xAsChild->setParent( pObjShell->GetModel() );
352*cdf0e10cSrcweir         }
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir         // tell the UNDO environment that we have a new forms collection
355*cdf0e10cSrcweir         if ( pFormsModel )
356*cdf0e10cSrcweir             pFormsModel->GetUndoEnv().AddForms( m_xForms );
357*cdf0e10cSrcweir     }
358*cdf0e10cSrcweir     return m_xForms;
359*cdf0e10cSrcweir }
360*cdf0e10cSrcweir 
361*cdf0e10cSrcweir //------------------------------------------------------------------------------
362*cdf0e10cSrcweir FmFormPageImpl::~FmFormPageImpl()
363*cdf0e10cSrcweir {
364*cdf0e10cSrcweir     xCurrentForm = NULL;
365*cdf0e10cSrcweir 
366*cdf0e10cSrcweir     ::comphelper::disposeComponent( m_xForms );
367*cdf0e10cSrcweir     DBG_DTOR(FmFormPageImpl,NULL);
368*cdf0e10cSrcweir }
369*cdf0e10cSrcweir 
370*cdf0e10cSrcweir //------------------------------------------------------------------------------
371*cdf0e10cSrcweir bool FmFormPageImpl::validateCurForm()
372*cdf0e10cSrcweir {
373*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "svx", "Ocke.Janssen@sun.com", "FmFormPageImpl::validateCurForm" );
374*cdf0e10cSrcweir     if ( !xCurrentForm.is() )
375*cdf0e10cSrcweir         return false;
376*cdf0e10cSrcweir 
377*cdf0e10cSrcweir     Reference< XChild > xAsChild( xCurrentForm, UNO_QUERY );
378*cdf0e10cSrcweir     DBG_ASSERT( xAsChild.is(), "FmFormPageImpl::validateCurForm: a form which is no child??" );
379*cdf0e10cSrcweir     if ( !xAsChild.is() || !xAsChild->getParent().is() )
380*cdf0e10cSrcweir         xCurrentForm.clear();
381*cdf0e10cSrcweir 
382*cdf0e10cSrcweir     return xCurrentForm.is();
383*cdf0e10cSrcweir }
384*cdf0e10cSrcweir 
385*cdf0e10cSrcweir //------------------------------------------------------------------------------
386*cdf0e10cSrcweir void FmFormPageImpl::setCurForm(Reference< ::com::sun::star::form::XForm >  xForm)
387*cdf0e10cSrcweir {
388*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "svx", "Ocke.Janssen@sun.com", "FmFormPageImpl::setCurForm" );
389*cdf0e10cSrcweir     xCurrentForm = xForm;
390*cdf0e10cSrcweir }
391*cdf0e10cSrcweir 
392*cdf0e10cSrcweir //------------------------------------------------------------------------------
393*cdf0e10cSrcweir Reference< XForm >  FmFormPageImpl::getDefaultForm()
394*cdf0e10cSrcweir {
395*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "svx", "Ocke.Janssen@sun.com", "FmFormPageImpl::getDefaultForm" );
396*cdf0e10cSrcweir     Reference< XForm > xForm;
397*cdf0e10cSrcweir 
398*cdf0e10cSrcweir     Reference< XNameContainer > xForms( getForms() );
399*cdf0e10cSrcweir 
400*cdf0e10cSrcweir     // by default, we use our "current form"
401*cdf0e10cSrcweir     if ( !validateCurForm() )
402*cdf0e10cSrcweir 	{
403*cdf0e10cSrcweir         // check whether there is a "standard" form
404*cdf0e10cSrcweir 		if ( xForms->hasElements() )
405*cdf0e10cSrcweir 		{
406*cdf0e10cSrcweir 			// suche die Standardform
407*cdf0e10cSrcweir 			::rtl::OUString sStandardFormname = String( SVX_RES( RID_STR_STDFORMNAME ) );
408*cdf0e10cSrcweir 
409*cdf0e10cSrcweir             try
410*cdf0e10cSrcweir             {
411*cdf0e10cSrcweir 			    if ( xForms->hasByName( sStandardFormname ) )
412*cdf0e10cSrcweir                     xForm.set( xForms->getByName( sStandardFormname ), UNO_QUERY_THROW );
413*cdf0e10cSrcweir                 else
414*cdf0e10cSrcweir 			    {
415*cdf0e10cSrcweir 				    Reference< XIndexAccess > xFormsByIndex( xForms, UNO_QUERY_THROW );
416*cdf0e10cSrcweir 				    xForm.set( xFormsByIndex->getByIndex(0), UNO_QUERY_THROW );
417*cdf0e10cSrcweir 			    }
418*cdf0e10cSrcweir             }
419*cdf0e10cSrcweir             catch( const Exception& )
420*cdf0e10cSrcweir             {
421*cdf0e10cSrcweir             	DBG_UNHANDLED_EXCEPTION();
422*cdf0e10cSrcweir             }
423*cdf0e10cSrcweir 		}
424*cdf0e10cSrcweir 	}
425*cdf0e10cSrcweir 	else
426*cdf0e10cSrcweir     {
427*cdf0e10cSrcweir 		xForm = xCurrentForm;
428*cdf0e10cSrcweir     }
429*cdf0e10cSrcweir 
430*cdf0e10cSrcweir 	// did not find an existing suitable form -> create a new one
431*cdf0e10cSrcweir 	if ( !xForm.is() )
432*cdf0e10cSrcweir 	{
433*cdf0e10cSrcweir 		SdrModel* pModel = m_rPage.GetModel();
434*cdf0e10cSrcweir 
435*cdf0e10cSrcweir 		if( pModel->IsUndoEnabled() )
436*cdf0e10cSrcweir 		{
437*cdf0e10cSrcweir 			XubString aStr(SVX_RES(RID_STR_FORM));
438*cdf0e10cSrcweir 			XubString aUndoStr(SVX_RES(RID_STR_UNDO_CONTAINER_INSERT));
439*cdf0e10cSrcweir 			aUndoStr.SearchAndReplace('#', aStr);
440*cdf0e10cSrcweir 			pModel->BegUndo(aUndoStr);
441*cdf0e10cSrcweir 		}
442*cdf0e10cSrcweir 
443*cdf0e10cSrcweir         try
444*cdf0e10cSrcweir         {
445*cdf0e10cSrcweir 		    xForm.set( ::comphelper::getProcessServiceFactory()->createInstance( FM_SUN_COMPONENT_FORM ), UNO_QUERY );
446*cdf0e10cSrcweir 
447*cdf0e10cSrcweir 		    // a form should always have the command type table as default
448*cdf0e10cSrcweir 		    Reference< XPropertySet > xFormProps( xForm, UNO_QUERY_THROW );
449*cdf0e10cSrcweir 			xFormProps->setPropertyValue( FM_PROP_COMMANDTYPE, makeAny( sal_Int32( CommandType::TABLE ) ) );
450*cdf0e10cSrcweir 
451*cdf0e10cSrcweir             // and the "Standard" name
452*cdf0e10cSrcweir             ::rtl::OUString sName = String( SVX_RES( RID_STR_STDFORMNAME ) );
453*cdf0e10cSrcweir 		    xFormProps->setPropertyValue( FM_PROP_NAME, makeAny( sName ) );
454*cdf0e10cSrcweir 
455*cdf0e10cSrcweir             Reference< XIndexContainer > xContainer( xForms, UNO_QUERY );
456*cdf0e10cSrcweir 			if( pModel->IsUndoEnabled() )
457*cdf0e10cSrcweir 			{
458*cdf0e10cSrcweir 				pModel->AddUndo(new FmUndoContainerAction(*(FmFormModel*)pModel,
459*cdf0e10cSrcweir 														   FmUndoContainerAction::Inserted,
460*cdf0e10cSrcweir 														   xContainer,
461*cdf0e10cSrcweir 														   xForm,
462*cdf0e10cSrcweir 														   xContainer->getCount()));
463*cdf0e10cSrcweir 			}
464*cdf0e10cSrcweir 		    xForms->insertByName( sName, makeAny( xForm ) );
465*cdf0e10cSrcweir 		    xCurrentForm = xForm;
466*cdf0e10cSrcweir         }
467*cdf0e10cSrcweir         catch( const Exception& )
468*cdf0e10cSrcweir         {
469*cdf0e10cSrcweir         	DBG_UNHANDLED_EXCEPTION();
470*cdf0e10cSrcweir             xForm.clear();
471*cdf0e10cSrcweir         }
472*cdf0e10cSrcweir 
473*cdf0e10cSrcweir 		if( pModel->IsUndoEnabled() )
474*cdf0e10cSrcweir 			pModel->EndUndo();
475*cdf0e10cSrcweir 	}
476*cdf0e10cSrcweir 
477*cdf0e10cSrcweir     return xForm;
478*cdf0e10cSrcweir }
479*cdf0e10cSrcweir 
480*cdf0e10cSrcweir //------------------------------------------------------------------------------
481*cdf0e10cSrcweir Reference< ::com::sun::star::form::XForm >  FmFormPageImpl::findPlaceInFormComponentHierarchy(
482*cdf0e10cSrcweir     const Reference< XFormComponent > & rContent, const Reference< XDataSource > & rDatabase,
483*cdf0e10cSrcweir     const ::rtl::OUString& rDBTitle, const ::rtl::OUString& rCursorSource, sal_Int32 nCommandType )
484*cdf0e10cSrcweir {
485*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "svx", "Ocke.Janssen@sun.com", "FmFormPageImpl::findPlaceInFormComponentHierarchy" );
486*cdf0e10cSrcweir     // if the control already is child of a form, don't do anything
487*cdf0e10cSrcweir     if (!rContent.is() || rContent->getParent().is())
488*cdf0e10cSrcweir         return NULL;
489*cdf0e10cSrcweir 
490*cdf0e10cSrcweir     Reference< XForm >  xForm;
491*cdf0e10cSrcweir 
492*cdf0e10cSrcweir     // Wenn Datenbank und CursorSource gesetzt sind, dann wird
493*cdf0e10cSrcweir     // die Form anhand dieser Kriterien gesucht, ansonsten nur aktuelle
494*cdf0e10cSrcweir     // und die StandardForm
495*cdf0e10cSrcweir     if (rDatabase.is() && rCursorSource.getLength())
496*cdf0e10cSrcweir     {
497*cdf0e10cSrcweir         validateCurForm();
498*cdf0e10cSrcweir 
499*cdf0e10cSrcweir         // erst in der aktuellen form suchen
500*cdf0e10cSrcweir         xForm = findFormForDataSource( xCurrentForm, rDatabase, rCursorSource, nCommandType );
501*cdf0e10cSrcweir 
502*cdf0e10cSrcweir         Reference< ::com::sun::star::container::XIndexAccess >  xFormsByIndex( getForms(), UNO_QUERY );
503*cdf0e10cSrcweir         DBG_ASSERT(xFormsByIndex.is(), "FmFormPageImpl::findPlaceInFormComponentHierarchy : no index access for my forms collection !");
504*cdf0e10cSrcweir         sal_Int32 nCount = xFormsByIndex->getCount();
505*cdf0e10cSrcweir         for (sal_Int32 i = 0; !xForm.is() && i < nCount; i++)
506*cdf0e10cSrcweir         {
507*cdf0e10cSrcweir             Reference< ::com::sun::star::form::XForm >  xToSearch;
508*cdf0e10cSrcweir             xFormsByIndex->getByIndex(i) >>= xToSearch;
509*cdf0e10cSrcweir             xForm = findFormForDataSource( xToSearch, rDatabase, rCursorSource, nCommandType );
510*cdf0e10cSrcweir         }
511*cdf0e10cSrcweir 
512*cdf0e10cSrcweir         // wenn keine ::com::sun::star::form gefunden, dann eine neue erzeugen
513*cdf0e10cSrcweir         if (!xForm.is())
514*cdf0e10cSrcweir         {
515*cdf0e10cSrcweir             SdrModel* pModel = m_rPage.GetModel();
516*cdf0e10cSrcweir 
517*cdf0e10cSrcweir 			const bool bUndo = pModel->IsUndoEnabled();
518*cdf0e10cSrcweir 
519*cdf0e10cSrcweir 			if( bUndo )
520*cdf0e10cSrcweir 			{
521*cdf0e10cSrcweir 				XubString aStr(SVX_RES(RID_STR_FORM));
522*cdf0e10cSrcweir 				XubString aUndoStr(SVX_RES(RID_STR_UNDO_CONTAINER_INSERT));
523*cdf0e10cSrcweir 				aUndoStr.SearchAndReplace('#', aStr);
524*cdf0e10cSrcweir 				pModel->BegUndo(aUndoStr);
525*cdf0e10cSrcweir 			}
526*cdf0e10cSrcweir 
527*cdf0e10cSrcweir             xForm = Reference< ::com::sun::star::form::XForm >(::comphelper::getProcessServiceFactory()->createInstance(FM_SUN_COMPONENT_FORM), UNO_QUERY);
528*cdf0e10cSrcweir             // a form should always have the command type table as default
529*cdf0e10cSrcweir             Reference< ::com::sun::star::beans::XPropertySet > xFormProps(xForm, UNO_QUERY);
530*cdf0e10cSrcweir             try { xFormProps->setPropertyValue(FM_PROP_COMMANDTYPE, makeAny(sal_Int32(CommandType::TABLE))); }
531*cdf0e10cSrcweir             catch(Exception&) { }
532*cdf0e10cSrcweir 
533*cdf0e10cSrcweir             if (rDBTitle.getLength())
534*cdf0e10cSrcweir                 xFormProps->setPropertyValue(FM_PROP_DATASOURCE,makeAny(rDBTitle));
535*cdf0e10cSrcweir             else
536*cdf0e10cSrcweir             {
537*cdf0e10cSrcweir                 Reference< ::com::sun::star::beans::XPropertySet >  xDatabaseProps(rDatabase, UNO_QUERY);
538*cdf0e10cSrcweir                 Any aDatabaseUrl = xDatabaseProps->getPropertyValue(FM_PROP_URL);
539*cdf0e10cSrcweir                 xFormProps->setPropertyValue(FM_PROP_DATASOURCE, aDatabaseUrl);
540*cdf0e10cSrcweir             }
541*cdf0e10cSrcweir 
542*cdf0e10cSrcweir             xFormProps->setPropertyValue(FM_PROP_COMMAND,makeAny(rCursorSource));
543*cdf0e10cSrcweir             xFormProps->setPropertyValue(FM_PROP_COMMANDTYPE, makeAny(nCommandType));
544*cdf0e10cSrcweir 
545*cdf0e10cSrcweir             Reference< ::com::sun::star::container::XNameAccess >  xNamedSet( getForms(), UNO_QUERY );
546*cdf0e10cSrcweir 
547*cdf0e10cSrcweir             const bool bTableOrQuery = ( CommandType::TABLE == nCommandType ) || ( CommandType::QUERY == nCommandType );
548*cdf0e10cSrcweir             ::rtl::OUString sName = FormControlFactory::getUniqueName( xNamedSet,
549*cdf0e10cSrcweir                 bTableOrQuery ? rCursorSource : ::rtl::OUString( String( SVX_RES( RID_STR_STDFORMNAME ) ) ) );
550*cdf0e10cSrcweir 
551*cdf0e10cSrcweir             xFormProps->setPropertyValue( FM_PROP_NAME, makeAny( sName ) );
552*cdf0e10cSrcweir 
553*cdf0e10cSrcweir 			if( bUndo )
554*cdf0e10cSrcweir 			{
555*cdf0e10cSrcweir 				Reference< ::com::sun::star::container::XIndexContainer >  xContainer( getForms(), UNO_QUERY );
556*cdf0e10cSrcweir 				pModel->AddUndo(new FmUndoContainerAction(*(FmFormModel*)pModel,
557*cdf0e10cSrcweir 														 FmUndoContainerAction::Inserted,
558*cdf0e10cSrcweir 														 xContainer,
559*cdf0e10cSrcweir 														 xForm,
560*cdf0e10cSrcweir 														 xContainer->getCount()));
561*cdf0e10cSrcweir 			}
562*cdf0e10cSrcweir 
563*cdf0e10cSrcweir             getForms()->insertByName( sName, makeAny( xForm ) );
564*cdf0e10cSrcweir 
565*cdf0e10cSrcweir 			if( bUndo )
566*cdf0e10cSrcweir 				pModel->EndUndo();
567*cdf0e10cSrcweir         }
568*cdf0e10cSrcweir         xCurrentForm = xForm;
569*cdf0e10cSrcweir     }
570*cdf0e10cSrcweir 
571*cdf0e10cSrcweir     xForm = getDefaultForm();
572*cdf0e10cSrcweir     return xForm;
573*cdf0e10cSrcweir }
574*cdf0e10cSrcweir 
575*cdf0e10cSrcweir //------------------------------------------------------------------------------
576*cdf0e10cSrcweir Reference< XForm >  FmFormPageImpl::findFormForDataSource(
577*cdf0e10cSrcweir 		const Reference< XForm > & rForm, const Reference< XDataSource > & _rxDatabase,
578*cdf0e10cSrcweir 		const ::rtl::OUString& _rCursorSource, sal_Int32 nCommandType)
579*cdf0e10cSrcweir {
580*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "svx", "Ocke.Janssen@sun.com", "FmFormPageImpl::findFormForDataSource" );
581*cdf0e10cSrcweir     Reference< XForm >			xResultForm;
582*cdf0e10cSrcweir     Reference< XRowSet >		xDBForm(rForm, UNO_QUERY);
583*cdf0e10cSrcweir     Reference< XPropertySet >	xFormProps(rForm, UNO_QUERY);
584*cdf0e10cSrcweir     if (!xDBForm.is() || !xFormProps.is())
585*cdf0e10cSrcweir         return xResultForm;
586*cdf0e10cSrcweir 
587*cdf0e10cSrcweir 	OSL_ENSURE(_rxDatabase.is(), "FmFormPageImpl::findFormForDataSource: invalid data source!");
588*cdf0e10cSrcweir 	::rtl::OUString sLookupName;			// the name of the data source we're looking for
589*cdf0e10cSrcweir 	::rtl::OUString sFormDataSourceName;	// the name of the data source the current connection in the form is based on
590*cdf0e10cSrcweir 	try
591*cdf0e10cSrcweir 	{
592*cdf0e10cSrcweir 		Reference< XPropertySet > xDSProps(_rxDatabase, UNO_QUERY);
593*cdf0e10cSrcweir 		if (xDSProps.is())
594*cdf0e10cSrcweir 			xDSProps->getPropertyValue(FM_PROP_NAME) >>= sLookupName;
595*cdf0e10cSrcweir 
596*cdf0e10cSrcweir 		xFormProps->getPropertyValue(FM_PROP_DATASOURCE) >>= sFormDataSourceName;
597*cdf0e10cSrcweir         // if there's no DataSourceName set at the form, check whether we can deduce one from its
598*cdf0e10cSrcweir         // ActiveConnection
599*cdf0e10cSrcweir 		if (0 == sFormDataSourceName.getLength())
600*cdf0e10cSrcweir 		{
601*cdf0e10cSrcweir             Reference< XConnection > xFormConnection;
602*cdf0e10cSrcweir             xFormProps->getPropertyValue( FM_PROP_ACTIVE_CONNECTION ) >>= xFormConnection;
603*cdf0e10cSrcweir             if ( !xFormConnection.is() )
604*cdf0e10cSrcweir                 OStaticDataAccessTools().isEmbeddedInDatabase( xFormProps, xFormConnection );
605*cdf0e10cSrcweir 			if (xFormConnection.is())
606*cdf0e10cSrcweir 			{
607*cdf0e10cSrcweir 				Reference< XChild > xConnAsChild(xFormConnection, UNO_QUERY);
608*cdf0e10cSrcweir 				if (xConnAsChild.is())
609*cdf0e10cSrcweir 				{
610*cdf0e10cSrcweir 					Reference< XDataSource > xFormDS(xConnAsChild->getParent(), UNO_QUERY);
611*cdf0e10cSrcweir 					if (xFormDS.is())
612*cdf0e10cSrcweir 					{
613*cdf0e10cSrcweir 						xDSProps = xDSProps.query(xFormDS);
614*cdf0e10cSrcweir 						if (xDSProps.is())
615*cdf0e10cSrcweir 							xDSProps->getPropertyValue(FM_PROP_NAME) >>= sFormDataSourceName;
616*cdf0e10cSrcweir 					}
617*cdf0e10cSrcweir 				}
618*cdf0e10cSrcweir 			}
619*cdf0e10cSrcweir 		}
620*cdf0e10cSrcweir 	}
621*cdf0e10cSrcweir 	catch(const Exception& e)
622*cdf0e10cSrcweir 	{
623*cdf0e10cSrcweir 		(void)e;
624*cdf0e10cSrcweir 		OSL_ENSURE(sal_False, "FmFormPageImpl::findFormForDataSource: caught an exception!");
625*cdf0e10cSrcweir 	}
626*cdf0e10cSrcweir 
627*cdf0e10cSrcweir 	if (sLookupName == sFormDataSourceName)
628*cdf0e10cSrcweir     {
629*cdf0e10cSrcweir         // jetzt noch ueberpruefen ob CursorSource und Type uebereinstimmen
630*cdf0e10cSrcweir         ::rtl::OUString aCursorSource = ::comphelper::getString(xFormProps->getPropertyValue(FM_PROP_COMMAND));
631*cdf0e10cSrcweir         sal_Int32 nType = ::comphelper::getINT32(xFormProps->getPropertyValue(FM_PROP_COMMANDTYPE));
632*cdf0e10cSrcweir         if (!aCursorSource.getLength() || ((nType == nCommandType) && (aCursorSource == _rCursorSource))) // found the form
633*cdf0e10cSrcweir         {
634*cdf0e10cSrcweir             xResultForm = rForm;
635*cdf0e10cSrcweir             // Ist noch keine Datenquelle gesetzt, wird dieses hier nachgeholt
636*cdf0e10cSrcweir             if (!aCursorSource.getLength())
637*cdf0e10cSrcweir             {
638*cdf0e10cSrcweir                 xFormProps->setPropertyValue(FM_PROP_COMMAND, makeAny(_rCursorSource));
639*cdf0e10cSrcweir                 xFormProps->setPropertyValue(FM_PROP_COMMANDTYPE, makeAny((sal_Int32)nCommandType));
640*cdf0e10cSrcweir             }
641*cdf0e10cSrcweir         }
642*cdf0e10cSrcweir     }
643*cdf0e10cSrcweir 
644*cdf0e10cSrcweir 	// as long as xResultForm is NULL, search the child forms of rForm
645*cdf0e10cSrcweir     Reference< XIndexAccess >  xComponents(rForm, UNO_QUERY);
646*cdf0e10cSrcweir     sal_Int32 nCount = xComponents->getCount();
647*cdf0e10cSrcweir     for (sal_Int32 i = 0; !xResultForm.is() && i < nCount; ++i)
648*cdf0e10cSrcweir     {
649*cdf0e10cSrcweir         Reference< ::com::sun::star::form::XForm >  xSearchForm;
650*cdf0e10cSrcweir         xComponents->getByIndex(i) >>= xSearchForm;
651*cdf0e10cSrcweir         // continue searching in the sub form
652*cdf0e10cSrcweir         if (xSearchForm.is())
653*cdf0e10cSrcweir             xResultForm = findFormForDataSource( xSearchForm, _rxDatabase, _rCursorSource, nCommandType );
654*cdf0e10cSrcweir     }
655*cdf0e10cSrcweir     return xResultForm;
656*cdf0e10cSrcweir }
657*cdf0e10cSrcweir 
658*cdf0e10cSrcweir //------------------------------------------------------------------------------
659*cdf0e10cSrcweir ::rtl::OUString FmFormPageImpl::setUniqueName(const Reference< XFormComponent > & xFormComponent, const Reference< XForm > & xControls)
660*cdf0e10cSrcweir {
661*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
662*cdf0e10cSrcweir     try
663*cdf0e10cSrcweir     {
664*cdf0e10cSrcweir         Reference< XChild > xChild( xFormComponent, UNO_QUERY_THROW );
665*cdf0e10cSrcweir         OSL_ENSURE( !xChild->getParent().is(), "FmFormPageImpl::setUniqueName: to be called before insertion!" );
666*cdf0e10cSrcweir     }
667*cdf0e10cSrcweir     catch( const Exception& )
668*cdf0e10cSrcweir     {
669*cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
670*cdf0e10cSrcweir     }
671*cdf0e10cSrcweir #endif
672*cdf0e10cSrcweir     ::rtl::OUString sName;
673*cdf0e10cSrcweir     Reference< ::com::sun::star::beans::XPropertySet >  xSet(xFormComponent, UNO_QUERY);
674*cdf0e10cSrcweir     if (xSet.is())
675*cdf0e10cSrcweir     {
676*cdf0e10cSrcweir         sName = ::comphelper::getString( xSet->getPropertyValue( FM_PROP_NAME ) );
677*cdf0e10cSrcweir         Reference< ::com::sun::star::container::XNameAccess >  xNameAcc(xControls, UNO_QUERY);
678*cdf0e10cSrcweir 
679*cdf0e10cSrcweir         if (!sName.getLength() || xNameAcc->hasByName(sName))
680*cdf0e10cSrcweir         {
681*cdf0e10cSrcweir             // setzen eines default Namens ueber die ClassId
682*cdf0e10cSrcweir             sal_Int16 nClassId( FormComponentType::CONTROL );
683*cdf0e10cSrcweir             xSet->getPropertyValue( FM_PROP_CLASSID ) >>= nClassId;
684*cdf0e10cSrcweir 
685*cdf0e10cSrcweir             ::rtl::OUString sDefaultName = FormControlFactory::getDefaultUniqueName_ByComponentType(
686*cdf0e10cSrcweir                 Reference< XNameAccess >( xControls, UNO_QUERY ), xSet );
687*cdf0e10cSrcweir 
688*cdf0e10cSrcweir             // bei Radiobuttons, die einen Namen haben, diesen nicht ueberschreiben!
689*cdf0e10cSrcweir             if (!sName.getLength() || nClassId != ::com::sun::star::form::FormComponentType::RADIOBUTTON)
690*cdf0e10cSrcweir             {
691*cdf0e10cSrcweir                 xSet->setPropertyValue(FM_PROP_NAME, makeAny(sDefaultName));
692*cdf0e10cSrcweir             }
693*cdf0e10cSrcweir 
694*cdf0e10cSrcweir             sName = sDefaultName;
695*cdf0e10cSrcweir         }
696*cdf0e10cSrcweir     }
697*cdf0e10cSrcweir     return sName;
698*cdf0e10cSrcweir }
699*cdf0e10cSrcweir 
700*cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
701*cdf0e10cSrcweir void FmFormPageImpl::formModelAssigned( const FmFormObj& _object )
702*cdf0e10cSrcweir {
703*cdf0e10cSrcweir     Reference< XMap > xControlShapeMap( m_aControlShapeMap.get(), UNO_QUERY );
704*cdf0e10cSrcweir     if ( !xControlShapeMap.is() )
705*cdf0e10cSrcweir         // our map does not exist -> not interested in this event
706*cdf0e10cSrcweir         return;
707*cdf0e10cSrcweir 
708*cdf0e10cSrcweir     try
709*cdf0e10cSrcweir     {
710*cdf0e10cSrcweir         lcl_removeFormObject_throw( _object,  xControlShapeMap, false );
711*cdf0e10cSrcweir         lcl_insertFormObject_throw( _object,  xControlShapeMap );
712*cdf0e10cSrcweir     }
713*cdf0e10cSrcweir     catch( const Exception& )
714*cdf0e10cSrcweir     {
715*cdf0e10cSrcweir     	DBG_UNHANDLED_EXCEPTION();
716*cdf0e10cSrcweir     }
717*cdf0e10cSrcweir }
718*cdf0e10cSrcweir 
719*cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
720*cdf0e10cSrcweir void FmFormPageImpl::formObjectInserted( const FmFormObj& _object )
721*cdf0e10cSrcweir {
722*cdf0e10cSrcweir     Reference< XMap > xControlShapeMap( m_aControlShapeMap.get(), UNO_QUERY );
723*cdf0e10cSrcweir     if ( !xControlShapeMap.is() )
724*cdf0e10cSrcweir         // our map does not exist -> not interested in this event
725*cdf0e10cSrcweir         return;
726*cdf0e10cSrcweir 
727*cdf0e10cSrcweir     try
728*cdf0e10cSrcweir     {
729*cdf0e10cSrcweir         lcl_insertFormObject_throw( _object,  xControlShapeMap );
730*cdf0e10cSrcweir     }
731*cdf0e10cSrcweir     catch( const Exception& )
732*cdf0e10cSrcweir     {
733*cdf0e10cSrcweir     	DBG_UNHANDLED_EXCEPTION();
734*cdf0e10cSrcweir     }
735*cdf0e10cSrcweir }
736*cdf0e10cSrcweir 
737*cdf0e10cSrcweir //----------------------------------------------------------------------------------------------------------------------
738*cdf0e10cSrcweir void FmFormPageImpl::formObjectRemoved( const FmFormObj& _object )
739*cdf0e10cSrcweir {
740*cdf0e10cSrcweir     Reference< XMap > xControlShapeMap( m_aControlShapeMap.get(), UNO_QUERY );
741*cdf0e10cSrcweir     if ( !xControlShapeMap.is() )
742*cdf0e10cSrcweir         // our map does not exist -> not interested in this event
743*cdf0e10cSrcweir         return;
744*cdf0e10cSrcweir 
745*cdf0e10cSrcweir     try
746*cdf0e10cSrcweir     {
747*cdf0e10cSrcweir         lcl_removeFormObject_throw( _object, xControlShapeMap );
748*cdf0e10cSrcweir     }
749*cdf0e10cSrcweir     catch( const Exception& )
750*cdf0e10cSrcweir     {
751*cdf0e10cSrcweir     	DBG_UNHANDLED_EXCEPTION();
752*cdf0e10cSrcweir     }
753*cdf0e10cSrcweir }
754