Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
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.
15 : : *
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).
21 : : *
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.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include "listselectiondlg.hxx"
30 : : #include "listselectiondlg.hrc"
31 : :
32 : : #include "modulepcr.hxx"
33 : : #include "formresid.hrc"
34 : : #include "formstrings.hxx"
35 : : #include <vcl/msgbox.hxx>
36 : :
37 : : //........................................................................
38 : : namespace pcr
39 : : {
40 : : //........................................................................
41 : :
42 : : using namespace ::com::sun::star::uno;
43 : : using namespace ::com::sun::star::beans;
44 : :
45 : : //====================================================================
46 : : //= ListSelectionDialog
47 : : //====================================================================
48 : : //--------------------------------------------------------------------
49 : 0 : ListSelectionDialog::ListSelectionDialog( Window* _pParent, const Reference< XPropertySet >& _rxListBox,
50 : : const ::rtl::OUString& _rPropertyName, const String& _rPropertyUIName )
51 : : :ModalDialog( _pParent, PcrRes( RID_DLG_SELECTION ) )
52 : : ,m_aLabel ( this, PcrRes( FT_ENTRIES ) )
53 : : ,m_aEntries ( this, PcrRes( LB_ENTRIES ) )
54 : : ,m_aOK ( this, PcrRes( PB_OK ) )
55 : : ,m_aCancel ( this, PcrRes( PB_CANCEL ) )
56 : : ,m_aHelp ( this, PcrRes( PB_HELP ) )
57 : : ,m_xListBox ( _rxListBox )
58 : 0 : ,m_sPropertyName( _rPropertyName )
59 : : {
60 : 0 : FreeResource();
61 : :
62 : : OSL_PRECOND( m_xListBox.is(), "ListSelectionDialog::ListSelectionDialog: invalid list box!" );
63 : :
64 : 0 : SetText( _rPropertyUIName );
65 : 0 : m_aLabel.SetText( _rPropertyUIName );
66 : :
67 : 0 : initialize( );
68 : 0 : }
69 : :
70 : : //------------------------------------------------------------------------
71 : 0 : short ListSelectionDialog::Execute()
72 : : {
73 : 0 : short nResult = ModalDialog::Execute();
74 : :
75 : 0 : if ( RET_OK == nResult )
76 : 0 : commitSelection();
77 : :
78 : 0 : return nResult;
79 : : }
80 : :
81 : : //--------------------------------------------------------------------
82 : 0 : void ListSelectionDialog::initialize( )
83 : : {
84 : 0 : if ( !m_xListBox.is() )
85 : 0 : return;
86 : :
87 : 0 : m_aEntries.SetStyle( GetStyle() | WB_SIMPLEMODE );
88 : :
89 : : try
90 : : {
91 : : // initialize the multi-selection flag
92 : 0 : sal_Bool bMultiSelection = sal_False;
93 : 0 : OSL_VERIFY( m_xListBox->getPropertyValue( PROPERTY_MULTISELECTION ) >>= bMultiSelection );
94 : 0 : m_aEntries.EnableMultiSelection( bMultiSelection );
95 : :
96 : : // fill the list box with all entries
97 : 0 : Sequence< ::rtl::OUString > aListEntries;
98 : 0 : OSL_VERIFY( m_xListBox->getPropertyValue( PROPERTY_STRINGITEMLIST ) >>= aListEntries );
99 : 0 : fillEntryList( aListEntries );
100 : :
101 : : // select entries according to the property
102 : 0 : Sequence< sal_Int16 > aSelection;
103 : 0 : OSL_VERIFY( m_xListBox->getPropertyValue( m_sPropertyName ) >>= aSelection );
104 : 0 : selectEntries( aSelection );
105 : : }
106 : 0 : catch( const Exception& )
107 : : {
108 : : OSL_FAIL( "ListSelectionDialog::initialize: caught an exception!" );
109 : : }
110 : : }
111 : :
112 : : //--------------------------------------------------------------------
113 : 0 : void ListSelectionDialog::commitSelection()
114 : : {
115 : 0 : if ( !m_xListBox.is() )
116 : 0 : return;
117 : :
118 : 0 : Sequence< sal_Int16 > aSelection;
119 : 0 : collectSelection( aSelection );
120 : :
121 : : try
122 : : {
123 : 0 : m_xListBox->setPropertyValue( m_sPropertyName, makeAny( aSelection ) );
124 : : }
125 : 0 : catch( const Exception& )
126 : : {
127 : : OSL_FAIL( "ListSelectionDialog::commitSelection: caught an exception!" );
128 : 0 : }
129 : : }
130 : :
131 : : //--------------------------------------------------------------------
132 : 0 : void ListSelectionDialog::fillEntryList( const Sequence< ::rtl::OUString >& _rListEntries )
133 : : {
134 : 0 : m_aEntries.Clear();
135 : 0 : const ::rtl::OUString* _pListEntries = _rListEntries.getConstArray();
136 : 0 : const ::rtl::OUString* _pListEntriesEnd = _rListEntries.getConstArray() + _rListEntries.getLength();
137 : 0 : for ( ; _pListEntries < _pListEntriesEnd; ++_pListEntries )
138 : 0 : m_aEntries.InsertEntry( *_pListEntries );
139 : 0 : }
140 : :
141 : : //--------------------------------------------------------------------
142 : 0 : void ListSelectionDialog::collectSelection( Sequence< sal_Int16 >& /* [out] */ _rSelection )
143 : : {
144 : 0 : sal_uInt16 nSelectedCount = m_aEntries.GetSelectEntryCount( );
145 : 0 : _rSelection.realloc( nSelectedCount );
146 : 0 : sal_Int16* pSelection = _rSelection.getArray();
147 : 0 : for ( sal_uInt16 selected = 0; selected < nSelectedCount; ++selected, ++pSelection )
148 : 0 : *pSelection = static_cast< sal_Int16 >( m_aEntries.GetSelectEntryPos( selected ) );
149 : 0 : }
150 : :
151 : : //--------------------------------------------------------------------
152 : 0 : void ListSelectionDialog::selectEntries( const Sequence< sal_Int16 >& /* [in ] */ _rSelection )
153 : : {
154 : 0 : m_aEntries.SetNoSelection();
155 : 0 : const sal_Int16* pSelection = _rSelection.getConstArray();
156 : 0 : const sal_Int16* pSelectionEnd = _rSelection.getConstArray() + _rSelection.getLength();
157 : 0 : for ( ; pSelection != pSelectionEnd; ++pSelection )
158 : 0 : m_aEntries.SelectEntryPos( *pSelection );
159 : 0 : }
160 : :
161 : : //........................................................................
162 : : } // namespace pcr
163 : : //........................................................................
164 : :
165 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|