merge the formfield patch from ooo-build
[ooovba.git] / extensions / source / propctrlr / listselectiondlg.cxx
blobe46156b74c356641c82f86b02df58b1b3f04bf6d
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: listselectiondlg.cxx,v $
10 * $Revision: 1.7 $
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 "listselectiondlg.hxx"
34 #ifndef EXTENSIONS_SOURCE_PROPCTRLR_LISTSELECTIONDLG_HRC
35 #include "listselectiondlg.hrc"
36 #endif
38 #ifndef _EXTENSIONS_PROPCTRLR_MODULEPRC_HXX_
39 #include "modulepcr.hxx"
40 #endif
41 #ifndef _EXTENSIONS_FORMCTRLR_PROPRESID_HRC_
42 #include "formresid.hrc"
43 #endif
44 #include "formstrings.hxx"
45 #include <vcl/msgbox.hxx>
47 /** === begin UNO includes === **/
48 /** === end UNO includes === **/
50 //........................................................................
51 namespace pcr
53 //........................................................................
55 using namespace ::com::sun::star::uno;
56 using namespace ::com::sun::star::beans;
58 //====================================================================
59 //= ListSelectionDialog
60 //====================================================================
61 //--------------------------------------------------------------------
62 ListSelectionDialog::ListSelectionDialog( Window* _pParent, const Reference< XPropertySet >& _rxListBox,
63 const ::rtl::OUString& _rPropertyName, const String& _rPropertyUIName )
64 :ModalDialog( _pParent, PcrRes( RID_DLG_SELECTION ) )
65 ,m_aLabel ( this, PcrRes( FT_ENTRIES ) )
66 ,m_aEntries ( this, PcrRes( LB_ENTRIES ) )
67 ,m_aOK ( this, PcrRes( PB_OK ) )
68 ,m_aCancel ( this, PcrRes( PB_CANCEL ) )
69 ,m_aHelp ( this, PcrRes( PB_HELP ) )
70 ,m_xListBox ( _rxListBox )
71 ,m_sPropertyName( _rPropertyName )
73 FreeResource();
75 OSL_PRECOND( m_xListBox.is(), "ListSelectionDialog::ListSelectionDialog: invalid list box!" );
77 SetText( _rPropertyUIName );
78 m_aLabel.SetText( _rPropertyUIName );
80 initialize( );
83 //------------------------------------------------------------------------
84 short ListSelectionDialog::Execute()
86 short nResult = ModalDialog::Execute();
88 if ( RET_OK == nResult )
89 commitSelection();
91 return nResult;
94 //--------------------------------------------------------------------
95 void ListSelectionDialog::initialize( )
97 if ( !m_xListBox.is() )
98 return;
100 m_aEntries.SetStyle( GetStyle() | WB_SIMPLEMODE );
104 // initialize the multi-selection flag
105 sal_Bool bMultiSelection = sal_False;
106 OSL_VERIFY( m_xListBox->getPropertyValue( PROPERTY_MULTISELECTION ) >>= bMultiSelection );
107 m_aEntries.EnableMultiSelection( bMultiSelection );
109 // fill the list box with all entries
110 Sequence< ::rtl::OUString > aListEntries;
111 OSL_VERIFY( m_xListBox->getPropertyValue( PROPERTY_STRINGITEMLIST ) >>= aListEntries );
112 fillEntryList( aListEntries );
114 // select entries according to the property
115 Sequence< sal_Int16 > aSelection;
116 OSL_VERIFY( m_xListBox->getPropertyValue( m_sPropertyName ) >>= aSelection );
117 selectEntries( aSelection );
119 catch( const Exception& )
121 OSL_ENSURE( sal_False, "ListSelectionDialog::initialize: caught an exception!" );
125 //--------------------------------------------------------------------
126 void ListSelectionDialog::commitSelection()
128 if ( !m_xListBox.is() )
129 return;
131 Sequence< sal_Int16 > aSelection;
132 collectSelection( aSelection );
136 m_xListBox->setPropertyValue( m_sPropertyName, makeAny( aSelection ) );
138 catch( const Exception& )
140 OSL_ENSURE( sal_False, "ListSelectionDialog::commitSelection: caught an exception!" );
144 //--------------------------------------------------------------------
145 void ListSelectionDialog::fillEntryList( const Sequence< ::rtl::OUString >& _rListEntries )
147 m_aEntries.Clear();
148 const ::rtl::OUString* _pListEntries = _rListEntries.getConstArray();
149 const ::rtl::OUString* _pListEntriesEnd = _rListEntries.getConstArray() + _rListEntries.getLength();
150 for ( ; _pListEntries < _pListEntriesEnd; ++_pListEntries )
151 m_aEntries.InsertEntry( *_pListEntries );
154 //--------------------------------------------------------------------
155 void ListSelectionDialog::collectSelection( Sequence< sal_Int16 >& /* [out] */ _rSelection )
157 sal_uInt16 nSelectedCount = m_aEntries.GetSelectEntryCount( );
158 _rSelection.realloc( nSelectedCount );
159 sal_Int16* pSelection = _rSelection.getArray();
160 for ( sal_uInt16 selected = 0; selected < nSelectedCount; ++selected, ++pSelection )
161 *pSelection = static_cast< sal_Int16 >( m_aEntries.GetSelectEntryPos( selected ) );
164 //--------------------------------------------------------------------
165 void ListSelectionDialog::selectEntries( const Sequence< sal_Int16 >& /* [in ] */ _rSelection )
167 m_aEntries.SetNoSelection();
168 const sal_Int16* pSelection = _rSelection.getConstArray();
169 const sal_Int16* pSelectionEnd = _rSelection.getConstArray() + _rSelection.getLength();
170 for ( ; pSelection != pSelectionEnd; ++pSelection )
171 m_aEntries.SelectEntryPos( *pSelection );
174 //........................................................................
175 } // namespace pcr
176 //........................................................................