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 : ListSelectionDialog::~ListSelectionDialog()
52 : {
53 0 : disposeOnce();
54 0 : }
55 :
56 0 : void ListSelectionDialog::dispose()
57 : {
58 0 : m_pEntries.clear();
59 0 : ModalDialog::dispose();
60 0 : }
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_pEntries->SetStyle( GetStyle() | WB_SIMPLEMODE );
79 :
80 : try
81 : {
82 : // initialize the multi-selection flag
83 0 : bool bMultiSelection = false;
84 0 : OSL_VERIFY( m_xListBox->getPropertyValue( PROPERTY_MULTISELECTION ) >>= bMultiSelection );
85 0 : m_pEntries->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_pEntries->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_pEntries->InsertEntry( *_pListEntries );
130 0 : }
131 :
132 :
133 0 : void ListSelectionDialog::collectSelection( Sequence< sal_Int16 >& /* [out] */ _rSelection )
134 : {
135 0 : sal_uInt16 nSelectedCount = m_pEntries->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_pEntries->GetSelectEntryPos( selected ) );
140 0 : }
141 :
142 :
143 0 : void ListSelectionDialog::selectEntries( const Sequence< sal_Int16 >& /* [in ] */ _rSelection )
144 : {
145 0 : m_pEntries->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_pEntries->SelectEntryPos( *pSelection );
150 0 : }
151 :
152 :
153 6 : } // namespace pcr
154 :
155 :
156 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|