merged tag ooo/OOO330_m14
[LibreOffice.git] / extensions / source / propctrlr / listselectiondlg.cxx
blob14e4ce7de68e6ffd4aac340bd7e56235fbf901dd
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
30 #include "listselectiondlg.hxx"
31 #ifndef EXTENSIONS_SOURCE_PROPCTRLR_LISTSELECTIONDLG_HRC
32 #include "listselectiondlg.hrc"
33 #endif
35 #ifndef _EXTENSIONS_PROPCTRLR_MODULEPRC_HXX_
36 #include "modulepcr.hxx"
37 #endif
38 #ifndef _EXTENSIONS_FORMCTRLR_PROPRESID_HRC_
39 #include "formresid.hrc"
40 #endif
41 #include "formstrings.hxx"
42 #include <vcl/msgbox.hxx>
44 /** === begin UNO includes === **/
45 /** === end UNO includes === **/
47 //........................................................................
48 namespace pcr
50 //........................................................................
52 using namespace ::com::sun::star::uno;
53 using namespace ::com::sun::star::beans;
55 //====================================================================
56 //= ListSelectionDialog
57 //====================================================================
58 //--------------------------------------------------------------------
59 ListSelectionDialog::ListSelectionDialog( Window* _pParent, const Reference< XPropertySet >& _rxListBox,
60 const ::rtl::OUString& _rPropertyName, const String& _rPropertyUIName )
61 :ModalDialog( _pParent, PcrRes( RID_DLG_SELECTION ) )
62 ,m_aLabel ( this, PcrRes( FT_ENTRIES ) )
63 ,m_aEntries ( this, PcrRes( LB_ENTRIES ) )
64 ,m_aOK ( this, PcrRes( PB_OK ) )
65 ,m_aCancel ( this, PcrRes( PB_CANCEL ) )
66 ,m_aHelp ( this, PcrRes( PB_HELP ) )
67 ,m_xListBox ( _rxListBox )
68 ,m_sPropertyName( _rPropertyName )
70 FreeResource();
72 OSL_PRECOND( m_xListBox.is(), "ListSelectionDialog::ListSelectionDialog: invalid list box!" );
74 SetText( _rPropertyUIName );
75 m_aLabel.SetText( _rPropertyUIName );
77 initialize( );
80 //------------------------------------------------------------------------
81 short ListSelectionDialog::Execute()
83 short nResult = ModalDialog::Execute();
85 if ( RET_OK == nResult )
86 commitSelection();
88 return nResult;
91 //--------------------------------------------------------------------
92 void ListSelectionDialog::initialize( )
94 if ( !m_xListBox.is() )
95 return;
97 m_aEntries.SetStyle( GetStyle() | WB_SIMPLEMODE );
99 try
101 // initialize the multi-selection flag
102 sal_Bool bMultiSelection = sal_False;
103 OSL_VERIFY( m_xListBox->getPropertyValue( PROPERTY_MULTISELECTION ) >>= bMultiSelection );
104 m_aEntries.EnableMultiSelection( bMultiSelection );
106 // fill the list box with all entries
107 Sequence< ::rtl::OUString > aListEntries;
108 OSL_VERIFY( m_xListBox->getPropertyValue( PROPERTY_STRINGITEMLIST ) >>= aListEntries );
109 fillEntryList( aListEntries );
111 // select entries according to the property
112 Sequence< sal_Int16 > aSelection;
113 OSL_VERIFY( m_xListBox->getPropertyValue( m_sPropertyName ) >>= aSelection );
114 selectEntries( aSelection );
116 catch( const Exception& )
118 OSL_ENSURE( sal_False, "ListSelectionDialog::initialize: caught an exception!" );
122 //--------------------------------------------------------------------
123 void ListSelectionDialog::commitSelection()
125 if ( !m_xListBox.is() )
126 return;
128 Sequence< sal_Int16 > aSelection;
129 collectSelection( aSelection );
133 m_xListBox->setPropertyValue( m_sPropertyName, makeAny( aSelection ) );
135 catch( const Exception& )
137 OSL_ENSURE( sal_False, "ListSelectionDialog::commitSelection: caught an exception!" );
141 //--------------------------------------------------------------------
142 void ListSelectionDialog::fillEntryList( const Sequence< ::rtl::OUString >& _rListEntries )
144 m_aEntries.Clear();
145 const ::rtl::OUString* _pListEntries = _rListEntries.getConstArray();
146 const ::rtl::OUString* _pListEntriesEnd = _rListEntries.getConstArray() + _rListEntries.getLength();
147 for ( ; _pListEntries < _pListEntriesEnd; ++_pListEntries )
148 m_aEntries.InsertEntry( *_pListEntries );
151 //--------------------------------------------------------------------
152 void ListSelectionDialog::collectSelection( Sequence< sal_Int16 >& /* [out] */ _rSelection )
154 sal_uInt16 nSelectedCount = m_aEntries.GetSelectEntryCount( );
155 _rSelection.realloc( nSelectedCount );
156 sal_Int16* pSelection = _rSelection.getArray();
157 for ( sal_uInt16 selected = 0; selected < nSelectedCount; ++selected, ++pSelection )
158 *pSelection = static_cast< sal_Int16 >( m_aEntries.GetSelectEntryPos( selected ) );
161 //--------------------------------------------------------------------
162 void ListSelectionDialog::selectEntries( const Sequence< sal_Int16 >& /* [in ] */ _rSelection )
164 m_aEntries.SetNoSelection();
165 const sal_Int16* pSelection = _rSelection.getConstArray();
166 const sal_Int16* pSelectionEnd = _rSelection.getConstArray() + _rSelection.getLength();
167 for ( ; pSelection != pSelectionEnd; ++pSelection )
168 m_aEntries.SelectEntryPos( *pSelection );
171 //........................................................................
172 } // namespace pcr
173 //........................................................................