merged tag LIBREOFFICE_3_2_99_3
[LibreOffice.git] / extensions / source / propctrlr / listselectiondlg.cxx
blob3bf379813739f1ab3787be2fb0550aac2d42d44e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_extensions.hxx"
31 #include "listselectiondlg.hxx"
32 #include "listselectiondlg.hrc"
34 #include "modulepcr.hxx"
35 #include "formresid.hrc"
36 #include "formstrings.hxx"
37 #include <vcl/msgbox.hxx>
39 /** === begin UNO includes === **/
40 /** === end UNO includes === **/
42 //........................................................................
43 namespace pcr
45 //........................................................................
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::beans;
50 //====================================================================
51 //= ListSelectionDialog
52 //====================================================================
53 //--------------------------------------------------------------------
54 ListSelectionDialog::ListSelectionDialog( Window* _pParent, const Reference< XPropertySet >& _rxListBox,
55 const ::rtl::OUString& _rPropertyName, const String& _rPropertyUIName )
56 :ModalDialog( _pParent, PcrRes( RID_DLG_SELECTION ) )
57 ,m_aLabel ( this, PcrRes( FT_ENTRIES ) )
58 ,m_aEntries ( this, PcrRes( LB_ENTRIES ) )
59 ,m_aOK ( this, PcrRes( PB_OK ) )
60 ,m_aCancel ( this, PcrRes( PB_CANCEL ) )
61 ,m_aHelp ( this, PcrRes( PB_HELP ) )
62 ,m_xListBox ( _rxListBox )
63 ,m_sPropertyName( _rPropertyName )
65 FreeResource();
67 OSL_PRECOND( m_xListBox.is(), "ListSelectionDialog::ListSelectionDialog: invalid list box!" );
69 SetText( _rPropertyUIName );
70 m_aLabel.SetText( _rPropertyUIName );
72 initialize( );
75 //------------------------------------------------------------------------
76 short ListSelectionDialog::Execute()
78 short nResult = ModalDialog::Execute();
80 if ( RET_OK == nResult )
81 commitSelection();
83 return nResult;
86 //--------------------------------------------------------------------
87 void ListSelectionDialog::initialize( )
89 if ( !m_xListBox.is() )
90 return;
92 m_aEntries.SetStyle( GetStyle() | WB_SIMPLEMODE );
94 try
96 // initialize the multi-selection flag
97 sal_Bool bMultiSelection = sal_False;
98 OSL_VERIFY( m_xListBox->getPropertyValue( PROPERTY_MULTISELECTION ) >>= bMultiSelection );
99 m_aEntries.EnableMultiSelection( bMultiSelection );
101 // fill the list box with all entries
102 Sequence< ::rtl::OUString > aListEntries;
103 OSL_VERIFY( m_xListBox->getPropertyValue( PROPERTY_STRINGITEMLIST ) >>= aListEntries );
104 fillEntryList( aListEntries );
106 // select entries according to the property
107 Sequence< sal_Int16 > aSelection;
108 OSL_VERIFY( m_xListBox->getPropertyValue( m_sPropertyName ) >>= aSelection );
109 selectEntries( aSelection );
111 catch( const Exception& )
113 OSL_ENSURE( sal_False, "ListSelectionDialog::initialize: caught an exception!" );
117 //--------------------------------------------------------------------
118 void ListSelectionDialog::commitSelection()
120 if ( !m_xListBox.is() )
121 return;
123 Sequence< sal_Int16 > aSelection;
124 collectSelection( aSelection );
128 m_xListBox->setPropertyValue( m_sPropertyName, makeAny( aSelection ) );
130 catch( const Exception& )
132 OSL_ENSURE( sal_False, "ListSelectionDialog::commitSelection: caught an exception!" );
136 //--------------------------------------------------------------------
137 void ListSelectionDialog::fillEntryList( const Sequence< ::rtl::OUString >& _rListEntries )
139 m_aEntries.Clear();
140 const ::rtl::OUString* _pListEntries = _rListEntries.getConstArray();
141 const ::rtl::OUString* _pListEntriesEnd = _rListEntries.getConstArray() + _rListEntries.getLength();
142 for ( ; _pListEntries < _pListEntriesEnd; ++_pListEntries )
143 m_aEntries.InsertEntry( *_pListEntries );
146 //--------------------------------------------------------------------
147 void ListSelectionDialog::collectSelection( Sequence< sal_Int16 >& /* [out] */ _rSelection )
149 sal_uInt16 nSelectedCount = m_aEntries.GetSelectEntryCount( );
150 _rSelection.realloc( nSelectedCount );
151 sal_Int16* pSelection = _rSelection.getArray();
152 for ( sal_uInt16 selected = 0; selected < nSelectedCount; ++selected, ++pSelection )
153 *pSelection = static_cast< sal_Int16 >( m_aEntries.GetSelectEntryPos( selected ) );
156 //--------------------------------------------------------------------
157 void ListSelectionDialog::selectEntries( const Sequence< sal_Int16 >& /* [in ] */ _rSelection )
159 m_aEntries.SetNoSelection();
160 const sal_Int16* pSelection = _rSelection.getConstArray();
161 const sal_Int16* pSelectionEnd = _rSelection.getConstArray() + _rSelection.getLength();
162 for ( ; pSelection != pSelectionEnd; ++pSelection )
163 m_aEntries.SelectEntryPos( *pSelection );
166 //........................................................................
167 } // namespace pcr
168 //........................................................................
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */