bump product version to 4.1.6.2
[LibreOffice.git] / sc / source / ui / unoobj / nameuno.cxx
blob659f6a49117f6d7bc241a87dbdc2239e77bd1482
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 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 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) && !rData.HasType(RT_SHARED);
88 ScNamedRangeObj::ScNamedRangeObj( rtl::Reference< ScNamedRangesObj > xParent, ScDocShell* pDocSh, const String& 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 String* pNewName, const ScTokenArray* pNewTokens, const String* 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 String aInsName = pOld->GetName();
176 if (pNewName)
177 aInsName = *pNewName;
179 String 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 String 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 String 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 String 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 bool bIsShared = false;
390 if( aValue >>= bIsShared )
392 sal_uInt16 nNewType = bIsShared ? RT_SHARED : RT_NAME;
393 Modify_Impl( NULL, NULL, NULL, NULL, &nNewType,formula::FormulaGrammar::GRAM_PODF_A1 );
398 uno::Any SAL_CALL ScNamedRangeObj::getPropertyValue( const OUString& rPropertyName )
399 throw(beans::UnknownPropertyException, lang::WrappedTargetException,
400 uno::RuntimeException)
402 SolarMutexGuard aGuard;
403 uno::Any aRet;
404 if ( rPropertyName == SC_UNO_LINKDISPBIT )
406 // no target bitmaps for individual entries (would be all equal)
407 // ScLinkTargetTypeObj::SetLinkTargetBitmap( aRet, SC_LINKTARGETTYPE_RANGENAME );
409 else if ( rPropertyName == SC_UNO_LINKDISPNAME )
410 aRet <<= OUString( aName );
411 else if ( rPropertyName == SC_UNONAME_TOKENINDEX )
413 // get index for use in formula tokens (read-only)
414 ScRangeData* pData = GetRangeData_Impl();
415 if (pData)
416 aRet <<= static_cast<sal_Int32>(pData->GetIndex());
418 else if ( rPropertyName == SC_UNONAME_ISSHAREDFMLA )
420 if( ScRangeData* pData = GetRangeData_Impl() )
421 aRet <<= static_cast< bool >( pData->HasType( RT_SHARED ) );
423 return aRet;
426 SC_IMPL_DUMMY_PROPERTY_LISTENER( ScNamedRangeObj )
428 // lang::XServiceInfo
430 OUString SAL_CALL ScNamedRangeObj::getImplementationName() throw(uno::RuntimeException)
432 return OUString( "ScNamedRangeObj" );
435 sal_Bool SAL_CALL ScNamedRangeObj::supportsService( const OUString& rServiceName )
436 throw(uno::RuntimeException)
438 return rServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( SCNAMEDRANGEOBJ_SERVICE ) ) ||
439 rServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( SCLINKTARGET_SERVICE ) );
442 uno::Sequence<OUString> SAL_CALL ScNamedRangeObj::getSupportedServiceNames()
443 throw(uno::RuntimeException)
445 uno::Sequence<OUString> aRet(2);
446 aRet[0] = OUString( SCNAMEDRANGEOBJ_SERVICE );
447 aRet[1] = OUString( SCLINKTARGET_SERVICE );
448 return aRet;
452 // XUnoTunnel
454 sal_Int64 SAL_CALL ScNamedRangeObj::getSomething(
455 const uno::Sequence<sal_Int8 >& rId ) throw(uno::RuntimeException)
457 if ( rId.getLength() == 16 &&
458 0 == memcmp( getUnoTunnelId().getConstArray(),
459 rId.getConstArray(), 16 ) )
461 return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_IntPtr>(this));
463 return 0;
466 namespace
468 class theScNamedRangeObjUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theScNamedRangeObjUnoTunnelId> {};
471 const uno::Sequence<sal_Int8>& ScNamedRangeObj::getUnoTunnelId()
473 return theScNamedRangeObjUnoTunnelId::get().getSeq();
476 //------------------------------------------------------------------------
478 ScNamedRangesObj::ScNamedRangesObj(ScDocShell* pDocSh) :
479 mbModifyAndBroadcast(true),
480 pDocShell( pDocSh )
482 pDocShell->GetDocument()->AddUnoObject(*this);
485 ScNamedRangesObj::~ScNamedRangesObj()
487 if (pDocShell)
488 pDocShell->GetDocument()->RemoveUnoObject(*this);
491 void ScNamedRangesObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
493 // Referenz-Update interessiert hier nicht
495 if ( rHint.ISA( SfxSimpleHint ) &&
496 ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
498 pDocShell = NULL; // ungueltig geworden
502 bool ScNamedRangesObj::IsModifyAndBroadcast() const
504 return mbModifyAndBroadcast;
507 // sheet::XNamedRanges
509 void SAL_CALL ScNamedRangesObj::addNewByName( const OUString& aName,
510 const OUString& aContent, const table::CellAddress& aPosition,
511 sal_Int32 nUnoType ) throw(uno::RuntimeException)
513 SolarMutexGuard aGuard;
514 ScAddress aPos( (SCCOL)aPosition.Column, (SCROW)aPosition.Row, aPosition.Sheet );
516 sal_uInt16 nNewType = RT_NAME;
517 if ( nUnoType & sheet::NamedRangeFlag::FILTER_CRITERIA ) nNewType |= RT_CRITERIA;
518 if ( nUnoType & sheet::NamedRangeFlag::PRINT_AREA ) nNewType |= RT_PRINTAREA;
519 if ( nUnoType & sheet::NamedRangeFlag::COLUMN_HEADER ) nNewType |= RT_COLHEADER;
520 if ( nUnoType & sheet::NamedRangeFlag::ROW_HEADER ) nNewType |= RT_ROWHEADER;
522 sal_Bool bDone = false;
523 if (pDocShell)
525 ScDocument* pDoc = pDocShell->GetDocument();
526 ScRangeName* pNames = GetRangeName_Impl();
527 if (pNames && !pNames->findByUpperName(ScGlobal::pCharClass->uppercase(aName)))
529 ScRangeName* pNewRanges = new ScRangeName( *pNames );
530 // GRAM_PODF_A1 for API compatibility.
531 ScRangeData* pNew = new ScRangeData( pDoc, aName, aContent,
532 aPos, nNewType,formula::FormulaGrammar::GRAM_PODF_A1 );
533 if ( pNewRanges->insert(pNew) )
535 pDocShell->GetDocFunc().SetNewRangeNames(pNewRanges, mbModifyAndBroadcast, GetTab_Impl());
536 bDone = true;
538 else
540 pNew = NULL;
541 delete pNewRanges;
546 if (!bDone)
547 throw uno::RuntimeException(); // no other exceptions specified
550 void SAL_CALL ScNamedRangesObj::addNewFromTitles( const table::CellRangeAddress& aSource,
551 sheet::Border aBorder ) throw(uno::RuntimeException)
553 SolarMutexGuard aGuard;
554 //! das darf kein enum sein, weil mehrere Bits gesetzt sein koennen !!!
556 sal_Bool bTop = ( aBorder == sheet::Border_TOP );
557 sal_Bool bLeft = ( aBorder == sheet::Border_LEFT );
558 sal_Bool bBottom = ( aBorder == sheet::Border_BOTTOM );
559 sal_Bool bRight = ( aBorder == sheet::Border_RIGHT );
561 ScRange aRange;
562 ScUnoConversion::FillScRange( aRange, aSource );
564 sal_uInt16 nFlags = 0;
565 if (bTop) nFlags |= NAME_TOP;
566 if (bLeft) nFlags |= NAME_LEFT;
567 if (bBottom) nFlags |= NAME_BOTTOM;
568 if (bRight) nFlags |= NAME_RIGHT;
570 if (nFlags)
571 pDocShell->GetDocFunc().CreateNames( aRange, nFlags, sal_True, GetTab_Impl() );
574 void SAL_CALL ScNamedRangesObj::removeByName( const OUString& aName )
575 throw(uno::RuntimeException)
577 SolarMutexGuard aGuard;
578 bool bDone = false;
579 if (pDocShell)
581 ScRangeName* pNames = GetRangeName_Impl();
582 if (pNames)
584 const ScRangeData* pData = pNames->findByUpperName(ScGlobal::pCharClass->uppercase(aName));
585 if (pData && lcl_UserVisibleName(*pData))
587 ScRangeName* pNewRanges = new ScRangeName(*pNames);
588 pNewRanges->erase(*pData);
589 pDocShell->GetDocFunc().SetNewRangeNames( pNewRanges, mbModifyAndBroadcast, GetTab_Impl());
590 bDone = true;
595 if (!bDone)
596 throw uno::RuntimeException(); // no other exceptions specified
599 void SAL_CALL ScNamedRangesObj::outputList( const table::CellAddress& aOutputPosition )
600 throw(uno::RuntimeException)
602 SolarMutexGuard aGuard;
603 ScAddress aPos( (SCCOL)aOutputPosition.Column, (SCROW)aOutputPosition.Row, aOutputPosition.Sheet );
604 if (pDocShell)
605 pDocShell->GetDocFunc().InsertNameList( aPos, sal_True );
608 // container::XEnumerationAccess
610 uno::Reference<container::XEnumeration> SAL_CALL ScNamedRangesObj::createEnumeration()
611 throw(uno::RuntimeException)
613 SolarMutexGuard aGuard;
614 return new ScIndexEnumeration(this, OUString("com.sun.star.sheet.NamedRangesEnumeration"));
617 // container::XIndexAccess
619 sal_Int32 SAL_CALL ScNamedRangesObj::getCount() throw(uno::RuntimeException)
621 SolarMutexGuard aGuard;
622 long nRet = 0;
623 if (pDocShell)
625 ScRangeName* pNames = GetRangeName_Impl();
626 if (pNames)
628 ScRangeName::const_iterator itr = pNames->begin(), itrEnd = pNames->end();
629 for (; itr != itrEnd; ++itr)
630 if (lcl_UserVisibleName(*itr->second))
631 ++nRet;
634 return nRet;
637 uno::Any SAL_CALL ScNamedRangesObj::getByIndex( sal_Int32 nIndex )
638 throw(lang::IndexOutOfBoundsException,
639 lang::WrappedTargetException, uno::RuntimeException)
641 SolarMutexGuard aGuard;
642 uno::Reference< sheet::XNamedRange > xRange(GetObjectByIndex_Impl((sal_uInt16)nIndex));
643 if ( xRange.is() )
644 return uno::makeAny(xRange);
645 else
646 throw lang::IndexOutOfBoundsException();
649 uno::Type SAL_CALL ScNamedRangesObj::getElementType() throw(uno::RuntimeException)
651 SolarMutexGuard aGuard;
652 return ::getCppuType((const uno::Reference< sheet::XNamedRange >*)0); // muss zu getByIndex passen
655 sal_Bool SAL_CALL ScNamedRangesObj::hasElements() throw(uno::RuntimeException)
657 SolarMutexGuard aGuard;
658 return ( getCount() != 0 );
661 Reference<beans::XPropertySetInfo> SAL_CALL ScNamedRangesObj::getPropertySetInfo()
662 throw(uno::RuntimeException)
664 static Reference<beans::XPropertySetInfo> aRef(
665 new SfxItemPropertySetInfo(lcl_GetNamedRangesMap()));
666 return aRef;
669 void SAL_CALL ScNamedRangesObj::setPropertyValue(
670 const OUString& rPropertyName, const uno::Any& aValue )
671 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
672 lang::IllegalArgumentException, lang::WrappedTargetException,
673 uno::RuntimeException)
675 if ( rPropertyName == SC_UNO_MODIFY_BROADCAST )
677 aValue >>= mbModifyAndBroadcast;
681 Any SAL_CALL ScNamedRangesObj::getPropertyValue( const OUString& rPropertyName )
682 throw(beans::UnknownPropertyException, lang::WrappedTargetException,
683 uno::RuntimeException)
685 Any aRet;
686 if ( rPropertyName == SC_UNO_MODIFY_BROADCAST )
688 aRet <<= mbModifyAndBroadcast;
691 return aRet;
694 SC_IMPL_DUMMY_PROPERTY_LISTENER( ScNamedRangesObj )
696 uno::Any SAL_CALL ScNamedRangesObj::getByName( const OUString& aName )
697 throw(container::NoSuchElementException,
698 lang::WrappedTargetException, uno::RuntimeException)
700 SolarMutexGuard aGuard;
701 uno::Reference< sheet::XNamedRange > xRange(GetObjectByName_Impl(aName));
702 if ( xRange.is() )
703 return uno::makeAny(xRange);
704 else
705 throw container::NoSuchElementException();
708 uno::Sequence<OUString> SAL_CALL ScNamedRangesObj::getElementNames()
709 throw(uno::RuntimeException)
711 SolarMutexGuard aGuard;
712 if (pDocShell)
714 ScRangeName* pNames = GetRangeName_Impl();
715 if (pNames)
717 long nVisCount = getCount(); // Namen mit lcl_UserVisibleName
718 uno::Sequence<OUString> aSeq(nVisCount);
719 OUString* pAry = aSeq.getArray();
720 sal_uInt16 nVisPos = 0;
721 ScRangeName::const_iterator itr = pNames->begin(), itrEnd = pNames->end();
722 for (; itr != itrEnd; ++itr)
724 if (lcl_UserVisibleName(*itr->second))
725 pAry[nVisPos++] = itr->second->GetName();
727 return aSeq;
730 return uno::Sequence<OUString>(0);
733 sal_Bool SAL_CALL ScNamedRangesObj::hasByName( const OUString& aName )
734 throw(uno::RuntimeException)
736 SolarMutexGuard aGuard;
737 if (pDocShell)
739 ScRangeName* pNames = GetRangeName_Impl();
740 if (pNames)
742 const ScRangeData* pData = pNames->findByUpperName(ScGlobal::pCharClass->uppercase(aName));
743 if (pData && lcl_UserVisibleName(*pData))
744 return sal_True;
747 return false;
750 /** called from the XActionLockable interface methods on initial locking */
751 void ScNamedRangesObj::lock()
753 pDocShell->GetDocument()->CompileNameFormula( sal_True ); // CreateFormulaString
756 /** called from the XActionLockable interface methods on final unlock */
757 void ScNamedRangesObj::unlock()
759 pDocShell->GetDocument()->CompileNameFormula( false ); // CompileFormulaString
762 // document::XActionLockable
764 sal_Bool ScNamedRangesObj::isActionLocked() throw(uno::RuntimeException)
766 SolarMutexGuard aGuard;
767 return pDocShell->GetDocument()->GetNamedRangesLockCount() != 0;
770 void ScNamedRangesObj::addActionLock() throw(uno::RuntimeException)
772 SolarMutexGuard aGuard;
773 ScDocument* pDoc = pDocShell->GetDocument();
774 sal_Int16 nLockCount = pDoc->GetNamedRangesLockCount();
775 ++nLockCount;
776 if ( nLockCount == 1 )
778 lock();
780 pDoc->SetNamedRangesLockCount( nLockCount );
783 void ScNamedRangesObj::removeActionLock() throw(uno::RuntimeException)
785 SolarMutexGuard aGuard;
786 ScDocument* pDoc = pDocShell->GetDocument();
787 sal_Int16 nLockCount = pDoc->GetNamedRangesLockCount();
788 if ( nLockCount > 0 )
790 --nLockCount;
791 if ( nLockCount == 0 )
793 unlock();
795 pDoc->SetNamedRangesLockCount( nLockCount );
799 void ScNamedRangesObj::setActionLocks( sal_Int16 nLock ) throw(uno::RuntimeException)
801 SolarMutexGuard aGuard;
802 if ( nLock >= 0 )
804 ScDocument* pDoc = pDocShell->GetDocument();
805 sal_Int16 nLockCount = pDoc->GetNamedRangesLockCount();
806 if ( nLock == 0 && nLockCount > 0 )
808 unlock();
810 if ( nLock > 0 && nLockCount == 0 )
812 lock();
814 pDoc->SetNamedRangesLockCount( nLock );
818 sal_Int16 ScNamedRangesObj::resetActionLocks() throw(uno::RuntimeException)
820 SolarMutexGuard aGuard;
821 ScDocument* pDoc = pDocShell->GetDocument();
822 sal_Int16 nLockCount = pDoc->GetNamedRangesLockCount();
823 if ( nLockCount > 0 )
825 unlock();
827 pDoc->SetNamedRangesLockCount( 0 );
828 return nLockCount;
831 //------------------------------------------------------------------------
833 ScGlobalNamedRangesObj::ScGlobalNamedRangesObj(ScDocShell* pDocSh)
834 : ScNamedRangesObj(pDocSh)
839 ScGlobalNamedRangesObj::~ScGlobalNamedRangesObj()
844 ScNamedRangeObj* ScGlobalNamedRangesObj::GetObjectByIndex_Impl(sal_uInt16 nIndex)
846 if (!pDocShell)
847 return NULL;
849 ScRangeName* pNames = pDocShell->GetDocument()->GetRangeName();
850 if (!pNames)
851 return NULL;
853 ScRangeName::const_iterator itr = pNames->begin(), itrEnd = pNames->end();
854 sal_uInt16 nPos = 0;
855 for (; itr != itrEnd; ++itr)
857 if (lcl_UserVisibleName(*itr->second))
859 if (nPos == nIndex)
860 return new ScNamedRangeObj(this, pDocShell, itr->second->GetName());
862 ++nPos;
864 return NULL;
867 ScNamedRangeObj* ScGlobalNamedRangesObj::GetObjectByName_Impl(const OUString& aName)
869 if ( pDocShell && hasByName(aName) )
870 return new ScNamedRangeObj(this, pDocShell, String(aName));
871 return NULL;
874 ScRangeName* ScGlobalNamedRangesObj::GetRangeName_Impl()
876 return pDocShell->GetDocument()->GetRangeName();
879 SCTAB ScGlobalNamedRangesObj::GetTab_Impl()
881 return -1;
884 //------------------------------------------------------------------------
886 ScLocalNamedRangesObj::ScLocalNamedRangesObj( ScDocShell* pDocSh, uno::Reference<container::XNamed> xSheet )
887 : ScNamedRangesObj(pDocSh),
888 mxSheet(xSheet)
893 ScLocalNamedRangesObj::~ScLocalNamedRangesObj()
898 ScNamedRangeObj* ScLocalNamedRangesObj::GetObjectByName_Impl(const OUString& aName)
900 if ( pDocShell && hasByName( aName ) )
901 return new ScNamedRangeObj( this, pDocShell, String(aName), mxSheet);
902 return NULL;
906 ScNamedRangeObj* ScLocalNamedRangesObj::GetObjectByIndex_Impl( sal_uInt16 nIndex )
908 if (!pDocShell)
909 return NULL;
911 OUString aName = mxSheet->getName();
912 ScDocument* pDoc = pDocShell->GetDocument();
913 SCTAB nTab;
914 pDoc->GetTable( aName, nTab );
916 ScRangeName* pNames = pDoc->GetRangeName( nTab );
917 if (!pNames)
918 return NULL;
920 ScRangeName::const_iterator itr = pNames->begin(), itrEnd = pNames->end();
921 sal_uInt16 nPos = 0;
922 for (; itr != itrEnd; ++itr)
924 if (lcl_UserVisibleName(*itr->second))
926 if (nPos == nIndex)
927 return new ScNamedRangeObj(this, pDocShell, itr->second->GetName(), mxSheet);
929 ++nPos;
931 return NULL;
934 ScRangeName* ScLocalNamedRangesObj::GetRangeName_Impl()
936 SCTAB nTab = GetTab_Impl();
937 return pDocShell->GetDocument()->GetRangeName( nTab );
940 SCTAB ScLocalNamedRangesObj::GetTab_Impl()
942 SCTAB nTab;
943 pDocShell->GetDocument()->GetTable(mxSheet->getName(), nTab);
944 return nTab;
947 //------------------------------------------------------------------------
949 ScLabelRangeObj::ScLabelRangeObj(ScDocShell* pDocSh, sal_Bool bCol, const ScRange& rR) :
950 pDocShell( pDocSh ),
951 bColumn( bCol ),
952 aRange( rR )
954 pDocShell->GetDocument()->AddUnoObject(*this);
957 ScLabelRangeObj::~ScLabelRangeObj()
959 if (pDocShell)
960 pDocShell->GetDocument()->RemoveUnoObject(*this);
963 void ScLabelRangeObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
965 //! Ref-Update !!!
967 if ( rHint.ISA( SfxSimpleHint ) && ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
968 pDocShell = NULL; // ungueltig geworden
971 // Hilfsfuntionen
973 ScRangePair* ScLabelRangeObj::GetData_Impl()
975 ScRangePair* pRet = NULL;
976 if (pDocShell)
978 ScDocument* pDoc = pDocShell->GetDocument();
979 ScRangePairList* pList = bColumn ? pDoc->GetColNameRanges() : pDoc->GetRowNameRanges();
980 if (pList)
981 pRet = pList->Find( aRange );
983 return pRet;
986 void ScLabelRangeObj::Modify_Impl( const ScRange* pLabel, const ScRange* pData )
988 if (pDocShell)
990 ScDocument* pDoc = pDocShell->GetDocument();
991 ScRangePairList* pOldList = bColumn ? pDoc->GetColNameRanges() : pDoc->GetRowNameRanges();
992 if (pOldList)
994 ScRangePairListRef xNewList(pOldList->Clone());
995 ScRangePair* pEntry = xNewList->Find( aRange );
996 if (pEntry)
998 xNewList->Remove( pEntry ); // nur aus der Liste entfernt, nicht geloescht
1000 if ( pLabel )
1001 pEntry->GetRange(0) = *pLabel;
1002 if ( pData )
1003 pEntry->GetRange(1) = *pData;
1005 xNewList->Join( *pEntry );
1006 delete pEntry;
1008 if (bColumn)
1009 pDoc->GetColNameRangesRef() = xNewList;
1010 else
1011 pDoc->GetRowNameRangesRef() = xNewList;
1013 pDoc->CompileColRowNameFormula();
1014 pDocShell->PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID );
1015 pDocShell->SetDocumentModified();
1017 //! Undo ?!?! (hier und aus Dialog)
1019 if ( pLabel )
1020 aRange = *pLabel; // Objekt anpassen, um Range wiederzufinden
1026 // sheet::XLabelRange
1028 table::CellRangeAddress SAL_CALL ScLabelRangeObj::getLabelArea()
1029 throw(uno::RuntimeException)
1031 SolarMutexGuard aGuard;
1032 table::CellRangeAddress aRet;
1033 ScRangePair* pData = GetData_Impl();
1034 if (pData)
1035 ScUnoConversion::FillApiRange( aRet, pData->GetRange(0) );
1036 return aRet;
1039 void SAL_CALL ScLabelRangeObj::setLabelArea( const table::CellRangeAddress& aLabelArea )
1040 throw(uno::RuntimeException)
1042 SolarMutexGuard aGuard;
1043 ScRange aLabelRange;
1044 ScUnoConversion::FillScRange( aLabelRange, aLabelArea );
1045 Modify_Impl( &aLabelRange, NULL );
1048 table::CellRangeAddress SAL_CALL ScLabelRangeObj::getDataArea()
1049 throw(uno::RuntimeException)
1051 SolarMutexGuard aGuard;
1052 table::CellRangeAddress aRet;
1053 ScRangePair* pData = GetData_Impl();
1054 if (pData)
1055 ScUnoConversion::FillApiRange( aRet, pData->GetRange(1) );
1056 return aRet;
1059 void SAL_CALL ScLabelRangeObj::setDataArea( const table::CellRangeAddress& aDataArea )
1060 throw(uno::RuntimeException)
1062 SolarMutexGuard aGuard;
1063 ScRange aDataRange;
1064 ScUnoConversion::FillScRange( aDataRange, aDataArea );
1065 Modify_Impl( NULL, &aDataRange );
1068 //------------------------------------------------------------------------
1070 ScLabelRangesObj::ScLabelRangesObj(ScDocShell* pDocSh, sal_Bool bCol) :
1071 pDocShell( pDocSh ),
1072 bColumn( bCol )
1074 pDocShell->GetDocument()->AddUnoObject(*this);
1077 ScLabelRangesObj::~ScLabelRangesObj()
1079 if (pDocShell)
1080 pDocShell->GetDocument()->RemoveUnoObject(*this);
1083 void ScLabelRangesObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
1085 // Referenz-Update interessiert hier nicht
1087 if ( rHint.ISA( SfxSimpleHint ) &&
1088 ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
1090 pDocShell = NULL; // ungueltig geworden
1094 // sheet::XLabelRanges
1096 ScLabelRangeObj* ScLabelRangesObj::GetObjectByIndex_Impl(size_t nIndex)
1098 if (pDocShell)
1100 ScDocument* pDoc = pDocShell->GetDocument();
1101 ScRangePairList* pList = bColumn ? pDoc->GetColNameRanges() : pDoc->GetRowNameRanges();
1102 if ( pList && nIndex < pList->size() )
1104 ScRangePair* pData = (*pList)[nIndex];
1105 if (pData)
1106 return new ScLabelRangeObj( pDocShell, bColumn, pData->GetRange(0) );
1109 return NULL;
1112 void SAL_CALL ScLabelRangesObj::addNew( const table::CellRangeAddress& aLabelArea,
1113 const table::CellRangeAddress& aDataArea )
1114 throw(uno::RuntimeException)
1116 SolarMutexGuard aGuard;
1117 if (pDocShell)
1119 ScDocument* pDoc = pDocShell->GetDocument();
1120 ScRangePairList* pOldList = bColumn ? pDoc->GetColNameRanges() : pDoc->GetRowNameRanges();
1121 if (pOldList)
1123 ScRangePairListRef xNewList(pOldList->Clone());
1125 ScRange aLabelRange;
1126 ScRange aDataRange;
1127 ScUnoConversion::FillScRange( aLabelRange, aLabelArea );
1128 ScUnoConversion::FillScRange( aDataRange, aDataArea );
1129 xNewList->Join( ScRangePair( aLabelRange, aDataRange ) );
1131 if (bColumn)
1132 pDoc->GetColNameRangesRef() = xNewList;
1133 else
1134 pDoc->GetRowNameRangesRef() = xNewList;
1136 pDoc->CompileColRowNameFormula();
1137 pDocShell->PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID );
1138 pDocShell->SetDocumentModified();
1140 //! Undo ?!?! (hier und aus Dialog)
1145 void SAL_CALL ScLabelRangesObj::removeByIndex( sal_Int32 nIndex )
1146 throw(uno::RuntimeException)
1148 SolarMutexGuard aGuard;
1149 sal_Bool bDone = false;
1150 if (pDocShell)
1152 ScDocument* pDoc = pDocShell->GetDocument();
1153 ScRangePairList* pOldList = bColumn ? pDoc->GetColNameRanges() : pDoc->GetRowNameRanges();
1155 if ( pOldList && nIndex >= 0 && nIndex < (sal_Int32)pOldList->size() )
1157 ScRangePairListRef xNewList(pOldList->Clone());
1159 ScRangePair* pEntry = (*xNewList)[nIndex];
1160 if (pEntry)
1162 xNewList->Remove( pEntry );
1163 delete pEntry;
1165 if (bColumn)
1166 pDoc->GetColNameRangesRef() = xNewList;
1167 else
1168 pDoc->GetRowNameRangesRef() = xNewList;
1170 pDoc->CompileColRowNameFormula();
1171 pDocShell->PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID );
1172 pDocShell->SetDocumentModified();
1173 bDone = sal_True;
1175 //! Undo ?!?! (hier und aus Dialog)
1179 if (!bDone)
1180 throw uno::RuntimeException(); // no other exceptions specified
1183 // container::XEnumerationAccess
1185 uno::Reference<container::XEnumeration> SAL_CALL ScLabelRangesObj::createEnumeration()
1186 throw(uno::RuntimeException)
1188 SolarMutexGuard aGuard;
1189 return new ScIndexEnumeration(this, OUString("com.sun.star.sheet.LabelRangesEnumeration"));
1192 // container::XIndexAccess
1194 sal_Int32 SAL_CALL ScLabelRangesObj::getCount() throw(uno::RuntimeException)
1196 SolarMutexGuard aGuard;
1197 if (pDocShell)
1199 ScDocument* pDoc = pDocShell->GetDocument();
1200 ScRangePairList* pList = bColumn ? pDoc->GetColNameRanges() : pDoc->GetRowNameRanges();
1201 if (pList)
1202 return pList->size();
1204 return 0;
1207 uno::Any SAL_CALL ScLabelRangesObj::getByIndex( sal_Int32 nIndex )
1208 throw(lang::IndexOutOfBoundsException,
1209 lang::WrappedTargetException, uno::RuntimeException)
1211 SolarMutexGuard aGuard;
1212 uno::Reference< sheet::XLabelRange > xRange(GetObjectByIndex_Impl((sal_uInt16)nIndex));
1213 if ( xRange.is() )
1214 return uno::makeAny(xRange);
1215 else
1216 throw lang::IndexOutOfBoundsException();
1219 uno::Type SAL_CALL ScLabelRangesObj::getElementType() throw(uno::RuntimeException)
1221 SolarMutexGuard aGuard;
1222 return ::getCppuType((const uno::Reference< sheet::XLabelRange >*)0); // muss zu getByIndex passen
1226 sal_Bool SAL_CALL ScLabelRangesObj::hasElements() throw(uno::RuntimeException)
1228 SolarMutexGuard aGuard;
1229 return ( getCount() != 0 );
1232 //------------------------------------------------------------------------
1236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */