update credits
[LibreOffice.git] / extensions / source / propctrlr / listselectiondlg.cxx
blob75b040df8d9fbaef95bb7ff2cc07c0665e762979
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "listselectiondlg.hxx"
21 #include "listselectiondlg.hrc"
23 #include "modulepcr.hxx"
24 #include "formresid.hrc"
25 #include "formstrings.hxx"
26 #include <vcl/msgbox.hxx>
28 //........................................................................
29 namespace pcr
31 //........................................................................
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::beans;
36 //====================================================================
37 //= ListSelectionDialog
38 //====================================================================
39 //--------------------------------------------------------------------
40 ListSelectionDialog::ListSelectionDialog( Window* _pParent, const Reference< XPropertySet >& _rxListBox,
41 const OUString& _rPropertyName, const String& _rPropertyUIName )
42 :ModalDialog( _pParent, PcrRes( RID_DLG_SELECTION ) )
43 ,m_aLabel ( this, PcrRes( FT_ENTRIES ) )
44 ,m_aEntries ( this, PcrRes( LB_ENTRIES ) )
45 ,m_aOK ( this, PcrRes( PB_OK ) )
46 ,m_aCancel ( this, PcrRes( PB_CANCEL ) )
47 ,m_aHelp ( this, PcrRes( PB_HELP ) )
48 ,m_xListBox ( _rxListBox )
49 ,m_sPropertyName( _rPropertyName )
51 FreeResource();
53 OSL_PRECOND( m_xListBox.is(), "ListSelectionDialog::ListSelectionDialog: invalid list box!" );
55 SetText( _rPropertyUIName );
56 m_aLabel.SetText( _rPropertyUIName );
58 initialize( );
61 //------------------------------------------------------------------------
62 short ListSelectionDialog::Execute()
64 short nResult = ModalDialog::Execute();
66 if ( RET_OK == nResult )
67 commitSelection();
69 return nResult;
72 //--------------------------------------------------------------------
73 void ListSelectionDialog::initialize( )
75 if ( !m_xListBox.is() )
76 return;
78 m_aEntries.SetStyle( GetStyle() | WB_SIMPLEMODE );
80 try
82 // initialize the multi-selection flag
83 sal_Bool bMultiSelection = sal_False;
84 OSL_VERIFY( m_xListBox->getPropertyValue( PROPERTY_MULTISELECTION ) >>= bMultiSelection );
85 m_aEntries.EnableMultiSelection( bMultiSelection );
87 // fill the list box with all entries
88 Sequence< OUString > aListEntries;
89 OSL_VERIFY( m_xListBox->getPropertyValue( PROPERTY_STRINGITEMLIST ) >>= aListEntries );
90 fillEntryList( aListEntries );
92 // select entries according to the property
93 Sequence< sal_Int16 > aSelection;
94 OSL_VERIFY( m_xListBox->getPropertyValue( m_sPropertyName ) >>= aSelection );
95 selectEntries( aSelection );
97 catch( const Exception& )
99 OSL_FAIL( "ListSelectionDialog::initialize: caught an exception!" );
103 //--------------------------------------------------------------------
104 void ListSelectionDialog::commitSelection()
106 if ( !m_xListBox.is() )
107 return;
109 Sequence< sal_Int16 > aSelection;
110 collectSelection( aSelection );
114 m_xListBox->setPropertyValue( m_sPropertyName, makeAny( aSelection ) );
116 catch( const Exception& )
118 OSL_FAIL( "ListSelectionDialog::commitSelection: caught an exception!" );
122 //--------------------------------------------------------------------
123 void ListSelectionDialog::fillEntryList( const Sequence< OUString >& _rListEntries )
125 m_aEntries.Clear();
126 const OUString* _pListEntries = _rListEntries.getConstArray();
127 const OUString* _pListEntriesEnd = _rListEntries.getConstArray() + _rListEntries.getLength();
128 for ( ; _pListEntries < _pListEntriesEnd; ++_pListEntries )
129 m_aEntries.InsertEntry( *_pListEntries );
132 //--------------------------------------------------------------------
133 void ListSelectionDialog::collectSelection( Sequence< sal_Int16 >& /* [out] */ _rSelection )
135 sal_uInt16 nSelectedCount = m_aEntries.GetSelectEntryCount( );
136 _rSelection.realloc( nSelectedCount );
137 sal_Int16* pSelection = _rSelection.getArray();
138 for ( sal_uInt16 selected = 0; selected < nSelectedCount; ++selected, ++pSelection )
139 *pSelection = static_cast< sal_Int16 >( m_aEntries.GetSelectEntryPos( selected ) );
142 //--------------------------------------------------------------------
143 void ListSelectionDialog::selectEntries( const Sequence< sal_Int16 >& /* [in ] */ _rSelection )
145 m_aEntries.SetNoSelection();
146 const sal_Int16* pSelection = _rSelection.getConstArray();
147 const sal_Int16* pSelectionEnd = _rSelection.getConstArray() + _rSelection.getLength();
148 for ( ; pSelection != pSelectionEnd; ++pSelection )
149 m_aEntries.SelectEntryPos( *pSelection );
152 //........................................................................
153 } // namespace pcr
154 //........................................................................
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */