Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / extensions / source / propctrlr / listselectiondlg.cxx
blob29b3b19e3a5874522c7a86955caf8e0df1f3a294
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"
22 #include "modulepcr.hxx"
23 #include <strings.hrc>
24 #include "formstrings.hxx"
25 #include <comphelper/sequence.hxx>
27 namespace pcr
29 using namespace ::com::sun::star::uno;
30 using namespace ::com::sun::star::beans;
32 ListSelectionDialog::ListSelectionDialog(weld::Window* pParent, const Reference< XPropertySet >& _rxListBox,
33 const OUString& _rPropertyName, const OUString& _rPropertyUIName)
34 : GenericDialogController(pParent, "modules/spropctrlr/ui/listselectdialog.ui", "ListSelectDialog")
35 , m_xListBox ( _rxListBox )
36 , m_sPropertyName( _rPropertyName )
37 , m_xFrame(m_xBuilder->weld_frame("frame"))
38 , m_xEntries(m_xBuilder->weld_tree_view("treeview"))
40 OSL_PRECOND( m_xListBox.is(), "ListSelectionDialog::ListSelectionDialog: invalid list box!" );
42 m_xEntries->set_size_request(m_xEntries->get_approximate_digit_width() * 40, m_xEntries->get_height_rows(9));
44 m_xDialog->set_title(_rPropertyUIName);
45 m_xFrame->set_label(_rPropertyUIName);
47 initialize( );
50 ListSelectionDialog::~ListSelectionDialog()
54 short ListSelectionDialog::run()
56 short nResult = GenericDialogController::run();
58 if ( RET_OK == nResult )
59 commitSelection();
61 return nResult;
65 void ListSelectionDialog::initialize( )
67 if ( !m_xListBox.is() )
68 return;
70 try
72 // initialize the multi-selection flag
73 bool bMultiSelection = false;
74 OSL_VERIFY( m_xListBox->getPropertyValue( PROPERTY_MULTISELECTION ) >>= bMultiSelection );
75 m_xEntries->set_selection_mode(bMultiSelection ? SelectionMode::Single : SelectionMode::Multiple);
77 // fill the list box with all entries
78 Sequence< OUString > aListEntries;
79 OSL_VERIFY( m_xListBox->getPropertyValue( PROPERTY_STRINGITEMLIST ) >>= aListEntries );
80 fillEntryList( aListEntries );
82 // select entries according to the property
83 Sequence< sal_Int16 > aSelection;
84 OSL_VERIFY( m_xListBox->getPropertyValue( m_sPropertyName ) >>= aSelection );
85 selectEntries( aSelection );
87 catch( const Exception& )
89 OSL_FAIL( "ListSelectionDialog::initialize: caught an exception!" );
93 void ListSelectionDialog::commitSelection()
95 if ( !m_xListBox.is() )
96 return;
98 std::vector< sal_Int16 > aSelection;
99 collectSelection( aSelection );
103 m_xListBox->setPropertyValue( m_sPropertyName, makeAny( comphelper::containerToSequence(aSelection) ) );
105 catch( const Exception& )
107 OSL_FAIL( "ListSelectionDialog::commitSelection: caught an exception!" );
111 void ListSelectionDialog::fillEntryList( const Sequence< OUString >& _rListEntries )
113 m_xEntries->freeze();
114 m_xEntries->clear();
115 for (auto const & entry : _rListEntries)
116 m_xEntries->append_text(entry);
117 m_xEntries->thaw();
120 void ListSelectionDialog::collectSelection( std::vector< sal_Int16 >& /* [out] */ _rSelection )
122 auto aSelection = m_xEntries->get_selected_rows();
123 _rSelection.resize(aSelection.size());
124 for (auto row : aSelection)
125 _rSelection.push_back(row);
128 void ListSelectionDialog::selectEntries( const Sequence< sal_Int16 >& /* [in ] */ _rSelection )
130 m_xEntries->unselect_all();
131 for (auto const & selection : _rSelection)
132 m_xEntries->select(selection);
136 } // namespace pcr
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */