1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "formstrings.hxx"
23 #include <comphelper/sequence.hxx>
25 #include <comphelper/diagnose_ex.hxx>
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 OUString _sPropertyName
, const OUString
& _rPropertyUIName
)
34 : GenericDialogController(pParent
, u
"modules/spropctrlr/ui/listselectdialog.ui"_ustr
, u
"ListSelectDialog"_ustr
)
35 , m_xListBox ( _rxListBox
)
36 , m_sPropertyName(std::move( _sPropertyName
))
37 , m_xFrame(m_xBuilder
->weld_frame(u
"frame"_ustr
))
38 , m_xEntries(m_xBuilder
->weld_tree_view(u
"treeview"_ustr
))
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
);
50 ListSelectionDialog::~ListSelectionDialog()
54 short ListSelectionDialog::run()
56 short nResult
= GenericDialogController::run();
58 if ( RET_OK
== nResult
)
65 void ListSelectionDialog::initialize( )
67 if ( !m_xListBox
.is() )
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 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "ListSelectionDialog::initialize" );
93 void ListSelectionDialog::commitSelection()
95 if ( !m_xListBox
.is() )
98 std::vector
< sal_Int16
> aSelection
;
99 collectSelection( aSelection
);
103 m_xListBox
->setPropertyValue( m_sPropertyName
, Any( comphelper::containerToSequence(aSelection
) ) );
105 catch( const Exception
& )
107 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "ListSelectionDialog::commitSelection" );
111 void ListSelectionDialog::fillEntryList( const Sequence
< OUString
>& _rListEntries
)
113 m_xEntries
->freeze();
115 for (auto const & entry
: _rListEntries
)
116 m_xEntries
->append_text(entry
);
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
);
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */