bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / propctrlr / browserlistbox.cxx
blobf5cfc3bc0a188d9523f3defc5ea1b7f6fa19fac7
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 <boost/noncopyable.hpp>
28 #include <com/sun/star/lang/DisposedException.hpp>
29 #include <com/sun/star/lang/XComponent.hpp>
30 #include <com/sun/star/inspection/PropertyControlType.hpp>
31 #include <tools/debug.hxx>
32 #include <tools/diagnose_ex.h>
33 #include <comphelper/asyncnotification.hxx>
34 #include <cppuhelper/implbase1.hxx>
35 #include <vcl/svapp.hxx>
36 #include <osl/mutex.hxx>
39 namespace pcr
43 #define FRAME_OFFSET 4
44 // TODO: find out what this is really for ... and check if it does make sense in the new
45 // browser environment
46 #define LAYOUT_HELP_WINDOW_DISTANCE_APPFONT 3
48 using ::com::sun::star::uno::Any;
49 using ::com::sun::star::uno::Exception;
50 using ::com::sun::star::inspection::XPropertyControlContext;
51 using ::com::sun::star::uno::Reference;
52 using ::com::sun::star::inspection::XPropertyControl;
53 using ::com::sun::star::uno::RuntimeException;
54 using ::com::sun::star::lang::DisposedException;
55 using ::com::sun::star::lang::XComponent;
56 using ::com::sun::star::uno::UNO_QUERY;
57 using ::com::sun::star::graphic::XGraphic;
59 namespace PropertyControlType = ::com::sun::star::inspection::PropertyControlType;
61 enum ControlEventType
63 FOCUS_GAINED,
64 VALUE_CHANGED,
65 ACTIVATE_NEXT
68 struct ControlEvent : public ::comphelper::AnyEvent
70 Reference< XPropertyControl > xControl;
71 ControlEventType eType;
73 ControlEvent( const Reference< XPropertyControl >& _rxControl, ControlEventType _eType )
74 :xControl( _rxControl )
75 ,eType( _eType )
80 class SharedNotifier: private boost::noncopyable
82 private:
83 static ::osl::Mutex& getMutex();
84 static ::rtl::Reference< ::comphelper::AsyncEventNotifier > s_pNotifier;
86 public:
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="com::sun::star::inspection">XPropertyControlContext</type>
120 which forwards all events to a non-UNO version of this interface
122 typedef ::cppu::WeakImplHelper1< 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 IControlContext* m_pContext;
135 NotificationMode m_eMode;
137 public:
138 /** creates an instance
139 @param _rContextImpl
140 the instance to delegate events to
142 PropertyControlContext_Impl( IControlContext& _rContextImpl );
144 /** disposes the context.
146 When you call this method, all subsequent callbacks to the
147 <type scope="com::sun::star::inspection">XPropertyControlContext</type> methods
148 will throw a <type scope="com::sun::star::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 IControlContext either synchronously or asynchronously
154 @param _eMode
155 the new notification mode
157 void setNotificationMode( NotificationMode _eMode );
159 virtual void SAL_CALL acquire() throw() SAL_OVERRIDE;
160 virtual void SAL_CALL release() throw() SAL_OVERRIDE;
162 protected:
163 virtual ~PropertyControlContext_Impl();
165 // XPropertyControlObserver
166 virtual void SAL_CALL focusGained( const Reference< XPropertyControl >& Control ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
167 virtual void SAL_CALL valueChanged( const Reference< XPropertyControl >& Control ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
168 // XPropertyControlContext
169 virtual void SAL_CALL activateNextControl( const Reference< XPropertyControl >& CurrentControl ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
171 // IEventProcessor
172 virtual void processEvent( const ::comphelper::AnyEvent& _rEvent ) SAL_OVERRIDE;
174 private:
175 /** processes the given event, i.e. notifies it to our IControlContext
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 we're alive
185 @throws DisposedException
186 if the instance is already disposed
188 void impl_checkAlive_throw() const;
190 /** checks whether the instance is already disposed
192 bool impl_isDisposed_nothrow() const { return m_pContext == NULL; }
194 /** notifies the given event originating from the given control
195 @throws DisposedException
196 @param _rxControl
197 @param _eType
199 void impl_notify_throw( const Reference< XPropertyControl >& _rxControl, ControlEventType _eType );
203 PropertyControlContext_Impl::PropertyControlContext_Impl( IControlContext& _rContextImpl )
204 :m_pContext( &_rContextImpl )
205 ,m_eMode( eAsynchronously )
210 PropertyControlContext_Impl::~PropertyControlContext_Impl()
212 if ( !impl_isDisposed_nothrow() )
213 dispose();
217 void PropertyControlContext_Impl::impl_checkAlive_throw() const
219 if ( impl_isDisposed_nothrow() )
220 throw DisposedException( OUString(), *const_cast< PropertyControlContext_Impl* >( this ) );
224 void SAL_CALL PropertyControlContext_Impl::dispose()
226 SolarMutexGuard aGuard;
227 if ( impl_isDisposed_nothrow() )
228 return;
230 SharedNotifier::getNotifier()->removeEventsForProcessor( this );
231 m_pContext = NULL;
235 void PropertyControlContext_Impl::setNotificationMode( NotificationMode _eMode )
237 SolarMutexGuard aGuard;
238 m_eMode = _eMode;
242 void PropertyControlContext_Impl::impl_notify_throw( const Reference< XPropertyControl >& _rxControl, ControlEventType _eType )
244 ::comphelper::AnyEventRef pEvent;
247 SolarMutexGuard aGuard;
248 impl_checkAlive_throw();
249 pEvent = new ControlEvent( _rxControl, _eType );
251 if ( m_eMode == eSynchronously )
253 impl_processEvent_throw( *pEvent );
254 return;
258 SharedNotifier::getNotifier()->addEvent( pEvent, this );
262 void SAL_CALL PropertyControlContext_Impl::focusGained( const Reference< XPropertyControl >& Control ) throw (RuntimeException, std::exception)
264 OSL_TRACE( "PropertyControlContext_Impl: FOCUS_GAINED" );
265 impl_notify_throw( Control, FOCUS_GAINED );
269 void SAL_CALL PropertyControlContext_Impl::valueChanged( const Reference< XPropertyControl >& Control ) throw (RuntimeException, std::exception)
271 OSL_TRACE( "PropertyControlContext_Impl: VALUE_CHANGED" );
272 impl_notify_throw( Control, VALUE_CHANGED );
276 void SAL_CALL PropertyControlContext_Impl::activateNextControl( const Reference< XPropertyControl >& CurrentControl ) throw (RuntimeException, std::exception)
278 OSL_TRACE( "PropertyControlContext_Impl: ACTIVATE_NEXT" );
279 impl_notify_throw( CurrentControl, ACTIVATE_NEXT );
283 void SAL_CALL PropertyControlContext_Impl::acquire() throw()
285 PropertyControlContext_Impl_Base::acquire();
289 void SAL_CALL PropertyControlContext_Impl::release() throw()
291 PropertyControlContext_Impl_Base::release();
295 void PropertyControlContext_Impl::processEvent( const ::comphelper::AnyEvent& _rEvent )
297 SolarMutexGuard aGuard;
298 if ( impl_isDisposed_nothrow() )
299 return;
303 impl_processEvent_throw( _rEvent );
305 catch( const Exception& )
307 // can't handle otherwise, since our caller (the notification thread) does not allow
308 // for exceptions (it could itself abort only)
309 DBG_UNHANDLED_EXCEPTION();
314 void PropertyControlContext_Impl::impl_processEvent_throw( const ::comphelper::AnyEvent& _rEvent )
316 const ControlEvent& rControlEvent = static_cast< const ControlEvent& >( _rEvent );
317 switch ( rControlEvent.eType )
319 case FOCUS_GAINED:
320 OSL_TRACE( "PropertyControlContext_Impl::processEvent: FOCUS_GAINED" );
321 m_pContext->focusGained( rControlEvent.xControl );
322 break;
323 case VALUE_CHANGED:
324 OSL_TRACE( "PropertyControlContext_Impl::processEvent: VALUE_CHANGED" );
325 m_pContext->valueChanged( rControlEvent.xControl );
326 break;
327 case ACTIVATE_NEXT:
328 OSL_TRACE( "PropertyControlContext_Impl::processEvent: ACTIVATE_NEXT" );
329 m_pContext->activateNextControl( rControlEvent.xControl );
330 break;
334 OBrowserListBox::OBrowserListBox( vcl::Window* pParent, WinBits nWinStyle)
335 :Control(pParent, nWinStyle| WB_CLIPCHILDREN)
336 ,m_aLinesPlayground(VclPtr<vcl::Window>::Create(this,WB_DIALOGCONTROL | WB_CLIPCHILDREN))
337 ,m_aVScroll(VclPtr<ScrollBar>::Create(this,WB_VSCROLL|WB_REPEAT|WB_DRAG))
338 ,m_pHelpWindow( VclPtr<InspectorHelpWindow>::Create( this ) )
339 ,m_pLineListener(NULL)
340 ,m_pControlObserver( NULL )
341 ,m_nYOffset(0)
342 ,m_nCurrentPreferredHelpHeight(0)
343 ,m_nTheNameSize(0)
344 ,m_bIsActive(false)
345 ,m_bUpdate(true)
346 ,m_pControlContextImpl( new PropertyControlContext_Impl( *this ) )
348 ScopedVclPtrInstance< ListBox > aListBox(this,WB_DROPDOWN);
349 aListBox->SetPosSizePixel(Point(0,0),Size(100,100));
350 m_nRowHeight = aListBox->GetSizePixel().Height()+2;
351 SetBackground( pParent->GetBackground() );
352 m_aLinesPlayground->SetBackground( GetBackground() );
354 m_aLinesPlayground->SetPosPixel(Point(0,0));
355 m_aLinesPlayground->SetPaintTransparent(true);
356 m_aLinesPlayground->Show();
357 m_aVScroll->Hide();
358 m_aVScroll->SetScrollHdl(LINK(this, OBrowserListBox, ScrollHdl));
362 OBrowserListBox::~OBrowserListBox()
364 disposeOnce();
367 void OBrowserListBox::dispose()
369 OSL_ENSURE( !IsModified(), "OBrowserListBox::~OBrowserListBox: still modified - should have been committed before!" );
370 // doing the commit here, while we, as well as our owner, as well as some other components,
371 // are already "half dead" (means within their dtor) is potentially dangerous.
372 // By definition, CommitModified has to be called (if necessary) before destruction
374 m_pControlContextImpl->dispose();
375 m_pControlContextImpl.clear();
377 Hide();
378 Clear();
379 m_aLinesPlayground.disposeAndClear();
380 m_aVScroll.disposeAndClear();
381 m_pHelpWindow.disposeAndClear();
382 Control::dispose();
386 bool OBrowserListBox::IsModified( ) const
388 bool bModified = false;
390 if ( m_bIsActive && m_xActiveControl.is() )
391 bModified = m_xActiveControl->isModified();
393 return bModified;
397 void OBrowserListBox::CommitModified( )
399 if ( IsModified() && m_xActiveControl.is() )
401 // for the time of this commit, notify all events synchronously
402 // #i63814#
403 m_pControlContextImpl->setNotificationMode( PropertyControlContext_Impl::eSynchronously );
406 m_xActiveControl->notifyModifiedValue();
408 catch( const Exception& )
410 DBG_UNHANDLED_EXCEPTION();
412 m_pControlContextImpl->setNotificationMode( PropertyControlContext_Impl::eAsynchronously );
417 void OBrowserListBox::ActivateListBox(bool _bActive)
419 m_bIsActive = _bActive;
420 if (m_bIsActive)
422 // TODO: what's the sense of this?
423 m_aVScroll->SetThumbPos(100);
424 MoveThumbTo(0);
425 Resize();
430 long OBrowserListBox::impl_getPrefererredHelpHeight()
432 return HasHelpSection() ? m_pHelpWindow->GetOptimalHeightPixel() : 0;
436 void OBrowserListBox::Resize()
438 Rectangle aPlayground( Point( 0, 0 ), GetOutputSizePixel() );
439 Size aHelpWindowDistance( LogicToPixel( Size( 0, LAYOUT_HELP_WINDOW_DISTANCE_APPFONT ), MAP_APPFONT ) );
441 long nHelpWindowHeight = m_nCurrentPreferredHelpHeight = impl_getPrefererredHelpHeight();
442 bool bPositionHelpWindow = ( nHelpWindowHeight != 0 );
444 Rectangle aLinesArea( aPlayground );
445 if ( bPositionHelpWindow )
447 aLinesArea.Bottom() -= nHelpWindowHeight;
448 aLinesArea.Bottom() -= aHelpWindowDistance.Height();
450 m_aLinesPlayground->SetPosSizePixel( aLinesArea.TopLeft(), aLinesArea.GetSize() );
452 UpdateVScroll();
454 bool bNeedScrollbar = m_aLines.size() > (sal_uInt32)CalcVisibleLines();
455 if ( !bNeedScrollbar )
457 if ( m_aVScroll->IsVisible() )
458 m_aVScroll->Hide();
459 // scroll to top
460 m_nYOffset = 0;
461 m_aVScroll->SetThumbPos( 0 );
463 else
465 Size aVScrollSize( m_aVScroll->GetSizePixel() );
467 // adjust the playground's width
468 aLinesArea.Right() -= aVScrollSize.Width();
469 m_aLinesPlayground->SetPosSizePixel( aLinesArea.TopLeft(), aLinesArea.GetSize() );
471 // position the scrollbar
472 aVScrollSize.Height() = aLinesArea.GetHeight();
473 Point aVScrollPos( aLinesArea.GetWidth(), 0 );
474 m_aVScroll->SetPosSizePixel( aVScrollPos, aVScrollSize );
477 for ( sal_uInt16 i = 0; i < m_aLines.size(); ++i )
478 m_aOutOfDateLines.insert( i );
480 // repaint
481 EnablePaint(false);
482 UpdatePlayGround();
483 EnablePaint(true);
485 // show the scrollbar
486 if ( bNeedScrollbar )
487 m_aVScroll->Show();
489 // position the help window
490 if ( bPositionHelpWindow )
492 Rectangle aHelpArea( aPlayground );
493 aHelpArea.Top() = aLinesArea.Bottom() + aHelpWindowDistance.Height();
494 m_pHelpWindow->SetPosSizePixel( aHelpArea.TopLeft(), aHelpArea.GetSize() );
499 void OBrowserListBox::SetListener( IPropertyLineListener* _pListener )
501 m_pLineListener = _pListener;
505 void OBrowserListBox::SetObserver( IPropertyControlObserver* _pObserver )
507 m_pControlObserver = _pObserver;
511 void OBrowserListBox::EnableHelpSection( bool _bEnable )
513 m_pHelpWindow->Show( _bEnable );
514 Resize();
518 bool OBrowserListBox::HasHelpSection() const
520 return m_pHelpWindow->IsVisible();
524 void OBrowserListBox::SetHelpText( const OUString& _rHelpText )
526 OSL_ENSURE( HasHelpSection(), "OBrowserListBox::SetHelpText: help section not visible!" );
527 m_pHelpWindow->SetText( _rHelpText );
528 if ( m_nCurrentPreferredHelpHeight != impl_getPrefererredHelpHeight() )
529 Resize();
533 void OBrowserListBox::SetHelpLineLimites( sal_Int32 _nMinLines, sal_Int32 _nMaxLines )
535 m_pHelpWindow->SetLimits( _nMinLines, _nMaxLines );
539 sal_uInt16 OBrowserListBox::CalcVisibleLines()
541 Size aSize(m_aLinesPlayground->GetOutputSizePixel());
542 sal_uInt16 nResult = 0;
543 if (0 != m_nRowHeight)
544 nResult = (sal_uInt16) aSize.Height()/m_nRowHeight;
546 return nResult;
550 void OBrowserListBox::UpdateVScroll()
552 sal_uInt16 nLines = CalcVisibleLines();
553 m_aVScroll->SetPageSize(nLines-1);
554 m_aVScroll->SetVisibleSize(nLines-1);
556 size_t nCount = m_aLines.size();
557 if (nCount>0)
559 m_aVScroll->SetRange(Range(0,nCount-1));
560 m_nYOffset = -m_aVScroll->GetThumbPos()*m_nRowHeight;
562 else
564 m_aVScroll->SetRange(Range(0,0));
565 m_nYOffset = 0;
570 void OBrowserListBox::PositionLine( sal_uInt16 _nIndex )
572 Size aSize(m_aLinesPlayground->GetOutputSizePixel());
573 Point aPos(0, m_nYOffset);
575 aSize.Height() = m_nRowHeight;
577 aPos.Y() += _nIndex * m_nRowHeight;
579 if ( _nIndex < m_aLines.size() )
581 BrowserLinePointer pLine = m_aLines[ _nIndex ].pLine;
583 pLine->SetPosSizePixel( aPos, aSize );
584 pLine->SetTitleWidth( m_nTheNameSize + 2 * FRAME_OFFSET );
586 // show the line if necessary
587 if ( !pLine->IsVisible() )
588 pLine->Show();
593 void OBrowserListBox::UpdatePosNSize()
595 for ( ::std::set< sal_uInt16 >::const_iterator aLoop = m_aOutOfDateLines.begin();
596 aLoop != m_aOutOfDateLines.end();
597 ++aLoop
600 DBG_ASSERT( *aLoop < m_aLines.size(), "OBrowserListBox::UpdatePosNSize: invalid line index!" );
601 if ( *aLoop < m_aLines.size() )
602 PositionLine( *aLoop );
604 m_aOutOfDateLines.clear();
608 void OBrowserListBox::UpdatePlayGround()
610 sal_Int32 nThumbPos = m_aVScroll->GetThumbPos();
611 sal_Int32 nLines = CalcVisibleLines();
613 sal_uInt16 nEnd = (sal_uInt16)(nThumbPos + nLines);
614 if (nEnd >= m_aLines.size())
615 nEnd = (sal_uInt16)m_aLines.size()-1;
617 if ( !m_aLines.empty() )
619 for ( sal_uInt16 i = (sal_uInt16)nThumbPos; i <= nEnd; ++i )
620 m_aOutOfDateLines.insert( i );
621 UpdatePosNSize();
626 void OBrowserListBox::UpdateAll()
628 Resize();
632 void OBrowserListBox::DisableUpdate()
634 m_bUpdate = false;
638 void OBrowserListBox::EnableUpdate()
640 m_bUpdate = true;
641 UpdateAll();
645 void OBrowserListBox::SetPropertyValue(const OUString& _rEntryName, const Any& _rValue, bool _bUnknownValue )
647 ListBoxLines::iterator line = m_aLines.begin();
648 for ( ; line != m_aLines.end() && ( line->aName != _rEntryName ); ++line )
651 if ( line != m_aLines.end() )
653 if ( _bUnknownValue )
655 Reference< XPropertyControl > xControl( line->pLine->getControl() );
656 OSL_ENSURE( xControl.is(), "OBrowserListBox::SetPropertyValue: illegal control!" );
657 if ( xControl.is() )
658 xControl->setValue( Any() );
660 else
661 impl_setControlAsPropertyValue( *line, _rValue );
666 sal_uInt16 OBrowserListBox::GetPropertyPos( const OUString& _rEntryName ) const
668 sal_uInt16 nRet = EDITOR_LIST_ENTRY_NOTFOUND;
669 for ( ListBoxLines::const_iterator linePos = m_aLines.begin();
670 linePos != m_aLines.end();
671 ++linePos
674 if ( linePos->aName == _rEntryName )
676 nRet = (sal_uInt16)( linePos - m_aLines.begin() );
677 break;
681 return nRet;
685 bool OBrowserListBox::impl_getBrowserLineForName( const OUString& _rEntryName, BrowserLinePointer& _out_rpLine ) const
687 ListBoxLines::const_iterator line = m_aLines.begin();
688 for ( ; line != m_aLines.end() && ( line->aName != _rEntryName ); ++line )
691 if ( line != m_aLines.end() )
692 _out_rpLine = line->pLine;
693 else
694 _out_rpLine.reset();
695 return ( NULL != _out_rpLine.get() );
699 void OBrowserListBox::EnablePropertyControls( const OUString& _rEntryName, sal_Int16 _nControls, bool _bEnable )
701 BrowserLinePointer pLine;
702 if ( impl_getBrowserLineForName( _rEntryName, pLine ) )
703 pLine->EnablePropertyControls( _nControls, _bEnable );
707 void OBrowserListBox::EnablePropertyLine( const OUString& _rEntryName, bool _bEnable )
709 BrowserLinePointer pLine;
710 if ( impl_getBrowserLineForName( _rEntryName, pLine ) )
711 pLine->EnablePropertyLine( _bEnable );
715 Reference< XPropertyControl > OBrowserListBox::GetPropertyControl( const OUString& _rEntryName )
717 BrowserLinePointer pLine;
718 if ( impl_getBrowserLineForName( _rEntryName, pLine ) )
719 return pLine->getControl();
720 return NULL;
724 sal_uInt16 OBrowserListBox::InsertEntry(const OLineDescriptor& _rPropertyData, sal_uInt16 _nPos)
726 // create a new line
727 BrowserLinePointer pBrowserLine( new OBrowserLine( _rPropertyData.sName, m_aLinesPlayground.get() ) );
729 // check that the name is unique
730 ListBoxLines::iterator it = m_aLines.begin();
731 for ( ; it != m_aLines.end() && ( it->aName != _rPropertyData.sName ); ++it )
733 OSL_ENSURE( it == m_aLines.end(), "OBrowserListBox::InsertEntry: already have another line for this name!" );
735 ListBoxLine aNewLine( _rPropertyData.sName, pBrowserLine, _rPropertyData.xPropertyHandler );
736 sal_uInt16 nInsertPos = _nPos;
737 if ( _nPos >= m_aLines.size() )
739 nInsertPos = static_cast< sal_uInt16 >( m_aLines.size() );
740 m_aLines.push_back( aNewLine );
742 else
743 m_aLines.insert( m_aLines.begin() + _nPos, aNewLine );
745 pBrowserLine->SetTitleWidth(m_nTheNameSize);
746 if (m_bUpdate)
748 UpdateVScroll();
749 Invalidate();
752 // initialize the entry
753 ChangeEntry(_rPropertyData, nInsertPos);
755 // update the positions of possibly affected lines
756 sal_uInt16 nUpdatePos = nInsertPos;
757 while ( nUpdatePos < m_aLines.size() )
758 m_aOutOfDateLines.insert( nUpdatePos++ );
759 UpdatePosNSize( );
761 return nInsertPos;
765 sal_Int32 OBrowserListBox::GetMinimumWidth()
767 return m_nTheNameSize + 2 * FRAME_OFFSET + (m_nRowHeight - 4) * 8;
771 sal_Int32 OBrowserListBox::GetMinimumHeight()
773 // assume that we want to display 5 rows, at least
774 sal_Int32 nMinHeight = m_nRowHeight * 5;
776 if ( HasHelpSection() )
778 Size aHelpWindowDistance( LogicToPixel( Size( 0, LAYOUT_HELP_WINDOW_DISTANCE_APPFONT ), MAP_APPFONT ) );
779 nMinHeight += aHelpWindowDistance.Height();
781 nMinHeight += m_pHelpWindow->GetMinimalHeightPixel();
784 return nMinHeight;
788 void OBrowserListBox::ShowEntry(sal_uInt16 _nPos)
790 if ( _nPos < m_aLines.size() )
792 sal_Int32 nThumbPos = m_aVScroll->GetThumbPos();
794 if (_nPos < nThumbPos)
795 MoveThumbTo(_nPos);
796 else
798 sal_Int32 nLines = CalcVisibleLines();
799 if (_nPos >= nThumbPos + nLines)
800 MoveThumbTo(_nPos - nLines + 1);
807 void OBrowserListBox::MoveThumbTo(sal_Int32 _nNewThumbPos)
809 // disable painting to prevent flicker
810 m_aLinesPlayground->EnablePaint(false);
812 sal_Int32 nDelta = _nNewThumbPos - m_aVScroll->GetThumbPos();
813 // adjust the scrollbar
814 m_aVScroll->SetThumbPos(_nNewThumbPos);
815 sal_Int32 nThumbPos = _nNewThumbPos;
817 m_nYOffset = -m_aVScroll->GetThumbPos() * m_nRowHeight;
819 sal_Int32 nLines = CalcVisibleLines();
820 sal_uInt16 nEnd = (sal_uInt16)(nThumbPos + nLines);
822 m_aLinesPlayground->Scroll(0, -nDelta * m_nRowHeight, SCROLL_CHILDREN);
824 if (1 == nDelta)
826 // TODO: what's the sense of this two PositionLines? Why not just one call?
827 PositionLine(nEnd-1);
828 PositionLine(nEnd);
830 else if (-1 == nDelta)
832 PositionLine((sal_uInt16)nThumbPos);
834 else if (0 != nDelta)
836 UpdatePlayGround();
839 m_aLinesPlayground->EnablePaint(true);
840 m_aLinesPlayground->Invalidate(INVALIDATE_CHILDREN);
844 IMPL_LINK(OBrowserListBox, ScrollHdl, ScrollBar*, _pScrollBar )
846 DBG_ASSERT(_pScrollBar == m_aVScroll.get(), "OBrowserListBox::ScrollHdl: where does this come from?");
847 (void)_pScrollBar;
849 // disable painting to prevent flicker
850 m_aLinesPlayground->EnablePaint(false);
852 sal_Int32 nThumbPos = m_aVScroll->GetThumbPos();
854 sal_Int32 nDelta = m_aVScroll->GetDelta();
855 m_nYOffset = -nThumbPos * m_nRowHeight;
857 sal_uInt16 nEnd = (sal_uInt16)(nThumbPos + CalcVisibleLines());
859 m_aLinesPlayground->Scroll(0, -nDelta * m_nRowHeight, SCROLL_CHILDREN);
861 if (1 == nDelta)
863 PositionLine(nEnd-1);
864 PositionLine(nEnd);
866 else if (nDelta==-1)
868 PositionLine((sal_uInt16)nThumbPos);
870 else if (nDelta!=0 || m_aVScroll->GetType() == SCROLL_DONTKNOW)
872 UpdatePlayGround();
875 m_aLinesPlayground->EnablePaint(true);
876 return 0;
880 void OBrowserListBox::buttonClicked( OBrowserLine* _pLine, bool _bPrimary )
882 DBG_ASSERT( _pLine, "OBrowserListBox::buttonClicked: invalid browser line!" );
883 if ( _pLine && m_pLineListener )
885 m_pLineListener->Clicked( _pLine->GetEntryName(), _bPrimary );
890 void OBrowserListBox::impl_setControlAsPropertyValue( const ListBoxLine& _rLine, const Any& _rPropertyValue )
892 Reference< XPropertyControl > xControl( _rLine.pLine->getControl() );
895 if ( _rPropertyValue.getValueType().equals( _rLine.pLine->getControl()->getValueType() ) )
897 xControl->setValue( _rPropertyValue );
899 else
901 #ifdef DBG_UTIL
902 if ( !_rLine.xHandler.is() )
904 OString sMessage( "OBrowserListBox::impl_setControlAsPropertyValue: no handler -> no conversion (property: '" );
905 OUString sPropertyName( _rLine.pLine->GetEntryName() );
906 sMessage += OString( sPropertyName.getStr(), sPropertyName.getLength(), RTL_TEXTENCODING_ASCII_US );
907 sMessage += OString( "')!" );
908 OSL_FAIL( sMessage.getStr() );
910 #endif
911 if ( _rLine.xHandler.is() )
913 Any aControlValue = _rLine.xHandler->convertToControlValue(
914 _rLine.pLine->GetEntryName(), _rPropertyValue, xControl->getValueType() );
915 xControl->setValue( aControlValue );
919 catch( const Exception& )
921 DBG_UNHANDLED_EXCEPTION();
926 Any OBrowserListBox::impl_getControlAsPropertyValue( const ListBoxLine& _rLine )
928 Reference< XPropertyControl > xControl( _rLine.pLine->getControl() );
929 Any aPropertyValue;
932 #ifdef DBG_UTIL
933 if ( !_rLine.xHandler.is() )
935 OString sMessage( "OBrowserListBox::impl_getControlAsPropertyValue: no handler -> no conversion (property: '" );
936 OUString sPropertyName( _rLine.pLine->GetEntryName() );
937 sMessage += OString( sPropertyName.getStr(), sPropertyName.getLength(), RTL_TEXTENCODING_ASCII_US );
938 sMessage += OString( "')!" );
939 OSL_FAIL( sMessage.getStr() );
941 #endif
942 if ( _rLine.xHandler.is() )
943 aPropertyValue = _rLine.xHandler->convertToPropertyValue( _rLine.pLine->GetEntryName(), xControl->getValue() );
944 else
945 aPropertyValue = xControl->getValue();
947 catch( const Exception& )
949 DBG_UNHANDLED_EXCEPTION();
951 return aPropertyValue;
955 sal_uInt16 OBrowserListBox::impl_getControlPos( const Reference< XPropertyControl >& _rxControl ) const
957 for ( ListBoxLines::const_iterator search = m_aLines.begin(); search != m_aLines.end(); ++search )
958 if ( search->pLine->getControl().get() == _rxControl.get() )
959 return sal_uInt16( search - m_aLines.begin() );
961 OSL_FAIL( "OBrowserListBox::impl_getControlPos: invalid control - not part of any of our lines!" );
962 return (sal_uInt16)-1;
966 void SAL_CALL OBrowserListBox::focusGained( const Reference< XPropertyControl >& _rxControl ) throw (RuntimeException)
968 DBG_TESTSOLARMUTEX();
970 DBG_ASSERT( _rxControl.is(), "OBrowserListBox::focusGained: invalid event source!" );
971 if ( !_rxControl.is() )
972 return;
974 if ( m_pControlObserver )
975 m_pControlObserver->focusGained( _rxControl );
977 m_xActiveControl = _rxControl;
978 ShowEntry( impl_getControlPos( m_xActiveControl ) );
982 void SAL_CALL OBrowserListBox::valueChanged( const Reference< XPropertyControl >& _rxControl ) throw (RuntimeException)
984 DBG_TESTSOLARMUTEX();
986 DBG_ASSERT( _rxControl.is(), "OBrowserListBox::valueChanged: invalid event source!" );
987 if ( !_rxControl.is() )
988 return;
990 if ( m_pControlObserver )
991 m_pControlObserver->valueChanged( _rxControl );
993 if ( m_pLineListener )
995 const ListBoxLine& rLine = m_aLines[ impl_getControlPos( _rxControl ) ];
996 m_pLineListener->Commit(
997 rLine.pLine->GetEntryName(),
998 impl_getControlAsPropertyValue( rLine )
1004 void SAL_CALL OBrowserListBox::activateNextControl( const Reference< XPropertyControl >& _rxCurrentControl ) throw (RuntimeException)
1006 DBG_TESTSOLARMUTEX();
1008 sal_uInt16 nLine = impl_getControlPos( _rxCurrentControl );
1010 // cycle forwards, 'til we've the next control which can grab the focus
1011 ++nLine;
1012 while ( static_cast< size_t >( nLine ) < m_aLines.size() )
1014 if ( m_aLines[nLine].pLine->GrabFocus() )
1015 break;
1016 ++nLine;
1019 // wrap around?
1020 if ( ( static_cast< size_t >( nLine ) >= m_aLines.size() ) && ( m_aLines.size() > 0 ) )
1021 m_aLines[0].pLine->GrabFocus();
1025 namespace
1028 void lcl_implDisposeControl_nothrow( const Reference< XPropertyControl >& _rxControl )
1030 if ( !_rxControl.is() )
1031 return;
1034 _rxControl->setControlContext( NULL );
1035 Reference< XComponent > xControlComponent( _rxControl, UNO_QUERY );
1036 if ( xControlComponent.is() )
1037 xControlComponent->dispose();
1039 catch( const Exception& )
1041 DBG_UNHANDLED_EXCEPTION();
1047 void OBrowserListBox::Clear()
1049 for ( ListBoxLines::iterator loop = m_aLines.begin(); loop != m_aLines.end(); ++loop )
1051 // hide the line
1052 loop->pLine->Hide();
1053 // reset the listener
1054 lcl_implDisposeControl_nothrow( loop->pLine->getControl() );
1057 clearContainer( m_aLines );
1061 bool OBrowserListBox::RemoveEntry( const OUString& _rName )
1063 sal_uInt16 nPos = 0;
1064 ListBoxLines::iterator it = m_aLines.begin();
1065 for ( ; it != m_aLines.end() && ( it->aName != _rName ); ++it, ++nPos )
1068 if ( it == m_aLines.end() )
1069 return false;
1071 m_aLines.erase( it );
1072 m_aOutOfDateLines.erase( (sal_uInt16)m_aLines.size() );
1074 // update the positions of possibly affected lines
1075 while ( nPos < m_aLines.size() )
1076 m_aOutOfDateLines.insert( nPos++ );
1077 UpdatePosNSize( );
1079 return true;
1083 void OBrowserListBox::ChangeEntry( const OLineDescriptor& _rPropertyData, sal_uInt16 nPos )
1085 OSL_PRECOND( _rPropertyData.Control.is(), "OBrowserListBox::ChangeEntry: invalid control!" );
1086 if ( !_rPropertyData.Control.is() )
1087 return;
1089 if ( nPos == EDITOR_LIST_REPLACE_EXISTING )
1090 nPos = GetPropertyPos( _rPropertyData.sName );
1092 if ( nPos < m_aLines.size() )
1094 vcl::Window* pRefWindow = NULL;
1095 if ( nPos > 0 )
1096 pRefWindow = m_aLines[nPos-1].pLine->GetRefWindow();
1098 // the current line and control
1099 ListBoxLine& rLine = m_aLines[nPos];
1101 // the old control and some data about it
1102 Reference< XPropertyControl > xControl = rLine.pLine->getControl();
1103 vcl::Window* pControlWindow = rLine.pLine->getControlWindow();
1104 Point aControlPos;
1105 if ( pControlWindow )
1106 aControlPos = pControlWindow->GetPosPixel();
1108 // clean up the old control
1109 lcl_implDisposeControl_nothrow( xControl );
1111 // set the new control at the line
1112 rLine.pLine->setControl( _rPropertyData.Control );
1113 xControl = rLine.pLine->getControl();
1115 if ( xControl.is() )
1116 xControl->setControlContext( m_pControlContextImpl.get() );
1118 // the initial property value
1119 if ( _rPropertyData.bUnknownValue )
1120 xControl->setValue( Any() );
1121 else
1122 impl_setControlAsPropertyValue( rLine, _rPropertyData.aValue );
1124 rLine.pLine->SetTitle(_rPropertyData.DisplayName);
1125 rLine.xHandler = _rPropertyData.xPropertyHandler;
1127 sal_uInt16 nTextWidth = (sal_uInt16)m_aLinesPlayground->GetTextWidth(_rPropertyData.DisplayName);
1128 if (m_nTheNameSize< nTextWidth)
1129 m_nTheNameSize = nTextWidth;
1131 if ( _rPropertyData.HasPrimaryButton )
1133 if ( !_rPropertyData.PrimaryButtonImageURL.isEmpty() )
1134 rLine.pLine->ShowBrowseButton( _rPropertyData.PrimaryButtonImageURL, true );
1135 else if ( _rPropertyData.PrimaryButtonImage.is() )
1136 rLine.pLine->ShowBrowseButton( Image( _rPropertyData.PrimaryButtonImage ), true );
1137 else
1138 rLine.pLine->ShowBrowseButton( true );
1140 if ( _rPropertyData.HasSecondaryButton )
1142 if ( !_rPropertyData.SecondaryButtonImageURL.isEmpty() )
1143 rLine.pLine->ShowBrowseButton( _rPropertyData.SecondaryButtonImageURL, false );
1144 else if ( _rPropertyData.SecondaryButtonImage.is() )
1145 rLine.pLine->ShowBrowseButton( Image( _rPropertyData.SecondaryButtonImage ), false );
1146 else
1147 rLine.pLine->ShowBrowseButton( false );
1149 else
1150 rLine.pLine->HideBrowseButton( false );
1152 rLine.pLine->SetClickListener( this );
1154 else
1156 rLine.pLine->HideBrowseButton( true );
1157 rLine.pLine->HideBrowseButton( false );
1160 DBG_ASSERT( ( _rPropertyData.IndentLevel == 0 ) || ( _rPropertyData.IndentLevel == 1 ),
1161 "OBrowserListBox::ChangeEntry: unsupported indent level!" );
1162 rLine.pLine->IndentTitle( _rPropertyData.IndentLevel > 0 );
1164 if ( nPos > 0 )
1165 rLine.pLine->SetTabOrder( pRefWindow, WINDOW_ZORDER_BEHIND );
1166 else
1167 rLine.pLine->SetTabOrder( pRefWindow, WINDOW_ZORDER_FIRST );
1169 m_aOutOfDateLines.insert( nPos );
1170 rLine.pLine->SetComponentHelpIds(
1171 HelpIdUrl::getHelpId( _rPropertyData.HelpURL ),
1172 OUStringToOString( _rPropertyData.PrimaryButtonId, RTL_TEXTENCODING_UTF8 ),
1173 OUStringToOString( _rPropertyData.SecondaryButtonId, RTL_TEXTENCODING_UTF8 )
1176 if ( _rPropertyData.bReadOnly )
1178 rLine.pLine->SetReadOnly( true );
1180 // user controls (i.e. the ones not provided by the usual
1181 // XPropertyControlFactory) have no chance to know that they should be read-only,
1182 // since XPropertyHandler::describePropertyLine does not transport this
1183 // information.
1184 // So, we manually switch this to read-only.
1185 if ( xControl.is() && ( xControl->getControlType() == PropertyControlType::Unknown ) )
1187 vcl::Window *pWindow = rLine.pLine->getControlWindow();
1188 Edit* pControlWindowAsEdit = dynamic_cast<Edit*>(pWindow);
1189 if (pControlWindowAsEdit)
1190 pControlWindowAsEdit->SetReadOnly(true);
1191 else
1192 pWindow->Enable(false);
1199 bool OBrowserListBox::PreNotify( NotifyEvent& _rNEvt )
1201 switch ( _rNEvt.GetType() )
1203 case MouseNotifyEvent::KEYINPUT:
1205 const KeyEvent* pKeyEvent = _rNEvt.GetKeyEvent();
1206 if ( ( pKeyEvent->GetKeyCode().GetModifier() != 0 )
1207 || ( ( pKeyEvent->GetKeyCode().GetCode() != KEY_PAGEUP )
1208 && ( pKeyEvent->GetKeyCode().GetCode() != KEY_PAGEDOWN )
1211 break;
1213 long nScrollOffset = 0;
1214 if ( m_aVScroll->IsVisible() )
1216 if ( pKeyEvent->GetKeyCode().GetCode() == KEY_PAGEUP )
1217 nScrollOffset = -m_aVScroll->GetPageSize();
1218 else if ( pKeyEvent->GetKeyCode().GetCode() == KEY_PAGEDOWN )
1219 nScrollOffset = m_aVScroll->GetPageSize();
1222 if ( nScrollOffset )
1224 long nNewThumbPos = m_aVScroll->GetThumbPos() + nScrollOffset;
1225 nNewThumbPos = ::std::max( nNewThumbPos, m_aVScroll->GetRangeMin() );
1226 nNewThumbPos = ::std::min( nNewThumbPos, m_aVScroll->GetRangeMax() );
1227 m_aVScroll->DoScroll( nNewThumbPos );
1228 nNewThumbPos = m_aVScroll->GetThumbPos();
1230 sal_uInt16 nFocusControlPos = 0;
1231 sal_uInt16 nActiveControlPos = impl_getControlPos( m_xActiveControl );
1232 if ( nActiveControlPos < nNewThumbPos )
1233 nFocusControlPos = (sal_uInt16)nNewThumbPos;
1234 else if ( nActiveControlPos >= nNewThumbPos + CalcVisibleLines() )
1235 nFocusControlPos = (sal_uInt16)nNewThumbPos + CalcVisibleLines() - 1;
1236 if ( nFocusControlPos )
1238 if ( nFocusControlPos < m_aLines.size() )
1240 m_aLines[ nFocusControlPos ].pLine->GrabFocus();
1242 else
1243 OSL_FAIL( "OBrowserListBox::PreNotify: internal error, invalid focus control position!" );
1247 return true;
1248 // handled this. In particular, we also consume PageUp/Down events if we do not use them for scrolling,
1249 // otherwise they would be used to scroll the document view, which does not sound like it is desired by
1250 // the user.
1252 default:
1253 break;
1255 return Control::PreNotify( _rNEvt );
1258 bool OBrowserListBox::Notify( NotifyEvent& _rNEvt )
1260 if ( _rNEvt.GetType() == MouseNotifyEvent::COMMAND)
1262 const CommandEvent* pCommand = _rNEvt.GetCommandEvent();
1263 if ( ( CommandEventId::Wheel == pCommand->GetCommand() )
1264 || ( CommandEventId::StartAutoScroll == pCommand->GetCommand() )
1265 || ( CommandEventId::AutoScroll == pCommand->GetCommand() )
1268 // interested in scroll events if we have a scrollbar
1269 if ( m_aVScroll->IsVisible() )
1271 HandleScrollCommand( *pCommand, NULL, m_aVScroll.get() );
1275 return Control::Notify( _rNEvt );
1279 } // namespace pcr
1283 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */