Linux x86 build fix
[LibreOffice.git] / extensions / source / propctrlr / formgeometryhandler.cxx
blob0196d3f8dae2a69422ed2d2b361274cc4e0eddce
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 <sal/config.h>
22 #include "pcrservices.hxx"
23 #include "propertyhandler.hxx"
24 #include "formmetadata.hxx"
25 #include "formstrings.hxx"
26 #include "handlerhelper.hxx"
27 #include "cellbindinghelper.hxx"
29 #include <com/sun/star/inspection/XObjectInspectorUI.hpp>
30 #include <com/sun/star/awt/XControlModel.hpp>
31 #include <com/sun/star/drawing/XControlShape.hpp>
32 #include <com/sun/star/container/XMap.hpp>
33 #include <com/sun/star/inspection/XNumericControl.hpp>
34 #include <com/sun/star/util/MeasureUnit.hpp>
35 #include <com/sun/star/text/TextContentAnchorType.hpp>
36 #include <com/sun/star/lang/XServiceInfo.hpp>
37 #include <com/sun/star/sheet/XSpreadsheet.hpp>
38 #include <com/sun/star/table/XColumnRowRange.hpp>
39 #include <com/sun/star/table/XCellRange.hpp>
40 #include <com/sun/star/container/XChild.hpp>
41 #include <com/sun/star/form/XGridColumnFactory.hpp>
43 #include <cppuhelper/interfacecontainer.hxx>
44 #include <comphelper/componentbase.hxx>
45 #include <tools/diagnose_ex.h>
47 namespace pcr
51 using ::com::sun::star::uno::Reference;
52 using ::com::sun::star::uno::XInterface;
53 using ::com::sun::star::uno::UNO_QUERY;
54 using ::com::sun::star::uno::UNO_QUERY_THROW;
55 using ::com::sun::star::uno::UNO_SET_THROW;
56 using ::com::sun::star::uno::Exception;
57 using ::com::sun::star::uno::RuntimeException;
58 using ::com::sun::star::uno::Any;
59 using ::com::sun::star::uno::makeAny;
60 using ::com::sun::star::uno::Sequence;
61 using ::com::sun::star::uno::Type;
62 using ::com::sun::star::uno::XComponentContext;
63 using ::com::sun::star::beans::UnknownPropertyException;
64 using ::com::sun::star::beans::Property;
65 using ::com::sun::star::awt::XControlModel;
66 using ::com::sun::star::drawing::XControlShape;
67 using ::com::sun::star::container::XMap;
68 using ::com::sun::star::inspection::LineDescriptor;
69 using ::com::sun::star::inspection::XPropertyControlFactory;
70 using ::com::sun::star::lang::NullPointerException;
71 using ::com::sun::star::beans::Optional;
72 using ::com::sun::star::inspection::XNumericControl;
73 using ::com::sun::star::drawing::XShape;
74 using ::com::sun::star::beans::PropertyChangeEvent;
75 using ::com::sun::star::lang::EventObject;
76 using ::com::sun::star::beans::XPropertySet;
77 using ::com::sun::star::beans::XPropertyChangeListener;
78 using ::com::sun::star::text::TextContentAnchorType;
79 using ::com::sun::star::text::TextContentAnchorType_AT_PARAGRAPH;
80 using ::com::sun::star::text::TextContentAnchorType_AS_CHARACTER;
81 using ::com::sun::star::beans::XPropertySetInfo;
82 using ::com::sun::star::inspection::XObjectInspectorUI;
83 using ::com::sun::star::lang::XServiceInfo;
84 using ::com::sun::star::sheet::XSpreadsheet;
85 using ::com::sun::star::table::XColumnRowRange;
86 using ::com::sun::star::table::XTableColumns;
87 using ::com::sun::star::table::XTableRows;
88 using ::com::sun::star::table::XCellRange;
89 using ::com::sun::star::container::XIndexAccess;
90 using ::com::sun::star::container::XChild;
91 using ::com::sun::star::form::XGridColumnFactory;
93 namespace MeasureUnit = ::com::sun::star::util::MeasureUnit;
95 typedef ::com::sun::star::awt::Point AwtPoint;
96 typedef ::com::sun::star::awt::Size AwtSize;
98 #define ANCHOR_TO_SHEET 0
99 #define ANCHOR_TO_CELL 1
102 //= BroadcastHelperBase
104 class BroadcastHelperBase
106 protected:
107 BroadcastHelperBase( ::osl::Mutex& _rMutex )
108 :maBHelper( _rMutex )
112 protected:
113 ::cppu::OBroadcastHelper& getBroadcastHelper() { return maBHelper; }
115 private:
116 ::cppu::OBroadcastHelper maBHelper;
120 //= ShapeGeometryChangeNotifier - declaration
122 /** helper class to work around the ...unfortunate implementation of property change broadcasts
123 in the XShape implementation, which broadcasts way too generous and unspecified
125 typedef ::comphelper::ComponentBase ShapeGeometryChangeNotifier_CBase;
126 typedef ::cppu::WeakImplHelper1 < ::com::sun::star::beans::XPropertyChangeListener
127 > ShapeGeometryChangeNotifier_IBase;
129 class ShapeGeometryChangeNotifier :public BroadcastHelperBase
130 ,public ShapeGeometryChangeNotifier_CBase
131 ,public ShapeGeometryChangeNotifier_IBase
133 public:
134 ShapeGeometryChangeNotifier( ::cppu::OWeakObject& _rParent, ::osl::Mutex& _rParentMutex, const Reference< XShape >& _shape )
135 :BroadcastHelperBase( _rParentMutex )
136 ,ShapeGeometryChangeNotifier_CBase( BroadcastHelperBase::getBroadcastHelper(), ::comphelper::ComponentBase::NoInitializationNeeded() )
137 ,ShapeGeometryChangeNotifier_IBase()
138 ,m_rParent( _rParent )
139 ,m_aPropertyChangeListeners( _rParentMutex )
140 ,m_xShape( _shape )
142 ENSURE_OR_THROW( m_xShape.is(), "illegal shape!" );
143 impl_init_nothrow();
146 // property change broadcasting
147 void addPropertyChangeListener( const Reference< XPropertyChangeListener >& _listener )
149 m_aPropertyChangeListeners.addInterface( _listener );
151 void removePropertyChangeListener( const Reference< XPropertyChangeListener >& _listener )
153 m_aPropertyChangeListeners.removeInterface( _listener );
156 // XComponent equivalent
157 void dispose()
159 ::osl::MutexGuard aGuard( getMutex() );
160 impl_dispose_nothrow();
163 // XInterface
164 virtual void SAL_CALL acquire( ) throw () SAL_OVERRIDE
166 m_rParent.acquire();
169 virtual void SAL_CALL release( ) throw () SAL_OVERRIDE
171 m_rParent.release();
174 // XPropertyChangeListener
175 virtual void SAL_CALL propertyChange( const PropertyChangeEvent& _event ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
177 // XEventListener
178 virtual void SAL_CALL disposing( const EventObject& _event ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
180 protected:
181 virtual ~ShapeGeometryChangeNotifier()
183 if ( !getBroadcastHelper().bDisposed )
185 acquire();
186 dispose();
190 protected:
191 ::cppu::OBroadcastHelper& getBroadcastHelper() { return BroadcastHelperBase::getBroadcastHelper(); }
193 private:
194 void impl_init_nothrow();
195 void impl_dispose_nothrow();
197 private:
198 ::cppu::OWeakObject& m_rParent;
199 ::cppu::OInterfaceContainerHelper m_aPropertyChangeListeners;
200 Reference< XShape > m_xShape;
204 //= FormGeometryHandler - declaration
206 class FormGeometryHandler;
207 typedef HandlerComponentBase< FormGeometryHandler > FormGeometryHandler_Base;
208 /** a property handler for any virtual string properties
210 class FormGeometryHandler : public FormGeometryHandler_Base
212 public:
213 FormGeometryHandler(
214 const Reference< XComponentContext >& _rxContext
217 static OUString SAL_CALL getImplementationName_static( ) throw (RuntimeException);
218 static Sequence< OUString > SAL_CALL getSupportedServiceNames_static( ) throw (RuntimeException);
220 protected:
221 virtual ~FormGeometryHandler();
223 protected:
224 // XPropertyHandler overriables
225 virtual Any SAL_CALL getPropertyValue( const OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException, std::exception) SAL_OVERRIDE;
226 virtual void SAL_CALL setPropertyValue( const OUString& _rPropertyName, const Any& _rValue ) throw (UnknownPropertyException, RuntimeException, std::exception) SAL_OVERRIDE;
227 virtual LineDescriptor SAL_CALL describePropertyLine( const OUString& _rPropertyName, const ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XPropertyControlFactory >& _rxControlFactory ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::NullPointerException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
228 virtual void SAL_CALL addPropertyChangeListener( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& _rxListener ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
229 virtual void SAL_CALL removePropertyChangeListener( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& _rxListener ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
230 virtual Sequence< OUString > SAL_CALL getActuatingProperties( ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
231 virtual void SAL_CALL actuatingPropertyChanged( const OUString& _rActuatingPropertyName, const Any& _rNewValue, const Any& _rOldValue, const Reference< XObjectInspectorUI >& _rxInspectorUI, sal_Bool _bFirstTimeInit ) throw (NullPointerException, RuntimeException, std::exception) SAL_OVERRIDE;
233 // OComponentHandler overridables
234 virtual void SAL_CALL disposing() SAL_OVERRIDE;
236 // PropertyHandler overridables
237 virtual Sequence< Property > SAL_CALL doDescribeSupportedProperties() const SAL_OVERRIDE;
239 protected:
240 virtual void onNewComponent() SAL_OVERRIDE;
242 private:
243 bool impl_haveTextAnchorType_nothrow() const;
244 bool impl_haveSheetAnchorType_nothrow() const;
245 void impl_setSheetAnchorType_nothrow( const sal_Int32 _nAnchorType ) const;
247 private:
248 Reference< XControlShape > m_xAssociatedShape;
249 Reference< XPropertySet > m_xShapeProperties;
250 ::rtl::Reference< ShapeGeometryChangeNotifier > m_xChangeNotifier;
254 //= FormGeometryHandler - implementation
257 FormGeometryHandler::FormGeometryHandler( const Reference< XComponentContext >& _rxContext )
258 :FormGeometryHandler_Base( _rxContext )
263 FormGeometryHandler::~FormGeometryHandler( )
265 if ( !rBHelper.bDisposed )
267 acquire();
268 dispose();
274 void FormGeometryHandler::onNewComponent()
276 if ( m_xChangeNotifier.is() )
278 m_xChangeNotifier->dispose();
279 m_xChangeNotifier.clear();
281 m_xAssociatedShape.clear();
282 m_xShapeProperties.clear();
284 FormGeometryHandler_Base::onNewComponent();
288 Reference< XControlModel > xControlModel( m_xComponent, UNO_QUERY );
289 if ( xControlModel.is() )
291 // do not ask the map for shapes for grid control columns ....
292 Reference< XChild > xCompChild( m_xComponent, UNO_QUERY_THROW );
293 Reference< XGridColumnFactory > xCheckGrid( xCompChild->getParent(), UNO_QUERY );
294 if ( !xCheckGrid.is() )
296 Reference< XMap > xControlMap;
297 Any any = m_xContext->getValueByName( "ControlShapeAccess" );
298 any >>= xControlMap;
299 m_xAssociatedShape.set( xControlMap->get( makeAny( xControlModel ) ), UNO_QUERY_THROW );
300 m_xShapeProperties.set( m_xAssociatedShape, UNO_QUERY_THROW );
304 catch( const Exception& )
306 DBG_UNHANDLED_EXCEPTION();
309 if ( m_xAssociatedShape.is() )
310 m_xChangeNotifier = new ShapeGeometryChangeNotifier( *this, m_aMutex, m_xAssociatedShape.get() );
314 OUString SAL_CALL FormGeometryHandler::getImplementationName_static( ) throw (RuntimeException)
316 return OUString( "com.sun.star.comp.extensions.FormGeometryHandler" );
320 Sequence< OUString > SAL_CALL FormGeometryHandler::getSupportedServiceNames_static( ) throw (RuntimeException)
322 Sequence< OUString > aSupported( 1 );
323 aSupported[0] = "com.sun.star.form.inspection.FormGeometryHandler";
324 return aSupported;
328 Any SAL_CALL FormGeometryHandler::getPropertyValue( const OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException, std::exception)
330 ::osl::MutexGuard aGuard( m_aMutex );
331 PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
333 ENSURE_OR_THROW2( m_xAssociatedShape.is(), "internal error: properties, but no shape!", *this );
334 ENSURE_OR_THROW2( m_xShapeProperties.is(), "internal error: no shape properties!", *this );
336 Any aReturn;
339 switch ( nPropId )
341 case PROPERTY_ID_POSITIONX:
342 aReturn <<= m_xAssociatedShape->getPosition().X;
343 break;
344 case PROPERTY_ID_POSITIONY:
345 aReturn <<= m_xAssociatedShape->getPosition().Y;
346 break;
347 case PROPERTY_ID_WIDTH:
348 aReturn <<= m_xAssociatedShape->getSize().Width;
349 break;
350 case PROPERTY_ID_HEIGHT:
351 aReturn <<= m_xAssociatedShape->getSize().Height;
352 break;
353 case PROPERTY_ID_TEXT_ANCHOR_TYPE:
354 aReturn = m_xShapeProperties->getPropertyValue( PROPERTY_ANCHOR_TYPE );
355 OSL_ENSURE( aReturn.hasValue(), "FormGeometryHandler::getPropertyValue: illegal anchor type!" );
356 break;
357 case PROPERTY_ID_SHEET_ANCHOR_TYPE:
359 Reference< XSpreadsheet > xAnchorSheet( m_xShapeProperties->getPropertyValue( PROPERTY_ANCHOR ), UNO_QUERY );
360 aReturn <<= sal_Int32( xAnchorSheet.is() ? ANCHOR_TO_SHEET : ANCHOR_TO_CELL );
362 break;
364 default:
365 OSL_FAIL( "FormGeometryHandler::getPropertyValue: huh?" );
366 break;
369 catch( const Exception& )
371 DBG_UNHANDLED_EXCEPTION();
373 return aReturn;
377 void SAL_CALL FormGeometryHandler::setPropertyValue( const OUString& _rPropertyName, const Any& _rValue ) throw (UnknownPropertyException, RuntimeException, std::exception)
379 ::osl::MutexGuard aGuard( m_aMutex );
380 PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
382 ENSURE_OR_THROW2( m_xAssociatedShape.is(), "internal error: properties, but no shape!", *this );
383 ENSURE_OR_THROW2( m_xShapeProperties.is(), "internal error: properties, but no shape!", *this );
387 switch ( nPropId )
389 case PROPERTY_ID_POSITIONX:
390 case PROPERTY_ID_POSITIONY:
392 sal_Int32 nPosition(0);
393 OSL_VERIFY( _rValue >>= nPosition );
395 AwtPoint aPos( m_xAssociatedShape->getPosition() );
396 if ( nPropId == PROPERTY_ID_POSITIONX )
397 aPos.X = nPosition;
398 else
399 aPos.Y = nPosition;
400 m_xAssociatedShape->setPosition( aPos );
402 break;
404 case PROPERTY_ID_WIDTH:
405 case PROPERTY_ID_HEIGHT:
407 sal_Int32 nSize(0);
408 OSL_VERIFY( _rValue >>= nSize );
410 AwtSize aSize( m_xAssociatedShape->getSize() );
411 if ( nPropId == PROPERTY_ID_WIDTH )
412 aSize.Width = nSize;
413 else
414 aSize.Height = nSize;
415 m_xAssociatedShape->setSize( aSize );
417 break;
419 case PROPERTY_ID_TEXT_ANCHOR_TYPE:
420 m_xShapeProperties->setPropertyValue( PROPERTY_ANCHOR_TYPE, _rValue );
421 break;
423 case PROPERTY_ID_SHEET_ANCHOR_TYPE:
425 sal_Int32 nSheetAnchorType = 0;
426 OSL_VERIFY( _rValue >>= nSheetAnchorType );
427 impl_setSheetAnchorType_nothrow( nSheetAnchorType );
429 break;
431 default:
432 OSL_FAIL( "FormGeometryHandler::getPropertyValue: huh?" );
433 break;
436 catch( const Exception& )
438 DBG_UNHANDLED_EXCEPTION();
443 LineDescriptor SAL_CALL FormGeometryHandler::describePropertyLine( const OUString& _rPropertyName,
444 const Reference< XPropertyControlFactory >& _rxControlFactory )
445 throw (UnknownPropertyException, NullPointerException, RuntimeException, std::exception)
447 ::osl::MutexGuard aGuard( m_aMutex );
448 PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
450 LineDescriptor aLineDesc( PropertyHandler::describePropertyLine( _rPropertyName, _rxControlFactory ) );
453 bool bIsSize = false;
454 switch ( nPropId )
456 case PROPERTY_ID_WIDTH:
457 case PROPERTY_ID_HEIGHT:
458 bIsSize = true;
459 // NO break!
460 case PROPERTY_ID_POSITIONX:
461 case PROPERTY_ID_POSITIONY:
463 Optional< double > aZero( sal_True, 0 );
464 Optional< double > aValueNotPresent( sal_False, 0 );
465 aLineDesc.Control = PropertyHandlerHelper::createNumericControl(
466 _rxControlFactory, 2, bIsSize ? aZero : aValueNotPresent, aValueNotPresent, false );
468 Reference< XNumericControl > xNumericControl( aLineDesc.Control, UNO_QUERY_THROW );
469 xNumericControl->setValueUnit( MeasureUnit::MM_100TH );
470 xNumericControl->setDisplayUnit( impl_getDocumentMeasurementUnit_throw() );
472 break;
474 case PROPERTY_ID_TEXT_ANCHOR_TYPE:
475 case PROPERTY_ID_SHEET_ANCHOR_TYPE:
476 // default handling from PropertyHandler is sufficient
477 break;
479 default:
480 OSL_FAIL( "FormGeometryHandler::describePropertyLine: huh?" );
481 break;
484 catch( const Exception& )
486 DBG_UNHANDLED_EXCEPTION();
488 return aLineDesc;
492 void SAL_CALL FormGeometryHandler::addPropertyChangeListener( const Reference< XPropertyChangeListener >& _listener ) throw (RuntimeException, std::exception)
494 ::osl::MutexGuard aGuard( m_aMutex );
495 OSL_PRECOND( m_xChangeNotifier.is(), "FormGeometryHandler::addPropertyChangeListener: no notified, implies no shape!?" );
496 if ( m_xChangeNotifier.is() )
497 m_xChangeNotifier->addPropertyChangeListener( _listener );
501 void SAL_CALL FormGeometryHandler::removePropertyChangeListener( const Reference< XPropertyChangeListener >& _listener ) throw (RuntimeException, std::exception)
503 ::osl::MutexGuard aGuard( m_aMutex );
504 OSL_PRECOND( m_xChangeNotifier.is(), "FormGeometryHandler::removePropertyChangeListener: no notified, implies no shape!?" );
505 if ( m_xChangeNotifier.is() )
506 m_xChangeNotifier->removePropertyChangeListener( _listener );
510 Sequence< OUString > SAL_CALL FormGeometryHandler::getActuatingProperties( ) throw (RuntimeException, std::exception)
512 Sequence< OUString > aInterestedIn(1);
513 aInterestedIn[0] = PROPERTY_TEXT_ANCHOR_TYPE;
514 return aInterestedIn;
518 void SAL_CALL FormGeometryHandler::actuatingPropertyChanged( const OUString& _rActuatingPropertyName, const Any& _rNewValue, const Any& /*_rOldValue*/, const Reference< XObjectInspectorUI >& _rxInspectorUI, sal_Bool /*_bFirstTimeInit*/ ) throw (NullPointerException, RuntimeException, std::exception)
520 if ( !_rxInspectorUI.is() )
521 throw NullPointerException();
523 ::osl::MutexGuard aGuard( m_aMutex );
524 PropertyId nActuatingPropId( impl_getPropertyId_nothrow( _rActuatingPropertyName ) );
526 switch ( nActuatingPropId )
528 case PROPERTY_ID_TEXT_ANCHOR_TYPE:
530 TextContentAnchorType eAnchorType( TextContentAnchorType_AT_PARAGRAPH );
531 OSL_VERIFY( _rNewValue >>= eAnchorType );
532 _rxInspectorUI->enablePropertyUI( PROPERTY_POSITIONX, eAnchorType != TextContentAnchorType_AS_CHARACTER );
534 break;
535 case -1:
536 throw RuntimeException();
537 break;
538 default:
539 OSL_FAIL( "FormGeometryHandler::actuatingPropertyChanged: not registered for this property!" );
540 break;
545 Sequence< Property > SAL_CALL FormGeometryHandler::doDescribeSupportedProperties() const
547 if ( !m_xAssociatedShape.is() )
548 return Sequence< Property >();
550 ::std::vector< Property > aProperties;
552 addInt32PropertyDescription( aProperties, PROPERTY_POSITIONX );
553 addInt32PropertyDescription( aProperties, PROPERTY_POSITIONY );
554 addInt32PropertyDescription( aProperties, PROPERTY_WIDTH );
555 addInt32PropertyDescription( aProperties, PROPERTY_HEIGHT );
557 if ( impl_haveTextAnchorType_nothrow() )
558 implAddPropertyDescription( aProperties, PROPERTY_TEXT_ANCHOR_TYPE, ::cppu::UnoType< TextContentAnchorType >::get() );
560 if ( impl_haveSheetAnchorType_nothrow() )
561 addInt32PropertyDescription( aProperties, PROPERTY_SHEET_ANCHOR_TYPE );
563 return Sequence< Property >( &(*aProperties.begin()), aProperties.size() );
567 void SAL_CALL FormGeometryHandler::disposing()
569 FormGeometryHandler_Base::disposing();
571 if ( m_xChangeNotifier.is() )
573 m_xChangeNotifier->dispose();
574 m_xChangeNotifier.clear();
579 bool FormGeometryHandler::impl_haveTextAnchorType_nothrow() const
581 ENSURE_OR_THROW( m_xShapeProperties.is(), "not to be called without shape properties" );
584 Reference< XPropertySetInfo > xPSI( m_xShapeProperties->getPropertySetInfo(), UNO_SET_THROW );
585 if ( xPSI->hasPropertyByName( PROPERTY_ANCHOR_TYPE ) )
586 return true;
588 catch( const Exception& )
590 DBG_UNHANDLED_EXCEPTION();
592 return false;
596 bool FormGeometryHandler::impl_haveSheetAnchorType_nothrow() const
598 ENSURE_OR_THROW( m_xShapeProperties.is(), "not to be called without shape properties" );
601 Reference< XPropertySetInfo > xPSI( m_xShapeProperties->getPropertySetInfo(), UNO_SET_THROW );
602 if ( !xPSI->hasPropertyByName( PROPERTY_ANCHOR ) )
603 return false;
604 Reference< XServiceInfo > xSI( m_xAssociatedShape, UNO_QUERY_THROW );
605 if ( xSI->supportsService("com.sun.star.sheet.Shape") )
606 return true;
608 catch( const Exception& )
610 DBG_UNHANDLED_EXCEPTION();
612 return false;
616 namespace
618 static sal_Int32 lcl_getLowerBoundRowOrColumn( const Reference< XIndexAccess >& _rxRowsOrColumns, const bool _bRows,
619 const AwtPoint& _rRelativePosition )
621 sal_Int32 nAccumulated = 0;
623 const sal_Int32& rRelativePos = _bRows ? _rRelativePosition.Y : _rRelativePosition.X;
625 sal_Int32 nElements = _rxRowsOrColumns->getCount();
626 sal_Int32 currentPos = 0;
627 for ( currentPos=0; currentPos<nElements; ++currentPos )
629 Reference< XPropertySet > xRowOrColumn( _rxRowsOrColumns->getByIndex( currentPos ), UNO_QUERY_THROW );
631 bool bIsVisible = true;
632 OSL_VERIFY( xRowOrColumn->getPropertyValue( PROPERTY_IS_VISIBLE ) >>= bIsVisible );
633 if ( !bIsVisible )
634 continue;
636 sal_Int32 nHeightOrWidth( 0 );
637 OSL_VERIFY( xRowOrColumn->getPropertyValue( _bRows ? OUString(PROPERTY_HEIGHT) : OUString(PROPERTY_WIDTH) ) >>= nHeightOrWidth );
639 if ( nAccumulated + nHeightOrWidth > rRelativePos )
640 break;
642 nAccumulated += nHeightOrWidth;
645 return currentPos;
650 void FormGeometryHandler::impl_setSheetAnchorType_nothrow( const sal_Int32 _nAnchorType ) const
652 ENSURE_OR_THROW( m_xShapeProperties.is(), "illegal to be called without shape properties." );
655 CellBindingHelper aHelper( m_xComponent, impl_getContextDocument_nothrow() );
656 // find the sheet which the control belongs to
657 Reference< XSpreadsheet > xSheet;
658 aHelper.getControlSheetIndex( xSheet );
660 switch ( _nAnchorType )
662 case ANCHOR_TO_SHEET:
663 OSL_ENSURE( xSheet.is(),
664 "FormGeometryHandler::impl_setSheetAnchorType_nothrow: sheet not found!" );
665 if ( xSheet.is() )
667 AwtPoint aPreservePosition( m_xAssociatedShape->getPosition() );
668 m_xShapeProperties->setPropertyValue( PROPERTY_ANCHOR, makeAny( xSheet ) );
669 m_xAssociatedShape->setPosition( aPreservePosition );
671 break;
673 case ANCHOR_TO_CELL:
675 Reference< XColumnRowRange > xColsRows( xSheet, UNO_QUERY_THROW );
677 // get the current anchor
678 Reference< XSpreadsheet > xCurrentAnchor;
679 OSL_VERIFY( m_xShapeProperties->getPropertyValue( PROPERTY_ANCHOR ) >>= xCurrentAnchor );
680 OSL_ENSURE( xCurrentAnchor.is(), "FormGeometryHandler::impl_setSheetAnchorType_nothrow: only to be called when currently anchored to a sheet!" );
682 // get the current position
683 AwtPoint aRelativePosition( m_xAssociatedShape->getPosition() );
685 Reference< XTableColumns > xCols( xColsRows->getColumns(), UNO_SET_THROW );
686 sal_Int32 nNewAnchorCol = lcl_getLowerBoundRowOrColumn( xCols.get(), false, aRelativePosition );
688 Reference< XTableRows > xRows( xColsRows->getRows(), UNO_SET_THROW );
689 sal_Int32 nNewAnchorRow = lcl_getLowerBoundRowOrColumn( xRows.get(), true, aRelativePosition );
691 Any aNewAnchorCell( xSheet->getCellByPosition( nNewAnchorCol, nNewAnchorRow ) );
692 m_xShapeProperties->setPropertyValue( PROPERTY_ANCHOR, aNewAnchorCell );
694 break;
696 default:
697 OSL_FAIL( "FormGeometryHandler::impl_setSheetAnchorType_nothrow: illegal anchor type!" );
698 break;
701 catch( const Exception& )
703 DBG_UNHANDLED_EXCEPTION();
708 //= ShapeGeometryChangeNotifier - implementation
710 namespace
712 struct EventTranslation
714 OUString sPropertyName;
715 Any aNewPropertyValue;
717 EventTranslation( const OUString& _propertyName, const Any& _newPropertyValue )
718 :sPropertyName( _propertyName )
719 ,aNewPropertyValue( _newPropertyValue )
726 void SAL_CALL ShapeGeometryChangeNotifier::propertyChange( const PropertyChangeEvent& _event ) throw (RuntimeException, std::exception)
728 ::comphelper::ComponentMethodGuard aGuard( *this );
730 ::std::vector< EventTranslation > aEventTranslations;
731 aEventTranslations.reserve(2);
733 if ( _event.PropertyName == "Position" )
735 AwtPoint aPos = m_xShape->getPosition();
736 aEventTranslations.push_back( EventTranslation( PROPERTY_POSITIONX, makeAny( aPos.X ) ) );
737 aEventTranslations.push_back( EventTranslation( PROPERTY_POSITIONY, makeAny( aPos.Y ) ) );
739 else if ( _event.PropertyName == "Size" )
741 AwtSize aSize = m_xShape->getSize();
742 aEventTranslations.push_back( EventTranslation( PROPERTY_WIDTH, makeAny( aSize.Width ) ) );
743 aEventTranslations.push_back( EventTranslation( PROPERTY_HEIGHT, makeAny( aSize.Height ) ) );
745 else if ( _event.PropertyName == PROPERTY_ANCHOR_TYPE )
747 aEventTranslations.push_back( EventTranslation( PROPERTY_TEXT_ANCHOR_TYPE, _event.NewValue ) );
749 else if ( _event.PropertyName == PROPERTY_ANCHOR )
751 aEventTranslations.push_back( EventTranslation( PROPERTY_SHEET_ANCHOR_TYPE, _event.NewValue ) );
754 PropertyChangeEvent aTranslatedEvent( _event );
755 aTranslatedEvent.Source = m_rParent;
757 aGuard.clear();
758 for ( ::std::vector< EventTranslation >::const_iterator t = aEventTranslations.begin();
759 t != aEventTranslations.end();
763 aTranslatedEvent.PropertyName = t->sPropertyName;
764 aTranslatedEvent.NewValue = t->aNewPropertyValue;
765 m_aPropertyChangeListeners.notifyEach( &XPropertyChangeListener::propertyChange, aTranslatedEvent );
770 void SAL_CALL ShapeGeometryChangeNotifier::disposing( const EventObject& /*_event*/ ) throw (RuntimeException, std::exception)
772 ::comphelper::ComponentMethodGuard aGuard( *this );
773 impl_dispose_nothrow();
777 void ShapeGeometryChangeNotifier::impl_init_nothrow()
779 osl_atomic_increment( &m_refCount );
782 Reference< XPropertySet > xShapeProperties( m_xShape, UNO_QUERY_THROW );
783 xShapeProperties->addPropertyChangeListener( OUString(), this );
785 catch( const Exception& )
787 DBG_UNHANDLED_EXCEPTION();
789 osl_atomic_decrement( &m_refCount );
793 void ShapeGeometryChangeNotifier::impl_dispose_nothrow()
797 Reference< XPropertySet > xShapeProperties( m_xShape, UNO_QUERY_THROW );
798 xShapeProperties->removePropertyChangeListener( OUString(), this );
800 catch( const Exception& )
802 DBG_UNHANDLED_EXCEPTION();
805 getBroadcastHelper().bDisposed = true;
809 } // namespace pcr
812 extern "C" void SAL_CALL createRegistryInfo_FormGeometryHandler()
814 ::pcr::FormGeometryHandler::registerImplementation();
817 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */