merge the formfield patch from ooo-build
[ooovba.git] / chart2 / source / model / main / Legend.cxx
blobf65c63aaba4874a45029ce812f023478361e70b3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Legend.cxx,v $
10 * $Revision: 1.19 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_chart2.hxx"
33 #include "Legend.hxx"
34 #include "macros.hxx"
35 #include "LineProperties.hxx"
36 #include "FillProperties.hxx"
37 #include "CharacterProperties.hxx"
38 #include "UserDefinedProperties.hxx"
39 #include "LegendHelper.hxx"
40 #include "ContainerHelper.hxx"
41 #include "CloneHelper.hxx"
42 #include "PropertyHelper.hxx"
43 #include <com/sun/star/beans/PropertyAttribute.hpp>
44 #include <com/sun/star/awt/Size.hpp>
45 #include <com/sun/star/chart2/LegendPosition.hpp>
46 #include <com/sun/star/chart2/LegendExpansion.hpp>
47 #include <com/sun/star/chart2/RelativePosition.hpp>
49 #include <algorithm>
51 using namespace ::com::sun::star;
52 using namespace ::com::sun::star::beans::PropertyAttribute;
54 using ::rtl::OUString;
55 using ::com::sun::star::uno::Sequence;
56 using ::com::sun::star::uno::Reference;
57 using ::com::sun::star::uno::Any;
58 using ::com::sun::star::beans::Property;
60 namespace
63 static const OUString lcl_aServiceName(
64 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart2.Legend" ));
66 enum
68 PROP_LEGEND_ANCHOR_POSITION,
69 PROP_LEGEND_PREFERRED_EXPANSION,
70 PROP_LEGEND_SHOW,
71 PROP_LEGEND_REF_PAGE_SIZE,
72 PROP_LEGEND_REL_POS
75 void lcl_AddPropertiesToVector(
76 ::std::vector< Property > & rOutProperties )
78 rOutProperties.push_back(
79 Property( C2U( "AnchorPosition" ),
80 PROP_LEGEND_ANCHOR_POSITION,
81 ::getCppuType( reinterpret_cast< const chart2::LegendPosition * >(0)),
82 beans::PropertyAttribute::BOUND
83 | beans::PropertyAttribute::MAYBEDEFAULT ));
85 rOutProperties.push_back(
86 Property( C2U( "Expansion" ),
87 PROP_LEGEND_PREFERRED_EXPANSION,
88 ::getCppuType( reinterpret_cast< const chart2::LegendExpansion * >(0)),
89 beans::PropertyAttribute::BOUND
90 | beans::PropertyAttribute::MAYBEDEFAULT ));
92 rOutProperties.push_back(
93 Property( C2U( "Show" ),
94 PROP_LEGEND_SHOW,
95 ::getBooleanCppuType(),
96 beans::PropertyAttribute::BOUND
97 | beans::PropertyAttribute::MAYBEDEFAULT ));
98 rOutProperties.push_back(
99 Property( C2U( "ReferencePageSize" ),
100 PROP_LEGEND_REF_PAGE_SIZE,
101 ::getCppuType( reinterpret_cast< const awt::Size * >(0)),
102 beans::PropertyAttribute::BOUND
103 | beans::PropertyAttribute::MAYBEVOID ));
105 rOutProperties.push_back(
106 Property( C2U( "RelativePosition" ),
107 PROP_LEGEND_REL_POS,
108 ::getCppuType( reinterpret_cast< const chart2::RelativePosition * >(0)),
109 beans::PropertyAttribute::BOUND
110 | beans::PropertyAttribute::MAYBEVOID ));
113 void lcl_AddDefaultsToMap(
114 ::chart::tPropertyValueMap & rOutMap )
116 ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_LEGEND_ANCHOR_POSITION, chart2::LegendPosition_LINE_END );
117 ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_LEGEND_PREFERRED_EXPANSION, chart2::LegendExpansion_HIGH );
118 ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_LEGEND_SHOW, true );
120 float fDefaultCharHeight = 8.0;
121 ::chart::PropertyHelper::setPropertyValue( rOutMap, ::chart::CharacterProperties::PROP_CHAR_CHAR_HEIGHT, fDefaultCharHeight );
122 ::chart::PropertyHelper::setPropertyValue( rOutMap, ::chart::CharacterProperties::PROP_CHAR_ASIAN_CHAR_HEIGHT, fDefaultCharHeight );
123 ::chart::PropertyHelper::setPropertyValue( rOutMap, ::chart::CharacterProperties::PROP_CHAR_COMPLEX_CHAR_HEIGHT, fDefaultCharHeight );
126 const Sequence< Property > & lcl_GetPropertySequence()
128 static Sequence< Property > aPropSeq;
130 // /--
131 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
132 if( 0 == aPropSeq.getLength() )
134 // get properties
135 ::std::vector< ::com::sun::star::beans::Property > aProperties;
136 lcl_AddPropertiesToVector( aProperties );
137 ::chart::LineProperties::AddPropertiesToVector( aProperties );
138 ::chart::FillProperties::AddPropertiesToVector( aProperties );
139 ::chart::CharacterProperties::AddPropertiesToVector( aProperties );
140 ::chart::UserDefinedProperties::AddPropertiesToVector( aProperties );
142 // and sort them for access via bsearch
143 ::std::sort( aProperties.begin(), aProperties.end(),
144 ::chart::PropertyNameLess() );
146 // transfer result to static Sequence
147 aPropSeq = ::chart::ContainerHelper::ContainerToSequence( aProperties );
150 return aPropSeq;
153 ::cppu::IPropertyArrayHelper & lcl_getInfoHelper()
155 static ::cppu::OPropertyArrayHelper aArrayHelper(
156 lcl_GetPropertySequence(),
157 /* bSorted = */ sal_True );
159 return aArrayHelper;
162 } // anonymous namespace
164 namespace chart
167 Legend::Legend( Reference< uno::XComponentContext > const & /* xContext */ ) :
168 ::property::OPropertySet( m_aMutex ),
169 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
173 Legend::Legend( const Legend & rOther ) :
174 MutexContainer(),
175 impl::Legend_Base(),
176 ::property::OPropertySet( rOther, m_aMutex ),
177 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
179 CloneHelper::CloneRefVector< Reference< chart2::XLegendEntry > >( rOther.m_aLegendEntries, m_aLegendEntries );
180 ModifyListenerHelper::addListenerToAllElements( m_aLegendEntries, m_xModifyEventForwarder );
183 Legend::~Legend()
187 ModifyListenerHelper::removeListenerFromAllElements( m_aLegendEntries, m_xModifyEventForwarder );
189 catch( const uno::Exception & ex )
191 ASSERT_EXCEPTION( ex );
195 // ____ XLegend ____
196 void SAL_CALL Legend::registerEntry( const Reference< chart2::XLegendEntry >& xEntry )
197 throw (lang::IllegalArgumentException,
198 uno::RuntimeException)
200 if( ::std::find( m_aLegendEntries.begin(),
201 m_aLegendEntries.end(),
202 xEntry ) != m_aLegendEntries.end())
203 throw lang::IllegalArgumentException();
205 m_aLegendEntries.push_back( xEntry );
206 ModifyListenerHelper::addListener( xEntry, m_xModifyEventForwarder );
207 fireModifyEvent();
210 void SAL_CALL Legend::revokeEntry( const Reference< chart2::XLegendEntry >& xEntry )
211 throw (container::NoSuchElementException,
212 uno::RuntimeException)
214 tLegendEntries::iterator aIt(
215 ::std::find( m_aLegendEntries.begin(),
216 m_aLegendEntries.end(),
217 xEntry ));
219 if( aIt == m_aLegendEntries.end())
220 throw container::NoSuchElementException();
222 m_aLegendEntries.erase( aIt );
223 ModifyListenerHelper::removeListener( xEntry, m_xModifyEventForwarder );
224 fireModifyEvent();
227 Sequence< Reference< chart2::XLegendEntry > > SAL_CALL Legend::getEntries()
228 throw (uno::RuntimeException)
230 return ContainerHelper::ContainerToSequence( m_aLegendEntries );
233 // ____ XCloneable ____
234 Reference< util::XCloneable > SAL_CALL Legend::createClone()
235 throw (uno::RuntimeException)
237 return Reference< util::XCloneable >( new Legend( *this ));
240 // ____ XModifyBroadcaster ____
241 void SAL_CALL Legend::addModifyListener( const Reference< util::XModifyListener >& aListener )
242 throw (uno::RuntimeException)
246 Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
247 xBroadcaster->addModifyListener( aListener );
249 catch( const uno::Exception & ex )
251 ASSERT_EXCEPTION( ex );
255 void SAL_CALL Legend::removeModifyListener( const Reference< util::XModifyListener >& aListener )
256 throw (uno::RuntimeException)
260 Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
261 xBroadcaster->removeModifyListener( aListener );
263 catch( const uno::Exception & ex )
265 ASSERT_EXCEPTION( ex );
269 // ____ XModifyListener ____
270 void SAL_CALL Legend::modified( const lang::EventObject& aEvent )
271 throw (uno::RuntimeException)
273 m_xModifyEventForwarder->modified( aEvent );
276 // ____ XEventListener (base of XModifyListener) ____
277 void SAL_CALL Legend::disposing( const lang::EventObject& /* Source */ )
278 throw (uno::RuntimeException)
280 // nothing
283 // ____ OPropertySet ____
284 void Legend::firePropertyChangeEvent()
286 fireModifyEvent();
289 void Legend::fireModifyEvent()
291 m_xModifyEventForwarder->modified( lang::EventObject( static_cast< uno::XWeak* >( this )));
294 // ================================================================================
296 Sequence< OUString > Legend::getSupportedServiceNames_Static()
298 const sal_Int32 nNumServices( 6 );
299 sal_Int32 nI = 0;
300 Sequence< OUString > aServices( nNumServices );
301 aServices[ nI++ ] = C2U( "com.sun.star.chart2.Legend" );
302 aServices[ nI++ ] = C2U( "com.sun.star.beans.PropertySet" );
303 aServices[ nI++ ] = C2U( "com.sun.star.drawing.FillProperties" );
304 aServices[ nI++ ] = C2U( "com.sun.star.drawing.LineProperties" );
305 aServices[ nI++ ] = C2U( "com.sun.star.style.CharacterProperties" );
306 aServices[ nI++ ] = C2U( "com.sun.star.layout.LayoutElement" );
307 OSL_ASSERT( nNumServices == nI );
308 return aServices;
311 // ____ OPropertySet ____
312 Any Legend::GetDefaultValue( sal_Int32 nHandle ) const
313 throw(beans::UnknownPropertyException)
315 static tPropertyValueMap aStaticDefaults;
317 // /--
318 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
319 if( 0 == aStaticDefaults.size() )
321 LineProperties::AddDefaultsToMap( aStaticDefaults );
322 FillProperties::AddDefaultsToMap( aStaticDefaults );
323 CharacterProperties::AddDefaultsToMap( aStaticDefaults );
324 // call last to overwrite some character property defaults
325 lcl_AddDefaultsToMap( aStaticDefaults );
328 tPropertyValueMap::const_iterator aFound(
329 aStaticDefaults.find( nHandle ));
331 if( aFound == aStaticDefaults.end())
332 return Any();
334 return (*aFound).second;
335 // \--
338 ::cppu::IPropertyArrayHelper & SAL_CALL Legend::getInfoHelper()
340 return lcl_getInfoHelper();
344 // ____ XPropertySet ____
345 Reference< beans::XPropertySetInfo > SAL_CALL
346 Legend::getPropertySetInfo()
347 throw (uno::RuntimeException)
349 static Reference< beans::XPropertySetInfo > xInfo;
351 // /--
352 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
353 if( !xInfo.is())
355 xInfo = ::cppu::OPropertySetHelper::createPropertySetInfo(
356 getInfoHelper());
359 return xInfo;
360 // \--
363 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
364 APPHELPER_XSERVICEINFO_IMPL( Legend, lcl_aServiceName );
366 // needed by MSC compiler
367 using impl::Legend_Base;
369 IMPLEMENT_FORWARD_XINTERFACE2( Legend, Legend_Base, ::property::OPropertySet )
370 IMPLEMENT_FORWARD_XTYPEPROVIDER2( Legend, Legend_Base, ::property::OPropertySet )
372 } // namespace chart