LibreOffice Module forms (master) 1
FormattedFieldWrapper.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
21#include "Edit.hxx"
22#include "FormattedField.hxx"
23#include <services.hxx>
27#include <tools/debug.hxx>
28#include <vcl/svapp.hxx>
29#include <vcl/settings.hxx>
31#include <com/sun/star/io/XMarkableStream.hpp>
32
33using namespace comphelper;
34using namespace frm;
35using namespace ::com::sun::star::uno;
36using namespace ::com::sun::star::sdb;
37using namespace ::com::sun::star::sdbc;
38using namespace ::com::sun::star::sdbcx;
39using namespace ::com::sun::star::beans;
40using namespace ::com::sun::star::container;
41using namespace ::com::sun::star::form;
42using namespace ::com::sun::star::awt;
43using namespace ::com::sun::star::io;
44using namespace ::com::sun::star::lang;
45using namespace ::com::sun::star::util;
46
47OFormattedFieldWrapper::OFormattedFieldWrapper(const Reference<XComponentContext>& _rxFactory,
48 OUString const & implementationName)
49 :m_xContext(_rxFactory)
50 ,m_implementationName(implementationName)
51{
52}
53
54css::uno::Reference<css::uno::XInterface> OFormattedFieldWrapper::createFormattedFieldWrapper(const css::uno::Reference< css::uno::XComponentContext>& _rxFactory, bool bActAsFormatted, OUString const & implementationName)
55{
58
59 if (bActAsFormatted)
60 {
61 // instantiate a FormattedModel
62 // (instantiate it directly ..., as the OFormattedModel isn't
63 // registered for any service names anymore)
64 rtl::Reference<OFormattedModel> pModel = new OFormattedModel(pRef->m_xContext);
65
66 pRef->m_xAggregate = pModel;
67 OSL_ENSURE(pRef->m_xAggregate.is(), "the OFormattedModel didn't have an XAggregation interface !");
68
69 // _before_ setting the delegator, give it to the member references
70 pRef->m_xFormattedPart = pModel;
71 pRef->m_pEditPart.set(new OEditModel(pRef->m_xContext));
72 }
73
74 if (pRef->m_xAggregate.is())
75 { // has to be in its own block because of the temporary variable created by *this
76 pRef->m_xAggregate->setDelegator(static_cast<XWeak*>(pRef.get()));
77 }
78
79 css::uno::Reference<css::uno::XInterface> xRef(*pRef);
80
81 return xRef;
82}
83
84Reference< XCloneable > SAL_CALL OFormattedFieldWrapper::createClone()
85{
87
90
91 Reference< XCloneable > xCloneAccess;
92 query_aggregation( m_xAggregate, xCloneAccess );
93
94 // clone the aggregate
95 if ( xCloneAccess.is() )
96 {
97 Reference< XCloneable > xClone = xCloneAccess->createClone();
98 xRef->m_xAggregate.set(xClone, UNO_QUERY);
99 OSL_ENSURE(xRef->m_xAggregate.is(), "invalid aggregate cloned !");
100
101 xRef->m_xFormattedPart.set(
102 Reference< XInterface >(xClone), css::uno::UNO_QUERY);
103
104 if ( m_pEditPart.is() )
105 {
106 xRef->m_pEditPart.set( new OEditModel(m_pEditPart.get(), m_xContext) );
107 }
108 }
109 else
110 { // the clone source does not yet have an aggregate -> we don't yet need one, too
111 }
112
113 if ( xRef->m_xAggregate.is() )
114 { // has to be in its own block because of the temporary variable created by *this
115 xRef->m_xAggregate->setDelegator(static_cast< XWeak* >(xRef.get()));
116 }
117
118 return xRef;
119}
120
122{
123 // release the aggregated object (if any)
124 if (m_xAggregate.is())
125 m_xAggregate->setDelegator(css::uno::Reference<css::uno::XInterface> ());
126
127}
128
130{
131 Any aReturn;
132
133 if (_rType.equals( cppu::UnoType<XTypeProvider>::get() ) )
134 { // a XTypeProvider interface needs a working aggregate - we don't want to give the type provider
135 // of our base class (OFormattedFieldWrapper_Base) to the caller as it supplies nearly nothing
137 if (m_xAggregate.is())
138 aReturn = m_xAggregate->queryAggregation(_rType);
139 }
140
141 if (!aReturn.hasValue())
142 {
144
145 if ((_rType.equals( cppu::UnoType<XServiceInfo>::get() ) ) && aReturn.hasValue())
146 { // somebody requested an XServiceInfo interface and our base class provided it
147 // check our aggregate if it has one, too
149 }
150
151 if (!aReturn.hasValue())
152 {
153 aReturn = ::cppu::queryInterface( _rType,
154 static_cast< XPersistObject* >( this ),
155 static_cast< XCloneable* >( this )
156 );
157
158 if (!aReturn.hasValue())
159 {
160 // somebody requests an interface other than the basics (XInterface) and other than
161 // the two we can supply without an aggregate. So ensure
162 // the aggregate exists.
164 if (m_xAggregate.is())
165 aReturn = m_xAggregate->queryAggregation(_rType);
166 }
167 }
168 }
169
170 return aReturn;
171}
172
174{
175 // return the old compatibility name for an EditModel
176 return FRM_COMPONENT_EDIT;
177}
178
180{
182}
183
184sal_Bool SAL_CALL OFormattedFieldWrapper::supportsService( const OUString& _rServiceName )
185{
186 return cppu::supportsService(this, _rServiceName);
187}
188
190{
191 DBG_ASSERT(m_xAggregate.is(), "OFormattedFieldWrapper::getSupportedServiceNames: should never have made it 'til here without an aggregate!");
192 Reference< XServiceInfo > xSI;
193 m_xAggregate->queryAggregation(cppu::UnoType<XServiceInfo>::get()) >>= xSI;
194 return xSI->getSupportedServiceNames();
195}
196
197void SAL_CALL OFormattedFieldWrapper::write(const Reference<XObjectOutputStream>& _rxOutStream)
198{
199 // can't write myself
201
202 // if we act as real edit field, we can simple forward this write request
203 if (!m_xFormattedPart.is())
204 {
205 Reference<XPersistObject> xAggregatePersistence;
206 query_aggregation(m_xAggregate, xAggregatePersistence);
207 DBG_ASSERT(xAggregatePersistence.is(), "OFormattedFieldWrapper::write : don't know how to handle this : can't write !");
208 // oops ... We gave an XPersistObject interface to the caller but now we aren't an XPersistObject ...
209 if (xAggregatePersistence.is())
210 xAggregatePersistence->write(_rxOutStream);
211 return;
212 }
213
214 // else we have to write an edit part first
215 OSL_ENSURE(m_pEditPart.is(), "OFormattedFieldWrapper::write : formatted part without edit part ?");
216 if ( !m_pEditPart.is() )
217 throw RuntimeException( OUString(), *this );
218
219 // for this we transfer the current props of the formatted part to the edit part
220 Reference<XPropertySet> xFormatProps(m_xFormattedPart, UNO_QUERY);
221 Reference<XPropertySet> xEditProps = m_pEditPart;
222
224 dbtools::TransferFormComponentProperties(xFormatProps, xEditProps, aAppLanguage);
225
226 // then write the edit part, after switching to "fake mode"
227 m_pEditPart->enableFormattedWriteFake();
228 m_pEditPart->write(_rxOutStream);
229 m_pEditPart->disableFormattedWriteFake();
230
231 // and finally write the formatted part we're really interested in
232 m_xFormattedPart->write(_rxOutStream);
233}
234
235void SAL_CALL OFormattedFieldWrapper::read(const Reference<XObjectInputStream>& _rxInStream)
236{
238 if (m_xAggregate.is())
239 { // we already made a decision if we're an EditModel or a FormattedModel
240
241 // if we act as formatted, we have to read the edit part first
242 if (m_xFormattedPart.is())
243 {
244 // two possible cases:
245 // a) the stuff was written by a version which didn't work with an Edit header (all intermediate
246 // versions >5.1 && <=568)
247 // b) it was written by a version using edit headers
248 // as we can distinguish a) from b) only after we have read the edit part, we need to remember the
249 // position
250 Reference<XMarkableStream> xInMarkable(_rxInStream, UNO_QUERY);
251 DBG_ASSERT(xInMarkable.is(), "OFormattedFieldWrapper::read : can only work with markable streams !");
252 sal_Int32 nBeforeEditPart = xInMarkable->createMark();
253
254 m_pEditPart->read(_rxInStream);
255 // this only works because an edit model can read the stuff written by a formatted model
256 // (maybe with some assertions) , but not vice versa
257 if (!m_pEditPart->lastReadWasFormattedFake())
258 { // case a), written with a version without the edit part fake, so seek to the start position, again
259 xInMarkable->jumpToMark(nBeforeEditPart);
260 }
261 xInMarkable->deleteMark(nBeforeEditPart);
262 }
263
264 Reference<XPersistObject> xAggregatePersistence;
265 query_aggregation(m_xAggregate, xAggregatePersistence);
266 DBG_ASSERT(xAggregatePersistence.is(), "OFormattedFieldWrapper::read : don't know how to handle this : can't read !");
267 // oops ... We gave an XPersistObject interface to the caller but now we aren't an XPersistObject ...
268
269 if (xAggregatePersistence.is())
270 xAggregatePersistence->read(_rxInStream);
271 return;
272 }
273
274 // we have to decide from the data within the stream whether we should
275 // be an EditModel or a FormattedModel
276
277 {
278 // let an OEditModel do the reading
280 pBasicReader->read(_rxInStream);
281
282 // was it really an edit model ?
283 if (!pBasicReader->lastReadWasFormattedFake())
284 {
285 // yes -> all fine
286 m_xAggregate = pBasicReader;
287 }
288 else
289 { // no -> substitute it with a formatted model
290 // let the formatted model do the reading
292 m_xFormattedPart->read(_rxInStream);
293 m_pEditPart = pBasicReader;
294 m_xAggregate.set( m_xFormattedPart, UNO_QUERY );
295 }
296 }
297
298 // do the aggregation
299 osl_atomic_increment(&m_refCount);
300 if (m_xAggregate.is())
301 { // has to be in its own block because of the temporary variable created by *this
302 m_xAggregate->setDelegator(static_cast<XWeak*>(this));
303 }
304 osl_atomic_decrement(&m_refCount);
305}
306
308{
309 if (m_xAggregate.is())
310 return;
311
312 {
313 // instantiate an EditModel (the only place where we are allowed to decide that we're a FormattedModel
314 // is in ::read)
315 css::uno::Reference<css::uno::XInterface> xEditModel = m_xContext->getServiceManager()->createInstanceWithContext(FRM_SUN_COMPONENT_TEXTFIELD, m_xContext);
316 if (!xEditModel.is())
317 {
318 // arghhh... instantiate it directly... it's dirty, but we really need this aggregate
320 xEditModel.set(static_cast<XWeak*>(pModel.get()), css::uno::UNO_QUERY);
321 }
322
323 m_xAggregate.set(xEditModel, UNO_QUERY);
324 DBG_ASSERT(m_xAggregate.is(), "OFormattedFieldWrapper::ensureAggregate : the OEditModel didn't have an XAggregation interface !");
325
326 {
327 Reference< XServiceInfo > xSI(m_xAggregate, UNO_QUERY);
328 if (!xSI.is())
329 {
330 OSL_FAIL("OFormattedFieldWrapper::ensureAggregate: the aggregate has no XServiceInfo!");
331 m_xAggregate.clear();
332 }
333 }
334 }
335
336 osl_atomic_increment(&m_refCount);
337 if (m_xAggregate.is())
338 { // has to be in its own block because of the temporary variable created by *this
339 m_xAggregate->setDelegator(static_cast<XWeak*>(this));
340 }
341 osl_atomic_decrement(&m_refCount);
342}
343
344extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
346 css::uno::Sequence<css::uno::Any> const &)
347{
348 css::uno::Reference<css::uno::XInterface> inst(
350 component, false, "com.sun.star.form.OFormattedFieldWrapper"));
351 inst->acquire();
352 return inst.get();
353}
354
355extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
357 css::uno::Sequence<css::uno::Any> const &)
358{
359 css::uno::Reference<css::uno::XInterface> inst(
361 component, true, "com.sun.star.comp.forms.OFormattedFieldWrapper_ForcedFormatted"));
362 inst->acquire();
363 return inst.get();
364}
365
366/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_form_OFormattedFieldWrapper_get_implementation(css::uno::XComponentContext *component, css::uno::Sequence< css::uno::Any > const &)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_forms_OFormattedFieldWrapper_ForcedFormatted_get_implementation(css::uno::XComponentContext *component, css::uno::Sequence< css::uno::Any > const &)
Reference< XComponentContext > m_xContext
const LanguageTag & GetUILanguageTag() const
static const AllSettings & GetSettings()
const css::lang::Locale & getLocale(bool bResolveSystem=true) const
oslInterlockedCount m_refCount
virtual css::uno::Any SAL_CALL queryAggregation(css::uno::Type const &rType) SAL_OVERRIDE
static css::uno::Reference< css::uno::XInterface > createFormattedFieldWrapper(const css::uno::Reference< css::uno::XComponentContext > &_rxFactory, bool bActAsFormatted, OUString const &implementationName)
virtual void SAL_CALL read(const css::uno::Reference< css::io::XObjectInputStream > &_rxInStream) override
css::uno::Reference< css::uno::XComponentContext > m_xContext
rtl::Reference< OEditModel > m_pEditPart
css::uno::Reference< css::io::XPersistObject > m_xFormattedPart
virtual ~OFormattedFieldWrapper() override
virtual css::uno::Reference< css::util::XCloneable > SAL_CALL createClone() override
virtual void SAL_CALL write(const css::uno::Reference< css::io::XObjectOutputStream > &_rxOutStream) override
OFormattedFieldWrapper(const css::uno::Reference< css::uno::XComponentContext > &_rxFactory, OUString const &implementationName)
void ensureAggregate()
ensure we're in a defined state, which means a FormattedModel OR an EditModel
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
css::uno::Reference< css::uno::XAggregation > m_xAggregate
virtual OUString SAL_CALL getServiceName() override
virtual css::uno::Any SAL_CALL queryAggregation(const css::uno::Type &_rType) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual OUString SAL_CALL getImplementationName() override
#define DBG_ASSERT(sCon, aError)
bool query_aggregation(const css::uno::Reference< css::uno::XAggregation > &_rxAggregate, css::uno::Reference< iface > &_rxOut)
Type
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
void TransferFormComponentProperties(const Reference< XPropertySet > &xOldProps, const Reference< XPropertySet > &xNewProps, const Locale &_rLocale)
ListBox is a bit confusing / different from other form components, so here are a few notes:
Definition: BaseListBox.hxx:25
constexpr OUStringLiteral implementationName
constexpr OUStringLiteral FRM_SUN_COMPONENT_TEXTFIELD
Definition: services.hxx:113
constexpr OUStringLiteral FRM_COMPONENT_EDIT
Definition: services.hxx:65
unsigned char sal_Bool