Update ooo320-m1
[ooovba.git] / extensions / source / propctrlr / browserlistbox.cxx
blob40cdb90cfcaa70dbb88332dc48f4d1f552c98601
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: browserlistbox.cxx,v $
10 * $Revision: 1.22 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
33 #include "browserlistbox.hxx"
34 #ifndef EXTENSIONS_PROPRESID_HRC
35 #include "propresid.hrc"
36 #endif
37 #include "proplinelistener.hxx"
38 #include "propcontrolobserver.hxx"
39 #include "linedescriptor.hxx"
40 #include "inspectorhelpwindow.hxx"
42 /** === begin UNO includes === **/
43 #include <com/sun/star/lang/DisposedException.hpp>
44 #include <com/sun/star/lang/XComponent.hpp>
45 #include <com/sun/star/inspection/PropertyControlType.hpp>
46 /** === end UNO includes === **/
47 #include <tools/debug.hxx>
48 #include <tools/diagnose_ex.h>
49 #include <comphelper/asyncnotification.hxx>
50 #include <cppuhelper/implbase1.hxx>
51 #include <vcl/svapp.hxx>
52 #include <vos/mutex.hxx>
54 //............................................................................
55 namespace pcr
57 //............................................................................
59 #define FRAME_OFFSET 4
60 // TODO: find out what this is really for ... and check if it does make sense in the new
61 // browser environment
62 #define LAYOUT_HELP_WINDOW_DISTANCE_APPFONT 3
64 /** === begin UNO using === **/
65 using ::com::sun::star::uno::Any;
66 using ::com::sun::star::uno::Exception;
67 using ::com::sun::star::inspection::XPropertyControlContext;
68 using ::com::sun::star::uno::Reference;
69 using ::com::sun::star::inspection::XPropertyControl;
70 using ::com::sun::star::uno::RuntimeException;
71 using ::com::sun::star::lang::DisposedException;
72 using ::com::sun::star::lang::XComponent;
73 using ::com::sun::star::uno::UNO_QUERY;
74 using ::com::sun::star::graphic::XGraphic;
75 /** === end UNO using === **/
76 namespace PropertyControlType = ::com::sun::star::inspection::PropertyControlType;
78 //==================================================================
79 //= ControlEvent
80 //==================================================================
81 enum ControlEventType
83 FOCUS_GAINED,
84 VALUE_CHANGED,
85 ACTIVATE_NEXT
88 struct ControlEvent : public ::comphelper::AnyEvent
90 Reference< XPropertyControl > xControl;
91 ControlEventType eType;
93 ControlEvent( const Reference< XPropertyControl >& _rxControl, ControlEventType _eType )
94 :xControl( _rxControl )
95 ,eType( _eType )
100 //==================================================================
101 //= SharedNotifier
102 //==================================================================
103 class SharedNotifier
105 private:
106 static ::osl::Mutex& getMutex();
107 static ::rtl::Reference< ::comphelper::AsyncEventNotifier > s_pNotifier;
109 public:
110 static const ::rtl::Reference< ::comphelper::AsyncEventNotifier >&
111 getNotifier();
113 private:
114 SharedNotifier(); // never implemented
115 SharedNotifier( const SharedNotifier& ); // never implemented
116 SharedNotifier& operator=( const SharedNotifier& ); // never implemented
119 //------------------------------------------------------------------
120 ::rtl::Reference< ::comphelper::AsyncEventNotifier > SharedNotifier::s_pNotifier;
122 //------------------------------------------------------------------
123 ::osl::Mutex& SharedNotifier::getMutex()
125 static ::osl::Mutex s_aMutex;
126 return s_aMutex;
129 //------------------------------------------------------------------
130 const ::rtl::Reference< ::comphelper::AsyncEventNotifier >& SharedNotifier::getNotifier()
132 ::osl::MutexGuard aGuard( getMutex() );
133 if ( !s_pNotifier.is() )
135 s_pNotifier.set( new ::comphelper::AsyncEventNotifier );
136 s_pNotifier->create();
138 return s_pNotifier;
141 //==================================================================
142 //= PropertyControlContext_Impl
143 //==================================================================
144 /** implementation for of <type scope="com::sun::star::inspection">XPropertyControlContext</type>
145 which forwards all events to a non-UNO version of this interface
147 typedef ::cppu::WeakImplHelper1< XPropertyControlContext > PropertyControlContext_Impl_Base;
148 class PropertyControlContext_Impl :public PropertyControlContext_Impl_Base
149 ,public ::comphelper::IEventProcessor
151 public:
152 enum NotifcationMode
154 eSynchronously,
155 eAsynchronously
158 private:
159 IControlContext* m_pContext;
160 NotifcationMode m_eMode;
162 public:
163 /** creates an instance
164 @param _rContextImpl
165 the instance to delegate events to
167 PropertyControlContext_Impl( IControlContext& _rContextImpl );
169 /** disposes the context.
171 When you call this method, all subsequent callbacks to the
172 <type scope="com::sun::star::inspection">XPropertyControlContext</type> methods
173 will throw a <type scope="com::sun::star::lang">DisposedException</type>.
175 void SAL_CALL dispose();
177 /** sets the notification mode, so that notifications recieved from the controls are
178 forwarded to our IControlContext either synchronously or asynchronously
179 @param _eMode
180 the new notification mode
182 void setNotificationMode( NotifcationMode _eMode );
184 virtual void SAL_CALL acquire() throw();
185 virtual void SAL_CALL release() throw();
187 protected:
188 ~PropertyControlContext_Impl();
190 // XPropertyControlObserver
191 virtual void SAL_CALL focusGained( const Reference< XPropertyControl >& Control ) throw (RuntimeException);
192 virtual void SAL_CALL valueChanged( const Reference< XPropertyControl >& Control ) throw (RuntimeException);
193 // XPropertyControlContext
194 virtual void SAL_CALL activateNextControl( const Reference< XPropertyControl >& CurrentControl ) throw (RuntimeException);
196 // IEventProcessor
197 virtual void processEvent( const ::comphelper::AnyEvent& _rEvent );
199 private:
200 /** processes the given event, i.e. notifies it to our IControlContext
201 @param _rEvent
202 the event no notify
203 @precond
204 our mutex (well, the SolarMutex) is locked
206 void impl_processEvent_throw( const ::comphelper::AnyEvent& _rEvent );
208 /** checks whether we're alive
210 @throws DisposedException
211 if the instance is already disposed
213 void impl_checkAlive_throw() const;
215 /** checks whether the instance is already disposed
217 bool impl_isDisposed_nothrow() const { return m_pContext == NULL; }
219 /** notifies the given event originating from the given control
220 @throws DisposedException
221 @param _rxControl
222 @param _eType
224 void impl_notify_throw( const Reference< XPropertyControl >& _rxControl, ControlEventType _eType );
227 //--------------------------------------------------------------------
228 PropertyControlContext_Impl::PropertyControlContext_Impl( IControlContext& _rContextImpl )
229 :m_pContext( &_rContextImpl )
230 ,m_eMode( eAsynchronously )
234 //--------------------------------------------------------------------
235 PropertyControlContext_Impl::~PropertyControlContext_Impl()
237 if ( !impl_isDisposed_nothrow() )
238 dispose();
241 //--------------------------------------------------------------------
242 void PropertyControlContext_Impl::impl_checkAlive_throw() const
244 if ( impl_isDisposed_nothrow() )
245 throw DisposedException( ::rtl::OUString(), *const_cast< PropertyControlContext_Impl* >( this ) );
248 //--------------------------------------------------------------------
249 void SAL_CALL PropertyControlContext_Impl::dispose()
251 ::vos::OGuard aGuard( Application::GetSolarMutex() );
252 if ( impl_isDisposed_nothrow() )
253 return;
255 SharedNotifier::getNotifier()->removeEventsForProcessor( this );
256 m_pContext = NULL;
259 //--------------------------------------------------------------------
260 void PropertyControlContext_Impl::setNotificationMode( NotifcationMode _eMode )
262 ::vos::OGuard aGuard( Application::GetSolarMutex() );
263 m_eMode = _eMode;
266 //--------------------------------------------------------------------
267 void PropertyControlContext_Impl::impl_notify_throw( const Reference< XPropertyControl >& _rxControl, ControlEventType _eType )
269 ::comphelper::AnyEventRef pEvent;
272 ::vos::OGuard aGuard( Application::GetSolarMutex() );
273 impl_checkAlive_throw();
274 pEvent = new ControlEvent( _rxControl, _eType );
276 if ( m_eMode == eSynchronously )
278 impl_processEvent_throw( *pEvent );
279 return;
283 SharedNotifier::getNotifier()->addEvent( pEvent, this );
286 //--------------------------------------------------------------------
287 void SAL_CALL PropertyControlContext_Impl::focusGained( const Reference< XPropertyControl >& Control ) throw (RuntimeException)
289 DBG_TRACE( "PropertyControlContext_Impl: FOCUS_GAINED" );
290 impl_notify_throw( Control, FOCUS_GAINED );
293 //--------------------------------------------------------------------
294 void SAL_CALL PropertyControlContext_Impl::valueChanged( const Reference< XPropertyControl >& Control ) throw (RuntimeException)
296 DBG_TRACE( "PropertyControlContext_Impl: VALUE_CHANGED" );
297 impl_notify_throw( Control, VALUE_CHANGED );
300 //--------------------------------------------------------------------
301 void SAL_CALL PropertyControlContext_Impl::activateNextControl( const Reference< XPropertyControl >& CurrentControl ) throw (RuntimeException)
303 DBG_TRACE( "PropertyControlContext_Impl: ACTIVATE_NEXT" );
304 impl_notify_throw( CurrentControl, ACTIVATE_NEXT );
307 //--------------------------------------------------------------------
308 void SAL_CALL PropertyControlContext_Impl::acquire() throw()
310 PropertyControlContext_Impl_Base::acquire();
313 //--------------------------------------------------------------------
314 void SAL_CALL PropertyControlContext_Impl::release() throw()
316 PropertyControlContext_Impl_Base::release();
319 //--------------------------------------------------------------------
320 void PropertyControlContext_Impl::processEvent( const ::comphelper::AnyEvent& _rEvent )
322 ::vos::OGuard aGuard( Application::GetSolarMutex() );
323 if ( impl_isDisposed_nothrow() )
324 return;
328 impl_processEvent_throw( _rEvent );
330 catch( const Exception& )
332 // can't handle otherwise, since our caller (the notification thread) does not allow
333 // for exceptions (it could itself abort only)
334 DBG_UNHANDLED_EXCEPTION();
338 //--------------------------------------------------------------------
339 void PropertyControlContext_Impl::impl_processEvent_throw( const ::comphelper::AnyEvent& _rEvent )
341 const ControlEvent& rControlEvent = static_cast< const ControlEvent& >( _rEvent );
342 switch ( rControlEvent.eType )
344 case FOCUS_GAINED:
345 DBG_TRACE( "PropertyControlContext_Impl::processEvent: FOCUS_GAINED" );
346 m_pContext->focusGained( rControlEvent.xControl );
347 break;
348 case VALUE_CHANGED:
349 DBG_TRACE( "PropertyControlContext_Impl::processEvent: VALUE_CHANGED" );
350 m_pContext->valueChanged( rControlEvent.xControl );
351 break;
352 case ACTIVATE_NEXT:
353 DBG_TRACE( "PropertyControlContext_Impl::processEvent: ACTIVATE_NEXT" );
354 m_pContext->activateNextControl( rControlEvent.xControl );
355 break;
359 //==================================================================
360 //= OBrowserListBox
361 //==================================================================
362 DBG_NAME(OBrowserListBox)
363 //------------------------------------------------------------------
364 OBrowserListBox::OBrowserListBox( Window* pParent, WinBits nWinStyle)
365 :Control(pParent, nWinStyle| WB_CLIPCHILDREN)
366 ,m_aLinesPlayground(this,WB_DIALOGCONTROL | WB_CLIPCHILDREN)
367 ,m_aVScroll(this,WB_VSCROLL|WB_REPEAT|WB_DRAG)
368 ,m_pHelpWindow( new InspectorHelpWindow( this ) )
369 ,m_pLineListener(NULL)
370 ,m_pControlObserver( NULL )
371 ,m_nYOffset(0)
372 ,m_nCurrentPreferredHelpHeight(0)
373 ,m_nTheNameSize(0)
374 ,m_bIsActive(sal_False)
375 ,m_bUpdate(sal_True)
376 ,m_pControlContextImpl( new PropertyControlContext_Impl( *this ) )
378 DBG_CTOR(OBrowserListBox,NULL);
380 ListBox aListBox(this,WB_DROPDOWN);
381 aListBox.SetPosSizePixel(Point(0,0),Size(100,100));
382 m_nRowHeight = (sal_uInt16)aListBox.GetSizePixel().Height()+2;
383 SetBackground( pParent->GetBackground() );
384 m_aLinesPlayground.SetBackground( GetBackground() );
386 m_aLinesPlayground.SetPosPixel(Point(0,0));
387 m_aLinesPlayground.SetPaintTransparent(sal_True);
388 m_aLinesPlayground.Show();
389 m_aVScroll.Hide();
390 m_aVScroll.SetScrollHdl(LINK(this, OBrowserListBox, ScrollHdl));
393 //------------------------------------------------------------------
394 OBrowserListBox::~OBrowserListBox()
396 OSL_ENSURE( !IsModified(), "OBrowserListBox::~OBrowserListBox: still modified - should have been committed before!" );
397 // doing the commit here, while we, as well as our owner, as well as some other components,
398 // are already "half dead" (means within their dtor) is potentially dangerous.
399 // By definition, CommitModified has to be called (if necessary) before destruction
400 // #105868# - 2002-12-13 - fs@openoffice.org
402 m_pControlContextImpl->dispose();
403 m_pControlContextImpl.clear();
405 Hide();
406 Clear();
408 DBG_DTOR(OBrowserListBox,NULL);
411 //------------------------------------------------------------------
412 sal_Bool OBrowserListBox::IsModified( ) const
414 sal_Bool bModified = sal_False;
416 if ( m_bIsActive && m_xActiveControl.is() )
417 bModified = m_xActiveControl->isModified();
419 return bModified;
422 //------------------------------------------------------------------
423 void OBrowserListBox::CommitModified( )
425 if ( IsModified() && m_xActiveControl.is() )
427 // for the time of this commit, notify all events synchronously
428 // #i63814# / 2006-03-31 / frank.schoenheit@sun.com
429 m_pControlContextImpl->setNotificationMode( PropertyControlContext_Impl::eSynchronously );
432 m_xActiveControl->notifyModifiedValue();
434 catch( const Exception& )
436 DBG_UNHANDLED_EXCEPTION();
438 m_pControlContextImpl->setNotificationMode( PropertyControlContext_Impl::eAsynchronously );
442 //------------------------------------------------------------------
443 void OBrowserListBox::ActivateListBox(sal_Bool _bActive)
445 m_bIsActive = _bActive;
446 if (m_bIsActive)
448 // TODO: what's the sense of this?
449 m_aVScroll.SetThumbPos(100);
450 MoveThumbTo(0);
451 Resize();
455 //------------------------------------------------------------------
456 long OBrowserListBox::impl_getPrefererredHelpHeight()
458 return HasHelpSection() ? m_pHelpWindow->GetOptimalHeightPixel() : 0;
461 //------------------------------------------------------------------
462 void OBrowserListBox::Resize()
464 Rectangle aPlayground( Point( 0, 0 ), GetOutputSizePixel() );
465 Size aHelpWindowDistance( LogicToPixel( Size( 0, LAYOUT_HELP_WINDOW_DISTANCE_APPFONT ), MAP_APPFONT ) );
467 long nHelpWindowHeight = m_nCurrentPreferredHelpHeight = impl_getPrefererredHelpHeight();
468 bool bPositionHelpWindow = ( nHelpWindowHeight != 0 );
470 Rectangle aLinesArea( aPlayground );
471 if ( bPositionHelpWindow )
473 aLinesArea.Bottom() -= nHelpWindowHeight;
474 aLinesArea.Bottom() -= aHelpWindowDistance.Height();
476 m_aLinesPlayground.SetPosSizePixel( aLinesArea.TopLeft(), aLinesArea.GetSize() );
478 UpdateVScroll();
480 sal_Bool bNeedScrollbar = m_aOrderedLines.size() > (sal_uInt32)CalcVisibleLines();
481 if ( !bNeedScrollbar )
483 if ( m_aVScroll.IsVisible() )
484 m_aVScroll.Hide();
485 // scroll to top
486 m_nYOffset = 0;
487 m_aVScroll.SetThumbPos( 0 );
489 else
491 Size aVScrollSize( m_aVScroll.GetSizePixel() );
493 // adjust the playground's width
494 aLinesArea.Right() -= aVScrollSize.Width();
495 m_aLinesPlayground.SetPosSizePixel( aLinesArea.TopLeft(), aLinesArea.GetSize() );
497 // position the scrollbar
498 aVScrollSize.Height() = aLinesArea.GetHeight();
499 Point aVScrollPos( aLinesArea.GetWidth(), 0 );
500 m_aVScroll.SetPosSizePixel( aVScrollPos, aVScrollSize );
503 for ( sal_uInt16 i = 0; i < m_aOrderedLines.size(); ++i )
504 m_aOutOfDateLines.insert( i );
506 // repaint
507 EnablePaint(sal_False);
508 UpdatePlayGround();
509 EnablePaint(sal_True);
511 // show the scrollbar
512 if ( bNeedScrollbar )
513 m_aVScroll.Show();
515 // position the help window
516 if ( bPositionHelpWindow )
518 Rectangle aHelpArea( aPlayground );
519 aHelpArea.Top() = aLinesArea.Bottom() + aHelpWindowDistance.Height();
520 m_pHelpWindow->SetPosSizePixel( aHelpArea.TopLeft(), aHelpArea.GetSize() );
524 //------------------------------------------------------------------
525 void OBrowserListBox::SetListener( IPropertyLineListener* _pListener )
527 m_pLineListener = _pListener;
530 //------------------------------------------------------------------
531 void OBrowserListBox::SetObserver( IPropertyControlObserver* _pObserver )
533 m_pControlObserver = _pObserver;
536 //------------------------------------------------------------------
537 void OBrowserListBox::EnableHelpSection( bool _bEnable )
539 m_pHelpWindow->Show( _bEnable );
540 Resize();
543 //------------------------------------------------------------------
544 bool OBrowserListBox::HasHelpSection() const
546 return m_pHelpWindow->IsVisible();
549 //------------------------------------------------------------------
550 void OBrowserListBox::SetHelpText( const ::rtl::OUString& _rHelpText )
552 OSL_ENSURE( HasHelpSection(), "OBrowserListBox::SetHelpText: help section not visible!" );
553 m_pHelpWindow->SetText( _rHelpText );
554 if ( m_nCurrentPreferredHelpHeight != impl_getPrefererredHelpHeight() )
555 Resize();
558 //------------------------------------------------------------------
559 void OBrowserListBox::SetHelpLineLimites( sal_Int32 _nMinLines, sal_Int32 _nMaxLines )
561 m_pHelpWindow->SetLimits( _nMinLines, _nMaxLines );
564 //------------------------------------------------------------------
565 sal_uInt16 OBrowserListBox::CalcVisibleLines()
567 Size aSize(m_aLinesPlayground.GetOutputSizePixel());
568 sal_uInt16 nResult = 0;
569 if (0 != m_nRowHeight)
570 nResult = (sal_uInt16) aSize.Height()/m_nRowHeight;
572 return nResult;
575 //------------------------------------------------------------------
576 void OBrowserListBox::UpdateVScroll()
578 sal_uInt16 nLines = CalcVisibleLines();
579 m_aVScroll.SetPageSize(nLines-1);
580 m_aVScroll.SetVisibleSize(nLines-1);
582 size_t nCount = m_aLines.size();
583 if (nCount>0)
585 m_aVScroll.SetRange(Range(0,nCount-1));
586 m_nYOffset = -m_aVScroll.GetThumbPos()*m_nRowHeight;
588 else
590 m_aVScroll.SetRange(Range(0,0));
591 m_nYOffset = 0;
595 //------------------------------------------------------------------
596 void OBrowserListBox::PositionLine( sal_uInt16 _nIndex )
598 Size aSize(m_aLinesPlayground.GetOutputSizePixel());
599 Point aPos(0, m_nYOffset);
601 aSize.Height() = m_nRowHeight;
603 aPos.Y() += _nIndex * m_nRowHeight;
605 if ( _nIndex < m_aOrderedLines.size() )
607 m_aOrderedLines[ _nIndex ]->second.pLine->SetPosSizePixel( aPos, aSize );
609 m_aOrderedLines[ _nIndex ]->second.pLine->SetTitleWidth( m_nTheNameSize + 2 * FRAME_OFFSET );
611 // show the line if necessary
612 if ( !m_aOrderedLines[ _nIndex ]->second.pLine->IsVisible() )
613 m_aOrderedLines[ _nIndex ]->second.pLine->Show();
617 //------------------------------------------------------------------
618 void OBrowserListBox::UpdatePosNSize()
620 for ( ::std::set< sal_uInt16 >::const_iterator aLoop = m_aOutOfDateLines.begin();
621 aLoop != m_aOutOfDateLines.end();
622 ++aLoop
625 DBG_ASSERT( *aLoop < m_aOrderedLines.size(), "OBrowserListBox::UpdatePosNSize: invalid line index!" );
626 if ( *aLoop < m_aOrderedLines.size() )
627 PositionLine( *aLoop );
629 m_aOutOfDateLines.clear();
632 //------------------------------------------------------------------
633 void OBrowserListBox::UpdatePlayGround()
635 sal_Int32 nThumbPos = m_aVScroll.GetThumbPos();
636 sal_Int32 nLines = CalcVisibleLines();
638 sal_uInt16 nEnd = (sal_uInt16)(nThumbPos + nLines);
639 if (nEnd >= m_aOrderedLines.size())
640 nEnd = (sal_uInt16)m_aOrderedLines.size()-1;
642 if ( !m_aOrderedLines.empty() )
644 for ( sal_uInt16 i = (sal_uInt16)nThumbPos; i <= nEnd; ++i )
645 m_aOutOfDateLines.insert( i );
646 UpdatePosNSize();
650 //------------------------------------------------------------------
651 void OBrowserListBox::UpdateAll()
653 Resize();
656 //------------------------------------------------------------------
657 void OBrowserListBox::DisableUpdate()
659 m_bUpdate = sal_False;
662 //------------------------------------------------------------------
663 void OBrowserListBox::EnableUpdate()
665 m_bUpdate = sal_True;
666 UpdateAll();
669 //------------------------------------------------------------------
670 void OBrowserListBox::SetPropertyValue(const ::rtl::OUString& _rEntryName, const Any& _rValue, bool _bUnknownValue )
672 ListBoxLines::iterator line = m_aLines.find( _rEntryName );
673 if ( line != m_aLines.end() )
675 if ( _bUnknownValue )
677 Reference< XPropertyControl > xControl( line->second.pLine->getControl() );
678 OSL_ENSURE( xControl.is(), "OBrowserListBox::SetPropertyValue: illegal control!" );
679 if ( xControl.is() )
680 xControl->setValue( Any() );
682 else
683 impl_setControlAsPropertyValue( line->second, _rValue );
687 //------------------------------------------------------------------------
688 Any OBrowserListBox::GetPropertyValue( const ::rtl::OUString& _rEntryName ) const
690 Any aValue;
691 ListBoxLines::const_iterator line = m_aLines.find( _rEntryName );
692 if ( line != m_aLines.end() )
693 aValue = impl_getControlAsPropertyValue( line->second );
694 return aValue;
697 //------------------------------------------------------------------------
698 sal_uInt16 OBrowserListBox::GetPropertyPos( const ::rtl::OUString& _rEntryName ) const
700 sal_uInt16 nRet = LISTBOX_ENTRY_NOTFOUND;
701 for ( OrderedListBoxLines::const_iterator linePos = m_aOrderedLines.begin();
702 linePos != m_aOrderedLines.end();
703 ++linePos
706 if ( (*linePos)->first == _rEntryName )
708 nRet = (sal_uInt16)( linePos - m_aOrderedLines.begin() );
709 break;
713 return nRet;
716 //------------------------------------------------------------------------
717 bool OBrowserListBox::impl_getBrowserLineForName( const ::rtl::OUString& _rEntryName, BrowserLinePointer& _out_rpLine ) const
719 ListBoxLines::const_iterator line = m_aLines.find( _rEntryName );
720 if ( line != m_aLines.end() )
721 _out_rpLine = line->second.pLine;
722 else
723 _out_rpLine.reset();
724 return ( NULL != _out_rpLine.get() );
727 //------------------------------------------------------------------------
728 sal_Bool OBrowserListBox::IsPropertyInputEnabled( const ::rtl::OUString& _rEntryName ) const
730 BrowserLinePointer pLine;
731 if ( impl_getBrowserLineForName( _rEntryName, pLine ) )
732 return pLine->IsPropertyInputEnabled();
733 return sal_False;
736 //------------------------------------------------------------------------
737 void OBrowserListBox::EnablePropertyControls( const ::rtl::OUString& _rEntryName, sal_Int16 _nControls, bool _bEnable )
739 BrowserLinePointer pLine;
740 if ( impl_getBrowserLineForName( _rEntryName, pLine ) )
741 pLine->EnablePropertyControls( _nControls, _bEnable );
744 //------------------------------------------------------------------------
745 void OBrowserListBox::EnablePropertyLine( const ::rtl::OUString& _rEntryName, bool _bEnable )
747 BrowserLinePointer pLine;
748 if ( impl_getBrowserLineForName( _rEntryName, pLine ) )
749 pLine->EnablePropertyLine( _bEnable );
752 //------------------------------------------------------------------------
753 Reference< XPropertyControl > OBrowserListBox::GetPropertyControl( const ::rtl::OUString& _rEntryName )
755 BrowserLinePointer pLine;
756 if ( impl_getBrowserLineForName( _rEntryName, pLine ) )
757 return pLine->getControl();
758 return NULL;
761 //------------------------------------------------------------------
762 sal_uInt16 OBrowserListBox::InsertEntry(const OLineDescriptor& _rPropertyData, sal_uInt16 _nPos)
764 // create a new line
765 BrowserLinePointer pBrowserLine( new OBrowserLine( _rPropertyData.sName, &m_aLinesPlayground ) );
767 ListBoxLine aNewLine( pBrowserLine, _rPropertyData.xPropertyHandler );
768 ::std::pair< ListBoxLines::iterator, bool > insertPoint =
769 m_aLines.insert( ListBoxLines::value_type( _rPropertyData.sName, aNewLine ) );
770 OSL_ENSURE( insertPoint.second, "OBrowserListBox::InsertEntry: already have another line for this name!" );
772 sal_uInt16 nInsertPos = _nPos;
773 if ( nInsertPos > m_aOrderedLines.size() )
774 nInsertPos = EDITOR_LIST_APPEND;
775 if ( EDITOR_LIST_APPEND == nInsertPos )
777 nInsertPos = (sal_uInt16)m_aOrderedLines.size();
778 m_aOrderedLines.push_back( insertPoint.first );
780 else
781 m_aOrderedLines.insert( m_aOrderedLines.begin() + nInsertPos, insertPoint.first );
783 pBrowserLine->SetTitleWidth(m_nTheNameSize);
784 if (m_bUpdate)
786 UpdateVScroll();
787 Invalidate();
790 // initialize the entry
791 ChangeEntry(_rPropertyData, nInsertPos);
793 // update the positions of possibly affected lines
794 sal_uInt16 nUpdatePos = nInsertPos;
795 while ( nUpdatePos < m_aOrderedLines.size() )
796 m_aOutOfDateLines.insert( nUpdatePos++ );
797 UpdatePosNSize( );
799 return nInsertPos;
802 //------------------------------------------------------------------
803 sal_Int32 OBrowserListBox::GetMinimumWidth()
805 return m_nTheNameSize + 2 * FRAME_OFFSET + (m_nRowHeight - 4) * 8;
808 //------------------------------------------------------------------
809 sal_Int32 OBrowserListBox::GetMinimumHeight()
811 // assume that we want to display 5 rows, at least
812 sal_Int32 nMinHeight = m_nRowHeight * 5;
814 if ( HasHelpSection() )
816 Size aHelpWindowDistance( LogicToPixel( Size( 0, LAYOUT_HELP_WINDOW_DISTANCE_APPFONT ), MAP_APPFONT ) );
817 nMinHeight += aHelpWindowDistance.Height();
819 nMinHeight += m_pHelpWindow->GetMinimalHeightPixel();
822 return nMinHeight;
825 //------------------------------------------------------------------
826 void OBrowserListBox::ShowEntry(sal_uInt16 _nPos)
828 if ( _nPos < m_aOrderedLines.size() )
830 sal_Int32 nThumbPos = m_aVScroll.GetThumbPos();
832 if (_nPos < nThumbPos)
833 MoveThumbTo(_nPos);
834 else
836 sal_Int32 nLines = CalcVisibleLines();
837 if (_nPos >= nThumbPos + nLines)
838 MoveThumbTo(_nPos - nLines + 1);
844 //------------------------------------------------------------------
845 void OBrowserListBox::MoveThumbTo(sal_Int32 _nNewThumbPos)
847 // disable painting to prevent flicker
848 m_aLinesPlayground.EnablePaint(sal_False);
850 sal_Int32 nDelta = _nNewThumbPos - m_aVScroll.GetThumbPos();
851 // adjust the scrollbar
852 m_aVScroll.SetThumbPos(_nNewThumbPos);
853 sal_Int32 nThumbPos = _nNewThumbPos;
855 m_nYOffset = -m_aVScroll.GetThumbPos() * m_nRowHeight;
857 sal_Int32 nLines = CalcVisibleLines();
858 sal_uInt16 nEnd = (sal_uInt16)(nThumbPos + nLines);
860 m_aLinesPlayground.Scroll(0, -nDelta * m_nRowHeight, SCROLL_CHILDREN);
862 if (1 == nDelta)
864 // TODO: what's the sense of this two PositionLines? Why not just one call?
865 PositionLine(nEnd-1);
866 PositionLine(nEnd);
868 else if (-1 == nDelta)
870 PositionLine((sal_uInt16)nThumbPos);
872 else if (0 != nDelta)
874 UpdatePlayGround();
877 m_aLinesPlayground.EnablePaint(sal_True);
878 m_aLinesPlayground.Invalidate(INVALIDATE_CHILDREN);
881 //------------------------------------------------------------------
882 IMPL_LINK(OBrowserListBox, ScrollHdl, ScrollBar*, _pScrollBar )
884 DBG_ASSERT(_pScrollBar == &m_aVScroll, "OBrowserListBox::ScrollHdl: where does this come from?");
885 (void)_pScrollBar;
887 // disable painting to prevent flicker
888 m_aLinesPlayground.EnablePaint(sal_False);
890 sal_Int32 nThumbPos = m_aVScroll.GetThumbPos();
892 sal_Int32 nDelta = m_aVScroll.GetDelta();
893 m_nYOffset = -nThumbPos * m_nRowHeight;
895 sal_uInt16 nEnd = (sal_uInt16)(nThumbPos + CalcVisibleLines());
897 m_aLinesPlayground.Scroll(0, -nDelta * m_nRowHeight, SCROLL_CHILDREN);
899 if (1 == nDelta)
901 PositionLine(nEnd-1);
902 PositionLine(nEnd);
904 else if (nDelta==-1)
906 PositionLine((sal_uInt16)nThumbPos);
908 else if (nDelta!=0 || m_aVScroll.GetType() == SCROLL_DONTKNOW)
910 UpdatePlayGround();
913 m_aLinesPlayground.EnablePaint(sal_True);
914 return 0;
917 //------------------------------------------------------------------
918 void OBrowserListBox::buttonClicked( OBrowserLine* _pLine, sal_Bool _bPrimary )
920 DBG_ASSERT( _pLine, "OBrowserListBox::buttonClicked: invalid browser line!" );
921 if ( _pLine && m_pLineListener )
923 m_pLineListener->Clicked( _pLine->GetEntryName(), _bPrimary );
927 //------------------------------------------------------------------
928 void OBrowserListBox::impl_setControlAsPropertyValue( const ListBoxLine& _rLine, const Any& _rPropertyValue )
930 Reference< XPropertyControl > xControl( _rLine.pLine->getControl() );
933 if ( _rPropertyValue.getValueType().equals( _rLine.pLine->getControl()->getValueType() ) )
935 xControl->setValue( _rPropertyValue );
937 else
939 #ifdef DBG_UTIL
940 if ( !_rLine.xHandler.is() )
942 ::rtl::OString sMessage( "OBrowserListBox::impl_setControlAsPropertyValue: no handler -> no conversion (property: '" );
943 ::rtl::OUString sPropertyName( _rLine.pLine->GetEntryName() );
944 sMessage += ::rtl::OString( sPropertyName.getStr(), sPropertyName.getLength(), RTL_TEXTENCODING_ASCII_US );
945 sMessage += ::rtl::OString( "')!" );
946 DBG_ERROR( sMessage );
948 #endif
949 if ( _rLine.xHandler.is() )
951 Any aControlValue = _rLine.xHandler->convertToControlValue(
952 _rLine.pLine->GetEntryName(), _rPropertyValue, xControl->getValueType() );
953 xControl->setValue( aControlValue );
957 catch( const Exception& )
959 DBG_UNHANDLED_EXCEPTION();
963 //------------------------------------------------------------------
964 Any OBrowserListBox::impl_getControlAsPropertyValue( const ListBoxLine& _rLine ) const
966 Reference< XPropertyControl > xControl( _rLine.pLine->getControl() );
967 Any aPropertyValue;
970 #ifdef DBG_UTIL
971 if ( !_rLine.xHandler.is() )
973 ::rtl::OString sMessage( "OBrowserListBox::impl_getControlAsPropertyValue: no handler -> no conversion (property: '" );
974 ::rtl::OUString sPropertyName( _rLine.pLine->GetEntryName() );
975 sMessage += ::rtl::OString( sPropertyName.getStr(), sPropertyName.getLength(), RTL_TEXTENCODING_ASCII_US );
976 sMessage += ::rtl::OString( "')!" );
977 DBG_ERROR( sMessage );
979 #endif
980 if ( _rLine.xHandler.is() )
981 aPropertyValue = _rLine.xHandler->convertToPropertyValue( _rLine.pLine->GetEntryName(), xControl->getValue() );
982 else
983 aPropertyValue = xControl->getValue();
985 catch( const Exception& )
987 DBG_UNHANDLED_EXCEPTION();
989 return aPropertyValue;
992 //------------------------------------------------------------------
993 sal_uInt16 OBrowserListBox::impl_getControlPos( const Reference< XPropertyControl >& _rxControl ) const
995 for ( OrderedListBoxLines::const_iterator search = m_aOrderedLines.begin();
996 search != m_aOrderedLines.end();
997 ++search
999 if ( (*search)->second.pLine->getControl().get() == _rxControl.get() )
1000 return sal_uInt16( search - m_aOrderedLines.begin() );
1001 DBG_ERROR( "OBrowserListBox::impl_getControlPos: invalid control - not part of any of our lines!" );
1002 return (sal_uInt16)-1;
1005 //--------------------------------------------------------------------
1006 void SAL_CALL OBrowserListBox::focusGained( const Reference< XPropertyControl >& _rxControl ) throw (RuntimeException)
1008 DBG_TESTSOLARMUTEX();
1010 DBG_ASSERT( _rxControl.is(), "OBrowserListBox::focusGained: invalid event source!" );
1011 if ( !_rxControl.is() )
1012 return;
1014 if ( m_pControlObserver )
1015 m_pControlObserver->focusGained( _rxControl );
1017 m_xActiveControl = _rxControl;
1018 ShowEntry( impl_getControlPos( m_xActiveControl ) );
1021 //--------------------------------------------------------------------
1022 void SAL_CALL OBrowserListBox::valueChanged( const Reference< XPropertyControl >& _rxControl ) throw (RuntimeException)
1024 DBG_TESTSOLARMUTEX();
1026 DBG_ASSERT( _rxControl.is(), "OBrowserListBox::valueChanged: invalid event source!" );
1027 if ( !_rxControl.is() )
1028 return;
1030 if ( m_pControlObserver )
1031 m_pControlObserver->valueChanged( _rxControl );
1033 if ( m_pLineListener )
1035 const ListBoxLine& rLine = impl_getControlLine( _rxControl );
1036 m_pLineListener->Commit(
1037 rLine.pLine->GetEntryName(),
1038 impl_getControlAsPropertyValue( rLine )
1043 //--------------------------------------------------------------------
1044 void SAL_CALL OBrowserListBox::activateNextControl( const Reference< XPropertyControl >& _rxCurrentControl ) throw (RuntimeException)
1046 DBG_TESTSOLARMUTEX();
1048 sal_uInt16 nLine = impl_getControlPos( _rxCurrentControl );
1050 // cycle forwards, 'til we've the next control which can grab the focus
1051 ++nLine;
1052 while ( (size_t)nLine < m_aOrderedLines.size() )
1054 if ( m_aOrderedLines[nLine]->second.pLine->GrabFocus() )
1055 break;
1056 ++nLine;
1059 if ( ( (size_t)nLine >= m_aOrderedLines.size() )
1060 && ( m_aOrderedLines.size() > 0 )
1062 // wrap around
1063 m_aOrderedLines[0]->second.pLine->GrabFocus();
1066 //------------------------------------------------------------------
1067 namespace
1069 //..............................................................
1070 void lcl_implDisposeControl_nothrow( const Reference< XPropertyControl >& _rxControl )
1072 if ( !_rxControl.is() )
1073 return;
1076 _rxControl->setControlContext( NULL );
1077 Reference< XComponent > xControlComponent( _rxControl, UNO_QUERY );
1078 if ( xControlComponent.is() )
1079 xControlComponent->dispose();
1081 catch( const Exception& )
1083 DBG_UNHANDLED_EXCEPTION();
1088 //------------------------------------------------------------------
1089 void OBrowserListBox::Clear()
1091 for ( ListBoxLines::iterator loop = m_aLines.begin();
1092 loop != m_aLines.end();
1093 ++loop
1096 // hide the line
1097 loop->second.pLine->Hide();
1098 // reset the listener
1099 lcl_implDisposeControl_nothrow( loop->second.pLine->getControl() );
1102 clearContainer( m_aLines );
1103 clearContainer( m_aOrderedLines );
1106 //------------------------------------------------------------------
1107 sal_Bool OBrowserListBox::RemoveEntry( const ::rtl::OUString& _rName )
1109 sal_uInt16 nPos = GetPropertyPos( _rName );
1110 if ( nPos == LISTBOX_ENTRY_NOTFOUND )
1111 return sal_False;
1113 OrderedListBoxLines::iterator orderedPos = m_aOrderedLines.begin() + nPos;
1114 BrowserLinePointer pLine = (*orderedPos)->second.pLine;
1115 pLine->Hide();
1116 lcl_implDisposeControl_nothrow( pLine->getControl() );
1118 m_aLines.erase( *orderedPos );
1119 m_aOrderedLines.erase( orderedPos );
1120 m_aOutOfDateLines.erase( (sal_uInt16)m_aOrderedLines.size() );
1121 // this index *may* have been out of date, which is obsoleted now by m_aOrderedLines shrinking
1123 // update the positions of possibly affected lines
1124 while ( nPos < m_aOrderedLines.size() )
1125 m_aOutOfDateLines.insert( nPos++ );
1126 UpdatePosNSize( );
1128 return sal_True;
1131 //------------------------------------------------------------------
1132 void OBrowserListBox::ChangeEntry( const OLineDescriptor& _rPropertyData, sal_uInt16 nPos )
1134 OSL_PRECOND( _rPropertyData.Control.is(), "OBrowserListBox::ChangeEntry: invalid control!" );
1135 if ( !_rPropertyData.Control.is() )
1136 return;
1138 if ( nPos == EDITOR_LIST_REPLACE_EXISTING )
1139 nPos = GetPropertyPos( _rPropertyData.sName );
1141 if ( nPos < m_aOrderedLines.size() )
1143 Window* pRefWindow = NULL;
1144 if ( nPos > 0 )
1145 pRefWindow = m_aOrderedLines[nPos-1]->second.pLine->GetRefWindow();
1147 // the current line and control
1148 ListBoxLine& rLine = m_aOrderedLines[nPos]->second;
1150 // the old control and some data about it
1151 Reference< XPropertyControl > xControl = rLine.pLine->getControl();
1152 Window* pControlWindow = rLine.pLine->getControlWindow();
1153 Point aControlPos;
1154 if ( pControlWindow )
1155 aControlPos = pControlWindow->GetPosPixel();
1157 // clean up the old control
1158 lcl_implDisposeControl_nothrow( xControl );
1160 // set the new control at the line
1161 rLine.pLine->setControl( _rPropertyData.Control );
1162 xControl = rLine.pLine->getControl();
1164 if ( xControl.is() )
1165 xControl->setControlContext( m_pControlContextImpl.get() );
1167 // the initial property value
1168 if ( _rPropertyData.bUnknownValue )
1169 xControl->setValue( Any() );
1170 else
1171 impl_setControlAsPropertyValue( rLine, _rPropertyData.aValue );
1173 rLine.pLine->SetTitle(_rPropertyData.DisplayName);
1174 rLine.xHandler = _rPropertyData.xPropertyHandler;
1176 sal_uInt16 nTextWidth = (sal_uInt16)m_aLinesPlayground.GetTextWidth(_rPropertyData.DisplayName);
1177 if (m_nTheNameSize< nTextWidth)
1178 m_nTheNameSize = nTextWidth;
1180 if ( _rPropertyData.HasPrimaryButton )
1182 if ( _rPropertyData.PrimaryButtonImageURL.getLength() )
1183 rLine.pLine->ShowBrowseButton( _rPropertyData.PrimaryButtonImageURL, true );
1184 else if ( _rPropertyData.PrimaryButtonImage.is() )
1185 rLine.pLine->ShowBrowseButton( Image( _rPropertyData.PrimaryButtonImage ), true );
1186 else
1187 rLine.pLine->ShowBrowseButton( true );
1189 if ( _rPropertyData.HasSecondaryButton )
1191 if ( _rPropertyData.SecondaryButtonImageURL.getLength() )
1192 rLine.pLine->ShowBrowseButton( _rPropertyData.SecondaryButtonImageURL, false );
1193 else if ( _rPropertyData.SecondaryButtonImage.is() )
1194 rLine.pLine->ShowBrowseButton( Image( _rPropertyData.SecondaryButtonImage ), false );
1195 else
1196 rLine.pLine->ShowBrowseButton( false );
1198 else
1199 rLine.pLine->HideBrowseButton( false );
1201 rLine.pLine->SetClickListener( this );
1203 else
1205 rLine.pLine->HideBrowseButton( true );
1206 rLine.pLine->HideBrowseButton( false );
1209 DBG_ASSERT( ( _rPropertyData.IndentLevel == 0 ) || ( _rPropertyData.IndentLevel == 1 ),
1210 "OBrowserListBox::ChangeEntry: unsupported indent level!" );
1211 rLine.pLine->IndentTitle( _rPropertyData.IndentLevel > 0 );
1213 if ( nPos > 0 )
1214 rLine.pLine->SetTabOrder( pRefWindow, WINDOW_ZORDER_BEHIND );
1215 else
1216 rLine.pLine->SetTabOrder( pRefWindow, WINDOW_ZORDER_FIRST );
1218 m_aOutOfDateLines.insert( nPos );
1219 rLine.pLine->SetComponentHelpIds(
1220 HelpIdUrl::getHelpId( _rPropertyData.HelpURL ),
1221 _rPropertyData.PrimaryButtonId,
1222 _rPropertyData.SecondaryButtonId
1225 if ( _rPropertyData.bReadOnly )
1227 rLine.pLine->SetReadOnly( true );
1229 // user controls (i.e. the ones not provided by the usual
1230 // XPropertyControlFactory) have no chance to know that they should be read-only,
1231 // since XPropertyHandler::describePropertyLine does not transport this
1232 // information.
1233 // So, we manually switch this to read-only.
1234 if ( xControl.is() && ( xControl->getControlType() == PropertyControlType::Unknown ) )
1236 Edit* pControlWindowAsEdit = dynamic_cast< Edit* >( rLine.pLine->getControlWindow() );
1237 if ( pControlWindowAsEdit )
1238 pControlWindowAsEdit->SetReadOnly( TRUE );
1239 else
1240 pControlWindowAsEdit->Enable( FALSE );
1246 //------------------------------------------------------------------
1247 long OBrowserListBox::PreNotify( NotifyEvent& _rNEvt )
1249 switch ( _rNEvt.GetType() )
1251 case EVENT_KEYINPUT:
1253 const KeyEvent* pKeyEvent = _rNEvt.GetKeyEvent();
1254 if ( ( pKeyEvent->GetKeyCode().GetModifier() != 0 )
1255 || ( ( pKeyEvent->GetKeyCode().GetCode() != KEY_PAGEUP )
1256 && ( pKeyEvent->GetKeyCode().GetCode() != KEY_PAGEDOWN )
1259 break;
1261 long nScrollOffset = 0;
1262 if ( m_aVScroll.IsVisible() )
1264 if ( pKeyEvent->GetKeyCode().GetCode() == KEY_PAGEUP )
1265 nScrollOffset = -m_aVScroll.GetPageSize();
1266 else if ( pKeyEvent->GetKeyCode().GetCode() == KEY_PAGEDOWN )
1267 nScrollOffset = m_aVScroll.GetPageSize();
1270 if ( nScrollOffset )
1272 long nNewThumbPos = m_aVScroll.GetThumbPos() + nScrollOffset;
1273 nNewThumbPos = ::std::max( nNewThumbPos, m_aVScroll.GetRangeMin() );
1274 nNewThumbPos = ::std::min( nNewThumbPos, m_aVScroll.GetRangeMax() );
1275 m_aVScroll.DoScroll( nNewThumbPos );
1276 nNewThumbPos = m_aVScroll.GetThumbPos();
1278 sal_uInt16 nFocusControlPos = 0;
1279 sal_uInt16 nActiveControlPos = impl_getControlPos( m_xActiveControl );
1280 if ( nActiveControlPos < nNewThumbPos )
1281 nFocusControlPos = (sal_uInt16)nNewThumbPos;
1282 else if ( nActiveControlPos >= nNewThumbPos + CalcVisibleLines() )
1283 nFocusControlPos = (sal_uInt16)nNewThumbPos + CalcVisibleLines() - 1;
1284 if ( nFocusControlPos )
1286 if ( nFocusControlPos < m_aOrderedLines.size() )
1288 m_aOrderedLines[ nFocusControlPos ]->second.pLine->GrabFocus();
1290 else
1291 OSL_ENSURE( false, "OBrowserListBox::PreNotify: internal error, invalid focus control position!" );
1295 return 1L;
1296 // handled this. In particular, we also consume PageUp/Down events if we do not use them for scrolling,
1297 // otherwise they would be used to scroll the document view, which does not sound like it is desired by
1298 // the user.
1301 return Control::PreNotify( _rNEvt );
1304 //------------------------------------------------------------------
1305 long OBrowserListBox::Notify( NotifyEvent& _rNEvt )
1307 switch ( _rNEvt.GetType() )
1309 case EVENT_COMMAND:
1311 const CommandEvent* pCommand = _rNEvt.GetCommandEvent();
1312 if ( ( COMMAND_WHEEL == pCommand->GetCommand() )
1313 || ( COMMAND_STARTAUTOSCROLL == pCommand->GetCommand() )
1314 || ( COMMAND_AUTOSCROLL == pCommand->GetCommand() )
1317 // interested in scroll events if we have a scrollbar
1318 if ( m_aVScroll.IsVisible() )
1320 HandleScrollCommand( *pCommand, NULL, &m_aVScroll );
1324 break;
1327 return Control::Notify( _rNEvt );
1330 //............................................................................
1331 } // namespace pcr
1332 //............................................................................