Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / svx / source / form / fmvwimp.cxx
blobaf99789063b695180aa861b53808376f1ecc45da
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "fmdocumentclassification.hxx"
22 #include "fmobj.hxx"
23 #include "fmpgeimp.hxx"
24 #include "fmprop.hrc"
25 #include "svx/fmresids.hrc"
26 #include "fmservs.hxx"
27 #include "fmshimp.hxx"
28 #include "svx/fmtools.hxx"
29 #include "fmundo.hxx"
30 #include "fmvwimp.hxx"
31 #include "formcontrolfactory.hxx"
32 #include "svx/sdrpaintwindow.hxx"
33 #include "svx/svditer.hxx"
34 #include "svx/dataaccessdescriptor.hxx"
35 #include "svx/dialmgr.hxx"
36 #include "svx/fmglob.hxx"
37 #include "svx/fmmodel.hxx"
38 #include "svx/fmpage.hxx"
39 #include "svx/fmshell.hxx"
40 #include "svx/fmview.hxx"
41 #include "svx/sdrpagewindow.hxx"
42 #include "svx/svdogrp.hxx"
43 #include "svx/svdpagv.hxx"
44 #include "svx/xmlexchg.hxx"
46 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
47 #include <com/sun/star/style/VerticalAlignment.hpp>
48 #include <com/sun/star/lang/XInitialization.hpp>
49 #include <com/sun/star/sdbc/XRowSet.hpp>
50 #include <com/sun/star/form/XLoadable.hpp>
51 #include <com/sun/star/awt/VisualEffect.hpp>
52 #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
53 #include <com/sun/star/util/XNumberFormats.hpp>
54 #include <com/sun/star/sdb/CommandType.hpp>
55 #include <com/sun/star/sdbc/DataType.hpp>
56 #include <com/sun/star/sdbc/ColumnValue.hpp>
57 #include <com/sun/star/form/FormComponentType.hpp>
58 #include <com/sun/star/form/FormButtonType.hpp>
59 #include <com/sun/star/form/XReset.hpp>
60 #include <com/sun/star/form/binding/XBindableValue.hpp>
61 #include <com/sun/star/form/binding/XValueBinding.hpp>
62 #include <com/sun/star/form/runtime/FormController.hpp>
63 #include <com/sun/star/form/submission/XSubmissionSupplier.hpp>
64 #include <com/sun/star/awt/XTabControllerModel.hpp>
65 #include <com/sun/star/awt/XControlContainer.hpp>
66 #include <com/sun/star/awt/XTabController.hpp>
67 #include <com/sun/star/container/XIndexAccess.hpp>
68 #include <com/sun/star/awt/XControl.hpp>
69 #include <com/sun/star/lang/XUnoTunnel.hpp>
70 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
71 #include <com/sun/star/sdbc/XPreparedStatement.hpp>
72 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
73 #include <com/sun/star/container/XContainer.hpp>
75 #include <comphelper/enumhelper.hxx>
76 #include <comphelper/extract.hxx>
77 #include <comphelper/namedvaluecollection.hxx>
78 #include <comphelper/numbers.hxx>
79 #include <comphelper/property.hxx>
80 #include <comphelper/processfactory.hxx>
81 #include <cppuhelper/exc_hlp.hxx>
82 #include <unotools/moduleoptions.hxx>
83 #include <tools/diagnose_ex.h>
84 #include <vcl/msgbox.hxx>
85 #include <vcl/stdtext.hxx>
86 #include <osl/mutex.hxx>
88 #include <algorithm>
90 using namespace ::comphelper;
91 using namespace ::svx;
92 using namespace ::svxform;
94 using namespace ::com::sun::star;
95 using ::com::sun::star::uno::Exception;
96 using ::com::sun::star::uno::RuntimeException;
97 using ::com::sun::star::uno::XInterface;
98 using ::com::sun::star::uno::Sequence;
99 using ::com::sun::star::uno::UNO_QUERY;
100 using ::com::sun::star::uno::UNO_QUERY_THROW;
101 using ::com::sun::star::uno::UNO_SET_THROW;
102 using ::com::sun::star::uno::Type;
103 using ::com::sun::star::uno::Reference;
104 using ::com::sun::star::uno::Any;
105 using ::com::sun::star::uno::makeAny;
106 using ::com::sun::star::uno::XComponentContext;
107 using ::com::sun::star::style::VerticalAlignment_MIDDLE;
108 using ::com::sun::star::form::FormButtonType_SUBMIT;
109 using ::com::sun::star::form::binding::XValueBinding;
110 using ::com::sun::star::form::binding::XBindableValue;
111 using ::com::sun::star::lang::XComponent;
112 using ::com::sun::star::container::XIndexAccess;
113 using ::com::sun::star::form::runtime::FormController;
114 using ::com::sun::star::form::runtime::XFormController;
115 using ::com::sun::star::script::XEventAttacherManager;
116 using ::com::sun::star::awt::XTabControllerModel;
117 using ::com::sun::star::container::XChild;
118 using ::com::sun::star::container::XEnumeration;
119 using ::com::sun::star::task::XInteractionHandler;
120 using ::com::sun::star::lang::XInitialization;
121 using ::com::sun::star::awt::XTabController;
122 using ::com::sun::star::lang::XUnoTunnel;
123 using ::com::sun::star::awt::XControlContainer;
124 using ::com::sun::star::awt::XControl;
125 using ::com::sun::star::form::XFormComponent;
126 using ::com::sun::star::form::XForm;
127 using ::com::sun::star::lang::IndexOutOfBoundsException;
128 using ::com::sun::star::lang::WrappedTargetException;
129 using ::com::sun::star::container::XContainer;
130 using ::com::sun::star::container::ContainerEvent;
131 using ::com::sun::star::lang::EventObject;
132 using ::com::sun::star::beans::NamedValue;
133 using ::com::sun::star::sdb::SQLErrorEvent;
134 using ::com::sun::star::sdbc::XRowSet;
135 using ::com::sun::star::beans::XPropertySet;
136 using ::com::sun::star::container::XElementAccess;
137 using ::com::sun::star::awt::XWindow;
138 using ::com::sun::star::awt::FocusEvent;
139 using ::com::sun::star::ui::dialogs::XExecutableDialog;
140 using ::com::sun::star::sdbc::XDataSource;
141 using ::com::sun::star::container::XIndexContainer;
142 using ::com::sun::star::sdbc::XConnection;
143 using ::com::sun::star::container::XNameAccess;
144 using ::com::sun::star::sdb::SQLContext;
145 using ::com::sun::star::sdbc::SQLWarning;
146 using ::com::sun::star::sdbc::SQLException;
147 using ::com::sun::star::util::XNumberFormatsSupplier;
148 using ::com::sun::star::util::XNumberFormats;
149 using ::com::sun::star::beans::XPropertySetInfo;
151 namespace FormComponentType = ::com::sun::star::form::FormComponentType;
152 namespace CommandType = ::com::sun::star::sdb::CommandType;
153 namespace DataType = ::com::sun::star::sdbc::DataType;
156 class FmXFormView::ObjectRemoveListener : public SfxListener
158 FmXFormView* m_pParent;
159 public:
160 ObjectRemoveListener( FmXFormView* pParent );
161 virtual void Notify( SfxBroadcaster& rBC, const SfxHint& rHint ) SAL_OVERRIDE;
166 FormViewPageWindowAdapter::FormViewPageWindowAdapter( const css::uno::Reference<css::uno::XComponentContext>& _rContext, const SdrPageWindow& _rWindow, FmXFormView* _pViewImpl )
167 : m_xControlContainer( _rWindow.GetControlContainer() ),
168 m_xContext( _rContext ),
169 m_pViewImpl( _pViewImpl ),
170 m_pWindow( dynamic_cast< Window* >( &_rWindow.GetPaintWindow().GetOutputDevice() ) )
173 // create an XFormController for every form
174 FmFormPage* pFormPage = dynamic_cast< FmFormPage* >( _rWindow.GetPageView().GetPage() );
175 DBG_ASSERT( pFormPage, "FormViewPageWindowAdapter::FormViewPageWindowAdapter: no FmFormPage found!" );
176 if ( pFormPage )
180 Reference< XIndexAccess > xForms( pFormPage->GetForms(), UNO_QUERY_THROW );
181 sal_uInt32 nLength = xForms->getCount();
182 for (sal_uInt32 i = 0; i < nLength; i++)
184 Reference< XForm > xForm( xForms->getByIndex(i), UNO_QUERY );
185 if ( xForm.is() )
186 setController( xForm, NULL );
189 catch (const Exception&)
191 DBG_UNHANDLED_EXCEPTION();
196 FormViewPageWindowAdapter::~FormViewPageWindowAdapter()
201 void FormViewPageWindowAdapter::dispose()
203 for ( ::std::vector< Reference< XFormController > >::const_iterator i = m_aControllerList.begin();
204 i != m_aControllerList.end();
210 Reference< XFormController > xController( *i, UNO_QUERY_THROW );
212 // detaching the events
213 Reference< XChild > xControllerModel( xController->getModel(), UNO_QUERY );
214 if ( xControllerModel.is() )
216 Reference< XEventAttacherManager > xEventManager( xControllerModel->getParent(), UNO_QUERY_THROW );
217 Reference< XInterface > xControllerNormalized( xController, UNO_QUERY_THROW );
218 xEventManager->detach( i - m_aControllerList.begin(), xControllerNormalized );
221 // dispose the formcontroller
222 xController->dispose();
224 catch (const Exception&)
226 DBG_UNHANDLED_EXCEPTION();
230 m_aControllerList.clear();
235 sal_Bool SAL_CALL FormViewPageWindowAdapter::hasElements(void) throw( RuntimeException, std::exception )
237 return getCount() != 0;
241 Type SAL_CALL FormViewPageWindowAdapter::getElementType(void) throw( RuntimeException, std::exception )
243 return cppu::UnoType<XFormController>::get();
246 // XEnumerationAccess
248 Reference< XEnumeration > SAL_CALL FormViewPageWindowAdapter::createEnumeration(void) throw( RuntimeException )
250 return new ::comphelper::OEnumerationByIndex(this);
253 // XIndexAccess
255 sal_Int32 SAL_CALL FormViewPageWindowAdapter::getCount(void) throw( RuntimeException, std::exception )
257 return m_aControllerList.size();
261 Any SAL_CALL FormViewPageWindowAdapter::getByIndex(sal_Int32 nIndex) throw( IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception )
263 if (nIndex < 0 ||
264 nIndex >= getCount())
265 throw IndexOutOfBoundsException();
267 Any aElement;
268 aElement <<= m_aControllerList[nIndex];
269 return aElement;
273 void SAL_CALL FormViewPageWindowAdapter::makeVisible( const Reference< XControl >& _Control ) throw (RuntimeException, std::exception)
275 SolarMutexGuard aSolarGuard;
277 Reference< XWindow > xWindow( _Control, UNO_QUERY );
278 if ( xWindow.is() && m_pViewImpl->getView() && m_pWindow )
280 awt::Rectangle aRect = xWindow->getPosSize();
281 ::Rectangle aNewRect( aRect.X, aRect.Y, aRect.X + aRect.Width, aRect.Y + aRect.Height );
282 aNewRect = m_pWindow->PixelToLogic( aNewRect );
283 m_pViewImpl->getView()->MakeVisible( aNewRect, *m_pWindow );
288 Reference< XFormController > getControllerSearchChildren( const Reference< XIndexAccess > & xIndex, const Reference< XTabControllerModel > & xModel)
290 if (xIndex.is() && xIndex->getCount())
292 Reference< XFormController > xController;
294 for (sal_Int32 n = xIndex->getCount(); n-- && !xController.is(); )
296 xIndex->getByIndex(n) >>= xController;
297 if ((XTabControllerModel*)xModel.get() == (XTabControllerModel*)xController->getModel().get())
298 return xController;
299 else
301 xController = getControllerSearchChildren(xController, xModel);
302 if ( xController.is() )
303 return xController;
307 return Reference< XFormController > ();
310 // Search the according controller
312 Reference< XFormController > FormViewPageWindowAdapter::getController( const Reference< XForm > & xForm ) const
314 Reference< XTabControllerModel > xModel(xForm, UNO_QUERY);
315 for (::std::vector< Reference< XFormController > >::const_iterator i = m_aControllerList.begin();
316 i != m_aControllerList.end(); ++i)
318 if ((XTabControllerModel*)(*i)->getModel().get() == (XTabControllerModel*)xModel.get())
319 return *i;
321 // the current-round controller isn't the right one. perhaps one of its children ?
322 Reference< XFormController > xChildSearch = getControllerSearchChildren(Reference< XIndexAccess > (*i, UNO_QUERY), xModel);
323 if (xChildSearch.is())
324 return xChildSearch;
326 return Reference< XFormController > ();
330 void FormViewPageWindowAdapter::setController(const Reference< XForm > & xForm, const Reference< XFormController >& _rxParentController )
332 DBG_ASSERT( xForm.is(), "FormViewPageWindowAdapter::setController: there should be a form!" );
333 Reference< XIndexAccess > xFormCps(xForm, UNO_QUERY);
334 if (!xFormCps.is())
335 return;
337 Reference< XTabControllerModel > xTabOrder(xForm, UNO_QUERY);
339 // create a form controller
340 Reference< XFormController > xController( FormController::create(m_xContext) );
342 Reference< XInteractionHandler > xHandler;
343 if ( _rxParentController.is() )
344 xHandler = _rxParentController->getInteractionHandler();
345 else
347 // TODO: should we create a default handler? Not really necessary, since the
348 // FormController itself has a default fallback
350 if ( xHandler.is() )
351 xController->setInteractionHandler( xHandler );
353 xController->setContext( this );
355 xController->setModel( xTabOrder );
356 xController->setContainer( m_xControlContainer );
357 xController->activateTabOrder();
358 xController->addActivateListener( m_pViewImpl );
360 if ( _rxParentController.is() )
361 _rxParentController->addChildController( xController );
362 else
364 m_aControllerList.push_back(xController);
366 xController->setParent( *this );
368 // attaching the events
369 Reference< XEventAttacherManager > xEventManager( xForm->getParent(), UNO_QUERY );
370 xEventManager->attach(m_aControllerList.size() - 1, xController, makeAny(xController) );
373 // jetzt die Subforms durchgehen
374 sal_uInt32 nLength = xFormCps->getCount();
375 Reference< XForm > xSubForm;
376 for (sal_uInt32 i = 0; i < nLength; i++)
378 if ( xFormCps->getByIndex(i) >>= xSubForm )
379 setController( xSubForm, xController );
384 void FormViewPageWindowAdapter::updateTabOrder( const Reference< XForm >& _rxForm )
386 OSL_PRECOND( _rxForm.is(), "FormViewPageWindowAdapter::updateTabOrder: illegal argument!" );
387 if ( !_rxForm.is() )
388 return;
392 Reference< XTabController > xTabCtrl( getController( _rxForm ).get() );
393 if ( xTabCtrl.is() )
394 { // if there already is a TabController for this form, then delegate the "updateTabOrder" request
395 xTabCtrl->activateTabOrder();
397 else
398 { // otherwise, create a TabController
400 // if it's a sub form, then we must ensure there exist TabControllers
401 // for all its ancestors, too
402 Reference< XForm > xParentForm( _rxForm->getParent(), UNO_QUERY );
403 // there is a parent form -> look for the respective controller
404 Reference< XFormController > xParentController;
405 if ( xParentForm.is() )
406 xParentController.set( getController( xParentForm ), UNO_QUERY );
408 setController( _rxForm, xParentController );
411 catch (const Exception&)
413 DBG_UNHANDLED_EXCEPTION();
418 FmXFormView::FmXFormView(FmFormView* _pView )
419 :m_pMarkedGrid(NULL)
420 ,m_pView(_pView)
421 ,m_nActivationEvent(0)
422 ,m_nErrorMessageEvent( 0 )
423 ,m_nAutoFocusEvent( 0 )
424 ,m_nControlWizardEvent( 0 )
425 ,m_pWatchStoredList( NULL )
426 ,m_bFirstActivation( true )
427 ,m_isTabOrderUpdateSuspended( false )
432 void FmXFormView::cancelEvents()
434 if ( m_nActivationEvent )
436 Application::RemoveUserEvent( m_nActivationEvent );
437 m_nActivationEvent = 0;
440 if ( m_nErrorMessageEvent )
442 Application::RemoveUserEvent( m_nErrorMessageEvent );
443 m_nErrorMessageEvent = 0;
446 if ( m_nAutoFocusEvent )
448 Application::RemoveUserEvent( m_nAutoFocusEvent );
449 m_nAutoFocusEvent = 0;
452 if ( m_nControlWizardEvent )
454 Application::RemoveUserEvent( m_nControlWizardEvent );
455 m_nControlWizardEvent = 0;
460 void FmXFormView::notifyViewDying( )
462 DBG_ASSERT( m_pView, "FmXFormView::notifyViewDying: my view already died!" );
463 m_pView = NULL;
464 cancelEvents();
468 FmXFormView::~FmXFormView()
470 DBG_ASSERT( m_aPageWindowAdapters.empty(), "FmXFormView::~FmXFormView: Window list not empty!" );
471 if ( !m_aPageWindowAdapters.empty() )
473 for ( PageWindowAdapterList::const_iterator loop = m_aPageWindowAdapters.begin();
474 loop != m_aPageWindowAdapters.end();
475 ++loop
478 (*loop)->dispose();
482 cancelEvents();
484 delete m_pWatchStoredList;
485 m_pWatchStoredList = NULL;
488 // EventListener
490 void SAL_CALL FmXFormView::disposing(const EventObject& Source) throw( RuntimeException, std::exception )
492 if ( m_xWindow.is() && Source.Source == m_xWindow )
493 removeGridWindowListening();
496 // XFormControllerListener
498 void SAL_CALL FmXFormView::formActivated(const EventObject& rEvent) throw( RuntimeException, std::exception )
500 if ( m_pView && m_pView->GetFormShell() && m_pView->GetFormShell()->GetImpl() )
501 m_pView->GetFormShell()->GetImpl()->formActivated( rEvent );
505 void SAL_CALL FmXFormView::formDeactivated(const EventObject& rEvent) throw( RuntimeException, std::exception )
507 if ( m_pView && m_pView->GetFormShell() && m_pView->GetFormShell()->GetImpl() )
508 m_pView->GetFormShell()->GetImpl()->formDeactivated( rEvent );
511 // XContainerListener
513 void SAL_CALL FmXFormView::elementInserted(const ContainerEvent& evt) throw( RuntimeException, std::exception )
517 Reference< XControlContainer > xControlContainer( evt.Source, UNO_QUERY_THROW );
518 Reference< XControl > xControl( evt.Element, UNO_QUERY_THROW );
519 Reference< XFormComponent > xControlModel( xControl->getModel(), UNO_QUERY_THROW );
520 Reference< XForm > xForm( xControlModel->getParent(), UNO_QUERY_THROW );
522 if ( m_isTabOrderUpdateSuspended )
524 // remember the container and the control, so we can update the tab order on resumeTabOrderUpdate
525 m_aNeedTabOrderUpdate[ xControlContainer ].insert( xForm );
527 else
529 PFormViewPageWindowAdapter pAdapter = findWindow( xControlContainer );
530 if ( pAdapter.is() )
531 pAdapter->updateTabOrder( xForm );
534 catch (const Exception&)
536 DBG_UNHANDLED_EXCEPTION();
541 void SAL_CALL FmXFormView::elementReplaced(const ContainerEvent& evt) throw( RuntimeException, std::exception )
543 elementInserted(evt);
547 void SAL_CALL FmXFormView::elementRemoved(const ContainerEvent& /*evt*/) throw( RuntimeException, std::exception )
552 PFormViewPageWindowAdapter FmXFormView::findWindow( const Reference< XControlContainer >& _rxCC ) const
554 for ( PageWindowAdapterList::const_iterator i = m_aPageWindowAdapters.begin();
555 i != m_aPageWindowAdapters.end();
559 if ( _rxCC == (*i)->getControlContainer() )
560 return *i;
562 return NULL;
566 void FmXFormView::addWindow(const SdrPageWindow& rWindow)
568 FmFormPage* pFormPage = PTR_CAST( FmFormPage, rWindow.GetPageView().GetPage() );
569 if ( !pFormPage )
570 return;
572 Reference< XControlContainer > xCC = rWindow.GetControlContainer();
573 if ( xCC.is()
574 && ( !findWindow( xCC ).is() )
577 PFormViewPageWindowAdapter pAdapter = new FormViewPageWindowAdapter( comphelper::getProcessComponentContext(), rWindow, this );
578 m_aPageWindowAdapters.push_back( pAdapter );
580 // Am ControlContainer horchen um Aenderungen mitzbekommen
581 Reference< XContainer > xContainer( xCC, UNO_QUERY );
582 if ( xContainer.is() )
583 xContainer->addContainerListener( this );
588 void FmXFormView::removeWindow( const Reference< XControlContainer >& _rxCC )
590 // Wird gerufen, wenn
591 // - in den Design-Modus geschaltet wird
592 // - ein Window geloescht wird, waehrend man im Design-Modus ist
593 // - der Control-Container fuer ein Window entfernt wird, waehrend
594 // der aktive Modus eingeschaltet ist.
596 for ( PageWindowAdapterList::iterator i = m_aPageWindowAdapters.begin();
597 i != m_aPageWindowAdapters.end();
601 if ( _rxCC != (*i)->getControlContainer() )
602 continue;
604 Reference< XContainer > xContainer( _rxCC, UNO_QUERY );
605 if ( xContainer.is() )
606 xContainer->removeContainerListener( this );
608 (*i)->dispose();
609 m_aPageWindowAdapters.erase( i );
610 break;
615 void FmXFormView::displayAsyncErrorMessage( const SQLErrorEvent& _rEvent )
617 DBG_ASSERT( 0 == m_nErrorMessageEvent, "FmXFormView::displayAsyncErrorMessage: not too fast, please!" );
618 // This should not happen - usually, the PostUserEvent is faster than any possible user
619 // interaction which could trigger a new error. If it happens, we need a queue for the events.
620 m_aAsyncError = _rEvent;
621 m_nErrorMessageEvent = Application::PostUserEvent( LINK( this, FmXFormView, OnDelayedErrorMessage ) );
625 IMPL_LINK(FmXFormView, OnDelayedErrorMessage, void*, /*EMPTYTAG*/)
627 m_nErrorMessageEvent = 0;
628 displayException( m_aAsyncError );
629 return 0L;
633 void FmXFormView::onFirstViewActivation( const FmFormModel* _pDocModel )
635 if ( _pDocModel && _pDocModel->GetAutoControlFocus() )
636 m_nAutoFocusEvent = Application::PostUserEvent( LINK( this, FmXFormView, OnAutoFocus ) );
640 void FmXFormView::suspendTabOrderUpdate()
642 OSL_ENSURE( !m_isTabOrderUpdateSuspended, "FmXFormView::suspendTabOrderUpdate: nesting not allowed!" );
643 m_isTabOrderUpdateSuspended = true;
647 void FmXFormView::resumeTabOrderUpdate()
649 OSL_ENSURE( m_isTabOrderUpdateSuspended, "FmXFormView::resumeTabOrderUpdate: not suspended!" );
650 m_isTabOrderUpdateSuspended = false;
652 // update the tab orders for all components which were collected since the suspendTabOrderUpdate call.
653 for ( MapControlContainerToSetOfForms::const_iterator container = m_aNeedTabOrderUpdate.begin();
654 container != m_aNeedTabOrderUpdate.end();
655 ++container
658 PFormViewPageWindowAdapter pAdapter = findWindow( container->first );
659 if ( !pAdapter.is() )
660 continue;
662 for ( SetOfForms::const_iterator form = container->second.begin();
663 form != container->second.end();
664 ++form
667 pAdapter->updateTabOrder( *form );
670 m_aNeedTabOrderUpdate.clear();
674 IMPL_LINK(FmXFormView, OnActivate, void*, /*EMPTYTAG*/)
676 m_nActivationEvent = 0;
678 if ( !m_pView )
680 OSL_FAIL( "FmXFormView::OnActivate: well .... seems we have a timing problem (the view already died)!" );
681 return 0;
684 // setting the controller to activate
685 if (m_pView->GetFormShell() && m_pView->GetActualOutDev() && m_pView->GetActualOutDev()->GetOutDevType() == OUTDEV_WINDOW)
687 Window* pWindow = const_cast<Window*>(static_cast<const Window*>(m_pView->GetActualOutDev()));
688 PFormViewPageWindowAdapter pAdapter = m_aPageWindowAdapters.empty() ? NULL : m_aPageWindowAdapters[0];
689 for ( PageWindowAdapterList::const_iterator i = m_aPageWindowAdapters.begin();
690 i != m_aPageWindowAdapters.end();
694 if ( pWindow == (*i)->getWindow() )
695 pAdapter =*i;
698 if ( pAdapter.get() )
700 for ( ::std::vector< Reference< XFormController > >::const_iterator i = pAdapter->GetList().begin();
701 i != pAdapter->GetList().end();
705 const Reference< XFormController > & xController = *i;
706 if ( !xController.is() )
707 continue;
709 // only database forms are to be activated
710 Reference< XRowSet > xForm(xController->getModel(), UNO_QUERY);
711 if ( !xForm.is() || !OStaticDataAccessTools().getRowSetConnection( xForm ).is() )
712 continue;
714 Reference< XPropertySet > xFormSet( xForm, UNO_QUERY );
715 if ( !xFormSet.is() )
717 SAL_WARN( "svx.form", "FmXFormView::OnActivate: a form which does not have properties?" );
718 continue;
721 const OUString aSource = ::comphelper::getString( xFormSet->getPropertyValue( FM_PROP_COMMAND ) );
722 if ( !aSource.isEmpty() )
724 FmXFormShell* pShImpl = m_pView->GetFormShell()->GetImpl();
725 if ( pShImpl )
726 pShImpl->setActiveController( xController );
727 break;
732 return 0;
736 void FmXFormView::Activate(bool bSync)
738 if (m_nActivationEvent)
740 Application::RemoveUserEvent(m_nActivationEvent);
741 m_nActivationEvent = 0;
744 if (bSync)
746 LINK(this,FmXFormView,OnActivate).Call(NULL);
748 else
749 m_nActivationEvent = Application::PostUserEvent(LINK(this,FmXFormView,OnActivate));
753 void FmXFormView::Deactivate(bool bDeactivateController)
755 if (m_nActivationEvent)
757 Application::RemoveUserEvent(m_nActivationEvent);
758 m_nActivationEvent = 0;
761 FmXFormShell* pShImpl = m_pView->GetFormShell() ? m_pView->GetFormShell()->GetImpl() : NULL;
762 if (pShImpl && bDeactivateController)
763 pShImpl->setActiveController( NULL );
767 FmFormShell* FmXFormView::GetFormShell() const
769 return m_pView ? m_pView->GetFormShell() : NULL;
772 void FmXFormView::AutoFocus( bool _bSync )
774 if (m_nAutoFocusEvent)
775 Application::RemoveUserEvent(m_nAutoFocusEvent);
777 if ( _bSync )
778 OnAutoFocus( NULL );
779 else
780 m_nAutoFocusEvent = Application::PostUserEvent(LINK(this, FmXFormView, OnAutoFocus));
784 bool FmXFormView::isFocusable( const Reference< XControl >& i_rControl )
786 if ( !i_rControl.is() )
787 return false;
791 Reference< XPropertySet > xModelProps( i_rControl->getModel(), UNO_QUERY_THROW );
793 // only enabled controls are allowed to participate
794 bool bEnabled = false;
795 OSL_VERIFY( xModelProps->getPropertyValue( FM_PROP_ENABLED ) >>= bEnabled );
796 if ( !bEnabled )
797 return false;
799 // check the class id of the control model
800 sal_Int16 nClassId = FormComponentType::CONTROL;
801 OSL_VERIFY( xModelProps->getPropertyValue( FM_PROP_CLASSID ) >>= nClassId );
803 // controls which are not focussable
804 if ( ( FormComponentType::CONTROL != nClassId )
805 && ( FormComponentType::IMAGEBUTTON != nClassId )
806 && ( FormComponentType::GROUPBOX != nClassId )
807 && ( FormComponentType::FIXEDTEXT != nClassId )
808 && ( FormComponentType::HIDDENCONTROL != nClassId )
809 && ( FormComponentType::IMAGECONTROL != nClassId )
810 && ( FormComponentType::SCROLLBAR != nClassId )
811 && ( FormComponentType::SPINBUTTON!= nClassId )
814 return true;
817 catch (const Exception&)
819 DBG_UNHANDLED_EXCEPTION();
821 return false;
825 static Reference< XControl > lcl_firstFocussableControl( const Sequence< Reference< XControl > >& _rControls )
827 Reference< XControl > xReturn;
829 // loop through all the controls
830 const Reference< XControl >* pControls = _rControls.getConstArray();
831 const Reference< XControl >* pControlsEnd = _rControls.getConstArray() + _rControls.getLength();
832 for ( ; pControls != pControlsEnd; ++pControls )
834 if ( !pControls->is() )
835 continue;
837 if ( FmXFormView::isFocusable( *pControls ) )
839 xReturn = *pControls;
840 break;
844 if ( !xReturn.is() && _rControls.getLength() )
845 xReturn = _rControls[0];
847 return xReturn;
851 namespace
854 void lcl_ensureControlsOfFormExist_nothrow( const SdrPage& _rPage, const SdrView& _rView, const Window& _rWindow, const Reference< XForm >& _rxForm )
858 Reference< XInterface > xNormalizedForm( _rxForm, UNO_QUERY_THROW );
860 SdrObjListIter aSdrObjectLoop( _rPage, IM_DEEPNOGROUPS );
861 while ( aSdrObjectLoop.IsMore() )
863 FmFormObj* pFormObject = FmFormObj::GetFormObject( aSdrObjectLoop.Next() );
864 if ( !pFormObject )
865 continue;
867 Reference< XChild > xModel( pFormObject->GetUnoControlModel(), UNO_QUERY_THROW );
868 Reference< XInterface > xModelParent( xModel->getParent(), UNO_QUERY_THROW );
870 if ( xNormalizedForm.get() != xModelParent.get() )
871 continue;
873 pFormObject->GetUnoControl( _rView, _rWindow );
876 catch (const Exception&)
878 DBG_UNHANDLED_EXCEPTION();
884 Reference< XFormController > FmXFormView::getFormController( const Reference< XForm >& _rxForm, const OutputDevice& _rDevice ) const
886 Reference< XFormController > xController;
888 for ( PageWindowAdapterList::const_iterator pos = m_aPageWindowAdapters.begin();
889 pos != m_aPageWindowAdapters.end();
890 ++pos
893 const PFormViewPageWindowAdapter pAdapter( *pos );
894 if ( !pAdapter.get() )
896 SAL_WARN( "svx.form", "FmXFormView::getFormController: invalid page window adapter!" );
897 continue;
900 if ( pAdapter->getWindow() != &_rDevice )
901 // wrong device
902 continue;
904 xController = pAdapter->getController( _rxForm );
905 if ( xController.is() )
906 break;
908 return xController;
912 IMPL_LINK(FmXFormView, OnAutoFocus, void*, /*EMPTYTAG*/)
914 m_nAutoFocusEvent = 0;
916 // go to the first form of our page, examine it's TabController, go to it's first (in terms of the tab order)
917 // control, give it the focus
922 SdrPageView *pPageView = m_pView ? m_pView->GetSdrPageView() : NULL;
923 SdrPage *pSdrPage = pPageView ? pPageView->GetPage() : NULL;
924 // get the forms collection of the page we belong to
925 FmFormPage* pPage = PTR_CAST( FmFormPage, pSdrPage );
926 Reference< XIndexAccess > xForms( pPage ? Reference< XIndexAccess >( pPage->GetForms(), UNO_QUERY ) : Reference< XIndexAccess >() );
928 const PFormViewPageWindowAdapter pAdapter = m_aPageWindowAdapters.empty() ? NULL : m_aPageWindowAdapters[0];
929 const Window* pWindow = pAdapter.get() ? pAdapter->getWindow() : NULL;
931 ENSURE_OR_RETURN( xForms.is() && pWindow, "FmXFormView::OnAutoFocus: could not collect all essentials!", 0L );
935 // go for the tab controller of the first form
936 if ( !xForms->getCount() )
937 break;
938 Reference< XForm > xForm( xForms->getByIndex( 0 ), UNO_QUERY_THROW );
939 Reference< XTabController > xTabController( pAdapter->getController( xForm ), UNO_QUERY_THROW );
941 // go for the first control of the controller
942 Sequence< Reference< XControl > > aControls( xTabController->getControls() );
943 if ( aControls.getLength() == 0 )
945 Reference< XElementAccess > xFormElementAccess( xForm, UNO_QUERY_THROW );
946 if (xFormElementAccess->hasElements() && pPage && m_pView)
948 // there are control models in the form, but no controls, yet.
949 // Well, since some time controls are created on demand only. In particular,
950 // they're normally created when they're first painted.
951 // Unfortunately, the FormController does not have any way to
952 // trigger the creation itself, so we must hack this ...
953 lcl_ensureControlsOfFormExist_nothrow( *pPage, *m_pView, *pWindow, xForm );
954 aControls = xTabController->getControls();
955 OSL_ENSURE( aControls.getLength(), "FmXFormView::OnAutoFocus: no controls at all!" );
959 // set the focus to this first control
960 Reference< XWindow > xControlWindow( lcl_firstFocussableControl( aControls ), UNO_QUERY );
961 if ( !xControlWindow.is() )
962 break;
964 xControlWindow->setFocus();
966 // ensure that the control is visible
967 // 80210 - 12/07/00 - FS
968 const Window* pCurrentWindow = dynamic_cast< const Window* >( m_pView->GetActualOutDev() );
969 if ( pCurrentWindow )
971 awt::Rectangle aRect = xControlWindow->getPosSize();
972 ::Rectangle aNonUnoRect( aRect.X, aRect.Y, aRect.X + aRect.Width, aRect.Y + aRect.Height );
973 m_pView->MakeVisible( pCurrentWindow->PixelToLogic( aNonUnoRect ), *const_cast< Window* >( pCurrentWindow ) );
976 catch (const Exception&)
978 DBG_UNHANDLED_EXCEPTION();
981 } // do
982 while ( false );
984 return 1L;
988 void FmXFormView::onCreatedFormObject( FmFormObj& _rFormObject )
990 FmFormShell* pShell = m_pView ? m_pView->GetFormShell() : NULL;
991 FmXFormShell* pShellImpl = pShell ? pShell->GetImpl() : NULL;
992 OSL_ENSURE( pShellImpl, "FmXFormView::onCreatedFormObject: no form shell!" );
993 if ( !pShellImpl )
994 return;
996 // it is valid that the form shell's forms collection is not initialized, yet
997 pShellImpl->UpdateForms( true );
999 m_xLastCreatedControlModel.set( _rFormObject.GetUnoControlModel(), UNO_QUERY );
1000 if ( !m_xLastCreatedControlModel.is() )
1001 return;
1003 // some initial property defaults
1004 FormControlFactory aControlFactory;
1005 aControlFactory.initializeControlModel( pShellImpl->getDocumentType(), _rFormObject );
1007 if ( !pShellImpl->GetWizardUsing() )
1008 return;
1010 // #i31958# don't call wizards in XForms mode
1011 if ( pShellImpl->isEnhancedForm() )
1012 return;
1014 // #i46898# no wizards if there is no Base installed - currently, all wizards are
1015 // database related
1016 if ( !SvtModuleOptions().IsModuleInstalled( SvtModuleOptions::E_SDATABASE ) )
1017 return;
1019 if ( m_nControlWizardEvent )
1020 Application::RemoveUserEvent( m_nControlWizardEvent );
1021 m_nControlWizardEvent = Application::PostUserEvent( LINK( this, FmXFormView, OnStartControlWizard ) );
1025 IMPL_LINK( FmXFormView, OnStartControlWizard, void*, /**/ )
1027 m_nControlWizardEvent = 0;
1028 OSL_PRECOND( m_xLastCreatedControlModel.is(), "FmXFormView::OnStartControlWizard: illegal call!" );
1029 if ( !m_xLastCreatedControlModel.is() )
1030 return 0L;
1032 sal_Int16 nClassId = FormComponentType::CONTROL;
1035 OSL_VERIFY( m_xLastCreatedControlModel->getPropertyValue( FM_PROP_CLASSID ) >>= nClassId );
1037 catch (const Exception&)
1039 DBG_UNHANDLED_EXCEPTION();
1042 const sal_Char* pWizardAsciiName = NULL;
1043 switch ( nClassId )
1045 case FormComponentType::GRIDCONTROL:
1046 pWizardAsciiName = "com.sun.star.sdb.GridControlAutoPilot";
1047 break;
1048 case FormComponentType::LISTBOX:
1049 case FormComponentType::COMBOBOX:
1050 pWizardAsciiName = "com.sun.star.sdb.ListComboBoxAutoPilot";
1051 break;
1052 case FormComponentType::GROUPBOX:
1053 pWizardAsciiName = "com.sun.star.sdb.GroupBoxAutoPilot";
1054 break;
1057 if ( pWizardAsciiName )
1059 // build the argument list
1060 ::comphelper::NamedValueCollection aWizardArgs;
1061 aWizardArgs.put( "ObjectModel", m_xLastCreatedControlModel );
1063 // create the wizard object
1064 Reference< XExecutableDialog > xWizard;
1067 Reference<XComponentContext> xContext = comphelper::getProcessComponentContext();
1068 xWizard.set( xContext->getServiceManager()->createInstanceWithArgumentsAndContext( OUString::createFromAscii(pWizardAsciiName), aWizardArgs.getWrappedPropertyValues(), xContext ), UNO_QUERY);;
1070 catch (const Exception&)
1072 DBG_UNHANDLED_EXCEPTION();
1075 if ( !xWizard.is() )
1077 ShowServiceNotAvailableError( NULL, OUString::createFromAscii(pWizardAsciiName), true );
1079 else
1081 // execute the wizard
1084 xWizard->execute();
1086 catch (const Exception&)
1088 DBG_UNHANDLED_EXCEPTION();
1093 m_xLastCreatedControlModel.clear();
1094 return 1L;
1098 namespace
1100 void lcl_insertIntoFormComponentHierarchy_throw( const FmFormView& _rView, const SdrUnoObj& _rSdrObj,
1101 const Reference< XDataSource >& _rxDataSource = NULL, const OUString& _rDataSourceName = OUString(),
1102 const OUString& _rCommand = OUString(), const sal_Int32 _nCommandType = -1 )
1104 FmFormPage& rPage = static_cast< FmFormPage& >( *_rView.GetSdrPageView()->GetPage() );
1106 Reference< XFormComponent > xFormComponent( _rSdrObj.GetUnoControlModel(), UNO_QUERY_THROW );
1107 Reference< XForm > xTargetForm(
1108 rPage.GetImpl().findPlaceInFormComponentHierarchy( xFormComponent, _rxDataSource, _rDataSourceName, _rCommand, _nCommandType ),
1109 UNO_SET_THROW );
1111 rPage.GetImpl().setUniqueName( xFormComponent, xTargetForm );
1113 Reference< XIndexContainer > xFormAsContainer( xTargetForm, UNO_QUERY_THROW );
1114 xFormAsContainer->insertByIndex( xFormAsContainer->getCount(), makeAny( xFormComponent ) );
1119 SdrObject* FmXFormView::implCreateFieldControl( const ::svx::ODataAccessDescriptor& _rColumnDescriptor )
1121 // not if we're in design mode
1122 if ( !m_pView->IsDesignMode() )
1123 return NULL;
1125 OUString sCommand, sFieldName;
1126 sal_Int32 nCommandType = CommandType::COMMAND;
1127 SharedConnection xConnection;
1129 OUString sDataSource = _rColumnDescriptor.getDataSource();
1130 _rColumnDescriptor[ daCommand ] >>= sCommand;
1131 _rColumnDescriptor[ daColumnName ] >>= sFieldName;
1132 _rColumnDescriptor[ daCommandType ] >>= nCommandType;
1134 Reference< XConnection > xExternalConnection;
1135 _rColumnDescriptor[ daConnection ] >>= xExternalConnection;
1136 xConnection.reset( xExternalConnection, SharedConnection::NoTakeOwnership );
1139 if ( sCommand.isEmpty()
1140 || sFieldName.isEmpty()
1141 || ( sDataSource.isEmpty()
1142 && !xConnection.is()
1146 OSL_FAIL( "FmXFormView::implCreateFieldControl: nonsense!" );
1149 Reference< XDataSource > xDataSource;
1150 SQLErrorEvent aError;
1153 if ( xConnection.is() && !xDataSource.is() && sDataSource.isEmpty() )
1155 Reference< XChild > xChild( xConnection, UNO_QUERY );
1156 if ( xChild.is() )
1157 xDataSource = xDataSource.query( xChild->getParent() );
1160 // obtain the data source
1161 if ( !xDataSource.is() )
1162 xDataSource = OStaticDataAccessTools().getDataSource( sDataSource, comphelper::getProcessComponentContext() );
1164 // and the connection, if necessary
1165 if ( !xConnection.is() )
1166 xConnection.reset( OStaticDataAccessTools().getConnection_withFeedback(
1167 sDataSource,
1168 OUString(),
1169 OUString(),
1170 comphelper::getProcessComponentContext()
1171 ) );
1173 catch (const SQLException&)
1175 aError.Reason = ::cppu::getCaughtException();
1177 catch (const Exception& )
1179 /* will be asserted below */
1181 if (aError.Reason.hasValue())
1183 displayAsyncErrorMessage( aError );
1184 return NULL;
1187 // need a data source and a connection here
1188 if (!xDataSource.is() || !xConnection.is())
1190 OSL_FAIL("FmXFormView::implCreateFieldControl : could not retrieve the data source or the connection!");
1191 return NULL;
1194 OStaticDataAccessTools aDBATools;
1195 Reference< XComponent > xKeepFieldsAlive;
1196 // go
1199 // determine the table/query field which we should create a control for
1200 Reference< XPropertySet > xField;
1202 Reference< XNameAccess > xFields = aDBATools.getFieldsByCommandDescriptor(
1203 xConnection, nCommandType, sCommand, xKeepFieldsAlive );
1205 if (xFields.is() && xFields->hasByName(sFieldName))
1206 xFields->getByName(sFieldName) >>= xField;
1207 if ( !xField.is() )
1208 return NULL;
1210 Reference< XNumberFormatsSupplier > xSupplier( aDBATools.getNumberFormats( xConnection, false ), UNO_SET_THROW );
1211 Reference< XNumberFormats > xNumberFormats( xSupplier->getNumberFormats(), UNO_SET_THROW );
1213 OUString sLabelPostfix;
1216 // nur fuer Textgroesse
1217 OutputDevice* pOutDev = NULL;
1218 if (m_pView->GetActualOutDev() && m_pView->GetActualOutDev()->GetOutDevType() == OUTDEV_WINDOW)
1219 pOutDev = const_cast<OutputDevice*>(m_pView->GetActualOutDev());
1220 else
1221 {// OutDev suchen
1222 SdrPageView* pPageView = m_pView->GetSdrPageView();
1223 if( pPageView && !pOutDev )
1225 // const SdrPageViewWinList& rWinList = pPageView->GetWinList();
1226 // const SdrPageViewWindows& rPageViewWindows = pPageView->GetPageViewWindows();
1228 for( sal_uInt32 i = 0L; i < pPageView->PageWindowCount(); i++ )
1230 const SdrPageWindow& rPageWindow = *pPageView->GetPageWindow(i);
1232 if( rPageWindow.GetPaintWindow().OutputToWindow())
1234 pOutDev = &rPageWindow.GetPaintWindow().GetOutputDevice();
1235 break;
1241 if ( !pOutDev )
1242 return NULL;
1244 sal_Int32 nDataType = ::comphelper::getINT32(xField->getPropertyValue(FM_PROP_FIELDTYPE));
1245 if ((DataType::BINARY == nDataType) || (DataType::VARBINARY == nDataType))
1246 return NULL;
1249 // determine the control type by examining the data type of the bound column
1250 sal_uInt16 nOBJID = 0;
1251 bool bDateNTimeField = false;
1253 bool bIsCurrency = false;
1254 if (::comphelper::hasProperty(FM_PROP_ISCURRENCY, xField))
1255 bIsCurrency = ::comphelper::getBOOL(xField->getPropertyValue(FM_PROP_ISCURRENCY));
1257 if (bIsCurrency)
1258 nOBJID = OBJ_FM_CURRENCYFIELD;
1259 else
1260 switch (nDataType)
1262 case DataType::BLOB:
1263 case DataType::LONGVARBINARY:
1264 nOBJID = OBJ_FM_IMAGECONTROL;
1265 break;
1266 case DataType::LONGVARCHAR:
1267 case DataType::CLOB:
1268 nOBJID = OBJ_FM_EDIT;
1269 break;
1270 case DataType::BINARY:
1271 case DataType::VARBINARY:
1272 return NULL;
1273 case DataType::BIT:
1274 case DataType::BOOLEAN:
1275 nOBJID = OBJ_FM_CHECKBOX;
1276 break;
1277 case DataType::TINYINT:
1278 case DataType::SMALLINT:
1279 case DataType::INTEGER:
1280 nOBJID = OBJ_FM_NUMERICFIELD;
1281 break;
1282 case DataType::REAL:
1283 case DataType::DOUBLE:
1284 case DataType::NUMERIC:
1285 case DataType::DECIMAL:
1286 nOBJID = OBJ_FM_FORMATTEDFIELD;
1287 break;
1288 case DataType::TIMESTAMP:
1289 bDateNTimeField = true;
1290 sLabelPostfix = SVX_RESSTR(RID_STR_POSTFIX_DATE);
1291 // DON'T break !
1292 case DataType::DATE:
1293 nOBJID = OBJ_FM_DATEFIELD;
1294 break;
1295 case DataType::TIME:
1296 nOBJID = OBJ_FM_TIMEFIELD;
1297 break;
1298 case DataType::CHAR:
1299 case DataType::VARCHAR:
1300 default:
1301 nOBJID = OBJ_FM_EDIT;
1302 break;
1304 if (!nOBJID)
1305 return NULL;
1307 SdrUnoObj* pLabel( NULL );
1308 SdrUnoObj* pControl( NULL );
1309 if ( !createControlLabelPair( *pOutDev, 0, 0, xField, xNumberFormats, nOBJID, sLabelPostfix,
1310 pLabel, pControl, xDataSource, sDataSource, sCommand, nCommandType )
1313 return NULL;
1317 // group objects
1318 bool bCheckbox = ( OBJ_FM_CHECKBOX == nOBJID );
1319 OSL_ENSURE( !bCheckbox || !pLabel, "FmXFormView::implCreateFieldControl: why was there a label created for a check box?" );
1320 if ( bCheckbox )
1321 return pControl;
1323 SdrObjGroup* pGroup = new SdrObjGroup();
1324 SdrObjList* pObjList = pGroup->GetSubList();
1325 pObjList->InsertObject( pLabel );
1326 pObjList->InsertObject( pControl );
1328 if ( bDateNTimeField )
1329 { // so far we created a date field only, but we also need a time field
1330 pLabel = pControl = NULL;
1331 if ( createControlLabelPair( *pOutDev, 0, 1000, xField, xNumberFormats, OBJ_FM_TIMEFIELD,
1332 SVX_RESSTR(RID_STR_POSTFIX_TIME), pLabel, pControl,
1333 xDataSource, sDataSource, sCommand, nCommandType )
1336 pObjList->InsertObject( pLabel );
1337 pObjList->InsertObject( pControl );
1341 return pGroup; // und fertig
1343 catch (const Exception&)
1345 DBG_UNHANDLED_EXCEPTION();
1349 return NULL;
1353 SdrObject* FmXFormView::implCreateXFormsControl( const ::svx::OXFormsDescriptor &_rDesc )
1355 // not if we're in design mode
1356 if ( !m_pView->IsDesignMode() )
1357 return NULL;
1359 Reference< XComponent > xKeepFieldsAlive;
1361 // go
1364 // determine the table/query field which we should create a control for
1365 Reference< XNumberFormats > xNumberFormats;
1366 OUString sLabelPostfix = _rDesc.szName;
1369 // nur fuer Textgroesse
1370 OutputDevice* pOutDev = NULL;
1371 if (m_pView->GetActualOutDev() && m_pView->GetActualOutDev()->GetOutDevType() == OUTDEV_WINDOW)
1372 pOutDev = const_cast<OutputDevice*>(m_pView->GetActualOutDev());
1373 else
1374 {// OutDev suchen
1375 SdrPageView* pPageView = m_pView->GetSdrPageView();
1376 if( pPageView && !pOutDev )
1378 // const SdrPageViewWinList& rWinList = pPageView->GetWinList();
1379 // const SdrPageViewWindows& rPageViewWindows = pPageView->GetPageViewWindows();
1381 for( sal_uInt32 i = 0L; i < pPageView->PageWindowCount(); i++ )
1383 const SdrPageWindow& rPageWindow = *pPageView->GetPageWindow(i);
1385 if( rPageWindow.GetPaintWindow().GetOutputDevice().GetOutDevType() == OUTDEV_WINDOW)
1387 pOutDev = &rPageWindow.GetPaintWindow().GetOutputDevice();
1388 break;
1394 if ( !pOutDev )
1395 return NULL;
1398 // The service name decides which control should be created
1399 sal_uInt16 nOBJID = OBJ_FM_EDIT;
1400 if(OUString(_rDesc.szServiceName).equals((OUString)FM_SUN_COMPONENT_NUMERICFIELD))
1401 nOBJID = OBJ_FM_NUMERICFIELD;
1402 if(OUString(_rDesc.szServiceName).equals((OUString)FM_SUN_COMPONENT_CHECKBOX))
1403 nOBJID = OBJ_FM_CHECKBOX;
1404 if(OUString(_rDesc.szServiceName).equals((OUString)FM_COMPONENT_COMMANDBUTTON))
1405 nOBJID = OBJ_FM_BUTTON;
1407 typedef ::com::sun::star::form::submission::XSubmission XSubmission_t;
1408 Reference< XSubmission_t > xSubmission(_rDesc.xPropSet, UNO_QUERY);
1410 // xform control or submission button?
1411 if ( !xSubmission.is() )
1413 SdrUnoObj* pLabel( NULL );
1414 SdrUnoObj* pControl( NULL );
1415 if ( !createControlLabelPair( *pOutDev, 0, 0, NULL, xNumberFormats, nOBJID, sLabelPostfix,
1416 pLabel, pControl )
1419 return NULL;
1423 // Now build the connection between the control and the data item.
1424 Reference< XValueBinding > xValueBinding(_rDesc.xPropSet,UNO_QUERY);
1425 Reference< XBindableValue > xBindableValue(pControl->GetUnoControlModel(),UNO_QUERY);
1427 DBG_ASSERT( xBindableValue.is(), "FmXFormView::implCreateXFormsControl: control's not bindable!" );
1428 if ( xBindableValue.is() )
1429 xBindableValue->setValueBinding(xValueBinding);
1431 bool bCheckbox = ( OBJ_FM_CHECKBOX == nOBJID );
1432 OSL_ENSURE( !bCheckbox || !pLabel, "FmXFormView::implCreateXFormsControl: why was there a label created for a check box?" );
1433 if ( bCheckbox )
1434 return pControl;
1437 // group objects
1438 SdrObjGroup* pGroup = new SdrObjGroup();
1439 SdrObjList* pObjList = pGroup->GetSubList();
1440 pObjList->InsertObject(pLabel);
1441 pObjList->InsertObject(pControl);
1443 return pGroup;
1445 else {
1447 // create a button control
1448 const MapMode eTargetMode( pOutDev->GetMapMode() );
1449 const MapMode eSourceMode(MAP_100TH_MM);
1450 const sal_uInt16 nObjID = OBJ_FM_BUTTON;
1451 ::Size controlSize(4000, 500);
1452 FmFormObj *pControl = static_cast<FmFormObj*>(SdrObjFactory::MakeNewObject( FmFormInventor, nObjID, NULL, NULL ));
1453 controlSize.Width() = Fraction(controlSize.Width(), 1) * eTargetMode.GetScaleX();
1454 controlSize.Height() = Fraction(controlSize.Height(), 1) * eTargetMode.GetScaleY();
1455 ::Point controlPos( pOutDev->LogicToLogic( ::Point( controlSize.Width(), 0 ), eSourceMode, eTargetMode ) );
1456 ::Rectangle controlRect( controlPos, pOutDev->LogicToLogic( controlSize, eSourceMode, eTargetMode ) );
1457 pControl->SetLogicRect(controlRect);
1459 // set the button label
1460 Reference< XPropertySet > xControlSet(pControl->GetUnoControlModel(), UNO_QUERY);
1461 xControlSet->setPropertyValue(FM_PROP_LABEL, makeAny(OUString(_rDesc.szName)));
1463 // connect the submission with the submission supplier (aka the button)
1464 xControlSet->setPropertyValue( FM_PROP_BUTTON_TYPE,
1465 makeAny( FormButtonType_SUBMIT ) );
1466 typedef ::com::sun::star::form::submission::XSubmissionSupplier XSubmissionSupplier_t;
1467 Reference< XSubmissionSupplier_t > xSubmissionSupplier(pControl->GetUnoControlModel(), UNO_QUERY);
1468 xSubmissionSupplier->setSubmission(xSubmission);
1470 return pControl;
1473 catch (const Exception&)
1475 OSL_FAIL("FmXFormView::implCreateXFormsControl: caught an exception while creating the control !");
1479 return NULL;
1483 bool FmXFormView::createControlLabelPair( OutputDevice& _rOutDev, sal_Int32 _nXOffsetMM, sal_Int32 _nYOffsetMM,
1484 const Reference< XPropertySet >& _rxField, const Reference< XNumberFormats >& _rxNumberFormats,
1485 sal_uInt16 _nControlObjectID, const OUString& _rFieldPostfix,
1486 SdrUnoObj*& _rpLabel, SdrUnoObj*& _rpControl,
1487 const Reference< XDataSource >& _rxDataSource, const OUString& _rDataSourceName,
1488 const OUString& _rCommand, const sal_Int32 _nCommandType )
1490 if ( !createControlLabelPair( _rOutDev, _nXOffsetMM, _nYOffsetMM,
1491 _rxField, _rxNumberFormats, _nControlObjectID, _rFieldPostfix, FmFormInventor, OBJ_FM_FIXEDTEXT,
1492 NULL, NULL, NULL, _rpLabel, _rpControl )
1494 return false;
1496 // insert the control model(s) into the form component hierarchy
1497 if ( _rpLabel )
1498 lcl_insertIntoFormComponentHierarchy_throw( *m_pView, *_rpLabel, _rxDataSource, _rDataSourceName, _rCommand, _nCommandType );
1499 lcl_insertIntoFormComponentHierarchy_throw( *m_pView, *_rpControl, _rxDataSource, _rDataSourceName, _rCommand, _nCommandType );
1501 // some context-dependent initializations
1502 FormControlFactory aControlFactory;
1503 if ( _rpLabel )
1504 aControlFactory.initializeControlModel( impl_getDocumentType(), *_rpLabel );
1505 aControlFactory.initializeControlModel( impl_getDocumentType(), *_rpControl );
1507 return true;
1511 bool FmXFormView::createControlLabelPair( OutputDevice& _rOutDev, sal_Int32 _nXOffsetMM, sal_Int32 _nYOffsetMM,
1512 const Reference< XPropertySet >& _rxField,
1513 const Reference< XNumberFormats >& _rxNumberFormats, sal_uInt16 _nControlObjectID,
1514 const OUString& _rFieldPostfix, sal_uInt32 _nInventor, sal_uInt16 _nLabelObjectID,
1515 SdrPage* _pLabelPage, SdrPage* _pControlPage, SdrModel* _pModel, SdrUnoObj*& _rpLabel, SdrUnoObj*& _rpControl)
1517 sal_Int32 nDataType = 0;
1518 OUString sFieldName;
1519 Any aFieldName;
1520 if ( _rxField.is() )
1522 nDataType = ::comphelper::getINT32(_rxField->getPropertyValue(FM_PROP_FIELDTYPE));
1523 aFieldName = Any(_rxField->getPropertyValue(FM_PROP_NAME));
1524 aFieldName >>= sFieldName;
1527 // calculate the positions, respecting the settings of the target device
1528 ::Size aTextSize( _rOutDev.GetTextWidth(sFieldName + _rFieldPostfix), _rOutDev.GetTextHeight() );
1530 MapMode eTargetMode( _rOutDev.GetMapMode() ),
1531 eSourceMode( MAP_100TH_MM );
1533 // Textbreite ist mindestens 4cm
1534 // Texthoehe immer halber cm
1535 ::Size aDefTxtSize(4000, 500);
1536 ::Size aDefSize(4000, 500);
1537 ::Size aDefImageSize(4000, 4000);
1539 ::Size aRealSize = _rOutDev.LogicToLogic(aTextSize, eTargetMode, eSourceMode);
1540 aRealSize.Width() = std::max(aRealSize.Width(), aDefTxtSize.Width());
1541 aRealSize.Height()= aDefSize.Height();
1543 // adjust to scaling of the target device (#53523#)
1544 aRealSize.Width() = long(Fraction(aRealSize.Width(), 1) * eTargetMode.GetScaleX());
1545 aRealSize.Height() = long(Fraction(aRealSize.Height(), 1) * eTargetMode.GetScaleY());
1547 // for boolean fields, we do not create a label, but just a checkbox
1548 bool bNeedLabel = ( _nControlObjectID != OBJ_FM_CHECKBOX );
1550 // the label
1551 SAL_WNODEPRECATED_DECLARATIONS_PUSH
1552 ::std::auto_ptr< SdrUnoObj > pLabel;
1553 SAL_WNODEPRECATED_DECLARATIONS_POP
1554 Reference< XPropertySet > xLabelModel;
1555 if ( bNeedLabel )
1557 pLabel.reset( dynamic_cast< SdrUnoObj* >(
1558 SdrObjFactory::MakeNewObject( _nInventor, _nLabelObjectID, _pLabelPage, _pModel ) ) );
1559 OSL_ENSURE( pLabel.get(), "FmXFormView::createControlLabelPair: could not create the label!" );
1560 if ( !pLabel.get() )
1561 return false;
1563 xLabelModel.set( pLabel->GetUnoControlModel(), UNO_QUERY );
1564 if ( xLabelModel.is() )
1566 OUString sLabel;
1567 if ( _rxField.is() && _rxField->getPropertySetInfo()->hasPropertyByName(FM_PROP_LABEL) )
1568 _rxField->getPropertyValue(FM_PROP_LABEL) >>= sLabel;
1569 if ( sLabel.isEmpty() )
1570 sLabel = sFieldName;
1572 xLabelModel->setPropertyValue( FM_PROP_LABEL, makeAny( sLabel + _rFieldPostfix ) );
1573 OUString sObjectLabel(SVX_RESSTR(RID_STR_OBJECT_LABEL).replaceAll("#object#", sFieldName));
1574 xLabelModel->setPropertyValue(FM_PROP_NAME, makeAny(sObjectLabel));
1577 pLabel->SetLogicRect( ::Rectangle(
1578 _rOutDev.LogicToLogic( ::Point( _nXOffsetMM, _nYOffsetMM ), eSourceMode, eTargetMode ),
1579 _rOutDev.LogicToLogic( aRealSize, eSourceMode, eTargetMode )
1580 ) );
1583 // the control
1584 SAL_WNODEPRECATED_DECLARATIONS_PUSH
1585 ::std::auto_ptr< SdrUnoObj > pControl( dynamic_cast< SdrUnoObj* >(
1586 SdrObjFactory::MakeNewObject( _nInventor, _nControlObjectID, _pControlPage, _pModel ) ) );
1587 SAL_WNODEPRECATED_DECLARATIONS_POP
1588 OSL_ENSURE( pControl.get(), "FmXFormView::createControlLabelPair: could not create the control!" );
1589 if ( !pControl.get() )
1590 return false;
1592 Reference< XPropertySet > xControlSet( pControl->GetUnoControlModel(), UNO_QUERY );
1593 if ( !xControlSet.is() )
1594 return false;
1596 // size of the control
1597 ::Size aControlSize( aDefSize );
1598 switch ( nDataType )
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;
1612 if ( OBJ_FM_IMAGECONTROL == _nControlObjectID )
1613 aControlSize = aDefImageSize;
1615 aControlSize.Width() = long(Fraction(aControlSize.Width(), 1) * eTargetMode.GetScaleX());
1616 aControlSize.Height() = long(Fraction(aControlSize.Height(), 1) * eTargetMode.GetScaleY());
1618 pControl->SetLogicRect( ::Rectangle(
1619 _rOutDev.LogicToLogic( ::Point( aRealSize.Width() + _nXOffsetMM, _nYOffsetMM ), eSourceMode, eTargetMode ),
1620 _rOutDev.LogicToLogic( aControlSize, eSourceMode, eTargetMode )
1621 ) );
1623 // some initializations
1624 Reference< XPropertySetInfo > xControlPropInfo = xControlSet->getPropertySetInfo();
1626 if ( aFieldName.hasValue() )
1628 xControlSet->setPropertyValue( FM_PROP_CONTROLSOURCE, aFieldName );
1629 xControlSet->setPropertyValue( FM_PROP_NAME, aFieldName );
1630 if ( !bNeedLabel )
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_FAIL( "FmXFormView::createControlLabelPair: can't set a label for the control!" );
1640 if ( (nDataType == DataType::LONGVARCHAR || nDataType == DataType::CLOB) && xControlPropInfo->hasPropertyByName( FM_PROP_MULTILINE ) )
1642 xControlSet->setPropertyValue( FM_PROP_MULTILINE, makeAny( true ) );
1645 // announce the label to the control
1646 if ( xControlPropInfo->hasPropertyByName( FM_PROP_CONTROLLABEL ) && xLabelModel.is() )
1650 xControlSet->setPropertyValue( FM_PROP_CONTROLLABEL, makeAny( xLabelModel ) );
1652 catch (const Exception&)
1654 DBG_UNHANDLED_EXCEPTION();
1658 if ( _rxField.is() )
1660 FormControlFactory aControlFactory;
1661 aControlFactory.initializeFieldDependentProperties( _rxField, xControlSet, _rxNumberFormats );
1664 _rpLabel = pLabel.release();
1665 _rpControl = pControl.release();
1666 return true;
1670 FmXFormView::ObjectRemoveListener::ObjectRemoveListener( FmXFormView* pParent )
1671 :m_pParent( pParent )
1676 void FmXFormView::ObjectRemoveListener::Notify( SfxBroadcaster& /*rBC*/, const SfxHint& rHint )
1678 if (rHint.ISA(SdrHint) && (((SdrHint&)rHint).GetKind() == HINT_OBJREMOVED))
1679 m_pParent->ObjectRemovedInAliveMode(((SdrHint&)rHint).GetObject());
1683 void FmXFormView::ObjectRemovedInAliveMode( const SdrObject* pObject )
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)
1689 sal_uIntPtr nCount = m_aMark.GetMarkCount();
1690 for (sal_uIntPtr i = 0; i < nCount; ++i)
1692 SdrMark* pMark = m_aMark.GetMark(i);
1693 SdrObject* pCurrent = pMark->GetMarkedSdrObj();
1694 if (pObject == pCurrent)
1696 m_aMark.DeleteMark(i);
1697 return;
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 ...
1705 void FmXFormView::stopMarkListWatching()
1707 if ( m_pWatchStoredList )
1709 m_pWatchStoredList->EndListeningAll();
1710 delete m_pWatchStoredList;
1711 m_pWatchStoredList = NULL;
1716 void FmXFormView::startMarkListWatching()
1718 if ( !m_pWatchStoredList )
1720 FmFormModel* pModel = GetFormShell() ? GetFormShell()->GetFormModel() : NULL;
1721 DBG_ASSERT( pModel != NULL, "FmXFormView::startMarkListWatching: shell has no model!" );
1722 if (pModel)
1724 m_pWatchStoredList = new ObjectRemoveListener( this );
1725 m_pWatchStoredList->StartListening( *static_cast< SfxBroadcaster* >( pModel ) );
1728 else
1730 OSL_FAIL( "FmXFormView::startMarkListWatching: already listening!" );
1735 void FmXFormView::saveMarkList( bool _bSmartUnmark )
1737 if ( m_pView )
1739 m_aMark = m_pView->GetMarkedObjectList();
1740 if ( _bSmartUnmark )
1742 sal_uIntPtr nCount = m_aMark.GetMarkCount( );
1743 for ( sal_uIntPtr i = 0; i < nCount; ++i )
1745 SdrMark* pMark = m_aMark.GetMark(i);
1746 SdrObject* pObj = pMark->GetMarkedSdrObj();
1748 if ( m_pView->IsObjMarked( pObj ) )
1750 if ( pObj->IsGroupObject() )
1752 SdrObjListIter aIter( *pObj->GetSubList() );
1753 bool bMixed = false;
1754 while ( aIter.IsMore() && !bMixed )
1755 bMixed = ( aIter.Next()->GetObjInventor() != FmFormInventor );
1757 if ( !bMixed )
1759 // all objects in the group are form objects
1760 m_pView->MarkObj( pMark->GetMarkedSdrObj(), pMark->GetPageView(), true /* unmark! */ );
1763 else
1765 if ( pObj->GetObjInventor() == FmFormInventor )
1766 { // this is a form layer object
1767 m_pView->MarkObj( pMark->GetMarkedSdrObj(), pMark->GetPageView(), true /* unmark! */ );
1774 else
1776 OSL_FAIL( "FmXFormView::saveMarkList: invalid view!" );
1777 m_aMark = SdrMarkList();
1782 static bool lcl_hasObject( SdrObjListIter& rIter, SdrObject* pObj )
1784 bool bFound = false;
1785 while (rIter.IsMore() && !bFound)
1786 bFound = pObj == rIter.Next();
1788 rIter.Reset();
1789 return bFound;
1793 void FmXFormView::restoreMarkList( SdrMarkList& _rRestoredMarkList )
1795 if ( !m_pView )
1796 return;
1798 _rRestoredMarkList.Clear();
1800 const SdrMarkList& rCurrentList = m_pView->GetMarkedObjectList();
1801 FmFormPage* pPage = GetFormShell() ? GetFormShell()->GetCurPage() : NULL;
1802 if (pPage)
1804 if (rCurrentList.GetMarkCount())
1805 { // there is a current mark ... hmm. Is it a subset of the mark we remembered in saveMarkList?
1806 bool bMisMatch = false;
1808 // loop through all current marks
1809 sal_uIntPtr nCurrentCount = rCurrentList.GetMarkCount();
1810 for ( sal_uIntPtr i=0; i<nCurrentCount&& !bMisMatch; ++i )
1812 const SdrObject* pCurrentMarked = rCurrentList.GetMark( i )->GetMarkedSdrObj();
1814 // loop through all saved marks, check for equality
1815 bool bFound = false;
1816 sal_uIntPtr nSavedCount = m_aMark.GetMarkCount();
1817 for ( sal_uIntPtr j=0; j<nSavedCount && !bFound; ++j )
1819 if ( m_aMark.GetMark( j )->GetMarkedSdrObj() == pCurrentMarked )
1820 bFound = true;
1823 // did not find a current mark in the saved marks
1824 if ( !bFound )
1825 bMisMatch = true;
1828 if ( bMisMatch )
1830 m_aMark.Clear();
1831 _rRestoredMarkList = rCurrentList;
1832 return;
1835 // wichtig ist das auf die Objecte der markliste nicht zugegriffen wird
1836 // da diese bereits zerstoert sein koennen
1837 SdrPageView* pCurPageView = m_pView->GetSdrPageView();
1838 SdrObjListIter aPageIter( *pPage );
1839 bool bFound = true;
1841 // gibt es noch alle Objecte
1842 sal_uIntPtr nCount = m_aMark.GetMarkCount();
1843 for (sal_uIntPtr i = 0; i < nCount && bFound; i++)
1845 SdrMark* pMark = m_aMark.GetMark(i);
1846 SdrObject* pObj = pMark->GetMarkedSdrObj();
1847 if (pObj->IsGroupObject())
1849 SdrObjListIter aIter(*pObj->GetSubList());
1850 while (aIter.IsMore() && bFound)
1851 bFound = lcl_hasObject(aPageIter, aIter.Next());
1853 else
1854 bFound = lcl_hasObject(aPageIter, pObj);
1856 bFound = bFound && pCurPageView == pMark->GetPageView();
1859 if (bFound)
1861 // Das LastObject auswerten
1862 if (nCount) // Objecte jetzt Markieren
1864 for (sal_uIntPtr i = 0; i < nCount; i++)
1866 SdrMark* pMark = m_aMark.GetMark(i);
1867 SdrObject* pObj = pMark->GetMarkedSdrObj();
1868 if ( pObj->GetObjInventor() == FmFormInventor )
1869 if ( !m_pView->IsObjMarked( pObj ) )
1870 m_pView->MarkObj( pObj, pMark->GetPageView() );
1873 _rRestoredMarkList = m_aMark;
1876 m_aMark.Clear();
1880 void SAL_CALL FmXFormView::focusGained( const FocusEvent& /*e*/ ) throw (RuntimeException, std::exception)
1882 if ( m_xWindow.is() && m_pView )
1884 m_pView->SetMoveOutside( true, FmFormView::ImplAccess() );
1888 void SAL_CALL FmXFormView::focusLost( const FocusEvent& /*e*/ ) throw (RuntimeException, std::exception)
1890 // when switch the focus outside the office the mark didn't change
1891 // so we can not remove us as focus listener
1892 if ( m_xWindow.is() && m_pView )
1894 m_pView->SetMoveOutside( false, FmFormView::ImplAccess() );
1898 void FmXFormView::removeGridWindowListening()
1900 if ( m_xWindow.is() )
1902 m_xWindow->removeFocusListener(this);
1903 if ( m_pView )
1905 m_pView->SetMoveOutside( false, FmFormView::ImplAccess() );
1907 m_xWindow = NULL;
1912 DocumentType FmXFormView::impl_getDocumentType() const
1914 if ( GetFormShell() && GetFormShell()->GetImpl() )
1915 return GetFormShell()->GetImpl()->getDocumentType();
1916 return eUnknownDocumentType;
1919 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */