bump product version to 5.0.4.1
[LibreOffice.git] / forms / source / component / Columns.cxx
blob944374e1ed2d1acfc8bee81b6098dd79716e3473
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 <string.h>
22 #include "Columns.hxx"
23 #include "property.hrc"
24 #include "property.hxx"
25 #include "componenttools.hxx"
26 #include "findpos.hxx"
27 #include <com/sun/star/io/XPersistObject.hpp>
28 #include <com/sun/star/io/XObjectOutputStream.hpp>
29 #include <com/sun/star/io/XObjectInputStream.hpp>
30 #include <com/sun/star/io/XMarkableStream.hpp>
31 #include <com/sun/star/form/XFormComponent.hpp>
32 #include <com/sun/star/lang/XServiceInfo.hpp>
33 #include <com/sun/star/form/binding/XBindableValue.hpp>
34 #include <com/sun/star/beans/XPropertyContainer.hpp>
35 #include <com/sun/star/text/XText.hpp>
36 #include <comphelper/sequence.hxx>
37 #include <comphelper/property.hxx>
38 #include <comphelper/basicio.hxx>
39 #include <comphelper/types.hxx>
40 #include <comphelper/servicehelper.hxx>
41 #include "services.hxx"
42 #include "frm_resource.hrc"
43 #include <tools/debug.hxx>
46 namespace frm
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::beans;
51 using namespace ::com::sun::star::container;
52 using namespace ::com::sun::star::form;
53 using namespace ::com::sun::star::awt;
54 using namespace ::com::sun::star::io;
55 using namespace ::com::sun::star::lang;
56 using namespace ::com::sun::star::util;
57 using namespace ::com::sun::star::text;
58 using namespace ::com::sun::star::form::binding;
60 const sal_uInt16 WIDTH = 0x0001;
61 const sal_uInt16 ALIGN = 0x0002;
62 const sal_uInt16 OLD_HIDDEN = 0x0004;
63 const sal_uInt16 COMPATIBLE_HIDDEN = 0x0008;
66 const StringSequence& getColumnTypes()
68 static StringSequence aColumnTypes(10);
69 if (aColumnTypes.getConstArray()[0].isEmpty())
71 OUString* pNames = aColumnTypes.getArray();
72 pNames[TYPE_CHECKBOX] = "CheckBox";
73 pNames[TYPE_COMBOBOX] = "ComboBox";
74 pNames[TYPE_CURRENCYFIELD] = "CurrencyField";
75 pNames[TYPE_DATEFIELD] = "DateField";
76 pNames[TYPE_FORMATTEDFIELD] = "FormattedField";
77 pNames[TYPE_LISTBOX] = "ListBox";
78 pNames[TYPE_NUMERICFIELD] = "NumericField";
79 pNames[TYPE_PATTERNFIELD] = "PatternField";
80 pNames[TYPE_TEXTFIELD] = "TextField";
81 pNames[TYPE_TIMEFIELD] = "TimeField";
83 return aColumnTypes;
87 sal_Int32 getColumnTypeByModelName(const OUString& aModelName)
89 const OUString aModelPrefix ("com.sun.star.form.component.");
90 const OUString aCompatibleModelPrefix ("stardiv.one.form.component.");
92 sal_Int32 nTypeId = -1;
93 if (aModelName == FRM_COMPONENT_EDIT)
94 nTypeId = TYPE_TEXTFIELD;
95 else
97 sal_Int32 nPrefixPos = aModelName.indexOf(aModelPrefix);
98 #ifdef DBG_UTIL
99 sal_Int32 nCompatiblePrefixPos = aModelName.indexOf(aCompatibleModelPrefix);
100 #endif
101 DBG_ASSERT( (nPrefixPos != -1) || (nCompatiblePrefixPos != -1),
102 "::getColumnTypeByModelName() : wrong servivce !");
104 OUString aColumnType = (nPrefixPos != -1)
105 ? aModelName.copy(aModelPrefix.getLength())
106 : aModelName.copy(aCompatibleModelPrefix.getLength());
108 const StringSequence& rColumnTypes = getColumnTypes();
109 nTypeId = ::detail::findPos(aColumnType, rColumnTypes);
111 return nTypeId;
114 namespace
116 class theOGridColumnImplementationId : public rtl::Static< UnoTunnelIdInit, theOGridColumnImplementationId > {};
119 const Sequence<sal_Int8>& OGridColumn::getUnoTunnelImplementationId()
121 return theOGridColumnImplementationId::get().getSeq();
125 sal_Int64 SAL_CALL OGridColumn::getSomething( const Sequence<sal_Int8>& _rIdentifier) throw(RuntimeException, std::exception)
127 sal_Int64 nReturn(0);
129 if ( (_rIdentifier.getLength() == 16)
130 && (0 == memcmp( getUnoTunnelImplementationId().getConstArray(), _rIdentifier.getConstArray(), 16 ))
133 nReturn = reinterpret_cast<sal_Int64>(this);
135 else
137 Reference< XUnoTunnel > xAggTunnel;
138 if ( query_aggregation( m_xAggregate, xAggTunnel ) )
139 return xAggTunnel->getSomething( _rIdentifier );
141 return nReturn;
145 Sequence<sal_Int8> SAL_CALL OGridColumn::getImplementationId() throw(RuntimeException, std::exception)
147 return css::uno::Sequence<sal_Int8>();
151 Sequence<Type> SAL_CALL OGridColumn::getTypes() throw(RuntimeException, std::exception)
153 TypeBag aTypes( OGridColumn_BASE::getTypes() );
154 // erase the types which we do not support
155 aTypes.removeType( cppu::UnoType<XFormComponent>::get() );
156 aTypes.removeType( cppu::UnoType<XServiceInfo>::get() );
157 aTypes.removeType( cppu::UnoType<XBindableValue>::get() );
158 aTypes.removeType( cppu::UnoType<XPropertyContainer>::get() );
160 // but re-add their base class(es)
161 aTypes.addType( cppu::UnoType<XChild>::get() );
163 Reference< XTypeProvider > xProv;
164 if ( query_aggregation( m_xAggregate, xProv ))
165 aTypes.addTypes( xProv->getTypes() );
167 aTypes.removeType( cppu::UnoType<XTextRange>::get() );
168 aTypes.removeType( cppu::UnoType<XSimpleText>::get() );
169 aTypes.removeType( cppu::UnoType<XText>::get() );
171 return aTypes.getTypes();
175 Any SAL_CALL OGridColumn::queryAggregation( const Type& _rType ) throw (RuntimeException, std::exception)
177 Any aReturn;
178 // some functionality at our aggregate cannot be reasonably fulfilled here.
179 if ( _rType.equals(cppu::UnoType<XFormComponent>::get())
180 || _rType.equals(cppu::UnoType<XServiceInfo>::get())
181 || _rType.equals(cppu::UnoType<XBindableValue>::get())
182 || _rType.equals(cppu::UnoType<XPropertyContainer>::get())
183 || comphelper::isAssignableFrom(cppu::UnoType<XTextRange>::get(),_rType)
185 return aReturn;
187 aReturn = OGridColumn_BASE::queryAggregation(_rType);
188 if (!aReturn.hasValue())
190 aReturn = OPropertySetAggregationHelper::queryInterface(_rType);
191 if (!aReturn.hasValue() && m_xAggregate.is())
192 aReturn = m_xAggregate->queryAggregation(_rType);
195 return aReturn;
199 OGridColumn::OGridColumn( const Reference<XComponentContext>& _rContext, const OUString& _sModelName )
200 :OGridColumn_BASE(m_aMutex)
201 ,OPropertySetAggregationHelper(OGridColumn_BASE::rBHelper)
202 ,m_aHidden( makeAny( sal_False ) )
203 ,m_aModelName(_sModelName)
206 // Create the UnoControlModel
207 if ( !m_aModelName.isEmpty() ) // is there a to-be-aggregated model?
209 osl_atomic_increment( &m_refCount );
212 m_xAggregate.set( _rContext->getServiceManager()->createInstanceWithContext( m_aModelName, _rContext ), UNO_QUERY );
213 setAggregation( m_xAggregate );
216 if ( m_xAggregate.is() )
217 { // don't omit those brackets - they ensure that the following temporary is properly deleted
218 m_xAggregate->setDelegator( static_cast< ::cppu::OWeakObject* >( this ) );
221 // Set refcount back to zero
222 osl_atomic_decrement( &m_refCount );
227 OGridColumn::OGridColumn( const OGridColumn* _pOriginal )
228 :OGridColumn_BASE( m_aMutex )
229 ,OPropertySetAggregationHelper( OGridColumn_BASE::rBHelper )
232 m_aWidth = _pOriginal->m_aWidth;
233 m_aAlign = _pOriginal->m_aAlign;
234 m_aHidden = _pOriginal->m_aHidden;
235 m_aModelName = _pOriginal->m_aModelName;
236 m_aLabel = _pOriginal->m_aLabel;
238 osl_atomic_increment( &m_refCount );
241 m_xAggregate = createAggregateClone( _pOriginal );
242 setAggregation( m_xAggregate );
245 if ( m_xAggregate.is() )
246 { // don't omit this brackets - they ensure that the following temporary is properly deleted
247 m_xAggregate->setDelegator( static_cast< ::cppu::OWeakObject* >( this ) );
250 osl_atomic_decrement( &m_refCount );
254 OGridColumn::~OGridColumn()
256 if (!OGridColumn_BASE::rBHelper.bDisposed)
258 acquire();
259 dispose();
262 // Free the aggregate
263 if (m_xAggregate.is())
265 InterfaceRef xIface;
266 m_xAggregate->setDelegator(xIface);
271 // XEventListener
273 void SAL_CALL OGridColumn::disposing(const EventObject& _rSource) throw(RuntimeException, std::exception)
275 OPropertySetAggregationHelper::disposing(_rSource);
277 Reference<XEventListener> xEvtLstner;
278 if (query_aggregation(m_xAggregate, xEvtLstner))
279 xEvtLstner->disposing(_rSource);
282 // OGridColumn_BASE
284 void OGridColumn::disposing()
286 OGridColumn_BASE::disposing();
287 OPropertySetAggregationHelper::disposing();
289 Reference<XComponent> xComp;
290 if (query_aggregation(m_xAggregate, xComp))
291 xComp->dispose();
295 void OGridColumn::clearAggregateProperties( Sequence< Property >& _rProps, bool bAllowDropDown )
297 // some properties are not to be exposed to the outer world
298 ::std::set< OUString > aForbiddenProperties;
299 aForbiddenProperties.insert( PROPERTY_ALIGN );
300 aForbiddenProperties.insert( PROPERTY_AUTOCOMPLETE );
301 aForbiddenProperties.insert( PROPERTY_BACKGROUNDCOLOR );
302 aForbiddenProperties.insert( PROPERTY_BORDER );
303 aForbiddenProperties.insert( PROPERTY_BORDERCOLOR );
304 aForbiddenProperties.insert( PROPERTY_ECHO_CHAR );
305 aForbiddenProperties.insert( PROPERTY_FILLCOLOR );
306 aForbiddenProperties.insert( PROPERTY_FONT );
307 aForbiddenProperties.insert( PROPERTY_FONT_NAME );
308 aForbiddenProperties.insert( PROPERTY_FONT_STYLENAME );
309 aForbiddenProperties.insert( PROPERTY_FONT_FAMILY );
310 aForbiddenProperties.insert( PROPERTY_FONT_CHARSET );
311 aForbiddenProperties.insert( PROPERTY_FONT_HEIGHT );
312 aForbiddenProperties.insert( PROPERTY_FONT_WEIGHT );
313 aForbiddenProperties.insert( PROPERTY_FONT_SLANT );
314 aForbiddenProperties.insert( PROPERTY_FONT_UNDERLINE );
315 aForbiddenProperties.insert( PROPERTY_FONT_STRIKEOUT );
316 aForbiddenProperties.insert( PROPERTY_FONT_WORDLINEMODE );
317 aForbiddenProperties.insert( PROPERTY_TEXTLINECOLOR );
318 aForbiddenProperties.insert( PROPERTY_FONTEMPHASISMARK );
319 aForbiddenProperties.insert( PROPERTY_FONTRELIEF );
320 aForbiddenProperties.insert( PROPERTY_HARDLINEBREAKS );
321 aForbiddenProperties.insert( PROPERTY_HSCROLL );
322 aForbiddenProperties.insert( PROPERTY_LABEL );
323 aForbiddenProperties.insert( PROPERTY_LINECOLOR );
324 aForbiddenProperties.insert( PROPERTY_MULTISELECTION );
325 aForbiddenProperties.insert( PROPERTY_PRINTABLE );
326 aForbiddenProperties.insert( PROPERTY_TABINDEX );
327 aForbiddenProperties.insert( PROPERTY_TABSTOP );
328 aForbiddenProperties.insert( PROPERTY_TEXTCOLOR );
329 aForbiddenProperties.insert( PROPERTY_VSCROLL );
330 aForbiddenProperties.insert( PROPERTY_CONTROLLABEL );
331 aForbiddenProperties.insert( PROPERTY_RICH_TEXT );
332 aForbiddenProperties.insert( PROPERTY_VERTICAL_ALIGN );
333 aForbiddenProperties.insert( PROPERTY_IMAGE_URL );
334 aForbiddenProperties.insert( PROPERTY_IMAGE_POSITION );
335 aForbiddenProperties.insert( OUString( "EnableVisible" ) );
336 if ( !bAllowDropDown )
337 aForbiddenProperties.insert( PROPERTY_DROPDOWN );
339 Sequence< Property > aNewProps( _rProps.getLength() );
340 Property* pNewProps = aNewProps.getArray();
342 const Property* pProps = _rProps.getConstArray();
343 const Property* pPropsEnd = pProps + _rProps.getLength();
344 for ( ; pProps != pPropsEnd; ++pProps )
346 if ( aForbiddenProperties.find( pProps->Name ) == aForbiddenProperties.end() )
347 *pNewProps++ = *pProps;
350 aNewProps.realloc( pNewProps - aNewProps.getArray() );
351 _rProps = aNewProps;
355 void OGridColumn::setOwnProperties(Sequence<Property>& aDescriptor)
357 aDescriptor.realloc(5);
358 Property* pProperties = aDescriptor.getArray();
359 DECL_PROP1(LABEL, OUString, BOUND);
360 DECL_PROP3(WIDTH, sal_Int32, BOUND, MAYBEVOID, MAYBEDEFAULT);
361 DECL_PROP3(ALIGN, sal_Int16, BOUND, MAYBEVOID, MAYBEDEFAULT);
362 DECL_BOOL_PROP2(HIDDEN, BOUND, MAYBEDEFAULT);
363 DECL_PROP1(COLUMNSERVICENAME, OUString, READONLY);
366 // Reference<XPropertySet>
368 void OGridColumn::getFastPropertyValue(Any& rValue, sal_Int32 nHandle ) const
370 switch (nHandle)
372 case PROPERTY_ID_COLUMNSERVICENAME:
373 rValue <<= m_aModelName;
374 break;
375 case PROPERTY_ID_LABEL:
376 rValue <<= m_aLabel;
377 break;
378 case PROPERTY_ID_WIDTH:
379 rValue = m_aWidth;
380 break;
381 case PROPERTY_ID_ALIGN:
382 rValue = m_aAlign;
383 break;
384 case PROPERTY_ID_HIDDEN:
385 rValue = m_aHidden;
386 break;
387 default:
388 OPropertySetAggregationHelper::getFastPropertyValue(rValue, nHandle);
393 sal_Bool OGridColumn::convertFastPropertyValue( Any& rConvertedValue, Any& rOldValue,
394 sal_Int32 nHandle, const Any& rValue )throw( IllegalArgumentException )
396 bool bModified(false);
397 switch (nHandle)
399 case PROPERTY_ID_LABEL:
400 bModified = tryPropertyValue(rConvertedValue, rOldValue, rValue, m_aLabel);
401 break;
402 case PROPERTY_ID_WIDTH:
403 bModified = tryPropertyValue(rConvertedValue, rOldValue, rValue, m_aWidth, cppu::UnoType<sal_Int32>::get());
404 break;
405 case PROPERTY_ID_ALIGN:
406 bModified = tryPropertyValue( rConvertedValue, rOldValue, rValue, m_aAlign, cppu::UnoType<sal_Int32>::get());
407 // strange enough, css.awt.TextAlign is a 32-bit integer, while the Align property (both here for grid controls
408 // and for ordinary toolkit controls) is a 16-bit integer. So, allow for 32 bit, but normalize it to 16 bit
409 if ( bModified )
411 sal_Int32 nAlign( 0 );
412 if ( rConvertedValue >>= nAlign )
413 rConvertedValue <<= (sal_Int16)nAlign;
415 break;
416 case PROPERTY_ID_HIDDEN:
417 bModified = tryPropertyValue(rConvertedValue, rOldValue, rValue, getBOOL(m_aHidden));
418 break;
420 return bModified;
424 void OGridColumn::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue ) throw (::com::sun::star::uno::Exception, std::exception)
426 switch (nHandle)
428 case PROPERTY_ID_LABEL:
429 DBG_ASSERT(rValue.getValueType().getTypeClass() == TypeClass_STRING, "invalid type" );
430 rValue >>= m_aLabel;
431 break;
432 case PROPERTY_ID_WIDTH:
433 m_aWidth = rValue;
434 break;
435 case PROPERTY_ID_ALIGN:
436 m_aAlign = rValue;
437 break;
438 case PROPERTY_ID_HIDDEN:
439 m_aHidden = rValue;
440 break;
445 // XPropertyState
447 Any OGridColumn::getPropertyDefaultByHandle( sal_Int32 nHandle ) const
449 switch (nHandle)
451 case PROPERTY_ID_WIDTH:
452 case PROPERTY_ID_ALIGN:
453 return Any();
454 case PROPERTY_ID_HIDDEN:
455 return makeAny(false);
456 default:
457 return OPropertySetAggregationHelper::getPropertyDefaultByHandle(nHandle);
461 // XCloneable
463 Reference< XCloneable > SAL_CALL OGridColumn::createClone( ) throw (RuntimeException, std::exception)
465 OGridColumn* pNewColumn = createCloneColumn();
466 return pNewColumn;
469 // XPersistObject
471 void SAL_CALL OGridColumn::write(const Reference<XObjectOutputStream>& _rxOutStream)
473 // 1. Write the UnoControl
474 Reference<XMarkableStream> xMark(_rxOutStream, UNO_QUERY);
475 sal_Int32 nMark = xMark->createMark();
477 sal_Int32 nLen = 0;
478 _rxOutStream->writeLong(nLen);
480 Reference<XPersistObject> xPersist;
481 if (query_aggregation(m_xAggregate, xPersist))
482 xPersist->write(_rxOutStream);
484 // Calculate the length
485 nLen = xMark->offsetToMark(nMark) - 4;
486 xMark->jumpToMark(nMark);
487 _rxOutStream->writeLong(nLen);
488 xMark->jumpToFurthest();
489 xMark->deleteMark(nMark);
491 // 2. Write a version number
492 _rxOutStream->writeShort(0x0002);
494 sal_uInt16 nAnyMask = 0;
495 if (m_aWidth.getValueType().getTypeClass() == TypeClass_LONG)
496 nAnyMask |= WIDTH;
498 if (m_aAlign.getValueTypeClass() == TypeClass_SHORT)
499 nAnyMask |= ALIGN;
501 nAnyMask |= COMPATIBLE_HIDDEN;
503 _rxOutStream->writeShort(nAnyMask);
505 if (nAnyMask & WIDTH)
506 _rxOutStream->writeLong(getINT32(m_aWidth));
507 if (nAnyMask & ALIGN)
508 _rxOutStream->writeShort(getINT16(m_aAlign));
510 // Name
511 _rxOutStream << m_aLabel;
513 // the new place for the hidden flag : after m_aLabel, so older office version read the correct label, too
514 if (nAnyMask & COMPATIBLE_HIDDEN)
515 _rxOutStream->writeBoolean(getBOOL(m_aHidden));
519 void SAL_CALL OGridColumn::read(const Reference<XObjectInputStream>& _rxInStream)
521 // 1. Read the UnoControl
522 sal_Int32 nLen = _rxInStream->readLong();
523 if (nLen)
525 Reference<XMarkableStream> xMark(_rxInStream, UNO_QUERY);
526 sal_Int32 nMark = xMark->createMark();
527 Reference<XPersistObject> xPersist;
528 if (query_aggregation(m_xAggregate, xPersist))
529 xPersist->read(_rxInStream);
531 xMark->jumpToMark(nMark);
532 _rxInStream->skipBytes(nLen);
533 xMark->deleteMark(nMark);
536 // 2. Write a version number
537 sal_uInt16 nVersion = _rxInStream->readShort(); (void)nVersion;
538 sal_uInt16 nAnyMask = _rxInStream->readShort();
540 if (nAnyMask & WIDTH)
542 sal_Int32 nValue = _rxInStream->readLong();
543 m_aWidth <<= (sal_Int32)nValue;
546 if (nAnyMask & ALIGN)
548 sal_Int16 nValue = _rxInStream->readShort();
549 m_aAlign <<= nValue;
551 if (nAnyMask & OLD_HIDDEN)
553 bool bValue = _rxInStream->readBoolean();
554 m_aHidden <<= bValue;
557 // Name
558 _rxInStream >> m_aLabel;
560 if (nAnyMask & COMPATIBLE_HIDDEN)
562 bool bValue = _rxInStream->readBoolean();
563 m_aHidden <<= bValue;
568 IMPL_COLUMN(TextFieldColumn, FRM_SUN_COMPONENT_TEXTFIELD, false);
569 IMPL_COLUMN(PatternFieldColumn, FRM_SUN_COMPONENT_PATTERNFIELD, false);
570 IMPL_COLUMN(DateFieldColumn, FRM_SUN_COMPONENT_DATEFIELD, true);
571 IMPL_COLUMN(TimeFieldColumn, FRM_SUN_COMPONENT_TIMEFIELD, false);
572 IMPL_COLUMN(NumericFieldColumn, FRM_SUN_COMPONENT_NUMERICFIELD, false);
573 IMPL_COLUMN(CurrencyFieldColumn, FRM_SUN_COMPONENT_CURRENCYFIELD, false);
574 IMPL_COLUMN(CheckBoxColumn, FRM_SUN_COMPONENT_CHECKBOX, false);
575 IMPL_COLUMN(ComboBoxColumn, FRM_SUN_COMPONENT_COMBOBOX, false);
576 IMPL_COLUMN(ListBoxColumn, FRM_SUN_COMPONENT_LISTBOX, false);
577 IMPL_COLUMN(FormattedFieldColumn, FRM_SUN_COMPONENT_FORMATTEDFIELD, false);
580 } // namespace frm
583 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */