Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / extensions / source / propctrlr / browserlistbox.cxx
blob269b14d033dc89374d47b5d9ff1391f6e8807699
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 "browserlistbox.hxx"
21 #include "propresid.hrc"
22 #include "proplinelistener.hxx"
23 #include "propcontrolobserver.hxx"
24 #include "linedescriptor.hxx"
25 #include "inspectorhelpwindow.hxx"
27 #include <com/sun/star/lang/DisposedException.hpp>
28 #include <com/sun/star/lang/XComponent.hpp>
29 #include <com/sun/star/inspection/PropertyControlType.hpp>
30 #include <tools/debug.hxx>
31 #include <tools/diagnose_ex.h>
32 #include <comphelper/asyncnotification.hxx>
33 #include <cppuhelper/implbase.hxx>
34 #include <vcl/svapp.hxx>
35 #include <osl/mutex.hxx>
38 namespace pcr
42 #define FRAME_OFFSET 4
43 // TODO: find out what this is really for ... and check if it does make sense in the new
44 // browser environment
45 #define LAYOUT_HELP_WINDOW_DISTANCE_APPFONT 3
47 using ::com::sun::star::uno::Any;
48 using ::com::sun::star::uno::Exception;
49 using ::com::sun::star::inspection::XPropertyControlContext;
50 using ::com::sun::star::uno::Reference;
51 using ::com::sun::star::inspection::XPropertyControl;
52 using ::com::sun::star::uno::RuntimeException;
53 using ::com::sun::star::lang::DisposedException;
54 using ::com::sun::star::lang::XComponent;
55 using ::com::sun::star::uno::UNO_QUERY;
57 namespace PropertyControlType = ::com::sun::star::inspection::PropertyControlType;
59 enum ControlEventType
61 FOCUS_GAINED,
62 VALUE_CHANGED,
63 ACTIVATE_NEXT
66 struct ControlEvent : public ::comphelper::AnyEvent
68 Reference< XPropertyControl > xControl;
69 ControlEventType eType;
71 ControlEvent( const Reference< XPropertyControl >& _rxControl, ControlEventType _eType )
72 :xControl( _rxControl )
73 ,eType( _eType )
78 class SharedNotifier
80 private:
81 static ::osl::Mutex& getMutex();
82 static ::rtl::Reference< ::comphelper::AsyncEventNotifier > s_pNotifier;
84 public:
85 SharedNotifier(const SharedNotifier&) = delete;
86 SharedNotifier& operator=(const SharedNotifier&) = delete;
87 static const ::rtl::Reference< ::comphelper::AsyncEventNotifier >&
88 getNotifier();
92 ::rtl::Reference< ::comphelper::AsyncEventNotifier > SharedNotifier::s_pNotifier;
95 ::osl::Mutex& SharedNotifier::getMutex()
97 static ::osl::Mutex s_aMutex;
98 return s_aMutex;
102 const ::rtl::Reference< ::comphelper::AsyncEventNotifier >& SharedNotifier::getNotifier()
104 ::osl::MutexGuard aGuard( getMutex() );
105 if ( !s_pNotifier.is() )
107 s_pNotifier.set(
108 new ::comphelper::AsyncEventNotifier("browserlistbox"));
109 s_pNotifier->launch();
110 //TODO: a protocol is missing how to join with the launched
111 // thread before exit(3), to ensure the thread is no longer
112 // relying on any infrastructure while that infrastructure is
113 // being shut down in atexit handlers
115 return s_pNotifier;
119 /** implementation for of <type scope="css::inspection">XPropertyControlContext</type>
120 which forwards all events to a non-UNO version of this interface
122 typedef ::cppu::WeakImplHelper< XPropertyControlContext > PropertyControlContext_Impl_Base;
123 class PropertyControlContext_Impl :public PropertyControlContext_Impl_Base
124 ,public ::comphelper::IEventProcessor
126 public:
127 enum NotificationMode
129 eSynchronously,
130 eAsynchronously
133 private:
134 VclPtr<OBrowserListBox> m_pContext;
135 NotificationMode m_eMode;
137 public:
138 /** creates an instance
139 @param _rContextImpl
140 the instance to delegate events to
142 explicit PropertyControlContext_Impl( OBrowserListBox& _rContextImpl );
144 /** disposes the context.
146 When you call this method, all subsequent callbacks to the
147 <type scope="css::inspection">XPropertyControlContext</type> methods
148 will throw a <type scope="css::lang">DisposedException</type>.
150 void SAL_CALL dispose();
152 /** sets the notification mode, so that notifications received from the controls are
153 forwarded to our OBrowserListBox either synchronously or asynchronously
154 @param _eMode
155 the new notification mode
157 void setNotificationMode( NotificationMode _eMode );
159 virtual void SAL_CALL acquire() throw() override;
160 virtual void SAL_CALL release() throw() override;
162 protected:
163 virtual ~PropertyControlContext_Impl() override;
165 // XPropertyControlObserver
166 virtual void SAL_CALL focusGained( const Reference< XPropertyControl >& Control ) override;
167 virtual void SAL_CALL valueChanged( const Reference< XPropertyControl >& Control ) override;
168 // XPropertyControlContext
169 virtual void SAL_CALL activateNextControl( const Reference< XPropertyControl >& CurrentControl ) override;
171 // IEventProcessor
172 virtual void processEvent( const ::comphelper::AnyEvent& _rEvent ) override;
174 private:
175 /** processes the given event, i.e. notifies it to our OBrowserListBox
176 @param _rEvent
177 the event no notify
178 @precond
179 our mutex (well, the SolarMutex) is locked
181 void impl_processEvent_throw( const ::comphelper::AnyEvent& _rEvent );
183 /** checks whether the instance is already disposed
185 bool impl_isDisposed_nothrow() const { return m_pContext.get() == nullptr; }
187 /** notifies the given event originating from the given control
188 @throws DisposedException
189 @param _rxControl
190 @param _eType
192 void impl_notify_throw( const Reference< XPropertyControl >& _rxControl, ControlEventType _eType );
196 PropertyControlContext_Impl::PropertyControlContext_Impl( OBrowserListBox& _rContextImpl )
197 :m_pContext( &_rContextImpl )
198 ,m_eMode( eAsynchronously )
203 PropertyControlContext_Impl::~PropertyControlContext_Impl()
205 if ( !impl_isDisposed_nothrow() )
206 dispose();
210 void SAL_CALL PropertyControlContext_Impl::dispose()
212 SolarMutexGuard aGuard;
213 if ( impl_isDisposed_nothrow() )
214 return;
216 SharedNotifier::getNotifier()->removeEventsForProcessor( this );
217 m_pContext = nullptr;
221 void PropertyControlContext_Impl::setNotificationMode( NotificationMode _eMode )
223 SolarMutexGuard aGuard;
224 m_eMode = _eMode;
228 void PropertyControlContext_Impl::impl_notify_throw( const Reference< XPropertyControl >& _rxControl, ControlEventType _eType )
230 ::comphelper::AnyEventRef pEvent;
233 SolarMutexGuard aGuard;
234 if ( impl_isDisposed_nothrow() )
235 throw DisposedException( OUString(), *this );
236 pEvent = new ControlEvent( _rxControl, _eType );
238 if ( m_eMode == eSynchronously )
240 impl_processEvent_throw( *pEvent );
241 return;
245 SharedNotifier::getNotifier()->addEvent( pEvent, this );
249 void SAL_CALL PropertyControlContext_Impl::focusGained( const Reference< XPropertyControl >& Control )
251 impl_notify_throw( Control, FOCUS_GAINED );
255 void SAL_CALL PropertyControlContext_Impl::valueChanged( const Reference< XPropertyControl >& Control )
257 impl_notify_throw( Control, VALUE_CHANGED );
261 void SAL_CALL PropertyControlContext_Impl::activateNextControl( const Reference< XPropertyControl >& CurrentControl )
263 impl_notify_throw( CurrentControl, ACTIVATE_NEXT );
267 void SAL_CALL PropertyControlContext_Impl::acquire() throw()
269 PropertyControlContext_Impl_Base::acquire();
273 void SAL_CALL PropertyControlContext_Impl::release() throw()
275 PropertyControlContext_Impl_Base::release();
279 void PropertyControlContext_Impl::processEvent( const ::comphelper::AnyEvent& _rEvent )
281 SolarMutexGuard aGuard;
282 if ( impl_isDisposed_nothrow() )
283 return;
287 impl_processEvent_throw( _rEvent );
289 catch( const Exception& )
291 // can't handle otherwise, since our caller (the notification thread) does not allow
292 // for exceptions (it could itself abort only)
293 DBG_UNHANDLED_EXCEPTION();
298 void PropertyControlContext_Impl::impl_processEvent_throw( const ::comphelper::AnyEvent& _rEvent )
300 const ControlEvent& rControlEvent = static_cast< const ControlEvent& >( _rEvent );
301 switch ( rControlEvent.eType )
303 case FOCUS_GAINED:
304 m_pContext->focusGained( rControlEvent.xControl );
305 break;
306 case VALUE_CHANGED:
307 m_pContext->valueChanged( rControlEvent.xControl );
308 break;
309 case ACTIVATE_NEXT:
310 m_pContext->activateNextControl( rControlEvent.xControl );
311 break;
315 OBrowserListBox::OBrowserListBox( vcl::Window* pParent)
316 :Control(pParent, WB_DIALOGCONTROL | WB_CLIPCHILDREN)
317 ,m_aLinesPlayground(VclPtr<vcl::Window>::Create(this,WB_DIALOGCONTROL | WB_CLIPCHILDREN))
318 ,m_aVScroll(VclPtr<ScrollBar>::Create(this,WB_VSCROLL|WB_REPEAT|WB_DRAG))
319 ,m_pHelpWindow( VclPtr<InspectorHelpWindow>::Create( this ) )
320 ,m_pLineListener(nullptr)
321 ,m_pControlObserver( nullptr )
322 ,m_nYOffset(0)
323 ,m_nCurrentPreferredHelpHeight(0)
324 ,m_nTheNameSize(0)
325 ,m_bIsActive(false)
326 ,m_bUpdate(true)
327 ,m_pControlContextImpl( new PropertyControlContext_Impl( *this ) )
329 ScopedVclPtrInstance<ListBox> aListBox(this, WB_DROPDOWN);
330 ScopedVclPtrInstance<Edit> aEditBox(this);
331 m_nRowHeight = std::max(aListBox->get_preferred_size().Height(),
332 aEditBox->get_preferred_size().Height());
333 m_nRowHeight += 2;
334 SetBackground( pParent->GetBackground() );
335 m_aLinesPlayground->SetBackground( GetBackground() );
337 m_aLinesPlayground->SetPosPixel(Point(0,0));
338 m_aLinesPlayground->SetPaintTransparent(true);
339 m_aLinesPlayground->Show();
340 m_aVScroll->Hide();
341 m_aVScroll->SetScrollHdl(LINK(this, OBrowserListBox, ScrollHdl));
344 OBrowserListBox::~OBrowserListBox()
346 disposeOnce();
349 void OBrowserListBox::dispose()
351 OSL_ENSURE( !IsModified(), "OBrowserListBox::~OBrowserListBox: still modified - should have been committed before!" );
352 // doing the commit here, while we, as well as our owner, as well as some other components,
353 // are already "half dead" (means within their dtor) is potentially dangerous.
354 // By definition, CommitModified has to be called (if necessary) before destruction
356 m_pControlContextImpl->dispose();
357 m_pControlContextImpl.clear();
359 Hide();
360 Clear();
361 m_aLinesPlayground.disposeAndClear();
362 m_aVScroll.disposeAndClear();
363 m_pHelpWindow.disposeAndClear();
364 Control::dispose();
368 bool OBrowserListBox::IsModified( ) const
370 bool bModified = false;
372 if ( m_bIsActive && m_xActiveControl.is() )
373 bModified = m_xActiveControl->isModified();
375 return bModified;
379 void OBrowserListBox::CommitModified( )
381 if ( IsModified() && m_xActiveControl.is() )
383 // for the time of this commit, notify all events synchronously
384 // #i63814#
385 m_pControlContextImpl->setNotificationMode( PropertyControlContext_Impl::eSynchronously );
388 m_xActiveControl->notifyModifiedValue();
390 catch( const Exception& )
392 DBG_UNHANDLED_EXCEPTION();
394 m_pControlContextImpl->setNotificationMode( PropertyControlContext_Impl::eAsynchronously );
399 void OBrowserListBox::ActivateListBox(bool _bActive)
401 m_bIsActive = _bActive;
402 if (m_bIsActive)
404 // TODO: what's the sense of this?
405 m_aVScroll->SetThumbPos(100);
406 MoveThumbTo(0);
407 Resize();
412 long OBrowserListBox::impl_getPrefererredHelpHeight()
414 return HasHelpSection() ? m_pHelpWindow->GetOptimalHeightPixel() : 0;
418 void OBrowserListBox::Resize()
420 tools::Rectangle aPlayground( Point( 0, 0 ), GetOutputSizePixel() );
421 Size aHelpWindowDistance( LogicToPixel( Size( 0, LAYOUT_HELP_WINDOW_DISTANCE_APPFONT ), MapUnit::MapAppFont ) );
423 long nHelpWindowHeight = m_nCurrentPreferredHelpHeight = impl_getPrefererredHelpHeight();
424 bool bPositionHelpWindow = ( nHelpWindowHeight != 0 );
426 tools::Rectangle aLinesArea( aPlayground );
427 if ( bPositionHelpWindow )
429 aLinesArea.Bottom() -= nHelpWindowHeight;
430 aLinesArea.Bottom() -= aHelpWindowDistance.Height();
432 m_aLinesPlayground->SetPosSizePixel( aLinesArea.TopLeft(), aLinesArea.GetSize() );
434 UpdateVScroll();
436 bool bNeedScrollbar = m_aLines.size() > (sal_uInt32)CalcVisibleLines();
437 if ( !bNeedScrollbar )
439 if ( m_aVScroll->IsVisible() )
440 m_aVScroll->Hide();
441 // scroll to top
442 m_nYOffset = 0;
443 m_aVScroll->SetThumbPos( 0 );
445 else
447 Size aVScrollSize( m_aVScroll->GetSizePixel() );
449 // adjust the playground's width
450 aLinesArea.Right() -= aVScrollSize.Width();
451 m_aLinesPlayground->SetPosSizePixel( aLinesArea.TopLeft(), aLinesArea.GetSize() );
453 // position the scrollbar
454 aVScrollSize.Height() = aLinesArea.GetHeight();
455 Point aVScrollPos( aLinesArea.GetWidth(), 0 );
456 m_aVScroll->SetPosSizePixel( aVScrollPos, aVScrollSize );
459 for ( ListBoxLines::size_type i = 0; i < m_aLines.size(); ++i )
460 m_aOutOfDateLines.insert( i );
462 // repaint
463 EnablePaint(false);
464 UpdatePlayGround();
465 EnablePaint(true);
467 // show the scrollbar
468 if ( bNeedScrollbar )
469 m_aVScroll->Show();
471 // position the help window
472 if ( bPositionHelpWindow )
474 tools::Rectangle aHelpArea( aPlayground );
475 aHelpArea.Top() = aLinesArea.Bottom() + aHelpWindowDistance.Height();
476 m_pHelpWindow->SetPosSizePixel( aHelpArea.TopLeft(), aHelpArea.GetSize() );
481 void OBrowserListBox::SetListener( IPropertyLineListener* _pListener )
483 m_pLineListener = _pListener;
487 void OBrowserListBox::SetObserver( IPropertyControlObserver* _pObserver )
489 m_pControlObserver = _pObserver;
493 void OBrowserListBox::EnableHelpSection( bool _bEnable )
495 m_pHelpWindow->Show( _bEnable );
496 Resize();
500 bool OBrowserListBox::HasHelpSection() const
502 return m_pHelpWindow->IsVisible();
506 void OBrowserListBox::SetHelpText( const OUString& _rHelpText )
508 OSL_ENSURE( HasHelpSection(), "OBrowserListBox::SetHelpText: help section not visible!" );
509 m_pHelpWindow->SetText( _rHelpText );
510 if ( m_nCurrentPreferredHelpHeight != impl_getPrefererredHelpHeight() )
511 Resize();
515 void OBrowserListBox::SetHelpLineLimites( sal_Int32 _nMinLines, sal_Int32 _nMaxLines )
517 m_pHelpWindow->SetLimits( _nMinLines, _nMaxLines );
521 sal_uInt16 OBrowserListBox::CalcVisibleLines()
523 Size aSize(m_aLinesPlayground->GetOutputSizePixel());
524 sal_uInt16 nResult = 0;
525 if (0 != m_nRowHeight)
526 nResult = (sal_uInt16) aSize.Height()/m_nRowHeight;
528 return nResult;
532 void OBrowserListBox::UpdateVScroll()
534 sal_uInt16 nLines = CalcVisibleLines();
535 m_aVScroll->SetPageSize(nLines-1);
536 m_aVScroll->SetVisibleSize(nLines-1);
538 size_t nCount = m_aLines.size();
539 if (nCount>0)
541 m_aVScroll->SetRange(Range(0,nCount-1));
542 m_nYOffset = -m_aVScroll->GetThumbPos()*m_nRowHeight;
544 else
546 m_aVScroll->SetRange(Range(0,0));
547 m_nYOffset = 0;
552 void OBrowserListBox::PositionLine( ListBoxLines::size_type _nIndex )
554 Size aSize(m_aLinesPlayground->GetOutputSizePixel());
555 Point aPos(0, m_nYOffset);
557 aSize.Height() = m_nRowHeight;
559 aPos.Y() += _nIndex * m_nRowHeight;
561 if ( _nIndex < m_aLines.size() )
563 BrowserLinePointer pLine = m_aLines[ _nIndex ].pLine;
565 pLine->SetPosSizePixel( aPos, aSize );
566 pLine->SetTitleWidth( m_nTheNameSize + 2 * FRAME_OFFSET );
568 // show the line if necessary
569 if ( !pLine->IsVisible() )
570 pLine->Show();
575 void OBrowserListBox::UpdatePosNSize()
577 for ( auto const & aLoop: m_aOutOfDateLines )
579 DBG_ASSERT( aLoop < m_aLines.size(), "OBrowserListBox::UpdatePosNSize: invalid line index!" );
580 if ( aLoop < m_aLines.size() )
581 PositionLine( aLoop );
583 m_aOutOfDateLines.clear();
587 void OBrowserListBox::UpdatePlayGround()
589 sal_Int32 nThumbPos = m_aVScroll->GetThumbPos();
590 sal_Int32 nLines = CalcVisibleLines();
592 ListBoxLines::size_type nEnd = nThumbPos + nLines;
593 if (nEnd >= m_aLines.size())
594 nEnd = m_aLines.size()-1;
596 if ( !m_aLines.empty() )
598 for ( ListBoxLines::size_type i = nThumbPos; i <= nEnd; ++i )
599 m_aOutOfDateLines.insert( i );
600 UpdatePosNSize();
605 void OBrowserListBox::DisableUpdate()
607 m_bUpdate = false;
611 void OBrowserListBox::EnableUpdate()
613 m_bUpdate = true;
614 Resize();
618 void OBrowserListBox::SetPropertyValue(const OUString& _rEntryName, const Any& _rValue, bool _bUnknownValue )
620 ListBoxLines::iterator line = m_aLines.begin();
621 for ( ; line != m_aLines.end() && ( line->aName != _rEntryName ); ++line )
624 if ( line != m_aLines.end() )
626 if ( _bUnknownValue )
628 Reference< XPropertyControl > xControl( line->pLine->getControl() );
629 OSL_ENSURE( xControl.is(), "OBrowserListBox::SetPropertyValue: illegal control!" );
630 if ( xControl.is() )
631 xControl->setValue( Any() );
633 else
634 impl_setControlAsPropertyValue( *line, _rValue );
639 sal_uInt16 OBrowserListBox::GetPropertyPos( const OUString& _rEntryName ) const
641 sal_uInt16 nRet = EDITOR_LIST_ENTRY_NOTFOUND;
642 for ( ListBoxLines::const_iterator linePos = m_aLines.begin();
643 linePos != m_aLines.end();
644 ++linePos
647 if ( linePos->aName == _rEntryName )
649 nRet = (sal_uInt16)( linePos - m_aLines.begin() );
650 break;
654 return nRet;
658 bool OBrowserListBox::impl_getBrowserLineForName( const OUString& _rEntryName, BrowserLinePointer& _out_rpLine ) const
660 ListBoxLines::const_iterator line = m_aLines.begin();
661 for ( ; line != m_aLines.end() && ( line->aName != _rEntryName ); ++line )
664 if ( line != m_aLines.end() )
665 _out_rpLine = line->pLine;
666 else
667 _out_rpLine.reset();
668 return ( nullptr != _out_rpLine.get() );
672 void OBrowserListBox::EnablePropertyControls( const OUString& _rEntryName, sal_Int16 _nControls, bool _bEnable )
674 BrowserLinePointer pLine;
675 if ( impl_getBrowserLineForName( _rEntryName, pLine ) )
676 pLine->EnablePropertyControls( _nControls, _bEnable );
680 void OBrowserListBox::EnablePropertyLine( const OUString& _rEntryName, bool _bEnable )
682 BrowserLinePointer pLine;
683 if ( impl_getBrowserLineForName( _rEntryName, pLine ) )
684 pLine->EnablePropertyLine( _bEnable );
688 Reference< XPropertyControl > OBrowserListBox::GetPropertyControl( const OUString& _rEntryName )
690 BrowserLinePointer pLine;
691 if ( impl_getBrowserLineForName( _rEntryName, pLine ) )
692 return pLine->getControl();
693 return nullptr;
697 void OBrowserListBox::InsertEntry(const OLineDescriptor& _rPropertyData, sal_uInt16 _nPos)
699 // create a new line
700 BrowserLinePointer pBrowserLine( new OBrowserLine( _rPropertyData.sName, m_aLinesPlayground.get() ) );
702 // check that the name is unique
703 ListBoxLines::iterator it = m_aLines.begin();
704 for ( ; it != m_aLines.end() && ( it->aName != _rPropertyData.sName ); ++it )
706 OSL_ENSURE( it == m_aLines.end(), "OBrowserListBox::InsertEntry: already have another line for this name!" );
708 ListBoxLine aNewLine( _rPropertyData.sName, pBrowserLine, _rPropertyData.xPropertyHandler );
709 ListBoxLines::size_type nInsertPos = _nPos;
710 if ( _nPos >= m_aLines.size() )
712 nInsertPos = m_aLines.size();
713 m_aLines.push_back( aNewLine );
715 else
716 m_aLines.insert( m_aLines.begin() + _nPos, aNewLine );
718 pBrowserLine->SetTitleWidth(m_nTheNameSize);
719 if (m_bUpdate)
721 UpdateVScroll();
722 Invalidate();
725 // initialize the entry
726 ChangeEntry(_rPropertyData, nInsertPos);
728 // update the positions of possibly affected lines
729 ListBoxLines::size_type nUpdatePos = nInsertPos;
730 while ( nUpdatePos < m_aLines.size() )
731 m_aOutOfDateLines.insert( nUpdatePos++ );
732 UpdatePosNSize( );
736 sal_Int32 OBrowserListBox::GetMinimumWidth()
738 return m_nTheNameSize + 2 * FRAME_OFFSET + (m_nRowHeight - 4) * 8;
742 sal_Int32 OBrowserListBox::GetMinimumHeight()
744 // assume that we want to display 5 rows, at least
745 sal_Int32 nMinHeight = m_nRowHeight * 5;
747 if ( HasHelpSection() )
749 Size aHelpWindowDistance( LogicToPixel( Size( 0, LAYOUT_HELP_WINDOW_DISTANCE_APPFONT ), MapUnit::MapAppFont ) );
750 nMinHeight += aHelpWindowDistance.Height();
752 nMinHeight += m_pHelpWindow->GetMinimalHeightPixel();
755 return nMinHeight;
759 void OBrowserListBox::ShowEntry(sal_uInt16 _nPos)
761 if ( _nPos < m_aLines.size() )
763 sal_Int32 nThumbPos = m_aVScroll->GetThumbPos();
765 if (_nPos < nThumbPos)
766 MoveThumbTo(_nPos);
767 else
769 sal_Int32 nLines = CalcVisibleLines();
770 if (_nPos >= nThumbPos + nLines)
771 MoveThumbTo(_nPos - nLines + 1);
778 void OBrowserListBox::MoveThumbTo(sal_Int32 _nNewThumbPos)
780 // disable painting to prevent flicker
781 m_aLinesPlayground->EnablePaint(false);
783 sal_Int32 nDelta = _nNewThumbPos - m_aVScroll->GetThumbPos();
784 // adjust the scrollbar
785 m_aVScroll->SetThumbPos(_nNewThumbPos);
786 sal_Int32 nThumbPos = _nNewThumbPos;
788 m_nYOffset = -m_aVScroll->GetThumbPos() * m_nRowHeight;
790 sal_Int32 nLines = CalcVisibleLines();
791 ListBoxLines::size_type nEnd = nThumbPos + nLines;
793 m_aLinesPlayground->Scroll(0, -nDelta * m_nRowHeight, ScrollFlags::Children);
795 if (1 == nDelta)
797 // TODO: what's the sense of this two PositionLines? Why not just one call?
798 PositionLine(nEnd-1);
799 PositionLine(nEnd);
801 else if (-1 == nDelta)
803 PositionLine(nThumbPos);
805 else if (0 != nDelta)
807 UpdatePlayGround();
810 m_aLinesPlayground->EnablePaint(true);
811 m_aLinesPlayground->Invalidate(InvalidateFlags::Children);
815 IMPL_LINK(OBrowserListBox, ScrollHdl, ScrollBar*, _pScrollBar, void )
817 DBG_ASSERT(_pScrollBar == m_aVScroll.get(), "OBrowserListBox::ScrollHdl: where does this come from?");
818 (void)_pScrollBar;
820 // disable painting to prevent flicker
821 m_aLinesPlayground->EnablePaint(false);
823 sal_Int32 nThumbPos = m_aVScroll->GetThumbPos();
825 sal_Int32 nDelta = m_aVScroll->GetDelta();
826 m_nYOffset = -nThumbPos * m_nRowHeight;
828 ListBoxLines::size_type nEnd = nThumbPos + CalcVisibleLines();
830 m_aLinesPlayground->Scroll(0, -nDelta * m_nRowHeight, ScrollFlags::Children);
832 if (1 == nDelta)
834 PositionLine(nEnd-1);
835 PositionLine(nEnd);
837 else if (nDelta==-1)
839 PositionLine(nThumbPos);
841 else if (nDelta!=0 || m_aVScroll->GetType() == ScrollType::DontKnow)
843 UpdatePlayGround();
846 m_aLinesPlayground->EnablePaint(true);
850 void OBrowserListBox::buttonClicked( OBrowserLine* _pLine, bool _bPrimary )
852 DBG_ASSERT( _pLine, "OBrowserListBox::buttonClicked: invalid browser line!" );
853 if ( _pLine && m_pLineListener )
855 m_pLineListener->Clicked( _pLine->GetEntryName(), _bPrimary );
860 void OBrowserListBox::impl_setControlAsPropertyValue( const ListBoxLine& _rLine, const Any& _rPropertyValue )
862 Reference< XPropertyControl > xControl( _rLine.pLine->getControl() );
865 if ( _rPropertyValue.getValueType().equals( _rLine.pLine->getControl()->getValueType() ) )
867 xControl->setValue( _rPropertyValue );
869 else
871 #ifdef DBG_UTIL
872 if ( !_rLine.xHandler.is() )
874 OString sMessage( "OBrowserListBox::impl_setControlAsPropertyValue: no handler -> no conversion (property: '" );
875 OUString sPropertyName( _rLine.pLine->GetEntryName() );
876 sMessage += OString( sPropertyName.getStr(), sPropertyName.getLength(), RTL_TEXTENCODING_ASCII_US );
877 sMessage += OString( "')!" );
878 OSL_FAIL( sMessage.getStr() );
880 #endif
881 if ( _rLine.xHandler.is() )
883 Any aControlValue = _rLine.xHandler->convertToControlValue(
884 _rLine.pLine->GetEntryName(), _rPropertyValue, xControl->getValueType() );
885 xControl->setValue( aControlValue );
889 catch( const Exception& )
891 DBG_UNHANDLED_EXCEPTION();
896 Any OBrowserListBox::impl_getControlAsPropertyValue( const ListBoxLine& _rLine )
898 Reference< XPropertyControl > xControl( _rLine.pLine->getControl() );
899 Any aPropertyValue;
902 #ifdef DBG_UTIL
903 if ( !_rLine.xHandler.is() )
905 OString sMessage( "OBrowserListBox::impl_getControlAsPropertyValue: no handler -> no conversion (property: '" );
906 OUString sPropertyName( _rLine.pLine->GetEntryName() );
907 sMessage += OString( sPropertyName.getStr(), sPropertyName.getLength(), RTL_TEXTENCODING_ASCII_US );
908 sMessage += OString( "')!" );
909 OSL_FAIL( sMessage.getStr() );
911 #endif
912 if ( _rLine.xHandler.is() )
913 aPropertyValue = _rLine.xHandler->convertToPropertyValue( _rLine.pLine->GetEntryName(), xControl->getValue() );
914 else
915 aPropertyValue = xControl->getValue();
917 catch( const Exception& )
919 DBG_UNHANDLED_EXCEPTION();
921 return aPropertyValue;
925 sal_uInt16 OBrowserListBox::impl_getControlPos( const Reference< XPropertyControl >& _rxControl ) const
927 for ( ListBoxLines::const_iterator search = m_aLines.begin(); search != m_aLines.end(); ++search )
928 if ( search->pLine->getControl().get() == _rxControl.get() )
929 return sal_uInt16( search - m_aLines.begin() );
931 OSL_FAIL( "OBrowserListBox::impl_getControlPos: invalid control - not part of any of our lines!" );
932 return (sal_uInt16)-1;
936 void SAL_CALL OBrowserListBox::focusGained( const Reference< XPropertyControl >& _rxControl )
938 DBG_TESTSOLARMUTEX();
940 DBG_ASSERT( _rxControl.is(), "OBrowserListBox::focusGained: invalid event source!" );
941 if ( !_rxControl.is() )
942 return;
944 if ( m_pControlObserver )
945 m_pControlObserver->focusGained( _rxControl );
947 m_xActiveControl = _rxControl;
948 ShowEntry( impl_getControlPos( m_xActiveControl ) );
952 void SAL_CALL OBrowserListBox::valueChanged( const Reference< XPropertyControl >& _rxControl )
954 DBG_TESTSOLARMUTEX();
956 DBG_ASSERT( _rxControl.is(), "OBrowserListBox::valueChanged: invalid event source!" );
957 if ( !_rxControl.is() )
958 return;
960 if ( m_pControlObserver )
961 m_pControlObserver->valueChanged( _rxControl );
963 if ( m_pLineListener )
965 const ListBoxLine& rLine = m_aLines[ impl_getControlPos( _rxControl ) ];
966 m_pLineListener->Commit(
967 rLine.pLine->GetEntryName(),
968 impl_getControlAsPropertyValue( rLine )
974 void SAL_CALL OBrowserListBox::activateNextControl( const Reference< XPropertyControl >& _rxCurrentControl )
976 DBG_TESTSOLARMUTEX();
978 sal_uInt16 nLine = impl_getControlPos( _rxCurrentControl );
980 // cycle forwards, 'til we've the next control which can grab the focus
981 ++nLine;
982 while ( static_cast< size_t >( nLine ) < m_aLines.size() )
984 if ( m_aLines[nLine].pLine->GrabFocus() )
985 break;
986 ++nLine;
989 // wrap around?
990 if ( ( static_cast< size_t >( nLine ) >= m_aLines.size() ) && ( m_aLines.size() > 0 ) )
991 m_aLines[0].pLine->GrabFocus();
995 namespace
998 void lcl_implDisposeControl_nothrow( const Reference< XPropertyControl >& _rxControl )
1000 if ( !_rxControl.is() )
1001 return;
1004 _rxControl->setControlContext( nullptr );
1005 Reference< XComponent > xControlComponent( _rxControl, UNO_QUERY );
1006 if ( xControlComponent.is() )
1007 xControlComponent->dispose();
1009 catch( const Exception& )
1011 DBG_UNHANDLED_EXCEPTION();
1017 void OBrowserListBox::Clear()
1019 for ( ListBoxLines::iterator loop = m_aLines.begin(); loop != m_aLines.end(); ++loop )
1021 // hide the line
1022 loop->pLine->Hide();
1023 // reset the listener
1024 lcl_implDisposeControl_nothrow( loop->pLine->getControl() );
1027 clearContainer( m_aLines );
1031 bool OBrowserListBox::RemoveEntry( const OUString& _rName )
1033 ListBoxLines::size_type nPos = 0;
1034 ListBoxLines::iterator it = m_aLines.begin();
1035 for ( ; it != m_aLines.end() && ( it->aName != _rName ); ++it, ++nPos )
1038 if ( it == m_aLines.end() )
1039 return false;
1041 m_aLines.erase( it );
1042 m_aOutOfDateLines.erase( m_aLines.size() );
1044 // update the positions of possibly affected lines
1045 while ( nPos < m_aLines.size() )
1046 m_aOutOfDateLines.insert( nPos++ );
1047 UpdatePosNSize( );
1049 return true;
1053 void OBrowserListBox::ChangeEntry( const OLineDescriptor& _rPropertyData, ListBoxLines::size_type nPos )
1055 OSL_PRECOND( _rPropertyData.Control.is(), "OBrowserListBox::ChangeEntry: invalid control!" );
1056 if ( !_rPropertyData.Control.is() )
1057 return;
1059 if ( nPos == EDITOR_LIST_REPLACE_EXISTING )
1060 nPos = GetPropertyPos( _rPropertyData.sName );
1062 if ( nPos < m_aLines.size() )
1064 vcl::Window* pRefWindow = nullptr;
1065 if ( nPos > 0 )
1066 pRefWindow = m_aLines[nPos-1].pLine->GetRefWindow();
1068 // the current line and control
1069 ListBoxLine& rLine = m_aLines[nPos];
1071 // the old control and some data about it
1072 Reference< XPropertyControl > xControl = rLine.pLine->getControl();
1073 vcl::Window* pControlWindow = rLine.pLine->getControlWindow();
1074 Point aControlPos;
1075 if ( pControlWindow )
1076 aControlPos = pControlWindow->GetPosPixel();
1078 // clean up the old control
1079 lcl_implDisposeControl_nothrow( xControl );
1081 // set the new control at the line
1082 rLine.pLine->setControl( _rPropertyData.Control );
1083 xControl = rLine.pLine->getControl();
1085 if ( xControl.is() )
1086 xControl->setControlContext( m_pControlContextImpl.get() );
1088 // the initial property value
1089 if ( _rPropertyData.bUnknownValue )
1090 xControl->setValue( Any() );
1091 else
1092 impl_setControlAsPropertyValue( rLine, _rPropertyData.aValue );
1094 rLine.pLine->SetTitle(_rPropertyData.DisplayName);
1095 rLine.xHandler = _rPropertyData.xPropertyHandler;
1097 sal_uInt16 nTextWidth = (sal_uInt16)m_aLinesPlayground->GetTextWidth(_rPropertyData.DisplayName);
1098 if (m_nTheNameSize< nTextWidth)
1099 m_nTheNameSize = nTextWidth;
1101 if ( _rPropertyData.HasPrimaryButton )
1103 if ( !_rPropertyData.PrimaryButtonImageURL.isEmpty() )
1104 rLine.pLine->ShowBrowseButton( _rPropertyData.PrimaryButtonImageURL, true );
1105 else if ( _rPropertyData.PrimaryButtonImage.is() )
1106 rLine.pLine->ShowBrowseButton( Image( _rPropertyData.PrimaryButtonImage ), true );
1107 else
1108 rLine.pLine->ShowBrowseButton( true );
1110 if ( _rPropertyData.HasSecondaryButton )
1112 if ( !_rPropertyData.SecondaryButtonImageURL.isEmpty() )
1113 rLine.pLine->ShowBrowseButton( _rPropertyData.SecondaryButtonImageURL, false );
1114 else if ( _rPropertyData.SecondaryButtonImage.is() )
1115 rLine.pLine->ShowBrowseButton( Image( _rPropertyData.SecondaryButtonImage ), false );
1116 else
1117 rLine.pLine->ShowBrowseButton( false );
1119 else
1120 rLine.pLine->HideBrowseButton( false );
1122 rLine.pLine->SetClickListener( this );
1124 else
1126 rLine.pLine->HideBrowseButton( true );
1127 rLine.pLine->HideBrowseButton( false );
1130 DBG_ASSERT( ( _rPropertyData.IndentLevel == 0 ) || ( _rPropertyData.IndentLevel == 1 ),
1131 "OBrowserListBox::ChangeEntry: unsupported indent level!" );
1132 rLine.pLine->IndentTitle( _rPropertyData.IndentLevel > 0 );
1134 if ( nPos > 0 )
1135 rLine.pLine->SetTabOrder( pRefWindow, ZOrderFlags::Behind );
1136 else
1137 rLine.pLine->SetTabOrder( pRefWindow, ZOrderFlags::First );
1139 m_aOutOfDateLines.insert( nPos );
1140 rLine.pLine->SetComponentHelpIds(
1141 HelpIdUrl::getHelpId( _rPropertyData.HelpURL )
1144 if ( _rPropertyData.bReadOnly )
1146 rLine.pLine->SetReadOnly( true );
1148 // user controls (i.e. the ones not provided by the usual
1149 // XPropertyControlFactory) have no chance to know that they should be read-only,
1150 // since XPropertyHandler::describePropertyLine does not transport this
1151 // information.
1152 // So, we manually switch this to read-only.
1153 if ( xControl.is() && ( xControl->getControlType() == PropertyControlType::Unknown ) )
1155 vcl::Window *pWindow = rLine.pLine->getControlWindow();
1156 Edit* pControlWindowAsEdit = dynamic_cast<Edit*>(pWindow);
1157 if (pControlWindowAsEdit)
1158 pControlWindowAsEdit->SetReadOnly();
1159 else
1160 pWindow->Enable(false);
1167 bool OBrowserListBox::PreNotify( NotifyEvent& _rNEvt )
1169 switch ( _rNEvt.GetType() )
1171 case MouseNotifyEvent::KEYINPUT:
1173 const KeyEvent* pKeyEvent = _rNEvt.GetKeyEvent();
1174 if ( ( pKeyEvent->GetKeyCode().GetModifier() != 0 )
1175 || ( ( pKeyEvent->GetKeyCode().GetCode() != KEY_PAGEUP )
1176 && ( pKeyEvent->GetKeyCode().GetCode() != KEY_PAGEDOWN )
1179 break;
1181 long nScrollOffset = 0;
1182 if ( m_aVScroll->IsVisible() )
1184 if ( pKeyEvent->GetKeyCode().GetCode() == KEY_PAGEUP )
1185 nScrollOffset = -m_aVScroll->GetPageSize();
1186 else if ( pKeyEvent->GetKeyCode().GetCode() == KEY_PAGEDOWN )
1187 nScrollOffset = m_aVScroll->GetPageSize();
1190 if ( nScrollOffset )
1192 long nNewThumbPos = m_aVScroll->GetThumbPos() + nScrollOffset;
1193 nNewThumbPos = std::max( nNewThumbPos, m_aVScroll->GetRangeMin() );
1194 nNewThumbPos = std::min( nNewThumbPos, m_aVScroll->GetRangeMax() );
1195 m_aVScroll->DoScroll( nNewThumbPos );
1196 nNewThumbPos = m_aVScroll->GetThumbPos();
1198 sal_uInt16 nFocusControlPos = 0;
1199 sal_uInt16 nActiveControlPos = impl_getControlPos( m_xActiveControl );
1200 if ( nActiveControlPos < nNewThumbPos )
1201 nFocusControlPos = (sal_uInt16)nNewThumbPos;
1202 else if ( nActiveControlPos >= nNewThumbPos + CalcVisibleLines() )
1203 nFocusControlPos = (sal_uInt16)nNewThumbPos + CalcVisibleLines() - 1;
1204 if ( nFocusControlPos )
1206 if ( nFocusControlPos < m_aLines.size() )
1208 m_aLines[ nFocusControlPos ].pLine->GrabFocus();
1210 else
1211 OSL_FAIL( "OBrowserListBox::PreNotify: internal error, invalid focus control position!" );
1215 return true;
1216 // handled this. In particular, we also consume PageUp/Down events if we do not use them for scrolling,
1217 // otherwise they would be used to scroll the document view, which does not sound like it is desired by
1218 // the user.
1220 default:
1221 break;
1223 return Control::PreNotify( _rNEvt );
1226 bool OBrowserListBox::EventNotify( NotifyEvent& _rNEvt )
1228 if ( _rNEvt.GetType() == MouseNotifyEvent::COMMAND)
1230 const CommandEvent* pCommand = _rNEvt.GetCommandEvent();
1231 if ( ( CommandEventId::Wheel == pCommand->GetCommand() )
1232 || ( CommandEventId::StartAutoScroll == pCommand->GetCommand() )
1233 || ( CommandEventId::AutoScroll == pCommand->GetCommand() )
1236 // interested in scroll events if we have a scrollbar
1237 if ( m_aVScroll->IsVisible() )
1239 HandleScrollCommand( *pCommand, nullptr, m_aVScroll.get() );
1243 return Control::EventNotify(_rNEvt);
1247 } // namespace pcr
1250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */