xref: /trunk/main/svx/source/form/fmvwimp.cxx (revision 86e1cf34)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svx.hxx"
26 
27 #include "fmdocumentclassification.hxx"
28 #include "fmobj.hxx"
29 #include "fmpgeimp.hxx"
30 #include "fmprop.hrc"
31 #include "svx/fmresids.hrc"
32 #include "fmservs.hxx"
33 #include "fmshimp.hxx"
34 #include "svx/fmtools.hxx"
35 #include "fmundo.hxx"
36 #include "fmvwimp.hxx"
37 #include "formcontrolfactory.hxx"
38 #include "svx/sdrpaintwindow.hxx"
39 #include "svx/svditer.hxx"
40 #include "svx/dataaccessdescriptor.hxx"
41 #include "svx/dialmgr.hxx"
42 #include "svx/fmglob.hxx"
43 #include "svx/fmmodel.hxx"
44 #include "svx/fmpage.hxx"
45 #include "svx/fmshell.hxx"
46 #include "svx/fmview.hxx"
47 #include "svx/sdrpagewindow.hxx"
48 #include "svx/svdogrp.hxx"
49 #include "svx/svdpagv.hxx"
50 #include "svx/xmlexchg.hxx"
51 
52 /** === begin UNO includes === **/
53 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
54 #include <com/sun/star/style/VerticalAlignment.hpp>
55 #include <com/sun/star/lang/XInitialization.hpp>
56 #include <com/sun/star/sdbc/XRowSet.hpp>
57 #include <com/sun/star/form/XLoadable.hpp>
58 #include <com/sun/star/awt/VisualEffect.hpp>
59 #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
60 #include <com/sun/star/util/XNumberFormats.hpp>
61 #include <com/sun/star/sdb/CommandType.hpp>
62 #include <com/sun/star/sdbc/DataType.hpp>
63 #include <com/sun/star/sdbc/ColumnValue.hpp>
64 #include <com/sun/star/form/FormComponentType.hpp>
65 #include <com/sun/star/form/FormButtonType.hpp>
66 #include <com/sun/star/form/XReset.hpp>
67 #include <com/sun/star/form/binding/XBindableValue.hpp>
68 #include <com/sun/star/form/binding/XValueBinding.hpp>
69 #include <com/sun/star/form/submission/XSubmissionSupplier.hpp>
70 #include <com/sun/star/awt/XTabControllerModel.hpp>
71 #include <com/sun/star/awt/XControlContainer.hpp>
72 #include <com/sun/star/awt/XTabController.hpp>
73 #include <com/sun/star/container/XIndexAccess.hpp>
74 #include <com/sun/star/awt/XControl.hpp>
75 #include <com/sun/star/lang/XUnoTunnel.hpp>
76 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
77 #include <com/sun/star/sdbc/XPreparedStatement.hpp>
78 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
79 #include <com/sun/star/container/XContainer.hpp>
80 /** === end UNO includes === **/
81 
82 #include <comphelper/enumhelper.hxx>
83 #include <comphelper/extract.hxx>
84 #include <comphelper/namedvaluecollection.hxx>
85 #include <comphelper/numbers.hxx>
86 #include <comphelper/property.hxx>
87 #include <cppuhelper/exc_hlp.hxx>
88 #include <unotools/moduleoptions.hxx>
89 #include <tools/diagnose_ex.h>
90 #include <vcl/msgbox.hxx>
91 #include <vcl/stdtext.hxx>
92 #include <vos/mutex.hxx>
93 #include <rtl/logfile.hxx>
94 
95 #include <algorithm>
96 
97 using namespace ::comphelper;
98 using namespace ::svx;
99 using namespace ::svxform;
100 
101     using namespace ::com::sun::star;
102     /** === begin UNO using === **/
103     using ::com::sun::star::uno::Exception;
104     using ::com::sun::star::uno::RuntimeException;
105     using ::com::sun::star::uno::XInterface;
106     using ::com::sun::star::uno::Sequence;
107     using ::com::sun::star::uno::UNO_QUERY;
108     using ::com::sun::star::uno::UNO_QUERY_THROW;
109     using ::com::sun::star::uno::UNO_SET_THROW;
110     using ::com::sun::star::uno::Type;
111     using ::com::sun::star::uno::Reference;
112     using ::com::sun::star::uno::Any;
113     using ::com::sun::star::uno::makeAny;
114     using ::com::sun::star::style::VerticalAlignment_MIDDLE;
115     using ::com::sun::star::form::FormButtonType_SUBMIT;
116     using ::com::sun::star::form::binding::XValueBinding;
117     using ::com::sun::star::form::binding::XBindableValue;
118     using ::com::sun::star::lang::XComponent;
119     using ::com::sun::star::container::XIndexAccess;
120     using ::com::sun::star::form::XForm;
121     using ::com::sun::star::form::runtime::XFormController;
122     using ::com::sun::star::script::XEventAttacherManager;
123     using ::com::sun::star::awt::XTabControllerModel;
124     using ::com::sun::star::container::XChild;
125     using ::com::sun::star::container::XEnumeration;
126     using ::com::sun::star::task::XInteractionHandler;
127     using ::com::sun::star::lang::XInitialization;
128     using ::com::sun::star::awt::XTabController;
129     using ::com::sun::star::lang::XUnoTunnel;
130     using ::com::sun::star::awt::XControlContainer;
131     using ::com::sun::star::awt::XControl;
132     using ::com::sun::star::form::XFormComponent;
133     using ::com::sun::star::form::XForm;
134     using ::com::sun::star::lang::IndexOutOfBoundsException;
135     using ::com::sun::star::lang::WrappedTargetException;
136     using ::com::sun::star::container::XContainer;
137     using ::com::sun::star::container::ContainerEvent;
138     using ::com::sun::star::lang::EventObject;
139     using ::com::sun::star::beans::NamedValue;
140     using ::com::sun::star::sdb::SQLErrorEvent;
141     using ::com::sun::star::sdbc::XRowSet;
142     using ::com::sun::star::beans::XPropertySet;
143     using ::com::sun::star::container::XElementAccess;
144     using ::com::sun::star::awt::XWindow;
145     using ::com::sun::star::awt::FocusEvent;
146     using ::com::sun::star::ui::dialogs::XExecutableDialog;
147     using ::com::sun::star::sdbc::XDataSource;
148     using ::com::sun::star::container::XIndexContainer;
149     using ::com::sun::star::sdbc::XConnection;
150     using ::com::sun::star::container::XNameAccess;
151     using ::com::sun::star::sdb::SQLContext;
152     using ::com::sun::star::sdbc::SQLWarning;
153     using ::com::sun::star::sdbc::SQLException;
154     using ::com::sun::star::util::XNumberFormatsSupplier;
155     using ::com::sun::star::util::XNumberFormats;
156     using ::com::sun::star::beans::XPropertySetInfo;
157     /** === end UNO using === **/
158     namespace FormComponentType = ::com::sun::star::form::FormComponentType;
159     namespace CommandType = ::com::sun::star::sdb::CommandType;
160     namespace DataType = ::com::sun::star::sdbc::DataType;
161 
162 //------------------------------------------------------------------------------
163 class FmXFormView::ObjectRemoveListener : public SfxListener
164 {
165 	FmXFormView* m_pParent;
166 public:
167 	ObjectRemoveListener( FmXFormView* pParent );
168 	virtual void Notify( SfxBroadcaster& rBC, const SfxHint& rHint );
169 };
170 
171 //========================================================================
DBG_NAME(FormViewPageWindowAdapter)172 DBG_NAME(FormViewPageWindowAdapter)
173 //------------------------------------------------------------------------
174 FormViewPageWindowAdapter::FormViewPageWindowAdapter( const ::comphelper::ComponentContext& _rContext, const SdrPageWindow& _rWindow, FmXFormView* _pViewImpl )
175 :	m_xControlContainer( _rWindow.GetControlContainer() ),
176 	m_aContext( _rContext ),
177 	m_pViewImpl( _pViewImpl ),
178     m_pWindow( dynamic_cast< Window* >( &_rWindow.GetPaintWindow().GetOutputDevice() ) )
179 {
180 	DBG_CTOR(FormViewPageWindowAdapter,NULL);
181 
182     // create an XFormController for every form
183     FmFormPage* pFormPage = dynamic_cast< FmFormPage* >( _rWindow.GetPageView().GetPage() );
184     DBG_ASSERT( pFormPage, "FormViewPageWindowAdapter::FormViewPageWindowAdapter: no FmFormPage found!" );
185     if ( pFormPage )
186 	{
187         try
188         {
189             Reference< XIndexAccess > xForms( pFormPage->GetForms(), UNO_QUERY_THROW );
190 		    sal_uInt32 nLength = xForms->getCount();
191 		    for (sal_uInt32 i = 0; i < nLength; i++)
192 		    {
193                 Reference< XForm > xForm( xForms->getByIndex(i), UNO_QUERY );
194                 if ( xForm.is() )
195 			        setController( xForm, NULL );
196 		    }
197         }
198         catch( const Exception& )
199         {
200         	DBG_UNHANDLED_EXCEPTION();
201         }
202 	}
203 }
204 // -----------------------------------------------------------------------------
~FormViewPageWindowAdapter()205 FormViewPageWindowAdapter::~FormViewPageWindowAdapter()
206 {
207 	DBG_DTOR(FormViewPageWindowAdapter,NULL);
208 }
209 
210 //------------------------------------------------------------------
dispose()211 void FormViewPageWindowAdapter::dispose()
212 {
213     for (   ::std::vector< Reference< XFormController > >::const_iterator i = m_aControllerList.begin();
214             i != m_aControllerList.end();
215             ++i
216         )
217     {
218         try
219         {
220             Reference< XFormController > xController( *i, UNO_QUERY_THROW );
221 
222             // detaching the events
223             Reference< XChild > xControllerModel( xController->getModel(), UNO_QUERY );
224             if ( xControllerModel.is() )
225             {
226                 Reference< XEventAttacherManager >  xEventManager( xControllerModel->getParent(), UNO_QUERY_THROW );
227                 Reference< XInterface > xControllerNormalized( xController, UNO_QUERY_THROW );
228                 xEventManager->detach( i - m_aControllerList.begin(), xControllerNormalized );
229             }
230 
231             // dispose the formcontroller
232             Reference< XComponent > xComp( xController, UNO_QUERY_THROW );
233             xComp->dispose();
234         }
235         catch( const Exception& )
236         {
237             DBG_UNHANDLED_EXCEPTION();
238         }
239     }
240 
241     m_aControllerList.clear();
242 }
243 
244 
245 //------------------------------------------------------------------------------
hasElements(void)246 sal_Bool SAL_CALL FormViewPageWindowAdapter::hasElements(void) throw( RuntimeException )
247 {
248 	return getCount() != 0;
249 }
250 
251 //------------------------------------------------------------------------------
getElementType(void)252 Type SAL_CALL  FormViewPageWindowAdapter::getElementType(void) throw( RuntimeException )
253 {
254 	return ::getCppuType((const Reference< XFormController>*)0);
255 }
256 
257 // XEnumerationAccess
258 //------------------------------------------------------------------------------
createEnumeration(void)259 Reference< XEnumeration >  SAL_CALL FormViewPageWindowAdapter::createEnumeration(void) throw( RuntimeException )
260 {
261 	return new ::comphelper::OEnumerationByIndex(this);
262 }
263 
264 // XIndexAccess
265 //------------------------------------------------------------------------------
getCount(void)266 sal_Int32 SAL_CALL FormViewPageWindowAdapter::getCount(void) throw( RuntimeException )
267 {
268 	return m_aControllerList.size();
269 }
270 
271 //------------------------------------------------------------------------------
getByIndex(sal_Int32 nIndex)272 Any SAL_CALL FormViewPageWindowAdapter::getByIndex(sal_Int32 nIndex) throw( IndexOutOfBoundsException, WrappedTargetException, RuntimeException )
273 {
274 	if (nIndex < 0 ||
275 		nIndex >= getCount())
276 		throw IndexOutOfBoundsException();
277 
278 	Any aElement;
279 	aElement <<= m_aControllerList[nIndex];
280 	return aElement;
281 }
282 
283 //------------------------------------------------------------------------
makeVisible(const Reference<XControl> & _Control)284 void SAL_CALL FormViewPageWindowAdapter::makeVisible( const Reference< XControl >& _Control ) throw (RuntimeException)
285 {
286 	::vos::OGuard aSolarGuard(Application::GetSolarMutex());
287 
288     Reference< XWindow >  xWindow( _Control, UNO_QUERY );
289 	if ( xWindow.is() && m_pViewImpl->getView() && m_pWindow )
290 	{
291 		awt::Rectangle aRect = xWindow->getPosSize();
292 		::Rectangle aNewRect( aRect.X, aRect.Y, aRect.X + aRect.Width, aRect.Y + aRect.Height );
293 		aNewRect = m_pWindow->PixelToLogic( aNewRect );
294 		m_pViewImpl->getView()->MakeVisible( aNewRect, *m_pWindow );
295 	}
296 }
297 
298 //------------------------------------------------------------------------
getControllerSearchChilds(const Reference<XIndexAccess> & xIndex,const Reference<XTabControllerModel> & xModel)299 Reference< XFormController >  getControllerSearchChilds( const Reference< XIndexAccess > & xIndex, const Reference< XTabControllerModel > & xModel)
300 {
301 	if (xIndex.is() && xIndex->getCount())
302 	{
303 		Reference< XFormController >  xController;
304 
305 		for (sal_Int32 n = xIndex->getCount(); n-- && !xController.is(); )
306 		{
307 			xIndex->getByIndex(n) >>= xController;
308 			if ((XTabControllerModel*)xModel.get() == (XTabControllerModel*)xController->getModel().get())
309 				return xController;
310 			else
311 			{
312 				xController = getControllerSearchChilds(Reference< XIndexAccess > (xController, UNO_QUERY), xModel);
313 				if ( xController.is() )
314 					return xController;
315 			}
316 		}
317 	}
318 	return Reference< XFormController > ();
319 }
320 
321 // Search the according controller
322 //------------------------------------------------------------------------
getController(const Reference<XForm> & xForm) const323 Reference< XFormController >  FormViewPageWindowAdapter::getController( const Reference< XForm > & xForm ) const
324 {
325 	Reference< XTabControllerModel >  xModel(xForm, UNO_QUERY);
326 	for (::std::vector< Reference< XFormController > >::const_iterator i = m_aControllerList.begin();
327 		 i != m_aControllerList.end(); i++)
328 	{
329 		if ((XTabControllerModel*)(*i)->getModel().get() == (XTabControllerModel*)xModel.get())
330 			return *i;
331 
332 		// the current-round controller isn't the right one. perhaps one of it's children ?
333 		Reference< XFormController >  xChildSearch = getControllerSearchChilds(Reference< XIndexAccess > (*i, UNO_QUERY), xModel);
334 		if (xChildSearch.is())
335 			return xChildSearch;
336 	}
337 	return Reference< XFormController > ();
338 }
339 
340 //------------------------------------------------------------------------
setController(const Reference<XForm> & xForm,const Reference<XFormController> & _rxParentController)341 void FormViewPageWindowAdapter::setController(const Reference< XForm > & xForm, const Reference< XFormController >& _rxParentController )
342 {
343     DBG_ASSERT( xForm.is(), "FormViewPageWindowAdapter::setController: there should be a form!" );
344 	Reference< XIndexAccess >  xFormCps(xForm, UNO_QUERY);
345 	if (!xFormCps.is())
346 		return;
347 
348 	Reference< XTabControllerModel >  xTabOrder(xForm, UNO_QUERY);
349 
350 	// create a form controller
351     Reference< XFormController > xController( m_aContext.createComponent( FM_FORM_CONTROLLER ), UNO_QUERY );
352     if ( !xController.is() )
353     {
354         ShowServiceNotAvailableError( m_pWindow, FM_FORM_CONTROLLER, sal_True );
355         return;
356     }
357 
358     Reference< XInteractionHandler > xHandler;
359     if ( _rxParentController.is() )
360         xHandler = _rxParentController->getInteractionHandler();
361     else
362     {
363         // TODO: should we create a default handler? Not really necessary, since the
364         // FormController itself has a default fallback
365     }
366     if ( xHandler.is() )
367         xController->setInteractionHandler( xHandler );
368 
369     xController->setContext( this );
370 
371 	xController->setModel( xTabOrder );
372 	xController->setContainer( m_xControlContainer );
373 	xController->activateTabOrder();
374 	xController->addActivateListener( m_pViewImpl );
375 
376 	if ( _rxParentController.is() )
377 		_rxParentController->addChildController( xController );
378 	else
379 	{
380 		m_aControllerList.push_back(xController);
381 
382 		xController->setParent( *this );
383 
384 		// attaching the events
385 		Reference< XEventAttacherManager > xEventManager( xForm->getParent(), UNO_QUERY );
386 		Reference< XInterface >  xIfc(xController, UNO_QUERY);
387 		xEventManager->attach(m_aControllerList.size() - 1, xIfc, makeAny(xController) );
388 	}
389 
390 	// jetzt die Subforms durchgehen
391 	sal_uInt32 nLength = xFormCps->getCount();
392 	Reference< XForm >  xSubForm;
393 	for (sal_uInt32 i = 0; i < nLength; i++)
394 	{
395 		if ( xFormCps->getByIndex(i) >>= xSubForm )
396 			setController( xSubForm, xController );
397 	}
398 }
399 
400 //------------------------------------------------------------------------
updateTabOrder(const Reference<XForm> & _rxForm)401 void FormViewPageWindowAdapter::updateTabOrder( const Reference< XForm >& _rxForm )
402 {
403     OSL_PRECOND( _rxForm.is(), "FormViewPageWindowAdapter::updateTabOrder: illegal argument!" );
404     if ( !_rxForm.is() )
405         return;
406 
407     try
408     {
409         Reference< XTabController > xTabCtrl( getController( _rxForm ).get() );
410         if ( xTabCtrl.is() )
411         {   // if there already is a TabController for this form, then delegate the "updateTabOrder" request
412             xTabCtrl->activateTabOrder();
413         }
414         else
415         {   // otherwise, create a TabController
416 
417             // if it's a sub form, then we must ensure there exist TabControllers
418             // for all its ancestors, too
419             Reference< XForm > xParentForm( _rxForm->getParent(), UNO_QUERY );
420             // there is a parent form -> look for the respective controller
421             Reference< XFormController > xParentController;
422             if ( xParentForm.is() )
423                 xParentController.set( getController( xParentForm ), UNO_QUERY );
424 
425             setController( _rxForm, xParentController );
426         }
427     }
428     catch( const Exception& )
429     {
430     	DBG_UNHANDLED_EXCEPTION();
431     }
432 }
433 
434 //------------------------------------------------------------------------
FmXFormView(const::comphelper::ComponentContext & _rContext,FmFormView * _pView)435 FmXFormView::FmXFormView(const ::comphelper::ComponentContext& _rContext, FmFormView* _pView )
436     :m_aContext( _rContext )
437 	,m_pMarkedGrid(NULL)
438 	,m_pView(_pView)
439 	,m_nActivationEvent(0)
440 	,m_nErrorMessageEvent( 0 )
441 	,m_nAutoFocusEvent( 0 )
442     ,m_nControlWizardEvent( 0 )
443 	,m_pWatchStoredList( NULL )
444 	,m_bFirstActivation( true )
445     ,m_isTabOrderUpdateSuspended( false )
446 {
447 }
448 
449 //------------------------------------------------------------------------
cancelEvents()450 void FmXFormView::cancelEvents()
451 {
452 	if ( m_nActivationEvent )
453 	{
454 		Application::RemoveUserEvent( m_nActivationEvent );
455 		m_nActivationEvent = 0;
456 	}
457 
458 	if ( m_nErrorMessageEvent )
459 	{
460 		Application::RemoveUserEvent( m_nErrorMessageEvent );
461 		m_nErrorMessageEvent = 0;
462 	}
463 
464 	if ( m_nAutoFocusEvent )
465 	{
466         Application::RemoveUserEvent( m_nAutoFocusEvent );
467 		m_nAutoFocusEvent = 0;
468 	}
469 
470     if ( m_nControlWizardEvent )
471 	{
472         Application::RemoveUserEvent( m_nControlWizardEvent );
473 		m_nControlWizardEvent = 0;
474 	}
475 }
476 
477 //------------------------------------------------------------------------
notifyViewDying()478 void FmXFormView::notifyViewDying( )
479 {
480 	DBG_ASSERT( m_pView, "FmXFormView::notifyViewDying: my view already died!" );
481 	m_pView = NULL;
482 	cancelEvents();
483 }
484 
485 //------------------------------------------------------------------------
~FmXFormView()486 FmXFormView::~FmXFormView()
487 {
488     DBG_ASSERT( m_aPageWindowAdapters.empty(), "FmXFormView::~FmXFormView: Window list not empty!" );
489     if ( !m_aPageWindowAdapters.empty() )
490     {
491         for (   PageWindowAdapterList::const_iterator loop = m_aPageWindowAdapters.begin();
492                 loop != m_aPageWindowAdapters.end();
493                 ++loop
494             )
495         {
496             (*loop)->dispose();
497         }
498     }
499 
500 	cancelEvents();
501 
502 	delete m_pWatchStoredList;
503 	m_pWatchStoredList = NULL;
504 }
505 
506 //      EventListener
507 //------------------------------------------------------------------------------
disposing(const EventObject & Source)508 void SAL_CALL FmXFormView::disposing(const EventObject& Source) throw( RuntimeException )
509 {
510 	if ( m_xWindow.is() && Source.Source == m_xWindow )
511 		removeGridWindowListening();
512 }
513 
514 // XFormControllerListener
515 //------------------------------------------------------------------------------
formActivated(const EventObject & rEvent)516 void SAL_CALL FmXFormView::formActivated(const EventObject& rEvent) throw( RuntimeException )
517 {
518 	if ( m_pView && m_pView->GetFormShell() && m_pView->GetFormShell()->GetImpl() )
519 		m_pView->GetFormShell()->GetImpl()->formActivated( rEvent );
520 }
521 
522 //------------------------------------------------------------------------------
formDeactivated(const EventObject & rEvent)523 void SAL_CALL FmXFormView::formDeactivated(const EventObject& rEvent) throw( RuntimeException )
524 {
525 	if ( m_pView && m_pView->GetFormShell() && m_pView->GetFormShell()->GetImpl() )
526 		m_pView->GetFormShell()->GetImpl()->formDeactivated( rEvent );
527 }
528 
529 // XContainerListener
530 //------------------------------------------------------------------------------
elementInserted(const ContainerEvent & evt)531 void SAL_CALL FmXFormView::elementInserted(const ContainerEvent& evt) throw( RuntimeException )
532 {
533     try
534     {
535 	    Reference< XControlContainer > xControlContainer( evt.Source, UNO_QUERY_THROW );
536 		Reference< XControl > xControl( evt.Element, UNO_QUERY_THROW );
537         Reference< XFormComponent > xControlModel( xControl->getModel(), UNO_QUERY_THROW );
538         Reference< XForm > xForm( xControlModel->getParent(), UNO_QUERY_THROW );
539 
540         if ( m_isTabOrderUpdateSuspended )
541         {
542             // remember the container and the control, so we can update the tab order on resumeTabOrderUpdate
543             m_aNeedTabOrderUpdate[ xControlContainer ].insert( xForm );
544         }
545         else
546         {
547             PFormViewPageWindowAdapter pAdapter = findWindow( xControlContainer );
548 		    if ( pAdapter.is() )
549 			    pAdapter->updateTabOrder( xForm );
550         }
551     }
552     catch( const Exception& )
553     {
554     	DBG_UNHANDLED_EXCEPTION();
555     }
556 }
557 
558 //------------------------------------------------------------------------------
elementReplaced(const ContainerEvent & evt)559 void SAL_CALL FmXFormView::elementReplaced(const ContainerEvent& evt) throw( RuntimeException )
560 {
561 	elementInserted(evt);
562 }
563 
564 //------------------------------------------------------------------------------
elementRemoved(const ContainerEvent &)565 void SAL_CALL FmXFormView::elementRemoved(const ContainerEvent& /*evt*/) throw( RuntimeException )
566 {
567 }
568 
569 //------------------------------------------------------------------------------
findWindow(const Reference<XControlContainer> & _rxCC) const570 PFormViewPageWindowAdapter FmXFormView::findWindow( const Reference< XControlContainer >& _rxCC )  const
571 {
572 	for (   PageWindowAdapterList::const_iterator i = m_aPageWindowAdapters.begin();
573 			i != m_aPageWindowAdapters.end();
574             ++i
575         )
576 	{
577 		if ( _rxCC == (*i)->getControlContainer() )
578 			return *i;
579 	}
580 	return NULL;
581 }
582 
583 //------------------------------------------------------------------------------
addWindow(const SdrPageWindow & rWindow)584 void FmXFormView::addWindow(const SdrPageWindow& rWindow)
585 {
586     FmFormPage* pFormPage = PTR_CAST( FmFormPage, rWindow.GetPageView().GetPage() );
587     if ( !pFormPage )
588         return;
589 
590     Reference< XControlContainer > xCC = rWindow.GetControlContainer();
591     if  (   xCC.is()
592         &&  ( !findWindow( xCC ).is() )
593         )
594     {
595         PFormViewPageWindowAdapter pAdapter = new FormViewPageWindowAdapter( m_aContext, rWindow, this );
596         m_aPageWindowAdapters.push_back( pAdapter );
597 
598         // Am ControlContainer horchen um Aenderungen mitzbekommen
599         Reference< XContainer >  xContainer( xCC, UNO_QUERY );
600         if ( xContainer.is() )
601             xContainer->addContainerListener( this );
602 	}
603 }
604 
605 //------------------------------------------------------------------------------
removeWindow(const Reference<XControlContainer> & _rxCC)606 void FmXFormView::removeWindow( const Reference< XControlContainer >& _rxCC )
607 {
608 	// Wird gerufen, wenn
609 	// - in den Design-Modus geschaltet wird
610 	// - ein Window geloescht wird, waehrend man im Design-Modus ist
611 	// - der Control-Container fuer ein Window entfernt wird, waehrend
612 	//   der aktive Modus eingeschaltet ist.
613 
614 	for (   PageWindowAdapterList::iterator i = m_aPageWindowAdapters.begin();
615 			i != m_aPageWindowAdapters.end();
616             ++i
617         )
618 	{
619 		if ( _rxCC != (*i)->getControlContainer() )
620             continue;
621 
622         Reference< XContainer >  xContainer( _rxCC, UNO_QUERY );
623 	    if ( xContainer.is() )
624 		    xContainer->removeContainerListener( this );
625 
626 	    (*i)->dispose();
627 	    m_aPageWindowAdapters.erase( i );
628         break;
629 	}
630 }
631 
632 //------------------------------------------------------------------------------
displayAsyncErrorMessage(const SQLErrorEvent & _rEvent)633 void FmXFormView::displayAsyncErrorMessage( const SQLErrorEvent& _rEvent )
634 {
635 	DBG_ASSERT( 0 == m_nErrorMessageEvent, "FmXFormView::displayAsyncErrorMessage: not too fast, please!" );
636 		// This should not happen - usually, the PostUserEvent is faster than any possible user
637 		// interaction which could trigger a new error. If it happens, we need a queue for the events.
638 	m_aAsyncError = _rEvent;
639 	m_nErrorMessageEvent = Application::PostUserEvent( LINK( this, FmXFormView, OnDelayedErrorMessage ) );
640 }
641 
642 //------------------------------------------------------------------------------
643 IMPL_LINK(FmXFormView, OnDelayedErrorMessage, void*, /*EMPTYTAG*/)
644 {
645 	m_nErrorMessageEvent = 0;
646 	displayException( m_aAsyncError );
647 	return 0L;
648 }
649 
650 //------------------------------------------------------------------------------
onFirstViewActivation(const FmFormModel * _pDocModel)651 void FmXFormView::onFirstViewActivation( const FmFormModel* _pDocModel )
652 {
653 	if ( _pDocModel && _pDocModel->GetAutoControlFocus() )
654 		m_nAutoFocusEvent = Application::PostUserEvent( LINK( this, FmXFormView, OnAutoFocus ) );
655 }
656 
657 //------------------------------------------------------------------------------
suspendTabOrderUpdate()658 void FmXFormView::suspendTabOrderUpdate()
659 {
660     OSL_ENSURE( !m_isTabOrderUpdateSuspended, "FmXFormView::suspendTabOrderUpdate: nesting not allowed!" );
661     m_isTabOrderUpdateSuspended = true;
662 }
663 
664 //------------------------------------------------------------------------------
resumeTabOrderUpdate()665 void FmXFormView::resumeTabOrderUpdate()
666 {
667     OSL_ENSURE( m_isTabOrderUpdateSuspended, "FmXFormView::resumeTabOrderUpdate: not suspended!" );
668     m_isTabOrderUpdateSuspended = false;
669 
670     // update the tab orders for all components which were collected since the suspendTabOrderUpdate call.
671     for (   MapControlContainerToSetOfForms::const_iterator container = m_aNeedTabOrderUpdate.begin();
672             container != m_aNeedTabOrderUpdate.end();
673             ++container
674         )
675     {
676         PFormViewPageWindowAdapter pAdapter = findWindow( container->first );
677         if ( !pAdapter.is() )
678             continue;
679 
680         for (   SetOfForms::const_iterator form = container->second.begin();
681                 form != container->second.end();
682                 ++form
683             )
684         {
685 	        pAdapter->updateTabOrder( *form );
686         }
687     }
688     m_aNeedTabOrderUpdate.clear();
689 }
690 
691 //------------------------------------------------------------------------------
692 IMPL_LINK(FmXFormView, OnActivate, void*, /*EMPTYTAG*/)
693 {
694 	m_nActivationEvent = 0;
695 
696 	if ( !m_pView )
697 	{
698 		DBG_ERROR( "FmXFormView::OnActivate: well .... seems we have a timing problem (the view already died)!" );
699 		return 0;
700 	}
701 
702 	// setting the controller to activate
703 	if (m_pView->GetFormShell() && m_pView->GetActualOutDev() && m_pView->GetActualOutDev()->GetOutDevType() == OUTDEV_WINDOW)
704 	{
705 		Window* pWindow = const_cast<Window*>(static_cast<const Window*>(m_pView->GetActualOutDev()));
706         PFormViewPageWindowAdapter pAdapter = m_aPageWindowAdapters.empty() ? NULL : m_aPageWindowAdapters[0];
707 		for (   PageWindowAdapterList::const_iterator i = m_aPageWindowAdapters.begin();
708 			    i != m_aPageWindowAdapters.end();
709                 ++i
710             )
711 		{
712 			if ( pWindow == (*i)->getWindow() )
713 				pAdapter =*i;
714 		}
715 
716 		if ( pAdapter.get() )
717 		{
718 			for (   ::std::vector< Reference< XFormController > >::const_iterator i = pAdapter->GetList().begin();
719 				    i != pAdapter->GetList().end();
720                     ++i
721                 )
722 			{
723 				const Reference< XFormController > & xController = *i;
724 				if ( !xController.is() )
725                     continue;
726 
727                 // only database forms are to be activated
728 				Reference< XRowSet >  xForm(xController->getModel(), UNO_QUERY);
729                 if ( !xForm.is() || !OStaticDataAccessTools().getRowSetConnection( xForm ).is() )
730                     continue;
731 
732                 Reference< XPropertySet > xFormSet( xForm, UNO_QUERY );
733                 ENSURE_OR_CONTINUE( xFormSet.is(), "FmXFormView::OnActivate: a form which does not have properties?" );
734 
735 				const ::rtl::OUString aSource = ::comphelper::getString( xFormSet->getPropertyValue( FM_PROP_COMMAND ) );
736 				if ( aSource.getLength() )
737 				{
738 					FmXFormShell* pShImpl =  m_pView->GetFormShell()->GetImpl();
739 					if ( pShImpl )
740 						pShImpl->setActiveController( xController );
741 					break;
742 				}
743 			}
744 		}
745 	}
746 	return 0;
747 }
748 
749 //------------------------------------------------------------------------------
Activate(sal_Bool bSync)750 void FmXFormView::Activate(sal_Bool bSync)
751 {
752 	if (m_nActivationEvent)
753 	{
754 		Application::RemoveUserEvent(m_nActivationEvent);
755 		m_nActivationEvent = 0;
756 	}
757 
758 	if (bSync)
759 	{
760 		LINK(this,FmXFormView,OnActivate).Call(NULL);
761 	}
762 	else
763 		m_nActivationEvent = Application::PostUserEvent(LINK(this,FmXFormView,OnActivate));
764 }
765 
766 //------------------------------------------------------------------------------
Deactivate(sal_Bool bDeactivateController)767 void FmXFormView::Deactivate(sal_Bool bDeactivateController)
768 {
769 	if (m_nActivationEvent)
770 	{
771 		Application::RemoveUserEvent(m_nActivationEvent);
772 		m_nActivationEvent = 0;
773 	}
774 
775 	FmXFormShell* pShImpl =  m_pView->GetFormShell() ? m_pView->GetFormShell()->GetImpl() : NULL;
776 	if (pShImpl && bDeactivateController)
777 		pShImpl->setActiveController( NULL );
778 }
779 
780 //------------------------------------------------------------------------------
GetFormShell() const781 FmFormShell* FmXFormView::GetFormShell() const
782 {
783 	return m_pView ? m_pView->GetFormShell() : NULL;
784 }
785 // -----------------------------------------------------------------------------
AutoFocus(sal_Bool _bSync)786 void FmXFormView::AutoFocus( sal_Bool _bSync )
787 {
788 	if (m_nAutoFocusEvent)
789         Application::RemoveUserEvent(m_nAutoFocusEvent);
790 
791 	if ( _bSync )
792 		OnAutoFocus( NULL );
793 	else
794 		m_nAutoFocusEvent = Application::PostUserEvent(LINK(this, FmXFormView, OnAutoFocus));
795 }
796 
797 // -----------------------------------------------------------------------------
isFocusable(const Reference<XControl> & i_rControl)798 bool FmXFormView::isFocusable( const Reference< XControl >& i_rControl )
799 {
800     if ( !i_rControl.is() )
801         return false;
802 
803 	try
804 	{
805 		Reference< XPropertySet > xModelProps( i_rControl->getModel(), UNO_QUERY_THROW );
806 
807         // only enabled controls are allowed to participate
808         sal_Bool bEnabled = sal_False;
809         OSL_VERIFY( xModelProps->getPropertyValue( FM_PROP_ENABLED ) >>= bEnabled );
810         if ( !bEnabled )
811             return false;
812 
813         // check the class id of the control model
814 		sal_Int16 nClassId = FormComponentType::CONTROL;
815 		OSL_VERIFY( xModelProps->getPropertyValue( FM_PROP_CLASSID ) >>= nClassId );
816 
817 		// controls which are not focussable
818 		if	(	( FormComponentType::CONTROL != nClassId )
819 			&&	( FormComponentType::IMAGEBUTTON != nClassId )
820 			&&	( FormComponentType::GROUPBOX != nClassId )
821 			&&	( FormComponentType::FIXEDTEXT != nClassId )
822 			&&	( FormComponentType::HIDDENCONTROL != nClassId )
823 			&&	( FormComponentType::IMAGECONTROL != nClassId )
824 			&&	( FormComponentType::SCROLLBAR != nClassId )
825 			&&	( FormComponentType::SPINBUTTON!= nClassId )
826 			)
827 		{
828             return true;
829 		}
830 	}
831 	catch( const Exception& e )
832 	{
833         DBG_UNHANDLED_EXCEPTION();
834 	}
835     return false;
836 }
837 
838 // -----------------------------------------------------------------------------
lcl_firstFocussableControl(const Sequence<Reference<XControl>> & _rControls)839 static Reference< XControl > lcl_firstFocussableControl( const Sequence< Reference< XControl > >& _rControls )
840 {
841 	Reference< XControl > xReturn;
842 
843 	// loop through all the controls
844 	const Reference< XControl >* pControls = _rControls.getConstArray();
845 	const Reference< XControl >* pControlsEnd = _rControls.getConstArray() + _rControls.getLength();
846 	for ( ; pControls != pControlsEnd; ++pControls )
847 	{
848 		if ( !pControls->is() )
849             continue;
850 
851         if ( FmXFormView::isFocusable( *pControls ) )
852         {
853             xReturn = *pControls;
854             break;
855         }
856 	}
857 
858 	if ( !xReturn.is() && _rControls.getLength() )
859 		xReturn = _rControls[0];
860 
861 	return xReturn;
862 }
863 
864 // -----------------------------------------------------------------------------
865 namespace
866 {
867     // .........................................................................
lcl_ensureControlsOfFormExist_nothrow(const SdrPage & _rPage,const SdrView & _rView,const Window & _rWindow,const Reference<XForm> & _rxForm)868     void lcl_ensureControlsOfFormExist_nothrow( const SdrPage& _rPage, const SdrView& _rView, const Window& _rWindow, const Reference< XForm >& _rxForm )
869     {
870         try
871         {
872             Reference< XInterface > xNormalizedForm( _rxForm, UNO_QUERY_THROW );
873 
874             SdrObjListIter aSdrObjectLoop( _rPage, IM_DEEPNOGROUPS );
875             while ( aSdrObjectLoop.IsMore() )
876             {
877                 FmFormObj* pFormObject = FmFormObj::GetFormObject( aSdrObjectLoop.Next() );
878                 if ( !pFormObject )
879                     continue;
880 
881                 Reference< XChild > xModel( pFormObject->GetUnoControlModel(), UNO_QUERY_THROW );
882                 Reference< XInterface > xModelParent( xModel->getParent(), UNO_QUERY_THROW );
883 
884                 if ( xNormalizedForm.get() != xModelParent.get() )
885                     continue;
886 
887                 pFormObject->GetUnoControl( _rView, _rWindow );
888             }
889         }
890         catch( const Exception& )
891         {
892         	DBG_UNHANDLED_EXCEPTION();
893         }
894     }
895 }
896 
897 // -----------------------------------------------------------------------------
getFormController(const Reference<XForm> & _rxForm,const OutputDevice & _rDevice) const898 Reference< XFormController > FmXFormView::getFormController( const Reference< XForm >& _rxForm, const OutputDevice& _rDevice ) const
899 {
900     Reference< XFormController > xController;
901 
902     for (   PageWindowAdapterList::const_iterator pos = m_aPageWindowAdapters.begin();
903             pos != m_aPageWindowAdapters.end();
904             ++pos
905         )
906     {
907         const PFormViewPageWindowAdapter pAdapter( *pos );
908         ENSURE_OR_CONTINUE( pAdapter.get(), "FmXFormView::getFormController: invalid page window adapter!" );
909         if ( pAdapter->getWindow() != &_rDevice )
910             // wrong device
911             continue;
912 
913         xController = pAdapter->getController( _rxForm );
914         if ( xController.is() )
915             break;
916     }
917     return xController;
918 }
919 
920 // -----------------------------------------------------------------------------
921 IMPL_LINK(FmXFormView, OnAutoFocus, void*, /*EMPTYTAG*/)
922 {
923 	m_nAutoFocusEvent = 0;
924 
925 	// go to the first form of our page, examine it's TabController, go to it's first (in terms of the tab order)
926 	// control, give it the focus
927 
928     do
929     {
930 
931 	// get the forms collection of the page we belong to
932     FmFormPage* pPage = m_pView ? PTR_CAST( FmFormPage, m_pView->GetSdrPageView()->GetPage() ) : NULL;
933     Reference< XIndexAccess > xForms( pPage ? Reference< XIndexAccess >( pPage->GetForms(), UNO_QUERY ) : Reference< XIndexAccess >() );
934 
935     const PFormViewPageWindowAdapter pAdapter = m_aPageWindowAdapters.empty() ? NULL : m_aPageWindowAdapters[0];
936     const Window* pWindow = pAdapter.get() ? pAdapter->getWindow() : NULL;
937 
938     ENSURE_OR_RETURN( xForms.is() && pWindow, "FmXFormView::OnAutoFocus: could not collect all essentials!", 0L );
939 
940     try
941 	{
942 		// go for the tab controller of the first form
943         if ( !xForms->getCount() )
944             break;
945         Reference< XForm > xForm( xForms->getByIndex( 0 ), UNO_QUERY_THROW );
946 		Reference< XTabController > xTabController( pAdapter->getController( xForm ), UNO_QUERY_THROW );
947 
948         // go for the first control of the controller
949 		Sequence< Reference< XControl > > aControls( xTabController->getControls() );
950         if ( aControls.getLength() == 0 )
951         {
952             Reference< XElementAccess > xFormElementAccess( xForm, UNO_QUERY_THROW );
953             if ( xFormElementAccess->hasElements() )
954             {
955                 // there are control models in the form, but no controls, yet.
956                 // Well, since some time controls are created on demand only. In particular,
957                 // they're normally created when they're first painted.
958                 // Unfortunately, the FormController does not have any way to
959                 // trigger the creation itself, so we must hack this ...
960                 lcl_ensureControlsOfFormExist_nothrow( *pPage, *m_pView, *pWindow, xForm );
961                 aControls = xTabController->getControls();
962                 OSL_ENSURE( aControls.getLength(), "FmXFormView::OnAutoFocus: no controls at all!" );
963             }
964         }
965 
966 		// set the focus to this first control
967 		Reference< XWindow > xControlWindow( lcl_firstFocussableControl( aControls ), UNO_QUERY );
968 		if ( !xControlWindow.is() )
969             break;
970 
971         xControlWindow->setFocus();
972 
973 		// ensure that the control is visible
974 		// 80210 - 12/07/00 - FS
975         const Window* pCurrentWindow = dynamic_cast< const Window* >( m_pView->GetActualOutDev() );
976         if ( pCurrentWindow )
977 		{
978             awt::Rectangle aRect = xControlWindow->getPosSize();
979 			::Rectangle aNonUnoRect( aRect.X, aRect.Y, aRect.X + aRect.Width, aRect.Y + aRect.Height );
980 			m_pView->MakeVisible( pCurrentWindow->PixelToLogic( aNonUnoRect ), *const_cast< Window* >( pCurrentWindow ) );
981 		}
982 	}
983     catch( const Exception& )
984     {
985     	DBG_UNHANDLED_EXCEPTION();
986     }
987 
988     }   // do
989     while ( false );
990 
991 	return 1L;
992 }
993 
994 // -----------------------------------------------------------------------------
onCreatedFormObject(FmFormObj & _rFormObject)995 void FmXFormView::onCreatedFormObject( FmFormObj& _rFormObject )
996 {
997     FmFormShell* pShell = m_pView ? m_pView->GetFormShell() : NULL;
998     FmXFormShell* pShellImpl = pShell ? pShell->GetImpl() : NULL;
999     OSL_ENSURE( pShellImpl, "FmXFormView::onCreatedFormObject: no form shell!" );
1000     if ( !pShellImpl )
1001         return;
1002 
1003     // it is valid that the form shell's forms collection is not initialized, yet
1004     pShellImpl->UpdateForms( sal_True );
1005 
1006     m_xLastCreatedControlModel.set( _rFormObject.GetUnoControlModel(), UNO_QUERY );
1007     if ( !m_xLastCreatedControlModel.is() )
1008         return;
1009 
1010     // some initial property defaults
1011     FormControlFactory aControlFactory( m_aContext );
1012     aControlFactory.initializeControlModel( pShellImpl->getDocumentType(), _rFormObject );
1013 
1014     if ( !pShellImpl->GetWizardUsing() )
1015         return;
1016 
1017     // #i31958# don't call wizards in XForms mode
1018     if ( pShellImpl->isEnhancedForm() )
1019         return;
1020 
1021     // #i46898# no wizards if there is no Base installed - currently, all wizards are
1022     // database related
1023     if ( !SvtModuleOptions().IsModuleInstalled( SvtModuleOptions::E_SDATABASE ) )
1024         return;
1025 
1026     if ( m_nControlWizardEvent )
1027         Application::RemoveUserEvent( m_nControlWizardEvent );
1028     m_nControlWizardEvent = Application::PostUserEvent( LINK( this, FmXFormView, OnStartControlWizard ) );
1029 }
1030 
1031 // -----------------------------------------------------------------------------
1032 IMPL_LINK( FmXFormView, OnStartControlWizard, void*, /**/ )
1033 {
1034     m_nControlWizardEvent = 0;
1035     OSL_PRECOND( m_xLastCreatedControlModel.is(), "FmXFormView::OnStartControlWizard: illegal call!" );
1036     if ( !m_xLastCreatedControlModel.is() )
1037         return 0L;
1038 
1039     sal_Int16 nClassId = FormComponentType::CONTROL;
1040     try
1041     {
1042         OSL_VERIFY( m_xLastCreatedControlModel->getPropertyValue( FM_PROP_CLASSID ) >>= nClassId );
1043     }
1044     catch( const Exception& )
1045     {
1046     	DBG_UNHANDLED_EXCEPTION();
1047     }
1048 
1049     const sal_Char* pWizardAsciiName = NULL;
1050     switch ( nClassId )
1051     {
1052         case FormComponentType::GRIDCONTROL:
1053             pWizardAsciiName = "com.sun.star.sdb.GridControlAutoPilot";
1054             break;
1055         case FormComponentType::LISTBOX:
1056         case FormComponentType::COMBOBOX:
1057             pWizardAsciiName = "com.sun.star.sdb.ListComboBoxAutoPilot";
1058             break;
1059         case FormComponentType::GROUPBOX:
1060             pWizardAsciiName = "com.sun.star.sdb.GroupBoxAutoPilot";
1061             break;
1062     }
1063 
1064     if ( pWizardAsciiName )
1065     {
1066         // build the argument list
1067         ::comphelper::NamedValueCollection aWizardArgs;
1068         aWizardArgs.put( "ObjectModel", m_xLastCreatedControlModel );
1069 
1070         // create the wizard object
1071         Reference< XExecutableDialog > xWizard;
1072         try
1073         {
1074             m_aContext.createComponentWithArguments( pWizardAsciiName, aWizardArgs.getWrappedPropertyValues(), xWizard );
1075         }
1076         catch( const Exception& )
1077         {
1078             DBG_UNHANDLED_EXCEPTION();
1079         }
1080 
1081         if ( !xWizard.is() )
1082         {
1083             ShowServiceNotAvailableError( NULL, String::CreateFromAscii( pWizardAsciiName ), sal_True );
1084         }
1085         else
1086         {
1087             // execute the wizard
1088             try
1089             {
1090                 xWizard->execute();
1091             }
1092             catch( const Exception& )
1093             {
1094                 DBG_UNHANDLED_EXCEPTION();
1095             }
1096         }
1097     }
1098 
1099     m_xLastCreatedControlModel.clear();
1100     return 1L;
1101 }
1102 
1103 // -----------------------------------------------------------------------------
1104 namespace
1105 {
lcl_insertIntoFormComponentHierarchy_throw(const FmFormView & _rView,const SdrUnoObj & _rSdrObj,const Reference<XDataSource> & _rxDataSource=NULL,const::rtl::OUString & _rDataSourceName=::rtl::OUString (),const::rtl::OUString & _rCommand=::rtl::OUString (),const sal_Int32 _nCommandType=-1)1106     void lcl_insertIntoFormComponentHierarchy_throw( const FmFormView& _rView, const SdrUnoObj& _rSdrObj,
1107         const Reference< XDataSource >& _rxDataSource = NULL, const ::rtl::OUString& _rDataSourceName = ::rtl::OUString(),
1108         const ::rtl::OUString& _rCommand = ::rtl::OUString(), const sal_Int32 _nCommandType = -1 )
1109     {
1110 		FmFormPage& rPage = static_cast< FmFormPage& >( *_rView.GetSdrPageView()->GetPage() );
1111 
1112 	    Reference< XFormComponent > xFormComponent( _rSdrObj.GetUnoControlModel(), UNO_QUERY_THROW );
1113         Reference< XForm > xTargetForm(
1114             rPage.GetImpl().findPlaceInFormComponentHierarchy( xFormComponent, _rxDataSource, _rDataSourceName, _rCommand, _nCommandType ),
1115             UNO_SET_THROW );
1116 
1117         rPage.GetImpl().setUniqueName( xFormComponent, xTargetForm );
1118 
1119         Reference< XIndexContainer > xFormAsContainer( xTargetForm, UNO_QUERY_THROW );
1120 	    xFormAsContainer->insertByIndex( xFormAsContainer->getCount(), makeAny( xFormComponent ) );
1121     }
1122 }
1123 
1124 // -----------------------------------------------------------------------------
implCreateFieldControl(const::svx::ODataAccessDescriptor & _rColumnDescriptor)1125 SdrObject* FmXFormView::implCreateFieldControl( const ::svx::ODataAccessDescriptor& _rColumnDescriptor )
1126 {
1127 	// not if we're in design mode
1128 	if ( !m_pView->IsDesignMode() )
1129 		return NULL;
1130 
1131 	::rtl::OUString sCommand, sFieldName;
1132 	sal_Int32 nCommandType = CommandType::COMMAND;
1133     SharedConnection xConnection;
1134 
1135 	::rtl::OUString sDataSource = _rColumnDescriptor.getDataSource();
1136 	_rColumnDescriptor[ daCommand ]		>>= sCommand;
1137 	_rColumnDescriptor[ daColumnName ]	>>= sFieldName;
1138 	_rColumnDescriptor[ daCommandType ]	>>= nCommandType;
1139     {
1140 	    Reference< XConnection > xExternalConnection;
1141 	    _rColumnDescriptor[ daConnection ]  >>= xExternalConnection;
1142         xConnection.reset( xExternalConnection, SharedConnection::NoTakeOwnership );
1143     }
1144 
1145 	if  (   !sCommand.getLength()
1146         ||  !sFieldName.getLength()
1147         ||  (   !sDataSource.getLength()
1148             &&  !xConnection.is()
1149             )
1150         )
1151     {
1152         DBG_ERROR( "FmXFormView::implCreateFieldControl: nonsense!" );
1153     }
1154 
1155 	Reference< XDataSource > xDataSource;
1156 	SQLErrorEvent aError;
1157 	try
1158 	{
1159         if ( xConnection.is() && !xDataSource.is() && !sDataSource.getLength() )
1160         {
1161             Reference< XChild > xChild( xConnection, UNO_QUERY );
1162             if ( xChild.is() )
1163                 xDataSource = xDataSource.query( xChild->getParent() );
1164         }
1165 
1166 	    // obtain the data source
1167         if ( !xDataSource.is() )
1168 		    xDataSource = OStaticDataAccessTools().getDataSource( sDataSource, m_aContext.getLegacyServiceFactory() );
1169 
1170         // and the connection, if necessary
1171 		if ( !xConnection.is() )
1172 			xConnection.reset( OStaticDataAccessTools().getConnection_withFeedback(
1173                 sDataSource,
1174                 ::rtl::OUString(),
1175                 ::rtl::OUString(),
1176                 m_aContext.getLegacyServiceFactory()
1177             ) );
1178 	}
1179     catch ( const SQLException& )
1180     {
1181         aError.Reason = ::cppu::getCaughtException();
1182     }
1183     catch( const Exception& ) { /* will be asserted below */ }
1184 	if (aError.Reason.hasValue())
1185 	{
1186 		displayAsyncErrorMessage( aError );
1187 		return NULL;
1188 	}
1189 
1190 	// need a data source and a connection here
1191 	if (!xDataSource.is() || !xConnection.is())
1192 	{
1193 		DBG_ERROR("FmXFormView::implCreateFieldControl : could not retrieve the data source or the connection!");
1194 		return NULL;
1195 	}
1196 
1197 	OStaticDataAccessTools aDBATools;
1198 	Reference< XComponent > xKeepFieldsAlive;
1199 	// go
1200 	try
1201 	{
1202 		// determine the table/query field which we should create a control for
1203 		Reference< XPropertySet >	xField;
1204 
1205 		Reference< XNameAccess >	xFields = aDBATools.getFieldsByCommandDescriptor(
1206 			xConnection, nCommandType, sCommand, xKeepFieldsAlive );
1207 
1208 		if (xFields.is() && xFields->hasByName(sFieldName))
1209 			xFields->getByName(sFieldName) >>= xField;
1210         if ( !xField.is() )
1211             return NULL;
1212 
1213 		Reference< XNumberFormatsSupplier > xSupplier( aDBATools.getNumberFormats( xConnection, sal_False ), UNO_SET_THROW );
1214 		Reference< XNumberFormats >  xNumberFormats( xSupplier->getNumberFormats(), UNO_SET_THROW );
1215 
1216 		::rtl::OUString sLabelPostfix;
1217 
1218 		////////////////////////////////////////////////////////////////
1219 		// nur fuer Textgroesse
1220 		OutputDevice* pOutDev = NULL;
1221 		if (m_pView->GetActualOutDev() && m_pView->GetActualOutDev()->GetOutDevType() == OUTDEV_WINDOW)
1222 			pOutDev = const_cast<OutputDevice*>(m_pView->GetActualOutDev());
1223 		else
1224 		{// OutDev suchen
1225 			SdrPageView* pPageView = m_pView->GetSdrPageView();
1226 			if( pPageView && !pOutDev )
1227 			{
1228 				// const SdrPageViewWinList& rWinList = pPageView->GetWinList();
1229 				// const SdrPageViewWindows& rPageViewWindows = pPageView->GetPageViewWindows();
1230 
1231 				for( sal_uInt32 i = 0L; i < pPageView->PageWindowCount(); i++ )
1232 				{
1233 					const SdrPageWindow& rPageWindow = *pPageView->GetPageWindow(i);
1234 
1235 					if( rPageWindow.GetPaintWindow().OutputToWindow())
1236 					{
1237 						pOutDev = &rPageWindow.GetPaintWindow().GetOutputDevice();
1238 						break;
1239 					}
1240 				}
1241 			}
1242 		}
1243 
1244 		if ( !pOutDev )
1245 			return NULL;
1246 
1247 		sal_Int32 nDataType = ::comphelper::getINT32(xField->getPropertyValue(FM_PROP_FIELDTYPE));
1248 		if ((DataType::BINARY == nDataType) || (DataType::VARBINARY == nDataType))
1249 			return NULL;
1250 
1251 		//////////////////////////////////////////////////////////////////////
1252 		// determine the control type by examining the data type of the bound column
1253 		sal_uInt16 nOBJID = 0;
1254 		sal_Bool bDateNTimeField = sal_False;
1255 
1256 		sal_Bool bIsCurrency = sal_False;
1257 		if (::comphelper::hasProperty(FM_PROP_ISCURRENCY, xField))
1258 			bIsCurrency = ::comphelper::getBOOL(xField->getPropertyValue(FM_PROP_ISCURRENCY));
1259 
1260 		if (bIsCurrency)
1261 			nOBJID = OBJ_FM_CURRENCYFIELD;
1262 		else
1263 			switch (nDataType)
1264 			{
1265 				case DataType::BLOB:
1266 				case DataType::LONGVARBINARY:
1267 					nOBJID = OBJ_FM_IMAGECONTROL;
1268 					break;
1269 				case DataType::LONGVARCHAR:
1270 				case DataType::CLOB:
1271 					nOBJID = OBJ_FM_EDIT;
1272 					break;
1273 				case DataType::BINARY:
1274 				case DataType::VARBINARY:
1275 					return NULL;
1276 				case DataType::BIT:
1277 				case DataType::BOOLEAN:
1278 					nOBJID = OBJ_FM_CHECKBOX;
1279 					break;
1280 				case DataType::TINYINT:
1281 				case DataType::SMALLINT:
1282 				case DataType::INTEGER:
1283 					nOBJID = OBJ_FM_NUMERICFIELD;
1284 					break;
1285 				case DataType::REAL:
1286 				case DataType::DOUBLE:
1287 				case DataType::NUMERIC:
1288 				case DataType::DECIMAL:
1289 					nOBJID = OBJ_FM_FORMATTEDFIELD;
1290 					break;
1291 				case DataType::TIMESTAMP:
1292 					bDateNTimeField = sal_True;
1293 					sLabelPostfix = String( SVX_RES( RID_STR_POSTFIX_DATE ) );
1294 					// DON'T break !
1295 				case DataType::DATE:
1296 					nOBJID = OBJ_FM_DATEFIELD;
1297 					break;
1298 				case DataType::TIME:
1299 					nOBJID = OBJ_FM_TIMEFIELD;
1300 					break;
1301 				case DataType::CHAR:
1302 				case DataType::VARCHAR:
1303 				default:
1304 					nOBJID = OBJ_FM_EDIT;
1305 					break;
1306 			}
1307 		if (!nOBJID)
1308 			return NULL;
1309 
1310 		SdrUnoObj* pLabel( NULL );
1311 		SdrUnoObj* pControl( NULL );
1312 		if  (   !createControlLabelPair( *pOutDev, 0, 0, xField, xNumberFormats, nOBJID, sLabelPostfix,
1313                     pLabel, pControl, xDataSource, sDataSource, sCommand, nCommandType )
1314             )
1315 		{
1316 			return NULL;
1317 		}
1318 
1319 		//////////////////////////////////////////////////////////////////////
1320 		// group objects
1321 		bool bCheckbox = ( OBJ_FM_CHECKBOX == nOBJID );
1322         OSL_ENSURE( !bCheckbox || !pLabel, "FmXFormView::implCreateFieldControl: why was there a label created for a check box?" );
1323         if ( bCheckbox )
1324             return pControl;
1325 
1326 		SdrObjGroup* pGroup  = new SdrObjGroup();
1327 		SdrObjList* pObjList = pGroup->GetSubList();
1328 		pObjList->InsertObject( pLabel );
1329 		pObjList->InsertObject( pControl );
1330 
1331 		if ( bDateNTimeField )
1332 		{	// so far we created a date field only, but we also need a time field
1333 			pLabel = pControl = NULL;
1334 			if  (   createControlLabelPair( *pOutDev, 0, 1000, xField, xNumberFormats, OBJ_FM_TIMEFIELD,
1335                         String( SVX_RES( RID_STR_POSTFIX_TIME ) ), pLabel, pControl,
1336                         xDataSource, sDataSource, sCommand, nCommandType )
1337                 )
1338 			{
1339 				pObjList->InsertObject( pLabel );
1340 				pObjList->InsertObject( pControl );
1341 			}
1342 		}
1343 
1344 		return pGroup; // und fertig
1345 	}
1346 	catch(const Exception&)
1347 	{
1348         DBG_UNHANDLED_EXCEPTION();
1349 	}
1350 
1351 
1352 	return NULL;
1353 }
1354 
1355 // -----------------------------------------------------------------------------
implCreateXFormsControl(const::svx::OXFormsDescriptor & _rDesc)1356 SdrObject* FmXFormView::implCreateXFormsControl( const ::svx::OXFormsDescriptor &_rDesc )
1357 {
1358 	// not if we're in design mode
1359 	if ( !m_pView->IsDesignMode() )
1360 		return NULL;
1361 
1362 	Reference< XComponent > xKeepFieldsAlive;
1363 
1364 	// go
1365 	try
1366 	{
1367 		// determine the table/query field which we should create a control for
1368 		Reference< XNumberFormats >	xNumberFormats;
1369 		::rtl::OUString sLabelPostfix = _rDesc.szName;
1370 
1371 		////////////////////////////////////////////////////////////////
1372 		// nur fuer Textgroesse
1373 		OutputDevice* pOutDev = NULL;
1374 		if (m_pView->GetActualOutDev() && m_pView->GetActualOutDev()->GetOutDevType() == OUTDEV_WINDOW)
1375 			pOutDev = const_cast<OutputDevice*>(m_pView->GetActualOutDev());
1376 		else
1377 		{// OutDev suchen
1378 			SdrPageView* pPageView = m_pView->GetSdrPageView();
1379 			if( pPageView && !pOutDev )
1380 			{
1381 				// const SdrPageViewWinList& rWinList = pPageView->GetWinList();
1382 				// const SdrPageViewWindows& rPageViewWindows = pPageView->GetPageViewWindows();
1383 
1384 				for( sal_uInt32 i = 0L; i < pPageView->PageWindowCount(); i++ )
1385 				{
1386 					const SdrPageWindow& rPageWindow = *pPageView->GetPageWindow(i);
1387 
1388 					if( rPageWindow.GetPaintWindow().GetOutputDevice().GetOutDevType() == OUTDEV_WINDOW)
1389 					{
1390 						pOutDev = &rPageWindow.GetPaintWindow().GetOutputDevice();
1391 						break;
1392 					}
1393 				}
1394 			}
1395 		}
1396 
1397 		if ( !pOutDev )
1398 			return NULL;
1399 
1400 		//////////////////////////////////////////////////////////////////////
1401 		// The service name decides which control should be created
1402 		sal_uInt16 nOBJID = OBJ_FM_EDIT;
1403 		if(::rtl::OUString(_rDesc.szServiceName).equals((::rtl::OUString)FM_SUN_COMPONENT_NUMERICFIELD))
1404 			nOBJID = OBJ_FM_NUMERICFIELD;
1405 		if(::rtl::OUString(_rDesc.szServiceName).equals((::rtl::OUString)FM_SUN_COMPONENT_CHECKBOX))
1406 			nOBJID = OBJ_FM_CHECKBOX;
1407 		if(::rtl::OUString(_rDesc.szServiceName).equals((::rtl::OUString)FM_COMPONENT_COMMANDBUTTON))
1408 			nOBJID = OBJ_FM_BUTTON;
1409 
1410 		typedef ::com::sun::star::form::submission::XSubmission XSubmission_t;
1411 		Reference< XSubmission_t > xSubmission(_rDesc.xPropSet, UNO_QUERY);
1412 
1413 		// xform control or submission button?
1414 		if ( !xSubmission.is() )
1415         {
1416 			SdrUnoObj* pLabel( NULL );
1417 			SdrUnoObj* pControl( NULL );
1418 			if  (   !createControlLabelPair( *pOutDev, 0, 0, NULL, xNumberFormats, nOBJID, sLabelPostfix,
1419                         pLabel, pControl )
1420                 )
1421             {
1422 				return NULL;
1423 			}
1424 
1425 			//////////////////////////////////////////////////////////////////////
1426 			// Now build the connection between the control and the data item.
1427 			Reference< XValueBinding > xValueBinding(_rDesc.xPropSet,UNO_QUERY);
1428 			Reference< XBindableValue > xBindableValue(pControl->GetUnoControlModel(),UNO_QUERY);
1429 
1430             DBG_ASSERT( xBindableValue.is(), "FmXFormView::implCreateXFormsControl: control's not bindable!" );
1431             if ( xBindableValue.is() )
1432 			    xBindableValue->setValueBinding(xValueBinding);
1433 
1434 		    bool bCheckbox = ( OBJ_FM_CHECKBOX == nOBJID );
1435             OSL_ENSURE( !bCheckbox || !pLabel, "FmXFormView::implCreateXFormsControl: why was there a label created for a check box?" );
1436             if ( bCheckbox )
1437                 return pControl;
1438 
1439 			//////////////////////////////////////////////////////////////////////
1440 			// group objects
1441 			SdrObjGroup* pGroup  = new SdrObjGroup();
1442 			SdrObjList* pObjList = pGroup->GetSubList();
1443 			pObjList->InsertObject(pLabel);
1444 			pObjList->InsertObject(pControl);
1445 
1446 			return pGroup;
1447 		}
1448 		else {
1449 
1450 			// create a button control
1451 			const MapMode eTargetMode( pOutDev->GetMapMode() );
1452 			const MapMode eSourceMode(MAP_100TH_MM);
1453 			const sal_uInt16 nObjID = OBJ_FM_BUTTON;
1454 			::Size controlSize(4000, 500);
1455 			FmFormObj *pControl = static_cast<FmFormObj*>(SdrObjFactory::MakeNewObject( FmFormInventor, nObjID, NULL, NULL ));
1456 			controlSize.Width() = Fraction(controlSize.Width(), 1) * eTargetMode.GetScaleX();
1457 			controlSize.Height() = Fraction(controlSize.Height(), 1) * eTargetMode.GetScaleY();
1458 			::Point controlPos( pOutDev->LogicToLogic( ::Point( controlSize.Width(), 0 ), eSourceMode, eTargetMode ) );
1459 			::Rectangle controlRect( controlPos, pOutDev->LogicToLogic( controlSize, eSourceMode, eTargetMode ) );
1460 			pControl->SetLogicRect(controlRect);
1461 
1462 			// set the button label
1463 			Reference< XPropertySet > xControlSet(pControl->GetUnoControlModel(), UNO_QUERY);
1464 			xControlSet->setPropertyValue(FM_PROP_LABEL, makeAny(::rtl::OUString(_rDesc.szName)));
1465 
1466 			// connect the submission with the submission supplier (aka the button)
1467             xControlSet->setPropertyValue( FM_PROP_BUTTON_TYPE,
1468                                            makeAny( FormButtonType_SUBMIT ) );
1469 			typedef ::com::sun::star::form::submission::XSubmissionSupplier XSubmissionSupplier_t;
1470 			Reference< XSubmissionSupplier_t > xSubmissionSupplier(pControl->GetUnoControlModel(), UNO_QUERY);
1471 			xSubmissionSupplier->setSubmission(xSubmission);
1472 
1473 			return pControl;
1474 		}
1475 	}
1476 	catch(const Exception&)
1477 	{
1478         DBG_ERROR("FmXFormView::implCreateXFormsControl: caught an exception while creating the control !");
1479 	}
1480 
1481 
1482 	return NULL;
1483 }
1484 
1485 //------------------------------------------------------------------------
createControlLabelPair(OutputDevice & _rOutDev,sal_Int32 _nXOffsetMM,sal_Int32 _nYOffsetMM,const Reference<XPropertySet> & _rxField,const Reference<XNumberFormats> & _rxNumberFormats,sal_uInt16 _nControlObjectID,const::rtl::OUString & _rFieldPostfix,SdrUnoObj * & _rpLabel,SdrUnoObj * & _rpControl,const Reference<XDataSource> & _rxDataSource,const::rtl::OUString & _rDataSourceName,const::rtl::OUString & _rCommand,const sal_Int32 _nCommandType)1486 bool FmXFormView::createControlLabelPair( OutputDevice& _rOutDev, sal_Int32 _nXOffsetMM, sal_Int32 _nYOffsetMM,
1487         const Reference< XPropertySet >& _rxField, const Reference< XNumberFormats >& _rxNumberFormats,
1488         sal_uInt16 _nControlObjectID, const ::rtl::OUString& _rFieldPostfix,
1489         SdrUnoObj*& _rpLabel, SdrUnoObj*& _rpControl,
1490         const Reference< XDataSource >& _rxDataSource, const ::rtl::OUString& _rDataSourceName,
1491         const ::rtl::OUString& _rCommand, const sal_Int32 _nCommandType )
1492 {
1493     if  (   !createControlLabelPair( m_aContext, _rOutDev, _nXOffsetMM, _nYOffsetMM,
1494                 _rxField, _rxNumberFormats, _nControlObjectID, _rFieldPostfix, FmFormInventor, OBJ_FM_FIXEDTEXT,
1495                 NULL, NULL, NULL, _rpLabel, _rpControl )
1496         )
1497         return false;
1498 
1499     // insert the control model(s) into the form component hierarchy
1500     if ( _rpLabel )
1501         lcl_insertIntoFormComponentHierarchy_throw( *m_pView, *_rpLabel, _rxDataSource, _rDataSourceName, _rCommand, _nCommandType );
1502     lcl_insertIntoFormComponentHierarchy_throw( *m_pView, *_rpControl, _rxDataSource, _rDataSourceName, _rCommand, _nCommandType );
1503 
1504     // some context-dependent initializations
1505     FormControlFactory aControlFactory( m_aContext );
1506     if ( _rpLabel )
1507         aControlFactory.initializeControlModel( impl_getDocumentType(), *_rpLabel );
1508     aControlFactory.initializeControlModel( impl_getDocumentType(), *_rpControl );
1509 
1510     return true;
1511 }
1512 
1513 //------------------------------------------------------------------------
createControlLabelPair(const::comphelper::ComponentContext & _rContext,OutputDevice & _rOutDev,sal_Int32 _nXOffsetMM,sal_Int32 _nYOffsetMM,const Reference<XPropertySet> & _rxField,const Reference<XNumberFormats> & _rxNumberFormats,sal_uInt16 _nControlObjectID,const::rtl::OUString & _rFieldPostfix,sal_uInt32 _nInventor,sal_uInt16 _nLabelObjectID,SdrPage * _pLabelPage,SdrPage * _pControlPage,SdrModel * _pModel,SdrUnoObj * & _rpLabel,SdrUnoObj * & _rpControl)1514 bool FmXFormView::createControlLabelPair( const ::comphelper::ComponentContext& _rContext,
1515     OutputDevice& _rOutDev, sal_Int32 _nXOffsetMM, sal_Int32 _nYOffsetMM, const Reference< XPropertySet >& _rxField,
1516     const Reference< XNumberFormats >& _rxNumberFormats, sal_uInt16 _nControlObjectID,
1517     const ::rtl::OUString& _rFieldPostfix, sal_uInt32 _nInventor, sal_uInt16 _nLabelObjectID,
1518     SdrPage* _pLabelPage, SdrPage* _pControlPage, SdrModel* _pModel, SdrUnoObj*& _rpLabel, SdrUnoObj*& _rpControl)
1519 {
1520 	sal_Int32 nDataType = 0;
1521 	::rtl::OUString sFieldName;
1522 	Any aFieldName;
1523 	if ( _rxField.is() )
1524 	{
1525 		nDataType = ::comphelper::getINT32(_rxField->getPropertyValue(FM_PROP_FIELDTYPE));
1526 		aFieldName = Any(_rxField->getPropertyValue(FM_PROP_NAME));
1527 		aFieldName >>= sFieldName;
1528 	}
1529 
1530 	// calculate the positions, respecting the settings of the target device
1531 	::Size aTextSize( _rOutDev.GetTextWidth(sFieldName + _rFieldPostfix), _rOutDev.GetTextHeight() );
1532 
1533 	MapMode   eTargetMode( _rOutDev.GetMapMode() ),
1534 			  eSourceMode( MAP_100TH_MM );
1535 
1536 	// Textbreite ist mindestens 4cm
1537 	// Texthoehe immer halber cm
1538 	::Size aDefTxtSize(4000, 500);
1539 	::Size aDefSize(4000, 500);
1540 	::Size aDefImageSize(4000, 4000);
1541 
1542 	::Size aRealSize = _rOutDev.LogicToLogic(aTextSize, eTargetMode, eSourceMode);
1543 	aRealSize.Width() = std::max(aRealSize.Width(), aDefTxtSize.Width());
1544 	aRealSize.Height()= aDefSize.Height();
1545 
1546 	// adjust to scaling of the target device (#53523#)
1547 	aRealSize.Width() = long(Fraction(aRealSize.Width(), 1) * eTargetMode.GetScaleX());
1548 	aRealSize.Height() = long(Fraction(aRealSize.Height(), 1) * eTargetMode.GetScaleY());
1549 
1550     // for boolean fields, we do not create a label, but just a checkbox
1551     bool bNeedLabel = ( _nControlObjectID != OBJ_FM_CHECKBOX );
1552 
1553 	// the label
1554     ::std::auto_ptr< SdrUnoObj > pLabel;
1555     Reference< XPropertySet > xLabelModel;
1556     if ( bNeedLabel )
1557     {
1558         pLabel.reset( dynamic_cast< SdrUnoObj* >(
1559             SdrObjFactory::MakeNewObject( _nInventor, _nLabelObjectID, _pLabelPage, _pModel ) ) );
1560         OSL_ENSURE( pLabel.get(), "FmXFormView::createControlLabelPair: could not create the label!" );
1561         if ( !pLabel.get() )
1562             return false;
1563 
1564 		xLabelModel.set( pLabel->GetUnoControlModel(), UNO_QUERY );
1565 		if ( xLabelModel.is() )
1566         {
1567             ::rtl::OUString sLabel;
1568             if ( _rxField.is() && _rxField->getPropertySetInfo()->hasPropertyByName(FM_PROP_LABEL) )
1569                 _rxField->getPropertyValue(FM_PROP_LABEL) >>= sLabel;
1570             if ( !sLabel.getLength() )
1571                 sLabel = sFieldName;
1572 
1573 		    xLabelModel->setPropertyValue( FM_PROP_LABEL, makeAny( sLabel + _rFieldPostfix ) );
1574             String sObjectLabel( SVX_RES( RID_STR_OBJECT_LABEL ) );
1575             sObjectLabel.SearchAndReplaceAllAscii( "#object#", sFieldName );
1576             xLabelModel->setPropertyValue( FM_PROP_NAME, makeAny( ::rtl::OUString( sObjectLabel ) ) );
1577         }
1578 
1579 	    pLabel->SetLogicRect( ::Rectangle(
1580             _rOutDev.LogicToLogic( ::Point( _nXOffsetMM, _nYOffsetMM ), eSourceMode, eTargetMode ),
1581             _rOutDev.LogicToLogic( aRealSize, eSourceMode, eTargetMode )
1582         ) );
1583     }
1584 
1585 	// the control
1586     ::std::auto_ptr< SdrUnoObj > pControl( dynamic_cast< SdrUnoObj* >(
1587 	    SdrObjFactory::MakeNewObject( _nInventor, _nControlObjectID, _pControlPage, _pModel ) ) );
1588     OSL_ENSURE( pControl.get(), "FmXFormView::createControlLabelPair: could not create the control!" );
1589     if ( !pControl.get() )
1590         return false;
1591 
1592     Reference< XPropertySet > xControlSet( pControl->GetUnoControlModel(), UNO_QUERY );
1593     if ( !xControlSet.is() )
1594         return false;
1595 
1596 	// size of the control
1597 	::Size aControlSize( aDefSize );
1598     switch ( nDataType )
1599     {
1600     case DataType::BIT:
1601     case DataType::BOOLEAN:
1602 		aControlSize = aDefSize;
1603         break;
1604     case DataType::LONGVARCHAR:
1605 	case DataType::CLOB:
1606     case DataType::LONGVARBINARY:
1607 	case DataType::BLOB:
1608 		aControlSize = aDefImageSize;
1609         break;
1610     }
1611 
1612     if ( OBJ_FM_IMAGECONTROL == _nControlObjectID )
1613 		aControlSize = aDefImageSize;
1614 
1615 	aControlSize.Width() = long(Fraction(aControlSize.Width(), 1) * eTargetMode.GetScaleX());
1616 	aControlSize.Height() = long(Fraction(aControlSize.Height(), 1) * eTargetMode.GetScaleY());
1617 
1618     pControl->SetLogicRect( ::Rectangle(
1619         _rOutDev.LogicToLogic( ::Point( aRealSize.Width() + _nXOffsetMM, _nYOffsetMM ), eSourceMode, eTargetMode ),
1620         _rOutDev.LogicToLogic( aControlSize, eSourceMode, eTargetMode )
1621     ) );
1622 
1623     // some initializations
1624     Reference< XPropertySetInfo > xControlPropInfo = xControlSet->getPropertySetInfo();
1625 
1626 	if ( aFieldName.hasValue() )
1627 	{
1628 		xControlSet->setPropertyValue( FM_PROP_CONTROLSOURCE, aFieldName );
1629 		xControlSet->setPropertyValue( FM_PROP_NAME, aFieldName );
1630         if ( !bNeedLabel )
1631         {
1632             // no dedicated label control => use the label property
1633             if ( xControlPropInfo->hasPropertyByName( FM_PROP_LABEL ) )
1634 		        xControlSet->setPropertyValue( FM_PROP_LABEL, makeAny( sFieldName + _rFieldPostfix ) );
1635             else
1636                 OSL_ENSURE( false, "FmXFormView::createControlLabelPair: can't set a label for the control!" );
1637         }
1638 	}
1639 
1640 	if ( (nDataType == DataType::LONGVARCHAR || nDataType == DataType::CLOB) && xControlPropInfo->hasPropertyByName( FM_PROP_MULTILINE ) )
1641 	{
1642 		xControlSet->setPropertyValue( FM_PROP_MULTILINE, makeAny( sal_Bool( sal_True ) ) );
1643 	}
1644 
1645     // announce the label to the control
1646     if ( xControlPropInfo->hasPropertyByName( FM_PROP_CONTROLLABEL ) && xLabelModel.is() )
1647     {
1648 	    try
1649 	    {
1650 		    xControlSet->setPropertyValue( FM_PROP_CONTROLLABEL, makeAny( xLabelModel ) );
1651 	    }
1652 	    catch( const Exception& )
1653 	    {
1654             DBG_UNHANDLED_EXCEPTION();
1655 	    }
1656     }
1657 
1658 	if ( _rxField.is() )
1659     {
1660     	FormControlFactory aControlFactory( _rContext );
1661 	    aControlFactory.initializeFieldDependentProperties( _rxField, xControlSet, _rxNumberFormats );
1662 	}
1663 
1664     _rpLabel = pLabel.release();
1665     _rpControl = pControl.release();
1666     return true;
1667 }
1668 
1669 //------------------------------------------------------------------------------
ObjectRemoveListener(FmXFormView * pParent)1670 FmXFormView::ObjectRemoveListener::ObjectRemoveListener( FmXFormView* pParent )
1671 	:m_pParent( pParent )
1672 {
1673 }
1674 
1675 //------------------------------------------------------------------------------
Notify(SfxBroadcaster &,const SfxHint & rHint)1676 void FmXFormView::ObjectRemoveListener::Notify( SfxBroadcaster& /*rBC*/, const SfxHint& rHint )
1677 {
1678 	if (rHint.ISA(SdrHint) && (((SdrHint&)rHint).GetKind() == HINT_OBJREMOVED))
1679 		m_pParent->ObjectRemovedInAliveMode(((SdrHint&)rHint).GetObject());
1680 }
1681 
1682 //------------------------------------------------------------------------------
ObjectRemovedInAliveMode(const SdrObject * pObject)1683 void FmXFormView::ObjectRemovedInAliveMode( const SdrObject* pObject )
1684 {
1685 	// wenn das entfernte Objekt in meiner MarkList, die ich mir beim Umschalten in den Alive-Mode gemerkt habe, steht,
1686 	// muss ich es jetzt da rausnehmen, da ich sonst beim Zurueckschalten versuche, die Markierung wieder zu setzen
1687 	// (interesanterweise geht das nur bei gruppierten Objekten schief (beim Zugriff auf deren ObjList GPF), nicht bei einzelnen)
1688 
1689 	sal_uIntPtr nCount = m_aMark.GetMarkCount();
1690 	for (sal_uIntPtr i = 0; i < nCount; ++i)
1691 	{
1692 		SdrMark* pMark = m_aMark.GetMark(i);
1693 		SdrObject* pCurrent = pMark->GetMarkedSdrObj();
1694 		if (pObject == pCurrent)
1695 		{
1696 			m_aMark.DeleteMark(i);
1697 			return;
1698 		}
1699 		// ich brauche nicht in GroupObjects absteigen : wenn dort unten ein Objekt geloescht wird, dann bleibt der
1700 		// Zeiger auf das GroupObject, den ich habe, trotzdem weiter gueltig bleibt ...
1701 	}
1702 }
1703 
1704 //------------------------------------------------------------------------------
stopMarkListWatching()1705 void FmXFormView::stopMarkListWatching()
1706 {
1707 	if ( m_pWatchStoredList )
1708 	{
1709 		m_pWatchStoredList->EndListeningAll();
1710 		delete m_pWatchStoredList;
1711 		m_pWatchStoredList = NULL;
1712 	}
1713 }
1714 
1715 //------------------------------------------------------------------------------
startMarkListWatching()1716 void FmXFormView::startMarkListWatching()
1717 {
1718 	if ( !m_pWatchStoredList )
1719 	{
1720 		m_pWatchStoredList = new ObjectRemoveListener( this );
1721 		FmFormModel* pModel = GetFormShell() ? GetFormShell()->GetFormModel() : NULL;
1722 		DBG_ASSERT( pModel != NULL, "FmXFormView::startMarkListWatching: shell has no model!" );
1723 		m_pWatchStoredList->StartListening( *static_cast< SfxBroadcaster* >( pModel ) );
1724 	}
1725 	else
1726 	{
1727 		DBG_ERROR( "FmXFormView::startMarkListWatching: already listening!" );
1728 	}
1729 }
1730 
1731 //------------------------------------------------------------------------------
saveMarkList(sal_Bool _bSmartUnmark)1732 void FmXFormView::saveMarkList( sal_Bool _bSmartUnmark )
1733 {
1734 	if ( m_pView )
1735 	{
1736 		m_aMark = m_pView->GetMarkedObjectList();
1737 		if ( _bSmartUnmark )
1738 		{
1739 			sal_uIntPtr nCount = m_aMark.GetMarkCount( );
1740 			for ( sal_uIntPtr i = 0; i < nCount; ++i )
1741 			{
1742 				SdrMark*   pMark = m_aMark.GetMark(i);
1743 				SdrObject* pObj  = pMark->GetMarkedSdrObj();
1744 
1745 				if ( m_pView->IsObjMarked( pObj ) )
1746 				{
1747 					if ( pObj->IsGroupObject() )
1748 					{
1749 						SdrObjListIter aIter( *pObj->GetSubList() );
1750 						sal_Bool bMixed = sal_False;
1751 						while ( aIter.IsMore() && !bMixed )
1752 							bMixed = ( aIter.Next()->GetObjInventor() != FmFormInventor );
1753 
1754 						if ( !bMixed )
1755 						{
1756 							// all objects in the group are form objects
1757 							m_pView->MarkObj( pMark->GetMarkedSdrObj(), pMark->GetPageView(), sal_True /* unmark! */ );
1758 						}
1759 					}
1760 					else
1761 					{
1762 						if ( pObj->GetObjInventor() == FmFormInventor )
1763 						{	// this is a form layer object
1764 							m_pView->MarkObj( pMark->GetMarkedSdrObj(), pMark->GetPageView(), sal_True /* unmark! */ );
1765 						}
1766 					}
1767 				}
1768 			}
1769 		}
1770 	}
1771 	else
1772 	{
1773 		DBG_ERROR( "FmXFormView::saveMarkList: invalid view!" );
1774 		m_aMark = SdrMarkList();
1775 	}
1776 }
1777 
1778 //--------------------------------------------------------------------------
lcl_hasObject(SdrObjListIter & rIter,SdrObject * pObj)1779 static sal_Bool lcl_hasObject( SdrObjListIter& rIter, SdrObject* pObj )
1780 {
1781 	sal_Bool bFound = sal_False;
1782 	while (rIter.IsMore() && !bFound)
1783 		bFound = pObj == rIter.Next();
1784 
1785 	rIter.Reset();
1786 	return bFound;
1787 }
1788 
1789 //------------------------------------------------------------------------------
restoreMarkList(SdrMarkList & _rRestoredMarkList)1790 void FmXFormView::restoreMarkList( SdrMarkList& _rRestoredMarkList )
1791 {
1792 	if ( !m_pView )
1793 		return;
1794 
1795 	_rRestoredMarkList.Clear();
1796 
1797 	const SdrMarkList& rCurrentList = m_pView->GetMarkedObjectList();
1798 	FmFormPage* pPage = GetFormShell() ? GetFormShell()->GetCurPage() : NULL;
1799 	if (pPage)
1800 	{
1801 		if (rCurrentList.GetMarkCount())
1802 		{	// there is a current mark ... hmm. Is it a subset of the mark we remembered in saveMarkList?
1803 			sal_Bool bMisMatch = sal_False;
1804 
1805 			// loop through all current marks
1806 			sal_uIntPtr nCurrentCount = rCurrentList.GetMarkCount();
1807 			for ( sal_uIntPtr i=0; i<nCurrentCount&& !bMisMatch; ++i )
1808 			{
1809 				const SdrObject* pCurrentMarked = rCurrentList.GetMark( i )->GetMarkedSdrObj();
1810 
1811 				// loop through all saved marks, check for equality
1812 				sal_Bool bFound = sal_False;
1813 				sal_uIntPtr nSavedCount = m_aMark.GetMarkCount();
1814 				for ( sal_uIntPtr j=0; j<nSavedCount && !bFound; ++j )
1815 				{
1816 					if ( m_aMark.GetMark( j )->GetMarkedSdrObj() == pCurrentMarked )
1817 						bFound = sal_True;
1818 				}
1819 
1820 				// did not find a current mark in the saved marks
1821 				if ( !bFound )
1822 					bMisMatch = sal_True;
1823 			}
1824 
1825 			if ( bMisMatch )
1826 			{
1827 				m_aMark.Clear();
1828 				_rRestoredMarkList = rCurrentList;
1829 				return;
1830 			}
1831 		}
1832 		// wichtig ist das auf die Objecte der markliste nicht zugegriffen wird
1833 		// da diese bereits zerstoert sein koennen
1834 		SdrPageView* pCurPageView = m_pView->GetSdrPageView();
1835 		SdrObjListIter aPageIter( *pPage );
1836 		sal_Bool bFound = sal_True;
1837 
1838 		// gibt es noch alle Objecte
1839 		sal_uIntPtr nCount = m_aMark.GetMarkCount();
1840 		for (sal_uIntPtr i = 0; i < nCount && bFound; i++)
1841 		{
1842 			SdrMark*   pMark = m_aMark.GetMark(i);
1843 			SdrObject* pObj  = pMark->GetMarkedSdrObj();
1844 			if (pObj->IsGroupObject())
1845 			{
1846 				SdrObjListIter aIter(*pObj->GetSubList());
1847 				while (aIter.IsMore() && bFound)
1848 					bFound = lcl_hasObject(aPageIter, aIter.Next());
1849 			}
1850 			else
1851 				bFound = lcl_hasObject(aPageIter, pObj);
1852 
1853 			bFound = bFound && pCurPageView == pMark->GetPageView();
1854 		}
1855 
1856 		if (bFound)
1857 		{
1858 			// Das LastObject auswerten
1859 			if (nCount) // Objecte jetzt Markieren
1860 			{
1861 				for (sal_uIntPtr i = 0; i < nCount; i++)
1862 				{
1863 					SdrMark* pMark = m_aMark.GetMark(i);
1864 					SdrObject* pObj = pMark->GetMarkedSdrObj();
1865 					if ( pObj->GetObjInventor() == FmFormInventor )
1866 						if ( !m_pView->IsObjMarked( pObj ) )
1867 							m_pView->MarkObj( pObj, pMark->GetPageView() );
1868 				}
1869 
1870 				_rRestoredMarkList = m_aMark;
1871 			}
1872 		}
1873 		m_aMark.Clear();
1874 	}
1875 }
1876 // -----------------------------------------------------------------------------
focusGained(const FocusEvent &)1877 void SAL_CALL FmXFormView::focusGained( const FocusEvent& /*e*/ ) throw (RuntimeException)
1878 {
1879 	if ( m_xWindow.is() && m_pView )
1880 	{
1881         m_pView->SetMoveOutside( sal_True, FmFormView::ImplAccess() );
1882 	}
1883 }
1884 // -----------------------------------------------------------------------------
focusLost(const FocusEvent &)1885 void SAL_CALL FmXFormView::focusLost( const FocusEvent& /*e*/ ) throw (RuntimeException)
1886 {
1887 	// when switch the focus outside the office the mark didn't change
1888 	// so we can not remove us as focus listener
1889 	if ( m_xWindow.is() && m_pView )
1890 	{
1891 		m_pView->SetMoveOutside( sal_False, FmFormView::ImplAccess() );
1892 	}
1893 }
1894 // -----------------------------------------------------------------------------
removeGridWindowListening()1895 void FmXFormView::removeGridWindowListening()
1896 {
1897 	if ( m_xWindow.is() )
1898 	{
1899 		m_xWindow->removeFocusListener(this);
1900 		if ( m_pView )
1901 		{
1902 			m_pView->SetMoveOutside( sal_False, FmFormView::ImplAccess() );
1903 		}
1904 		m_xWindow = NULL;
1905 	}
1906 }
1907 
1908 // -----------------------------------------------------------------------------
impl_getDocumentType() const1909 DocumentType FmXFormView::impl_getDocumentType() const
1910 {
1911     if ( GetFormShell() && GetFormShell()->GetImpl() )
1912         return GetFormShell()->GetImpl()->getDocumentType();
1913     return eUnknownDocumentType;
1914 }
1915