1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/implbase1.hxx>
34 #include <vcl/svapp.hxx>
35 #include <osl/mutex.hxx>
37 //............................................................................
40 //............................................................................
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
;
56 using ::com::sun::star::graphic::XGraphic
;
58 namespace PropertyControlType
= ::com::sun::star::inspection::PropertyControlType
;
60 //==================================================================
62 //==================================================================
70 struct ControlEvent
: public ::comphelper::AnyEvent
72 Reference
< XPropertyControl
> xControl
;
73 ControlEventType eType
;
75 ControlEvent( const Reference
< XPropertyControl
>& _rxControl
, ControlEventType _eType
)
76 :xControl( _rxControl
)
82 //==================================================================
84 //==================================================================
88 static ::osl::Mutex
& getMutex();
89 static ::rtl::Reference
< ::comphelper::AsyncEventNotifier
> s_pNotifier
;
92 static const ::rtl::Reference
< ::comphelper::AsyncEventNotifier
>&
96 SharedNotifier(); // never implemented
97 SharedNotifier( const SharedNotifier
& ); // never implemented
98 SharedNotifier
& operator=( const SharedNotifier
& ); // never implemented
101 //------------------------------------------------------------------
102 ::rtl::Reference
< ::comphelper::AsyncEventNotifier
> SharedNotifier::s_pNotifier
;
104 //------------------------------------------------------------------
105 ::osl::Mutex
& SharedNotifier::getMutex()
107 static ::osl::Mutex s_aMutex
;
111 //------------------------------------------------------------------
112 const ::rtl::Reference
< ::comphelper::AsyncEventNotifier
>& SharedNotifier::getNotifier()
114 ::osl::MutexGuard
aGuard( getMutex() );
115 if ( !s_pNotifier
.is() )
118 new ::comphelper::AsyncEventNotifier("browserlistbox"));
119 s_pNotifier
->launch();
120 //TODO: a protocol is missing how to join with the launched
121 // thread before exit(3), to ensure the thread is no longer
122 // relying on any infrastructure while that infrastructure is
123 // being shut down in atexit handlers
128 //==================================================================
129 //= PropertyControlContext_Impl
130 //==================================================================
131 /** implementation for of <type scope="com::sun::star::inspection">XPropertyControlContext</type>
132 which forwards all events to a non-UNO version of this interface
134 typedef ::cppu::WeakImplHelper1
< XPropertyControlContext
> PropertyControlContext_Impl_Base
;
135 class PropertyControlContext_Impl
:public PropertyControlContext_Impl_Base
136 ,public ::comphelper::IEventProcessor
146 IControlContext
* m_pContext
;
147 NotifcationMode m_eMode
;
150 /** creates an instance
152 the instance to delegate events to
154 PropertyControlContext_Impl( IControlContext
& _rContextImpl
);
156 /** disposes the context.
158 When you call this method, all subsequent callbacks to the
159 <type scope="com::sun::star::inspection">XPropertyControlContext</type> methods
160 will throw a <type scope="com::sun::star::lang">DisposedException</type>.
162 void SAL_CALL
dispose();
164 /** sets the notification mode, so that notifications received from the controls are
165 forwarded to our IControlContext either synchronously or asynchronously
167 the new notification mode
169 void setNotificationMode( NotifcationMode _eMode
);
171 virtual void SAL_CALL
acquire() throw();
172 virtual void SAL_CALL
release() throw();
175 ~PropertyControlContext_Impl();
177 // XPropertyControlObserver
178 virtual void SAL_CALL
focusGained( const Reference
< XPropertyControl
>& Control
) throw (RuntimeException
);
179 virtual void SAL_CALL
valueChanged( const Reference
< XPropertyControl
>& Control
) throw (RuntimeException
);
180 // XPropertyControlContext
181 virtual void SAL_CALL
activateNextControl( const Reference
< XPropertyControl
>& CurrentControl
) throw (RuntimeException
);
184 virtual void processEvent( const ::comphelper::AnyEvent
& _rEvent
);
187 /** processes the given event, i.e. notifies it to our IControlContext
191 our mutex (well, the SolarMutex) is locked
193 void impl_processEvent_throw( const ::comphelper::AnyEvent
& _rEvent
);
195 /** checks whether we're alive
197 @throws DisposedException
198 if the instance is already disposed
200 void impl_checkAlive_throw() const;
202 /** checks whether the instance is already disposed
204 bool impl_isDisposed_nothrow() const { return m_pContext
== NULL
; }
206 /** notifies the given event originating from the given control
207 @throws DisposedException
211 void impl_notify_throw( const Reference
< XPropertyControl
>& _rxControl
, ControlEventType _eType
);
214 //--------------------------------------------------------------------
215 PropertyControlContext_Impl::PropertyControlContext_Impl( IControlContext
& _rContextImpl
)
216 :m_pContext( &_rContextImpl
)
217 ,m_eMode( eAsynchronously
)
221 //--------------------------------------------------------------------
222 PropertyControlContext_Impl::~PropertyControlContext_Impl()
224 if ( !impl_isDisposed_nothrow() )
228 //--------------------------------------------------------------------
229 void PropertyControlContext_Impl::impl_checkAlive_throw() const
231 if ( impl_isDisposed_nothrow() )
232 throw DisposedException( OUString(), *const_cast< PropertyControlContext_Impl
* >( this ) );
235 //--------------------------------------------------------------------
236 void SAL_CALL
PropertyControlContext_Impl::dispose()
238 SolarMutexGuard aGuard
;
239 if ( impl_isDisposed_nothrow() )
242 SharedNotifier::getNotifier()->removeEventsForProcessor( this );
246 //--------------------------------------------------------------------
247 void PropertyControlContext_Impl::setNotificationMode( NotifcationMode _eMode
)
249 SolarMutexGuard aGuard
;
253 //--------------------------------------------------------------------
254 void PropertyControlContext_Impl::impl_notify_throw( const Reference
< XPropertyControl
>& _rxControl
, ControlEventType _eType
)
256 ::comphelper::AnyEventRef pEvent
;
259 SolarMutexGuard aGuard
;
260 impl_checkAlive_throw();
261 pEvent
= new ControlEvent( _rxControl
, _eType
);
263 if ( m_eMode
== eSynchronously
)
265 impl_processEvent_throw( *pEvent
);
270 SharedNotifier::getNotifier()->addEvent( pEvent
, this );
273 //--------------------------------------------------------------------
274 void SAL_CALL
PropertyControlContext_Impl::focusGained( const Reference
< XPropertyControl
>& Control
) throw (RuntimeException
)
276 OSL_TRACE( "PropertyControlContext_Impl: FOCUS_GAINED" );
277 impl_notify_throw( Control
, FOCUS_GAINED
);
280 //--------------------------------------------------------------------
281 void SAL_CALL
PropertyControlContext_Impl::valueChanged( const Reference
< XPropertyControl
>& Control
) throw (RuntimeException
)
283 OSL_TRACE( "PropertyControlContext_Impl: VALUE_CHANGED" );
284 impl_notify_throw( Control
, VALUE_CHANGED
);
287 //--------------------------------------------------------------------
288 void SAL_CALL
PropertyControlContext_Impl::activateNextControl( const Reference
< XPropertyControl
>& CurrentControl
) throw (RuntimeException
)
290 OSL_TRACE( "PropertyControlContext_Impl: ACTIVATE_NEXT" );
291 impl_notify_throw( CurrentControl
, ACTIVATE_NEXT
);
294 //--------------------------------------------------------------------
295 void SAL_CALL
PropertyControlContext_Impl::acquire() throw()
297 PropertyControlContext_Impl_Base::acquire();
300 //--------------------------------------------------------------------
301 void SAL_CALL
PropertyControlContext_Impl::release() throw()
303 PropertyControlContext_Impl_Base::release();
306 //--------------------------------------------------------------------
307 void PropertyControlContext_Impl::processEvent( const ::comphelper::AnyEvent
& _rEvent
)
309 SolarMutexGuard aGuard
;
310 if ( impl_isDisposed_nothrow() )
315 impl_processEvent_throw( _rEvent
);
317 catch( const Exception
& )
319 // can't handle otherwise, since our caller (the notification thread) does not allow
320 // for exceptions (it could itself abort only)
321 DBG_UNHANDLED_EXCEPTION();
325 //--------------------------------------------------------------------
326 void PropertyControlContext_Impl::impl_processEvent_throw( const ::comphelper::AnyEvent
& _rEvent
)
328 const ControlEvent
& rControlEvent
= static_cast< const ControlEvent
& >( _rEvent
);
329 switch ( rControlEvent
.eType
)
332 OSL_TRACE( "PropertyControlContext_Impl::processEvent: FOCUS_GAINED" );
333 m_pContext
->focusGained( rControlEvent
.xControl
);
336 OSL_TRACE( "PropertyControlContext_Impl::processEvent: VALUE_CHANGED" );
337 m_pContext
->valueChanged( rControlEvent
.xControl
);
340 OSL_TRACE( "PropertyControlContext_Impl::processEvent: ACTIVATE_NEXT" );
341 m_pContext
->activateNextControl( rControlEvent
.xControl
);
346 //==================================================================
348 //==================================================================
349 DBG_NAME(OBrowserListBox
)
350 //------------------------------------------------------------------
351 OBrowserListBox::OBrowserListBox( Window
* pParent
, WinBits nWinStyle
)
352 :Control(pParent
, nWinStyle
| WB_CLIPCHILDREN
)
353 ,m_aLinesPlayground(this,WB_DIALOGCONTROL
| WB_CLIPCHILDREN
)
354 ,m_aVScroll(this,WB_VSCROLL
|WB_REPEAT
|WB_DRAG
)
355 ,m_pHelpWindow( new InspectorHelpWindow( this ) )
356 ,m_pLineListener(NULL
)
357 ,m_pControlObserver( NULL
)
359 ,m_nCurrentPreferredHelpHeight(0)
361 ,m_bIsActive(sal_False
)
363 ,m_pControlContextImpl( new PropertyControlContext_Impl( *this ) )
365 DBG_CTOR(OBrowserListBox
,NULL
);
367 ListBox
aListBox(this,WB_DROPDOWN
);
368 aListBox
.SetPosSizePixel(Point(0,0),Size(100,100));
369 m_nRowHeight
= (sal_uInt16
)aListBox
.GetSizePixel().Height()+2;
370 SetBackground( pParent
->GetBackground() );
371 m_aLinesPlayground
.SetBackground( GetBackground() );
373 m_aLinesPlayground
.SetPosPixel(Point(0,0));
374 m_aLinesPlayground
.SetPaintTransparent(sal_True
);
375 m_aLinesPlayground
.Show();
377 m_aVScroll
.SetScrollHdl(LINK(this, OBrowserListBox
, ScrollHdl
));
380 //------------------------------------------------------------------
381 OBrowserListBox::~OBrowserListBox()
383 OSL_ENSURE( !IsModified(), "OBrowserListBox::~OBrowserListBox: still modified - should have been committed before!" );
384 // doing the commit here, while we, as well as our owner, as well as some other components,
385 // are already "half dead" (means within their dtor) is potentially dangerous.
386 // By definition, CommitModified has to be called (if necessary) before destruction
388 m_pControlContextImpl
->dispose();
389 m_pControlContextImpl
.clear();
394 DBG_DTOR(OBrowserListBox
,NULL
);
397 //------------------------------------------------------------------
398 sal_Bool
OBrowserListBox::IsModified( ) const
400 sal_Bool bModified
= sal_False
;
402 if ( m_bIsActive
&& m_xActiveControl
.is() )
403 bModified
= m_xActiveControl
->isModified();
408 //------------------------------------------------------------------
409 void OBrowserListBox::CommitModified( )
411 if ( IsModified() && m_xActiveControl
.is() )
413 // for the time of this commit, notify all events synchronously
415 m_pControlContextImpl
->setNotificationMode( PropertyControlContext_Impl::eSynchronously
);
418 m_xActiveControl
->notifyModifiedValue();
420 catch( const Exception
& )
422 DBG_UNHANDLED_EXCEPTION();
424 m_pControlContextImpl
->setNotificationMode( PropertyControlContext_Impl::eAsynchronously
);
428 //------------------------------------------------------------------
429 void OBrowserListBox::ActivateListBox(sal_Bool _bActive
)
431 m_bIsActive
= _bActive
;
434 // TODO: what's the sense of this?
435 m_aVScroll
.SetThumbPos(100);
441 //------------------------------------------------------------------
442 long OBrowserListBox::impl_getPrefererredHelpHeight()
444 return HasHelpSection() ? m_pHelpWindow
->GetOptimalHeightPixel() : 0;
447 //------------------------------------------------------------------
448 void OBrowserListBox::Resize()
450 Rectangle
aPlayground( Point( 0, 0 ), GetOutputSizePixel() );
451 Size
aHelpWindowDistance( LogicToPixel( Size( 0, LAYOUT_HELP_WINDOW_DISTANCE_APPFONT
), MAP_APPFONT
) );
453 long nHelpWindowHeight
= m_nCurrentPreferredHelpHeight
= impl_getPrefererredHelpHeight();
454 bool bPositionHelpWindow
= ( nHelpWindowHeight
!= 0 );
456 Rectangle
aLinesArea( aPlayground
);
457 if ( bPositionHelpWindow
)
459 aLinesArea
.Bottom() -= nHelpWindowHeight
;
460 aLinesArea
.Bottom() -= aHelpWindowDistance
.Height();
462 m_aLinesPlayground
.SetPosSizePixel( aLinesArea
.TopLeft(), aLinesArea
.GetSize() );
466 sal_Bool bNeedScrollbar
= m_aLines
.size() > (sal_uInt32
)CalcVisibleLines();
467 if ( !bNeedScrollbar
)
469 if ( m_aVScroll
.IsVisible() )
473 m_aVScroll
.SetThumbPos( 0 );
477 Size
aVScrollSize( m_aVScroll
.GetSizePixel() );
479 // adjust the playground's width
480 aLinesArea
.Right() -= aVScrollSize
.Width();
481 m_aLinesPlayground
.SetPosSizePixel( aLinesArea
.TopLeft(), aLinesArea
.GetSize() );
483 // position the scrollbar
484 aVScrollSize
.Height() = aLinesArea
.GetHeight();
485 Point
aVScrollPos( aLinesArea
.GetWidth(), 0 );
486 m_aVScroll
.SetPosSizePixel( aVScrollPos
, aVScrollSize
);
489 for ( sal_uInt16 i
= 0; i
< m_aLines
.size(); ++i
)
490 m_aOutOfDateLines
.insert( i
);
493 EnablePaint(sal_False
);
495 EnablePaint(sal_True
);
497 // show the scrollbar
498 if ( bNeedScrollbar
)
501 // position the help window
502 if ( bPositionHelpWindow
)
504 Rectangle
aHelpArea( aPlayground
);
505 aHelpArea
.Top() = aLinesArea
.Bottom() + aHelpWindowDistance
.Height();
506 m_pHelpWindow
->SetPosSizePixel( aHelpArea
.TopLeft(), aHelpArea
.GetSize() );
510 //------------------------------------------------------------------
511 void OBrowserListBox::SetListener( IPropertyLineListener
* _pListener
)
513 m_pLineListener
= _pListener
;
516 //------------------------------------------------------------------
517 void OBrowserListBox::SetObserver( IPropertyControlObserver
* _pObserver
)
519 m_pControlObserver
= _pObserver
;
522 //------------------------------------------------------------------
523 void OBrowserListBox::EnableHelpSection( bool _bEnable
)
525 m_pHelpWindow
->Show( _bEnable
);
529 //------------------------------------------------------------------
530 bool OBrowserListBox::HasHelpSection() const
532 return m_pHelpWindow
->IsVisible();
535 //------------------------------------------------------------------
536 void OBrowserListBox::SetHelpText( const OUString
& _rHelpText
)
538 OSL_ENSURE( HasHelpSection(), "OBrowserListBox::SetHelpText: help section not visible!" );
539 m_pHelpWindow
->SetText( _rHelpText
);
540 if ( m_nCurrentPreferredHelpHeight
!= impl_getPrefererredHelpHeight() )
544 //------------------------------------------------------------------
545 void OBrowserListBox::SetHelpLineLimites( sal_Int32 _nMinLines
, sal_Int32 _nMaxLines
)
547 m_pHelpWindow
->SetLimits( _nMinLines
, _nMaxLines
);
550 //------------------------------------------------------------------
551 sal_uInt16
OBrowserListBox::CalcVisibleLines()
553 Size
aSize(m_aLinesPlayground
.GetOutputSizePixel());
554 sal_uInt16 nResult
= 0;
555 if (0 != m_nRowHeight
)
556 nResult
= (sal_uInt16
) aSize
.Height()/m_nRowHeight
;
561 //------------------------------------------------------------------
562 void OBrowserListBox::UpdateVScroll()
564 sal_uInt16 nLines
= CalcVisibleLines();
565 m_aVScroll
.SetPageSize(nLines
-1);
566 m_aVScroll
.SetVisibleSize(nLines
-1);
568 size_t nCount
= m_aLines
.size();
571 m_aVScroll
.SetRange(Range(0,nCount
-1));
572 m_nYOffset
= -m_aVScroll
.GetThumbPos()*m_nRowHeight
;
576 m_aVScroll
.SetRange(Range(0,0));
581 //------------------------------------------------------------------
582 void OBrowserListBox::PositionLine( sal_uInt16 _nIndex
)
584 Size
aSize(m_aLinesPlayground
.GetOutputSizePixel());
585 Point
aPos(0, m_nYOffset
);
587 aSize
.Height() = m_nRowHeight
;
589 aPos
.Y() += _nIndex
* m_nRowHeight
;
591 if ( _nIndex
< m_aLines
.size() )
593 BrowserLinePointer pLine
= m_aLines
[ _nIndex
].pLine
;
595 pLine
->SetPosSizePixel( aPos
, aSize
);
596 pLine
->SetTitleWidth( m_nTheNameSize
+ 2 * FRAME_OFFSET
);
598 // show the line if necessary
599 if ( !pLine
->IsVisible() )
604 //------------------------------------------------------------------
605 void OBrowserListBox::UpdatePosNSize()
607 for ( ::std::set
< sal_uInt16
>::const_iterator aLoop
= m_aOutOfDateLines
.begin();
608 aLoop
!= m_aOutOfDateLines
.end();
612 DBG_ASSERT( *aLoop
< m_aLines
.size(), "OBrowserListBox::UpdatePosNSize: invalid line index!" );
613 if ( *aLoop
< m_aLines
.size() )
614 PositionLine( *aLoop
);
616 m_aOutOfDateLines
.clear();
619 //------------------------------------------------------------------
620 void OBrowserListBox::UpdatePlayGround()
622 sal_Int32 nThumbPos
= m_aVScroll
.GetThumbPos();
623 sal_Int32 nLines
= CalcVisibleLines();
625 sal_uInt16 nEnd
= (sal_uInt16
)(nThumbPos
+ nLines
);
626 if (nEnd
>= m_aLines
.size())
627 nEnd
= (sal_uInt16
)m_aLines
.size()-1;
629 if ( !m_aLines
.empty() )
631 for ( sal_uInt16 i
= (sal_uInt16
)nThumbPos
; i
<= nEnd
; ++i
)
632 m_aOutOfDateLines
.insert( i
);
637 //------------------------------------------------------------------
638 void OBrowserListBox::UpdateAll()
643 //------------------------------------------------------------------
644 void OBrowserListBox::DisableUpdate()
646 m_bUpdate
= sal_False
;
649 //------------------------------------------------------------------
650 void OBrowserListBox::EnableUpdate()
652 m_bUpdate
= sal_True
;
656 //------------------------------------------------------------------
657 void OBrowserListBox::SetPropertyValue(const OUString
& _rEntryName
, const Any
& _rValue
, bool _bUnknownValue
)
659 ListBoxLines::iterator line
= m_aLines
.begin();
660 for ( ; line
!= m_aLines
.end() && ( line
->aName
!= _rEntryName
); ++line
)
663 if ( line
!= m_aLines
.end() )
665 if ( _bUnknownValue
)
667 Reference
< XPropertyControl
> xControl( line
->pLine
->getControl() );
668 OSL_ENSURE( xControl
.is(), "OBrowserListBox::SetPropertyValue: illegal control!" );
670 xControl
->setValue( Any() );
673 impl_setControlAsPropertyValue( *line
, _rValue
);
677 //------------------------------------------------------------------------
678 sal_uInt16
OBrowserListBox::GetPropertyPos( const OUString
& _rEntryName
) const
680 sal_uInt16 nRet
= LISTBOX_ENTRY_NOTFOUND
;
681 for ( ListBoxLines::const_iterator linePos
= m_aLines
.begin();
682 linePos
!= m_aLines
.end();
686 if ( linePos
->aName
== _rEntryName
)
688 nRet
= (sal_uInt16
)( linePos
- m_aLines
.begin() );
696 //------------------------------------------------------------------------
697 bool OBrowserListBox::impl_getBrowserLineForName( const OUString
& _rEntryName
, BrowserLinePointer
& _out_rpLine
) const
699 ListBoxLines::const_iterator line
= m_aLines
.begin();
700 for ( ; line
!= m_aLines
.end() && ( line
->aName
!= _rEntryName
); ++line
)
703 if ( line
!= m_aLines
.end() )
704 _out_rpLine
= line
->pLine
;
707 return ( NULL
!= _out_rpLine
.get() );
710 //------------------------------------------------------------------------
711 void OBrowserListBox::EnablePropertyControls( const OUString
& _rEntryName
, sal_Int16 _nControls
, bool _bEnable
)
713 BrowserLinePointer pLine
;
714 if ( impl_getBrowserLineForName( _rEntryName
, pLine
) )
715 pLine
->EnablePropertyControls( _nControls
, _bEnable
);
718 //------------------------------------------------------------------------
719 void OBrowserListBox::EnablePropertyLine( const OUString
& _rEntryName
, bool _bEnable
)
721 BrowserLinePointer pLine
;
722 if ( impl_getBrowserLineForName( _rEntryName
, pLine
) )
723 pLine
->EnablePropertyLine( _bEnable
);
726 //------------------------------------------------------------------------
727 Reference
< XPropertyControl
> OBrowserListBox::GetPropertyControl( const OUString
& _rEntryName
)
729 BrowserLinePointer pLine
;
730 if ( impl_getBrowserLineForName( _rEntryName
, pLine
) )
731 return pLine
->getControl();
735 //------------------------------------------------------------------
736 sal_uInt16
OBrowserListBox::InsertEntry(const OLineDescriptor
& _rPropertyData
, sal_uInt16 _nPos
)
739 BrowserLinePointer
pBrowserLine( new OBrowserLine( _rPropertyData
.sName
, &m_aLinesPlayground
) );
741 // check that the name is unique
742 ListBoxLines::iterator it
= m_aLines
.begin();
743 for ( ; it
!= m_aLines
.end() && ( it
->aName
!= _rPropertyData
.sName
); ++it
)
745 OSL_ENSURE( it
== m_aLines
.end(), "OBrowserListBox::InsertEntry: already have another line for this name!" );
747 ListBoxLine
aNewLine( _rPropertyData
.sName
, pBrowserLine
, _rPropertyData
.xPropertyHandler
);
748 sal_uInt16 nInsertPos
= _nPos
;
749 if ( _nPos
>= m_aLines
.size() )
751 nInsertPos
= static_cast< sal_uInt16
>( m_aLines
.size() );
752 m_aLines
.push_back( aNewLine
);
755 m_aLines
.insert( m_aLines
.begin() + _nPos
, aNewLine
);
757 pBrowserLine
->SetTitleWidth(m_nTheNameSize
);
764 // initialize the entry
765 ChangeEntry(_rPropertyData
, nInsertPos
);
767 // update the positions of possibly affected lines
768 sal_uInt16 nUpdatePos
= nInsertPos
;
769 while ( nUpdatePos
< m_aLines
.size() )
770 m_aOutOfDateLines
.insert( nUpdatePos
++ );
776 //------------------------------------------------------------------
777 sal_Int32
OBrowserListBox::GetMinimumWidth()
779 return m_nTheNameSize
+ 2 * FRAME_OFFSET
+ (m_nRowHeight
- 4) * 8;
782 //------------------------------------------------------------------
783 sal_Int32
OBrowserListBox::GetMinimumHeight()
785 // assume that we want to display 5 rows, at least
786 sal_Int32 nMinHeight
= m_nRowHeight
* 5;
788 if ( HasHelpSection() )
790 Size
aHelpWindowDistance( LogicToPixel( Size( 0, LAYOUT_HELP_WINDOW_DISTANCE_APPFONT
), MAP_APPFONT
) );
791 nMinHeight
+= aHelpWindowDistance
.Height();
793 nMinHeight
+= m_pHelpWindow
->GetMinimalHeightPixel();
799 //------------------------------------------------------------------
800 void OBrowserListBox::ShowEntry(sal_uInt16 _nPos
)
802 if ( _nPos
< m_aLines
.size() )
804 sal_Int32 nThumbPos
= m_aVScroll
.GetThumbPos();
806 if (_nPos
< nThumbPos
)
810 sal_Int32 nLines
= CalcVisibleLines();
811 if (_nPos
>= nThumbPos
+ nLines
)
812 MoveThumbTo(_nPos
- nLines
+ 1);
818 //------------------------------------------------------------------
819 void OBrowserListBox::MoveThumbTo(sal_Int32 _nNewThumbPos
)
821 // disable painting to prevent flicker
822 m_aLinesPlayground
.EnablePaint(sal_False
);
824 sal_Int32 nDelta
= _nNewThumbPos
- m_aVScroll
.GetThumbPos();
825 // adjust the scrollbar
826 m_aVScroll
.SetThumbPos(_nNewThumbPos
);
827 sal_Int32 nThumbPos
= _nNewThumbPos
;
829 m_nYOffset
= -m_aVScroll
.GetThumbPos() * m_nRowHeight
;
831 sal_Int32 nLines
= CalcVisibleLines();
832 sal_uInt16 nEnd
= (sal_uInt16
)(nThumbPos
+ nLines
);
834 m_aLinesPlayground
.Scroll(0, -nDelta
* m_nRowHeight
, SCROLL_CHILDREN
);
838 // TODO: what's the sense of this two PositionLines? Why not just one call?
839 PositionLine(nEnd
-1);
842 else if (-1 == nDelta
)
844 PositionLine((sal_uInt16
)nThumbPos
);
846 else if (0 != nDelta
)
851 m_aLinesPlayground
.EnablePaint(sal_True
);
852 m_aLinesPlayground
.Invalidate(INVALIDATE_CHILDREN
);
855 //------------------------------------------------------------------
856 IMPL_LINK(OBrowserListBox
, ScrollHdl
, ScrollBar
*, _pScrollBar
)
858 DBG_ASSERT(_pScrollBar
== &m_aVScroll
, "OBrowserListBox::ScrollHdl: where does this come from?");
861 // disable painting to prevent flicker
862 m_aLinesPlayground
.EnablePaint(sal_False
);
864 sal_Int32 nThumbPos
= m_aVScroll
.GetThumbPos();
866 sal_Int32 nDelta
= m_aVScroll
.GetDelta();
867 m_nYOffset
= -nThumbPos
* m_nRowHeight
;
869 sal_uInt16 nEnd
= (sal_uInt16
)(nThumbPos
+ CalcVisibleLines());
871 m_aLinesPlayground
.Scroll(0, -nDelta
* m_nRowHeight
, SCROLL_CHILDREN
);
875 PositionLine(nEnd
-1);
880 PositionLine((sal_uInt16
)nThumbPos
);
882 else if (nDelta
!=0 || m_aVScroll
.GetType() == SCROLL_DONTKNOW
)
887 m_aLinesPlayground
.EnablePaint(sal_True
);
891 //------------------------------------------------------------------
892 void OBrowserListBox::buttonClicked( OBrowserLine
* _pLine
, sal_Bool _bPrimary
)
894 DBG_ASSERT( _pLine
, "OBrowserListBox::buttonClicked: invalid browser line!" );
895 if ( _pLine
&& m_pLineListener
)
897 m_pLineListener
->Clicked( _pLine
->GetEntryName(), _bPrimary
);
901 //------------------------------------------------------------------
902 void OBrowserListBox::impl_setControlAsPropertyValue( const ListBoxLine
& _rLine
, const Any
& _rPropertyValue
)
904 Reference
< XPropertyControl
> xControl( _rLine
.pLine
->getControl() );
907 if ( _rPropertyValue
.getValueType().equals( _rLine
.pLine
->getControl()->getValueType() ) )
909 xControl
->setValue( _rPropertyValue
);
914 if ( !_rLine
.xHandler
.is() )
916 OString
sMessage( "OBrowserListBox::impl_setControlAsPropertyValue: no handler -> no conversion (property: '" );
917 OUString
sPropertyName( _rLine
.pLine
->GetEntryName() );
918 sMessage
+= OString( sPropertyName
.getStr(), sPropertyName
.getLength(), RTL_TEXTENCODING_ASCII_US
);
919 sMessage
+= OString( "')!" );
920 OSL_FAIL( sMessage
.getStr() );
923 if ( _rLine
.xHandler
.is() )
925 Any aControlValue
= _rLine
.xHandler
->convertToControlValue(
926 _rLine
.pLine
->GetEntryName(), _rPropertyValue
, xControl
->getValueType() );
927 xControl
->setValue( aControlValue
);
931 catch( const Exception
& )
933 DBG_UNHANDLED_EXCEPTION();
937 //------------------------------------------------------------------
938 Any
OBrowserListBox::impl_getControlAsPropertyValue( const ListBoxLine
& _rLine
) const
940 Reference
< XPropertyControl
> xControl( _rLine
.pLine
->getControl() );
945 if ( !_rLine
.xHandler
.is() )
947 OString
sMessage( "OBrowserListBox::impl_getControlAsPropertyValue: no handler -> no conversion (property: '" );
948 OUString
sPropertyName( _rLine
.pLine
->GetEntryName() );
949 sMessage
+= OString( sPropertyName
.getStr(), sPropertyName
.getLength(), RTL_TEXTENCODING_ASCII_US
);
950 sMessage
+= OString( "')!" );
951 OSL_FAIL( sMessage
.getStr() );
954 if ( _rLine
.xHandler
.is() )
955 aPropertyValue
= _rLine
.xHandler
->convertToPropertyValue( _rLine
.pLine
->GetEntryName(), xControl
->getValue() );
957 aPropertyValue
= xControl
->getValue();
959 catch( const Exception
& )
961 DBG_UNHANDLED_EXCEPTION();
963 return aPropertyValue
;
966 //------------------------------------------------------------------
967 sal_uInt16
OBrowserListBox::impl_getControlPos( const Reference
< XPropertyControl
>& _rxControl
) const
969 for ( ListBoxLines::const_iterator search
= m_aLines
.begin(); search
!= m_aLines
.end(); ++search
)
970 if ( search
->pLine
->getControl().get() == _rxControl
.get() )
971 return sal_uInt16( search
- m_aLines
.begin() );
973 OSL_FAIL( "OBrowserListBox::impl_getControlPos: invalid control - not part of any of our lines!" );
974 return (sal_uInt16
)-1;
977 //--------------------------------------------------------------------
978 void SAL_CALL
OBrowserListBox::focusGained( const Reference
< XPropertyControl
>& _rxControl
) throw (RuntimeException
)
980 DBG_TESTSOLARMUTEX();
982 DBG_ASSERT( _rxControl
.is(), "OBrowserListBox::focusGained: invalid event source!" );
983 if ( !_rxControl
.is() )
986 if ( m_pControlObserver
)
987 m_pControlObserver
->focusGained( _rxControl
);
989 m_xActiveControl
= _rxControl
;
990 ShowEntry( impl_getControlPos( m_xActiveControl
) );
993 //--------------------------------------------------------------------
994 void SAL_CALL
OBrowserListBox::valueChanged( const Reference
< XPropertyControl
>& _rxControl
) throw (RuntimeException
)
996 DBG_TESTSOLARMUTEX();
998 DBG_ASSERT( _rxControl
.is(), "OBrowserListBox::valueChanged: invalid event source!" );
999 if ( !_rxControl
.is() )
1002 if ( m_pControlObserver
)
1003 m_pControlObserver
->valueChanged( _rxControl
);
1005 if ( m_pLineListener
)
1007 const ListBoxLine
& rLine
= m_aLines
[ impl_getControlPos( _rxControl
) ];
1008 m_pLineListener
->Commit(
1009 rLine
.pLine
->GetEntryName(),
1010 impl_getControlAsPropertyValue( rLine
)
1015 //--------------------------------------------------------------------
1016 void SAL_CALL
OBrowserListBox::activateNextControl( const Reference
< XPropertyControl
>& _rxCurrentControl
) throw (RuntimeException
)
1018 DBG_TESTSOLARMUTEX();
1020 sal_uInt16 nLine
= impl_getControlPos( _rxCurrentControl
);
1022 // cycle forwards, 'til we've the next control which can grab the focus
1024 while ( static_cast< size_t >( nLine
) < m_aLines
.size() )
1026 if ( m_aLines
[nLine
].pLine
->GrabFocus() )
1032 if ( ( static_cast< size_t >( nLine
) >= m_aLines
.size() ) && ( m_aLines
.size() > 0 ) )
1033 m_aLines
[0].pLine
->GrabFocus();
1036 //------------------------------------------------------------------
1039 //..............................................................
1040 void lcl_implDisposeControl_nothrow( const Reference
< XPropertyControl
>& _rxControl
)
1042 if ( !_rxControl
.is() )
1046 _rxControl
->setControlContext( NULL
);
1047 Reference
< XComponent
> xControlComponent( _rxControl
, UNO_QUERY
);
1048 if ( xControlComponent
.is() )
1049 xControlComponent
->dispose();
1051 catch( const Exception
& )
1053 DBG_UNHANDLED_EXCEPTION();
1058 //------------------------------------------------------------------
1059 void OBrowserListBox::Clear()
1061 for ( ListBoxLines::iterator loop
= m_aLines
.begin(); loop
!= m_aLines
.end(); ++loop
)
1064 loop
->pLine
->Hide();
1065 // reset the listener
1066 lcl_implDisposeControl_nothrow( loop
->pLine
->getControl() );
1069 clearContainer( m_aLines
);
1072 //------------------------------------------------------------------
1073 sal_Bool
OBrowserListBox::RemoveEntry( const OUString
& _rName
)
1075 sal_uInt16 nPos
= 0;
1076 ListBoxLines::iterator it
= m_aLines
.begin();
1077 for ( ; it
!= m_aLines
.end() && ( it
->aName
!= _rName
); ++it
, ++nPos
)
1080 if ( it
== m_aLines
.end() )
1083 m_aLines
.erase( it
);
1084 m_aOutOfDateLines
.erase( (sal_uInt16
)m_aLines
.size() );
1086 // update the positions of possibly affected lines
1087 while ( nPos
< m_aLines
.size() )
1088 m_aOutOfDateLines
.insert( nPos
++ );
1094 //------------------------------------------------------------------
1095 void OBrowserListBox::ChangeEntry( const OLineDescriptor
& _rPropertyData
, sal_uInt16 nPos
)
1097 OSL_PRECOND( _rPropertyData
.Control
.is(), "OBrowserListBox::ChangeEntry: invalid control!" );
1098 if ( !_rPropertyData
.Control
.is() )
1101 if ( nPos
== EDITOR_LIST_REPLACE_EXISTING
)
1102 nPos
= GetPropertyPos( _rPropertyData
.sName
);
1104 if ( nPos
< m_aLines
.size() )
1106 Window
* pRefWindow
= NULL
;
1108 pRefWindow
= m_aLines
[nPos
-1].pLine
->GetRefWindow();
1110 // the current line and control
1111 ListBoxLine
& rLine
= m_aLines
[nPos
];
1113 // the old control and some data about it
1114 Reference
< XPropertyControl
> xControl
= rLine
.pLine
->getControl();
1115 Window
* pControlWindow
= rLine
.pLine
->getControlWindow();
1117 if ( pControlWindow
)
1118 aControlPos
= pControlWindow
->GetPosPixel();
1120 // clean up the old control
1121 lcl_implDisposeControl_nothrow( xControl
);
1123 // set the new control at the line
1124 rLine
.pLine
->setControl( _rPropertyData
.Control
);
1125 xControl
= rLine
.pLine
->getControl();
1127 if ( xControl
.is() )
1128 xControl
->setControlContext( m_pControlContextImpl
.get() );
1130 // the initial property value
1131 if ( _rPropertyData
.bUnknownValue
)
1132 xControl
->setValue( Any() );
1134 impl_setControlAsPropertyValue( rLine
, _rPropertyData
.aValue
);
1136 rLine
.pLine
->SetTitle(_rPropertyData
.DisplayName
);
1137 rLine
.xHandler
= _rPropertyData
.xPropertyHandler
;
1139 sal_uInt16 nTextWidth
= (sal_uInt16
)m_aLinesPlayground
.GetTextWidth(_rPropertyData
.DisplayName
);
1140 if (m_nTheNameSize
< nTextWidth
)
1141 m_nTheNameSize
= nTextWidth
;
1143 if ( _rPropertyData
.HasPrimaryButton
)
1145 if ( !_rPropertyData
.PrimaryButtonImageURL
.isEmpty() )
1146 rLine
.pLine
->ShowBrowseButton( _rPropertyData
.PrimaryButtonImageURL
, true );
1147 else if ( _rPropertyData
.PrimaryButtonImage
.is() )
1148 rLine
.pLine
->ShowBrowseButton( Image( _rPropertyData
.PrimaryButtonImage
), true );
1150 rLine
.pLine
->ShowBrowseButton( true );
1152 if ( _rPropertyData
.HasSecondaryButton
)
1154 if ( !_rPropertyData
.SecondaryButtonImageURL
.isEmpty() )
1155 rLine
.pLine
->ShowBrowseButton( _rPropertyData
.SecondaryButtonImageURL
, false );
1156 else if ( _rPropertyData
.SecondaryButtonImage
.is() )
1157 rLine
.pLine
->ShowBrowseButton( Image( _rPropertyData
.SecondaryButtonImage
), false );
1159 rLine
.pLine
->ShowBrowseButton( false );
1162 rLine
.pLine
->HideBrowseButton( false );
1164 rLine
.pLine
->SetClickListener( this );
1168 rLine
.pLine
->HideBrowseButton( true );
1169 rLine
.pLine
->HideBrowseButton( false );
1172 DBG_ASSERT( ( _rPropertyData
.IndentLevel
== 0 ) || ( _rPropertyData
.IndentLevel
== 1 ),
1173 "OBrowserListBox::ChangeEntry: unsupported indent level!" );
1174 rLine
.pLine
->IndentTitle( _rPropertyData
.IndentLevel
> 0 );
1177 rLine
.pLine
->SetTabOrder( pRefWindow
, WINDOW_ZORDER_BEHIND
);
1179 rLine
.pLine
->SetTabOrder( pRefWindow
, WINDOW_ZORDER_FIRST
);
1181 m_aOutOfDateLines
.insert( nPos
);
1182 rLine
.pLine
->SetComponentHelpIds(
1183 HelpIdUrl::getHelpId( _rPropertyData
.HelpURL
),
1184 OUStringToOString( _rPropertyData
.PrimaryButtonId
, RTL_TEXTENCODING_UTF8
),
1185 OUStringToOString( _rPropertyData
.SecondaryButtonId
, RTL_TEXTENCODING_UTF8
)
1188 if ( _rPropertyData
.bReadOnly
)
1190 rLine
.pLine
->SetReadOnly( true );
1192 // user controls (i.e. the ones not provided by the usual
1193 // XPropertyControlFactory) have no chance to know that they should be read-only,
1194 // since XPropertyHandler::describePropertyLine does not transport this
1196 // So, we manually switch this to read-only.
1197 if ( xControl
.is() && ( xControl
->getControlType() == PropertyControlType::Unknown
) )
1199 Edit
* pControlWindowAsEdit
= dynamic_cast< Edit
* >( rLine
.pLine
->getControlWindow() );
1200 if ( pControlWindowAsEdit
)
1201 pControlWindowAsEdit
->SetReadOnly( sal_True
);
1203 pControlWindowAsEdit
->Enable( sal_False
);
1209 //------------------------------------------------------------------
1210 long OBrowserListBox::PreNotify( NotifyEvent
& _rNEvt
)
1212 switch ( _rNEvt
.GetType() )
1214 case EVENT_KEYINPUT
:
1216 const KeyEvent
* pKeyEvent
= _rNEvt
.GetKeyEvent();
1217 if ( ( pKeyEvent
->GetKeyCode().GetModifier() != 0 )
1218 || ( ( pKeyEvent
->GetKeyCode().GetCode() != KEY_PAGEUP
)
1219 && ( pKeyEvent
->GetKeyCode().GetCode() != KEY_PAGEDOWN
)
1224 long nScrollOffset
= 0;
1225 if ( m_aVScroll
.IsVisible() )
1227 if ( pKeyEvent
->GetKeyCode().GetCode() == KEY_PAGEUP
)
1228 nScrollOffset
= -m_aVScroll
.GetPageSize();
1229 else if ( pKeyEvent
->GetKeyCode().GetCode() == KEY_PAGEDOWN
)
1230 nScrollOffset
= m_aVScroll
.GetPageSize();
1233 if ( nScrollOffset
)
1235 long nNewThumbPos
= m_aVScroll
.GetThumbPos() + nScrollOffset
;
1236 nNewThumbPos
= ::std::max( nNewThumbPos
, m_aVScroll
.GetRangeMin() );
1237 nNewThumbPos
= ::std::min( nNewThumbPos
, m_aVScroll
.GetRangeMax() );
1238 m_aVScroll
.DoScroll( nNewThumbPos
);
1239 nNewThumbPos
= m_aVScroll
.GetThumbPos();
1241 sal_uInt16 nFocusControlPos
= 0;
1242 sal_uInt16 nActiveControlPos
= impl_getControlPos( m_xActiveControl
);
1243 if ( nActiveControlPos
< nNewThumbPos
)
1244 nFocusControlPos
= (sal_uInt16
)nNewThumbPos
;
1245 else if ( nActiveControlPos
>= nNewThumbPos
+ CalcVisibleLines() )
1246 nFocusControlPos
= (sal_uInt16
)nNewThumbPos
+ CalcVisibleLines() - 1;
1247 if ( nFocusControlPos
)
1249 if ( nFocusControlPos
< m_aLines
.size() )
1251 m_aLines
[ nFocusControlPos
].pLine
->GrabFocus();
1254 OSL_FAIL( "OBrowserListBox::PreNotify: internal error, invalid focus control position!" );
1259 // handled this. In particular, we also consume PageUp/Down events if we do not use them for scrolling,
1260 // otherwise they would be used to scroll the document view, which does not sound like it is desired by
1264 return Control::PreNotify( _rNEvt
);
1267 //------------------------------------------------------------------
1268 long OBrowserListBox::Notify( NotifyEvent
& _rNEvt
)
1270 switch ( _rNEvt
.GetType() )
1274 const CommandEvent
* pCommand
= _rNEvt
.GetCommandEvent();
1275 if ( ( COMMAND_WHEEL
== pCommand
->GetCommand() )
1276 || ( COMMAND_STARTAUTOSCROLL
== pCommand
->GetCommand() )
1277 || ( COMMAND_AUTOSCROLL
== pCommand
->GetCommand() )
1280 // interested in scroll events if we have a scrollbar
1281 if ( m_aVScroll
.IsVisible() )
1283 HandleScrollCommand( *pCommand
, NULL
, &m_aVScroll
);
1290 return Control::Notify( _rNEvt
);
1293 //............................................................................
1295 //............................................................................
1298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */