LibreOffice Module forms (master) 1
entrylisthelper.cxx
Go to the documentation of this file.
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 "entrylisthelper.hxx"
21#include <FormComponent.hxx>
22
23#include <o3tl/safeint.hxx>
24#include <osl/diagnose.h>
27#include <com/sun/star/form/binding/XListEntryTypedSource.hpp>
28#include <algorithm>
29
30
31namespace frm
32{
33
34
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::lang;
37 using namespace ::com::sun::star::util;
38 using namespace ::com::sun::star::form::binding;
39
41 :m_rControlModel( _rControlModel )
42 ,m_aRefreshListeners( _rControlModel.getInstanceMutex() )
43 {
44 }
45
46
48 :m_rControlModel( _rControlModel )
49 ,m_xListSource ( _rSource.m_xListSource )
50 ,m_aStringItems( _rSource.m_aStringItems )
51 ,m_aRefreshListeners( _rControlModel.getInstanceMutex() )
52 {
53 }
54
55
57 {
58 }
59
60
61 void SAL_CALL OEntryListHelper::setListEntrySource( const Reference< XListEntrySource >& _rxSource )
62 {
64
65 // disconnect from the current external list source
67
68 // and connect to the new one
69 if ( _rxSource.is() )
70 connectExternalListSource( _rxSource, aLock );
71 }
72
73
74 Reference< XListEntrySource > SAL_CALL OEntryListHelper::getListEntrySource( )
75 {
76 return m_xListSource;
77 }
78
79
80 void SAL_CALL OEntryListHelper::entryChanged( const ListEntryEvent& _rEvent )
81 {
83
84 OSL_ENSURE( _rEvent.Source == m_xListSource,
85 "OEntryListHelper::entryChanged: where did this come from?" );
86 OSL_ENSURE( ( _rEvent.Position >= 0 ) && ( o3tl::make_unsigned(_rEvent.Position) < m_aStringItems.size() ),
87 "OEntryListHelper::entryChanged: invalid index!" );
88 OSL_ENSURE( _rEvent.Entries.getLength() == 1,
89 "OEntryListHelper::entryChanged: invalid string list!" );
90
91 if ( ( _rEvent.Position >= 0 )
92 && ( o3tl::make_unsigned(_rEvent.Position) < m_aStringItems.size() )
93 && _rEvent.Entries.hasElements()
94 )
95 {
96 m_aStringItems[ _rEvent.Position ] = _rEvent.Entries[ 0 ];
97 if (m_aTypedItems.hasElements())
98 m_aTypedItems = Sequence<Any>(); // doesn't match anymore
99 stringItemListChanged( aLock );
100 }
101 }
102
103
104 void SAL_CALL OEntryListHelper::entryRangeInserted( const ListEntryEvent& _rEvent )
105 {
107
108 OSL_ENSURE( _rEvent.Source == m_xListSource,
109 "OEntryListHelper::entryRangeInserted: where did this come from?" );
110 OSL_ENSURE( ( _rEvent.Position > 0 ) && ( o3tl::make_unsigned(_rEvent.Position) < m_aStringItems.size() ) && _rEvent.Entries.hasElements(),
111 "OEntryListHelper::entryRangeRemoved: invalid count and/or position!" );
112
113 if ( ( _rEvent.Position > 0 )
114 && ( o3tl::make_unsigned(_rEvent.Position) < m_aStringItems.size() )
115 && _rEvent.Entries.hasElements()
116 )
117 {
118 m_aStringItems.insert(m_aStringItems.begin() + _rEvent.Position, _rEvent.Entries.begin(), _rEvent.Entries.end());
119 if (m_aTypedItems.hasElements())
120 m_aTypedItems = Sequence<Any>(); // doesn't match anymore
121 stringItemListChanged( aLock );
122 }
123 }
124
125
126 void SAL_CALL OEntryListHelper::entryRangeRemoved( const ListEntryEvent& _rEvent )
127 {
129
130 OSL_ENSURE( _rEvent.Source == m_xListSource,
131 "OEntryListHelper::entryRangeRemoved: where did this come from?" );
132 OSL_ENSURE( ( _rEvent.Position > 0 ) && ( _rEvent.Count > 0 ) && ( _rEvent.Position + _rEvent.Count <= static_cast<sal_Int32>(m_aStringItems.size()) ),
133 "OEntryListHelper::entryRangeRemoved: invalid count and/or position!" );
134
135 if ( !(( _rEvent.Position > 0 )
136 && ( _rEvent.Count > 0 )
137 && ( _rEvent.Position + _rEvent.Count <= static_cast<sal_Int32>(m_aStringItems.size()) ))
138 )
139 return;
140
141 m_aStringItems.erase(m_aStringItems.begin() + _rEvent.Position,
142 m_aStringItems.begin() + _rEvent.Position + _rEvent.Count );
143 if (_rEvent.Position + _rEvent.Count <= m_aTypedItems.getLength())
144 {
145 Sequence<Any> aTmp( m_aTypedItems.getLength() - _rEvent.Count );
146 auto aTmpRange = asNonConstRange(aTmp);
147 sal_Int32 nStop = _rEvent.Position;
148 sal_Int32 i = 0;
149 for ( ; i < nStop; ++i)
150 {
151 aTmpRange[i] = m_aTypedItems[i];
152 }
153 nStop = aTmp.getLength();
154 for (sal_Int32 j = _rEvent.Position + _rEvent.Count; i < nStop; ++i, ++j)
155 {
156 aTmpRange[i] = m_aTypedItems[j];
157 }
158 m_aTypedItems = aTmp;
159 }
160 else if (m_aTypedItems.hasElements())
161 {
162 m_aTypedItems = Sequence<Any>(); // doesn't match anymore
163 }
164 stringItemListChanged( aLock );
165 }
166
167
168 void SAL_CALL OEntryListHelper::allEntriesChanged( const EventObject& _rEvent )
169 {
171
172 OSL_ENSURE( _rEvent.Source == m_xListSource,
173 "OEntryListHelper::allEntriesChanged: where did this come from?" );
174
175 if ( _rEvent.Source == m_xListSource )
176 {
177 impl_lock_refreshList( aLock );
178 }
179 }
180
181 // XRefreshable
182
183 void SAL_CALL OEntryListHelper::addRefreshListener(const Reference<XRefreshListener>& _rxListener)
184 {
185 if ( _rxListener.is() )
186 m_aRefreshListeners.addInterface( _rxListener );
187 }
188
189
190 void SAL_CALL OEntryListHelper::removeRefreshListener(const Reference<XRefreshListener>& _rxListener)
191 {
192 if ( _rxListener.is() )
194 }
195
196
198 {
199 {
201 impl_lock_refreshList( aLock );
202 }
203
204 EventObject aEvt( static_cast< XRefreshable* >( this ) );
205 m_aRefreshListeners.notifyEach( &XRefreshListener::refreshed, aEvt );
206 }
207
208
210 {
211 if ( hasExternalListSource() )
212 obtainListSourceEntries( _rInstanceLock );
213 else
215 }
216
217
218 bool OEntryListHelper::handleDisposing( const EventObject& _rEvent )
219 {
220 if ( m_xListSource .is() && ( _rEvent.Source == m_xListSource ) )
221 {
223 return true;
224 }
225 return false;
226 }
227
228
230 {
231 EventObject aEvt( static_cast< XRefreshable* >( this ) );
233
234 if ( hasExternalListSource( ) )
236 }
237
238
240 {
241 if ( m_xListSource.is() )
242 m_xListSource->removeListEntryListener( this );
243
244 m_xListSource.clear();
245 }
246
247
248 void OEntryListHelper::connectExternalListSource( const Reference< XListEntrySource >& _rxSource, ControlModelLock& _rInstanceLock )
249 {
250 OSL_ENSURE( !hasExternalListSource(), "OEntryListHelper::connectExternalListSource: only to be called if no external source is active!" );
251 OSL_ENSURE( _rxSource.is(), "OEntryListHelper::connectExternalListSource: invalid list source!" );
252
253 // remember it
254 m_xListSource = _rxSource;
255
256 // initially fill our item list
257 if ( m_xListSource.is() )
258 {
259 // be notified when the list changes ...
260 m_xListSource->addListEntryListener( this );
261
262 obtainListSourceEntries( _rInstanceLock );
263 }
264 }
265
266
268 {
269 Reference< XListEntryTypedSource > xTyped;
270 xTyped.set( m_xListSource, UNO_QUERY);
271 if (xTyped.is())
272 {
273 comphelper::sequenceToContainer( m_aStringItems, xTyped->getAllListEntriesTyped( m_aTypedItems));
274 }
275 else
276 {
278 if (m_aTypedItems.hasElements())
279 m_aTypedItems = Sequence<Any>();
280 }
281 stringItemListChanged( _rInstanceLock );
282 }
283
284
286 Any& _rOldValue, const Any& _rValue )
287 {
288 if ( hasExternalListSource() )
289 throw IllegalArgumentException( );
290 // TODO: error message
291
292 return ::comphelper::tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, comphelper::containerToSequence(m_aStringItems) );
293 }
294
295
296 void OEntryListHelper::setNewStringItemList( const css::uno::Any& _rValue, ControlModelLock& _rInstanceLock )
297 {
298 OSL_PRECOND( !hasExternalListSource(), "OEntryListHelper::setNewStringItemList: this should never have survived convertNewListSourceProperty!" );
299 css::uno::Sequence<OUString> aTmp;
300 OSL_VERIFY( _rValue >>= aTmp );
302 if (m_aTypedItems.hasElements())
303 m_aTypedItems = Sequence<Any>(); // doesn't match anymore
304 stringItemListChanged( _rInstanceLock );
305 }
306
307
308 void OEntryListHelper::setNewTypedItemList( const css::uno::Any& _rValue, ControlModelLock& _rInstanceLock )
309 {
310 OSL_PRECOND( !hasExternalListSource(), "OEntryListHelper::setNewTypedItemList: this should never have survived convertNewListSourceProperty!" );
311 if (!(_rValue >>= m_aTypedItems ))
312 {
313 OSL_VERIFY(false);
314 if (m_aTypedItems.hasElements())
315 m_aTypedItems = Sequence<Any>(); // doesn't match anymore
316 }
317 // Sets both properties, assuming that TypedItemList belongs to StringItemList.
318 stringItemListChanged( _rInstanceLock );
319 }
320
321
322} // namespace frm
323
324
325/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 addInterface(const css::uno::Reference< ListenerT > &rxIFace)
void disposeAndClear(const css::lang::EventObject &rEvt)
sal_Int32 removeInterface(const css::uno::Reference< ListenerT > &rxIFace)
void notifyEach(void(SAL_CALL ListenerT::*NotificationMethod)(const EventT &), const EventT &Event)
class whose instances lock an OControlModel
void obtainListSourceEntries(ControlModelLock &_rInstanceLock)
obtains list entries and possibly data values from list source
::comphelper::OInterfaceContainerHelper3< css::util::XRefreshListener > m_aRefreshListeners
"overridden" TypedItemList property value
OEntryListHelper(OControlModel &_rControlModel)
bool convertNewListSourceProperty(css::uno::Any &_rConvertedValue, css::uno::Any &_rOldValue, const css::uno::Any &_rValue)
helper for implementing convertFastPropertyValue( StringItemList )
void setNewStringItemList(const css::uno::Any &_rValue, ControlModelLock &_rInstanceLock)
helper for implementing setFastPropertyValueNoBroadcast
virtual void refreshInternalEntryList()=0
called when XRefreshable::refresh has been called, and we do not have an external list source
virtual void stringItemListChanged(ControlModelLock &_rInstanceLock)=0
announces that the list of entries has changed.
std::vector< OUString > m_aStringItems
our external list source
css::uno::Sequence< css::uno::Any > m_aTypedItems
"overridden" StringItemList property value
virtual css::uno::Reference< css::form::binding::XListEntrySource > SAL_CALL getListEntrySource() override
void connectExternalListSource(const css::uno::Reference< css::form::binding::XListEntrySource > &_rxSource, ControlModelLock &_rInstanceLock)
connects to a new external list source
virtual void SAL_CALL entryRangeInserted(const css::form::binding::ListEntryEvent &_rSource) override
void disconnectExternalListSource()
disconnects from the active external list source, if present
void impl_lock_refreshList(ControlModelLock &_rInstanceLock)
refreshes our list entries
virtual void SAL_CALL refresh() override
virtual void SAL_CALL removeRefreshListener(const css::uno::Reference< css::util::XRefreshListener > &_rxListener) override
void disposing()
to be called by derived classes' instances when they're being disposed
void setNewTypedItemList(const css::uno::Any &_rValue, ControlModelLock &_rInstanceLock)
helper for implementing setFastPropertyValueNoBroadcast
css::uno::Reference< css::form::binding::XListEntrySource > m_xListSource
OControlModel & m_rControlModel
virtual void SAL_CALL allEntriesChanged(const css::lang::EventObject &_rSource) override
bool handleDisposing(const css::lang::EventObject &_rEvent)
handling the XEventListener::disposing call for the case where our list source is being disposed
virtual void SAL_CALL setListEntrySource(const css::uno::Reference< css::form::binding::XListEntrySource > &_rxSource) override
virtual void SAL_CALL entryChanged(const css::form::binding::ListEntryEvent &_rSource) override
bool hasExternalListSource() const
determines whether we actually have an external list source
virtual void SAL_CALL addRefreshListener(const css::uno::Reference< css::util::XRefreshListener > &_rxListener) override
virtual void SAL_CALL entryRangeRemoved(const css::form::binding::ListEntryEvent &_rSource) override
DstType sequenceToContainer(const css::uno::Sequence< SrcType > &i_Sequence)
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
ListBox is a bit confusing / different from other form components, so here are a few notes:
Definition: BaseListBox.hxx:25
int i
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)