fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / ui / unoobj / nameuno.cxx
blobf4e328217f0a29fd3dca14dcb9f666a0eb89272c
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 <svl/smplhint.hxx>
21 #include <vcl/svapp.hxx>
23 #include <com/sun/star/sheet/NamedRangeFlag.hpp>
24 #include <com/sun/star/awt/XBitmap.hpp>
25 #include <com/sun/star/beans/PropertyAttribute.hpp>
27 #include <comphelper/servicehelper.hxx>
28 #include <cppuhelper/supportsservice.hxx>
30 using namespace ::com::sun::star;
31 using ::com::sun::star::uno::Reference;
32 using ::com::sun::star::uno::Any;
34 #include "nameuno.hxx"
35 #include "miscuno.hxx"
36 #include "cellsuno.hxx"
37 #include "convuno.hxx"
38 #include "targuno.hxx"
39 #include "tokenuno.hxx"
40 #include "tokenarray.hxx"
41 #include "docsh.hxx"
42 #include "docfunc.hxx"
43 #include "rangenam.hxx"
44 #include "unonames.hxx"
46 #include "scui_def.hxx"
48 static const SfxItemPropertyMapEntry* lcl_GetNamedRangeMap()
50 static const SfxItemPropertyMapEntry aNamedRangeMap_Impl[] =
52 {OUString(SC_UNO_LINKDISPBIT), 0, cppu::UnoType<awt::XBitmap>::get(), beans::PropertyAttribute::READONLY, 0 },
53 {OUString(SC_UNO_LINKDISPNAME), 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::READONLY, 0 },
54 {OUString(SC_UNONAME_TOKENINDEX), 0, cppu::UnoType<sal_Int32>::get(), beans::PropertyAttribute::READONLY, 0 },
55 {OUString(SC_UNONAME_ISSHAREDFMLA), 0, cppu::UnoType<bool>::get(), 0, 0 },
56 { OUString(), 0, css::uno::Type(), 0, 0 }
58 return aNamedRangeMap_Impl;
61 static const SfxItemPropertyMapEntry* lcl_GetNamedRangesMap()
63 static const SfxItemPropertyMapEntry aNamedRangesMap_Impl[] =
65 {OUString(SC_UNO_MODIFY_BROADCAST), 0, cppu::UnoType<bool>::get(), 0, 0 },
66 { OUString(), 0, css::uno::Type(), 0, 0 }
68 return aNamedRangesMap_Impl;
71 #define SCNAMEDRANGEOBJ_SERVICE "com.sun.star.sheet.NamedRange"
73 SC_SIMPLE_SERVICE_INFO( ScLabelRangeObj, "ScLabelRangeObj", "com.sun.star.sheet.LabelRange" )
74 SC_SIMPLE_SERVICE_INFO( ScLabelRangesObj, "ScLabelRangesObj", "com.sun.star.sheet.LabelRanges" )
75 SC_SIMPLE_SERVICE_INFO( ScNamedRangesObj, "ScNamedRangesObj", "com.sun.star.sheet.NamedRanges" )
77 static bool lcl_UserVisibleName(const ScRangeData& rData)
79 //! as method to ScRangeData
81 return !rData.HasType(RT_DATABASE);
84 ScNamedRangeObj::ScNamedRangeObj( rtl::Reference< ScNamedRangesObj > xParent, ScDocShell* pDocSh, const OUString& rNm, Reference<container::XNamed> xSheet):
85 mxParent(xParent),
86 pDocShell( pDocSh ),
87 aName( rNm ),
88 mxSheet( xSheet )
90 pDocShell->GetDocument().AddUnoObject(*this);
93 ScNamedRangeObj::~ScNamedRangeObj()
95 SolarMutexGuard g;
97 if (pDocShell)
98 pDocShell->GetDocument().RemoveUnoObject(*this);
101 void ScNamedRangeObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
103 // reference update is of no interest
105 const SfxSimpleHint* pSimpleHint = dynamic_cast<const SfxSimpleHint*>(&rHint);
106 if ( pSimpleHint && pSimpleHint->GetId() == SFX_HINT_DYING )
107 pDocShell = NULL; // became invalid
110 // Helper functions
112 ScRangeData* ScNamedRangeObj::GetRangeData_Impl()
114 ScRangeData* pRet = NULL;
115 if (pDocShell)
117 ScRangeName* pNames;
118 SCTAB nTab = GetTab_Impl();
119 if (nTab >= 0)
120 pNames = pDocShell->GetDocument().GetRangeName(nTab);
121 else
122 pNames = pDocShell->GetDocument().GetRangeName();
123 if (pNames)
125 pRet = pNames->findByUpperName(ScGlobal::pCharClass->uppercase(aName));
126 if (pRet)
127 pRet->ValidateTabRefs(); // adjust relative tab refs to valid tables
130 return pRet;
133 SCTAB ScNamedRangeObj::GetTab_Impl()
135 if (mxSheet.is())
137 if (!pDocShell)
138 return -2;
139 ScDocument& rDoc = pDocShell->GetDocument();
140 SCTAB nTab;
141 OUString sName = mxSheet->getName();
142 bool bFound = rDoc.GetTable(sName, nTab);
143 assert(bFound); (void)bFound; // fouled up?
144 return nTab;
146 else
147 return -1;//global range name
150 // sheet::XNamedRange
152 void ScNamedRangeObj::Modify_Impl( const OUString* pNewName, const ScTokenArray* pNewTokens, const OUString* pNewContent,
153 const ScAddress* pNewPos, const sal_uInt16* pNewType,
154 const formula::FormulaGrammar::Grammar eGrammar )
156 if (!pDocShell)
157 return;
159 ScDocument& rDoc = pDocShell->GetDocument();
160 ScRangeName* pNames;
161 SCTAB nTab = GetTab_Impl();
162 if (nTab >= 0)
163 pNames = rDoc.GetRangeName(nTab);
164 else
165 pNames = rDoc.GetRangeName();
166 if (!pNames)
167 return;
169 const ScRangeData* pOld = pNames->findByUpperName(ScGlobal::pCharClass->uppercase(aName));
170 if (!pOld)
171 return;
173 ScRangeName* pNewRanges = new ScRangeName(*pNames);
175 OUString aInsName = pOld->GetName();
176 if (pNewName)
177 aInsName = *pNewName;
179 OUString aContent; // Content string based =>
180 pOld->GetSymbol( aContent, eGrammar); // no problems with changed positions and such.
181 if (pNewContent)
182 aContent = *pNewContent;
184 ScAddress aPos = pOld->GetPos();
185 if (pNewPos)
186 aPos = *pNewPos;
188 sal_uInt16 nType = pOld->GetType();
189 if (pNewType)
190 nType = *pNewType;
192 ScRangeData* pNew = NULL;
193 if (pNewTokens)
194 pNew = new ScRangeData( &rDoc, aInsName, *pNewTokens, aPos, nType );
195 else
196 pNew = new ScRangeData( &rDoc, aInsName, aContent, aPos, nType, eGrammar );
198 pNew->SetIndex( pOld->GetIndex() );
200 pNewRanges->erase(*pOld);
201 if (pNewRanges->insert(pNew))
203 pDocShell->GetDocFunc().SetNewRangeNames(pNewRanges, mxParent->IsModifyAndBroadcast(), nTab);
205 aName = aInsName; //! broadcast?
207 else
209 pNew = NULL; //! uno::Exception/Error or something
210 delete pNewRanges;
214 OUString SAL_CALL ScNamedRangeObj::getName() throw(uno::RuntimeException, std::exception)
216 SolarMutexGuard aGuard;
217 return aName;
220 void SAL_CALL ScNamedRangeObj::setName( const OUString& aNewName )
221 throw (uno::RuntimeException, std::exception)
223 SolarMutexGuard aGuard;
224 //! adapt formulas ?????
226 OUString aNewStr(aNewName);
227 // GRAM_PODF_A1 for API compatibility.
228 Modify_Impl( &aNewStr, NULL, NULL, NULL, NULL,formula::FormulaGrammar::GRAM_PODF_A1 );
230 if ( aName != aNewStr ) // some error occurred...
231 throw uno::RuntimeException(); // no other exceptions specified
234 OUString SAL_CALL ScNamedRangeObj::getContent() throw(uno::RuntimeException, std::exception)
236 SolarMutexGuard aGuard;
237 OUString aContent;
238 ScRangeData* pData = GetRangeData_Impl();
239 if (pData)
240 // GRAM_PODF_A1 for API compatibility.
241 pData->GetSymbol( aContent,formula::FormulaGrammar::GRAM_PODF_A1);
242 return aContent;
245 void SAL_CALL ScNamedRangeObj::setContent( const OUString& aContent )
246 throw (uno::RuntimeException, std::exception)
248 SolarMutexGuard aGuard;
249 OUString aContStr(aContent);
250 // GRAM_PODF_A1 for API compatibility.
251 Modify_Impl( NULL, NULL, &aContStr, NULL, NULL,formula::FormulaGrammar::GRAM_PODF_A1 );
254 table::CellAddress SAL_CALL ScNamedRangeObj::getReferencePosition()
255 throw(uno::RuntimeException, std::exception)
257 SolarMutexGuard aGuard;
258 ScAddress aPos;
259 ScRangeData* pData = GetRangeData_Impl();
260 if (pData)
261 aPos = pData->GetPos();
262 table::CellAddress aAddress;
263 aAddress.Column = aPos.Col();
264 aAddress.Row = aPos.Row();
265 aAddress.Sheet = aPos.Tab();
266 if (pDocShell)
268 SCTAB nDocTabs = pDocShell->GetDocument().GetTableCount();
269 if ( aAddress.Sheet >= nDocTabs && nDocTabs > 0 )
271 // Even after ValidateTabRefs, the position can be invalid if
272 // the content points to preceding tables. The resulting string
273 // is invalid in any case, so the position is just shifted.
274 aAddress.Sheet = nDocTabs - 1;
277 return aAddress;
280 void SAL_CALL ScNamedRangeObj::setReferencePosition( const table::CellAddress& aReferencePosition )
281 throw (uno::RuntimeException, std::exception)
283 SolarMutexGuard aGuard;
284 ScAddress aPos( (SCCOL)aReferencePosition.Column, (SCROW)aReferencePosition.Row, aReferencePosition.Sheet );
285 // GRAM_PODF_A1 for API compatibility.
286 Modify_Impl( NULL, NULL, NULL, &aPos, NULL,formula::FormulaGrammar::GRAM_PODF_A1 );
289 sal_Int32 SAL_CALL ScNamedRangeObj::getType() throw(uno::RuntimeException, std::exception)
291 SolarMutexGuard aGuard;
292 sal_Int32 nType=0;
293 ScRangeData* pData = GetRangeData_Impl();
294 if (pData)
296 // do not return internal RT_* flags
297 // see property 'IsSharedFormula' for RT_SHARED
298 if ( pData->HasType(RT_CRITERIA) ) nType |= sheet::NamedRangeFlag::FILTER_CRITERIA;
299 if ( pData->HasType(RT_PRINTAREA) ) nType |= sheet::NamedRangeFlag::PRINT_AREA;
300 if ( pData->HasType(RT_COLHEADER) ) nType |= sheet::NamedRangeFlag::COLUMN_HEADER;
301 if ( pData->HasType(RT_ROWHEADER) ) nType |= sheet::NamedRangeFlag::ROW_HEADER;
303 return nType;
306 void SAL_CALL ScNamedRangeObj::setType( sal_Int32 nUnoType )
307 throw (uno::RuntimeException, std::exception)
309 // see property 'IsSharedFormula' for RT_SHARED
310 SolarMutexGuard aGuard;
311 sal_uInt16 nNewType = RT_NAME;
312 if ( nUnoType & sheet::NamedRangeFlag::FILTER_CRITERIA ) nNewType |= RT_CRITERIA;
313 if ( nUnoType & sheet::NamedRangeFlag::PRINT_AREA ) nNewType |= RT_PRINTAREA;
314 if ( nUnoType & sheet::NamedRangeFlag::COLUMN_HEADER ) nNewType |= RT_COLHEADER;
315 if ( nUnoType & sheet::NamedRangeFlag::ROW_HEADER ) nNewType |= RT_ROWHEADER;
317 // GRAM_PODF_A1 for API compatibility.
318 Modify_Impl( NULL, NULL, NULL, NULL, &nNewType,formula::FormulaGrammar::GRAM_PODF_A1 );
321 // XFormulaTokens
323 uno::Sequence<sheet::FormulaToken> SAL_CALL ScNamedRangeObj::getTokens()
324 throw (uno::RuntimeException, std::exception)
326 SolarMutexGuard aGuard;
327 uno::Sequence<sheet::FormulaToken> aSequence;
328 ScRangeData* pData = GetRangeData_Impl();
329 if (pData && pDocShell)
331 ScTokenArray* pTokenArray = pData->GetCode();
332 if ( pTokenArray )
333 (void)ScTokenConversion::ConvertToTokenSequence( pDocShell->GetDocument(), aSequence, *pTokenArray );
335 return aSequence;
338 void SAL_CALL ScNamedRangeObj::setTokens( const uno::Sequence<sheet::FormulaToken>& rTokens )
339 throw (uno::RuntimeException, std::exception)
341 SolarMutexGuard aGuard;
342 if( pDocShell )
344 ScTokenArray aTokenArray;
345 (void)ScTokenConversion::ConvertToTokenArray( pDocShell->GetDocument(), aTokenArray, rTokens );
346 // GRAM_PODF_A1 for API compatibility.
347 Modify_Impl( NULL, &aTokenArray, NULL, NULL, NULL, formula::FormulaGrammar::GRAM_PODF_A1 );
351 // XCellRangeSource
353 uno::Reference<table::XCellRange> SAL_CALL ScNamedRangeObj::getReferredCells()
354 throw(uno::RuntimeException, std::exception)
356 SolarMutexGuard aGuard;
357 ScRange aRange;
358 ScRangeData* pData = GetRangeData_Impl();
359 if ( pData && pData->IsValidReference( aRange ) )
361 //! static function to create ScCellObj/ScCellRangeObj at ScCellRangeObj ???
363 if ( aRange.aStart == aRange.aEnd )
364 return new ScCellObj( pDocShell, aRange.aStart );
365 else
366 return new ScCellRangeObj( pDocShell, aRange );
368 return NULL;
371 // beans::XPropertySet
373 uno::Reference<beans::XPropertySetInfo> SAL_CALL ScNamedRangeObj::getPropertySetInfo()
374 throw(uno::RuntimeException, std::exception)
376 SolarMutexGuard aGuard;
377 static uno::Reference< beans::XPropertySetInfo > aRef(new SfxItemPropertySetInfo( lcl_GetNamedRangeMap() ));
378 return aRef;
381 void SAL_CALL ScNamedRangeObj::setPropertyValue(
382 const OUString& rPropertyName, const uno::Any& /*aValue*/ )
383 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
384 lang::IllegalArgumentException, lang::WrappedTargetException,
385 uno::RuntimeException, std::exception)
387 SolarMutexGuard aGuard;
388 if ( rPropertyName == SC_UNONAME_ISSHAREDFMLA )
390 // Ignore this.
394 uno::Any SAL_CALL ScNamedRangeObj::getPropertyValue( const OUString& rPropertyName )
395 throw(beans::UnknownPropertyException, lang::WrappedTargetException,
396 uno::RuntimeException, std::exception)
398 SolarMutexGuard aGuard;
399 uno::Any aRet;
400 if ( rPropertyName == SC_UNO_LINKDISPBIT )
402 // no target bitmaps for individual entries (would be all equal)
403 // ScLinkTargetTypeObj::SetLinkTargetBitmap( aRet, SC_LINKTARGETTYPE_RANGENAME );
405 else if ( rPropertyName == SC_UNO_LINKDISPNAME )
406 aRet <<= OUString( aName );
407 else if ( rPropertyName == SC_UNONAME_TOKENINDEX )
409 // get index for use in formula tokens (read-only)
410 ScRangeData* pData = GetRangeData_Impl();
411 if (pData)
412 aRet <<= static_cast<sal_Int32>(pData->GetIndex());
414 else if ( rPropertyName == SC_UNONAME_ISSHAREDFMLA )
416 if (GetRangeData_Impl())
417 aRet <<= false;
419 return aRet;
422 SC_IMPL_DUMMY_PROPERTY_LISTENER( ScNamedRangeObj )
424 // lang::XServiceInfo
426 OUString SAL_CALL ScNamedRangeObj::getImplementationName() throw(uno::RuntimeException, std::exception)
428 return OUString( "ScNamedRangeObj" );
431 sal_Bool SAL_CALL ScNamedRangeObj::supportsService( const OUString& rServiceName )
432 throw(uno::RuntimeException, std::exception)
434 return cppu::supportsService(this, rServiceName);
437 uno::Sequence<OUString> SAL_CALL ScNamedRangeObj::getSupportedServiceNames()
438 throw(uno::RuntimeException, std::exception)
440 uno::Sequence<OUString> aRet(2);
441 aRet[0] = SCNAMEDRANGEOBJ_SERVICE;
442 aRet[1] = SCLINKTARGET_SERVICE;
443 return aRet;
446 // XUnoTunnel
448 sal_Int64 SAL_CALL ScNamedRangeObj::getSomething(
449 const uno::Sequence<sal_Int8 >& rId ) throw(uno::RuntimeException, std::exception)
451 if ( rId.getLength() == 16 &&
452 0 == memcmp( getUnoTunnelId().getConstArray(),
453 rId.getConstArray(), 16 ) )
455 return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_IntPtr>(this));
457 return 0;
460 namespace
462 class theScNamedRangeObjUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theScNamedRangeObjUnoTunnelId> {};
465 const uno::Sequence<sal_Int8>& ScNamedRangeObj::getUnoTunnelId()
467 return theScNamedRangeObjUnoTunnelId::get().getSeq();
470 ScNamedRangesObj::ScNamedRangesObj(ScDocShell* pDocSh) :
471 mbModifyAndBroadcast(true),
472 pDocShell( pDocSh )
474 pDocShell->GetDocument().AddUnoObject(*this);
477 ScNamedRangesObj::~ScNamedRangesObj()
479 SolarMutexGuard g;
481 if (pDocShell)
482 pDocShell->GetDocument().RemoveUnoObject(*this);
485 void ScNamedRangesObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
487 // reference update is of no interest
489 const SfxSimpleHint* pSimpleHint = dynamic_cast<const SfxSimpleHint*>(&rHint);
490 if ( pSimpleHint && pSimpleHint->GetId() == SFX_HINT_DYING )
492 pDocShell = NULL; // became invalid
496 // sheet::XNamedRanges
498 void SAL_CALL ScNamedRangesObj::addNewByName( const OUString& aName,
499 const OUString& aContent, const table::CellAddress& aPosition,
500 sal_Int32 nUnoType )
501 throw (uno::RuntimeException, std::exception)
503 SolarMutexGuard aGuard;
504 ScAddress aPos( (SCCOL)aPosition.Column, (SCROW)aPosition.Row, aPosition.Sheet );
506 sal_uInt16 nNewType = RT_NAME;
507 if ( nUnoType & sheet::NamedRangeFlag::FILTER_CRITERIA ) nNewType |= RT_CRITERIA;
508 if ( nUnoType & sheet::NamedRangeFlag::PRINT_AREA ) nNewType |= RT_PRINTAREA;
509 if ( nUnoType & sheet::NamedRangeFlag::COLUMN_HEADER ) nNewType |= RT_COLHEADER;
510 if ( nUnoType & sheet::NamedRangeFlag::ROW_HEADER ) nNewType |= RT_ROWHEADER;
512 bool bDone = false;
513 if (pDocShell)
515 ScDocument& rDoc = pDocShell->GetDocument();
516 ScRangeName* pNames = GetRangeName_Impl();
517 if (pNames && !pNames->findByUpperName(ScGlobal::pCharClass->uppercase(aName)))
519 ScRangeName* pNewRanges = new ScRangeName( *pNames );
520 // GRAM_PODF_A1 for API compatibility.
521 ScRangeData* pNew = new ScRangeData( &rDoc, aName, aContent,
522 aPos, nNewType,formula::FormulaGrammar::GRAM_PODF_A1 );
523 if ( pNewRanges->insert(pNew) )
525 pDocShell->GetDocFunc().SetNewRangeNames(pNewRanges, mbModifyAndBroadcast, GetTab_Impl());
526 bDone = true;
528 else
530 pNew = NULL;
531 delete pNewRanges;
536 if (!bDone)
537 throw uno::RuntimeException(); // no other exceptions specified
540 void SAL_CALL ScNamedRangesObj::addNewFromTitles( const table::CellRangeAddress& aSource,
541 sheet::Border aBorder ) throw(uno::RuntimeException, std::exception)
543 SolarMutexGuard aGuard;
544 //! this cannot be an enum, because multiple bits can be set !!!
546 bool bTop = ( aBorder == sheet::Border_TOP );
547 bool bLeft = ( aBorder == sheet::Border_LEFT );
548 bool bBottom = ( aBorder == sheet::Border_BOTTOM );
549 bool bRight = ( aBorder == sheet::Border_RIGHT );
551 ScRange aRange;
552 ScUnoConversion::FillScRange( aRange, aSource );
554 sal_uInt16 nFlags = 0;
555 if (bTop) nFlags |= NAME_TOP;
556 if (bLeft) nFlags |= NAME_LEFT;
557 if (bBottom) nFlags |= NAME_BOTTOM;
558 if (bRight) nFlags |= NAME_RIGHT;
560 if (nFlags)
561 pDocShell->GetDocFunc().CreateNames( aRange, nFlags, true, GetTab_Impl() );
564 void SAL_CALL ScNamedRangesObj::removeByName( const OUString& aName )
565 throw(uno::RuntimeException, std::exception)
567 SolarMutexGuard aGuard;
568 bool bDone = false;
569 if (pDocShell)
571 ScRangeName* pNames = GetRangeName_Impl();
572 if (pNames)
574 const ScRangeData* pData = pNames->findByUpperName(ScGlobal::pCharClass->uppercase(aName));
575 if (pData && lcl_UserVisibleName(*pData))
577 ScRangeName* pNewRanges = new ScRangeName(*pNames);
578 pNewRanges->erase(*pData);
579 pDocShell->GetDocFunc().SetNewRangeNames( pNewRanges, mbModifyAndBroadcast, GetTab_Impl());
580 bDone = true;
585 if (!bDone)
586 throw uno::RuntimeException(); // no other exceptions specified
589 void SAL_CALL ScNamedRangesObj::outputList( const table::CellAddress& aOutputPosition )
590 throw(uno::RuntimeException, std::exception)
592 SolarMutexGuard aGuard;
593 ScAddress aPos( (SCCOL)aOutputPosition.Column, (SCROW)aOutputPosition.Row, aOutputPosition.Sheet );
594 if (pDocShell)
595 pDocShell->GetDocFunc().InsertNameList( aPos, true );
598 // container::XEnumerationAccess
600 uno::Reference<container::XEnumeration> SAL_CALL ScNamedRangesObj::createEnumeration()
601 throw(uno::RuntimeException, std::exception)
603 SolarMutexGuard aGuard;
604 return new ScIndexEnumeration(this, OUString("com.sun.star.sheet.NamedRangesEnumeration"));
607 // container::XIndexAccess
609 sal_Int32 SAL_CALL ScNamedRangesObj::getCount() throw(uno::RuntimeException, std::exception)
611 SolarMutexGuard aGuard;
612 long nRet = 0;
613 if (pDocShell)
615 ScRangeName* pNames = GetRangeName_Impl();
616 if (pNames)
618 ScRangeName::const_iterator itr = pNames->begin(), itrEnd = pNames->end();
619 for (; itr != itrEnd; ++itr)
620 if (lcl_UserVisibleName(*itr->second))
621 ++nRet;
624 return nRet;
627 uno::Any SAL_CALL ScNamedRangesObj::getByIndex( sal_Int32 nIndex )
628 throw(lang::IndexOutOfBoundsException,
629 lang::WrappedTargetException, uno::RuntimeException, std::exception)
631 SolarMutexGuard aGuard;
632 uno::Reference< sheet::XNamedRange > xRange(GetObjectByIndex_Impl((sal_uInt16)nIndex));
633 if ( xRange.is() )
634 return uno::makeAny(xRange);
635 else
636 throw lang::IndexOutOfBoundsException();
639 uno::Type SAL_CALL ScNamedRangesObj::getElementType() throw(uno::RuntimeException, std::exception)
641 SolarMutexGuard aGuard;
642 return cppu::UnoType<sheet::XNamedRange>::get(); // must be suitable for getByIndex
645 sal_Bool SAL_CALL ScNamedRangesObj::hasElements() throw(uno::RuntimeException, std::exception)
647 SolarMutexGuard aGuard;
648 return ( getCount() != 0 );
651 Reference<beans::XPropertySetInfo> SAL_CALL ScNamedRangesObj::getPropertySetInfo()
652 throw(uno::RuntimeException, std::exception)
654 static Reference<beans::XPropertySetInfo> aRef(
655 new SfxItemPropertySetInfo(lcl_GetNamedRangesMap()));
656 return aRef;
659 void SAL_CALL ScNamedRangesObj::setPropertyValue(
660 const OUString& rPropertyName, const uno::Any& aValue )
661 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
662 lang::IllegalArgumentException, lang::WrappedTargetException,
663 uno::RuntimeException, std::exception)
665 if ( rPropertyName == SC_UNO_MODIFY_BROADCAST )
667 aValue >>= mbModifyAndBroadcast;
671 Any SAL_CALL ScNamedRangesObj::getPropertyValue( const OUString& rPropertyName )
672 throw(beans::UnknownPropertyException, lang::WrappedTargetException,
673 uno::RuntimeException, std::exception)
675 Any aRet;
676 if ( rPropertyName == SC_UNO_MODIFY_BROADCAST )
678 aRet <<= mbModifyAndBroadcast;
681 return aRet;
684 SC_IMPL_DUMMY_PROPERTY_LISTENER( ScNamedRangesObj )
686 uno::Any SAL_CALL ScNamedRangesObj::getByName( const OUString& aName )
687 throw(container::NoSuchElementException,
688 lang::WrappedTargetException, uno::RuntimeException, std::exception)
690 SolarMutexGuard aGuard;
691 uno::Reference< sheet::XNamedRange > xRange(GetObjectByName_Impl(aName));
692 if ( xRange.is() )
693 return uno::makeAny(xRange);
694 else
695 throw container::NoSuchElementException();
698 uno::Sequence<OUString> SAL_CALL ScNamedRangesObj::getElementNames()
699 throw(uno::RuntimeException, std::exception)
701 SolarMutexGuard aGuard;
702 if (pDocShell)
704 ScRangeName* pNames = GetRangeName_Impl();
705 if (pNames)
707 long nVisCount = getCount(); // names with lcl_UserVisibleName
708 uno::Sequence<OUString> aSeq(nVisCount);
709 OUString* pAry = aSeq.getArray();
710 sal_uInt16 nVisPos = 0;
711 ScRangeName::const_iterator itr = pNames->begin(), itrEnd = pNames->end();
712 for (; itr != itrEnd; ++itr)
714 if (lcl_UserVisibleName(*itr->second))
715 pAry[nVisPos++] = itr->second->GetName();
717 return aSeq;
720 return uno::Sequence<OUString>(0);
723 sal_Bool SAL_CALL ScNamedRangesObj::hasByName( const OUString& aName )
724 throw(uno::RuntimeException, std::exception)
726 SolarMutexGuard aGuard;
727 if (pDocShell)
729 ScRangeName* pNames = GetRangeName_Impl();
730 if (pNames)
732 const ScRangeData* pData = pNames->findByUpperName(ScGlobal::pCharClass->uppercase(aName));
733 if (pData && lcl_UserVisibleName(*pData))
734 return sal_True;
737 return false;
740 /** called from the XActionLockable interface methods on initial locking */
741 void ScNamedRangesObj::lock()
743 pDocShell->GetDocument().PreprocessRangeNameUpdate();
746 /** called from the XActionLockable interface methods on final unlock */
747 void ScNamedRangesObj::unlock()
749 pDocShell->GetDocument().CompileHybridFormula();
752 // document::XActionLockable
754 sal_Bool ScNamedRangesObj::isActionLocked() throw(uno::RuntimeException, std::exception)
756 SolarMutexGuard aGuard;
757 return pDocShell->GetDocument().GetNamedRangesLockCount() != 0;
760 void ScNamedRangesObj::addActionLock() throw(uno::RuntimeException, std::exception)
762 SolarMutexGuard aGuard;
763 ScDocument& rDoc = pDocShell->GetDocument();
764 sal_Int16 nLockCount = rDoc.GetNamedRangesLockCount();
765 ++nLockCount;
766 if ( nLockCount == 1 )
768 lock();
770 rDoc.SetNamedRangesLockCount( nLockCount );
773 void ScNamedRangesObj::removeActionLock() throw(uno::RuntimeException, std::exception)
775 SolarMutexGuard aGuard;
776 ScDocument& rDoc = pDocShell->GetDocument();
777 sal_Int16 nLockCount = rDoc.GetNamedRangesLockCount();
778 if ( nLockCount > 0 )
780 --nLockCount;
781 if ( nLockCount == 0 )
783 unlock();
785 rDoc.SetNamedRangesLockCount( nLockCount );
789 void ScNamedRangesObj::setActionLocks( sal_Int16 nLock ) throw(uno::RuntimeException, std::exception)
791 SolarMutexGuard aGuard;
792 if ( nLock >= 0 )
794 ScDocument& rDoc = pDocShell->GetDocument();
795 sal_Int16 nLockCount = rDoc.GetNamedRangesLockCount();
796 if ( nLock == 0 && nLockCount > 0 )
798 unlock();
800 if ( nLock > 0 && nLockCount == 0 )
802 lock();
804 rDoc.SetNamedRangesLockCount( nLock );
808 sal_Int16 ScNamedRangesObj::resetActionLocks() throw(uno::RuntimeException, std::exception)
810 SolarMutexGuard aGuard;
811 ScDocument& rDoc = pDocShell->GetDocument();
812 sal_Int16 nLockCount = rDoc.GetNamedRangesLockCount();
813 if ( nLockCount > 0 )
815 unlock();
817 rDoc.SetNamedRangesLockCount( 0 );
818 return nLockCount;
821 ScGlobalNamedRangesObj::ScGlobalNamedRangesObj(ScDocShell* pDocSh)
822 : ScNamedRangesObj(pDocSh)
827 ScGlobalNamedRangesObj::~ScGlobalNamedRangesObj()
832 ScNamedRangeObj* ScGlobalNamedRangesObj::GetObjectByIndex_Impl(sal_uInt16 nIndex)
834 if (!pDocShell)
835 return NULL;
837 ScRangeName* pNames = pDocShell->GetDocument().GetRangeName();
838 if (!pNames)
839 return NULL;
841 ScRangeName::const_iterator itr = pNames->begin(), itrEnd = pNames->end();
842 sal_uInt16 nPos = 0;
843 for (; itr != itrEnd; ++itr)
845 if (lcl_UserVisibleName(*itr->second))
847 if (nPos == nIndex)
848 return new ScNamedRangeObj(this, pDocShell, itr->second->GetName());
850 ++nPos;
852 return NULL;
855 ScNamedRangeObj* ScGlobalNamedRangesObj::GetObjectByName_Impl(const OUString& aName)
857 if ( pDocShell && hasByName(aName) )
858 return new ScNamedRangeObj(this, pDocShell, aName);
859 return NULL;
862 ScRangeName* ScGlobalNamedRangesObj::GetRangeName_Impl()
864 return pDocShell->GetDocument().GetRangeName();
867 SCTAB ScGlobalNamedRangesObj::GetTab_Impl()
869 return -1;
872 ScLocalNamedRangesObj::ScLocalNamedRangesObj( ScDocShell* pDocSh, uno::Reference<container::XNamed> xSheet )
873 : ScNamedRangesObj(pDocSh),
874 mxSheet(xSheet)
879 ScLocalNamedRangesObj::~ScLocalNamedRangesObj()
884 ScNamedRangeObj* ScLocalNamedRangesObj::GetObjectByName_Impl(const OUString& aName)
886 if ( pDocShell && hasByName( aName ) )
887 return new ScNamedRangeObj( this, pDocShell, aName, mxSheet);
888 return NULL;
892 ScNamedRangeObj* ScLocalNamedRangesObj::GetObjectByIndex_Impl( sal_uInt16 nIndex )
894 if (!pDocShell)
895 return NULL;
897 OUString aName = mxSheet->getName();
898 ScDocument& rDoc = pDocShell->GetDocument();
899 SCTAB nTab;
900 if (!rDoc.GetTable(aName, nTab))
901 return NULL;
903 ScRangeName* pNames = rDoc.GetRangeName( nTab );
904 if (!pNames)
905 return NULL;
907 ScRangeName::const_iterator itr = pNames->begin(), itrEnd = pNames->end();
908 sal_uInt16 nPos = 0;
909 for (; itr != itrEnd; ++itr)
911 if (lcl_UserVisibleName(*itr->second))
913 if (nPos == nIndex)
914 return new ScNamedRangeObj(this, pDocShell, itr->second->GetName(), mxSheet);
916 ++nPos;
918 return NULL;
921 ScRangeName* ScLocalNamedRangesObj::GetRangeName_Impl()
923 SCTAB nTab = GetTab_Impl();
924 return pDocShell->GetDocument().GetRangeName( nTab );
927 SCTAB ScLocalNamedRangesObj::GetTab_Impl()
929 SCTAB nTab;
930 (void)pDocShell->GetDocument().GetTable(mxSheet->getName(), nTab);
931 return nTab;
934 ScLabelRangeObj::ScLabelRangeObj(ScDocShell* pDocSh, bool bCol, const ScRange& rR) :
935 pDocShell( pDocSh ),
936 bColumn( bCol ),
937 aRange( rR )
939 pDocShell->GetDocument().AddUnoObject(*this);
942 ScLabelRangeObj::~ScLabelRangeObj()
944 SolarMutexGuard g;
946 if (pDocShell)
947 pDocShell->GetDocument().RemoveUnoObject(*this);
950 void ScLabelRangeObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
952 //! Ref-Update !!!
954 const SfxSimpleHint* pSimpleHint = dynamic_cast<const SfxSimpleHint*>(&rHint);
955 if ( pSimpleHint && pSimpleHint->GetId() == SFX_HINT_DYING )
956 pDocShell = NULL; // became invalid
959 // Helper functions
961 ScRangePair* ScLabelRangeObj::GetData_Impl()
963 ScRangePair* pRet = NULL;
964 if (pDocShell)
966 ScDocument& rDoc = pDocShell->GetDocument();
967 ScRangePairList* pList = bColumn ? rDoc.GetColNameRanges() : rDoc.GetRowNameRanges();
968 if (pList)
969 pRet = pList->Find( aRange );
971 return pRet;
974 void ScLabelRangeObj::Modify_Impl( const ScRange* pLabel, const ScRange* pData )
976 if (pDocShell)
978 ScDocument& rDoc = pDocShell->GetDocument();
979 ScRangePairList* pOldList = bColumn ? rDoc.GetColNameRanges() : rDoc.GetRowNameRanges();
980 if (pOldList)
982 ScRangePairListRef xNewList(pOldList->Clone());
983 ScRangePair* pEntry = xNewList->Find( aRange );
984 if (pEntry)
986 xNewList->Remove( pEntry ); // only removed from list, not deleted
988 if ( pLabel )
989 pEntry->GetRange(0) = *pLabel;
990 if ( pData )
991 pEntry->GetRange(1) = *pData;
993 xNewList->Join( *pEntry );
994 delete pEntry;
996 if (bColumn)
997 rDoc.GetColNameRangesRef() = xNewList;
998 else
999 rDoc.GetRowNameRangesRef() = xNewList;
1001 rDoc.CompileColRowNameFormula();
1002 pDocShell->PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID );
1003 pDocShell->SetDocumentModified();
1005 //! Undo ?!?! (here and from dialog)
1007 if ( pLabel )
1008 aRange = *pLabel; // adapt object to find range again
1014 // sheet::XLabelRange
1016 table::CellRangeAddress SAL_CALL ScLabelRangeObj::getLabelArea()
1017 throw(uno::RuntimeException, std::exception)
1019 SolarMutexGuard aGuard;
1020 table::CellRangeAddress aRet;
1021 ScRangePair* pData = GetData_Impl();
1022 if (pData)
1023 ScUnoConversion::FillApiRange( aRet, pData->GetRange(0) );
1024 return aRet;
1027 void SAL_CALL ScLabelRangeObj::setLabelArea( const table::CellRangeAddress& aLabelArea )
1028 throw(uno::RuntimeException, std::exception)
1030 SolarMutexGuard aGuard;
1031 ScRange aLabelRange;
1032 ScUnoConversion::FillScRange( aLabelRange, aLabelArea );
1033 Modify_Impl( &aLabelRange, NULL );
1036 table::CellRangeAddress SAL_CALL ScLabelRangeObj::getDataArea()
1037 throw(uno::RuntimeException, std::exception)
1039 SolarMutexGuard aGuard;
1040 table::CellRangeAddress aRet;
1041 ScRangePair* pData = GetData_Impl();
1042 if (pData)
1043 ScUnoConversion::FillApiRange( aRet, pData->GetRange(1) );
1044 return aRet;
1047 void SAL_CALL ScLabelRangeObj::setDataArea( const table::CellRangeAddress& aDataArea )
1048 throw(uno::RuntimeException, std::exception)
1050 SolarMutexGuard aGuard;
1051 ScRange aDataRange;
1052 ScUnoConversion::FillScRange( aDataRange, aDataArea );
1053 Modify_Impl( NULL, &aDataRange );
1056 ScLabelRangesObj::ScLabelRangesObj(ScDocShell* pDocSh, bool bCol) :
1057 pDocShell( pDocSh ),
1058 bColumn( bCol )
1060 pDocShell->GetDocument().AddUnoObject(*this);
1063 ScLabelRangesObj::~ScLabelRangesObj()
1065 SolarMutexGuard g;
1067 if (pDocShell)
1068 pDocShell->GetDocument().RemoveUnoObject(*this);
1071 void ScLabelRangesObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
1073 // reference update is of no interest
1075 const SfxSimpleHint* pSimpleHint = dynamic_cast<const SfxSimpleHint*>(&rHint);
1076 if ( pSimpleHint && pSimpleHint->GetId() == SFX_HINT_DYING )
1078 pDocShell = NULL; // became invalid
1082 // sheet::XLabelRanges
1084 ScLabelRangeObj* ScLabelRangesObj::GetObjectByIndex_Impl(size_t nIndex)
1086 if (pDocShell)
1088 ScDocument& rDoc = pDocShell->GetDocument();
1089 ScRangePairList* pList = bColumn ? rDoc.GetColNameRanges() : rDoc.GetRowNameRanges();
1090 if ( pList && nIndex < pList->size() )
1092 ScRangePair* pData = (*pList)[nIndex];
1093 if (pData)
1094 return new ScLabelRangeObj( pDocShell, bColumn, pData->GetRange(0) );
1097 return NULL;
1100 void SAL_CALL ScLabelRangesObj::addNew( const table::CellRangeAddress& aLabelArea,
1101 const table::CellRangeAddress& aDataArea )
1102 throw(uno::RuntimeException, std::exception)
1104 SolarMutexGuard aGuard;
1105 if (pDocShell)
1107 ScDocument& rDoc = pDocShell->GetDocument();
1108 ScRangePairList* pOldList = bColumn ? rDoc.GetColNameRanges() : rDoc.GetRowNameRanges();
1109 if (pOldList)
1111 ScRangePairListRef xNewList(pOldList->Clone());
1113 ScRange aLabelRange;
1114 ScRange aDataRange;
1115 ScUnoConversion::FillScRange( aLabelRange, aLabelArea );
1116 ScUnoConversion::FillScRange( aDataRange, aDataArea );
1117 xNewList->Join( ScRangePair( aLabelRange, aDataRange ) );
1119 if (bColumn)
1120 rDoc.GetColNameRangesRef() = xNewList;
1121 else
1122 rDoc.GetRowNameRangesRef() = xNewList;
1124 rDoc.CompileColRowNameFormula();
1125 pDocShell->PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID );
1126 pDocShell->SetDocumentModified();
1128 //! Undo ?!?! (here and from dialog)
1133 void SAL_CALL ScLabelRangesObj::removeByIndex( sal_Int32 nIndex )
1134 throw(uno::RuntimeException, std::exception)
1136 SolarMutexGuard aGuard;
1137 bool bDone = false;
1138 if (pDocShell)
1140 ScDocument& rDoc = pDocShell->GetDocument();
1141 ScRangePairList* pOldList = bColumn ? rDoc.GetColNameRanges() : rDoc.GetRowNameRanges();
1143 if ( pOldList && nIndex >= 0 && nIndex < (sal_Int32)pOldList->size() )
1145 ScRangePairListRef xNewList(pOldList->Clone());
1147 ScRangePair* pEntry = (*xNewList)[nIndex];
1148 if (pEntry)
1150 xNewList->Remove( pEntry );
1151 delete pEntry;
1153 if (bColumn)
1154 rDoc.GetColNameRangesRef() = xNewList;
1155 else
1156 rDoc.GetRowNameRangesRef() = xNewList;
1158 rDoc.CompileColRowNameFormula();
1159 pDocShell->PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID );
1160 pDocShell->SetDocumentModified();
1161 bDone = true;
1163 //! Undo ?!?! (here and from dialog)
1167 if (!bDone)
1168 throw uno::RuntimeException(); // no other exceptions specified
1171 // container::XEnumerationAccess
1173 uno::Reference<container::XEnumeration> SAL_CALL ScLabelRangesObj::createEnumeration()
1174 throw(uno::RuntimeException, std::exception)
1176 SolarMutexGuard aGuard;
1177 return new ScIndexEnumeration(this, OUString("com.sun.star.sheet.LabelRangesEnumeration"));
1180 // container::XIndexAccess
1182 sal_Int32 SAL_CALL ScLabelRangesObj::getCount() throw(uno::RuntimeException, std::exception)
1184 SolarMutexGuard aGuard;
1185 if (pDocShell)
1187 ScDocument& rDoc = pDocShell->GetDocument();
1188 ScRangePairList* pList = bColumn ? rDoc.GetColNameRanges() : rDoc.GetRowNameRanges();
1189 if (pList)
1190 return pList->size();
1192 return 0;
1195 uno::Any SAL_CALL ScLabelRangesObj::getByIndex( sal_Int32 nIndex )
1196 throw(lang::IndexOutOfBoundsException,
1197 lang::WrappedTargetException, uno::RuntimeException, std::exception)
1199 SolarMutexGuard aGuard;
1200 uno::Reference< sheet::XLabelRange > xRange(GetObjectByIndex_Impl((sal_uInt16)nIndex));
1201 if ( xRange.is() )
1202 return uno::makeAny(xRange);
1203 else
1204 throw lang::IndexOutOfBoundsException();
1207 uno::Type SAL_CALL ScLabelRangesObj::getElementType() throw(uno::RuntimeException, std::exception)
1209 SolarMutexGuard aGuard;
1210 return cppu::UnoType<sheet::XLabelRange>::get(); // must be suitable for getByIndex
1214 sal_Bool SAL_CALL ScLabelRangesObj::hasElements() throw(uno::RuntimeException, std::exception)
1216 SolarMutexGuard aGuard;
1217 return ( getCount() != 0 );
1220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */