Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / forms / source / component / ListBox.hxx
blobbd9d7aac6c432cffe3c90123eac3c1dc83fd805c
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 #ifndef INCLUDED_FORMS_SOURCE_COMPONENT_LISTBOX_HXX
21 #define INCLUDED_FORMS_SOURCE_COMPONENT_LISTBOX_HXX
23 #include <FormComponent.hxx>
24 #include "cachedrowset.hxx"
25 #include "errorbroadcaster.hxx"
26 #include "entrylisthelper.hxx"
28 #include <com/sun/star/util/XNumberFormatter.hpp>
29 #include <com/sun/star/sdb/XSQLErrorBroadcaster.hpp>
30 #include <com/sun/star/form/ListSourceType.hpp>
31 #include <com/sun/star/awt/XItemListener.hpp>
32 #include <com/sun/star/awt/XFocusListener.hpp>
33 #include <com/sun/star/awt/XListBox.hpp>
34 #include <com/sun/star/form/XChangeBroadcaster.hpp>
35 #include <com/sun/star/sdbc/DataType.hpp>
37 #include <comphelper/asyncnotification.hxx>
38 #include <connectivity/FValue.hxx>
39 #include <cppuhelper/interfacecontainer.hxx>
40 #include <cppuhelper/implbase4.hxx>
41 #include <vcl/timer.hxx>
42 #include <vcl/idle.hxx>
44 #include <vector>
46 /** ListBox is a bit confusing / different from other form components,
47 so here are a few notes:
49 The general design philosophy is that a ListBox is a mechanism
50 to translate back and forth between:
51 1) *display* values (strings that the user sees and chooses)
52 2) *binding* values, which is what the program (for a dialog),
53 the database, ... cares about.
55 A non-data aware ListBox exposes this mechanism through
56 com.sun.star.awt.XItemList (get|set)ItemData.
58 In a data-aware ListBox, this is naturally embodied by the
59 StringItemList on the one hand, and the ValueList on the other
60 hand (where, depending on ListSourceType, the ValueList is
61 possibly automatically filled from the BoundColumn of the
62 ListSource).
64 This source file implements data-aware ListBox, and the rest
65 of this comment applies to data-aware ListBox (only).
67 In all public APIs of the *model* (OListBoxModel),
68 the value of the control is the *binding* value.
69 That is what the bound database field gets,
70 that is what a validator validates,
71 that is what an external value binding
72 (com.sun.star.form.binding.XValueBinding)
73 exchanges with the control.
75 As an *implementation* choice, we keep the current value of the
76 ListBox as a sequence of *indices* in the value list, and do the
77 lookup on demand:
79 - ListBox's content property (or value property, sorry the
80 terminology is not always consistent) is SelectedItems which is
81 a sequence of *indices* in the value list.
83 - That is used to synchronise with our peer (UnoControlListBoxModel).
85 In particular, note that getCurrentValue() is a public API (and
86 deals with bound values), but getControlValue and
87 (do)setControlValue are *internal* implementation helpers that
88 deal with *indices*.
90 Note that the *view* (OListBoxControl) presents a different story
91 than the model. E.g. the "SelectedItems" property is *display* *values*.
95 namespace frm
98 typedef ::std::vector< ::connectivity::ORowSetValue > ValueList;
100 class OListBoxModel final :public OBoundControlModel
101 ,public OEntryListHelper
102 ,public OErrorBroadcaster
105 CachedRowSet m_aListRowSet; // the row set to fill the list
106 ::connectivity::ORowSetValue m_aSaveValue;
108 // <properties>
109 css::form::ListSourceType m_eListSourceType; // type of list source
110 css::uno::Any m_aBoundColumn;
111 ValueList m_aListSourceValues;
112 ValueList m_aBoundValues; // do not write directly; use setBoundValues()
113 mutable ValueList m_aConvertedBoundValues;
114 mutable sal_Int32 m_nConvertedBoundValuesType;
115 css::uno::Sequence<sal_Int16> m_aDefaultSelectSeq; // DefaultSelected
116 // </properties>
118 mutable sal_Int16 m_nNULLPos; // position of the NULL value in our list
119 sal_Int32 m_nBoundColumnType;
121 private:
122 ::connectivity::ORowSetValue getFirstSelectedValue() const;
124 virtual css::uno::Sequence< css::uno::Type> _getTypes() override;
126 public:
127 DECLARE_DEFAULT_LEAF_XTOR( OListBoxModel );
129 // XServiceInfo
130 OUString SAL_CALL getImplementationName() override
131 { return "com.sun.star.form.OListBoxModel"; }
133 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
135 // UNO binding
136 DECLARE_UNO3_AGG_DEFAULTS(OListBoxModel, OBoundControlModel)
137 virtual css::uno::Any SAL_CALL queryAggregation( const css::uno::Type& _rType ) override;
139 // OComponentHelper
140 virtual void SAL_CALL disposing() override;
142 // OPropertySetHelper
143 virtual void SAL_CALL getFastPropertyValue(css::uno::Any& rValue, sal_Int32 nHandle) const override;
144 virtual void SAL_CALL setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const css::uno::Any& rValue ) override;
145 virtual sal_Bool SAL_CALL convertFastPropertyValue(
146 css::uno::Any& _rConvertedValue, css::uno::Any& _rOldValue, sal_Int32 _nHandle, const css::uno::Any& _rValue ) override;
148 private:
149 static const ::connectivity::ORowSetValue s_aEmptyValue;
150 static const ::connectivity::ORowSetValue s_aEmptyStringValue;
152 // XMultiPropertySet
153 virtual void SAL_CALL setPropertyValues(const css::uno::Sequence< OUString >& PropertyNames, const css::uno::Sequence< css::uno::Any >& Values) override;
155 // XPersistObject
156 virtual OUString SAL_CALL getServiceName() override;
157 virtual void SAL_CALL
158 write(const css::uno::Reference< css::io::XObjectOutputStream>& _rxOutStream) override;
159 virtual void SAL_CALL
160 read(const css::uno::Reference< css::io::XObjectInputStream>& _rxInStream) override;
162 // OControlModel's property handling
163 virtual void describeFixedProperties(
164 css::uno::Sequence< css::beans::Property >& /* [out] */ _rProps
165 ) const override;
166 virtual void describeAggregateProperties(
167 css::uno::Sequence< css::beans::Property >& /* [out] */ _rAggregateProps
168 ) const override;
170 // XEventListener
171 virtual void SAL_CALL disposing(const css::lang::EventObject& Source) override;
173 // OPropertyChangeListener
174 virtual void _propertyChanged( const css::beans::PropertyChangeEvent& _rEvt ) override;
176 // prevent method hiding
177 using OBoundControlModel::getFastPropertyValue;
178 using OBoundControlModel::setPropertyValues;
180 // OBoundControlModel overridables
181 virtual css::uno::Any translateDbColumnToControlValue( ) override;
182 virtual css::uno::Sequence< css::uno::Type >
183 getSupportedBindingTypes() override;
184 virtual css::uno::Any translateExternalValueToControlValue( const css::uno::Any& _rExternalValue ) const override;
185 virtual css::uno::Any translateControlValueToExternalValue( ) const override;
186 virtual css::uno::Any translateControlValueToValidatableValue( ) const override;
187 virtual bool commitControlValueToDbColumn( bool _bPostReset ) override;
189 virtual void onConnectedDbColumn( const css::uno::Reference< css::uno::XInterface >& _rxForm ) override;
190 virtual void onDisconnectedDbColumn() override;
192 virtual css::uno::Any getDefaultForReset() const override;
193 virtual void resetNoBroadcast() override;
195 virtual css::uno::Any getCurrentFormComponentValue() const override;
197 // OEntryListHelper overridables
198 virtual void stringItemListChanged( ControlModelLock& _rInstanceLock ) override;
199 virtual void refreshInternalEntryList() override;
201 virtual css::uno::Reference< css::util::XCloneable > SAL_CALL createClone( ) override;
203 void init();
204 css::uno::Any getCurrentSingleValue() const;
205 css::uno::Sequence<css::uno::Any> getCurrentMultiValue() const;
206 css::uno::Sequence< sal_Int16 > translateBindingValuesToControlValue(
207 const css::uno::Sequence< const css::uno::Any > &i_aValues)
208 const;
209 css::uno::Sequence< sal_Int16 > translateDbValueToControlValue(
210 const ::connectivity::ORowSetValue &aValue)
211 const;
213 void loadData( bool _bForce );
215 /** refreshes the list boxes list data
216 @precond we don't actually have an external list source
218 void impl_refreshDbEntryList( bool _bForce );
220 void setBoundValues(const ValueList&);
221 void clearBoundValues();
223 ValueList impl_getValues() const;
225 sal_Int32 getValueType() const;
227 void convertBoundValues(sal_Int32 nType) const;
231 //= OListBoxControl
233 typedef ::cppu::ImplHelper4 < css::awt::XFocusListener
234 , css::awt::XItemListener
235 , css::awt::XListBox
236 , css::form::XChangeBroadcaster
237 > OListBoxControl_BASE;
239 class OListBoxControl :public OBoundControl
240 ,public OListBoxControl_BASE
241 ,public IEventProcessor
243 private:
244 ::comphelper::OInterfaceContainerHelper2 m_aChangeListeners;
245 ::comphelper::OInterfaceContainerHelper2 m_aItemListeners;
247 css::uno::Any m_aCurrentSelection;
248 Idle m_aChangeIdle;
250 css::uno::Reference< css::awt::XListBox >
251 m_xAggregateListBox;
253 ::rtl::Reference< ::comphelper::AsyncEventNotifier >
254 m_pItemBroadcaster;
256 protected:
257 // UNO binding
258 virtual css::uno::Sequence< css::uno::Type> _getTypes() override;
260 public:
261 explicit OListBoxControl(const css::uno::Reference< css::uno::XComponentContext>& _rxFactory);
262 virtual ~OListBoxControl() override;
264 // UNO binding
265 DECLARE_UNO3_AGG_DEFAULTS(OListBoxControl, OBoundControl)
266 virtual css::uno::Any SAL_CALL queryAggregation( const css::uno::Type& _rType ) override;
268 // XServiceInfo
269 OUString SAL_CALL getImplementationName() override
270 { return "com.sun.star.form.OListBoxControl"; }
272 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
274 // XChangeBroadcaster
275 virtual void SAL_CALL addChangeListener(const css::uno::Reference< css::form::XChangeListener>& _rxListener) override;
276 virtual void SAL_CALL removeChangeListener(const css::uno::Reference< css::form::XChangeListener>& _rxListener) override;
278 // XFocusListener
279 virtual void SAL_CALL focusGained(const css::awt::FocusEvent& _rEvent) override;
280 virtual void SAL_CALL focusLost(const css::awt::FocusEvent& _rEvent) override;
282 // XItemListener
283 virtual void SAL_CALL itemStateChanged(const css::awt::ItemEvent& _rEvent) override;
285 // XEventListener
286 virtual void SAL_CALL disposing(const css::lang::EventObject& Source) override;
288 // OComponentHelper
289 virtual void SAL_CALL disposing() override;
291 // XListBox
292 virtual void SAL_CALL addItemListener( const css::uno::Reference< css::awt::XItemListener >& l ) override;
293 virtual void SAL_CALL removeItemListener( const css::uno::Reference< css::awt::XItemListener >& l ) override;
294 virtual void SAL_CALL addActionListener( const css::uno::Reference< css::awt::XActionListener >& l ) override;
295 virtual void SAL_CALL removeActionListener( const css::uno::Reference< css::awt::XActionListener >& l ) override;
296 virtual void SAL_CALL addItem( const OUString& aItem, ::sal_Int16 nPos ) override;
297 virtual void SAL_CALL addItems( const css::uno::Sequence< OUString >& aItems, ::sal_Int16 nPos ) override;
298 virtual void SAL_CALL removeItems( ::sal_Int16 nPos, ::sal_Int16 nCount ) override;
299 virtual ::sal_Int16 SAL_CALL getItemCount( ) override;
300 virtual OUString SAL_CALL getItem( ::sal_Int16 nPos ) override;
301 virtual css::uno::Sequence< OUString > SAL_CALL getItems( ) override;
302 virtual ::sal_Int16 SAL_CALL getSelectedItemPos( ) override;
303 virtual css::uno::Sequence< ::sal_Int16 > SAL_CALL getSelectedItemsPos( ) override;
304 virtual OUString SAL_CALL getSelectedItem( ) override;
305 virtual css::uno::Sequence< OUString > SAL_CALL getSelectedItems( ) override;
306 virtual void SAL_CALL selectItemPos( ::sal_Int16 nPos, sal_Bool bSelect ) override;
307 virtual void SAL_CALL selectItemsPos( const css::uno::Sequence< ::sal_Int16 >& aPositions, sal_Bool bSelect ) override;
308 virtual void SAL_CALL selectItem( const OUString& aItem, sal_Bool bSelect ) override;
309 virtual sal_Bool SAL_CALL isMutipleMode( ) override;
310 virtual void SAL_CALL setMultipleMode( sal_Bool bMulti ) override;
311 virtual ::sal_Int16 SAL_CALL getDropDownLineCount( ) override;
312 virtual void SAL_CALL setDropDownLineCount( ::sal_Int16 nLines ) override;
313 virtual void SAL_CALL makeVisible( ::sal_Int16 nEntry ) override;
315 protected:
316 // IEventProcessor
317 virtual void processEvent( const ::comphelper::AnyEvent& _rEvent ) override;
319 private:
320 DECL_LINK( OnTimeout, Timer*, void );
327 #endif // INCLUDED_FORMS_SOURCE_COMPONENT_LISTBOX_HXX
329 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */