Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "listselectiondlg.hxx"
21 : #include "listselectiondlg.hrc"
22 :
23 : #include "modulepcr.hxx"
24 : #include "formresid.hrc"
25 : #include "formstrings.hxx"
26 : #include <vcl/msgbox.hxx>
27 :
28 :
29 : namespace pcr
30 : {
31 :
32 :
33 : using namespace ::com::sun::star::uno;
34 : using namespace ::com::sun::star::beans;
35 :
36 :
37 : //= ListSelectionDialog
38 :
39 :
40 0 : ListSelectionDialog::ListSelectionDialog( Window* _pParent, const Reference< XPropertySet >& _rxListBox,
41 : const OUString& _rPropertyName, const OUString& _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 0 : ,m_sPropertyName( _rPropertyName )
50 : {
51 0 : FreeResource();
52 :
53 : OSL_PRECOND( m_xListBox.is(), "ListSelectionDialog::ListSelectionDialog: invalid list box!" );
54 :
55 0 : SetText( _rPropertyUIName );
56 0 : m_aLabel.SetText( _rPropertyUIName );
57 :
58 0 : initialize( );
59 0 : }
60 :
61 :
62 0 : short ListSelectionDialog::Execute()
63 : {
64 0 : short nResult = ModalDialog::Execute();
65 :
66 0 : if ( RET_OK == nResult )
67 0 : commitSelection();
68 :
69 0 : return nResult;
70 : }
71 :
72 :
73 0 : void ListSelectionDialog::initialize( )
74 : {
75 0 : if ( !m_xListBox.is() )
76 0 : return;
77 :
78 0 : m_aEntries.SetStyle( GetStyle() | WB_SIMPLEMODE );
79 :
80 : try
81 : {
82 : // initialize the multi-selection flag
83 0 : sal_Bool bMultiSelection = sal_False;
84 0 : OSL_VERIFY( m_xListBox->getPropertyValue( PROPERTY_MULTISELECTION ) >>= bMultiSelection );
85 0 : m_aEntries.EnableMultiSelection( bMultiSelection );
86 :
87 : // fill the list box with all entries
88 0 : Sequence< OUString > aListEntries;
89 0 : OSL_VERIFY( m_xListBox->getPropertyValue( PROPERTY_STRINGITEMLIST ) >>= aListEntries );
90 0 : fillEntryList( aListEntries );
91 :
92 : // select entries according to the property
93 0 : Sequence< sal_Int16 > aSelection;
94 0 : OSL_VERIFY( m_xListBox->getPropertyValue( m_sPropertyName ) >>= aSelection );
95 0 : selectEntries( aSelection );
96 : }
97 0 : catch( const Exception& )
98 : {
99 : OSL_FAIL( "ListSelectionDialog::initialize: caught an exception!" );
100 : }
101 : }
102 :
103 :
104 0 : void ListSelectionDialog::commitSelection()
105 : {
106 0 : if ( !m_xListBox.is() )
107 0 : return;
108 :
109 0 : Sequence< sal_Int16 > aSelection;
110 0 : collectSelection( aSelection );
111 :
112 : try
113 : {
114 0 : m_xListBox->setPropertyValue( m_sPropertyName, makeAny( aSelection ) );
115 : }
116 0 : catch( const Exception& )
117 : {
118 : OSL_FAIL( "ListSelectionDialog::commitSelection: caught an exception!" );
119 0 : }
120 : }
121 :
122 :
123 0 : void ListSelectionDialog::fillEntryList( const Sequence< OUString >& _rListEntries )
124 : {
125 0 : m_aEntries.Clear();
126 0 : const OUString* _pListEntries = _rListEntries.getConstArray();
127 0 : const OUString* _pListEntriesEnd = _rListEntries.getConstArray() + _rListEntries.getLength();
128 0 : for ( ; _pListEntries < _pListEntriesEnd; ++_pListEntries )
129 0 : m_aEntries.InsertEntry( *_pListEntries );
130 0 : }
131 :
132 :
133 0 : void ListSelectionDialog::collectSelection( Sequence< sal_Int16 >& /* [out] */ _rSelection )
134 : {
135 0 : sal_uInt16 nSelectedCount = m_aEntries.GetSelectEntryCount( );
136 0 : _rSelection.realloc( nSelectedCount );
137 0 : sal_Int16* pSelection = _rSelection.getArray();
138 0 : for ( sal_uInt16 selected = 0; selected < nSelectedCount; ++selected, ++pSelection )
139 0 : *pSelection = static_cast< sal_Int16 >( m_aEntries.GetSelectEntryPos( selected ) );
140 0 : }
141 :
142 :
143 0 : void ListSelectionDialog::selectEntries( const Sequence< sal_Int16 >& /* [in ] */ _rSelection )
144 : {
145 0 : m_aEntries.SetNoSelection();
146 0 : const sal_Int16* pSelection = _rSelection.getConstArray();
147 0 : const sal_Int16* pSelectionEnd = _rSelection.getConstArray() + _rSelection.getLength();
148 0 : for ( ; pSelection != pSelectionEnd; ++pSelection )
149 0 : m_aEntries.SelectEntryPos( *pSelection );
150 0 : }
151 :
152 :
153 : } // namespace pcr
154 :
155 :
156 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|