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 :
22 : #include "modulepcr.hxx"
23 : #include "formresid.hrc"
24 : #include "formstrings.hxx"
25 : #include <vcl/msgbox.hxx>
26 :
27 : namespace pcr
28 : {
29 : using namespace ::com::sun::star::uno;
30 : using namespace ::com::sun::star::beans;
31 :
32 0 : ListSelectionDialog::ListSelectionDialog(vcl::Window* _pParent, const Reference< XPropertySet >& _rxListBox,
33 : const OUString& _rPropertyName, const OUString& _rPropertyUIName)
34 : : ModalDialog( _pParent, "ListSelectDialog", "modules/spropctrlr/ui/listselectdialog.ui" )
35 : ,m_xListBox ( _rxListBox )
36 0 : ,m_sPropertyName( _rPropertyName )
37 : {
38 : OSL_PRECOND( m_xListBox.is(), "ListSelectionDialog::ListSelectionDialog: invalid list box!" );
39 :
40 0 : get(m_pEntries, "treeview");
41 0 : Size aSize(LogicToPixel(Size(85, 97), MAP_APPFONT));
42 0 : m_pEntries->set_width_request(aSize.Width());
43 0 : m_pEntries->set_height_request(aSize.Height());
44 :
45 0 : SetText(_rPropertyUIName);
46 0 : get<VclFrame>("frame")->set_label(_rPropertyUIName);
47 :
48 0 : initialize( );
49 0 : }
50 :
51 0 : short ListSelectionDialog::Execute()
52 : {
53 0 : short nResult = ModalDialog::Execute();
54 :
55 0 : if ( RET_OK == nResult )
56 0 : commitSelection();
57 :
58 0 : return nResult;
59 : }
60 :
61 :
62 0 : void ListSelectionDialog::initialize( )
63 : {
64 0 : if ( !m_xListBox.is() )
65 0 : return;
66 :
67 0 : m_pEntries->SetStyle( GetStyle() | WB_SIMPLEMODE );
68 :
69 : try
70 : {
71 : // initialize the multi-selection flag
72 0 : bool bMultiSelection = false;
73 0 : OSL_VERIFY( m_xListBox->getPropertyValue( PROPERTY_MULTISELECTION ) >>= bMultiSelection );
74 0 : m_pEntries->EnableMultiSelection( bMultiSelection );
75 :
76 : // fill the list box with all entries
77 0 : Sequence< OUString > aListEntries;
78 0 : OSL_VERIFY( m_xListBox->getPropertyValue( PROPERTY_STRINGITEMLIST ) >>= aListEntries );
79 0 : fillEntryList( aListEntries );
80 :
81 : // select entries according to the property
82 0 : Sequence< sal_Int16 > aSelection;
83 0 : OSL_VERIFY( m_xListBox->getPropertyValue( m_sPropertyName ) >>= aSelection );
84 0 : selectEntries( aSelection );
85 : }
86 0 : catch( const Exception& )
87 : {
88 : OSL_FAIL( "ListSelectionDialog::initialize: caught an exception!" );
89 : }
90 : }
91 :
92 :
93 0 : void ListSelectionDialog::commitSelection()
94 : {
95 0 : if ( !m_xListBox.is() )
96 0 : return;
97 :
98 0 : Sequence< sal_Int16 > aSelection;
99 0 : collectSelection( aSelection );
100 :
101 : try
102 : {
103 0 : m_xListBox->setPropertyValue( m_sPropertyName, makeAny( aSelection ) );
104 : }
105 0 : catch( const Exception& )
106 : {
107 : OSL_FAIL( "ListSelectionDialog::commitSelection: caught an exception!" );
108 0 : }
109 : }
110 :
111 :
112 0 : void ListSelectionDialog::fillEntryList( const Sequence< OUString >& _rListEntries )
113 : {
114 0 : m_pEntries->Clear();
115 0 : const OUString* _pListEntries = _rListEntries.getConstArray();
116 0 : const OUString* _pListEntriesEnd = _rListEntries.getConstArray() + _rListEntries.getLength();
117 0 : for ( ; _pListEntries < _pListEntriesEnd; ++_pListEntries )
118 0 : m_pEntries->InsertEntry( *_pListEntries );
119 0 : }
120 :
121 :
122 0 : void ListSelectionDialog::collectSelection( Sequence< sal_Int16 >& /* [out] */ _rSelection )
123 : {
124 0 : sal_uInt16 nSelectedCount = m_pEntries->GetSelectEntryCount( );
125 0 : _rSelection.realloc( nSelectedCount );
126 0 : sal_Int16* pSelection = _rSelection.getArray();
127 0 : for ( sal_uInt16 selected = 0; selected < nSelectedCount; ++selected, ++pSelection )
128 0 : *pSelection = static_cast< sal_Int16 >( m_pEntries->GetSelectEntryPos( selected ) );
129 0 : }
130 :
131 :
132 0 : void ListSelectionDialog::selectEntries( const Sequence< sal_Int16 >& /* [in ] */ _rSelection )
133 : {
134 0 : m_pEntries->SetNoSelection();
135 0 : const sal_Int16* pSelection = _rSelection.getConstArray();
136 0 : const sal_Int16* pSelectionEnd = _rSelection.getConstArray() + _rSelection.getLength();
137 0 : for ( ; pSelection != pSelectionEnd; ++pSelection )
138 0 : m_pEntries->SelectEntryPos( *pSelection );
139 0 : }
140 :
141 :
142 12 : } // namespace pcr
143 :
144 :
145 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|