bump product version to 5.0.4.1
[LibreOffice.git] / svx / source / form / filtnav.cxx
blob78a44cdf083cce963ff479dec928df9638535d7c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "filtnav.hxx"
21 #include "fmexch.hxx"
22 #include "fmhelp.hrc"
23 #include "fmitems.hxx"
24 #include "fmprop.hrc"
25 #include "svx/fmresids.hrc"
27 #include <com/sun/star/awt/XControlModel.hpp>
28 #include <com/sun/star/awt/XControl.hpp>
29 #include <com/sun/star/awt/XTextComponent.hpp>
30 #include <com/sun/star/form/runtime/XFormController.hpp>
31 #include <com/sun/star/lang/XUnoTunnel.hpp>
32 #include <com/sun/star/util/NumberFormatter.hpp>
33 #include <com/sun/star/beans/XFastPropertySet.hpp>
35 #include <comphelper/processfactory.hxx>
36 #include <comphelper/property.hxx>
37 #include <comphelper/sequence.hxx>
38 #include <comphelper/string.hxx>
39 #include <comphelper/uno3.hxx>
40 #include <connectivity/dbtools.hxx>
41 #include <connectivity/sqlnode.hxx>
42 #include <cppuhelper/implbase1.hxx>
43 #include <fmservs.hxx>
44 #include <fmshimp.hxx>
45 #include <sfx2/dispatch.hxx>
46 #include <sfx2/objitem.hxx>
47 #include <sfx2/objsh.hxx>
48 #include <sfx2/request.hxx>
49 #include <svx/dialmgr.hxx>
50 #include <svx/fmshell.hxx>
51 #include <svx/fmtools.hxx>
52 #include <svx/svxids.hrc>
53 #include <vcl/wrkwin.hxx>
54 #include <vcl/settings.hxx>
55 #include <tools/diagnose_ex.h>
56 #include <svtools/svlbitm.hxx>
57 #include "svtools/treelistentry.hxx"
58 #include "svtools/viewdataentry.hxx"
60 #include <functional>
62 #define DROP_ACTION_TIMER_INITIAL_TICKS 10
63 // solange dauert es, bis das Scrollen anspringt
64 #define DROP_ACTION_TIMER_SCROLL_TICKS 3
65 // in diesen Intervallen wird jeweils eine Zeile gescrollt
66 #define DROP_ACTION_TIMER_TICK_BASE 10
67 // das ist die Basis, mit der beide Angaben multipliziert werden (in ms)
69 using namespace ::svxform;
70 using namespace ::connectivity;
71 using namespace ::dbtools;
74 namespace svxform
78 using ::com::sun::star::uno::Reference;
79 using ::com::sun::star::lang::XMultiServiceFactory;
80 using ::com::sun::star::awt::TextEvent;
81 using ::com::sun::star::container::XIndexAccess;
82 using ::com::sun::star::uno::UNO_QUERY;
83 using ::com::sun::star::beans::XPropertySet;
84 using ::com::sun::star::form::runtime::XFormController;
85 using ::com::sun::star::form::runtime::XFilterController;
86 using ::com::sun::star::form::runtime::XFilterControllerListener;
87 using ::com::sun::star::form::runtime::FilterEvent;
88 using ::com::sun::star::lang::EventObject;
89 using ::com::sun::star::uno::RuntimeException;
90 using ::com::sun::star::form::XForm;
91 using ::com::sun::star::container::XChild;
92 using ::com::sun::star::awt::XControl;
93 using ::com::sun::star::sdbc::XConnection;
94 using ::com::sun::star::util::XNumberFormatsSupplier;
95 using ::com::sun::star::util::XNumberFormatter;
96 using ::com::sun::star::util::XNumberFormatter2;
97 using ::com::sun::star::util::NumberFormatter;
98 using ::com::sun::star::sdbc::XRowSet;
99 using ::com::sun::star::lang::Locale;
100 using ::com::sun::star::sdb::SQLContext;
101 using ::com::sun::star::uno::XInterface;
102 using ::com::sun::star::uno::UNO_QUERY_THROW;
103 using ::com::sun::star::uno::UNO_SET_THROW;
104 using ::com::sun::star::uno::Exception;
105 using ::com::sun::star::awt::XTextComponent;
106 using ::com::sun::star::uno::Sequence;
109 OFilterItemExchange::OFilterItemExchange()
110 : m_pFormItem(NULL)
114 void OFilterItemExchange::AddSupportedFormats()
116 AddFormat(getFormatId());
120 SotClipboardFormatId OFilterItemExchange::getFormatId()
122 static SotClipboardFormatId s_nFormat = static_cast<SotClipboardFormatId>(-1);
123 if (static_cast<SotClipboardFormatId>(-1) == s_nFormat)
125 s_nFormat = SotExchange::RegisterFormatName(OUString("application/x-openoffice;windows_formatname=\"form.FilterControlExchange\""));
126 DBG_ASSERT(static_cast<SotClipboardFormatId>(-1) != s_nFormat, "OFilterExchangeHelper::getFormatId: bad exchange id!");
128 return s_nFormat;
132 OLocalExchange* OFilterExchangeHelper::createExchange() const
134 return new OFilterItemExchange;
138 TYPEINIT0(FmFilterData);
139 Image FmFilterData::GetImage() const
141 return Image();
145 TYPEINIT1(FmParentData, FmFilterData);
147 FmParentData::~FmParentData()
149 for (::std::vector<FmFilterData*>::const_iterator i = m_aChildren.begin();
150 i != m_aChildren.end(); ++i)
151 delete (*i);
155 TYPEINIT1(FmFormItem, FmParentData);
157 Image FmFormItem::GetImage() const
159 ImageList aNavigatorImages( SVX_RES( RID_SVXIMGLIST_FMEXPL ) );
160 return aNavigatorImages.GetImage( RID_SVXIMG_FORM );
164 TYPEINIT1(FmFilterItems, FmParentData);
166 FmFilterItem* FmFilterItems::Find( const ::sal_Int32 _nFilterComponentIndex ) const
168 for ( ::std::vector< FmFilterData* >::const_iterator i = m_aChildren.begin();
169 i != m_aChildren.end();
173 FmFilterItem* pCondition = PTR_CAST( FmFilterItem, *i );
174 DBG_ASSERT( pCondition, "FmFilterItems::Find: Wrong element in container!" );
175 if ( _nFilterComponentIndex == pCondition->GetComponentIndex() )
176 return pCondition;
178 return NULL;
182 Image FmFilterItems::GetImage() const
184 ImageList aNavigatorImages( SVX_RES( RID_SVXIMGLIST_FMEXPL ) );
185 return aNavigatorImages.GetImage( RID_SVXIMG_FILTER );
189 TYPEINIT1(FmFilterItem, FmFilterData);
191 FmFilterItem::FmFilterItem( FmFilterItems* pParent,
192 const OUString& aFieldName,
193 const OUString& aText,
194 const sal_Int32 _nComponentIndex )
195 :FmFilterData(pParent, aText)
196 ,m_aFieldName(aFieldName)
197 ,m_nComponentIndex( _nComponentIndex )
202 Image FmFilterItem::GetImage() const
204 ImageList aNavigatorImages( SVX_RES( RID_SVXIMGLIST_FMEXPL ) );
205 return aNavigatorImages.GetImage( RID_SVXIMG_FIELD );
209 // Hints for communicatition between model and view
211 class FmFilterHint : public SfxHint
213 FmFilterData* m_pData;
215 public:
216 FmFilterHint(FmFilterData* pData):m_pData(pData){}
217 FmFilterData* GetData() const { return m_pData; }
221 class FmFilterInsertedHint : public FmFilterHint
223 sal_uLong m_nPos; // Position relative to the parent of the data
225 public:
226 FmFilterInsertedHint(FmFilterData* pData, sal_uLong nRelPos)
227 :FmFilterHint(pData)
228 ,m_nPos(nRelPos){}
230 sal_uLong GetPos() const { return m_nPos; }
234 class FmFilterRemovedHint : public FmFilterHint
236 public:
237 FmFilterRemovedHint(FmFilterData* pData)
238 :FmFilterHint(pData){}
242 class FmFilterTextChangedHint : public FmFilterHint
244 public:
245 FmFilterTextChangedHint(FmFilterData* pData)
246 :FmFilterHint(pData){}
250 class FilterClearingHint : public SfxHint
252 public:
253 FilterClearingHint(){}
257 class FmFilterCurrentChangedHint : public SfxHint
259 public:
260 FmFilterCurrentChangedHint(){}
264 // class FmFilterAdapter, Listener an den FilterControls
266 class FmFilterAdapter : public ::cppu::WeakImplHelper1< XFilterControllerListener >
268 FmFilterModel* m_pModel;
269 Reference< XIndexAccess > m_xControllers;
271 public:
272 FmFilterAdapter(FmFilterModel* pModel, const Reference< XIndexAccess >& xControllers);
274 // XEventListener
275 virtual void SAL_CALL disposing(const EventObject& Source) throw( RuntimeException, std::exception ) SAL_OVERRIDE;
277 // XFilterControllerListener
278 virtual void SAL_CALL predicateExpressionChanged( const FilterEvent& _Event ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
279 virtual void SAL_CALL disjunctiveTermRemoved( const FilterEvent& _Event ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
280 virtual void SAL_CALL disjunctiveTermAdded( const FilterEvent& _Event ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
282 // helpers
283 void dispose() throw( RuntimeException );
285 void AddOrRemoveListener( const Reference< XIndexAccess >& _rxControllers, const bool _bAdd );
287 static void setText(sal_Int32 nPos,
288 const FmFilterItem* pFilterItem,
289 const OUString& rText);
293 FmFilterAdapter::FmFilterAdapter(FmFilterModel* pModel, const Reference< XIndexAccess >& xControllers)
294 :m_pModel( pModel )
295 ,m_xControllers( xControllers )
297 AddOrRemoveListener( m_xControllers, true );
301 void FmFilterAdapter::dispose() throw( RuntimeException )
303 AddOrRemoveListener( m_xControllers, false );
307 void FmFilterAdapter::AddOrRemoveListener( const Reference< XIndexAccess >& _rxControllers, const bool _bAdd )
309 for (sal_Int32 i = 0, nLen = _rxControllers->getCount(); i < nLen; ++i)
311 Reference< XIndexAccess > xElement( _rxControllers->getByIndex(i), UNO_QUERY );
313 // step down
314 AddOrRemoveListener( xElement, _bAdd );
316 // handle this particular controller
317 Reference< XFilterController > xController( xElement, UNO_QUERY );
318 OSL_ENSURE( xController.is(), "FmFilterAdapter::InsertElements: no XFilterController, cannot sync data!" );
319 if ( xController.is() )
321 if ( _bAdd )
322 xController->addFilterControllerListener( this );
323 else
324 xController->removeFilterControllerListener( this );
330 void FmFilterAdapter::setText(sal_Int32 nRowPos,
331 const FmFilterItem* pFilterItem,
332 const OUString& rText)
334 FmFormItem* pFormItem = PTR_CAST( FmFormItem, pFilterItem->GetParent()->GetParent() );
338 Reference< XFilterController > xController( pFormItem->GetController(), UNO_QUERY_THROW );
339 xController->setPredicateExpression( pFilterItem->GetComponentIndex(), nRowPos, rText );
341 catch( const Exception& )
343 DBG_UNHANDLED_EXCEPTION();
348 // XEventListener
350 void SAL_CALL FmFilterAdapter::disposing(const EventObject& /*e*/) throw( RuntimeException, std::exception )
355 namespace
357 OUString lcl_getLabelName_nothrow( const Reference< XControl >& _rxControl )
359 OUString sLabelName;
362 Reference< XPropertySet > xModel( _rxControl->getModel(), UNO_QUERY_THROW );
363 sLabelName = getLabelName( xModel );
365 catch( const Exception& )
367 DBG_UNHANDLED_EXCEPTION();
369 return sLabelName;
372 Reference< XPropertySet > lcl_getBoundField_nothrow( const Reference< XControl >& _rxControl )
374 Reference< XPropertySet > xField;
377 Reference< XPropertySet > xModelProps( _rxControl->getModel(), UNO_QUERY_THROW );
378 xField.set( xModelProps->getPropertyValue( FM_PROP_BOUNDFIELD ), UNO_QUERY_THROW );
380 catch( const Exception& )
382 DBG_UNHANDLED_EXCEPTION();
384 return xField;
388 // XFilterControllerListener
390 void FmFilterAdapter::predicateExpressionChanged( const FilterEvent& _Event ) throw( RuntimeException, std::exception )
392 SolarMutexGuard aGuard;
394 if ( !m_pModel )
395 return;
397 // the controller which sent the event
398 Reference< XFormController > xController( _Event.Source, UNO_QUERY_THROW );
399 Reference< XFilterController > xFilterController( _Event.Source, UNO_QUERY_THROW );
400 Reference< XForm > xForm( xController->getModel(), UNO_QUERY_THROW );
402 FmFormItem* pFormItem = m_pModel->Find( m_pModel->m_aChildren, xForm );
403 OSL_ENSURE( pFormItem, "FmFilterAdapter::predicateExpressionChanged: don't know this form!" );
404 if ( !pFormItem )
405 return;
407 const sal_Int32 nActiveTerm( xFilterController->getActiveTerm() );
409 FmFilterItems* pFilter = PTR_CAST( FmFilterItems, pFormItem->GetChildren()[ nActiveTerm ] );
410 FmFilterItem* pFilterItem = pFilter->Find( _Event.FilterComponent );
411 if ( pFilterItem )
413 if ( !_Event.PredicateExpression.isEmpty())
415 pFilterItem->SetText( _Event.PredicateExpression );
416 // UI benachrichtigen
417 FmFilterTextChangedHint aChangeHint(pFilterItem);
418 m_pModel->Broadcast( aChangeHint );
420 else
422 // no text anymore so remove the condition
423 m_pModel->Remove(pFilterItem);
426 else
428 // searching the component by field name
429 OUString aFieldName( lcl_getLabelName_nothrow( xFilterController->getFilterComponent( _Event.FilterComponent ) ) );
431 pFilterItem = new FmFilterItem( pFilter, aFieldName, _Event.PredicateExpression, _Event.FilterComponent );
432 m_pModel->Insert(pFilter->GetChildren().end(), pFilterItem);
435 // ensure there's one empty term in the filter, just in case the active term was previously empty
436 m_pModel->EnsureEmptyFilterRows( *pFormItem );
440 void SAL_CALL FmFilterAdapter::disjunctiveTermRemoved( const FilterEvent& _Event ) throw (RuntimeException, std::exception)
442 SolarMutexGuard aGuard;
444 Reference< XFormController > xController( _Event.Source, UNO_QUERY_THROW );
445 Reference< XFilterController > xFilterController( _Event.Source, UNO_QUERY_THROW );
446 Reference< XForm > xForm( xController->getModel(), UNO_QUERY_THROW );
448 FmFormItem* pFormItem = m_pModel->Find( m_pModel->m_aChildren, xForm );
449 OSL_ENSURE( pFormItem, "FmFilterAdapter::disjunctiveTermRemoved: don't know this form!" );
450 if ( !pFormItem )
451 return;
453 ::std::vector< FmFilterData* >& rTermItems = pFormItem->GetChildren();
454 const bool bValidIndex = ( _Event.DisjunctiveTerm >= 0 ) && ( (size_t)_Event.DisjunctiveTerm < rTermItems.size() );
455 OSL_ENSURE( bValidIndex, "FmFilterAdapter::disjunctiveTermRemoved: invalid term index!" );
456 if ( !bValidIndex )
457 return;
459 // if the first term was removed, then the to-be first term needs its text updated
460 if ( _Event.DisjunctiveTerm == 0 )
462 rTermItems[1]->SetText( SVX_RESSTR(RID_STR_FILTER_FILTER_FOR));
463 FmFilterTextChangedHint aChangeHint( rTermItems[1] );
464 m_pModel->Broadcast( aChangeHint );
467 // finally remove the entry from the model
468 m_pModel->Remove( rTermItems.begin() + _Event.DisjunctiveTerm );
470 // ensure there's one empty term in the filter, just in case the currently removed one was the last empty one
471 m_pModel->EnsureEmptyFilterRows( *pFormItem );
475 void SAL_CALL FmFilterAdapter::disjunctiveTermAdded( const FilterEvent& _Event ) throw (RuntimeException, std::exception)
477 SolarMutexGuard aGuard;
479 Reference< XFormController > xController( _Event.Source, UNO_QUERY_THROW );
480 Reference< XFilterController > xFilterController( _Event.Source, UNO_QUERY_THROW );
481 Reference< XForm > xForm( xController->getModel(), UNO_QUERY_THROW );
483 FmFormItem* pFormItem = m_pModel->Find( m_pModel->m_aChildren, xForm );
484 OSL_ENSURE( pFormItem, "FmFilterAdapter::disjunctiveTermAdded: don't know this form!" );
485 if ( !pFormItem )
486 return;
488 const sal_Int32 nInsertPos = _Event.DisjunctiveTerm;
489 bool bValidIndex = ( nInsertPos >= 0 ) && ( (size_t)nInsertPos <= pFormItem->GetChildren().size() );
490 if ( !bValidIndex )
492 OSL_FAIL( "FmFilterAdapter::disjunctiveTermAdded: invalid index!" );
493 return;
496 const ::std::vector< FmFilterData* >::iterator insertPos = pFormItem->GetChildren().begin() + nInsertPos;
498 FmFilterItems* pFilterItems = new FmFilterItems(pFormItem, SVX_RESSTR(RID_STR_FILTER_FILTER_OR));
499 m_pModel->Insert( insertPos, pFilterItems );
502 TYPEINIT1(FmFilterModel, FmParentData);
504 FmFilterModel::FmFilterModel()
505 :FmParentData(NULL, OUString())
506 ,OSQLParserClient(comphelper::getProcessComponentContext())
507 ,m_pAdapter(NULL)
508 ,m_pCurrentItems(NULL)
513 FmFilterModel::~FmFilterModel()
515 Clear();
519 void FmFilterModel::Clear()
521 // notify
522 FilterClearingHint aClearedHint;
523 Broadcast( aClearedHint );
525 // lose endings
526 if (m_pAdapter)
528 m_pAdapter->dispose();
529 m_pAdapter->release();
530 m_pAdapter= NULL;
533 m_pCurrentItems = NULL;
534 m_xController = NULL;
535 m_xControllers = NULL;
537 for (::std::vector<FmFilterData*>::const_iterator i = m_aChildren.begin();
538 i != m_aChildren.end(); ++i)
539 delete (*i);
541 m_aChildren.clear();
545 void FmFilterModel::Update(const Reference< XIndexAccess > & xControllers, const Reference< XFormController > & xCurrent)
547 if ( xCurrent == m_xController )
548 return;
550 if (!xControllers.is())
552 Clear();
553 return;
556 // there is only a new current controller
557 if ( m_xControllers != xControllers )
559 Clear();
561 m_xControllers = xControllers;
562 Update(m_xControllers, this);
564 DBG_ASSERT(xCurrent.is(), "FmFilterModel::Update(...) no current controller");
566 // Listening for TextChanges
567 m_pAdapter = new FmFilterAdapter(this, xControllers);
568 m_pAdapter->acquire();
570 SetCurrentController(xCurrent);
571 EnsureEmptyFilterRows( *this );
573 else
574 SetCurrentController(xCurrent);
578 void FmFilterModel::Update(const Reference< XIndexAccess > & xControllers, FmParentData* pParent)
582 sal_Int32 nCount = xControllers->getCount();
583 for ( sal_Int32 i = 0; i < nCount; ++i )
585 Reference< XFormController > xController( xControllers->getByIndex(i), UNO_QUERY_THROW );
587 Reference< XPropertySet > xFormProperties( xController->getModel(), UNO_QUERY_THROW );
588 OUString aName;
589 OSL_VERIFY( xFormProperties->getPropertyValue( FM_PROP_NAME ) >>= aName );
591 // Insert a new item for the form
592 FmFormItem* pFormItem = new FmFormItem( pParent, xController, aName );
593 Insert( pParent->GetChildren().end(), pFormItem );
595 Reference< XFilterController > xFilterController( pFormItem->GetFilterController(), UNO_SET_THROW );
597 // insert the existing filters for the form
598 OUString aTitle(SVX_RESSTR(RID_STR_FILTER_FILTER_FOR));
600 Sequence< Sequence< OUString > > aExpressions = xFilterController->getPredicateExpressions();
601 for ( const Sequence< OUString >* pConjunctionTerm = aExpressions.getConstArray();
602 pConjunctionTerm != aExpressions.getConstArray() + aExpressions.getLength();
603 ++pConjunctionTerm
606 // we always display one row, even if there's no term to be displayed
607 FmFilterItems* pFilterItems = new FmFilterItems( pFormItem, aTitle );
608 Insert( pFormItem->GetChildren().end(), pFilterItems );
610 const Sequence< OUString >& rDisjunction( *pConjunctionTerm );
611 for ( const OUString* pDisjunctiveTerm = rDisjunction.getConstArray();
612 pDisjunctiveTerm != rDisjunction.getConstArray() + rDisjunction.getLength();
613 ++pDisjunctiveTerm
616 if ( pDisjunctiveTerm->isEmpty() )
617 // no condition for this particular component in this particular conjunction term
618 continue;
620 const sal_Int32 nComponentIndex = pDisjunctiveTerm - rDisjunction.getConstArray();
622 // determine the display name of the control
623 const Reference< XControl > xFilterControl( xFilterController->getFilterComponent( nComponentIndex ) );
624 const OUString sDisplayName( lcl_getLabelName_nothrow( xFilterControl ) );
626 // insert a new entry
627 FmFilterItem* pANDCondition = new FmFilterItem( pFilterItems, sDisplayName, *pDisjunctiveTerm, nComponentIndex );
628 Insert( pFilterItems->GetChildren().end(), pANDCondition );
631 // title for the next conditions
632 aTitle = SVX_RESSTR( RID_STR_FILTER_FILTER_OR );
635 // now add dependent controllers
636 Update( xController, pFormItem );
639 catch( const Exception& )
641 DBG_UNHANDLED_EXCEPTION();
646 FmFormItem* FmFilterModel::Find(const ::std::vector<FmFilterData*>& rItems, const Reference< XFormController > & xController) const
648 for (::std::vector<FmFilterData*>::const_iterator i = rItems.begin();
649 i != rItems.end(); ++i)
651 FmFormItem* pForm = PTR_CAST(FmFormItem,*i);
652 if (pForm)
654 if ( xController == pForm->GetController() )
655 return pForm;
656 else
658 pForm = Find(pForm->GetChildren(), xController);
659 if (pForm)
660 return pForm;
664 return NULL;
668 FmFormItem* FmFilterModel::Find(const ::std::vector<FmFilterData*>& rItems, const Reference< XForm >& xForm) const
670 for (::std::vector<FmFilterData*>::const_iterator i = rItems.begin();
671 i != rItems.end(); ++i)
673 FmFormItem* pForm = PTR_CAST(FmFormItem,*i);
674 if (pForm)
676 if (xForm == pForm->GetController()->getModel())
677 return pForm;
678 else
680 pForm = Find(pForm->GetChildren(), xForm);
681 if (pForm)
682 return pForm;
686 return NULL;
690 void FmFilterModel::SetCurrentController(const Reference< XFormController > & xCurrent)
692 if ( xCurrent == m_xController )
693 return;
695 m_xController = xCurrent;
697 FmFormItem* pItem = Find( m_aChildren, xCurrent );
698 if ( !pItem )
699 return;
703 Reference< XFilterController > xFilterController( m_xController, UNO_QUERY_THROW );
704 const sal_Int32 nActiveTerm( xFilterController->getActiveTerm() );
705 if ( pItem->GetChildren().size() > (size_t)nActiveTerm )
707 SetCurrentItems( static_cast< FmFilterItems* >( pItem->GetChildren()[ nActiveTerm ] ) );
710 catch( const Exception& )
712 DBG_UNHANDLED_EXCEPTION();
717 void FmFilterModel::AppendFilterItems( FmFormItem& _rFormItem )
719 // insert the condition behind the last filter items
720 ::std::vector<FmFilterData*>::reverse_iterator iter;
721 for ( iter = _rFormItem.GetChildren().rbegin();
722 iter != _rFormItem.GetChildren().rend();
723 ++iter
726 if ((*iter)->ISA(FmFilterItems))
727 break;
730 sal_Int32 nInsertPos = iter.base() - _rFormItem.GetChildren().begin();
731 // delegate this to the FilterController, it will notify us, which will let us update our model
734 Reference< XFilterController > xFilterController( _rFormItem.GetFilterController(), UNO_SET_THROW );
735 if ( nInsertPos >= xFilterController->getDisjunctiveTerms() )
736 xFilterController->appendEmptyDisjunctiveTerm();
738 catch( const Exception& )
740 DBG_UNHANDLED_EXCEPTION();
745 void FmFilterModel::Insert(const ::std::vector<FmFilterData*>::iterator& rPos, FmFilterData* pData)
747 ::std::vector<FmFilterData*>& rItems = pData->GetParent()->GetChildren();
748 sal_uLong nPos = rPos == rItems.end() ? CONTAINER_APPEND : rPos - rItems.begin();
749 if (nPos == CONTAINER_APPEND)
751 rItems.push_back(pData);
752 nPos = rItems.size() - 1;
754 else
756 rItems.insert(rPos, pData);
759 // UI benachrichtigen
760 FmFilterInsertedHint aInsertedHint(pData, nPos);
761 Broadcast( aInsertedHint );
765 void FmFilterModel::Remove(FmFilterData* pData)
767 FmParentData* pParent = pData->GetParent();
768 ::std::vector<FmFilterData*>& rItems = pParent->GetChildren();
770 // erase the item from the model
771 ::std::vector<FmFilterData*>::iterator i = ::std::find(rItems.begin(), rItems.end(), pData);
772 DBG_ASSERT(i != rItems.end(), "FmFilterModel::Remove(): unknown Item");
773 // position within the parent
774 sal_Int32 nPos = i - rItems.begin();
775 if (pData->ISA(FmFilterItems))
777 FmFormItem* pFormItem = static_cast<FmFormItem*>(pParent);
781 Reference< XFilterController > xFilterController( pFormItem->GetFilterController(), UNO_SET_THROW );
783 bool bEmptyLastTerm = ( ( nPos == 0 ) && xFilterController->getDisjunctiveTerms() == 1 );
784 if ( bEmptyLastTerm )
786 // remove all children (by setting an empty predicate expression)
787 ::std::vector< FmFilterData* >& rChildren = static_cast<FmFilterItems*>(pData)->GetChildren();
788 while ( !rChildren.empty() )
790 ::std::vector< FmFilterData* >::iterator removePos = rChildren.end() - 1;
791 FmFilterItem* pFilterItem = PTR_CAST( FmFilterItem, *removePos );
792 FmFilterAdapter::setText( nPos, pFilterItem, OUString() );
793 Remove( removePos );
796 else
798 xFilterController->removeDisjunctiveTerm( nPos );
801 catch( const Exception& )
803 DBG_UNHANDLED_EXCEPTION();
806 else // FormItems can not be deleted
808 FmFilterItem* pFilterItem = PTR_CAST(FmFilterItem, pData);
810 // if its the last condition remove the parent
811 if (rItems.size() == 1)
812 Remove(pFilterItem->GetParent());
813 else
815 // find the position of the father within his father
816 ::std::vector<FmFilterData*>& rParentParentItems = pData->GetParent()->GetParent()->GetChildren();
817 ::std::vector<FmFilterData*>::iterator j = ::std::find(rParentParentItems.begin(), rParentParentItems.end(), pFilterItem->GetParent());
818 DBG_ASSERT(j != rParentParentItems.end(), "FmFilterModel::Remove(): unknown Item");
819 sal_Int32 nParentPos = j - rParentParentItems.begin();
821 // EmptyText removes the filter
822 FmFilterAdapter::setText(nParentPos, pFilterItem, OUString());
823 Remove( i );
829 void FmFilterModel::Remove( const ::std::vector<FmFilterData*>::iterator& rPos )
831 // remove from parent's child list
832 FmFilterData* pData = *rPos;
833 pData->GetParent()->GetChildren().erase( rPos );
835 // notify the view, this will remove the actual SvTreeListEntry
836 FmFilterRemovedHint aRemoveHint( pData );
837 Broadcast( aRemoveHint );
839 delete pData;
843 bool FmFilterModel::ValidateText(FmFilterItem* pItem, OUString& rText, OUString& rErrorMsg) const
845 FmFormItem* pFormItem = PTR_CAST( FmFormItem, pItem->GetParent()->GetParent() );
848 Reference< XFormController > xFormController( pFormItem->GetController() );
849 // obtain the connection of the form belonging to the controller
850 Reference< XRowSet > xRowSet( xFormController->getModel(), UNO_QUERY_THROW );
851 Reference< XConnection > xConnection( getConnection( xRowSet ) );
853 // obtain a number formatter for this connection
854 // TODO: shouldn't this be cached?
855 Reference< XNumberFormatsSupplier > xFormatSupplier = getNumberFormats( xConnection, true );
856 Reference< XNumberFormatter > xFormatter( NumberFormatter::create( comphelper::getProcessComponentContext() ), UNO_QUERY_THROW );
857 xFormatter->attachNumberFormatsSupplier( xFormatSupplier );
859 // get the field (database column) which the item is responsible for
860 Reference< XFilterController > xFilterController( xFormController, UNO_QUERY_THROW );
861 Reference< XPropertySet > xField( lcl_getBoundField_nothrow( xFilterController->getFilterComponent( pItem->GetComponentIndex() ) ), UNO_SET_THROW );
863 // parse the given text as filter predicate
864 OUString aErr, aTxt( rText );
865 std::shared_ptr< OSQLParseNode > pParseNode = predicateTree( aErr, aTxt, xFormatter, xField );
866 rErrorMsg = aErr;
867 rText = aTxt;
868 if ( pParseNode != nullptr )
870 OUString aPreparedText;
871 Locale aAppLocale = Application::GetSettings().GetUILanguageTag().getLocale();
872 pParseNode->parseNodeToPredicateStr(
873 aPreparedText, xConnection, xFormatter, xField, OUString(), aAppLocale, '.', getParseContext() );
874 rText = aPreparedText;
875 return true;
878 catch( const Exception& )
880 DBG_UNHANDLED_EXCEPTION();
883 return false;
887 void FmFilterModel::Append(FmFilterItems* pItems, FmFilterItem* pFilterItem)
889 Insert(pItems->GetChildren().end(), pFilterItem);
893 void FmFilterModel::SetTextForItem(FmFilterItem* pItem, const OUString& rText)
895 ::std::vector<FmFilterData*>& rItems = pItem->GetParent()->GetParent()->GetChildren();
896 ::std::vector<FmFilterData*>::iterator i = ::std::find(rItems.begin(), rItems.end(), pItem->GetParent());
897 sal_Int32 nParentPos = i - rItems.begin();
899 FmFilterAdapter::setText(nParentPos, pItem, rText);
901 if (rText.isEmpty())
902 Remove(pItem);
903 else
905 // Change the text
906 pItem->SetText(rText);
907 FmFilterTextChangedHint aChangeHint(pItem);
908 Broadcast( aChangeHint );
913 void FmFilterModel::SetCurrentItems(FmFilterItems* pCurrent)
915 if (m_pCurrentItems == pCurrent)
916 return;
918 // search for the condition
919 if (pCurrent)
921 FmFormItem* pFormItem = static_cast<FmFormItem*>(pCurrent->GetParent());
922 ::std::vector<FmFilterData*>& rItems = pFormItem->GetChildren();
923 ::std::vector<FmFilterData*>::const_iterator i = ::std::find(rItems.begin(), rItems.end(), pCurrent);
925 if (i != rItems.end())
927 // determine the filter position
928 sal_Int32 nPos = i - rItems.begin();
931 Reference< XFilterController > xFilterController( pFormItem->GetFilterController(), UNO_SET_THROW );
932 xFilterController->setActiveTerm( nPos );
934 catch( const Exception& )
936 DBG_UNHANDLED_EXCEPTION();
939 if ( m_xController != pFormItem->GetController() )
940 // calls SetCurrentItems again
941 SetCurrentController( pFormItem->GetController() );
942 else
943 m_pCurrentItems = pCurrent;
945 else
946 m_pCurrentItems = NULL;
948 else
949 m_pCurrentItems = NULL;
952 // UI benachrichtigen
953 FmFilterCurrentChangedHint aHint;
954 Broadcast( aHint );
958 void FmFilterModel::EnsureEmptyFilterRows( FmParentData& _rItem )
960 // checks whether for each form there's one free level for input
961 ::std::vector< FmFilterData* >& rChildren = _rItem.GetChildren();
962 bool bAppendLevel = _rItem.ISA( FmFormItem );
964 for ( ::std::vector<FmFilterData*>::iterator i = rChildren.begin();
965 i != rChildren.end();
969 FmFilterItems* pItems = PTR_CAST(FmFilterItems, *i);
970 if ( pItems && pItems->GetChildren().empty() )
972 bAppendLevel = false;
973 break;
976 FmFormItem* pFormItem = PTR_CAST(FmFormItem, *i);
977 if (pFormItem)
979 EnsureEmptyFilterRows( *pFormItem );
980 continue;
984 if ( bAppendLevel )
986 FmFormItem* pFormItem = PTR_CAST( FmFormItem, &_rItem );
987 OSL_ENSURE( pFormItem, "FmFilterModel::EnsureEmptyFilterRows: no FmFormItem, but a FmFilterItems child?" );
988 if ( pFormItem )
989 AppendFilterItems( *pFormItem );
993 class FmFilterItemsString : public SvLBoxString
995 public:
996 FmFilterItemsString( SvTreeListEntry* pEntry, sal_uInt16 nFlags, const OUString& rStr )
997 :SvLBoxString(pEntry,nFlags,rStr){}
999 virtual void Paint(const Point& rPos, SvTreeListBox& rDev, vcl::RenderContext& rRenderContext,
1000 const SvViewDataEntry* pView, const SvTreeListEntry* pEntry) SAL_OVERRIDE;
1001 virtual void InitViewData( SvTreeListBox* pView,SvTreeListEntry* pEntry, SvViewDataItem* pViewData) SAL_OVERRIDE;
1004 const int nxDBmp = 12;
1006 void FmFilterItemsString::Paint(const Point& rPos, SvTreeListBox& rDev, vcl::RenderContext& rRenderContext,
1007 const SvViewDataEntry* /*pView*/, const SvTreeListEntry* pEntry)
1009 FmFilterItems* pRow = static_cast<FmFilterItems*>(pEntry->GetUserData());
1010 FmFormItem* pForm = static_cast<FmFormItem*>(pRow->GetParent());
1012 // current filter is significant painted
1013 const bool bIsCurrentFilter = pForm->GetChildren()[ pForm->GetFilterController()->getActiveTerm() ] == pRow;
1014 if (bIsCurrentFilter)
1016 rRenderContext.Push(PushFlags::LINECOLOR);
1017 rRenderContext.SetLineColor(rRenderContext.GetTextColor());
1019 Rectangle aRect(rPos, GetSize(&rDev, pEntry));
1020 Point aFirst(rPos.X(), aRect.Bottom() - 6);
1021 Point aSecond(aFirst .X() + 2, aFirst.Y() + 3);
1023 rRenderContext.DrawLine(aFirst, aSecond);
1025 aFirst = aSecond;
1026 aFirst.X() += 1;
1027 aSecond.X() += 6;
1028 aSecond.Y() -= 5;
1030 rRenderContext.DrawLine(aFirst, aSecond);
1031 rRenderContext.Pop();
1034 rRenderContext.DrawText(Point(rPos.X() + nxDBmp, rPos.Y()), GetText());
1038 void FmFilterItemsString::InitViewData( SvTreeListBox* pView,SvTreeListEntry* pEntry, SvViewDataItem* pViewData)
1040 if( !pViewData )
1041 pViewData = pView->GetViewDataItem( pEntry, this );
1043 Size aSize(pView->GetTextWidth(GetText()), pView->GetTextHeight());
1044 aSize.Width() += nxDBmp;
1045 pViewData->maSize = aSize;
1048 class FmFilterString : public SvLBoxString
1050 OUString m_aName;
1052 public:
1053 FmFilterString( SvTreeListEntry* pEntry, sal_uInt16 nFlags, const OUString& rStr, const OUString& aName)
1054 : SvLBoxString(pEntry,nFlags,rStr)
1055 , m_aName(aName)
1057 m_aName += ": ";
1060 virtual void Paint(const Point& rPos, SvTreeListBox& rDev, vcl::RenderContext& rRenderContext,
1061 const SvViewDataEntry* pView, const SvTreeListEntry* pEntry) SAL_OVERRIDE;
1062 virtual void InitViewData( SvTreeListBox* pView,SvTreeListEntry* pEntry, SvViewDataItem* pViewData) SAL_OVERRIDE;
1065 const int nxD = 4;
1068 void FmFilterString::InitViewData( SvTreeListBox* pView,SvTreeListEntry* pEntry, SvViewDataItem* pViewData)
1070 if( !pViewData )
1071 pViewData = pView->GetViewDataItem( pEntry, this );
1073 vcl::Font aOldFont( pView->GetFont());
1074 vcl::Font aFont( aOldFont );
1075 aFont.SetWeight(WEIGHT_BOLD);
1076 pView->Control::SetFont( aFont );
1078 Size aSize(pView->GetTextWidth(m_aName), pView->GetTextHeight());
1079 pView->Control::SetFont( aOldFont );
1080 aSize.Width() += pView->GetTextWidth(GetText()) + nxD;
1081 pViewData->maSize = aSize;
1085 void FmFilterString::Paint(const Point& rPos, SvTreeListBox& rDev, vcl::RenderContext& rRenderContext,
1086 const SvViewDataEntry* /*pView*/, const SvTreeListEntry* /*pEntry*/)
1088 rRenderContext.Push(PushFlags::FONT);
1089 vcl::Font aFont(rRenderContext.GetFont());
1090 aFont.SetWeight(WEIGHT_BOLD);
1091 rRenderContext.SetFont(aFont);
1093 Point aPos(rPos);
1094 rRenderContext.DrawText(aPos, m_aName);
1096 // position for the second text
1097 aPos.X() += rDev.GetTextWidth(m_aName) + nxD;
1098 rRenderContext.Pop();
1099 rDev.DrawText(aPos, GetText());
1102 FmFilterNavigator::FmFilterNavigator( vcl::Window* pParent )
1103 :SvTreeListBox( pParent, WB_HASBUTTONS|WB_HASLINES|WB_BORDER|WB_HASBUTTONSATROOT )
1104 ,m_pModel( NULL )
1105 ,m_pEditingCurrently( NULL )
1106 ,m_aControlExchange( this )
1107 ,m_aTimerCounter( 0 )
1108 ,m_aDropActionType( DA_SCROLLUP )
1110 SetHelpId( HID_FILTER_NAVIGATOR );
1113 ImageList aNavigatorImages( SVX_RES( RID_SVXIMGLIST_FMEXPL ) );
1114 SetNodeBitmaps(
1115 aNavigatorImages.GetImage( RID_SVXIMG_COLLAPSEDNODE ),
1116 aNavigatorImages.GetImage( RID_SVXIMG_EXPANDEDNODE )
1120 m_pModel = new FmFilterModel();
1121 StartListening( *m_pModel );
1123 EnableInplaceEditing( true );
1124 SetSelectionMode(MULTIPLE_SELECTION);
1126 SetDragDropMode(DragDropMode::ALL);
1128 m_aDropActionTimer.SetTimeoutHdl(LINK(this, FmFilterNavigator, OnDropActionTimer));
1132 FmFilterNavigator::~FmFilterNavigator()
1134 disposeOnce();
1137 void FmFilterNavigator::dispose()
1139 EndListening( *m_pModel );
1140 delete m_pModel;
1141 SvTreeListBox::dispose();
1145 void FmFilterNavigator::UpdateContent(const Reference< XIndexAccess > & xControllers, const Reference< XFormController > & xCurrent)
1147 if (xCurrent == m_pModel->GetCurrentController())
1148 return;
1150 m_pModel->Update(xControllers, xCurrent);
1152 // expand the filters for the current controller
1153 SvTreeListEntry* pEntry = FindEntry(m_pModel->GetCurrentForm());
1154 if (pEntry && !IsExpanded(pEntry))
1156 SelectAll(false);
1158 if (!IsExpanded(pEntry))
1159 Expand(pEntry);
1161 pEntry = FindEntry(m_pModel->GetCurrentItems());
1162 if (pEntry)
1164 if (!IsExpanded(pEntry))
1165 Expand(pEntry);
1166 Select(pEntry, true);
1172 bool FmFilterNavigator::EditingEntry( SvTreeListEntry* pEntry, Selection& rSelection )
1174 m_pEditingCurrently = pEntry;
1175 if (!SvTreeListBox::EditingEntry( pEntry, rSelection ))
1176 return false;
1178 return pEntry && static_cast<FmFilterData*>(pEntry->GetUserData())->ISA(FmFilterItem);
1182 bool FmFilterNavigator::EditedEntry( SvTreeListEntry* pEntry, const OUString& rNewText )
1184 DBG_ASSERT(pEntry == m_pEditingCurrently, "FmFilterNavigator::EditedEntry: suspicious entry!");
1185 m_pEditingCurrently = NULL;
1187 if (EditingCanceled())
1188 return true;
1190 DBG_ASSERT(static_cast<FmFilterData*>(pEntry->GetUserData())->ISA(FmFilterItem),
1191 "FmFilterNavigator::EditedEntry() wrong entry");
1193 OUString aText(comphelper::string::strip(rNewText, ' '));
1194 if (aText.isEmpty())
1196 // deleting the entry asynchron
1197 PostUserEvent(LINK(this, FmFilterNavigator, OnRemove), pEntry, true);
1199 else
1201 OUString aErrorMsg;
1203 if (m_pModel->ValidateText(static_cast<FmFilterItem*>(pEntry->GetUserData()), aText, aErrorMsg))
1205 GrabFocus();
1206 // this will set the text at the FmFilterItem, as well as update any filter controls
1207 // which are connected to this particular entry
1208 m_pModel->SetTextForItem( static_cast< FmFilterItem* >( pEntry->GetUserData() ), aText );
1210 SetCursor( pEntry, true );
1211 SetEntryText( pEntry, aText );
1213 else
1215 // display the error and return sal_False
1216 SQLContext aError;
1217 aError.Message = SVX_RESSTR(RID_STR_SYNTAXERROR);
1218 aError.Details = aErrorMsg;
1219 displayException(aError, this);
1221 return false;
1224 return true;
1228 IMPL_LINK( FmFilterNavigator, OnRemove, SvTreeListEntry*, pEntry )
1230 // now remove the entry
1231 m_pModel->Remove(static_cast<FmFilterData*>(pEntry->GetUserData()));
1232 return 0L;
1236 IMPL_LINK_NOARG_TYPED(FmFilterNavigator, OnDropActionTimer, Timer *, void)
1238 if (--m_aTimerCounter > 0)
1239 return;
1241 switch (m_aDropActionType)
1243 case DA_SCROLLUP :
1244 ScrollOutputArea(1);
1245 m_aTimerCounter = DROP_ACTION_TIMER_SCROLL_TICKS;
1246 break;
1247 case DA_SCROLLDOWN :
1248 ScrollOutputArea(-1);
1249 m_aTimerCounter = DROP_ACTION_TIMER_SCROLL_TICKS;
1250 break;
1251 case DA_EXPANDNODE:
1253 SvTreeListEntry* pToExpand = GetEntry(m_aTimerTriggered);
1254 if (pToExpand && (GetChildCount(pToExpand) > 0) && !IsExpanded(pToExpand))
1255 Expand(pToExpand);
1256 m_aDropActionTimer.Stop();
1258 break;
1264 sal_Int8 FmFilterNavigator::AcceptDrop( const AcceptDropEvent& rEvt )
1266 Point aDropPos = rEvt.maPosPixel;
1268 // possible DropActions scroll and expand
1269 if (rEvt.mbLeaving)
1271 if (m_aDropActionTimer.IsActive())
1272 m_aDropActionTimer.Stop();
1274 else
1276 bool bNeedTrigger = false;
1277 // first entry ?
1278 if ((aDropPos.Y() >= 0) && (aDropPos.Y() < GetEntryHeight()))
1280 m_aDropActionType = DA_SCROLLUP;
1281 bNeedTrigger = true;
1283 else
1285 if ((aDropPos.Y() < GetSizePixel().Height()) && (aDropPos.Y() >= GetSizePixel().Height() - GetEntryHeight()))
1287 m_aDropActionType = DA_SCROLLDOWN;
1288 bNeedTrigger = true;
1290 else
1291 { // is it an entry with children, and not yet expanded?
1292 SvTreeListEntry* pDropppedOn = GetEntry(aDropPos);
1293 if (pDropppedOn && (GetChildCount(pDropppedOn) > 0) && !IsExpanded(pDropppedOn))
1295 // -> expand
1296 m_aDropActionType = DA_EXPANDNODE;
1297 bNeedTrigger = true;
1301 if (bNeedTrigger && (m_aTimerTriggered != aDropPos))
1303 m_aTimerCounter = DROP_ACTION_TIMER_INITIAL_TICKS;
1304 // remember DropPos because there are QueryDrops even though the mouse was not moved
1305 m_aTimerTriggered = aDropPos;
1306 if (!m_aDropActionTimer.IsActive())
1308 m_aDropActionTimer.SetTimeout(DROP_ACTION_TIMER_TICK_BASE);
1309 m_aDropActionTimer.Start();
1312 else if (!bNeedTrigger)
1313 m_aDropActionTimer.Stop();
1316 if (!m_aControlExchange.isDragSource())
1317 return DND_ACTION_NONE;
1319 if (!OFilterItemExchange::hasFormat(GetDataFlavorExVector()))
1320 return DND_ACTION_NONE;
1322 // do we conain the formitem?
1323 if (!FindEntry(m_aControlExchange->getFormItem()))
1324 return DND_ACTION_NONE;
1326 SvTreeListEntry* pDropTarget = GetEntry(aDropPos);
1327 if (!pDropTarget)
1328 return DND_ACTION_NONE;
1330 FmFilterData* pData = static_cast<FmFilterData*>(pDropTarget->GetUserData());
1331 FmFormItem* pForm = NULL;
1332 if (pData->ISA(FmFilterItem))
1334 pForm = PTR_CAST(FmFormItem,pData->GetParent()->GetParent());
1335 if (pForm != m_aControlExchange->getFormItem())
1336 return DND_ACTION_NONE;
1338 else if (pData->ISA(FmFilterItems))
1340 pForm = PTR_CAST(FmFormItem,pData->GetParent());
1341 if (pForm != m_aControlExchange->getFormItem())
1342 return DND_ACTION_NONE;
1344 else
1345 return DND_ACTION_NONE;
1347 return rEvt.mnAction;
1350 namespace
1352 FmFilterItems* getTargetItems(SvTreeListEntry* _pTarget)
1354 FmFilterData* pData = static_cast<FmFilterData*>(_pTarget->GetUserData());
1355 FmFilterItems* pTargetItems = dynamic_cast<FmFilterItems*>(pData);
1356 if (!pTargetItems)
1357 pTargetItems = dynamic_cast<FmFilterItems*>(pData->GetParent());
1358 return pTargetItems;
1362 sal_Int8 FmFilterNavigator::ExecuteDrop( const ExecuteDropEvent& rEvt )
1364 // you can't scroll after dropping...
1365 if (m_aDropActionTimer.IsActive())
1366 m_aDropActionTimer.Stop();
1368 if (!m_aControlExchange.isDragSource())
1369 return DND_ACTION_NONE;
1371 Point aDropPos = rEvt.maPosPixel;
1372 SvTreeListEntry* pDropTarget = GetEntry( aDropPos );
1373 if (!pDropTarget)
1374 return DND_ACTION_NONE;
1376 // search the container where to add the items
1377 FmFilterItems* pTargetItems = getTargetItems(pDropTarget);
1378 SelectAll(false);
1379 SvTreeListEntry* pEntry = FindEntry(pTargetItems);
1380 Select(pEntry, true);
1381 SetCurEntry(pEntry);
1383 insertFilterItem(m_aControlExchange->getDraggedEntries(),pTargetItems,DND_ACTION_COPY == rEvt.mnAction);
1385 return DND_ACTION_COPY;
1389 void FmFilterNavigator::InitEntry(SvTreeListEntry* pEntry,
1390 const OUString& rStr,
1391 const Image& rImg1,
1392 const Image& rImg2,
1393 SvLBoxButtonKind eButtonKind)
1395 SvTreeListBox::InitEntry( pEntry, rStr, rImg1, rImg2, eButtonKind );
1396 SvLBoxString* pString = NULL;
1398 if (static_cast<FmFilterData*>(pEntry->GetUserData())->ISA(FmFilterItem))
1399 pString = new FmFilterString(pEntry, 0, rStr, static_cast<FmFilterItem*>(pEntry->GetUserData())->GetFieldName());
1400 else if (static_cast<FmFilterData*>(pEntry->GetUserData())->ISA(FmFilterItems))
1401 pString = new FmFilterItemsString(pEntry, 0, rStr );
1403 if (pString)
1404 pEntry->ReplaceItem( pString, 1 );
1408 bool FmFilterNavigator::Select( SvTreeListEntry* pEntry, bool bSelect )
1410 if (bSelect == IsSelected(pEntry)) // das passiert manchmal, ich glaube, die Basisklasse geht zu sehr auf Nummer sicher ;)
1411 return true;
1413 if (SvTreeListBox::Select(pEntry, bSelect))
1415 if (bSelect)
1417 FmFormItem* pFormItem = NULL;
1418 if (static_cast<FmFilterData*>(pEntry->GetUserData())->ISA(FmFilterItem))
1419 pFormItem = static_cast<FmFormItem*>(static_cast<FmFilterItem*>(pEntry->GetUserData())->GetParent()->GetParent());
1420 else if (static_cast<FmFilterData*>(pEntry->GetUserData())->ISA(FmFilterItems))
1421 pFormItem = static_cast<FmFormItem*>(static_cast<FmFilterItem*>(pEntry->GetUserData())->GetParent()->GetParent());
1422 else if (static_cast<FmFilterData*>(pEntry->GetUserData())->ISA(FmFormItem))
1423 pFormItem = static_cast<FmFormItem*>(pEntry->GetUserData());
1425 if (pFormItem)
1427 // will the controller be exchanged?
1428 if (static_cast<FmFilterData*>(pEntry->GetUserData())->ISA(FmFilterItem))
1429 m_pModel->SetCurrentItems(static_cast<FmFilterItems*>(static_cast<FmFilterItem*>(pEntry->GetUserData())->GetParent()));
1430 else if (static_cast<FmFilterData*>(pEntry->GetUserData())->ISA(FmFilterItems))
1431 m_pModel->SetCurrentItems(static_cast<FmFilterItems*>(pEntry->GetUserData()));
1432 else if (static_cast<FmFilterData*>(pEntry->GetUserData())->ISA(FmFormItem))
1433 m_pModel->SetCurrentController(static_cast<FmFormItem*>(pEntry->GetUserData())->GetController());
1436 return true;
1438 else
1439 return false;
1442 void FmFilterNavigator::Notify( SfxBroadcaster& /*rBC*/, const SfxHint& rHint )
1444 if (const FmFilterInsertedHint* pInsertHint = dynamic_cast<const FmFilterInsertedHint*>(&rHint))
1446 Insert(pInsertHint->GetData(), pInsertHint->GetPos());
1448 else if( dynamic_cast<const FilterClearingHint*>(&rHint) )
1450 SvTreeListBox::Clear();
1452 else if (const FmFilterRemovedHint* pRemoveHint = dynamic_cast<const FmFilterRemovedHint*>(&rHint))
1454 Remove(pRemoveHint->GetData());
1456 else if (const FmFilterTextChangedHint *pChangeHint = dynamic_cast<const FmFilterTextChangedHint*>(&rHint))
1458 SvTreeListEntry* pEntry = FindEntry(pChangeHint->GetData());
1459 if (pEntry)
1460 SetEntryText( pEntry, pChangeHint->GetData()->GetText());
1462 else if( dynamic_cast<const FmFilterCurrentChangedHint*>(&rHint) )
1464 // invalidate the entries
1465 for (SvTreeListEntry* pEntry = First(); pEntry != NULL;
1466 pEntry = Next(pEntry))
1467 GetModel()->InvalidateEntry( pEntry );
1471 SvTreeListEntry* FmFilterNavigator::FindEntry(const FmFilterData* pItem) const
1473 SvTreeListEntry* pEntry = NULL;
1474 if (pItem)
1476 for (pEntry = First(); pEntry != NULL; pEntry = Next( pEntry ))
1478 FmFilterData* pEntryItem = static_cast<FmFilterData*>(pEntry->GetUserData());
1479 if (pEntryItem == pItem)
1480 break;
1483 return pEntry;
1487 void FmFilterNavigator::Insert(FmFilterData* pItem, sal_uLong nPos)
1489 const FmParentData* pParent = pItem->GetParent() ? pItem->GetParent() : GetFilterModel();
1491 // insert the item
1492 SvTreeListEntry* pParentEntry = FindEntry( pParent );
1493 InsertEntry( pItem->GetText(), pItem->GetImage(), pItem->GetImage(), pParentEntry, false, nPos, pItem );
1494 if ( pParentEntry )
1495 Expand( pParentEntry );
1499 void FmFilterNavigator::Remove(FmFilterData* pItem)
1501 // der Entry zu den Daten
1502 SvTreeListEntry* pEntry = FindEntry(pItem);
1504 if (pEntry == m_pEditingCurrently)
1505 // cancel editing
1506 EndEditing(true);
1508 if (pEntry)
1509 GetModel()->Remove( pEntry );
1512 FmFormItem* FmFilterNavigator::getSelectedFilterItems(::std::vector<FmFilterItem*>& _rItemList)
1514 // be sure that the data is only used within only one form!
1515 FmFormItem* pFirstItem = NULL;
1517 bool bHandled = true;
1518 bool bFoundSomething = false;
1519 for (SvTreeListEntry* pEntry = FirstSelected();
1520 bHandled && pEntry != NULL;
1521 pEntry = NextSelected(pEntry))
1523 FmFilterItem* pFilter = PTR_CAST(FmFilterItem, static_cast<FmFilterData*>(pEntry->GetUserData()));
1524 if (pFilter)
1526 FmFormItem* pForm = PTR_CAST(FmFormItem,pFilter->GetParent()->GetParent());
1527 if (!pForm)
1528 bHandled = false;
1529 else if (!pFirstItem)
1530 pFirstItem = pForm;
1531 else if (pFirstItem != pForm)
1532 bHandled = false;
1534 if (bHandled)
1536 _rItemList.push_back(pFilter);
1537 bFoundSomething = true;
1541 if ( !bHandled || !bFoundSomething )
1542 pFirstItem = NULL;
1543 return pFirstItem;
1546 void FmFilterNavigator::insertFilterItem(const ::std::vector<FmFilterItem*>& _rFilterList,FmFilterItems* _pTargetItems,bool _bCopy)
1548 ::std::vector<FmFilterItem*>::const_iterator aEnd = _rFilterList.end();
1549 for ( ::std::vector< FmFilterItem* >::const_iterator i = _rFilterList.begin();
1550 i != aEnd;
1554 FmFilterItem* pLookupItem( *i );
1555 if ( pLookupItem->GetParent() == _pTargetItems )
1556 continue;
1558 FmFilterItem* pFilterItem = _pTargetItems->Find( pLookupItem->GetComponentIndex() );
1559 OUString aText = pLookupItem->GetText();
1560 if ( !pFilterItem )
1562 pFilterItem = new FmFilterItem( _pTargetItems, pLookupItem->GetFieldName(), aText, pLookupItem->GetComponentIndex() );
1563 m_pModel->Append( _pTargetItems, pFilterItem );
1566 if ( !_bCopy )
1567 m_pModel->Remove( pLookupItem );
1569 // now set the text for the new dragged item
1570 m_pModel->SetTextForItem( pFilterItem, aText );
1573 m_pModel->EnsureEmptyFilterRows( *_pTargetItems->GetParent() );
1577 void FmFilterNavigator::StartDrag( sal_Int8 /*_nAction*/, const Point& /*_rPosPixel*/ )
1579 EndSelection();
1581 // be sure that the data is only used within a only one form!
1582 m_aControlExchange.prepareDrag();
1584 ::std::vector<FmFilterItem*> aItemList;
1585 if ( FmFormItem* pFirstItem = getSelectedFilterItems(aItemList) )
1587 m_aControlExchange->setDraggedEntries(aItemList);
1588 m_aControlExchange->setFormItem(pFirstItem);
1589 m_aControlExchange.startDrag( DND_ACTION_COPYMOVE );
1594 void FmFilterNavigator::Command( const CommandEvent& rEvt )
1596 bool bHandled = false;
1597 switch (rEvt.GetCommand())
1599 case CommandEventId::ContextMenu:
1601 // die Stelle, an der geklickt wurde
1602 Point aWhere;
1603 SvTreeListEntry* pClicked = NULL;
1604 if (rEvt.IsMouseEvent())
1606 aWhere = rEvt.GetMousePosPixel();
1607 pClicked = GetEntry(aWhere);
1608 if (pClicked == NULL)
1609 break;
1611 if (!IsSelected(pClicked))
1613 SelectAll(false);
1614 Select(pClicked, true);
1615 SetCurEntry(pClicked);
1618 else
1620 pClicked = GetCurEntry();
1621 if (!pClicked)
1622 break;
1623 aWhere = GetEntryPosition( pClicked );
1626 ::std::vector<FmFilterData*> aSelectList;
1627 for (SvTreeListEntry* pEntry = FirstSelected();
1628 pEntry != NULL;
1629 pEntry = NextSelected(pEntry))
1631 // don't delete forms
1632 FmFormItem* pForm = PTR_CAST(FmFormItem, static_cast<FmFilterData*>(pEntry->GetUserData()));
1633 if (!pForm)
1634 aSelectList.push_back(static_cast<FmFilterData*>(pEntry->GetUserData()));
1636 if (aSelectList.size() == 1)
1638 // don't delete the only empty row of a form
1639 FmFilterItems* pFilterItems = PTR_CAST(FmFilterItems, aSelectList[0]);
1640 if (pFilterItems && pFilterItems->GetChildren().empty()
1641 && pFilterItems->GetParent()->GetChildren().size() == 1)
1642 aSelectList.clear();
1645 PopupMenu aContextMenu(SVX_RES(RID_FM_FILTER_MENU));
1647 // every condition could be deleted except the first one if its the only one
1648 aContextMenu.EnableItem( SID_FM_DELETE, !aSelectList.empty() );
1651 bool bEdit = PTR_CAST(FmFilterItem, static_cast<FmFilterData*>(pClicked->GetUserData())) != NULL &&
1652 IsSelected(pClicked) && GetSelectionCount() == 1;
1654 aContextMenu.EnableItem( SID_FM_FILTER_EDIT,
1655 bEdit );
1656 aContextMenu.EnableItem( SID_FM_FILTER_IS_NULL,
1657 bEdit );
1658 aContextMenu.EnableItem( SID_FM_FILTER_IS_NOT_NULL,
1659 bEdit );
1661 aContextMenu.RemoveDisabledEntries(true, true);
1662 sal_uInt16 nSlotId = aContextMenu.Execute( this, aWhere );
1663 switch( nSlotId )
1665 case SID_FM_FILTER_EDIT:
1667 EditEntry( pClicked );
1668 } break;
1669 case SID_FM_FILTER_IS_NULL:
1670 case SID_FM_FILTER_IS_NOT_NULL:
1672 OUString aErrorMsg;
1673 OUString aText;
1674 if (nSlotId == SID_FM_FILTER_IS_NULL)
1675 aText = "IS NULL";
1676 else
1677 aText = "IS NOT NULL";
1679 m_pModel->ValidateText(static_cast<FmFilterItem*>(pClicked->GetUserData()),
1680 aText, aErrorMsg);
1681 m_pModel->SetTextForItem(static_cast<FmFilterItem*>(pClicked->GetUserData()), aText);
1682 } break;
1683 case SID_FM_DELETE:
1685 DeleteSelection();
1686 } break;
1688 bHandled = true;
1690 break;
1691 default: break;
1694 if (!bHandled)
1695 SvTreeListBox::Command( rEvt );
1698 SvTreeListEntry* FmFilterNavigator::getNextEntry(SvTreeListEntry* _pStartWith)
1700 SvTreeListEntry* pEntry = _pStartWith ? _pStartWith : LastSelected();
1701 pEntry = Next(pEntry);
1702 // we need the next filter entry
1703 while( pEntry && GetChildCount( pEntry ) == 0 && pEntry != Last() )
1704 pEntry = Next(pEntry);
1705 return pEntry;
1708 SvTreeListEntry* FmFilterNavigator::getPrevEntry(SvTreeListEntry* _pStartWith)
1710 SvTreeListEntry* pEntry = _pStartWith ? _pStartWith : FirstSelected();
1711 pEntry = Prev(pEntry);
1712 // check if the previous entry is a filter, if so get the next prev
1713 if ( pEntry && GetChildCount( pEntry ) != 0 )
1715 pEntry = Prev(pEntry);
1716 // if the entry is still no leaf return
1717 if ( pEntry && GetChildCount( pEntry ) != 0 )
1718 pEntry = NULL;
1720 return pEntry;
1723 void FmFilterNavigator::KeyInput(const KeyEvent& rKEvt)
1725 const vcl::KeyCode& rKeyCode = rKEvt.GetKeyCode();
1727 switch ( rKeyCode.GetCode() )
1729 case KEY_UP:
1730 case KEY_DOWN:
1732 if ( !rKeyCode.IsMod1() || !rKeyCode.IsMod2() || rKeyCode.IsShift() )
1733 break;
1735 ::std::vector<FmFilterItem*> aItemList;
1736 if ( !getSelectedFilterItems( aItemList ) )
1737 break;
1739 ::std::mem_fun1_t<SvTreeListEntry*,FmFilterNavigator,SvTreeListEntry*> getter = ::std::mem_fun(&FmFilterNavigator::getNextEntry);
1740 if ( rKeyCode.GetCode() == KEY_UP )
1741 getter = ::std::mem_fun(&FmFilterNavigator::getPrevEntry);
1743 SvTreeListEntry* pTarget = getter( this, NULL );
1744 if ( !pTarget )
1745 break;
1747 FmFilterItems* pTargetItems = getTargetItems( pTarget );
1748 if ( !pTargetItems )
1749 break;
1751 ::std::vector<FmFilterItem*>::const_iterator aEnd = aItemList.end();
1752 bool bNextTargetItem = true;
1753 while ( bNextTargetItem )
1755 ::std::vector<FmFilterItem*>::const_iterator i = aItemList.begin();
1756 for (; i != aEnd; ++i)
1758 if ( (*i)->GetParent() == pTargetItems )
1760 pTarget = getter(this,pTarget);
1761 if ( !pTarget )
1762 return;
1763 pTargetItems = getTargetItems( pTarget );
1764 break;
1766 else
1768 FmFilterItem* pFilterItem = pTargetItems->Find( (*i)->GetComponentIndex() );
1769 // we found the text component so jump above
1770 if ( pFilterItem )
1772 pTarget = getter( this, pTarget );
1773 if ( !pTarget )
1774 return;
1776 pTargetItems = getTargetItems( pTarget );
1777 break;
1781 bNextTargetItem = i != aEnd && pTargetItems;
1784 if ( pTargetItems )
1786 insertFilterItem( aItemList, pTargetItems );
1787 return;
1790 break;
1792 case KEY_DELETE:
1794 if ( rKeyCode.GetModifier() )
1795 break;
1797 if ( !IsSelected( First() ) || GetEntryCount() > 1 )
1798 DeleteSelection();
1799 return;
1803 SvTreeListBox::KeyInput(rKEvt);
1807 void FmFilterNavigator::DeleteSelection()
1809 // to avoid the deletion of an entry twice (e.g. deletion of a parent and afterward
1810 // the deletion of its child, i have to shrink the selecton list
1811 ::std::vector<SvTreeListEntry*> aEntryList;
1812 for (SvTreeListEntry* pEntry = FirstSelected();
1813 pEntry != NULL;
1814 pEntry = NextSelected(pEntry))
1816 FmFilterItem* pFilterItem = PTR_CAST(FmFilterItem, static_cast<FmFilterData*>(pEntry->GetUserData()));
1817 if (pFilterItem && IsSelected(GetParent(pEntry)))
1818 continue;
1820 FmFormItem* pForm = PTR_CAST(FmFormItem, static_cast<FmFilterData*>(pEntry->GetUserData()));
1821 if (!pForm)
1822 aEntryList.push_back(pEntry);
1825 // Remove the selection
1826 SelectAll(false);
1828 for (::std::vector<SvTreeListEntry*>::reverse_iterator i = aEntryList.rbegin();
1829 // link problems with operator ==
1830 i.base() != aEntryList.rend().base(); ++i)
1832 m_pModel->Remove(static_cast<FmFilterData*>((*i)->GetUserData()));
1836 FmFilterNavigatorWin::FmFilterNavigatorWin( SfxBindings* _pBindings, SfxChildWindow* _pMgr,
1837 vcl::Window* _pParent )
1838 :SfxDockingWindow( _pBindings, _pMgr, _pParent, WinBits(WB_STDMODELESS|WB_SIZEABLE|WB_ROLLABLE|WB_3DLOOK|WB_DOCKABLE) )
1839 ,SfxControllerItem( SID_FM_FILTER_NAVIGATOR_CONTROL, *_pBindings )
1841 SetHelpId( HID_FILTER_NAVIGATOR_WIN );
1843 m_pNavigator = VclPtr<FmFilterNavigator>::Create( this );
1844 m_pNavigator->Show();
1845 SetText( SVX_RES(RID_STR_FILTER_NAVIGATOR) );
1846 SfxDockingWindow::SetFloatingSize( Size(200,200) );
1850 FmFilterNavigatorWin::~FmFilterNavigatorWin()
1852 disposeOnce();
1855 void FmFilterNavigatorWin::dispose()
1857 m_pNavigator.disposeAndClear();
1858 ::SfxControllerItem::dispose();
1859 SfxDockingWindow::dispose();
1863 void FmFilterNavigatorWin::UpdateContent(FmFormShell* pFormShell)
1865 if (!m_pNavigator)
1866 return;
1868 if (!pFormShell)
1869 m_pNavigator->UpdateContent( NULL, NULL );
1870 else
1872 Reference< XFormController > xController(pFormShell->GetImpl()->getActiveInternalController());
1873 Reference< XIndexAccess > xContainer;
1874 if (xController.is())
1876 Reference< XChild > xChild(xController, UNO_QUERY);
1877 for (Reference< XInterface > xParent(xChild->getParent());
1878 xParent.is();
1879 xParent = xChild.is() ? xChild->getParent() : Reference< XInterface > ())
1881 xContainer = Reference< XIndexAccess > (xParent, UNO_QUERY);
1882 xChild = Reference< XChild > (xParent, UNO_QUERY);
1885 m_pNavigator->UpdateContent(xContainer, xController);
1890 void FmFilterNavigatorWin::StateChanged( sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem* pState )
1892 if( !pState || SID_FM_FILTER_NAVIGATOR_CONTROL != nSID )
1893 return;
1895 if( eState >= SfxItemState::DEFAULT )
1897 FmFormShell* pShell = PTR_CAST( FmFormShell, static_cast<const SfxObjectItem*>(pState)->GetShell() );
1898 UpdateContent( pShell );
1900 else
1901 UpdateContent( NULL );
1905 bool FmFilterNavigatorWin::Close()
1907 if ( m_pNavigator && m_pNavigator->IsEditingActive() )
1908 m_pNavigator->EndEditing();
1910 if ( m_pNavigator && m_pNavigator->IsEditingActive() )
1911 // the EndEditing was vetoed (perhaps of an syntax error or such)
1912 return false;
1914 UpdateContent( NULL );
1915 return SfxDockingWindow::Close();
1919 void FmFilterNavigatorWin::FillInfo( SfxChildWinInfo& rInfo ) const
1921 SfxDockingWindow::FillInfo( rInfo );
1922 rInfo.bVisible = false;
1926 Size FmFilterNavigatorWin::CalcDockingSize( SfxChildAlignment eAlign )
1928 if ( ( eAlign == SfxChildAlignment::TOP ) || ( eAlign == SfxChildAlignment::BOTTOM ) )
1929 return Size();
1931 return SfxDockingWindow::CalcDockingSize( eAlign );
1935 SfxChildAlignment FmFilterNavigatorWin::CheckAlignment( SfxChildAlignment eActAlign, SfxChildAlignment eAlign )
1937 switch (eAlign)
1939 case SfxChildAlignment::LEFT:
1940 case SfxChildAlignment::RIGHT:
1941 case SfxChildAlignment::NOALIGNMENT:
1942 return eAlign;
1943 default:
1944 break;
1947 return eActAlign;
1951 void FmFilterNavigatorWin::Resize()
1953 SfxDockingWindow::Resize();
1955 Size aLogOutputSize = PixelToLogic( GetOutputSizePixel(), MAP_APPFONT );
1956 Size aLogExplSize = aLogOutputSize;
1957 aLogExplSize.Width() -= 6;
1958 aLogExplSize.Height() -= 6;
1960 Point aExplPos = LogicToPixel( Point(3,3), MAP_APPFONT );
1961 Size aExplSize = LogicToPixel( aLogExplSize, MAP_APPFONT );
1963 m_pNavigator->SetPosSizePixel( aExplPos, aExplSize );
1966 void FmFilterNavigatorWin::GetFocus()
1968 // oj #97405#
1969 if ( m_pNavigator )
1970 m_pNavigator->GrabFocus();
1973 SFX_IMPL_DOCKINGWINDOW( FmFilterNavigatorWinMgr, SID_FM_FILTER_NAVIGATOR )
1976 FmFilterNavigatorWinMgr::FmFilterNavigatorWinMgr( vcl::Window *_pParent, sal_uInt16 _nId,
1977 SfxBindings *_pBindings, SfxChildWinInfo* _pInfo )
1978 :SfxChildWindow( _pParent, _nId )
1980 pWindow = VclPtr<FmFilterNavigatorWin>::Create( _pBindings, this, _pParent );
1981 eChildAlignment = SfxChildAlignment::NOALIGNMENT;
1982 static_cast<SfxDockingWindow*>(pWindow.get())->Initialize( _pInfo );
1989 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */