Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / unoobj / nameuno.cxx
blobc67e577d61569a81772bd29a7e9c3e8d5c1a522d
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>
29 using namespace ::com::sun::star;
30 using ::com::sun::star::uno::Reference;
31 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 //------------------------------------------------------------------------
50 static const SfxItemPropertyMapEntry* lcl_GetNamedRangeMap()
52 static const SfxItemPropertyMapEntry aNamedRangeMap_Impl[] =
54 {MAP_CHAR_LEN(SC_UNO_LINKDISPBIT), 0, &getCppuType((uno::Reference<awt::XBitmap>*)0), beans::PropertyAttribute::READONLY, 0 },
55 {MAP_CHAR_LEN(SC_UNO_LINKDISPNAME), 0, &getCppuType((OUString*)0), beans::PropertyAttribute::READONLY, 0 },
56 {MAP_CHAR_LEN(SC_UNONAME_TOKENINDEX), 0, &getCppuType((sal_Int32*)0), beans::PropertyAttribute::READONLY, 0 },
57 {MAP_CHAR_LEN(SC_UNONAME_ISSHAREDFMLA), 0, &getBooleanCppuType(), 0, 0 },
58 {0,0,0,0,0,0}
60 return aNamedRangeMap_Impl;
63 static const SfxItemPropertyMapEntry* lcl_GetNamedRangesMap()
65 static const SfxItemPropertyMapEntry aNamedRangesMap_Impl[] =
67 {MAP_CHAR_LEN(SC_UNO_MODIFY_BROADCAST), 0, &getBooleanCppuType(), 0, 0 },
68 {0,0,0,0,0,0}
70 return aNamedRangesMap_Impl;
73 //------------------------------------------------------------------------
75 #define SCNAMEDRANGEOBJ_SERVICE "com.sun.star.sheet.NamedRange"
77 SC_SIMPLE_SERVICE_INFO( ScLabelRangeObj, "ScLabelRangeObj", "com.sun.star.sheet.LabelRange" )
78 SC_SIMPLE_SERVICE_INFO( ScLabelRangesObj, "ScLabelRangesObj", "com.sun.star.sheet.LabelRanges" )
79 SC_SIMPLE_SERVICE_INFO( ScNamedRangesObj, "ScNamedRangesObj", "com.sun.star.sheet.NamedRanges" )
81 static bool lcl_UserVisibleName(const ScRangeData& rData)
83 //! als Methode an ScRangeData
85 return !rData.HasType(RT_DATABASE);
88 ScNamedRangeObj::ScNamedRangeObj( rtl::Reference< ScNamedRangesObj > xParent, ScDocShell* pDocSh, const OUString& rNm, Reference<container::XNamed> xSheet):
89 mxParent(xParent),
90 pDocShell( pDocSh ),
91 aName( rNm ),
92 mxSheet( xSheet )
94 pDocShell->GetDocument()->AddUnoObject(*this);
97 ScNamedRangeObj::~ScNamedRangeObj()
99 if (pDocShell)
100 pDocShell->GetDocument()->RemoveUnoObject(*this);
103 void ScNamedRangeObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
105 // Ref-Update interessiert nicht
107 if ( rHint.ISA( SfxSimpleHint ) && ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
108 pDocShell = NULL; // ungueltig geworden
111 // Hilfsfuntionen
113 ScRangeData* ScNamedRangeObj::GetRangeData_Impl()
115 ScRangeData* pRet = NULL;
116 if (pDocShell)
118 ScRangeName* pNames;
119 SCTAB nTab = GetTab_Impl();
120 if (nTab >= 0)
121 pNames = pDocShell->GetDocument()->GetRangeName(nTab);
122 else
123 pNames = pDocShell->GetDocument()->GetRangeName();
124 if (pNames)
126 pRet = pNames->findByUpperName(ScGlobal::pCharClass->uppercase(aName));
127 if (pRet)
128 pRet->ValidateTabRefs(); // adjust relative tab refs to valid tables
131 return pRet;
134 SCTAB ScNamedRangeObj::GetTab_Impl()
136 if (mxSheet.is())
138 if (!pDocShell)
139 return -2;
140 ScDocument* pDoc = pDocShell->GetDocument();
141 SCTAB nTab;
142 OUString sName = mxSheet->getName();
143 pDoc->GetTable(sName, nTab);
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* pDoc = pDocShell->GetDocument();
160 ScRangeName* pNames;
161 SCTAB nTab = GetTab_Impl();
162 if (nTab >= 0)
163 pNames = pDoc->GetRangeName(nTab);
164 else
165 pNames = pDoc->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( pDoc, aInsName, *pNewTokens, aPos, nType );
195 else
196 pNew = new ScRangeData( pDoc, 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/Fehler oder so
210 delete pNewRanges;
215 OUString SAL_CALL ScNamedRangeObj::getName() throw(uno::RuntimeException)
217 SolarMutexGuard aGuard;
218 return aName;
221 void SAL_CALL ScNamedRangeObj::setName( const OUString& aNewName )
222 throw(uno::RuntimeException)
224 SolarMutexGuard aGuard;
225 //! Formeln anpassen ?????
227 OUString aNewStr(aNewName);
228 // GRAM_PODF_A1 for API compatibility.
229 Modify_Impl( &aNewStr, NULL, NULL, NULL, NULL,formula::FormulaGrammar::GRAM_PODF_A1 );
231 if ( aName != aNewStr ) // some error occurred...
232 throw uno::RuntimeException(); // no other exceptions specified
235 OUString SAL_CALL ScNamedRangeObj::getContent() throw(uno::RuntimeException)
237 SolarMutexGuard aGuard;
238 OUString aContent;
239 ScRangeData* pData = GetRangeData_Impl();
240 if (pData)
241 // GRAM_PODF_A1 for API compatibility.
242 pData->GetSymbol( aContent,formula::FormulaGrammar::GRAM_PODF_A1);
243 return aContent;
246 void SAL_CALL ScNamedRangeObj::setContent( const OUString& aContent )
247 throw(uno::RuntimeException)
249 SolarMutexGuard aGuard;
250 OUString aContStr(aContent);
251 // GRAM_PODF_A1 for API compatibility.
252 Modify_Impl( NULL, NULL, &aContStr, NULL, NULL,formula::FormulaGrammar::GRAM_PODF_A1 );
255 table::CellAddress SAL_CALL ScNamedRangeObj::getReferencePosition()
256 throw(uno::RuntimeException)
258 SolarMutexGuard aGuard;
259 ScAddress aPos;
260 ScRangeData* pData = GetRangeData_Impl();
261 if (pData)
262 aPos = pData->GetPos();
263 table::CellAddress aAddress;
264 aAddress.Column = aPos.Col();
265 aAddress.Row = aPos.Row();
266 aAddress.Sheet = aPos.Tab();
267 if (pDocShell)
269 SCTAB nDocTabs = pDocShell->GetDocument()->GetTableCount();
270 if ( aAddress.Sheet >= nDocTabs && nDocTabs > 0 )
272 // Even after ValidateTabRefs, the position can be invalid if
273 // the content points to preceding tables. The resulting string
274 // is invalid in any case, so the position is just shifted.
275 aAddress.Sheet = nDocTabs - 1;
278 return aAddress;
281 void SAL_CALL ScNamedRangeObj::setReferencePosition( const table::CellAddress& aReferencePosition )
282 throw(uno::RuntimeException)
284 SolarMutexGuard aGuard;
285 ScAddress aPos( (SCCOL)aReferencePosition.Column, (SCROW)aReferencePosition.Row, aReferencePosition.Sheet );
286 // GRAM_PODF_A1 for API compatibility.
287 Modify_Impl( NULL, NULL, NULL, &aPos, NULL,formula::FormulaGrammar::GRAM_PODF_A1 );
290 sal_Int32 SAL_CALL ScNamedRangeObj::getType() throw(uno::RuntimeException)
292 SolarMutexGuard aGuard;
293 sal_Int32 nType=0;
294 ScRangeData* pData = GetRangeData_Impl();
295 if (pData)
297 // do not return internal RT_* flags
298 // see property 'IsSharedFormula' for RT_SHARED
299 if ( pData->HasType(RT_CRITERIA) ) nType |= sheet::NamedRangeFlag::FILTER_CRITERIA;
300 if ( pData->HasType(RT_PRINTAREA) ) nType |= sheet::NamedRangeFlag::PRINT_AREA;
301 if ( pData->HasType(RT_COLHEADER) ) nType |= sheet::NamedRangeFlag::COLUMN_HEADER;
302 if ( pData->HasType(RT_ROWHEADER) ) nType |= sheet::NamedRangeFlag::ROW_HEADER;
304 return nType;
307 void SAL_CALL ScNamedRangeObj::setType( sal_Int32 nUnoType ) throw(uno::RuntimeException)
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() throw(uno::RuntimeException)
325 SolarMutexGuard aGuard;
326 uno::Sequence<sheet::FormulaToken> aSequence;
327 ScRangeData* pData = GetRangeData_Impl();
328 if (pData && pDocShell)
330 ScTokenArray* pTokenArray = pData->GetCode();
331 if ( pTokenArray )
332 (void)ScTokenConversion::ConvertToTokenSequence( *pDocShell->GetDocument(), aSequence, *pTokenArray );
334 return aSequence;
337 void SAL_CALL ScNamedRangeObj::setTokens( const uno::Sequence<sheet::FormulaToken>& rTokens ) throw(uno::RuntimeException)
339 SolarMutexGuard aGuard;
340 if( pDocShell )
342 ScTokenArray aTokenArray;
343 (void)ScTokenConversion::ConvertToTokenArray( *pDocShell->GetDocument(), aTokenArray, rTokens );
344 // GRAM_PODF_A1 for API compatibility.
345 Modify_Impl( NULL, &aTokenArray, NULL, NULL, NULL, formula::FormulaGrammar::GRAM_PODF_A1 );
350 // XCellRangeSource
352 uno::Reference<table::XCellRange> SAL_CALL ScNamedRangeObj::getReferredCells()
353 throw(uno::RuntimeException)
355 SolarMutexGuard aGuard;
356 ScRange aRange;
357 ScRangeData* pData = GetRangeData_Impl();
358 if ( pData && pData->IsValidReference( aRange ) )
360 //! static Funktion um ScCellObj/ScCellRangeObj zu erzeugen am ScCellRangeObj ???
362 if ( aRange.aStart == aRange.aEnd )
363 return new ScCellObj( pDocShell, aRange.aStart );
364 else
365 return new ScCellRangeObj( pDocShell, aRange );
367 return NULL;
370 // beans::XPropertySet
372 uno::Reference<beans::XPropertySetInfo> SAL_CALL ScNamedRangeObj::getPropertySetInfo()
373 throw(uno::RuntimeException)
375 SolarMutexGuard aGuard;
376 static uno::Reference< beans::XPropertySetInfo > aRef(new SfxItemPropertySetInfo( lcl_GetNamedRangeMap() ));
377 return aRef;
380 void SAL_CALL ScNamedRangeObj::setPropertyValue(
381 const OUString& rPropertyName, const uno::Any& /*aValue*/ )
382 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
383 lang::IllegalArgumentException, lang::WrappedTargetException,
384 uno::RuntimeException)
386 SolarMutexGuard aGuard;
387 if ( rPropertyName == SC_UNONAME_ISSHAREDFMLA )
389 // Ignore this.
393 uno::Any SAL_CALL ScNamedRangeObj::getPropertyValue( const OUString& rPropertyName )
394 throw(beans::UnknownPropertyException, lang::WrappedTargetException,
395 uno::RuntimeException)
397 SolarMutexGuard aGuard;
398 uno::Any aRet;
399 if ( rPropertyName == SC_UNO_LINKDISPBIT )
401 // no target bitmaps for individual entries (would be all equal)
402 // ScLinkTargetTypeObj::SetLinkTargetBitmap( aRet, SC_LINKTARGETTYPE_RANGENAME );
404 else if ( rPropertyName == SC_UNO_LINKDISPNAME )
405 aRet <<= OUString( aName );
406 else if ( rPropertyName == SC_UNONAME_TOKENINDEX )
408 // get index for use in formula tokens (read-only)
409 ScRangeData* pData = GetRangeData_Impl();
410 if (pData)
411 aRet <<= static_cast<sal_Int32>(pData->GetIndex());
413 else if ( rPropertyName == SC_UNONAME_ISSHAREDFMLA )
415 if (GetRangeData_Impl())
416 aRet <<= false;
418 return aRet;
421 SC_IMPL_DUMMY_PROPERTY_LISTENER( ScNamedRangeObj )
423 // lang::XServiceInfo
425 OUString SAL_CALL ScNamedRangeObj::getImplementationName() throw(uno::RuntimeException)
427 return OUString( "ScNamedRangeObj" );
430 sal_Bool SAL_CALL ScNamedRangeObj::supportsService( const OUString& rServiceName )
431 throw(uno::RuntimeException)
433 return rServiceName == SCNAMEDRANGEOBJ_SERVICE ||
434 rServiceName == SCLINKTARGET_SERVICE;
437 uno::Sequence<OUString> SAL_CALL ScNamedRangeObj::getSupportedServiceNames()
438 throw(uno::RuntimeException)
440 uno::Sequence<OUString> aRet(2);
441 aRet[0] = OUString( SCNAMEDRANGEOBJ_SERVICE );
442 aRet[1] = OUString( SCLINKTARGET_SERVICE );
443 return aRet;
447 // XUnoTunnel
449 sal_Int64 SAL_CALL ScNamedRangeObj::getSomething(
450 const uno::Sequence<sal_Int8 >& rId ) throw(uno::RuntimeException)
452 if ( rId.getLength() == 16 &&
453 0 == memcmp( getUnoTunnelId().getConstArray(),
454 rId.getConstArray(), 16 ) )
456 return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_IntPtr>(this));
458 return 0;
461 namespace
463 class theScNamedRangeObjUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theScNamedRangeObjUnoTunnelId> {};
466 const uno::Sequence<sal_Int8>& ScNamedRangeObj::getUnoTunnelId()
468 return theScNamedRangeObjUnoTunnelId::get().getSeq();
471 //------------------------------------------------------------------------
473 ScNamedRangesObj::ScNamedRangesObj(ScDocShell* pDocSh) :
474 mbModifyAndBroadcast(true),
475 pDocShell( pDocSh )
477 pDocShell->GetDocument()->AddUnoObject(*this);
480 ScNamedRangesObj::~ScNamedRangesObj()
482 if (pDocShell)
483 pDocShell->GetDocument()->RemoveUnoObject(*this);
486 void ScNamedRangesObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
488 // Referenz-Update interessiert hier nicht
490 if ( rHint.ISA( SfxSimpleHint ) &&
491 ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
493 pDocShell = NULL; // ungueltig geworden
497 bool ScNamedRangesObj::IsModifyAndBroadcast() const
499 return mbModifyAndBroadcast;
502 // sheet::XNamedRanges
504 void SAL_CALL ScNamedRangesObj::addNewByName( const OUString& aName,
505 const OUString& aContent, const table::CellAddress& aPosition,
506 sal_Int32 nUnoType ) throw(uno::RuntimeException)
508 SolarMutexGuard aGuard;
509 ScAddress aPos( (SCCOL)aPosition.Column, (SCROW)aPosition.Row, aPosition.Sheet );
511 sal_uInt16 nNewType = RT_NAME;
512 if ( nUnoType & sheet::NamedRangeFlag::FILTER_CRITERIA ) nNewType |= RT_CRITERIA;
513 if ( nUnoType & sheet::NamedRangeFlag::PRINT_AREA ) nNewType |= RT_PRINTAREA;
514 if ( nUnoType & sheet::NamedRangeFlag::COLUMN_HEADER ) nNewType |= RT_COLHEADER;
515 if ( nUnoType & sheet::NamedRangeFlag::ROW_HEADER ) nNewType |= RT_ROWHEADER;
517 sal_Bool bDone = false;
518 if (pDocShell)
520 ScDocument* pDoc = pDocShell->GetDocument();
521 ScRangeName* pNames = GetRangeName_Impl();
522 if (pNames && !pNames->findByUpperName(ScGlobal::pCharClass->uppercase(aName)))
524 ScRangeName* pNewRanges = new ScRangeName( *pNames );
525 // GRAM_PODF_A1 for API compatibility.
526 ScRangeData* pNew = new ScRangeData( pDoc, aName, aContent,
527 aPos, nNewType,formula::FormulaGrammar::GRAM_PODF_A1 );
528 if ( pNewRanges->insert(pNew) )
530 pDocShell->GetDocFunc().SetNewRangeNames(pNewRanges, mbModifyAndBroadcast, GetTab_Impl());
531 bDone = true;
533 else
535 pNew = NULL;
536 delete pNewRanges;
541 if (!bDone)
542 throw uno::RuntimeException(); // no other exceptions specified
545 void SAL_CALL ScNamedRangesObj::addNewFromTitles( const table::CellRangeAddress& aSource,
546 sheet::Border aBorder ) throw(uno::RuntimeException)
548 SolarMutexGuard aGuard;
549 //! das darf kein enum sein, weil mehrere Bits gesetzt sein koennen !!!
551 sal_Bool bTop = ( aBorder == sheet::Border_TOP );
552 sal_Bool bLeft = ( aBorder == sheet::Border_LEFT );
553 sal_Bool bBottom = ( aBorder == sheet::Border_BOTTOM );
554 sal_Bool bRight = ( aBorder == sheet::Border_RIGHT );
556 ScRange aRange;
557 ScUnoConversion::FillScRange( aRange, aSource );
559 sal_uInt16 nFlags = 0;
560 if (bTop) nFlags |= NAME_TOP;
561 if (bLeft) nFlags |= NAME_LEFT;
562 if (bBottom) nFlags |= NAME_BOTTOM;
563 if (bRight) nFlags |= NAME_RIGHT;
565 if (nFlags)
566 pDocShell->GetDocFunc().CreateNames( aRange, nFlags, sal_True, GetTab_Impl() );
569 void SAL_CALL ScNamedRangesObj::removeByName( const OUString& aName )
570 throw(uno::RuntimeException)
572 SolarMutexGuard aGuard;
573 bool bDone = false;
574 if (pDocShell)
576 ScRangeName* pNames = GetRangeName_Impl();
577 if (pNames)
579 const ScRangeData* pData = pNames->findByUpperName(ScGlobal::pCharClass->uppercase(aName));
580 if (pData && lcl_UserVisibleName(*pData))
582 ScRangeName* pNewRanges = new ScRangeName(*pNames);
583 pNewRanges->erase(*pData);
584 pDocShell->GetDocFunc().SetNewRangeNames( pNewRanges, mbModifyAndBroadcast, GetTab_Impl());
585 bDone = true;
590 if (!bDone)
591 throw uno::RuntimeException(); // no other exceptions specified
594 void SAL_CALL ScNamedRangesObj::outputList( const table::CellAddress& aOutputPosition )
595 throw(uno::RuntimeException)
597 SolarMutexGuard aGuard;
598 ScAddress aPos( (SCCOL)aOutputPosition.Column, (SCROW)aOutputPosition.Row, aOutputPosition.Sheet );
599 if (pDocShell)
600 pDocShell->GetDocFunc().InsertNameList( aPos, sal_True );
603 // container::XEnumerationAccess
605 uno::Reference<container::XEnumeration> SAL_CALL ScNamedRangesObj::createEnumeration()
606 throw(uno::RuntimeException)
608 SolarMutexGuard aGuard;
609 return new ScIndexEnumeration(this, OUString("com.sun.star.sheet.NamedRangesEnumeration"));
612 // container::XIndexAccess
614 sal_Int32 SAL_CALL ScNamedRangesObj::getCount() throw(uno::RuntimeException)
616 SolarMutexGuard aGuard;
617 long nRet = 0;
618 if (pDocShell)
620 ScRangeName* pNames = GetRangeName_Impl();
621 if (pNames)
623 ScRangeName::const_iterator itr = pNames->begin(), itrEnd = pNames->end();
624 for (; itr != itrEnd; ++itr)
625 if (lcl_UserVisibleName(*itr->second))
626 ++nRet;
629 return nRet;
632 uno::Any SAL_CALL ScNamedRangesObj::getByIndex( sal_Int32 nIndex )
633 throw(lang::IndexOutOfBoundsException,
634 lang::WrappedTargetException, uno::RuntimeException)
636 SolarMutexGuard aGuard;
637 uno::Reference< sheet::XNamedRange > xRange(GetObjectByIndex_Impl((sal_uInt16)nIndex));
638 if ( xRange.is() )
639 return uno::makeAny(xRange);
640 else
641 throw lang::IndexOutOfBoundsException();
644 uno::Type SAL_CALL ScNamedRangesObj::getElementType() throw(uno::RuntimeException)
646 SolarMutexGuard aGuard;
647 return ::getCppuType((const uno::Reference< sheet::XNamedRange >*)0); // muss zu getByIndex passen
650 sal_Bool SAL_CALL ScNamedRangesObj::hasElements() throw(uno::RuntimeException)
652 SolarMutexGuard aGuard;
653 return ( getCount() != 0 );
656 Reference<beans::XPropertySetInfo> SAL_CALL ScNamedRangesObj::getPropertySetInfo()
657 throw(uno::RuntimeException)
659 static Reference<beans::XPropertySetInfo> aRef(
660 new SfxItemPropertySetInfo(lcl_GetNamedRangesMap()));
661 return aRef;
664 void SAL_CALL ScNamedRangesObj::setPropertyValue(
665 const OUString& rPropertyName, const uno::Any& aValue )
666 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
667 lang::IllegalArgumentException, lang::WrappedTargetException,
668 uno::RuntimeException)
670 if ( rPropertyName == SC_UNO_MODIFY_BROADCAST )
672 aValue >>= mbModifyAndBroadcast;
676 Any SAL_CALL ScNamedRangesObj::getPropertyValue( const OUString& rPropertyName )
677 throw(beans::UnknownPropertyException, lang::WrappedTargetException,
678 uno::RuntimeException)
680 Any aRet;
681 if ( rPropertyName == SC_UNO_MODIFY_BROADCAST )
683 aRet <<= mbModifyAndBroadcast;
686 return aRet;
689 SC_IMPL_DUMMY_PROPERTY_LISTENER( ScNamedRangesObj )
691 uno::Any SAL_CALL ScNamedRangesObj::getByName( const OUString& aName )
692 throw(container::NoSuchElementException,
693 lang::WrappedTargetException, uno::RuntimeException)
695 SolarMutexGuard aGuard;
696 uno::Reference< sheet::XNamedRange > xRange(GetObjectByName_Impl(aName));
697 if ( xRange.is() )
698 return uno::makeAny(xRange);
699 else
700 throw container::NoSuchElementException();
703 uno::Sequence<OUString> SAL_CALL ScNamedRangesObj::getElementNames()
704 throw(uno::RuntimeException)
706 SolarMutexGuard aGuard;
707 if (pDocShell)
709 ScRangeName* pNames = GetRangeName_Impl();
710 if (pNames)
712 long nVisCount = getCount(); // Namen mit lcl_UserVisibleName
713 uno::Sequence<OUString> aSeq(nVisCount);
714 OUString* pAry = aSeq.getArray();
715 sal_uInt16 nVisPos = 0;
716 ScRangeName::const_iterator itr = pNames->begin(), itrEnd = pNames->end();
717 for (; itr != itrEnd; ++itr)
719 if (lcl_UserVisibleName(*itr->second))
720 pAry[nVisPos++] = itr->second->GetName();
722 return aSeq;
725 return uno::Sequence<OUString>(0);
728 sal_Bool SAL_CALL ScNamedRangesObj::hasByName( const OUString& aName )
729 throw(uno::RuntimeException)
731 SolarMutexGuard aGuard;
732 if (pDocShell)
734 ScRangeName* pNames = GetRangeName_Impl();
735 if (pNames)
737 const ScRangeData* pData = pNames->findByUpperName(ScGlobal::pCharClass->uppercase(aName));
738 if (pData && lcl_UserVisibleName(*pData))
739 return sal_True;
742 return false;
745 /** called from the XActionLockable interface methods on initial locking */
746 void ScNamedRangesObj::lock()
748 pDocShell->GetDocument()->CompileNameFormula( sal_True ); // CreateFormulaString
751 /** called from the XActionLockable interface methods on final unlock */
752 void ScNamedRangesObj::unlock()
754 pDocShell->GetDocument()->CompileNameFormula( false ); // CompileFormulaString
757 // document::XActionLockable
759 sal_Bool ScNamedRangesObj::isActionLocked() throw(uno::RuntimeException)
761 SolarMutexGuard aGuard;
762 return pDocShell->GetDocument()->GetNamedRangesLockCount() != 0;
765 void ScNamedRangesObj::addActionLock() throw(uno::RuntimeException)
767 SolarMutexGuard aGuard;
768 ScDocument* pDoc = pDocShell->GetDocument();
769 sal_Int16 nLockCount = pDoc->GetNamedRangesLockCount();
770 ++nLockCount;
771 if ( nLockCount == 1 )
773 lock();
775 pDoc->SetNamedRangesLockCount( nLockCount );
778 void ScNamedRangesObj::removeActionLock() throw(uno::RuntimeException)
780 SolarMutexGuard aGuard;
781 ScDocument* pDoc = pDocShell->GetDocument();
782 sal_Int16 nLockCount = pDoc->GetNamedRangesLockCount();
783 if ( nLockCount > 0 )
785 --nLockCount;
786 if ( nLockCount == 0 )
788 unlock();
790 pDoc->SetNamedRangesLockCount( nLockCount );
794 void ScNamedRangesObj::setActionLocks( sal_Int16 nLock ) throw(uno::RuntimeException)
796 SolarMutexGuard aGuard;
797 if ( nLock >= 0 )
799 ScDocument* pDoc = pDocShell->GetDocument();
800 sal_Int16 nLockCount = pDoc->GetNamedRangesLockCount();
801 if ( nLock == 0 && nLockCount > 0 )
803 unlock();
805 if ( nLock > 0 && nLockCount == 0 )
807 lock();
809 pDoc->SetNamedRangesLockCount( nLock );
813 sal_Int16 ScNamedRangesObj::resetActionLocks() throw(uno::RuntimeException)
815 SolarMutexGuard aGuard;
816 ScDocument* pDoc = pDocShell->GetDocument();
817 sal_Int16 nLockCount = pDoc->GetNamedRangesLockCount();
818 if ( nLockCount > 0 )
820 unlock();
822 pDoc->SetNamedRangesLockCount( 0 );
823 return nLockCount;
826 //------------------------------------------------------------------------
828 ScGlobalNamedRangesObj::ScGlobalNamedRangesObj(ScDocShell* pDocSh)
829 : ScNamedRangesObj(pDocSh)
834 ScGlobalNamedRangesObj::~ScGlobalNamedRangesObj()
839 ScNamedRangeObj* ScGlobalNamedRangesObj::GetObjectByIndex_Impl(sal_uInt16 nIndex)
841 if (!pDocShell)
842 return NULL;
844 ScRangeName* pNames = pDocShell->GetDocument()->GetRangeName();
845 if (!pNames)
846 return NULL;
848 ScRangeName::const_iterator itr = pNames->begin(), itrEnd = pNames->end();
849 sal_uInt16 nPos = 0;
850 for (; itr != itrEnd; ++itr)
852 if (lcl_UserVisibleName(*itr->second))
854 if (nPos == nIndex)
855 return new ScNamedRangeObj(this, pDocShell, itr->second->GetName());
857 ++nPos;
859 return NULL;
862 ScNamedRangeObj* ScGlobalNamedRangesObj::GetObjectByName_Impl(const OUString& aName)
864 if ( pDocShell && hasByName(aName) )
865 return new ScNamedRangeObj(this, pDocShell, aName);
866 return NULL;
869 ScRangeName* ScGlobalNamedRangesObj::GetRangeName_Impl()
871 return pDocShell->GetDocument()->GetRangeName();
874 SCTAB ScGlobalNamedRangesObj::GetTab_Impl()
876 return -1;
879 //------------------------------------------------------------------------
881 ScLocalNamedRangesObj::ScLocalNamedRangesObj( ScDocShell* pDocSh, uno::Reference<container::XNamed> xSheet )
882 : ScNamedRangesObj(pDocSh),
883 mxSheet(xSheet)
888 ScLocalNamedRangesObj::~ScLocalNamedRangesObj()
893 ScNamedRangeObj* ScLocalNamedRangesObj::GetObjectByName_Impl(const OUString& aName)
895 if ( pDocShell && hasByName( aName ) )
896 return new ScNamedRangeObj( this, pDocShell, aName, mxSheet);
897 return NULL;
901 ScNamedRangeObj* ScLocalNamedRangesObj::GetObjectByIndex_Impl( sal_uInt16 nIndex )
903 if (!pDocShell)
904 return NULL;
906 OUString aName = mxSheet->getName();
907 ScDocument* pDoc = pDocShell->GetDocument();
908 SCTAB nTab;
909 pDoc->GetTable( aName, nTab );
911 ScRangeName* pNames = pDoc->GetRangeName( nTab );
912 if (!pNames)
913 return NULL;
915 ScRangeName::const_iterator itr = pNames->begin(), itrEnd = pNames->end();
916 sal_uInt16 nPos = 0;
917 for (; itr != itrEnd; ++itr)
919 if (lcl_UserVisibleName(*itr->second))
921 if (nPos == nIndex)
922 return new ScNamedRangeObj(this, pDocShell, itr->second->GetName(), mxSheet);
924 ++nPos;
926 return NULL;
929 ScRangeName* ScLocalNamedRangesObj::GetRangeName_Impl()
931 SCTAB nTab = GetTab_Impl();
932 return pDocShell->GetDocument()->GetRangeName( nTab );
935 SCTAB ScLocalNamedRangesObj::GetTab_Impl()
937 SCTAB nTab;
938 pDocShell->GetDocument()->GetTable(mxSheet->getName(), nTab);
939 return nTab;
942 //------------------------------------------------------------------------
944 ScLabelRangeObj::ScLabelRangeObj(ScDocShell* pDocSh, sal_Bool bCol, const ScRange& rR) :
945 pDocShell( pDocSh ),
946 bColumn( bCol ),
947 aRange( rR )
949 pDocShell->GetDocument()->AddUnoObject(*this);
952 ScLabelRangeObj::~ScLabelRangeObj()
954 if (pDocShell)
955 pDocShell->GetDocument()->RemoveUnoObject(*this);
958 void ScLabelRangeObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
960 //! Ref-Update !!!
962 if ( rHint.ISA( SfxSimpleHint ) && ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
963 pDocShell = NULL; // ungueltig geworden
966 // Hilfsfuntionen
968 ScRangePair* ScLabelRangeObj::GetData_Impl()
970 ScRangePair* pRet = NULL;
971 if (pDocShell)
973 ScDocument* pDoc = pDocShell->GetDocument();
974 ScRangePairList* pList = bColumn ? pDoc->GetColNameRanges() : pDoc->GetRowNameRanges();
975 if (pList)
976 pRet = pList->Find( aRange );
978 return pRet;
981 void ScLabelRangeObj::Modify_Impl( const ScRange* pLabel, const ScRange* pData )
983 if (pDocShell)
985 ScDocument* pDoc = pDocShell->GetDocument();
986 ScRangePairList* pOldList = bColumn ? pDoc->GetColNameRanges() : pDoc->GetRowNameRanges();
987 if (pOldList)
989 ScRangePairListRef xNewList(pOldList->Clone());
990 ScRangePair* pEntry = xNewList->Find( aRange );
991 if (pEntry)
993 xNewList->Remove( pEntry ); // nur aus der Liste entfernt, nicht geloescht
995 if ( pLabel )
996 pEntry->GetRange(0) = *pLabel;
997 if ( pData )
998 pEntry->GetRange(1) = *pData;
1000 xNewList->Join( *pEntry );
1001 delete pEntry;
1003 if (bColumn)
1004 pDoc->GetColNameRangesRef() = xNewList;
1005 else
1006 pDoc->GetRowNameRangesRef() = xNewList;
1008 pDoc->CompileColRowNameFormula();
1009 pDocShell->PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID );
1010 pDocShell->SetDocumentModified();
1012 //! Undo ?!?! (hier und aus Dialog)
1014 if ( pLabel )
1015 aRange = *pLabel; // Objekt anpassen, um Range wiederzufinden
1021 // sheet::XLabelRange
1023 table::CellRangeAddress SAL_CALL ScLabelRangeObj::getLabelArea()
1024 throw(uno::RuntimeException)
1026 SolarMutexGuard aGuard;
1027 table::CellRangeAddress aRet;
1028 ScRangePair* pData = GetData_Impl();
1029 if (pData)
1030 ScUnoConversion::FillApiRange( aRet, pData->GetRange(0) );
1031 return aRet;
1034 void SAL_CALL ScLabelRangeObj::setLabelArea( const table::CellRangeAddress& aLabelArea )
1035 throw(uno::RuntimeException)
1037 SolarMutexGuard aGuard;
1038 ScRange aLabelRange;
1039 ScUnoConversion::FillScRange( aLabelRange, aLabelArea );
1040 Modify_Impl( &aLabelRange, NULL );
1043 table::CellRangeAddress SAL_CALL ScLabelRangeObj::getDataArea()
1044 throw(uno::RuntimeException)
1046 SolarMutexGuard aGuard;
1047 table::CellRangeAddress aRet;
1048 ScRangePair* pData = GetData_Impl();
1049 if (pData)
1050 ScUnoConversion::FillApiRange( aRet, pData->GetRange(1) );
1051 return aRet;
1054 void SAL_CALL ScLabelRangeObj::setDataArea( const table::CellRangeAddress& aDataArea )
1055 throw(uno::RuntimeException)
1057 SolarMutexGuard aGuard;
1058 ScRange aDataRange;
1059 ScUnoConversion::FillScRange( aDataRange, aDataArea );
1060 Modify_Impl( NULL, &aDataRange );
1063 //------------------------------------------------------------------------
1065 ScLabelRangesObj::ScLabelRangesObj(ScDocShell* pDocSh, sal_Bool bCol) :
1066 pDocShell( pDocSh ),
1067 bColumn( bCol )
1069 pDocShell->GetDocument()->AddUnoObject(*this);
1072 ScLabelRangesObj::~ScLabelRangesObj()
1074 if (pDocShell)
1075 pDocShell->GetDocument()->RemoveUnoObject(*this);
1078 void ScLabelRangesObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
1080 // Referenz-Update interessiert hier nicht
1082 if ( rHint.ISA( SfxSimpleHint ) &&
1083 ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
1085 pDocShell = NULL; // ungueltig geworden
1089 // sheet::XLabelRanges
1091 ScLabelRangeObj* ScLabelRangesObj::GetObjectByIndex_Impl(size_t nIndex)
1093 if (pDocShell)
1095 ScDocument* pDoc = pDocShell->GetDocument();
1096 ScRangePairList* pList = bColumn ? pDoc->GetColNameRanges() : pDoc->GetRowNameRanges();
1097 if ( pList && nIndex < pList->size() )
1099 ScRangePair* pData = (*pList)[nIndex];
1100 if (pData)
1101 return new ScLabelRangeObj( pDocShell, bColumn, pData->GetRange(0) );
1104 return NULL;
1107 void SAL_CALL ScLabelRangesObj::addNew( const table::CellRangeAddress& aLabelArea,
1108 const table::CellRangeAddress& aDataArea )
1109 throw(uno::RuntimeException)
1111 SolarMutexGuard aGuard;
1112 if (pDocShell)
1114 ScDocument* pDoc = pDocShell->GetDocument();
1115 ScRangePairList* pOldList = bColumn ? pDoc->GetColNameRanges() : pDoc->GetRowNameRanges();
1116 if (pOldList)
1118 ScRangePairListRef xNewList(pOldList->Clone());
1120 ScRange aLabelRange;
1121 ScRange aDataRange;
1122 ScUnoConversion::FillScRange( aLabelRange, aLabelArea );
1123 ScUnoConversion::FillScRange( aDataRange, aDataArea );
1124 xNewList->Join( ScRangePair( aLabelRange, aDataRange ) );
1126 if (bColumn)
1127 pDoc->GetColNameRangesRef() = xNewList;
1128 else
1129 pDoc->GetRowNameRangesRef() = xNewList;
1131 pDoc->CompileColRowNameFormula();
1132 pDocShell->PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID );
1133 pDocShell->SetDocumentModified();
1135 //! Undo ?!?! (hier und aus Dialog)
1140 void SAL_CALL ScLabelRangesObj::removeByIndex( sal_Int32 nIndex )
1141 throw(uno::RuntimeException)
1143 SolarMutexGuard aGuard;
1144 sal_Bool bDone = false;
1145 if (pDocShell)
1147 ScDocument* pDoc = pDocShell->GetDocument();
1148 ScRangePairList* pOldList = bColumn ? pDoc->GetColNameRanges() : pDoc->GetRowNameRanges();
1150 if ( pOldList && nIndex >= 0 && nIndex < (sal_Int32)pOldList->size() )
1152 ScRangePairListRef xNewList(pOldList->Clone());
1154 ScRangePair* pEntry = (*xNewList)[nIndex];
1155 if (pEntry)
1157 xNewList->Remove( pEntry );
1158 delete pEntry;
1160 if (bColumn)
1161 pDoc->GetColNameRangesRef() = xNewList;
1162 else
1163 pDoc->GetRowNameRangesRef() = xNewList;
1165 pDoc->CompileColRowNameFormula();
1166 pDocShell->PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID );
1167 pDocShell->SetDocumentModified();
1168 bDone = sal_True;
1170 //! Undo ?!?! (hier und aus Dialog)
1174 if (!bDone)
1175 throw uno::RuntimeException(); // no other exceptions specified
1178 // container::XEnumerationAccess
1180 uno::Reference<container::XEnumeration> SAL_CALL ScLabelRangesObj::createEnumeration()
1181 throw(uno::RuntimeException)
1183 SolarMutexGuard aGuard;
1184 return new ScIndexEnumeration(this, OUString("com.sun.star.sheet.LabelRangesEnumeration"));
1187 // container::XIndexAccess
1189 sal_Int32 SAL_CALL ScLabelRangesObj::getCount() throw(uno::RuntimeException)
1191 SolarMutexGuard aGuard;
1192 if (pDocShell)
1194 ScDocument* pDoc = pDocShell->GetDocument();
1195 ScRangePairList* pList = bColumn ? pDoc->GetColNameRanges() : pDoc->GetRowNameRanges();
1196 if (pList)
1197 return pList->size();
1199 return 0;
1202 uno::Any SAL_CALL ScLabelRangesObj::getByIndex( sal_Int32 nIndex )
1203 throw(lang::IndexOutOfBoundsException,
1204 lang::WrappedTargetException, uno::RuntimeException)
1206 SolarMutexGuard aGuard;
1207 uno::Reference< sheet::XLabelRange > xRange(GetObjectByIndex_Impl((sal_uInt16)nIndex));
1208 if ( xRange.is() )
1209 return uno::makeAny(xRange);
1210 else
1211 throw lang::IndexOutOfBoundsException();
1214 uno::Type SAL_CALL ScLabelRangesObj::getElementType() throw(uno::RuntimeException)
1216 SolarMutexGuard aGuard;
1217 return ::getCppuType((const uno::Reference< sheet::XLabelRange >*)0); // muss zu getByIndex passen
1221 sal_Bool SAL_CALL ScLabelRangesObj::hasElements() throw(uno::RuntimeException)
1223 SolarMutexGuard aGuard;
1224 return ( getCount() != 0 );
1227 //------------------------------------------------------------------------
1231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */