LibreOffice Module svx (master)  1
formcontroller.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 
22 #include <fmcontrollayout.hxx>
23 #include <formcontroller.hxx>
26 #include <formcontrolling.hxx>
27 #include <fmprop.hxx>
28 #include <svx/dialmgr.hxx>
29 #include <svx/strings.hrc>
30 #include <fmservs.hxx>
31 #include <svx/fmtools.hxx>
32 #include <fmurl.hxx>
33 
34 #include <com/sun/star/awt/FocusChangeReason.hpp>
35 #include <com/sun/star/awt/XCheckBox.hpp>
36 #include <com/sun/star/awt/XComboBox.hpp>
37 #include <com/sun/star/awt/XListBox.hpp>
38 #include <com/sun/star/awt/XVclWindowPeer.hpp>
39 #include <com/sun/star/awt/TabController.hpp>
40 #include <com/sun/star/beans/PropertyAttribute.hpp>
41 #include <com/sun/star/container/XIdentifierReplace.hpp>
42 #include <com/sun/star/form/TabulatorCycle.hpp>
43 #include <com/sun/star/form/validation/XValidatableFormComponent.hpp>
44 #include <com/sun/star/form/XBoundComponent.hpp>
45 #include <com/sun/star/form/XBoundControl.hpp>
46 #include <com/sun/star/form/XGridControl.hpp>
47 #include <com/sun/star/form/XLoadable.hpp>
48 #include <com/sun/star/form/XReset.hpp>
49 #include <com/sun/star/form/control/FilterControl.hpp>
50 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
51 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
52 #include <com/sun/star/lang/NoSupportException.hpp>
53 #include <com/sun/star/sdb/ParametersRequest.hpp>
54 #include <com/sun/star/sdb/RowChangeAction.hpp>
55 #include <com/sun/star/sdb/SQLFilterOperator.hpp>
56 #include <com/sun/star/sdb/XInteractionSupplyParameters.hpp>
57 #include <com/sun/star/sdbc/ColumnValue.hpp>
58 #include <com/sun/star/task/InteractionHandler.hpp>
59 #include <com/sun/star/form/runtime/FormOperations.hpp>
60 #include <com/sun/star/form/runtime/FormFeature.hpp>
61 #include <com/sun/star/container/XContainer.hpp>
62 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
63 #include <com/sun/star/util/NumberFormatter.hpp>
64 #include <com/sun/star/sdb/SQLContext.hpp>
65 #include <com/sun/star/sdb/XColumn.hpp>
66 
70 #include <comphelper/property.hxx>
71 #include <comphelper/sequence.hxx>
72 #include <comphelper/flagguard.hxx>
73 #include <comphelper/types.hxx>
76 #include <connectivity/dbtools.hxx>
80 #include <tools/debug.hxx>
81 #include <tools/diagnose_ex.h>
83 #include <vcl/svapp.hxx>
84 #include <vcl/settings.hxx>
85 #include <vcl/window.hxx>
86 #include <o3tl/safeint.hxx>
87 #include <osl/mutex.hxx>
88 #include <sal/log.hxx>
89 
90 #include <algorithm>
91 #include <iterator>
92 
93 using namespace ::com::sun::star;
94 using namespace ::comphelper;
95 using namespace ::connectivity;
96 using namespace ::dbtools;
97 
98 
99 css::uno::Reference< css::uno::XInterface >
100  FormController_NewInstance_Impl( const css::uno::Reference< css::lang::XMultiServiceFactory > & _rxORB )
101 {
102  return *( new ::svxform::FormController( comphelper::getComponentContext(_rxORB) ) );
103 }
104 
105 namespace svxform
106 {
107 
108  using ::com::sun::star::sdb::XColumn;
109  using ::com::sun::star::awt::XControl;
110  using ::com::sun::star::awt::TabController;
111  using ::com::sun::star::awt::XToolkit;
112  using ::com::sun::star::awt::XWindowPeer;
113  using ::com::sun::star::form::XGrid;
115  using ::com::sun::star::uno::UNO_SET_THROW;
116  using ::com::sun::star::uno::UNO_QUERY_THROW;
117  using ::com::sun::star::container::XIndexAccess;
118  using ::com::sun::star::uno::Exception;
119  using ::com::sun::star::uno::XInterface;
120  using ::com::sun::star::uno::UNO_QUERY;
121  using ::com::sun::star::uno::Sequence;
122  using ::com::sun::star::uno::Reference;
123  using ::com::sun::star::beans::XPropertySetInfo;
124  using ::com::sun::star::beans::PropertyValue;
125  using ::com::sun::star::lang::IndexOutOfBoundsException;
126  using ::com::sun::star::sdb::XInteractionSupplyParameters;
127  using ::com::sun::star::awt::XTextComponent;
128  using ::com::sun::star::awt::XTextListener;
129  using ::com::sun::star::uno::Any;
130  using ::com::sun::star::frame::XDispatch;
131  using ::com::sun::star::lang::XMultiServiceFactory;
132  using ::com::sun::star::uno::Type;
133  using ::com::sun::star::lang::IllegalArgumentException;
134  using ::com::sun::star::sdbc::XConnection;
135  using ::com::sun::star::sdbc::XRowSet;
136  using ::com::sun::star::sdbc::XDatabaseMetaData;
137  using ::com::sun::star::util::XNumberFormatsSupplier;
138  using ::com::sun::star::util::NumberFormatter;
139  using ::com::sun::star::util::XNumberFormatter;
140  using ::com::sun::star::sdbcx::XColumnsSupplier;
141  using ::com::sun::star::container::XNameAccess;
142  using ::com::sun::star::lang::EventObject;
143  using ::com::sun::star::beans::Property;
144  using ::com::sun::star::container::XEnumeration;
145  using ::com::sun::star::form::XFormComponent;
146  using ::com::sun::star::form::runtime::XFormOperations;
147  using ::com::sun::star::form::runtime::FilterEvent;
148  using ::com::sun::star::form::runtime::XFilterControllerListener;
149  using ::com::sun::star::awt::XControlContainer;
150  using ::com::sun::star::container::XIdentifierReplace;
151  using ::com::sun::star::form::XFormControllerListener;
152  using ::com::sun::star::awt::XWindow;
153  using ::com::sun::star::sdbc::XResultSet;
154  using ::com::sun::star::awt::XControlModel;
155  using ::com::sun::star::awt::XTabControllerModel;
156  using ::com::sun::star::beans::PropertyChangeEvent;
157  using ::com::sun::star::form::validation::XValidatableFormComponent;
158  using ::com::sun::star::form::XLoadable;
159  using ::com::sun::star::form::XBoundControl;
160  using ::com::sun::star::beans::XPropertyChangeListener;
161  using ::com::sun::star::awt::TextEvent;
162  using ::com::sun::star::form::XBoundComponent;
163  using ::com::sun::star::awt::XCheckBox;
164  using ::com::sun::star::awt::XComboBox;
165  using ::com::sun::star::awt::XListBox;
166  using ::com::sun::star::awt::ItemEvent;
167  using ::com::sun::star::util::XModifyListener;
168  using ::com::sun::star::form::XReset;
169  using ::com::sun::star::frame::XDispatchProviderInterception;
170  using ::com::sun::star::form::XGridControl;
171  using ::com::sun::star::awt::XVclWindowPeer;
172  using ::com::sun::star::form::validation::XValidator;
173  using ::com::sun::star::awt::FocusEvent;
174  using ::com::sun::star::sdb::SQLContext;
175  using ::com::sun::star::container::XChild;
176  using ::com::sun::star::form::TabulatorCycle_RECORDS;
177  using ::com::sun::star::container::ContainerEvent;
178  using ::com::sun::star::lang::DisposedException;
179  using ::com::sun::star::lang::Locale;
180  using ::com::sun::star::lang::NoSupportException;
181  using ::com::sun::star::sdb::RowChangeEvent;
182  using ::com::sun::star::frame::XStatusListener;
183  using ::com::sun::star::frame::XDispatchProviderInterceptor;
184  using ::com::sun::star::sdb::SQLErrorEvent;
185  using ::com::sun::star::form::DatabaseParameterEvent;
186  using ::com::sun::star::sdb::ParametersRequest;
187  using ::com::sun::star::task::XInteractionRequest;
188  using ::com::sun::star::util::URL;
189  using ::com::sun::star::frame::FeatureStateEvent;
190  using ::com::sun::star::form::runtime::XFormControllerContext;
191  using ::com::sun::star::task::InteractionHandler;
192  using ::com::sun::star::task::XInteractionHandler;
193  using ::com::sun::star::form::runtime::FormOperations;
194  using ::com::sun::star::container::XContainer;
195  using ::com::sun::star::sdbc::SQLWarning;
196 
197  namespace ColumnValue = ::com::sun::star::sdbc::ColumnValue;
198  namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
199  namespace FocusChangeReason = ::com::sun::star::awt::FocusChangeReason;
200  namespace RowChangeAction = ::com::sun::star::sdb::RowChangeAction;
201  namespace FormFeature = ::com::sun::star::form::runtime::FormFeature;
202 
203 namespace {
204 
205 struct ColumnInfo
206 {
207  // information about the column itself
208  Reference< XColumn > xColumn;
209  sal_Int32 nNullable;
211  bool bReadOnly;
212  OUString sName;
213 
214  // information about the control(s) bound to this column
215 
217  Reference< XControl > xFirstControlWithInputRequired;
226 
227  ColumnInfo()
228  :xColumn()
229  ,nNullable( ColumnValue::NULLABLE_UNKNOWN )
230  ,bAutoIncrement( false )
231  ,bReadOnly( false )
232  ,sName()
233  ,xFirstControlWithInputRequired()
234  ,xFirstGridWithInputRequiredColumn()
235  ,nRequiredGridColumn( -1 )
236  {
237  }
238 };
239 
240 }
241 
243 {
244 public:
245  explicit ColumnInfoCache( const Reference< XColumnsSupplier >& _rxColSupplier );
246 
247  size_t getColumnCount() const { return m_aColumns.size(); }
248  const ColumnInfo& getColumnInfo( size_t _pos );
249 
251  void initializeControls( const Sequence< Reference< XControl > >& _rControls );
252  void deinitializeControls();
253 
254 private:
255  typedef ::std::vector< ColumnInfo > ColumnInfos;
256  ColumnInfos m_aColumns;
258 };
259 
260 
261 ColumnInfoCache::ColumnInfoCache( const Reference< XColumnsSupplier >& _rxColSupplier )
262  :m_aColumns()
263  ,m_bControlsInitialized( false )
264 {
265  try
266  {
267  m_aColumns.clear();
268 
269  Reference< XIndexAccess > xColumns( _rxColSupplier->getColumns(), UNO_QUERY_THROW );
270  sal_Int32 nColumnCount = xColumns->getCount();
271  m_aColumns.reserve( nColumnCount );
272 
273  Reference< XPropertySet > xColumnProps;
274  for ( sal_Int32 i = 0; i < nColumnCount; ++i )
275  {
276  ColumnInfo aColInfo;
277  aColInfo.xColumn.set( xColumns->getByIndex(i), UNO_QUERY_THROW );
278 
279  xColumnProps.set( aColInfo.xColumn, UNO_QUERY_THROW );
280  OSL_VERIFY( xColumnProps->getPropertyValue( FM_PROP_ISNULLABLE ) >>= aColInfo.nNullable );
281  OSL_VERIFY( xColumnProps->getPropertyValue( FM_PROP_AUTOINCREMENT ) >>= aColInfo.bAutoIncrement );
282  OSL_VERIFY( xColumnProps->getPropertyValue( FM_PROP_NAME ) >>= aColInfo.sName );
283  OSL_VERIFY( xColumnProps->getPropertyValue( FM_PROP_ISREADONLY ) >>= aColInfo.bReadOnly );
284 
285  m_aColumns.push_back( aColInfo );
286  }
287  }
288  catch( const Exception& )
289  {
291  }
292 }
293 
294 
295 namespace
296 {
297  bool lcl_isBoundTo( const Reference< XPropertySet >& _rxControlModel, const Reference< XInterface >& _rxNormDBField )
298  {
299  Reference< XInterface > xNormBoundField( _rxControlModel->getPropertyValue( FM_PROP_BOUNDFIELD ), UNO_QUERY );
300  return ( xNormBoundField == _rxNormDBField );
301  }
302 
303  bool lcl_isInputRequired( const Reference< XPropertySet >& _rxControlModel )
304  {
305  bool bInputRequired = false;
306  OSL_VERIFY( _rxControlModel->getPropertyValue( FM_PROP_INPUT_REQUIRED ) >>= bInputRequired );
307  return bInputRequired;
308  }
309 
310  void lcl_resetColumnControlInfo( ColumnInfo& _rColInfo )
311  {
312  _rColInfo.xFirstControlWithInputRequired.clear();
313  _rColInfo.xFirstGridWithInputRequiredColumn.clear();
314  _rColInfo.nRequiredGridColumn = -1;
315  }
316 }
317 
318 
320 {
321  for (auto& rCol : m_aColumns)
322  {
323  lcl_resetColumnControlInfo( rCol );
324  }
325  m_bControlsInitialized = false;
326 }
327 
328 
329 void ColumnInfoCache::initializeControls( const Sequence< Reference< XControl > >& _rControls )
330 {
331  try
332  {
333  // for every of our known columns, find the controls which are bound to this column
334  for (auto& rCol : m_aColumns)
335  {
336  OSL_ENSURE( !rCol.xFirstControlWithInputRequired.is() && !rCol.xFirstGridWithInputRequiredColumn.is()
337  && ( rCol.nRequiredGridColumn == -1 ), "ColumnInfoCache::initializeControls: called me twice?" );
338 
339  lcl_resetColumnControlInfo( rCol );
340 
341  Reference< XInterface > xNormColumn( rCol.xColumn, UNO_QUERY_THROW );
342 
343  const Reference< XControl >* pControl( _rControls.getConstArray() );
344  const Reference< XControl >* pControlEnd( pControl + _rControls.getLength() );
345  for ( ; pControl != pControlEnd; ++pControl )
346  {
347  if ( !pControl->is() )
348  continue;
349 
350  Reference< XPropertySet > xModel( (*pControl)->getModel(), UNO_QUERY_THROW );
351  Reference< XPropertySetInfo > xModelPSI( xModel->getPropertySetInfo(), UNO_SET_THROW );
352 
353  // special handling for grid controls
354  Reference< XGrid > xGrid( *pControl, UNO_QUERY );
355  if ( xGrid.is() )
356  {
357  Reference< XIndexAccess > xGridColAccess( xModel, UNO_QUERY_THROW );
358  sal_Int32 gridColCount = xGridColAccess->getCount();
359  sal_Int32 gridCol = 0;
360  for ( gridCol = 0; gridCol < gridColCount; ++gridCol )
361  {
362  Reference< XPropertySet > xGridColumnModel( xGridColAccess->getByIndex( gridCol ), UNO_QUERY_THROW );
363 
364  if ( !lcl_isBoundTo( xGridColumnModel, xNormColumn )
365  || !lcl_isInputRequired( xGridColumnModel )
366  )
367  continue; // with next grid column
368 
369  break;
370  }
371 
372  if ( gridCol < gridColCount )
373  {
374  // found a grid column which is bound to the given
375  rCol.xFirstGridWithInputRequiredColumn = xGrid;
376  rCol.nRequiredGridColumn = gridCol;
377  break;
378  }
379 
380  continue; // with next control
381  }
382 
383  if ( !xModelPSI->hasPropertyByName( FM_PROP_BOUNDFIELD )
384  || !lcl_isBoundTo( xModel, xNormColumn )
385  || !lcl_isInputRequired( xModel )
386  )
387  continue; // with next control
388 
389  break;
390  }
391 
392  if ( pControl == pControlEnd )
393  // did not find a control which is bound to this particular column, and for which the input is required
394  continue; // with next DB column
395 
396  rCol.xFirstControlWithInputRequired = *pControl;
397  }
398  }
399  catch( const Exception& )
400  {
402  }
403 
404  m_bControlsInitialized = true;
405 }
406 
407 
408 const ColumnInfo& ColumnInfoCache::getColumnInfo( size_t _pos )
409 {
410  if ( _pos >= m_aColumns.size() )
411  throw IndexOutOfBoundsException();
412 
413  return m_aColumns[ _pos ];
414 }
415 
416 namespace {
417 
418 class OParameterContinuation : public OInteraction< XInteractionSupplyParameters >
419 {
420  Sequence< PropertyValue > m_aValues;
421 
422 public:
423  OParameterContinuation() { }
424 
425  const Sequence< PropertyValue >& getValues() const { return m_aValues; }
426 
427 // XInteractionSupplyParameters
428  virtual void SAL_CALL setParameters( const Sequence< PropertyValue >& _rValues ) override;
429 };
430 
431 }
432 
433 void SAL_CALL OParameterContinuation::setParameters( const Sequence< PropertyValue >& _rValues )
434 {
435  m_aValues = _rValues;
436 }
437 
438 
439 // FmXAutoControl
440 
442 {
443  OUString aFieldName;
445  Reference< XTextComponent > xText;
446 
447  FmFieldInfo(const Reference< XPropertySet >& _xField, const Reference< XTextComponent >& _xText)
448  :xField(_xField)
449  ,xText(_xText)
450  {xField->getPropertyValue(FM_PROP_NAME) >>= aFieldName;}
451 };
452 
453 namespace {
454 
455 class FmXAutoControl: public UnoControl
456 
457 {
458 public:
459  FmXAutoControl() :UnoControl()
460  {
461  }
462 
463  virtual OUString GetComponentServiceName() override {return "Edit";}
464  virtual void SAL_CALL createPeer( const Reference< XToolkit > & rxToolkit, const Reference< XWindowPeer > & rParentPeer ) override;
465 
466 protected:
467  virtual void ImplSetPeerProperty( const OUString& rPropName, const Any& rVal ) override;
468 };
469 
470 }
471 
472 void FmXAutoControl::createPeer( const Reference< XToolkit > & rxToolkit, const Reference< XWindowPeer > & rParentPeer )
473 {
474  UnoControl::createPeer( rxToolkit, rParentPeer );
475 
476  Reference< XTextComponent > xText(getPeer() , UNO_QUERY);
477  if (xText.is())
478  {
479  xText->setText(SvxResId(RID_STR_AUTOFIELD));
480  xText->setEditable(false);
481  }
482 }
483 
484 
485 void FmXAutoControl::ImplSetPeerProperty( const OUString& rPropName, const Any& rVal )
486 {
487  // these properties are ignored
488  if (rPropName == FM_PROP_TEXT)
489  return;
490 
491  UnoControl::ImplSetPeerProperty( rPropName, rVal );
492 }
493 
494 
495 IMPL_LINK_NOARG( FormController, OnActivateTabOrder, Timer*, void )
496 {
497  activateTabOrder();
498 }
499 
500 namespace {
501 
502 struct UpdateAllListeners
503 {
504  bool operator()( const Reference< XDispatch >& _rxDispatcher ) const
505  {
506  static_cast< svx::OSingleFeatureDispatcher* >( _rxDispatcher.get() )->updateAllListeners();
507  // the return is a dummy only so we can use this struct in a lambda expression
508  return true;
509  }
510 };
511 
512 }
513 
514 IMPL_LINK_NOARG( FormController, OnInvalidateFeatures, Timer*, void )
515 {
516  ::osl::MutexGuard aGuard( m_aMutex );
517  for (const auto& rFeature : m_aInvalidFeatures)
518  {
519  DispatcherContainer::const_iterator aDispatcherPos = m_aFeatureDispatchers.find( rFeature );
520  if ( aDispatcherPos != m_aFeatureDispatchers.end() )
521  {
522  // TODO: for the real and actual listener notifications, we should release
523  // our mutex
524  UpdateAllListeners( )( aDispatcherPos->second );
525  }
526  }
527 }
528 
529 FormController::FormController(const Reference< css::uno::XComponentContext > & _rxORB )
531  ,OPropertySetHelper( FormController_BASE::rBHelper )
532  ,OSQLParserClient( _rxORB )
533  ,m_xComponentContext( _rxORB )
534  ,m_aActivateListeners(m_aMutex)
535  ,m_aModifyListeners(m_aMutex)
536  ,m_aErrorListeners(m_aMutex)
537  ,m_aDeleteListeners(m_aMutex)
538  ,m_aRowSetApproveListeners(m_aMutex)
539  ,m_aParameterListeners(m_aMutex)
540  ,m_aFilterListeners(m_aMutex)
541  ,m_xFormOperations()
542  ,m_aMode( OUString( "DataMode" ) )
543  ,m_aLoadEvent( LINK( this, FormController, OnLoad ) )
544  ,m_aToggleEvent( LINK( this, FormController, OnToggleAutoFields ) )
545  ,m_aActivationEvent( LINK( this, FormController, OnActivated ) )
546  ,m_aDeactivationEvent( LINK( this, FormController, OnDeactivated ) )
547  ,m_nCurrentFilterPosition(-1)
548  ,m_bCurrentRecordModified(false)
549  ,m_bCurrentRecordNew(false)
550  ,m_bLocked(false)
551  ,m_bDBConnection(false)
552  ,m_bCycle(false)
553  ,m_bCanInsert(false)
554  ,m_bCanUpdate(false)
555  ,m_bCommitLock(false)
556  ,m_bModified(false)
557  ,m_bControlsSorted(false)
558  ,m_bFiltering(false)
559  ,m_bAttachEvents(true)
560  ,m_bDetachEvents(true)
561  ,m_bAttemptedHandlerCreation( false )
562  ,m_bSuspendFilterTextListening( false )
563 {
564 
565  osl_atomic_increment(&m_refCount);
566  {
567  m_xTabController = TabController::create( m_xComponentContext );
568  m_xAggregate.set( m_xTabController, UNO_QUERY_THROW );
569  m_xAggregate->setDelegator( *this );
570  }
571  osl_atomic_decrement(&m_refCount);
572 
573  m_aTabActivationIdle.SetPriority( TaskPriority::LOWEST );
574  m_aTabActivationIdle.SetInvokeHandler( LINK( this, FormController, OnActivateTabOrder ) );
575 
576  m_aFeatureInvalidationTimer.SetTimeout( 200 );
577  m_aFeatureInvalidationTimer.SetInvokeHandler( LINK( this, FormController, OnInvalidateFeatures ) );
578 }
579 
580 
581 FormController::~FormController()
582 {
583  {
584  ::osl::MutexGuard aGuard( m_aMutex );
585 
586  m_aLoadEvent.CancelPendingCall();
587  m_aToggleEvent.CancelPendingCall();
588  m_aActivationEvent.CancelPendingCall();
589  m_aDeactivationEvent.CancelPendingCall();
590 
591  if ( m_aTabActivationIdle.IsActive() )
592  m_aTabActivationIdle.Stop();
593  }
594 
595  if ( m_aFeatureInvalidationTimer.IsActive() )
596  m_aFeatureInvalidationTimer.Stop();
597 
598  disposeAllFeaturesAndDispatchers();
599 
600  if ( m_xFormOperations.is() )
601  m_xFormOperations->dispose();
602  m_xFormOperations.clear();
603 
604  // release of aggregation
605  if ( m_xAggregate.is() )
606  {
607  m_xAggregate->setDelegator( nullptr );
608  m_xAggregate.clear();
609  }
610 }
611 
612 
613 void SAL_CALL FormController::acquire() throw ()
614 {
615  FormController_BASE::acquire();
616 }
617 
618 
619 void SAL_CALL FormController::release() throw ()
620 {
621  FormController_BASE::release();
622 }
623 
624 
625 Any SAL_CALL FormController::queryInterface( const Type& _rType )
626 {
627  Any aRet = FormController_BASE::queryInterface( _rType );
628  if ( !aRet.hasValue() )
629  aRet = OPropertySetHelper::queryInterface( _rType );
630  if ( !aRet.hasValue() )
631  aRet = m_xAggregate->queryAggregation( _rType );
632  return aRet;
633 }
634 
635 
636 Sequence< sal_Int8 > SAL_CALL FormController::getImplementationId()
637 {
638  return css::uno::Sequence<sal_Int8>();
639 }
640 
641 Sequence< Type > SAL_CALL FormController::getTypes( )
642 {
644  FormController_BASE::getTypes(),
646  );
647 }
648 
649 // XServiceInfo
650 sal_Bool SAL_CALL FormController::supportsService(const OUString& ServiceName)
651 {
652  return cppu::supportsService(this, ServiceName);
653 }
654 
655 OUString SAL_CALL FormController::getImplementationName()
656 {
657  return "org.openoffice.comp.svx.FormController";
658 }
659 
660 Sequence< OUString> SAL_CALL FormController::getSupportedServiceNames()
661 {
662  // service names which are supported only, but cannot be used to created an
663  // instance at a service factory
664  Sequence<OUString> aNonCreatableServiceNames { "com.sun.star.form.FormControllerDispatcher" };
665 
666  // services which can be used to created an instance at a service factory
667  Sequence< OUString > aCreatableServiceNames( getSupportedServiceNames_Static() );
668  return ::comphelper::concatSequences( aCreatableServiceNames, aNonCreatableServiceNames );
669 }
670 
671 
672 sal_Bool SAL_CALL FormController::approveReset(const EventObject& /*rEvent*/)
673 {
674  return true;
675 }
676 
677 
678 void SAL_CALL FormController::resetted(const EventObject& rEvent)
679 {
680  ::osl::MutexGuard aGuard(m_aMutex);
681  if (getCurrentControl().is() && (getCurrentControl()->getModel() == rEvent.Source))
682  m_bModified = false;
683 }
684 
685 
686 Sequence< OUString> const & FormController::getSupportedServiceNames_Static()
687 {
688  static Sequence< OUString> const aServices
689  {
690  "com.sun.star.form.runtime.FormController",
691  "com.sun.star.awt.control.TabController"
692  };
693  return aServices;
694 }
695 
696 
697 namespace
698 {
699  struct ResetComponentText
700  {
701  void operator()( const Reference< XTextComponent >& _rxText )
702  {
703  _rxText->setText( OUString() );
704  }
705  };
706 
707  struct RemoveComponentTextListener
708  {
709  explicit RemoveComponentTextListener( const Reference< XTextListener >& _rxListener )
710  :m_xListener( _rxListener )
711  {
712  }
713 
714  void operator()( const Reference< XTextComponent >& _rxText )
715  {
716  _rxText->removeTextListener( m_xListener );
717  }
718 
719  private:
720  Reference< XTextListener > m_xListener;
721  };
722 }
723 
724 
725 void FormController::impl_setTextOnAllFilter_throw()
726 {
727  m_bSuspendFilterTextListening = true;
728  ::comphelper::FlagGuard aResetFlag( m_bSuspendFilterTextListening );
729 
730  // reset the text for all controls
731  ::std::for_each( m_aFilterComponents.begin(), m_aFilterComponents.end(), ResetComponentText() );
732 
733  if ( m_aFilterRows.empty() )
734  // nothing to do anymore
735  return;
736 
737  if ( m_nCurrentFilterPosition < 0 )
738  return;
739 
740  // set the text for all filters
741  OSL_ENSURE( m_aFilterRows.size() > o3tl::make_unsigned(m_nCurrentFilterPosition),
742  "FormController::impl_setTextOnAllFilter_throw: m_nCurrentFilterPosition too big" );
743 
744  if ( o3tl::make_unsigned(m_nCurrentFilterPosition) < m_aFilterRows.size() )
745  {
746  FmFilterRow& rRow = m_aFilterRows[ m_nCurrentFilterPosition ];
747  for (const auto& rEntry : rRow)
748  {
749  rEntry.first->setText( rEntry.second );
750  }
751  }
752 }
753 // OPropertySetHelper
754 
755 sal_Bool FormController::convertFastPropertyValue( Any & /*rConvertedValue*/, Any & /*rOldValue*/,
756  sal_Int32 /*nHandle*/, const Any& /*rValue*/ )
757 {
758  return false;
759 }
760 
761 
762 void FormController::setFastPropertyValue_NoBroadcast( sal_Int32 /*nHandle*/, const Any& /*rValue*/ )
763 {
764 }
765 
766 
767 void FormController::getFastPropertyValue( Any& rValue, sal_Int32 nHandle ) const
768 {
769  switch (nHandle)
770  {
771  case FM_ATTR_FILTER:
772  {
773  OUStringBuffer aFilter;
774  Reference<XConnection> xConnection(getConnection(Reference< XRowSet>(m_xModelAsIndex, UNO_QUERY)));
775  if (xConnection.is())
776  {
777  Reference< XNumberFormatsSupplier> xFormatSupplier( getNumberFormats( xConnection, true ) );
778  Reference< XNumberFormatter> xFormatter = NumberFormatter::create(m_xComponentContext);
779  xFormatter->attachNumberFormatsSupplier(xFormatSupplier);
780 
781  // now add the filter rows
782  try
783  {
784  for (const FmFilterRow& rRow : m_aFilterRows)
785  {
786  if ( rRow.empty() )
787  continue;
788 
789  OUStringBuffer aRowFilter;
790  for ( FmFilterRow::const_iterator condition = rRow.begin(); condition != rRow.end(); ++condition )
791  {
792  // get the field of the controls map
793  Reference< XControl > xControl( condition->first, UNO_QUERY_THROW );
794  Reference< XPropertySet > xModelProps( xControl->getModel(), UNO_QUERY_THROW );
795  Reference< XPropertySet > xField( xModelProps->getPropertyValue( FM_PROP_BOUNDFIELD ), UNO_QUERY_THROW );
796 
797  OUString sFilterValue( condition->second );
798 
799  OUString sErrorMsg, sCriteria;
800  const std::unique_ptr< OSQLParseNode > pParseNode =
801  predicateTree( sErrorMsg, sFilterValue, xFormatter, xField );
802  OSL_ENSURE( pParseNode != nullptr, "FormController::getFastPropertyValue: could not parse the field value predicate!" );
803  if ( pParseNode != nullptr )
804  {
805  // don't use a parse context here, we need it unlocalized
806  pParseNode->parseNodeToStr( sCriteria, xConnection );
807  if ( condition != rRow.begin() )
808  aRowFilter.append( " AND " );
809  aRowFilter.append( sCriteria );
810  }
811  }
812  if ( !aRowFilter.isEmpty() )
813  {
814  if ( !aFilter.isEmpty() )
815  aFilter.append( " OR " );
816 
817  aFilter.append( "( " );
818  aFilter.append( aRowFilter.makeStringAndClear() );
819  aFilter.append( " )" );
820  }
821  }
822  }
823  catch( const Exception& )
824  {
826  aFilter.setLength(0);
827  }
828  }
829  rValue <<= aFilter.makeStringAndClear();
830  }
831  break;
832 
834  rValue <<= m_xFormOperations;
835  break;
836  }
837 }
838 
839 
840 Reference< XPropertySetInfo > FormController::getPropertySetInfo()
841 {
842  static Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
843  return xInfo;
844 }
845 
846 
847 void FormController::fillProperties(
848  Sequence< Property >& /* [out] */ _rProps,
849  Sequence< Property >& /* [out] */ /*_rAggregateProps*/
850  ) const
851 {
852  _rProps.realloc(2);
853  sal_Int32 nPos = 0;
854  Property* pDesc = _rProps.getArray();
855 
856  pDesc[nPos++] = Property(FM_PROP_FILTER, FM_ATTR_FILTER,
858  PropertyAttribute::READONLY);
861  PropertyAttribute::READONLY);
862 }
863 
864 
865 ::cppu::IPropertyArrayHelper& FormController::getInfoHelper()
866 {
867  return *getArrayHelper();
868 }
869 
870 // XFilterController
871 
872 void SAL_CALL FormController::addFilterControllerListener( const Reference< XFilterControllerListener >& Listener )
873 {
874  m_aFilterListeners.addInterface( Listener );
875 }
876 
877 
878 void SAL_CALL FormController::removeFilterControllerListener( const Reference< XFilterControllerListener >& Listener )
879 {
880  m_aFilterListeners.removeInterface( Listener );
881 }
882 
883 
884 ::sal_Int32 SAL_CALL FormController::getFilterComponents()
885 {
886  ::osl::MutexGuard aGuard( m_aMutex );
887  impl_checkDisposed_throw();
888 
889  return m_aFilterComponents.size();
890 }
891 
892 
893 ::sal_Int32 SAL_CALL FormController::getDisjunctiveTerms()
894 {
895  ::osl::MutexGuard aGuard( m_aMutex );
896  impl_checkDisposed_throw();
897 
898  return m_aFilterRows.size();
899 }
900 
901 
902 void SAL_CALL FormController::setPredicateExpression( ::sal_Int32 Component, ::sal_Int32 Term, const OUString& PredicateExpression )
903 {
904  ::osl::MutexGuard aGuard( m_aMutex );
905  impl_checkDisposed_throw();
906 
907  if ( ( Component < 0 ) || ( Component >= getFilterComponents() ) || ( Term < 0 ) || ( Term >= getDisjunctiveTerms() ) )
908  throw IndexOutOfBoundsException( OUString(), *this );
909 
910  Reference< XTextComponent > xText( m_aFilterComponents[ Component ] );
911  xText->setText( PredicateExpression );
912 
913  FmFilterRow& rFilterRow = m_aFilterRows[ Term ];
914  if ( !PredicateExpression.isEmpty() )
915  rFilterRow[ xText ] = PredicateExpression;
916  else
917  rFilterRow.erase( xText );
918 }
919 
920 
921 Reference< XControl > FormController::getFilterComponent( ::sal_Int32 Component )
922 {
923  ::osl::MutexGuard aGuard( m_aMutex );
924  impl_checkDisposed_throw();
925 
926  if ( ( Component < 0 ) || ( Component >= getFilterComponents() ) )
927  throw IndexOutOfBoundsException( OUString(), *this );
928 
929  return Reference< XControl >( m_aFilterComponents[ Component ], UNO_QUERY );
930 }
931 
932 
933 Sequence< Sequence< OUString > > FormController::getPredicateExpressions()
934 {
935  ::osl::MutexGuard aGuard( m_aMutex );
936  impl_checkDisposed_throw();
937 
938  Sequence< Sequence< OUString > > aExpressions( m_aFilterRows.size() );
939  sal_Int32 termIndex = 0;
940  for (const FmFilterRow& rRow : m_aFilterRows)
941  {
942  Sequence< OUString > aConjunction( m_aFilterComponents.size() );
943  sal_Int32 componentIndex = 0;
944  for (const auto& rComp : m_aFilterComponents)
945  {
946  FmFilterRow::const_iterator predicate = rRow.find( rComp );
947  if ( predicate != rRow.end() )
948  aConjunction[ componentIndex ] = predicate->second;
949  ++componentIndex;
950  }
951 
952  aExpressions[ termIndex ] = aConjunction;
953  ++termIndex;
954  }
955 
956  return aExpressions;
957 }
958 
959 
960 void SAL_CALL FormController::removeDisjunctiveTerm( ::sal_Int32 Term )
961 {
962  // SYNCHRONIZED -->
963  ::osl::ClearableMutexGuard aGuard( m_aMutex );
964  impl_checkDisposed_throw();
965 
966  if ( ( Term < 0 ) || ( Term >= getDisjunctiveTerms() ) )
967  throw IndexOutOfBoundsException( OUString(), *this );
968 
969  // if the to-be-deleted row is our current row, we need to shift
970  if ( Term == m_nCurrentFilterPosition )
971  {
972  if ( m_nCurrentFilterPosition < sal_Int32( m_aFilterRows.size() - 1 ) )
973  ++m_nCurrentFilterPosition;
974  else
975  --m_nCurrentFilterPosition;
976  }
977 
978  FmFilterRows::iterator pos = m_aFilterRows.begin() + Term;
979  m_aFilterRows.erase( pos );
980 
981  // adjust m_nCurrentFilterPosition if the removed row preceded it
982  if ( Term < m_nCurrentFilterPosition )
983  --m_nCurrentFilterPosition;
984 
985  SAL_WARN_IF( !( ( m_nCurrentFilterPosition < 0 ) != ( m_aFilterRows.empty() ) ),
986  "svx.form", "FormController::removeDisjunctiveTerm: inconsistency!" );
987 
988  // update the texts in the filter controls
989  impl_setTextOnAllFilter_throw();
990 
991  FilterEvent aEvent;
992  aEvent.Source = *this;
993  aEvent.DisjunctiveTerm = Term;
994  aGuard.clear();
995  // <-- SYNCHRONIZED
996 
997  m_aFilterListeners.notifyEach( &XFilterControllerListener::disjunctiveTermRemoved, aEvent );
998 }
999 
1000 
1001 void SAL_CALL FormController::appendEmptyDisjunctiveTerm()
1002 {
1003  // SYNCHRONIZED -->
1004  ::osl::ClearableMutexGuard aGuard( m_aMutex );
1005  impl_checkDisposed_throw();
1006 
1007  impl_appendEmptyFilterRow( aGuard );
1008  // <-- SYNCHRONIZED
1009 }
1010 
1011 
1012 ::sal_Int32 SAL_CALL FormController::getActiveTerm()
1013 {
1014  ::osl::MutexGuard aGuard( m_aMutex );
1015  impl_checkDisposed_throw();
1016 
1017  return m_nCurrentFilterPosition;
1018 }
1019 
1020 
1021 void SAL_CALL FormController::setActiveTerm( ::sal_Int32 ActiveTerm )
1022 {
1023  ::osl::MutexGuard aGuard( m_aMutex );
1024  impl_checkDisposed_throw();
1025 
1026  if ( ( ActiveTerm < 0 ) || ( ActiveTerm >= getDisjunctiveTerms() ) )
1027  throw IndexOutOfBoundsException( OUString(), *this );
1028 
1029  if ( ActiveTerm == getActiveTerm() )
1030  return;
1031 
1032  m_nCurrentFilterPosition = ActiveTerm;
1033  impl_setTextOnAllFilter_throw();
1034 }
1035 
1036 // XElementAccess
1037 
1038 sal_Bool SAL_CALL FormController::hasElements()
1039 {
1040  ::osl::MutexGuard aGuard( m_aMutex );
1041  return !m_aChildren.empty();
1042 }
1043 
1044 
1045 Type SAL_CALL FormController::getElementType()
1046 {
1048 
1049 }
1050 
1051 // XEnumerationAccess
1052 
1053 Reference< XEnumeration > SAL_CALL FormController::createEnumeration()
1054 {
1055  ::osl::MutexGuard aGuard( m_aMutex );
1056  return new ::comphelper::OEnumerationByIndex(this);
1057 }
1058 
1059 // XIndexAccess
1060 
1061 sal_Int32 SAL_CALL FormController::getCount()
1062 {
1063  ::osl::MutexGuard aGuard( m_aMutex );
1064  return m_aChildren.size();
1065 }
1066 
1067 
1068 Any SAL_CALL FormController::getByIndex(sal_Int32 Index)
1069 {
1070  ::osl::MutexGuard aGuard( m_aMutex );
1071  if (Index < 0 ||
1072  Index >= static_cast<sal_Int32>(m_aChildren.size()))
1073  throw IndexOutOfBoundsException();
1074 
1075  return makeAny( m_aChildren[ Index ] );
1076 }
1077 
1078 // EventListener
1079 
1080 void SAL_CALL FormController::disposing(const EventObject& e)
1081 {
1082  // has the container been disposed
1083  ::osl::MutexGuard aGuard( m_aMutex );
1084  Reference< XControlContainer > xContainer(e.Source, UNO_QUERY);
1085  if (xContainer.is())
1086  {
1087  setContainer(Reference< XControlContainer > ());
1088  }
1089  else
1090  {
1091  // has a control been disposed
1092  Reference< XControl > xControl(e.Source, UNO_QUERY);
1093  if (xControl.is())
1094  {
1095  if (getContainer().is())
1096  removeControl(xControl);
1097  }
1098  }
1099 }
1100 
1101 // OComponentHelper
1102 
1103 void FormController::disposeAllFeaturesAndDispatchers()
1104 {
1105  for (auto& rDispatcher : m_aFeatureDispatchers)
1106  {
1107  try
1108  {
1109  ::comphelper::disposeComponent( rDispatcher.second );
1110  }
1111  catch( const Exception& )
1112  {
1113  DBG_UNHANDLED_EXCEPTION("svx");
1114  }
1115  }
1116  m_aFeatureDispatchers.clear();
1117 }
1118 
1119 
1120 void FormController::disposing()
1121 {
1122  EventObject aEvt( *this );
1123 
1124  // if we're still active, simulate a "deactivated" event
1125  if ( m_xActiveControl.is() )
1126  m_aActivateListeners.notifyEach( &XFormControllerListener::formDeactivated, aEvt );
1127 
1128  // notify all our listeners
1129  m_aActivateListeners.disposeAndClear(aEvt);
1130  m_aModifyListeners.disposeAndClear(aEvt);
1131  m_aErrorListeners.disposeAndClear(aEvt);
1132  m_aDeleteListeners.disposeAndClear(aEvt);
1133  m_aRowSetApproveListeners.disposeAndClear(aEvt);
1134  m_aParameterListeners.disposeAndClear(aEvt);
1135  m_aFilterListeners.disposeAndClear(aEvt);
1136 
1137  removeBoundFieldListener();
1138  stopFiltering();
1139 
1140  m_aControlBorderManager.restoreAll();
1141 
1142  m_aFilterRows.clear();
1143 
1144  ::osl::MutexGuard aGuard( m_aMutex );
1145  m_xActiveControl = nullptr;
1146  implSetCurrentControl( nullptr );
1147 
1148  // clean up our children
1149  for (const auto& rpChild : m_aChildren)
1150  {
1151  // search the position of the model within the form
1152  Reference< XFormComponent > xForm(rpChild->getModel(), UNO_QUERY);
1153  sal_uInt32 nPos = m_xModelAsIndex->getCount();
1154  Reference< XFormComponent > xTemp;
1155  for( ; nPos; )
1156  {
1157 
1158  m_xModelAsIndex->getByIndex( --nPos ) >>= xTemp;
1159  if ( xForm.get() == xTemp.get() )
1160  {
1161  Reference< XInterface > xIfc( rpChild, UNO_QUERY );
1162  m_xModelAsManager->detach( nPos, xIfc );
1163  break;
1164  }
1165  }
1166 
1167  Reference< XComponent > (rpChild, UNO_QUERY_THROW)->dispose();
1168  }
1169  m_aChildren.clear();
1170 
1171  disposeAllFeaturesAndDispatchers();
1172 
1173  if ( m_xFormOperations.is() )
1174  m_xFormOperations->dispose();
1175  m_xFormOperations.clear();
1176 
1177  if (m_bDBConnection)
1178  unload();
1179 
1180  setContainer( nullptr );
1181  setModel( nullptr );
1182  setParent( nullptr );
1183 
1184  ::comphelper::disposeComponent( m_xComposer );
1185 
1186  m_bDBConnection = false;
1187 }
1188 
1189 
1190 namespace
1191 {
1192  bool lcl_shouldUseDynamicControlBorder( const Reference< XInterface >& _rxForm, const Any& _rDynamicColorProp )
1193  {
1194  bool bDoUse = false;
1195  if ( !( _rDynamicColorProp >>= bDoUse ) )
1196  {
1197  DocumentType eDocType = DocumentClassification::classifyHostDocument( _rxForm );
1198  return ControlLayouter::useDynamicBorderColor( eDocType );
1199  }
1200  return bDoUse;
1201  }
1202 }
1203 
1204 
1205 void SAL_CALL FormController::propertyChange(const PropertyChangeEvent& evt)
1206 {
1207  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
1208  if ( evt.PropertyName == FM_PROP_BOUNDFIELD )
1209  {
1210  Reference<XPropertySet> xOldBound;
1211  evt.OldValue >>= xOldBound;
1212  if ( !xOldBound.is() && evt.NewValue.hasValue() )
1213  {
1214  Reference< XControlModel > xControlModel(evt.Source,UNO_QUERY);
1215  Reference< XControl > xControl = findControl(m_aControls,xControlModel,false,false);
1216  if ( xControl.is() )
1217  {
1218  startControlModifyListening( xControl );
1219  Reference<XPropertySet> xProp(xControlModel,UNO_QUERY);
1220  if ( xProp.is() )
1221  xProp->removePropertyChangeListener(FM_PROP_BOUNDFIELD, this);
1222  }
1223  }
1224  }
1225  else
1226  {
1227  bool bModifiedChanged = (evt.PropertyName == FM_PROP_ISMODIFIED);
1228  bool bNewChanged = (evt.PropertyName == FM_PROP_ISNEW);
1229  if (bModifiedChanged || bNewChanged)
1230  {
1231  ::osl::MutexGuard aGuard( m_aMutex );
1232  if (bModifiedChanged)
1233  m_bCurrentRecordModified = ::comphelper::getBOOL(evt.NewValue);
1234  else
1235  m_bCurrentRecordNew = ::comphelper::getBOOL(evt.NewValue);
1236 
1237  // toggle the locking
1238  if (m_bLocked != determineLockState())
1239  {
1240  m_bLocked = !m_bLocked;
1241  setLocks();
1242  if (isListeningForChanges())
1243  startListening();
1244  else
1245  stopListening();
1246  }
1247 
1248  if ( bNewChanged )
1249  m_aToggleEvent.Call();
1250 
1251  if (!m_bCurrentRecordModified)
1252  m_bModified = false;
1253  }
1254  else if ( evt.PropertyName == FM_PROP_DYNAMIC_CONTROL_BORDER )
1255  {
1256  bool bEnable = lcl_shouldUseDynamicControlBorder( evt.Source, evt.NewValue );
1257  if ( bEnable )
1258  {
1259  m_aControlBorderManager.enableDynamicBorderColor();
1260  if ( m_xActiveControl.is() )
1261  m_aControlBorderManager.focusGained( m_xActiveControl.get() );
1262  }
1263  else
1264  {
1265  m_aControlBorderManager.disableDynamicBorderColor();
1266  }
1267  }
1268  }
1269 }
1270 
1271 
1272 bool FormController::replaceControl( const Reference< XControl >& _rxExistentControl, const Reference< XControl >& _rxNewControl )
1273 {
1274  bool bSuccess = false;
1275  try
1276  {
1277  Reference< XIdentifierReplace > xContainer( getContainer(), UNO_QUERY );
1278  DBG_ASSERT( xContainer.is(), "FormController::replaceControl: yes, it's not required by the service description, but XIdentifierReplace would be nice!" );
1279  if ( xContainer.is() )
1280  {
1281  // look up the ID of _rxExistentControl
1282  Sequence< sal_Int32 > aIdentifiers( xContainer->getIdentifiers() );
1283  const sal_Int32* pIdentifiers = std::find_if(aIdentifiers.begin(), aIdentifiers.end(),
1284  [&xContainer, &_rxExistentControl](const sal_Int32 nId) {
1285  Reference< XControl > xCheck( xContainer->getByIdentifier( nId ), UNO_QUERY );
1286  return xCheck == _rxExistentControl;
1287  });
1288  DBG_ASSERT( pIdentifiers != aIdentifiers.end(), "FormController::replaceControl: did not find the control in the container!" );
1289  if ( pIdentifiers != aIdentifiers.end() )
1290  {
1291  bool bReplacedWasActive = ( m_xActiveControl.get() == _rxExistentControl.get() );
1292  bool bReplacedWasCurrent = ( m_xCurrentControl.get() == _rxExistentControl.get() );
1293 
1294  if ( bReplacedWasActive )
1295  {
1296  m_xActiveControl = nullptr;
1297  implSetCurrentControl( nullptr );
1298  }
1299  else if ( bReplacedWasCurrent )
1300  {
1301  implSetCurrentControl( _rxNewControl );
1302  }
1303 
1304  // carry over the model
1305  _rxNewControl->setModel( _rxExistentControl->getModel() );
1306 
1307  xContainer->replaceByIdentifer( *pIdentifiers, makeAny( _rxNewControl ) );
1308  bSuccess = true;
1309 
1310  if ( bReplacedWasActive )
1311  {
1312  Reference< XWindow > xControlWindow( _rxNewControl, UNO_QUERY );
1313  if ( xControlWindow.is() )
1314  xControlWindow->setFocus();
1315  }
1316  }
1317  }
1318  }
1319  catch( const Exception& )
1320  {
1321  DBG_UNHANDLED_EXCEPTION("svx");
1322  }
1323 
1324  Reference< XControl > xDisposeIt( bSuccess ? _rxExistentControl : _rxNewControl );
1325  ::comphelper::disposeComponent( xDisposeIt );
1326  return bSuccess;
1327 }
1328 
1329 
1330 void FormController::toggleAutoFields(bool bAutoFields)
1331 {
1332  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
1333 
1334 
1335  Sequence< Reference< XControl > > aControlsCopy( m_aControls );
1336  const Reference< XControl >* pControls = aControlsCopy.getConstArray();
1337  sal_Int32 nControls = aControlsCopy.getLength();
1338 
1339  if (bAutoFields)
1340  {
1341  // as we don't want new controls to be attached to the scripting environment
1342  // we change attach flags
1343  m_bAttachEvents = false;
1344  for (sal_Int32 i = nControls; i > 0;)
1345  {
1346  Reference< XControl > xControl = pControls[--i];
1347  if (xControl.is())
1348  {
1349  Reference< XPropertySet > xSet(xControl->getModel(), UNO_QUERY);
1350  if (xSet.is() && ::comphelper::hasProperty(FM_PROP_BOUNDFIELD, xSet))
1351  {
1352  // does the model use a bound field ?
1354  xSet->getPropertyValue(FM_PROP_BOUNDFIELD) >>= xField;
1355 
1356  // is it an autofield?
1357  if ( xField.is()
1358  && ::comphelper::hasProperty( FM_PROP_AUTOINCREMENT, xField )
1359  && ::comphelper::getBOOL( xField->getPropertyValue( FM_PROP_AUTOINCREMENT ) )
1360  )
1361  {
1362  replaceControl( xControl, new FmXAutoControl() );
1363  }
1364  }
1365  }
1366  }
1367  m_bAttachEvents = true;
1368  }
1369  else
1370  {
1371  m_bDetachEvents = false;
1372  for (sal_Int32 i = nControls; i > 0;)
1373  {
1374  Reference< XControl > xControl = pControls[--i];
1375  if (xControl.is())
1376  {
1377  Reference< XPropertySet > xSet(xControl->getModel(), UNO_QUERY);
1378  if (xSet.is() && ::comphelper::hasProperty(FM_PROP_BOUNDFIELD, xSet))
1379  {
1380  // does the model use a bound field ?
1382  xSet->getPropertyValue(FM_PROP_BOUNDFIELD) >>= xField;
1383 
1384  // is it an autofield?
1385  if ( xField.is()
1386  && ::comphelper::hasProperty( FM_PROP_AUTOINCREMENT, xField )
1387  && ::comphelper::getBOOL( xField->getPropertyValue(FM_PROP_AUTOINCREMENT ) )
1388  )
1389  {
1390  OUString sServiceName;
1391  OSL_VERIFY( xSet->getPropertyValue( FM_PROP_DEFAULTCONTROL ) >>= sServiceName );
1392  Reference< XControl > xNewControl( m_xComponentContext->getServiceManager()->createInstanceWithContext( sServiceName, m_xComponentContext ), UNO_QUERY );
1393  replaceControl( xControl, xNewControl );
1394  }
1395  }
1396  }
1397  }
1398  m_bDetachEvents = true;
1399  }
1400 }
1401 
1402 
1403 IMPL_LINK_NOARG(FormController, OnToggleAutoFields, void*, void)
1404 {
1405  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
1406 
1407  toggleAutoFields(m_bCurrentRecordNew);
1408 }
1409 
1410 // XTextListener
1411 void SAL_CALL FormController::textChanged(const TextEvent& e)
1412 {
1413  // SYNCHRONIZED -->
1414  ::osl::ClearableMutexGuard aGuard( m_aMutex );
1415  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
1416  if ( !m_bFiltering )
1417  {
1418  impl_onModify();
1419  return;
1420  }
1421 
1422  if ( m_bSuspendFilterTextListening )
1423  return;
1424 
1425  Reference< XTextComponent > xText(e.Source,UNO_QUERY);
1426  OUString aText = xText->getText();
1427 
1428  if ( m_aFilterRows.empty() )
1429  appendEmptyDisjunctiveTerm();
1430 
1431  // find the current row
1432  if ( ( m_nCurrentFilterPosition < 0 ) || ( o3tl::make_unsigned(m_nCurrentFilterPosition) >= m_aFilterRows.size() ) )
1433  {
1434  OSL_ENSURE( false, "FormController::textChanged: m_nCurrentFilterPosition is wrong!" );
1435  return;
1436  }
1437 
1438  FmFilterRow& rRow = m_aFilterRows[ m_nCurrentFilterPosition ];
1439 
1440  // do we have a new filter
1441  if (!aText.isEmpty())
1442  rRow[xText] = aText;
1443  else
1444  {
1445  // do we have the control in the row
1446  FmFilterRow::iterator iter = rRow.find(xText);
1447  // erase the entry out of the row
1448  if (iter != rRow.end())
1449  rRow.erase(iter);
1450  }
1451 
1452  // multiplex the event to our FilterControllerListeners
1453  FilterEvent aEvent;
1454  aEvent.Source = *this;
1455  aEvent.FilterComponent = ::std::find( m_aFilterComponents.begin(), m_aFilterComponents.end(), xText ) - m_aFilterComponents.begin();
1456  aEvent.DisjunctiveTerm = getActiveTerm();
1457  aEvent.PredicateExpression = aText;
1458 
1459  aGuard.clear();
1460  // <-- SYNCHRONIZED
1461 
1462  // notify the changed filter expression
1463  m_aFilterListeners.notifyEach( &XFilterControllerListener::predicateExpressionChanged, aEvent );
1464 }
1465 
1466 // XItemListener
1467 void SAL_CALL FormController::itemStateChanged(const ItemEvent& /*rEvent*/)
1468 {
1469  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
1470  impl_onModify();
1471 }
1472 
1473 // XModificationBroadcaster
1474 void SAL_CALL FormController::addModifyListener(const Reference< XModifyListener > & l)
1475 {
1476  ::osl::MutexGuard aGuard( m_aMutex );
1477  impl_checkDisposed_throw();
1478  m_aModifyListeners.addInterface( l );
1479 }
1480 
1481 void FormController::removeModifyListener(const Reference< XModifyListener > & l)
1482 {
1483  ::osl::MutexGuard aGuard( m_aMutex );
1484  impl_checkDisposed_throw();
1485  m_aModifyListeners.removeInterface( l );
1486 }
1487 
1488 // XModificationListener
1489 void FormController::modified( const EventObject& _rEvent )
1490 {
1491  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
1492 
1493  try
1494  {
1495  if ( _rEvent.Source != m_xActiveControl )
1496  { // let this control grab the focus
1497  // (this case may happen if somebody moves the scroll wheel of the mouse over a control
1498  // which does not have the focus)
1499  // 85511 - 29.05.2001 - frank.schoenheit@germany.sun.com
1500 
1501  // also, it happens when an image control gets a new image by double-clicking it
1502  // #i88458# / 2009-01-12 / frank.schoenheit@sun.com
1503  Reference< XWindow > xControlWindow( _rEvent.Source, UNO_QUERY_THROW );
1504  xControlWindow->setFocus();
1505  }
1506  }
1507  catch( const Exception& )
1508  {
1509  DBG_UNHANDLED_EXCEPTION("svx");
1510  }
1511 
1512  impl_onModify();
1513 }
1514 
1515 void FormController::impl_checkDisposed_throw() const
1516 {
1517  if ( impl_isDisposed_nofail() )
1518  throw DisposedException( OUString(), *const_cast< FormController* >( this ) );
1519 }
1520 
1521 void FormController::impl_onModify()
1522 {
1523  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
1524 
1525  {
1526  ::osl::MutexGuard aGuard( m_aMutex );
1527  if ( !m_bModified )
1528  m_bModified = true;
1529  }
1530 
1531  EventObject aEvt(static_cast<cppu::OWeakObject*>(this));
1532  m_aModifyListeners.notifyEach( &XModifyListener::modified, aEvt );
1533 }
1534 
1535 void FormController::impl_addFilterRow( const FmFilterRow& _row )
1536 {
1537  m_aFilterRows.push_back( _row );
1538 
1539  if ( m_aFilterRows.size() == 1 )
1540  { // that's the first row ever
1541  OSL_ENSURE( m_nCurrentFilterPosition == -1, "FormController::impl_addFilterRow: inconsistency!" );
1542  m_nCurrentFilterPosition = 0;
1543  }
1544 }
1545 
1546 void FormController::impl_appendEmptyFilterRow( ::osl::ClearableMutexGuard& _rClearBeforeNotify )
1547 {
1548  // SYNCHRONIZED -->
1549  impl_addFilterRow( FmFilterRow() );
1550 
1551  // notify the listeners
1552  FilterEvent aEvent;
1553  aEvent.Source = *this;
1554  aEvent.DisjunctiveTerm = static_cast<sal_Int32>(m_aFilterRows.size()) - 1;
1555  _rClearBeforeNotify.clear();
1556  // <-- SYNCHRONIZED
1557  m_aFilterListeners.notifyEach( &XFilterControllerListener::disjunctiveTermAdded, aEvent );
1558 }
1559 
1560 bool FormController::determineLockState() const
1561 {
1562  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
1563  // a.) in filter mode we are always locked
1564  // b.) if we have no valid model or our model (a result set) is not alive -> we're locked
1565  // c.) if we are inserting everything is OK and we are not locked
1566  // d.) if are not updatable or on invalid position
1567  Reference< XResultSet > xResultSet(m_xModelAsIndex, UNO_QUERY);
1568  if (m_bFiltering || !xResultSet.is() || !isRowSetAlive(xResultSet))
1569  return true;
1570  else
1571  return !(m_bCanInsert && m_bCurrentRecordNew)
1572  && (xResultSet->isBeforeFirst() || xResultSet->isAfterLast() || xResultSet->rowDeleted() || !m_bCanUpdate);
1573 }
1574 
1575 // FocusListener
1576 void FormController::focusGained(const FocusEvent& e)
1577 {
1578  // SYNCHRONIZED -->
1579  ::osl::ClearableMutexGuard aGuard( m_aMutex );
1580  impl_checkDisposed_throw();
1581 
1582  m_aControlBorderManager.focusGained( e.Source );
1583 
1584  Reference< XControl > xControl(e.Source, UNO_QUERY);
1585  if (m_bDBConnection)
1586  {
1587  // do we need to keep the locking of the commit
1588  // we hold the lock as long as the control differs from the current
1589  // otherwise we disabled the lock
1590  m_bCommitLock = m_bCommitLock && xControl.get() != m_xCurrentControl.get();
1591  if (m_bCommitLock)
1592  return;
1593 
1594  // when do we have to commit a value to form or a filter
1595  // a.) if the current value is modified
1596  // b.) there must be a current control
1597  // c.) and it must be different from the new focus owning control or
1598  // d.) the focus is moving around (so we have only one control)
1599 
1600  if ( ( m_bModified || m_bFiltering )
1601  && m_xCurrentControl.is()
1602  && ( ( xControl.get() != m_xCurrentControl.get() )
1603  || ( ( e.FocusFlags & FocusChangeReason::AROUND )
1604  && ( m_bCycle || m_bFiltering )
1605  )
1606  )
1607  )
1608  {
1609  // check the old control if the content is ok
1610 #if OSL_DEBUG_LEVEL > 0 && !defined NDEBUG
1611  Reference< XBoundControl > xLockingTest(m_xCurrentControl, UNO_QUERY);
1612  bool bControlIsLocked = xLockingTest.is() && xLockingTest->getLock();
1613  assert(!bControlIsLocked && "FormController::Gained: I'm modified and the current control is locked ? How this ?");
1614  // normally, a locked control should not be modified, so probably my bModified must
1615  // have been set from a different context, which I would not understand ...
1616 #endif
1617  DBG_ASSERT(m_xCurrentControl.is(), "no CurrentControl set");
1618  // first the control ask if it supports the IFace
1619  Reference< XBoundComponent > xBound(m_xCurrentControl, UNO_QUERY);
1620  if (!xBound.is() && m_xCurrentControl.is())
1621  xBound.set(m_xCurrentControl->getModel(), UNO_QUERY);
1622 
1623  // lock if we lose the focus during commit
1624  m_bCommitLock = true;
1625 
1626  // commit unsuccessful, reset focus
1627  if (xBound.is() && !xBound->commit())
1628  {
1629  // the commit failed and we don't commit again until the current control
1630  // which couldn't be commit gains the focus again
1631  Reference< XWindow > xWindow(m_xCurrentControl, UNO_QUERY);
1632  if (xWindow.is())
1633  xWindow->setFocus();
1634  return;
1635  }
1636  else
1637  {
1638  m_bModified = false;
1639  m_bCommitLock = false;
1640  }
1641  }
1642 
1643  if (!m_bFiltering && m_bCycle && (e.FocusFlags & FocusChangeReason::AROUND) && m_xCurrentControl.is())
1644  {
1645  OSL_ENSURE( m_xFormOperations.is(), "FormController::focusGained: hmm?" );
1646  // should have been created in setModel
1647  try
1648  {
1649  if ( e.FocusFlags & FocusChangeReason::FORWARD )
1650  {
1651  if ( m_xFormOperations.is() && m_xFormOperations->isEnabled( FormFeature::MoveToNext ) )
1652  m_xFormOperations->execute( FormFeature::MoveToNext );
1653  }
1654  else // backward
1655  {
1656  if ( m_xFormOperations.is() && m_xFormOperations->isEnabled( FormFeature::MoveToPrevious ) )
1657  m_xFormOperations->execute( FormFeature::MoveToPrevious );
1658  }
1659  }
1660  catch ( const Exception& )
1661  {
1662  // don't handle this any further. That's an ... admissible error.
1663  DBG_UNHANDLED_EXCEPTION("svx");
1664  }
1665  }
1666  }
1667 
1668  // still one and the same control
1669  if ( ( m_xActiveControl == xControl )
1670  && ( xControl == m_xCurrentControl )
1671  )
1672  {
1673  DBG_ASSERT(m_xCurrentControl.is(), "No CurrentControl selected");
1674  return;
1675  }
1676 
1677  bool bActivated = !m_xActiveControl.is() && xControl.is();
1678 
1679  m_xActiveControl = xControl;
1680 
1681  implSetCurrentControl( xControl );
1682  SAL_WARN_IF( !m_xCurrentControl.is(), "svx.form", "implSetCurrentControl did nonsense!" );
1683 
1684  if ( bActivated )
1685  {
1686  // (asynchronously) call activation handlers
1687  m_aActivationEvent.Call();
1688 
1689  // call modify listeners
1690  if ( m_bModified )
1691  m_aModifyListeners.notifyEach( &XModifyListener::modified, EventObject( *this ) );
1692  }
1693 
1694  // invalidate all features which depend on the currently focused control
1695  if ( m_bDBConnection && !m_bFiltering )
1696  implInvalidateCurrentControlDependentFeatures();
1697 
1698  if ( !m_xCurrentControl.is() )
1699  return;
1700 
1701  // control gets focus, then possibly in the visible range
1702  Reference< XFormControllerContext > xContext( m_xFormControllerContext );
1703  Reference< XControl > xCurrentControl( m_xCurrentControl );
1704  aGuard.clear();
1705  // <-- SYNCHRONIZED
1706 
1707  if ( xContext.is() )
1708  xContext->makeVisible( xCurrentControl );
1709 }
1710 
1711 IMPL_LINK_NOARG( FormController, OnActivated, void*, void )
1712 {
1713  EventObject aEvent;
1714  aEvent.Source = *this;
1715  m_aActivateListeners.notifyEach( &XFormControllerListener::formActivated, aEvent );
1716 }
1717 
1718 IMPL_LINK_NOARG( FormController, OnDeactivated, void*, void )
1719 {
1720  EventObject aEvent;
1721  aEvent.Source = *this;
1722  m_aActivateListeners.notifyEach( &XFormControllerListener::formDeactivated, aEvent );
1723 }
1724 
1725 void FormController::focusLost(const FocusEvent& e)
1726 {
1727  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
1728 
1729  m_aControlBorderManager.focusLost( e.Source );
1730 
1731  Reference< XWindowPeer > xNext(e.NextFocus, UNO_QUERY);
1732  // if focus hasn't passed to some other window, e.g. focus in a welded item, don't deactivate
1733  if (!xNext)
1734  return;
1735  Reference< XControl > xNextControl = isInList(xNext);
1736  if (!xNextControl.is())
1737  {
1738  m_xActiveControl = nullptr;
1739  m_aDeactivationEvent.Call();
1740  }
1741 }
1742 
1743 void SAL_CALL FormController::mousePressed( const awt::MouseEvent& /*_rEvent*/ )
1744 {
1745  // not interested in
1746 }
1747 
1748 void SAL_CALL FormController::mouseReleased( const awt::MouseEvent& /*_rEvent*/ )
1749 {
1750  // not interested in
1751 }
1752 
1753 void SAL_CALL FormController::mouseEntered( const awt::MouseEvent& _rEvent )
1754 {
1755  m_aControlBorderManager.mouseEntered( _rEvent.Source );
1756 }
1757 
1758 void SAL_CALL FormController::mouseExited( const awt::MouseEvent& _rEvent )
1759 {
1760  m_aControlBorderManager.mouseExited( _rEvent.Source );
1761 }
1762 
1763 void SAL_CALL FormController::componentValidityChanged( const EventObject& _rSource )
1764 {
1765  Reference< XControl > xControl( findControl( m_aControls, Reference< XControlModel >( _rSource.Source, UNO_QUERY ), false, false ) );
1766  Reference< XValidatableFormComponent > xValidatable( _rSource.Source, UNO_QUERY );
1767 
1768  OSL_ENSURE( xControl.is() && xValidatable.is(), "FormController::componentValidityChanged: huh?" );
1769 
1770  if ( xControl.is() && xValidatable.is() )
1771  m_aControlBorderManager.validityChanged( xControl, xValidatable );
1772 }
1773 
1774 
1775 void FormController::setModel(const Reference< XTabControllerModel > & Model)
1776 {
1777  ::osl::MutexGuard aGuard( m_aMutex );
1778  impl_checkDisposed_throw();
1779 
1780  DBG_ASSERT(m_xTabController.is(), "FormController::setModel : invalid aggregate !");
1781 
1782  try
1783  {
1784  // disconnect from the old model
1785  if (m_xModelAsIndex.is())
1786  {
1787  if (m_bDBConnection)
1788  {
1789  // we are currently working on the model
1790  EventObject aEvt(m_xModelAsIndex);
1791  unloaded(aEvt);
1792  }
1793 
1794  Reference< XLoadable > xForm(m_xModelAsIndex, UNO_QUERY);
1795  if (xForm.is())
1796  xForm->removeLoadListener(this);
1797 
1798  Reference< XSQLErrorBroadcaster > xBroadcaster(m_xModelAsIndex, UNO_QUERY);
1799  if (xBroadcaster.is())
1800  xBroadcaster->removeSQLErrorListener(this);
1801 
1802  Reference< XDatabaseParameterBroadcaster > xParamBroadcaster(m_xModelAsIndex, UNO_QUERY);
1803  if (xParamBroadcaster.is())
1804  xParamBroadcaster->removeParameterListener(this);
1805 
1806  }
1807 
1808  disposeAllFeaturesAndDispatchers();
1809 
1810  if ( m_xFormOperations.is() )
1811  m_xFormOperations->dispose();
1812  m_xFormOperations.clear();
1813 
1814  // set the new model wait for the load event
1815  if (m_xTabController.is())
1816  m_xTabController->setModel(Model);
1817  m_xModelAsIndex.set(Model, UNO_QUERY);
1818  m_xModelAsManager.set(Model, UNO_QUERY);
1819 
1820  // only if both ifaces exit, the controller will work successful
1821  if (!m_xModelAsIndex.is() || !m_xModelAsManager.is())
1822  {
1823  m_xModelAsManager = nullptr;
1824  m_xModelAsIndex = nullptr;
1825  }
1826 
1827  if (m_xModelAsIndex.is())
1828  {
1829  // re-create m_xFormOperations
1830  m_xFormOperations = FormOperations::createWithFormController( m_xComponentContext, this );
1831  m_xFormOperations->setFeatureInvalidation( this );
1832 
1833  // adding load and ui interaction listeners
1834  Reference< XLoadable > xForm(Model, UNO_QUERY);
1835  if (xForm.is())
1836  xForm->addLoadListener(this);
1837 
1838  Reference< XSQLErrorBroadcaster > xBroadcaster(Model, UNO_QUERY);
1839  if (xBroadcaster.is())
1840  xBroadcaster->addSQLErrorListener(this);
1841 
1842  Reference< XDatabaseParameterBroadcaster > xParamBroadcaster(Model, UNO_QUERY);
1843  if (xParamBroadcaster.is())
1844  xParamBroadcaster->addParameterListener(this);
1845 
1846  // well, is the database already loaded?
1847  // then we have to simulate a load event
1848  Reference< XLoadable > xCursor(m_xModelAsIndex, UNO_QUERY);
1849  if (xCursor.is() && xCursor->isLoaded())
1850  {
1851  EventObject aEvt(xCursor);
1852  loaded(aEvt);
1853  }
1854 
1855  Reference< XPropertySet > xModelProps( m_xModelAsIndex, UNO_QUERY );
1856  Reference< XPropertySetInfo > xPropInfo( xModelProps->getPropertySetInfo() );
1857  if ( xPropInfo.is()
1858  && xPropInfo->hasPropertyByName( FM_PROP_DYNAMIC_CONTROL_BORDER )
1859  && xPropInfo->hasPropertyByName( FM_PROP_CONTROL_BORDER_COLOR_FOCUS )
1860  && xPropInfo->hasPropertyByName( FM_PROP_CONTROL_BORDER_COLOR_MOUSE )
1861  && xPropInfo->hasPropertyByName( FM_PROP_CONTROL_BORDER_COLOR_INVALID )
1862  )
1863  {
1864  bool bEnableDynamicControlBorder = lcl_shouldUseDynamicControlBorder(
1865  xModelProps.get(), xModelProps->getPropertyValue( FM_PROP_DYNAMIC_CONTROL_BORDER ) );
1866  if ( bEnableDynamicControlBorder )
1867  m_aControlBorderManager.enableDynamicBorderColor();
1868  else
1869  m_aControlBorderManager.disableDynamicBorderColor();
1870 
1871  Color nColor;
1872  if ( xModelProps->getPropertyValue( FM_PROP_CONTROL_BORDER_COLOR_FOCUS ) >>= nColor )
1873  m_aControlBorderManager.setStatusColor( ControlStatus::Focused, nColor );
1874  if ( xModelProps->getPropertyValue( FM_PROP_CONTROL_BORDER_COLOR_MOUSE ) >>= nColor )
1875  m_aControlBorderManager.setStatusColor( ControlStatus::MouseHover, nColor );
1876  if ( xModelProps->getPropertyValue( FM_PROP_CONTROL_BORDER_COLOR_INVALID ) >>= nColor )
1877  m_aControlBorderManager.setStatusColor( ControlStatus::Invalid, nColor );
1878  }
1879  }
1880  }
1881  catch( const Exception& )
1882  {
1883  DBG_UNHANDLED_EXCEPTION("svx");
1884  }
1885 }
1886 
1887 
1888 Reference< XTabControllerModel > FormController::getModel()
1889 {
1890  ::osl::MutexGuard aGuard( m_aMutex );
1891  impl_checkDisposed_throw();
1892 
1893  DBG_ASSERT(m_xTabController.is(), "FormController::getModel : invalid aggregate !");
1894  if (!m_xTabController.is())
1895  return Reference< XTabControllerModel > ();
1896  return m_xTabController->getModel();
1897 }
1898 
1899 
1900 void FormController::addToEventAttacher(const Reference< XControl > & xControl)
1901 {
1902  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
1903  OSL_ENSURE( xControl.is(), "FormController::addToEventAttacher: invalid control - how did you reach this?" );
1904  if ( !xControl.is() )
1905  return; /* throw IllegalArgumentException(); */
1906 
1907  // register at the event attacher
1908  Reference< XFormComponent > xComp(xControl->getModel(), UNO_QUERY);
1909  if (!(xComp.is() && m_xModelAsIndex.is()))
1910  return;
1911 
1912  // and look for the position of the ControlModel in it
1913  sal_uInt32 nPos = m_xModelAsIndex->getCount();
1914  Reference< XFormComponent > xTemp;
1915  for( ; nPos; )
1916  {
1917  m_xModelAsIndex->getByIndex(--nPos) >>= xTemp;
1918  if (xComp.get() == xTemp.get())
1919  {
1920  m_xModelAsManager->attach( nPos, Reference<XInterface>( xControl, UNO_QUERY ), makeAny(xControl) );
1921  break;
1922  }
1923  }
1924 }
1925 
1926 
1927 void FormController::removeFromEventAttacher(const Reference< XControl > & xControl)
1928 {
1929  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
1930  OSL_ENSURE( xControl.is(), "FormController::removeFromEventAttacher: invalid control - how did you reach this?" );
1931  if ( !xControl.is() )
1932  return; /* throw IllegalArgumentException(); */
1933 
1934  // register at the event attacher
1935  Reference< XFormComponent > xComp(xControl->getModel(), UNO_QUERY);
1936  if ( !(xComp.is() && m_xModelAsIndex.is()) )
1937  return;
1938 
1939  // and look for the position of the ControlModel in it
1940  sal_uInt32 nPos = m_xModelAsIndex->getCount();
1941  Reference< XFormComponent > xTemp;
1942  for( ; nPos; )
1943  {
1944  m_xModelAsIndex->getByIndex(--nPos) >>= xTemp;
1945  if (xComp.get() == xTemp.get())
1946  {
1947  m_xModelAsManager->detach( nPos, Reference<XInterface>( xControl, UNO_QUERY ) );
1948  break;
1949  }
1950  }
1951 }
1952 
1953 
1954 void FormController::setContainer(const Reference< XControlContainer > & xContainer)
1955 {
1956  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
1957  Reference< XTabControllerModel > xTabModel(getModel());
1958  DBG_ASSERT(xTabModel.is() || !xContainer.is(), "No Model defined");
1959  // if we have a new container we need a model
1960  DBG_ASSERT(m_xTabController.is(), "FormController::setContainer : invalid aggregate !");
1961 
1962  ::osl::MutexGuard aGuard( m_aMutex );
1963  Reference< XContainer > xCurrentContainer;
1964  if (m_xTabController.is())
1965  xCurrentContainer.set(m_xTabController->getContainer(), UNO_QUERY);
1966  if (xCurrentContainer.is())
1967  {
1968  xCurrentContainer->removeContainerListener(this);
1969 
1970  if ( m_aTabActivationIdle.IsActive() )
1971  m_aTabActivationIdle.Stop();
1972 
1973  // clear the filter map
1974  ::std::for_each( m_aFilterComponents.begin(), m_aFilterComponents.end(), RemoveComponentTextListener( this ) );
1975  m_aFilterComponents.clear();
1976 
1977  // collecting the controls
1978  for ( const Reference< XControl >& rControl : std::as_const(m_aControls) )
1979  implControlRemoved( rControl, true );
1980 
1981  // make database-specific things
1982  if (m_bDBConnection && isListeningForChanges())
1983  stopListening();
1984 
1985  m_aControls.realloc( 0 );
1986  }
1987 
1988  if (m_xTabController.is())
1989  m_xTabController->setContainer(xContainer);
1990 
1991  // What controls belong to the container?
1992  if (xContainer.is() && xTabModel.is())
1993  {
1994  const Sequence< Reference< XControlModel > > aModels = xTabModel->getControlModels();
1995  Sequence< Reference< XControl > > aAllControls = xContainer->getControls();
1996 
1997  sal_Int32 nCount = aModels.getLength();
1998  m_aControls = Sequence< Reference< XControl > >( nCount );
1999  Reference< XControl > * pControls = m_aControls.getArray();
2000 
2001  // collecting the controls
2002  sal_Int32 j = 0;
2003  for (const Reference< XControlModel >& rModel : aModels )
2004  {
2005  Reference< XControl > xControl = findControl( aAllControls, rModel, false, true );
2006  if ( xControl.is() )
2007  {
2008  pControls[j++] = xControl;
2009  implControlInserted( xControl, true );
2010  }
2011  }
2012 
2013  // not every model had an associated control
2014  if (j != nCount)
2015  m_aControls.realloc(j);
2016 
2017  // listen at the container
2018  Reference< XContainer > xNewContainer(xContainer, UNO_QUERY);
2019  if (xNewContainer.is())
2020  xNewContainer->addContainerListener(this);
2021 
2022  // make database-specific things
2023  if (m_bDBConnection)
2024  {
2025  m_bLocked = determineLockState();
2026  setLocks();
2027  if (!isLocked())
2028  startListening();
2029  }
2030  }
2031  // the controls are in the right order
2032  m_bControlsSorted = true;
2033 }
2034 
2035 
2036 Reference< XControlContainer > FormController::getContainer()
2037 {
2038  ::osl::MutexGuard aGuard( m_aMutex );
2039  impl_checkDisposed_throw();
2040 
2041  DBG_ASSERT(m_xTabController.is(), "FormController::getContainer : invalid aggregate !");
2042  if (!m_xTabController.is())
2043  return Reference< XControlContainer > ();
2044  return m_xTabController->getContainer();
2045 }
2046 
2047 
2048 Sequence< Reference< XControl > > FormController::getControls()
2049 {
2050  ::osl::MutexGuard aGuard( m_aMutex );
2051  impl_checkDisposed_throw();
2052 
2053  if (!m_bControlsSorted)
2054  {
2055  Reference< XTabControllerModel > xModel = getModel();
2056  if (!xModel.is())
2057  return m_aControls;
2058 
2059  const Sequence< Reference< XControlModel > > aControlModels = xModel->getControlModels();
2060  sal_Int32 nModels = aControlModels.getLength();
2061 
2062  Sequence< Reference< XControl > > aNewControls(nModels);
2063 
2064  Reference< XControl > * pControls = aNewControls.getArray();
2065  Reference< XControl > xControl;
2066 
2067  // rearrange the controls according to the tab order sequence
2068  sal_Int32 j = 0;
2069  for ( const Reference< XControlModel >& rModel : aControlModels )
2070  {
2071  xControl = findControl( m_aControls, rModel, true, true );
2072  if ( xControl.is() )
2073  pControls[j++] = xControl;
2074  }
2075 
2076  // not every model had an associated control
2077  if ( j != nModels )
2078  aNewControls.realloc( j );
2079 
2080  m_aControls = aNewControls;
2081  m_bControlsSorted = true;
2082  }
2083  return m_aControls;
2084 }
2085 
2086 
2087 void FormController::autoTabOrder()
2088 {
2089  ::osl::MutexGuard aGuard( m_aMutex );
2090  impl_checkDisposed_throw();
2091 
2092  DBG_ASSERT(m_xTabController.is(), "FormController::autoTabOrder : invalid aggregate !");
2093  if (m_xTabController.is())
2094  m_xTabController->autoTabOrder();
2095 }
2096 
2097 
2098 void FormController::activateTabOrder()
2099 {
2100  ::osl::MutexGuard aGuard( m_aMutex );
2101  impl_checkDisposed_throw();
2102 
2103  DBG_ASSERT(m_xTabController.is(), "FormController::activateTabOrder : invalid aggregate !");
2104  if (m_xTabController.is())
2105  m_xTabController->activateTabOrder();
2106 }
2107 
2108 
2109 void FormController::setControlLock(const Reference< XControl > & xControl)
2110 {
2111  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2112  bool bLocked = isLocked();
2113 
2114  // It is locked
2115  // a. if the entire record is locked
2116  // b. if the associated field is locked
2117  Reference< XBoundControl > xBound(xControl, UNO_QUERY);
2118  if (!(xBound.is() &&
2119  ( (bLocked && bLocked != bool(xBound->getLock())) ||
2120  !bLocked))) // always uncheck individual fields when unlocking
2121  return;
2122 
2123  // there is a data source
2124  Reference< XPropertySet > xSet(xControl->getModel(), UNO_QUERY);
2125  if (!(xSet.is() && ::comphelper::hasProperty(FM_PROP_BOUNDFIELD, xSet)))
2126  return;
2127 
2128  // what about the ReadOnly and Enable properties
2129  bool bTouch = true;
2131  bTouch = ::comphelper::getBOOL(xSet->getPropertyValue(FM_PROP_ENABLED));
2133  bTouch = !::comphelper::getBOOL(xSet->getPropertyValue(FM_PROP_READONLY));
2134 
2135  if (!bTouch)
2136  return;
2137 
2139  xSet->getPropertyValue(FM_PROP_BOUNDFIELD) >>= xField;
2140  if (!xField.is())
2141  return;
2142 
2143  if (bLocked)
2144  xBound->setLock(bLocked);
2145  else
2146  {
2147  try
2148  {
2149  Any aVal = xField->getPropertyValue(FM_PROP_ISREADONLY);
2150  if (aVal.hasValue() && ::comphelper::getBOOL(aVal))
2151  xBound->setLock(true);
2152  else
2153  xBound->setLock(bLocked);
2154  }
2155  catch( const Exception& )
2156  {
2157  DBG_UNHANDLED_EXCEPTION("svx");
2158  }
2159 
2160  }
2161 }
2162 
2163 
2164 void FormController::setLocks()
2165 {
2166  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2167  // lock/unlock all controls connected to a data source
2168  for ( const Reference< XControl >& rControl : std::as_const(m_aControls) )
2169  setControlLock( rControl );
2170 }
2171 
2172 
2173 namespace
2174 {
2175  bool lcl_shouldListenForModifications( const Reference< XControl >& _rxControl, const Reference< XPropertyChangeListener >& _rxBoundFieldListener )
2176  {
2177  bool bShould = false;
2178 
2179  Reference< XBoundComponent > xBound( _rxControl, UNO_QUERY );
2180  if ( xBound.is() )
2181  {
2182  bShould = true;
2183  }
2184  else if ( _rxControl.is() )
2185  {
2186  Reference< XPropertySet > xModelProps( _rxControl->getModel(), UNO_QUERY );
2187  if ( xModelProps.is() && ::comphelper::hasProperty( FM_PROP_BOUNDFIELD, xModelProps ) )
2188  {
2190  xModelProps->getPropertyValue( FM_PROP_BOUNDFIELD ) >>= xField;
2191  bShould = xField.is();
2192 
2193  if ( !bShould && _rxBoundFieldListener.is() )
2194  xModelProps->addPropertyChangeListener( FM_PROP_BOUNDFIELD, _rxBoundFieldListener );
2195  }
2196  }
2197 
2198  return bShould;
2199  }
2200 }
2201 
2202 
2203 void FormController::startControlModifyListening(const Reference< XControl > & xControl)
2204 {
2205  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2206 
2207  bool bModifyListening = lcl_shouldListenForModifications( xControl, this );
2208 
2209  // artificial while
2210  while ( bModifyListening )
2211  {
2212  Reference< XModifyBroadcaster > xMod(xControl, UNO_QUERY);
2213  if (xMod.is())
2214  {
2215  xMod->addModifyListener(this);
2216  break;
2217  }
2218 
2219  // all the text to prematurely recognize a modified
2220  Reference< XTextComponent > xText(xControl, UNO_QUERY);
2221  if (xText.is())
2222  {
2223  xText->addTextListener(this);
2224  break;
2225  }
2226 
2227  Reference< XCheckBox > xBox(xControl, UNO_QUERY);
2228  if (xBox.is())
2229  {
2230  xBox->addItemListener(this);
2231  break;
2232  }
2233 
2234  Reference< XComboBox > xCbBox(xControl, UNO_QUERY);
2235  if (xCbBox.is())
2236  {
2237  xCbBox->addItemListener(this);
2238  break;
2239  }
2240 
2241  Reference< XListBox > xListBox(xControl, UNO_QUERY);
2242  if (xListBox.is())
2243  {
2244  xListBox->addItemListener(this);
2245  break;
2246  }
2247  break;
2248  }
2249 }
2250 
2251 
2252 void FormController::stopControlModifyListening(const Reference< XControl > & xControl)
2253 {
2254  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2255 
2256  bool bModifyListening = lcl_shouldListenForModifications( xControl, nullptr );
2257 
2258  // artificial while
2259  while (bModifyListening)
2260  {
2261  Reference< XModifyBroadcaster > xMod(xControl, UNO_QUERY);
2262  if (xMod.is())
2263  {
2264  xMod->removeModifyListener(this);
2265  break;
2266  }
2267  // all the text to prematurely recognize a modified
2268  Reference< XTextComponent > xText(xControl, UNO_QUERY);
2269  if (xText.is())
2270  {
2271  xText->removeTextListener(this);
2272  break;
2273  }
2274 
2275  Reference< XCheckBox > xBox(xControl, UNO_QUERY);
2276  if (xBox.is())
2277  {
2278  xBox->removeItemListener(this);
2279  break;
2280  }
2281 
2282  Reference< XComboBox > xCbBox(xControl, UNO_QUERY);
2283  if (xCbBox.is())
2284  {
2285  xCbBox->removeItemListener(this);
2286  break;
2287  }
2288 
2289  Reference< XListBox > xListBox(xControl, UNO_QUERY);
2290  if (xListBox.is())
2291  {
2292  xListBox->removeItemListener(this);
2293  break;
2294  }
2295  break;
2296  }
2297 }
2298 
2299 
2300 void FormController::startListening()
2301 {
2302  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2303  m_bModified = false;
2304 
2305  // now register at bound fields
2306  for ( const Reference< XControl >& rControl : std::as_const(m_aControls) )
2307  startControlModifyListening( rControl );
2308 }
2309 
2310 
2311 void FormController::stopListening()
2312 {
2313  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2314  m_bModified = false;
2315 
2316  // now register at bound fields
2317  for ( const Reference< XControl >& rControl : std::as_const(m_aControls) )
2318  stopControlModifyListening( rControl );
2319 }
2320 
2321 
2322 Reference< XControl > FormController::findControl(Sequence< Reference< XControl > >& _rControls, const Reference< XControlModel > & xCtrlModel ,bool _bRemove,bool _bOverWrite) const
2323 {
2324  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2325  DBG_ASSERT( xCtrlModel.is(), "findControl - which ?!" );
2326 
2327  Reference< XControl >* pControls = std::find_if(_rControls.begin(), _rControls.end(),
2328  [&xCtrlModel](const Reference< XControl >& rControl) {
2329  return rControl.is() && rControl->getModel().get() == xCtrlModel.get(); });
2330  if (pControls != _rControls.end())
2331  {
2332  Reference< XControl > xControl( *pControls );
2333  if ( _bRemove )
2334  {
2335  auto i = static_cast<sal_Int32>(std::distance(_rControls.begin(), pControls));
2336  ::comphelper::removeElementAt( _rControls, i );
2337  }
2338  else if ( _bOverWrite )
2339  pControls->clear();
2340  return xControl;
2341  }
2342  return Reference< XControl > ();
2343 }
2344 
2345 
2346 void FormController::implControlInserted( const Reference< XControl>& _rxControl, bool _bAddToEventAttacher )
2347 {
2348  Reference< XWindow > xWindow( _rxControl, UNO_QUERY );
2349  if ( xWindow.is() )
2350  {
2351  xWindow->addFocusListener( this );
2352  xWindow->addMouseListener( this );
2353 
2354  if ( _bAddToEventAttacher )
2355  addToEventAttacher( _rxControl );
2356  }
2357 
2358  // add a dispatch interceptor to the control (if supported)
2359  Reference< XDispatchProviderInterception > xInterception( _rxControl, UNO_QUERY );
2360  if ( xInterception.is() )
2361  createInterceptor( xInterception );
2362 
2363  if ( !_rxControl.is() )
2364  return;
2365 
2366  Reference< XControlModel > xModel( _rxControl->getModel() );
2367 
2368  // we want to know about the reset of the model of our controls
2369  // (for correctly resetting m_bModified)
2370  Reference< XReset > xReset( xModel, UNO_QUERY );
2371  if ( xReset.is() )
2372  xReset->addResetListener( this );
2373 
2374  // and we want to know about the validity, to visually indicate it
2375  Reference< XValidatableFormComponent > xValidatable( xModel, UNO_QUERY );
2376  if ( xValidatable.is() )
2377  {
2378  xValidatable->addFormComponentValidityListener( this );
2379  m_aControlBorderManager.validityChanged( _rxControl, xValidatable );
2380  }
2381 
2382 }
2383 
2384 
2385 void FormController::implControlRemoved( const Reference< XControl>& _rxControl, bool _bRemoveFromEventAttacher )
2386 {
2387  Reference< XWindow > xWindow( _rxControl, UNO_QUERY );
2388  if ( xWindow.is() )
2389  {
2390  xWindow->removeFocusListener( this );
2391  xWindow->removeMouseListener( this );
2392 
2393  if ( _bRemoveFromEventAttacher )
2394  removeFromEventAttacher( _rxControl );
2395  }
2396 
2397  Reference< XDispatchProviderInterception > xInterception( _rxControl, UNO_QUERY);
2398  if ( xInterception.is() )
2399  deleteInterceptor( xInterception );
2400 
2401  if ( _rxControl.is() )
2402  {
2403  Reference< XControlModel > xModel( _rxControl->getModel() );
2404 
2405  Reference< XReset > xReset( xModel, UNO_QUERY );
2406  if ( xReset.is() )
2407  xReset->removeResetListener( this );
2408 
2409  Reference< XValidatableFormComponent > xValidatable( xModel, UNO_QUERY );
2410  if ( xValidatable.is() )
2411  xValidatable->removeFormComponentValidityListener( this );
2412  }
2413 }
2414 
2415 
2416 void FormController::implSetCurrentControl( const Reference< XControl >& _rxControl )
2417 {
2418  if ( m_xCurrentControl.get() == _rxControl.get() )
2419  return;
2420 
2421  Reference< XGridControl > xGridControl( m_xCurrentControl, UNO_QUERY );
2422  if ( xGridControl.is() )
2423  xGridControl->removeGridControlListener( this );
2424 
2425  m_xCurrentControl = _rxControl;
2426 
2427  xGridControl.set( m_xCurrentControl, UNO_QUERY );
2428  if ( xGridControl.is() )
2429  xGridControl->addGridControlListener( this );
2430 }
2431 
2432 
2433 void FormController::insertControl(const Reference< XControl > & xControl)
2434 {
2435  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2436  m_bControlsSorted = false;
2437  m_aControls.realloc(m_aControls.getLength() + 1);
2438  m_aControls.getArray()[m_aControls.getLength() - 1] = xControl;
2439 
2440  if (m_pColumnInfoCache)
2441  m_pColumnInfoCache->deinitializeControls();
2442 
2443  implControlInserted( xControl, m_bAttachEvents );
2444 
2445  if (m_bDBConnection && !m_bFiltering)
2446  setControlLock(xControl);
2447 
2448  if (isListeningForChanges() && m_bAttachEvents)
2449  startControlModifyListening( xControl );
2450 }
2451 
2452 
2453 void FormController::removeControl(const Reference< XControl > & xControl)
2454 {
2455  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2456  auto pControl = std::find_if(m_aControls.begin(), m_aControls.end(),
2457  [&xControl](const Reference< XControl >& rControl) { return xControl.get() == rControl.get(); });
2458  if (pControl != m_aControls.end())
2459  {
2460  auto nIndex = static_cast<sal_Int32>(std::distance(m_aControls.begin(), pControl));
2461  ::comphelper::removeElementAt( m_aControls, nIndex );
2462  }
2463 
2464  FilterComponents::iterator componentPos = ::std::find( m_aFilterComponents.begin(), m_aFilterComponents.end(), xControl );
2465  if ( componentPos != m_aFilterComponents.end() )
2466  m_aFilterComponents.erase( componentPos );
2467 
2468  implControlRemoved( xControl, m_bDetachEvents );
2469 
2470  if ( isListeningForChanges() && m_bDetachEvents )
2471  stopControlModifyListening( xControl );
2472 }
2473 
2474 // XLoadListener
2475 
2476 void FormController::loaded(const EventObject& rEvent)
2477 {
2478  OSL_ENSURE( rEvent.Source == m_xModelAsIndex, "FormController::loaded: where did this come from?" );
2479 
2480  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2481  ::osl::MutexGuard aGuard( m_aMutex );
2482  Reference< XRowSet > xForm(rEvent.Source, UNO_QUERY);
2483  // do we have a connected data source
2484  if (xForm.is() && getConnection(xForm).is())
2485  {
2486  Reference< XPropertySet > xSet(xForm, UNO_QUERY);
2487  if (xSet.is())
2488  {
2489  Any aVal = xSet->getPropertyValue(FM_PROP_CYCLE);
2490  sal_Int32 aVal2 = 0;
2491  ::cppu::enum2int(aVal2,aVal);
2492  m_bCycle = !aVal.hasValue() || static_cast<form::TabulatorCycle>(aVal2) == TabulatorCycle_RECORDS;
2493  m_bCanUpdate = canUpdate(xSet);
2494  m_bCanInsert = canInsert(xSet);
2495  m_bCurrentRecordModified = ::comphelper::getBOOL(xSet->getPropertyValue(FM_PROP_ISMODIFIED));
2496  m_bCurrentRecordNew = ::comphelper::getBOOL(xSet->getPropertyValue(FM_PROP_ISNEW));
2497 
2498  startFormListening( xSet, false );
2499 
2500  // set the locks for the current controls
2501  if (getContainer().is())
2502  {
2503  m_aLoadEvent.Call();
2504  }
2505  }
2506  else
2507  {
2508  m_bCanInsert = m_bCanUpdate = m_bCycle = false;
2509  m_bCurrentRecordModified = false;
2510  m_bCurrentRecordNew = false;
2511  m_bLocked = false;
2512  }
2513  m_bDBConnection = true;
2514  }
2515  else
2516  {
2517  m_bDBConnection = false;
2518  m_bCanInsert = m_bCanUpdate = m_bCycle = false;
2519  m_bCurrentRecordModified = false;
2520  m_bCurrentRecordNew = false;
2521  m_bLocked = false;
2522  }
2523 
2524  Reference< XColumnsSupplier > xFormColumns( xForm, UNO_QUERY );
2525  m_pColumnInfoCache.reset( xFormColumns.is() ? new ColumnInfoCache( xFormColumns ) : nullptr );
2526 
2527  updateAllDispatchers();
2528 }
2529 
2530 
2531 void FormController::updateAllDispatchers() const
2532 {
2533  ::std::for_each(
2534  m_aFeatureDispatchers.begin(),
2535  m_aFeatureDispatchers.end(),
2536  [] (const DispatcherContainer::value_type& dispatcher) {
2537  UpdateAllListeners()(dispatcher.second);
2538  });
2539 }
2540 
2541 
2542 IMPL_LINK_NOARG(FormController, OnLoad, void*, void)
2543 {
2544  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2545  m_bLocked = determineLockState();
2546 
2547  setLocks();
2548 
2549  if (!m_bLocked)
2550  startListening();
2551 
2552  // just one exception toggle the auto values
2553  if (m_bCurrentRecordNew)
2554  toggleAutoFields(true);
2555 }
2556 
2557 
2558 void FormController::unloaded(const EventObject& /*rEvent*/)
2559 {
2560  ::osl::MutexGuard aGuard( m_aMutex );
2561  impl_checkDisposed_throw();
2562 
2563  updateAllDispatchers();
2564 }
2565 
2566 
2567 void FormController::reloading(const EventObject& /*aEvent*/)
2568 {
2569  ::osl::MutexGuard aGuard( m_aMutex );
2570  impl_checkDisposed_throw();
2571 
2572  // do the same like in unloading
2573  // just one exception toggle the auto values
2574  m_aToggleEvent.CancelPendingCall();
2575  unload();
2576 }
2577 
2578 
2579 void FormController::reloaded(const EventObject& aEvent)
2580 {
2581  ::osl::MutexGuard aGuard( m_aMutex );
2582  impl_checkDisposed_throw();
2583 
2584  loaded(aEvent);
2585 }
2586 
2587 
2588 void FormController::unloading(const EventObject& /*aEvent*/)
2589 {
2590  ::osl::MutexGuard aGuard( m_aMutex );
2591  impl_checkDisposed_throw();
2592 
2593  unload();
2594 }
2595 
2596 
2597 void FormController::unload()
2598 {
2599  ::osl::MutexGuard aGuard( m_aMutex );
2600  impl_checkDisposed_throw();
2601 
2602  m_aLoadEvent.CancelPendingCall();
2603 
2604  // be sure not to have autofields
2605  if (m_bCurrentRecordNew)
2606  toggleAutoFields(false);
2607 
2608  // remove bound field listing again
2609  removeBoundFieldListener();
2610 
2611  if (m_bDBConnection && isListeningForChanges())
2612  stopListening();
2613 
2614  Reference< XPropertySet > xSet( m_xModelAsIndex, UNO_QUERY );
2615  if ( m_bDBConnection && xSet.is() )
2616  stopFormListening( xSet, false );
2617 
2618  m_bDBConnection = false;
2619  m_bCanInsert = m_bCanUpdate = m_bCycle = false;
2620  m_bCurrentRecordModified = m_bCurrentRecordNew = m_bLocked = false;
2621 
2622  m_pColumnInfoCache.reset();
2623 }
2624 
2625 
2626 void FormController::removeBoundFieldListener()
2627 {
2628  for ( const Reference< XControl >& rControl : std::as_const(m_aControls) )
2629  {
2630  Reference< XPropertySet > xProp( rControl, UNO_QUERY );
2631  if ( xProp.is() )
2632  xProp->removePropertyChangeListener( FM_PROP_BOUNDFIELD, this );
2633  }
2634 }
2635 
2636 
2637 void FormController::startFormListening( const Reference< XPropertySet >& _rxForm, bool _bPropertiesOnly )
2638 {
2639  try
2640  {
2641  if ( m_bCanInsert || m_bCanUpdate ) // form can be modified
2642  {
2643  _rxForm->addPropertyChangeListener( FM_PROP_ISNEW, this );
2644  _rxForm->addPropertyChangeListener( FM_PROP_ISMODIFIED, this );
2645 
2646  if ( !_bPropertiesOnly )
2647  {
2648  // set the Listener for UI interaction
2649  Reference< XRowSetApproveBroadcaster > xApprove( _rxForm, UNO_QUERY );
2650  if ( xApprove.is() )
2651  xApprove->addRowSetApproveListener( this );
2652 
2653  // listener for row set changes
2654  Reference< XRowSet > xRowSet( _rxForm, UNO_QUERY );
2655  if ( xRowSet.is() )
2656  xRowSet->addRowSetListener( this );
2657  }
2658  }
2659 
2660  Reference< XPropertySetInfo > xInfo = _rxForm->getPropertySetInfo();
2661  if ( xInfo.is() && xInfo->hasPropertyByName( FM_PROP_DYNAMIC_CONTROL_BORDER ) )
2662  _rxForm->addPropertyChangeListener( FM_PROP_DYNAMIC_CONTROL_BORDER, this );
2663  }
2664  catch( const Exception& )
2665  {
2666  DBG_UNHANDLED_EXCEPTION("svx");
2667  }
2668 }
2669 
2670 
2671 void FormController::stopFormListening( const Reference< XPropertySet >& _rxForm, bool _bPropertiesOnly )
2672 {
2673  try
2674  {
2675  if ( m_bCanInsert || m_bCanUpdate )
2676  {
2677  _rxForm->removePropertyChangeListener( FM_PROP_ISNEW, this );
2678  _rxForm->removePropertyChangeListener( FM_PROP_ISMODIFIED, this );
2679 
2680  if ( !_bPropertiesOnly )
2681  {
2682  Reference< XRowSetApproveBroadcaster > xApprove( _rxForm, UNO_QUERY );
2683  if (xApprove.is())
2684  xApprove->removeRowSetApproveListener(this);
2685 
2686  Reference< XRowSet > xRowSet( _rxForm, UNO_QUERY );
2687  if ( xRowSet.is() )
2688  xRowSet->removeRowSetListener( this );
2689  }
2690  }
2691 
2692  Reference< XPropertySetInfo > xInfo = _rxForm->getPropertySetInfo();
2693  if ( xInfo.is() && xInfo->hasPropertyByName( FM_PROP_DYNAMIC_CONTROL_BORDER ) )
2694  _rxForm->removePropertyChangeListener( FM_PROP_DYNAMIC_CONTROL_BORDER, this );
2695  }
2696  catch( const Exception& )
2697  {
2698  DBG_UNHANDLED_EXCEPTION("svx");
2699  }
2700 }
2701 
2702 // css::sdbc::XRowSetListener
2703 
2704 void FormController::cursorMoved(const EventObject& /*event*/)
2705 {
2706  ::osl::MutexGuard aGuard( m_aMutex );
2707  impl_checkDisposed_throw();
2708 
2709  // toggle the locking ?
2710  if (m_bLocked != determineLockState())
2711  {
2712  m_bLocked = !m_bLocked;
2713  setLocks();
2714  if (isListeningForChanges())
2715  startListening();
2716  else
2717  stopListening();
2718  }
2719 
2720  // neither the current control nor the current record are modified anymore
2721  m_bCurrentRecordModified = m_bModified = false;
2722 }
2723 
2724 
2725 void FormController::rowChanged(const EventObject& /*event*/)
2726 {
2727  // not interested in ...
2728 }
2729 
2730 void FormController::rowSetChanged(const EventObject& /*event*/)
2731 {
2732  // not interested in ...
2733 }
2734 
2735 
2736 // XContainerListener
2737 
2738 void SAL_CALL FormController::elementInserted(const ContainerEvent& evt)
2739 {
2740  ::osl::MutexGuard aGuard( m_aMutex );
2741  impl_checkDisposed_throw();
2742 
2743  Reference< XControl > xControl( evt.Element, UNO_QUERY );
2744  if ( !xControl.is() )
2745  return;
2746 
2747  Reference< XFormComponent > xModel(xControl->getModel(), UNO_QUERY);
2748  if (xModel.is() && m_xModelAsIndex == xModel->getParent())
2749  {
2750  insertControl(xControl);
2751 
2752  if ( m_aTabActivationIdle.IsActive() )
2753  m_aTabActivationIdle.Stop();
2754 
2755  m_aTabActivationIdle.Start();
2756  }
2757  // are we in filtermode and a XModeSelector has inserted an element
2758  else if (m_bFiltering && Reference< XModeSelector > (evt.Source, UNO_QUERY).is())
2759  {
2760  xModel.set(evt.Source, UNO_QUERY);
2761  if (xModel.is() && m_xModelAsIndex == xModel->getParent())
2762  {
2763  Reference< XPropertySet > xSet(xControl->getModel(), UNO_QUERY);
2764  if (xSet.is() && ::comphelper::hasProperty(FM_PROP_BOUNDFIELD, xSet))
2765  {
2766  // does the model use a bound field ?
2768  xSet->getPropertyValue(FM_PROP_BOUNDFIELD) >>= xField;
2769 
2770  Reference< XTextComponent > xText(xControl, UNO_QUERY);
2771  // may we filter the field?
2772  if (xText.is() && xField.is() && ::comphelper::hasProperty(FM_PROP_SEARCHABLE, xField) &&
2773  ::comphelper::getBOOL(xField->getPropertyValue(FM_PROP_SEARCHABLE)))
2774  {
2775  m_aFilterComponents.push_back( xText );
2776  xText->addTextListener( this );
2777  }
2778  }
2779  }
2780  }
2781 }
2782 
2783 
2784 void SAL_CALL FormController::elementReplaced(const ContainerEvent& evt)
2785 {
2786  // simulate an elementRemoved
2787  ContainerEvent aRemoveEvent( evt );
2788  aRemoveEvent.Element = evt.ReplacedElement;
2789  aRemoveEvent.ReplacedElement = Any();
2790  elementRemoved( aRemoveEvent );
2791 
2792  // simulate an elementInserted
2793  ContainerEvent aInsertEvent( evt );
2794  aInsertEvent.ReplacedElement = Any();
2795  elementInserted( aInsertEvent );
2796 }
2797 
2798 
2799 void SAL_CALL FormController::elementRemoved(const ContainerEvent& evt)
2800 {
2801  ::osl::MutexGuard aGuard( m_aMutex );
2802  impl_checkDisposed_throw();
2803 
2804  Reference< XControl > xControl;
2805  evt.Element >>= xControl;
2806  if (!xControl.is())
2807  return;
2808 
2809  Reference< XFormComponent > xModel(xControl->getModel(), UNO_QUERY);
2810  if (xModel.is() && m_xModelAsIndex == xModel->getParent())
2811  {
2812  removeControl(xControl);
2813  // Do not recalculate TabOrder, because it must already work internally!
2814  }
2815  // are we in filtermode and a XModeSelector has inserted an element
2816  else if (m_bFiltering && Reference< XModeSelector > (evt.Source, UNO_QUERY).is())
2817  {
2818  FilterComponents::iterator componentPos = ::std::find(
2819  m_aFilterComponents.begin(), m_aFilterComponents.end(), xControl );
2820  if ( componentPos != m_aFilterComponents.end() )
2821  m_aFilterComponents.erase( componentPos );
2822  }
2823 }
2824 
2825 
2826 Reference< XControl > FormController::isInList(const Reference< XWindowPeer > & xPeer) const
2827 {
2828  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2829  const Reference< XControl >* pControls = m_aControls.getConstArray();
2830 
2831  sal_uInt32 nCtrls = m_aControls.getLength();
2832  for ( sal_uInt32 n = 0; n < nCtrls && xPeer.is(); ++n, ++pControls )
2833  {
2834  if ( pControls->is() )
2835  {
2836  Reference< XVclWindowPeer > xCtrlPeer( (*pControls)->getPeer(), UNO_QUERY);
2837  if ( ( xCtrlPeer.get() == xPeer.get() ) || xCtrlPeer->isChild( xPeer ) )
2838  return *pControls;
2839  }
2840  }
2841  return Reference< XControl > ();
2842 }
2843 
2844 
2845 void FormController::activateFirst()
2846 {
2847  ::osl::MutexGuard aGuard( m_aMutex );
2848  impl_checkDisposed_throw();
2849 
2850  DBG_ASSERT(m_xTabController.is(), "FormController::activateFirst : invalid aggregate !");
2851  if (m_xTabController.is())
2852  m_xTabController->activateFirst();
2853 }
2854 
2855 
2856 void FormController::activateLast()
2857 {
2858  ::osl::MutexGuard aGuard( m_aMutex );
2859  impl_checkDisposed_throw();
2860 
2861  DBG_ASSERT(m_xTabController.is(), "FormController::activateLast : invalid aggregate !");
2862  if (m_xTabController.is())
2863  m_xTabController->activateLast();
2864 }
2865 
2866 // XFormController
2867 
2868 Reference< XFormOperations > SAL_CALL FormController::getFormOperations()
2869 {
2870  ::osl::MutexGuard aGuard( m_aMutex );
2871  impl_checkDisposed_throw();
2872 
2873  return m_xFormOperations;
2874 }
2875 
2876 
2877 Reference< XControl> SAL_CALL FormController::getCurrentControl()
2878 {
2879  ::osl::MutexGuard aGuard( m_aMutex );
2880  impl_checkDisposed_throw();
2881  return m_xCurrentControl;
2882 }
2883 
2884 
2885 void SAL_CALL FormController::addActivateListener(const Reference< XFormControllerListener > & l)
2886 {
2887  ::osl::MutexGuard aGuard( m_aMutex );
2888  impl_checkDisposed_throw();
2889  m_aActivateListeners.addInterface(l);
2890 }
2891 
2892 void SAL_CALL FormController::removeActivateListener(const Reference< XFormControllerListener > & l)
2893 {
2894  ::osl::MutexGuard aGuard( m_aMutex );
2895  impl_checkDisposed_throw();
2896  m_aActivateListeners.removeInterface(l);
2897 }
2898 
2899 
2900 void SAL_CALL FormController::addChildController( const Reference< XFormController >& ChildController )
2901 {
2902  ::osl::MutexGuard aGuard( m_aMutex );
2903  impl_checkDisposed_throw();
2904 
2905  if ( !ChildController.is() )
2906  throw IllegalArgumentException( OUString(), *this, 1 );
2907  // TODO: (localized) error message
2908 
2909  // the parent of our (to-be-)child must be our own model
2910  Reference< XFormComponent > xFormOfChild( ChildController->getModel(), UNO_QUERY );
2911  if ( !xFormOfChild.is() )
2912  throw IllegalArgumentException( OUString(), *this, 1 );
2913  // TODO: (localized) error message
2914 
2915  if ( xFormOfChild->getParent() != m_xModelAsIndex )
2916  throw IllegalArgumentException( OUString(), *this, 1 );
2917  // TODO: (localized) error message
2918 
2919  m_aChildren.push_back( ChildController );
2920  ChildController->setParent( *this );
2921 
2922  // search the position of the model within the form
2923  sal_uInt32 nPos = m_xModelAsIndex->getCount();
2924  Reference< XFormComponent > xTemp;
2925  for( ; nPos; )
2926  {
2927  m_xModelAsIndex->getByIndex(--nPos) >>= xTemp;
2928  if ( xFormOfChild == xTemp )
2929  {
2930  m_xModelAsManager->attach( nPos, Reference<XInterface>( ChildController, UNO_QUERY ), makeAny( ChildController) );
2931  break;
2932  }
2933  }
2934 }
2935 
2936 
2937 Reference< XFormControllerContext > SAL_CALL FormController::getContext()
2938 {
2939  ::osl::MutexGuard aGuard( m_aMutex );
2940  impl_checkDisposed_throw();
2941  return m_xFormControllerContext;
2942 }
2943 
2944 
2945 void SAL_CALL FormController::setContext( const Reference< XFormControllerContext >& _context )
2946 {
2947  ::osl::MutexGuard aGuard( m_aMutex );
2948  impl_checkDisposed_throw();
2949  m_xFormControllerContext = _context;
2950 }
2951 
2952 
2953 Reference< XInteractionHandler > SAL_CALL FormController::getInteractionHandler()
2954 {
2955  ::osl::MutexGuard aGuard( m_aMutex );
2956  impl_checkDisposed_throw();
2957  return m_xInteractionHandler;
2958 }
2959 
2960 
2961 void SAL_CALL FormController::setInteractionHandler( const Reference< XInteractionHandler >& _interactionHandler )
2962 {
2963  ::osl::MutexGuard aGuard( m_aMutex );
2964  impl_checkDisposed_throw();
2965  m_xInteractionHandler = _interactionHandler;
2966 }
2967 
2968 
2969 void FormController::setFilter(::std::vector<FmFieldInfo>& rFieldInfos)
2970 {
2971  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2972  // create the composer
2973  Reference< XRowSet > xForm(m_xModelAsIndex, UNO_QUERY);
2974  Reference< XConnection > xConnection(getConnection(xForm));
2975  if (xForm.is())
2976  {
2977  try
2978  {
2979  Reference< XMultiServiceFactory > xFactory( xConnection, UNO_QUERY_THROW );
2980  m_xComposer.set(
2981  xFactory->createInstance("com.sun.star.sdb.SingleSelectQueryComposer"),
2982  UNO_QUERY_THROW );
2983 
2984  Reference< XPropertySet > xSet( xForm, UNO_QUERY );
2985  OUString sStatement = ::comphelper::getString( xSet->getPropertyValue( FM_PROP_ACTIVECOMMAND ) );
2986  OUString sFilter = ::comphelper::getString( xSet->getPropertyValue( FM_PROP_FILTER ) );
2987  m_xComposer->setElementaryQuery( sStatement );
2988  m_xComposer->setFilter( sFilter );
2989  }
2990  catch( const Exception& )
2991  {
2992  DBG_UNHANDLED_EXCEPTION("svx");
2993  }
2994  }
2995 
2996  if (m_xComposer.is())
2997  {
2998  const Sequence< Sequence < PropertyValue > > aFilterRows = m_xComposer->getStructuredFilter();
2999 
3000  // ok, we receive the list of filters as sequence of fieldnames, value
3001  // now we have to transform the fieldname into UI names, that could be a label of the field or
3002  // an aliasname or the fieldname itself
3003 
3004  // first adjust the field names if necessary
3005  Reference< XNameAccess > xQueryColumns =
3006  Reference< XColumnsSupplier >( m_xComposer, UNO_QUERY_THROW )->getColumns();
3007 
3008  for (auto& rFieldInfo : rFieldInfos)
3009  {
3010  if ( xQueryColumns->hasByName(rFieldInfo.aFieldName) )
3011  {
3012  if ( (xQueryColumns->getByName(rFieldInfo.aFieldName) >>= rFieldInfo.xField) && rFieldInfo.xField.is() )
3013  rFieldInfo.xField->getPropertyValue(FM_PROP_REALNAME) >>= rFieldInfo.aFieldName;
3014  }
3015  }
3016 
3017  Reference< XDatabaseMetaData> xMetaData(xConnection->getMetaData());
3018  // now transfer the filters into Value/TextComponent pairs
3019  ::comphelper::UStringMixEqual aCompare(xMetaData->storesMixedCaseQuotedIdentifiers());
3020 
3021  // need to parse criteria localized
3022  Reference< XNumberFormatsSupplier> xFormatSupplier( getNumberFormats(xConnection, true));
3023  Reference< XNumberFormatter> xFormatter = NumberFormatter::create(m_xComponentContext);
3024  xFormatter->attachNumberFormatsSupplier(xFormatSupplier);
3026  const LocaleDataWrapper& rLocaleWrapper( Application::GetSettings().GetUILocaleDataWrapper() );
3027  OUString strDecimalSeparator = rLocaleWrapper.getNumDecimalSep();
3028 
3029  // retrieving the filter
3030  for (const Sequence < PropertyValue >& rRow : aFilterRows)
3031  {
3032  FmFilterRow aRow;
3033 
3034  // search a field for the given name
3035  for (const PropertyValue& rRefValue : rRow)
3036  {
3037  // look for the text component
3039  try
3040  {
3042  OUString aRealName;
3043 
3044  // first look with the given name
3045  if (xQueryColumns->hasByName(rRefValue.Name))
3046  {
3047  xQueryColumns->getByName(rRefValue.Name) >>= xSet;
3048 
3049  // get the RealName
3050  xSet->getPropertyValue("RealName") >>= aRealName;
3051 
3052  // compare the condition field name and the RealName
3053  if (aCompare(aRealName, rRefValue.Name))
3054  xField = xSet;
3055  }
3056  if (!xField.is())
3057  {
3058  // no we have to check every column to find the realname
3059  Reference< XIndexAccess > xColumnsByIndex(xQueryColumns, UNO_QUERY);
3060  for (sal_Int32 n = 0, nCount = xColumnsByIndex->getCount(); n < nCount; n++)
3061  {
3062  xColumnsByIndex->getByIndex(n) >>= xSet;
3063  xSet->getPropertyValue("RealName") >>= aRealName;
3064  if (aCompare(aRealName, rRefValue.Name))
3065  {
3066  // get the column by its alias
3067  xField = xSet;
3068  break;
3069  }
3070  }
3071  }
3072  if (!xField.is())
3073  continue;
3074  }
3075  catch (const Exception&)
3076  {
3077  continue;
3078  }
3079 
3080  // find the text component
3081  for (const auto& rFieldInfo : rFieldInfos)
3082  {
3083  // we found the field so insert a new entry to the filter row
3084  if (rFieldInfo.xField == xField)
3085  {
3086  // do we already have the control ?
3087  if (aRow.find(rFieldInfo.xText) != aRow.end())
3088  {
3089  OString aVal = m_pParser->getContext().getIntlKeywordAscii(IParseContext::InternationalKeyCode::And);
3090  OUString aCompText = aRow[rFieldInfo.xText] + " " +
3091  OUString(aVal.getStr(),aVal.getLength(),RTL_TEXTENCODING_ASCII_US) + " " +
3092  ::comphelper::getString(rRefValue.Value);
3093  aRow[rFieldInfo.xText] = aCompText;
3094  }
3095  else
3096  {
3097  OUString sPredicate,sErrorMsg;
3098  rRefValue.Value >>= sPredicate;
3099  std::unique_ptr< OSQLParseNode > pParseNode = predicateTree(sErrorMsg, sPredicate, xFormatter, xField);
3100  if ( pParseNode != nullptr )
3101  {
3102  OUString sCriteria;
3103  switch (rRefValue.Handle)
3104  {
3105  case css::sdb::SQLFilterOperator::EQUAL:
3106  sCriteria += "=";
3107  break;
3108  case css::sdb::SQLFilterOperator::NOT_EQUAL:
3109  sCriteria += "!=";
3110  break;
3111  case css::sdb::SQLFilterOperator::LESS:
3112  sCriteria += "<";
3113  break;
3114  case css::sdb::SQLFilterOperator::GREATER:
3115  sCriteria += ">";
3116  break;
3117  case css::sdb::SQLFilterOperator::LESS_EQUAL:
3118  sCriteria += "<=";
3119  break;
3120  case css::sdb::SQLFilterOperator::GREATER_EQUAL:
3121  sCriteria += ">=";
3122  break;
3123  case css::sdb::SQLFilterOperator::LIKE:
3124  sCriteria += "LIKE ";
3125  break;
3126  case css::sdb::SQLFilterOperator::NOT_LIKE:
3127  sCriteria += "NOT LIKE ";
3128  break;
3129  case css::sdb::SQLFilterOperator::SQLNULL:
3130  sCriteria += "IS NULL";
3131  break;
3132  case css::sdb::SQLFilterOperator::NOT_SQLNULL:
3133  sCriteria += "IS NOT NULL";
3134  break;
3135  }
3136  pParseNode->parseNodeToPredicateStr( sCriteria
3137  ,xConnection
3138  ,xFormatter
3139  ,xField
3140  ,OUString()
3141  ,aAppLocale
3142  ,strDecimalSeparator
3143  ,getParseContext());
3144  aRow[rFieldInfo.xText] = sCriteria;
3145  }
3146  }
3147  }
3148  }
3149  }
3150 
3151  if (aRow.empty())
3152  continue;
3153 
3154  impl_addFilterRow( aRow );
3155  }
3156  }
3157 
3158  // now set the filter controls
3159  for (const auto& rFieldInfo : rFieldInfos)
3160  {
3161  m_aFilterComponents.push_back( rFieldInfo.xText );
3162  }
3163 }
3164 
3165 
3166 void FormController::startFiltering()
3167 {
3168  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
3169 
3170  Reference< XConnection > xConnection( getConnection( Reference< XRowSet >( m_xModelAsIndex, UNO_QUERY ) ) );
3171  if ( !xConnection.is() )
3172  // nothing to do - can't filter a form which is not connected
3173  return;
3174 
3175  // stop listening for controls
3176  if (isListeningForChanges())
3177  stopListening();
3178 
3179  m_bFiltering = true;
3180 
3181  // as we don't want new controls to be attached to the scripting environment
3182  // we change attach flags
3183  m_bAttachEvents = false;
3184 
3185  // exchanging the controls for the current form
3186  Sequence< Reference< XControl > > aControlsCopy( m_aControls );
3187  const Reference< XControl >* pControls = aControlsCopy.getConstArray();
3188  sal_Int32 nControlCount = aControlsCopy.getLength();
3189 
3190  // the control we have to activate after replacement
3191  Reference< XNumberFormatsSupplier > xFormatSupplier = getNumberFormats(xConnection, true);
3192  Reference< XNumberFormatter > xFormatter = NumberFormatter::create(m_xComponentContext);
3193  xFormatter->attachNumberFormatsSupplier(xFormatSupplier);
3194 
3195  // structure for storing the field info
3196  ::std::vector<FmFieldInfo> aFieldInfos;
3197 
3198  for (sal_Int32 i = nControlCount; i > 0;)
3199  {
3200  Reference< XControl > xControl = pControls[--i];
3201  if (xControl.is())
3202  {
3203  // no events for the control anymore
3204  removeFromEventAttacher(xControl);
3205 
3206  // do we have a mode selector
3207  Reference< XModeSelector > xSelector(xControl, UNO_QUERY);
3208  if (xSelector.is())
3209  {
3210  xSelector->setMode( "FilterMode" );
3211 
3212  // listening for new controls of the selector
3213  Reference< XContainer > xContainer(xSelector, UNO_QUERY);
3214  if (xContainer.is())
3215  xContainer->addContainerListener(this);
3216 
3217  Reference< XEnumerationAccess > xElementAccess(xSelector, UNO_QUERY);
3218  if (xElementAccess.is())
3219  {
3220  Reference< XEnumeration > xEnumeration(xElementAccess->createEnumeration());
3221  Reference< XControl > xSubControl;
3222  while (xEnumeration->hasMoreElements())
3223  {
3224  xEnumeration->nextElement() >>= xSubControl;
3225  if (xSubControl.is())
3226  {
3227  Reference< XPropertySet > xSet(xSubControl->getModel(), UNO_QUERY);
3228  if (xSet.is() && ::comphelper::hasProperty(FM_PROP_BOUNDFIELD, xSet))
3229  {
3230  // does the model use a bound field ?
3232  xSet->getPropertyValue(FM_PROP_BOUNDFIELD) >>= xField;
3233 
3234  Reference< XTextComponent > xText(xSubControl, UNO_QUERY);
3235  // may we filter the field?
3236  if (xText.is() && xField.is() && ::comphelper::hasProperty(FM_PROP_SEARCHABLE, xField) &&
3237  ::comphelper::getBOOL(xField->getPropertyValue(FM_PROP_SEARCHABLE)))
3238  {
3239  aFieldInfos.emplace_back(xField, xText);
3240  xText->addTextListener(this);
3241  }
3242  }
3243  }
3244  }
3245  }
3246  continue;
3247  }
3248 
3249  Reference< XPropertySet > xModel( xControl->getModel(), UNO_QUERY );
3250  if (xModel.is() && ::comphelper::hasProperty(FM_PROP_BOUNDFIELD, xModel))
3251  {
3252  // does the model use a bound field ?
3253  Any aVal = xModel->getPropertyValue(FM_PROP_BOUNDFIELD);
3255  aVal >>= xField;
3256 
3257  // may we filter the field?
3258 
3259  if ( xField.is()
3260  && ::comphelper::hasProperty( FM_PROP_SEARCHABLE, xField )
3261  && ::comphelper::getBOOL( xField->getPropertyValue( FM_PROP_SEARCHABLE ) )
3262  )
3263  {
3264  // create a filter control
3265  Reference< XControl > xFilterControl = form::control::FilterControl::createWithFormat(
3267  VCLUnoHelper::GetInterface( getDialogParentWindow() ),
3268  xFormatter,
3269  xModel);
3270 
3271  if ( replaceControl( xControl, xFilterControl ) )
3272  {
3273  Reference< XTextComponent > xFilterText( xFilterControl, UNO_QUERY );
3274  aFieldInfos.emplace_back( xField, xFilterText );
3275  xFilterText->addTextListener(this);
3276  }
3277  }
3278  }
3279  else
3280  {
3281  // unsubscribe from EventManager
3282  }
3283  }
3284  }
3285 
3286  // we have all filter controls now, so the next step is to read the filters from the form
3287  // resolve all aliases and set the current filter to the according structure
3288  setFilter(aFieldInfos);
3289 
3290  Reference< XPropertySet > xSet( m_xModelAsIndex, UNO_QUERY );
3291  if ( xSet.is() )
3292  stopFormListening( xSet, true );
3293 
3294  impl_setTextOnAllFilter_throw();
3295 
3296  // lock all controls which are not used for filtering
3297  m_bLocked = determineLockState();
3298  setLocks();
3299  m_bAttachEvents = true;
3300 }
3301 
3302 
3303 void FormController::stopFiltering()
3304 {
3305  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
3306  if ( !m_bFiltering ) // #104693# OJ
3307  { // nothing to do
3308  return;
3309  }
3310 
3311  m_bFiltering = false;
3312  m_bDetachEvents = false;
3313 
3314  ::comphelper::disposeComponent(m_xComposer);
3315 
3316  // exchanging the controls for the current form
3317  Sequence< Reference< XControl > > aControlsCopy( m_aControls );
3318  const Reference< XControl > * pControls = aControlsCopy.getConstArray();
3319  sal_Int32 nControlCount = aControlsCopy.getLength();
3320 
3321  // clear the filter control map
3322  ::std::for_each( m_aFilterComponents.begin(), m_aFilterComponents.end(), RemoveComponentTextListener( this ) );
3323  m_aFilterComponents.clear();
3324 
3325  for ( sal_Int32 i = nControlCount; i > 0; )
3326  {
3327  Reference< XControl > xControl = pControls[--i];
3328  if (xControl.is())
3329  {
3330  // now enable event handling again
3331  addToEventAttacher(xControl);
3332 
3333  Reference< XModeSelector > xSelector(xControl, UNO_QUERY);
3334  if (xSelector.is())
3335  {
3336  xSelector->setMode( "DataMode" );
3337 
3338  // listening for new controls of the selector
3339  Reference< XContainer > xContainer(xSelector, UNO_QUERY);
3340  if (xContainer.is())
3341  xContainer->removeContainerListener(this);
3342  continue;
3343  }
3344 
3345  Reference< XPropertySet > xSet(xControl->getModel(), UNO_QUERY);
3346  if (xSet.is() && ::comphelper::hasProperty(FM_PROP_BOUNDFIELD, xSet))
3347  {
3348  // does the model use a bound field ?
3350  xSet->getPropertyValue(FM_PROP_BOUNDFIELD) >>= xField;
3351 
3352  // may we filter the field?
3353  if ( xField.is()
3354  && ::comphelper::hasProperty( FM_PROP_SEARCHABLE, xField )
3355  && ::comphelper::getBOOL( xField->getPropertyValue( FM_PROP_SEARCHABLE ) )
3356  )
3357  {
3358  OUString sServiceName;
3359  OSL_VERIFY( xSet->getPropertyValue( FM_PROP_DEFAULTCONTROL ) >>= sServiceName );
3360  Reference< XControl > xNewControl( m_xComponentContext->getServiceManager()->createInstanceWithContext( sServiceName, m_xComponentContext ), UNO_QUERY );
3361  replaceControl( xControl, xNewControl );
3362  }
3363  }
3364  }
3365  }
3366 
3367  Reference< XPropertySet > xSet( m_xModelAsIndex, UNO_QUERY );
3368  if ( xSet.is() )
3369  startFormListening( xSet, true );
3370 
3371  m_bDetachEvents = true;
3372 
3373  m_aFilterRows.clear();
3374  m_nCurrentFilterPosition = -1;
3375 
3376  // release the locks if possible
3377  // lock all controls which are not used for filtering
3378  m_bLocked = determineLockState();
3379  setLocks();
3380 
3381  // restart listening for control modifications
3382  if (isListeningForChanges())
3383  startListening();
3384 }
3385 
3386 // XModeSelector
3387 
3388 void FormController::setMode(const OUString& Mode)
3389 {
3390  ::osl::MutexGuard aGuard( m_aMutex );
3391  impl_checkDisposed_throw();
3392 
3393  if (!supportsMode(Mode))
3394  throw NoSupportException();
3395 
3396  if (Mode == m_aMode)
3397  return;
3398 
3399  m_aMode = Mode;
3400 
3401  if ( Mode == "FilterMode" )
3402  startFiltering();
3403  else
3404  stopFiltering();
3405 
3406  for (const auto& rChild : m_aChildren)
3407  {
3408  Reference< XModeSelector > xMode(rChild, UNO_QUERY);
3409  if ( xMode.is() )
3410  xMode->setMode(Mode);
3411  }
3412 }
3413 
3414 
3415 OUString SAL_CALL FormController::getMode()
3416 {
3417  ::osl::MutexGuard aGuard( m_aMutex );
3418  impl_checkDisposed_throw();
3419 
3420  return m_aMode;
3421 }
3422 
3423 
3424 Sequence< OUString > SAL_CALL FormController::getSupportedModes()
3425 {
3426  ::osl::MutexGuard aGuard( m_aMutex );
3427  impl_checkDisposed_throw();
3428 
3429  static Sequence< OUString > const aModes
3430  {
3431  "DataMode",
3432  "FilterMode"
3433  };
3434  return aModes;
3435 }
3436 
3437 
3438 sal_Bool SAL_CALL FormController::supportsMode(const OUString& Mode)
3439 {
3440  ::osl::MutexGuard aGuard( m_aMutex );
3441  impl_checkDisposed_throw();
3442 
3443  Sequence< OUString > aModes(getSupportedModes());
3444  return comphelper::findValue(aModes, Mode) != -1;
3445 }
3446 
3447 
3448 vcl::Window* FormController::getDialogParentWindow()
3449 {
3450  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
3451  vcl::Window* pParentWindow = nullptr;
3452  try
3453  {
3454  Reference< XControl > xContainerControl( getContainer(), UNO_QUERY_THROW );
3455  Reference< XWindowPeer > xContainerPeer( xContainerControl->getPeer(), UNO_SET_THROW );
3456  pParentWindow = VCLUnoHelper::GetWindow( xContainerPeer ).get();
3457  }
3458  catch( const Exception& )
3459  {
3460  DBG_UNHANDLED_EXCEPTION("svx");
3461  }
3462  return pParentWindow;
3463 }
3464 
3465 bool FormController::checkFormComponentValidity( OUString& /* [out] */ _rFirstInvalidityExplanation, Reference< XControlModel >& /* [out] */ _rxFirstInvalidModel )
3466 {
3467  try
3468  {
3469  Reference< XEnumerationAccess > xControlEnumAcc( getModel(), UNO_QUERY );
3470  Reference< XEnumeration > xControlEnumeration;
3471  if ( xControlEnumAcc.is() )
3472  xControlEnumeration = xControlEnumAcc->createEnumeration();
3473  OSL_ENSURE( xControlEnumeration.is(), "FormController::checkFormComponentValidity: cannot enumerate the controls!" );
3474  if ( !xControlEnumeration.is() )
3475  // assume all valid
3476  return true;
3477 
3478  Reference< XValidatableFormComponent > xValidatable;
3479  while ( xControlEnumeration->hasMoreElements() )
3480  {
3481  if ( !( xControlEnumeration->nextElement() >>= xValidatable ) )
3482  // control does not support validation
3483  continue;
3484 
3485  if ( xValidatable->isValid() )
3486  continue;
3487 
3488  Reference< XValidator > xValidator( xValidatable->getValidator() );
3489  OSL_ENSURE( xValidator.is(), "FormController::checkFormComponentValidity: invalid, but no validator?" );
3490  if ( !xValidator.is() )
3491  // this violates the interface definition of css.form.validation.XValidatableFormComponent ...
3492  continue;
3493 
3494  _rFirstInvalidityExplanation = xValidator->explainInvalid( xValidatable->getCurrentValue() );
3495  _rxFirstInvalidModel.set(xValidatable, css::uno::UNO_QUERY);
3496  return false;
3497  }
3498  }
3499  catch( const Exception& )
3500  {
3501  DBG_UNHANDLED_EXCEPTION("svx");
3502  }
3503  return true;
3504 }
3505 
3506 
3507 Reference< XControl > FormController::locateControl( const Reference< XControlModel >& _rxModel )
3508 {
3509  try
3510  {
3511  const Sequence< Reference< XControl > > aControls( getControls() );
3512 
3513  for ( auto const & control : aControls )
3514  {
3515  OSL_ENSURE( control.is(), "FormController::locateControl: NULL-control?" );
3516  if ( control.is() )
3517  {
3518  if ( control->getModel() == _rxModel )
3519  return control;
3520  }
3521  }
3522  OSL_FAIL( "FormController::locateControl: did not find a control for this model!" );
3523  }
3524  catch( const Exception& )
3525  {
3526  DBG_UNHANDLED_EXCEPTION("svx");
3527  }
3528  return nullptr;
3529 }
3530 
3531 
3532 namespace
3533 {
3534  void displayErrorSetFocus( const OUString& _rMessage, const Reference< XControl >& _rxFocusControl, vcl::Window* _pDialogParent )
3535  {
3536  SQLContext aError;
3537  aError.Message = SvxResId(RID_STR_WRITEERROR);
3538  aError.Details = _rMessage;
3539  displayException( aError, _pDialogParent );
3540 
3541  if ( _rxFocusControl.is() )
3542  {
3543  Reference< XWindow > xControlWindow( _rxFocusControl, UNO_QUERY );
3544  OSL_ENSURE( xControlWindow.is(), "displayErrorSetFocus: invalid control!" );
3545  if ( xControlWindow.is() )
3546  xControlWindow->setFocus();
3547  }
3548  }
3549 
3550  bool lcl_shouldValidateRequiredFields_nothrow( const Reference< XInterface >& _rxForm )
3551  {
3552  try
3553  {
3554  static const char s_sFormsCheckRequiredFields[] = "FormsCheckRequiredFields";
3555 
3556  // first, check whether the form has a property telling us the answer
3557  // this allows people to use the XPropertyContainer interface of a form to control
3558  // the behaviour on a per-form basis.
3559  Reference< XPropertySet > xFormProps( _rxForm, UNO_QUERY_THROW );
3560  Reference< XPropertySetInfo > xPSI( xFormProps->getPropertySetInfo() );
3561  if ( xPSI->hasPropertyByName( s_sFormsCheckRequiredFields ) )
3562  {
3563  bool bShouldValidate = true;
3564  OSL_VERIFY( xFormProps->getPropertyValue( s_sFormsCheckRequiredFields ) >>= bShouldValidate );
3565  return bShouldValidate;
3566  }
3567 
3568  // next, check the data source which created the connection
3569  Reference< XChild > xConnectionAsChild( xFormProps->getPropertyValue( FM_PROP_ACTIVE_CONNECTION ), UNO_QUERY_THROW );
3570  Reference< XPropertySet > xDataSource( xConnectionAsChild->getParent(), UNO_QUERY );
3571  if ( !xDataSource.is() )
3572  // seldom (but possible): this is not a connection created by a data source
3573  return true;
3574 
3575  Reference< XPropertySet > xDataSourceSettings(
3576  xDataSource->getPropertyValue("Settings"),
3577  UNO_QUERY_THROW );
3578 
3579  bool bShouldValidate = true;
3580  OSL_VERIFY( xDataSourceSettings->getPropertyValue( s_sFormsCheckRequiredFields ) >>= bShouldValidate );
3581  return bShouldValidate;
3582  }
3583  catch( const Exception& )
3584  {
3585  DBG_UNHANDLED_EXCEPTION("svx");
3586  }
3587 
3588  return true;
3589  }
3590 }
3591 
3592 // XRowSetApproveListener
3593 
3594 sal_Bool SAL_CALL FormController::approveRowChange(const RowChangeEvent& _rEvent)
3595 {
3596  ::osl::ClearableMutexGuard aGuard( m_aMutex );
3597  impl_checkDisposed_throw();
3598 
3599  ::comphelper::OInterfaceIteratorHelper2 aIter(m_aRowSetApproveListeners);
3600  bool bValid = true;
3601  if (aIter.hasMoreElements())
3602  {
3603  RowChangeEvent aEvt( _rEvent );
3604  aEvt.Source = *this;
3605  bValid = static_cast<XRowSetApproveListener*>(aIter.next())->approveRowChange(aEvt);
3606  }
3607 
3608  if ( !bValid )
3609  return bValid;
3610 
3611  if ( ( _rEvent.Action != RowChangeAction::INSERT )
3612  && ( _rEvent.Action != RowChangeAction::UPDATE )
3613  )
3614  return bValid;
3615 
3616  // if some of the control models are bound to validators, check them
3617  OUString sInvalidityExplanation;
3618  Reference< XControlModel > xInvalidModel;
3619  if ( !checkFormComponentValidity( sInvalidityExplanation, xInvalidModel ) )
3620  {
3621  Reference< XControl > xControl( locateControl( xInvalidModel ) );
3622  aGuard.clear();
3623  displayErrorSetFocus( sInvalidityExplanation, xControl, getDialogParentWindow() );
3624  return false;
3625  }
3626 
3627  // check values on NULL and required flag
3628  if ( !lcl_shouldValidateRequiredFields_nothrow( _rEvent.Source ) )
3629  return true;
3630 
3631  OSL_ENSURE(m_pColumnInfoCache, "FormController::approveRowChange: no column infos!");
3632  if (!m_pColumnInfoCache)
3633  return true;
3634 
3635  try
3636  {
3637  if ( !m_pColumnInfoCache->controlsInitialized() )
3638  m_pColumnInfoCache->initializeControls( getControls() );
3639 
3640  size_t colCount = m_pColumnInfoCache->getColumnCount();
3641  for ( size_t col = 0; col < colCount; ++col )
3642  {
3643  const ColumnInfo& rColInfo = m_pColumnInfoCache->getColumnInfo( col );
3644 
3645  if ( rColInfo.bAutoIncrement )
3646  continue;
3647 
3648  if ( rColInfo.bReadOnly )
3649  continue;
3650 
3651  if ( !rColInfo.xFirstControlWithInputRequired.is() && !rColInfo.xFirstGridWithInputRequiredColumn.is() )
3652  {
3653  continue;
3654  }
3655 
3656  // TODO: in case of binary fields, this "getString" below is extremely expensive
3657  if ( !rColInfo.xColumn->wasNull() || !rColInfo.xColumn->getString().isEmpty() )
3658  continue;
3659 
3660  OUString sMessage( SvxResId( RID_ERR_FIELDREQUIRED ) );
3661  sMessage = sMessage.replaceFirst( "#", rColInfo.sName );
3662 
3663  // the control to focus
3664  Reference< XControl > xControl( rColInfo.xFirstControlWithInputRequired );
3665  if ( !xControl.is() )
3666  xControl.set( rColInfo.xFirstGridWithInputRequiredColumn, UNO_QUERY );
3667 
3668  aGuard.clear();
3669  displayErrorSetFocus( sMessage, rColInfo.xFirstControlWithInputRequired, getDialogParentWindow() );
3670  return false;
3671  }
3672  }
3673  catch( const Exception& )
3674  {
3675  DBG_UNHANDLED_EXCEPTION("svx");
3676  }
3677 
3678  return true;
3679 }
3680 
3681 
3682 sal_Bool SAL_CALL FormController::approveCursorMove(const EventObject& event)
3683 {
3684  ::osl::MutexGuard aGuard( m_aMutex );
3685  impl_checkDisposed_throw();
3686 
3687  ::comphelper::OInterfaceIteratorHelper2 aIter(m_aRowSetApproveListeners);
3688  if (aIter.hasMoreElements())
3689  {
3690  EventObject aEvt(event);
3691  aEvt.Source = *this;
3692  return static_cast<XRowSetApproveListener*>(aIter.next())->approveCursorMove(aEvt);
3693  }
3694 
3695  return true;
3696 }
3697 
3698 
3699 sal_Bool SAL_CALL FormController::approveRowSetChange(const EventObject& event)
3700 {
3701  ::osl::MutexGuard aGuard( m_aMutex );
3702  impl_checkDisposed_throw();
3703 
3704  ::comphelper::OInterfaceIteratorHelper2 aIter(m_aRowSetApproveListeners);
3705  if (aIter.hasMoreElements())
3706  {
3707  EventObject aEvt(event);
3708  aEvt.Source = *this;
3709  return static_cast<XRowSetApproveListener*>(aIter.next())->approveRowSetChange(aEvt);
3710  }
3711 
3712  return true;
3713 }
3714 
3715 // XRowSetApproveBroadcaster
3716 
3717 void SAL_CALL FormController::addRowSetApproveListener(const Reference< XRowSetApproveListener > & _rxListener)
3718 {
3719  ::osl::MutexGuard aGuard( m_aMutex );
3720  impl_checkDisposed_throw();
3721 
3722  m_aRowSetApproveListeners.addInterface(_rxListener);
3723 }
3724 
3725 
3726 void SAL_CALL FormController::removeRowSetApproveListener(const Reference< XRowSetApproveListener > & _rxListener)
3727 {
3728  ::osl::MutexGuard aGuard( m_aMutex );
3729  impl_checkDisposed_throw();
3730 
3731  m_aRowSetApproveListeners.removeInterface(_rxListener);
3732 }
3733 
3734 // XErrorListener
3735 
3736 void SAL_CALL FormController::errorOccured(const SQLErrorEvent& aEvent)
3737 {
3738  ::osl::ClearableMutexGuard aGuard( m_aMutex );
3739  impl_checkDisposed_throw();
3740 
3741  ::comphelper::OInterfaceIteratorHelper2 aIter(m_aErrorListeners);
3742  if (aIter.hasMoreElements())
3743  {
3744  SQLErrorEvent aEvt(aEvent);
3745  aEvt.Source = *this;
3746  static_cast<XSQLErrorListener*>(aIter.next())->errorOccured(aEvt);
3747  }
3748  else
3749  {
3750  aGuard.clear();
3751  displayException(aEvent, getDialogParentWindow());
3752  }
3753 }
3754 
3755 // XErrorBroadcaster
3756 
3757 void SAL_CALL FormController::addSQLErrorListener(const Reference< XSQLErrorListener > & aListener)
3758 {
3759  ::osl::MutexGuard aGuard( m_aMutex );
3760  impl_checkDisposed_throw();
3761 
3762  m_aErrorListeners.addInterface(aListener);
3763 }
3764 
3765 
3766 void SAL_CALL FormController::removeSQLErrorListener(const Reference< XSQLErrorListener > & aListener)
3767 {
3768  ::osl::MutexGuard aGuard( m_aMutex );
3769  impl_checkDisposed_throw();
3770 
3771  m_aErrorListeners.removeInterface(aListener);
3772 }
3773 
3774 // XDatabaseParameterBroadcaster2
3775 
3776 void SAL_CALL FormController::addDatabaseParameterListener(const Reference< XDatabaseParameterListener > & aListener)
3777 {
3778  ::osl::MutexGuard aGuard( m_aMutex );
3779  impl_checkDisposed_throw();
3780 
3781  m_aParameterListeners.addInterface(aListener);
3782 }
3783 
3784 
3785 void SAL_CALL FormController::removeDatabaseParameterListener(const Reference< XDatabaseParameterListener > & aListener)
3786 {
3787  ::osl::MutexGuard aGuard( m_aMutex );
3788  impl_checkDisposed_throw();
3789 
3790  m_aParameterListeners.removeInterface(aListener);
3791 }
3792 
3793 // XDatabaseParameterBroadcaster
3794 
3795 void SAL_CALL FormController::addParameterListener(const Reference< XDatabaseParameterListener > & aListener)
3796 {
3797  FormController::addDatabaseParameterListener( aListener );
3798 }
3799 
3800 
3801 void SAL_CALL FormController::removeParameterListener(const Reference< XDatabaseParameterListener > & aListener)
3802 {
3803  FormController::removeDatabaseParameterListener( aListener );
3804 }
3805 
3806 // XDatabaseParameterListener
3807 
3808 sal_Bool SAL_CALL FormController::approveParameter(const DatabaseParameterEvent& aEvent)
3809 {
3810  SolarMutexGuard aSolarGuard;
3811  ::osl::MutexGuard aGuard( m_aMutex );
3812  impl_checkDisposed_throw();
3813 
3814  ::comphelper::OInterfaceIteratorHelper2 aIter(m_aParameterListeners);
3815  if (aIter.hasMoreElements())
3816  {
3817  DatabaseParameterEvent aEvt(aEvent);
3818  aEvt.Source = *this;
3819  return static_cast<XDatabaseParameterListener*>(aIter.next())->approveParameter(aEvt);
3820  }
3821  else
3822  {
3823  // default handling: instantiate an interaction handler and let it handle the parameter request
3824  try
3825  {
3826  if ( !ensureInteractionHandler() )
3827  return false;
3828 
3829  // two continuations allowed: OK and Cancel
3830  OParameterContinuation* pParamValues = new OParameterContinuation;
3831  OInteractionAbort* pAbort = new OInteractionAbort;
3832  // the request
3833  ParametersRequest aRequest;
3834  aRequest.Parameters = aEvent.Parameters;
3835  aRequest.Connection = getConnection(Reference< XRowSet >(aEvent.Source, UNO_QUERY));
3836  OInteractionRequest* pParamRequest = new OInteractionRequest(makeAny(aRequest));
3837  Reference< XInteractionRequest > xParamRequest(pParamRequest);
3838  // some knittings
3839  pParamRequest->addContinuation(pParamValues);
3840  pParamRequest->addContinuation(pAbort);
3841 
3842  // handle the request
3843  m_xInteractionHandler->handle(xParamRequest);
3844 
3845  if (!pParamValues->wasSelected())
3846  // canceled
3847  return false;
3848 
3849  // transfer the values into the parameter supplier
3850  Sequence< PropertyValue > aFinalValues = pParamValues->getValues();
3851  if (aFinalValues.getLength() != aRequest.Parameters->getCount())
3852  {
3853  OSL_FAIL("FormController::approveParameter: the InteractionHandler returned nonsense!");
3854  return false;
3855  }
3856  const PropertyValue* pFinalValues = aFinalValues.getConstArray();
3857  for (sal_Int32 i=0; i<aFinalValues.getLength(); ++i, ++pFinalValues)
3858  {
3860  aRequest.Parameters->getByIndex(i), css::uno::UNO_QUERY);
3861  if (xParam.is())
3862  {
3863 #ifdef DBG_UTIL
3864  OUString sName;
3865  xParam->getPropertyValue(FM_PROP_NAME) >>= sName;
3866  DBG_ASSERT(sName == pFinalValues->Name, "FormController::approveParameter: suspicious value names!");
3867 #endif
3868  try { xParam->setPropertyValue(FM_PROP_VALUE, pFinalValues->Value); }
3869  catch(Exception&)
3870  {
3871  OSL_FAIL("FormController::approveParameter: setting one of the properties failed!");
3872  }
3873  }
3874  }
3875  }
3876  catch(Exception&)
3877  {
3878  DBG_UNHANDLED_EXCEPTION("svx");
3879  }
3880  }
3881  return true;
3882 }
3883 
3884 // XConfirmDeleteBroadcaster
3885 
3886 void SAL_CALL FormController::addConfirmDeleteListener(const Reference< XConfirmDeleteListener > & aListener)
3887 {
3888  ::osl::MutexGuard aGuard( m_aMutex );
3889  impl_checkDisposed_throw();
3890 
3891  m_aDeleteListeners.addInterface(aListener);
3892 }
3893 
3894 
3895 void SAL_CALL FormController::removeConfirmDeleteListener(const Reference< XConfirmDeleteListener > & aListener)
3896 {
3897  ::osl::MutexGuard aGuard( m_aMutex );
3898  impl_checkDisposed_throw();
3899 
3900  m_aDeleteListeners.removeInterface(aListener);
3901 }
3902 
3903 // XConfirmDeleteListener
3904 
3905 sal_Bool SAL_CALL FormController::confirmDelete(const RowChangeEvent& aEvent)
3906 {
3907  ::osl::MutexGuard aGuard( m_aMutex );
3908  impl_checkDisposed_throw();
3909 
3910  ::comphelper::OInterfaceIteratorHelper2 aIter(m_aDeleteListeners);
3911  if (aIter.hasMoreElements())
3912  {
3913  RowChangeEvent aEvt(aEvent);
3914  aEvt.Source = *this;
3915  return static_cast<XConfirmDeleteListener*>(aIter.next())->confirmDelete(aEvt);
3916  }
3917  // default handling: instantiate an interaction handler and let it handle the request
3918 
3919  OUString sTitle;
3920  sal_Int32 nLength = aEvent.Rows;
3921  if ( nLength > 1 )
3922  {
3923  sTitle = SvxResId( RID_STR_DELETECONFIRM_RECORDS );
3924  sTitle = sTitle.replaceFirst( "#", OUString::number(nLength) );
3925  }
3926  else
3927  sTitle = SvxResId( RID_STR_DELETECONFIRM_RECORD );
3928 
3929  try
3930  {
3931  if ( !ensureInteractionHandler() )
3932  return false;
3933 
3934  // two continuations allowed: Yes and No
3935  OInteractionApprove* pApprove = new OInteractionApprove;
3937 
3938  // the request
3939  SQLWarning aWarning;
3940  aWarning.Message = sTitle;
3941  SQLWarning aDetails;
3942  aDetails.Message = SvxResId(RID_STR_DELETECONFIRM);
3943  aWarning.NextException <<= aDetails;
3944 
3945  OInteractionRequest* pRequest = new OInteractionRequest( makeAny( aWarning ) );
3946  Reference< XInteractionRequest > xRequest( pRequest );
3947 
3948  // some knittings
3949  pRequest->addContinuation( pApprove );
3950  pRequest->addContinuation( pDisapprove );
3951 
3952  // handle the request
3953  m_xInteractionHandler->handle( xRequest );
3954 
3955  if ( pApprove->wasSelected() )
3956  return true;
3957  }
3958  catch( const Exception& )
3959  {
3960  DBG_UNHANDLED_EXCEPTION("svx");
3961  }
3962 
3963  return false;
3964 }
3965 
3966 
3967 void SAL_CALL FormController::invalidateFeatures( const Sequence< ::sal_Int16 >& Features )
3968 {
3969  ::osl::MutexGuard aGuard( m_aMutex );
3970  // for now, just copy the ids of the features, because...
3971  m_aInvalidFeatures.insert( Features.begin(), Features.end() );
3972 
3973  // ... we will do the real invalidation asynchronously
3974  if ( !m_aFeatureInvalidationTimer.IsActive() )
3975  m_aFeatureInvalidationTimer.Start();
3976 }
3977 
3978 
3979 void SAL_CALL FormController::invalidateAllFeatures( )
3980 {
3981  ::osl::ClearableMutexGuard aGuard( m_aMutex );
3982 
3983  Sequence< sal_Int16 > aInterceptedFeatures( comphelper::mapKeysToSequence(m_aFeatureDispatchers) );
3984 
3985  aGuard.clear();
3986  if ( aInterceptedFeatures.hasElements() )
3987  invalidateFeatures( aInterceptedFeatures );
3988 }
3989 
3990 
3991 Reference< XDispatch >
3992 FormController::interceptedQueryDispatch( const URL& aURL,
3993  const OUString& /*aTargetFrameName*/, sal_Int32 /*nSearchFlags*/)
3994 {
3995  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
3996  Reference< XDispatch > xReturn;
3997  // dispatches handled by ourself
3998  if ( ( aURL.Complete == FMURL_CONFIRM_DELETION )
3999  || ( ( aURL.Complete == "private:/InteractionHandler" )
4000  && ensureInteractionHandler()
4001  )
4002  )
4003  xReturn = static_cast< XDispatch* >( this );
4004 
4005  // dispatches of FormSlot-URLs we have to translate
4006  if ( !xReturn.is() && m_xFormOperations.is() )
4007  {
4008  // find the slot id which corresponds to the URL
4009  sal_Int32 nFeatureSlotId = svx::FeatureSlotTranslation::getControllerFeatureSlotIdForURL( aURL.Main );
4010  sal_Int16 nFormFeature = ( nFeatureSlotId != -1 ) ? svx::FeatureSlotTranslation::getFormFeatureForSlotId( nFeatureSlotId ) : -1;
4011  if ( nFormFeature > 0 )
4012  {
4013  // get the dispatcher for this feature, create if necessary
4014  DispatcherContainer::const_iterator aDispatcherPos = m_aFeatureDispatchers.find( nFormFeature );
4015  if ( aDispatcherPos == m_aFeatureDispatchers.end() )
4016  {
4017  aDispatcherPos = m_aFeatureDispatchers.emplace(
4018  nFormFeature, new svx::OSingleFeatureDispatcher( aURL, nFormFeature, m_xFormOperations, m_aMutex )
4019  ).first;
4020  }
4021 
4022  OSL_ENSURE( aDispatcherPos->second.is(), "FormController::interceptedQueryDispatch: should have a dispatcher by now!" );
4023  return aDispatcherPos->second;
4024  }
4025  }
4026 
4027  // no more to offer
4028  return xReturn;
4029 }
4030 
4031 
4032 void SAL_CALL FormController::dispatch( const URL& _rURL, const Sequence< PropertyValue >& _rArgs )
4033 {
4034  if ( _rArgs.getLength() != 1 )
4035  {
4036  OSL_FAIL( "FormController::dispatch: no arguments -> no dispatch!" );
4037  return;
4038  }
4039 
4040  if ( _rURL.Complete == "private:/InteractionHandler" )
4041  {
4042  Reference< XInteractionRequest > xRequest;
4043  OSL_VERIFY( _rArgs[0].Value >>= xRequest );
4044  if ( xRequest.is() )
4045  handle( xRequest );
4046  return;
4047  }
4048 
4049  if ( _rURL.Complete == FMURL_CONFIRM_DELETION )
4050  {
4051  OSL_FAIL( "FormController::dispatch: How do you expect me to return something via this call?" );
4052  // confirmDelete has a return value - dispatch hasn't
4053  return;
4054  }
4055 
4056  OSL_FAIL( "FormController::dispatch: unknown URL!" );
4057 }
4058 
4059 
4060 void SAL_CALL FormController::addStatusListener( const Reference< XStatusListener >& _rxListener, const URL& _rURL )
4061 {
4062  if (_rURL.Complete == FMURL_CONFIRM_DELETION)
4063  {
4064  if (_rxListener.is())
4065  { // send an initial statusChanged event
4066  FeatureStateEvent aEvent;
4067  aEvent.FeatureURL = _rURL;
4068  aEvent.IsEnabled = true;
4069  _rxListener->statusChanged(aEvent);
4070  // and don't add the listener at all (the status will never change)
4071  }
4072  }
4073  else
4074  OSL_FAIL("FormController::addStatusListener: invalid (unsupported) URL!");
4075 }
4076 
4077 
4078 Reference< XInterface > SAL_CALL FormController::getParent()
4079 {
4080  return m_xParent;
4081 }
4082 
4083 
4084 void SAL_CALL FormController::setParent( const Reference< XInterface >& Parent)
4085 {
4086  m_xParent = Parent;
4087 }
4088 
4089 
4090 void SAL_CALL FormController::removeStatusListener( const Reference< XStatusListener >& /*_rxListener*/, const URL& _rURL )
4091 {
4092  OSL_ENSURE(_rURL.Complete == FMURL_CONFIRM_DELETION, "FormController::removeStatusListener: invalid (unsupported) URL!");
4093  // we never really added the listener, so we don't need to remove it
4094 }
4095 
4096 
4097 Reference< XDispatchProviderInterceptor > FormController::createInterceptor(const Reference< XDispatchProviderInterception > & _xInterception)
4098 {
4099  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
4100 #ifdef DBG_UTIL
4101  // check if we already have an interceptor for the given object
4102  for ( const auto & it : m_aControlDispatchInterceptors )
4103  {
4104  if (it->getIntercepted() == _xInterception)
4105  OSL_FAIL("FormController::createInterceptor : we already do intercept this objects dispatches !");
4106  }
4107 #endif
4108 
4109  rtl::Reference<DispatchInterceptionMultiplexer> pInterceptor(new DispatchInterceptionMultiplexer( _xInterception, this ));
4110  m_aControlDispatchInterceptors.push_back( pInterceptor );
4111 
4112  return pInterceptor.get();
4113 }
4114 
4115 
4116 bool FormController::ensureInteractionHandler()
4117 {
4118  if ( m_xInteractionHandler.is() )
4119  return true;
4120  if ( m_bAttemptedHandlerCreation )
4121  return false;
4122  m_bAttemptedHandlerCreation = true;
4123 
4124  m_xInteractionHandler = InteractionHandler::createWithParent(m_xComponentContext,
4125  VCLUnoHelper::GetInterface(getDialogParentWindow()));
4126  return m_xInteractionHandler.is();
4127 }
4128 
4129 
4130 void SAL_CALL FormController::handle( const Reference< XInteractionRequest >& _rRequest )
4131 {
4132  if ( !ensureInteractionHandler() )
4133  return;
4134  m_xInteractionHandler->handle( _rRequest );
4135 }
4136 
4137 
4138 void FormController::deleteInterceptor(const Reference< XDispatchProviderInterception > & _xInterception)
4139 {
4140  OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
4141  // search the interceptor responsible for the given object
4142  auto aIter = std::find_if(m_aControlDispatchInterceptors.begin(), m_aControlDispatchInterceptors.end(),
4143  [&_xInterception](const rtl::Reference<DispatchInterceptionMultiplexer>& rpInterceptor) {
4144  return rpInterceptor->getIntercepted() == _xInterception;
4145  });
4146  if (aIter != m_aControlDispatchInterceptors.end())
4147  {
4148  // log off the interception from its interception object
4149  (*aIter)->dispose();
4150  // remove the interceptor from our array
4151  m_aControlDispatchInterceptors.erase(aIter);
4152  }
4153 }
4154 
4155 
4156 void FormController::implInvalidateCurrentControlDependentFeatures()
4157 {
4158  Sequence< sal_Int16 > aCurrentControlDependentFeatures(4);
4159 
4160  aCurrentControlDependentFeatures[0] = FormFeature::SortAscending;
4161  aCurrentControlDependentFeatures[1] = FormFeature::SortDescending;
4162  aCurrentControlDependentFeatures[2] = FormFeature::AutoFilter;
4163  aCurrentControlDependentFeatures[3] = FormFeature::RefreshCurrentControl;
4164 
4165  invalidateFeatures( aCurrentControlDependentFeatures );
4166 }
4167 
4168 
4169 void SAL_CALL FormController::columnChanged( const EventObject& /*_event*/ )
4170 {
4171  implInvalidateCurrentControlDependentFeatures();
4172 }
4173 
4174 }
4175 
4176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void ImplSetPeerProperty(const OUString &rPropName, const css::uno::Any &rVal)
Type
#define FM_PROP_READONLY
Definition: fmprop.hxx:46
FmFieldInfo(const Reference< XPropertySet > &_xField, const Reference< XTextComponent > &_xText)
OUString sMessage
sal_Int32 nIndex
sal_Int32 nNullable
const ColumnInfo & getColumnInfo(size_t _pos)
FormController(const css::uno::Reference< css::uno::XComponentContext > &_rxORB)
IMPL_LINK_NOARG(FormController, OnLoad, void *, void)
osl::Mutex m_aMutex
exports com.sun.star.form. control
const LanguageTag & GetUILanguageTag() const
sal_Int32 findValue(const css::uno::Sequence< T1 > &_rList, const T2 &_rValue)
#define FM_PROP_VALUE
Definition: fmprop.hxx:35
::std::vector< ColumnInfo > ColumnInfos
sal_uInt16 char char * pDesc
#define FM_PROP_ISMODIFIED
Definition: fmprop.hxx:114
OInteraction< css::task::XInteractionAbort > OInteractionAbort
static const AllSettings & GetSettings()
static VclPtr< vcl::Window > GetWindow(const css::uno::Reference< css::awt::XWindow > &rxWindow)
#define FM_PROP_DYNAMIC_CONTROL_BORDER
Definition: fmprop.hxx:138
bool controlsInitialized() const
bool getBOOL(const Any &_rAny)
sal_Int64 n
Reference< XTextComponent > xText
cppu::WeakComponentImplHelper< css::form::runtime::XFormController, css::form::runtime::XFilterController, css::awt::XFocusListener, css::form::XLoadListener, css::beans::XPropertyChangeListener, css::awt::XTextListener, css::awt::XItemListener, css::container::XContainerListener, css::util::XModifyListener, css::form::XConfirmDeleteListener, css::sdb::XSQLErrorListener, css::sdbc::XRowSetListener, css::sdb::XRowSetApproveListener, css::form::XDatabaseParameterListener, css::lang::XServiceInfo, css::form::XResetListener, css::frame::XDispatch, css::awt::XMouseListener, css::form::validation::XFormComponentValidityListener, css::task::XInteractionHandler, css::form::XGridControlListener, css::form::runtime::XFeatureInvalidation > FormController_BASE
#define FM_PROP_ISNEW
Definition: fmprop.hxx:115
sal_Int16 nId
IMPL_LINK_NOARG(XFormsPage, ItemSelectHdl, weld::TreeView &, void)
Definition: datanavi.cxx:242
Reference< XInteractionHandler > m_xInteractionHandler
Mode
Reference< XNumberFormatsSupplier > getNumberFormats(const Reference< XConnection > &_rxConn, bool _bAlloweDefault, const Reference< XComponentContext > &_rxContext)
bool bReadOnly
#define FM_PROP_AUTOINCREMENT
Definition: fmprop.hxx:48
bool isRowSetAlive(const Reference< XInterface > &_rxRowSet)
Definition: fmtools.cxx:371
const css::lang::Locale & getLocale(bool bResolveSystem=true) const
bool canUpdate(const Reference< XPropertySet > &_rxCursorSet)
#define FM_PROP_NAME
Definition: fmprop.hxx:29
Value
#define FM_PROP_CONTROL_BORDER_COLOR_FOCUS
Definition: fmprop.hxx:139
OUString SvxResId(const char *pId)
Definition: dialmgr.cxx:28
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
size_t pos
int nCount
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
OInteraction< css::task::XInteractionDisapprove > OInteractionDisapprove
#define FM_PROP_BOUNDFIELD
Definition: fmprop.hxx:101
sal_Int32 nRequiredGridColumn
if xFirstControlWithInputRequired is a grid control, then nRequiredGridColumn specifies the position ...
#define FMURL_CONFIRM_DELETION
Definition: fmurl.hxx:42
#define FM_PROP_CONTROL_BORDER_COLOR_MOUSE
Definition: fmprop.hxx:140
#define FM_PROP_CYCLE
Definition: fmprop.hxx:42
bool bAutoIncrement
Reference< XComponentContext > const m_xComponentContext
Reference< XPropertySet > xField
const OUString & getNumDecimalSep() const
#define DBG_UNHANDLED_EXCEPTION(...)
#define FM_PROP_SEARCHABLE
Definition: fmprop.hxx:50
#define DBG_ASSERT(sCon, aError)
int i
Reference< XTextListener > m_xListener
#define FM_PROP_FORM_OPERATIONS
Definition: fmprop.hxx:143
class SAL_NO_VTABLE XPropertySet
Definition: xmlexchg.hxx:28
OUString sName
void displayException(const Any &_rExcept, vcl::Window *_pParent)
Definition: fmtools.cxx:88
css::uno::Sequence< css::uno::Type > getTypes()
#define FM_PROP_ISNULLABLE
Definition: fmprop.hxx:124
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
static sal_Int32 getControllerFeatureSlotIdForURL(const OUString &_rMainURL)
retrieves the feature id for a given feature URL
unsigned char sal_Bool
void SAL_CALL elementRemoved(const css::container::ContainerEvent &Event) override
DECL_LISTENERMULTIPLEXER_END void SAL_CALL elementInserted(const css::container::ContainerEvent &Event) override
#define FM_PROP_FILTER
Definition: fmprop.hxx:47
css::uno::Type const & get()
const sal_Int16 nFormFeature
Reference< XComponentContext > getComponentContext(Reference< XMultiServiceFactory > const &factory)
css::uno::Sequence< T > concatSequences(const css::uno::Sequence< T > &rS1, const Ss &...rSn)
ColumnInfoCache(const Reference< XColumnsSupplier > &_rxColSupplier)
::std::map< css::uno::Reference< css::awt::XTextComponent >, OUString, FmXTextComponentLess > FmFilterRow
Sequence< PropertyValue > m_aValues
size_t getColumnCount() const
#define SAL_WARN_IF(condition, area, stream)
#define FM_ATTR_FILTER
Definition: fmprop.hxx:26
#define FM_PROP_ACTIVE_CONNECTION
Definition: fmprop.hxx:128
bool canInsert(const Reference< XPropertySet > &_rxCursorSet)
Reference< XGrid > xFirstGridWithInputRequiredColumn
the first grid control which contains a column which is bound to the given database column...
#define FM_PROP_CONTROL_BORDER_COLOR_INVALID
Definition: fmprop.hxx:141
#define FM_PROP_ACTIVECOMMAND
Definition: fmprop.hxx:125
#define FM_PROP_INPUT_REQUIRED
Definition: fmprop.hxx:144
void initializeControls(const Sequence< Reference< XControl > > &_rControls)
#define FM_PROP_ISREADONLY
Definition: fmprop.hxx:74
Reference< XColumn > xColumn
static sal_Int16 getFormFeatureForSlotId(sal_Int32 _nSlotId)
retrieves the css.form.runtime.FormFeature ID for a given slot ID
Reference< XConnection > getConnection(const Reference< XRowSet > &_rxRowSet)
::std::pair< MetaAction *, int > Component
bool hasProperty(const OUString &_rName, const Reference< XPropertySet > &_rxSet)
vcl::Window * get() const
Reference< XSingleServiceFactory > xFactory
Reference< XModel > xModel
class FmSearchEngine - Impl class for FmSearchDialog
css::uno::XInterface * next()
#define FM_PROP_DEFAULTCONTROL
Definition: fmprop.hxx:53
const char sServiceName[]
sal_Int32 nLength
void dispose()
#define FM_PROP_TEXT
Definition: fmprop.hxx:39
OUString getString(const Any &_rAny)
static css::uno::Reference< css::awt::XWindow > GetInterface(vcl::Window *pWindow)
css::uno::Reference< css::uno::XInterface > FormController_NewInstance_Impl(const css::uno::Reference< css::lang::XMultiServiceFactory > &_rxORB)
#define FM_PROP_REALNAME
Definition: fmprop.hxx:130
Reference< XControl > xFirstControlWithInputRequired
the first control which is bound to the given column, and which requires input
#define FM_ATTR_FORM_OPERATIONS
Definition: fmprop.hxx:27
OInteraction< css::task::XInteractionApprove > OInteractionApprove
struct _ADOIndex Index
AnyEventRef aEvent
void SAL_CALL createPeer(const css::uno::Reference< css::awt::XToolkit > &Toolkit, const css::uno::Reference< css::awt::XWindowPeer > &Parent) override
sal_uInt16 nPos
css::uno::Sequence< typename M::key_type > mapKeysToSequence(M const &map)
#define FM_PROP_ENABLED
Definition: fmprop.hxx:44
bool useDynamicBorderColor(DocumentType _eDocType)
determines whether for the given document type, dynamic control border coloring is enabled ...
css::uno::Any SAL_CALL makeAny(const SharedUNOComponent< INTERFACE, COMPONENT > &value)